1//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Expr constant evaluator.
10//
11// Constant expression evaluation produces four main results:
12//
13// * A success/failure flag indicating whether constant folding was successful.
14// This is the 'bool' return value used by most of the code in this file. A
15// 'false' return value indicates that constant folding has failed, and any
16// appropriate diagnostic has already been produced.
17//
18// * An evaluated result, valid only if constant folding has not failed.
19//
20// * A flag indicating if evaluation encountered (unevaluated) side-effects.
21// These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1),
22// where it is possible to determine the evaluated result regardless.
23//
24// * A set of notes indicating why the evaluation was not a constant expression
25// (under the C++11 / C++1y rules only, at the moment), or, if folding failed
26// too, why the expression could not be folded.
27//
28// If we are checking for a potential constant expression, failure to constant
29// fold a potential constant sub-expression will be indicated by a 'false'
30// return value (the expression could not be folded) and no diagnostic (the
31// expression is not necessarily non-constant).
32//
33//===----------------------------------------------------------------------===//
34
35#include "ByteCode/Context.h"
36#include "ByteCode/Frame.h"
37#include "ByteCode/State.h"
38#include "ExprConstShared.h"
39#include "clang/AST/APValue.h"
40#include "clang/AST/ASTContext.h"
41#include "clang/AST/ASTLambda.h"
42#include "clang/AST/Attr.h"
43#include "clang/AST/CXXInheritance.h"
44#include "clang/AST/CharUnits.h"
45#include "clang/AST/ComparisonCategories.h"
46#include "clang/AST/CurrentSourceLocExprScope.h"
47#include "clang/AST/Expr.h"
48#include "clang/AST/InferAlloc.h"
49#include "clang/AST/OSLog.h"
50#include "clang/AST/OptionalDiagnostic.h"
51#include "clang/AST/RecordLayout.h"
52#include "clang/AST/StmtVisitor.h"
53#include "clang/AST/Type.h"
54#include "clang/AST/TypeLoc.h"
55#include "clang/Basic/Builtins.h"
56#include "clang/Basic/DiagnosticSema.h"
57#include "clang/Basic/TargetBuiltins.h"
58#include "clang/Basic/TargetInfo.h"
59#include "llvm/ADT/APFixedPoint.h"
60#include "llvm/ADT/Sequence.h"
61#include "llvm/ADT/SmallBitVector.h"
62#include "llvm/ADT/StringExtras.h"
63#include "llvm/Support/Casting.h"
64#include "llvm/Support/Debug.h"
65#include "llvm/Support/SaveAndRestore.h"
66#include "llvm/Support/SipHash.h"
67#include "llvm/Support/TimeProfiler.h"
68#include "llvm/Support/raw_ostream.h"
69#include <cstring>
70#include <functional>
71#include <limits>
72#include <optional>
73
74#define DEBUG_TYPE "exprconstant"
75
76using namespace clang;
77using llvm::APFixedPoint;
78using llvm::APInt;
79using llvm::APSInt;
80using llvm::APFloat;
81using llvm::FixedPointSemantics;
82
83namespace {
84 struct LValue;
85 class CallStackFrame;
86 class EvalInfo;
87
88 using SourceLocExprScopeGuard =
89 CurrentSourceLocExprScope::SourceLocExprScopeGuard;
90
91 static QualType getType(APValue::LValueBase B) {
92 return B.getType();
93 }
94
95 /// Get an LValue path entry, which is known to not be an array index, as a
96 /// field declaration.
97 static const FieldDecl *getAsField(APValue::LValuePathEntry E) {
98 return dyn_cast_or_null<FieldDecl>(Val: E.getAsBaseOrMember().getPointer());
99 }
100 /// Get an LValue path entry, which is known to not be an array index, as a
101 /// base class declaration.
102 static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
103 return dyn_cast_or_null<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer());
104 }
105 /// Determine whether this LValue path entry for a base class names a virtual
106 /// base class.
107 static bool isVirtualBaseClass(APValue::LValuePathEntry E) {
108 return E.getAsBaseOrMember().getInt();
109 }
110
111 /// Given an expression, determine the type used to store the result of
112 /// evaluating that expression.
113 static QualType getStorageType(const ASTContext &Ctx, const Expr *E) {
114 if (E->isPRValue())
115 return E->getType();
116 return Ctx.getLValueReferenceType(T: E->getType());
117 }
118
119 static unsigned countNonVirtualBases(const CXXRecordDecl *RD) {
120 return llvm::count_if(Range: RD->bases(), P: [](auto &B) { return !B.isVirtual(); });
121 }
122
123 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
124 /// This will look through a single cast.
125 ///
126 /// Returns null if we couldn't unwrap a function with alloc_size.
127 static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) {
128 if (!E->getType()->isPointerType())
129 return nullptr;
130
131 E = E->IgnoreParens();
132 // If we're doing a variable assignment from e.g. malloc(N), there will
133 // probably be a cast of some kind. In exotic cases, we might also see a
134 // top-level ExprWithCleanups. Ignore them either way.
135 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
136 E = FE->getSubExpr()->IgnoreParens();
137
138 if (const auto *Cast = dyn_cast<CastExpr>(Val: E))
139 E = Cast->getSubExpr()->IgnoreParens();
140
141 if (const auto *CE = dyn_cast<CallExpr>(Val: E))
142 return CE->getCalleeAllocSizeAttr() ? CE : nullptr;
143 return nullptr;
144 }
145
146 /// Determines whether or not the given Base contains a call to a function
147 /// with the alloc_size attribute.
148 static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) {
149 const auto *E = Base.dyn_cast<const Expr *>();
150 return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E);
151 }
152
153 /// Determines whether the given kind of constant expression is only ever
154 /// used for name mangling. If so, it's permitted to reference things that we
155 /// can't generate code for (in particular, dllimported functions).
156 static bool isForManglingOnly(ConstantExprKind Kind) {
157 switch (Kind) {
158 case ConstantExprKind::Normal:
159 case ConstantExprKind::ClassTemplateArgument:
160 case ConstantExprKind::ImmediateInvocation:
161 // Note that non-type template arguments of class type are emitted as
162 // template parameter objects.
163 return false;
164
165 case ConstantExprKind::NonClassTemplateArgument:
166 return true;
167 }
168 llvm_unreachable("unknown ConstantExprKind");
169 }
170
171 static bool isTemplateArgument(ConstantExprKind Kind) {
172 switch (Kind) {
173 case ConstantExprKind::Normal:
174 case ConstantExprKind::ImmediateInvocation:
175 return false;
176
177 case ConstantExprKind::ClassTemplateArgument:
178 case ConstantExprKind::NonClassTemplateArgument:
179 return true;
180 }
181 llvm_unreachable("unknown ConstantExprKind");
182 }
183
184 /// The bound to claim that an array of unknown bound has.
185 /// The value in MostDerivedArraySize is undefined in this case. So, set it
186 /// to an arbitrary value that's likely to loudly break things if it's used.
187 static const uint64_t AssumedSizeForUnsizedArray =
188 std::numeric_limits<uint64_t>::max() / 2;
189
190 /// Determines if an LValue with the given LValueBase will have an unsized
191 /// array in its designator.
192 /// Find the path length and type of the most-derived subobject in the given
193 /// path, and find the size of the containing array, if any.
194 static unsigned
195 findMostDerivedSubobject(const ASTContext &Ctx, APValue::LValueBase Base,
196 ArrayRef<APValue::LValuePathEntry> Path,
197 uint64_t &ArraySize, QualType &Type, bool &IsArray,
198 bool &FirstEntryIsUnsizedArray) {
199 // This only accepts LValueBases from APValues, and APValues don't support
200 // arrays that lack size info.
201 assert(!isBaseAnAllocSizeCall(Base) &&
202 "Unsized arrays shouldn't appear here");
203 unsigned MostDerivedLength = 0;
204 // The type of Base is a reference type if the base is a constexpr-unknown
205 // variable. In that case, look through the reference type.
206 Type = getType(B: Base).getNonReferenceType();
207
208 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
209 if (Type->isArrayType()) {
210 const ArrayType *AT = Ctx.getAsArrayType(T: Type);
211 Type = AT->getElementType();
212 MostDerivedLength = I + 1;
213 IsArray = true;
214
215 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) {
216 ArraySize = CAT->getZExtSize();
217 } else {
218 assert(I == 0 && "unexpected unsized array designator");
219 FirstEntryIsUnsizedArray = true;
220 ArraySize = AssumedSizeForUnsizedArray;
221 }
222 } else if (Type->isAnyComplexType()) {
223 const ComplexType *CT = Type->castAs<ComplexType>();
224 Type = CT->getElementType();
225 ArraySize = 2;
226 MostDerivedLength = I + 1;
227 IsArray = true;
228 } else if (const auto *VT = Type->getAs<VectorType>()) {
229 Type = VT->getElementType();
230 ArraySize = VT->getNumElements();
231 MostDerivedLength = I + 1;
232 IsArray = true;
233 } else if (const FieldDecl *FD = getAsField(E: Path[I])) {
234 Type = FD->getType();
235 ArraySize = 0;
236 MostDerivedLength = I + 1;
237 IsArray = false;
238 } else {
239 // Path[I] describes a base class.
240 ArraySize = 0;
241 IsArray = false;
242 }
243 }
244 return MostDerivedLength;
245 }
246
247 /// A path from a glvalue to a subobject of that glvalue.
248 struct SubobjectDesignator {
249 /// True if the subobject was named in a manner not supported by C++11. Such
250 /// lvalues can still be folded, but they are not core constant expressions
251 /// and we cannot perform lvalue-to-rvalue conversions on them.
252 LLVM_PREFERRED_TYPE(bool)
253 unsigned Invalid : 1;
254
255 /// Is this a pointer one past the end of an object?
256 LLVM_PREFERRED_TYPE(bool)
257 unsigned IsOnePastTheEnd : 1;
258
259 /// Indicator of whether the first entry is an unsized array.
260 LLVM_PREFERRED_TYPE(bool)
261 unsigned FirstEntryIsAnUnsizedArray : 1;
262
263 /// Indicator of whether the most-derived object is an array element.
264 LLVM_PREFERRED_TYPE(bool)
265 unsigned MostDerivedIsArrayElement : 1;
266
267 /// The length of the path to the most-derived object of which this is a
268 /// subobject.
269 unsigned MostDerivedPathLength : 28;
270
271 /// The size of the array of which the most-derived object is an element.
272 /// This will always be 0 if the most-derived object is not an array
273 /// element. 0 is not an indicator of whether or not the most-derived object
274 /// is an array, however, because 0-length arrays are allowed.
275 ///
276 /// If the current array is an unsized array, the value of this is
277 /// undefined.
278 uint64_t MostDerivedArraySize;
279 /// The type of the most derived object referred to by this address.
280 QualType MostDerivedType;
281
282 typedef APValue::LValuePathEntry PathEntry;
283
284 /// The entries on the path from the glvalue to the designated subobject.
285 SmallVector<PathEntry, 8> Entries;
286
287 SubobjectDesignator() : Invalid(true) {}
288
289 explicit SubobjectDesignator(QualType T)
290 : Invalid(false), IsOnePastTheEnd(false),
291 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
292 MostDerivedPathLength(0), MostDerivedArraySize(0),
293 MostDerivedType(T.isNull() ? QualType() : T.getNonReferenceType()) {}
294
295 SubobjectDesignator(const ASTContext &Ctx, const APValue &V)
296 : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
297 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
298 MostDerivedPathLength(0), MostDerivedArraySize(0) {
299 assert(V.isLValue() && "Non-LValue used to make an LValue designator?");
300 if (!Invalid) {
301 IsOnePastTheEnd = V.isLValueOnePastTheEnd();
302 llvm::append_range(C&: Entries, R: V.getLValuePath());
303 if (V.getLValueBase()) {
304 bool IsArray = false;
305 bool FirstIsUnsizedArray = false;
306 MostDerivedPathLength = findMostDerivedSubobject(
307 Ctx, Base: V.getLValueBase(), Path: V.getLValuePath(), ArraySize&: MostDerivedArraySize,
308 Type&: MostDerivedType, IsArray, FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
309 MostDerivedIsArrayElement = IsArray;
310 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
311 }
312 }
313 }
314
315 void truncate(ASTContext &Ctx, APValue::LValueBase Base,
316 unsigned NewLength) {
317 if (Invalid)
318 return;
319
320 assert(Base && "cannot truncate path for null pointer");
321 assert(NewLength <= Entries.size() && "not a truncation");
322
323 if (NewLength == Entries.size())
324 return;
325 Entries.resize(N: NewLength);
326
327 bool IsArray = false;
328 bool FirstIsUnsizedArray = false;
329 MostDerivedPathLength = findMostDerivedSubobject(
330 Ctx, Base, Path: Entries, ArraySize&: MostDerivedArraySize, Type&: MostDerivedType, IsArray,
331 FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
332 MostDerivedIsArrayElement = IsArray;
333 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
334 }
335
336 void setInvalid() {
337 Invalid = true;
338 Entries.clear();
339 }
340
341 /// Determine whether the most derived subobject is an array without a
342 /// known bound.
343 bool isMostDerivedAnUnsizedArray() const {
344 assert(!Invalid && "Calling this makes no sense on invalid designators");
345 return Entries.size() == 1 && FirstEntryIsAnUnsizedArray;
346 }
347
348 /// Determine what the most derived array's size is. Results in an assertion
349 /// failure if the most derived array lacks a size.
350 uint64_t getMostDerivedArraySize() const {
351 assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size");
352 return MostDerivedArraySize;
353 }
354
355 /// Determine whether this is a one-past-the-end pointer.
356 bool isOnePastTheEnd() const {
357 assert(!Invalid);
358 if (IsOnePastTheEnd)
359 return true;
360 if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement &&
361 Entries[MostDerivedPathLength - 1].getAsArrayIndex() ==
362 MostDerivedArraySize)
363 return true;
364 return false;
365 }
366
367 /// Get the range of valid index adjustments in the form
368 /// {maximum value that can be subtracted from this pointer,
369 /// maximum value that can be added to this pointer}
370 std::pair<uint64_t, uint64_t> validIndexAdjustments() {
371 if (Invalid || isMostDerivedAnUnsizedArray())
372 return {0, 0};
373
374 // [expr.add]p4: For the purposes of these operators, a pointer to a
375 // nonarray object behaves the same as a pointer to the first element of
376 // an array of length one with the type of the object as its element type.
377 bool IsArray = MostDerivedPathLength == Entries.size() &&
378 MostDerivedIsArrayElement;
379 uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex()
380 : (uint64_t)IsOnePastTheEnd;
381 uint64_t ArraySize =
382 IsArray ? getMostDerivedArraySize() : (uint64_t)1;
383 return {ArrayIndex, ArraySize - ArrayIndex};
384 }
385
386 /// Check that this refers to a valid subobject.
387 bool isValidSubobject() const {
388 if (Invalid)
389 return false;
390 return !isOnePastTheEnd();
391 }
392 /// Check that this refers to a valid subobject, and if not, produce a
393 /// relevant diagnostic and set the designator as invalid.
394 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
395
396 /// Get the type of the designated object.
397 QualType getType(ASTContext &Ctx) const {
398 assert(!Invalid && "invalid designator has no subobject type");
399 return MostDerivedPathLength == Entries.size()
400 ? MostDerivedType
401 : Ctx.getCanonicalTagType(TD: getAsBaseClass(E: Entries.back()));
402 }
403
404 /// Update this designator to refer to the first element within this array.
405 void addArrayUnchecked(const ConstantArrayType *CAT) {
406 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
407
408 // This is a most-derived object.
409 MostDerivedType = CAT->getElementType();
410 MostDerivedIsArrayElement = true;
411 MostDerivedArraySize = CAT->getZExtSize();
412 MostDerivedPathLength = Entries.size();
413 }
414 /// Update this designator to refer to the first element within the array of
415 /// elements of type T. This is an array of unknown size.
416 void addUnsizedArrayUnchecked(QualType ElemTy) {
417 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
418
419 MostDerivedType = ElemTy;
420 MostDerivedIsArrayElement = true;
421 // The value in MostDerivedArraySize is undefined in this case. So, set it
422 // to an arbitrary value that's likely to loudly break things if it's
423 // used.
424 MostDerivedArraySize = AssumedSizeForUnsizedArray;
425 MostDerivedPathLength = Entries.size();
426 }
427 /// Update this designator to refer to the given base or member of this
428 /// object.
429 void addDeclUnchecked(const Decl *D, bool Virtual = false) {
430 Entries.push_back(Elt: APValue::BaseOrMemberType(D, Virtual));
431
432 // If this isn't a base class, it's a new most-derived object.
433 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) {
434 MostDerivedType = FD->getType();
435 MostDerivedIsArrayElement = false;
436 MostDerivedArraySize = 0;
437 MostDerivedPathLength = Entries.size();
438 }
439 }
440 /// Update this designator to refer to the given complex component.
441 void addComplexUnchecked(QualType EltTy, bool Imag) {
442 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Imag));
443
444 // This is technically a most-derived object, though in practice this
445 // is unlikely to matter.
446 MostDerivedType = EltTy;
447 MostDerivedIsArrayElement = true;
448 MostDerivedArraySize = 2;
449 MostDerivedPathLength = Entries.size();
450 }
451
452 void addVectorElementUnchecked(QualType EltTy, uint64_t Size,
453 uint64_t Idx) {
454 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Idx));
455 MostDerivedType = EltTy;
456 MostDerivedPathLength = Entries.size();
457 MostDerivedArraySize = 0;
458 MostDerivedIsArrayElement = false;
459 }
460
461 void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E);
462 void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E,
463 const APSInt &N);
464 /// Add N to the address of this subobject.
465 void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, const LValue &LV);
466 };
467
468 /// A scope at the end of which an object can need to be destroyed.
469 enum class ScopeKind {
470 Block,
471 FullExpression,
472 Call
473 };
474
475 /// A reference to a particular call and its arguments.
476 struct CallRef {
477 CallRef() : OrigCallee(), CallIndex(0), Version() {}
478 CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version)
479 : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {}
480
481 explicit operator bool() const { return OrigCallee; }
482
483 /// Get the parameter that the caller initialized, corresponding to the
484 /// given parameter in the callee.
485 const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const {
486 return OrigCallee ? OrigCallee->getParamDecl(i: PVD->getFunctionScopeIndex())
487 : PVD;
488 }
489
490 /// The callee at the point where the arguments were evaluated. This might
491 /// be different from the actual callee (a different redeclaration, or a
492 /// virtual override), but this function's parameters are the ones that
493 /// appear in the parameter map.
494 const FunctionDecl *OrigCallee;
495 /// The call index of the frame that holds the argument values.
496 unsigned CallIndex;
497 /// The version of the parameters corresponding to this call.
498 unsigned Version;
499 };
500
501 /// A stack frame in the constexpr call stack.
502 class CallStackFrame : public interp::Frame {
503 public:
504 EvalInfo &Info;
505
506 /// Parent - The caller of this stack frame.
507 CallStackFrame *Caller;
508
509 /// Callee - The function which was called.
510 const FunctionDecl *Callee;
511
512 /// This - The binding for the this pointer in this call, if any.
513 const LValue *This;
514
515 /// CallExpr - The syntactical structure of member function calls
516 const Expr *CallExpr;
517
518 /// Information on how to find the arguments to this call. Our arguments
519 /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a
520 /// key and this value as the version.
521 CallRef Arguments;
522
523 /// Source location information about the default argument or default
524 /// initializer expression we're evaluating, if any.
525 CurrentSourceLocExprScope CurSourceLocExprScope;
526
527 // Note that we intentionally use std::map here so that references to
528 // values are stable.
529 typedef std::pair<const void *, unsigned> MapKeyTy;
530 typedef std::map<MapKeyTy, APValue> MapTy;
531 /// Temporaries - Temporary lvalues materialized within this stack frame.
532 MapTy Temporaries;
533
534 /// CallRange - The source range of the call expression for this call.
535 SourceRange CallRange;
536
537 /// Index - The call index of this call.
538 unsigned Index;
539
540 /// The stack of integers for tracking version numbers for temporaries.
541 SmallVector<unsigned, 2> TempVersionStack = {1};
542 unsigned CurTempVersion = TempVersionStack.back();
543
544 unsigned getTempVersion() const { return TempVersionStack.back(); }
545
546 void pushTempVersion() {
547 TempVersionStack.push_back(Elt: ++CurTempVersion);
548 }
549
550 void popTempVersion() {
551 TempVersionStack.pop_back();
552 }
553
554 CallRef createCall(const FunctionDecl *Callee) {
555 return {Callee, Index, ++CurTempVersion};
556 }
557
558 // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact
559 // on the overall stack usage of deeply-recursing constexpr evaluations.
560 // (We should cache this map rather than recomputing it repeatedly.)
561 // But let's try this and see how it goes; we can look into caching the map
562 // as a later change.
563
564 /// LambdaCaptureFields - Mapping from captured variables/this to
565 /// corresponding data members in the closure class.
566 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
567 FieldDecl *LambdaThisCaptureField = nullptr;
568
569 CallStackFrame(EvalInfo &Info, SourceRange CallRange,
570 const FunctionDecl *Callee, const LValue *This,
571 const Expr *CallExpr, CallRef Arguments);
572 ~CallStackFrame();
573
574 // Return the temporary for Key whose version number is Version.
575 APValue *getTemporary(const void *Key, unsigned Version) {
576 MapKeyTy KV(Key, Version);
577 auto LB = Temporaries.lower_bound(x: KV);
578 if (LB != Temporaries.end() && LB->first == KV)
579 return &LB->second;
580 return nullptr;
581 }
582
583 // Return the current temporary for Key in the map.
584 APValue *getCurrentTemporary(const void *Key) {
585 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
586 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
587 return &std::prev(x: UB)->second;
588 return nullptr;
589 }
590
591 // Return the version number of the current temporary for Key.
592 unsigned getCurrentTemporaryVersion(const void *Key) const {
593 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
594 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
595 return std::prev(x: UB)->first.second;
596 return 0;
597 }
598
599 /// Allocate storage for an object of type T in this stack frame.
600 /// Populates LV with a handle to the created object. Key identifies
601 /// the temporary within the stack frame, and must not be reused without
602 /// bumping the temporary version number.
603 template<typename KeyT>
604 APValue &createTemporary(const KeyT *Key, QualType T,
605 ScopeKind Scope, LValue &LV);
606
607 /// Allocate storage for a parameter of a function call made in this frame.
608 APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV);
609
610 void describe(llvm::raw_ostream &OS) const override;
611
612 Frame *getCaller() const override { return Caller; }
613 SourceRange getCallRange() const override { return CallRange; }
614 const FunctionDecl *getCallee() const override { return Callee; }
615
616 bool isStdFunction() const {
617 for (const DeclContext *DC = Callee; DC; DC = DC->getParent())
618 if (DC->isStdNamespace())
619 return true;
620 return false;
621 }
622
623 /// Whether we're in a context where [[msvc::constexpr]] evaluation is
624 /// permitted. See MSConstexprDocs for description of permitted contexts.
625 bool CanEvalMSConstexpr = false;
626
627 private:
628 APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T,
629 ScopeKind Scope);
630 };
631
632 /// Temporarily override 'this'.
633 class ThisOverrideRAII {
634 public:
635 ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable)
636 : Frame(Frame), OldThis(Frame.This) {
637 if (Enable)
638 Frame.This = NewThis;
639 }
640 ~ThisOverrideRAII() {
641 Frame.This = OldThis;
642 }
643 private:
644 CallStackFrame &Frame;
645 const LValue *OldThis;
646 };
647
648 // A shorthand time trace scope struct, prints source range, for example
649 // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}}
650 class ExprTimeTraceScope {
651 public:
652 ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name)
653 : TimeScope(Name, [E, &Ctx] {
654 return E->getSourceRange().printToString(SM: Ctx.getSourceManager());
655 }) {}
656
657 private:
658 llvm::TimeTraceScope TimeScope;
659 };
660
661 /// RAII object used to change the current ability of
662 /// [[msvc::constexpr]] evaulation.
663 struct MSConstexprContextRAII {
664 CallStackFrame &Frame;
665 bool OldValue;
666 explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value)
667 : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) {
668 Frame.CanEvalMSConstexpr = Value;
669 }
670
671 ~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; }
672 };
673}
674
675static bool HandleDestruction(EvalInfo &Info, const Expr *E,
676 const LValue &This, QualType ThisType);
677static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
678 APValue::LValueBase LVBase, APValue &Value,
679 QualType T);
680
681namespace {
682 /// A cleanup, and a flag indicating whether it is lifetime-extended.
683 class Cleanup {
684 llvm::PointerIntPair<APValue*, 2, ScopeKind> Value;
685 APValue::LValueBase Base;
686 QualType T;
687
688 public:
689 Cleanup(APValue *Val, APValue::LValueBase Base, QualType T,
690 ScopeKind Scope)
691 : Value(Val, Scope), Base(Base), T(T) {}
692
693 /// Determine whether this cleanup should be performed at the end of the
694 /// given kind of scope.
695 bool isDestroyedAtEndOf(ScopeKind K) const {
696 return (int)Value.getInt() >= (int)K;
697 }
698 bool endLifetime(EvalInfo &Info, bool RunDestructors) {
699 if (RunDestructors) {
700 SourceLocation Loc;
701 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
702 Loc = VD->getLocation();
703 else if (const Expr *E = Base.dyn_cast<const Expr*>())
704 Loc = E->getExprLoc();
705 return HandleDestruction(Info, Loc, LVBase: Base, Value&: *Value.getPointer(), T);
706 }
707 *Value.getPointer() = APValue();
708 return true;
709 }
710
711 bool hasSideEffect() {
712 return T.isDestructedType();
713 }
714 };
715
716 /// A reference to an object whose construction we are currently evaluating.
717 struct ObjectUnderConstruction {
718 APValue::LValueBase Base;
719 ArrayRef<APValue::LValuePathEntry> Path;
720 friend bool operator==(const ObjectUnderConstruction &LHS,
721 const ObjectUnderConstruction &RHS) {
722 return LHS.Base == RHS.Base && LHS.Path == RHS.Path;
723 }
724 friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) {
725 return llvm::hash_combine(args: Obj.Base, args: Obj.Path);
726 }
727 };
728 enum class ConstructionPhase {
729 None,
730 Bases,
731 AfterBases,
732 AfterFields,
733 Destroying,
734 DestroyingBases
735 };
736}
737
738namespace llvm {
739template<> struct DenseMapInfo<ObjectUnderConstruction> {
740 using Base = DenseMapInfo<APValue::LValueBase>;
741 static unsigned getHashValue(const ObjectUnderConstruction &Object) {
742 return hash_value(Obj: Object);
743 }
744 static bool isEqual(const ObjectUnderConstruction &LHS,
745 const ObjectUnderConstruction &RHS) {
746 return LHS == RHS;
747 }
748};
749}
750
751namespace {
752 /// A dynamically-allocated heap object.
753 struct DynAlloc {
754 /// The value of this heap-allocated object.
755 APValue Value;
756 /// The allocating expression; used for diagnostics. Either a CXXNewExpr
757 /// or a CallExpr (the latter is for direct calls to operator new inside
758 /// std::allocator<T>::allocate).
759 const Expr *AllocExpr = nullptr;
760
761 enum Kind {
762 New,
763 ArrayNew,
764 StdAllocator
765 };
766
767 /// Get the kind of the allocation. This must match between allocation
768 /// and deallocation.
769 Kind getKind() const {
770 if (auto *NE = dyn_cast<CXXNewExpr>(Val: AllocExpr))
771 return NE->isArray() ? ArrayNew : New;
772 assert(isa<CallExpr>(AllocExpr));
773 return StdAllocator;
774 }
775 };
776
777 struct DynAllocOrder {
778 bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const {
779 return L.getIndex() < R.getIndex();
780 }
781 };
782
783 /// EvalInfo - This is a private struct used by the evaluator to capture
784 /// information about a subexpression as it is folded. It retains information
785 /// about the AST context, but also maintains information about the folded
786 /// expression.
787 ///
788 /// If an expression could be evaluated, it is still possible it is not a C
789 /// "integer constant expression" or constant expression. If not, this struct
790 /// captures information about how and why not.
791 ///
792 /// One bit of information passed *into* the request for constant folding
793 /// indicates whether the subexpression is "evaluated" or not according to C
794 /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
795 /// evaluate the expression regardless of what the RHS is, but C only allows
796 /// certain things in certain situations.
797 class EvalInfo final : public interp::State {
798 public:
799 /// CurrentCall - The top of the constexpr call stack.
800 CallStackFrame *CurrentCall;
801
802 /// CallStackDepth - The number of calls in the call stack right now.
803 unsigned CallStackDepth;
804
805 /// NextCallIndex - The next call index to assign.
806 unsigned NextCallIndex;
807
808 /// StepsLeft - The remaining number of evaluation steps we're permitted
809 /// to perform. This is essentially a limit for the number of statements
810 /// we will evaluate.
811 unsigned StepsLeft;
812
813 /// Enable the experimental new constant interpreter. If an expression is
814 /// not supported by the interpreter, an error is triggered.
815 bool EnableNewConstInterp;
816
817 /// BottomFrame - The frame in which evaluation started. This must be
818 /// initialized after CurrentCall and CallStackDepth.
819 CallStackFrame BottomFrame;
820
821 /// A stack of values whose lifetimes end at the end of some surrounding
822 /// evaluation frame.
823 llvm::SmallVector<Cleanup, 16> CleanupStack;
824
825 /// EvaluatingDecl - This is the declaration whose initializer is being
826 /// evaluated, if any.
827 APValue::LValueBase EvaluatingDecl;
828
829 enum class EvaluatingDeclKind {
830 None,
831 /// We're evaluating the construction of EvaluatingDecl.
832 Ctor,
833 /// We're evaluating the destruction of EvaluatingDecl.
834 Dtor,
835 };
836 EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None;
837
838 /// EvaluatingDeclValue - This is the value being constructed for the
839 /// declaration whose initializer is being evaluated, if any.
840 APValue *EvaluatingDeclValue;
841
842 /// Stack of loops and 'switch' statements which we're currently
843 /// breaking/continuing; null entries are used to mark unlabeled
844 /// break/continue.
845 SmallVector<const Stmt *> BreakContinueStack;
846
847 /// Set of objects that are currently being constructed.
848 llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase>
849 ObjectsUnderConstruction;
850
851 /// Current heap allocations, along with the location where each was
852 /// allocated. We use std::map here because we need stable addresses
853 /// for the stored APValues.
854 std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs;
855
856 /// The number of heap allocations performed so far in this evaluation.
857 unsigned NumHeapAllocs = 0;
858
859 struct EvaluatingConstructorRAII {
860 EvalInfo &EI;
861 ObjectUnderConstruction Object;
862 bool DidInsert;
863 EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object,
864 bool HasBases)
865 : EI(EI), Object(Object) {
866 DidInsert =
867 EI.ObjectsUnderConstruction
868 .insert(KV: {Object, HasBases ? ConstructionPhase::Bases
869 : ConstructionPhase::AfterBases})
870 .second;
871 }
872 void finishedConstructingBases() {
873 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases;
874 }
875 void finishedConstructingFields() {
876 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields;
877 }
878 ~EvaluatingConstructorRAII() {
879 if (DidInsert) EI.ObjectsUnderConstruction.erase(Val: Object);
880 }
881 };
882
883 struct EvaluatingDestructorRAII {
884 EvalInfo &EI;
885 ObjectUnderConstruction Object;
886 bool DidInsert;
887 EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object)
888 : EI(EI), Object(Object) {
889 DidInsert = EI.ObjectsUnderConstruction
890 .insert(KV: {Object, ConstructionPhase::Destroying})
891 .second;
892 }
893 void startedDestroyingBases() {
894 EI.ObjectsUnderConstruction[Object] =
895 ConstructionPhase::DestroyingBases;
896 }
897 ~EvaluatingDestructorRAII() {
898 if (DidInsert)
899 EI.ObjectsUnderConstruction.erase(Val: Object);
900 }
901 };
902
903 ConstructionPhase
904 isEvaluatingCtorDtor(APValue::LValueBase Base,
905 ArrayRef<APValue::LValuePathEntry> Path) {
906 return ObjectsUnderConstruction.lookup(Val: {.Base: Base, .Path: Path});
907 }
908
909 /// If we're currently speculatively evaluating, the outermost call stack
910 /// depth at which we can mutate state, otherwise 0.
911 unsigned SpeculativeEvaluationDepth = 0;
912
913 /// The current array initialization index, if we're performing array
914 /// initialization.
915 uint64_t ArrayInitIndex = -1;
916
917 EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode)
918 : State(const_cast<ASTContext &>(C), S), CurrentCall(nullptr),
919 CallStackDepth(0), NextCallIndex(1),
920 StepsLeft(C.getLangOpts().ConstexprStepLimit),
921 EnableNewConstInterp(C.getLangOpts().EnableNewConstInterp),
922 BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr,
923 /*This=*/nullptr,
924 /*CallExpr=*/nullptr, CallRef()),
925 EvaluatingDecl((const ValueDecl *)nullptr),
926 EvaluatingDeclValue(nullptr) {
927 EvalMode = Mode;
928 }
929
930 ~EvalInfo() {
931 discardCleanups();
932 }
933
934 void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value,
935 EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) {
936 EvaluatingDecl = Base;
937 IsEvaluatingDecl = EDK;
938 EvaluatingDeclValue = &Value;
939 }
940
941 bool CheckCallLimit(SourceLocation Loc) {
942 // Don't perform any constexpr calls (other than the call we're checking)
943 // when checking a potential constant expression.
944 if (checkingPotentialConstantExpression() && CallStackDepth > 1)
945 return false;
946 if (NextCallIndex == 0) {
947 // NextCallIndex has wrapped around.
948 FFDiag(Loc, DiagId: diag::note_constexpr_call_limit_exceeded);
949 return false;
950 }
951 if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
952 return true;
953 FFDiag(Loc, DiagId: diag::note_constexpr_depth_limit_exceeded)
954 << getLangOpts().ConstexprCallDepth;
955 return false;
956 }
957
958 bool CheckArraySize(SourceLocation Loc, unsigned BitWidth,
959 uint64_t ElemCount, bool Diag) {
960 // FIXME: GH63562
961 // APValue stores array extents as unsigned,
962 // so anything that is greater that unsigned would overflow when
963 // constructing the array, we catch this here.
964 if (BitWidth > ConstantArrayType::getMaxSizeBits(Context: Ctx) ||
965 ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) {
966 if (Diag)
967 FFDiag(Loc, DiagId: diag::note_constexpr_new_too_large) << ElemCount;
968 return false;
969 }
970
971 // FIXME: GH63562
972 // Arrays allocate an APValue per element.
973 // We use the number of constexpr steps as a proxy for the maximum size
974 // of arrays to avoid exhausting the system resources, as initialization
975 // of each element is likely to take some number of steps anyway.
976 uint64_t Limit = getLangOpts().ConstexprStepLimit;
977 if (Limit != 0 && ElemCount > Limit) {
978 if (Diag) {
979 FFDiag(Loc, DiagId: diag::note_constexpr_new_exceeds_limits, ExtraNotes: 1)
980 << ElemCount << Limit;
981 Note(Loc, DiagId: diag::note_constexpr_steps);
982 }
983 return false;
984 }
985 return true;
986 }
987
988 std::pair<CallStackFrame *, unsigned>
989 getCallFrameAndDepth(unsigned CallIndex) {
990 assert(CallIndex && "no call index in getCallFrameAndDepth");
991 // We will eventually hit BottomFrame, which has Index 1, so Frame can't
992 // be null in this loop.
993 unsigned Depth = CallStackDepth;
994 CallStackFrame *Frame = CurrentCall;
995 while (Frame->Index > CallIndex) {
996 Frame = Frame->Caller;
997 --Depth;
998 }
999 if (Frame->Index == CallIndex)
1000 return {Frame, Depth};
1001 return {nullptr, 0};
1002 }
1003
1004 bool nextStep(const Stmt *S) {
1005 if (getLangOpts().ConstexprStepLimit == 0)
1006 return true;
1007
1008 if (!StepsLeft) {
1009 FFDiag(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_step_limit_exceeded, ExtraNotes: 1)
1010 << getLangOpts().ConstexprStepLimit;
1011 Note(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_steps);
1012 return false;
1013 }
1014 --StepsLeft;
1015 return true;
1016 }
1017
1018 APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV);
1019
1020 std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) {
1021 std::optional<DynAlloc *> Result;
1022 auto It = HeapAllocs.find(x: DA);
1023 if (It != HeapAllocs.end())
1024 Result = &It->second;
1025 return Result;
1026 }
1027
1028 /// Get the allocated storage for the given parameter of the given call.
1029 APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) {
1030 CallStackFrame *Frame = getCallFrameAndDepth(CallIndex: Call.CallIndex).first;
1031 return Frame ? Frame->getTemporary(Key: Call.getOrigParam(PVD), Version: Call.Version)
1032 : nullptr;
1033 }
1034
1035 /// Information about a stack frame for std::allocator<T>::[de]allocate.
1036 struct StdAllocatorCaller {
1037 unsigned FrameIndex;
1038 QualType ElemType;
1039 const Expr *Call;
1040 explicit operator bool() const { return FrameIndex != 0; };
1041 };
1042
1043 StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const {
1044 for (const CallStackFrame *Call = CurrentCall; Call->Caller != nullptr;
1045 Call = Call->Caller) {
1046 const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: Call->Callee);
1047 if (!MD)
1048 continue;
1049 const IdentifierInfo *FnII = MD->getIdentifier();
1050 if (!FnII || !FnII->isStr(Str: FnName))
1051 continue;
1052
1053 const auto *CTSD =
1054 dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent());
1055 if (!CTSD)
1056 continue;
1057
1058 const IdentifierInfo *ClassII = CTSD->getIdentifier();
1059 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
1060 if (CTSD->isInStdNamespace() && ClassII &&
1061 ClassII->isStr(Str: "allocator") && TAL.size() >= 1 &&
1062 TAL[0].getKind() == TemplateArgument::Type)
1063 return {.FrameIndex: Call->Index, .ElemType: TAL[0].getAsType(), .Call: Call->CallExpr};
1064 }
1065
1066 return {};
1067 }
1068
1069 void performLifetimeExtension() {
1070 // Disable the cleanups for lifetime-extended temporaries.
1071 llvm::erase_if(C&: CleanupStack, P: [](Cleanup &C) {
1072 return !C.isDestroyedAtEndOf(K: ScopeKind::FullExpression);
1073 });
1074 }
1075
1076 /// Throw away any remaining cleanups at the end of evaluation. If any
1077 /// cleanups would have had a side-effect, note that as an unmodeled
1078 /// side-effect and return false. Otherwise, return true.
1079 bool discardCleanups() {
1080 for (Cleanup &C : CleanupStack) {
1081 if (C.hasSideEffect() && !noteSideEffect()) {
1082 CleanupStack.clear();
1083 return false;
1084 }
1085 }
1086 CleanupStack.clear();
1087 return true;
1088 }
1089
1090 private:
1091 const interp::Frame *getCurrentFrame() override { return CurrentCall; }
1092
1093 unsigned getCallStackDepth() override { return CallStackDepth; }
1094 bool stepsLeft() const override { return StepsLeft > 0; }
1095
1096 public:
1097 /// Notes that we failed to evaluate an expression that other expressions
1098 /// directly depend on, and determine if we should keep evaluating. This
1099 /// should only be called if we actually intend to keep evaluating.
1100 ///
1101 /// Call noteSideEffect() instead if we may be able to ignore the value that
1102 /// we failed to evaluate, e.g. if we failed to evaluate Foo() in:
1103 ///
1104 /// (Foo(), 1) // use noteSideEffect
1105 /// (Foo() || true) // use noteSideEffect
1106 /// Foo() + 1 // use noteFailure
1107 [[nodiscard]] bool noteFailure() {
1108 // Failure when evaluating some expression often means there is some
1109 // subexpression whose evaluation was skipped. Therefore, (because we
1110 // don't track whether we skipped an expression when unwinding after an
1111 // evaluation failure) every evaluation failure that bubbles up from a
1112 // subexpression implies that a side-effect has potentially happened. We
1113 // skip setting the HasSideEffects flag to true until we decide to
1114 // continue evaluating after that point, which happens here.
1115 bool KeepGoing = keepEvaluatingAfterFailure();
1116 EvalStatus.HasSideEffects |= KeepGoing;
1117 return KeepGoing;
1118 }
1119
1120 class ArrayInitLoopIndex {
1121 EvalInfo &Info;
1122 uint64_t OuterIndex;
1123
1124 public:
1125 ArrayInitLoopIndex(EvalInfo &Info)
1126 : Info(Info), OuterIndex(Info.ArrayInitIndex) {
1127 Info.ArrayInitIndex = 0;
1128 }
1129 ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; }
1130
1131 operator uint64_t&() { return Info.ArrayInitIndex; }
1132 };
1133 };
1134
1135 /// Object used to treat all foldable expressions as constant expressions.
1136 struct FoldConstant {
1137 EvalInfo &Info;
1138 bool Enabled;
1139 bool HadNoPriorDiags;
1140 EvaluationMode OldMode;
1141
1142 explicit FoldConstant(EvalInfo &Info, bool Enabled)
1143 : Info(Info),
1144 Enabled(Enabled),
1145 HadNoPriorDiags(Info.EvalStatus.Diag &&
1146 Info.EvalStatus.Diag->empty() &&
1147 !Info.EvalStatus.HasSideEffects),
1148 OldMode(Info.EvalMode) {
1149 if (Enabled)
1150 Info.EvalMode = EvaluationMode::ConstantFold;
1151 }
1152 void keepDiagnostics() { Enabled = false; }
1153 ~FoldConstant() {
1154 if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() &&
1155 !Info.EvalStatus.HasSideEffects) {
1156 Info.EvalStatus.Diag->clear();
1157 Info.EvalStatus.DiagEmitted = false;
1158 }
1159 Info.EvalMode = OldMode;
1160 }
1161 };
1162
1163 /// RAII object used to set the current evaluation mode to ignore
1164 /// side-effects.
1165 struct IgnoreSideEffectsRAII {
1166 EvalInfo &Info;
1167 EvaluationMode OldMode;
1168 explicit IgnoreSideEffectsRAII(EvalInfo &Info)
1169 : Info(Info), OldMode(Info.EvalMode) {
1170 Info.EvalMode = EvaluationMode::IgnoreSideEffects;
1171 }
1172
1173 ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; }
1174 };
1175
1176 /// RAII object used to optionally suppress diagnostics and side-effects from
1177 /// a speculative evaluation.
1178 class SpeculativeEvaluationRAII {
1179 EvalInfo *Info = nullptr;
1180 Expr::EvalStatus OldStatus;
1181 unsigned OldSpeculativeEvaluationDepth = 0;
1182
1183 void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) {
1184 Info = Other.Info;
1185 OldStatus = Other.OldStatus;
1186 OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth;
1187 Other.Info = nullptr;
1188 }
1189
1190 void maybeRestoreState() {
1191 if (!Info)
1192 return;
1193
1194 Info->EvalStatus = OldStatus;
1195 Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth;
1196 }
1197
1198 public:
1199 SpeculativeEvaluationRAII() = default;
1200
1201 SpeculativeEvaluationRAII(
1202 EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr)
1203 : Info(&Info), OldStatus(Info.EvalStatus),
1204 OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) {
1205 Info.EvalStatus.Diag = NewDiag;
1206 Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1;
1207 }
1208
1209 SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete;
1210 SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) {
1211 moveFromAndCancel(Other: std::move(Other));
1212 }
1213
1214 SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) {
1215 maybeRestoreState();
1216 moveFromAndCancel(Other: std::move(Other));
1217 return *this;
1218 }
1219
1220 ~SpeculativeEvaluationRAII() { maybeRestoreState(); }
1221 };
1222
1223 /// RAII object wrapping a full-expression or block scope, and handling
1224 /// the ending of the lifetime of temporaries created within it.
1225 template<ScopeKind Kind>
1226 class ScopeRAII {
1227 EvalInfo &Info;
1228 unsigned OldStackSize;
1229 public:
1230 ScopeRAII(EvalInfo &Info)
1231 : Info(Info), OldStackSize(Info.CleanupStack.size()) {
1232 // Push a new temporary version. This is needed to distinguish between
1233 // temporaries created in different iterations of a loop.
1234 Info.CurrentCall->pushTempVersion();
1235 }
1236 bool destroy(bool RunDestructors = true) {
1237 bool OK = cleanup(Info, RunDestructors, OldStackSize);
1238 OldStackSize = std::numeric_limits<unsigned>::max();
1239 return OK;
1240 }
1241 ~ScopeRAII() {
1242 if (OldStackSize != std::numeric_limits<unsigned>::max())
1243 destroy(RunDestructors: false);
1244 // Body moved to a static method to encourage the compiler to inline away
1245 // instances of this class.
1246 Info.CurrentCall->popTempVersion();
1247 }
1248 private:
1249 static bool cleanup(EvalInfo &Info, bool RunDestructors,
1250 unsigned OldStackSize) {
1251 assert(OldStackSize <= Info.CleanupStack.size() &&
1252 "running cleanups out of order?");
1253
1254 // Run all cleanups for a block scope, and non-lifetime-extended cleanups
1255 // for a full-expression scope.
1256 bool Success = true;
1257 for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) {
1258 if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(K: Kind)) {
1259 if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) {
1260 Success = false;
1261 break;
1262 }
1263 }
1264 }
1265
1266 // Compact any retained cleanups.
1267 auto NewEnd = Info.CleanupStack.begin() + OldStackSize;
1268 if (Kind != ScopeKind::Block)
1269 NewEnd =
1270 std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) {
1271 return C.isDestroyedAtEndOf(K: Kind);
1272 });
1273 Info.CleanupStack.erase(CS: NewEnd, CE: Info.CleanupStack.end());
1274 return Success;
1275 }
1276 };
1277 typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII;
1278 typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII;
1279 typedef ScopeRAII<ScopeKind::Call> CallScopeRAII;
1280}
1281
1282bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
1283 CheckSubobjectKind CSK) {
1284 if (Invalid)
1285 return false;
1286 if (isOnePastTheEnd()) {
1287 Info.CCEDiag(E, DiagId: diag::note_constexpr_past_end_subobject)
1288 << CSK;
1289 setInvalid();
1290 return false;
1291 }
1292 // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there
1293 // must actually be at least one array element; even a VLA cannot have a
1294 // bound of zero. And if our index is nonzero, we already had a CCEDiag.
1295 return true;
1296}
1297
1298void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info,
1299 const Expr *E) {
1300 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsized_array_indexed);
1301 // Do not set the designator as invalid: we can represent this situation,
1302 // and correct handling of __builtin_object_size requires us to do so.
1303}
1304
1305void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
1306 const Expr *E,
1307 const APSInt &N) {
1308 // If we're complaining, we must be able to statically determine the size of
1309 // the most derived array.
1310 if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement)
1311 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1312 << N << /*array*/ 0
1313 << static_cast<unsigned>(getMostDerivedArraySize());
1314 else
1315 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1316 << N << /*non-array*/ 1;
1317 setInvalid();
1318}
1319
1320CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange,
1321 const FunctionDecl *Callee, const LValue *This,
1322 const Expr *CallExpr, CallRef Call)
1323 : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This),
1324 CallExpr(CallExpr), Arguments(Call), CallRange(CallRange),
1325 Index(Info.NextCallIndex++) {
1326 Info.CurrentCall = this;
1327 ++Info.CallStackDepth;
1328}
1329
1330CallStackFrame::~CallStackFrame() {
1331 assert(Info.CurrentCall == this && "calls retired out of order");
1332 --Info.CallStackDepth;
1333 Info.CurrentCall = Caller;
1334}
1335
1336static bool isRead(AccessKinds AK) {
1337 return AK == AK_Read || AK == AK_ReadObjectRepresentation ||
1338 AK == AK_IsWithinLifetime || AK == AK_Dereference;
1339}
1340
1341static bool isModification(AccessKinds AK) {
1342 switch (AK) {
1343 case AK_Read:
1344 case AK_ReadObjectRepresentation:
1345 case AK_MemberCall:
1346 case AK_DynamicCast:
1347 case AK_TypeId:
1348 case AK_IsWithinLifetime:
1349 case AK_Dereference:
1350 return false;
1351 case AK_Assign:
1352 case AK_Increment:
1353 case AK_Decrement:
1354 case AK_Construct:
1355 case AK_Destroy:
1356 return true;
1357 }
1358 llvm_unreachable("unknown access kind");
1359}
1360
1361static bool isAnyAccess(AccessKinds AK) {
1362 return isRead(AK) || isModification(AK);
1363}
1364
1365/// Is this an access per the C++ definition?
1366static bool isFormalAccess(AccessKinds AK) {
1367 return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy &&
1368 AK != AK_IsWithinLifetime && AK != AK_Dereference;
1369}
1370
1371/// Is this kind of access valid on an indeterminate object value?
1372static bool isValidIndeterminateAccess(AccessKinds AK) {
1373 switch (AK) {
1374 case AK_Read:
1375 case AK_Increment:
1376 case AK_Decrement:
1377 case AK_Dereference:
1378 // These need the object's value.
1379 return false;
1380
1381 case AK_IsWithinLifetime:
1382 case AK_ReadObjectRepresentation:
1383 case AK_Assign:
1384 case AK_Construct:
1385 case AK_Destroy:
1386 // Construction and destruction don't need the value.
1387 return true;
1388
1389 case AK_MemberCall:
1390 case AK_DynamicCast:
1391 case AK_TypeId:
1392 // These aren't really meaningful on scalars.
1393 return true;
1394 }
1395 llvm_unreachable("unknown access kind");
1396}
1397
1398namespace {
1399 struct ComplexValue {
1400 private:
1401 bool IsInt;
1402
1403 public:
1404 APSInt IntReal, IntImag;
1405 APFloat FloatReal, FloatImag;
1406
1407 ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {}
1408
1409 void makeComplexFloat() { IsInt = false; }
1410 bool isComplexFloat() const { return !IsInt; }
1411 APFloat &getComplexFloatReal() { return FloatReal; }
1412 APFloat &getComplexFloatImag() { return FloatImag; }
1413
1414 void makeComplexInt() { IsInt = true; }
1415 bool isComplexInt() const { return IsInt; }
1416 APSInt &getComplexIntReal() { return IntReal; }
1417 APSInt &getComplexIntImag() { return IntImag; }
1418
1419 void moveInto(APValue &v) const {
1420 if (isComplexFloat())
1421 v = APValue(FloatReal, FloatImag);
1422 else
1423 v = APValue(IntReal, IntImag);
1424 }
1425 void setFrom(const APValue &v) {
1426 assert(v.isComplexFloat() || v.isComplexInt());
1427 if (v.isComplexFloat()) {
1428 makeComplexFloat();
1429 FloatReal = v.getComplexFloatReal();
1430 FloatImag = v.getComplexFloatImag();
1431 } else {
1432 makeComplexInt();
1433 IntReal = v.getComplexIntReal();
1434 IntImag = v.getComplexIntImag();
1435 }
1436 }
1437 };
1438
1439 struct LValue {
1440 APValue::LValueBase Base;
1441 CharUnits Offset;
1442 SubobjectDesignator Designator;
1443 bool IsNullPtr : 1;
1444 bool InvalidBase : 1;
1445 // P2280R4 track if we have an unknown reference or pointer.
1446 bool AllowConstexprUnknown = false;
1447
1448 const APValue::LValueBase getLValueBase() const { return Base; }
1449 bool allowConstexprUnknown() const { return AllowConstexprUnknown; }
1450 CharUnits &getLValueOffset() { return Offset; }
1451 const CharUnits &getLValueOffset() const { return Offset; }
1452 SubobjectDesignator &getLValueDesignator() { return Designator; }
1453 const SubobjectDesignator &getLValueDesignator() const { return Designator;}
1454 bool isNullPointer() const { return IsNullPtr;}
1455
1456 unsigned getLValueCallIndex() const { return Base.getCallIndex(); }
1457 unsigned getLValueVersion() const { return Base.getVersion(); }
1458
1459 bool pointsToCompleteClass(const CXXRecordDecl *D) const {
1460 if (Designator.Entries.empty())
1461 return true;
1462
1463 return Designator.MostDerivedType->getAsCXXRecordDecl() == D;
1464 }
1465
1466 void moveInto(APValue &V) const {
1467 if (Designator.Invalid)
1468 V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr);
1469 else {
1470 assert(!InvalidBase && "APValues can't handle invalid LValue bases");
1471 V = APValue(Base, Offset, Designator.Entries,
1472 Designator.IsOnePastTheEnd, IsNullPtr);
1473 }
1474 if (AllowConstexprUnknown)
1475 V.setConstexprUnknown();
1476 }
1477 void setFrom(const ASTContext &Ctx, const APValue &V) {
1478 assert(V.isLValue() && "Setting LValue from a non-LValue?");
1479 Base = V.getLValueBase();
1480 Offset = V.getLValueOffset();
1481 InvalidBase = false;
1482 Designator = SubobjectDesignator(Ctx, V);
1483 IsNullPtr = V.isNullPointer();
1484 AllowConstexprUnknown = V.allowConstexprUnknown();
1485 }
1486
1487 void set(APValue::LValueBase B, bool BInvalid = false) {
1488#ifndef NDEBUG
1489 // We only allow a few types of invalid bases. Enforce that here.
1490 if (BInvalid) {
1491 const auto *E = B.get<const Expr *>();
1492 assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) &&
1493 "Unexpected type of invalid base");
1494 }
1495#endif
1496
1497 Base = B;
1498 Offset = CharUnits::fromQuantity(Quantity: 0);
1499 InvalidBase = BInvalid;
1500 Designator = SubobjectDesignator(getType(B));
1501 IsNullPtr = false;
1502 AllowConstexprUnknown = false;
1503 }
1504
1505 void setNull(ASTContext &Ctx, QualType PointerTy) {
1506 Base = (const ValueDecl *)nullptr;
1507 Offset =
1508 CharUnits::fromQuantity(Quantity: Ctx.getTargetNullPointerValue(QT: PointerTy));
1509 InvalidBase = false;
1510 Designator = SubobjectDesignator(PointerTy->getPointeeType());
1511 IsNullPtr = true;
1512 AllowConstexprUnknown = false;
1513 }
1514
1515 void setInvalid(APValue::LValueBase B, unsigned I = 0) {
1516 set(B, BInvalid: true);
1517 }
1518
1519 std::string toString(ASTContext &Ctx, QualType T) const {
1520 APValue Printable;
1521 moveInto(V&: Printable);
1522 return Printable.getAsString(Ctx, Ty: T);
1523 }
1524
1525 private:
1526 // Check that this LValue is not based on a null pointer. If it is, produce
1527 // a diagnostic and mark the designator as invalid.
1528 template <typename GenDiagType>
1529 bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {
1530 if (Designator.Invalid)
1531 return false;
1532 if (IsNullPtr) {
1533 GenDiag();
1534 Designator.setInvalid();
1535 return false;
1536 }
1537 return true;
1538 }
1539
1540 public:
1541 bool checkNullPointer(EvalInfo &Info, const Expr *E,
1542 CheckSubobjectKind CSK) {
1543 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, CSK] {
1544 Info.CCEDiag(E, DiagId: diag::note_constexpr_null_subobject) << CSK;
1545 });
1546 }
1547
1548 bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E,
1549 AccessKinds AK) {
1550 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, AK] {
1551 if (AK == AccessKinds::AK_Dereference)
1552 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
1553 else
1554 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
1555 });
1556 }
1557
1558 // Check this LValue refers to an object. If not, set the designator to be
1559 // invalid and emit a diagnostic.
1560 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
1561 return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) &&
1562 Designator.checkSubobject(Info, E, CSK);
1563 }
1564
1565 void addDecl(EvalInfo &Info, const Expr *E,
1566 const Decl *D, bool Virtual = false) {
1567 if (checkSubobject(Info, E, CSK: isa<FieldDecl>(Val: D) ? CSK_Field : CSK_Base))
1568 Designator.addDeclUnchecked(D, Virtual);
1569 }
1570 void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) {
1571 if (!Designator.Entries.empty()) {
1572 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsupported_unsized_array);
1573 Designator.setInvalid();
1574 return;
1575 }
1576 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer)) {
1577 assert(!Base || getType(Base).getNonReferenceType()->isPointerType() ||
1578 getType(Base).getNonReferenceType()->isArrayType());
1579 Designator.FirstEntryIsAnUnsizedArray = true;
1580 Designator.addUnsizedArrayUnchecked(ElemTy);
1581 }
1582 }
1583 void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
1584 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer))
1585 Designator.addArrayUnchecked(CAT);
1586 }
1587 void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
1588 if (checkSubobject(Info, E, CSK: Imag ? CSK_Imag : CSK_Real))
1589 Designator.addComplexUnchecked(EltTy, Imag);
1590 }
1591 void addVectorElement(EvalInfo &Info, const Expr *E, QualType EltTy,
1592 uint64_t Size, uint64_t Idx) {
1593 if (checkSubobject(Info, E, CSK: CSK_VectorElement))
1594 Designator.addVectorElementUnchecked(EltTy, Size, Idx);
1595 }
1596 void clearIsNullPointer() {
1597 IsNullPtr = false;
1598 }
1599 void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E,
1600 const APSInt &Index, CharUnits ElementSize) {
1601 // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB,
1602 // but we're not required to diagnose it and it's valid in C++.)
1603 if (!Index)
1604 return;
1605
1606 // Compute the new offset in the appropriate width, wrapping at 64 bits.
1607 // FIXME: When compiling for a 32-bit target, we should use 32-bit
1608 // offsets.
1609 uint64_t Offset64 = Offset.getQuantity();
1610 uint64_t ElemSize64 = ElementSize.getQuantity();
1611 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
1612 Offset = CharUnits::fromQuantity(Quantity: Offset64 + ElemSize64 * Index64);
1613
1614 if (checkNullPointer(Info, E, CSK: CSK_ArrayIndex))
1615 Designator.adjustIndex(Info, E, N: Index, LV: *this);
1616 clearIsNullPointer();
1617 }
1618 void adjustOffset(CharUnits N) {
1619 Offset += N;
1620 if (N.getQuantity())
1621 clearIsNullPointer();
1622 }
1623 };
1624
1625 struct MemberPtr {
1626 MemberPtr() {}
1627 explicit MemberPtr(const ValueDecl *Decl)
1628 : DeclAndIsDerivedMember(Decl, false) {}
1629
1630 /// The member or (direct or indirect) field referred to by this member
1631 /// pointer, or 0 if this is a null member pointer.
1632 const ValueDecl *getDecl() const {
1633 return DeclAndIsDerivedMember.getPointer();
1634 }
1635 /// Is this actually a member of some type derived from the relevant class?
1636 bool isDerivedMember() const {
1637 return DeclAndIsDerivedMember.getInt();
1638 }
1639 /// Get the class which the declaration actually lives in.
1640 const CXXRecordDecl *getContainingRecord() const {
1641 return cast<CXXRecordDecl>(
1642 Val: DeclAndIsDerivedMember.getPointer()->getDeclContext());
1643 }
1644
1645 void moveInto(APValue &V) const {
1646 V = APValue(getDecl(), isDerivedMember(), Path);
1647 }
1648 void setFrom(const APValue &V) {
1649 assert(V.isMemberPointer());
1650 DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
1651 DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
1652 Path.clear();
1653 llvm::append_range(C&: Path, R: V.getMemberPointerPath());
1654 }
1655
1656 /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
1657 /// whether the member is a member of some class derived from the class type
1658 /// of the member pointer.
1659 llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
1660 /// Path - The path of base/derived classes from the member declaration's
1661 /// class (exclusive) to the class type of the member pointer (inclusive).
1662 SmallVector<const CXXRecordDecl*, 4> Path;
1663
1664 /// Perform a cast towards the class of the Decl (either up or down the
1665 /// hierarchy).
1666 bool castBack(const CXXRecordDecl *Class) {
1667 assert(!Path.empty());
1668 const CXXRecordDecl *Expected;
1669 if (Path.size() >= 2)
1670 Expected = Path[Path.size() - 2];
1671 else
1672 Expected = getContainingRecord();
1673 if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
1674 // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
1675 // if B does not contain the original member and is not a base or
1676 // derived class of the class containing the original member, the result
1677 // of the cast is undefined.
1678 // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
1679 // (D::*). We consider that to be a language defect.
1680 return false;
1681 }
1682 Path.pop_back();
1683 return true;
1684 }
1685 /// Perform a base-to-derived member pointer cast.
1686 bool castToDerived(const CXXRecordDecl *Derived) {
1687 if (!getDecl())
1688 return true;
1689 if (!isDerivedMember()) {
1690 Path.push_back(Elt: Derived);
1691 return true;
1692 }
1693 if (!castBack(Class: Derived))
1694 return false;
1695 if (Path.empty())
1696 DeclAndIsDerivedMember.setInt(false);
1697 return true;
1698 }
1699 /// Perform a derived-to-base member pointer cast.
1700 bool castToBase(const CXXRecordDecl *Base) {
1701 if (!getDecl())
1702 return true;
1703 if (Path.empty())
1704 DeclAndIsDerivedMember.setInt(true);
1705 if (isDerivedMember()) {
1706 Path.push_back(Elt: Base);
1707 return true;
1708 }
1709 return castBack(Class: Base);
1710 }
1711 };
1712
1713 /// Compare two member pointers, which are assumed to be of the same type.
1714 static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
1715 if (!LHS.getDecl() || !RHS.getDecl())
1716 return !LHS.getDecl() && !RHS.getDecl();
1717 if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
1718 return false;
1719 return LHS.Path == RHS.Path;
1720 }
1721}
1722
1723void SubobjectDesignator::adjustIndex(EvalInfo &Info, const Expr *E, APSInt N,
1724 const LValue &LV) {
1725 if (Invalid || !N)
1726 return;
1727 uint64_t TruncatedN = N.extOrTrunc(width: 64).getZExtValue();
1728 if (isMostDerivedAnUnsizedArray()) {
1729 diagnoseUnsizedArrayPointerArithmetic(Info, E);
1730 // Can't verify -- trust that the user is doing the right thing (or if
1731 // not, trust that the caller will catch the bad behavior).
1732 // FIXME: Should we reject if this overflows, at least?
1733 Entries.back() =
1734 PathEntry::ArrayIndex(Index: Entries.back().getAsArrayIndex() + TruncatedN);
1735 return;
1736 }
1737
1738 // [expr.add]p4: For the purposes of these operators, a pointer to a
1739 // nonarray object behaves the same as a pointer to the first element of
1740 // an array of length one with the type of the object as its element type.
1741 bool IsArray =
1742 MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement;
1743 uint64_t ArrayIndex =
1744 IsArray ? Entries.back().getAsArrayIndex() : (uint64_t)IsOnePastTheEnd;
1745 uint64_t ArraySize = IsArray ? getMostDerivedArraySize() : (uint64_t)1;
1746
1747 if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) {
1748 if (!Info.checkingPotentialConstantExpression() ||
1749 !LV.AllowConstexprUnknown) {
1750 // Calculate the actual index in a wide enough type, so we can include
1751 // it in the note.
1752 N = N.extend(width: std::max<unsigned>(a: N.getBitWidth() + 1, b: 65));
1753 (llvm::APInt &)N += ArrayIndex;
1754 assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index");
1755 diagnosePointerArithmetic(Info, E, N);
1756 }
1757 setInvalid();
1758 return;
1759 }
1760
1761 ArrayIndex += TruncatedN;
1762 assert(ArrayIndex <= ArraySize &&
1763 "bounds check succeeded for out-of-bounds index");
1764
1765 if (IsArray)
1766 Entries.back() = PathEntry::ArrayIndex(Index: ArrayIndex);
1767 else
1768 IsOnePastTheEnd = (ArrayIndex != 0);
1769}
1770
1771static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
1772static bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
1773 const LValue &This, const Expr *E,
1774 bool AllowNonLiteralTypes = false);
1775static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
1776 bool InvalidBaseOK = false);
1777static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info,
1778 bool InvalidBaseOK = false);
1779static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
1780 EvalInfo &Info);
1781static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
1782static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
1783static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
1784 EvalInfo &Info);
1785static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
1786static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
1787static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info);
1788static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
1789 EvalInfo &Info);
1790static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result);
1791static std::optional<uint64_t>
1792EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
1793 std::string *StringResult = nullptr);
1794
1795/// Evaluate an integer or fixed point expression into an APResult.
1796static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
1797 EvalInfo &Info);
1798
1799/// Evaluate only a fixed point expression into an APResult.
1800static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
1801 EvalInfo &Info);
1802
1803//===----------------------------------------------------------------------===//
1804// Misc utilities
1805//===----------------------------------------------------------------------===//
1806
1807/// Negate an APSInt in place, converting it to a signed form if necessary, and
1808/// preserving its value (by extending by up to one bit as needed).
1809static void negateAsSigned(APSInt &Int) {
1810 if (Int.isUnsigned() || Int.isMinSignedValue()) {
1811 Int = Int.extend(width: Int.getBitWidth() + 1);
1812 Int.setIsSigned(true);
1813 }
1814 Int = -Int;
1815}
1816
1817template<typename KeyT>
1818APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T,
1819 ScopeKind Scope, LValue &LV) {
1820 unsigned Version = getTempVersion();
1821 APValue::LValueBase Base(Key, Index, Version);
1822 LV.set(B: Base);
1823 return createLocal(Base, Key, T, Scope);
1824}
1825
1826/// Allocate storage for a parameter of a function call made in this frame.
1827APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD,
1828 LValue &LV) {
1829 assert(Args.CallIndex == Index && "creating parameter in wrong frame");
1830 APValue::LValueBase Base(PVD, Index, Args.Version);
1831 LV.set(B: Base);
1832 // We always destroy parameters at the end of the call, even if we'd allow
1833 // them to live to the end of the full-expression at runtime, in order to
1834 // give portable results and match other compilers.
1835 return createLocal(Base, Key: PVD, T: PVD->getType(), Scope: ScopeKind::Call);
1836}
1837
1838APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key,
1839 QualType T, ScopeKind Scope) {
1840 assert(Base.getCallIndex() == Index && "lvalue for wrong frame");
1841 unsigned Version = Base.getVersion();
1842 APValue &Result = Temporaries[MapKeyTy(Key, Version)];
1843 assert(Result.isAbsent() && "local created multiple times");
1844
1845 // If we're creating a local immediately in the operand of a speculative
1846 // evaluation, don't register a cleanup to be run outside the speculative
1847 // evaluation context, since we won't actually be able to initialize this
1848 // object.
1849 if (Index <= Info.SpeculativeEvaluationDepth) {
1850 if (T.isDestructedType())
1851 Info.noteSideEffect();
1852 } else {
1853 Info.CleanupStack.push_back(Elt: Cleanup(&Result, Base, T, Scope));
1854 }
1855 return Result;
1856}
1857
1858APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) {
1859 if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) {
1860 FFDiag(E, DiagId: diag::note_constexpr_heap_alloc_limit_exceeded);
1861 return nullptr;
1862 }
1863
1864 DynamicAllocLValue DA(NumHeapAllocs++);
1865 LV.set(B: APValue::LValueBase::getDynamicAlloc(LV: DA, Type: T));
1866 auto Result = HeapAllocs.emplace(args: std::piecewise_construct,
1867 args: std::forward_as_tuple(args&: DA), args: std::tuple<>());
1868 assert(Result.second && "reused a heap alloc index?");
1869 Result.first->second.AllocExpr = E;
1870 return &Result.first->second.Value;
1871}
1872
1873/// Produce a string describing the given constexpr call.
1874void CallStackFrame::describe(raw_ostream &Out) const {
1875 bool IsMemberCall = false;
1876 bool ExplicitInstanceParam = false;
1877 clang::PrintingPolicy PrintingPolicy = Info.Ctx.getPrintingPolicy();
1878 PrintingPolicy.SuppressLambdaBody = true;
1879
1880 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee)) {
1881 IsMemberCall = !isa<CXXConstructorDecl>(Val: MD) && !MD->isStatic();
1882 ExplicitInstanceParam = MD->isExplicitObjectMemberFunction();
1883 }
1884
1885 if (!IsMemberCall)
1886 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1887 /*Qualified=*/false);
1888
1889 if (This && IsMemberCall) {
1890 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Val: CallExpr)) {
1891 const Expr *Object = MCE->getImplicitObjectArgument();
1892 Object->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1893 /*Indentation=*/0);
1894 if (Object->getType()->isPointerType())
1895 Out << "->";
1896 else
1897 Out << ".";
1898 } else if (const auto *OCE =
1899 dyn_cast_if_present<CXXOperatorCallExpr>(Val: CallExpr)) {
1900 OCE->getArg(Arg: 0)->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1901 /*Indentation=*/0);
1902 Out << ".";
1903 } else {
1904 APValue Val;
1905 This->moveInto(V&: Val);
1906 Val.printPretty(
1907 OS&: Out, Ctx: Info.Ctx,
1908 Ty: Info.Ctx.getLValueReferenceType(T: This->Designator.MostDerivedType));
1909 Out << ".";
1910 }
1911 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1912 /*Qualified=*/false);
1913 }
1914
1915 Out << '(';
1916
1917 llvm::ListSeparator Comma;
1918 for (const ParmVarDecl *Param :
1919 Callee->parameters().slice(N: ExplicitInstanceParam)) {
1920 Out << Comma;
1921 const APValue *V = Info.getParamSlot(Call: Arguments, PVD: Param);
1922 if (V)
1923 V->printPretty(OS&: Out, Ctx: Info.Ctx, Ty: Param->getType());
1924 else
1925 Out << "<...>";
1926 }
1927
1928 Out << ')';
1929}
1930
1931/// Evaluate an expression to see if it had side-effects, and discard its
1932/// result.
1933/// \return \c true if the caller should keep evaluating.
1934static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) {
1935 assert(!E->isValueDependent());
1936 APValue Scratch;
1937 if (!Evaluate(Result&: Scratch, Info, E))
1938 // We don't need the value, but we might have skipped a side effect here.
1939 return Info.noteSideEffect();
1940 return true;
1941}
1942
1943/// Should this call expression be treated as forming an opaque constant?
1944static bool IsOpaqueConstantCall(const CallExpr *E) {
1945 unsigned Builtin = E->getBuiltinCallee();
1946 return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
1947 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
1948 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
1949 Builtin == Builtin::BI__builtin_function_start);
1950}
1951
1952static bool IsOpaqueConstantCall(const LValue &LVal) {
1953 const auto *BaseExpr =
1954 llvm::dyn_cast_if_present<CallExpr>(Val: LVal.Base.dyn_cast<const Expr *>());
1955 return BaseExpr && IsOpaqueConstantCall(E: BaseExpr);
1956}
1957
1958static bool IsGlobalLValue(APValue::LValueBase B) {
1959 // C++11 [expr.const]p3 An address constant expression is a prvalue core
1960 // constant expression of pointer type that evaluates to...
1961
1962 // ... a null pointer value, or a prvalue core constant expression of type
1963 // std::nullptr_t.
1964 if (!B)
1965 return true;
1966
1967 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
1968 // ... the address of an object with static storage duration,
1969 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
1970 return VD->hasGlobalStorage();
1971 if (isa<TemplateParamObjectDecl>(Val: D))
1972 return true;
1973 // ... the address of a function,
1974 // ... the address of a GUID [MS extension],
1975 // ... the address of an unnamed global constant
1976 return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(Val: D);
1977 }
1978
1979 if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>())
1980 return true;
1981
1982 const Expr *E = B.get<const Expr*>();
1983 switch (E->getStmtClass()) {
1984 default:
1985 return false;
1986 case Expr::CompoundLiteralExprClass: {
1987 const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(Val: E);
1988 return CLE->isFileScope() && CLE->isLValue();
1989 }
1990 case Expr::MaterializeTemporaryExprClass:
1991 // A materialized temporary might have been lifetime-extended to static
1992 // storage duration.
1993 return cast<MaterializeTemporaryExpr>(Val: E)->getStorageDuration() == SD_Static;
1994 // A string literal has static storage duration.
1995 case Expr::StringLiteralClass:
1996 case Expr::PredefinedExprClass:
1997 case Expr::ObjCStringLiteralClass:
1998 case Expr::ObjCEncodeExprClass:
1999 return true;
2000 case Expr::ObjCBoxedExprClass:
2001 case Expr::ObjCArrayLiteralClass:
2002 case Expr::ObjCDictionaryLiteralClass:
2003 return cast<ObjCObjectLiteral>(Val: E)->isExpressibleAsConstantInitializer();
2004 case Expr::CallExprClass:
2005 return IsOpaqueConstantCall(E: cast<CallExpr>(Val: E));
2006 // For GCC compatibility, &&label has static storage duration.
2007 case Expr::AddrLabelExprClass:
2008 return true;
2009 // A Block literal expression may be used as the initialization value for
2010 // Block variables at global or local static scope.
2011 case Expr::BlockExprClass:
2012 return !cast<BlockExpr>(Val: E)->getBlockDecl()->hasCaptures();
2013 // The APValue generated from a __builtin_source_location will be emitted as a
2014 // literal.
2015 case Expr::SourceLocExprClass:
2016 return true;
2017 case Expr::ImplicitValueInitExprClass:
2018 // FIXME:
2019 // We can never form an lvalue with an implicit value initialization as its
2020 // base through expression evaluation, so these only appear in one case: the
2021 // implicit variable declaration we invent when checking whether a constexpr
2022 // constructor can produce a constant expression. We must assume that such
2023 // an expression might be a global lvalue.
2024 return true;
2025 }
2026}
2027
2028static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
2029 return LVal.Base.dyn_cast<const ValueDecl*>();
2030}
2031
2032// Information about an LValueBase that is some kind of string.
2033struct LValueBaseString {
2034 std::string ObjCEncodeStorage;
2035 StringRef Bytes;
2036 int CharWidth;
2037};
2038
2039// Gets the lvalue base of LVal as a string.
2040static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal,
2041 LValueBaseString &AsString) {
2042 const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>();
2043 if (!BaseExpr)
2044 return false;
2045
2046 // For ObjCEncodeExpr, we need to compute and store the string.
2047 if (const auto *EE = dyn_cast<ObjCEncodeExpr>(Val: BaseExpr)) {
2048 Info.Ctx.getObjCEncodingForType(T: EE->getEncodedType(),
2049 S&: AsString.ObjCEncodeStorage);
2050 AsString.Bytes = AsString.ObjCEncodeStorage;
2051 AsString.CharWidth = 1;
2052 return true;
2053 }
2054
2055 // Otherwise, we have a StringLiteral.
2056 const auto *Lit = dyn_cast<StringLiteral>(Val: BaseExpr);
2057 if (const auto *PE = dyn_cast<PredefinedExpr>(Val: BaseExpr))
2058 Lit = PE->getFunctionName();
2059
2060 if (!Lit)
2061 return false;
2062
2063 AsString.Bytes = Lit->getBytes();
2064 AsString.CharWidth = Lit->getCharByteWidth();
2065 return true;
2066}
2067
2068// Determine whether two string literals potentially overlap. This will be the
2069// case if they agree on the values of all the bytes on the overlapping region
2070// between them.
2071//
2072// The overlapping region is the portion of the two string literals that must
2073// overlap in memory if the pointers actually point to the same address at
2074// runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then
2075// the overlapping region is "cdef\0", which in this case does agree, so the
2076// strings are potentially overlapping. Conversely, for "foobar" + 3 versus
2077// "bazbar" + 3, the overlapping region contains all of both strings, so they
2078// are not potentially overlapping, even though they agree from the given
2079// addresses onwards.
2080//
2081// See open core issue CWG2765 which is discussing the desired rule here.
2082static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info,
2083 const LValue &LHS,
2084 const LValue &RHS) {
2085 LValueBaseString LHSString, RHSString;
2086 if (!GetLValueBaseAsString(Info, LVal: LHS, AsString&: LHSString) ||
2087 !GetLValueBaseAsString(Info, LVal: RHS, AsString&: RHSString))
2088 return false;
2089
2090 // This is the byte offset to the location of the first character of LHS
2091 // within RHS. We don't need to look at the characters of one string that
2092 // would appear before the start of the other string if they were merged.
2093 CharUnits Offset = RHS.Offset - LHS.Offset;
2094 if (Offset.isNegative()) {
2095 if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity())
2096 return false;
2097 LHSString.Bytes = LHSString.Bytes.drop_front(N: -Offset.getQuantity());
2098 } else {
2099 if (RHSString.Bytes.size() < (size_t)Offset.getQuantity())
2100 return false;
2101 RHSString.Bytes = RHSString.Bytes.drop_front(N: Offset.getQuantity());
2102 }
2103
2104 bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size();
2105 StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes;
2106 StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes;
2107 int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth;
2108
2109 // The null terminator isn't included in the string data, so check for it
2110 // manually. If the longer string doesn't have a null terminator where the
2111 // shorter string ends, they aren't potentially overlapping.
2112 for (int NullByte : llvm::seq(Size: ShorterCharWidth)) {
2113 if (Shorter.size() + NullByte >= Longer.size())
2114 break;
2115 if (Longer[Shorter.size() + NullByte])
2116 return false;
2117 }
2118
2119 // Otherwise, they're potentially overlapping if and only if the overlapping
2120 // region is the same.
2121 return Shorter == Longer.take_front(N: Shorter.size());
2122}
2123
2124static bool IsWeakLValue(const LValue &Value) {
2125 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2126 return Decl && Decl->isWeak();
2127}
2128
2129static bool isZeroSized(const LValue &Value) {
2130 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2131 if (isa_and_nonnull<VarDecl>(Val: Decl)) {
2132 QualType Ty = Decl->getType();
2133 if (Ty->isArrayType())
2134 return Ty->isIncompleteType() ||
2135 Decl->getASTContext().getTypeSize(T: Ty) == 0;
2136 }
2137 return false;
2138}
2139
2140static bool HasSameBase(const LValue &A, const LValue &B) {
2141 if (!A.getLValueBase())
2142 return !B.getLValueBase();
2143 if (!B.getLValueBase())
2144 return false;
2145
2146 if (A.getLValueBase().getOpaqueValue() !=
2147 B.getLValueBase().getOpaqueValue())
2148 return false;
2149
2150 return A.getLValueCallIndex() == B.getLValueCallIndex() &&
2151 A.getLValueVersion() == B.getLValueVersion();
2152}
2153
2154static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
2155 assert(Base && "no location for a null lvalue");
2156 const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
2157
2158 // For a parameter, find the corresponding call stack frame (if it still
2159 // exists), and point at the parameter of the function definition we actually
2160 // invoked.
2161 if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(Val: VD)) {
2162 unsigned Idx = PVD->getFunctionScopeIndex();
2163 for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) {
2164 if (F->Arguments.CallIndex == Base.getCallIndex() &&
2165 F->Arguments.Version == Base.getVersion() && F->Callee &&
2166 Idx < F->Callee->getNumParams()) {
2167 VD = F->Callee->getParamDecl(i: Idx);
2168 break;
2169 }
2170 }
2171 }
2172
2173 if (VD)
2174 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
2175 else if (const Expr *E = Base.dyn_cast<const Expr*>())
2176 Info.Note(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
2177 else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
2178 // FIXME: Produce a note for dangling pointers too.
2179 if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA))
2180 Info.Note(Loc: (*Alloc)->AllocExpr->getExprLoc(),
2181 DiagId: diag::note_constexpr_dynamic_alloc_here);
2182 }
2183
2184 // We have no information to show for a typeid(T) object.
2185}
2186
2187enum class CheckEvaluationResultKind {
2188 ConstantExpression,
2189 FullyInitialized,
2190};
2191
2192/// Materialized temporaries that we've already checked to determine if they're
2193/// initializsed by a constant expression.
2194using CheckedTemporaries =
2195 llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>;
2196
2197static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2198 EvalInfo &Info, SourceLocation DiagLoc,
2199 QualType Type, const APValue &Value,
2200 ConstantExprKind Kind,
2201 const FieldDecl *SubobjectDecl,
2202 CheckedTemporaries &CheckedTemps,
2203 bool IsCompleteClass = true);
2204
2205/// Check that this reference or pointer core constant expression is a valid
2206/// value for an address or reference constant expression. Return true if we
2207/// can fold this expression, whether or not it's a constant expression.
2208static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
2209 QualType Type, const LValue &LVal,
2210 ConstantExprKind Kind,
2211 CheckedTemporaries &CheckedTemps) {
2212 bool IsReferenceType = Type->isReferenceType();
2213
2214 APValue::LValueBase Base = LVal.getLValueBase();
2215 const SubobjectDesignator &Designator = LVal.getLValueDesignator();
2216
2217 const Expr *BaseE = Base.dyn_cast<const Expr *>();
2218 const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>();
2219
2220 // Additional restrictions apply in a template argument. We only enforce the
2221 // C++20 restrictions here; additional syntactic and semantic restrictions
2222 // are applied elsewhere.
2223 if (isTemplateArgument(Kind)) {
2224 int InvalidBaseKind = -1;
2225 StringRef Ident;
2226 if (Base.is<TypeInfoLValue>())
2227 InvalidBaseKind = 0;
2228 else if (isa_and_nonnull<StringLiteral>(Val: BaseE))
2229 InvalidBaseKind = 1;
2230 else if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) ||
2231 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD))
2232 InvalidBaseKind = 2;
2233 else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(Val: BaseE)) {
2234 InvalidBaseKind = 3;
2235 Ident = PE->getIdentKindName();
2236 }
2237
2238 if (InvalidBaseKind != -1) {
2239 Info.FFDiag(Loc, DiagId: diag::note_constexpr_invalid_template_arg)
2240 << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind
2241 << Ident;
2242 return false;
2243 }
2244 }
2245
2246 if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: BaseVD);
2247 FD && FD->isImmediateFunction()) {
2248 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible)
2249 << !Type->isAnyPointerType();
2250 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2251 return false;
2252 }
2253
2254 // Check that the object is a global. Note that the fake 'this' object we
2255 // manufacture when checking potential constant expressions is conservatively
2256 // assumed to be global here.
2257 if (!IsGlobalLValue(B: Base)) {
2258 if (Info.getLangOpts().CPlusPlus11) {
2259 Info.FFDiag(Loc, DiagId: diag::note_constexpr_non_global, ExtraNotes: 1)
2260 << IsReferenceType << !Designator.Entries.empty() << !!BaseVD
2261 << BaseVD;
2262 auto *VarD = dyn_cast_or_null<VarDecl>(Val: BaseVD);
2263 if (VarD && VarD->isConstexpr()) {
2264 // Non-static local constexpr variables have unintuitive semantics:
2265 // constexpr int a = 1;
2266 // constexpr const int *p = &a;
2267 // ... is invalid because the address of 'a' is not constant. Suggest
2268 // adding a 'static' in this case.
2269 Info.Note(Loc: VarD->getLocation(), DiagId: diag::note_constexpr_not_static)
2270 << VarD
2271 << FixItHint::CreateInsertion(InsertionLoc: VarD->getBeginLoc(), Code: "static ");
2272 } else {
2273 NoteLValueLocation(Info, Base);
2274 }
2275 } else {
2276 Info.FFDiag(Loc);
2277 }
2278 // Don't allow references to temporaries to escape.
2279 return false;
2280 }
2281 assert((Info.checkingPotentialConstantExpression() ||
2282 LVal.getLValueCallIndex() == 0) &&
2283 "have call index for global lvalue");
2284
2285 if (LVal.allowConstexprUnknown()) {
2286 if (BaseVD) {
2287 Info.FFDiag(Loc, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << BaseVD;
2288 NoteLValueLocation(Info, Base);
2289 } else {
2290 Info.FFDiag(Loc);
2291 }
2292 return false;
2293 }
2294
2295 if (Base.is<DynamicAllocLValue>()) {
2296 Info.FFDiag(Loc, DiagId: diag::note_constexpr_dynamic_alloc)
2297 << IsReferenceType << !Designator.Entries.empty();
2298 NoteLValueLocation(Info, Base);
2299 return false;
2300 }
2301
2302 if (BaseVD) {
2303 if (const VarDecl *Var = dyn_cast<const VarDecl>(Val: BaseVD)) {
2304 // Check if this is a thread-local variable.
2305 if (Var->getTLSKind())
2306 // FIXME: Diagnostic!
2307 return false;
2308
2309 // A dllimport variable never acts like a constant, unless we're
2310 // evaluating a value for use only in name mangling, and unless it's a
2311 // static local. For the latter case, we'd still need to evaluate the
2312 // constant expression in case we're inside a (inlined) function.
2313 if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>() &&
2314 !Var->isStaticLocal())
2315 return false;
2316
2317 // Address of a managed variable is never a constant expression.
2318 if (Info.getLangOpts().CUDA && Var->hasAttr<HIPManagedAttr>())
2319 return false;
2320
2321 // In CUDA/HIP device compilation, only device side variables have
2322 // constant addresses.
2323 if (Info.getLangOpts().CUDA && Info.getLangOpts().CUDAIsDevice &&
2324 Info.Ctx.CUDAConstantEvalCtx.NoWrongSidedVars) {
2325 if ((!Var->hasAttr<CUDADeviceAttr>() &&
2326 !Var->hasAttr<CUDAConstantAttr>() &&
2327 !Var->getType()->isCUDADeviceBuiltinSurfaceType() &&
2328 !Var->getType()->isCUDADeviceBuiltinTextureType()))
2329 return false;
2330 }
2331 }
2332 if (const auto *FD = dyn_cast<const FunctionDecl>(Val: BaseVD)) {
2333 // __declspec(dllimport) must be handled very carefully:
2334 // We must never initialize an expression with the thunk in C++.
2335 // Doing otherwise would allow the same id-expression to yield
2336 // different addresses for the same function in different translation
2337 // units. However, this means that we must dynamically initialize the
2338 // expression with the contents of the import address table at runtime.
2339 //
2340 // The C language has no notion of ODR; furthermore, it has no notion of
2341 // dynamic initialization. This means that we are permitted to
2342 // perform initialization with the address of the thunk.
2343 if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) &&
2344 FD->hasAttr<DLLImportAttr>())
2345 // FIXME: Diagnostic!
2346 return false;
2347 }
2348 } else if (const auto *MTE =
2349 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: BaseE)) {
2350 if (CheckedTemps.insert(Ptr: MTE).second) {
2351 QualType TempType = getType(B: Base);
2352 if (TempType.isDestructedType()) {
2353 Info.FFDiag(Loc: MTE->getExprLoc(),
2354 DiagId: diag::note_constexpr_unsupported_temporary_nontrivial_dtor)
2355 << TempType;
2356 return false;
2357 }
2358
2359 APValue *V = MTE->getOrCreateValue(MayCreate: false);
2360 assert(V && "evasluation result refers to uninitialised temporary");
2361 if (!CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2362 Info, DiagLoc: MTE->getExprLoc(), Type: TempType, Value: *V, Kind,
2363 /*SubobjectDecl=*/nullptr, CheckedTemps))
2364 return false;
2365 }
2366 }
2367
2368 // Allow address constant expressions to be past-the-end pointers. This is
2369 // an extension: the standard requires them to point to an object.
2370 if (!IsReferenceType)
2371 return true;
2372
2373 // A reference constant expression must refer to an object.
2374 if (!Base) {
2375 // FIXME: diagnostic
2376 Info.CCEDiag(Loc);
2377 return true;
2378 }
2379
2380 // Does this refer one past the end of some object?
2381 if (!Designator.Invalid && Designator.isOnePastTheEnd()) {
2382 Info.FFDiag(Loc, DiagId: diag::note_constexpr_past_end, ExtraNotes: 1)
2383 << !Designator.Entries.empty() << !!BaseVD << BaseVD;
2384 NoteLValueLocation(Info, Base);
2385 }
2386
2387 return true;
2388}
2389
2390/// Member pointers are constant expressions unless they point to a
2391/// non-virtual dllimport member function.
2392static bool CheckMemberPointerConstantExpression(EvalInfo &Info,
2393 SourceLocation Loc,
2394 QualType Type,
2395 const APValue &Value,
2396 ConstantExprKind Kind) {
2397 const ValueDecl *Member = Value.getMemberPointerDecl();
2398 const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Val: Member);
2399 if (!FD)
2400 return true;
2401 if (FD->isImmediateFunction()) {
2402 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible) << /*pointer*/ 0;
2403 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2404 return false;
2405 }
2406 return isForManglingOnly(Kind) || FD->isVirtual() ||
2407 !FD->hasAttr<DLLImportAttr>();
2408}
2409
2410/// Check that this core constant expression is of literal type, and if not,
2411/// produce an appropriate diagnostic.
2412static bool CheckLiteralType(EvalInfo &Info, const Expr *E,
2413 const LValue *This = nullptr) {
2414 // The restriction to literal types does not exist in C++23 anymore.
2415 if (Info.getLangOpts().CPlusPlus23)
2416 return true;
2417
2418 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Info.Ctx))
2419 return true;
2420
2421 // C++1y: A constant initializer for an object o [...] may also invoke
2422 // constexpr constructors for o and its subobjects even if those objects
2423 // are of non-literal class types.
2424 //
2425 // C++11 missed this detail for aggregates, so classes like this:
2426 // struct foo_t { union { int i; volatile int j; } u; };
2427 // are not (obviously) initializable like so:
2428 // __attribute__((__require_constant_initialization__))
2429 // static const foo_t x = {{0}};
2430 // because "i" is a subobject with non-literal initialization (due to the
2431 // volatile member of the union). See:
2432 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
2433 // Therefore, we use the C++1y behavior.
2434 if (This && Info.EvaluatingDecl == This->getLValueBase())
2435 return true;
2436
2437 // Prvalue constant expressions must be of literal types.
2438 if (Info.getLangOpts().CPlusPlus11)
2439 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral)
2440 << E->getType();
2441 else
2442 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
2443 return false;
2444}
2445
2446static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2447 EvalInfo &Info, SourceLocation DiagLoc,
2448 QualType Type, const APValue &Value,
2449 ConstantExprKind Kind,
2450 const FieldDecl *SubobjectDecl,
2451 CheckedTemporaries &CheckedTemps,
2452 bool IsCompleteClass) {
2453 if (!Value.hasValue()) {
2454 if (SubobjectDecl) {
2455 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2456 << /*(name)*/ 1 << SubobjectDecl;
2457 Info.Note(Loc: SubobjectDecl->getLocation(),
2458 DiagId: diag::note_constexpr_subobject_declared_here);
2459 } else {
2460 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2461 << /*of type*/ 0 << Type;
2462 }
2463 return false;
2464 }
2465
2466 // We allow _Atomic(T) to be initialized from anything that T can be
2467 // initialized from.
2468 if (const AtomicType *AT = Type->getAs<AtomicType>())
2469 Type = AT->getValueType();
2470
2471 // Core issue 1454: For a literal constant expression of array or class type,
2472 // each subobject of its value shall have been initialized by a constant
2473 // expression.
2474 if (Value.isArray()) {
2475 QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
2476 for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
2477 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2478 Value: Value.getArrayInitializedElt(I), Kind,
2479 SubobjectDecl, CheckedTemps))
2480 return false;
2481 }
2482 if (!Value.hasArrayFiller())
2483 return true;
2484 return CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2485 Value: Value.getArrayFiller(), Kind, SubobjectDecl,
2486 CheckedTemps);
2487 }
2488 if (Value.isUnion() && Value.getUnionField()) {
2489 return CheckEvaluationResult(
2490 CERK, Info, DiagLoc, Type: Value.getUnionField()->getType(),
2491 Value: Value.getUnionValue(), Kind, SubobjectDecl: Value.getUnionField(), CheckedTemps);
2492 }
2493 if (Value.isStruct()) {
2494 auto *RD = Type->castAsRecordDecl();
2495 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2496 unsigned BaseIndex = 0;
2497 for (const CXXBaseSpecifier &BS : CD->bases()) {
2498 if (BS.isVirtual())
2499 continue;
2500 const APValue &BaseValue = Value.getStructBase(i: BaseIndex);
2501 if (!BaseValue.hasValue()) {
2502 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2503 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2504 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2505 return false;
2506 }
2507 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(), Value: BaseValue,
2508 Kind, /*SubobjectDecl=*/nullptr,
2509 CheckedTemps, /*IsCompleteClass=*/false))
2510 return false;
2511 ++BaseIndex;
2512 }
2513 }
2514 for (const auto *I : RD->fields()) {
2515 if (I->isUnnamedBitField())
2516 continue;
2517
2518 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: I->getType(),
2519 Value: Value.getStructField(i: I->getFieldIndex()), Kind,
2520 SubobjectDecl: I, CheckedTemps))
2521 return false;
2522 }
2523
2524 if (IsCompleteClass) {
2525 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2526 unsigned BaseIndex = 0;
2527 for (const CXXBaseSpecifier &BS : CD->vbases()) {
2528 assert(BS.isVirtual());
2529 const APValue &BaseValue = Value.getStructVirtualBase(i: BaseIndex);
2530 if (!BaseValue.hasValue()) {
2531 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2532 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2533 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2534 return false;
2535 }
2536 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(),
2537 Value: BaseValue, Kind, /*SubobjectDecl=*/nullptr,
2538 CheckedTemps, /*IsCompleteClass=*/false))
2539 return false;
2540 ++BaseIndex;
2541 }
2542 }
2543 }
2544 }
2545
2546 if (Value.isLValue() &&
2547 CERK == CheckEvaluationResultKind::ConstantExpression) {
2548 LValue LVal;
2549 LVal.setFrom(Ctx: Info.Ctx, V: Value);
2550 return CheckLValueConstantExpression(Info, Loc: DiagLoc, Type, LVal, Kind,
2551 CheckedTemps);
2552 }
2553
2554 if (Value.isMemberPointer() &&
2555 CERK == CheckEvaluationResultKind::ConstantExpression)
2556 return CheckMemberPointerConstantExpression(Info, Loc: DiagLoc, Type, Value, Kind);
2557
2558 // Everything else is fine.
2559 return true;
2560}
2561
2562/// Check that this core constant expression value is a valid value for a
2563/// constant expression. If not, report an appropriate diagnostic. Does not
2564/// check that the expression is of literal type.
2565static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
2566 QualType Type, const APValue &Value,
2567 ConstantExprKind Kind) {
2568 // Nothing to check for a constant expression of type 'cv void'.
2569 if (Type->isVoidType())
2570 return true;
2571
2572 CheckedTemporaries CheckedTemps;
2573 return CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2574 Info, DiagLoc, Type, Value, Kind,
2575 /*SubobjectDecl=*/nullptr, CheckedTemps);
2576}
2577
2578/// Check that this evaluated value is fully-initialized and can be loaded by
2579/// an lvalue-to-rvalue conversion.
2580static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc,
2581 QualType Type, const APValue &Value) {
2582 CheckedTemporaries CheckedTemps;
2583 return CheckEvaluationResult(
2584 CERK: CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value,
2585 Kind: ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps);
2586}
2587
2588/// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless
2589/// "the allocated storage is deallocated within the evaluation".
2590static bool CheckMemoryLeaks(EvalInfo &Info) {
2591 if (!Info.HeapAllocs.empty()) {
2592 // We can still fold to a constant despite a compile-time memory leak,
2593 // so long as the heap allocation isn't referenced in the result (we check
2594 // that in CheckConstantExpression).
2595 Info.CCEDiag(E: Info.HeapAllocs.begin()->second.AllocExpr,
2596 DiagId: diag::note_constexpr_memory_leak)
2597 << unsigned(Info.HeapAllocs.size() - 1);
2598 }
2599 return true;
2600}
2601
2602static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
2603 // A null base expression indicates a null pointer. These are always
2604 // evaluatable, and they are false unless the offset is zero.
2605 if (!Value.getLValueBase()) {
2606 // TODO: Should a non-null pointer with an offset of zero evaluate to true?
2607 Result = !Value.getLValueOffset().isZero();
2608 return true;
2609 }
2610
2611 // We have a non-null base. These are generally known to be true, but if it's
2612 // a weak declaration it can be null at runtime.
2613 Result = true;
2614 const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
2615 return !Decl || !Decl->isWeak();
2616}
2617
2618static bool HandleConversionToBool(const APValue &Val, bool &Result) {
2619 // TODO: This function should produce notes if it fails.
2620 switch (Val.getKind()) {
2621 case APValue::None:
2622 case APValue::Indeterminate:
2623 return false;
2624 case APValue::Int:
2625 Result = Val.getInt().getBoolValue();
2626 return true;
2627 case APValue::FixedPoint:
2628 Result = Val.getFixedPoint().getBoolValue();
2629 return true;
2630 case APValue::Float:
2631 Result = !Val.getFloat().isZero();
2632 return true;
2633 case APValue::ComplexInt:
2634 Result = Val.getComplexIntReal().getBoolValue() ||
2635 Val.getComplexIntImag().getBoolValue();
2636 return true;
2637 case APValue::ComplexFloat:
2638 Result = !Val.getComplexFloatReal().isZero() ||
2639 !Val.getComplexFloatImag().isZero();
2640 return true;
2641 case APValue::LValue:
2642 return EvalPointerValueAsBool(Value: Val, Result);
2643 case APValue::MemberPointer:
2644 if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) {
2645 return false;
2646 }
2647 Result = Val.getMemberPointerDecl();
2648 return true;
2649 case APValue::Vector:
2650 case APValue::Matrix:
2651 case APValue::Array:
2652 case APValue::Struct:
2653 case APValue::Union:
2654 case APValue::AddrLabelDiff:
2655 return false;
2656 }
2657
2658 llvm_unreachable("unknown APValue kind");
2659}
2660
2661static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
2662 EvalInfo &Info) {
2663 assert(!E->isValueDependent());
2664 assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition");
2665 APValue Val;
2666 if (!Evaluate(Result&: Val, Info, E))
2667 return false;
2668 return HandleConversionToBool(Val, Result);
2669}
2670
2671template<typename T>
2672static bool HandleOverflow(EvalInfo &Info, const Expr *E,
2673 const T &SrcValue, QualType DestType) {
2674 Info.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << SrcValue << DestType;
2675 if (const auto *OBT = DestType->getAs<OverflowBehaviorType>();
2676 OBT && OBT->isTrapKind()) {
2677 return false;
2678 }
2679 return Info.noteUndefinedBehavior();
2680}
2681
2682static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
2683 QualType SrcType, const APFloat &Value,
2684 QualType DestType, APSInt &Result) {
2685 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2686 // Determine whether we are converting to unsigned or signed.
2687 bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
2688
2689 Result = APSInt(DestWidth, !DestSigned);
2690 bool ignored;
2691 if (Value.convertToInteger(Result, RM: llvm::APFloat::rmTowardZero, IsExact: &ignored)
2692 & APFloat::opInvalidOp)
2693 return HandleOverflow(Info, E, SrcValue: Value, DestType);
2694 return true;
2695}
2696
2697/// Get rounding mode to use in evaluation of the specified expression.
2698///
2699/// If rounding mode is unknown at compile time, still try to evaluate the
2700/// expression. If the result is exact, it does not depend on rounding mode.
2701/// So return "tonearest" mode instead of "dynamic".
2702static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) {
2703 llvm::RoundingMode RM =
2704 E->getFPFeaturesInEffect(LO: Info.getLangOpts()).getRoundingMode();
2705 if (RM == llvm::RoundingMode::Dynamic)
2706 RM = llvm::RoundingMode::NearestTiesToEven;
2707 return RM;
2708}
2709
2710/// Check if the given floating-point evaluation result is allowed for
2711/// compile-time constant folding during translation (as opposed to mandatory
2712/// constant expression evaluation).
2713static bool checkFloatingPointResultForConstantFolding(EvalInfo &Info,
2714 const Expr *E,
2715 APFloat::opStatus St) {
2716 // In a constant context, assume that any dynamic rounding mode or FP
2717 // exception state matches the default floating-point environment.
2718 if (Info.InConstantContext)
2719 return true;
2720
2721 FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.getLangOpts());
2722 if ((St & APFloat::opInexact) &&
2723 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
2724 // Inexact result means that it depends on rounding mode. If the requested
2725 // mode is dynamic, the evaluation cannot be made in compile time.
2726 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
2727 return false;
2728 }
2729
2730 if ((St != APFloat::opOK) &&
2731 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
2732 FPO.getExceptionMode() != LangOptions::FPE_Ignore ||
2733 FPO.getAllowFEnvAccess())) {
2734 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
2735 return false;
2736 }
2737
2738 if ((St & APFloat::opStatus::opInvalidOp) &&
2739 FPO.getExceptionMode() != LangOptions::FPE_Ignore) {
2740 // There is no usefully definable result.
2741 Info.FFDiag(E);
2742 return false;
2743 }
2744
2745 // FIXME: if:
2746 // - evaluation triggered other FP exception, and
2747 // - exception mode is not "ignore", and
2748 // - the expression being evaluated is not a part of global variable
2749 // initializer,
2750 // the evaluation probably need to be rejected.
2751 return true;
2752}
2753
2754static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
2755 QualType SrcType, QualType DestType,
2756 APFloat &Result) {
2757 assert((isa<CastExpr>(E) || isa<CompoundAssignOperator>(E) ||
2758 isa<ConvertVectorExpr>(E)) &&
2759 "HandleFloatToFloatCast has been checked with only CastExpr, "
2760 "CompoundAssignOperator and ConvertVectorExpr. Please either validate "
2761 "the new expression or address the root cause of this usage.");
2762 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2763 APFloat::opStatus St;
2764 APFloat Value = Result;
2765 bool ignored;
2766 St = Result.convert(ToSemantics: Info.Ctx.getFloatTypeSemantics(T: DestType), RM, losesInfo: &ignored);
2767 return checkFloatingPointResultForConstantFolding(Info, E, St);
2768}
2769
2770static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
2771 QualType DestType, QualType SrcType,
2772 const APSInt &Value) {
2773 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2774 // Figure out if this is a truncate, extend or noop cast.
2775 // If the input is signed, do a sign extend, noop, or truncate.
2776 APSInt Result = Value.extOrTrunc(width: DestWidth);
2777 Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
2778 if (DestType->isBooleanType())
2779 Result = Value.getBoolValue();
2780 return Result;
2781}
2782
2783static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
2784 const FPOptions FPO,
2785 QualType SrcType, const APSInt &Value,
2786 QualType DestType, APFloat &Result) {
2787 Result = APFloat(Info.Ctx.getFloatTypeSemantics(T: DestType), 1);
2788 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2789 APFloat::opStatus St = Result.convertFromAPInt(Input: Value, IsSigned: Value.isSigned(), RM);
2790 return checkFloatingPointResultForConstantFolding(Info, E, St);
2791}
2792
2793static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,
2794 APValue &Value, const FieldDecl *FD) {
2795 assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield");
2796
2797 if (!Value.isInt()) {
2798 // Trying to store a pointer-cast-to-integer into a bitfield.
2799 // FIXME: In this case, we should provide the diagnostic for casting
2800 // a pointer to an integer.
2801 assert(Value.isLValue() && "integral value neither int nor lvalue?");
2802 Info.FFDiag(E);
2803 return false;
2804 }
2805
2806 APSInt &Int = Value.getInt();
2807 unsigned OldBitWidth = Int.getBitWidth();
2808 unsigned NewBitWidth = FD->getBitWidthValue();
2809 if (NewBitWidth < OldBitWidth)
2810 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
2811 return true;
2812}
2813
2814/// Perform the given integer operation, which is known to need at most BitWidth
2815/// bits, and check for overflow in the original type (if that type was not an
2816/// unsigned type).
2817template<typename Operation>
2818static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
2819 const APSInt &LHS, const APSInt &RHS,
2820 unsigned BitWidth, Operation Op,
2821 APSInt &Result) {
2822 if (LHS.isUnsigned()) {
2823 Result = Op(LHS, RHS);
2824 return true;
2825 }
2826
2827 APSInt Value(Op(LHS.extend(width: BitWidth), RHS.extend(width: BitWidth)), false);
2828 Result = Value.trunc(width: LHS.getBitWidth());
2829 if (Result.extend(width: BitWidth) != Value && !E->getType().isWrapType()) {
2830 if (Info.checkingForUndefinedBehavior())
2831 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
2832 DiagID: diag::warn_integer_constant_overflow)
2833 << toString(I: Result, Radix: 10, Signed: Result.isSigned(), /*formatAsCLiteral=*/false,
2834 /*UpperCase=*/true, /*InsertSeparators=*/true)
2835 << E->getType() << E->getSourceRange();
2836 return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType());
2837 }
2838 return true;
2839}
2840
2841/// Perform the given binary integer operation.
2842static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,
2843 const APSInt &LHS, BinaryOperatorKind Opcode,
2844 APSInt RHS, APSInt &Result) {
2845 bool HandleOverflowResult = true;
2846 switch (Opcode) {
2847 default:
2848 Info.FFDiag(E);
2849 return false;
2850 case BO_Mul:
2851 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() * 2,
2852 Op: std::multiplies<APSInt>(), Result);
2853 case BO_Add:
2854 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2855 Op: std::plus<APSInt>(), Result);
2856 case BO_Sub:
2857 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2858 Op: std::minus<APSInt>(), Result);
2859 case BO_And: Result = LHS & RHS; return true;
2860 case BO_Xor: Result = LHS ^ RHS; return true;
2861 case BO_Or: Result = LHS | RHS; return true;
2862 case BO_Div:
2863 case BO_Rem:
2864 if (RHS == 0) {
2865 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero)
2866 << E->getRHS()->getSourceRange();
2867 return false;
2868 }
2869 // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports
2870 // this operation and gives the two's complement result.
2871 if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() &&
2872 LHS.isMinSignedValue())
2873 HandleOverflowResult = HandleOverflow(
2874 Info, E, SrcValue: -LHS.extend(width: LHS.getBitWidth() + 1), DestType: E->getType());
2875 Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS);
2876 return HandleOverflowResult;
2877 case BO_Shl: {
2878 if (Info.getLangOpts().OpenCL)
2879 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2880 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2881 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2882 RHS.isUnsigned());
2883 else if (RHS.isSigned() && RHS.isNegative()) {
2884 // During constant-folding, a negative shift is an opposite shift. Such
2885 // a shift is not a constant expression.
2886 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2887 if (!Info.noteUndefinedBehavior())
2888 return false;
2889 RHS = -RHS;
2890 goto shift_right;
2891 }
2892 shift_left:
2893 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
2894 // the shifted type.
2895 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2896 if (SA != RHS) {
2897 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2898 << RHS << E->getType() << LHS.getBitWidth();
2899 if (!Info.noteUndefinedBehavior())
2900 return false;
2901 } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) {
2902 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
2903 // operand, and must not overflow the corresponding unsigned type.
2904 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
2905 // E1 x 2^E2 module 2^N.
2906 if (LHS.isNegative()) {
2907 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_of_negative) << LHS;
2908 if (!Info.noteUndefinedBehavior())
2909 return false;
2910 } else if (LHS.countl_zero() < SA) {
2911 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_discards);
2912 if (!Info.noteUndefinedBehavior())
2913 return false;
2914 }
2915 }
2916 Result = LHS << SA;
2917 return true;
2918 }
2919 case BO_Shr: {
2920 if (Info.getLangOpts().OpenCL)
2921 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2922 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2923 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2924 RHS.isUnsigned());
2925 else if (RHS.isSigned() && RHS.isNegative()) {
2926 // During constant-folding, a negative shift is an opposite shift. Such a
2927 // shift is not a constant expression.
2928 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2929 if (!Info.noteUndefinedBehavior())
2930 return false;
2931 RHS = -RHS;
2932 goto shift_left;
2933 }
2934 shift_right:
2935 // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
2936 // shifted type.
2937 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2938 if (SA != RHS) {
2939 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2940 << RHS << E->getType() << LHS.getBitWidth();
2941 if (!Info.noteUndefinedBehavior())
2942 return false;
2943 }
2944
2945 Result = LHS >> SA;
2946 return true;
2947 }
2948
2949 case BO_LT: Result = LHS < RHS; return true;
2950 case BO_GT: Result = LHS > RHS; return true;
2951 case BO_LE: Result = LHS <= RHS; return true;
2952 case BO_GE: Result = LHS >= RHS; return true;
2953 case BO_EQ: Result = LHS == RHS; return true;
2954 case BO_NE: Result = LHS != RHS; return true;
2955 case BO_Cmp:
2956 llvm_unreachable("BO_Cmp should be handled elsewhere");
2957 }
2958}
2959
2960/// Perform the given binary floating-point operation, in-place, on LHS.
2961static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E,
2962 APFloat &LHS, BinaryOperatorKind Opcode,
2963 const APFloat &RHS) {
2964 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2965 APFloat::opStatus St;
2966 switch (Opcode) {
2967 default:
2968 Info.FFDiag(E);
2969 return false;
2970 case BO_Mul:
2971 St = LHS.multiply(RHS, RM);
2972 break;
2973 case BO_Add:
2974 St = LHS.add(RHS, RM);
2975 break;
2976 case BO_Sub:
2977 St = LHS.subtract(RHS, RM);
2978 break;
2979 case BO_Div:
2980 // [expr.mul]p4:
2981 // If the second operand of / or % is zero the behavior is undefined.
2982 if (RHS.isZero())
2983 Info.CCEDiag(E, DiagId: diag::note_expr_divide_by_zero);
2984 St = LHS.divide(RHS, RM);
2985 break;
2986 }
2987
2988 // FIXME: The standard quote below is deleted by P3899R3.
2989 // [expr.pre]p4:
2990 // If during the evaluation of an expression, the result is not
2991 // mathematically defined [...], the behavior is undefined.
2992 // FIXME: C++ rules require us to not conform to IEEE 754 here.
2993 // FIXME: The NaN check should not be applied outside of "constant contexts"
2994 // because it prevents NaN propagation and the "invalid" status is the
2995 // responsibility of checkFloatingPointResultForConstantFolding.
2996 if (LHS.isNaN()) {
2997 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << LHS.isNaN();
2998 return Info.noteUndefinedBehavior();
2999 }
3000
3001 return checkFloatingPointResultForConstantFolding(Info, E, St);
3002}
3003
3004static bool handleLogicalOpForVector(const APInt &LHSValue,
3005 BinaryOperatorKind Opcode,
3006 const APInt &RHSValue, APInt &Result) {
3007 bool LHS = (LHSValue != 0);
3008 bool RHS = (RHSValue != 0);
3009
3010 if (Opcode == BO_LAnd)
3011 Result = LHS && RHS;
3012 else
3013 Result = LHS || RHS;
3014 return true;
3015}
3016static bool handleLogicalOpForVector(const APFloat &LHSValue,
3017 BinaryOperatorKind Opcode,
3018 const APFloat &RHSValue, APInt &Result) {
3019 bool LHS = !LHSValue.isZero();
3020 bool RHS = !RHSValue.isZero();
3021
3022 if (Opcode == BO_LAnd)
3023 Result = LHS && RHS;
3024 else
3025 Result = LHS || RHS;
3026 return true;
3027}
3028
3029static bool handleLogicalOpForVector(const APValue &LHSValue,
3030 BinaryOperatorKind Opcode,
3031 const APValue &RHSValue, APInt &Result) {
3032 // The result is always an int type, however operands match the first.
3033 if (LHSValue.getKind() == APValue::Int)
3034 return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode,
3035 RHSValue: RHSValue.getInt(), Result);
3036 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3037 return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode,
3038 RHSValue: RHSValue.getFloat(), Result);
3039}
3040
3041template <typename APTy>
3042static bool
3043handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode,
3044 const APTy &RHSValue, APInt &Result) {
3045 switch (Opcode) {
3046 default:
3047 llvm_unreachable("unsupported binary operator");
3048 case BO_EQ:
3049 Result = (LHSValue == RHSValue);
3050 break;
3051 case BO_NE:
3052 Result = (LHSValue != RHSValue);
3053 break;
3054 case BO_LT:
3055 Result = (LHSValue < RHSValue);
3056 break;
3057 case BO_GT:
3058 Result = (LHSValue > RHSValue);
3059 break;
3060 case BO_LE:
3061 Result = (LHSValue <= RHSValue);
3062 break;
3063 case BO_GE:
3064 Result = (LHSValue >= RHSValue);
3065 break;
3066 }
3067
3068 // The boolean operations on these vector types use an instruction that
3069 // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1
3070 // to -1 to make sure that we produce the correct value.
3071 Result.negate();
3072
3073 return true;
3074}
3075
3076static bool handleCompareOpForVector(const APValue &LHSValue,
3077 BinaryOperatorKind Opcode,
3078 const APValue &RHSValue, APInt &Result) {
3079 // The result is always an int type, however operands match the first.
3080 if (LHSValue.getKind() == APValue::Int)
3081 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode,
3082 RHSValue: RHSValue.getInt(), Result);
3083 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3084 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode,
3085 RHSValue: RHSValue.getFloat(), Result);
3086}
3087
3088// Perform binary operations for vector types, in place on the LHS.
3089static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E,
3090 BinaryOperatorKind Opcode,
3091 APValue &LHSValue,
3092 const APValue &RHSValue) {
3093 assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI &&
3094 "Operation not supported on vector types");
3095
3096 const auto *VT = E->getType()->castAs<VectorType>();
3097 unsigned NumElements = VT->getNumElements();
3098 QualType EltTy = VT->getElementType();
3099
3100 // In the cases (typically C as I've observed) where we aren't evaluating
3101 // constexpr but are checking for cases where the LHS isn't yet evaluatable,
3102 // just give up.
3103 if (!LHSValue.isVector()) {
3104 assert(LHSValue.isLValue() &&
3105 "A vector result that isn't a vector OR uncalculated LValue");
3106 Info.FFDiag(E);
3107 return false;
3108 }
3109
3110 assert(LHSValue.getVectorLength() == NumElements &&
3111 RHSValue.getVectorLength() == NumElements && "Different vector sizes");
3112
3113 SmallVector<APValue, 4> ResultElements;
3114
3115 for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) {
3116 APValue LHSElt = LHSValue.getVectorElt(I: EltNum);
3117 APValue RHSElt = RHSValue.getVectorElt(I: EltNum);
3118
3119 if (EltTy->isIntegerType()) {
3120 APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy),
3121 EltTy->isUnsignedIntegerType()};
3122 bool Success = true;
3123
3124 if (BinaryOperator::isLogicalOp(Opc: Opcode))
3125 Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3126 else if (BinaryOperator::isComparisonOp(Opc: Opcode))
3127 Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3128 else
3129 Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode,
3130 RHS: RHSElt.getInt(), Result&: EltResult);
3131
3132 if (!Success) {
3133 Info.FFDiag(E);
3134 return false;
3135 }
3136 ResultElements.emplace_back(Args&: EltResult);
3137
3138 } else if (EltTy->isFloatingType()) {
3139 assert(LHSElt.getKind() == APValue::Float &&
3140 RHSElt.getKind() == APValue::Float &&
3141 "Mismatched LHS/RHS/Result Type");
3142 APFloat LHSFloat = LHSElt.getFloat();
3143
3144 if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode,
3145 RHS: RHSElt.getFloat())) {
3146 Info.FFDiag(E);
3147 return false;
3148 }
3149
3150 ResultElements.emplace_back(Args&: LHSFloat);
3151 }
3152 }
3153
3154 LHSValue = APValue(ResultElements.data(), ResultElements.size());
3155 return true;
3156}
3157
3158/// Cast an lvalue referring to a base subobject to a derived class, by
3159/// truncating the lvalue's path to the given length.
3160static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
3161 const RecordDecl *TruncatedType,
3162 unsigned TruncatedElements) {
3163 SubobjectDesignator &D = Result.Designator;
3164
3165 // Check we actually point to a derived class object.
3166 if (TruncatedElements == D.Entries.size())
3167 return true;
3168 assert(TruncatedElements >= D.MostDerivedPathLength &&
3169 "not casting to a derived class");
3170 if (!Result.checkSubobject(Info, E, CSK: CSK_Derived))
3171 return false;
3172
3173 // Truncate the path to the subobject, and remove any derived-to-base offsets.
3174 const RecordDecl *RD = TruncatedType;
3175 for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
3176 if (RD->isInvalidDecl()) return false;
3177 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
3178 const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]);
3179 if (isVirtualBaseClass(E: D.Entries[I]))
3180 Result.Offset -= Layout.getVBaseClassOffset(VBase: Base);
3181 else
3182 Result.Offset -= Layout.getBaseClassOffset(Base);
3183 RD = Base;
3184 }
3185 D.Entries.resize(N: TruncatedElements);
3186 return true;
3187}
3188
3189static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3190 const CXXRecordDecl *Derived,
3191 const CXXRecordDecl *Base,
3192 const ASTRecordLayout *RL = nullptr) {
3193 if (!RL) {
3194 if (Derived->isInvalidDecl()) return false;
3195 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3196 }
3197
3198 Obj.addDecl(Info, E, D: Base, /*Virtual=*/false);
3199 Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
3200 return true;
3201}
3202
3203static bool HandleLValueDirectVirtualBase(EvalInfo &Info, const Expr *E,
3204 LValue &Obj,
3205 const CXXRecordDecl *Derived,
3206 const CXXRecordDecl *Base,
3207 const ASTRecordLayout *RL = nullptr) {
3208 if (!RL) {
3209 if (Derived->isInvalidDecl())
3210 return false;
3211 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3212 }
3213
3214 Obj.addDecl(Info, E, D: Base, /*Virtual=*/true);
3215 Obj.getLValueOffset() += RL->getVBaseClassOffset(VBase: Base);
3216 return true;
3217}
3218
3219static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3220 const CXXRecordDecl *DerivedDecl,
3221 const CXXBaseSpecifier *Base) {
3222 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
3223
3224 if (!Base->isVirtual())
3225 return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl);
3226
3227 SubobjectDesignator &D = Obj.Designator;
3228 if (D.Invalid)
3229 return false;
3230
3231 // Extract most-derived object and corresponding type.
3232 // FIXME: After implementing P2280R4 it became possible to get references
3233 // here. We do MostDerivedType->getAsCXXRecordDecl() in several other
3234 // locations and if we see crashes in those locations in the future
3235 // it may make more sense to move this fix into Lvalue::set.
3236 DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl();
3237 if (!CastToDerivedClass(Info, E, Result&: Obj, TruncatedType: DerivedDecl, TruncatedElements: D.MostDerivedPathLength))
3238 return false;
3239
3240 // Find the virtual base class.
3241 if (DerivedDecl->isInvalidDecl()) return false;
3242 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: DerivedDecl);
3243 Obj.addDecl(Info, E, D: BaseDecl, /*Virtual*/ true);
3244 Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl);
3245 return true;
3246}
3247
3248static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E,
3249 QualType Type, LValue &Result) {
3250 for (CastExpr::path_const_iterator PathI = E->path_begin(),
3251 PathE = E->path_end();
3252 PathI != PathE; ++PathI) {
3253 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Type->getAsCXXRecordDecl(),
3254 Base: *PathI))
3255 return false;
3256 Type = (*PathI)->getType();
3257 }
3258 return true;
3259}
3260
3261/// Cast an lvalue referring to a derived class to a known base subobject.
3262static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result,
3263 const CXXRecordDecl *DerivedRD,
3264 const CXXRecordDecl *BaseRD) {
3265 CXXBasePaths Paths(/*FindAmbiguities=*/false,
3266 /*RecordPaths=*/true, /*DetectVirtual=*/false);
3267 if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths))
3268 llvm_unreachable("Class must be derived from the passed in base class!");
3269
3270 for (CXXBasePathElement &Elem : Paths.front())
3271 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base))
3272 return false;
3273 return true;
3274}
3275
3276/// Update LVal to refer to the given field, which must be a member of the type
3277/// currently described by LVal.
3278static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
3279 const FieldDecl *FD,
3280 const ASTRecordLayout *RL = nullptr) {
3281 if (!RL) {
3282 const RecordDecl *RD = FD->getParent();
3283 if (RD->isInvalidDecl())
3284 return false;
3285 // There are some cases where the base is not yet complete but we haven't
3286 // disagnosed (such as in a template instantation of an attribute that
3287 // references the expression, ala enable_if). These aren't necessarily
3288 // constant expressions so we return 'false', but they might be, so we don't
3289 // diagnose.
3290 if (!RD->isCompleteDefinition())
3291 return false;
3292 RL = &Info.Ctx.getASTRecordLayout(D: RD);
3293 }
3294
3295 unsigned I = FD->getFieldIndex();
3296 LVal.addDecl(Info, E, D: FD);
3297 LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I)));
3298 return true;
3299}
3300
3301/// Update LVal to refer to the given indirect field.
3302static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
3303 LValue &LVal,
3304 const IndirectFieldDecl *IFD) {
3305 for (const auto *C : IFD->chain())
3306 if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C)))
3307 return false;
3308 return true;
3309}
3310
3311enum class SizeOfType {
3312 SizeOf,
3313 DataSizeOf,
3314};
3315
3316/// Get the size of the given type in char units.
3317static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,
3318 CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) {
3319 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
3320 // extension.
3321 if (Type->isVoidType() || Type->isFunctionType()) {
3322 Size = CharUnits::One();
3323 return true;
3324 }
3325
3326 if (Type->isDependentType()) {
3327 Info.FFDiag(Loc);
3328 return false;
3329 }
3330
3331 if (!Type->isConstantSizeType()) {
3332 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
3333 // FIXME: Better diagnostic.
3334 Info.FFDiag(Loc);
3335 return false;
3336 }
3337
3338 if (SOT == SizeOfType::SizeOf)
3339 Size = Info.Ctx.getTypeSizeInChars(T: Type);
3340 else
3341 Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width;
3342 return true;
3343}
3344
3345/// Update a pointer value to model pointer arithmetic.
3346/// \param Info - Information about the ongoing evaluation.
3347/// \param E - The expression being evaluated, for diagnostic purposes.
3348/// \param LVal - The pointer value to be updated.
3349/// \param EltTy - The pointee type represented by LVal.
3350/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
3351static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3352 LValue &LVal, QualType EltTy,
3353 APSInt Adjustment) {
3354 CharUnits SizeOfPointee;
3355 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee))
3356 return false;
3357
3358 LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee);
3359 return true;
3360}
3361
3362static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3363 LValue &LVal, QualType EltTy,
3364 int64_t Adjustment) {
3365 return HandleLValueArrayAdjustment(Info, E, LVal, EltTy,
3366 Adjustment: APSInt::get(X: Adjustment));
3367}
3368
3369/// Update an lvalue to refer to a component of a complex number.
3370/// \param Info - Information about the ongoing evaluation.
3371/// \param LVal - The lvalue to be updated.
3372/// \param EltTy - The complex number's component type.
3373/// \param Imag - False for the real component, true for the imaginary.
3374static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
3375 LValue &LVal, QualType EltTy,
3376 bool Imag) {
3377 if (Imag) {
3378 CharUnits SizeOfComponent;
3379 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent))
3380 return false;
3381 LVal.Offset += SizeOfComponent;
3382 }
3383 LVal.addComplex(Info, E, EltTy, Imag);
3384 return true;
3385}
3386
3387static bool HandleLValueVectorElement(EvalInfo &Info, const Expr *E,
3388 LValue &LVal, QualType EltTy,
3389 uint64_t Size, uint64_t Idx) {
3390 if (Idx) {
3391 CharUnits SizeOfElement;
3392 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfElement))
3393 return false;
3394 LVal.Offset += SizeOfElement * Idx;
3395 }
3396 LVal.addVectorElement(Info, E, EltTy, Size, Idx);
3397 return true;
3398}
3399
3400/// Try to evaluate the initializer for a variable declaration.
3401///
3402/// \param Info Information about the ongoing evaluation.
3403/// \param E An expression to be used when printing diagnostics.
3404/// \param VD The variable whose initializer should be obtained.
3405/// \param Version The version of the variable within the frame.
3406/// \param Frame The frame in which the variable was created. Must be null
3407/// if this variable is not local to the evaluation.
3408/// \param Result Filled in with a pointer to the value of the variable.
3409static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
3410 const VarDecl *VD, CallStackFrame *Frame,
3411 unsigned Version, APValue *&Result) {
3412 // C++23 [expr.const]p8 If we have a reference type allow unknown references
3413 // and pointers.
3414 bool AllowConstexprUnknown =
3415 Info.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType();
3416
3417 APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version);
3418
3419 auto CheckUninitReference = [&](bool IsLocalVariable) {
3420 if (!Result || (!Result->hasValue() && VD->getType()->isReferenceType())) {
3421 // C++23 [expr.const]p8
3422 // ... For such an object that is not usable in constant expressions, the
3423 // dynamic type of the object is constexpr-unknown. For such a reference
3424 // that is not usable in constant expressions, the reference is treated
3425 // as binding to an unspecified object of the referenced type whose
3426 // lifetime and that of all subobjects includes the entire constant
3427 // evaluation and whose dynamic type is constexpr-unknown.
3428 //
3429 // Variables that are part of the current evaluation are not
3430 // constexpr-unknown.
3431 if (!AllowConstexprUnknown || IsLocalVariable) {
3432 if (!Info.checkingPotentialConstantExpression())
3433 Info.FFDiag(E, DiagId: diag::note_constexpr_use_uninit_reference);
3434 return false;
3435 }
3436 Result = nullptr;
3437 }
3438 return true;
3439 };
3440
3441 // If this is a local variable, dig out its value.
3442 if (Frame) {
3443 Result = Frame->getTemporary(Key: VD, Version);
3444 if (Result)
3445 return CheckUninitReference(/*IsLocalVariable=*/true);
3446
3447 if (!isa<ParmVarDecl>(Val: VD)) {
3448 // Assume variables referenced within a lambda's call operator that were
3449 // not declared within the call operator are captures and during checking
3450 // of a potential constant expression, assume they are unknown constant
3451 // expressions.
3452 assert(isLambdaCallOperator(Frame->Callee) &&
3453 (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) &&
3454 "missing value for local variable");
3455 if (Info.checkingPotentialConstantExpression())
3456 return false;
3457
3458 llvm_unreachable(
3459 "A variable in a frame should either be a local or a parameter");
3460 }
3461 }
3462
3463 // If we're currently evaluating the initializer of this declaration, use that
3464 // in-flight value.
3465 if (Info.EvaluatingDecl == Base) {
3466 Result = Info.EvaluatingDeclValue;
3467 return CheckUninitReference(/*IsLocalVariable=*/false);
3468 }
3469
3470 // P2280R4 struck the restriction that variable of reference type lifetime
3471 // should begin within the evaluation of E
3472 // Used to be C++20 [expr.const]p5.12.2:
3473 // ... its lifetime began within the evaluation of E;
3474 if (isa<ParmVarDecl>(Val: VD)) {
3475 if (AllowConstexprUnknown) {
3476 Result = nullptr;
3477 return true;
3478 }
3479
3480 // Assume parameters of a potential constant expression are usable in
3481 // constant expressions.
3482 if (!Info.checkingPotentialConstantExpression() ||
3483 !Info.CurrentCall->Callee ||
3484 !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) {
3485 if (Info.getLangOpts().CPlusPlus11) {
3486 Info.FFDiag(E, DiagId: diag::note_constexpr_function_param_value_unknown)
3487 << VD;
3488 NoteLValueLocation(Info, Base);
3489 } else {
3490 Info.FFDiag(E);
3491 }
3492 }
3493 return false;
3494 }
3495
3496 if (E->isValueDependent())
3497 return false;
3498
3499 // Dig out the initializer, and use the declaration which it's attached to.
3500 // FIXME: We should eventually check whether the variable has a reachable
3501 // initializing declaration.
3502 const Expr *Init = VD->getAnyInitializer(D&: VD);
3503 // P2280R4 struck the restriction that variable of reference type should have
3504 // a preceding initialization.
3505 // Used to be C++20 [expr.const]p5.12:
3506 // ... reference has a preceding initialization and either ...
3507 if (!Init && !AllowConstexprUnknown) {
3508 // Don't diagnose during potential constant expression checking; an
3509 // initializer might be added later.
3510 if (!Info.checkingPotentialConstantExpression()) {
3511 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_unknown, ExtraNotes: 1)
3512 << VD;
3513 NoteLValueLocation(Info, Base);
3514 }
3515 return false;
3516 }
3517
3518 // P2280R4 struck the initialization requirement for variables of reference
3519 // type so we can no longer assume we have an Init.
3520 // Used to be C++20 [expr.const]p5.12:
3521 // ... reference has a preceding initialization and either ...
3522 if (Init && Init->isValueDependent()) {
3523 // The DeclRefExpr is not value-dependent, but the variable it refers to
3524 // has a value-dependent initializer. This should only happen in
3525 // constant-folding cases, where the variable is not actually of a suitable
3526 // type for use in a constant expression (otherwise the DeclRefExpr would
3527 // have been value-dependent too), so diagnose that.
3528 assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx));
3529 if (!Info.checkingPotentialConstantExpression()) {
3530 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
3531 ? diag::note_constexpr_ltor_non_constexpr
3532 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
3533 << VD << VD->getType();
3534 NoteLValueLocation(Info, Base);
3535 }
3536 return false;
3537 }
3538
3539 // Check that we can fold the initializer. In C++, we will have already done
3540 // this in the cases where it matters for conformance.
3541 // P2280R4 struck the initialization requirement for variables of reference
3542 // type so we can no longer assume we have an Init.
3543 // Used to be C++20 [expr.const]p5.12:
3544 // ... reference has a preceding initialization and either ...
3545 if (Init && !VD->evaluateValue() && !AllowConstexprUnknown) {
3546 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3547 NoteLValueLocation(Info, Base);
3548 return false;
3549 }
3550
3551 // Check that the variable is actually usable in constant expressions. For a
3552 // const integral variable or a reference, we might have a non-constant
3553 // initializer that we can nonetheless evaluate the initializer for. Such
3554 // variables are not usable in constant expressions. In C++98, the
3555 // initializer also syntactically needs to be an ICE.
3556 //
3557 // FIXME: We don't diagnose cases that aren't potentially usable in constant
3558 // expressions here; doing so would regress diagnostics for things like
3559 // reading from a volatile constexpr variable.
3560 if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() &&
3561 VD->mightBeUsableInConstantExpressions(C: Info.Ctx) &&
3562 !AllowConstexprUnknown) ||
3563 ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
3564 !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) {
3565 if (Init) {
3566 Info.CCEDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3567 NoteLValueLocation(Info, Base);
3568 } else {
3569 Info.CCEDiag(E);
3570 }
3571 }
3572
3573 // Never use the initializer of a weak variable, not even for constant
3574 // folding. We can't be sure that this is the definition that will be used.
3575 if (VD->isWeak()) {
3576 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_weak) << VD;
3577 NoteLValueLocation(Info, Base);
3578 return false;
3579 }
3580
3581 Result = const_cast<APValue *>(VD->getEvaluatedValue());
3582
3583 if (!Result && !AllowConstexprUnknown)
3584 return false;
3585
3586 return CheckUninitReference(/*IsLocalVariable=*/false);
3587}
3588
3589/// Get the base index of the given base class within an APValue representing
3590/// the given derived class.
3591static unsigned getBaseIndex(const CXXRecordDecl *Derived,
3592 const CXXRecordDecl *Base) {
3593 Base = Base->getCanonicalDecl();
3594 unsigned Index = 0;
3595 for (const CXXBaseSpecifier &B : Derived->bases()) {
3596 if (B.isVirtual())
3597 continue;
3598 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3599 return Index;
3600 ++Index;
3601 }
3602
3603 for (const CXXBaseSpecifier &B : Derived->vbases()) {
3604 if (B.getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3605 return Index;
3606 ++Index;
3607 }
3608
3609 llvm_unreachable("base class missing from derived class's bases list");
3610}
3611
3612/// Extract the value of a character from a string literal.
3613static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
3614 uint64_t Index) {
3615 assert(!isa<SourceLocExpr>(Lit) &&
3616 "SourceLocExpr should have already been converted to a StringLiteral");
3617
3618 // FIXME: Support MakeStringConstant
3619 if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) {
3620 std::string Str;
3621 Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str);
3622 assert(Index <= Str.size() && "Index too large");
3623 return APSInt::getUnsigned(X: Str.c_str()[Index]);
3624 }
3625
3626 if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit))
3627 Lit = PE->getFunctionName();
3628 const StringLiteral *S = cast<StringLiteral>(Val: Lit);
3629 const ConstantArrayType *CAT =
3630 Info.Ctx.getAsConstantArrayType(T: S->getType());
3631 assert(CAT && "string literal isn't an array");
3632 QualType CharType = CAT->getElementType();
3633 assert(CharType->isIntegerType() && "unexpected character type");
3634 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3635 CharType->isUnsignedIntegerType());
3636 if (Index < S->getLength())
3637 Value = S->getCodeUnit(I: Index);
3638 return Value;
3639}
3640
3641// Expand a string literal into an array of characters.
3642//
3643// FIXME: This is inefficient; we should probably introduce something similar
3644// to the LLVM ConstantDataArray to make this cheaper.
3645static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S,
3646 APValue &Result,
3647 QualType AllocType = QualType()) {
3648 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
3649 T: AllocType.isNull() ? S->getType() : AllocType);
3650 assert(CAT && "string literal isn't an array");
3651 QualType CharType = CAT->getElementType();
3652 assert(CharType->isIntegerType() && "unexpected character type");
3653
3654 unsigned Elts = CAT->getZExtSize();
3655 Result = APValue(APValue::UninitArray(),
3656 std::min(a: S->getLength(), b: Elts), Elts);
3657 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3658 CharType->isUnsignedIntegerType());
3659 if (Result.hasArrayFiller())
3660 Result.getArrayFiller() = APValue(Value);
3661 for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) {
3662 Value = S->getCodeUnit(I);
3663 Result.getArrayInitializedElt(I) = APValue(Value);
3664 }
3665}
3666
3667// Expand an array so that it has more than Index filled elements.
3668static void expandArray(APValue &Array, unsigned Index) {
3669 unsigned Size = Array.getArraySize();
3670 assert(Index < Size);
3671
3672 // Always at least double the number of elements for which we store a value.
3673 unsigned OldElts = Array.getArrayInitializedElts();
3674 unsigned NewElts = std::max(a: Index+1, b: OldElts * 2);
3675 NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u));
3676
3677 // Copy the data across.
3678 APValue NewValue(APValue::UninitArray(), NewElts, Size);
3679 for (unsigned I = 0; I != OldElts; ++I)
3680 NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I));
3681 for (unsigned I = OldElts; I != NewElts; ++I)
3682 NewValue.getArrayInitializedElt(I) = Array.getArrayFiller();
3683 if (NewValue.hasArrayFiller())
3684 NewValue.getArrayFiller() = Array.getArrayFiller();
3685 Array.swap(RHS&: NewValue);
3686}
3687
3688// Expand an indeterminate vector to materialize all elements.
3689static void expandVector(APValue &Vec, unsigned NumElements) {
3690 assert(Vec.isIndeterminate());
3691 SmallVector<APValue, 4> Elts(NumElements, APValue::IndeterminateValue());
3692 Vec = APValue(Elts.data(), Elts.size());
3693}
3694
3695/// Determine whether a type would actually be read by an lvalue-to-rvalue
3696/// conversion. If it's of class type, we may assume that the copy operation
3697/// is trivial. Note that this is never true for a union type with fields
3698/// (because the copy always "reads" the active member) and always true for
3699/// a non-class type.
3700static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD);
3701static bool isReadByLvalueToRvalueConversion(QualType T) {
3702 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3703 return !RD || isReadByLvalueToRvalueConversion(RD);
3704}
3705static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) {
3706 // FIXME: A trivial copy of a union copies the object representation, even if
3707 // the union is empty.
3708 if (RD->isUnion())
3709 return !RD->field_empty();
3710 if (RD->isEmpty())
3711 return false;
3712
3713 for (auto *Field : RD->fields())
3714 if (!Field->isUnnamedBitField() &&
3715 isReadByLvalueToRvalueConversion(T: Field->getType()))
3716 return true;
3717
3718 for (auto &BaseSpec : RD->bases())
3719 if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType()))
3720 return true;
3721
3722 return false;
3723}
3724
3725/// Diagnose an attempt to read from any unreadable field within the specified
3726/// type, which might be a class type.
3727static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK,
3728 QualType T) {
3729 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3730 if (!RD)
3731 return false;
3732
3733 if (!RD->hasMutableFields())
3734 return false;
3735
3736 for (auto *Field : RD->fields()) {
3737 // If we're actually going to read this field in some way, then it can't
3738 // be mutable. If we're in a union, then assigning to a mutable field
3739 // (even an empty one) can change the active member, so that's not OK.
3740 // FIXME: Add core issue number for the union case.
3741 if (Field->isMutable() &&
3742 (RD->isUnion() || isReadByLvalueToRvalueConversion(T: Field->getType()))) {
3743 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1) << AK << Field;
3744 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
3745 return true;
3746 }
3747
3748 if (diagnoseMutableFields(Info, E, AK, T: Field->getType()))
3749 return true;
3750 }
3751
3752 for (auto &BaseSpec : RD->bases())
3753 if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType()))
3754 return true;
3755
3756 // All mutable fields were empty, and thus not actually read.
3757 return false;
3758}
3759
3760static bool lifetimeStartedInEvaluation(EvalInfo &Info,
3761 APValue::LValueBase Base,
3762 bool MutableSubobject = false) {
3763 // A temporary or transient heap allocation we created.
3764 if (Base.getCallIndex() || Base.is<DynamicAllocLValue>())
3765 return true;
3766
3767 switch (Info.IsEvaluatingDecl) {
3768 case EvalInfo::EvaluatingDeclKind::None:
3769 return false;
3770
3771 case EvalInfo::EvaluatingDeclKind::Ctor:
3772 // The variable whose initializer we're evaluating.
3773 if (Info.EvaluatingDecl == Base)
3774 return true;
3775
3776 // A temporary lifetime-extended by the variable whose initializer we're
3777 // evaluating.
3778 if (auto *BaseE = Base.dyn_cast<const Expr *>())
3779 if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE))
3780 return Info.EvaluatingDecl == BaseMTE->getExtendingDecl();
3781 return false;
3782
3783 case EvalInfo::EvaluatingDeclKind::Dtor:
3784 // C++2a [expr.const]p6:
3785 // [during constant destruction] the lifetime of a and its non-mutable
3786 // subobjects (but not its mutable subobjects) [are] considered to start
3787 // within e.
3788 if (MutableSubobject || Base != Info.EvaluatingDecl)
3789 return false;
3790 // FIXME: We can meaningfully extend this to cover non-const objects, but
3791 // we will need special handling: we should be able to access only
3792 // subobjects of such objects that are themselves declared const.
3793 QualType T = getType(B: Base);
3794 return T.isConstQualified() || T->isReferenceType();
3795 }
3796
3797 llvm_unreachable("unknown evaluating decl kind");
3798}
3799
3800static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT,
3801 SourceLocation CallLoc = {}) {
3802 return Info.CheckArraySize(
3803 Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc,
3804 BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getZExtSize(),
3805 /*Diag=*/true);
3806}
3807
3808static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,
3809 QualType SourceTy, QualType DestTy,
3810 APValue const &Original, APValue &Result) {
3811 // boolean must be checked before integer
3812 // since IsIntegerType() is true for bool
3813 if (SourceTy->isBooleanType()) {
3814 if (DestTy->isBooleanType()) {
3815 Result = Original;
3816 return true;
3817 }
3818 if (DestTy->isIntegerType() || DestTy->isRealFloatingType()) {
3819 bool BoolResult;
3820 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3821 return false;
3822 uint64_t IntResult = BoolResult;
3823 QualType IntType = DestTy->isIntegerType()
3824 ? DestTy
3825 : Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false);
3826 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: IntType));
3827 }
3828 if (DestTy->isRealFloatingType()) {
3829 APValue Result2 = APValue(APFloat(0.0));
3830 if (!HandleIntToFloatCast(Info, E, FPO,
3831 SrcType: Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false),
3832 Value: Result.getInt(), DestType: DestTy, Result&: Result2.getFloat()))
3833 return false;
3834 Result = std::move(Result2);
3835 }
3836 return true;
3837 }
3838 if (SourceTy->isIntegerType()) {
3839 if (DestTy->isRealFloatingType()) {
3840 Result = APValue(APFloat(0.0));
3841 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
3842 DestType: DestTy, Result&: Result.getFloat());
3843 }
3844 if (DestTy->isBooleanType()) {
3845 bool BoolResult;
3846 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3847 return false;
3848 uint64_t IntResult = BoolResult;
3849 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3850 return true;
3851 }
3852 if (DestTy->isIntegerType()) {
3853 Result = APValue(
3854 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
3855 return true;
3856 }
3857 } else if (SourceTy->isRealFloatingType()) {
3858 if (DestTy->isRealFloatingType()) {
3859 Result = Original;
3860 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
3861 Result&: Result.getFloat());
3862 }
3863 if (DestTy->isBooleanType()) {
3864 bool BoolResult;
3865 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3866 return false;
3867 uint64_t IntResult = BoolResult;
3868 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3869 return true;
3870 }
3871 if (DestTy->isIntegerType()) {
3872 Result = APValue(APSInt());
3873 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
3874 DestType: DestTy, Result&: Result.getInt());
3875 }
3876 }
3877
3878 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3879 return false;
3880}
3881
3882// do the heavy lifting for casting to aggregate types
3883// because we have to deal with bitfields specially
3884static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
3885 const Expr *E, APValue &Result,
3886 QualType ResultType,
3887 SmallVectorImpl<APValue> &Elements,
3888 SmallVectorImpl<QualType> &ElTypes) {
3889
3890 SmallVector<std::tuple<APValue *, QualType, unsigned>> WorkList = {
3891 {&Result, ResultType, 0}};
3892
3893 unsigned ElI = 0;
3894 while (!WorkList.empty() && ElI < Elements.size()) {
3895 auto [Res, Type, BitWidth] = WorkList.pop_back_val();
3896
3897 if (Type->isRealFloatingType()) {
3898 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3899 Result&: *Res))
3900 return false;
3901 ElI++;
3902 continue;
3903 }
3904 if (Type->isIntegerType()) {
3905 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3906 Result&: *Res))
3907 return false;
3908 if (BitWidth > 0) {
3909 if (!Res->isInt())
3910 return false;
3911 APSInt &Int = Res->getInt();
3912 unsigned OldBitWidth = Int.getBitWidth();
3913 unsigned NewBitWidth = BitWidth;
3914 if (NewBitWidth < OldBitWidth)
3915 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
3916 }
3917 ElI++;
3918 continue;
3919 }
3920 if (Type->isVectorType()) {
3921 QualType ElTy = Type->castAs<VectorType>()->getElementType();
3922 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
3923 SmallVector<APValue> Vals(NumEl);
3924 for (unsigned I = 0; I < NumEl; ++I) {
3925 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: ElTy, Original: Elements[ElI],
3926 Result&: Vals[I]))
3927 return false;
3928 ElI++;
3929 }
3930 *Res = APValue(Vals.data(), NumEl);
3931 continue;
3932 }
3933 if (Type->isConstantArrayType()) {
3934 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
3935 ->getElementType();
3936 uint64_t Size =
3937 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
3938 *Res = APValue(APValue::UninitArray(), Size, Size);
3939 for (int64_t I = Size - 1; I > -1; --I)
3940 WorkList.emplace_back(Args: &Res->getArrayInitializedElt(I), Args&: ElTy, Args: 0u);
3941 continue;
3942 }
3943 if (Type->isRecordType()) {
3944 const RecordDecl *RD = Type->getAsRecordDecl();
3945
3946 unsigned NumBases = 0;
3947 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
3948 NumBases = CXXRD->getNumBases();
3949
3950 *Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());
3951
3952 SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;
3953 // we need to traverse backwards
3954 // Visit the base classes.
3955 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
3956 if (CXXRD->getNumBases() > 0) {
3957 assert(CXXRD->getNumBases() == 1);
3958 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
3959 ReverseList.emplace_back(Args: &Res->getStructBase(i: 0), Args: BS.getType(), Args: 0u);
3960 }
3961 }
3962
3963 // Visit the fields.
3964 for (FieldDecl *FD : RD->fields()) {
3965 unsigned FDBW = 0;
3966 if (FD->isUnnamedBitField())
3967 continue;
3968 if (FD->isBitField()) {
3969 FDBW = FD->getBitWidthValue();
3970 }
3971
3972 ReverseList.emplace_back(Args: &Res->getStructField(i: FD->getFieldIndex()),
3973 Args: FD->getType(), Args&: FDBW);
3974 }
3975
3976 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
3977 llvm::append_range(C&: WorkList, R&: ReverseList);
3978 continue;
3979 }
3980 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3981 return false;
3982 }
3983 return true;
3984}
3985
3986static bool handleElementwiseCast(EvalInfo &Info, const Expr *E,
3987 const FPOptions FPO,
3988 SmallVectorImpl<APValue> &Elements,
3989 SmallVectorImpl<QualType> &SrcTypes,
3990 SmallVectorImpl<QualType> &DestTypes,
3991 SmallVectorImpl<APValue> &Results) {
3992
3993 assert((Elements.size() == SrcTypes.size()) &&
3994 (Elements.size() == DestTypes.size()));
3995
3996 for (unsigned I = 0, ESz = Elements.size(); I < ESz; ++I) {
3997 APValue Original = Elements[I];
3998 QualType SourceTy = SrcTypes[I];
3999 QualType DestTy = DestTypes[I];
4000
4001 if (!handleScalarCast(Info, FPO, E, SourceTy, DestTy, Original, Result&: Results[I]))
4002 return false;
4003 }
4004 return true;
4005}
4006
4007static unsigned elementwiseSize(EvalInfo &Info, QualType BaseTy) {
4008
4009 SmallVector<QualType> WorkList = {BaseTy};
4010
4011 unsigned Size = 0;
4012 while (!WorkList.empty()) {
4013 QualType Type = WorkList.pop_back_val();
4014 if (Type->isRealFloatingType() || Type->isIntegerType() ||
4015 Type->isBooleanType()) {
4016 ++Size;
4017 continue;
4018 }
4019 if (Type->isVectorType()) {
4020 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
4021 Size += NumEl;
4022 continue;
4023 }
4024 if (Type->isConstantMatrixType()) {
4025 unsigned NumEl =
4026 Type->castAs<ConstantMatrixType>()->getNumElementsFlattened();
4027 Size += NumEl;
4028 continue;
4029 }
4030 if (Type->isConstantArrayType()) {
4031 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4032 ->getElementType();
4033 uint64_t ArrSize =
4034 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
4035 for (uint64_t I = 0; I < ArrSize; ++I) {
4036 WorkList.push_back(Elt: ElTy);
4037 }
4038 continue;
4039 }
4040 if (Type->isRecordType()) {
4041 const RecordDecl *RD = Type->getAsRecordDecl();
4042
4043 // Visit the base classes.
4044 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4045 if (CXXRD->getNumBases() > 0) {
4046 assert(CXXRD->getNumBases() == 1);
4047 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4048 WorkList.push_back(Elt: BS.getType());
4049 }
4050 }
4051
4052 // visit the fields.
4053 for (FieldDecl *FD : RD->fields()) {
4054 if (FD->isUnnamedBitField())
4055 continue;
4056 WorkList.push_back(Elt: FD->getType());
4057 }
4058 continue;
4059 }
4060 }
4061 return Size;
4062}
4063
4064static bool hlslAggSplatHelper(EvalInfo &Info, const Expr *E, APValue &SrcVal,
4065 QualType &SrcTy) {
4066 SrcTy = E->getType();
4067
4068 if (!Evaluate(Result&: SrcVal, Info, E))
4069 return false;
4070
4071 assert((SrcVal.isFloat() || SrcVal.isInt() ||
4072 (SrcVal.isVector() && SrcVal.getVectorLength() == 1)) &&
4073 "Not a valid HLSLAggregateSplatCast.");
4074
4075 if (SrcVal.isVector()) {
4076 assert(SrcTy->isVectorType() && "Type mismatch.");
4077 SrcTy = SrcTy->castAs<VectorType>()->getElementType();
4078 SrcVal = SrcVal.getVectorElt(I: 0);
4079 }
4080 if (SrcVal.isMatrix()) {
4081 assert(SrcTy->isConstantMatrixType() && "Type mismatch.");
4082 SrcTy = SrcTy->castAs<ConstantMatrixType>()->getElementType();
4083 SrcVal = SrcVal.getMatrixElt(Row: 0, Col: 0);
4084 }
4085 return true;
4086}
4087
4088static bool flattenAPValue(EvalInfo &Info, const Expr *E, APValue Value,
4089 QualType BaseTy, SmallVectorImpl<APValue> &Elements,
4090 SmallVectorImpl<QualType> &Types, unsigned Size) {
4091
4092 SmallVector<std::pair<APValue, QualType>> WorkList = {{Value, BaseTy}};
4093 unsigned Populated = 0;
4094 while (!WorkList.empty() && Populated < Size) {
4095 auto [Work, Type] = WorkList.pop_back_val();
4096
4097 if (Work.isFloat() || Work.isInt()) {
4098 Elements.push_back(Elt: Work);
4099 Types.push_back(Elt: Type);
4100 Populated++;
4101 continue;
4102 }
4103 if (Work.isVector()) {
4104 assert(Type->isVectorType() && "Type mismatch.");
4105 QualType ElTy = Type->castAs<VectorType>()->getElementType();
4106 for (unsigned I = 0; I < Work.getVectorLength() && Populated < Size;
4107 I++) {
4108 Elements.push_back(Elt: Work.getVectorElt(I));
4109 Types.push_back(Elt: ElTy);
4110 Populated++;
4111 }
4112 continue;
4113 }
4114 if (Work.isMatrix()) {
4115 assert(Type->isConstantMatrixType() && "Type mismatch.");
4116 const auto *MT = Type->castAs<ConstantMatrixType>();
4117 QualType ElTy = MT->getElementType();
4118 // Matrix elements are flattened in row-major order.
4119 for (unsigned Row = 0; Row < Work.getMatrixNumRows() && Populated < Size;
4120 Row++) {
4121 for (unsigned Col = 0;
4122 Col < Work.getMatrixNumColumns() && Populated < Size; Col++) {
4123 Elements.push_back(Elt: Work.getMatrixElt(Row, Col));
4124 Types.push_back(Elt: ElTy);
4125 Populated++;
4126 }
4127 }
4128 continue;
4129 }
4130 if (Work.isArray()) {
4131 assert(Type->isConstantArrayType() && "Type mismatch.");
4132 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4133 ->getElementType();
4134 for (int64_t I = Work.getArraySize() - 1; I > -1; --I) {
4135 WorkList.emplace_back(Args&: Work.getArrayInitializedElt(I), Args&: ElTy);
4136 }
4137 continue;
4138 }
4139
4140 if (Work.isStruct()) {
4141 assert(Type->isRecordType() && "Type mismatch.");
4142
4143 const RecordDecl *RD = Type->getAsRecordDecl();
4144
4145 SmallVector<std::pair<APValue, QualType>> ReverseList;
4146 // Visit the fields.
4147 for (FieldDecl *FD : RD->fields()) {
4148 if (FD->isUnnamedBitField())
4149 continue;
4150 ReverseList.emplace_back(Args&: Work.getStructField(i: FD->getFieldIndex()),
4151 Args: FD->getType());
4152 }
4153
4154 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
4155 llvm::append_range(C&: WorkList, R&: ReverseList);
4156
4157 // Visit the base classes.
4158 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4159 if (CXXRD->getNumBases() > 0) {
4160 assert(CXXRD->getNumBases() == 1);
4161 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4162 const APValue &Base = Work.getStructBase(i: 0);
4163
4164 // Can happen in error cases.
4165 if (!Base.isStruct())
4166 return false;
4167
4168 WorkList.emplace_back(Args: Base, Args: BS.getType());
4169 }
4170 }
4171 continue;
4172 }
4173 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4174 return false;
4175 }
4176 return true;
4177}
4178
4179namespace {
4180/// A handle to a complete object (an object that is not a subobject of
4181/// another object).
4182struct CompleteObject {
4183 /// The identity of the object.
4184 APValue::LValueBase Base;
4185 /// The value of the complete object.
4186 APValue *Value;
4187 /// The type of the complete object.
4188 QualType Type;
4189
4190 CompleteObject() : Value(nullptr) {}
4191 CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type)
4192 : Base(Base), Value(Value), Type(Type) {}
4193
4194 bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const {
4195 // If this isn't a "real" access (eg, if it's just accessing the type
4196 // info), allow it. We assume the type doesn't change dynamically for
4197 // subobjects of constexpr objects (even though we'd hit UB here if it
4198 // did). FIXME: Is this right?
4199 if (!isAnyAccess(AK))
4200 return true;
4201
4202 // In C++14 onwards, it is permitted to read a mutable member whose
4203 // lifetime began within the evaluation.
4204 // FIXME: Should we also allow this in C++11?
4205 if (!Info.getLangOpts().CPlusPlus14 &&
4206 AK != AccessKinds::AK_IsWithinLifetime)
4207 return false;
4208 return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true);
4209 }
4210
4211 explicit operator bool() const { return !Type.isNull(); }
4212};
4213} // end anonymous namespace
4214
4215static QualType getSubobjectType(QualType ObjType, QualType SubobjType,
4216 bool IsMutable = false) {
4217 // C++ [basic.type.qualifier]p1:
4218 // - A const object is an object of type const T or a non-mutable subobject
4219 // of a const object.
4220 if (ObjType.isConstQualified() && !IsMutable)
4221 SubobjType.addConst();
4222 // - A volatile object is an object of type const T or a subobject of a
4223 // volatile object.
4224 if (ObjType.isVolatileQualified())
4225 SubobjType.addVolatile();
4226 return SubobjType;
4227}
4228
4229/// Find the designated sub-object of an rvalue.
4230template <typename SubobjectHandler>
4231static typename SubobjectHandler::result_type
4232findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
4233 const SubobjectDesignator &Sub, SubobjectHandler &handler) {
4234 if (Sub.Invalid)
4235 // A diagnostic will have already been produced.
4236 return handler.failed();
4237 if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) {
4238 if (Info.getLangOpts().CPlusPlus11)
4239 Info.FFDiag(E, DiagId: Sub.isOnePastTheEnd()
4240 ? diag::note_constexpr_access_past_end
4241 : diag::note_constexpr_access_unsized_array)
4242 << handler.AccessKind;
4243 else
4244 Info.FFDiag(E);
4245 return handler.failed();
4246 }
4247
4248 APValue *O = Obj.Value;
4249 QualType ObjType = Obj.Type;
4250 const FieldDecl *LastField = nullptr;
4251 const FieldDecl *VolatileField = nullptr;
4252
4253 // Walk the designator's path to find the subobject.
4254 for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) {
4255 // Reading an indeterminate value is undefined, but assigning over one is OK.
4256 if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) ||
4257 (O->isIndeterminate() &&
4258 !isValidIndeterminateAccess(handler.AccessKind))) {
4259 // Object has ended lifetime.
4260 // If I is non-zero, some subobject (member or array element) of a
4261 // complete object has ended its lifetime, so this is valid for
4262 // IsWithinLifetime, resulting in false.
4263 if (I != 0 && handler.AccessKind == AK_IsWithinLifetime)
4264 return false;
4265 if (!Info.checkingPotentialConstantExpression()) {
4266 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
4267 << handler.AccessKind << O->isIndeterminate()
4268 << E->getSourceRange();
4269 NoteLValueLocation(Info, Base: Obj.Base);
4270 }
4271 return handler.failed();
4272 }
4273
4274 // C++ [class.ctor]p5, C++ [class.dtor]p5:
4275 // const and volatile semantics are not applied on an object under
4276 // {con,de}struction.
4277 if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) &&
4278 ObjType->isRecordType() &&
4279 Info.isEvaluatingCtorDtor(
4280 Base: Obj.Base, Path: ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) !=
4281 ConstructionPhase::None) {
4282 ObjType = Info.Ctx.getCanonicalType(T: ObjType);
4283 ObjType.removeLocalConst();
4284 ObjType.removeLocalVolatile();
4285 }
4286
4287 // If this is our last pass, check that the final object type is OK.
4288 if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) {
4289 // Accesses to volatile objects are prohibited.
4290 if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) {
4291 if (Info.getLangOpts().CPlusPlus) {
4292 int DiagKind;
4293 SourceLocation Loc;
4294 const NamedDecl *Decl = nullptr;
4295 if (VolatileField) {
4296 DiagKind = 2;
4297 Loc = VolatileField->getLocation();
4298 Decl = VolatileField;
4299 } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) {
4300 DiagKind = 1;
4301 Loc = VD->getLocation();
4302 Decl = VD;
4303 } else {
4304 DiagKind = 0;
4305 if (auto *E = Obj.Base.dyn_cast<const Expr *>())
4306 Loc = E->getExprLoc();
4307 }
4308 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_obj, ExtraNotes: 1)
4309 << handler.AccessKind << DiagKind << Decl;
4310 Info.Note(Loc, DiagId: diag::note_constexpr_volatile_here) << DiagKind;
4311 } else {
4312 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4313 }
4314 return handler.failed();
4315 }
4316
4317 // If we are reading an object of class type, there may still be more
4318 // things we need to check: if there are any mutable subobjects, we
4319 // cannot perform this read. (This only happens when performing a trivial
4320 // copy or assignment.)
4321 if (ObjType->isRecordType() &&
4322 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) &&
4323 diagnoseMutableFields(Info, E, handler.AccessKind, ObjType))
4324 return handler.failed();
4325 }
4326
4327 if (I == N) {
4328 if (!handler.found(*O, ObjType, Obj.Base))
4329 return false;
4330
4331 // If we modified a bit-field, truncate it to the right width.
4332 if (isModification(handler.AccessKind) &&
4333 LastField && LastField->isBitField() &&
4334 !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField))
4335 return false;
4336
4337 return true;
4338 }
4339
4340 LastField = nullptr;
4341
4342 // The value of an atomic object is represented like a value of the
4343 // underlying type, so look through the _Atomic wrapper.
4344 if (const AtomicType *AT = ObjType->getAs<AtomicType>())
4345 ObjType = Info.Ctx.getQualifiedType(T: AT->getValueType(),
4346 Qs: ObjType.getQualifiers());
4347
4348 if (ObjType->isArrayType()) {
4349 // Next subobject is an array element.
4350 const ArrayType *AT = Info.Ctx.getAsArrayType(T: ObjType);
4351 assert((isa<ConstantArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
4352 "vla in literal type?");
4353 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4354 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4355 CAT && CAT->getSize().ule(RHS: Index)) {
4356 // Note, it should not be possible to form a pointer with a valid
4357 // designator which points more than one past the end of the array.
4358 if (Info.getLangOpts().CPlusPlus11)
4359 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4360 << handler.AccessKind;
4361 else
4362 Info.FFDiag(E);
4363 return handler.failed();
4364 }
4365
4366 ObjType = AT->getElementType();
4367
4368 if (O->getArrayInitializedElts() > Index)
4369 O = &O->getArrayInitializedElt(I: Index);
4370 else if (!isRead(handler.AccessKind)) {
4371 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4372 CAT && !CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
4373 return handler.failed();
4374
4375 expandArray(Array&: *O, Index);
4376 O = &O->getArrayInitializedElt(I: Index);
4377 } else
4378 O = &O->getArrayFiller();
4379 } else if (ObjType->isAnyComplexType()) {
4380 // Next subobject is a complex number.
4381 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4382 if (Index > 1) {
4383 if (Info.getLangOpts().CPlusPlus11)
4384 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4385 << handler.AccessKind;
4386 else
4387 Info.FFDiag(E);
4388 return handler.failed();
4389 }
4390
4391 ObjType = getSubobjectType(
4392 ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType());
4393
4394 assert(I == N - 1 && "extracting subobject of scalar?");
4395 if (O->isComplexInt()) {
4396 return handler.found(Index ? O->getComplexIntImag()
4397 : O->getComplexIntReal(), ObjType);
4398 } else {
4399 assert(O->isComplexFloat());
4400 return handler.found(Index ? O->getComplexFloatImag()
4401 : O->getComplexFloatReal(), ObjType);
4402 }
4403 } else if (const auto *VT = ObjType->getAs<VectorType>()) {
4404 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4405 unsigned NumElements = VT->getNumElements();
4406 if (Index == NumElements) {
4407 if (Info.getLangOpts().CPlusPlus11)
4408 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4409 << handler.AccessKind;
4410 else
4411 Info.FFDiag(E);
4412 return handler.failed();
4413 }
4414
4415 if (Index > NumElements) {
4416 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
4417 << Index << /*array*/ 0 << NumElements;
4418 return handler.failed();
4419 }
4420
4421 ObjType = VT->getElementType();
4422 assert(I == N - 1 && "extracting subobject of scalar?");
4423
4424 if (O->isIndeterminate()) {
4425 if (isRead(handler.AccessKind)) {
4426 Info.FFDiag(E);
4427 return handler.failed();
4428 }
4429 expandVector(Vec&: *O, NumElements);
4430 }
4431 assert(O->isVector() && "unexpected object during vector element access");
4432 return handler.found(O->getVectorElt(I: Index), ObjType, Obj.Base);
4433 } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) {
4434 if (Field->isMutable() &&
4435 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) {
4436 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1)
4437 << handler.AccessKind << Field;
4438 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
4439 return handler.failed();
4440 }
4441
4442 // Next subobject is a class, struct or union field.
4443 RecordDecl *RD = ObjType->castAsCanonical<RecordType>()->getDecl();
4444 if (RD->isUnion()) {
4445 const FieldDecl *UnionField = O->getUnionField();
4446 if (!UnionField ||
4447 UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
4448 if (I == N - 1 && handler.AccessKind == AK_Construct) {
4449 // Placement new onto an inactive union member makes it active.
4450 O->setUnion(Field, Value: APValue());
4451 } else {
4452 // Pointer to/into inactive union member: Not within lifetime
4453 if (handler.AccessKind == AK_IsWithinLifetime)
4454 return false;
4455 // FIXME: If O->getUnionValue() is absent, report that there's no
4456 // active union member rather than reporting the prior active union
4457 // member. We'll need to fix nullptr_t to not use APValue() as its
4458 // representation first.
4459 Info.FFDiag(E, DiagId: diag::note_constexpr_access_inactive_union_member)
4460 << handler.AccessKind << Field << !UnionField << UnionField;
4461 return handler.failed();
4462 }
4463 }
4464 O = &O->getUnionValue();
4465 } else
4466 O = &O->getStructField(i: Field->getFieldIndex());
4467
4468 ObjType = getSubobjectType(ObjType, SubobjType: Field->getType(), IsMutable: Field->isMutable());
4469 LastField = Field;
4470 if (Field->getType().isVolatileQualified())
4471 VolatileField = Field;
4472 } else {
4473 // Next subobject is a base class.
4474 const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
4475 const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]);
4476
4477 unsigned BaseIndex = getBaseIndex(Derived, Base);
4478 unsigned NumNonVirtualBases = O->getStructNumBases();
4479 if (BaseIndex >= NumNonVirtualBases) {
4480 O = &O->getStructVirtualBase(i: BaseIndex - NumNonVirtualBases);
4481 } else
4482 O = &O->getStructBase(i: BaseIndex);
4483
4484 ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getCanonicalTagType(TD: Base));
4485 }
4486 }
4487}
4488
4489namespace {
4490struct ExtractSubobjectHandler {
4491 EvalInfo &Info;
4492 const Expr *E;
4493 APValue &Result;
4494 const AccessKinds AccessKind;
4495
4496 typedef bool result_type;
4497 bool failed() { return false; }
4498 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4499 Result = Subobj;
4500 if (AccessKind == AK_ReadObjectRepresentation)
4501 return true;
4502 return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result);
4503 }
4504 bool found(APSInt &Value, QualType SubobjType) {
4505 Result = APValue(Value);
4506 return true;
4507 }
4508 bool found(APFloat &Value, QualType SubobjType) {
4509 Result = APValue(Value);
4510 return true;
4511 }
4512};
4513} // end anonymous namespace
4514
4515/// Extract the designated sub-object of an rvalue.
4516static bool extractSubobject(EvalInfo &Info, const Expr *E,
4517 const CompleteObject &Obj,
4518 const SubobjectDesignator &Sub, APValue &Result,
4519 AccessKinds AK = AK_Read) {
4520 assert(AK == AK_Read || AK == AK_ReadObjectRepresentation);
4521 ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK};
4522 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4523}
4524
4525namespace {
4526struct ModifySubobjectHandler {
4527 EvalInfo &Info;
4528 APValue &NewVal;
4529 const Expr *E;
4530
4531 typedef bool result_type;
4532 static const AccessKinds AccessKind = AK_Assign;
4533
4534 bool checkConst(QualType QT) {
4535 // Assigning to a const object has undefined behavior.
4536 if (QT.isConstQualified()) {
4537 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
4538 return false;
4539 }
4540 return true;
4541 }
4542
4543 bool failed() { return false; }
4544 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4545 if (!checkConst(QT: SubobjType))
4546 return false;
4547 // We've been given ownership of NewVal, so just swap it in.
4548 Subobj.swap(RHS&: NewVal);
4549 return true;
4550 }
4551 bool found(APSInt &Value, QualType SubobjType) {
4552 if (!checkConst(QT: SubobjType))
4553 return false;
4554 if (!NewVal.isInt()) {
4555 // Maybe trying to write a cast pointer value into a complex?
4556 Info.FFDiag(E);
4557 return false;
4558 }
4559 Value = NewVal.getInt();
4560 return true;
4561 }
4562 bool found(APFloat &Value, QualType SubobjType) {
4563 if (!checkConst(QT: SubobjType))
4564 return false;
4565 Value = NewVal.getFloat();
4566 return true;
4567 }
4568};
4569} // end anonymous namespace
4570
4571const AccessKinds ModifySubobjectHandler::AccessKind;
4572
4573/// Update the designated sub-object of an rvalue to the given value.
4574static bool modifySubobject(EvalInfo &Info, const Expr *E,
4575 const CompleteObject &Obj,
4576 const SubobjectDesignator &Sub,
4577 APValue &NewVal) {
4578 ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E };
4579 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4580}
4581
4582/// Find the position where two subobject designators diverge, or equivalently
4583/// the length of the common initial subsequence.
4584static unsigned FindDesignatorMismatch(QualType ObjType,
4585 const SubobjectDesignator &A,
4586 const SubobjectDesignator &B,
4587 bool &WasArrayIndex) {
4588 unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size());
4589 for (/**/; I != N; ++I) {
4590 if (!ObjType.isNull() &&
4591 (ObjType->isArrayType() || ObjType->isAnyComplexType())) {
4592 // Next subobject is an array element.
4593 if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) {
4594 WasArrayIndex = true;
4595 return I;
4596 }
4597 if (ObjType->isAnyComplexType())
4598 ObjType = ObjType->castAs<ComplexType>()->getElementType();
4599 else
4600 ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
4601 } else {
4602 if (A.Entries[I].getAsBaseOrMember() !=
4603 B.Entries[I].getAsBaseOrMember()) {
4604 WasArrayIndex = false;
4605 return I;
4606 }
4607 if (const FieldDecl *FD = getAsField(E: A.Entries[I]))
4608 // Next subobject is a field.
4609 ObjType = FD->getType();
4610 else
4611 // Next subobject is a base class.
4612 ObjType = QualType();
4613 }
4614 }
4615 WasArrayIndex = false;
4616 return I;
4617}
4618
4619/// Determine whether the given subobject designators refer to elements of the
4620/// same array object.
4621static bool AreElementsOfSameArray(QualType ObjType,
4622 const SubobjectDesignator &A,
4623 const SubobjectDesignator &B) {
4624 if (A.Entries.size() != B.Entries.size())
4625 return false;
4626
4627 bool IsArray = A.MostDerivedIsArrayElement;
4628 if (IsArray && A.MostDerivedPathLength != A.Entries.size())
4629 // A is a subobject of the array element.
4630 return false;
4631
4632 // If A (and B) designates an array element, the last entry will be the array
4633 // index. That doesn't have to match. Otherwise, we're in the 'implicit array
4634 // of length 1' case, and the entire path must match.
4635 bool WasArrayIndex;
4636 unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
4637 return CommonLength >= A.Entries.size() - IsArray;
4638}
4639
4640/// Find the complete object to which an LValue refers.
4641static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
4642 AccessKinds AK, const LValue &LVal,
4643 QualType LValType) {
4644 if (LVal.InvalidBase) {
4645 Info.FFDiag(E);
4646 return CompleteObject();
4647 }
4648
4649 if (!LVal.Base) {
4650 if (AK == AccessKinds::AK_Dereference)
4651 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
4652 else
4653 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
4654 return CompleteObject();
4655 }
4656
4657 CallStackFrame *Frame = nullptr;
4658 unsigned Depth = 0;
4659 if (LVal.getLValueCallIndex()) {
4660 std::tie(args&: Frame, args&: Depth) =
4661 Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex());
4662 if (!Frame) {
4663 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit, ExtraNotes: 1)
4664 << AK << /*Indeterminate=*/false << E->getSourceRange();
4665 NoteLValueLocation(Info, Base: LVal.Base);
4666 return CompleteObject();
4667 }
4668 }
4669
4670 bool IsAccess = isAnyAccess(AK);
4671
4672 // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
4673 // is not a constant expression (even if the object is non-volatile). We also
4674 // apply this rule to C++98, in order to conform to the expected 'volatile'
4675 // semantics.
4676 if (isFormalAccess(AK) && LValType.isVolatileQualified()) {
4677 if (Info.getLangOpts().CPlusPlus)
4678 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_type)
4679 << AK << LValType;
4680 else
4681 Info.FFDiag(E);
4682 return CompleteObject();
4683 }
4684
4685 // Compute value storage location and type of base object.
4686 APValue *BaseVal = nullptr;
4687 QualType BaseType = getType(B: LVal.Base);
4688
4689 if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl &&
4690 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4691 // This is the object whose initializer we're evaluating, so its lifetime
4692 // started in the current evaluation.
4693 BaseVal = Info.EvaluatingDeclValue;
4694 } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) {
4695 // Allow reading from a GUID declaration.
4696 if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) {
4697 if (isModification(AK)) {
4698 // All the remaining cases do not permit modification of the object.
4699 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4700 return CompleteObject();
4701 }
4702 APValue &V = GD->getAsAPValue();
4703 if (V.isAbsent()) {
4704 Info.FFDiag(E, DiagId: diag::note_constexpr_unsupported_layout)
4705 << GD->getType();
4706 return CompleteObject();
4707 }
4708 return CompleteObject(LVal.Base, &V, GD->getType());
4709 }
4710
4711 // Allow reading the APValue from an UnnamedGlobalConstantDecl.
4712 if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) {
4713 if (isModification(AK)) {
4714 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4715 return CompleteObject();
4716 }
4717 return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()),
4718 GCD->getType());
4719 }
4720
4721 // Allow reading from template parameter objects.
4722 if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) {
4723 if (isModification(AK)) {
4724 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4725 return CompleteObject();
4726 }
4727 return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()),
4728 TPO->getType());
4729 }
4730
4731 // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
4732 // In C++11, constexpr, non-volatile variables initialized with constant
4733 // expressions are constant expressions too. Inside constexpr functions,
4734 // parameters are constant expressions even if they're non-const.
4735 // In C++1y, objects local to a constant expression (those with a Frame) are
4736 // both readable and writable inside constant expressions.
4737 // In C, such things can also be folded, although they are not ICEs.
4738 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
4739 if (VD) {
4740 if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx))
4741 VD = VDef;
4742 }
4743 if (!VD || VD->isInvalidDecl()) {
4744 Info.FFDiag(E);
4745 return CompleteObject();
4746 }
4747
4748 bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx);
4749 bool ConstexprVar = false;
4750 if (const auto *VD = dyn_cast_if_present<VarDecl>(
4751 Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))
4752 ConstexprVar = VD->isConstexpr();
4753
4754 // Unless we're looking at a local variable or argument in a constexpr call,
4755 // the variable we're reading must be const (unless we are binding to a
4756 // reference).
4757 if (AK != clang::AK_Dereference && !Frame) {
4758 if (IsAccess && isa<ParmVarDecl>(Val: VD)) {
4759 // Access of a parameter that's not associated with a frame isn't going
4760 // to work out, but we can leave it to evaluateVarDeclInit to provide a
4761 // suitable diagnostic.
4762 } else if (Info.getLangOpts().CPlusPlus14 &&
4763 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4764 // OK, we can read and modify an object if we're in the process of
4765 // evaluating its initializer, because its lifetime began in this
4766 // evaluation.
4767 } else if (isModification(AK)) {
4768 // All the remaining cases do not permit modification of the object.
4769 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4770 return CompleteObject();
4771 } else if (VD->isConstexpr()) {
4772 // OK, we can read this variable.
4773 } else if (Info.getLangOpts().C23 && ConstexprVar) {
4774 Info.FFDiag(E);
4775 return CompleteObject();
4776 } else if (BaseType->isIntegralOrEnumerationType()) {
4777 if (!IsConstant) {
4778 if (!IsAccess)
4779 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4780 if (Info.getLangOpts().CPlusPlus) {
4781 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_non_const_int, ExtraNotes: 1) << VD;
4782 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4783 } else {
4784 Info.FFDiag(E);
4785 }
4786 return CompleteObject();
4787 }
4788 } else if (!IsAccess) {
4789 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4790 } else if ((IsConstant || BaseType->isReferenceType()) &&
4791 Info.checkingPotentialConstantExpression() &&
4792 BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) {
4793 // This variable might end up being constexpr. Don't diagnose it yet.
4794 } else if (IsConstant) {
4795 // Keep evaluating to see what we can do. In particular, we support
4796 // folding of const floating-point types, in order to make static const
4797 // data members of such types (supported as an extension) more useful.
4798 if (Info.getLangOpts().CPlusPlus) {
4799 Info.CCEDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4800 ? diag::note_constexpr_ltor_non_constexpr
4801 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4802 << VD << BaseType;
4803 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4804 } else {
4805 Info.CCEDiag(E);
4806 }
4807 } else {
4808 // Never allow reading a non-const value.
4809 if (Info.getLangOpts().CPlusPlus) {
4810 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4811 ? diag::note_constexpr_ltor_non_constexpr
4812 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4813 << VD << BaseType;
4814 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4815 } else {
4816 Info.FFDiag(E);
4817 }
4818 return CompleteObject();
4819 }
4820 }
4821
4822 // When binding to a reference, the variable does not need to be constexpr
4823 // or have constant initalization.
4824 if (AK != clang::AK_Dereference &&
4825 !evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(),
4826 Result&: BaseVal))
4827 return CompleteObject();
4828 // If evaluateVarDeclInit sees a constexpr-unknown variable, it returns
4829 // a null BaseVal. Any constexpr-unknown variable seen here is an error:
4830 // we can't access a constexpr-unknown object.
4831 if (AK != clang::AK_Dereference && !BaseVal) {
4832 if (!Info.checkingPotentialConstantExpression()) {
4833 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unknown_variable, ExtraNotes: 1)
4834 << AK << VD;
4835 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4836 }
4837 return CompleteObject();
4838 }
4839 } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {
4840 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
4841 if (!Alloc) {
4842 Info.FFDiag(E, DiagId: diag::note_constexpr_access_deleted_object) << AK;
4843 return CompleteObject();
4844 }
4845 return CompleteObject(LVal.Base, &(*Alloc)->Value,
4846 LVal.Base.getDynamicAllocType());
4847 }
4848 // When binding to a reference, the variable does not need to be
4849 // within its lifetime.
4850 else if (AK != clang::AK_Dereference) {
4851 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4852
4853 if (!Frame) {
4854 if (const MaterializeTemporaryExpr *MTE =
4855 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) {
4856 assert(MTE->getStorageDuration() == SD_Static &&
4857 "should have a frame for a non-global materialized temporary");
4858
4859 // C++20 [expr.const]p4: [DR2126]
4860 // An object or reference is usable in constant expressions if it is
4861 // - a temporary object of non-volatile const-qualified literal type
4862 // whose lifetime is extended to that of a variable that is usable
4863 // in constant expressions
4864 //
4865 // C++20 [expr.const]p5:
4866 // an lvalue-to-rvalue conversion [is not allowed unless it applies to]
4867 // - a non-volatile glvalue that refers to an object that is usable
4868 // in constant expressions, or
4869 // - a non-volatile glvalue of literal type that refers to a
4870 // non-volatile object whose lifetime began within the evaluation
4871 // of E;
4872 //
4873 // C++11 misses the 'began within the evaluation of e' check and
4874 // instead allows all temporaries, including things like:
4875 // int &&r = 1;
4876 // int x = ++r;
4877 // constexpr int k = r;
4878 // Therefore we use the C++14-onwards rules in C++11 too.
4879 //
4880 // Note that temporaries whose lifetimes began while evaluating a
4881 // variable's constructor are not usable while evaluating the
4882 // corresponding destructor, not even if they're of const-qualified
4883 // types.
4884 if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) &&
4885 !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4886 if (!IsAccess)
4887 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4888 Info.FFDiag(E, DiagId: diag::note_constexpr_access_static_temporary, ExtraNotes: 1) << AK;
4889 Info.Note(Loc: MTE->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
4890 return CompleteObject();
4891 }
4892
4893 BaseVal = MTE->getOrCreateValue(MayCreate: false);
4894 assert(BaseVal && "got reference to unevaluated temporary");
4895 } else if (const CompoundLiteralExpr *CLE =
4896 dyn_cast_or_null<CompoundLiteralExpr>(Val: Base)) {
4897 // According to GCC info page:
4898 //
4899 // 6.28 Compound Literals
4900 //
4901 // As an optimization, G++ sometimes gives array compound literals
4902 // longer lifetimes: when the array either appears outside a function or
4903 // has a const-qualified type. If foo and its initializer had elements
4904 // of type char *const rather than char *, or if foo were a global
4905 // variable, the array would have static storage duration. But it is
4906 // probably safest just to avoid the use of array compound literals in
4907 // C++ code.
4908 //
4909 // Obey that rule by checking constness for converted array types.
4910 if (QualType CLETy = CLE->getType(); CLETy->isArrayType() &&
4911 !LValType->isArrayType() &&
4912 !CLETy.isConstant(Ctx: Info.Ctx)) {
4913 Info.FFDiag(E);
4914 Info.Note(Loc: CLE->getExprLoc(), DiagId: diag::note_declared_at);
4915 return CompleteObject();
4916 }
4917
4918 BaseVal = &CLE->getStaticValue();
4919 } else {
4920 if (!IsAccess)
4921 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4922 APValue Val;
4923 LVal.moveInto(V&: Val);
4924 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unreadable_object)
4925 << AK
4926 << Val.getAsString(Ctx: Info.Ctx,
4927 Ty: Info.Ctx.getLValueReferenceType(T: LValType));
4928 NoteLValueLocation(Info, Base: LVal.Base);
4929 return CompleteObject();
4930 }
4931 } else if (AK != clang::AK_Dereference) {
4932 BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion());
4933 assert(BaseVal && "missing value for temporary");
4934 }
4935 }
4936
4937 // In C++14, we can't safely access any mutable state when we might be
4938 // evaluating after an unmodeled side effect. Parameters are modeled as state
4939 // in the caller, but aren't visible once the call returns, so they can be
4940 // modified in a speculatively-evaluated call.
4941 //
4942 // FIXME: Not all local state is mutable. Allow local constant subobjects
4943 // to be read here (but take care with 'mutable' fields).
4944 unsigned VisibleDepth = Depth;
4945 if (llvm::isa_and_nonnull<ParmVarDecl>(
4946 Val: LVal.Base.dyn_cast<const ValueDecl *>()))
4947 ++VisibleDepth;
4948 if ((Frame && Info.getLangOpts().CPlusPlus14 &&
4949 Info.EvalStatus.HasSideEffects) ||
4950 (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth))
4951 return CompleteObject();
4952
4953 return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType);
4954}
4955
4956/// Perform an lvalue-to-rvalue conversion on the given glvalue. This
4957/// can also be used for 'lvalue-to-lvalue' conversions for looking up the
4958/// glvalue referred to by an entity of reference type.
4959///
4960/// \param Info - Information about the ongoing evaluation.
4961/// \param Conv - The expression for which we are performing the conversion.
4962/// Used for diagnostics.
4963/// \param Type - The type of the glvalue (before stripping cv-qualifiers in the
4964/// case of a non-class type).
4965/// \param LVal - The glvalue on which we are attempting to perform this action.
4966/// \param RVal - The produced value will be placed here.
4967/// \param WantObjectRepresentation - If true, we're looking for the object
4968/// representation rather than the value, and in particular,
4969/// there is no requirement that the result be fully initialized.
4970static bool
4971handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type,
4972 const LValue &LVal, APValue &RVal,
4973 bool WantObjectRepresentation = false) {
4974 if (LVal.Designator.Invalid)
4975 return false;
4976
4977 // Check for special cases where there is no existing APValue to look at.
4978 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4979
4980 AccessKinds AK =
4981 WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read;
4982
4983 if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) {
4984 if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) {
4985 // Special-case character extraction so we don't have to construct an
4986 // APValue for the whole string.
4987 assert(LVal.Designator.Entries.size() <= 1 &&
4988 "Can only read characters from string literals");
4989 if (LVal.Designator.Entries.empty()) {
4990 // Fail for now for LValue to RValue conversion of an array.
4991 // (This shouldn't show up in C/C++, but it could be triggered by a
4992 // weird EvaluateAsRValue call from a tool.)
4993 Info.FFDiag(E: Conv);
4994 return false;
4995 }
4996 if (LVal.Designator.isOnePastTheEnd()) {
4997 if (Info.getLangOpts().CPlusPlus11)
4998 Info.FFDiag(E: Conv, DiagId: diag::note_constexpr_access_past_end) << AK;
4999 else
5000 Info.FFDiag(E: Conv);
5001 return false;
5002 }
5003 uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex();
5004 RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex));
5005 return true;
5006 }
5007 }
5008
5009 CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type);
5010 return Obj && extractSubobject(Info, E: Conv, Obj, Sub: LVal.Designator, Result&: RVal, AK);
5011}
5012
5013static bool hlslElementwiseCastHelper(EvalInfo &Info, const Expr *E,
5014 QualType DestTy,
5015 SmallVectorImpl<APValue> &SrcVals,
5016 SmallVectorImpl<QualType> &SrcTypes) {
5017 APValue Val;
5018 if (!Evaluate(Result&: Val, Info, E))
5019 return false;
5020
5021 // must be dealing with a record
5022 if (Val.isLValue()) {
5023 LValue LVal;
5024 LVal.setFrom(Ctx: Info.Ctx, V: Val);
5025 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal, RVal&: Val))
5026 return false;
5027 }
5028
5029 unsigned NEls = elementwiseSize(Info, BaseTy: DestTy);
5030 // flatten the source
5031 if (!flattenAPValue(Info, E, Value: Val, BaseTy: E->getType(), Elements&: SrcVals, Types&: SrcTypes, Size: NEls))
5032 return false;
5033
5034 return true;
5035}
5036
5037/// Perform an assignment of Val to LVal. Takes ownership of Val.
5038static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal,
5039 QualType LValType, APValue &Val) {
5040 if (LVal.Designator.Invalid)
5041 return false;
5042
5043 if (!Info.getLangOpts().CPlusPlus14) {
5044 Info.FFDiag(E);
5045 return false;
5046 }
5047
5048 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5049 return Obj && modifySubobject(Info, E, Obj, Sub: LVal.Designator, NewVal&: Val);
5050}
5051
5052namespace {
5053struct CompoundAssignSubobjectHandler {
5054 EvalInfo &Info;
5055 const CompoundAssignOperator *E;
5056 QualType PromotedLHSType;
5057 BinaryOperatorKind Opcode;
5058 const APValue &RHS;
5059
5060 static const AccessKinds AccessKind = AK_Assign;
5061
5062 typedef bool result_type;
5063
5064 bool checkConst(QualType QT) {
5065 // Assigning to a const object has undefined behavior.
5066 if (QT.isConstQualified()) {
5067 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5068 return false;
5069 }
5070 return true;
5071 }
5072
5073 bool failed() { return false; }
5074 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5075 switch (Subobj.getKind()) {
5076 case APValue::Int:
5077 return found(Value&: Subobj.getInt(), SubobjType);
5078 case APValue::Float:
5079 return found(Value&: Subobj.getFloat(), SubobjType);
5080 case APValue::ComplexInt:
5081 case APValue::ComplexFloat:
5082 // FIXME: Implement complex compound assignment.
5083 Info.FFDiag(E);
5084 return false;
5085 case APValue::LValue:
5086 return foundPointer(Subobj, SubobjType);
5087 case APValue::Vector:
5088 return foundVector(Value&: Subobj, SubobjType);
5089 case APValue::Indeterminate:
5090 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
5091 << /*read of=*/0 << /*uninitialized object=*/1
5092 << E->getLHS()->getSourceRange();
5093 NoteLValueLocation(Info, Base);
5094 return false;
5095 default:
5096 // FIXME: can this happen?
5097 Info.FFDiag(E);
5098 return false;
5099 }
5100 }
5101
5102 bool foundVector(APValue &Value, QualType SubobjType) {
5103 if (!checkConst(QT: SubobjType))
5104 return false;
5105
5106 if (!SubobjType->isVectorType()) {
5107 Info.FFDiag(E);
5108 return false;
5109 }
5110 return handleVectorVectorBinOp(Info, E, Opcode, LHSValue&: Value, RHSValue: RHS);
5111 }
5112
5113 bool found(APSInt &Value, QualType SubobjType) {
5114 if (!checkConst(QT: SubobjType))
5115 return false;
5116
5117 if (!SubobjType->isIntegerType()) {
5118 // We don't support compound assignment on integer-cast-to-pointer
5119 // values.
5120 Info.FFDiag(E);
5121 return false;
5122 }
5123
5124 if (RHS.isInt()) {
5125 APSInt LHS =
5126 HandleIntToIntCast(Info, E, DestType: PromotedLHSType, SrcType: SubobjType, Value);
5127 if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS: RHS.getInt(), Result&: LHS))
5128 return false;
5129 Value = HandleIntToIntCast(Info, E, DestType: SubobjType, SrcType: PromotedLHSType, Value: LHS);
5130 return true;
5131 } else if (RHS.isFloat()) {
5132 const FPOptions FPO = E->getFPFeaturesInEffect(
5133 LO: Info.Ctx.getLangOpts());
5134 APFloat FValue(0.0);
5135 return HandleIntToFloatCast(Info, E, FPO, SrcType: SubobjType, Value,
5136 DestType: PromotedLHSType, Result&: FValue) &&
5137 handleFloatFloatBinOp(Info, E, LHS&: FValue, Opcode, RHS: RHS.getFloat()) &&
5138 HandleFloatToIntCast(Info, E, SrcType: PromotedLHSType, Value: FValue, DestType: SubobjType,
5139 Result&: Value);
5140 }
5141
5142 Info.FFDiag(E);
5143 return false;
5144 }
5145 bool found(APFloat &Value, QualType SubobjType) {
5146 return checkConst(QT: SubobjType) &&
5147 HandleFloatToFloatCast(Info, E, SrcType: SubobjType, DestType: PromotedLHSType,
5148 Result&: Value) &&
5149 handleFloatFloatBinOp(Info, E, LHS&: Value, Opcode, RHS: RHS.getFloat()) &&
5150 HandleFloatToFloatCast(Info, E, SrcType: PromotedLHSType, DestType: SubobjType, Result&: Value);
5151 }
5152 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5153 if (!checkConst(QT: SubobjType))
5154 return false;
5155
5156 QualType PointeeType;
5157 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5158 PointeeType = PT->getPointeeType();
5159
5160 if (PointeeType.isNull() || !RHS.isInt() ||
5161 (Opcode != BO_Add && Opcode != BO_Sub)) {
5162 Info.FFDiag(E);
5163 return false;
5164 }
5165
5166 APSInt Offset = RHS.getInt();
5167 if (Opcode == BO_Sub)
5168 negateAsSigned(Int&: Offset);
5169
5170 LValue LVal;
5171 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5172 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType, Adjustment: Offset))
5173 return false;
5174 LVal.moveInto(V&: Subobj);
5175 return true;
5176 }
5177};
5178} // end anonymous namespace
5179
5180const AccessKinds CompoundAssignSubobjectHandler::AccessKind;
5181
5182/// Perform a compound assignment of LVal <op>= RVal.
5183static bool handleCompoundAssignment(EvalInfo &Info,
5184 const CompoundAssignOperator *E,
5185 const LValue &LVal, QualType LValType,
5186 QualType PromotedLValType,
5187 BinaryOperatorKind Opcode,
5188 const APValue &RVal) {
5189 if (LVal.Designator.Invalid)
5190 return false;
5191
5192 if (!Info.getLangOpts().CPlusPlus14) {
5193 Info.FFDiag(E);
5194 return false;
5195 }
5196
5197 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5198 CompoundAssignSubobjectHandler Handler = { .Info: Info, .E: E, .PromotedLHSType: PromotedLValType, .Opcode: Opcode,
5199 .RHS: RVal };
5200 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5201}
5202
5203namespace {
5204struct IncDecSubobjectHandler {
5205 EvalInfo &Info;
5206 const UnaryOperator *E;
5207 AccessKinds AccessKind;
5208 APValue *Old;
5209
5210 typedef bool result_type;
5211
5212 bool checkConst(QualType QT) {
5213 // Assigning to a const object has undefined behavior.
5214 if (QT.isConstQualified()) {
5215 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5216 return false;
5217 }
5218 return true;
5219 }
5220
5221 bool failed() { return false; }
5222 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5223 // Stash the old value. Also clear Old, so we don't clobber it later
5224 // if we're post-incrementing a complex.
5225 if (Old) {
5226 *Old = Subobj;
5227 Old = nullptr;
5228 }
5229
5230 switch (Subobj.getKind()) {
5231 case APValue::Int:
5232 return found(Value&: Subobj.getInt(), SubobjType);
5233 case APValue::Float:
5234 return found(Value&: Subobj.getFloat(), SubobjType);
5235 case APValue::ComplexInt:
5236 return found(Value&: Subobj.getComplexIntReal(),
5237 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5238 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5239 case APValue::ComplexFloat:
5240 return found(Value&: Subobj.getComplexFloatReal(),
5241 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5242 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5243 case APValue::LValue:
5244 return foundPointer(Subobj, SubobjType);
5245 default:
5246 // FIXME: can this happen?
5247 Info.FFDiag(E);
5248 return false;
5249 }
5250 }
5251 bool found(APSInt &Value, QualType SubobjType) {
5252 if (!checkConst(QT: SubobjType))
5253 return false;
5254
5255 if (!SubobjType->isIntegerType()) {
5256 // We don't support increment / decrement on integer-cast-to-pointer
5257 // values.
5258 Info.FFDiag(E);
5259 return false;
5260 }
5261
5262 if (Old) *Old = APValue(Value);
5263
5264 // bool arithmetic promotes to int, and the conversion back to bool
5265 // doesn't reduce mod 2^n, so special-case it.
5266 if (SubobjType->isBooleanType()) {
5267 if (AccessKind == AK_Increment)
5268 Value = 1;
5269 else
5270 Value = !Value;
5271 return true;
5272 }
5273
5274 bool WasNegative = Value.isNegative();
5275 if (AccessKind == AK_Increment) {
5276 ++Value;
5277
5278 if (!WasNegative && Value.isNegative() && E->canOverflow() &&
5279 !SubobjType.isWrapType()) {
5280 APSInt ActualValue(Value, /*IsUnsigned*/true);
5281 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5282 }
5283 } else {
5284 --Value;
5285
5286 if (WasNegative && !Value.isNegative() && E->canOverflow() &&
5287 !SubobjType.isWrapType()) {
5288 unsigned BitWidth = Value.getBitWidth();
5289 APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false);
5290 ActualValue.setBit(BitWidth);
5291 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5292 }
5293 }
5294 return true;
5295 }
5296 bool found(APFloat &Value, QualType SubobjType) {
5297 if (!checkConst(QT: SubobjType))
5298 return false;
5299
5300 if (Old) *Old = APValue(Value);
5301
5302 APFloat One(Value.getSemantics(), 1);
5303 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
5304 APFloat::opStatus St;
5305 if (AccessKind == AK_Increment)
5306 St = Value.add(RHS: One, RM);
5307 else
5308 St = Value.subtract(RHS: One, RM);
5309 return checkFloatingPointResultForConstantFolding(Info, E, St);
5310 }
5311 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5312 if (!checkConst(QT: SubobjType))
5313 return false;
5314
5315 QualType PointeeType;
5316 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5317 PointeeType = PT->getPointeeType();
5318 else {
5319 Info.FFDiag(E);
5320 return false;
5321 }
5322
5323 LValue LVal;
5324 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5325 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType,
5326 Adjustment: AccessKind == AK_Increment ? 1 : -1))
5327 return false;
5328 LVal.moveInto(V&: Subobj);
5329 return true;
5330 }
5331};
5332} // end anonymous namespace
5333
5334/// Perform an increment or decrement on LVal.
5335static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal,
5336 QualType LValType, bool IsIncrement, APValue *Old) {
5337 if (LVal.Designator.Invalid)
5338 return false;
5339
5340 if (!Info.getLangOpts().CPlusPlus14) {
5341 Info.FFDiag(E);
5342 return false;
5343 }
5344
5345 AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement;
5346 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType);
5347 IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old};
5348 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5349}
5350
5351/// Build an lvalue for the object argument of a member function call.
5352static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
5353 LValue &This) {
5354 if (Object->getType()->isPointerType() && Object->isPRValue())
5355 return EvaluatePointer(E: Object, Result&: This, Info);
5356
5357 if (Object->isGLValue())
5358 return EvaluateLValue(E: Object, Result&: This, Info);
5359
5360 if (Object->getType()->isLiteralType(Ctx: Info.Ctx))
5361 return EvaluateTemporary(E: Object, Result&: This, Info);
5362
5363 if (Object->getType()->isRecordType() && Object->isPRValue())
5364 return EvaluateTemporary(E: Object, Result&: This, Info);
5365
5366 Info.FFDiag(E: Object, DiagId: diag::note_constexpr_nonliteral) << Object->getType();
5367 return false;
5368}
5369
5370/// HandleMemberPointerAccess - Evaluate a member access operation and build an
5371/// lvalue referring to the result.
5372///
5373/// \param Info - Information about the ongoing evaluation.
5374/// \param LV - An lvalue referring to the base of the member pointer.
5375/// \param RHS - The member pointer expression.
5376/// \param IncludeMember - Specifies whether the member itself is included in
5377/// the resulting LValue subobject designator. This is not possible when
5378/// creating a bound member function.
5379/// \return The field or method declaration to which the member pointer refers,
5380/// or 0 if evaluation fails.
5381static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5382 QualType LVType,
5383 LValue &LV,
5384 const Expr *RHS,
5385 bool IncludeMember = true) {
5386 MemberPtr MemPtr;
5387 if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info))
5388 return nullptr;
5389
5390 // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
5391 // member value, the behavior is undefined.
5392 if (!MemPtr.getDecl()) {
5393 // FIXME: Specific diagnostic.
5394 Info.FFDiag(E: RHS);
5395 return nullptr;
5396 }
5397
5398 if (MemPtr.isDerivedMember()) {
5399 // This is a member of some derived class. Truncate LV appropriately.
5400 // The end of the derived-to-base path for the base object must match the
5401 // derived-to-base path for the member pointer.
5402 // C++23 [expr.mptr.oper]p4:
5403 // If the result of E1 is an object [...] whose most derived object does
5404 // not contain the member to which E2 refers, the behavior is undefined.
5405 if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
5406 LV.Designator.Entries.size()) {
5407 Info.FFDiag(E: RHS);
5408 return nullptr;
5409 }
5410 unsigned PathLengthToMember =
5411 LV.Designator.Entries.size() - MemPtr.Path.size();
5412 for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
5413 const CXXRecordDecl *LVDecl = getAsBaseClass(
5414 E: LV.Designator.Entries[PathLengthToMember + I]);
5415 const CXXRecordDecl *MPDecl = MemPtr.Path[I];
5416 if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) {
5417 Info.FFDiag(E: RHS);
5418 return nullptr;
5419 }
5420 }
5421 // MemPtr.Path only contains the base classes of the class directly
5422 // containing the member E2. It is still necessary to check that the class
5423 // directly containing the member E2 lies on the derived-to-base path of E1
5424 // to avoid incorrectly permitting member pointer access into a sibling
5425 // class of the class containing the member E2. If this class would
5426 // correspond to the most-derived class of E1, it either isn't contained in
5427 // LV.Designator.Entries or the corresponding entry refers to an array
5428 // element instead. Therefore get the most derived class directly in this
5429 // case. Otherwise the previous entry should correpond to this class.
5430 const CXXRecordDecl *LastLVDecl =
5431 (PathLengthToMember > LV.Designator.MostDerivedPathLength)
5432 ? getAsBaseClass(E: LV.Designator.Entries[PathLengthToMember - 1])
5433 : LV.Designator.MostDerivedType->getAsCXXRecordDecl();
5434 const CXXRecordDecl *LastMPDecl = MemPtr.getContainingRecord();
5435 if (LastLVDecl->getCanonicalDecl() != LastMPDecl->getCanonicalDecl()) {
5436 Info.FFDiag(E: RHS);
5437 return nullptr;
5438 }
5439
5440 // Truncate the lvalue to the appropriate derived class.
5441 if (!CastToDerivedClass(Info, E: RHS, Result&: LV, TruncatedType: MemPtr.getContainingRecord(),
5442 TruncatedElements: PathLengthToMember))
5443 return nullptr;
5444 } else if (!MemPtr.Path.empty()) {
5445 // Extend the LValue path with the member pointer's path.
5446 LV.Designator.Entries.reserve(N: LV.Designator.Entries.size() +
5447 MemPtr.Path.size() + IncludeMember);
5448
5449 // Walk down to the appropriate base class.
5450 if (const PointerType *PT = LVType->getAs<PointerType>())
5451 LVType = PT->getPointeeType();
5452 const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
5453 assert(RD && "member pointer access on non-class-type expression");
5454 // The first class in the path is that of the lvalue.
5455 for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
5456 const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
5457 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base))
5458 return nullptr;
5459 RD = Base;
5460 }
5461 // Finally cast to the class containing the member.
5462 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD,
5463 Base: MemPtr.getContainingRecord()))
5464 return nullptr;
5465 }
5466
5467 // Add the member. Note that we cannot build bound member functions here.
5468 if (IncludeMember) {
5469 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) {
5470 if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD))
5471 return nullptr;
5472 } else if (const IndirectFieldDecl *IFD =
5473 dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) {
5474 if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD))
5475 return nullptr;
5476 } else {
5477 llvm_unreachable("can't construct reference to bound member function");
5478 }
5479 }
5480
5481 return MemPtr.getDecl();
5482}
5483
5484static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5485 const BinaryOperator *BO,
5486 LValue &LV,
5487 bool IncludeMember = true) {
5488 assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
5489
5490 if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) {
5491 if (Info.noteFailure()) {
5492 MemberPtr MemPtr;
5493 EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info);
5494 }
5495 return nullptr;
5496 }
5497
5498 return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV,
5499 RHS: BO->getRHS(), IncludeMember);
5500}
5501
5502/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
5503/// the provided lvalue, which currently refers to the base object.
5504static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
5505 LValue &Result) {
5506 SubobjectDesignator &D = Result.Designator;
5507 if (D.Invalid || !Result.checkNullPointer(Info, E, CSK: CSK_Derived))
5508 return false;
5509
5510 QualType TargetQT = E->getType();
5511 if (const PointerType *PT = TargetQT->getAs<PointerType>())
5512 TargetQT = PT->getPointeeType();
5513
5514 auto InvalidCast = [&]() {
5515 if (!Info.checkingPotentialConstantExpression() ||
5516 !Result.AllowConstexprUnknown) {
5517 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_downcast)
5518 << D.MostDerivedType << TargetQT;
5519 }
5520 return false;
5521 };
5522
5523 // Check this cast lands within the final derived-to-base subobject path.
5524 if (D.MostDerivedPathLength + E->path_size() > D.Entries.size())
5525 return InvalidCast();
5526
5527 // Check the type of the final cast. We don't need to check the path,
5528 // since a cast can only be formed if the path is unique.
5529 unsigned NewEntriesSize = D.Entries.size() - E->path_size();
5530 const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
5531 const CXXRecordDecl *FinalType;
5532 if (NewEntriesSize == D.MostDerivedPathLength)
5533 FinalType = D.MostDerivedType->getAsCXXRecordDecl();
5534 else
5535 FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]);
5536 if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl())
5537 return InvalidCast();
5538
5539 // Truncate the lvalue to the appropriate derived class.
5540 return CastToDerivedClass(Info, E, Result, TruncatedType: TargetType, TruncatedElements: NewEntriesSize);
5541}
5542
5543/// Get the value to use for a default-initialized object of type T.
5544/// Return false if it encounters something invalid.
5545static bool handleDefaultInitValue(QualType T, APValue &Result,
5546 bool IsCompleteClass = true) {
5547 bool Success = true;
5548
5549 // If there is already a value present don't overwrite it.
5550 if (!Result.isAbsent())
5551 return true;
5552
5553 if (auto *RD = T->getAsCXXRecordDecl()) {
5554 if (RD->isInvalidDecl()) {
5555 Result = APValue();
5556 return false;
5557 }
5558 if (RD->isUnion()) {
5559 Result = APValue((const FieldDecl *)nullptr);
5560 return true;
5561 }
5562
5563 // bases() includes directly specified virtual bases as well.
5564 unsigned NonVirtualBases = countNonVirtualBases(RD);
5565 Result =
5566 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
5567 IsCompleteClass ? RD->getNumVBases() : 0);
5568
5569 unsigned Index = 0;
5570 for (const CXXBaseSpecifier &B : RD->bases()) {
5571 if (B.isVirtual())
5572 continue;
5573 Success &= handleDefaultInitValue(
5574 T: B.getType(), Result&: Result.getStructBase(i: Index), /*IsCompleteClass=*/false);
5575 ++Index;
5576 }
5577
5578 for (const auto *I : RD->fields()) {
5579 if (I->isUnnamedBitField())
5580 continue;
5581 Success &= handleDefaultInitValue(
5582 T: I->getType(), Result&: Result.getStructField(i: I->getFieldIndex()));
5583 }
5584
5585 if (IsCompleteClass) {
5586 Index = 0;
5587
5588 for (const auto &B : RD->vbases()) {
5589 Success &= handleDefaultInitValue(T: B.getType(),
5590 Result&: Result.getStructVirtualBase(i: Index),
5591 /*IsCompleteClass=*/false);
5592 ++Index;
5593 }
5594 } else {
5595 // Virtual bases should only exist at the top level of an APValue.
5596 assert(Result.getStructNumVirtualBases() == 0);
5597 }
5598
5599 return Success;
5600 }
5601
5602 if (auto *AT =
5603 dyn_cast_or_null<ConstantArrayType>(Val: T->getAsArrayTypeUnsafe())) {
5604 Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize());
5605 if (Result.hasArrayFiller())
5606 Success &=
5607 handleDefaultInitValue(T: AT->getElementType(), Result&: Result.getArrayFiller());
5608 return Success;
5609 }
5610
5611 Result = APValue::IndeterminateValue();
5612 return true;
5613}
5614
5615namespace {
5616enum EvalStmtResult {
5617 /// Evaluation failed.
5618 ESR_Failed,
5619 /// Hit a 'return' statement.
5620 ESR_Returned,
5621 /// Evaluation succeeded.
5622 ESR_Succeeded,
5623 /// Hit a 'continue' statement.
5624 ESR_Continue,
5625 /// Hit a 'break' statement.
5626 ESR_Break,
5627 /// Still scanning for 'case' or 'default' statement.
5628 ESR_CaseNotFound
5629};
5630}
5631/// Evaluates the initializer of a reference.
5632static bool EvaluateInitForDeclOfReferenceType(EvalInfo &Info,
5633 const ValueDecl *D,
5634 const Expr *Init, LValue &Result,
5635 APValue &Val) {
5636 assert(Init->isGLValue() && D->getType()->isReferenceType());
5637 // A reference is an lvalue.
5638 if (!EvaluateLValue(E: Init, Result, Info))
5639 return false;
5640 // [C++26][decl.ref]
5641 // The object designated by such a glvalue can be outside its lifetime
5642 // Because a null pointer value or a pointer past the end of an object
5643 // does not point to an object, a reference in a well-defined program cannot
5644 // refer to such things;
5645 if (!Result.Designator.Invalid && Result.Designator.isOnePastTheEnd()) {
5646 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_access_past_end) << AK_Dereference;
5647 return false;
5648 }
5649
5650 // Save the result.
5651 Result.moveInto(V&: Val);
5652 return true;
5653}
5654
5655static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) {
5656 if (VD->isInvalidDecl())
5657 return false;
5658 // We don't need to evaluate the initializer for a static local.
5659 if (!VD->hasLocalStorage())
5660 return true;
5661
5662 LValue Result;
5663 APValue &Val = Info.CurrentCall->createTemporary(Key: VD, T: VD->getType(),
5664 Scope: ScopeKind::Block, LV&: Result);
5665
5666 const Expr *InitE = VD->getInit();
5667 if (!InitE) {
5668 if (VD->getType()->isDependentType())
5669 return Info.noteSideEffect();
5670 return handleDefaultInitValue(T: VD->getType(), Result&: Val);
5671 }
5672 if (InitE->isValueDependent())
5673 return false;
5674
5675 // For references to objects, check they do not designate a one-past-the-end
5676 // object.
5677 if (VD->getType()->isReferenceType()) {
5678 return EvaluateInitForDeclOfReferenceType(Info, D: VD, Init: InitE, Result, Val);
5679 } else if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) {
5680 // Wipe out any partially-computed value, to allow tracking that this
5681 // evaluation failed.
5682 Val = APValue();
5683 return false;
5684 }
5685
5686 return true;
5687}
5688
5689static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5690 const DecompositionDecl *DD);
5691
5692static bool EvaluateDecl(EvalInfo &Info, const Decl *D,
5693 bool EvaluateConditionDecl = false) {
5694 bool OK = true;
5695 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
5696 OK &= EvaluateVarDecl(Info, VD);
5697
5698 if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D);
5699 EvaluateConditionDecl && DD)
5700 OK &= EvaluateDecompositionDeclInit(Info, DD);
5701
5702 return OK;
5703}
5704
5705static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5706 const DecompositionDecl *DD) {
5707 bool OK = true;
5708 for (auto *BD : DD->flat_bindings())
5709 if (auto *VD = BD->getHoldingVar())
5710 OK &= EvaluateDecl(Info, D: VD, /*EvaluateConditionDecl=*/true);
5711
5712 return OK;
5713}
5714
5715static bool MaybeEvaluateDeferredVarDeclInit(EvalInfo &Info,
5716 const VarDecl *VD) {
5717 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) {
5718 if (!EvaluateDecompositionDeclInit(Info, DD))
5719 return false;
5720 }
5721 return true;
5722}
5723
5724static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) {
5725 assert(E->isValueDependent());
5726 if (Info.noteSideEffect())
5727 return true;
5728 assert(E->containsErrors() && "valid value-dependent expression should never "
5729 "reach invalid code path.");
5730 return false;
5731}
5732
5733/// Evaluate a condition (either a variable declaration or an expression).
5734static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl,
5735 const Expr *Cond, bool &Result) {
5736 if (Cond->isValueDependent())
5737 return false;
5738 FullExpressionRAII Scope(Info);
5739 if (CondDecl && !EvaluateDecl(Info, D: CondDecl))
5740 return false;
5741 if (!EvaluateAsBooleanCondition(E: Cond, Result, Info))
5742 return false;
5743 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: CondDecl))
5744 return false;
5745 return Scope.destroy();
5746}
5747
5748namespace {
5749/// A location where the result (returned value) of evaluating a
5750/// statement should be stored.
5751struct StmtResult {
5752 /// The APValue that should be filled in with the returned value.
5753 APValue &Value;
5754 /// The location containing the result, if any (used to support RVO).
5755 const LValue *Slot;
5756};
5757
5758struct TempVersionRAII {
5759 CallStackFrame &Frame;
5760
5761 TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) {
5762 Frame.pushTempVersion();
5763 }
5764
5765 ~TempVersionRAII() {
5766 Frame.popTempVersion();
5767 }
5768};
5769
5770}
5771
5772static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5773 const Stmt *S,
5774 const SwitchCase *SC = nullptr);
5775
5776/// Helper to implement named break/continue. Returns 'true' if the evaluation
5777/// result should be propagated up. Otherwise, it sets the evaluation result
5778/// to either Continue to continue the current loop, or Succeeded to break it.
5779static bool ShouldPropagateBreakContinue(EvalInfo &Info,
5780 const Stmt *LoopOrSwitch,
5781 ArrayRef<BlockScopeRAII *> Scopes,
5782 EvalStmtResult &ESR) {
5783 bool IsSwitch = isa<SwitchStmt>(Val: LoopOrSwitch);
5784
5785 // For loops, map Succeeded to Continue so we don't have to check for both.
5786 if (!IsSwitch && ESR == ESR_Succeeded) {
5787 ESR = ESR_Continue;
5788 return false;
5789 }
5790
5791 if (ESR != ESR_Break && ESR != ESR_Continue)
5792 return false;
5793
5794 // Are we breaking out of or continuing this statement?
5795 bool CanBreakOrContinue = !IsSwitch || ESR == ESR_Break;
5796 const Stmt *StackTop = Info.BreakContinueStack.back();
5797 if (CanBreakOrContinue && (StackTop == nullptr || StackTop == LoopOrSwitch)) {
5798 Info.BreakContinueStack.pop_back();
5799 if (ESR == ESR_Break)
5800 ESR = ESR_Succeeded;
5801 return false;
5802 }
5803
5804 // We're not. Propagate the result up.
5805 for (BlockScopeRAII *S : Scopes) {
5806 if (!S->destroy()) {
5807 ESR = ESR_Failed;
5808 break;
5809 }
5810 }
5811 return true;
5812}
5813
5814/// Evaluate the body of a loop, and translate the result as appropriate.
5815static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info,
5816 const Stmt *Body,
5817 const SwitchCase *Case = nullptr) {
5818 BlockScopeRAII Scope(Info);
5819
5820 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case);
5821 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5822 ESR = ESR_Failed;
5823
5824 return ESR;
5825}
5826
5827/// Evaluate a switch statement.
5828static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
5829 const SwitchStmt *SS) {
5830 BlockScopeRAII Scope(Info);
5831
5832 // Evaluate the switch condition.
5833 APSInt Value;
5834 {
5835 if (const Stmt *Init = SS->getInit()) {
5836 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
5837 if (ESR != ESR_Succeeded) {
5838 if (ESR != ESR_Failed && !Scope.destroy())
5839 ESR = ESR_Failed;
5840 return ESR;
5841 }
5842 }
5843
5844 FullExpressionRAII CondScope(Info);
5845 if (SS->getConditionVariable() &&
5846 !EvaluateDecl(Info, D: SS->getConditionVariable()))
5847 return ESR_Failed;
5848 if (SS->getCond()->isValueDependent()) {
5849 // We don't know what the value is, and which branch should jump to.
5850 EvaluateDependentExpr(E: SS->getCond(), Info);
5851 return ESR_Failed;
5852 }
5853 if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info))
5854 return ESR_Failed;
5855
5856 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: SS->getConditionVariable()))
5857 return ESR_Failed;
5858
5859 if (!CondScope.destroy())
5860 return ESR_Failed;
5861 }
5862
5863 // Find the switch case corresponding to the value of the condition.
5864 // FIXME: Cache this lookup.
5865 const SwitchCase *Found = nullptr;
5866 for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
5867 SC = SC->getNextSwitchCase()) {
5868 if (isa<DefaultStmt>(Val: SC)) {
5869 Found = SC;
5870 continue;
5871 }
5872
5873 const CaseStmt *CS = cast<CaseStmt>(Val: SC);
5874 const Expr *LHS = CS->getLHS();
5875 const Expr *RHS = CS->getRHS();
5876 if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))
5877 return ESR_Failed;
5878 APSInt LHSValue = LHS->EvaluateKnownConstInt(Ctx: Info.Ctx);
5879 APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Ctx: Info.Ctx) : LHSValue;
5880 if (LHSValue <= Value && Value <= RHSValue) {
5881 Found = SC;
5882 break;
5883 }
5884 }
5885
5886 if (!Found)
5887 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
5888
5889 // Search the switch body for the switch case and evaluate it from there.
5890 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found);
5891 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5892 return ESR_Failed;
5893 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: SS, /*Scopes=*/{}, ESR))
5894 return ESR;
5895
5896 switch (ESR) {
5897 case ESR_Break:
5898 llvm_unreachable("Should have been converted to Succeeded");
5899 case ESR_Succeeded:
5900 case ESR_Continue:
5901 case ESR_Failed:
5902 case ESR_Returned:
5903 return ESR;
5904 case ESR_CaseNotFound:
5905 // This can only happen if the switch case is nested within a statement
5906 // expression. We have no intention of supporting that.
5907 Info.FFDiag(Loc: Found->getBeginLoc(),
5908 DiagId: diag::note_constexpr_stmt_expr_unsupported);
5909 return ESR_Failed;
5910 }
5911 llvm_unreachable("Invalid EvalStmtResult!");
5912}
5913
5914static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) {
5915 // An expression E is a core constant expression unless the evaluation of E
5916 // would evaluate one of the following: [C++23] - a control flow that passes
5917 // through a declaration of a variable with static or thread storage duration
5918 // unless that variable is usable in constant expressions.
5919 if (VD->isLocalVarDecl() && VD->isStaticLocal() &&
5920 !VD->isUsableInConstantExpressions(C: Info.Ctx)) {
5921 Info.CCEDiag(Loc: VD->getLocation(), DiagId: diag::note_constexpr_static_local)
5922 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
5923 return false;
5924 }
5925 return true;
5926}
5927
5928// Evaluate a statement.
5929static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5930 const Stmt *S, const SwitchCase *Case) {
5931 if (!Info.nextStep(S))
5932 return ESR_Failed;
5933
5934 // If we're hunting down a 'case' or 'default' label, recurse through
5935 // substatements until we hit the label.
5936 if (Case) {
5937 switch (S->getStmtClass()) {
5938 case Stmt::CompoundStmtClass:
5939 // FIXME: Precompute which substatement of a compound statement we
5940 // would jump to, and go straight there rather than performing a
5941 // linear scan each time.
5942 case Stmt::LabelStmtClass:
5943 case Stmt::AttributedStmtClass:
5944 case Stmt::DoStmtClass:
5945 break;
5946
5947 case Stmt::CaseStmtClass:
5948 case Stmt::DefaultStmtClass:
5949 if (Case == S)
5950 Case = nullptr;
5951 break;
5952
5953 case Stmt::IfStmtClass: {
5954 // FIXME: Precompute which side of an 'if' we would jump to, and go
5955 // straight there rather than scanning both sides.
5956 const IfStmt *IS = cast<IfStmt>(Val: S);
5957
5958 // Wrap the evaluation in a block scope, in case it's a DeclStmt
5959 // preceded by our switch label.
5960 BlockScopeRAII Scope(Info);
5961
5962 // Step into the init statement in case it brings an (uninitialized)
5963 // variable into scope.
5964 if (const Stmt *Init = IS->getInit()) {
5965 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
5966 if (ESR != ESR_CaseNotFound) {
5967 assert(ESR != ESR_Succeeded);
5968 return ESR;
5969 }
5970 }
5971
5972 // Condition variable must be initialized if it exists.
5973 // FIXME: We can skip evaluating the body if there's a condition
5974 // variable, as there can't be any case labels within it.
5975 // (The same is true for 'for' statements.)
5976
5977 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case);
5978 if (ESR == ESR_Failed)
5979 return ESR;
5980 if (ESR != ESR_CaseNotFound)
5981 return Scope.destroy() ? ESR : ESR_Failed;
5982 if (!IS->getElse())
5983 return ESR_CaseNotFound;
5984
5985 ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case);
5986 if (ESR == ESR_Failed)
5987 return ESR;
5988 if (ESR != ESR_CaseNotFound)
5989 return Scope.destroy() ? ESR : ESR_Failed;
5990 return ESR_CaseNotFound;
5991 }
5992
5993 case Stmt::WhileStmtClass: {
5994 EvalStmtResult ESR =
5995 EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case);
5996 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: S, /*Scopes=*/{}, ESR))
5997 return ESR;
5998 if (ESR != ESR_Continue)
5999 return ESR;
6000 break;
6001 }
6002
6003 case Stmt::ForStmtClass: {
6004 const ForStmt *FS = cast<ForStmt>(Val: S);
6005 BlockScopeRAII Scope(Info);
6006
6007 // Step into the init statement in case it brings an (uninitialized)
6008 // variable into scope.
6009 if (const Stmt *Init = FS->getInit()) {
6010 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
6011 if (ESR != ESR_CaseNotFound) {
6012 assert(ESR != ESR_Succeeded);
6013 return ESR;
6014 }
6015 }
6016
6017 EvalStmtResult ESR =
6018 EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case);
6019 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, /*Scopes=*/{}, ESR))
6020 return ESR;
6021 if (ESR != ESR_Continue)
6022 return ESR;
6023 if (const auto *Inc = FS->getInc()) {
6024 if (Inc->isValueDependent()) {
6025 if (!EvaluateDependentExpr(E: Inc, Info))
6026 return ESR_Failed;
6027 } else {
6028 FullExpressionRAII IncScope(Info);
6029 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6030 return ESR_Failed;
6031 }
6032 }
6033 break;
6034 }
6035
6036 case Stmt::DeclStmtClass: {
6037 // Start the lifetime of any uninitialized variables we encounter. They
6038 // might be used by the selected branch of the switch.
6039 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6040 for (const auto *D : DS->decls()) {
6041 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
6042 if (!CheckLocalVariableDeclaration(Info, VD))
6043 return ESR_Failed;
6044 if (VD->hasLocalStorage() && !VD->getInit())
6045 if (!EvaluateVarDecl(Info, VD))
6046 return ESR_Failed;
6047 // FIXME: If the variable has initialization that can't be jumped
6048 // over, bail out of any immediately-surrounding compound-statement
6049 // too. There can't be any case labels here.
6050 }
6051 }
6052 return ESR_CaseNotFound;
6053 }
6054
6055 default:
6056 return ESR_CaseNotFound;
6057 }
6058 }
6059
6060 switch (S->getStmtClass()) {
6061 default:
6062 if (const Expr *E = dyn_cast<Expr>(Val: S)) {
6063 if (E->isValueDependent()) {
6064 if (!EvaluateDependentExpr(E, Info))
6065 return ESR_Failed;
6066 } else {
6067 // Don't bother evaluating beyond an expression-statement which couldn't
6068 // be evaluated.
6069 // FIXME: Do we need the FullExpressionRAII object here?
6070 // VisitExprWithCleanups should create one when necessary.
6071 FullExpressionRAII Scope(Info);
6072 if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy())
6073 return ESR_Failed;
6074 }
6075 return ESR_Succeeded;
6076 }
6077
6078 Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange();
6079 return ESR_Failed;
6080
6081 case Stmt::NullStmtClass:
6082 return ESR_Succeeded;
6083
6084 case Stmt::DeclStmtClass: {
6085 const DeclStmt *DS = cast<DeclStmt>(Val: S);
6086 for (const auto *D : DS->decls()) {
6087 const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D);
6088 if (VD && !CheckLocalVariableDeclaration(Info, VD))
6089 return ESR_Failed;
6090
6091 if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(Val: D)) {
6092 assert(ESD->getInstantiations() && "not expanded?");
6093 return EvaluateStmt(Result, Info, S: ESD->getInstantiations(), Case);
6094 }
6095
6096 // Each declaration initialization is its own full-expression.
6097 FullExpressionRAII Scope(Info);
6098 if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&
6099 !Info.noteFailure())
6100 return ESR_Failed;
6101 if (!Scope.destroy())
6102 return ESR_Failed;
6103 }
6104 return ESR_Succeeded;
6105 }
6106
6107 case Stmt::ReturnStmtClass: {
6108 const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue();
6109 FullExpressionRAII Scope(Info);
6110 if (RetExpr && RetExpr->isValueDependent()) {
6111 EvaluateDependentExpr(E: RetExpr, Info);
6112 // We know we returned, but we don't know what the value is.
6113 return ESR_Failed;
6114 }
6115 if (RetExpr &&
6116 !(Result.Slot
6117 ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr)
6118 : Evaluate(Result&: Result.Value, Info, E: RetExpr)))
6119 return ESR_Failed;
6120 return Scope.destroy() ? ESR_Returned : ESR_Failed;
6121 }
6122
6123 case Stmt::CompoundStmtClass: {
6124 BlockScopeRAII Scope(Info);
6125
6126 const CompoundStmt *CS = cast<CompoundStmt>(Val: S);
6127 for (const auto *BI : CS->body()) {
6128 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case);
6129 if (ESR == ESR_Succeeded)
6130 Case = nullptr;
6131 else if (ESR != ESR_CaseNotFound) {
6132 if (ESR != ESR_Failed && !Scope.destroy())
6133 return ESR_Failed;
6134 return ESR;
6135 }
6136 }
6137 if (Case)
6138 return ESR_CaseNotFound;
6139 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6140 }
6141
6142 case Stmt::IfStmtClass: {
6143 const IfStmt *IS = cast<IfStmt>(Val: S);
6144
6145 // Evaluate the condition, as either a var decl or as an expression.
6146 BlockScopeRAII Scope(Info);
6147 if (const Stmt *Init = IS->getInit()) {
6148 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
6149 if (ESR != ESR_Succeeded) {
6150 if (ESR != ESR_Failed && !Scope.destroy())
6151 return ESR_Failed;
6152 return ESR;
6153 }
6154 }
6155 bool Cond;
6156 if (IS->isConsteval()) {
6157 Cond = IS->isNonNegatedConsteval();
6158 // If we are not in a constant context, if consteval should not evaluate
6159 // to true.
6160 if (!Info.InConstantContext)
6161 Cond = !Cond;
6162 } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(),
6163 Result&: Cond))
6164 return ESR_Failed;
6165
6166 if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) {
6167 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt);
6168 if (ESR != ESR_Succeeded) {
6169 if (ESR != ESR_Failed && !Scope.destroy())
6170 return ESR_Failed;
6171 return ESR;
6172 }
6173 }
6174 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6175 }
6176
6177 case Stmt::WhileStmtClass: {
6178 const WhileStmt *WS = cast<WhileStmt>(Val: S);
6179 while (true) {
6180 BlockScopeRAII Scope(Info);
6181 bool Continue;
6182 if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(),
6183 Result&: Continue))
6184 return ESR_Failed;
6185 if (!Continue)
6186 break;
6187
6188 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody());
6189 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: WS, Scopes: &Scope, ESR))
6190 return ESR;
6191
6192 if (ESR != ESR_Continue) {
6193 if (ESR != ESR_Failed && !Scope.destroy())
6194 return ESR_Failed;
6195 return ESR;
6196 }
6197 if (!Scope.destroy())
6198 return ESR_Failed;
6199 }
6200 return ESR_Succeeded;
6201 }
6202
6203 case Stmt::DoStmtClass: {
6204 const DoStmt *DS = cast<DoStmt>(Val: S);
6205 bool Continue;
6206 do {
6207 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case);
6208 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: DS, /*Scopes=*/{}, ESR))
6209 return ESR;
6210 if (ESR != ESR_Continue)
6211 return ESR;
6212 Case = nullptr;
6213
6214 if (DS->getCond()->isValueDependent()) {
6215 EvaluateDependentExpr(E: DS->getCond(), Info);
6216 // Bailout as we don't know whether to keep going or terminate the loop.
6217 return ESR_Failed;
6218 }
6219 FullExpressionRAII CondScope(Info);
6220 if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) ||
6221 !CondScope.destroy())
6222 return ESR_Failed;
6223 } while (Continue);
6224 return ESR_Succeeded;
6225 }
6226
6227 case Stmt::ForStmtClass: {
6228 const ForStmt *FS = cast<ForStmt>(Val: S);
6229 BlockScopeRAII ForScope(Info);
6230 if (FS->getInit()) {
6231 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6232 if (ESR != ESR_Succeeded) {
6233 if (ESR != ESR_Failed && !ForScope.destroy())
6234 return ESR_Failed;
6235 return ESR;
6236 }
6237 }
6238 while (true) {
6239 BlockScopeRAII IterScope(Info);
6240 bool Continue = true;
6241 if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(),
6242 Cond: FS->getCond(), Result&: Continue))
6243 return ESR_Failed;
6244
6245 if (!Continue) {
6246 if (!IterScope.destroy())
6247 return ESR_Failed;
6248 break;
6249 }
6250
6251 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6252 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&IterScope, &ForScope}, ESR))
6253 return ESR;
6254 if (ESR != ESR_Continue) {
6255 if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy()))
6256 return ESR_Failed;
6257 return ESR;
6258 }
6259
6260 if (const auto *Inc = FS->getInc()) {
6261 if (Inc->isValueDependent()) {
6262 if (!EvaluateDependentExpr(E: Inc, Info))
6263 return ESR_Failed;
6264 } else {
6265 FullExpressionRAII IncScope(Info);
6266 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6267 return ESR_Failed;
6268 }
6269 }
6270
6271 if (!IterScope.destroy())
6272 return ESR_Failed;
6273 }
6274 return ForScope.destroy() ? ESR_Succeeded : ESR_Failed;
6275 }
6276
6277 case Stmt::CXXForRangeStmtClass: {
6278 const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S);
6279 BlockScopeRAII Scope(Info);
6280
6281 // Evaluate the init-statement if present.
6282 if (FS->getInit()) {
6283 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6284 if (ESR != ESR_Succeeded) {
6285 if (ESR != ESR_Failed && !Scope.destroy())
6286 return ESR_Failed;
6287 return ESR;
6288 }
6289 }
6290
6291 // Initialize the __range variable.
6292 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt());
6293 if (ESR != ESR_Succeeded) {
6294 if (ESR != ESR_Failed && !Scope.destroy())
6295 return ESR_Failed;
6296 return ESR;
6297 }
6298
6299 // In error-recovery cases it's possible to get here even if we failed to
6300 // synthesize the __begin and __end variables.
6301 if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())
6302 return ESR_Failed;
6303
6304 // Create the __begin and __end iterators.
6305 ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt());
6306 if (ESR != ESR_Succeeded) {
6307 if (ESR != ESR_Failed && !Scope.destroy())
6308 return ESR_Failed;
6309 return ESR;
6310 }
6311 ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt());
6312 if (ESR != ESR_Succeeded) {
6313 if (ESR != ESR_Failed && !Scope.destroy())
6314 return ESR_Failed;
6315 return ESR;
6316 }
6317
6318 while (true) {
6319 // Condition: __begin != __end.
6320 {
6321 if (FS->getCond()->isValueDependent()) {
6322 EvaluateDependentExpr(E: FS->getCond(), Info);
6323 // We don't know whether to keep going or terminate the loop.
6324 return ESR_Failed;
6325 }
6326 bool Continue = true;
6327 FullExpressionRAII CondExpr(Info);
6328 if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info))
6329 return ESR_Failed;
6330 if (!Continue)
6331 break;
6332 }
6333
6334 // User's variable declaration, initialized by *__begin.
6335 BlockScopeRAII InnerScope(Info);
6336 ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt());
6337 if (ESR != ESR_Succeeded) {
6338 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6339 return ESR_Failed;
6340 return ESR;
6341 }
6342
6343 // Loop body.
6344 ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6345 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&InnerScope, &Scope}, ESR))
6346 return ESR;
6347 if (ESR != ESR_Continue) {
6348 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6349 return ESR_Failed;
6350 return ESR;
6351 }
6352 if (FS->getInc()->isValueDependent()) {
6353 if (!EvaluateDependentExpr(E: FS->getInc(), Info))
6354 return ESR_Failed;
6355 } else {
6356 // Increment: ++__begin
6357 if (!EvaluateIgnoredValue(Info, E: FS->getInc()))
6358 return ESR_Failed;
6359 }
6360
6361 if (!InnerScope.destroy())
6362 return ESR_Failed;
6363 }
6364
6365 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6366 }
6367
6368 case Stmt::CXXExpansionStmtInstantiationClass: {
6369 BlockScopeRAII Scope(Info);
6370 const auto *Expansion = cast<CXXExpansionStmtInstantiation>(Val: S);
6371 for (const Stmt *PreambleStmt : Expansion->getPreambleStmts()) {
6372 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: PreambleStmt);
6373 if (ESR != ESR_Succeeded) {
6374 if (ESR != ESR_Failed && !Scope.destroy())
6375 return ESR_Failed;
6376 return ESR;
6377 }
6378 }
6379
6380 // No need to push an extra scope for these since they're already
6381 // CompoundStmts.
6382 EvalStmtResult ESR = ESR_Succeeded;
6383 for (const Stmt *Instantiation : Expansion->getInstantiations()) {
6384 ESR = EvaluateStmt(Result, Info, S: Instantiation);
6385 if (ESR == ESR_Failed ||
6386 ShouldPropagateBreakContinue(Info, LoopOrSwitch: Expansion, Scopes: &Scope, ESR))
6387 return ESR;
6388 if (ESR != ESR_Continue) {
6389 // Succeeded here actually means we encountered a 'break'.
6390 assert(ESR == ESR_Succeeded || ESR == ESR_Returned);
6391 break;
6392 }
6393 }
6394
6395 // Map Continue back to Succeeded if we fell off the end of the loop.
6396 if (ESR == ESR_Continue)
6397 ESR = ESR_Succeeded;
6398
6399 return Scope.destroy() ? ESR : ESR_Failed;
6400 }
6401
6402 case Stmt::SwitchStmtClass:
6403 return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S));
6404
6405 case Stmt::ContinueStmtClass:
6406 case Stmt::BreakStmtClass: {
6407 auto *B = cast<LoopControlStmt>(Val: S);
6408 Info.BreakContinueStack.push_back(Elt: B->getNamedLoopOrSwitch());
6409 return isa<ContinueStmt>(Val: S) ? ESR_Continue : ESR_Break;
6410 }
6411
6412 case Stmt::LabelStmtClass:
6413 return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case);
6414
6415 case Stmt::AttributedStmtClass: {
6416 const auto *AS = cast<AttributedStmt>(Val: S);
6417 const auto *SS = AS->getSubStmt();
6418 MSConstexprContextRAII ConstexprContext(
6419 *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(container: AS->getAttrs()) &&
6420 isa<ReturnStmt>(Val: SS));
6421
6422 auto LO = Info.Ctx.getLangOpts();
6423 if (LO.CXXAssumptions && !LO.MSVCCompat) {
6424 for (auto *Attr : AS->getAttrs()) {
6425 auto *AA = dyn_cast<CXXAssumeAttr>(Val: Attr);
6426 if (!AA)
6427 continue;
6428
6429 auto *Assumption = AA->getAssumption();
6430 if (Assumption->isValueDependent())
6431 return ESR_Failed;
6432
6433 if (Assumption->HasSideEffects(Ctx: Info.Ctx))
6434 continue;
6435
6436 bool Value;
6437 if (!EvaluateAsBooleanCondition(E: Assumption, Result&: Value, Info))
6438 return ESR_Failed;
6439 if (!Value) {
6440 Info.CCEDiag(Loc: Assumption->getExprLoc(),
6441 DiagId: diag::note_constexpr_assumption_failed);
6442 return ESR_Failed;
6443 }
6444 }
6445 }
6446
6447 return EvaluateStmt(Result, Info, S: SS, Case);
6448 }
6449
6450 case Stmt::CaseStmtClass:
6451 case Stmt::DefaultStmtClass:
6452 return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case);
6453 case Stmt::CXXTryStmtClass:
6454 // Evaluate try blocks by evaluating all sub statements.
6455 return EvaluateStmt(Result, Info, S: cast<CXXTryStmt>(Val: S)->getTryBlock(), Case);
6456 }
6457}
6458
6459/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
6460/// default constructor. If so, we'll fold it whether or not it's marked as
6461/// constexpr. If it is marked as constexpr, we will never implicitly define it,
6462/// so we need special handling.
6463static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
6464 const CXXConstructorDecl *CD,
6465 bool IsValueInitialization) {
6466 if (!CD->isTrivial() || !CD->isDefaultConstructor())
6467 return false;
6468
6469 // Value-initialization does not call a trivial default constructor, so such a
6470 // call is a core constant expression whether or not the constructor is
6471 // constexpr.
6472 if (!CD->isConstexpr() && !IsValueInitialization) {
6473 if (Info.getLangOpts().CPlusPlus11) {
6474 // FIXME: If DiagDecl is an implicitly-declared special member function,
6475 // we should be much more explicit about why it's not constexpr.
6476 Info.CCEDiag(Loc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6477 << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
6478 Info.Note(Loc: CD->getLocation(), DiagId: diag::note_declared_at);
6479 } else {
6480 Info.CCEDiag(Loc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6481 }
6482 }
6483 return true;
6484}
6485
6486/// CheckConstexprFunction - Check that a function can be called in a constant
6487/// expression.
6488static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
6489 const FunctionDecl *Declaration,
6490 const FunctionDecl *Definition,
6491 const Stmt *Body) {
6492 // Potential constant expressions can contain calls to declared, but not yet
6493 // defined, constexpr functions.
6494 if (Info.checkingPotentialConstantExpression() && !Definition &&
6495 Declaration->isConstexpr())
6496 return false;
6497
6498 // Bail out if the function declaration itself is invalid. We will
6499 // have produced a relevant diagnostic while parsing it, so just
6500 // note the problematic sub-expression.
6501 if (Declaration->isInvalidDecl()) {
6502 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6503 return false;
6504 }
6505
6506 // DR1872: An instantiated virtual constexpr function can't be called in a
6507 // constant expression (prior to C++20). We can still constant-fold such a
6508 // call.
6509 if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Val: Declaration) &&
6510 cast<CXXMethodDecl>(Val: Declaration)->isVirtual())
6511 Info.CCEDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_call);
6512
6513 if (Definition && Definition->isInvalidDecl()) {
6514 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6515 return false;
6516 }
6517
6518 // Can we evaluate this function call?
6519 if (Definition && Body &&
6520 (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr &&
6521 Definition->hasAttr<MSConstexprAttr>())))
6522 return true;
6523
6524 const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
6525 // Special note for the assert() macro, as the normal error message falsely
6526 // implies we cannot use an assertion during constant evaluation.
6527 if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) {
6528 // FIXME: Instead of checking for an implementation-defined function,
6529 // check and evaluate the assert() macro.
6530 StringRef Name = DiagDecl->getName();
6531 bool AssertFailed =
6532 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
6533 if (AssertFailed) {
6534 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_assert_failed);
6535 return false;
6536 }
6537 }
6538
6539 if (Info.getLangOpts().CPlusPlus11) {
6540 // If this function is not constexpr because it is an inherited
6541 // non-constexpr constructor, diagnose that directly.
6542 auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl);
6543 if (CD && CD->isInheritingConstructor()) {
6544 auto *Inherited = CD->getInheritedConstructor().getConstructor();
6545 if (!Inherited->isConstexpr())
6546 DiagDecl = CD = Inherited;
6547 }
6548
6549 // FIXME: If DiagDecl is an implicitly-declared special member function
6550 // or an inheriting constructor, we should be much more explicit about why
6551 // it's not constexpr.
6552 if (CD && CD->isInheritingConstructor())
6553 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_inhctor, ExtraNotes: 1)
6554 << CD->getInheritedConstructor().getConstructor()->getParent();
6555 else
6556 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6557 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
6558 Info.Note(Loc: DiagDecl->getLocation(), DiagId: diag::note_declared_at);
6559 } else {
6560 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6561 }
6562 return false;
6563}
6564
6565namespace {
6566struct CheckDynamicTypeHandler {
6567 AccessKinds AccessKind;
6568 typedef bool result_type;
6569 bool failed() { return false; }
6570 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6571 return true;
6572 }
6573 bool found(APSInt &Value, QualType SubobjType) { return true; }
6574 bool found(APFloat &Value, QualType SubobjType) { return true; }
6575};
6576} // end anonymous namespace
6577
6578/// Check that we can access the notional vptr of an object / determine its
6579/// dynamic type.
6580static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,
6581 AccessKinds AK, bool Polymorphic) {
6582 if (This.Designator.Invalid)
6583 return false;
6584
6585 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType());
6586
6587 if (!Obj)
6588 return false;
6589
6590 if (!Obj.Value) {
6591 // The object is not usable in constant expressions, so we can't inspect
6592 // its value to see if it's in-lifetime or what the active union members
6593 // are. We can still check for a one-past-the-end lvalue.
6594 if (This.Designator.isOnePastTheEnd() ||
6595 This.Designator.isMostDerivedAnUnsizedArray()) {
6596 Info.FFDiag(E, DiagId: This.Designator.isOnePastTheEnd()
6597 ? diag::note_constexpr_access_past_end
6598 : diag::note_constexpr_access_unsized_array)
6599 << AK;
6600 return false;
6601 } else if (Polymorphic) {
6602 // Conservatively refuse to perform a polymorphic operation if we would
6603 // not be able to read a notional 'vptr' value.
6604 if (!Info.checkingPotentialConstantExpression() ||
6605 !This.AllowConstexprUnknown) {
6606 APValue Val;
6607 This.moveInto(V&: Val);
6608 QualType StarThisType =
6609 Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Ctx&: Info.Ctx));
6610 Info.FFDiag(E, DiagId: diag::note_constexpr_polymorphic_unknown_dynamic_type)
6611 << AK << Val.getAsString(Ctx: Info.Ctx, Ty: StarThisType);
6612 }
6613 return false;
6614 }
6615 return true;
6616 }
6617
6618 CheckDynamicTypeHandler Handler{.AccessKind: AK};
6619 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
6620}
6621
6622/// Check that the pointee of the 'this' pointer in a member function call is
6623/// either within its lifetime or in its period of construction or destruction.
6624static bool
6625checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E,
6626 const LValue &This,
6627 const CXXMethodDecl *NamedMember) {
6628 return checkDynamicType(
6629 Info, E, This,
6630 AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false);
6631}
6632
6633struct DynamicType {
6634 /// The dynamic class type of the object.
6635 const CXXRecordDecl *Type;
6636 /// The corresponding path length in the lvalue.
6637 unsigned PathLength;
6638};
6639
6640static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator,
6641 unsigned PathLength) {
6642 assert(PathLength >= Designator.MostDerivedPathLength && PathLength <=
6643 Designator.Entries.size() && "invalid path length");
6644 return (PathLength == Designator.MostDerivedPathLength)
6645 ? Designator.MostDerivedType->getAsCXXRecordDecl()
6646 : getAsBaseClass(E: Designator.Entries[PathLength - 1]);
6647}
6648
6649/// Determine the dynamic type of an object.
6650static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info,
6651 const Expr *E,
6652 LValue &This,
6653 AccessKinds AK) {
6654 // If we don't have an lvalue denoting an object of class type, there is no
6655 // meaningful dynamic type. (We consider objects of non-class type to have no
6656 // dynamic type.)
6657 if (!checkDynamicType(Info, E, This, AK,
6658 Polymorphic: AK != AK_TypeId || This.AllowConstexprUnknown))
6659 return std::nullopt;
6660
6661 if (This.Designator.Invalid)
6662 return std::nullopt;
6663
6664 // Refuse to compute a dynamic type in the presence of virtual bases
6665 // before C++26. This shouldn't happen other than in constant-folding
6666 // situations, since literal types can't have virtual bases.
6667 const CXXRecordDecl *Class =
6668 This.Designator.MostDerivedType->getAsCXXRecordDecl();
6669 if (!Class || (!Info.getLangOpts().CPlusPlus26 && Class->getNumVBases())) {
6670 Info.FFDiag(E);
6671 return std::nullopt;
6672 }
6673
6674 // FIXME: For very deep class hierarchies, it might be beneficial to use a
6675 // binary search here instead. But the overwhelmingly common case is that
6676 // we're not in the middle of a constructor, so it probably doesn't matter
6677 // in practice.
6678 ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries;
6679 for (unsigned PathLength = This.Designator.MostDerivedPathLength;
6680 PathLength <= Path.size(); ++PathLength) {
6681 switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(),
6682 Path: Path.slice(N: 0, M: PathLength))) {
6683 case ConstructionPhase::Bases:
6684 case ConstructionPhase::DestroyingBases:
6685 // We're constructing or destroying a base class. This is not the dynamic
6686 // type.
6687 break;
6688
6689 case ConstructionPhase::None:
6690 case ConstructionPhase::AfterBases:
6691 case ConstructionPhase::AfterFields:
6692 case ConstructionPhase::Destroying:
6693 // We've finished constructing the base classes and not yet started
6694 // destroying them again, so this is the dynamic type.
6695 return DynamicType{.Type: getBaseClassType(Designator&: This.Designator, PathLength),
6696 .PathLength: PathLength};
6697 }
6698 }
6699
6700 // CWG issue 1517: we're constructing a base class of the object described by
6701 // 'This', so that object has not yet begun its period of construction and
6702 // any polymorphic operation on it results in undefined behavior.
6703 Info.FFDiag(E);
6704 return std::nullopt;
6705}
6706
6707/// Perform virtual dispatch.
6708static const CXXMethodDecl *HandleVirtualDispatch(
6709 EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found,
6710 llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) {
6711 std::optional<DynamicType> DynType = ComputeDynamicType(
6712 Info, E, This,
6713 AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall);
6714 if (!DynType)
6715 return nullptr;
6716
6717 // Find the final overrider. It must be declared in one of the classes on the
6718 // path from the dynamic type to the static type.
6719 // FIXME: If we ever allow literal types to have virtual base classes, that
6720 // won't be true.
6721 const CXXMethodDecl *Callee = Found;
6722 unsigned PathLength = DynType->PathLength;
6723 for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) {
6724 const CXXRecordDecl *Class = getBaseClassType(Designator&: This.Designator, PathLength);
6725 const CXXMethodDecl *Overrider =
6726 Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false);
6727 if (Overrider) {
6728 Callee = Overrider;
6729 break;
6730 }
6731 }
6732
6733 // C++2a [class.abstract]p6:
6734 // the effect of making a virtual call to a pure virtual function [...] is
6735 // undefined
6736 if (Callee->isPureVirtual()) {
6737 Info.FFDiag(E, DiagId: diag::note_constexpr_pure_virtual_call, ExtraNotes: 1) << Callee;
6738 Info.Note(Loc: Callee->getLocation(), DiagId: diag::note_declared_at);
6739 return nullptr;
6740 }
6741
6742 // If necessary, walk the rest of the path to determine the sequence of
6743 // covariant adjustment steps to apply.
6744 if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(),
6745 T2: Found->getReturnType())) {
6746 CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType());
6747 for (unsigned CovariantPathLength = PathLength + 1;
6748 CovariantPathLength != This.Designator.Entries.size();
6749 ++CovariantPathLength) {
6750 const CXXRecordDecl *NextClass =
6751 getBaseClassType(Designator&: This.Designator, PathLength: CovariantPathLength);
6752 const CXXMethodDecl *Next =
6753 Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false);
6754 if (Next && !Info.Ctx.hasSameUnqualifiedType(
6755 T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back()))
6756 CovariantAdjustmentPath.push_back(Elt: Next->getReturnType());
6757 }
6758 if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(),
6759 T2: CovariantAdjustmentPath.back()))
6760 CovariantAdjustmentPath.push_back(Elt: Found->getReturnType());
6761 }
6762
6763 // Perform 'this' adjustment.
6764 if (!CastToDerivedClass(Info, E, Result&: This, TruncatedType: Callee->getParent(), TruncatedElements: PathLength))
6765 return nullptr;
6766
6767 return Callee;
6768}
6769
6770/// Perform the adjustment from a value returned by a virtual function to
6771/// a value of the statically expected type, which may be a pointer or
6772/// reference to a base class of the returned type.
6773static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E,
6774 APValue &Result,
6775 ArrayRef<QualType> Path) {
6776 assert(Result.isLValue() &&
6777 "unexpected kind of APValue for covariant return");
6778 if (Result.isNullPointer())
6779 return true;
6780
6781 LValue LVal;
6782 LVal.setFrom(Ctx: Info.Ctx, V: Result);
6783
6784 const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl();
6785 for (unsigned I = 1; I != Path.size(); ++I) {
6786 const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl();
6787 assert(OldClass && NewClass && "unexpected kind of covariant return");
6788 if (OldClass != NewClass &&
6789 !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass))
6790 return false;
6791 OldClass = NewClass;
6792 }
6793
6794 LVal.moveInto(V&: Result);
6795 return true;
6796}
6797
6798/// Determine whether \p Base, which is known to be a direct base class of
6799/// \p Derived, is a public base class.
6800static bool isBaseClassPublic(const CXXRecordDecl *Derived,
6801 const CXXRecordDecl *Base) {
6802 for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) {
6803 if (BaseSpec.isVirtual())
6804 continue;
6805 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6806 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6807 return BaseSpec.getAccessSpecifier() == AS_public;
6808 }
6809 for (const CXXBaseSpecifier &BaseSpec : Derived->vbases()) {
6810 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6811 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6812 return BaseSpec.getAccessSpecifier() == AS_public;
6813 }
6814
6815 llvm_unreachable("Base is not a direct base of Derived");
6816}
6817
6818/// Apply the given dynamic cast operation on the provided lvalue.
6819///
6820/// This implements the hard case of dynamic_cast, requiring a "runtime check"
6821/// to find a suitable target subobject.
6822static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E,
6823 LValue &Ptr) {
6824 // We can't do anything with a non-symbolic pointer value.
6825 SubobjectDesignator &D = Ptr.Designator;
6826 if (D.Invalid)
6827 return false;
6828
6829 // C++ [expr.dynamic.cast]p6:
6830 // If v is a null pointer value, the result is a null pointer value.
6831 if (Ptr.isNullPointer() && !E->isGLValue())
6832 return true;
6833
6834 // For all the other cases, we need the pointer to point to an object within
6835 // its lifetime / period of construction / destruction, and we need to know
6836 // its dynamic type.
6837 std::optional<DynamicType> DynType =
6838 ComputeDynamicType(Info, E, This&: Ptr, AK: AK_DynamicCast);
6839 if (!DynType)
6840 return false;
6841
6842 // C++ [expr.dynamic.cast]p7:
6843 // If T is "pointer to cv void", then the result is a pointer to the most
6844 // derived object
6845 if (E->getType()->isVoidPointerType())
6846 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength);
6847
6848 const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl();
6849 assert(C && "dynamic_cast target is not void pointer nor class");
6850 CanQualType CQT = Info.Ctx.getCanonicalTagType(TD: C);
6851
6852 auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) {
6853 // C++ [expr.dynamic.cast]p9:
6854 if (!E->isGLValue()) {
6855 // The value of a failed cast to pointer type is the null pointer value
6856 // of the required result type.
6857 Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
6858 return true;
6859 }
6860
6861 // A failed cast to reference type throws [...] std::bad_cast.
6862 unsigned DiagKind;
6863 if (!Paths && (declaresSameEntity(D1: DynType->Type, D2: C) ||
6864 DynType->Type->isDerivedFrom(Base: C)))
6865 DiagKind = 0;
6866 else if (!Paths || Paths->begin() == Paths->end())
6867 DiagKind = 1;
6868 else if (Paths->isAmbiguous(BaseType: CQT))
6869 DiagKind = 2;
6870 else {
6871 assert(Paths->front().Access != AS_public && "why did the cast fail?");
6872 DiagKind = 3;
6873 }
6874 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_cast_to_reference_failed)
6875 << DiagKind << Ptr.Designator.getType(Ctx&: Info.Ctx)
6876 << Info.Ctx.getCanonicalTagType(TD: DynType->Type)
6877 << E->getType().getUnqualifiedType();
6878 return false;
6879 };
6880
6881 // Runtime check, phase 1:
6882 // Walk from the base subobject towards the derived object looking for the
6883 // target type.
6884 for (int PathLength = Ptr.Designator.Entries.size();
6885 PathLength >= (int)DynType->PathLength; --PathLength) {
6886 const CXXRecordDecl *Class = getBaseClassType(Designator&: Ptr.Designator, PathLength);
6887 if (declaresSameEntity(D1: Class, D2: C))
6888 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: Class, TruncatedElements: PathLength);
6889 // We can only walk across public inheritance edges.
6890 if (PathLength > (int)DynType->PathLength &&
6891 !isBaseClassPublic(Derived: getBaseClassType(Designator&: Ptr.Designator, PathLength: PathLength - 1),
6892 Base: Class))
6893 return RuntimeCheckFailed(nullptr);
6894 }
6895
6896 // Runtime check, phase 2:
6897 // Search the dynamic type for an unambiguous public base of type C.
6898 CXXBasePaths Paths(/*FindAmbiguities=*/true,
6899 /*RecordPaths=*/true, /*DetectVirtual=*/false);
6900 if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) &&
6901 Paths.front().Access == AS_public) {
6902 // Downcast to the dynamic type...
6903 if (!CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength))
6904 return false;
6905 // ... then upcast to the chosen base class subobject.
6906 for (CXXBasePathElement &Elem : Paths.front())
6907 if (!HandleLValueBase(Info, E, Obj&: Ptr, DerivedDecl: Elem.Class, Base: Elem.Base))
6908 return false;
6909 return true;
6910 }
6911
6912 // Otherwise, the runtime check fails.
6913 return RuntimeCheckFailed(&Paths);
6914}
6915
6916namespace {
6917struct StartLifetimeOfUnionMemberHandler {
6918 EvalInfo &Info;
6919 const Expr *LHSExpr;
6920 const FieldDecl *Field;
6921 bool DuringInit;
6922 bool Failed = false;
6923 static const AccessKinds AccessKind = AK_Assign;
6924
6925 typedef bool result_type;
6926 bool failed() { return Failed; }
6927 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6928 // We are supposed to perform no initialization but begin the lifetime of
6929 // the object. We interpret that as meaning to do what default
6930 // initialization of the object would do if all constructors involved were
6931 // trivial:
6932 // * All base, non-variant member, and array element subobjects' lifetimes
6933 // begin
6934 // * No variant members' lifetimes begin
6935 // * All scalar subobjects whose lifetimes begin have indeterminate values
6936 assert(SubobjType->isUnionType());
6937 if (declaresSameEntity(D1: Subobj.getUnionField(), D2: Field)) {
6938 // This union member is already active. If it's also in-lifetime, there's
6939 // nothing to do.
6940 if (Subobj.getUnionValue().hasValue())
6941 return true;
6942 } else if (DuringInit) {
6943 // We're currently in the process of initializing a different union
6944 // member. If we carried on, that initialization would attempt to
6945 // store to an inactive union member, resulting in undefined behavior.
6946 Info.FFDiag(E: LHSExpr,
6947 DiagId: diag::note_constexpr_union_member_change_during_init);
6948 return false;
6949 }
6950 APValue Result;
6951 Failed = !handleDefaultInitValue(T: Field->getType(), Result);
6952 Subobj.setUnion(Field, Value: Result);
6953 return true;
6954 }
6955 bool found(APSInt &Value, QualType SubobjType) {
6956 llvm_unreachable("wrong value kind for union object");
6957 }
6958 bool found(APFloat &Value, QualType SubobjType) {
6959 llvm_unreachable("wrong value kind for union object");
6960 }
6961};
6962} // end anonymous namespace
6963
6964const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind;
6965
6966/// Handle a builtin simple-assignment or a call to a trivial assignment
6967/// operator whose left-hand side might involve a union member access. If it
6968/// does, implicitly start the lifetime of any accessed union elements per
6969/// C++20 [class.union]5.
6970static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info,
6971 const Expr *LHSExpr,
6972 const LValue &LHS) {
6973 if (LHS.InvalidBase || LHS.Designator.Invalid)
6974 return false;
6975
6976 llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths;
6977 // C++ [class.union]p5:
6978 // define the set S(E) of subexpressions of E as follows:
6979 unsigned PathLength = LHS.Designator.Entries.size();
6980 for (const Expr *E = LHSExpr; E != nullptr;) {
6981 // -- If E is of the form A.B, S(E) contains the elements of S(A)...
6982 if (auto *ME = dyn_cast<MemberExpr>(Val: E)) {
6983 auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
6984 // Note that we can't implicitly start the lifetime of a reference,
6985 // so we don't need to proceed any further if we reach one.
6986 if (!FD || FD->getType()->isReferenceType())
6987 break;
6988
6989 // ... and also contains A.B if B names a union member ...
6990 if (FD->getParent()->isUnion()) {
6991 // ... of a non-class, non-array type, or of a class type with a
6992 // trivial default constructor that is not deleted, or an array of
6993 // such types.
6994 auto *RD =
6995 FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
6996 if (!RD || RD->hasTrivialDefaultConstructor())
6997 UnionPathLengths.push_back(Elt: {PathLength - 1, FD});
6998 }
6999
7000 E = ME->getBase();
7001 --PathLength;
7002 assert(declaresSameEntity(FD,
7003 LHS.Designator.Entries[PathLength]
7004 .getAsBaseOrMember().getPointer()));
7005
7006 // -- If E is of the form A[B] and is interpreted as a built-in array
7007 // subscripting operator, S(E) is [S(the array operand, if any)].
7008 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) {
7009 // Step over an ArrayToPointerDecay implicit cast.
7010 auto *Base = ASE->getBase()->IgnoreImplicit();
7011 if (!Base->getType()->isArrayType())
7012 break;
7013
7014 E = Base;
7015 --PathLength;
7016
7017 } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) {
7018 // Step over a derived-to-base conversion.
7019 E = ICE->getSubExpr();
7020 if (ICE->getCastKind() == CK_NoOp)
7021 continue;
7022 if (ICE->getCastKind() != CK_DerivedToBase &&
7023 ICE->getCastKind() != CK_UncheckedDerivedToBase)
7024 break;
7025 // Walk path backwards as we walk up from the base to the derived class.
7026 for (const CXXBaseSpecifier *Elt : llvm::reverse(C: ICE->path())) {
7027 if (Elt->isVirtual()) {
7028 // A class with virtual base classes never has a trivial default
7029 // constructor, so S(E) is empty in this case.
7030 E = nullptr;
7031 break;
7032 }
7033
7034 --PathLength;
7035 assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(),
7036 LHS.Designator.Entries[PathLength]
7037 .getAsBaseOrMember().getPointer()));
7038 }
7039
7040 // -- Otherwise, S(E) is empty.
7041 } else {
7042 break;
7043 }
7044 }
7045
7046 // Common case: no unions' lifetimes are started.
7047 if (UnionPathLengths.empty())
7048 return true;
7049
7050 // if modification of X [would access an inactive union member], an object
7051 // of the type of X is implicitly created
7052 CompleteObject Obj =
7053 findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType());
7054 if (!Obj)
7055 return false;
7056 for (std::pair<unsigned, const FieldDecl *> LengthAndField :
7057 llvm::reverse(C&: UnionPathLengths)) {
7058 // Form a designator for the union object.
7059 SubobjectDesignator D = LHS.Designator;
7060 D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first);
7061
7062 bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) ==
7063 ConstructionPhase::AfterBases;
7064 StartLifetimeOfUnionMemberHandler StartLifetime{
7065 .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit};
7066 if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime))
7067 return false;
7068 }
7069
7070 return true;
7071}
7072
7073static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg,
7074 CallRef Call, EvalInfo &Info, bool NonNull = false,
7075 APValue **EvaluatedArg = nullptr) {
7076 LValue LV;
7077 // Create the parameter slot and register its destruction. For a vararg
7078 // argument, create a temporary.
7079 // FIXME: For calling conventions that destroy parameters in the callee,
7080 // should we consider performing destruction when the function returns
7081 // instead?
7082 APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV)
7083 : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(),
7084 Scope: ScopeKind::Call, LV);
7085 if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg))
7086 return false;
7087
7088 // Passing a null pointer to an __attribute__((nonnull)) parameter results in
7089 // undefined behavior, so is non-constant.
7090 if (NonNull && V.isLValue() && V.isNullPointer()) {
7091 Info.CCEDiag(E: Arg, DiagId: diag::note_non_null_attribute_failed);
7092 return false;
7093 }
7094
7095 if (EvaluatedArg)
7096 *EvaluatedArg = &V;
7097
7098 return true;
7099}
7100
7101/// Evaluate the arguments to a function call.
7102static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call,
7103 EvalInfo &Info, const FunctionDecl *Callee,
7104 bool RightToLeft = false,
7105 LValue *ObjectArg = nullptr) {
7106 bool Success = true;
7107 llvm::SmallBitVector ForbiddenNullArgs;
7108 if (Callee->hasAttr<NonNullAttr>()) {
7109 ForbiddenNullArgs.resize(N: Args.size());
7110 for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) {
7111 if (!Attr->args_size()) {
7112 ForbiddenNullArgs.set();
7113 break;
7114 } else
7115 for (auto Idx : Attr->args()) {
7116 unsigned ASTIdx = Idx.getASTIndex();
7117 if (ASTIdx >= Args.size())
7118 continue;
7119 ForbiddenNullArgs[ASTIdx] = true;
7120 }
7121 }
7122 }
7123 for (unsigned I = 0; I < Args.size(); I++) {
7124 unsigned Idx = RightToLeft ? Args.size() - I - 1 : I;
7125 const ParmVarDecl *PVD =
7126 Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr;
7127 bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx];
7128 APValue *That = nullptr;
7129 if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull, EvaluatedArg: &That)) {
7130 // If we're checking for a potential constant expression, evaluate all
7131 // initializers even if some of them fail.
7132 if (!Info.noteFailure())
7133 return false;
7134 Success = false;
7135 }
7136 if (PVD && PVD->isExplicitObjectParameter() && That && That->isLValue())
7137 ObjectArg->setFrom(Ctx: Info.Ctx, V: *That);
7138 }
7139 return Success;
7140}
7141
7142/// Perform a trivial copy from Param, which is the parameter of a copy or move
7143/// constructor or assignment operator.
7144static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param,
7145 const Expr *E, APValue &Result,
7146 bool CopyObjectRepresentation) {
7147 // Find the reference argument.
7148 CallStackFrame *Frame = Info.CurrentCall;
7149 APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param);
7150 if (!RefValue) {
7151 Info.FFDiag(E);
7152 return false;
7153 }
7154
7155 // Copy out the contents of the RHS object.
7156 LValue RefLValue;
7157 RefLValue.setFrom(Ctx: Info.Ctx, V: *RefValue);
7158 return handleLValueToRValueConversion(
7159 Info, Conv: E, Type: Param->getType().getNonReferenceType(), LVal: RefLValue, RVal&: Result,
7160 WantObjectRepresentation: CopyObjectRepresentation);
7161}
7162
7163/// Evaluate a function call.
7164static bool HandleFunctionCall(SourceLocation CallLoc,
7165 const FunctionDecl *Callee,
7166 const LValue *ObjectArg, const Expr *E,
7167 ArrayRef<const Expr *> Args, CallRef Call,
7168 const Stmt *Body, EvalInfo &Info,
7169 APValue &Result, const LValue *ResultSlot) {
7170 if (!Info.CheckCallLimit(Loc: CallLoc))
7171 return false;
7172
7173 CallStackFrame Frame(Info, E->getSourceRange(), Callee, ObjectArg, E, Call);
7174
7175 // For a trivial copy or move assignment, perform an APValue copy. This is
7176 // essential for unions, where the operations performed by the assignment
7177 // operator cannot be represented as statements.
7178 //
7179 // Skip this for non-union classes with no fields; in that case, the defaulted
7180 // copy/move does not actually read the object.
7181 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee);
7182
7183 auto IsTrivialMemoryOperation = [&](const CXXMethodDecl *MD) {
7184 if (!MD || !MD->isDefaulted())
7185 return false;
7186 if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator())
7187 return false;
7188 return MD->getParent()->isUnion() ||
7189 (MD->isTrivial() &&
7190 isReadByLvalueToRvalueConversion(RD: MD->getParent()));
7191 };
7192
7193 if (IsTrivialMemoryOperation(MD)) {
7194 unsigned ExplicitOffset = MD->isExplicitObjectMemberFunction() ? 1 : 0;
7195 assert(ObjectArg);
7196 APValue RHSValue;
7197 if (!handleTrivialCopy(Info, Param: MD->getParamDecl(i: 0), E: Args[0], Result&: RHSValue,
7198 CopyObjectRepresentation: MD->getParent()->isUnion()))
7199 return false;
7200
7201 LValue Obj;
7202 if (!handleAssignment(Info, E: Args[ExplicitOffset], LVal: *ObjectArg,
7203 LValType: MD->getFunctionObjectParameterReferenceType(),
7204 Val&: RHSValue))
7205 return false;
7206 ObjectArg->moveInto(V&: Result);
7207 return true;
7208 } else if (MD && isLambdaCallOperator(MD)) {
7209 // We're in a lambda; determine the lambda capture field maps unless we're
7210 // just constexpr checking a lambda's call operator. constexpr checking is
7211 // done before the captures have been added to the closure object (unless
7212 // we're inferring constexpr-ness), so we don't have access to them in this
7213 // case. But since we don't need the captures to constexpr check, we can
7214 // just ignore them.
7215 if (!Info.checkingPotentialConstantExpression())
7216 MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields,
7217 ThisCapture&: Frame.LambdaThisCaptureField);
7218 }
7219
7220 StmtResult Ret = {.Value: Result, .Slot: ResultSlot};
7221 EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body);
7222 if (ESR == ESR_Succeeded) {
7223 if (Callee->getReturnType()->isVoidType())
7224 return true;
7225 Info.FFDiag(Loc: Callee->getEndLoc(), DiagId: diag::note_constexpr_no_return);
7226 }
7227 return ESR == ESR_Returned;
7228}
7229
7230static bool HandleConstructorCall(const Expr *E, const LValue &This,
7231 CallRef Call,
7232 const CXXConstructorDecl *Definition,
7233 EvalInfo &Info, APValue &Result,
7234 bool IsCompleteClass = true);
7235
7236static bool HandleConstructorCall(const Expr *E, const LValue &This,
7237 ArrayRef<const Expr *> Args,
7238 const CXXConstructorDecl *Definition,
7239 EvalInfo &Info, APValue &Result,
7240 bool IsCompleteClass = true) {
7241 CallScopeRAII CallScope(Info);
7242 CallRef Call = Info.CurrentCall->createCall(Callee: Definition);
7243 if (!EvaluateArgs(Args, Call, Info, Callee: Definition))
7244 return false;
7245
7246 return HandleConstructorCall(E, This, Call, Definition, Info, Result,
7247 IsCompleteClass) &&
7248 CallScope.destroy();
7249}
7250
7251/// Evaluate a constructor call.
7252static bool HandleConstructorCall(const Expr *E, const LValue &This,
7253 CallRef Call,
7254 const CXXConstructorDecl *Definition,
7255 EvalInfo &Info, APValue &Result,
7256 bool IsCompleteClass) {
7257
7258 SourceLocation CallLoc = E->getExprLoc();
7259 if (!Info.CheckCallLimit(Loc: CallLoc))
7260 return false;
7261
7262 const CXXRecordDecl *RD = Definition->getParent();
7263 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7264 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_base) << RD;
7265 return false;
7266 }
7267
7268 EvalInfo::EvaluatingConstructorRAII EvalObj(
7269 Info,
7270 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
7271 RD->getNumBases());
7272 CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call);
7273
7274 // FIXME: Creating an APValue just to hold a nonexistent return value is
7275 // wasteful.
7276 APValue RetVal;
7277 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7278
7279 // If it's a delegating constructor, delegate.
7280 if (Definition->isDelegatingConstructor()) {
7281 CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
7282 if ((*I)->getInit()->isValueDependent()) {
7283 if (!EvaluateDependentExpr(E: (*I)->getInit(), Info))
7284 return false;
7285 } else {
7286 FullExpressionRAII InitScope(Info);
7287 if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) ||
7288 !InitScope.destroy())
7289 return false;
7290 }
7291 return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed;
7292 }
7293
7294 // For a trivial copy or move constructor, perform an APValue copy. This is
7295 // essential for unions (or classes with anonymous union members), where the
7296 // operations performed by the constructor cannot be represented by
7297 // ctor-initializers.
7298 //
7299 // Skip this for empty non-union classes; we should not perform an
7300 // lvalue-to-rvalue conversion on them because their copy constructor does not
7301 // actually read them.
7302 if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() &&
7303 (Definition->getParent()->isUnion() ||
7304 (Definition->isTrivial() &&
7305 isReadByLvalueToRvalueConversion(RD: Definition->getParent())))) {
7306 return handleTrivialCopy(Info, Param: Definition->getParamDecl(i: 0), E, Result,
7307 CopyObjectRepresentation: Definition->getParent()->isUnion());
7308 }
7309
7310 // Reserve space for the struct members.
7311 if (!Result.hasValue()) {
7312 if (!RD->isUnion()) {
7313 unsigned NonVirtualBases = countNonVirtualBases(RD);
7314 Result = APValue(APValue::UninitStruct(), NonVirtualBases,
7315 RD->getNumFields(), RD->getNumVBases());
7316 } else
7317 // A union starts with no active member.
7318 Result = APValue((const FieldDecl*)nullptr);
7319 }
7320
7321 if (RD->isInvalidDecl()) return false;
7322 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7323
7324 // A scope for temporaries lifetime-extended by reference members.
7325 BlockScopeRAII LifetimeExtendedScope(Info);
7326
7327 bool Success = true;
7328 unsigned BasesSeen = 0;
7329 unsigned VirtualBasesSeen = 0;
7330 unsigned NonVirtualBases = countNonVirtualBases(RD);
7331
7332 CXXRecordDecl::field_iterator FieldIt = RD->field_begin();
7333 auto SkipToField = [&](FieldDecl *FD, bool Indirect) {
7334 // We might be initializing the same field again if this is an indirect
7335 // field initialization.
7336 if (FieldIt == RD->field_end() ||
7337 FieldIt->getFieldIndex() > FD->getFieldIndex()) {
7338 assert(Indirect && "fields out of order?");
7339 return;
7340 }
7341
7342 // Default-initialize any fields with no explicit initializer.
7343 for (; !declaresSameEntity(D1: *FieldIt, D2: FD); ++FieldIt) {
7344 assert(FieldIt != RD->field_end() && "missing field?");
7345 if (!FieldIt->isUnnamedBitField())
7346 Success &= handleDefaultInitValue(
7347 T: FieldIt->getType(),
7348 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7349 }
7350 ++FieldIt;
7351 };
7352 for (const auto *I : Definition->inits()) {
7353 LValue Subobject = This;
7354 LValue SubobjectParent = This;
7355 APValue *Value = &Result;
7356
7357 // Determine the subobject to initialize.
7358 FieldDecl *FD = nullptr;
7359 if (I->isBaseInitializer()) {
7360 QualType BaseType(I->getBaseClass(), 0);
7361 if (I->isBaseVirtual()) {
7362 if (This.pointsToCompleteClass(D: RD)) {
7363 if (!HandleLValueDirectVirtualBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7364 Base: BaseType->getAsCXXRecordDecl(),
7365 RL: &Layout))
7366 return false;
7367 Value = &Result.getStructVirtualBase(i: VirtualBasesSeen++);
7368 } else {
7369 continue;
7370 }
7371
7372 } else {
7373 if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7374 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7375 return false;
7376 Value = &Result.getStructBase(i: BasesSeen++);
7377 }
7378 } else if ((FD = I->getMember())) {
7379 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout))
7380 return false;
7381 if (RD->isUnion()) {
7382 Result = APValue(FD);
7383 Value = &Result.getUnionValue();
7384 } else {
7385 SkipToField(FD, false);
7386 Value = &Result.getStructField(i: FD->getFieldIndex());
7387 }
7388 } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) {
7389 // Walk the indirect field decl's chain to find the object to initialize,
7390 // and make sure we've initialized every step along it.
7391 auto IndirectFieldChain = IFD->chain();
7392 for (auto *C : IndirectFieldChain) {
7393 FD = cast<FieldDecl>(Val: C);
7394 CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent());
7395 // Switch the union field if it differs. This happens if we had
7396 // preceding zero-initialization, and we're now initializing a union
7397 // subobject other than the first.
7398 // FIXME: In this case, the values of the other subobjects are
7399 // specified, since zero-initialization sets all padding bits to zero.
7400 if (!Value->hasValue() ||
7401 (Value->isUnion() &&
7402 !declaresSameEntity(D1: Value->getUnionField(), D2: FD))) {
7403 if (CD->isUnion())
7404 *Value = APValue(FD);
7405 else
7406 // FIXME: This immediately starts the lifetime of all members of
7407 // an anonymous struct. It would be preferable to strictly start
7408 // member lifetime in initialization order.
7409 Success &= handleDefaultInitValue(T: Info.Ctx.getCanonicalTagType(TD: CD),
7410 Result&: *Value);
7411 }
7412 // Store Subobject as its parent before updating it for the last element
7413 // in the chain.
7414 if (C == IndirectFieldChain.back())
7415 SubobjectParent = Subobject;
7416 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD))
7417 return false;
7418 if (CD->isUnion())
7419 Value = &Value->getUnionValue();
7420 else {
7421 if (C == IndirectFieldChain.front() && !RD->isUnion())
7422 SkipToField(FD, true);
7423 Value = &Value->getStructField(i: FD->getFieldIndex());
7424 }
7425 }
7426 } else {
7427 llvm_unreachable("unknown base initializer kind");
7428 }
7429
7430 // Need to override This for implicit field initializers as in this case
7431 // This refers to innermost anonymous struct/union containing initializer,
7432 // not to currently constructed class.
7433 const Expr *Init = I->getInit();
7434 if (Init->isValueDependent()) {
7435 if (!EvaluateDependentExpr(E: Init, Info))
7436 return false;
7437 } else {
7438 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent,
7439 isa<CXXDefaultInitExpr>(Val: Init));
7440 FullExpressionRAII InitScope(Info);
7441 if (FD && FD->getType()->isReferenceType() &&
7442 !FD->getType()->isFunctionReferenceType()) {
7443 LValue Result;
7444 if (!EvaluateInitForDeclOfReferenceType(Info, D: FD, Init, Result,
7445 Val&: *Value)) {
7446 if (!Info.noteFailure())
7447 return false;
7448 Success = false;
7449 }
7450 } else if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) ||
7451 (FD && FD->isBitField() &&
7452 !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) {
7453 // If we're checking for a potential constant expression, evaluate all
7454 // initializers even if some of them fail.
7455 if (!Info.noteFailure())
7456 return false;
7457 Success = false;
7458 }
7459 }
7460
7461 // This is the point at which the dynamic type of the object becomes this
7462 // class type.
7463 if (I->isBaseInitializer() && BasesSeen == NonVirtualBases)
7464 EvalObj.finishedConstructingBases();
7465 }
7466
7467 // Default-initialize any remaining fields.
7468 if (!RD->isUnion()) {
7469 for (; FieldIt != RD->field_end(); ++FieldIt) {
7470 if (!FieldIt->isUnnamedBitField())
7471 Success &= handleDefaultInitValue(
7472 T: FieldIt->getType(),
7473 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7474 }
7475 }
7476
7477 EvalObj.finishedConstructingFields();
7478
7479 return Success &&
7480 EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed &&
7481 LifetimeExtendedScope.destroy();
7482}
7483
7484static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange,
7485 const LValue &This, APValue &Value,
7486 QualType T, bool IsCompleteClass = true) {
7487 // Objects can only be destroyed while they're within their lifetimes.
7488 // FIXME: We have no representation for whether an object of type nullptr_t
7489 // is in its lifetime; it usually doesn't matter. Perhaps we should model it
7490 // as indeterminate instead?
7491 if (Value.isAbsent() && !T->isNullPtrType()) {
7492 APValue Printable;
7493 This.moveInto(V&: Printable);
7494 Info.FFDiag(Loc: CallRange.getBegin(),
7495 DiagId: diag::note_constexpr_destroy_out_of_lifetime)
7496 << Printable.getAsString(Ctx: Info.Ctx, Ty: Info.Ctx.getLValueReferenceType(T));
7497 return false;
7498 }
7499
7500 // Invent an expression for location purposes.
7501 // FIXME: We shouldn't need to do this.
7502 OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue);
7503
7504 // For arrays, destroy elements right-to-left.
7505 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) {
7506 uint64_t Size = CAT->getZExtSize();
7507 QualType ElemT = CAT->getElementType();
7508
7509 if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin()))
7510 return false;
7511
7512 LValue ElemLV = This;
7513 ElemLV.addArray(Info, E: &LocE, CAT);
7514 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: Size))
7515 return false;
7516
7517 // Ensure that we have actual array elements available to destroy; the
7518 // destructors might mutate the value, so we can't run them on the array
7519 // filler.
7520 if (Size && Size > Value.getArrayInitializedElts())
7521 expandArray(Array&: Value, Index: Value.getArraySize() - 1);
7522
7523 // The size of the array might have been reduced by
7524 // a placement new.
7525 for (Size = Value.getArraySize(); Size != 0; --Size) {
7526 APValue &Elem = Value.getArrayInitializedElt(I: Size - 1);
7527 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: -1) ||
7528 !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT))
7529 return false;
7530 }
7531
7532 // End the lifetime of this array now.
7533 Value = APValue();
7534 return true;
7535 }
7536
7537 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
7538 if (!RD) {
7539 if (T.isDestructedType()) {
7540 Info.FFDiag(Loc: CallRange.getBegin(),
7541 DiagId: diag::note_constexpr_unsupported_destruction)
7542 << T;
7543 return false;
7544 }
7545
7546 Value = APValue();
7547 return true;
7548 }
7549
7550 if (!Info.getLangOpts().CPlusPlus26 && RD->getNumVBases()) {
7551 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_virtual_base) << RD;
7552 return false;
7553 }
7554
7555 // If an anonymous union would be destroyed, some enclosing destructor must
7556 // have been explicitly defined, and the anonymous union destruction should
7557 // have no effect.
7558 if (RD->isAnonymousStructOrUnion() && RD->isUnion()) {
7559 Value = APValue();
7560 return true;
7561 }
7562
7563 const CXXDestructorDecl *DD = RD->getDestructor();
7564 if (!DD && !RD->hasTrivialDestructor()) {
7565 Info.FFDiag(Loc: CallRange.getBegin());
7566 return false;
7567 }
7568
7569 if (!DD || DD->isTrivial()) {
7570 // A trivial destructor just ends the lifetime of the object. Check for
7571 // this case before checking for a body, because we might not bother
7572 // building a body for a trivial destructor. Note that it doesn't matter
7573 // whether the destructor is constexpr in this case; all trivial
7574 // destructors are constexpr.
7575 Value = APValue();
7576 return true;
7577 }
7578
7579 if (!Info.CheckCallLimit(Loc: CallRange.getBegin()))
7580 return false;
7581
7582 const FunctionDecl *Definition = nullptr;
7583 const Stmt *Body = DD->getBody(Definition);
7584
7585 if (!CheckConstexprFunction(Info, CallLoc: CallRange.getBegin(), Declaration: DD, Definition, Body))
7586 return false;
7587
7588 CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr,
7589 CallRef());
7590
7591 // We're now in the period of destruction of this object.
7592 EvalInfo::EvaluatingDestructorRAII EvalObj(
7593 Info,
7594 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries});
7595 unsigned NonVirtualBases = countNonVirtualBases(RD);
7596 unsigned NumVirtualBases = RD->getNumVBases();
7597 unsigned BasesLeft = NonVirtualBases;
7598 if (!EvalObj.DidInsert) {
7599 // C++2a [class.dtor]p19:
7600 // the behavior is undefined if the destructor is invoked for an object
7601 // whose lifetime has ended
7602 // (Note that formally the lifetime ends when the period of destruction
7603 // begins, even though certain uses of the object remain valid until the
7604 // period of destruction ends.)
7605 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_double_destroy);
7606 return false;
7607 }
7608
7609 // FIXME: Creating an APValue just to hold a nonexistent return value is
7610 // wasteful.
7611 APValue RetVal;
7612 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7613 if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed)
7614 return false;
7615
7616 // A union destructor does not implicitly destroy its members.
7617 if (RD->isUnion())
7618 return true;
7619
7620 if (!ASTContext::hasLayout(D: RD))
7621 return false;
7622 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7623
7624 // We don't have a good way to iterate fields in reverse, so collect all the
7625 // fields first and then walk them backwards.
7626 SmallVector<FieldDecl*, 16> Fields(RD->fields());
7627 for (const FieldDecl *FD : llvm::reverse(C&: Fields)) {
7628 if (FD->isUnnamedBitField())
7629 continue;
7630
7631 LValue Subobject = This;
7632 if (!HandleLValueMember(Info, E: &LocE, LVal&: Subobject, FD, RL: &Layout))
7633 return false;
7634
7635 APValue *SubobjectValue = &Value.getStructField(i: FD->getFieldIndex());
7636 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7637 T: FD->getType()))
7638 return false;
7639 }
7640
7641 if (BasesLeft != 0 || NumVirtualBases != 0)
7642 EvalObj.startedDestroyingBases();
7643
7644 // Destroy base classes in reverse order.
7645 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) {
7646 if (Base.isVirtual())
7647 continue;
7648 --BasesLeft;
7649
7650 QualType BaseType = Base.getType();
7651 LValue Subobject = This;
7652 if (!HandleLValueDirectBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7653 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7654 return false;
7655
7656 APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft);
7657 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7658 T: BaseType, /*IsCompleteClass=*/false))
7659 return false;
7660 }
7661 assert(BasesLeft == 0 && "NumBases was wrong?");
7662
7663 // Virtual bases.
7664 if (IsCompleteClass) {
7665 unsigned VirtualBasesLeft = NumVirtualBases;
7666 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->vbases())) {
7667 --VirtualBasesLeft;
7668
7669 QualType BaseType = Base.getType();
7670 LValue Subobject = This;
7671 if (!HandleLValueDirectVirtualBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7672 Base: BaseType->getAsCXXRecordDecl(),
7673 RL: &Layout))
7674 return false;
7675
7676 APValue *SubobjectValue = &Value.getStructVirtualBase(i: VirtualBasesLeft);
7677 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7678 T: BaseType, /*IsCompleteClass=*/false))
7679 return false;
7680 }
7681 assert(VirtualBasesLeft == 0 && "NumVirtualBases was wrong?");
7682 }
7683
7684 // The period of destruction ends now. The object is gone.
7685 Value = APValue();
7686 return true;
7687}
7688
7689namespace {
7690struct DestroyObjectHandler {
7691 EvalInfo &Info;
7692 const Expr *E;
7693 const LValue &This;
7694 const AccessKinds AccessKind;
7695
7696 typedef bool result_type;
7697 bool failed() { return false; }
7698 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
7699 return HandleDestructionImpl(Info, CallRange: E->getSourceRange(), This, Value&: Subobj,
7700 T: SubobjType);
7701 }
7702 bool found(APSInt &Value, QualType SubobjType) {
7703 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7704 return false;
7705 }
7706 bool found(APFloat &Value, QualType SubobjType) {
7707 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7708 return false;
7709 }
7710};
7711}
7712
7713/// Perform a destructor or pseudo-destructor call on the given object, which
7714/// might in general not be a complete object.
7715static bool HandleDestruction(EvalInfo &Info, const Expr *E,
7716 const LValue &This, QualType ThisType) {
7717 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType);
7718 DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy};
7719 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
7720}
7721
7722/// Destroy and end the lifetime of the given complete object.
7723static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
7724 APValue::LValueBase LVBase, APValue &Value,
7725 QualType T) {
7726 // If we've had an unmodeled side-effect, we can't rely on mutable state
7727 // (such as the object we're about to destroy) being correct.
7728 if (Info.EvalStatus.HasSideEffects)
7729 return false;
7730
7731 LValue LV;
7732 LV.set(B: {LVBase});
7733 return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T);
7734}
7735
7736/// Perform a call to 'operator new' or to `__builtin_operator_new'.
7737static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E,
7738 LValue &Result) {
7739 if (Info.checkingPotentialConstantExpression() ||
7740 Info.SpeculativeEvaluationDepth)
7741 return false;
7742
7743 // This is permitted only within a call to std::allocator<T>::allocate.
7744 auto Caller = Info.getStdAllocatorCaller(FnName: "allocate");
7745 if (!Caller) {
7746 Info.FFDiag(Loc: E->getExprLoc(), DiagId: Info.getLangOpts().CPlusPlus20
7747 ? diag::note_constexpr_new_untyped
7748 : diag::note_constexpr_new);
7749 return false;
7750 }
7751
7752 QualType ElemType = Caller.ElemType;
7753 if (ElemType->isIncompleteType() || ElemType->isFunctionType()) {
7754 Info.FFDiag(Loc: E->getExprLoc(),
7755 DiagId: diag::note_constexpr_new_not_complete_object_type)
7756 << (ElemType->isIncompleteType() ? 0 : 1) << ElemType;
7757 return false;
7758 }
7759
7760 APSInt ByteSize;
7761 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info))
7762 return false;
7763 bool IsNothrow = false;
7764 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) {
7765 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7766 IsNothrow |= E->getType()->isNothrowT();
7767 }
7768
7769 CharUnits ElemSize;
7770 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElemType, Size&: ElemSize))
7771 return false;
7772 APInt Size, Remainder;
7773 APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity());
7774 APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder);
7775 if (Remainder != 0) {
7776 // This likely indicates a bug in the implementation of 'std::allocator'.
7777 Info.FFDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_operator_new_bad_size)
7778 << ByteSize << APSInt(ElemSizeAP, true) << ElemType;
7779 return false;
7780 }
7781
7782 if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(),
7783 ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) {
7784 if (IsNothrow) {
7785 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
7786 return true;
7787 }
7788 return false;
7789 }
7790
7791 QualType AllocType = Info.Ctx.getConstantArrayType(
7792 EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
7793 APValue *Val = Info.createHeapAlloc(E: Caller.Call, T: AllocType, LV&: Result);
7794 *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue());
7795 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: AllocType));
7796 return true;
7797}
7798
7799static bool hasVirtualDestructor(QualType T) {
7800 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7801 if (CXXDestructorDecl *DD = RD->getDestructor())
7802 return DD->isVirtual();
7803 return false;
7804}
7805
7806static const FunctionDecl *getVirtualOperatorDelete(QualType T) {
7807 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7808 if (CXXDestructorDecl *DD = RD->getDestructor())
7809 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
7810 return nullptr;
7811}
7812
7813/// Check that the given object is a suitable pointer to a heap allocation that
7814/// still exists and is of the right kind for the purpose of a deletion.
7815///
7816/// On success, returns the heap allocation to deallocate. On failure, produces
7817/// a diagnostic and returns std::nullopt.
7818static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E,
7819 const LValue &Pointer,
7820 DynAlloc::Kind DeallocKind) {
7821 auto PointerAsString = [&] {
7822 return Pointer.toString(Ctx&: Info.Ctx, T: Info.Ctx.VoidPtrTy);
7823 };
7824
7825 DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>();
7826 if (!DA) {
7827 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_not_heap_alloc)
7828 << PointerAsString();
7829 if (Pointer.Base)
7830 NoteLValueLocation(Info, Base: Pointer.Base);
7831 return std::nullopt;
7832 }
7833
7834 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
7835 if (!Alloc) {
7836 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
7837 return std::nullopt;
7838 }
7839
7840 if (DeallocKind != (*Alloc)->getKind()) {
7841 QualType AllocType = Pointer.Base.getDynamicAllocType();
7842 Info.FFDiag(E, DiagId: diag::note_constexpr_new_delete_mismatch)
7843 << DeallocKind << (*Alloc)->getKind() << AllocType;
7844 NoteLValueLocation(Info, Base: Pointer.Base);
7845 return std::nullopt;
7846 }
7847
7848 bool Subobject = false;
7849 if (DeallocKind == DynAlloc::New) {
7850 Subobject = Pointer.Designator.MostDerivedPathLength != 0 ||
7851 Pointer.Designator.isOnePastTheEnd();
7852 } else {
7853 Subobject = Pointer.Designator.Entries.size() != 1 ||
7854 Pointer.Designator.Entries[0].getAsArrayIndex() != 0;
7855 }
7856 if (Subobject) {
7857 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_subobject)
7858 << PointerAsString() << Pointer.Designator.isOnePastTheEnd();
7859 return std::nullopt;
7860 }
7861
7862 return Alloc;
7863}
7864
7865// Perform a call to 'operator delete' or '__builtin_operator_delete'.
7866static bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) {
7867 if (Info.checkingPotentialConstantExpression() ||
7868 Info.SpeculativeEvaluationDepth)
7869 return false;
7870
7871 // This is permitted only within a call to std::allocator<T>::deallocate.
7872 if (!Info.getStdAllocatorCaller(FnName: "deallocate")) {
7873 Info.FFDiag(Loc: E->getExprLoc());
7874 return true;
7875 }
7876
7877 LValue Pointer;
7878 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info))
7879 return false;
7880 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)
7881 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7882
7883 if (Pointer.Designator.Invalid)
7884 return false;
7885
7886 // Deleting a null pointer would have no effect, but it's not permitted by
7887 // std::allocator<T>::deallocate's contract.
7888 if (Pointer.isNullPointer()) {
7889 Info.CCEDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_deallocate_null);
7890 return true;
7891 }
7892
7893 if (!CheckDeleteKind(Info, E, Pointer, DeallocKind: DynAlloc::StdAllocator))
7894 return false;
7895
7896 Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>());
7897 return true;
7898}
7899
7900//===----------------------------------------------------------------------===//
7901// Generic Evaluation
7902//===----------------------------------------------------------------------===//
7903namespace {
7904
7905class BitCastBuffer {
7906 // FIXME: We're going to need bit-level granularity when we support
7907 // bit-fields.
7908 // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but
7909 // we don't support a host or target where that is the case. Still, we should
7910 // use a more generic type in case we ever do.
7911 SmallVector<std::optional<unsigned char>, 32> Bytes;
7912
7913 static_assert(std::numeric_limits<unsigned char>::digits >= 8,
7914 "Need at least 8 bit unsigned char");
7915
7916 bool TargetIsLittleEndian;
7917
7918public:
7919 BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian)
7920 : Bytes(Width.getQuantity()),
7921 TargetIsLittleEndian(TargetIsLittleEndian) {}
7922
7923 [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width,
7924 SmallVectorImpl<unsigned char> &Output) const {
7925 for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) {
7926 // If a byte of an integer is uninitialized, then the whole integer is
7927 // uninitialized.
7928 if (!Bytes[I.getQuantity()])
7929 return false;
7930 Output.push_back(Elt: *Bytes[I.getQuantity()]);
7931 }
7932 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
7933 std::reverse(first: Output.begin(), last: Output.end());
7934 return true;
7935 }
7936
7937 void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) {
7938 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
7939 std::reverse(first: Input.begin(), last: Input.end());
7940
7941 size_t Index = 0;
7942 for (unsigned char Byte : Input) {
7943 assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?");
7944 Bytes[Offset.getQuantity() + Index] = Byte;
7945 ++Index;
7946 }
7947 }
7948
7949 size_t size() { return Bytes.size(); }
7950};
7951
7952/// Traverse an APValue to produce an BitCastBuffer, emulating how the current
7953/// target would represent the value at runtime.
7954class APValueToBufferConverter {
7955 EvalInfo &Info;
7956 BitCastBuffer Buffer;
7957 const CastExpr *BCE;
7958
7959 APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth,
7960 const CastExpr *BCE)
7961 : Info(Info),
7962 Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()),
7963 BCE(BCE) {}
7964
7965 bool visit(const APValue &Val, QualType Ty) {
7966 return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0));
7967 }
7968
7969 // Write out Val with type Ty into Buffer starting at Offset.
7970 bool visit(const APValue &Val, QualType Ty, CharUnits Offset) {
7971 assert((size_t)Offset.getQuantity() <= Buffer.size());
7972
7973 // As a special case, nullptr_t has an indeterminate value.
7974 if (Ty->isNullPtrType())
7975 return true;
7976
7977 // Dig through Src to find the byte at SrcOffset.
7978 switch (Val.getKind()) {
7979 case APValue::Indeterminate:
7980 case APValue::None:
7981 return true;
7982
7983 case APValue::Int:
7984 return visitInt(Val: Val.getInt(), Ty, Offset);
7985 case APValue::Float:
7986 return visitFloat(Val: Val.getFloat(), Ty, Offset);
7987 case APValue::Array:
7988 return visitArray(Val, Ty, Offset);
7989 case APValue::Struct:
7990 return visitRecord(Val, Ty, Offset);
7991 case APValue::Vector:
7992 return visitVector(Val, Ty, Offset);
7993
7994 case APValue::ComplexInt:
7995 case APValue::ComplexFloat:
7996 return visitComplex(Val, Ty, Offset);
7997 case APValue::FixedPoint:
7998 // FIXME: We should support these.
7999
8000 case APValue::LValue:
8001 case APValue::Matrix:
8002 case APValue::Union:
8003 case APValue::MemberPointer:
8004 case APValue::AddrLabelDiff: {
8005 Info.FFDiag(Loc: BCE->getBeginLoc(),
8006 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8007 << Ty;
8008 return false;
8009 }
8010 }
8011 llvm_unreachable("Unhandled APValue::ValueKind");
8012 }
8013
8014 bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) {
8015 const RecordDecl *RD = Ty->getAsRecordDecl();
8016 if (!ASTContext::hasLayout(D: RD))
8017 return false;
8018 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8019
8020 // Visit the base classes.
8021 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8022 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8023 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8024 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8025 const APValue &Base = Val.getStructBase(i: I);
8026
8027 // Can happen in error cases.
8028 if (!Base.isStruct())
8029 return false;
8030
8031 if (!visitRecord(Val: Base, Ty: BS.getType(),
8032 Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset))
8033 return false;
8034 }
8035 }
8036
8037 // Visit the fields.
8038 unsigned FieldIdx = 0;
8039 for (FieldDecl *FD : RD->fields()) {
8040 if (FD->isBitField()) {
8041 Info.FFDiag(Loc: BCE->getBeginLoc(),
8042 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8043 return false;
8044 }
8045
8046 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8047
8048 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 &&
8049 "only bit-fields can have sub-char alignment");
8050 CharUnits FieldOffset =
8051 Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset;
8052 QualType FieldTy = FD->getType();
8053 if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset))
8054 return false;
8055 ++FieldIdx;
8056 }
8057
8058 return true;
8059 }
8060
8061 bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) {
8062 const auto *CAT =
8063 dyn_cast_or_null<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe());
8064 if (!CAT)
8065 return false;
8066
8067 CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(T: CAT->getElementType());
8068 unsigned NumInitializedElts = Val.getArrayInitializedElts();
8069 unsigned ArraySize = Val.getArraySize();
8070 // First, initialize the initialized elements.
8071 for (unsigned I = 0; I != NumInitializedElts; ++I) {
8072 const APValue &SubObj = Val.getArrayInitializedElt(I);
8073 if (!visit(Val: SubObj, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8074 return false;
8075 }
8076
8077 // Next, initialize the rest of the array using the filler.
8078 if (Val.hasArrayFiller()) {
8079 const APValue &Filler = Val.getArrayFiller();
8080 for (unsigned I = NumInitializedElts; I != ArraySize; ++I) {
8081 if (!visit(Val: Filler, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
8082 return false;
8083 }
8084 }
8085
8086 return true;
8087 }
8088
8089 bool visitComplex(const APValue &Val, QualType Ty, CharUnits Offset) {
8090 const ComplexType *ComplexTy = Ty->castAs<ComplexType>();
8091 QualType EltTy = ComplexTy->getElementType();
8092 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8093 bool IsInt = Val.isComplexInt();
8094
8095 if (IsInt) {
8096 if (!visitInt(Val: Val.getComplexIntReal(), Ty: EltTy,
8097 Offset: Offset + (0 * EltSizeChars)))
8098 return false;
8099 if (!visitInt(Val: Val.getComplexIntImag(), Ty: EltTy,
8100 Offset: Offset + (1 * EltSizeChars)))
8101 return false;
8102 } else {
8103 if (!visitFloat(Val: Val.getComplexFloatReal(), Ty: EltTy,
8104 Offset: Offset + (0 * EltSizeChars)))
8105 return false;
8106 if (!visitFloat(Val: Val.getComplexFloatImag(), Ty: EltTy,
8107 Offset: Offset + (1 * EltSizeChars)))
8108 return false;
8109 }
8110
8111 return true;
8112 }
8113
8114 bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {
8115 const VectorType *VTy = Ty->castAs<VectorType>();
8116 QualType EltTy = VTy->getElementType();
8117 unsigned NElts = VTy->getNumElements();
8118
8119 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8120 // Special handling for OpenCL bool vectors:
8121 // Since these vectors are stored as packed bits, but we can't write
8122 // individual bits to the BitCastBuffer, we'll buffer all of the elements
8123 // together into an appropriately sized APInt and write them all out at
8124 // once. Because we don't accept vectors where NElts * EltSize isn't a
8125 // multiple of the char size, there will be no padding space, so we don't
8126 // have to worry about writing data which should have been left
8127 // uninitialized.
8128 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8129
8130 llvm::APInt Res = llvm::APInt::getZero(numBits: NElts);
8131 for (unsigned I = 0; I < NElts; ++I) {
8132 const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt();
8133 assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 &&
8134 "bool vector element must be 1-bit unsigned integer!");
8135
8136 Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I);
8137 }
8138
8139 SmallVector<uint8_t, 8> Bytes(NElts / 8);
8140 llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8);
8141 Buffer.writeObject(Offset, Input&: Bytes);
8142 } else {
8143 // Iterate over each of the elements and write them out to the buffer at
8144 // the appropriate offset.
8145 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8146 for (unsigned I = 0; I < NElts; ++I) {
8147 if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars))
8148 return false;
8149 }
8150 }
8151
8152 return true;
8153 }
8154
8155 bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) {
8156 APSInt AdjustedVal = Val;
8157 unsigned Width = AdjustedVal.getBitWidth();
8158 if (Ty->isBooleanType()) {
8159 Width = Info.Ctx.getTypeSize(T: Ty);
8160 AdjustedVal = AdjustedVal.extend(width: Width);
8161 }
8162
8163 SmallVector<uint8_t, 8> Bytes(Width / 8);
8164 llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8);
8165 Buffer.writeObject(Offset, Input&: Bytes);
8166 return true;
8167 }
8168
8169 bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) {
8170 APSInt AsInt(Val.bitcastToAPInt());
8171 return visitInt(Val: AsInt, Ty, Offset);
8172 }
8173
8174public:
8175 static std::optional<BitCastBuffer>
8176 convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) {
8177 CharUnits DstSize = Info.Ctx.getTypeSizeInChars(T: BCE->getType());
8178 APValueToBufferConverter Converter(Info, DstSize, BCE);
8179 if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType()))
8180 return std::nullopt;
8181 return Converter.Buffer;
8182 }
8183};
8184
8185/// Write an BitCastBuffer into an APValue.
8186class BufferToAPValueConverter {
8187 EvalInfo &Info;
8188 const BitCastBuffer &Buffer;
8189 const CastExpr *BCE;
8190
8191 BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer,
8192 const CastExpr *BCE)
8193 : Info(Info), Buffer(Buffer), BCE(BCE) {}
8194
8195 // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast
8196 // with an invalid type, so anything left is a deficiency on our part (FIXME).
8197 // Ideally this will be unreachable.
8198 std::nullopt_t unsupportedType(QualType Ty) {
8199 Info.FFDiag(Loc: BCE->getBeginLoc(),
8200 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8201 << Ty;
8202 return std::nullopt;
8203 }
8204
8205 std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) {
8206 Info.FFDiag(Loc: BCE->getBeginLoc(),
8207 DiagId: diag::note_constexpr_bit_cast_unrepresentable_value)
8208 << Ty << toString(I: Val, /*Radix=*/10);
8209 return std::nullopt;
8210 }
8211
8212 std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset,
8213 const EnumType *EnumSugar = nullptr) {
8214 if (T->isNullPtrType()) {
8215 uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0));
8216 return APValue((Expr *)nullptr,
8217 /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue),
8218 APValue::NoLValuePath{}, /*IsNullPtr=*/true);
8219 }
8220
8221 CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T);
8222
8223 // Work around floating point types that contain unused padding bytes. This
8224 // is really just `long double` on x86, which is the only fundamental type
8225 // with padding bytes.
8226 if (T->isRealFloatingType()) {
8227 const llvm::fltSemantics &Semantics =
8228 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8229 unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics);
8230 assert(NumBits % 8 == 0);
8231 CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8);
8232 if (NumBytes != SizeOf)
8233 SizeOf = NumBytes;
8234 }
8235
8236 SmallVector<uint8_t, 8> Bytes;
8237 if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) {
8238 // If this is std::byte or unsigned char, then its okay to store an
8239 // indeterminate value.
8240 bool IsStdByte = EnumSugar && EnumSugar->isStdByteType();
8241 bool IsUChar =
8242 !EnumSugar && (T->isSpecificBuiltinType(K: BuiltinType::UChar) ||
8243 T->isSpecificBuiltinType(K: BuiltinType::Char_U));
8244 if (!IsStdByte && !IsUChar) {
8245 QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0);
8246 Info.FFDiag(Loc: BCE->getExprLoc(),
8247 DiagId: diag::note_constexpr_bit_cast_indet_dest)
8248 << DisplayType << Info.Ctx.getLangOpts().CharIsSigned;
8249 return std::nullopt;
8250 }
8251
8252 return APValue::IndeterminateValue();
8253 }
8254
8255 APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true);
8256 llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8257
8258 if (T->isIntegralOrEnumerationType()) {
8259 Val.setIsSigned(T->isSignedIntegerOrEnumerationType());
8260
8261 unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0));
8262 if (IntWidth != Val.getBitWidth()) {
8263 APSInt Truncated = Val.trunc(width: IntWidth);
8264 if (Truncated.extend(width: Val.getBitWidth()) != Val)
8265 return unrepresentableValue(Ty: QualType(T, 0), Val);
8266 Val = Truncated;
8267 }
8268
8269 return APValue(Val);
8270 }
8271
8272 if (T->isRealFloatingType()) {
8273 const llvm::fltSemantics &Semantics =
8274 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8275 return APValue(APFloat(Semantics, Val));
8276 }
8277
8278 return unsupportedType(Ty: QualType(T, 0));
8279 }
8280
8281 std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) {
8282 const RecordDecl *RD = RTy->getAsRecordDecl();
8283 if (RD->isInvalidDecl())
8284 return std::nullopt;
8285 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8286
8287 unsigned NumBases = 0;
8288 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
8289 NumBases = CXXRD->getNumBases();
8290
8291 APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());
8292
8293 // Visit the base classes.
8294 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8295 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8296 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8297 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8298
8299 std::optional<APValue> SubObj = visitType(
8300 Ty: BS.getType(), Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset);
8301 if (!SubObj)
8302 return std::nullopt;
8303 ResultVal.getStructBase(i: I) = *SubObj;
8304 }
8305 }
8306
8307 // Visit the fields.
8308 unsigned FieldIdx = 0;
8309 for (FieldDecl *FD : RD->fields()) {
8310 // FIXME: We don't currently support bit-fields. A lot of the logic for
8311 // this is in CodeGen, so we need to factor it around.
8312 if (FD->isBitField()) {
8313 Info.FFDiag(Loc: BCE->getBeginLoc(),
8314 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8315 return std::nullopt;
8316 }
8317
8318 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8319 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0);
8320
8321 CharUnits FieldOffset =
8322 CharUnits::fromQuantity(Quantity: FieldOffsetBits / Info.Ctx.getCharWidth()) +
8323 Offset;
8324 QualType FieldTy = FD->getType();
8325 std::optional<APValue> SubObj = visitType(Ty: FieldTy, Offset: FieldOffset);
8326 if (!SubObj)
8327 return std::nullopt;
8328 ResultVal.getStructField(i: FieldIdx) = *SubObj;
8329 ++FieldIdx;
8330 }
8331
8332 return ResultVal;
8333 }
8334
8335 std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) {
8336 QualType RepresentationType =
8337 Ty->getDecl()->getDefinitionOrSelf()->getIntegerType();
8338 assert(!RepresentationType.isNull() &&
8339 "enum forward decl should be caught by Sema");
8340 const auto *AsBuiltin =
8341 RepresentationType.getCanonicalType()->castAs<BuiltinType>();
8342 // Recurse into the underlying type. Treat std::byte transparently as
8343 // unsigned char.
8344 return visit(T: AsBuiltin, Offset, /*EnumTy=*/EnumSugar: Ty);
8345 }
8346
8347 std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) {
8348 size_t Size = Ty->getLimitedSize();
8349 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: Ty->getElementType());
8350
8351 APValue ArrayValue(APValue::UninitArray(), Size, Size);
8352 for (size_t I = 0; I != Size; ++I) {
8353 std::optional<APValue> ElementValue =
8354 visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8355 if (!ElementValue)
8356 return std::nullopt;
8357 ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue);
8358 }
8359
8360 return ArrayValue;
8361 }
8362
8363 std::optional<APValue> visit(const ComplexType *Ty, CharUnits Offset) {
8364 QualType ElementType = Ty->getElementType();
8365 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: ElementType);
8366 bool IsInt = ElementType->isIntegerType();
8367
8368 std::optional<APValue> Values[2];
8369 for (unsigned I = 0; I != 2; ++I) {
8370 Values[I] = visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8371 if (!Values[I])
8372 return std::nullopt;
8373 }
8374
8375 if (IsInt)
8376 return APValue(Values[0]->getInt(), Values[1]->getInt());
8377 return APValue(Values[0]->getFloat(), Values[1]->getFloat());
8378 }
8379
8380 std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) {
8381 QualType EltTy = VTy->getElementType();
8382 unsigned NElts = VTy->getNumElements();
8383 unsigned EltSize =
8384 VTy->isPackedVectorBoolType(ctx: Info.Ctx) ? 1 : Info.Ctx.getTypeSize(T: EltTy);
8385
8386 SmallVector<APValue, 4> Elts;
8387 Elts.reserve(N: NElts);
8388 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8389 // Special handling for OpenCL bool vectors:
8390 // Since these vectors are stored as packed bits, but we can't read
8391 // individual bits from the BitCastBuffer, we'll buffer all of the
8392 // elements together into an appropriately sized APInt and write them all
8393 // out at once. Because we don't accept vectors where NElts * EltSize
8394 // isn't a multiple of the char size, there will be no padding space, so
8395 // we don't have to worry about reading any padding data which didn't
8396 // actually need to be accessed.
8397 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8398
8399 SmallVector<uint8_t, 8> Bytes;
8400 Bytes.reserve(N: NElts / 8);
8401 if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes))
8402 return std::nullopt;
8403
8404 APSInt SValInt(NElts, true);
8405 llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8406
8407 for (unsigned I = 0; I < NElts; ++I) {
8408 llvm::APInt Elt =
8409 SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize);
8410 Elts.emplace_back(
8411 Args: APSInt(std::move(Elt), !EltTy->isSignedIntegerType()));
8412 }
8413 } else {
8414 // Iterate over each of the elements and read them from the buffer at
8415 // the appropriate offset.
8416 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8417 for (unsigned I = 0; I < NElts; ++I) {
8418 std::optional<APValue> EltValue =
8419 visitType(Ty: EltTy, Offset: Offset + I * EltSizeChars);
8420 if (!EltValue)
8421 return std::nullopt;
8422 Elts.push_back(Elt: std::move(*EltValue));
8423 }
8424 }
8425
8426 return APValue(Elts.data(), Elts.size());
8427 }
8428
8429 std::optional<APValue> visit(const Type *Ty, CharUnits Offset) {
8430 return unsupportedType(Ty: QualType(Ty, 0));
8431 }
8432
8433 std::optional<APValue> visitType(QualType Ty, CharUnits Offset) {
8434 QualType Can = Ty.getCanonicalType();
8435
8436 switch (Can->getTypeClass()) {
8437#define TYPE(Class, Base) \
8438 case Type::Class: \
8439 return visit(cast<Class##Type>(Can.getTypePtr()), Offset);
8440#define ABSTRACT_TYPE(Class, Base)
8441#define NON_CANONICAL_TYPE(Class, Base) \
8442 case Type::Class: \
8443 llvm_unreachable("non-canonical type should be impossible!");
8444#define DEPENDENT_TYPE(Class, Base) \
8445 case Type::Class: \
8446 llvm_unreachable( \
8447 "dependent types aren't supported in the constant evaluator!");
8448#define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \
8449 case Type::Class: \
8450 llvm_unreachable("either dependent or not canonical!");
8451#include "clang/AST/TypeNodes.inc"
8452 }
8453 llvm_unreachable("Unhandled Type::TypeClass");
8454 }
8455
8456public:
8457 // Pull out a full value of type DstType.
8458 static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer,
8459 const CastExpr *BCE) {
8460 BufferToAPValueConverter Converter(Info, Buffer, BCE);
8461 return Converter.visitType(Ty: BCE->getType(), Offset: CharUnits::fromQuantity(Quantity: 0));
8462 }
8463};
8464
8465static bool checkBitCastConstexprEligibilityType(SourceLocation Loc,
8466 QualType Ty, EvalInfo *Info,
8467 const ASTContext &Ctx,
8468 bool CheckingDest) {
8469 Ty = Ty.getCanonicalType();
8470
8471 auto diag = [&](int Reason) {
8472 if (Info)
8473 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_type)
8474 << CheckingDest << (Reason == 4) << Reason;
8475 return false;
8476 };
8477 auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) {
8478 if (Info)
8479 Info->Note(Loc: NoteLoc, DiagId: diag::note_constexpr_bit_cast_invalid_subtype)
8480 << NoteTy << Construct << Ty;
8481 return false;
8482 };
8483
8484 if (Ty->isUnionType())
8485 return diag(0);
8486 if (Ty->isPointerType())
8487 return diag(1);
8488 if (Ty->isMemberPointerType())
8489 return diag(2);
8490 if (Ty.isVolatileQualified())
8491 return diag(3);
8492
8493 if (RecordDecl *Record = Ty->getAsRecordDecl()) {
8494 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: Record)) {
8495 for (CXXBaseSpecifier &BS : CXXRD->bases())
8496 if (!checkBitCastConstexprEligibilityType(Loc, Ty: BS.getType(), Info, Ctx,
8497 CheckingDest))
8498 return note(1, BS.getType(), BS.getBeginLoc());
8499 }
8500 for (FieldDecl *FD : Record->fields()) {
8501 if (FD->getType()->isReferenceType())
8502 return diag(4);
8503 if (!checkBitCastConstexprEligibilityType(Loc, Ty: FD->getType(), Info, Ctx,
8504 CheckingDest))
8505 return note(0, FD->getType(), FD->getBeginLoc());
8506 }
8507 }
8508
8509 if (Ty->isArrayType() &&
8510 !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty),
8511 Info, Ctx, CheckingDest))
8512 return false;
8513
8514 if (const auto *VTy = Ty->getAs<VectorType>()) {
8515 QualType EltTy = VTy->getElementType();
8516 unsigned NElts = VTy->getNumElements();
8517 unsigned EltSize =
8518 VTy->isPackedVectorBoolType(ctx: Ctx) ? 1 : Ctx.getTypeSize(T: EltTy);
8519
8520 if ((NElts * EltSize) % Ctx.getCharWidth() != 0) {
8521 // The vector's size in bits is not a multiple of the target's byte size,
8522 // so its layout is unspecified. For now, we'll simply treat these cases
8523 // as unsupported (this should only be possible with OpenCL bool vectors
8524 // whose element count isn't a multiple of the byte size).
8525 if (Info)
8526 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_vector)
8527 << QualType(VTy, 0) << EltSize << NElts << Ctx.getCharWidth();
8528 return false;
8529 }
8530
8531 if (EltTy->isRealFloatingType() &&
8532 &Ctx.getFloatTypeSemantics(T: EltTy) == &APFloat::x87DoubleExtended()) {
8533 // The layout for x86_fp80 vectors seems to be handled very inconsistently
8534 // by both clang and LLVM, so for now we won't allow bit_casts involving
8535 // it in a constexpr context.
8536 if (Info)
8537 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8538 << EltTy;
8539 return false;
8540 }
8541 }
8542
8543 return true;
8544}
8545
8546static bool checkBitCastConstexprEligibility(EvalInfo *Info,
8547 const ASTContext &Ctx,
8548 const CastExpr *BCE) {
8549 bool DestOK = checkBitCastConstexprEligibilityType(
8550 Loc: BCE->getBeginLoc(), Ty: BCE->getType(), Info, Ctx, CheckingDest: true);
8551 bool SourceOK = DestOK && checkBitCastConstexprEligibilityType(
8552 Loc: BCE->getBeginLoc(),
8553 Ty: BCE->getSubExpr()->getType(), Info, Ctx, CheckingDest: false);
8554 return SourceOK;
8555}
8556
8557static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8558 const APValue &SourceRValue,
8559 const CastExpr *BCE) {
8560 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8561 "no host or target supports non 8-bit chars");
8562
8563 if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE))
8564 return false;
8565
8566 // Read out SourceValue into a char buffer.
8567 std::optional<BitCastBuffer> Buffer =
8568 APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE);
8569 if (!Buffer)
8570 return false;
8571
8572 // Write out the buffer into a new APValue.
8573 std::optional<APValue> MaybeDestValue =
8574 BufferToAPValueConverter::convert(Info, Buffer&: *Buffer, BCE);
8575 if (!MaybeDestValue)
8576 return false;
8577
8578 DestValue = std::move(*MaybeDestValue);
8579 return true;
8580}
8581
8582static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8583 APValue &SourceValue,
8584 const CastExpr *BCE) {
8585 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8586 "no host or target supports non 8-bit chars");
8587 assert(SourceValue.isLValue() &&
8588 "LValueToRValueBitcast requires an lvalue operand!");
8589
8590 LValue SourceLValue;
8591 APValue SourceRValue;
8592 SourceLValue.setFrom(Ctx: Info.Ctx, V: SourceValue);
8593 if (!handleLValueToRValueConversion(
8594 Info, Conv: BCE, Type: BCE->getSubExpr()->getType().withConst(), LVal: SourceLValue,
8595 RVal&: SourceRValue, /*WantObjectRepresentation=*/true))
8596 return false;
8597
8598 return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE);
8599}
8600
8601template <class Derived>
8602class ExprEvaluatorBase
8603 : public ConstStmtVisitor<Derived, bool> {
8604private:
8605 Derived &getDerived() { return static_cast<Derived&>(*this); }
8606 bool DerivedSuccess(const APValue &V, const Expr *E) {
8607 return getDerived().Success(V, E);
8608 }
8609 bool DerivedZeroInitialization(const Expr *E) {
8610 return getDerived().ZeroInitialization(E);
8611 }
8612
8613 // Check whether a conditional operator with a non-constant condition is a
8614 // potential constant expression. If neither arm is a potential constant
8615 // expression, then the conditional operator is not either.
8616 template<typename ConditionalOperator>
8617 void CheckPotentialConstantConditional(const ConditionalOperator *E) {
8618 assert(Info.checkingPotentialConstantExpression());
8619
8620 // Speculatively evaluate both arms.
8621 SmallVector<PartialDiagnosticAt, 8> Diag;
8622 {
8623 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8624 StmtVisitorTy::Visit(E->getFalseExpr());
8625 if (Diag.empty())
8626 return;
8627 }
8628
8629 {
8630 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8631 Diag.clear();
8632 Info.EvalStatus.DiagEmitted = false;
8633 StmtVisitorTy::Visit(E->getTrueExpr());
8634 if (Diag.empty())
8635 return;
8636 }
8637
8638 Error(E, diag::note_constexpr_conditional_never_const);
8639 }
8640
8641
8642 template<typename ConditionalOperator>
8643 bool HandleConditionalOperator(const ConditionalOperator *E) {
8644 bool BoolResult;
8645 if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
8646 if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) {
8647 CheckPotentialConstantConditional(E);
8648 return false;
8649 }
8650 if (Info.noteFailure()) {
8651 StmtVisitorTy::Visit(E->getTrueExpr());
8652 StmtVisitorTy::Visit(E->getFalseExpr());
8653 }
8654 return false;
8655 }
8656
8657 Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
8658 return StmtVisitorTy::Visit(EvalExpr);
8659 }
8660
8661protected:
8662 EvalInfo &Info;
8663 typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy;
8664 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
8665
8666 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
8667 return Info.CCEDiag(E, DiagId: D);
8668 }
8669
8670 bool ZeroInitialization(const Expr *E) { return Error(E); }
8671
8672 bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) {
8673 unsigned BuiltinOp = E->getBuiltinCallee();
8674 return BuiltinOp != 0 &&
8675 Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp);
8676 }
8677
8678public:
8679 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
8680
8681 EvalInfo &getEvalInfo() { return Info; }
8682
8683 /// Report an evaluation error. This should only be called when an error is
8684 /// first discovered. When propagating an error, just return false.
8685 bool Error(const Expr *E, diag::kind D) {
8686 Info.FFDiag(E, DiagId: D) << E->getSourceRange();
8687 return false;
8688 }
8689 bool Error(const Expr *E) {
8690 return Error(E, diag::note_invalid_subexpr_in_const_expr);
8691 }
8692
8693 bool VisitStmt(const Stmt *) {
8694 llvm_unreachable("Expression evaluator should not be called on stmts");
8695 }
8696 bool VisitExpr(const Expr *E) {
8697 return Error(E);
8698 }
8699
8700 bool VisitEmbedExpr(const EmbedExpr *E) {
8701 const auto It = E->begin();
8702 return StmtVisitorTy::Visit(*It);
8703 }
8704
8705 bool VisitPredefinedExpr(const PredefinedExpr *E) {
8706 return StmtVisitorTy::Visit(E->getFunctionName());
8707 }
8708 bool VisitConstantExpr(const ConstantExpr *E) {
8709 if (E->hasAPValueResult())
8710 return DerivedSuccess(V: E->getAPValueResult(), E);
8711
8712 return StmtVisitorTy::Visit(E->getSubExpr());
8713 }
8714
8715 bool VisitParenExpr(const ParenExpr *E)
8716 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8717 bool VisitUnaryExtension(const UnaryOperator *E)
8718 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8719 bool VisitUnaryPlus(const UnaryOperator *E)
8720 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8721 bool VisitChooseExpr(const ChooseExpr *E)
8722 { return StmtVisitorTy::Visit(E->getChosenSubExpr()); }
8723 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
8724 { return StmtVisitorTy::Visit(E->getResultExpr()); }
8725 bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
8726 { return StmtVisitorTy::Visit(E->getReplacement()); }
8727 bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
8728 TempVersionRAII RAII(*Info.CurrentCall);
8729 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8730 return StmtVisitorTy::Visit(E->getExpr());
8731 }
8732 bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
8733 TempVersionRAII RAII(*Info.CurrentCall);
8734 // The initializer may not have been parsed yet, or might be erroneous.
8735 if (!E->getExpr())
8736 return Error(E);
8737 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8738 return StmtVisitorTy::Visit(E->getExpr());
8739 }
8740
8741 bool VisitExprWithCleanups(const ExprWithCleanups *E) {
8742 FullExpressionRAII Scope(Info);
8743 return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy();
8744 }
8745
8746 // Temporaries are registered when created, so we don't care about
8747 // CXXBindTemporaryExpr.
8748 bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
8749 return StmtVisitorTy::Visit(E->getSubExpr());
8750 }
8751
8752 bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
8753 if (E->getCastKind() != CK_PointerToIntegral)
8754 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8755 << diag::ConstexprInvalidCastKind::Reinterpret;
8756 return static_cast<Derived*>(this)->VisitCastExpr(E);
8757 }
8758 bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
8759 if (!Info.Ctx.getLangOpts().CPlusPlus20)
8760 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8761 << diag::ConstexprInvalidCastKind::Dynamic;
8762 return static_cast<Derived*>(this)->VisitCastExpr(E);
8763 }
8764 bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
8765 return static_cast<Derived*>(this)->VisitCastExpr(E);
8766 }
8767
8768 bool VisitBinaryOperator(const BinaryOperator *E) {
8769 switch (E->getOpcode()) {
8770 default:
8771 return Error(E);
8772
8773 case BO_Comma:
8774 VisitIgnoredValue(E: E->getLHS());
8775 return StmtVisitorTy::Visit(E->getRHS());
8776
8777 case BO_PtrMemD:
8778 case BO_PtrMemI: {
8779 LValue Obj;
8780 if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj))
8781 return false;
8782 APValue Result;
8783 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: Obj, RVal&: Result))
8784 return false;
8785 return DerivedSuccess(V: Result, E);
8786 }
8787 }
8788 }
8789
8790 bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) {
8791 return StmtVisitorTy::Visit(E->getSemanticForm());
8792 }
8793
8794 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
8795 // Evaluate and cache the common expression. We treat it as a temporary,
8796 // even though it's not quite the same thing.
8797 LValue CommonLV;
8798 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8799 Key: E->getOpaqueValue(),
8800 T: getStorageType(Ctx: Info.Ctx, E: E->getOpaqueValue()),
8801 Scope: ScopeKind::FullExpression, LV&: CommonLV),
8802 Info, E: E->getCommon()))
8803 return false;
8804
8805 return HandleConditionalOperator(E);
8806 }
8807
8808 bool VisitConditionalOperator(const ConditionalOperator *E) {
8809 bool IsBcpCall = false;
8810 // If the condition (ignoring parens) is a __builtin_constant_p call,
8811 // the result is a constant expression if it can be folded without
8812 // side-effects. This is an important GNU extension. See GCC PR38377
8813 // for discussion.
8814 if (const CallExpr *CallCE =
8815 dyn_cast<CallExpr>(Val: E->getCond()->IgnoreParenCasts()))
8816 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
8817 IsBcpCall = true;
8818
8819 // Always assume __builtin_constant_p(...) ? ... : ... is a potential
8820 // constant expression; we can't check whether it's potentially foldable.
8821 // FIXME: We should instead treat __builtin_constant_p as non-constant if
8822 // it would return 'false' in this mode.
8823 if (Info.checkingPotentialConstantExpression() && IsBcpCall)
8824 return false;
8825
8826 FoldConstant Fold(Info, IsBcpCall);
8827 if (!HandleConditionalOperator(E)) {
8828 Fold.keepDiagnostics();
8829 return false;
8830 }
8831
8832 return true;
8833 }
8834
8835 bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
8836 if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E);
8837 Value && !Value->isAbsent())
8838 return DerivedSuccess(V: *Value, E);
8839
8840 const Expr *Source = E->getSourceExpr();
8841 if (!Source)
8842 return Error(E);
8843 if (Source == E) {
8844 assert(0 && "OpaqueValueExpr recursively refers to itself");
8845 return Error(E);
8846 }
8847 return StmtVisitorTy::Visit(Source);
8848 }
8849
8850 bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
8851 for (const Expr *SemE : E->semantics()) {
8852 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) {
8853 // FIXME: We can't handle the case where an OpaqueValueExpr is also the
8854 // result expression: there could be two different LValues that would
8855 // refer to the same object in that case, and we can't model that.
8856 if (SemE == E->getResultExpr())
8857 return Error(E);
8858
8859 // Unique OVEs get evaluated if and when we encounter them when
8860 // emitting the rest of the semantic form, rather than eagerly.
8861 if (OVE->isUnique())
8862 continue;
8863
8864 LValue LV;
8865 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8866 Key: OVE, T: getStorageType(Ctx: Info.Ctx, E: OVE),
8867 Scope: ScopeKind::FullExpression, LV),
8868 Info, E: OVE->getSourceExpr()))
8869 return false;
8870 } else if (SemE == E->getResultExpr()) {
8871 if (!StmtVisitorTy::Visit(SemE))
8872 return false;
8873 } else {
8874 if (!EvaluateIgnoredValue(Info, E: SemE))
8875 return false;
8876 }
8877 }
8878 return true;
8879 }
8880
8881 bool VisitCallExpr(const CallExpr *E) {
8882 APValue Result;
8883 if (!handleCallExpr(E, Result, ResultSlot: nullptr))
8884 return false;
8885 return DerivedSuccess(V: Result, E);
8886 }
8887
8888 bool handleCallExpr(const CallExpr *E, APValue &Result,
8889 const LValue *ResultSlot) {
8890 CallScopeRAII CallScope(Info);
8891
8892 const Expr *Callee = E->getCallee()->IgnoreParens();
8893 QualType CalleeType = Callee->getType();
8894
8895 const FunctionDecl *FD = nullptr;
8896 LValue *This = nullptr, ObjectArg;
8897 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
8898 bool HasQualifier = false;
8899
8900 CallRef Call;
8901
8902 // Extract function decl and 'this' pointer from the callee.
8903 if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) {
8904 const CXXMethodDecl *Member = nullptr;
8905 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: Callee)) {
8906 // Explicit bound member calls, such as x.f() or p->g();
8907 if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ObjectArg))
8908 return false;
8909 Member = dyn_cast<CXXMethodDecl>(Val: ME->getMemberDecl());
8910 if (!Member)
8911 return Error(Callee);
8912 This = &ObjectArg;
8913 HasQualifier = ME->hasQualifier();
8914 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Val: Callee)) {
8915 // Indirect bound member calls ('.*' or '->*').
8916 const ValueDecl *D =
8917 HandleMemberPointerAccess(Info, BO: BE, LV&: ObjectArg, IncludeMember: false);
8918 if (!D)
8919 return false;
8920 Member = dyn_cast<CXXMethodDecl>(Val: D);
8921 if (!Member)
8922 return Error(Callee);
8923 This = &ObjectArg;
8924 } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Val: Callee)) {
8925 if (!Info.getLangOpts().CPlusPlus20)
8926 Info.CCEDiag(E: PDE, DiagId: diag::note_constexpr_pseudo_destructor);
8927 return EvaluateObjectArgument(Info, Object: PDE->getBase(), This&: ObjectArg) &&
8928 HandleDestruction(Info, E: PDE, This: ObjectArg, ThisType: PDE->getDestroyedType());
8929 } else
8930 return Error(Callee);
8931 FD = Member;
8932 } else if (CalleeType->isFunctionPointerType()) {
8933 LValue CalleeLV;
8934 if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info))
8935 return false;
8936
8937 if (!CalleeLV.getLValueOffset().isZero())
8938 return Error(Callee);
8939 if (CalleeLV.isNullPointer()) {
8940 Info.FFDiag(E: Callee, DiagId: diag::note_constexpr_null_callee)
8941 << const_cast<Expr *>(Callee);
8942 return false;
8943 }
8944 FD = dyn_cast_or_null<FunctionDecl>(
8945 Val: CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>());
8946 if (!FD)
8947 return Error(Callee);
8948 // Don't call function pointers which have been cast to some other type.
8949 // Per DR (no number yet), the caller and callee can differ in noexcept.
8950 if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec(
8951 T: CalleeType->getPointeeType(), U: FD->getType())) {
8952 return Error(E);
8953 }
8954
8955 // For an (overloaded) assignment expression, evaluate the RHS before the
8956 // LHS.
8957 auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E);
8958 if (OCE && OCE->isAssignmentOp()) {
8959 assert(Args.size() == 2 && "wrong number of arguments in assignment");
8960 Call = Info.CurrentCall->createCall(Callee: FD);
8961 bool HasThis = false;
8962 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD))
8963 HasThis = MD->isImplicitObjectMemberFunction();
8964 if (!EvaluateArgs(Args: HasThis ? Args.slice(N: 1) : Args, Call, Info, Callee: FD,
8965 /*RightToLeft=*/true, ObjectArg: &ObjectArg))
8966 return false;
8967 }
8968
8969 // Overloaded operator calls to member functions are represented as normal
8970 // calls with '*this' as the first argument.
8971 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
8972 if (MD &&
8973 (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) {
8974 // FIXME: When selecting an implicit conversion for an overloaded
8975 // operator delete, we sometimes try to evaluate calls to conversion
8976 // operators without a 'this' parameter!
8977 if (Args.empty())
8978 return Error(E);
8979
8980 if (!EvaluateObjectArgument(Info, Object: Args[0], This&: ObjectArg))
8981 return false;
8982
8983 // If we are calling a static operator, the 'this' argument needs to be
8984 // ignored after being evaluated.
8985 if (MD->isInstance())
8986 This = &ObjectArg;
8987
8988 // If this is syntactically a simple assignment using a trivial
8989 // assignment operator, start the lifetimes of union members as needed,
8990 // per C++20 [class.union]5.
8991 if (Info.getLangOpts().CPlusPlus20 && OCE &&
8992 OCE->getOperator() == OO_Equal && MD->isTrivial() &&
8993 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: Args[0], LHS: ObjectArg))
8994 return false;
8995
8996 Args = Args.slice(N: 1);
8997 } else if (MD && MD->isLambdaStaticInvoker()) {
8998 // Map the static invoker for the lambda back to the call operator.
8999 // Conveniently, we don't have to slice out the 'this' argument (as is
9000 // being done for the non-static case), since a static member function
9001 // doesn't have an implicit argument passed in.
9002 const CXXRecordDecl *ClosureClass = MD->getParent();
9003 assert(
9004 ClosureClass->captures().empty() &&
9005 "Number of captures must be zero for conversion to function-ptr");
9006
9007 const CXXMethodDecl *LambdaCallOp =
9008 ClosureClass->getLambdaCallOperator();
9009
9010 // Set 'FD', the function that will be called below, to the call
9011 // operator. If the closure object represents a generic lambda, find
9012 // the corresponding specialization of the call operator.
9013
9014 if (ClosureClass->isGenericLambda()) {
9015 assert(MD->isFunctionTemplateSpecialization() &&
9016 "A generic lambda's static-invoker function must be a "
9017 "template specialization");
9018 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
9019 FunctionTemplateDecl *CallOpTemplate =
9020 LambdaCallOp->getDescribedFunctionTemplate();
9021 void *InsertPos = nullptr;
9022 FunctionDecl *CorrespondingCallOpSpecialization =
9023 CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos);
9024 assert(CorrespondingCallOpSpecialization &&
9025 "We must always have a function call operator specialization "
9026 "that corresponds to our static invoker specialization");
9027 assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization));
9028 FD = CorrespondingCallOpSpecialization;
9029 } else
9030 FD = LambdaCallOp;
9031 } else if (FD->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
9032 if (FD->getDeclName().isAnyOperatorNew()) {
9033 LValue Ptr;
9034 if (!HandleOperatorNewCall(Info, E, Result&: Ptr))
9035 return false;
9036 Ptr.moveInto(V&: Result);
9037 return CallScope.destroy();
9038 } else {
9039 return HandleOperatorDeleteCall(Info, E) && CallScope.destroy();
9040 }
9041 }
9042 } else
9043 return Error(E);
9044
9045 // Evaluate the arguments now if we've not already done so.
9046 if (!Call) {
9047 Call = Info.CurrentCall->createCall(Callee: FD);
9048 if (!EvaluateArgs(Args, Call, Info, Callee: FD, /*RightToLeft*/ false,
9049 ObjectArg: &ObjectArg))
9050 return false;
9051 }
9052
9053 SmallVector<QualType, 4> CovariantAdjustmentPath;
9054 if (This) {
9055 auto *NamedMember = dyn_cast<CXXMethodDecl>(Val: FD);
9056 if (NamedMember && NamedMember->isVirtual() && !HasQualifier) {
9057 // Perform virtual dispatch, if necessary.
9058 FD = HandleVirtualDispatch(Info, E, This&: *This, Found: NamedMember,
9059 CovariantAdjustmentPath);
9060 if (!FD)
9061 return false;
9062 } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) {
9063 // Check that the 'this' pointer points to an object of the right type.
9064 // FIXME: If this is an assignment operator call, we may need to change
9065 // the active union member before we check this.
9066 if (!checkNonVirtualMemberCallThisPointer(Info, E, This: *This, NamedMember))
9067 return false;
9068 }
9069 }
9070
9071 // Destructor calls are different enough that they have their own codepath.
9072 if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: FD)) {
9073 assert(This && "no 'this' pointer for destructor call");
9074 return HandleDestruction(Info, E, This: *This,
9075 ThisType: Info.Ctx.getCanonicalTagType(TD: DD->getParent())) &&
9076 CallScope.destroy();
9077 }
9078
9079 const FunctionDecl *Definition = nullptr;
9080 Stmt *Body = FD->getBody(Definition);
9081 SourceLocation Loc = E->getExprLoc();
9082
9083 // Treat the object argument as `this` when evaluating defaulted
9084 // special menmber functions
9085 if (FD->hasCXXExplicitFunctionObjectParameter())
9086 This = &ObjectArg;
9087
9088 if (!CheckConstexprFunction(Info, CallLoc: Loc, Declaration: FD, Definition, Body) ||
9089 !HandleFunctionCall(CallLoc: Loc, Callee: Definition, ObjectArg: This, E, Args, Call, Body, Info,
9090 Result, ResultSlot))
9091 return false;
9092
9093 if (!CovariantAdjustmentPath.empty() &&
9094 !HandleCovariantReturnAdjustment(Info, E, Result,
9095 Path: CovariantAdjustmentPath))
9096 return false;
9097
9098 return CallScope.destroy();
9099 }
9100
9101 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9102 return StmtVisitorTy::Visit(E->getInitializer());
9103 }
9104 bool VisitInitListExpr(const InitListExpr *E) {
9105 if (E->getNumInits() == 0)
9106 return DerivedZeroInitialization(E);
9107 if (E->getNumInits() == 1)
9108 return StmtVisitorTy::Visit(E->getInit(Init: 0));
9109 return Error(E);
9110 }
9111 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
9112 return DerivedZeroInitialization(E);
9113 }
9114 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
9115 return DerivedZeroInitialization(E);
9116 }
9117 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
9118 return DerivedZeroInitialization(E);
9119 }
9120
9121 /// A member expression where the object is a prvalue is itself a prvalue.
9122 bool VisitMemberExpr(const MemberExpr *E) {
9123 assert(!Info.Ctx.getLangOpts().CPlusPlus11 &&
9124 "missing temporary materialization conversion");
9125 assert(!E->isArrow() && "missing call to bound member function?");
9126
9127 APValue Val;
9128 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9129 return false;
9130
9131 QualType BaseTy = E->getBase()->getType();
9132
9133 const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
9134 if (!FD) return Error(E);
9135 assert(!FD->getType()->isReferenceType() && "prvalue reference?");
9136 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9137 FD->getParent()->getCanonicalDecl() &&
9138 "record / field mismatch");
9139
9140 // Note: there is no lvalue base here. But this case should only ever
9141 // happen in C or in C++98, where we cannot be evaluating a constexpr
9142 // constructor, which is the only case the base matters.
9143 CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy);
9144 SubobjectDesignator Designator(BaseTy);
9145 Designator.addDeclUnchecked(D: FD);
9146
9147 APValue Result;
9148 return extractSubobject(Info, E, Obj, Sub: Designator, Result) &&
9149 DerivedSuccess(V: Result, E);
9150 }
9151
9152 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) {
9153 APValue Val;
9154 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
9155 return false;
9156
9157 if (Val.isVector()) {
9158 SmallVector<uint32_t, 4> Indices;
9159 E->getEncodedElementAccess(Elts&: Indices);
9160 if (Indices.size() == 1) {
9161 // Return scalar.
9162 return DerivedSuccess(V: Val.getVectorElt(I: Indices[0]), E);
9163 } else {
9164 // Construct new APValue vector.
9165 SmallVector<APValue, 4> Elts;
9166 for (unsigned I = 0; I < Indices.size(); ++I) {
9167 Elts.push_back(Elt: Val.getVectorElt(I: Indices[I]));
9168 }
9169 APValue VecResult(Elts.data(), Indices.size());
9170 return DerivedSuccess(V: VecResult, E);
9171 }
9172 }
9173
9174 return false;
9175 }
9176
9177 bool VisitCastExpr(const CastExpr *E) {
9178 switch (E->getCastKind()) {
9179 default:
9180 break;
9181
9182 case CK_AtomicToNonAtomic: {
9183 APValue AtomicVal;
9184 // This does not need to be done in place even for class/array types:
9185 // atomic-to-non-atomic conversion implies copying the object
9186 // representation.
9187 if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr()))
9188 return false;
9189 return DerivedSuccess(V: AtomicVal, E);
9190 }
9191
9192 case CK_NoOp:
9193 case CK_UserDefinedConversion:
9194 return StmtVisitorTy::Visit(E->getSubExpr());
9195
9196 case CK_HLSLArrayRValue: {
9197 const Expr *SubExpr = E->getSubExpr();
9198 if (!SubExpr->isGLValue()) {
9199 APValue Val;
9200 if (!Evaluate(Result&: Val, Info, E: SubExpr))
9201 return false;
9202 return DerivedSuccess(V: Val, E);
9203 }
9204
9205 LValue LVal;
9206 if (!EvaluateLValue(E: SubExpr, Result&: LVal, Info))
9207 return false;
9208 APValue RVal;
9209 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9210 if (!handleLValueToRValueConversion(Info, Conv: E, Type: SubExpr->getType(), LVal,
9211 RVal))
9212 return false;
9213 return DerivedSuccess(V: RVal, E);
9214 }
9215 case CK_LValueToRValue: {
9216 LValue LVal;
9217 if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info))
9218 return false;
9219 APValue RVal;
9220 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9221 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
9222 LVal, RVal))
9223 return false;
9224 return DerivedSuccess(V: RVal, E);
9225 }
9226 case CK_LValueToRValueBitCast: {
9227 APValue DestValue, SourceValue;
9228 if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr()))
9229 return false;
9230 if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E))
9231 return false;
9232 return DerivedSuccess(V: DestValue, E);
9233 }
9234
9235 case CK_AddressSpaceConversion: {
9236 APValue Value;
9237 if (!Evaluate(Result&: Value, Info, E: E->getSubExpr()))
9238 return false;
9239 return DerivedSuccess(V: Value, E);
9240 }
9241 }
9242
9243 return Error(E);
9244 }
9245
9246 bool VisitUnaryPostInc(const UnaryOperator *UO) {
9247 return VisitUnaryPostIncDec(UO);
9248 }
9249 bool VisitUnaryPostDec(const UnaryOperator *UO) {
9250 return VisitUnaryPostIncDec(UO);
9251 }
9252 bool VisitUnaryPostIncDec(const UnaryOperator *UO) {
9253 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9254 return Error(UO);
9255
9256 LValue LVal;
9257 if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info))
9258 return false;
9259 APValue RVal;
9260 if (!handleIncDec(Info&: this->Info, E: UO, LVal, LValType: UO->getSubExpr()->getType(),
9261 IsIncrement: UO->isIncrementOp(), Old: &RVal))
9262 return false;
9263 return DerivedSuccess(V: RVal, E: UO);
9264 }
9265
9266 bool VisitStmtExpr(const StmtExpr *E) {
9267 // We will have checked the full-expressions inside the statement expression
9268 // when they were completed, and don't need to check them again now.
9269 llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior,
9270 false);
9271
9272 const CompoundStmt *CS = E->getSubStmt();
9273 if (CS->body_empty())
9274 return true;
9275
9276 BlockScopeRAII Scope(Info);
9277 for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
9278 BE = CS->body_end();
9279 /**/; ++BI) {
9280 if (BI + 1 == BE) {
9281 const Expr *FinalExpr = dyn_cast<Expr>(Val: *BI);
9282 if (!FinalExpr) {
9283 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9284 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9285 return false;
9286 }
9287 return this->Visit(FinalExpr) && Scope.destroy();
9288 }
9289
9290 APValue ReturnValue;
9291 StmtResult Result = { .Value: ReturnValue, .Slot: nullptr };
9292 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI);
9293 if (ESR != ESR_Succeeded) {
9294 // FIXME: If the statement-expression terminated due to 'return',
9295 // 'break', or 'continue', it would be nice to propagate that to
9296 // the outer statement evaluation rather than bailing out.
9297 if (ESR != ESR_Failed)
9298 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9299 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9300 return false;
9301 }
9302 }
9303
9304 llvm_unreachable("Return from function from the loop above.");
9305 }
9306
9307 bool VisitPackIndexingExpr(const PackIndexingExpr *E) {
9308 return StmtVisitorTy::Visit(E->getSelectedExpr());
9309 }
9310
9311 /// Visit a value which is evaluated, but whose value is ignored.
9312 void VisitIgnoredValue(const Expr *E) {
9313 EvaluateIgnoredValue(Info, E);
9314 }
9315
9316 /// Potentially visit a MemberExpr's base expression.
9317 void VisitIgnoredBaseExpression(const Expr *E) {
9318 // While MSVC doesn't evaluate the base expression, it does diagnose the
9319 // presence of side-effecting behavior.
9320 if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx))
9321 return;
9322 VisitIgnoredValue(E);
9323 }
9324};
9325
9326} // namespace
9327
9328//===----------------------------------------------------------------------===//
9329// Common base class for lvalue and temporary evaluation.
9330//===----------------------------------------------------------------------===//
9331namespace {
9332template<class Derived>
9333class LValueExprEvaluatorBase
9334 : public ExprEvaluatorBase<Derived> {
9335protected:
9336 LValue &Result;
9337 bool InvalidBaseOK;
9338 typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
9339 typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy;
9340
9341 bool Success(APValue::LValueBase B) {
9342 Result.set(B);
9343 return true;
9344 }
9345
9346 bool evaluatePointer(const Expr *E, LValue &Result) {
9347 return EvaluatePointer(E, Result, this->Info, InvalidBaseOK);
9348 }
9349
9350public:
9351 LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK)
9352 : ExprEvaluatorBaseTy(Info), Result(Result),
9353 InvalidBaseOK(InvalidBaseOK) {}
9354
9355 bool Success(const APValue &V, const Expr *E) {
9356 Result.setFrom(Ctx: this->Info.Ctx, V);
9357 return true;
9358 }
9359
9360 bool VisitMemberExpr(const MemberExpr *E) {
9361 // Handle non-static data members.
9362 QualType BaseTy;
9363 bool EvalOK;
9364 if (E->isArrow()) {
9365 EvalOK = evaluatePointer(E: E->getBase(), Result);
9366 BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType();
9367 } else if (E->getBase()->isPRValue()) {
9368 assert(E->getBase()->getType()->isRecordType());
9369 EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info);
9370 BaseTy = E->getBase()->getType();
9371 } else {
9372 EvalOK = this->Visit(E->getBase());
9373 BaseTy = E->getBase()->getType();
9374 }
9375 if (!EvalOK) {
9376 if (!InvalidBaseOK)
9377 return false;
9378 Result.setInvalid(B: E);
9379 return true;
9380 }
9381
9382 const ValueDecl *MD = E->getMemberDecl();
9383 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) {
9384 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9385 FD->getParent()->getCanonicalDecl() &&
9386 "record / field mismatch");
9387 (void)BaseTy;
9388 if (!HandleLValueMember(this->Info, E, Result, FD))
9389 return false;
9390 } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) {
9391 if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))
9392 return false;
9393 } else
9394 return this->Error(E);
9395
9396 if (MD->getType()->isReferenceType()) {
9397 APValue RefValue;
9398 if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
9399 RefValue))
9400 return false;
9401 return Success(RefValue, E);
9402 }
9403 return true;
9404 }
9405
9406 bool VisitBinaryOperator(const BinaryOperator *E) {
9407 switch (E->getOpcode()) {
9408 default:
9409 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
9410
9411 case BO_PtrMemD:
9412 case BO_PtrMemI:
9413 return HandleMemberPointerAccess(this->Info, E, Result);
9414 }
9415 }
9416
9417 bool VisitCastExpr(const CastExpr *E) {
9418 switch (E->getCastKind()) {
9419 default:
9420 return ExprEvaluatorBaseTy::VisitCastExpr(E);
9421
9422 case CK_DerivedToBase:
9423 case CK_UncheckedDerivedToBase:
9424 if (!this->Visit(E->getSubExpr()))
9425 return false;
9426
9427 // Now figure out the necessary offset to add to the base LV to get from
9428 // the derived class to the base class.
9429 return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(),
9430 Result);
9431 }
9432 }
9433};
9434}
9435
9436//===----------------------------------------------------------------------===//
9437// LValue Evaluation
9438//
9439// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
9440// function designators (in C), decl references to void objects (in C), and
9441// temporaries (if building with -Wno-address-of-temporary).
9442//
9443// LValue evaluation produces values comprising a base expression of one of the
9444// following types:
9445// - Declarations
9446// * VarDecl
9447// * FunctionDecl
9448// - Literals
9449// * CompoundLiteralExpr in C (and in global scope in C++)
9450// * StringLiteral
9451// * PredefinedExpr
9452// * ObjCStringLiteralExpr
9453// * ObjCEncodeExpr
9454// * AddrLabelExpr
9455// * BlockExpr
9456// * CallExpr for a MakeStringConstant builtin
9457// - typeid(T) expressions, as TypeInfoLValues
9458// - Locals and temporaries
9459// * MaterializeTemporaryExpr
9460// * Any Expr, with a CallIndex indicating the function in which the temporary
9461// was evaluated, for cases where the MaterializeTemporaryExpr is missing
9462// from the AST (FIXME).
9463// * A MaterializeTemporaryExpr that has static storage duration, with no
9464// CallIndex, for a lifetime-extended temporary.
9465// * The ConstantExpr that is currently being evaluated during evaluation of an
9466// immediate invocation.
9467// plus an offset in bytes.
9468//===----------------------------------------------------------------------===//
9469namespace {
9470class LValueExprEvaluator
9471 : public LValueExprEvaluatorBase<LValueExprEvaluator> {
9472public:
9473 LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) :
9474 LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {}
9475
9476 bool VisitVarDecl(const Expr *E, const VarDecl *VD);
9477 bool VisitUnaryPreIncDec(const UnaryOperator *UO);
9478
9479 bool VisitCallExpr(const CallExpr *E);
9480 bool VisitDeclRefExpr(const DeclRefExpr *E);
9481 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(B: E); }
9482 bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
9483 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
9484 bool VisitMemberExpr(const MemberExpr *E);
9485 bool VisitStringLiteral(const StringLiteral *E) {
9486 return Success(
9487 B: APValue::LValueBase(E, 0, Info.Ctx.getNextStringLiteralVersion()));
9488 }
9489 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(B: E); }
9490 bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
9491 bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
9492 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
9493 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);
9494 bool VisitUnaryDeref(const UnaryOperator *E);
9495 bool VisitUnaryReal(const UnaryOperator *E);
9496 bool VisitUnaryImag(const UnaryOperator *E);
9497 bool VisitUnaryPreInc(const UnaryOperator *UO) {
9498 return VisitUnaryPreIncDec(UO);
9499 }
9500 bool VisitUnaryPreDec(const UnaryOperator *UO) {
9501 return VisitUnaryPreIncDec(UO);
9502 }
9503 bool VisitBinAssign(const BinaryOperator *BO);
9504 bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO);
9505
9506 bool VisitCastExpr(const CastExpr *E) {
9507 switch (E->getCastKind()) {
9508 default:
9509 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
9510
9511 case CK_LValueBitCast:
9512 this->CCEDiag(E, D: diag::note_constexpr_invalid_cast)
9513 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9514 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
9515 if (!Visit(S: E->getSubExpr()))
9516 return false;
9517 Result.Designator.setInvalid();
9518 return true;
9519
9520 case CK_BaseToDerived:
9521 if (!Visit(S: E->getSubExpr()))
9522 return false;
9523 return HandleBaseToDerivedCast(Info, E, Result);
9524
9525 case CK_Dynamic:
9526 if (!Visit(S: E->getSubExpr()))
9527 return false;
9528 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
9529 }
9530 }
9531};
9532} // end anonymous namespace
9533
9534/// Get an lvalue to a field of a lambda's closure type.
9535static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result,
9536 const CXXMethodDecl *MD, const FieldDecl *FD,
9537 bool LValueToRValueConversion) {
9538 // Static lambda function call operators can't have captures. We already
9539 // diagnosed this, so bail out here.
9540 if (MD->isStatic()) {
9541 assert(Info.CurrentCall->This == nullptr &&
9542 "This should not be set for a static call operator");
9543 return false;
9544 }
9545
9546 // Start with 'Result' referring to the complete closure object...
9547 if (MD->isExplicitObjectMemberFunction()) {
9548 // Self may be passed by reference or by value.
9549 const ParmVarDecl *Self = MD->getParamDecl(i: 0);
9550 if (Self->getType()->isReferenceType()) {
9551 APValue *RefValue = Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: Self);
9552 if (!RefValue->allowConstexprUnknown() || RefValue->hasValue())
9553 Result.setFrom(Ctx: Info.Ctx, V: *RefValue);
9554 } else {
9555 const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(PVD: Self);
9556 CallStackFrame *Frame =
9557 Info.getCallFrameAndDepth(CallIndex: Info.CurrentCall->Arguments.CallIndex)
9558 .first;
9559 unsigned Version = Info.CurrentCall->Arguments.Version;
9560 Result.set(B: {VD, Frame->Index, Version});
9561 }
9562 } else
9563 Result = *Info.CurrentCall->This;
9564
9565 // ... then update it to refer to the field of the closure object
9566 // that represents the capture.
9567 if (!HandleLValueMember(Info, E, LVal&: Result, FD))
9568 return false;
9569
9570 // And if the field is of reference type (or if we captured '*this' by
9571 // reference), update 'Result' to refer to what
9572 // the field refers to.
9573 if (LValueToRValueConversion) {
9574 APValue RVal;
9575 if (!handleLValueToRValueConversion(Info, Conv: E, Type: FD->getType(), LVal: Result, RVal))
9576 return false;
9577 Result.setFrom(Ctx: Info.Ctx, V: RVal);
9578 }
9579 return true;
9580}
9581
9582/// Evaluate an expression as an lvalue. This can be legitimately called on
9583/// expressions which are not glvalues, in three cases:
9584/// * function designators in C, and
9585/// * "extern void" objects
9586/// * @selector() expressions in Objective-C
9587static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
9588 bool InvalidBaseOK) {
9589 assert(!E->isValueDependent());
9590 assert(E->isGLValue() || E->getType()->isFunctionType() ||
9591 E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens()));
9592 return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
9593}
9594
9595bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
9596 const ValueDecl *D = E->getDecl();
9597
9598 // If we are within a lambda's call operator, check whether the 'VD' referred
9599 // to within 'E' actually represents a lambda-capture that maps to a
9600 // data-member/field within the closure object, and if so, evaluate to the
9601 // field or what the field refers to.
9602 if (Info.CurrentCall && isLambdaCallOperator(DC: Info.CurrentCall->Callee) &&
9603 E->refersToEnclosingVariableOrCapture()) {
9604 // We don't always have a complete capture-map when checking or inferring if
9605 // the function call operator meets the requirements of a constexpr function
9606 // - but we don't need to evaluate the captures to determine constexprness
9607 // (dcl.constexpr C++17).
9608 if (Info.checkingPotentialConstantExpression())
9609 return false;
9610
9611 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(Val: D)) {
9612 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
9613 return HandleLambdaCapture(Info, E, Result, MD, FD,
9614 LValueToRValueConversion: FD->getType()->isReferenceType());
9615 }
9616 }
9617
9618 if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl,
9619 UnnamedGlobalConstantDecl>(Val: D))
9620 return Success(B: cast<ValueDecl>(Val: D));
9621 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
9622 return VisitVarDecl(E, VD);
9623 if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D))
9624 return Visit(S: BD->getBinding());
9625 return Error(E);
9626}
9627
9628bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
9629 CallStackFrame *Frame = nullptr;
9630 unsigned Version = 0;
9631 if (VD->hasLocalStorage()) {
9632 // Only if a local variable was declared in the function currently being
9633 // evaluated, do we expect to be able to find its value in the current
9634 // frame. (Otherwise it was likely declared in an enclosing context and
9635 // could either have a valid evaluatable value (for e.g. a constexpr
9636 // variable) or be ill-formed (and trigger an appropriate evaluation
9637 // diagnostic)).
9638 CallStackFrame *CurrFrame = Info.CurrentCall;
9639 if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) {
9640 // Function parameters are stored in some caller's frame. (Usually the
9641 // immediate caller, but for an inherited constructor they may be more
9642 // distant.)
9643 if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) {
9644 if (CurrFrame->Arguments) {
9645 VD = CurrFrame->Arguments.getOrigParam(PVD);
9646 Frame =
9647 Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first;
9648 Version = CurrFrame->Arguments.Version;
9649 }
9650 } else {
9651 Frame = CurrFrame;
9652 Version = CurrFrame->getCurrentTemporaryVersion(Key: VD);
9653 }
9654 }
9655 }
9656
9657 if (!VD->getType()->isReferenceType()) {
9658 if (Frame) {
9659 Result.set(B: {VD, Frame->Index, Version});
9660 return true;
9661 }
9662 return Success(B: VD);
9663 }
9664
9665 if (!Info.getLangOpts().CPlusPlus11) {
9666 Info.CCEDiag(E, DiagId: diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
9667 << VD << VD->getType();
9668 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
9669 }
9670
9671 APValue *V;
9672 if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V))
9673 return false;
9674
9675 if (!V) {
9676 Result.set(B: VD);
9677 Result.AllowConstexprUnknown = true;
9678 return true;
9679 }
9680
9681 return Success(V: *V, E);
9682}
9683
9684bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) {
9685 if (!IsConstantEvaluatedBuiltinCall(E))
9686 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9687
9688 switch (E->getBuiltinCallee()) {
9689 default:
9690 return false;
9691 case Builtin::BIas_const:
9692 case Builtin::BIforward:
9693 case Builtin::BIforward_like:
9694 case Builtin::BImove:
9695 case Builtin::BImove_if_noexcept:
9696 if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr())
9697 return Visit(S: E->getArg(Arg: 0));
9698 break;
9699 }
9700
9701 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9702}
9703
9704bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
9705 const MaterializeTemporaryExpr *E) {
9706 // Walk through the expression to find the materialized temporary itself.
9707 SmallVector<const Expr *, 2> CommaLHSs;
9708 SmallVector<SubobjectAdjustment, 2> Adjustments;
9709 const Expr *Inner =
9710 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments);
9711
9712 // If we passed any comma operators, evaluate their LHSs.
9713 for (const Expr *E : CommaLHSs)
9714 if (!EvaluateIgnoredValue(Info, E))
9715 return false;
9716
9717 // A materialized temporary with static storage duration can appear within the
9718 // result of a constant expression evaluation, so we need to preserve its
9719 // value for use outside this evaluation.
9720 APValue *Value;
9721 if (E->getStorageDuration() == SD_Static) {
9722 if (Info.EvalMode == EvaluationMode::ConstantFold)
9723 return false;
9724 // FIXME: What about SD_Thread?
9725 Value = E->getOrCreateValue(MayCreate: true);
9726 *Value = APValue();
9727 Result.set(B: E);
9728 } else {
9729 Value = &Info.CurrentCall->createTemporary(
9730 Key: E, T: Inner->getType(),
9731 Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression
9732 : ScopeKind::Block,
9733 LV&: Result);
9734 }
9735
9736 QualType Type = Inner->getType();
9737
9738 // Materialize the temporary itself.
9739 if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) {
9740 *Value = APValue();
9741 return false;
9742 }
9743
9744 // Adjust our lvalue to refer to the desired subobject.
9745 for (unsigned I = Adjustments.size(); I != 0; /**/) {
9746 --I;
9747 switch (Adjustments[I].Kind) {
9748 case SubobjectAdjustment::DerivedToBaseAdjustment:
9749 if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath,
9750 Type, Result))
9751 return false;
9752 Type = Adjustments[I].DerivedToBase.BasePath->getType();
9753 break;
9754
9755 case SubobjectAdjustment::FieldAdjustment:
9756 if (!HandleLValueMember(Info, E, LVal&: Result, FD: Adjustments[I].Field))
9757 return false;
9758 Type = Adjustments[I].Field->getType();
9759 break;
9760
9761 case SubobjectAdjustment::MemberPointerAdjustment:
9762 if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result,
9763 RHS: Adjustments[I].Ptr.RHS))
9764 return false;
9765 Type = Adjustments[I].Ptr.MPT->getPointeeType();
9766 break;
9767 }
9768 }
9769
9770 return true;
9771}
9772
9773bool
9774LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9775 assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) &&
9776 "lvalue compound literal in c++?");
9777 APValue *Lit;
9778 // If CompountLiteral has static storage, its value can be used outside
9779 // this expression. So evaluate it once and store it in ASTContext.
9780 if (E->hasStaticStorage()) {
9781 Lit = &E->getOrCreateStaticValue(Ctx&: Info.Ctx);
9782 Result.set(B: E);
9783 // Reset any previously evaluated state, otherwise evaluation below might
9784 // fail.
9785 // FIXME: Should we just re-use the previously evaluated value instead?
9786 *Lit = APValue();
9787 } else {
9788 assert(!Info.getLangOpts().CPlusPlus);
9789 Lit = &Info.CurrentCall->createTemporary(Key: E, T: E->getInitializer()->getType(),
9790 Scope: ScopeKind::Block, LV&: Result);
9791 }
9792 // FIXME: Evaluating in place isn't always right. We should figure out how to
9793 // use appropriate evaluation context here, see
9794 // clang/test/AST/static-compound-literals-reeval.cpp for a failure.
9795 if (!EvaluateInPlace(Result&: *Lit, Info, This: Result, E: E->getInitializer())) {
9796 *Lit = APValue();
9797 return false;
9798 }
9799 return true;
9800}
9801
9802bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
9803 TypeInfoLValue TypeInfo;
9804
9805 if (!E->isPotentiallyEvaluated()) {
9806 if (E->isTypeOperand())
9807 TypeInfo = TypeInfoLValue(E->getTypeOperand(Context: Info.Ctx).getTypePtr());
9808 else
9809 TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr());
9810 } else {
9811 if (!Info.Ctx.getLangOpts().CPlusPlus20) {
9812 Info.CCEDiag(E, DiagId: diag::note_constexpr_typeid_polymorphic)
9813 << E->getExprOperand()->getType()
9814 << E->getExprOperand()->getSourceRange();
9815 }
9816
9817 if (!Visit(S: E->getExprOperand()))
9818 return false;
9819
9820 std::optional<DynamicType> DynType =
9821 ComputeDynamicType(Info, E, This&: Result, AK: AK_TypeId);
9822 if (!DynType)
9823 return false;
9824
9825 TypeInfo = TypeInfoLValue(
9826 Info.Ctx.getCanonicalTagType(TD: DynType->Type).getTypePtr());
9827 }
9828
9829 return Success(B: APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType()));
9830}
9831
9832bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
9833 return Success(B: E->getGuidDecl());
9834}
9835
9836bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
9837 // Handle static data members.
9838 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) {
9839 VisitIgnoredBaseExpression(E: E->getBase());
9840 return VisitVarDecl(E, VD);
9841 }
9842
9843 // Handle static member functions.
9844 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) {
9845 if (MD->isStatic()) {
9846 VisitIgnoredBaseExpression(E: E->getBase());
9847 return Success(B: MD);
9848 }
9849 }
9850
9851 // Handle non-static data members.
9852 return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
9853}
9854
9855bool LValueExprEvaluator::VisitExtVectorElementExpr(
9856 const ExtVectorElementExpr *E) {
9857 bool Success = true;
9858
9859 APValue Val;
9860 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9861 if (!Info.noteFailure())
9862 return false;
9863 Success = false;
9864 }
9865
9866 SmallVector<uint32_t, 4> Indices;
9867 E->getEncodedElementAccess(Elts&: Indices);
9868 // FIXME: support accessing more than one element
9869 if (Indices.size() > 1)
9870 return false;
9871
9872 if (Success) {
9873 Result.setFrom(Ctx: Info.Ctx, V: Val);
9874 QualType BaseType = E->getBase()->getType();
9875 if (E->isArrow())
9876 BaseType = BaseType->getPointeeType();
9877 const auto *VT = BaseType->castAs<VectorType>();
9878 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9879 Size: VT->getNumElements(), Idx: Indices[0]);
9880 }
9881
9882 return Success;
9883}
9884
9885bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
9886 if (E->getBase()->getType()->isSveVLSBuiltinType())
9887 return Error(E);
9888
9889 APSInt Index;
9890 bool Success = true;
9891
9892 if (const auto *VT = E->getBase()->getType()->getAs<VectorType>()) {
9893 APValue Val;
9894 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9895 if (!Info.noteFailure())
9896 return false;
9897 Success = false;
9898 }
9899
9900 if (!EvaluateInteger(E: E->getIdx(), Result&: Index, Info)) {
9901 if (!Info.noteFailure())
9902 return false;
9903 Success = false;
9904 }
9905
9906 if (Success) {
9907 Result.setFrom(Ctx: Info.Ctx, V: Val);
9908 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9909 Size: VT->getNumElements(), Idx: Index.getZExtValue());
9910 }
9911
9912 return Success;
9913 }
9914
9915 // C++17's rules require us to evaluate the LHS first, regardless of which
9916 // side is the base.
9917 for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) {
9918 if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result)
9919 : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) {
9920 if (!Info.noteFailure())
9921 return false;
9922 Success = false;
9923 }
9924 }
9925
9926 return Success &&
9927 HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: E->getType(), Adjustment: Index);
9928}
9929
9930bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
9931 bool Success = evaluatePointer(E: E->getSubExpr(), Result);
9932 // [C++26][expr.unary.op]
9933 // If the operand points to an object or function, the result
9934 // denotes that object or function; otherwise, the behavior is undefined.
9935 // Because &(*(type*)0) is a common pattern, we do not fail the evaluation
9936 // immediately.
9937 if (!Success || !E->getType().getNonReferenceType()->isObjectType())
9938 return Success;
9939 return bool(findCompleteObject(Info, E, AK: AK_Dereference, LVal: Result,
9940 LValType: E->getType())) ||
9941 Info.noteUndefinedBehavior();
9942}
9943
9944bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
9945 if (!Visit(S: E->getSubExpr()))
9946 return false;
9947 // __real is a no-op on scalar lvalues.
9948 if (E->getSubExpr()->getType()->isAnyComplexType())
9949 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: false);
9950 return true;
9951}
9952
9953bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
9954 assert(E->getSubExpr()->getType()->isAnyComplexType() &&
9955 "lvalue __imag__ on scalar?");
9956 if (!Visit(S: E->getSubExpr()))
9957 return false;
9958 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: true);
9959 return true;
9960}
9961
9962bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) {
9963 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9964 return Error(E: UO);
9965
9966 if (!this->Visit(S: UO->getSubExpr()))
9967 return false;
9968
9969 return handleIncDec(
9970 Info&: this->Info, E: UO, LVal: Result, LValType: UO->getSubExpr()->getType(),
9971 IsIncrement: UO->isIncrementOp(), Old: nullptr);
9972}
9973
9974bool LValueExprEvaluator::VisitCompoundAssignOperator(
9975 const CompoundAssignOperator *CAO) {
9976 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9977 return Error(E: CAO);
9978
9979 bool Success = true;
9980
9981 // C++17 onwards require that we evaluate the RHS first.
9982 APValue RHS;
9983 if (!Evaluate(Result&: RHS, Info&: this->Info, E: CAO->getRHS())) {
9984 if (!Info.noteFailure())
9985 return false;
9986 Success = false;
9987 }
9988
9989 // The overall lvalue result is the result of evaluating the LHS.
9990 if (!this->Visit(S: CAO->getLHS()) || !Success)
9991 return false;
9992
9993 return handleCompoundAssignment(
9994 Info&: this->Info, E: CAO,
9995 LVal: Result, LValType: CAO->getLHS()->getType(), PromotedLValType: CAO->getComputationLHSType(),
9996 Opcode: CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RVal: RHS);
9997}
9998
9999bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
10000 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
10001 return Error(E);
10002
10003 bool Success = true;
10004
10005 // C++17 onwards require that we evaluate the RHS first.
10006 APValue NewVal;
10007 if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) {
10008 if (!Info.noteFailure())
10009 return false;
10010 Success = false;
10011 }
10012
10013 if (!this->Visit(S: E->getLHS()) || !Success)
10014 return false;
10015
10016 if (Info.getLangOpts().CPlusPlus20 &&
10017 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result))
10018 return false;
10019
10020 return handleAssignment(Info&: this->Info, E, LVal: Result, LValType: E->getLHS()->getType(),
10021 Val&: NewVal);
10022}
10023
10024//===----------------------------------------------------------------------===//
10025// Pointer Evaluation
10026//===----------------------------------------------------------------------===//
10027
10028/// Convenience function. LVal's base must be a call to an alloc_size
10029/// function.
10030static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
10031 const LValue &LVal,
10032 llvm::APInt &Result) {
10033 assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) &&
10034 "Can't get the size of a non alloc_size function");
10035 const auto *Base = LVal.getLValueBase().get<const Expr *>();
10036 const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base);
10037 std::optional<llvm::APInt> Size =
10038 CE->evaluateBytesReturnedByAllocSizeCall(Ctx);
10039 if (!Size)
10040 return false;
10041
10042 Result = std::move(*Size);
10043 return true;
10044}
10045
10046/// Attempts to evaluate the given LValueBase as the result of a call to
10047/// a function with the alloc_size attribute. If it was possible to do so, this
10048/// function will return true, make Result's Base point to said function call,
10049/// and mark Result's Base as invalid.
10050static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base,
10051 LValue &Result) {
10052 if (Base.isNull())
10053 return false;
10054
10055 // Because we do no form of static analysis, we only support const variables.
10056 //
10057 // Additionally, we can't support parameters, nor can we support static
10058 // variables (in the latter case, use-before-assign isn't UB; in the former,
10059 // we have no clue what they'll be assigned to).
10060 const auto *VD =
10061 dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>());
10062 if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified())
10063 return false;
10064
10065 const Expr *Init = VD->getAnyInitializer();
10066 if (!Init || Init->getType().isNull())
10067 return false;
10068
10069 const Expr *E = Init->IgnoreParens();
10070 if (!tryUnwrapAllocSizeCall(E))
10071 return false;
10072
10073 // Store E instead of E unwrapped so that the type of the LValue's base is
10074 // what the user wanted.
10075 Result.setInvalid(B: E);
10076
10077 QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType();
10078 Result.addUnsizedArray(Info, E, ElemTy: Pointee);
10079 return true;
10080}
10081
10082namespace {
10083class PointerExprEvaluator
10084 : public ExprEvaluatorBase<PointerExprEvaluator> {
10085 LValue &Result;
10086 bool InvalidBaseOK;
10087
10088 bool Success(const Expr *E) {
10089 Result.set(B: E);
10090 return true;
10091 }
10092
10093 bool evaluateLValue(const Expr *E, LValue &Result) {
10094 return EvaluateLValue(E, Result, Info, InvalidBaseOK);
10095 }
10096
10097 bool evaluatePointer(const Expr *E, LValue &Result) {
10098 return EvaluatePointer(E, Result, Info, InvalidBaseOK);
10099 }
10100
10101 bool visitNonBuiltinCallExpr(const CallExpr *E);
10102public:
10103
10104 PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK)
10105 : ExprEvaluatorBaseTy(info), Result(Result),
10106 InvalidBaseOK(InvalidBaseOK) {}
10107
10108 bool Success(const APValue &V, const Expr *E) {
10109 Result.setFrom(Ctx: Info.Ctx, V);
10110 return true;
10111 }
10112 bool ZeroInitialization(const Expr *E) {
10113 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10114 return true;
10115 }
10116
10117 bool VisitBinaryOperator(const BinaryOperator *E);
10118 bool VisitCastExpr(const CastExpr* E);
10119 bool VisitUnaryAddrOf(const UnaryOperator *E);
10120 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
10121 { return Success(E); }
10122 bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
10123 if (E->isExpressibleAsConstantInitializer())
10124 return Success(E);
10125 if (Info.noteFailure())
10126 EvaluateIgnoredValue(Info, E: E->getSubExpr());
10127 return Error(E);
10128 }
10129 bool VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
10130 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10131 }
10132 bool VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
10133 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
10134 }
10135 bool VisitAddrLabelExpr(const AddrLabelExpr *E)
10136 { return Success(E); }
10137 bool VisitCallExpr(const CallExpr *E);
10138 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
10139 bool VisitBlockExpr(const BlockExpr *E) {
10140 if (!E->getBlockDecl()->hasCaptures())
10141 return Success(E);
10142 return Error(E);
10143 }
10144 bool VisitCXXThisExpr(const CXXThisExpr *E) {
10145 auto DiagnoseInvalidUseOfThis = [&] {
10146 if (Info.getLangOpts().CPlusPlus11)
10147 Info.FFDiag(E, DiagId: diag::note_constexpr_this) << E->isImplicit();
10148 else
10149 Info.FFDiag(E);
10150 };
10151
10152 // Can't look at 'this' when checking a potential constant expression.
10153 if (Info.checkingPotentialConstantExpression())
10154 return false;
10155
10156 bool IsExplicitLambda =
10157 isLambdaCallWithExplicitObjectParameter(DC: Info.CurrentCall->Callee);
10158 if (!IsExplicitLambda) {
10159 if (!Info.CurrentCall->This) {
10160 DiagnoseInvalidUseOfThis();
10161 return false;
10162 }
10163
10164 Result = *Info.CurrentCall->This;
10165 }
10166
10167 if (isLambdaCallOperator(DC: Info.CurrentCall->Callee)) {
10168 // Ensure we actually have captured 'this'. If something was wrong with
10169 // 'this' capture, the error would have been previously reported.
10170 // Otherwise we can be inside of a default initialization of an object
10171 // declared by lambda's body, so no need to return false.
10172 if (!Info.CurrentCall->LambdaThisCaptureField) {
10173 if (IsExplicitLambda && !Info.CurrentCall->This) {
10174 DiagnoseInvalidUseOfThis();
10175 return false;
10176 }
10177
10178 return true;
10179 }
10180
10181 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
10182 return HandleLambdaCapture(
10183 Info, E, Result, MD, FD: Info.CurrentCall->LambdaThisCaptureField,
10184 LValueToRValueConversion: Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType());
10185 }
10186 return true;
10187 }
10188
10189 bool VisitCXXNewExpr(const CXXNewExpr *E);
10190
10191 bool VisitSourceLocExpr(const SourceLocExpr *E) {
10192 assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?");
10193 APValue LValResult = E->EvaluateInContext(
10194 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
10195 Result.setFrom(Ctx: Info.Ctx, V: LValResult);
10196 return true;
10197 }
10198
10199 bool VisitEmbedExpr(const EmbedExpr *E) {
10200 llvm::report_fatal_error(reason: "Not yet implemented for ExprConstant.cpp");
10201 return true;
10202 }
10203
10204 bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) {
10205 std::string ResultStr = E->ComputeName(Context&: Info.Ctx);
10206
10207 QualType CharTy = Info.Ctx.CharTy.withConst();
10208 APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()),
10209 ResultStr.size() + 1);
10210 QualType ArrayTy = Info.Ctx.getConstantArrayType(
10211 EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
10212
10213 StringLiteral *SL =
10214 StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary,
10215 /*Pascal*/ false, Ty: ArrayTy, Locs: E->getLocation());
10216
10217 evaluateLValue(E: SL, Result);
10218 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: ArrayTy));
10219 return true;
10220 }
10221
10222 // FIXME: Missing: @protocol, @selector
10223};
10224} // end anonymous namespace
10225
10226static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info,
10227 bool InvalidBaseOK) {
10228 assert(!E->isValueDependent());
10229 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
10230 return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
10231}
10232
10233bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
10234 if (E->getOpcode() != BO_Add &&
10235 E->getOpcode() != BO_Sub)
10236 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
10237
10238 const Expr *PExp = E->getLHS();
10239 const Expr *IExp = E->getRHS();
10240 if (IExp->getType()->isPointerType())
10241 std::swap(a&: PExp, b&: IExp);
10242
10243 bool EvalPtrOK = evaluatePointer(E: PExp, Result);
10244 if (!EvalPtrOK && !Info.noteFailure())
10245 return false;
10246
10247 llvm::APSInt Offset;
10248 if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK)
10249 return false;
10250
10251 if (E->getOpcode() == BO_Sub)
10252 negateAsSigned(Int&: Offset);
10253
10254 QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType();
10255 return HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: Pointee, Adjustment: Offset);
10256}
10257
10258bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
10259 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
10260 // operator, neither operator is evaluated and the result is as if both were
10261 // omitted (except that the operators' constraints, already enforced by Sema,
10262 // still apply, and the result is not an lvalue). So '&*p' is just the pointer
10263 // value 'p' with no dereference, and forming it is therefore not undefined
10264 // behavior even when 'p' is null, e.g. '&*(int *)0'. Evaluate the pointer
10265 // operand directly so we don't spuriously diagnose a null dereference.
10266 if (!Info.getLangOpts().CPlusPlus) {
10267 const Expr *Sub = E->getSubExpr()->IgnoreParens();
10268 if (const auto *Deref = dyn_cast<UnaryOperator>(Val: Sub);
10269 Deref && Deref->getOpcode() == UO_Deref)
10270 return evaluatePointer(E: Deref->getSubExpr(), Result);
10271 }
10272 return evaluateLValue(E: E->getSubExpr(), Result);
10273}
10274
10275// Is the provided decl 'std::source_location::current'?
10276static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) {
10277 if (!FD)
10278 return false;
10279 const IdentifierInfo *FnII = FD->getIdentifier();
10280 if (!FnII || !FnII->isStr(Str: "current"))
10281 return false;
10282
10283 const auto *RD = dyn_cast<RecordDecl>(Val: FD->getParent());
10284 if (!RD)
10285 return false;
10286
10287 const IdentifierInfo *ClassII = RD->getIdentifier();
10288 return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location");
10289}
10290
10291bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
10292 const Expr *SubExpr = E->getSubExpr();
10293
10294 switch (E->getCastKind()) {
10295 default:
10296 break;
10297 case CK_BitCast:
10298 case CK_CPointerToObjCPointerCast:
10299 case CK_BlockPointerToObjCPointerCast:
10300 case CK_AnyPointerToBlockPointerCast:
10301 case CK_AddressSpaceConversion:
10302 if (!Visit(S: SubExpr))
10303 return false;
10304 if (E->getType()->isFunctionPointerType() ||
10305 SubExpr->getType()->isFunctionPointerType()) {
10306 // Casting between two function pointer types, or between a function
10307 // pointer and an object pointer, is always a reinterpret_cast.
10308 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10309 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10310 << Info.Ctx.getLangOpts().CPlusPlus;
10311 Result.Designator.setInvalid();
10312 } else if (!E->getType()->isVoidPointerType()) {
10313 // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
10314 // permitted in constant expressions in C++11. Bitcasts from cv void* are
10315 // also static_casts, but we disallow them as a resolution to DR1312.
10316 //
10317 // In some circumstances, we permit casting from void* to cv1 T*, when the
10318 // actual pointee object is actually a cv2 T.
10319 bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
10320 !Result.IsNullPtr;
10321 bool VoidPtrCastMaybeOK =
10322 Result.IsNullPtr ||
10323 (HasValidResult &&
10324 Info.Ctx.hasSimilarType(T1: Result.Designator.getType(Ctx&: Info.Ctx),
10325 T2: E->getType()->getPointeeType()));
10326 // 1. We'll allow it in std::allocator::allocate, and anything which that
10327 // calls.
10328 // 2. HACK 2022-03-28: Work around an issue with libstdc++'s
10329 // <source_location> header. Fixed in GCC 12 and later (2022-04-??).
10330 // We'll allow it in the body of std::source_location::current. GCC's
10331 // implementation had a parameter of type `void*`, and casts from
10332 // that back to `const __impl*` in its body.
10333 if (VoidPtrCastMaybeOK &&
10334 (Info.getStdAllocatorCaller(FnName: "allocate") ||
10335 IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) ||
10336 Info.getLangOpts().CPlusPlus26)) {
10337 // Permitted.
10338 } else {
10339 if (SubExpr->getType()->isVoidPointerType() &&
10340 Info.getLangOpts().CPlusPlus) {
10341 if (HasValidResult)
10342 CCEDiag(E, D: diag::note_constexpr_invalid_void_star_cast)
10343 << SubExpr->getType() << Info.getLangOpts().CPlusPlus26
10344 << Result.Designator.getType(Ctx&: Info.Ctx).getCanonicalType()
10345 << E->getType()->getPointeeType();
10346 else
10347 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10348 << diag::ConstexprInvalidCastKind::CastFrom
10349 << SubExpr->getType();
10350 } else
10351 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10352 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10353 << Info.Ctx.getLangOpts().CPlusPlus;
10354 Result.Designator.setInvalid();
10355 }
10356 }
10357 if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr)
10358 ZeroInitialization(E);
10359 return true;
10360
10361 case CK_DerivedToBase:
10362 case CK_UncheckedDerivedToBase:
10363 if (!evaluatePointer(E: E->getSubExpr(), Result))
10364 return false;
10365 if (!Result.Base && Result.Offset.isZero())
10366 return true;
10367
10368 // Now figure out the necessary offset to add to the base LV to get from
10369 // the derived class to the base class.
10370 return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()->
10371 castAs<PointerType>()->getPointeeType(),
10372 Result);
10373
10374 case CK_BaseToDerived:
10375 if (!Visit(S: E->getSubExpr()))
10376 return false;
10377 if (!Result.Base && Result.Offset.isZero())
10378 return true;
10379 return HandleBaseToDerivedCast(Info, E, Result);
10380
10381 case CK_Dynamic:
10382 if (!Visit(S: E->getSubExpr()))
10383 return false;
10384 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
10385
10386 case CK_NullToPointer:
10387 VisitIgnoredValue(E: E->getSubExpr());
10388 return ZeroInitialization(E);
10389
10390 case CK_IntegralToPointer: {
10391 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10392 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10393 << Info.Ctx.getLangOpts().CPlusPlus;
10394
10395 APValue Value;
10396 if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info))
10397 break;
10398
10399 if (Value.isInt()) {
10400 unsigned Size = Info.Ctx.getTypeSize(T: E->getType());
10401 uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue();
10402 if (N == Info.Ctx.getTargetNullPointerValue(QT: E->getType())) {
10403 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10404 } else {
10405 Result.Base = (Expr *)nullptr;
10406 Result.InvalidBase = false;
10407 Result.Offset = CharUnits::fromQuantity(Quantity: N);
10408 Result.Designator.setInvalid();
10409 Result.IsNullPtr = false;
10410 }
10411 return true;
10412 } else {
10413 // In rare instances, the value isn't an lvalue.
10414 // For example, when the value is the difference between the addresses of
10415 // two labels. We reject that as a constant expression because we can't
10416 // compute a valid offset to convert into a pointer.
10417 if (!Value.isLValue())
10418 return false;
10419
10420 // Cast is of an lvalue, no need to change value.
10421 Result.setFrom(Ctx: Info.Ctx, V: Value);
10422 return true;
10423 }
10424 }
10425
10426 case CK_ArrayToPointerDecay: {
10427 if (SubExpr->isGLValue()) {
10428 if (!evaluateLValue(E: SubExpr, Result))
10429 return false;
10430 } else {
10431 APValue &Value = Info.CurrentCall->createTemporary(
10432 Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
10433 if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr))
10434 return false;
10435 }
10436 // The result is a pointer to the first element of the array.
10437 auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType());
10438 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT))
10439 Result.addArray(Info, E, CAT);
10440 else
10441 Result.addUnsizedArray(Info, E, ElemTy: AT->getElementType());
10442 return true;
10443 }
10444
10445 case CK_FunctionToPointerDecay:
10446 return evaluateLValue(E: SubExpr, Result);
10447
10448 case CK_LValueToRValue: {
10449 LValue LVal;
10450 if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal))
10451 return false;
10452
10453 APValue RVal;
10454 // Note, we use the subexpression's type in order to retain cv-qualifiers.
10455 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
10456 LVal, RVal))
10457 return InvalidBaseOK &&
10458 evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result);
10459 return Success(V: RVal, E);
10460 }
10461 }
10462
10463 return ExprEvaluatorBaseTy::VisitCastExpr(E);
10464}
10465
10466static CharUnits GetAlignOfType(const ASTContext &Ctx, QualType T,
10467 UnaryExprOrTypeTrait ExprKind) {
10468 // C++ [expr.alignof]p3:
10469 // When alignof is applied to a reference type, the result is the
10470 // alignment of the referenced type.
10471 T = T.getNonReferenceType();
10472
10473 if (T.getQualifiers().hasUnaligned())
10474 return CharUnits::One();
10475
10476 const bool AlignOfReturnsPreferred =
10477 Ctx.getLangOpts().isCompatibleWith(Version: LangOptions::ClangABI::Ver7);
10478
10479 // __alignof is defined to return the preferred alignment.
10480 // Before 8, clang returned the preferred alignment for alignof and _Alignof
10481 // as well.
10482 if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
10483 return Ctx.toCharUnitsFromBits(BitSize: Ctx.getPreferredTypeAlign(T: T.getTypePtr()));
10484 // alignof and _Alignof are defined to return the ABI alignment.
10485 else if (ExprKind == UETT_AlignOf)
10486 return Ctx.getTypeAlignInChars(T: T.getTypePtr());
10487 else
10488 llvm_unreachable("GetAlignOfType on a non-alignment ExprKind");
10489}
10490
10491// Convert a builtin ID to the canonical x86 builtin ID the constant evaluators
10492// dispatch on in their x86 target-specific cases, or 0 if \p BuiltinOp is a
10493// target builtin those cases should not handle.
10494//
10495// Target-independent builtins are returned unchanged. Target builtin IDs of
10496// different targets overlap (each target numbers its builtins from
10497// Builtin::FirstTSBuiltin), so a target builtin ID is only meaningful for the
10498// target that owns it. Determine the owning target (translating an auxiliary ID
10499// back to its canonical value) and only return the ID when x86 owns it;
10500// otherwise an overlapping ID could be misinterpreted as an unrelated x86
10501// builtin.
10502unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10503 unsigned BuiltinOp) {
10504 // Target-independent builtins have the same ID regardless of the target, so
10505 // they can be dispatched as-is. This is the common case and is intentionally
10506 // kept to a single comparison so callers can use this on hot paths (e.g. the
10507 // bytecode interpreter's builtin dispatch) without re-deriving the ID from
10508 // the call expression.
10509 if (BuiltinOp < Builtin::FirstTSBuiltin)
10510 return BuiltinOp;
10511
10512 // Determine the target that owns this builtin, translating an auxiliary ID
10513 // back to its canonical value.
10514 const TargetInfo *OwningTarget;
10515 if (Ctx.BuiltinInfo.isAuxBuiltinID(ID: BuiltinOp)) {
10516 OwningTarget = Ctx.getAuxTargetInfo();
10517 BuiltinOp = Ctx.BuiltinInfo.getAuxBuiltinID(ID: BuiltinOp);
10518 } else {
10519 OwningTarget = &Ctx.getTargetInfo();
10520 }
10521
10522 if (!OwningTarget)
10523 return 0;
10524
10525 // x86 and x86_64 share a single builtin set and are the only architectures
10526 // whose target-specific builtins the constant evaluators currently fold.
10527 switch (OwningTarget->getTriple().getArch()) {
10528 case llvm::Triple::x86:
10529 case llvm::Triple::x86_64:
10530 return BuiltinOp;
10531 default:
10532 return 0;
10533 }
10534}
10535
10536unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx,
10537 const CallExpr *E) {
10538 return ConvertBuiltinIDToX86BuiltinID(Ctx, BuiltinOp: E->getBuiltinCallee());
10539}
10540
10541CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
10542 UnaryExprOrTypeTrait ExprKind) {
10543 E = E->IgnoreParens();
10544
10545 // The kinds of expressions that we have special-case logic here for
10546 // should be kept up to date with the special checks for those
10547 // expressions in Sema.
10548
10549 // alignof decl is always accepted, even if it doesn't make sense: we default
10550 // to 1 in those cases.
10551 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E))
10552 return Ctx.getDeclAlign(D: DRE->getDecl(),
10553 /*RefAsPointee*/ ForAlignof: true);
10554
10555 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E))
10556 return Ctx.getDeclAlign(D: ME->getMemberDecl(),
10557 /*RefAsPointee*/ ForAlignof: true);
10558
10559 return GetAlignOfType(Ctx, T: E->getType(), ExprKind);
10560}
10561
10562static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) {
10563 if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>())
10564 return Info.Ctx.getDeclAlign(D: VD);
10565 if (const auto *E = Value.Base.dyn_cast<const Expr *>())
10566 return GetAlignOfExpr(Ctx: Info.Ctx, E, ExprKind: UETT_AlignOf);
10567 return GetAlignOfType(Ctx: Info.Ctx, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf);
10568}
10569
10570/// Evaluate the value of the alignment argument to __builtin_align_{up,down},
10571/// __builtin_is_aligned and __builtin_assume_aligned.
10572static bool getAlignmentArgument(const Expr *E, QualType ForType,
10573 EvalInfo &Info, APSInt &Alignment) {
10574 if (!EvaluateInteger(E, Result&: Alignment, Info))
10575 return false;
10576 if (Alignment < 0 || !Alignment.isPowerOf2()) {
10577 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_alignment) << Alignment;
10578 return false;
10579 }
10580 unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType);
10581 APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1));
10582 if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) {
10583 Info.FFDiag(E, DiagId: diag::note_constexpr_alignment_too_big)
10584 << MaxValue << ForType << Alignment;
10585 return false;
10586 }
10587 // Ensure both alignment and source value have the same bit width so that we
10588 // don't assert when computing the resulting value.
10589 APSInt ExtAlignment =
10590 APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true);
10591 assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 &&
10592 "Alignment should not be changed by ext/trunc");
10593 Alignment = ExtAlignment;
10594 assert(Alignment.getBitWidth() == SrcWidth);
10595 return true;
10596}
10597
10598// To be clear: this happily visits unsupported builtins. Better name welcomed.
10599bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
10600 if (ExprEvaluatorBaseTy::VisitCallExpr(E))
10601 return true;
10602
10603 if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr()))
10604 return false;
10605
10606 Result.setInvalid(B: E);
10607 QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType();
10608 Result.addUnsizedArray(Info, E, ElemTy: PointeeTy);
10609 return true;
10610}
10611
10612bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
10613 if (!IsConstantEvaluatedBuiltinCall(E))
10614 return visitNonBuiltinCallExpr(E);
10615 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
10616}
10617
10618// Determine if T is a character type for which we guarantee that
10619// sizeof(T) == 1.
10620static bool isOneByteCharacterType(QualType T) {
10621 return T->isCharType() || T->isChar8Type();
10622}
10623
10624bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
10625 unsigned BuiltinOp) {
10626 if (IsOpaqueConstantCall(E))
10627 return Success(E);
10628
10629 switch (BuiltinOp) {
10630 case Builtin::BIaddressof:
10631 case Builtin::BI__addressof:
10632 case Builtin::BI__builtin_addressof:
10633 return evaluateLValue(E: E->getArg(Arg: 0), Result);
10634 case Builtin::BI__builtin_assume_aligned: {
10635 // We need to be very careful here because: if the pointer does not have the
10636 // asserted alignment, then the behavior is undefined, and undefined
10637 // behavior is non-constant.
10638 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10639 return false;
10640
10641 LValue OffsetResult(Result);
10642 APSInt Alignment;
10643 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10644 Alignment))
10645 return false;
10646 CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue());
10647
10648 if (E->getNumArgs() > 2) {
10649 APSInt Offset;
10650 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info))
10651 return false;
10652
10653 int64_t AdditionalOffset = -Offset.getZExtValue();
10654 OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset);
10655 }
10656
10657 // If there is a base object, then it must have the correct alignment.
10658 if (OffsetResult.Base) {
10659 CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult);
10660
10661 if (BaseAlignment < Align) {
10662 Result.Designator.setInvalid();
10663 CCEDiag(E: E->getArg(Arg: 0), D: diag::note_constexpr_baa_insufficient_alignment)
10664 << 0 << BaseAlignment.getQuantity() << Align.getQuantity();
10665 return false;
10666 }
10667 }
10668
10669 // The offset must also have the correct alignment.
10670 if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) {
10671 Result.Designator.setInvalid();
10672
10673 (OffsetResult.Base
10674 ? CCEDiag(E: E->getArg(Arg: 0),
10675 D: diag::note_constexpr_baa_insufficient_alignment)
10676 << 1
10677 : CCEDiag(E: E->getArg(Arg: 0),
10678 D: diag::note_constexpr_baa_value_insufficient_alignment))
10679 << OffsetResult.Offset.getQuantity() << Align.getQuantity();
10680 return false;
10681 }
10682
10683 return true;
10684 }
10685 case Builtin::BI__builtin_align_up:
10686 case Builtin::BI__builtin_align_down: {
10687 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10688 return false;
10689 APSInt Alignment;
10690 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10691 Alignment))
10692 return false;
10693 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result);
10694 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset);
10695 // For align_up/align_down, we can return the same value if the alignment
10696 // is known to be greater or equal to the requested value.
10697 if (PtrAlign.getQuantity() >= Alignment)
10698 return true;
10699
10700 // The alignment could be greater than the minimum at run-time, so we cannot
10701 // infer much about the resulting pointer value. One case is possible:
10702 // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we
10703 // can infer the correct index if the requested alignment is smaller than
10704 // the base alignment so we can perform the computation on the offset.
10705 if (BaseAlignment.getQuantity() >= Alignment) {
10706 assert(Alignment.getBitWidth() <= 64 &&
10707 "Cannot handle > 64-bit address-space");
10708 uint64_t Alignment64 = Alignment.getZExtValue();
10709 CharUnits NewOffset = CharUnits::fromQuantity(
10710 Quantity: BuiltinOp == Builtin::BI__builtin_align_down
10711 ? llvm::alignDown(Value: Result.Offset.getQuantity(), Align: Alignment64)
10712 : llvm::alignTo(Value: Result.Offset.getQuantity(), Align: Alignment64));
10713 Result.adjustOffset(N: NewOffset - Result.Offset);
10714 // TODO: diagnose out-of-bounds values/only allow for arrays?
10715 return true;
10716 }
10717 // Otherwise, we cannot constant-evaluate the result.
10718 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_adjust)
10719 << Alignment;
10720 return false;
10721 }
10722 case Builtin::BI__builtin_operator_new:
10723 return HandleOperatorNewCall(Info, E, Result);
10724 case Builtin::BI__builtin_launder:
10725 return evaluatePointer(E: E->getArg(Arg: 0), Result);
10726 case Builtin::BIstrchr:
10727 case Builtin::BIwcschr:
10728 case Builtin::BImemchr:
10729 case Builtin::BIwmemchr:
10730 if (Info.getLangOpts().CPlusPlus11)
10731 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10732 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10733 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10734 else
10735 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10736 [[fallthrough]];
10737 case Builtin::BI__builtin_strchr:
10738 case Builtin::BI__builtin_wcschr:
10739 case Builtin::BI__builtin_memchr:
10740 case Builtin::BI__builtin_char_memchr:
10741 case Builtin::BI__builtin_wmemchr: {
10742 if (!Visit(S: E->getArg(Arg: 0)))
10743 return false;
10744 APSInt Desired;
10745 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info))
10746 return false;
10747 uint64_t MaxLength = uint64_t(-1);
10748 if (BuiltinOp != Builtin::BIstrchr &&
10749 BuiltinOp != Builtin::BIwcschr &&
10750 BuiltinOp != Builtin::BI__builtin_strchr &&
10751 BuiltinOp != Builtin::BI__builtin_wcschr) {
10752 APSInt N;
10753 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10754 return false;
10755 MaxLength = N.getZExtValue();
10756 }
10757 // We cannot find the value if there are no candidates to match against.
10758 if (MaxLength == 0u)
10759 return ZeroInitialization(E);
10760 if (!Result.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
10761 Result.Designator.Invalid)
10762 return false;
10763 QualType CharTy = Result.Designator.getType(Ctx&: Info.Ctx);
10764 bool IsRawByte = BuiltinOp == Builtin::BImemchr ||
10765 BuiltinOp == Builtin::BI__builtin_memchr;
10766 assert(IsRawByte ||
10767 Info.Ctx.hasSameUnqualifiedType(
10768 CharTy, E->getArg(0)->getType()->getPointeeType()));
10769 // Pointers to const void may point to objects of incomplete type.
10770 if (IsRawByte && CharTy->isIncompleteType()) {
10771 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_incomplete_type) << CharTy;
10772 return false;
10773 }
10774 // Give up on byte-oriented matching against multibyte elements.
10775 // FIXME: We can compare the bytes in the correct order.
10776 if (IsRawByte && !isOneByteCharacterType(T: CharTy)) {
10777 Info.FFDiag(E, DiagId: diag::note_constexpr_memchr_unsupported)
10778 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy;
10779 return false;
10780 }
10781 // Figure out what value we're actually looking for (after converting to
10782 // the corresponding unsigned type if necessary).
10783 uint64_t DesiredVal;
10784 bool StopAtNull = false;
10785 switch (BuiltinOp) {
10786 case Builtin::BIstrchr:
10787 case Builtin::BI__builtin_strchr:
10788 // strchr compares directly to the passed integer, and therefore
10789 // always fails if given an int that is not a char.
10790 if (!APSInt::isSameValue(I1: HandleIntToIntCast(Info, E, DestType: CharTy,
10791 SrcType: E->getArg(Arg: 1)->getType(),
10792 Value: Desired),
10793 I2: Desired))
10794 return ZeroInitialization(E);
10795 StopAtNull = true;
10796 [[fallthrough]];
10797 case Builtin::BImemchr:
10798 case Builtin::BI__builtin_memchr:
10799 case Builtin::BI__builtin_char_memchr:
10800 // memchr compares by converting both sides to unsigned char. That's also
10801 // correct for strchr if we get this far (to cope with plain char being
10802 // unsigned in the strchr case).
10803 DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue();
10804 break;
10805
10806 case Builtin::BIwcschr:
10807 case Builtin::BI__builtin_wcschr:
10808 StopAtNull = true;
10809 [[fallthrough]];
10810 case Builtin::BIwmemchr:
10811 case Builtin::BI__builtin_wmemchr:
10812 // wcschr and wmemchr are given a wchar_t to look for. Just use it.
10813 DesiredVal = Desired.getZExtValue();
10814 break;
10815 }
10816
10817 for (; MaxLength; --MaxLength) {
10818 APValue Char;
10819 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Result, RVal&: Char) ||
10820 !Char.isInt())
10821 return false;
10822 if (Char.getInt().getZExtValue() == DesiredVal)
10823 return true;
10824 if (StopAtNull && !Char.getInt())
10825 break;
10826 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: CharTy, Adjustment: 1))
10827 return false;
10828 }
10829 // Not found: return nullptr.
10830 return ZeroInitialization(E);
10831 }
10832
10833 case Builtin::BImemcpy:
10834 case Builtin::BImemmove:
10835 case Builtin::BIwmemcpy:
10836 case Builtin::BIwmemmove:
10837 if (Info.getLangOpts().CPlusPlus11)
10838 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10839 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10840 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10841 else
10842 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10843 [[fallthrough]];
10844 case Builtin::BI__builtin_memcpy:
10845 case Builtin::BI__builtin_memmove:
10846 case Builtin::BI__builtin_wmemcpy:
10847 case Builtin::BI__builtin_wmemmove: {
10848 bool WChar = BuiltinOp == Builtin::BIwmemcpy ||
10849 BuiltinOp == Builtin::BIwmemmove ||
10850 BuiltinOp == Builtin::BI__builtin_wmemcpy ||
10851 BuiltinOp == Builtin::BI__builtin_wmemmove;
10852 bool Move = BuiltinOp == Builtin::BImemmove ||
10853 BuiltinOp == Builtin::BIwmemmove ||
10854 BuiltinOp == Builtin::BI__builtin_memmove ||
10855 BuiltinOp == Builtin::BI__builtin_wmemmove;
10856
10857 // The result of mem* is the first argument.
10858 if (!Visit(S: E->getArg(Arg: 0)))
10859 return false;
10860 LValue Dest = Result;
10861
10862 LValue Src;
10863 if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info))
10864 return false;
10865
10866 APSInt N;
10867 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10868 return false;
10869 assert(!N.isSigned() && "memcpy and friends take an unsigned size");
10870
10871 // If the size is zero, we treat this as always being a valid no-op.
10872 // (Even if one of the src and dest pointers is null.)
10873 if (!N)
10874 return true;
10875
10876 // Otherwise, if either of the operands is null, we can't proceed. Don't
10877 // try to determine the type of the copied objects, because there aren't
10878 // any.
10879 if (!Src.Base || !Dest.Base) {
10880 APValue Val;
10881 (!Src.Base ? Src : Dest).moveInto(V&: Val);
10882 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_null)
10883 << Move << WChar << !!Src.Base
10884 << Val.getAsString(Ctx: Info.Ctx, Ty: E->getArg(Arg: 0)->getType());
10885 return false;
10886 }
10887 if (Src.Designator.Invalid || Dest.Designator.Invalid)
10888 return false;
10889
10890 // We require that Src and Dest are both pointers to arrays of
10891 // trivially-copyable type. (For the wide version, the designator will be
10892 // invalid if the designated object is not a wchar_t.)
10893 QualType T = Dest.Designator.getType(Ctx&: Info.Ctx);
10894 QualType SrcT = Src.Designator.getType(Ctx&: Info.Ctx);
10895 if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) {
10896 // FIXME: Consider using our bit_cast implementation to support this.
10897 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T;
10898 return false;
10899 }
10900 if (T->isIncompleteType()) {
10901 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_incomplete_type) << Move << T;
10902 return false;
10903 }
10904 if (!T.isTriviallyCopyableType(Context: Info.Ctx)) {
10905 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_nontrivial) << Move << T;
10906 return false;
10907 }
10908
10909 // Figure out how many T's we're copying.
10910 uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity();
10911 if (TSize == 0)
10912 return false;
10913 if (!WChar) {
10914 uint64_t Remainder;
10915 llvm::APInt OrigN = N;
10916 llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder);
10917 if (Remainder) {
10918 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
10919 << Move << WChar << 0 << T << toString(I: OrigN, Radix: 10, /*Signed*/false)
10920 << (unsigned)TSize;
10921 return false;
10922 }
10923 }
10924
10925 // Check that the copying will remain within the arrays, just so that we
10926 // can give a more meaningful diagnostic. This implicitly also checks that
10927 // N fits into 64 bits.
10928 uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second;
10929 uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second;
10930 if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) {
10931 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
10932 << Move << WChar << (N.ugt(RHS: RemainingSrcSize) ? 1 : 2) << T
10933 << toString(I: N, Radix: 10, /*Signed*/false);
10934 return false;
10935 }
10936 uint64_t NElems = N.getZExtValue();
10937 uint64_t NBytes = NElems * TSize;
10938
10939 // Check for overlap.
10940 int Direction = 1;
10941 if (HasSameBase(A: Src, B: Dest)) {
10942 uint64_t SrcOffset = Src.getLValueOffset().getQuantity();
10943 uint64_t DestOffset = Dest.getLValueOffset().getQuantity();
10944 if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) {
10945 // Dest is inside the source region.
10946 if (!Move) {
10947 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
10948 return false;
10949 }
10950 // For memmove and friends, copy backwards.
10951 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: NElems - 1) ||
10952 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: NElems - 1))
10953 return false;
10954 Direction = -1;
10955 } else if (!Move && SrcOffset >= DestOffset &&
10956 SrcOffset - DestOffset < NBytes) {
10957 // Src is inside the destination region for memcpy: invalid.
10958 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
10959 return false;
10960 }
10961 }
10962
10963 while (true) {
10964 APValue Val;
10965 // FIXME: Set WantObjectRepresentation to true if we're copying a
10966 // char-like type?
10967 if (!handleLValueToRValueConversion(Info, Conv: E, Type: T, LVal: Src, RVal&: Val) ||
10968 !handleAssignment(Info, E, LVal: Dest, LValType: T, Val))
10969 return false;
10970 // Do not iterate past the last element; if we're copying backwards, that
10971 // might take us off the start of the array.
10972 if (--NElems == 0)
10973 return true;
10974 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: Direction) ||
10975 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: Direction))
10976 return false;
10977 }
10978 }
10979
10980 default:
10981 return false;
10982 }
10983}
10984
10985static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
10986 APValue &Result, const InitListExpr *ILE,
10987 QualType AllocType);
10988static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
10989 APValue &Result,
10990 const CXXConstructExpr *CCE,
10991 QualType AllocType);
10992
10993bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
10994 if (!Info.getLangOpts().CPlusPlus20)
10995 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
10996
10997 // We cannot speculatively evaluate a delete expression.
10998 if (Info.SpeculativeEvaluationDepth)
10999 return false;
11000
11001 FunctionDecl *OperatorNew = E->getOperatorNew();
11002 QualType AllocType = E->getAllocatedType();
11003 QualType TargetType = AllocType;
11004
11005 bool IsNothrow = false;
11006 bool IsPlacement = false;
11007
11008 if (E->getNumPlacementArgs() == 1 &&
11009 E->getPlacementArg(I: 0)->getType()->isNothrowT()) {
11010 // The only new-placement list we support is of the form (std::nothrow).
11011 //
11012 // FIXME: There is no restriction on this, but it's not clear that any
11013 // other form makes any sense. We get here for cases such as:
11014 //
11015 // new (std::align_val_t{N}) X(int)
11016 //
11017 // (which should presumably be valid only if N is a multiple of
11018 // alignof(int), and in any case can't be deallocated unless N is
11019 // alignof(X) and X has new-extended alignment).
11020 LValue Nothrow;
11021 if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info))
11022 return false;
11023 IsNothrow = true;
11024 } else if (OperatorNew->isReservedGlobalPlacementOperator()) {
11025 if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 ||
11026 (Info.CurrentCall->CanEvalMSConstexpr &&
11027 OperatorNew->hasAttr<MSConstexprAttr>())) {
11028 if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info))
11029 return false;
11030 if (Result.Designator.Invalid)
11031 return false;
11032 TargetType = E->getPlacementArg(I: 0)->getType();
11033 IsPlacement = true;
11034 } else {
11035 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11036 << /*C++26 feature*/ 1 << E->getSourceRange();
11037 return false;
11038 }
11039 } else if (E->getNumPlacementArgs()) {
11040 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
11041 << /*Unsupported*/ 0 << E->getSourceRange();
11042 return false;
11043 } else if (!OperatorNew
11044 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
11045 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
11046 << isa<CXXMethodDecl>(Val: OperatorNew) << OperatorNew;
11047 return false;
11048 }
11049
11050 const Expr *Init = E->getInitializer();
11051 const InitListExpr *ResizedArrayILE = nullptr;
11052 const CXXConstructExpr *ResizedArrayCCE = nullptr;
11053 bool ValueInit = false;
11054
11055 if (std::optional<const Expr *> ArraySize = E->getArraySize()) {
11056 const Expr *Stripped = *ArraySize;
11057 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped);
11058 Stripped = ICE->getSubExpr())
11059 if (ICE->getCastKind() != CK_NoOp &&
11060 ICE->getCastKind() != CK_IntegralCast)
11061 break;
11062
11063 llvm::APSInt ArrayBound;
11064 if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info))
11065 return false;
11066
11067 // C++ [expr.new]p9:
11068 // The expression is erroneous if:
11069 // -- [...] its value before converting to size_t [or] applying the
11070 // second standard conversion sequence is less than zero
11071 if (ArrayBound.isSigned() && ArrayBound.isNegative()) {
11072 if (IsNothrow)
11073 return ZeroInitialization(E);
11074
11075 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_negative)
11076 << ArrayBound << (*ArraySize)->getSourceRange();
11077 return false;
11078 }
11079
11080 // -- its value is such that the size of the allocated object would
11081 // exceed the implementation-defined limit
11082 if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(),
11083 BitWidth: ConstantArrayType::getNumAddressingBits(
11084 Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound),
11085 ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) {
11086 if (IsNothrow)
11087 return ZeroInitialization(E);
11088 return false;
11089 }
11090
11091 // -- the new-initializer is a braced-init-list and the number of
11092 // array elements for which initializers are provided [...]
11093 // exceeds the number of elements to initialize
11094 if (!Init) {
11095 // No initialization is performed.
11096 } else if (isa<CXXScalarValueInitExpr>(Val: Init) ||
11097 isa<ImplicitValueInitExpr>(Val: Init)) {
11098 ValueInit = true;
11099 } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) {
11100 ResizedArrayCCE = CCE;
11101 } else {
11102 auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType());
11103 assert(CAT && "unexpected type for array initializer");
11104
11105 unsigned Bits =
11106 std::max(a: CAT->getSizeBitWidth(), b: ArrayBound.getBitWidth());
11107 llvm::APInt InitBound = CAT->getSize().zext(width: Bits);
11108 llvm::APInt AllocBound = ArrayBound.zext(width: Bits);
11109 if (InitBound.ugt(RHS: AllocBound)) {
11110 if (IsNothrow)
11111 return ZeroInitialization(E);
11112
11113 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_too_small)
11114 << toString(I: AllocBound, Radix: 10, /*Signed=*/false)
11115 << toString(I: InitBound, Radix: 10, /*Signed=*/false)
11116 << (*ArraySize)->getSourceRange();
11117 return false;
11118 }
11119
11120 // If the sizes differ, we must have an initializer list, and we need
11121 // special handling for this case when we initialize.
11122 if (InitBound != AllocBound)
11123 ResizedArrayILE = cast<InitListExpr>(Val: Init);
11124 }
11125
11126 AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr,
11127 ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
11128 } else if (E->isArray()) {
11129 // We have an array new-expression whose array size could not be
11130 // determined, e.g. 'new int[]()', where the bound is neither given nor
11131 // deducible from the initializer. This is ill-formed and already
11132 // diagnosed, so bail out rather than mis-evaluating a scalar allocation
11133 // as an array (which would later crash the evaluator).
11134 return false;
11135 } else {
11136 assert(!AllocType->isArrayType() &&
11137 "array allocation with non-array new");
11138 }
11139
11140 APValue *Val;
11141 if (IsPlacement) {
11142 AccessKinds AK = AK_Construct;
11143 struct FindObjectHandler {
11144 EvalInfo &Info;
11145 const Expr *E;
11146 QualType AllocType;
11147 const AccessKinds AccessKind;
11148 APValue *Value;
11149
11150 typedef bool result_type;
11151 bool failed() { return false; }
11152 bool checkConst(QualType QT) {
11153 if (QT.isConstQualified()) {
11154 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
11155 return false;
11156 }
11157 return true;
11158 }
11159 bool found(APValue &Subobj, QualType SubobjType,
11160 APValue::LValueBase Base) {
11161 if (!checkConst(QT: SubobjType))
11162 return false;
11163 // FIXME: Reject the cases where [basic.life]p8 would not permit the
11164 // old name of the object to be used to name the new object.
11165 if (!Info.Ctx.hasSimilarType(T1: SubobjType, T2: AllocType)) {
11166 Info.FFDiag(E, DiagId: diag::note_constexpr_placement_new_wrong_type)
11167 << SubobjType << AllocType;
11168 return false;
11169 }
11170 Value = &Subobj;
11171 return true;
11172 }
11173 bool found(APSInt &Value, QualType SubobjType) {
11174 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11175 return false;
11176 }
11177 bool found(APFloat &Value, QualType SubobjType) {
11178 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
11179 return false;
11180 }
11181 } Handler = {.Info: Info, .E: E, .AllocType: AllocType, .AccessKind: AK, .Value: nullptr};
11182
11183 if (AllocType->isArrayType() &&
11184 Result.Designator.MostDerivedIsArrayElement &&
11185 Result.Designator.Entries.back().getAsArrayIndex() == 0) {
11186 // The destination of placement new is pointing to the first element
11187 // of an array. There's a special case in [expr.const]: "[...] if T is an
11188 // array type, to the first element of such an object [...]". Handle
11189 // that case here by dropping the last entry in the designator list.
11190 QualType AllocElementType =
11191 Info.Ctx.getAsArrayType(T: AllocType)->getElementType();
11192 if (Info.Ctx.hasSimilarType(T1: AllocElementType,
11193 T2: Result.Designator.MostDerivedType)) {
11194 Result.Designator.truncate(Ctx&: Info.Ctx, Base: Result.Base,
11195 NewLength: Result.Designator.MostDerivedPathLength - 1);
11196 }
11197 }
11198
11199 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: Result, LValType: AllocType);
11200 if (!Obj || !findSubobject(Info, E, Obj, Sub: Result.Designator, handler&: Handler))
11201 return false;
11202
11203 Val = Handler.Value;
11204
11205 // [basic.life]p1:
11206 // The lifetime of an object o of type T ends when [...] the storage
11207 // which the object occupies is [...] reused by an object that is not
11208 // nested within o (6.6.2).
11209 *Val = APValue();
11210 } else {
11211 // Perform the allocation and obtain a pointer to the resulting object.
11212 Val = Info.createHeapAlloc(E, T: AllocType, LV&: Result);
11213 if (!Val)
11214 return false;
11215 }
11216
11217 if (ValueInit) {
11218 ImplicitValueInitExpr VIE(AllocType);
11219 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: &VIE))
11220 return false;
11221 } else if (ResizedArrayILE) {
11222 if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE,
11223 AllocType))
11224 return false;
11225 } else if (ResizedArrayCCE) {
11226 if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE,
11227 AllocType))
11228 return false;
11229 } else if (Init) {
11230 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init))
11231 return false;
11232 } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) {
11233 return false;
11234 }
11235
11236 // Array new returns a pointer to the first element, not a pointer to the
11237 // array.
11238 if (auto *AT = AllocType->getAsArrayTypeUnsafe())
11239 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val: AT));
11240
11241 return true;
11242}
11243//===----------------------------------------------------------------------===//
11244// Member Pointer Evaluation
11245//===----------------------------------------------------------------------===//
11246
11247namespace {
11248class MemberPointerExprEvaluator
11249 : public ExprEvaluatorBase<MemberPointerExprEvaluator> {
11250 MemberPtr &Result;
11251
11252 bool Success(const ValueDecl *D) {
11253 Result = MemberPtr(D);
11254 return true;
11255 }
11256public:
11257
11258 MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
11259 : ExprEvaluatorBaseTy(Info), Result(Result) {}
11260
11261 bool Success(const APValue &V, const Expr *E) {
11262 Result.setFrom(V);
11263 return true;
11264 }
11265 bool ZeroInitialization(const Expr *E) {
11266 return Success(D: (const ValueDecl*)nullptr);
11267 }
11268
11269 bool VisitCastExpr(const CastExpr *E);
11270 bool VisitUnaryAddrOf(const UnaryOperator *E);
11271};
11272} // end anonymous namespace
11273
11274static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
11275 EvalInfo &Info) {
11276 assert(!E->isValueDependent());
11277 assert(E->isPRValue() && E->getType()->isMemberPointerType());
11278 return MemberPointerExprEvaluator(Info, Result).Visit(S: E);
11279}
11280
11281bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
11282 switch (E->getCastKind()) {
11283 default:
11284 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11285
11286 case CK_NullToMemberPointer:
11287 VisitIgnoredValue(E: E->getSubExpr());
11288 return ZeroInitialization(E);
11289
11290 case CK_BaseToDerivedMemberPointer: {
11291 if (!Visit(S: E->getSubExpr()))
11292 return false;
11293 if (E->path_empty())
11294 return true;
11295 // Base-to-derived member pointer casts store the path in derived-to-base
11296 // order, so iterate backwards. The CXXBaseSpecifier also provides us with
11297 // the wrong end of the derived->base arc, so stagger the path by one class.
11298 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
11299 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
11300 PathI != PathE; ++PathI) {
11301 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11302 const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
11303 if (!Result.castToDerived(Derived))
11304 return Error(E);
11305 }
11306 if (!Result.castToDerived(Derived: E->getType()
11307 ->castAs<MemberPointerType>()
11308 ->getMostRecentCXXRecordDecl()))
11309 return Error(E);
11310 return true;
11311 }
11312
11313 case CK_DerivedToBaseMemberPointer:
11314 if (!Visit(S: E->getSubExpr()))
11315 return false;
11316 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11317 PathE = E->path_end(); PathI != PathE; ++PathI) {
11318 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11319 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11320 if (!Result.castToBase(Base))
11321 return Error(E);
11322 }
11323 return true;
11324 }
11325}
11326
11327bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
11328 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
11329 // member can be formed.
11330 return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl());
11331}
11332
11333//===----------------------------------------------------------------------===//
11334// Record Evaluation
11335//===----------------------------------------------------------------------===//
11336
11337namespace {
11338 class RecordExprEvaluator
11339 : public ExprEvaluatorBase<RecordExprEvaluator> {
11340 const LValue &This;
11341 APValue &Result;
11342 public:
11343
11344 RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
11345 : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
11346
11347 bool Success(const APValue &V, const Expr *E) {
11348 Result = V;
11349 return true;
11350 }
11351 bool ZeroInitialization(const Expr *E) {
11352 return ZeroInitialization(E, T: E->getType());
11353 }
11354 bool ZeroInitialization(const Expr *E, QualType T);
11355
11356 bool VisitCallExpr(const CallExpr *E) {
11357 return handleCallExpr(E, Result, ResultSlot: &This);
11358 }
11359 bool VisitCastExpr(const CastExpr *E);
11360 bool VisitInitListExpr(const InitListExpr *E);
11361 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11362 return VisitCXXConstructExpr(E, T: E->getType());
11363 }
11364 bool VisitLambdaExpr(const LambdaExpr *E);
11365 bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
11366 bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T);
11367 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);
11368 bool VisitBinCmp(const BinaryOperator *E);
11369 bool VisitTypeTraitExpr(const TypeTraitExpr *E);
11370 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
11371 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
11372 ArrayRef<Expr *> Args);
11373 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
11374 };
11375}
11376
11377/// Perform zero-initialization on an object of non-union class type.
11378/// C++11 [dcl.init]p5:
11379/// To zero-initialize an object or reference of type T means:
11380/// [...]
11381/// -- if T is a (possibly cv-qualified) non-union class type,
11382/// each non-static data member and each base-class subobject is
11383/// zero-initialized
11384static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
11385 const RecordDecl *RD,
11386 const LValue &This, APValue &Result,
11387 bool IsCompleteClass = true) {
11388 assert(!RD->isUnion() && "Expected non-union class type");
11389 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD);
11390
11391 if (CD) {
11392 unsigned NonVirtualBases = countNonVirtualBases(RD: CD);
11393 Result =
11394 APValue(APValue::UninitStruct(), NonVirtualBases, RD->getNumFields(),
11395 IsCompleteClass ? CD->getNumVBases() : 0);
11396 } else {
11397 Result = APValue(APValue::UninitStruct(), 0, RD->getNumFields());
11398 }
11399
11400 if (RD->isInvalidDecl()) return false;
11401 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11402
11403 if (CD) {
11404 unsigned Index = 0;
11405
11406 for (const auto &B : CD->bases()) {
11407 if (B.isVirtual())
11408 continue;
11409 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11410 LValue Subobject = This;
11411 if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11412 return false;
11413 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11414 Result&: Result.getStructBase(i: Index),
11415 /*IsCompleteClass=*/false))
11416 return false;
11417 ++Index;
11418 }
11419 }
11420
11421 for (const auto *I : RD->fields()) {
11422 // -- if T is a reference type, no initialization is performed.
11423 if (I->isUnnamedBitField() || I->getType()->isReferenceType())
11424 continue;
11425
11426 LValue Subobject = This;
11427 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout))
11428 return false;
11429
11430 ImplicitValueInitExpr VIE(I->getType());
11431 if (!EvaluateInPlace(
11432 Result&: Result.getStructField(i: I->getFieldIndex()), Info, This: Subobject, E: &VIE))
11433 return false;
11434 }
11435
11436 if (CD && This.pointsToCompleteClass(D: CD)) {
11437 unsigned Index = 0;
11438 for (const auto &B : CD->vbases()) {
11439 const CXXRecordDecl *Base = B.getType()->getAsCXXRecordDecl();
11440 LValue Subobject = This;
11441 if (!HandleLValueDirectVirtualBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11442 return false;
11443 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11444 Result&: Result.getStructVirtualBase(i: Index),
11445 /*IsCompleteClass=*/false))
11446 return false;
11447 ++Index;
11448 }
11449 }
11450
11451 return true;
11452}
11453
11454bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {
11455 const auto *RD = T->castAsRecordDecl();
11456 if (RD->isInvalidDecl()) return false;
11457 if (RD->isUnion()) {
11458 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
11459 // object's first non-static named data member is zero-initialized
11460 RecordDecl::field_iterator I = RD->field_begin();
11461 while (I != RD->field_end() && (*I)->isUnnamedBitField())
11462 ++I;
11463 if (I == RD->field_end()) {
11464 Result = APValue((const FieldDecl*)nullptr);
11465 return true;
11466 }
11467
11468 LValue Subobject = This;
11469 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I))
11470 return false;
11471 Result = APValue(*I);
11472 ImplicitValueInitExpr VIE(I->getType());
11473 return EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: &VIE);
11474 }
11475
11476 if (!Info.getLangOpts().CPlusPlus26) {
11477 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11478 CXXRD && CXXRD->getNumVBases()) {
11479 Info.FFDiag(E, DiagId: diag::note_constexpr_virtual_base) << RD;
11480 return false;
11481 }
11482 }
11483
11484 return HandleClassZeroInitialization(Info, E, RD, This, Result);
11485}
11486
11487bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
11488 switch (E->getCastKind()) {
11489 default:
11490 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11491
11492 case CK_ConstructorConversion:
11493 return Visit(S: E->getSubExpr());
11494
11495 case CK_DerivedToBase:
11496 case CK_UncheckedDerivedToBase: {
11497 APValue DerivedObject;
11498 if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr()))
11499 return false;
11500 if (!DerivedObject.isStruct())
11501 return Error(E: E->getSubExpr());
11502
11503 // Derived-to-base rvalue conversion: just slice off the derived part.
11504 APValue *Value = &DerivedObject;
11505 const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
11506 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11507 PathE = E->path_end(); PathI != PathE; ++PathI) {
11508 assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
11509 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11510 Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base));
11511 RD = Base;
11512 }
11513 Result = *Value;
11514 return true;
11515 }
11516 case CK_HLSLAggregateSplatCast: {
11517 APValue Val;
11518 QualType ValTy;
11519
11520 if (!hlslAggSplatHelper(Info, E: E->getSubExpr(), SrcVal&: Val, SrcTy&: ValTy))
11521 return false;
11522
11523 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
11524 // splat our Val
11525 SmallVector<APValue> SplatEls(NEls, Val);
11526 SmallVector<QualType> SplatType(NEls, ValTy);
11527
11528 // cast the elements and construct our struct result
11529 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11530 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
11531 ElTypes&: SplatType))
11532 return false;
11533
11534 return true;
11535 }
11536 case CK_HLSLElementwiseCast: {
11537 SmallVector<APValue> SrcEls;
11538 SmallVector<QualType> SrcTypes;
11539
11540 if (!hlslElementwiseCastHelper(Info, E: E->getSubExpr(), DestTy: E->getType(), SrcVals&: SrcEls,
11541 SrcTypes))
11542 return false;
11543
11544 // cast the elements and construct our struct result
11545 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11546 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
11547 ElTypes&: SrcTypes))
11548 return false;
11549
11550 return true;
11551 }
11552 case CK_ToUnion: {
11553 const FieldDecl *Field = E->getTargetUnionField();
11554 LValue Subobject = This;
11555 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field))
11556 return false;
11557 Result = APValue(Field);
11558 if (!EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject,
11559 E: E->getSubExpr()))
11560 return false;
11561 if (Field->isBitField()) {
11562 if (!truncateBitfieldValue(Info, E: E->getSubExpr(), Value&: Result.getUnionValue(),
11563 FD: Field))
11564 return false;
11565 }
11566 return true;
11567 }
11568 }
11569}
11570
11571bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
11572 if (E->isTransparent())
11573 return Visit(S: E->getInit(Init: 0));
11574 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits());
11575}
11576
11577bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(
11578 const Expr *ExprToVisit, ArrayRef<Expr *> Args) {
11579 const auto *RD = ExprToVisit->getType()->castAsRecordDecl();
11580 if (RD->isInvalidDecl()) return false;
11581 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11582 auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11583
11584 EvalInfo::EvaluatingConstructorRAII EvalObj(
11585 Info,
11586 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
11587 CXXRD && CXXRD->getNumBases());
11588
11589 if (RD->isUnion()) {
11590 const FieldDecl *Field;
11591 if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) {
11592 Field = ILE->getInitializedFieldInUnion();
11593 } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) {
11594 Field = PLIE->getInitializedFieldInUnion();
11595 } else {
11596 llvm_unreachable(
11597 "Expression is neither an init list nor a C++ paren list");
11598 }
11599
11600 Result = APValue(Field);
11601 if (!Field)
11602 return true;
11603
11604 // If the initializer list for a union does not contain any elements, the
11605 // first element of the union is value-initialized.
11606 // FIXME: The element should be initialized from an initializer list.
11607 // Is this difference ever observable for initializer lists which
11608 // we don't build?
11609 ImplicitValueInitExpr VIE(Field->getType());
11610 const Expr *InitExpr = Args.empty() ? &VIE : Args[0];
11611
11612 LValue Subobject = This;
11613 if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout))
11614 return false;
11615
11616 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11617 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11618 isa<CXXDefaultInitExpr>(Val: InitExpr));
11619
11620 if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) {
11621 if (Field->isBitField())
11622 return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(),
11623 FD: Field);
11624 return true;
11625 }
11626
11627 return false;
11628 }
11629
11630 if (!Result.hasValue())
11631 Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,
11632 RD->getNumFields());
11633 unsigned ElementNo = 0;
11634 bool Success = true;
11635
11636 // Initialize base classes.
11637 if (CXXRD && CXXRD->getNumBases()) {
11638 for (const auto &Base : CXXRD->bases()) {
11639 assert(ElementNo < Args.size() && "missing init for base class");
11640 const Expr *Init = Args[ElementNo];
11641
11642 LValue Subobject = This;
11643 if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base))
11644 return false;
11645
11646 APValue &FieldVal = Result.getStructBase(i: ElementNo);
11647 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) {
11648 if (!Info.noteFailure())
11649 return false;
11650 Success = false;
11651 }
11652 ++ElementNo;
11653 }
11654
11655 EvalObj.finishedConstructingBases();
11656 }
11657
11658 // Initialize members.
11659 for (const auto *Field : RD->fields()) {
11660 // Anonymous bit-fields are not considered members of the class for
11661 // purposes of aggregate initialization.
11662 if (Field->isUnnamedBitField())
11663 continue;
11664
11665 LValue Subobject = This;
11666
11667 bool HaveInit = ElementNo < Args.size();
11668
11669 // FIXME: Diagnostics here should point to the end of the initializer
11670 // list, not the start.
11671 if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit,
11672 LVal&: Subobject, FD: Field, RL: &Layout))
11673 return false;
11674
11675 // Perform an implicit value-initialization for members beyond the end of
11676 // the initializer list.
11677 ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
11678 const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE;
11679
11680 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
11681 // aren't supposed to be modified.
11682 if (isa<NoInitExpr>(Val: Init))
11683 continue;
11684
11685 if (Field->getType()->isIncompleteArrayType()) {
11686 if (auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType())) {
11687 if (!CAT->isZeroSize()) {
11688 // Bail out for now. This might sort of "work", but the rest of the
11689 // code isn't really prepared to handle it.
11690 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_unsupported_flexible_array);
11691 return false;
11692 }
11693 }
11694 }
11695
11696 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11697 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11698 isa<CXXDefaultInitExpr>(Val: Init));
11699
11700 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11701 if (Field->getType()->isReferenceType()) {
11702 LValue Result;
11703 if (!EvaluateInitForDeclOfReferenceType(Info, D: Field, Init, Result,
11704 Val&: FieldVal)) {
11705 if (!Info.noteFailure())
11706 return false;
11707 Success = false;
11708 }
11709 } else if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) ||
11710 (Field->isBitField() &&
11711 !truncateBitfieldValue(Info, E: Init, Value&: FieldVal, FD: Field))) {
11712 if (!Info.noteFailure())
11713 return false;
11714 Success = false;
11715 }
11716 }
11717
11718 EvalObj.finishedConstructingFields();
11719
11720 return Success;
11721}
11722
11723bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
11724 QualType T) {
11725 // Note that E's type is not necessarily the type of our class here; we might
11726 // be initializing an array element instead.
11727 const CXXConstructorDecl *FD = E->getConstructor();
11728 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;
11729
11730 bool ZeroInit = E->requiresZeroInitialization();
11731 if (CheckTrivialDefaultConstructor(Info, Loc: E->getExprLoc(), CD: FD, IsValueInitialization: ZeroInit)) {
11732 if (ZeroInit)
11733 return ZeroInitialization(E, T);
11734
11735 return handleDefaultInitValue(T, Result);
11736 }
11737
11738 const FunctionDecl *Definition = nullptr;
11739 auto Body = FD->getBody(Definition);
11740
11741 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11742 return false;
11743
11744 // Avoid materializing a temporary for an elidable copy/move constructor.
11745 if (E->isElidable() && !ZeroInit) {
11746 // FIXME: This only handles the simplest case, where the source object
11747 // is passed directly as the first argument to the constructor.
11748 // This should also handle stepping though implicit casts and
11749 // and conversion sequences which involve two steps, with a
11750 // conversion operator followed by a converting constructor.
11751 const Expr *SrcObj = E->getArg(Arg: 0);
11752 assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));
11753 assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
11754 if (const MaterializeTemporaryExpr *ME =
11755 dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj))
11756 return Visit(S: ME->getSubExpr());
11757 }
11758
11759 if (ZeroInit && !ZeroInitialization(E, T))
11760 return false;
11761
11762 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
11763 return HandleConstructorCall(E, This, Args,
11764 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11765 Result);
11766}
11767
11768bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr(
11769 const CXXInheritedCtorInitExpr *E) {
11770 if (!Info.CurrentCall) {
11771 assert(Info.checkingPotentialConstantExpression());
11772 return false;
11773 }
11774
11775 const CXXConstructorDecl *FD = E->getConstructor();
11776 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl())
11777 return false;
11778
11779 const FunctionDecl *Definition = nullptr;
11780 auto Body = FD->getBody(Definition);
11781
11782 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11783 return false;
11784
11785 return HandleConstructorCall(E, This, Call: Info.CurrentCall->Arguments,
11786 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11787 Result);
11788}
11789
11790bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(
11791 const CXXStdInitializerListExpr *E) {
11792 const ConstantArrayType *ArrayType =
11793 Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType());
11794
11795 LValue Array;
11796 if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info))
11797 return false;
11798
11799 assert(ArrayType && "unexpected type for array initializer");
11800
11801 // Get a pointer to the first element of the array.
11802 Array.addArray(Info, E, CAT: ArrayType);
11803
11804 // FIXME: What if the initializer_list type has base classes, etc?
11805 Result = APValue(APValue::UninitStruct(), 0, 2);
11806 Array.moveInto(V&: Result.getStructField(i: 0));
11807
11808 auto *Record = E->getType()->castAsRecordDecl();
11809 RecordDecl::field_iterator Field = Record->field_begin();
11810 assert(Field != Record->field_end() &&
11811 Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11812 ArrayType->getElementType()) &&
11813 "Expected std::initializer_list first field to be const E *");
11814 ++Field;
11815 assert(Field != Record->field_end() &&
11816 "Expected std::initializer_list to have two fields");
11817
11818 if (Info.Ctx.hasSameType(T1: Field->getType(), T2: Info.Ctx.getSizeType())) {
11819 // Length.
11820 Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize()));
11821 } else {
11822 // End pointer.
11823 assert(Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11824 ArrayType->getElementType()) &&
11825 "Expected std::initializer_list second field to be const E *");
11826 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Array,
11827 EltTy: ArrayType->getElementType(),
11828 Adjustment: ArrayType->getZExtSize()))
11829 return false;
11830 Array.moveInto(V&: Result.getStructField(i: 1));
11831 }
11832
11833 assert(++Field == Record->field_end() &&
11834 "Expected std::initializer_list to only have two fields");
11835
11836 return true;
11837}
11838
11839bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
11840 const CXXRecordDecl *ClosureClass = E->getLambdaClass();
11841 if (ClosureClass->isInvalidDecl())
11842 return false;
11843
11844 const size_t NumFields = ClosureClass->getNumFields();
11845
11846 assert(NumFields == (size_t)std::distance(E->capture_init_begin(),
11847 E->capture_init_end()) &&
11848 "The number of lambda capture initializers should equal the number of "
11849 "fields within the closure type");
11850
11851 Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields);
11852 // Iterate through all the lambda's closure object's fields and initialize
11853 // them.
11854 auto *CaptureInitIt = E->capture_init_begin();
11855 bool Success = true;
11856 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: ClosureClass);
11857 for (const auto *Field : ClosureClass->fields()) {
11858 assert(CaptureInitIt != E->capture_init_end());
11859 // Get the initializer for this field
11860 Expr *const CurFieldInit = *CaptureInitIt++;
11861
11862 // If there is no initializer, either this is a VLA or an error has
11863 // occurred.
11864 if (!CurFieldInit || CurFieldInit->containsErrors())
11865 return Error(E);
11866
11867 LValue Subobject = This;
11868
11869 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field, RL: &Layout))
11870 return false;
11871
11872 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11873 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: CurFieldInit)) {
11874 if (!Info.keepEvaluatingAfterFailure())
11875 return false;
11876 Success = false;
11877 }
11878 }
11879 return Success;
11880}
11881
11882bool RecordExprEvaluator::VisitDesignatedInitUpdateExpr(
11883 const DesignatedInitUpdateExpr *E) {
11884 if (!Visit(S: E->getBase()))
11885 return false;
11886 return Visit(S: E->getUpdater());
11887}
11888
11889static bool EvaluateRecord(const Expr *E, const LValue &This,
11890 APValue &Result, EvalInfo &Info) {
11891 assert(!E->isValueDependent());
11892 assert(E->isPRValue() && E->getType()->isRecordType() &&
11893 "can't evaluate expression as a record rvalue");
11894 return RecordExprEvaluator(Info, This, Result).Visit(S: E);
11895}
11896
11897//===----------------------------------------------------------------------===//
11898// Temporary Evaluation
11899//
11900// Temporaries are represented in the AST as rvalues, but generally behave like
11901// lvalues. The full-object of which the temporary is a subobject is implicitly
11902// materialized so that a reference can bind to it.
11903//===----------------------------------------------------------------------===//
11904namespace {
11905class TemporaryExprEvaluator
11906 : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
11907public:
11908 TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
11909 LValueExprEvaluatorBaseTy(Info, Result, false) {}
11910
11911 /// Visit an expression which constructs the value of this temporary.
11912 bool VisitConstructExpr(const Expr *E) {
11913 APValue &Value = Info.CurrentCall->createTemporary(
11914 Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
11915 return EvaluateInPlace(Result&: Value, Info, This: Result, E);
11916 }
11917
11918 bool VisitCastExpr(const CastExpr *E) {
11919 switch (E->getCastKind()) {
11920 default:
11921 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
11922
11923 case CK_ConstructorConversion:
11924 return VisitConstructExpr(E: E->getSubExpr());
11925 }
11926 }
11927 bool VisitInitListExpr(const InitListExpr *E) {
11928 return VisitConstructExpr(E);
11929 }
11930 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11931 return VisitConstructExpr(E);
11932 }
11933 bool VisitCallExpr(const CallExpr *E) {
11934 return VisitConstructExpr(E);
11935 }
11936 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) {
11937 return VisitConstructExpr(E);
11938 }
11939 bool VisitLambdaExpr(const LambdaExpr *E) {
11940 return VisitConstructExpr(E);
11941 }
11942};
11943} // end anonymous namespace
11944
11945/// Evaluate an expression of record type as a temporary.
11946static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
11947 assert(!E->isValueDependent());
11948 assert(E->isPRValue() && E->getType()->isRecordType());
11949 return TemporaryExprEvaluator(Info, Result).Visit(S: E);
11950}
11951
11952//===----------------------------------------------------------------------===//
11953// Vector Evaluation
11954//===----------------------------------------------------------------------===//
11955
11956namespace {
11957 class VectorExprEvaluator
11958 : public ExprEvaluatorBase<VectorExprEvaluator> {
11959 APValue &Result;
11960 public:
11961
11962 VectorExprEvaluator(EvalInfo &info, APValue &Result)
11963 : ExprEvaluatorBaseTy(info), Result(Result) {}
11964
11965 bool Success(ArrayRef<APValue> V, const Expr *E) {
11966 assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
11967 // FIXME: remove this APValue copy.
11968 Result = APValue(V.data(), V.size());
11969 return true;
11970 }
11971 bool Success(const APValue &V, const Expr *E) {
11972 assert(V.isVector());
11973 Result = V;
11974 return true;
11975 }
11976 bool ZeroInitialization(const Expr *E);
11977
11978 bool VisitUnaryReal(const UnaryOperator *E)
11979 { return Visit(S: E->getSubExpr()); }
11980 bool VisitCastExpr(const CastExpr* E);
11981 bool VisitInitListExpr(const InitListExpr *E);
11982 bool VisitUnaryImag(const UnaryOperator *E);
11983 bool VisitBinaryOperator(const BinaryOperator *E);
11984 bool VisitUnaryOperator(const UnaryOperator *E);
11985 bool VisitCallExpr(const CallExpr *E);
11986 bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
11987 bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
11988
11989 // FIXME: Missing: conditional operator (for GNU
11990 // conditional select), ExtVectorElementExpr
11991 };
11992} // end anonymous namespace
11993
11994static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
11995 assert(E->isPRValue() && E->getType()->isVectorType() &&
11996 "not a vector prvalue");
11997 return VectorExprEvaluator(Info, Result).Visit(S: E);
11998}
11999
12000static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) {
12001 assert(Val.isVector() && "expected vector APValue");
12002 unsigned NumElts = Val.getVectorLength();
12003
12004 // Each element is one bit, so create an integer with NumElts bits.
12005 llvm::APInt Result(NumElts, 0);
12006
12007 for (unsigned I = 0; I < NumElts; ++I) {
12008 const APValue &Elt = Val.getVectorElt(I);
12009 assert(Elt.isInt() && "expected integer element in bool vector");
12010
12011 if (Elt.getInt().getBoolValue())
12012 Result.setBit(I);
12013 }
12014
12015 return Result;
12016}
12017
12018bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
12019 const VectorType *VTy = E->getType()->castAs<VectorType>();
12020 unsigned NElts = VTy->getNumElements();
12021
12022 const Expr *SE = E->getSubExpr();
12023 QualType SETy = SE->getType();
12024
12025 switch (E->getCastKind()) {
12026 case CK_VectorSplat: {
12027 APValue Val = APValue();
12028 if (SETy->isIntegerType()) {
12029 APSInt IntResult;
12030 if (!EvaluateInteger(E: SE, Result&: IntResult, Info))
12031 return false;
12032 Val = APValue(std::move(IntResult));
12033 } else if (SETy->isRealFloatingType()) {
12034 APFloat FloatResult(0.0);
12035 if (!EvaluateFloat(E: SE, Result&: FloatResult, Info))
12036 return false;
12037 Val = APValue(std::move(FloatResult));
12038 } else {
12039 return Error(E);
12040 }
12041
12042 // Splat and create vector APValue.
12043 SmallVector<APValue, 4> Elts(NElts, Val);
12044 return Success(V: Elts, E);
12045 }
12046 case CK_BitCast: {
12047 APValue SVal;
12048 if (!Evaluate(Result&: SVal, Info, E: SE))
12049 return false;
12050
12051 if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) {
12052 // Give up if the input isn't an int, float, or vector. For example, we
12053 // reject "(v4i16)(intptr_t)&a".
12054 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_cast)
12055 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
12056 << Info.Ctx.getLangOpts().CPlusPlus;
12057 return false;
12058 }
12059
12060 if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E))
12061 return false;
12062
12063 return true;
12064 }
12065 case CK_HLSLVectorTruncation: {
12066 APValue Val;
12067 SmallVector<APValue, 4> Elements;
12068 if (!EvaluateVector(E: SE, Result&: Val, Info))
12069 return Error(E);
12070 for (unsigned I = 0; I < NElts; I++)
12071 Elements.push_back(Elt: Val.getVectorElt(I));
12072 return Success(V: Elements, E);
12073 }
12074 case CK_HLSLMatrixTruncation: {
12075 // Matrix truncation occurs in row-major order.
12076 APValue Val;
12077 if (!EvaluateMatrix(E: SE, Result&: Val, Info))
12078 return Error(E);
12079 SmallVector<APValue, 16> Elements;
12080 for (unsigned Row = 0;
12081 Row < Val.getMatrixNumRows() && Elements.size() < NElts; Row++)
12082 for (unsigned Col = 0;
12083 Col < Val.getMatrixNumColumns() && Elements.size() < NElts; Col++)
12084 Elements.push_back(Elt: Val.getMatrixElt(Row, Col));
12085 return Success(V: Elements, E);
12086 }
12087 case CK_HLSLAggregateSplatCast: {
12088 APValue Val;
12089 QualType ValTy;
12090
12091 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
12092 return false;
12093
12094 // cast our Val once.
12095 APValue Result;
12096 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12097 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: VTy->getElementType(), Original: Val,
12098 Result))
12099 return false;
12100
12101 SmallVector<APValue, 4> SplatEls(NElts, Result);
12102 return Success(V: SplatEls, E);
12103 }
12104 case CK_HLSLElementwiseCast: {
12105 SmallVector<APValue> SrcVals;
12106 SmallVector<QualType> SrcTypes;
12107
12108 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
12109 return false;
12110
12111 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12112 SmallVector<QualType, 4> DestTypes(NElts, VTy->getElementType());
12113 SmallVector<APValue, 4> ResultEls(NElts);
12114 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
12115 Results&: ResultEls))
12116 return false;
12117 return Success(V: ResultEls, E);
12118 }
12119 case CK_IntegralToFloating:
12120 case CK_FloatingToIntegral:
12121 case CK_IntegralCast:
12122 case CK_FloatingCast:
12123 case CK_FloatingToBoolean:
12124 case CK_IntegralToBoolean: {
12125 // These casts apply element-wise when the source is a vector type.
12126 assert(SETy->isVectorType() && "expected vector source type");
12127 APValue SrcVal;
12128 if (!EvaluateVector(E: SE, Result&: SrcVal, Info))
12129 return Error(E);
12130
12131 assert(SrcVal.getVectorLength() == NElts);
12132 QualType SrcEltTy = SETy->castAs<VectorType>()->getElementType();
12133 QualType DstEltTy = VTy->getElementType();
12134 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
12135
12136 SmallVector<APValue, 4> ResultEls(NElts);
12137 for (unsigned I = 0; I < NElts; ++I) {
12138 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcEltTy, DestTy: DstEltTy,
12139 Original: SrcVal.getVectorElt(I), Result&: ResultEls[I]))
12140 return Error(E);
12141 }
12142 return Success(V: ResultEls, E);
12143 }
12144 default:
12145 return ExprEvaluatorBaseTy::VisitCastExpr(E);
12146 }
12147}
12148
12149bool
12150VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
12151 const VectorType *VT = E->getType()->castAs<VectorType>();
12152 unsigned NumInits = E->getNumInits();
12153 unsigned NumElements = VT->getNumElements();
12154
12155 QualType EltTy = VT->getElementType();
12156 SmallVector<APValue, 4> Elements;
12157
12158 // MFloat8 type doesn't have constants and thus constant folding
12159 // is impossible.
12160 if (EltTy->isMFloat8Type())
12161 return false;
12162
12163 // The number of initializers can be less than the number of
12164 // vector elements. For OpenCL, this can be due to nested vector
12165 // initialization. For GCC compatibility, missing trailing elements
12166 // should be initialized with zeroes.
12167 unsigned CountInits = 0, CountElts = 0;
12168 while (CountElts < NumElements) {
12169 // Handle nested vector initialization.
12170 if (CountInits < NumInits
12171 && E->getInit(Init: CountInits)->getType()->isVectorType()) {
12172 APValue v;
12173 if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info))
12174 return Error(E);
12175 unsigned vlen = v.getVectorLength();
12176 for (unsigned j = 0; j < vlen; j++)
12177 Elements.push_back(Elt: v.getVectorElt(I: j));
12178 CountElts += vlen;
12179 } else if (EltTy->isIntegerType()) {
12180 llvm::APSInt sInt(32);
12181 if (CountInits < NumInits) {
12182 if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info))
12183 return false;
12184 } else // trailing integer zero.
12185 sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy);
12186 Elements.push_back(Elt: APValue(sInt));
12187 CountElts++;
12188 } else {
12189 llvm::APFloat f(0.0);
12190 if (CountInits < NumInits) {
12191 if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info))
12192 return false;
12193 } else // trailing float zero.
12194 f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy));
12195 Elements.push_back(Elt: APValue(f));
12196 CountElts++;
12197 }
12198 CountInits++;
12199 }
12200 return Success(V: Elements, E);
12201}
12202
12203bool
12204VectorExprEvaluator::ZeroInitialization(const Expr *E) {
12205 const auto *VT = E->getType()->castAs<VectorType>();
12206 QualType EltTy = VT->getElementType();
12207 APValue ZeroElement;
12208 if (EltTy->isIntegerType())
12209 ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy));
12210 else
12211 ZeroElement =
12212 APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)));
12213
12214 SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
12215 return Success(V: Elements, E);
12216}
12217
12218bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
12219 VisitIgnoredValue(E: E->getSubExpr());
12220 return ZeroInitialization(E);
12221}
12222
12223bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
12224 BinaryOperatorKind Op = E->getOpcode();
12225 assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp &&
12226 "Operation not supported on vector types");
12227
12228 if (Op == BO_Comma)
12229 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
12230
12231 Expr *LHS = E->getLHS();
12232 Expr *RHS = E->getRHS();
12233
12234 assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() &&
12235 "Must both be vector types");
12236 // Checking JUST the types are the same would be fine, except shifts don't
12237 // need to have their types be the same (since you always shift by an int).
12238 assert(LHS->getType()->castAs<VectorType>()->getNumElements() ==
12239 E->getType()->castAs<VectorType>()->getNumElements() &&
12240 RHS->getType()->castAs<VectorType>()->getNumElements() ==
12241 E->getType()->castAs<VectorType>()->getNumElements() &&
12242 "All operands must be the same size.");
12243
12244 APValue LHSValue;
12245 APValue RHSValue;
12246 bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS);
12247 if (!LHSOK && !Info.noteFailure())
12248 return false;
12249 if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK)
12250 return false;
12251
12252 if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue))
12253 return false;
12254
12255 return Success(V: LHSValue, E);
12256}
12257
12258static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx,
12259 QualType ResultTy,
12260 UnaryOperatorKind Op,
12261 APValue Elt) {
12262 switch (Op) {
12263 case UO_Plus:
12264 // Nothing to do here.
12265 return Elt;
12266 case UO_Minus:
12267 if (Elt.getKind() == APValue::Int) {
12268 Elt.getInt().negate();
12269 } else {
12270 assert(Elt.getKind() == APValue::Float &&
12271 "Vector can only be int or float type");
12272 Elt.getFloat().changeSign();
12273 }
12274 return Elt;
12275 case UO_Not:
12276 // This is only valid for integral types anyway, so we don't have to handle
12277 // float here.
12278 assert(Elt.getKind() == APValue::Int &&
12279 "Vector operator ~ can only be int");
12280 Elt.getInt().flipAllBits();
12281 return Elt;
12282 case UO_LNot: {
12283 if (Elt.getKind() == APValue::Int) {
12284 Elt.getInt() = !Elt.getInt();
12285 // operator ! on vectors returns -1 for 'truth', so negate it.
12286 Elt.getInt().negate();
12287 return Elt;
12288 }
12289 assert(Elt.getKind() == APValue::Float &&
12290 "Vector can only be int or float type");
12291 // Float types result in an int of the same size, but -1 for true, or 0 for
12292 // false.
12293 APSInt EltResult{Ctx.getIntWidth(T: ResultTy),
12294 ResultTy->isUnsignedIntegerType()};
12295 if (Elt.getFloat().isZero())
12296 EltResult.setAllBits();
12297 else
12298 EltResult.clearAllBits();
12299
12300 return APValue{EltResult};
12301 }
12302 default:
12303 // FIXME: Implement the rest of the unary operators.
12304 return std::nullopt;
12305 }
12306}
12307
12308bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
12309 Expr *SubExpr = E->getSubExpr();
12310 const auto *VD = SubExpr->getType()->castAs<VectorType>();
12311 // This result element type differs in the case of negating a floating point
12312 // vector, since the result type is the a vector of the equivilant sized
12313 // integer.
12314 const QualType ResultEltTy = VD->getElementType();
12315 UnaryOperatorKind Op = E->getOpcode();
12316
12317 APValue SubExprValue;
12318 if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr))
12319 return false;
12320
12321 // FIXME: This vector evaluator someday needs to be changed to be LValue
12322 // aware/keep LValue information around, rather than dealing with just vector
12323 // types directly. Until then, we cannot handle cases where the operand to
12324 // these unary operators is an LValue. The only case I've been able to see
12325 // cause this is operator++ assigning to a member expression (only valid in
12326 // altivec compilations) in C mode, so this shouldn't limit us too much.
12327 if (SubExprValue.isLValue())
12328 return false;
12329
12330 assert(SubExprValue.getVectorLength() == VD->getNumElements() &&
12331 "Vector length doesn't match type?");
12332
12333 SmallVector<APValue, 4> ResultElements;
12334 for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) {
12335 std::optional<APValue> Elt = handleVectorUnaryOperator(
12336 Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum));
12337 if (!Elt)
12338 return false;
12339 ResultElements.push_back(Elt: *Elt);
12340 }
12341 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12342}
12343
12344static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,
12345 const Expr *E, QualType SourceTy,
12346 QualType DestTy, APValue const &Original,
12347 APValue &Result) {
12348 if (SourceTy->isIntegerType()) {
12349 if (DestTy->isRealFloatingType()) {
12350 Result = APValue(APFloat(0.0));
12351 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
12352 DestType: DestTy, Result&: Result.getFloat());
12353 }
12354 if (DestTy->isIntegerType()) {
12355 Result = APValue(
12356 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
12357 return true;
12358 }
12359 } else if (SourceTy->isRealFloatingType()) {
12360 if (DestTy->isRealFloatingType()) {
12361 Result = Original;
12362 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
12363 Result&: Result.getFloat());
12364 }
12365 if (DestTy->isIntegerType()) {
12366 Result = APValue(APSInt());
12367 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
12368 DestType: DestTy, Result&: Result.getInt());
12369 }
12370 }
12371
12372 Info.FFDiag(E, DiagId: diag::err_convertvector_constexpr_unsupported_vector_cast)
12373 << SourceTy << DestTy;
12374 return false;
12375}
12376
12377static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result,
12378 llvm::function_ref<APInt(const APSInt &)> PackFn) {
12379 APValue LHS, RHS;
12380 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: LHS) ||
12381 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: RHS))
12382 return false;
12383
12384 unsigned LHSVecLen = LHS.getVectorLength();
12385 unsigned RHSVecLen = RHS.getVectorLength();
12386
12387 assert(LHSVecLen != 0 && LHSVecLen == RHSVecLen &&
12388 "pack builtin LHSVecLen must equal to RHSVecLen");
12389
12390 const VectorType *VT0 = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
12391 const unsigned SrcBits = Info.Ctx.getIntWidth(T: VT0->getElementType());
12392
12393 const VectorType *DstVT = E->getType()->castAs<VectorType>();
12394 QualType DstElemTy = DstVT->getElementType();
12395 const bool DstIsUnsigned = DstElemTy->isUnsignedIntegerType();
12396
12397 const unsigned SrcPerLane = 128 / SrcBits;
12398 const unsigned Lanes = LHSVecLen * SrcBits / 128;
12399
12400 SmallVector<APValue, 64> Out;
12401 Out.reserve(N: LHSVecLen + RHSVecLen);
12402
12403 for (unsigned Lane = 0; Lane != Lanes; ++Lane) {
12404 unsigned base = Lane * SrcPerLane;
12405 for (unsigned I = 0; I != SrcPerLane; ++I)
12406 Out.emplace_back(Args: APValue(
12407 APSInt(PackFn(LHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12408 for (unsigned I = 0; I != SrcPerLane; ++I)
12409 Out.emplace_back(Args: APValue(
12410 APSInt(PackFn(RHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12411 }
12412
12413 Result = APValue(Out.data(), Out.size());
12414 return true;
12415}
12416
12417static bool evalShuffleGeneric(
12418 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12419 llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)>
12420 GetSourceIndex) {
12421
12422 const auto *VT = Call->getType()->getAs<VectorType>();
12423 if (!VT)
12424 return false;
12425
12426 unsigned ShuffleMask = 0;
12427 APValue A, MaskVector, B;
12428 bool IsVectorMask = false;
12429 bool IsSingleOperand = (Call->getNumArgs() == 2);
12430
12431 if (IsSingleOperand) {
12432 QualType MaskType = Call->getArg(Arg: 1)->getType();
12433 if (MaskType->isVectorType()) {
12434 IsVectorMask = true;
12435 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12436 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector))
12437 return false;
12438 B = A;
12439 } else if (MaskType->isIntegerType()) {
12440 APSInt MaskImm;
12441 if (!EvaluateInteger(E: Call->getArg(Arg: 1), Result&: MaskImm, Info))
12442 return false;
12443 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12444 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A))
12445 return false;
12446 B = A;
12447 } else {
12448 return false;
12449 }
12450 } else {
12451 QualType Arg2Type = Call->getArg(Arg: 2)->getType();
12452 if (Arg2Type->isVectorType()) {
12453 IsVectorMask = true;
12454 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12455 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector) ||
12456 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 2), Result&: B))
12457 return false;
12458 } else if (Arg2Type->isIntegerType()) {
12459 APSInt MaskImm;
12460 if (!EvaluateInteger(E: Call->getArg(Arg: 2), Result&: MaskImm, Info))
12461 return false;
12462 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12463 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12464 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: B))
12465 return false;
12466 } else {
12467 return false;
12468 }
12469 }
12470
12471 unsigned NumElts = VT->getNumElements();
12472 SmallVector<APValue, 64> ResultElements;
12473 ResultElements.reserve(N: NumElts);
12474
12475 for (unsigned DstIdx = 0; DstIdx != NumElts; ++DstIdx) {
12476 if (IsVectorMask) {
12477 ShuffleMask = static_cast<unsigned>(
12478 MaskVector.getVectorElt(I: DstIdx).getInt().getZExtValue());
12479 }
12480 auto [SrcVecIdx, SrcIdx] = GetSourceIndex(DstIdx, ShuffleMask);
12481
12482 if (SrcIdx < 0) {
12483 // Zero out this element
12484 QualType ElemTy = VT->getElementType();
12485 if (ElemTy->isRealFloatingType()) {
12486 ResultElements.push_back(
12487 Elt: APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy))));
12488 } else if (ElemTy->isIntegerType()) {
12489 APValue Zero(Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy));
12490 ResultElements.push_back(Elt: APValue(Zero));
12491 } else {
12492 // Other types of fallback logic
12493 ResultElements.push_back(Elt: APValue());
12494 }
12495 } else {
12496 const APValue &Src = (SrcVecIdx == 0) ? A : B;
12497 ResultElements.push_back(Elt: Src.getVectorElt(I: SrcIdx));
12498 }
12499 }
12500
12501 Out = APValue(ResultElements.data(), ResultElements.size());
12502 return true;
12503}
12504static bool ConvertDoubleToFloatStrict(EvalInfo &Info, const Expr *E,
12505 APFloat OrigVal, APValue &Result) {
12506
12507 if (OrigVal.isInfinity()) {
12508 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 0;
12509 return false;
12510 }
12511 if (OrigVal.isNaN()) {
12512 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 1;
12513 return false;
12514 }
12515
12516 APFloat Val = OrigVal;
12517 bool LosesInfo = false;
12518 APFloat::opStatus Status = Val.convert(
12519 ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, losesInfo: &LosesInfo);
12520
12521 if (LosesInfo || Val.isDenormal()) {
12522 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
12523 return false;
12524 }
12525
12526 if (Status != APFloat::opOK) {
12527 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
12528 return false;
12529 }
12530
12531 Result = APValue(Val);
12532 return true;
12533}
12534static bool evalShiftWithCount(
12535 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12536 llvm::function_ref<APInt(const APInt &, uint64_t)> ShiftOp,
12537 llvm::function_ref<APInt(const APInt &, unsigned)> OverflowOp) {
12538
12539 APValue Source, Count;
12540 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: Source) ||
12541 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: Count))
12542 return false;
12543
12544 assert(Call->getNumArgs() == 2);
12545
12546 QualType SourceTy = Call->getArg(Arg: 0)->getType();
12547 assert(SourceTy->isVectorType() &&
12548 Call->getArg(1)->getType()->isVectorType());
12549
12550 QualType DestEltTy = SourceTy->castAs<VectorType>()->getElementType();
12551 unsigned DestEltWidth = Source.getVectorElt(I: 0).getInt().getBitWidth();
12552 unsigned DestLen = Source.getVectorLength();
12553 bool IsDestUnsigned = DestEltTy->isUnsignedIntegerType();
12554 unsigned CountEltWidth = Count.getVectorElt(I: 0).getInt().getBitWidth();
12555 unsigned NumBitsInQWord = 64;
12556 unsigned NumCountElts = NumBitsInQWord / CountEltWidth;
12557 SmallVector<APValue, 64> Result;
12558 Result.reserve(N: DestLen);
12559
12560 uint64_t CountLQWord = 0;
12561 for (unsigned EltIdx = 0; EltIdx != NumCountElts; ++EltIdx) {
12562 uint64_t Elt = Count.getVectorElt(I: EltIdx).getInt().getZExtValue();
12563 CountLQWord |= (Elt << (EltIdx * CountEltWidth));
12564 }
12565
12566 for (unsigned EltIdx = 0; EltIdx != DestLen; ++EltIdx) {
12567 APInt Elt = Source.getVectorElt(I: EltIdx).getInt();
12568 if (CountLQWord < DestEltWidth) {
12569 Result.push_back(
12570 Elt: APValue(APSInt(ShiftOp(Elt, CountLQWord), IsDestUnsigned)));
12571 } else {
12572 Result.push_back(
12573 Elt: APValue(APSInt(OverflowOp(Elt, DestEltWidth), IsDestUnsigned)));
12574 }
12575 }
12576 Out = APValue(Result.data(), Result.size());
12577 return true;
12578}
12579
12580std::optional<APFloat> EvalScalarMinMaxFp(const APFloat &A, const APFloat &B,
12581 std::optional<APSInt> RoundingMode,
12582 bool IsMin) {
12583 APSInt DefaultMode(APInt(32, 4), /*isUnsigned=*/true);
12584 if (RoundingMode.value_or(u&: DefaultMode) != 4)
12585 return std::nullopt;
12586 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
12587 B.isInfinity() || B.isDenormal())
12588 return std::nullopt;
12589 if (A.isZero() && B.isZero())
12590 return B;
12591 return IsMin ? llvm::minimum(A, B) : llvm::maximum(A, B);
12592}
12593
12594bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
12595 if (!IsConstantEvaluatedBuiltinCall(E))
12596 return ExprEvaluatorBaseTy::VisitCallExpr(E);
12597
12598 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
12599
12600 auto EvaluateBinOpExpr =
12601 [&](llvm::function_ref<APInt(const APSInt &, const APSInt &)> Fn) {
12602 APValue SourceLHS, SourceRHS;
12603 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12604 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12605 return false;
12606
12607 auto *DestTy = E->getType()->castAs<VectorType>();
12608 QualType DestEltTy = DestTy->getElementType();
12609 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12610 unsigned SourceLen = SourceLHS.getVectorLength();
12611 SmallVector<APValue, 4> ResultElements;
12612 ResultElements.reserve(N: SourceLen);
12613
12614 if (SourceRHS.isInt()) {
12615 const APSInt &RHS = SourceRHS.getInt();
12616 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12617 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12618 ResultElements.push_back(
12619 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12620 }
12621 } else {
12622 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12623 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12624 const APSInt &RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12625 ResultElements.push_back(
12626 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12627 }
12628 }
12629 return Success(V: APValue(ResultElements.data(), SourceLen), E);
12630 };
12631
12632 auto EvaluateFpBinOpExpr =
12633 [&](llvm::function_ref<std::optional<APFloat>(
12634 const APFloat &, const APFloat &, std::optional<APSInt>)>
12635 Fn,
12636 bool IsScalar = false) {
12637 assert(E->getNumArgs() == 2 || E->getNumArgs() == 3);
12638 APValue A, B;
12639 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
12640 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
12641 return false;
12642
12643 assert(A.isVector() && B.isVector());
12644 assert(A.getVectorLength() == B.getVectorLength());
12645
12646 std::optional<APSInt> RoundingMode;
12647 if (E->getNumArgs() == 3) {
12648 APSInt Imm;
12649 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
12650 return false;
12651 RoundingMode = Imm;
12652 }
12653
12654 unsigned NumElems = A.getVectorLength();
12655 SmallVector<APValue, 4> ResultElements;
12656 ResultElements.reserve(N: NumElems);
12657
12658 for (unsigned EltNum = 0; EltNum < NumElems; ++EltNum) {
12659 if (IsScalar && EltNum > 0) {
12660 ResultElements.push_back(Elt: A.getVectorElt(I: EltNum));
12661 continue;
12662 }
12663 const APFloat &EltA = A.getVectorElt(I: EltNum).getFloat();
12664 const APFloat &EltB = B.getVectorElt(I: EltNum).getFloat();
12665 std::optional<APFloat> Result = Fn(EltA, EltB, RoundingMode);
12666 if (!Result)
12667 return false;
12668 ResultElements.push_back(Elt: APValue(*Result));
12669 }
12670 return Success(V: APValue(ResultElements.data(), NumElems), E);
12671 };
12672
12673 auto EvaluateScalarFpRoundMaskBinOp =
12674 [&](llvm::function_ref<std::optional<APFloat>(
12675 const APFloat &, const APFloat &, std::optional<APSInt>)>
12676 Fn) {
12677 assert(E->getNumArgs() == 5);
12678 APValue VecA, VecB, VecSrc;
12679 APSInt MaskVal, Rounding;
12680
12681 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
12682 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
12683 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
12684 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskVal, Info) ||
12685 !EvaluateInteger(E: E->getArg(Arg: 4), Result&: Rounding, Info))
12686 return false;
12687
12688 unsigned NumElems = VecA.getVectorLength();
12689 SmallVector<APValue, 8> ResultElements;
12690 ResultElements.reserve(N: NumElems);
12691
12692 if (MaskVal.getZExtValue() & 1) {
12693 const APFloat &EltA = VecA.getVectorElt(I: 0).getFloat();
12694 const APFloat &EltB = VecB.getVectorElt(I: 0).getFloat();
12695 std::optional<APFloat> Result = Fn(EltA, EltB, Rounding);
12696 if (!Result)
12697 return false;
12698 ResultElements.push_back(Elt: APValue(*Result));
12699 } else {
12700 ResultElements.push_back(Elt: VecSrc.getVectorElt(I: 0));
12701 }
12702
12703 for (unsigned I = 1; I < NumElems; ++I)
12704 ResultElements.push_back(Elt: VecA.getVectorElt(I));
12705
12706 return Success(V: APValue(ResultElements.data(), NumElems), E);
12707 };
12708
12709 auto EvalSelectScalar = [&](unsigned Len) -> bool {
12710 APSInt Mask;
12711 APValue AVal, WVal;
12712 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info) ||
12713 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: AVal) ||
12714 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: WVal))
12715 return false;
12716
12717 bool TakeA0 = (Mask.getZExtValue() & 1u) != 0;
12718 SmallVector<APValue, 4> Res;
12719 Res.reserve(N: Len);
12720 Res.push_back(Elt: TakeA0 ? AVal.getVectorElt(I: 0) : WVal.getVectorElt(I: 0));
12721 for (unsigned I = 1; I < Len; ++I)
12722 Res.push_back(Elt: WVal.getVectorElt(I));
12723 APValue V(Res.data(), Res.size());
12724 return Success(V, E);
12725 };
12726
12727 auto EvalVectorDotProduct = [&](bool IsSaturating) -> bool {
12728 APValue Source, OperandA, OperandB;
12729 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
12730 !EvaluateVector(E: E->getArg(Arg: 1), Result&: OperandA, Info) ||
12731 !EvaluateVector(E: E->getArg(Arg: 2), Result&: OperandB, Info)) {
12732 return false;
12733 }
12734
12735 unsigned NumSrcElems = Source.getVectorLength();
12736 unsigned NumOperandElems = OperandA.getVectorLength();
12737 unsigned ElemsPerLane = NumOperandElems / NumSrcElems;
12738
12739 assert(OperandA.getVectorLength() == OperandB.getVectorLength());
12740
12741 SmallVector<APValue, 16> Result;
12742 Result.reserve(N: NumSrcElems);
12743 for (unsigned I = 0; I != NumSrcElems; ++I) {
12744 APSInt DotProduct = Source.getVectorElt(I).getInt();
12745 DotProduct = DotProduct.extend(width: 64);
12746 for (unsigned J = 0; J != ElemsPerLane; ++J) {
12747 APSInt OpA = APSInt(
12748 OperandA.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12749 false);
12750 APSInt OpB = APSInt(
12751 OperandB.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12752 false);
12753 DotProduct += OpA * OpB;
12754 }
12755 if (IsSaturating) {
12756 DotProduct = APSInt(DotProduct.truncSSat(width: 32), false);
12757 } else {
12758 DotProduct = APSInt(DotProduct.trunc(width: 32), false);
12759 }
12760 Result.push_back(Elt: APValue(DotProduct));
12761 }
12762
12763 return Success(V: APValue(Result.data(), Result.size()), E);
12764 };
12765
12766 switch (BuiltinOp) {
12767 default:
12768 return false;
12769 case Builtin::BI__builtin_elementwise_popcount:
12770 case Builtin::BI__builtin_elementwise_bitreverse: {
12771 APValue Source;
12772 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12773 return false;
12774
12775 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12776 unsigned SourceLen = Source.getVectorLength();
12777 SmallVector<APValue, 4> ResultElements;
12778 ResultElements.reserve(N: SourceLen);
12779
12780 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12781 APSInt Elt = Source.getVectorElt(I: EltNum).getInt();
12782 switch (BuiltinOp) {
12783 case Builtin::BI__builtin_elementwise_popcount:
12784 ResultElements.push_back(Elt: APValue(
12785 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), Elt.popcount()),
12786 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12787 break;
12788 case Builtin::BI__builtin_elementwise_bitreverse:
12789 ResultElements.push_back(
12790 Elt: APValue(APSInt(Elt.reverseBits(),
12791 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12792 break;
12793 }
12794 }
12795
12796 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12797 }
12798 case Builtin::BI__builtin_elementwise_abs: {
12799 APValue Source;
12800 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12801 return false;
12802
12803 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12804 unsigned SourceLen = Source.getVectorLength();
12805 SmallVector<APValue, 4> ResultElements;
12806 ResultElements.reserve(N: SourceLen);
12807
12808 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12809 APValue CurrentEle = Source.getVectorElt(I: EltNum);
12810 APValue Val = DestEltTy->isFloatingType()
12811 ? APValue(llvm::abs(X: CurrentEle.getFloat()))
12812 : APValue(APSInt(
12813 CurrentEle.getInt().abs(),
12814 DestEltTy->isUnsignedIntegerOrEnumerationType()));
12815 ResultElements.push_back(Elt: Val);
12816 }
12817
12818 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12819 }
12820
12821 case Builtin::BI__builtin_elementwise_add_sat:
12822 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12823 return LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
12824 });
12825
12826 case Builtin::BI__builtin_elementwise_sub_sat:
12827 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12828 return LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
12829 });
12830
12831 case X86::BI__builtin_ia32_extract128i256:
12832 case X86::BI__builtin_ia32_vextractf128_pd256:
12833 case X86::BI__builtin_ia32_vextractf128_ps256:
12834 case X86::BI__builtin_ia32_vextractf128_si256: {
12835 APValue SourceVec, SourceImm;
12836 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12837 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceImm))
12838 return false;
12839
12840 if (!SourceVec.isVector())
12841 return false;
12842
12843 const auto *RetVT = E->getType()->castAs<VectorType>();
12844 unsigned RetLen = RetVT->getNumElements();
12845 unsigned Idx = SourceImm.getInt().getZExtValue() & 1;
12846
12847 SmallVector<APValue, 32> ResultElements;
12848 ResultElements.reserve(N: RetLen);
12849
12850 for (unsigned I = 0; I < RetLen; I++)
12851 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Idx * RetLen + I));
12852
12853 return Success(V: APValue(ResultElements.data(), RetLen), E);
12854 }
12855
12856 case clang::X86::BI__builtin_ia32_cvtmask2b128:
12857 case clang::X86::BI__builtin_ia32_cvtmask2b256:
12858 case clang::X86::BI__builtin_ia32_cvtmask2b512:
12859 case clang::X86::BI__builtin_ia32_cvtmask2w128:
12860 case clang::X86::BI__builtin_ia32_cvtmask2w256:
12861 case clang::X86::BI__builtin_ia32_cvtmask2w512:
12862 case clang::X86::BI__builtin_ia32_cvtmask2d128:
12863 case clang::X86::BI__builtin_ia32_cvtmask2d256:
12864 case clang::X86::BI__builtin_ia32_cvtmask2d512:
12865 case clang::X86::BI__builtin_ia32_cvtmask2q128:
12866 case clang::X86::BI__builtin_ia32_cvtmask2q256:
12867 case clang::X86::BI__builtin_ia32_cvtmask2q512: {
12868 assert(E->getNumArgs() == 1);
12869 APSInt Mask;
12870 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info))
12871 return false;
12872
12873 QualType VecTy = E->getType();
12874 const VectorType *VT = VecTy->castAs<VectorType>();
12875 unsigned VectorLen = VT->getNumElements();
12876 QualType ElemTy = VT->getElementType();
12877 unsigned ElemWidth = Info.Ctx.getTypeSize(T: ElemTy);
12878
12879 SmallVector<APValue, 16> Elems;
12880 for (unsigned I = 0; I != VectorLen; ++I) {
12881 bool BitSet = Mask[I];
12882 APSInt ElemVal(ElemWidth, /*isUnsigned=*/false);
12883 if (BitSet) {
12884 ElemVal.setAllBits();
12885 }
12886 Elems.push_back(Elt: APValue(ElemVal));
12887 }
12888 return Success(V: APValue(Elems.data(), VectorLen), E);
12889 }
12890
12891 case X86::BI__builtin_ia32_extracti32x4_256_mask:
12892 case X86::BI__builtin_ia32_extractf32x4_256_mask:
12893 case X86::BI__builtin_ia32_extracti32x4_mask:
12894 case X86::BI__builtin_ia32_extractf32x4_mask:
12895 case X86::BI__builtin_ia32_extracti32x8_mask:
12896 case X86::BI__builtin_ia32_extractf32x8_mask:
12897 case X86::BI__builtin_ia32_extracti64x2_256_mask:
12898 case X86::BI__builtin_ia32_extractf64x2_256_mask:
12899 case X86::BI__builtin_ia32_extracti64x2_512_mask:
12900 case X86::BI__builtin_ia32_extractf64x2_512_mask:
12901 case X86::BI__builtin_ia32_extracti64x4_mask:
12902 case X86::BI__builtin_ia32_extractf64x4_mask: {
12903 APValue SourceVec, MergeVec;
12904 APSInt Imm, MaskImm;
12905
12906 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12907 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info) ||
12908 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MergeVec) ||
12909 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskImm, Info))
12910 return false;
12911
12912 const auto *RetVT = E->getType()->castAs<VectorType>();
12913 unsigned RetLen = RetVT->getNumElements();
12914
12915 if (!SourceVec.isVector() || !MergeVec.isVector())
12916 return false;
12917 unsigned SrcLen = SourceVec.getVectorLength();
12918 unsigned Lanes = SrcLen / RetLen;
12919 unsigned Lane = static_cast<unsigned>(Imm.getZExtValue() % Lanes);
12920 unsigned Base = Lane * RetLen;
12921
12922 SmallVector<APValue, 32> ResultElements;
12923 ResultElements.reserve(N: RetLen);
12924 for (unsigned I = 0; I < RetLen; ++I) {
12925 if (MaskImm[I])
12926 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Base + I));
12927 else
12928 ResultElements.push_back(Elt: MergeVec.getVectorElt(I));
12929 }
12930 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12931 }
12932
12933 case clang::X86::BI__builtin_ia32_pavgb128:
12934 case clang::X86::BI__builtin_ia32_pavgw128:
12935 case clang::X86::BI__builtin_ia32_pavgb256:
12936 case clang::X86::BI__builtin_ia32_pavgw256:
12937 case clang::X86::BI__builtin_ia32_pavgb512:
12938 case clang::X86::BI__builtin_ia32_pavgw512:
12939 return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU);
12940
12941 case clang::X86::BI__builtin_ia32_pmulhrsw128:
12942 case clang::X86::BI__builtin_ia32_pmulhrsw256:
12943 case clang::X86::BI__builtin_ia32_pmulhrsw512:
12944 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12945 return (llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS).ashr(ShiftAmt: 14) + 1)
12946 .extractBits(numBits: 16, bitPosition: 1);
12947 });
12948
12949 case clang::X86::BI__builtin_ia32_psadbw128:
12950 case clang::X86::BI__builtin_ia32_psadbw256:
12951 case clang::X86::BI__builtin_ia32_psadbw512: {
12952 APValue SourceLHS, SourceRHS;
12953 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12954 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12955 return false;
12956
12957 assert(SourceLHS.isVector() && SourceRHS.isVector());
12958 unsigned SourceLen = SourceLHS.getVectorLength();
12959 assert(SourceLen == SourceRHS.getVectorLength());
12960 assert((SourceLen % 8) == 0);
12961
12962 auto *DestTy = E->getType()->castAs<VectorType>();
12963 QualType DestEltTy = DestTy->getElementType();
12964 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12965 SmallVector<APValue, 8> ResultElements;
12966 ResultElements.reserve(N: SourceLen / 8);
12967
12968 for (unsigned Lane = 0; Lane != SourceLen; Lane += 8) {
12969 APInt Sum(64, 0);
12970 for (unsigned I = 0; I != 8; ++I) {
12971 APInt LHS = SourceLHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
12972 APInt RHS = SourceRHS.getVectorElt(I: Lane + I).getInt().extOrTrunc(width: 8);
12973 Sum += llvm::APIntOps::abdu(A: LHS, B: RHS).zext(width: 64);
12974 }
12975 ResultElements.push_back(Elt: APValue(APSInt(Sum, DestUnsigned)));
12976 }
12977
12978 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12979 }
12980
12981 case clang::X86::BI__builtin_ia32_pmaddubsw128:
12982 case clang::X86::BI__builtin_ia32_pmaddubsw256:
12983 case clang::X86::BI__builtin_ia32_pmaddubsw512:
12984 case clang::X86::BI__builtin_ia32_pmaddwd128:
12985 case clang::X86::BI__builtin_ia32_pmaddwd256:
12986 case clang::X86::BI__builtin_ia32_pmaddwd512: {
12987 APValue SourceLHS, SourceRHS;
12988 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12989 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12990 return false;
12991
12992 auto *DestTy = E->getType()->castAs<VectorType>();
12993 QualType DestEltTy = DestTy->getElementType();
12994 unsigned SourceLen = SourceLHS.getVectorLength();
12995 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12996 SmallVector<APValue, 4> ResultElements;
12997 ResultElements.reserve(N: SourceLen / 2);
12998
12999 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13000 const APSInt &LoLHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13001 const APSInt &HiLHS = SourceLHS.getVectorElt(I: EltNum + 1).getInt();
13002 const APSInt &LoRHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13003 const APSInt &HiRHS = SourceRHS.getVectorElt(I: EltNum + 1).getInt();
13004 unsigned BitWidth = 2 * LoLHS.getBitWidth();
13005
13006 switch (BuiltinOp) {
13007 case clang::X86::BI__builtin_ia32_pmaddubsw128:
13008 case clang::X86::BI__builtin_ia32_pmaddubsw256:
13009 case clang::X86::BI__builtin_ia32_pmaddubsw512:
13010 ResultElements.push_back(Elt: APValue(
13011 APSInt((LoLHS.zext(width: BitWidth) * LoRHS.sext(width: BitWidth))
13012 .sadd_sat(RHS: (HiLHS.zext(width: BitWidth) * HiRHS.sext(width: BitWidth))),
13013 DestUnsigned)));
13014 break;
13015 case clang::X86::BI__builtin_ia32_pmaddwd128:
13016 case clang::X86::BI__builtin_ia32_pmaddwd256:
13017 case clang::X86::BI__builtin_ia32_pmaddwd512:
13018 ResultElements.push_back(
13019 Elt: APValue(APSInt((LoLHS.sext(width: BitWidth) * LoRHS.sext(width: BitWidth)) +
13020 (HiLHS.sext(width: BitWidth) * HiRHS.sext(width: BitWidth)),
13021 DestUnsigned)));
13022 break;
13023 }
13024 }
13025
13026 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13027 }
13028
13029 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v16hi:
13030 case clang::X86::BI__builtin_ia32_bmacor16x16x16_v32hi:
13031 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi:
13032 case clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi: {
13033 // Bit Matrix Multiply and Accumulate (AVX512BMM). Each 256-bit lane holds
13034 // a 16x16 bit matrix as 16 x i16 elements; element i is row i and bit j of
13035 // that element is entry [i][j]. The accumulator (third argument, src1 in
13036 // the AMD ISA) provides the initial value of each result bit, into which
13037 // the bit-matrix product of the first two arguments (src2 * src3) is
13038 // reduced with OR (vbmacor) or XOR (vbmacxor):
13039 // for i in 0..15, j in 0..15:
13040 // bit = C[16*i+j]
13041 // for k in 0..15: bit OP= A[16*i+k] & B[16*k+j]
13042 // dest[16*i+j] = bit
13043 APValue SourceA, SourceB, SourceC;
13044 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13045 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13046 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13047 return false;
13048
13049 bool IsXor = E->getBuiltinCallee() ==
13050 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v16hi ||
13051 E->getBuiltinCallee() ==
13052 clang::X86::BI__builtin_ia32_bmacxor16x16x16_v32hi;
13053
13054 unsigned SourceLen = SourceA.getVectorLength();
13055 assert(SourceLen % 16 == 0 && "BMM operates on 256-bit lanes of 16 x i16");
13056 auto *DestTy = E->getType()->castAs<VectorType>();
13057 QualType DestEltTy = DestTy->getElementType();
13058 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13059
13060 SmallVector<APValue, 32> ResultElements(SourceLen);
13061 for (unsigned Lane = 0; Lane != SourceLen; Lane += 16) {
13062 for (unsigned I = 0; I != 16; ++I) {
13063 uint16_t A =
13064 (uint16_t)SourceA.getVectorElt(I: Lane + I).getInt().getZExtValue();
13065 uint16_t Dst =
13066 (uint16_t)SourceC.getVectorElt(I: Lane + I).getInt().getZExtValue();
13067 for (unsigned J = 0; J != 16; ++J) {
13068 // Seed the reduction with the accumulator bit, then fold in each
13069 // product term with the same operator (OR for vbmacor, XOR for
13070 // vbmacxor).
13071 unsigned Bit = (Dst >> J) & 1u;
13072 for (unsigned K = 0; K != 16; ++K) {
13073 uint16_t B = (uint16_t)SourceB.getVectorElt(I: Lane + K)
13074 .getInt()
13075 .getZExtValue();
13076 unsigned Product = ((A >> K) & 1u) & ((B >> J) & 1u);
13077 Bit = IsXor ? (Bit ^ Product) : (Bit | Product);
13078 }
13079 Dst = (Dst & ~(uint16_t(1) << J)) | (uint16_t(Bit) << J);
13080 }
13081 ResultElements[Lane + I] =
13082 APValue(APSInt(APInt(16, Dst), DestUnsigned));
13083 }
13084 }
13085 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13086 }
13087
13088 case clang::X86::BI__builtin_ia32_dbpsadbw128:
13089 case clang::X86::BI__builtin_ia32_dbpsadbw256:
13090 case clang::X86::BI__builtin_ia32_dbpsadbw512: {
13091 APValue SourceA, SourceB, SourceImm;
13092 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
13093 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
13094 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceImm))
13095 return false;
13096
13097 unsigned SourceLen = SourceA.getVectorLength();
13098 constexpr unsigned LaneSize = 16; // 128-bit lane = 16 bytes
13099 unsigned Imm = SourceImm.getInt().getZExtValue();
13100
13101 auto *DestTy = E->getType()->castAs<VectorType>();
13102 QualType DestEltTy = DestTy->getElementType();
13103 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13104 SmallVector<APValue, 32> ResultElements;
13105 ResultElements.reserve(N: SourceLen / 2);
13106
13107 // Phase 1: Shuffle SourceB using all four 2-bit fields of imm8.
13108 // Within each 128-bit lane, for group j (0..3), select a 4-byte block
13109 // from SourceB based on bits [2*j+1:2*j] of imm8.
13110 SmallVector<uint8_t, 64> Shuffled(SourceLen);
13111 for (unsigned I = 0; I < SourceLen; I += LaneSize) {
13112 for (unsigned J = 0; J < 4; ++J) {
13113 unsigned Part = (Imm >> (2 * J)) & 3;
13114 for (unsigned K = 0; K < 4; ++K) {
13115 Shuffled[I + 4 * J + K] = static_cast<uint8_t>(
13116 SourceB.getVectorElt(I: I + 4 * Part + K).getInt().getZExtValue());
13117 }
13118 }
13119 }
13120
13121 // Phase 2: Sliding SAD computation.
13122 // For every group of 4 output u16 values, compute absolute differences
13123 // using overlapping windows into SourceA and the shuffled array.
13124 unsigned Size = SourceLen / 2; // number of output u16 elements
13125 for (unsigned I = 0; I < Size; I += 4) {
13126 unsigned Sad[4] = {0, 0, 0, 0};
13127 for (unsigned J = 0; J < 4; ++J) {
13128 uint8_t A1 = static_cast<uint8_t>(
13129 SourceA.getVectorElt(I: 2 * I + J).getInt().getZExtValue());
13130 uint8_t A2 = static_cast<uint8_t>(
13131 SourceA.getVectorElt(I: 2 * I + J + 4).getInt().getZExtValue());
13132 uint8_t B0 = Shuffled[2 * I + J];
13133 uint8_t B1 = Shuffled[2 * I + J + 1];
13134 uint8_t B2 = Shuffled[2 * I + J + 2];
13135 uint8_t B3 = Shuffled[2 * I + J + 3];
13136 Sad[0] += (A1 > B0) ? (A1 - B0) : (B0 - A1);
13137 Sad[1] += (A1 > B1) ? (A1 - B1) : (B1 - A1);
13138 Sad[2] += (A2 > B2) ? (A2 - B2) : (B2 - A2);
13139 Sad[3] += (A2 > B3) ? (A2 - B3) : (B3 - A2);
13140 }
13141 for (unsigned R = 0; R < 4; ++R)
13142 ResultElements.push_back(
13143 Elt: APValue(APSInt(APInt(16, Sad[R]), DestUnsigned)));
13144 }
13145
13146 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13147 }
13148
13149 case clang::X86::BI__builtin_ia32_mpsadbw128:
13150 case clang::X86::BI__builtin_ia32_mpsadbw256: {
13151 APValue SourceA, SourceB;
13152 APSInt SourceImm;
13153 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: SourceA, Info) ||
13154 !EvaluateVector(E: E->getArg(Arg: 1), Result&: SourceB, Info) ||
13155 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: SourceImm, Info))
13156 return false;
13157 unsigned SourceLen = SourceA.getVectorLength();
13158 constexpr unsigned LaneSize = 16;
13159 assert((SourceLen == LaneSize || SourceLen == 2 * LaneSize) &&
13160 "MPSADBW operates on 128-bit or 256-bit vectors");
13161 unsigned NumLanes = SourceLen / LaneSize;
13162 unsigned Imm = SourceImm.getZExtValue();
13163
13164 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13165 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13166 SmallVector<APValue, 16> ResultElements;
13167 ResultElements.reserve(N: SourceLen / 2);
13168
13169 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
13170 unsigned Ctrl = (Imm >> (3 * Lane)) & 0x7;
13171 unsigned AOff = ((Ctrl >> 2) & 1) * 4;
13172 unsigned BOff = (Ctrl & 3) * 4;
13173 for (unsigned J = 0; J != 8; ++J) {
13174 uint16_t Sad = 0;
13175 for (unsigned K = 0; K != 4; ++K) {
13176 uint8_t A = static_cast<uint8_t>(
13177 SourceA.getVectorElt(I: Lane * LaneSize + AOff + J + K)
13178 .getInt()
13179 .getZExtValue());
13180 uint8_t B = static_cast<uint8_t>(
13181 SourceB.getVectorElt(I: Lane * LaneSize + BOff + K)
13182 .getInt()
13183 .getZExtValue());
13184 Sad += (A > B) ? (A - B) : (B - A);
13185 }
13186 ResultElements.push_back(Elt: APValue(APSInt(APInt(16, Sad), DestUnsigned)));
13187 }
13188 }
13189 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13190 }
13191
13192 case clang::X86::BI__builtin_ia32_pmulhuw128:
13193 case clang::X86::BI__builtin_ia32_pmulhuw256:
13194 case clang::X86::BI__builtin_ia32_pmulhuw512:
13195 return EvaluateBinOpExpr(llvm::APIntOps::mulhu);
13196
13197 case clang::X86::BI__builtin_ia32_pmulhw128:
13198 case clang::X86::BI__builtin_ia32_pmulhw256:
13199 case clang::X86::BI__builtin_ia32_pmulhw512:
13200 return EvaluateBinOpExpr(llvm::APIntOps::mulhs);
13201
13202 case clang::X86::BI__builtin_ia32_psllv2di:
13203 case clang::X86::BI__builtin_ia32_psllv4di:
13204 case clang::X86::BI__builtin_ia32_psllv4si:
13205 case clang::X86::BI__builtin_ia32_psllv8di:
13206 case clang::X86::BI__builtin_ia32_psllv8hi:
13207 case clang::X86::BI__builtin_ia32_psllv8si:
13208 case clang::X86::BI__builtin_ia32_psllv16hi:
13209 case clang::X86::BI__builtin_ia32_psllv16si:
13210 case clang::X86::BI__builtin_ia32_psllv32hi:
13211 case clang::X86::BI__builtin_ia32_psllwi128:
13212 case clang::X86::BI__builtin_ia32_pslldi128:
13213 case clang::X86::BI__builtin_ia32_psllqi128:
13214 case clang::X86::BI__builtin_ia32_psllwi256:
13215 case clang::X86::BI__builtin_ia32_pslldi256:
13216 case clang::X86::BI__builtin_ia32_psllqi256:
13217 case clang::X86::BI__builtin_ia32_psllwi512:
13218 case clang::X86::BI__builtin_ia32_pslldi512:
13219 case clang::X86::BI__builtin_ia32_psllqi512:
13220 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13221 if (RHS.uge(RHS: LHS.getBitWidth())) {
13222 return APInt::getZero(numBits: LHS.getBitWidth());
13223 }
13224 return LHS.shl(shiftAmt: RHS.getZExtValue());
13225 });
13226
13227 case clang::X86::BI__builtin_ia32_psrav4si:
13228 case clang::X86::BI__builtin_ia32_psrav8di:
13229 case clang::X86::BI__builtin_ia32_psrav8hi:
13230 case clang::X86::BI__builtin_ia32_psrav8si:
13231 case clang::X86::BI__builtin_ia32_psrav16hi:
13232 case clang::X86::BI__builtin_ia32_psrav16si:
13233 case clang::X86::BI__builtin_ia32_psrav32hi:
13234 case clang::X86::BI__builtin_ia32_psravq128:
13235 case clang::X86::BI__builtin_ia32_psravq256:
13236 case clang::X86::BI__builtin_ia32_psrawi128:
13237 case clang::X86::BI__builtin_ia32_psradi128:
13238 case clang::X86::BI__builtin_ia32_psraqi128:
13239 case clang::X86::BI__builtin_ia32_psrawi256:
13240 case clang::X86::BI__builtin_ia32_psradi256:
13241 case clang::X86::BI__builtin_ia32_psraqi256:
13242 case clang::X86::BI__builtin_ia32_psrawi512:
13243 case clang::X86::BI__builtin_ia32_psradi512:
13244 case clang::X86::BI__builtin_ia32_psraqi512:
13245 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13246 if (RHS.uge(RHS: LHS.getBitWidth())) {
13247 return LHS.ashr(ShiftAmt: LHS.getBitWidth() - 1);
13248 }
13249 return LHS.ashr(ShiftAmt: RHS.getZExtValue());
13250 });
13251
13252 case clang::X86::BI__builtin_ia32_psrlv2di:
13253 case clang::X86::BI__builtin_ia32_psrlv4di:
13254 case clang::X86::BI__builtin_ia32_psrlv4si:
13255 case clang::X86::BI__builtin_ia32_psrlv8di:
13256 case clang::X86::BI__builtin_ia32_psrlv8hi:
13257 case clang::X86::BI__builtin_ia32_psrlv8si:
13258 case clang::X86::BI__builtin_ia32_psrlv16hi:
13259 case clang::X86::BI__builtin_ia32_psrlv16si:
13260 case clang::X86::BI__builtin_ia32_psrlv32hi:
13261 case clang::X86::BI__builtin_ia32_psrlwi128:
13262 case clang::X86::BI__builtin_ia32_psrldi128:
13263 case clang::X86::BI__builtin_ia32_psrlqi128:
13264 case clang::X86::BI__builtin_ia32_psrlwi256:
13265 case clang::X86::BI__builtin_ia32_psrldi256:
13266 case clang::X86::BI__builtin_ia32_psrlqi256:
13267 case clang::X86::BI__builtin_ia32_psrlwi512:
13268 case clang::X86::BI__builtin_ia32_psrldi512:
13269 case clang::X86::BI__builtin_ia32_psrlqi512:
13270 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
13271 if (RHS.uge(RHS: LHS.getBitWidth())) {
13272 return APInt::getZero(numBits: LHS.getBitWidth());
13273 }
13274 return LHS.lshr(shiftAmt: RHS.getZExtValue());
13275 });
13276 case X86::BI__builtin_ia32_packsswb128:
13277 case X86::BI__builtin_ia32_packsswb256:
13278 case X86::BI__builtin_ia32_packsswb512:
13279 case X86::BI__builtin_ia32_packssdw128:
13280 case X86::BI__builtin_ia32_packssdw256:
13281 case X86::BI__builtin_ia32_packssdw512:
13282 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13283 return APSInt(Src).truncSSat(width: Src.getBitWidth() / 2);
13284 });
13285 case X86::BI__builtin_ia32_packusdw128:
13286 case X86::BI__builtin_ia32_packusdw256:
13287 case X86::BI__builtin_ia32_packusdw512:
13288 case X86::BI__builtin_ia32_packuswb128:
13289 case X86::BI__builtin_ia32_packuswb256:
13290 case X86::BI__builtin_ia32_packuswb512:
13291 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
13292 return APSInt(Src).truncSSatU(width: Src.getBitWidth() / 2);
13293 });
13294 case clang::X86::BI__builtin_ia32_selectss_128:
13295 return EvalSelectScalar(4);
13296 case clang::X86::BI__builtin_ia32_selectsd_128:
13297 return EvalSelectScalar(2);
13298 case clang::X86::BI__builtin_ia32_selectsh_128:
13299 case clang::X86::BI__builtin_ia32_selectsbf_128:
13300 return EvalSelectScalar(8);
13301 case clang::X86::BI__builtin_ia32_pmuldq128:
13302 case clang::X86::BI__builtin_ia32_pmuldq256:
13303 case clang::X86::BI__builtin_ia32_pmuldq512:
13304 case clang::X86::BI__builtin_ia32_pmuludq128:
13305 case clang::X86::BI__builtin_ia32_pmuludq256:
13306 case clang::X86::BI__builtin_ia32_pmuludq512: {
13307 APValue SourceLHS, SourceRHS;
13308 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13309 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13310 return false;
13311
13312 unsigned SourceLen = SourceLHS.getVectorLength();
13313 SmallVector<APValue, 4> ResultElements;
13314 ResultElements.reserve(N: SourceLen / 2);
13315
13316 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
13317 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13318 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13319
13320 switch (BuiltinOp) {
13321 case clang::X86::BI__builtin_ia32_pmuludq128:
13322 case clang::X86::BI__builtin_ia32_pmuludq256:
13323 case clang::X86::BI__builtin_ia32_pmuludq512:
13324 ResultElements.push_back(
13325 Elt: APValue(APSInt(llvm::APIntOps::muluExtended(C1: LHS, C2: RHS), true)));
13326 break;
13327 case clang::X86::BI__builtin_ia32_pmuldq128:
13328 case clang::X86::BI__builtin_ia32_pmuldq256:
13329 case clang::X86::BI__builtin_ia32_pmuldq512:
13330 ResultElements.push_back(
13331 Elt: APValue(APSInt(llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS), false)));
13332 break;
13333 }
13334 }
13335
13336 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13337 }
13338
13339 case X86::BI__builtin_ia32_vpmadd52luq128:
13340 case X86::BI__builtin_ia32_vpmadd52luq256:
13341 case X86::BI__builtin_ia32_vpmadd52luq512: {
13342 APValue A, B, C;
13343 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13344 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13345 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13346 return false;
13347
13348 unsigned ALen = A.getVectorLength();
13349 SmallVector<APValue, 4> ResultElements;
13350 ResultElements.reserve(N: ALen);
13351
13352 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13353 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13354 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13355 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13356 APSInt ResElt(AElt + (BElt * CElt).zext(width: 64), false);
13357 ResultElements.push_back(Elt: APValue(ResElt));
13358 }
13359
13360 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13361 }
13362 case X86::BI__builtin_ia32_vpmadd52huq128:
13363 case X86::BI__builtin_ia32_vpmadd52huq256:
13364 case X86::BI__builtin_ia32_vpmadd52huq512: {
13365 APValue A, B, C;
13366 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
13367 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
13368 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
13369 return false;
13370
13371 unsigned ALen = A.getVectorLength();
13372 SmallVector<APValue, 4> ResultElements;
13373 ResultElements.reserve(N: ALen);
13374
13375 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
13376 APInt AElt = A.getVectorElt(I: EltNum).getInt();
13377 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13378 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
13379 APSInt ResElt(AElt + llvm::APIntOps::mulhu(C1: BElt, C2: CElt).zext(width: 64), false);
13380 ResultElements.push_back(Elt: APValue(ResElt));
13381 }
13382
13383 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13384 }
13385
13386 case clang::X86::BI__builtin_ia32_vprotbi:
13387 case clang::X86::BI__builtin_ia32_vprotdi:
13388 case clang::X86::BI__builtin_ia32_vprotqi:
13389 case clang::X86::BI__builtin_ia32_vprotwi:
13390 case clang::X86::BI__builtin_ia32_prold128:
13391 case clang::X86::BI__builtin_ia32_prold256:
13392 case clang::X86::BI__builtin_ia32_prold512:
13393 case clang::X86::BI__builtin_ia32_prolq128:
13394 case clang::X86::BI__builtin_ia32_prolq256:
13395 case clang::X86::BI__builtin_ia32_prolq512:
13396 return EvaluateBinOpExpr(
13397 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotl(rotateAmt: RHS); });
13398
13399 case clang::X86::BI__builtin_ia32_prord128:
13400 case clang::X86::BI__builtin_ia32_prord256:
13401 case clang::X86::BI__builtin_ia32_prord512:
13402 case clang::X86::BI__builtin_ia32_prorq128:
13403 case clang::X86::BI__builtin_ia32_prorq256:
13404 case clang::X86::BI__builtin_ia32_prorq512:
13405 return EvaluateBinOpExpr(
13406 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotr(rotateAmt: RHS); });
13407
13408 case Builtin::BI__builtin_elementwise_max:
13409 case Builtin::BI__builtin_elementwise_min: {
13410 APValue SourceLHS, SourceRHS;
13411 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13412 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13413 return false;
13414
13415 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13416
13417 if (!DestEltTy->isIntegerType())
13418 return false;
13419
13420 unsigned SourceLen = SourceLHS.getVectorLength();
13421 SmallVector<APValue, 4> ResultElements;
13422 ResultElements.reserve(N: SourceLen);
13423
13424 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13425 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13426 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13427 switch (BuiltinOp) {
13428 case Builtin::BI__builtin_elementwise_max:
13429 ResultElements.push_back(
13430 Elt: APValue(APSInt(std::max(a: LHS, b: RHS),
13431 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13432 break;
13433 case Builtin::BI__builtin_elementwise_min:
13434 ResultElements.push_back(
13435 Elt: APValue(APSInt(std::min(a: LHS, b: RHS),
13436 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13437 break;
13438 }
13439 }
13440
13441 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13442 }
13443 case X86::BI__builtin_ia32_vpshldd128:
13444 case X86::BI__builtin_ia32_vpshldd256:
13445 case X86::BI__builtin_ia32_vpshldd512:
13446 case X86::BI__builtin_ia32_vpshldq128:
13447 case X86::BI__builtin_ia32_vpshldq256:
13448 case X86::BI__builtin_ia32_vpshldq512:
13449 case X86::BI__builtin_ia32_vpshldw128:
13450 case X86::BI__builtin_ia32_vpshldw256:
13451 case X86::BI__builtin_ia32_vpshldw512: {
13452 APValue SourceHi, SourceLo, SourceAmt;
13453 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
13454 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
13455 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13456 return false;
13457
13458 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13459 unsigned SourceLen = SourceHi.getVectorLength();
13460 SmallVector<APValue, 32> ResultElements;
13461 ResultElements.reserve(N: SourceLen);
13462
13463 APInt Amt = SourceAmt.getInt();
13464 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13465 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13466 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13467 APInt R = llvm::APIntOps::fshl(Hi, Lo, Shift: Amt);
13468 ResultElements.push_back(
13469 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13470 }
13471
13472 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13473 }
13474 case X86::BI__builtin_ia32_vpshrdd128:
13475 case X86::BI__builtin_ia32_vpshrdd256:
13476 case X86::BI__builtin_ia32_vpshrdd512:
13477 case X86::BI__builtin_ia32_vpshrdq128:
13478 case X86::BI__builtin_ia32_vpshrdq256:
13479 case X86::BI__builtin_ia32_vpshrdq512:
13480 case X86::BI__builtin_ia32_vpshrdw128:
13481 case X86::BI__builtin_ia32_vpshrdw256:
13482 case X86::BI__builtin_ia32_vpshrdw512: {
13483 // NOTE: Reversed Hi/Lo operands.
13484 APValue SourceHi, SourceLo, SourceAmt;
13485 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLo) ||
13486 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceHi) ||
13487 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13488 return false;
13489
13490 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13491 unsigned SourceLen = SourceHi.getVectorLength();
13492 SmallVector<APValue, 32> ResultElements;
13493 ResultElements.reserve(N: SourceLen);
13494
13495 APInt Amt = SourceAmt.getInt();
13496 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13497 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13498 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13499 APInt R = llvm::APIntOps::fshr(Hi, Lo, Shift: Amt);
13500 ResultElements.push_back(
13501 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13502 }
13503
13504 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13505 }
13506 case X86::BI__builtin_ia32_compressdf128_mask:
13507 case X86::BI__builtin_ia32_compressdf256_mask:
13508 case X86::BI__builtin_ia32_compressdf512_mask:
13509 case X86::BI__builtin_ia32_compressdi128_mask:
13510 case X86::BI__builtin_ia32_compressdi256_mask:
13511 case X86::BI__builtin_ia32_compressdi512_mask:
13512 case X86::BI__builtin_ia32_compresshi128_mask:
13513 case X86::BI__builtin_ia32_compresshi256_mask:
13514 case X86::BI__builtin_ia32_compresshi512_mask:
13515 case X86::BI__builtin_ia32_compressqi128_mask:
13516 case X86::BI__builtin_ia32_compressqi256_mask:
13517 case X86::BI__builtin_ia32_compressqi512_mask:
13518 case X86::BI__builtin_ia32_compresssf128_mask:
13519 case X86::BI__builtin_ia32_compresssf256_mask:
13520 case X86::BI__builtin_ia32_compresssf512_mask:
13521 case X86::BI__builtin_ia32_compresssi128_mask:
13522 case X86::BI__builtin_ia32_compresssi256_mask:
13523 case X86::BI__builtin_ia32_compresssi512_mask: {
13524 APValue Source, Passthru;
13525 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13526 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13527 return false;
13528 APSInt Mask;
13529 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13530 return false;
13531
13532 unsigned NumElts = Source.getVectorLength();
13533 SmallVector<APValue, 64> ResultElements;
13534 ResultElements.reserve(N: NumElts);
13535
13536 for (unsigned I = 0; I != NumElts; ++I) {
13537 if (Mask[I])
13538 ResultElements.push_back(Elt: Source.getVectorElt(I));
13539 }
13540 for (unsigned I = ResultElements.size(); I != NumElts; ++I) {
13541 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13542 }
13543
13544 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13545 }
13546 case X86::BI__builtin_ia32_expanddf128_mask:
13547 case X86::BI__builtin_ia32_expanddf256_mask:
13548 case X86::BI__builtin_ia32_expanddf512_mask:
13549 case X86::BI__builtin_ia32_expanddi128_mask:
13550 case X86::BI__builtin_ia32_expanddi256_mask:
13551 case X86::BI__builtin_ia32_expanddi512_mask:
13552 case X86::BI__builtin_ia32_expandhi128_mask:
13553 case X86::BI__builtin_ia32_expandhi256_mask:
13554 case X86::BI__builtin_ia32_expandhi512_mask:
13555 case X86::BI__builtin_ia32_expandqi128_mask:
13556 case X86::BI__builtin_ia32_expandqi256_mask:
13557 case X86::BI__builtin_ia32_expandqi512_mask:
13558 case X86::BI__builtin_ia32_expandsf128_mask:
13559 case X86::BI__builtin_ia32_expandsf256_mask:
13560 case X86::BI__builtin_ia32_expandsf512_mask:
13561 case X86::BI__builtin_ia32_expandsi128_mask:
13562 case X86::BI__builtin_ia32_expandsi256_mask:
13563 case X86::BI__builtin_ia32_expandsi512_mask: {
13564 APValue Source, Passthru;
13565 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13566 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13567 return false;
13568 APSInt Mask;
13569 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13570 return false;
13571
13572 unsigned NumElts = Source.getVectorLength();
13573 SmallVector<APValue, 64> ResultElements;
13574 ResultElements.reserve(N: NumElts);
13575
13576 unsigned SourceIdx = 0;
13577 for (unsigned I = 0; I != NumElts; ++I) {
13578 if (Mask[I])
13579 ResultElements.push_back(Elt: Source.getVectorElt(I: SourceIdx++));
13580 else
13581 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13582 }
13583 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13584 }
13585 case X86::BI__builtin_ia32_vpconflictsi_128:
13586 case X86::BI__builtin_ia32_vpconflictsi_256:
13587 case X86::BI__builtin_ia32_vpconflictsi_512:
13588 case X86::BI__builtin_ia32_vpconflictdi_128:
13589 case X86::BI__builtin_ia32_vpconflictdi_256:
13590 case X86::BI__builtin_ia32_vpconflictdi_512: {
13591 APValue Source;
13592
13593 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
13594 return false;
13595
13596 unsigned SourceLen = Source.getVectorLength();
13597 SmallVector<APValue, 32> ResultElements;
13598 ResultElements.reserve(N: SourceLen);
13599
13600 const auto *VecT = E->getType()->castAs<VectorType>();
13601 bool DestUnsigned =
13602 VecT->getElementType()->isUnsignedIntegerOrEnumerationType();
13603
13604 for (unsigned I = 0; I != SourceLen; ++I) {
13605 const APValue &EltI = Source.getVectorElt(I);
13606
13607 APInt ConflictMask(EltI.getInt().getBitWidth(), 0);
13608 for (unsigned J = 0; J != I; ++J) {
13609 const APValue &EltJ = Source.getVectorElt(I: J);
13610 ConflictMask.setBitVal(BitPosition: J, BitValue: EltI.getInt() == EltJ.getInt());
13611 }
13612 ResultElements.push_back(Elt: APValue(APSInt(ConflictMask, DestUnsigned)));
13613 }
13614 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13615 }
13616 case X86::BI__builtin_ia32_blendpd:
13617 case X86::BI__builtin_ia32_blendpd256:
13618 case X86::BI__builtin_ia32_blendps:
13619 case X86::BI__builtin_ia32_blendps256:
13620 case X86::BI__builtin_ia32_pblendw128:
13621 case X86::BI__builtin_ia32_pblendw256:
13622 case X86::BI__builtin_ia32_pblendd128:
13623 case X86::BI__builtin_ia32_pblendd256: {
13624 APValue SourceF, SourceT, SourceC;
13625 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13626 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13627 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13628 return false;
13629
13630 const APInt &C = SourceC.getInt();
13631 unsigned SourceLen = SourceF.getVectorLength();
13632 SmallVector<APValue, 32> ResultElements;
13633 ResultElements.reserve(N: SourceLen);
13634 for (unsigned EltNum = 0; EltNum != SourceLen; ++EltNum) {
13635 const APValue &F = SourceF.getVectorElt(I: EltNum);
13636 const APValue &T = SourceT.getVectorElt(I: EltNum);
13637 ResultElements.push_back(Elt: C[EltNum % 8] ? T : F);
13638 }
13639
13640 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13641 }
13642
13643 case X86::BI__builtin_ia32_psignb128:
13644 case X86::BI__builtin_ia32_psignb256:
13645 case X86::BI__builtin_ia32_psignw128:
13646 case X86::BI__builtin_ia32_psignw256:
13647 case X86::BI__builtin_ia32_psignd128:
13648 case X86::BI__builtin_ia32_psignd256:
13649 return EvaluateBinOpExpr([](const APInt &AElem, const APInt &BElem) {
13650 if (BElem.isZero())
13651 return APInt::getZero(numBits: AElem.getBitWidth());
13652 if (BElem.isNegative())
13653 return -AElem;
13654 return AElem;
13655 });
13656
13657 case X86::BI__builtin_ia32_blendvpd:
13658 case X86::BI__builtin_ia32_blendvpd256:
13659 case X86::BI__builtin_ia32_blendvps:
13660 case X86::BI__builtin_ia32_blendvps256:
13661 case X86::BI__builtin_ia32_pblendvb128:
13662 case X86::BI__builtin_ia32_pblendvb256: {
13663 // SSE blendv by mask signbit: "Result = C[] < 0 ? T[] : F[]".
13664 APValue SourceF, SourceT, SourceC;
13665 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13666 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13667 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13668 return false;
13669
13670 unsigned SourceLen = SourceF.getVectorLength();
13671 SmallVector<APValue, 32> ResultElements;
13672 ResultElements.reserve(N: SourceLen);
13673
13674 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13675 const APValue &F = SourceF.getVectorElt(I: EltNum);
13676 const APValue &T = SourceT.getVectorElt(I: EltNum);
13677 const APValue &C = SourceC.getVectorElt(I: EltNum);
13678 APInt M = C.isInt() ? (APInt)C.getInt() : C.getFloat().bitcastToAPInt();
13679 ResultElements.push_back(Elt: M.isNegative() ? T : F);
13680 }
13681
13682 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13683 }
13684 case X86::BI__builtin_ia32_selectb_128:
13685 case X86::BI__builtin_ia32_selectb_256:
13686 case X86::BI__builtin_ia32_selectb_512:
13687 case X86::BI__builtin_ia32_selectw_128:
13688 case X86::BI__builtin_ia32_selectw_256:
13689 case X86::BI__builtin_ia32_selectw_512:
13690 case X86::BI__builtin_ia32_selectd_128:
13691 case X86::BI__builtin_ia32_selectd_256:
13692 case X86::BI__builtin_ia32_selectd_512:
13693 case X86::BI__builtin_ia32_selectq_128:
13694 case X86::BI__builtin_ia32_selectq_256:
13695 case X86::BI__builtin_ia32_selectq_512:
13696 case X86::BI__builtin_ia32_selectph_128:
13697 case X86::BI__builtin_ia32_selectph_256:
13698 case X86::BI__builtin_ia32_selectph_512:
13699 case X86::BI__builtin_ia32_selectpbf_128:
13700 case X86::BI__builtin_ia32_selectpbf_256:
13701 case X86::BI__builtin_ia32_selectpbf_512:
13702 case X86::BI__builtin_ia32_selectps_128:
13703 case X86::BI__builtin_ia32_selectps_256:
13704 case X86::BI__builtin_ia32_selectps_512:
13705 case X86::BI__builtin_ia32_selectpd_128:
13706 case X86::BI__builtin_ia32_selectpd_256:
13707 case X86::BI__builtin_ia32_selectpd_512: {
13708 // AVX512 predicated move: "Result = Mask[] ? LHS[] : RHS[]".
13709 APValue SourceMask, SourceLHS, SourceRHS;
13710 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceMask) ||
13711 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLHS) ||
13712 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceRHS))
13713 return false;
13714
13715 APSInt Mask = SourceMask.getInt();
13716 unsigned SourceLen = SourceLHS.getVectorLength();
13717 SmallVector<APValue, 4> ResultElements;
13718 ResultElements.reserve(N: SourceLen);
13719
13720 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13721 const APValue &LHS = SourceLHS.getVectorElt(I: EltNum);
13722 const APValue &RHS = SourceRHS.getVectorElt(I: EltNum);
13723 ResultElements.push_back(Elt: Mask[EltNum] ? LHS : RHS);
13724 }
13725
13726 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13727 }
13728
13729 case X86::BI__builtin_ia32_cvtsd2ss: {
13730 APValue VecA, VecB;
13731 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13732 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB))
13733 return false;
13734
13735 SmallVector<APValue, 4> Elements;
13736
13737 APValue ResultVal;
13738 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13739 Result&: ResultVal))
13740 return false;
13741
13742 Elements.push_back(Elt: ResultVal);
13743
13744 unsigned NumEltsA = VecA.getVectorLength();
13745 for (unsigned I = 1; I < NumEltsA; ++I) {
13746 Elements.push_back(Elt: VecA.getVectorElt(I));
13747 }
13748
13749 return Success(V: Elements, E);
13750 }
13751 case X86::BI__builtin_ia32_cvtsd2ss_round_mask: {
13752 APValue VecA, VecB, VecSrc, MaskValue;
13753
13754 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13755 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
13756 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
13757 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: MaskValue))
13758 return false;
13759
13760 unsigned Mask = MaskValue.getInt().getZExtValue();
13761 SmallVector<APValue, 4> Elements;
13762
13763 if (Mask & 1) {
13764 APValue ResultVal;
13765 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13766 Result&: ResultVal))
13767 return false;
13768 Elements.push_back(Elt: ResultVal);
13769 } else {
13770 Elements.push_back(Elt: VecSrc.getVectorElt(I: 0));
13771 }
13772
13773 unsigned NumEltsA = VecA.getVectorLength();
13774 for (unsigned I = 1; I < NumEltsA; ++I) {
13775 Elements.push_back(Elt: VecA.getVectorElt(I));
13776 }
13777
13778 return Success(V: Elements, E);
13779 }
13780 case X86::BI__builtin_ia32_cvtpd2ps:
13781 case X86::BI__builtin_ia32_cvtpd2ps256:
13782 case X86::BI__builtin_ia32_cvtpd2ps_mask:
13783 case X86::BI__builtin_ia32_cvtpd2ps512_mask: {
13784
13785 const auto BuiltinID = BuiltinOp;
13786 bool IsMasked = (BuiltinID == X86::BI__builtin_ia32_cvtpd2ps_mask ||
13787 BuiltinID == X86::BI__builtin_ia32_cvtpd2ps512_mask);
13788
13789 APValue InputValue;
13790 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: InputValue))
13791 return false;
13792
13793 APValue MergeValue;
13794 unsigned Mask = 0xFFFFFFFF;
13795 bool NeedsMerge = false;
13796 if (IsMasked) {
13797 APValue MaskValue;
13798 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MaskValue))
13799 return false;
13800 Mask = MaskValue.getInt().getZExtValue();
13801 auto NumEltsResult = E->getType()->getAs<VectorType>()->getNumElements();
13802 for (unsigned I = 0; I < NumEltsResult; ++I) {
13803 if (!((Mask >> I) & 1)) {
13804 NeedsMerge = true;
13805 break;
13806 }
13807 }
13808 if (NeedsMerge) {
13809 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: MergeValue))
13810 return false;
13811 }
13812 }
13813
13814 unsigned NumEltsResult =
13815 E->getType()->getAs<VectorType>()->getNumElements();
13816 unsigned NumEltsInput = InputValue.getVectorLength();
13817 SmallVector<APValue, 8> Elements;
13818 for (unsigned I = 0; I < NumEltsResult; ++I) {
13819 if (IsMasked && !((Mask >> I) & 1)) {
13820 if (!NeedsMerge) {
13821 return false;
13822 }
13823 Elements.push_back(Elt: MergeValue.getVectorElt(I));
13824 continue;
13825 }
13826
13827 if (I >= NumEltsInput) {
13828 Elements.push_back(Elt: APValue(APFloat::getZero(Sem: APFloat::IEEEsingle())));
13829 continue;
13830 }
13831
13832 APValue ResultVal;
13833 if (!ConvertDoubleToFloatStrict(
13834 Info, E, OrigVal: InputValue.getVectorElt(I).getFloat(), Result&: ResultVal))
13835 return false;
13836
13837 Elements.push_back(Elt: ResultVal);
13838 }
13839 return Success(V: Elements, E);
13840 }
13841
13842 case X86::BI__builtin_ia32_shufps:
13843 case X86::BI__builtin_ia32_shufps256:
13844 case X86::BI__builtin_ia32_shufps512: {
13845 APValue R;
13846 if (!evalShuffleGeneric(
13847 Info, Call: E, Out&: R,
13848 GetSourceIndex: [](unsigned DstIdx,
13849 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13850 constexpr unsigned LaneBits = 128u;
13851 unsigned NumElemPerLane = LaneBits / 32;
13852 unsigned NumSelectableElems = NumElemPerLane / 2;
13853 unsigned BitsPerElem = 2;
13854 unsigned IndexMask = (1u << BitsPerElem) - 1;
13855 unsigned MaskBits = 8;
13856 unsigned Lane = DstIdx / NumElemPerLane;
13857 unsigned ElemInLane = DstIdx % NumElemPerLane;
13858 unsigned LaneOffset = Lane * NumElemPerLane;
13859 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13860 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
13861 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
13862 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
13863 }))
13864 return false;
13865 return Success(V: R, E);
13866 }
13867 case X86::BI__builtin_ia32_shufpd:
13868 case X86::BI__builtin_ia32_shufpd256:
13869 case X86::BI__builtin_ia32_shufpd512: {
13870 APValue R;
13871 if (!evalShuffleGeneric(
13872 Info, Call: E, Out&: R,
13873 GetSourceIndex: [](unsigned DstIdx,
13874 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13875 constexpr unsigned LaneBits = 128u;
13876 unsigned NumElemPerLane = LaneBits / 64;
13877 unsigned NumSelectableElems = NumElemPerLane / 2;
13878 unsigned BitsPerElem = 1;
13879 unsigned IndexMask = (1u << BitsPerElem) - 1;
13880 unsigned MaskBits = 8;
13881 unsigned Lane = DstIdx / NumElemPerLane;
13882 unsigned ElemInLane = DstIdx % NumElemPerLane;
13883 unsigned LaneOffset = Lane * NumElemPerLane;
13884 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13885 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
13886 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
13887 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
13888 }))
13889 return false;
13890 return Success(V: R, E);
13891 }
13892 case X86::BI__builtin_ia32_insertps128: {
13893 APValue R;
13894 if (!evalShuffleGeneric(
13895 Info, Call: E, Out&: R,
13896 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13897 // Bits [3:0]: zero mask - if bit is set, zero this element
13898 if ((Mask & (1 << DstIdx)) != 0) {
13899 return {0, -1};
13900 }
13901 // Bits [7:6]: select element from source vector Y (0-3)
13902 // Bits [5:4]: select destination position (0-3)
13903 unsigned SrcElem = (Mask >> 6) & 0x3;
13904 unsigned DstElem = (Mask >> 4) & 0x3;
13905 if (DstIdx == DstElem) {
13906 // Insert element from source vector (B) at this position
13907 return {1, static_cast<int>(SrcElem)};
13908 } else {
13909 // Copy from destination vector (A)
13910 return {0, static_cast<int>(DstIdx)};
13911 }
13912 }))
13913 return false;
13914 return Success(V: R, E);
13915 }
13916 case X86::BI__builtin_ia32_pshufb128:
13917 case X86::BI__builtin_ia32_pshufb256:
13918 case X86::BI__builtin_ia32_pshufb512: {
13919 APValue R;
13920 if (!evalShuffleGeneric(
13921 Info, Call: E, Out&: R,
13922 GetSourceIndex: [](unsigned DstIdx,
13923 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13924 uint8_t Ctlb = static_cast<uint8_t>(ShuffleMask);
13925 if (Ctlb & 0x80)
13926 return std::make_pair(x: 0, y: -1);
13927
13928 unsigned LaneBase = (DstIdx / 16) * 16;
13929 unsigned SrcOffset = Ctlb & 0x0F;
13930 unsigned SrcIdx = LaneBase + SrcOffset;
13931 return std::make_pair(x: 0, y: static_cast<int>(SrcIdx));
13932 }))
13933 return false;
13934 return Success(V: R, E);
13935 }
13936
13937 case X86::BI__builtin_ia32_pshuflw:
13938 case X86::BI__builtin_ia32_pshuflw256:
13939 case X86::BI__builtin_ia32_pshuflw512: {
13940 APValue R;
13941 if (!evalShuffleGeneric(
13942 Info, Call: E, Out&: R,
13943 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13944 constexpr unsigned LaneBits = 128u;
13945 constexpr unsigned ElemBits = 16u;
13946 constexpr unsigned LaneElts = LaneBits / ElemBits;
13947 constexpr unsigned HalfSize = 4;
13948 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13949 unsigned LaneIdx = DstIdx % LaneElts;
13950 if (LaneIdx < HalfSize) {
13951 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
13952 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
13953 }
13954 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
13955 }))
13956 return false;
13957 return Success(V: R, E);
13958 }
13959
13960 case X86::BI__builtin_ia32_pshufhw:
13961 case X86::BI__builtin_ia32_pshufhw256:
13962 case X86::BI__builtin_ia32_pshufhw512: {
13963 APValue R;
13964 if (!evalShuffleGeneric(
13965 Info, Call: E, Out&: R,
13966 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13967 constexpr unsigned LaneBits = 128u;
13968 constexpr unsigned ElemBits = 16u;
13969 constexpr unsigned LaneElts = LaneBits / ElemBits;
13970 constexpr unsigned HalfSize = 4;
13971 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13972 unsigned LaneIdx = DstIdx % LaneElts;
13973 if (LaneIdx >= HalfSize) {
13974 unsigned Rel = LaneIdx - HalfSize;
13975 unsigned Sel = (Mask >> (2 * Rel)) & 0x3;
13976 return std::make_pair(
13977 x: 0, y: static_cast<int>(LaneBase + HalfSize + Sel));
13978 }
13979 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
13980 }))
13981 return false;
13982 return Success(V: R, E);
13983 }
13984
13985 case X86::BI__builtin_ia32_pshufd:
13986 case X86::BI__builtin_ia32_pshufd256:
13987 case X86::BI__builtin_ia32_pshufd512:
13988 case X86::BI__builtin_ia32_vpermilps:
13989 case X86::BI__builtin_ia32_vpermilps256:
13990 case X86::BI__builtin_ia32_vpermilps512: {
13991 APValue R;
13992 if (!evalShuffleGeneric(
13993 Info, Call: E, Out&: R,
13994 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13995 constexpr unsigned LaneBits = 128u;
13996 constexpr unsigned ElemBits = 32u;
13997 constexpr unsigned LaneElts = LaneBits / ElemBits;
13998 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13999 unsigned LaneIdx = DstIdx % LaneElts;
14000 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
14001 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
14002 }))
14003 return false;
14004 return Success(V: R, E);
14005 }
14006
14007 case X86::BI__builtin_ia32_vpermilvarpd:
14008 case X86::BI__builtin_ia32_vpermilvarpd256:
14009 case X86::BI__builtin_ia32_vpermilvarpd512: {
14010 APValue R;
14011 if (!evalShuffleGeneric(
14012 Info, Call: E, Out&: R,
14013 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14014 unsigned NumElemPerLane = 2;
14015 unsigned Lane = DstIdx / NumElemPerLane;
14016 unsigned Offset = Mask & 0b10 ? 1 : 0;
14017 return std::make_pair(
14018 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14019 }))
14020 return false;
14021 return Success(V: R, E);
14022 }
14023
14024 case X86::BI__builtin_ia32_vpermilpd:
14025 case X86::BI__builtin_ia32_vpermilpd256:
14026 case X86::BI__builtin_ia32_vpermilpd512: {
14027 APValue R;
14028 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14029 unsigned NumElemPerLane = 2;
14030 unsigned BitsPerElem = 1;
14031 unsigned MaskBits = 8;
14032 unsigned IndexMask = 0x1;
14033 unsigned Lane = DstIdx / NumElemPerLane;
14034 unsigned LaneOffset = Lane * NumElemPerLane;
14035 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
14036 unsigned Index = (Control >> BitIndex) & IndexMask;
14037 return std::make_pair(x: 0, y: static_cast<int>(LaneOffset + Index));
14038 }))
14039 return false;
14040 return Success(V: R, E);
14041 }
14042
14043 case X86::BI__builtin_ia32_permdf256:
14044 case X86::BI__builtin_ia32_permdi256: {
14045 APValue R;
14046 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
14047 // permute4x64 operates on 4 64-bit elements
14048 // For element i (0-3), extract bits [2*i+1:2*i] from Control
14049 unsigned Index = (Control >> (2 * DstIdx)) & 0x3;
14050 return std::make_pair(x: 0, y: static_cast<int>(Index));
14051 }))
14052 return false;
14053 return Success(V: R, E);
14054 }
14055
14056 case X86::BI__builtin_ia32_vpermilvarps:
14057 case X86::BI__builtin_ia32_vpermilvarps256:
14058 case X86::BI__builtin_ia32_vpermilvarps512: {
14059 APValue R;
14060 if (!evalShuffleGeneric(
14061 Info, Call: E, Out&: R,
14062 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
14063 unsigned NumElemPerLane = 4;
14064 unsigned Lane = DstIdx / NumElemPerLane;
14065 unsigned Offset = Mask & 0b11;
14066 return std::make_pair(
14067 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
14068 }))
14069 return false;
14070 return Success(V: R, E);
14071 }
14072
14073 case X86::BI__builtin_ia32_vpmultishiftqb128:
14074 case X86::BI__builtin_ia32_vpmultishiftqb256:
14075 case X86::BI__builtin_ia32_vpmultishiftqb512: {
14076 assert(E->getNumArgs() == 2);
14077
14078 APValue A, B;
14079 if (!Evaluate(Result&: A, Info, E: E->getArg(Arg: 0)) || !Evaluate(Result&: B, Info, E: E->getArg(Arg: 1)))
14080 return false;
14081
14082 assert(A.getVectorLength() == B.getVectorLength());
14083 unsigned NumBytesInQWord = 8;
14084 unsigned NumBitsInByte = 8;
14085 unsigned NumBytes = A.getVectorLength();
14086 unsigned NumQWords = NumBytes / NumBytesInQWord;
14087 SmallVector<APValue, 64> Result;
14088 Result.reserve(N: NumBytes);
14089
14090 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
14091 APInt BQWord(64, 0);
14092 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14093 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14094 uint64_t Byte = B.getVectorElt(I: Idx).getInt().getZExtValue();
14095 BQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
14096 }
14097
14098 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14099 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
14100 uint64_t Ctrl = A.getVectorElt(I: Idx).getInt().getZExtValue() & 0x3F;
14101
14102 APInt Byte(8, 0);
14103 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
14104 Byte.setBitVal(BitPosition: BitIdx, BitValue: BQWord[(Ctrl + BitIdx) & 0x3F]);
14105 }
14106 Result.push_back(Elt: APValue(APSInt(Byte, /*isUnsigned*/ true)));
14107 }
14108 }
14109 return Success(V: APValue(Result.data(), Result.size()), E);
14110 }
14111
14112 case X86::BI__builtin_ia32_phminposuw128: {
14113 APValue Source;
14114 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
14115 return false;
14116 unsigned SourceLen = Source.getVectorLength();
14117 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14118 QualType ElemQT = VT->getElementType();
14119 unsigned ElemBitWidth = Info.Ctx.getTypeSize(T: ElemQT);
14120
14121 APInt MinIndex(ElemBitWidth, 0);
14122 APInt MinVal = Source.getVectorElt(I: 0).getInt();
14123 for (unsigned I = 1; I != SourceLen; ++I) {
14124 APInt Val = Source.getVectorElt(I).getInt();
14125 if (MinVal.ugt(RHS: Val)) {
14126 MinVal = Val;
14127 MinIndex = I;
14128 }
14129 }
14130
14131 bool ResultUnsigned = E->getCallReturnType(Ctx: Info.Ctx)
14132 ->castAs<VectorType>()
14133 ->getElementType()
14134 ->isUnsignedIntegerOrEnumerationType();
14135
14136 SmallVector<APValue, 8> Result;
14137 Result.reserve(N: SourceLen);
14138 Result.emplace_back(Args: APSInt(MinVal, ResultUnsigned));
14139 Result.emplace_back(Args: APSInt(MinIndex, ResultUnsigned));
14140 for (unsigned I = 0; I != SourceLen - 2; ++I) {
14141 Result.emplace_back(Args: APSInt(APInt(ElemBitWidth, 0), ResultUnsigned));
14142 }
14143 return Success(V: APValue(Result.data(), Result.size()), E);
14144 }
14145
14146 case X86::BI__builtin_ia32_psraq128:
14147 case X86::BI__builtin_ia32_psraq256:
14148 case X86::BI__builtin_ia32_psraq512:
14149 case X86::BI__builtin_ia32_psrad128:
14150 case X86::BI__builtin_ia32_psrad256:
14151 case X86::BI__builtin_ia32_psrad512:
14152 case X86::BI__builtin_ia32_psraw128:
14153 case X86::BI__builtin_ia32_psraw256:
14154 case X86::BI__builtin_ia32_psraw512: {
14155 APValue R;
14156 if (!evalShiftWithCount(
14157 Info, Call: E, Out&: R,
14158 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.ashr(ShiftAmt: Count); },
14159 OverflowOp: [](const APInt &Elt, unsigned Width) {
14160 return Elt.ashr(ShiftAmt: Width - 1);
14161 }))
14162 return false;
14163 return Success(V: R, E);
14164 }
14165
14166 case X86::BI__builtin_ia32_psllq128:
14167 case X86::BI__builtin_ia32_psllq256:
14168 case X86::BI__builtin_ia32_psllq512:
14169 case X86::BI__builtin_ia32_pslld128:
14170 case X86::BI__builtin_ia32_pslld256:
14171 case X86::BI__builtin_ia32_pslld512:
14172 case X86::BI__builtin_ia32_psllw128:
14173 case X86::BI__builtin_ia32_psllw256:
14174 case X86::BI__builtin_ia32_psllw512: {
14175 APValue R;
14176 if (!evalShiftWithCount(
14177 Info, Call: E, Out&: R,
14178 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.shl(shiftAmt: Count); },
14179 OverflowOp: [](const APInt &Elt, unsigned Width) {
14180 return APInt::getZero(numBits: Width);
14181 }))
14182 return false;
14183 return Success(V: R, E);
14184 }
14185
14186 case X86::BI__builtin_ia32_psrlq128:
14187 case X86::BI__builtin_ia32_psrlq256:
14188 case X86::BI__builtin_ia32_psrlq512:
14189 case X86::BI__builtin_ia32_psrld128:
14190 case X86::BI__builtin_ia32_psrld256:
14191 case X86::BI__builtin_ia32_psrld512:
14192 case X86::BI__builtin_ia32_psrlw128:
14193 case X86::BI__builtin_ia32_psrlw256:
14194 case X86::BI__builtin_ia32_psrlw512: {
14195 APValue R;
14196 if (!evalShiftWithCount(
14197 Info, Call: E, Out&: R,
14198 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.lshr(shiftAmt: Count); },
14199 OverflowOp: [](const APInt &Elt, unsigned Width) {
14200 return APInt::getZero(numBits: Width);
14201 }))
14202 return false;
14203 return Success(V: R, E);
14204 }
14205
14206 case X86::BI__builtin_ia32_pternlogd128_mask:
14207 case X86::BI__builtin_ia32_pternlogd256_mask:
14208 case X86::BI__builtin_ia32_pternlogd512_mask:
14209 case X86::BI__builtin_ia32_pternlogq128_mask:
14210 case X86::BI__builtin_ia32_pternlogq256_mask:
14211 case X86::BI__builtin_ia32_pternlogq512_mask: {
14212 APValue AValue, BValue, CValue, ImmValue, UValue;
14213 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14214 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14215 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14216 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14217 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14218 return false;
14219
14220 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14221 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14222 APInt Imm = ImmValue.getInt();
14223 APInt U = UValue.getInt();
14224 unsigned ResultLen = AValue.getVectorLength();
14225 SmallVector<APValue, 16> ResultElements;
14226 ResultElements.reserve(N: ResultLen);
14227
14228 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14229 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14230 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14231 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14232
14233 if (U[EltNum]) {
14234 unsigned BitWidth = ALane.getBitWidth();
14235 APInt ResLane(BitWidth, 0);
14236
14237 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14238 unsigned ABit = ALane[Bit];
14239 unsigned BBit = BLane[Bit];
14240 unsigned CBit = CLane[Bit];
14241
14242 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14243 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14244 }
14245 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14246 } else {
14247 ResultElements.push_back(Elt: APValue(APSInt(ALane, DestUnsigned)));
14248 }
14249 }
14250 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14251 }
14252 case X86::BI__builtin_ia32_pternlogd128_maskz:
14253 case X86::BI__builtin_ia32_pternlogd256_maskz:
14254 case X86::BI__builtin_ia32_pternlogd512_maskz:
14255 case X86::BI__builtin_ia32_pternlogq128_maskz:
14256 case X86::BI__builtin_ia32_pternlogq256_maskz:
14257 case X86::BI__builtin_ia32_pternlogq512_maskz: {
14258 APValue AValue, BValue, CValue, ImmValue, UValue;
14259 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
14260 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
14261 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
14262 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
14263 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
14264 return false;
14265
14266 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14267 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14268 APInt Imm = ImmValue.getInt();
14269 APInt U = UValue.getInt();
14270 unsigned ResultLen = AValue.getVectorLength();
14271 SmallVector<APValue, 16> ResultElements;
14272 ResultElements.reserve(N: ResultLen);
14273
14274 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
14275 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
14276 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
14277 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
14278
14279 unsigned BitWidth = ALane.getBitWidth();
14280 APInt ResLane(BitWidth, 0);
14281
14282 if (U[EltNum]) {
14283 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
14284 unsigned ABit = ALane[Bit];
14285 unsigned BBit = BLane[Bit];
14286 unsigned CBit = CLane[Bit];
14287
14288 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
14289 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
14290 }
14291 }
14292 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
14293 }
14294 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14295 }
14296
14297 case Builtin::BI__builtin_elementwise_clzg:
14298 case Builtin::BI__builtin_elementwise_ctzg: {
14299 APValue SourceLHS;
14300 std::optional<APValue> Fallback;
14301 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS))
14302 return false;
14303 if (E->getNumArgs() > 1) {
14304 APValue FallbackTmp;
14305 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: FallbackTmp))
14306 return false;
14307 Fallback = FallbackTmp;
14308 }
14309
14310 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14311 unsigned SourceLen = SourceLHS.getVectorLength();
14312 SmallVector<APValue, 4> ResultElements;
14313 ResultElements.reserve(N: SourceLen);
14314
14315 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14316 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
14317 if (!LHS) {
14318 // Without a fallback, a zero element is undefined
14319 if (!Fallback) {
14320 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
14321 << /*IsTrailing=*/(BuiltinOp ==
14322 Builtin::BI__builtin_elementwise_ctzg);
14323 return false;
14324 }
14325 ResultElements.push_back(Elt: Fallback->getVectorElt(I: EltNum));
14326 continue;
14327 }
14328 switch (BuiltinOp) {
14329 case Builtin::BI__builtin_elementwise_clzg:
14330 ResultElements.push_back(Elt: APValue(
14331 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countl_zero()),
14332 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14333 break;
14334 case Builtin::BI__builtin_elementwise_ctzg:
14335 ResultElements.push_back(Elt: APValue(
14336 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countr_zero()),
14337 DestEltTy->isUnsignedIntegerOrEnumerationType())));
14338 break;
14339 }
14340 }
14341
14342 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14343 }
14344
14345 case Builtin::BI__builtin_elementwise_fma: {
14346 APValue SourceX, SourceY, SourceZ;
14347 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceX) ||
14348 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceY) ||
14349 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceZ))
14350 return false;
14351
14352 unsigned SourceLen = SourceX.getVectorLength();
14353 SmallVector<APValue> ResultElements;
14354 ResultElements.reserve(N: SourceLen);
14355 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14356 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14357 const APFloat &X = SourceX.getVectorElt(I: EltNum).getFloat();
14358 const APFloat &Y = SourceY.getVectorElt(I: EltNum).getFloat();
14359 const APFloat &Z = SourceZ.getVectorElt(I: EltNum).getFloat();
14360 APFloat Result(X);
14361 (void)Result.fusedMultiplyAdd(Multiplicand: Y, Addend: Z, RM);
14362 ResultElements.push_back(Elt: APValue(Result));
14363 }
14364 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14365 }
14366
14367 case clang::X86::BI__builtin_ia32_phaddw128:
14368 case clang::X86::BI__builtin_ia32_phaddw256:
14369 case clang::X86::BI__builtin_ia32_phaddd128:
14370 case clang::X86::BI__builtin_ia32_phaddd256:
14371 case clang::X86::BI__builtin_ia32_phaddsw128:
14372 case clang::X86::BI__builtin_ia32_phaddsw256:
14373
14374 case clang::X86::BI__builtin_ia32_phsubw128:
14375 case clang::X86::BI__builtin_ia32_phsubw256:
14376 case clang::X86::BI__builtin_ia32_phsubd128:
14377 case clang::X86::BI__builtin_ia32_phsubd256:
14378 case clang::X86::BI__builtin_ia32_phsubsw128:
14379 case clang::X86::BI__builtin_ia32_phsubsw256: {
14380 APValue SourceLHS, SourceRHS;
14381 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14382 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14383 return false;
14384 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14385 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14386
14387 unsigned NumElts = SourceLHS.getVectorLength();
14388 unsigned EltBits = Info.Ctx.getIntWidth(T: DestEltTy);
14389 unsigned EltsPerLane = 128 / EltBits;
14390 SmallVector<APValue, 4> ResultElements;
14391 ResultElements.reserve(N: NumElts);
14392
14393 for (unsigned LaneStart = 0; LaneStart != NumElts;
14394 LaneStart += EltsPerLane) {
14395 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14396 APSInt LHSA = SourceLHS.getVectorElt(I: LaneStart + I).getInt();
14397 APSInt LHSB = SourceLHS.getVectorElt(I: LaneStart + I + 1).getInt();
14398 switch (BuiltinOp) {
14399 case clang::X86::BI__builtin_ia32_phaddw128:
14400 case clang::X86::BI__builtin_ia32_phaddw256:
14401 case clang::X86::BI__builtin_ia32_phaddd128:
14402 case clang::X86::BI__builtin_ia32_phaddd256: {
14403 APSInt Res(LHSA + LHSB, DestUnsigned);
14404 ResultElements.push_back(Elt: APValue(Res));
14405 break;
14406 }
14407 case clang::X86::BI__builtin_ia32_phaddsw128:
14408 case clang::X86::BI__builtin_ia32_phaddsw256: {
14409 APSInt Res(LHSA.sadd_sat(RHS: LHSB));
14410 ResultElements.push_back(Elt: APValue(Res));
14411 break;
14412 }
14413 case clang::X86::BI__builtin_ia32_phsubw128:
14414 case clang::X86::BI__builtin_ia32_phsubw256:
14415 case clang::X86::BI__builtin_ia32_phsubd128:
14416 case clang::X86::BI__builtin_ia32_phsubd256: {
14417 APSInt Res(LHSA - LHSB, DestUnsigned);
14418 ResultElements.push_back(Elt: APValue(Res));
14419 break;
14420 }
14421 case clang::X86::BI__builtin_ia32_phsubsw128:
14422 case clang::X86::BI__builtin_ia32_phsubsw256: {
14423 APSInt Res(LHSA.ssub_sat(RHS: LHSB));
14424 ResultElements.push_back(Elt: APValue(Res));
14425 break;
14426 }
14427 }
14428 }
14429 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14430 APSInt RHSA = SourceRHS.getVectorElt(I: LaneStart + I).getInt();
14431 APSInt RHSB = SourceRHS.getVectorElt(I: LaneStart + I + 1).getInt();
14432 switch (BuiltinOp) {
14433 case clang::X86::BI__builtin_ia32_phaddw128:
14434 case clang::X86::BI__builtin_ia32_phaddw256:
14435 case clang::X86::BI__builtin_ia32_phaddd128:
14436 case clang::X86::BI__builtin_ia32_phaddd256: {
14437 APSInt Res(RHSA + RHSB, DestUnsigned);
14438 ResultElements.push_back(Elt: APValue(Res));
14439 break;
14440 }
14441 case clang::X86::BI__builtin_ia32_phaddsw128:
14442 case clang::X86::BI__builtin_ia32_phaddsw256: {
14443 APSInt Res(RHSA.sadd_sat(RHS: RHSB));
14444 ResultElements.push_back(Elt: APValue(Res));
14445 break;
14446 }
14447 case clang::X86::BI__builtin_ia32_phsubw128:
14448 case clang::X86::BI__builtin_ia32_phsubw256:
14449 case clang::X86::BI__builtin_ia32_phsubd128:
14450 case clang::X86::BI__builtin_ia32_phsubd256: {
14451 APSInt Res(RHSA - RHSB, DestUnsigned);
14452 ResultElements.push_back(Elt: APValue(Res));
14453 break;
14454 }
14455 case clang::X86::BI__builtin_ia32_phsubsw128:
14456 case clang::X86::BI__builtin_ia32_phsubsw256: {
14457 APSInt Res(RHSA.ssub_sat(RHS: RHSB));
14458 ResultElements.push_back(Elt: APValue(Res));
14459 break;
14460 }
14461 }
14462 }
14463 }
14464 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14465 }
14466 case clang::X86::BI__builtin_ia32_haddpd:
14467 case clang::X86::BI__builtin_ia32_haddps:
14468 case clang::X86::BI__builtin_ia32_haddps256:
14469 case clang::X86::BI__builtin_ia32_haddpd256:
14470 case clang::X86::BI__builtin_ia32_hsubpd:
14471 case clang::X86::BI__builtin_ia32_hsubps:
14472 case clang::X86::BI__builtin_ia32_hsubps256:
14473 case clang::X86::BI__builtin_ia32_hsubpd256: {
14474 APValue SourceLHS, SourceRHS;
14475 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14476 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14477 return false;
14478 unsigned NumElts = SourceLHS.getVectorLength();
14479 SmallVector<APValue, 4> ResultElements;
14480 ResultElements.reserve(N: NumElts);
14481 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14482 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14483 unsigned EltBits = Info.Ctx.getTypeSize(T: DestEltTy);
14484 unsigned NumLanes = NumElts * EltBits / 128;
14485 unsigned NumElemsPerLane = NumElts / NumLanes;
14486 unsigned HalfElemsPerLane = NumElemsPerLane / 2;
14487
14488 for (unsigned L = 0; L != NumElts; L += NumElemsPerLane) {
14489 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14490 APFloat LHSA = SourceLHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14491 APFloat LHSB = SourceLHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14492 switch (BuiltinOp) {
14493 case clang::X86::BI__builtin_ia32_haddpd:
14494 case clang::X86::BI__builtin_ia32_haddps:
14495 case clang::X86::BI__builtin_ia32_haddps256:
14496 case clang::X86::BI__builtin_ia32_haddpd256:
14497 LHSA.add(RHS: LHSB, RM);
14498 break;
14499 case clang::X86::BI__builtin_ia32_hsubpd:
14500 case clang::X86::BI__builtin_ia32_hsubps:
14501 case clang::X86::BI__builtin_ia32_hsubps256:
14502 case clang::X86::BI__builtin_ia32_hsubpd256:
14503 LHSA.subtract(RHS: LHSB, RM);
14504 break;
14505 }
14506 ResultElements.push_back(Elt: APValue(LHSA));
14507 }
14508 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14509 APFloat RHSA = SourceRHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14510 APFloat RHSB = SourceRHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14511 switch (BuiltinOp) {
14512 case clang::X86::BI__builtin_ia32_haddpd:
14513 case clang::X86::BI__builtin_ia32_haddps:
14514 case clang::X86::BI__builtin_ia32_haddps256:
14515 case clang::X86::BI__builtin_ia32_haddpd256:
14516 RHSA.add(RHS: RHSB, RM);
14517 break;
14518 case clang::X86::BI__builtin_ia32_hsubpd:
14519 case clang::X86::BI__builtin_ia32_hsubps:
14520 case clang::X86::BI__builtin_ia32_hsubps256:
14521 case clang::X86::BI__builtin_ia32_hsubpd256:
14522 RHSA.subtract(RHS: RHSB, RM);
14523 break;
14524 }
14525 ResultElements.push_back(Elt: APValue(RHSA));
14526 }
14527 }
14528 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14529 }
14530 case clang::X86::BI__builtin_ia32_addsubpd:
14531 case clang::X86::BI__builtin_ia32_addsubps:
14532 case clang::X86::BI__builtin_ia32_addsubpd256:
14533 case clang::X86::BI__builtin_ia32_addsubps256: {
14534 // Addsub: alternates between subtraction and addition
14535 // Result[i] = (i % 2 == 0) ? (a[i] - b[i]) : (a[i] + b[i])
14536 APValue SourceLHS, SourceRHS;
14537 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14538 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14539 return false;
14540 unsigned NumElems = SourceLHS.getVectorLength();
14541 SmallVector<APValue, 8> ResultElements;
14542 ResultElements.reserve(N: NumElems);
14543 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14544
14545 for (unsigned I = 0; I != NumElems; ++I) {
14546 APFloat LHS = SourceLHS.getVectorElt(I).getFloat();
14547 APFloat RHS = SourceRHS.getVectorElt(I).getFloat();
14548 if (I % 2 == 0) {
14549 // Even indices: subtract
14550 LHS.subtract(RHS, RM);
14551 } else {
14552 // Odd indices: add
14553 LHS.add(RHS, RM);
14554 }
14555 ResultElements.push_back(Elt: APValue(LHS));
14556 }
14557 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14558 }
14559 case clang::X86::BI__builtin_ia32_pclmulqdq128:
14560 case clang::X86::BI__builtin_ia32_pclmulqdq256:
14561 case clang::X86::BI__builtin_ia32_pclmulqdq512: {
14562 // PCLMULQDQ: carry-less multiplication of selected 64-bit halves
14563 // imm8 bit 0: selects lower (0) or upper (1) 64 bits of first operand
14564 // imm8 bit 4: selects lower (0) or upper (1) 64 bits of second operand
14565 APValue SourceLHS, SourceRHS;
14566 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14567 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14568 return false;
14569
14570 APSInt Imm8;
14571 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm8, Info))
14572 return false;
14573
14574 // Extract bits 0 and 4 from imm8
14575 bool SelectUpperA = (Imm8 & 0x01) != 0;
14576 bool SelectUpperB = (Imm8 & 0x10) != 0;
14577
14578 unsigned NumElems = SourceLHS.getVectorLength();
14579 SmallVector<APValue, 8> ResultElements;
14580 ResultElements.reserve(N: NumElems);
14581 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14582 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14583
14584 // Process each 128-bit lane
14585 for (unsigned Lane = 0; Lane < NumElems; Lane += 2) {
14586 // Get the two 64-bit halves of the first operand
14587 APSInt A0 = SourceLHS.getVectorElt(I: Lane + 0).getInt();
14588 APSInt A1 = SourceLHS.getVectorElt(I: Lane + 1).getInt();
14589 // Get the two 64-bit halves of the second operand
14590 APSInt B0 = SourceRHS.getVectorElt(I: Lane + 0).getInt();
14591 APSInt B1 = SourceRHS.getVectorElt(I: Lane + 1).getInt();
14592
14593 // Select the appropriate 64-bit values based on imm8
14594 APInt A = SelectUpperA ? A1 : A0;
14595 APInt B = SelectUpperB ? B1 : B0;
14596
14597 // Extend both operands to 128 bits for carry-less multiplication
14598 APInt A128 = A.zext(width: 128);
14599 APInt B128 = B.zext(width: 128);
14600
14601 // Use APIntOps::clmul for carry-less multiplication
14602 APInt Result = llvm::APIntOps::clmul(LHS: A128, RHS: B128);
14603
14604 // Split the 128-bit result into two 64-bit halves
14605 APSInt ResultLow(Result.extractBits(numBits: 64, bitPosition: 0), DestUnsigned);
14606 APSInt ResultHigh(Result.extractBits(numBits: 64, bitPosition: 64), DestUnsigned);
14607
14608 ResultElements.push_back(Elt: APValue(ResultLow));
14609 ResultElements.push_back(Elt: APValue(ResultHigh));
14610 }
14611
14612 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14613 }
14614 case Builtin::BI__builtin_elementwise_clmul:
14615 return EvaluateBinOpExpr(llvm::APIntOps::clmul);
14616 case Builtin::BI__builtin_elementwise_pext:
14617 return EvaluateBinOpExpr(llvm::APIntOps::pext);
14618 case Builtin::BI__builtin_elementwise_pdep:
14619 return EvaluateBinOpExpr(llvm::APIntOps::pdep);
14620 case Builtin::BI__builtin_elementwise_fshl:
14621 case Builtin::BI__builtin_elementwise_fshr: {
14622 APValue SourceHi, SourceLo, SourceShift;
14623 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
14624 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
14625 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceShift))
14626 return false;
14627
14628 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14629 if (!DestEltTy->isIntegerType())
14630 return false;
14631
14632 unsigned SourceLen = SourceHi.getVectorLength();
14633 SmallVector<APValue> ResultElements;
14634 ResultElements.reserve(N: SourceLen);
14635 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14636 const APSInt &Hi = SourceHi.getVectorElt(I: EltNum).getInt();
14637 const APSInt &Lo = SourceLo.getVectorElt(I: EltNum).getInt();
14638 const APSInt &Shift = SourceShift.getVectorElt(I: EltNum).getInt();
14639 switch (BuiltinOp) {
14640 case Builtin::BI__builtin_elementwise_fshl:
14641 ResultElements.push_back(Elt: APValue(
14642 APSInt(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned())));
14643 break;
14644 case Builtin::BI__builtin_elementwise_fshr:
14645 ResultElements.push_back(Elt: APValue(
14646 APSInt(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned())));
14647 break;
14648 }
14649 }
14650
14651 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14652 }
14653
14654 case X86::BI__builtin_ia32_shuf_f32x4_256:
14655 case X86::BI__builtin_ia32_shuf_i32x4_256:
14656 case X86::BI__builtin_ia32_shuf_f64x2_256:
14657 case X86::BI__builtin_ia32_shuf_i64x2_256:
14658 case X86::BI__builtin_ia32_shuf_f32x4:
14659 case X86::BI__builtin_ia32_shuf_i32x4:
14660 case X86::BI__builtin_ia32_shuf_f64x2:
14661 case X86::BI__builtin_ia32_shuf_i64x2: {
14662 APValue SourceA, SourceB;
14663 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
14664 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB))
14665 return false;
14666
14667 APSInt Imm;
14668 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14669 return false;
14670
14671 // Destination and sources A, B all have the same type.
14672 unsigned NumElems = SourceA.getVectorLength();
14673 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14674 QualType ElemQT = VT->getElementType();
14675 unsigned ElemBits = Info.Ctx.getTypeSize(T: ElemQT);
14676 unsigned LaneBits = 128u;
14677 unsigned NumLanes = (NumElems * ElemBits) / LaneBits;
14678 unsigned NumElemsPerLane = LaneBits / ElemBits;
14679
14680 unsigned DstLen = SourceA.getVectorLength();
14681 SmallVector<APValue, 16> ResultElements;
14682 ResultElements.reserve(N: DstLen);
14683
14684 APValue R;
14685 if (!evalShuffleGeneric(
14686 Info, Call: E, Out&: R,
14687 GetSourceIndex: [NumLanes, NumElemsPerLane](unsigned DstIdx, unsigned ShuffleMask)
14688 -> std::pair<unsigned, int> {
14689 // DstIdx determines source. ShuffleMask selects lane in source.
14690 unsigned BitsPerElem = NumLanes / 2;
14691 unsigned IndexMask = (1u << BitsPerElem) - 1;
14692 unsigned Lane = DstIdx / NumElemsPerLane;
14693 unsigned SrcIdx = (Lane < NumLanes / 2) ? 0 : 1;
14694 unsigned BitIdx = BitsPerElem * Lane;
14695 unsigned SrcLaneIdx = (ShuffleMask >> BitIdx) & IndexMask;
14696 unsigned ElemInLane = DstIdx % NumElemsPerLane;
14697 unsigned IdxToPick = SrcLaneIdx * NumElemsPerLane + ElemInLane;
14698 return {SrcIdx, IdxToPick};
14699 }))
14700 return false;
14701 return Success(V: R, E);
14702 }
14703
14704 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14705 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14706 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi:
14707 case X86::BI__builtin_ia32_vgf2p8affineqb_v16qi:
14708 case X86::BI__builtin_ia32_vgf2p8affineqb_v32qi:
14709 case X86::BI__builtin_ia32_vgf2p8affineqb_v64qi: {
14710
14711 APValue X, A;
14712 APSInt Imm;
14713 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: X) ||
14714 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: A) ||
14715 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14716 return false;
14717
14718 assert(X.isVector() && A.isVector());
14719 assert(X.getVectorLength() == A.getVectorLength());
14720
14721 bool IsInverse = false;
14722 switch (BuiltinOp) {
14723 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14724 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14725 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi: {
14726 IsInverse = true;
14727 }
14728 }
14729
14730 unsigned NumBitsInByte = 8;
14731 unsigned NumBytesInQWord = 8;
14732 unsigned NumBitsInQWord = 64;
14733 unsigned NumBytes = A.getVectorLength();
14734 unsigned NumQWords = NumBytes / NumBytesInQWord;
14735 SmallVector<APValue, 64> Result;
14736 Result.reserve(N: NumBytes);
14737
14738 // computing A*X + Imm
14739 for (unsigned QWordIdx = 0; QWordIdx != NumQWords; ++QWordIdx) {
14740 // Extract the QWords from X, A
14741 APInt XQWord(NumBitsInQWord, 0);
14742 APInt AQWord(NumBitsInQWord, 0);
14743 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14744 unsigned Idx = QWordIdx * NumBytesInQWord + ByteIdx;
14745 APInt XByte = X.getVectorElt(I: Idx).getInt();
14746 APInt AByte = A.getVectorElt(I: Idx).getInt();
14747 XQWord.insertBits(SubBits: XByte, bitPosition: ByteIdx * NumBitsInByte);
14748 AQWord.insertBits(SubBits: AByte, bitPosition: ByteIdx * NumBitsInByte);
14749 }
14750
14751 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14752 uint8_t XByte =
14753 XQWord.lshr(shiftAmt: ByteIdx * NumBitsInByte).getLoBits(numBits: 8).getZExtValue();
14754 Result.push_back(Elt: APValue(APSInt(
14755 APInt(8, GFNIAffine(XByte, AQword: AQWord, Imm, Inverse: IsInverse)), false)));
14756 }
14757 }
14758
14759 return Success(V: APValue(Result.data(), Result.size()), E);
14760 }
14761
14762 case X86::BI__builtin_ia32_vgf2p8mulb_v16qi:
14763 case X86::BI__builtin_ia32_vgf2p8mulb_v32qi:
14764 case X86::BI__builtin_ia32_vgf2p8mulb_v64qi: {
14765 APValue A, B;
14766 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
14767 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
14768 return false;
14769
14770 assert(A.isVector() && B.isVector());
14771 assert(A.getVectorLength() == B.getVectorLength());
14772
14773 unsigned NumBytes = A.getVectorLength();
14774 SmallVector<APValue, 64> Result;
14775 Result.reserve(N: NumBytes);
14776
14777 for (unsigned ByteIdx = 0; ByteIdx != NumBytes; ++ByteIdx) {
14778 uint8_t AByte = A.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14779 uint8_t BByte = B.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14780 Result.push_back(Elt: APValue(
14781 APSInt(APInt(8, GFNIMul(AByte, BByte)), /*IsUnsigned=*/false)));
14782 }
14783
14784 return Success(V: APValue(Result.data(), Result.size()), E);
14785 }
14786
14787 case X86::BI__builtin_ia32_insertf32x4_256:
14788 case X86::BI__builtin_ia32_inserti32x4_256:
14789 case X86::BI__builtin_ia32_insertf64x2_256:
14790 case X86::BI__builtin_ia32_inserti64x2_256:
14791 case X86::BI__builtin_ia32_insertf32x4:
14792 case X86::BI__builtin_ia32_inserti32x4:
14793 case X86::BI__builtin_ia32_insertf64x2_512:
14794 case X86::BI__builtin_ia32_inserti64x2_512:
14795 case X86::BI__builtin_ia32_insertf32x8:
14796 case X86::BI__builtin_ia32_inserti32x8:
14797 case X86::BI__builtin_ia32_insertf64x4:
14798 case X86::BI__builtin_ia32_inserti64x4:
14799 case X86::BI__builtin_ia32_vinsertf128_ps256:
14800 case X86::BI__builtin_ia32_vinsertf128_pd256:
14801 case X86::BI__builtin_ia32_vinsertf128_si256:
14802 case X86::BI__builtin_ia32_insert128i256: {
14803 APValue SourceDst, SourceSub;
14804 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceDst) ||
14805 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceSub))
14806 return false;
14807
14808 APSInt Imm;
14809 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14810 return false;
14811
14812 assert(SourceDst.isVector() && SourceSub.isVector());
14813 unsigned DstLen = SourceDst.getVectorLength();
14814 unsigned SubLen = SourceSub.getVectorLength();
14815 assert(SubLen != 0 && DstLen != 0 && (DstLen % SubLen) == 0);
14816 unsigned NumLanes = DstLen / SubLen;
14817 unsigned LaneIdx = (Imm.getZExtValue() % NumLanes) * SubLen;
14818
14819 SmallVector<APValue, 16> ResultElements;
14820 ResultElements.reserve(N: DstLen);
14821
14822 for (unsigned EltNum = 0; EltNum < DstLen; ++EltNum) {
14823 if (EltNum >= LaneIdx && EltNum < LaneIdx + SubLen)
14824 ResultElements.push_back(Elt: SourceSub.getVectorElt(I: EltNum - LaneIdx));
14825 else
14826 ResultElements.push_back(Elt: SourceDst.getVectorElt(I: EltNum));
14827 }
14828
14829 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14830 }
14831
14832 case clang::X86::BI__builtin_ia32_vec_set_v4hi:
14833 case clang::X86::BI__builtin_ia32_vec_set_v16qi:
14834 case clang::X86::BI__builtin_ia32_vec_set_v8hi:
14835 case clang::X86::BI__builtin_ia32_vec_set_v4si:
14836 case clang::X86::BI__builtin_ia32_vec_set_v2di:
14837 case clang::X86::BI__builtin_ia32_vec_set_v32qi:
14838 case clang::X86::BI__builtin_ia32_vec_set_v16hi:
14839 case clang::X86::BI__builtin_ia32_vec_set_v8si:
14840 case clang::X86::BI__builtin_ia32_vec_set_v4di: {
14841 APValue VecVal;
14842 APSInt Scalar, IndexAPS;
14843 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: VecVal, Info) ||
14844 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Scalar, Info) ||
14845 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: IndexAPS, Info))
14846 return false;
14847
14848 QualType ElemTy = E->getType()->castAs<VectorType>()->getElementType();
14849 unsigned ElemWidth = Info.Ctx.getIntWidth(T: ElemTy);
14850 bool ElemUnsigned = ElemTy->isUnsignedIntegerOrEnumerationType();
14851 Scalar.setIsUnsigned(ElemUnsigned);
14852 APSInt ElemAPS = Scalar.extOrTrunc(width: ElemWidth);
14853 APValue ElemAV(ElemAPS);
14854
14855 unsigned NumElems = VecVal.getVectorLength();
14856 unsigned Index =
14857 static_cast<unsigned>(IndexAPS.getZExtValue() & (NumElems - 1));
14858
14859 SmallVector<APValue, 4> Elems;
14860 Elems.reserve(N: NumElems);
14861 for (unsigned ElemNum = 0; ElemNum != NumElems; ++ElemNum)
14862 Elems.push_back(Elt: ElemNum == Index ? ElemAV : VecVal.getVectorElt(I: ElemNum));
14863
14864 return Success(V: APValue(Elems.data(), NumElems), E);
14865 }
14866
14867 case X86::BI__builtin_ia32_pslldqi128_byteshift:
14868 case X86::BI__builtin_ia32_pslldqi256_byteshift:
14869 case X86::BI__builtin_ia32_pslldqi512_byteshift: {
14870 APValue R;
14871 if (!evalShuffleGeneric(
14872 Info, Call: E, Out&: R,
14873 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
14874 unsigned LaneBase = (DstIdx / 16) * 16;
14875 unsigned LaneIdx = DstIdx % 16;
14876 if (LaneIdx < Shift)
14877 return std::make_pair(x: 0, y: -1);
14878
14879 return std::make_pair(
14880 x: 0, y: static_cast<int>(LaneBase + LaneIdx - Shift));
14881 }))
14882 return false;
14883 return Success(V: R, E);
14884 }
14885
14886 case X86::BI__builtin_ia32_psrldqi128_byteshift:
14887 case X86::BI__builtin_ia32_psrldqi256_byteshift:
14888 case X86::BI__builtin_ia32_psrldqi512_byteshift: {
14889 APValue R;
14890 if (!evalShuffleGeneric(
14891 Info, Call: E, Out&: R,
14892 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
14893 unsigned LaneBase = (DstIdx / 16) * 16;
14894 unsigned LaneIdx = DstIdx % 16;
14895 if (LaneIdx + Shift < 16)
14896 return std::make_pair(
14897 x: 0, y: static_cast<int>(LaneBase + LaneIdx + Shift));
14898
14899 return std::make_pair(x: 0, y: -1);
14900 }))
14901 return false;
14902 return Success(V: R, E);
14903 }
14904
14905 case X86::BI__builtin_ia32_palignr128:
14906 case X86::BI__builtin_ia32_palignr256:
14907 case X86::BI__builtin_ia32_palignr512: {
14908 APValue R;
14909 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Shift) {
14910 // Default to -1 → zero-fill this destination element
14911 unsigned VecIdx = 1;
14912 int ElemIdx = -1;
14913
14914 int Lane = DstIdx / 16;
14915 int Offset = DstIdx % 16;
14916
14917 // Elements come from VecB first, then VecA after the shift boundary
14918 unsigned ShiftedIdx = Offset + (Shift & 0xFF);
14919 if (ShiftedIdx < 16) { // from VecB
14920 ElemIdx = ShiftedIdx + (Lane * 16);
14921 } else if (ShiftedIdx < 32) { // from VecA
14922 VecIdx = 0;
14923 ElemIdx = (ShiftedIdx - 16) + (Lane * 16);
14924 }
14925
14926 return std::pair<unsigned, int>{VecIdx, ElemIdx};
14927 }))
14928 return false;
14929 return Success(V: R, E);
14930 }
14931 case X86::BI__builtin_ia32_alignd128:
14932 case X86::BI__builtin_ia32_alignd256:
14933 case X86::BI__builtin_ia32_alignd512:
14934 case X86::BI__builtin_ia32_alignq128:
14935 case X86::BI__builtin_ia32_alignq256:
14936 case X86::BI__builtin_ia32_alignq512: {
14937 APValue R;
14938 unsigned NumElems = E->getType()->castAs<VectorType>()->getNumElements();
14939 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14940 GetSourceIndex: [NumElems](unsigned DstIdx, unsigned Shift) {
14941 unsigned Imm = Shift & 0xFF;
14942 unsigned EffectiveShift = Imm & (NumElems - 1);
14943 unsigned SourcePos = DstIdx + EffectiveShift;
14944 unsigned VecIdx = SourcePos < NumElems ? 1 : 0;
14945 unsigned ElemIdx = SourcePos & (NumElems - 1);
14946
14947 return std::pair<unsigned, int>{
14948 VecIdx, static_cast<int>(ElemIdx)};
14949 }))
14950 return false;
14951 return Success(V: R, E);
14952 }
14953 case X86::BI__builtin_ia32_permvarsi256:
14954 case X86::BI__builtin_ia32_permvarsf256:
14955 case X86::BI__builtin_ia32_permvardf512:
14956 case X86::BI__builtin_ia32_permvardi512:
14957 case X86::BI__builtin_ia32_permvarhi128: {
14958 APValue R;
14959 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14960 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14961 int Offset = ShuffleMask & 0x7;
14962 return std::pair<unsigned, int>{0, Offset};
14963 }))
14964 return false;
14965 return Success(V: R, E);
14966 }
14967 case X86::BI__builtin_ia32_permvarqi128:
14968 case X86::BI__builtin_ia32_permvarhi256:
14969 case X86::BI__builtin_ia32_permvarsi512:
14970 case X86::BI__builtin_ia32_permvarsf512: {
14971 APValue R;
14972 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14973 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14974 int Offset = ShuffleMask & 0xF;
14975 return std::pair<unsigned, int>{0, Offset};
14976 }))
14977 return false;
14978 return Success(V: R, E);
14979 }
14980 case X86::BI__builtin_ia32_permvardi256:
14981 case X86::BI__builtin_ia32_permvardf256: {
14982 APValue R;
14983 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14984 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14985 int Offset = ShuffleMask & 0x3;
14986 return std::pair<unsigned, int>{0, Offset};
14987 }))
14988 return false;
14989 return Success(V: R, E);
14990 }
14991 case X86::BI__builtin_ia32_permvarqi256:
14992 case X86::BI__builtin_ia32_permvarhi512: {
14993 APValue R;
14994 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14995 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14996 int Offset = ShuffleMask & 0x1F;
14997 return std::pair<unsigned, int>{0, Offset};
14998 }))
14999 return false;
15000 return Success(V: R, E);
15001 }
15002 case X86::BI__builtin_ia32_permvarqi512: {
15003 APValue R;
15004 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15005 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15006 int Offset = ShuffleMask & 0x3F;
15007 return std::pair<unsigned, int>{0, Offset};
15008 }))
15009 return false;
15010 return Success(V: R, E);
15011 }
15012 case X86::BI__builtin_ia32_vpermi2varq128:
15013 case X86::BI__builtin_ia32_vpermi2varpd128: {
15014 APValue R;
15015 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15016 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15017 int Offset = ShuffleMask & 0x1;
15018 unsigned SrcIdx = (ShuffleMask >> 1) & 0x1;
15019 return std::pair<unsigned, int>{SrcIdx, Offset};
15020 }))
15021 return false;
15022 return Success(V: R, E);
15023 }
15024 case X86::BI__builtin_ia32_vpermi2vard128:
15025 case X86::BI__builtin_ia32_vpermi2varps128:
15026 case X86::BI__builtin_ia32_vpermi2varq256:
15027 case X86::BI__builtin_ia32_vpermi2varpd256: {
15028 APValue R;
15029 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15030 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15031 int Offset = ShuffleMask & 0x3;
15032 unsigned SrcIdx = (ShuffleMask >> 2) & 0x1;
15033 return std::pair<unsigned, int>{SrcIdx, Offset};
15034 }))
15035 return false;
15036 return Success(V: R, E);
15037 }
15038 case X86::BI__builtin_ia32_vpermi2varhi128:
15039 case X86::BI__builtin_ia32_vpermi2vard256:
15040 case X86::BI__builtin_ia32_vpermi2varps256:
15041 case X86::BI__builtin_ia32_vpermi2varq512:
15042 case X86::BI__builtin_ia32_vpermi2varpd512: {
15043 APValue R;
15044 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15045 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15046 int Offset = ShuffleMask & 0x7;
15047 unsigned SrcIdx = (ShuffleMask >> 3) & 0x1;
15048 return std::pair<unsigned, int>{SrcIdx, Offset};
15049 }))
15050 return false;
15051 return Success(V: R, E);
15052 }
15053 case X86::BI__builtin_ia32_vpermi2varqi128:
15054 case X86::BI__builtin_ia32_vpermi2varhi256:
15055 case X86::BI__builtin_ia32_vpermi2vard512:
15056 case X86::BI__builtin_ia32_vpermi2varps512: {
15057 APValue R;
15058 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15059 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15060 int Offset = ShuffleMask & 0xF;
15061 unsigned SrcIdx = (ShuffleMask >> 4) & 0x1;
15062 return std::pair<unsigned, int>{SrcIdx, Offset};
15063 }))
15064 return false;
15065 return Success(V: R, E);
15066 }
15067 case X86::BI__builtin_ia32_vpermi2varqi256:
15068 case X86::BI__builtin_ia32_vpermi2varhi512: {
15069 APValue R;
15070 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15071 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15072 int Offset = ShuffleMask & 0x1F;
15073 unsigned SrcIdx = (ShuffleMask >> 5) & 0x1;
15074 return std::pair<unsigned, int>{SrcIdx, Offset};
15075 }))
15076 return false;
15077 return Success(V: R, E);
15078 }
15079 case X86::BI__builtin_ia32_vpermi2varqi512: {
15080 APValue R;
15081 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
15082 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
15083 int Offset = ShuffleMask & 0x3F;
15084 unsigned SrcIdx = (ShuffleMask >> 6) & 0x1;
15085 return std::pair<unsigned, int>{SrcIdx, Offset};
15086 }))
15087 return false;
15088 return Success(V: R, E);
15089 }
15090
15091 case clang::X86::BI__builtin_ia32_minps:
15092 case clang::X86::BI__builtin_ia32_minpd:
15093 case clang::X86::BI__builtin_ia32_minps256:
15094 case clang::X86::BI__builtin_ia32_minpd256:
15095 case clang::X86::BI__builtin_ia32_minps512:
15096 case clang::X86::BI__builtin_ia32_minpd512:
15097 case clang::X86::BI__builtin_ia32_minph128:
15098 case clang::X86::BI__builtin_ia32_minph256:
15099 case clang::X86::BI__builtin_ia32_minph512:
15100 return EvaluateFpBinOpExpr(
15101 [](const APFloat &A, const APFloat &B,
15102 std::optional<APSInt>) -> std::optional<APFloat> {
15103 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15104 B.isInfinity() || B.isDenormal())
15105 return std::nullopt;
15106 if (A.isZero() && B.isZero())
15107 return B;
15108 return llvm::minimum(A, B);
15109 });
15110
15111 case clang::X86::BI__builtin_ia32_minss:
15112 case clang::X86::BI__builtin_ia32_minsd:
15113 return EvaluateFpBinOpExpr(
15114 [](const APFloat &A, const APFloat &B,
15115 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15116 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/true);
15117 },
15118 /*IsScalar=*/true);
15119
15120 case clang::X86::BI__builtin_ia32_minsd_round_mask:
15121 case clang::X86::BI__builtin_ia32_minss_round_mask:
15122 case clang::X86::BI__builtin_ia32_minsh_round_mask:
15123 case clang::X86::BI__builtin_ia32_maxsd_round_mask:
15124 case clang::X86::BI__builtin_ia32_maxss_round_mask:
15125 case clang::X86::BI__builtin_ia32_maxsh_round_mask: {
15126 bool IsMin = BuiltinOp == clang::X86::BI__builtin_ia32_minsd_round_mask ||
15127 BuiltinOp == clang::X86::BI__builtin_ia32_minss_round_mask ||
15128 BuiltinOp == clang::X86::BI__builtin_ia32_minsh_round_mask;
15129 return EvaluateScalarFpRoundMaskBinOp(
15130 [IsMin](const APFloat &A, const APFloat &B,
15131 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15132 return EvalScalarMinMaxFp(A, B, RoundingMode, IsMin);
15133 });
15134 }
15135
15136 case clang::X86::BI__builtin_ia32_maxps:
15137 case clang::X86::BI__builtin_ia32_maxpd:
15138 case clang::X86::BI__builtin_ia32_maxps256:
15139 case clang::X86::BI__builtin_ia32_maxpd256:
15140 case clang::X86::BI__builtin_ia32_maxps512:
15141 case clang::X86::BI__builtin_ia32_maxpd512:
15142 case clang::X86::BI__builtin_ia32_maxph128:
15143 case clang::X86::BI__builtin_ia32_maxph256:
15144 case clang::X86::BI__builtin_ia32_maxph512:
15145 return EvaluateFpBinOpExpr(
15146 [](const APFloat &A, const APFloat &B,
15147 std::optional<APSInt>) -> std::optional<APFloat> {
15148 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
15149 B.isInfinity() || B.isDenormal())
15150 return std::nullopt;
15151 if (A.isZero() && B.isZero())
15152 return B;
15153 return llvm::maximum(A, B);
15154 });
15155
15156 case clang::X86::BI__builtin_ia32_maxss:
15157 case clang::X86::BI__builtin_ia32_maxsd:
15158 return EvaluateFpBinOpExpr(
15159 [](const APFloat &A, const APFloat &B,
15160 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
15161 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/false);
15162 },
15163 /*IsScalar=*/true);
15164
15165 case clang::X86::BI__builtin_ia32_vcvtps2ph:
15166 case clang::X86::BI__builtin_ia32_vcvtps2ph256: {
15167 APValue SrcVec;
15168 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec))
15169 return false;
15170
15171 APSInt Imm;
15172 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info))
15173 return false;
15174
15175 const auto *SrcVTy = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
15176 unsigned SrcNumElems = SrcVTy->getNumElements();
15177 const auto *DstVTy = E->getType()->castAs<VectorType>();
15178 unsigned DstNumElems = DstVTy->getNumElements();
15179 QualType DstElemTy = DstVTy->getElementType();
15180
15181 const llvm::fltSemantics &HalfSem =
15182 Info.Ctx.getFloatTypeSemantics(T: Info.Ctx.HalfTy);
15183
15184 int ImmVal = Imm.getZExtValue();
15185 bool UseMXCSR = (ImmVal & 4) != 0;
15186 bool IsFPConstrained =
15187 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained();
15188
15189 llvm::RoundingMode RM;
15190 if (!UseMXCSR) {
15191 switch (ImmVal & 3) {
15192 case 0:
15193 RM = llvm::RoundingMode::NearestTiesToEven;
15194 break;
15195 case 1:
15196 RM = llvm::RoundingMode::TowardNegative;
15197 break;
15198 case 2:
15199 RM = llvm::RoundingMode::TowardPositive;
15200 break;
15201 case 3:
15202 RM = llvm::RoundingMode::TowardZero;
15203 break;
15204 default:
15205 llvm_unreachable("Invalid immediate rounding mode");
15206 }
15207 } else {
15208 RM = llvm::RoundingMode::NearestTiesToEven;
15209 }
15210
15211 SmallVector<APValue, 8> ResultElements;
15212 ResultElements.reserve(N: DstNumElems);
15213
15214 for (unsigned I = 0; I < SrcNumElems; ++I) {
15215 APFloat SrcVal = SrcVec.getVectorElt(I).getFloat();
15216
15217 bool LostInfo;
15218 APFloat::opStatus St = SrcVal.convert(ToSemantics: HalfSem, RM, losesInfo: &LostInfo);
15219
15220 if (UseMXCSR && IsFPConstrained && St != APFloat::opOK) {
15221 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
15222 return false;
15223 }
15224
15225 APSInt DstInt(SrcVal.bitcastToAPInt(),
15226 DstElemTy->isUnsignedIntegerOrEnumerationType());
15227 ResultElements.push_back(Elt: APValue(DstInt));
15228 }
15229
15230 if (DstNumElems > SrcNumElems) {
15231 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: DstElemTy);
15232 for (unsigned I = SrcNumElems; I < DstNumElems; ++I) {
15233 ResultElements.push_back(Elt: APValue(Zero));
15234 }
15235 }
15236
15237 return Success(V: ResultElements, E);
15238 }
15239 case X86::BI__builtin_ia32_vperm2f128_pd256:
15240 case X86::BI__builtin_ia32_vperm2f128_ps256:
15241 case X86::BI__builtin_ia32_vperm2f128_si256:
15242 case X86::BI__builtin_ia32_permti256: {
15243 unsigned NumElements =
15244 E->getArg(Arg: 0)->getType()->getAs<VectorType>()->getNumElements();
15245 unsigned PreservedBitsCnt = NumElements >> 2;
15246 APValue R;
15247 if (!evalShuffleGeneric(
15248 Info, Call: E, Out&: R,
15249 GetSourceIndex: [PreservedBitsCnt](unsigned DstIdx, unsigned ShuffleMask) {
15250 unsigned ControlBitsCnt = DstIdx >> PreservedBitsCnt << 2;
15251 unsigned ControlBits = ShuffleMask >> ControlBitsCnt;
15252
15253 if (ControlBits & 0b1000)
15254 return std::make_pair(x: 0u, y: -1);
15255
15256 unsigned SrcVecIdx = (ControlBits & 0b10) >> 1;
15257 unsigned PreservedBitsMask = (1 << PreservedBitsCnt) - 1;
15258 int SrcIdx = ((ControlBits & 0b1) << PreservedBitsCnt) |
15259 (DstIdx & PreservedBitsMask);
15260 return std::make_pair(x&: SrcVecIdx, y&: SrcIdx);
15261 }))
15262 return false;
15263 return Success(V: R, E);
15264 }
15265 case X86::BI__builtin_ia32_vpdpwssd128:
15266 case X86::BI__builtin_ia32_vpdpwssd256:
15267 case X86::BI__builtin_ia32_vpdpwssd512:
15268 case X86::BI__builtin_ia32_vpdpbusd128:
15269 case X86::BI__builtin_ia32_vpdpbusd256:
15270 case X86::BI__builtin_ia32_vpdpbusd512:
15271 return EvalVectorDotProduct(false);
15272 case X86::BI__builtin_ia32_vpdpwssds128:
15273 case X86::BI__builtin_ia32_vpdpwssds256:
15274 case X86::BI__builtin_ia32_vpdpwssds512:
15275 case X86::BI__builtin_ia32_vpdpbusds128:
15276 case X86::BI__builtin_ia32_vpdpbusds256:
15277 case X86::BI__builtin_ia32_vpdpbusds512:
15278 return EvalVectorDotProduct(true);
15279 case X86::BI__builtin_ia32_cvtpd2dq:
15280 case X86::BI__builtin_ia32_cvtps2dq:
15281 case X86::BI__builtin_ia32_cvttpd2dq:
15282 case X86::BI__builtin_ia32_cvttps2dq:
15283 case X86::BI__builtin_ia32_cvtpd2dq256:
15284 case X86::BI__builtin_ia32_cvtps2dq256:
15285 case X86::BI__builtin_ia32_cvttpd2dq256:
15286 case X86::BI__builtin_ia32_cvttps2dq256: {
15287 APValue SrcVec;
15288 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec) || !SrcVec.isVector())
15289 return false;
15290
15291 const auto *VT = E->getType()->castAs<VectorType>();
15292 QualType EltTy = VT->getElementType();
15293 bool isUnsigned = EltTy->isUnsignedIntegerType();
15294 unsigned BitWidth = Info.Ctx.getIntWidth(T: EltTy);
15295
15296 unsigned NumSrcElems = SrcVec.getVectorLength();
15297 unsigned NumDstElems = VT->getNumElements();
15298
15299 SmallVector<APValue, 8> ResultElts;
15300 for (unsigned i = 0; i != NumDstElems; ++i) {
15301 if (i < NumSrcElems) {
15302 llvm::APFloat FloatElem = SrcVec.getVectorElt(I: i).getFloat();
15303 llvm::APSInt IntResult(BitWidth, isUnsigned);
15304 bool IsExact = false;
15305 // We only allow exact conversions so rounding mode does not matter for
15306 // cvt* and cvtt* builtins
15307 FloatElem.convertToInteger(Result&: IntResult, RM: llvm::APFloat::rmTowardZero,
15308 IsExact: &IsExact);
15309 if (!IsExact)
15310 return false;
15311 ResultElts.push_back(Elt: APValue(IntResult));
15312 } else
15313 // Pad remaining lanes with zero
15314 ResultElts.push_back(Elt: APValue(llvm::APSInt(BitWidth, isUnsigned)));
15315 }
15316 return Success(V: ResultElts, E);
15317 }
15318 }
15319}
15320
15321bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
15322 APValue Source;
15323 QualType SourceVecType = E->getSrcExpr()->getType();
15324 if (!EvaluateAsRValue(Info, E: E->getSrcExpr(), Result&: Source))
15325 return false;
15326
15327 QualType DestTy = E->getType()->castAs<VectorType>()->getElementType();
15328 QualType SourceTy = SourceVecType->castAs<VectorType>()->getElementType();
15329
15330 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15331
15332 auto SourceLen = Source.getVectorLength();
15333 SmallVector<APValue, 4> ResultElements;
15334 ResultElements.reserve(N: SourceLen);
15335 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
15336 APValue Elt;
15337 if (!handleVectorElementCast(Info, FPO, E, SourceTy, DestTy,
15338 Original: Source.getVectorElt(I: EltNum), Result&: Elt))
15339 return false;
15340 ResultElements.push_back(Elt: std::move(Elt));
15341 }
15342
15343 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15344}
15345
15346static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,
15347 QualType ElemType, APValue const &VecVal1,
15348 APValue const &VecVal2, unsigned EltNum,
15349 APValue &Result) {
15350 unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength();
15351 unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength();
15352
15353 APSInt IndexVal = E->getShuffleMaskIdx(N: EltNum);
15354 int64_t index = IndexVal.getExtValue();
15355 // The spec says that -1 should be treated as undef for optimizations,
15356 // but in constexpr we'd have to produce an APValue::Indeterminate,
15357 // which is prohibited from being a top-level constant value. Emit a
15358 // diagnostic instead.
15359 if (index == -1) {
15360 Info.FFDiag(
15361 E, DiagId: diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
15362 << EltNum;
15363 return false;
15364 }
15365
15366 if (index < 0 ||
15367 index >= TotalElementsInInputVector1 + TotalElementsInInputVector2)
15368 llvm_unreachable("Out of bounds shuffle index");
15369
15370 if (index >= TotalElementsInInputVector1)
15371 Result = VecVal2.getVectorElt(I: index - TotalElementsInInputVector1);
15372 else
15373 Result = VecVal1.getVectorElt(I: index);
15374 return true;
15375}
15376
15377bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
15378 // FIXME: Unary shuffle with mask not currently supported.
15379 if (E->getNumSubExprs() == 2)
15380 return Error(E);
15381 APValue VecVal1;
15382 const Expr *Vec1 = E->getExpr(Index: 0);
15383 if (!EvaluateAsRValue(Info, E: Vec1, Result&: VecVal1))
15384 return false;
15385 APValue VecVal2;
15386 const Expr *Vec2 = E->getExpr(Index: 1);
15387 if (!EvaluateAsRValue(Info, E: Vec2, Result&: VecVal2))
15388 return false;
15389
15390 VectorType const *DestVecTy = E->getType()->castAs<VectorType>();
15391 QualType DestElTy = DestVecTy->getElementType();
15392
15393 auto TotalElementsInOutputVector = DestVecTy->getNumElements();
15394
15395 SmallVector<APValue, 4> ResultElements;
15396 ResultElements.reserve(N: TotalElementsInOutputVector);
15397 for (unsigned EltNum = 0; EltNum < TotalElementsInOutputVector; ++EltNum) {
15398 APValue Elt;
15399 if (!handleVectorShuffle(Info, E, ElemType: DestElTy, VecVal1, VecVal2, EltNum, Result&: Elt))
15400 return false;
15401 ResultElements.push_back(Elt: std::move(Elt));
15402 }
15403
15404 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
15405}
15406
15407//===----------------------------------------------------------------------===//
15408// Matrix Evaluation
15409//===----------------------------------------------------------------------===//
15410
15411namespace {
15412class MatrixExprEvaluator : public ExprEvaluatorBase<MatrixExprEvaluator> {
15413 APValue &Result;
15414
15415public:
15416 MatrixExprEvaluator(EvalInfo &Info, APValue &Result)
15417 : ExprEvaluatorBaseTy(Info), Result(Result) {}
15418
15419 bool Success(ArrayRef<APValue> M, const Expr *E) {
15420 auto *CMTy = E->getType()->castAs<ConstantMatrixType>();
15421 assert(M.size() == CMTy->getNumElementsFlattened());
15422 // FIXME: remove this APValue copy.
15423 Result = APValue(M.data(), CMTy->getNumRows(), CMTy->getNumColumns());
15424 return true;
15425 }
15426 bool Success(const APValue &M, const Expr *E) {
15427 assert(M.isMatrix() && "expected matrix");
15428 Result = M;
15429 return true;
15430 }
15431
15432 bool VisitCastExpr(const CastExpr *E);
15433 bool VisitInitListExpr(const InitListExpr *E);
15434};
15435} // end anonymous namespace
15436
15437static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info) {
15438 assert(E->isPRValue() && E->getType()->isConstantMatrixType() &&
15439 "not a matrix prvalue");
15440 return MatrixExprEvaluator(Info, Result).Visit(S: E);
15441}
15442
15443bool MatrixExprEvaluator::VisitCastExpr(const CastExpr *E) {
15444 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15445 unsigned NumRows = MT->getNumRows();
15446 unsigned NumCols = MT->getNumColumns();
15447 unsigned NElts = NumRows * NumCols;
15448 QualType EltTy = MT->getElementType();
15449 const Expr *SE = E->getSubExpr();
15450
15451 switch (E->getCastKind()) {
15452 case CK_HLSLAggregateSplatCast: {
15453 APValue Val;
15454 QualType ValTy;
15455
15456 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15457 return false;
15458
15459 APValue CastedVal;
15460 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15461 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: EltTy, Original: Val, Result&: CastedVal))
15462 return false;
15463
15464 SmallVector<APValue, 16> SplatEls(NElts, CastedVal);
15465 return Success(M: SplatEls, E);
15466 }
15467 case CK_HLSLElementwiseCast: {
15468 SmallVector<APValue> SrcVals;
15469 SmallVector<QualType> SrcTypes;
15470
15471 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
15472 return false;
15473
15474 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15475 SmallVector<QualType, 16> DestTypes(NElts, EltTy);
15476 SmallVector<APValue, 16> ResultEls(NElts);
15477 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
15478 Results&: ResultEls))
15479 return false;
15480 return Success(M: ResultEls, E);
15481 }
15482 default:
15483 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15484 }
15485}
15486
15487bool MatrixExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
15488 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15489 QualType EltTy = MT->getElementType();
15490
15491 assert(E->getNumInits() == MT->getNumElementsFlattened() &&
15492 "Expected number of elements in initializer list to match the number "
15493 "of matrix elements");
15494
15495 SmallVector<APValue, 16> Elements;
15496 Elements.reserve(N: MT->getNumElementsFlattened());
15497
15498 // The following loop assumes the elements of the matrix InitListExpr are in
15499 // row-major order, which matches the row-major ordering assumption of the
15500 // matrix APValue.
15501 for (unsigned I = 0, N = MT->getNumElementsFlattened(); I < N; ++I) {
15502 if (EltTy->isIntegerType()) {
15503 llvm::APSInt IntVal;
15504 if (!EvaluateInteger(E: E->getInit(Init: I), Result&: IntVal, Info))
15505 return false;
15506 Elements.push_back(Elt: APValue(IntVal));
15507 } else {
15508 llvm::APFloat FloatVal(0.0);
15509 if (!EvaluateFloat(E: E->getInit(Init: I), Result&: FloatVal, Info))
15510 return false;
15511 Elements.push_back(Elt: APValue(FloatVal));
15512 }
15513 }
15514
15515 return Success(M: Elements, E);
15516}
15517
15518//===----------------------------------------------------------------------===//
15519// Array Evaluation
15520//===----------------------------------------------------------------------===//
15521
15522namespace {
15523 class ArrayExprEvaluator
15524 : public ExprEvaluatorBase<ArrayExprEvaluator> {
15525 const LValue &This;
15526 APValue &Result;
15527 public:
15528
15529 ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
15530 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
15531
15532 bool Success(const APValue &V, const Expr *E) {
15533 assert(V.isArray() && "expected array");
15534 Result = V;
15535 return true;
15536 }
15537
15538 bool ZeroInitialization(const Expr *E) {
15539 const ConstantArrayType *CAT =
15540 Info.Ctx.getAsConstantArrayType(T: E->getType());
15541 if (!CAT) {
15542 if (E->getType()->isIncompleteArrayType()) {
15543 // We can be asked to zero-initialize a flexible array member; this
15544 // is represented as an ImplicitValueInitExpr of incomplete array
15545 // type. In this case, the array has zero elements.
15546 Result = APValue(APValue::UninitArray(), 0, 0);
15547 return true;
15548 }
15549 // FIXME: We could handle VLAs here.
15550 return Error(E);
15551 }
15552
15553 Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize());
15554 if (!Result.hasArrayFiller())
15555 return true;
15556
15557 // Zero-initialize all elements.
15558 LValue Subobject = This;
15559 Subobject.addArray(Info, E, CAT);
15560 ImplicitValueInitExpr VIE(CAT->getElementType());
15561 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, E: &VIE);
15562 }
15563
15564 bool VisitCallExpr(const CallExpr *E) {
15565 return handleCallExpr(E, Result, ResultSlot: &This);
15566 }
15567 bool VisitCastExpr(const CastExpr *E);
15568 bool VisitInitListExpr(const InitListExpr *E,
15569 QualType AllocType = QualType());
15570 bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E);
15571 bool VisitCXXConstructExpr(const CXXConstructExpr *E);
15572 bool VisitCXXConstructExpr(const CXXConstructExpr *E,
15573 const LValue &Subobject,
15574 APValue *Value, QualType Type);
15575 bool VisitStringLiteral(const StringLiteral *E,
15576 QualType AllocType = QualType()) {
15577 expandStringLiteral(Info, S: E, Result, AllocType);
15578 return true;
15579 }
15580 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
15581 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
15582 ArrayRef<Expr *> Args,
15583 const Expr *ArrayFiller,
15584 QualType AllocType = QualType());
15585 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
15586 };
15587} // end anonymous namespace
15588
15589static bool EvaluateArray(const Expr *E, const LValue &This,
15590 APValue &Result, EvalInfo &Info) {
15591 assert(!E->isValueDependent());
15592 assert(E->isPRValue() && E->getType()->isArrayType() &&
15593 "not an array prvalue");
15594 return ArrayExprEvaluator(Info, This, Result).Visit(S: E);
15595}
15596
15597static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
15598 APValue &Result, const InitListExpr *ILE,
15599 QualType AllocType) {
15600 assert(!ILE->isValueDependent());
15601 assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
15602 "not an array prvalue");
15603 return ArrayExprEvaluator(Info, This, Result)
15604 .VisitInitListExpr(E: ILE, AllocType);
15605}
15606
15607static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
15608 APValue &Result,
15609 const CXXConstructExpr *CCE,
15610 QualType AllocType) {
15611 assert(!CCE->isValueDependent());
15612 assert(CCE->isPRValue() && CCE->getType()->isArrayType() &&
15613 "not an array prvalue");
15614 return ArrayExprEvaluator(Info, This, Result)
15615 .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType);
15616}
15617
15618// Return true iff the given array filler may depend on the element index.
15619static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) {
15620 // For now, just allow non-class value-initialization and initialization
15621 // lists comprised of them.
15622 if (isa<ImplicitValueInitExpr>(Val: FillerExpr))
15623 return false;
15624 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) {
15625 for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) {
15626 if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I)))
15627 return true;
15628 }
15629
15630 if (ILE->hasArrayFiller() &&
15631 MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller()))
15632 return true;
15633
15634 return false;
15635 }
15636 return true;
15637}
15638
15639bool ArrayExprEvaluator::VisitCastExpr(const CastExpr *E) {
15640 const Expr *SE = E->getSubExpr();
15641
15642 switch (E->getCastKind()) {
15643 default:
15644 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15645 case CK_HLSLAggregateSplatCast: {
15646 APValue Val;
15647 QualType ValTy;
15648
15649 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15650 return false;
15651
15652 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
15653
15654 SmallVector<APValue> SplatEls(NEls, Val);
15655 SmallVector<QualType> SplatType(NEls, ValTy);
15656
15657 // cast the elements
15658 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15659 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
15660 ElTypes&: SplatType))
15661 return false;
15662
15663 return true;
15664 }
15665 case CK_HLSLElementwiseCast: {
15666 SmallVector<APValue> SrcEls;
15667 SmallVector<QualType> SrcTypes;
15668
15669 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals&: SrcEls, SrcTypes))
15670 return false;
15671
15672 // cast the elements
15673 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15674 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
15675 ElTypes&: SrcTypes))
15676 return false;
15677 return true;
15678 }
15679 }
15680}
15681
15682bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E,
15683 QualType AllocType) {
15684 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15685 T: AllocType.isNull() ? E->getType() : AllocType);
15686 if (!CAT)
15687 return Error(E);
15688
15689 // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
15690 // an appropriately-typed string literal enclosed in braces.
15691 if (E->isStringLiteralInit()) {
15692 auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts());
15693 // FIXME: Support ObjCEncodeExpr here once we support it in
15694 // ArrayExprEvaluator generally.
15695 if (!SL)
15696 return Error(E);
15697 return VisitStringLiteral(E: SL, AllocType);
15698 }
15699 // Any other transparent list init will need proper handling of the
15700 // AllocType; we can't just recurse to the inner initializer.
15701 assert(!E->isTransparent() &&
15702 "transparent array list initialization is not string literal init?");
15703
15704 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits(), ArrayFiller: E->getArrayFiller(),
15705 AllocType);
15706}
15707
15708bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
15709 const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller,
15710 QualType AllocType) {
15711 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15712 T: AllocType.isNull() ? ExprToVisit->getType() : AllocType);
15713
15714 bool Success = true;
15715
15716 unsigned NumEltsToInit = Args.size();
15717 unsigned NumElts = CAT->getZExtSize();
15718
15719 // If the initializer might depend on the array index, run it for each
15720 // array element.
15721 if (NumEltsToInit != NumElts &&
15722 MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) {
15723 NumEltsToInit = NumElts;
15724 } else {
15725 // Add additional elements represented by EmbedExpr.
15726 for (auto *Init : Args) {
15727 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts()))
15728 NumEltsToInit += EmbedS->getDataElementCount() - 1;
15729 }
15730 // If we have extra elements in the list, they will be discarded.
15731 if (NumEltsToInit > NumElts)
15732 NumEltsToInit = NumElts;
15733 // If we're overwriting memory which already has an object, make sure we
15734 // don't reduce the number of non-filler elements. (It's possible to
15735 // optimize this in some cases, but the logic gets really complicated.)
15736 if (Result.hasValue() && NumEltsToInit < Result.getArrayInitializedElts())
15737 NumEltsToInit = Result.getArrayInitializedElts();
15738 }
15739
15740 LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
15741 << NumEltsToInit << ".\n");
15742
15743 if (!Result.hasValue()) {
15744 Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15745 } else if (Result.getArrayInitializedElts() != NumEltsToInit) {
15746 // Number of inititalized elts changed. Recreate the APValue, and copy over
15747 // the relevant elements. (This is essentially just fixing the internal
15748 // representation of the value, because it's tied to the number of
15749 // non-filler elements.)
15750 //
15751 // This should be hit rarely, but there are some edge cases:
15752 //
15753 // - The array could be zero-initialized.
15754 // - There could be a DesignatedInitListExpr.
15755 // - operator new[] can be used to start the lifetime early.
15756 APValue NewResult = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15757 // First copy existing elements.
15758 unsigned NumOldElts = Result.getArrayInitializedElts();
15759 for (unsigned I = 0; I < NumOldElts; ++I) {
15760 NewResult.getArrayInitializedElt(I) =
15761 std::move(Result.getArrayInitializedElt(I));
15762 }
15763 // Then copy the array filler over the remaining elements.
15764 for (unsigned I = Result.getArrayInitializedElts(); I < NumEltsToInit; ++I)
15765 NewResult.getArrayInitializedElt(I) = Result.getArrayFiller();
15766 if (NewResult.hasArrayFiller() && Result.hasArrayFiller())
15767 NewResult.getArrayFiller() = Result.getArrayFiller();
15768 Result = std::move(NewResult);
15769 }
15770
15771 LValue Subobject = This;
15772 Subobject.addArray(Info, E: ExprToVisit, CAT);
15773 auto Eval = [&](const Expr *Init, unsigned ArrayIndex) {
15774 if (Init->isValueDependent())
15775 return EvaluateDependentExpr(E: Init, Info);
15776
15777 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
15778 // aren't supposed to be modified.
15779 if (isa<NoInitExpr>(Val: Init))
15780 return true;
15781
15782 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: ArrayIndex), Info,
15783 This: Subobject, E: Init) ||
15784 !HandleLValueArrayAdjustment(Info, E: Init, LVal&: Subobject,
15785 EltTy: CAT->getElementType(), Adjustment: 1)) {
15786 if (!Info.noteFailure())
15787 return false;
15788 Success = false;
15789 }
15790 return true;
15791 };
15792 unsigned ArrayIndex = 0;
15793 QualType DestTy = CAT->getElementType();
15794 APSInt Value(Info.Ctx.getTypeSize(T: DestTy), DestTy->isUnsignedIntegerType());
15795 for (unsigned Index = 0; Index != NumEltsToInit; ++Index) {
15796 const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller;
15797 if (ArrayIndex >= NumEltsToInit)
15798 break;
15799 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) {
15800 StringLiteral *SL = EmbedS->getDataStringLiteral();
15801 for (unsigned I = EmbedS->getStartingElementPos(),
15802 N = EmbedS->getDataElementCount();
15803 I != EmbedS->getStartingElementPos() + N; ++I) {
15804 Value = SL->getCodeUnit(I);
15805 if (DestTy->isIntegerType()) {
15806 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(Value);
15807 } else {
15808 assert(DestTy->isFloatingType() && "unexpected type");
15809 const FPOptions FPO =
15810 Init->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15811 APFloat FValue(0.0);
15812 if (!HandleIntToFloatCast(Info, E: Init, FPO, SrcType: EmbedS->getType(), Value,
15813 DestType: DestTy, Result&: FValue))
15814 return false;
15815 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(FValue);
15816 }
15817 ArrayIndex++;
15818 }
15819 } else {
15820 if (!Eval(Init, ArrayIndex))
15821 return false;
15822 ++ArrayIndex;
15823 }
15824 }
15825
15826 if (!Result.hasArrayFiller())
15827 return Success;
15828
15829 // If we get here, we have a trivial filler, which we can just evaluate
15830 // once and splat over the rest of the array elements.
15831 assert(ArrayFiller && "no array filler for incomplete init list");
15832 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject,
15833 E: ArrayFiller) &&
15834 Success;
15835}
15836
15837bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
15838 LValue CommonLV;
15839 if (E->getCommonExpr() &&
15840 !Evaluate(Result&: Info.CurrentCall->createTemporary(
15841 Key: E->getCommonExpr(),
15842 T: getStorageType(Ctx: Info.Ctx, E: E->getCommonExpr()),
15843 Scope: ScopeKind::FullExpression, LV&: CommonLV),
15844 Info, E: E->getCommonExpr()->getSourceExpr()))
15845 return false;
15846
15847 auto *CAT = cast<ConstantArrayType>(Val: E->getType()->castAsArrayTypeUnsafe());
15848
15849 uint64_t Elements = CAT->getZExtSize();
15850 Result = APValue(APValue::UninitArray(), Elements, Elements);
15851
15852 LValue Subobject = This;
15853 Subobject.addArray(Info, E, CAT);
15854
15855 bool Success = true;
15856 for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) {
15857 // C++ [class.temporary]/5
15858 // There are four contexts in which temporaries are destroyed at a different
15859 // point than the end of the full-expression. [...] The second context is
15860 // when a copy constructor is called to copy an element of an array while
15861 // the entire array is copied [...]. In either case, if the constructor has
15862 // one or more default arguments, the destruction of every temporary created
15863 // in a default argument is sequenced before the construction of the next
15864 // array element, if any.
15865 FullExpressionRAII Scope(Info);
15866
15867 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index),
15868 Info, This: Subobject, E: E->getSubExpr()) ||
15869 !HandleLValueArrayAdjustment(Info, E, LVal&: Subobject,
15870 EltTy: CAT->getElementType(), Adjustment: 1)) {
15871 if (!Info.noteFailure())
15872 return false;
15873 Success = false;
15874 }
15875
15876 // Make sure we run the destructors too.
15877 Scope.destroy();
15878 }
15879
15880 return Success;
15881}
15882
15883bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
15884 return VisitCXXConstructExpr(E, Subobject: This, Value: &Result, Type: E->getType());
15885}
15886
15887bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
15888 const LValue &Subobject,
15889 APValue *Value,
15890 QualType Type) {
15891 bool HadZeroInit = Value->hasValue();
15892
15893 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) {
15894 unsigned FinalSize = CAT->getZExtSize();
15895
15896 // Preserve the array filler if we had prior zero-initialization.
15897 APValue Filler =
15898 HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()
15899 : APValue();
15900
15901 *Value = APValue(APValue::UninitArray(), 0, FinalSize);
15902 if (FinalSize == 0)
15903 return true;
15904
15905 bool HasTrivialConstructor = CheckTrivialDefaultConstructor(
15906 Info, Loc: E->getExprLoc(), CD: E->getConstructor(),
15907 IsValueInitialization: E->requiresZeroInitialization());
15908 LValue ArrayElt = Subobject;
15909 ArrayElt.addArray(Info, E, CAT);
15910 // We do the whole initialization in two passes, first for just one element,
15911 // then for the whole array. It's possible we may find out we can't do const
15912 // init in the first pass, in which case we avoid allocating a potentially
15913 // large array. We don't do more passes because expanding array requires
15914 // copying the data, which is wasteful.
15915 for (const unsigned N : {1u, FinalSize}) {
15916 unsigned OldElts = Value->getArrayInitializedElts();
15917 if (OldElts == N)
15918 break;
15919
15920 // Expand the array to appropriate size.
15921 APValue NewValue(APValue::UninitArray(), N, FinalSize);
15922 for (unsigned I = 0; I < OldElts; ++I)
15923 NewValue.getArrayInitializedElt(I).swap(
15924 RHS&: Value->getArrayInitializedElt(I));
15925 Value->swap(RHS&: NewValue);
15926
15927 if (HadZeroInit)
15928 for (unsigned I = OldElts; I < N; ++I)
15929 Value->getArrayInitializedElt(I) = Filler;
15930
15931 if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) {
15932 // If we have a trivial constructor, only evaluate it once and copy
15933 // the result into all the array elements.
15934 APValue &FirstResult = Value->getArrayInitializedElt(I: 0);
15935 for (unsigned I = OldElts; I < FinalSize; ++I)
15936 Value->getArrayInitializedElt(I) = FirstResult;
15937 } else {
15938 for (unsigned I = OldElts; I < N; ++I) {
15939 if (!VisitCXXConstructExpr(E, Subobject: ArrayElt,
15940 Value: &Value->getArrayInitializedElt(I),
15941 Type: CAT->getElementType()) ||
15942 !HandleLValueArrayAdjustment(Info, E, LVal&: ArrayElt,
15943 EltTy: CAT->getElementType(), Adjustment: 1))
15944 return false;
15945 // When checking for const initilization any diagnostic is considered
15946 // an error.
15947 if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
15948 !Info.keepEvaluatingAfterFailure())
15949 return false;
15950 }
15951 }
15952 }
15953
15954 return true;
15955 }
15956
15957 if (!Type->isRecordType())
15958 return Error(E);
15959
15960 return RecordExprEvaluator(Info, Subobject, *Value)
15961 .VisitCXXConstructExpr(E, T: Type);
15962}
15963
15964bool ArrayExprEvaluator::VisitCXXParenListInitExpr(
15965 const CXXParenListInitExpr *E) {
15966 assert(E->getType()->isConstantArrayType() &&
15967 "Expression result is not a constant array type");
15968
15969 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs(),
15970 ArrayFiller: E->getArrayFiller());
15971}
15972
15973bool ArrayExprEvaluator::VisitDesignatedInitUpdateExpr(
15974 const DesignatedInitUpdateExpr *E) {
15975 if (!Visit(S: E->getBase()))
15976 return false;
15977 return Visit(S: E->getUpdater());
15978}
15979
15980//===----------------------------------------------------------------------===//
15981// Integer Evaluation
15982//
15983// As a GNU extension, we support casting pointers to sufficiently-wide integer
15984// types and back in constant folding. Integer values are thus represented
15985// either as an integer-valued APValue, or as an lvalue-valued APValue.
15986//===----------------------------------------------------------------------===//
15987
15988namespace {
15989class IntExprEvaluator
15990 : public ExprEvaluatorBase<IntExprEvaluator> {
15991 APValue &Result;
15992public:
15993 IntExprEvaluator(EvalInfo &info, APValue &result)
15994 : ExprEvaluatorBaseTy(info), Result(result) {}
15995
15996 bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
15997 assert(E->getType()->isIntegralOrEnumerationType() &&
15998 "Invalid evaluation result.");
15999 assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
16000 "Invalid evaluation result.");
16001 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16002 "Invalid evaluation result.");
16003 Result = APValue(SI);
16004 return true;
16005 }
16006 bool Success(const llvm::APSInt &SI, const Expr *E) {
16007 return Success(SI, E, Result);
16008 }
16009
16010 bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
16011 assert(E->getType()->isIntegralOrEnumerationType() &&
16012 "Invalid evaluation result.");
16013 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16014 "Invalid evaluation result.");
16015 Result = APValue(APSInt(I));
16016 Result.getInt().setIsUnsigned(
16017 E->getType()->isUnsignedIntegerOrEnumerationType());
16018 return true;
16019 }
16020 bool Success(const llvm::APInt &I, const Expr *E) {
16021 return Success(I, E, Result);
16022 }
16023
16024 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
16025 assert(E->getType()->isIntegralOrEnumerationType() &&
16026 "Invalid evaluation result.");
16027 Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType()));
16028 return true;
16029 }
16030 bool Success(uint64_t Value, const Expr *E) {
16031 return Success(Value, E, Result);
16032 }
16033
16034 bool Success(CharUnits Size, const Expr *E) {
16035 return Success(Value: Size.getQuantity(), E);
16036 }
16037
16038 bool Success(const APValue &V, const Expr *E) {
16039 // C++23 [expr.const]p8 If we have a variable that is unknown reference or
16040 // pointer allow further evaluation of the value.
16041 if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate() ||
16042 V.allowConstexprUnknown()) {
16043 Result = V;
16044 return true;
16045 }
16046 return Success(SI: V.getInt(), E);
16047 }
16048
16049 bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); }
16050
16051 friend std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &,
16052 const CallExpr *);
16053
16054 //===--------------------------------------------------------------------===//
16055 // Visitor Methods
16056 //===--------------------------------------------------------------------===//
16057
16058 bool VisitIntegerLiteral(const IntegerLiteral *E) {
16059 return Success(I: E->getValue(), E);
16060 }
16061 bool VisitCharacterLiteral(const CharacterLiteral *E) {
16062 return Success(Value: E->getValue(), E);
16063 }
16064
16065 bool CheckReferencedDecl(const Expr *E, const Decl *D);
16066 bool VisitDeclRefExpr(const DeclRefExpr *E) {
16067 if (CheckReferencedDecl(E, D: E->getDecl()))
16068 return true;
16069
16070 return ExprEvaluatorBaseTy::VisitDeclRefExpr(S: E);
16071 }
16072 bool VisitMemberExpr(const MemberExpr *E) {
16073 if (CheckReferencedDecl(E, D: E->getMemberDecl())) {
16074 VisitIgnoredBaseExpression(E: E->getBase());
16075 return true;
16076 }
16077
16078 return ExprEvaluatorBaseTy::VisitMemberExpr(E);
16079 }
16080
16081 bool VisitCallExpr(const CallExpr *E);
16082 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
16083 bool VisitBinaryOperator(const BinaryOperator *E);
16084 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
16085 bool VisitUnaryOperator(const UnaryOperator *E);
16086
16087 bool VisitCastExpr(const CastExpr* E);
16088 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
16089
16090 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
16091 return Success(Value: E->getValue(), E);
16092 }
16093
16094 bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
16095 return Success(Value: E->getValue(), E);
16096 }
16097
16098 bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
16099 if (Info.ArrayInitIndex == uint64_t(-1)) {
16100 // We were asked to evaluate this subexpression independent of the
16101 // enclosing ArrayInitLoopExpr. We can't do that.
16102 Info.FFDiag(E);
16103 return false;
16104 }
16105 return Success(Value: Info.ArrayInitIndex, E);
16106 }
16107
16108 // Note, GNU defines __null as an integer, not a pointer.
16109 bool VisitGNUNullExpr(const GNUNullExpr *E) {
16110 return ZeroInitialization(E);
16111 }
16112
16113 bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
16114 if (E->isStoredAsBoolean())
16115 return Success(Value: E->getBoolValue(), E);
16116 if (E->getAPValue().isAbsent())
16117 return false;
16118 assert(E->getAPValue().isInt() && "APValue type not supported");
16119 return Success(SI: E->getAPValue().getInt(), E);
16120 }
16121
16122 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
16123 return Success(Value: E->getValue(), E);
16124 }
16125
16126 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
16127 return Success(Value: E->getValue(), E);
16128 }
16129
16130 bool VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *E) {
16131 // This should not be evaluated during constant expr evaluation, as it
16132 // should always be in an unevaluated context (the args list of a 'gang' or
16133 // 'tile' clause).
16134 return Error(E);
16135 }
16136
16137 bool VisitUnaryReal(const UnaryOperator *E);
16138 bool VisitUnaryImag(const UnaryOperator *E);
16139
16140 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
16141 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
16142 bool VisitSourceLocExpr(const SourceLocExpr *E);
16143 bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
16144 bool VisitRequiresExpr(const RequiresExpr *E);
16145 // FIXME: Missing: array subscript of vector, member of vector
16146};
16147
16148class FixedPointExprEvaluator
16149 : public ExprEvaluatorBase<FixedPointExprEvaluator> {
16150 APValue &Result;
16151
16152 public:
16153 FixedPointExprEvaluator(EvalInfo &info, APValue &result)
16154 : ExprEvaluatorBaseTy(info), Result(result) {}
16155
16156 bool Success(const llvm::APInt &I, const Expr *E) {
16157 return Success(
16158 V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16159 }
16160
16161 bool Success(uint64_t Value, const Expr *E) {
16162 return Success(
16163 V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
16164 }
16165
16166 bool Success(const APValue &V, const Expr *E) {
16167 return Success(V: V.getFixedPoint(), E);
16168 }
16169
16170 bool Success(const APFixedPoint &V, const Expr *E) {
16171 assert(E->getType()->isFixedPointType() && "Invalid evaluation result.");
16172 assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) &&
16173 "Invalid evaluation result.");
16174 Result = APValue(V);
16175 return true;
16176 }
16177
16178 bool ZeroInitialization(const Expr *E) {
16179 return Success(Value: 0, E);
16180 }
16181
16182 //===--------------------------------------------------------------------===//
16183 // Visitor Methods
16184 //===--------------------------------------------------------------------===//
16185
16186 bool VisitFixedPointLiteral(const FixedPointLiteral *E) {
16187 return Success(I: E->getValue(), E);
16188 }
16189
16190 bool VisitCastExpr(const CastExpr *E);
16191 bool VisitUnaryOperator(const UnaryOperator *E);
16192 bool VisitBinaryOperator(const BinaryOperator *E);
16193};
16194} // end anonymous namespace
16195
16196/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
16197/// produce either the integer value or a pointer.
16198///
16199/// GCC has a heinous extension which folds casts between pointer types and
16200/// pointer-sized integral types. We support this by allowing the evaluation of
16201/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
16202/// Some simple arithmetic on such values is supported (they are treated much
16203/// like char*).
16204static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
16205 EvalInfo &Info) {
16206 assert(!E->isValueDependent());
16207 assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType());
16208 return IntExprEvaluator(Info, Result).Visit(S: E);
16209}
16210
16211static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
16212 assert(!E->isValueDependent());
16213 APValue Val;
16214 if (!EvaluateIntegerOrLValue(E, Result&: Val, Info))
16215 return false;
16216 if (!Val.isInt()) {
16217 // FIXME: It would be better to produce the diagnostic for casting
16218 // a pointer to an integer.
16219 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
16220 return false;
16221 }
16222 Result = Val.getInt();
16223 return true;
16224}
16225
16226bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) {
16227 APValue Evaluated = E->EvaluateInContext(
16228 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
16229 return Success(V: Evaluated, E);
16230}
16231
16232static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
16233 EvalInfo &Info) {
16234 assert(!E->isValueDependent());
16235 if (E->getType()->isFixedPointType()) {
16236 APValue Val;
16237 if (!FixedPointExprEvaluator(Info, Val).Visit(S: E))
16238 return false;
16239 if (!Val.isFixedPoint())
16240 return false;
16241
16242 Result = Val.getFixedPoint();
16243 return true;
16244 }
16245 return false;
16246}
16247
16248static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
16249 EvalInfo &Info) {
16250 assert(!E->isValueDependent());
16251 if (E->getType()->isIntegerType()) {
16252 auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType());
16253 APSInt Val;
16254 if (!EvaluateInteger(E, Result&: Val, Info))
16255 return false;
16256 Result = APFixedPoint(Val, FXSema);
16257 return true;
16258 } else if (E->getType()->isFixedPointType()) {
16259 return EvaluateFixedPoint(E, Result, Info);
16260 }
16261 return false;
16262}
16263
16264/// Check whether the given declaration can be directly converted to an integral
16265/// rvalue. If not, no diagnostic is produced; there are other things we can
16266/// try.
16267bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
16268 // Enums are integer constant exprs.
16269 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) {
16270 // Check for signedness/width mismatches between E type and ECD value.
16271 bool SameSign = (ECD->getInitVal().isSigned()
16272 == E->getType()->isSignedIntegerOrEnumerationType());
16273 bool SameWidth = (ECD->getInitVal().getBitWidth()
16274 == Info.Ctx.getIntWidth(T: E->getType()));
16275 if (SameSign && SameWidth)
16276 return Success(SI: ECD->getInitVal(), E);
16277 else {
16278 // Get rid of mismatch (otherwise Success assertions will fail)
16279 // by computing a new value matching the type of E.
16280 llvm::APSInt Val = ECD->getInitVal();
16281 if (!SameSign)
16282 Val.setIsSigned(!ECD->getInitVal().isSigned());
16283 if (!SameWidth)
16284 Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType()));
16285 return Success(SI: Val, E);
16286 }
16287 }
16288 return false;
16289}
16290
16291/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16292/// as GCC.
16293GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
16294 const LangOptions &LangOpts) {
16295 assert(!T->isDependentType() && "unexpected dependent type");
16296
16297 QualType CanTy = T.getCanonicalType();
16298
16299 switch (CanTy->getTypeClass()) {
16300#define TYPE(ID, BASE)
16301#define DEPENDENT_TYPE(ID, BASE) case Type::ID:
16302#define NON_CANONICAL_TYPE(ID, BASE) case Type::ID:
16303#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID:
16304#include "clang/AST/TypeNodes.inc"
16305 case Type::Auto:
16306 case Type::DeducedTemplateSpecialization:
16307 llvm_unreachable("unexpected non-canonical or dependent type");
16308
16309 case Type::Builtin:
16310 switch (cast<BuiltinType>(Val&: CanTy)->getKind()) {
16311#define BUILTIN_TYPE(ID, SINGLETON_ID)
16312#define SIGNED_TYPE(ID, SINGLETON_ID) \
16313 case BuiltinType::ID: return GCCTypeClass::Integer;
16314#define FLOATING_TYPE(ID, SINGLETON_ID) \
16315 case BuiltinType::ID: return GCCTypeClass::RealFloat;
16316#define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \
16317 case BuiltinType::ID: break;
16318#include "clang/AST/BuiltinTypes.def"
16319 case BuiltinType::Void:
16320 return GCCTypeClass::Void;
16321
16322 case BuiltinType::Bool:
16323 return GCCTypeClass::Bool;
16324
16325 case BuiltinType::Char_U:
16326 case BuiltinType::UChar:
16327 case BuiltinType::WChar_U:
16328 case BuiltinType::Char8:
16329 case BuiltinType::Char16:
16330 case BuiltinType::Char32:
16331 case BuiltinType::UShort:
16332 case BuiltinType::UInt:
16333 case BuiltinType::ULong:
16334 case BuiltinType::ULongLong:
16335 case BuiltinType::UInt128:
16336 return GCCTypeClass::Integer;
16337
16338 case BuiltinType::UShortAccum:
16339 case BuiltinType::UAccum:
16340 case BuiltinType::ULongAccum:
16341 case BuiltinType::UShortFract:
16342 case BuiltinType::UFract:
16343 case BuiltinType::ULongFract:
16344 case BuiltinType::SatUShortAccum:
16345 case BuiltinType::SatUAccum:
16346 case BuiltinType::SatULongAccum:
16347 case BuiltinType::SatUShortFract:
16348 case BuiltinType::SatUFract:
16349 case BuiltinType::SatULongFract:
16350 return GCCTypeClass::None;
16351
16352 case BuiltinType::NullPtr:
16353
16354 case BuiltinType::ObjCId:
16355 case BuiltinType::ObjCClass:
16356 case BuiltinType::ObjCSel:
16357#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
16358 case BuiltinType::Id:
16359#include "clang/Basic/OpenCLImageTypes.def"
16360#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
16361 case BuiltinType::Id:
16362#include "clang/Basic/OpenCLExtensionTypes.def"
16363 case BuiltinType::OCLSampler:
16364 case BuiltinType::OCLEvent:
16365 case BuiltinType::OCLClkEvent:
16366 case BuiltinType::OCLQueue:
16367 case BuiltinType::OCLReserveID:
16368#define SVE_TYPE(Name, Id, SingletonId) \
16369 case BuiltinType::Id:
16370#include "clang/Basic/AArch64ACLETypes.def"
16371#define PPC_VECTOR_TYPE(Name, Id, Size) \
16372 case BuiltinType::Id:
16373#include "clang/Basic/PPCTypes.def"
16374#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16375#include "clang/Basic/RISCVVTypes.def"
16376#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16377#include "clang/Basic/WebAssemblyReferenceTypes.def"
16378#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
16379#include "clang/Basic/AMDGPUTypes.def"
16380#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16381#include "clang/Basic/HLSLIntangibleTypes.def"
16382#define SPIRV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
16383#include "clang/Basic/SPIRVTypes.def"
16384 return GCCTypeClass::None;
16385
16386 case BuiltinType::Dependent:
16387 llvm_unreachable("unexpected dependent type");
16388 };
16389 llvm_unreachable("unexpected placeholder type");
16390
16391 case Type::Enum:
16392 return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer;
16393
16394 case Type::Pointer:
16395 case Type::ConstantArray:
16396 case Type::VariableArray:
16397 case Type::IncompleteArray:
16398 case Type::FunctionNoProto:
16399 case Type::FunctionProto:
16400 case Type::ArrayParameter:
16401 return GCCTypeClass::Pointer;
16402
16403 case Type::MemberPointer:
16404 return CanTy->isMemberDataPointerType()
16405 ? GCCTypeClass::PointerToDataMember
16406 : GCCTypeClass::PointerToMemberFunction;
16407
16408 case Type::Complex:
16409 return GCCTypeClass::Complex;
16410
16411 case Type::Record:
16412 return CanTy->isUnionType() ? GCCTypeClass::Union
16413 : GCCTypeClass::ClassOrStruct;
16414
16415 case Type::Atomic:
16416 // GCC classifies _Atomic T the same as T.
16417 return EvaluateBuiltinClassifyType(
16418 T: CanTy->castAs<AtomicType>()->getValueType(), LangOpts);
16419
16420 case Type::Vector:
16421 case Type::ExtVector:
16422 return GCCTypeClass::Vector;
16423
16424 case Type::BlockPointer:
16425 case Type::ConstantMatrix:
16426 case Type::ObjCObject:
16427 case Type::ObjCInterface:
16428 case Type::ObjCObjectPointer:
16429 case Type::Pipe:
16430 case Type::HLSLAttributedResource:
16431 case Type::HLSLInlineSpirv:
16432 case Type::OverflowBehavior:
16433 // Classify all other types that don't fit into the regular
16434 // classification the same way.
16435 return GCCTypeClass::None;
16436
16437 case Type::BitInt:
16438 return GCCTypeClass::BitInt;
16439
16440 case Type::LValueReference:
16441 case Type::RValueReference:
16442 llvm_unreachable("invalid type for expression");
16443 }
16444
16445 llvm_unreachable("unexpected type class");
16446}
16447
16448/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16449/// as GCC.
16450static GCCTypeClass
16451EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) {
16452 // If no argument was supplied, default to None. This isn't
16453 // ideal, however it is what gcc does.
16454 if (E->getNumArgs() == 0)
16455 return GCCTypeClass::None;
16456
16457 // FIXME: Bizarrely, GCC treats a call with more than one argument as not
16458 // being an ICE, but still folds it to a constant using the type of the first
16459 // argument.
16460 return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts);
16461}
16462
16463/// EvaluateBuiltinConstantPForLValue - Determine the result of
16464/// __builtin_constant_p when applied to the given pointer.
16465///
16466/// A pointer is only "constant" if it is null (or a pointer cast to integer)
16467/// or it points to the first character of a string literal.
16468static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) {
16469 APValue::LValueBase Base = LV.getLValueBase();
16470 if (Base.isNull()) {
16471 // A null base is acceptable.
16472 return true;
16473 } else if (const Expr *E = Base.dyn_cast<const Expr *>()) {
16474 if (!isa<StringLiteral>(Val: E))
16475 return false;
16476 return LV.getLValueOffset().isZero();
16477 } else if (Base.is<TypeInfoLValue>()) {
16478 // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to
16479 // evaluate to true.
16480 return true;
16481 } else {
16482 // Any other base is not constant enough for GCC.
16483 return false;
16484 }
16485}
16486
16487/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
16488/// GCC as we can manage.
16489static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) {
16490 // This evaluation is not permitted to have side-effects, so evaluate it in
16491 // a speculative evaluation context.
16492 SpeculativeEvaluationRAII SpeculativeEval(Info);
16493
16494 // Constant-folding is always enabled for the operand of __builtin_constant_p
16495 // (even when the enclosing evaluation context otherwise requires a strict
16496 // language-specific constant expression).
16497 FoldConstant Fold(Info, true);
16498
16499 QualType ArgType = Arg->getType();
16500
16501 // __builtin_constant_p always has one operand. The rules which gcc follows
16502 // are not precisely documented, but are as follows:
16503 //
16504 // - If the operand is of integral, floating, complex or enumeration type,
16505 // and can be folded to a known value of that type, it returns 1.
16506 // - If the operand can be folded to a pointer to the first character
16507 // of a string literal (or such a pointer cast to an integral type)
16508 // or to a null pointer or an integer cast to a pointer, it returns 1.
16509 //
16510 // Otherwise, it returns 0.
16511 //
16512 // FIXME: GCC also intends to return 1 for literals of aggregate types, but
16513 // its support for this did not work prior to GCC 9 and is not yet well
16514 // understood.
16515 if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||
16516 ArgType->isAnyComplexType() || ArgType->isPointerType() ||
16517 ArgType->isNullPtrType()) {
16518 APValue V;
16519 if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) {
16520 Fold.keepDiagnostics();
16521 return false;
16522 }
16523
16524 // For a pointer (possibly cast to integer), there are special rules.
16525 if (V.getKind() == APValue::LValue)
16526 return EvaluateBuiltinConstantPForLValue(LV: V);
16527
16528 // Otherwise, any constant value is good enough.
16529 return V.hasValue();
16530 }
16531
16532 // Anything else isn't considered to be sufficiently constant.
16533 return false;
16534}
16535
16536/// Retrieves the "underlying object type" of the given expression,
16537/// as used by __builtin_object_size.
16538static QualType getObjectType(APValue::LValueBase B) {
16539 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
16540 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
16541 return VD->getType();
16542 } else if (const Expr *E = B.dyn_cast<const Expr*>()) {
16543 if (isa<CompoundLiteralExpr>(Val: E))
16544 return E->getType();
16545 } else if (B.is<TypeInfoLValue>()) {
16546 return B.getTypeInfoType();
16547 } else if (B.is<DynamicAllocLValue>()) {
16548 return B.getDynamicAllocType();
16549 }
16550
16551 return QualType();
16552}
16553
16554/// A more selective version of E->IgnoreParenCasts for
16555/// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only
16556/// to change the type of E.
16557/// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo`
16558///
16559/// Always returns an RValue with a pointer representation.
16560static const Expr *ignorePointerCastsAndParens(const Expr *E) {
16561 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
16562
16563 const Expr *NoParens = E->IgnoreParens();
16564 const auto *Cast = dyn_cast<CastExpr>(Val: NoParens);
16565 if (Cast == nullptr)
16566 return NoParens;
16567
16568 // We only conservatively allow a few kinds of casts, because this code is
16569 // inherently a simple solution that seeks to support the common case.
16570 auto CastKind = Cast->getCastKind();
16571 if (CastKind != CK_NoOp && CastKind != CK_BitCast &&
16572 CastKind != CK_AddressSpaceConversion)
16573 return NoParens;
16574
16575 const auto *SubExpr = Cast->getSubExpr();
16576 if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue())
16577 return NoParens;
16578 return ignorePointerCastsAndParens(E: SubExpr);
16579}
16580
16581/// Checks to see if the given LValue's Designator is at the end of the LValue's
16582/// record layout. e.g.
16583/// struct { struct { int a, b; } fst, snd; } obj;
16584/// obj.fst // no
16585/// obj.snd // yes
16586/// obj.fst.a // no
16587/// obj.fst.b // no
16588/// obj.snd.a // no
16589/// obj.snd.b // yes
16590///
16591/// Please note: this function is specialized for how __builtin_object_size
16592/// views "objects".
16593///
16594/// If this encounters an invalid RecordDecl or otherwise cannot determine the
16595/// correct result, it will always return true.
16596static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) {
16597 assert(!LVal.Designator.Invalid);
16598
16599 auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD) {
16600 const RecordDecl *Parent = FD->getParent();
16601 if (Parent->isInvalidDecl() || Parent->isUnion())
16602 return true;
16603 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent);
16604 return FD->getFieldIndex() + 1 == Layout.getFieldCount();
16605 };
16606
16607 auto &Base = LVal.getLValueBase();
16608 if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) {
16609 if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) {
16610 if (!IsLastOrInvalidFieldDecl(FD))
16611 return false;
16612 } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) {
16613 for (auto *FD : IFD->chain()) {
16614 if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD)))
16615 return false;
16616 }
16617 }
16618 }
16619
16620 unsigned I = 0;
16621 QualType BaseType = getType(B: Base);
16622 if (LVal.Designator.FirstEntryIsAnUnsizedArray) {
16623 // If we don't know the array bound, conservatively assume we're looking at
16624 // the final array element.
16625 ++I;
16626 if (BaseType->isIncompleteArrayType())
16627 BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType();
16628 else
16629 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
16630 }
16631
16632 for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) {
16633 const auto &Entry = LVal.Designator.Entries[I];
16634 if (BaseType->isArrayType()) {
16635 // Because __builtin_object_size treats arrays as objects, we can ignore
16636 // the index iff this is the last array in the Designator.
16637 if (I + 1 == E)
16638 return true;
16639 const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType));
16640 uint64_t Index = Entry.getAsArrayIndex();
16641 if (Index + 1 != CAT->getZExtSize())
16642 return false;
16643 BaseType = CAT->getElementType();
16644 } else if (BaseType->isAnyComplexType()) {
16645 const auto *CT = BaseType->castAs<ComplexType>();
16646 uint64_t Index = Entry.getAsArrayIndex();
16647 if (Index != 1)
16648 return false;
16649 BaseType = CT->getElementType();
16650 } else if (auto *FD = getAsField(E: Entry)) {
16651 if (!IsLastOrInvalidFieldDecl(FD))
16652 return false;
16653 BaseType = FD->getType();
16654 } else {
16655 assert(getAsBaseClass(Entry) && "Expecting cast to a base class");
16656 return false;
16657 }
16658 }
16659 return true;
16660}
16661
16662/// Tests to see if the LValue has a user-specified designator (that isn't
16663/// necessarily valid). Note that this always returns 'true' if the LValue has
16664/// an unsized array as its first designator entry, because there's currently no
16665/// way to tell if the user typed *foo or foo[0].
16666static bool refersToCompleteObject(const LValue &LVal) {
16667 if (LVal.Designator.Invalid)
16668 return false;
16669
16670 if (!LVal.Designator.Entries.empty())
16671 return LVal.Designator.isMostDerivedAnUnsizedArray();
16672
16673 if (!LVal.InvalidBase)
16674 return true;
16675
16676 // If `E` is a MemberExpr, then the first part of the designator is hiding in
16677 // the LValueBase.
16678 const auto *E = LVal.Base.dyn_cast<const Expr *>();
16679 return !E || !isa<MemberExpr>(Val: E);
16680}
16681
16682/// Attempts to detect a user writing into a piece of memory that's impossible
16683/// to figure out the size of by just using types.
16684static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {
16685 const SubobjectDesignator &Designator = LVal.Designator;
16686 // Notes:
16687 // - Users can only write off of the end when we have an invalid base. Invalid
16688 // bases imply we don't know where the memory came from.
16689 // - We used to be a bit more aggressive here; we'd only be conservative if
16690 // the array at the end was flexible, or if it had 0 or 1 elements. This
16691 // broke some common standard library extensions (PR30346), but was
16692 // otherwise seemingly fine. It may be useful to reintroduce this behavior
16693 // with some sort of list. OTOH, it seems that GCC is always
16694 // conservative with the last element in structs (if it's an array), so our
16695 // current behavior is more compatible than an explicit list approach would
16696 // be.
16697 auto isFlexibleArrayMember = [&] {
16698 using FAMKind = LangOptions::StrictFlexArraysLevelKind;
16699 FAMKind StrictFlexArraysLevel =
16700 Ctx.getLangOpts().getStrictFlexArraysLevel();
16701
16702 if (Designator.isMostDerivedAnUnsizedArray())
16703 return true;
16704
16705 if (StrictFlexArraysLevel == FAMKind::Default)
16706 return true;
16707
16708 if (Designator.getMostDerivedArraySize() == 0 &&
16709 StrictFlexArraysLevel != FAMKind::IncompleteOnly)
16710 return true;
16711
16712 if (Designator.getMostDerivedArraySize() == 1 &&
16713 StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete)
16714 return true;
16715
16716 return false;
16717 };
16718
16719 return LVal.InvalidBase &&
16720 Designator.Entries.size() == Designator.MostDerivedPathLength &&
16721 Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() &&
16722 isDesignatorAtObjectEnd(Ctx, LVal);
16723}
16724
16725/// Converts the given APInt to CharUnits, assuming the APInt is unsigned.
16726/// Fails if the conversion would cause loss of precision.
16727static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int,
16728 CharUnits &Result) {
16729 auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max();
16730 if (Int.ugt(RHS: CharUnitsMax))
16731 return false;
16732 Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue());
16733 return true;
16734}
16735
16736/// If we're evaluating the object size of an instance of a struct that
16737/// contains a flexible array member, add the size of the initializer.
16738static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T,
16739 const LValue &LV, CharUnits &Size) {
16740 if (!T.isNull() && T->isStructureType() &&
16741 T->castAsRecordDecl()->hasFlexibleArrayMember())
16742 if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>())
16743 if (const auto *VD = dyn_cast<VarDecl>(Val: V))
16744 if (VD->hasInit())
16745 Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx);
16746}
16747
16748/// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will
16749/// determine how many bytes exist from the beginning of the object to either
16750/// the end of the current subobject, or the end of the object itself, depending
16751/// on what the LValue looks like + the value of Type.
16752///
16753/// If this returns false, the value of Result is undefined.
16754static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,
16755 unsigned Type, const LValue &LVal,
16756 CharUnits &EndOffset) {
16757 bool DetermineForCompleteObject = refersToCompleteObject(LVal);
16758
16759 auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) {
16760 if (Ty.isNull())
16761 return false;
16762
16763 Ty = Ty.getNonReferenceType();
16764
16765 if (Ty->isIncompleteType() || Ty->isFunctionType())
16766 return false;
16767
16768 return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result);
16769 };
16770
16771 // We want to evaluate the size of the entire object. This is a valid fallback
16772 // for when Type=1 and the designator is invalid, because we're asked for an
16773 // upper-bound.
16774 if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) {
16775 // Type=3 wants a lower bound, so we can't fall back to this.
16776 if (Type == 3 && !DetermineForCompleteObject)
16777 return false;
16778
16779 llvm::APInt APEndOffset;
16780 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16781 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16782 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16783
16784 if (LVal.InvalidBase)
16785 return false;
16786
16787 QualType BaseTy = getObjectType(B: LVal.getLValueBase());
16788 const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset);
16789 addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset);
16790 return Ret;
16791 }
16792
16793 // We want to evaluate the size of a subobject.
16794 const SubobjectDesignator &Designator = LVal.Designator;
16795
16796 // The following is a moderately common idiom in C:
16797 //
16798 // struct Foo { int a; char c[1]; };
16799 // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar));
16800 // strcpy(&F->c[0], Bar);
16801 //
16802 // In order to not break too much legacy code, we need to support it.
16803 if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) {
16804 // If we can resolve this to an alloc_size call, we can hand that back,
16805 // because we know for certain how many bytes there are to write to.
16806 llvm::APInt APEndOffset;
16807 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16808 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16809 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16810
16811 // If we cannot determine the size of the initial allocation, then we can't
16812 // given an accurate upper-bound. However, we are still able to give
16813 // conservative lower-bounds for Type=3.
16814 if (Type == 1)
16815 return false;
16816 }
16817
16818 CharUnits BytesPerElem;
16819 if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem))
16820 return false;
16821
16822 // According to the GCC documentation, we want the size of the subobject
16823 // denoted by the pointer. But that's not quite right -- what we actually
16824 // want is the size of the immediately-enclosing array, if there is one.
16825 int64_t ElemsRemaining;
16826 if (Designator.MostDerivedIsArrayElement &&
16827 Designator.Entries.size() == Designator.MostDerivedPathLength) {
16828 uint64_t ArraySize = Designator.getMostDerivedArraySize();
16829 uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex();
16830 ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex;
16831 } else {
16832 ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1;
16833 }
16834
16835 EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining;
16836 return true;
16837}
16838
16839/// Tries to evaluate the __builtin_object_size for @p E.
16840///
16841/// If @p IsDynamic is true (i.e. we're evaluating
16842/// __builtin_dynamic_object_size) and the operand designates a flexible array
16843/// member annotated with 'counted_by', we refuse to fold so that IR generation
16844/// can emit the count-based runtime size computation.
16845static std::optional<uint64_t>
16846tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, EvalInfo &Info,
16847 bool IsDynamic = false) {
16848
16849 // Determine the denoted object.
16850 LValue LVal;
16851 {
16852 // The operand of __builtin_object_size is never evaluated for side-effects.
16853 // If there are any, but we can determine the pointed-to object anyway, then
16854 // ignore the side-effects.
16855 SpeculativeEvaluationRAII SpeculativeEval(Info);
16856 IgnoreSideEffectsRAII Fold(Info);
16857
16858 if (E->isGLValue()) {
16859 // It's possible for us to be given GLValues if we're called via
16860 // Expr::tryEvaluateObjectSize.
16861 APValue RVal;
16862 if (!EvaluateAsRValue(Info, E, Result&: RVal))
16863 return std::nullopt;
16864 LVal.setFrom(Ctx: Info.Ctx, V: RVal);
16865 } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info,
16866 /*InvalidBaseOK=*/true))
16867 return std::nullopt;
16868 }
16869
16870 // If we point to before the start of the object, there are no accessible
16871 // bytes.
16872 if (LVal.getLValueOffset().isNegative())
16873 return 0;
16874
16875 // For __builtin_dynamic_object_size on a counted_by-annotated flexible
16876 // array member, defer to IR generation (emitCountedBySize in CGBuiltin):
16877 // its runtime computation uses the live 'count' field and is more accurate
16878 // than the layout/initializer-derived size we'd produce here. Use the same
16879 // findStructFieldAccess form-recognition CGBuiltin does, so we refuse to
16880 // fold on exactly the shapes that path handles (and, importantly, *not*
16881 // on '&af.fam' which designates the array-as-a-whole and stays on the
16882 // layout-derived path to match GCC). Checked after the negative-offset
16883 // early return above so that obviously out-of-bounds operands still fold
16884 // to 0, preserving existing behavior.
16885 if (IsDynamic) {
16886 const auto *ME = dyn_cast_or_null<MemberExpr>(Val: findStructFieldAccess(E));
16887 const auto *FD = ME ? dyn_cast<FieldDecl>(Val: ME->getMemberDecl()) : nullptr;
16888 if (FD && FD->getType()->isCountAttributedType())
16889 return std::nullopt;
16890 }
16891
16892 CharUnits EndOffset;
16893 if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset))
16894 return std::nullopt;
16895
16896 // If we've fallen outside of the end offset, just pretend there's nothing to
16897 // write to/read from.
16898 if (EndOffset <= LVal.getLValueOffset())
16899 return 0;
16900 return (EndOffset - LVal.getLValueOffset()).getQuantity();
16901}
16902
16903bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
16904 if (!IsConstantEvaluatedBuiltinCall(E))
16905 return ExprEvaluatorBaseTy::VisitCallExpr(E);
16906 return VisitBuiltinCallExpr(E, BuiltinOp: ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E));
16907}
16908
16909static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info,
16910 APValue &Val, APSInt &Alignment) {
16911 QualType SrcTy = E->getArg(Arg: 0)->getType();
16912 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment))
16913 return false;
16914 // Even though we are evaluating integer expressions we could get a pointer
16915 // argument for the __builtin_is_aligned() case.
16916 if (SrcTy->isPointerType()) {
16917 LValue Ptr;
16918 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info))
16919 return false;
16920 Ptr.moveInto(V&: Val);
16921 } else if (!SrcTy->isIntegralOrEnumerationType()) {
16922 Info.FFDiag(E: E->getArg(Arg: 0));
16923 return false;
16924 } else {
16925 APSInt SrcInt;
16926 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info))
16927 return false;
16928 assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() &&
16929 "Bit widths must be the same");
16930 Val = APValue(SrcInt);
16931 }
16932 assert(Val.hasValue());
16933 return true;
16934}
16935
16936bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
16937 unsigned BuiltinOp) {
16938 auto EvalTestOp = [&](llvm::function_ref<bool(const APInt &, const APInt &)>
16939 Fn) {
16940 APValue SourceLHS, SourceRHS;
16941 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
16942 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
16943 return false;
16944
16945 unsigned SourceLen = SourceLHS.getVectorLength();
16946 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
16947 QualType ElemQT = VT->getElementType();
16948 unsigned LaneWidth = Info.Ctx.getTypeSize(T: ElemQT);
16949
16950 APInt AWide(LaneWidth * SourceLen, 0);
16951 APInt BWide(LaneWidth * SourceLen, 0);
16952
16953 for (unsigned I = 0; I != SourceLen; ++I) {
16954 APInt ALane;
16955 APInt BLane;
16956 if (ElemQT->isIntegerType()) { // Get value.
16957 ALane = SourceLHS.getVectorElt(I).getInt();
16958 BLane = SourceRHS.getVectorElt(I).getInt();
16959 } else if (ElemQT->isFloatingType()) { // Get only sign bit.
16960 ALane =
16961 SourceLHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
16962 BLane =
16963 SourceRHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
16964 } else { // Must be integer or floating type.
16965 return false;
16966 }
16967 AWide.insertBits(SubBits: ALane, bitPosition: I * LaneWidth);
16968 BWide.insertBits(SubBits: BLane, bitPosition: I * LaneWidth);
16969 }
16970 return Success(Value: Fn(AWide, BWide), E);
16971 };
16972
16973 auto HandleMaskBinOp =
16974 [&](llvm::function_ref<APSInt(const APSInt &, const APSInt &)> Fn)
16975 -> bool {
16976 APValue LHS, RHS;
16977 if (!Evaluate(Result&: LHS, Info, E: E->getArg(Arg: 0)) ||
16978 !Evaluate(Result&: RHS, Info, E: E->getArg(Arg: 1)))
16979 return false;
16980
16981 APSInt ResultInt = Fn(LHS.getInt(), RHS.getInt());
16982
16983 return Success(V: APValue(ResultInt), E);
16984 };
16985
16986 auto HandleCRC32 = [&](unsigned DataBytes) -> bool {
16987 APSInt CRC, Data;
16988 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CRC, Info) ||
16989 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Data, Info))
16990 return false;
16991
16992 uint64_t CRCVal = CRC.getZExtValue();
16993 uint64_t DataVal = Data.getZExtValue();
16994
16995 // CRC32C polynomial (iSCSI polynomial, bit-reversed)
16996 static const uint32_t CRC32C_POLY = 0x82F63B78;
16997
16998 // Process each byte
16999 uint32_t Result = static_cast<uint32_t>(CRCVal);
17000 for (unsigned I = 0; I != DataBytes; ++I) {
17001 uint8_t Byte = static_cast<uint8_t>((DataVal >> (I * 8)) & 0xFF);
17002 Result ^= Byte;
17003 for (int J = 0; J != 8; ++J) {
17004 Result = (Result >> 1) ^ ((Result & 1) ? CRC32C_POLY : 0);
17005 }
17006 }
17007
17008 return Success(Value: Result, E);
17009 };
17010
17011 switch (BuiltinOp) {
17012 default:
17013 return false;
17014
17015 case X86::BI__builtin_ia32_crc32qi:
17016 return HandleCRC32(1);
17017 case X86::BI__builtin_ia32_crc32hi:
17018 return HandleCRC32(2);
17019 case X86::BI__builtin_ia32_crc32si:
17020 return HandleCRC32(4);
17021 case X86::BI__builtin_ia32_crc32di:
17022 return HandleCRC32(8);
17023
17024 case Builtin::BI__builtin_dynamic_object_size:
17025 case Builtin::BI__builtin_object_size: {
17026 // The type was checked when we built the expression.
17027 unsigned Type =
17028 E->getArg(Arg: 1)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
17029 assert(Type <= 3 && "unexpected type");
17030
17031 bool IsDynamic = BuiltinOp == Builtin::BI__builtin_dynamic_object_size;
17032 if (std::optional<uint64_t> Size =
17033 tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, IsDynamic))
17034 return Success(Value: *Size, E);
17035
17036 if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx))
17037 return Success(Value: (Type & 2) ? 0 : -1, E);
17038
17039 // Expression had no side effects, but we couldn't statically determine the
17040 // size of the referenced object.
17041 switch (Info.EvalMode) {
17042 case EvaluationMode::ConstantExpression:
17043 case EvaluationMode::ConstantFold:
17044 case EvaluationMode::IgnoreSideEffects:
17045 // Leave it to IR generation.
17046 return Error(E);
17047 case EvaluationMode::ConstantExpressionUnevaluated:
17048 // Reduce it to a constant now.
17049 return Success(Value: (Type & 2) ? 0 : -1, E);
17050 }
17051
17052 llvm_unreachable("unexpected EvalMode");
17053 }
17054
17055 case Builtin::BI__builtin_os_log_format_buffer_size: {
17056 analyze_os_log::OSLogBufferLayout Layout;
17057 analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout);
17058 return Success(Value: Layout.size().getQuantity(), E);
17059 }
17060
17061 case Builtin::BI__builtin_is_aligned: {
17062 APValue Src;
17063 APSInt Alignment;
17064 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17065 return false;
17066 if (Src.isLValue()) {
17067 // If we evaluated a pointer, check the minimum known alignment.
17068 LValue Ptr;
17069 Ptr.setFrom(Ctx: Info.Ctx, V: Src);
17070 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr);
17071 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset);
17072 // We can return true if the known alignment at the computed offset is
17073 // greater than the requested alignment.
17074 assert(PtrAlign.isPowerOfTwo());
17075 assert(Alignment.isPowerOf2());
17076 if (PtrAlign.getQuantity() >= Alignment)
17077 return Success(Value: 1, E);
17078 // If the alignment is not known to be sufficient, some cases could still
17079 // be aligned at run time. However, if the requested alignment is less or
17080 // equal to the base alignment and the offset is not aligned, we know that
17081 // the run-time value can never be aligned.
17082 if (BaseAlignment.getQuantity() >= Alignment &&
17083 PtrAlign.getQuantity() < Alignment)
17084 return Success(Value: 0, E);
17085 // Otherwise we can't infer whether the value is sufficiently aligned.
17086 // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N)
17087 // in cases where we can't fully evaluate the pointer.
17088 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_compute)
17089 << Alignment;
17090 return false;
17091 }
17092 assert(Src.isInt());
17093 return Success(Value: (Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E);
17094 }
17095 case Builtin::BI__builtin_align_up: {
17096 APValue Src;
17097 APSInt Alignment;
17098 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17099 return false;
17100 if (!Src.isInt())
17101 return Error(E);
17102 APSInt AlignedVal =
17103 APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1),
17104 Src.getInt().isUnsigned());
17105 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17106 return Success(SI: AlignedVal, E);
17107 }
17108 case Builtin::BI__builtin_align_down: {
17109 APValue Src;
17110 APSInt Alignment;
17111 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
17112 return false;
17113 if (!Src.isInt())
17114 return Error(E);
17115 APSInt AlignedVal =
17116 APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned());
17117 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
17118 return Success(SI: AlignedVal, E);
17119 }
17120
17121 case Builtin::BI__builtin_bitreverseg:
17122 case Builtin::BI__builtin_bitreverse8:
17123 case Builtin::BI__builtin_bitreverse16:
17124 case Builtin::BI__builtin_bitreverse32:
17125 case Builtin::BI__builtin_bitreverse64:
17126 case Builtin::BI__builtin_elementwise_bitreverse: {
17127 APSInt Val;
17128 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17129 return false;
17130
17131 return Success(I: Val.reverseBits(), E);
17132 }
17133 case Builtin::BI__builtin_bswapg:
17134 case Builtin::BI__builtin_bswap16:
17135 case Builtin::BI__builtin_bswap32:
17136 case Builtin::BI__builtin_bswap64:
17137 case Builtin::BIstdc_memreverse8u8:
17138 case Builtin::BIstdc_memreverse8u16:
17139 case Builtin::BIstdc_memreverse8u32:
17140 case Builtin::BIstdc_memreverse8u64: {
17141 APSInt Val;
17142 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17143 return false;
17144 if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
17145 return Success(SI: Val, E);
17146
17147 return Success(I: Val.byteSwap(), E);
17148 }
17149
17150 case Builtin::BI__builtin_classify_type:
17151 return Success(Value: (int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E);
17152
17153 case Builtin::BI__builtin_clrsb:
17154 case Builtin::BI__builtin_clrsbl:
17155 case Builtin::BI__builtin_clrsbll: {
17156 APSInt Val;
17157 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17158 return false;
17159
17160 return Success(Value: Val.getBitWidth() - Val.getSignificantBits(), E);
17161 }
17162
17163 case Builtin::BI__builtin_clz:
17164 case Builtin::BI__builtin_clzl:
17165 case Builtin::BI__builtin_clzll:
17166 case Builtin::BI__builtin_clzs:
17167 case Builtin::BI__builtin_clzg:
17168 case Builtin::BI__builtin_elementwise_clzg:
17169 case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes
17170 case Builtin::BI__lzcnt:
17171 case Builtin::BI__lzcnt64: {
17172 APSInt Val;
17173 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17174 APValue Vec;
17175 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17176 return false;
17177 Val = ConvertBoolVectorToInt(Val: Vec);
17178 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17179 return false;
17180 }
17181
17182 std::optional<APSInt> Fallback;
17183 if ((BuiltinOp == Builtin::BI__builtin_clzg ||
17184 BuiltinOp == Builtin::BI__builtin_elementwise_clzg) &&
17185 E->getNumArgs() > 1) {
17186 APSInt FallbackTemp;
17187 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17188 return false;
17189 Fallback = FallbackTemp;
17190 }
17191
17192 if (!Val) {
17193 if (Fallback)
17194 return Success(SI: *Fallback, E);
17195
17196 // When the argument is 0, the result of GCC builtins is undefined,
17197 // whereas for Microsoft intrinsics, the result is the bit-width of the
17198 // argument.
17199 bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 &&
17200 BuiltinOp != Builtin::BI__lzcnt &&
17201 BuiltinOp != Builtin::BI__lzcnt64;
17202
17203 if (BuiltinOp == Builtin::BI__builtin_elementwise_clzg) {
17204 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17205 << /*IsTrailing=*/false;
17206 }
17207
17208 if (ZeroIsUndefined)
17209 return Error(E);
17210 }
17211
17212 return Success(Value: Val.countl_zero(), E);
17213 }
17214
17215 case Builtin::BI__builtin_constant_p: {
17216 const Expr *Arg = E->getArg(Arg: 0);
17217 if (EvaluateBuiltinConstantP(Info, Arg))
17218 return Success(Value: true, E);
17219 if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) {
17220 // Outside a constant context, eagerly evaluate to false in the presence
17221 // of side-effects in order to avoid -Wunsequenced false-positives in
17222 // a branch on __builtin_constant_p(expr).
17223 return Success(Value: false, E);
17224 }
17225 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17226 return false;
17227 }
17228
17229 case Builtin::BI__noop:
17230 // __noop always evaluates successfully and returns 0.
17231 return Success(Value: 0, E);
17232
17233 case Builtin::BI__builtin_is_constant_evaluated: {
17234 const auto *Callee = Info.CurrentCall->getCallee();
17235 if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression &&
17236 (Info.CallStackDepth == 1 ||
17237 (Info.CallStackDepth == 2 && Callee->isInStdNamespace() &&
17238 Callee->getIdentifier() &&
17239 Callee->getIdentifier()->isStr(Str: "is_constant_evaluated")))) {
17240 // FIXME: Find a better way to avoid duplicated diagnostics.
17241 if (Info.EvalStatus.Diag)
17242 Info.report(Loc: (Info.CallStackDepth == 1)
17243 ? E->getExprLoc()
17244 : Info.CurrentCall->getCallRange().getBegin(),
17245 DiagId: diag::warn_is_constant_evaluated_always_true_constexpr)
17246 << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated"
17247 : "std::is_constant_evaluated");
17248 }
17249
17250 return Success(Value: Info.InConstantContext, E);
17251 }
17252
17253 case Builtin::BI__builtin_is_within_lifetime:
17254 if (auto result = EvaluateBuiltinIsWithinLifetime(*this, E))
17255 return Success(Value: *result, E);
17256 return false;
17257
17258 case Builtin::BI__builtin_ctz:
17259 case Builtin::BI__builtin_ctzl:
17260 case Builtin::BI__builtin_ctzll:
17261 case Builtin::BI__builtin_ctzs:
17262 case Builtin::BI__builtin_ctzg:
17263 case Builtin::BI__builtin_elementwise_ctzg: {
17264 APSInt Val;
17265 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17266 APValue Vec;
17267 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17268 return false;
17269 Val = ConvertBoolVectorToInt(Val: Vec);
17270 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17271 return false;
17272 }
17273
17274 std::optional<APSInt> Fallback;
17275 if ((BuiltinOp == Builtin::BI__builtin_ctzg ||
17276 BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) &&
17277 E->getNumArgs() > 1) {
17278 APSInt FallbackTemp;
17279 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
17280 return false;
17281 Fallback = FallbackTemp;
17282 }
17283
17284 if (!Val) {
17285 if (Fallback)
17286 return Success(SI: *Fallback, E);
17287
17288 if (BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) {
17289 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
17290 << /*IsTrailing=*/true;
17291 }
17292 return Error(E);
17293 }
17294
17295 return Success(Value: Val.countr_zero(), E);
17296 }
17297
17298 case Builtin::BI__builtin_eh_return_data_regno: {
17299 int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
17300 Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand);
17301 return Success(Value: Operand, E);
17302 }
17303
17304 case Builtin::BI__builtin_elementwise_abs: {
17305 APSInt Val;
17306 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17307 return false;
17308
17309 return Success(I: Val.abs(), E);
17310 }
17311
17312 case Builtin::BI__builtin_expect:
17313 case Builtin::BI__builtin_expect_with_probability:
17314 return Visit(S: E->getArg(Arg: 0));
17315
17316 case Builtin::BI__builtin_ptrauth_string_discriminator: {
17317 const auto *Literal =
17318 cast<StringLiteral>(Val: E->getArg(Arg: 0)->IgnoreParenImpCasts());
17319 uint64_t Result = getPointerAuthStableSipHash(S: Literal->getString());
17320 return Success(Value: Result, E);
17321 }
17322
17323 case Builtin::BI__builtin_infer_alloc_token: {
17324 // If we fail to infer a type, this fails to be a constant expression; this
17325 // can be checked with __builtin_constant_p(...).
17326 QualType AllocType = infer_alloc::inferPossibleType(E, Ctx: Info.Ctx, CastE: nullptr);
17327 if (AllocType.isNull())
17328 return Error(
17329 E, D: diag::note_constexpr_infer_alloc_token_type_inference_failed);
17330 auto ATMD = infer_alloc::getAllocTokenMetadata(T: AllocType, Ctx: Info.Ctx);
17331 if (!ATMD)
17332 return Error(E, D: diag::note_constexpr_infer_alloc_token_no_metadata);
17333 auto Mode =
17334 Info.getLangOpts().AllocTokenMode.value_or(u: llvm::DefaultAllocTokenMode);
17335 uint64_t BitWidth = Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType());
17336 auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;
17337 uint64_t MaxTokens =
17338 MaxTokensOpt.value_or(u: 0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
17339 auto MaybeToken = llvm::getAllocToken(Mode, Metadata: *ATMD, MaxTokens);
17340 if (!MaybeToken)
17341 return Error(E, D: diag::note_constexpr_infer_alloc_token_stateful_mode);
17342 return Success(I: llvm::APInt(BitWidth, *MaybeToken), E);
17343 }
17344
17345 case Builtin::BI__builtin_ffs:
17346 case Builtin::BI__builtin_ffsl:
17347 case Builtin::BI__builtin_ffsll: {
17348 APSInt Val;
17349 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17350 return false;
17351
17352 unsigned N = Val.countr_zero();
17353 return Success(Value: N == Val.getBitWidth() ? 0 : N + 1, E);
17354 }
17355
17356 case Builtin::BI__builtin_fpclassify: {
17357 APFloat Val(0.0);
17358 if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info))
17359 return false;
17360 unsigned Arg;
17361 switch (Val.getCategory()) {
17362 case APFloat::fcNaN: Arg = 0; break;
17363 case APFloat::fcInfinity: Arg = 1; break;
17364 case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break;
17365 case APFloat::fcZero: Arg = 4; break;
17366 }
17367 return Visit(S: E->getArg(Arg));
17368 }
17369
17370 case Builtin::BI__builtin_isinf_sign: {
17371 APFloat Val(0.0);
17372 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17373 Success(Value: Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E);
17374 }
17375
17376 case Builtin::BI__builtin_isinf: {
17377 APFloat Val(0.0);
17378 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17379 Success(Value: Val.isInfinity() ? 1 : 0, E);
17380 }
17381
17382 case Builtin::BI__builtin_isfinite: {
17383 APFloat Val(0.0);
17384 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17385 Success(Value: Val.isFinite() ? 1 : 0, E);
17386 }
17387
17388 case Builtin::BI__builtin_isnan: {
17389 APFloat Val(0.0);
17390 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17391 Success(Value: Val.isNaN() ? 1 : 0, E);
17392 }
17393
17394 case Builtin::BI__builtin_isnormal: {
17395 APFloat Val(0.0);
17396 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17397 Success(Value: Val.isNormal() ? 1 : 0, E);
17398 }
17399
17400 case Builtin::BI__builtin_issubnormal: {
17401 APFloat Val(0.0);
17402 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17403 Success(Value: Val.isDenormal() ? 1 : 0, E);
17404 }
17405
17406 case Builtin::BI__builtin_iszero: {
17407 APFloat Val(0.0);
17408 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17409 Success(Value: Val.isZero() ? 1 : 0, E);
17410 }
17411
17412 case Builtin::BI__builtin_signbit:
17413 case Builtin::BI__builtin_signbitf:
17414 case Builtin::BI__builtin_signbitl: {
17415 APFloat Val(0.0);
17416 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17417 Success(Value: Val.isNegative() ? 1 : 0, E);
17418 }
17419
17420 case Builtin::BI__builtin_isgreater:
17421 case Builtin::BI__builtin_isgreaterequal:
17422 case Builtin::BI__builtin_isless:
17423 case Builtin::BI__builtin_islessequal:
17424 case Builtin::BI__builtin_islessgreater:
17425 case Builtin::BI__builtin_isunordered: {
17426 APFloat LHS(0.0);
17427 APFloat RHS(0.0);
17428 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17429 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
17430 return false;
17431
17432 return Success(
17433 Value: [&] {
17434 switch (BuiltinOp) {
17435 case Builtin::BI__builtin_isgreater:
17436 return LHS > RHS;
17437 case Builtin::BI__builtin_isgreaterequal:
17438 return LHS >= RHS;
17439 case Builtin::BI__builtin_isless:
17440 return LHS < RHS;
17441 case Builtin::BI__builtin_islessequal:
17442 return LHS <= RHS;
17443 case Builtin::BI__builtin_islessgreater: {
17444 APFloat::cmpResult cmp = LHS.compare(RHS);
17445 return cmp == APFloat::cmpResult::cmpLessThan ||
17446 cmp == APFloat::cmpResult::cmpGreaterThan;
17447 }
17448 case Builtin::BI__builtin_isunordered:
17449 return LHS.compare(RHS) == APFloat::cmpResult::cmpUnordered;
17450 default:
17451 llvm_unreachable("Unexpected builtin ID: Should be a floating "
17452 "point comparison function");
17453 }
17454 }()
17455 ? 1
17456 : 0,
17457 E);
17458 }
17459
17460 case Builtin::BI__builtin_issignaling: {
17461 APFloat Val(0.0);
17462 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17463 Success(Value: Val.isSignaling() ? 1 : 0, E);
17464 }
17465
17466 case Builtin::BI__builtin_isfpclass: {
17467 APSInt MaskVal;
17468 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info))
17469 return false;
17470 unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue());
17471 APFloat Val(0.0);
17472 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17473 Success(Value: (Val.classify() & Test) ? 1 : 0, E);
17474 }
17475
17476 case Builtin::BI__builtin_parity:
17477 case Builtin::BI__builtin_parityl:
17478 case Builtin::BI__builtin_parityll: {
17479 APSInt Val;
17480 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17481 return false;
17482
17483 return Success(Value: Val.popcount() % 2, E);
17484 }
17485
17486 case Builtin::BI__builtin_abs:
17487 case Builtin::BI__builtin_labs:
17488 case Builtin::BI__builtin_llabs: {
17489 APSInt Val;
17490 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17491 return false;
17492 if (Val == APSInt(APInt::getSignedMinValue(numBits: Val.getBitWidth()),
17493 /*IsUnsigned=*/false))
17494 return false;
17495 if (Val.isNegative())
17496 Val.negate();
17497 return Success(SI: Val, E);
17498 }
17499
17500 case Builtin::BI__builtin_popcount:
17501 case Builtin::BI__builtin_popcountl:
17502 case Builtin::BI__builtin_popcountll:
17503 case Builtin::BI__builtin_popcountg:
17504 case Builtin::BI__builtin_elementwise_popcount:
17505 case Builtin::BI__popcnt16: // Microsoft variants of popcount
17506 case Builtin::BI__popcnt:
17507 case Builtin::BI__popcnt64: {
17508 APSInt Val;
17509 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17510 APValue Vec;
17511 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17512 return false;
17513 Val = ConvertBoolVectorToInt(Val: Vec);
17514 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17515 return false;
17516 }
17517
17518 return Success(Value: Val.popcount(), E);
17519 }
17520
17521 case Builtin::BI__builtin_rotateleft8:
17522 case Builtin::BI__builtin_rotateleft16:
17523 case Builtin::BI__builtin_rotateleft32:
17524 case Builtin::BI__builtin_rotateleft64:
17525 case Builtin::BI__builtin_rotateright8:
17526 case Builtin::BI__builtin_rotateright16:
17527 case Builtin::BI__builtin_rotateright32:
17528 case Builtin::BI__builtin_rotateright64:
17529 case Builtin::BI__builtin_stdc_rotate_left:
17530 case Builtin::BI__builtin_stdc_rotate_right:
17531 case Builtin::BIstdc_rotate_left_uc:
17532 case Builtin::BIstdc_rotate_left_us:
17533 case Builtin::BIstdc_rotate_left_ui:
17534 case Builtin::BIstdc_rotate_left_ul:
17535 case Builtin::BIstdc_rotate_left_ull:
17536 case Builtin::BIstdc_rotate_right_uc:
17537 case Builtin::BIstdc_rotate_right_us:
17538 case Builtin::BIstdc_rotate_right_ui:
17539 case Builtin::BIstdc_rotate_right_ul:
17540 case Builtin::BIstdc_rotate_right_ull:
17541 case Builtin::BI_rotl8: // Microsoft variants of rotate left
17542 case Builtin::BI_rotl16:
17543 case Builtin::BI_rotl:
17544 case Builtin::BI_lrotl:
17545 case Builtin::BI_rotl64:
17546 case Builtin::BI_rotr8: // Microsoft variants of rotate right
17547 case Builtin::BI_rotr16:
17548 case Builtin::BI_rotr:
17549 case Builtin::BI_lrotr:
17550 case Builtin::BI_rotr64: {
17551 APSInt Value, Amount;
17552 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Value, Info) ||
17553 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amount, Info))
17554 return false;
17555
17556 Amount = NormalizeRotateAmount(Value, Amount);
17557
17558 switch (BuiltinOp) {
17559 case Builtin::BI__builtin_rotateright8:
17560 case Builtin::BI__builtin_rotateright16:
17561 case Builtin::BI__builtin_rotateright32:
17562 case Builtin::BI__builtin_rotateright64:
17563 case Builtin::BI__builtin_stdc_rotate_right:
17564 case Builtin::BIstdc_rotate_right_uc:
17565 case Builtin::BIstdc_rotate_right_us:
17566 case Builtin::BIstdc_rotate_right_ui:
17567 case Builtin::BIstdc_rotate_right_ul:
17568 case Builtin::BIstdc_rotate_right_ull:
17569 case Builtin::BI_rotr8:
17570 case Builtin::BI_rotr16:
17571 case Builtin::BI_rotr:
17572 case Builtin::BI_lrotr:
17573 case Builtin::BI_rotr64:
17574 return Success(
17575 SI: APSInt(Value.rotr(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17576 default:
17577 return Success(
17578 SI: APSInt(Value.rotl(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17579 }
17580 }
17581
17582 case Builtin::BIstdc_leading_zeros_uc:
17583 case Builtin::BIstdc_leading_zeros_us:
17584 case Builtin::BIstdc_leading_zeros_ui:
17585 case Builtin::BIstdc_leading_zeros_ul:
17586 case Builtin::BIstdc_leading_zeros_ull:
17587 case Builtin::BIstdc_leading_ones_uc:
17588 case Builtin::BIstdc_leading_ones_us:
17589 case Builtin::BIstdc_leading_ones_ui:
17590 case Builtin::BIstdc_leading_ones_ul:
17591 case Builtin::BIstdc_leading_ones_ull:
17592 case Builtin::BIstdc_trailing_zeros_uc:
17593 case Builtin::BIstdc_trailing_zeros_us:
17594 case Builtin::BIstdc_trailing_zeros_ui:
17595 case Builtin::BIstdc_trailing_zeros_ul:
17596 case Builtin::BIstdc_trailing_zeros_ull:
17597 case Builtin::BIstdc_trailing_ones_uc:
17598 case Builtin::BIstdc_trailing_ones_us:
17599 case Builtin::BIstdc_trailing_ones_ui:
17600 case Builtin::BIstdc_trailing_ones_ul:
17601 case Builtin::BIstdc_trailing_ones_ull:
17602 case Builtin::BIstdc_first_leading_zero_uc:
17603 case Builtin::BIstdc_first_leading_zero_us:
17604 case Builtin::BIstdc_first_leading_zero_ui:
17605 case Builtin::BIstdc_first_leading_zero_ul:
17606 case Builtin::BIstdc_first_leading_zero_ull:
17607 case Builtin::BIstdc_first_leading_one_uc:
17608 case Builtin::BIstdc_first_leading_one_us:
17609 case Builtin::BIstdc_first_leading_one_ui:
17610 case Builtin::BIstdc_first_leading_one_ul:
17611 case Builtin::BIstdc_first_leading_one_ull:
17612 case Builtin::BIstdc_first_trailing_zero_uc:
17613 case Builtin::BIstdc_first_trailing_zero_us:
17614 case Builtin::BIstdc_first_trailing_zero_ui:
17615 case Builtin::BIstdc_first_trailing_zero_ul:
17616 case Builtin::BIstdc_first_trailing_zero_ull:
17617 case Builtin::BIstdc_first_trailing_one_uc:
17618 case Builtin::BIstdc_first_trailing_one_us:
17619 case Builtin::BIstdc_first_trailing_one_ui:
17620 case Builtin::BIstdc_first_trailing_one_ul:
17621 case Builtin::BIstdc_first_trailing_one_ull:
17622 case Builtin::BIstdc_count_zeros_uc:
17623 case Builtin::BIstdc_count_zeros_us:
17624 case Builtin::BIstdc_count_zeros_ui:
17625 case Builtin::BIstdc_count_zeros_ul:
17626 case Builtin::BIstdc_count_zeros_ull:
17627 case Builtin::BIstdc_count_ones_uc:
17628 case Builtin::BIstdc_count_ones_us:
17629 case Builtin::BIstdc_count_ones_ui:
17630 case Builtin::BIstdc_count_ones_ul:
17631 case Builtin::BIstdc_count_ones_ull:
17632 case Builtin::BIstdc_has_single_bit_uc:
17633 case Builtin::BIstdc_has_single_bit_us:
17634 case Builtin::BIstdc_has_single_bit_ui:
17635 case Builtin::BIstdc_has_single_bit_ul:
17636 case Builtin::BIstdc_has_single_bit_ull:
17637 case Builtin::BIstdc_bit_width_uc:
17638 case Builtin::BIstdc_bit_width_us:
17639 case Builtin::BIstdc_bit_width_ui:
17640 case Builtin::BIstdc_bit_width_ul:
17641 case Builtin::BIstdc_bit_width_ull:
17642 case Builtin::BIstdc_bit_floor_uc:
17643 case Builtin::BIstdc_bit_floor_us:
17644 case Builtin::BIstdc_bit_floor_ui:
17645 case Builtin::BIstdc_bit_floor_ul:
17646 case Builtin::BIstdc_bit_floor_ull:
17647 case Builtin::BIstdc_bit_ceil_uc:
17648 case Builtin::BIstdc_bit_ceil_us:
17649 case Builtin::BIstdc_bit_ceil_ui:
17650 case Builtin::BIstdc_bit_ceil_ul:
17651 case Builtin::BIstdc_bit_ceil_ull:
17652 case Builtin::BI__builtin_stdc_leading_zeros:
17653 case Builtin::BI__builtin_stdc_leading_ones:
17654 case Builtin::BI__builtin_stdc_trailing_zeros:
17655 case Builtin::BI__builtin_stdc_trailing_ones:
17656 case Builtin::BI__builtin_stdc_first_leading_zero:
17657 case Builtin::BI__builtin_stdc_first_leading_one:
17658 case Builtin::BI__builtin_stdc_first_trailing_zero:
17659 case Builtin::BI__builtin_stdc_first_trailing_one:
17660 case Builtin::BI__builtin_stdc_count_zeros:
17661 case Builtin::BI__builtin_stdc_count_ones:
17662 case Builtin::BI__builtin_stdc_has_single_bit:
17663 case Builtin::BI__builtin_stdc_bit_width:
17664 case Builtin::BI__builtin_stdc_bit_floor:
17665 case Builtin::BI__builtin_stdc_bit_ceil: {
17666 APSInt Val;
17667 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17668 return false;
17669
17670 unsigned BitWidth = Val.getBitWidth();
17671 const unsigned ResBitWidth = Info.Ctx.getIntWidth(T: E->getType());
17672
17673 switch (BuiltinOp) {
17674 case Builtin::BIstdc_leading_zeros_uc:
17675 case Builtin::BIstdc_leading_zeros_us:
17676 case Builtin::BIstdc_leading_zeros_ui:
17677 case Builtin::BIstdc_leading_zeros_ul:
17678 case Builtin::BIstdc_leading_zeros_ull:
17679 case Builtin::BI__builtin_stdc_leading_zeros:
17680 return Success(I: APInt(ResBitWidth, Val.countl_zero()), E);
17681 case Builtin::BIstdc_leading_ones_uc:
17682 case Builtin::BIstdc_leading_ones_us:
17683 case Builtin::BIstdc_leading_ones_ui:
17684 case Builtin::BIstdc_leading_ones_ul:
17685 case Builtin::BIstdc_leading_ones_ull:
17686 case Builtin::BI__builtin_stdc_leading_ones:
17687 return Success(I: APInt(ResBitWidth, Val.countl_one()), E);
17688 case Builtin::BIstdc_trailing_zeros_uc:
17689 case Builtin::BIstdc_trailing_zeros_us:
17690 case Builtin::BIstdc_trailing_zeros_ui:
17691 case Builtin::BIstdc_trailing_zeros_ul:
17692 case Builtin::BIstdc_trailing_zeros_ull:
17693 case Builtin::BI__builtin_stdc_trailing_zeros:
17694 return Success(I: APInt(ResBitWidth, Val.countr_zero()), E);
17695 case Builtin::BIstdc_trailing_ones_uc:
17696 case Builtin::BIstdc_trailing_ones_us:
17697 case Builtin::BIstdc_trailing_ones_ui:
17698 case Builtin::BIstdc_trailing_ones_ul:
17699 case Builtin::BIstdc_trailing_ones_ull:
17700 case Builtin::BI__builtin_stdc_trailing_ones:
17701 return Success(I: APInt(ResBitWidth, Val.countr_one()), E);
17702 case Builtin::BIstdc_first_leading_zero_uc:
17703 case Builtin::BIstdc_first_leading_zero_us:
17704 case Builtin::BIstdc_first_leading_zero_ui:
17705 case Builtin::BIstdc_first_leading_zero_ul:
17706 case Builtin::BIstdc_first_leading_zero_ull:
17707 case Builtin::BI__builtin_stdc_first_leading_zero:
17708 return Success(
17709 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countl_one() + 1), E);
17710 case Builtin::BIstdc_first_leading_one_uc:
17711 case Builtin::BIstdc_first_leading_one_us:
17712 case Builtin::BIstdc_first_leading_one_ui:
17713 case Builtin::BIstdc_first_leading_one_ul:
17714 case Builtin::BIstdc_first_leading_one_ull:
17715 case Builtin::BI__builtin_stdc_first_leading_one:
17716 return Success(
17717 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countl_zero() + 1), E);
17718 case Builtin::BIstdc_first_trailing_zero_uc:
17719 case Builtin::BIstdc_first_trailing_zero_us:
17720 case Builtin::BIstdc_first_trailing_zero_ui:
17721 case Builtin::BIstdc_first_trailing_zero_ul:
17722 case Builtin::BIstdc_first_trailing_zero_ull:
17723 case Builtin::BI__builtin_stdc_first_trailing_zero:
17724 return Success(
17725 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countr_one() + 1), E);
17726 case Builtin::BIstdc_first_trailing_one_uc:
17727 case Builtin::BIstdc_first_trailing_one_us:
17728 case Builtin::BIstdc_first_trailing_one_ui:
17729 case Builtin::BIstdc_first_trailing_one_ul:
17730 case Builtin::BIstdc_first_trailing_one_ull:
17731 case Builtin::BI__builtin_stdc_first_trailing_one:
17732 return Success(
17733 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countr_zero() + 1), E);
17734 case Builtin::BIstdc_count_zeros_uc:
17735 case Builtin::BIstdc_count_zeros_us:
17736 case Builtin::BIstdc_count_zeros_ui:
17737 case Builtin::BIstdc_count_zeros_ul:
17738 case Builtin::BIstdc_count_zeros_ull:
17739 case Builtin::BI__builtin_stdc_count_zeros: {
17740 APInt Cnt(ResBitWidth, BitWidth - Val.popcount());
17741 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17742 }
17743 case Builtin::BIstdc_count_ones_uc:
17744 case Builtin::BIstdc_count_ones_us:
17745 case Builtin::BIstdc_count_ones_ui:
17746 case Builtin::BIstdc_count_ones_ul:
17747 case Builtin::BIstdc_count_ones_ull:
17748 case Builtin::BI__builtin_stdc_count_ones: {
17749 APInt Cnt(ResBitWidth, Val.popcount());
17750 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17751 }
17752 case Builtin::BIstdc_has_single_bit_uc:
17753 case Builtin::BIstdc_has_single_bit_us:
17754 case Builtin::BIstdc_has_single_bit_ui:
17755 case Builtin::BIstdc_has_single_bit_ul:
17756 case Builtin::BIstdc_has_single_bit_ull:
17757 case Builtin::BI__builtin_stdc_has_single_bit: {
17758 APInt Res(ResBitWidth, Val.popcount() == 1 ? 1 : 0);
17759 return Success(SI: APSInt(Res, /*IsUnsigned*/ true), E);
17760 }
17761 case Builtin::BIstdc_bit_width_uc:
17762 case Builtin::BIstdc_bit_width_us:
17763 case Builtin::BIstdc_bit_width_ui:
17764 case Builtin::BIstdc_bit_width_ul:
17765 case Builtin::BIstdc_bit_width_ull:
17766 case Builtin::BI__builtin_stdc_bit_width:
17767 return Success(I: APInt(ResBitWidth, BitWidth - Val.countl_zero()), E);
17768 case Builtin::BIstdc_bit_floor_uc:
17769 case Builtin::BIstdc_bit_floor_us:
17770 case Builtin::BIstdc_bit_floor_ui:
17771 case Builtin::BIstdc_bit_floor_ul:
17772 case Builtin::BIstdc_bit_floor_ull:
17773 case Builtin::BI__builtin_stdc_bit_floor: {
17774 if (Val.isZero())
17775 return Success(I: APInt(BitWidth, 0), E);
17776 unsigned Exp = BitWidth - Val.countl_zero() - 1;
17777 return Success(
17778 SI: APSInt(APInt::getOneBitSet(numBits: BitWidth, BitNo: Exp), /*IsUnsigned*/ true), E);
17779 }
17780 case Builtin::BIstdc_bit_ceil_uc:
17781 case Builtin::BIstdc_bit_ceil_us:
17782 case Builtin::BIstdc_bit_ceil_ui:
17783 case Builtin::BIstdc_bit_ceil_ul:
17784 case Builtin::BIstdc_bit_ceil_ull:
17785 case Builtin::BI__builtin_stdc_bit_ceil: {
17786 if (Val.ule(RHS: 1))
17787 return Success(SI: APSInt(APInt(BitWidth, 1), /*IsUnsigned*/ true), E);
17788 APInt ValMinusOne = Val - 1;
17789 unsigned LZ = ValMinusOne.countl_zero();
17790 if (LZ == 0)
17791 return Success(SI: APSInt(APInt(BitWidth, 0), /*IsUnsigned*/ true),
17792 E); // overflows; wrap to 0
17793 APInt Result = APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - LZ);
17794 return Success(SI: APSInt(Result, /*IsUnsigned*/ true), E);
17795 }
17796 default:
17797 llvm_unreachable("Unknown stdc builtin");
17798 }
17799 }
17800
17801 case Builtin::BI__builtin_elementwise_add_sat: {
17802 APSInt LHS, RHS;
17803 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17804 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17805 return false;
17806
17807 APInt Result = LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
17808 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17809 }
17810 case Builtin::BI__builtin_elementwise_sub_sat: {
17811 APSInt LHS, RHS;
17812 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17813 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17814 return false;
17815
17816 APInt Result = LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
17817 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17818 }
17819 case Builtin::BI__builtin_elementwise_max: {
17820 APSInt LHS, RHS;
17821 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17822 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17823 return false;
17824
17825 APInt Result = std::max(a: LHS, b: RHS);
17826 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17827 }
17828 case Builtin::BI__builtin_elementwise_min: {
17829 APSInt LHS, RHS;
17830 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17831 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17832 return false;
17833
17834 APInt Result = std::min(a: LHS, b: RHS);
17835 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17836 }
17837 case Builtin::BI__builtin_elementwise_clmul: {
17838 APSInt LHS, RHS;
17839 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17840 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17841 return false;
17842
17843 APInt Result = llvm::APIntOps::clmul(LHS, RHS);
17844 return Success(SI: APSInt(Result, LHS.isUnsigned()), E);
17845 }
17846 case Builtin::BI__builtin_elementwise_fshl:
17847 case Builtin::BI__builtin_elementwise_fshr: {
17848 APSInt Hi, Lo, Shift;
17849 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Hi, Info) ||
17850 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Lo, Info) ||
17851 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Shift, Info))
17852 return false;
17853
17854 switch (BuiltinOp) {
17855 case Builtin::BI__builtin_elementwise_fshl: {
17856 APSInt Result(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned());
17857 return Success(SI: Result, E);
17858 }
17859 case Builtin::BI__builtin_elementwise_fshr: {
17860 APSInt Result(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned());
17861 return Success(SI: Result, E);
17862 }
17863 }
17864 llvm_unreachable("Fully covered switch above");
17865 }
17866 case Builtin::BIstrlen:
17867 case Builtin::BIwcslen:
17868 // A call to strlen is not a constant expression.
17869 if (Info.getLangOpts().CPlusPlus11)
17870 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
17871 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
17872 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
17873 else
17874 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17875 [[fallthrough]];
17876 case Builtin::BI__builtin_strlen:
17877 case Builtin::BI__builtin_wcslen: {
17878 // As an extension, we support __builtin_strlen() as a constant expression,
17879 // and support folding strlen() to a constant.
17880 if (std::optional<uint64_t> StrLen =
17881 EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Info))
17882 return Success(Value: *StrLen, E);
17883 return false;
17884 }
17885
17886 case Builtin::BIstrcmp:
17887 case Builtin::BIwcscmp:
17888 case Builtin::BIstrncmp:
17889 case Builtin::BIwcsncmp:
17890 case Builtin::BImemcmp:
17891 case Builtin::BIbcmp:
17892 case Builtin::BIwmemcmp:
17893 // A call to strlen is not a constant expression.
17894 if (Info.getLangOpts().CPlusPlus11)
17895 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
17896 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
17897 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
17898 else
17899 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17900 [[fallthrough]];
17901 case Builtin::BI__builtin_strcmp:
17902 case Builtin::BI__builtin_wcscmp:
17903 case Builtin::BI__builtin_strncmp:
17904 case Builtin::BI__builtin_wcsncmp:
17905 case Builtin::BI__builtin_memcmp:
17906 case Builtin::BI__builtin_bcmp:
17907 case Builtin::BI__builtin_wmemcmp: {
17908 LValue String1, String2;
17909 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) ||
17910 !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info))
17911 return false;
17912
17913 uint64_t MaxLength = uint64_t(-1);
17914 if (BuiltinOp != Builtin::BIstrcmp &&
17915 BuiltinOp != Builtin::BIwcscmp &&
17916 BuiltinOp != Builtin::BI__builtin_strcmp &&
17917 BuiltinOp != Builtin::BI__builtin_wcscmp) {
17918 APSInt N;
17919 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
17920 return false;
17921 MaxLength = N.getZExtValue();
17922 }
17923
17924 // Empty substrings compare equal by definition.
17925 if (MaxLength == 0u)
17926 return Success(Value: 0, E);
17927
17928 if (!String1.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17929 !String2.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17930 String1.Designator.Invalid || String2.Designator.Invalid)
17931 return false;
17932
17933 QualType CharTy1 = String1.Designator.getType(Ctx&: Info.Ctx);
17934 QualType CharTy2 = String2.Designator.getType(Ctx&: Info.Ctx);
17935
17936 bool IsRawByte = BuiltinOp == Builtin::BImemcmp ||
17937 BuiltinOp == Builtin::BIbcmp ||
17938 BuiltinOp == Builtin::BI__builtin_memcmp ||
17939 BuiltinOp == Builtin::BI__builtin_bcmp;
17940
17941 assert(IsRawByte ||
17942 (Info.Ctx.hasSameUnqualifiedType(
17943 CharTy1, E->getArg(0)->getType()->getPointeeType()) &&
17944 Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2)));
17945
17946 // For memcmp, allow comparing any arrays of '[[un]signed] char' or
17947 // 'char8_t', but no other types.
17948 if (IsRawByte &&
17949 !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) {
17950 // FIXME: Consider using our bit_cast implementation to support this.
17951 Info.FFDiag(E, DiagId: diag::note_constexpr_memcmp_unsupported)
17952 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy1
17953 << CharTy2;
17954 return false;
17955 }
17956
17957 const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) {
17958 return handleLValueToRValueConversion(Info, Conv: E, Type: CharTy1, LVal: String1, RVal&: Char1) &&
17959 handleLValueToRValueConversion(Info, Conv: E, Type: CharTy2, LVal: String2, RVal&: Char2) &&
17960 Char1.isInt() && Char2.isInt();
17961 };
17962 const auto &AdvanceElems = [&] {
17963 return HandleLValueArrayAdjustment(Info, E, LVal&: String1, EltTy: CharTy1, Adjustment: 1) &&
17964 HandleLValueArrayAdjustment(Info, E, LVal&: String2, EltTy: CharTy2, Adjustment: 1);
17965 };
17966
17967 bool StopAtNull =
17968 (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp &&
17969 BuiltinOp != Builtin::BIwmemcmp &&
17970 BuiltinOp != Builtin::BI__builtin_memcmp &&
17971 BuiltinOp != Builtin::BI__builtin_bcmp &&
17972 BuiltinOp != Builtin::BI__builtin_wmemcmp);
17973 bool IsWide = BuiltinOp == Builtin::BIwcscmp ||
17974 BuiltinOp == Builtin::BIwcsncmp ||
17975 BuiltinOp == Builtin::BIwmemcmp ||
17976 BuiltinOp == Builtin::BI__builtin_wcscmp ||
17977 BuiltinOp == Builtin::BI__builtin_wcsncmp ||
17978 BuiltinOp == Builtin::BI__builtin_wmemcmp;
17979
17980 for (; MaxLength; --MaxLength) {
17981 APValue Char1, Char2;
17982 if (!ReadCurElems(Char1, Char2))
17983 return false;
17984 if (Char1.getInt().ne(RHS: Char2.getInt())) {
17985 if (IsWide) // wmemcmp compares with wchar_t signedness.
17986 return Success(Value: Char1.getInt() < Char2.getInt() ? -1 : 1, E);
17987 // memcmp always compares unsigned chars.
17988 return Success(Value: Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E);
17989 }
17990 if (StopAtNull && !Char1.getInt())
17991 return Success(Value: 0, E);
17992 assert(!(StopAtNull && !Char2.getInt()));
17993 if (!AdvanceElems())
17994 return false;
17995 }
17996 // We hit the strncmp / memcmp limit.
17997 return Success(Value: 0, E);
17998 }
17999
18000 case Builtin::BI__atomic_always_lock_free:
18001 case Builtin::BI__atomic_is_lock_free:
18002 case Builtin::BI__c11_atomic_is_lock_free: {
18003 APSInt SizeVal;
18004 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info))
18005 return false;
18006
18007 // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
18008 // of two less than or equal to the maximum inline atomic width, we know it
18009 // is lock-free. If the size isn't a power of two, or greater than the
18010 // maximum alignment where we promote atomics, we know it is not lock-free
18011 // (at least not in the sense of atomic_is_lock_free). Otherwise,
18012 // the answer can only be determined at runtime; for example, 16-byte
18013 // atomics have lock-free implementations on some, but not all,
18014 // x86-64 processors.
18015
18016 // Check power-of-two.
18017 CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue());
18018 if (Size.isPowerOfTwo()) {
18019 // Check against inlining width.
18020 unsigned InlineWidthBits =
18021 Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
18022 if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) {
18023 if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
18024 Size == CharUnits::One())
18025 return Success(Value: 1, E);
18026
18027 // If the pointer argument can be evaluated to a compile-time constant
18028 // integer (or nullptr), check if that value is appropriately aligned.
18029 const Expr *PtrArg = E->getArg(Arg: 1);
18030 Expr::EvalResult ExprResult;
18031 APSInt IntResult;
18032 if (PtrArg->EvaluateAsRValue(Result&: ExprResult, Ctx: Info.Ctx) &&
18033 ExprResult.Val.toIntegralConstant(Result&: IntResult, SrcTy: PtrArg->getType(),
18034 Ctx: Info.Ctx) &&
18035 IntResult.isAligned(A: Size.getAsAlign()))
18036 return Success(Value: 1, E);
18037
18038 // Otherwise, check if the type's alignment against Size.
18039 if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: PtrArg)) {
18040 // Drop the potential implicit-cast to 'const volatile void*', getting
18041 // the underlying type.
18042 if (ICE->getCastKind() == CK_BitCast)
18043 PtrArg = ICE->getSubExpr();
18044 }
18045
18046 if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) {
18047 QualType PointeeType = PtrTy->getPointeeType();
18048 if (!PointeeType->isIncompleteType() &&
18049 Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) {
18050 // OK, we will inline operations on this object.
18051 return Success(Value: 1, E);
18052 }
18053 }
18054 }
18055 }
18056
18057 return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
18058 Success(Value: 0, E) : Error(E);
18059 }
18060 case Builtin::BI__builtin_addcb:
18061 case Builtin::BI__builtin_addcs:
18062 case Builtin::BI__builtin_addc:
18063 case Builtin::BI__builtin_addcl:
18064 case Builtin::BI__builtin_addcll:
18065 case Builtin::BI__builtin_subcb:
18066 case Builtin::BI__builtin_subcs:
18067 case Builtin::BI__builtin_subc:
18068 case Builtin::BI__builtin_subcl:
18069 case Builtin::BI__builtin_subcll: {
18070 LValue CarryOutLValue;
18071 APSInt LHS, RHS, CarryIn, CarryOut, Result;
18072 QualType ResultType = E->getArg(Arg: 0)->getType();
18073 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18074 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18075 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: CarryIn, Info) ||
18076 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: CarryOutLValue, Info))
18077 return false;
18078 // Copy the number of bits and sign.
18079 Result = LHS;
18080 CarryOut = LHS;
18081
18082 bool FirstOverflowed = false;
18083 bool SecondOverflowed = false;
18084 switch (BuiltinOp) {
18085 default:
18086 llvm_unreachable("Invalid value for BuiltinOp");
18087 case Builtin::BI__builtin_addcb:
18088 case Builtin::BI__builtin_addcs:
18089 case Builtin::BI__builtin_addc:
18090 case Builtin::BI__builtin_addcl:
18091 case Builtin::BI__builtin_addcll:
18092 Result =
18093 LHS.uadd_ov(RHS, Overflow&: FirstOverflowed).uadd_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18094 break;
18095 case Builtin::BI__builtin_subcb:
18096 case Builtin::BI__builtin_subcs:
18097 case Builtin::BI__builtin_subc:
18098 case Builtin::BI__builtin_subcl:
18099 case Builtin::BI__builtin_subcll:
18100 Result =
18101 LHS.usub_ov(RHS, Overflow&: FirstOverflowed).usub_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
18102 break;
18103 }
18104
18105 // It is possible for both overflows to happen but CGBuiltin uses an OR so
18106 // this is consistent.
18107 CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed);
18108 APValue APV{CarryOut};
18109 if (!handleAssignment(Info, E, LVal: CarryOutLValue, LValType: ResultType, Val&: APV))
18110 return false;
18111 return Success(SI: Result, E);
18112 }
18113 case Builtin::BI__builtin_add_overflow:
18114 case Builtin::BI__builtin_sub_overflow:
18115 case Builtin::BI__builtin_mul_overflow:
18116 case Builtin::BI__builtin_sadd_overflow:
18117 case Builtin::BI__builtin_uadd_overflow:
18118 case Builtin::BI__builtin_uaddl_overflow:
18119 case Builtin::BI__builtin_uaddll_overflow:
18120 case Builtin::BI__builtin_usub_overflow:
18121 case Builtin::BI__builtin_usubl_overflow:
18122 case Builtin::BI__builtin_usubll_overflow:
18123 case Builtin::BI__builtin_umul_overflow:
18124 case Builtin::BI__builtin_umull_overflow:
18125 case Builtin::BI__builtin_umulll_overflow:
18126 case Builtin::BI__builtin_saddl_overflow:
18127 case Builtin::BI__builtin_saddll_overflow:
18128 case Builtin::BI__builtin_ssub_overflow:
18129 case Builtin::BI__builtin_ssubl_overflow:
18130 case Builtin::BI__builtin_ssubll_overflow:
18131 case Builtin::BI__builtin_smul_overflow:
18132 case Builtin::BI__builtin_smull_overflow:
18133 case Builtin::BI__builtin_smulll_overflow: {
18134 LValue ResultLValue;
18135 APSInt LHS, RHS;
18136
18137 QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType();
18138 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18139 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18140 !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info))
18141 return false;
18142
18143 APSInt Result;
18144 bool DidOverflow = false;
18145
18146 // If the types don't have to match, enlarge all 3 to the largest of them.
18147 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18148 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18149 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18150 bool IsSigned = LHS.isSigned() || RHS.isSigned() ||
18151 ResultType->isSignedIntegerOrEnumerationType();
18152 bool AllSigned = LHS.isSigned() && RHS.isSigned() &&
18153 ResultType->isSignedIntegerOrEnumerationType();
18154 uint64_t LHSSize = LHS.getBitWidth();
18155 uint64_t RHSSize = RHS.getBitWidth();
18156 uint64_t ResultSize = Info.Ctx.getIntWidth(T: ResultType);
18157 uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize);
18158
18159 // Add an additional bit if the signedness isn't uniformly agreed to. We
18160 // could do this ONLY if there is a signed and an unsigned that both have
18161 // MaxBits, but the code to check that is pretty nasty. The issue will be
18162 // caught in the shrink-to-result later anyway.
18163 if (IsSigned && !AllSigned)
18164 ++MaxBits;
18165
18166 LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned);
18167 RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned);
18168 Result = APSInt(MaxBits, !IsSigned);
18169 }
18170
18171 // Find largest int.
18172 switch (BuiltinOp) {
18173 default:
18174 llvm_unreachable("Invalid value for BuiltinOp");
18175 case Builtin::BI__builtin_add_overflow:
18176 case Builtin::BI__builtin_sadd_overflow:
18177 case Builtin::BI__builtin_saddl_overflow:
18178 case Builtin::BI__builtin_saddll_overflow:
18179 case Builtin::BI__builtin_uadd_overflow:
18180 case Builtin::BI__builtin_uaddl_overflow:
18181 case Builtin::BI__builtin_uaddll_overflow:
18182 Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow)
18183 : LHS.uadd_ov(RHS, Overflow&: DidOverflow);
18184 break;
18185 case Builtin::BI__builtin_sub_overflow:
18186 case Builtin::BI__builtin_ssub_overflow:
18187 case Builtin::BI__builtin_ssubl_overflow:
18188 case Builtin::BI__builtin_ssubll_overflow:
18189 case Builtin::BI__builtin_usub_overflow:
18190 case Builtin::BI__builtin_usubl_overflow:
18191 case Builtin::BI__builtin_usubll_overflow:
18192 Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow)
18193 : LHS.usub_ov(RHS, Overflow&: DidOverflow);
18194 break;
18195 case Builtin::BI__builtin_mul_overflow:
18196 case Builtin::BI__builtin_smul_overflow:
18197 case Builtin::BI__builtin_smull_overflow:
18198 case Builtin::BI__builtin_smulll_overflow:
18199 case Builtin::BI__builtin_umul_overflow:
18200 case Builtin::BI__builtin_umull_overflow:
18201 case Builtin::BI__builtin_umulll_overflow:
18202 Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow)
18203 : LHS.umul_ov(RHS, Overflow&: DidOverflow);
18204 break;
18205 }
18206
18207 // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,
18208 // since it will give us the behavior of a TruncOrSelf in the case where
18209 // its parameter <= its size. We previously set Result to be at least the
18210 // integer width of the result, so getIntWidth(ResultType) <=
18211 // Result.BitWidth will work exactly like TruncOrSelf.
18212 APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getIntWidth(T: ResultType));
18213 Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());
18214
18215 // In the case where multiple sizes are allowed, truncate and see if
18216 // the values are the same.
18217 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
18218 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
18219 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
18220 if (!APSInt::isSameValue(I1: Temp, I2: Result))
18221 DidOverflow = true;
18222 }
18223 Result = Temp;
18224
18225 APValue APV{Result};
18226 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18227 return false;
18228 return Success(Value: DidOverflow, E);
18229 }
18230
18231 case Builtin::BI__builtin_reduce_add:
18232 case Builtin::BI__builtin_reduce_mul:
18233 case Builtin::BI__builtin_reduce_and:
18234 case Builtin::BI__builtin_reduce_or:
18235 case Builtin::BI__builtin_reduce_xor:
18236 case Builtin::BI__builtin_reduce_min:
18237 case Builtin::BI__builtin_reduce_max: {
18238 APValue Source;
18239 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
18240 return false;
18241
18242 unsigned SourceLen = Source.getVectorLength();
18243 APSInt Reduced = Source.getVectorElt(I: 0).getInt();
18244 for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
18245 switch (BuiltinOp) {
18246 default:
18247 return false;
18248 case Builtin::BI__builtin_reduce_add: {
18249 if (!CheckedIntArithmetic(
18250 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18251 BitWidth: Reduced.getBitWidth() + 1, Op: std::plus<APSInt>(), Result&: Reduced))
18252 return false;
18253 break;
18254 }
18255 case Builtin::BI__builtin_reduce_mul: {
18256 if (!CheckedIntArithmetic(
18257 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
18258 BitWidth: Reduced.getBitWidth() * 2, Op: std::multiplies<APSInt>(), Result&: Reduced))
18259 return false;
18260 break;
18261 }
18262 case Builtin::BI__builtin_reduce_and: {
18263 Reduced &= Source.getVectorElt(I: EltNum).getInt();
18264 break;
18265 }
18266 case Builtin::BI__builtin_reduce_or: {
18267 Reduced |= Source.getVectorElt(I: EltNum).getInt();
18268 break;
18269 }
18270 case Builtin::BI__builtin_reduce_xor: {
18271 Reduced ^= Source.getVectorElt(I: EltNum).getInt();
18272 break;
18273 }
18274 case Builtin::BI__builtin_reduce_min: {
18275 Reduced = std::min(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18276 break;
18277 }
18278 case Builtin::BI__builtin_reduce_max: {
18279 Reduced = std::max(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
18280 break;
18281 }
18282 }
18283 }
18284
18285 return Success(SI: Reduced, E);
18286 }
18287
18288 case clang::X86::BI__builtin_ia32_addcarryx_u32:
18289 case clang::X86::BI__builtin_ia32_addcarryx_u64:
18290 case clang::X86::BI__builtin_ia32_subborrow_u32:
18291 case clang::X86::BI__builtin_ia32_subborrow_u64: {
18292 LValue ResultLValue;
18293 APSInt CarryIn, LHS, RHS;
18294 QualType ResultType = E->getArg(Arg: 3)->getType()->getPointeeType();
18295 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CarryIn, Info) ||
18296 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: LHS, Info) ||
18297 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: RHS, Info) ||
18298 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: ResultLValue, Info))
18299 return false;
18300
18301 bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
18302 BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
18303
18304 unsigned BitWidth = LHS.getBitWidth();
18305 unsigned CarryInBit = CarryIn.ugt(RHS: 0) ? 1 : 0;
18306 APInt ExResult =
18307 IsAdd
18308 ? (LHS.zext(width: BitWidth + 1) + (RHS.zext(width: BitWidth + 1) + CarryInBit))
18309 : (LHS.zext(width: BitWidth + 1) - (RHS.zext(width: BitWidth + 1) + CarryInBit));
18310
18311 APInt Result = ExResult.extractBits(numBits: BitWidth, bitPosition: 0);
18312 uint64_t CarryOut = ExResult.extractBitsAsZExtValue(numBits: 1, bitPosition: BitWidth);
18313
18314 APValue APV{APSInt(Result, /*isUnsigned=*/true)};
18315 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
18316 return false;
18317 return Success(Value: CarryOut, E);
18318 }
18319
18320 case clang::X86::BI__builtin_ia32_movmskps:
18321 case clang::X86::BI__builtin_ia32_movmskpd:
18322 case clang::X86::BI__builtin_ia32_pmovmskb128:
18323 case clang::X86::BI__builtin_ia32_pmovmskb256:
18324 case clang::X86::BI__builtin_ia32_movmskps256:
18325 case clang::X86::BI__builtin_ia32_movmskpd256: {
18326 APValue Source;
18327 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
18328 return false;
18329 unsigned SourceLen = Source.getVectorLength();
18330 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
18331 QualType ElemQT = VT->getElementType();
18332 unsigned ResultLen = Info.Ctx.getTypeSize(
18333 T: E->getCallReturnType(Ctx: Info.Ctx)); // Always 32-bit integer.
18334 APInt Result(ResultLen, 0);
18335
18336 for (unsigned I = 0; I != SourceLen; ++I) {
18337 APInt Elem;
18338 if (ElemQT->isIntegerType()) {
18339 Elem = Source.getVectorElt(I).getInt();
18340 } else if (ElemQT->isRealFloatingType()) {
18341 Elem = Source.getVectorElt(I).getFloat().bitcastToAPInt();
18342 } else {
18343 return false;
18344 }
18345 Result.setBitVal(BitPosition: I, BitValue: Elem.isNegative());
18346 }
18347 return Success(I: Result, E);
18348 }
18349
18350 case clang::X86::BI__builtin_ia32_bextr_u32:
18351 case clang::X86::BI__builtin_ia32_bextr_u64:
18352 case clang::X86::BI__builtin_ia32_bextri_u32:
18353 case clang::X86::BI__builtin_ia32_bextri_u64: {
18354 APSInt Val, Idx;
18355 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18356 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18357 return false;
18358
18359 unsigned BitWidth = Val.getBitWidth();
18360 uint64_t Shift = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18361 uint64_t Length = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 8);
18362 Length = Length > BitWidth ? BitWidth : Length;
18363
18364 // Handle out of bounds cases.
18365 if (Length == 0 || Shift >= BitWidth)
18366 return Success(Value: 0, E);
18367
18368 uint64_t Result = Val.getZExtValue() >> Shift;
18369 Result &= llvm::maskTrailingOnes<uint64_t>(N: Length);
18370 return Success(Value: Result, E);
18371 }
18372
18373 case clang::X86::BI__builtin_ia32_bzhi_si:
18374 case clang::X86::BI__builtin_ia32_bzhi_di: {
18375 APSInt Val, Idx;
18376 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18377 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
18378 return false;
18379
18380 unsigned BitWidth = Val.getBitWidth();
18381 unsigned Index = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
18382 if (Index < BitWidth)
18383 Val.clearHighBits(hiBits: BitWidth - Index);
18384 return Success(SI: Val, E);
18385 }
18386
18387 case clang::X86::BI__builtin_ia32_ktestcqi:
18388 case clang::X86::BI__builtin_ia32_ktestchi:
18389 case clang::X86::BI__builtin_ia32_ktestcsi:
18390 case clang::X86::BI__builtin_ia32_ktestcdi: {
18391 APSInt A, B;
18392 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18393 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18394 return false;
18395
18396 return Success(Value: (~A & B) == 0, E);
18397 }
18398
18399 case clang::X86::BI__builtin_ia32_ktestzqi:
18400 case clang::X86::BI__builtin_ia32_ktestzhi:
18401 case clang::X86::BI__builtin_ia32_ktestzsi:
18402 case clang::X86::BI__builtin_ia32_ktestzdi: {
18403 APSInt A, B;
18404 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18405 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18406 return false;
18407
18408 return Success(Value: (A & B) == 0, E);
18409 }
18410
18411 case clang::X86::BI__builtin_ia32_kortestcqi:
18412 case clang::X86::BI__builtin_ia32_kortestchi:
18413 case clang::X86::BI__builtin_ia32_kortestcsi:
18414 case clang::X86::BI__builtin_ia32_kortestcdi: {
18415 APSInt A, B;
18416 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18417 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18418 return false;
18419
18420 return Success(Value: ~(A | B) == 0, E);
18421 }
18422
18423 case clang::X86::BI__builtin_ia32_kortestzqi:
18424 case clang::X86::BI__builtin_ia32_kortestzhi:
18425 case clang::X86::BI__builtin_ia32_kortestzsi:
18426 case clang::X86::BI__builtin_ia32_kortestzdi: {
18427 APSInt A, B;
18428 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18429 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18430 return false;
18431
18432 return Success(Value: (A | B) == 0, E);
18433 }
18434
18435 case clang::X86::BI__builtin_ia32_kunpckhi:
18436 case clang::X86::BI__builtin_ia32_kunpckdi:
18437 case clang::X86::BI__builtin_ia32_kunpcksi: {
18438 APSInt A, B;
18439 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18440 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18441 return false;
18442
18443 // Generic kunpack: extract lower half of each operand and concatenate
18444 // Result = A[HalfWidth-1:0] concat B[HalfWidth-1:0]
18445 unsigned BW = A.getBitWidth();
18446 APSInt Result(A.trunc(width: BW / 2).concat(NewLSB: B.trunc(width: BW / 2)), A.isUnsigned());
18447 return Success(SI: Result, E);
18448 }
18449
18450 case clang::X86::BI__builtin_ia32_lzcnt_u16:
18451 case clang::X86::BI__builtin_ia32_lzcnt_u32:
18452 case clang::X86::BI__builtin_ia32_lzcnt_u64: {
18453 APSInt Val;
18454 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18455 return false;
18456 return Success(Value: Val.countLeadingZeros(), E);
18457 }
18458
18459 case clang::X86::BI__builtin_ia32_tzcnt_u16:
18460 case clang::X86::BI__builtin_ia32_tzcnt_u32:
18461 case clang::X86::BI__builtin_ia32_tzcnt_u64: {
18462 APSInt Val;
18463 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18464 return false;
18465 return Success(Value: Val.countTrailingZeros(), E);
18466 }
18467
18468 case clang::X86::BI__builtin_ia32_pdep_si:
18469 case clang::X86::BI__builtin_ia32_pdep_di:
18470 case Builtin::BI__builtin_elementwise_pdep: {
18471 APSInt Val, Msk;
18472 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18473 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18474 return false;
18475 return Success(I: llvm::APIntOps::pdep(Val, Mask: Msk), E);
18476 }
18477
18478 case clang::X86::BI__builtin_ia32_pext_si:
18479 case clang::X86::BI__builtin_ia32_pext_di:
18480 case Builtin::BI__builtin_elementwise_pext: {
18481 APSInt Val, Msk;
18482 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18483 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18484 return false;
18485 return Success(I: llvm::APIntOps::pext(Val, Mask: Msk), E);
18486 }
18487 case X86::BI__builtin_ia32_ptestz128:
18488 case X86::BI__builtin_ia32_ptestz256:
18489 case X86::BI__builtin_ia32_vtestzps:
18490 case X86::BI__builtin_ia32_vtestzps256:
18491 case X86::BI__builtin_ia32_vtestzpd:
18492 case X86::BI__builtin_ia32_vtestzpd256: {
18493 return EvalTestOp(
18494 [](const APInt &A, const APInt &B) { return (A & B) == 0; });
18495 }
18496 case X86::BI__builtin_ia32_ptestc128:
18497 case X86::BI__builtin_ia32_ptestc256:
18498 case X86::BI__builtin_ia32_vtestcps:
18499 case X86::BI__builtin_ia32_vtestcps256:
18500 case X86::BI__builtin_ia32_vtestcpd:
18501 case X86::BI__builtin_ia32_vtestcpd256: {
18502 return EvalTestOp(
18503 [](const APInt &A, const APInt &B) { return (~A & B) == 0; });
18504 }
18505 case X86::BI__builtin_ia32_ptestnzc128:
18506 case X86::BI__builtin_ia32_ptestnzc256:
18507 case X86::BI__builtin_ia32_vtestnzcps:
18508 case X86::BI__builtin_ia32_vtestnzcps256:
18509 case X86::BI__builtin_ia32_vtestnzcpd:
18510 case X86::BI__builtin_ia32_vtestnzcpd256: {
18511 return EvalTestOp([](const APInt &A, const APInt &B) {
18512 return ((A & B) != 0) && ((~A & B) != 0);
18513 });
18514 }
18515 case X86::BI__builtin_ia32_kandqi:
18516 case X86::BI__builtin_ia32_kandhi:
18517 case X86::BI__builtin_ia32_kandsi:
18518 case X86::BI__builtin_ia32_kanddi: {
18519 return HandleMaskBinOp(
18520 [](const APSInt &LHS, const APSInt &RHS) { return LHS & RHS; });
18521 }
18522
18523 case X86::BI__builtin_ia32_kandnqi:
18524 case X86::BI__builtin_ia32_kandnhi:
18525 case X86::BI__builtin_ia32_kandnsi:
18526 case X86::BI__builtin_ia32_kandndi: {
18527 return HandleMaskBinOp(
18528 [](const APSInt &LHS, const APSInt &RHS) { return ~LHS & RHS; });
18529 }
18530
18531 case X86::BI__builtin_ia32_korqi:
18532 case X86::BI__builtin_ia32_korhi:
18533 case X86::BI__builtin_ia32_korsi:
18534 case X86::BI__builtin_ia32_kordi: {
18535 return HandleMaskBinOp(
18536 [](const APSInt &LHS, const APSInt &RHS) { return LHS | RHS; });
18537 }
18538
18539 case X86::BI__builtin_ia32_kxnorqi:
18540 case X86::BI__builtin_ia32_kxnorhi:
18541 case X86::BI__builtin_ia32_kxnorsi:
18542 case X86::BI__builtin_ia32_kxnordi: {
18543 return HandleMaskBinOp(
18544 [](const APSInt &LHS, const APSInt &RHS) { return ~(LHS ^ RHS); });
18545 }
18546
18547 case X86::BI__builtin_ia32_kxorqi:
18548 case X86::BI__builtin_ia32_kxorhi:
18549 case X86::BI__builtin_ia32_kxorsi:
18550 case X86::BI__builtin_ia32_kxordi: {
18551 return HandleMaskBinOp(
18552 [](const APSInt &LHS, const APSInt &RHS) { return LHS ^ RHS; });
18553 }
18554
18555 case X86::BI__builtin_ia32_knotqi:
18556 case X86::BI__builtin_ia32_knothi:
18557 case X86::BI__builtin_ia32_knotsi:
18558 case X86::BI__builtin_ia32_knotdi: {
18559 APSInt Val;
18560 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18561 return false;
18562 APSInt Result = ~Val;
18563 return Success(V: APValue(Result), E);
18564 }
18565
18566 case X86::BI__builtin_ia32_kaddqi:
18567 case X86::BI__builtin_ia32_kaddhi:
18568 case X86::BI__builtin_ia32_kaddsi:
18569 case X86::BI__builtin_ia32_kadddi: {
18570 return HandleMaskBinOp(
18571 [](const APSInt &LHS, const APSInt &RHS) { return LHS + RHS; });
18572 }
18573
18574 case X86::BI__builtin_ia32_kmovb:
18575 case X86::BI__builtin_ia32_kmovw:
18576 case X86::BI__builtin_ia32_kmovd:
18577 case X86::BI__builtin_ia32_kmovq: {
18578 APSInt Val;
18579 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18580 return false;
18581 return Success(SI: Val, E);
18582 }
18583
18584 case X86::BI__builtin_ia32_kshiftliqi:
18585 case X86::BI__builtin_ia32_kshiftlihi:
18586 case X86::BI__builtin_ia32_kshiftlisi:
18587 case X86::BI__builtin_ia32_kshiftlidi: {
18588 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18589 unsigned Amt = RHS.getZExtValue() & 0xFF;
18590 if (Amt >= LHS.getBitWidth())
18591 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18592 return APSInt(LHS.shl(shiftAmt: Amt), LHS.isUnsigned());
18593 });
18594 }
18595
18596 case X86::BI__builtin_ia32_kshiftriqi:
18597 case X86::BI__builtin_ia32_kshiftrihi:
18598 case X86::BI__builtin_ia32_kshiftrisi:
18599 case X86::BI__builtin_ia32_kshiftridi: {
18600 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18601 unsigned Amt = RHS.getZExtValue() & 0xFF;
18602 if (Amt >= LHS.getBitWidth())
18603 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18604 return APSInt(LHS.lshr(shiftAmt: Amt), LHS.isUnsigned());
18605 });
18606 }
18607
18608 case clang::X86::BI__builtin_ia32_vec_ext_v4hi:
18609 case clang::X86::BI__builtin_ia32_vec_ext_v16qi:
18610 case clang::X86::BI__builtin_ia32_vec_ext_v8hi:
18611 case clang::X86::BI__builtin_ia32_vec_ext_v4si:
18612 case clang::X86::BI__builtin_ia32_vec_ext_v2di:
18613 case clang::X86::BI__builtin_ia32_vec_ext_v32qi:
18614 case clang::X86::BI__builtin_ia32_vec_ext_v16hi:
18615 case clang::X86::BI__builtin_ia32_vec_ext_v8si:
18616 case clang::X86::BI__builtin_ia32_vec_ext_v4di: {
18617 APValue Vec;
18618 APSInt IdxAPS;
18619 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
18620 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
18621 return false;
18622 unsigned N = Vec.getVectorLength();
18623 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
18624 return Success(SI: Vec.getVectorElt(I: Idx).getInt(), E);
18625 }
18626
18627 case clang::X86::BI__builtin_ia32_cvtb2mask128:
18628 case clang::X86::BI__builtin_ia32_cvtb2mask256:
18629 case clang::X86::BI__builtin_ia32_cvtb2mask512:
18630 case clang::X86::BI__builtin_ia32_cvtw2mask128:
18631 case clang::X86::BI__builtin_ia32_cvtw2mask256:
18632 case clang::X86::BI__builtin_ia32_cvtw2mask512:
18633 case clang::X86::BI__builtin_ia32_cvtd2mask128:
18634 case clang::X86::BI__builtin_ia32_cvtd2mask256:
18635 case clang::X86::BI__builtin_ia32_cvtd2mask512:
18636 case clang::X86::BI__builtin_ia32_cvtq2mask128:
18637 case clang::X86::BI__builtin_ia32_cvtq2mask256:
18638 case clang::X86::BI__builtin_ia32_cvtq2mask512: {
18639 assert(E->getNumArgs() == 1);
18640 APValue Vec;
18641 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
18642 return false;
18643
18644 unsigned VectorLen = Vec.getVectorLength();
18645 unsigned RetWidth = Info.Ctx.getIntWidth(T: E->getType());
18646 llvm::APInt Bits(RetWidth, 0);
18647
18648 for (unsigned ElemNum = 0; ElemNum != VectorLen; ++ElemNum) {
18649 const APSInt &A = Vec.getVectorElt(I: ElemNum).getInt();
18650 unsigned MSB = A[A.getBitWidth() - 1];
18651 Bits.setBitVal(BitPosition: ElemNum, BitValue: MSB);
18652 }
18653
18654 APSInt RetMask(Bits, /*isUnsigned=*/true);
18655 return Success(V: APValue(RetMask), E);
18656 }
18657
18658 case clang::X86::BI__builtin_ia32_cmpb128_mask:
18659 case clang::X86::BI__builtin_ia32_cmpw128_mask:
18660 case clang::X86::BI__builtin_ia32_cmpd128_mask:
18661 case clang::X86::BI__builtin_ia32_cmpq128_mask:
18662 case clang::X86::BI__builtin_ia32_cmpb256_mask:
18663 case clang::X86::BI__builtin_ia32_cmpw256_mask:
18664 case clang::X86::BI__builtin_ia32_cmpd256_mask:
18665 case clang::X86::BI__builtin_ia32_cmpq256_mask:
18666 case clang::X86::BI__builtin_ia32_cmpb512_mask:
18667 case clang::X86::BI__builtin_ia32_cmpw512_mask:
18668 case clang::X86::BI__builtin_ia32_cmpd512_mask:
18669 case clang::X86::BI__builtin_ia32_cmpq512_mask:
18670 case clang::X86::BI__builtin_ia32_ucmpb128_mask:
18671 case clang::X86::BI__builtin_ia32_ucmpw128_mask:
18672 case clang::X86::BI__builtin_ia32_ucmpd128_mask:
18673 case clang::X86::BI__builtin_ia32_ucmpq128_mask:
18674 case clang::X86::BI__builtin_ia32_ucmpb256_mask:
18675 case clang::X86::BI__builtin_ia32_ucmpw256_mask:
18676 case clang::X86::BI__builtin_ia32_ucmpd256_mask:
18677 case clang::X86::BI__builtin_ia32_ucmpq256_mask:
18678 case clang::X86::BI__builtin_ia32_ucmpb512_mask:
18679 case clang::X86::BI__builtin_ia32_ucmpw512_mask:
18680 case clang::X86::BI__builtin_ia32_ucmpd512_mask:
18681 case clang::X86::BI__builtin_ia32_ucmpq512_mask: {
18682 assert(E->getNumArgs() == 4);
18683
18684 bool IsUnsigned =
18685 (BuiltinOp >= clang::X86::BI__builtin_ia32_ucmpb128_mask &&
18686 BuiltinOp <= clang::X86::BI__builtin_ia32_ucmpw512_mask);
18687
18688 APValue LHS, RHS;
18689 APSInt Mask, Opcode;
18690 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18691 !EvaluateVector(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18692 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Opcode, Info) ||
18693 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: Mask, Info))
18694 return false;
18695
18696 assert(LHS.getVectorLength() == RHS.getVectorLength());
18697
18698 unsigned VectorLen = LHS.getVectorLength();
18699 unsigned RetWidth = Mask.getBitWidth();
18700
18701 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18702
18703 for (unsigned ElemNum = 0; ElemNum < VectorLen; ++ElemNum) {
18704 const APSInt &A = LHS.getVectorElt(I: ElemNum).getInt();
18705 const APSInt &B = RHS.getVectorElt(I: ElemNum).getInt();
18706 bool Result = false;
18707
18708 switch (Opcode.getExtValue() & 0x7) {
18709 case 0: // _MM_CMPINT_EQ
18710 Result = (A == B);
18711 break;
18712 case 1: // _MM_CMPINT_LT
18713 Result = IsUnsigned ? A.ult(RHS: B) : A.slt(RHS: B);
18714 break;
18715 case 2: // _MM_CMPINT_LE
18716 Result = IsUnsigned ? A.ule(RHS: B) : A.sle(RHS: B);
18717 break;
18718 case 3: // _MM_CMPINT_FALSE
18719 Result = false;
18720 break;
18721 case 4: // _MM_CMPINT_NE
18722 Result = (A != B);
18723 break;
18724 case 5: // _MM_CMPINT_NLT (>=)
18725 Result = IsUnsigned ? A.uge(RHS: B) : A.sge(RHS: B);
18726 break;
18727 case 6: // _MM_CMPINT_NLE (>)
18728 Result = IsUnsigned ? A.ugt(RHS: B) : A.sgt(RHS: B);
18729 break;
18730 case 7: // _MM_CMPINT_TRUE
18731 Result = true;
18732 break;
18733 }
18734
18735 RetMask.setBitVal(BitPosition: ElemNum, BitValue: Mask[ElemNum] && Result);
18736 }
18737
18738 return Success(V: APValue(RetMask), E);
18739 }
18740 case X86::BI__builtin_ia32_cvtss2si:
18741 case X86::BI__builtin_ia32_cvtsd2si:
18742 case X86::BI__builtin_ia32_cvttss2si:
18743 case X86::BI__builtin_ia32_cvttsd2si:
18744 case X86::BI__builtin_ia32_cvtss2si64:
18745 case X86::BI__builtin_ia32_cvtsd2si64:
18746 case X86::BI__builtin_ia32_cvttss2si64:
18747 case X86::BI__builtin_ia32_cvttsd2si64: {
18748 APValue ArgVal;
18749 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: ArgVal))
18750 return false;
18751
18752 assert(ArgVal.isVector() && "Expected a vector argument");
18753 llvm::APFloat FloatElem = ArgVal.getVectorElt(I: 0).getFloat();
18754 unsigned BitWidth = Info.Ctx.getIntWidth(T: E->getType());
18755 bool isUnsigned = E->getType()->isUnsignedIntegerType();
18756
18757 llvm::APSInt IntResult(BitWidth, isUnsigned);
18758 bool IsExact = false;
18759 // We only allow exact conversions so rounding mode does not matter for cvt*
18760 // and cvtt* builtins
18761 FloatElem.convertToInteger(Result&: IntResult, RM: llvm::APFloat::rmTowardZero,
18762 IsExact: &IsExact);
18763 if (!IsExact)
18764 return false;
18765
18766 return Success(SI: IntResult, E);
18767 }
18768 case X86::BI__builtin_ia32_vpshufbitqmb128_mask:
18769 case X86::BI__builtin_ia32_vpshufbitqmb256_mask:
18770 case X86::BI__builtin_ia32_vpshufbitqmb512_mask: {
18771 assert(E->getNumArgs() == 3);
18772
18773 APValue Source, ShuffleMask;
18774 APSInt ZeroMask;
18775 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
18776 !EvaluateVector(E: E->getArg(Arg: 1), Result&: ShuffleMask, Info) ||
18777 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: ZeroMask, Info))
18778 return false;
18779
18780 assert(Source.getVectorLength() == ShuffleMask.getVectorLength());
18781 assert(ZeroMask.getBitWidth() == Source.getVectorLength());
18782
18783 unsigned NumBytesInQWord = 8;
18784 unsigned NumBitsInByte = 8;
18785 unsigned NumBytes = Source.getVectorLength();
18786 unsigned NumQWords = NumBytes / NumBytesInQWord;
18787 unsigned RetWidth = ZeroMask.getBitWidth();
18788 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18789
18790 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
18791 APInt SourceQWord(64, 0);
18792 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18793 uint64_t Byte = Source.getVectorElt(I: QWordId * NumBytesInQWord + ByteIdx)
18794 .getInt()
18795 .getZExtValue();
18796 SourceQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
18797 }
18798
18799 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18800 unsigned SelIdx = QWordId * NumBytesInQWord + ByteIdx;
18801 unsigned M =
18802 ShuffleMask.getVectorElt(I: SelIdx).getInt().getZExtValue() & 0x3F;
18803 if (ZeroMask[SelIdx]) {
18804 RetMask.setBitVal(BitPosition: SelIdx, BitValue: SourceQWord[M]);
18805 }
18806 }
18807 }
18808 return Success(V: APValue(RetMask), E);
18809 }
18810 }
18811}
18812
18813/// Determine whether this is a pointer past the end of the complete
18814/// object referred to by the lvalue.
18815static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx,
18816 const LValue &LV) {
18817 // A null pointer can be viewed as being "past the end" but we don't
18818 // choose to look at it that way here.
18819 if (!LV.getLValueBase())
18820 return false;
18821
18822 // If the designator is valid and refers to a subobject, we're not pointing
18823 // past the end.
18824 if (!LV.getLValueDesignator().Invalid &&
18825 !LV.getLValueDesignator().isOnePastTheEnd())
18826 return false;
18827
18828 // A pointer to an incomplete type might be past-the-end if the type's size is
18829 // zero. We cannot tell because the type is incomplete.
18830 QualType Ty = getType(B: LV.getLValueBase());
18831 if (Ty->isIncompleteType())
18832 return true;
18833
18834 // Can't be past the end of an invalid object.
18835 if (LV.getLValueDesignator().Invalid)
18836 return false;
18837
18838 // We're a past-the-end pointer if we point to the byte after the object,
18839 // no matter what our type or path is.
18840 auto Size = Ctx.getTypeSizeInChars(T: Ty);
18841 return LV.getLValueOffset() == Size;
18842}
18843
18844namespace {
18845
18846/// Data recursive integer evaluator of certain binary operators.
18847///
18848/// We use a data recursive algorithm for binary operators so that we are able
18849/// to handle extreme cases of chained binary operators without causing stack
18850/// overflow.
18851class DataRecursiveIntBinOpEvaluator {
18852 struct EvalResult {
18853 APValue Val;
18854 bool Failed = false;
18855
18856 EvalResult() = default;
18857
18858 void swap(EvalResult &RHS) {
18859 Val.swap(RHS&: RHS.Val);
18860 Failed = RHS.Failed;
18861 RHS.Failed = false;
18862 }
18863 };
18864
18865 struct Job {
18866 const Expr *E;
18867 EvalResult LHSResult; // meaningful only for binary operator expression.
18868 enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
18869
18870 Job() = default;
18871 Job(Job &&) = default;
18872
18873 void startSpeculativeEval(EvalInfo &Info) {
18874 SpecEvalRAII = SpeculativeEvaluationRAII(Info);
18875 }
18876
18877 private:
18878 SpeculativeEvaluationRAII SpecEvalRAII;
18879 };
18880
18881 SmallVector<Job, 16> Queue;
18882
18883 IntExprEvaluator &IntEval;
18884 EvalInfo &Info;
18885 APValue &FinalResult;
18886
18887public:
18888 DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
18889 : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
18890
18891 /// True if \param E is a binary operator that we are going to handle
18892 /// data recursively.
18893 /// We handle binary operators that are comma, logical, or that have operands
18894 /// with integral or enumeration type.
18895 static bool shouldEnqueue(const BinaryOperator *E) {
18896 return E->getOpcode() == BO_Comma || E->isLogicalOp() ||
18897 (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() &&
18898 E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18899 E->getRHS()->getType()->isIntegralOrEnumerationType());
18900 }
18901
18902 bool Traverse(const BinaryOperator *E) {
18903 enqueue(E);
18904 EvalResult PrevResult;
18905 while (!Queue.empty())
18906 process(Result&: PrevResult);
18907
18908 if (PrevResult.Failed) return false;
18909
18910 FinalResult.swap(RHS&: PrevResult.Val);
18911 return true;
18912 }
18913
18914private:
18915 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
18916 return IntEval.Success(Value, E, Result);
18917 }
18918 bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
18919 return IntEval.Success(SI: Value, E, Result);
18920 }
18921 bool Error(const Expr *E) {
18922 return IntEval.Error(E);
18923 }
18924 bool Error(const Expr *E, diag::kind D) {
18925 return IntEval.Error(E, D);
18926 }
18927
18928 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
18929 return Info.CCEDiag(E, DiagId: D);
18930 }
18931
18932 // Returns true if visiting the RHS is necessary, false otherwise.
18933 bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
18934 bool &SuppressRHSDiags);
18935
18936 bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
18937 const BinaryOperator *E, APValue &Result);
18938
18939 void EvaluateExpr(const Expr *E, EvalResult &Result) {
18940 Result.Failed = !Evaluate(Result&: Result.Val, Info, E);
18941 if (Result.Failed)
18942 Result.Val = APValue();
18943 }
18944
18945 void process(EvalResult &Result);
18946
18947 void enqueue(const Expr *E) {
18948 E = E->IgnoreParens();
18949 Queue.resize(N: Queue.size()+1);
18950 Queue.back().E = E;
18951 Queue.back().Kind = Job::AnyExprKind;
18952 }
18953};
18954
18955}
18956
18957bool DataRecursiveIntBinOpEvaluator::
18958 VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
18959 bool &SuppressRHSDiags) {
18960 if (E->getOpcode() == BO_Comma) {
18961 // Ignore LHS but note if we could not evaluate it.
18962 if (LHSResult.Failed)
18963 return Info.noteSideEffect();
18964 return true;
18965 }
18966
18967 if (E->isLogicalOp()) {
18968 bool LHSAsBool;
18969 if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) {
18970 // We were able to evaluate the LHS, see if we can get away with not
18971 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
18972 if (LHSAsBool == (E->getOpcode() == BO_LOr)) {
18973 Success(Value: LHSAsBool, E, Result&: LHSResult.Val);
18974 return false; // Ignore RHS
18975 }
18976 } else {
18977 LHSResult.Failed = true;
18978
18979 // Since we weren't able to evaluate the left hand side, it
18980 // might have had side effects.
18981 if (!Info.noteSideEffect())
18982 return false;
18983
18984 // We can't evaluate the LHS; however, sometimes the result
18985 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
18986 // Don't ignore RHS and suppress diagnostics from this arm.
18987 SuppressRHSDiags = true;
18988 }
18989
18990 return true;
18991 }
18992
18993 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18994 E->getRHS()->getType()->isIntegralOrEnumerationType());
18995
18996 if (LHSResult.Failed && !Info.noteFailure())
18997 return false; // Ignore RHS;
18998
18999 return true;
19000}
19001
19002static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index,
19003 bool IsSub) {
19004 // Compute the new offset in the appropriate width, wrapping at 64 bits.
19005 // FIXME: When compiling for a 32-bit target, we should use 32-bit
19006 // offsets.
19007 assert(!LVal.hasLValuePath() && "have designator for integer lvalue");
19008 CharUnits &Offset = LVal.getLValueOffset();
19009 uint64_t Offset64 = Offset.getQuantity();
19010 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
19011 Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64
19012 : Offset64 + Index64);
19013}
19014
19015bool DataRecursiveIntBinOpEvaluator::
19016 VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
19017 const BinaryOperator *E, APValue &Result) {
19018 if (E->getOpcode() == BO_Comma) {
19019 if (RHSResult.Failed)
19020 return false;
19021 Result = RHSResult.Val;
19022 return true;
19023 }
19024
19025 if (E->isLogicalOp()) {
19026 bool lhsResult, rhsResult;
19027 bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult);
19028 bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult);
19029
19030 if (LHSIsOK) {
19031 if (RHSIsOK) {
19032 if (E->getOpcode() == BO_LOr)
19033 return Success(Value: lhsResult || rhsResult, E, Result);
19034 else
19035 return Success(Value: lhsResult && rhsResult, E, Result);
19036 }
19037 } else {
19038 if (RHSIsOK) {
19039 // We can't evaluate the LHS; however, sometimes the result
19040 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
19041 if (rhsResult == (E->getOpcode() == BO_LOr))
19042 return Success(Value: rhsResult, E, Result);
19043 }
19044 }
19045
19046 return false;
19047 }
19048
19049 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
19050 E->getRHS()->getType()->isIntegralOrEnumerationType());
19051
19052 if (LHSResult.Failed || RHSResult.Failed)
19053 return false;
19054
19055 const APValue &LHSVal = LHSResult.Val;
19056 const APValue &RHSVal = RHSResult.Val;
19057
19058 // Handle cases like (unsigned long)&a + 4.
19059 if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
19060 Result = LHSVal;
19061 addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub);
19062 return true;
19063 }
19064
19065 // Handle cases like 4 + (unsigned long)&a
19066 if (E->getOpcode() == BO_Add &&
19067 RHSVal.isLValue() && LHSVal.isInt()) {
19068 Result = RHSVal;
19069 addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false);
19070 return true;
19071 }
19072
19073 if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
19074 // Handle (intptr_t)&&A - (intptr_t)&&B.
19075 if (!LHSVal.getLValueOffset().isZero() ||
19076 !RHSVal.getLValueOffset().isZero())
19077 return false;
19078 const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
19079 const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
19080 if (!LHSExpr || !RHSExpr)
19081 return false;
19082 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19083 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19084 if (!LHSAddrExpr || !RHSAddrExpr)
19085 return false;
19086 // Make sure both labels come from the same function.
19087 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19088 RHSAddrExpr->getLabel()->getDeclContext())
19089 return false;
19090 Result = APValue(LHSAddrExpr, RHSAddrExpr);
19091 return true;
19092 }
19093
19094 // All the remaining cases expect both operands to be an integer
19095 if (!LHSVal.isInt() || !RHSVal.isInt())
19096 return Error(E);
19097
19098 // Set up the width and signedness manually, in case it can't be deduced
19099 // from the operation we're performing.
19100 // FIXME: Don't do this in the cases where we can deduce it.
19101 APSInt Value(Info.Ctx.getIntWidth(T: E->getType()),
19102 E->getType()->isUnsignedIntegerOrEnumerationType());
19103 if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(),
19104 RHS: RHSVal.getInt(), Result&: Value))
19105 return false;
19106 return Success(Value, E, Result);
19107}
19108
19109void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
19110 Job &job = Queue.back();
19111
19112 switch (job.Kind) {
19113 case Job::AnyExprKind: {
19114 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) {
19115 if (shouldEnqueue(E: Bop)) {
19116 job.Kind = Job::BinOpKind;
19117 enqueue(E: Bop->getLHS());
19118 return;
19119 }
19120 }
19121
19122 EvaluateExpr(E: job.E, Result);
19123 Queue.pop_back();
19124 return;
19125 }
19126
19127 case Job::BinOpKind: {
19128 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19129 bool SuppressRHSDiags = false;
19130 if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) {
19131 Queue.pop_back();
19132 return;
19133 }
19134 if (SuppressRHSDiags)
19135 job.startSpeculativeEval(Info);
19136 job.LHSResult.swap(RHS&: Result);
19137 job.Kind = Job::BinOpVisitedLHSKind;
19138 enqueue(E: Bop->getRHS());
19139 return;
19140 }
19141
19142 case Job::BinOpVisitedLHSKind: {
19143 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
19144 EvalResult RHS;
19145 RHS.swap(RHS&: Result);
19146 Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val);
19147 Queue.pop_back();
19148 return;
19149 }
19150 }
19151
19152 llvm_unreachable("Invalid Job::Kind!");
19153}
19154
19155namespace {
19156enum class CmpResult {
19157 Unequal,
19158 Less,
19159 Equal,
19160 Greater,
19161 Unordered,
19162};
19163}
19164
19165template <class SuccessCB, class AfterCB>
19166static bool
19167EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
19168 SuccessCB &&Success, AfterCB &&DoAfter) {
19169 assert(!E->isValueDependent());
19170 assert(E->isComparisonOp() && "expected comparison operator");
19171 assert((E->getOpcode() == BO_Cmp ||
19172 E->getType()->isIntegralOrEnumerationType()) &&
19173 "unsupported binary expression evaluation");
19174 auto Error = [&](const Expr *E) {
19175 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
19176 return false;
19177 };
19178
19179 bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp;
19180 bool IsEquality = E->isEqualityOp();
19181
19182 QualType LHSTy = E->getLHS()->getType();
19183 QualType RHSTy = E->getRHS()->getType();
19184
19185 if (LHSTy->isIntegralOrEnumerationType() &&
19186 RHSTy->isIntegralOrEnumerationType()) {
19187 APSInt LHS, RHS;
19188 bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info);
19189 if (!LHSOK && !Info.noteFailure())
19190 return false;
19191 if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
19192 return false;
19193 if (LHS < RHS)
19194 return Success(CmpResult::Less, E);
19195 if (LHS > RHS)
19196 return Success(CmpResult::Greater, E);
19197 return Success(CmpResult::Equal, E);
19198 }
19199
19200 if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) {
19201 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy));
19202 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy));
19203
19204 bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info);
19205 if (!LHSOK && !Info.noteFailure())
19206 return false;
19207 if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK)
19208 return false;
19209 if (LHSFX < RHSFX)
19210 return Success(CmpResult::Less, E);
19211 if (LHSFX > RHSFX)
19212 return Success(CmpResult::Greater, E);
19213 return Success(CmpResult::Equal, E);
19214 }
19215
19216 if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) {
19217 ComplexValue LHS, RHS;
19218 bool LHSOK;
19219 if (E->isAssignmentOp()) {
19220 LValue LV;
19221 EvaluateLValue(E: E->getLHS(), Result&: LV, Info);
19222 LHSOK = false;
19223 } else if (LHSTy->isRealFloatingType()) {
19224 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info);
19225 if (LHSOK) {
19226 LHS.makeComplexFloat();
19227 LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics());
19228 }
19229 } else {
19230 LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info);
19231 }
19232 if (!LHSOK && !Info.noteFailure())
19233 return false;
19234
19235 if (E->getRHS()->getType()->isRealFloatingType()) {
19236 if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK)
19237 return false;
19238 RHS.makeComplexFloat();
19239 RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics());
19240 } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK)
19241 return false;
19242
19243 if (LHS.isComplexFloat()) {
19244 APFloat::cmpResult CR_r =
19245 LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal());
19246 APFloat::cmpResult CR_i =
19247 LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag());
19248 bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual;
19249 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19250 } else {
19251 assert(IsEquality && "invalid complex comparison");
19252 bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
19253 LHS.getComplexIntImag() == RHS.getComplexIntImag();
19254 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
19255 }
19256 }
19257
19258 if (LHSTy->isRealFloatingType() &&
19259 RHSTy->isRealFloatingType()) {
19260 APFloat RHS(0.0), LHS(0.0);
19261
19262 bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info);
19263 if (!LHSOK && !Info.noteFailure())
19264 return false;
19265
19266 if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK)
19267 return false;
19268
19269 assert(E->isComparisonOp() && "Invalid binary operator!");
19270 llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS);
19271 if (!Info.InConstantContext &&
19272 APFloatCmpResult == APFloat::cmpUnordered &&
19273 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) {
19274 // Note: Compares may raise invalid in some cases involving NaN or sNaN.
19275 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
19276 return false;
19277 }
19278 auto GetCmpRes = [&]() {
19279 switch (APFloatCmpResult) {
19280 case APFloat::cmpEqual:
19281 return CmpResult::Equal;
19282 case APFloat::cmpLessThan:
19283 return CmpResult::Less;
19284 case APFloat::cmpGreaterThan:
19285 return CmpResult::Greater;
19286 case APFloat::cmpUnordered:
19287 return CmpResult::Unordered;
19288 }
19289 llvm_unreachable("Unrecognised APFloat::cmpResult enum");
19290 };
19291 return Success(GetCmpRes(), E);
19292 }
19293
19294 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
19295 LValue LHSValue, RHSValue;
19296
19297 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19298 if (!LHSOK && !Info.noteFailure())
19299 return false;
19300
19301 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19302 return false;
19303
19304 // Reject differing bases from the normal codepath; we special-case
19305 // comparisons to null.
19306 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19307 // Bail out early if we're checking potential constant expression.
19308 // Otherwise, prefer to diagnose other issues.
19309 if (Info.checkingPotentialConstantExpression() &&
19310 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19311 return false;
19312 auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) {
19313 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19314 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19315 Info.FFDiag(E, DiagId: DiagID)
19316 << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS);
19317 return false;
19318 };
19319 // Inequalities and subtractions between unrelated pointers have
19320 // unspecified or undefined behavior.
19321 if (!IsEquality)
19322 return DiagComparison(
19323 diag::note_constexpr_pointer_comparison_unspecified);
19324 // A constant address may compare equal to the address of a symbol.
19325 // The one exception is that address of an object cannot compare equal
19326 // to a null pointer constant.
19327 // TODO: Should we restrict this to actual null pointers, and exclude the
19328 // case of zero cast to pointer type?
19329 if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
19330 (!RHSValue.Base && !RHSValue.Offset.isZero()))
19331 return DiagComparison(diag::note_constexpr_pointer_constant_comparison,
19332 !RHSValue.Base);
19333 // C++2c [intro.object]/10:
19334 // Two objects [...] may have the same address if [...] they are both
19335 // potentially non-unique objects.
19336 // C++2c [intro.object]/9:
19337 // An object is potentially non-unique if it is a string literal object,
19338 // the backing array of an initializer list, or a subobject thereof.
19339 //
19340 // This makes the comparison result unspecified, so it's not a constant
19341 // expression.
19342 //
19343 // TODO: Do we need to handle the initializer list case here?
19344 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19345 return DiagComparison(diag::note_constexpr_literal_comparison);
19346 if (IsOpaqueConstantCall(LVal: LHSValue) || IsOpaqueConstantCall(LVal: RHSValue))
19347 return DiagComparison(diag::note_constexpr_opaque_call_comparison,
19348 !IsOpaqueConstantCall(LVal: LHSValue));
19349 // We can't tell whether weak symbols will end up pointing to the same
19350 // object.
19351 if (IsWeakLValue(Value: LHSValue) || IsWeakLValue(Value: RHSValue))
19352 return DiagComparison(diag::note_constexpr_pointer_weak_comparison,
19353 !IsWeakLValue(Value: LHSValue));
19354 // We can't compare the address of the start of one object with the
19355 // past-the-end address of another object, per C++ DR1652.
19356 if (LHSValue.Base && LHSValue.Offset.isZero() &&
19357 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: RHSValue))
19358 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19359 true);
19360 if (RHSValue.Base && RHSValue.Offset.isZero() &&
19361 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: LHSValue))
19362 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
19363 false);
19364 // We can't tell whether an object is at the same address as another
19365 // zero sized object.
19366 if ((RHSValue.Base && isZeroSized(Value: LHSValue)) ||
19367 (LHSValue.Base && isZeroSized(Value: RHSValue)))
19368 return DiagComparison(
19369 diag::note_constexpr_pointer_comparison_zero_sized);
19370 if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)
19371 return DiagComparison(
19372 diag::note_constexpr_pointer_comparison_unspecified);
19373 // FIXME: Verify both variables are live.
19374 return Success(CmpResult::Unequal, E);
19375 }
19376
19377 const CharUnits &LHSOffset = LHSValue.getLValueOffset();
19378 const CharUnits &RHSOffset = RHSValue.getLValueOffset();
19379
19380 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19381 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19382
19383 // C++11 [expr.rel]p2:
19384 // - If two pointers point to non-static data members of the same object,
19385 // or to subobjects or array elements fo such members, recursively, the
19386 // pointer to the later declared member compares greater provided the
19387 // two members have the same access control and provided their class is
19388 // not a union.
19389 // [...]
19390 // - Otherwise pointer comparisons are unspecified.
19391 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
19392 bool WasArrayIndex;
19393 unsigned Mismatch = FindDesignatorMismatch(
19394 ObjType: LHSValue.Base.isNull() ? QualType()
19395 : getType(B: LHSValue.Base).getNonReferenceType(),
19396 A: LHSDesignator, B: RHSDesignator, WasArrayIndex);
19397 // At the point where the designators diverge, the comparison has a
19398 // specified value if:
19399 // - we are comparing array indices
19400 // - we are comparing fields of a union, or fields with the same access
19401 // Otherwise, the result is unspecified and thus the comparison is not a
19402 // constant expression.
19403 if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
19404 Mismatch < RHSDesignator.Entries.size()) {
19405 const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]);
19406 const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]);
19407 if (!LF && !RF)
19408 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_classes);
19409 else if (!LF)
19410 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19411 << getAsBaseClass(E: LHSDesignator.Entries[Mismatch])
19412 << RF->getParent() << RF;
19413 else if (!RF)
19414 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
19415 << getAsBaseClass(E: RHSDesignator.Entries[Mismatch])
19416 << LF->getParent() << LF;
19417 else if (!LF->getParent()->isUnion() &&
19418 LF->getAccess() != RF->getAccess())
19419 Info.CCEDiag(E,
19420 DiagId: diag::note_constexpr_pointer_comparison_differing_access)
19421 << LF << LF->getAccess() << RF << RF->getAccess()
19422 << LF->getParent();
19423 }
19424 }
19425
19426 // The comparison here must be unsigned, and performed with the same
19427 // width as the pointer.
19428 unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy);
19429 uint64_t CompareLHS = LHSOffset.getQuantity();
19430 uint64_t CompareRHS = RHSOffset.getQuantity();
19431 assert(PtrSize <= 64 && "Unexpected pointer width");
19432 uint64_t Mask = ~0ULL >> (64 - PtrSize);
19433 CompareLHS &= Mask;
19434 CompareRHS &= Mask;
19435
19436 // If there is a base and this is a relational operator, we can only
19437 // compare pointers within the object in question; otherwise, the result
19438 // depends on where the object is located in memory.
19439 if (!LHSValue.Base.isNull() && IsRelational) {
19440 QualType BaseTy = getType(B: LHSValue.Base).getNonReferenceType();
19441 if (BaseTy->isIncompleteType())
19442 return Error(E);
19443 CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy);
19444 uint64_t OffsetLimit = Size.getQuantity();
19445 if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
19446 return Error(E);
19447 }
19448
19449 if (CompareLHS < CompareRHS)
19450 return Success(CmpResult::Less, E);
19451 if (CompareLHS > CompareRHS)
19452 return Success(CmpResult::Greater, E);
19453 return Success(CmpResult::Equal, E);
19454 }
19455
19456 if (LHSTy->isMemberPointerType()) {
19457 assert(IsEquality && "unexpected member pointer operation");
19458 assert(RHSTy->isMemberPointerType() && "invalid comparison");
19459
19460 MemberPtr LHSValue, RHSValue;
19461
19462 bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info);
19463 if (!LHSOK && !Info.noteFailure())
19464 return false;
19465
19466 if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19467 return false;
19468
19469 // If either operand is a pointer to a weak function, the comparison is not
19470 // constant.
19471 if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) {
19472 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19473 << LHSValue.getDecl();
19474 return false;
19475 }
19476 if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) {
19477 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19478 << RHSValue.getDecl();
19479 return false;
19480 }
19481
19482 // C++11 [expr.eq]p2:
19483 // If both operands are null, they compare equal. Otherwise if only one is
19484 // null, they compare unequal.
19485 if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
19486 bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
19487 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19488 }
19489
19490 // Otherwise if either is a pointer to a virtual member function, the
19491 // result is unspecified.
19492 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: LHSValue.getDecl()))
19493 if (MD->isVirtual())
19494 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19495 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: RHSValue.getDecl()))
19496 if (MD->isVirtual())
19497 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19498
19499 // Otherwise they compare equal if and only if they would refer to the
19500 // same member of the same most derived object or the same subobject if
19501 // they were dereferenced with a hypothetical object of the associated
19502 // class type.
19503 bool Equal = LHSValue == RHSValue;
19504 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19505 }
19506
19507 if (LHSTy->isNullPtrType()) {
19508 assert(E->isComparisonOp() && "unexpected nullptr operation");
19509 assert(RHSTy->isNullPtrType() && "missing pointer conversion");
19510 // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
19511 // are compared, the result is true of the operator is <=, >= or ==, and
19512 // false otherwise.
19513 LValue Res;
19514 if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) ||
19515 !EvaluatePointer(E: E->getRHS(), Result&: Res, Info))
19516 return false;
19517 return Success(CmpResult::Equal, E);
19518 }
19519
19520 return DoAfter();
19521}
19522
19523static bool EvaluateComparisonResult(EvalInfo &Info, const Expr *E,
19524 ComparisonCategoryResult CCR,
19525 APValue &Result) {
19526 const ComparisonCategoryInfo &CmpInfo =
19527 Info.Ctx.CompCategories.getInfoForType(Ty: E->getType());
19528 const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD;
19529
19530 // Check and evaluate the result as a constant expression.
19531 LValue LV;
19532 LV.set(B: VD);
19533 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
19534 return false;
19535 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
19536 Kind: ConstantExprKind::Normal);
19537}
19538
19539bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) {
19540 if (!CheckLiteralType(Info, E))
19541 return false;
19542
19543 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19544 ComparisonCategoryResult CCR;
19545 switch (CR) {
19546 case CmpResult::Unequal:
19547 llvm_unreachable("should never produce Unequal for three-way comparison");
19548 case CmpResult::Less:
19549 CCR = ComparisonCategoryResult::Less;
19550 break;
19551 case CmpResult::Equal:
19552 CCR = ComparisonCategoryResult::Equal;
19553 break;
19554 case CmpResult::Greater:
19555 CCR = ComparisonCategoryResult::Greater;
19556 break;
19557 case CmpResult::Unordered:
19558 CCR = ComparisonCategoryResult::Unordered;
19559 break;
19560 }
19561 return EvaluateComparisonResult(Info, E, CCR, Result);
19562 };
19563 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19564 return ExprEvaluatorBaseTy::VisitBinCmp(S: E);
19565 });
19566}
19567
19568bool RecordExprEvaluator::VisitTypeTraitExpr(const TypeTraitExpr *E) {
19569 if (!CheckLiteralType(Info, E))
19570 return false;
19571
19572 assert(E->isStoredAsComparisonResult() &&
19573 "expected a strong_ordering type trait with a stored value");
19574
19575 ComparisonCategoryResult CCR = static_cast<ComparisonCategoryResult>(
19576 E->getAPValue().getInt().getZExtValue());
19577 return EvaluateComparisonResult(Info, E, CCR, Result);
19578}
19579
19580bool RecordExprEvaluator::VisitCXXParenListInitExpr(
19581 const CXXParenListInitExpr *E) {
19582 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs());
19583}
19584
19585bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
19586 // We don't support assignment in C. C++ assignments don't get here because
19587 // assignment is an lvalue in C++.
19588 if (E->isAssignmentOp()) {
19589 Error(E);
19590 if (!Info.noteFailure())
19591 return false;
19592 }
19593
19594 if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
19595 return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
19596
19597 assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() ||
19598 !E->getRHS()->getType()->isIntegralOrEnumerationType()) &&
19599 "DataRecursiveIntBinOpEvaluator should have handled integral types");
19600
19601 if (E->isComparisonOp()) {
19602 // Evaluate builtin binary comparisons by evaluating them as three-way
19603 // comparisons and then translating the result.
19604 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19605 assert((CR != CmpResult::Unequal || E->isEqualityOp()) &&
19606 "should only produce Unequal for equality comparisons");
19607 bool IsEqual = CR == CmpResult::Equal,
19608 IsLess = CR == CmpResult::Less,
19609 IsGreater = CR == CmpResult::Greater;
19610 auto Op = E->getOpcode();
19611 switch (Op) {
19612 default:
19613 llvm_unreachable("unsupported binary operator");
19614 case BO_EQ:
19615 case BO_NE:
19616 return Success(Value: IsEqual == (Op == BO_EQ), E);
19617 case BO_LT:
19618 return Success(Value: IsLess, E);
19619 case BO_GT:
19620 return Success(Value: IsGreater, E);
19621 case BO_LE:
19622 return Success(Value: IsEqual || IsLess, E);
19623 case BO_GE:
19624 return Success(Value: IsEqual || IsGreater, E);
19625 }
19626 };
19627 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19628 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19629 });
19630 }
19631
19632 QualType LHSTy = E->getLHS()->getType();
19633 QualType RHSTy = E->getRHS()->getType();
19634
19635 if (LHSTy->isPointerType() && RHSTy->isPointerType() &&
19636 E->getOpcode() == BO_Sub) {
19637 LValue LHSValue, RHSValue;
19638
19639 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19640 if (!LHSOK && !Info.noteFailure())
19641 return false;
19642
19643 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19644 return false;
19645
19646 // Reject differing bases from the normal codepath; we special-case
19647 // comparisons to null.
19648 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19649 if (Info.checkingPotentialConstantExpression() &&
19650 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19651 return false;
19652
19653 const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
19654 const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();
19655
19656 auto DiagArith = [&](unsigned DiagID) {
19657 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19658 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19659 Info.FFDiag(E, DiagId: DiagID) << LHS << RHS;
19660 if (LHSExpr && LHSExpr == RHSExpr)
19661 Info.Note(Loc: LHSExpr->getExprLoc(),
19662 DiagId: diag::note_constexpr_repeated_literal_eval)
19663 << LHSExpr->getSourceRange();
19664 return false;
19665 };
19666
19667 if (!LHSExpr || !RHSExpr)
19668 return DiagArith(diag::note_constexpr_pointer_arith_unspecified);
19669
19670 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19671 return DiagArith(diag::note_constexpr_literal_arith);
19672
19673 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19674 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19675 if (!LHSAddrExpr || !RHSAddrExpr)
19676 return Error(E);
19677 // Make sure both labels come from the same function.
19678 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19679 RHSAddrExpr->getLabel()->getDeclContext())
19680 return Error(E);
19681 return Success(V: APValue(LHSAddrExpr, RHSAddrExpr), E);
19682 }
19683 const CharUnits &LHSOffset = LHSValue.getLValueOffset();
19684 const CharUnits &RHSOffset = RHSValue.getLValueOffset();
19685
19686 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19687 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19688
19689 // C++11 [expr.add]p6:
19690 // Unless both pointers point to elements of the same array object, or
19691 // one past the last element of the array object, the behavior is
19692 // undefined.
19693 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
19694 !AreElementsOfSameArray(ObjType: getType(B: LHSValue.Base), A: LHSDesignator,
19695 B: RHSDesignator))
19696 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_not_same_array);
19697
19698 QualType Type = E->getLHS()->getType();
19699 QualType ElementType = Type->castAs<PointerType>()->getPointeeType();
19700
19701 CharUnits ElementSize;
19702 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize))
19703 return false;
19704
19705 // As an extension, a type may have zero size (empty struct or union in
19706 // C, array of zero length). Pointer subtraction in such cases has
19707 // undefined behavior, so is not constant.
19708 if (ElementSize.isZero()) {
19709 Info.FFDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_zero_size)
19710 << ElementType;
19711 return false;
19712 }
19713
19714 // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
19715 // and produce incorrect results when it overflows. Such behavior
19716 // appears to be non-conforming, but is common, so perhaps we should
19717 // assume the standard intended for such cases to be undefined behavior
19718 // and check for them.
19719
19720 // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
19721 // overflow in the final conversion to ptrdiff_t.
19722 APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
19723 APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
19724 APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true),
19725 false);
19726 APSInt TrueResult = (LHS - RHS) / ElemSize;
19727 APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType()));
19728
19729 if (Result.extend(width: 65) != TrueResult &&
19730 !HandleOverflow(Info, E, SrcValue: TrueResult, DestType: E->getType()))
19731 return false;
19732 return Success(SI: Result, E);
19733 }
19734
19735 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19736}
19737
19738/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
19739/// a result as the expression's type.
19740bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
19741 const UnaryExprOrTypeTraitExpr *E) {
19742 switch(E->getKind()) {
19743 case UETT_PreferredAlignOf:
19744 case UETT_AlignOf: {
19745 if (E->isArgumentType())
19746 return Success(
19747 Size: GetAlignOfType(Ctx: Info.Ctx, T: E->getArgumentType(), ExprKind: E->getKind()), E);
19748 else
19749 return Success(
19750 Size: GetAlignOfExpr(Ctx: Info.Ctx, E: E->getArgumentExpr(), ExprKind: E->getKind()), E);
19751 }
19752
19753 case UETT_PtrAuthTypeDiscriminator: {
19754 if (E->getArgumentType()->isDependentType())
19755 return false;
19756 return Success(
19757 Value: Info.Ctx.getPointerAuthTypeDiscriminator(T: E->getArgumentType()), E);
19758 }
19759 case UETT_VecStep: {
19760 QualType Ty = E->getTypeOfArgument();
19761
19762 if (Ty->isVectorType()) {
19763 unsigned n = Ty->castAs<VectorType>()->getNumElements();
19764
19765 // The vec_step built-in functions that take a 3-component
19766 // vector return 4. (OpenCL 1.1 spec 6.11.12)
19767 if (n == 3)
19768 n = 4;
19769
19770 return Success(Value: n, E);
19771 } else
19772 return Success(Value: 1, E);
19773 }
19774
19775 case UETT_DataSizeOf:
19776 case UETT_SizeOf: {
19777 QualType SrcTy = E->getTypeOfArgument();
19778 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
19779 // the result is the size of the referenced type."
19780 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
19781 SrcTy = Ref->getPointeeType();
19782
19783 CharUnits Sizeof;
19784 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: SrcTy, Size&: Sizeof,
19785 SOT: E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf
19786 : SizeOfType::SizeOf)) {
19787 return false;
19788 }
19789 return Success(Size: Sizeof, E);
19790 }
19791 case UETT_OpenMPRequiredSimdAlign:
19792 assert(E->isArgumentType());
19793 return Success(
19794 Value: Info.Ctx.toCharUnitsFromBits(
19795 BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType()))
19796 .getQuantity(),
19797 E);
19798 case UETT_VectorElements: {
19799 QualType Ty = E->getTypeOfArgument();
19800 // If the vector has a fixed size, we can determine the number of elements
19801 // at compile time.
19802 if (const auto *VT = Ty->getAs<VectorType>())
19803 return Success(Value: VT->getNumElements(), E);
19804
19805 assert(Ty->isSizelessVectorType());
19806 if (Info.InConstantContext)
19807 Info.CCEDiag(E, DiagId: diag::note_constexpr_non_const_vectorelements)
19808 << E->getSourceRange();
19809
19810 return false;
19811 }
19812 case UETT_CountOf: {
19813 QualType Ty = E->getTypeOfArgument();
19814 assert(Ty->isArrayType());
19815
19816 // We don't need to worry about array element qualifiers, so getting the
19817 // unsafe array type is fine.
19818 if (const auto *CAT =
19819 dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) {
19820 return Success(I: CAT->getSize(), E);
19821 }
19822
19823 assert(!Ty->isConstantSizeType());
19824
19825 // If it's a variable-length array type, we need to check whether it is a
19826 // multidimensional array. If so, we need to check the size expression of
19827 // the VLA to see if it's a constant size. If so, we can return that value.
19828 const auto *VAT = Info.Ctx.getAsVariableArrayType(T: Ty);
19829 assert(VAT);
19830 if (VAT->getElementType()->isArrayType()) {
19831 // Variable array size expression could be missing (e.g. int a[*][10]) In
19832 // that case, it can't be a constant expression.
19833 if (!VAT->getSizeExpr()) {
19834 Info.FFDiag(Loc: E->getBeginLoc());
19835 return false;
19836 }
19837
19838 std::optional<APSInt> Res =
19839 VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: Info.Ctx);
19840 if (Res) {
19841 // The resulting value always has type size_t, so we need to make the
19842 // returned APInt have the correct sign and bit-width.
19843 APInt Val{
19844 static_cast<unsigned>(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType())),
19845 Res->getZExtValue()};
19846 return Success(I: Val, E);
19847 }
19848 }
19849
19850 // Definitely a variable-length type, which is not an ICE.
19851 // FIXME: Better diagnostic.
19852 Info.FFDiag(Loc: E->getBeginLoc());
19853 return false;
19854 }
19855 }
19856
19857 llvm_unreachable("unknown expr/type trait");
19858}
19859
19860bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
19861 Info.Ctx.recordOffsetOfEvaluation(E: OOE);
19862 CharUnits Result;
19863 unsigned n = OOE->getNumComponents();
19864 if (n == 0)
19865 return Error(E: OOE);
19866 QualType CurrentType = OOE->getTypeSourceInfo()->getType();
19867 for (unsigned i = 0; i != n; ++i) {
19868 OffsetOfNode ON = OOE->getComponent(Idx: i);
19869 switch (ON.getKind()) {
19870 case OffsetOfNode::Array: {
19871 const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex());
19872 APSInt IdxResult;
19873 if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info))
19874 return false;
19875 const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType);
19876 if (!AT)
19877 return Error(E: OOE);
19878 CurrentType = AT->getElementType();
19879 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType);
19880 // Reject negative indices, indices too large to fit in int64_t,
19881 // and overflow in the offset computation.
19882 if (IdxResult.isNegative() || IdxResult.getActiveBits() > 63)
19883 return Error(E: OOE);
19884 int64_t IdxVal = IdxResult.getExtValue();
19885 int64_t ElemSize = ElementSize.getQuantity();
19886 if (IdxVal != 0 &&
19887 ElemSize > std::numeric_limits<int64_t>::max() / IdxVal)
19888 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
19889 int64_t Offset = IdxVal * ElemSize;
19890 if (Result.getQuantity() > std::numeric_limits<int64_t>::max() - Offset)
19891 return Error(E: OOE, D: diag::note_constexpr_offsetof_overflow);
19892 Result += CharUnits::fromQuantity(Quantity: Offset);
19893 break;
19894 }
19895
19896 case OffsetOfNode::Field: {
19897 FieldDecl *MemberDecl = ON.getField();
19898 const auto *RD = CurrentType->getAsRecordDecl();
19899 if (!RD)
19900 return Error(E: OOE);
19901 if (RD->isInvalidDecl()) return false;
19902 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
19903 unsigned i = MemberDecl->getFieldIndex();
19904 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
19905 Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i));
19906 CurrentType = MemberDecl->getType().getNonReferenceType();
19907 break;
19908 }
19909
19910 case OffsetOfNode::Identifier:
19911 llvm_unreachable("dependent __builtin_offsetof");
19912
19913 case OffsetOfNode::Base: {
19914 CXXBaseSpecifier *BaseSpec = ON.getBase();
19915 if (BaseSpec->isVirtual())
19916 return Error(E: OOE);
19917
19918 // Find the layout of the class whose base we are looking into.
19919 const auto *RD = CurrentType->getAsCXXRecordDecl();
19920 if (!RD)
19921 return Error(E: OOE);
19922 if (RD->isInvalidDecl()) return false;
19923 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
19924
19925 // Find the base class itself.
19926 CurrentType = BaseSpec->getType();
19927 const auto *BaseRD = CurrentType->getAsCXXRecordDecl();
19928 if (!BaseRD)
19929 return Error(E: OOE);
19930
19931 // Add the offset to the base.
19932 Result += RL.getBaseClassOffset(Base: BaseRD);
19933 break;
19934 }
19935 }
19936 }
19937 return Success(Size: Result, E: OOE);
19938}
19939
19940bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
19941 switch (E->getOpcode()) {
19942 default:
19943 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
19944 // See C99 6.6p3.
19945 return Error(E);
19946 case UO_Extension:
19947 // FIXME: Should extension allow i-c-e extension expressions in its scope?
19948 // If so, we could clear the diagnostic ID.
19949 return Visit(S: E->getSubExpr());
19950 case UO_Plus:
19951 // The result is just the value.
19952 return Visit(S: E->getSubExpr());
19953 case UO_Minus: {
19954 if (!Visit(S: E->getSubExpr()))
19955 return false;
19956 if (!Result.isInt()) return Error(E);
19957 const APSInt &Value = Result.getInt();
19958 if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() &&
19959 !E->getType().isWrapType()) {
19960 if (Info.checkingForUndefinedBehavior())
19961 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
19962 DiagID: diag::warn_integer_constant_overflow)
19963 << toString(I: Value, Radix: 10, Signed: Value.isSigned(), /*formatAsCLiteral=*/false,
19964 /*UpperCase=*/true, /*InsertSeparators=*/true)
19965 << E->getType() << E->getSourceRange();
19966
19967 if (!HandleOverflow(Info, E, SrcValue: -Value.extend(width: Value.getBitWidth() + 1),
19968 DestType: E->getType()))
19969 return false;
19970 }
19971 return Success(SI: -Value, E);
19972 }
19973 case UO_Not: {
19974 if (!Visit(S: E->getSubExpr()))
19975 return false;
19976 if (!Result.isInt()) return Error(E);
19977 return Success(SI: ~Result.getInt(), E);
19978 }
19979 case UO_LNot: {
19980 bool bres;
19981 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
19982 return false;
19983 return Success(Value: !bres, E);
19984 }
19985 }
19986}
19987
19988/// HandleCast - This is used to evaluate implicit or explicit casts where the
19989/// result type is integer.
19990bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
19991 const Expr *SubExpr = E->getSubExpr();
19992 QualType DestType = E->getType();
19993 QualType SrcType = SubExpr->getType();
19994
19995 switch (E->getCastKind()) {
19996 case CK_BaseToDerived:
19997 case CK_DerivedToBase:
19998 case CK_UncheckedDerivedToBase:
19999 case CK_Dynamic:
20000 case CK_ToUnion:
20001 case CK_ArrayToPointerDecay:
20002 case CK_FunctionToPointerDecay:
20003 case CK_NullToPointer:
20004 case CK_NullToMemberPointer:
20005 case CK_BaseToDerivedMemberPointer:
20006 case CK_DerivedToBaseMemberPointer:
20007 case CK_ReinterpretMemberPointer:
20008 case CK_ConstructorConversion:
20009 case CK_IntegralToPointer:
20010 case CK_ToVoid:
20011 case CK_VectorSplat:
20012 case CK_IntegralToFloating:
20013 case CK_FloatingCast:
20014 case CK_CPointerToObjCPointerCast:
20015 case CK_BlockPointerToObjCPointerCast:
20016 case CK_AnyPointerToBlockPointerCast:
20017 case CK_ObjCObjectLValueCast:
20018 case CK_FloatingRealToComplex:
20019 case CK_FloatingComplexToReal:
20020 case CK_FloatingComplexCast:
20021 case CK_FloatingComplexToIntegralComplex:
20022 case CK_IntegralRealToComplex:
20023 case CK_IntegralComplexCast:
20024 case CK_IntegralComplexToFloatingComplex:
20025 case CK_BuiltinFnToFnPtr:
20026 case CK_ZeroToOCLOpaqueType:
20027 case CK_NonAtomicToAtomic:
20028 case CK_AddressSpaceConversion:
20029 case CK_IntToOCLSampler:
20030 case CK_FloatingToFixedPoint:
20031 case CK_FixedPointToFloating:
20032 case CK_FixedPointCast:
20033 case CK_IntegralToFixedPoint:
20034 case CK_MatrixCast:
20035 case CK_HLSLAggregateSplatCast:
20036 llvm_unreachable("invalid cast kind for integral value");
20037
20038 case CK_BitCast:
20039 case CK_Dependent:
20040 case CK_LValueBitCast:
20041 case CK_ARCProduceObject:
20042 case CK_ARCConsumeObject:
20043 case CK_ARCReclaimReturnedObject:
20044 case CK_ARCExtendBlockObject:
20045 case CK_CopyAndAutoreleaseBlockObject:
20046 return Error(E);
20047
20048 case CK_UserDefinedConversion:
20049 case CK_LValueToRValue:
20050 case CK_AtomicToNonAtomic:
20051 case CK_NoOp:
20052 case CK_LValueToRValueBitCast:
20053 case CK_HLSLArrayRValue:
20054 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20055
20056 case CK_MemberPointerToBoolean:
20057 case CK_PointerToBoolean:
20058 case CK_IntegralToBoolean:
20059 case CK_FloatingToBoolean:
20060 case CK_BooleanToSignedIntegral:
20061 case CK_FloatingComplexToBoolean:
20062 case CK_IntegralComplexToBoolean: {
20063 bool BoolResult;
20064 if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info))
20065 return false;
20066 uint64_t IntResult = BoolResult;
20067 if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral)
20068 IntResult = (uint64_t)-1;
20069 return Success(Value: IntResult, E);
20070 }
20071
20072 case CK_FixedPointToIntegral: {
20073 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType));
20074 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20075 return false;
20076 bool Overflowed;
20077 llvm::APSInt Result = Src.convertToInt(
20078 DstWidth: Info.Ctx.getIntWidth(T: DestType),
20079 DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed);
20080 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Result, DestType))
20081 return false;
20082 return Success(SI: Result, E);
20083 }
20084
20085 case CK_FixedPointToBoolean: {
20086 // Unsigned padding does not affect this.
20087 APValue Val;
20088 if (!Evaluate(Result&: Val, Info, E: SubExpr))
20089 return false;
20090 return Success(Value: Val.getFixedPoint().getBoolValue(), E);
20091 }
20092
20093 case CK_IntegralCast: {
20094 if (!Visit(S: SubExpr))
20095 return false;
20096
20097 if (!Result.isInt()) {
20098 // Allow casts of address-of-label differences if they are no-ops
20099 // or narrowing, if the result is at least 32 bits wide.
20100 // (The narrowing case isn't actually guaranteed to
20101 // be constant-evaluatable except in some narrow cases which are hard
20102 // to detect here. We let it through on the assumption the user knows
20103 // what they are doing.)
20104 if (Result.isAddrLabelDiff()) {
20105 unsigned DestBits = Info.Ctx.getTypeSize(T: DestType);
20106 return DestBits >= 32 && DestBits <= Info.Ctx.getTypeSize(T: SrcType);
20107 }
20108 // Only allow casts of lvalues if they are lossless.
20109 return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType);
20110 }
20111
20112 if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
20113 const auto *ED = DestType->getAsEnumDecl();
20114 // Check that the value is within the range of the enumeration values.
20115 //
20116 // This corressponds to [expr.static.cast]p10 which says:
20117 // A value of integral or enumeration type can be explicitly converted
20118 // to a complete enumeration type ... If the enumeration type does not
20119 // have a fixed underlying type, the value is unchanged if the original
20120 // value is within the range of the enumeration values ([dcl.enum]), and
20121 // otherwise, the behavior is undefined.
20122 //
20123 // This was resolved as part of DR2338 which has CD5 status.
20124 if (!ED->isFixed()) {
20125 llvm::APInt Min;
20126 llvm::APInt Max;
20127
20128 ED->getValueRange(Max, Min);
20129 --Max;
20130
20131 if (ED->getNumNegativeBits() &&
20132 (Max.slt(RHS: Result.getInt().getSExtValue()) ||
20133 Min.sgt(RHS: Result.getInt().getSExtValue())))
20134 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20135 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getSExtValue()
20136 << Max.getSExtValue() << ED;
20137 else if (!ED->getNumNegativeBits() &&
20138 Max.ult(RHS: Result.getInt().getZExtValue()))
20139 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
20140 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getZExtValue()
20141 << Max.getZExtValue() << ED;
20142 }
20143 }
20144
20145 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType,
20146 Value: Result.getInt()), E);
20147 }
20148
20149 case CK_PointerToIntegral: {
20150 CCEDiag(E, D: diag::note_constexpr_invalid_cast_ptrtoint)
20151 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
20152 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
20153
20154 LValue LV;
20155 if (!EvaluatePointer(E: SubExpr, Result&: LV, Info))
20156 return false;
20157
20158 if (LV.getLValueBase()) {
20159 CCEDiag(E, D: diag::note_constexpr_has_lvalue) << E->getSourceRange();
20160 // Only allow based lvalue casts if they are lossless.
20161 // FIXME: Allow a larger integer size than the pointer size, and allow
20162 // narrowing back down to pointer width in subsequent integral casts.
20163 // FIXME: Check integer type's active bits, not its type size.
20164 if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType))
20165 return Error(E);
20166
20167 LV.Designator.setInvalid();
20168 LV.moveInto(V&: Result);
20169 return true;
20170 }
20171
20172 APSInt AsInt;
20173 APValue V;
20174 LV.moveInto(V);
20175 if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx))
20176 llvm_unreachable("Can't cast this!");
20177
20178 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType, Value: AsInt), E);
20179 }
20180
20181 case CK_IntegralComplexToReal: {
20182 ComplexValue C;
20183 if (!EvaluateComplex(E: SubExpr, Res&: C, Info))
20184 return false;
20185 return Success(SI: C.getComplexIntReal(), E);
20186 }
20187
20188 case CK_FloatingToIntegral: {
20189 APFloat F(0.0);
20190 if (!EvaluateFloat(E: SubExpr, Result&: F, Info))
20191 return false;
20192
20193 APSInt Value;
20194 if (!HandleFloatToIntCast(Info, E, SrcType, Value: F, DestType, Result&: Value))
20195 return false;
20196 return Success(SI: Value, E);
20197 }
20198 case CK_HLSLVectorTruncation: {
20199 APValue Val;
20200 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20201 return Error(E);
20202 return Success(V: Val.getVectorElt(I: 0), E);
20203 }
20204 case CK_HLSLMatrixTruncation: {
20205 APValue Val;
20206 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20207 return Error(E);
20208 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), E);
20209 }
20210 case CK_HLSLElementwiseCast: {
20211 SmallVector<APValue> SrcVals;
20212 SmallVector<QualType> SrcTypes;
20213
20214 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: DestType, SrcVals, SrcTypes))
20215 return false;
20216
20217 // cast our single element
20218 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20219 APValue ResultVal;
20220 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: DestType, Original: SrcVals[0],
20221 Result&: ResultVal))
20222 return false;
20223 return Success(V: ResultVal, E);
20224 }
20225 }
20226
20227 llvm_unreachable("unknown cast resulting in integral value");
20228}
20229
20230bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20231 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20232 ComplexValue LV;
20233 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20234 return false;
20235 if (!LV.isComplexInt())
20236 return Error(E);
20237 return Success(SI: LV.getComplexIntReal(), E);
20238 }
20239
20240 return Visit(S: E->getSubExpr());
20241}
20242
20243bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20244 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
20245 ComplexValue LV;
20246 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
20247 return false;
20248 if (!LV.isComplexInt())
20249 return Error(E);
20250 return Success(SI: LV.getComplexIntImag(), E);
20251 }
20252
20253 VisitIgnoredValue(E: E->getSubExpr());
20254 return Success(Value: 0, E);
20255}
20256
20257bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
20258 return Success(Value: E->getPackLength(), E);
20259}
20260
20261bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
20262 return Success(Value: E->getValue(), E);
20263}
20264
20265bool IntExprEvaluator::VisitConceptSpecializationExpr(
20266 const ConceptSpecializationExpr *E) {
20267 return Success(Value: E->isSatisfied(), E);
20268}
20269
20270bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) {
20271 return Success(Value: E->isSatisfied(), E);
20272}
20273
20274bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20275 switch (E->getOpcode()) {
20276 default:
20277 // Invalid unary operators
20278 return Error(E);
20279 case UO_Plus:
20280 // The result is just the value.
20281 return Visit(S: E->getSubExpr());
20282 case UO_Minus: {
20283 if (!Visit(S: E->getSubExpr())) return false;
20284 if (!Result.isFixedPoint())
20285 return Error(E);
20286 bool Overflowed;
20287 APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed);
20288 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Negated, DestType: E->getType()))
20289 return false;
20290 return Success(V: Negated, E);
20291 }
20292 case UO_LNot: {
20293 bool bres;
20294 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
20295 return false;
20296 return Success(Value: !bres, E);
20297 }
20298 }
20299}
20300
20301bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) {
20302 const Expr *SubExpr = E->getSubExpr();
20303 QualType DestType = E->getType();
20304 assert(DestType->isFixedPointType() &&
20305 "Expected destination type to be a fixed point type");
20306 auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType);
20307
20308 switch (E->getCastKind()) {
20309 case CK_FixedPointCast: {
20310 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20311 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
20312 return false;
20313 bool Overflowed;
20314 APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed);
20315 if (Overflowed) {
20316 if (Info.checkingForUndefinedBehavior())
20317 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20318 DiagID: diag::warn_fixedpoint_constant_overflow)
20319 << Result.toString() << E->getType();
20320 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20321 return false;
20322 }
20323 return Success(V: Result, E);
20324 }
20325 case CK_IntegralToFixedPoint: {
20326 APSInt Src;
20327 if (!EvaluateInteger(E: SubExpr, Result&: Src, Info))
20328 return false;
20329
20330 bool Overflowed;
20331 APFixedPoint IntResult = APFixedPoint::getFromIntValue(
20332 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20333
20334 if (Overflowed) {
20335 if (Info.checkingForUndefinedBehavior())
20336 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20337 DiagID: diag::warn_fixedpoint_constant_overflow)
20338 << IntResult.toString() << E->getType();
20339 if (!HandleOverflow(Info, E, SrcValue: IntResult, DestType: E->getType()))
20340 return false;
20341 }
20342
20343 return Success(V: IntResult, E);
20344 }
20345 case CK_FloatingToFixedPoint: {
20346 APFloat Src(0.0);
20347 if (!EvaluateFloat(E: SubExpr, Result&: Src, Info))
20348 return false;
20349
20350 bool Overflowed;
20351 APFixedPoint Result = APFixedPoint::getFromFloatValue(
20352 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
20353
20354 if (Overflowed) {
20355 if (Info.checkingForUndefinedBehavior())
20356 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20357 DiagID: diag::warn_fixedpoint_constant_overflow)
20358 << Result.toString() << E->getType();
20359 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20360 return false;
20361 }
20362
20363 return Success(V: Result, E);
20364 }
20365 case CK_NoOp:
20366 case CK_LValueToRValue:
20367 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20368 default:
20369 return Error(E);
20370 }
20371}
20372
20373bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20374 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20375 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20376
20377 const Expr *LHS = E->getLHS();
20378 const Expr *RHS = E->getRHS();
20379 FixedPointSemantics ResultFXSema =
20380 Info.Ctx.getFixedPointSemantics(Ty: E->getType());
20381
20382 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType()));
20383 if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info))
20384 return false;
20385 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType()));
20386 if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info))
20387 return false;
20388
20389 bool OpOverflow = false, ConversionOverflow = false;
20390 APFixedPoint Result(LHSFX.getSemantics());
20391 switch (E->getOpcode()) {
20392 case BO_Add: {
20393 Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow)
20394 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20395 break;
20396 }
20397 case BO_Sub: {
20398 Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow)
20399 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20400 break;
20401 }
20402 case BO_Mul: {
20403 Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow)
20404 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20405 break;
20406 }
20407 case BO_Div: {
20408 if (RHSFX.getValue() == 0) {
20409 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero);
20410 return false;
20411 }
20412 Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow)
20413 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
20414 break;
20415 }
20416 case BO_Shl:
20417 case BO_Shr: {
20418 FixedPointSemantics LHSSema = LHSFX.getSemantics();
20419 llvm::APSInt RHSVal = RHSFX.getValue();
20420
20421 unsigned ShiftBW =
20422 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding();
20423 unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1);
20424 // Embedded-C 4.1.6.2.2:
20425 // The right operand must be nonnegative and less than the total number
20426 // of (nonpadding) bits of the fixed-point operand ...
20427 if (RHSVal.isNegative())
20428 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHSVal;
20429 else if (Amt != RHSVal)
20430 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
20431 << RHSVal << E->getType() << ShiftBW;
20432
20433 if (E->getOpcode() == BO_Shl)
20434 Result = LHSFX.shl(Amt, Overflow: &OpOverflow);
20435 else
20436 Result = LHSFX.shr(Amt, Overflow: &OpOverflow);
20437 break;
20438 }
20439 default:
20440 return false;
20441 }
20442 if (OpOverflow || ConversionOverflow) {
20443 if (Info.checkingForUndefinedBehavior())
20444 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
20445 DiagID: diag::warn_fixedpoint_constant_overflow)
20446 << Result.toString() << E->getType();
20447 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
20448 return false;
20449 }
20450 return Success(V: Result, E);
20451}
20452
20453//===----------------------------------------------------------------------===//
20454// Float Evaluation
20455//===----------------------------------------------------------------------===//
20456
20457namespace {
20458class FloatExprEvaluator
20459 : public ExprEvaluatorBase<FloatExprEvaluator> {
20460 APFloat &Result;
20461public:
20462 FloatExprEvaluator(EvalInfo &info, APFloat &result)
20463 : ExprEvaluatorBaseTy(info), Result(result) {}
20464
20465 bool Success(const APValue &V, const Expr *e) {
20466 Result = V.getFloat();
20467 return true;
20468 }
20469
20470 bool ZeroInitialization(const Expr *E) {
20471 Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20472 return true;
20473 }
20474
20475 bool VisitCallExpr(const CallExpr *E);
20476
20477 bool VisitUnaryOperator(const UnaryOperator *E);
20478 bool VisitBinaryOperator(const BinaryOperator *E);
20479 bool VisitFloatingLiteral(const FloatingLiteral *E);
20480 bool VisitCastExpr(const CastExpr *E);
20481
20482 bool VisitUnaryReal(const UnaryOperator *E);
20483 bool VisitUnaryImag(const UnaryOperator *E);
20484
20485 // FIXME: Missing: array subscript of vector, member of vector
20486};
20487} // end anonymous namespace
20488
20489static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
20490 assert(!E->isValueDependent());
20491 assert(E->isPRValue() && E->getType()->isRealFloatingType());
20492 return FloatExprEvaluator(Info, Result).Visit(S: E);
20493}
20494
20495static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
20496 QualType ResultTy,
20497 const Expr *Arg,
20498 bool SNaN,
20499 llvm::APFloat &Result) {
20500 const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts());
20501 if (!S || !S->isOrdinary())
20502 return false;
20503
20504 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy);
20505
20506 llvm::APInt fill;
20507
20508 // Treat empty strings as if they were zero.
20509 if (S->getString().empty())
20510 fill = llvm::APInt(32, 0);
20511 else if (S->getString().getAsInteger(Radix: 0, Result&: fill))
20512 return false;
20513
20514 if (Context.getTargetInfo().isNan2008()) {
20515 if (SNaN)
20516 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20517 else
20518 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20519 } else {
20520 // Prior to IEEE 754-2008, architectures were allowed to choose whether
20521 // the first bit of their significand was set for qNaN or sNaN. MIPS chose
20522 // a different encoding to what became a standard in 2008, and for pre-
20523 // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as
20524 // sNaN. This is now known as "legacy NaN" encoding.
20525 if (SNaN)
20526 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20527 else
20528 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20529 }
20530
20531 return true;
20532}
20533
20534bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
20535 if (!IsConstantEvaluatedBuiltinCall(E))
20536 return ExprEvaluatorBaseTy::VisitCallExpr(E);
20537
20538 unsigned BuiltinOp = ConvertBuiltinIDToX86BuiltinID(Ctx: Info.Ctx, E);
20539
20540 switch (BuiltinOp) {
20541 default:
20542 return false;
20543
20544 case Builtin::BI__builtin_huge_val:
20545 case Builtin::BI__builtin_huge_valf:
20546 case Builtin::BI__builtin_huge_vall:
20547 case Builtin::BI__builtin_huge_valf16:
20548 case Builtin::BI__builtin_huge_valf128:
20549 case Builtin::BI__builtin_inf:
20550 case Builtin::BI__builtin_inff:
20551 case Builtin::BI__builtin_infl:
20552 case Builtin::BI__builtin_inff16:
20553 case Builtin::BI__builtin_inff128: {
20554 const llvm::fltSemantics &Sem =
20555 Info.Ctx.getFloatTypeSemantics(T: E->getType());
20556 Result = llvm::APFloat::getInf(Sem);
20557 return true;
20558 }
20559
20560 case Builtin::BI__builtin_nans:
20561 case Builtin::BI__builtin_nansf:
20562 case Builtin::BI__builtin_nansl:
20563 case Builtin::BI__builtin_nansf16:
20564 case Builtin::BI__builtin_nansf128:
20565 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20566 SNaN: true, Result))
20567 return Error(E);
20568 return true;
20569
20570 case Builtin::BI__builtin_nan:
20571 case Builtin::BI__builtin_nanf:
20572 case Builtin::BI__builtin_nanl:
20573 case Builtin::BI__builtin_nanf16:
20574 case Builtin::BI__builtin_nanf128:
20575 // If this is __builtin_nan() turn this into a nan, otherwise we
20576 // can't constant fold it.
20577 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20578 SNaN: false, Result))
20579 return Error(E);
20580 return true;
20581
20582 case Builtin::BI__builtin_elementwise_abs:
20583 case Builtin::BI__builtin_fabs:
20584 case Builtin::BI__builtin_fabsf:
20585 case Builtin::BI__builtin_fabsl:
20586 case Builtin::BI__builtin_fabsf128:
20587 // The C standard says "fabs raises no floating-point exceptions,
20588 // even if x is a signaling NaN. The returned value is independent of
20589 // the current rounding direction mode." Therefore constant folding can
20590 // proceed without regard to the floating point settings.
20591 // Reference, WG14 N2478 F.10.4.3
20592 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info))
20593 return false;
20594
20595 if (Result.isNegative())
20596 Result.changeSign();
20597 return true;
20598
20599 case Builtin::BI__arithmetic_fence:
20600 return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info);
20601
20602 // FIXME: Builtin::BI__builtin_powi
20603 // FIXME: Builtin::BI__builtin_powif
20604 // FIXME: Builtin::BI__builtin_powil
20605
20606 case Builtin::BI__builtin_copysign:
20607 case Builtin::BI__builtin_copysignf:
20608 case Builtin::BI__builtin_copysignl:
20609 case Builtin::BI__builtin_copysignf128: {
20610 APFloat RHS(0.);
20611 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20612 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20613 return false;
20614 Result.copySign(RHS);
20615 return true;
20616 }
20617
20618 case Builtin::BI__builtin_fmax:
20619 case Builtin::BI__builtin_fmaxf:
20620 case Builtin::BI__builtin_fmaxl:
20621 case Builtin::BI__builtin_fmaxf16:
20622 case Builtin::BI__builtin_fmaxf128: {
20623 APFloat RHS(0.);
20624 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20625 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20626 return false;
20627 Result = maxnum(A: Result, B: RHS);
20628 return true;
20629 }
20630
20631 case Builtin::BI__builtin_fmin:
20632 case Builtin::BI__builtin_fminf:
20633 case Builtin::BI__builtin_fminl:
20634 case Builtin::BI__builtin_fminf16:
20635 case Builtin::BI__builtin_fminf128: {
20636 APFloat RHS(0.);
20637 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20638 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20639 return false;
20640 Result = minnum(A: Result, B: RHS);
20641 return true;
20642 }
20643
20644 case Builtin::BI__builtin_fmaximum_num:
20645 case Builtin::BI__builtin_fmaximum_numf:
20646 case Builtin::BI__builtin_fmaximum_numl:
20647 case Builtin::BI__builtin_fmaximum_numf16:
20648 case Builtin::BI__builtin_fmaximum_numf128: {
20649 APFloat RHS(0.);
20650 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20651 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20652 return false;
20653 Result = maximumnum(A: Result, B: RHS);
20654 return true;
20655 }
20656
20657 case Builtin::BI__builtin_fminimum_num:
20658 case Builtin::BI__builtin_fminimum_numf:
20659 case Builtin::BI__builtin_fminimum_numl:
20660 case Builtin::BI__builtin_fminimum_numf16:
20661 case Builtin::BI__builtin_fminimum_numf128: {
20662 APFloat RHS(0.);
20663 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20664 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20665 return false;
20666 Result = minimumnum(A: Result, B: RHS);
20667 return true;
20668 }
20669
20670 case Builtin::BI__builtin_elementwise_fma: {
20671 if (!E->getArg(Arg: 0)->isPRValue() || !E->getArg(Arg: 1)->isPRValue() ||
20672 !E->getArg(Arg: 2)->isPRValue()) {
20673 return false;
20674 }
20675 APFloat SourceY(0.), SourceZ(0.);
20676 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20677 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: SourceY, Info) ||
20678 !EvaluateFloat(E: E->getArg(Arg: 2), Result&: SourceZ, Info))
20679 return false;
20680 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
20681 (void)Result.fusedMultiplyAdd(Multiplicand: SourceY, Addend: SourceZ, RM);
20682 return true;
20683 }
20684
20685 case clang::X86::BI__builtin_ia32_vec_ext_v4sf: {
20686 APValue Vec;
20687 APSInt IdxAPS;
20688 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
20689 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
20690 return false;
20691 unsigned N = Vec.getVectorLength();
20692 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
20693 return Success(V: Vec.getVectorElt(I: Idx), e: E);
20694 }
20695 }
20696}
20697
20698bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20699 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20700 ComplexValue CV;
20701 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20702 return false;
20703 Result = CV.FloatReal;
20704 return true;
20705 }
20706
20707 return Visit(S: E->getSubExpr());
20708}
20709
20710bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20711 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20712 ComplexValue CV;
20713 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20714 return false;
20715 Result = CV.FloatImag;
20716 return true;
20717 }
20718
20719 VisitIgnoredValue(E: E->getSubExpr());
20720 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType());
20721 Result = llvm::APFloat::getZero(Sem);
20722 return true;
20723}
20724
20725bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20726 switch (E->getOpcode()) {
20727 default: return Error(E);
20728 case UO_Plus:
20729 return EvaluateFloat(E: E->getSubExpr(), Result, Info);
20730 case UO_Minus:
20731 // In C standard, WG14 N2478 F.3 p4
20732 // "the unary - raises no floating point exceptions,
20733 // even if the operand is signalling."
20734 if (!EvaluateFloat(E: E->getSubExpr(), Result, Info))
20735 return false;
20736 Result.changeSign();
20737 return true;
20738 }
20739}
20740
20741bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20742 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20743 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20744
20745 APFloat RHS(0.0);
20746 bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info);
20747 if (!LHSOK && !Info.noteFailure())
20748 return false;
20749 return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK &&
20750 handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS);
20751}
20752
20753bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
20754 Result = E->getValue();
20755 return true;
20756}
20757
20758bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
20759 const Expr* SubExpr = E->getSubExpr();
20760
20761 switch (E->getCastKind()) {
20762 default:
20763 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20764
20765 case CK_HLSLAggregateSplatCast:
20766 llvm_unreachable("invalid cast kind for floating value");
20767
20768 case CK_IntegralToFloating: {
20769 APSInt IntResult;
20770 const FPOptions FPO = E->getFPFeaturesInEffect(
20771 LO: Info.Ctx.getLangOpts());
20772 return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) &&
20773 HandleIntToFloatCast(Info, E, FPO, SrcType: SubExpr->getType(),
20774 Value: IntResult, DestType: E->getType(), Result);
20775 }
20776
20777 case CK_FixedPointToFloating: {
20778 APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20779 if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info))
20780 return false;
20781 Result =
20782 FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20783 return true;
20784 }
20785
20786 case CK_FloatingCast: {
20787 if (!Visit(S: SubExpr))
20788 return false;
20789 return HandleFloatToFloatCast(Info, E, SrcType: SubExpr->getType(), DestType: E->getType(),
20790 Result);
20791 }
20792
20793 case CK_FloatingComplexToReal: {
20794 ComplexValue V;
20795 if (!EvaluateComplex(E: SubExpr, Res&: V, Info))
20796 return false;
20797 Result = V.getComplexFloatReal();
20798 return true;
20799 }
20800 case CK_HLSLVectorTruncation: {
20801 APValue Val;
20802 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20803 return Error(E);
20804 return Success(V: Val.getVectorElt(I: 0), e: E);
20805 }
20806 case CK_HLSLMatrixTruncation: {
20807 APValue Val;
20808 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20809 return Error(E);
20810 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), e: E);
20811 }
20812 case CK_HLSLElementwiseCast: {
20813 SmallVector<APValue> SrcVals;
20814 SmallVector<QualType> SrcTypes;
20815
20816 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: E->getType(), SrcVals,
20817 SrcTypes))
20818 return false;
20819 APValue Val;
20820
20821 // cast our single element
20822 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20823 APValue ResultVal;
20824 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: E->getType(), Original: SrcVals[0],
20825 Result&: ResultVal))
20826 return false;
20827 return Success(V: ResultVal, e: E);
20828 }
20829 }
20830}
20831
20832//===----------------------------------------------------------------------===//
20833// Complex Evaluation (for float and integer)
20834//===----------------------------------------------------------------------===//
20835
20836namespace {
20837class ComplexExprEvaluator
20838 : public ExprEvaluatorBase<ComplexExprEvaluator> {
20839 ComplexValue &Result;
20840
20841public:
20842 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
20843 : ExprEvaluatorBaseTy(info), Result(Result) {}
20844
20845 bool Success(const APValue &V, const Expr *e) {
20846 Result.setFrom(V);
20847 return true;
20848 }
20849
20850 bool ZeroInitialization(const Expr *E);
20851
20852 //===--------------------------------------------------------------------===//
20853 // Visitor Methods
20854 //===--------------------------------------------------------------------===//
20855
20856 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
20857 bool VisitCastExpr(const CastExpr *E);
20858 bool VisitBinaryOperator(const BinaryOperator *E);
20859 bool VisitUnaryOperator(const UnaryOperator *E);
20860 bool VisitInitListExpr(const InitListExpr *E);
20861 bool VisitCallExpr(const CallExpr *E);
20862};
20863} // end anonymous namespace
20864
20865static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
20866 EvalInfo &Info) {
20867 assert(!E->isValueDependent());
20868 assert(E->isPRValue() && E->getType()->isAnyComplexType());
20869 return ComplexExprEvaluator(Info, Result).Visit(S: E);
20870}
20871
20872bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
20873 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
20874 if (ElemTy->isRealFloatingType()) {
20875 Result.makeComplexFloat();
20876 APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy));
20877 Result.FloatReal = Zero;
20878 Result.FloatImag = Zero;
20879 } else {
20880 Result.makeComplexInt();
20881 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy);
20882 Result.IntReal = Zero;
20883 Result.IntImag = Zero;
20884 }
20885 return true;
20886}
20887
20888bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
20889 const Expr* SubExpr = E->getSubExpr();
20890
20891 if (SubExpr->getType()->isRealFloatingType()) {
20892 Result.makeComplexFloat();
20893 APFloat &Imag = Result.FloatImag;
20894 if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info))
20895 return false;
20896
20897 Result.FloatReal = APFloat(Imag.getSemantics());
20898 return true;
20899 } else {
20900 assert(SubExpr->getType()->isIntegerType() &&
20901 "Unexpected imaginary literal.");
20902
20903 Result.makeComplexInt();
20904 APSInt &Imag = Result.IntImag;
20905 if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info))
20906 return false;
20907
20908 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
20909 return true;
20910 }
20911}
20912
20913bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
20914
20915 switch (E->getCastKind()) {
20916 case CK_BitCast:
20917 case CK_BaseToDerived:
20918 case CK_DerivedToBase:
20919 case CK_UncheckedDerivedToBase:
20920 case CK_Dynamic:
20921 case CK_ToUnion:
20922 case CK_ArrayToPointerDecay:
20923 case CK_FunctionToPointerDecay:
20924 case CK_NullToPointer:
20925 case CK_NullToMemberPointer:
20926 case CK_BaseToDerivedMemberPointer:
20927 case CK_DerivedToBaseMemberPointer:
20928 case CK_MemberPointerToBoolean:
20929 case CK_ReinterpretMemberPointer:
20930 case CK_ConstructorConversion:
20931 case CK_IntegralToPointer:
20932 case CK_PointerToIntegral:
20933 case CK_PointerToBoolean:
20934 case CK_ToVoid:
20935 case CK_VectorSplat:
20936 case CK_IntegralCast:
20937 case CK_BooleanToSignedIntegral:
20938 case CK_IntegralToBoolean:
20939 case CK_IntegralToFloating:
20940 case CK_FloatingToIntegral:
20941 case CK_FloatingToBoolean:
20942 case CK_FloatingCast:
20943 case CK_CPointerToObjCPointerCast:
20944 case CK_BlockPointerToObjCPointerCast:
20945 case CK_AnyPointerToBlockPointerCast:
20946 case CK_ObjCObjectLValueCast:
20947 case CK_FloatingComplexToReal:
20948 case CK_FloatingComplexToBoolean:
20949 case CK_IntegralComplexToReal:
20950 case CK_IntegralComplexToBoolean:
20951 case CK_ARCProduceObject:
20952 case CK_ARCConsumeObject:
20953 case CK_ARCReclaimReturnedObject:
20954 case CK_ARCExtendBlockObject:
20955 case CK_CopyAndAutoreleaseBlockObject:
20956 case CK_BuiltinFnToFnPtr:
20957 case CK_ZeroToOCLOpaqueType:
20958 case CK_NonAtomicToAtomic:
20959 case CK_AddressSpaceConversion:
20960 case CK_IntToOCLSampler:
20961 case CK_FloatingToFixedPoint:
20962 case CK_FixedPointToFloating:
20963 case CK_FixedPointCast:
20964 case CK_FixedPointToBoolean:
20965 case CK_FixedPointToIntegral:
20966 case CK_IntegralToFixedPoint:
20967 case CK_MatrixCast:
20968 case CK_HLSLVectorTruncation:
20969 case CK_HLSLMatrixTruncation:
20970 case CK_HLSLElementwiseCast:
20971 case CK_HLSLAggregateSplatCast:
20972 llvm_unreachable("invalid cast kind for complex value");
20973
20974 case CK_LValueToRValue:
20975 case CK_AtomicToNonAtomic:
20976 case CK_NoOp:
20977 case CK_LValueToRValueBitCast:
20978 case CK_HLSLArrayRValue:
20979 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20980
20981 case CK_Dependent:
20982 case CK_LValueBitCast:
20983 case CK_UserDefinedConversion:
20984 return Error(E);
20985
20986 case CK_FloatingRealToComplex: {
20987 APFloat &Real = Result.FloatReal;
20988 if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info))
20989 return false;
20990
20991 Result.makeComplexFloat();
20992 Result.FloatImag = APFloat(Real.getSemantics());
20993 return true;
20994 }
20995
20996 case CK_FloatingComplexCast: {
20997 if (!Visit(S: E->getSubExpr()))
20998 return false;
20999
21000 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21001 QualType From
21002 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21003
21004 return HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatReal) &&
21005 HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatImag);
21006 }
21007
21008 case CK_FloatingComplexToIntegralComplex: {
21009 if (!Visit(S: E->getSubExpr()))
21010 return false;
21011
21012 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21013 QualType From
21014 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21015 Result.makeComplexInt();
21016 return HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatReal,
21017 DestType: To, Result&: Result.IntReal) &&
21018 HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatImag,
21019 DestType: To, Result&: Result.IntImag);
21020 }
21021
21022 case CK_IntegralRealToComplex: {
21023 APSInt &Real = Result.IntReal;
21024 if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info))
21025 return false;
21026
21027 Result.makeComplexInt();
21028 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
21029 return true;
21030 }
21031
21032 case CK_IntegralComplexCast: {
21033 if (!Visit(S: E->getSubExpr()))
21034 return false;
21035
21036 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21037 QualType From
21038 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21039
21040 Result.IntReal = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntReal);
21041 Result.IntImag = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntImag);
21042 return true;
21043 }
21044
21045 case CK_IntegralComplexToFloatingComplex: {
21046 if (!Visit(S: E->getSubExpr()))
21047 return false;
21048
21049 const FPOptions FPO = E->getFPFeaturesInEffect(
21050 LO: Info.Ctx.getLangOpts());
21051 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
21052 QualType From
21053 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
21054 Result.makeComplexFloat();
21055 return HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntReal,
21056 DestType: To, Result&: Result.FloatReal) &&
21057 HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntImag,
21058 DestType: To, Result&: Result.FloatImag);
21059 }
21060 }
21061
21062 llvm_unreachable("unknown cast resulting in complex value");
21063}
21064
21065uint8_t GFNIMultiplicativeInverse(uint8_t Byte) {
21066 // Lookup Table for Multiplicative Inverse in GF(2^8)
21067 const uint8_t GFInv[256] = {
21068 0x00, 0x01, 0x8d, 0xf6, 0xcb, 0x52, 0x7b, 0xd1, 0xe8, 0x4f, 0x29, 0xc0,
21069 0xb0, 0xe1, 0xe5, 0xc7, 0x74, 0xb4, 0xaa, 0x4b, 0x99, 0x2b, 0x60, 0x5f,
21070 0x58, 0x3f, 0xfd, 0xcc, 0xff, 0x40, 0xee, 0xb2, 0x3a, 0x6e, 0x5a, 0xf1,
21071 0x55, 0x4d, 0xa8, 0xc9, 0xc1, 0x0a, 0x98, 0x15, 0x30, 0x44, 0xa2, 0xc2,
21072 0x2c, 0x45, 0x92, 0x6c, 0xf3, 0x39, 0x66, 0x42, 0xf2, 0x35, 0x20, 0x6f,
21073 0x77, 0xbb, 0x59, 0x19, 0x1d, 0xfe, 0x37, 0x67, 0x2d, 0x31, 0xf5, 0x69,
21074 0xa7, 0x64, 0xab, 0x13, 0x54, 0x25, 0xe9, 0x09, 0xed, 0x5c, 0x05, 0xca,
21075 0x4c, 0x24, 0x87, 0xbf, 0x18, 0x3e, 0x22, 0xf0, 0x51, 0xec, 0x61, 0x17,
21076 0x16, 0x5e, 0xaf, 0xd3, 0x49, 0xa6, 0x36, 0x43, 0xf4, 0x47, 0x91, 0xdf,
21077 0x33, 0x93, 0x21, 0x3b, 0x79, 0xb7, 0x97, 0x85, 0x10, 0xb5, 0xba, 0x3c,
21078 0xb6, 0x70, 0xd0, 0x06, 0xa1, 0xfa, 0x81, 0x82, 0x83, 0x7e, 0x7f, 0x80,
21079 0x96, 0x73, 0xbe, 0x56, 0x9b, 0x9e, 0x95, 0xd9, 0xf7, 0x02, 0xb9, 0xa4,
21080 0xde, 0x6a, 0x32, 0x6d, 0xd8, 0x8a, 0x84, 0x72, 0x2a, 0x14, 0x9f, 0x88,
21081 0xf9, 0xdc, 0x89, 0x9a, 0xfb, 0x7c, 0x2e, 0xc3, 0x8f, 0xb8, 0x65, 0x48,
21082 0x26, 0xc8, 0x12, 0x4a, 0xce, 0xe7, 0xd2, 0x62, 0x0c, 0xe0, 0x1f, 0xef,
21083 0x11, 0x75, 0x78, 0x71, 0xa5, 0x8e, 0x76, 0x3d, 0xbd, 0xbc, 0x86, 0x57,
21084 0x0b, 0x28, 0x2f, 0xa3, 0xda, 0xd4, 0xe4, 0x0f, 0xa9, 0x27, 0x53, 0x04,
21085 0x1b, 0xfc, 0xac, 0xe6, 0x7a, 0x07, 0xae, 0x63, 0xc5, 0xdb, 0xe2, 0xea,
21086 0x94, 0x8b, 0xc4, 0xd5, 0x9d, 0xf8, 0x90, 0x6b, 0xb1, 0x0d, 0xd6, 0xeb,
21087 0xc6, 0x0e, 0xcf, 0xad, 0x08, 0x4e, 0xd7, 0xe3, 0x5d, 0x50, 0x1e, 0xb3,
21088 0x5b, 0x23, 0x38, 0x34, 0x68, 0x46, 0x03, 0x8c, 0xdd, 0x9c, 0x7d, 0xa0,
21089 0xcd, 0x1a, 0x41, 0x1c};
21090
21091 return GFInv[Byte];
21092}
21093
21094uint8_t GFNIAffine(uint8_t XByte, const APInt &AQword, const APSInt &Imm,
21095 bool Inverse) {
21096 unsigned NumBitsInByte = 8;
21097 // Computing the affine transformation
21098 uint8_t RetByte = 0;
21099 for (uint32_t BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21100 uint8_t AByte =
21101 AQword.lshr(shiftAmt: (7 - static_cast<int32_t>(BitIdx)) * NumBitsInByte)
21102 .getLoBits(numBits: 8)
21103 .getZExtValue();
21104 uint8_t Product;
21105 if (Inverse) {
21106 Product = AByte & GFNIMultiplicativeInverse(Byte: XByte);
21107 } else {
21108 Product = AByte & XByte;
21109 }
21110 uint8_t Parity = 0;
21111
21112 // Dot product in GF(2) uses XOR instead of addition
21113 for (unsigned PBitIdx = 0; PBitIdx != NumBitsInByte; ++PBitIdx) {
21114 Parity = Parity ^ ((Product >> PBitIdx) & 0x1);
21115 }
21116
21117 uint8_t Temp = Imm[BitIdx] ? 1 : 0;
21118 RetByte |= (Temp ^ Parity) << BitIdx;
21119 }
21120 return RetByte;
21121}
21122
21123uint8_t GFNIMul(uint8_t AByte, uint8_t BByte) {
21124 // Multiplying two polynomials of degree 7
21125 // Polynomial of degree 7
21126 // x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
21127 uint16_t TWord = 0;
21128 unsigned NumBitsInByte = 8;
21129 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
21130 if ((BByte >> BitIdx) & 0x1) {
21131 TWord = TWord ^ (AByte << BitIdx);
21132 }
21133 }
21134
21135 // When multiplying two polynomials of degree 7
21136 // results in a polynomial of degree 14
21137 // so the result has to be reduced to 7
21138 // Reduction polynomial is x^8 + x^4 + x^3 + x + 1 i.e. 0x11B
21139 for (int32_t BitIdx = 14; BitIdx > 7; --BitIdx) {
21140 if ((TWord >> BitIdx) & 0x1) {
21141 TWord = TWord ^ (0x11B << (BitIdx - 8));
21142 }
21143 }
21144 return (TWord & 0xFF);
21145}
21146
21147void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D,
21148 APFloat &ResR, APFloat &ResI) {
21149 // This is an implementation of complex multiplication according to the
21150 // constraints laid out in C11 Annex G. The implementation uses the
21151 // following naming scheme:
21152 // (a + ib) * (c + id)
21153
21154 APFloat AC = A * C;
21155 APFloat BD = B * D;
21156 APFloat AD = A * D;
21157 APFloat BC = B * C;
21158 ResR = AC - BD;
21159 ResI = AD + BC;
21160 if (ResR.isNaN() && ResI.isNaN()) {
21161 bool Recalc = false;
21162 if (A.isInfinity() || B.isInfinity()) {
21163 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21164 Sign: A);
21165 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21166 Sign: B);
21167 if (C.isNaN())
21168 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21169 if (D.isNaN())
21170 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21171 Recalc = true;
21172 }
21173 if (C.isInfinity() || D.isInfinity()) {
21174 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21175 Sign: C);
21176 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21177 Sign: D);
21178 if (A.isNaN())
21179 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21180 if (B.isNaN())
21181 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21182 Recalc = true;
21183 }
21184 if (!Recalc && (AC.isInfinity() || BD.isInfinity() || AD.isInfinity() ||
21185 BC.isInfinity())) {
21186 if (A.isNaN())
21187 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
21188 if (B.isNaN())
21189 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
21190 if (C.isNaN())
21191 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
21192 if (D.isNaN())
21193 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
21194 Recalc = true;
21195 }
21196 if (Recalc) {
21197 ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D);
21198 ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C);
21199 }
21200 }
21201}
21202
21203void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D,
21204 APFloat &ResR, APFloat &ResI) {
21205 // This is an implementation of complex division according to the
21206 // constraints laid out in C11 Annex G. The implementation uses the
21207 // following naming scheme:
21208 // (a + ib) / (c + id)
21209
21210 int DenomLogB = 0;
21211 APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D));
21212 if (MaxCD.isFinite()) {
21213 DenomLogB = ilogb(Arg: MaxCD);
21214 C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21215 D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21216 }
21217 APFloat Denom = C * C + D * D;
21218 ResR =
21219 scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21220 ResI =
21221 scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
21222 if (ResR.isNaN() && ResI.isNaN()) {
21223 if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) {
21224 ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A;
21225 ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B;
21226 } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() &&
21227 D.isFinite()) {
21228 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
21229 Sign: A);
21230 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
21231 Sign: B);
21232 ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D);
21233 ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D);
21234 } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) {
21235 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
21236 Sign: C);
21237 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
21238 Sign: D);
21239 ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D);
21240 ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D);
21241 }
21242 }
21243}
21244
21245APSInt NormalizeRotateAmount(const APSInt &Value, const APSInt &Amount) {
21246 // Normalize shift amount to [0, BitWidth) range to match runtime behavior
21247 APSInt NormAmt = Amount;
21248 unsigned BitWidth = Value.getBitWidth();
21249 unsigned AmtBitWidth = NormAmt.getBitWidth();
21250 if (BitWidth == 1) {
21251 // Rotating a 1-bit value is always a no-op
21252 NormAmt = APSInt(APInt(AmtBitWidth, 0), NormAmt.isUnsigned());
21253 } else if (BitWidth == 2) {
21254 // For 2-bit values: rotation amount is 0 or 1 based on
21255 // whether the amount is even or odd. We can't use srem here because
21256 // the divisor (2) would be misinterpreted as -2 in 2-bit signed arithmetic.
21257 NormAmt =
21258 APSInt(APInt(AmtBitWidth, NormAmt[0] ? 1 : 0), NormAmt.isUnsigned());
21259 } else {
21260 APInt Divisor;
21261 if (AmtBitWidth > BitWidth) {
21262 Divisor = llvm::APInt(AmtBitWidth, BitWidth);
21263 } else {
21264 Divisor = llvm::APInt(BitWidth, BitWidth);
21265 if (AmtBitWidth < BitWidth) {
21266 NormAmt = NormAmt.extend(width: BitWidth);
21267 }
21268 }
21269
21270 // Normalize to [0, BitWidth)
21271 if (NormAmt.isSigned()) {
21272 NormAmt = APSInt(NormAmt.srem(RHS: Divisor), /*isUnsigned=*/false);
21273 if (NormAmt.isNegative()) {
21274 APSInt SignedDivisor(Divisor, /*isUnsigned=*/false);
21275 NormAmt += SignedDivisor;
21276 }
21277 } else {
21278 NormAmt = APSInt(NormAmt.urem(RHS: Divisor), /*isUnsigned=*/true);
21279 }
21280 }
21281
21282 return NormAmt;
21283}
21284
21285bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
21286 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
21287 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
21288
21289 // Track whether the LHS or RHS is real at the type system level. When this is
21290 // the case we can simplify our evaluation strategy.
21291 bool LHSReal = false, RHSReal = false;
21292
21293 bool LHSOK;
21294 if (E->getLHS()->getType()->isRealFloatingType()) {
21295 LHSReal = true;
21296 APFloat &Real = Result.FloatReal;
21297 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info);
21298 if (LHSOK) {
21299 Result.makeComplexFloat();
21300 Result.FloatImag = APFloat(Real.getSemantics());
21301 }
21302 } else {
21303 LHSOK = Visit(S: E->getLHS());
21304 }
21305 if (!LHSOK && !Info.noteFailure())
21306 return false;
21307
21308 ComplexValue RHS;
21309 if (E->getRHS()->getType()->isRealFloatingType()) {
21310 RHSReal = true;
21311 APFloat &Real = RHS.FloatReal;
21312 if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK)
21313 return false;
21314 RHS.makeComplexFloat();
21315 RHS.FloatImag = APFloat(Real.getSemantics());
21316 } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
21317 return false;
21318
21319 assert(!(LHSReal && RHSReal) &&
21320 "Cannot have both operands of a complex operation be real.");
21321 switch (E->getOpcode()) {
21322 default: return Error(E);
21323 case BO_Add:
21324 if (Result.isComplexFloat()) {
21325 Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(),
21326 RM: APFloat::rmNearestTiesToEven);
21327 if (LHSReal)
21328 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21329 else if (!RHSReal)
21330 Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(),
21331 RM: APFloat::rmNearestTiesToEven);
21332 } else {
21333 Result.getComplexIntReal() += RHS.getComplexIntReal();
21334 Result.getComplexIntImag() += RHS.getComplexIntImag();
21335 }
21336 break;
21337 case BO_Sub:
21338 if (Result.isComplexFloat()) {
21339 Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(),
21340 RM: APFloat::rmNearestTiesToEven);
21341 if (LHSReal) {
21342 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
21343 Result.getComplexFloatImag().changeSign();
21344 } else if (!RHSReal) {
21345 Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(),
21346 RM: APFloat::rmNearestTiesToEven);
21347 }
21348 } else {
21349 Result.getComplexIntReal() -= RHS.getComplexIntReal();
21350 Result.getComplexIntImag() -= RHS.getComplexIntImag();
21351 }
21352 break;
21353 case BO_Mul:
21354 if (Result.isComplexFloat()) {
21355 // This is an implementation of complex multiplication according to the
21356 // constraints laid out in C11 Annex G. The implementation uses the
21357 // following naming scheme:
21358 // (a + ib) * (c + id)
21359 ComplexValue LHS = Result;
21360 APFloat &A = LHS.getComplexFloatReal();
21361 APFloat &B = LHS.getComplexFloatImag();
21362 APFloat &C = RHS.getComplexFloatReal();
21363 APFloat &D = RHS.getComplexFloatImag();
21364 APFloat &ResR = Result.getComplexFloatReal();
21365 APFloat &ResI = Result.getComplexFloatImag();
21366 if (LHSReal) {
21367 assert(!RHSReal && "Cannot have two real operands for a complex op!");
21368 ResR = A;
21369 ResI = A;
21370 // ResR = A * C;
21371 // ResI = A * D;
21372 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: C) ||
21373 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: D))
21374 return false;
21375 } else if (RHSReal) {
21376 // ResR = C * A;
21377 // ResI = C * B;
21378 ResR = C;
21379 ResI = C;
21380 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: A) ||
21381 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: B))
21382 return false;
21383 } else {
21384 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
21385 }
21386 } else {
21387 ComplexValue LHS = Result;
21388 Result.getComplexIntReal() =
21389 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
21390 LHS.getComplexIntImag() * RHS.getComplexIntImag());
21391 Result.getComplexIntImag() =
21392 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
21393 LHS.getComplexIntImag() * RHS.getComplexIntReal());
21394 }
21395 break;
21396 case BO_Div:
21397 if (Result.isComplexFloat()) {
21398 // This is an implementation of complex division according to the
21399 // constraints laid out in C11 Annex G. The implementation uses the
21400 // following naming scheme:
21401 // (a + ib) / (c + id)
21402 ComplexValue LHS = Result;
21403 APFloat &A = LHS.getComplexFloatReal();
21404 APFloat &B = LHS.getComplexFloatImag();
21405 APFloat &C = RHS.getComplexFloatReal();
21406 APFloat &D = RHS.getComplexFloatImag();
21407 APFloat &ResR = Result.getComplexFloatReal();
21408 APFloat &ResI = Result.getComplexFloatImag();
21409 if (RHSReal) {
21410 ResR = A;
21411 ResI = B;
21412 // ResR = A / C;
21413 // ResI = B / C;
21414 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Div, RHS: C) ||
21415 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Div, RHS: C))
21416 return false;
21417 } else {
21418 if (LHSReal) {
21419 // No real optimizations we can do here, stub out with zero.
21420 B = APFloat::getZero(Sem: A.getSemantics());
21421 }
21422 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
21423 }
21424 } else {
21425 ComplexValue LHS = Result;
21426 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
21427 RHS.getComplexIntImag() * RHS.getComplexIntImag();
21428 if (Den.isZero())
21429 return Error(E, D: diag::note_expr_divide_by_zero);
21430
21431 Result.getComplexIntReal() =
21432 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
21433 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
21434 Result.getComplexIntImag() =
21435 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
21436 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
21437 }
21438 break;
21439 }
21440
21441 return true;
21442}
21443
21444bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
21445 // Get the operand value into 'Result'.
21446 if (!Visit(S: E->getSubExpr()))
21447 return false;
21448
21449 switch (E->getOpcode()) {
21450 default:
21451 return Error(E);
21452 case UO_Extension:
21453 return true;
21454 case UO_Plus:
21455 // The result is always just the subexpr.
21456 return true;
21457 case UO_Minus:
21458 if (Result.isComplexFloat()) {
21459 Result.getComplexFloatReal().changeSign();
21460 Result.getComplexFloatImag().changeSign();
21461 }
21462 else {
21463 Result.getComplexIntReal() = -Result.getComplexIntReal();
21464 Result.getComplexIntImag() = -Result.getComplexIntImag();
21465 }
21466 return true;
21467 case UO_Not:
21468 if (Result.isComplexFloat())
21469 Result.getComplexFloatImag().changeSign();
21470 else
21471 Result.getComplexIntImag() = -Result.getComplexIntImag();
21472 return true;
21473 }
21474}
21475
21476bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
21477 if (E->getNumInits() == 2) {
21478 if (E->getType()->isComplexType()) {
21479 Result.makeComplexFloat();
21480 if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info))
21481 return false;
21482 if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info))
21483 return false;
21484 } else {
21485 Result.makeComplexInt();
21486 if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info))
21487 return false;
21488 if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info))
21489 return false;
21490 }
21491 return true;
21492 }
21493 return ExprEvaluatorBaseTy::VisitInitListExpr(E);
21494}
21495
21496bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) {
21497 if (!IsConstantEvaluatedBuiltinCall(E))
21498 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21499
21500 switch (E->getBuiltinCallee()) {
21501 case Builtin::BI__builtin_complex:
21502 Result.makeComplexFloat();
21503 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info))
21504 return false;
21505 if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info))
21506 return false;
21507 return true;
21508
21509 default:
21510 return false;
21511 }
21512}
21513
21514//===----------------------------------------------------------------------===//
21515// Atomic expression evaluation, essentially just handling the NonAtomicToAtomic
21516// implicit conversion.
21517//===----------------------------------------------------------------------===//
21518
21519namespace {
21520class AtomicExprEvaluator :
21521 public ExprEvaluatorBase<AtomicExprEvaluator> {
21522 const LValue *This;
21523 APValue &Result;
21524public:
21525 AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result)
21526 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
21527
21528 bool Success(const APValue &V, const Expr *E) {
21529 Result = V;
21530 return true;
21531 }
21532
21533 bool ZeroInitialization(const Expr *E) {
21534 ImplicitValueInitExpr VIE(
21535 E->getType()->castAs<AtomicType>()->getValueType());
21536 // For atomic-qualified class (and array) types in C++, initialize the
21537 // _Atomic-wrapped subobject directly, in-place.
21538 return This ? EvaluateInPlace(Result, Info, This: *This, E: &VIE)
21539 : Evaluate(Result, Info, E: &VIE);
21540 }
21541
21542 bool VisitCastExpr(const CastExpr *E) {
21543 switch (E->getCastKind()) {
21544 default:
21545 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21546 case CK_NullToPointer:
21547 VisitIgnoredValue(E: E->getSubExpr());
21548 return ZeroInitialization(E);
21549 case CK_NonAtomicToAtomic:
21550 return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr())
21551 : Evaluate(Result, Info, E: E->getSubExpr());
21552 }
21553 }
21554};
21555} // end anonymous namespace
21556
21557static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
21558 EvalInfo &Info) {
21559 assert(!E->isValueDependent());
21560 assert(E->isPRValue() && E->getType()->isAtomicType());
21561 return AtomicExprEvaluator(Info, This, Result).Visit(S: E);
21562}
21563
21564//===----------------------------------------------------------------------===//
21565// Void expression evaluation, primarily for a cast to void on the LHS of a
21566// comma operator
21567//===----------------------------------------------------------------------===//
21568
21569namespace {
21570class VoidExprEvaluator
21571 : public ExprEvaluatorBase<VoidExprEvaluator> {
21572public:
21573 VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
21574
21575 bool Success(const APValue &V, const Expr *e) { return true; }
21576
21577 bool ZeroInitialization(const Expr *E) { return true; }
21578
21579 bool VisitCastExpr(const CastExpr *E) {
21580 switch (E->getCastKind()) {
21581 default:
21582 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21583 case CK_ToVoid:
21584 VisitIgnoredValue(E: E->getSubExpr());
21585 return true;
21586 }
21587 }
21588
21589 bool VisitCallExpr(const CallExpr *E) {
21590 if (!IsConstantEvaluatedBuiltinCall(E))
21591 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21592
21593 switch (E->getBuiltinCallee()) {
21594 case Builtin::BI__assume:
21595 case Builtin::BI__builtin_assume:
21596 // The argument is not evaluated!
21597 return true;
21598
21599 case Builtin::BI__builtin_operator_delete:
21600 return HandleOperatorDeleteCall(Info, E);
21601
21602 default:
21603 return false;
21604 }
21605 }
21606
21607 bool VisitCXXDeleteExpr(const CXXDeleteExpr *E);
21608};
21609} // end anonymous namespace
21610
21611bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
21612 // We cannot speculatively evaluate a delete expression.
21613 if (Info.SpeculativeEvaluationDepth)
21614 return false;
21615
21616 FunctionDecl *OperatorDelete = E->getOperatorDelete();
21617 if (!OperatorDelete
21618 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21619 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21620 << isa<CXXMethodDecl>(Val: OperatorDelete) << OperatorDelete;
21621 return false;
21622 }
21623
21624 const Expr *Arg = E->getArgument();
21625
21626 LValue Pointer;
21627 if (!EvaluatePointer(E: Arg, Result&: Pointer, Info))
21628 return false;
21629 if (Pointer.Designator.Invalid)
21630 return false;
21631
21632 // Deleting a null pointer has no effect.
21633 if (Pointer.isNullPointer()) {
21634 // This is the only case where we need to produce an extension warning:
21635 // the only other way we can succeed is if we find a dynamic allocation,
21636 // and we will have warned when we allocated it in that case.
21637 if (!Info.getLangOpts().CPlusPlus20)
21638 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
21639 return true;
21640 }
21641
21642 std::optional<DynAlloc *> Alloc = CheckDeleteKind(
21643 Info, E, Pointer, DeallocKind: E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New);
21644 if (!Alloc)
21645 return false;
21646 QualType AllocType = Pointer.Base.getDynamicAllocType();
21647
21648 // For the non-array case, the designator must be empty if the static type
21649 // does not have a virtual destructor.
21650 if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 &&
21651 !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) {
21652 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_base_nonvirt_dtor)
21653 << Arg->getType()->getPointeeType() << AllocType;
21654 return false;
21655 }
21656
21657 // For a class type with a virtual destructor, the selected operator delete
21658 // is the one looked up when building the destructor.
21659 if (!E->isArrayForm() && !E->isGlobalDelete()) {
21660 const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType);
21661 if (VirtualDelete &&
21662 !VirtualDelete
21663 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21664 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21665 << isa<CXXMethodDecl>(Val: VirtualDelete) << VirtualDelete;
21666 return false;
21667 }
21668 }
21669
21670 if (!HandleDestruction(Info, Loc: E->getExprLoc(), LVBase: Pointer.getLValueBase(),
21671 Value&: (*Alloc)->Value, T: AllocType))
21672 return false;
21673
21674 if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) {
21675 // The element was already erased. This means the destructor call also
21676 // deleted the object.
21677 // FIXME: This probably results in undefined behavior before we get this
21678 // far, and should be diagnosed elsewhere first.
21679 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
21680 return false;
21681 }
21682
21683 return true;
21684}
21685
21686static bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
21687 assert(!E->isValueDependent());
21688 assert(E->isPRValue() && E->getType()->isVoidType());
21689 return VoidExprEvaluator(Info).Visit(S: E);
21690}
21691
21692//===----------------------------------------------------------------------===//
21693// Top level Expr::EvaluateAsRValue method.
21694//===----------------------------------------------------------------------===//
21695
21696static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
21697 assert(!E->isValueDependent());
21698 // In C, function designators are not lvalues, but we evaluate them as if they
21699 // are.
21700 QualType T = E->getType();
21701 if (E->isGLValue() || T->isFunctionType()) {
21702 LValue LV;
21703 if (!EvaluateLValue(E, Result&: LV, Info))
21704 return false;
21705 LV.moveInto(V&: Result);
21706 } else if (T->isVectorType()) {
21707 if (!EvaluateVector(E, Result, Info))
21708 return false;
21709 } else if (T->isConstantMatrixType()) {
21710 if (!EvaluateMatrix(E, Result, Info))
21711 return false;
21712 } else if (T->isIntegralOrEnumerationType()) {
21713 if (!IntExprEvaluator(Info, Result).Visit(S: E))
21714 return false;
21715 } else if (T->hasPointerRepresentation()) {
21716 LValue LV;
21717 if (!EvaluatePointer(E, Result&: LV, Info))
21718 return false;
21719 LV.moveInto(V&: Result);
21720 } else if (T->isRealFloatingType()) {
21721 llvm::APFloat F(0.0);
21722 if (!EvaluateFloat(E, Result&: F, Info))
21723 return false;
21724 Result = APValue(F);
21725 } else if (T->isAnyComplexType()) {
21726 ComplexValue C;
21727 if (!EvaluateComplex(E, Result&: C, Info))
21728 return false;
21729 C.moveInto(v&: Result);
21730 } else if (T->isFixedPointType()) {
21731 if (!FixedPointExprEvaluator(Info, Result).Visit(S: E)) return false;
21732 } else if (T->isMemberPointerType()) {
21733 MemberPtr P;
21734 if (!EvaluateMemberPointer(E, Result&: P, Info))
21735 return false;
21736 P.moveInto(V&: Result);
21737 return true;
21738 } else if (T->isArrayType()) {
21739 LValue LV;
21740 APValue &Value =
21741 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21742 if (!EvaluateArray(E, This: LV, Result&: Value, Info))
21743 return false;
21744 Result = Value;
21745 } else if (T->isRecordType()) {
21746 LValue LV;
21747 APValue &Value =
21748 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21749 if (!EvaluateRecord(E, This: LV, Result&: Value, Info))
21750 return false;
21751 Result = Value;
21752 } else if (T->isVoidType()) {
21753 if (!Info.getLangOpts().CPlusPlus11)
21754 Info.CCEDiag(E, DiagId: diag::note_constexpr_nonliteral)
21755 << E->getType();
21756 if (!EvaluateVoid(E, Info))
21757 return false;
21758 } else if (T->isAtomicType()) {
21759 QualType Unqual = T.getAtomicUnqualifiedType();
21760 if (Unqual->isArrayType() || Unqual->isRecordType()) {
21761 LValue LV;
21762 APValue &Value = Info.CurrentCall->createTemporary(
21763 Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV);
21764 if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info))
21765 return false;
21766 Result = Value;
21767 } else {
21768 if (!EvaluateAtomic(E, This: nullptr, Result, Info))
21769 return false;
21770 }
21771 } else if (Info.getLangOpts().CPlusPlus11) {
21772 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral) << E->getType();
21773 return false;
21774 } else {
21775 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
21776 return false;
21777 }
21778
21779 return true;
21780}
21781
21782/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
21783/// cases, the in-place evaluation is essential, since later initializers for
21784/// an object can indirectly refer to subobjects which were initialized earlier.
21785static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
21786 const Expr *E, bool AllowNonLiteralTypes) {
21787 assert(!E->isValueDependent());
21788
21789 // Normally expressions passed to EvaluateInPlace have a type, but not when
21790 // a VarDecl initializer is evaluated before the untyped ParenListExpr is
21791 // replaced with a CXXConstructExpr. This can happen in LLDB.
21792 if (E->getType().isNull())
21793 return false;
21794
21795 if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This))
21796 return false;
21797
21798 if (E->isPRValue()) {
21799 // Evaluate arrays and record types in-place, so that later initializers can
21800 // refer to earlier-initialized members of the object.
21801 QualType T = E->getType();
21802 if (T->isArrayType())
21803 return EvaluateArray(E, This, Result, Info);
21804 else if (T->isRecordType())
21805 return EvaluateRecord(E, This, Result, Info);
21806 else if (T->isAtomicType()) {
21807 QualType Unqual = T.getAtomicUnqualifiedType();
21808 if (Unqual->isArrayType() || Unqual->isRecordType())
21809 return EvaluateAtomic(E, This: &This, Result, Info);
21810 }
21811 }
21812
21813 // For any other type, in-place evaluation is unimportant.
21814 return Evaluate(Result, Info, E);
21815}
21816
21817/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
21818/// lvalue-to-rvalue cast if it is an lvalue.
21819static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
21820 assert(!E->isValueDependent());
21821
21822 if (E->getType().isNull())
21823 return false;
21824
21825 if (!CheckLiteralType(Info, E))
21826 return false;
21827
21828 if (Info.EnableNewConstInterp) {
21829 if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E, Result))
21830 return false;
21831 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
21832 Kind: ConstantExprKind::Normal);
21833 }
21834
21835 if (!::Evaluate(Result, Info, E))
21836 return false;
21837
21838 // Implicit lvalue-to-rvalue cast.
21839 if (E->isGLValue()) {
21840 LValue LV;
21841 LV.setFrom(Ctx: Info.Ctx, V: Result);
21842 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
21843 return false;
21844 }
21845
21846 // Check this core constant expression is a constant expression.
21847 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
21848 Kind: ConstantExprKind::Normal) &&
21849 CheckMemoryLeaks(Info);
21850}
21851
21852static bool FastEvaluateAsRValue(const Expr *Exp, APValue &Result,
21853 const ASTContext &Ctx, bool &IsConst) {
21854 // Fast-path evaluations of integer literals, since we sometimes see files
21855 // containing vast quantities of these.
21856 if (const auto *L = dyn_cast<IntegerLiteral>(Val: Exp)) {
21857 Result =
21858 APValue(APSInt(L->getValue(), L->getType()->isUnsignedIntegerType()));
21859 IsConst = true;
21860 return true;
21861 }
21862
21863 if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) {
21864 Result = APValue(APSInt(APInt(1, L->getValue())));
21865 IsConst = true;
21866 return true;
21867 }
21868
21869 if (const auto *FL = dyn_cast<FloatingLiteral>(Val: Exp)) {
21870 Result = APValue(FL->getValue());
21871 IsConst = true;
21872 return true;
21873 }
21874
21875 if (const auto *L = dyn_cast<CharacterLiteral>(Val: Exp)) {
21876 Result = APValue(Ctx.MakeIntValue(Value: L->getValue(), Type: L->getType()));
21877 IsConst = true;
21878 return true;
21879 }
21880
21881 if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) {
21882 if (CE->hasAPValueResult()) {
21883 APValue APV = CE->getAPValueResult();
21884 if (!APV.isLValue()) {
21885 Result = std::move(APV);
21886 IsConst = true;
21887 return true;
21888 }
21889 }
21890
21891 // The SubExpr is usually just an IntegerLiteral.
21892 return FastEvaluateAsRValue(Exp: CE->getSubExpr(), Result, Ctx, IsConst);
21893 }
21894
21895 // This case should be rare, but we need to check it before we check on
21896 // the type below.
21897 if (Exp->getType().isNull()) {
21898 IsConst = false;
21899 return true;
21900 }
21901
21902 return false;
21903}
21904
21905static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result,
21906 Expr::SideEffectsKind SEK) {
21907 return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) ||
21908 (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior);
21909}
21910
21911static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,
21912 const ASTContext &Ctx, EvalInfo &Info) {
21913 assert(!E->isValueDependent());
21914 bool IsConst;
21915 if (FastEvaluateAsRValue(Exp: E, Result&: Result.Val, Ctx, IsConst))
21916 return IsConst;
21917
21918 return EvaluateAsRValue(Info, E, Result&: Result.Val);
21919}
21920
21921static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult,
21922 const ASTContext &Ctx,
21923 Expr::SideEffectsKind AllowSideEffects,
21924 EvalInfo &Info) {
21925 assert(!E->isValueDependent());
21926 if (!E->getType()->isIntegralOrEnumerationType())
21927 return false;
21928
21929 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) ||
21930 !ExprResult.Val.isInt() ||
21931 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21932 return false;
21933
21934 return true;
21935}
21936
21937static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult,
21938 const ASTContext &Ctx,
21939 Expr::SideEffectsKind AllowSideEffects,
21940 EvalInfo &Info) {
21941 assert(!E->isValueDependent());
21942 if (!E->getType()->isFixedPointType())
21943 return false;
21944
21945 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info))
21946 return false;
21947
21948 if (!ExprResult.Val.isFixedPoint() ||
21949 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21950 return false;
21951
21952 return true;
21953}
21954
21955/// EvaluateAsRValue - Return true if this is a constant which we can fold using
21956/// any crazy technique (that has nothing to do with language standards) that
21957/// we want to. If this function returns true, it returns the folded constant
21958/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
21959/// will be applied to the result.
21960bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,
21961 bool InConstantContext) const {
21962 assert(!isValueDependent() &&
21963 "Expression evaluator can't be called on a dependent expression.");
21964 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue");
21965 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21966 Info.InConstantContext = InConstantContext;
21967 return ::EvaluateAsRValue(E: this, Result, Ctx, Info);
21968}
21969
21970bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx,
21971 bool InConstantContext) const {
21972 assert(!isValueDependent() &&
21973 "Expression evaluator can't be called on a dependent expression.");
21974 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition");
21975 EvalResult Scratch;
21976 return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) &&
21977 HandleConversionToBool(Val: Scratch.Val, Result);
21978}
21979
21980bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx,
21981 SideEffectsKind AllowSideEffects,
21982 bool InConstantContext) const {
21983 assert(!isValueDependent() &&
21984 "Expression evaluator can't be called on a dependent expression.");
21985 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt");
21986 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21987 Info.InConstantContext = InConstantContext;
21988 return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
21989}
21990
21991bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx,
21992 SideEffectsKind AllowSideEffects,
21993 bool InConstantContext) const {
21994 assert(!isValueDependent() &&
21995 "Expression evaluator can't be called on a dependent expression.");
21996 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint");
21997 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21998 Info.InConstantContext = InConstantContext;
21999 return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
22000}
22001
22002bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx,
22003 SideEffectsKind AllowSideEffects,
22004 bool InConstantContext) const {
22005 assert(!isValueDependent() &&
22006 "Expression evaluator can't be called on a dependent expression.");
22007
22008 if (!getType()->isRealFloatingType())
22009 return false;
22010
22011 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat");
22012 EvalResult ExprResult;
22013 if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) ||
22014 !ExprResult.Val.isFloat() ||
22015 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
22016 return false;
22017
22018 Result = ExprResult.Val.getFloat();
22019 return true;
22020}
22021
22022bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx,
22023 bool InConstantContext) const {
22024 assert(!isValueDependent() &&
22025 "Expression evaluator can't be called on a dependent expression.");
22026
22027 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue");
22028 EvalInfo Info(Ctx, Result, EvaluationMode::ConstantFold);
22029 Info.InConstantContext = InConstantContext;
22030 LValue LV;
22031 CheckedTemporaries CheckedTemps;
22032
22033 if (Info.EnableNewConstInterp) {
22034 if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val,
22035 Kind: ConstantExprKind::Normal))
22036 return false;
22037
22038 LV.setFrom(Ctx, V: Result.Val);
22039 return CheckLValueConstantExpression(
22040 Info, Loc: getExprLoc(), Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
22041 Kind: ConstantExprKind::Normal, CheckedTemps);
22042 }
22043
22044 if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() ||
22045 Result.HasSideEffects ||
22046 !CheckLValueConstantExpression(Info, Loc: getExprLoc(),
22047 Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
22048 Kind: ConstantExprKind::Normal, CheckedTemps))
22049 return false;
22050
22051 LV.moveInto(V&: Result.Val);
22052 return true;
22053}
22054
22055static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base,
22056 APValue DestroyedValue, QualType Type,
22057 SourceLocation Loc, Expr::EvalStatus &EStatus,
22058 bool IsConstantDestruction) {
22059 EvalInfo Info(Ctx, EStatus,
22060 IsConstantDestruction ? EvaluationMode::ConstantExpression
22061 : EvaluationMode::ConstantFold);
22062 Info.setEvaluatingDecl(Base, Value&: DestroyedValue,
22063 EDK: EvalInfo::EvaluatingDeclKind::Dtor);
22064 Info.InConstantContext = IsConstantDestruction;
22065
22066 LValue LVal;
22067 LVal.set(B: Base);
22068
22069 if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) ||
22070 EStatus.HasSideEffects)
22071 return false;
22072
22073 if (!Info.discardCleanups())
22074 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22075
22076 return true;
22077}
22078
22079bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
22080 ConstantExprKind Kind) const {
22081 assert(!isValueDependent() &&
22082 "Expression evaluator can't be called on a dependent expression.");
22083 bool IsConst;
22084 if (FastEvaluateAsRValue(Exp: this, Result&: Result.Val, Ctx, IsConst) &&
22085 Result.Val.hasValue())
22086 return true;
22087
22088 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");
22089 EvaluationMode EM = EvaluationMode::ConstantExpression;
22090 EvalInfo Info(Ctx, Result, EM);
22091 Info.InConstantContext = true;
22092
22093 if (Info.EnableNewConstInterp) {
22094 if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val, Kind))
22095 return false;
22096 return CheckConstantExpression(Info, DiagLoc: getExprLoc(),
22097 Type: getStorageType(Ctx, E: this), Value: Result.Val, Kind);
22098 }
22099
22100 // The type of the object we're initializing is 'const T' for a class NTTP.
22101 QualType T = getType();
22102 if (Kind == ConstantExprKind::ClassTemplateArgument)
22103 T.addConst();
22104
22105 // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to
22106 // represent the result of the evaluation. CheckConstantExpression ensures
22107 // this doesn't escape.
22108 MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true);
22109 APValue::LValueBase Base(&BaseMTE);
22110 Info.setEvaluatingDecl(Base, Value&: Result.Val);
22111
22112 LValue LVal;
22113 LVal.set(B: Base);
22114 // C++23 [intro.execution]/p5
22115 // A full-expression is [...] a constant-expression
22116 // So we need to make sure temporary objects are destroyed after having
22117 // evaluating the expression (per C++23 [class.temporary]/p4).
22118 FullExpressionRAII Scope(Info);
22119 if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E: this) ||
22120 Result.HasSideEffects || !Scope.destroy())
22121 return false;
22122
22123 if (!Info.discardCleanups())
22124 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22125
22126 if (!CheckConstantExpression(Info, DiagLoc: getExprLoc(), Type: getStorageType(Ctx, E: this),
22127 Value: Result.Val, Kind))
22128 return false;
22129 if (!CheckMemoryLeaks(Info))
22130 return false;
22131
22132 // If this is a class template argument, it's required to have constant
22133 // destruction too.
22134 if (Kind == ConstantExprKind::ClassTemplateArgument &&
22135 (!EvaluateDestruction(Ctx, Base, DestroyedValue: Result.Val, Type: T, Loc: getBeginLoc(), EStatus&: Result,
22136 IsConstantDestruction: true) ||
22137 Result.HasSideEffects)) {
22138 // FIXME: Prefix a note to indicate that the problem is lack of constant
22139 // destruction.
22140 return false;
22141 }
22142 return true;
22143}
22144
22145bool Expr::EvaluateAsInitializer(const ASTContext &Ctx, const VarDecl *VD,
22146 Expr::EvalResult &EStatus,
22147 bool IsConstantInitialization) const {
22148 assert(!isValueDependent() &&
22149 "Expression evaluator can't be called on a dependent expression.");
22150 assert(VD && "Need a valid VarDecl");
22151
22152 llvm::TimeTraceScope TimeScope("EvaluateAsInitializer", [&] {
22153 std::string Name;
22154 llvm::raw_string_ostream OS(Name);
22155 VD->printQualifiedName(OS);
22156 return Name;
22157 });
22158
22159 EvalInfo Info(Ctx, EStatus,
22160 (IsConstantInitialization &&
22161 (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23))
22162 ? EvaluationMode::ConstantExpression
22163 : EvaluationMode::ConstantFold);
22164 Info.setEvaluatingDecl(Base: VD, Value&: EStatus.Val);
22165 Info.InConstantContext = IsConstantInitialization;
22166
22167 SourceLocation DeclLoc = VD->getLocation();
22168 QualType DeclTy = VD->getType();
22169
22170 if (Info.EnableNewConstInterp) {
22171 auto &InterpCtx = Ctx.getInterpContext();
22172 if (!InterpCtx.evaluateAsInitializer(Parent&: Info, VD, Init: this, Result&: EStatus.Val))
22173 return false;
22174
22175 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
22176 Kind: ConstantExprKind::Normal);
22177 } else {
22178 LValue LVal;
22179 LVal.set(B: VD);
22180
22181 {
22182 // C++23 [intro.execution]/p5
22183 // A full-expression is ... an init-declarator ([dcl.decl]) or a
22184 // mem-initializer.
22185 // So we need to make sure temporary objects are destroyed after having
22186 // evaluated the expression (per C++23 [class.temporary]/p4).
22187 //
22188 // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the
22189 // serialization code calls ParmVarDecl::getDefaultArg() which strips the
22190 // outermost FullExpr, such as ExprWithCleanups.
22191 FullExpressionRAII Scope(Info);
22192 if (!EvaluateInPlace(Result&: EStatus.Val, Info, This: LVal, E: this,
22193 /*AllowNonLiteralTypes=*/true) ||
22194 EStatus.HasSideEffects)
22195 return false;
22196 }
22197
22198 // At this point, any lifetime-extended temporaries are completely
22199 // initialized.
22200 Info.performLifetimeExtension();
22201
22202 if (!Info.discardCleanups())
22203 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
22204 }
22205
22206 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
22207 Kind: ConstantExprKind::Normal) &&
22208 CheckMemoryLeaks(Info);
22209}
22210
22211bool VarDecl::evaluateDestruction(
22212 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
22213 // This function is only meaningful for records and arrays of records.
22214 QualType VarTy = getType();
22215 if (VarTy->isArrayType()) {
22216 QualType ElemTy = getASTContext().getBaseElementType(QT: VarTy);
22217 if (!ElemTy->isRecordType()) {
22218 ensureEvaluatedStmt()->HasConstantDestruction = true;
22219 return true;
22220 }
22221 } else if (!VarTy->isRecordType()) {
22222 ensureEvaluatedStmt()->HasConstantDestruction = true;
22223 return true;
22224 }
22225
22226 Expr::EvalStatus EStatus;
22227 EStatus.Diag = &Notes;
22228
22229 // Only treat the destruction as constant destruction if we formally have
22230 // constant initialization (or are usable in a constant expression).
22231 bool IsConstantDestruction = hasConstantInitialization();
22232 ASTContext &Ctx = getASTContext();
22233
22234 // Make a copy of the value for the destructor to mutate, if we know it.
22235 // Otherwise, treat the value as default-initialized; if the destructor works
22236 // anyway, then the destruction is constant (and must be essentially empty).
22237 APValue DestroyedValue;
22238 if (getEvaluatedValue())
22239 DestroyedValue = *getEvaluatedValue();
22240 else if (!handleDefaultInitValue(T: VarTy, Result&: DestroyedValue))
22241 return false;
22242
22243 if (Ctx.getLangOpts().EnableNewConstInterp) {
22244 EvalInfo Info(Ctx, EStatus,
22245 IsConstantDestruction ? EvaluationMode::ConstantExpression
22246 : EvaluationMode::ConstantFold);
22247 Info.InConstantContext = IsConstantDestruction;
22248 if (!Ctx.getInterpContext().evaluateDestruction(Parent&: Info, VD: this,
22249 Value: std::move(DestroyedValue)))
22250 return false;
22251 ensureEvaluatedStmt()->HasConstantDestruction = true;
22252 return true;
22253 }
22254
22255 if (!EvaluateDestruction(Ctx, Base: this, DestroyedValue: std::move(DestroyedValue), Type: VarTy,
22256 Loc: getLocation(), EStatus, IsConstantDestruction) ||
22257 EStatus.HasSideEffects)
22258 return false;
22259
22260 ensureEvaluatedStmt()->HasConstantDestruction = true;
22261 return true;
22262}
22263
22264/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
22265/// constant folded, but discard the result.
22266bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const {
22267 assert(!isValueDependent() &&
22268 "Expression evaluator can't be called on a dependent expression.");
22269
22270 EvalResult Result;
22271 return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) &&
22272 !hasUnacceptableSideEffect(Result, SEK);
22273}
22274
22275APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
22276 assert(!isValueDependent() &&
22277 "Expression evaluator can't be called on a dependent expression.");
22278
22279 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt");
22280 EvalResult EVResult;
22281 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22282 Info.InConstantContext = true;
22283
22284 bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info);
22285 (void)Result;
22286 assert(Result && "Could not evaluate expression");
22287 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22288
22289 return EVResult.Val.getInt();
22290}
22291
22292APSInt Expr::EvaluateKnownConstIntCheckOverflow(
22293 const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const {
22294 assert(!isValueDependent() &&
22295 "Expression evaluator can't be called on a dependent expression.");
22296
22297 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow");
22298 EvalResult EVResult;
22299 EVResult.Diag = Diag;
22300 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22301 Info.InConstantContext = true;
22302 Info.CheckingForUndefinedBehavior = true;
22303
22304 bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22305 (void)Result;
22306 assert(Result && "Could not evaluate expression");
22307 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
22308
22309 return EVResult.Val.getInt();
22310}
22311
22312void Expr::EvaluateForOverflow(const ASTContext &Ctx) const {
22313 assert(!isValueDependent() &&
22314 "Expression evaluator can't be called on a dependent expression.");
22315
22316 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow");
22317 bool IsConst;
22318 EvalResult EVResult;
22319 if (!FastEvaluateAsRValue(Exp: this, Result&: EVResult.Val, Ctx, IsConst)) {
22320 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
22321 Info.CheckingForUndefinedBehavior = true;
22322 (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
22323 }
22324}
22325
22326bool Expr::EvalResult::isGlobalLValue() const {
22327 assert(Val.isLValue());
22328 return IsGlobalLValue(B: Val.getLValueBase());
22329}
22330
22331/// isIntegerConstantExpr - this recursive routine will test if an expression is
22332/// an integer constant expression.
22333
22334/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
22335/// comma, etc
22336
22337// CheckICE - This function does the fundamental ICE checking: the returned
22338// ICEDiag contains an ICEKind indicating whether the expression is an ICE.
22339//
22340// Note that to reduce code duplication, this helper does no evaluation
22341// itself; the caller checks whether the expression is evaluatable, and
22342// in the rare cases where CheckICE actually cares about the evaluated
22343// value, it calls into Evaluate.
22344
22345namespace {
22346
22347enum ICEKind {
22348 /// This expression is an ICE.
22349 IK_ICE,
22350 /// This expression is not an ICE, but if it isn't evaluated, it's
22351 /// a legal subexpression for an ICE. This return value is used to handle
22352 /// the comma operator in C99 mode, and non-constant subexpressions.
22353 IK_ICEIfUnevaluated,
22354 /// This expression is not an ICE, and is not a legal subexpression for one.
22355 IK_NotICE
22356};
22357
22358struct ICEDiag {
22359 ICEKind Kind;
22360 SourceLocation Loc;
22361
22362 ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {}
22363};
22364
22365}
22366
22367static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); }
22368
22369static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; }
22370
22371static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) {
22372 Expr::EvalResult EVResult;
22373 Expr::EvalStatus Status;
22374 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22375
22376 Info.InConstantContext = true;
22377 if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects ||
22378 !EVResult.Val.isInt())
22379 return ICEDiag(IK_NotICE, E->getBeginLoc());
22380
22381 return NoDiag();
22382}
22383
22384static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {
22385 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
22386 if (!E->getType()->isIntegralOrEnumerationType())
22387 return ICEDiag(IK_NotICE, E->getBeginLoc());
22388
22389 switch (E->getStmtClass()) {
22390#define ABSTRACT_STMT(Node)
22391#define STMT(Node, Base) case Expr::Node##Class:
22392#define EXPR(Node, Base)
22393#include "clang/AST/StmtNodes.inc"
22394 case Expr::PredefinedExprClass:
22395 case Expr::FloatingLiteralClass:
22396 case Expr::ImaginaryLiteralClass:
22397 case Expr::StringLiteralClass:
22398 case Expr::ArraySubscriptExprClass:
22399 case Expr::MatrixSingleSubscriptExprClass:
22400 case Expr::MatrixSubscriptExprClass:
22401 case Expr::ArraySectionExprClass:
22402 case Expr::OMPArrayShapingExprClass:
22403 case Expr::OMPIteratorExprClass:
22404 case Expr::CompoundAssignOperatorClass:
22405 case Expr::CompoundLiteralExprClass:
22406 case Expr::ExtVectorElementExprClass:
22407 case Expr::MatrixElementExprClass:
22408 case Expr::DesignatedInitExprClass:
22409 case Expr::ArrayInitLoopExprClass:
22410 case Expr::ArrayInitIndexExprClass:
22411 case Expr::NoInitExprClass:
22412 case Expr::DesignatedInitUpdateExprClass:
22413 case Expr::ImplicitValueInitExprClass:
22414 case Expr::ParenListExprClass:
22415 case Expr::VAArgExprClass:
22416 case Expr::AddrLabelExprClass:
22417 case Expr::StmtExprClass:
22418 case Expr::CXXMemberCallExprClass:
22419 case Expr::CUDAKernelCallExprClass:
22420 case Expr::CXXAddrspaceCastExprClass:
22421 case Expr::CXXDynamicCastExprClass:
22422 case Expr::CXXTypeidExprClass:
22423 case Expr::CXXUuidofExprClass:
22424 case Expr::MSPropertyRefExprClass:
22425 case Expr::MSPropertySubscriptExprClass:
22426 case Expr::CXXNullPtrLiteralExprClass:
22427 case Expr::UserDefinedLiteralClass:
22428 case Expr::CXXThisExprClass:
22429 case Expr::CXXThrowExprClass:
22430 case Expr::CXXNewExprClass:
22431 case Expr::CXXDeleteExprClass:
22432 case Expr::CXXPseudoDestructorExprClass:
22433 case Expr::UnresolvedLookupExprClass:
22434 case Expr::RecoveryExprClass:
22435 case Expr::DependentScopeDeclRefExprClass:
22436 case Expr::DependentTemplateIdExprClass:
22437 case Expr::CXXConstructExprClass:
22438 case Expr::CXXInheritedCtorInitExprClass:
22439 case Expr::CXXStdInitializerListExprClass:
22440 case Expr::CXXBindTemporaryExprClass:
22441 case Expr::ExprWithCleanupsClass:
22442 case Expr::CXXTemporaryObjectExprClass:
22443 case Expr::CXXUnresolvedConstructExprClass:
22444 case Expr::CXXDependentScopeMemberExprClass:
22445 case Expr::UnresolvedMemberExprClass:
22446 case Expr::ObjCStringLiteralClass:
22447 case Expr::ObjCBoxedExprClass:
22448 case Expr::ObjCArrayLiteralClass:
22449 case Expr::ObjCDictionaryLiteralClass:
22450 case Expr::ObjCEncodeExprClass:
22451 case Expr::ObjCMessageExprClass:
22452 case Expr::ObjCSelectorExprClass:
22453 case Expr::ObjCProtocolExprClass:
22454 case Expr::ObjCIvarRefExprClass:
22455 case Expr::ObjCPropertyRefExprClass:
22456 case Expr::ObjCSubscriptRefExprClass:
22457 case Expr::ObjCIsaExprClass:
22458 case Expr::ObjCAvailabilityCheckExprClass:
22459 case Expr::ShuffleVectorExprClass:
22460 case Expr::ConvertVectorExprClass:
22461 case Expr::BlockExprClass:
22462 case Expr::NoStmtClass:
22463 case Expr::OpaqueValueExprClass:
22464 case Expr::PackExpansionExprClass:
22465 case Expr::SubstNonTypeTemplateParmPackExprClass:
22466 case Expr::FunctionParmPackExprClass:
22467 case Expr::AsTypeExprClass:
22468 case Expr::ObjCIndirectCopyRestoreExprClass:
22469 case Expr::MaterializeTemporaryExprClass:
22470 case Expr::PseudoObjectExprClass:
22471 case Expr::AtomicExprClass:
22472 case Expr::LambdaExprClass:
22473 case Expr::CXXFoldExprClass:
22474 case Expr::CoawaitExprClass:
22475 case Expr::DependentCoawaitExprClass:
22476 case Expr::CoyieldExprClass:
22477 case Expr::SYCLUniqueStableNameExprClass:
22478 case Expr::CXXParenListInitExprClass:
22479 case Expr::HLSLOutArgExprClass:
22480 case Expr::CXXExpansionSelectExprClass:
22481 return ICEDiag(IK_NotICE, E->getBeginLoc());
22482
22483 case Expr::MemberExprClass: {
22484 if (Ctx.getLangOpts().C23) {
22485 const Expr *ME = E->IgnoreParenImpCasts();
22486 while (const auto *M = dyn_cast<MemberExpr>(Val: ME)) {
22487 if (M->isArrow())
22488 return ICEDiag(IK_NotICE, E->getBeginLoc());
22489 ME = M->getBase()->IgnoreParenImpCasts();
22490 }
22491 const auto *DRE = dyn_cast<DeclRefExpr>(Val: ME);
22492 if (DRE) {
22493 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl());
22494 VD && VD->isConstexpr())
22495 return CheckEvalInICE(E, Ctx);
22496 }
22497 }
22498 return ICEDiag(IK_NotICE, E->getBeginLoc());
22499 }
22500
22501 case Expr::InitListExprClass: {
22502 // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the
22503 // form "T x = { a };" is equivalent to "T x = a;".
22504 // Unless we're initializing a reference, T is a scalar as it is known to be
22505 // of integral or enumeration type.
22506 if (E->isPRValue())
22507 if (cast<InitListExpr>(Val: E)->getNumInits() == 1)
22508 return CheckICE(E: cast<InitListExpr>(Val: E)->getInit(Init: 0), Ctx);
22509 return ICEDiag(IK_NotICE, E->getBeginLoc());
22510 }
22511
22512 case Expr::SizeOfPackExprClass:
22513 case Expr::GNUNullExprClass:
22514 case Expr::SourceLocExprClass:
22515 case Expr::EmbedExprClass:
22516 case Expr::OpenACCAsteriskSizeExprClass:
22517 return NoDiag();
22518
22519 case Expr::PackIndexingExprClass:
22520 return CheckICE(E: cast<PackIndexingExpr>(Val: E)->getSelectedExpr(), Ctx);
22521
22522 case Expr::SubstNonTypeTemplateParmExprClass:
22523 return
22524 CheckICE(E: cast<SubstNonTypeTemplateParmExpr>(Val: E)->getReplacement(), Ctx);
22525
22526 case Expr::ConstantExprClass:
22527 return CheckICE(E: cast<ConstantExpr>(Val: E)->getSubExpr(), Ctx);
22528
22529 case Expr::ParenExprClass:
22530 return CheckICE(E: cast<ParenExpr>(Val: E)->getSubExpr(), Ctx);
22531 case Expr::GenericSelectionExprClass:
22532 return CheckICE(E: cast<GenericSelectionExpr>(Val: E)->getResultExpr(), Ctx);
22533 case Expr::IntegerLiteralClass:
22534 case Expr::FixedPointLiteralClass:
22535 case Expr::CharacterLiteralClass:
22536 case Expr::ObjCBoolLiteralExprClass:
22537 case Expr::CXXBoolLiteralExprClass:
22538 case Expr::CXXScalarValueInitExprClass:
22539 case Expr::TypeTraitExprClass:
22540 case Expr::ConceptSpecializationExprClass:
22541 case Expr::RequiresExprClass:
22542 case Expr::ArrayTypeTraitExprClass:
22543 case Expr::ExpressionTraitExprClass:
22544 case Expr::CXXNoexceptExprClass:
22545 case Expr::CXXReflectExprClass:
22546 return NoDiag();
22547 case Expr::CallExprClass:
22548 case Expr::CXXOperatorCallExprClass: {
22549 // C99 6.6/3 allows function calls within unevaluated subexpressions of
22550 // constant expressions, but they can never be ICEs because an ICE cannot
22551 // contain an operand of (pointer to) function type.
22552 const CallExpr *CE = cast<CallExpr>(Val: E);
22553 if (CE->getBuiltinCallee())
22554 return CheckEvalInICE(E, Ctx);
22555 return ICEDiag(IK_NotICE, E->getBeginLoc());
22556 }
22557 case Expr::CXXRewrittenBinaryOperatorClass:
22558 return CheckICE(E: cast<CXXRewrittenBinaryOperator>(Val: E)->getSemanticForm(),
22559 Ctx);
22560 case Expr::DeclRefExprClass: {
22561 const NamedDecl *D = cast<DeclRefExpr>(Val: E)->getDecl();
22562 if (isa<EnumConstantDecl>(Val: D))
22563 return NoDiag();
22564
22565 // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified
22566 // integer variables in constant expressions:
22567 //
22568 // C++ 7.1.5.1p2
22569 // A variable of non-volatile const-qualified integral or enumeration
22570 // type initialized by an ICE can be used in ICEs.
22571 //
22572 // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In
22573 // that mode, use of reference variables should not be allowed.
22574 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
22575 if (VD && VD->isUsableInConstantExpressions(C: Ctx) &&
22576 !VD->getType()->isReferenceType())
22577 return NoDiag();
22578
22579 return ICEDiag(IK_NotICE, E->getBeginLoc());
22580 }
22581 case Expr::UnaryOperatorClass: {
22582 const UnaryOperator *Exp = cast<UnaryOperator>(Val: E);
22583 switch (Exp->getOpcode()) {
22584 case UO_PostInc:
22585 case UO_PostDec:
22586 case UO_PreInc:
22587 case UO_PreDec:
22588 case UO_AddrOf:
22589 case UO_Deref:
22590 case UO_Coawait:
22591 // C99 6.6/3 allows increment and decrement within unevaluated
22592 // subexpressions of constant expressions, but they can never be ICEs
22593 // because an ICE cannot contain an lvalue operand.
22594 return ICEDiag(IK_NotICE, E->getBeginLoc());
22595 case UO_Extension:
22596 case UO_LNot:
22597 case UO_Plus:
22598 case UO_Minus:
22599 case UO_Not:
22600 case UO_Real:
22601 case UO_Imag:
22602 return CheckICE(E: Exp->getSubExpr(), Ctx);
22603 }
22604 llvm_unreachable("invalid unary operator class");
22605 }
22606 case Expr::OffsetOfExprClass: {
22607 // Note that per C99, offsetof must be an ICE. And AFAIK, using
22608 // EvaluateAsRValue matches the proposed gcc behavior for cases like
22609 // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect
22610 // compliance: we should warn earlier for offsetof expressions with
22611 // array subscripts that aren't ICEs, and if the array subscripts
22612 // are ICEs, the value of the offsetof must be an integer constant.
22613 return CheckEvalInICE(E, Ctx);
22614 }
22615 case Expr::UnaryExprOrTypeTraitExprClass: {
22616 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(Val: E);
22617 if ((Exp->getKind() == UETT_SizeOf) &&
22618 Exp->getTypeOfArgument()->isVariableArrayType())
22619 return ICEDiag(IK_NotICE, E->getBeginLoc());
22620 if (Exp->getKind() == UETT_CountOf) {
22621 QualType ArgTy = Exp->getTypeOfArgument();
22622 if (ArgTy->isVariableArrayType()) {
22623 // We need to look whether the array is multidimensional. If it is,
22624 // then we want to check the size expression manually to see whether
22625 // it is an ICE or not.
22626 const auto *VAT = Ctx.getAsVariableArrayType(T: ArgTy);
22627 if (VAT->getElementType()->isArrayType())
22628 // Variable array size expression could be missing (e.g. int a[*][10])
22629 // In that case, it can't be a constant expression.
22630 return VAT->getSizeExpr() ? CheckICE(E: VAT->getSizeExpr(), Ctx)
22631 : ICEDiag(IK_NotICE, E->getBeginLoc());
22632
22633 // Otherwise, this is a regular VLA, which is definitely not an ICE.
22634 return ICEDiag(IK_NotICE, E->getBeginLoc());
22635 }
22636 }
22637 return NoDiag();
22638 }
22639 case Expr::BinaryOperatorClass: {
22640 const BinaryOperator *Exp = cast<BinaryOperator>(Val: E);
22641 switch (Exp->getOpcode()) {
22642 case BO_PtrMemD:
22643 case BO_PtrMemI:
22644 case BO_Assign:
22645 case BO_MulAssign:
22646 case BO_DivAssign:
22647 case BO_RemAssign:
22648 case BO_AddAssign:
22649 case BO_SubAssign:
22650 case BO_ShlAssign:
22651 case BO_ShrAssign:
22652 case BO_AndAssign:
22653 case BO_XorAssign:
22654 case BO_OrAssign:
22655 // C99 6.6/3 allows assignments within unevaluated subexpressions of
22656 // constant expressions, but they can never be ICEs because an ICE cannot
22657 // contain an lvalue operand.
22658 return ICEDiag(IK_NotICE, E->getBeginLoc());
22659
22660 case BO_Mul:
22661 case BO_Div:
22662 case BO_Rem:
22663 case BO_Add:
22664 case BO_Sub:
22665 case BO_Shl:
22666 case BO_Shr:
22667 case BO_LT:
22668 case BO_GT:
22669 case BO_LE:
22670 case BO_GE:
22671 case BO_EQ:
22672 case BO_NE:
22673 case BO_And:
22674 case BO_Xor:
22675 case BO_Or:
22676 case BO_Comma:
22677 case BO_Cmp: {
22678 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22679 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22680 if (Exp->getOpcode() == BO_Div ||
22681 Exp->getOpcode() == BO_Rem) {
22682 // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
22683 // we don't evaluate one.
22684 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) {
22685 llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
22686 if (REval == 0)
22687 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22688 if (REval.isSigned() && REval.isAllOnes()) {
22689 llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
22690 if (LEval.isMinSignedValue())
22691 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22692 }
22693 }
22694 }
22695 if (Exp->getOpcode() == BO_Comma) {
22696 if (Ctx.getLangOpts().C99) {
22697 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
22698 // if it isn't evaluated.
22699 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE)
22700 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22701 } else {
22702 // In both C89 and C++, commas in ICEs are illegal.
22703 return ICEDiag(IK_NotICE, E->getBeginLoc());
22704 }
22705 }
22706 return Worst(A: LHSResult, B: RHSResult);
22707 }
22708 case BO_LAnd:
22709 case BO_LOr: {
22710 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22711 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22712 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) {
22713 // Rare case where the RHS has a comma "side-effect"; we need
22714 // to actually check the condition to see whether the side
22715 // with the comma is evaluated.
22716 if ((Exp->getOpcode() == BO_LAnd) !=
22717 (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
22718 return RHSResult;
22719 return NoDiag();
22720 }
22721
22722 return Worst(A: LHSResult, B: RHSResult);
22723 }
22724 }
22725 llvm_unreachable("invalid binary operator kind");
22726 }
22727 case Expr::ImplicitCastExprClass:
22728 case Expr::CStyleCastExprClass:
22729 case Expr::CXXFunctionalCastExprClass:
22730 case Expr::CXXStaticCastExprClass:
22731 case Expr::CXXReinterpretCastExprClass:
22732 case Expr::CXXConstCastExprClass:
22733 case Expr::ObjCBridgedCastExprClass: {
22734 const Expr *SubExpr = cast<CastExpr>(Val: E)->getSubExpr();
22735 if (isa<ExplicitCastExpr>(Val: E)) {
22736 if (const FloatingLiteral *FL
22737 = dyn_cast<FloatingLiteral>(Val: SubExpr->IgnoreParenImpCasts())) {
22738 unsigned DestWidth = Ctx.getIntWidth(T: E->getType());
22739 bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
22740 APSInt IgnoredVal(DestWidth, !DestSigned);
22741 bool Ignored;
22742 // If the value does not fit in the destination type, the behavior is
22743 // undefined, so we are not required to treat it as a constant
22744 // expression.
22745 if (FL->getValue().convertToInteger(Result&: IgnoredVal,
22746 RM: llvm::APFloat::rmTowardZero,
22747 IsExact: &Ignored) & APFloat::opInvalidOp)
22748 return ICEDiag(IK_NotICE, E->getBeginLoc());
22749 return NoDiag();
22750 }
22751 }
22752 switch (cast<CastExpr>(Val: E)->getCastKind()) {
22753 case CK_LValueToRValue:
22754 case CK_AtomicToNonAtomic:
22755 case CK_NonAtomicToAtomic:
22756 case CK_NoOp:
22757 case CK_IntegralToBoolean:
22758 case CK_IntegralCast:
22759 return CheckICE(E: SubExpr, Ctx);
22760 default:
22761 return ICEDiag(IK_NotICE, E->getBeginLoc());
22762 }
22763 }
22764 case Expr::BinaryConditionalOperatorClass: {
22765 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(Val: E);
22766 ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx);
22767 if (CommonResult.Kind == IK_NotICE) return CommonResult;
22768 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
22769 if (FalseResult.Kind == IK_NotICE) return FalseResult;
22770 if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult;
22771 if (FalseResult.Kind == IK_ICEIfUnevaluated &&
22772 Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag();
22773 return FalseResult;
22774 }
22775 case Expr::ConditionalOperatorClass: {
22776 const ConditionalOperator *Exp = cast<ConditionalOperator>(Val: E);
22777 // If the condition (ignoring parens) is a __builtin_constant_p call,
22778 // then only the true side is actually considered in an integer constant
22779 // expression, and it is fully evaluated. This is an important GNU
22780 // extension. See GCC PR38377 for discussion.
22781 if (const CallExpr *CallCE
22782 = dyn_cast<CallExpr>(Val: Exp->getCond()->IgnoreParenCasts()))
22783 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
22784 return CheckEvalInICE(E, Ctx);
22785 ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx);
22786 if (CondResult.Kind == IK_NotICE)
22787 return CondResult;
22788
22789 ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx);
22790 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
22791
22792 if (TrueResult.Kind == IK_NotICE)
22793 return TrueResult;
22794 if (FalseResult.Kind == IK_NotICE)
22795 return FalseResult;
22796 if (CondResult.Kind == IK_ICEIfUnevaluated)
22797 return CondResult;
22798 if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE)
22799 return NoDiag();
22800 // Rare case where the diagnostics depend on which side is evaluated
22801 // Note that if we get here, CondResult is 0, and at least one of
22802 // TrueResult and FalseResult is non-zero.
22803 if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0)
22804 return FalseResult;
22805 return TrueResult;
22806 }
22807 case Expr::CXXDefaultArgExprClass:
22808 return CheckICE(E: cast<CXXDefaultArgExpr>(Val: E)->getExpr(), Ctx);
22809 case Expr::CXXDefaultInitExprClass:
22810 return CheckICE(E: cast<CXXDefaultInitExpr>(Val: E)->getExpr(), Ctx);
22811 case Expr::ChooseExprClass: {
22812 return CheckICE(E: cast<ChooseExpr>(Val: E)->getChosenSubExpr(), Ctx);
22813 }
22814 case Expr::BuiltinBitCastExprClass: {
22815 if (!checkBitCastConstexprEligibility(Info: nullptr, Ctx, BCE: cast<CastExpr>(Val: E)))
22816 return ICEDiag(IK_NotICE, E->getBeginLoc());
22817 return CheckICE(E: cast<CastExpr>(Val: E)->getSubExpr(), Ctx);
22818 }
22819 }
22820
22821 llvm_unreachable("Invalid StmtClass!");
22822}
22823
22824/// Evaluate an expression as a C++11 integral constant expression.
22825static bool
22826EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx, const Expr *E,
22827 llvm::APSInt *Value,
22828 bool AllowRelaxedEval = false) {
22829 if (!E->getType()->isIntegralOrUnscopedEnumerationType())
22830 return false;
22831
22832 APValue Result;
22833 if (!E->isCXX11ConstantExpr(Ctx, Result: &Result, AllowRelaxedEval))
22834 return false;
22835
22836 if (!Result.isInt())
22837 return false;
22838
22839 if (Value) *Value = Result.getInt();
22840 return true;
22841}
22842
22843bool Expr::isIntegerConstantExpr(const ASTContext &Ctx) const {
22844 assert(!isValueDependent() &&
22845 "Expression evaluator can't be called on a dependent expression.");
22846
22847 ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr");
22848
22849 if (Ctx.getLangOpts().CPlusPlus11)
22850 return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr);
22851
22852 ICEDiag D = CheckICE(E: this, Ctx);
22853 if (D.Kind != IK_ICE)
22854 return false;
22855 return true;
22856}
22857
22858std::optional<llvm::APSInt>
22859Expr::getIntegerConstantExpr(const ASTContext &Ctx,
22860 bool AllowRelaxedEval) const {
22861 if (isValueDependent()) {
22862 // Expression evaluator can't succeed on a dependent expression.
22863 return std::nullopt;
22864 }
22865
22866 if (Ctx.getLangOpts().CPlusPlus11) {
22867 APSInt Value;
22868 if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value,
22869 AllowRelaxedEval))
22870 return Value;
22871 return std::nullopt;
22872 }
22873
22874 if (!isIntegerConstantExpr(Ctx))
22875 return std::nullopt;
22876
22877 // The only possible side-effects here are due to UB discovered in the
22878 // evaluation (for instance, INT_MAX + 1). In such a case, we are still
22879 // required to treat the expression as an ICE, so we produce the folded
22880 // value.
22881 EvalResult ExprResult;
22882 Expr::EvalStatus Status;
22883 EvalInfo Info(Ctx, Status, EvaluationMode::IgnoreSideEffects);
22884 Info.InConstantContext = true;
22885
22886 if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info))
22887 llvm_unreachable("ICE cannot be evaluated!");
22888
22889 return ExprResult.Val.getInt();
22890}
22891
22892bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const {
22893 assert(!isValueDependent() &&
22894 "Expression evaluator can't be called on a dependent expression.");
22895
22896 return CheckICE(E: this, Ctx).Kind == IK_ICE;
22897}
22898
22899bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result,
22900 bool AllowRelaxedEval) const {
22901 assert(!isValueDependent() &&
22902 "Expression evaluator can't be called on a dependent expression.");
22903
22904 // We support this checking in C++98 mode in order to diagnose compatibility
22905 // issues.
22906 assert(Ctx.getLangOpts().CPlusPlus);
22907
22908 bool IsConst;
22909 APValue Scratch;
22910 if (FastEvaluateAsRValue(Exp: this, Result&: Scratch, Ctx, IsConst) && Scratch.hasValue()) {
22911 if (Result)
22912 *Result = std::move(Scratch);
22913 return true;
22914 }
22915
22916 // Build evaluation settings.
22917 Expr::EvalStatus Status;
22918 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22919 SmallVector<PartialDiagnosticAt> MSRelaxedDiag;
22920 Status.ExtendedDiag = AllowRelaxedEval ? &MSRelaxedDiag : nullptr;
22921
22922 bool IsConstExpr =
22923 ::EvaluateAsRValue(Info, E: this, Result&: Result ? *Result : Scratch) &&
22924 // NOTE: We don't produce a diagnostic for this, but the callers that
22925 // call us on arbitrary full-expressions should generally not care.
22926 Info.discardCleanups() && !Status.HasSideEffects;
22927
22928 return IsConstExpr && !Status.DiagEmitted;
22929}
22930
22931bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx,
22932 const FunctionDecl *Callee,
22933 ArrayRef<const Expr*> Args,
22934 const Expr *This) const {
22935 assert(!isValueDependent() &&
22936 "Expression evaluator can't be called on a dependent expression.");
22937
22938 llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution", [&] {
22939 std::string Name;
22940 llvm::raw_string_ostream OS(Name);
22941 Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(),
22942 /*Qualified=*/true);
22943 return Name;
22944 });
22945
22946 Expr::EvalStatus Status;
22947 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpressionUnevaluated);
22948 Info.InConstantContext = true;
22949
22950 if (Info.EnableNewConstInterp) {
22951 if (std::optional<bool> BoolResult =
22952 Info.Ctx.getInterpContext().evaluateWithSubstitution(
22953 Parent&: Info, Callee, Args, This, Condition: this)) {
22954 Value = APValue(APSInt(APInt(1, static_cast<uint64_t>(*BoolResult))));
22955 return true;
22956 }
22957 return false;
22958 }
22959
22960 LValue ThisVal;
22961 const LValue *ThisPtr = nullptr;
22962 if (This) {
22963#ifndef NDEBUG
22964 auto *MD = dyn_cast<CXXMethodDecl>(Callee);
22965 assert(MD && "Don't provide `this` for non-methods.");
22966 assert(MD->isImplicitObjectMemberFunction() &&
22967 "Don't provide `this` for methods without an implicit object.");
22968#endif
22969 if (!This->isValueDependent() &&
22970 EvaluateObjectArgument(Info, Object: This, This&: ThisVal) &&
22971 !Info.EvalStatus.HasSideEffects)
22972 ThisPtr = &ThisVal;
22973
22974 // Ignore any side-effects from a failed evaluation. This is safe because
22975 // they can't interfere with any other argument evaluation.
22976 Info.EvalStatus.HasSideEffects = false;
22977 }
22978
22979 CallRef Call = Info.CurrentCall->createCall(Callee);
22980 for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
22981 I != E; ++I) {
22982 unsigned Idx = I - Args.begin();
22983 if (Idx >= Callee->getNumParams())
22984 break;
22985 const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx);
22986 if ((*I)->isValueDependent() ||
22987 !EvaluateCallArg(PVD, Arg: *I, Call, Info) ||
22988 Info.EvalStatus.HasSideEffects) {
22989 // If evaluation fails, throw away the argument entirely.
22990 if (APValue *Slot = Info.getParamSlot(Call, PVD))
22991 *Slot = APValue();
22992 }
22993
22994 // Ignore any side-effects from a failed evaluation. This is safe because
22995 // they can't interfere with any other argument evaluation.
22996 Info.EvalStatus.HasSideEffects = false;
22997 }
22998
22999 // Parameter cleanups happen in the caller and are not part of this
23000 // evaluation.
23001 Info.discardCleanups();
23002 Info.EvalStatus.HasSideEffects = false;
23003
23004 // Build fake call to Callee.
23005 CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This,
23006 Call);
23007 // FIXME: Missing ExprWithCleanups in enable_if conditions?
23008 FullExpressionRAII Scope(Info);
23009 return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() &&
23010 !Info.EvalStatus.HasSideEffects;
23011}
23012
23013bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
23014 SmallVectorImpl<
23015 PartialDiagnosticAt> &Diags) {
23016 // FIXME: It would be useful to check constexpr function templates, but at the
23017 // moment the constant expression evaluator cannot cope with the non-rigorous
23018 // ASTs which we build for dependent expressions.
23019 if (FD->isDependentContext())
23020 return true;
23021
23022 llvm::TimeTraceScope TimeScope("isPotentialConstantExpr", [&] {
23023 std::string Name;
23024 llvm::raw_string_ostream OS(Name);
23025 FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(),
23026 /*Qualified=*/true);
23027 return Name;
23028 });
23029
23030 Expr::EvalStatus Status;
23031 Status.Diag = &Diags;
23032
23033 EvalInfo Info(FD->getASTContext(), Status,
23034 EvaluationMode::ConstantExpression);
23035 Info.InConstantContext = true;
23036 Info.CheckingPotentialConstantExpression = true;
23037
23038 // The constexpr VM attempts to compile all methods to bytecode here.
23039 if (Info.EnableNewConstInterp) {
23040 Info.Ctx.getInterpContext().isPotentialConstantExpr(Parent&: Info, FD);
23041 return Diags.empty();
23042 }
23043
23044 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
23045 const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr;
23046
23047 // Fabricate an arbitrary expression on the stack and pretend that it
23048 // is a temporary being used as the 'this' pointer.
23049 LValue This;
23050 ImplicitValueInitExpr VIE(RD ? Info.Ctx.getCanonicalTagType(TD: RD)
23051 : Info.Ctx.IntTy);
23052 This.set(B: {&VIE, Info.CurrentCall->Index});
23053
23054 ArrayRef<const Expr*> Args;
23055
23056 APValue Scratch;
23057 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) {
23058 // Evaluate the call as a constant initializer, to allow the construction
23059 // of objects of non-literal types.
23060 Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch);
23061 HandleConstructorCall(E: &VIE, This, Args, Definition: CD, Info, Result&: Scratch);
23062 } else {
23063 SourceLocation Loc = FD->getLocation();
23064 HandleFunctionCall(
23065 CallLoc: Loc, Callee: FD, ObjectArg: (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr,
23066 E: &VIE, Args, Call: CallRef(), Body: FD->getBody(), Info, Result&: Scratch,
23067 /*ResultSlot=*/nullptr);
23068 }
23069
23070 return Diags.empty();
23071}
23072
23073bool Expr::isPotentialConstantExprUnevaluated(Expr *E,
23074 const FunctionDecl *FD,
23075 SmallVectorImpl<
23076 PartialDiagnosticAt> &Diags) {
23077 assert(!E->isValueDependent() &&
23078 "Expression evaluator can't be called on a dependent expression.");
23079
23080 Expr::EvalStatus Status;
23081 Status.Diag = &Diags;
23082
23083 EvalInfo Info(FD->getASTContext(), Status,
23084 EvaluationMode::ConstantExpressionUnevaluated);
23085 Info.InConstantContext = true;
23086 Info.CheckingPotentialConstantExpression = true;
23087
23088 if (Info.EnableNewConstInterp) {
23089 Info.Ctx.getInterpContext().isPotentialConstantExprUnevaluated(Parent&: Info, E, FD);
23090 return Diags.empty();
23091 }
23092
23093 // Fabricate a call stack frame to give the arguments a plausible cover story.
23094 CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr,
23095 /*CallExpr=*/nullptr, CallRef());
23096
23097 APValue ResultScratch;
23098 Evaluate(Result&: ResultScratch, Info, E);
23099 return Diags.empty();
23100}
23101
23102std::optional<uint64_t> Expr::tryEvaluateObjectSize(const ASTContext &Ctx,
23103 unsigned Type) const {
23104 if (!getType()->isPointerType())
23105 return std::nullopt;
23106
23107 Expr::EvalStatus Status;
23108 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23109 if (Info.EnableNewConstInterp)
23110 return Info.Ctx.getInterpContext().tryEvaluateObjectSize(Parent&: Info, E: this, Kind: Type);
23111 return tryEvaluateBuiltinObjectSize(E: this, Type, Info);
23112}
23113
23114static std::optional<uint64_t>
23115EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
23116 std::string *StringResult) {
23117 if (!E->getType()->hasPointerRepresentation() || !E->isPRValue())
23118 return std::nullopt;
23119
23120 LValue String;
23121
23122 if (!EvaluatePointer(E, Result&: String, Info))
23123 return std::nullopt;
23124
23125 // Fast path: if it's a string literal, search the string value.
23126 if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>(
23127 Val: String.getLValueBase().dyn_cast<const Expr *>())) {
23128 StringRef Str = S->getBytes();
23129 int64_t Off = String.Offset.getQuantity();
23130 if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size()) {
23131 UnsignedOrNone ZeroIndex = S->findZeroCodeUnit(StartIndex: Off);
23132 if (StringResult) {
23133 if (ZeroIndex)
23134 Str = Str.substr(Start: Off, N: *ZeroIndex);
23135 *StringResult = Str;
23136 }
23137
23138 return ZeroIndex.value_or(Def: Str.size());
23139 }
23140 // For an invalid index, fall through to the offset handling below.
23141 }
23142
23143 QualType CharTy = E->getType()->getPointeeType();
23144 // Slow path: scan the bytes of the string looking for the terminating 0.
23145 for (uint64_t Strlen = 0; /**/; ++Strlen) {
23146 APValue Char;
23147 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) ||
23148 !Char.isInt())
23149 return std::nullopt;
23150 if (!Char.getInt())
23151 return Strlen;
23152 else if (StringResult)
23153 StringResult->push_back(c: Char.getInt().getExtValue());
23154 if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1))
23155 return std::nullopt;
23156 }
23157}
23158
23159std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {
23160 Expr::EvalStatus Status;
23161 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23162 std::string StringResult;
23163
23164 if (Info.EnableNewConstInterp) {
23165 if (!Info.Ctx.getInterpContext().evaluateString(Parent&: Info, E: this, Result&: StringResult))
23166 return std::nullopt;
23167 return StringResult;
23168 }
23169
23170 if (EvaluateBuiltinStrLen(E: this, Info, StringResult: &StringResult))
23171 return StringResult;
23172 return std::nullopt;
23173}
23174
23175template <typename T>
23176static bool EvaluateCharRangeAsStringImpl(const Expr *, T &Result,
23177 const Expr *SizeExpression,
23178 const Expr *PtrExpression,
23179 ASTContext &Ctx,
23180 Expr::EvalResult &Status) {
23181 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
23182 Info.InConstantContext = true;
23183
23184 if (Info.EnableNewConstInterp)
23185 return Info.Ctx.getInterpContext().evaluateCharRange(Info, SizeExpression,
23186 PtrExpression, Result);
23187
23188 LValue String;
23189 FullExpressionRAII Scope(Info);
23190 APSInt SizeValue;
23191 if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info))
23192 return false;
23193
23194 uint64_t Size = SizeValue.getZExtValue();
23195
23196 // FIXME: better protect against invalid or excessive sizes
23197 if constexpr (std::is_same_v<APValue, T>)
23198 Result = APValue(APValue::UninitArray{}, Size, Size);
23199 else {
23200 if (Size < Result.max_size())
23201 Result.reserve(Size);
23202 }
23203 if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info))
23204 return false;
23205
23206 QualType CharTy = PtrExpression->getType()->getPointeeType();
23207 for (uint64_t I = 0; I < Size; ++I) {
23208 APValue Char;
23209 if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String,
23210 RVal&: Char))
23211 return false;
23212
23213 if constexpr (std::is_same_v<APValue, T>) {
23214 Result.getArrayInitializedElt(I) = std::move(Char);
23215 } else {
23216 APSInt C = Char.getInt();
23217
23218 assert(C.getBitWidth() <= 8 &&
23219 "string element not representable in char");
23220
23221 Result.push_back(static_cast<char>(C.getExtValue()));
23222 }
23223
23224 if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1))
23225 return false;
23226 }
23227
23228 return Scope.destroy() && CheckMemoryLeaks(Info);
23229}
23230
23231bool Expr::EvaluateCharRangeAsString(std::string &Result,
23232 const Expr *SizeExpression,
23233 const Expr *PtrExpression, ASTContext &Ctx,
23234 EvalResult &Status) const {
23235 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23236 PtrExpression, Ctx, Status);
23237}
23238
23239bool Expr::EvaluateCharRangeAsString(APValue &Result,
23240 const Expr *SizeExpression,
23241 const Expr *PtrExpression, ASTContext &Ctx,
23242 EvalResult &Status) const {
23243 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
23244 PtrExpression, Ctx, Status);
23245}
23246
23247std::optional<uint64_t> Expr::tryEvaluateStrLen(const ASTContext &Ctx) const {
23248 Expr::EvalStatus Status;
23249 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
23250
23251 if (Info.EnableNewConstInterp)
23252 return Info.Ctx.getInterpContext().evaluateStrlen(Parent&: Info, E: this);
23253 return EvaluateBuiltinStrLen(E: this, Info);
23254}
23255
23256namespace {
23257struct IsWithinLifetimeHandler {
23258 EvalInfo &Info;
23259 static constexpr AccessKinds AccessKind = AccessKinds::AK_IsWithinLifetime;
23260 using result_type = std::optional<bool>;
23261 std::optional<bool> failed() { return std::nullopt; }
23262 template <typename T>
23263 std::optional<bool> found(T &Subobj, QualType SubobjType,
23264 APValue::LValueBase) {
23265 return true;
23266 }
23267 template <typename T>
23268 std::optional<bool> found(T &Subobj, QualType SubobjType) {
23269 return true;
23270 }
23271};
23272
23273std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &IEE,
23274 const CallExpr *E) {
23275 EvalInfo &Info = IEE.Info;
23276 // Sometimes this is called during some sorts of constant folding / early
23277 // evaluation. These are meant for non-constant expressions and are not
23278 // necessary since this consteval builtin will never be evaluated at runtime.
23279 // Just fail to evaluate when not in a constant context.
23280 if (!Info.InConstantContext)
23281 return std::nullopt;
23282 assert(E->getBuiltinCallee() == Builtin::BI__builtin_is_within_lifetime);
23283 const Expr *Arg = E->getArg(Arg: 0);
23284 if (Arg->isValueDependent())
23285 return std::nullopt;
23286 LValue Val;
23287 if (!EvaluatePointer(E: Arg, Result&: Val, Info))
23288 return std::nullopt;
23289
23290 if (Val.allowConstexprUnknown())
23291 return true;
23292
23293 auto Error = [&](int Diag) {
23294 bool CalledFromStd = false;
23295 const auto *Callee = Info.CurrentCall->getCallee();
23296 if (Callee && Callee->isInStdNamespace()) {
23297 const IdentifierInfo *Identifier = Callee->getIdentifier();
23298 CalledFromStd = Identifier && Identifier->isStr(Str: "is_within_lifetime");
23299 }
23300 Info.CCEDiag(Loc: CalledFromStd ? Info.CurrentCall->getCallRange().getBegin()
23301 : E->getExprLoc(),
23302 DiagId: diag::err_invalid_is_within_lifetime)
23303 << (CalledFromStd ? "std::is_within_lifetime"
23304 : "__builtin_is_within_lifetime")
23305 << Diag;
23306 return std::nullopt;
23307 };
23308 // C++2c [meta.const.eval]p4:
23309 // During the evaluation of an expression E as a core constant expression, a
23310 // call to this function is ill-formed unless p points to an object that is
23311 // usable in constant expressions or whose complete object's lifetime began
23312 // within E.
23313
23314 // Make sure it points to an object
23315 // nullptr does not point to an object
23316 if (Val.isNullPointer() || Val.getLValueBase().isNull())
23317 return Error(0);
23318 QualType T = Val.getLValueBase().getType();
23319 assert(!T->isFunctionType() &&
23320 "Pointers to functions should have been typed as function pointers "
23321 "which would have been rejected earlier");
23322 assert(T->isObjectType());
23323 // Hypothetical array element is not an object
23324 if (Val.getLValueDesignator().isOnePastTheEnd())
23325 return Error(1);
23326 assert(Val.getLValueDesignator().isValidSubobject() &&
23327 "Unchecked case for valid subobject");
23328 // All other ill-formed values should have failed EvaluatePointer, so the
23329 // object should be a pointer to an object that is usable in a constant
23330 // expression or whose complete lifetime began within the expression
23331 CompleteObject CO =
23332 findCompleteObject(Info, E, AK: AccessKinds::AK_IsWithinLifetime, LVal: Val, LValType: T);
23333 // The lifetime hasn't begun yet if we are still evaluating the
23334 // initializer ([basic.life]p(1.2))
23335 if (Info.EvaluatingDeclValue && CO.Value == Info.EvaluatingDeclValue)
23336 return Error(2);
23337
23338 if (!CO)
23339 return false;
23340 IsWithinLifetimeHandler handler{.Info: Info};
23341 return findSubobject(Info, E, Obj: CO, Sub: Val.getLValueDesignator(), handler);
23342}
23343} // namespace
23344