1//===--- ExprConstant.cpp - Expression Constant Evaluator -----------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements the Expr constant evaluator.
10//
11// Constant expression evaluation produces four main results:
12//
13// * A success/failure flag indicating whether constant folding was successful.
14// This is the 'bool' return value used by most of the code in this file. A
15// 'false' return value indicates that constant folding has failed, and any
16// appropriate diagnostic has already been produced.
17//
18// * An evaluated result, valid only if constant folding has not failed.
19//
20// * A flag indicating if evaluation encountered (unevaluated) side-effects.
21// These arise in cases such as (sideEffect(), 0) and (sideEffect() || 1),
22// where it is possible to determine the evaluated result regardless.
23//
24// * A set of notes indicating why the evaluation was not a constant expression
25// (under the C++11 / C++1y rules only, at the moment), or, if folding failed
26// too, why the expression could not be folded.
27//
28// If we are checking for a potential constant expression, failure to constant
29// fold a potential constant sub-expression will be indicated by a 'false'
30// return value (the expression could not be folded) and no diagnostic (the
31// expression is not necessarily non-constant).
32//
33//===----------------------------------------------------------------------===//
34
35#include "ByteCode/Context.h"
36#include "ByteCode/Frame.h"
37#include "ByteCode/State.h"
38#include "ExprConstShared.h"
39#include "clang/AST/APValue.h"
40#include "clang/AST/ASTContext.h"
41#include "clang/AST/ASTLambda.h"
42#include "clang/AST/Attr.h"
43#include "clang/AST/CXXInheritance.h"
44#include "clang/AST/CharUnits.h"
45#include "clang/AST/CurrentSourceLocExprScope.h"
46#include "clang/AST/Expr.h"
47#include "clang/AST/InferAlloc.h"
48#include "clang/AST/OSLog.h"
49#include "clang/AST/OptionalDiagnostic.h"
50#include "clang/AST/RecordLayout.h"
51#include "clang/AST/StmtVisitor.h"
52#include "clang/AST/Type.h"
53#include "clang/AST/TypeLoc.h"
54#include "clang/Basic/Builtins.h"
55#include "clang/Basic/DiagnosticSema.h"
56#include "clang/Basic/TargetBuiltins.h"
57#include "clang/Basic/TargetInfo.h"
58#include "llvm/ADT/APFixedPoint.h"
59#include "llvm/ADT/Sequence.h"
60#include "llvm/ADT/SmallBitVector.h"
61#include "llvm/ADT/StringExtras.h"
62#include "llvm/Support/Casting.h"
63#include "llvm/Support/Debug.h"
64#include "llvm/Support/SaveAndRestore.h"
65#include "llvm/Support/SipHash.h"
66#include "llvm/Support/TimeProfiler.h"
67#include "llvm/Support/raw_ostream.h"
68#include <cstring>
69#include <functional>
70#include <limits>
71#include <optional>
72
73#define DEBUG_TYPE "exprconstant"
74
75using namespace clang;
76using llvm::APFixedPoint;
77using llvm::APInt;
78using llvm::APSInt;
79using llvm::APFloat;
80using llvm::FixedPointSemantics;
81
82namespace {
83 struct LValue;
84 class CallStackFrame;
85 class EvalInfo;
86
87 using SourceLocExprScopeGuard =
88 CurrentSourceLocExprScope::SourceLocExprScopeGuard;
89
90 static QualType getType(APValue::LValueBase B) {
91 return B.getType();
92 }
93
94 /// Get an LValue path entry, which is known to not be an array index, as a
95 /// field declaration.
96 static const FieldDecl *getAsField(APValue::LValuePathEntry E) {
97 return dyn_cast_or_null<FieldDecl>(Val: E.getAsBaseOrMember().getPointer());
98 }
99 /// Get an LValue path entry, which is known to not be an array index, as a
100 /// base class declaration.
101 static const CXXRecordDecl *getAsBaseClass(APValue::LValuePathEntry E) {
102 return dyn_cast_or_null<CXXRecordDecl>(Val: E.getAsBaseOrMember().getPointer());
103 }
104 /// Determine whether this LValue path entry for a base class names a virtual
105 /// base class.
106 static bool isVirtualBaseClass(APValue::LValuePathEntry E) {
107 return E.getAsBaseOrMember().getInt();
108 }
109
110 /// Given an expression, determine the type used to store the result of
111 /// evaluating that expression.
112 static QualType getStorageType(const ASTContext &Ctx, const Expr *E) {
113 if (E->isPRValue())
114 return E->getType();
115 return Ctx.getLValueReferenceType(T: E->getType());
116 }
117
118 /// Attempts to unwrap a CallExpr (with an alloc_size attribute) from an Expr.
119 /// This will look through a single cast.
120 ///
121 /// Returns null if we couldn't unwrap a function with alloc_size.
122 static const CallExpr *tryUnwrapAllocSizeCall(const Expr *E) {
123 if (!E->getType()->isPointerType())
124 return nullptr;
125
126 E = E->IgnoreParens();
127 // If we're doing a variable assignment from e.g. malloc(N), there will
128 // probably be a cast of some kind. In exotic cases, we might also see a
129 // top-level ExprWithCleanups. Ignore them either way.
130 if (const auto *FE = dyn_cast<FullExpr>(Val: E))
131 E = FE->getSubExpr()->IgnoreParens();
132
133 if (const auto *Cast = dyn_cast<CastExpr>(Val: E))
134 E = Cast->getSubExpr()->IgnoreParens();
135
136 if (const auto *CE = dyn_cast<CallExpr>(Val: E))
137 return CE->getCalleeAllocSizeAttr() ? CE : nullptr;
138 return nullptr;
139 }
140
141 /// Determines whether or not the given Base contains a call to a function
142 /// with the alloc_size attribute.
143 static bool isBaseAnAllocSizeCall(APValue::LValueBase Base) {
144 const auto *E = Base.dyn_cast<const Expr *>();
145 return E && E->getType()->isPointerType() && tryUnwrapAllocSizeCall(E);
146 }
147
148 /// Determines whether the given kind of constant expression is only ever
149 /// used for name mangling. If so, it's permitted to reference things that we
150 /// can't generate code for (in particular, dllimported functions).
151 static bool isForManglingOnly(ConstantExprKind Kind) {
152 switch (Kind) {
153 case ConstantExprKind::Normal:
154 case ConstantExprKind::ClassTemplateArgument:
155 case ConstantExprKind::ImmediateInvocation:
156 // Note that non-type template arguments of class type are emitted as
157 // template parameter objects.
158 return false;
159
160 case ConstantExprKind::NonClassTemplateArgument:
161 return true;
162 }
163 llvm_unreachable("unknown ConstantExprKind");
164 }
165
166 static bool isTemplateArgument(ConstantExprKind Kind) {
167 switch (Kind) {
168 case ConstantExprKind::Normal:
169 case ConstantExprKind::ImmediateInvocation:
170 return false;
171
172 case ConstantExprKind::ClassTemplateArgument:
173 case ConstantExprKind::NonClassTemplateArgument:
174 return true;
175 }
176 llvm_unreachable("unknown ConstantExprKind");
177 }
178
179 /// The bound to claim that an array of unknown bound has.
180 /// The value in MostDerivedArraySize is undefined in this case. So, set it
181 /// to an arbitrary value that's likely to loudly break things if it's used.
182 static const uint64_t AssumedSizeForUnsizedArray =
183 std::numeric_limits<uint64_t>::max() / 2;
184
185 /// Determines if an LValue with the given LValueBase will have an unsized
186 /// array in its designator.
187 /// Find the path length and type of the most-derived subobject in the given
188 /// path, and find the size of the containing array, if any.
189 static unsigned
190 findMostDerivedSubobject(const ASTContext &Ctx, APValue::LValueBase Base,
191 ArrayRef<APValue::LValuePathEntry> Path,
192 uint64_t &ArraySize, QualType &Type, bool &IsArray,
193 bool &FirstEntryIsUnsizedArray) {
194 // This only accepts LValueBases from APValues, and APValues don't support
195 // arrays that lack size info.
196 assert(!isBaseAnAllocSizeCall(Base) &&
197 "Unsized arrays shouldn't appear here");
198 unsigned MostDerivedLength = 0;
199 // The type of Base is a reference type if the base is a constexpr-unknown
200 // variable. In that case, look through the reference type.
201 Type = getType(B: Base).getNonReferenceType();
202
203 for (unsigned I = 0, N = Path.size(); I != N; ++I) {
204 if (Type->isArrayType()) {
205 const ArrayType *AT = Ctx.getAsArrayType(T: Type);
206 Type = AT->getElementType();
207 MostDerivedLength = I + 1;
208 IsArray = true;
209
210 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT)) {
211 ArraySize = CAT->getZExtSize();
212 } else {
213 assert(I == 0 && "unexpected unsized array designator");
214 FirstEntryIsUnsizedArray = true;
215 ArraySize = AssumedSizeForUnsizedArray;
216 }
217 } else if (Type->isAnyComplexType()) {
218 const ComplexType *CT = Type->castAs<ComplexType>();
219 Type = CT->getElementType();
220 ArraySize = 2;
221 MostDerivedLength = I + 1;
222 IsArray = true;
223 } else if (const auto *VT = Type->getAs<VectorType>()) {
224 Type = VT->getElementType();
225 ArraySize = VT->getNumElements();
226 MostDerivedLength = I + 1;
227 IsArray = true;
228 } else if (const FieldDecl *FD = getAsField(E: Path[I])) {
229 Type = FD->getType();
230 ArraySize = 0;
231 MostDerivedLength = I + 1;
232 IsArray = false;
233 } else {
234 // Path[I] describes a base class.
235 ArraySize = 0;
236 IsArray = false;
237 }
238 }
239 return MostDerivedLength;
240 }
241
242 /// A path from a glvalue to a subobject of that glvalue.
243 struct SubobjectDesignator {
244 /// True if the subobject was named in a manner not supported by C++11. Such
245 /// lvalues can still be folded, but they are not core constant expressions
246 /// and we cannot perform lvalue-to-rvalue conversions on them.
247 LLVM_PREFERRED_TYPE(bool)
248 unsigned Invalid : 1;
249
250 /// Is this a pointer one past the end of an object?
251 LLVM_PREFERRED_TYPE(bool)
252 unsigned IsOnePastTheEnd : 1;
253
254 /// Indicator of whether the first entry is an unsized array.
255 LLVM_PREFERRED_TYPE(bool)
256 unsigned FirstEntryIsAnUnsizedArray : 1;
257
258 /// Indicator of whether the most-derived object is an array element.
259 LLVM_PREFERRED_TYPE(bool)
260 unsigned MostDerivedIsArrayElement : 1;
261
262 /// The length of the path to the most-derived object of which this is a
263 /// subobject.
264 unsigned MostDerivedPathLength : 28;
265
266 /// The size of the array of which the most-derived object is an element.
267 /// This will always be 0 if the most-derived object is not an array
268 /// element. 0 is not an indicator of whether or not the most-derived object
269 /// is an array, however, because 0-length arrays are allowed.
270 ///
271 /// If the current array is an unsized array, the value of this is
272 /// undefined.
273 uint64_t MostDerivedArraySize;
274 /// The type of the most derived object referred to by this address.
275 QualType MostDerivedType;
276
277 typedef APValue::LValuePathEntry PathEntry;
278
279 /// The entries on the path from the glvalue to the designated subobject.
280 SmallVector<PathEntry, 8> Entries;
281
282 SubobjectDesignator() : Invalid(true) {}
283
284 explicit SubobjectDesignator(QualType T)
285 : Invalid(false), IsOnePastTheEnd(false),
286 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
287 MostDerivedPathLength(0), MostDerivedArraySize(0),
288 MostDerivedType(T.isNull() ? QualType() : T.getNonReferenceType()) {}
289
290 SubobjectDesignator(const ASTContext &Ctx, const APValue &V)
291 : Invalid(!V.isLValue() || !V.hasLValuePath()), IsOnePastTheEnd(false),
292 FirstEntryIsAnUnsizedArray(false), MostDerivedIsArrayElement(false),
293 MostDerivedPathLength(0), MostDerivedArraySize(0) {
294 assert(V.isLValue() && "Non-LValue used to make an LValue designator?");
295 if (!Invalid) {
296 IsOnePastTheEnd = V.isLValueOnePastTheEnd();
297 llvm::append_range(C&: Entries, R: V.getLValuePath());
298 if (V.getLValueBase()) {
299 bool IsArray = false;
300 bool FirstIsUnsizedArray = false;
301 MostDerivedPathLength = findMostDerivedSubobject(
302 Ctx, Base: V.getLValueBase(), Path: V.getLValuePath(), ArraySize&: MostDerivedArraySize,
303 Type&: MostDerivedType, IsArray, FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
304 MostDerivedIsArrayElement = IsArray;
305 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
306 }
307 }
308 }
309
310 void truncate(ASTContext &Ctx, APValue::LValueBase Base,
311 unsigned NewLength) {
312 if (Invalid)
313 return;
314
315 assert(Base && "cannot truncate path for null pointer");
316 assert(NewLength <= Entries.size() && "not a truncation");
317
318 if (NewLength == Entries.size())
319 return;
320 Entries.resize(N: NewLength);
321
322 bool IsArray = false;
323 bool FirstIsUnsizedArray = false;
324 MostDerivedPathLength = findMostDerivedSubobject(
325 Ctx, Base, Path: Entries, ArraySize&: MostDerivedArraySize, Type&: MostDerivedType, IsArray,
326 FirstEntryIsUnsizedArray&: FirstIsUnsizedArray);
327 MostDerivedIsArrayElement = IsArray;
328 FirstEntryIsAnUnsizedArray = FirstIsUnsizedArray;
329 }
330
331 void setInvalid() {
332 Invalid = true;
333 Entries.clear();
334 }
335
336 /// Determine whether the most derived subobject is an array without a
337 /// known bound.
338 bool isMostDerivedAnUnsizedArray() const {
339 assert(!Invalid && "Calling this makes no sense on invalid designators");
340 return Entries.size() == 1 && FirstEntryIsAnUnsizedArray;
341 }
342
343 /// Determine what the most derived array's size is. Results in an assertion
344 /// failure if the most derived array lacks a size.
345 uint64_t getMostDerivedArraySize() const {
346 assert(!isMostDerivedAnUnsizedArray() && "Unsized array has no size");
347 return MostDerivedArraySize;
348 }
349
350 /// Determine whether this is a one-past-the-end pointer.
351 bool isOnePastTheEnd() const {
352 assert(!Invalid);
353 if (IsOnePastTheEnd)
354 return true;
355 if (!isMostDerivedAnUnsizedArray() && MostDerivedIsArrayElement &&
356 Entries[MostDerivedPathLength - 1].getAsArrayIndex() ==
357 MostDerivedArraySize)
358 return true;
359 return false;
360 }
361
362 /// Get the range of valid index adjustments in the form
363 /// {maximum value that can be subtracted from this pointer,
364 /// maximum value that can be added to this pointer}
365 std::pair<uint64_t, uint64_t> validIndexAdjustments() {
366 if (Invalid || isMostDerivedAnUnsizedArray())
367 return {0, 0};
368
369 // [expr.add]p4: For the purposes of these operators, a pointer to a
370 // nonarray object behaves the same as a pointer to the first element of
371 // an array of length one with the type of the object as its element type.
372 bool IsArray = MostDerivedPathLength == Entries.size() &&
373 MostDerivedIsArrayElement;
374 uint64_t ArrayIndex = IsArray ? Entries.back().getAsArrayIndex()
375 : (uint64_t)IsOnePastTheEnd;
376 uint64_t ArraySize =
377 IsArray ? getMostDerivedArraySize() : (uint64_t)1;
378 return {ArrayIndex, ArraySize - ArrayIndex};
379 }
380
381 /// Check that this refers to a valid subobject.
382 bool isValidSubobject() const {
383 if (Invalid)
384 return false;
385 return !isOnePastTheEnd();
386 }
387 /// Check that this refers to a valid subobject, and if not, produce a
388 /// relevant diagnostic and set the designator as invalid.
389 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK);
390
391 /// Get the type of the designated object.
392 QualType getType(ASTContext &Ctx) const {
393 assert(!Invalid && "invalid designator has no subobject type");
394 return MostDerivedPathLength == Entries.size()
395 ? MostDerivedType
396 : Ctx.getCanonicalTagType(TD: getAsBaseClass(E: Entries.back()));
397 }
398
399 /// Update this designator to refer to the first element within this array.
400 void addArrayUnchecked(const ConstantArrayType *CAT) {
401 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
402
403 // This is a most-derived object.
404 MostDerivedType = CAT->getElementType();
405 MostDerivedIsArrayElement = true;
406 MostDerivedArraySize = CAT->getZExtSize();
407 MostDerivedPathLength = Entries.size();
408 }
409 /// Update this designator to refer to the first element within the array of
410 /// elements of type T. This is an array of unknown size.
411 void addUnsizedArrayUnchecked(QualType ElemTy) {
412 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: 0));
413
414 MostDerivedType = ElemTy;
415 MostDerivedIsArrayElement = true;
416 // The value in MostDerivedArraySize is undefined in this case. So, set it
417 // to an arbitrary value that's likely to loudly break things if it's
418 // used.
419 MostDerivedArraySize = AssumedSizeForUnsizedArray;
420 MostDerivedPathLength = Entries.size();
421 }
422 /// Update this designator to refer to the given base or member of this
423 /// object.
424 void addDeclUnchecked(const Decl *D, bool Virtual = false) {
425 Entries.push_back(Elt: APValue::BaseOrMemberType(D, Virtual));
426
427 // If this isn't a base class, it's a new most-derived object.
428 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: D)) {
429 MostDerivedType = FD->getType();
430 MostDerivedIsArrayElement = false;
431 MostDerivedArraySize = 0;
432 MostDerivedPathLength = Entries.size();
433 }
434 }
435 /// Update this designator to refer to the given complex component.
436 void addComplexUnchecked(QualType EltTy, bool Imag) {
437 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Imag));
438
439 // This is technically a most-derived object, though in practice this
440 // is unlikely to matter.
441 MostDerivedType = EltTy;
442 MostDerivedIsArrayElement = true;
443 MostDerivedArraySize = 2;
444 MostDerivedPathLength = Entries.size();
445 }
446
447 void addVectorElementUnchecked(QualType EltTy, uint64_t Size,
448 uint64_t Idx) {
449 Entries.push_back(Elt: PathEntry::ArrayIndex(Index: Idx));
450 MostDerivedType = EltTy;
451 MostDerivedPathLength = Entries.size();
452 MostDerivedArraySize = 0;
453 MostDerivedIsArrayElement = false;
454 }
455
456 void diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info, const Expr *E);
457 void diagnosePointerArithmetic(EvalInfo &Info, const Expr *E,
458 const APSInt &N);
459 /// Add N to the address of this subobject.
460 void adjustIndex(EvalInfo &Info, const Expr *E, APSInt N, const LValue &LV);
461 };
462
463 /// A scope at the end of which an object can need to be destroyed.
464 enum class ScopeKind {
465 Block,
466 FullExpression,
467 Call
468 };
469
470 /// A reference to a particular call and its arguments.
471 struct CallRef {
472 CallRef() : OrigCallee(), CallIndex(0), Version() {}
473 CallRef(const FunctionDecl *Callee, unsigned CallIndex, unsigned Version)
474 : OrigCallee(Callee), CallIndex(CallIndex), Version(Version) {}
475
476 explicit operator bool() const { return OrigCallee; }
477
478 /// Get the parameter that the caller initialized, corresponding to the
479 /// given parameter in the callee.
480 const ParmVarDecl *getOrigParam(const ParmVarDecl *PVD) const {
481 return OrigCallee ? OrigCallee->getParamDecl(i: PVD->getFunctionScopeIndex())
482 : PVD;
483 }
484
485 /// The callee at the point where the arguments were evaluated. This might
486 /// be different from the actual callee (a different redeclaration, or a
487 /// virtual override), but this function's parameters are the ones that
488 /// appear in the parameter map.
489 const FunctionDecl *OrigCallee;
490 /// The call index of the frame that holds the argument values.
491 unsigned CallIndex;
492 /// The version of the parameters corresponding to this call.
493 unsigned Version;
494 };
495
496 /// A stack frame in the constexpr call stack.
497 class CallStackFrame : public interp::Frame {
498 public:
499 EvalInfo &Info;
500
501 /// Parent - The caller of this stack frame.
502 CallStackFrame *Caller;
503
504 /// Callee - The function which was called.
505 const FunctionDecl *Callee;
506
507 /// This - The binding for the this pointer in this call, if any.
508 const LValue *This;
509
510 /// CallExpr - The syntactical structure of member function calls
511 const Expr *CallExpr;
512
513 /// Information on how to find the arguments to this call. Our arguments
514 /// are stored in our parent's CallStackFrame, using the ParmVarDecl* as a
515 /// key and this value as the version.
516 CallRef Arguments;
517
518 /// Source location information about the default argument or default
519 /// initializer expression we're evaluating, if any.
520 CurrentSourceLocExprScope CurSourceLocExprScope;
521
522 // Note that we intentionally use std::map here so that references to
523 // values are stable.
524 typedef std::pair<const void *, unsigned> MapKeyTy;
525 typedef std::map<MapKeyTy, APValue> MapTy;
526 /// Temporaries - Temporary lvalues materialized within this stack frame.
527 MapTy Temporaries;
528
529 /// CallRange - The source range of the call expression for this call.
530 SourceRange CallRange;
531
532 /// Index - The call index of this call.
533 unsigned Index;
534
535 /// The stack of integers for tracking version numbers for temporaries.
536 SmallVector<unsigned, 2> TempVersionStack = {1};
537 unsigned CurTempVersion = TempVersionStack.back();
538
539 unsigned getTempVersion() const { return TempVersionStack.back(); }
540
541 void pushTempVersion() {
542 TempVersionStack.push_back(Elt: ++CurTempVersion);
543 }
544
545 void popTempVersion() {
546 TempVersionStack.pop_back();
547 }
548
549 CallRef createCall(const FunctionDecl *Callee) {
550 return {Callee, Index, ++CurTempVersion};
551 }
552
553 // FIXME: Adding this to every 'CallStackFrame' may have a nontrivial impact
554 // on the overall stack usage of deeply-recursing constexpr evaluations.
555 // (We should cache this map rather than recomputing it repeatedly.)
556 // But let's try this and see how it goes; we can look into caching the map
557 // as a later change.
558
559 /// LambdaCaptureFields - Mapping from captured variables/this to
560 /// corresponding data members in the closure class.
561 llvm::DenseMap<const ValueDecl *, FieldDecl *> LambdaCaptureFields;
562 FieldDecl *LambdaThisCaptureField = nullptr;
563
564 CallStackFrame(EvalInfo &Info, SourceRange CallRange,
565 const FunctionDecl *Callee, const LValue *This,
566 const Expr *CallExpr, CallRef Arguments);
567 ~CallStackFrame();
568
569 // Return the temporary for Key whose version number is Version.
570 APValue *getTemporary(const void *Key, unsigned Version) {
571 MapKeyTy KV(Key, Version);
572 auto LB = Temporaries.lower_bound(x: KV);
573 if (LB != Temporaries.end() && LB->first == KV)
574 return &LB->second;
575 return nullptr;
576 }
577
578 // Return the current temporary for Key in the map.
579 APValue *getCurrentTemporary(const void *Key) {
580 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
581 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
582 return &std::prev(x: UB)->second;
583 return nullptr;
584 }
585
586 // Return the version number of the current temporary for Key.
587 unsigned getCurrentTemporaryVersion(const void *Key) const {
588 auto UB = Temporaries.upper_bound(x: MapKeyTy(Key, UINT_MAX));
589 if (UB != Temporaries.begin() && std::prev(x: UB)->first.first == Key)
590 return std::prev(x: UB)->first.second;
591 return 0;
592 }
593
594 /// Allocate storage for an object of type T in this stack frame.
595 /// Populates LV with a handle to the created object. Key identifies
596 /// the temporary within the stack frame, and must not be reused without
597 /// bumping the temporary version number.
598 template<typename KeyT>
599 APValue &createTemporary(const KeyT *Key, QualType T,
600 ScopeKind Scope, LValue &LV);
601
602 /// Allocate storage for a parameter of a function call made in this frame.
603 APValue &createParam(CallRef Args, const ParmVarDecl *PVD, LValue &LV);
604
605 void describe(llvm::raw_ostream &OS) const override;
606
607 Frame *getCaller() const override { return Caller; }
608 SourceRange getCallRange() const override { return CallRange; }
609 const FunctionDecl *getCallee() const override { return Callee; }
610
611 bool isStdFunction() const {
612 for (const DeclContext *DC = Callee; DC; DC = DC->getParent())
613 if (DC->isStdNamespace())
614 return true;
615 return false;
616 }
617
618 /// Whether we're in a context where [[msvc::constexpr]] evaluation is
619 /// permitted. See MSConstexprDocs for description of permitted contexts.
620 bool CanEvalMSConstexpr = false;
621
622 private:
623 APValue &createLocal(APValue::LValueBase Base, const void *Key, QualType T,
624 ScopeKind Scope);
625 };
626
627 /// Temporarily override 'this'.
628 class ThisOverrideRAII {
629 public:
630 ThisOverrideRAII(CallStackFrame &Frame, const LValue *NewThis, bool Enable)
631 : Frame(Frame), OldThis(Frame.This) {
632 if (Enable)
633 Frame.This = NewThis;
634 }
635 ~ThisOverrideRAII() {
636 Frame.This = OldThis;
637 }
638 private:
639 CallStackFrame &Frame;
640 const LValue *OldThis;
641 };
642
643 // A shorthand time trace scope struct, prints source range, for example
644 // {"name":"EvaluateAsRValue","args":{"detail":"<test.cc:8:21, col:25>"}}}
645 class ExprTimeTraceScope {
646 public:
647 ExprTimeTraceScope(const Expr *E, const ASTContext &Ctx, StringRef Name)
648 : TimeScope(Name, [E, &Ctx] {
649 return E->getSourceRange().printToString(SM: Ctx.getSourceManager());
650 }) {}
651
652 private:
653 llvm::TimeTraceScope TimeScope;
654 };
655
656 /// RAII object used to change the current ability of
657 /// [[msvc::constexpr]] evaulation.
658 struct MSConstexprContextRAII {
659 CallStackFrame &Frame;
660 bool OldValue;
661 explicit MSConstexprContextRAII(CallStackFrame &Frame, bool Value)
662 : Frame(Frame), OldValue(Frame.CanEvalMSConstexpr) {
663 Frame.CanEvalMSConstexpr = Value;
664 }
665
666 ~MSConstexprContextRAII() { Frame.CanEvalMSConstexpr = OldValue; }
667 };
668}
669
670static bool HandleDestruction(EvalInfo &Info, const Expr *E,
671 const LValue &This, QualType ThisType);
672static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
673 APValue::LValueBase LVBase, APValue &Value,
674 QualType T);
675
676namespace {
677 /// A cleanup, and a flag indicating whether it is lifetime-extended.
678 class Cleanup {
679 llvm::PointerIntPair<APValue*, 2, ScopeKind> Value;
680 APValue::LValueBase Base;
681 QualType T;
682
683 public:
684 Cleanup(APValue *Val, APValue::LValueBase Base, QualType T,
685 ScopeKind Scope)
686 : Value(Val, Scope), Base(Base), T(T) {}
687
688 /// Determine whether this cleanup should be performed at the end of the
689 /// given kind of scope.
690 bool isDestroyedAtEndOf(ScopeKind K) const {
691 return (int)Value.getInt() >= (int)K;
692 }
693 bool endLifetime(EvalInfo &Info, bool RunDestructors) {
694 if (RunDestructors) {
695 SourceLocation Loc;
696 if (const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>())
697 Loc = VD->getLocation();
698 else if (const Expr *E = Base.dyn_cast<const Expr*>())
699 Loc = E->getExprLoc();
700 return HandleDestruction(Info, Loc, LVBase: Base, Value&: *Value.getPointer(), T);
701 }
702 *Value.getPointer() = APValue();
703 return true;
704 }
705
706 bool hasSideEffect() {
707 return T.isDestructedType();
708 }
709 };
710
711 /// A reference to an object whose construction we are currently evaluating.
712 struct ObjectUnderConstruction {
713 APValue::LValueBase Base;
714 ArrayRef<APValue::LValuePathEntry> Path;
715 friend bool operator==(const ObjectUnderConstruction &LHS,
716 const ObjectUnderConstruction &RHS) {
717 return LHS.Base == RHS.Base && LHS.Path == RHS.Path;
718 }
719 friend llvm::hash_code hash_value(const ObjectUnderConstruction &Obj) {
720 return llvm::hash_combine(args: Obj.Base, args: Obj.Path);
721 }
722 };
723 enum class ConstructionPhase {
724 None,
725 Bases,
726 AfterBases,
727 AfterFields,
728 Destroying,
729 DestroyingBases
730 };
731}
732
733namespace llvm {
734template<> struct DenseMapInfo<ObjectUnderConstruction> {
735 using Base = DenseMapInfo<APValue::LValueBase>;
736 static unsigned getHashValue(const ObjectUnderConstruction &Object) {
737 return hash_value(Obj: Object);
738 }
739 static bool isEqual(const ObjectUnderConstruction &LHS,
740 const ObjectUnderConstruction &RHS) {
741 return LHS == RHS;
742 }
743};
744}
745
746namespace {
747 /// A dynamically-allocated heap object.
748 struct DynAlloc {
749 /// The value of this heap-allocated object.
750 APValue Value;
751 /// The allocating expression; used for diagnostics. Either a CXXNewExpr
752 /// or a CallExpr (the latter is for direct calls to operator new inside
753 /// std::allocator<T>::allocate).
754 const Expr *AllocExpr = nullptr;
755
756 enum Kind {
757 New,
758 ArrayNew,
759 StdAllocator
760 };
761
762 /// Get the kind of the allocation. This must match between allocation
763 /// and deallocation.
764 Kind getKind() const {
765 if (auto *NE = dyn_cast<CXXNewExpr>(Val: AllocExpr))
766 return NE->isArray() ? ArrayNew : New;
767 assert(isa<CallExpr>(AllocExpr));
768 return StdAllocator;
769 }
770 };
771
772 struct DynAllocOrder {
773 bool operator()(DynamicAllocLValue L, DynamicAllocLValue R) const {
774 return L.getIndex() < R.getIndex();
775 }
776 };
777
778 /// EvalInfo - This is a private struct used by the evaluator to capture
779 /// information about a subexpression as it is folded. It retains information
780 /// about the AST context, but also maintains information about the folded
781 /// expression.
782 ///
783 /// If an expression could be evaluated, it is still possible it is not a C
784 /// "integer constant expression" or constant expression. If not, this struct
785 /// captures information about how and why not.
786 ///
787 /// One bit of information passed *into* the request for constant folding
788 /// indicates whether the subexpression is "evaluated" or not according to C
789 /// rules. For example, the RHS of (0 && foo()) is not evaluated. We can
790 /// evaluate the expression regardless of what the RHS is, but C only allows
791 /// certain things in certain situations.
792 class EvalInfo final : public interp::State {
793 public:
794 /// CurrentCall - The top of the constexpr call stack.
795 CallStackFrame *CurrentCall;
796
797 /// CallStackDepth - The number of calls in the call stack right now.
798 unsigned CallStackDepth;
799
800 /// NextCallIndex - The next call index to assign.
801 unsigned NextCallIndex;
802
803 /// StepsLeft - The remaining number of evaluation steps we're permitted
804 /// to perform. This is essentially a limit for the number of statements
805 /// we will evaluate.
806 unsigned StepsLeft;
807
808 /// Enable the experimental new constant interpreter. If an expression is
809 /// not supported by the interpreter, an error is triggered.
810 bool EnableNewConstInterp;
811
812 /// BottomFrame - The frame in which evaluation started. This must be
813 /// initialized after CurrentCall and CallStackDepth.
814 CallStackFrame BottomFrame;
815
816 /// A stack of values whose lifetimes end at the end of some surrounding
817 /// evaluation frame.
818 llvm::SmallVector<Cleanup, 16> CleanupStack;
819
820 /// EvaluatingDecl - This is the declaration whose initializer is being
821 /// evaluated, if any.
822 APValue::LValueBase EvaluatingDecl;
823
824 enum class EvaluatingDeclKind {
825 None,
826 /// We're evaluating the construction of EvaluatingDecl.
827 Ctor,
828 /// We're evaluating the destruction of EvaluatingDecl.
829 Dtor,
830 };
831 EvaluatingDeclKind IsEvaluatingDecl = EvaluatingDeclKind::None;
832
833 /// EvaluatingDeclValue - This is the value being constructed for the
834 /// declaration whose initializer is being evaluated, if any.
835 APValue *EvaluatingDeclValue;
836
837 /// Stack of loops and 'switch' statements which we're currently
838 /// breaking/continuing; null entries are used to mark unlabeled
839 /// break/continue.
840 SmallVector<const Stmt *> BreakContinueStack;
841
842 /// Set of objects that are currently being constructed.
843 llvm::DenseMap<ObjectUnderConstruction, ConstructionPhase>
844 ObjectsUnderConstruction;
845
846 /// Current heap allocations, along with the location where each was
847 /// allocated. We use std::map here because we need stable addresses
848 /// for the stored APValues.
849 std::map<DynamicAllocLValue, DynAlloc, DynAllocOrder> HeapAllocs;
850
851 /// The number of heap allocations performed so far in this evaluation.
852 unsigned NumHeapAllocs = 0;
853
854 struct EvaluatingConstructorRAII {
855 EvalInfo &EI;
856 ObjectUnderConstruction Object;
857 bool DidInsert;
858 EvaluatingConstructorRAII(EvalInfo &EI, ObjectUnderConstruction Object,
859 bool HasBases)
860 : EI(EI), Object(Object) {
861 DidInsert =
862 EI.ObjectsUnderConstruction
863 .insert(KV: {Object, HasBases ? ConstructionPhase::Bases
864 : ConstructionPhase::AfterBases})
865 .second;
866 }
867 void finishedConstructingBases() {
868 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterBases;
869 }
870 void finishedConstructingFields() {
871 EI.ObjectsUnderConstruction[Object] = ConstructionPhase::AfterFields;
872 }
873 ~EvaluatingConstructorRAII() {
874 if (DidInsert) EI.ObjectsUnderConstruction.erase(Val: Object);
875 }
876 };
877
878 struct EvaluatingDestructorRAII {
879 EvalInfo &EI;
880 ObjectUnderConstruction Object;
881 bool DidInsert;
882 EvaluatingDestructorRAII(EvalInfo &EI, ObjectUnderConstruction Object)
883 : EI(EI), Object(Object) {
884 DidInsert = EI.ObjectsUnderConstruction
885 .insert(KV: {Object, ConstructionPhase::Destroying})
886 .second;
887 }
888 void startedDestroyingBases() {
889 EI.ObjectsUnderConstruction[Object] =
890 ConstructionPhase::DestroyingBases;
891 }
892 ~EvaluatingDestructorRAII() {
893 if (DidInsert)
894 EI.ObjectsUnderConstruction.erase(Val: Object);
895 }
896 };
897
898 ConstructionPhase
899 isEvaluatingCtorDtor(APValue::LValueBase Base,
900 ArrayRef<APValue::LValuePathEntry> Path) {
901 return ObjectsUnderConstruction.lookup(Val: {.Base: Base, .Path: Path});
902 }
903
904 /// If we're currently speculatively evaluating, the outermost call stack
905 /// depth at which we can mutate state, otherwise 0.
906 unsigned SpeculativeEvaluationDepth = 0;
907
908 /// The current array initialization index, if we're performing array
909 /// initialization.
910 uint64_t ArrayInitIndex = -1;
911
912 EvalInfo(const ASTContext &C, Expr::EvalStatus &S, EvaluationMode Mode)
913 : State(const_cast<ASTContext &>(C), S), CurrentCall(nullptr),
914 CallStackDepth(0), NextCallIndex(1),
915 StepsLeft(C.getLangOpts().ConstexprStepLimit),
916 EnableNewConstInterp(C.getLangOpts().EnableNewConstInterp),
917 BottomFrame(*this, SourceLocation(), /*Callee=*/nullptr,
918 /*This=*/nullptr,
919 /*CallExpr=*/nullptr, CallRef()),
920 EvaluatingDecl((const ValueDecl *)nullptr),
921 EvaluatingDeclValue(nullptr) {
922 EvalMode = Mode;
923 }
924
925 ~EvalInfo() {
926 discardCleanups();
927 }
928
929 void setEvaluatingDecl(APValue::LValueBase Base, APValue &Value,
930 EvaluatingDeclKind EDK = EvaluatingDeclKind::Ctor) {
931 EvaluatingDecl = Base;
932 IsEvaluatingDecl = EDK;
933 EvaluatingDeclValue = &Value;
934 }
935
936 bool CheckCallLimit(SourceLocation Loc) {
937 // Don't perform any constexpr calls (other than the call we're checking)
938 // when checking a potential constant expression.
939 if (checkingPotentialConstantExpression() && CallStackDepth > 1)
940 return false;
941 if (NextCallIndex == 0) {
942 // NextCallIndex has wrapped around.
943 FFDiag(Loc, DiagId: diag::note_constexpr_call_limit_exceeded);
944 return false;
945 }
946 if (CallStackDepth <= getLangOpts().ConstexprCallDepth)
947 return true;
948 FFDiag(Loc, DiagId: diag::note_constexpr_depth_limit_exceeded)
949 << getLangOpts().ConstexprCallDepth;
950 return false;
951 }
952
953 bool CheckArraySize(SourceLocation Loc, unsigned BitWidth,
954 uint64_t ElemCount, bool Diag) {
955 // FIXME: GH63562
956 // APValue stores array extents as unsigned,
957 // so anything that is greater that unsigned would overflow when
958 // constructing the array, we catch this here.
959 if (BitWidth > ConstantArrayType::getMaxSizeBits(Context: Ctx) ||
960 ElemCount > uint64_t(std::numeric_limits<unsigned>::max())) {
961 if (Diag)
962 FFDiag(Loc, DiagId: diag::note_constexpr_new_too_large) << ElemCount;
963 return false;
964 }
965
966 // FIXME: GH63562
967 // Arrays allocate an APValue per element.
968 // We use the number of constexpr steps as a proxy for the maximum size
969 // of arrays to avoid exhausting the system resources, as initialization
970 // of each element is likely to take some number of steps anyway.
971 uint64_t Limit = getLangOpts().ConstexprStepLimit;
972 if (Limit != 0 && ElemCount > Limit) {
973 if (Diag)
974 FFDiag(Loc, DiagId: diag::note_constexpr_new_exceeds_limits)
975 << ElemCount << Limit;
976 return false;
977 }
978 return true;
979 }
980
981 std::pair<CallStackFrame *, unsigned>
982 getCallFrameAndDepth(unsigned CallIndex) {
983 assert(CallIndex && "no call index in getCallFrameAndDepth");
984 // We will eventually hit BottomFrame, which has Index 1, so Frame can't
985 // be null in this loop.
986 unsigned Depth = CallStackDepth;
987 CallStackFrame *Frame = CurrentCall;
988 while (Frame->Index > CallIndex) {
989 Frame = Frame->Caller;
990 --Depth;
991 }
992 if (Frame->Index == CallIndex)
993 return {Frame, Depth};
994 return {nullptr, 0};
995 }
996
997 bool nextStep(const Stmt *S) {
998 if (getLangOpts().ConstexprStepLimit == 0)
999 return true;
1000
1001 if (!StepsLeft) {
1002 FFDiag(Loc: S->getBeginLoc(), DiagId: diag::note_constexpr_step_limit_exceeded);
1003 return false;
1004 }
1005 --StepsLeft;
1006 return true;
1007 }
1008
1009 APValue *createHeapAlloc(const Expr *E, QualType T, LValue &LV);
1010
1011 std::optional<DynAlloc *> lookupDynamicAlloc(DynamicAllocLValue DA) {
1012 std::optional<DynAlloc *> Result;
1013 auto It = HeapAllocs.find(x: DA);
1014 if (It != HeapAllocs.end())
1015 Result = &It->second;
1016 return Result;
1017 }
1018
1019 /// Get the allocated storage for the given parameter of the given call.
1020 APValue *getParamSlot(CallRef Call, const ParmVarDecl *PVD) {
1021 CallStackFrame *Frame = getCallFrameAndDepth(CallIndex: Call.CallIndex).first;
1022 return Frame ? Frame->getTemporary(Key: Call.getOrigParam(PVD), Version: Call.Version)
1023 : nullptr;
1024 }
1025
1026 /// Information about a stack frame for std::allocator<T>::[de]allocate.
1027 struct StdAllocatorCaller {
1028 unsigned FrameIndex;
1029 QualType ElemType;
1030 const Expr *Call;
1031 explicit operator bool() const { return FrameIndex != 0; };
1032 };
1033
1034 StdAllocatorCaller getStdAllocatorCaller(StringRef FnName) const {
1035 for (const CallStackFrame *Call = CurrentCall; Call->Caller != nullptr;
1036 Call = Call->Caller) {
1037 const auto *MD = dyn_cast_or_null<CXXMethodDecl>(Val: Call->Callee);
1038 if (!MD)
1039 continue;
1040 const IdentifierInfo *FnII = MD->getIdentifier();
1041 if (!FnII || !FnII->isStr(Str: FnName))
1042 continue;
1043
1044 const auto *CTSD =
1045 dyn_cast<ClassTemplateSpecializationDecl>(Val: MD->getParent());
1046 if (!CTSD)
1047 continue;
1048
1049 const IdentifierInfo *ClassII = CTSD->getIdentifier();
1050 const TemplateArgumentList &TAL = CTSD->getTemplateArgs();
1051 if (CTSD->isInStdNamespace() && ClassII &&
1052 ClassII->isStr(Str: "allocator") && TAL.size() >= 1 &&
1053 TAL[0].getKind() == TemplateArgument::Type)
1054 return {.FrameIndex: Call->Index, .ElemType: TAL[0].getAsType(), .Call: Call->CallExpr};
1055 }
1056
1057 return {};
1058 }
1059
1060 void performLifetimeExtension() {
1061 // Disable the cleanups for lifetime-extended temporaries.
1062 llvm::erase_if(C&: CleanupStack, P: [](Cleanup &C) {
1063 return !C.isDestroyedAtEndOf(K: ScopeKind::FullExpression);
1064 });
1065 }
1066
1067 /// Throw away any remaining cleanups at the end of evaluation. If any
1068 /// cleanups would have had a side-effect, note that as an unmodeled
1069 /// side-effect and return false. Otherwise, return true.
1070 bool discardCleanups() {
1071 for (Cleanup &C : CleanupStack) {
1072 if (C.hasSideEffect() && !noteSideEffect()) {
1073 CleanupStack.clear();
1074 return false;
1075 }
1076 }
1077 CleanupStack.clear();
1078 return true;
1079 }
1080
1081 private:
1082 const interp::Frame *getCurrentFrame() override { return CurrentCall; }
1083
1084 unsigned getCallStackDepth() override { return CallStackDepth; }
1085 bool stepsLeft() const override { return StepsLeft > 0; }
1086
1087 public:
1088 /// Notes that we failed to evaluate an expression that other expressions
1089 /// directly depend on, and determine if we should keep evaluating. This
1090 /// should only be called if we actually intend to keep evaluating.
1091 ///
1092 /// Call noteSideEffect() instead if we may be able to ignore the value that
1093 /// we failed to evaluate, e.g. if we failed to evaluate Foo() in:
1094 ///
1095 /// (Foo(), 1) // use noteSideEffect
1096 /// (Foo() || true) // use noteSideEffect
1097 /// Foo() + 1 // use noteFailure
1098 [[nodiscard]] bool noteFailure() {
1099 // Failure when evaluating some expression often means there is some
1100 // subexpression whose evaluation was skipped. Therefore, (because we
1101 // don't track whether we skipped an expression when unwinding after an
1102 // evaluation failure) every evaluation failure that bubbles up from a
1103 // subexpression implies that a side-effect has potentially happened. We
1104 // skip setting the HasSideEffects flag to true until we decide to
1105 // continue evaluating after that point, which happens here.
1106 bool KeepGoing = keepEvaluatingAfterFailure();
1107 EvalStatus.HasSideEffects |= KeepGoing;
1108 return KeepGoing;
1109 }
1110
1111 class ArrayInitLoopIndex {
1112 EvalInfo &Info;
1113 uint64_t OuterIndex;
1114
1115 public:
1116 ArrayInitLoopIndex(EvalInfo &Info)
1117 : Info(Info), OuterIndex(Info.ArrayInitIndex) {
1118 Info.ArrayInitIndex = 0;
1119 }
1120 ~ArrayInitLoopIndex() { Info.ArrayInitIndex = OuterIndex; }
1121
1122 operator uint64_t&() { return Info.ArrayInitIndex; }
1123 };
1124 };
1125
1126 /// Object used to treat all foldable expressions as constant expressions.
1127 struct FoldConstant {
1128 EvalInfo &Info;
1129 bool Enabled;
1130 bool HadNoPriorDiags;
1131 EvaluationMode OldMode;
1132
1133 explicit FoldConstant(EvalInfo &Info, bool Enabled)
1134 : Info(Info),
1135 Enabled(Enabled),
1136 HadNoPriorDiags(Info.EvalStatus.Diag &&
1137 Info.EvalStatus.Diag->empty() &&
1138 !Info.EvalStatus.HasSideEffects),
1139 OldMode(Info.EvalMode) {
1140 if (Enabled)
1141 Info.EvalMode = EvaluationMode::ConstantFold;
1142 }
1143 void keepDiagnostics() { Enabled = false; }
1144 ~FoldConstant() {
1145 if (Enabled && HadNoPriorDiags && !Info.EvalStatus.Diag->empty() &&
1146 !Info.EvalStatus.HasSideEffects) {
1147 Info.EvalStatus.Diag->clear();
1148 Info.EvalStatus.DiagEmitted = false;
1149 }
1150 Info.EvalMode = OldMode;
1151 }
1152 };
1153
1154 /// RAII object used to set the current evaluation mode to ignore
1155 /// side-effects.
1156 struct IgnoreSideEffectsRAII {
1157 EvalInfo &Info;
1158 EvaluationMode OldMode;
1159 explicit IgnoreSideEffectsRAII(EvalInfo &Info)
1160 : Info(Info), OldMode(Info.EvalMode) {
1161 Info.EvalMode = EvaluationMode::IgnoreSideEffects;
1162 }
1163
1164 ~IgnoreSideEffectsRAII() { Info.EvalMode = OldMode; }
1165 };
1166
1167 /// RAII object used to optionally suppress diagnostics and side-effects from
1168 /// a speculative evaluation.
1169 class SpeculativeEvaluationRAII {
1170 EvalInfo *Info = nullptr;
1171 Expr::EvalStatus OldStatus;
1172 unsigned OldSpeculativeEvaluationDepth = 0;
1173
1174 void moveFromAndCancel(SpeculativeEvaluationRAII &&Other) {
1175 Info = Other.Info;
1176 OldStatus = Other.OldStatus;
1177 OldSpeculativeEvaluationDepth = Other.OldSpeculativeEvaluationDepth;
1178 Other.Info = nullptr;
1179 }
1180
1181 void maybeRestoreState() {
1182 if (!Info)
1183 return;
1184
1185 Info->EvalStatus = OldStatus;
1186 Info->SpeculativeEvaluationDepth = OldSpeculativeEvaluationDepth;
1187 }
1188
1189 public:
1190 SpeculativeEvaluationRAII() = default;
1191
1192 SpeculativeEvaluationRAII(
1193 EvalInfo &Info, SmallVectorImpl<PartialDiagnosticAt> *NewDiag = nullptr)
1194 : Info(&Info), OldStatus(Info.EvalStatus),
1195 OldSpeculativeEvaluationDepth(Info.SpeculativeEvaluationDepth) {
1196 Info.EvalStatus.Diag = NewDiag;
1197 Info.SpeculativeEvaluationDepth = Info.CallStackDepth + 1;
1198 }
1199
1200 SpeculativeEvaluationRAII(const SpeculativeEvaluationRAII &Other) = delete;
1201 SpeculativeEvaluationRAII(SpeculativeEvaluationRAII &&Other) {
1202 moveFromAndCancel(Other: std::move(Other));
1203 }
1204
1205 SpeculativeEvaluationRAII &operator=(SpeculativeEvaluationRAII &&Other) {
1206 maybeRestoreState();
1207 moveFromAndCancel(Other: std::move(Other));
1208 return *this;
1209 }
1210
1211 ~SpeculativeEvaluationRAII() { maybeRestoreState(); }
1212 };
1213
1214 /// RAII object wrapping a full-expression or block scope, and handling
1215 /// the ending of the lifetime of temporaries created within it.
1216 template<ScopeKind Kind>
1217 class ScopeRAII {
1218 EvalInfo &Info;
1219 unsigned OldStackSize;
1220 public:
1221 ScopeRAII(EvalInfo &Info)
1222 : Info(Info), OldStackSize(Info.CleanupStack.size()) {
1223 // Push a new temporary version. This is needed to distinguish between
1224 // temporaries created in different iterations of a loop.
1225 Info.CurrentCall->pushTempVersion();
1226 }
1227 bool destroy(bool RunDestructors = true) {
1228 bool OK = cleanup(Info, RunDestructors, OldStackSize);
1229 OldStackSize = std::numeric_limits<unsigned>::max();
1230 return OK;
1231 }
1232 ~ScopeRAII() {
1233 if (OldStackSize != std::numeric_limits<unsigned>::max())
1234 destroy(RunDestructors: false);
1235 // Body moved to a static method to encourage the compiler to inline away
1236 // instances of this class.
1237 Info.CurrentCall->popTempVersion();
1238 }
1239 private:
1240 static bool cleanup(EvalInfo &Info, bool RunDestructors,
1241 unsigned OldStackSize) {
1242 assert(OldStackSize <= Info.CleanupStack.size() &&
1243 "running cleanups out of order?");
1244
1245 // Run all cleanups for a block scope, and non-lifetime-extended cleanups
1246 // for a full-expression scope.
1247 bool Success = true;
1248 for (unsigned I = Info.CleanupStack.size(); I > OldStackSize; --I) {
1249 if (Info.CleanupStack[I - 1].isDestroyedAtEndOf(K: Kind)) {
1250 if (!Info.CleanupStack[I - 1].endLifetime(Info, RunDestructors)) {
1251 Success = false;
1252 break;
1253 }
1254 }
1255 }
1256
1257 // Compact any retained cleanups.
1258 auto NewEnd = Info.CleanupStack.begin() + OldStackSize;
1259 if (Kind != ScopeKind::Block)
1260 NewEnd =
1261 std::remove_if(NewEnd, Info.CleanupStack.end(), [](Cleanup &C) {
1262 return C.isDestroyedAtEndOf(K: Kind);
1263 });
1264 Info.CleanupStack.erase(CS: NewEnd, CE: Info.CleanupStack.end());
1265 return Success;
1266 }
1267 };
1268 typedef ScopeRAII<ScopeKind::Block> BlockScopeRAII;
1269 typedef ScopeRAII<ScopeKind::FullExpression> FullExpressionRAII;
1270 typedef ScopeRAII<ScopeKind::Call> CallScopeRAII;
1271}
1272
1273bool SubobjectDesignator::checkSubobject(EvalInfo &Info, const Expr *E,
1274 CheckSubobjectKind CSK) {
1275 if (Invalid)
1276 return false;
1277 if (isOnePastTheEnd()) {
1278 Info.CCEDiag(E, DiagId: diag::note_constexpr_past_end_subobject)
1279 << CSK;
1280 setInvalid();
1281 return false;
1282 }
1283 // Note, we do not diagnose if isMostDerivedAnUnsizedArray(), because there
1284 // must actually be at least one array element; even a VLA cannot have a
1285 // bound of zero. And if our index is nonzero, we already had a CCEDiag.
1286 return true;
1287}
1288
1289void SubobjectDesignator::diagnoseUnsizedArrayPointerArithmetic(EvalInfo &Info,
1290 const Expr *E) {
1291 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsized_array_indexed);
1292 // Do not set the designator as invalid: we can represent this situation,
1293 // and correct handling of __builtin_object_size requires us to do so.
1294}
1295
1296void SubobjectDesignator::diagnosePointerArithmetic(EvalInfo &Info,
1297 const Expr *E,
1298 const APSInt &N) {
1299 // If we're complaining, we must be able to statically determine the size of
1300 // the most derived array.
1301 if (MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement)
1302 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1303 << N << /*array*/ 0
1304 << static_cast<unsigned>(getMostDerivedArraySize());
1305 else
1306 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
1307 << N << /*non-array*/ 1;
1308 setInvalid();
1309}
1310
1311CallStackFrame::CallStackFrame(EvalInfo &Info, SourceRange CallRange,
1312 const FunctionDecl *Callee, const LValue *This,
1313 const Expr *CallExpr, CallRef Call)
1314 : Info(Info), Caller(Info.CurrentCall), Callee(Callee), This(This),
1315 CallExpr(CallExpr), Arguments(Call), CallRange(CallRange),
1316 Index(Info.NextCallIndex++) {
1317 Info.CurrentCall = this;
1318 ++Info.CallStackDepth;
1319}
1320
1321CallStackFrame::~CallStackFrame() {
1322 assert(Info.CurrentCall == this && "calls retired out of order");
1323 --Info.CallStackDepth;
1324 Info.CurrentCall = Caller;
1325}
1326
1327static bool isRead(AccessKinds AK) {
1328 return AK == AK_Read || AK == AK_ReadObjectRepresentation ||
1329 AK == AK_IsWithinLifetime || AK == AK_Dereference;
1330}
1331
1332static bool isModification(AccessKinds AK) {
1333 switch (AK) {
1334 case AK_Read:
1335 case AK_ReadObjectRepresentation:
1336 case AK_MemberCall:
1337 case AK_DynamicCast:
1338 case AK_TypeId:
1339 case AK_IsWithinLifetime:
1340 case AK_Dereference:
1341 return false;
1342 case AK_Assign:
1343 case AK_Increment:
1344 case AK_Decrement:
1345 case AK_Construct:
1346 case AK_Destroy:
1347 return true;
1348 }
1349 llvm_unreachable("unknown access kind");
1350}
1351
1352static bool isAnyAccess(AccessKinds AK) {
1353 return isRead(AK) || isModification(AK);
1354}
1355
1356/// Is this an access per the C++ definition?
1357static bool isFormalAccess(AccessKinds AK) {
1358 return isAnyAccess(AK) && AK != AK_Construct && AK != AK_Destroy &&
1359 AK != AK_IsWithinLifetime && AK != AK_Dereference;
1360}
1361
1362/// Is this kind of access valid on an indeterminate object value?
1363static bool isValidIndeterminateAccess(AccessKinds AK) {
1364 switch (AK) {
1365 case AK_Read:
1366 case AK_Increment:
1367 case AK_Decrement:
1368 case AK_Dereference:
1369 // These need the object's value.
1370 return false;
1371
1372 case AK_IsWithinLifetime:
1373 case AK_ReadObjectRepresentation:
1374 case AK_Assign:
1375 case AK_Construct:
1376 case AK_Destroy:
1377 // Construction and destruction don't need the value.
1378 return true;
1379
1380 case AK_MemberCall:
1381 case AK_DynamicCast:
1382 case AK_TypeId:
1383 // These aren't really meaningful on scalars.
1384 return true;
1385 }
1386 llvm_unreachable("unknown access kind");
1387}
1388
1389namespace {
1390 struct ComplexValue {
1391 private:
1392 bool IsInt;
1393
1394 public:
1395 APSInt IntReal, IntImag;
1396 APFloat FloatReal, FloatImag;
1397
1398 ComplexValue() : FloatReal(APFloat::Bogus()), FloatImag(APFloat::Bogus()) {}
1399
1400 void makeComplexFloat() { IsInt = false; }
1401 bool isComplexFloat() const { return !IsInt; }
1402 APFloat &getComplexFloatReal() { return FloatReal; }
1403 APFloat &getComplexFloatImag() { return FloatImag; }
1404
1405 void makeComplexInt() { IsInt = true; }
1406 bool isComplexInt() const { return IsInt; }
1407 APSInt &getComplexIntReal() { return IntReal; }
1408 APSInt &getComplexIntImag() { return IntImag; }
1409
1410 void moveInto(APValue &v) const {
1411 if (isComplexFloat())
1412 v = APValue(FloatReal, FloatImag);
1413 else
1414 v = APValue(IntReal, IntImag);
1415 }
1416 void setFrom(const APValue &v) {
1417 assert(v.isComplexFloat() || v.isComplexInt());
1418 if (v.isComplexFloat()) {
1419 makeComplexFloat();
1420 FloatReal = v.getComplexFloatReal();
1421 FloatImag = v.getComplexFloatImag();
1422 } else {
1423 makeComplexInt();
1424 IntReal = v.getComplexIntReal();
1425 IntImag = v.getComplexIntImag();
1426 }
1427 }
1428 };
1429
1430 struct LValue {
1431 APValue::LValueBase Base;
1432 CharUnits Offset;
1433 SubobjectDesignator Designator;
1434 bool IsNullPtr : 1;
1435 bool InvalidBase : 1;
1436 // P2280R4 track if we have an unknown reference or pointer.
1437 bool AllowConstexprUnknown = false;
1438
1439 const APValue::LValueBase getLValueBase() const { return Base; }
1440 bool allowConstexprUnknown() const { return AllowConstexprUnknown; }
1441 CharUnits &getLValueOffset() { return Offset; }
1442 const CharUnits &getLValueOffset() const { return Offset; }
1443 SubobjectDesignator &getLValueDesignator() { return Designator; }
1444 const SubobjectDesignator &getLValueDesignator() const { return Designator;}
1445 bool isNullPointer() const { return IsNullPtr;}
1446
1447 unsigned getLValueCallIndex() const { return Base.getCallIndex(); }
1448 unsigned getLValueVersion() const { return Base.getVersion(); }
1449
1450 void moveInto(APValue &V) const {
1451 if (Designator.Invalid)
1452 V = APValue(Base, Offset, APValue::NoLValuePath(), IsNullPtr);
1453 else {
1454 assert(!InvalidBase && "APValues can't handle invalid LValue bases");
1455 V = APValue(Base, Offset, Designator.Entries,
1456 Designator.IsOnePastTheEnd, IsNullPtr);
1457 }
1458 if (AllowConstexprUnknown)
1459 V.setConstexprUnknown();
1460 }
1461 void setFrom(const ASTContext &Ctx, const APValue &V) {
1462 assert(V.isLValue() && "Setting LValue from a non-LValue?");
1463 Base = V.getLValueBase();
1464 Offset = V.getLValueOffset();
1465 InvalidBase = false;
1466 Designator = SubobjectDesignator(Ctx, V);
1467 IsNullPtr = V.isNullPointer();
1468 AllowConstexprUnknown = V.allowConstexprUnknown();
1469 }
1470
1471 void set(APValue::LValueBase B, bool BInvalid = false) {
1472#ifndef NDEBUG
1473 // We only allow a few types of invalid bases. Enforce that here.
1474 if (BInvalid) {
1475 const auto *E = B.get<const Expr *>();
1476 assert((isa<MemberExpr>(E) || tryUnwrapAllocSizeCall(E)) &&
1477 "Unexpected type of invalid base");
1478 }
1479#endif
1480
1481 Base = B;
1482 Offset = CharUnits::fromQuantity(Quantity: 0);
1483 InvalidBase = BInvalid;
1484 Designator = SubobjectDesignator(getType(B));
1485 IsNullPtr = false;
1486 AllowConstexprUnknown = false;
1487 }
1488
1489 void setNull(ASTContext &Ctx, QualType PointerTy) {
1490 Base = (const ValueDecl *)nullptr;
1491 Offset =
1492 CharUnits::fromQuantity(Quantity: Ctx.getTargetNullPointerValue(QT: PointerTy));
1493 InvalidBase = false;
1494 Designator = SubobjectDesignator(PointerTy->getPointeeType());
1495 IsNullPtr = true;
1496 AllowConstexprUnknown = false;
1497 }
1498
1499 void setInvalid(APValue::LValueBase B, unsigned I = 0) {
1500 set(B, BInvalid: true);
1501 }
1502
1503 std::string toString(ASTContext &Ctx, QualType T) const {
1504 APValue Printable;
1505 moveInto(V&: Printable);
1506 return Printable.getAsString(Ctx, Ty: T);
1507 }
1508
1509 private:
1510 // Check that this LValue is not based on a null pointer. If it is, produce
1511 // a diagnostic and mark the designator as invalid.
1512 template <typename GenDiagType>
1513 bool checkNullPointerDiagnosingWith(const GenDiagType &GenDiag) {
1514 if (Designator.Invalid)
1515 return false;
1516 if (IsNullPtr) {
1517 GenDiag();
1518 Designator.setInvalid();
1519 return false;
1520 }
1521 return true;
1522 }
1523
1524 public:
1525 bool checkNullPointer(EvalInfo &Info, const Expr *E,
1526 CheckSubobjectKind CSK) {
1527 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, CSK] {
1528 Info.CCEDiag(E, DiagId: diag::note_constexpr_null_subobject) << CSK;
1529 });
1530 }
1531
1532 bool checkNullPointerForFoldAccess(EvalInfo &Info, const Expr *E,
1533 AccessKinds AK) {
1534 return checkNullPointerDiagnosingWith(GenDiag: [&Info, E, AK] {
1535 if (AK == AccessKinds::AK_Dereference)
1536 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
1537 else
1538 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
1539 });
1540 }
1541
1542 // Check this LValue refers to an object. If not, set the designator to be
1543 // invalid and emit a diagnostic.
1544 bool checkSubobject(EvalInfo &Info, const Expr *E, CheckSubobjectKind CSK) {
1545 return (CSK == CSK_ArrayToPointer || checkNullPointer(Info, E, CSK)) &&
1546 Designator.checkSubobject(Info, E, CSK);
1547 }
1548
1549 void addDecl(EvalInfo &Info, const Expr *E,
1550 const Decl *D, bool Virtual = false) {
1551 if (checkSubobject(Info, E, CSK: isa<FieldDecl>(Val: D) ? CSK_Field : CSK_Base))
1552 Designator.addDeclUnchecked(D, Virtual);
1553 }
1554 void addUnsizedArray(EvalInfo &Info, const Expr *E, QualType ElemTy) {
1555 if (!Designator.Entries.empty()) {
1556 Info.CCEDiag(E, DiagId: diag::note_constexpr_unsupported_unsized_array);
1557 Designator.setInvalid();
1558 return;
1559 }
1560 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer)) {
1561 assert(getType(Base).getNonReferenceType()->isPointerType() ||
1562 getType(Base).getNonReferenceType()->isArrayType());
1563 Designator.FirstEntryIsAnUnsizedArray = true;
1564 Designator.addUnsizedArrayUnchecked(ElemTy);
1565 }
1566 }
1567 void addArray(EvalInfo &Info, const Expr *E, const ConstantArrayType *CAT) {
1568 if (checkSubobject(Info, E, CSK: CSK_ArrayToPointer))
1569 Designator.addArrayUnchecked(CAT);
1570 }
1571 void addComplex(EvalInfo &Info, const Expr *E, QualType EltTy, bool Imag) {
1572 if (checkSubobject(Info, E, CSK: Imag ? CSK_Imag : CSK_Real))
1573 Designator.addComplexUnchecked(EltTy, Imag);
1574 }
1575 void addVectorElement(EvalInfo &Info, const Expr *E, QualType EltTy,
1576 uint64_t Size, uint64_t Idx) {
1577 if (checkSubobject(Info, E, CSK: CSK_VectorElement))
1578 Designator.addVectorElementUnchecked(EltTy, Size, Idx);
1579 }
1580 void clearIsNullPointer() {
1581 IsNullPtr = false;
1582 }
1583 void adjustOffsetAndIndex(EvalInfo &Info, const Expr *E,
1584 const APSInt &Index, CharUnits ElementSize) {
1585 // An index of 0 has no effect. (In C, adding 0 to a null pointer is UB,
1586 // but we're not required to diagnose it and it's valid in C++.)
1587 if (!Index)
1588 return;
1589
1590 // Compute the new offset in the appropriate width, wrapping at 64 bits.
1591 // FIXME: When compiling for a 32-bit target, we should use 32-bit
1592 // offsets.
1593 uint64_t Offset64 = Offset.getQuantity();
1594 uint64_t ElemSize64 = ElementSize.getQuantity();
1595 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
1596 Offset = CharUnits::fromQuantity(Quantity: Offset64 + ElemSize64 * Index64);
1597
1598 if (checkNullPointer(Info, E, CSK: CSK_ArrayIndex))
1599 Designator.adjustIndex(Info, E, N: Index, LV: *this);
1600 clearIsNullPointer();
1601 }
1602 void adjustOffset(CharUnits N) {
1603 Offset += N;
1604 if (N.getQuantity())
1605 clearIsNullPointer();
1606 }
1607 };
1608
1609 struct MemberPtr {
1610 MemberPtr() {}
1611 explicit MemberPtr(const ValueDecl *Decl)
1612 : DeclAndIsDerivedMember(Decl, false) {}
1613
1614 /// The member or (direct or indirect) field referred to by this member
1615 /// pointer, or 0 if this is a null member pointer.
1616 const ValueDecl *getDecl() const {
1617 return DeclAndIsDerivedMember.getPointer();
1618 }
1619 /// Is this actually a member of some type derived from the relevant class?
1620 bool isDerivedMember() const {
1621 return DeclAndIsDerivedMember.getInt();
1622 }
1623 /// Get the class which the declaration actually lives in.
1624 const CXXRecordDecl *getContainingRecord() const {
1625 return cast<CXXRecordDecl>(
1626 Val: DeclAndIsDerivedMember.getPointer()->getDeclContext());
1627 }
1628
1629 void moveInto(APValue &V) const {
1630 V = APValue(getDecl(), isDerivedMember(), Path);
1631 }
1632 void setFrom(const APValue &V) {
1633 assert(V.isMemberPointer());
1634 DeclAndIsDerivedMember.setPointer(V.getMemberPointerDecl());
1635 DeclAndIsDerivedMember.setInt(V.isMemberPointerToDerivedMember());
1636 Path.clear();
1637 llvm::append_range(C&: Path, R: V.getMemberPointerPath());
1638 }
1639
1640 /// DeclAndIsDerivedMember - The member declaration, and a flag indicating
1641 /// whether the member is a member of some class derived from the class type
1642 /// of the member pointer.
1643 llvm::PointerIntPair<const ValueDecl*, 1, bool> DeclAndIsDerivedMember;
1644 /// Path - The path of base/derived classes from the member declaration's
1645 /// class (exclusive) to the class type of the member pointer (inclusive).
1646 SmallVector<const CXXRecordDecl*, 4> Path;
1647
1648 /// Perform a cast towards the class of the Decl (either up or down the
1649 /// hierarchy).
1650 bool castBack(const CXXRecordDecl *Class) {
1651 assert(!Path.empty());
1652 const CXXRecordDecl *Expected;
1653 if (Path.size() >= 2)
1654 Expected = Path[Path.size() - 2];
1655 else
1656 Expected = getContainingRecord();
1657 if (Expected->getCanonicalDecl() != Class->getCanonicalDecl()) {
1658 // C++11 [expr.static.cast]p12: In a conversion from (D::*) to (B::*),
1659 // if B does not contain the original member and is not a base or
1660 // derived class of the class containing the original member, the result
1661 // of the cast is undefined.
1662 // C++11 [conv.mem]p2 does not cover this case for a cast from (B::*) to
1663 // (D::*). We consider that to be a language defect.
1664 return false;
1665 }
1666 Path.pop_back();
1667 return true;
1668 }
1669 /// Perform a base-to-derived member pointer cast.
1670 bool castToDerived(const CXXRecordDecl *Derived) {
1671 if (!getDecl())
1672 return true;
1673 if (!isDerivedMember()) {
1674 Path.push_back(Elt: Derived);
1675 return true;
1676 }
1677 if (!castBack(Class: Derived))
1678 return false;
1679 if (Path.empty())
1680 DeclAndIsDerivedMember.setInt(false);
1681 return true;
1682 }
1683 /// Perform a derived-to-base member pointer cast.
1684 bool castToBase(const CXXRecordDecl *Base) {
1685 if (!getDecl())
1686 return true;
1687 if (Path.empty())
1688 DeclAndIsDerivedMember.setInt(true);
1689 if (isDerivedMember()) {
1690 Path.push_back(Elt: Base);
1691 return true;
1692 }
1693 return castBack(Class: Base);
1694 }
1695 };
1696
1697 /// Compare two member pointers, which are assumed to be of the same type.
1698 static bool operator==(const MemberPtr &LHS, const MemberPtr &RHS) {
1699 if (!LHS.getDecl() || !RHS.getDecl())
1700 return !LHS.getDecl() && !RHS.getDecl();
1701 if (LHS.getDecl()->getCanonicalDecl() != RHS.getDecl()->getCanonicalDecl())
1702 return false;
1703 return LHS.Path == RHS.Path;
1704 }
1705}
1706
1707void SubobjectDesignator::adjustIndex(EvalInfo &Info, const Expr *E, APSInt N,
1708 const LValue &LV) {
1709 if (Invalid || !N)
1710 return;
1711 uint64_t TruncatedN = N.extOrTrunc(width: 64).getZExtValue();
1712 if (isMostDerivedAnUnsizedArray()) {
1713 diagnoseUnsizedArrayPointerArithmetic(Info, E);
1714 // Can't verify -- trust that the user is doing the right thing (or if
1715 // not, trust that the caller will catch the bad behavior).
1716 // FIXME: Should we reject if this overflows, at least?
1717 Entries.back() =
1718 PathEntry::ArrayIndex(Index: Entries.back().getAsArrayIndex() + TruncatedN);
1719 return;
1720 }
1721
1722 // [expr.add]p4: For the purposes of these operators, a pointer to a
1723 // nonarray object behaves the same as a pointer to the first element of
1724 // an array of length one with the type of the object as its element type.
1725 bool IsArray =
1726 MostDerivedPathLength == Entries.size() && MostDerivedIsArrayElement;
1727 uint64_t ArrayIndex =
1728 IsArray ? Entries.back().getAsArrayIndex() : (uint64_t)IsOnePastTheEnd;
1729 uint64_t ArraySize = IsArray ? getMostDerivedArraySize() : (uint64_t)1;
1730
1731 if (N < -(int64_t)ArrayIndex || N > ArraySize - ArrayIndex) {
1732 if (!Info.checkingPotentialConstantExpression() ||
1733 !LV.AllowConstexprUnknown) {
1734 // Calculate the actual index in a wide enough type, so we can include
1735 // it in the note.
1736 N = N.extend(width: std::max<unsigned>(a: N.getBitWidth() + 1, b: 65));
1737 (llvm::APInt &)N += ArrayIndex;
1738 assert(N.ugt(ArraySize) && "bounds check failed for in-bounds index");
1739 diagnosePointerArithmetic(Info, E, N);
1740 }
1741 setInvalid();
1742 return;
1743 }
1744
1745 ArrayIndex += TruncatedN;
1746 assert(ArrayIndex <= ArraySize &&
1747 "bounds check succeeded for out-of-bounds index");
1748
1749 if (IsArray)
1750 Entries.back() = PathEntry::ArrayIndex(Index: ArrayIndex);
1751 else
1752 IsOnePastTheEnd = (ArrayIndex != 0);
1753}
1754
1755static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E);
1756static bool EvaluateInPlace(APValue &Result, EvalInfo &Info,
1757 const LValue &This, const Expr *E,
1758 bool AllowNonLiteralTypes = false);
1759static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
1760 bool InvalidBaseOK = false);
1761static bool EvaluatePointer(const Expr *E, LValue &Result, EvalInfo &Info,
1762 bool InvalidBaseOK = false);
1763static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
1764 EvalInfo &Info);
1765static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info);
1766static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info);
1767static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
1768 EvalInfo &Info);
1769static bool EvaluateFloat(const Expr *E, APFloat &Result, EvalInfo &Info);
1770static bool EvaluateComplex(const Expr *E, ComplexValue &Res, EvalInfo &Info);
1771static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info);
1772static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
1773 EvalInfo &Info);
1774static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result);
1775static std::optional<uint64_t>
1776EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
1777 std::string *StringResult = nullptr);
1778
1779/// Evaluate an integer or fixed point expression into an APResult.
1780static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
1781 EvalInfo &Info);
1782
1783/// Evaluate only a fixed point expression into an APResult.
1784static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
1785 EvalInfo &Info);
1786
1787//===----------------------------------------------------------------------===//
1788// Misc utilities
1789//===----------------------------------------------------------------------===//
1790
1791/// Negate an APSInt in place, converting it to a signed form if necessary, and
1792/// preserving its value (by extending by up to one bit as needed).
1793static void negateAsSigned(APSInt &Int) {
1794 if (Int.isUnsigned() || Int.isMinSignedValue()) {
1795 Int = Int.extend(width: Int.getBitWidth() + 1);
1796 Int.setIsSigned(true);
1797 }
1798 Int = -Int;
1799}
1800
1801template<typename KeyT>
1802APValue &CallStackFrame::createTemporary(const KeyT *Key, QualType T,
1803 ScopeKind Scope, LValue &LV) {
1804 unsigned Version = getTempVersion();
1805 APValue::LValueBase Base(Key, Index, Version);
1806 LV.set(B: Base);
1807 return createLocal(Base, Key, T, Scope);
1808}
1809
1810/// Allocate storage for a parameter of a function call made in this frame.
1811APValue &CallStackFrame::createParam(CallRef Args, const ParmVarDecl *PVD,
1812 LValue &LV) {
1813 assert(Args.CallIndex == Index && "creating parameter in wrong frame");
1814 APValue::LValueBase Base(PVD, Index, Args.Version);
1815 LV.set(B: Base);
1816 // We always destroy parameters at the end of the call, even if we'd allow
1817 // them to live to the end of the full-expression at runtime, in order to
1818 // give portable results and match other compilers.
1819 return createLocal(Base, Key: PVD, T: PVD->getType(), Scope: ScopeKind::Call);
1820}
1821
1822APValue &CallStackFrame::createLocal(APValue::LValueBase Base, const void *Key,
1823 QualType T, ScopeKind Scope) {
1824 assert(Base.getCallIndex() == Index && "lvalue for wrong frame");
1825 unsigned Version = Base.getVersion();
1826 APValue &Result = Temporaries[MapKeyTy(Key, Version)];
1827 assert(Result.isAbsent() && "local created multiple times");
1828
1829 // If we're creating a local immediately in the operand of a speculative
1830 // evaluation, don't register a cleanup to be run outside the speculative
1831 // evaluation context, since we won't actually be able to initialize this
1832 // object.
1833 if (Index <= Info.SpeculativeEvaluationDepth) {
1834 if (T.isDestructedType())
1835 Info.noteSideEffect();
1836 } else {
1837 Info.CleanupStack.push_back(Elt: Cleanup(&Result, Base, T, Scope));
1838 }
1839 return Result;
1840}
1841
1842APValue *EvalInfo::createHeapAlloc(const Expr *E, QualType T, LValue &LV) {
1843 if (NumHeapAllocs > DynamicAllocLValue::getMaxIndex()) {
1844 FFDiag(E, DiagId: diag::note_constexpr_heap_alloc_limit_exceeded);
1845 return nullptr;
1846 }
1847
1848 DynamicAllocLValue DA(NumHeapAllocs++);
1849 LV.set(B: APValue::LValueBase::getDynamicAlloc(LV: DA, Type: T));
1850 auto Result = HeapAllocs.emplace(args: std::piecewise_construct,
1851 args: std::forward_as_tuple(args&: DA), args: std::tuple<>());
1852 assert(Result.second && "reused a heap alloc index?");
1853 Result.first->second.AllocExpr = E;
1854 return &Result.first->second.Value;
1855}
1856
1857/// Produce a string describing the given constexpr call.
1858void CallStackFrame::describe(raw_ostream &Out) const {
1859 bool IsMemberCall = false;
1860 bool ExplicitInstanceParam = false;
1861 clang::PrintingPolicy PrintingPolicy = Info.Ctx.getPrintingPolicy();
1862 PrintingPolicy.SuppressLambdaBody = true;
1863
1864 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: Callee)) {
1865 IsMemberCall = !isa<CXXConstructorDecl>(Val: MD) && !MD->isStatic();
1866 ExplicitInstanceParam = MD->isExplicitObjectMemberFunction();
1867 }
1868
1869 if (!IsMemberCall)
1870 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1871 /*Qualified=*/false);
1872
1873 if (This && IsMemberCall) {
1874 if (const auto *MCE = dyn_cast_if_present<CXXMemberCallExpr>(Val: CallExpr)) {
1875 const Expr *Object = MCE->getImplicitObjectArgument();
1876 Object->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1877 /*Indentation=*/0);
1878 if (Object->getType()->isPointerType())
1879 Out << "->";
1880 else
1881 Out << ".";
1882 } else if (const auto *OCE =
1883 dyn_cast_if_present<CXXOperatorCallExpr>(Val: CallExpr)) {
1884 OCE->getArg(Arg: 0)->printPretty(OS&: Out, /*Helper=*/nullptr, Policy: PrintingPolicy,
1885 /*Indentation=*/0);
1886 Out << ".";
1887 } else {
1888 APValue Val;
1889 This->moveInto(V&: Val);
1890 Val.printPretty(
1891 OS&: Out, Ctx: Info.Ctx,
1892 Ty: Info.Ctx.getLValueReferenceType(T: This->Designator.MostDerivedType));
1893 Out << ".";
1894 }
1895 Callee->getNameForDiagnostic(OS&: Out, Policy: PrintingPolicy,
1896 /*Qualified=*/false);
1897 }
1898
1899 Out << '(';
1900
1901 llvm::ListSeparator Comma;
1902 for (const ParmVarDecl *Param :
1903 Callee->parameters().slice(N: ExplicitInstanceParam)) {
1904 Out << Comma;
1905 const APValue *V = Info.getParamSlot(Call: Arguments, PVD: Param);
1906 if (V)
1907 V->printPretty(OS&: Out, Ctx: Info.Ctx, Ty: Param->getType());
1908 else
1909 Out << "<...>";
1910 }
1911
1912 Out << ')';
1913}
1914
1915/// Evaluate an expression to see if it had side-effects, and discard its
1916/// result.
1917/// \return \c true if the caller should keep evaluating.
1918static bool EvaluateIgnoredValue(EvalInfo &Info, const Expr *E) {
1919 assert(!E->isValueDependent());
1920 APValue Scratch;
1921 if (!Evaluate(Result&: Scratch, Info, E))
1922 // We don't need the value, but we might have skipped a side effect here.
1923 return Info.noteSideEffect();
1924 return true;
1925}
1926
1927/// Should this call expression be treated as forming an opaque constant?
1928static bool IsOpaqueConstantCall(const CallExpr *E) {
1929 unsigned Builtin = E->getBuiltinCallee();
1930 return (Builtin == Builtin::BI__builtin___CFStringMakeConstantString ||
1931 Builtin == Builtin::BI__builtin___NSStringMakeConstantString ||
1932 Builtin == Builtin::BI__builtin_ptrauth_sign_constant ||
1933 Builtin == Builtin::BI__builtin_function_start);
1934}
1935
1936static bool IsOpaqueConstantCall(const LValue &LVal) {
1937 const auto *BaseExpr =
1938 llvm::dyn_cast_if_present<CallExpr>(Val: LVal.Base.dyn_cast<const Expr *>());
1939 return BaseExpr && IsOpaqueConstantCall(E: BaseExpr);
1940}
1941
1942static bool IsGlobalLValue(APValue::LValueBase B) {
1943 // C++11 [expr.const]p3 An address constant expression is a prvalue core
1944 // constant expression of pointer type that evaluates to...
1945
1946 // ... a null pointer value, or a prvalue core constant expression of type
1947 // std::nullptr_t.
1948 if (!B)
1949 return true;
1950
1951 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
1952 // ... the address of an object with static storage duration,
1953 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
1954 return VD->hasGlobalStorage();
1955 if (isa<TemplateParamObjectDecl>(Val: D))
1956 return true;
1957 // ... the address of a function,
1958 // ... the address of a GUID [MS extension],
1959 // ... the address of an unnamed global constant
1960 return isa<FunctionDecl, MSGuidDecl, UnnamedGlobalConstantDecl>(Val: D);
1961 }
1962
1963 if (B.is<TypeInfoLValue>() || B.is<DynamicAllocLValue>())
1964 return true;
1965
1966 const Expr *E = B.get<const Expr*>();
1967 switch (E->getStmtClass()) {
1968 default:
1969 return false;
1970 case Expr::CompoundLiteralExprClass: {
1971 const CompoundLiteralExpr *CLE = cast<CompoundLiteralExpr>(Val: E);
1972 return CLE->isFileScope() && CLE->isLValue();
1973 }
1974 case Expr::MaterializeTemporaryExprClass:
1975 // A materialized temporary might have been lifetime-extended to static
1976 // storage duration.
1977 return cast<MaterializeTemporaryExpr>(Val: E)->getStorageDuration() == SD_Static;
1978 // A string literal has static storage duration.
1979 case Expr::StringLiteralClass:
1980 case Expr::PredefinedExprClass:
1981 case Expr::ObjCStringLiteralClass:
1982 case Expr::ObjCEncodeExprClass:
1983 return true;
1984 case Expr::ObjCBoxedExprClass:
1985 case Expr::ObjCArrayLiteralClass:
1986 case Expr::ObjCDictionaryLiteralClass:
1987 return cast<ObjCObjectLiteral>(Val: E)->isExpressibleAsConstantInitializer();
1988 case Expr::CallExprClass:
1989 return IsOpaqueConstantCall(E: cast<CallExpr>(Val: E));
1990 // For GCC compatibility, &&label has static storage duration.
1991 case Expr::AddrLabelExprClass:
1992 return true;
1993 // A Block literal expression may be used as the initialization value for
1994 // Block variables at global or local static scope.
1995 case Expr::BlockExprClass:
1996 return !cast<BlockExpr>(Val: E)->getBlockDecl()->hasCaptures();
1997 // The APValue generated from a __builtin_source_location will be emitted as a
1998 // literal.
1999 case Expr::SourceLocExprClass:
2000 return true;
2001 case Expr::ImplicitValueInitExprClass:
2002 // FIXME:
2003 // We can never form an lvalue with an implicit value initialization as its
2004 // base through expression evaluation, so these only appear in one case: the
2005 // implicit variable declaration we invent when checking whether a constexpr
2006 // constructor can produce a constant expression. We must assume that such
2007 // an expression might be a global lvalue.
2008 return true;
2009 }
2010}
2011
2012static const ValueDecl *GetLValueBaseDecl(const LValue &LVal) {
2013 return LVal.Base.dyn_cast<const ValueDecl*>();
2014}
2015
2016// Information about an LValueBase that is some kind of string.
2017struct LValueBaseString {
2018 std::string ObjCEncodeStorage;
2019 StringRef Bytes;
2020 int CharWidth;
2021};
2022
2023// Gets the lvalue base of LVal as a string.
2024static bool GetLValueBaseAsString(const EvalInfo &Info, const LValue &LVal,
2025 LValueBaseString &AsString) {
2026 const auto *BaseExpr = LVal.Base.dyn_cast<const Expr *>();
2027 if (!BaseExpr)
2028 return false;
2029
2030 // For ObjCEncodeExpr, we need to compute and store the string.
2031 if (const auto *EE = dyn_cast<ObjCEncodeExpr>(Val: BaseExpr)) {
2032 Info.Ctx.getObjCEncodingForType(T: EE->getEncodedType(),
2033 S&: AsString.ObjCEncodeStorage);
2034 AsString.Bytes = AsString.ObjCEncodeStorage;
2035 AsString.CharWidth = 1;
2036 return true;
2037 }
2038
2039 // Otherwise, we have a StringLiteral.
2040 const auto *Lit = dyn_cast<StringLiteral>(Val: BaseExpr);
2041 if (const auto *PE = dyn_cast<PredefinedExpr>(Val: BaseExpr))
2042 Lit = PE->getFunctionName();
2043
2044 if (!Lit)
2045 return false;
2046
2047 AsString.Bytes = Lit->getBytes();
2048 AsString.CharWidth = Lit->getCharByteWidth();
2049 return true;
2050}
2051
2052// Determine whether two string literals potentially overlap. This will be the
2053// case if they agree on the values of all the bytes on the overlapping region
2054// between them.
2055//
2056// The overlapping region is the portion of the two string literals that must
2057// overlap in memory if the pointers actually point to the same address at
2058// runtime. For example, if LHS is "abcdef" + 3 and RHS is "cdef\0gh" + 1 then
2059// the overlapping region is "cdef\0", which in this case does agree, so the
2060// strings are potentially overlapping. Conversely, for "foobar" + 3 versus
2061// "bazbar" + 3, the overlapping region contains all of both strings, so they
2062// are not potentially overlapping, even though they agree from the given
2063// addresses onwards.
2064//
2065// See open core issue CWG2765 which is discussing the desired rule here.
2066static bool ArePotentiallyOverlappingStringLiterals(const EvalInfo &Info,
2067 const LValue &LHS,
2068 const LValue &RHS) {
2069 LValueBaseString LHSString, RHSString;
2070 if (!GetLValueBaseAsString(Info, LVal: LHS, AsString&: LHSString) ||
2071 !GetLValueBaseAsString(Info, LVal: RHS, AsString&: RHSString))
2072 return false;
2073
2074 // This is the byte offset to the location of the first character of LHS
2075 // within RHS. We don't need to look at the characters of one string that
2076 // would appear before the start of the other string if they were merged.
2077 CharUnits Offset = RHS.Offset - LHS.Offset;
2078 if (Offset.isNegative()) {
2079 if (LHSString.Bytes.size() < (size_t)-Offset.getQuantity())
2080 return false;
2081 LHSString.Bytes = LHSString.Bytes.drop_front(N: -Offset.getQuantity());
2082 } else {
2083 if (RHSString.Bytes.size() < (size_t)Offset.getQuantity())
2084 return false;
2085 RHSString.Bytes = RHSString.Bytes.drop_front(N: Offset.getQuantity());
2086 }
2087
2088 bool LHSIsLonger = LHSString.Bytes.size() > RHSString.Bytes.size();
2089 StringRef Longer = LHSIsLonger ? LHSString.Bytes : RHSString.Bytes;
2090 StringRef Shorter = LHSIsLonger ? RHSString.Bytes : LHSString.Bytes;
2091 int ShorterCharWidth = (LHSIsLonger ? RHSString : LHSString).CharWidth;
2092
2093 // The null terminator isn't included in the string data, so check for it
2094 // manually. If the longer string doesn't have a null terminator where the
2095 // shorter string ends, they aren't potentially overlapping.
2096 for (int NullByte : llvm::seq(Size: ShorterCharWidth)) {
2097 if (Shorter.size() + NullByte >= Longer.size())
2098 break;
2099 if (Longer[Shorter.size() + NullByte])
2100 return false;
2101 }
2102
2103 // Otherwise, they're potentially overlapping if and only if the overlapping
2104 // region is the same.
2105 return Shorter == Longer.take_front(N: Shorter.size());
2106}
2107
2108static bool IsWeakLValue(const LValue &Value) {
2109 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2110 return Decl && Decl->isWeak();
2111}
2112
2113static bool isZeroSized(const LValue &Value) {
2114 const ValueDecl *Decl = GetLValueBaseDecl(LVal: Value);
2115 if (isa_and_nonnull<VarDecl>(Val: Decl)) {
2116 QualType Ty = Decl->getType();
2117 if (Ty->isArrayType())
2118 return Ty->isIncompleteType() ||
2119 Decl->getASTContext().getTypeSize(T: Ty) == 0;
2120 }
2121 return false;
2122}
2123
2124static bool HasSameBase(const LValue &A, const LValue &B) {
2125 if (!A.getLValueBase())
2126 return !B.getLValueBase();
2127 if (!B.getLValueBase())
2128 return false;
2129
2130 if (A.getLValueBase().getOpaqueValue() !=
2131 B.getLValueBase().getOpaqueValue())
2132 return false;
2133
2134 return A.getLValueCallIndex() == B.getLValueCallIndex() &&
2135 A.getLValueVersion() == B.getLValueVersion();
2136}
2137
2138static void NoteLValueLocation(EvalInfo &Info, APValue::LValueBase Base) {
2139 assert(Base && "no location for a null lvalue");
2140 const ValueDecl *VD = Base.dyn_cast<const ValueDecl*>();
2141
2142 // For a parameter, find the corresponding call stack frame (if it still
2143 // exists), and point at the parameter of the function definition we actually
2144 // invoked.
2145 if (auto *PVD = dyn_cast_or_null<ParmVarDecl>(Val: VD)) {
2146 unsigned Idx = PVD->getFunctionScopeIndex();
2147 for (CallStackFrame *F = Info.CurrentCall; F; F = F->Caller) {
2148 if (F->Arguments.CallIndex == Base.getCallIndex() &&
2149 F->Arguments.Version == Base.getVersion() && F->Callee &&
2150 Idx < F->Callee->getNumParams()) {
2151 VD = F->Callee->getParamDecl(i: Idx);
2152 break;
2153 }
2154 }
2155 }
2156
2157 if (VD)
2158 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
2159 else if (const Expr *E = Base.dyn_cast<const Expr*>())
2160 Info.Note(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
2161 else if (DynamicAllocLValue DA = Base.dyn_cast<DynamicAllocLValue>()) {
2162 // FIXME: Produce a note for dangling pointers too.
2163 if (std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA))
2164 Info.Note(Loc: (*Alloc)->AllocExpr->getExprLoc(),
2165 DiagId: diag::note_constexpr_dynamic_alloc_here);
2166 }
2167
2168 // We have no information to show for a typeid(T) object.
2169}
2170
2171enum class CheckEvaluationResultKind {
2172 ConstantExpression,
2173 FullyInitialized,
2174};
2175
2176/// Materialized temporaries that we've already checked to determine if they're
2177/// initializsed by a constant expression.
2178using CheckedTemporaries =
2179 llvm::SmallPtrSet<const MaterializeTemporaryExpr *, 8>;
2180
2181static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2182 EvalInfo &Info, SourceLocation DiagLoc,
2183 QualType Type, const APValue &Value,
2184 ConstantExprKind Kind,
2185 const FieldDecl *SubobjectDecl,
2186 CheckedTemporaries &CheckedTemps);
2187
2188/// Check that this reference or pointer core constant expression is a valid
2189/// value for an address or reference constant expression. Return true if we
2190/// can fold this expression, whether or not it's a constant expression.
2191static bool CheckLValueConstantExpression(EvalInfo &Info, SourceLocation Loc,
2192 QualType Type, const LValue &LVal,
2193 ConstantExprKind Kind,
2194 CheckedTemporaries &CheckedTemps) {
2195 bool IsReferenceType = Type->isReferenceType();
2196
2197 APValue::LValueBase Base = LVal.getLValueBase();
2198 const SubobjectDesignator &Designator = LVal.getLValueDesignator();
2199
2200 const Expr *BaseE = Base.dyn_cast<const Expr *>();
2201 const ValueDecl *BaseVD = Base.dyn_cast<const ValueDecl*>();
2202
2203 // Additional restrictions apply in a template argument. We only enforce the
2204 // C++20 restrictions here; additional syntactic and semantic restrictions
2205 // are applied elsewhere.
2206 if (isTemplateArgument(Kind)) {
2207 int InvalidBaseKind = -1;
2208 StringRef Ident;
2209 if (Base.is<TypeInfoLValue>())
2210 InvalidBaseKind = 0;
2211 else if (isa_and_nonnull<StringLiteral>(Val: BaseE))
2212 InvalidBaseKind = 1;
2213 else if (isa_and_nonnull<MaterializeTemporaryExpr>(Val: BaseE) ||
2214 isa_and_nonnull<LifetimeExtendedTemporaryDecl>(Val: BaseVD))
2215 InvalidBaseKind = 2;
2216 else if (auto *PE = dyn_cast_or_null<PredefinedExpr>(Val: BaseE)) {
2217 InvalidBaseKind = 3;
2218 Ident = PE->getIdentKindName();
2219 }
2220
2221 if (InvalidBaseKind != -1) {
2222 Info.FFDiag(Loc, DiagId: diag::note_constexpr_invalid_template_arg)
2223 << IsReferenceType << !Designator.Entries.empty() << InvalidBaseKind
2224 << Ident;
2225 return false;
2226 }
2227 }
2228
2229 if (auto *FD = dyn_cast_or_null<FunctionDecl>(Val: BaseVD);
2230 FD && FD->isImmediateFunction()) {
2231 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible)
2232 << !Type->isAnyPointerType();
2233 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2234 return false;
2235 }
2236
2237 // Check that the object is a global. Note that the fake 'this' object we
2238 // manufacture when checking potential constant expressions is conservatively
2239 // assumed to be global here.
2240 if (!IsGlobalLValue(B: Base)) {
2241 if (Info.getLangOpts().CPlusPlus11) {
2242 Info.FFDiag(Loc, DiagId: diag::note_constexpr_non_global, ExtraNotes: 1)
2243 << IsReferenceType << !Designator.Entries.empty() << !!BaseVD
2244 << BaseVD;
2245 auto *VarD = dyn_cast_or_null<VarDecl>(Val: BaseVD);
2246 if (VarD && VarD->isConstexpr()) {
2247 // Non-static local constexpr variables have unintuitive semantics:
2248 // constexpr int a = 1;
2249 // constexpr const int *p = &a;
2250 // ... is invalid because the address of 'a' is not constant. Suggest
2251 // adding a 'static' in this case.
2252 Info.Note(Loc: VarD->getLocation(), DiagId: diag::note_constexpr_not_static)
2253 << VarD
2254 << FixItHint::CreateInsertion(InsertionLoc: VarD->getBeginLoc(), Code: "static ");
2255 } else {
2256 NoteLValueLocation(Info, Base);
2257 }
2258 } else {
2259 Info.FFDiag(Loc);
2260 }
2261 // Don't allow references to temporaries to escape.
2262 return false;
2263 }
2264 assert((Info.checkingPotentialConstantExpression() ||
2265 LVal.getLValueCallIndex() == 0) &&
2266 "have call index for global lvalue");
2267
2268 if (LVal.allowConstexprUnknown()) {
2269 if (BaseVD) {
2270 Info.FFDiag(Loc, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << BaseVD;
2271 NoteLValueLocation(Info, Base);
2272 } else {
2273 Info.FFDiag(Loc);
2274 }
2275 return false;
2276 }
2277
2278 if (Base.is<DynamicAllocLValue>()) {
2279 Info.FFDiag(Loc, DiagId: diag::note_constexpr_dynamic_alloc)
2280 << IsReferenceType << !Designator.Entries.empty();
2281 NoteLValueLocation(Info, Base);
2282 return false;
2283 }
2284
2285 if (BaseVD) {
2286 if (const VarDecl *Var = dyn_cast<const VarDecl>(Val: BaseVD)) {
2287 // Check if this is a thread-local variable.
2288 if (Var->getTLSKind())
2289 // FIXME: Diagnostic!
2290 return false;
2291
2292 // A dllimport variable never acts like a constant, unless we're
2293 // evaluating a value for use only in name mangling, and unless it's a
2294 // static local. For the latter case, we'd still need to evaluate the
2295 // constant expression in case we're inside a (inlined) function.
2296 if (!isForManglingOnly(Kind) && Var->hasAttr<DLLImportAttr>() &&
2297 !Var->isStaticLocal())
2298 return false;
2299
2300 // In CUDA/HIP device compilation, only device side variables have
2301 // constant addresses.
2302 if (Info.getLangOpts().CUDA && Info.getLangOpts().CUDAIsDevice &&
2303 Info.Ctx.CUDAConstantEvalCtx.NoWrongSidedVars) {
2304 if ((!Var->hasAttr<CUDADeviceAttr>() &&
2305 !Var->hasAttr<CUDAConstantAttr>() &&
2306 !Var->getType()->isCUDADeviceBuiltinSurfaceType() &&
2307 !Var->getType()->isCUDADeviceBuiltinTextureType()) ||
2308 Var->hasAttr<HIPManagedAttr>())
2309 return false;
2310 }
2311 }
2312 if (const auto *FD = dyn_cast<const FunctionDecl>(Val: BaseVD)) {
2313 // __declspec(dllimport) must be handled very carefully:
2314 // We must never initialize an expression with the thunk in C++.
2315 // Doing otherwise would allow the same id-expression to yield
2316 // different addresses for the same function in different translation
2317 // units. However, this means that we must dynamically initialize the
2318 // expression with the contents of the import address table at runtime.
2319 //
2320 // The C language has no notion of ODR; furthermore, it has no notion of
2321 // dynamic initialization. This means that we are permitted to
2322 // perform initialization with the address of the thunk.
2323 if (Info.getLangOpts().CPlusPlus && !isForManglingOnly(Kind) &&
2324 FD->hasAttr<DLLImportAttr>())
2325 // FIXME: Diagnostic!
2326 return false;
2327 }
2328 } else if (const auto *MTE =
2329 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: BaseE)) {
2330 if (CheckedTemps.insert(Ptr: MTE).second) {
2331 QualType TempType = getType(B: Base);
2332 if (TempType.isDestructedType()) {
2333 Info.FFDiag(Loc: MTE->getExprLoc(),
2334 DiagId: diag::note_constexpr_unsupported_temporary_nontrivial_dtor)
2335 << TempType;
2336 return false;
2337 }
2338
2339 APValue *V = MTE->getOrCreateValue(MayCreate: false);
2340 assert(V && "evasluation result refers to uninitialised temporary");
2341 if (!CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2342 Info, DiagLoc: MTE->getExprLoc(), Type: TempType, Value: *V, Kind,
2343 /*SubobjectDecl=*/nullptr, CheckedTemps))
2344 return false;
2345 }
2346 }
2347
2348 // Allow address constant expressions to be past-the-end pointers. This is
2349 // an extension: the standard requires them to point to an object.
2350 if (!IsReferenceType)
2351 return true;
2352
2353 // A reference constant expression must refer to an object.
2354 if (!Base) {
2355 // FIXME: diagnostic
2356 Info.CCEDiag(Loc);
2357 return true;
2358 }
2359
2360 // Does this refer one past the end of some object?
2361 if (!Designator.Invalid && Designator.isOnePastTheEnd()) {
2362 Info.FFDiag(Loc, DiagId: diag::note_constexpr_past_end, ExtraNotes: 1)
2363 << !Designator.Entries.empty() << !!BaseVD << BaseVD;
2364 NoteLValueLocation(Info, Base);
2365 }
2366
2367 return true;
2368}
2369
2370/// Member pointers are constant expressions unless they point to a
2371/// non-virtual dllimport member function.
2372static bool CheckMemberPointerConstantExpression(EvalInfo &Info,
2373 SourceLocation Loc,
2374 QualType Type,
2375 const APValue &Value,
2376 ConstantExprKind Kind) {
2377 const ValueDecl *Member = Value.getMemberPointerDecl();
2378 const auto *FD = dyn_cast_or_null<CXXMethodDecl>(Val: Member);
2379 if (!FD)
2380 return true;
2381 if (FD->isImmediateFunction()) {
2382 Info.FFDiag(Loc, DiagId: diag::note_consteval_address_accessible) << /*pointer*/ 0;
2383 Info.Note(Loc: FD->getLocation(), DiagId: diag::note_declared_at);
2384 return false;
2385 }
2386 return isForManglingOnly(Kind) || FD->isVirtual() ||
2387 !FD->hasAttr<DLLImportAttr>();
2388}
2389
2390/// Check that this core constant expression is of literal type, and if not,
2391/// produce an appropriate diagnostic.
2392static bool CheckLiteralType(EvalInfo &Info, const Expr *E,
2393 const LValue *This = nullptr) {
2394 // The restriction to literal types does not exist in C++23 anymore.
2395 if (Info.getLangOpts().CPlusPlus23)
2396 return true;
2397
2398 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx: Info.Ctx))
2399 return true;
2400
2401 // C++1y: A constant initializer for an object o [...] may also invoke
2402 // constexpr constructors for o and its subobjects even if those objects
2403 // are of non-literal class types.
2404 //
2405 // C++11 missed this detail for aggregates, so classes like this:
2406 // struct foo_t { union { int i; volatile int j; } u; };
2407 // are not (obviously) initializable like so:
2408 // __attribute__((__require_constant_initialization__))
2409 // static const foo_t x = {{0}};
2410 // because "i" is a subobject with non-literal initialization (due to the
2411 // volatile member of the union). See:
2412 // http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_active.html#1677
2413 // Therefore, we use the C++1y behavior.
2414 if (This && Info.EvaluatingDecl == This->getLValueBase())
2415 return true;
2416
2417 // Prvalue constant expressions must be of literal types.
2418 if (Info.getLangOpts().CPlusPlus11)
2419 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral)
2420 << E->getType();
2421 else
2422 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
2423 return false;
2424}
2425
2426static bool CheckEvaluationResult(CheckEvaluationResultKind CERK,
2427 EvalInfo &Info, SourceLocation DiagLoc,
2428 QualType Type, const APValue &Value,
2429 ConstantExprKind Kind,
2430 const FieldDecl *SubobjectDecl,
2431 CheckedTemporaries &CheckedTemps) {
2432 if (!Value.hasValue()) {
2433 if (SubobjectDecl) {
2434 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2435 << /*(name)*/ 1 << SubobjectDecl;
2436 Info.Note(Loc: SubobjectDecl->getLocation(),
2437 DiagId: diag::note_constexpr_subobject_declared_here);
2438 } else {
2439 Info.FFDiag(Loc: DiagLoc, DiagId: diag::note_constexpr_uninitialized)
2440 << /*of type*/ 0 << Type;
2441 }
2442 return false;
2443 }
2444
2445 // We allow _Atomic(T) to be initialized from anything that T can be
2446 // initialized from.
2447 if (const AtomicType *AT = Type->getAs<AtomicType>())
2448 Type = AT->getValueType();
2449
2450 // Core issue 1454: For a literal constant expression of array or class type,
2451 // each subobject of its value shall have been initialized by a constant
2452 // expression.
2453 if (Value.isArray()) {
2454 QualType EltTy = Type->castAsArrayTypeUnsafe()->getElementType();
2455 for (unsigned I = 0, N = Value.getArrayInitializedElts(); I != N; ++I) {
2456 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2457 Value: Value.getArrayInitializedElt(I), Kind,
2458 SubobjectDecl, CheckedTemps))
2459 return false;
2460 }
2461 if (!Value.hasArrayFiller())
2462 return true;
2463 return CheckEvaluationResult(CERK, Info, DiagLoc, Type: EltTy,
2464 Value: Value.getArrayFiller(), Kind, SubobjectDecl,
2465 CheckedTemps);
2466 }
2467 if (Value.isUnion() && Value.getUnionField()) {
2468 return CheckEvaluationResult(
2469 CERK, Info, DiagLoc, Type: Value.getUnionField()->getType(),
2470 Value: Value.getUnionValue(), Kind, SubobjectDecl: Value.getUnionField(), CheckedTemps);
2471 }
2472 if (Value.isStruct()) {
2473 auto *RD = Type->castAsRecordDecl();
2474 if (const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD)) {
2475 unsigned BaseIndex = 0;
2476 for (const CXXBaseSpecifier &BS : CD->bases()) {
2477 const APValue &BaseValue = Value.getStructBase(i: BaseIndex);
2478 if (!BaseValue.hasValue()) {
2479 SourceLocation TypeBeginLoc = BS.getBaseTypeLoc();
2480 Info.FFDiag(Loc: TypeBeginLoc, DiagId: diag::note_constexpr_uninitialized_base)
2481 << BS.getType() << SourceRange(TypeBeginLoc, BS.getEndLoc());
2482 return false;
2483 }
2484 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: BS.getType(), Value: BaseValue,
2485 Kind, /*SubobjectDecl=*/nullptr,
2486 CheckedTemps))
2487 return false;
2488 ++BaseIndex;
2489 }
2490 }
2491 for (const auto *I : RD->fields()) {
2492 if (I->isUnnamedBitField())
2493 continue;
2494
2495 if (!CheckEvaluationResult(CERK, Info, DiagLoc, Type: I->getType(),
2496 Value: Value.getStructField(i: I->getFieldIndex()), Kind,
2497 SubobjectDecl: I, CheckedTemps))
2498 return false;
2499 }
2500 }
2501
2502 if (Value.isLValue() &&
2503 CERK == CheckEvaluationResultKind::ConstantExpression) {
2504 LValue LVal;
2505 LVal.setFrom(Ctx: Info.Ctx, V: Value);
2506 return CheckLValueConstantExpression(Info, Loc: DiagLoc, Type, LVal, Kind,
2507 CheckedTemps);
2508 }
2509
2510 if (Value.isMemberPointer() &&
2511 CERK == CheckEvaluationResultKind::ConstantExpression)
2512 return CheckMemberPointerConstantExpression(Info, Loc: DiagLoc, Type, Value, Kind);
2513
2514 // Everything else is fine.
2515 return true;
2516}
2517
2518/// Check that this core constant expression value is a valid value for a
2519/// constant expression. If not, report an appropriate diagnostic. Does not
2520/// check that the expression is of literal type.
2521static bool CheckConstantExpression(EvalInfo &Info, SourceLocation DiagLoc,
2522 QualType Type, const APValue &Value,
2523 ConstantExprKind Kind) {
2524 // Nothing to check for a constant expression of type 'cv void'.
2525 if (Type->isVoidType())
2526 return true;
2527
2528 CheckedTemporaries CheckedTemps;
2529 return CheckEvaluationResult(CERK: CheckEvaluationResultKind::ConstantExpression,
2530 Info, DiagLoc, Type, Value, Kind,
2531 /*SubobjectDecl=*/nullptr, CheckedTemps);
2532}
2533
2534/// Check that this evaluated value is fully-initialized and can be loaded by
2535/// an lvalue-to-rvalue conversion.
2536static bool CheckFullyInitialized(EvalInfo &Info, SourceLocation DiagLoc,
2537 QualType Type, const APValue &Value) {
2538 CheckedTemporaries CheckedTemps;
2539 return CheckEvaluationResult(
2540 CERK: CheckEvaluationResultKind::FullyInitialized, Info, DiagLoc, Type, Value,
2541 Kind: ConstantExprKind::Normal, /*SubobjectDecl=*/nullptr, CheckedTemps);
2542}
2543
2544/// Enforce C++2a [expr.const]/4.17, which disallows new-expressions unless
2545/// "the allocated storage is deallocated within the evaluation".
2546static bool CheckMemoryLeaks(EvalInfo &Info) {
2547 if (!Info.HeapAllocs.empty()) {
2548 // We can still fold to a constant despite a compile-time memory leak,
2549 // so long as the heap allocation isn't referenced in the result (we check
2550 // that in CheckConstantExpression).
2551 Info.CCEDiag(E: Info.HeapAllocs.begin()->second.AllocExpr,
2552 DiagId: diag::note_constexpr_memory_leak)
2553 << unsigned(Info.HeapAllocs.size() - 1);
2554 }
2555 return true;
2556}
2557
2558static bool EvalPointerValueAsBool(const APValue &Value, bool &Result) {
2559 // A null base expression indicates a null pointer. These are always
2560 // evaluatable, and they are false unless the offset is zero.
2561 if (!Value.getLValueBase()) {
2562 // TODO: Should a non-null pointer with an offset of zero evaluate to true?
2563 Result = !Value.getLValueOffset().isZero();
2564 return true;
2565 }
2566
2567 // We have a non-null base. These are generally known to be true, but if it's
2568 // a weak declaration it can be null at runtime.
2569 Result = true;
2570 const ValueDecl *Decl = Value.getLValueBase().dyn_cast<const ValueDecl*>();
2571 return !Decl || !Decl->isWeak();
2572}
2573
2574static bool HandleConversionToBool(const APValue &Val, bool &Result) {
2575 // TODO: This function should produce notes if it fails.
2576 switch (Val.getKind()) {
2577 case APValue::None:
2578 case APValue::Indeterminate:
2579 return false;
2580 case APValue::Int:
2581 Result = Val.getInt().getBoolValue();
2582 return true;
2583 case APValue::FixedPoint:
2584 Result = Val.getFixedPoint().getBoolValue();
2585 return true;
2586 case APValue::Float:
2587 Result = !Val.getFloat().isZero();
2588 return true;
2589 case APValue::ComplexInt:
2590 Result = Val.getComplexIntReal().getBoolValue() ||
2591 Val.getComplexIntImag().getBoolValue();
2592 return true;
2593 case APValue::ComplexFloat:
2594 Result = !Val.getComplexFloatReal().isZero() ||
2595 !Val.getComplexFloatImag().isZero();
2596 return true;
2597 case APValue::LValue:
2598 return EvalPointerValueAsBool(Value: Val, Result);
2599 case APValue::MemberPointer:
2600 if (Val.getMemberPointerDecl() && Val.getMemberPointerDecl()->isWeak()) {
2601 return false;
2602 }
2603 Result = Val.getMemberPointerDecl();
2604 return true;
2605 case APValue::Vector:
2606 case APValue::Matrix:
2607 case APValue::Array:
2608 case APValue::Struct:
2609 case APValue::Union:
2610 case APValue::AddrLabelDiff:
2611 return false;
2612 }
2613
2614 llvm_unreachable("unknown APValue kind");
2615}
2616
2617static bool EvaluateAsBooleanCondition(const Expr *E, bool &Result,
2618 EvalInfo &Info) {
2619 assert(!E->isValueDependent());
2620 assert(E->isPRValue() && "missing lvalue-to-rvalue conv in bool condition");
2621 APValue Val;
2622 if (!Evaluate(Result&: Val, Info, E))
2623 return false;
2624 return HandleConversionToBool(Val, Result);
2625}
2626
2627template<typename T>
2628static bool HandleOverflow(EvalInfo &Info, const Expr *E,
2629 const T &SrcValue, QualType DestType) {
2630 Info.CCEDiag(E, DiagId: diag::note_constexpr_overflow) << SrcValue << DestType;
2631 if (const auto *OBT = DestType->getAs<OverflowBehaviorType>();
2632 OBT && OBT->isTrapKind()) {
2633 return false;
2634 }
2635 return Info.noteUndefinedBehavior();
2636}
2637
2638static bool HandleFloatToIntCast(EvalInfo &Info, const Expr *E,
2639 QualType SrcType, const APFloat &Value,
2640 QualType DestType, APSInt &Result) {
2641 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2642 // Determine whether we are converting to unsigned or signed.
2643 bool DestSigned = DestType->isSignedIntegerOrEnumerationType();
2644
2645 Result = APSInt(DestWidth, !DestSigned);
2646 bool ignored;
2647 if (Value.convertToInteger(Result, RM: llvm::APFloat::rmTowardZero, IsExact: &ignored)
2648 & APFloat::opInvalidOp)
2649 return HandleOverflow(Info, E, SrcValue: Value, DestType);
2650 return true;
2651}
2652
2653/// Get rounding mode to use in evaluation of the specified expression.
2654///
2655/// If rounding mode is unknown at compile time, still try to evaluate the
2656/// expression. If the result is exact, it does not depend on rounding mode.
2657/// So return "tonearest" mode instead of "dynamic".
2658static llvm::RoundingMode getActiveRoundingMode(EvalInfo &Info, const Expr *E) {
2659 llvm::RoundingMode RM =
2660 E->getFPFeaturesInEffect(LO: Info.getLangOpts()).getRoundingMode();
2661 if (RM == llvm::RoundingMode::Dynamic)
2662 RM = llvm::RoundingMode::NearestTiesToEven;
2663 return RM;
2664}
2665
2666/// Check if the given evaluation result is allowed for constant evaluation.
2667static bool checkFloatingPointResult(EvalInfo &Info, const Expr *E,
2668 APFloat::opStatus St) {
2669 // In a constant context, assume that any dynamic rounding mode or FP
2670 // exception state matches the default floating-point environment.
2671 if (Info.InConstantContext)
2672 return true;
2673
2674 FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.getLangOpts());
2675 if ((St & APFloat::opInexact) &&
2676 FPO.getRoundingMode() == llvm::RoundingMode::Dynamic) {
2677 // Inexact result means that it depends on rounding mode. If the requested
2678 // mode is dynamic, the evaluation cannot be made in compile time.
2679 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
2680 return false;
2681 }
2682
2683 if ((St != APFloat::opOK) &&
2684 (FPO.getRoundingMode() == llvm::RoundingMode::Dynamic ||
2685 FPO.getExceptionMode() != LangOptions::FPE_Ignore ||
2686 FPO.getAllowFEnvAccess())) {
2687 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
2688 return false;
2689 }
2690
2691 if ((St & APFloat::opStatus::opInvalidOp) &&
2692 FPO.getExceptionMode() != LangOptions::FPE_Ignore) {
2693 // There is no usefully definable result.
2694 Info.FFDiag(E);
2695 return false;
2696 }
2697
2698 // FIXME: if:
2699 // - evaluation triggered other FP exception, and
2700 // - exception mode is not "ignore", and
2701 // - the expression being evaluated is not a part of global variable
2702 // initializer,
2703 // the evaluation probably need to be rejected.
2704 return true;
2705}
2706
2707static bool HandleFloatToFloatCast(EvalInfo &Info, const Expr *E,
2708 QualType SrcType, QualType DestType,
2709 APFloat &Result) {
2710 assert((isa<CastExpr>(E) || isa<CompoundAssignOperator>(E) ||
2711 isa<ConvertVectorExpr>(E)) &&
2712 "HandleFloatToFloatCast has been checked with only CastExpr, "
2713 "CompoundAssignOperator and ConvertVectorExpr. Please either validate "
2714 "the new expression or address the root cause of this usage.");
2715 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2716 APFloat::opStatus St;
2717 APFloat Value = Result;
2718 bool ignored;
2719 St = Result.convert(ToSemantics: Info.Ctx.getFloatTypeSemantics(T: DestType), RM, losesInfo: &ignored);
2720 return checkFloatingPointResult(Info, E, St);
2721}
2722
2723static APSInt HandleIntToIntCast(EvalInfo &Info, const Expr *E,
2724 QualType DestType, QualType SrcType,
2725 const APSInt &Value) {
2726 unsigned DestWidth = Info.Ctx.getIntWidth(T: DestType);
2727 // Figure out if this is a truncate, extend or noop cast.
2728 // If the input is signed, do a sign extend, noop, or truncate.
2729 APSInt Result = Value.extOrTrunc(width: DestWidth);
2730 Result.setIsUnsigned(DestType->isUnsignedIntegerOrEnumerationType());
2731 if (DestType->isBooleanType())
2732 Result = Value.getBoolValue();
2733 return Result;
2734}
2735
2736static bool HandleIntToFloatCast(EvalInfo &Info, const Expr *E,
2737 const FPOptions FPO,
2738 QualType SrcType, const APSInt &Value,
2739 QualType DestType, APFloat &Result) {
2740 Result = APFloat(Info.Ctx.getFloatTypeSemantics(T: DestType), 1);
2741 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2742 APFloat::opStatus St = Result.convertFromAPInt(Input: Value, IsSigned: Value.isSigned(), RM);
2743 return checkFloatingPointResult(Info, E, St);
2744}
2745
2746static bool truncateBitfieldValue(EvalInfo &Info, const Expr *E,
2747 APValue &Value, const FieldDecl *FD) {
2748 assert(FD->isBitField() && "truncateBitfieldValue on non-bitfield");
2749
2750 if (!Value.isInt()) {
2751 // Trying to store a pointer-cast-to-integer into a bitfield.
2752 // FIXME: In this case, we should provide the diagnostic for casting
2753 // a pointer to an integer.
2754 assert(Value.isLValue() && "integral value neither int nor lvalue?");
2755 Info.FFDiag(E);
2756 return false;
2757 }
2758
2759 APSInt &Int = Value.getInt();
2760 unsigned OldBitWidth = Int.getBitWidth();
2761 unsigned NewBitWidth = FD->getBitWidthValue();
2762 if (NewBitWidth < OldBitWidth)
2763 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
2764 return true;
2765}
2766
2767/// Perform the given integer operation, which is known to need at most BitWidth
2768/// bits, and check for overflow in the original type (if that type was not an
2769/// unsigned type).
2770template<typename Operation>
2771static bool CheckedIntArithmetic(EvalInfo &Info, const Expr *E,
2772 const APSInt &LHS, const APSInt &RHS,
2773 unsigned BitWidth, Operation Op,
2774 APSInt &Result) {
2775 if (LHS.isUnsigned()) {
2776 Result = Op(LHS, RHS);
2777 return true;
2778 }
2779
2780 APSInt Value(Op(LHS.extend(width: BitWidth), RHS.extend(width: BitWidth)), false);
2781 Result = Value.trunc(width: LHS.getBitWidth());
2782 if (Result.extend(width: BitWidth) != Value && !E->getType().isWrapType()) {
2783 if (Info.checkingForUndefinedBehavior())
2784 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
2785 DiagID: diag::warn_integer_constant_overflow)
2786 << toString(I: Result, Radix: 10, Signed: Result.isSigned(), /*formatAsCLiteral=*/false,
2787 /*UpperCase=*/true, /*InsertSeparators=*/true)
2788 << E->getType() << E->getSourceRange();
2789 return HandleOverflow(Info, E, SrcValue: Value, DestType: E->getType());
2790 }
2791 return true;
2792}
2793
2794/// Perform the given binary integer operation.
2795static bool handleIntIntBinOp(EvalInfo &Info, const BinaryOperator *E,
2796 const APSInt &LHS, BinaryOperatorKind Opcode,
2797 APSInt RHS, APSInt &Result) {
2798 bool HandleOverflowResult = true;
2799 switch (Opcode) {
2800 default:
2801 Info.FFDiag(E);
2802 return false;
2803 case BO_Mul:
2804 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() * 2,
2805 Op: std::multiplies<APSInt>(), Result);
2806 case BO_Add:
2807 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2808 Op: std::plus<APSInt>(), Result);
2809 case BO_Sub:
2810 return CheckedIntArithmetic(Info, E, LHS, RHS, BitWidth: LHS.getBitWidth() + 1,
2811 Op: std::minus<APSInt>(), Result);
2812 case BO_And: Result = LHS & RHS; return true;
2813 case BO_Xor: Result = LHS ^ RHS; return true;
2814 case BO_Or: Result = LHS | RHS; return true;
2815 case BO_Div:
2816 case BO_Rem:
2817 if (RHS == 0) {
2818 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero)
2819 << E->getRHS()->getSourceRange();
2820 return false;
2821 }
2822 // Check for overflow case: INT_MIN / -1 or INT_MIN % -1. APSInt supports
2823 // this operation and gives the two's complement result.
2824 if (RHS.isNegative() && RHS.isAllOnes() && LHS.isSigned() &&
2825 LHS.isMinSignedValue())
2826 HandleOverflowResult = HandleOverflow(
2827 Info, E, SrcValue: -LHS.extend(width: LHS.getBitWidth() + 1), DestType: E->getType());
2828 Result = (Opcode == BO_Rem ? LHS % RHS : LHS / RHS);
2829 return HandleOverflowResult;
2830 case BO_Shl: {
2831 if (Info.getLangOpts().OpenCL)
2832 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2833 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2834 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2835 RHS.isUnsigned());
2836 else if (RHS.isSigned() && RHS.isNegative()) {
2837 // During constant-folding, a negative shift is an opposite shift. Such
2838 // a shift is not a constant expression.
2839 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2840 if (!Info.noteUndefinedBehavior())
2841 return false;
2842 RHS = -RHS;
2843 goto shift_right;
2844 }
2845 shift_left:
2846 // C++11 [expr.shift]p1: Shift width must be less than the bit width of
2847 // the shifted type.
2848 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2849 if (SA != RHS) {
2850 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2851 << RHS << E->getType() << LHS.getBitWidth();
2852 if (!Info.noteUndefinedBehavior())
2853 return false;
2854 } else if (LHS.isSigned() && !Info.getLangOpts().CPlusPlus20) {
2855 // C++11 [expr.shift]p2: A signed left shift must have a non-negative
2856 // operand, and must not overflow the corresponding unsigned type.
2857 // C++2a [expr.shift]p2: E1 << E2 is the unique value congruent to
2858 // E1 x 2^E2 module 2^N.
2859 if (LHS.isNegative()) {
2860 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_of_negative) << LHS;
2861 if (!Info.noteUndefinedBehavior())
2862 return false;
2863 } else if (LHS.countl_zero() < SA) {
2864 Info.CCEDiag(E, DiagId: diag::note_constexpr_lshift_discards);
2865 if (!Info.noteUndefinedBehavior())
2866 return false;
2867 }
2868 }
2869 Result = LHS << SA;
2870 return true;
2871 }
2872 case BO_Shr: {
2873 if (Info.getLangOpts().OpenCL)
2874 // OpenCL 6.3j: shift values are effectively % word size of LHS.
2875 RHS &= APSInt(llvm::APInt(RHS.getBitWidth(),
2876 static_cast<uint64_t>(LHS.getBitWidth() - 1)),
2877 RHS.isUnsigned());
2878 else if (RHS.isSigned() && RHS.isNegative()) {
2879 // During constant-folding, a negative shift is an opposite shift. Such a
2880 // shift is not a constant expression.
2881 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHS;
2882 if (!Info.noteUndefinedBehavior())
2883 return false;
2884 RHS = -RHS;
2885 goto shift_left;
2886 }
2887 shift_right:
2888 // C++11 [expr.shift]p1: Shift width must be less than the bit width of the
2889 // shifted type.
2890 unsigned SA = (unsigned) RHS.getLimitedValue(Limit: LHS.getBitWidth()-1);
2891 if (SA != RHS) {
2892 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
2893 << RHS << E->getType() << LHS.getBitWidth();
2894 if (!Info.noteUndefinedBehavior())
2895 return false;
2896 }
2897
2898 Result = LHS >> SA;
2899 return true;
2900 }
2901
2902 case BO_LT: Result = LHS < RHS; return true;
2903 case BO_GT: Result = LHS > RHS; return true;
2904 case BO_LE: Result = LHS <= RHS; return true;
2905 case BO_GE: Result = LHS >= RHS; return true;
2906 case BO_EQ: Result = LHS == RHS; return true;
2907 case BO_NE: Result = LHS != RHS; return true;
2908 case BO_Cmp:
2909 llvm_unreachable("BO_Cmp should be handled elsewhere");
2910 }
2911}
2912
2913/// Perform the given binary floating-point operation, in-place, on LHS.
2914static bool handleFloatFloatBinOp(EvalInfo &Info, const BinaryOperator *E,
2915 APFloat &LHS, BinaryOperatorKind Opcode,
2916 const APFloat &RHS) {
2917 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
2918 APFloat::opStatus St;
2919 switch (Opcode) {
2920 default:
2921 Info.FFDiag(E);
2922 return false;
2923 case BO_Mul:
2924 St = LHS.multiply(RHS, RM);
2925 break;
2926 case BO_Add:
2927 St = LHS.add(RHS, RM);
2928 break;
2929 case BO_Sub:
2930 St = LHS.subtract(RHS, RM);
2931 break;
2932 case BO_Div:
2933 // [expr.mul]p4:
2934 // If the second operand of / or % is zero the behavior is undefined.
2935 if (RHS.isZero())
2936 Info.CCEDiag(E, DiagId: diag::note_expr_divide_by_zero);
2937 St = LHS.divide(RHS, RM);
2938 break;
2939 }
2940
2941 // [expr.pre]p4:
2942 // If during the evaluation of an expression, the result is not
2943 // mathematically defined [...], the behavior is undefined.
2944 // FIXME: C++ rules require us to not conform to IEEE 754 here.
2945 if (LHS.isNaN()) {
2946 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << LHS.isNaN();
2947 return Info.noteUndefinedBehavior();
2948 }
2949
2950 return checkFloatingPointResult(Info, E, St);
2951}
2952
2953static bool handleLogicalOpForVector(const APInt &LHSValue,
2954 BinaryOperatorKind Opcode,
2955 const APInt &RHSValue, APInt &Result) {
2956 bool LHS = (LHSValue != 0);
2957 bool RHS = (RHSValue != 0);
2958
2959 if (Opcode == BO_LAnd)
2960 Result = LHS && RHS;
2961 else
2962 Result = LHS || RHS;
2963 return true;
2964}
2965static bool handleLogicalOpForVector(const APFloat &LHSValue,
2966 BinaryOperatorKind Opcode,
2967 const APFloat &RHSValue, APInt &Result) {
2968 bool LHS = !LHSValue.isZero();
2969 bool RHS = !RHSValue.isZero();
2970
2971 if (Opcode == BO_LAnd)
2972 Result = LHS && RHS;
2973 else
2974 Result = LHS || RHS;
2975 return true;
2976}
2977
2978static bool handleLogicalOpForVector(const APValue &LHSValue,
2979 BinaryOperatorKind Opcode,
2980 const APValue &RHSValue, APInt &Result) {
2981 // The result is always an int type, however operands match the first.
2982 if (LHSValue.getKind() == APValue::Int)
2983 return handleLogicalOpForVector(LHSValue: LHSValue.getInt(), Opcode,
2984 RHSValue: RHSValue.getInt(), Result);
2985 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
2986 return handleLogicalOpForVector(LHSValue: LHSValue.getFloat(), Opcode,
2987 RHSValue: RHSValue.getFloat(), Result);
2988}
2989
2990template <typename APTy>
2991static bool
2992handleCompareOpForVectorHelper(const APTy &LHSValue, BinaryOperatorKind Opcode,
2993 const APTy &RHSValue, APInt &Result) {
2994 switch (Opcode) {
2995 default:
2996 llvm_unreachable("unsupported binary operator");
2997 case BO_EQ:
2998 Result = (LHSValue == RHSValue);
2999 break;
3000 case BO_NE:
3001 Result = (LHSValue != RHSValue);
3002 break;
3003 case BO_LT:
3004 Result = (LHSValue < RHSValue);
3005 break;
3006 case BO_GT:
3007 Result = (LHSValue > RHSValue);
3008 break;
3009 case BO_LE:
3010 Result = (LHSValue <= RHSValue);
3011 break;
3012 case BO_GE:
3013 Result = (LHSValue >= RHSValue);
3014 break;
3015 }
3016
3017 // The boolean operations on these vector types use an instruction that
3018 // results in a mask of '-1' for the 'truth' value. Ensure that we negate 1
3019 // to -1 to make sure that we produce the correct value.
3020 Result.negate();
3021
3022 return true;
3023}
3024
3025static bool handleCompareOpForVector(const APValue &LHSValue,
3026 BinaryOperatorKind Opcode,
3027 const APValue &RHSValue, APInt &Result) {
3028 // The result is always an int type, however operands match the first.
3029 if (LHSValue.getKind() == APValue::Int)
3030 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getInt(), Opcode,
3031 RHSValue: RHSValue.getInt(), Result);
3032 assert(LHSValue.getKind() == APValue::Float && "Should be no other options");
3033 return handleCompareOpForVectorHelper(LHSValue: LHSValue.getFloat(), Opcode,
3034 RHSValue: RHSValue.getFloat(), Result);
3035}
3036
3037// Perform binary operations for vector types, in place on the LHS.
3038static bool handleVectorVectorBinOp(EvalInfo &Info, const BinaryOperator *E,
3039 BinaryOperatorKind Opcode,
3040 APValue &LHSValue,
3041 const APValue &RHSValue) {
3042 assert(Opcode != BO_PtrMemD && Opcode != BO_PtrMemI &&
3043 "Operation not supported on vector types");
3044
3045 const auto *VT = E->getType()->castAs<VectorType>();
3046 unsigned NumElements = VT->getNumElements();
3047 QualType EltTy = VT->getElementType();
3048
3049 // In the cases (typically C as I've observed) where we aren't evaluating
3050 // constexpr but are checking for cases where the LHS isn't yet evaluatable,
3051 // just give up.
3052 if (!LHSValue.isVector()) {
3053 assert(LHSValue.isLValue() &&
3054 "A vector result that isn't a vector OR uncalculated LValue");
3055 Info.FFDiag(E);
3056 return false;
3057 }
3058
3059 assert(LHSValue.getVectorLength() == NumElements &&
3060 RHSValue.getVectorLength() == NumElements && "Different vector sizes");
3061
3062 SmallVector<APValue, 4> ResultElements;
3063
3064 for (unsigned EltNum = 0; EltNum < NumElements; ++EltNum) {
3065 APValue LHSElt = LHSValue.getVectorElt(I: EltNum);
3066 APValue RHSElt = RHSValue.getVectorElt(I: EltNum);
3067
3068 if (EltTy->isIntegerType()) {
3069 APSInt EltResult{Info.Ctx.getIntWidth(T: EltTy),
3070 EltTy->isUnsignedIntegerType()};
3071 bool Success = true;
3072
3073 if (BinaryOperator::isLogicalOp(Opc: Opcode))
3074 Success = handleLogicalOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3075 else if (BinaryOperator::isComparisonOp(Opc: Opcode))
3076 Success = handleCompareOpForVector(LHSValue: LHSElt, Opcode, RHSValue: RHSElt, Result&: EltResult);
3077 else
3078 Success = handleIntIntBinOp(Info, E, LHS: LHSElt.getInt(), Opcode,
3079 RHS: RHSElt.getInt(), Result&: EltResult);
3080
3081 if (!Success) {
3082 Info.FFDiag(E);
3083 return false;
3084 }
3085 ResultElements.emplace_back(Args&: EltResult);
3086
3087 } else if (EltTy->isFloatingType()) {
3088 assert(LHSElt.getKind() == APValue::Float &&
3089 RHSElt.getKind() == APValue::Float &&
3090 "Mismatched LHS/RHS/Result Type");
3091 APFloat LHSFloat = LHSElt.getFloat();
3092
3093 if (!handleFloatFloatBinOp(Info, E, LHS&: LHSFloat, Opcode,
3094 RHS: RHSElt.getFloat())) {
3095 Info.FFDiag(E);
3096 return false;
3097 }
3098
3099 ResultElements.emplace_back(Args&: LHSFloat);
3100 }
3101 }
3102
3103 LHSValue = APValue(ResultElements.data(), ResultElements.size());
3104 return true;
3105}
3106
3107/// Cast an lvalue referring to a base subobject to a derived class, by
3108/// truncating the lvalue's path to the given length.
3109static bool CastToDerivedClass(EvalInfo &Info, const Expr *E, LValue &Result,
3110 const RecordDecl *TruncatedType,
3111 unsigned TruncatedElements) {
3112 SubobjectDesignator &D = Result.Designator;
3113
3114 // Check we actually point to a derived class object.
3115 if (TruncatedElements == D.Entries.size())
3116 return true;
3117 assert(TruncatedElements >= D.MostDerivedPathLength &&
3118 "not casting to a derived class");
3119 if (!Result.checkSubobject(Info, E, CSK: CSK_Derived))
3120 return false;
3121
3122 // Truncate the path to the subobject, and remove any derived-to-base offsets.
3123 const RecordDecl *RD = TruncatedType;
3124 for (unsigned I = TruncatedElements, N = D.Entries.size(); I != N; ++I) {
3125 if (RD->isInvalidDecl()) return false;
3126 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
3127 const CXXRecordDecl *Base = getAsBaseClass(E: D.Entries[I]);
3128 if (isVirtualBaseClass(E: D.Entries[I]))
3129 Result.Offset -= Layout.getVBaseClassOffset(VBase: Base);
3130 else
3131 Result.Offset -= Layout.getBaseClassOffset(Base);
3132 RD = Base;
3133 }
3134 D.Entries.resize(N: TruncatedElements);
3135 return true;
3136}
3137
3138static bool HandleLValueDirectBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3139 const CXXRecordDecl *Derived,
3140 const CXXRecordDecl *Base,
3141 const ASTRecordLayout *RL = nullptr) {
3142 if (!RL) {
3143 if (Derived->isInvalidDecl()) return false;
3144 RL = &Info.Ctx.getASTRecordLayout(D: Derived);
3145 }
3146
3147 Obj.addDecl(Info, E, D: Base, /*Virtual*/ false);
3148 Obj.getLValueOffset() += RL->getBaseClassOffset(Base);
3149 return true;
3150}
3151
3152static bool HandleLValueBase(EvalInfo &Info, const Expr *E, LValue &Obj,
3153 const CXXRecordDecl *DerivedDecl,
3154 const CXXBaseSpecifier *Base) {
3155 const CXXRecordDecl *BaseDecl = Base->getType()->getAsCXXRecordDecl();
3156
3157 if (!Base->isVirtual())
3158 return HandleLValueDirectBase(Info, E, Obj, Derived: DerivedDecl, Base: BaseDecl);
3159
3160 SubobjectDesignator &D = Obj.Designator;
3161 if (D.Invalid)
3162 return false;
3163
3164 // Extract most-derived object and corresponding type.
3165 // FIXME: After implementing P2280R4 it became possible to get references
3166 // here. We do MostDerivedType->getAsCXXRecordDecl() in several other
3167 // locations and if we see crashes in those locations in the future
3168 // it may make more sense to move this fix into Lvalue::set.
3169 DerivedDecl = D.MostDerivedType.getNonReferenceType()->getAsCXXRecordDecl();
3170 if (!CastToDerivedClass(Info, E, Result&: Obj, TruncatedType: DerivedDecl, TruncatedElements: D.MostDerivedPathLength))
3171 return false;
3172
3173 // Find the virtual base class.
3174 if (DerivedDecl->isInvalidDecl()) return false;
3175 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: DerivedDecl);
3176 Obj.addDecl(Info, E, D: BaseDecl, /*Virtual*/ true);
3177 Obj.getLValueOffset() += Layout.getVBaseClassOffset(VBase: BaseDecl);
3178 return true;
3179}
3180
3181static bool HandleLValueBasePath(EvalInfo &Info, const CastExpr *E,
3182 QualType Type, LValue &Result) {
3183 for (CastExpr::path_const_iterator PathI = E->path_begin(),
3184 PathE = E->path_end();
3185 PathI != PathE; ++PathI) {
3186 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Type->getAsCXXRecordDecl(),
3187 Base: *PathI))
3188 return false;
3189 Type = (*PathI)->getType();
3190 }
3191 return true;
3192}
3193
3194/// Cast an lvalue referring to a derived class to a known base subobject.
3195static bool CastToBaseClass(EvalInfo &Info, const Expr *E, LValue &Result,
3196 const CXXRecordDecl *DerivedRD,
3197 const CXXRecordDecl *BaseRD) {
3198 CXXBasePaths Paths(/*FindAmbiguities=*/false,
3199 /*RecordPaths=*/true, /*DetectVirtual=*/false);
3200 if (!DerivedRD->isDerivedFrom(Base: BaseRD, Paths))
3201 llvm_unreachable("Class must be derived from the passed in base class!");
3202
3203 for (CXXBasePathElement &Elem : Paths.front())
3204 if (!HandleLValueBase(Info, E, Obj&: Result, DerivedDecl: Elem.Class, Base: Elem.Base))
3205 return false;
3206 return true;
3207}
3208
3209/// Update LVal to refer to the given field, which must be a member of the type
3210/// currently described by LVal.
3211static bool HandleLValueMember(EvalInfo &Info, const Expr *E, LValue &LVal,
3212 const FieldDecl *FD,
3213 const ASTRecordLayout *RL = nullptr) {
3214 if (!RL) {
3215 if (FD->getParent()->isInvalidDecl()) return false;
3216 RL = &Info.Ctx.getASTRecordLayout(D: FD->getParent());
3217 }
3218
3219 unsigned I = FD->getFieldIndex();
3220 LVal.addDecl(Info, E, D: FD);
3221 LVal.adjustOffset(N: Info.Ctx.toCharUnitsFromBits(BitSize: RL->getFieldOffset(FieldNo: I)));
3222 return true;
3223}
3224
3225/// Update LVal to refer to the given indirect field.
3226static bool HandleLValueIndirectMember(EvalInfo &Info, const Expr *E,
3227 LValue &LVal,
3228 const IndirectFieldDecl *IFD) {
3229 for (const auto *C : IFD->chain())
3230 if (!HandleLValueMember(Info, E, LVal, FD: cast<FieldDecl>(Val: C)))
3231 return false;
3232 return true;
3233}
3234
3235enum class SizeOfType {
3236 SizeOf,
3237 DataSizeOf,
3238};
3239
3240/// Get the size of the given type in char units.
3241static bool HandleSizeof(EvalInfo &Info, SourceLocation Loc, QualType Type,
3242 CharUnits &Size, SizeOfType SOT = SizeOfType::SizeOf) {
3243 // sizeof(void), __alignof__(void), sizeof(function) = 1 as a gcc
3244 // extension.
3245 if (Type->isVoidType() || Type->isFunctionType()) {
3246 Size = CharUnits::One();
3247 return true;
3248 }
3249
3250 if (Type->isDependentType()) {
3251 Info.FFDiag(Loc);
3252 return false;
3253 }
3254
3255 if (!Type->isConstantSizeType()) {
3256 // sizeof(vla) is not a constantexpr: C99 6.5.3.4p2.
3257 // FIXME: Better diagnostic.
3258 Info.FFDiag(Loc);
3259 return false;
3260 }
3261
3262 if (SOT == SizeOfType::SizeOf)
3263 Size = Info.Ctx.getTypeSizeInChars(T: Type);
3264 else
3265 Size = Info.Ctx.getTypeInfoDataSizeInChars(T: Type).Width;
3266 return true;
3267}
3268
3269/// Update a pointer value to model pointer arithmetic.
3270/// \param Info - Information about the ongoing evaluation.
3271/// \param E - The expression being evaluated, for diagnostic purposes.
3272/// \param LVal - The pointer value to be updated.
3273/// \param EltTy - The pointee type represented by LVal.
3274/// \param Adjustment - The adjustment, in objects of type EltTy, to add.
3275static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3276 LValue &LVal, QualType EltTy,
3277 APSInt Adjustment) {
3278 CharUnits SizeOfPointee;
3279 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfPointee))
3280 return false;
3281
3282 LVal.adjustOffsetAndIndex(Info, E, Index: Adjustment, ElementSize: SizeOfPointee);
3283 return true;
3284}
3285
3286static bool HandleLValueArrayAdjustment(EvalInfo &Info, const Expr *E,
3287 LValue &LVal, QualType EltTy,
3288 int64_t Adjustment) {
3289 return HandleLValueArrayAdjustment(Info, E, LVal, EltTy,
3290 Adjustment: APSInt::get(X: Adjustment));
3291}
3292
3293/// Update an lvalue to refer to a component of a complex number.
3294/// \param Info - Information about the ongoing evaluation.
3295/// \param LVal - The lvalue to be updated.
3296/// \param EltTy - The complex number's component type.
3297/// \param Imag - False for the real component, true for the imaginary.
3298static bool HandleLValueComplexElement(EvalInfo &Info, const Expr *E,
3299 LValue &LVal, QualType EltTy,
3300 bool Imag) {
3301 if (Imag) {
3302 CharUnits SizeOfComponent;
3303 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfComponent))
3304 return false;
3305 LVal.Offset += SizeOfComponent;
3306 }
3307 LVal.addComplex(Info, E, EltTy, Imag);
3308 return true;
3309}
3310
3311static bool HandleLValueVectorElement(EvalInfo &Info, const Expr *E,
3312 LValue &LVal, QualType EltTy,
3313 uint64_t Size, uint64_t Idx) {
3314 if (Idx) {
3315 CharUnits SizeOfElement;
3316 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: EltTy, Size&: SizeOfElement))
3317 return false;
3318 LVal.Offset += SizeOfElement * Idx;
3319 }
3320 LVal.addVectorElement(Info, E, EltTy, Size, Idx);
3321 return true;
3322}
3323
3324/// Try to evaluate the initializer for a variable declaration.
3325///
3326/// \param Info Information about the ongoing evaluation.
3327/// \param E An expression to be used when printing diagnostics.
3328/// \param VD The variable whose initializer should be obtained.
3329/// \param Version The version of the variable within the frame.
3330/// \param Frame The frame in which the variable was created. Must be null
3331/// if this variable is not local to the evaluation.
3332/// \param Result Filled in with a pointer to the value of the variable.
3333static bool evaluateVarDeclInit(EvalInfo &Info, const Expr *E,
3334 const VarDecl *VD, CallStackFrame *Frame,
3335 unsigned Version, APValue *&Result) {
3336 // C++23 [expr.const]p8 If we have a reference type allow unknown references
3337 // and pointers.
3338 bool AllowConstexprUnknown =
3339 Info.getLangOpts().CPlusPlus23 && VD->getType()->isReferenceType();
3340
3341 APValue::LValueBase Base(VD, Frame ? Frame->Index : 0, Version);
3342
3343 auto CheckUninitReference = [&](bool IsLocalVariable) {
3344 if (!Result || (!Result->hasValue() && VD->getType()->isReferenceType())) {
3345 // C++23 [expr.const]p8
3346 // ... For such an object that is not usable in constant expressions, the
3347 // dynamic type of the object is constexpr-unknown. For such a reference
3348 // that is not usable in constant expressions, the reference is treated
3349 // as binding to an unspecified object of the referenced type whose
3350 // lifetime and that of all subobjects includes the entire constant
3351 // evaluation and whose dynamic type is constexpr-unknown.
3352 //
3353 // Variables that are part of the current evaluation are not
3354 // constexpr-unknown.
3355 if (!AllowConstexprUnknown || IsLocalVariable) {
3356 if (!Info.checkingPotentialConstantExpression())
3357 Info.FFDiag(E, DiagId: diag::note_constexpr_use_uninit_reference);
3358 return false;
3359 }
3360 Result = nullptr;
3361 }
3362 return true;
3363 };
3364
3365 // If this is a local variable, dig out its value.
3366 if (Frame) {
3367 Result = Frame->getTemporary(Key: VD, Version);
3368 if (Result)
3369 return CheckUninitReference(/*IsLocalVariable=*/true);
3370
3371 if (!isa<ParmVarDecl>(Val: VD)) {
3372 // Assume variables referenced within a lambda's call operator that were
3373 // not declared within the call operator are captures and during checking
3374 // of a potential constant expression, assume they are unknown constant
3375 // expressions.
3376 assert(isLambdaCallOperator(Frame->Callee) &&
3377 (VD->getDeclContext() != Frame->Callee || VD->isInitCapture()) &&
3378 "missing value for local variable");
3379 if (Info.checkingPotentialConstantExpression())
3380 return false;
3381
3382 llvm_unreachable(
3383 "A variable in a frame should either be a local or a parameter");
3384 }
3385 }
3386
3387 // If we're currently evaluating the initializer of this declaration, use that
3388 // in-flight value.
3389 if (Info.EvaluatingDecl == Base) {
3390 Result = Info.EvaluatingDeclValue;
3391 return CheckUninitReference(/*IsLocalVariable=*/false);
3392 }
3393
3394 // P2280R4 struck the restriction that variable of reference type lifetime
3395 // should begin within the evaluation of E
3396 // Used to be C++20 [expr.const]p5.12.2:
3397 // ... its lifetime began within the evaluation of E;
3398 if (isa<ParmVarDecl>(Val: VD)) {
3399 if (AllowConstexprUnknown) {
3400 Result = nullptr;
3401 return true;
3402 }
3403
3404 // Assume parameters of a potential constant expression are usable in
3405 // constant expressions.
3406 if (!Info.checkingPotentialConstantExpression() ||
3407 !Info.CurrentCall->Callee ||
3408 !Info.CurrentCall->Callee->Equals(DC: VD->getDeclContext())) {
3409 if (Info.getLangOpts().CPlusPlus11) {
3410 Info.FFDiag(E, DiagId: diag::note_constexpr_function_param_value_unknown)
3411 << VD;
3412 NoteLValueLocation(Info, Base);
3413 } else {
3414 Info.FFDiag(E);
3415 }
3416 }
3417 return false;
3418 }
3419
3420 if (E->isValueDependent())
3421 return false;
3422
3423 // Dig out the initializer, and use the declaration which it's attached to.
3424 // FIXME: We should eventually check whether the variable has a reachable
3425 // initializing declaration.
3426 const Expr *Init = VD->getAnyInitializer(D&: VD);
3427 // P2280R4 struck the restriction that variable of reference type should have
3428 // a preceding initialization.
3429 // Used to be C++20 [expr.const]p5.12:
3430 // ... reference has a preceding initialization and either ...
3431 if (!Init && !AllowConstexprUnknown) {
3432 // Don't diagnose during potential constant expression checking; an
3433 // initializer might be added later.
3434 if (!Info.checkingPotentialConstantExpression()) {
3435 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_unknown, ExtraNotes: 1)
3436 << VD;
3437 NoteLValueLocation(Info, Base);
3438 }
3439 return false;
3440 }
3441
3442 // P2280R4 struck the initialization requirement for variables of reference
3443 // type so we can no longer assume we have an Init.
3444 // Used to be C++20 [expr.const]p5.12:
3445 // ... reference has a preceding initialization and either ...
3446 if (Init && Init->isValueDependent()) {
3447 // The DeclRefExpr is not value-dependent, but the variable it refers to
3448 // has a value-dependent initializer. This should only happen in
3449 // constant-folding cases, where the variable is not actually of a suitable
3450 // type for use in a constant expression (otherwise the DeclRefExpr would
3451 // have been value-dependent too), so diagnose that.
3452 assert(!VD->mightBeUsableInConstantExpressions(Info.Ctx));
3453 if (!Info.checkingPotentialConstantExpression()) {
3454 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
3455 ? diag::note_constexpr_ltor_non_constexpr
3456 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
3457 << VD << VD->getType();
3458 NoteLValueLocation(Info, Base);
3459 }
3460 return false;
3461 }
3462
3463 // Check that we can fold the initializer. In C++, we will have already done
3464 // this in the cases where it matters for conformance.
3465 // P2280R4 struck the initialization requirement for variables of reference
3466 // type so we can no longer assume we have an Init.
3467 // Used to be C++20 [expr.const]p5.12:
3468 // ... reference has a preceding initialization and either ...
3469 if (Init && !VD->evaluateValue() && !AllowConstexprUnknown) {
3470 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3471 NoteLValueLocation(Info, Base);
3472 return false;
3473 }
3474
3475 // Check that the variable is actually usable in constant expressions. For a
3476 // const integral variable or a reference, we might have a non-constant
3477 // initializer that we can nonetheless evaluate the initializer for. Such
3478 // variables are not usable in constant expressions. In C++98, the
3479 // initializer also syntactically needs to be an ICE.
3480 //
3481 // FIXME: We don't diagnose cases that aren't potentially usable in constant
3482 // expressions here; doing so would regress diagnostics for things like
3483 // reading from a volatile constexpr variable.
3484 if ((Info.getLangOpts().CPlusPlus && !VD->hasConstantInitialization() &&
3485 VD->mightBeUsableInConstantExpressions(C: Info.Ctx) &&
3486 !AllowConstexprUnknown) ||
3487 ((Info.getLangOpts().CPlusPlus || Info.getLangOpts().OpenCL) &&
3488 !Info.getLangOpts().CPlusPlus11 && !VD->hasICEInitializer(Context: Info.Ctx))) {
3489 if (Init) {
3490 Info.CCEDiag(E, DiagId: diag::note_constexpr_var_init_non_constant, ExtraNotes: 1) << VD;
3491 NoteLValueLocation(Info, Base);
3492 } else {
3493 Info.CCEDiag(E);
3494 }
3495 }
3496
3497 // Never use the initializer of a weak variable, not even for constant
3498 // folding. We can't be sure that this is the definition that will be used.
3499 if (VD->isWeak()) {
3500 Info.FFDiag(E, DiagId: diag::note_constexpr_var_init_weak) << VD;
3501 NoteLValueLocation(Info, Base);
3502 return false;
3503 }
3504
3505 Result = const_cast<APValue *>(VD->getEvaluatedValue());
3506
3507 if (!Result && !AllowConstexprUnknown)
3508 return false;
3509
3510 return CheckUninitReference(/*IsLocalVariable=*/false);
3511}
3512
3513/// Get the base index of the given base class within an APValue representing
3514/// the given derived class.
3515static unsigned getBaseIndex(const CXXRecordDecl *Derived,
3516 const CXXRecordDecl *Base) {
3517 Base = Base->getCanonicalDecl();
3518 unsigned Index = 0;
3519 for (CXXRecordDecl::base_class_const_iterator I = Derived->bases_begin(),
3520 E = Derived->bases_end(); I != E; ++I, ++Index) {
3521 if (I->getType()->getAsCXXRecordDecl()->getCanonicalDecl() == Base)
3522 return Index;
3523 }
3524
3525 llvm_unreachable("base class missing from derived class's bases list");
3526}
3527
3528/// Extract the value of a character from a string literal.
3529static APSInt extractStringLiteralCharacter(EvalInfo &Info, const Expr *Lit,
3530 uint64_t Index) {
3531 assert(!isa<SourceLocExpr>(Lit) &&
3532 "SourceLocExpr should have already been converted to a StringLiteral");
3533
3534 // FIXME: Support MakeStringConstant
3535 if (const auto *ObjCEnc = dyn_cast<ObjCEncodeExpr>(Val: Lit)) {
3536 std::string Str;
3537 Info.Ctx.getObjCEncodingForType(T: ObjCEnc->getEncodedType(), S&: Str);
3538 assert(Index <= Str.size() && "Index too large");
3539 return APSInt::getUnsigned(X: Str.c_str()[Index]);
3540 }
3541
3542 if (auto PE = dyn_cast<PredefinedExpr>(Val: Lit))
3543 Lit = PE->getFunctionName();
3544 const StringLiteral *S = cast<StringLiteral>(Val: Lit);
3545 const ConstantArrayType *CAT =
3546 Info.Ctx.getAsConstantArrayType(T: S->getType());
3547 assert(CAT && "string literal isn't an array");
3548 QualType CharType = CAT->getElementType();
3549 assert(CharType->isIntegerType() && "unexpected character type");
3550 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3551 CharType->isUnsignedIntegerType());
3552 if (Index < S->getLength())
3553 Value = S->getCodeUnit(i: Index);
3554 return Value;
3555}
3556
3557// Expand a string literal into an array of characters.
3558//
3559// FIXME: This is inefficient; we should probably introduce something similar
3560// to the LLVM ConstantDataArray to make this cheaper.
3561static void expandStringLiteral(EvalInfo &Info, const StringLiteral *S,
3562 APValue &Result,
3563 QualType AllocType = QualType()) {
3564 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
3565 T: AllocType.isNull() ? S->getType() : AllocType);
3566 assert(CAT && "string literal isn't an array");
3567 QualType CharType = CAT->getElementType();
3568 assert(CharType->isIntegerType() && "unexpected character type");
3569
3570 unsigned Elts = CAT->getZExtSize();
3571 Result = APValue(APValue::UninitArray(),
3572 std::min(a: S->getLength(), b: Elts), Elts);
3573 APSInt Value(Info.Ctx.getTypeSize(T: CharType),
3574 CharType->isUnsignedIntegerType());
3575 if (Result.hasArrayFiller())
3576 Result.getArrayFiller() = APValue(Value);
3577 for (unsigned I = 0, N = Result.getArrayInitializedElts(); I != N; ++I) {
3578 Value = S->getCodeUnit(i: I);
3579 Result.getArrayInitializedElt(I) = APValue(Value);
3580 }
3581}
3582
3583// Expand an array so that it has more than Index filled elements.
3584static void expandArray(APValue &Array, unsigned Index) {
3585 unsigned Size = Array.getArraySize();
3586 assert(Index < Size);
3587
3588 // Always at least double the number of elements for which we store a value.
3589 unsigned OldElts = Array.getArrayInitializedElts();
3590 unsigned NewElts = std::max(a: Index+1, b: OldElts * 2);
3591 NewElts = std::min(a: Size, b: std::max(a: NewElts, b: 8u));
3592
3593 // Copy the data across.
3594 APValue NewValue(APValue::UninitArray(), NewElts, Size);
3595 for (unsigned I = 0; I != OldElts; ++I)
3596 NewValue.getArrayInitializedElt(I).swap(RHS&: Array.getArrayInitializedElt(I));
3597 for (unsigned I = OldElts; I != NewElts; ++I)
3598 NewValue.getArrayInitializedElt(I) = Array.getArrayFiller();
3599 if (NewValue.hasArrayFiller())
3600 NewValue.getArrayFiller() = Array.getArrayFiller();
3601 Array.swap(RHS&: NewValue);
3602}
3603
3604// Expand an indeterminate vector to materialize all elements.
3605static void expandVector(APValue &Vec, unsigned NumElements) {
3606 assert(Vec.isIndeterminate());
3607 SmallVector<APValue, 4> Elts(NumElements, APValue::IndeterminateValue());
3608 Vec = APValue(Elts.data(), Elts.size());
3609}
3610
3611/// Determine whether a type would actually be read by an lvalue-to-rvalue
3612/// conversion. If it's of class type, we may assume that the copy operation
3613/// is trivial. Note that this is never true for a union type with fields
3614/// (because the copy always "reads" the active member) and always true for
3615/// a non-class type.
3616static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD);
3617static bool isReadByLvalueToRvalueConversion(QualType T) {
3618 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3619 return !RD || isReadByLvalueToRvalueConversion(RD);
3620}
3621static bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD) {
3622 // FIXME: A trivial copy of a union copies the object representation, even if
3623 // the union is empty.
3624 if (RD->isUnion())
3625 return !RD->field_empty();
3626 if (RD->isEmpty())
3627 return false;
3628
3629 for (auto *Field : RD->fields())
3630 if (!Field->isUnnamedBitField() &&
3631 isReadByLvalueToRvalueConversion(T: Field->getType()))
3632 return true;
3633
3634 for (auto &BaseSpec : RD->bases())
3635 if (isReadByLvalueToRvalueConversion(T: BaseSpec.getType()))
3636 return true;
3637
3638 return false;
3639}
3640
3641/// Diagnose an attempt to read from any unreadable field within the specified
3642/// type, which might be a class type.
3643static bool diagnoseMutableFields(EvalInfo &Info, const Expr *E, AccessKinds AK,
3644 QualType T) {
3645 CXXRecordDecl *RD = T->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
3646 if (!RD)
3647 return false;
3648
3649 if (!RD->hasMutableFields())
3650 return false;
3651
3652 for (auto *Field : RD->fields()) {
3653 // If we're actually going to read this field in some way, then it can't
3654 // be mutable. If we're in a union, then assigning to a mutable field
3655 // (even an empty one) can change the active member, so that's not OK.
3656 // FIXME: Add core issue number for the union case.
3657 if (Field->isMutable() &&
3658 (RD->isUnion() || isReadByLvalueToRvalueConversion(T: Field->getType()))) {
3659 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1) << AK << Field;
3660 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
3661 return true;
3662 }
3663
3664 if (diagnoseMutableFields(Info, E, AK, T: Field->getType()))
3665 return true;
3666 }
3667
3668 for (auto &BaseSpec : RD->bases())
3669 if (diagnoseMutableFields(Info, E, AK, T: BaseSpec.getType()))
3670 return true;
3671
3672 // All mutable fields were empty, and thus not actually read.
3673 return false;
3674}
3675
3676static bool lifetimeStartedInEvaluation(EvalInfo &Info,
3677 APValue::LValueBase Base,
3678 bool MutableSubobject = false) {
3679 // A temporary or transient heap allocation we created.
3680 if (Base.getCallIndex() || Base.is<DynamicAllocLValue>())
3681 return true;
3682
3683 switch (Info.IsEvaluatingDecl) {
3684 case EvalInfo::EvaluatingDeclKind::None:
3685 return false;
3686
3687 case EvalInfo::EvaluatingDeclKind::Ctor:
3688 // The variable whose initializer we're evaluating.
3689 if (Info.EvaluatingDecl == Base)
3690 return true;
3691
3692 // A temporary lifetime-extended by the variable whose initializer we're
3693 // evaluating.
3694 if (auto *BaseE = Base.dyn_cast<const Expr *>())
3695 if (auto *BaseMTE = dyn_cast<MaterializeTemporaryExpr>(Val: BaseE))
3696 return Info.EvaluatingDecl == BaseMTE->getExtendingDecl();
3697 return false;
3698
3699 case EvalInfo::EvaluatingDeclKind::Dtor:
3700 // C++2a [expr.const]p6:
3701 // [during constant destruction] the lifetime of a and its non-mutable
3702 // subobjects (but not its mutable subobjects) [are] considered to start
3703 // within e.
3704 if (MutableSubobject || Base != Info.EvaluatingDecl)
3705 return false;
3706 // FIXME: We can meaningfully extend this to cover non-const objects, but
3707 // we will need special handling: we should be able to access only
3708 // subobjects of such objects that are themselves declared const.
3709 QualType T = getType(B: Base);
3710 return T.isConstQualified() || T->isReferenceType();
3711 }
3712
3713 llvm_unreachable("unknown evaluating decl kind");
3714}
3715
3716static bool CheckArraySize(EvalInfo &Info, const ConstantArrayType *CAT,
3717 SourceLocation CallLoc = {}) {
3718 return Info.CheckArraySize(
3719 Loc: CAT->getSizeExpr() ? CAT->getSizeExpr()->getBeginLoc() : CallLoc,
3720 BitWidth: CAT->getNumAddressingBits(Context: Info.Ctx), ElemCount: CAT->getZExtSize(),
3721 /*Diag=*/true);
3722}
3723
3724static bool handleScalarCast(EvalInfo &Info, const FPOptions FPO, const Expr *E,
3725 QualType SourceTy, QualType DestTy,
3726 APValue const &Original, APValue &Result) {
3727 // boolean must be checked before integer
3728 // since IsIntegerType() is true for bool
3729 if (SourceTy->isBooleanType()) {
3730 if (DestTy->isBooleanType()) {
3731 Result = Original;
3732 return true;
3733 }
3734 if (DestTy->isIntegerType() || DestTy->isRealFloatingType()) {
3735 bool BoolResult;
3736 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3737 return false;
3738 uint64_t IntResult = BoolResult;
3739 QualType IntType = DestTy->isIntegerType()
3740 ? DestTy
3741 : Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false);
3742 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: IntType));
3743 }
3744 if (DestTy->isRealFloatingType()) {
3745 APValue Result2 = APValue(APFloat(0.0));
3746 if (!HandleIntToFloatCast(Info, E, FPO,
3747 SrcType: Info.Ctx.getIntTypeForBitwidth(DestWidth: 64, Signed: false),
3748 Value: Result.getInt(), DestType: DestTy, Result&: Result2.getFloat()))
3749 return false;
3750 Result = std::move(Result2);
3751 }
3752 return true;
3753 }
3754 if (SourceTy->isIntegerType()) {
3755 if (DestTy->isRealFloatingType()) {
3756 Result = APValue(APFloat(0.0));
3757 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
3758 DestType: DestTy, Result&: Result.getFloat());
3759 }
3760 if (DestTy->isBooleanType()) {
3761 bool BoolResult;
3762 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3763 return false;
3764 uint64_t IntResult = BoolResult;
3765 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3766 return true;
3767 }
3768 if (DestTy->isIntegerType()) {
3769 Result = APValue(
3770 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
3771 return true;
3772 }
3773 } else if (SourceTy->isRealFloatingType()) {
3774 if (DestTy->isRealFloatingType()) {
3775 Result = Original;
3776 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
3777 Result&: Result.getFloat());
3778 }
3779 if (DestTy->isBooleanType()) {
3780 bool BoolResult;
3781 if (!HandleConversionToBool(Val: Original, Result&: BoolResult))
3782 return false;
3783 uint64_t IntResult = BoolResult;
3784 Result = APValue(Info.Ctx.MakeIntValue(Value: IntResult, Type: DestTy));
3785 return true;
3786 }
3787 if (DestTy->isIntegerType()) {
3788 Result = APValue(APSInt());
3789 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
3790 DestType: DestTy, Result&: Result.getInt());
3791 }
3792 }
3793
3794 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3795 return false;
3796}
3797
3798// do the heavy lifting for casting to aggregate types
3799// because we have to deal with bitfields specially
3800static bool constructAggregate(EvalInfo &Info, const FPOptions FPO,
3801 const Expr *E, APValue &Result,
3802 QualType ResultType,
3803 SmallVectorImpl<APValue> &Elements,
3804 SmallVectorImpl<QualType> &ElTypes) {
3805
3806 SmallVector<std::tuple<APValue *, QualType, unsigned>> WorkList = {
3807 {&Result, ResultType, 0}};
3808
3809 unsigned ElI = 0;
3810 while (!WorkList.empty() && ElI < Elements.size()) {
3811 auto [Res, Type, BitWidth] = WorkList.pop_back_val();
3812
3813 if (Type->isRealFloatingType()) {
3814 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3815 Result&: *Res))
3816 return false;
3817 ElI++;
3818 continue;
3819 }
3820 if (Type->isIntegerType()) {
3821 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: Type, Original: Elements[ElI],
3822 Result&: *Res))
3823 return false;
3824 if (BitWidth > 0) {
3825 if (!Res->isInt())
3826 return false;
3827 APSInt &Int = Res->getInt();
3828 unsigned OldBitWidth = Int.getBitWidth();
3829 unsigned NewBitWidth = BitWidth;
3830 if (NewBitWidth < OldBitWidth)
3831 Int = Int.trunc(width: NewBitWidth).extend(width: OldBitWidth);
3832 }
3833 ElI++;
3834 continue;
3835 }
3836 if (Type->isVectorType()) {
3837 QualType ElTy = Type->castAs<VectorType>()->getElementType();
3838 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
3839 SmallVector<APValue> Vals(NumEl);
3840 for (unsigned I = 0; I < NumEl; ++I) {
3841 if (!handleScalarCast(Info, FPO, E, SourceTy: ElTypes[ElI], DestTy: ElTy, Original: Elements[ElI],
3842 Result&: Vals[I]))
3843 return false;
3844 ElI++;
3845 }
3846 *Res = APValue(Vals.data(), NumEl);
3847 continue;
3848 }
3849 if (Type->isConstantArrayType()) {
3850 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
3851 ->getElementType();
3852 uint64_t Size =
3853 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
3854 *Res = APValue(APValue::UninitArray(), Size, Size);
3855 for (int64_t I = Size - 1; I > -1; --I)
3856 WorkList.emplace_back(Args: &Res->getArrayInitializedElt(I), Args&: ElTy, Args: 0u);
3857 continue;
3858 }
3859 if (Type->isRecordType()) {
3860 const RecordDecl *RD = Type->getAsRecordDecl();
3861
3862 unsigned NumBases = 0;
3863 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
3864 NumBases = CXXRD->getNumBases();
3865
3866 *Res = APValue(APValue::UninitStruct(), NumBases, RD->getNumFields());
3867
3868 SmallVector<std::tuple<APValue *, QualType, unsigned>> ReverseList;
3869 // we need to traverse backwards
3870 // Visit the base classes.
3871 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
3872 if (CXXRD->getNumBases() > 0) {
3873 assert(CXXRD->getNumBases() == 1);
3874 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
3875 ReverseList.emplace_back(Args: &Res->getStructBase(i: 0), Args: BS.getType(), Args: 0u);
3876 }
3877 }
3878
3879 // Visit the fields.
3880 for (FieldDecl *FD : RD->fields()) {
3881 unsigned FDBW = 0;
3882 if (FD->isUnnamedBitField())
3883 continue;
3884 if (FD->isBitField()) {
3885 FDBW = FD->getBitWidthValue();
3886 }
3887
3888 ReverseList.emplace_back(Args: &Res->getStructField(i: FD->getFieldIndex()),
3889 Args: FD->getType(), Args&: FDBW);
3890 }
3891
3892 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
3893 llvm::append_range(C&: WorkList, R&: ReverseList);
3894 continue;
3895 }
3896 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
3897 return false;
3898 }
3899 return true;
3900}
3901
3902static bool handleElementwiseCast(EvalInfo &Info, const Expr *E,
3903 const FPOptions FPO,
3904 SmallVectorImpl<APValue> &Elements,
3905 SmallVectorImpl<QualType> &SrcTypes,
3906 SmallVectorImpl<QualType> &DestTypes,
3907 SmallVectorImpl<APValue> &Results) {
3908
3909 assert((Elements.size() == SrcTypes.size()) &&
3910 (Elements.size() == DestTypes.size()));
3911
3912 for (unsigned I = 0, ESz = Elements.size(); I < ESz; ++I) {
3913 APValue Original = Elements[I];
3914 QualType SourceTy = SrcTypes[I];
3915 QualType DestTy = DestTypes[I];
3916
3917 if (!handleScalarCast(Info, FPO, E, SourceTy, DestTy, Original, Result&: Results[I]))
3918 return false;
3919 }
3920 return true;
3921}
3922
3923static unsigned elementwiseSize(EvalInfo &Info, QualType BaseTy) {
3924
3925 SmallVector<QualType> WorkList = {BaseTy};
3926
3927 unsigned Size = 0;
3928 while (!WorkList.empty()) {
3929 QualType Type = WorkList.pop_back_val();
3930 if (Type->isRealFloatingType() || Type->isIntegerType() ||
3931 Type->isBooleanType()) {
3932 ++Size;
3933 continue;
3934 }
3935 if (Type->isVectorType()) {
3936 unsigned NumEl = Type->castAs<VectorType>()->getNumElements();
3937 Size += NumEl;
3938 continue;
3939 }
3940 if (Type->isConstantMatrixType()) {
3941 unsigned NumEl =
3942 Type->castAs<ConstantMatrixType>()->getNumElementsFlattened();
3943 Size += NumEl;
3944 continue;
3945 }
3946 if (Type->isConstantArrayType()) {
3947 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
3948 ->getElementType();
3949 uint64_t ArrSize =
3950 cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))->getZExtSize();
3951 for (uint64_t I = 0; I < ArrSize; ++I) {
3952 WorkList.push_back(Elt: ElTy);
3953 }
3954 continue;
3955 }
3956 if (Type->isRecordType()) {
3957 const RecordDecl *RD = Type->getAsRecordDecl();
3958
3959 // Visit the base classes.
3960 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
3961 if (CXXRD->getNumBases() > 0) {
3962 assert(CXXRD->getNumBases() == 1);
3963 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
3964 WorkList.push_back(Elt: BS.getType());
3965 }
3966 }
3967
3968 // visit the fields.
3969 for (FieldDecl *FD : RD->fields()) {
3970 if (FD->isUnnamedBitField())
3971 continue;
3972 WorkList.push_back(Elt: FD->getType());
3973 }
3974 continue;
3975 }
3976 }
3977 return Size;
3978}
3979
3980static bool hlslAggSplatHelper(EvalInfo &Info, const Expr *E, APValue &SrcVal,
3981 QualType &SrcTy) {
3982 SrcTy = E->getType();
3983
3984 if (!Evaluate(Result&: SrcVal, Info, E))
3985 return false;
3986
3987 assert((SrcVal.isFloat() || SrcVal.isInt() ||
3988 (SrcVal.isVector() && SrcVal.getVectorLength() == 1)) &&
3989 "Not a valid HLSLAggregateSplatCast.");
3990
3991 if (SrcVal.isVector()) {
3992 assert(SrcTy->isVectorType() && "Type mismatch.");
3993 SrcTy = SrcTy->castAs<VectorType>()->getElementType();
3994 SrcVal = SrcVal.getVectorElt(I: 0);
3995 }
3996 if (SrcVal.isMatrix()) {
3997 assert(SrcTy->isConstantMatrixType() && "Type mismatch.");
3998 SrcTy = SrcTy->castAs<ConstantMatrixType>()->getElementType();
3999 SrcVal = SrcVal.getMatrixElt(Row: 0, Col: 0);
4000 }
4001 return true;
4002}
4003
4004static bool flattenAPValue(EvalInfo &Info, const Expr *E, APValue Value,
4005 QualType BaseTy, SmallVectorImpl<APValue> &Elements,
4006 SmallVectorImpl<QualType> &Types, unsigned Size) {
4007
4008 SmallVector<std::pair<APValue, QualType>> WorkList = {{Value, BaseTy}};
4009 unsigned Populated = 0;
4010 while (!WorkList.empty() && Populated < Size) {
4011 auto [Work, Type] = WorkList.pop_back_val();
4012
4013 if (Work.isFloat() || Work.isInt()) {
4014 Elements.push_back(Elt: Work);
4015 Types.push_back(Elt: Type);
4016 Populated++;
4017 continue;
4018 }
4019 if (Work.isVector()) {
4020 assert(Type->isVectorType() && "Type mismatch.");
4021 QualType ElTy = Type->castAs<VectorType>()->getElementType();
4022 for (unsigned I = 0; I < Work.getVectorLength() && Populated < Size;
4023 I++) {
4024 Elements.push_back(Elt: Work.getVectorElt(I));
4025 Types.push_back(Elt: ElTy);
4026 Populated++;
4027 }
4028 continue;
4029 }
4030 if (Work.isMatrix()) {
4031 assert(Type->isConstantMatrixType() && "Type mismatch.");
4032 const auto *MT = Type->castAs<ConstantMatrixType>();
4033 QualType ElTy = MT->getElementType();
4034 // Matrix elements are flattened in row-major order.
4035 for (unsigned Row = 0; Row < Work.getMatrixNumRows() && Populated < Size;
4036 Row++) {
4037 for (unsigned Col = 0;
4038 Col < Work.getMatrixNumColumns() && Populated < Size; Col++) {
4039 Elements.push_back(Elt: Work.getMatrixElt(Row, Col));
4040 Types.push_back(Elt: ElTy);
4041 Populated++;
4042 }
4043 }
4044 continue;
4045 }
4046 if (Work.isArray()) {
4047 assert(Type->isConstantArrayType() && "Type mismatch.");
4048 QualType ElTy = cast<ConstantArrayType>(Val: Info.Ctx.getAsArrayType(T: Type))
4049 ->getElementType();
4050 for (int64_t I = Work.getArraySize() - 1; I > -1; --I) {
4051 WorkList.emplace_back(Args&: Work.getArrayInitializedElt(I), Args&: ElTy);
4052 }
4053 continue;
4054 }
4055
4056 if (Work.isStruct()) {
4057 assert(Type->isRecordType() && "Type mismatch.");
4058
4059 const RecordDecl *RD = Type->getAsRecordDecl();
4060
4061 SmallVector<std::pair<APValue, QualType>> ReverseList;
4062 // Visit the fields.
4063 for (FieldDecl *FD : RD->fields()) {
4064 if (FD->isUnnamedBitField())
4065 continue;
4066 ReverseList.emplace_back(Args&: Work.getStructField(i: FD->getFieldIndex()),
4067 Args: FD->getType());
4068 }
4069
4070 std::reverse(first: ReverseList.begin(), last: ReverseList.end());
4071 llvm::append_range(C&: WorkList, R&: ReverseList);
4072
4073 // Visit the base classes.
4074 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
4075 if (CXXRD->getNumBases() > 0) {
4076 assert(CXXRD->getNumBases() == 1);
4077 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[0];
4078 const APValue &Base = Work.getStructBase(i: 0);
4079
4080 // Can happen in error cases.
4081 if (!Base.isStruct())
4082 return false;
4083
4084 WorkList.emplace_back(Args: Base, Args: BS.getType());
4085 }
4086 }
4087 continue;
4088 }
4089 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4090 return false;
4091 }
4092 return true;
4093}
4094
4095namespace {
4096/// A handle to a complete object (an object that is not a subobject of
4097/// another object).
4098struct CompleteObject {
4099 /// The identity of the object.
4100 APValue::LValueBase Base;
4101 /// The value of the complete object.
4102 APValue *Value;
4103 /// The type of the complete object.
4104 QualType Type;
4105
4106 CompleteObject() : Value(nullptr) {}
4107 CompleteObject(APValue::LValueBase Base, APValue *Value, QualType Type)
4108 : Base(Base), Value(Value), Type(Type) {}
4109
4110 bool mayAccessMutableMembers(EvalInfo &Info, AccessKinds AK) const {
4111 // If this isn't a "real" access (eg, if it's just accessing the type
4112 // info), allow it. We assume the type doesn't change dynamically for
4113 // subobjects of constexpr objects (even though we'd hit UB here if it
4114 // did). FIXME: Is this right?
4115 if (!isAnyAccess(AK))
4116 return true;
4117
4118 // In C++14 onwards, it is permitted to read a mutable member whose
4119 // lifetime began within the evaluation.
4120 // FIXME: Should we also allow this in C++11?
4121 if (!Info.getLangOpts().CPlusPlus14 &&
4122 AK != AccessKinds::AK_IsWithinLifetime)
4123 return false;
4124 return lifetimeStartedInEvaluation(Info, Base, /*MutableSubobject*/true);
4125 }
4126
4127 explicit operator bool() const { return !Type.isNull(); }
4128};
4129} // end anonymous namespace
4130
4131static QualType getSubobjectType(QualType ObjType, QualType SubobjType,
4132 bool IsMutable = false) {
4133 // C++ [basic.type.qualifier]p1:
4134 // - A const object is an object of type const T or a non-mutable subobject
4135 // of a const object.
4136 if (ObjType.isConstQualified() && !IsMutable)
4137 SubobjType.addConst();
4138 // - A volatile object is an object of type const T or a subobject of a
4139 // volatile object.
4140 if (ObjType.isVolatileQualified())
4141 SubobjType.addVolatile();
4142 return SubobjType;
4143}
4144
4145/// Find the designated sub-object of an rvalue.
4146template <typename SubobjectHandler>
4147static typename SubobjectHandler::result_type
4148findSubobject(EvalInfo &Info, const Expr *E, const CompleteObject &Obj,
4149 const SubobjectDesignator &Sub, SubobjectHandler &handler) {
4150 if (Sub.Invalid)
4151 // A diagnostic will have already been produced.
4152 return handler.failed();
4153 if (Sub.isOnePastTheEnd() || Sub.isMostDerivedAnUnsizedArray()) {
4154 if (Info.getLangOpts().CPlusPlus11)
4155 Info.FFDiag(E, DiagId: Sub.isOnePastTheEnd()
4156 ? diag::note_constexpr_access_past_end
4157 : diag::note_constexpr_access_unsized_array)
4158 << handler.AccessKind;
4159 else
4160 Info.FFDiag(E);
4161 return handler.failed();
4162 }
4163
4164 APValue *O = Obj.Value;
4165 QualType ObjType = Obj.Type;
4166 const FieldDecl *LastField = nullptr;
4167 const FieldDecl *VolatileField = nullptr;
4168
4169 // Walk the designator's path to find the subobject.
4170 for (unsigned I = 0, N = Sub.Entries.size(); /**/; ++I) {
4171 // Reading an indeterminate value is undefined, but assigning over one is OK.
4172 if ((O->isAbsent() && !(handler.AccessKind == AK_Construct && I == N)) ||
4173 (O->isIndeterminate() &&
4174 !isValidIndeterminateAccess(handler.AccessKind))) {
4175 // Object has ended lifetime.
4176 // If I is non-zero, some subobject (member or array element) of a
4177 // complete object has ended its lifetime, so this is valid for
4178 // IsWithinLifetime, resulting in false.
4179 if (I != 0 && handler.AccessKind == AK_IsWithinLifetime)
4180 return false;
4181 if (!Info.checkingPotentialConstantExpression()) {
4182 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
4183 << handler.AccessKind << O->isIndeterminate()
4184 << E->getSourceRange();
4185 NoteLValueLocation(Info, Base: Obj.Base);
4186 }
4187 return handler.failed();
4188 }
4189
4190 // C++ [class.ctor]p5, C++ [class.dtor]p5:
4191 // const and volatile semantics are not applied on an object under
4192 // {con,de}struction.
4193 if ((ObjType.isConstQualified() || ObjType.isVolatileQualified()) &&
4194 ObjType->isRecordType() &&
4195 Info.isEvaluatingCtorDtor(
4196 Base: Obj.Base, Path: ArrayRef(Sub.Entries.begin(), Sub.Entries.begin() + I)) !=
4197 ConstructionPhase::None) {
4198 ObjType = Info.Ctx.getCanonicalType(T: ObjType);
4199 ObjType.removeLocalConst();
4200 ObjType.removeLocalVolatile();
4201 }
4202
4203 // If this is our last pass, check that the final object type is OK.
4204 if (I == N || (I == N - 1 && ObjType->isAnyComplexType())) {
4205 // Accesses to volatile objects are prohibited.
4206 if (ObjType.isVolatileQualified() && isFormalAccess(handler.AccessKind)) {
4207 if (Info.getLangOpts().CPlusPlus) {
4208 int DiagKind;
4209 SourceLocation Loc;
4210 const NamedDecl *Decl = nullptr;
4211 if (VolatileField) {
4212 DiagKind = 2;
4213 Loc = VolatileField->getLocation();
4214 Decl = VolatileField;
4215 } else if (auto *VD = Obj.Base.dyn_cast<const ValueDecl*>()) {
4216 DiagKind = 1;
4217 Loc = VD->getLocation();
4218 Decl = VD;
4219 } else {
4220 DiagKind = 0;
4221 if (auto *E = Obj.Base.dyn_cast<const Expr *>())
4222 Loc = E->getExprLoc();
4223 }
4224 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_obj, ExtraNotes: 1)
4225 << handler.AccessKind << DiagKind << Decl;
4226 Info.Note(Loc, DiagId: diag::note_constexpr_volatile_here) << DiagKind;
4227 } else {
4228 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
4229 }
4230 return handler.failed();
4231 }
4232
4233 // If we are reading an object of class type, there may still be more
4234 // things we need to check: if there are any mutable subobjects, we
4235 // cannot perform this read. (This only happens when performing a trivial
4236 // copy or assignment.)
4237 if (ObjType->isRecordType() &&
4238 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind) &&
4239 diagnoseMutableFields(Info, E, handler.AccessKind, ObjType))
4240 return handler.failed();
4241 }
4242
4243 if (I == N) {
4244 if (!handler.found(*O, ObjType, Obj.Base))
4245 return false;
4246
4247 // If we modified a bit-field, truncate it to the right width.
4248 if (isModification(handler.AccessKind) &&
4249 LastField && LastField->isBitField() &&
4250 !truncateBitfieldValue(Info, E, Value&: *O, FD: LastField))
4251 return false;
4252
4253 return true;
4254 }
4255
4256 LastField = nullptr;
4257 if (ObjType->isArrayType()) {
4258 // Next subobject is an array element.
4259 const ArrayType *AT = Info.Ctx.getAsArrayType(T: ObjType);
4260 assert((isa<ConstantArrayType>(AT) || isa<IncompleteArrayType>(AT)) &&
4261 "vla in literal type?");
4262 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4263 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4264 CAT && CAT->getSize().ule(RHS: Index)) {
4265 // Note, it should not be possible to form a pointer with a valid
4266 // designator which points more than one past the end of the array.
4267 if (Info.getLangOpts().CPlusPlus11)
4268 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4269 << handler.AccessKind;
4270 else
4271 Info.FFDiag(E);
4272 return handler.failed();
4273 }
4274
4275 ObjType = AT->getElementType();
4276
4277 if (O->getArrayInitializedElts() > Index)
4278 O = &O->getArrayInitializedElt(I: Index);
4279 else if (!isRead(handler.AccessKind)) {
4280 if (const auto *CAT = dyn_cast<ConstantArrayType>(Val: AT);
4281 CAT && !CheckArraySize(Info, CAT, CallLoc: E->getExprLoc()))
4282 return handler.failed();
4283
4284 expandArray(Array&: *O, Index);
4285 O = &O->getArrayInitializedElt(I: Index);
4286 } else
4287 O = &O->getArrayFiller();
4288 } else if (ObjType->isAnyComplexType()) {
4289 // Next subobject is a complex number.
4290 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4291 if (Index > 1) {
4292 if (Info.getLangOpts().CPlusPlus11)
4293 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4294 << handler.AccessKind;
4295 else
4296 Info.FFDiag(E);
4297 return handler.failed();
4298 }
4299
4300 ObjType = getSubobjectType(
4301 ObjType, SubobjType: ObjType->castAs<ComplexType>()->getElementType());
4302
4303 assert(I == N - 1 && "extracting subobject of scalar?");
4304 if (O->isComplexInt()) {
4305 return handler.found(Index ? O->getComplexIntImag()
4306 : O->getComplexIntReal(), ObjType);
4307 } else {
4308 assert(O->isComplexFloat());
4309 return handler.found(Index ? O->getComplexFloatImag()
4310 : O->getComplexFloatReal(), ObjType);
4311 }
4312 } else if (const auto *VT = ObjType->getAs<VectorType>()) {
4313 uint64_t Index = Sub.Entries[I].getAsArrayIndex();
4314 unsigned NumElements = VT->getNumElements();
4315 if (Index == NumElements) {
4316 if (Info.getLangOpts().CPlusPlus11)
4317 Info.FFDiag(E, DiagId: diag::note_constexpr_access_past_end)
4318 << handler.AccessKind;
4319 else
4320 Info.FFDiag(E);
4321 return handler.failed();
4322 }
4323
4324 if (Index > NumElements) {
4325 Info.CCEDiag(E, DiagId: diag::note_constexpr_array_index)
4326 << Index << /*array*/ 0 << NumElements;
4327 return handler.failed();
4328 }
4329
4330 ObjType = VT->getElementType();
4331 assert(I == N - 1 && "extracting subobject of scalar?");
4332
4333 if (O->isIndeterminate()) {
4334 if (isRead(handler.AccessKind)) {
4335 Info.FFDiag(E);
4336 return handler.failed();
4337 }
4338 expandVector(Vec&: *O, NumElements);
4339 }
4340 assert(O->isVector() && "unexpected object during vector element access");
4341 return handler.found(O->getVectorElt(I: Index), ObjType, Obj.Base);
4342 } else if (const FieldDecl *Field = getAsField(E: Sub.Entries[I])) {
4343 if (Field->isMutable() &&
4344 !Obj.mayAccessMutableMembers(Info, AK: handler.AccessKind)) {
4345 Info.FFDiag(E, DiagId: diag::note_constexpr_access_mutable, ExtraNotes: 1)
4346 << handler.AccessKind << Field;
4347 Info.Note(Loc: Field->getLocation(), DiagId: diag::note_declared_at);
4348 return handler.failed();
4349 }
4350
4351 // Next subobject is a class, struct or union field.
4352 RecordDecl *RD = ObjType->castAsCanonical<RecordType>()->getDecl();
4353 if (RD->isUnion()) {
4354 const FieldDecl *UnionField = O->getUnionField();
4355 if (!UnionField ||
4356 UnionField->getCanonicalDecl() != Field->getCanonicalDecl()) {
4357 if (I == N - 1 && handler.AccessKind == AK_Construct) {
4358 // Placement new onto an inactive union member makes it active.
4359 O->setUnion(Field, Value: APValue());
4360 } else {
4361 // Pointer to/into inactive union member: Not within lifetime
4362 if (handler.AccessKind == AK_IsWithinLifetime)
4363 return false;
4364 // FIXME: If O->getUnionValue() is absent, report that there's no
4365 // active union member rather than reporting the prior active union
4366 // member. We'll need to fix nullptr_t to not use APValue() as its
4367 // representation first.
4368 Info.FFDiag(E, DiagId: diag::note_constexpr_access_inactive_union_member)
4369 << handler.AccessKind << Field << !UnionField << UnionField;
4370 return handler.failed();
4371 }
4372 }
4373 O = &O->getUnionValue();
4374 } else
4375 O = &O->getStructField(i: Field->getFieldIndex());
4376
4377 ObjType = getSubobjectType(ObjType, SubobjType: Field->getType(), IsMutable: Field->isMutable());
4378 LastField = Field;
4379 if (Field->getType().isVolatileQualified())
4380 VolatileField = Field;
4381 } else {
4382 // Next subobject is a base class.
4383 const CXXRecordDecl *Derived = ObjType->getAsCXXRecordDecl();
4384 const CXXRecordDecl *Base = getAsBaseClass(E: Sub.Entries[I]);
4385 O = &O->getStructBase(i: getBaseIndex(Derived, Base));
4386
4387 ObjType = getSubobjectType(ObjType, SubobjType: Info.Ctx.getCanonicalTagType(TD: Base));
4388 }
4389 }
4390}
4391
4392namespace {
4393struct ExtractSubobjectHandler {
4394 EvalInfo &Info;
4395 const Expr *E;
4396 APValue &Result;
4397 const AccessKinds AccessKind;
4398
4399 typedef bool result_type;
4400 bool failed() { return false; }
4401 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4402 Result = Subobj;
4403 if (AccessKind == AK_ReadObjectRepresentation)
4404 return true;
4405 return CheckFullyInitialized(Info, DiagLoc: E->getExprLoc(), Type: SubobjType, Value: Result);
4406 }
4407 bool found(APSInt &Value, QualType SubobjType) {
4408 Result = APValue(Value);
4409 return true;
4410 }
4411 bool found(APFloat &Value, QualType SubobjType) {
4412 Result = APValue(Value);
4413 return true;
4414 }
4415};
4416} // end anonymous namespace
4417
4418/// Extract the designated sub-object of an rvalue.
4419static bool extractSubobject(EvalInfo &Info, const Expr *E,
4420 const CompleteObject &Obj,
4421 const SubobjectDesignator &Sub, APValue &Result,
4422 AccessKinds AK = AK_Read) {
4423 assert(AK == AK_Read || AK == AK_ReadObjectRepresentation);
4424 ExtractSubobjectHandler Handler = {.Info: Info, .E: E, .Result: Result, .AccessKind: AK};
4425 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4426}
4427
4428namespace {
4429struct ModifySubobjectHandler {
4430 EvalInfo &Info;
4431 APValue &NewVal;
4432 const Expr *E;
4433
4434 typedef bool result_type;
4435 static const AccessKinds AccessKind = AK_Assign;
4436
4437 bool checkConst(QualType QT) {
4438 // Assigning to a const object has undefined behavior.
4439 if (QT.isConstQualified()) {
4440 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
4441 return false;
4442 }
4443 return true;
4444 }
4445
4446 bool failed() { return false; }
4447 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4448 if (!checkConst(QT: SubobjType))
4449 return false;
4450 // We've been given ownership of NewVal, so just swap it in.
4451 Subobj.swap(RHS&: NewVal);
4452 return true;
4453 }
4454 bool found(APSInt &Value, QualType SubobjType) {
4455 if (!checkConst(QT: SubobjType))
4456 return false;
4457 if (!NewVal.isInt()) {
4458 // Maybe trying to write a cast pointer value into a complex?
4459 Info.FFDiag(E);
4460 return false;
4461 }
4462 Value = NewVal.getInt();
4463 return true;
4464 }
4465 bool found(APFloat &Value, QualType SubobjType) {
4466 if (!checkConst(QT: SubobjType))
4467 return false;
4468 Value = NewVal.getFloat();
4469 return true;
4470 }
4471};
4472} // end anonymous namespace
4473
4474const AccessKinds ModifySubobjectHandler::AccessKind;
4475
4476/// Update the designated sub-object of an rvalue to the given value.
4477static bool modifySubobject(EvalInfo &Info, const Expr *E,
4478 const CompleteObject &Obj,
4479 const SubobjectDesignator &Sub,
4480 APValue &NewVal) {
4481 ModifySubobjectHandler Handler = { .Info: Info, .NewVal: NewVal, .E: E };
4482 return findSubobject(Info, E, Obj, Sub, handler&: Handler);
4483}
4484
4485/// Find the position where two subobject designators diverge, or equivalently
4486/// the length of the common initial subsequence.
4487static unsigned FindDesignatorMismatch(QualType ObjType,
4488 const SubobjectDesignator &A,
4489 const SubobjectDesignator &B,
4490 bool &WasArrayIndex) {
4491 unsigned I = 0, N = std::min(a: A.Entries.size(), b: B.Entries.size());
4492 for (/**/; I != N; ++I) {
4493 if (!ObjType.isNull() &&
4494 (ObjType->isArrayType() || ObjType->isAnyComplexType())) {
4495 // Next subobject is an array element.
4496 if (A.Entries[I].getAsArrayIndex() != B.Entries[I].getAsArrayIndex()) {
4497 WasArrayIndex = true;
4498 return I;
4499 }
4500 if (ObjType->isAnyComplexType())
4501 ObjType = ObjType->castAs<ComplexType>()->getElementType();
4502 else
4503 ObjType = ObjType->castAsArrayTypeUnsafe()->getElementType();
4504 } else {
4505 if (A.Entries[I].getAsBaseOrMember() !=
4506 B.Entries[I].getAsBaseOrMember()) {
4507 WasArrayIndex = false;
4508 return I;
4509 }
4510 if (const FieldDecl *FD = getAsField(E: A.Entries[I]))
4511 // Next subobject is a field.
4512 ObjType = FD->getType();
4513 else
4514 // Next subobject is a base class.
4515 ObjType = QualType();
4516 }
4517 }
4518 WasArrayIndex = false;
4519 return I;
4520}
4521
4522/// Determine whether the given subobject designators refer to elements of the
4523/// same array object.
4524static bool AreElementsOfSameArray(QualType ObjType,
4525 const SubobjectDesignator &A,
4526 const SubobjectDesignator &B) {
4527 if (A.Entries.size() != B.Entries.size())
4528 return false;
4529
4530 bool IsArray = A.MostDerivedIsArrayElement;
4531 if (IsArray && A.MostDerivedPathLength != A.Entries.size())
4532 // A is a subobject of the array element.
4533 return false;
4534
4535 // If A (and B) designates an array element, the last entry will be the array
4536 // index. That doesn't have to match. Otherwise, we're in the 'implicit array
4537 // of length 1' case, and the entire path must match.
4538 bool WasArrayIndex;
4539 unsigned CommonLength = FindDesignatorMismatch(ObjType, A, B, WasArrayIndex);
4540 return CommonLength >= A.Entries.size() - IsArray;
4541}
4542
4543/// Find the complete object to which an LValue refers.
4544static CompleteObject findCompleteObject(EvalInfo &Info, const Expr *E,
4545 AccessKinds AK, const LValue &LVal,
4546 QualType LValType) {
4547 if (LVal.InvalidBase) {
4548 Info.FFDiag(E);
4549 return CompleteObject();
4550 }
4551
4552 if (!LVal.Base) {
4553 if (AK == AccessKinds::AK_Dereference)
4554 Info.FFDiag(E, DiagId: diag::note_constexpr_dereferencing_null);
4555 else
4556 Info.FFDiag(E, DiagId: diag::note_constexpr_access_null) << AK;
4557 return CompleteObject();
4558 }
4559
4560 CallStackFrame *Frame = nullptr;
4561 unsigned Depth = 0;
4562 if (LVal.getLValueCallIndex()) {
4563 std::tie(args&: Frame, args&: Depth) =
4564 Info.getCallFrameAndDepth(CallIndex: LVal.getLValueCallIndex());
4565 if (!Frame) {
4566 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit, ExtraNotes: 1)
4567 << AK << /*Indeterminate=*/false << E->getSourceRange();
4568 NoteLValueLocation(Info, Base: LVal.Base);
4569 return CompleteObject();
4570 }
4571 }
4572
4573 bool IsAccess = isAnyAccess(AK);
4574
4575 // C++11 DR1311: An lvalue-to-rvalue conversion on a volatile-qualified type
4576 // is not a constant expression (even if the object is non-volatile). We also
4577 // apply this rule to C++98, in order to conform to the expected 'volatile'
4578 // semantics.
4579 if (isFormalAccess(AK) && LValType.isVolatileQualified()) {
4580 if (Info.getLangOpts().CPlusPlus)
4581 Info.FFDiag(E, DiagId: diag::note_constexpr_access_volatile_type)
4582 << AK << LValType;
4583 else
4584 Info.FFDiag(E);
4585 return CompleteObject();
4586 }
4587
4588 // Compute value storage location and type of base object.
4589 APValue *BaseVal = nullptr;
4590 QualType BaseType = getType(B: LVal.Base);
4591
4592 if (Info.getLangOpts().CPlusPlus14 && LVal.Base == Info.EvaluatingDecl &&
4593 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4594 // This is the object whose initializer we're evaluating, so its lifetime
4595 // started in the current evaluation.
4596 BaseVal = Info.EvaluatingDeclValue;
4597 } else if (const ValueDecl *D = LVal.Base.dyn_cast<const ValueDecl *>()) {
4598 // Allow reading from a GUID declaration.
4599 if (auto *GD = dyn_cast<MSGuidDecl>(Val: D)) {
4600 if (isModification(AK)) {
4601 // All the remaining cases do not permit modification of the object.
4602 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4603 return CompleteObject();
4604 }
4605 APValue &V = GD->getAsAPValue();
4606 if (V.isAbsent()) {
4607 Info.FFDiag(E, DiagId: diag::note_constexpr_unsupported_layout)
4608 << GD->getType();
4609 return CompleteObject();
4610 }
4611 return CompleteObject(LVal.Base, &V, GD->getType());
4612 }
4613
4614 // Allow reading the APValue from an UnnamedGlobalConstantDecl.
4615 if (auto *GCD = dyn_cast<UnnamedGlobalConstantDecl>(Val: D)) {
4616 if (isModification(AK)) {
4617 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4618 return CompleteObject();
4619 }
4620 return CompleteObject(LVal.Base, const_cast<APValue *>(&GCD->getValue()),
4621 GCD->getType());
4622 }
4623
4624 // Allow reading from template parameter objects.
4625 if (auto *TPO = dyn_cast<TemplateParamObjectDecl>(Val: D)) {
4626 if (isModification(AK)) {
4627 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4628 return CompleteObject();
4629 }
4630 return CompleteObject(LVal.Base, const_cast<APValue *>(&TPO->getValue()),
4631 TPO->getType());
4632 }
4633
4634 // In C++98, const, non-volatile integers initialized with ICEs are ICEs.
4635 // In C++11, constexpr, non-volatile variables initialized with constant
4636 // expressions are constant expressions too. Inside constexpr functions,
4637 // parameters are constant expressions even if they're non-const.
4638 // In C++1y, objects local to a constant expression (those with a Frame) are
4639 // both readable and writable inside constant expressions.
4640 // In C, such things can also be folded, although they are not ICEs.
4641 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
4642 if (VD) {
4643 if (const VarDecl *VDef = VD->getDefinition(C&: Info.Ctx))
4644 VD = VDef;
4645 }
4646 if (!VD || VD->isInvalidDecl()) {
4647 Info.FFDiag(E);
4648 return CompleteObject();
4649 }
4650
4651 bool IsConstant = BaseType.isConstant(Ctx: Info.Ctx);
4652 bool ConstexprVar = false;
4653 if (const auto *VD = dyn_cast_if_present<VarDecl>(
4654 Val: Info.EvaluatingDecl.dyn_cast<const ValueDecl *>()))
4655 ConstexprVar = VD->isConstexpr();
4656
4657 // Unless we're looking at a local variable or argument in a constexpr call,
4658 // the variable we're reading must be const (unless we are binding to a
4659 // reference).
4660 if (AK != clang::AK_Dereference && !Frame) {
4661 if (IsAccess && isa<ParmVarDecl>(Val: VD)) {
4662 // Access of a parameter that's not associated with a frame isn't going
4663 // to work out, but we can leave it to evaluateVarDeclInit to provide a
4664 // suitable diagnostic.
4665 } else if (Info.getLangOpts().CPlusPlus14 &&
4666 lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4667 // OK, we can read and modify an object if we're in the process of
4668 // evaluating its initializer, because its lifetime began in this
4669 // evaluation.
4670 } else if (isModification(AK)) {
4671 // All the remaining cases do not permit modification of the object.
4672 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_global);
4673 return CompleteObject();
4674 } else if (VD->isConstexpr()) {
4675 // OK, we can read this variable.
4676 } else if (Info.getLangOpts().C23 && ConstexprVar) {
4677 Info.FFDiag(E);
4678 return CompleteObject();
4679 } else if (BaseType->isIntegralOrEnumerationType()) {
4680 if (!IsConstant) {
4681 if (!IsAccess)
4682 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4683 if (Info.getLangOpts().CPlusPlus) {
4684 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_non_const_int, ExtraNotes: 1) << VD;
4685 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4686 } else {
4687 Info.FFDiag(E);
4688 }
4689 return CompleteObject();
4690 }
4691 } else if (!IsAccess) {
4692 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4693 } else if ((IsConstant || BaseType->isReferenceType()) &&
4694 Info.checkingPotentialConstantExpression() &&
4695 BaseType->isLiteralType(Ctx: Info.Ctx) && !VD->hasDefinition()) {
4696 // This variable might end up being constexpr. Don't diagnose it yet.
4697 } else if (IsConstant) {
4698 // Keep evaluating to see what we can do. In particular, we support
4699 // folding of const floating-point types, in order to make static const
4700 // data members of such types (supported as an extension) more useful.
4701 if (Info.getLangOpts().CPlusPlus) {
4702 Info.CCEDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4703 ? diag::note_constexpr_ltor_non_constexpr
4704 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4705 << VD << BaseType;
4706 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4707 } else {
4708 Info.CCEDiag(E);
4709 }
4710 } else {
4711 // Never allow reading a non-const value.
4712 if (Info.getLangOpts().CPlusPlus) {
4713 Info.FFDiag(E, DiagId: Info.getLangOpts().CPlusPlus11
4714 ? diag::note_constexpr_ltor_non_constexpr
4715 : diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
4716 << VD << BaseType;
4717 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4718 } else {
4719 Info.FFDiag(E);
4720 }
4721 return CompleteObject();
4722 }
4723 }
4724
4725 // When binding to a reference, the variable does not need to be constexpr
4726 // or have constant initalization.
4727 if (AK != clang::AK_Dereference &&
4728 !evaluateVarDeclInit(Info, E, VD, Frame, Version: LVal.getLValueVersion(),
4729 Result&: BaseVal))
4730 return CompleteObject();
4731 // If evaluateVarDeclInit sees a constexpr-unknown variable, it returns
4732 // a null BaseVal. Any constexpr-unknown variable seen here is an error:
4733 // we can't access a constexpr-unknown object.
4734 if (AK != clang::AK_Dereference && !BaseVal) {
4735 if (!Info.checkingPotentialConstantExpression()) {
4736 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unknown_variable, ExtraNotes: 1)
4737 << AK << VD;
4738 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
4739 }
4740 return CompleteObject();
4741 }
4742 } else if (DynamicAllocLValue DA = LVal.Base.dyn_cast<DynamicAllocLValue>()) {
4743 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
4744 if (!Alloc) {
4745 Info.FFDiag(E, DiagId: diag::note_constexpr_access_deleted_object) << AK;
4746 return CompleteObject();
4747 }
4748 return CompleteObject(LVal.Base, &(*Alloc)->Value,
4749 LVal.Base.getDynamicAllocType());
4750 }
4751 // When binding to a reference, the variable does not need to be
4752 // within its lifetime.
4753 else if (AK != clang::AK_Dereference) {
4754 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4755
4756 if (!Frame) {
4757 if (const MaterializeTemporaryExpr *MTE =
4758 dyn_cast_or_null<MaterializeTemporaryExpr>(Val: Base)) {
4759 assert(MTE->getStorageDuration() == SD_Static &&
4760 "should have a frame for a non-global materialized temporary");
4761
4762 // C++20 [expr.const]p4: [DR2126]
4763 // An object or reference is usable in constant expressions if it is
4764 // - a temporary object of non-volatile const-qualified literal type
4765 // whose lifetime is extended to that of a variable that is usable
4766 // in constant expressions
4767 //
4768 // C++20 [expr.const]p5:
4769 // an lvalue-to-rvalue conversion [is not allowed unless it applies to]
4770 // - a non-volatile glvalue that refers to an object that is usable
4771 // in constant expressions, or
4772 // - a non-volatile glvalue of literal type that refers to a
4773 // non-volatile object whose lifetime began within the evaluation
4774 // of E;
4775 //
4776 // C++11 misses the 'began within the evaluation of e' check and
4777 // instead allows all temporaries, including things like:
4778 // int &&r = 1;
4779 // int x = ++r;
4780 // constexpr int k = r;
4781 // Therefore we use the C++14-onwards rules in C++11 too.
4782 //
4783 // Note that temporaries whose lifetimes began while evaluating a
4784 // variable's constructor are not usable while evaluating the
4785 // corresponding destructor, not even if they're of const-qualified
4786 // types.
4787 if (!MTE->isUsableInConstantExpressions(Context: Info.Ctx) &&
4788 !lifetimeStartedInEvaluation(Info, Base: LVal.Base)) {
4789 if (!IsAccess)
4790 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4791 Info.FFDiag(E, DiagId: diag::note_constexpr_access_static_temporary, ExtraNotes: 1) << AK;
4792 Info.Note(Loc: MTE->getExprLoc(), DiagId: diag::note_constexpr_temporary_here);
4793 return CompleteObject();
4794 }
4795
4796 BaseVal = MTE->getOrCreateValue(MayCreate: false);
4797 assert(BaseVal && "got reference to unevaluated temporary");
4798 } else if (const CompoundLiteralExpr *CLE =
4799 dyn_cast_or_null<CompoundLiteralExpr>(Val: Base)) {
4800 // According to GCC info page:
4801 //
4802 // 6.28 Compound Literals
4803 //
4804 // As an optimization, G++ sometimes gives array compound literals
4805 // longer lifetimes: when the array either appears outside a function or
4806 // has a const-qualified type. If foo and its initializer had elements
4807 // of type char *const rather than char *, or if foo were a global
4808 // variable, the array would have static storage duration. But it is
4809 // probably safest just to avoid the use of array compound literals in
4810 // C++ code.
4811 //
4812 // Obey that rule by checking constness for converted array types.
4813 if (QualType CLETy = CLE->getType(); CLETy->isArrayType() &&
4814 !LValType->isArrayType() &&
4815 !CLETy.isConstant(Ctx: Info.Ctx)) {
4816 Info.FFDiag(E);
4817 Info.Note(Loc: CLE->getExprLoc(), DiagId: diag::note_declared_at);
4818 return CompleteObject();
4819 }
4820
4821 BaseVal = &CLE->getStaticValue();
4822 } else {
4823 if (!IsAccess)
4824 return CompleteObject(LVal.getLValueBase(), nullptr, BaseType);
4825 APValue Val;
4826 LVal.moveInto(V&: Val);
4827 Info.FFDiag(E, DiagId: diag::note_constexpr_access_unreadable_object)
4828 << AK
4829 << Val.getAsString(Ctx: Info.Ctx,
4830 Ty: Info.Ctx.getLValueReferenceType(T: LValType));
4831 NoteLValueLocation(Info, Base: LVal.Base);
4832 return CompleteObject();
4833 }
4834 } else if (AK != clang::AK_Dereference) {
4835 BaseVal = Frame->getTemporary(Key: Base, Version: LVal.Base.getVersion());
4836 assert(BaseVal && "missing value for temporary");
4837 }
4838 }
4839
4840 // In C++14, we can't safely access any mutable state when we might be
4841 // evaluating after an unmodeled side effect. Parameters are modeled as state
4842 // in the caller, but aren't visible once the call returns, so they can be
4843 // modified in a speculatively-evaluated call.
4844 //
4845 // FIXME: Not all local state is mutable. Allow local constant subobjects
4846 // to be read here (but take care with 'mutable' fields).
4847 unsigned VisibleDepth = Depth;
4848 if (llvm::isa_and_nonnull<ParmVarDecl>(
4849 Val: LVal.Base.dyn_cast<const ValueDecl *>()))
4850 ++VisibleDepth;
4851 if ((Frame && Info.getLangOpts().CPlusPlus14 &&
4852 Info.EvalStatus.HasSideEffects) ||
4853 (isModification(AK) && VisibleDepth < Info.SpeculativeEvaluationDepth))
4854 return CompleteObject();
4855
4856 return CompleteObject(LVal.getLValueBase(), BaseVal, BaseType);
4857}
4858
4859/// Perform an lvalue-to-rvalue conversion on the given glvalue. This
4860/// can also be used for 'lvalue-to-lvalue' conversions for looking up the
4861/// glvalue referred to by an entity of reference type.
4862///
4863/// \param Info - Information about the ongoing evaluation.
4864/// \param Conv - The expression for which we are performing the conversion.
4865/// Used for diagnostics.
4866/// \param Type - The type of the glvalue (before stripping cv-qualifiers in the
4867/// case of a non-class type).
4868/// \param LVal - The glvalue on which we are attempting to perform this action.
4869/// \param RVal - The produced value will be placed here.
4870/// \param WantObjectRepresentation - If true, we're looking for the object
4871/// representation rather than the value, and in particular,
4872/// there is no requirement that the result be fully initialized.
4873static bool
4874handleLValueToRValueConversion(EvalInfo &Info, const Expr *Conv, QualType Type,
4875 const LValue &LVal, APValue &RVal,
4876 bool WantObjectRepresentation = false) {
4877 if (LVal.Designator.Invalid)
4878 return false;
4879
4880 // Check for special cases where there is no existing APValue to look at.
4881 const Expr *Base = LVal.Base.dyn_cast<const Expr*>();
4882
4883 AccessKinds AK =
4884 WantObjectRepresentation ? AK_ReadObjectRepresentation : AK_Read;
4885
4886 if (Base && !LVal.getLValueCallIndex() && !Type.isVolatileQualified()) {
4887 if (isa<StringLiteral>(Val: Base) || isa<PredefinedExpr>(Val: Base)) {
4888 // Special-case character extraction so we don't have to construct an
4889 // APValue for the whole string.
4890 assert(LVal.Designator.Entries.size() <= 1 &&
4891 "Can only read characters from string literals");
4892 if (LVal.Designator.Entries.empty()) {
4893 // Fail for now for LValue to RValue conversion of an array.
4894 // (This shouldn't show up in C/C++, but it could be triggered by a
4895 // weird EvaluateAsRValue call from a tool.)
4896 Info.FFDiag(E: Conv);
4897 return false;
4898 }
4899 if (LVal.Designator.isOnePastTheEnd()) {
4900 if (Info.getLangOpts().CPlusPlus11)
4901 Info.FFDiag(E: Conv, DiagId: diag::note_constexpr_access_past_end) << AK;
4902 else
4903 Info.FFDiag(E: Conv);
4904 return false;
4905 }
4906 uint64_t CharIndex = LVal.Designator.Entries[0].getAsArrayIndex();
4907 RVal = APValue(extractStringLiteralCharacter(Info, Lit: Base, Index: CharIndex));
4908 return true;
4909 }
4910 }
4911
4912 CompleteObject Obj = findCompleteObject(Info, E: Conv, AK, LVal, LValType: Type);
4913 return Obj && extractSubobject(Info, E: Conv, Obj, Sub: LVal.Designator, Result&: RVal, AK);
4914}
4915
4916static bool hlslElementwiseCastHelper(EvalInfo &Info, const Expr *E,
4917 QualType DestTy,
4918 SmallVectorImpl<APValue> &SrcVals,
4919 SmallVectorImpl<QualType> &SrcTypes) {
4920 APValue Val;
4921 if (!Evaluate(Result&: Val, Info, E))
4922 return false;
4923
4924 // must be dealing with a record
4925 if (Val.isLValue()) {
4926 LValue LVal;
4927 LVal.setFrom(Ctx: Info.Ctx, V: Val);
4928 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal, RVal&: Val))
4929 return false;
4930 }
4931
4932 unsigned NEls = elementwiseSize(Info, BaseTy: DestTy);
4933 // flatten the source
4934 if (!flattenAPValue(Info, E, Value: Val, BaseTy: E->getType(), Elements&: SrcVals, Types&: SrcTypes, Size: NEls))
4935 return false;
4936
4937 return true;
4938}
4939
4940/// Perform an assignment of Val to LVal. Takes ownership of Val.
4941static bool handleAssignment(EvalInfo &Info, const Expr *E, const LValue &LVal,
4942 QualType LValType, APValue &Val) {
4943 if (LVal.Designator.Invalid)
4944 return false;
4945
4946 if (!Info.getLangOpts().CPlusPlus14) {
4947 Info.FFDiag(E);
4948 return false;
4949 }
4950
4951 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
4952 return Obj && modifySubobject(Info, E, Obj, Sub: LVal.Designator, NewVal&: Val);
4953}
4954
4955namespace {
4956struct CompoundAssignSubobjectHandler {
4957 EvalInfo &Info;
4958 const CompoundAssignOperator *E;
4959 QualType PromotedLHSType;
4960 BinaryOperatorKind Opcode;
4961 const APValue &RHS;
4962
4963 static const AccessKinds AccessKind = AK_Assign;
4964
4965 typedef bool result_type;
4966
4967 bool checkConst(QualType QT) {
4968 // Assigning to a const object has undefined behavior.
4969 if (QT.isConstQualified()) {
4970 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
4971 return false;
4972 }
4973 return true;
4974 }
4975
4976 bool failed() { return false; }
4977 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
4978 switch (Subobj.getKind()) {
4979 case APValue::Int:
4980 return found(Value&: Subobj.getInt(), SubobjType);
4981 case APValue::Float:
4982 return found(Value&: Subobj.getFloat(), SubobjType);
4983 case APValue::ComplexInt:
4984 case APValue::ComplexFloat:
4985 // FIXME: Implement complex compound assignment.
4986 Info.FFDiag(E);
4987 return false;
4988 case APValue::LValue:
4989 return foundPointer(Subobj, SubobjType);
4990 case APValue::Vector:
4991 return foundVector(Value&: Subobj, SubobjType);
4992 case APValue::Indeterminate:
4993 Info.FFDiag(E, DiagId: diag::note_constexpr_access_uninit)
4994 << /*read of=*/0 << /*uninitialized object=*/1
4995 << E->getLHS()->getSourceRange();
4996 NoteLValueLocation(Info, Base);
4997 return false;
4998 default:
4999 // FIXME: can this happen?
5000 Info.FFDiag(E);
5001 return false;
5002 }
5003 }
5004
5005 bool foundVector(APValue &Value, QualType SubobjType) {
5006 if (!checkConst(QT: SubobjType))
5007 return false;
5008
5009 if (!SubobjType->isVectorType()) {
5010 Info.FFDiag(E);
5011 return false;
5012 }
5013 return handleVectorVectorBinOp(Info, E, Opcode, LHSValue&: Value, RHSValue: RHS);
5014 }
5015
5016 bool found(APSInt &Value, QualType SubobjType) {
5017 if (!checkConst(QT: SubobjType))
5018 return false;
5019
5020 if (!SubobjType->isIntegerType()) {
5021 // We don't support compound assignment on integer-cast-to-pointer
5022 // values.
5023 Info.FFDiag(E);
5024 return false;
5025 }
5026
5027 if (RHS.isInt()) {
5028 APSInt LHS =
5029 HandleIntToIntCast(Info, E, DestType: PromotedLHSType, SrcType: SubobjType, Value);
5030 if (!handleIntIntBinOp(Info, E, LHS, Opcode, RHS: RHS.getInt(), Result&: LHS))
5031 return false;
5032 Value = HandleIntToIntCast(Info, E, DestType: SubobjType, SrcType: PromotedLHSType, Value: LHS);
5033 return true;
5034 } else if (RHS.isFloat()) {
5035 const FPOptions FPO = E->getFPFeaturesInEffect(
5036 LO: Info.Ctx.getLangOpts());
5037 APFloat FValue(0.0);
5038 return HandleIntToFloatCast(Info, E, FPO, SrcType: SubobjType, Value,
5039 DestType: PromotedLHSType, Result&: FValue) &&
5040 handleFloatFloatBinOp(Info, E, LHS&: FValue, Opcode, RHS: RHS.getFloat()) &&
5041 HandleFloatToIntCast(Info, E, SrcType: PromotedLHSType, Value: FValue, DestType: SubobjType,
5042 Result&: Value);
5043 }
5044
5045 Info.FFDiag(E);
5046 return false;
5047 }
5048 bool found(APFloat &Value, QualType SubobjType) {
5049 return checkConst(QT: SubobjType) &&
5050 HandleFloatToFloatCast(Info, E, SrcType: SubobjType, DestType: PromotedLHSType,
5051 Result&: Value) &&
5052 handleFloatFloatBinOp(Info, E, LHS&: Value, Opcode, RHS: RHS.getFloat()) &&
5053 HandleFloatToFloatCast(Info, E, SrcType: PromotedLHSType, DestType: SubobjType, Result&: Value);
5054 }
5055 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5056 if (!checkConst(QT: SubobjType))
5057 return false;
5058
5059 QualType PointeeType;
5060 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5061 PointeeType = PT->getPointeeType();
5062
5063 if (PointeeType.isNull() || !RHS.isInt() ||
5064 (Opcode != BO_Add && Opcode != BO_Sub)) {
5065 Info.FFDiag(E);
5066 return false;
5067 }
5068
5069 APSInt Offset = RHS.getInt();
5070 if (Opcode == BO_Sub)
5071 negateAsSigned(Int&: Offset);
5072
5073 LValue LVal;
5074 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5075 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType, Adjustment: Offset))
5076 return false;
5077 LVal.moveInto(V&: Subobj);
5078 return true;
5079 }
5080};
5081} // end anonymous namespace
5082
5083const AccessKinds CompoundAssignSubobjectHandler::AccessKind;
5084
5085/// Perform a compound assignment of LVal <op>= RVal.
5086static bool handleCompoundAssignment(EvalInfo &Info,
5087 const CompoundAssignOperator *E,
5088 const LValue &LVal, QualType LValType,
5089 QualType PromotedLValType,
5090 BinaryOperatorKind Opcode,
5091 const APValue &RVal) {
5092 if (LVal.Designator.Invalid)
5093 return false;
5094
5095 if (!Info.getLangOpts().CPlusPlus14) {
5096 Info.FFDiag(E);
5097 return false;
5098 }
5099
5100 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Assign, LVal, LValType);
5101 CompoundAssignSubobjectHandler Handler = { .Info: Info, .E: E, .PromotedLHSType: PromotedLValType, .Opcode: Opcode,
5102 .RHS: RVal };
5103 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5104}
5105
5106namespace {
5107struct IncDecSubobjectHandler {
5108 EvalInfo &Info;
5109 const UnaryOperator *E;
5110 AccessKinds AccessKind;
5111 APValue *Old;
5112
5113 typedef bool result_type;
5114
5115 bool checkConst(QualType QT) {
5116 // Assigning to a const object has undefined behavior.
5117 if (QT.isConstQualified()) {
5118 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
5119 return false;
5120 }
5121 return true;
5122 }
5123
5124 bool failed() { return false; }
5125 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
5126 // Stash the old value. Also clear Old, so we don't clobber it later
5127 // if we're post-incrementing a complex.
5128 if (Old) {
5129 *Old = Subobj;
5130 Old = nullptr;
5131 }
5132
5133 switch (Subobj.getKind()) {
5134 case APValue::Int:
5135 return found(Value&: Subobj.getInt(), SubobjType);
5136 case APValue::Float:
5137 return found(Value&: Subobj.getFloat(), SubobjType);
5138 case APValue::ComplexInt:
5139 return found(Value&: Subobj.getComplexIntReal(),
5140 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5141 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5142 case APValue::ComplexFloat:
5143 return found(Value&: Subobj.getComplexFloatReal(),
5144 SubobjType: SubobjType->castAs<ComplexType>()->getElementType()
5145 .withCVRQualifiers(CVR: SubobjType.getCVRQualifiers()));
5146 case APValue::LValue:
5147 return foundPointer(Subobj, SubobjType);
5148 default:
5149 // FIXME: can this happen?
5150 Info.FFDiag(E);
5151 return false;
5152 }
5153 }
5154 bool found(APSInt &Value, QualType SubobjType) {
5155 if (!checkConst(QT: SubobjType))
5156 return false;
5157
5158 if (!SubobjType->isIntegerType()) {
5159 // We don't support increment / decrement on integer-cast-to-pointer
5160 // values.
5161 Info.FFDiag(E);
5162 return false;
5163 }
5164
5165 if (Old) *Old = APValue(Value);
5166
5167 // bool arithmetic promotes to int, and the conversion back to bool
5168 // doesn't reduce mod 2^n, so special-case it.
5169 if (SubobjType->isBooleanType()) {
5170 if (AccessKind == AK_Increment)
5171 Value = 1;
5172 else
5173 Value = !Value;
5174 return true;
5175 }
5176
5177 bool WasNegative = Value.isNegative();
5178 if (AccessKind == AK_Increment) {
5179 ++Value;
5180
5181 if (!WasNegative && Value.isNegative() && E->canOverflow() &&
5182 !SubobjType.isWrapType()) {
5183 APSInt ActualValue(Value, /*IsUnsigned*/true);
5184 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5185 }
5186 } else {
5187 --Value;
5188
5189 if (WasNegative && !Value.isNegative() && E->canOverflow() &&
5190 !SubobjType.isWrapType()) {
5191 unsigned BitWidth = Value.getBitWidth();
5192 APSInt ActualValue(Value.sext(width: BitWidth + 1), /*IsUnsigned*/false);
5193 ActualValue.setBit(BitWidth);
5194 return HandleOverflow(Info, E, SrcValue: ActualValue, DestType: SubobjType);
5195 }
5196 }
5197 return true;
5198 }
5199 bool found(APFloat &Value, QualType SubobjType) {
5200 if (!checkConst(QT: SubobjType))
5201 return false;
5202
5203 if (Old) *Old = APValue(Value);
5204
5205 APFloat One(Value.getSemantics(), 1);
5206 llvm::RoundingMode RM = getActiveRoundingMode(Info, E);
5207 APFloat::opStatus St;
5208 if (AccessKind == AK_Increment)
5209 St = Value.add(RHS: One, RM);
5210 else
5211 St = Value.subtract(RHS: One, RM);
5212 return checkFloatingPointResult(Info, E, St);
5213 }
5214 bool foundPointer(APValue &Subobj, QualType SubobjType) {
5215 if (!checkConst(QT: SubobjType))
5216 return false;
5217
5218 QualType PointeeType;
5219 if (const PointerType *PT = SubobjType->getAs<PointerType>())
5220 PointeeType = PT->getPointeeType();
5221 else {
5222 Info.FFDiag(E);
5223 return false;
5224 }
5225
5226 LValue LVal;
5227 LVal.setFrom(Ctx: Info.Ctx, V: Subobj);
5228 if (!HandleLValueArrayAdjustment(Info, E, LVal, EltTy: PointeeType,
5229 Adjustment: AccessKind == AK_Increment ? 1 : -1))
5230 return false;
5231 LVal.moveInto(V&: Subobj);
5232 return true;
5233 }
5234};
5235} // end anonymous namespace
5236
5237/// Perform an increment or decrement on LVal.
5238static bool handleIncDec(EvalInfo &Info, const Expr *E, const LValue &LVal,
5239 QualType LValType, bool IsIncrement, APValue *Old) {
5240 if (LVal.Designator.Invalid)
5241 return false;
5242
5243 if (!Info.getLangOpts().CPlusPlus14) {
5244 Info.FFDiag(E);
5245 return false;
5246 }
5247
5248 AccessKinds AK = IsIncrement ? AK_Increment : AK_Decrement;
5249 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal, LValType);
5250 IncDecSubobjectHandler Handler = {.Info: Info, .E: cast<UnaryOperator>(Val: E), .AccessKind: AK, .Old: Old};
5251 return Obj && findSubobject(Info, E, Obj, Sub: LVal.Designator, handler&: Handler);
5252}
5253
5254/// Build an lvalue for the object argument of a member function call.
5255static bool EvaluateObjectArgument(EvalInfo &Info, const Expr *Object,
5256 LValue &This) {
5257 if (Object->getType()->isPointerType() && Object->isPRValue())
5258 return EvaluatePointer(E: Object, Result&: This, Info);
5259
5260 if (Object->isGLValue())
5261 return EvaluateLValue(E: Object, Result&: This, Info);
5262
5263 if (Object->getType()->isLiteralType(Ctx: Info.Ctx))
5264 return EvaluateTemporary(E: Object, Result&: This, Info);
5265
5266 if (Object->getType()->isRecordType() && Object->isPRValue())
5267 return EvaluateTemporary(E: Object, Result&: This, Info);
5268
5269 Info.FFDiag(E: Object, DiagId: diag::note_constexpr_nonliteral) << Object->getType();
5270 return false;
5271}
5272
5273/// HandleMemberPointerAccess - Evaluate a member access operation and build an
5274/// lvalue referring to the result.
5275///
5276/// \param Info - Information about the ongoing evaluation.
5277/// \param LV - An lvalue referring to the base of the member pointer.
5278/// \param RHS - The member pointer expression.
5279/// \param IncludeMember - Specifies whether the member itself is included in
5280/// the resulting LValue subobject designator. This is not possible when
5281/// creating a bound member function.
5282/// \return The field or method declaration to which the member pointer refers,
5283/// or 0 if evaluation fails.
5284static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5285 QualType LVType,
5286 LValue &LV,
5287 const Expr *RHS,
5288 bool IncludeMember = true) {
5289 MemberPtr MemPtr;
5290 if (!EvaluateMemberPointer(E: RHS, Result&: MemPtr, Info))
5291 return nullptr;
5292
5293 // C++11 [expr.mptr.oper]p6: If the second operand is the null pointer to
5294 // member value, the behavior is undefined.
5295 if (!MemPtr.getDecl()) {
5296 // FIXME: Specific diagnostic.
5297 Info.FFDiag(E: RHS);
5298 return nullptr;
5299 }
5300
5301 if (MemPtr.isDerivedMember()) {
5302 // This is a member of some derived class. Truncate LV appropriately.
5303 // The end of the derived-to-base path for the base object must match the
5304 // derived-to-base path for the member pointer.
5305 // C++23 [expr.mptr.oper]p4:
5306 // If the result of E1 is an object [...] whose most derived object does
5307 // not contain the member to which E2 refers, the behavior is undefined.
5308 if (LV.Designator.MostDerivedPathLength + MemPtr.Path.size() >
5309 LV.Designator.Entries.size()) {
5310 Info.FFDiag(E: RHS);
5311 return nullptr;
5312 }
5313 unsigned PathLengthToMember =
5314 LV.Designator.Entries.size() - MemPtr.Path.size();
5315 for (unsigned I = 0, N = MemPtr.Path.size(); I != N; ++I) {
5316 const CXXRecordDecl *LVDecl = getAsBaseClass(
5317 E: LV.Designator.Entries[PathLengthToMember + I]);
5318 const CXXRecordDecl *MPDecl = MemPtr.Path[I];
5319 if (LVDecl->getCanonicalDecl() != MPDecl->getCanonicalDecl()) {
5320 Info.FFDiag(E: RHS);
5321 return nullptr;
5322 }
5323 }
5324 // MemPtr.Path only contains the base classes of the class directly
5325 // containing the member E2. It is still necessary to check that the class
5326 // directly containing the member E2 lies on the derived-to-base path of E1
5327 // to avoid incorrectly permitting member pointer access into a sibling
5328 // class of the class containing the member E2. If this class would
5329 // correspond to the most-derived class of E1, it either isn't contained in
5330 // LV.Designator.Entries or the corresponding entry refers to an array
5331 // element instead. Therefore get the most derived class directly in this
5332 // case. Otherwise the previous entry should correpond to this class.
5333 const CXXRecordDecl *LastLVDecl =
5334 (PathLengthToMember > LV.Designator.MostDerivedPathLength)
5335 ? getAsBaseClass(E: LV.Designator.Entries[PathLengthToMember - 1])
5336 : LV.Designator.MostDerivedType->getAsCXXRecordDecl();
5337 const CXXRecordDecl *LastMPDecl = MemPtr.getContainingRecord();
5338 if (LastLVDecl->getCanonicalDecl() != LastMPDecl->getCanonicalDecl()) {
5339 Info.FFDiag(E: RHS);
5340 return nullptr;
5341 }
5342
5343 // Truncate the lvalue to the appropriate derived class.
5344 if (!CastToDerivedClass(Info, E: RHS, Result&: LV, TruncatedType: MemPtr.getContainingRecord(),
5345 TruncatedElements: PathLengthToMember))
5346 return nullptr;
5347 } else if (!MemPtr.Path.empty()) {
5348 // Extend the LValue path with the member pointer's path.
5349 LV.Designator.Entries.reserve(N: LV.Designator.Entries.size() +
5350 MemPtr.Path.size() + IncludeMember);
5351
5352 // Walk down to the appropriate base class.
5353 if (const PointerType *PT = LVType->getAs<PointerType>())
5354 LVType = PT->getPointeeType();
5355 const CXXRecordDecl *RD = LVType->getAsCXXRecordDecl();
5356 assert(RD && "member pointer access on non-class-type expression");
5357 // The first class in the path is that of the lvalue.
5358 for (unsigned I = 1, N = MemPtr.Path.size(); I != N; ++I) {
5359 const CXXRecordDecl *Base = MemPtr.Path[N - I - 1];
5360 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD, Base))
5361 return nullptr;
5362 RD = Base;
5363 }
5364 // Finally cast to the class containing the member.
5365 if (!HandleLValueDirectBase(Info, E: RHS, Obj&: LV, Derived: RD,
5366 Base: MemPtr.getContainingRecord()))
5367 return nullptr;
5368 }
5369
5370 // Add the member. Note that we cannot build bound member functions here.
5371 if (IncludeMember) {
5372 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: MemPtr.getDecl())) {
5373 if (!HandleLValueMember(Info, E: RHS, LVal&: LV, FD))
5374 return nullptr;
5375 } else if (const IndirectFieldDecl *IFD =
5376 dyn_cast<IndirectFieldDecl>(Val: MemPtr.getDecl())) {
5377 if (!HandleLValueIndirectMember(Info, E: RHS, LVal&: LV, IFD))
5378 return nullptr;
5379 } else {
5380 llvm_unreachable("can't construct reference to bound member function");
5381 }
5382 }
5383
5384 return MemPtr.getDecl();
5385}
5386
5387static const ValueDecl *HandleMemberPointerAccess(EvalInfo &Info,
5388 const BinaryOperator *BO,
5389 LValue &LV,
5390 bool IncludeMember = true) {
5391 assert(BO->getOpcode() == BO_PtrMemD || BO->getOpcode() == BO_PtrMemI);
5392
5393 if (!EvaluateObjectArgument(Info, Object: BO->getLHS(), This&: LV)) {
5394 if (Info.noteFailure()) {
5395 MemberPtr MemPtr;
5396 EvaluateMemberPointer(E: BO->getRHS(), Result&: MemPtr, Info);
5397 }
5398 return nullptr;
5399 }
5400
5401 return HandleMemberPointerAccess(Info, LVType: BO->getLHS()->getType(), LV,
5402 RHS: BO->getRHS(), IncludeMember);
5403}
5404
5405/// HandleBaseToDerivedCast - Apply the given base-to-derived cast operation on
5406/// the provided lvalue, which currently refers to the base object.
5407static bool HandleBaseToDerivedCast(EvalInfo &Info, const CastExpr *E,
5408 LValue &Result) {
5409 SubobjectDesignator &D = Result.Designator;
5410 if (D.Invalid || !Result.checkNullPointer(Info, E, CSK: CSK_Derived))
5411 return false;
5412
5413 QualType TargetQT = E->getType();
5414 if (const PointerType *PT = TargetQT->getAs<PointerType>())
5415 TargetQT = PT->getPointeeType();
5416
5417 auto InvalidCast = [&]() {
5418 if (!Info.checkingPotentialConstantExpression() ||
5419 !Result.AllowConstexprUnknown) {
5420 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_downcast)
5421 << D.MostDerivedType << TargetQT;
5422 }
5423 return false;
5424 };
5425
5426 // Check this cast lands within the final derived-to-base subobject path.
5427 if (D.MostDerivedPathLength + E->path_size() > D.Entries.size())
5428 return InvalidCast();
5429
5430 // Check the type of the final cast. We don't need to check the path,
5431 // since a cast can only be formed if the path is unique.
5432 unsigned NewEntriesSize = D.Entries.size() - E->path_size();
5433 const CXXRecordDecl *TargetType = TargetQT->getAsCXXRecordDecl();
5434 const CXXRecordDecl *FinalType;
5435 if (NewEntriesSize == D.MostDerivedPathLength)
5436 FinalType = D.MostDerivedType->getAsCXXRecordDecl();
5437 else
5438 FinalType = getAsBaseClass(E: D.Entries[NewEntriesSize - 1]);
5439 if (FinalType->getCanonicalDecl() != TargetType->getCanonicalDecl())
5440 return InvalidCast();
5441
5442 // Truncate the lvalue to the appropriate derived class.
5443 return CastToDerivedClass(Info, E, Result, TruncatedType: TargetType, TruncatedElements: NewEntriesSize);
5444}
5445
5446/// Get the value to use for a default-initialized object of type T.
5447/// Return false if it encounters something invalid.
5448static bool handleDefaultInitValue(QualType T, APValue &Result) {
5449 bool Success = true;
5450
5451 // If there is already a value present don't overwrite it.
5452 if (!Result.isAbsent())
5453 return true;
5454
5455 if (auto *RD = T->getAsCXXRecordDecl()) {
5456 if (RD->isInvalidDecl()) {
5457 Result = APValue();
5458 return false;
5459 }
5460 if (RD->isUnion()) {
5461 Result = APValue((const FieldDecl *)nullptr);
5462 return true;
5463 }
5464 Result =
5465 APValue(APValue::UninitStruct(), RD->getNumBases(), RD->getNumFields());
5466
5467 unsigned Index = 0;
5468 for (CXXRecordDecl::base_class_const_iterator I = RD->bases_begin(),
5469 End = RD->bases_end();
5470 I != End; ++I, ++Index)
5471 Success &=
5472 handleDefaultInitValue(T: I->getType(), Result&: Result.getStructBase(i: Index));
5473
5474 for (const auto *I : RD->fields()) {
5475 if (I->isUnnamedBitField())
5476 continue;
5477 Success &= handleDefaultInitValue(
5478 T: I->getType(), Result&: Result.getStructField(i: I->getFieldIndex()));
5479 }
5480 return Success;
5481 }
5482
5483 if (auto *AT =
5484 dyn_cast_or_null<ConstantArrayType>(Val: T->getAsArrayTypeUnsafe())) {
5485 Result = APValue(APValue::UninitArray(), 0, AT->getZExtSize());
5486 if (Result.hasArrayFiller())
5487 Success &=
5488 handleDefaultInitValue(T: AT->getElementType(), Result&: Result.getArrayFiller());
5489
5490 return Success;
5491 }
5492
5493 Result = APValue::IndeterminateValue();
5494 return true;
5495}
5496
5497namespace {
5498enum EvalStmtResult {
5499 /// Evaluation failed.
5500 ESR_Failed,
5501 /// Hit a 'return' statement.
5502 ESR_Returned,
5503 /// Evaluation succeeded.
5504 ESR_Succeeded,
5505 /// Hit a 'continue' statement.
5506 ESR_Continue,
5507 /// Hit a 'break' statement.
5508 ESR_Break,
5509 /// Still scanning for 'case' or 'default' statement.
5510 ESR_CaseNotFound
5511};
5512}
5513/// Evaluates the initializer of a reference.
5514static bool EvaluateInitForDeclOfReferenceType(EvalInfo &Info,
5515 const ValueDecl *D,
5516 const Expr *Init, LValue &Result,
5517 APValue &Val) {
5518 assert(Init->isGLValue() && D->getType()->isReferenceType());
5519 // A reference is an lvalue.
5520 if (!EvaluateLValue(E: Init, Result, Info))
5521 return false;
5522 // [C++26][decl.ref]
5523 // The object designated by such a glvalue can be outside its lifetime
5524 // Because a null pointer value or a pointer past the end of an object
5525 // does not point to an object, a reference in a well-defined program cannot
5526 // refer to such things;
5527 if (!Result.Designator.Invalid && Result.Designator.isOnePastTheEnd()) {
5528 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_access_past_end) << AK_Dereference;
5529 return false;
5530 }
5531
5532 // Save the result.
5533 Result.moveInto(V&: Val);
5534 return true;
5535}
5536
5537static bool EvaluateVarDecl(EvalInfo &Info, const VarDecl *VD) {
5538 if (VD->isInvalidDecl())
5539 return false;
5540 // We don't need to evaluate the initializer for a static local.
5541 if (!VD->hasLocalStorage())
5542 return true;
5543
5544 LValue Result;
5545 APValue &Val = Info.CurrentCall->createTemporary(Key: VD, T: VD->getType(),
5546 Scope: ScopeKind::Block, LV&: Result);
5547
5548 const Expr *InitE = VD->getInit();
5549 if (!InitE) {
5550 if (VD->getType()->isDependentType())
5551 return Info.noteSideEffect();
5552 return handleDefaultInitValue(T: VD->getType(), Result&: Val);
5553 }
5554 if (InitE->isValueDependent())
5555 return false;
5556
5557 // For references to objects, check they do not designate a one-past-the-end
5558 // object.
5559 if (VD->getType()->isReferenceType()) {
5560 return EvaluateInitForDeclOfReferenceType(Info, D: VD, Init: InitE, Result, Val);
5561 } else if (!EvaluateInPlace(Result&: Val, Info, This: Result, E: InitE)) {
5562 // Wipe out any partially-computed value, to allow tracking that this
5563 // evaluation failed.
5564 Val = APValue();
5565 return false;
5566 }
5567
5568 return true;
5569}
5570
5571static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5572 const DecompositionDecl *DD);
5573
5574static bool EvaluateDecl(EvalInfo &Info, const Decl *D,
5575 bool EvaluateConditionDecl = false) {
5576 bool OK = true;
5577 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
5578 OK &= EvaluateVarDecl(Info, VD);
5579
5580 if (const DecompositionDecl *DD = dyn_cast<DecompositionDecl>(Val: D);
5581 EvaluateConditionDecl && DD)
5582 OK &= EvaluateDecompositionDeclInit(Info, DD);
5583
5584 return OK;
5585}
5586
5587static bool EvaluateDecompositionDeclInit(EvalInfo &Info,
5588 const DecompositionDecl *DD) {
5589 bool OK = true;
5590 for (auto *BD : DD->flat_bindings())
5591 if (auto *VD = BD->getHoldingVar())
5592 OK &= EvaluateDecl(Info, D: VD, /*EvaluateConditionDecl=*/true);
5593
5594 return OK;
5595}
5596
5597static bool MaybeEvaluateDeferredVarDeclInit(EvalInfo &Info,
5598 const VarDecl *VD) {
5599 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(Val: VD)) {
5600 if (!EvaluateDecompositionDeclInit(Info, DD))
5601 return false;
5602 }
5603 return true;
5604}
5605
5606static bool EvaluateDependentExpr(const Expr *E, EvalInfo &Info) {
5607 assert(E->isValueDependent());
5608 if (Info.noteSideEffect())
5609 return true;
5610 assert(E->containsErrors() && "valid value-dependent expression should never "
5611 "reach invalid code path.");
5612 return false;
5613}
5614
5615/// Evaluate a condition (either a variable declaration or an expression).
5616static bool EvaluateCond(EvalInfo &Info, const VarDecl *CondDecl,
5617 const Expr *Cond, bool &Result) {
5618 if (Cond->isValueDependent())
5619 return false;
5620 FullExpressionRAII Scope(Info);
5621 if (CondDecl && !EvaluateDecl(Info, D: CondDecl))
5622 return false;
5623 if (!EvaluateAsBooleanCondition(E: Cond, Result, Info))
5624 return false;
5625 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: CondDecl))
5626 return false;
5627 return Scope.destroy();
5628}
5629
5630namespace {
5631/// A location where the result (returned value) of evaluating a
5632/// statement should be stored.
5633struct StmtResult {
5634 /// The APValue that should be filled in with the returned value.
5635 APValue &Value;
5636 /// The location containing the result, if any (used to support RVO).
5637 const LValue *Slot;
5638};
5639
5640struct TempVersionRAII {
5641 CallStackFrame &Frame;
5642
5643 TempVersionRAII(CallStackFrame &Frame) : Frame(Frame) {
5644 Frame.pushTempVersion();
5645 }
5646
5647 ~TempVersionRAII() {
5648 Frame.popTempVersion();
5649 }
5650};
5651
5652}
5653
5654static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5655 const Stmt *S,
5656 const SwitchCase *SC = nullptr);
5657
5658/// Helper to implement named break/continue. Returns 'true' if the evaluation
5659/// result should be propagated up. Otherwise, it sets the evaluation result
5660/// to either Continue to continue the current loop, or Succeeded to break it.
5661static bool ShouldPropagateBreakContinue(EvalInfo &Info,
5662 const Stmt *LoopOrSwitch,
5663 ArrayRef<BlockScopeRAII *> Scopes,
5664 EvalStmtResult &ESR) {
5665 bool IsSwitch = isa<SwitchStmt>(Val: LoopOrSwitch);
5666
5667 // For loops, map Succeeded to Continue so we don't have to check for both.
5668 if (!IsSwitch && ESR == ESR_Succeeded) {
5669 ESR = ESR_Continue;
5670 return false;
5671 }
5672
5673 if (ESR != ESR_Break && ESR != ESR_Continue)
5674 return false;
5675
5676 // Are we breaking out of or continuing this statement?
5677 bool CanBreakOrContinue = !IsSwitch || ESR == ESR_Break;
5678 const Stmt *StackTop = Info.BreakContinueStack.back();
5679 if (CanBreakOrContinue && (StackTop == nullptr || StackTop == LoopOrSwitch)) {
5680 Info.BreakContinueStack.pop_back();
5681 if (ESR == ESR_Break)
5682 ESR = ESR_Succeeded;
5683 return false;
5684 }
5685
5686 // We're not. Propagate the result up.
5687 for (BlockScopeRAII *S : Scopes) {
5688 if (!S->destroy()) {
5689 ESR = ESR_Failed;
5690 break;
5691 }
5692 }
5693 return true;
5694}
5695
5696/// Evaluate the body of a loop, and translate the result as appropriate.
5697static EvalStmtResult EvaluateLoopBody(StmtResult &Result, EvalInfo &Info,
5698 const Stmt *Body,
5699 const SwitchCase *Case = nullptr) {
5700 BlockScopeRAII Scope(Info);
5701
5702 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Body, SC: Case);
5703 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5704 ESR = ESR_Failed;
5705
5706 return ESR;
5707}
5708
5709/// Evaluate a switch statement.
5710static EvalStmtResult EvaluateSwitch(StmtResult &Result, EvalInfo &Info,
5711 const SwitchStmt *SS) {
5712 BlockScopeRAII Scope(Info);
5713
5714 // Evaluate the switch condition.
5715 APSInt Value;
5716 {
5717 if (const Stmt *Init = SS->getInit()) {
5718 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
5719 if (ESR != ESR_Succeeded) {
5720 if (ESR != ESR_Failed && !Scope.destroy())
5721 ESR = ESR_Failed;
5722 return ESR;
5723 }
5724 }
5725
5726 FullExpressionRAII CondScope(Info);
5727 if (SS->getConditionVariable() &&
5728 !EvaluateDecl(Info, D: SS->getConditionVariable()))
5729 return ESR_Failed;
5730 if (SS->getCond()->isValueDependent()) {
5731 // We don't know what the value is, and which branch should jump to.
5732 EvaluateDependentExpr(E: SS->getCond(), Info);
5733 return ESR_Failed;
5734 }
5735 if (!EvaluateInteger(E: SS->getCond(), Result&: Value, Info))
5736 return ESR_Failed;
5737
5738 if (!MaybeEvaluateDeferredVarDeclInit(Info, VD: SS->getConditionVariable()))
5739 return ESR_Failed;
5740
5741 if (!CondScope.destroy())
5742 return ESR_Failed;
5743 }
5744
5745 // Find the switch case corresponding to the value of the condition.
5746 // FIXME: Cache this lookup.
5747 const SwitchCase *Found = nullptr;
5748 for (const SwitchCase *SC = SS->getSwitchCaseList(); SC;
5749 SC = SC->getNextSwitchCase()) {
5750 if (isa<DefaultStmt>(Val: SC)) {
5751 Found = SC;
5752 continue;
5753 }
5754
5755 const CaseStmt *CS = cast<CaseStmt>(Val: SC);
5756 const Expr *LHS = CS->getLHS();
5757 const Expr *RHS = CS->getRHS();
5758 if (LHS->isValueDependent() || (RHS && RHS->isValueDependent()))
5759 return ESR_Failed;
5760 APSInt LHSValue = LHS->EvaluateKnownConstInt(Ctx: Info.Ctx);
5761 APSInt RHSValue = RHS ? RHS->EvaluateKnownConstInt(Ctx: Info.Ctx) : LHSValue;
5762 if (LHSValue <= Value && Value <= RHSValue) {
5763 Found = SC;
5764 break;
5765 }
5766 }
5767
5768 if (!Found)
5769 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
5770
5771 // Search the switch body for the switch case and evaluate it from there.
5772 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SS->getBody(), SC: Found);
5773 if (ESR != ESR_Failed && ESR != ESR_CaseNotFound && !Scope.destroy())
5774 return ESR_Failed;
5775 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: SS, /*Scopes=*/{}, ESR))
5776 return ESR;
5777
5778 switch (ESR) {
5779 case ESR_Break:
5780 llvm_unreachable("Should have been converted to Succeeded");
5781 case ESR_Succeeded:
5782 case ESR_Continue:
5783 case ESR_Failed:
5784 case ESR_Returned:
5785 return ESR;
5786 case ESR_CaseNotFound:
5787 // This can only happen if the switch case is nested within a statement
5788 // expression. We have no intention of supporting that.
5789 Info.FFDiag(Loc: Found->getBeginLoc(),
5790 DiagId: diag::note_constexpr_stmt_expr_unsupported);
5791 return ESR_Failed;
5792 }
5793 llvm_unreachable("Invalid EvalStmtResult!");
5794}
5795
5796static bool CheckLocalVariableDeclaration(EvalInfo &Info, const VarDecl *VD) {
5797 // An expression E is a core constant expression unless the evaluation of E
5798 // would evaluate one of the following: [C++23] - a control flow that passes
5799 // through a declaration of a variable with static or thread storage duration
5800 // unless that variable is usable in constant expressions.
5801 if (VD->isLocalVarDecl() && VD->isStaticLocal() &&
5802 !VD->isUsableInConstantExpressions(C: Info.Ctx)) {
5803 Info.CCEDiag(Loc: VD->getLocation(), DiagId: diag::note_constexpr_static_local)
5804 << (VD->getTSCSpec() == TSCS_unspecified ? 0 : 1) << VD;
5805 return false;
5806 }
5807 return true;
5808}
5809
5810// Evaluate a statement.
5811static EvalStmtResult EvaluateStmt(StmtResult &Result, EvalInfo &Info,
5812 const Stmt *S, const SwitchCase *Case) {
5813 if (!Info.nextStep(S))
5814 return ESR_Failed;
5815
5816 // If we're hunting down a 'case' or 'default' label, recurse through
5817 // substatements until we hit the label.
5818 if (Case) {
5819 switch (S->getStmtClass()) {
5820 case Stmt::CompoundStmtClass:
5821 // FIXME: Precompute which substatement of a compound statement we
5822 // would jump to, and go straight there rather than performing a
5823 // linear scan each time.
5824 case Stmt::LabelStmtClass:
5825 case Stmt::AttributedStmtClass:
5826 case Stmt::DoStmtClass:
5827 break;
5828
5829 case Stmt::CaseStmtClass:
5830 case Stmt::DefaultStmtClass:
5831 if (Case == S)
5832 Case = nullptr;
5833 break;
5834
5835 case Stmt::IfStmtClass: {
5836 // FIXME: Precompute which side of an 'if' we would jump to, and go
5837 // straight there rather than scanning both sides.
5838 const IfStmt *IS = cast<IfStmt>(Val: S);
5839
5840 // Wrap the evaluation in a block scope, in case it's a DeclStmt
5841 // preceded by our switch label.
5842 BlockScopeRAII Scope(Info);
5843
5844 // Step into the init statement in case it brings an (uninitialized)
5845 // variable into scope.
5846 if (const Stmt *Init = IS->getInit()) {
5847 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
5848 if (ESR != ESR_CaseNotFound) {
5849 assert(ESR != ESR_Succeeded);
5850 return ESR;
5851 }
5852 }
5853
5854 // Condition variable must be initialized if it exists.
5855 // FIXME: We can skip evaluating the body if there's a condition
5856 // variable, as there can't be any case labels within it.
5857 // (The same is true for 'for' statements.)
5858
5859 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: IS->getThen(), Case);
5860 if (ESR == ESR_Failed)
5861 return ESR;
5862 if (ESR != ESR_CaseNotFound)
5863 return Scope.destroy() ? ESR : ESR_Failed;
5864 if (!IS->getElse())
5865 return ESR_CaseNotFound;
5866
5867 ESR = EvaluateStmt(Result, Info, S: IS->getElse(), Case);
5868 if (ESR == ESR_Failed)
5869 return ESR;
5870 if (ESR != ESR_CaseNotFound)
5871 return Scope.destroy() ? ESR : ESR_Failed;
5872 return ESR_CaseNotFound;
5873 }
5874
5875 case Stmt::WhileStmtClass: {
5876 EvalStmtResult ESR =
5877 EvaluateLoopBody(Result, Info, Body: cast<WhileStmt>(Val: S)->getBody(), Case);
5878 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: S, /*Scopes=*/{}, ESR))
5879 return ESR;
5880 if (ESR != ESR_Continue)
5881 return ESR;
5882 break;
5883 }
5884
5885 case Stmt::ForStmtClass: {
5886 const ForStmt *FS = cast<ForStmt>(Val: S);
5887 BlockScopeRAII Scope(Info);
5888
5889 // Step into the init statement in case it brings an (uninitialized)
5890 // variable into scope.
5891 if (const Stmt *Init = FS->getInit()) {
5892 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init, Case);
5893 if (ESR != ESR_CaseNotFound) {
5894 assert(ESR != ESR_Succeeded);
5895 return ESR;
5896 }
5897 }
5898
5899 EvalStmtResult ESR =
5900 EvaluateLoopBody(Result, Info, Body: FS->getBody(), Case);
5901 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, /*Scopes=*/{}, ESR))
5902 return ESR;
5903 if (ESR != ESR_Continue)
5904 return ESR;
5905 if (const auto *Inc = FS->getInc()) {
5906 if (Inc->isValueDependent()) {
5907 if (!EvaluateDependentExpr(E: Inc, Info))
5908 return ESR_Failed;
5909 } else {
5910 FullExpressionRAII IncScope(Info);
5911 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
5912 return ESR_Failed;
5913 }
5914 }
5915 break;
5916 }
5917
5918 case Stmt::DeclStmtClass: {
5919 // Start the lifetime of any uninitialized variables we encounter. They
5920 // might be used by the selected branch of the switch.
5921 const DeclStmt *DS = cast<DeclStmt>(Val: S);
5922 for (const auto *D : DS->decls()) {
5923 if (const auto *VD = dyn_cast<VarDecl>(Val: D)) {
5924 if (!CheckLocalVariableDeclaration(Info, VD))
5925 return ESR_Failed;
5926 if (VD->hasLocalStorage() && !VD->getInit())
5927 if (!EvaluateVarDecl(Info, VD))
5928 return ESR_Failed;
5929 // FIXME: If the variable has initialization that can't be jumped
5930 // over, bail out of any immediately-surrounding compound-statement
5931 // too. There can't be any case labels here.
5932 }
5933 }
5934 return ESR_CaseNotFound;
5935 }
5936
5937 default:
5938 return ESR_CaseNotFound;
5939 }
5940 }
5941
5942 switch (S->getStmtClass()) {
5943 default:
5944 if (const Expr *E = dyn_cast<Expr>(Val: S)) {
5945 if (E->isValueDependent()) {
5946 if (!EvaluateDependentExpr(E, Info))
5947 return ESR_Failed;
5948 } else {
5949 // Don't bother evaluating beyond an expression-statement which couldn't
5950 // be evaluated.
5951 // FIXME: Do we need the FullExpressionRAII object here?
5952 // VisitExprWithCleanups should create one when necessary.
5953 FullExpressionRAII Scope(Info);
5954 if (!EvaluateIgnoredValue(Info, E) || !Scope.destroy())
5955 return ESR_Failed;
5956 }
5957 return ESR_Succeeded;
5958 }
5959
5960 Info.FFDiag(Loc: S->getBeginLoc()) << S->getSourceRange();
5961 return ESR_Failed;
5962
5963 case Stmt::NullStmtClass:
5964 return ESR_Succeeded;
5965
5966 case Stmt::DeclStmtClass: {
5967 const DeclStmt *DS = cast<DeclStmt>(Val: S);
5968 for (const auto *D : DS->decls()) {
5969 const VarDecl *VD = dyn_cast_or_null<VarDecl>(Val: D);
5970 if (VD && !CheckLocalVariableDeclaration(Info, VD))
5971 return ESR_Failed;
5972 // Each declaration initialization is its own full-expression.
5973 FullExpressionRAII Scope(Info);
5974 if (!EvaluateDecl(Info, D, /*EvaluateConditionDecl=*/true) &&
5975 !Info.noteFailure())
5976 return ESR_Failed;
5977 if (!Scope.destroy())
5978 return ESR_Failed;
5979 }
5980 return ESR_Succeeded;
5981 }
5982
5983 case Stmt::ReturnStmtClass: {
5984 const Expr *RetExpr = cast<ReturnStmt>(Val: S)->getRetValue();
5985 FullExpressionRAII Scope(Info);
5986 if (RetExpr && RetExpr->isValueDependent()) {
5987 EvaluateDependentExpr(E: RetExpr, Info);
5988 // We know we returned, but we don't know what the value is.
5989 return ESR_Failed;
5990 }
5991 if (RetExpr &&
5992 !(Result.Slot
5993 ? EvaluateInPlace(Result&: Result.Value, Info, This: *Result.Slot, E: RetExpr)
5994 : Evaluate(Result&: Result.Value, Info, E: RetExpr)))
5995 return ESR_Failed;
5996 return Scope.destroy() ? ESR_Returned : ESR_Failed;
5997 }
5998
5999 case Stmt::CompoundStmtClass: {
6000 BlockScopeRAII Scope(Info);
6001
6002 const CompoundStmt *CS = cast<CompoundStmt>(Val: S);
6003 for (const auto *BI : CS->body()) {
6004 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: BI, Case);
6005 if (ESR == ESR_Succeeded)
6006 Case = nullptr;
6007 else if (ESR != ESR_CaseNotFound) {
6008 if (ESR != ESR_Failed && !Scope.destroy())
6009 return ESR_Failed;
6010 return ESR;
6011 }
6012 }
6013 if (Case)
6014 return ESR_CaseNotFound;
6015 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6016 }
6017
6018 case Stmt::IfStmtClass: {
6019 const IfStmt *IS = cast<IfStmt>(Val: S);
6020
6021 // Evaluate the condition, as either a var decl or as an expression.
6022 BlockScopeRAII Scope(Info);
6023 if (const Stmt *Init = IS->getInit()) {
6024 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: Init);
6025 if (ESR != ESR_Succeeded) {
6026 if (ESR != ESR_Failed && !Scope.destroy())
6027 return ESR_Failed;
6028 return ESR;
6029 }
6030 }
6031 bool Cond;
6032 if (IS->isConsteval()) {
6033 Cond = IS->isNonNegatedConsteval();
6034 // If we are not in a constant context, if consteval should not evaluate
6035 // to true.
6036 if (!Info.InConstantContext)
6037 Cond = !Cond;
6038 } else if (!EvaluateCond(Info, CondDecl: IS->getConditionVariable(), Cond: IS->getCond(),
6039 Result&: Cond))
6040 return ESR_Failed;
6041
6042 if (const Stmt *SubStmt = Cond ? IS->getThen() : IS->getElse()) {
6043 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: SubStmt);
6044 if (ESR != ESR_Succeeded) {
6045 if (ESR != ESR_Failed && !Scope.destroy())
6046 return ESR_Failed;
6047 return ESR;
6048 }
6049 }
6050 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6051 }
6052
6053 case Stmt::WhileStmtClass: {
6054 const WhileStmt *WS = cast<WhileStmt>(Val: S);
6055 while (true) {
6056 BlockScopeRAII Scope(Info);
6057 bool Continue;
6058 if (!EvaluateCond(Info, CondDecl: WS->getConditionVariable(), Cond: WS->getCond(),
6059 Result&: Continue))
6060 return ESR_Failed;
6061 if (!Continue)
6062 break;
6063
6064 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: WS->getBody());
6065 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: WS, Scopes: &Scope, ESR))
6066 return ESR;
6067
6068 if (ESR != ESR_Continue) {
6069 if (ESR != ESR_Failed && !Scope.destroy())
6070 return ESR_Failed;
6071 return ESR;
6072 }
6073 if (!Scope.destroy())
6074 return ESR_Failed;
6075 }
6076 return ESR_Succeeded;
6077 }
6078
6079 case Stmt::DoStmtClass: {
6080 const DoStmt *DS = cast<DoStmt>(Val: S);
6081 bool Continue;
6082 do {
6083 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: DS->getBody(), Case);
6084 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: DS, /*Scopes=*/{}, ESR))
6085 return ESR;
6086 if (ESR != ESR_Continue)
6087 return ESR;
6088 Case = nullptr;
6089
6090 if (DS->getCond()->isValueDependent()) {
6091 EvaluateDependentExpr(E: DS->getCond(), Info);
6092 // Bailout as we don't know whether to keep going or terminate the loop.
6093 return ESR_Failed;
6094 }
6095 FullExpressionRAII CondScope(Info);
6096 if (!EvaluateAsBooleanCondition(E: DS->getCond(), Result&: Continue, Info) ||
6097 !CondScope.destroy())
6098 return ESR_Failed;
6099 } while (Continue);
6100 return ESR_Succeeded;
6101 }
6102
6103 case Stmt::ForStmtClass: {
6104 const ForStmt *FS = cast<ForStmt>(Val: S);
6105 BlockScopeRAII ForScope(Info);
6106 if (FS->getInit()) {
6107 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6108 if (ESR != ESR_Succeeded) {
6109 if (ESR != ESR_Failed && !ForScope.destroy())
6110 return ESR_Failed;
6111 return ESR;
6112 }
6113 }
6114 while (true) {
6115 BlockScopeRAII IterScope(Info);
6116 bool Continue = true;
6117 if (FS->getCond() && !EvaluateCond(Info, CondDecl: FS->getConditionVariable(),
6118 Cond: FS->getCond(), Result&: Continue))
6119 return ESR_Failed;
6120
6121 if (!Continue) {
6122 if (!IterScope.destroy())
6123 return ESR_Failed;
6124 break;
6125 }
6126
6127 EvalStmtResult ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6128 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&IterScope, &ForScope}, ESR))
6129 return ESR;
6130 if (ESR != ESR_Continue) {
6131 if (ESR != ESR_Failed && (!IterScope.destroy() || !ForScope.destroy()))
6132 return ESR_Failed;
6133 return ESR;
6134 }
6135
6136 if (const auto *Inc = FS->getInc()) {
6137 if (Inc->isValueDependent()) {
6138 if (!EvaluateDependentExpr(E: Inc, Info))
6139 return ESR_Failed;
6140 } else {
6141 FullExpressionRAII IncScope(Info);
6142 if (!EvaluateIgnoredValue(Info, E: Inc) || !IncScope.destroy())
6143 return ESR_Failed;
6144 }
6145 }
6146
6147 if (!IterScope.destroy())
6148 return ESR_Failed;
6149 }
6150 return ForScope.destroy() ? ESR_Succeeded : ESR_Failed;
6151 }
6152
6153 case Stmt::CXXForRangeStmtClass: {
6154 const CXXForRangeStmt *FS = cast<CXXForRangeStmt>(Val: S);
6155 BlockScopeRAII Scope(Info);
6156
6157 // Evaluate the init-statement if present.
6158 if (FS->getInit()) {
6159 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getInit());
6160 if (ESR != ESR_Succeeded) {
6161 if (ESR != ESR_Failed && !Scope.destroy())
6162 return ESR_Failed;
6163 return ESR;
6164 }
6165 }
6166
6167 // Initialize the __range variable.
6168 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: FS->getRangeStmt());
6169 if (ESR != ESR_Succeeded) {
6170 if (ESR != ESR_Failed && !Scope.destroy())
6171 return ESR_Failed;
6172 return ESR;
6173 }
6174
6175 // In error-recovery cases it's possible to get here even if we failed to
6176 // synthesize the __begin and __end variables.
6177 if (!FS->getBeginStmt() || !FS->getEndStmt() || !FS->getCond())
6178 return ESR_Failed;
6179
6180 // Create the __begin and __end iterators.
6181 ESR = EvaluateStmt(Result, Info, S: FS->getBeginStmt());
6182 if (ESR != ESR_Succeeded) {
6183 if (ESR != ESR_Failed && !Scope.destroy())
6184 return ESR_Failed;
6185 return ESR;
6186 }
6187 ESR = EvaluateStmt(Result, Info, S: FS->getEndStmt());
6188 if (ESR != ESR_Succeeded) {
6189 if (ESR != ESR_Failed && !Scope.destroy())
6190 return ESR_Failed;
6191 return ESR;
6192 }
6193
6194 while (true) {
6195 // Condition: __begin != __end.
6196 {
6197 if (FS->getCond()->isValueDependent()) {
6198 EvaluateDependentExpr(E: FS->getCond(), Info);
6199 // We don't know whether to keep going or terminate the loop.
6200 return ESR_Failed;
6201 }
6202 bool Continue = true;
6203 FullExpressionRAII CondExpr(Info);
6204 if (!EvaluateAsBooleanCondition(E: FS->getCond(), Result&: Continue, Info))
6205 return ESR_Failed;
6206 if (!Continue)
6207 break;
6208 }
6209
6210 // User's variable declaration, initialized by *__begin.
6211 BlockScopeRAII InnerScope(Info);
6212 ESR = EvaluateStmt(Result, Info, S: FS->getLoopVarStmt());
6213 if (ESR != ESR_Succeeded) {
6214 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6215 return ESR_Failed;
6216 return ESR;
6217 }
6218
6219 // Loop body.
6220 ESR = EvaluateLoopBody(Result, Info, Body: FS->getBody());
6221 if (ShouldPropagateBreakContinue(Info, LoopOrSwitch: FS, Scopes: {&InnerScope, &Scope}, ESR))
6222 return ESR;
6223 if (ESR != ESR_Continue) {
6224 if (ESR != ESR_Failed && (!InnerScope.destroy() || !Scope.destroy()))
6225 return ESR_Failed;
6226 return ESR;
6227 }
6228 if (FS->getInc()->isValueDependent()) {
6229 if (!EvaluateDependentExpr(E: FS->getInc(), Info))
6230 return ESR_Failed;
6231 } else {
6232 // Increment: ++__begin
6233 if (!EvaluateIgnoredValue(Info, E: FS->getInc()))
6234 return ESR_Failed;
6235 }
6236
6237 if (!InnerScope.destroy())
6238 return ESR_Failed;
6239 }
6240
6241 return Scope.destroy() ? ESR_Succeeded : ESR_Failed;
6242 }
6243
6244 case Stmt::SwitchStmtClass:
6245 return EvaluateSwitch(Result, Info, SS: cast<SwitchStmt>(Val: S));
6246
6247 case Stmt::ContinueStmtClass:
6248 case Stmt::BreakStmtClass: {
6249 auto *B = cast<LoopControlStmt>(Val: S);
6250 Info.BreakContinueStack.push_back(Elt: B->getNamedLoopOrSwitch());
6251 return isa<ContinueStmt>(Val: S) ? ESR_Continue : ESR_Break;
6252 }
6253
6254 case Stmt::LabelStmtClass:
6255 return EvaluateStmt(Result, Info, S: cast<LabelStmt>(Val: S)->getSubStmt(), Case);
6256
6257 case Stmt::AttributedStmtClass: {
6258 const auto *AS = cast<AttributedStmt>(Val: S);
6259 const auto *SS = AS->getSubStmt();
6260 MSConstexprContextRAII ConstexprContext(
6261 *Info.CurrentCall, hasSpecificAttr<MSConstexprAttr>(container: AS->getAttrs()) &&
6262 isa<ReturnStmt>(Val: SS));
6263
6264 auto LO = Info.Ctx.getLangOpts();
6265 if (LO.CXXAssumptions && !LO.MSVCCompat) {
6266 for (auto *Attr : AS->getAttrs()) {
6267 auto *AA = dyn_cast<CXXAssumeAttr>(Val: Attr);
6268 if (!AA)
6269 continue;
6270
6271 auto *Assumption = AA->getAssumption();
6272 if (Assumption->isValueDependent())
6273 return ESR_Failed;
6274
6275 if (Assumption->HasSideEffects(Ctx: Info.Ctx))
6276 continue;
6277
6278 bool Value;
6279 if (!EvaluateAsBooleanCondition(E: Assumption, Result&: Value, Info))
6280 return ESR_Failed;
6281 if (!Value) {
6282 Info.CCEDiag(Loc: Assumption->getExprLoc(),
6283 DiagId: diag::note_constexpr_assumption_failed);
6284 return ESR_Failed;
6285 }
6286 }
6287 }
6288
6289 return EvaluateStmt(Result, Info, S: SS, Case);
6290 }
6291
6292 case Stmt::CaseStmtClass:
6293 case Stmt::DefaultStmtClass:
6294 return EvaluateStmt(Result, Info, S: cast<SwitchCase>(Val: S)->getSubStmt(), Case);
6295 case Stmt::CXXTryStmtClass:
6296 // Evaluate try blocks by evaluating all sub statements.
6297 return EvaluateStmt(Result, Info, S: cast<CXXTryStmt>(Val: S)->getTryBlock(), Case);
6298 }
6299}
6300
6301/// CheckTrivialDefaultConstructor - Check whether a constructor is a trivial
6302/// default constructor. If so, we'll fold it whether or not it's marked as
6303/// constexpr. If it is marked as constexpr, we will never implicitly define it,
6304/// so we need special handling.
6305static bool CheckTrivialDefaultConstructor(EvalInfo &Info, SourceLocation Loc,
6306 const CXXConstructorDecl *CD,
6307 bool IsValueInitialization) {
6308 if (!CD->isTrivial() || !CD->isDefaultConstructor())
6309 return false;
6310
6311 // Value-initialization does not call a trivial default constructor, so such a
6312 // call is a core constant expression whether or not the constructor is
6313 // constexpr.
6314 if (!CD->isConstexpr() && !IsValueInitialization) {
6315 if (Info.getLangOpts().CPlusPlus11) {
6316 // FIXME: If DiagDecl is an implicitly-declared special member function,
6317 // we should be much more explicit about why it's not constexpr.
6318 Info.CCEDiag(Loc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6319 << /*IsConstexpr*/0 << /*IsConstructor*/1 << CD;
6320 Info.Note(Loc: CD->getLocation(), DiagId: diag::note_declared_at);
6321 } else {
6322 Info.CCEDiag(Loc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6323 }
6324 }
6325 return true;
6326}
6327
6328/// CheckConstexprFunction - Check that a function can be called in a constant
6329/// expression.
6330static bool CheckConstexprFunction(EvalInfo &Info, SourceLocation CallLoc,
6331 const FunctionDecl *Declaration,
6332 const FunctionDecl *Definition,
6333 const Stmt *Body) {
6334 // Potential constant expressions can contain calls to declared, but not yet
6335 // defined, constexpr functions.
6336 if (Info.checkingPotentialConstantExpression() && !Definition &&
6337 Declaration->isConstexpr())
6338 return false;
6339
6340 // Bail out if the function declaration itself is invalid. We will
6341 // have produced a relevant diagnostic while parsing it, so just
6342 // note the problematic sub-expression.
6343 if (Declaration->isInvalidDecl()) {
6344 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6345 return false;
6346 }
6347
6348 // DR1872: An instantiated virtual constexpr function can't be called in a
6349 // constant expression (prior to C++20). We can still constant-fold such a
6350 // call.
6351 if (!Info.Ctx.getLangOpts().CPlusPlus20 && isa<CXXMethodDecl>(Val: Declaration) &&
6352 cast<CXXMethodDecl>(Val: Declaration)->isVirtual())
6353 Info.CCEDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_call);
6354
6355 if (Definition && Definition->isInvalidDecl()) {
6356 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6357 return false;
6358 }
6359
6360 // Can we evaluate this function call?
6361 if (Definition && Body &&
6362 (Definition->isConstexpr() || (Info.CurrentCall->CanEvalMSConstexpr &&
6363 Definition->hasAttr<MSConstexprAttr>())))
6364 return true;
6365
6366 const FunctionDecl *DiagDecl = Definition ? Definition : Declaration;
6367 // Special note for the assert() macro, as the normal error message falsely
6368 // implies we cannot use an assertion during constant evaluation.
6369 if (CallLoc.isMacroID() && DiagDecl->getIdentifier()) {
6370 // FIXME: Instead of checking for an implementation-defined function,
6371 // check and evaluate the assert() macro.
6372 StringRef Name = DiagDecl->getName();
6373 bool AssertFailed =
6374 Name == "__assert_rtn" || Name == "__assert_fail" || Name == "_wassert";
6375 if (AssertFailed) {
6376 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_assert_failed);
6377 return false;
6378 }
6379 }
6380
6381 if (Info.getLangOpts().CPlusPlus11) {
6382 // If this function is not constexpr because it is an inherited
6383 // non-constexpr constructor, diagnose that directly.
6384 auto *CD = dyn_cast<CXXConstructorDecl>(Val: DiagDecl);
6385 if (CD && CD->isInheritingConstructor()) {
6386 auto *Inherited = CD->getInheritedConstructor().getConstructor();
6387 if (!Inherited->isConstexpr())
6388 DiagDecl = CD = Inherited;
6389 }
6390
6391 // FIXME: If DiagDecl is an implicitly-declared special member function
6392 // or an inheriting constructor, we should be much more explicit about why
6393 // it's not constexpr.
6394 if (CD && CD->isInheritingConstructor())
6395 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_inhctor, ExtraNotes: 1)
6396 << CD->getInheritedConstructor().getConstructor()->getParent();
6397 else
6398 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_invalid_function, ExtraNotes: 1)
6399 << DiagDecl->isConstexpr() << (bool)CD << DiagDecl;
6400 Info.Note(Loc: DiagDecl->getLocation(), DiagId: diag::note_declared_at);
6401 } else {
6402 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_invalid_subexpr_in_const_expr);
6403 }
6404 return false;
6405}
6406
6407namespace {
6408struct CheckDynamicTypeHandler {
6409 AccessKinds AccessKind;
6410 typedef bool result_type;
6411 bool failed() { return false; }
6412 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6413 return true;
6414 }
6415 bool found(APSInt &Value, QualType SubobjType) { return true; }
6416 bool found(APFloat &Value, QualType SubobjType) { return true; }
6417};
6418} // end anonymous namespace
6419
6420/// Check that we can access the notional vptr of an object / determine its
6421/// dynamic type.
6422static bool checkDynamicType(EvalInfo &Info, const Expr *E, const LValue &This,
6423 AccessKinds AK, bool Polymorphic) {
6424 if (This.Designator.Invalid)
6425 return false;
6426
6427 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: This, LValType: QualType());
6428
6429 if (!Obj)
6430 return false;
6431
6432 if (!Obj.Value) {
6433 // The object is not usable in constant expressions, so we can't inspect
6434 // its value to see if it's in-lifetime or what the active union members
6435 // are. We can still check for a one-past-the-end lvalue.
6436 if (This.Designator.isOnePastTheEnd() ||
6437 This.Designator.isMostDerivedAnUnsizedArray()) {
6438 Info.FFDiag(E, DiagId: This.Designator.isOnePastTheEnd()
6439 ? diag::note_constexpr_access_past_end
6440 : diag::note_constexpr_access_unsized_array)
6441 << AK;
6442 return false;
6443 } else if (Polymorphic) {
6444 // Conservatively refuse to perform a polymorphic operation if we would
6445 // not be able to read a notional 'vptr' value.
6446 if (!Info.checkingPotentialConstantExpression() ||
6447 !This.AllowConstexprUnknown) {
6448 APValue Val;
6449 This.moveInto(V&: Val);
6450 QualType StarThisType =
6451 Info.Ctx.getLValueReferenceType(T: This.Designator.getType(Ctx&: Info.Ctx));
6452 Info.FFDiag(E, DiagId: diag::note_constexpr_polymorphic_unknown_dynamic_type)
6453 << AK << Val.getAsString(Ctx: Info.Ctx, Ty: StarThisType);
6454 }
6455 return false;
6456 }
6457 return true;
6458 }
6459
6460 CheckDynamicTypeHandler Handler{.AccessKind: AK};
6461 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
6462}
6463
6464/// Check that the pointee of the 'this' pointer in a member function call is
6465/// either within its lifetime or in its period of construction or destruction.
6466static bool
6467checkNonVirtualMemberCallThisPointer(EvalInfo &Info, const Expr *E,
6468 const LValue &This,
6469 const CXXMethodDecl *NamedMember) {
6470 return checkDynamicType(
6471 Info, E, This,
6472 AK: isa<CXXDestructorDecl>(Val: NamedMember) ? AK_Destroy : AK_MemberCall, Polymorphic: false);
6473}
6474
6475struct DynamicType {
6476 /// The dynamic class type of the object.
6477 const CXXRecordDecl *Type;
6478 /// The corresponding path length in the lvalue.
6479 unsigned PathLength;
6480};
6481
6482static const CXXRecordDecl *getBaseClassType(SubobjectDesignator &Designator,
6483 unsigned PathLength) {
6484 assert(PathLength >= Designator.MostDerivedPathLength && PathLength <=
6485 Designator.Entries.size() && "invalid path length");
6486 return (PathLength == Designator.MostDerivedPathLength)
6487 ? Designator.MostDerivedType->getAsCXXRecordDecl()
6488 : getAsBaseClass(E: Designator.Entries[PathLength - 1]);
6489}
6490
6491/// Determine the dynamic type of an object.
6492static std::optional<DynamicType> ComputeDynamicType(EvalInfo &Info,
6493 const Expr *E,
6494 LValue &This,
6495 AccessKinds AK) {
6496 // If we don't have an lvalue denoting an object of class type, there is no
6497 // meaningful dynamic type. (We consider objects of non-class type to have no
6498 // dynamic type.)
6499 if (!checkDynamicType(Info, E, This, AK,
6500 Polymorphic: AK != AK_TypeId || This.AllowConstexprUnknown))
6501 return std::nullopt;
6502
6503 if (This.Designator.Invalid)
6504 return std::nullopt;
6505
6506 // Refuse to compute a dynamic type in the presence of virtual bases. This
6507 // shouldn't happen other than in constant-folding situations, since literal
6508 // types can't have virtual bases.
6509 //
6510 // Note that consumers of DynamicType assume that the type has no virtual
6511 // bases, and will need modifications if this restriction is relaxed.
6512 const CXXRecordDecl *Class =
6513 This.Designator.MostDerivedType->getAsCXXRecordDecl();
6514 if (!Class || Class->getNumVBases()) {
6515 Info.FFDiag(E);
6516 return std::nullopt;
6517 }
6518
6519 // FIXME: For very deep class hierarchies, it might be beneficial to use a
6520 // binary search here instead. But the overwhelmingly common case is that
6521 // we're not in the middle of a constructor, so it probably doesn't matter
6522 // in practice.
6523 ArrayRef<APValue::LValuePathEntry> Path = This.Designator.Entries;
6524 for (unsigned PathLength = This.Designator.MostDerivedPathLength;
6525 PathLength <= Path.size(); ++PathLength) {
6526 switch (Info.isEvaluatingCtorDtor(Base: This.getLValueBase(),
6527 Path: Path.slice(N: 0, M: PathLength))) {
6528 case ConstructionPhase::Bases:
6529 case ConstructionPhase::DestroyingBases:
6530 // We're constructing or destroying a base class. This is not the dynamic
6531 // type.
6532 break;
6533
6534 case ConstructionPhase::None:
6535 case ConstructionPhase::AfterBases:
6536 case ConstructionPhase::AfterFields:
6537 case ConstructionPhase::Destroying:
6538 // We've finished constructing the base classes and not yet started
6539 // destroying them again, so this is the dynamic type.
6540 return DynamicType{.Type: getBaseClassType(Designator&: This.Designator, PathLength),
6541 .PathLength: PathLength};
6542 }
6543 }
6544
6545 // CWG issue 1517: we're constructing a base class of the object described by
6546 // 'This', so that object has not yet begun its period of construction and
6547 // any polymorphic operation on it results in undefined behavior.
6548 Info.FFDiag(E);
6549 return std::nullopt;
6550}
6551
6552/// Perform virtual dispatch.
6553static const CXXMethodDecl *HandleVirtualDispatch(
6554 EvalInfo &Info, const Expr *E, LValue &This, const CXXMethodDecl *Found,
6555 llvm::SmallVectorImpl<QualType> &CovariantAdjustmentPath) {
6556 std::optional<DynamicType> DynType = ComputeDynamicType(
6557 Info, E, This,
6558 AK: isa<CXXDestructorDecl>(Val: Found) ? AK_Destroy : AK_MemberCall);
6559 if (!DynType)
6560 return nullptr;
6561
6562 // Find the final overrider. It must be declared in one of the classes on the
6563 // path from the dynamic type to the static type.
6564 // FIXME: If we ever allow literal types to have virtual base classes, that
6565 // won't be true.
6566 const CXXMethodDecl *Callee = Found;
6567 unsigned PathLength = DynType->PathLength;
6568 for (/**/; PathLength <= This.Designator.Entries.size(); ++PathLength) {
6569 const CXXRecordDecl *Class = getBaseClassType(Designator&: This.Designator, PathLength);
6570 const CXXMethodDecl *Overrider =
6571 Found->getCorrespondingMethodDeclaredInClass(RD: Class, MayBeBase: false);
6572 if (Overrider) {
6573 Callee = Overrider;
6574 break;
6575 }
6576 }
6577
6578 // C++2a [class.abstract]p6:
6579 // the effect of making a virtual call to a pure virtual function [...] is
6580 // undefined
6581 if (Callee->isPureVirtual()) {
6582 Info.FFDiag(E, DiagId: diag::note_constexpr_pure_virtual_call, ExtraNotes: 1) << Callee;
6583 Info.Note(Loc: Callee->getLocation(), DiagId: diag::note_declared_at);
6584 return nullptr;
6585 }
6586
6587 // If necessary, walk the rest of the path to determine the sequence of
6588 // covariant adjustment steps to apply.
6589 if (!Info.Ctx.hasSameUnqualifiedType(T1: Callee->getReturnType(),
6590 T2: Found->getReturnType())) {
6591 CovariantAdjustmentPath.push_back(Elt: Callee->getReturnType());
6592 for (unsigned CovariantPathLength = PathLength + 1;
6593 CovariantPathLength != This.Designator.Entries.size();
6594 ++CovariantPathLength) {
6595 const CXXRecordDecl *NextClass =
6596 getBaseClassType(Designator&: This.Designator, PathLength: CovariantPathLength);
6597 const CXXMethodDecl *Next =
6598 Found->getCorrespondingMethodDeclaredInClass(RD: NextClass, MayBeBase: false);
6599 if (Next && !Info.Ctx.hasSameUnqualifiedType(
6600 T1: Next->getReturnType(), T2: CovariantAdjustmentPath.back()))
6601 CovariantAdjustmentPath.push_back(Elt: Next->getReturnType());
6602 }
6603 if (!Info.Ctx.hasSameUnqualifiedType(T1: Found->getReturnType(),
6604 T2: CovariantAdjustmentPath.back()))
6605 CovariantAdjustmentPath.push_back(Elt: Found->getReturnType());
6606 }
6607
6608 // Perform 'this' adjustment.
6609 if (!CastToDerivedClass(Info, E, Result&: This, TruncatedType: Callee->getParent(), TruncatedElements: PathLength))
6610 return nullptr;
6611
6612 return Callee;
6613}
6614
6615/// Perform the adjustment from a value returned by a virtual function to
6616/// a value of the statically expected type, which may be a pointer or
6617/// reference to a base class of the returned type.
6618static bool HandleCovariantReturnAdjustment(EvalInfo &Info, const Expr *E,
6619 APValue &Result,
6620 ArrayRef<QualType> Path) {
6621 assert(Result.isLValue() &&
6622 "unexpected kind of APValue for covariant return");
6623 if (Result.isNullPointer())
6624 return true;
6625
6626 LValue LVal;
6627 LVal.setFrom(Ctx: Info.Ctx, V: Result);
6628
6629 const CXXRecordDecl *OldClass = Path[0]->getPointeeCXXRecordDecl();
6630 for (unsigned I = 1; I != Path.size(); ++I) {
6631 const CXXRecordDecl *NewClass = Path[I]->getPointeeCXXRecordDecl();
6632 assert(OldClass && NewClass && "unexpected kind of covariant return");
6633 if (OldClass != NewClass &&
6634 !CastToBaseClass(Info, E, Result&: LVal, DerivedRD: OldClass, BaseRD: NewClass))
6635 return false;
6636 OldClass = NewClass;
6637 }
6638
6639 LVal.moveInto(V&: Result);
6640 return true;
6641}
6642
6643/// Determine whether \p Base, which is known to be a direct base class of
6644/// \p Derived, is a public base class.
6645static bool isBaseClassPublic(const CXXRecordDecl *Derived,
6646 const CXXRecordDecl *Base) {
6647 for (const CXXBaseSpecifier &BaseSpec : Derived->bases()) {
6648 auto *BaseClass = BaseSpec.getType()->getAsCXXRecordDecl();
6649 if (BaseClass && declaresSameEntity(D1: BaseClass, D2: Base))
6650 return BaseSpec.getAccessSpecifier() == AS_public;
6651 }
6652 llvm_unreachable("Base is not a direct base of Derived");
6653}
6654
6655/// Apply the given dynamic cast operation on the provided lvalue.
6656///
6657/// This implements the hard case of dynamic_cast, requiring a "runtime check"
6658/// to find a suitable target subobject.
6659static bool HandleDynamicCast(EvalInfo &Info, const ExplicitCastExpr *E,
6660 LValue &Ptr) {
6661 // We can't do anything with a non-symbolic pointer value.
6662 SubobjectDesignator &D = Ptr.Designator;
6663 if (D.Invalid)
6664 return false;
6665
6666 // C++ [expr.dynamic.cast]p6:
6667 // If v is a null pointer value, the result is a null pointer value.
6668 if (Ptr.isNullPointer() && !E->isGLValue())
6669 return true;
6670
6671 // For all the other cases, we need the pointer to point to an object within
6672 // its lifetime / period of construction / destruction, and we need to know
6673 // its dynamic type.
6674 std::optional<DynamicType> DynType =
6675 ComputeDynamicType(Info, E, This&: Ptr, AK: AK_DynamicCast);
6676 if (!DynType)
6677 return false;
6678
6679 // C++ [expr.dynamic.cast]p7:
6680 // If T is "pointer to cv void", then the result is a pointer to the most
6681 // derived object
6682 if (E->getType()->isVoidPointerType())
6683 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength);
6684
6685 const CXXRecordDecl *C = E->getTypeAsWritten()->getPointeeCXXRecordDecl();
6686 assert(C && "dynamic_cast target is not void pointer nor class");
6687 CanQualType CQT = Info.Ctx.getCanonicalTagType(TD: C);
6688
6689 auto RuntimeCheckFailed = [&] (CXXBasePaths *Paths) {
6690 // C++ [expr.dynamic.cast]p9:
6691 if (!E->isGLValue()) {
6692 // The value of a failed cast to pointer type is the null pointer value
6693 // of the required result type.
6694 Ptr.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
6695 return true;
6696 }
6697
6698 // A failed cast to reference type throws [...] std::bad_cast.
6699 unsigned DiagKind;
6700 if (!Paths && (declaresSameEntity(D1: DynType->Type, D2: C) ||
6701 DynType->Type->isDerivedFrom(Base: C)))
6702 DiagKind = 0;
6703 else if (!Paths || Paths->begin() == Paths->end())
6704 DiagKind = 1;
6705 else if (Paths->isAmbiguous(BaseType: CQT))
6706 DiagKind = 2;
6707 else {
6708 assert(Paths->front().Access != AS_public && "why did the cast fail?");
6709 DiagKind = 3;
6710 }
6711 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_cast_to_reference_failed)
6712 << DiagKind << Ptr.Designator.getType(Ctx&: Info.Ctx)
6713 << Info.Ctx.getCanonicalTagType(TD: DynType->Type)
6714 << E->getType().getUnqualifiedType();
6715 return false;
6716 };
6717
6718 // Runtime check, phase 1:
6719 // Walk from the base subobject towards the derived object looking for the
6720 // target type.
6721 for (int PathLength = Ptr.Designator.Entries.size();
6722 PathLength >= (int)DynType->PathLength; --PathLength) {
6723 const CXXRecordDecl *Class = getBaseClassType(Designator&: Ptr.Designator, PathLength);
6724 if (declaresSameEntity(D1: Class, D2: C))
6725 return CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: Class, TruncatedElements: PathLength);
6726 // We can only walk across public inheritance edges.
6727 if (PathLength > (int)DynType->PathLength &&
6728 !isBaseClassPublic(Derived: getBaseClassType(Designator&: Ptr.Designator, PathLength: PathLength - 1),
6729 Base: Class))
6730 return RuntimeCheckFailed(nullptr);
6731 }
6732
6733 // Runtime check, phase 2:
6734 // Search the dynamic type for an unambiguous public base of type C.
6735 CXXBasePaths Paths(/*FindAmbiguities=*/true,
6736 /*RecordPaths=*/true, /*DetectVirtual=*/false);
6737 if (DynType->Type->isDerivedFrom(Base: C, Paths) && !Paths.isAmbiguous(BaseType: CQT) &&
6738 Paths.front().Access == AS_public) {
6739 // Downcast to the dynamic type...
6740 if (!CastToDerivedClass(Info, E, Result&: Ptr, TruncatedType: DynType->Type, TruncatedElements: DynType->PathLength))
6741 return false;
6742 // ... then upcast to the chosen base class subobject.
6743 for (CXXBasePathElement &Elem : Paths.front())
6744 if (!HandleLValueBase(Info, E, Obj&: Ptr, DerivedDecl: Elem.Class, Base: Elem.Base))
6745 return false;
6746 return true;
6747 }
6748
6749 // Otherwise, the runtime check fails.
6750 return RuntimeCheckFailed(&Paths);
6751}
6752
6753namespace {
6754struct StartLifetimeOfUnionMemberHandler {
6755 EvalInfo &Info;
6756 const Expr *LHSExpr;
6757 const FieldDecl *Field;
6758 bool DuringInit;
6759 bool Failed = false;
6760 static const AccessKinds AccessKind = AK_Assign;
6761
6762 typedef bool result_type;
6763 bool failed() { return Failed; }
6764 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
6765 // We are supposed to perform no initialization but begin the lifetime of
6766 // the object. We interpret that as meaning to do what default
6767 // initialization of the object would do if all constructors involved were
6768 // trivial:
6769 // * All base, non-variant member, and array element subobjects' lifetimes
6770 // begin
6771 // * No variant members' lifetimes begin
6772 // * All scalar subobjects whose lifetimes begin have indeterminate values
6773 assert(SubobjType->isUnionType());
6774 if (declaresSameEntity(D1: Subobj.getUnionField(), D2: Field)) {
6775 // This union member is already active. If it's also in-lifetime, there's
6776 // nothing to do.
6777 if (Subobj.getUnionValue().hasValue())
6778 return true;
6779 } else if (DuringInit) {
6780 // We're currently in the process of initializing a different union
6781 // member. If we carried on, that initialization would attempt to
6782 // store to an inactive union member, resulting in undefined behavior.
6783 Info.FFDiag(E: LHSExpr,
6784 DiagId: diag::note_constexpr_union_member_change_during_init);
6785 return false;
6786 }
6787 APValue Result;
6788 Failed = !handleDefaultInitValue(T: Field->getType(), Result);
6789 Subobj.setUnion(Field, Value: Result);
6790 return true;
6791 }
6792 bool found(APSInt &Value, QualType SubobjType) {
6793 llvm_unreachable("wrong value kind for union object");
6794 }
6795 bool found(APFloat &Value, QualType SubobjType) {
6796 llvm_unreachable("wrong value kind for union object");
6797 }
6798};
6799} // end anonymous namespace
6800
6801const AccessKinds StartLifetimeOfUnionMemberHandler::AccessKind;
6802
6803/// Handle a builtin simple-assignment or a call to a trivial assignment
6804/// operator whose left-hand side might involve a union member access. If it
6805/// does, implicitly start the lifetime of any accessed union elements per
6806/// C++20 [class.union]5.
6807static bool MaybeHandleUnionActiveMemberChange(EvalInfo &Info,
6808 const Expr *LHSExpr,
6809 const LValue &LHS) {
6810 if (LHS.InvalidBase || LHS.Designator.Invalid)
6811 return false;
6812
6813 llvm::SmallVector<std::pair<unsigned, const FieldDecl*>, 4> UnionPathLengths;
6814 // C++ [class.union]p5:
6815 // define the set S(E) of subexpressions of E as follows:
6816 unsigned PathLength = LHS.Designator.Entries.size();
6817 for (const Expr *E = LHSExpr; E != nullptr;) {
6818 // -- If E is of the form A.B, S(E) contains the elements of S(A)...
6819 if (auto *ME = dyn_cast<MemberExpr>(Val: E)) {
6820 auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl());
6821 // Note that we can't implicitly start the lifetime of a reference,
6822 // so we don't need to proceed any further if we reach one.
6823 if (!FD || FD->getType()->isReferenceType())
6824 break;
6825
6826 // ... and also contains A.B if B names a union member ...
6827 if (FD->getParent()->isUnion()) {
6828 // ... of a non-class, non-array type, or of a class type with a
6829 // trivial default constructor that is not deleted, or an array of
6830 // such types.
6831 auto *RD =
6832 FD->getType()->getBaseElementTypeUnsafe()->getAsCXXRecordDecl();
6833 if (!RD || RD->hasTrivialDefaultConstructor())
6834 UnionPathLengths.push_back(Elt: {PathLength - 1, FD});
6835 }
6836
6837 E = ME->getBase();
6838 --PathLength;
6839 assert(declaresSameEntity(FD,
6840 LHS.Designator.Entries[PathLength]
6841 .getAsBaseOrMember().getPointer()));
6842
6843 // -- If E is of the form A[B] and is interpreted as a built-in array
6844 // subscripting operator, S(E) is [S(the array operand, if any)].
6845 } else if (auto *ASE = dyn_cast<ArraySubscriptExpr>(Val: E)) {
6846 // Step over an ArrayToPointerDecay implicit cast.
6847 auto *Base = ASE->getBase()->IgnoreImplicit();
6848 if (!Base->getType()->isArrayType())
6849 break;
6850
6851 E = Base;
6852 --PathLength;
6853
6854 } else if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: E)) {
6855 // Step over a derived-to-base conversion.
6856 E = ICE->getSubExpr();
6857 if (ICE->getCastKind() == CK_NoOp)
6858 continue;
6859 if (ICE->getCastKind() != CK_DerivedToBase &&
6860 ICE->getCastKind() != CK_UncheckedDerivedToBase)
6861 break;
6862 // Walk path backwards as we walk up from the base to the derived class.
6863 for (const CXXBaseSpecifier *Elt : llvm::reverse(C: ICE->path())) {
6864 if (Elt->isVirtual()) {
6865 // A class with virtual base classes never has a trivial default
6866 // constructor, so S(E) is empty in this case.
6867 E = nullptr;
6868 break;
6869 }
6870
6871 --PathLength;
6872 assert(declaresSameEntity(Elt->getType()->getAsCXXRecordDecl(),
6873 LHS.Designator.Entries[PathLength]
6874 .getAsBaseOrMember().getPointer()));
6875 }
6876
6877 // -- Otherwise, S(E) is empty.
6878 } else {
6879 break;
6880 }
6881 }
6882
6883 // Common case: no unions' lifetimes are started.
6884 if (UnionPathLengths.empty())
6885 return true;
6886
6887 // if modification of X [would access an inactive union member], an object
6888 // of the type of X is implicitly created
6889 CompleteObject Obj =
6890 findCompleteObject(Info, E: LHSExpr, AK: AK_Assign, LVal: LHS, LValType: LHSExpr->getType());
6891 if (!Obj)
6892 return false;
6893 for (std::pair<unsigned, const FieldDecl *> LengthAndField :
6894 llvm::reverse(C&: UnionPathLengths)) {
6895 // Form a designator for the union object.
6896 SubobjectDesignator D = LHS.Designator;
6897 D.truncate(Ctx&: Info.Ctx, Base: LHS.Base, NewLength: LengthAndField.first);
6898
6899 bool DuringInit = Info.isEvaluatingCtorDtor(Base: LHS.Base, Path: D.Entries) ==
6900 ConstructionPhase::AfterBases;
6901 StartLifetimeOfUnionMemberHandler StartLifetime{
6902 .Info: Info, .LHSExpr: LHSExpr, .Field: LengthAndField.second, .DuringInit: DuringInit};
6903 if (!findSubobject(Info, E: LHSExpr, Obj, Sub: D, handler&: StartLifetime))
6904 return false;
6905 }
6906
6907 return true;
6908}
6909
6910static bool EvaluateCallArg(const ParmVarDecl *PVD, const Expr *Arg,
6911 CallRef Call, EvalInfo &Info, bool NonNull = false,
6912 APValue **EvaluatedArg = nullptr) {
6913 LValue LV;
6914 // Create the parameter slot and register its destruction. For a vararg
6915 // argument, create a temporary.
6916 // FIXME: For calling conventions that destroy parameters in the callee,
6917 // should we consider performing destruction when the function returns
6918 // instead?
6919 APValue &V = PVD ? Info.CurrentCall->createParam(Args: Call, PVD, LV)
6920 : Info.CurrentCall->createTemporary(Key: Arg, T: Arg->getType(),
6921 Scope: ScopeKind::Call, LV);
6922 if (!EvaluateInPlace(Result&: V, Info, This: LV, E: Arg))
6923 return false;
6924
6925 // Passing a null pointer to an __attribute__((nonnull)) parameter results in
6926 // undefined behavior, so is non-constant.
6927 if (NonNull && V.isLValue() && V.isNullPointer()) {
6928 Info.CCEDiag(E: Arg, DiagId: diag::note_non_null_attribute_failed);
6929 return false;
6930 }
6931
6932 if (EvaluatedArg)
6933 *EvaluatedArg = &V;
6934
6935 return true;
6936}
6937
6938/// Evaluate the arguments to a function call.
6939static bool EvaluateArgs(ArrayRef<const Expr *> Args, CallRef Call,
6940 EvalInfo &Info, const FunctionDecl *Callee,
6941 bool RightToLeft = false,
6942 LValue *ObjectArg = nullptr) {
6943 bool Success = true;
6944 llvm::SmallBitVector ForbiddenNullArgs;
6945 if (Callee->hasAttr<NonNullAttr>()) {
6946 ForbiddenNullArgs.resize(N: Args.size());
6947 for (const auto *Attr : Callee->specific_attrs<NonNullAttr>()) {
6948 if (!Attr->args_size()) {
6949 ForbiddenNullArgs.set();
6950 break;
6951 } else
6952 for (auto Idx : Attr->args()) {
6953 unsigned ASTIdx = Idx.getASTIndex();
6954 if (ASTIdx >= Args.size())
6955 continue;
6956 ForbiddenNullArgs[ASTIdx] = true;
6957 }
6958 }
6959 }
6960 for (unsigned I = 0; I < Args.size(); I++) {
6961 unsigned Idx = RightToLeft ? Args.size() - I - 1 : I;
6962 const ParmVarDecl *PVD =
6963 Idx < Callee->getNumParams() ? Callee->getParamDecl(i: Idx) : nullptr;
6964 bool NonNull = !ForbiddenNullArgs.empty() && ForbiddenNullArgs[Idx];
6965 APValue *That = nullptr;
6966 if (!EvaluateCallArg(PVD, Arg: Args[Idx], Call, Info, NonNull, EvaluatedArg: &That)) {
6967 // If we're checking for a potential constant expression, evaluate all
6968 // initializers even if some of them fail.
6969 if (!Info.noteFailure())
6970 return false;
6971 Success = false;
6972 }
6973 if (PVD && PVD->isExplicitObjectParameter() && That && That->isLValue())
6974 ObjectArg->setFrom(Ctx: Info.Ctx, V: *That);
6975 }
6976 return Success;
6977}
6978
6979/// Perform a trivial copy from Param, which is the parameter of a copy or move
6980/// constructor or assignment operator.
6981static bool handleTrivialCopy(EvalInfo &Info, const ParmVarDecl *Param,
6982 const Expr *E, APValue &Result,
6983 bool CopyObjectRepresentation) {
6984 // Find the reference argument.
6985 CallStackFrame *Frame = Info.CurrentCall;
6986 APValue *RefValue = Info.getParamSlot(Call: Frame->Arguments, PVD: Param);
6987 if (!RefValue) {
6988 Info.FFDiag(E);
6989 return false;
6990 }
6991
6992 // Copy out the contents of the RHS object.
6993 LValue RefLValue;
6994 RefLValue.setFrom(Ctx: Info.Ctx, V: *RefValue);
6995 return handleLValueToRValueConversion(
6996 Info, Conv: E, Type: Param->getType().getNonReferenceType(), LVal: RefLValue, RVal&: Result,
6997 WantObjectRepresentation: CopyObjectRepresentation);
6998}
6999
7000/// Evaluate a function call.
7001static bool HandleFunctionCall(SourceLocation CallLoc,
7002 const FunctionDecl *Callee,
7003 const LValue *ObjectArg, const Expr *E,
7004 ArrayRef<const Expr *> Args, CallRef Call,
7005 const Stmt *Body, EvalInfo &Info,
7006 APValue &Result, const LValue *ResultSlot) {
7007 if (!Info.CheckCallLimit(Loc: CallLoc))
7008 return false;
7009
7010 CallStackFrame Frame(Info, E->getSourceRange(), Callee, ObjectArg, E, Call);
7011
7012 // For a trivial copy or move assignment, perform an APValue copy. This is
7013 // essential for unions, where the operations performed by the assignment
7014 // operator cannot be represented as statements.
7015 //
7016 // Skip this for non-union classes with no fields; in that case, the defaulted
7017 // copy/move does not actually read the object.
7018 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: Callee);
7019
7020 auto IsTrivialMemoryOperation = [&](const CXXMethodDecl *MD) {
7021 if (!MD || !MD->isDefaulted())
7022 return false;
7023 if (!MD->isCopyAssignmentOperator() && !MD->isMoveAssignmentOperator())
7024 return false;
7025 return MD->getParent()->isUnion() ||
7026 (MD->isTrivial() &&
7027 isReadByLvalueToRvalueConversion(RD: MD->getParent()));
7028 };
7029
7030 if (IsTrivialMemoryOperation(MD)) {
7031 unsigned ExplicitOffset = MD->isExplicitObjectMemberFunction() ? 1 : 0;
7032 assert(ObjectArg);
7033 APValue RHSValue;
7034 if (!handleTrivialCopy(Info, Param: MD->getParamDecl(i: 0), E: Args[0], Result&: RHSValue,
7035 CopyObjectRepresentation: MD->getParent()->isUnion()))
7036 return false;
7037
7038 LValue Obj;
7039 if (!handleAssignment(Info, E: Args[ExplicitOffset], LVal: *ObjectArg,
7040 LValType: MD->getFunctionObjectParameterReferenceType(),
7041 Val&: RHSValue))
7042 return false;
7043 ObjectArg->moveInto(V&: Result);
7044 return true;
7045 } else if (MD && isLambdaCallOperator(MD)) {
7046 // We're in a lambda; determine the lambda capture field maps unless we're
7047 // just constexpr checking a lambda's call operator. constexpr checking is
7048 // done before the captures have been added to the closure object (unless
7049 // we're inferring constexpr-ness), so we don't have access to them in this
7050 // case. But since we don't need the captures to constexpr check, we can
7051 // just ignore them.
7052 if (!Info.checkingPotentialConstantExpression())
7053 MD->getParent()->getCaptureFields(Captures&: Frame.LambdaCaptureFields,
7054 ThisCapture&: Frame.LambdaThisCaptureField);
7055 }
7056
7057 StmtResult Ret = {.Value: Result, .Slot: ResultSlot};
7058 EvalStmtResult ESR = EvaluateStmt(Result&: Ret, Info, S: Body);
7059 if (ESR == ESR_Succeeded) {
7060 if (Callee->getReturnType()->isVoidType())
7061 return true;
7062 Info.FFDiag(Loc: Callee->getEndLoc(), DiagId: diag::note_constexpr_no_return);
7063 }
7064 return ESR == ESR_Returned;
7065}
7066
7067/// Evaluate a constructor call.
7068static bool HandleConstructorCall(const Expr *E, const LValue &This,
7069 CallRef Call,
7070 const CXXConstructorDecl *Definition,
7071 EvalInfo &Info, APValue &Result) {
7072 SourceLocation CallLoc = E->getExprLoc();
7073 if (!Info.CheckCallLimit(Loc: CallLoc))
7074 return false;
7075
7076 const CXXRecordDecl *RD = Definition->getParent();
7077 if (RD->getNumVBases()) {
7078 Info.FFDiag(Loc: CallLoc, DiagId: diag::note_constexpr_virtual_base) << RD;
7079 return false;
7080 }
7081
7082 EvalInfo::EvaluatingConstructorRAII EvalObj(
7083 Info,
7084 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
7085 RD->getNumBases());
7086 CallStackFrame Frame(Info, E->getSourceRange(), Definition, &This, E, Call);
7087
7088 // FIXME: Creating an APValue just to hold a nonexistent return value is
7089 // wasteful.
7090 APValue RetVal;
7091 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7092
7093 // If it's a delegating constructor, delegate.
7094 if (Definition->isDelegatingConstructor()) {
7095 CXXConstructorDecl::init_const_iterator I = Definition->init_begin();
7096 if ((*I)->getInit()->isValueDependent()) {
7097 if (!EvaluateDependentExpr(E: (*I)->getInit(), Info))
7098 return false;
7099 } else {
7100 FullExpressionRAII InitScope(Info);
7101 if (!EvaluateInPlace(Result, Info, This, E: (*I)->getInit()) ||
7102 !InitScope.destroy())
7103 return false;
7104 }
7105 return EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed;
7106 }
7107
7108 // For a trivial copy or move constructor, perform an APValue copy. This is
7109 // essential for unions (or classes with anonymous union members), where the
7110 // operations performed by the constructor cannot be represented by
7111 // ctor-initializers.
7112 //
7113 // Skip this for empty non-union classes; we should not perform an
7114 // lvalue-to-rvalue conversion on them because their copy constructor does not
7115 // actually read them.
7116 if (Definition->isDefaulted() && Definition->isCopyOrMoveConstructor() &&
7117 (Definition->getParent()->isUnion() ||
7118 (Definition->isTrivial() &&
7119 isReadByLvalueToRvalueConversion(RD: Definition->getParent())))) {
7120 return handleTrivialCopy(Info, Param: Definition->getParamDecl(i: 0), E, Result,
7121 CopyObjectRepresentation: Definition->getParent()->isUnion());
7122 }
7123
7124 // Reserve space for the struct members.
7125 if (!Result.hasValue()) {
7126 if (!RD->isUnion())
7127 Result = APValue(APValue::UninitStruct(), RD->getNumBases(),
7128 RD->getNumFields());
7129 else
7130 // A union starts with no active member.
7131 Result = APValue((const FieldDecl*)nullptr);
7132 }
7133
7134 if (RD->isInvalidDecl()) return false;
7135 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7136
7137 // A scope for temporaries lifetime-extended by reference members.
7138 BlockScopeRAII LifetimeExtendedScope(Info);
7139
7140 bool Success = true;
7141 unsigned BasesSeen = 0;
7142#ifndef NDEBUG
7143 CXXRecordDecl::base_class_const_iterator BaseIt = RD->bases_begin();
7144#endif
7145 CXXRecordDecl::field_iterator FieldIt = RD->field_begin();
7146 auto SkipToField = [&](FieldDecl *FD, bool Indirect) {
7147 // We might be initializing the same field again if this is an indirect
7148 // field initialization.
7149 if (FieldIt == RD->field_end() ||
7150 FieldIt->getFieldIndex() > FD->getFieldIndex()) {
7151 assert(Indirect && "fields out of order?");
7152 return;
7153 }
7154
7155 // Default-initialize any fields with no explicit initializer.
7156 for (; !declaresSameEntity(D1: *FieldIt, D2: FD); ++FieldIt) {
7157 assert(FieldIt != RD->field_end() && "missing field?");
7158 if (!FieldIt->isUnnamedBitField())
7159 Success &= handleDefaultInitValue(
7160 T: FieldIt->getType(),
7161 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7162 }
7163 ++FieldIt;
7164 };
7165 for (const auto *I : Definition->inits()) {
7166 LValue Subobject = This;
7167 LValue SubobjectParent = This;
7168 APValue *Value = &Result;
7169
7170 // Determine the subobject to initialize.
7171 FieldDecl *FD = nullptr;
7172 if (I->isBaseInitializer()) {
7173 QualType BaseType(I->getBaseClass(), 0);
7174#ifndef NDEBUG
7175 // Non-virtual base classes are initialized in the order in the class
7176 // definition. We have already checked for virtual base classes.
7177 assert(!BaseIt->isVirtual() && "virtual base for literal type");
7178 assert(Info.Ctx.hasSameUnqualifiedType(BaseIt->getType(), BaseType) &&
7179 "base class initializers not in expected order");
7180 ++BaseIt;
7181#endif
7182 if (!HandleLValueDirectBase(Info, E: I->getInit(), Obj&: Subobject, Derived: RD,
7183 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7184 return false;
7185 Value = &Result.getStructBase(i: BasesSeen++);
7186 } else if ((FD = I->getMember())) {
7187 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD, RL: &Layout))
7188 return false;
7189 if (RD->isUnion()) {
7190 Result = APValue(FD);
7191 Value = &Result.getUnionValue();
7192 } else {
7193 SkipToField(FD, false);
7194 Value = &Result.getStructField(i: FD->getFieldIndex());
7195 }
7196 } else if (IndirectFieldDecl *IFD = I->getIndirectMember()) {
7197 // Walk the indirect field decl's chain to find the object to initialize,
7198 // and make sure we've initialized every step along it.
7199 auto IndirectFieldChain = IFD->chain();
7200 for (auto *C : IndirectFieldChain) {
7201 FD = cast<FieldDecl>(Val: C);
7202 CXXRecordDecl *CD = cast<CXXRecordDecl>(Val: FD->getParent());
7203 // Switch the union field if it differs. This happens if we had
7204 // preceding zero-initialization, and we're now initializing a union
7205 // subobject other than the first.
7206 // FIXME: In this case, the values of the other subobjects are
7207 // specified, since zero-initialization sets all padding bits to zero.
7208 if (!Value->hasValue() ||
7209 (Value->isUnion() &&
7210 !declaresSameEntity(D1: Value->getUnionField(), D2: FD))) {
7211 if (CD->isUnion())
7212 *Value = APValue(FD);
7213 else
7214 // FIXME: This immediately starts the lifetime of all members of
7215 // an anonymous struct. It would be preferable to strictly start
7216 // member lifetime in initialization order.
7217 Success &= handleDefaultInitValue(T: Info.Ctx.getCanonicalTagType(TD: CD),
7218 Result&: *Value);
7219 }
7220 // Store Subobject as its parent before updating it for the last element
7221 // in the chain.
7222 if (C == IndirectFieldChain.back())
7223 SubobjectParent = Subobject;
7224 if (!HandleLValueMember(Info, E: I->getInit(), LVal&: Subobject, FD))
7225 return false;
7226 if (CD->isUnion())
7227 Value = &Value->getUnionValue();
7228 else {
7229 if (C == IndirectFieldChain.front() && !RD->isUnion())
7230 SkipToField(FD, true);
7231 Value = &Value->getStructField(i: FD->getFieldIndex());
7232 }
7233 }
7234 } else {
7235 llvm_unreachable("unknown base initializer kind");
7236 }
7237
7238 // Need to override This for implicit field initializers as in this case
7239 // This refers to innermost anonymous struct/union containing initializer,
7240 // not to currently constructed class.
7241 const Expr *Init = I->getInit();
7242 if (Init->isValueDependent()) {
7243 if (!EvaluateDependentExpr(E: Init, Info))
7244 return false;
7245 } else {
7246 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &SubobjectParent,
7247 isa<CXXDefaultInitExpr>(Val: Init));
7248 FullExpressionRAII InitScope(Info);
7249 if (FD && FD->getType()->isReferenceType() &&
7250 !FD->getType()->isFunctionReferenceType()) {
7251 LValue Result;
7252 if (!EvaluateInitForDeclOfReferenceType(Info, D: FD, Init, Result,
7253 Val&: *Value)) {
7254 if (!Info.noteFailure())
7255 return false;
7256 Success = false;
7257 }
7258 } else if (!EvaluateInPlace(Result&: *Value, Info, This: Subobject, E: Init) ||
7259 (FD && FD->isBitField() &&
7260 !truncateBitfieldValue(Info, E: Init, Value&: *Value, FD))) {
7261 // If we're checking for a potential constant expression, evaluate all
7262 // initializers even if some of them fail.
7263 if (!Info.noteFailure())
7264 return false;
7265 Success = false;
7266 }
7267 }
7268
7269 // This is the point at which the dynamic type of the object becomes this
7270 // class type.
7271 if (I->isBaseInitializer() && BasesSeen == RD->getNumBases())
7272 EvalObj.finishedConstructingBases();
7273 }
7274
7275 // Default-initialize any remaining fields.
7276 if (!RD->isUnion()) {
7277 for (; FieldIt != RD->field_end(); ++FieldIt) {
7278 if (!FieldIt->isUnnamedBitField())
7279 Success &= handleDefaultInitValue(
7280 T: FieldIt->getType(),
7281 Result&: Result.getStructField(i: FieldIt->getFieldIndex()));
7282 }
7283 }
7284
7285 EvalObj.finishedConstructingFields();
7286
7287 return Success &&
7288 EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) != ESR_Failed &&
7289 LifetimeExtendedScope.destroy();
7290}
7291
7292static bool HandleConstructorCall(const Expr *E, const LValue &This,
7293 ArrayRef<const Expr*> Args,
7294 const CXXConstructorDecl *Definition,
7295 EvalInfo &Info, APValue &Result) {
7296 CallScopeRAII CallScope(Info);
7297 CallRef Call = Info.CurrentCall->createCall(Callee: Definition);
7298 if (!EvaluateArgs(Args, Call, Info, Callee: Definition))
7299 return false;
7300
7301 return HandleConstructorCall(E, This, Call, Definition, Info, Result) &&
7302 CallScope.destroy();
7303}
7304
7305static bool HandleDestructionImpl(EvalInfo &Info, SourceRange CallRange,
7306 const LValue &This, APValue &Value,
7307 QualType T) {
7308 // Objects can only be destroyed while they're within their lifetimes.
7309 // FIXME: We have no representation for whether an object of type nullptr_t
7310 // is in its lifetime; it usually doesn't matter. Perhaps we should model it
7311 // as indeterminate instead?
7312 if (Value.isAbsent() && !T->isNullPtrType()) {
7313 APValue Printable;
7314 This.moveInto(V&: Printable);
7315 Info.FFDiag(Loc: CallRange.getBegin(),
7316 DiagId: diag::note_constexpr_destroy_out_of_lifetime)
7317 << Printable.getAsString(Ctx: Info.Ctx, Ty: Info.Ctx.getLValueReferenceType(T));
7318 return false;
7319 }
7320
7321 // Invent an expression for location purposes.
7322 // FIXME: We shouldn't need to do this.
7323 OpaqueValueExpr LocE(CallRange.getBegin(), Info.Ctx.IntTy, VK_PRValue);
7324
7325 // For arrays, destroy elements right-to-left.
7326 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T)) {
7327 uint64_t Size = CAT->getZExtSize();
7328 QualType ElemT = CAT->getElementType();
7329
7330 if (!CheckArraySize(Info, CAT, CallLoc: CallRange.getBegin()))
7331 return false;
7332
7333 LValue ElemLV = This;
7334 ElemLV.addArray(Info, E: &LocE, CAT);
7335 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: Size))
7336 return false;
7337
7338 // Ensure that we have actual array elements available to destroy; the
7339 // destructors might mutate the value, so we can't run them on the array
7340 // filler.
7341 if (Size && Size > Value.getArrayInitializedElts())
7342 expandArray(Array&: Value, Index: Value.getArraySize() - 1);
7343
7344 // The size of the array might have been reduced by
7345 // a placement new.
7346 for (Size = Value.getArraySize(); Size != 0; --Size) {
7347 APValue &Elem = Value.getArrayInitializedElt(I: Size - 1);
7348 if (!HandleLValueArrayAdjustment(Info, E: &LocE, LVal&: ElemLV, EltTy: ElemT, Adjustment: -1) ||
7349 !HandleDestructionImpl(Info, CallRange, This: ElemLV, Value&: Elem, T: ElemT))
7350 return false;
7351 }
7352
7353 // End the lifetime of this array now.
7354 Value = APValue();
7355 return true;
7356 }
7357
7358 const CXXRecordDecl *RD = T->getAsCXXRecordDecl();
7359 if (!RD) {
7360 if (T.isDestructedType()) {
7361 Info.FFDiag(Loc: CallRange.getBegin(),
7362 DiagId: diag::note_constexpr_unsupported_destruction)
7363 << T;
7364 return false;
7365 }
7366
7367 Value = APValue();
7368 return true;
7369 }
7370
7371 if (RD->getNumVBases()) {
7372 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_virtual_base) << RD;
7373 return false;
7374 }
7375
7376 const CXXDestructorDecl *DD = RD->getDestructor();
7377 if (!DD && !RD->hasTrivialDestructor()) {
7378 Info.FFDiag(Loc: CallRange.getBegin());
7379 return false;
7380 }
7381
7382 if (!DD || DD->isTrivial() ||
7383 (RD->isAnonymousStructOrUnion() && RD->isUnion())) {
7384 // A trivial destructor just ends the lifetime of the object. Check for
7385 // this case before checking for a body, because we might not bother
7386 // building a body for a trivial destructor. Note that it doesn't matter
7387 // whether the destructor is constexpr in this case; all trivial
7388 // destructors are constexpr.
7389 //
7390 // If an anonymous union would be destroyed, some enclosing destructor must
7391 // have been explicitly defined, and the anonymous union destruction should
7392 // have no effect.
7393 Value = APValue();
7394 return true;
7395 }
7396
7397 if (!Info.CheckCallLimit(Loc: CallRange.getBegin()))
7398 return false;
7399
7400 const FunctionDecl *Definition = nullptr;
7401 const Stmt *Body = DD->getBody(Definition);
7402
7403 if (!CheckConstexprFunction(Info, CallLoc: CallRange.getBegin(), Declaration: DD, Definition, Body))
7404 return false;
7405
7406 CallStackFrame Frame(Info, CallRange, Definition, &This, /*CallExpr=*/nullptr,
7407 CallRef());
7408
7409 // We're now in the period of destruction of this object.
7410 unsigned BasesLeft = RD->getNumBases();
7411 EvalInfo::EvaluatingDestructorRAII EvalObj(
7412 Info,
7413 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries});
7414 if (!EvalObj.DidInsert) {
7415 // C++2a [class.dtor]p19:
7416 // the behavior is undefined if the destructor is invoked for an object
7417 // whose lifetime has ended
7418 // (Note that formally the lifetime ends when the period of destruction
7419 // begins, even though certain uses of the object remain valid until the
7420 // period of destruction ends.)
7421 Info.FFDiag(Loc: CallRange.getBegin(), DiagId: diag::note_constexpr_double_destroy);
7422 return false;
7423 }
7424
7425 // FIXME: Creating an APValue just to hold a nonexistent return value is
7426 // wasteful.
7427 APValue RetVal;
7428 StmtResult Ret = {.Value: RetVal, .Slot: nullptr};
7429 if (EvaluateStmt(Result&: Ret, Info, S: Definition->getBody()) == ESR_Failed)
7430 return false;
7431
7432 // A union destructor does not implicitly destroy its members.
7433 if (RD->isUnion())
7434 return true;
7435
7436 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7437
7438 // We don't have a good way to iterate fields in reverse, so collect all the
7439 // fields first and then walk them backwards.
7440 SmallVector<FieldDecl*, 16> Fields(RD->fields());
7441 for (const FieldDecl *FD : llvm::reverse(C&: Fields)) {
7442 if (FD->isUnnamedBitField())
7443 continue;
7444
7445 LValue Subobject = This;
7446 if (!HandleLValueMember(Info, E: &LocE, LVal&: Subobject, FD, RL: &Layout))
7447 return false;
7448
7449 APValue *SubobjectValue = &Value.getStructField(i: FD->getFieldIndex());
7450 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7451 T: FD->getType()))
7452 return false;
7453 }
7454
7455 if (BasesLeft != 0)
7456 EvalObj.startedDestroyingBases();
7457
7458 // Destroy base classes in reverse order.
7459 for (const CXXBaseSpecifier &Base : llvm::reverse(C: RD->bases())) {
7460 --BasesLeft;
7461
7462 QualType BaseType = Base.getType();
7463 LValue Subobject = This;
7464 if (!HandleLValueDirectBase(Info, E: &LocE, Obj&: Subobject, Derived: RD,
7465 Base: BaseType->getAsCXXRecordDecl(), RL: &Layout))
7466 return false;
7467
7468 APValue *SubobjectValue = &Value.getStructBase(i: BasesLeft);
7469 if (!HandleDestructionImpl(Info, CallRange, This: Subobject, Value&: *SubobjectValue,
7470 T: BaseType))
7471 return false;
7472 }
7473 assert(BasesLeft == 0 && "NumBases was wrong?");
7474
7475 // The period of destruction ends now. The object is gone.
7476 Value = APValue();
7477 return true;
7478}
7479
7480namespace {
7481struct DestroyObjectHandler {
7482 EvalInfo &Info;
7483 const Expr *E;
7484 const LValue &This;
7485 const AccessKinds AccessKind;
7486
7487 typedef bool result_type;
7488 bool failed() { return false; }
7489 bool found(APValue &Subobj, QualType SubobjType, APValue::LValueBase Base) {
7490 return HandleDestructionImpl(Info, CallRange: E->getSourceRange(), This, Value&: Subobj,
7491 T: SubobjType);
7492 }
7493 bool found(APSInt &Value, QualType SubobjType) {
7494 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7495 return false;
7496 }
7497 bool found(APFloat &Value, QualType SubobjType) {
7498 Info.FFDiag(E, DiagId: diag::note_constexpr_destroy_complex_elem);
7499 return false;
7500 }
7501};
7502}
7503
7504/// Perform a destructor or pseudo-destructor call on the given object, which
7505/// might in general not be a complete object.
7506static bool HandleDestruction(EvalInfo &Info, const Expr *E,
7507 const LValue &This, QualType ThisType) {
7508 CompleteObject Obj = findCompleteObject(Info, E, AK: AK_Destroy, LVal: This, LValType: ThisType);
7509 DestroyObjectHandler Handler = {.Info: Info, .E: E, .This: This, .AccessKind: AK_Destroy};
7510 return Obj && findSubobject(Info, E, Obj, Sub: This.Designator, handler&: Handler);
7511}
7512
7513/// Destroy and end the lifetime of the given complete object.
7514static bool HandleDestruction(EvalInfo &Info, SourceLocation Loc,
7515 APValue::LValueBase LVBase, APValue &Value,
7516 QualType T) {
7517 // If we've had an unmodeled side-effect, we can't rely on mutable state
7518 // (such as the object we're about to destroy) being correct.
7519 if (Info.EvalStatus.HasSideEffects)
7520 return false;
7521
7522 LValue LV;
7523 LV.set(B: {LVBase});
7524 return HandleDestructionImpl(Info, CallRange: Loc, This: LV, Value, T);
7525}
7526
7527/// Perform a call to 'operator new' or to `__builtin_operator_new'.
7528static bool HandleOperatorNewCall(EvalInfo &Info, const CallExpr *E,
7529 LValue &Result) {
7530 if (Info.checkingPotentialConstantExpression() ||
7531 Info.SpeculativeEvaluationDepth)
7532 return false;
7533
7534 // This is permitted only within a call to std::allocator<T>::allocate.
7535 auto Caller = Info.getStdAllocatorCaller(FnName: "allocate");
7536 if (!Caller) {
7537 Info.FFDiag(Loc: E->getExprLoc(), DiagId: Info.getLangOpts().CPlusPlus20
7538 ? diag::note_constexpr_new_untyped
7539 : diag::note_constexpr_new);
7540 return false;
7541 }
7542
7543 QualType ElemType = Caller.ElemType;
7544 if (ElemType->isIncompleteType() || ElemType->isFunctionType()) {
7545 Info.FFDiag(Loc: E->getExprLoc(),
7546 DiagId: diag::note_constexpr_new_not_complete_object_type)
7547 << (ElemType->isIncompleteType() ? 0 : 1) << ElemType;
7548 return false;
7549 }
7550
7551 APSInt ByteSize;
7552 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: ByteSize, Info))
7553 return false;
7554 bool IsNothrow = false;
7555 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I) {
7556 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7557 IsNothrow |= E->getType()->isNothrowT();
7558 }
7559
7560 CharUnits ElemSize;
7561 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElemType, Size&: ElemSize))
7562 return false;
7563 APInt Size, Remainder;
7564 APInt ElemSizeAP(ByteSize.getBitWidth(), ElemSize.getQuantity());
7565 APInt::udivrem(LHS: ByteSize, RHS: ElemSizeAP, Quotient&: Size, Remainder);
7566 if (Remainder != 0) {
7567 // This likely indicates a bug in the implementation of 'std::allocator'.
7568 Info.FFDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_operator_new_bad_size)
7569 << ByteSize << APSInt(ElemSizeAP, true) << ElemType;
7570 return false;
7571 }
7572
7573 if (!Info.CheckArraySize(Loc: E->getBeginLoc(), BitWidth: ByteSize.getActiveBits(),
7574 ElemCount: Size.getZExtValue(), /*Diag=*/!IsNothrow)) {
7575 if (IsNothrow) {
7576 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
7577 return true;
7578 }
7579 return false;
7580 }
7581
7582 QualType AllocType = Info.Ctx.getConstantArrayType(
7583 EltTy: ElemType, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
7584 APValue *Val = Info.createHeapAlloc(E: Caller.Call, T: AllocType, LV&: Result);
7585 *Val = APValue(APValue::UninitArray(), 0, Size.getZExtValue());
7586 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: AllocType));
7587 return true;
7588}
7589
7590static bool hasVirtualDestructor(QualType T) {
7591 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7592 if (CXXDestructorDecl *DD = RD->getDestructor())
7593 return DD->isVirtual();
7594 return false;
7595}
7596
7597static const FunctionDecl *getVirtualOperatorDelete(QualType T) {
7598 if (CXXRecordDecl *RD = T->getAsCXXRecordDecl())
7599 if (CXXDestructorDecl *DD = RD->getDestructor())
7600 return DD->isVirtual() ? DD->getOperatorDelete() : nullptr;
7601 return nullptr;
7602}
7603
7604/// Check that the given object is a suitable pointer to a heap allocation that
7605/// still exists and is of the right kind for the purpose of a deletion.
7606///
7607/// On success, returns the heap allocation to deallocate. On failure, produces
7608/// a diagnostic and returns std::nullopt.
7609static std::optional<DynAlloc *> CheckDeleteKind(EvalInfo &Info, const Expr *E,
7610 const LValue &Pointer,
7611 DynAlloc::Kind DeallocKind) {
7612 auto PointerAsString = [&] {
7613 return Pointer.toString(Ctx&: Info.Ctx, T: Info.Ctx.VoidPtrTy);
7614 };
7615
7616 DynamicAllocLValue DA = Pointer.Base.dyn_cast<DynamicAllocLValue>();
7617 if (!DA) {
7618 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_not_heap_alloc)
7619 << PointerAsString();
7620 if (Pointer.Base)
7621 NoteLValueLocation(Info, Base: Pointer.Base);
7622 return std::nullopt;
7623 }
7624
7625 std::optional<DynAlloc *> Alloc = Info.lookupDynamicAlloc(DA);
7626 if (!Alloc) {
7627 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
7628 return std::nullopt;
7629 }
7630
7631 if (DeallocKind != (*Alloc)->getKind()) {
7632 QualType AllocType = Pointer.Base.getDynamicAllocType();
7633 Info.FFDiag(E, DiagId: diag::note_constexpr_new_delete_mismatch)
7634 << DeallocKind << (*Alloc)->getKind() << AllocType;
7635 NoteLValueLocation(Info, Base: Pointer.Base);
7636 return std::nullopt;
7637 }
7638
7639 bool Subobject = false;
7640 if (DeallocKind == DynAlloc::New) {
7641 Subobject = Pointer.Designator.MostDerivedPathLength != 0 ||
7642 Pointer.Designator.isOnePastTheEnd();
7643 } else {
7644 Subobject = Pointer.Designator.Entries.size() != 1 ||
7645 Pointer.Designator.Entries[0].getAsArrayIndex() != 0;
7646 }
7647 if (Subobject) {
7648 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_subobject)
7649 << PointerAsString() << Pointer.Designator.isOnePastTheEnd();
7650 return std::nullopt;
7651 }
7652
7653 return Alloc;
7654}
7655
7656// Perform a call to 'operator delete' or '__builtin_operator_delete'.
7657static bool HandleOperatorDeleteCall(EvalInfo &Info, const CallExpr *E) {
7658 if (Info.checkingPotentialConstantExpression() ||
7659 Info.SpeculativeEvaluationDepth)
7660 return false;
7661
7662 // This is permitted only within a call to std::allocator<T>::deallocate.
7663 if (!Info.getStdAllocatorCaller(FnName: "deallocate")) {
7664 Info.FFDiag(Loc: E->getExprLoc());
7665 return true;
7666 }
7667
7668 LValue Pointer;
7669 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Pointer, Info))
7670 return false;
7671 for (unsigned I = 1, N = E->getNumArgs(); I != N; ++I)
7672 EvaluateIgnoredValue(Info, E: E->getArg(Arg: I));
7673
7674 if (Pointer.Designator.Invalid)
7675 return false;
7676
7677 // Deleting a null pointer would have no effect, but it's not permitted by
7678 // std::allocator<T>::deallocate's contract.
7679 if (Pointer.isNullPointer()) {
7680 Info.CCEDiag(Loc: E->getExprLoc(), DiagId: diag::note_constexpr_deallocate_null);
7681 return true;
7682 }
7683
7684 if (!CheckDeleteKind(Info, E, Pointer, DeallocKind: DynAlloc::StdAllocator))
7685 return false;
7686
7687 Info.HeapAllocs.erase(x: Pointer.Base.get<DynamicAllocLValue>());
7688 return true;
7689}
7690
7691//===----------------------------------------------------------------------===//
7692// Generic Evaluation
7693//===----------------------------------------------------------------------===//
7694namespace {
7695
7696class BitCastBuffer {
7697 // FIXME: We're going to need bit-level granularity when we support
7698 // bit-fields.
7699 // FIXME: Its possible under the C++ standard for 'char' to not be 8 bits, but
7700 // we don't support a host or target where that is the case. Still, we should
7701 // use a more generic type in case we ever do.
7702 SmallVector<std::optional<unsigned char>, 32> Bytes;
7703
7704 static_assert(std::numeric_limits<unsigned char>::digits >= 8,
7705 "Need at least 8 bit unsigned char");
7706
7707 bool TargetIsLittleEndian;
7708
7709public:
7710 BitCastBuffer(CharUnits Width, bool TargetIsLittleEndian)
7711 : Bytes(Width.getQuantity()),
7712 TargetIsLittleEndian(TargetIsLittleEndian) {}
7713
7714 [[nodiscard]] bool readObject(CharUnits Offset, CharUnits Width,
7715 SmallVectorImpl<unsigned char> &Output) const {
7716 for (CharUnits I = Offset, E = Offset + Width; I != E; ++I) {
7717 // If a byte of an integer is uninitialized, then the whole integer is
7718 // uninitialized.
7719 if (!Bytes[I.getQuantity()])
7720 return false;
7721 Output.push_back(Elt: *Bytes[I.getQuantity()]);
7722 }
7723 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
7724 std::reverse(first: Output.begin(), last: Output.end());
7725 return true;
7726 }
7727
7728 void writeObject(CharUnits Offset, SmallVectorImpl<unsigned char> &Input) {
7729 if (llvm::sys::IsLittleEndianHost != TargetIsLittleEndian)
7730 std::reverse(first: Input.begin(), last: Input.end());
7731
7732 size_t Index = 0;
7733 for (unsigned char Byte : Input) {
7734 assert(!Bytes[Offset.getQuantity() + Index] && "overwriting a byte?");
7735 Bytes[Offset.getQuantity() + Index] = Byte;
7736 ++Index;
7737 }
7738 }
7739
7740 size_t size() { return Bytes.size(); }
7741};
7742
7743/// Traverse an APValue to produce an BitCastBuffer, emulating how the current
7744/// target would represent the value at runtime.
7745class APValueToBufferConverter {
7746 EvalInfo &Info;
7747 BitCastBuffer Buffer;
7748 const CastExpr *BCE;
7749
7750 APValueToBufferConverter(EvalInfo &Info, CharUnits ObjectWidth,
7751 const CastExpr *BCE)
7752 : Info(Info),
7753 Buffer(ObjectWidth, Info.Ctx.getTargetInfo().isLittleEndian()),
7754 BCE(BCE) {}
7755
7756 bool visit(const APValue &Val, QualType Ty) {
7757 return visit(Val, Ty, Offset: CharUnits::fromQuantity(Quantity: 0));
7758 }
7759
7760 // Write out Val with type Ty into Buffer starting at Offset.
7761 bool visit(const APValue &Val, QualType Ty, CharUnits Offset) {
7762 assert((size_t)Offset.getQuantity() <= Buffer.size());
7763
7764 // As a special case, nullptr_t has an indeterminate value.
7765 if (Ty->isNullPtrType())
7766 return true;
7767
7768 // Dig through Src to find the byte at SrcOffset.
7769 switch (Val.getKind()) {
7770 case APValue::Indeterminate:
7771 case APValue::None:
7772 return true;
7773
7774 case APValue::Int:
7775 return visitInt(Val: Val.getInt(), Ty, Offset);
7776 case APValue::Float:
7777 return visitFloat(Val: Val.getFloat(), Ty, Offset);
7778 case APValue::Array:
7779 return visitArray(Val, Ty, Offset);
7780 case APValue::Struct:
7781 return visitRecord(Val, Ty, Offset);
7782 case APValue::Vector:
7783 return visitVector(Val, Ty, Offset);
7784
7785 case APValue::ComplexInt:
7786 case APValue::ComplexFloat:
7787 return visitComplex(Val, Ty, Offset);
7788 case APValue::FixedPoint:
7789 // FIXME: We should support these.
7790
7791 case APValue::LValue:
7792 case APValue::Matrix:
7793 case APValue::Union:
7794 case APValue::MemberPointer:
7795 case APValue::AddrLabelDiff: {
7796 Info.FFDiag(Loc: BCE->getBeginLoc(),
7797 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
7798 << Ty;
7799 return false;
7800 }
7801 }
7802 llvm_unreachable("Unhandled APValue::ValueKind");
7803 }
7804
7805 bool visitRecord(const APValue &Val, QualType Ty, CharUnits Offset) {
7806 const RecordDecl *RD = Ty->getAsRecordDecl();
7807 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
7808
7809 // Visit the base classes.
7810 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
7811 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
7812 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
7813 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
7814 const APValue &Base = Val.getStructBase(i: I);
7815
7816 // Can happen in error cases.
7817 if (!Base.isStruct())
7818 return false;
7819
7820 if (!visitRecord(Val: Base, Ty: BS.getType(),
7821 Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset))
7822 return false;
7823 }
7824 }
7825
7826 // Visit the fields.
7827 unsigned FieldIdx = 0;
7828 for (FieldDecl *FD : RD->fields()) {
7829 if (FD->isBitField()) {
7830 Info.FFDiag(Loc: BCE->getBeginLoc(),
7831 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
7832 return false;
7833 }
7834
7835 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
7836
7837 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0 &&
7838 "only bit-fields can have sub-char alignment");
7839 CharUnits FieldOffset =
7840 Info.Ctx.toCharUnitsFromBits(BitSize: FieldOffsetBits) + Offset;
7841 QualType FieldTy = FD->getType();
7842 if (!visit(Val: Val.getStructField(i: FieldIdx), Ty: FieldTy, Offset: FieldOffset))
7843 return false;
7844 ++FieldIdx;
7845 }
7846
7847 return true;
7848 }
7849
7850 bool visitArray(const APValue &Val, QualType Ty, CharUnits Offset) {
7851 const auto *CAT =
7852 dyn_cast_or_null<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe());
7853 if (!CAT)
7854 return false;
7855
7856 CharUnits ElemWidth = Info.Ctx.getTypeSizeInChars(T: CAT->getElementType());
7857 unsigned NumInitializedElts = Val.getArrayInitializedElts();
7858 unsigned ArraySize = Val.getArraySize();
7859 // First, initialize the initialized elements.
7860 for (unsigned I = 0; I != NumInitializedElts; ++I) {
7861 const APValue &SubObj = Val.getArrayInitializedElt(I);
7862 if (!visit(Val: SubObj, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
7863 return false;
7864 }
7865
7866 // Next, initialize the rest of the array using the filler.
7867 if (Val.hasArrayFiller()) {
7868 const APValue &Filler = Val.getArrayFiller();
7869 for (unsigned I = NumInitializedElts; I != ArraySize; ++I) {
7870 if (!visit(Val: Filler, Ty: CAT->getElementType(), Offset: Offset + I * ElemWidth))
7871 return false;
7872 }
7873 }
7874
7875 return true;
7876 }
7877
7878 bool visitComplex(const APValue &Val, QualType Ty, CharUnits Offset) {
7879 const ComplexType *ComplexTy = Ty->castAs<ComplexType>();
7880 QualType EltTy = ComplexTy->getElementType();
7881 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
7882 bool IsInt = Val.isComplexInt();
7883
7884 if (IsInt) {
7885 if (!visitInt(Val: Val.getComplexIntReal(), Ty: EltTy,
7886 Offset: Offset + (0 * EltSizeChars)))
7887 return false;
7888 if (!visitInt(Val: Val.getComplexIntImag(), Ty: EltTy,
7889 Offset: Offset + (1 * EltSizeChars)))
7890 return false;
7891 } else {
7892 if (!visitFloat(Val: Val.getComplexFloatReal(), Ty: EltTy,
7893 Offset: Offset + (0 * EltSizeChars)))
7894 return false;
7895 if (!visitFloat(Val: Val.getComplexFloatImag(), Ty: EltTy,
7896 Offset: Offset + (1 * EltSizeChars)))
7897 return false;
7898 }
7899
7900 return true;
7901 }
7902
7903 bool visitVector(const APValue &Val, QualType Ty, CharUnits Offset) {
7904 const VectorType *VTy = Ty->castAs<VectorType>();
7905 QualType EltTy = VTy->getElementType();
7906 unsigned NElts = VTy->getNumElements();
7907
7908 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
7909 // Special handling for OpenCL bool vectors:
7910 // Since these vectors are stored as packed bits, but we can't write
7911 // individual bits to the BitCastBuffer, we'll buffer all of the elements
7912 // together into an appropriately sized APInt and write them all out at
7913 // once. Because we don't accept vectors where NElts * EltSize isn't a
7914 // multiple of the char size, there will be no padding space, so we don't
7915 // have to worry about writing data which should have been left
7916 // uninitialized.
7917 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
7918
7919 llvm::APInt Res = llvm::APInt::getZero(numBits: NElts);
7920 for (unsigned I = 0; I < NElts; ++I) {
7921 const llvm::APSInt &EltAsInt = Val.getVectorElt(I).getInt();
7922 assert(EltAsInt.isUnsigned() && EltAsInt.getBitWidth() == 1 &&
7923 "bool vector element must be 1-bit unsigned integer!");
7924
7925 Res.insertBits(SubBits: EltAsInt, bitPosition: BigEndian ? (NElts - I - 1) : I);
7926 }
7927
7928 SmallVector<uint8_t, 8> Bytes(NElts / 8);
7929 llvm::StoreIntToMemory(IntVal: Res, Dst: &*Bytes.begin(), StoreBytes: NElts / 8);
7930 Buffer.writeObject(Offset, Input&: Bytes);
7931 } else {
7932 // Iterate over each of the elements and write them out to the buffer at
7933 // the appropriate offset.
7934 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
7935 for (unsigned I = 0; I < NElts; ++I) {
7936 if (!visit(Val: Val.getVectorElt(I), Ty: EltTy, Offset: Offset + I * EltSizeChars))
7937 return false;
7938 }
7939 }
7940
7941 return true;
7942 }
7943
7944 bool visitInt(const APSInt &Val, QualType Ty, CharUnits Offset) {
7945 APSInt AdjustedVal = Val;
7946 unsigned Width = AdjustedVal.getBitWidth();
7947 if (Ty->isBooleanType()) {
7948 Width = Info.Ctx.getTypeSize(T: Ty);
7949 AdjustedVal = AdjustedVal.extend(width: Width);
7950 }
7951
7952 SmallVector<uint8_t, 8> Bytes(Width / 8);
7953 llvm::StoreIntToMemory(IntVal: AdjustedVal, Dst: &*Bytes.begin(), StoreBytes: Width / 8);
7954 Buffer.writeObject(Offset, Input&: Bytes);
7955 return true;
7956 }
7957
7958 bool visitFloat(const APFloat &Val, QualType Ty, CharUnits Offset) {
7959 APSInt AsInt(Val.bitcastToAPInt());
7960 return visitInt(Val: AsInt, Ty, Offset);
7961 }
7962
7963public:
7964 static std::optional<BitCastBuffer>
7965 convert(EvalInfo &Info, const APValue &Src, const CastExpr *BCE) {
7966 CharUnits DstSize = Info.Ctx.getTypeSizeInChars(T: BCE->getType());
7967 APValueToBufferConverter Converter(Info, DstSize, BCE);
7968 if (!Converter.visit(Val: Src, Ty: BCE->getSubExpr()->getType()))
7969 return std::nullopt;
7970 return Converter.Buffer;
7971 }
7972};
7973
7974/// Write an BitCastBuffer into an APValue.
7975class BufferToAPValueConverter {
7976 EvalInfo &Info;
7977 const BitCastBuffer &Buffer;
7978 const CastExpr *BCE;
7979
7980 BufferToAPValueConverter(EvalInfo &Info, const BitCastBuffer &Buffer,
7981 const CastExpr *BCE)
7982 : Info(Info), Buffer(Buffer), BCE(BCE) {}
7983
7984 // Emit an unsupported bit_cast type error. Sema refuses to build a bit_cast
7985 // with an invalid type, so anything left is a deficiency on our part (FIXME).
7986 // Ideally this will be unreachable.
7987 std::nullopt_t unsupportedType(QualType Ty) {
7988 Info.FFDiag(Loc: BCE->getBeginLoc(),
7989 DiagId: diag::note_constexpr_bit_cast_unsupported_type)
7990 << Ty;
7991 return std::nullopt;
7992 }
7993
7994 std::nullopt_t unrepresentableValue(QualType Ty, const APSInt &Val) {
7995 Info.FFDiag(Loc: BCE->getBeginLoc(),
7996 DiagId: diag::note_constexpr_bit_cast_unrepresentable_value)
7997 << Ty << toString(I: Val, /*Radix=*/10);
7998 return std::nullopt;
7999 }
8000
8001 std::optional<APValue> visit(const BuiltinType *T, CharUnits Offset,
8002 const EnumType *EnumSugar = nullptr) {
8003 if (T->isNullPtrType()) {
8004 uint64_t NullValue = Info.Ctx.getTargetNullPointerValue(QT: QualType(T, 0));
8005 return APValue((Expr *)nullptr,
8006 /*Offset=*/CharUnits::fromQuantity(Quantity: NullValue),
8007 APValue::NoLValuePath{}, /*IsNullPtr=*/true);
8008 }
8009
8010 CharUnits SizeOf = Info.Ctx.getTypeSizeInChars(T);
8011
8012 // Work around floating point types that contain unused padding bytes. This
8013 // is really just `long double` on x86, which is the only fundamental type
8014 // with padding bytes.
8015 if (T->isRealFloatingType()) {
8016 const llvm::fltSemantics &Semantics =
8017 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8018 unsigned NumBits = llvm::APFloatBase::getSizeInBits(Sem: Semantics);
8019 assert(NumBits % 8 == 0);
8020 CharUnits NumBytes = CharUnits::fromQuantity(Quantity: NumBits / 8);
8021 if (NumBytes != SizeOf)
8022 SizeOf = NumBytes;
8023 }
8024
8025 SmallVector<uint8_t, 8> Bytes;
8026 if (!Buffer.readObject(Offset, Width: SizeOf, Output&: Bytes)) {
8027 // If this is std::byte or unsigned char, then its okay to store an
8028 // indeterminate value.
8029 bool IsStdByte = EnumSugar && EnumSugar->isStdByteType();
8030 bool IsUChar =
8031 !EnumSugar && (T->isSpecificBuiltinType(K: BuiltinType::UChar) ||
8032 T->isSpecificBuiltinType(K: BuiltinType::Char_U));
8033 if (!IsStdByte && !IsUChar) {
8034 QualType DisplayType(EnumSugar ? (const Type *)EnumSugar : T, 0);
8035 Info.FFDiag(Loc: BCE->getExprLoc(),
8036 DiagId: diag::note_constexpr_bit_cast_indet_dest)
8037 << DisplayType << Info.Ctx.getLangOpts().CharIsSigned;
8038 return std::nullopt;
8039 }
8040
8041 return APValue::IndeterminateValue();
8042 }
8043
8044 APSInt Val(SizeOf.getQuantity() * Info.Ctx.getCharWidth(), true);
8045 llvm::LoadIntFromMemory(IntVal&: Val, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8046
8047 if (T->isIntegralOrEnumerationType()) {
8048 Val.setIsSigned(T->isSignedIntegerOrEnumerationType());
8049
8050 unsigned IntWidth = Info.Ctx.getIntWidth(T: QualType(T, 0));
8051 if (IntWidth != Val.getBitWidth()) {
8052 APSInt Truncated = Val.trunc(width: IntWidth);
8053 if (Truncated.extend(width: Val.getBitWidth()) != Val)
8054 return unrepresentableValue(Ty: QualType(T, 0), Val);
8055 Val = Truncated;
8056 }
8057
8058 return APValue(Val);
8059 }
8060
8061 if (T->isRealFloatingType()) {
8062 const llvm::fltSemantics &Semantics =
8063 Info.Ctx.getFloatTypeSemantics(T: QualType(T, 0));
8064 return APValue(APFloat(Semantics, Val));
8065 }
8066
8067 return unsupportedType(Ty: QualType(T, 0));
8068 }
8069
8070 std::optional<APValue> visit(const RecordType *RTy, CharUnits Offset) {
8071 const RecordDecl *RD = RTy->getAsRecordDecl();
8072 if (RD->isInvalidDecl())
8073 return std::nullopt;
8074 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
8075
8076 unsigned NumBases = 0;
8077 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD))
8078 NumBases = CXXRD->getNumBases();
8079
8080 APValue ResultVal(APValue::UninitStruct(), NumBases, RD->getNumFields());
8081
8082 // Visit the base classes.
8083 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD)) {
8084 for (size_t I = 0, E = CXXRD->getNumBases(); I != E; ++I) {
8085 const CXXBaseSpecifier &BS = CXXRD->bases_begin()[I];
8086 CXXRecordDecl *BaseDecl = BS.getType()->getAsCXXRecordDecl();
8087
8088 std::optional<APValue> SubObj = visitType(
8089 Ty: BS.getType(), Offset: Layout.getBaseClassOffset(Base: BaseDecl) + Offset);
8090 if (!SubObj)
8091 return std::nullopt;
8092 ResultVal.getStructBase(i: I) = *SubObj;
8093 }
8094 }
8095
8096 // Visit the fields.
8097 unsigned FieldIdx = 0;
8098 for (FieldDecl *FD : RD->fields()) {
8099 // FIXME: We don't currently support bit-fields. A lot of the logic for
8100 // this is in CodeGen, so we need to factor it around.
8101 if (FD->isBitField()) {
8102 Info.FFDiag(Loc: BCE->getBeginLoc(),
8103 DiagId: diag::note_constexpr_bit_cast_unsupported_bitfield);
8104 return std::nullopt;
8105 }
8106
8107 uint64_t FieldOffsetBits = Layout.getFieldOffset(FieldNo: FieldIdx);
8108 assert(FieldOffsetBits % Info.Ctx.getCharWidth() == 0);
8109
8110 CharUnits FieldOffset =
8111 CharUnits::fromQuantity(Quantity: FieldOffsetBits / Info.Ctx.getCharWidth()) +
8112 Offset;
8113 QualType FieldTy = FD->getType();
8114 std::optional<APValue> SubObj = visitType(Ty: FieldTy, Offset: FieldOffset);
8115 if (!SubObj)
8116 return std::nullopt;
8117 ResultVal.getStructField(i: FieldIdx) = *SubObj;
8118 ++FieldIdx;
8119 }
8120
8121 return ResultVal;
8122 }
8123
8124 std::optional<APValue> visit(const EnumType *Ty, CharUnits Offset) {
8125 QualType RepresentationType =
8126 Ty->getDecl()->getDefinitionOrSelf()->getIntegerType();
8127 assert(!RepresentationType.isNull() &&
8128 "enum forward decl should be caught by Sema");
8129 const auto *AsBuiltin =
8130 RepresentationType.getCanonicalType()->castAs<BuiltinType>();
8131 // Recurse into the underlying type. Treat std::byte transparently as
8132 // unsigned char.
8133 return visit(T: AsBuiltin, Offset, /*EnumTy=*/EnumSugar: Ty);
8134 }
8135
8136 std::optional<APValue> visit(const ConstantArrayType *Ty, CharUnits Offset) {
8137 size_t Size = Ty->getLimitedSize();
8138 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: Ty->getElementType());
8139
8140 APValue ArrayValue(APValue::UninitArray(), Size, Size);
8141 for (size_t I = 0; I != Size; ++I) {
8142 std::optional<APValue> ElementValue =
8143 visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8144 if (!ElementValue)
8145 return std::nullopt;
8146 ArrayValue.getArrayInitializedElt(I) = std::move(*ElementValue);
8147 }
8148
8149 return ArrayValue;
8150 }
8151
8152 std::optional<APValue> visit(const ComplexType *Ty, CharUnits Offset) {
8153 QualType ElementType = Ty->getElementType();
8154 CharUnits ElementWidth = Info.Ctx.getTypeSizeInChars(T: ElementType);
8155 bool IsInt = ElementType->isIntegerType();
8156
8157 std::optional<APValue> Values[2];
8158 for (unsigned I = 0; I != 2; ++I) {
8159 Values[I] = visitType(Ty: Ty->getElementType(), Offset: Offset + I * ElementWidth);
8160 if (!Values[I])
8161 return std::nullopt;
8162 }
8163
8164 if (IsInt)
8165 return APValue(Values[0]->getInt(), Values[1]->getInt());
8166 return APValue(Values[0]->getFloat(), Values[1]->getFloat());
8167 }
8168
8169 std::optional<APValue> visit(const VectorType *VTy, CharUnits Offset) {
8170 QualType EltTy = VTy->getElementType();
8171 unsigned NElts = VTy->getNumElements();
8172 unsigned EltSize =
8173 VTy->isPackedVectorBoolType(ctx: Info.Ctx) ? 1 : Info.Ctx.getTypeSize(T: EltTy);
8174
8175 SmallVector<APValue, 4> Elts;
8176 Elts.reserve(N: NElts);
8177 if (VTy->isPackedVectorBoolType(ctx: Info.Ctx)) {
8178 // Special handling for OpenCL bool vectors:
8179 // Since these vectors are stored as packed bits, but we can't read
8180 // individual bits from the BitCastBuffer, we'll buffer all of the
8181 // elements together into an appropriately sized APInt and write them all
8182 // out at once. Because we don't accept vectors where NElts * EltSize
8183 // isn't a multiple of the char size, there will be no padding space, so
8184 // we don't have to worry about reading any padding data which didn't
8185 // actually need to be accessed.
8186 bool BigEndian = Info.Ctx.getTargetInfo().isBigEndian();
8187
8188 SmallVector<uint8_t, 8> Bytes;
8189 Bytes.reserve(N: NElts / 8);
8190 if (!Buffer.readObject(Offset, Width: CharUnits::fromQuantity(Quantity: NElts / 8), Output&: Bytes))
8191 return std::nullopt;
8192
8193 APSInt SValInt(NElts, true);
8194 llvm::LoadIntFromMemory(IntVal&: SValInt, Src: &*Bytes.begin(), LoadBytes: Bytes.size());
8195
8196 for (unsigned I = 0; I < NElts; ++I) {
8197 llvm::APInt Elt =
8198 SValInt.extractBits(numBits: 1, bitPosition: (BigEndian ? NElts - I - 1 : I) * EltSize);
8199 Elts.emplace_back(
8200 Args: APSInt(std::move(Elt), !EltTy->isSignedIntegerType()));
8201 }
8202 } else {
8203 // Iterate over each of the elements and read them from the buffer at
8204 // the appropriate offset.
8205 CharUnits EltSizeChars = Info.Ctx.getTypeSizeInChars(T: EltTy);
8206 for (unsigned I = 0; I < NElts; ++I) {
8207 std::optional<APValue> EltValue =
8208 visitType(Ty: EltTy, Offset: Offset + I * EltSizeChars);
8209 if (!EltValue)
8210 return std::nullopt;
8211 Elts.push_back(Elt: std::move(*EltValue));
8212 }
8213 }
8214
8215 return APValue(Elts.data(), Elts.size());
8216 }
8217
8218 std::optional<APValue> visit(const Type *Ty, CharUnits Offset) {
8219 return unsupportedType(Ty: QualType(Ty, 0));
8220 }
8221
8222 std::optional<APValue> visitType(QualType Ty, CharUnits Offset) {
8223 QualType Can = Ty.getCanonicalType();
8224
8225 switch (Can->getTypeClass()) {
8226#define TYPE(Class, Base) \
8227 case Type::Class: \
8228 return visit(cast<Class##Type>(Can.getTypePtr()), Offset);
8229#define ABSTRACT_TYPE(Class, Base)
8230#define NON_CANONICAL_TYPE(Class, Base) \
8231 case Type::Class: \
8232 llvm_unreachable("non-canonical type should be impossible!");
8233#define DEPENDENT_TYPE(Class, Base) \
8234 case Type::Class: \
8235 llvm_unreachable( \
8236 "dependent types aren't supported in the constant evaluator!");
8237#define NON_CANONICAL_UNLESS_DEPENDENT(Class, Base) \
8238 case Type::Class: \
8239 llvm_unreachable("either dependent or not canonical!");
8240#include "clang/AST/TypeNodes.inc"
8241 }
8242 llvm_unreachable("Unhandled Type::TypeClass");
8243 }
8244
8245public:
8246 // Pull out a full value of type DstType.
8247 static std::optional<APValue> convert(EvalInfo &Info, BitCastBuffer &Buffer,
8248 const CastExpr *BCE) {
8249 BufferToAPValueConverter Converter(Info, Buffer, BCE);
8250 return Converter.visitType(Ty: BCE->getType(), Offset: CharUnits::fromQuantity(Quantity: 0));
8251 }
8252};
8253
8254static bool checkBitCastConstexprEligibilityType(SourceLocation Loc,
8255 QualType Ty, EvalInfo *Info,
8256 const ASTContext &Ctx,
8257 bool CheckingDest) {
8258 Ty = Ty.getCanonicalType();
8259
8260 auto diag = [&](int Reason) {
8261 if (Info)
8262 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_type)
8263 << CheckingDest << (Reason == 4) << Reason;
8264 return false;
8265 };
8266 auto note = [&](int Construct, QualType NoteTy, SourceLocation NoteLoc) {
8267 if (Info)
8268 Info->Note(Loc: NoteLoc, DiagId: diag::note_constexpr_bit_cast_invalid_subtype)
8269 << NoteTy << Construct << Ty;
8270 return false;
8271 };
8272
8273 if (Ty->isUnionType())
8274 return diag(0);
8275 if (Ty->isPointerType())
8276 return diag(1);
8277 if (Ty->isMemberPointerType())
8278 return diag(2);
8279 if (Ty.isVolatileQualified())
8280 return diag(3);
8281
8282 if (RecordDecl *Record = Ty->getAsRecordDecl()) {
8283 if (auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: Record)) {
8284 for (CXXBaseSpecifier &BS : CXXRD->bases())
8285 if (!checkBitCastConstexprEligibilityType(Loc, Ty: BS.getType(), Info, Ctx,
8286 CheckingDest))
8287 return note(1, BS.getType(), BS.getBeginLoc());
8288 }
8289 for (FieldDecl *FD : Record->fields()) {
8290 if (FD->getType()->isReferenceType())
8291 return diag(4);
8292 if (!checkBitCastConstexprEligibilityType(Loc, Ty: FD->getType(), Info, Ctx,
8293 CheckingDest))
8294 return note(0, FD->getType(), FD->getBeginLoc());
8295 }
8296 }
8297
8298 if (Ty->isArrayType() &&
8299 !checkBitCastConstexprEligibilityType(Loc, Ty: Ctx.getBaseElementType(QT: Ty),
8300 Info, Ctx, CheckingDest))
8301 return false;
8302
8303 if (const auto *VTy = Ty->getAs<VectorType>()) {
8304 QualType EltTy = VTy->getElementType();
8305 unsigned NElts = VTy->getNumElements();
8306 unsigned EltSize =
8307 VTy->isPackedVectorBoolType(ctx: Ctx) ? 1 : Ctx.getTypeSize(T: EltTy);
8308
8309 if ((NElts * EltSize) % Ctx.getCharWidth() != 0) {
8310 // The vector's size in bits is not a multiple of the target's byte size,
8311 // so its layout is unspecified. For now, we'll simply treat these cases
8312 // as unsupported (this should only be possible with OpenCL bool vectors
8313 // whose element count isn't a multiple of the byte size).
8314 if (Info)
8315 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_invalid_vector)
8316 << QualType(VTy, 0) << EltSize << NElts << Ctx.getCharWidth();
8317 return false;
8318 }
8319
8320 if (EltTy->isRealFloatingType() &&
8321 &Ctx.getFloatTypeSemantics(T: EltTy) == &APFloat::x87DoubleExtended()) {
8322 // The layout for x86_fp80 vectors seems to be handled very inconsistently
8323 // by both clang and LLVM, so for now we won't allow bit_casts involving
8324 // it in a constexpr context.
8325 if (Info)
8326 Info->FFDiag(Loc, DiagId: diag::note_constexpr_bit_cast_unsupported_type)
8327 << EltTy;
8328 return false;
8329 }
8330 }
8331
8332 return true;
8333}
8334
8335static bool checkBitCastConstexprEligibility(EvalInfo *Info,
8336 const ASTContext &Ctx,
8337 const CastExpr *BCE) {
8338 bool DestOK = checkBitCastConstexprEligibilityType(
8339 Loc: BCE->getBeginLoc(), Ty: BCE->getType(), Info, Ctx, CheckingDest: true);
8340 bool SourceOK = DestOK && checkBitCastConstexprEligibilityType(
8341 Loc: BCE->getBeginLoc(),
8342 Ty: BCE->getSubExpr()->getType(), Info, Ctx, CheckingDest: false);
8343 return SourceOK;
8344}
8345
8346static bool handleRValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8347 const APValue &SourceRValue,
8348 const CastExpr *BCE) {
8349 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8350 "no host or target supports non 8-bit chars");
8351
8352 if (!checkBitCastConstexprEligibility(Info: &Info, Ctx: Info.Ctx, BCE))
8353 return false;
8354
8355 // Read out SourceValue into a char buffer.
8356 std::optional<BitCastBuffer> Buffer =
8357 APValueToBufferConverter::convert(Info, Src: SourceRValue, BCE);
8358 if (!Buffer)
8359 return false;
8360
8361 // Write out the buffer into a new APValue.
8362 std::optional<APValue> MaybeDestValue =
8363 BufferToAPValueConverter::convert(Info, Buffer&: *Buffer, BCE);
8364 if (!MaybeDestValue)
8365 return false;
8366
8367 DestValue = std::move(*MaybeDestValue);
8368 return true;
8369}
8370
8371static bool handleLValueToRValueBitCast(EvalInfo &Info, APValue &DestValue,
8372 APValue &SourceValue,
8373 const CastExpr *BCE) {
8374 assert(CHAR_BIT == 8 && Info.Ctx.getTargetInfo().getCharWidth() == 8 &&
8375 "no host or target supports non 8-bit chars");
8376 assert(SourceValue.isLValue() &&
8377 "LValueToRValueBitcast requires an lvalue operand!");
8378
8379 LValue SourceLValue;
8380 APValue SourceRValue;
8381 SourceLValue.setFrom(Ctx: Info.Ctx, V: SourceValue);
8382 if (!handleLValueToRValueConversion(
8383 Info, Conv: BCE, Type: BCE->getSubExpr()->getType().withConst(), LVal: SourceLValue,
8384 RVal&: SourceRValue, /*WantObjectRepresentation=*/true))
8385 return false;
8386
8387 return handleRValueToRValueBitCast(Info, DestValue, SourceRValue, BCE);
8388}
8389
8390template <class Derived>
8391class ExprEvaluatorBase
8392 : public ConstStmtVisitor<Derived, bool> {
8393private:
8394 Derived &getDerived() { return static_cast<Derived&>(*this); }
8395 bool DerivedSuccess(const APValue &V, const Expr *E) {
8396 return getDerived().Success(V, E);
8397 }
8398 bool DerivedZeroInitialization(const Expr *E) {
8399 return getDerived().ZeroInitialization(E);
8400 }
8401
8402 // Check whether a conditional operator with a non-constant condition is a
8403 // potential constant expression. If neither arm is a potential constant
8404 // expression, then the conditional operator is not either.
8405 template<typename ConditionalOperator>
8406 void CheckPotentialConstantConditional(const ConditionalOperator *E) {
8407 assert(Info.checkingPotentialConstantExpression());
8408
8409 // Speculatively evaluate both arms.
8410 SmallVector<PartialDiagnosticAt, 8> Diag;
8411 {
8412 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8413 StmtVisitorTy::Visit(E->getFalseExpr());
8414 if (Diag.empty())
8415 return;
8416 }
8417
8418 {
8419 SpeculativeEvaluationRAII Speculate(Info, &Diag);
8420 Diag.clear();
8421 Info.EvalStatus.DiagEmitted = false;
8422 StmtVisitorTy::Visit(E->getTrueExpr());
8423 if (Diag.empty())
8424 return;
8425 }
8426
8427 Error(E, diag::note_constexpr_conditional_never_const);
8428 }
8429
8430
8431 template<typename ConditionalOperator>
8432 bool HandleConditionalOperator(const ConditionalOperator *E) {
8433 bool BoolResult;
8434 if (!EvaluateAsBooleanCondition(E->getCond(), BoolResult, Info)) {
8435 if (Info.checkingPotentialConstantExpression() && Info.noteFailure()) {
8436 CheckPotentialConstantConditional(E);
8437 return false;
8438 }
8439 if (Info.noteFailure()) {
8440 StmtVisitorTy::Visit(E->getTrueExpr());
8441 StmtVisitorTy::Visit(E->getFalseExpr());
8442 }
8443 return false;
8444 }
8445
8446 Expr *EvalExpr = BoolResult ? E->getTrueExpr() : E->getFalseExpr();
8447 return StmtVisitorTy::Visit(EvalExpr);
8448 }
8449
8450protected:
8451 EvalInfo &Info;
8452 typedef ConstStmtVisitor<Derived, bool> StmtVisitorTy;
8453 typedef ExprEvaluatorBase ExprEvaluatorBaseTy;
8454
8455 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
8456 return Info.CCEDiag(E, DiagId: D);
8457 }
8458
8459 bool ZeroInitialization(const Expr *E) { return Error(E); }
8460
8461 bool IsConstantEvaluatedBuiltinCall(const CallExpr *E) {
8462 unsigned BuiltinOp = E->getBuiltinCallee();
8463 return BuiltinOp != 0 &&
8464 Info.Ctx.BuiltinInfo.isConstantEvaluated(ID: BuiltinOp);
8465 }
8466
8467public:
8468 ExprEvaluatorBase(EvalInfo &Info) : Info(Info) {}
8469
8470 EvalInfo &getEvalInfo() { return Info; }
8471
8472 /// Report an evaluation error. This should only be called when an error is
8473 /// first discovered. When propagating an error, just return false.
8474 bool Error(const Expr *E, diag::kind D) {
8475 Info.FFDiag(E, DiagId: D) << E->getSourceRange();
8476 return false;
8477 }
8478 bool Error(const Expr *E) {
8479 return Error(E, diag::note_invalid_subexpr_in_const_expr);
8480 }
8481
8482 bool VisitStmt(const Stmt *) {
8483 llvm_unreachable("Expression evaluator should not be called on stmts");
8484 }
8485 bool VisitExpr(const Expr *E) {
8486 return Error(E);
8487 }
8488
8489 bool VisitEmbedExpr(const EmbedExpr *E) {
8490 const auto It = E->begin();
8491 return StmtVisitorTy::Visit(*It);
8492 }
8493
8494 bool VisitPredefinedExpr(const PredefinedExpr *E) {
8495 return StmtVisitorTy::Visit(E->getFunctionName());
8496 }
8497 bool VisitConstantExpr(const ConstantExpr *E) {
8498 if (E->hasAPValueResult())
8499 return DerivedSuccess(V: E->getAPValueResult(), E);
8500
8501 return StmtVisitorTy::Visit(E->getSubExpr());
8502 }
8503
8504 bool VisitParenExpr(const ParenExpr *E)
8505 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8506 bool VisitUnaryExtension(const UnaryOperator *E)
8507 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8508 bool VisitUnaryPlus(const UnaryOperator *E)
8509 { return StmtVisitorTy::Visit(E->getSubExpr()); }
8510 bool VisitChooseExpr(const ChooseExpr *E)
8511 { return StmtVisitorTy::Visit(E->getChosenSubExpr()); }
8512 bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
8513 { return StmtVisitorTy::Visit(E->getResultExpr()); }
8514 bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
8515 { return StmtVisitorTy::Visit(E->getReplacement()); }
8516 bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
8517 TempVersionRAII RAII(*Info.CurrentCall);
8518 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8519 return StmtVisitorTy::Visit(E->getExpr());
8520 }
8521 bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
8522 TempVersionRAII RAII(*Info.CurrentCall);
8523 // The initializer may not have been parsed yet, or might be erroneous.
8524 if (!E->getExpr())
8525 return Error(E);
8526 SourceLocExprScopeGuard Guard(E, Info.CurrentCall->CurSourceLocExprScope);
8527 return StmtVisitorTy::Visit(E->getExpr());
8528 }
8529
8530 bool VisitExprWithCleanups(const ExprWithCleanups *E) {
8531 FullExpressionRAII Scope(Info);
8532 return StmtVisitorTy::Visit(E->getSubExpr()) && Scope.destroy();
8533 }
8534
8535 // Temporaries are registered when created, so we don't care about
8536 // CXXBindTemporaryExpr.
8537 bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E) {
8538 return StmtVisitorTy::Visit(E->getSubExpr());
8539 }
8540
8541 bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E) {
8542 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8543 << diag::ConstexprInvalidCastKind::Reinterpret;
8544 return static_cast<Derived*>(this)->VisitCastExpr(E);
8545 }
8546 bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E) {
8547 if (!Info.Ctx.getLangOpts().CPlusPlus20)
8548 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
8549 << diag::ConstexprInvalidCastKind::Dynamic;
8550 return static_cast<Derived*>(this)->VisitCastExpr(E);
8551 }
8552 bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E) {
8553 return static_cast<Derived*>(this)->VisitCastExpr(E);
8554 }
8555
8556 bool VisitBinaryOperator(const BinaryOperator *E) {
8557 switch (E->getOpcode()) {
8558 default:
8559 return Error(E);
8560
8561 case BO_Comma:
8562 VisitIgnoredValue(E: E->getLHS());
8563 return StmtVisitorTy::Visit(E->getRHS());
8564
8565 case BO_PtrMemD:
8566 case BO_PtrMemI: {
8567 LValue Obj;
8568 if (!HandleMemberPointerAccess(Info, BO: E, LV&: Obj))
8569 return false;
8570 APValue Result;
8571 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: Obj, RVal&: Result))
8572 return false;
8573 return DerivedSuccess(V: Result, E);
8574 }
8575 }
8576 }
8577
8578 bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E) {
8579 return StmtVisitorTy::Visit(E->getSemanticForm());
8580 }
8581
8582 bool VisitBinaryConditionalOperator(const BinaryConditionalOperator *E) {
8583 // Evaluate and cache the common expression. We treat it as a temporary,
8584 // even though it's not quite the same thing.
8585 LValue CommonLV;
8586 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8587 Key: E->getOpaqueValue(),
8588 T: getStorageType(Ctx: Info.Ctx, E: E->getOpaqueValue()),
8589 Scope: ScopeKind::FullExpression, LV&: CommonLV),
8590 Info, E: E->getCommon()))
8591 return false;
8592
8593 return HandleConditionalOperator(E);
8594 }
8595
8596 bool VisitConditionalOperator(const ConditionalOperator *E) {
8597 bool IsBcpCall = false;
8598 // If the condition (ignoring parens) is a __builtin_constant_p call,
8599 // the result is a constant expression if it can be folded without
8600 // side-effects. This is an important GNU extension. See GCC PR38377
8601 // for discussion.
8602 if (const CallExpr *CallCE =
8603 dyn_cast<CallExpr>(Val: E->getCond()->IgnoreParenCasts()))
8604 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
8605 IsBcpCall = true;
8606
8607 // Always assume __builtin_constant_p(...) ? ... : ... is a potential
8608 // constant expression; we can't check whether it's potentially foldable.
8609 // FIXME: We should instead treat __builtin_constant_p as non-constant if
8610 // it would return 'false' in this mode.
8611 if (Info.checkingPotentialConstantExpression() && IsBcpCall)
8612 return false;
8613
8614 FoldConstant Fold(Info, IsBcpCall);
8615 if (!HandleConditionalOperator(E)) {
8616 Fold.keepDiagnostics();
8617 return false;
8618 }
8619
8620 return true;
8621 }
8622
8623 bool VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
8624 if (APValue *Value = Info.CurrentCall->getCurrentTemporary(Key: E);
8625 Value && !Value->isAbsent())
8626 return DerivedSuccess(V: *Value, E);
8627
8628 const Expr *Source = E->getSourceExpr();
8629 if (!Source)
8630 return Error(E);
8631 if (Source == E) {
8632 assert(0 && "OpaqueValueExpr recursively refers to itself");
8633 return Error(E);
8634 }
8635 return StmtVisitorTy::Visit(Source);
8636 }
8637
8638 bool VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
8639 for (const Expr *SemE : E->semantics()) {
8640 if (auto *OVE = dyn_cast<OpaqueValueExpr>(Val: SemE)) {
8641 // FIXME: We can't handle the case where an OpaqueValueExpr is also the
8642 // result expression: there could be two different LValues that would
8643 // refer to the same object in that case, and we can't model that.
8644 if (SemE == E->getResultExpr())
8645 return Error(E);
8646
8647 // Unique OVEs get evaluated if and when we encounter them when
8648 // emitting the rest of the semantic form, rather than eagerly.
8649 if (OVE->isUnique())
8650 continue;
8651
8652 LValue LV;
8653 if (!Evaluate(Result&: Info.CurrentCall->createTemporary(
8654 Key: OVE, T: getStorageType(Ctx: Info.Ctx, E: OVE),
8655 Scope: ScopeKind::FullExpression, LV),
8656 Info, E: OVE->getSourceExpr()))
8657 return false;
8658 } else if (SemE == E->getResultExpr()) {
8659 if (!StmtVisitorTy::Visit(SemE))
8660 return false;
8661 } else {
8662 if (!EvaluateIgnoredValue(Info, E: SemE))
8663 return false;
8664 }
8665 }
8666 return true;
8667 }
8668
8669 bool VisitCallExpr(const CallExpr *E) {
8670 APValue Result;
8671 if (!handleCallExpr(E, Result, ResultSlot: nullptr))
8672 return false;
8673 return DerivedSuccess(V: Result, E);
8674 }
8675
8676 bool handleCallExpr(const CallExpr *E, APValue &Result,
8677 const LValue *ResultSlot) {
8678 CallScopeRAII CallScope(Info);
8679
8680 const Expr *Callee = E->getCallee()->IgnoreParens();
8681 QualType CalleeType = Callee->getType();
8682
8683 const FunctionDecl *FD = nullptr;
8684 LValue *This = nullptr, ObjectArg;
8685 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
8686 bool HasQualifier = false;
8687
8688 CallRef Call;
8689
8690 // Extract function decl and 'this' pointer from the callee.
8691 if (CalleeType->isSpecificBuiltinType(K: BuiltinType::BoundMember)) {
8692 const CXXMethodDecl *Member = nullptr;
8693 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: Callee)) {
8694 // Explicit bound member calls, such as x.f() or p->g();
8695 if (!EvaluateObjectArgument(Info, Object: ME->getBase(), This&: ObjectArg))
8696 return false;
8697 Member = dyn_cast<CXXMethodDecl>(Val: ME->getMemberDecl());
8698 if (!Member)
8699 return Error(Callee);
8700 This = &ObjectArg;
8701 HasQualifier = ME->hasQualifier();
8702 } else if (const BinaryOperator *BE = dyn_cast<BinaryOperator>(Val: Callee)) {
8703 // Indirect bound member calls ('.*' or '->*').
8704 const ValueDecl *D =
8705 HandleMemberPointerAccess(Info, BO: BE, LV&: ObjectArg, IncludeMember: false);
8706 if (!D)
8707 return false;
8708 Member = dyn_cast<CXXMethodDecl>(Val: D);
8709 if (!Member)
8710 return Error(Callee);
8711 This = &ObjectArg;
8712 } else if (const auto *PDE = dyn_cast<CXXPseudoDestructorExpr>(Val: Callee)) {
8713 if (!Info.getLangOpts().CPlusPlus20)
8714 Info.CCEDiag(E: PDE, DiagId: diag::note_constexpr_pseudo_destructor);
8715 return EvaluateObjectArgument(Info, Object: PDE->getBase(), This&: ObjectArg) &&
8716 HandleDestruction(Info, E: PDE, This: ObjectArg, ThisType: PDE->getDestroyedType());
8717 } else
8718 return Error(Callee);
8719 FD = Member;
8720 } else if (CalleeType->isFunctionPointerType()) {
8721 LValue CalleeLV;
8722 if (!EvaluatePointer(E: Callee, Result&: CalleeLV, Info))
8723 return false;
8724
8725 if (!CalleeLV.getLValueOffset().isZero())
8726 return Error(Callee);
8727 if (CalleeLV.isNullPointer()) {
8728 Info.FFDiag(E: Callee, DiagId: diag::note_constexpr_null_callee)
8729 << const_cast<Expr *>(Callee);
8730 return false;
8731 }
8732 FD = dyn_cast_or_null<FunctionDecl>(
8733 Val: CalleeLV.getLValueBase().dyn_cast<const ValueDecl *>());
8734 if (!FD)
8735 return Error(Callee);
8736 // Don't call function pointers which have been cast to some other type.
8737 // Per DR (no number yet), the caller and callee can differ in noexcept.
8738 if (!Info.Ctx.hasSameFunctionTypeIgnoringExceptionSpec(
8739 T: CalleeType->getPointeeType(), U: FD->getType())) {
8740 return Error(E);
8741 }
8742
8743 // For an (overloaded) assignment expression, evaluate the RHS before the
8744 // LHS.
8745 auto *OCE = dyn_cast<CXXOperatorCallExpr>(Val: E);
8746 if (OCE && OCE->isAssignmentOp()) {
8747 assert(Args.size() == 2 && "wrong number of arguments in assignment");
8748 Call = Info.CurrentCall->createCall(Callee: FD);
8749 bool HasThis = false;
8750 if (const auto *MD = dyn_cast<CXXMethodDecl>(Val: FD))
8751 HasThis = MD->isImplicitObjectMemberFunction();
8752 if (!EvaluateArgs(Args: HasThis ? Args.slice(N: 1) : Args, Call, Info, Callee: FD,
8753 /*RightToLeft=*/true, ObjectArg: &ObjectArg))
8754 return false;
8755 }
8756
8757 // Overloaded operator calls to member functions are represented as normal
8758 // calls with '*this' as the first argument.
8759 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
8760 if (MD &&
8761 (MD->isImplicitObjectMemberFunction() || (OCE && MD->isStatic()))) {
8762 // FIXME: When selecting an implicit conversion for an overloaded
8763 // operator delete, we sometimes try to evaluate calls to conversion
8764 // operators without a 'this' parameter!
8765 if (Args.empty())
8766 return Error(E);
8767
8768 if (!EvaluateObjectArgument(Info, Object: Args[0], This&: ObjectArg))
8769 return false;
8770
8771 // If we are calling a static operator, the 'this' argument needs to be
8772 // ignored after being evaluated.
8773 if (MD->isInstance())
8774 This = &ObjectArg;
8775
8776 // If this is syntactically a simple assignment using a trivial
8777 // assignment operator, start the lifetimes of union members as needed,
8778 // per C++20 [class.union]5.
8779 if (Info.getLangOpts().CPlusPlus20 && OCE &&
8780 OCE->getOperator() == OO_Equal && MD->isTrivial() &&
8781 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: Args[0], LHS: ObjectArg))
8782 return false;
8783
8784 Args = Args.slice(N: 1);
8785 } else if (MD && MD->isLambdaStaticInvoker()) {
8786 // Map the static invoker for the lambda back to the call operator.
8787 // Conveniently, we don't have to slice out the 'this' argument (as is
8788 // being done for the non-static case), since a static member function
8789 // doesn't have an implicit argument passed in.
8790 const CXXRecordDecl *ClosureClass = MD->getParent();
8791 assert(
8792 ClosureClass->captures().empty() &&
8793 "Number of captures must be zero for conversion to function-ptr");
8794
8795 const CXXMethodDecl *LambdaCallOp =
8796 ClosureClass->getLambdaCallOperator();
8797
8798 // Set 'FD', the function that will be called below, to the call
8799 // operator. If the closure object represents a generic lambda, find
8800 // the corresponding specialization of the call operator.
8801
8802 if (ClosureClass->isGenericLambda()) {
8803 assert(MD->isFunctionTemplateSpecialization() &&
8804 "A generic lambda's static-invoker function must be a "
8805 "template specialization");
8806 const TemplateArgumentList *TAL = MD->getTemplateSpecializationArgs();
8807 FunctionTemplateDecl *CallOpTemplate =
8808 LambdaCallOp->getDescribedFunctionTemplate();
8809 void *InsertPos = nullptr;
8810 FunctionDecl *CorrespondingCallOpSpecialization =
8811 CallOpTemplate->findSpecialization(Args: TAL->asArray(), InsertPos);
8812 assert(CorrespondingCallOpSpecialization &&
8813 "We must always have a function call operator specialization "
8814 "that corresponds to our static invoker specialization");
8815 assert(isa<CXXMethodDecl>(CorrespondingCallOpSpecialization));
8816 FD = CorrespondingCallOpSpecialization;
8817 } else
8818 FD = LambdaCallOp;
8819 } else if (FD->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
8820 if (FD->getDeclName().isAnyOperatorNew()) {
8821 LValue Ptr;
8822 if (!HandleOperatorNewCall(Info, E, Result&: Ptr))
8823 return false;
8824 Ptr.moveInto(V&: Result);
8825 return CallScope.destroy();
8826 } else {
8827 return HandleOperatorDeleteCall(Info, E) && CallScope.destroy();
8828 }
8829 }
8830 } else
8831 return Error(E);
8832
8833 // Evaluate the arguments now if we've not already done so.
8834 if (!Call) {
8835 Call = Info.CurrentCall->createCall(Callee: FD);
8836 if (!EvaluateArgs(Args, Call, Info, Callee: FD, /*RightToLeft*/ false,
8837 ObjectArg: &ObjectArg))
8838 return false;
8839 }
8840
8841 SmallVector<QualType, 4> CovariantAdjustmentPath;
8842 if (This) {
8843 auto *NamedMember = dyn_cast<CXXMethodDecl>(Val: FD);
8844 if (NamedMember && NamedMember->isVirtual() && !HasQualifier) {
8845 // Perform virtual dispatch, if necessary.
8846 FD = HandleVirtualDispatch(Info, E, This&: *This, Found: NamedMember,
8847 CovariantAdjustmentPath);
8848 if (!FD)
8849 return false;
8850 } else if (NamedMember && NamedMember->isImplicitObjectMemberFunction()) {
8851 // Check that the 'this' pointer points to an object of the right type.
8852 // FIXME: If this is an assignment operator call, we may need to change
8853 // the active union member before we check this.
8854 if (!checkNonVirtualMemberCallThisPointer(Info, E, This: *This, NamedMember))
8855 return false;
8856 }
8857 }
8858
8859 // Destructor calls are different enough that they have their own codepath.
8860 if (auto *DD = dyn_cast<CXXDestructorDecl>(Val: FD)) {
8861 assert(This && "no 'this' pointer for destructor call");
8862 return HandleDestruction(Info, E, This: *This,
8863 ThisType: Info.Ctx.getCanonicalTagType(TD: DD->getParent())) &&
8864 CallScope.destroy();
8865 }
8866
8867 const FunctionDecl *Definition = nullptr;
8868 Stmt *Body = FD->getBody(Definition);
8869 SourceLocation Loc = E->getExprLoc();
8870
8871 // Treat the object argument as `this` when evaluating defaulted
8872 // special menmber functions
8873 if (FD->hasCXXExplicitFunctionObjectParameter())
8874 This = &ObjectArg;
8875
8876 if (!CheckConstexprFunction(Info, CallLoc: Loc, Declaration: FD, Definition, Body) ||
8877 !HandleFunctionCall(CallLoc: Loc, Callee: Definition, ObjectArg: This, E, Args, Call, Body, Info,
8878 Result, ResultSlot))
8879 return false;
8880
8881 if (!CovariantAdjustmentPath.empty() &&
8882 !HandleCovariantReturnAdjustment(Info, E, Result,
8883 Path: CovariantAdjustmentPath))
8884 return false;
8885
8886 return CallScope.destroy();
8887 }
8888
8889 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
8890 return StmtVisitorTy::Visit(E->getInitializer());
8891 }
8892 bool VisitInitListExpr(const InitListExpr *E) {
8893 if (E->getNumInits() == 0)
8894 return DerivedZeroInitialization(E);
8895 if (E->getNumInits() == 1)
8896 return StmtVisitorTy::Visit(E->getInit(Init: 0));
8897 return Error(E);
8898 }
8899 bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
8900 return DerivedZeroInitialization(E);
8901 }
8902 bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E) {
8903 return DerivedZeroInitialization(E);
8904 }
8905 bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E) {
8906 return DerivedZeroInitialization(E);
8907 }
8908
8909 /// A member expression where the object is a prvalue is itself a prvalue.
8910 bool VisitMemberExpr(const MemberExpr *E) {
8911 assert(!Info.Ctx.getLangOpts().CPlusPlus11 &&
8912 "missing temporary materialization conversion");
8913 assert(!E->isArrow() && "missing call to bound member function?");
8914
8915 APValue Val;
8916 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
8917 return false;
8918
8919 QualType BaseTy = E->getBase()->getType();
8920
8921 const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl());
8922 if (!FD) return Error(E);
8923 assert(!FD->getType()->isReferenceType() && "prvalue reference?");
8924 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
8925 FD->getParent()->getCanonicalDecl() &&
8926 "record / field mismatch");
8927
8928 // Note: there is no lvalue base here. But this case should only ever
8929 // happen in C or in C++98, where we cannot be evaluating a constexpr
8930 // constructor, which is the only case the base matters.
8931 CompleteObject Obj(APValue::LValueBase(), &Val, BaseTy);
8932 SubobjectDesignator Designator(BaseTy);
8933 Designator.addDeclUnchecked(D: FD);
8934
8935 APValue Result;
8936 return extractSubobject(Info, E, Obj, Sub: Designator, Result) &&
8937 DerivedSuccess(V: Result, E);
8938 }
8939
8940 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E) {
8941 APValue Val;
8942 if (!Evaluate(Result&: Val, Info, E: E->getBase()))
8943 return false;
8944
8945 if (Val.isVector()) {
8946 SmallVector<uint32_t, 4> Indices;
8947 E->getEncodedElementAccess(Elts&: Indices);
8948 if (Indices.size() == 1) {
8949 // Return scalar.
8950 return DerivedSuccess(V: Val.getVectorElt(I: Indices[0]), E);
8951 } else {
8952 // Construct new APValue vector.
8953 SmallVector<APValue, 4> Elts;
8954 for (unsigned I = 0; I < Indices.size(); ++I) {
8955 Elts.push_back(Elt: Val.getVectorElt(I: Indices[I]));
8956 }
8957 APValue VecResult(Elts.data(), Indices.size());
8958 return DerivedSuccess(V: VecResult, E);
8959 }
8960 }
8961
8962 return false;
8963 }
8964
8965 bool VisitCastExpr(const CastExpr *E) {
8966 switch (E->getCastKind()) {
8967 default:
8968 break;
8969
8970 case CK_AtomicToNonAtomic: {
8971 APValue AtomicVal;
8972 // This does not need to be done in place even for class/array types:
8973 // atomic-to-non-atomic conversion implies copying the object
8974 // representation.
8975 if (!Evaluate(Result&: AtomicVal, Info, E: E->getSubExpr()))
8976 return false;
8977 return DerivedSuccess(V: AtomicVal, E);
8978 }
8979
8980 case CK_NoOp:
8981 case CK_UserDefinedConversion:
8982 return StmtVisitorTy::Visit(E->getSubExpr());
8983
8984 case CK_HLSLArrayRValue: {
8985 const Expr *SubExpr = E->getSubExpr();
8986 if (!SubExpr->isGLValue()) {
8987 APValue Val;
8988 if (!Evaluate(Result&: Val, Info, E: SubExpr))
8989 return false;
8990 return DerivedSuccess(V: Val, E);
8991 }
8992
8993 LValue LVal;
8994 if (!EvaluateLValue(E: SubExpr, Result&: LVal, Info))
8995 return false;
8996 APValue RVal;
8997 // Note, we use the subexpression's type in order to retain cv-qualifiers.
8998 if (!handleLValueToRValueConversion(Info, Conv: E, Type: SubExpr->getType(), LVal,
8999 RVal))
9000 return false;
9001 return DerivedSuccess(V: RVal, E);
9002 }
9003 case CK_LValueToRValue: {
9004 LValue LVal;
9005 if (!EvaluateLValue(E: E->getSubExpr(), Result&: LVal, Info))
9006 return false;
9007 APValue RVal;
9008 // Note, we use the subexpression's type in order to retain cv-qualifiers.
9009 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
9010 LVal, RVal))
9011 return false;
9012 return DerivedSuccess(V: RVal, E);
9013 }
9014 case CK_LValueToRValueBitCast: {
9015 APValue DestValue, SourceValue;
9016 if (!Evaluate(Result&: SourceValue, Info, E: E->getSubExpr()))
9017 return false;
9018 if (!handleLValueToRValueBitCast(Info, DestValue, SourceValue, BCE: E))
9019 return false;
9020 return DerivedSuccess(V: DestValue, E);
9021 }
9022
9023 case CK_AddressSpaceConversion: {
9024 APValue Value;
9025 if (!Evaluate(Result&: Value, Info, E: E->getSubExpr()))
9026 return false;
9027 return DerivedSuccess(V: Value, E);
9028 }
9029 }
9030
9031 return Error(E);
9032 }
9033
9034 bool VisitUnaryPostInc(const UnaryOperator *UO) {
9035 return VisitUnaryPostIncDec(UO);
9036 }
9037 bool VisitUnaryPostDec(const UnaryOperator *UO) {
9038 return VisitUnaryPostIncDec(UO);
9039 }
9040 bool VisitUnaryPostIncDec(const UnaryOperator *UO) {
9041 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9042 return Error(UO);
9043
9044 LValue LVal;
9045 if (!EvaluateLValue(E: UO->getSubExpr(), Result&: LVal, Info))
9046 return false;
9047 APValue RVal;
9048 if (!handleIncDec(Info&: this->Info, E: UO, LVal, LValType: UO->getSubExpr()->getType(),
9049 IsIncrement: UO->isIncrementOp(), Old: &RVal))
9050 return false;
9051 return DerivedSuccess(V: RVal, E: UO);
9052 }
9053
9054 bool VisitStmtExpr(const StmtExpr *E) {
9055 // We will have checked the full-expressions inside the statement expression
9056 // when they were completed, and don't need to check them again now.
9057 llvm::SaveAndRestore NotCheckingForUB(Info.CheckingForUndefinedBehavior,
9058 false);
9059
9060 const CompoundStmt *CS = E->getSubStmt();
9061 if (CS->body_empty())
9062 return true;
9063
9064 BlockScopeRAII Scope(Info);
9065 for (CompoundStmt::const_body_iterator BI = CS->body_begin(),
9066 BE = CS->body_end();
9067 /**/; ++BI) {
9068 if (BI + 1 == BE) {
9069 const Expr *FinalExpr = dyn_cast<Expr>(Val: *BI);
9070 if (!FinalExpr) {
9071 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9072 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9073 return false;
9074 }
9075 return this->Visit(FinalExpr) && Scope.destroy();
9076 }
9077
9078 APValue ReturnValue;
9079 StmtResult Result = { .Value: ReturnValue, .Slot: nullptr };
9080 EvalStmtResult ESR = EvaluateStmt(Result, Info, S: *BI);
9081 if (ESR != ESR_Succeeded) {
9082 // FIXME: If the statement-expression terminated due to 'return',
9083 // 'break', or 'continue', it would be nice to propagate that to
9084 // the outer statement evaluation rather than bailing out.
9085 if (ESR != ESR_Failed)
9086 Info.FFDiag(Loc: (*BI)->getBeginLoc(),
9087 DiagId: diag::note_constexpr_stmt_expr_unsupported);
9088 return false;
9089 }
9090 }
9091
9092 llvm_unreachable("Return from function from the loop above.");
9093 }
9094
9095 bool VisitPackIndexingExpr(const PackIndexingExpr *E) {
9096 return StmtVisitorTy::Visit(E->getSelectedExpr());
9097 }
9098
9099 /// Visit a value which is evaluated, but whose value is ignored.
9100 void VisitIgnoredValue(const Expr *E) {
9101 EvaluateIgnoredValue(Info, E);
9102 }
9103
9104 /// Potentially visit a MemberExpr's base expression.
9105 void VisitIgnoredBaseExpression(const Expr *E) {
9106 // While MSVC doesn't evaluate the base expression, it does diagnose the
9107 // presence of side-effecting behavior.
9108 if (Info.getLangOpts().MSVCCompat && !E->HasSideEffects(Ctx: Info.Ctx))
9109 return;
9110 VisitIgnoredValue(E);
9111 }
9112};
9113
9114} // namespace
9115
9116//===----------------------------------------------------------------------===//
9117// Common base class for lvalue and temporary evaluation.
9118//===----------------------------------------------------------------------===//
9119namespace {
9120template<class Derived>
9121class LValueExprEvaluatorBase
9122 : public ExprEvaluatorBase<Derived> {
9123protected:
9124 LValue &Result;
9125 bool InvalidBaseOK;
9126 typedef LValueExprEvaluatorBase LValueExprEvaluatorBaseTy;
9127 typedef ExprEvaluatorBase<Derived> ExprEvaluatorBaseTy;
9128
9129 bool Success(APValue::LValueBase B) {
9130 Result.set(B);
9131 return true;
9132 }
9133
9134 bool evaluatePointer(const Expr *E, LValue &Result) {
9135 return EvaluatePointer(E, Result, this->Info, InvalidBaseOK);
9136 }
9137
9138public:
9139 LValueExprEvaluatorBase(EvalInfo &Info, LValue &Result, bool InvalidBaseOK)
9140 : ExprEvaluatorBaseTy(Info), Result(Result),
9141 InvalidBaseOK(InvalidBaseOK) {}
9142
9143 bool Success(const APValue &V, const Expr *E) {
9144 Result.setFrom(Ctx: this->Info.Ctx, V);
9145 return true;
9146 }
9147
9148 bool VisitMemberExpr(const MemberExpr *E) {
9149 // Handle non-static data members.
9150 QualType BaseTy;
9151 bool EvalOK;
9152 if (E->isArrow()) {
9153 EvalOK = evaluatePointer(E: E->getBase(), Result);
9154 BaseTy = E->getBase()->getType()->castAs<PointerType>()->getPointeeType();
9155 } else if (E->getBase()->isPRValue()) {
9156 assert(E->getBase()->getType()->isRecordType());
9157 EvalOK = EvaluateTemporary(E->getBase(), Result, this->Info);
9158 BaseTy = E->getBase()->getType();
9159 } else {
9160 EvalOK = this->Visit(E->getBase());
9161 BaseTy = E->getBase()->getType();
9162 }
9163 if (!EvalOK) {
9164 if (!InvalidBaseOK)
9165 return false;
9166 Result.setInvalid(B: E);
9167 return true;
9168 }
9169
9170 const ValueDecl *MD = E->getMemberDecl();
9171 if (const FieldDecl *FD = dyn_cast<FieldDecl>(Val: E->getMemberDecl())) {
9172 assert(BaseTy->castAsCanonical<RecordType>()->getDecl() ==
9173 FD->getParent()->getCanonicalDecl() &&
9174 "record / field mismatch");
9175 (void)BaseTy;
9176 if (!HandleLValueMember(this->Info, E, Result, FD))
9177 return false;
9178 } else if (const IndirectFieldDecl *IFD = dyn_cast<IndirectFieldDecl>(Val: MD)) {
9179 if (!HandleLValueIndirectMember(this->Info, E, Result, IFD))
9180 return false;
9181 } else
9182 return this->Error(E);
9183
9184 if (MD->getType()->isReferenceType()) {
9185 APValue RefValue;
9186 if (!handleLValueToRValueConversion(this->Info, E, MD->getType(), Result,
9187 RefValue))
9188 return false;
9189 return Success(RefValue, E);
9190 }
9191 return true;
9192 }
9193
9194 bool VisitBinaryOperator(const BinaryOperator *E) {
9195 switch (E->getOpcode()) {
9196 default:
9197 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
9198
9199 case BO_PtrMemD:
9200 case BO_PtrMemI:
9201 return HandleMemberPointerAccess(this->Info, E, Result);
9202 }
9203 }
9204
9205 bool VisitCastExpr(const CastExpr *E) {
9206 switch (E->getCastKind()) {
9207 default:
9208 return ExprEvaluatorBaseTy::VisitCastExpr(E);
9209
9210 case CK_DerivedToBase:
9211 case CK_UncheckedDerivedToBase:
9212 if (!this->Visit(E->getSubExpr()))
9213 return false;
9214
9215 // Now figure out the necessary offset to add to the base LV to get from
9216 // the derived class to the base class.
9217 return HandleLValueBasePath(this->Info, E, E->getSubExpr()->getType(),
9218 Result);
9219 }
9220 }
9221};
9222}
9223
9224//===----------------------------------------------------------------------===//
9225// LValue Evaluation
9226//
9227// This is used for evaluating lvalues (in C and C++), xvalues (in C++11),
9228// function designators (in C), decl references to void objects (in C), and
9229// temporaries (if building with -Wno-address-of-temporary).
9230//
9231// LValue evaluation produces values comprising a base expression of one of the
9232// following types:
9233// - Declarations
9234// * VarDecl
9235// * FunctionDecl
9236// - Literals
9237// * CompoundLiteralExpr in C (and in global scope in C++)
9238// * StringLiteral
9239// * PredefinedExpr
9240// * ObjCStringLiteralExpr
9241// * ObjCEncodeExpr
9242// * AddrLabelExpr
9243// * BlockExpr
9244// * CallExpr for a MakeStringConstant builtin
9245// - typeid(T) expressions, as TypeInfoLValues
9246// - Locals and temporaries
9247// * MaterializeTemporaryExpr
9248// * Any Expr, with a CallIndex indicating the function in which the temporary
9249// was evaluated, for cases where the MaterializeTemporaryExpr is missing
9250// from the AST (FIXME).
9251// * A MaterializeTemporaryExpr that has static storage duration, with no
9252// CallIndex, for a lifetime-extended temporary.
9253// * The ConstantExpr that is currently being evaluated during evaluation of an
9254// immediate invocation.
9255// plus an offset in bytes.
9256//===----------------------------------------------------------------------===//
9257namespace {
9258class LValueExprEvaluator
9259 : public LValueExprEvaluatorBase<LValueExprEvaluator> {
9260public:
9261 LValueExprEvaluator(EvalInfo &Info, LValue &Result, bool InvalidBaseOK) :
9262 LValueExprEvaluatorBaseTy(Info, Result, InvalidBaseOK) {}
9263
9264 bool VisitVarDecl(const Expr *E, const VarDecl *VD);
9265 bool VisitUnaryPreIncDec(const UnaryOperator *UO);
9266
9267 bool VisitCallExpr(const CallExpr *E);
9268 bool VisitDeclRefExpr(const DeclRefExpr *E);
9269 bool VisitPredefinedExpr(const PredefinedExpr *E) { return Success(B: E); }
9270 bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E);
9271 bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E);
9272 bool VisitMemberExpr(const MemberExpr *E);
9273 bool VisitStringLiteral(const StringLiteral *E) {
9274 return Success(
9275 B: APValue::LValueBase(E, 0, Info.Ctx.getNextStringLiteralVersion()));
9276 }
9277 bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E) { return Success(B: E); }
9278 bool VisitCXXTypeidExpr(const CXXTypeidExpr *E);
9279 bool VisitCXXUuidofExpr(const CXXUuidofExpr *E);
9280 bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E);
9281 bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E);
9282 bool VisitUnaryDeref(const UnaryOperator *E);
9283 bool VisitUnaryReal(const UnaryOperator *E);
9284 bool VisitUnaryImag(const UnaryOperator *E);
9285 bool VisitUnaryPreInc(const UnaryOperator *UO) {
9286 return VisitUnaryPreIncDec(UO);
9287 }
9288 bool VisitUnaryPreDec(const UnaryOperator *UO) {
9289 return VisitUnaryPreIncDec(UO);
9290 }
9291 bool VisitBinAssign(const BinaryOperator *BO);
9292 bool VisitCompoundAssignOperator(const CompoundAssignOperator *CAO);
9293
9294 bool VisitCastExpr(const CastExpr *E) {
9295 switch (E->getCastKind()) {
9296 default:
9297 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
9298
9299 case CK_LValueBitCast:
9300 this->CCEDiag(E, D: diag::note_constexpr_invalid_cast)
9301 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
9302 << Info.Ctx.getLangOpts().CPlusPlus;
9303 if (!Visit(S: E->getSubExpr()))
9304 return false;
9305 Result.Designator.setInvalid();
9306 return true;
9307
9308 case CK_BaseToDerived:
9309 if (!Visit(S: E->getSubExpr()))
9310 return false;
9311 return HandleBaseToDerivedCast(Info, E, Result);
9312
9313 case CK_Dynamic:
9314 if (!Visit(S: E->getSubExpr()))
9315 return false;
9316 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
9317 }
9318 }
9319};
9320} // end anonymous namespace
9321
9322/// Get an lvalue to a field of a lambda's closure type.
9323static bool HandleLambdaCapture(EvalInfo &Info, const Expr *E, LValue &Result,
9324 const CXXMethodDecl *MD, const FieldDecl *FD,
9325 bool LValueToRValueConversion) {
9326 // Static lambda function call operators can't have captures. We already
9327 // diagnosed this, so bail out here.
9328 if (MD->isStatic()) {
9329 assert(Info.CurrentCall->This == nullptr &&
9330 "This should not be set for a static call operator");
9331 return false;
9332 }
9333
9334 // Start with 'Result' referring to the complete closure object...
9335 if (MD->isExplicitObjectMemberFunction()) {
9336 // Self may be passed by reference or by value.
9337 const ParmVarDecl *Self = MD->getParamDecl(i: 0);
9338 if (Self->getType()->isReferenceType()) {
9339 APValue *RefValue = Info.getParamSlot(Call: Info.CurrentCall->Arguments, PVD: Self);
9340 if (!RefValue->allowConstexprUnknown() || RefValue->hasValue())
9341 Result.setFrom(Ctx: Info.Ctx, V: *RefValue);
9342 } else {
9343 const ParmVarDecl *VD = Info.CurrentCall->Arguments.getOrigParam(PVD: Self);
9344 CallStackFrame *Frame =
9345 Info.getCallFrameAndDepth(CallIndex: Info.CurrentCall->Arguments.CallIndex)
9346 .first;
9347 unsigned Version = Info.CurrentCall->Arguments.Version;
9348 Result.set(B: {VD, Frame->Index, Version});
9349 }
9350 } else
9351 Result = *Info.CurrentCall->This;
9352
9353 // ... then update it to refer to the field of the closure object
9354 // that represents the capture.
9355 if (!HandleLValueMember(Info, E, LVal&: Result, FD))
9356 return false;
9357
9358 // And if the field is of reference type (or if we captured '*this' by
9359 // reference), update 'Result' to refer to what
9360 // the field refers to.
9361 if (LValueToRValueConversion) {
9362 APValue RVal;
9363 if (!handleLValueToRValueConversion(Info, Conv: E, Type: FD->getType(), LVal: Result, RVal))
9364 return false;
9365 Result.setFrom(Ctx: Info.Ctx, V: RVal);
9366 }
9367 return true;
9368}
9369
9370/// Evaluate an expression as an lvalue. This can be legitimately called on
9371/// expressions which are not glvalues, in three cases:
9372/// * function designators in C, and
9373/// * "extern void" objects
9374/// * @selector() expressions in Objective-C
9375static bool EvaluateLValue(const Expr *E, LValue &Result, EvalInfo &Info,
9376 bool InvalidBaseOK) {
9377 assert(!E->isValueDependent());
9378 assert(E->isGLValue() || E->getType()->isFunctionType() ||
9379 E->getType()->isVoidType() || isa<ObjCSelectorExpr>(E->IgnoreParens()));
9380 return LValueExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
9381}
9382
9383bool LValueExprEvaluator::VisitDeclRefExpr(const DeclRefExpr *E) {
9384 const ValueDecl *D = E->getDecl();
9385
9386 // If we are within a lambda's call operator, check whether the 'VD' referred
9387 // to within 'E' actually represents a lambda-capture that maps to a
9388 // data-member/field within the closure object, and if so, evaluate to the
9389 // field or what the field refers to.
9390 if (Info.CurrentCall && isLambdaCallOperator(DC: Info.CurrentCall->Callee) &&
9391 E->refersToEnclosingVariableOrCapture()) {
9392 // We don't always have a complete capture-map when checking or inferring if
9393 // the function call operator meets the requirements of a constexpr function
9394 // - but we don't need to evaluate the captures to determine constexprness
9395 // (dcl.constexpr C++17).
9396 if (Info.checkingPotentialConstantExpression())
9397 return false;
9398
9399 if (auto *FD = Info.CurrentCall->LambdaCaptureFields.lookup(Val: D)) {
9400 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
9401 return HandleLambdaCapture(Info, E, Result, MD, FD,
9402 LValueToRValueConversion: FD->getType()->isReferenceType());
9403 }
9404 }
9405
9406 if (isa<FunctionDecl, MSGuidDecl, TemplateParamObjectDecl,
9407 UnnamedGlobalConstantDecl>(Val: D))
9408 return Success(B: cast<ValueDecl>(Val: D));
9409 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
9410 return VisitVarDecl(E, VD);
9411 if (const BindingDecl *BD = dyn_cast<BindingDecl>(Val: D))
9412 return Visit(S: BD->getBinding());
9413 return Error(E);
9414}
9415
9416bool LValueExprEvaluator::VisitVarDecl(const Expr *E, const VarDecl *VD) {
9417 CallStackFrame *Frame = nullptr;
9418 unsigned Version = 0;
9419 if (VD->hasLocalStorage()) {
9420 // Only if a local variable was declared in the function currently being
9421 // evaluated, do we expect to be able to find its value in the current
9422 // frame. (Otherwise it was likely declared in an enclosing context and
9423 // could either have a valid evaluatable value (for e.g. a constexpr
9424 // variable) or be ill-formed (and trigger an appropriate evaluation
9425 // diagnostic)).
9426 CallStackFrame *CurrFrame = Info.CurrentCall;
9427 if (CurrFrame->Callee && CurrFrame->Callee->Equals(DC: VD->getDeclContext())) {
9428 // Function parameters are stored in some caller's frame. (Usually the
9429 // immediate caller, but for an inherited constructor they may be more
9430 // distant.)
9431 if (auto *PVD = dyn_cast<ParmVarDecl>(Val: VD)) {
9432 if (CurrFrame->Arguments) {
9433 VD = CurrFrame->Arguments.getOrigParam(PVD);
9434 Frame =
9435 Info.getCallFrameAndDepth(CallIndex: CurrFrame->Arguments.CallIndex).first;
9436 Version = CurrFrame->Arguments.Version;
9437 }
9438 } else {
9439 Frame = CurrFrame;
9440 Version = CurrFrame->getCurrentTemporaryVersion(Key: VD);
9441 }
9442 }
9443 }
9444
9445 if (!VD->getType()->isReferenceType()) {
9446 if (Frame) {
9447 Result.set(B: {VD, Frame->Index, Version});
9448 return true;
9449 }
9450 return Success(B: VD);
9451 }
9452
9453 if (!Info.getLangOpts().CPlusPlus11) {
9454 Info.CCEDiag(E, DiagId: diag::note_constexpr_ltor_non_integral, ExtraNotes: 1)
9455 << VD << VD->getType();
9456 Info.Note(Loc: VD->getLocation(), DiagId: diag::note_declared_at);
9457 }
9458
9459 APValue *V;
9460 if (!evaluateVarDeclInit(Info, E, VD, Frame, Version, Result&: V))
9461 return false;
9462
9463 if (!V) {
9464 Result.set(B: VD);
9465 Result.AllowConstexprUnknown = true;
9466 return true;
9467 }
9468
9469 return Success(V: *V, E);
9470}
9471
9472bool LValueExprEvaluator::VisitCallExpr(const CallExpr *E) {
9473 if (!IsConstantEvaluatedBuiltinCall(E))
9474 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9475
9476 switch (E->getBuiltinCallee()) {
9477 default:
9478 return false;
9479 case Builtin::BIas_const:
9480 case Builtin::BIforward:
9481 case Builtin::BIforward_like:
9482 case Builtin::BImove:
9483 case Builtin::BImove_if_noexcept:
9484 if (cast<FunctionDecl>(Val: E->getCalleeDecl())->isConstexpr())
9485 return Visit(S: E->getArg(Arg: 0));
9486 break;
9487 }
9488
9489 return ExprEvaluatorBaseTy::VisitCallExpr(E);
9490}
9491
9492bool LValueExprEvaluator::VisitMaterializeTemporaryExpr(
9493 const MaterializeTemporaryExpr *E) {
9494 // Walk through the expression to find the materialized temporary itself.
9495 SmallVector<const Expr *, 2> CommaLHSs;
9496 SmallVector<SubobjectAdjustment, 2> Adjustments;
9497 const Expr *Inner =
9498 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHS&: CommaLHSs, Adjustments);
9499
9500 // If we passed any comma operators, evaluate their LHSs.
9501 for (const Expr *E : CommaLHSs)
9502 if (!EvaluateIgnoredValue(Info, E))
9503 return false;
9504
9505 // A materialized temporary with static storage duration can appear within the
9506 // result of a constant expression evaluation, so we need to preserve its
9507 // value for use outside this evaluation.
9508 APValue *Value;
9509 if (E->getStorageDuration() == SD_Static) {
9510 if (Info.EvalMode == EvaluationMode::ConstantFold)
9511 return false;
9512 // FIXME: What about SD_Thread?
9513 Value = E->getOrCreateValue(MayCreate: true);
9514 *Value = APValue();
9515 Result.set(B: E);
9516 } else {
9517 Value = &Info.CurrentCall->createTemporary(
9518 Key: E, T: Inner->getType(),
9519 Scope: E->getStorageDuration() == SD_FullExpression ? ScopeKind::FullExpression
9520 : ScopeKind::Block,
9521 LV&: Result);
9522 }
9523
9524 QualType Type = Inner->getType();
9525
9526 // Materialize the temporary itself.
9527 if (!EvaluateInPlace(Result&: *Value, Info, This: Result, E: Inner)) {
9528 *Value = APValue();
9529 return false;
9530 }
9531
9532 // Adjust our lvalue to refer to the desired subobject.
9533 for (unsigned I = Adjustments.size(); I != 0; /**/) {
9534 --I;
9535 switch (Adjustments[I].Kind) {
9536 case SubobjectAdjustment::DerivedToBaseAdjustment:
9537 if (!HandleLValueBasePath(Info, E: Adjustments[I].DerivedToBase.BasePath,
9538 Type, Result))
9539 return false;
9540 Type = Adjustments[I].DerivedToBase.BasePath->getType();
9541 break;
9542
9543 case SubobjectAdjustment::FieldAdjustment:
9544 if (!HandleLValueMember(Info, E, LVal&: Result, FD: Adjustments[I].Field))
9545 return false;
9546 Type = Adjustments[I].Field->getType();
9547 break;
9548
9549 case SubobjectAdjustment::MemberPointerAdjustment:
9550 if (!HandleMemberPointerAccess(Info&: this->Info, LVType: Type, LV&: Result,
9551 RHS: Adjustments[I].Ptr.RHS))
9552 return false;
9553 Type = Adjustments[I].Ptr.MPT->getPointeeType();
9554 break;
9555 }
9556 }
9557
9558 return true;
9559}
9560
9561bool
9562LValueExprEvaluator::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
9563 assert((!Info.getLangOpts().CPlusPlus || E->isFileScope()) &&
9564 "lvalue compound literal in c++?");
9565 APValue *Lit;
9566 // If CompountLiteral has static storage, its value can be used outside
9567 // this expression. So evaluate it once and store it in ASTContext.
9568 if (E->hasStaticStorage()) {
9569 Lit = &E->getOrCreateStaticValue(Ctx&: Info.Ctx);
9570 Result.set(B: E);
9571 // Reset any previously evaluated state, otherwise evaluation below might
9572 // fail.
9573 // FIXME: Should we just re-use the previously evaluated value instead?
9574 *Lit = APValue();
9575 } else {
9576 assert(!Info.getLangOpts().CPlusPlus);
9577 Lit = &Info.CurrentCall->createTemporary(Key: E, T: E->getInitializer()->getType(),
9578 Scope: ScopeKind::Block, LV&: Result);
9579 }
9580 // FIXME: Evaluating in place isn't always right. We should figure out how to
9581 // use appropriate evaluation context here, see
9582 // clang/test/AST/static-compound-literals-reeval.cpp for a failure.
9583 if (!EvaluateInPlace(Result&: *Lit, Info, This: Result, E: E->getInitializer())) {
9584 *Lit = APValue();
9585 return false;
9586 }
9587 return true;
9588}
9589
9590bool LValueExprEvaluator::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
9591 TypeInfoLValue TypeInfo;
9592
9593 if (!E->isPotentiallyEvaluated()) {
9594 if (E->isTypeOperand())
9595 TypeInfo = TypeInfoLValue(E->getTypeOperand(Context: Info.Ctx).getTypePtr());
9596 else
9597 TypeInfo = TypeInfoLValue(E->getExprOperand()->getType().getTypePtr());
9598 } else {
9599 if (!Info.Ctx.getLangOpts().CPlusPlus20) {
9600 Info.CCEDiag(E, DiagId: diag::note_constexpr_typeid_polymorphic)
9601 << E->getExprOperand()->getType()
9602 << E->getExprOperand()->getSourceRange();
9603 }
9604
9605 if (!Visit(S: E->getExprOperand()))
9606 return false;
9607
9608 std::optional<DynamicType> DynType =
9609 ComputeDynamicType(Info, E, This&: Result, AK: AK_TypeId);
9610 if (!DynType)
9611 return false;
9612
9613 TypeInfo = TypeInfoLValue(
9614 Info.Ctx.getCanonicalTagType(TD: DynType->Type).getTypePtr());
9615 }
9616
9617 return Success(B: APValue::LValueBase::getTypeInfo(LV: TypeInfo, TypeInfo: E->getType()));
9618}
9619
9620bool LValueExprEvaluator::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
9621 return Success(B: E->getGuidDecl());
9622}
9623
9624bool LValueExprEvaluator::VisitMemberExpr(const MemberExpr *E) {
9625 // Handle static data members.
9626 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: E->getMemberDecl())) {
9627 VisitIgnoredBaseExpression(E: E->getBase());
9628 return VisitVarDecl(E, VD);
9629 }
9630
9631 // Handle static member functions.
9632 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: E->getMemberDecl())) {
9633 if (MD->isStatic()) {
9634 VisitIgnoredBaseExpression(E: E->getBase());
9635 return Success(B: MD);
9636 }
9637 }
9638
9639 // Handle non-static data members.
9640 return LValueExprEvaluatorBaseTy::VisitMemberExpr(E);
9641}
9642
9643bool LValueExprEvaluator::VisitExtVectorElementExpr(
9644 const ExtVectorElementExpr *E) {
9645 bool Success = true;
9646
9647 APValue Val;
9648 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9649 if (!Info.noteFailure())
9650 return false;
9651 Success = false;
9652 }
9653
9654 SmallVector<uint32_t, 4> Indices;
9655 E->getEncodedElementAccess(Elts&: Indices);
9656 // FIXME: support accessing more than one element
9657 if (Indices.size() > 1)
9658 return false;
9659
9660 if (Success) {
9661 Result.setFrom(Ctx: Info.Ctx, V: Val);
9662 QualType BaseType = E->getBase()->getType();
9663 if (E->isArrow())
9664 BaseType = BaseType->getPointeeType();
9665 const auto *VT = BaseType->castAs<VectorType>();
9666 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9667 Size: VT->getNumElements(), Idx: Indices[0]);
9668 }
9669
9670 return Success;
9671}
9672
9673bool LValueExprEvaluator::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
9674 if (E->getBase()->getType()->isSveVLSBuiltinType())
9675 return Error(E);
9676
9677 APSInt Index;
9678 bool Success = true;
9679
9680 if (const auto *VT = E->getBase()->getType()->getAs<VectorType>()) {
9681 APValue Val;
9682 if (!Evaluate(Result&: Val, Info, E: E->getBase())) {
9683 if (!Info.noteFailure())
9684 return false;
9685 Success = false;
9686 }
9687
9688 if (!EvaluateInteger(E: E->getIdx(), Result&: Index, Info)) {
9689 if (!Info.noteFailure())
9690 return false;
9691 Success = false;
9692 }
9693
9694 if (Success) {
9695 Result.setFrom(Ctx: Info.Ctx, V: Val);
9696 HandleLValueVectorElement(Info, E, LVal&: Result, EltTy: VT->getElementType(),
9697 Size: VT->getNumElements(), Idx: Index.getZExtValue());
9698 }
9699
9700 return Success;
9701 }
9702
9703 // C++17's rules require us to evaluate the LHS first, regardless of which
9704 // side is the base.
9705 for (const Expr *SubExpr : {E->getLHS(), E->getRHS()}) {
9706 if (SubExpr == E->getBase() ? !evaluatePointer(E: SubExpr, Result)
9707 : !EvaluateInteger(E: SubExpr, Result&: Index, Info)) {
9708 if (!Info.noteFailure())
9709 return false;
9710 Success = false;
9711 }
9712 }
9713
9714 return Success &&
9715 HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: E->getType(), Adjustment: Index);
9716}
9717
9718bool LValueExprEvaluator::VisitUnaryDeref(const UnaryOperator *E) {
9719 bool Success = evaluatePointer(E: E->getSubExpr(), Result);
9720 // [C++26][expr.unary.op]
9721 // If the operand points to an object or function, the result
9722 // denotes that object or function; otherwise, the behavior is undefined.
9723 // Because &(*(type*)0) is a common pattern, we do not fail the evaluation
9724 // immediately.
9725 if (!Success || !E->getType().getNonReferenceType()->isObjectType())
9726 return Success;
9727 return bool(findCompleteObject(Info, E, AK: AK_Dereference, LVal: Result,
9728 LValType: E->getType())) ||
9729 Info.noteUndefinedBehavior();
9730}
9731
9732bool LValueExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
9733 if (!Visit(S: E->getSubExpr()))
9734 return false;
9735 // __real is a no-op on scalar lvalues.
9736 if (E->getSubExpr()->getType()->isAnyComplexType())
9737 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: false);
9738 return true;
9739}
9740
9741bool LValueExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
9742 assert(E->getSubExpr()->getType()->isAnyComplexType() &&
9743 "lvalue __imag__ on scalar?");
9744 if (!Visit(S: E->getSubExpr()))
9745 return false;
9746 HandleLValueComplexElement(Info, E, LVal&: Result, EltTy: E->getType(), Imag: true);
9747 return true;
9748}
9749
9750bool LValueExprEvaluator::VisitUnaryPreIncDec(const UnaryOperator *UO) {
9751 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9752 return Error(E: UO);
9753
9754 if (!this->Visit(S: UO->getSubExpr()))
9755 return false;
9756
9757 return handleIncDec(
9758 Info&: this->Info, E: UO, LVal: Result, LValType: UO->getSubExpr()->getType(),
9759 IsIncrement: UO->isIncrementOp(), Old: nullptr);
9760}
9761
9762bool LValueExprEvaluator::VisitCompoundAssignOperator(
9763 const CompoundAssignOperator *CAO) {
9764 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9765 return Error(E: CAO);
9766
9767 bool Success = true;
9768
9769 // C++17 onwards require that we evaluate the RHS first.
9770 APValue RHS;
9771 if (!Evaluate(Result&: RHS, Info&: this->Info, E: CAO->getRHS())) {
9772 if (!Info.noteFailure())
9773 return false;
9774 Success = false;
9775 }
9776
9777 // The overall lvalue result is the result of evaluating the LHS.
9778 if (!this->Visit(S: CAO->getLHS()) || !Success)
9779 return false;
9780
9781 return handleCompoundAssignment(
9782 Info&: this->Info, E: CAO,
9783 LVal: Result, LValType: CAO->getLHS()->getType(), PromotedLValType: CAO->getComputationLHSType(),
9784 Opcode: CAO->getOpForCompoundAssignment(Opc: CAO->getOpcode()), RVal: RHS);
9785}
9786
9787bool LValueExprEvaluator::VisitBinAssign(const BinaryOperator *E) {
9788 if (!Info.getLangOpts().CPlusPlus14 && !Info.keepEvaluatingAfterFailure())
9789 return Error(E);
9790
9791 bool Success = true;
9792
9793 // C++17 onwards require that we evaluate the RHS first.
9794 APValue NewVal;
9795 if (!Evaluate(Result&: NewVal, Info&: this->Info, E: E->getRHS())) {
9796 if (!Info.noteFailure())
9797 return false;
9798 Success = false;
9799 }
9800
9801 if (!this->Visit(S: E->getLHS()) || !Success)
9802 return false;
9803
9804 if (Info.getLangOpts().CPlusPlus20 &&
9805 !MaybeHandleUnionActiveMemberChange(Info, LHSExpr: E->getLHS(), LHS: Result))
9806 return false;
9807
9808 return handleAssignment(Info&: this->Info, E, LVal: Result, LValType: E->getLHS()->getType(),
9809 Val&: NewVal);
9810}
9811
9812//===----------------------------------------------------------------------===//
9813// Pointer Evaluation
9814//===----------------------------------------------------------------------===//
9815
9816/// Convenience function. LVal's base must be a call to an alloc_size
9817/// function.
9818static bool getBytesReturnedByAllocSizeCall(const ASTContext &Ctx,
9819 const LValue &LVal,
9820 llvm::APInt &Result) {
9821 assert(isBaseAnAllocSizeCall(LVal.getLValueBase()) &&
9822 "Can't get the size of a non alloc_size function");
9823 const auto *Base = LVal.getLValueBase().get<const Expr *>();
9824 const CallExpr *CE = tryUnwrapAllocSizeCall(E: Base);
9825 std::optional<llvm::APInt> Size =
9826 CE->evaluateBytesReturnedByAllocSizeCall(Ctx);
9827 if (!Size)
9828 return false;
9829
9830 Result = std::move(*Size);
9831 return true;
9832}
9833
9834/// Attempts to evaluate the given LValueBase as the result of a call to
9835/// a function with the alloc_size attribute. If it was possible to do so, this
9836/// function will return true, make Result's Base point to said function call,
9837/// and mark Result's Base as invalid.
9838static bool evaluateLValueAsAllocSize(EvalInfo &Info, APValue::LValueBase Base,
9839 LValue &Result) {
9840 if (Base.isNull())
9841 return false;
9842
9843 // Because we do no form of static analysis, we only support const variables.
9844 //
9845 // Additionally, we can't support parameters, nor can we support static
9846 // variables (in the latter case, use-before-assign isn't UB; in the former,
9847 // we have no clue what they'll be assigned to).
9848 const auto *VD =
9849 dyn_cast_or_null<VarDecl>(Val: Base.dyn_cast<const ValueDecl *>());
9850 if (!VD || !VD->isLocalVarDecl() || !VD->getType().isConstQualified())
9851 return false;
9852
9853 const Expr *Init = VD->getAnyInitializer();
9854 if (!Init || Init->getType().isNull())
9855 return false;
9856
9857 const Expr *E = Init->IgnoreParens();
9858 if (!tryUnwrapAllocSizeCall(E))
9859 return false;
9860
9861 // Store E instead of E unwrapped so that the type of the LValue's base is
9862 // what the user wanted.
9863 Result.setInvalid(B: E);
9864
9865 QualType Pointee = E->getType()->castAs<PointerType>()->getPointeeType();
9866 Result.addUnsizedArray(Info, E, ElemTy: Pointee);
9867 return true;
9868}
9869
9870namespace {
9871class PointerExprEvaluator
9872 : public ExprEvaluatorBase<PointerExprEvaluator> {
9873 LValue &Result;
9874 bool InvalidBaseOK;
9875
9876 bool Success(const Expr *E) {
9877 Result.set(B: E);
9878 return true;
9879 }
9880
9881 bool evaluateLValue(const Expr *E, LValue &Result) {
9882 return EvaluateLValue(E, Result, Info, InvalidBaseOK);
9883 }
9884
9885 bool evaluatePointer(const Expr *E, LValue &Result) {
9886 return EvaluatePointer(E, Result, Info, InvalidBaseOK);
9887 }
9888
9889 bool visitNonBuiltinCallExpr(const CallExpr *E);
9890public:
9891
9892 PointerExprEvaluator(EvalInfo &info, LValue &Result, bool InvalidBaseOK)
9893 : ExprEvaluatorBaseTy(info), Result(Result),
9894 InvalidBaseOK(InvalidBaseOK) {}
9895
9896 bool Success(const APValue &V, const Expr *E) {
9897 Result.setFrom(Ctx: Info.Ctx, V);
9898 return true;
9899 }
9900 bool ZeroInitialization(const Expr *E) {
9901 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
9902 return true;
9903 }
9904
9905 bool VisitBinaryOperator(const BinaryOperator *E);
9906 bool VisitCastExpr(const CastExpr* E);
9907 bool VisitUnaryAddrOf(const UnaryOperator *E);
9908 bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
9909 { return Success(E); }
9910 bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
9911 if (E->isExpressibleAsConstantInitializer())
9912 return Success(E);
9913 if (Info.noteFailure())
9914 EvaluateIgnoredValue(Info, E: E->getSubExpr());
9915 return Error(E);
9916 }
9917 bool VisitObjCArrayLiteral(const ObjCArrayLiteral *E) {
9918 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
9919 }
9920 bool VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E) {
9921 return E->isExpressibleAsConstantInitializer() ? Success(E) : Error(E);
9922 }
9923 bool VisitAddrLabelExpr(const AddrLabelExpr *E)
9924 { return Success(E); }
9925 bool VisitCallExpr(const CallExpr *E);
9926 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
9927 bool VisitBlockExpr(const BlockExpr *E) {
9928 if (!E->getBlockDecl()->hasCaptures())
9929 return Success(E);
9930 return Error(E);
9931 }
9932 bool VisitCXXThisExpr(const CXXThisExpr *E) {
9933 auto DiagnoseInvalidUseOfThis = [&] {
9934 if (Info.getLangOpts().CPlusPlus11)
9935 Info.FFDiag(E, DiagId: diag::note_constexpr_this) << E->isImplicit();
9936 else
9937 Info.FFDiag(E);
9938 };
9939
9940 // Can't look at 'this' when checking a potential constant expression.
9941 if (Info.checkingPotentialConstantExpression())
9942 return false;
9943
9944 bool IsExplicitLambda =
9945 isLambdaCallWithExplicitObjectParameter(DC: Info.CurrentCall->Callee);
9946 if (!IsExplicitLambda) {
9947 if (!Info.CurrentCall->This) {
9948 DiagnoseInvalidUseOfThis();
9949 return false;
9950 }
9951
9952 Result = *Info.CurrentCall->This;
9953 }
9954
9955 if (isLambdaCallOperator(DC: Info.CurrentCall->Callee)) {
9956 // Ensure we actually have captured 'this'. If something was wrong with
9957 // 'this' capture, the error would have been previously reported.
9958 // Otherwise we can be inside of a default initialization of an object
9959 // declared by lambda's body, so no need to return false.
9960 if (!Info.CurrentCall->LambdaThisCaptureField) {
9961 if (IsExplicitLambda && !Info.CurrentCall->This) {
9962 DiagnoseInvalidUseOfThis();
9963 return false;
9964 }
9965
9966 return true;
9967 }
9968
9969 const auto *MD = cast<CXXMethodDecl>(Val: Info.CurrentCall->Callee);
9970 return HandleLambdaCapture(
9971 Info, E, Result, MD, FD: Info.CurrentCall->LambdaThisCaptureField,
9972 LValueToRValueConversion: Info.CurrentCall->LambdaThisCaptureField->getType()->isPointerType());
9973 }
9974 return true;
9975 }
9976
9977 bool VisitCXXNewExpr(const CXXNewExpr *E);
9978
9979 bool VisitSourceLocExpr(const SourceLocExpr *E) {
9980 assert(!E->isIntType() && "SourceLocExpr isn't a pointer type?");
9981 APValue LValResult = E->EvaluateInContext(
9982 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
9983 Result.setFrom(Ctx: Info.Ctx, V: LValResult);
9984 return true;
9985 }
9986
9987 bool VisitEmbedExpr(const EmbedExpr *E) {
9988 llvm::report_fatal_error(reason: "Not yet implemented for ExprConstant.cpp");
9989 return true;
9990 }
9991
9992 bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E) {
9993 std::string ResultStr = E->ComputeName(Context&: Info.Ctx);
9994
9995 QualType CharTy = Info.Ctx.CharTy.withConst();
9996 APInt Size(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType()),
9997 ResultStr.size() + 1);
9998 QualType ArrayTy = Info.Ctx.getConstantArrayType(
9999 EltTy: CharTy, ArySize: Size, SizeExpr: nullptr, ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
10000
10001 StringLiteral *SL =
10002 StringLiteral::Create(Ctx: Info.Ctx, Str: ResultStr, Kind: StringLiteralKind::Ordinary,
10003 /*Pascal*/ false, Ty: ArrayTy, Locs: E->getLocation());
10004
10005 evaluateLValue(E: SL, Result);
10006 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val&: ArrayTy));
10007 return true;
10008 }
10009
10010 // FIXME: Missing: @protocol, @selector
10011};
10012} // end anonymous namespace
10013
10014static bool EvaluatePointer(const Expr* E, LValue& Result, EvalInfo &Info,
10015 bool InvalidBaseOK) {
10016 assert(!E->isValueDependent());
10017 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
10018 return PointerExprEvaluator(Info, Result, InvalidBaseOK).Visit(S: E);
10019}
10020
10021bool PointerExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
10022 if (E->getOpcode() != BO_Add &&
10023 E->getOpcode() != BO_Sub)
10024 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
10025
10026 const Expr *PExp = E->getLHS();
10027 const Expr *IExp = E->getRHS();
10028 if (IExp->getType()->isPointerType())
10029 std::swap(a&: PExp, b&: IExp);
10030
10031 bool EvalPtrOK = evaluatePointer(E: PExp, Result);
10032 if (!EvalPtrOK && !Info.noteFailure())
10033 return false;
10034
10035 llvm::APSInt Offset;
10036 if (!EvaluateInteger(E: IExp, Result&: Offset, Info) || !EvalPtrOK)
10037 return false;
10038
10039 if (E->getOpcode() == BO_Sub)
10040 negateAsSigned(Int&: Offset);
10041
10042 QualType Pointee = PExp->getType()->castAs<PointerType>()->getPointeeType();
10043 return HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: Pointee, Adjustment: Offset);
10044}
10045
10046bool PointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
10047 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
10048 // operator, neither operator is evaluated and the result is as if both were
10049 // omitted (except that the operators' constraints, already enforced by Sema,
10050 // still apply, and the result is not an lvalue). So '&*p' is just the pointer
10051 // value 'p' with no dereference, and forming it is therefore not undefined
10052 // behavior even when 'p' is null, e.g. '&*(int *)0'. Evaluate the pointer
10053 // operand directly so we don't spuriously diagnose a null dereference.
10054 if (!Info.getLangOpts().CPlusPlus) {
10055 const Expr *Sub = E->getSubExpr()->IgnoreParens();
10056 if (const auto *Deref = dyn_cast<UnaryOperator>(Val: Sub);
10057 Deref && Deref->getOpcode() == UO_Deref)
10058 return evaluatePointer(E: Deref->getSubExpr(), Result);
10059 }
10060 return evaluateLValue(E: E->getSubExpr(), Result);
10061}
10062
10063// Is the provided decl 'std::source_location::current'?
10064static bool IsDeclSourceLocationCurrent(const FunctionDecl *FD) {
10065 if (!FD)
10066 return false;
10067 const IdentifierInfo *FnII = FD->getIdentifier();
10068 if (!FnII || !FnII->isStr(Str: "current"))
10069 return false;
10070
10071 const auto *RD = dyn_cast<RecordDecl>(Val: FD->getParent());
10072 if (!RD)
10073 return false;
10074
10075 const IdentifierInfo *ClassII = RD->getIdentifier();
10076 return RD->isInStdNamespace() && ClassII && ClassII->isStr(Str: "source_location");
10077}
10078
10079bool PointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
10080 const Expr *SubExpr = E->getSubExpr();
10081
10082 switch (E->getCastKind()) {
10083 default:
10084 break;
10085 case CK_BitCast:
10086 case CK_CPointerToObjCPointerCast:
10087 case CK_BlockPointerToObjCPointerCast:
10088 case CK_AnyPointerToBlockPointerCast:
10089 case CK_AddressSpaceConversion:
10090 if (!Visit(S: SubExpr))
10091 return false;
10092 if (E->getType()->isFunctionPointerType() ||
10093 SubExpr->getType()->isFunctionPointerType()) {
10094 // Casting between two function pointer types, or between a function
10095 // pointer and an object pointer, is always a reinterpret_cast.
10096 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10097 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10098 << Info.Ctx.getLangOpts().CPlusPlus;
10099 Result.Designator.setInvalid();
10100 } else if (!E->getType()->isVoidPointerType()) {
10101 // Bitcasts to cv void* are static_casts, not reinterpret_casts, so are
10102 // permitted in constant expressions in C++11. Bitcasts from cv void* are
10103 // also static_casts, but we disallow them as a resolution to DR1312.
10104 //
10105 // In some circumstances, we permit casting from void* to cv1 T*, when the
10106 // actual pointee object is actually a cv2 T.
10107 bool HasValidResult = !Result.InvalidBase && !Result.Designator.Invalid &&
10108 !Result.IsNullPtr;
10109 bool VoidPtrCastMaybeOK =
10110 Result.IsNullPtr ||
10111 (HasValidResult &&
10112 Info.Ctx.hasSimilarType(T1: Result.Designator.getType(Ctx&: Info.Ctx),
10113 T2: E->getType()->getPointeeType()));
10114 // 1. We'll allow it in std::allocator::allocate, and anything which that
10115 // calls.
10116 // 2. HACK 2022-03-28: Work around an issue with libstdc++'s
10117 // <source_location> header. Fixed in GCC 12 and later (2022-04-??).
10118 // We'll allow it in the body of std::source_location::current. GCC's
10119 // implementation had a parameter of type `void*`, and casts from
10120 // that back to `const __impl*` in its body.
10121 if (VoidPtrCastMaybeOK &&
10122 (Info.getStdAllocatorCaller(FnName: "allocate") ||
10123 IsDeclSourceLocationCurrent(FD: Info.CurrentCall->Callee) ||
10124 Info.getLangOpts().CPlusPlus26)) {
10125 // Permitted.
10126 } else {
10127 if (SubExpr->getType()->isVoidPointerType() &&
10128 Info.getLangOpts().CPlusPlus) {
10129 if (HasValidResult)
10130 CCEDiag(E, D: diag::note_constexpr_invalid_void_star_cast)
10131 << SubExpr->getType() << Info.getLangOpts().CPlusPlus26
10132 << Result.Designator.getType(Ctx&: Info.Ctx).getCanonicalType()
10133 << E->getType()->getPointeeType();
10134 else
10135 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10136 << diag::ConstexprInvalidCastKind::CastFrom
10137 << SubExpr->getType();
10138 } else
10139 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10140 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10141 << Info.Ctx.getLangOpts().CPlusPlus;
10142 Result.Designator.setInvalid();
10143 }
10144 }
10145 if (E->getCastKind() == CK_AddressSpaceConversion && Result.IsNullPtr)
10146 ZeroInitialization(E);
10147 return true;
10148
10149 case CK_DerivedToBase:
10150 case CK_UncheckedDerivedToBase:
10151 if (!evaluatePointer(E: E->getSubExpr(), Result))
10152 return false;
10153 if (!Result.Base && Result.Offset.isZero())
10154 return true;
10155
10156 // Now figure out the necessary offset to add to the base LV to get from
10157 // the derived class to the base class.
10158 return HandleLValueBasePath(Info, E, Type: E->getSubExpr()->getType()->
10159 castAs<PointerType>()->getPointeeType(),
10160 Result);
10161
10162 case CK_BaseToDerived:
10163 if (!Visit(S: E->getSubExpr()))
10164 return false;
10165 if (!Result.Base && Result.Offset.isZero())
10166 return true;
10167 return HandleBaseToDerivedCast(Info, E, Result);
10168
10169 case CK_Dynamic:
10170 if (!Visit(S: E->getSubExpr()))
10171 return false;
10172 return HandleDynamicCast(Info, E: cast<ExplicitCastExpr>(Val: E), Ptr&: Result);
10173
10174 case CK_NullToPointer:
10175 VisitIgnoredValue(E: E->getSubExpr());
10176 return ZeroInitialization(E);
10177
10178 case CK_IntegralToPointer: {
10179 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
10180 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
10181 << Info.Ctx.getLangOpts().CPlusPlus;
10182
10183 APValue Value;
10184 if (!EvaluateIntegerOrLValue(E: SubExpr, Result&: Value, Info))
10185 break;
10186
10187 if (Value.isInt()) {
10188 unsigned Size = Info.Ctx.getTypeSize(T: E->getType());
10189 uint64_t N = Value.getInt().extOrTrunc(width: Size).getZExtValue();
10190 if (N == Info.Ctx.getTargetNullPointerValue(QT: E->getType())) {
10191 Result.setNull(Ctx&: Info.Ctx, PointerTy: E->getType());
10192 } else {
10193 Result.Base = (Expr *)nullptr;
10194 Result.InvalidBase = false;
10195 Result.Offset = CharUnits::fromQuantity(Quantity: N);
10196 Result.Designator.setInvalid();
10197 Result.IsNullPtr = false;
10198 }
10199 return true;
10200 } else {
10201 // In rare instances, the value isn't an lvalue.
10202 // For example, when the value is the difference between the addresses of
10203 // two labels. We reject that as a constant expression because we can't
10204 // compute a valid offset to convert into a pointer.
10205 if (!Value.isLValue())
10206 return false;
10207
10208 // Cast is of an lvalue, no need to change value.
10209 Result.setFrom(Ctx: Info.Ctx, V: Value);
10210 return true;
10211 }
10212 }
10213
10214 case CK_ArrayToPointerDecay: {
10215 if (SubExpr->isGLValue()) {
10216 if (!evaluateLValue(E: SubExpr, Result))
10217 return false;
10218 } else {
10219 APValue &Value = Info.CurrentCall->createTemporary(
10220 Key: SubExpr, T: SubExpr->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
10221 if (!EvaluateInPlace(Result&: Value, Info, This: Result, E: SubExpr))
10222 return false;
10223 }
10224 // The result is a pointer to the first element of the array.
10225 auto *AT = Info.Ctx.getAsArrayType(T: SubExpr->getType());
10226 if (auto *CAT = dyn_cast<ConstantArrayType>(Val: AT))
10227 Result.addArray(Info, E, CAT);
10228 else
10229 Result.addUnsizedArray(Info, E, ElemTy: AT->getElementType());
10230 return true;
10231 }
10232
10233 case CK_FunctionToPointerDecay:
10234 return evaluateLValue(E: SubExpr, Result);
10235
10236 case CK_LValueToRValue: {
10237 LValue LVal;
10238 if (!evaluateLValue(E: E->getSubExpr(), Result&: LVal))
10239 return false;
10240
10241 APValue RVal;
10242 // Note, we use the subexpression's type in order to retain cv-qualifiers.
10243 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getSubExpr()->getType(),
10244 LVal, RVal))
10245 return InvalidBaseOK &&
10246 evaluateLValueAsAllocSize(Info, Base: LVal.Base, Result);
10247 return Success(V: RVal, E);
10248 }
10249 }
10250
10251 return ExprEvaluatorBaseTy::VisitCastExpr(E);
10252}
10253
10254static CharUnits GetAlignOfType(const ASTContext &Ctx, QualType T,
10255 UnaryExprOrTypeTrait ExprKind) {
10256 // C++ [expr.alignof]p3:
10257 // When alignof is applied to a reference type, the result is the
10258 // alignment of the referenced type.
10259 T = T.getNonReferenceType();
10260
10261 if (T.getQualifiers().hasUnaligned())
10262 return CharUnits::One();
10263
10264 const bool AlignOfReturnsPreferred =
10265 Ctx.getLangOpts().isCompatibleWith(Version: LangOptions::ClangABI::Ver7);
10266
10267 // __alignof is defined to return the preferred alignment.
10268 // Before 8, clang returned the preferred alignment for alignof and _Alignof
10269 // as well.
10270 if (ExprKind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
10271 return Ctx.toCharUnitsFromBits(BitSize: Ctx.getPreferredTypeAlign(T: T.getTypePtr()));
10272 // alignof and _Alignof are defined to return the ABI alignment.
10273 else if (ExprKind == UETT_AlignOf)
10274 return Ctx.getTypeAlignInChars(T: T.getTypePtr());
10275 else
10276 llvm_unreachable("GetAlignOfType on a non-alignment ExprKind");
10277}
10278
10279CharUnits GetAlignOfExpr(const ASTContext &Ctx, const Expr *E,
10280 UnaryExprOrTypeTrait ExprKind) {
10281 E = E->IgnoreParens();
10282
10283 // The kinds of expressions that we have special-case logic here for
10284 // should be kept up to date with the special checks for those
10285 // expressions in Sema.
10286
10287 // alignof decl is always accepted, even if it doesn't make sense: we default
10288 // to 1 in those cases.
10289 if (const DeclRefExpr *DRE = dyn_cast<DeclRefExpr>(Val: E))
10290 return Ctx.getDeclAlign(D: DRE->getDecl(),
10291 /*RefAsPointee*/ ForAlignof: true);
10292
10293 if (const MemberExpr *ME = dyn_cast<MemberExpr>(Val: E))
10294 return Ctx.getDeclAlign(D: ME->getMemberDecl(),
10295 /*RefAsPointee*/ ForAlignof: true);
10296
10297 return GetAlignOfType(Ctx, T: E->getType(), ExprKind);
10298}
10299
10300static CharUnits getBaseAlignment(EvalInfo &Info, const LValue &Value) {
10301 if (const auto *VD = Value.Base.dyn_cast<const ValueDecl *>())
10302 return Info.Ctx.getDeclAlign(D: VD);
10303 if (const auto *E = Value.Base.dyn_cast<const Expr *>())
10304 return GetAlignOfExpr(Ctx: Info.Ctx, E, ExprKind: UETT_AlignOf);
10305 return GetAlignOfType(Ctx: Info.Ctx, T: Value.Base.getTypeInfoType(), ExprKind: UETT_AlignOf);
10306}
10307
10308/// Evaluate the value of the alignment argument to __builtin_align_{up,down},
10309/// __builtin_is_aligned and __builtin_assume_aligned.
10310static bool getAlignmentArgument(const Expr *E, QualType ForType,
10311 EvalInfo &Info, APSInt &Alignment) {
10312 if (!EvaluateInteger(E, Result&: Alignment, Info))
10313 return false;
10314 if (Alignment < 0 || !Alignment.isPowerOf2()) {
10315 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_alignment) << Alignment;
10316 return false;
10317 }
10318 unsigned SrcWidth = Info.Ctx.getIntWidth(T: ForType);
10319 APSInt MaxValue(APInt::getOneBitSet(numBits: SrcWidth, BitNo: SrcWidth - 1));
10320 if (APSInt::compareValues(I1: Alignment, I2: MaxValue) > 0) {
10321 Info.FFDiag(E, DiagId: diag::note_constexpr_alignment_too_big)
10322 << MaxValue << ForType << Alignment;
10323 return false;
10324 }
10325 // Ensure both alignment and source value have the same bit width so that we
10326 // don't assert when computing the resulting value.
10327 APSInt ExtAlignment =
10328 APSInt(Alignment.zextOrTrunc(width: SrcWidth), /*isUnsigned=*/true);
10329 assert(APSInt::compareValues(Alignment, ExtAlignment) == 0 &&
10330 "Alignment should not be changed by ext/trunc");
10331 Alignment = ExtAlignment;
10332 assert(Alignment.getBitWidth() == SrcWidth);
10333 return true;
10334}
10335
10336// To be clear: this happily visits unsupported builtins. Better name welcomed.
10337bool PointerExprEvaluator::visitNonBuiltinCallExpr(const CallExpr *E) {
10338 if (ExprEvaluatorBaseTy::VisitCallExpr(E))
10339 return true;
10340
10341 if (!(InvalidBaseOK && E->getCalleeAllocSizeAttr()))
10342 return false;
10343
10344 Result.setInvalid(B: E);
10345 QualType PointeeTy = E->getType()->castAs<PointerType>()->getPointeeType();
10346 Result.addUnsizedArray(Info, E, ElemTy: PointeeTy);
10347 return true;
10348}
10349
10350bool PointerExprEvaluator::VisitCallExpr(const CallExpr *E) {
10351 if (!IsConstantEvaluatedBuiltinCall(E))
10352 return visitNonBuiltinCallExpr(E);
10353 return VisitBuiltinCallExpr(E, BuiltinOp: E->getBuiltinCallee());
10354}
10355
10356// Determine if T is a character type for which we guarantee that
10357// sizeof(T) == 1.
10358static bool isOneByteCharacterType(QualType T) {
10359 return T->isCharType() || T->isChar8Type();
10360}
10361
10362bool PointerExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
10363 unsigned BuiltinOp) {
10364 if (IsOpaqueConstantCall(E))
10365 return Success(E);
10366
10367 switch (BuiltinOp) {
10368 case Builtin::BIaddressof:
10369 case Builtin::BI__addressof:
10370 case Builtin::BI__builtin_addressof:
10371 return evaluateLValue(E: E->getArg(Arg: 0), Result);
10372 case Builtin::BI__builtin_assume_aligned: {
10373 // We need to be very careful here because: if the pointer does not have the
10374 // asserted alignment, then the behavior is undefined, and undefined
10375 // behavior is non-constant.
10376 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10377 return false;
10378
10379 LValue OffsetResult(Result);
10380 APSInt Alignment;
10381 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10382 Alignment))
10383 return false;
10384 CharUnits Align = CharUnits::fromQuantity(Quantity: Alignment.getZExtValue());
10385
10386 if (E->getNumArgs() > 2) {
10387 APSInt Offset;
10388 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Offset, Info))
10389 return false;
10390
10391 int64_t AdditionalOffset = -Offset.getZExtValue();
10392 OffsetResult.Offset += CharUnits::fromQuantity(Quantity: AdditionalOffset);
10393 }
10394
10395 // If there is a base object, then it must have the correct alignment.
10396 if (OffsetResult.Base) {
10397 CharUnits BaseAlignment = getBaseAlignment(Info, Value: OffsetResult);
10398
10399 if (BaseAlignment < Align) {
10400 Result.Designator.setInvalid();
10401 CCEDiag(E: E->getArg(Arg: 0), D: diag::note_constexpr_baa_insufficient_alignment)
10402 << 0 << BaseAlignment.getQuantity() << Align.getQuantity();
10403 return false;
10404 }
10405 }
10406
10407 // The offset must also have the correct alignment.
10408 if (OffsetResult.Offset.alignTo(Align) != OffsetResult.Offset) {
10409 Result.Designator.setInvalid();
10410
10411 (OffsetResult.Base
10412 ? CCEDiag(E: E->getArg(Arg: 0),
10413 D: diag::note_constexpr_baa_insufficient_alignment)
10414 << 1
10415 : CCEDiag(E: E->getArg(Arg: 0),
10416 D: diag::note_constexpr_baa_value_insufficient_alignment))
10417 << OffsetResult.Offset.getQuantity() << Align.getQuantity();
10418 return false;
10419 }
10420
10421 return true;
10422 }
10423 case Builtin::BI__builtin_align_up:
10424 case Builtin::BI__builtin_align_down: {
10425 if (!evaluatePointer(E: E->getArg(Arg: 0), Result))
10426 return false;
10427 APSInt Alignment;
10428 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: E->getArg(Arg: 0)->getType(), Info,
10429 Alignment))
10430 return false;
10431 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Result);
10432 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Result.Offset);
10433 // For align_up/align_down, we can return the same value if the alignment
10434 // is known to be greater or equal to the requested value.
10435 if (PtrAlign.getQuantity() >= Alignment)
10436 return true;
10437
10438 // The alignment could be greater than the minimum at run-time, so we cannot
10439 // infer much about the resulting pointer value. One case is possible:
10440 // For `_Alignas(32) char buf[N]; __builtin_align_down(&buf[idx], 32)` we
10441 // can infer the correct index if the requested alignment is smaller than
10442 // the base alignment so we can perform the computation on the offset.
10443 if (BaseAlignment.getQuantity() >= Alignment) {
10444 assert(Alignment.getBitWidth() <= 64 &&
10445 "Cannot handle > 64-bit address-space");
10446 uint64_t Alignment64 = Alignment.getZExtValue();
10447 CharUnits NewOffset = CharUnits::fromQuantity(
10448 Quantity: BuiltinOp == Builtin::BI__builtin_align_down
10449 ? llvm::alignDown(Value: Result.Offset.getQuantity(), Align: Alignment64)
10450 : llvm::alignTo(Value: Result.Offset.getQuantity(), Align: Alignment64));
10451 Result.adjustOffset(N: NewOffset - Result.Offset);
10452 // TODO: diagnose out-of-bounds values/only allow for arrays?
10453 return true;
10454 }
10455 // Otherwise, we cannot constant-evaluate the result.
10456 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_adjust)
10457 << Alignment;
10458 return false;
10459 }
10460 case Builtin::BI__builtin_operator_new:
10461 return HandleOperatorNewCall(Info, E, Result);
10462 case Builtin::BI__builtin_launder:
10463 return evaluatePointer(E: E->getArg(Arg: 0), Result);
10464 case Builtin::BIstrchr:
10465 case Builtin::BIwcschr:
10466 case Builtin::BImemchr:
10467 case Builtin::BIwmemchr:
10468 if (Info.getLangOpts().CPlusPlus11)
10469 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10470 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10471 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10472 else
10473 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10474 [[fallthrough]];
10475 case Builtin::BI__builtin_strchr:
10476 case Builtin::BI__builtin_wcschr:
10477 case Builtin::BI__builtin_memchr:
10478 case Builtin::BI__builtin_char_memchr:
10479 case Builtin::BI__builtin_wmemchr: {
10480 if (!Visit(S: E->getArg(Arg: 0)))
10481 return false;
10482 APSInt Desired;
10483 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Desired, Info))
10484 return false;
10485 uint64_t MaxLength = uint64_t(-1);
10486 if (BuiltinOp != Builtin::BIstrchr &&
10487 BuiltinOp != Builtin::BIwcschr &&
10488 BuiltinOp != Builtin::BI__builtin_strchr &&
10489 BuiltinOp != Builtin::BI__builtin_wcschr) {
10490 APSInt N;
10491 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10492 return false;
10493 MaxLength = N.getZExtValue();
10494 }
10495 // We cannot find the value if there are no candidates to match against.
10496 if (MaxLength == 0u)
10497 return ZeroInitialization(E);
10498 if (!Result.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
10499 Result.Designator.Invalid)
10500 return false;
10501 QualType CharTy = Result.Designator.getType(Ctx&: Info.Ctx);
10502 bool IsRawByte = BuiltinOp == Builtin::BImemchr ||
10503 BuiltinOp == Builtin::BI__builtin_memchr;
10504 assert(IsRawByte ||
10505 Info.Ctx.hasSameUnqualifiedType(
10506 CharTy, E->getArg(0)->getType()->getPointeeType()));
10507 // Pointers to const void may point to objects of incomplete type.
10508 if (IsRawByte && CharTy->isIncompleteType()) {
10509 Info.FFDiag(E, DiagId: diag::note_constexpr_ltor_incomplete_type) << CharTy;
10510 return false;
10511 }
10512 // Give up on byte-oriented matching against multibyte elements.
10513 // FIXME: We can compare the bytes in the correct order.
10514 if (IsRawByte && !isOneByteCharacterType(T: CharTy)) {
10515 Info.FFDiag(E, DiagId: diag::note_constexpr_memchr_unsupported)
10516 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy;
10517 return false;
10518 }
10519 // Figure out what value we're actually looking for (after converting to
10520 // the corresponding unsigned type if necessary).
10521 uint64_t DesiredVal;
10522 bool StopAtNull = false;
10523 switch (BuiltinOp) {
10524 case Builtin::BIstrchr:
10525 case Builtin::BI__builtin_strchr:
10526 // strchr compares directly to the passed integer, and therefore
10527 // always fails if given an int that is not a char.
10528 if (!APSInt::isSameValue(I1: HandleIntToIntCast(Info, E, DestType: CharTy,
10529 SrcType: E->getArg(Arg: 1)->getType(),
10530 Value: Desired),
10531 I2: Desired))
10532 return ZeroInitialization(E);
10533 StopAtNull = true;
10534 [[fallthrough]];
10535 case Builtin::BImemchr:
10536 case Builtin::BI__builtin_memchr:
10537 case Builtin::BI__builtin_char_memchr:
10538 // memchr compares by converting both sides to unsigned char. That's also
10539 // correct for strchr if we get this far (to cope with plain char being
10540 // unsigned in the strchr case).
10541 DesiredVal = Desired.trunc(width: Info.Ctx.getCharWidth()).getZExtValue();
10542 break;
10543
10544 case Builtin::BIwcschr:
10545 case Builtin::BI__builtin_wcschr:
10546 StopAtNull = true;
10547 [[fallthrough]];
10548 case Builtin::BIwmemchr:
10549 case Builtin::BI__builtin_wmemchr:
10550 // wcschr and wmemchr are given a wchar_t to look for. Just use it.
10551 DesiredVal = Desired.getZExtValue();
10552 break;
10553 }
10554
10555 for (; MaxLength; --MaxLength) {
10556 APValue Char;
10557 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: Result, RVal&: Char) ||
10558 !Char.isInt())
10559 return false;
10560 if (Char.getInt().getZExtValue() == DesiredVal)
10561 return true;
10562 if (StopAtNull && !Char.getInt())
10563 break;
10564 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Result, EltTy: CharTy, Adjustment: 1))
10565 return false;
10566 }
10567 // Not found: return nullptr.
10568 return ZeroInitialization(E);
10569 }
10570
10571 case Builtin::BImemcpy:
10572 case Builtin::BImemmove:
10573 case Builtin::BIwmemcpy:
10574 case Builtin::BIwmemmove:
10575 if (Info.getLangOpts().CPlusPlus11)
10576 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
10577 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
10578 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
10579 else
10580 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
10581 [[fallthrough]];
10582 case Builtin::BI__builtin_memcpy:
10583 case Builtin::BI__builtin_memmove:
10584 case Builtin::BI__builtin_wmemcpy:
10585 case Builtin::BI__builtin_wmemmove: {
10586 bool WChar = BuiltinOp == Builtin::BIwmemcpy ||
10587 BuiltinOp == Builtin::BIwmemmove ||
10588 BuiltinOp == Builtin::BI__builtin_wmemcpy ||
10589 BuiltinOp == Builtin::BI__builtin_wmemmove;
10590 bool Move = BuiltinOp == Builtin::BImemmove ||
10591 BuiltinOp == Builtin::BIwmemmove ||
10592 BuiltinOp == Builtin::BI__builtin_memmove ||
10593 BuiltinOp == Builtin::BI__builtin_wmemmove;
10594
10595 // The result of mem* is the first argument.
10596 if (!Visit(S: E->getArg(Arg: 0)))
10597 return false;
10598 LValue Dest = Result;
10599
10600 LValue Src;
10601 if (!EvaluatePointer(E: E->getArg(Arg: 1), Result&: Src, Info))
10602 return false;
10603
10604 APSInt N;
10605 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
10606 return false;
10607 assert(!N.isSigned() && "memcpy and friends take an unsigned size");
10608
10609 // If the size is zero, we treat this as always being a valid no-op.
10610 // (Even if one of the src and dest pointers is null.)
10611 if (!N)
10612 return true;
10613
10614 // Otherwise, if either of the operands is null, we can't proceed. Don't
10615 // try to determine the type of the copied objects, because there aren't
10616 // any.
10617 if (!Src.Base || !Dest.Base) {
10618 APValue Val;
10619 (!Src.Base ? Src : Dest).moveInto(V&: Val);
10620 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_null)
10621 << Move << WChar << !!Src.Base
10622 << Val.getAsString(Ctx: Info.Ctx, Ty: E->getArg(Arg: 0)->getType());
10623 return false;
10624 }
10625 if (Src.Designator.Invalid || Dest.Designator.Invalid)
10626 return false;
10627
10628 // We require that Src and Dest are both pointers to arrays of
10629 // trivially-copyable type. (For the wide version, the designator will be
10630 // invalid if the designated object is not a wchar_t.)
10631 QualType T = Dest.Designator.getType(Ctx&: Info.Ctx);
10632 QualType SrcT = Src.Designator.getType(Ctx&: Info.Ctx);
10633 if (!Info.Ctx.hasSameUnqualifiedType(T1: T, T2: SrcT)) {
10634 // FIXME: Consider using our bit_cast implementation to support this.
10635 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_type_pun) << Move << SrcT << T;
10636 return false;
10637 }
10638 if (T->isIncompleteType()) {
10639 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_incomplete_type) << Move << T;
10640 return false;
10641 }
10642 if (!T.isTriviallyCopyableType(Context: Info.Ctx)) {
10643 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_nontrivial) << Move << T;
10644 return false;
10645 }
10646
10647 // Figure out how many T's we're copying.
10648 uint64_t TSize = Info.Ctx.getTypeSizeInChars(T).getQuantity();
10649 if (TSize == 0)
10650 return false;
10651 if (!WChar) {
10652 uint64_t Remainder;
10653 llvm::APInt OrigN = N;
10654 llvm::APInt::udivrem(LHS: OrigN, RHS: TSize, Quotient&: N, Remainder);
10655 if (Remainder) {
10656 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
10657 << Move << WChar << 0 << T << toString(I: OrigN, Radix: 10, /*Signed*/false)
10658 << (unsigned)TSize;
10659 return false;
10660 }
10661 }
10662
10663 // Check that the copying will remain within the arrays, just so that we
10664 // can give a more meaningful diagnostic. This implicitly also checks that
10665 // N fits into 64 bits.
10666 uint64_t RemainingSrcSize = Src.Designator.validIndexAdjustments().second;
10667 uint64_t RemainingDestSize = Dest.Designator.validIndexAdjustments().second;
10668 if (N.ugt(RHS: RemainingSrcSize) || N.ugt(RHS: RemainingDestSize)) {
10669 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_unsupported)
10670 << Move << WChar << (N.ugt(RHS: RemainingSrcSize) ? 1 : 2) << T
10671 << toString(I: N, Radix: 10, /*Signed*/false);
10672 return false;
10673 }
10674 uint64_t NElems = N.getZExtValue();
10675 uint64_t NBytes = NElems * TSize;
10676
10677 // Check for overlap.
10678 int Direction = 1;
10679 if (HasSameBase(A: Src, B: Dest)) {
10680 uint64_t SrcOffset = Src.getLValueOffset().getQuantity();
10681 uint64_t DestOffset = Dest.getLValueOffset().getQuantity();
10682 if (DestOffset >= SrcOffset && DestOffset - SrcOffset < NBytes) {
10683 // Dest is inside the source region.
10684 if (!Move) {
10685 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
10686 return false;
10687 }
10688 // For memmove and friends, copy backwards.
10689 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: NElems - 1) ||
10690 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: NElems - 1))
10691 return false;
10692 Direction = -1;
10693 } else if (!Move && SrcOffset >= DestOffset &&
10694 SrcOffset - DestOffset < NBytes) {
10695 // Src is inside the destination region for memcpy: invalid.
10696 Info.FFDiag(E, DiagId: diag::note_constexpr_memcpy_overlap) << WChar;
10697 return false;
10698 }
10699 }
10700
10701 while (true) {
10702 APValue Val;
10703 // FIXME: Set WantObjectRepresentation to true if we're copying a
10704 // char-like type?
10705 if (!handleLValueToRValueConversion(Info, Conv: E, Type: T, LVal: Src, RVal&: Val) ||
10706 !handleAssignment(Info, E, LVal: Dest, LValType: T, Val))
10707 return false;
10708 // Do not iterate past the last element; if we're copying backwards, that
10709 // might take us off the start of the array.
10710 if (--NElems == 0)
10711 return true;
10712 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Src, EltTy: T, Adjustment: Direction) ||
10713 !HandleLValueArrayAdjustment(Info, E, LVal&: Dest, EltTy: T, Adjustment: Direction))
10714 return false;
10715 }
10716 }
10717
10718 default:
10719 return false;
10720 }
10721}
10722
10723static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
10724 APValue &Result, const InitListExpr *ILE,
10725 QualType AllocType);
10726static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
10727 APValue &Result,
10728 const CXXConstructExpr *CCE,
10729 QualType AllocType);
10730
10731bool PointerExprEvaluator::VisitCXXNewExpr(const CXXNewExpr *E) {
10732 if (!Info.getLangOpts().CPlusPlus20)
10733 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
10734
10735 // We cannot speculatively evaluate a delete expression.
10736 if (Info.SpeculativeEvaluationDepth)
10737 return false;
10738
10739 FunctionDecl *OperatorNew = E->getOperatorNew();
10740 QualType AllocType = E->getAllocatedType();
10741 QualType TargetType = AllocType;
10742
10743 bool IsNothrow = false;
10744 bool IsPlacement = false;
10745
10746 if (E->getNumPlacementArgs() == 1 &&
10747 E->getPlacementArg(I: 0)->getType()->isNothrowT()) {
10748 // The only new-placement list we support is of the form (std::nothrow).
10749 //
10750 // FIXME: There is no restriction on this, but it's not clear that any
10751 // other form makes any sense. We get here for cases such as:
10752 //
10753 // new (std::align_val_t{N}) X(int)
10754 //
10755 // (which should presumably be valid only if N is a multiple of
10756 // alignof(int), and in any case can't be deallocated unless N is
10757 // alignof(X) and X has new-extended alignment).
10758 LValue Nothrow;
10759 if (!EvaluateLValue(E: E->getPlacementArg(I: 0), Result&: Nothrow, Info))
10760 return false;
10761 IsNothrow = true;
10762 } else if (OperatorNew->isReservedGlobalPlacementOperator()) {
10763 if (Info.CurrentCall->isStdFunction() || Info.getLangOpts().CPlusPlus26 ||
10764 (Info.CurrentCall->CanEvalMSConstexpr &&
10765 OperatorNew->hasAttr<MSConstexprAttr>())) {
10766 if (!EvaluatePointer(E: E->getPlacementArg(I: 0), Result, Info))
10767 return false;
10768 if (Result.Designator.Invalid)
10769 return false;
10770 TargetType = E->getPlacementArg(I: 0)->getType();
10771 IsPlacement = true;
10772 } else {
10773 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
10774 << /*C++26 feature*/ 1 << E->getSourceRange();
10775 return false;
10776 }
10777 } else if (E->getNumPlacementArgs()) {
10778 Info.FFDiag(E, DiagId: diag::note_constexpr_new_placement)
10779 << /*Unsupported*/ 0 << E->getSourceRange();
10780 return false;
10781 } else if (!OperatorNew
10782 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
10783 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
10784 << isa<CXXMethodDecl>(Val: OperatorNew) << OperatorNew;
10785 return false;
10786 }
10787
10788 const Expr *Init = E->getInitializer();
10789 const InitListExpr *ResizedArrayILE = nullptr;
10790 const CXXConstructExpr *ResizedArrayCCE = nullptr;
10791 bool ValueInit = false;
10792
10793 if (std::optional<const Expr *> ArraySize = E->getArraySize()) {
10794 const Expr *Stripped = *ArraySize;
10795 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Val: Stripped);
10796 Stripped = ICE->getSubExpr())
10797 if (ICE->getCastKind() != CK_NoOp &&
10798 ICE->getCastKind() != CK_IntegralCast)
10799 break;
10800
10801 llvm::APSInt ArrayBound;
10802 if (!EvaluateInteger(E: Stripped, Result&: ArrayBound, Info))
10803 return false;
10804
10805 // C++ [expr.new]p9:
10806 // The expression is erroneous if:
10807 // -- [...] its value before converting to size_t [or] applying the
10808 // second standard conversion sequence is less than zero
10809 if (ArrayBound.isSigned() && ArrayBound.isNegative()) {
10810 if (IsNothrow)
10811 return ZeroInitialization(E);
10812
10813 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_negative)
10814 << ArrayBound << (*ArraySize)->getSourceRange();
10815 return false;
10816 }
10817
10818 // -- its value is such that the size of the allocated object would
10819 // exceed the implementation-defined limit
10820 if (!Info.CheckArraySize(Loc: ArraySize.value()->getExprLoc(),
10821 BitWidth: ConstantArrayType::getNumAddressingBits(
10822 Context: Info.Ctx, ElementType: AllocType, NumElements: ArrayBound),
10823 ElemCount: ArrayBound.getZExtValue(), /*Diag=*/!IsNothrow)) {
10824 if (IsNothrow)
10825 return ZeroInitialization(E);
10826 return false;
10827 }
10828
10829 // -- the new-initializer is a braced-init-list and the number of
10830 // array elements for which initializers are provided [...]
10831 // exceeds the number of elements to initialize
10832 if (!Init) {
10833 // No initialization is performed.
10834 } else if (isa<CXXScalarValueInitExpr>(Val: Init) ||
10835 isa<ImplicitValueInitExpr>(Val: Init)) {
10836 ValueInit = true;
10837 } else if (auto *CCE = dyn_cast<CXXConstructExpr>(Val: Init)) {
10838 ResizedArrayCCE = CCE;
10839 } else {
10840 auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType());
10841 assert(CAT && "unexpected type for array initializer");
10842
10843 unsigned Bits =
10844 std::max(a: CAT->getSizeBitWidth(), b: ArrayBound.getBitWidth());
10845 llvm::APInt InitBound = CAT->getSize().zext(width: Bits);
10846 llvm::APInt AllocBound = ArrayBound.zext(width: Bits);
10847 if (InitBound.ugt(RHS: AllocBound)) {
10848 if (IsNothrow)
10849 return ZeroInitialization(E);
10850
10851 Info.FFDiag(E: *ArraySize, DiagId: diag::note_constexpr_new_too_small)
10852 << toString(I: AllocBound, Radix: 10, /*Signed=*/false)
10853 << toString(I: InitBound, Radix: 10, /*Signed=*/false)
10854 << (*ArraySize)->getSourceRange();
10855 return false;
10856 }
10857
10858 // If the sizes differ, we must have an initializer list, and we need
10859 // special handling for this case when we initialize.
10860 if (InitBound != AllocBound)
10861 ResizedArrayILE = cast<InitListExpr>(Val: Init);
10862 }
10863
10864 AllocType = Info.Ctx.getConstantArrayType(EltTy: AllocType, ArySize: ArrayBound, SizeExpr: nullptr,
10865 ASM: ArraySizeModifier::Normal, IndexTypeQuals: 0);
10866 } else {
10867 assert(!AllocType->isArrayType() &&
10868 "array allocation with non-array new");
10869 }
10870
10871 APValue *Val;
10872 if (IsPlacement) {
10873 AccessKinds AK = AK_Construct;
10874 struct FindObjectHandler {
10875 EvalInfo &Info;
10876 const Expr *E;
10877 QualType AllocType;
10878 const AccessKinds AccessKind;
10879 APValue *Value;
10880
10881 typedef bool result_type;
10882 bool failed() { return false; }
10883 bool checkConst(QualType QT) {
10884 if (QT.isConstQualified()) {
10885 Info.FFDiag(E, DiagId: diag::note_constexpr_modify_const_type) << QT;
10886 return false;
10887 }
10888 return true;
10889 }
10890 bool found(APValue &Subobj, QualType SubobjType,
10891 APValue::LValueBase Base) {
10892 if (!checkConst(QT: SubobjType))
10893 return false;
10894 // FIXME: Reject the cases where [basic.life]p8 would not permit the
10895 // old name of the object to be used to name the new object.
10896 unsigned SubobjectSize = 1;
10897 unsigned AllocSize = 1;
10898 if (auto *CAT = dyn_cast<ConstantArrayType>(Val&: AllocType))
10899 AllocSize = CAT->getZExtSize();
10900 if (auto *CAT = dyn_cast<ConstantArrayType>(Val&: SubobjType))
10901 SubobjectSize = CAT->getZExtSize();
10902 if (SubobjectSize < AllocSize ||
10903 !Info.Ctx.hasSimilarType(T1: Info.Ctx.getBaseElementType(QT: SubobjType),
10904 T2: Info.Ctx.getBaseElementType(QT: AllocType))) {
10905 Info.FFDiag(E, DiagId: diag::note_constexpr_placement_new_wrong_type)
10906 << SubobjType << AllocType;
10907 return false;
10908 }
10909 Value = &Subobj;
10910 return true;
10911 }
10912 bool found(APSInt &Value, QualType SubobjType) {
10913 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
10914 return false;
10915 }
10916 bool found(APFloat &Value, QualType SubobjType) {
10917 Info.FFDiag(E, DiagId: diag::note_constexpr_construct_complex_elem);
10918 return false;
10919 }
10920 } Handler = {.Info: Info, .E: E, .AllocType: AllocType, .AccessKind: AK, .Value: nullptr};
10921
10922 CompleteObject Obj = findCompleteObject(Info, E, AK, LVal: Result, LValType: AllocType);
10923 if (!Obj || !findSubobject(Info, E, Obj, Sub: Result.Designator, handler&: Handler))
10924 return false;
10925
10926 Val = Handler.Value;
10927
10928 // [basic.life]p1:
10929 // The lifetime of an object o of type T ends when [...] the storage
10930 // which the object occupies is [...] reused by an object that is not
10931 // nested within o (6.6.2).
10932 *Val = APValue();
10933 } else {
10934 // Perform the allocation and obtain a pointer to the resulting object.
10935 Val = Info.createHeapAlloc(E, T: AllocType, LV&: Result);
10936 if (!Val)
10937 return false;
10938 }
10939
10940 if (ValueInit) {
10941 ImplicitValueInitExpr VIE(AllocType);
10942 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: &VIE))
10943 return false;
10944 } else if (ResizedArrayILE) {
10945 if (!EvaluateArrayNewInitList(Info, This&: Result, Result&: *Val, ILE: ResizedArrayILE,
10946 AllocType))
10947 return false;
10948 } else if (ResizedArrayCCE) {
10949 if (!EvaluateArrayNewConstructExpr(Info, This&: Result, Result&: *Val, CCE: ResizedArrayCCE,
10950 AllocType))
10951 return false;
10952 } else if (Init) {
10953 if (!EvaluateInPlace(Result&: *Val, Info, This: Result, E: Init))
10954 return false;
10955 } else if (!handleDefaultInitValue(T: AllocType, Result&: *Val)) {
10956 return false;
10957 }
10958
10959 // Array new returns a pointer to the first element, not a pointer to the
10960 // array.
10961 if (auto *AT = AllocType->getAsArrayTypeUnsafe())
10962 Result.addArray(Info, E, CAT: cast<ConstantArrayType>(Val: AT));
10963
10964 return true;
10965}
10966//===----------------------------------------------------------------------===//
10967// Member Pointer Evaluation
10968//===----------------------------------------------------------------------===//
10969
10970namespace {
10971class MemberPointerExprEvaluator
10972 : public ExprEvaluatorBase<MemberPointerExprEvaluator> {
10973 MemberPtr &Result;
10974
10975 bool Success(const ValueDecl *D) {
10976 Result = MemberPtr(D);
10977 return true;
10978 }
10979public:
10980
10981 MemberPointerExprEvaluator(EvalInfo &Info, MemberPtr &Result)
10982 : ExprEvaluatorBaseTy(Info), Result(Result) {}
10983
10984 bool Success(const APValue &V, const Expr *E) {
10985 Result.setFrom(V);
10986 return true;
10987 }
10988 bool ZeroInitialization(const Expr *E) {
10989 return Success(D: (const ValueDecl*)nullptr);
10990 }
10991
10992 bool VisitCastExpr(const CastExpr *E);
10993 bool VisitUnaryAddrOf(const UnaryOperator *E);
10994};
10995} // end anonymous namespace
10996
10997static bool EvaluateMemberPointer(const Expr *E, MemberPtr &Result,
10998 EvalInfo &Info) {
10999 assert(!E->isValueDependent());
11000 assert(E->isPRValue() && E->getType()->isMemberPointerType());
11001 return MemberPointerExprEvaluator(Info, Result).Visit(S: E);
11002}
11003
11004bool MemberPointerExprEvaluator::VisitCastExpr(const CastExpr *E) {
11005 switch (E->getCastKind()) {
11006 default:
11007 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11008
11009 case CK_NullToMemberPointer:
11010 VisitIgnoredValue(E: E->getSubExpr());
11011 return ZeroInitialization(E);
11012
11013 case CK_BaseToDerivedMemberPointer: {
11014 if (!Visit(S: E->getSubExpr()))
11015 return false;
11016 if (E->path_empty())
11017 return true;
11018 // Base-to-derived member pointer casts store the path in derived-to-base
11019 // order, so iterate backwards. The CXXBaseSpecifier also provides us with
11020 // the wrong end of the derived->base arc, so stagger the path by one class.
11021 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
11022 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
11023 PathI != PathE; ++PathI) {
11024 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11025 const CXXRecordDecl *Derived = (*PathI)->getType()->getAsCXXRecordDecl();
11026 if (!Result.castToDerived(Derived))
11027 return Error(E);
11028 }
11029 if (!Result.castToDerived(Derived: E->getType()
11030 ->castAs<MemberPointerType>()
11031 ->getMostRecentCXXRecordDecl()))
11032 return Error(E);
11033 return true;
11034 }
11035
11036 case CK_DerivedToBaseMemberPointer:
11037 if (!Visit(S: E->getSubExpr()))
11038 return false;
11039 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11040 PathE = E->path_end(); PathI != PathE; ++PathI) {
11041 assert(!(*PathI)->isVirtual() && "memptr cast through vbase");
11042 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11043 if (!Result.castToBase(Base))
11044 return Error(E);
11045 }
11046 return true;
11047 }
11048}
11049
11050bool MemberPointerExprEvaluator::VisitUnaryAddrOf(const UnaryOperator *E) {
11051 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
11052 // member can be formed.
11053 return Success(D: cast<DeclRefExpr>(Val: E->getSubExpr())->getDecl());
11054}
11055
11056//===----------------------------------------------------------------------===//
11057// Record Evaluation
11058//===----------------------------------------------------------------------===//
11059
11060namespace {
11061 class RecordExprEvaluator
11062 : public ExprEvaluatorBase<RecordExprEvaluator> {
11063 const LValue &This;
11064 APValue &Result;
11065 public:
11066
11067 RecordExprEvaluator(EvalInfo &info, const LValue &This, APValue &Result)
11068 : ExprEvaluatorBaseTy(info), This(This), Result(Result) {}
11069
11070 bool Success(const APValue &V, const Expr *E) {
11071 Result = V;
11072 return true;
11073 }
11074 bool ZeroInitialization(const Expr *E) {
11075 return ZeroInitialization(E, T: E->getType());
11076 }
11077 bool ZeroInitialization(const Expr *E, QualType T);
11078
11079 bool VisitCallExpr(const CallExpr *E) {
11080 return handleCallExpr(E, Result, ResultSlot: &This);
11081 }
11082 bool VisitCastExpr(const CastExpr *E);
11083 bool VisitInitListExpr(const InitListExpr *E);
11084 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11085 return VisitCXXConstructExpr(E, T: E->getType());
11086 }
11087 bool VisitLambdaExpr(const LambdaExpr *E);
11088 bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E);
11089 bool VisitCXXConstructExpr(const CXXConstructExpr *E, QualType T);
11090 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E);
11091 bool VisitBinCmp(const BinaryOperator *E);
11092 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
11093 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
11094 ArrayRef<Expr *> Args);
11095 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
11096 };
11097}
11098
11099/// Perform zero-initialization on an object of non-union class type.
11100/// C++11 [dcl.init]p5:
11101/// To zero-initialize an object or reference of type T means:
11102/// [...]
11103/// -- if T is a (possibly cv-qualified) non-union class type,
11104/// each non-static data member and each base-class subobject is
11105/// zero-initialized
11106static bool HandleClassZeroInitialization(EvalInfo &Info, const Expr *E,
11107 const RecordDecl *RD,
11108 const LValue &This, APValue &Result) {
11109 assert(!RD->isUnion() && "Expected non-union class type");
11110 const CXXRecordDecl *CD = dyn_cast<CXXRecordDecl>(Val: RD);
11111 Result = APValue(APValue::UninitStruct(), CD ? CD->getNumBases() : 0,
11112 RD->getNumFields());
11113
11114 if (RD->isInvalidDecl()) return false;
11115 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11116
11117 if (CD) {
11118 unsigned Index = 0;
11119 for (CXXRecordDecl::base_class_const_iterator I = CD->bases_begin(),
11120 End = CD->bases_end(); I != End; ++I, ++Index) {
11121 const CXXRecordDecl *Base = I->getType()->getAsCXXRecordDecl();
11122 LValue Subobject = This;
11123 if (!HandleLValueDirectBase(Info, E, Obj&: Subobject, Derived: CD, Base, RL: &Layout))
11124 return false;
11125 if (!HandleClassZeroInitialization(Info, E, RD: Base, This: Subobject,
11126 Result&: Result.getStructBase(i: Index)))
11127 return false;
11128 }
11129 }
11130
11131 for (const auto *I : RD->fields()) {
11132 // -- if T is a reference type, no initialization is performed.
11133 if (I->isUnnamedBitField() || I->getType()->isReferenceType())
11134 continue;
11135
11136 LValue Subobject = This;
11137 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: I, RL: &Layout))
11138 return false;
11139
11140 ImplicitValueInitExpr VIE(I->getType());
11141 if (!EvaluateInPlace(
11142 Result&: Result.getStructField(i: I->getFieldIndex()), Info, This: Subobject, E: &VIE))
11143 return false;
11144 }
11145
11146 return true;
11147}
11148
11149bool RecordExprEvaluator::ZeroInitialization(const Expr *E, QualType T) {
11150 const auto *RD = T->castAsRecordDecl();
11151 if (RD->isInvalidDecl()) return false;
11152 if (RD->isUnion()) {
11153 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
11154 // object's first non-static named data member is zero-initialized
11155 RecordDecl::field_iterator I = RD->field_begin();
11156 while (I != RD->field_end() && (*I)->isUnnamedBitField())
11157 ++I;
11158 if (I == RD->field_end()) {
11159 Result = APValue((const FieldDecl*)nullptr);
11160 return true;
11161 }
11162
11163 LValue Subobject = This;
11164 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: *I))
11165 return false;
11166 Result = APValue(*I);
11167 ImplicitValueInitExpr VIE(I->getType());
11168 return EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: &VIE);
11169 }
11170
11171 if (isa<CXXRecordDecl>(Val: RD) && cast<CXXRecordDecl>(Val: RD)->getNumVBases()) {
11172 Info.FFDiag(E, DiagId: diag::note_constexpr_virtual_base) << RD;
11173 return false;
11174 }
11175
11176 return HandleClassZeroInitialization(Info, E, RD, This, Result);
11177}
11178
11179bool RecordExprEvaluator::VisitCastExpr(const CastExpr *E) {
11180 switch (E->getCastKind()) {
11181 default:
11182 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11183
11184 case CK_ConstructorConversion:
11185 return Visit(S: E->getSubExpr());
11186
11187 case CK_DerivedToBase:
11188 case CK_UncheckedDerivedToBase: {
11189 APValue DerivedObject;
11190 if (!Evaluate(Result&: DerivedObject, Info, E: E->getSubExpr()))
11191 return false;
11192 if (!DerivedObject.isStruct())
11193 return Error(E: E->getSubExpr());
11194
11195 // Derived-to-base rvalue conversion: just slice off the derived part.
11196 APValue *Value = &DerivedObject;
11197 const CXXRecordDecl *RD = E->getSubExpr()->getType()->getAsCXXRecordDecl();
11198 for (CastExpr::path_const_iterator PathI = E->path_begin(),
11199 PathE = E->path_end(); PathI != PathE; ++PathI) {
11200 assert(!(*PathI)->isVirtual() && "record rvalue with virtual base");
11201 const CXXRecordDecl *Base = (*PathI)->getType()->getAsCXXRecordDecl();
11202 Value = &Value->getStructBase(i: getBaseIndex(Derived: RD, Base));
11203 RD = Base;
11204 }
11205 Result = *Value;
11206 return true;
11207 }
11208 case CK_HLSLAggregateSplatCast: {
11209 APValue Val;
11210 QualType ValTy;
11211
11212 if (!hlslAggSplatHelper(Info, E: E->getSubExpr(), SrcVal&: Val, SrcTy&: ValTy))
11213 return false;
11214
11215 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
11216 // splat our Val
11217 SmallVector<APValue> SplatEls(NEls, Val);
11218 SmallVector<QualType> SplatType(NEls, ValTy);
11219
11220 // cast the elements and construct our struct result
11221 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11222 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
11223 ElTypes&: SplatType))
11224 return false;
11225
11226 return true;
11227 }
11228 case CK_HLSLElementwiseCast: {
11229 SmallVector<APValue> SrcEls;
11230 SmallVector<QualType> SrcTypes;
11231
11232 if (!hlslElementwiseCastHelper(Info, E: E->getSubExpr(), DestTy: E->getType(), SrcVals&: SrcEls,
11233 SrcTypes))
11234 return false;
11235
11236 // cast the elements and construct our struct result
11237 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11238 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
11239 ElTypes&: SrcTypes))
11240 return false;
11241
11242 return true;
11243 }
11244 case CK_ToUnion: {
11245 const FieldDecl *Field = E->getTargetUnionField();
11246 LValue Subobject = This;
11247 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field))
11248 return false;
11249 Result = APValue(Field);
11250 if (!EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject,
11251 E: E->getSubExpr()))
11252 return false;
11253 if (Field->isBitField()) {
11254 if (!truncateBitfieldValue(Info, E: E->getSubExpr(), Value&: Result.getUnionValue(),
11255 FD: Field))
11256 return false;
11257 }
11258 return true;
11259 }
11260 }
11261}
11262
11263bool RecordExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
11264 if (E->isTransparent())
11265 return Visit(S: E->getInit(Init: 0));
11266 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits());
11267}
11268
11269bool RecordExprEvaluator::VisitCXXParenListOrInitListExpr(
11270 const Expr *ExprToVisit, ArrayRef<Expr *> Args) {
11271 const auto *RD = ExprToVisit->getType()->castAsRecordDecl();
11272 if (RD->isInvalidDecl()) return false;
11273 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: RD);
11274 auto *CXXRD = dyn_cast<CXXRecordDecl>(Val: RD);
11275
11276 EvalInfo::EvaluatingConstructorRAII EvalObj(
11277 Info,
11278 ObjectUnderConstruction{.Base: This.getLValueBase(), .Path: This.Designator.Entries},
11279 CXXRD && CXXRD->getNumBases());
11280
11281 if (RD->isUnion()) {
11282 const FieldDecl *Field;
11283 if (auto *ILE = dyn_cast<InitListExpr>(Val: ExprToVisit)) {
11284 Field = ILE->getInitializedFieldInUnion();
11285 } else if (auto *PLIE = dyn_cast<CXXParenListInitExpr>(Val: ExprToVisit)) {
11286 Field = PLIE->getInitializedFieldInUnion();
11287 } else {
11288 llvm_unreachable(
11289 "Expression is neither an init list nor a C++ paren list");
11290 }
11291
11292 Result = APValue(Field);
11293 if (!Field)
11294 return true;
11295
11296 // If the initializer list for a union does not contain any elements, the
11297 // first element of the union is value-initialized.
11298 // FIXME: The element should be initialized from an initializer list.
11299 // Is this difference ever observable for initializer lists which
11300 // we don't build?
11301 ImplicitValueInitExpr VIE(Field->getType());
11302 const Expr *InitExpr = Args.empty() ? &VIE : Args[0];
11303
11304 LValue Subobject = This;
11305 if (!HandleLValueMember(Info, E: InitExpr, LVal&: Subobject, FD: Field, RL: &Layout))
11306 return false;
11307
11308 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11309 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11310 isa<CXXDefaultInitExpr>(Val: InitExpr));
11311
11312 if (EvaluateInPlace(Result&: Result.getUnionValue(), Info, This: Subobject, E: InitExpr)) {
11313 if (Field->isBitField())
11314 return truncateBitfieldValue(Info, E: InitExpr, Value&: Result.getUnionValue(),
11315 FD: Field);
11316 return true;
11317 }
11318
11319 return false;
11320 }
11321
11322 if (!Result.hasValue())
11323 Result = APValue(APValue::UninitStruct(), CXXRD ? CXXRD->getNumBases() : 0,
11324 RD->getNumFields());
11325 unsigned ElementNo = 0;
11326 bool Success = true;
11327
11328 // Initialize base classes.
11329 if (CXXRD && CXXRD->getNumBases()) {
11330 for (const auto &Base : CXXRD->bases()) {
11331 assert(ElementNo < Args.size() && "missing init for base class");
11332 const Expr *Init = Args[ElementNo];
11333
11334 LValue Subobject = This;
11335 if (!HandleLValueBase(Info, E: Init, Obj&: Subobject, DerivedDecl: CXXRD, Base: &Base))
11336 return false;
11337
11338 APValue &FieldVal = Result.getStructBase(i: ElementNo);
11339 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init)) {
11340 if (!Info.noteFailure())
11341 return false;
11342 Success = false;
11343 }
11344 ++ElementNo;
11345 }
11346
11347 EvalObj.finishedConstructingBases();
11348 }
11349
11350 // Initialize members.
11351 for (const auto *Field : RD->fields()) {
11352 // Anonymous bit-fields are not considered members of the class for
11353 // purposes of aggregate initialization.
11354 if (Field->isUnnamedBitField())
11355 continue;
11356
11357 LValue Subobject = This;
11358
11359 bool HaveInit = ElementNo < Args.size();
11360
11361 // FIXME: Diagnostics here should point to the end of the initializer
11362 // list, not the start.
11363 if (!HandleLValueMember(Info, E: HaveInit ? Args[ElementNo] : ExprToVisit,
11364 LVal&: Subobject, FD: Field, RL: &Layout))
11365 return false;
11366
11367 // Perform an implicit value-initialization for members beyond the end of
11368 // the initializer list.
11369 ImplicitValueInitExpr VIE(HaveInit ? Info.Ctx.IntTy : Field->getType());
11370 const Expr *Init = HaveInit ? Args[ElementNo++] : &VIE;
11371
11372 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
11373 // aren't supposed to be modified.
11374 if (isa<NoInitExpr>(Val: Init))
11375 continue;
11376
11377 if (Field->getType()->isIncompleteArrayType()) {
11378 if (auto *CAT = Info.Ctx.getAsConstantArrayType(T: Init->getType())) {
11379 if (!CAT->isZeroSize()) {
11380 // Bail out for now. This might sort of "work", but the rest of the
11381 // code isn't really prepared to handle it.
11382 Info.FFDiag(E: Init, DiagId: diag::note_constexpr_unsupported_flexible_array);
11383 return false;
11384 }
11385 }
11386 }
11387
11388 // Temporarily override This, in case there's a CXXDefaultInitExpr in here.
11389 ThisOverrideRAII ThisOverride(*Info.CurrentCall, &This,
11390 isa<CXXDefaultInitExpr>(Val: Init));
11391
11392 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11393 if (Field->getType()->isReferenceType()) {
11394 LValue Result;
11395 if (!EvaluateInitForDeclOfReferenceType(Info, D: Field, Init, Result,
11396 Val&: FieldVal)) {
11397 if (!Info.noteFailure())
11398 return false;
11399 Success = false;
11400 }
11401 } else if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: Init) ||
11402 (Field->isBitField() &&
11403 !truncateBitfieldValue(Info, E: Init, Value&: FieldVal, FD: Field))) {
11404 if (!Info.noteFailure())
11405 return false;
11406 Success = false;
11407 }
11408 }
11409
11410 EvalObj.finishedConstructingFields();
11411
11412 return Success;
11413}
11414
11415bool RecordExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
11416 QualType T) {
11417 // Note that E's type is not necessarily the type of our class here; we might
11418 // be initializing an array element instead.
11419 const CXXConstructorDecl *FD = E->getConstructor();
11420 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl()) return false;
11421
11422 bool ZeroInit = E->requiresZeroInitialization();
11423 if (CheckTrivialDefaultConstructor(Info, Loc: E->getExprLoc(), CD: FD, IsValueInitialization: ZeroInit)) {
11424 if (ZeroInit)
11425 return ZeroInitialization(E, T);
11426
11427 return handleDefaultInitValue(T, Result);
11428 }
11429
11430 const FunctionDecl *Definition = nullptr;
11431 auto Body = FD->getBody(Definition);
11432
11433 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11434 return false;
11435
11436 // Avoid materializing a temporary for an elidable copy/move constructor.
11437 if (E->isElidable() && !ZeroInit) {
11438 // FIXME: This only handles the simplest case, where the source object
11439 // is passed directly as the first argument to the constructor.
11440 // This should also handle stepping though implicit casts and
11441 // and conversion sequences which involve two steps, with a
11442 // conversion operator followed by a converting constructor.
11443 const Expr *SrcObj = E->getArg(Arg: 0);
11444 assert(SrcObj->isTemporaryObject(Info.Ctx, FD->getParent()));
11445 assert(Info.Ctx.hasSameUnqualifiedType(E->getType(), SrcObj->getType()));
11446 if (const MaterializeTemporaryExpr *ME =
11447 dyn_cast<MaterializeTemporaryExpr>(Val: SrcObj))
11448 return Visit(S: ME->getSubExpr());
11449 }
11450
11451 if (ZeroInit && !ZeroInitialization(E, T))
11452 return false;
11453
11454 auto Args = ArrayRef(E->getArgs(), E->getNumArgs());
11455 return HandleConstructorCall(E, This, Args,
11456 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11457 Result);
11458}
11459
11460bool RecordExprEvaluator::VisitCXXInheritedCtorInitExpr(
11461 const CXXInheritedCtorInitExpr *E) {
11462 if (!Info.CurrentCall) {
11463 assert(Info.checkingPotentialConstantExpression());
11464 return false;
11465 }
11466
11467 const CXXConstructorDecl *FD = E->getConstructor();
11468 if (FD->isInvalidDecl() || FD->getParent()->isInvalidDecl())
11469 return false;
11470
11471 const FunctionDecl *Definition = nullptr;
11472 auto Body = FD->getBody(Definition);
11473
11474 if (!CheckConstexprFunction(Info, CallLoc: E->getExprLoc(), Declaration: FD, Definition, Body))
11475 return false;
11476
11477 return HandleConstructorCall(E, This, Call: Info.CurrentCall->Arguments,
11478 Definition: cast<CXXConstructorDecl>(Val: Definition), Info,
11479 Result);
11480}
11481
11482bool RecordExprEvaluator::VisitCXXStdInitializerListExpr(
11483 const CXXStdInitializerListExpr *E) {
11484 const ConstantArrayType *ArrayType =
11485 Info.Ctx.getAsConstantArrayType(T: E->getSubExpr()->getType());
11486
11487 LValue Array;
11488 if (!EvaluateLValue(E: E->getSubExpr(), Result&: Array, Info))
11489 return false;
11490
11491 assert(ArrayType && "unexpected type for array initializer");
11492
11493 // Get a pointer to the first element of the array.
11494 Array.addArray(Info, E, CAT: ArrayType);
11495
11496 // FIXME: What if the initializer_list type has base classes, etc?
11497 Result = APValue(APValue::UninitStruct(), 0, 2);
11498 Array.moveInto(V&: Result.getStructField(i: 0));
11499
11500 auto *Record = E->getType()->castAsRecordDecl();
11501 RecordDecl::field_iterator Field = Record->field_begin();
11502 assert(Field != Record->field_end() &&
11503 Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11504 ArrayType->getElementType()) &&
11505 "Expected std::initializer_list first field to be const E *");
11506 ++Field;
11507 assert(Field != Record->field_end() &&
11508 "Expected std::initializer_list to have two fields");
11509
11510 if (Info.Ctx.hasSameType(T1: Field->getType(), T2: Info.Ctx.getSizeType())) {
11511 // Length.
11512 Result.getStructField(i: 1) = APValue(APSInt(ArrayType->getSize()));
11513 } else {
11514 // End pointer.
11515 assert(Info.Ctx.hasSameType(Field->getType()->getPointeeType(),
11516 ArrayType->getElementType()) &&
11517 "Expected std::initializer_list second field to be const E *");
11518 if (!HandleLValueArrayAdjustment(Info, E, LVal&: Array,
11519 EltTy: ArrayType->getElementType(),
11520 Adjustment: ArrayType->getZExtSize()))
11521 return false;
11522 Array.moveInto(V&: Result.getStructField(i: 1));
11523 }
11524
11525 assert(++Field == Record->field_end() &&
11526 "Expected std::initializer_list to only have two fields");
11527
11528 return true;
11529}
11530
11531bool RecordExprEvaluator::VisitLambdaExpr(const LambdaExpr *E) {
11532 const CXXRecordDecl *ClosureClass = E->getLambdaClass();
11533 if (ClosureClass->isInvalidDecl())
11534 return false;
11535
11536 const size_t NumFields = ClosureClass->getNumFields();
11537
11538 assert(NumFields == (size_t)std::distance(E->capture_init_begin(),
11539 E->capture_init_end()) &&
11540 "The number of lambda capture initializers should equal the number of "
11541 "fields within the closure type");
11542
11543 Result = APValue(APValue::UninitStruct(), /*NumBases*/0, NumFields);
11544 // Iterate through all the lambda's closure object's fields and initialize
11545 // them.
11546 auto *CaptureInitIt = E->capture_init_begin();
11547 bool Success = true;
11548 const ASTRecordLayout &Layout = Info.Ctx.getASTRecordLayout(D: ClosureClass);
11549 for (const auto *Field : ClosureClass->fields()) {
11550 assert(CaptureInitIt != E->capture_init_end());
11551 // Get the initializer for this field
11552 Expr *const CurFieldInit = *CaptureInitIt++;
11553
11554 // If there is no initializer, either this is a VLA or an error has
11555 // occurred.
11556 if (!CurFieldInit || CurFieldInit->containsErrors())
11557 return Error(E);
11558
11559 LValue Subobject = This;
11560
11561 if (!HandleLValueMember(Info, E, LVal&: Subobject, FD: Field, RL: &Layout))
11562 return false;
11563
11564 APValue &FieldVal = Result.getStructField(i: Field->getFieldIndex());
11565 if (!EvaluateInPlace(Result&: FieldVal, Info, This: Subobject, E: CurFieldInit)) {
11566 if (!Info.keepEvaluatingAfterFailure())
11567 return false;
11568 Success = false;
11569 }
11570 }
11571 return Success;
11572}
11573
11574bool RecordExprEvaluator::VisitDesignatedInitUpdateExpr(
11575 const DesignatedInitUpdateExpr *E) {
11576 if (!Visit(S: E->getBase()))
11577 return false;
11578 return Visit(S: E->getUpdater());
11579}
11580
11581static bool EvaluateRecord(const Expr *E, const LValue &This,
11582 APValue &Result, EvalInfo &Info) {
11583 assert(!E->isValueDependent());
11584 assert(E->isPRValue() && E->getType()->isRecordType() &&
11585 "can't evaluate expression as a record rvalue");
11586 return RecordExprEvaluator(Info, This, Result).Visit(S: E);
11587}
11588
11589//===----------------------------------------------------------------------===//
11590// Temporary Evaluation
11591//
11592// Temporaries are represented in the AST as rvalues, but generally behave like
11593// lvalues. The full-object of which the temporary is a subobject is implicitly
11594// materialized so that a reference can bind to it.
11595//===----------------------------------------------------------------------===//
11596namespace {
11597class TemporaryExprEvaluator
11598 : public LValueExprEvaluatorBase<TemporaryExprEvaluator> {
11599public:
11600 TemporaryExprEvaluator(EvalInfo &Info, LValue &Result) :
11601 LValueExprEvaluatorBaseTy(Info, Result, false) {}
11602
11603 /// Visit an expression which constructs the value of this temporary.
11604 bool VisitConstructExpr(const Expr *E) {
11605 APValue &Value = Info.CurrentCall->createTemporary(
11606 Key: E, T: E->getType(), Scope: ScopeKind::FullExpression, LV&: Result);
11607 return EvaluateInPlace(Result&: Value, Info, This: Result, E);
11608 }
11609
11610 bool VisitCastExpr(const CastExpr *E) {
11611 switch (E->getCastKind()) {
11612 default:
11613 return LValueExprEvaluatorBaseTy::VisitCastExpr(E);
11614
11615 case CK_ConstructorConversion:
11616 return VisitConstructExpr(E: E->getSubExpr());
11617 }
11618 }
11619 bool VisitInitListExpr(const InitListExpr *E) {
11620 return VisitConstructExpr(E);
11621 }
11622 bool VisitCXXConstructExpr(const CXXConstructExpr *E) {
11623 return VisitConstructExpr(E);
11624 }
11625 bool VisitCallExpr(const CallExpr *E) {
11626 return VisitConstructExpr(E);
11627 }
11628 bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E) {
11629 return VisitConstructExpr(E);
11630 }
11631 bool VisitLambdaExpr(const LambdaExpr *E) {
11632 return VisitConstructExpr(E);
11633 }
11634};
11635} // end anonymous namespace
11636
11637/// Evaluate an expression of record type as a temporary.
11638static bool EvaluateTemporary(const Expr *E, LValue &Result, EvalInfo &Info) {
11639 assert(!E->isValueDependent());
11640 assert(E->isPRValue() && E->getType()->isRecordType());
11641 return TemporaryExprEvaluator(Info, Result).Visit(S: E);
11642}
11643
11644//===----------------------------------------------------------------------===//
11645// Vector Evaluation
11646//===----------------------------------------------------------------------===//
11647
11648namespace {
11649 class VectorExprEvaluator
11650 : public ExprEvaluatorBase<VectorExprEvaluator> {
11651 APValue &Result;
11652 public:
11653
11654 VectorExprEvaluator(EvalInfo &info, APValue &Result)
11655 : ExprEvaluatorBaseTy(info), Result(Result) {}
11656
11657 bool Success(ArrayRef<APValue> V, const Expr *E) {
11658 assert(V.size() == E->getType()->castAs<VectorType>()->getNumElements());
11659 // FIXME: remove this APValue copy.
11660 Result = APValue(V.data(), V.size());
11661 return true;
11662 }
11663 bool Success(const APValue &V, const Expr *E) {
11664 assert(V.isVector());
11665 Result = V;
11666 return true;
11667 }
11668 bool ZeroInitialization(const Expr *E);
11669
11670 bool VisitUnaryReal(const UnaryOperator *E)
11671 { return Visit(S: E->getSubExpr()); }
11672 bool VisitCastExpr(const CastExpr* E);
11673 bool VisitInitListExpr(const InitListExpr *E);
11674 bool VisitUnaryImag(const UnaryOperator *E);
11675 bool VisitBinaryOperator(const BinaryOperator *E);
11676 bool VisitUnaryOperator(const UnaryOperator *E);
11677 bool VisitCallExpr(const CallExpr *E);
11678 bool VisitConvertVectorExpr(const ConvertVectorExpr *E);
11679 bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E);
11680
11681 // FIXME: Missing: conditional operator (for GNU
11682 // conditional select), ExtVectorElementExpr
11683 };
11684} // end anonymous namespace
11685
11686static bool EvaluateVector(const Expr* E, APValue& Result, EvalInfo &Info) {
11687 assert(E->isPRValue() && E->getType()->isVectorType() &&
11688 "not a vector prvalue");
11689 return VectorExprEvaluator(Info, Result).Visit(S: E);
11690}
11691
11692static llvm::APInt ConvertBoolVectorToInt(const APValue &Val) {
11693 assert(Val.isVector() && "expected vector APValue");
11694 unsigned NumElts = Val.getVectorLength();
11695
11696 // Each element is one bit, so create an integer with NumElts bits.
11697 llvm::APInt Result(NumElts, 0);
11698
11699 for (unsigned I = 0; I < NumElts; ++I) {
11700 const APValue &Elt = Val.getVectorElt(I);
11701 assert(Elt.isInt() && "expected integer element in bool vector");
11702
11703 if (Elt.getInt().getBoolValue())
11704 Result.setBit(I);
11705 }
11706
11707 return Result;
11708}
11709
11710bool VectorExprEvaluator::VisitCastExpr(const CastExpr *E) {
11711 const VectorType *VTy = E->getType()->castAs<VectorType>();
11712 unsigned NElts = VTy->getNumElements();
11713
11714 const Expr *SE = E->getSubExpr();
11715 QualType SETy = SE->getType();
11716
11717 switch (E->getCastKind()) {
11718 case CK_VectorSplat: {
11719 APValue Val = APValue();
11720 if (SETy->isIntegerType()) {
11721 APSInt IntResult;
11722 if (!EvaluateInteger(E: SE, Result&: IntResult, Info))
11723 return false;
11724 Val = APValue(std::move(IntResult));
11725 } else if (SETy->isRealFloatingType()) {
11726 APFloat FloatResult(0.0);
11727 if (!EvaluateFloat(E: SE, Result&: FloatResult, Info))
11728 return false;
11729 Val = APValue(std::move(FloatResult));
11730 } else {
11731 return Error(E);
11732 }
11733
11734 // Splat and create vector APValue.
11735 SmallVector<APValue, 4> Elts(NElts, Val);
11736 return Success(V: Elts, E);
11737 }
11738 case CK_BitCast: {
11739 APValue SVal;
11740 if (!Evaluate(Result&: SVal, Info, E: SE))
11741 return false;
11742
11743 if (!SVal.isInt() && !SVal.isFloat() && !SVal.isVector()) {
11744 // Give up if the input isn't an int, float, or vector. For example, we
11745 // reject "(v4i16)(intptr_t)&a".
11746 Info.FFDiag(E, DiagId: diag::note_constexpr_invalid_cast)
11747 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
11748 << Info.Ctx.getLangOpts().CPlusPlus;
11749 return false;
11750 }
11751
11752 if (!handleRValueToRValueBitCast(Info, DestValue&: Result, SourceRValue: SVal, BCE: E))
11753 return false;
11754
11755 return true;
11756 }
11757 case CK_HLSLVectorTruncation: {
11758 APValue Val;
11759 SmallVector<APValue, 4> Elements;
11760 if (!EvaluateVector(E: SE, Result&: Val, Info))
11761 return Error(E);
11762 for (unsigned I = 0; I < NElts; I++)
11763 Elements.push_back(Elt: Val.getVectorElt(I));
11764 return Success(V: Elements, E);
11765 }
11766 case CK_HLSLMatrixTruncation: {
11767 // Matrix truncation occurs in row-major order.
11768 APValue Val;
11769 if (!EvaluateMatrix(E: SE, Result&: Val, Info))
11770 return Error(E);
11771 SmallVector<APValue, 16> Elements;
11772 for (unsigned Row = 0;
11773 Row < Val.getMatrixNumRows() && Elements.size() < NElts; Row++)
11774 for (unsigned Col = 0;
11775 Col < Val.getMatrixNumColumns() && Elements.size() < NElts; Col++)
11776 Elements.push_back(Elt: Val.getMatrixElt(Row, Col));
11777 return Success(V: Elements, E);
11778 }
11779 case CK_HLSLAggregateSplatCast: {
11780 APValue Val;
11781 QualType ValTy;
11782
11783 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
11784 return false;
11785
11786 // cast our Val once.
11787 APValue Result;
11788 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11789 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: VTy->getElementType(), Original: Val,
11790 Result))
11791 return false;
11792
11793 SmallVector<APValue, 4> SplatEls(NElts, Result);
11794 return Success(V: SplatEls, E);
11795 }
11796 case CK_HLSLElementwiseCast: {
11797 SmallVector<APValue> SrcVals;
11798 SmallVector<QualType> SrcTypes;
11799
11800 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
11801 return false;
11802
11803 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11804 SmallVector<QualType, 4> DestTypes(NElts, VTy->getElementType());
11805 SmallVector<APValue, 4> ResultEls(NElts);
11806 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
11807 Results&: ResultEls))
11808 return false;
11809 return Success(V: ResultEls, E);
11810 }
11811 case CK_IntegralToFloating:
11812 case CK_FloatingToIntegral:
11813 case CK_IntegralCast:
11814 case CK_FloatingCast:
11815 case CK_FloatingToBoolean:
11816 case CK_IntegralToBoolean: {
11817 // These casts apply element-wise when the source is a vector type.
11818 assert(SETy->isVectorType() && "expected vector source type");
11819 APValue SrcVal;
11820 if (!EvaluateVector(E: SE, Result&: SrcVal, Info))
11821 return Error(E);
11822
11823 assert(SrcVal.getVectorLength() == NElts);
11824 QualType SrcEltTy = SETy->castAs<VectorType>()->getElementType();
11825 QualType DstEltTy = VTy->getElementType();
11826 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
11827
11828 SmallVector<APValue, 4> ResultEls(NElts);
11829 for (unsigned I = 0; I < NElts; ++I) {
11830 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcEltTy, DestTy: DstEltTy,
11831 Original: SrcVal.getVectorElt(I), Result&: ResultEls[I]))
11832 return Error(E);
11833 }
11834 return Success(V: ResultEls, E);
11835 }
11836 default:
11837 return ExprEvaluatorBaseTy::VisitCastExpr(E);
11838 }
11839}
11840
11841bool
11842VectorExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
11843 const VectorType *VT = E->getType()->castAs<VectorType>();
11844 unsigned NumInits = E->getNumInits();
11845 unsigned NumElements = VT->getNumElements();
11846
11847 QualType EltTy = VT->getElementType();
11848 SmallVector<APValue, 4> Elements;
11849
11850 // MFloat8 type doesn't have constants and thus constant folding
11851 // is impossible.
11852 if (EltTy->isMFloat8Type())
11853 return false;
11854
11855 // The number of initializers can be less than the number of
11856 // vector elements. For OpenCL, this can be due to nested vector
11857 // initialization. For GCC compatibility, missing trailing elements
11858 // should be initialized with zeroes.
11859 unsigned CountInits = 0, CountElts = 0;
11860 while (CountElts < NumElements) {
11861 // Handle nested vector initialization.
11862 if (CountInits < NumInits
11863 && E->getInit(Init: CountInits)->getType()->isVectorType()) {
11864 APValue v;
11865 if (!EvaluateVector(E: E->getInit(Init: CountInits), Result&: v, Info))
11866 return Error(E);
11867 unsigned vlen = v.getVectorLength();
11868 for (unsigned j = 0; j < vlen; j++)
11869 Elements.push_back(Elt: v.getVectorElt(I: j));
11870 CountElts += vlen;
11871 } else if (EltTy->isIntegerType()) {
11872 llvm::APSInt sInt(32);
11873 if (CountInits < NumInits) {
11874 if (!EvaluateInteger(E: E->getInit(Init: CountInits), Result&: sInt, Info))
11875 return false;
11876 } else // trailing integer zero.
11877 sInt = Info.Ctx.MakeIntValue(Value: 0, Type: EltTy);
11878 Elements.push_back(Elt: APValue(sInt));
11879 CountElts++;
11880 } else {
11881 llvm::APFloat f(0.0);
11882 if (CountInits < NumInits) {
11883 if (!EvaluateFloat(E: E->getInit(Init: CountInits), Result&: f, Info))
11884 return false;
11885 } else // trailing float zero.
11886 f = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy));
11887 Elements.push_back(Elt: APValue(f));
11888 CountElts++;
11889 }
11890 CountInits++;
11891 }
11892 return Success(V: Elements, E);
11893}
11894
11895bool
11896VectorExprEvaluator::ZeroInitialization(const Expr *E) {
11897 const auto *VT = E->getType()->castAs<VectorType>();
11898 QualType EltTy = VT->getElementType();
11899 APValue ZeroElement;
11900 if (EltTy->isIntegerType())
11901 ZeroElement = APValue(Info.Ctx.MakeIntValue(Value: 0, Type: EltTy));
11902 else
11903 ZeroElement =
11904 APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: EltTy)));
11905
11906 SmallVector<APValue, 4> Elements(VT->getNumElements(), ZeroElement);
11907 return Success(V: Elements, E);
11908}
11909
11910bool VectorExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
11911 VisitIgnoredValue(E: E->getSubExpr());
11912 return ZeroInitialization(E);
11913}
11914
11915bool VectorExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
11916 BinaryOperatorKind Op = E->getOpcode();
11917 assert(Op != BO_PtrMemD && Op != BO_PtrMemI && Op != BO_Cmp &&
11918 "Operation not supported on vector types");
11919
11920 if (Op == BO_Comma)
11921 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
11922
11923 Expr *LHS = E->getLHS();
11924 Expr *RHS = E->getRHS();
11925
11926 assert(LHS->getType()->isVectorType() && RHS->getType()->isVectorType() &&
11927 "Must both be vector types");
11928 // Checking JUST the types are the same would be fine, except shifts don't
11929 // need to have their types be the same (since you always shift by an int).
11930 assert(LHS->getType()->castAs<VectorType>()->getNumElements() ==
11931 E->getType()->castAs<VectorType>()->getNumElements() &&
11932 RHS->getType()->castAs<VectorType>()->getNumElements() ==
11933 E->getType()->castAs<VectorType>()->getNumElements() &&
11934 "All operands must be the same size.");
11935
11936 APValue LHSValue;
11937 APValue RHSValue;
11938 bool LHSOK = Evaluate(Result&: LHSValue, Info, E: LHS);
11939 if (!LHSOK && !Info.noteFailure())
11940 return false;
11941 if (!Evaluate(Result&: RHSValue, Info, E: RHS) || !LHSOK)
11942 return false;
11943
11944 if (!handleVectorVectorBinOp(Info, E, Opcode: Op, LHSValue, RHSValue))
11945 return false;
11946
11947 return Success(V: LHSValue, E);
11948}
11949
11950static std::optional<APValue> handleVectorUnaryOperator(ASTContext &Ctx,
11951 QualType ResultTy,
11952 UnaryOperatorKind Op,
11953 APValue Elt) {
11954 switch (Op) {
11955 case UO_Plus:
11956 // Nothing to do here.
11957 return Elt;
11958 case UO_Minus:
11959 if (Elt.getKind() == APValue::Int) {
11960 Elt.getInt().negate();
11961 } else {
11962 assert(Elt.getKind() == APValue::Float &&
11963 "Vector can only be int or float type");
11964 Elt.getFloat().changeSign();
11965 }
11966 return Elt;
11967 case UO_Not:
11968 // This is only valid for integral types anyway, so we don't have to handle
11969 // float here.
11970 assert(Elt.getKind() == APValue::Int &&
11971 "Vector operator ~ can only be int");
11972 Elt.getInt().flipAllBits();
11973 return Elt;
11974 case UO_LNot: {
11975 if (Elt.getKind() == APValue::Int) {
11976 Elt.getInt() = !Elt.getInt();
11977 // operator ! on vectors returns -1 for 'truth', so negate it.
11978 Elt.getInt().negate();
11979 return Elt;
11980 }
11981 assert(Elt.getKind() == APValue::Float &&
11982 "Vector can only be int or float type");
11983 // Float types result in an int of the same size, but -1 for true, or 0 for
11984 // false.
11985 APSInt EltResult{Ctx.getIntWidth(T: ResultTy),
11986 ResultTy->isUnsignedIntegerType()};
11987 if (Elt.getFloat().isZero())
11988 EltResult.setAllBits();
11989 else
11990 EltResult.clearAllBits();
11991
11992 return APValue{EltResult};
11993 }
11994 default:
11995 // FIXME: Implement the rest of the unary operators.
11996 return std::nullopt;
11997 }
11998}
11999
12000bool VectorExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
12001 Expr *SubExpr = E->getSubExpr();
12002 const auto *VD = SubExpr->getType()->castAs<VectorType>();
12003 // This result element type differs in the case of negating a floating point
12004 // vector, since the result type is the a vector of the equivilant sized
12005 // integer.
12006 const QualType ResultEltTy = VD->getElementType();
12007 UnaryOperatorKind Op = E->getOpcode();
12008
12009 APValue SubExprValue;
12010 if (!Evaluate(Result&: SubExprValue, Info, E: SubExpr))
12011 return false;
12012
12013 // FIXME: This vector evaluator someday needs to be changed to be LValue
12014 // aware/keep LValue information around, rather than dealing with just vector
12015 // types directly. Until then, we cannot handle cases where the operand to
12016 // these unary operators is an LValue. The only case I've been able to see
12017 // cause this is operator++ assigning to a member expression (only valid in
12018 // altivec compilations) in C mode, so this shouldn't limit us too much.
12019 if (SubExprValue.isLValue())
12020 return false;
12021
12022 assert(SubExprValue.getVectorLength() == VD->getNumElements() &&
12023 "Vector length doesn't match type?");
12024
12025 SmallVector<APValue, 4> ResultElements;
12026 for (unsigned EltNum = 0; EltNum < VD->getNumElements(); ++EltNum) {
12027 std::optional<APValue> Elt = handleVectorUnaryOperator(
12028 Ctx&: Info.Ctx, ResultTy: ResultEltTy, Op, Elt: SubExprValue.getVectorElt(I: EltNum));
12029 if (!Elt)
12030 return false;
12031 ResultElements.push_back(Elt: *Elt);
12032 }
12033 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12034}
12035
12036static bool handleVectorElementCast(EvalInfo &Info, const FPOptions FPO,
12037 const Expr *E, QualType SourceTy,
12038 QualType DestTy, APValue const &Original,
12039 APValue &Result) {
12040 if (SourceTy->isIntegerType()) {
12041 if (DestTy->isRealFloatingType()) {
12042 Result = APValue(APFloat(0.0));
12043 return HandleIntToFloatCast(Info, E, FPO, SrcType: SourceTy, Value: Original.getInt(),
12044 DestType: DestTy, Result&: Result.getFloat());
12045 }
12046 if (DestTy->isIntegerType()) {
12047 Result = APValue(
12048 HandleIntToIntCast(Info, E, DestType: DestTy, SrcType: SourceTy, Value: Original.getInt()));
12049 return true;
12050 }
12051 } else if (SourceTy->isRealFloatingType()) {
12052 if (DestTy->isRealFloatingType()) {
12053 Result = Original;
12054 return HandleFloatToFloatCast(Info, E, SrcType: SourceTy, DestType: DestTy,
12055 Result&: Result.getFloat());
12056 }
12057 if (DestTy->isIntegerType()) {
12058 Result = APValue(APSInt());
12059 return HandleFloatToIntCast(Info, E, SrcType: SourceTy, Value: Original.getFloat(),
12060 DestType: DestTy, Result&: Result.getInt());
12061 }
12062 }
12063
12064 Info.FFDiag(E, DiagId: diag::err_convertvector_constexpr_unsupported_vector_cast)
12065 << SourceTy << DestTy;
12066 return false;
12067}
12068
12069static bool evalPackBuiltin(const CallExpr *E, EvalInfo &Info, APValue &Result,
12070 llvm::function_ref<APInt(const APSInt &)> PackFn) {
12071 APValue LHS, RHS;
12072 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: LHS) ||
12073 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: RHS))
12074 return false;
12075
12076 unsigned LHSVecLen = LHS.getVectorLength();
12077 unsigned RHSVecLen = RHS.getVectorLength();
12078
12079 assert(LHSVecLen != 0 && LHSVecLen == RHSVecLen &&
12080 "pack builtin LHSVecLen must equal to RHSVecLen");
12081
12082 const VectorType *VT0 = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
12083 const unsigned SrcBits = Info.Ctx.getIntWidth(T: VT0->getElementType());
12084
12085 const VectorType *DstVT = E->getType()->castAs<VectorType>();
12086 QualType DstElemTy = DstVT->getElementType();
12087 const bool DstIsUnsigned = DstElemTy->isUnsignedIntegerType();
12088
12089 const unsigned SrcPerLane = 128 / SrcBits;
12090 const unsigned Lanes = LHSVecLen * SrcBits / 128;
12091
12092 SmallVector<APValue, 64> Out;
12093 Out.reserve(N: LHSVecLen + RHSVecLen);
12094
12095 for (unsigned Lane = 0; Lane != Lanes; ++Lane) {
12096 unsigned base = Lane * SrcPerLane;
12097 for (unsigned I = 0; I != SrcPerLane; ++I)
12098 Out.emplace_back(Args: APValue(
12099 APSInt(PackFn(LHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12100 for (unsigned I = 0; I != SrcPerLane; ++I)
12101 Out.emplace_back(Args: APValue(
12102 APSInt(PackFn(RHS.getVectorElt(I: base + I).getInt()), DstIsUnsigned)));
12103 }
12104
12105 Result = APValue(Out.data(), Out.size());
12106 return true;
12107}
12108
12109static bool evalShuffleGeneric(
12110 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12111 llvm::function_ref<std::pair<unsigned, int>(unsigned, unsigned)>
12112 GetSourceIndex) {
12113
12114 const auto *VT = Call->getType()->getAs<VectorType>();
12115 if (!VT)
12116 return false;
12117
12118 unsigned ShuffleMask = 0;
12119 APValue A, MaskVector, B;
12120 bool IsVectorMask = false;
12121 bool IsSingleOperand = (Call->getNumArgs() == 2);
12122
12123 if (IsSingleOperand) {
12124 QualType MaskType = Call->getArg(Arg: 1)->getType();
12125 if (MaskType->isVectorType()) {
12126 IsVectorMask = true;
12127 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12128 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector))
12129 return false;
12130 B = A;
12131 } else if (MaskType->isIntegerType()) {
12132 APSInt MaskImm;
12133 if (!EvaluateInteger(E: Call->getArg(Arg: 1), Result&: MaskImm, Info))
12134 return false;
12135 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12136 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A))
12137 return false;
12138 B = A;
12139 } else {
12140 return false;
12141 }
12142 } else {
12143 QualType Arg2Type = Call->getArg(Arg: 2)->getType();
12144 if (Arg2Type->isVectorType()) {
12145 IsVectorMask = true;
12146 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12147 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: MaskVector) ||
12148 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 2), Result&: B))
12149 return false;
12150 } else if (Arg2Type->isIntegerType()) {
12151 APSInt MaskImm;
12152 if (!EvaluateInteger(E: Call->getArg(Arg: 2), Result&: MaskImm, Info))
12153 return false;
12154 ShuffleMask = static_cast<unsigned>(MaskImm.getZExtValue());
12155 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: A) ||
12156 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: B))
12157 return false;
12158 } else {
12159 return false;
12160 }
12161 }
12162
12163 unsigned NumElts = VT->getNumElements();
12164 SmallVector<APValue, 64> ResultElements;
12165 ResultElements.reserve(N: NumElts);
12166
12167 for (unsigned DstIdx = 0; DstIdx != NumElts; ++DstIdx) {
12168 if (IsVectorMask) {
12169 ShuffleMask = static_cast<unsigned>(
12170 MaskVector.getVectorElt(I: DstIdx).getInt().getZExtValue());
12171 }
12172 auto [SrcVecIdx, SrcIdx] = GetSourceIndex(DstIdx, ShuffleMask);
12173
12174 if (SrcIdx < 0) {
12175 // Zero out this element
12176 QualType ElemTy = VT->getElementType();
12177 if (ElemTy->isRealFloatingType()) {
12178 ResultElements.push_back(
12179 Elt: APValue(APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy))));
12180 } else if (ElemTy->isIntegerType()) {
12181 APValue Zero(Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy));
12182 ResultElements.push_back(Elt: APValue(Zero));
12183 } else {
12184 // Other types of fallback logic
12185 ResultElements.push_back(Elt: APValue());
12186 }
12187 } else {
12188 const APValue &Src = (SrcVecIdx == 0) ? A : B;
12189 ResultElements.push_back(Elt: Src.getVectorElt(I: SrcIdx));
12190 }
12191 }
12192
12193 Out = APValue(ResultElements.data(), ResultElements.size());
12194 return true;
12195}
12196static bool ConvertDoubleToFloatStrict(EvalInfo &Info, const Expr *E,
12197 APFloat OrigVal, APValue &Result) {
12198
12199 if (OrigVal.isInfinity()) {
12200 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 0;
12201 return false;
12202 }
12203 if (OrigVal.isNaN()) {
12204 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic) << 1;
12205 return false;
12206 }
12207
12208 APFloat Val = OrigVal;
12209 bool LosesInfo = false;
12210 APFloat::opStatus Status = Val.convert(
12211 ToSemantics: APFloat::IEEEsingle(), RM: APFloat::rmNearestTiesToEven, losesInfo: &LosesInfo);
12212
12213 if (LosesInfo || Val.isDenormal()) {
12214 Info.CCEDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
12215 return false;
12216 }
12217
12218 if (Status != APFloat::opOK) {
12219 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
12220 return false;
12221 }
12222
12223 Result = APValue(Val);
12224 return true;
12225}
12226static bool evalShiftWithCount(
12227 EvalInfo &Info, const CallExpr *Call, APValue &Out,
12228 llvm::function_ref<APInt(const APInt &, uint64_t)> ShiftOp,
12229 llvm::function_ref<APInt(const APInt &, unsigned)> OverflowOp) {
12230
12231 APValue Source, Count;
12232 if (!EvaluateAsRValue(Info, E: Call->getArg(Arg: 0), Result&: Source) ||
12233 !EvaluateAsRValue(Info, E: Call->getArg(Arg: 1), Result&: Count))
12234 return false;
12235
12236 assert(Call->getNumArgs() == 2);
12237
12238 QualType SourceTy = Call->getArg(Arg: 0)->getType();
12239 assert(SourceTy->isVectorType() &&
12240 Call->getArg(1)->getType()->isVectorType());
12241
12242 QualType DestEltTy = SourceTy->castAs<VectorType>()->getElementType();
12243 unsigned DestEltWidth = Source.getVectorElt(I: 0).getInt().getBitWidth();
12244 unsigned DestLen = Source.getVectorLength();
12245 bool IsDestUnsigned = DestEltTy->isUnsignedIntegerType();
12246 unsigned CountEltWidth = Count.getVectorElt(I: 0).getInt().getBitWidth();
12247 unsigned NumBitsInQWord = 64;
12248 unsigned NumCountElts = NumBitsInQWord / CountEltWidth;
12249 SmallVector<APValue, 64> Result;
12250 Result.reserve(N: DestLen);
12251
12252 uint64_t CountLQWord = 0;
12253 for (unsigned EltIdx = 0; EltIdx != NumCountElts; ++EltIdx) {
12254 uint64_t Elt = Count.getVectorElt(I: EltIdx).getInt().getZExtValue();
12255 CountLQWord |= (Elt << (EltIdx * CountEltWidth));
12256 }
12257
12258 for (unsigned EltIdx = 0; EltIdx != DestLen; ++EltIdx) {
12259 APInt Elt = Source.getVectorElt(I: EltIdx).getInt();
12260 if (CountLQWord < DestEltWidth) {
12261 Result.push_back(
12262 Elt: APValue(APSInt(ShiftOp(Elt, CountLQWord), IsDestUnsigned)));
12263 } else {
12264 Result.push_back(
12265 Elt: APValue(APSInt(OverflowOp(Elt, DestEltWidth), IsDestUnsigned)));
12266 }
12267 }
12268 Out = APValue(Result.data(), Result.size());
12269 return true;
12270}
12271
12272std::optional<APFloat> EvalScalarMinMaxFp(const APFloat &A, const APFloat &B,
12273 std::optional<APSInt> RoundingMode,
12274 bool IsMin) {
12275 APSInt DefaultMode(APInt(32, 4), /*isUnsigned=*/true);
12276 if (RoundingMode.value_or(u&: DefaultMode) != 4)
12277 return std::nullopt;
12278 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
12279 B.isInfinity() || B.isDenormal())
12280 return std::nullopt;
12281 if (A.isZero() && B.isZero())
12282 return B;
12283 return IsMin ? llvm::minimum(A, B) : llvm::maximum(A, B);
12284}
12285
12286bool VectorExprEvaluator::VisitCallExpr(const CallExpr *E) {
12287 if (!IsConstantEvaluatedBuiltinCall(E))
12288 return ExprEvaluatorBaseTy::VisitCallExpr(E);
12289
12290 auto EvaluateBinOpExpr =
12291 [&](llvm::function_ref<APInt(const APSInt &, const APSInt &)> Fn) {
12292 APValue SourceLHS, SourceRHS;
12293 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12294 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12295 return false;
12296
12297 auto *DestTy = E->getType()->castAs<VectorType>();
12298 QualType DestEltTy = DestTy->getElementType();
12299 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12300 unsigned SourceLen = SourceLHS.getVectorLength();
12301 SmallVector<APValue, 4> ResultElements;
12302 ResultElements.reserve(N: SourceLen);
12303
12304 if (SourceRHS.isInt()) {
12305 const APSInt &RHS = SourceRHS.getInt();
12306 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12307 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12308 ResultElements.push_back(
12309 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12310 }
12311 } else {
12312 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12313 const APSInt &LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12314 const APSInt &RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12315 ResultElements.push_back(
12316 Elt: APValue(APSInt(Fn(LHS, RHS), DestUnsigned)));
12317 }
12318 }
12319 return Success(V: APValue(ResultElements.data(), SourceLen), E);
12320 };
12321
12322 auto EvaluateFpBinOpExpr =
12323 [&](llvm::function_ref<std::optional<APFloat>(
12324 const APFloat &, const APFloat &, std::optional<APSInt>)>
12325 Fn,
12326 bool IsScalar = false) {
12327 assert(E->getNumArgs() == 2 || E->getNumArgs() == 3);
12328 APValue A, B;
12329 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
12330 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
12331 return false;
12332
12333 assert(A.isVector() && B.isVector());
12334 assert(A.getVectorLength() == B.getVectorLength());
12335
12336 std::optional<APSInt> RoundingMode;
12337 if (E->getNumArgs() == 3) {
12338 APSInt Imm;
12339 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
12340 return false;
12341 RoundingMode = Imm;
12342 }
12343
12344 unsigned NumElems = A.getVectorLength();
12345 SmallVector<APValue, 4> ResultElements;
12346 ResultElements.reserve(N: NumElems);
12347
12348 for (unsigned EltNum = 0; EltNum < NumElems; ++EltNum) {
12349 if (IsScalar && EltNum > 0) {
12350 ResultElements.push_back(Elt: A.getVectorElt(I: EltNum));
12351 continue;
12352 }
12353 const APFloat &EltA = A.getVectorElt(I: EltNum).getFloat();
12354 const APFloat &EltB = B.getVectorElt(I: EltNum).getFloat();
12355 std::optional<APFloat> Result = Fn(EltA, EltB, RoundingMode);
12356 if (!Result)
12357 return false;
12358 ResultElements.push_back(Elt: APValue(*Result));
12359 }
12360 return Success(V: APValue(ResultElements.data(), NumElems), E);
12361 };
12362
12363 auto EvaluateScalarFpRoundMaskBinOp =
12364 [&](llvm::function_ref<std::optional<APFloat>(
12365 const APFloat &, const APFloat &, std::optional<APSInt>)>
12366 Fn) {
12367 assert(E->getNumArgs() == 5);
12368 APValue VecA, VecB, VecSrc;
12369 APSInt MaskVal, Rounding;
12370
12371 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
12372 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
12373 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
12374 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskVal, Info) ||
12375 !EvaluateInteger(E: E->getArg(Arg: 4), Result&: Rounding, Info))
12376 return false;
12377
12378 unsigned NumElems = VecA.getVectorLength();
12379 SmallVector<APValue, 8> ResultElements;
12380 ResultElements.reserve(N: NumElems);
12381
12382 if (MaskVal.getZExtValue() & 1) {
12383 const APFloat &EltA = VecA.getVectorElt(I: 0).getFloat();
12384 const APFloat &EltB = VecB.getVectorElt(I: 0).getFloat();
12385 std::optional<APFloat> Result = Fn(EltA, EltB, Rounding);
12386 if (!Result)
12387 return false;
12388 ResultElements.push_back(Elt: APValue(*Result));
12389 } else {
12390 ResultElements.push_back(Elt: VecSrc.getVectorElt(I: 0));
12391 }
12392
12393 for (unsigned I = 1; I < NumElems; ++I)
12394 ResultElements.push_back(Elt: VecA.getVectorElt(I));
12395
12396 return Success(V: APValue(ResultElements.data(), NumElems), E);
12397 };
12398
12399 auto EvalSelectScalar = [&](unsigned Len) -> bool {
12400 APSInt Mask;
12401 APValue AVal, WVal;
12402 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info) ||
12403 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: AVal) ||
12404 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: WVal))
12405 return false;
12406
12407 bool TakeA0 = (Mask.getZExtValue() & 1u) != 0;
12408 SmallVector<APValue, 4> Res;
12409 Res.reserve(N: Len);
12410 Res.push_back(Elt: TakeA0 ? AVal.getVectorElt(I: 0) : WVal.getVectorElt(I: 0));
12411 for (unsigned I = 1; I < Len; ++I)
12412 Res.push_back(Elt: WVal.getVectorElt(I));
12413 APValue V(Res.data(), Res.size());
12414 return Success(V, E);
12415 };
12416
12417 auto EvalVectorDotProduct = [&](bool IsSaturating) -> bool {
12418 APValue Source, OperandA, OperandB;
12419 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
12420 !EvaluateVector(E: E->getArg(Arg: 1), Result&: OperandA, Info) ||
12421 !EvaluateVector(E: E->getArg(Arg: 2), Result&: OperandB, Info)) {
12422 return false;
12423 }
12424
12425 unsigned NumSrcElems = Source.getVectorLength();
12426 unsigned NumOperandElems = OperandA.getVectorLength();
12427 unsigned ElemsPerLane = NumOperandElems / NumSrcElems;
12428
12429 assert(OperandA.getVectorLength() == OperandB.getVectorLength());
12430
12431 SmallVector<APValue, 16> Result;
12432 Result.reserve(N: NumSrcElems);
12433 for (unsigned I = 0; I != NumSrcElems; ++I) {
12434 APSInt DotProduct = Source.getVectorElt(I).getInt();
12435 DotProduct = DotProduct.extend(width: 64);
12436 for (unsigned J = 0; J != ElemsPerLane; ++J) {
12437 APSInt OpA = APSInt(
12438 OperandA.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12439 false);
12440 APSInt OpB = APSInt(
12441 OperandB.getVectorElt(I: ElemsPerLane * I + J).getInt().extend(width: 64),
12442 false);
12443 DotProduct += OpA * OpB;
12444 }
12445 if (IsSaturating) {
12446 DotProduct = APSInt(DotProduct.truncSSat(width: 32), false);
12447 } else {
12448 DotProduct = APSInt(DotProduct.trunc(width: 32), false);
12449 }
12450 Result.push_back(Elt: APValue(DotProduct));
12451 }
12452
12453 return Success(V: APValue(Result.data(), Result.size()), E);
12454 };
12455
12456 switch (E->getBuiltinCallee()) {
12457 default:
12458 return false;
12459 case Builtin::BI__builtin_elementwise_popcount:
12460 case Builtin::BI__builtin_elementwise_bitreverse: {
12461 APValue Source;
12462 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12463 return false;
12464
12465 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12466 unsigned SourceLen = Source.getVectorLength();
12467 SmallVector<APValue, 4> ResultElements;
12468 ResultElements.reserve(N: SourceLen);
12469
12470 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12471 APSInt Elt = Source.getVectorElt(I: EltNum).getInt();
12472 switch (E->getBuiltinCallee()) {
12473 case Builtin::BI__builtin_elementwise_popcount:
12474 ResultElements.push_back(Elt: APValue(
12475 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), Elt.popcount()),
12476 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12477 break;
12478 case Builtin::BI__builtin_elementwise_bitreverse:
12479 ResultElements.push_back(
12480 Elt: APValue(APSInt(Elt.reverseBits(),
12481 DestEltTy->isUnsignedIntegerOrEnumerationType())));
12482 break;
12483 }
12484 }
12485
12486 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12487 }
12488 case Builtin::BI__builtin_elementwise_abs: {
12489 APValue Source;
12490 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
12491 return false;
12492
12493 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12494 unsigned SourceLen = Source.getVectorLength();
12495 SmallVector<APValue, 4> ResultElements;
12496 ResultElements.reserve(N: SourceLen);
12497
12498 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
12499 APValue CurrentEle = Source.getVectorElt(I: EltNum);
12500 APValue Val = DestEltTy->isFloatingType()
12501 ? APValue(llvm::abs(X: CurrentEle.getFloat()))
12502 : APValue(APSInt(
12503 CurrentEle.getInt().abs(),
12504 DestEltTy->isUnsignedIntegerOrEnumerationType()));
12505 ResultElements.push_back(Elt: Val);
12506 }
12507
12508 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12509 }
12510
12511 case Builtin::BI__builtin_elementwise_add_sat:
12512 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12513 return LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
12514 });
12515
12516 case Builtin::BI__builtin_elementwise_sub_sat:
12517 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12518 return LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
12519 });
12520
12521 case X86::BI__builtin_ia32_extract128i256:
12522 case X86::BI__builtin_ia32_vextractf128_pd256:
12523 case X86::BI__builtin_ia32_vextractf128_ps256:
12524 case X86::BI__builtin_ia32_vextractf128_si256: {
12525 APValue SourceVec, SourceImm;
12526 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12527 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceImm))
12528 return false;
12529
12530 if (!SourceVec.isVector())
12531 return false;
12532
12533 const auto *RetVT = E->getType()->castAs<VectorType>();
12534 unsigned RetLen = RetVT->getNumElements();
12535 unsigned Idx = SourceImm.getInt().getZExtValue() & 1;
12536
12537 SmallVector<APValue, 32> ResultElements;
12538 ResultElements.reserve(N: RetLen);
12539
12540 for (unsigned I = 0; I < RetLen; I++)
12541 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Idx * RetLen + I));
12542
12543 return Success(V: APValue(ResultElements.data(), RetLen), E);
12544 }
12545
12546 case clang::X86::BI__builtin_ia32_cvtmask2b128:
12547 case clang::X86::BI__builtin_ia32_cvtmask2b256:
12548 case clang::X86::BI__builtin_ia32_cvtmask2b512:
12549 case clang::X86::BI__builtin_ia32_cvtmask2w128:
12550 case clang::X86::BI__builtin_ia32_cvtmask2w256:
12551 case clang::X86::BI__builtin_ia32_cvtmask2w512:
12552 case clang::X86::BI__builtin_ia32_cvtmask2d128:
12553 case clang::X86::BI__builtin_ia32_cvtmask2d256:
12554 case clang::X86::BI__builtin_ia32_cvtmask2d512:
12555 case clang::X86::BI__builtin_ia32_cvtmask2q128:
12556 case clang::X86::BI__builtin_ia32_cvtmask2q256:
12557 case clang::X86::BI__builtin_ia32_cvtmask2q512: {
12558 assert(E->getNumArgs() == 1);
12559 APSInt Mask;
12560 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Mask, Info))
12561 return false;
12562
12563 QualType VecTy = E->getType();
12564 const VectorType *VT = VecTy->castAs<VectorType>();
12565 unsigned VectorLen = VT->getNumElements();
12566 QualType ElemTy = VT->getElementType();
12567 unsigned ElemWidth = Info.Ctx.getTypeSize(T: ElemTy);
12568
12569 SmallVector<APValue, 16> Elems;
12570 for (unsigned I = 0; I != VectorLen; ++I) {
12571 bool BitSet = Mask[I];
12572 APSInt ElemVal(ElemWidth, /*isUnsigned=*/false);
12573 if (BitSet) {
12574 ElemVal.setAllBits();
12575 }
12576 Elems.push_back(Elt: APValue(ElemVal));
12577 }
12578 return Success(V: APValue(Elems.data(), VectorLen), E);
12579 }
12580
12581 case X86::BI__builtin_ia32_extracti32x4_256_mask:
12582 case X86::BI__builtin_ia32_extractf32x4_256_mask:
12583 case X86::BI__builtin_ia32_extracti32x4_mask:
12584 case X86::BI__builtin_ia32_extractf32x4_mask:
12585 case X86::BI__builtin_ia32_extracti32x8_mask:
12586 case X86::BI__builtin_ia32_extractf32x8_mask:
12587 case X86::BI__builtin_ia32_extracti64x2_256_mask:
12588 case X86::BI__builtin_ia32_extractf64x2_256_mask:
12589 case X86::BI__builtin_ia32_extracti64x2_512_mask:
12590 case X86::BI__builtin_ia32_extractf64x2_512_mask:
12591 case X86::BI__builtin_ia32_extracti64x4_mask:
12592 case X86::BI__builtin_ia32_extractf64x4_mask: {
12593 APValue SourceVec, MergeVec;
12594 APSInt Imm, MaskImm;
12595
12596 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceVec) ||
12597 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info) ||
12598 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MergeVec) ||
12599 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: MaskImm, Info))
12600 return false;
12601
12602 const auto *RetVT = E->getType()->castAs<VectorType>();
12603 unsigned RetLen = RetVT->getNumElements();
12604
12605 if (!SourceVec.isVector() || !MergeVec.isVector())
12606 return false;
12607 unsigned SrcLen = SourceVec.getVectorLength();
12608 unsigned Lanes = SrcLen / RetLen;
12609 unsigned Lane = static_cast<unsigned>(Imm.getZExtValue() % Lanes);
12610 unsigned Base = Lane * RetLen;
12611
12612 SmallVector<APValue, 32> ResultElements;
12613 ResultElements.reserve(N: RetLen);
12614 for (unsigned I = 0; I < RetLen; ++I) {
12615 if (MaskImm[I])
12616 ResultElements.push_back(Elt: SourceVec.getVectorElt(I: Base + I));
12617 else
12618 ResultElements.push_back(Elt: MergeVec.getVectorElt(I));
12619 }
12620 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12621 }
12622
12623 case clang::X86::BI__builtin_ia32_pavgb128:
12624 case clang::X86::BI__builtin_ia32_pavgw128:
12625 case clang::X86::BI__builtin_ia32_pavgb256:
12626 case clang::X86::BI__builtin_ia32_pavgw256:
12627 case clang::X86::BI__builtin_ia32_pavgb512:
12628 case clang::X86::BI__builtin_ia32_pavgw512:
12629 return EvaluateBinOpExpr(llvm::APIntOps::avgCeilU);
12630
12631 case clang::X86::BI__builtin_ia32_pmulhrsw128:
12632 case clang::X86::BI__builtin_ia32_pmulhrsw256:
12633 case clang::X86::BI__builtin_ia32_pmulhrsw512:
12634 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12635 return (llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS).ashr(ShiftAmt: 14) + 1)
12636 .extractBits(numBits: 16, bitPosition: 1);
12637 });
12638
12639 case clang::X86::BI__builtin_ia32_pmaddubsw128:
12640 case clang::X86::BI__builtin_ia32_pmaddubsw256:
12641 case clang::X86::BI__builtin_ia32_pmaddubsw512:
12642 case clang::X86::BI__builtin_ia32_pmaddwd128:
12643 case clang::X86::BI__builtin_ia32_pmaddwd256:
12644 case clang::X86::BI__builtin_ia32_pmaddwd512: {
12645 APValue SourceLHS, SourceRHS;
12646 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12647 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12648 return false;
12649
12650 auto *DestTy = E->getType()->castAs<VectorType>();
12651 QualType DestEltTy = DestTy->getElementType();
12652 unsigned SourceLen = SourceLHS.getVectorLength();
12653 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12654 SmallVector<APValue, 4> ResultElements;
12655 ResultElements.reserve(N: SourceLen / 2);
12656
12657 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
12658 const APSInt &LoLHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12659 const APSInt &HiLHS = SourceLHS.getVectorElt(I: EltNum + 1).getInt();
12660 const APSInt &LoRHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12661 const APSInt &HiRHS = SourceRHS.getVectorElt(I: EltNum + 1).getInt();
12662 unsigned BitWidth = 2 * LoLHS.getBitWidth();
12663
12664 switch (E->getBuiltinCallee()) {
12665 case clang::X86::BI__builtin_ia32_pmaddubsw128:
12666 case clang::X86::BI__builtin_ia32_pmaddubsw256:
12667 case clang::X86::BI__builtin_ia32_pmaddubsw512:
12668 ResultElements.push_back(Elt: APValue(
12669 APSInt((LoLHS.zext(width: BitWidth) * LoRHS.sext(width: BitWidth))
12670 .sadd_sat(RHS: (HiLHS.zext(width: BitWidth) * HiRHS.sext(width: BitWidth))),
12671 DestUnsigned)));
12672 break;
12673 case clang::X86::BI__builtin_ia32_pmaddwd128:
12674 case clang::X86::BI__builtin_ia32_pmaddwd256:
12675 case clang::X86::BI__builtin_ia32_pmaddwd512:
12676 ResultElements.push_back(
12677 Elt: APValue(APSInt((LoLHS.sext(width: BitWidth) * LoRHS.sext(width: BitWidth)) +
12678 (HiLHS.sext(width: BitWidth) * HiRHS.sext(width: BitWidth)),
12679 DestUnsigned)));
12680 break;
12681 }
12682 }
12683
12684 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12685 }
12686
12687 case clang::X86::BI__builtin_ia32_dbpsadbw128:
12688 case clang::X86::BI__builtin_ia32_dbpsadbw256:
12689 case clang::X86::BI__builtin_ia32_dbpsadbw512: {
12690 APValue SourceA, SourceB, SourceImm;
12691 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
12692 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB) ||
12693 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceImm))
12694 return false;
12695
12696 unsigned SourceLen = SourceA.getVectorLength();
12697 constexpr unsigned LaneSize = 16; // 128-bit lane = 16 bytes
12698 unsigned Imm = SourceImm.getInt().getZExtValue();
12699
12700 auto *DestTy = E->getType()->castAs<VectorType>();
12701 QualType DestEltTy = DestTy->getElementType();
12702 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12703 SmallVector<APValue, 32> ResultElements;
12704 ResultElements.reserve(N: SourceLen / 2);
12705
12706 // Phase 1: Shuffle SourceB using all four 2-bit fields of imm8.
12707 // Within each 128-bit lane, for group j (0..3), select a 4-byte block
12708 // from SourceB based on bits [2*j+1:2*j] of imm8.
12709 SmallVector<uint8_t, 64> Shuffled(SourceLen);
12710 for (unsigned I = 0; I < SourceLen; I += LaneSize) {
12711 for (unsigned J = 0; J < 4; ++J) {
12712 unsigned Part = (Imm >> (2 * J)) & 3;
12713 for (unsigned K = 0; K < 4; ++K) {
12714 Shuffled[I + 4 * J + K] = static_cast<uint8_t>(
12715 SourceB.getVectorElt(I: I + 4 * Part + K).getInt().getZExtValue());
12716 }
12717 }
12718 }
12719
12720 // Phase 2: Sliding SAD computation.
12721 // For every group of 4 output u16 values, compute absolute differences
12722 // using overlapping windows into SourceA and the shuffled array.
12723 unsigned Size = SourceLen / 2; // number of output u16 elements
12724 for (unsigned I = 0; I < Size; I += 4) {
12725 unsigned Sad[4] = {0, 0, 0, 0};
12726 for (unsigned J = 0; J < 4; ++J) {
12727 uint8_t A1 = static_cast<uint8_t>(
12728 SourceA.getVectorElt(I: 2 * I + J).getInt().getZExtValue());
12729 uint8_t A2 = static_cast<uint8_t>(
12730 SourceA.getVectorElt(I: 2 * I + J + 4).getInt().getZExtValue());
12731 uint8_t B0 = Shuffled[2 * I + J];
12732 uint8_t B1 = Shuffled[2 * I + J + 1];
12733 uint8_t B2 = Shuffled[2 * I + J + 2];
12734 uint8_t B3 = Shuffled[2 * I + J + 3];
12735 Sad[0] += (A1 > B0) ? (A1 - B0) : (B0 - A1);
12736 Sad[1] += (A1 > B1) ? (A1 - B1) : (B1 - A1);
12737 Sad[2] += (A2 > B2) ? (A2 - B2) : (B2 - A2);
12738 Sad[3] += (A2 > B3) ? (A2 - B3) : (B3 - A2);
12739 }
12740 for (unsigned R = 0; R < 4; ++R)
12741 ResultElements.push_back(
12742 Elt: APValue(APSInt(APInt(16, Sad[R]), DestUnsigned)));
12743 }
12744
12745 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12746 }
12747
12748 case clang::X86::BI__builtin_ia32_mpsadbw128:
12749 case clang::X86::BI__builtin_ia32_mpsadbw256: {
12750 APValue SourceA, SourceB;
12751 APSInt SourceImm;
12752 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: SourceA, Info) ||
12753 !EvaluateVector(E: E->getArg(Arg: 1), Result&: SourceB, Info) ||
12754 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: SourceImm, Info))
12755 return false;
12756 unsigned SourceLen = SourceA.getVectorLength();
12757 constexpr unsigned LaneSize = 16;
12758 assert((SourceLen == LaneSize || SourceLen == 2 * LaneSize) &&
12759 "MPSADBW operates on 128-bit or 256-bit vectors");
12760 unsigned NumLanes = SourceLen / LaneSize;
12761 unsigned Imm = SourceImm.getZExtValue();
12762
12763 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
12764 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
12765 SmallVector<APValue, 16> ResultElements;
12766 ResultElements.reserve(N: SourceLen / 2);
12767
12768 for (unsigned Lane = 0; Lane != NumLanes; ++Lane) {
12769 unsigned Ctrl = (Imm >> (3 * Lane)) & 0x7;
12770 unsigned AOff = ((Ctrl >> 2) & 1) * 4;
12771 unsigned BOff = (Ctrl & 3) * 4;
12772 for (unsigned J = 0; J != 8; ++J) {
12773 uint16_t Sad = 0;
12774 for (unsigned K = 0; K != 4; ++K) {
12775 uint8_t A = static_cast<uint8_t>(
12776 SourceA.getVectorElt(I: Lane * LaneSize + AOff + J + K)
12777 .getInt()
12778 .getZExtValue());
12779 uint8_t B = static_cast<uint8_t>(
12780 SourceB.getVectorElt(I: Lane * LaneSize + BOff + K)
12781 .getInt()
12782 .getZExtValue());
12783 Sad += (A > B) ? (A - B) : (B - A);
12784 }
12785 ResultElements.push_back(Elt: APValue(APSInt(APInt(16, Sad), DestUnsigned)));
12786 }
12787 }
12788 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12789 }
12790
12791 case clang::X86::BI__builtin_ia32_pmulhuw128:
12792 case clang::X86::BI__builtin_ia32_pmulhuw256:
12793 case clang::X86::BI__builtin_ia32_pmulhuw512:
12794 return EvaluateBinOpExpr(llvm::APIntOps::mulhu);
12795
12796 case clang::X86::BI__builtin_ia32_pmulhw128:
12797 case clang::X86::BI__builtin_ia32_pmulhw256:
12798 case clang::X86::BI__builtin_ia32_pmulhw512:
12799 return EvaluateBinOpExpr(llvm::APIntOps::mulhs);
12800
12801 case clang::X86::BI__builtin_ia32_psllv2di:
12802 case clang::X86::BI__builtin_ia32_psllv4di:
12803 case clang::X86::BI__builtin_ia32_psllv4si:
12804 case clang::X86::BI__builtin_ia32_psllv8di:
12805 case clang::X86::BI__builtin_ia32_psllv8hi:
12806 case clang::X86::BI__builtin_ia32_psllv8si:
12807 case clang::X86::BI__builtin_ia32_psllv16hi:
12808 case clang::X86::BI__builtin_ia32_psllv16si:
12809 case clang::X86::BI__builtin_ia32_psllv32hi:
12810 case clang::X86::BI__builtin_ia32_psllwi128:
12811 case clang::X86::BI__builtin_ia32_pslldi128:
12812 case clang::X86::BI__builtin_ia32_psllqi128:
12813 case clang::X86::BI__builtin_ia32_psllwi256:
12814 case clang::X86::BI__builtin_ia32_pslldi256:
12815 case clang::X86::BI__builtin_ia32_psllqi256:
12816 case clang::X86::BI__builtin_ia32_psllwi512:
12817 case clang::X86::BI__builtin_ia32_pslldi512:
12818 case clang::X86::BI__builtin_ia32_psllqi512:
12819 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12820 if (RHS.uge(RHS: LHS.getBitWidth())) {
12821 return APInt::getZero(numBits: LHS.getBitWidth());
12822 }
12823 return LHS.shl(shiftAmt: RHS.getZExtValue());
12824 });
12825
12826 case clang::X86::BI__builtin_ia32_psrav4si:
12827 case clang::X86::BI__builtin_ia32_psrav8di:
12828 case clang::X86::BI__builtin_ia32_psrav8hi:
12829 case clang::X86::BI__builtin_ia32_psrav8si:
12830 case clang::X86::BI__builtin_ia32_psrav16hi:
12831 case clang::X86::BI__builtin_ia32_psrav16si:
12832 case clang::X86::BI__builtin_ia32_psrav32hi:
12833 case clang::X86::BI__builtin_ia32_psravq128:
12834 case clang::X86::BI__builtin_ia32_psravq256:
12835 case clang::X86::BI__builtin_ia32_psrawi128:
12836 case clang::X86::BI__builtin_ia32_psradi128:
12837 case clang::X86::BI__builtin_ia32_psraqi128:
12838 case clang::X86::BI__builtin_ia32_psrawi256:
12839 case clang::X86::BI__builtin_ia32_psradi256:
12840 case clang::X86::BI__builtin_ia32_psraqi256:
12841 case clang::X86::BI__builtin_ia32_psrawi512:
12842 case clang::X86::BI__builtin_ia32_psradi512:
12843 case clang::X86::BI__builtin_ia32_psraqi512:
12844 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12845 if (RHS.uge(RHS: LHS.getBitWidth())) {
12846 return LHS.ashr(ShiftAmt: LHS.getBitWidth() - 1);
12847 }
12848 return LHS.ashr(ShiftAmt: RHS.getZExtValue());
12849 });
12850
12851 case clang::X86::BI__builtin_ia32_psrlv2di:
12852 case clang::X86::BI__builtin_ia32_psrlv4di:
12853 case clang::X86::BI__builtin_ia32_psrlv4si:
12854 case clang::X86::BI__builtin_ia32_psrlv8di:
12855 case clang::X86::BI__builtin_ia32_psrlv8hi:
12856 case clang::X86::BI__builtin_ia32_psrlv8si:
12857 case clang::X86::BI__builtin_ia32_psrlv16hi:
12858 case clang::X86::BI__builtin_ia32_psrlv16si:
12859 case clang::X86::BI__builtin_ia32_psrlv32hi:
12860 case clang::X86::BI__builtin_ia32_psrlwi128:
12861 case clang::X86::BI__builtin_ia32_psrldi128:
12862 case clang::X86::BI__builtin_ia32_psrlqi128:
12863 case clang::X86::BI__builtin_ia32_psrlwi256:
12864 case clang::X86::BI__builtin_ia32_psrldi256:
12865 case clang::X86::BI__builtin_ia32_psrlqi256:
12866 case clang::X86::BI__builtin_ia32_psrlwi512:
12867 case clang::X86::BI__builtin_ia32_psrldi512:
12868 case clang::X86::BI__builtin_ia32_psrlqi512:
12869 return EvaluateBinOpExpr([](const APSInt &LHS, const APSInt &RHS) {
12870 if (RHS.uge(RHS: LHS.getBitWidth())) {
12871 return APInt::getZero(numBits: LHS.getBitWidth());
12872 }
12873 return LHS.lshr(shiftAmt: RHS.getZExtValue());
12874 });
12875 case X86::BI__builtin_ia32_packsswb128:
12876 case X86::BI__builtin_ia32_packsswb256:
12877 case X86::BI__builtin_ia32_packsswb512:
12878 case X86::BI__builtin_ia32_packssdw128:
12879 case X86::BI__builtin_ia32_packssdw256:
12880 case X86::BI__builtin_ia32_packssdw512:
12881 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
12882 return APSInt(Src).truncSSat(width: Src.getBitWidth() / 2);
12883 });
12884 case X86::BI__builtin_ia32_packusdw128:
12885 case X86::BI__builtin_ia32_packusdw256:
12886 case X86::BI__builtin_ia32_packusdw512:
12887 case X86::BI__builtin_ia32_packuswb128:
12888 case X86::BI__builtin_ia32_packuswb256:
12889 case X86::BI__builtin_ia32_packuswb512:
12890 return evalPackBuiltin(E, Info, Result, PackFn: [](const APSInt &Src) {
12891 return APSInt(Src).truncSSatU(width: Src.getBitWidth() / 2);
12892 });
12893 case clang::X86::BI__builtin_ia32_selectss_128:
12894 return EvalSelectScalar(4);
12895 case clang::X86::BI__builtin_ia32_selectsd_128:
12896 return EvalSelectScalar(2);
12897 case clang::X86::BI__builtin_ia32_selectsh_128:
12898 case clang::X86::BI__builtin_ia32_selectsbf_128:
12899 return EvalSelectScalar(8);
12900 case clang::X86::BI__builtin_ia32_pmuldq128:
12901 case clang::X86::BI__builtin_ia32_pmuldq256:
12902 case clang::X86::BI__builtin_ia32_pmuldq512:
12903 case clang::X86::BI__builtin_ia32_pmuludq128:
12904 case clang::X86::BI__builtin_ia32_pmuludq256:
12905 case clang::X86::BI__builtin_ia32_pmuludq512: {
12906 APValue SourceLHS, SourceRHS;
12907 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
12908 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
12909 return false;
12910
12911 unsigned SourceLen = SourceLHS.getVectorLength();
12912 SmallVector<APValue, 4> ResultElements;
12913 ResultElements.reserve(N: SourceLen / 2);
12914
12915 for (unsigned EltNum = 0; EltNum < SourceLen; EltNum += 2) {
12916 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
12917 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
12918
12919 switch (E->getBuiltinCallee()) {
12920 case clang::X86::BI__builtin_ia32_pmuludq128:
12921 case clang::X86::BI__builtin_ia32_pmuludq256:
12922 case clang::X86::BI__builtin_ia32_pmuludq512:
12923 ResultElements.push_back(
12924 Elt: APValue(APSInt(llvm::APIntOps::muluExtended(C1: LHS, C2: RHS), true)));
12925 break;
12926 case clang::X86::BI__builtin_ia32_pmuldq128:
12927 case clang::X86::BI__builtin_ia32_pmuldq256:
12928 case clang::X86::BI__builtin_ia32_pmuldq512:
12929 ResultElements.push_back(
12930 Elt: APValue(APSInt(llvm::APIntOps::mulsExtended(C1: LHS, C2: RHS), false)));
12931 break;
12932 }
12933 }
12934
12935 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12936 }
12937
12938 case X86::BI__builtin_ia32_vpmadd52luq128:
12939 case X86::BI__builtin_ia32_vpmadd52luq256:
12940 case X86::BI__builtin_ia32_vpmadd52luq512: {
12941 APValue A, B, C;
12942 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
12943 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
12944 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
12945 return false;
12946
12947 unsigned ALen = A.getVectorLength();
12948 SmallVector<APValue, 4> ResultElements;
12949 ResultElements.reserve(N: ALen);
12950
12951 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
12952 APInt AElt = A.getVectorElt(I: EltNum).getInt();
12953 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
12954 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
12955 APSInt ResElt(AElt + (BElt * CElt).zext(width: 64), false);
12956 ResultElements.push_back(Elt: APValue(ResElt));
12957 }
12958
12959 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12960 }
12961 case X86::BI__builtin_ia32_vpmadd52huq128:
12962 case X86::BI__builtin_ia32_vpmadd52huq256:
12963 case X86::BI__builtin_ia32_vpmadd52huq512: {
12964 APValue A, B, C;
12965 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
12966 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B) ||
12967 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: C))
12968 return false;
12969
12970 unsigned ALen = A.getVectorLength();
12971 SmallVector<APValue, 4> ResultElements;
12972 ResultElements.reserve(N: ALen);
12973
12974 for (unsigned EltNum = 0; EltNum < ALen; EltNum += 1) {
12975 APInt AElt = A.getVectorElt(I: EltNum).getInt();
12976 APInt BElt = B.getVectorElt(I: EltNum).getInt().trunc(width: 52);
12977 APInt CElt = C.getVectorElt(I: EltNum).getInt().trunc(width: 52);
12978 APSInt ResElt(AElt + llvm::APIntOps::mulhu(C1: BElt, C2: CElt).zext(width: 64), false);
12979 ResultElements.push_back(Elt: APValue(ResElt));
12980 }
12981
12982 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
12983 }
12984
12985 case clang::X86::BI__builtin_ia32_vprotbi:
12986 case clang::X86::BI__builtin_ia32_vprotdi:
12987 case clang::X86::BI__builtin_ia32_vprotqi:
12988 case clang::X86::BI__builtin_ia32_vprotwi:
12989 case clang::X86::BI__builtin_ia32_prold128:
12990 case clang::X86::BI__builtin_ia32_prold256:
12991 case clang::X86::BI__builtin_ia32_prold512:
12992 case clang::X86::BI__builtin_ia32_prolq128:
12993 case clang::X86::BI__builtin_ia32_prolq256:
12994 case clang::X86::BI__builtin_ia32_prolq512:
12995 return EvaluateBinOpExpr(
12996 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotl(rotateAmt: RHS); });
12997
12998 case clang::X86::BI__builtin_ia32_prord128:
12999 case clang::X86::BI__builtin_ia32_prord256:
13000 case clang::X86::BI__builtin_ia32_prord512:
13001 case clang::X86::BI__builtin_ia32_prorq128:
13002 case clang::X86::BI__builtin_ia32_prorq256:
13003 case clang::X86::BI__builtin_ia32_prorq512:
13004 return EvaluateBinOpExpr(
13005 [](const APSInt &LHS, const APSInt &RHS) { return LHS.rotr(rotateAmt: RHS); });
13006
13007 case Builtin::BI__builtin_elementwise_max:
13008 case Builtin::BI__builtin_elementwise_min: {
13009 APValue SourceLHS, SourceRHS;
13010 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13011 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13012 return false;
13013
13014 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13015
13016 if (!DestEltTy->isIntegerType())
13017 return false;
13018
13019 unsigned SourceLen = SourceLHS.getVectorLength();
13020 SmallVector<APValue, 4> ResultElements;
13021 ResultElements.reserve(N: SourceLen);
13022
13023 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13024 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13025 APSInt RHS = SourceRHS.getVectorElt(I: EltNum).getInt();
13026 switch (E->getBuiltinCallee()) {
13027 case Builtin::BI__builtin_elementwise_max:
13028 ResultElements.push_back(
13029 Elt: APValue(APSInt(std::max(a: LHS, b: RHS),
13030 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13031 break;
13032 case Builtin::BI__builtin_elementwise_min:
13033 ResultElements.push_back(
13034 Elt: APValue(APSInt(std::min(a: LHS, b: RHS),
13035 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13036 break;
13037 }
13038 }
13039
13040 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13041 }
13042 case X86::BI__builtin_ia32_vpshldd128:
13043 case X86::BI__builtin_ia32_vpshldd256:
13044 case X86::BI__builtin_ia32_vpshldd512:
13045 case X86::BI__builtin_ia32_vpshldq128:
13046 case X86::BI__builtin_ia32_vpshldq256:
13047 case X86::BI__builtin_ia32_vpshldq512:
13048 case X86::BI__builtin_ia32_vpshldw128:
13049 case X86::BI__builtin_ia32_vpshldw256:
13050 case X86::BI__builtin_ia32_vpshldw512: {
13051 APValue SourceHi, SourceLo, SourceAmt;
13052 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
13053 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
13054 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13055 return false;
13056
13057 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13058 unsigned SourceLen = SourceHi.getVectorLength();
13059 SmallVector<APValue, 32> ResultElements;
13060 ResultElements.reserve(N: SourceLen);
13061
13062 APInt Amt = SourceAmt.getInt();
13063 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13064 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13065 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13066 APInt R = llvm::APIntOps::fshl(Hi, Lo, Shift: Amt);
13067 ResultElements.push_back(
13068 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13069 }
13070
13071 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13072 }
13073 case X86::BI__builtin_ia32_vpshrdd128:
13074 case X86::BI__builtin_ia32_vpshrdd256:
13075 case X86::BI__builtin_ia32_vpshrdd512:
13076 case X86::BI__builtin_ia32_vpshrdq128:
13077 case X86::BI__builtin_ia32_vpshrdq256:
13078 case X86::BI__builtin_ia32_vpshrdq512:
13079 case X86::BI__builtin_ia32_vpshrdw128:
13080 case X86::BI__builtin_ia32_vpshrdw256:
13081 case X86::BI__builtin_ia32_vpshrdw512: {
13082 // NOTE: Reversed Hi/Lo operands.
13083 APValue SourceHi, SourceLo, SourceAmt;
13084 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLo) ||
13085 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceHi) ||
13086 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceAmt))
13087 return false;
13088
13089 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13090 unsigned SourceLen = SourceHi.getVectorLength();
13091 SmallVector<APValue, 32> ResultElements;
13092 ResultElements.reserve(N: SourceLen);
13093
13094 APInt Amt = SourceAmt.getInt();
13095 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13096 APInt Hi = SourceHi.getVectorElt(I: EltNum).getInt();
13097 APInt Lo = SourceLo.getVectorElt(I: EltNum).getInt();
13098 APInt R = llvm::APIntOps::fshr(Hi, Lo, Shift: Amt);
13099 ResultElements.push_back(
13100 Elt: APValue(APSInt(R, DestEltTy->isUnsignedIntegerOrEnumerationType())));
13101 }
13102
13103 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13104 }
13105 case X86::BI__builtin_ia32_compressdf128_mask:
13106 case X86::BI__builtin_ia32_compressdf256_mask:
13107 case X86::BI__builtin_ia32_compressdf512_mask:
13108 case X86::BI__builtin_ia32_compressdi128_mask:
13109 case X86::BI__builtin_ia32_compressdi256_mask:
13110 case X86::BI__builtin_ia32_compressdi512_mask:
13111 case X86::BI__builtin_ia32_compresshi128_mask:
13112 case X86::BI__builtin_ia32_compresshi256_mask:
13113 case X86::BI__builtin_ia32_compresshi512_mask:
13114 case X86::BI__builtin_ia32_compressqi128_mask:
13115 case X86::BI__builtin_ia32_compressqi256_mask:
13116 case X86::BI__builtin_ia32_compressqi512_mask:
13117 case X86::BI__builtin_ia32_compresssf128_mask:
13118 case X86::BI__builtin_ia32_compresssf256_mask:
13119 case X86::BI__builtin_ia32_compresssf512_mask:
13120 case X86::BI__builtin_ia32_compresssi128_mask:
13121 case X86::BI__builtin_ia32_compresssi256_mask:
13122 case X86::BI__builtin_ia32_compresssi512_mask: {
13123 APValue Source, Passthru;
13124 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13125 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13126 return false;
13127 APSInt Mask;
13128 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13129 return false;
13130
13131 unsigned NumElts = Source.getVectorLength();
13132 SmallVector<APValue, 64> ResultElements;
13133 ResultElements.reserve(N: NumElts);
13134
13135 for (unsigned I = 0; I != NumElts; ++I) {
13136 if (Mask[I])
13137 ResultElements.push_back(Elt: Source.getVectorElt(I));
13138 }
13139 for (unsigned I = ResultElements.size(); I != NumElts; ++I) {
13140 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13141 }
13142
13143 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13144 }
13145 case X86::BI__builtin_ia32_expanddf128_mask:
13146 case X86::BI__builtin_ia32_expanddf256_mask:
13147 case X86::BI__builtin_ia32_expanddf512_mask:
13148 case X86::BI__builtin_ia32_expanddi128_mask:
13149 case X86::BI__builtin_ia32_expanddi256_mask:
13150 case X86::BI__builtin_ia32_expanddi512_mask:
13151 case X86::BI__builtin_ia32_expandhi128_mask:
13152 case X86::BI__builtin_ia32_expandhi256_mask:
13153 case X86::BI__builtin_ia32_expandhi512_mask:
13154 case X86::BI__builtin_ia32_expandqi128_mask:
13155 case X86::BI__builtin_ia32_expandqi256_mask:
13156 case X86::BI__builtin_ia32_expandqi512_mask:
13157 case X86::BI__builtin_ia32_expandsf128_mask:
13158 case X86::BI__builtin_ia32_expandsf256_mask:
13159 case X86::BI__builtin_ia32_expandsf512_mask:
13160 case X86::BI__builtin_ia32_expandsi128_mask:
13161 case X86::BI__builtin_ia32_expandsi256_mask:
13162 case X86::BI__builtin_ia32_expandsi512_mask: {
13163 APValue Source, Passthru;
13164 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source) ||
13165 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: Passthru))
13166 return false;
13167 APSInt Mask;
13168 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Mask, Info))
13169 return false;
13170
13171 unsigned NumElts = Source.getVectorLength();
13172 SmallVector<APValue, 64> ResultElements;
13173 ResultElements.reserve(N: NumElts);
13174
13175 unsigned SourceIdx = 0;
13176 for (unsigned I = 0; I != NumElts; ++I) {
13177 if (Mask[I])
13178 ResultElements.push_back(Elt: Source.getVectorElt(I: SourceIdx++));
13179 else
13180 ResultElements.push_back(Elt: Passthru.getVectorElt(I));
13181 }
13182 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13183 }
13184 case X86::BI__builtin_ia32_vpconflictsi_128:
13185 case X86::BI__builtin_ia32_vpconflictsi_256:
13186 case X86::BI__builtin_ia32_vpconflictsi_512:
13187 case X86::BI__builtin_ia32_vpconflictdi_128:
13188 case X86::BI__builtin_ia32_vpconflictdi_256:
13189 case X86::BI__builtin_ia32_vpconflictdi_512: {
13190 APValue Source;
13191
13192 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
13193 return false;
13194
13195 unsigned SourceLen = Source.getVectorLength();
13196 SmallVector<APValue, 32> ResultElements;
13197 ResultElements.reserve(N: SourceLen);
13198
13199 const auto *VecT = E->getType()->castAs<VectorType>();
13200 bool DestUnsigned =
13201 VecT->getElementType()->isUnsignedIntegerOrEnumerationType();
13202
13203 for (unsigned I = 0; I != SourceLen; ++I) {
13204 const APValue &EltI = Source.getVectorElt(I);
13205
13206 APInt ConflictMask(EltI.getInt().getBitWidth(), 0);
13207 for (unsigned J = 0; J != I; ++J) {
13208 const APValue &EltJ = Source.getVectorElt(I: J);
13209 ConflictMask.setBitVal(BitPosition: J, BitValue: EltI.getInt() == EltJ.getInt());
13210 }
13211 ResultElements.push_back(Elt: APValue(APSInt(ConflictMask, DestUnsigned)));
13212 }
13213 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13214 }
13215 case X86::BI__builtin_ia32_blendpd:
13216 case X86::BI__builtin_ia32_blendpd256:
13217 case X86::BI__builtin_ia32_blendps:
13218 case X86::BI__builtin_ia32_blendps256:
13219 case X86::BI__builtin_ia32_pblendw128:
13220 case X86::BI__builtin_ia32_pblendw256:
13221 case X86::BI__builtin_ia32_pblendd128:
13222 case X86::BI__builtin_ia32_pblendd256: {
13223 APValue SourceF, SourceT, SourceC;
13224 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13225 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13226 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13227 return false;
13228
13229 const APInt &C = SourceC.getInt();
13230 unsigned SourceLen = SourceF.getVectorLength();
13231 SmallVector<APValue, 32> ResultElements;
13232 ResultElements.reserve(N: SourceLen);
13233 for (unsigned EltNum = 0; EltNum != SourceLen; ++EltNum) {
13234 const APValue &F = SourceF.getVectorElt(I: EltNum);
13235 const APValue &T = SourceT.getVectorElt(I: EltNum);
13236 ResultElements.push_back(Elt: C[EltNum % 8] ? T : F);
13237 }
13238
13239 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13240 }
13241
13242 case X86::BI__builtin_ia32_psignb128:
13243 case X86::BI__builtin_ia32_psignb256:
13244 case X86::BI__builtin_ia32_psignw128:
13245 case X86::BI__builtin_ia32_psignw256:
13246 case X86::BI__builtin_ia32_psignd128:
13247 case X86::BI__builtin_ia32_psignd256:
13248 return EvaluateBinOpExpr([](const APInt &AElem, const APInt &BElem) {
13249 if (BElem.isZero())
13250 return APInt::getZero(numBits: AElem.getBitWidth());
13251 if (BElem.isNegative())
13252 return -AElem;
13253 return AElem;
13254 });
13255
13256 case X86::BI__builtin_ia32_blendvpd:
13257 case X86::BI__builtin_ia32_blendvpd256:
13258 case X86::BI__builtin_ia32_blendvps:
13259 case X86::BI__builtin_ia32_blendvps256:
13260 case X86::BI__builtin_ia32_pblendvb128:
13261 case X86::BI__builtin_ia32_pblendvb256: {
13262 // SSE blendv by mask signbit: "Result = C[] < 0 ? T[] : F[]".
13263 APValue SourceF, SourceT, SourceC;
13264 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceF) ||
13265 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceT) ||
13266 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceC))
13267 return false;
13268
13269 unsigned SourceLen = SourceF.getVectorLength();
13270 SmallVector<APValue, 32> ResultElements;
13271 ResultElements.reserve(N: SourceLen);
13272
13273 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13274 const APValue &F = SourceF.getVectorElt(I: EltNum);
13275 const APValue &T = SourceT.getVectorElt(I: EltNum);
13276 const APValue &C = SourceC.getVectorElt(I: EltNum);
13277 APInt M = C.isInt() ? (APInt)C.getInt() : C.getFloat().bitcastToAPInt();
13278 ResultElements.push_back(Elt: M.isNegative() ? T : F);
13279 }
13280
13281 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13282 }
13283 case X86::BI__builtin_ia32_selectb_128:
13284 case X86::BI__builtin_ia32_selectb_256:
13285 case X86::BI__builtin_ia32_selectb_512:
13286 case X86::BI__builtin_ia32_selectw_128:
13287 case X86::BI__builtin_ia32_selectw_256:
13288 case X86::BI__builtin_ia32_selectw_512:
13289 case X86::BI__builtin_ia32_selectd_128:
13290 case X86::BI__builtin_ia32_selectd_256:
13291 case X86::BI__builtin_ia32_selectd_512:
13292 case X86::BI__builtin_ia32_selectq_128:
13293 case X86::BI__builtin_ia32_selectq_256:
13294 case X86::BI__builtin_ia32_selectq_512:
13295 case X86::BI__builtin_ia32_selectph_128:
13296 case X86::BI__builtin_ia32_selectph_256:
13297 case X86::BI__builtin_ia32_selectph_512:
13298 case X86::BI__builtin_ia32_selectpbf_128:
13299 case X86::BI__builtin_ia32_selectpbf_256:
13300 case X86::BI__builtin_ia32_selectpbf_512:
13301 case X86::BI__builtin_ia32_selectps_128:
13302 case X86::BI__builtin_ia32_selectps_256:
13303 case X86::BI__builtin_ia32_selectps_512:
13304 case X86::BI__builtin_ia32_selectpd_128:
13305 case X86::BI__builtin_ia32_selectpd_256:
13306 case X86::BI__builtin_ia32_selectpd_512: {
13307 // AVX512 predicated move: "Result = Mask[] ? LHS[] : RHS[]".
13308 APValue SourceMask, SourceLHS, SourceRHS;
13309 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceMask) ||
13310 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLHS) ||
13311 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceRHS))
13312 return false;
13313
13314 APSInt Mask = SourceMask.getInt();
13315 unsigned SourceLen = SourceLHS.getVectorLength();
13316 SmallVector<APValue, 4> ResultElements;
13317 ResultElements.reserve(N: SourceLen);
13318
13319 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13320 const APValue &LHS = SourceLHS.getVectorElt(I: EltNum);
13321 const APValue &RHS = SourceRHS.getVectorElt(I: EltNum);
13322 ResultElements.push_back(Elt: Mask[EltNum] ? LHS : RHS);
13323 }
13324
13325 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13326 }
13327
13328 case X86::BI__builtin_ia32_cvtsd2ss: {
13329 APValue VecA, VecB;
13330 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13331 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB))
13332 return false;
13333
13334 SmallVector<APValue, 4> Elements;
13335
13336 APValue ResultVal;
13337 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13338 Result&: ResultVal))
13339 return false;
13340
13341 Elements.push_back(Elt: ResultVal);
13342
13343 unsigned NumEltsA = VecA.getVectorLength();
13344 for (unsigned I = 1; I < NumEltsA; ++I) {
13345 Elements.push_back(Elt: VecA.getVectorElt(I));
13346 }
13347
13348 return Success(V: Elements, E);
13349 }
13350 case X86::BI__builtin_ia32_cvtsd2ss_round_mask: {
13351 APValue VecA, VecB, VecSrc, MaskValue;
13352
13353 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: VecA) ||
13354 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: VecB) ||
13355 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: VecSrc) ||
13356 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: MaskValue))
13357 return false;
13358
13359 unsigned Mask = MaskValue.getInt().getZExtValue();
13360 SmallVector<APValue, 4> Elements;
13361
13362 if (Mask & 1) {
13363 APValue ResultVal;
13364 if (!ConvertDoubleToFloatStrict(Info, E, OrigVal: VecB.getVectorElt(I: 0).getFloat(),
13365 Result&: ResultVal))
13366 return false;
13367 Elements.push_back(Elt: ResultVal);
13368 } else {
13369 Elements.push_back(Elt: VecSrc.getVectorElt(I: 0));
13370 }
13371
13372 unsigned NumEltsA = VecA.getVectorLength();
13373 for (unsigned I = 1; I < NumEltsA; ++I) {
13374 Elements.push_back(Elt: VecA.getVectorElt(I));
13375 }
13376
13377 return Success(V: Elements, E);
13378 }
13379 case X86::BI__builtin_ia32_cvtpd2ps:
13380 case X86::BI__builtin_ia32_cvtpd2ps256:
13381 case X86::BI__builtin_ia32_cvtpd2ps_mask:
13382 case X86::BI__builtin_ia32_cvtpd2ps512_mask: {
13383
13384 const auto BuiltinID = E->getBuiltinCallee();
13385 bool IsMasked = (BuiltinID == X86::BI__builtin_ia32_cvtpd2ps_mask ||
13386 BuiltinID == X86::BI__builtin_ia32_cvtpd2ps512_mask);
13387
13388 APValue InputValue;
13389 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: InputValue))
13390 return false;
13391
13392 APValue MergeValue;
13393 unsigned Mask = 0xFFFFFFFF;
13394 bool NeedsMerge = false;
13395 if (IsMasked) {
13396 APValue MaskValue;
13397 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: MaskValue))
13398 return false;
13399 Mask = MaskValue.getInt().getZExtValue();
13400 auto NumEltsResult = E->getType()->getAs<VectorType>()->getNumElements();
13401 for (unsigned I = 0; I < NumEltsResult; ++I) {
13402 if (!((Mask >> I) & 1)) {
13403 NeedsMerge = true;
13404 break;
13405 }
13406 }
13407 if (NeedsMerge) {
13408 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: MergeValue))
13409 return false;
13410 }
13411 }
13412
13413 unsigned NumEltsResult =
13414 E->getType()->getAs<VectorType>()->getNumElements();
13415 unsigned NumEltsInput = InputValue.getVectorLength();
13416 SmallVector<APValue, 8> Elements;
13417 for (unsigned I = 0; I < NumEltsResult; ++I) {
13418 if (IsMasked && !((Mask >> I) & 1)) {
13419 if (!NeedsMerge) {
13420 return false;
13421 }
13422 Elements.push_back(Elt: MergeValue.getVectorElt(I));
13423 continue;
13424 }
13425
13426 if (I >= NumEltsInput) {
13427 Elements.push_back(Elt: APValue(APFloat::getZero(Sem: APFloat::IEEEsingle())));
13428 continue;
13429 }
13430
13431 APValue ResultVal;
13432 if (!ConvertDoubleToFloatStrict(
13433 Info, E, OrigVal: InputValue.getVectorElt(I).getFloat(), Result&: ResultVal))
13434 return false;
13435
13436 Elements.push_back(Elt: ResultVal);
13437 }
13438 return Success(V: Elements, E);
13439 }
13440
13441 case X86::BI__builtin_ia32_shufps:
13442 case X86::BI__builtin_ia32_shufps256:
13443 case X86::BI__builtin_ia32_shufps512: {
13444 APValue R;
13445 if (!evalShuffleGeneric(
13446 Info, Call: E, Out&: R,
13447 GetSourceIndex: [](unsigned DstIdx,
13448 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13449 constexpr unsigned LaneBits = 128u;
13450 unsigned NumElemPerLane = LaneBits / 32;
13451 unsigned NumSelectableElems = NumElemPerLane / 2;
13452 unsigned BitsPerElem = 2;
13453 unsigned IndexMask = (1u << BitsPerElem) - 1;
13454 unsigned MaskBits = 8;
13455 unsigned Lane = DstIdx / NumElemPerLane;
13456 unsigned ElemInLane = DstIdx % NumElemPerLane;
13457 unsigned LaneOffset = Lane * NumElemPerLane;
13458 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13459 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
13460 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
13461 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
13462 }))
13463 return false;
13464 return Success(V: R, E);
13465 }
13466 case X86::BI__builtin_ia32_shufpd:
13467 case X86::BI__builtin_ia32_shufpd256:
13468 case X86::BI__builtin_ia32_shufpd512: {
13469 APValue R;
13470 if (!evalShuffleGeneric(
13471 Info, Call: E, Out&: R,
13472 GetSourceIndex: [](unsigned DstIdx,
13473 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13474 constexpr unsigned LaneBits = 128u;
13475 unsigned NumElemPerLane = LaneBits / 64;
13476 unsigned NumSelectableElems = NumElemPerLane / 2;
13477 unsigned BitsPerElem = 1;
13478 unsigned IndexMask = (1u << BitsPerElem) - 1;
13479 unsigned MaskBits = 8;
13480 unsigned Lane = DstIdx / NumElemPerLane;
13481 unsigned ElemInLane = DstIdx % NumElemPerLane;
13482 unsigned LaneOffset = Lane * NumElemPerLane;
13483 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13484 unsigned SrcIdx = (ElemInLane < NumSelectableElems) ? 0 : 1;
13485 unsigned Index = (ShuffleMask >> BitIndex) & IndexMask;
13486 return {SrcIdx, static_cast<int>(LaneOffset + Index)};
13487 }))
13488 return false;
13489 return Success(V: R, E);
13490 }
13491 case X86::BI__builtin_ia32_insertps128: {
13492 APValue R;
13493 if (!evalShuffleGeneric(
13494 Info, Call: E, Out&: R,
13495 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13496 // Bits [3:0]: zero mask - if bit is set, zero this element
13497 if ((Mask & (1 << DstIdx)) != 0) {
13498 return {0, -1};
13499 }
13500 // Bits [7:6]: select element from source vector Y (0-3)
13501 // Bits [5:4]: select destination position (0-3)
13502 unsigned SrcElem = (Mask >> 6) & 0x3;
13503 unsigned DstElem = (Mask >> 4) & 0x3;
13504 if (DstIdx == DstElem) {
13505 // Insert element from source vector (B) at this position
13506 return {1, static_cast<int>(SrcElem)};
13507 } else {
13508 // Copy from destination vector (A)
13509 return {0, static_cast<int>(DstIdx)};
13510 }
13511 }))
13512 return false;
13513 return Success(V: R, E);
13514 }
13515 case X86::BI__builtin_ia32_pshufb128:
13516 case X86::BI__builtin_ia32_pshufb256:
13517 case X86::BI__builtin_ia32_pshufb512: {
13518 APValue R;
13519 if (!evalShuffleGeneric(
13520 Info, Call: E, Out&: R,
13521 GetSourceIndex: [](unsigned DstIdx,
13522 unsigned ShuffleMask) -> std::pair<unsigned, int> {
13523 uint8_t Ctlb = static_cast<uint8_t>(ShuffleMask);
13524 if (Ctlb & 0x80)
13525 return std::make_pair(x: 0, y: -1);
13526
13527 unsigned LaneBase = (DstIdx / 16) * 16;
13528 unsigned SrcOffset = Ctlb & 0x0F;
13529 unsigned SrcIdx = LaneBase + SrcOffset;
13530 return std::make_pair(x: 0, y: static_cast<int>(SrcIdx));
13531 }))
13532 return false;
13533 return Success(V: R, E);
13534 }
13535
13536 case X86::BI__builtin_ia32_pshuflw:
13537 case X86::BI__builtin_ia32_pshuflw256:
13538 case X86::BI__builtin_ia32_pshuflw512: {
13539 APValue R;
13540 if (!evalShuffleGeneric(
13541 Info, Call: E, Out&: R,
13542 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13543 constexpr unsigned LaneBits = 128u;
13544 constexpr unsigned ElemBits = 16u;
13545 constexpr unsigned LaneElts = LaneBits / ElemBits;
13546 constexpr unsigned HalfSize = 4;
13547 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13548 unsigned LaneIdx = DstIdx % LaneElts;
13549 if (LaneIdx < HalfSize) {
13550 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
13551 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
13552 }
13553 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
13554 }))
13555 return false;
13556 return Success(V: R, E);
13557 }
13558
13559 case X86::BI__builtin_ia32_pshufhw:
13560 case X86::BI__builtin_ia32_pshufhw256:
13561 case X86::BI__builtin_ia32_pshufhw512: {
13562 APValue R;
13563 if (!evalShuffleGeneric(
13564 Info, Call: E, Out&: R,
13565 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13566 constexpr unsigned LaneBits = 128u;
13567 constexpr unsigned ElemBits = 16u;
13568 constexpr unsigned LaneElts = LaneBits / ElemBits;
13569 constexpr unsigned HalfSize = 4;
13570 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13571 unsigned LaneIdx = DstIdx % LaneElts;
13572 if (LaneIdx >= HalfSize) {
13573 unsigned Rel = LaneIdx - HalfSize;
13574 unsigned Sel = (Mask >> (2 * Rel)) & 0x3;
13575 return std::make_pair(
13576 x: 0, y: static_cast<int>(LaneBase + HalfSize + Sel));
13577 }
13578 return std::make_pair(x: 0, y: static_cast<int>(DstIdx));
13579 }))
13580 return false;
13581 return Success(V: R, E);
13582 }
13583
13584 case X86::BI__builtin_ia32_pshufd:
13585 case X86::BI__builtin_ia32_pshufd256:
13586 case X86::BI__builtin_ia32_pshufd512:
13587 case X86::BI__builtin_ia32_vpermilps:
13588 case X86::BI__builtin_ia32_vpermilps256:
13589 case X86::BI__builtin_ia32_vpermilps512: {
13590 APValue R;
13591 if (!evalShuffleGeneric(
13592 Info, Call: E, Out&: R,
13593 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13594 constexpr unsigned LaneBits = 128u;
13595 constexpr unsigned ElemBits = 32u;
13596 constexpr unsigned LaneElts = LaneBits / ElemBits;
13597 unsigned LaneBase = (DstIdx / LaneElts) * LaneElts;
13598 unsigned LaneIdx = DstIdx % LaneElts;
13599 unsigned Sel = (Mask >> (2 * LaneIdx)) & 0x3;
13600 return std::make_pair(x: 0, y: static_cast<int>(LaneBase + Sel));
13601 }))
13602 return false;
13603 return Success(V: R, E);
13604 }
13605
13606 case X86::BI__builtin_ia32_vpermilvarpd:
13607 case X86::BI__builtin_ia32_vpermilvarpd256:
13608 case X86::BI__builtin_ia32_vpermilvarpd512: {
13609 APValue R;
13610 if (!evalShuffleGeneric(
13611 Info, Call: E, Out&: R,
13612 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13613 unsigned NumElemPerLane = 2;
13614 unsigned Lane = DstIdx / NumElemPerLane;
13615 unsigned Offset = Mask & 0b10 ? 1 : 0;
13616 return std::make_pair(
13617 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
13618 }))
13619 return false;
13620 return Success(V: R, E);
13621 }
13622
13623 case X86::BI__builtin_ia32_vpermilpd:
13624 case X86::BI__builtin_ia32_vpermilpd256:
13625 case X86::BI__builtin_ia32_vpermilpd512: {
13626 APValue R;
13627 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
13628 unsigned NumElemPerLane = 2;
13629 unsigned BitsPerElem = 1;
13630 unsigned MaskBits = 8;
13631 unsigned IndexMask = 0x1;
13632 unsigned Lane = DstIdx / NumElemPerLane;
13633 unsigned LaneOffset = Lane * NumElemPerLane;
13634 unsigned BitIndex = (DstIdx * BitsPerElem) % MaskBits;
13635 unsigned Index = (Control >> BitIndex) & IndexMask;
13636 return std::make_pair(x: 0, y: static_cast<int>(LaneOffset + Index));
13637 }))
13638 return false;
13639 return Success(V: R, E);
13640 }
13641
13642 case X86::BI__builtin_ia32_permdf256:
13643 case X86::BI__builtin_ia32_permdi256: {
13644 APValue R;
13645 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Control) {
13646 // permute4x64 operates on 4 64-bit elements
13647 // For element i (0-3), extract bits [2*i+1:2*i] from Control
13648 unsigned Index = (Control >> (2 * DstIdx)) & 0x3;
13649 return std::make_pair(x: 0, y: static_cast<int>(Index));
13650 }))
13651 return false;
13652 return Success(V: R, E);
13653 }
13654
13655 case X86::BI__builtin_ia32_vpermilvarps:
13656 case X86::BI__builtin_ia32_vpermilvarps256:
13657 case X86::BI__builtin_ia32_vpermilvarps512: {
13658 APValue R;
13659 if (!evalShuffleGeneric(
13660 Info, Call: E, Out&: R,
13661 GetSourceIndex: [](unsigned DstIdx, unsigned Mask) -> std::pair<unsigned, int> {
13662 unsigned NumElemPerLane = 4;
13663 unsigned Lane = DstIdx / NumElemPerLane;
13664 unsigned Offset = Mask & 0b11;
13665 return std::make_pair(
13666 x: 0, y: static_cast<int>(Lane * NumElemPerLane + Offset));
13667 }))
13668 return false;
13669 return Success(V: R, E);
13670 }
13671
13672 case X86::BI__builtin_ia32_vpmultishiftqb128:
13673 case X86::BI__builtin_ia32_vpmultishiftqb256:
13674 case X86::BI__builtin_ia32_vpmultishiftqb512: {
13675 assert(E->getNumArgs() == 2);
13676
13677 APValue A, B;
13678 if (!Evaluate(Result&: A, Info, E: E->getArg(Arg: 0)) || !Evaluate(Result&: B, Info, E: E->getArg(Arg: 1)))
13679 return false;
13680
13681 assert(A.getVectorLength() == B.getVectorLength());
13682 unsigned NumBytesInQWord = 8;
13683 unsigned NumBitsInByte = 8;
13684 unsigned NumBytes = A.getVectorLength();
13685 unsigned NumQWords = NumBytes / NumBytesInQWord;
13686 SmallVector<APValue, 64> Result;
13687 Result.reserve(N: NumBytes);
13688
13689 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
13690 APInt BQWord(64, 0);
13691 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
13692 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
13693 uint64_t Byte = B.getVectorElt(I: Idx).getInt().getZExtValue();
13694 BQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
13695 }
13696
13697 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
13698 unsigned Idx = QWordId * NumBytesInQWord + ByteIdx;
13699 uint64_t Ctrl = A.getVectorElt(I: Idx).getInt().getZExtValue() & 0x3F;
13700
13701 APInt Byte(8, 0);
13702 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
13703 Byte.setBitVal(BitPosition: BitIdx, BitValue: BQWord[(Ctrl + BitIdx) & 0x3F]);
13704 }
13705 Result.push_back(Elt: APValue(APSInt(Byte, /*isUnsigned*/ true)));
13706 }
13707 }
13708 return Success(V: APValue(Result.data(), Result.size()), E);
13709 }
13710
13711 case X86::BI__builtin_ia32_phminposuw128: {
13712 APValue Source;
13713 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
13714 return false;
13715 unsigned SourceLen = Source.getVectorLength();
13716 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
13717 QualType ElemQT = VT->getElementType();
13718 unsigned ElemBitWidth = Info.Ctx.getTypeSize(T: ElemQT);
13719
13720 APInt MinIndex(ElemBitWidth, 0);
13721 APInt MinVal = Source.getVectorElt(I: 0).getInt();
13722 for (unsigned I = 1; I != SourceLen; ++I) {
13723 APInt Val = Source.getVectorElt(I).getInt();
13724 if (MinVal.ugt(RHS: Val)) {
13725 MinVal = Val;
13726 MinIndex = I;
13727 }
13728 }
13729
13730 bool ResultUnsigned = E->getCallReturnType(Ctx: Info.Ctx)
13731 ->castAs<VectorType>()
13732 ->getElementType()
13733 ->isUnsignedIntegerOrEnumerationType();
13734
13735 SmallVector<APValue, 8> Result;
13736 Result.reserve(N: SourceLen);
13737 Result.emplace_back(Args: APSInt(MinVal, ResultUnsigned));
13738 Result.emplace_back(Args: APSInt(MinIndex, ResultUnsigned));
13739 for (unsigned I = 0; I != SourceLen - 2; ++I) {
13740 Result.emplace_back(Args: APSInt(APInt(ElemBitWidth, 0), ResultUnsigned));
13741 }
13742 return Success(V: APValue(Result.data(), Result.size()), E);
13743 }
13744
13745 case X86::BI__builtin_ia32_psraq128:
13746 case X86::BI__builtin_ia32_psraq256:
13747 case X86::BI__builtin_ia32_psraq512:
13748 case X86::BI__builtin_ia32_psrad128:
13749 case X86::BI__builtin_ia32_psrad256:
13750 case X86::BI__builtin_ia32_psrad512:
13751 case X86::BI__builtin_ia32_psraw128:
13752 case X86::BI__builtin_ia32_psraw256:
13753 case X86::BI__builtin_ia32_psraw512: {
13754 APValue R;
13755 if (!evalShiftWithCount(
13756 Info, Call: E, Out&: R,
13757 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.ashr(ShiftAmt: Count); },
13758 OverflowOp: [](const APInt &Elt, unsigned Width) {
13759 return Elt.ashr(ShiftAmt: Width - 1);
13760 }))
13761 return false;
13762 return Success(V: R, E);
13763 }
13764
13765 case X86::BI__builtin_ia32_psllq128:
13766 case X86::BI__builtin_ia32_psllq256:
13767 case X86::BI__builtin_ia32_psllq512:
13768 case X86::BI__builtin_ia32_pslld128:
13769 case X86::BI__builtin_ia32_pslld256:
13770 case X86::BI__builtin_ia32_pslld512:
13771 case X86::BI__builtin_ia32_psllw128:
13772 case X86::BI__builtin_ia32_psllw256:
13773 case X86::BI__builtin_ia32_psllw512: {
13774 APValue R;
13775 if (!evalShiftWithCount(
13776 Info, Call: E, Out&: R,
13777 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.shl(shiftAmt: Count); },
13778 OverflowOp: [](const APInt &Elt, unsigned Width) {
13779 return APInt::getZero(numBits: Width);
13780 }))
13781 return false;
13782 return Success(V: R, E);
13783 }
13784
13785 case X86::BI__builtin_ia32_psrlq128:
13786 case X86::BI__builtin_ia32_psrlq256:
13787 case X86::BI__builtin_ia32_psrlq512:
13788 case X86::BI__builtin_ia32_psrld128:
13789 case X86::BI__builtin_ia32_psrld256:
13790 case X86::BI__builtin_ia32_psrld512:
13791 case X86::BI__builtin_ia32_psrlw128:
13792 case X86::BI__builtin_ia32_psrlw256:
13793 case X86::BI__builtin_ia32_psrlw512: {
13794 APValue R;
13795 if (!evalShiftWithCount(
13796 Info, Call: E, Out&: R,
13797 ShiftOp: [](const APInt &Elt, uint64_t Count) { return Elt.lshr(shiftAmt: Count); },
13798 OverflowOp: [](const APInt &Elt, unsigned Width) {
13799 return APInt::getZero(numBits: Width);
13800 }))
13801 return false;
13802 return Success(V: R, E);
13803 }
13804
13805 case X86::BI__builtin_ia32_pternlogd128_mask:
13806 case X86::BI__builtin_ia32_pternlogd256_mask:
13807 case X86::BI__builtin_ia32_pternlogd512_mask:
13808 case X86::BI__builtin_ia32_pternlogq128_mask:
13809 case X86::BI__builtin_ia32_pternlogq256_mask:
13810 case X86::BI__builtin_ia32_pternlogq512_mask: {
13811 APValue AValue, BValue, CValue, ImmValue, UValue;
13812 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
13813 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
13814 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
13815 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
13816 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
13817 return false;
13818
13819 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13820 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13821 APInt Imm = ImmValue.getInt();
13822 APInt U = UValue.getInt();
13823 unsigned ResultLen = AValue.getVectorLength();
13824 SmallVector<APValue, 16> ResultElements;
13825 ResultElements.reserve(N: ResultLen);
13826
13827 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
13828 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
13829 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
13830 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
13831
13832 if (U[EltNum]) {
13833 unsigned BitWidth = ALane.getBitWidth();
13834 APInt ResLane(BitWidth, 0);
13835
13836 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
13837 unsigned ABit = ALane[Bit];
13838 unsigned BBit = BLane[Bit];
13839 unsigned CBit = CLane[Bit];
13840
13841 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
13842 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
13843 }
13844 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
13845 } else {
13846 ResultElements.push_back(Elt: APValue(APSInt(ALane, DestUnsigned)));
13847 }
13848 }
13849 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13850 }
13851 case X86::BI__builtin_ia32_pternlogd128_maskz:
13852 case X86::BI__builtin_ia32_pternlogd256_maskz:
13853 case X86::BI__builtin_ia32_pternlogd512_maskz:
13854 case X86::BI__builtin_ia32_pternlogq128_maskz:
13855 case X86::BI__builtin_ia32_pternlogq256_maskz:
13856 case X86::BI__builtin_ia32_pternlogq512_maskz: {
13857 APValue AValue, BValue, CValue, ImmValue, UValue;
13858 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: AValue) ||
13859 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: BValue) ||
13860 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: CValue) ||
13861 !EvaluateAsRValue(Info, E: E->getArg(Arg: 3), Result&: ImmValue) ||
13862 !EvaluateAsRValue(Info, E: E->getArg(Arg: 4), Result&: UValue))
13863 return false;
13864
13865 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13866 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13867 APInt Imm = ImmValue.getInt();
13868 APInt U = UValue.getInt();
13869 unsigned ResultLen = AValue.getVectorLength();
13870 SmallVector<APValue, 16> ResultElements;
13871 ResultElements.reserve(N: ResultLen);
13872
13873 for (unsigned EltNum = 0; EltNum < ResultLen; ++EltNum) {
13874 APInt ALane = AValue.getVectorElt(I: EltNum).getInt();
13875 APInt BLane = BValue.getVectorElt(I: EltNum).getInt();
13876 APInt CLane = CValue.getVectorElt(I: EltNum).getInt();
13877
13878 unsigned BitWidth = ALane.getBitWidth();
13879 APInt ResLane(BitWidth, 0);
13880
13881 if (U[EltNum]) {
13882 for (unsigned Bit = 0; Bit < BitWidth; ++Bit) {
13883 unsigned ABit = ALane[Bit];
13884 unsigned BBit = BLane[Bit];
13885 unsigned CBit = CLane[Bit];
13886
13887 unsigned Idx = (ABit << 2) | (BBit << 1) | CBit;
13888 ResLane.setBitVal(BitPosition: Bit, BitValue: Imm[Idx]);
13889 }
13890 }
13891 ResultElements.push_back(Elt: APValue(APSInt(ResLane, DestUnsigned)));
13892 }
13893 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13894 }
13895
13896 case Builtin::BI__builtin_elementwise_clzg:
13897 case Builtin::BI__builtin_elementwise_ctzg: {
13898 APValue SourceLHS;
13899 std::optional<APValue> Fallback;
13900 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS))
13901 return false;
13902 if (E->getNumArgs() > 1) {
13903 APValue FallbackTmp;
13904 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: FallbackTmp))
13905 return false;
13906 Fallback = FallbackTmp;
13907 }
13908
13909 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13910 unsigned SourceLen = SourceLHS.getVectorLength();
13911 SmallVector<APValue, 4> ResultElements;
13912 ResultElements.reserve(N: SourceLen);
13913
13914 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13915 APSInt LHS = SourceLHS.getVectorElt(I: EltNum).getInt();
13916 if (!LHS) {
13917 // Without a fallback, a zero element is undefined
13918 if (!Fallback) {
13919 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
13920 << /*IsTrailing=*/(E->getBuiltinCallee() ==
13921 Builtin::BI__builtin_elementwise_ctzg);
13922 return false;
13923 }
13924 ResultElements.push_back(Elt: Fallback->getVectorElt(I: EltNum));
13925 continue;
13926 }
13927 switch (E->getBuiltinCallee()) {
13928 case Builtin::BI__builtin_elementwise_clzg:
13929 ResultElements.push_back(Elt: APValue(
13930 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countl_zero()),
13931 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13932 break;
13933 case Builtin::BI__builtin_elementwise_ctzg:
13934 ResultElements.push_back(Elt: APValue(
13935 APSInt(APInt(Info.Ctx.getIntWidth(T: DestEltTy), LHS.countr_zero()),
13936 DestEltTy->isUnsignedIntegerOrEnumerationType())));
13937 break;
13938 }
13939 }
13940
13941 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13942 }
13943
13944 case Builtin::BI__builtin_elementwise_fma: {
13945 APValue SourceX, SourceY, SourceZ;
13946 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceX) ||
13947 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceY) ||
13948 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceZ))
13949 return false;
13950
13951 unsigned SourceLen = SourceX.getVectorLength();
13952 SmallVector<APValue> ResultElements;
13953 ResultElements.reserve(N: SourceLen);
13954 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
13955 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
13956 const APFloat &X = SourceX.getVectorElt(I: EltNum).getFloat();
13957 const APFloat &Y = SourceY.getVectorElt(I: EltNum).getFloat();
13958 const APFloat &Z = SourceZ.getVectorElt(I: EltNum).getFloat();
13959 APFloat Result(X);
13960 (void)Result.fusedMultiplyAdd(Multiplicand: Y, Addend: Z, RM);
13961 ResultElements.push_back(Elt: APValue(Result));
13962 }
13963 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
13964 }
13965
13966 case clang::X86::BI__builtin_ia32_phaddw128:
13967 case clang::X86::BI__builtin_ia32_phaddw256:
13968 case clang::X86::BI__builtin_ia32_phaddd128:
13969 case clang::X86::BI__builtin_ia32_phaddd256:
13970 case clang::X86::BI__builtin_ia32_phaddsw128:
13971 case clang::X86::BI__builtin_ia32_phaddsw256:
13972
13973 case clang::X86::BI__builtin_ia32_phsubw128:
13974 case clang::X86::BI__builtin_ia32_phsubw256:
13975 case clang::X86::BI__builtin_ia32_phsubd128:
13976 case clang::X86::BI__builtin_ia32_phsubd256:
13977 case clang::X86::BI__builtin_ia32_phsubsw128:
13978 case clang::X86::BI__builtin_ia32_phsubsw256: {
13979 APValue SourceLHS, SourceRHS;
13980 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
13981 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
13982 return false;
13983 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
13984 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
13985
13986 unsigned NumElts = SourceLHS.getVectorLength();
13987 unsigned EltBits = Info.Ctx.getIntWidth(T: DestEltTy);
13988 unsigned EltsPerLane = 128 / EltBits;
13989 SmallVector<APValue, 4> ResultElements;
13990 ResultElements.reserve(N: NumElts);
13991
13992 for (unsigned LaneStart = 0; LaneStart != NumElts;
13993 LaneStart += EltsPerLane) {
13994 for (unsigned I = 0; I != EltsPerLane; I += 2) {
13995 APSInt LHSA = SourceLHS.getVectorElt(I: LaneStart + I).getInt();
13996 APSInt LHSB = SourceLHS.getVectorElt(I: LaneStart + I + 1).getInt();
13997 switch (E->getBuiltinCallee()) {
13998 case clang::X86::BI__builtin_ia32_phaddw128:
13999 case clang::X86::BI__builtin_ia32_phaddw256:
14000 case clang::X86::BI__builtin_ia32_phaddd128:
14001 case clang::X86::BI__builtin_ia32_phaddd256: {
14002 APSInt Res(LHSA + LHSB, DestUnsigned);
14003 ResultElements.push_back(Elt: APValue(Res));
14004 break;
14005 }
14006 case clang::X86::BI__builtin_ia32_phaddsw128:
14007 case clang::X86::BI__builtin_ia32_phaddsw256: {
14008 APSInt Res(LHSA.sadd_sat(RHS: LHSB));
14009 ResultElements.push_back(Elt: APValue(Res));
14010 break;
14011 }
14012 case clang::X86::BI__builtin_ia32_phsubw128:
14013 case clang::X86::BI__builtin_ia32_phsubw256:
14014 case clang::X86::BI__builtin_ia32_phsubd128:
14015 case clang::X86::BI__builtin_ia32_phsubd256: {
14016 APSInt Res(LHSA - LHSB, DestUnsigned);
14017 ResultElements.push_back(Elt: APValue(Res));
14018 break;
14019 }
14020 case clang::X86::BI__builtin_ia32_phsubsw128:
14021 case clang::X86::BI__builtin_ia32_phsubsw256: {
14022 APSInt Res(LHSA.ssub_sat(RHS: LHSB));
14023 ResultElements.push_back(Elt: APValue(Res));
14024 break;
14025 }
14026 }
14027 }
14028 for (unsigned I = 0; I != EltsPerLane; I += 2) {
14029 APSInt RHSA = SourceRHS.getVectorElt(I: LaneStart + I).getInt();
14030 APSInt RHSB = SourceRHS.getVectorElt(I: LaneStart + I + 1).getInt();
14031 switch (E->getBuiltinCallee()) {
14032 case clang::X86::BI__builtin_ia32_phaddw128:
14033 case clang::X86::BI__builtin_ia32_phaddw256:
14034 case clang::X86::BI__builtin_ia32_phaddd128:
14035 case clang::X86::BI__builtin_ia32_phaddd256: {
14036 APSInt Res(RHSA + RHSB, DestUnsigned);
14037 ResultElements.push_back(Elt: APValue(Res));
14038 break;
14039 }
14040 case clang::X86::BI__builtin_ia32_phaddsw128:
14041 case clang::X86::BI__builtin_ia32_phaddsw256: {
14042 APSInt Res(RHSA.sadd_sat(RHS: RHSB));
14043 ResultElements.push_back(Elt: APValue(Res));
14044 break;
14045 }
14046 case clang::X86::BI__builtin_ia32_phsubw128:
14047 case clang::X86::BI__builtin_ia32_phsubw256:
14048 case clang::X86::BI__builtin_ia32_phsubd128:
14049 case clang::X86::BI__builtin_ia32_phsubd256: {
14050 APSInt Res(RHSA - RHSB, DestUnsigned);
14051 ResultElements.push_back(Elt: APValue(Res));
14052 break;
14053 }
14054 case clang::X86::BI__builtin_ia32_phsubsw128:
14055 case clang::X86::BI__builtin_ia32_phsubsw256: {
14056 APSInt Res(RHSA.ssub_sat(RHS: RHSB));
14057 ResultElements.push_back(Elt: APValue(Res));
14058 break;
14059 }
14060 }
14061 }
14062 }
14063 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14064 }
14065 case clang::X86::BI__builtin_ia32_haddpd:
14066 case clang::X86::BI__builtin_ia32_haddps:
14067 case clang::X86::BI__builtin_ia32_haddps256:
14068 case clang::X86::BI__builtin_ia32_haddpd256:
14069 case clang::X86::BI__builtin_ia32_hsubpd:
14070 case clang::X86::BI__builtin_ia32_hsubps:
14071 case clang::X86::BI__builtin_ia32_hsubps256:
14072 case clang::X86::BI__builtin_ia32_hsubpd256: {
14073 APValue SourceLHS, SourceRHS;
14074 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14075 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14076 return false;
14077 unsigned NumElts = SourceLHS.getVectorLength();
14078 SmallVector<APValue, 4> ResultElements;
14079 ResultElements.reserve(N: NumElts);
14080 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14081 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14082 unsigned EltBits = Info.Ctx.getTypeSize(T: DestEltTy);
14083 unsigned NumLanes = NumElts * EltBits / 128;
14084 unsigned NumElemsPerLane = NumElts / NumLanes;
14085 unsigned HalfElemsPerLane = NumElemsPerLane / 2;
14086
14087 for (unsigned L = 0; L != NumElts; L += NumElemsPerLane) {
14088 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14089 APFloat LHSA = SourceLHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14090 APFloat LHSB = SourceLHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14091 switch (E->getBuiltinCallee()) {
14092 case clang::X86::BI__builtin_ia32_haddpd:
14093 case clang::X86::BI__builtin_ia32_haddps:
14094 case clang::X86::BI__builtin_ia32_haddps256:
14095 case clang::X86::BI__builtin_ia32_haddpd256:
14096 LHSA.add(RHS: LHSB, RM);
14097 break;
14098 case clang::X86::BI__builtin_ia32_hsubpd:
14099 case clang::X86::BI__builtin_ia32_hsubps:
14100 case clang::X86::BI__builtin_ia32_hsubps256:
14101 case clang::X86::BI__builtin_ia32_hsubpd256:
14102 LHSA.subtract(RHS: LHSB, RM);
14103 break;
14104 }
14105 ResultElements.push_back(Elt: APValue(LHSA));
14106 }
14107 for (unsigned I = 0; I != HalfElemsPerLane; ++I) {
14108 APFloat RHSA = SourceRHS.getVectorElt(I: L + (2 * I) + 0).getFloat();
14109 APFloat RHSB = SourceRHS.getVectorElt(I: L + (2 * I) + 1).getFloat();
14110 switch (E->getBuiltinCallee()) {
14111 case clang::X86::BI__builtin_ia32_haddpd:
14112 case clang::X86::BI__builtin_ia32_haddps:
14113 case clang::X86::BI__builtin_ia32_haddps256:
14114 case clang::X86::BI__builtin_ia32_haddpd256:
14115 RHSA.add(RHS: RHSB, RM);
14116 break;
14117 case clang::X86::BI__builtin_ia32_hsubpd:
14118 case clang::X86::BI__builtin_ia32_hsubps:
14119 case clang::X86::BI__builtin_ia32_hsubps256:
14120 case clang::X86::BI__builtin_ia32_hsubpd256:
14121 RHSA.subtract(RHS: RHSB, RM);
14122 break;
14123 }
14124 ResultElements.push_back(Elt: APValue(RHSA));
14125 }
14126 }
14127 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14128 }
14129 case clang::X86::BI__builtin_ia32_addsubpd:
14130 case clang::X86::BI__builtin_ia32_addsubps:
14131 case clang::X86::BI__builtin_ia32_addsubpd256:
14132 case clang::X86::BI__builtin_ia32_addsubps256: {
14133 // Addsub: alternates between subtraction and addition
14134 // Result[i] = (i % 2 == 0) ? (a[i] - b[i]) : (a[i] + b[i])
14135 APValue SourceLHS, SourceRHS;
14136 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14137 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14138 return false;
14139 unsigned NumElems = SourceLHS.getVectorLength();
14140 SmallVector<APValue, 8> ResultElements;
14141 ResultElements.reserve(N: NumElems);
14142 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
14143
14144 for (unsigned I = 0; I != NumElems; ++I) {
14145 APFloat LHS = SourceLHS.getVectorElt(I).getFloat();
14146 APFloat RHS = SourceRHS.getVectorElt(I).getFloat();
14147 if (I % 2 == 0) {
14148 // Even indices: subtract
14149 LHS.subtract(RHS, RM);
14150 } else {
14151 // Odd indices: add
14152 LHS.add(RHS, RM);
14153 }
14154 ResultElements.push_back(Elt: APValue(LHS));
14155 }
14156 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14157 }
14158 case clang::X86::BI__builtin_ia32_pclmulqdq128:
14159 case clang::X86::BI__builtin_ia32_pclmulqdq256:
14160 case clang::X86::BI__builtin_ia32_pclmulqdq512: {
14161 // PCLMULQDQ: carry-less multiplication of selected 64-bit halves
14162 // imm8 bit 0: selects lower (0) or upper (1) 64 bits of first operand
14163 // imm8 bit 4: selects lower (0) or upper (1) 64 bits of second operand
14164 APValue SourceLHS, SourceRHS;
14165 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
14166 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
14167 return false;
14168
14169 APSInt Imm8;
14170 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm8, Info))
14171 return false;
14172
14173 // Extract bits 0 and 4 from imm8
14174 bool SelectUpperA = (Imm8 & 0x01) != 0;
14175 bool SelectUpperB = (Imm8 & 0x10) != 0;
14176
14177 unsigned NumElems = SourceLHS.getVectorLength();
14178 SmallVector<APValue, 8> ResultElements;
14179 ResultElements.reserve(N: NumElems);
14180 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14181 bool DestUnsigned = DestEltTy->isUnsignedIntegerOrEnumerationType();
14182
14183 // Process each 128-bit lane
14184 for (unsigned Lane = 0; Lane < NumElems; Lane += 2) {
14185 // Get the two 64-bit halves of the first operand
14186 APSInt A0 = SourceLHS.getVectorElt(I: Lane + 0).getInt();
14187 APSInt A1 = SourceLHS.getVectorElt(I: Lane + 1).getInt();
14188 // Get the two 64-bit halves of the second operand
14189 APSInt B0 = SourceRHS.getVectorElt(I: Lane + 0).getInt();
14190 APSInt B1 = SourceRHS.getVectorElt(I: Lane + 1).getInt();
14191
14192 // Select the appropriate 64-bit values based on imm8
14193 APInt A = SelectUpperA ? A1 : A0;
14194 APInt B = SelectUpperB ? B1 : B0;
14195
14196 // Extend both operands to 128 bits for carry-less multiplication
14197 APInt A128 = A.zext(width: 128);
14198 APInt B128 = B.zext(width: 128);
14199
14200 // Use APIntOps::clmul for carry-less multiplication
14201 APInt Result = llvm::APIntOps::clmul(LHS: A128, RHS: B128);
14202
14203 // Split the 128-bit result into two 64-bit halves
14204 APSInt ResultLow(Result.extractBits(numBits: 64, bitPosition: 0), DestUnsigned);
14205 APSInt ResultHigh(Result.extractBits(numBits: 64, bitPosition: 64), DestUnsigned);
14206
14207 ResultElements.push_back(Elt: APValue(ResultLow));
14208 ResultElements.push_back(Elt: APValue(ResultHigh));
14209 }
14210
14211 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14212 }
14213 case Builtin::BI__builtin_elementwise_clmul:
14214 return EvaluateBinOpExpr(llvm::APIntOps::clmul);
14215 case Builtin::BI__builtin_elementwise_pext:
14216 return EvaluateBinOpExpr(llvm::APIntOps::pext);
14217 case Builtin::BI__builtin_elementwise_pdep:
14218 return EvaluateBinOpExpr(llvm::APIntOps::pdep);
14219 case Builtin::BI__builtin_elementwise_fshl:
14220 case Builtin::BI__builtin_elementwise_fshr: {
14221 APValue SourceHi, SourceLo, SourceShift;
14222 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceHi) ||
14223 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceLo) ||
14224 !EvaluateAsRValue(Info, E: E->getArg(Arg: 2), Result&: SourceShift))
14225 return false;
14226
14227 QualType DestEltTy = E->getType()->castAs<VectorType>()->getElementType();
14228 if (!DestEltTy->isIntegerType())
14229 return false;
14230
14231 unsigned SourceLen = SourceHi.getVectorLength();
14232 SmallVector<APValue> ResultElements;
14233 ResultElements.reserve(N: SourceLen);
14234 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14235 const APSInt &Hi = SourceHi.getVectorElt(I: EltNum).getInt();
14236 const APSInt &Lo = SourceLo.getVectorElt(I: EltNum).getInt();
14237 const APSInt &Shift = SourceShift.getVectorElt(I: EltNum).getInt();
14238 switch (E->getBuiltinCallee()) {
14239 case Builtin::BI__builtin_elementwise_fshl:
14240 ResultElements.push_back(Elt: APValue(
14241 APSInt(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned())));
14242 break;
14243 case Builtin::BI__builtin_elementwise_fshr:
14244 ResultElements.push_back(Elt: APValue(
14245 APSInt(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned())));
14246 break;
14247 }
14248 }
14249
14250 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14251 }
14252
14253 case X86::BI__builtin_ia32_shuf_f32x4_256:
14254 case X86::BI__builtin_ia32_shuf_i32x4_256:
14255 case X86::BI__builtin_ia32_shuf_f64x2_256:
14256 case X86::BI__builtin_ia32_shuf_i64x2_256:
14257 case X86::BI__builtin_ia32_shuf_f32x4:
14258 case X86::BI__builtin_ia32_shuf_i32x4:
14259 case X86::BI__builtin_ia32_shuf_f64x2:
14260 case X86::BI__builtin_ia32_shuf_i64x2: {
14261 APValue SourceA, SourceB;
14262 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceA) ||
14263 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceB))
14264 return false;
14265
14266 APSInt Imm;
14267 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14268 return false;
14269
14270 // Destination and sources A, B all have the same type.
14271 unsigned NumElems = SourceA.getVectorLength();
14272 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14273 QualType ElemQT = VT->getElementType();
14274 unsigned ElemBits = Info.Ctx.getTypeSize(T: ElemQT);
14275 unsigned LaneBits = 128u;
14276 unsigned NumLanes = (NumElems * ElemBits) / LaneBits;
14277 unsigned NumElemsPerLane = LaneBits / ElemBits;
14278
14279 unsigned DstLen = SourceA.getVectorLength();
14280 SmallVector<APValue, 16> ResultElements;
14281 ResultElements.reserve(N: DstLen);
14282
14283 APValue R;
14284 if (!evalShuffleGeneric(
14285 Info, Call: E, Out&: R,
14286 GetSourceIndex: [NumLanes, NumElemsPerLane](unsigned DstIdx, unsigned ShuffleMask)
14287 -> std::pair<unsigned, int> {
14288 // DstIdx determines source. ShuffleMask selects lane in source.
14289 unsigned BitsPerElem = NumLanes / 2;
14290 unsigned IndexMask = (1u << BitsPerElem) - 1;
14291 unsigned Lane = DstIdx / NumElemsPerLane;
14292 unsigned SrcIdx = (Lane < NumLanes / 2) ? 0 : 1;
14293 unsigned BitIdx = BitsPerElem * Lane;
14294 unsigned SrcLaneIdx = (ShuffleMask >> BitIdx) & IndexMask;
14295 unsigned ElemInLane = DstIdx % NumElemsPerLane;
14296 unsigned IdxToPick = SrcLaneIdx * NumElemsPerLane + ElemInLane;
14297 return {SrcIdx, IdxToPick};
14298 }))
14299 return false;
14300 return Success(V: R, E);
14301 }
14302
14303 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14304 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14305 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi:
14306 case X86::BI__builtin_ia32_vgf2p8affineqb_v16qi:
14307 case X86::BI__builtin_ia32_vgf2p8affineqb_v32qi:
14308 case X86::BI__builtin_ia32_vgf2p8affineqb_v64qi: {
14309
14310 APValue X, A;
14311 APSInt Imm;
14312 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: X) ||
14313 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: A) ||
14314 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14315 return false;
14316
14317 assert(X.isVector() && A.isVector());
14318 assert(X.getVectorLength() == A.getVectorLength());
14319
14320 bool IsInverse = false;
14321 switch (E->getBuiltinCallee()) {
14322 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v16qi:
14323 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v32qi:
14324 case X86::BI__builtin_ia32_vgf2p8affineinvqb_v64qi: {
14325 IsInverse = true;
14326 }
14327 }
14328
14329 unsigned NumBitsInByte = 8;
14330 unsigned NumBytesInQWord = 8;
14331 unsigned NumBitsInQWord = 64;
14332 unsigned NumBytes = A.getVectorLength();
14333 unsigned NumQWords = NumBytes / NumBytesInQWord;
14334 SmallVector<APValue, 64> Result;
14335 Result.reserve(N: NumBytes);
14336
14337 // computing A*X + Imm
14338 for (unsigned QWordIdx = 0; QWordIdx != NumQWords; ++QWordIdx) {
14339 // Extract the QWords from X, A
14340 APInt XQWord(NumBitsInQWord, 0);
14341 APInt AQWord(NumBitsInQWord, 0);
14342 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14343 unsigned Idx = QWordIdx * NumBytesInQWord + ByteIdx;
14344 APInt XByte = X.getVectorElt(I: Idx).getInt();
14345 APInt AByte = A.getVectorElt(I: Idx).getInt();
14346 XQWord.insertBits(SubBits: XByte, bitPosition: ByteIdx * NumBitsInByte);
14347 AQWord.insertBits(SubBits: AByte, bitPosition: ByteIdx * NumBitsInByte);
14348 }
14349
14350 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
14351 uint8_t XByte =
14352 XQWord.lshr(shiftAmt: ByteIdx * NumBitsInByte).getLoBits(numBits: 8).getZExtValue();
14353 Result.push_back(Elt: APValue(APSInt(
14354 APInt(8, GFNIAffine(XByte, AQword: AQWord, Imm, Inverse: IsInverse)), false)));
14355 }
14356 }
14357
14358 return Success(V: APValue(Result.data(), Result.size()), E);
14359 }
14360
14361 case X86::BI__builtin_ia32_vgf2p8mulb_v16qi:
14362 case X86::BI__builtin_ia32_vgf2p8mulb_v32qi:
14363 case X86::BI__builtin_ia32_vgf2p8mulb_v64qi: {
14364 APValue A, B;
14365 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: A) ||
14366 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: B))
14367 return false;
14368
14369 assert(A.isVector() && B.isVector());
14370 assert(A.getVectorLength() == B.getVectorLength());
14371
14372 unsigned NumBytes = A.getVectorLength();
14373 SmallVector<APValue, 64> Result;
14374 Result.reserve(N: NumBytes);
14375
14376 for (unsigned ByteIdx = 0; ByteIdx != NumBytes; ++ByteIdx) {
14377 uint8_t AByte = A.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14378 uint8_t BByte = B.getVectorElt(I: ByteIdx).getInt().getZExtValue();
14379 Result.push_back(Elt: APValue(
14380 APSInt(APInt(8, GFNIMul(AByte, BByte)), /*IsUnsigned=*/false)));
14381 }
14382
14383 return Success(V: APValue(Result.data(), Result.size()), E);
14384 }
14385
14386 case X86::BI__builtin_ia32_insertf32x4_256:
14387 case X86::BI__builtin_ia32_inserti32x4_256:
14388 case X86::BI__builtin_ia32_insertf64x2_256:
14389 case X86::BI__builtin_ia32_inserti64x2_256:
14390 case X86::BI__builtin_ia32_insertf32x4:
14391 case X86::BI__builtin_ia32_inserti32x4:
14392 case X86::BI__builtin_ia32_insertf64x2_512:
14393 case X86::BI__builtin_ia32_inserti64x2_512:
14394 case X86::BI__builtin_ia32_insertf32x8:
14395 case X86::BI__builtin_ia32_inserti32x8:
14396 case X86::BI__builtin_ia32_insertf64x4:
14397 case X86::BI__builtin_ia32_inserti64x4:
14398 case X86::BI__builtin_ia32_vinsertf128_ps256:
14399 case X86::BI__builtin_ia32_vinsertf128_pd256:
14400 case X86::BI__builtin_ia32_vinsertf128_si256:
14401 case X86::BI__builtin_ia32_insert128i256: {
14402 APValue SourceDst, SourceSub;
14403 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceDst) ||
14404 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceSub))
14405 return false;
14406
14407 APSInt Imm;
14408 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: Imm, Info))
14409 return false;
14410
14411 assert(SourceDst.isVector() && SourceSub.isVector());
14412 unsigned DstLen = SourceDst.getVectorLength();
14413 unsigned SubLen = SourceSub.getVectorLength();
14414 assert(SubLen != 0 && DstLen != 0 && (DstLen % SubLen) == 0);
14415 unsigned NumLanes = DstLen / SubLen;
14416 unsigned LaneIdx = (Imm.getZExtValue() % NumLanes) * SubLen;
14417
14418 SmallVector<APValue, 16> ResultElements;
14419 ResultElements.reserve(N: DstLen);
14420
14421 for (unsigned EltNum = 0; EltNum < DstLen; ++EltNum) {
14422 if (EltNum >= LaneIdx && EltNum < LaneIdx + SubLen)
14423 ResultElements.push_back(Elt: SourceSub.getVectorElt(I: EltNum - LaneIdx));
14424 else
14425 ResultElements.push_back(Elt: SourceDst.getVectorElt(I: EltNum));
14426 }
14427
14428 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14429 }
14430
14431 case clang::X86::BI__builtin_ia32_vec_set_v4hi:
14432 case clang::X86::BI__builtin_ia32_vec_set_v16qi:
14433 case clang::X86::BI__builtin_ia32_vec_set_v8hi:
14434 case clang::X86::BI__builtin_ia32_vec_set_v4si:
14435 case clang::X86::BI__builtin_ia32_vec_set_v2di:
14436 case clang::X86::BI__builtin_ia32_vec_set_v32qi:
14437 case clang::X86::BI__builtin_ia32_vec_set_v16hi:
14438 case clang::X86::BI__builtin_ia32_vec_set_v8si:
14439 case clang::X86::BI__builtin_ia32_vec_set_v4di: {
14440 APValue VecVal;
14441 APSInt Scalar, IndexAPS;
14442 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: VecVal, Info) ||
14443 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Scalar, Info) ||
14444 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: IndexAPS, Info))
14445 return false;
14446
14447 QualType ElemTy = E->getType()->castAs<VectorType>()->getElementType();
14448 unsigned ElemWidth = Info.Ctx.getIntWidth(T: ElemTy);
14449 bool ElemUnsigned = ElemTy->isUnsignedIntegerOrEnumerationType();
14450 Scalar.setIsUnsigned(ElemUnsigned);
14451 APSInt ElemAPS = Scalar.extOrTrunc(width: ElemWidth);
14452 APValue ElemAV(ElemAPS);
14453
14454 unsigned NumElems = VecVal.getVectorLength();
14455 unsigned Index =
14456 static_cast<unsigned>(IndexAPS.getZExtValue() & (NumElems - 1));
14457
14458 SmallVector<APValue, 4> Elems;
14459 Elems.reserve(N: NumElems);
14460 for (unsigned ElemNum = 0; ElemNum != NumElems; ++ElemNum)
14461 Elems.push_back(Elt: ElemNum == Index ? ElemAV : VecVal.getVectorElt(I: ElemNum));
14462
14463 return Success(V: APValue(Elems.data(), NumElems), E);
14464 }
14465
14466 case X86::BI__builtin_ia32_pslldqi128_byteshift:
14467 case X86::BI__builtin_ia32_pslldqi256_byteshift:
14468 case X86::BI__builtin_ia32_pslldqi512_byteshift: {
14469 APValue R;
14470 if (!evalShuffleGeneric(
14471 Info, Call: E, Out&: R,
14472 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
14473 unsigned LaneBase = (DstIdx / 16) * 16;
14474 unsigned LaneIdx = DstIdx % 16;
14475 if (LaneIdx < Shift)
14476 return std::make_pair(x: 0, y: -1);
14477
14478 return std::make_pair(
14479 x: 0, y: static_cast<int>(LaneBase + LaneIdx - Shift));
14480 }))
14481 return false;
14482 return Success(V: R, E);
14483 }
14484
14485 case X86::BI__builtin_ia32_psrldqi128_byteshift:
14486 case X86::BI__builtin_ia32_psrldqi256_byteshift:
14487 case X86::BI__builtin_ia32_psrldqi512_byteshift: {
14488 APValue R;
14489 if (!evalShuffleGeneric(
14490 Info, Call: E, Out&: R,
14491 GetSourceIndex: [](unsigned DstIdx, unsigned Shift) -> std::pair<unsigned, int> {
14492 unsigned LaneBase = (DstIdx / 16) * 16;
14493 unsigned LaneIdx = DstIdx % 16;
14494 if (LaneIdx + Shift < 16)
14495 return std::make_pair(
14496 x: 0, y: static_cast<int>(LaneBase + LaneIdx + Shift));
14497
14498 return std::make_pair(x: 0, y: -1);
14499 }))
14500 return false;
14501 return Success(V: R, E);
14502 }
14503
14504 case X86::BI__builtin_ia32_palignr128:
14505 case X86::BI__builtin_ia32_palignr256:
14506 case X86::BI__builtin_ia32_palignr512: {
14507 APValue R;
14508 if (!evalShuffleGeneric(Info, Call: E, Out&: R, GetSourceIndex: [](unsigned DstIdx, unsigned Shift) {
14509 // Default to -1 → zero-fill this destination element
14510 unsigned VecIdx = 1;
14511 int ElemIdx = -1;
14512
14513 int Lane = DstIdx / 16;
14514 int Offset = DstIdx % 16;
14515
14516 // Elements come from VecB first, then VecA after the shift boundary
14517 unsigned ShiftedIdx = Offset + (Shift & 0xFF);
14518 if (ShiftedIdx < 16) { // from VecB
14519 ElemIdx = ShiftedIdx + (Lane * 16);
14520 } else if (ShiftedIdx < 32) { // from VecA
14521 VecIdx = 0;
14522 ElemIdx = (ShiftedIdx - 16) + (Lane * 16);
14523 }
14524
14525 return std::pair<unsigned, int>{VecIdx, ElemIdx};
14526 }))
14527 return false;
14528 return Success(V: R, E);
14529 }
14530 case X86::BI__builtin_ia32_alignd128:
14531 case X86::BI__builtin_ia32_alignd256:
14532 case X86::BI__builtin_ia32_alignd512:
14533 case X86::BI__builtin_ia32_alignq128:
14534 case X86::BI__builtin_ia32_alignq256:
14535 case X86::BI__builtin_ia32_alignq512: {
14536 APValue R;
14537 unsigned NumElems = E->getType()->castAs<VectorType>()->getNumElements();
14538 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14539 GetSourceIndex: [NumElems](unsigned DstIdx, unsigned Shift) {
14540 unsigned Imm = Shift & 0xFF;
14541 unsigned EffectiveShift = Imm & (NumElems - 1);
14542 unsigned SourcePos = DstIdx + EffectiveShift;
14543 unsigned VecIdx = SourcePos < NumElems ? 1 : 0;
14544 unsigned ElemIdx = SourcePos & (NumElems - 1);
14545
14546 return std::pair<unsigned, int>{
14547 VecIdx, static_cast<int>(ElemIdx)};
14548 }))
14549 return false;
14550 return Success(V: R, E);
14551 }
14552 case X86::BI__builtin_ia32_permvarsi256:
14553 case X86::BI__builtin_ia32_permvarsf256:
14554 case X86::BI__builtin_ia32_permvardf512:
14555 case X86::BI__builtin_ia32_permvardi512:
14556 case X86::BI__builtin_ia32_permvarhi128: {
14557 APValue R;
14558 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14559 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14560 int Offset = ShuffleMask & 0x7;
14561 return std::pair<unsigned, int>{0, Offset};
14562 }))
14563 return false;
14564 return Success(V: R, E);
14565 }
14566 case X86::BI__builtin_ia32_permvarqi128:
14567 case X86::BI__builtin_ia32_permvarhi256:
14568 case X86::BI__builtin_ia32_permvarsi512:
14569 case X86::BI__builtin_ia32_permvarsf512: {
14570 APValue R;
14571 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14572 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14573 int Offset = ShuffleMask & 0xF;
14574 return std::pair<unsigned, int>{0, Offset};
14575 }))
14576 return false;
14577 return Success(V: R, E);
14578 }
14579 case X86::BI__builtin_ia32_permvardi256:
14580 case X86::BI__builtin_ia32_permvardf256: {
14581 APValue R;
14582 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14583 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14584 int Offset = ShuffleMask & 0x3;
14585 return std::pair<unsigned, int>{0, Offset};
14586 }))
14587 return false;
14588 return Success(V: R, E);
14589 }
14590 case X86::BI__builtin_ia32_permvarqi256:
14591 case X86::BI__builtin_ia32_permvarhi512: {
14592 APValue R;
14593 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14594 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14595 int Offset = ShuffleMask & 0x1F;
14596 return std::pair<unsigned, int>{0, Offset};
14597 }))
14598 return false;
14599 return Success(V: R, E);
14600 }
14601 case X86::BI__builtin_ia32_permvarqi512: {
14602 APValue R;
14603 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14604 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14605 int Offset = ShuffleMask & 0x3F;
14606 return std::pair<unsigned, int>{0, Offset};
14607 }))
14608 return false;
14609 return Success(V: R, E);
14610 }
14611 case X86::BI__builtin_ia32_vpermi2varq128:
14612 case X86::BI__builtin_ia32_vpermi2varpd128: {
14613 APValue R;
14614 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14615 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14616 int Offset = ShuffleMask & 0x1;
14617 unsigned SrcIdx = (ShuffleMask >> 1) & 0x1;
14618 return std::pair<unsigned, int>{SrcIdx, Offset};
14619 }))
14620 return false;
14621 return Success(V: R, E);
14622 }
14623 case X86::BI__builtin_ia32_vpermi2vard128:
14624 case X86::BI__builtin_ia32_vpermi2varps128:
14625 case X86::BI__builtin_ia32_vpermi2varq256:
14626 case X86::BI__builtin_ia32_vpermi2varpd256: {
14627 APValue R;
14628 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14629 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14630 int Offset = ShuffleMask & 0x3;
14631 unsigned SrcIdx = (ShuffleMask >> 2) & 0x1;
14632 return std::pair<unsigned, int>{SrcIdx, Offset};
14633 }))
14634 return false;
14635 return Success(V: R, E);
14636 }
14637 case X86::BI__builtin_ia32_vpermi2varhi128:
14638 case X86::BI__builtin_ia32_vpermi2vard256:
14639 case X86::BI__builtin_ia32_vpermi2varps256:
14640 case X86::BI__builtin_ia32_vpermi2varq512:
14641 case X86::BI__builtin_ia32_vpermi2varpd512: {
14642 APValue R;
14643 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14644 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14645 int Offset = ShuffleMask & 0x7;
14646 unsigned SrcIdx = (ShuffleMask >> 3) & 0x1;
14647 return std::pair<unsigned, int>{SrcIdx, Offset};
14648 }))
14649 return false;
14650 return Success(V: R, E);
14651 }
14652 case X86::BI__builtin_ia32_vpermi2varqi128:
14653 case X86::BI__builtin_ia32_vpermi2varhi256:
14654 case X86::BI__builtin_ia32_vpermi2vard512:
14655 case X86::BI__builtin_ia32_vpermi2varps512: {
14656 APValue R;
14657 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14658 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14659 int Offset = ShuffleMask & 0xF;
14660 unsigned SrcIdx = (ShuffleMask >> 4) & 0x1;
14661 return std::pair<unsigned, int>{SrcIdx, Offset};
14662 }))
14663 return false;
14664 return Success(V: R, E);
14665 }
14666 case X86::BI__builtin_ia32_vpermi2varqi256:
14667 case X86::BI__builtin_ia32_vpermi2varhi512: {
14668 APValue R;
14669 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14670 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14671 int Offset = ShuffleMask & 0x1F;
14672 unsigned SrcIdx = (ShuffleMask >> 5) & 0x1;
14673 return std::pair<unsigned, int>{SrcIdx, Offset};
14674 }))
14675 return false;
14676 return Success(V: R, E);
14677 }
14678 case X86::BI__builtin_ia32_vpermi2varqi512: {
14679 APValue R;
14680 if (!evalShuffleGeneric(Info, Call: E, Out&: R,
14681 GetSourceIndex: [](unsigned DstIdx, unsigned ShuffleMask) {
14682 int Offset = ShuffleMask & 0x3F;
14683 unsigned SrcIdx = (ShuffleMask >> 6) & 0x1;
14684 return std::pair<unsigned, int>{SrcIdx, Offset};
14685 }))
14686 return false;
14687 return Success(V: R, E);
14688 }
14689
14690 case clang::X86::BI__builtin_ia32_minps:
14691 case clang::X86::BI__builtin_ia32_minpd:
14692 case clang::X86::BI__builtin_ia32_minps256:
14693 case clang::X86::BI__builtin_ia32_minpd256:
14694 case clang::X86::BI__builtin_ia32_minps512:
14695 case clang::X86::BI__builtin_ia32_minpd512:
14696 case clang::X86::BI__builtin_ia32_minph128:
14697 case clang::X86::BI__builtin_ia32_minph256:
14698 case clang::X86::BI__builtin_ia32_minph512:
14699 return EvaluateFpBinOpExpr(
14700 [](const APFloat &A, const APFloat &B,
14701 std::optional<APSInt>) -> std::optional<APFloat> {
14702 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
14703 B.isInfinity() || B.isDenormal())
14704 return std::nullopt;
14705 if (A.isZero() && B.isZero())
14706 return B;
14707 return llvm::minimum(A, B);
14708 });
14709
14710 case clang::X86::BI__builtin_ia32_minss:
14711 case clang::X86::BI__builtin_ia32_minsd:
14712 return EvaluateFpBinOpExpr(
14713 [](const APFloat &A, const APFloat &B,
14714 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
14715 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/true);
14716 },
14717 /*IsScalar=*/true);
14718
14719 case clang::X86::BI__builtin_ia32_minsd_round_mask:
14720 case clang::X86::BI__builtin_ia32_minss_round_mask:
14721 case clang::X86::BI__builtin_ia32_minsh_round_mask:
14722 case clang::X86::BI__builtin_ia32_maxsd_round_mask:
14723 case clang::X86::BI__builtin_ia32_maxss_round_mask:
14724 case clang::X86::BI__builtin_ia32_maxsh_round_mask: {
14725 bool IsMin =
14726 E->getBuiltinCallee() ==
14727 clang::X86::BI__builtin_ia32_minsd_round_mask ||
14728 E->getBuiltinCallee() ==
14729 clang::X86::BI__builtin_ia32_minss_round_mask ||
14730 E->getBuiltinCallee() == clang::X86::BI__builtin_ia32_minsh_round_mask;
14731 return EvaluateScalarFpRoundMaskBinOp(
14732 [IsMin](const APFloat &A, const APFloat &B,
14733 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
14734 return EvalScalarMinMaxFp(A, B, RoundingMode, IsMin);
14735 });
14736 }
14737
14738 case clang::X86::BI__builtin_ia32_maxps:
14739 case clang::X86::BI__builtin_ia32_maxpd:
14740 case clang::X86::BI__builtin_ia32_maxps256:
14741 case clang::X86::BI__builtin_ia32_maxpd256:
14742 case clang::X86::BI__builtin_ia32_maxps512:
14743 case clang::X86::BI__builtin_ia32_maxpd512:
14744 case clang::X86::BI__builtin_ia32_maxph128:
14745 case clang::X86::BI__builtin_ia32_maxph256:
14746 case clang::X86::BI__builtin_ia32_maxph512:
14747 return EvaluateFpBinOpExpr(
14748 [](const APFloat &A, const APFloat &B,
14749 std::optional<APSInt>) -> std::optional<APFloat> {
14750 if (A.isNaN() || A.isInfinity() || A.isDenormal() || B.isNaN() ||
14751 B.isInfinity() || B.isDenormal())
14752 return std::nullopt;
14753 if (A.isZero() && B.isZero())
14754 return B;
14755 return llvm::maximum(A, B);
14756 });
14757
14758 case clang::X86::BI__builtin_ia32_maxss:
14759 case clang::X86::BI__builtin_ia32_maxsd:
14760 return EvaluateFpBinOpExpr(
14761 [](const APFloat &A, const APFloat &B,
14762 std::optional<APSInt> RoundingMode) -> std::optional<APFloat> {
14763 return EvalScalarMinMaxFp(A, B, RoundingMode, /*IsMin=*/false);
14764 },
14765 /*IsScalar=*/true);
14766
14767 case clang::X86::BI__builtin_ia32_vcvtps2ph:
14768 case clang::X86::BI__builtin_ia32_vcvtps2ph256: {
14769 APValue SrcVec;
14770 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SrcVec))
14771 return false;
14772
14773 APSInt Imm;
14774 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: Imm, Info))
14775 return false;
14776
14777 const auto *SrcVTy = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
14778 unsigned SrcNumElems = SrcVTy->getNumElements();
14779 const auto *DstVTy = E->getType()->castAs<VectorType>();
14780 unsigned DstNumElems = DstVTy->getNumElements();
14781 QualType DstElemTy = DstVTy->getElementType();
14782
14783 const llvm::fltSemantics &HalfSem =
14784 Info.Ctx.getFloatTypeSemantics(T: Info.Ctx.HalfTy);
14785
14786 int ImmVal = Imm.getZExtValue();
14787 bool UseMXCSR = (ImmVal & 4) != 0;
14788 bool IsFPConstrained =
14789 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained();
14790
14791 llvm::RoundingMode RM;
14792 if (!UseMXCSR) {
14793 switch (ImmVal & 3) {
14794 case 0:
14795 RM = llvm::RoundingMode::NearestTiesToEven;
14796 break;
14797 case 1:
14798 RM = llvm::RoundingMode::TowardNegative;
14799 break;
14800 case 2:
14801 RM = llvm::RoundingMode::TowardPositive;
14802 break;
14803 case 3:
14804 RM = llvm::RoundingMode::TowardZero;
14805 break;
14806 default:
14807 llvm_unreachable("Invalid immediate rounding mode");
14808 }
14809 } else {
14810 RM = llvm::RoundingMode::NearestTiesToEven;
14811 }
14812
14813 SmallVector<APValue, 8> ResultElements;
14814 ResultElements.reserve(N: DstNumElems);
14815
14816 for (unsigned I = 0; I < SrcNumElems; ++I) {
14817 APFloat SrcVal = SrcVec.getVectorElt(I).getFloat();
14818
14819 bool LostInfo;
14820 APFloat::opStatus St = SrcVal.convert(ToSemantics: HalfSem, RM, losesInfo: &LostInfo);
14821
14822 if (UseMXCSR && IsFPConstrained && St != APFloat::opOK) {
14823 Info.FFDiag(E, DiagId: diag::note_constexpr_dynamic_rounding);
14824 return false;
14825 }
14826
14827 APSInt DstInt(SrcVal.bitcastToAPInt(),
14828 DstElemTy->isUnsignedIntegerOrEnumerationType());
14829 ResultElements.push_back(Elt: APValue(DstInt));
14830 }
14831
14832 if (DstNumElems > SrcNumElems) {
14833 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: DstElemTy);
14834 for (unsigned I = SrcNumElems; I < DstNumElems; ++I) {
14835 ResultElements.push_back(Elt: APValue(Zero));
14836 }
14837 }
14838
14839 return Success(V: ResultElements, E);
14840 }
14841 case X86::BI__builtin_ia32_vperm2f128_pd256:
14842 case X86::BI__builtin_ia32_vperm2f128_ps256:
14843 case X86::BI__builtin_ia32_vperm2f128_si256:
14844 case X86::BI__builtin_ia32_permti256: {
14845 unsigned NumElements =
14846 E->getArg(Arg: 0)->getType()->getAs<VectorType>()->getNumElements();
14847 unsigned PreservedBitsCnt = NumElements >> 2;
14848 APValue R;
14849 if (!evalShuffleGeneric(
14850 Info, Call: E, Out&: R,
14851 GetSourceIndex: [PreservedBitsCnt](unsigned DstIdx, unsigned ShuffleMask) {
14852 unsigned ControlBitsCnt = DstIdx >> PreservedBitsCnt << 2;
14853 unsigned ControlBits = ShuffleMask >> ControlBitsCnt;
14854
14855 if (ControlBits & 0b1000)
14856 return std::make_pair(x: 0u, y: -1);
14857
14858 unsigned SrcVecIdx = (ControlBits & 0b10) >> 1;
14859 unsigned PreservedBitsMask = (1 << PreservedBitsCnt) - 1;
14860 int SrcIdx = ((ControlBits & 0b1) << PreservedBitsCnt) |
14861 (DstIdx & PreservedBitsMask);
14862 return std::make_pair(x&: SrcVecIdx, y&: SrcIdx);
14863 }))
14864 return false;
14865 return Success(V: R, E);
14866 }
14867 case X86::BI__builtin_ia32_vpdpwssd128:
14868 case X86::BI__builtin_ia32_vpdpwssd256:
14869 case X86::BI__builtin_ia32_vpdpwssd512:
14870 case X86::BI__builtin_ia32_vpdpbusd128:
14871 case X86::BI__builtin_ia32_vpdpbusd256:
14872 case X86::BI__builtin_ia32_vpdpbusd512:
14873 return EvalVectorDotProduct(false);
14874 case X86::BI__builtin_ia32_vpdpwssds128:
14875 case X86::BI__builtin_ia32_vpdpwssds256:
14876 case X86::BI__builtin_ia32_vpdpwssds512:
14877 case X86::BI__builtin_ia32_vpdpbusds128:
14878 case X86::BI__builtin_ia32_vpdpbusds256:
14879 case X86::BI__builtin_ia32_vpdpbusds512:
14880 return EvalVectorDotProduct(true);
14881 }
14882}
14883
14884bool VectorExprEvaluator::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
14885 APValue Source;
14886 QualType SourceVecType = E->getSrcExpr()->getType();
14887 if (!EvaluateAsRValue(Info, E: E->getSrcExpr(), Result&: Source))
14888 return false;
14889
14890 QualType DestTy = E->getType()->castAs<VectorType>()->getElementType();
14891 QualType SourceTy = SourceVecType->castAs<VectorType>()->getElementType();
14892
14893 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
14894
14895 auto SourceLen = Source.getVectorLength();
14896 SmallVector<APValue, 4> ResultElements;
14897 ResultElements.reserve(N: SourceLen);
14898 for (unsigned EltNum = 0; EltNum < SourceLen; ++EltNum) {
14899 APValue Elt;
14900 if (!handleVectorElementCast(Info, FPO, E, SourceTy, DestTy,
14901 Original: Source.getVectorElt(I: EltNum), Result&: Elt))
14902 return false;
14903 ResultElements.push_back(Elt: std::move(Elt));
14904 }
14905
14906 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14907}
14908
14909static bool handleVectorShuffle(EvalInfo &Info, const ShuffleVectorExpr *E,
14910 QualType ElemType, APValue const &VecVal1,
14911 APValue const &VecVal2, unsigned EltNum,
14912 APValue &Result) {
14913 unsigned const TotalElementsInInputVector1 = VecVal1.getVectorLength();
14914 unsigned const TotalElementsInInputVector2 = VecVal2.getVectorLength();
14915
14916 APSInt IndexVal = E->getShuffleMaskIdx(N: EltNum);
14917 int64_t index = IndexVal.getExtValue();
14918 // The spec says that -1 should be treated as undef for optimizations,
14919 // but in constexpr we'd have to produce an APValue::Indeterminate,
14920 // which is prohibited from being a top-level constant value. Emit a
14921 // diagnostic instead.
14922 if (index == -1) {
14923 Info.FFDiag(
14924 E, DiagId: diag::err_shufflevector_minus_one_is_undefined_behavior_constexpr)
14925 << EltNum;
14926 return false;
14927 }
14928
14929 if (index < 0 ||
14930 index >= TotalElementsInInputVector1 + TotalElementsInInputVector2)
14931 llvm_unreachable("Out of bounds shuffle index");
14932
14933 if (index >= TotalElementsInInputVector1)
14934 Result = VecVal2.getVectorElt(I: index - TotalElementsInInputVector1);
14935 else
14936 Result = VecVal1.getVectorElt(I: index);
14937 return true;
14938}
14939
14940bool VectorExprEvaluator::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
14941 // FIXME: Unary shuffle with mask not currently supported.
14942 if (E->getNumSubExprs() == 2)
14943 return Error(E);
14944 APValue VecVal1;
14945 const Expr *Vec1 = E->getExpr(Index: 0);
14946 if (!EvaluateAsRValue(Info, E: Vec1, Result&: VecVal1))
14947 return false;
14948 APValue VecVal2;
14949 const Expr *Vec2 = E->getExpr(Index: 1);
14950 if (!EvaluateAsRValue(Info, E: Vec2, Result&: VecVal2))
14951 return false;
14952
14953 VectorType const *DestVecTy = E->getType()->castAs<VectorType>();
14954 QualType DestElTy = DestVecTy->getElementType();
14955
14956 auto TotalElementsInOutputVector = DestVecTy->getNumElements();
14957
14958 SmallVector<APValue, 4> ResultElements;
14959 ResultElements.reserve(N: TotalElementsInOutputVector);
14960 for (unsigned EltNum = 0; EltNum < TotalElementsInOutputVector; ++EltNum) {
14961 APValue Elt;
14962 if (!handleVectorShuffle(Info, E, ElemType: DestElTy, VecVal1, VecVal2, EltNum, Result&: Elt))
14963 return false;
14964 ResultElements.push_back(Elt: std::move(Elt));
14965 }
14966
14967 return Success(V: APValue(ResultElements.data(), ResultElements.size()), E);
14968}
14969
14970//===----------------------------------------------------------------------===//
14971// Matrix Evaluation
14972//===----------------------------------------------------------------------===//
14973
14974namespace {
14975class MatrixExprEvaluator : public ExprEvaluatorBase<MatrixExprEvaluator> {
14976 APValue &Result;
14977
14978public:
14979 MatrixExprEvaluator(EvalInfo &Info, APValue &Result)
14980 : ExprEvaluatorBaseTy(Info), Result(Result) {}
14981
14982 bool Success(ArrayRef<APValue> M, const Expr *E) {
14983 auto *CMTy = E->getType()->castAs<ConstantMatrixType>();
14984 assert(M.size() == CMTy->getNumElementsFlattened());
14985 // FIXME: remove this APValue copy.
14986 Result = APValue(M.data(), CMTy->getNumRows(), CMTy->getNumColumns());
14987 return true;
14988 }
14989 bool Success(const APValue &M, const Expr *E) {
14990 assert(M.isMatrix() && "expected matrix");
14991 Result = M;
14992 return true;
14993 }
14994
14995 bool VisitCastExpr(const CastExpr *E);
14996 bool VisitInitListExpr(const InitListExpr *E);
14997};
14998} // end anonymous namespace
14999
15000static bool EvaluateMatrix(const Expr *E, APValue &Result, EvalInfo &Info) {
15001 assert(E->isPRValue() && E->getType()->isConstantMatrixType() &&
15002 "not a matrix prvalue");
15003 return MatrixExprEvaluator(Info, Result).Visit(S: E);
15004}
15005
15006bool MatrixExprEvaluator::VisitCastExpr(const CastExpr *E) {
15007 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15008 unsigned NumRows = MT->getNumRows();
15009 unsigned NumCols = MT->getNumColumns();
15010 unsigned NElts = NumRows * NumCols;
15011 QualType EltTy = MT->getElementType();
15012 const Expr *SE = E->getSubExpr();
15013
15014 switch (E->getCastKind()) {
15015 case CK_HLSLAggregateSplatCast: {
15016 APValue Val;
15017 QualType ValTy;
15018
15019 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15020 return false;
15021
15022 APValue CastedVal;
15023 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15024 if (!handleScalarCast(Info, FPO, E, SourceTy: ValTy, DestTy: EltTy, Original: Val, Result&: CastedVal))
15025 return false;
15026
15027 SmallVector<APValue, 16> SplatEls(NElts, CastedVal);
15028 return Success(M: SplatEls, E);
15029 }
15030 case CK_HLSLElementwiseCast: {
15031 SmallVector<APValue> SrcVals;
15032 SmallVector<QualType> SrcTypes;
15033
15034 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals, SrcTypes))
15035 return false;
15036
15037 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15038 SmallVector<QualType, 16> DestTypes(NElts, EltTy);
15039 SmallVector<APValue, 16> ResultEls(NElts);
15040 if (!handleElementwiseCast(Info, E, FPO, Elements&: SrcVals, SrcTypes, DestTypes,
15041 Results&: ResultEls))
15042 return false;
15043 return Success(M: ResultEls, E);
15044 }
15045 default:
15046 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15047 }
15048}
15049
15050bool MatrixExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
15051 const auto *MT = E->getType()->castAs<ConstantMatrixType>();
15052 QualType EltTy = MT->getElementType();
15053
15054 assert(E->getNumInits() == MT->getNumElementsFlattened() &&
15055 "Expected number of elements in initializer list to match the number "
15056 "of matrix elements");
15057
15058 SmallVector<APValue, 16> Elements;
15059 Elements.reserve(N: MT->getNumElementsFlattened());
15060
15061 // The following loop assumes the elements of the matrix InitListExpr are in
15062 // row-major order, which matches the row-major ordering assumption of the
15063 // matrix APValue.
15064 for (unsigned I = 0, N = MT->getNumElementsFlattened(); I < N; ++I) {
15065 if (EltTy->isIntegerType()) {
15066 llvm::APSInt IntVal;
15067 if (!EvaluateInteger(E: E->getInit(Init: I), Result&: IntVal, Info))
15068 return false;
15069 Elements.push_back(Elt: APValue(IntVal));
15070 } else {
15071 llvm::APFloat FloatVal(0.0);
15072 if (!EvaluateFloat(E: E->getInit(Init: I), Result&: FloatVal, Info))
15073 return false;
15074 Elements.push_back(Elt: APValue(FloatVal));
15075 }
15076 }
15077
15078 return Success(M: Elements, E);
15079}
15080
15081//===----------------------------------------------------------------------===//
15082// Array Evaluation
15083//===----------------------------------------------------------------------===//
15084
15085namespace {
15086 class ArrayExprEvaluator
15087 : public ExprEvaluatorBase<ArrayExprEvaluator> {
15088 const LValue &This;
15089 APValue &Result;
15090 public:
15091
15092 ArrayExprEvaluator(EvalInfo &Info, const LValue &This, APValue &Result)
15093 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
15094
15095 bool Success(const APValue &V, const Expr *E) {
15096 assert(V.isArray() && "expected array");
15097 Result = V;
15098 return true;
15099 }
15100
15101 bool ZeroInitialization(const Expr *E) {
15102 const ConstantArrayType *CAT =
15103 Info.Ctx.getAsConstantArrayType(T: E->getType());
15104 if (!CAT) {
15105 if (E->getType()->isIncompleteArrayType()) {
15106 // We can be asked to zero-initialize a flexible array member; this
15107 // is represented as an ImplicitValueInitExpr of incomplete array
15108 // type. In this case, the array has zero elements.
15109 Result = APValue(APValue::UninitArray(), 0, 0);
15110 return true;
15111 }
15112 // FIXME: We could handle VLAs here.
15113 return Error(E);
15114 }
15115
15116 Result = APValue(APValue::UninitArray(), 0, CAT->getZExtSize());
15117 if (!Result.hasArrayFiller())
15118 return true;
15119
15120 // Zero-initialize all elements.
15121 LValue Subobject = This;
15122 Subobject.addArray(Info, E, CAT);
15123 ImplicitValueInitExpr VIE(CAT->getElementType());
15124 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject, E: &VIE);
15125 }
15126
15127 bool VisitCallExpr(const CallExpr *E) {
15128 return handleCallExpr(E, Result, ResultSlot: &This);
15129 }
15130 bool VisitCastExpr(const CastExpr *E);
15131 bool VisitInitListExpr(const InitListExpr *E,
15132 QualType AllocType = QualType());
15133 bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E);
15134 bool VisitCXXConstructExpr(const CXXConstructExpr *E);
15135 bool VisitCXXConstructExpr(const CXXConstructExpr *E,
15136 const LValue &Subobject,
15137 APValue *Value, QualType Type);
15138 bool VisitStringLiteral(const StringLiteral *E,
15139 QualType AllocType = QualType()) {
15140 expandStringLiteral(Info, S: E, Result, AllocType);
15141 return true;
15142 }
15143 bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E);
15144 bool VisitCXXParenListOrInitListExpr(const Expr *ExprToVisit,
15145 ArrayRef<Expr *> Args,
15146 const Expr *ArrayFiller,
15147 QualType AllocType = QualType());
15148 bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E);
15149 };
15150} // end anonymous namespace
15151
15152static bool EvaluateArray(const Expr *E, const LValue &This,
15153 APValue &Result, EvalInfo &Info) {
15154 assert(!E->isValueDependent());
15155 assert(E->isPRValue() && E->getType()->isArrayType() &&
15156 "not an array prvalue");
15157 return ArrayExprEvaluator(Info, This, Result).Visit(S: E);
15158}
15159
15160static bool EvaluateArrayNewInitList(EvalInfo &Info, LValue &This,
15161 APValue &Result, const InitListExpr *ILE,
15162 QualType AllocType) {
15163 assert(!ILE->isValueDependent());
15164 assert(ILE->isPRValue() && ILE->getType()->isArrayType() &&
15165 "not an array prvalue");
15166 return ArrayExprEvaluator(Info, This, Result)
15167 .VisitInitListExpr(E: ILE, AllocType);
15168}
15169
15170static bool EvaluateArrayNewConstructExpr(EvalInfo &Info, LValue &This,
15171 APValue &Result,
15172 const CXXConstructExpr *CCE,
15173 QualType AllocType) {
15174 assert(!CCE->isValueDependent());
15175 assert(CCE->isPRValue() && CCE->getType()->isArrayType() &&
15176 "not an array prvalue");
15177 return ArrayExprEvaluator(Info, This, Result)
15178 .VisitCXXConstructExpr(E: CCE, Subobject: This, Value: &Result, Type: AllocType);
15179}
15180
15181// Return true iff the given array filler may depend on the element index.
15182static bool MaybeElementDependentArrayFiller(const Expr *FillerExpr) {
15183 // For now, just allow non-class value-initialization and initialization
15184 // lists comprised of them.
15185 if (isa<ImplicitValueInitExpr>(Val: FillerExpr))
15186 return false;
15187 if (const InitListExpr *ILE = dyn_cast<InitListExpr>(Val: FillerExpr)) {
15188 for (unsigned I = 0, E = ILE->getNumInits(); I != E; ++I) {
15189 if (MaybeElementDependentArrayFiller(FillerExpr: ILE->getInit(Init: I)))
15190 return true;
15191 }
15192
15193 if (ILE->hasArrayFiller() &&
15194 MaybeElementDependentArrayFiller(FillerExpr: ILE->getArrayFiller()))
15195 return true;
15196
15197 return false;
15198 }
15199 return true;
15200}
15201
15202bool ArrayExprEvaluator::VisitCastExpr(const CastExpr *E) {
15203 const Expr *SE = E->getSubExpr();
15204
15205 switch (E->getCastKind()) {
15206 default:
15207 return ExprEvaluatorBaseTy::VisitCastExpr(E);
15208 case CK_HLSLAggregateSplatCast: {
15209 APValue Val;
15210 QualType ValTy;
15211
15212 if (!hlslAggSplatHelper(Info, E: SE, SrcVal&: Val, SrcTy&: ValTy))
15213 return false;
15214
15215 unsigned NEls = elementwiseSize(Info, BaseTy: E->getType());
15216
15217 SmallVector<APValue> SplatEls(NEls, Val);
15218 SmallVector<QualType> SplatType(NEls, ValTy);
15219
15220 // cast the elements
15221 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15222 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SplatEls,
15223 ElTypes&: SplatType))
15224 return false;
15225
15226 return true;
15227 }
15228 case CK_HLSLElementwiseCast: {
15229 SmallVector<APValue> SrcEls;
15230 SmallVector<QualType> SrcTypes;
15231
15232 if (!hlslElementwiseCastHelper(Info, E: SE, DestTy: E->getType(), SrcVals&: SrcEls, SrcTypes))
15233 return false;
15234
15235 // cast the elements
15236 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15237 if (!constructAggregate(Info, FPO, E, Result, ResultType: E->getType(), Elements&: SrcEls,
15238 ElTypes&: SrcTypes))
15239 return false;
15240 return true;
15241 }
15242 }
15243}
15244
15245bool ArrayExprEvaluator::VisitInitListExpr(const InitListExpr *E,
15246 QualType AllocType) {
15247 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15248 T: AllocType.isNull() ? E->getType() : AllocType);
15249 if (!CAT)
15250 return Error(E);
15251
15252 // C++11 [dcl.init.string]p1: A char array [...] can be initialized by [...]
15253 // an appropriately-typed string literal enclosed in braces.
15254 if (E->isStringLiteralInit()) {
15255 auto *SL = dyn_cast<StringLiteral>(Val: E->getInit(Init: 0)->IgnoreParenImpCasts());
15256 // FIXME: Support ObjCEncodeExpr here once we support it in
15257 // ArrayExprEvaluator generally.
15258 if (!SL)
15259 return Error(E);
15260 return VisitStringLiteral(E: SL, AllocType);
15261 }
15262 // Any other transparent list init will need proper handling of the
15263 // AllocType; we can't just recurse to the inner initializer.
15264 assert(!E->isTransparent() &&
15265 "transparent array list initialization is not string literal init?");
15266
15267 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->inits(), ArrayFiller: E->getArrayFiller(),
15268 AllocType);
15269}
15270
15271bool ArrayExprEvaluator::VisitCXXParenListOrInitListExpr(
15272 const Expr *ExprToVisit, ArrayRef<Expr *> Args, const Expr *ArrayFiller,
15273 QualType AllocType) {
15274 const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(
15275 T: AllocType.isNull() ? ExprToVisit->getType() : AllocType);
15276
15277 bool Success = true;
15278
15279 unsigned NumEltsToInit = Args.size();
15280 unsigned NumElts = CAT->getZExtSize();
15281
15282 // If the initializer might depend on the array index, run it for each
15283 // array element.
15284 if (NumEltsToInit != NumElts &&
15285 MaybeElementDependentArrayFiller(FillerExpr: ArrayFiller)) {
15286 NumEltsToInit = NumElts;
15287 } else {
15288 // Add additional elements represented by EmbedExpr.
15289 for (auto *Init : Args) {
15290 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts()))
15291 NumEltsToInit += EmbedS->getDataElementCount() - 1;
15292 }
15293 // If we have extra elements in the list, they will be discarded.
15294 if (NumEltsToInit > NumElts)
15295 NumEltsToInit = NumElts;
15296 // If we're overwriting memory which already has an object, make sure we
15297 // don't reduce the number of non-filler elements. (It's possible to
15298 // optimize this in some cases, but the logic gets really complicated.)
15299 if (Result.hasValue() && NumEltsToInit < Result.getArrayInitializedElts())
15300 NumEltsToInit = Result.getArrayInitializedElts();
15301 }
15302
15303 LLVM_DEBUG(llvm::dbgs() << "The number of elements to initialize: "
15304 << NumEltsToInit << ".\n");
15305
15306 if (!Result.hasValue()) {
15307 Result = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15308 } else if (Result.getArrayInitializedElts() != NumEltsToInit) {
15309 // Number of inititalized elts changed. Recreate the APValue, and copy over
15310 // the relevant elements. (This is essentially just fixing the internal
15311 // representation of the value, because it's tied to the number of
15312 // non-filler elements.)
15313 //
15314 // This should be hit rarely, but there are some edge cases:
15315 //
15316 // - The array could be zero-initialized.
15317 // - There could be a DesignatedInitListExpr.
15318 // - operator new[] can be used to start the lifetime early.
15319 APValue NewResult = APValue(APValue::UninitArray(), NumEltsToInit, NumElts);
15320 // First copy existing elements.
15321 unsigned NumOldElts = Result.getArrayInitializedElts();
15322 for (unsigned I = 0; I < NumOldElts; ++I) {
15323 NewResult.getArrayInitializedElt(I) =
15324 std::move(Result.getArrayInitializedElt(I));
15325 }
15326 // Then copy the array filler over the remaining elements.
15327 for (unsigned I = Result.getArrayInitializedElts(); I < NumEltsToInit; ++I)
15328 NewResult.getArrayInitializedElt(I) = Result.getArrayFiller();
15329 if (NewResult.hasArrayFiller() && Result.hasArrayFiller())
15330 NewResult.getArrayFiller() = Result.getArrayFiller();
15331 Result = std::move(NewResult);
15332 }
15333
15334 LValue Subobject = This;
15335 Subobject.addArray(Info, E: ExprToVisit, CAT);
15336 auto Eval = [&](const Expr *Init, unsigned ArrayIndex) {
15337 if (Init->isValueDependent())
15338 return EvaluateDependentExpr(E: Init, Info);
15339
15340 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
15341 // aren't supposed to be modified.
15342 if (isa<NoInitExpr>(Val: Init))
15343 return true;
15344
15345 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: ArrayIndex), Info,
15346 This: Subobject, E: Init) ||
15347 !HandleLValueArrayAdjustment(Info, E: Init, LVal&: Subobject,
15348 EltTy: CAT->getElementType(), Adjustment: 1)) {
15349 if (!Info.noteFailure())
15350 return false;
15351 Success = false;
15352 }
15353 return true;
15354 };
15355 unsigned ArrayIndex = 0;
15356 QualType DestTy = CAT->getElementType();
15357 APSInt Value(Info.Ctx.getTypeSize(T: DestTy), DestTy->isUnsignedIntegerType());
15358 for (unsigned Index = 0; Index != NumEltsToInit; ++Index) {
15359 const Expr *Init = Index < Args.size() ? Args[Index] : ArrayFiller;
15360 if (ArrayIndex >= NumEltsToInit)
15361 break;
15362 if (auto *EmbedS = dyn_cast<EmbedExpr>(Val: Init->IgnoreParenImpCasts())) {
15363 StringLiteral *SL = EmbedS->getDataStringLiteral();
15364 for (unsigned I = EmbedS->getStartingElementPos(),
15365 N = EmbedS->getDataElementCount();
15366 I != EmbedS->getStartingElementPos() + N; ++I) {
15367 Value = SL->getCodeUnit(i: I);
15368 if (DestTy->isIntegerType()) {
15369 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(Value);
15370 } else {
15371 assert(DestTy->isFloatingType() && "unexpected type");
15372 const FPOptions FPO =
15373 Init->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
15374 APFloat FValue(0.0);
15375 if (!HandleIntToFloatCast(Info, E: Init, FPO, SrcType: EmbedS->getType(), Value,
15376 DestType: DestTy, Result&: FValue))
15377 return false;
15378 Result.getArrayInitializedElt(I: ArrayIndex) = APValue(FValue);
15379 }
15380 ArrayIndex++;
15381 }
15382 } else {
15383 if (!Eval(Init, ArrayIndex))
15384 return false;
15385 ++ArrayIndex;
15386 }
15387 }
15388
15389 if (!Result.hasArrayFiller())
15390 return Success;
15391
15392 // If we get here, we have a trivial filler, which we can just evaluate
15393 // once and splat over the rest of the array elements.
15394 assert(ArrayFiller && "no array filler for incomplete init list");
15395 return EvaluateInPlace(Result&: Result.getArrayFiller(), Info, This: Subobject,
15396 E: ArrayFiller) &&
15397 Success;
15398}
15399
15400bool ArrayExprEvaluator::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
15401 LValue CommonLV;
15402 if (E->getCommonExpr() &&
15403 !Evaluate(Result&: Info.CurrentCall->createTemporary(
15404 Key: E->getCommonExpr(),
15405 T: getStorageType(Ctx: Info.Ctx, E: E->getCommonExpr()),
15406 Scope: ScopeKind::FullExpression, LV&: CommonLV),
15407 Info, E: E->getCommonExpr()->getSourceExpr()))
15408 return false;
15409
15410 auto *CAT = cast<ConstantArrayType>(Val: E->getType()->castAsArrayTypeUnsafe());
15411
15412 uint64_t Elements = CAT->getZExtSize();
15413 Result = APValue(APValue::UninitArray(), Elements, Elements);
15414
15415 LValue Subobject = This;
15416 Subobject.addArray(Info, E, CAT);
15417
15418 bool Success = true;
15419 for (EvalInfo::ArrayInitLoopIndex Index(Info); Index != Elements; ++Index) {
15420 // C++ [class.temporary]/5
15421 // There are four contexts in which temporaries are destroyed at a different
15422 // point than the end of the full-expression. [...] The second context is
15423 // when a copy constructor is called to copy an element of an array while
15424 // the entire array is copied [...]. In either case, if the constructor has
15425 // one or more default arguments, the destruction of every temporary created
15426 // in a default argument is sequenced before the construction of the next
15427 // array element, if any.
15428 FullExpressionRAII Scope(Info);
15429
15430 if (!EvaluateInPlace(Result&: Result.getArrayInitializedElt(I: Index),
15431 Info, This: Subobject, E: E->getSubExpr()) ||
15432 !HandleLValueArrayAdjustment(Info, E, LVal&: Subobject,
15433 EltTy: CAT->getElementType(), Adjustment: 1)) {
15434 if (!Info.noteFailure())
15435 return false;
15436 Success = false;
15437 }
15438
15439 // Make sure we run the destructors too.
15440 Scope.destroy();
15441 }
15442
15443 return Success;
15444}
15445
15446bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E) {
15447 return VisitCXXConstructExpr(E, Subobject: This, Value: &Result, Type: E->getType());
15448}
15449
15450bool ArrayExprEvaluator::VisitCXXConstructExpr(const CXXConstructExpr *E,
15451 const LValue &Subobject,
15452 APValue *Value,
15453 QualType Type) {
15454 bool HadZeroInit = Value->hasValue();
15455
15456 if (const ConstantArrayType *CAT = Info.Ctx.getAsConstantArrayType(T: Type)) {
15457 unsigned FinalSize = CAT->getZExtSize();
15458
15459 // Preserve the array filler if we had prior zero-initialization.
15460 APValue Filler =
15461 HadZeroInit && Value->hasArrayFiller() ? Value->getArrayFiller()
15462 : APValue();
15463
15464 *Value = APValue(APValue::UninitArray(), 0, FinalSize);
15465 if (FinalSize == 0)
15466 return true;
15467
15468 bool HasTrivialConstructor = CheckTrivialDefaultConstructor(
15469 Info, Loc: E->getExprLoc(), CD: E->getConstructor(),
15470 IsValueInitialization: E->requiresZeroInitialization());
15471 LValue ArrayElt = Subobject;
15472 ArrayElt.addArray(Info, E, CAT);
15473 // We do the whole initialization in two passes, first for just one element,
15474 // then for the whole array. It's possible we may find out we can't do const
15475 // init in the first pass, in which case we avoid allocating a potentially
15476 // large array. We don't do more passes because expanding array requires
15477 // copying the data, which is wasteful.
15478 for (const unsigned N : {1u, FinalSize}) {
15479 unsigned OldElts = Value->getArrayInitializedElts();
15480 if (OldElts == N)
15481 break;
15482
15483 // Expand the array to appropriate size.
15484 APValue NewValue(APValue::UninitArray(), N, FinalSize);
15485 for (unsigned I = 0; I < OldElts; ++I)
15486 NewValue.getArrayInitializedElt(I).swap(
15487 RHS&: Value->getArrayInitializedElt(I));
15488 Value->swap(RHS&: NewValue);
15489
15490 if (HadZeroInit)
15491 for (unsigned I = OldElts; I < N; ++I)
15492 Value->getArrayInitializedElt(I) = Filler;
15493
15494 if (HasTrivialConstructor && N == FinalSize && FinalSize != 1) {
15495 // If we have a trivial constructor, only evaluate it once and copy
15496 // the result into all the array elements.
15497 APValue &FirstResult = Value->getArrayInitializedElt(I: 0);
15498 for (unsigned I = OldElts; I < FinalSize; ++I)
15499 Value->getArrayInitializedElt(I) = FirstResult;
15500 } else {
15501 for (unsigned I = OldElts; I < N; ++I) {
15502 if (!VisitCXXConstructExpr(E, Subobject: ArrayElt,
15503 Value: &Value->getArrayInitializedElt(I),
15504 Type: CAT->getElementType()) ||
15505 !HandleLValueArrayAdjustment(Info, E, LVal&: ArrayElt,
15506 EltTy: CAT->getElementType(), Adjustment: 1))
15507 return false;
15508 // When checking for const initilization any diagnostic is considered
15509 // an error.
15510 if (Info.EvalStatus.Diag && !Info.EvalStatus.Diag->empty() &&
15511 !Info.keepEvaluatingAfterFailure())
15512 return false;
15513 }
15514 }
15515 }
15516
15517 return true;
15518 }
15519
15520 if (!Type->isRecordType())
15521 return Error(E);
15522
15523 return RecordExprEvaluator(Info, Subobject, *Value)
15524 .VisitCXXConstructExpr(E, T: Type);
15525}
15526
15527bool ArrayExprEvaluator::VisitCXXParenListInitExpr(
15528 const CXXParenListInitExpr *E) {
15529 assert(E->getType()->isConstantArrayType() &&
15530 "Expression result is not a constant array type");
15531
15532 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs(),
15533 ArrayFiller: E->getArrayFiller());
15534}
15535
15536bool ArrayExprEvaluator::VisitDesignatedInitUpdateExpr(
15537 const DesignatedInitUpdateExpr *E) {
15538 if (!Visit(S: E->getBase()))
15539 return false;
15540 return Visit(S: E->getUpdater());
15541}
15542
15543//===----------------------------------------------------------------------===//
15544// Integer Evaluation
15545//
15546// As a GNU extension, we support casting pointers to sufficiently-wide integer
15547// types and back in constant folding. Integer values are thus represented
15548// either as an integer-valued APValue, or as an lvalue-valued APValue.
15549//===----------------------------------------------------------------------===//
15550
15551namespace {
15552class IntExprEvaluator
15553 : public ExprEvaluatorBase<IntExprEvaluator> {
15554 APValue &Result;
15555public:
15556 IntExprEvaluator(EvalInfo &info, APValue &result)
15557 : ExprEvaluatorBaseTy(info), Result(result) {}
15558
15559 bool Success(const llvm::APSInt &SI, const Expr *E, APValue &Result) {
15560 assert(E->getType()->isIntegralOrEnumerationType() &&
15561 "Invalid evaluation result.");
15562 assert(SI.isSigned() == E->getType()->isSignedIntegerOrEnumerationType() &&
15563 "Invalid evaluation result.");
15564 assert(SI.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
15565 "Invalid evaluation result.");
15566 Result = APValue(SI);
15567 return true;
15568 }
15569 bool Success(const llvm::APSInt &SI, const Expr *E) {
15570 return Success(SI, E, Result);
15571 }
15572
15573 bool Success(const llvm::APInt &I, const Expr *E, APValue &Result) {
15574 assert(E->getType()->isIntegralOrEnumerationType() &&
15575 "Invalid evaluation result.");
15576 assert(I.getBitWidth() == Info.Ctx.getIntWidth(E->getType()) &&
15577 "Invalid evaluation result.");
15578 Result = APValue(APSInt(I));
15579 Result.getInt().setIsUnsigned(
15580 E->getType()->isUnsignedIntegerOrEnumerationType());
15581 return true;
15582 }
15583 bool Success(const llvm::APInt &I, const Expr *E) {
15584 return Success(I, E, Result);
15585 }
15586
15587 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
15588 assert(E->getType()->isIntegralOrEnumerationType() &&
15589 "Invalid evaluation result.");
15590 Result = APValue(Info.Ctx.MakeIntValue(Value, Type: E->getType()));
15591 return true;
15592 }
15593 bool Success(uint64_t Value, const Expr *E) {
15594 return Success(Value, E, Result);
15595 }
15596
15597 bool Success(CharUnits Size, const Expr *E) {
15598 return Success(Value: Size.getQuantity(), E);
15599 }
15600
15601 bool Success(const APValue &V, const Expr *E) {
15602 // C++23 [expr.const]p8 If we have a variable that is unknown reference or
15603 // pointer allow further evaluation of the value.
15604 if (V.isLValue() || V.isAddrLabelDiff() || V.isIndeterminate() ||
15605 V.allowConstexprUnknown()) {
15606 Result = V;
15607 return true;
15608 }
15609 return Success(SI: V.getInt(), E);
15610 }
15611
15612 bool ZeroInitialization(const Expr *E) { return Success(Value: 0, E); }
15613
15614 friend std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &,
15615 const CallExpr *);
15616
15617 //===--------------------------------------------------------------------===//
15618 // Visitor Methods
15619 //===--------------------------------------------------------------------===//
15620
15621 bool VisitIntegerLiteral(const IntegerLiteral *E) {
15622 return Success(I: E->getValue(), E);
15623 }
15624 bool VisitCharacterLiteral(const CharacterLiteral *E) {
15625 return Success(Value: E->getValue(), E);
15626 }
15627
15628 bool CheckReferencedDecl(const Expr *E, const Decl *D);
15629 bool VisitDeclRefExpr(const DeclRefExpr *E) {
15630 if (CheckReferencedDecl(E, D: E->getDecl()))
15631 return true;
15632
15633 return ExprEvaluatorBaseTy::VisitDeclRefExpr(S: E);
15634 }
15635 bool VisitMemberExpr(const MemberExpr *E) {
15636 if (CheckReferencedDecl(E, D: E->getMemberDecl())) {
15637 VisitIgnoredBaseExpression(E: E->getBase());
15638 return true;
15639 }
15640
15641 return ExprEvaluatorBaseTy::VisitMemberExpr(E);
15642 }
15643
15644 bool VisitCallExpr(const CallExpr *E);
15645 bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinOp);
15646 bool VisitBinaryOperator(const BinaryOperator *E);
15647 bool VisitOffsetOfExpr(const OffsetOfExpr *E);
15648 bool VisitUnaryOperator(const UnaryOperator *E);
15649
15650 bool VisitCastExpr(const CastExpr* E);
15651 bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E);
15652
15653 bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
15654 return Success(Value: E->getValue(), E);
15655 }
15656
15657 bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
15658 return Success(Value: E->getValue(), E);
15659 }
15660
15661 bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
15662 if (Info.ArrayInitIndex == uint64_t(-1)) {
15663 // We were asked to evaluate this subexpression independent of the
15664 // enclosing ArrayInitLoopExpr. We can't do that.
15665 Info.FFDiag(E);
15666 return false;
15667 }
15668 return Success(Value: Info.ArrayInitIndex, E);
15669 }
15670
15671 // Note, GNU defines __null as an integer, not a pointer.
15672 bool VisitGNUNullExpr(const GNUNullExpr *E) {
15673 return ZeroInitialization(E);
15674 }
15675
15676 bool VisitTypeTraitExpr(const TypeTraitExpr *E) {
15677 if (E->isStoredAsBoolean())
15678 return Success(Value: E->getBoolValue(), E);
15679 if (E->getAPValue().isAbsent())
15680 return false;
15681 assert(E->getAPValue().isInt() && "APValue type not supported");
15682 return Success(SI: E->getAPValue().getInt(), E);
15683 }
15684
15685 bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
15686 return Success(Value: E->getValue(), E);
15687 }
15688
15689 bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
15690 return Success(Value: E->getValue(), E);
15691 }
15692
15693 bool VisitOpenACCAsteriskSizeExpr(const OpenACCAsteriskSizeExpr *E) {
15694 // This should not be evaluated during constant expr evaluation, as it
15695 // should always be in an unevaluated context (the args list of a 'gang' or
15696 // 'tile' clause).
15697 return Error(E);
15698 }
15699
15700 bool VisitUnaryReal(const UnaryOperator *E);
15701 bool VisitUnaryImag(const UnaryOperator *E);
15702
15703 bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E);
15704 bool VisitSizeOfPackExpr(const SizeOfPackExpr *E);
15705 bool VisitSourceLocExpr(const SourceLocExpr *E);
15706 bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E);
15707 bool VisitRequiresExpr(const RequiresExpr *E);
15708 // FIXME: Missing: array subscript of vector, member of vector
15709};
15710
15711class FixedPointExprEvaluator
15712 : public ExprEvaluatorBase<FixedPointExprEvaluator> {
15713 APValue &Result;
15714
15715 public:
15716 FixedPointExprEvaluator(EvalInfo &info, APValue &result)
15717 : ExprEvaluatorBaseTy(info), Result(result) {}
15718
15719 bool Success(const llvm::APInt &I, const Expr *E) {
15720 return Success(
15721 V: APFixedPoint(I, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
15722 }
15723
15724 bool Success(uint64_t Value, const Expr *E) {
15725 return Success(
15726 V: APFixedPoint(Value, Info.Ctx.getFixedPointSemantics(Ty: E->getType())), E);
15727 }
15728
15729 bool Success(const APValue &V, const Expr *E) {
15730 return Success(V: V.getFixedPoint(), E);
15731 }
15732
15733 bool Success(const APFixedPoint &V, const Expr *E) {
15734 assert(E->getType()->isFixedPointType() && "Invalid evaluation result.");
15735 assert(V.getWidth() == Info.Ctx.getIntWidth(E->getType()) &&
15736 "Invalid evaluation result.");
15737 Result = APValue(V);
15738 return true;
15739 }
15740
15741 bool ZeroInitialization(const Expr *E) {
15742 return Success(Value: 0, E);
15743 }
15744
15745 //===--------------------------------------------------------------------===//
15746 // Visitor Methods
15747 //===--------------------------------------------------------------------===//
15748
15749 bool VisitFixedPointLiteral(const FixedPointLiteral *E) {
15750 return Success(I: E->getValue(), E);
15751 }
15752
15753 bool VisitCastExpr(const CastExpr *E);
15754 bool VisitUnaryOperator(const UnaryOperator *E);
15755 bool VisitBinaryOperator(const BinaryOperator *E);
15756};
15757} // end anonymous namespace
15758
15759/// EvaluateIntegerOrLValue - Evaluate an rvalue integral-typed expression, and
15760/// produce either the integer value or a pointer.
15761///
15762/// GCC has a heinous extension which folds casts between pointer types and
15763/// pointer-sized integral types. We support this by allowing the evaluation of
15764/// an integer rvalue to produce a pointer (represented as an lvalue) instead.
15765/// Some simple arithmetic on such values is supported (they are treated much
15766/// like char*).
15767static bool EvaluateIntegerOrLValue(const Expr *E, APValue &Result,
15768 EvalInfo &Info) {
15769 assert(!E->isValueDependent());
15770 assert(E->isPRValue() && E->getType()->isIntegralOrEnumerationType());
15771 return IntExprEvaluator(Info, Result).Visit(S: E);
15772}
15773
15774static bool EvaluateInteger(const Expr *E, APSInt &Result, EvalInfo &Info) {
15775 assert(!E->isValueDependent());
15776 APValue Val;
15777 if (!EvaluateIntegerOrLValue(E, Result&: Val, Info))
15778 return false;
15779 if (!Val.isInt()) {
15780 // FIXME: It would be better to produce the diagnostic for casting
15781 // a pointer to an integer.
15782 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
15783 return false;
15784 }
15785 Result = Val.getInt();
15786 return true;
15787}
15788
15789bool IntExprEvaluator::VisitSourceLocExpr(const SourceLocExpr *E) {
15790 APValue Evaluated = E->EvaluateInContext(
15791 Ctx: Info.Ctx, DefaultExpr: Info.CurrentCall->CurSourceLocExprScope.getDefaultExpr());
15792 return Success(V: Evaluated, E);
15793}
15794
15795static bool EvaluateFixedPoint(const Expr *E, APFixedPoint &Result,
15796 EvalInfo &Info) {
15797 assert(!E->isValueDependent());
15798 if (E->getType()->isFixedPointType()) {
15799 APValue Val;
15800 if (!FixedPointExprEvaluator(Info, Val).Visit(S: E))
15801 return false;
15802 if (!Val.isFixedPoint())
15803 return false;
15804
15805 Result = Val.getFixedPoint();
15806 return true;
15807 }
15808 return false;
15809}
15810
15811static bool EvaluateFixedPointOrInteger(const Expr *E, APFixedPoint &Result,
15812 EvalInfo &Info) {
15813 assert(!E->isValueDependent());
15814 if (E->getType()->isIntegerType()) {
15815 auto FXSema = Info.Ctx.getFixedPointSemantics(Ty: E->getType());
15816 APSInt Val;
15817 if (!EvaluateInteger(E, Result&: Val, Info))
15818 return false;
15819 Result = APFixedPoint(Val, FXSema);
15820 return true;
15821 } else if (E->getType()->isFixedPointType()) {
15822 return EvaluateFixedPoint(E, Result, Info);
15823 }
15824 return false;
15825}
15826
15827/// Check whether the given declaration can be directly converted to an integral
15828/// rvalue. If not, no diagnostic is produced; there are other things we can
15829/// try.
15830bool IntExprEvaluator::CheckReferencedDecl(const Expr* E, const Decl* D) {
15831 // Enums are integer constant exprs.
15832 if (const EnumConstantDecl *ECD = dyn_cast<EnumConstantDecl>(Val: D)) {
15833 // Check for signedness/width mismatches between E type and ECD value.
15834 bool SameSign = (ECD->getInitVal().isSigned()
15835 == E->getType()->isSignedIntegerOrEnumerationType());
15836 bool SameWidth = (ECD->getInitVal().getBitWidth()
15837 == Info.Ctx.getIntWidth(T: E->getType()));
15838 if (SameSign && SameWidth)
15839 return Success(SI: ECD->getInitVal(), E);
15840 else {
15841 // Get rid of mismatch (otherwise Success assertions will fail)
15842 // by computing a new value matching the type of E.
15843 llvm::APSInt Val = ECD->getInitVal();
15844 if (!SameSign)
15845 Val.setIsSigned(!ECD->getInitVal().isSigned());
15846 if (!SameWidth)
15847 Val = Val.extOrTrunc(width: Info.Ctx.getIntWidth(T: E->getType()));
15848 return Success(SI: Val, E);
15849 }
15850 }
15851 return false;
15852}
15853
15854/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
15855/// as GCC.
15856GCCTypeClass EvaluateBuiltinClassifyType(QualType T,
15857 const LangOptions &LangOpts) {
15858 assert(!T->isDependentType() && "unexpected dependent type");
15859
15860 QualType CanTy = T.getCanonicalType();
15861
15862 switch (CanTy->getTypeClass()) {
15863#define TYPE(ID, BASE)
15864#define DEPENDENT_TYPE(ID, BASE) case Type::ID:
15865#define NON_CANONICAL_TYPE(ID, BASE) case Type::ID:
15866#define NON_CANONICAL_UNLESS_DEPENDENT_TYPE(ID, BASE) case Type::ID:
15867#include "clang/AST/TypeNodes.inc"
15868 case Type::Auto:
15869 case Type::DeducedTemplateSpecialization:
15870 llvm_unreachable("unexpected non-canonical or dependent type");
15871
15872 case Type::Builtin:
15873 switch (cast<BuiltinType>(Val&: CanTy)->getKind()) {
15874#define BUILTIN_TYPE(ID, SINGLETON_ID)
15875#define SIGNED_TYPE(ID, SINGLETON_ID) \
15876 case BuiltinType::ID: return GCCTypeClass::Integer;
15877#define FLOATING_TYPE(ID, SINGLETON_ID) \
15878 case BuiltinType::ID: return GCCTypeClass::RealFloat;
15879#define PLACEHOLDER_TYPE(ID, SINGLETON_ID) \
15880 case BuiltinType::ID: break;
15881#include "clang/AST/BuiltinTypes.def"
15882 case BuiltinType::Void:
15883 return GCCTypeClass::Void;
15884
15885 case BuiltinType::Bool:
15886 return GCCTypeClass::Bool;
15887
15888 case BuiltinType::Char_U:
15889 case BuiltinType::UChar:
15890 case BuiltinType::WChar_U:
15891 case BuiltinType::Char8:
15892 case BuiltinType::Char16:
15893 case BuiltinType::Char32:
15894 case BuiltinType::UShort:
15895 case BuiltinType::UInt:
15896 case BuiltinType::ULong:
15897 case BuiltinType::ULongLong:
15898 case BuiltinType::UInt128:
15899 return GCCTypeClass::Integer;
15900
15901 case BuiltinType::UShortAccum:
15902 case BuiltinType::UAccum:
15903 case BuiltinType::ULongAccum:
15904 case BuiltinType::UShortFract:
15905 case BuiltinType::UFract:
15906 case BuiltinType::ULongFract:
15907 case BuiltinType::SatUShortAccum:
15908 case BuiltinType::SatUAccum:
15909 case BuiltinType::SatULongAccum:
15910 case BuiltinType::SatUShortFract:
15911 case BuiltinType::SatUFract:
15912 case BuiltinType::SatULongFract:
15913 return GCCTypeClass::None;
15914
15915 case BuiltinType::NullPtr:
15916
15917 case BuiltinType::ObjCId:
15918 case BuiltinType::ObjCClass:
15919 case BuiltinType::ObjCSel:
15920#define IMAGE_TYPE(ImgType, Id, SingletonId, Access, Suffix) \
15921 case BuiltinType::Id:
15922#include "clang/Basic/OpenCLImageTypes.def"
15923#define EXT_OPAQUE_TYPE(ExtType, Id, Ext) \
15924 case BuiltinType::Id:
15925#include "clang/Basic/OpenCLExtensionTypes.def"
15926 case BuiltinType::OCLSampler:
15927 case BuiltinType::OCLEvent:
15928 case BuiltinType::OCLClkEvent:
15929 case BuiltinType::OCLQueue:
15930 case BuiltinType::OCLReserveID:
15931#define SVE_TYPE(Name, Id, SingletonId) \
15932 case BuiltinType::Id:
15933#include "clang/Basic/AArch64ACLETypes.def"
15934#define PPC_VECTOR_TYPE(Name, Id, Size) \
15935 case BuiltinType::Id:
15936#include "clang/Basic/PPCTypes.def"
15937#define RVV_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
15938#include "clang/Basic/RISCVVTypes.def"
15939#define WASM_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
15940#include "clang/Basic/WebAssemblyReferenceTypes.def"
15941#define AMDGPU_TYPE(Name, Id, SingletonId, Width, Align) case BuiltinType::Id:
15942#include "clang/Basic/AMDGPUTypes.def"
15943#define HLSL_INTANGIBLE_TYPE(Name, Id, SingletonId) case BuiltinType::Id:
15944#include "clang/Basic/HLSLIntangibleTypes.def"
15945 return GCCTypeClass::None;
15946
15947 case BuiltinType::Dependent:
15948 llvm_unreachable("unexpected dependent type");
15949 };
15950 llvm_unreachable("unexpected placeholder type");
15951
15952 case Type::Enum:
15953 return LangOpts.CPlusPlus ? GCCTypeClass::Enum : GCCTypeClass::Integer;
15954
15955 case Type::Pointer:
15956 case Type::ConstantArray:
15957 case Type::VariableArray:
15958 case Type::IncompleteArray:
15959 case Type::FunctionNoProto:
15960 case Type::FunctionProto:
15961 case Type::ArrayParameter:
15962 return GCCTypeClass::Pointer;
15963
15964 case Type::MemberPointer:
15965 return CanTy->isMemberDataPointerType()
15966 ? GCCTypeClass::PointerToDataMember
15967 : GCCTypeClass::PointerToMemberFunction;
15968
15969 case Type::Complex:
15970 return GCCTypeClass::Complex;
15971
15972 case Type::Record:
15973 return CanTy->isUnionType() ? GCCTypeClass::Union
15974 : GCCTypeClass::ClassOrStruct;
15975
15976 case Type::Atomic:
15977 // GCC classifies _Atomic T the same as T.
15978 return EvaluateBuiltinClassifyType(
15979 T: CanTy->castAs<AtomicType>()->getValueType(), LangOpts);
15980
15981 case Type::Vector:
15982 case Type::ExtVector:
15983 return GCCTypeClass::Vector;
15984
15985 case Type::BlockPointer:
15986 case Type::ConstantMatrix:
15987 case Type::ObjCObject:
15988 case Type::ObjCInterface:
15989 case Type::ObjCObjectPointer:
15990 case Type::Pipe:
15991 case Type::HLSLAttributedResource:
15992 case Type::HLSLInlineSpirv:
15993 case Type::OverflowBehavior:
15994 // Classify all other types that don't fit into the regular
15995 // classification the same way.
15996 return GCCTypeClass::None;
15997
15998 case Type::BitInt:
15999 return GCCTypeClass::BitInt;
16000
16001 case Type::LValueReference:
16002 case Type::RValueReference:
16003 llvm_unreachable("invalid type for expression");
16004 }
16005
16006 llvm_unreachable("unexpected type class");
16007}
16008
16009/// EvaluateBuiltinClassifyType - Evaluate __builtin_classify_type the same way
16010/// as GCC.
16011static GCCTypeClass
16012EvaluateBuiltinClassifyType(const CallExpr *E, const LangOptions &LangOpts) {
16013 // If no argument was supplied, default to None. This isn't
16014 // ideal, however it is what gcc does.
16015 if (E->getNumArgs() == 0)
16016 return GCCTypeClass::None;
16017
16018 // FIXME: Bizarrely, GCC treats a call with more than one argument as not
16019 // being an ICE, but still folds it to a constant using the type of the first
16020 // argument.
16021 return EvaluateBuiltinClassifyType(T: E->getArg(Arg: 0)->getType(), LangOpts);
16022}
16023
16024/// EvaluateBuiltinConstantPForLValue - Determine the result of
16025/// __builtin_constant_p when applied to the given pointer.
16026///
16027/// A pointer is only "constant" if it is null (or a pointer cast to integer)
16028/// or it points to the first character of a string literal.
16029static bool EvaluateBuiltinConstantPForLValue(const APValue &LV) {
16030 APValue::LValueBase Base = LV.getLValueBase();
16031 if (Base.isNull()) {
16032 // A null base is acceptable.
16033 return true;
16034 } else if (const Expr *E = Base.dyn_cast<const Expr *>()) {
16035 if (!isa<StringLiteral>(Val: E))
16036 return false;
16037 return LV.getLValueOffset().isZero();
16038 } else if (Base.is<TypeInfoLValue>()) {
16039 // Surprisingly, GCC considers __builtin_constant_p(&typeid(int)) to
16040 // evaluate to true.
16041 return true;
16042 } else {
16043 // Any other base is not constant enough for GCC.
16044 return false;
16045 }
16046}
16047
16048/// EvaluateBuiltinConstantP - Evaluate __builtin_constant_p as similarly to
16049/// GCC as we can manage.
16050static bool EvaluateBuiltinConstantP(EvalInfo &Info, const Expr *Arg) {
16051 // This evaluation is not permitted to have side-effects, so evaluate it in
16052 // a speculative evaluation context.
16053 SpeculativeEvaluationRAII SpeculativeEval(Info);
16054
16055 // Constant-folding is always enabled for the operand of __builtin_constant_p
16056 // (even when the enclosing evaluation context otherwise requires a strict
16057 // language-specific constant expression).
16058 FoldConstant Fold(Info, true);
16059
16060 QualType ArgType = Arg->getType();
16061
16062 // __builtin_constant_p always has one operand. The rules which gcc follows
16063 // are not precisely documented, but are as follows:
16064 //
16065 // - If the operand is of integral, floating, complex or enumeration type,
16066 // and can be folded to a known value of that type, it returns 1.
16067 // - If the operand can be folded to a pointer to the first character
16068 // of a string literal (or such a pointer cast to an integral type)
16069 // or to a null pointer or an integer cast to a pointer, it returns 1.
16070 //
16071 // Otherwise, it returns 0.
16072 //
16073 // FIXME: GCC also intends to return 1 for literals of aggregate types, but
16074 // its support for this did not work prior to GCC 9 and is not yet well
16075 // understood.
16076 if (ArgType->isIntegralOrEnumerationType() || ArgType->isFloatingType() ||
16077 ArgType->isAnyComplexType() || ArgType->isPointerType() ||
16078 ArgType->isNullPtrType()) {
16079 APValue V;
16080 if (!::EvaluateAsRValue(Info, E: Arg, Result&: V) || Info.EvalStatus.HasSideEffects) {
16081 Fold.keepDiagnostics();
16082 return false;
16083 }
16084
16085 // For a pointer (possibly cast to integer), there are special rules.
16086 if (V.getKind() == APValue::LValue)
16087 return EvaluateBuiltinConstantPForLValue(LV: V);
16088
16089 // Otherwise, any constant value is good enough.
16090 return V.hasValue();
16091 }
16092
16093 // Anything else isn't considered to be sufficiently constant.
16094 return false;
16095}
16096
16097/// Retrieves the "underlying object type" of the given expression,
16098/// as used by __builtin_object_size.
16099static QualType getObjectType(APValue::LValueBase B) {
16100 if (const ValueDecl *D = B.dyn_cast<const ValueDecl*>()) {
16101 if (const VarDecl *VD = dyn_cast<VarDecl>(Val: D))
16102 return VD->getType();
16103 } else if (const Expr *E = B.dyn_cast<const Expr*>()) {
16104 if (isa<CompoundLiteralExpr>(Val: E))
16105 return E->getType();
16106 } else if (B.is<TypeInfoLValue>()) {
16107 return B.getTypeInfoType();
16108 } else if (B.is<DynamicAllocLValue>()) {
16109 return B.getDynamicAllocType();
16110 }
16111
16112 return QualType();
16113}
16114
16115/// A more selective version of E->IgnoreParenCasts for
16116/// tryEvaluateBuiltinObjectSize. This ignores some casts/parens that serve only
16117/// to change the type of E.
16118/// Ex. For E = `(short*)((char*)(&foo))`, returns `&foo`
16119///
16120/// Always returns an RValue with a pointer representation.
16121static const Expr *ignorePointerCastsAndParens(const Expr *E) {
16122 assert(E->isPRValue() && E->getType()->hasPointerRepresentation());
16123
16124 const Expr *NoParens = E->IgnoreParens();
16125 const auto *Cast = dyn_cast<CastExpr>(Val: NoParens);
16126 if (Cast == nullptr)
16127 return NoParens;
16128
16129 // We only conservatively allow a few kinds of casts, because this code is
16130 // inherently a simple solution that seeks to support the common case.
16131 auto CastKind = Cast->getCastKind();
16132 if (CastKind != CK_NoOp && CastKind != CK_BitCast &&
16133 CastKind != CK_AddressSpaceConversion)
16134 return NoParens;
16135
16136 const auto *SubExpr = Cast->getSubExpr();
16137 if (!SubExpr->getType()->hasPointerRepresentation() || !SubExpr->isPRValue())
16138 return NoParens;
16139 return ignorePointerCastsAndParens(E: SubExpr);
16140}
16141
16142/// Checks to see if the given LValue's Designator is at the end of the LValue's
16143/// record layout. e.g.
16144/// struct { struct { int a, b; } fst, snd; } obj;
16145/// obj.fst // no
16146/// obj.snd // yes
16147/// obj.fst.a // no
16148/// obj.fst.b // no
16149/// obj.snd.a // no
16150/// obj.snd.b // yes
16151///
16152/// Please note: this function is specialized for how __builtin_object_size
16153/// views "objects".
16154///
16155/// If this encounters an invalid RecordDecl or otherwise cannot determine the
16156/// correct result, it will always return true.
16157static bool isDesignatorAtObjectEnd(const ASTContext &Ctx, const LValue &LVal) {
16158 assert(!LVal.Designator.Invalid);
16159
16160 auto IsLastOrInvalidFieldDecl = [&Ctx](const FieldDecl *FD) {
16161 const RecordDecl *Parent = FD->getParent();
16162 if (Parent->isInvalidDecl() || Parent->isUnion())
16163 return true;
16164 const ASTRecordLayout &Layout = Ctx.getASTRecordLayout(D: Parent);
16165 return FD->getFieldIndex() + 1 == Layout.getFieldCount();
16166 };
16167
16168 auto &Base = LVal.getLValueBase();
16169 if (auto *ME = dyn_cast_or_null<MemberExpr>(Val: Base.dyn_cast<const Expr *>())) {
16170 if (auto *FD = dyn_cast<FieldDecl>(Val: ME->getMemberDecl())) {
16171 if (!IsLastOrInvalidFieldDecl(FD))
16172 return false;
16173 } else if (auto *IFD = dyn_cast<IndirectFieldDecl>(Val: ME->getMemberDecl())) {
16174 for (auto *FD : IFD->chain()) {
16175 if (!IsLastOrInvalidFieldDecl(cast<FieldDecl>(Val: FD)))
16176 return false;
16177 }
16178 }
16179 }
16180
16181 unsigned I = 0;
16182 QualType BaseType = getType(B: Base);
16183 if (LVal.Designator.FirstEntryIsAnUnsizedArray) {
16184 // If we don't know the array bound, conservatively assume we're looking at
16185 // the final array element.
16186 ++I;
16187 if (BaseType->isIncompleteArrayType())
16188 BaseType = Ctx.getAsArrayType(T: BaseType)->getElementType();
16189 else
16190 BaseType = BaseType->castAs<PointerType>()->getPointeeType();
16191 }
16192
16193 for (unsigned E = LVal.Designator.Entries.size(); I != E; ++I) {
16194 const auto &Entry = LVal.Designator.Entries[I];
16195 if (BaseType->isArrayType()) {
16196 // Because __builtin_object_size treats arrays as objects, we can ignore
16197 // the index iff this is the last array in the Designator.
16198 if (I + 1 == E)
16199 return true;
16200 const auto *CAT = cast<ConstantArrayType>(Val: Ctx.getAsArrayType(T: BaseType));
16201 uint64_t Index = Entry.getAsArrayIndex();
16202 if (Index + 1 != CAT->getZExtSize())
16203 return false;
16204 BaseType = CAT->getElementType();
16205 } else if (BaseType->isAnyComplexType()) {
16206 const auto *CT = BaseType->castAs<ComplexType>();
16207 uint64_t Index = Entry.getAsArrayIndex();
16208 if (Index != 1)
16209 return false;
16210 BaseType = CT->getElementType();
16211 } else if (auto *FD = getAsField(E: Entry)) {
16212 if (!IsLastOrInvalidFieldDecl(FD))
16213 return false;
16214 BaseType = FD->getType();
16215 } else {
16216 assert(getAsBaseClass(Entry) && "Expecting cast to a base class");
16217 return false;
16218 }
16219 }
16220 return true;
16221}
16222
16223/// Tests to see if the LValue has a user-specified designator (that isn't
16224/// necessarily valid). Note that this always returns 'true' if the LValue has
16225/// an unsized array as its first designator entry, because there's currently no
16226/// way to tell if the user typed *foo or foo[0].
16227static bool refersToCompleteObject(const LValue &LVal) {
16228 if (LVal.Designator.Invalid)
16229 return false;
16230
16231 if (!LVal.Designator.Entries.empty())
16232 return LVal.Designator.isMostDerivedAnUnsizedArray();
16233
16234 if (!LVal.InvalidBase)
16235 return true;
16236
16237 // If `E` is a MemberExpr, then the first part of the designator is hiding in
16238 // the LValueBase.
16239 const auto *E = LVal.Base.dyn_cast<const Expr *>();
16240 return !E || !isa<MemberExpr>(Val: E);
16241}
16242
16243/// Attempts to detect a user writing into a piece of memory that's impossible
16244/// to figure out the size of by just using types.
16245static bool isUserWritingOffTheEnd(const ASTContext &Ctx, const LValue &LVal) {
16246 const SubobjectDesignator &Designator = LVal.Designator;
16247 // Notes:
16248 // - Users can only write off of the end when we have an invalid base. Invalid
16249 // bases imply we don't know where the memory came from.
16250 // - We used to be a bit more aggressive here; we'd only be conservative if
16251 // the array at the end was flexible, or if it had 0 or 1 elements. This
16252 // broke some common standard library extensions (PR30346), but was
16253 // otherwise seemingly fine. It may be useful to reintroduce this behavior
16254 // with some sort of list. OTOH, it seems that GCC is always
16255 // conservative with the last element in structs (if it's an array), so our
16256 // current behavior is more compatible than an explicit list approach would
16257 // be.
16258 auto isFlexibleArrayMember = [&] {
16259 using FAMKind = LangOptions::StrictFlexArraysLevelKind;
16260 FAMKind StrictFlexArraysLevel =
16261 Ctx.getLangOpts().getStrictFlexArraysLevel();
16262
16263 if (Designator.isMostDerivedAnUnsizedArray())
16264 return true;
16265
16266 if (StrictFlexArraysLevel == FAMKind::Default)
16267 return true;
16268
16269 if (Designator.getMostDerivedArraySize() == 0 &&
16270 StrictFlexArraysLevel != FAMKind::IncompleteOnly)
16271 return true;
16272
16273 if (Designator.getMostDerivedArraySize() == 1 &&
16274 StrictFlexArraysLevel == FAMKind::OneZeroOrIncomplete)
16275 return true;
16276
16277 return false;
16278 };
16279
16280 return LVal.InvalidBase &&
16281 Designator.Entries.size() == Designator.MostDerivedPathLength &&
16282 Designator.MostDerivedIsArrayElement && isFlexibleArrayMember() &&
16283 isDesignatorAtObjectEnd(Ctx, LVal);
16284}
16285
16286/// Converts the given APInt to CharUnits, assuming the APInt is unsigned.
16287/// Fails if the conversion would cause loss of precision.
16288static bool convertUnsignedAPIntToCharUnits(const llvm::APInt &Int,
16289 CharUnits &Result) {
16290 auto CharUnitsMax = std::numeric_limits<CharUnits::QuantityType>::max();
16291 if (Int.ugt(RHS: CharUnitsMax))
16292 return false;
16293 Result = CharUnits::fromQuantity(Quantity: Int.getZExtValue());
16294 return true;
16295}
16296
16297/// If we're evaluating the object size of an instance of a struct that
16298/// contains a flexible array member, add the size of the initializer.
16299static void addFlexibleArrayMemberInitSize(EvalInfo &Info, const QualType &T,
16300 const LValue &LV, CharUnits &Size) {
16301 if (!T.isNull() && T->isStructureType() &&
16302 T->castAsRecordDecl()->hasFlexibleArrayMember())
16303 if (const auto *V = LV.getLValueBase().dyn_cast<const ValueDecl *>())
16304 if (const auto *VD = dyn_cast<VarDecl>(Val: V))
16305 if (VD->hasInit())
16306 Size += VD->getFlexibleArrayInitChars(Ctx: Info.Ctx);
16307}
16308
16309/// Helper for tryEvaluateBuiltinObjectSize -- Given an LValue, this will
16310/// determine how many bytes exist from the beginning of the object to either
16311/// the end of the current subobject, or the end of the object itself, depending
16312/// on what the LValue looks like + the value of Type.
16313///
16314/// If this returns false, the value of Result is undefined.
16315static bool determineEndOffset(EvalInfo &Info, SourceLocation ExprLoc,
16316 unsigned Type, const LValue &LVal,
16317 CharUnits &EndOffset) {
16318 bool DetermineForCompleteObject = refersToCompleteObject(LVal);
16319
16320 auto CheckedHandleSizeof = [&](QualType Ty, CharUnits &Result) {
16321 if (Ty.isNull())
16322 return false;
16323
16324 Ty = Ty.getNonReferenceType();
16325
16326 if (Ty->isIncompleteType() || Ty->isFunctionType())
16327 return false;
16328
16329 return HandleSizeof(Info, Loc: ExprLoc, Type: Ty, Size&: Result);
16330 };
16331
16332 // We want to evaluate the size of the entire object. This is a valid fallback
16333 // for when Type=1 and the designator is invalid, because we're asked for an
16334 // upper-bound.
16335 if (!(Type & 1) || LVal.Designator.Invalid || DetermineForCompleteObject) {
16336 // Type=3 wants a lower bound, so we can't fall back to this.
16337 if (Type == 3 && !DetermineForCompleteObject)
16338 return false;
16339
16340 llvm::APInt APEndOffset;
16341 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16342 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16343 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16344
16345 if (LVal.InvalidBase)
16346 return false;
16347
16348 QualType BaseTy = getObjectType(B: LVal.getLValueBase());
16349 const bool Ret = CheckedHandleSizeof(BaseTy, EndOffset);
16350 addFlexibleArrayMemberInitSize(Info, T: BaseTy, LV: LVal, Size&: EndOffset);
16351 return Ret;
16352 }
16353
16354 // We want to evaluate the size of a subobject.
16355 const SubobjectDesignator &Designator = LVal.Designator;
16356
16357 // The following is a moderately common idiom in C:
16358 //
16359 // struct Foo { int a; char c[1]; };
16360 // struct Foo *F = (struct Foo *)malloc(sizeof(struct Foo) + strlen(Bar));
16361 // strcpy(&F->c[0], Bar);
16362 //
16363 // In order to not break too much legacy code, we need to support it.
16364 if (isUserWritingOffTheEnd(Ctx: Info.Ctx, LVal)) {
16365 // If we can resolve this to an alloc_size call, we can hand that back,
16366 // because we know for certain how many bytes there are to write to.
16367 llvm::APInt APEndOffset;
16368 if (isBaseAnAllocSizeCall(Base: LVal.getLValueBase()) &&
16369 getBytesReturnedByAllocSizeCall(Ctx: Info.Ctx, LVal, Result&: APEndOffset))
16370 return convertUnsignedAPIntToCharUnits(Int: APEndOffset, Result&: EndOffset);
16371
16372 // If we cannot determine the size of the initial allocation, then we can't
16373 // given an accurate upper-bound. However, we are still able to give
16374 // conservative lower-bounds for Type=3.
16375 if (Type == 1)
16376 return false;
16377 }
16378
16379 CharUnits BytesPerElem;
16380 if (!CheckedHandleSizeof(Designator.MostDerivedType, BytesPerElem))
16381 return false;
16382
16383 // According to the GCC documentation, we want the size of the subobject
16384 // denoted by the pointer. But that's not quite right -- what we actually
16385 // want is the size of the immediately-enclosing array, if there is one.
16386 int64_t ElemsRemaining;
16387 if (Designator.MostDerivedIsArrayElement &&
16388 Designator.Entries.size() == Designator.MostDerivedPathLength) {
16389 uint64_t ArraySize = Designator.getMostDerivedArraySize();
16390 uint64_t ArrayIndex = Designator.Entries.back().getAsArrayIndex();
16391 ElemsRemaining = ArraySize <= ArrayIndex ? 0 : ArraySize - ArrayIndex;
16392 } else {
16393 ElemsRemaining = Designator.isOnePastTheEnd() ? 0 : 1;
16394 }
16395
16396 EndOffset = LVal.getLValueOffset() + BytesPerElem * ElemsRemaining;
16397 return true;
16398}
16399
16400/// Tries to evaluate the __builtin_object_size for @p E. If successful,
16401/// returns true and stores the result in @p Size.
16402///
16403/// If @p WasError is non-null, this will report whether the failure to evaluate
16404/// is to be treated as an Error in IntExprEvaluator.
16405///
16406/// If @p IsDynamic is true (i.e. we're evaluating
16407/// __builtin_dynamic_object_size) and the operand designates a flexible array
16408/// member annotated with 'counted_by', we refuse to fold so that IR generation
16409/// can emit the count-based runtime size computation.
16410static std::optional<uint64_t>
16411tryEvaluateBuiltinObjectSize(const Expr *E, unsigned Type, EvalInfo &Info,
16412 bool IsDynamic = false) {
16413 // Determine the denoted object.
16414 LValue LVal;
16415 {
16416 // The operand of __builtin_object_size is never evaluated for side-effects.
16417 // If there are any, but we can determine the pointed-to object anyway, then
16418 // ignore the side-effects.
16419 SpeculativeEvaluationRAII SpeculativeEval(Info);
16420 IgnoreSideEffectsRAII Fold(Info);
16421
16422 if (E->isGLValue()) {
16423 // It's possible for us to be given GLValues if we're called via
16424 // Expr::tryEvaluateObjectSize.
16425 APValue RVal;
16426 if (!EvaluateAsRValue(Info, E, Result&: RVal))
16427 return std::nullopt;
16428 LVal.setFrom(Ctx: Info.Ctx, V: RVal);
16429 } else if (!EvaluatePointer(E: ignorePointerCastsAndParens(E), Result&: LVal, Info,
16430 /*InvalidBaseOK=*/true))
16431 return std::nullopt;
16432 }
16433
16434 // If we point to before the start of the object, there are no accessible
16435 // bytes.
16436 if (LVal.getLValueOffset().isNegative())
16437 return 0;
16438
16439 // For __builtin_dynamic_object_size on a counted_by-annotated flexible
16440 // array member, defer to IR generation (emitCountedBySize in CGBuiltin):
16441 // its runtime computation uses the live 'count' field and is more accurate
16442 // than the layout/initializer-derived size we'd produce here. Use the same
16443 // findStructFieldAccess form-recognition CGBuiltin does, so we refuse to
16444 // fold on exactly the shapes that path handles (and, importantly, *not*
16445 // on '&af.fam' which designates the array-as-a-whole and stays on the
16446 // layout-derived path to match GCC). Checked after the negative-offset
16447 // early return above so that obviously out-of-bounds operands still fold
16448 // to 0, preserving existing behavior.
16449 if (IsDynamic) {
16450 const auto *ME = dyn_cast_or_null<MemberExpr>(Val: findStructFieldAccess(E));
16451 const auto *FD = ME ? dyn_cast<FieldDecl>(Val: ME->getMemberDecl()) : nullptr;
16452 if (FD && FD->getType()->isCountAttributedType())
16453 return std::nullopt;
16454 }
16455
16456 CharUnits EndOffset;
16457 if (!determineEndOffset(Info, ExprLoc: E->getExprLoc(), Type, LVal, EndOffset))
16458 return std::nullopt;
16459
16460 // If we've fallen outside of the end offset, just pretend there's nothing to
16461 // write to/read from.
16462 if (EndOffset <= LVal.getLValueOffset())
16463 return 0;
16464 return (EndOffset - LVal.getLValueOffset()).getQuantity();
16465}
16466
16467bool IntExprEvaluator::VisitCallExpr(const CallExpr *E) {
16468 if (!IsConstantEvaluatedBuiltinCall(E))
16469 return ExprEvaluatorBaseTy::VisitCallExpr(E);
16470 return VisitBuiltinCallExpr(E, BuiltinOp: E->getBuiltinCallee());
16471}
16472
16473static bool getBuiltinAlignArguments(const CallExpr *E, EvalInfo &Info,
16474 APValue &Val, APSInt &Alignment) {
16475 QualType SrcTy = E->getArg(Arg: 0)->getType();
16476 if (!getAlignmentArgument(E: E->getArg(Arg: 1), ForType: SrcTy, Info, Alignment))
16477 return false;
16478 // Even though we are evaluating integer expressions we could get a pointer
16479 // argument for the __builtin_is_aligned() case.
16480 if (SrcTy->isPointerType()) {
16481 LValue Ptr;
16482 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: Ptr, Info))
16483 return false;
16484 Ptr.moveInto(V&: Val);
16485 } else if (!SrcTy->isIntegralOrEnumerationType()) {
16486 Info.FFDiag(E: E->getArg(Arg: 0));
16487 return false;
16488 } else {
16489 APSInt SrcInt;
16490 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SrcInt, Info))
16491 return false;
16492 assert(SrcInt.getBitWidth() >= Alignment.getBitWidth() &&
16493 "Bit widths must be the same");
16494 Val = APValue(SrcInt);
16495 }
16496 assert(Val.hasValue());
16497 return true;
16498}
16499
16500bool IntExprEvaluator::VisitBuiltinCallExpr(const CallExpr *E,
16501 unsigned BuiltinOp) {
16502 auto EvalTestOp = [&](llvm::function_ref<bool(const APInt &, const APInt &)>
16503 Fn) {
16504 APValue SourceLHS, SourceRHS;
16505 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: SourceLHS) ||
16506 !EvaluateAsRValue(Info, E: E->getArg(Arg: 1), Result&: SourceRHS))
16507 return false;
16508
16509 unsigned SourceLen = SourceLHS.getVectorLength();
16510 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
16511 QualType ElemQT = VT->getElementType();
16512 unsigned LaneWidth = Info.Ctx.getTypeSize(T: ElemQT);
16513
16514 APInt AWide(LaneWidth * SourceLen, 0);
16515 APInt BWide(LaneWidth * SourceLen, 0);
16516
16517 for (unsigned I = 0; I != SourceLen; ++I) {
16518 APInt ALane;
16519 APInt BLane;
16520 if (ElemQT->isIntegerType()) { // Get value.
16521 ALane = SourceLHS.getVectorElt(I).getInt();
16522 BLane = SourceRHS.getVectorElt(I).getInt();
16523 } else if (ElemQT->isFloatingType()) { // Get only sign bit.
16524 ALane =
16525 SourceLHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
16526 BLane =
16527 SourceRHS.getVectorElt(I).getFloat().bitcastToAPInt().isNegative();
16528 } else { // Must be integer or floating type.
16529 return false;
16530 }
16531 AWide.insertBits(SubBits: ALane, bitPosition: I * LaneWidth);
16532 BWide.insertBits(SubBits: BLane, bitPosition: I * LaneWidth);
16533 }
16534 return Success(Value: Fn(AWide, BWide), E);
16535 };
16536
16537 auto HandleMaskBinOp =
16538 [&](llvm::function_ref<APSInt(const APSInt &, const APSInt &)> Fn)
16539 -> bool {
16540 APValue LHS, RHS;
16541 if (!Evaluate(Result&: LHS, Info, E: E->getArg(Arg: 0)) ||
16542 !Evaluate(Result&: RHS, Info, E: E->getArg(Arg: 1)))
16543 return false;
16544
16545 APSInt ResultInt = Fn(LHS.getInt(), RHS.getInt());
16546
16547 return Success(V: APValue(ResultInt), E);
16548 };
16549
16550 auto HandleCRC32 = [&](unsigned DataBytes) -> bool {
16551 APSInt CRC, Data;
16552 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CRC, Info) ||
16553 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Data, Info))
16554 return false;
16555
16556 uint64_t CRCVal = CRC.getZExtValue();
16557 uint64_t DataVal = Data.getZExtValue();
16558
16559 // CRC32C polynomial (iSCSI polynomial, bit-reversed)
16560 static const uint32_t CRC32C_POLY = 0x82F63B78;
16561
16562 // Process each byte
16563 uint32_t Result = static_cast<uint32_t>(CRCVal);
16564 for (unsigned I = 0; I != DataBytes; ++I) {
16565 uint8_t Byte = static_cast<uint8_t>((DataVal >> (I * 8)) & 0xFF);
16566 Result ^= Byte;
16567 for (int J = 0; J != 8; ++J) {
16568 Result = (Result >> 1) ^ ((Result & 1) ? CRC32C_POLY : 0);
16569 }
16570 }
16571
16572 return Success(Value: Result, E);
16573 };
16574
16575 switch (BuiltinOp) {
16576 default:
16577 return false;
16578
16579 case X86::BI__builtin_ia32_crc32qi:
16580 return HandleCRC32(1);
16581 case X86::BI__builtin_ia32_crc32hi:
16582 return HandleCRC32(2);
16583 case X86::BI__builtin_ia32_crc32si:
16584 return HandleCRC32(4);
16585 case X86::BI__builtin_ia32_crc32di:
16586 return HandleCRC32(8);
16587
16588 case Builtin::BI__builtin_dynamic_object_size:
16589 case Builtin::BI__builtin_object_size: {
16590 // The type was checked when we built the expression.
16591 unsigned Type =
16592 E->getArg(Arg: 1)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
16593 assert(Type <= 3 && "unexpected type");
16594
16595 bool IsDynamic = BuiltinOp == Builtin::BI__builtin_dynamic_object_size;
16596 if (std::optional<uint64_t> Size =
16597 tryEvaluateBuiltinObjectSize(E: E->getArg(Arg: 0), Type, Info, IsDynamic))
16598 return Success(Value: *Size, E);
16599
16600 if (E->getArg(Arg: 0)->HasSideEffects(Ctx: Info.Ctx))
16601 return Success(Value: (Type & 2) ? 0 : -1, E);
16602
16603 // Expression had no side effects, but we couldn't statically determine the
16604 // size of the referenced object.
16605 switch (Info.EvalMode) {
16606 case EvaluationMode::ConstantExpression:
16607 case EvaluationMode::ConstantFold:
16608 case EvaluationMode::IgnoreSideEffects:
16609 // Leave it to IR generation.
16610 return Error(E);
16611 case EvaluationMode::ConstantExpressionUnevaluated:
16612 // Reduce it to a constant now.
16613 return Success(Value: (Type & 2) ? 0 : -1, E);
16614 }
16615
16616 llvm_unreachable("unexpected EvalMode");
16617 }
16618
16619 case Builtin::BI__builtin_os_log_format_buffer_size: {
16620 analyze_os_log::OSLogBufferLayout Layout;
16621 analyze_os_log::computeOSLogBufferLayout(Ctx&: Info.Ctx, E, layout&: Layout);
16622 return Success(Value: Layout.size().getQuantity(), E);
16623 }
16624
16625 case Builtin::BI__builtin_is_aligned: {
16626 APValue Src;
16627 APSInt Alignment;
16628 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
16629 return false;
16630 if (Src.isLValue()) {
16631 // If we evaluated a pointer, check the minimum known alignment.
16632 LValue Ptr;
16633 Ptr.setFrom(Ctx: Info.Ctx, V: Src);
16634 CharUnits BaseAlignment = getBaseAlignment(Info, Value: Ptr);
16635 CharUnits PtrAlign = BaseAlignment.alignmentAtOffset(offset: Ptr.Offset);
16636 // We can return true if the known alignment at the computed offset is
16637 // greater than the requested alignment.
16638 assert(PtrAlign.isPowerOfTwo());
16639 assert(Alignment.isPowerOf2());
16640 if (PtrAlign.getQuantity() >= Alignment)
16641 return Success(Value: 1, E);
16642 // If the alignment is not known to be sufficient, some cases could still
16643 // be aligned at run time. However, if the requested alignment is less or
16644 // equal to the base alignment and the offset is not aligned, we know that
16645 // the run-time value can never be aligned.
16646 if (BaseAlignment.getQuantity() >= Alignment &&
16647 PtrAlign.getQuantity() < Alignment)
16648 return Success(Value: 0, E);
16649 // Otherwise we can't infer whether the value is sufficiently aligned.
16650 // TODO: __builtin_is_aligned(__builtin_align_{down,up{(expr, N), N)
16651 // in cases where we can't fully evaluate the pointer.
16652 Info.FFDiag(E: E->getArg(Arg: 0), DiagId: diag::note_constexpr_alignment_compute)
16653 << Alignment;
16654 return false;
16655 }
16656 assert(Src.isInt());
16657 return Success(Value: (Src.getInt() & (Alignment - 1)) == 0 ? 1 : 0, E);
16658 }
16659 case Builtin::BI__builtin_align_up: {
16660 APValue Src;
16661 APSInt Alignment;
16662 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
16663 return false;
16664 if (!Src.isInt())
16665 return Error(E);
16666 APSInt AlignedVal =
16667 APSInt((Src.getInt() + (Alignment - 1)) & ~(Alignment - 1),
16668 Src.getInt().isUnsigned());
16669 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
16670 return Success(SI: AlignedVal, E);
16671 }
16672 case Builtin::BI__builtin_align_down: {
16673 APValue Src;
16674 APSInt Alignment;
16675 if (!getBuiltinAlignArguments(E, Info, Val&: Src, Alignment))
16676 return false;
16677 if (!Src.isInt())
16678 return Error(E);
16679 APSInt AlignedVal =
16680 APSInt(Src.getInt() & ~(Alignment - 1), Src.getInt().isUnsigned());
16681 assert(AlignedVal.getBitWidth() == Src.getInt().getBitWidth());
16682 return Success(SI: AlignedVal, E);
16683 }
16684
16685 case Builtin::BI__builtin_bitreverseg:
16686 case Builtin::BI__builtin_bitreverse8:
16687 case Builtin::BI__builtin_bitreverse16:
16688 case Builtin::BI__builtin_bitreverse32:
16689 case Builtin::BI__builtin_bitreverse64:
16690 case Builtin::BI__builtin_elementwise_bitreverse: {
16691 APSInt Val;
16692 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
16693 return false;
16694
16695 return Success(I: Val.reverseBits(), E);
16696 }
16697 case Builtin::BI__builtin_bswapg:
16698 case Builtin::BI__builtin_bswap16:
16699 case Builtin::BI__builtin_bswap32:
16700 case Builtin::BI__builtin_bswap64:
16701 case Builtin::BIstdc_memreverse8u8:
16702 case Builtin::BIstdc_memreverse8u16:
16703 case Builtin::BIstdc_memreverse8u32:
16704 case Builtin::BIstdc_memreverse8u64: {
16705 APSInt Val;
16706 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
16707 return false;
16708 if (Val.getBitWidth() == 8 || Val.getBitWidth() == 1)
16709 return Success(SI: Val, E);
16710
16711 return Success(I: Val.byteSwap(), E);
16712 }
16713
16714 case Builtin::BI__builtin_classify_type:
16715 return Success(Value: (int)EvaluateBuiltinClassifyType(E, LangOpts: Info.getLangOpts()), E);
16716
16717 case Builtin::BI__builtin_clrsb:
16718 case Builtin::BI__builtin_clrsbl:
16719 case Builtin::BI__builtin_clrsbll: {
16720 APSInt Val;
16721 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
16722 return false;
16723
16724 return Success(Value: Val.getBitWidth() - Val.getSignificantBits(), E);
16725 }
16726
16727 case Builtin::BI__builtin_clz:
16728 case Builtin::BI__builtin_clzl:
16729 case Builtin::BI__builtin_clzll:
16730 case Builtin::BI__builtin_clzs:
16731 case Builtin::BI__builtin_clzg:
16732 case Builtin::BI__builtin_elementwise_clzg:
16733 case Builtin::BI__lzcnt16: // Microsoft variants of count leading-zeroes
16734 case Builtin::BI__lzcnt:
16735 case Builtin::BI__lzcnt64: {
16736 APSInt Val;
16737 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
16738 APValue Vec;
16739 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
16740 return false;
16741 Val = ConvertBoolVectorToInt(Val: Vec);
16742 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
16743 return false;
16744 }
16745
16746 std::optional<APSInt> Fallback;
16747 if ((BuiltinOp == Builtin::BI__builtin_clzg ||
16748 BuiltinOp == Builtin::BI__builtin_elementwise_clzg) &&
16749 E->getNumArgs() > 1) {
16750 APSInt FallbackTemp;
16751 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
16752 return false;
16753 Fallback = FallbackTemp;
16754 }
16755
16756 if (!Val) {
16757 if (Fallback)
16758 return Success(SI: *Fallback, E);
16759
16760 // When the argument is 0, the result of GCC builtins is undefined,
16761 // whereas for Microsoft intrinsics, the result is the bit-width of the
16762 // argument.
16763 bool ZeroIsUndefined = BuiltinOp != Builtin::BI__lzcnt16 &&
16764 BuiltinOp != Builtin::BI__lzcnt &&
16765 BuiltinOp != Builtin::BI__lzcnt64;
16766
16767 if (BuiltinOp == Builtin::BI__builtin_elementwise_clzg) {
16768 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
16769 << /*IsTrailing=*/false;
16770 }
16771
16772 if (ZeroIsUndefined)
16773 return Error(E);
16774 }
16775
16776 return Success(Value: Val.countl_zero(), E);
16777 }
16778
16779 case Builtin::BI__builtin_constant_p: {
16780 const Expr *Arg = E->getArg(Arg: 0);
16781 if (EvaluateBuiltinConstantP(Info, Arg))
16782 return Success(Value: true, E);
16783 if (Info.InConstantContext || Arg->HasSideEffects(Ctx: Info.Ctx)) {
16784 // Outside a constant context, eagerly evaluate to false in the presence
16785 // of side-effects in order to avoid -Wunsequenced false-positives in
16786 // a branch on __builtin_constant_p(expr).
16787 return Success(Value: false, E);
16788 }
16789 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
16790 return false;
16791 }
16792
16793 case Builtin::BI__noop:
16794 // __noop always evaluates successfully and returns 0.
16795 return Success(Value: 0, E);
16796
16797 case Builtin::BI__builtin_is_constant_evaluated: {
16798 const auto *Callee = Info.CurrentCall->getCallee();
16799 if (Info.InConstantContext && !Info.CheckingPotentialConstantExpression &&
16800 (Info.CallStackDepth == 1 ||
16801 (Info.CallStackDepth == 2 && Callee->isInStdNamespace() &&
16802 Callee->getIdentifier() &&
16803 Callee->getIdentifier()->isStr(Str: "is_constant_evaluated")))) {
16804 // FIXME: Find a better way to avoid duplicated diagnostics.
16805 if (Info.EvalStatus.Diag)
16806 Info.report(Loc: (Info.CallStackDepth == 1)
16807 ? E->getExprLoc()
16808 : Info.CurrentCall->getCallRange().getBegin(),
16809 DiagId: diag::warn_is_constant_evaluated_always_true_constexpr)
16810 << (Info.CallStackDepth == 1 ? "__builtin_is_constant_evaluated"
16811 : "std::is_constant_evaluated");
16812 }
16813
16814 return Success(Value: Info.InConstantContext, E);
16815 }
16816
16817 case Builtin::BI__builtin_is_within_lifetime:
16818 if (auto result = EvaluateBuiltinIsWithinLifetime(*this, E))
16819 return Success(Value: *result, E);
16820 return false;
16821
16822 case Builtin::BI__builtin_ctz:
16823 case Builtin::BI__builtin_ctzl:
16824 case Builtin::BI__builtin_ctzll:
16825 case Builtin::BI__builtin_ctzs:
16826 case Builtin::BI__builtin_ctzg:
16827 case Builtin::BI__builtin_elementwise_ctzg: {
16828 APSInt Val;
16829 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
16830 APValue Vec;
16831 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
16832 return false;
16833 Val = ConvertBoolVectorToInt(Val: Vec);
16834 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
16835 return false;
16836 }
16837
16838 std::optional<APSInt> Fallback;
16839 if ((BuiltinOp == Builtin::BI__builtin_ctzg ||
16840 BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) &&
16841 E->getNumArgs() > 1) {
16842 APSInt FallbackTemp;
16843 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: FallbackTemp, Info))
16844 return false;
16845 Fallback = FallbackTemp;
16846 }
16847
16848 if (!Val) {
16849 if (Fallback)
16850 return Success(SI: *Fallback, E);
16851
16852 if (BuiltinOp == Builtin::BI__builtin_elementwise_ctzg) {
16853 Info.FFDiag(E, DiagId: diag::note_constexpr_countzeroes_zero)
16854 << /*IsTrailing=*/true;
16855 }
16856 return Error(E);
16857 }
16858
16859 return Success(Value: Val.countr_zero(), E);
16860 }
16861
16862 case Builtin::BI__builtin_eh_return_data_regno: {
16863 int Operand = E->getArg(Arg: 0)->EvaluateKnownConstInt(Ctx: Info.Ctx).getZExtValue();
16864 Operand = Info.Ctx.getTargetInfo().getEHDataRegisterNumber(RegNo: Operand);
16865 return Success(Value: Operand, E);
16866 }
16867
16868 case Builtin::BI__builtin_elementwise_abs: {
16869 APSInt Val;
16870 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
16871 return false;
16872
16873 return Success(I: Val.abs(), E);
16874 }
16875
16876 case Builtin::BI__builtin_expect:
16877 case Builtin::BI__builtin_expect_with_probability:
16878 return Visit(S: E->getArg(Arg: 0));
16879
16880 case Builtin::BI__builtin_ptrauth_string_discriminator: {
16881 const auto *Literal =
16882 cast<StringLiteral>(Val: E->getArg(Arg: 0)->IgnoreParenImpCasts());
16883 uint64_t Result = getPointerAuthStableSipHash(S: Literal->getString());
16884 return Success(Value: Result, E);
16885 }
16886
16887 case Builtin::BI__builtin_infer_alloc_token: {
16888 // If we fail to infer a type, this fails to be a constant expression; this
16889 // can be checked with __builtin_constant_p(...).
16890 QualType AllocType = infer_alloc::inferPossibleType(E, Ctx: Info.Ctx, CastE: nullptr);
16891 if (AllocType.isNull())
16892 return Error(
16893 E, D: diag::note_constexpr_infer_alloc_token_type_inference_failed);
16894 auto ATMD = infer_alloc::getAllocTokenMetadata(T: AllocType, Ctx: Info.Ctx);
16895 if (!ATMD)
16896 return Error(E, D: diag::note_constexpr_infer_alloc_token_no_metadata);
16897 auto Mode =
16898 Info.getLangOpts().AllocTokenMode.value_or(u: llvm::DefaultAllocTokenMode);
16899 uint64_t BitWidth = Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType());
16900 auto MaxTokensOpt = Info.getLangOpts().AllocTokenMax;
16901 uint64_t MaxTokens =
16902 MaxTokensOpt.value_or(u: 0) ? *MaxTokensOpt : (~0ULL >> (64 - BitWidth));
16903 auto MaybeToken = llvm::getAllocToken(Mode, Metadata: *ATMD, MaxTokens);
16904 if (!MaybeToken)
16905 return Error(E, D: diag::note_constexpr_infer_alloc_token_stateful_mode);
16906 return Success(I: llvm::APInt(BitWidth, *MaybeToken), E);
16907 }
16908
16909 case Builtin::BI__builtin_ffs:
16910 case Builtin::BI__builtin_ffsl:
16911 case Builtin::BI__builtin_ffsll: {
16912 APSInt Val;
16913 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
16914 return false;
16915
16916 unsigned N = Val.countr_zero();
16917 return Success(Value: N == Val.getBitWidth() ? 0 : N + 1, E);
16918 }
16919
16920 case Builtin::BI__builtin_fpclassify: {
16921 APFloat Val(0.0);
16922 if (!EvaluateFloat(E: E->getArg(Arg: 5), Result&: Val, Info))
16923 return false;
16924 unsigned Arg;
16925 switch (Val.getCategory()) {
16926 case APFloat::fcNaN: Arg = 0; break;
16927 case APFloat::fcInfinity: Arg = 1; break;
16928 case APFloat::fcNormal: Arg = Val.isDenormal() ? 3 : 2; break;
16929 case APFloat::fcZero: Arg = 4; break;
16930 }
16931 return Visit(S: E->getArg(Arg));
16932 }
16933
16934 case Builtin::BI__builtin_isinf_sign: {
16935 APFloat Val(0.0);
16936 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
16937 Success(Value: Val.isInfinity() ? (Val.isNegative() ? -1 : 1) : 0, E);
16938 }
16939
16940 case Builtin::BI__builtin_isinf: {
16941 APFloat Val(0.0);
16942 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
16943 Success(Value: Val.isInfinity() ? 1 : 0, E);
16944 }
16945
16946 case Builtin::BI__builtin_isfinite: {
16947 APFloat Val(0.0);
16948 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
16949 Success(Value: Val.isFinite() ? 1 : 0, E);
16950 }
16951
16952 case Builtin::BI__builtin_isnan: {
16953 APFloat Val(0.0);
16954 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
16955 Success(Value: Val.isNaN() ? 1 : 0, E);
16956 }
16957
16958 case Builtin::BI__builtin_isnormal: {
16959 APFloat Val(0.0);
16960 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
16961 Success(Value: Val.isNormal() ? 1 : 0, E);
16962 }
16963
16964 case Builtin::BI__builtin_issubnormal: {
16965 APFloat Val(0.0);
16966 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
16967 Success(Value: Val.isDenormal() ? 1 : 0, E);
16968 }
16969
16970 case Builtin::BI__builtin_iszero: {
16971 APFloat Val(0.0);
16972 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
16973 Success(Value: Val.isZero() ? 1 : 0, E);
16974 }
16975
16976 case Builtin::BI__builtin_signbit:
16977 case Builtin::BI__builtin_signbitf:
16978 case Builtin::BI__builtin_signbitl: {
16979 APFloat Val(0.0);
16980 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
16981 Success(Value: Val.isNegative() ? 1 : 0, E);
16982 }
16983
16984 case Builtin::BI__builtin_isgreater:
16985 case Builtin::BI__builtin_isgreaterequal:
16986 case Builtin::BI__builtin_isless:
16987 case Builtin::BI__builtin_islessequal:
16988 case Builtin::BI__builtin_islessgreater:
16989 case Builtin::BI__builtin_isunordered: {
16990 APFloat LHS(0.0);
16991 APFloat RHS(0.0);
16992 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
16993 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
16994 return false;
16995
16996 return Success(
16997 Value: [&] {
16998 switch (BuiltinOp) {
16999 case Builtin::BI__builtin_isgreater:
17000 return LHS > RHS;
17001 case Builtin::BI__builtin_isgreaterequal:
17002 return LHS >= RHS;
17003 case Builtin::BI__builtin_isless:
17004 return LHS < RHS;
17005 case Builtin::BI__builtin_islessequal:
17006 return LHS <= RHS;
17007 case Builtin::BI__builtin_islessgreater: {
17008 APFloat::cmpResult cmp = LHS.compare(RHS);
17009 return cmp == APFloat::cmpResult::cmpLessThan ||
17010 cmp == APFloat::cmpResult::cmpGreaterThan;
17011 }
17012 case Builtin::BI__builtin_isunordered:
17013 return LHS.compare(RHS) == APFloat::cmpResult::cmpUnordered;
17014 default:
17015 llvm_unreachable("Unexpected builtin ID: Should be a floating "
17016 "point comparison function");
17017 }
17018 }()
17019 ? 1
17020 : 0,
17021 E);
17022 }
17023
17024 case Builtin::BI__builtin_issignaling: {
17025 APFloat Val(0.0);
17026 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17027 Success(Value: Val.isSignaling() ? 1 : 0, E);
17028 }
17029
17030 case Builtin::BI__builtin_isfpclass: {
17031 APSInt MaskVal;
17032 if (!EvaluateInteger(E: E->getArg(Arg: 1), Result&: MaskVal, Info))
17033 return false;
17034 unsigned Test = static_cast<llvm::FPClassTest>(MaskVal.getZExtValue());
17035 APFloat Val(0.0);
17036 return EvaluateFloat(E: E->getArg(Arg: 0), Result&: Val, Info) &&
17037 Success(Value: (Val.classify() & Test) ? 1 : 0, E);
17038 }
17039
17040 case Builtin::BI__builtin_parity:
17041 case Builtin::BI__builtin_parityl:
17042 case Builtin::BI__builtin_parityll: {
17043 APSInt Val;
17044 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17045 return false;
17046
17047 return Success(Value: Val.popcount() % 2, E);
17048 }
17049
17050 case Builtin::BI__builtin_abs:
17051 case Builtin::BI__builtin_labs:
17052 case Builtin::BI__builtin_llabs: {
17053 APSInt Val;
17054 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17055 return false;
17056 if (Val == APSInt(APInt::getSignedMinValue(numBits: Val.getBitWidth()),
17057 /*IsUnsigned=*/false))
17058 return false;
17059 if (Val.isNegative())
17060 Val.negate();
17061 return Success(SI: Val, E);
17062 }
17063
17064 case Builtin::BI__builtin_popcount:
17065 case Builtin::BI__builtin_popcountl:
17066 case Builtin::BI__builtin_popcountll:
17067 case Builtin::BI__builtin_popcountg:
17068 case Builtin::BI__builtin_elementwise_popcount:
17069 case Builtin::BI__popcnt16: // Microsoft variants of popcount
17070 case Builtin::BI__popcnt:
17071 case Builtin::BI__popcnt64: {
17072 APSInt Val;
17073 if (E->getArg(Arg: 0)->getType()->isExtVectorBoolType()) {
17074 APValue Vec;
17075 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
17076 return false;
17077 Val = ConvertBoolVectorToInt(Val: Vec);
17078 } else if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info)) {
17079 return false;
17080 }
17081
17082 return Success(Value: Val.popcount(), E);
17083 }
17084
17085 case Builtin::BI__builtin_rotateleft8:
17086 case Builtin::BI__builtin_rotateleft16:
17087 case Builtin::BI__builtin_rotateleft32:
17088 case Builtin::BI__builtin_rotateleft64:
17089 case Builtin::BI__builtin_rotateright8:
17090 case Builtin::BI__builtin_rotateright16:
17091 case Builtin::BI__builtin_rotateright32:
17092 case Builtin::BI__builtin_rotateright64:
17093 case Builtin::BI__builtin_stdc_rotate_left:
17094 case Builtin::BI__builtin_stdc_rotate_right:
17095 case Builtin::BIstdc_rotate_left_uc:
17096 case Builtin::BIstdc_rotate_left_us:
17097 case Builtin::BIstdc_rotate_left_ui:
17098 case Builtin::BIstdc_rotate_left_ul:
17099 case Builtin::BIstdc_rotate_left_ull:
17100 case Builtin::BIstdc_rotate_right_uc:
17101 case Builtin::BIstdc_rotate_right_us:
17102 case Builtin::BIstdc_rotate_right_ui:
17103 case Builtin::BIstdc_rotate_right_ul:
17104 case Builtin::BIstdc_rotate_right_ull:
17105 case Builtin::BI_rotl8: // Microsoft variants of rotate left
17106 case Builtin::BI_rotl16:
17107 case Builtin::BI_rotl:
17108 case Builtin::BI_lrotl:
17109 case Builtin::BI_rotl64:
17110 case Builtin::BI_rotr8: // Microsoft variants of rotate right
17111 case Builtin::BI_rotr16:
17112 case Builtin::BI_rotr:
17113 case Builtin::BI_lrotr:
17114 case Builtin::BI_rotr64: {
17115 APSInt Value, Amount;
17116 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Value, Info) ||
17117 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Amount, Info))
17118 return false;
17119
17120 Amount = NormalizeRotateAmount(Value, Amount);
17121
17122 switch (BuiltinOp) {
17123 case Builtin::BI__builtin_rotateright8:
17124 case Builtin::BI__builtin_rotateright16:
17125 case Builtin::BI__builtin_rotateright32:
17126 case Builtin::BI__builtin_rotateright64:
17127 case Builtin::BI__builtin_stdc_rotate_right:
17128 case Builtin::BIstdc_rotate_right_uc:
17129 case Builtin::BIstdc_rotate_right_us:
17130 case Builtin::BIstdc_rotate_right_ui:
17131 case Builtin::BIstdc_rotate_right_ul:
17132 case Builtin::BIstdc_rotate_right_ull:
17133 case Builtin::BI_rotr8:
17134 case Builtin::BI_rotr16:
17135 case Builtin::BI_rotr:
17136 case Builtin::BI_lrotr:
17137 case Builtin::BI_rotr64:
17138 return Success(
17139 SI: APSInt(Value.rotr(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17140 default:
17141 return Success(
17142 SI: APSInt(Value.rotl(rotateAmt: Amount.getZExtValue()), Value.isUnsigned()), E);
17143 }
17144 }
17145
17146 case Builtin::BIstdc_leading_zeros_uc:
17147 case Builtin::BIstdc_leading_zeros_us:
17148 case Builtin::BIstdc_leading_zeros_ui:
17149 case Builtin::BIstdc_leading_zeros_ul:
17150 case Builtin::BIstdc_leading_zeros_ull:
17151 case Builtin::BIstdc_leading_ones_uc:
17152 case Builtin::BIstdc_leading_ones_us:
17153 case Builtin::BIstdc_leading_ones_ui:
17154 case Builtin::BIstdc_leading_ones_ul:
17155 case Builtin::BIstdc_leading_ones_ull:
17156 case Builtin::BIstdc_trailing_zeros_uc:
17157 case Builtin::BIstdc_trailing_zeros_us:
17158 case Builtin::BIstdc_trailing_zeros_ui:
17159 case Builtin::BIstdc_trailing_zeros_ul:
17160 case Builtin::BIstdc_trailing_zeros_ull:
17161 case Builtin::BIstdc_trailing_ones_uc:
17162 case Builtin::BIstdc_trailing_ones_us:
17163 case Builtin::BIstdc_trailing_ones_ui:
17164 case Builtin::BIstdc_trailing_ones_ul:
17165 case Builtin::BIstdc_trailing_ones_ull:
17166 case Builtin::BIstdc_first_leading_zero_uc:
17167 case Builtin::BIstdc_first_leading_zero_us:
17168 case Builtin::BIstdc_first_leading_zero_ui:
17169 case Builtin::BIstdc_first_leading_zero_ul:
17170 case Builtin::BIstdc_first_leading_zero_ull:
17171 case Builtin::BIstdc_first_leading_one_uc:
17172 case Builtin::BIstdc_first_leading_one_us:
17173 case Builtin::BIstdc_first_leading_one_ui:
17174 case Builtin::BIstdc_first_leading_one_ul:
17175 case Builtin::BIstdc_first_leading_one_ull:
17176 case Builtin::BIstdc_first_trailing_zero_uc:
17177 case Builtin::BIstdc_first_trailing_zero_us:
17178 case Builtin::BIstdc_first_trailing_zero_ui:
17179 case Builtin::BIstdc_first_trailing_zero_ul:
17180 case Builtin::BIstdc_first_trailing_zero_ull:
17181 case Builtin::BIstdc_first_trailing_one_uc:
17182 case Builtin::BIstdc_first_trailing_one_us:
17183 case Builtin::BIstdc_first_trailing_one_ui:
17184 case Builtin::BIstdc_first_trailing_one_ul:
17185 case Builtin::BIstdc_first_trailing_one_ull:
17186 case Builtin::BIstdc_count_zeros_uc:
17187 case Builtin::BIstdc_count_zeros_us:
17188 case Builtin::BIstdc_count_zeros_ui:
17189 case Builtin::BIstdc_count_zeros_ul:
17190 case Builtin::BIstdc_count_zeros_ull:
17191 case Builtin::BIstdc_count_ones_uc:
17192 case Builtin::BIstdc_count_ones_us:
17193 case Builtin::BIstdc_count_ones_ui:
17194 case Builtin::BIstdc_count_ones_ul:
17195 case Builtin::BIstdc_count_ones_ull:
17196 case Builtin::BIstdc_has_single_bit_uc:
17197 case Builtin::BIstdc_has_single_bit_us:
17198 case Builtin::BIstdc_has_single_bit_ui:
17199 case Builtin::BIstdc_has_single_bit_ul:
17200 case Builtin::BIstdc_has_single_bit_ull:
17201 case Builtin::BIstdc_bit_width_uc:
17202 case Builtin::BIstdc_bit_width_us:
17203 case Builtin::BIstdc_bit_width_ui:
17204 case Builtin::BIstdc_bit_width_ul:
17205 case Builtin::BIstdc_bit_width_ull:
17206 case Builtin::BIstdc_bit_floor_uc:
17207 case Builtin::BIstdc_bit_floor_us:
17208 case Builtin::BIstdc_bit_floor_ui:
17209 case Builtin::BIstdc_bit_floor_ul:
17210 case Builtin::BIstdc_bit_floor_ull:
17211 case Builtin::BIstdc_bit_ceil_uc:
17212 case Builtin::BIstdc_bit_ceil_us:
17213 case Builtin::BIstdc_bit_ceil_ui:
17214 case Builtin::BIstdc_bit_ceil_ul:
17215 case Builtin::BIstdc_bit_ceil_ull:
17216 case Builtin::BI__builtin_stdc_leading_zeros:
17217 case Builtin::BI__builtin_stdc_leading_ones:
17218 case Builtin::BI__builtin_stdc_trailing_zeros:
17219 case Builtin::BI__builtin_stdc_trailing_ones:
17220 case Builtin::BI__builtin_stdc_first_leading_zero:
17221 case Builtin::BI__builtin_stdc_first_leading_one:
17222 case Builtin::BI__builtin_stdc_first_trailing_zero:
17223 case Builtin::BI__builtin_stdc_first_trailing_one:
17224 case Builtin::BI__builtin_stdc_count_zeros:
17225 case Builtin::BI__builtin_stdc_count_ones:
17226 case Builtin::BI__builtin_stdc_has_single_bit:
17227 case Builtin::BI__builtin_stdc_bit_width:
17228 case Builtin::BI__builtin_stdc_bit_floor:
17229 case Builtin::BI__builtin_stdc_bit_ceil: {
17230 APSInt Val;
17231 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
17232 return false;
17233
17234 unsigned BitWidth = Val.getBitWidth();
17235 const unsigned ResBitWidth = Info.Ctx.getIntWidth(T: E->getType());
17236
17237 switch (BuiltinOp) {
17238 case Builtin::BIstdc_leading_zeros_uc:
17239 case Builtin::BIstdc_leading_zeros_us:
17240 case Builtin::BIstdc_leading_zeros_ui:
17241 case Builtin::BIstdc_leading_zeros_ul:
17242 case Builtin::BIstdc_leading_zeros_ull:
17243 case Builtin::BI__builtin_stdc_leading_zeros:
17244 return Success(I: APInt(ResBitWidth, Val.countl_zero()), E);
17245 case Builtin::BIstdc_leading_ones_uc:
17246 case Builtin::BIstdc_leading_ones_us:
17247 case Builtin::BIstdc_leading_ones_ui:
17248 case Builtin::BIstdc_leading_ones_ul:
17249 case Builtin::BIstdc_leading_ones_ull:
17250 case Builtin::BI__builtin_stdc_leading_ones:
17251 return Success(I: APInt(ResBitWidth, Val.countl_one()), E);
17252 case Builtin::BIstdc_trailing_zeros_uc:
17253 case Builtin::BIstdc_trailing_zeros_us:
17254 case Builtin::BIstdc_trailing_zeros_ui:
17255 case Builtin::BIstdc_trailing_zeros_ul:
17256 case Builtin::BIstdc_trailing_zeros_ull:
17257 case Builtin::BI__builtin_stdc_trailing_zeros:
17258 return Success(I: APInt(ResBitWidth, Val.countr_zero()), E);
17259 case Builtin::BIstdc_trailing_ones_uc:
17260 case Builtin::BIstdc_trailing_ones_us:
17261 case Builtin::BIstdc_trailing_ones_ui:
17262 case Builtin::BIstdc_trailing_ones_ul:
17263 case Builtin::BIstdc_trailing_ones_ull:
17264 case Builtin::BI__builtin_stdc_trailing_ones:
17265 return Success(I: APInt(ResBitWidth, Val.countr_one()), E);
17266 case Builtin::BIstdc_first_leading_zero_uc:
17267 case Builtin::BIstdc_first_leading_zero_us:
17268 case Builtin::BIstdc_first_leading_zero_ui:
17269 case Builtin::BIstdc_first_leading_zero_ul:
17270 case Builtin::BIstdc_first_leading_zero_ull:
17271 case Builtin::BI__builtin_stdc_first_leading_zero:
17272 return Success(
17273 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countl_one() + 1), E);
17274 case Builtin::BIstdc_first_leading_one_uc:
17275 case Builtin::BIstdc_first_leading_one_us:
17276 case Builtin::BIstdc_first_leading_one_ui:
17277 case Builtin::BIstdc_first_leading_one_ul:
17278 case Builtin::BIstdc_first_leading_one_ull:
17279 case Builtin::BI__builtin_stdc_first_leading_one:
17280 return Success(
17281 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countl_zero() + 1), E);
17282 case Builtin::BIstdc_first_trailing_zero_uc:
17283 case Builtin::BIstdc_first_trailing_zero_us:
17284 case Builtin::BIstdc_first_trailing_zero_ui:
17285 case Builtin::BIstdc_first_trailing_zero_ul:
17286 case Builtin::BIstdc_first_trailing_zero_ull:
17287 case Builtin::BI__builtin_stdc_first_trailing_zero:
17288 return Success(
17289 I: APInt(ResBitWidth, Val.isAllOnes() ? 0 : Val.countr_one() + 1), E);
17290 case Builtin::BIstdc_first_trailing_one_uc:
17291 case Builtin::BIstdc_first_trailing_one_us:
17292 case Builtin::BIstdc_first_trailing_one_ui:
17293 case Builtin::BIstdc_first_trailing_one_ul:
17294 case Builtin::BIstdc_first_trailing_one_ull:
17295 case Builtin::BI__builtin_stdc_first_trailing_one:
17296 return Success(
17297 I: APInt(ResBitWidth, Val.isZero() ? 0 : Val.countr_zero() + 1), E);
17298 case Builtin::BIstdc_count_zeros_uc:
17299 case Builtin::BIstdc_count_zeros_us:
17300 case Builtin::BIstdc_count_zeros_ui:
17301 case Builtin::BIstdc_count_zeros_ul:
17302 case Builtin::BIstdc_count_zeros_ull:
17303 case Builtin::BI__builtin_stdc_count_zeros: {
17304 APInt Cnt(ResBitWidth, BitWidth - Val.popcount());
17305 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17306 }
17307 case Builtin::BIstdc_count_ones_uc:
17308 case Builtin::BIstdc_count_ones_us:
17309 case Builtin::BIstdc_count_ones_ui:
17310 case Builtin::BIstdc_count_ones_ul:
17311 case Builtin::BIstdc_count_ones_ull:
17312 case Builtin::BI__builtin_stdc_count_ones: {
17313 APInt Cnt(ResBitWidth, Val.popcount());
17314 return Success(SI: APSInt(Cnt, /*IsUnsigned*/ true), E);
17315 }
17316 case Builtin::BIstdc_has_single_bit_uc:
17317 case Builtin::BIstdc_has_single_bit_us:
17318 case Builtin::BIstdc_has_single_bit_ui:
17319 case Builtin::BIstdc_has_single_bit_ul:
17320 case Builtin::BIstdc_has_single_bit_ull:
17321 case Builtin::BI__builtin_stdc_has_single_bit: {
17322 APInt Res(ResBitWidth, Val.popcount() == 1 ? 1 : 0);
17323 return Success(SI: APSInt(Res, /*IsUnsigned*/ true), E);
17324 }
17325 case Builtin::BIstdc_bit_width_uc:
17326 case Builtin::BIstdc_bit_width_us:
17327 case Builtin::BIstdc_bit_width_ui:
17328 case Builtin::BIstdc_bit_width_ul:
17329 case Builtin::BIstdc_bit_width_ull:
17330 case Builtin::BI__builtin_stdc_bit_width:
17331 return Success(I: APInt(ResBitWidth, BitWidth - Val.countl_zero()), E);
17332 case Builtin::BIstdc_bit_floor_uc:
17333 case Builtin::BIstdc_bit_floor_us:
17334 case Builtin::BIstdc_bit_floor_ui:
17335 case Builtin::BIstdc_bit_floor_ul:
17336 case Builtin::BIstdc_bit_floor_ull:
17337 case Builtin::BI__builtin_stdc_bit_floor: {
17338 if (Val.isZero())
17339 return Success(I: APInt(BitWidth, 0), E);
17340 unsigned Exp = BitWidth - Val.countl_zero() - 1;
17341 return Success(
17342 SI: APSInt(APInt::getOneBitSet(numBits: BitWidth, BitNo: Exp), /*IsUnsigned*/ true), E);
17343 }
17344 case Builtin::BIstdc_bit_ceil_uc:
17345 case Builtin::BIstdc_bit_ceil_us:
17346 case Builtin::BIstdc_bit_ceil_ui:
17347 case Builtin::BIstdc_bit_ceil_ul:
17348 case Builtin::BIstdc_bit_ceil_ull:
17349 case Builtin::BI__builtin_stdc_bit_ceil: {
17350 if (Val.ule(RHS: 1))
17351 return Success(SI: APSInt(APInt(BitWidth, 1), /*IsUnsigned*/ true), E);
17352 APInt ValMinusOne = Val - 1;
17353 unsigned LZ = ValMinusOne.countl_zero();
17354 if (LZ == 0)
17355 return Success(SI: APSInt(APInt(BitWidth, 0), /*IsUnsigned*/ true),
17356 E); // overflows; wrap to 0
17357 APInt Result = APInt::getOneBitSet(numBits: BitWidth, BitNo: BitWidth - LZ);
17358 return Success(SI: APSInt(Result, /*IsUnsigned*/ true), E);
17359 }
17360 default:
17361 llvm_unreachable("Unknown stdc builtin");
17362 }
17363 }
17364
17365 case Builtin::BI__builtin_elementwise_add_sat: {
17366 APSInt LHS, RHS;
17367 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17368 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17369 return false;
17370
17371 APInt Result = LHS.isSigned() ? LHS.sadd_sat(RHS) : LHS.uadd_sat(RHS);
17372 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17373 }
17374 case Builtin::BI__builtin_elementwise_sub_sat: {
17375 APSInt LHS, RHS;
17376 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17377 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17378 return false;
17379
17380 APInt Result = LHS.isSigned() ? LHS.ssub_sat(RHS) : LHS.usub_sat(RHS);
17381 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17382 }
17383 case Builtin::BI__builtin_elementwise_max: {
17384 APSInt LHS, RHS;
17385 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17386 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17387 return false;
17388
17389 APInt Result = std::max(a: LHS, b: RHS);
17390 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17391 }
17392 case Builtin::BI__builtin_elementwise_min: {
17393 APSInt LHS, RHS;
17394 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17395 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17396 return false;
17397
17398 APInt Result = std::min(a: LHS, b: RHS);
17399 return Success(SI: APSInt(Result, !LHS.isSigned()), E);
17400 }
17401 case Builtin::BI__builtin_elementwise_clmul: {
17402 APSInt LHS, RHS;
17403 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17404 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info))
17405 return false;
17406
17407 APInt Result = llvm::APIntOps::clmul(LHS, RHS);
17408 return Success(SI: APSInt(Result, LHS.isUnsigned()), E);
17409 }
17410 case Builtin::BI__builtin_elementwise_fshl:
17411 case Builtin::BI__builtin_elementwise_fshr: {
17412 APSInt Hi, Lo, Shift;
17413 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Hi, Info) ||
17414 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Lo, Info) ||
17415 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Shift, Info))
17416 return false;
17417
17418 switch (BuiltinOp) {
17419 case Builtin::BI__builtin_elementwise_fshl: {
17420 APSInt Result(llvm::APIntOps::fshl(Hi, Lo, Shift), Hi.isUnsigned());
17421 return Success(SI: Result, E);
17422 }
17423 case Builtin::BI__builtin_elementwise_fshr: {
17424 APSInt Result(llvm::APIntOps::fshr(Hi, Lo, Shift), Hi.isUnsigned());
17425 return Success(SI: Result, E);
17426 }
17427 }
17428 llvm_unreachable("Fully covered switch above");
17429 }
17430 case Builtin::BIstrlen:
17431 case Builtin::BIwcslen:
17432 // A call to strlen is not a constant expression.
17433 if (Info.getLangOpts().CPlusPlus11)
17434 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
17435 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
17436 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
17437 else
17438 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17439 [[fallthrough]];
17440 case Builtin::BI__builtin_strlen:
17441 case Builtin::BI__builtin_wcslen: {
17442 // As an extension, we support __builtin_strlen() as a constant expression,
17443 // and support folding strlen() to a constant.
17444 if (std::optional<uint64_t> StrLen =
17445 EvaluateBuiltinStrLen(E: E->getArg(Arg: 0), Info))
17446 return Success(Value: *StrLen, E);
17447 return false;
17448 }
17449
17450 case Builtin::BIstrcmp:
17451 case Builtin::BIwcscmp:
17452 case Builtin::BIstrncmp:
17453 case Builtin::BIwcsncmp:
17454 case Builtin::BImemcmp:
17455 case Builtin::BIbcmp:
17456 case Builtin::BIwmemcmp:
17457 // A call to strlen is not a constant expression.
17458 if (Info.getLangOpts().CPlusPlus11)
17459 Info.CCEDiag(E, DiagId: diag::note_constexpr_invalid_function)
17460 << /*isConstexpr*/ 0 << /*isConstructor*/ 0
17461 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp);
17462 else
17463 Info.CCEDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
17464 [[fallthrough]];
17465 case Builtin::BI__builtin_strcmp:
17466 case Builtin::BI__builtin_wcscmp:
17467 case Builtin::BI__builtin_strncmp:
17468 case Builtin::BI__builtin_wcsncmp:
17469 case Builtin::BI__builtin_memcmp:
17470 case Builtin::BI__builtin_bcmp:
17471 case Builtin::BI__builtin_wmemcmp: {
17472 LValue String1, String2;
17473 if (!EvaluatePointer(E: E->getArg(Arg: 0), Result&: String1, Info) ||
17474 !EvaluatePointer(E: E->getArg(Arg: 1), Result&: String2, Info))
17475 return false;
17476
17477 uint64_t MaxLength = uint64_t(-1);
17478 if (BuiltinOp != Builtin::BIstrcmp &&
17479 BuiltinOp != Builtin::BIwcscmp &&
17480 BuiltinOp != Builtin::BI__builtin_strcmp &&
17481 BuiltinOp != Builtin::BI__builtin_wcscmp) {
17482 APSInt N;
17483 if (!EvaluateInteger(E: E->getArg(Arg: 2), Result&: N, Info))
17484 return false;
17485 MaxLength = N.getZExtValue();
17486 }
17487
17488 // Empty substrings compare equal by definition.
17489 if (MaxLength == 0u)
17490 return Success(Value: 0, E);
17491
17492 if (!String1.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17493 !String2.checkNullPointerForFoldAccess(Info, E, AK: AK_Read) ||
17494 String1.Designator.Invalid || String2.Designator.Invalid)
17495 return false;
17496
17497 QualType CharTy1 = String1.Designator.getType(Ctx&: Info.Ctx);
17498 QualType CharTy2 = String2.Designator.getType(Ctx&: Info.Ctx);
17499
17500 bool IsRawByte = BuiltinOp == Builtin::BImemcmp ||
17501 BuiltinOp == Builtin::BIbcmp ||
17502 BuiltinOp == Builtin::BI__builtin_memcmp ||
17503 BuiltinOp == Builtin::BI__builtin_bcmp;
17504
17505 assert(IsRawByte ||
17506 (Info.Ctx.hasSameUnqualifiedType(
17507 CharTy1, E->getArg(0)->getType()->getPointeeType()) &&
17508 Info.Ctx.hasSameUnqualifiedType(CharTy1, CharTy2)));
17509
17510 // For memcmp, allow comparing any arrays of '[[un]signed] char' or
17511 // 'char8_t', but no other types.
17512 if (IsRawByte &&
17513 !(isOneByteCharacterType(T: CharTy1) && isOneByteCharacterType(T: CharTy2))) {
17514 // FIXME: Consider using our bit_cast implementation to support this.
17515 Info.FFDiag(E, DiagId: diag::note_constexpr_memcmp_unsupported)
17516 << Info.Ctx.BuiltinInfo.getQuotedName(ID: BuiltinOp) << CharTy1
17517 << CharTy2;
17518 return false;
17519 }
17520
17521 const auto &ReadCurElems = [&](APValue &Char1, APValue &Char2) {
17522 return handleLValueToRValueConversion(Info, Conv: E, Type: CharTy1, LVal: String1, RVal&: Char1) &&
17523 handleLValueToRValueConversion(Info, Conv: E, Type: CharTy2, LVal: String2, RVal&: Char2) &&
17524 Char1.isInt() && Char2.isInt();
17525 };
17526 const auto &AdvanceElems = [&] {
17527 return HandleLValueArrayAdjustment(Info, E, LVal&: String1, EltTy: CharTy1, Adjustment: 1) &&
17528 HandleLValueArrayAdjustment(Info, E, LVal&: String2, EltTy: CharTy2, Adjustment: 1);
17529 };
17530
17531 bool StopAtNull =
17532 (BuiltinOp != Builtin::BImemcmp && BuiltinOp != Builtin::BIbcmp &&
17533 BuiltinOp != Builtin::BIwmemcmp &&
17534 BuiltinOp != Builtin::BI__builtin_memcmp &&
17535 BuiltinOp != Builtin::BI__builtin_bcmp &&
17536 BuiltinOp != Builtin::BI__builtin_wmemcmp);
17537 bool IsWide = BuiltinOp == Builtin::BIwcscmp ||
17538 BuiltinOp == Builtin::BIwcsncmp ||
17539 BuiltinOp == Builtin::BIwmemcmp ||
17540 BuiltinOp == Builtin::BI__builtin_wcscmp ||
17541 BuiltinOp == Builtin::BI__builtin_wcsncmp ||
17542 BuiltinOp == Builtin::BI__builtin_wmemcmp;
17543
17544 for (; MaxLength; --MaxLength) {
17545 APValue Char1, Char2;
17546 if (!ReadCurElems(Char1, Char2))
17547 return false;
17548 if (Char1.getInt().ne(RHS: Char2.getInt())) {
17549 if (IsWide) // wmemcmp compares with wchar_t signedness.
17550 return Success(Value: Char1.getInt() < Char2.getInt() ? -1 : 1, E);
17551 // memcmp always compares unsigned chars.
17552 return Success(Value: Char1.getInt().ult(RHS: Char2.getInt()) ? -1 : 1, E);
17553 }
17554 if (StopAtNull && !Char1.getInt())
17555 return Success(Value: 0, E);
17556 assert(!(StopAtNull && !Char2.getInt()));
17557 if (!AdvanceElems())
17558 return false;
17559 }
17560 // We hit the strncmp / memcmp limit.
17561 return Success(Value: 0, E);
17562 }
17563
17564 case Builtin::BI__atomic_always_lock_free:
17565 case Builtin::BI__atomic_is_lock_free:
17566 case Builtin::BI__c11_atomic_is_lock_free: {
17567 APSInt SizeVal;
17568 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: SizeVal, Info))
17569 return false;
17570
17571 // For __atomic_is_lock_free(sizeof(_Atomic(T))), if the size is a power
17572 // of two less than or equal to the maximum inline atomic width, we know it
17573 // is lock-free. If the size isn't a power of two, or greater than the
17574 // maximum alignment where we promote atomics, we know it is not lock-free
17575 // (at least not in the sense of atomic_is_lock_free). Otherwise,
17576 // the answer can only be determined at runtime; for example, 16-byte
17577 // atomics have lock-free implementations on some, but not all,
17578 // x86-64 processors.
17579
17580 // Check power-of-two.
17581 CharUnits Size = CharUnits::fromQuantity(Quantity: SizeVal.getZExtValue());
17582 if (Size.isPowerOfTwo()) {
17583 // Check against inlining width.
17584 unsigned InlineWidthBits =
17585 Info.Ctx.getTargetInfo().getMaxAtomicInlineWidth();
17586 if (Size <= Info.Ctx.toCharUnitsFromBits(BitSize: InlineWidthBits)) {
17587 if (BuiltinOp == Builtin::BI__c11_atomic_is_lock_free ||
17588 Size == CharUnits::One())
17589 return Success(Value: 1, E);
17590
17591 // If the pointer argument can be evaluated to a compile-time constant
17592 // integer (or nullptr), check if that value is appropriately aligned.
17593 const Expr *PtrArg = E->getArg(Arg: 1);
17594 Expr::EvalResult ExprResult;
17595 APSInt IntResult;
17596 if (PtrArg->EvaluateAsRValue(Result&: ExprResult, Ctx: Info.Ctx) &&
17597 ExprResult.Val.toIntegralConstant(Result&: IntResult, SrcTy: PtrArg->getType(),
17598 Ctx: Info.Ctx) &&
17599 IntResult.isAligned(A: Size.getAsAlign()))
17600 return Success(Value: 1, E);
17601
17602 // Otherwise, check if the type's alignment against Size.
17603 if (auto *ICE = dyn_cast<ImplicitCastExpr>(Val: PtrArg)) {
17604 // Drop the potential implicit-cast to 'const volatile void*', getting
17605 // the underlying type.
17606 if (ICE->getCastKind() == CK_BitCast)
17607 PtrArg = ICE->getSubExpr();
17608 }
17609
17610 if (auto PtrTy = PtrArg->getType()->getAs<PointerType>()) {
17611 QualType PointeeType = PtrTy->getPointeeType();
17612 if (!PointeeType->isIncompleteType() &&
17613 Info.Ctx.getTypeAlignInChars(T: PointeeType) >= Size) {
17614 // OK, we will inline operations on this object.
17615 return Success(Value: 1, E);
17616 }
17617 }
17618 }
17619 }
17620
17621 return BuiltinOp == Builtin::BI__atomic_always_lock_free ?
17622 Success(Value: 0, E) : Error(E);
17623 }
17624 case Builtin::BI__builtin_addcb:
17625 case Builtin::BI__builtin_addcs:
17626 case Builtin::BI__builtin_addc:
17627 case Builtin::BI__builtin_addcl:
17628 case Builtin::BI__builtin_addcll:
17629 case Builtin::BI__builtin_subcb:
17630 case Builtin::BI__builtin_subcs:
17631 case Builtin::BI__builtin_subc:
17632 case Builtin::BI__builtin_subcl:
17633 case Builtin::BI__builtin_subcll: {
17634 LValue CarryOutLValue;
17635 APSInt LHS, RHS, CarryIn, CarryOut, Result;
17636 QualType ResultType = E->getArg(Arg: 0)->getType();
17637 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17638 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
17639 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: CarryIn, Info) ||
17640 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: CarryOutLValue, Info))
17641 return false;
17642 // Copy the number of bits and sign.
17643 Result = LHS;
17644 CarryOut = LHS;
17645
17646 bool FirstOverflowed = false;
17647 bool SecondOverflowed = false;
17648 switch (BuiltinOp) {
17649 default:
17650 llvm_unreachable("Invalid value for BuiltinOp");
17651 case Builtin::BI__builtin_addcb:
17652 case Builtin::BI__builtin_addcs:
17653 case Builtin::BI__builtin_addc:
17654 case Builtin::BI__builtin_addcl:
17655 case Builtin::BI__builtin_addcll:
17656 Result =
17657 LHS.uadd_ov(RHS, Overflow&: FirstOverflowed).uadd_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
17658 break;
17659 case Builtin::BI__builtin_subcb:
17660 case Builtin::BI__builtin_subcs:
17661 case Builtin::BI__builtin_subc:
17662 case Builtin::BI__builtin_subcl:
17663 case Builtin::BI__builtin_subcll:
17664 Result =
17665 LHS.usub_ov(RHS, Overflow&: FirstOverflowed).usub_ov(RHS: CarryIn, Overflow&: SecondOverflowed);
17666 break;
17667 }
17668
17669 // It is possible for both overflows to happen but CGBuiltin uses an OR so
17670 // this is consistent.
17671 CarryOut = (uint64_t)(FirstOverflowed | SecondOverflowed);
17672 APValue APV{CarryOut};
17673 if (!handleAssignment(Info, E, LVal: CarryOutLValue, LValType: ResultType, Val&: APV))
17674 return false;
17675 return Success(SI: Result, E);
17676 }
17677 case Builtin::BI__builtin_add_overflow:
17678 case Builtin::BI__builtin_sub_overflow:
17679 case Builtin::BI__builtin_mul_overflow:
17680 case Builtin::BI__builtin_sadd_overflow:
17681 case Builtin::BI__builtin_uadd_overflow:
17682 case Builtin::BI__builtin_uaddl_overflow:
17683 case Builtin::BI__builtin_uaddll_overflow:
17684 case Builtin::BI__builtin_usub_overflow:
17685 case Builtin::BI__builtin_usubl_overflow:
17686 case Builtin::BI__builtin_usubll_overflow:
17687 case Builtin::BI__builtin_umul_overflow:
17688 case Builtin::BI__builtin_umull_overflow:
17689 case Builtin::BI__builtin_umulll_overflow:
17690 case Builtin::BI__builtin_saddl_overflow:
17691 case Builtin::BI__builtin_saddll_overflow:
17692 case Builtin::BI__builtin_ssub_overflow:
17693 case Builtin::BI__builtin_ssubl_overflow:
17694 case Builtin::BI__builtin_ssubll_overflow:
17695 case Builtin::BI__builtin_smul_overflow:
17696 case Builtin::BI__builtin_smull_overflow:
17697 case Builtin::BI__builtin_smulll_overflow: {
17698 LValue ResultLValue;
17699 APSInt LHS, RHS;
17700
17701 QualType ResultType = E->getArg(Arg: 2)->getType()->getPointeeType();
17702 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
17703 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
17704 !EvaluatePointer(E: E->getArg(Arg: 2), Result&: ResultLValue, Info))
17705 return false;
17706
17707 APSInt Result;
17708 bool DidOverflow = false;
17709
17710 // If the types don't have to match, enlarge all 3 to the largest of them.
17711 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
17712 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
17713 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
17714 bool IsSigned = LHS.isSigned() || RHS.isSigned() ||
17715 ResultType->isSignedIntegerOrEnumerationType();
17716 bool AllSigned = LHS.isSigned() && RHS.isSigned() &&
17717 ResultType->isSignedIntegerOrEnumerationType();
17718 uint64_t LHSSize = LHS.getBitWidth();
17719 uint64_t RHSSize = RHS.getBitWidth();
17720 uint64_t ResultSize = Info.Ctx.getIntWidth(T: ResultType);
17721 uint64_t MaxBits = std::max(a: std::max(a: LHSSize, b: RHSSize), b: ResultSize);
17722
17723 // Add an additional bit if the signedness isn't uniformly agreed to. We
17724 // could do this ONLY if there is a signed and an unsigned that both have
17725 // MaxBits, but the code to check that is pretty nasty. The issue will be
17726 // caught in the shrink-to-result later anyway.
17727 if (IsSigned && !AllSigned)
17728 ++MaxBits;
17729
17730 LHS = APSInt(LHS.extOrTrunc(width: MaxBits), !IsSigned);
17731 RHS = APSInt(RHS.extOrTrunc(width: MaxBits), !IsSigned);
17732 Result = APSInt(MaxBits, !IsSigned);
17733 }
17734
17735 // Find largest int.
17736 switch (BuiltinOp) {
17737 default:
17738 llvm_unreachable("Invalid value for BuiltinOp");
17739 case Builtin::BI__builtin_add_overflow:
17740 case Builtin::BI__builtin_sadd_overflow:
17741 case Builtin::BI__builtin_saddl_overflow:
17742 case Builtin::BI__builtin_saddll_overflow:
17743 case Builtin::BI__builtin_uadd_overflow:
17744 case Builtin::BI__builtin_uaddl_overflow:
17745 case Builtin::BI__builtin_uaddll_overflow:
17746 Result = LHS.isSigned() ? LHS.sadd_ov(RHS, Overflow&: DidOverflow)
17747 : LHS.uadd_ov(RHS, Overflow&: DidOverflow);
17748 break;
17749 case Builtin::BI__builtin_sub_overflow:
17750 case Builtin::BI__builtin_ssub_overflow:
17751 case Builtin::BI__builtin_ssubl_overflow:
17752 case Builtin::BI__builtin_ssubll_overflow:
17753 case Builtin::BI__builtin_usub_overflow:
17754 case Builtin::BI__builtin_usubl_overflow:
17755 case Builtin::BI__builtin_usubll_overflow:
17756 Result = LHS.isSigned() ? LHS.ssub_ov(RHS, Overflow&: DidOverflow)
17757 : LHS.usub_ov(RHS, Overflow&: DidOverflow);
17758 break;
17759 case Builtin::BI__builtin_mul_overflow:
17760 case Builtin::BI__builtin_smul_overflow:
17761 case Builtin::BI__builtin_smull_overflow:
17762 case Builtin::BI__builtin_smulll_overflow:
17763 case Builtin::BI__builtin_umul_overflow:
17764 case Builtin::BI__builtin_umull_overflow:
17765 case Builtin::BI__builtin_umulll_overflow:
17766 Result = LHS.isSigned() ? LHS.smul_ov(RHS, Overflow&: DidOverflow)
17767 : LHS.umul_ov(RHS, Overflow&: DidOverflow);
17768 break;
17769 }
17770
17771 // APSInt doesn't have a TruncOrSelf, so we use extOrTrunc instead,
17772 // since it will give us the behavior of a TruncOrSelf in the case where
17773 // its parameter <= its size. We previously set Result to be at least the
17774 // integer width of the result, so getIntWidth(ResultType) <=
17775 // Result.BitWidth will work exactly like TruncOrSelf.
17776 APSInt Temp = Result.extOrTrunc(width: Info.Ctx.getIntWidth(T: ResultType));
17777 Temp.setIsSigned(ResultType->isSignedIntegerOrEnumerationType());
17778
17779 // In the case where multiple sizes are allowed, truncate and see if
17780 // the values are the same.
17781 if (BuiltinOp == Builtin::BI__builtin_add_overflow ||
17782 BuiltinOp == Builtin::BI__builtin_sub_overflow ||
17783 BuiltinOp == Builtin::BI__builtin_mul_overflow) {
17784 if (!APSInt::isSameValue(I1: Temp, I2: Result))
17785 DidOverflow = true;
17786 }
17787 Result = Temp;
17788
17789 APValue APV{Result};
17790 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
17791 return false;
17792 return Success(Value: DidOverflow, E);
17793 }
17794
17795 case Builtin::BI__builtin_reduce_add:
17796 case Builtin::BI__builtin_reduce_mul:
17797 case Builtin::BI__builtin_reduce_and:
17798 case Builtin::BI__builtin_reduce_or:
17799 case Builtin::BI__builtin_reduce_xor:
17800 case Builtin::BI__builtin_reduce_min:
17801 case Builtin::BI__builtin_reduce_max: {
17802 APValue Source;
17803 if (!EvaluateAsRValue(Info, E: E->getArg(Arg: 0), Result&: Source))
17804 return false;
17805
17806 unsigned SourceLen = Source.getVectorLength();
17807 APSInt Reduced = Source.getVectorElt(I: 0).getInt();
17808 for (unsigned EltNum = 1; EltNum < SourceLen; ++EltNum) {
17809 switch (BuiltinOp) {
17810 default:
17811 return false;
17812 case Builtin::BI__builtin_reduce_add: {
17813 if (!CheckedIntArithmetic(
17814 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
17815 BitWidth: Reduced.getBitWidth() + 1, Op: std::plus<APSInt>(), Result&: Reduced))
17816 return false;
17817 break;
17818 }
17819 case Builtin::BI__builtin_reduce_mul: {
17820 if (!CheckedIntArithmetic(
17821 Info, E, LHS: Reduced, RHS: Source.getVectorElt(I: EltNum).getInt(),
17822 BitWidth: Reduced.getBitWidth() * 2, Op: std::multiplies<APSInt>(), Result&: Reduced))
17823 return false;
17824 break;
17825 }
17826 case Builtin::BI__builtin_reduce_and: {
17827 Reduced &= Source.getVectorElt(I: EltNum).getInt();
17828 break;
17829 }
17830 case Builtin::BI__builtin_reduce_or: {
17831 Reduced |= Source.getVectorElt(I: EltNum).getInt();
17832 break;
17833 }
17834 case Builtin::BI__builtin_reduce_xor: {
17835 Reduced ^= Source.getVectorElt(I: EltNum).getInt();
17836 break;
17837 }
17838 case Builtin::BI__builtin_reduce_min: {
17839 Reduced = std::min(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
17840 break;
17841 }
17842 case Builtin::BI__builtin_reduce_max: {
17843 Reduced = std::max(a: Reduced, b: Source.getVectorElt(I: EltNum).getInt());
17844 break;
17845 }
17846 }
17847 }
17848
17849 return Success(SI: Reduced, E);
17850 }
17851
17852 case clang::X86::BI__builtin_ia32_addcarryx_u32:
17853 case clang::X86::BI__builtin_ia32_addcarryx_u64:
17854 case clang::X86::BI__builtin_ia32_subborrow_u32:
17855 case clang::X86::BI__builtin_ia32_subborrow_u64: {
17856 LValue ResultLValue;
17857 APSInt CarryIn, LHS, RHS;
17858 QualType ResultType = E->getArg(Arg: 3)->getType()->getPointeeType();
17859 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: CarryIn, Info) ||
17860 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: LHS, Info) ||
17861 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: RHS, Info) ||
17862 !EvaluatePointer(E: E->getArg(Arg: 3), Result&: ResultLValue, Info))
17863 return false;
17864
17865 bool IsAdd = BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u32 ||
17866 BuiltinOp == clang::X86::BI__builtin_ia32_addcarryx_u64;
17867
17868 unsigned BitWidth = LHS.getBitWidth();
17869 unsigned CarryInBit = CarryIn.ugt(RHS: 0) ? 1 : 0;
17870 APInt ExResult =
17871 IsAdd
17872 ? (LHS.zext(width: BitWidth + 1) + (RHS.zext(width: BitWidth + 1) + CarryInBit))
17873 : (LHS.zext(width: BitWidth + 1) - (RHS.zext(width: BitWidth + 1) + CarryInBit));
17874
17875 APInt Result = ExResult.extractBits(numBits: BitWidth, bitPosition: 0);
17876 uint64_t CarryOut = ExResult.extractBitsAsZExtValue(numBits: 1, bitPosition: BitWidth);
17877
17878 APValue APV{APSInt(Result, /*isUnsigned=*/true)};
17879 if (!handleAssignment(Info, E, LVal: ResultLValue, LValType: ResultType, Val&: APV))
17880 return false;
17881 return Success(Value: CarryOut, E);
17882 }
17883
17884 case clang::X86::BI__builtin_ia32_movmskps:
17885 case clang::X86::BI__builtin_ia32_movmskpd:
17886 case clang::X86::BI__builtin_ia32_pmovmskb128:
17887 case clang::X86::BI__builtin_ia32_pmovmskb256:
17888 case clang::X86::BI__builtin_ia32_movmskps256:
17889 case clang::X86::BI__builtin_ia32_movmskpd256: {
17890 APValue Source;
17891 if (!Evaluate(Result&: Source, Info, E: E->getArg(Arg: 0)))
17892 return false;
17893 unsigned SourceLen = Source.getVectorLength();
17894 const VectorType *VT = E->getArg(Arg: 0)->getType()->castAs<VectorType>();
17895 QualType ElemQT = VT->getElementType();
17896 unsigned ResultLen = Info.Ctx.getTypeSize(
17897 T: E->getCallReturnType(Ctx: Info.Ctx)); // Always 32-bit integer.
17898 APInt Result(ResultLen, 0);
17899
17900 for (unsigned I = 0; I != SourceLen; ++I) {
17901 APInt Elem;
17902 if (ElemQT->isIntegerType()) {
17903 Elem = Source.getVectorElt(I).getInt();
17904 } else if (ElemQT->isRealFloatingType()) {
17905 Elem = Source.getVectorElt(I).getFloat().bitcastToAPInt();
17906 } else {
17907 return false;
17908 }
17909 Result.setBitVal(BitPosition: I, BitValue: Elem.isNegative());
17910 }
17911 return Success(I: Result, E);
17912 }
17913
17914 case clang::X86::BI__builtin_ia32_bextr_u32:
17915 case clang::X86::BI__builtin_ia32_bextr_u64:
17916 case clang::X86::BI__builtin_ia32_bextri_u32:
17917 case clang::X86::BI__builtin_ia32_bextri_u64: {
17918 APSInt Val, Idx;
17919 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
17920 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
17921 return false;
17922
17923 unsigned BitWidth = Val.getBitWidth();
17924 uint64_t Shift = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
17925 uint64_t Length = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 8);
17926 Length = Length > BitWidth ? BitWidth : Length;
17927
17928 // Handle out of bounds cases.
17929 if (Length == 0 || Shift >= BitWidth)
17930 return Success(Value: 0, E);
17931
17932 uint64_t Result = Val.getZExtValue() >> Shift;
17933 Result &= llvm::maskTrailingOnes<uint64_t>(N: Length);
17934 return Success(Value: Result, E);
17935 }
17936
17937 case clang::X86::BI__builtin_ia32_bzhi_si:
17938 case clang::X86::BI__builtin_ia32_bzhi_di: {
17939 APSInt Val, Idx;
17940 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
17941 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Idx, Info))
17942 return false;
17943
17944 unsigned BitWidth = Val.getBitWidth();
17945 unsigned Index = Idx.extractBitsAsZExtValue(numBits: 8, bitPosition: 0);
17946 if (Index < BitWidth)
17947 Val.clearHighBits(hiBits: BitWidth - Index);
17948 return Success(SI: Val, E);
17949 }
17950
17951 case clang::X86::BI__builtin_ia32_ktestcqi:
17952 case clang::X86::BI__builtin_ia32_ktestchi:
17953 case clang::X86::BI__builtin_ia32_ktestcsi:
17954 case clang::X86::BI__builtin_ia32_ktestcdi: {
17955 APSInt A, B;
17956 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
17957 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
17958 return false;
17959
17960 return Success(Value: (~A & B) == 0, E);
17961 }
17962
17963 case clang::X86::BI__builtin_ia32_ktestzqi:
17964 case clang::X86::BI__builtin_ia32_ktestzhi:
17965 case clang::X86::BI__builtin_ia32_ktestzsi:
17966 case clang::X86::BI__builtin_ia32_ktestzdi: {
17967 APSInt A, B;
17968 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
17969 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
17970 return false;
17971
17972 return Success(Value: (A & B) == 0, E);
17973 }
17974
17975 case clang::X86::BI__builtin_ia32_kortestcqi:
17976 case clang::X86::BI__builtin_ia32_kortestchi:
17977 case clang::X86::BI__builtin_ia32_kortestcsi:
17978 case clang::X86::BI__builtin_ia32_kortestcdi: {
17979 APSInt A, B;
17980 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
17981 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
17982 return false;
17983
17984 return Success(Value: ~(A | B) == 0, E);
17985 }
17986
17987 case clang::X86::BI__builtin_ia32_kortestzqi:
17988 case clang::X86::BI__builtin_ia32_kortestzhi:
17989 case clang::X86::BI__builtin_ia32_kortestzsi:
17990 case clang::X86::BI__builtin_ia32_kortestzdi: {
17991 APSInt A, B;
17992 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
17993 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
17994 return false;
17995
17996 return Success(Value: (A | B) == 0, E);
17997 }
17998
17999 case clang::X86::BI__builtin_ia32_kunpckhi:
18000 case clang::X86::BI__builtin_ia32_kunpckdi:
18001 case clang::X86::BI__builtin_ia32_kunpcksi: {
18002 APSInt A, B;
18003 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: A, Info) ||
18004 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: B, Info))
18005 return false;
18006
18007 // Generic kunpack: extract lower half of each operand and concatenate
18008 // Result = A[HalfWidth-1:0] concat B[HalfWidth-1:0]
18009 unsigned BW = A.getBitWidth();
18010 APSInt Result(A.trunc(width: BW / 2).concat(NewLSB: B.trunc(width: BW / 2)), A.isUnsigned());
18011 return Success(SI: Result, E);
18012 }
18013
18014 case clang::X86::BI__builtin_ia32_lzcnt_u16:
18015 case clang::X86::BI__builtin_ia32_lzcnt_u32:
18016 case clang::X86::BI__builtin_ia32_lzcnt_u64: {
18017 APSInt Val;
18018 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18019 return false;
18020 return Success(Value: Val.countLeadingZeros(), E);
18021 }
18022
18023 case clang::X86::BI__builtin_ia32_tzcnt_u16:
18024 case clang::X86::BI__builtin_ia32_tzcnt_u32:
18025 case clang::X86::BI__builtin_ia32_tzcnt_u64: {
18026 APSInt Val;
18027 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18028 return false;
18029 return Success(Value: Val.countTrailingZeros(), E);
18030 }
18031
18032 case clang::X86::BI__builtin_ia32_pdep_si:
18033 case clang::X86::BI__builtin_ia32_pdep_di:
18034 case Builtin::BI__builtin_elementwise_pdep: {
18035 APSInt Val, Msk;
18036 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18037 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18038 return false;
18039 return Success(I: llvm::APIntOps::pdep(Val, Mask: Msk), E);
18040 }
18041
18042 case clang::X86::BI__builtin_ia32_pext_si:
18043 case clang::X86::BI__builtin_ia32_pext_di:
18044 case Builtin::BI__builtin_elementwise_pext: {
18045 APSInt Val, Msk;
18046 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info) ||
18047 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: Msk, Info))
18048 return false;
18049 return Success(I: llvm::APIntOps::pext(Val, Mask: Msk), E);
18050 }
18051 case X86::BI__builtin_ia32_ptestz128:
18052 case X86::BI__builtin_ia32_ptestz256:
18053 case X86::BI__builtin_ia32_vtestzps:
18054 case X86::BI__builtin_ia32_vtestzps256:
18055 case X86::BI__builtin_ia32_vtestzpd:
18056 case X86::BI__builtin_ia32_vtestzpd256: {
18057 return EvalTestOp(
18058 [](const APInt &A, const APInt &B) { return (A & B) == 0; });
18059 }
18060 case X86::BI__builtin_ia32_ptestc128:
18061 case X86::BI__builtin_ia32_ptestc256:
18062 case X86::BI__builtin_ia32_vtestcps:
18063 case X86::BI__builtin_ia32_vtestcps256:
18064 case X86::BI__builtin_ia32_vtestcpd:
18065 case X86::BI__builtin_ia32_vtestcpd256: {
18066 return EvalTestOp(
18067 [](const APInt &A, const APInt &B) { return (~A & B) == 0; });
18068 }
18069 case X86::BI__builtin_ia32_ptestnzc128:
18070 case X86::BI__builtin_ia32_ptestnzc256:
18071 case X86::BI__builtin_ia32_vtestnzcps:
18072 case X86::BI__builtin_ia32_vtestnzcps256:
18073 case X86::BI__builtin_ia32_vtestnzcpd:
18074 case X86::BI__builtin_ia32_vtestnzcpd256: {
18075 return EvalTestOp([](const APInt &A, const APInt &B) {
18076 return ((A & B) != 0) && ((~A & B) != 0);
18077 });
18078 }
18079 case X86::BI__builtin_ia32_kandqi:
18080 case X86::BI__builtin_ia32_kandhi:
18081 case X86::BI__builtin_ia32_kandsi:
18082 case X86::BI__builtin_ia32_kanddi: {
18083 return HandleMaskBinOp(
18084 [](const APSInt &LHS, const APSInt &RHS) { return LHS & RHS; });
18085 }
18086
18087 case X86::BI__builtin_ia32_kandnqi:
18088 case X86::BI__builtin_ia32_kandnhi:
18089 case X86::BI__builtin_ia32_kandnsi:
18090 case X86::BI__builtin_ia32_kandndi: {
18091 return HandleMaskBinOp(
18092 [](const APSInt &LHS, const APSInt &RHS) { return ~LHS & RHS; });
18093 }
18094
18095 case X86::BI__builtin_ia32_korqi:
18096 case X86::BI__builtin_ia32_korhi:
18097 case X86::BI__builtin_ia32_korsi:
18098 case X86::BI__builtin_ia32_kordi: {
18099 return HandleMaskBinOp(
18100 [](const APSInt &LHS, const APSInt &RHS) { return LHS | RHS; });
18101 }
18102
18103 case X86::BI__builtin_ia32_kxnorqi:
18104 case X86::BI__builtin_ia32_kxnorhi:
18105 case X86::BI__builtin_ia32_kxnorsi:
18106 case X86::BI__builtin_ia32_kxnordi: {
18107 return HandleMaskBinOp(
18108 [](const APSInt &LHS, const APSInt &RHS) { return ~(LHS ^ RHS); });
18109 }
18110
18111 case X86::BI__builtin_ia32_kxorqi:
18112 case X86::BI__builtin_ia32_kxorhi:
18113 case X86::BI__builtin_ia32_kxorsi:
18114 case X86::BI__builtin_ia32_kxordi: {
18115 return HandleMaskBinOp(
18116 [](const APSInt &LHS, const APSInt &RHS) { return LHS ^ RHS; });
18117 }
18118
18119 case X86::BI__builtin_ia32_knotqi:
18120 case X86::BI__builtin_ia32_knothi:
18121 case X86::BI__builtin_ia32_knotsi:
18122 case X86::BI__builtin_ia32_knotdi: {
18123 APSInt Val;
18124 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18125 return false;
18126 APSInt Result = ~Val;
18127 return Success(V: APValue(Result), E);
18128 }
18129
18130 case X86::BI__builtin_ia32_kaddqi:
18131 case X86::BI__builtin_ia32_kaddhi:
18132 case X86::BI__builtin_ia32_kaddsi:
18133 case X86::BI__builtin_ia32_kadddi: {
18134 return HandleMaskBinOp(
18135 [](const APSInt &LHS, const APSInt &RHS) { return LHS + RHS; });
18136 }
18137
18138 case X86::BI__builtin_ia32_kmovb:
18139 case X86::BI__builtin_ia32_kmovw:
18140 case X86::BI__builtin_ia32_kmovd:
18141 case X86::BI__builtin_ia32_kmovq: {
18142 APSInt Val;
18143 if (!EvaluateInteger(E: E->getArg(Arg: 0), Result&: Val, Info))
18144 return false;
18145 return Success(SI: Val, E);
18146 }
18147
18148 case X86::BI__builtin_ia32_kshiftliqi:
18149 case X86::BI__builtin_ia32_kshiftlihi:
18150 case X86::BI__builtin_ia32_kshiftlisi:
18151 case X86::BI__builtin_ia32_kshiftlidi: {
18152 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18153 unsigned Amt = RHS.getZExtValue() & 0xFF;
18154 if (Amt >= LHS.getBitWidth())
18155 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18156 return APSInt(LHS.shl(shiftAmt: Amt), LHS.isUnsigned());
18157 });
18158 }
18159
18160 case X86::BI__builtin_ia32_kshiftriqi:
18161 case X86::BI__builtin_ia32_kshiftrihi:
18162 case X86::BI__builtin_ia32_kshiftrisi:
18163 case X86::BI__builtin_ia32_kshiftridi: {
18164 return HandleMaskBinOp([](const APSInt &LHS, const APSInt &RHS) {
18165 unsigned Amt = RHS.getZExtValue() & 0xFF;
18166 if (Amt >= LHS.getBitWidth())
18167 return APSInt(APInt::getZero(numBits: LHS.getBitWidth()), LHS.isUnsigned());
18168 return APSInt(LHS.lshr(shiftAmt: Amt), LHS.isUnsigned());
18169 });
18170 }
18171
18172 case clang::X86::BI__builtin_ia32_vec_ext_v4hi:
18173 case clang::X86::BI__builtin_ia32_vec_ext_v16qi:
18174 case clang::X86::BI__builtin_ia32_vec_ext_v8hi:
18175 case clang::X86::BI__builtin_ia32_vec_ext_v4si:
18176 case clang::X86::BI__builtin_ia32_vec_ext_v2di:
18177 case clang::X86::BI__builtin_ia32_vec_ext_v32qi:
18178 case clang::X86::BI__builtin_ia32_vec_ext_v16hi:
18179 case clang::X86::BI__builtin_ia32_vec_ext_v8si:
18180 case clang::X86::BI__builtin_ia32_vec_ext_v4di: {
18181 APValue Vec;
18182 APSInt IdxAPS;
18183 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
18184 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
18185 return false;
18186 unsigned N = Vec.getVectorLength();
18187 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
18188 return Success(SI: Vec.getVectorElt(I: Idx).getInt(), E);
18189 }
18190
18191 case clang::X86::BI__builtin_ia32_cvtb2mask128:
18192 case clang::X86::BI__builtin_ia32_cvtb2mask256:
18193 case clang::X86::BI__builtin_ia32_cvtb2mask512:
18194 case clang::X86::BI__builtin_ia32_cvtw2mask128:
18195 case clang::X86::BI__builtin_ia32_cvtw2mask256:
18196 case clang::X86::BI__builtin_ia32_cvtw2mask512:
18197 case clang::X86::BI__builtin_ia32_cvtd2mask128:
18198 case clang::X86::BI__builtin_ia32_cvtd2mask256:
18199 case clang::X86::BI__builtin_ia32_cvtd2mask512:
18200 case clang::X86::BI__builtin_ia32_cvtq2mask128:
18201 case clang::X86::BI__builtin_ia32_cvtq2mask256:
18202 case clang::X86::BI__builtin_ia32_cvtq2mask512: {
18203 assert(E->getNumArgs() == 1);
18204 APValue Vec;
18205 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info))
18206 return false;
18207
18208 unsigned VectorLen = Vec.getVectorLength();
18209 unsigned RetWidth = Info.Ctx.getIntWidth(T: E->getType());
18210 llvm::APInt Bits(RetWidth, 0);
18211
18212 for (unsigned ElemNum = 0; ElemNum != VectorLen; ++ElemNum) {
18213 const APSInt &A = Vec.getVectorElt(I: ElemNum).getInt();
18214 unsigned MSB = A[A.getBitWidth() - 1];
18215 Bits.setBitVal(BitPosition: ElemNum, BitValue: MSB);
18216 }
18217
18218 APSInt RetMask(Bits, /*isUnsigned=*/true);
18219 return Success(V: APValue(RetMask), E);
18220 }
18221
18222 case clang::X86::BI__builtin_ia32_cmpb128_mask:
18223 case clang::X86::BI__builtin_ia32_cmpw128_mask:
18224 case clang::X86::BI__builtin_ia32_cmpd128_mask:
18225 case clang::X86::BI__builtin_ia32_cmpq128_mask:
18226 case clang::X86::BI__builtin_ia32_cmpb256_mask:
18227 case clang::X86::BI__builtin_ia32_cmpw256_mask:
18228 case clang::X86::BI__builtin_ia32_cmpd256_mask:
18229 case clang::X86::BI__builtin_ia32_cmpq256_mask:
18230 case clang::X86::BI__builtin_ia32_cmpb512_mask:
18231 case clang::X86::BI__builtin_ia32_cmpw512_mask:
18232 case clang::X86::BI__builtin_ia32_cmpd512_mask:
18233 case clang::X86::BI__builtin_ia32_cmpq512_mask:
18234 case clang::X86::BI__builtin_ia32_ucmpb128_mask:
18235 case clang::X86::BI__builtin_ia32_ucmpw128_mask:
18236 case clang::X86::BI__builtin_ia32_ucmpd128_mask:
18237 case clang::X86::BI__builtin_ia32_ucmpq128_mask:
18238 case clang::X86::BI__builtin_ia32_ucmpb256_mask:
18239 case clang::X86::BI__builtin_ia32_ucmpw256_mask:
18240 case clang::X86::BI__builtin_ia32_ucmpd256_mask:
18241 case clang::X86::BI__builtin_ia32_ucmpq256_mask:
18242 case clang::X86::BI__builtin_ia32_ucmpb512_mask:
18243 case clang::X86::BI__builtin_ia32_ucmpw512_mask:
18244 case clang::X86::BI__builtin_ia32_ucmpd512_mask:
18245 case clang::X86::BI__builtin_ia32_ucmpq512_mask: {
18246 assert(E->getNumArgs() == 4);
18247
18248 bool IsUnsigned =
18249 (BuiltinOp >= clang::X86::BI__builtin_ia32_ucmpb128_mask &&
18250 BuiltinOp <= clang::X86::BI__builtin_ia32_ucmpw512_mask);
18251
18252 APValue LHS, RHS;
18253 APSInt Mask, Opcode;
18254 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: LHS, Info) ||
18255 !EvaluateVector(E: E->getArg(Arg: 1), Result&: RHS, Info) ||
18256 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: Opcode, Info) ||
18257 !EvaluateInteger(E: E->getArg(Arg: 3), Result&: Mask, Info))
18258 return false;
18259
18260 assert(LHS.getVectorLength() == RHS.getVectorLength());
18261
18262 unsigned VectorLen = LHS.getVectorLength();
18263 unsigned RetWidth = Mask.getBitWidth();
18264
18265 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18266
18267 for (unsigned ElemNum = 0; ElemNum < VectorLen; ++ElemNum) {
18268 const APSInt &A = LHS.getVectorElt(I: ElemNum).getInt();
18269 const APSInt &B = RHS.getVectorElt(I: ElemNum).getInt();
18270 bool Result = false;
18271
18272 switch (Opcode.getExtValue() & 0x7) {
18273 case 0: // _MM_CMPINT_EQ
18274 Result = (A == B);
18275 break;
18276 case 1: // _MM_CMPINT_LT
18277 Result = IsUnsigned ? A.ult(RHS: B) : A.slt(RHS: B);
18278 break;
18279 case 2: // _MM_CMPINT_LE
18280 Result = IsUnsigned ? A.ule(RHS: B) : A.sle(RHS: B);
18281 break;
18282 case 3: // _MM_CMPINT_FALSE
18283 Result = false;
18284 break;
18285 case 4: // _MM_CMPINT_NE
18286 Result = (A != B);
18287 break;
18288 case 5: // _MM_CMPINT_NLT (>=)
18289 Result = IsUnsigned ? A.uge(RHS: B) : A.sge(RHS: B);
18290 break;
18291 case 6: // _MM_CMPINT_NLE (>)
18292 Result = IsUnsigned ? A.ugt(RHS: B) : A.sgt(RHS: B);
18293 break;
18294 case 7: // _MM_CMPINT_TRUE
18295 Result = true;
18296 break;
18297 }
18298
18299 RetMask.setBitVal(BitPosition: ElemNum, BitValue: Mask[ElemNum] && Result);
18300 }
18301
18302 return Success(V: APValue(RetMask), E);
18303 }
18304 case X86::BI__builtin_ia32_vpshufbitqmb128_mask:
18305 case X86::BI__builtin_ia32_vpshufbitqmb256_mask:
18306 case X86::BI__builtin_ia32_vpshufbitqmb512_mask: {
18307 assert(E->getNumArgs() == 3);
18308
18309 APValue Source, ShuffleMask;
18310 APSInt ZeroMask;
18311 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Source, Info) ||
18312 !EvaluateVector(E: E->getArg(Arg: 1), Result&: ShuffleMask, Info) ||
18313 !EvaluateInteger(E: E->getArg(Arg: 2), Result&: ZeroMask, Info))
18314 return false;
18315
18316 assert(Source.getVectorLength() == ShuffleMask.getVectorLength());
18317 assert(ZeroMask.getBitWidth() == Source.getVectorLength());
18318
18319 unsigned NumBytesInQWord = 8;
18320 unsigned NumBitsInByte = 8;
18321 unsigned NumBytes = Source.getVectorLength();
18322 unsigned NumQWords = NumBytes / NumBytesInQWord;
18323 unsigned RetWidth = ZeroMask.getBitWidth();
18324 APSInt RetMask(llvm::APInt(RetWidth, 0), /*isUnsigned=*/true);
18325
18326 for (unsigned QWordId = 0; QWordId != NumQWords; ++QWordId) {
18327 APInt SourceQWord(64, 0);
18328 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18329 uint64_t Byte = Source.getVectorElt(I: QWordId * NumBytesInQWord + ByteIdx)
18330 .getInt()
18331 .getZExtValue();
18332 SourceQWord.insertBits(SubBits: APInt(8, Byte & 0xFF), bitPosition: ByteIdx * NumBitsInByte);
18333 }
18334
18335 for (unsigned ByteIdx = 0; ByteIdx != NumBytesInQWord; ++ByteIdx) {
18336 unsigned SelIdx = QWordId * NumBytesInQWord + ByteIdx;
18337 unsigned M =
18338 ShuffleMask.getVectorElt(I: SelIdx).getInt().getZExtValue() & 0x3F;
18339 if (ZeroMask[SelIdx]) {
18340 RetMask.setBitVal(BitPosition: SelIdx, BitValue: SourceQWord[M]);
18341 }
18342 }
18343 }
18344 return Success(V: APValue(RetMask), E);
18345 }
18346 }
18347}
18348
18349/// Determine whether this is a pointer past the end of the complete
18350/// object referred to by the lvalue.
18351static bool isOnePastTheEndOfCompleteObject(const ASTContext &Ctx,
18352 const LValue &LV) {
18353 // A null pointer can be viewed as being "past the end" but we don't
18354 // choose to look at it that way here.
18355 if (!LV.getLValueBase())
18356 return false;
18357
18358 // If the designator is valid and refers to a subobject, we're not pointing
18359 // past the end.
18360 if (!LV.getLValueDesignator().Invalid &&
18361 !LV.getLValueDesignator().isOnePastTheEnd())
18362 return false;
18363
18364 // A pointer to an incomplete type might be past-the-end if the type's size is
18365 // zero. We cannot tell because the type is incomplete.
18366 QualType Ty = getType(B: LV.getLValueBase());
18367 if (Ty->isIncompleteType())
18368 return true;
18369
18370 // Can't be past the end of an invalid object.
18371 if (LV.getLValueDesignator().Invalid)
18372 return false;
18373
18374 // We're a past-the-end pointer if we point to the byte after the object,
18375 // no matter what our type or path is.
18376 auto Size = Ctx.getTypeSizeInChars(T: Ty);
18377 return LV.getLValueOffset() == Size;
18378}
18379
18380namespace {
18381
18382/// Data recursive integer evaluator of certain binary operators.
18383///
18384/// We use a data recursive algorithm for binary operators so that we are able
18385/// to handle extreme cases of chained binary operators without causing stack
18386/// overflow.
18387class DataRecursiveIntBinOpEvaluator {
18388 struct EvalResult {
18389 APValue Val;
18390 bool Failed = false;
18391
18392 EvalResult() = default;
18393
18394 void swap(EvalResult &RHS) {
18395 Val.swap(RHS&: RHS.Val);
18396 Failed = RHS.Failed;
18397 RHS.Failed = false;
18398 }
18399 };
18400
18401 struct Job {
18402 const Expr *E;
18403 EvalResult LHSResult; // meaningful only for binary operator expression.
18404 enum { AnyExprKind, BinOpKind, BinOpVisitedLHSKind } Kind;
18405
18406 Job() = default;
18407 Job(Job &&) = default;
18408
18409 void startSpeculativeEval(EvalInfo &Info) {
18410 SpecEvalRAII = SpeculativeEvaluationRAII(Info);
18411 }
18412
18413 private:
18414 SpeculativeEvaluationRAII SpecEvalRAII;
18415 };
18416
18417 SmallVector<Job, 16> Queue;
18418
18419 IntExprEvaluator &IntEval;
18420 EvalInfo &Info;
18421 APValue &FinalResult;
18422
18423public:
18424 DataRecursiveIntBinOpEvaluator(IntExprEvaluator &IntEval, APValue &Result)
18425 : IntEval(IntEval), Info(IntEval.getEvalInfo()), FinalResult(Result) { }
18426
18427 /// True if \param E is a binary operator that we are going to handle
18428 /// data recursively.
18429 /// We handle binary operators that are comma, logical, or that have operands
18430 /// with integral or enumeration type.
18431 static bool shouldEnqueue(const BinaryOperator *E) {
18432 return E->getOpcode() == BO_Comma || E->isLogicalOp() ||
18433 (E->isPRValue() && E->getType()->isIntegralOrEnumerationType() &&
18434 E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18435 E->getRHS()->getType()->isIntegralOrEnumerationType());
18436 }
18437
18438 bool Traverse(const BinaryOperator *E) {
18439 enqueue(E);
18440 EvalResult PrevResult;
18441 while (!Queue.empty())
18442 process(Result&: PrevResult);
18443
18444 if (PrevResult.Failed) return false;
18445
18446 FinalResult.swap(RHS&: PrevResult.Val);
18447 return true;
18448 }
18449
18450private:
18451 bool Success(uint64_t Value, const Expr *E, APValue &Result) {
18452 return IntEval.Success(Value, E, Result);
18453 }
18454 bool Success(const APSInt &Value, const Expr *E, APValue &Result) {
18455 return IntEval.Success(SI: Value, E, Result);
18456 }
18457 bool Error(const Expr *E) {
18458 return IntEval.Error(E);
18459 }
18460 bool Error(const Expr *E, diag::kind D) {
18461 return IntEval.Error(E, D);
18462 }
18463
18464 OptionalDiagnostic CCEDiag(const Expr *E, diag::kind D) {
18465 return Info.CCEDiag(E, DiagId: D);
18466 }
18467
18468 // Returns true if visiting the RHS is necessary, false otherwise.
18469 bool VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
18470 bool &SuppressRHSDiags);
18471
18472 bool VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
18473 const BinaryOperator *E, APValue &Result);
18474
18475 void EvaluateExpr(const Expr *E, EvalResult &Result) {
18476 Result.Failed = !Evaluate(Result&: Result.Val, Info, E);
18477 if (Result.Failed)
18478 Result.Val = APValue();
18479 }
18480
18481 void process(EvalResult &Result);
18482
18483 void enqueue(const Expr *E) {
18484 E = E->IgnoreParens();
18485 Queue.resize(N: Queue.size()+1);
18486 Queue.back().E = E;
18487 Queue.back().Kind = Job::AnyExprKind;
18488 }
18489};
18490
18491}
18492
18493bool DataRecursiveIntBinOpEvaluator::
18494 VisitBinOpLHSOnly(EvalResult &LHSResult, const BinaryOperator *E,
18495 bool &SuppressRHSDiags) {
18496 if (E->getOpcode() == BO_Comma) {
18497 // Ignore LHS but note if we could not evaluate it.
18498 if (LHSResult.Failed)
18499 return Info.noteSideEffect();
18500 return true;
18501 }
18502
18503 if (E->isLogicalOp()) {
18504 bool LHSAsBool;
18505 if (!LHSResult.Failed && HandleConversionToBool(Val: LHSResult.Val, Result&: LHSAsBool)) {
18506 // We were able to evaluate the LHS, see if we can get away with not
18507 // evaluating the RHS: 0 && X -> 0, 1 || X -> 1
18508 if (LHSAsBool == (E->getOpcode() == BO_LOr)) {
18509 Success(Value: LHSAsBool, E, Result&: LHSResult.Val);
18510 return false; // Ignore RHS
18511 }
18512 } else {
18513 LHSResult.Failed = true;
18514
18515 // Since we weren't able to evaluate the left hand side, it
18516 // might have had side effects.
18517 if (!Info.noteSideEffect())
18518 return false;
18519
18520 // We can't evaluate the LHS; however, sometimes the result
18521 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
18522 // Don't ignore RHS and suppress diagnostics from this arm.
18523 SuppressRHSDiags = true;
18524 }
18525
18526 return true;
18527 }
18528
18529 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18530 E->getRHS()->getType()->isIntegralOrEnumerationType());
18531
18532 if (LHSResult.Failed && !Info.noteFailure())
18533 return false; // Ignore RHS;
18534
18535 return true;
18536}
18537
18538static void addOrSubLValueAsInteger(APValue &LVal, const APSInt &Index,
18539 bool IsSub) {
18540 // Compute the new offset in the appropriate width, wrapping at 64 bits.
18541 // FIXME: When compiling for a 32-bit target, we should use 32-bit
18542 // offsets.
18543 assert(!LVal.hasLValuePath() && "have designator for integer lvalue");
18544 CharUnits &Offset = LVal.getLValueOffset();
18545 uint64_t Offset64 = Offset.getQuantity();
18546 uint64_t Index64 = Index.extOrTrunc(width: 64).getZExtValue();
18547 Offset = CharUnits::fromQuantity(Quantity: IsSub ? Offset64 - Index64
18548 : Offset64 + Index64);
18549}
18550
18551bool DataRecursiveIntBinOpEvaluator::
18552 VisitBinOp(const EvalResult &LHSResult, const EvalResult &RHSResult,
18553 const BinaryOperator *E, APValue &Result) {
18554 if (E->getOpcode() == BO_Comma) {
18555 if (RHSResult.Failed)
18556 return false;
18557 Result = RHSResult.Val;
18558 return true;
18559 }
18560
18561 if (E->isLogicalOp()) {
18562 bool lhsResult, rhsResult;
18563 bool LHSIsOK = HandleConversionToBool(Val: LHSResult.Val, Result&: lhsResult);
18564 bool RHSIsOK = HandleConversionToBool(Val: RHSResult.Val, Result&: rhsResult);
18565
18566 if (LHSIsOK) {
18567 if (RHSIsOK) {
18568 if (E->getOpcode() == BO_LOr)
18569 return Success(Value: lhsResult || rhsResult, E, Result);
18570 else
18571 return Success(Value: lhsResult && rhsResult, E, Result);
18572 }
18573 } else {
18574 if (RHSIsOK) {
18575 // We can't evaluate the LHS; however, sometimes the result
18576 // is determined by the RHS: X && 0 -> 0, X || 1 -> 1.
18577 if (rhsResult == (E->getOpcode() == BO_LOr))
18578 return Success(Value: rhsResult, E, Result);
18579 }
18580 }
18581
18582 return false;
18583 }
18584
18585 assert(E->getLHS()->getType()->isIntegralOrEnumerationType() &&
18586 E->getRHS()->getType()->isIntegralOrEnumerationType());
18587
18588 if (LHSResult.Failed || RHSResult.Failed)
18589 return false;
18590
18591 const APValue &LHSVal = LHSResult.Val;
18592 const APValue &RHSVal = RHSResult.Val;
18593
18594 // Handle cases like (unsigned long)&a + 4.
18595 if (E->isAdditiveOp() && LHSVal.isLValue() && RHSVal.isInt()) {
18596 Result = LHSVal;
18597 addOrSubLValueAsInteger(LVal&: Result, Index: RHSVal.getInt(), IsSub: E->getOpcode() == BO_Sub);
18598 return true;
18599 }
18600
18601 // Handle cases like 4 + (unsigned long)&a
18602 if (E->getOpcode() == BO_Add &&
18603 RHSVal.isLValue() && LHSVal.isInt()) {
18604 Result = RHSVal;
18605 addOrSubLValueAsInteger(LVal&: Result, Index: LHSVal.getInt(), /*IsSub*/false);
18606 return true;
18607 }
18608
18609 if (E->getOpcode() == BO_Sub && LHSVal.isLValue() && RHSVal.isLValue()) {
18610 // Handle (intptr_t)&&A - (intptr_t)&&B.
18611 if (!LHSVal.getLValueOffset().isZero() ||
18612 !RHSVal.getLValueOffset().isZero())
18613 return false;
18614 const Expr *LHSExpr = LHSVal.getLValueBase().dyn_cast<const Expr*>();
18615 const Expr *RHSExpr = RHSVal.getLValueBase().dyn_cast<const Expr*>();
18616 if (!LHSExpr || !RHSExpr)
18617 return false;
18618 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
18619 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
18620 if (!LHSAddrExpr || !RHSAddrExpr)
18621 return false;
18622 // Make sure both labels come from the same function.
18623 if (LHSAddrExpr->getLabel()->getDeclContext() !=
18624 RHSAddrExpr->getLabel()->getDeclContext())
18625 return false;
18626 Result = APValue(LHSAddrExpr, RHSAddrExpr);
18627 return true;
18628 }
18629
18630 // All the remaining cases expect both operands to be an integer
18631 if (!LHSVal.isInt() || !RHSVal.isInt())
18632 return Error(E);
18633
18634 // Set up the width and signedness manually, in case it can't be deduced
18635 // from the operation we're performing.
18636 // FIXME: Don't do this in the cases where we can deduce it.
18637 APSInt Value(Info.Ctx.getIntWidth(T: E->getType()),
18638 E->getType()->isUnsignedIntegerOrEnumerationType());
18639 if (!handleIntIntBinOp(Info, E, LHS: LHSVal.getInt(), Opcode: E->getOpcode(),
18640 RHS: RHSVal.getInt(), Result&: Value))
18641 return false;
18642 return Success(Value, E, Result);
18643}
18644
18645void DataRecursiveIntBinOpEvaluator::process(EvalResult &Result) {
18646 Job &job = Queue.back();
18647
18648 switch (job.Kind) {
18649 case Job::AnyExprKind: {
18650 if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(Val: job.E)) {
18651 if (shouldEnqueue(E: Bop)) {
18652 job.Kind = Job::BinOpKind;
18653 enqueue(E: Bop->getLHS());
18654 return;
18655 }
18656 }
18657
18658 EvaluateExpr(E: job.E, Result);
18659 Queue.pop_back();
18660 return;
18661 }
18662
18663 case Job::BinOpKind: {
18664 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
18665 bool SuppressRHSDiags = false;
18666 if (!VisitBinOpLHSOnly(LHSResult&: Result, E: Bop, SuppressRHSDiags)) {
18667 Queue.pop_back();
18668 return;
18669 }
18670 if (SuppressRHSDiags)
18671 job.startSpeculativeEval(Info);
18672 job.LHSResult.swap(RHS&: Result);
18673 job.Kind = Job::BinOpVisitedLHSKind;
18674 enqueue(E: Bop->getRHS());
18675 return;
18676 }
18677
18678 case Job::BinOpVisitedLHSKind: {
18679 const BinaryOperator *Bop = cast<BinaryOperator>(Val: job.E);
18680 EvalResult RHS;
18681 RHS.swap(RHS&: Result);
18682 Result.Failed = !VisitBinOp(LHSResult: job.LHSResult, RHSResult: RHS, E: Bop, Result&: Result.Val);
18683 Queue.pop_back();
18684 return;
18685 }
18686 }
18687
18688 llvm_unreachable("Invalid Job::Kind!");
18689}
18690
18691namespace {
18692enum class CmpResult {
18693 Unequal,
18694 Less,
18695 Equal,
18696 Greater,
18697 Unordered,
18698};
18699}
18700
18701template <class SuccessCB, class AfterCB>
18702static bool
18703EvaluateComparisonBinaryOperator(EvalInfo &Info, const BinaryOperator *E,
18704 SuccessCB &&Success, AfterCB &&DoAfter) {
18705 assert(!E->isValueDependent());
18706 assert(E->isComparisonOp() && "expected comparison operator");
18707 assert((E->getOpcode() == BO_Cmp ||
18708 E->getType()->isIntegralOrEnumerationType()) &&
18709 "unsupported binary expression evaluation");
18710 auto Error = [&](const Expr *E) {
18711 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
18712 return false;
18713 };
18714
18715 bool IsRelational = E->isRelationalOp() || E->getOpcode() == BO_Cmp;
18716 bool IsEquality = E->isEqualityOp();
18717
18718 QualType LHSTy = E->getLHS()->getType();
18719 QualType RHSTy = E->getRHS()->getType();
18720
18721 if (LHSTy->isIntegralOrEnumerationType() &&
18722 RHSTy->isIntegralOrEnumerationType()) {
18723 APSInt LHS, RHS;
18724 bool LHSOK = EvaluateInteger(E: E->getLHS(), Result&: LHS, Info);
18725 if (!LHSOK && !Info.noteFailure())
18726 return false;
18727 if (!EvaluateInteger(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
18728 return false;
18729 if (LHS < RHS)
18730 return Success(CmpResult::Less, E);
18731 if (LHS > RHS)
18732 return Success(CmpResult::Greater, E);
18733 return Success(CmpResult::Equal, E);
18734 }
18735
18736 if (LHSTy->isFixedPointType() || RHSTy->isFixedPointType()) {
18737 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHSTy));
18738 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHSTy));
18739
18740 bool LHSOK = EvaluateFixedPointOrInteger(E: E->getLHS(), Result&: LHSFX, Info);
18741 if (!LHSOK && !Info.noteFailure())
18742 return false;
18743 if (!EvaluateFixedPointOrInteger(E: E->getRHS(), Result&: RHSFX, Info) || !LHSOK)
18744 return false;
18745 if (LHSFX < RHSFX)
18746 return Success(CmpResult::Less, E);
18747 if (LHSFX > RHSFX)
18748 return Success(CmpResult::Greater, E);
18749 return Success(CmpResult::Equal, E);
18750 }
18751
18752 if (LHSTy->isAnyComplexType() || RHSTy->isAnyComplexType()) {
18753 ComplexValue LHS, RHS;
18754 bool LHSOK;
18755 if (E->isAssignmentOp()) {
18756 LValue LV;
18757 EvaluateLValue(E: E->getLHS(), Result&: LV, Info);
18758 LHSOK = false;
18759 } else if (LHSTy->isRealFloatingType()) {
18760 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: LHS.FloatReal, Info);
18761 if (LHSOK) {
18762 LHS.makeComplexFloat();
18763 LHS.FloatImag = APFloat(LHS.FloatReal.getSemantics());
18764 }
18765 } else {
18766 LHSOK = EvaluateComplex(E: E->getLHS(), Res&: LHS, Info);
18767 }
18768 if (!LHSOK && !Info.noteFailure())
18769 return false;
18770
18771 if (E->getRHS()->getType()->isRealFloatingType()) {
18772 if (!EvaluateFloat(E: E->getRHS(), Result&: RHS.FloatReal, Info) || !LHSOK)
18773 return false;
18774 RHS.makeComplexFloat();
18775 RHS.FloatImag = APFloat(RHS.FloatReal.getSemantics());
18776 } else if (!EvaluateComplex(E: E->getRHS(), Res&: RHS, Info) || !LHSOK)
18777 return false;
18778
18779 if (LHS.isComplexFloat()) {
18780 APFloat::cmpResult CR_r =
18781 LHS.getComplexFloatReal().compare(RHS: RHS.getComplexFloatReal());
18782 APFloat::cmpResult CR_i =
18783 LHS.getComplexFloatImag().compare(RHS: RHS.getComplexFloatImag());
18784 bool IsEqual = CR_r == APFloat::cmpEqual && CR_i == APFloat::cmpEqual;
18785 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
18786 } else {
18787 assert(IsEquality && "invalid complex comparison");
18788 bool IsEqual = LHS.getComplexIntReal() == RHS.getComplexIntReal() &&
18789 LHS.getComplexIntImag() == RHS.getComplexIntImag();
18790 return Success(IsEqual ? CmpResult::Equal : CmpResult::Unequal, E);
18791 }
18792 }
18793
18794 if (LHSTy->isRealFloatingType() &&
18795 RHSTy->isRealFloatingType()) {
18796 APFloat RHS(0.0), LHS(0.0);
18797
18798 bool LHSOK = EvaluateFloat(E: E->getRHS(), Result&: RHS, Info);
18799 if (!LHSOK && !Info.noteFailure())
18800 return false;
18801
18802 if (!EvaluateFloat(E: E->getLHS(), Result&: LHS, Info) || !LHSOK)
18803 return false;
18804
18805 assert(E->isComparisonOp() && "Invalid binary operator!");
18806 llvm::APFloatBase::cmpResult APFloatCmpResult = LHS.compare(RHS);
18807 if (!Info.InConstantContext &&
18808 APFloatCmpResult == APFloat::cmpUnordered &&
18809 E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts()).isFPConstrained()) {
18810 // Note: Compares may raise invalid in some cases involving NaN or sNaN.
18811 Info.FFDiag(E, DiagId: diag::note_constexpr_float_arithmetic_strict);
18812 return false;
18813 }
18814 auto GetCmpRes = [&]() {
18815 switch (APFloatCmpResult) {
18816 case APFloat::cmpEqual:
18817 return CmpResult::Equal;
18818 case APFloat::cmpLessThan:
18819 return CmpResult::Less;
18820 case APFloat::cmpGreaterThan:
18821 return CmpResult::Greater;
18822 case APFloat::cmpUnordered:
18823 return CmpResult::Unordered;
18824 }
18825 llvm_unreachable("Unrecognised APFloat::cmpResult enum");
18826 };
18827 return Success(GetCmpRes(), E);
18828 }
18829
18830 if (LHSTy->isPointerType() && RHSTy->isPointerType()) {
18831 LValue LHSValue, RHSValue;
18832
18833 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
18834 if (!LHSOK && !Info.noteFailure())
18835 return false;
18836
18837 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
18838 return false;
18839
18840 // Reject differing bases from the normal codepath; we special-case
18841 // comparisons to null.
18842 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
18843 // Bail out early if we're checking potential constant expression.
18844 // Otherwise, prefer to diagnose other issues.
18845 if (Info.checkingPotentialConstantExpression() &&
18846 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
18847 return false;
18848 auto DiagComparison = [&] (unsigned DiagID, bool Reversed = false) {
18849 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
18850 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
18851 Info.FFDiag(E, DiagId: DiagID)
18852 << (Reversed ? RHS : LHS) << (Reversed ? LHS : RHS);
18853 return false;
18854 };
18855 // Inequalities and subtractions between unrelated pointers have
18856 // unspecified or undefined behavior.
18857 if (!IsEquality)
18858 return DiagComparison(
18859 diag::note_constexpr_pointer_comparison_unspecified);
18860 // A constant address may compare equal to the address of a symbol.
18861 // The one exception is that address of an object cannot compare equal
18862 // to a null pointer constant.
18863 // TODO: Should we restrict this to actual null pointers, and exclude the
18864 // case of zero cast to pointer type?
18865 if ((!LHSValue.Base && !LHSValue.Offset.isZero()) ||
18866 (!RHSValue.Base && !RHSValue.Offset.isZero()))
18867 return DiagComparison(diag::note_constexpr_pointer_constant_comparison,
18868 !RHSValue.Base);
18869 // C++2c [intro.object]/10:
18870 // Two objects [...] may have the same address if [...] they are both
18871 // potentially non-unique objects.
18872 // C++2c [intro.object]/9:
18873 // An object is potentially non-unique if it is a string literal object,
18874 // the backing array of an initializer list, or a subobject thereof.
18875 //
18876 // This makes the comparison result unspecified, so it's not a constant
18877 // expression.
18878 //
18879 // TODO: Do we need to handle the initializer list case here?
18880 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
18881 return DiagComparison(diag::note_constexpr_literal_comparison);
18882 if (IsOpaqueConstantCall(LVal: LHSValue) || IsOpaqueConstantCall(LVal: RHSValue))
18883 return DiagComparison(diag::note_constexpr_opaque_call_comparison,
18884 !IsOpaqueConstantCall(LVal: LHSValue));
18885 // We can't tell whether weak symbols will end up pointing to the same
18886 // object.
18887 if (IsWeakLValue(Value: LHSValue) || IsWeakLValue(Value: RHSValue))
18888 return DiagComparison(diag::note_constexpr_pointer_weak_comparison,
18889 !IsWeakLValue(Value: LHSValue));
18890 // We can't compare the address of the start of one object with the
18891 // past-the-end address of another object, per C++ DR1652.
18892 if (LHSValue.Base && LHSValue.Offset.isZero() &&
18893 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: RHSValue))
18894 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
18895 true);
18896 if (RHSValue.Base && RHSValue.Offset.isZero() &&
18897 isOnePastTheEndOfCompleteObject(Ctx: Info.Ctx, LV: LHSValue))
18898 return DiagComparison(diag::note_constexpr_pointer_comparison_past_end,
18899 false);
18900 // We can't tell whether an object is at the same address as another
18901 // zero sized object.
18902 if ((RHSValue.Base && isZeroSized(Value: LHSValue)) ||
18903 (LHSValue.Base && isZeroSized(Value: RHSValue)))
18904 return DiagComparison(
18905 diag::note_constexpr_pointer_comparison_zero_sized);
18906 if (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown)
18907 return DiagComparison(
18908 diag::note_constexpr_pointer_comparison_unspecified);
18909 // FIXME: Verify both variables are live.
18910 return Success(CmpResult::Unequal, E);
18911 }
18912
18913 const CharUnits &LHSOffset = LHSValue.getLValueOffset();
18914 const CharUnits &RHSOffset = RHSValue.getLValueOffset();
18915
18916 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
18917 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
18918
18919 // C++11 [expr.rel]p2:
18920 // - If two pointers point to non-static data members of the same object,
18921 // or to subobjects or array elements fo such members, recursively, the
18922 // pointer to the later declared member compares greater provided the
18923 // two members have the same access control and provided their class is
18924 // not a union.
18925 // [...]
18926 // - Otherwise pointer comparisons are unspecified.
18927 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid && IsRelational) {
18928 bool WasArrayIndex;
18929 unsigned Mismatch = FindDesignatorMismatch(
18930 ObjType: LHSValue.Base.isNull() ? QualType()
18931 : getType(B: LHSValue.Base).getNonReferenceType(),
18932 A: LHSDesignator, B: RHSDesignator, WasArrayIndex);
18933 // At the point where the designators diverge, the comparison has a
18934 // specified value if:
18935 // - we are comparing array indices
18936 // - we are comparing fields of a union, or fields with the same access
18937 // Otherwise, the result is unspecified and thus the comparison is not a
18938 // constant expression.
18939 if (!WasArrayIndex && Mismatch < LHSDesignator.Entries.size() &&
18940 Mismatch < RHSDesignator.Entries.size()) {
18941 const FieldDecl *LF = getAsField(E: LHSDesignator.Entries[Mismatch]);
18942 const FieldDecl *RF = getAsField(E: RHSDesignator.Entries[Mismatch]);
18943 if (!LF && !RF)
18944 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_classes);
18945 else if (!LF)
18946 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
18947 << getAsBaseClass(E: LHSDesignator.Entries[Mismatch])
18948 << RF->getParent() << RF;
18949 else if (!RF)
18950 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_comparison_base_field)
18951 << getAsBaseClass(E: RHSDesignator.Entries[Mismatch])
18952 << LF->getParent() << LF;
18953 else if (!LF->getParent()->isUnion() &&
18954 LF->getAccess() != RF->getAccess())
18955 Info.CCEDiag(E,
18956 DiagId: diag::note_constexpr_pointer_comparison_differing_access)
18957 << LF << LF->getAccess() << RF << RF->getAccess()
18958 << LF->getParent();
18959 }
18960 }
18961
18962 // The comparison here must be unsigned, and performed with the same
18963 // width as the pointer.
18964 unsigned PtrSize = Info.Ctx.getTypeSize(T: LHSTy);
18965 uint64_t CompareLHS = LHSOffset.getQuantity();
18966 uint64_t CompareRHS = RHSOffset.getQuantity();
18967 assert(PtrSize <= 64 && "Unexpected pointer width");
18968 uint64_t Mask = ~0ULL >> (64 - PtrSize);
18969 CompareLHS &= Mask;
18970 CompareRHS &= Mask;
18971
18972 // If there is a base and this is a relational operator, we can only
18973 // compare pointers within the object in question; otherwise, the result
18974 // depends on where the object is located in memory.
18975 if (!LHSValue.Base.isNull() && IsRelational) {
18976 QualType BaseTy = getType(B: LHSValue.Base).getNonReferenceType();
18977 if (BaseTy->isIncompleteType())
18978 return Error(E);
18979 CharUnits Size = Info.Ctx.getTypeSizeInChars(T: BaseTy);
18980 uint64_t OffsetLimit = Size.getQuantity();
18981 if (CompareLHS > OffsetLimit || CompareRHS > OffsetLimit)
18982 return Error(E);
18983 }
18984
18985 if (CompareLHS < CompareRHS)
18986 return Success(CmpResult::Less, E);
18987 if (CompareLHS > CompareRHS)
18988 return Success(CmpResult::Greater, E);
18989 return Success(CmpResult::Equal, E);
18990 }
18991
18992 if (LHSTy->isMemberPointerType()) {
18993 assert(IsEquality && "unexpected member pointer operation");
18994 assert(RHSTy->isMemberPointerType() && "invalid comparison");
18995
18996 MemberPtr LHSValue, RHSValue;
18997
18998 bool LHSOK = EvaluateMemberPointer(E: E->getLHS(), Result&: LHSValue, Info);
18999 if (!LHSOK && !Info.noteFailure())
19000 return false;
19001
19002 if (!EvaluateMemberPointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19003 return false;
19004
19005 // If either operand is a pointer to a weak function, the comparison is not
19006 // constant.
19007 if (LHSValue.getDecl() && LHSValue.getDecl()->isWeak()) {
19008 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19009 << LHSValue.getDecl();
19010 return false;
19011 }
19012 if (RHSValue.getDecl() && RHSValue.getDecl()->isWeak()) {
19013 Info.FFDiag(E, DiagId: diag::note_constexpr_mem_pointer_weak_comparison)
19014 << RHSValue.getDecl();
19015 return false;
19016 }
19017
19018 // C++11 [expr.eq]p2:
19019 // If both operands are null, they compare equal. Otherwise if only one is
19020 // null, they compare unequal.
19021 if (!LHSValue.getDecl() || !RHSValue.getDecl()) {
19022 bool Equal = !LHSValue.getDecl() && !RHSValue.getDecl();
19023 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19024 }
19025
19026 // Otherwise if either is a pointer to a virtual member function, the
19027 // result is unspecified.
19028 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: LHSValue.getDecl()))
19029 if (MD->isVirtual())
19030 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19031 if (const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: RHSValue.getDecl()))
19032 if (MD->isVirtual())
19033 Info.CCEDiag(E, DiagId: diag::note_constexpr_compare_virtual_mem_ptr) << MD;
19034
19035 // Otherwise they compare equal if and only if they would refer to the
19036 // same member of the same most derived object or the same subobject if
19037 // they were dereferenced with a hypothetical object of the associated
19038 // class type.
19039 bool Equal = LHSValue == RHSValue;
19040 return Success(Equal ? CmpResult::Equal : CmpResult::Unequal, E);
19041 }
19042
19043 if (LHSTy->isNullPtrType()) {
19044 assert(E->isComparisonOp() && "unexpected nullptr operation");
19045 assert(RHSTy->isNullPtrType() && "missing pointer conversion");
19046 // C++11 [expr.rel]p4, [expr.eq]p3: If two operands of type std::nullptr_t
19047 // are compared, the result is true of the operator is <=, >= or ==, and
19048 // false otherwise.
19049 LValue Res;
19050 if (!EvaluatePointer(E: E->getLHS(), Result&: Res, Info) ||
19051 !EvaluatePointer(E: E->getRHS(), Result&: Res, Info))
19052 return false;
19053 return Success(CmpResult::Equal, E);
19054 }
19055
19056 return DoAfter();
19057}
19058
19059bool RecordExprEvaluator::VisitBinCmp(const BinaryOperator *E) {
19060 if (!CheckLiteralType(Info, E))
19061 return false;
19062
19063 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19064 ComparisonCategoryResult CCR;
19065 switch (CR) {
19066 case CmpResult::Unequal:
19067 llvm_unreachable("should never produce Unequal for three-way comparison");
19068 case CmpResult::Less:
19069 CCR = ComparisonCategoryResult::Less;
19070 break;
19071 case CmpResult::Equal:
19072 CCR = ComparisonCategoryResult::Equal;
19073 break;
19074 case CmpResult::Greater:
19075 CCR = ComparisonCategoryResult::Greater;
19076 break;
19077 case CmpResult::Unordered:
19078 CCR = ComparisonCategoryResult::Unordered;
19079 break;
19080 }
19081 // Evaluation succeeded. Lookup the information for the comparison category
19082 // type and fetch the VarDecl for the result.
19083 const ComparisonCategoryInfo &CmpInfo =
19084 Info.Ctx.CompCategories.getInfoForType(Ty: E->getType());
19085 const VarDecl *VD = CmpInfo.getValueInfo(ValueKind: CmpInfo.makeWeakResult(Res: CCR))->VD;
19086 // Check and evaluate the result as a constant expression.
19087 LValue LV;
19088 LV.set(B: VD);
19089 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
19090 return false;
19091 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
19092 Kind: ConstantExprKind::Normal);
19093 };
19094 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19095 return ExprEvaluatorBaseTy::VisitBinCmp(S: E);
19096 });
19097}
19098
19099bool RecordExprEvaluator::VisitCXXParenListInitExpr(
19100 const CXXParenListInitExpr *E) {
19101 return VisitCXXParenListOrInitListExpr(ExprToVisit: E, Args: E->getInitExprs());
19102}
19103
19104bool IntExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
19105 // We don't support assignment in C. C++ assignments don't get here because
19106 // assignment is an lvalue in C++.
19107 if (E->isAssignmentOp()) {
19108 Error(E);
19109 if (!Info.noteFailure())
19110 return false;
19111 }
19112
19113 if (DataRecursiveIntBinOpEvaluator::shouldEnqueue(E))
19114 return DataRecursiveIntBinOpEvaluator(*this, Result).Traverse(E);
19115
19116 assert((!E->getLHS()->getType()->isIntegralOrEnumerationType() ||
19117 !E->getRHS()->getType()->isIntegralOrEnumerationType()) &&
19118 "DataRecursiveIntBinOpEvaluator should have handled integral types");
19119
19120 if (E->isComparisonOp()) {
19121 // Evaluate builtin binary comparisons by evaluating them as three-way
19122 // comparisons and then translating the result.
19123 auto OnSuccess = [&](CmpResult CR, const BinaryOperator *E) {
19124 assert((CR != CmpResult::Unequal || E->isEqualityOp()) &&
19125 "should only produce Unequal for equality comparisons");
19126 bool IsEqual = CR == CmpResult::Equal,
19127 IsLess = CR == CmpResult::Less,
19128 IsGreater = CR == CmpResult::Greater;
19129 auto Op = E->getOpcode();
19130 switch (Op) {
19131 default:
19132 llvm_unreachable("unsupported binary operator");
19133 case BO_EQ:
19134 case BO_NE:
19135 return Success(Value: IsEqual == (Op == BO_EQ), E);
19136 case BO_LT:
19137 return Success(Value: IsLess, E);
19138 case BO_GT:
19139 return Success(Value: IsGreater, E);
19140 case BO_LE:
19141 return Success(Value: IsEqual || IsLess, E);
19142 case BO_GE:
19143 return Success(Value: IsEqual || IsGreater, E);
19144 }
19145 };
19146 return EvaluateComparisonBinaryOperator(Info, E, Success&: OnSuccess, DoAfter: [&]() {
19147 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19148 });
19149 }
19150
19151 QualType LHSTy = E->getLHS()->getType();
19152 QualType RHSTy = E->getRHS()->getType();
19153
19154 if (LHSTy->isPointerType() && RHSTy->isPointerType() &&
19155 E->getOpcode() == BO_Sub) {
19156 LValue LHSValue, RHSValue;
19157
19158 bool LHSOK = EvaluatePointer(E: E->getLHS(), Result&: LHSValue, Info);
19159 if (!LHSOK && !Info.noteFailure())
19160 return false;
19161
19162 if (!EvaluatePointer(E: E->getRHS(), Result&: RHSValue, Info) || !LHSOK)
19163 return false;
19164
19165 // Reject differing bases from the normal codepath; we special-case
19166 // comparisons to null.
19167 if (!HasSameBase(A: LHSValue, B: RHSValue)) {
19168 if (Info.checkingPotentialConstantExpression() &&
19169 (LHSValue.AllowConstexprUnknown || RHSValue.AllowConstexprUnknown))
19170 return false;
19171
19172 const Expr *LHSExpr = LHSValue.Base.dyn_cast<const Expr *>();
19173 const Expr *RHSExpr = RHSValue.Base.dyn_cast<const Expr *>();
19174
19175 auto DiagArith = [&](unsigned DiagID) {
19176 std::string LHS = LHSValue.toString(Ctx&: Info.Ctx, T: E->getLHS()->getType());
19177 std::string RHS = RHSValue.toString(Ctx&: Info.Ctx, T: E->getRHS()->getType());
19178 Info.FFDiag(E, DiagId: DiagID) << LHS << RHS;
19179 if (LHSExpr && LHSExpr == RHSExpr)
19180 Info.Note(Loc: LHSExpr->getExprLoc(),
19181 DiagId: diag::note_constexpr_repeated_literal_eval)
19182 << LHSExpr->getSourceRange();
19183 return false;
19184 };
19185
19186 if (!LHSExpr || !RHSExpr)
19187 return DiagArith(diag::note_constexpr_pointer_arith_unspecified);
19188
19189 if (ArePotentiallyOverlappingStringLiterals(Info, LHS: LHSValue, RHS: RHSValue))
19190 return DiagArith(diag::note_constexpr_literal_arith);
19191
19192 const AddrLabelExpr *LHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: LHSExpr);
19193 const AddrLabelExpr *RHSAddrExpr = dyn_cast<AddrLabelExpr>(Val: RHSExpr);
19194 if (!LHSAddrExpr || !RHSAddrExpr)
19195 return Error(E);
19196 // Make sure both labels come from the same function.
19197 if (LHSAddrExpr->getLabel()->getDeclContext() !=
19198 RHSAddrExpr->getLabel()->getDeclContext())
19199 return Error(E);
19200 return Success(V: APValue(LHSAddrExpr, RHSAddrExpr), E);
19201 }
19202 const CharUnits &LHSOffset = LHSValue.getLValueOffset();
19203 const CharUnits &RHSOffset = RHSValue.getLValueOffset();
19204
19205 SubobjectDesignator &LHSDesignator = LHSValue.getLValueDesignator();
19206 SubobjectDesignator &RHSDesignator = RHSValue.getLValueDesignator();
19207
19208 // C++11 [expr.add]p6:
19209 // Unless both pointers point to elements of the same array object, or
19210 // one past the last element of the array object, the behavior is
19211 // undefined.
19212 if (!LHSDesignator.Invalid && !RHSDesignator.Invalid &&
19213 !AreElementsOfSameArray(ObjType: getType(B: LHSValue.Base), A: LHSDesignator,
19214 B: RHSDesignator))
19215 Info.CCEDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_not_same_array);
19216
19217 QualType Type = E->getLHS()->getType();
19218 QualType ElementType = Type->castAs<PointerType>()->getPointeeType();
19219
19220 CharUnits ElementSize;
19221 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: ElementType, Size&: ElementSize))
19222 return false;
19223
19224 // As an extension, a type may have zero size (empty struct or union in
19225 // C, array of zero length). Pointer subtraction in such cases has
19226 // undefined behavior, so is not constant.
19227 if (ElementSize.isZero()) {
19228 Info.FFDiag(E, DiagId: diag::note_constexpr_pointer_subtraction_zero_size)
19229 << ElementType;
19230 return false;
19231 }
19232
19233 // FIXME: LLVM and GCC both compute LHSOffset - RHSOffset at runtime,
19234 // and produce incorrect results when it overflows. Such behavior
19235 // appears to be non-conforming, but is common, so perhaps we should
19236 // assume the standard intended for such cases to be undefined behavior
19237 // and check for them.
19238
19239 // Compute (LHSOffset - RHSOffset) / Size carefully, checking for
19240 // overflow in the final conversion to ptrdiff_t.
19241 APSInt LHS(llvm::APInt(65, (int64_t)LHSOffset.getQuantity(), true), false);
19242 APSInt RHS(llvm::APInt(65, (int64_t)RHSOffset.getQuantity(), true), false);
19243 APSInt ElemSize(llvm::APInt(65, (int64_t)ElementSize.getQuantity(), true),
19244 false);
19245 APSInt TrueResult = (LHS - RHS) / ElemSize;
19246 APSInt Result = TrueResult.trunc(width: Info.Ctx.getIntWidth(T: E->getType()));
19247
19248 if (Result.extend(width: 65) != TrueResult &&
19249 !HandleOverflow(Info, E, SrcValue: TrueResult, DestType: E->getType()))
19250 return false;
19251 return Success(SI: Result, E);
19252 }
19253
19254 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19255}
19256
19257/// VisitUnaryExprOrTypeTraitExpr - Evaluate a sizeof, alignof or vec_step with
19258/// a result as the expression's type.
19259bool IntExprEvaluator::VisitUnaryExprOrTypeTraitExpr(
19260 const UnaryExprOrTypeTraitExpr *E) {
19261 switch(E->getKind()) {
19262 case UETT_PreferredAlignOf:
19263 case UETT_AlignOf: {
19264 if (E->isArgumentType())
19265 return Success(
19266 Size: GetAlignOfType(Ctx: Info.Ctx, T: E->getArgumentType(), ExprKind: E->getKind()), E);
19267 else
19268 return Success(
19269 Size: GetAlignOfExpr(Ctx: Info.Ctx, E: E->getArgumentExpr(), ExprKind: E->getKind()), E);
19270 }
19271
19272 case UETT_PtrAuthTypeDiscriminator: {
19273 if (E->getArgumentType()->isDependentType())
19274 return false;
19275 return Success(
19276 Value: Info.Ctx.getPointerAuthTypeDiscriminator(T: E->getArgumentType()), E);
19277 }
19278 case UETT_VecStep: {
19279 QualType Ty = E->getTypeOfArgument();
19280
19281 if (Ty->isVectorType()) {
19282 unsigned n = Ty->castAs<VectorType>()->getNumElements();
19283
19284 // The vec_step built-in functions that take a 3-component
19285 // vector return 4. (OpenCL 1.1 spec 6.11.12)
19286 if (n == 3)
19287 n = 4;
19288
19289 return Success(Value: n, E);
19290 } else
19291 return Success(Value: 1, E);
19292 }
19293
19294 case UETT_DataSizeOf:
19295 case UETT_SizeOf: {
19296 QualType SrcTy = E->getTypeOfArgument();
19297 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
19298 // the result is the size of the referenced type."
19299 if (const ReferenceType *Ref = SrcTy->getAs<ReferenceType>())
19300 SrcTy = Ref->getPointeeType();
19301
19302 CharUnits Sizeof;
19303 if (!HandleSizeof(Info, Loc: E->getExprLoc(), Type: SrcTy, Size&: Sizeof,
19304 SOT: E->getKind() == UETT_DataSizeOf ? SizeOfType::DataSizeOf
19305 : SizeOfType::SizeOf)) {
19306 return false;
19307 }
19308 return Success(Size: Sizeof, E);
19309 }
19310 case UETT_OpenMPRequiredSimdAlign:
19311 assert(E->isArgumentType());
19312 return Success(
19313 Value: Info.Ctx.toCharUnitsFromBits(
19314 BitSize: Info.Ctx.getOpenMPDefaultSimdAlign(T: E->getArgumentType()))
19315 .getQuantity(),
19316 E);
19317 case UETT_VectorElements: {
19318 QualType Ty = E->getTypeOfArgument();
19319 // If the vector has a fixed size, we can determine the number of elements
19320 // at compile time.
19321 if (const auto *VT = Ty->getAs<VectorType>())
19322 return Success(Value: VT->getNumElements(), E);
19323
19324 assert(Ty->isSizelessVectorType());
19325 if (Info.InConstantContext)
19326 Info.CCEDiag(E, DiagId: diag::note_constexpr_non_const_vectorelements)
19327 << E->getSourceRange();
19328
19329 return false;
19330 }
19331 case UETT_CountOf: {
19332 QualType Ty = E->getTypeOfArgument();
19333 assert(Ty->isArrayType());
19334
19335 // We don't need to worry about array element qualifiers, so getting the
19336 // unsafe array type is fine.
19337 if (const auto *CAT =
19338 dyn_cast<ConstantArrayType>(Val: Ty->getAsArrayTypeUnsafe())) {
19339 return Success(I: CAT->getSize(), E);
19340 }
19341
19342 assert(!Ty->isConstantSizeType());
19343
19344 // If it's a variable-length array type, we need to check whether it is a
19345 // multidimensional array. If so, we need to check the size expression of
19346 // the VLA to see if it's a constant size. If so, we can return that value.
19347 const auto *VAT = Info.Ctx.getAsVariableArrayType(T: Ty);
19348 assert(VAT);
19349 if (VAT->getElementType()->isArrayType()) {
19350 // Variable array size expression could be missing (e.g. int a[*][10]) In
19351 // that case, it can't be a constant expression.
19352 if (!VAT->getSizeExpr()) {
19353 Info.FFDiag(Loc: E->getBeginLoc());
19354 return false;
19355 }
19356
19357 std::optional<APSInt> Res =
19358 VAT->getSizeExpr()->getIntegerConstantExpr(Ctx: Info.Ctx);
19359 if (Res) {
19360 // The resulting value always has type size_t, so we need to make the
19361 // returned APInt have the correct sign and bit-width.
19362 APInt Val{
19363 static_cast<unsigned>(Info.Ctx.getTypeSize(T: Info.Ctx.getSizeType())),
19364 Res->getZExtValue()};
19365 return Success(I: Val, E);
19366 }
19367 }
19368
19369 // Definitely a variable-length type, which is not an ICE.
19370 // FIXME: Better diagnostic.
19371 Info.FFDiag(Loc: E->getBeginLoc());
19372 return false;
19373 }
19374 }
19375
19376 llvm_unreachable("unknown expr/type trait");
19377}
19378
19379bool IntExprEvaluator::VisitOffsetOfExpr(const OffsetOfExpr *OOE) {
19380 Info.Ctx.recordOffsetOfEvaluation(E: OOE);
19381 CharUnits Result;
19382 unsigned n = OOE->getNumComponents();
19383 if (n == 0)
19384 return Error(E: OOE);
19385 QualType CurrentType = OOE->getTypeSourceInfo()->getType();
19386 for (unsigned i = 0; i != n; ++i) {
19387 OffsetOfNode ON = OOE->getComponent(Idx: i);
19388 switch (ON.getKind()) {
19389 case OffsetOfNode::Array: {
19390 const Expr *Idx = OOE->getIndexExpr(Idx: ON.getArrayExprIndex());
19391 APSInt IdxResult;
19392 if (!EvaluateInteger(E: Idx, Result&: IdxResult, Info))
19393 return false;
19394 const ArrayType *AT = Info.Ctx.getAsArrayType(T: CurrentType);
19395 if (!AT)
19396 return Error(E: OOE);
19397 CurrentType = AT->getElementType();
19398 CharUnits ElementSize = Info.Ctx.getTypeSizeInChars(T: CurrentType);
19399 Result += IdxResult.getSExtValue() * ElementSize;
19400 break;
19401 }
19402
19403 case OffsetOfNode::Field: {
19404 FieldDecl *MemberDecl = ON.getField();
19405 const auto *RD = CurrentType->getAsRecordDecl();
19406 if (!RD)
19407 return Error(E: OOE);
19408 if (RD->isInvalidDecl()) return false;
19409 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
19410 unsigned i = MemberDecl->getFieldIndex();
19411 assert(i < RL.getFieldCount() && "offsetof field in wrong type");
19412 Result += Info.Ctx.toCharUnitsFromBits(BitSize: RL.getFieldOffset(FieldNo: i));
19413 CurrentType = MemberDecl->getType().getNonReferenceType();
19414 break;
19415 }
19416
19417 case OffsetOfNode::Identifier:
19418 llvm_unreachable("dependent __builtin_offsetof");
19419
19420 case OffsetOfNode::Base: {
19421 CXXBaseSpecifier *BaseSpec = ON.getBase();
19422 if (BaseSpec->isVirtual())
19423 return Error(E: OOE);
19424
19425 // Find the layout of the class whose base we are looking into.
19426 const auto *RD = CurrentType->getAsCXXRecordDecl();
19427 if (!RD)
19428 return Error(E: OOE);
19429 if (RD->isInvalidDecl()) return false;
19430 const ASTRecordLayout &RL = Info.Ctx.getASTRecordLayout(D: RD);
19431
19432 // Find the base class itself.
19433 CurrentType = BaseSpec->getType();
19434 const auto *BaseRD = CurrentType->getAsCXXRecordDecl();
19435 if (!BaseRD)
19436 return Error(E: OOE);
19437
19438 // Add the offset to the base.
19439 Result += RL.getBaseClassOffset(Base: BaseRD);
19440 break;
19441 }
19442 }
19443 }
19444 return Success(Size: Result, E: OOE);
19445}
19446
19447bool IntExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
19448 switch (E->getOpcode()) {
19449 default:
19450 // Address, indirect, pre/post inc/dec, etc are not valid constant exprs.
19451 // See C99 6.6p3.
19452 return Error(E);
19453 case UO_Extension:
19454 // FIXME: Should extension allow i-c-e extension expressions in its scope?
19455 // If so, we could clear the diagnostic ID.
19456 return Visit(S: E->getSubExpr());
19457 case UO_Plus:
19458 // The result is just the value.
19459 return Visit(S: E->getSubExpr());
19460 case UO_Minus: {
19461 if (!Visit(S: E->getSubExpr()))
19462 return false;
19463 if (!Result.isInt()) return Error(E);
19464 const APSInt &Value = Result.getInt();
19465 if (Value.isSigned() && Value.isMinSignedValue() && E->canOverflow() &&
19466 !E->getType().isWrapType()) {
19467 if (Info.checkingForUndefinedBehavior())
19468 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
19469 DiagID: diag::warn_integer_constant_overflow)
19470 << toString(I: Value, Radix: 10, Signed: Value.isSigned(), /*formatAsCLiteral=*/false,
19471 /*UpperCase=*/true, /*InsertSeparators=*/true)
19472 << E->getType() << E->getSourceRange();
19473
19474 if (!HandleOverflow(Info, E, SrcValue: -Value.extend(width: Value.getBitWidth() + 1),
19475 DestType: E->getType()))
19476 return false;
19477 }
19478 return Success(SI: -Value, E);
19479 }
19480 case UO_Not: {
19481 if (!Visit(S: E->getSubExpr()))
19482 return false;
19483 if (!Result.isInt()) return Error(E);
19484 return Success(SI: ~Result.getInt(), E);
19485 }
19486 case UO_LNot: {
19487 bool bres;
19488 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
19489 return false;
19490 return Success(Value: !bres, E);
19491 }
19492 }
19493}
19494
19495/// HandleCast - This is used to evaluate implicit or explicit casts where the
19496/// result type is integer.
19497bool IntExprEvaluator::VisitCastExpr(const CastExpr *E) {
19498 const Expr *SubExpr = E->getSubExpr();
19499 QualType DestType = E->getType();
19500 QualType SrcType = SubExpr->getType();
19501
19502 switch (E->getCastKind()) {
19503 case CK_BaseToDerived:
19504 case CK_DerivedToBase:
19505 case CK_UncheckedDerivedToBase:
19506 case CK_Dynamic:
19507 case CK_ToUnion:
19508 case CK_ArrayToPointerDecay:
19509 case CK_FunctionToPointerDecay:
19510 case CK_NullToPointer:
19511 case CK_NullToMemberPointer:
19512 case CK_BaseToDerivedMemberPointer:
19513 case CK_DerivedToBaseMemberPointer:
19514 case CK_ReinterpretMemberPointer:
19515 case CK_ConstructorConversion:
19516 case CK_IntegralToPointer:
19517 case CK_ToVoid:
19518 case CK_VectorSplat:
19519 case CK_IntegralToFloating:
19520 case CK_FloatingCast:
19521 case CK_CPointerToObjCPointerCast:
19522 case CK_BlockPointerToObjCPointerCast:
19523 case CK_AnyPointerToBlockPointerCast:
19524 case CK_ObjCObjectLValueCast:
19525 case CK_FloatingRealToComplex:
19526 case CK_FloatingComplexToReal:
19527 case CK_FloatingComplexCast:
19528 case CK_FloatingComplexToIntegralComplex:
19529 case CK_IntegralRealToComplex:
19530 case CK_IntegralComplexCast:
19531 case CK_IntegralComplexToFloatingComplex:
19532 case CK_BuiltinFnToFnPtr:
19533 case CK_ZeroToOCLOpaqueType:
19534 case CK_NonAtomicToAtomic:
19535 case CK_AddressSpaceConversion:
19536 case CK_IntToOCLSampler:
19537 case CK_FloatingToFixedPoint:
19538 case CK_FixedPointToFloating:
19539 case CK_FixedPointCast:
19540 case CK_IntegralToFixedPoint:
19541 case CK_MatrixCast:
19542 case CK_HLSLAggregateSplatCast:
19543 llvm_unreachable("invalid cast kind for integral value");
19544
19545 case CK_BitCast:
19546 case CK_Dependent:
19547 case CK_LValueBitCast:
19548 case CK_ARCProduceObject:
19549 case CK_ARCConsumeObject:
19550 case CK_ARCReclaimReturnedObject:
19551 case CK_ARCExtendBlockObject:
19552 case CK_CopyAndAutoreleaseBlockObject:
19553 return Error(E);
19554
19555 case CK_UserDefinedConversion:
19556 case CK_LValueToRValue:
19557 case CK_AtomicToNonAtomic:
19558 case CK_NoOp:
19559 case CK_LValueToRValueBitCast:
19560 case CK_HLSLArrayRValue:
19561 return ExprEvaluatorBaseTy::VisitCastExpr(E);
19562
19563 case CK_MemberPointerToBoolean:
19564 case CK_PointerToBoolean:
19565 case CK_IntegralToBoolean:
19566 case CK_FloatingToBoolean:
19567 case CK_BooleanToSignedIntegral:
19568 case CK_FloatingComplexToBoolean:
19569 case CK_IntegralComplexToBoolean: {
19570 bool BoolResult;
19571 if (!EvaluateAsBooleanCondition(E: SubExpr, Result&: BoolResult, Info))
19572 return false;
19573 uint64_t IntResult = BoolResult;
19574 if (BoolResult && E->getCastKind() == CK_BooleanToSignedIntegral)
19575 IntResult = (uint64_t)-1;
19576 return Success(Value: IntResult, E);
19577 }
19578
19579 case CK_FixedPointToIntegral: {
19580 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SrcType));
19581 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
19582 return false;
19583 bool Overflowed;
19584 llvm::APSInt Result = Src.convertToInt(
19585 DstWidth: Info.Ctx.getIntWidth(T: DestType),
19586 DstSign: DestType->isSignedIntegerOrEnumerationType(), Overflow: &Overflowed);
19587 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Result, DestType))
19588 return false;
19589 return Success(SI: Result, E);
19590 }
19591
19592 case CK_FixedPointToBoolean: {
19593 // Unsigned padding does not affect this.
19594 APValue Val;
19595 if (!Evaluate(Result&: Val, Info, E: SubExpr))
19596 return false;
19597 return Success(Value: Val.getFixedPoint().getBoolValue(), E);
19598 }
19599
19600 case CK_IntegralCast: {
19601 if (!Visit(S: SubExpr))
19602 return false;
19603
19604 if (!Result.isInt()) {
19605 // Allow casts of address-of-label differences if they are no-ops
19606 // or narrowing, if the result is at least 32 bits wide.
19607 // (The narrowing case isn't actually guaranteed to
19608 // be constant-evaluatable except in some narrow cases which are hard
19609 // to detect here. We let it through on the assumption the user knows
19610 // what they are doing.)
19611 if (Result.isAddrLabelDiff()) {
19612 unsigned DestBits = Info.Ctx.getTypeSize(T: DestType);
19613 return DestBits >= 32 && DestBits <= Info.Ctx.getTypeSize(T: SrcType);
19614 }
19615 // Only allow casts of lvalues if they are lossless.
19616 return Info.Ctx.getTypeSize(T: DestType) == Info.Ctx.getTypeSize(T: SrcType);
19617 }
19618
19619 if (Info.Ctx.getLangOpts().CPlusPlus && DestType->isEnumeralType()) {
19620 const auto *ED = DestType->getAsEnumDecl();
19621 // Check that the value is within the range of the enumeration values.
19622 //
19623 // This corressponds to [expr.static.cast]p10 which says:
19624 // A value of integral or enumeration type can be explicitly converted
19625 // to a complete enumeration type ... If the enumeration type does not
19626 // have a fixed underlying type, the value is unchanged if the original
19627 // value is within the range of the enumeration values ([dcl.enum]), and
19628 // otherwise, the behavior is undefined.
19629 //
19630 // This was resolved as part of DR2338 which has CD5 status.
19631 if (!ED->isFixed()) {
19632 llvm::APInt Min;
19633 llvm::APInt Max;
19634
19635 ED->getValueRange(Max, Min);
19636 --Max;
19637
19638 if (ED->getNumNegativeBits() &&
19639 (Max.slt(RHS: Result.getInt().getSExtValue()) ||
19640 Min.sgt(RHS: Result.getInt().getSExtValue())))
19641 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
19642 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getSExtValue()
19643 << Max.getSExtValue() << ED;
19644 else if (!ED->getNumNegativeBits() &&
19645 Max.ult(RHS: Result.getInt().getZExtValue()))
19646 Info.CCEDiag(E, DiagId: diag::note_constexpr_unscoped_enum_out_of_range)
19647 << llvm::toString(I: Result.getInt(), Radix: 10) << Min.getZExtValue()
19648 << Max.getZExtValue() << ED;
19649 }
19650 }
19651
19652 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType,
19653 Value: Result.getInt()), E);
19654 }
19655
19656 case CK_PointerToIntegral: {
19657 CCEDiag(E, D: diag::note_constexpr_invalid_cast)
19658 << diag::ConstexprInvalidCastKind::ThisConversionOrReinterpret
19659 << Info.Ctx.getLangOpts().CPlusPlus << E->getSourceRange();
19660
19661 LValue LV;
19662 if (!EvaluatePointer(E: SubExpr, Result&: LV, Info))
19663 return false;
19664
19665 if (LV.getLValueBase()) {
19666 // Only allow based lvalue casts if they are lossless.
19667 // FIXME: Allow a larger integer size than the pointer size, and allow
19668 // narrowing back down to pointer width in subsequent integral casts.
19669 // FIXME: Check integer type's active bits, not its type size.
19670 if (Info.Ctx.getTypeSize(T: DestType) != Info.Ctx.getTypeSize(T: SrcType))
19671 return Error(E);
19672
19673 LV.Designator.setInvalid();
19674 LV.moveInto(V&: Result);
19675 return true;
19676 }
19677
19678 APSInt AsInt;
19679 APValue V;
19680 LV.moveInto(V);
19681 if (!V.toIntegralConstant(Result&: AsInt, SrcTy: SrcType, Ctx: Info.Ctx))
19682 llvm_unreachable("Can't cast this!");
19683
19684 return Success(SI: HandleIntToIntCast(Info, E, DestType, SrcType, Value: AsInt), E);
19685 }
19686
19687 case CK_IntegralComplexToReal: {
19688 ComplexValue C;
19689 if (!EvaluateComplex(E: SubExpr, Res&: C, Info))
19690 return false;
19691 return Success(SI: C.getComplexIntReal(), E);
19692 }
19693
19694 case CK_FloatingToIntegral: {
19695 APFloat F(0.0);
19696 if (!EvaluateFloat(E: SubExpr, Result&: F, Info))
19697 return false;
19698
19699 APSInt Value;
19700 if (!HandleFloatToIntCast(Info, E, SrcType, Value: F, DestType, Result&: Value))
19701 return false;
19702 return Success(SI: Value, E);
19703 }
19704 case CK_HLSLVectorTruncation: {
19705 APValue Val;
19706 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
19707 return Error(E);
19708 return Success(V: Val.getVectorElt(I: 0), E);
19709 }
19710 case CK_HLSLMatrixTruncation: {
19711 APValue Val;
19712 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
19713 return Error(E);
19714 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), E);
19715 }
19716 case CK_HLSLElementwiseCast: {
19717 SmallVector<APValue> SrcVals;
19718 SmallVector<QualType> SrcTypes;
19719
19720 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: DestType, SrcVals, SrcTypes))
19721 return false;
19722
19723 // cast our single element
19724 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
19725 APValue ResultVal;
19726 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: DestType, Original: SrcVals[0],
19727 Result&: ResultVal))
19728 return false;
19729 return Success(V: ResultVal, E);
19730 }
19731 }
19732
19733 llvm_unreachable("unknown cast resulting in integral value");
19734}
19735
19736bool IntExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
19737 if (E->getSubExpr()->getType()->isAnyComplexType()) {
19738 ComplexValue LV;
19739 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
19740 return false;
19741 if (!LV.isComplexInt())
19742 return Error(E);
19743 return Success(SI: LV.getComplexIntReal(), E);
19744 }
19745
19746 return Visit(S: E->getSubExpr());
19747}
19748
19749bool IntExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
19750 if (E->getSubExpr()->getType()->isComplexIntegerType()) {
19751 ComplexValue LV;
19752 if (!EvaluateComplex(E: E->getSubExpr(), Res&: LV, Info))
19753 return false;
19754 if (!LV.isComplexInt())
19755 return Error(E);
19756 return Success(SI: LV.getComplexIntImag(), E);
19757 }
19758
19759 VisitIgnoredValue(E: E->getSubExpr());
19760 return Success(Value: 0, E);
19761}
19762
19763bool IntExprEvaluator::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
19764 return Success(Value: E->getPackLength(), E);
19765}
19766
19767bool IntExprEvaluator::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
19768 return Success(Value: E->getValue(), E);
19769}
19770
19771bool IntExprEvaluator::VisitConceptSpecializationExpr(
19772 const ConceptSpecializationExpr *E) {
19773 return Success(Value: E->isSatisfied(), E);
19774}
19775
19776bool IntExprEvaluator::VisitRequiresExpr(const RequiresExpr *E) {
19777 return Success(Value: E->isSatisfied(), E);
19778}
19779
19780bool FixedPointExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
19781 switch (E->getOpcode()) {
19782 default:
19783 // Invalid unary operators
19784 return Error(E);
19785 case UO_Plus:
19786 // The result is just the value.
19787 return Visit(S: E->getSubExpr());
19788 case UO_Minus: {
19789 if (!Visit(S: E->getSubExpr())) return false;
19790 if (!Result.isFixedPoint())
19791 return Error(E);
19792 bool Overflowed;
19793 APFixedPoint Negated = Result.getFixedPoint().negate(Overflow: &Overflowed);
19794 if (Overflowed && !HandleOverflow(Info, E, SrcValue: Negated, DestType: E->getType()))
19795 return false;
19796 return Success(V: Negated, E);
19797 }
19798 case UO_LNot: {
19799 bool bres;
19800 if (!EvaluateAsBooleanCondition(E: E->getSubExpr(), Result&: bres, Info))
19801 return false;
19802 return Success(Value: !bres, E);
19803 }
19804 }
19805}
19806
19807bool FixedPointExprEvaluator::VisitCastExpr(const CastExpr *E) {
19808 const Expr *SubExpr = E->getSubExpr();
19809 QualType DestType = E->getType();
19810 assert(DestType->isFixedPointType() &&
19811 "Expected destination type to be a fixed point type");
19812 auto DestFXSema = Info.Ctx.getFixedPointSemantics(Ty: DestType);
19813
19814 switch (E->getCastKind()) {
19815 case CK_FixedPointCast: {
19816 APFixedPoint Src(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
19817 if (!EvaluateFixedPoint(E: SubExpr, Result&: Src, Info))
19818 return false;
19819 bool Overflowed;
19820 APFixedPoint Result = Src.convert(DstSema: DestFXSema, Overflow: &Overflowed);
19821 if (Overflowed) {
19822 if (Info.checkingForUndefinedBehavior())
19823 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
19824 DiagID: diag::warn_fixedpoint_constant_overflow)
19825 << Result.toString() << E->getType();
19826 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
19827 return false;
19828 }
19829 return Success(V: Result, E);
19830 }
19831 case CK_IntegralToFixedPoint: {
19832 APSInt Src;
19833 if (!EvaluateInteger(E: SubExpr, Result&: Src, Info))
19834 return false;
19835
19836 bool Overflowed;
19837 APFixedPoint IntResult = APFixedPoint::getFromIntValue(
19838 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
19839
19840 if (Overflowed) {
19841 if (Info.checkingForUndefinedBehavior())
19842 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
19843 DiagID: diag::warn_fixedpoint_constant_overflow)
19844 << IntResult.toString() << E->getType();
19845 if (!HandleOverflow(Info, E, SrcValue: IntResult, DestType: E->getType()))
19846 return false;
19847 }
19848
19849 return Success(V: IntResult, E);
19850 }
19851 case CK_FloatingToFixedPoint: {
19852 APFloat Src(0.0);
19853 if (!EvaluateFloat(E: SubExpr, Result&: Src, Info))
19854 return false;
19855
19856 bool Overflowed;
19857 APFixedPoint Result = APFixedPoint::getFromFloatValue(
19858 Value: Src, DstFXSema: Info.Ctx.getFixedPointSemantics(Ty: DestType), Overflow: &Overflowed);
19859
19860 if (Overflowed) {
19861 if (Info.checkingForUndefinedBehavior())
19862 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
19863 DiagID: diag::warn_fixedpoint_constant_overflow)
19864 << Result.toString() << E->getType();
19865 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
19866 return false;
19867 }
19868
19869 return Success(V: Result, E);
19870 }
19871 case CK_NoOp:
19872 case CK_LValueToRValue:
19873 return ExprEvaluatorBaseTy::VisitCastExpr(E);
19874 default:
19875 return Error(E);
19876 }
19877}
19878
19879bool FixedPointExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
19880 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
19881 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
19882
19883 const Expr *LHS = E->getLHS();
19884 const Expr *RHS = E->getRHS();
19885 FixedPointSemantics ResultFXSema =
19886 Info.Ctx.getFixedPointSemantics(Ty: E->getType());
19887
19888 APFixedPoint LHSFX(Info.Ctx.getFixedPointSemantics(Ty: LHS->getType()));
19889 if (!EvaluateFixedPointOrInteger(E: LHS, Result&: LHSFX, Info))
19890 return false;
19891 APFixedPoint RHSFX(Info.Ctx.getFixedPointSemantics(Ty: RHS->getType()));
19892 if (!EvaluateFixedPointOrInteger(E: RHS, Result&: RHSFX, Info))
19893 return false;
19894
19895 bool OpOverflow = false, ConversionOverflow = false;
19896 APFixedPoint Result(LHSFX.getSemantics());
19897 switch (E->getOpcode()) {
19898 case BO_Add: {
19899 Result = LHSFX.add(Other: RHSFX, Overflow: &OpOverflow)
19900 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
19901 break;
19902 }
19903 case BO_Sub: {
19904 Result = LHSFX.sub(Other: RHSFX, Overflow: &OpOverflow)
19905 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
19906 break;
19907 }
19908 case BO_Mul: {
19909 Result = LHSFX.mul(Other: RHSFX, Overflow: &OpOverflow)
19910 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
19911 break;
19912 }
19913 case BO_Div: {
19914 if (RHSFX.getValue() == 0) {
19915 Info.FFDiag(E, DiagId: diag::note_expr_divide_by_zero);
19916 return false;
19917 }
19918 Result = LHSFX.div(Other: RHSFX, Overflow: &OpOverflow)
19919 .convert(DstSema: ResultFXSema, Overflow: &ConversionOverflow);
19920 break;
19921 }
19922 case BO_Shl:
19923 case BO_Shr: {
19924 FixedPointSemantics LHSSema = LHSFX.getSemantics();
19925 llvm::APSInt RHSVal = RHSFX.getValue();
19926
19927 unsigned ShiftBW =
19928 LHSSema.getWidth() - (unsigned)LHSSema.hasUnsignedPadding();
19929 unsigned Amt = RHSVal.getLimitedValue(Limit: ShiftBW - 1);
19930 // Embedded-C 4.1.6.2.2:
19931 // The right operand must be nonnegative and less than the total number
19932 // of (nonpadding) bits of the fixed-point operand ...
19933 if (RHSVal.isNegative())
19934 Info.CCEDiag(E, DiagId: diag::note_constexpr_negative_shift) << RHSVal;
19935 else if (Amt != RHSVal)
19936 Info.CCEDiag(E, DiagId: diag::note_constexpr_large_shift)
19937 << RHSVal << E->getType() << ShiftBW;
19938
19939 if (E->getOpcode() == BO_Shl)
19940 Result = LHSFX.shl(Amt, Overflow: &OpOverflow);
19941 else
19942 Result = LHSFX.shr(Amt, Overflow: &OpOverflow);
19943 break;
19944 }
19945 default:
19946 return false;
19947 }
19948 if (OpOverflow || ConversionOverflow) {
19949 if (Info.checkingForUndefinedBehavior())
19950 Info.Ctx.getDiagnostics().Report(Loc: E->getExprLoc(),
19951 DiagID: diag::warn_fixedpoint_constant_overflow)
19952 << Result.toString() << E->getType();
19953 if (!HandleOverflow(Info, E, SrcValue: Result, DestType: E->getType()))
19954 return false;
19955 }
19956 return Success(V: Result, E);
19957}
19958
19959//===----------------------------------------------------------------------===//
19960// Float Evaluation
19961//===----------------------------------------------------------------------===//
19962
19963namespace {
19964class FloatExprEvaluator
19965 : public ExprEvaluatorBase<FloatExprEvaluator> {
19966 APFloat &Result;
19967public:
19968 FloatExprEvaluator(EvalInfo &info, APFloat &result)
19969 : ExprEvaluatorBaseTy(info), Result(result) {}
19970
19971 bool Success(const APValue &V, const Expr *e) {
19972 Result = V.getFloat();
19973 return true;
19974 }
19975
19976 bool ZeroInitialization(const Expr *E) {
19977 Result = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
19978 return true;
19979 }
19980
19981 bool VisitCallExpr(const CallExpr *E);
19982
19983 bool VisitUnaryOperator(const UnaryOperator *E);
19984 bool VisitBinaryOperator(const BinaryOperator *E);
19985 bool VisitFloatingLiteral(const FloatingLiteral *E);
19986 bool VisitCastExpr(const CastExpr *E);
19987
19988 bool VisitUnaryReal(const UnaryOperator *E);
19989 bool VisitUnaryImag(const UnaryOperator *E);
19990
19991 // FIXME: Missing: array subscript of vector, member of vector
19992};
19993} // end anonymous namespace
19994
19995static bool EvaluateFloat(const Expr* E, APFloat& Result, EvalInfo &Info) {
19996 assert(!E->isValueDependent());
19997 assert(E->isPRValue() && E->getType()->isRealFloatingType());
19998 return FloatExprEvaluator(Info, Result).Visit(S: E);
19999}
20000
20001static bool TryEvaluateBuiltinNaN(const ASTContext &Context,
20002 QualType ResultTy,
20003 const Expr *Arg,
20004 bool SNaN,
20005 llvm::APFloat &Result) {
20006 const StringLiteral *S = dyn_cast<StringLiteral>(Val: Arg->IgnoreParenCasts());
20007 if (!S) return false;
20008
20009 const llvm::fltSemantics &Sem = Context.getFloatTypeSemantics(T: ResultTy);
20010
20011 llvm::APInt fill;
20012
20013 // Treat empty strings as if they were zero.
20014 if (S->getString().empty())
20015 fill = llvm::APInt(32, 0);
20016 else if (S->getString().getAsInteger(Radix: 0, Result&: fill))
20017 return false;
20018
20019 if (Context.getTargetInfo().isNan2008()) {
20020 if (SNaN)
20021 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20022 else
20023 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20024 } else {
20025 // Prior to IEEE 754-2008, architectures were allowed to choose whether
20026 // the first bit of their significand was set for qNaN or sNaN. MIPS chose
20027 // a different encoding to what became a standard in 2008, and for pre-
20028 // 2008 revisions, MIPS interpreted sNaN-2008 as qNan and qNaN-2008 as
20029 // sNaN. This is now known as "legacy NaN" encoding.
20030 if (SNaN)
20031 Result = llvm::APFloat::getQNaN(Sem, Negative: false, payload: &fill);
20032 else
20033 Result = llvm::APFloat::getSNaN(Sem, Negative: false, payload: &fill);
20034 }
20035
20036 return true;
20037}
20038
20039bool FloatExprEvaluator::VisitCallExpr(const CallExpr *E) {
20040 if (!IsConstantEvaluatedBuiltinCall(E))
20041 return ExprEvaluatorBaseTy::VisitCallExpr(E);
20042
20043 switch (E->getBuiltinCallee()) {
20044 default:
20045 return false;
20046
20047 case Builtin::BI__builtin_huge_val:
20048 case Builtin::BI__builtin_huge_valf:
20049 case Builtin::BI__builtin_huge_vall:
20050 case Builtin::BI__builtin_huge_valf16:
20051 case Builtin::BI__builtin_huge_valf128:
20052 case Builtin::BI__builtin_inf:
20053 case Builtin::BI__builtin_inff:
20054 case Builtin::BI__builtin_infl:
20055 case Builtin::BI__builtin_inff16:
20056 case Builtin::BI__builtin_inff128: {
20057 const llvm::fltSemantics &Sem =
20058 Info.Ctx.getFloatTypeSemantics(T: E->getType());
20059 Result = llvm::APFloat::getInf(Sem);
20060 return true;
20061 }
20062
20063 case Builtin::BI__builtin_nans:
20064 case Builtin::BI__builtin_nansf:
20065 case Builtin::BI__builtin_nansl:
20066 case Builtin::BI__builtin_nansf16:
20067 case Builtin::BI__builtin_nansf128:
20068 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20069 SNaN: true, Result))
20070 return Error(E);
20071 return true;
20072
20073 case Builtin::BI__builtin_nan:
20074 case Builtin::BI__builtin_nanf:
20075 case Builtin::BI__builtin_nanl:
20076 case Builtin::BI__builtin_nanf16:
20077 case Builtin::BI__builtin_nanf128:
20078 // If this is __builtin_nan() turn this into a nan, otherwise we
20079 // can't constant fold it.
20080 if (!TryEvaluateBuiltinNaN(Context: Info.Ctx, ResultTy: E->getType(), Arg: E->getArg(Arg: 0),
20081 SNaN: false, Result))
20082 return Error(E);
20083 return true;
20084
20085 case Builtin::BI__builtin_elementwise_abs:
20086 case Builtin::BI__builtin_fabs:
20087 case Builtin::BI__builtin_fabsf:
20088 case Builtin::BI__builtin_fabsl:
20089 case Builtin::BI__builtin_fabsf128:
20090 // The C standard says "fabs raises no floating-point exceptions,
20091 // even if x is a signaling NaN. The returned value is independent of
20092 // the current rounding direction mode." Therefore constant folding can
20093 // proceed without regard to the floating point settings.
20094 // Reference, WG14 N2478 F.10.4.3
20095 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info))
20096 return false;
20097
20098 if (Result.isNegative())
20099 Result.changeSign();
20100 return true;
20101
20102 case Builtin::BI__arithmetic_fence:
20103 return EvaluateFloat(E: E->getArg(Arg: 0), Result, Info);
20104
20105 // FIXME: Builtin::BI__builtin_powi
20106 // FIXME: Builtin::BI__builtin_powif
20107 // FIXME: Builtin::BI__builtin_powil
20108
20109 case Builtin::BI__builtin_copysign:
20110 case Builtin::BI__builtin_copysignf:
20111 case Builtin::BI__builtin_copysignl:
20112 case Builtin::BI__builtin_copysignf128: {
20113 APFloat RHS(0.);
20114 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20115 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20116 return false;
20117 Result.copySign(RHS);
20118 return true;
20119 }
20120
20121 case Builtin::BI__builtin_fmax:
20122 case Builtin::BI__builtin_fmaxf:
20123 case Builtin::BI__builtin_fmaxl:
20124 case Builtin::BI__builtin_fmaxf16:
20125 case Builtin::BI__builtin_fmaxf128: {
20126 APFloat RHS(0.);
20127 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20128 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20129 return false;
20130 Result = maxnum(A: Result, B: RHS);
20131 return true;
20132 }
20133
20134 case Builtin::BI__builtin_fmin:
20135 case Builtin::BI__builtin_fminf:
20136 case Builtin::BI__builtin_fminl:
20137 case Builtin::BI__builtin_fminf16:
20138 case Builtin::BI__builtin_fminf128: {
20139 APFloat RHS(0.);
20140 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20141 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20142 return false;
20143 Result = minnum(A: Result, B: RHS);
20144 return true;
20145 }
20146
20147 case Builtin::BI__builtin_fmaximum_num:
20148 case Builtin::BI__builtin_fmaximum_numf:
20149 case Builtin::BI__builtin_fmaximum_numl:
20150 case Builtin::BI__builtin_fmaximum_numf16:
20151 case Builtin::BI__builtin_fmaximum_numf128: {
20152 APFloat RHS(0.);
20153 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20154 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20155 return false;
20156 Result = maximumnum(A: Result, B: RHS);
20157 return true;
20158 }
20159
20160 case Builtin::BI__builtin_fminimum_num:
20161 case Builtin::BI__builtin_fminimum_numf:
20162 case Builtin::BI__builtin_fminimum_numl:
20163 case Builtin::BI__builtin_fminimum_numf16:
20164 case Builtin::BI__builtin_fminimum_numf128: {
20165 APFloat RHS(0.);
20166 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20167 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: RHS, Info))
20168 return false;
20169 Result = minimumnum(A: Result, B: RHS);
20170 return true;
20171 }
20172
20173 case Builtin::BI__builtin_elementwise_fma: {
20174 if (!E->getArg(Arg: 0)->isPRValue() || !E->getArg(Arg: 1)->isPRValue() ||
20175 !E->getArg(Arg: 2)->isPRValue()) {
20176 return false;
20177 }
20178 APFloat SourceY(0.), SourceZ(0.);
20179 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result, Info) ||
20180 !EvaluateFloat(E: E->getArg(Arg: 1), Result&: SourceY, Info) ||
20181 !EvaluateFloat(E: E->getArg(Arg: 2), Result&: SourceZ, Info))
20182 return false;
20183 llvm::RoundingMode RM = getActiveRoundingMode(Info&: getEvalInfo(), E);
20184 (void)Result.fusedMultiplyAdd(Multiplicand: SourceY, Addend: SourceZ, RM);
20185 return true;
20186 }
20187
20188 case clang::X86::BI__builtin_ia32_vec_ext_v4sf: {
20189 APValue Vec;
20190 APSInt IdxAPS;
20191 if (!EvaluateVector(E: E->getArg(Arg: 0), Result&: Vec, Info) ||
20192 !EvaluateInteger(E: E->getArg(Arg: 1), Result&: IdxAPS, Info))
20193 return false;
20194 unsigned N = Vec.getVectorLength();
20195 unsigned Idx = static_cast<unsigned>(IdxAPS.getZExtValue() & (N - 1));
20196 return Success(V: Vec.getVectorElt(I: Idx), e: E);
20197 }
20198 }
20199}
20200
20201bool FloatExprEvaluator::VisitUnaryReal(const UnaryOperator *E) {
20202 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20203 ComplexValue CV;
20204 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20205 return false;
20206 Result = CV.FloatReal;
20207 return true;
20208 }
20209
20210 return Visit(S: E->getSubExpr());
20211}
20212
20213bool FloatExprEvaluator::VisitUnaryImag(const UnaryOperator *E) {
20214 if (E->getSubExpr()->getType()->isAnyComplexType()) {
20215 ComplexValue CV;
20216 if (!EvaluateComplex(E: E->getSubExpr(), Res&: CV, Info))
20217 return false;
20218 Result = CV.FloatImag;
20219 return true;
20220 }
20221
20222 VisitIgnoredValue(E: E->getSubExpr());
20223 const llvm::fltSemantics &Sem = Info.Ctx.getFloatTypeSemantics(T: E->getType());
20224 Result = llvm::APFloat::getZero(Sem);
20225 return true;
20226}
20227
20228bool FloatExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20229 switch (E->getOpcode()) {
20230 default: return Error(E);
20231 case UO_Plus:
20232 return EvaluateFloat(E: E->getSubExpr(), Result, Info);
20233 case UO_Minus:
20234 // In C standard, WG14 N2478 F.3 p4
20235 // "the unary - raises no floating point exceptions,
20236 // even if the operand is signalling."
20237 if (!EvaluateFloat(E: E->getSubExpr(), Result, Info))
20238 return false;
20239 Result.changeSign();
20240 return true;
20241 }
20242}
20243
20244bool FloatExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20245 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20246 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20247
20248 APFloat RHS(0.0);
20249 bool LHSOK = EvaluateFloat(E: E->getLHS(), Result, Info);
20250 if (!LHSOK && !Info.noteFailure())
20251 return false;
20252 return EvaluateFloat(E: E->getRHS(), Result&: RHS, Info) && LHSOK &&
20253 handleFloatFloatBinOp(Info, E, LHS&: Result, Opcode: E->getOpcode(), RHS);
20254}
20255
20256bool FloatExprEvaluator::VisitFloatingLiteral(const FloatingLiteral *E) {
20257 Result = E->getValue();
20258 return true;
20259}
20260
20261bool FloatExprEvaluator::VisitCastExpr(const CastExpr *E) {
20262 const Expr* SubExpr = E->getSubExpr();
20263
20264 switch (E->getCastKind()) {
20265 default:
20266 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20267
20268 case CK_HLSLAggregateSplatCast:
20269 llvm_unreachable("invalid cast kind for floating value");
20270
20271 case CK_IntegralToFloating: {
20272 APSInt IntResult;
20273 const FPOptions FPO = E->getFPFeaturesInEffect(
20274 LO: Info.Ctx.getLangOpts());
20275 return EvaluateInteger(E: SubExpr, Result&: IntResult, Info) &&
20276 HandleIntToFloatCast(Info, E, FPO, SrcType: SubExpr->getType(),
20277 Value: IntResult, DestType: E->getType(), Result);
20278 }
20279
20280 case CK_FixedPointToFloating: {
20281 APFixedPoint FixResult(Info.Ctx.getFixedPointSemantics(Ty: SubExpr->getType()));
20282 if (!EvaluateFixedPoint(E: SubExpr, Result&: FixResult, Info))
20283 return false;
20284 Result =
20285 FixResult.convertToFloat(FloatSema: Info.Ctx.getFloatTypeSemantics(T: E->getType()));
20286 return true;
20287 }
20288
20289 case CK_FloatingCast: {
20290 if (!Visit(S: SubExpr))
20291 return false;
20292 return HandleFloatToFloatCast(Info, E, SrcType: SubExpr->getType(), DestType: E->getType(),
20293 Result);
20294 }
20295
20296 case CK_FloatingComplexToReal: {
20297 ComplexValue V;
20298 if (!EvaluateComplex(E: SubExpr, Res&: V, Info))
20299 return false;
20300 Result = V.getComplexFloatReal();
20301 return true;
20302 }
20303 case CK_HLSLVectorTruncation: {
20304 APValue Val;
20305 if (!EvaluateVector(E: SubExpr, Result&: Val, Info))
20306 return Error(E);
20307 return Success(V: Val.getVectorElt(I: 0), e: E);
20308 }
20309 case CK_HLSLMatrixTruncation: {
20310 APValue Val;
20311 if (!EvaluateMatrix(E: SubExpr, Result&: Val, Info))
20312 return Error(E);
20313 return Success(V: Val.getMatrixElt(Row: 0, Col: 0), e: E);
20314 }
20315 case CK_HLSLElementwiseCast: {
20316 SmallVector<APValue> SrcVals;
20317 SmallVector<QualType> SrcTypes;
20318
20319 if (!hlslElementwiseCastHelper(Info, E: SubExpr, DestTy: E->getType(), SrcVals,
20320 SrcTypes))
20321 return false;
20322 APValue Val;
20323
20324 // cast our single element
20325 const FPOptions FPO = E->getFPFeaturesInEffect(LO: Info.Ctx.getLangOpts());
20326 APValue ResultVal;
20327 if (!handleScalarCast(Info, FPO, E, SourceTy: SrcTypes[0], DestTy: E->getType(), Original: SrcVals[0],
20328 Result&: ResultVal))
20329 return false;
20330 return Success(V: ResultVal, e: E);
20331 }
20332 }
20333}
20334
20335//===----------------------------------------------------------------------===//
20336// Complex Evaluation (for float and integer)
20337//===----------------------------------------------------------------------===//
20338
20339namespace {
20340class ComplexExprEvaluator
20341 : public ExprEvaluatorBase<ComplexExprEvaluator> {
20342 ComplexValue &Result;
20343
20344public:
20345 ComplexExprEvaluator(EvalInfo &info, ComplexValue &Result)
20346 : ExprEvaluatorBaseTy(info), Result(Result) {}
20347
20348 bool Success(const APValue &V, const Expr *e) {
20349 Result.setFrom(V);
20350 return true;
20351 }
20352
20353 bool ZeroInitialization(const Expr *E);
20354
20355 //===--------------------------------------------------------------------===//
20356 // Visitor Methods
20357 //===--------------------------------------------------------------------===//
20358
20359 bool VisitImaginaryLiteral(const ImaginaryLiteral *E);
20360 bool VisitCastExpr(const CastExpr *E);
20361 bool VisitBinaryOperator(const BinaryOperator *E);
20362 bool VisitUnaryOperator(const UnaryOperator *E);
20363 bool VisitInitListExpr(const InitListExpr *E);
20364 bool VisitCallExpr(const CallExpr *E);
20365};
20366} // end anonymous namespace
20367
20368static bool EvaluateComplex(const Expr *E, ComplexValue &Result,
20369 EvalInfo &Info) {
20370 assert(!E->isValueDependent());
20371 assert(E->isPRValue() && E->getType()->isAnyComplexType());
20372 return ComplexExprEvaluator(Info, Result).Visit(S: E);
20373}
20374
20375bool ComplexExprEvaluator::ZeroInitialization(const Expr *E) {
20376 QualType ElemTy = E->getType()->castAs<ComplexType>()->getElementType();
20377 if (ElemTy->isRealFloatingType()) {
20378 Result.makeComplexFloat();
20379 APFloat Zero = APFloat::getZero(Sem: Info.Ctx.getFloatTypeSemantics(T: ElemTy));
20380 Result.FloatReal = Zero;
20381 Result.FloatImag = Zero;
20382 } else {
20383 Result.makeComplexInt();
20384 APSInt Zero = Info.Ctx.MakeIntValue(Value: 0, Type: ElemTy);
20385 Result.IntReal = Zero;
20386 Result.IntImag = Zero;
20387 }
20388 return true;
20389}
20390
20391bool ComplexExprEvaluator::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
20392 const Expr* SubExpr = E->getSubExpr();
20393
20394 if (SubExpr->getType()->isRealFloatingType()) {
20395 Result.makeComplexFloat();
20396 APFloat &Imag = Result.FloatImag;
20397 if (!EvaluateFloat(E: SubExpr, Result&: Imag, Info))
20398 return false;
20399
20400 Result.FloatReal = APFloat(Imag.getSemantics());
20401 return true;
20402 } else {
20403 assert(SubExpr->getType()->isIntegerType() &&
20404 "Unexpected imaginary literal.");
20405
20406 Result.makeComplexInt();
20407 APSInt &Imag = Result.IntImag;
20408 if (!EvaluateInteger(E: SubExpr, Result&: Imag, Info))
20409 return false;
20410
20411 Result.IntReal = APSInt(Imag.getBitWidth(), !Imag.isSigned());
20412 return true;
20413 }
20414}
20415
20416bool ComplexExprEvaluator::VisitCastExpr(const CastExpr *E) {
20417
20418 switch (E->getCastKind()) {
20419 case CK_BitCast:
20420 case CK_BaseToDerived:
20421 case CK_DerivedToBase:
20422 case CK_UncheckedDerivedToBase:
20423 case CK_Dynamic:
20424 case CK_ToUnion:
20425 case CK_ArrayToPointerDecay:
20426 case CK_FunctionToPointerDecay:
20427 case CK_NullToPointer:
20428 case CK_NullToMemberPointer:
20429 case CK_BaseToDerivedMemberPointer:
20430 case CK_DerivedToBaseMemberPointer:
20431 case CK_MemberPointerToBoolean:
20432 case CK_ReinterpretMemberPointer:
20433 case CK_ConstructorConversion:
20434 case CK_IntegralToPointer:
20435 case CK_PointerToIntegral:
20436 case CK_PointerToBoolean:
20437 case CK_ToVoid:
20438 case CK_VectorSplat:
20439 case CK_IntegralCast:
20440 case CK_BooleanToSignedIntegral:
20441 case CK_IntegralToBoolean:
20442 case CK_IntegralToFloating:
20443 case CK_FloatingToIntegral:
20444 case CK_FloatingToBoolean:
20445 case CK_FloatingCast:
20446 case CK_CPointerToObjCPointerCast:
20447 case CK_BlockPointerToObjCPointerCast:
20448 case CK_AnyPointerToBlockPointerCast:
20449 case CK_ObjCObjectLValueCast:
20450 case CK_FloatingComplexToReal:
20451 case CK_FloatingComplexToBoolean:
20452 case CK_IntegralComplexToReal:
20453 case CK_IntegralComplexToBoolean:
20454 case CK_ARCProduceObject:
20455 case CK_ARCConsumeObject:
20456 case CK_ARCReclaimReturnedObject:
20457 case CK_ARCExtendBlockObject:
20458 case CK_CopyAndAutoreleaseBlockObject:
20459 case CK_BuiltinFnToFnPtr:
20460 case CK_ZeroToOCLOpaqueType:
20461 case CK_NonAtomicToAtomic:
20462 case CK_AddressSpaceConversion:
20463 case CK_IntToOCLSampler:
20464 case CK_FloatingToFixedPoint:
20465 case CK_FixedPointToFloating:
20466 case CK_FixedPointCast:
20467 case CK_FixedPointToBoolean:
20468 case CK_FixedPointToIntegral:
20469 case CK_IntegralToFixedPoint:
20470 case CK_MatrixCast:
20471 case CK_HLSLVectorTruncation:
20472 case CK_HLSLMatrixTruncation:
20473 case CK_HLSLElementwiseCast:
20474 case CK_HLSLAggregateSplatCast:
20475 llvm_unreachable("invalid cast kind for complex value");
20476
20477 case CK_LValueToRValue:
20478 case CK_AtomicToNonAtomic:
20479 case CK_NoOp:
20480 case CK_LValueToRValueBitCast:
20481 case CK_HLSLArrayRValue:
20482 return ExprEvaluatorBaseTy::VisitCastExpr(E);
20483
20484 case CK_Dependent:
20485 case CK_LValueBitCast:
20486 case CK_UserDefinedConversion:
20487 return Error(E);
20488
20489 case CK_FloatingRealToComplex: {
20490 APFloat &Real = Result.FloatReal;
20491 if (!EvaluateFloat(E: E->getSubExpr(), Result&: Real, Info))
20492 return false;
20493
20494 Result.makeComplexFloat();
20495 Result.FloatImag = APFloat(Real.getSemantics());
20496 return true;
20497 }
20498
20499 case CK_FloatingComplexCast: {
20500 if (!Visit(S: E->getSubExpr()))
20501 return false;
20502
20503 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
20504 QualType From
20505 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
20506
20507 return HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatReal) &&
20508 HandleFloatToFloatCast(Info, E, SrcType: From, DestType: To, Result&: Result.FloatImag);
20509 }
20510
20511 case CK_FloatingComplexToIntegralComplex: {
20512 if (!Visit(S: E->getSubExpr()))
20513 return false;
20514
20515 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
20516 QualType From
20517 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
20518 Result.makeComplexInt();
20519 return HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatReal,
20520 DestType: To, Result&: Result.IntReal) &&
20521 HandleFloatToIntCast(Info, E, SrcType: From, Value: Result.FloatImag,
20522 DestType: To, Result&: Result.IntImag);
20523 }
20524
20525 case CK_IntegralRealToComplex: {
20526 APSInt &Real = Result.IntReal;
20527 if (!EvaluateInteger(E: E->getSubExpr(), Result&: Real, Info))
20528 return false;
20529
20530 Result.makeComplexInt();
20531 Result.IntImag = APSInt(Real.getBitWidth(), !Real.isSigned());
20532 return true;
20533 }
20534
20535 case CK_IntegralComplexCast: {
20536 if (!Visit(S: E->getSubExpr()))
20537 return false;
20538
20539 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
20540 QualType From
20541 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
20542
20543 Result.IntReal = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntReal);
20544 Result.IntImag = HandleIntToIntCast(Info, E, DestType: To, SrcType: From, Value: Result.IntImag);
20545 return true;
20546 }
20547
20548 case CK_IntegralComplexToFloatingComplex: {
20549 if (!Visit(S: E->getSubExpr()))
20550 return false;
20551
20552 const FPOptions FPO = E->getFPFeaturesInEffect(
20553 LO: Info.Ctx.getLangOpts());
20554 QualType To = E->getType()->castAs<ComplexType>()->getElementType();
20555 QualType From
20556 = E->getSubExpr()->getType()->castAs<ComplexType>()->getElementType();
20557 Result.makeComplexFloat();
20558 return HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntReal,
20559 DestType: To, Result&: Result.FloatReal) &&
20560 HandleIntToFloatCast(Info, E, FPO, SrcType: From, Value: Result.IntImag,
20561 DestType: To, Result&: Result.FloatImag);
20562 }
20563 }
20564
20565 llvm_unreachable("unknown cast resulting in complex value");
20566}
20567
20568uint8_t GFNIMultiplicativeInverse(uint8_t Byte) {
20569 // Lookup Table for Multiplicative Inverse in GF(2^8)
20570 const uint8_t GFInv[256] = {
20571 0x00, 0x01, 0x8d, 0xf6, 0xcb, 0x52, 0x7b, 0xd1, 0xe8, 0x4f, 0x29, 0xc0,
20572 0xb0, 0xe1, 0xe5, 0xc7, 0x74, 0xb4, 0xaa, 0x4b, 0x99, 0x2b, 0x60, 0x5f,
20573 0x58, 0x3f, 0xfd, 0xcc, 0xff, 0x40, 0xee, 0xb2, 0x3a, 0x6e, 0x5a, 0xf1,
20574 0x55, 0x4d, 0xa8, 0xc9, 0xc1, 0x0a, 0x98, 0x15, 0x30, 0x44, 0xa2, 0xc2,
20575 0x2c, 0x45, 0x92, 0x6c, 0xf3, 0x39, 0x66, 0x42, 0xf2, 0x35, 0x20, 0x6f,
20576 0x77, 0xbb, 0x59, 0x19, 0x1d, 0xfe, 0x37, 0x67, 0x2d, 0x31, 0xf5, 0x69,
20577 0xa7, 0x64, 0xab, 0x13, 0x54, 0x25, 0xe9, 0x09, 0xed, 0x5c, 0x05, 0xca,
20578 0x4c, 0x24, 0x87, 0xbf, 0x18, 0x3e, 0x22, 0xf0, 0x51, 0xec, 0x61, 0x17,
20579 0x16, 0x5e, 0xaf, 0xd3, 0x49, 0xa6, 0x36, 0x43, 0xf4, 0x47, 0x91, 0xdf,
20580 0x33, 0x93, 0x21, 0x3b, 0x79, 0xb7, 0x97, 0x85, 0x10, 0xb5, 0xba, 0x3c,
20581 0xb6, 0x70, 0xd0, 0x06, 0xa1, 0xfa, 0x81, 0x82, 0x83, 0x7e, 0x7f, 0x80,
20582 0x96, 0x73, 0xbe, 0x56, 0x9b, 0x9e, 0x95, 0xd9, 0xf7, 0x02, 0xb9, 0xa4,
20583 0xde, 0x6a, 0x32, 0x6d, 0xd8, 0x8a, 0x84, 0x72, 0x2a, 0x14, 0x9f, 0x88,
20584 0xf9, 0xdc, 0x89, 0x9a, 0xfb, 0x7c, 0x2e, 0xc3, 0x8f, 0xb8, 0x65, 0x48,
20585 0x26, 0xc8, 0x12, 0x4a, 0xce, 0xe7, 0xd2, 0x62, 0x0c, 0xe0, 0x1f, 0xef,
20586 0x11, 0x75, 0x78, 0x71, 0xa5, 0x8e, 0x76, 0x3d, 0xbd, 0xbc, 0x86, 0x57,
20587 0x0b, 0x28, 0x2f, 0xa3, 0xda, 0xd4, 0xe4, 0x0f, 0xa9, 0x27, 0x53, 0x04,
20588 0x1b, 0xfc, 0xac, 0xe6, 0x7a, 0x07, 0xae, 0x63, 0xc5, 0xdb, 0xe2, 0xea,
20589 0x94, 0x8b, 0xc4, 0xd5, 0x9d, 0xf8, 0x90, 0x6b, 0xb1, 0x0d, 0xd6, 0xeb,
20590 0xc6, 0x0e, 0xcf, 0xad, 0x08, 0x4e, 0xd7, 0xe3, 0x5d, 0x50, 0x1e, 0xb3,
20591 0x5b, 0x23, 0x38, 0x34, 0x68, 0x46, 0x03, 0x8c, 0xdd, 0x9c, 0x7d, 0xa0,
20592 0xcd, 0x1a, 0x41, 0x1c};
20593
20594 return GFInv[Byte];
20595}
20596
20597uint8_t GFNIAffine(uint8_t XByte, const APInt &AQword, const APSInt &Imm,
20598 bool Inverse) {
20599 unsigned NumBitsInByte = 8;
20600 // Computing the affine transformation
20601 uint8_t RetByte = 0;
20602 for (uint32_t BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
20603 uint8_t AByte =
20604 AQword.lshr(shiftAmt: (7 - static_cast<int32_t>(BitIdx)) * NumBitsInByte)
20605 .getLoBits(numBits: 8)
20606 .getZExtValue();
20607 uint8_t Product;
20608 if (Inverse) {
20609 Product = AByte & GFNIMultiplicativeInverse(Byte: XByte);
20610 } else {
20611 Product = AByte & XByte;
20612 }
20613 uint8_t Parity = 0;
20614
20615 // Dot product in GF(2) uses XOR instead of addition
20616 for (unsigned PBitIdx = 0; PBitIdx != NumBitsInByte; ++PBitIdx) {
20617 Parity = Parity ^ ((Product >> PBitIdx) & 0x1);
20618 }
20619
20620 uint8_t Temp = Imm[BitIdx] ? 1 : 0;
20621 RetByte |= (Temp ^ Parity) << BitIdx;
20622 }
20623 return RetByte;
20624}
20625
20626uint8_t GFNIMul(uint8_t AByte, uint8_t BByte) {
20627 // Multiplying two polynomials of degree 7
20628 // Polynomial of degree 7
20629 // x^7 + x^6 + x^5 + x^4 + x^3 + x^2 + x + 1
20630 uint16_t TWord = 0;
20631 unsigned NumBitsInByte = 8;
20632 for (unsigned BitIdx = 0; BitIdx != NumBitsInByte; ++BitIdx) {
20633 if ((BByte >> BitIdx) & 0x1) {
20634 TWord = TWord ^ (AByte << BitIdx);
20635 }
20636 }
20637
20638 // When multiplying two polynomials of degree 7
20639 // results in a polynomial of degree 14
20640 // so the result has to be reduced to 7
20641 // Reduction polynomial is x^8 + x^4 + x^3 + x + 1 i.e. 0x11B
20642 for (int32_t BitIdx = 14; BitIdx > 7; --BitIdx) {
20643 if ((TWord >> BitIdx) & 0x1) {
20644 TWord = TWord ^ (0x11B << (BitIdx - 8));
20645 }
20646 }
20647 return (TWord & 0xFF);
20648}
20649
20650void HandleComplexComplexMul(APFloat A, APFloat B, APFloat C, APFloat D,
20651 APFloat &ResR, APFloat &ResI) {
20652 // This is an implementation of complex multiplication according to the
20653 // constraints laid out in C11 Annex G. The implementation uses the
20654 // following naming scheme:
20655 // (a + ib) * (c + id)
20656
20657 APFloat AC = A * C;
20658 APFloat BD = B * D;
20659 APFloat AD = A * D;
20660 APFloat BC = B * C;
20661 ResR = AC - BD;
20662 ResI = AD + BC;
20663 if (ResR.isNaN() && ResI.isNaN()) {
20664 bool Recalc = false;
20665 if (A.isInfinity() || B.isInfinity()) {
20666 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
20667 Sign: A);
20668 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
20669 Sign: B);
20670 if (C.isNaN())
20671 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
20672 if (D.isNaN())
20673 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
20674 Recalc = true;
20675 }
20676 if (C.isInfinity() || D.isInfinity()) {
20677 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
20678 Sign: C);
20679 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
20680 Sign: D);
20681 if (A.isNaN())
20682 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
20683 if (B.isNaN())
20684 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
20685 Recalc = true;
20686 }
20687 if (!Recalc && (AC.isInfinity() || BD.isInfinity() || AD.isInfinity() ||
20688 BC.isInfinity())) {
20689 if (A.isNaN())
20690 A = APFloat::copySign(Value: APFloat(A.getSemantics()), Sign: A);
20691 if (B.isNaN())
20692 B = APFloat::copySign(Value: APFloat(B.getSemantics()), Sign: B);
20693 if (C.isNaN())
20694 C = APFloat::copySign(Value: APFloat(C.getSemantics()), Sign: C);
20695 if (D.isNaN())
20696 D = APFloat::copySign(Value: APFloat(D.getSemantics()), Sign: D);
20697 Recalc = true;
20698 }
20699 if (Recalc) {
20700 ResR = APFloat::getInf(Sem: A.getSemantics()) * (A * C - B * D);
20701 ResI = APFloat::getInf(Sem: A.getSemantics()) * (A * D + B * C);
20702 }
20703 }
20704}
20705
20706void HandleComplexComplexDiv(APFloat A, APFloat B, APFloat C, APFloat D,
20707 APFloat &ResR, APFloat &ResI) {
20708 // This is an implementation of complex division according to the
20709 // constraints laid out in C11 Annex G. The implementation uses the
20710 // following naming scheme:
20711 // (a + ib) / (c + id)
20712
20713 int DenomLogB = 0;
20714 APFloat MaxCD = maxnum(A: abs(X: C), B: abs(X: D));
20715 if (MaxCD.isFinite()) {
20716 DenomLogB = ilogb(Arg: MaxCD);
20717 C = scalbn(X: C, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
20718 D = scalbn(X: D, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
20719 }
20720 APFloat Denom = C * C + D * D;
20721 ResR =
20722 scalbn(X: (A * C + B * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
20723 ResI =
20724 scalbn(X: (B * C - A * D) / Denom, Exp: -DenomLogB, RM: APFloat::rmNearestTiesToEven);
20725 if (ResR.isNaN() && ResI.isNaN()) {
20726 if (Denom.isPosZero() && (!A.isNaN() || !B.isNaN())) {
20727 ResR = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * A;
20728 ResI = APFloat::getInf(Sem: ResR.getSemantics(), Negative: C.isNegative()) * B;
20729 } else if ((A.isInfinity() || B.isInfinity()) && C.isFinite() &&
20730 D.isFinite()) {
20731 A = APFloat::copySign(Value: APFloat(A.getSemantics(), A.isInfinity() ? 1 : 0),
20732 Sign: A);
20733 B = APFloat::copySign(Value: APFloat(B.getSemantics(), B.isInfinity() ? 1 : 0),
20734 Sign: B);
20735 ResR = APFloat::getInf(Sem: ResR.getSemantics()) * (A * C + B * D);
20736 ResI = APFloat::getInf(Sem: ResI.getSemantics()) * (B * C - A * D);
20737 } else if (MaxCD.isInfinity() && A.isFinite() && B.isFinite()) {
20738 C = APFloat::copySign(Value: APFloat(C.getSemantics(), C.isInfinity() ? 1 : 0),
20739 Sign: C);
20740 D = APFloat::copySign(Value: APFloat(D.getSemantics(), D.isInfinity() ? 1 : 0),
20741 Sign: D);
20742 ResR = APFloat::getZero(Sem: ResR.getSemantics()) * (A * C + B * D);
20743 ResI = APFloat::getZero(Sem: ResI.getSemantics()) * (B * C - A * D);
20744 }
20745 }
20746}
20747
20748APSInt NormalizeRotateAmount(const APSInt &Value, const APSInt &Amount) {
20749 // Normalize shift amount to [0, BitWidth) range to match runtime behavior
20750 APSInt NormAmt = Amount;
20751 unsigned BitWidth = Value.getBitWidth();
20752 unsigned AmtBitWidth = NormAmt.getBitWidth();
20753 if (BitWidth == 1) {
20754 // Rotating a 1-bit value is always a no-op
20755 NormAmt = APSInt(APInt(AmtBitWidth, 0), NormAmt.isUnsigned());
20756 } else if (BitWidth == 2) {
20757 // For 2-bit values: rotation amount is 0 or 1 based on
20758 // whether the amount is even or odd. We can't use srem here because
20759 // the divisor (2) would be misinterpreted as -2 in 2-bit signed arithmetic.
20760 NormAmt =
20761 APSInt(APInt(AmtBitWidth, NormAmt[0] ? 1 : 0), NormAmt.isUnsigned());
20762 } else {
20763 APInt Divisor;
20764 if (AmtBitWidth > BitWidth) {
20765 Divisor = llvm::APInt(AmtBitWidth, BitWidth);
20766 } else {
20767 Divisor = llvm::APInt(BitWidth, BitWidth);
20768 if (AmtBitWidth < BitWidth) {
20769 NormAmt = NormAmt.extend(width: BitWidth);
20770 }
20771 }
20772
20773 // Normalize to [0, BitWidth)
20774 if (NormAmt.isSigned()) {
20775 NormAmt = APSInt(NormAmt.srem(RHS: Divisor), /*isUnsigned=*/false);
20776 if (NormAmt.isNegative()) {
20777 APSInt SignedDivisor(Divisor, /*isUnsigned=*/false);
20778 NormAmt += SignedDivisor;
20779 }
20780 } else {
20781 NormAmt = APSInt(NormAmt.urem(RHS: Divisor), /*isUnsigned=*/true);
20782 }
20783 }
20784
20785 return NormAmt;
20786}
20787
20788bool ComplexExprEvaluator::VisitBinaryOperator(const BinaryOperator *E) {
20789 if (E->isPtrMemOp() || E->isAssignmentOp() || E->getOpcode() == BO_Comma)
20790 return ExprEvaluatorBaseTy::VisitBinaryOperator(E);
20791
20792 // Track whether the LHS or RHS is real at the type system level. When this is
20793 // the case we can simplify our evaluation strategy.
20794 bool LHSReal = false, RHSReal = false;
20795
20796 bool LHSOK;
20797 if (E->getLHS()->getType()->isRealFloatingType()) {
20798 LHSReal = true;
20799 APFloat &Real = Result.FloatReal;
20800 LHSOK = EvaluateFloat(E: E->getLHS(), Result&: Real, Info);
20801 if (LHSOK) {
20802 Result.makeComplexFloat();
20803 Result.FloatImag = APFloat(Real.getSemantics());
20804 }
20805 } else {
20806 LHSOK = Visit(S: E->getLHS());
20807 }
20808 if (!LHSOK && !Info.noteFailure())
20809 return false;
20810
20811 ComplexValue RHS;
20812 if (E->getRHS()->getType()->isRealFloatingType()) {
20813 RHSReal = true;
20814 APFloat &Real = RHS.FloatReal;
20815 if (!EvaluateFloat(E: E->getRHS(), Result&: Real, Info) || !LHSOK)
20816 return false;
20817 RHS.makeComplexFloat();
20818 RHS.FloatImag = APFloat(Real.getSemantics());
20819 } else if (!EvaluateComplex(E: E->getRHS(), Result&: RHS, Info) || !LHSOK)
20820 return false;
20821
20822 assert(!(LHSReal && RHSReal) &&
20823 "Cannot have both operands of a complex operation be real.");
20824 switch (E->getOpcode()) {
20825 default: return Error(E);
20826 case BO_Add:
20827 if (Result.isComplexFloat()) {
20828 Result.getComplexFloatReal().add(RHS: RHS.getComplexFloatReal(),
20829 RM: APFloat::rmNearestTiesToEven);
20830 if (LHSReal)
20831 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
20832 else if (!RHSReal)
20833 Result.getComplexFloatImag().add(RHS: RHS.getComplexFloatImag(),
20834 RM: APFloat::rmNearestTiesToEven);
20835 } else {
20836 Result.getComplexIntReal() += RHS.getComplexIntReal();
20837 Result.getComplexIntImag() += RHS.getComplexIntImag();
20838 }
20839 break;
20840 case BO_Sub:
20841 if (Result.isComplexFloat()) {
20842 Result.getComplexFloatReal().subtract(RHS: RHS.getComplexFloatReal(),
20843 RM: APFloat::rmNearestTiesToEven);
20844 if (LHSReal) {
20845 Result.getComplexFloatImag() = RHS.getComplexFloatImag();
20846 Result.getComplexFloatImag().changeSign();
20847 } else if (!RHSReal) {
20848 Result.getComplexFloatImag().subtract(RHS: RHS.getComplexFloatImag(),
20849 RM: APFloat::rmNearestTiesToEven);
20850 }
20851 } else {
20852 Result.getComplexIntReal() -= RHS.getComplexIntReal();
20853 Result.getComplexIntImag() -= RHS.getComplexIntImag();
20854 }
20855 break;
20856 case BO_Mul:
20857 if (Result.isComplexFloat()) {
20858 // This is an implementation of complex multiplication according to the
20859 // constraints laid out in C11 Annex G. The implementation uses the
20860 // following naming scheme:
20861 // (a + ib) * (c + id)
20862 ComplexValue LHS = Result;
20863 APFloat &A = LHS.getComplexFloatReal();
20864 APFloat &B = LHS.getComplexFloatImag();
20865 APFloat &C = RHS.getComplexFloatReal();
20866 APFloat &D = RHS.getComplexFloatImag();
20867 APFloat &ResR = Result.getComplexFloatReal();
20868 APFloat &ResI = Result.getComplexFloatImag();
20869 if (LHSReal) {
20870 assert(!RHSReal && "Cannot have two real operands for a complex op!");
20871 ResR = A;
20872 ResI = A;
20873 // ResR = A * C;
20874 // ResI = A * D;
20875 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: C) ||
20876 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: D))
20877 return false;
20878 } else if (RHSReal) {
20879 // ResR = C * A;
20880 // ResI = C * B;
20881 ResR = C;
20882 ResI = C;
20883 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Mul, RHS: A) ||
20884 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Mul, RHS: B))
20885 return false;
20886 } else {
20887 HandleComplexComplexMul(A, B, C, D, ResR, ResI);
20888 }
20889 } else {
20890 ComplexValue LHS = Result;
20891 Result.getComplexIntReal() =
20892 (LHS.getComplexIntReal() * RHS.getComplexIntReal() -
20893 LHS.getComplexIntImag() * RHS.getComplexIntImag());
20894 Result.getComplexIntImag() =
20895 (LHS.getComplexIntReal() * RHS.getComplexIntImag() +
20896 LHS.getComplexIntImag() * RHS.getComplexIntReal());
20897 }
20898 break;
20899 case BO_Div:
20900 if (Result.isComplexFloat()) {
20901 // This is an implementation of complex division according to the
20902 // constraints laid out in C11 Annex G. The implementation uses the
20903 // following naming scheme:
20904 // (a + ib) / (c + id)
20905 ComplexValue LHS = Result;
20906 APFloat &A = LHS.getComplexFloatReal();
20907 APFloat &B = LHS.getComplexFloatImag();
20908 APFloat &C = RHS.getComplexFloatReal();
20909 APFloat &D = RHS.getComplexFloatImag();
20910 APFloat &ResR = Result.getComplexFloatReal();
20911 APFloat &ResI = Result.getComplexFloatImag();
20912 if (RHSReal) {
20913 ResR = A;
20914 ResI = B;
20915 // ResR = A / C;
20916 // ResI = B / C;
20917 if (!handleFloatFloatBinOp(Info, E, LHS&: ResR, Opcode: BO_Div, RHS: C) ||
20918 !handleFloatFloatBinOp(Info, E, LHS&: ResI, Opcode: BO_Div, RHS: C))
20919 return false;
20920 } else {
20921 if (LHSReal) {
20922 // No real optimizations we can do here, stub out with zero.
20923 B = APFloat::getZero(Sem: A.getSemantics());
20924 }
20925 HandleComplexComplexDiv(A, B, C, D, ResR, ResI);
20926 }
20927 } else {
20928 ComplexValue LHS = Result;
20929 APSInt Den = RHS.getComplexIntReal() * RHS.getComplexIntReal() +
20930 RHS.getComplexIntImag() * RHS.getComplexIntImag();
20931 if (Den.isZero())
20932 return Error(E, D: diag::note_expr_divide_by_zero);
20933
20934 Result.getComplexIntReal() =
20935 (LHS.getComplexIntReal() * RHS.getComplexIntReal() +
20936 LHS.getComplexIntImag() * RHS.getComplexIntImag()) / Den;
20937 Result.getComplexIntImag() =
20938 (LHS.getComplexIntImag() * RHS.getComplexIntReal() -
20939 LHS.getComplexIntReal() * RHS.getComplexIntImag()) / Den;
20940 }
20941 break;
20942 }
20943
20944 return true;
20945}
20946
20947bool ComplexExprEvaluator::VisitUnaryOperator(const UnaryOperator *E) {
20948 // Get the operand value into 'Result'.
20949 if (!Visit(S: E->getSubExpr()))
20950 return false;
20951
20952 switch (E->getOpcode()) {
20953 default:
20954 return Error(E);
20955 case UO_Extension:
20956 return true;
20957 case UO_Plus:
20958 // The result is always just the subexpr.
20959 return true;
20960 case UO_Minus:
20961 if (Result.isComplexFloat()) {
20962 Result.getComplexFloatReal().changeSign();
20963 Result.getComplexFloatImag().changeSign();
20964 }
20965 else {
20966 Result.getComplexIntReal() = -Result.getComplexIntReal();
20967 Result.getComplexIntImag() = -Result.getComplexIntImag();
20968 }
20969 return true;
20970 case UO_Not:
20971 if (Result.isComplexFloat())
20972 Result.getComplexFloatImag().changeSign();
20973 else
20974 Result.getComplexIntImag() = -Result.getComplexIntImag();
20975 return true;
20976 }
20977}
20978
20979bool ComplexExprEvaluator::VisitInitListExpr(const InitListExpr *E) {
20980 if (E->getNumInits() == 2) {
20981 if (E->getType()->isComplexType()) {
20982 Result.makeComplexFloat();
20983 if (!EvaluateFloat(E: E->getInit(Init: 0), Result&: Result.FloatReal, Info))
20984 return false;
20985 if (!EvaluateFloat(E: E->getInit(Init: 1), Result&: Result.FloatImag, Info))
20986 return false;
20987 } else {
20988 Result.makeComplexInt();
20989 if (!EvaluateInteger(E: E->getInit(Init: 0), Result&: Result.IntReal, Info))
20990 return false;
20991 if (!EvaluateInteger(E: E->getInit(Init: 1), Result&: Result.IntImag, Info))
20992 return false;
20993 }
20994 return true;
20995 }
20996 return ExprEvaluatorBaseTy::VisitInitListExpr(E);
20997}
20998
20999bool ComplexExprEvaluator::VisitCallExpr(const CallExpr *E) {
21000 if (!IsConstantEvaluatedBuiltinCall(E))
21001 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21002
21003 switch (E->getBuiltinCallee()) {
21004 case Builtin::BI__builtin_complex:
21005 Result.makeComplexFloat();
21006 if (!EvaluateFloat(E: E->getArg(Arg: 0), Result&: Result.FloatReal, Info))
21007 return false;
21008 if (!EvaluateFloat(E: E->getArg(Arg: 1), Result&: Result.FloatImag, Info))
21009 return false;
21010 return true;
21011
21012 default:
21013 return false;
21014 }
21015}
21016
21017//===----------------------------------------------------------------------===//
21018// Atomic expression evaluation, essentially just handling the NonAtomicToAtomic
21019// implicit conversion.
21020//===----------------------------------------------------------------------===//
21021
21022namespace {
21023class AtomicExprEvaluator :
21024 public ExprEvaluatorBase<AtomicExprEvaluator> {
21025 const LValue *This;
21026 APValue &Result;
21027public:
21028 AtomicExprEvaluator(EvalInfo &Info, const LValue *This, APValue &Result)
21029 : ExprEvaluatorBaseTy(Info), This(This), Result(Result) {}
21030
21031 bool Success(const APValue &V, const Expr *E) {
21032 Result = V;
21033 return true;
21034 }
21035
21036 bool ZeroInitialization(const Expr *E) {
21037 ImplicitValueInitExpr VIE(
21038 E->getType()->castAs<AtomicType>()->getValueType());
21039 // For atomic-qualified class (and array) types in C++, initialize the
21040 // _Atomic-wrapped subobject directly, in-place.
21041 return This ? EvaluateInPlace(Result, Info, This: *This, E: &VIE)
21042 : Evaluate(Result, Info, E: &VIE);
21043 }
21044
21045 bool VisitCastExpr(const CastExpr *E) {
21046 switch (E->getCastKind()) {
21047 default:
21048 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21049 case CK_NullToPointer:
21050 VisitIgnoredValue(E: E->getSubExpr());
21051 return ZeroInitialization(E);
21052 case CK_NonAtomicToAtomic:
21053 return This ? EvaluateInPlace(Result, Info, This: *This, E: E->getSubExpr())
21054 : Evaluate(Result, Info, E: E->getSubExpr());
21055 }
21056 }
21057};
21058} // end anonymous namespace
21059
21060static bool EvaluateAtomic(const Expr *E, const LValue *This, APValue &Result,
21061 EvalInfo &Info) {
21062 assert(!E->isValueDependent());
21063 assert(E->isPRValue() && E->getType()->isAtomicType());
21064 return AtomicExprEvaluator(Info, This, Result).Visit(S: E);
21065}
21066
21067//===----------------------------------------------------------------------===//
21068// Void expression evaluation, primarily for a cast to void on the LHS of a
21069// comma operator
21070//===----------------------------------------------------------------------===//
21071
21072namespace {
21073class VoidExprEvaluator
21074 : public ExprEvaluatorBase<VoidExprEvaluator> {
21075public:
21076 VoidExprEvaluator(EvalInfo &Info) : ExprEvaluatorBaseTy(Info) {}
21077
21078 bool Success(const APValue &V, const Expr *e) { return true; }
21079
21080 bool ZeroInitialization(const Expr *E) { return true; }
21081
21082 bool VisitCastExpr(const CastExpr *E) {
21083 switch (E->getCastKind()) {
21084 default:
21085 return ExprEvaluatorBaseTy::VisitCastExpr(E);
21086 case CK_ToVoid:
21087 VisitIgnoredValue(E: E->getSubExpr());
21088 return true;
21089 }
21090 }
21091
21092 bool VisitCallExpr(const CallExpr *E) {
21093 if (!IsConstantEvaluatedBuiltinCall(E))
21094 return ExprEvaluatorBaseTy::VisitCallExpr(E);
21095
21096 switch (E->getBuiltinCallee()) {
21097 case Builtin::BI__assume:
21098 case Builtin::BI__builtin_assume:
21099 // The argument is not evaluated!
21100 return true;
21101
21102 case Builtin::BI__builtin_operator_delete:
21103 return HandleOperatorDeleteCall(Info, E);
21104
21105 default:
21106 return false;
21107 }
21108 }
21109
21110 bool VisitCXXDeleteExpr(const CXXDeleteExpr *E);
21111};
21112} // end anonymous namespace
21113
21114bool VoidExprEvaluator::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
21115 // We cannot speculatively evaluate a delete expression.
21116 if (Info.SpeculativeEvaluationDepth)
21117 return false;
21118
21119 FunctionDecl *OperatorDelete = E->getOperatorDelete();
21120 if (!OperatorDelete
21121 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21122 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21123 << isa<CXXMethodDecl>(Val: OperatorDelete) << OperatorDelete;
21124 return false;
21125 }
21126
21127 const Expr *Arg = E->getArgument();
21128
21129 LValue Pointer;
21130 if (!EvaluatePointer(E: Arg, Result&: Pointer, Info))
21131 return false;
21132 if (Pointer.Designator.Invalid)
21133 return false;
21134
21135 // Deleting a null pointer has no effect.
21136 if (Pointer.isNullPointer()) {
21137 // This is the only case where we need to produce an extension warning:
21138 // the only other way we can succeed is if we find a dynamic allocation,
21139 // and we will have warned when we allocated it in that case.
21140 if (!Info.getLangOpts().CPlusPlus20)
21141 Info.CCEDiag(E, DiagId: diag::note_constexpr_new);
21142 return true;
21143 }
21144
21145 std::optional<DynAlloc *> Alloc = CheckDeleteKind(
21146 Info, E, Pointer, DeallocKind: E->isArrayForm() ? DynAlloc::ArrayNew : DynAlloc::New);
21147 if (!Alloc)
21148 return false;
21149 QualType AllocType = Pointer.Base.getDynamicAllocType();
21150
21151 // For the non-array case, the designator must be empty if the static type
21152 // does not have a virtual destructor.
21153 if (!E->isArrayForm() && Pointer.Designator.Entries.size() != 0 &&
21154 !hasVirtualDestructor(T: Arg->getType()->getPointeeType())) {
21155 Info.FFDiag(E, DiagId: diag::note_constexpr_delete_base_nonvirt_dtor)
21156 << Arg->getType()->getPointeeType() << AllocType;
21157 return false;
21158 }
21159
21160 // For a class type with a virtual destructor, the selected operator delete
21161 // is the one looked up when building the destructor.
21162 if (!E->isArrayForm() && !E->isGlobalDelete()) {
21163 const FunctionDecl *VirtualDelete = getVirtualOperatorDelete(T: AllocType);
21164 if (VirtualDelete &&
21165 !VirtualDelete
21166 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
21167 Info.FFDiag(E, DiagId: diag::note_constexpr_new_non_replaceable)
21168 << isa<CXXMethodDecl>(Val: VirtualDelete) << VirtualDelete;
21169 return false;
21170 }
21171 }
21172
21173 if (!HandleDestruction(Info, Loc: E->getExprLoc(), LVBase: Pointer.getLValueBase(),
21174 Value&: (*Alloc)->Value, T: AllocType))
21175 return false;
21176
21177 if (!Info.HeapAllocs.erase(x: Pointer.Base.dyn_cast<DynamicAllocLValue>())) {
21178 // The element was already erased. This means the destructor call also
21179 // deleted the object.
21180 // FIXME: This probably results in undefined behavior before we get this
21181 // far, and should be diagnosed elsewhere first.
21182 Info.FFDiag(E, DiagId: diag::note_constexpr_double_delete);
21183 return false;
21184 }
21185
21186 return true;
21187}
21188
21189static bool EvaluateVoid(const Expr *E, EvalInfo &Info) {
21190 assert(!E->isValueDependent());
21191 assert(E->isPRValue() && E->getType()->isVoidType());
21192 return VoidExprEvaluator(Info).Visit(S: E);
21193}
21194
21195//===----------------------------------------------------------------------===//
21196// Top level Expr::EvaluateAsRValue method.
21197//===----------------------------------------------------------------------===//
21198
21199static bool Evaluate(APValue &Result, EvalInfo &Info, const Expr *E) {
21200 assert(!E->isValueDependent());
21201 // In C, function designators are not lvalues, but we evaluate them as if they
21202 // are.
21203 QualType T = E->getType();
21204 if (E->isGLValue() || T->isFunctionType()) {
21205 LValue LV;
21206 if (!EvaluateLValue(E, Result&: LV, Info))
21207 return false;
21208 LV.moveInto(V&: Result);
21209 } else if (T->isVectorType()) {
21210 if (!EvaluateVector(E, Result, Info))
21211 return false;
21212 } else if (T->isConstantMatrixType()) {
21213 if (!EvaluateMatrix(E, Result, Info))
21214 return false;
21215 } else if (T->isIntegralOrEnumerationType()) {
21216 if (!IntExprEvaluator(Info, Result).Visit(S: E))
21217 return false;
21218 } else if (T->hasPointerRepresentation()) {
21219 LValue LV;
21220 if (!EvaluatePointer(E, Result&: LV, Info))
21221 return false;
21222 LV.moveInto(V&: Result);
21223 } else if (T->isRealFloatingType()) {
21224 llvm::APFloat F(0.0);
21225 if (!EvaluateFloat(E, Result&: F, Info))
21226 return false;
21227 Result = APValue(F);
21228 } else if (T->isAnyComplexType()) {
21229 ComplexValue C;
21230 if (!EvaluateComplex(E, Result&: C, Info))
21231 return false;
21232 C.moveInto(v&: Result);
21233 } else if (T->isFixedPointType()) {
21234 if (!FixedPointExprEvaluator(Info, Result).Visit(S: E)) return false;
21235 } else if (T->isMemberPointerType()) {
21236 MemberPtr P;
21237 if (!EvaluateMemberPointer(E, Result&: P, Info))
21238 return false;
21239 P.moveInto(V&: Result);
21240 return true;
21241 } else if (T->isArrayType()) {
21242 LValue LV;
21243 APValue &Value =
21244 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21245 if (!EvaluateArray(E, This: LV, Result&: Value, Info))
21246 return false;
21247 Result = Value;
21248 } else if (T->isRecordType()) {
21249 LValue LV;
21250 APValue &Value =
21251 Info.CurrentCall->createTemporary(Key: E, T, Scope: ScopeKind::FullExpression, LV);
21252 if (!EvaluateRecord(E, This: LV, Result&: Value, Info))
21253 return false;
21254 Result = Value;
21255 } else if (T->isVoidType()) {
21256 if (!Info.getLangOpts().CPlusPlus11)
21257 Info.CCEDiag(E, DiagId: diag::note_constexpr_nonliteral)
21258 << E->getType();
21259 if (!EvaluateVoid(E, Info))
21260 return false;
21261 } else if (T->isAtomicType()) {
21262 QualType Unqual = T.getAtomicUnqualifiedType();
21263 if (Unqual->isArrayType() || Unqual->isRecordType()) {
21264 LValue LV;
21265 APValue &Value = Info.CurrentCall->createTemporary(
21266 Key: E, T: Unqual, Scope: ScopeKind::FullExpression, LV);
21267 if (!EvaluateAtomic(E, This: &LV, Result&: Value, Info))
21268 return false;
21269 Result = Value;
21270 } else {
21271 if (!EvaluateAtomic(E, This: nullptr, Result, Info))
21272 return false;
21273 }
21274 } else if (Info.getLangOpts().CPlusPlus11) {
21275 Info.FFDiag(E, DiagId: diag::note_constexpr_nonliteral) << E->getType();
21276 return false;
21277 } else {
21278 Info.FFDiag(E, DiagId: diag::note_invalid_subexpr_in_const_expr);
21279 return false;
21280 }
21281
21282 return true;
21283}
21284
21285/// EvaluateInPlace - Evaluate an expression in-place in an APValue. In some
21286/// cases, the in-place evaluation is essential, since later initializers for
21287/// an object can indirectly refer to subobjects which were initialized earlier.
21288static bool EvaluateInPlace(APValue &Result, EvalInfo &Info, const LValue &This,
21289 const Expr *E, bool AllowNonLiteralTypes) {
21290 assert(!E->isValueDependent());
21291
21292 // Normally expressions passed to EvaluateInPlace have a type, but not when
21293 // a VarDecl initializer is evaluated before the untyped ParenListExpr is
21294 // replaced with a CXXConstructExpr. This can happen in LLDB.
21295 if (E->getType().isNull())
21296 return false;
21297
21298 if (!AllowNonLiteralTypes && !CheckLiteralType(Info, E, This: &This))
21299 return false;
21300
21301 if (E->isPRValue()) {
21302 // Evaluate arrays and record types in-place, so that later initializers can
21303 // refer to earlier-initialized members of the object.
21304 QualType T = E->getType();
21305 if (T->isArrayType())
21306 return EvaluateArray(E, This, Result, Info);
21307 else if (T->isRecordType())
21308 return EvaluateRecord(E, This, Result, Info);
21309 else if (T->isAtomicType()) {
21310 QualType Unqual = T.getAtomicUnqualifiedType();
21311 if (Unqual->isArrayType() || Unqual->isRecordType())
21312 return EvaluateAtomic(E, This: &This, Result, Info);
21313 }
21314 }
21315
21316 // For any other type, in-place evaluation is unimportant.
21317 return Evaluate(Result, Info, E);
21318}
21319
21320/// EvaluateAsRValue - Try to evaluate this expression, performing an implicit
21321/// lvalue-to-rvalue cast if it is an lvalue.
21322static bool EvaluateAsRValue(EvalInfo &Info, const Expr *E, APValue &Result) {
21323 assert(!E->isValueDependent());
21324
21325 if (E->getType().isNull())
21326 return false;
21327
21328 if (!CheckLiteralType(Info, E))
21329 return false;
21330
21331 if (Info.EnableNewConstInterp) {
21332 if (!Info.Ctx.getInterpContext().evaluateAsRValue(Parent&: Info, E, Result))
21333 return false;
21334 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
21335 Kind: ConstantExprKind::Normal);
21336 }
21337
21338 if (!::Evaluate(Result, Info, E))
21339 return false;
21340
21341 // Implicit lvalue-to-rvalue cast.
21342 if (E->isGLValue()) {
21343 LValue LV;
21344 LV.setFrom(Ctx: Info.Ctx, V: Result);
21345 if (!handleLValueToRValueConversion(Info, Conv: E, Type: E->getType(), LVal: LV, RVal&: Result))
21346 return false;
21347 }
21348
21349 // Check this core constant expression is a constant expression.
21350 return CheckConstantExpression(Info, DiagLoc: E->getExprLoc(), Type: E->getType(), Value: Result,
21351 Kind: ConstantExprKind::Normal) &&
21352 CheckMemoryLeaks(Info);
21353}
21354
21355static bool FastEvaluateAsRValue(const Expr *Exp, APValue &Result,
21356 const ASTContext &Ctx, bool &IsConst) {
21357 // Fast-path evaluations of integer literals, since we sometimes see files
21358 // containing vast quantities of these.
21359 if (const auto *L = dyn_cast<IntegerLiteral>(Val: Exp)) {
21360 Result =
21361 APValue(APSInt(L->getValue(), L->getType()->isUnsignedIntegerType()));
21362 IsConst = true;
21363 return true;
21364 }
21365
21366 if (const auto *L = dyn_cast<CXXBoolLiteralExpr>(Val: Exp)) {
21367 Result = APValue(APSInt(APInt(1, L->getValue())));
21368 IsConst = true;
21369 return true;
21370 }
21371
21372 if (const auto *FL = dyn_cast<FloatingLiteral>(Val: Exp)) {
21373 Result = APValue(FL->getValue());
21374 IsConst = true;
21375 return true;
21376 }
21377
21378 if (const auto *L = dyn_cast<CharacterLiteral>(Val: Exp)) {
21379 Result = APValue(Ctx.MakeIntValue(Value: L->getValue(), Type: L->getType()));
21380 IsConst = true;
21381 return true;
21382 }
21383
21384 if (const auto *CE = dyn_cast<ConstantExpr>(Val: Exp)) {
21385 if (CE->hasAPValueResult()) {
21386 APValue APV = CE->getAPValueResult();
21387 if (!APV.isLValue()) {
21388 Result = std::move(APV);
21389 IsConst = true;
21390 return true;
21391 }
21392 }
21393
21394 // The SubExpr is usually just an IntegerLiteral.
21395 return FastEvaluateAsRValue(Exp: CE->getSubExpr(), Result, Ctx, IsConst);
21396 }
21397
21398 // This case should be rare, but we need to check it before we check on
21399 // the type below.
21400 if (Exp->getType().isNull()) {
21401 IsConst = false;
21402 return true;
21403 }
21404
21405 return false;
21406}
21407
21408static bool hasUnacceptableSideEffect(Expr::EvalStatus &Result,
21409 Expr::SideEffectsKind SEK) {
21410 return (SEK < Expr::SE_AllowSideEffects && Result.HasSideEffects) ||
21411 (SEK < Expr::SE_AllowUndefinedBehavior && Result.HasUndefinedBehavior);
21412}
21413
21414static bool EvaluateAsRValue(const Expr *E, Expr::EvalResult &Result,
21415 const ASTContext &Ctx, EvalInfo &Info) {
21416 assert(!E->isValueDependent());
21417 bool IsConst;
21418 if (FastEvaluateAsRValue(Exp: E, Result&: Result.Val, Ctx, IsConst))
21419 return IsConst;
21420
21421 return EvaluateAsRValue(Info, E, Result&: Result.Val);
21422}
21423
21424static bool EvaluateAsInt(const Expr *E, Expr::EvalResult &ExprResult,
21425 const ASTContext &Ctx,
21426 Expr::SideEffectsKind AllowSideEffects,
21427 EvalInfo &Info) {
21428 assert(!E->isValueDependent());
21429 if (!E->getType()->isIntegralOrEnumerationType())
21430 return false;
21431
21432 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info) ||
21433 !ExprResult.Val.isInt() ||
21434 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21435 return false;
21436
21437 return true;
21438}
21439
21440static bool EvaluateAsFixedPoint(const Expr *E, Expr::EvalResult &ExprResult,
21441 const ASTContext &Ctx,
21442 Expr::SideEffectsKind AllowSideEffects,
21443 EvalInfo &Info) {
21444 assert(!E->isValueDependent());
21445 if (!E->getType()->isFixedPointType())
21446 return false;
21447
21448 if (!::EvaluateAsRValue(E, Result&: ExprResult, Ctx, Info))
21449 return false;
21450
21451 if (!ExprResult.Val.isFixedPoint() ||
21452 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21453 return false;
21454
21455 return true;
21456}
21457
21458/// EvaluateAsRValue - Return true if this is a constant which we can fold using
21459/// any crazy technique (that has nothing to do with language standards) that
21460/// we want to. If this function returns true, it returns the folded constant
21461/// in Result. If this expression is a glvalue, an lvalue-to-rvalue conversion
21462/// will be applied to the result.
21463bool Expr::EvaluateAsRValue(EvalResult &Result, const ASTContext &Ctx,
21464 bool InConstantContext) const {
21465 assert(!isValueDependent() &&
21466 "Expression evaluator can't be called on a dependent expression.");
21467 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsRValue");
21468 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21469 Info.InConstantContext = InConstantContext;
21470 return ::EvaluateAsRValue(E: this, Result, Ctx, Info);
21471}
21472
21473bool Expr::EvaluateAsBooleanCondition(bool &Result, const ASTContext &Ctx,
21474 bool InConstantContext) const {
21475 assert(!isValueDependent() &&
21476 "Expression evaluator can't be called on a dependent expression.");
21477 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsBooleanCondition");
21478 EvalResult Scratch;
21479 return EvaluateAsRValue(Result&: Scratch, Ctx, InConstantContext) &&
21480 HandleConversionToBool(Val: Scratch.Val, Result);
21481}
21482
21483bool Expr::EvaluateAsInt(EvalResult &Result, const ASTContext &Ctx,
21484 SideEffectsKind AllowSideEffects,
21485 bool InConstantContext) const {
21486 assert(!isValueDependent() &&
21487 "Expression evaluator can't be called on a dependent expression.");
21488 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsInt");
21489 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21490 Info.InConstantContext = InConstantContext;
21491 return ::EvaluateAsInt(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
21492}
21493
21494bool Expr::EvaluateAsFixedPoint(EvalResult &Result, const ASTContext &Ctx,
21495 SideEffectsKind AllowSideEffects,
21496 bool InConstantContext) const {
21497 assert(!isValueDependent() &&
21498 "Expression evaluator can't be called on a dependent expression.");
21499 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFixedPoint");
21500 EvalInfo Info(Ctx, Result, EvaluationMode::IgnoreSideEffects);
21501 Info.InConstantContext = InConstantContext;
21502 return ::EvaluateAsFixedPoint(E: this, ExprResult&: Result, Ctx, AllowSideEffects, Info);
21503}
21504
21505bool Expr::EvaluateAsFloat(APFloat &Result, const ASTContext &Ctx,
21506 SideEffectsKind AllowSideEffects,
21507 bool InConstantContext) const {
21508 assert(!isValueDependent() &&
21509 "Expression evaluator can't be called on a dependent expression.");
21510
21511 if (!getType()->isRealFloatingType())
21512 return false;
21513
21514 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsFloat");
21515 EvalResult ExprResult;
21516 if (!EvaluateAsRValue(Result&: ExprResult, Ctx, InConstantContext) ||
21517 !ExprResult.Val.isFloat() ||
21518 hasUnacceptableSideEffect(Result&: ExprResult, SEK: AllowSideEffects))
21519 return false;
21520
21521 Result = ExprResult.Val.getFloat();
21522 return true;
21523}
21524
21525bool Expr::EvaluateAsLValue(EvalResult &Result, const ASTContext &Ctx,
21526 bool InConstantContext) const {
21527 assert(!isValueDependent() &&
21528 "Expression evaluator can't be called on a dependent expression.");
21529
21530 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsLValue");
21531 EvalInfo Info(Ctx, Result, EvaluationMode::ConstantFold);
21532 Info.InConstantContext = InConstantContext;
21533 LValue LV;
21534 CheckedTemporaries CheckedTemps;
21535
21536 if (Info.EnableNewConstInterp) {
21537 if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val,
21538 Kind: ConstantExprKind::Normal))
21539 return false;
21540
21541 LV.setFrom(Ctx, V: Result.Val);
21542 return CheckLValueConstantExpression(
21543 Info, Loc: getExprLoc(), Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
21544 Kind: ConstantExprKind::Normal, CheckedTemps);
21545 }
21546
21547 if (!EvaluateLValue(E: this, Result&: LV, Info) || !Info.discardCleanups() ||
21548 Result.HasSideEffects ||
21549 !CheckLValueConstantExpression(Info, Loc: getExprLoc(),
21550 Type: Ctx.getLValueReferenceType(T: getType()), LVal: LV,
21551 Kind: ConstantExprKind::Normal, CheckedTemps))
21552 return false;
21553
21554 LV.moveInto(V&: Result.Val);
21555 return true;
21556}
21557
21558static bool EvaluateDestruction(const ASTContext &Ctx, APValue::LValueBase Base,
21559 APValue DestroyedValue, QualType Type,
21560 SourceLocation Loc, Expr::EvalStatus &EStatus,
21561 bool IsConstantDestruction) {
21562 EvalInfo Info(Ctx, EStatus,
21563 IsConstantDestruction ? EvaluationMode::ConstantExpression
21564 : EvaluationMode::ConstantFold);
21565 Info.setEvaluatingDecl(Base, Value&: DestroyedValue,
21566 EDK: EvalInfo::EvaluatingDeclKind::Dtor);
21567 Info.InConstantContext = IsConstantDestruction;
21568
21569 LValue LVal;
21570 LVal.set(B: Base);
21571
21572 if (!HandleDestruction(Info, Loc, LVBase: Base, Value&: DestroyedValue, T: Type) ||
21573 EStatus.HasSideEffects)
21574 return false;
21575
21576 if (!Info.discardCleanups())
21577 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
21578
21579 return true;
21580}
21581
21582bool Expr::EvaluateAsConstantExpr(EvalResult &Result, const ASTContext &Ctx,
21583 ConstantExprKind Kind) const {
21584 assert(!isValueDependent() &&
21585 "Expression evaluator can't be called on a dependent expression.");
21586 bool IsConst;
21587 if (FastEvaluateAsRValue(Exp: this, Result&: Result.Val, Ctx, IsConst) &&
21588 Result.Val.hasValue())
21589 return true;
21590
21591 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateAsConstantExpr");
21592 EvaluationMode EM = EvaluationMode::ConstantExpression;
21593 EvalInfo Info(Ctx, Result, EM);
21594 Info.InConstantContext = true;
21595
21596 if (Info.EnableNewConstInterp) {
21597 if (!Info.Ctx.getInterpContext().evaluate(Parent&: Info, E: this, Result&: Result.Val, Kind))
21598 return false;
21599 return CheckConstantExpression(Info, DiagLoc: getExprLoc(),
21600 Type: getStorageType(Ctx, E: this), Value: Result.Val, Kind);
21601 }
21602
21603 // The type of the object we're initializing is 'const T' for a class NTTP.
21604 QualType T = getType();
21605 if (Kind == ConstantExprKind::ClassTemplateArgument)
21606 T.addConst();
21607
21608 // If we're evaluating a prvalue, fake up a MaterializeTemporaryExpr to
21609 // represent the result of the evaluation. CheckConstantExpression ensures
21610 // this doesn't escape.
21611 MaterializeTemporaryExpr BaseMTE(T, const_cast<Expr*>(this), true);
21612 APValue::LValueBase Base(&BaseMTE);
21613 Info.setEvaluatingDecl(Base, Value&: Result.Val);
21614
21615 LValue LVal;
21616 LVal.set(B: Base);
21617 // C++23 [intro.execution]/p5
21618 // A full-expression is [...] a constant-expression
21619 // So we need to make sure temporary objects are destroyed after having
21620 // evaluating the expression (per C++23 [class.temporary]/p4).
21621 FullExpressionRAII Scope(Info);
21622 if (!::EvaluateInPlace(Result&: Result.Val, Info, This: LVal, E: this) ||
21623 Result.HasSideEffects || !Scope.destroy())
21624 return false;
21625
21626 if (!Info.discardCleanups())
21627 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
21628
21629 if (!CheckConstantExpression(Info, DiagLoc: getExprLoc(), Type: getStorageType(Ctx, E: this),
21630 Value: Result.Val, Kind))
21631 return false;
21632 if (!CheckMemoryLeaks(Info))
21633 return false;
21634
21635 // If this is a class template argument, it's required to have constant
21636 // destruction too.
21637 if (Kind == ConstantExprKind::ClassTemplateArgument &&
21638 (!EvaluateDestruction(Ctx, Base, DestroyedValue: Result.Val, Type: T, Loc: getBeginLoc(), EStatus&: Result,
21639 IsConstantDestruction: true) ||
21640 Result.HasSideEffects)) {
21641 // FIXME: Prefix a note to indicate that the problem is lack of constant
21642 // destruction.
21643 return false;
21644 }
21645
21646 return true;
21647}
21648
21649bool Expr::EvaluateAsInitializer(const ASTContext &Ctx, const VarDecl *VD,
21650 Expr::EvalResult &EStatus,
21651 bool IsConstantInitialization) const {
21652 assert(!isValueDependent() &&
21653 "Expression evaluator can't be called on a dependent expression.");
21654 assert(VD && "Need a valid VarDecl");
21655
21656 llvm::TimeTraceScope TimeScope("EvaluateAsInitializer", [&] {
21657 std::string Name;
21658 llvm::raw_string_ostream OS(Name);
21659 VD->printQualifiedName(OS);
21660 return Name;
21661 });
21662
21663 EvalInfo Info(Ctx, EStatus,
21664 (IsConstantInitialization &&
21665 (Ctx.getLangOpts().CPlusPlus || Ctx.getLangOpts().C23))
21666 ? EvaluationMode::ConstantExpression
21667 : EvaluationMode::ConstantFold);
21668 Info.setEvaluatingDecl(Base: VD, Value&: EStatus.Val);
21669 Info.InConstantContext = IsConstantInitialization;
21670
21671 SourceLocation DeclLoc = VD->getLocation();
21672 QualType DeclTy = VD->getType();
21673
21674 if (Info.EnableNewConstInterp) {
21675 auto &InterpCtx = Ctx.getInterpContext();
21676 if (!InterpCtx.evaluateAsInitializer(Parent&: Info, VD, Init: this, Result&: EStatus.Val))
21677 return false;
21678
21679 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
21680 Kind: ConstantExprKind::Normal);
21681 } else {
21682 LValue LVal;
21683 LVal.set(B: VD);
21684
21685 {
21686 // C++23 [intro.execution]/p5
21687 // A full-expression is ... an init-declarator ([dcl.decl]) or a
21688 // mem-initializer.
21689 // So we need to make sure temporary objects are destroyed after having
21690 // evaluated the expression (per C++23 [class.temporary]/p4).
21691 //
21692 // FIXME: Otherwise this may break test/Modules/pr68702.cpp because the
21693 // serialization code calls ParmVarDecl::getDefaultArg() which strips the
21694 // outermost FullExpr, such as ExprWithCleanups.
21695 FullExpressionRAII Scope(Info);
21696 if (!EvaluateInPlace(Result&: EStatus.Val, Info, This: LVal, E: this,
21697 /*AllowNonLiteralTypes=*/true) ||
21698 EStatus.HasSideEffects)
21699 return false;
21700 }
21701
21702 // At this point, any lifetime-extended temporaries are completely
21703 // initialized.
21704 Info.performLifetimeExtension();
21705
21706 if (!Info.discardCleanups())
21707 llvm_unreachable("Unhandled cleanup; missing full expression marker?");
21708 }
21709
21710 return CheckConstantExpression(Info, DiagLoc: DeclLoc, Type: DeclTy, Value: EStatus.Val,
21711 Kind: ConstantExprKind::Normal) &&
21712 CheckMemoryLeaks(Info);
21713}
21714
21715bool VarDecl::evaluateDestruction(
21716 SmallVectorImpl<PartialDiagnosticAt> &Notes) const {
21717 // This function is only meaningful for records and arrays of records.
21718 QualType VarTy = getType();
21719 if (VarTy->isArrayType()) {
21720 QualType ElemTy = getASTContext().getBaseElementType(QT: VarTy);
21721 if (!ElemTy->isRecordType()) {
21722 ensureEvaluatedStmt()->HasConstantDestruction = true;
21723 return true;
21724 }
21725 } else if (!VarTy->isRecordType()) {
21726 ensureEvaluatedStmt()->HasConstantDestruction = true;
21727 return true;
21728 }
21729
21730 Expr::EvalStatus EStatus;
21731 EStatus.Diag = &Notes;
21732
21733 // Only treat the destruction as constant destruction if we formally have
21734 // constant initialization (or are usable in a constant expression).
21735 bool IsConstantDestruction = hasConstantInitialization();
21736 ASTContext &Ctx = getASTContext();
21737
21738 // Make a copy of the value for the destructor to mutate, if we know it.
21739 // Otherwise, treat the value as default-initialized; if the destructor works
21740 // anyway, then the destruction is constant (and must be essentially empty).
21741 APValue DestroyedValue;
21742 if (getEvaluatedValue())
21743 DestroyedValue = *getEvaluatedValue();
21744 else if (!handleDefaultInitValue(T: VarTy, Result&: DestroyedValue))
21745 return false;
21746
21747 if (Ctx.getLangOpts().EnableNewConstInterp) {
21748 EvalInfo Info(Ctx, EStatus,
21749 IsConstantDestruction ? EvaluationMode::ConstantExpression
21750 : EvaluationMode::ConstantFold);
21751 Info.InConstantContext = IsConstantDestruction;
21752 if (!Ctx.getInterpContext().evaluateDestruction(Parent&: Info, VD: this,
21753 Value: std::move(DestroyedValue)))
21754 return false;
21755 ensureEvaluatedStmt()->HasConstantDestruction = true;
21756 return true;
21757 }
21758
21759 if (!EvaluateDestruction(Ctx, Base: this, DestroyedValue: std::move(DestroyedValue), Type: VarTy,
21760 Loc: getLocation(), EStatus, IsConstantDestruction) ||
21761 EStatus.HasSideEffects)
21762 return false;
21763
21764 ensureEvaluatedStmt()->HasConstantDestruction = true;
21765 return true;
21766}
21767
21768/// isEvaluatable - Call EvaluateAsRValue to see if this expression can be
21769/// constant folded, but discard the result.
21770bool Expr::isEvaluatable(const ASTContext &Ctx, SideEffectsKind SEK) const {
21771 assert(!isValueDependent() &&
21772 "Expression evaluator can't be called on a dependent expression.");
21773
21774 EvalResult Result;
21775 return EvaluateAsRValue(Result, Ctx, /* in constant context */ InConstantContext: true) &&
21776 !hasUnacceptableSideEffect(Result, SEK);
21777}
21778
21779APSInt Expr::EvaluateKnownConstInt(const ASTContext &Ctx) const {
21780 assert(!isValueDependent() &&
21781 "Expression evaluator can't be called on a dependent expression.");
21782
21783 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstInt");
21784 EvalResult EVResult;
21785 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
21786 Info.InConstantContext = true;
21787
21788 bool Result = ::EvaluateAsRValue(E: this, Result&: EVResult, Ctx, Info);
21789 (void)Result;
21790 assert(Result && "Could not evaluate expression");
21791 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
21792
21793 return EVResult.Val.getInt();
21794}
21795
21796APSInt Expr::EvaluateKnownConstIntCheckOverflow(
21797 const ASTContext &Ctx, SmallVectorImpl<PartialDiagnosticAt> *Diag) const {
21798 assert(!isValueDependent() &&
21799 "Expression evaluator can't be called on a dependent expression.");
21800
21801 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateKnownConstIntCheckOverflow");
21802 EvalResult EVResult;
21803 EVResult.Diag = Diag;
21804 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
21805 Info.InConstantContext = true;
21806 Info.CheckingForUndefinedBehavior = true;
21807
21808 bool Result = ::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
21809 (void)Result;
21810 assert(Result && "Could not evaluate expression");
21811 assert(EVResult.Val.isInt() && "Expression did not evaluate to integer");
21812
21813 return EVResult.Val.getInt();
21814}
21815
21816void Expr::EvaluateForOverflow(const ASTContext &Ctx) const {
21817 assert(!isValueDependent() &&
21818 "Expression evaluator can't be called on a dependent expression.");
21819
21820 ExprTimeTraceScope TimeScope(this, Ctx, "EvaluateForOverflow");
21821 bool IsConst;
21822 EvalResult EVResult;
21823 if (!FastEvaluateAsRValue(Exp: this, Result&: EVResult.Val, Ctx, IsConst)) {
21824 EvalInfo Info(Ctx, EVResult, EvaluationMode::IgnoreSideEffects);
21825 Info.CheckingForUndefinedBehavior = true;
21826 (void)::EvaluateAsRValue(Info, E: this, Result&: EVResult.Val);
21827 }
21828}
21829
21830bool Expr::EvalResult::isGlobalLValue() const {
21831 assert(Val.isLValue());
21832 return IsGlobalLValue(B: Val.getLValueBase());
21833}
21834
21835/// isIntegerConstantExpr - this recursive routine will test if an expression is
21836/// an integer constant expression.
21837
21838/// FIXME: Pass up a reason why! Invalid operation in i-c-e, division by zero,
21839/// comma, etc
21840
21841// CheckICE - This function does the fundamental ICE checking: the returned
21842// ICEDiag contains an ICEKind indicating whether the expression is an ICE.
21843//
21844// Note that to reduce code duplication, this helper does no evaluation
21845// itself; the caller checks whether the expression is evaluatable, and
21846// in the rare cases where CheckICE actually cares about the evaluated
21847// value, it calls into Evaluate.
21848
21849namespace {
21850
21851enum ICEKind {
21852 /// This expression is an ICE.
21853 IK_ICE,
21854 /// This expression is not an ICE, but if it isn't evaluated, it's
21855 /// a legal subexpression for an ICE. This return value is used to handle
21856 /// the comma operator in C99 mode, and non-constant subexpressions.
21857 IK_ICEIfUnevaluated,
21858 /// This expression is not an ICE, and is not a legal subexpression for one.
21859 IK_NotICE
21860};
21861
21862struct ICEDiag {
21863 ICEKind Kind;
21864 SourceLocation Loc;
21865
21866 ICEDiag(ICEKind IK, SourceLocation l) : Kind(IK), Loc(l) {}
21867};
21868
21869}
21870
21871static ICEDiag NoDiag() { return ICEDiag(IK_ICE, SourceLocation()); }
21872
21873static ICEDiag Worst(ICEDiag A, ICEDiag B) { return A.Kind >= B.Kind ? A : B; }
21874
21875static ICEDiag CheckEvalInICE(const Expr* E, const ASTContext &Ctx) {
21876 Expr::EvalResult EVResult;
21877 Expr::EvalStatus Status;
21878 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
21879
21880 Info.InConstantContext = true;
21881 if (!::EvaluateAsRValue(E, Result&: EVResult, Ctx, Info) || EVResult.HasSideEffects ||
21882 !EVResult.Val.isInt())
21883 return ICEDiag(IK_NotICE, E->getBeginLoc());
21884
21885 return NoDiag();
21886}
21887
21888static ICEDiag CheckICE(const Expr* E, const ASTContext &Ctx) {
21889 assert(!E->isValueDependent() && "Should not see value dependent exprs!");
21890 if (!E->getType()->isIntegralOrEnumerationType())
21891 return ICEDiag(IK_NotICE, E->getBeginLoc());
21892
21893 switch (E->getStmtClass()) {
21894#define ABSTRACT_STMT(Node)
21895#define STMT(Node, Base) case Expr::Node##Class:
21896#define EXPR(Node, Base)
21897#include "clang/AST/StmtNodes.inc"
21898 case Expr::PredefinedExprClass:
21899 case Expr::FloatingLiteralClass:
21900 case Expr::ImaginaryLiteralClass:
21901 case Expr::StringLiteralClass:
21902 case Expr::ArraySubscriptExprClass:
21903 case Expr::MatrixSingleSubscriptExprClass:
21904 case Expr::MatrixSubscriptExprClass:
21905 case Expr::ArraySectionExprClass:
21906 case Expr::OMPArrayShapingExprClass:
21907 case Expr::OMPIteratorExprClass:
21908 case Expr::CompoundAssignOperatorClass:
21909 case Expr::CompoundLiteralExprClass:
21910 case Expr::ExtVectorElementExprClass:
21911 case Expr::MatrixElementExprClass:
21912 case Expr::DesignatedInitExprClass:
21913 case Expr::ArrayInitLoopExprClass:
21914 case Expr::ArrayInitIndexExprClass:
21915 case Expr::NoInitExprClass:
21916 case Expr::DesignatedInitUpdateExprClass:
21917 case Expr::ImplicitValueInitExprClass:
21918 case Expr::ParenListExprClass:
21919 case Expr::VAArgExprClass:
21920 case Expr::AddrLabelExprClass:
21921 case Expr::StmtExprClass:
21922 case Expr::CXXMemberCallExprClass:
21923 case Expr::CUDAKernelCallExprClass:
21924 case Expr::CXXAddrspaceCastExprClass:
21925 case Expr::CXXDynamicCastExprClass:
21926 case Expr::CXXTypeidExprClass:
21927 case Expr::CXXUuidofExprClass:
21928 case Expr::MSPropertyRefExprClass:
21929 case Expr::MSPropertySubscriptExprClass:
21930 case Expr::CXXNullPtrLiteralExprClass:
21931 case Expr::UserDefinedLiteralClass:
21932 case Expr::CXXThisExprClass:
21933 case Expr::CXXThrowExprClass:
21934 case Expr::CXXNewExprClass:
21935 case Expr::CXXDeleteExprClass:
21936 case Expr::CXXPseudoDestructorExprClass:
21937 case Expr::UnresolvedLookupExprClass:
21938 case Expr::RecoveryExprClass:
21939 case Expr::DependentScopeDeclRefExprClass:
21940 case Expr::CXXConstructExprClass:
21941 case Expr::CXXInheritedCtorInitExprClass:
21942 case Expr::CXXStdInitializerListExprClass:
21943 case Expr::CXXBindTemporaryExprClass:
21944 case Expr::ExprWithCleanupsClass:
21945 case Expr::CXXTemporaryObjectExprClass:
21946 case Expr::CXXUnresolvedConstructExprClass:
21947 case Expr::CXXDependentScopeMemberExprClass:
21948 case Expr::UnresolvedMemberExprClass:
21949 case Expr::ObjCStringLiteralClass:
21950 case Expr::ObjCBoxedExprClass:
21951 case Expr::ObjCArrayLiteralClass:
21952 case Expr::ObjCDictionaryLiteralClass:
21953 case Expr::ObjCEncodeExprClass:
21954 case Expr::ObjCMessageExprClass:
21955 case Expr::ObjCSelectorExprClass:
21956 case Expr::ObjCProtocolExprClass:
21957 case Expr::ObjCIvarRefExprClass:
21958 case Expr::ObjCPropertyRefExprClass:
21959 case Expr::ObjCSubscriptRefExprClass:
21960 case Expr::ObjCIsaExprClass:
21961 case Expr::ObjCAvailabilityCheckExprClass:
21962 case Expr::ShuffleVectorExprClass:
21963 case Expr::ConvertVectorExprClass:
21964 case Expr::BlockExprClass:
21965 case Expr::NoStmtClass:
21966 case Expr::OpaqueValueExprClass:
21967 case Expr::PackExpansionExprClass:
21968 case Expr::SubstNonTypeTemplateParmPackExprClass:
21969 case Expr::FunctionParmPackExprClass:
21970 case Expr::AsTypeExprClass:
21971 case Expr::ObjCIndirectCopyRestoreExprClass:
21972 case Expr::MaterializeTemporaryExprClass:
21973 case Expr::PseudoObjectExprClass:
21974 case Expr::AtomicExprClass:
21975 case Expr::LambdaExprClass:
21976 case Expr::CXXFoldExprClass:
21977 case Expr::CoawaitExprClass:
21978 case Expr::DependentCoawaitExprClass:
21979 case Expr::CoyieldExprClass:
21980 case Expr::SYCLUniqueStableNameExprClass:
21981 case Expr::CXXParenListInitExprClass:
21982 case Expr::HLSLOutArgExprClass:
21983 return ICEDiag(IK_NotICE, E->getBeginLoc());
21984
21985 case Expr::MemberExprClass: {
21986 if (Ctx.getLangOpts().C23) {
21987 const Expr *ME = E->IgnoreParenImpCasts();
21988 while (const auto *M = dyn_cast<MemberExpr>(Val: ME)) {
21989 if (M->isArrow())
21990 return ICEDiag(IK_NotICE, E->getBeginLoc());
21991 ME = M->getBase()->IgnoreParenImpCasts();
21992 }
21993 const auto *DRE = dyn_cast<DeclRefExpr>(Val: ME);
21994 if (DRE) {
21995 if (const auto *VD = dyn_cast<VarDecl>(Val: DRE->getDecl());
21996 VD && VD->isConstexpr())
21997 return CheckEvalInICE(E, Ctx);
21998 }
21999 }
22000 return ICEDiag(IK_NotICE, E->getBeginLoc());
22001 }
22002
22003 case Expr::InitListExprClass: {
22004 // C++03 [dcl.init]p13: If T is a scalar type, then a declaration of the
22005 // form "T x = { a };" is equivalent to "T x = a;".
22006 // Unless we're initializing a reference, T is a scalar as it is known to be
22007 // of integral or enumeration type.
22008 if (E->isPRValue())
22009 if (cast<InitListExpr>(Val: E)->getNumInits() == 1)
22010 return CheckICE(E: cast<InitListExpr>(Val: E)->getInit(Init: 0), Ctx);
22011 return ICEDiag(IK_NotICE, E->getBeginLoc());
22012 }
22013
22014 case Expr::SizeOfPackExprClass:
22015 case Expr::GNUNullExprClass:
22016 case Expr::SourceLocExprClass:
22017 case Expr::EmbedExprClass:
22018 case Expr::OpenACCAsteriskSizeExprClass:
22019 return NoDiag();
22020
22021 case Expr::PackIndexingExprClass:
22022 return CheckICE(E: cast<PackIndexingExpr>(Val: E)->getSelectedExpr(), Ctx);
22023
22024 case Expr::SubstNonTypeTemplateParmExprClass:
22025 return
22026 CheckICE(E: cast<SubstNonTypeTemplateParmExpr>(Val: E)->getReplacement(), Ctx);
22027
22028 case Expr::ConstantExprClass:
22029 return CheckICE(E: cast<ConstantExpr>(Val: E)->getSubExpr(), Ctx);
22030
22031 case Expr::ParenExprClass:
22032 return CheckICE(E: cast<ParenExpr>(Val: E)->getSubExpr(), Ctx);
22033 case Expr::GenericSelectionExprClass:
22034 return CheckICE(E: cast<GenericSelectionExpr>(Val: E)->getResultExpr(), Ctx);
22035 case Expr::IntegerLiteralClass:
22036 case Expr::FixedPointLiteralClass:
22037 case Expr::CharacterLiteralClass:
22038 case Expr::ObjCBoolLiteralExprClass:
22039 case Expr::CXXBoolLiteralExprClass:
22040 case Expr::CXXScalarValueInitExprClass:
22041 case Expr::TypeTraitExprClass:
22042 case Expr::ConceptSpecializationExprClass:
22043 case Expr::RequiresExprClass:
22044 case Expr::ArrayTypeTraitExprClass:
22045 case Expr::ExpressionTraitExprClass:
22046 case Expr::CXXNoexceptExprClass:
22047 case Expr::CXXReflectExprClass:
22048 return NoDiag();
22049 case Expr::CallExprClass:
22050 case Expr::CXXOperatorCallExprClass: {
22051 // C99 6.6/3 allows function calls within unevaluated subexpressions of
22052 // constant expressions, but they can never be ICEs because an ICE cannot
22053 // contain an operand of (pointer to) function type.
22054 const CallExpr *CE = cast<CallExpr>(Val: E);
22055 if (CE->getBuiltinCallee())
22056 return CheckEvalInICE(E, Ctx);
22057 return ICEDiag(IK_NotICE, E->getBeginLoc());
22058 }
22059 case Expr::CXXRewrittenBinaryOperatorClass:
22060 return CheckICE(E: cast<CXXRewrittenBinaryOperator>(Val: E)->getSemanticForm(),
22061 Ctx);
22062 case Expr::DeclRefExprClass: {
22063 const NamedDecl *D = cast<DeclRefExpr>(Val: E)->getDecl();
22064 if (isa<EnumConstantDecl>(Val: D))
22065 return NoDiag();
22066
22067 // C++ and OpenCL (FIXME: spec reference?) allow reading const-qualified
22068 // integer variables in constant expressions:
22069 //
22070 // C++ 7.1.5.1p2
22071 // A variable of non-volatile const-qualified integral or enumeration
22072 // type initialized by an ICE can be used in ICEs.
22073 //
22074 // We sometimes use CheckICE to check the C++98 rules in C++11 mode. In
22075 // that mode, use of reference variables should not be allowed.
22076 const VarDecl *VD = dyn_cast<VarDecl>(Val: D);
22077 if (VD && VD->isUsableInConstantExpressions(C: Ctx) &&
22078 !VD->getType()->isReferenceType())
22079 return NoDiag();
22080
22081 return ICEDiag(IK_NotICE, E->getBeginLoc());
22082 }
22083 case Expr::UnaryOperatorClass: {
22084 const UnaryOperator *Exp = cast<UnaryOperator>(Val: E);
22085 switch (Exp->getOpcode()) {
22086 case UO_PostInc:
22087 case UO_PostDec:
22088 case UO_PreInc:
22089 case UO_PreDec:
22090 case UO_AddrOf:
22091 case UO_Deref:
22092 case UO_Coawait:
22093 // C99 6.6/3 allows increment and decrement within unevaluated
22094 // subexpressions of constant expressions, but they can never be ICEs
22095 // because an ICE cannot contain an lvalue operand.
22096 return ICEDiag(IK_NotICE, E->getBeginLoc());
22097 case UO_Extension:
22098 case UO_LNot:
22099 case UO_Plus:
22100 case UO_Minus:
22101 case UO_Not:
22102 case UO_Real:
22103 case UO_Imag:
22104 return CheckICE(E: Exp->getSubExpr(), Ctx);
22105 }
22106 llvm_unreachable("invalid unary operator class");
22107 }
22108 case Expr::OffsetOfExprClass: {
22109 // Note that per C99, offsetof must be an ICE. And AFAIK, using
22110 // EvaluateAsRValue matches the proposed gcc behavior for cases like
22111 // "offsetof(struct s{int x[4];}, x[1.0])". This doesn't affect
22112 // compliance: we should warn earlier for offsetof expressions with
22113 // array subscripts that aren't ICEs, and if the array subscripts
22114 // are ICEs, the value of the offsetof must be an integer constant.
22115 return CheckEvalInICE(E, Ctx);
22116 }
22117 case Expr::UnaryExprOrTypeTraitExprClass: {
22118 const UnaryExprOrTypeTraitExpr *Exp = cast<UnaryExprOrTypeTraitExpr>(Val: E);
22119 if ((Exp->getKind() == UETT_SizeOf) &&
22120 Exp->getTypeOfArgument()->isVariableArrayType())
22121 return ICEDiag(IK_NotICE, E->getBeginLoc());
22122 if (Exp->getKind() == UETT_CountOf) {
22123 QualType ArgTy = Exp->getTypeOfArgument();
22124 if (ArgTy->isVariableArrayType()) {
22125 // We need to look whether the array is multidimensional. If it is,
22126 // then we want to check the size expression manually to see whether
22127 // it is an ICE or not.
22128 const auto *VAT = Ctx.getAsVariableArrayType(T: ArgTy);
22129 if (VAT->getElementType()->isArrayType())
22130 // Variable array size expression could be missing (e.g. int a[*][10])
22131 // In that case, it can't be a constant expression.
22132 return VAT->getSizeExpr() ? CheckICE(E: VAT->getSizeExpr(), Ctx)
22133 : ICEDiag(IK_NotICE, E->getBeginLoc());
22134
22135 // Otherwise, this is a regular VLA, which is definitely not an ICE.
22136 return ICEDiag(IK_NotICE, E->getBeginLoc());
22137 }
22138 }
22139 return NoDiag();
22140 }
22141 case Expr::BinaryOperatorClass: {
22142 const BinaryOperator *Exp = cast<BinaryOperator>(Val: E);
22143 switch (Exp->getOpcode()) {
22144 case BO_PtrMemD:
22145 case BO_PtrMemI:
22146 case BO_Assign:
22147 case BO_MulAssign:
22148 case BO_DivAssign:
22149 case BO_RemAssign:
22150 case BO_AddAssign:
22151 case BO_SubAssign:
22152 case BO_ShlAssign:
22153 case BO_ShrAssign:
22154 case BO_AndAssign:
22155 case BO_XorAssign:
22156 case BO_OrAssign:
22157 // C99 6.6/3 allows assignments within unevaluated subexpressions of
22158 // constant expressions, but they can never be ICEs because an ICE cannot
22159 // contain an lvalue operand.
22160 return ICEDiag(IK_NotICE, E->getBeginLoc());
22161
22162 case BO_Mul:
22163 case BO_Div:
22164 case BO_Rem:
22165 case BO_Add:
22166 case BO_Sub:
22167 case BO_Shl:
22168 case BO_Shr:
22169 case BO_LT:
22170 case BO_GT:
22171 case BO_LE:
22172 case BO_GE:
22173 case BO_EQ:
22174 case BO_NE:
22175 case BO_And:
22176 case BO_Xor:
22177 case BO_Or:
22178 case BO_Comma:
22179 case BO_Cmp: {
22180 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22181 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22182 if (Exp->getOpcode() == BO_Div ||
22183 Exp->getOpcode() == BO_Rem) {
22184 // EvaluateAsRValue gives an error for undefined Div/Rem, so make sure
22185 // we don't evaluate one.
22186 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE) {
22187 llvm::APSInt REval = Exp->getRHS()->EvaluateKnownConstInt(Ctx);
22188 if (REval == 0)
22189 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22190 if (REval.isSigned() && REval.isAllOnes()) {
22191 llvm::APSInt LEval = Exp->getLHS()->EvaluateKnownConstInt(Ctx);
22192 if (LEval.isMinSignedValue())
22193 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22194 }
22195 }
22196 }
22197 if (Exp->getOpcode() == BO_Comma) {
22198 if (Ctx.getLangOpts().C99) {
22199 // C99 6.6p3 introduces a strange edge case: comma can be in an ICE
22200 // if it isn't evaluated.
22201 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICE)
22202 return ICEDiag(IK_ICEIfUnevaluated, E->getBeginLoc());
22203 } else {
22204 // In both C89 and C++, commas in ICEs are illegal.
22205 return ICEDiag(IK_NotICE, E->getBeginLoc());
22206 }
22207 }
22208 return Worst(A: LHSResult, B: RHSResult);
22209 }
22210 case BO_LAnd:
22211 case BO_LOr: {
22212 ICEDiag LHSResult = CheckICE(E: Exp->getLHS(), Ctx);
22213 ICEDiag RHSResult = CheckICE(E: Exp->getRHS(), Ctx);
22214 if (LHSResult.Kind == IK_ICE && RHSResult.Kind == IK_ICEIfUnevaluated) {
22215 // Rare case where the RHS has a comma "side-effect"; we need
22216 // to actually check the condition to see whether the side
22217 // with the comma is evaluated.
22218 if ((Exp->getOpcode() == BO_LAnd) !=
22219 (Exp->getLHS()->EvaluateKnownConstInt(Ctx) == 0))
22220 return RHSResult;
22221 return NoDiag();
22222 }
22223
22224 return Worst(A: LHSResult, B: RHSResult);
22225 }
22226 }
22227 llvm_unreachable("invalid binary operator kind");
22228 }
22229 case Expr::ImplicitCastExprClass:
22230 case Expr::CStyleCastExprClass:
22231 case Expr::CXXFunctionalCastExprClass:
22232 case Expr::CXXStaticCastExprClass:
22233 case Expr::CXXReinterpretCastExprClass:
22234 case Expr::CXXConstCastExprClass:
22235 case Expr::ObjCBridgedCastExprClass: {
22236 const Expr *SubExpr = cast<CastExpr>(Val: E)->getSubExpr();
22237 if (isa<ExplicitCastExpr>(Val: E)) {
22238 if (const FloatingLiteral *FL
22239 = dyn_cast<FloatingLiteral>(Val: SubExpr->IgnoreParenImpCasts())) {
22240 unsigned DestWidth = Ctx.getIntWidth(T: E->getType());
22241 bool DestSigned = E->getType()->isSignedIntegerOrEnumerationType();
22242 APSInt IgnoredVal(DestWidth, !DestSigned);
22243 bool Ignored;
22244 // If the value does not fit in the destination type, the behavior is
22245 // undefined, so we are not required to treat it as a constant
22246 // expression.
22247 if (FL->getValue().convertToInteger(Result&: IgnoredVal,
22248 RM: llvm::APFloat::rmTowardZero,
22249 IsExact: &Ignored) & APFloat::opInvalidOp)
22250 return ICEDiag(IK_NotICE, E->getBeginLoc());
22251 return NoDiag();
22252 }
22253 }
22254 switch (cast<CastExpr>(Val: E)->getCastKind()) {
22255 case CK_LValueToRValue:
22256 case CK_AtomicToNonAtomic:
22257 case CK_NonAtomicToAtomic:
22258 case CK_NoOp:
22259 case CK_IntegralToBoolean:
22260 case CK_IntegralCast:
22261 return CheckICE(E: SubExpr, Ctx);
22262 default:
22263 return ICEDiag(IK_NotICE, E->getBeginLoc());
22264 }
22265 }
22266 case Expr::BinaryConditionalOperatorClass: {
22267 const BinaryConditionalOperator *Exp = cast<BinaryConditionalOperator>(Val: E);
22268 ICEDiag CommonResult = CheckICE(E: Exp->getCommon(), Ctx);
22269 if (CommonResult.Kind == IK_NotICE) return CommonResult;
22270 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
22271 if (FalseResult.Kind == IK_NotICE) return FalseResult;
22272 if (CommonResult.Kind == IK_ICEIfUnevaluated) return CommonResult;
22273 if (FalseResult.Kind == IK_ICEIfUnevaluated &&
22274 Exp->getCommon()->EvaluateKnownConstInt(Ctx) != 0) return NoDiag();
22275 return FalseResult;
22276 }
22277 case Expr::ConditionalOperatorClass: {
22278 const ConditionalOperator *Exp = cast<ConditionalOperator>(Val: E);
22279 // If the condition (ignoring parens) is a __builtin_constant_p call,
22280 // then only the true side is actually considered in an integer constant
22281 // expression, and it is fully evaluated. This is an important GNU
22282 // extension. See GCC PR38377 for discussion.
22283 if (const CallExpr *CallCE
22284 = dyn_cast<CallExpr>(Val: Exp->getCond()->IgnoreParenCasts()))
22285 if (CallCE->getBuiltinCallee() == Builtin::BI__builtin_constant_p)
22286 return CheckEvalInICE(E, Ctx);
22287 ICEDiag CondResult = CheckICE(E: Exp->getCond(), Ctx);
22288 if (CondResult.Kind == IK_NotICE)
22289 return CondResult;
22290
22291 ICEDiag TrueResult = CheckICE(E: Exp->getTrueExpr(), Ctx);
22292 ICEDiag FalseResult = CheckICE(E: Exp->getFalseExpr(), Ctx);
22293
22294 if (TrueResult.Kind == IK_NotICE)
22295 return TrueResult;
22296 if (FalseResult.Kind == IK_NotICE)
22297 return FalseResult;
22298 if (CondResult.Kind == IK_ICEIfUnevaluated)
22299 return CondResult;
22300 if (TrueResult.Kind == IK_ICE && FalseResult.Kind == IK_ICE)
22301 return NoDiag();
22302 // Rare case where the diagnostics depend on which side is evaluated
22303 // Note that if we get here, CondResult is 0, and at least one of
22304 // TrueResult and FalseResult is non-zero.
22305 if (Exp->getCond()->EvaluateKnownConstInt(Ctx) == 0)
22306 return FalseResult;
22307 return TrueResult;
22308 }
22309 case Expr::CXXDefaultArgExprClass:
22310 return CheckICE(E: cast<CXXDefaultArgExpr>(Val: E)->getExpr(), Ctx);
22311 case Expr::CXXDefaultInitExprClass:
22312 return CheckICE(E: cast<CXXDefaultInitExpr>(Val: E)->getExpr(), Ctx);
22313 case Expr::ChooseExprClass: {
22314 return CheckICE(E: cast<ChooseExpr>(Val: E)->getChosenSubExpr(), Ctx);
22315 }
22316 case Expr::BuiltinBitCastExprClass: {
22317 if (!checkBitCastConstexprEligibility(Info: nullptr, Ctx, BCE: cast<CastExpr>(Val: E)))
22318 return ICEDiag(IK_NotICE, E->getBeginLoc());
22319 return CheckICE(E: cast<CastExpr>(Val: E)->getSubExpr(), Ctx);
22320 }
22321 }
22322
22323 llvm_unreachable("Invalid StmtClass!");
22324}
22325
22326/// Evaluate an expression as a C++11 integral constant expression.
22327static bool EvaluateCPlusPlus11IntegralConstantExpr(const ASTContext &Ctx,
22328 const Expr *E,
22329 llvm::APSInt *Value) {
22330 if (!E->getType()->isIntegralOrUnscopedEnumerationType())
22331 return false;
22332
22333 APValue Result;
22334 if (!E->isCXX11ConstantExpr(Ctx, Result: &Result))
22335 return false;
22336
22337 if (!Result.isInt())
22338 return false;
22339
22340 if (Value) *Value = Result.getInt();
22341 return true;
22342}
22343
22344bool Expr::isIntegerConstantExpr(const ASTContext &Ctx) const {
22345 assert(!isValueDependent() &&
22346 "Expression evaluator can't be called on a dependent expression.");
22347
22348 ExprTimeTraceScope TimeScope(this, Ctx, "isIntegerConstantExpr");
22349
22350 if (Ctx.getLangOpts().CPlusPlus11)
22351 return EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: nullptr);
22352
22353 ICEDiag D = CheckICE(E: this, Ctx);
22354 if (D.Kind != IK_ICE)
22355 return false;
22356 return true;
22357}
22358
22359std::optional<llvm::APSInt>
22360Expr::getIntegerConstantExpr(const ASTContext &Ctx) const {
22361 if (isValueDependent()) {
22362 // Expression evaluator can't succeed on a dependent expression.
22363 return std::nullopt;
22364 }
22365
22366 if (Ctx.getLangOpts().CPlusPlus11) {
22367 APSInt Value;
22368 if (EvaluateCPlusPlus11IntegralConstantExpr(Ctx, E: this, Value: &Value))
22369 return Value;
22370 return std::nullopt;
22371 }
22372
22373 if (!isIntegerConstantExpr(Ctx))
22374 return std::nullopt;
22375
22376 // The only possible side-effects here are due to UB discovered in the
22377 // evaluation (for instance, INT_MAX + 1). In such a case, we are still
22378 // required to treat the expression as an ICE, so we produce the folded
22379 // value.
22380 EvalResult ExprResult;
22381 Expr::EvalStatus Status;
22382 EvalInfo Info(Ctx, Status, EvaluationMode::IgnoreSideEffects);
22383 Info.InConstantContext = true;
22384
22385 if (!::EvaluateAsInt(E: this, ExprResult, Ctx, AllowSideEffects: SE_AllowSideEffects, Info))
22386 llvm_unreachable("ICE cannot be evaluated!");
22387
22388 return ExprResult.Val.getInt();
22389}
22390
22391bool Expr::isCXX98IntegralConstantExpr(const ASTContext &Ctx) const {
22392 assert(!isValueDependent() &&
22393 "Expression evaluator can't be called on a dependent expression.");
22394
22395 return CheckICE(E: this, Ctx).Kind == IK_ICE;
22396}
22397
22398bool Expr::isCXX11ConstantExpr(const ASTContext &Ctx, APValue *Result) const {
22399 assert(!isValueDependent() &&
22400 "Expression evaluator can't be called on a dependent expression.");
22401
22402 // We support this checking in C++98 mode in order to diagnose compatibility
22403 // issues.
22404 assert(Ctx.getLangOpts().CPlusPlus);
22405
22406 bool IsConst;
22407 APValue Scratch;
22408 if (FastEvaluateAsRValue(Exp: this, Result&: Scratch, Ctx, IsConst) && Scratch.hasValue()) {
22409 if (Result)
22410 *Result = std::move(Scratch);
22411 return true;
22412 }
22413
22414 // Build evaluation settings.
22415 Expr::EvalStatus Status;
22416 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22417
22418 bool IsConstExpr =
22419 ::EvaluateAsRValue(Info, E: this, Result&: Result ? *Result : Scratch) &&
22420 // NOTE: We don't produce a diagnostic for this, but the callers that
22421 // call us on arbitrary full-expressions should generally not care.
22422 Info.discardCleanups() && !Status.HasSideEffects;
22423
22424 return IsConstExpr && !Status.DiagEmitted;
22425}
22426
22427bool Expr::EvaluateWithSubstitution(APValue &Value, ASTContext &Ctx,
22428 const FunctionDecl *Callee,
22429 ArrayRef<const Expr*> Args,
22430 const Expr *This) const {
22431 assert(!isValueDependent() &&
22432 "Expression evaluator can't be called on a dependent expression.");
22433
22434 llvm::TimeTraceScope TimeScope("EvaluateWithSubstitution", [&] {
22435 std::string Name;
22436 llvm::raw_string_ostream OS(Name);
22437 Callee->getNameForDiagnostic(OS, Policy: Ctx.getPrintingPolicy(),
22438 /*Qualified=*/true);
22439 return Name;
22440 });
22441
22442 Expr::EvalStatus Status;
22443 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpressionUnevaluated);
22444 Info.InConstantContext = true;
22445
22446 LValue ThisVal;
22447 const LValue *ThisPtr = nullptr;
22448 if (This) {
22449#ifndef NDEBUG
22450 auto *MD = dyn_cast<CXXMethodDecl>(Callee);
22451 assert(MD && "Don't provide `this` for non-methods.");
22452 assert(MD->isImplicitObjectMemberFunction() &&
22453 "Don't provide `this` for methods without an implicit object.");
22454#endif
22455 if (!This->isValueDependent() &&
22456 EvaluateObjectArgument(Info, Object: This, This&: ThisVal) &&
22457 !Info.EvalStatus.HasSideEffects)
22458 ThisPtr = &ThisVal;
22459
22460 // Ignore any side-effects from a failed evaluation. This is safe because
22461 // they can't interfere with any other argument evaluation.
22462 Info.EvalStatus.HasSideEffects = false;
22463 }
22464
22465 CallRef Call = Info.CurrentCall->createCall(Callee);
22466 for (ArrayRef<const Expr*>::iterator I = Args.begin(), E = Args.end();
22467 I != E; ++I) {
22468 unsigned Idx = I - Args.begin();
22469 if (Idx >= Callee->getNumParams())
22470 break;
22471 const ParmVarDecl *PVD = Callee->getParamDecl(i: Idx);
22472 if ((*I)->isValueDependent() ||
22473 !EvaluateCallArg(PVD, Arg: *I, Call, Info) ||
22474 Info.EvalStatus.HasSideEffects) {
22475 // If evaluation fails, throw away the argument entirely.
22476 if (APValue *Slot = Info.getParamSlot(Call, PVD))
22477 *Slot = APValue();
22478 }
22479
22480 // Ignore any side-effects from a failed evaluation. This is safe because
22481 // they can't interfere with any other argument evaluation.
22482 Info.EvalStatus.HasSideEffects = false;
22483 }
22484
22485 // Parameter cleanups happen in the caller and are not part of this
22486 // evaluation.
22487 Info.discardCleanups();
22488 Info.EvalStatus.HasSideEffects = false;
22489
22490 // Build fake call to Callee.
22491 CallStackFrame Frame(Info, Callee->getLocation(), Callee, ThisPtr, This,
22492 Call);
22493 // FIXME: Missing ExprWithCleanups in enable_if conditions?
22494 FullExpressionRAII Scope(Info);
22495 return Evaluate(Result&: Value, Info, E: this) && Scope.destroy() &&
22496 !Info.EvalStatus.HasSideEffects;
22497}
22498
22499bool Expr::isPotentialConstantExpr(const FunctionDecl *FD,
22500 SmallVectorImpl<
22501 PartialDiagnosticAt> &Diags) {
22502 // FIXME: It would be useful to check constexpr function templates, but at the
22503 // moment the constant expression evaluator cannot cope with the non-rigorous
22504 // ASTs which we build for dependent expressions.
22505 if (FD->isDependentContext())
22506 return true;
22507
22508 llvm::TimeTraceScope TimeScope("isPotentialConstantExpr", [&] {
22509 std::string Name;
22510 llvm::raw_string_ostream OS(Name);
22511 FD->getNameForDiagnostic(OS, Policy: FD->getASTContext().getPrintingPolicy(),
22512 /*Qualified=*/true);
22513 return Name;
22514 });
22515
22516 Expr::EvalStatus Status;
22517 Status.Diag = &Diags;
22518
22519 EvalInfo Info(FD->getASTContext(), Status,
22520 EvaluationMode::ConstantExpression);
22521 Info.InConstantContext = true;
22522 Info.CheckingPotentialConstantExpression = true;
22523
22524 // The constexpr VM attempts to compile all methods to bytecode here.
22525 if (Info.EnableNewConstInterp) {
22526 Info.Ctx.getInterpContext().isPotentialConstantExpr(Parent&: Info, FD);
22527 return Diags.empty();
22528 }
22529
22530 const CXXMethodDecl *MD = dyn_cast<CXXMethodDecl>(Val: FD);
22531 const CXXRecordDecl *RD = MD ? MD->getParent()->getCanonicalDecl() : nullptr;
22532
22533 // Fabricate an arbitrary expression on the stack and pretend that it
22534 // is a temporary being used as the 'this' pointer.
22535 LValue This;
22536 ImplicitValueInitExpr VIE(RD ? Info.Ctx.getCanonicalTagType(TD: RD)
22537 : Info.Ctx.IntTy);
22538 This.set(B: {&VIE, Info.CurrentCall->Index});
22539
22540 ArrayRef<const Expr*> Args;
22541
22542 APValue Scratch;
22543 if (const CXXConstructorDecl *CD = dyn_cast<CXXConstructorDecl>(Val: FD)) {
22544 // Evaluate the call as a constant initializer, to allow the construction
22545 // of objects of non-literal types.
22546 Info.setEvaluatingDecl(Base: This.getLValueBase(), Value&: Scratch);
22547 HandleConstructorCall(E: &VIE, This, Args, Definition: CD, Info, Result&: Scratch);
22548 } else {
22549 SourceLocation Loc = FD->getLocation();
22550 HandleFunctionCall(
22551 CallLoc: Loc, Callee: FD, ObjectArg: (MD && MD->isImplicitObjectMemberFunction()) ? &This : nullptr,
22552 E: &VIE, Args, Call: CallRef(), Body: FD->getBody(), Info, Result&: Scratch,
22553 /*ResultSlot=*/nullptr);
22554 }
22555
22556 return Diags.empty();
22557}
22558
22559bool Expr::isPotentialConstantExprUnevaluated(Expr *E,
22560 const FunctionDecl *FD,
22561 SmallVectorImpl<
22562 PartialDiagnosticAt> &Diags) {
22563 assert(!E->isValueDependent() &&
22564 "Expression evaluator can't be called on a dependent expression.");
22565
22566 Expr::EvalStatus Status;
22567 Status.Diag = &Diags;
22568
22569 EvalInfo Info(FD->getASTContext(), Status,
22570 EvaluationMode::ConstantExpressionUnevaluated);
22571 Info.InConstantContext = true;
22572 Info.CheckingPotentialConstantExpression = true;
22573
22574 if (Info.EnableNewConstInterp) {
22575 Info.Ctx.getInterpContext().isPotentialConstantExprUnevaluated(Parent&: Info, E, FD);
22576 return Diags.empty();
22577 }
22578
22579 // Fabricate a call stack frame to give the arguments a plausible cover story.
22580 CallStackFrame Frame(Info, SourceLocation(), FD, /*This=*/nullptr,
22581 /*CallExpr=*/nullptr, CallRef());
22582
22583 APValue ResultScratch;
22584 Evaluate(Result&: ResultScratch, Info, E);
22585 return Diags.empty();
22586}
22587
22588std::optional<uint64_t> Expr::tryEvaluateObjectSize(const ASTContext &Ctx,
22589 unsigned Type) const {
22590 if (!getType()->isPointerType())
22591 return std::nullopt;
22592
22593 Expr::EvalStatus Status;
22594 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
22595 if (Info.EnableNewConstInterp)
22596 return Info.Ctx.getInterpContext().tryEvaluateObjectSize(Parent&: Info, E: this, Kind: Type);
22597 return tryEvaluateBuiltinObjectSize(E: this, Type, Info);
22598}
22599
22600static std::optional<uint64_t>
22601EvaluateBuiltinStrLen(const Expr *E, EvalInfo &Info,
22602 std::string *StringResult) {
22603 if (!E->getType()->hasPointerRepresentation() || !E->isPRValue())
22604 return std::nullopt;
22605
22606 LValue String;
22607
22608 if (!EvaluatePointer(E, Result&: String, Info))
22609 return std::nullopt;
22610
22611 QualType CharTy = E->getType()->getPointeeType();
22612
22613 // Fast path: if it's a string literal, search the string value.
22614 if (const StringLiteral *S = dyn_cast_or_null<StringLiteral>(
22615 Val: String.getLValueBase().dyn_cast<const Expr *>())) {
22616 StringRef Str = S->getBytes();
22617 int64_t Off = String.Offset.getQuantity();
22618 if (Off >= 0 && (uint64_t)Off <= (uint64_t)Str.size() &&
22619 S->getCharByteWidth() == 1 &&
22620 // FIXME: Add fast-path for wchar_t too.
22621 Info.Ctx.hasSameUnqualifiedType(T1: CharTy, T2: Info.Ctx.CharTy)) {
22622 Str = Str.substr(Start: Off);
22623
22624 StringRef::size_type Pos = Str.find(C: 0);
22625 if (Pos != StringRef::npos)
22626 Str = Str.substr(Start: 0, N: Pos);
22627
22628 if (StringResult)
22629 *StringResult = Str;
22630 return Str.size();
22631 }
22632
22633 // Fall through to slow path.
22634 }
22635
22636 // Slow path: scan the bytes of the string looking for the terminating 0.
22637 for (uint64_t Strlen = 0; /**/; ++Strlen) {
22638 APValue Char;
22639 if (!handleLValueToRValueConversion(Info, Conv: E, Type: CharTy, LVal: String, RVal&: Char) ||
22640 !Char.isInt())
22641 return std::nullopt;
22642 if (!Char.getInt())
22643 return Strlen;
22644 else if (StringResult)
22645 StringResult->push_back(c: Char.getInt().getExtValue());
22646 if (!HandleLValueArrayAdjustment(Info, E, LVal&: String, EltTy: CharTy, Adjustment: 1))
22647 return std::nullopt;
22648 }
22649}
22650
22651std::optional<std::string> Expr::tryEvaluateString(ASTContext &Ctx) const {
22652 Expr::EvalStatus Status;
22653 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
22654 std::string StringResult;
22655
22656 if (Info.EnableNewConstInterp) {
22657 if (!Info.Ctx.getInterpContext().evaluateString(Parent&: Info, E: this, Result&: StringResult))
22658 return std::nullopt;
22659 return StringResult;
22660 }
22661
22662 if (EvaluateBuiltinStrLen(E: this, Info, StringResult: &StringResult))
22663 return StringResult;
22664 return std::nullopt;
22665}
22666
22667template <typename T>
22668static bool EvaluateCharRangeAsStringImpl(const Expr *, T &Result,
22669 const Expr *SizeExpression,
22670 const Expr *PtrExpression,
22671 ASTContext &Ctx,
22672 Expr::EvalResult &Status) {
22673 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantExpression);
22674 Info.InConstantContext = true;
22675
22676 if (Info.EnableNewConstInterp)
22677 return Info.Ctx.getInterpContext().evaluateCharRange(Info, SizeExpression,
22678 PtrExpression, Result);
22679
22680 LValue String;
22681 FullExpressionRAII Scope(Info);
22682 APSInt SizeValue;
22683 if (!::EvaluateInteger(E: SizeExpression, Result&: SizeValue, Info))
22684 return false;
22685
22686 uint64_t Size = SizeValue.getZExtValue();
22687
22688 // FIXME: better protect against invalid or excessive sizes
22689 if constexpr (std::is_same_v<APValue, T>)
22690 Result = APValue(APValue::UninitArray{}, Size, Size);
22691 else {
22692 if (Size < Result.max_size())
22693 Result.reserve(Size);
22694 }
22695 if (!::EvaluatePointer(E: PtrExpression, Result&: String, Info))
22696 return false;
22697
22698 QualType CharTy = PtrExpression->getType()->getPointeeType();
22699 for (uint64_t I = 0; I < Size; ++I) {
22700 APValue Char;
22701 if (!handleLValueToRValueConversion(Info, Conv: PtrExpression, Type: CharTy, LVal: String,
22702 RVal&: Char))
22703 return false;
22704
22705 if constexpr (std::is_same_v<APValue, T>) {
22706 Result.getArrayInitializedElt(I) = std::move(Char);
22707 } else {
22708 APSInt C = Char.getInt();
22709
22710 assert(C.getBitWidth() <= 8 &&
22711 "string element not representable in char");
22712
22713 Result.push_back(static_cast<char>(C.getExtValue()));
22714 }
22715
22716 if (!HandleLValueArrayAdjustment(Info, E: PtrExpression, LVal&: String, EltTy: CharTy, Adjustment: 1))
22717 return false;
22718 }
22719
22720 return Scope.destroy() && CheckMemoryLeaks(Info);
22721}
22722
22723bool Expr::EvaluateCharRangeAsString(std::string &Result,
22724 const Expr *SizeExpression,
22725 const Expr *PtrExpression, ASTContext &Ctx,
22726 EvalResult &Status) const {
22727 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
22728 PtrExpression, Ctx, Status);
22729}
22730
22731bool Expr::EvaluateCharRangeAsString(APValue &Result,
22732 const Expr *SizeExpression,
22733 const Expr *PtrExpression, ASTContext &Ctx,
22734 EvalResult &Status) const {
22735 return EvaluateCharRangeAsStringImpl(this, Result, SizeExpression,
22736 PtrExpression, Ctx, Status);
22737}
22738
22739std::optional<uint64_t> Expr::tryEvaluateStrLen(const ASTContext &Ctx) const {
22740 Expr::EvalStatus Status;
22741 EvalInfo Info(Ctx, Status, EvaluationMode::ConstantFold);
22742
22743 if (Info.EnableNewConstInterp)
22744 return Info.Ctx.getInterpContext().evaluateStrlen(Parent&: Info, E: this);
22745 return EvaluateBuiltinStrLen(E: this, Info);
22746}
22747
22748namespace {
22749struct IsWithinLifetimeHandler {
22750 EvalInfo &Info;
22751 static constexpr AccessKinds AccessKind = AccessKinds::AK_IsWithinLifetime;
22752 using result_type = std::optional<bool>;
22753 std::optional<bool> failed() { return std::nullopt; }
22754 template <typename T>
22755 std::optional<bool> found(T &Subobj, QualType SubobjType,
22756 APValue::LValueBase) {
22757 return true;
22758 }
22759 template <typename T>
22760 std::optional<bool> found(T &Subobj, QualType SubobjType) {
22761 return true;
22762 }
22763};
22764
22765std::optional<bool> EvaluateBuiltinIsWithinLifetime(IntExprEvaluator &IEE,
22766 const CallExpr *E) {
22767 EvalInfo &Info = IEE.Info;
22768 // Sometimes this is called during some sorts of constant folding / early
22769 // evaluation. These are meant for non-constant expressions and are not
22770 // necessary since this consteval builtin will never be evaluated at runtime.
22771 // Just fail to evaluate when not in a constant context.
22772 if (!Info.InConstantContext)
22773 return std::nullopt;
22774 assert(E->getBuiltinCallee() == Builtin::BI__builtin_is_within_lifetime);
22775 const Expr *Arg = E->getArg(Arg: 0);
22776 if (Arg->isValueDependent())
22777 return std::nullopt;
22778 LValue Val;
22779 if (!EvaluatePointer(E: Arg, Result&: Val, Info))
22780 return std::nullopt;
22781
22782 if (Val.allowConstexprUnknown())
22783 return true;
22784
22785 auto Error = [&](int Diag) {
22786 bool CalledFromStd = false;
22787 const auto *Callee = Info.CurrentCall->getCallee();
22788 if (Callee && Callee->isInStdNamespace()) {
22789 const IdentifierInfo *Identifier = Callee->getIdentifier();
22790 CalledFromStd = Identifier && Identifier->isStr(Str: "is_within_lifetime");
22791 }
22792 Info.CCEDiag(Loc: CalledFromStd ? Info.CurrentCall->getCallRange().getBegin()
22793 : E->getExprLoc(),
22794 DiagId: diag::err_invalid_is_within_lifetime)
22795 << (CalledFromStd ? "std::is_within_lifetime"
22796 : "__builtin_is_within_lifetime")
22797 << Diag;
22798 return std::nullopt;
22799 };
22800 // C++2c [meta.const.eval]p4:
22801 // During the evaluation of an expression E as a core constant expression, a
22802 // call to this function is ill-formed unless p points to an object that is
22803 // usable in constant expressions or whose complete object's lifetime began
22804 // within E.
22805
22806 // Make sure it points to an object
22807 // nullptr does not point to an object
22808 if (Val.isNullPointer() || Val.getLValueBase().isNull())
22809 return Error(0);
22810 QualType T = Val.getLValueBase().getType();
22811 assert(!T->isFunctionType() &&
22812 "Pointers to functions should have been typed as function pointers "
22813 "which would have been rejected earlier");
22814 assert(T->isObjectType());
22815 // Hypothetical array element is not an object
22816 if (Val.getLValueDesignator().isOnePastTheEnd())
22817 return Error(1);
22818 assert(Val.getLValueDesignator().isValidSubobject() &&
22819 "Unchecked case for valid subobject");
22820 // All other ill-formed values should have failed EvaluatePointer, so the
22821 // object should be a pointer to an object that is usable in a constant
22822 // expression or whose complete lifetime began within the expression
22823 CompleteObject CO =
22824 findCompleteObject(Info, E, AK: AccessKinds::AK_IsWithinLifetime, LVal: Val, LValType: T);
22825 // The lifetime hasn't begun yet if we are still evaluating the
22826 // initializer ([basic.life]p(1.2))
22827 if (Info.EvaluatingDeclValue && CO.Value == Info.EvaluatingDeclValue)
22828 return Error(2);
22829
22830 if (!CO)
22831 return false;
22832 IsWithinLifetimeHandler handler{.Info: Info};
22833 return findSubobject(Info, E, Obj: CO, Sub: Val.getLValueDesignator(), handler);
22834}
22835} // namespace
22836