clang 24.0.0git
Compiler.cpp
Go to the documentation of this file.
1//===--- Compiler.cpp - Code generator for expressions ---*- C++ -*-===//
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#include "Compiler.h"
10#include "../ExprConstShared.h"
11#include "ByteCodeEmitter.h"
12#include "Context.h"
13#include "FixedPoint.h"
14#include "Floating.h"
15#include "Function.h"
16#include "InterpShared.h"
17#include "PrimType.h"
18#include "Program.h"
19#include "clang/AST/Attr.h"
21#include "llvm/Support/SaveAndRestore.h"
22
23using namespace clang;
24using namespace clang::interp;
25
26using APSInt = llvm::APSInt;
27
28namespace clang {
29namespace interp {
30
31static std::optional<bool> getBoolValue(const Expr *E) {
32 if (const auto *CE = dyn_cast_if_present<ConstantExpr>(E);
33 CE && CE->hasAPValueResult() &&
34 CE->getResultAPValueKind() == APValue::ValueKind::Int) {
35 return CE->getResultAsAPSInt().getBoolValue();
36 }
37
38 return std::nullopt;
39}
40
41/// Check if \c E has side-effects. This is used to avoid some temporary
42/// variables and is supposed to be a quick check, not exhaustive. That's why
43/// we're not using Expr::HasSideEffects().
44static bool isSideEffectFree(const Expr *E) {
47 return true;
48 if (isa<DeclRefExpr>(E))
49 return true;
50
51 return false;
52}
53
54/// Whether the CheckArraySize op rejects an array with \p NumElems elements.
55static bool exceedsArraySizeLimit(const LangOptions &LangOpts,
56 uint64_t NumElems) {
57 if (NumElems > std::numeric_limits<unsigned>::max())
58 return true;
59 uint64_t Limit = LangOpts.ConstexprStepLimit;
60 return Limit != 0 && NumElems > Limit;
61}
62
63/// Scope chain managing the variable lifetimes.
64template <class Emitter> class VariableScope {
65public:
67 : Ctx(Ctx), Parent(Ctx->VarScope), Kind(Kind) {
68 if (Parent)
69 this->LocalsAlwaysEnabled = Parent->LocalsAlwaysEnabled;
70 Ctx->VarScope = this;
71 }
72
73 virtual ~VariableScope() { Ctx->VarScope = this->Parent; }
74
75 virtual void addLocal(Scope::Local Local) {
76 llvm_unreachable("Shouldn't be called");
77 }
78 /// Like addExtended, but adds to the nearest scope of the given kind.
80 VariableScope *P = this;
81 while (P) {
82 // We found the right scope kind.
83 if (P->Kind == Kind) {
84 P->addLocal(Local);
85 return;
86 }
87 // If we reached the root scope and we're looking for a Block scope,
88 // attach it to the root instead of the current scope.
89 if (!P->Parent && Kind == ScopeKind::Block) {
90 P->addLocal(Local);
91 return;
92 }
93 P = P->Parent;
94 if (!P)
95 break;
96 }
97
98 // Add to this scope.
99 this->addLocal(Local);
100 }
101
102 virtual bool emitDestructors(const Expr *E = nullptr) { return true; }
103 virtual bool destroyLocals(const Expr *E = nullptr) { return true; }
104 VariableScope *getParent() const { return Parent; }
105 ScopeKind getKind() const { return Kind; }
106
107 /// Whether locals added to this scope are enabled by default.
108 /// This is almost always true, except for the two branches
109 /// of a conditional operator.
111
112protected:
113 /// Compiler instance.
115 /// Link to the parent scope.
118};
119
120/// Generic scope for local variables.
121template <class Emitter> class LocalScope : public VariableScope<Emitter> {
122public:
125
126 /// Emit a Destroy op for this scope.
127 ~LocalScope() override {
128 if (!Idx || ExplicitlyDestroyed)
129 return;
130 this->Ctx->emitDestroy(*Idx, SourceInfo{});
132 }
133 /// Explicit destruction of local variables.
134 bool destroyLocals(const Expr *E = nullptr) override {
135 if (!Idx)
136 return true;
137
138 // NB: We are *not* resetting Idx here as to allow multiple
139 // calls to destroyLocals().
140 bool Success = this->emitDestructors(E);
141 this->Ctx->emitDestroy(*Idx, E);
142 ExplicitlyDestroyed = true;
143 return Success;
144 }
145
146 void addLocal(Scope::Local Local) override {
147 if (!Idx) {
148 Idx = static_cast<unsigned>(this->Ctx->Descriptors.size());
149 this->Ctx->Descriptors.emplace_back();
150 }
151
152 Local.EnabledByDefault = this->LocalsAlwaysEnabled;
153 this->Ctx->Descriptors[*Idx].emplace_back(Local);
154 }
155
156 bool emitDestructors(const Expr *E = nullptr) override {
157 if (!Idx)
158 return true;
159
160 // Emit destructor calls for local variables of record
161 // type with a destructor.
162 for (Scope::Local &Local : llvm::reverse(this->Ctx->Descriptors[*Idx])) {
163 if (Local.Desc->hasTrivialDtor())
164 continue;
165
166 if (!Local.EnabledByDefault) {
167 typename Emitter::LabelTy EndLabel = this->Ctx->getLabel();
168 if (!this->Ctx->emitGetLocalEnabled(Local.Offset, E))
169 return false;
170 if (!this->Ctx->jumpFalse(EndLabel, E))
171 return false;
172
173 if (!this->Ctx->emitGetPtrLocal(Local.Offset, E))
174 return false;
175
176 if (!this->Ctx->emitDestructionPop(Local.Desc, Local.Desc->getLoc()))
177 return false;
178
179 this->Ctx->fallthrough(EndLabel);
180 this->Ctx->emitLabel(EndLabel);
181 } else {
182 if (!this->Ctx->emitGetPtrLocal(Local.Offset, E))
183 return false;
184 if (!this->Ctx->emitDestructionPop(Local.Desc, Local.Desc->getLoc()))
185 return false;
186 }
187
189 }
190 return true;
191 }
192
194 if (!Idx)
195 return;
196
197 for (const Scope::Local &Local : this->Ctx->Descriptors[*Idx]) {
199 }
200 }
201
203 if (const auto *OVE =
204 llvm::dyn_cast_if_present<OpaqueValueExpr>(Local.Desc->asExpr())) {
205 this->Ctx->OpaqueExprs.erase(OVE);
206 };
207 }
208
209 /// Index of the scope in the chain.
210 UnsignedOrNone Idx = std::nullopt;
212};
213
214template <class Emitter> class ArrayIndexScope final {
215public:
216 ArrayIndexScope(Compiler<Emitter> *Ctx, uint64_t Index) : Ctx(Ctx) {
217 OldArrayIndex = Ctx->ArrayIndex;
218 Ctx->ArrayIndex = Index;
219 }
220
221 ~ArrayIndexScope() { Ctx->ArrayIndex = OldArrayIndex; }
222
223private:
225 std::optional<uint64_t> OldArrayIndex;
226};
227
228template <class Emitter> class SourceLocScope final {
229public:
230 SourceLocScope(Compiler<Emitter> *Ctx, const Expr *DefaultExpr) : Ctx(Ctx) {
231 assert(DefaultExpr);
232 // We only switch if the current SourceLocDefaultExpr is null.
233 if (!Ctx->SourceLocDefaultExpr) {
234 Enabled = true;
235 Ctx->SourceLocDefaultExpr = DefaultExpr;
236 }
237 }
238
240 if (Enabled)
241 Ctx->SourceLocDefaultExpr = nullptr;
242 }
243
244private:
246 bool Enabled = false;
247};
248
249template <class Emitter> class InitLinkScope final {
250public:
252 Ctx->InitStack.push_back(std::move(Link));
253 }
254
255 ~InitLinkScope() { this->Ctx->InitStack.pop_back(); }
256
257public:
259};
260
261template <class Emitter> class InitStackScope final {
262public:
264 : Ctx(Ctx), OldValue(Ctx->InitStackActive), Active(Active) {
265 // An explicit initializer nested in a default member initializer still
266 // needs the surrounding default initializer's `this` reconstruction.
267 Ctx->InitStackActive = OldValue || Active;
268 if (Active)
269 Ctx->InitStack.push_back(InitLink::DIE());
270 }
271
273 this->Ctx->InitStackActive = OldValue;
274 if (Active)
275 Ctx->InitStack.pop_back();
276 }
277
278private:
280 bool OldValue;
281 bool Active;
282};
283
284/// Scope used to handle temporaries in toplevel variable declarations.
285template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
286public:
288 : LocalScope<Emitter>(Ctx), Scope(Ctx->P),
289 OldInitializingDecl(Ctx->InitializingDecl) {
290 Ctx->InitializingDecl = VD;
291 Ctx->InitStack.push_back(InitLink::Decl(VD));
292 }
293
295 this->Ctx->InitializingDecl = OldInitializingDecl;
296 this->Ctx->InitStack.pop_back();
297 }
298
299private:
301 const VarDecl *OldInitializingDecl;
302};
303
304/// Scope used to handle initialization methods.
305template <class Emitter> class OptionScope final {
306public:
307 /// Root constructor, compiling or discarding primitives.
308 OptionScope(Compiler<Emitter> *Ctx, bool NewDiscardResult,
309 bool NewInitializing, bool NewToLValue)
310 : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult),
311 OldInitializing(Ctx->Initializing), OldToLValue(Ctx->ToLValue) {
312 Ctx->DiscardResult = NewDiscardResult;
313 Ctx->Initializing = NewInitializing;
314 Ctx->ToLValue = NewToLValue;
315 }
316
318 Ctx->DiscardResult = OldDiscardResult;
319 Ctx->Initializing = OldInitializing;
320 Ctx->ToLValue = OldToLValue;
321 }
322
323private:
324 /// Parent context.
326 /// Old discard flag to restore.
327 bool OldDiscardResult;
328 bool OldInitializing;
329 bool OldToLValue;
330};
331
332template <class Emitter>
333bool InitLink::emit(Compiler<Emitter> *Ctx, const Expr *E) const {
334 switch (Kind) {
335 case K_This:
336 return Ctx->emitThis(E);
337 case K_Field:
338 // We're assuming there's a base pointer on the stack already.
339 return Ctx->emitGetPtrFieldPop(Offset, E);
340 case K_Base:
341 return Ctx->emitGetPtrBasePop(Offset, false, E);
342 case K_Temp:
343 return Ctx->emitGetPtrLocal(Offset, E);
344 case K_Decl:
345 return Ctx->visitDeclRef(D, E);
346 case K_Elem:
347 if (!Ctx->emitConstUint32(Offset, E))
348 return false;
349 return Ctx->emitArrayElemPtrPopUint32(E);
350 case K_RVO:
351 return Ctx->emitRVOPtr(E);
352 case K_InitList:
353 return true;
354 default:
355 llvm_unreachable("Unhandled InitLink kind");
356 }
357 return true;
358}
359
360/// Sets the context for break/continue statements.
361template <class Emitter> class LoopScope final {
362public:
366
367 LoopScope(Compiler<Emitter> *Ctx, const Stmt *Name, LabelTy BreakLabel,
368 LabelTy ContinueLabel)
369 : Ctx(Ctx) {
370#ifndef NDEBUG
371 for (const LabelInfo &LI : Ctx->LabelInfoStack)
372 assert(LI.Name != Name);
373#endif
374
375 this->Ctx->LabelInfoStack.emplace_back(Name, BreakLabel, ContinueLabel,
376 /*DefaultLabel=*/std::nullopt,
377 Ctx->VarScope);
378 }
379
380 ~LoopScope() { this->Ctx->LabelInfoStack.pop_back(); }
381
382private:
384};
385
386// Sets the context for a switch scope, mapping labels.
387template <class Emitter> class SwitchScope final {
388public:
393
394 SwitchScope(Compiler<Emitter> *Ctx, const Stmt *Name, CaseMap &&CaseLabels,
395 LabelTy BreakLabel, OptLabelTy DefaultLabel)
396 : Ctx(Ctx), OldCaseLabels(std::move(this->Ctx->CaseLabels)) {
397#ifndef NDEBUG
398 for (const LabelInfo &LI : Ctx->LabelInfoStack)
399 assert(LI.Name != Name);
400#endif
401
402 this->Ctx->CaseLabels = std::move(CaseLabels);
403 this->Ctx->LabelInfoStack.emplace_back(Name, BreakLabel,
404 /*ContinueLabel=*/std::nullopt,
405 DefaultLabel, Ctx->VarScope);
406 }
407
409 this->Ctx->CaseLabels = std::move(OldCaseLabels);
410 this->Ctx->LabelInfoStack.pop_back();
411 }
412
413private:
415 CaseMap OldCaseLabels;
416};
417
418/// When generating code for e.g. implicit field initializers in constructors,
419/// we don't have anything to point to in case the initializer causes an error.
420/// In that case, we need to disable location tracking for the initializer so
421/// we later point to the call range instead.
422template <class Emitter> class LocOverrideScope final {
423public:
425 bool Enabled = true)
426 : Ctx(Ctx), OldFlag(Ctx->LocOverride), Enabled(Enabled) {
427
428 if (Enabled)
429 Ctx->LocOverride = NewValue;
430 }
431
433 if (Enabled)
434 Ctx->LocOverride = OldFlag;
435 }
436
437private:
439 std::optional<SourceInfo> OldFlag;
440 bool Enabled;
441};
442
443} // namespace interp
444} // namespace clang
445
446template <class Emitter>
448 const Expr *SubExpr = E->getSubExpr();
449
450 if (DiscardResult)
451 return this->delegate(SubExpr);
452
453 switch (E->getCastKind()) {
454 case CK_LValueToRValue: {
455 // This *could* work I guess, but the current interpreter rejects (via
456 // checkLiteralType).
457 if (!Ctx.getLangOpts().HLSL && E->getType()->isConstantMatrixType())
458 return false;
459
460 if (ToLValue && E->getType()->isPointerType()) {
461 assert(!DiscardResult);
462 if (!this->visit(SubExpr))
463 return false;
464 return this->emitLoadPopL(E);
465 }
466
467 if (SubExpr->getType().isVolatileQualified())
468 return this->emitInvalidCast(CastKind::Volatile, /*Fatal=*/true, E);
469
470 OptPrimType SubExprT = classify(SubExpr->getType());
471 // Try to load the value directly. This is purely a performance
472 // optimization.
473 if (SubExprT) {
474 if (const auto *DRE = dyn_cast<DeclRefExpr>(SubExpr)) {
475 const ValueDecl *D = DRE->getDecl();
476 bool IsReference = D->getType()->isReferenceType();
477
478 if (!IsReference) {
480 if (auto GlobalIndex = P.getGlobal(D))
481 return this->emitGetGlobal(*SubExprT, *GlobalIndex, E);
482 } else if (auto It = Locals.find(D); It != Locals.end()) {
483 return this->emitGetLocal(*SubExprT, It->second.Offset, E);
484 } else if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
485 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
486 return this->emitGetParam(*SubExprT, It->second.Index, E);
487 }
488 }
489 }
490 }
491 }
492
493 // Prepare storage for the result.
494 if (!Initializing && !SubExprT) {
495 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
496 if (!LocalIndex)
497 return false;
498 if (!this->emitGetPtrLocal(*LocalIndex, E))
499 return false;
500 }
501
502 if (!this->visit(SubExpr))
503 return false;
504
505 if (SubExprT)
506 return this->emitLoadPop(*SubExprT, E);
507
508 // If the subexpr type is not primitive, we need to perform a copy here.
509 // This happens for example in C when dereferencing a pointer of struct
510 // type.
511 return this->emitMemcpy(E);
512 }
513
514 case CK_DerivedToBaseMemberPointer: {
515 if (E->containsErrors())
516 return false;
517 assert(classifyPrim(E) == PT_MemberPtr);
518 assert(classifyPrim(SubExpr) == PT_MemberPtr);
519
520 if (!this->delegate(SubExpr))
521 return false;
522
523 const CXXRecordDecl *CurDecl = SubExpr->getType()
525 ->getMostRecentCXXRecordDecl();
526 for (const CXXBaseSpecifier *B : E->path()) {
527 const CXXRecordDecl *ToDecl = B->getType()->castAsCXXRecordDecl();
528 unsigned DerivedOffset = Ctx.collectBaseOffset(ToDecl, CurDecl);
529
530 if (!this->emitCastMemberPtrBasePop(DerivedOffset, ToDecl, E))
531 return false;
532 CurDecl = ToDecl;
533 }
534
535 return true;
536 }
537
538 case CK_BaseToDerivedMemberPointer: {
539 if (E->containsErrors())
540 return false;
541 assert(classifyPrim(E) == PT_MemberPtr);
542 assert(classifyPrim(SubExpr) == PT_MemberPtr);
543
544 if (!this->delegate(SubExpr))
545 return false;
546
547 const CXXRecordDecl *CurDecl = SubExpr->getType()
549 ->getMostRecentCXXRecordDecl();
550 // Base-to-derived member pointer casts store the path in derived-to-base
551 // order, so iterate backwards. The CXXBaseSpecifier also provides us with
552 // the wrong end of the derived->base arc, so stagger the path by one class.
553 typedef std::reverse_iterator<CastExpr::path_const_iterator> ReverseIter;
554 for (ReverseIter PathI(E->path_end() - 1), PathE(E->path_begin());
555 PathI != PathE; ++PathI) {
556 const CXXRecordDecl *ToDecl = (*PathI)->getType()->castAsCXXRecordDecl();
557 unsigned DerivedOffset = Ctx.collectBaseOffset(CurDecl, ToDecl);
558
559 if (!this->emitCastMemberPtrDerivedPop(-DerivedOffset, ToDecl, E))
560 return false;
561 CurDecl = ToDecl;
562 }
563
564 const CXXRecordDecl *ToDecl =
565 E->getType()->castAs<MemberPointerType>()->getMostRecentCXXRecordDecl();
566 assert(ToDecl != CurDecl);
567 unsigned DerivedOffset = Ctx.collectBaseOffset(CurDecl, ToDecl);
568
569 if (!this->emitCastMemberPtrDerivedPop(-DerivedOffset, ToDecl, E))
570 return false;
571
572 return true;
573 }
574
575 case CK_UncheckedDerivedToBase:
576 case CK_DerivedToBase: {
577 if (!this->delegate(SubExpr))
578 return false;
579
580 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
581 if (const auto *PT = dyn_cast<PointerType>(Ty))
582 return PT->getPointeeType()->castAsCXXRecordDecl();
583 return Ty->castAsCXXRecordDecl();
584 };
585
586 // FIXME: We can express a series of non-virtual casts as a single
587 // GetPtrBasePop op.
588 QualType CurType = SubExpr->getType();
589 for (const CXXBaseSpecifier *B : E->path()) {
590 if (B->isVirtual()) {
591 if (!this->emitGetPtrVirtBasePop(extractRecordDecl(B->getType()), E))
592 return false;
593 CurType = B->getType();
594 } else {
595 unsigned DerivedOffset = collectBaseOffset(B->getType(), CurType);
596 if (!this->emitGetPtrBasePop(
597 DerivedOffset, /*NullOK=*/E->getType()->isPointerType(), E))
598 return false;
599 CurType = B->getType();
600 }
601 }
602
603 return true;
604 }
605
606 case CK_BaseToDerived: {
607 if (!this->delegate(SubExpr))
608 return false;
609
610 auto unwrapPointerType = [](const Type *T) -> const Type * {
611 if (T->isPointerOrReferenceType())
612 return T->getPointeeType().getTypePtr();
613 return T;
614 };
615
616 bool NullOK = false;
617 const Type *FinalType = E->getType().getTypePtr();
618 if (FinalType->isPointerOrReferenceType()) {
619 NullOK = true;
620 FinalType = FinalType->getPointeeType().getTypePtr();
621 }
622
623 for (const CXXBaseSpecifier *B :
624 llvm::drop_begin(llvm::reverse(E->path()))) {
625 const Type *TargetType = unwrapPointerType(B->getType().getTypePtr());
626 if (!this->emitGetPtrDerivedPop(NullOK, TargetType, FinalType, E))
627 return false;
628 }
629
630 return this->emitGetPtrDerivedPop(NullOK, FinalType, FinalType, E);
631 }
632
633 case CK_FloatingCast: {
634 // HLSL uses CK_FloatingCast to cast between vectors.
635 if (E->getType()->isVectorType())
636 return this->emitVectorConversion(E->getSubExpr(), E);
637 if (!SubExpr->getType()->isFloatingType() ||
638 !E->getType()->isFloatingType())
639 return false;
640 if (!this->visit(SubExpr))
641 return false;
642 const auto *TargetSemantics = &Ctx.getFloatSemantics(E->getType());
643 return this->emitCastFP(TargetSemantics, getRoundingMode(E), E);
644 }
645
646 case CK_IntegralToFloating: {
647 if (E->getType()->isVectorType())
648 return this->emitVectorConversion(E->getSubExpr(), E);
649 if (!E->getType()->isRealFloatingType())
650 return false;
651 if (!this->visit(SubExpr))
652 return false;
653 const auto *TargetSemantics = &Ctx.getFloatSemantics(E->getType());
654 return this->emitCastIntegralFloating(classifyPrim(SubExpr),
655 TargetSemantics, getFPOptions(E), E);
656 }
657
658 case CK_FloatingToBoolean: {
659 if (E->getType()->isVectorType())
660 return this->emitVectorConversion(E->getSubExpr(), E);
661 if (!SubExpr->getType()->isRealFloatingType() ||
663 return false;
664 if (const auto *FL = dyn_cast<FloatingLiteral>(SubExpr))
665 return this->emitConstBool(FL->getValue().isNonZero(), E);
666 if (!this->visit(SubExpr))
667 return false;
668 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
669 }
670
671 case CK_FloatingToIntegral: {
672 if (E->getType()->isVectorType())
673 return this->emitVectorConversion(E->getSubExpr(), E);
675 return false;
676 if (!this->visit(SubExpr))
677 return false;
678 PrimType ToT = classifyPrim(E);
679 if (ToT == PT_IntAP)
680 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(E->getType()),
681 getFPOptions(E), E);
682 if (ToT == PT_IntAPS)
683 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(E->getType()),
684 getFPOptions(E), E);
685
686 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
687 }
688
689 case CK_NullToPointer:
690 case CK_NullToMemberPointer: {
691 if (!this->discard(SubExpr))
692 return false;
693 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
694 return this->emitNull(classifyPrim(E->getType()), Val,
695 E->getType().getTypePtr(), E);
696 }
697
698 case CK_PointerToIntegral: {
699 if (!this->visit(SubExpr))
700 return false;
701
702 // If SubExpr doesn't result in a pointer, make it one.
703 if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) {
704 assert(isPtrType(FromT));
705 if (!this->emitDecayPtr(FromT, PT_Ptr, E))
706 return false;
707 }
708
710 if (T == PT_IntAP)
711 return this->emitCastPointerIntegralAP(Ctx.getBitWidth(E->getType()), E);
712 if (T == PT_IntAPS)
713 return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(E->getType()), E);
714 return this->emitCastPointerIntegral(T, E);
715 }
716
717 case CK_ArrayToPointerDecay: {
718 if (!this->visit(SubExpr))
719 return false;
720 return this->emitArrayDecay(E);
721 }
722
723 case CK_IntegralToPointer: {
724 QualType IntType = SubExpr->getType();
725 assert(IntType->isIntegralOrEnumerationType());
726 if (!this->visit(SubExpr))
727 return false;
728 // FIXME: I think the discard is wrong since the int->ptr cast might cause a
729 // diagnostic.
730 PrimType T = classifyPrim(IntType);
731 if (!this->emitGetIntPtr(T, E->getType().getTypePtr(), E))
732 return false;
733
734 QualType PtrType = E->getType();
735 PrimType DestPtrT = classifyPrim(PtrType);
736 if (DestPtrT == PT_Ptr)
737 return true;
738
739 // In case we're converting the integer to a non-Pointer.
740 return this->emitDecayPtr(PT_Ptr, DestPtrT, E);
741 }
742
743 case CK_AtomicToNonAtomic:
744 case CK_ConstructorConversion:
745 case CK_FunctionToPointerDecay:
746 case CK_NonAtomicToAtomic:
747 case CK_NoOp:
748 case CK_UserDefinedConversion:
749 case CK_CPointerToObjCPointerCast:
750 return this->delegate(SubExpr);
751
752 case CK_AddressSpaceConversion: {
753 if (E->containsErrors())
754 return false;
755
756 if (!this->visit(SubExpr))
757 return false;
758
759 uint64_t Val;
760 if (E->getType()->isPointerType())
761 Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
762 else
763 Val = 0;
764
765 if (!this->emitCastAddressSpace(Val, E->getType().getTypePtr(), E))
766 return false;
767 if (DiscardResult)
768 return this->emitPopPtr(E);
769 return true;
770 }
771
772 case CK_BitCast: {
773 if (E->containsErrors())
774 return false;
775 QualType ETy = E->getType();
776 // Reject bitcasts to atomic types.
777 if (ETy->isAtomicType()) {
778 if (!this->discard(SubExpr))
779 return false;
780 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);
781 }
782 QualType SubExprTy = SubExpr->getType();
783 OptPrimType FromT = classify(SubExprTy);
784 // Casts between vectors and integers of the same same size.
785 if (ETy->isVectorType() || SubExprTy->isVectorType())
786 return this->emitBuiltinBitCast(E);
787
788 OptPrimType ToT = classify(E->getType());
789 if (!FromT || !ToT)
790 return false;
791
792 assert(isPtrType(*FromT));
793 assert(isPtrType(*ToT));
794 bool SrcIsVoidPtr = SubExprTy->isVoidPointerType();
795 if (FromT == ToT) {
796 if (E->getType()->isVoidPointerType() &&
797 !SubExprTy->isFunctionPointerType()) {
798 return this->delegate(SubExpr);
799 }
800
801 if (!this->visit(SubExpr))
802 return false;
803 if (!this->emitCheckBitCast(ETy->getPointeeType().getTypePtr(),
804 SrcIsVoidPtr, E))
805 return false;
806
807 if (E->getType()->isFunctionPointerType() ||
808 SubExprTy->isFunctionPointerType()) {
809 return this->emitFnPtrCast(E);
810 }
811 if (FromT == PT_Ptr)
812 return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(),
813 E->getType().getTypePtr(), E);
814 return true;
815 }
816
817 if (!this->visit(SubExpr))
818 return false;
819 return this->emitDecayPtr(*FromT, *ToT, E);
820 }
821 case CK_IntegralToBoolean:
822 case CK_FixedPointToBoolean: {
823 if (E->getType()->isVectorType())
824 return this->emitVectorConversion(E->getSubExpr(), E);
825 // HLSL uses this to cast to one-element vectors.
826 OptPrimType FromT = classify(SubExpr->getType());
827 if (!FromT)
828 return false;
829
830 if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr))
831 return this->emitConst(IL->getValue(), E);
832 if (!this->visit(SubExpr))
833 return false;
834 return this->emitCast(*FromT, classifyPrim(E), E);
835 }
836
837 case CK_IntegralCast:
838 if (E->getType()->isVectorType())
839 return this->emitVectorConversion(E->getSubExpr(), E);
840 [[fallthrough]];
841 case CK_BooleanToSignedIntegral: {
842 OptPrimType FromT = classify(SubExpr->getType());
843 OptPrimType ToT = classify(E->getType());
844 if (!FromT || !ToT)
845 return false;
846
847 // Try to emit a casted known constant value directly.
848 if (const auto *IL = dyn_cast<IntegerLiteral>(SubExpr)) {
849 if (ToT != PT_IntAP && ToT != PT_IntAPS && FromT != PT_IntAP &&
850 FromT != PT_IntAPS && !E->getType()->isEnumeralType())
851 return this->emitConst(APSInt(IL->getValue(), !isSignedType(*FromT)),
852 E);
853 if (!this->emitConst(IL->getValue(), SubExpr))
854 return false;
855 } else {
856 if (!this->visit(SubExpr))
857 return false;
858 }
859
860 // Possibly diagnose casts to enum types if the target type does not
861 // have a fixed size.
862 if (Ctx.getLangOpts().CPlusPlus && E->getType()->isEnumeralType()) {
863 const auto *ED = E->getType()->castAsEnumDecl();
864 if (!ED->isFixed()) {
865 if (!this->emitCheckEnumValue(*FromT, ED, E))
866 return false;
867 }
868 }
869
870 if (ToT == PT_IntAP) {
871 if (!this->emitCastAP(*FromT, Ctx.getBitWidth(E->getType()), E))
872 return false;
873 } else if (ToT == PT_IntAPS) {
874 if (!this->emitCastAPS(*FromT, Ctx.getBitWidth(E->getType()), E))
875 return false;
876 } else {
877 if (FromT == ToT)
878 return true;
879 if (!this->emitCast(*FromT, *ToT, E))
880 return false;
881 }
882 if (E->getCastKind() == CK_BooleanToSignedIntegral)
883 return this->emitNeg(*ToT, E);
884 return true;
885 }
886
887 case CK_PointerToBoolean:
888 if (!this->visit(SubExpr))
889 return false;
890 return this->emitIsNonNullPtr(E);
891
892 case CK_MemberPointerToBoolean:
893 if (!this->visit(SubExpr))
894 return false;
895 return this->emitIsNonNullMemberPtr(E);
896
897 case CK_IntegralComplexToBoolean:
898 case CK_FloatingComplexToBoolean: {
899 if (!this->visit(SubExpr))
900 return false;
901 return this->emitComplexBoolCast(SubExpr);
902 }
903
904 case CK_IntegralComplexToReal:
905 case CK_FloatingComplexToReal:
906 return this->emitComplexReal(SubExpr);
907
908 case CK_IntegralRealToComplex:
909 case CK_FloatingRealToComplex: {
910 // We're creating a complex value here, so we need to
911 // allocate storage for it.
912 if (!Initializing) {
913 UnsignedOrNone LocalIndex = allocateTemporary(E);
914 if (!LocalIndex)
915 return false;
916 if (!this->emitGetPtrLocal(*LocalIndex, E))
917 return false;
918 }
919
920 PrimType T = classifyPrim(SubExpr->getType());
921 // Init the complex value to {SubExpr, 0}.
922 if (!this->visitArrayElemInit(0, SubExpr, T))
923 return false;
924 // Zero-init the second element.
925 if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
926 return false;
927 return this->emitInitElem(T, 1, SubExpr);
928 }
929
930 case CK_IntegralComplexCast:
931 case CK_FloatingComplexCast:
932 case CK_IntegralComplexToFloatingComplex:
933 case CK_FloatingComplexToIntegralComplex: {
934 assert(E->getType()->isAnyComplexType());
935 assert(SubExpr->getType()->isAnyComplexType());
936 if (!Initializing) {
937 UnsignedOrNone LocalIndex = allocateLocal(E);
938 if (!LocalIndex)
939 return false;
940 if (!this->emitGetPtrLocal(*LocalIndex, E))
941 return false;
942 }
943
944 // Location for the SubExpr.
945 // Since SubExpr is of complex type, visiting it results in a pointer
946 // anyway, so we just create a temporary pointer variable.
947 unsigned SubExprOffset =
948 allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
949 if (!this->visit(SubExpr))
950 return false;
951 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
952 return false;
953
954 PrimType SourceElemT = classifyComplexElementType(SubExpr->getType());
955 QualType DestElemType =
956 E->getType()->getAs<ComplexType>()->getElementType();
957 PrimType DestElemT = classifyPrim(DestElemType);
958 // Cast both elements individually.
959 for (unsigned I = 0; I != 2; ++I) {
960 if (!this->emitGetLocal(PT_Ptr, SubExprOffset, E))
961 return false;
962 if (!this->emitArrayElemPop(SourceElemT, I, E))
963 return false;
964
965 // Do the cast.
966 if (!this->emitPrimCast(SourceElemT, DestElemT, DestElemType, E))
967 return false;
968
969 // Save the value.
970 if (!this->emitInitElem(DestElemT, I, E))
971 return false;
972 }
973 return true;
974 }
975
976 case CK_VectorSplat: {
977 assert(!canClassify(E->getType()));
978 assert(E->getType()->isVectorType());
979
980 if (!canClassify(SubExpr->getType()))
981 return false;
982
983 if (!Initializing) {
984 UnsignedOrNone LocalIndex = allocateLocal(E);
985 if (!LocalIndex)
986 return false;
987 if (!this->emitGetPtrLocal(*LocalIndex, E))
988 return false;
989 }
990
991 const auto *VT = E->getType()->getAs<VectorType>();
992 PrimType ElemT = classifyPrim(SubExpr->getType());
993 unsigned ElemOffset =
994 allocateLocalPrimitive(SubExpr, ElemT, /*IsConst=*/true);
995
996 // Prepare a local variable for the scalar value.
997 if (!this->visit(SubExpr))
998 return false;
999 if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, E))
1000 return false;
1001
1002 if (!this->emitSetLocal(ElemT, ElemOffset, E))
1003 return false;
1004
1005 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
1006 if (!this->emitGetLocal(ElemT, ElemOffset, E))
1007 return false;
1008 if (!this->emitInitElem(ElemT, I, E))
1009 return false;
1010 }
1011
1012 return true;
1013 }
1014
1015 case CK_HLSLVectorTruncation: {
1016 assert(SubExpr->getType()->isVectorType());
1017 if (OptPrimType ResultT = classify(E)) {
1018 assert(!DiscardResult);
1019 // Result must be either a float or integer. Take the first element.
1020 if (!this->visit(SubExpr))
1021 return false;
1022 return this->emitArrayElemPop(*ResultT, 0, E);
1023 }
1024 // Otherwise, this truncates from one vector type to another.
1025 assert(E->getType()->isVectorType());
1026
1027 if (!Initializing) {
1028 UnsignedOrNone LocalIndex = allocateTemporary(E);
1029 if (!LocalIndex)
1030 return false;
1031 if (!this->emitGetPtrLocal(*LocalIndex, E))
1032 return false;
1033 }
1034 unsigned ToSize = E->getType()->getAs<VectorType>()->getNumElements();
1035 assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize);
1036 if (!this->visit(SubExpr))
1037 return false;
1038 return this->emitCopyArray(classifyVectorElementType(E->getType()), 0, 0,
1039 ToSize, E);
1040 };
1041
1042 case CK_IntegralToFixedPoint: {
1043 if (!this->visit(SubExpr))
1044 return false;
1045
1046 auto Sem =
1047 Ctx.getASTContext().getFixedPointSemantics(E->getType()).toOpaqueInt();
1048 if (!this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()), Sem,
1049 E))
1050 return false;
1051 if (DiscardResult)
1052 return this->emitPopFixedPoint(E);
1053 return true;
1054 }
1055 case CK_FloatingToFixedPoint: {
1056 if (!this->visit(SubExpr))
1057 return false;
1058
1059 auto Sem =
1060 Ctx.getASTContext().getFixedPointSemantics(E->getType()).toOpaqueInt();
1061 if (!this->emitCastFloatingFixedPoint(Sem, E))
1062 return false;
1063 if (DiscardResult)
1064 return this->emitPopFixedPoint(E);
1065 return true;
1066 }
1067 case CK_FixedPointToFloating: {
1068 if (!this->visit(SubExpr))
1069 return false;
1070 const auto *TargetSemantics = &Ctx.getFloatSemantics(E->getType());
1071 if (!this->emitCastFixedPointFloating(TargetSemantics, E))
1072 return false;
1073 if (DiscardResult)
1074 return this->emitPopFloat(E);
1075 return true;
1076 }
1077 case CK_FixedPointToIntegral: {
1078 if (!this->visit(SubExpr))
1079 return false;
1080 PrimType IntegralT = classifyPrim(E->getType());
1081 if (!this->emitCastFixedPointIntegral(IntegralT, E))
1082 return false;
1083 if (DiscardResult)
1084 return this->emitPop(IntegralT, E);
1085 return true;
1086 }
1087 case CK_FixedPointCast: {
1088 if (!this->visit(SubExpr))
1089 return false;
1090 auto Sem =
1091 Ctx.getASTContext().getFixedPointSemantics(E->getType()).toOpaqueInt();
1092 if (!this->emitCastFixedPoint(Sem, E))
1093 return false;
1094 if (DiscardResult)
1095 return this->emitPopFixedPoint(E);
1096 return true;
1097 }
1098
1099 case CK_ToVoid:
1100 return discard(SubExpr);
1101
1102 case CK_Dynamic:
1103 llvm_unreachable("CXXDynamicCastExpr has its own function");
1104
1105 case CK_LValueBitCast:
1106 if (!this->emitInvalidCast(CastKind::ReinterpretLike, /*Fatal=*/false, E))
1107 return false;
1108 return this->delegate(SubExpr);
1109
1110 case CK_HLSLArrayRValue: {
1111 // Non-decaying array rvalue cast - creates an rvalue copy of an lvalue
1112 // array, similar to LValueToRValue for composite types.
1113 if (!Initializing) {
1114 UnsignedOrNone LocalIndex = allocateLocal(E);
1115 if (!LocalIndex)
1116 return false;
1117 if (!this->emitGetPtrLocal(*LocalIndex, E))
1118 return false;
1119 }
1120 if (!this->visit(SubExpr))
1121 return false;
1122 return this->emitMemcpy(E);
1123 }
1124
1125 case CK_HLSLMatrixTruncation: {
1126 assert(SubExpr->getType()->isConstantMatrixType());
1127 if (OptPrimType ResultT = classify(E)) {
1128 assert(!DiscardResult);
1129 // Result must be either a float or integer. Take the first element.
1130 if (!this->visit(SubExpr))
1131 return false;
1132 return this->emitArrayElemPop(*ResultT, 0, E);
1133 }
1134 // Otherwise, this truncates to a a constant matrix type.
1135 assert(E->getType()->isConstantMatrixType());
1136
1137 if (!Initializing) {
1138 UnsignedOrNone LocalIndex = allocateTemporary(E);
1139 if (!LocalIndex)
1140 return false;
1141 if (!this->emitGetPtrLocal(*LocalIndex, E))
1142 return false;
1143 }
1144 unsigned ToSize =
1145 E->getType()->getAs<ConstantMatrixType>()->getNumElementsFlattened();
1146 if (!this->visit(SubExpr))
1147 return false;
1148 return this->emitCopyArray(classifyMatrixElementType(SubExpr->getType()), 0,
1149 0, ToSize, E);
1150 }
1151
1152 case CK_HLSLAggregateSplatCast: {
1153 // Aggregate splat cast: convert a scalar value to one of an aggregate type
1154 // by replicating and casting the scalar to every element of the destination
1155 // aggregate (vector, matrix, array, or struct).
1156 assert(canClassify(SubExpr->getType()));
1157
1158 if (!Initializing) {
1159 UnsignedOrNone LocalIndex = allocateLocal(E);
1160 if (!LocalIndex)
1161 return false;
1162 if (!this->emitGetPtrLocal(*LocalIndex, E))
1163 return false;
1164 }
1165
1166 // The scalar to be splatted is stored in a local to be repeatedly loaded
1167 // once for every scalar element of the destination.
1168 PrimType SrcElemT = classifyPrim(SubExpr->getType());
1169 unsigned SrcOffset =
1170 allocateLocalPrimitive(SubExpr, SrcElemT, /*IsConst=*/true);
1171
1172 if (!this->visit(SubExpr))
1173 return false;
1174 if (!this->emitSetLocal(SrcElemT, SrcOffset, E))
1175 return false;
1176
1177 // Recursively splat the scalar into every element of the destination.
1178 return emitHLSLAggregateSplat(SrcElemT, SrcOffset, E->getType(), E);
1179 }
1180
1181 case CK_HLSLElementwiseCast: {
1182 // Elementwise cast: flatten the elements of one aggregate source type and
1183 // store to a destination scalar or aggregate type of the same or fewer
1184 // number of elements. Casts are inserted element-wise to convert each
1185 // source scalar element to its corresponding destination scalar element.
1186 QualType SrcType = SubExpr->getType();
1187 QualType DestType = E->getType();
1188
1189 if (OptPrimType DestT = classify(DestType)) {
1190 // When the destination is a scalar, we only need the first scalar
1191 // element of the source.
1192 unsigned SrcPtrOffset =
1193 allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
1194 if (!this->visit(SubExpr))
1195 return false;
1196 if (!this->emitSetLocal(PT_Ptr, SrcPtrOffset, E))
1197 return false;
1198
1200 if (!emitHLSLFlattenAggregate(SrcType, SrcPtrOffset, Elements, 1, E))
1201 return false;
1202 if (Elements.empty())
1203 return false;
1204
1205 const HLSLFlatElement &Src = Elements[0];
1206 if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E))
1207 return false;
1208 return this->emitPrimCast(Src.Type, *DestT, DestType, E);
1209 }
1210
1211 if (!Initializing) {
1212 UnsignedOrNone LocalIndex = allocateLocal(E);
1213 if (!LocalIndex)
1214 return false;
1215 if (!this->emitGetPtrLocal(*LocalIndex, E))
1216 return false;
1217 }
1218
1219 unsigned SrcOffset =
1220 allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
1221 if (!this->visit(SubExpr))
1222 return false;
1223 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
1224 return false;
1225
1226 // Only flatten as many source elements as the destination requires.
1227 unsigned ElemCount = countHLSLFlatElements(DestType);
1228
1230 Elements.reserve(ElemCount);
1231 if (!emitHLSLFlattenAggregate(SrcType, SrcOffset, Elements, ElemCount, E))
1232 return false;
1233
1234 // Sema is expected to reject an elementwise cast whose source has fewer
1235 // scalar elements than the destination.
1236 assert(Elements.size() == ElemCount &&
1237 "Source type has fewer scalar elements than the destination type");
1238
1239 return emitHLSLConstructAggregate(DestType, Elements, E);
1240 }
1241
1242 case CK_ToUnion: {
1243 const FieldDecl *UnionField = E->getTargetUnionField();
1244 const Record *R = this->getRecord(E->getType());
1245 assert(R);
1246 const Record::Field *RF = R->getField(UnionField);
1247
1248 if (OptPrimType PT = RF->T) {
1249 if (!this->visit(SubExpr))
1250 return false;
1251 if (RF->isBitField())
1252 return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(),
1253 E);
1254 return this->emitInitFieldActivate(*PT, RF->Offset, E);
1255 }
1256
1257 if (!this->emitGetPtrField(RF->Offset, E))
1258 return false;
1259 if (!this->emitActivate(E))
1260 return false;
1261 return this->visitInitializerPop(SubExpr);
1262 }
1263
1264 default:
1265 return this->emitInvalid(E);
1266 }
1267 llvm_unreachable("Unhandled clang::CastKind enum");
1268}
1269
1270template <class Emitter>
1272 return this->emitBuiltinBitCast(E);
1273}
1274
1275template <class Emitter>
1277 if (DiscardResult)
1278 return true;
1279
1280 return this->emitConst(LE->getValue(), LE);
1281}
1282
1283template <class Emitter>
1285 if (DiscardResult)
1286 return true;
1287
1288 APFloat F = E->getValue();
1289 return this->emitFloat(F, E);
1290}
1291
1292template <class Emitter>
1294 assert(E->getType()->isAnyComplexType());
1295 if (DiscardResult)
1296 return true;
1297
1298 if (!Initializing) {
1299 UnsignedOrNone LocalIndex = allocateTemporary(E);
1300 if (!LocalIndex)
1301 return false;
1302 if (!this->emitGetPtrLocal(*LocalIndex, E))
1303 return false;
1304 }
1305
1306 const Expr *SubExpr = E->getSubExpr();
1307 PrimType SubExprT = classifyPrim(SubExpr->getType());
1308
1309 if (!this->visitZeroInitializer(SubExprT, SubExpr->getType(), SubExpr))
1310 return false;
1311 if (!this->emitInitElem(SubExprT, 0, SubExpr))
1312 return false;
1313 if (!this->visitArrayElemInit(1, SubExpr, SubExprT))
1314 return false;
1315 return this->emitFinishInit(E);
1316}
1317
1318template <class Emitter>
1320 assert(E->getType()->isFixedPointType());
1321 assert(classifyPrim(E) == PT_FixedPoint);
1322
1323 if (DiscardResult)
1324 return true;
1325
1326 auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
1327 APInt Value = E->getValue();
1328 return this->emitConstFixedPoint(FixedPoint(Value, Sem), E);
1329}
1330
1331template <class Emitter>
1333 return this->delegate(E->getSubExpr());
1334}
1335
1336template <class Emitter>
1338 // Need short-circuiting for these.
1339 if (E->isLogicalOp() && !E->getType()->isVectorType())
1340 return this->VisitLogicalBinOp(E);
1341
1342 const Expr *LHS = E->getLHS();
1343 const Expr *RHS = E->getRHS();
1344
1345 // Handle comma operators. Just discard the LHS
1346 // and delegate to RHS.
1347 if (E->isCommaOp()) {
1348 if (!this->discard(LHS))
1349 return false;
1350 if (RHS->getType()->isVoidType())
1351 return this->discard(RHS);
1352
1353 return this->delegate(RHS);
1354 }
1355
1356 if (E->getType()->isAnyComplexType())
1357 return this->VisitComplexBinOp(E);
1358 if (E->getType()->isVectorType())
1359 return this->VisitVectorBinOp(E);
1360 if ((LHS->getType()->isAnyComplexType() ||
1361 RHS->getType()->isAnyComplexType()) &&
1362 E->isComparisonOp())
1363 return this->emitComplexComparison(LHS, RHS, E);
1364 if (LHS->getType()->isFixedPointType() || RHS->getType()->isFixedPointType())
1365 return this->VisitFixedPointBinOp(E);
1366
1367 if (E->isPtrMemOp()) {
1368 if (E->containsErrors())
1369 return false;
1370
1371 if (!this->visit(LHS))
1372 return false;
1373
1374 if (!this->visit(RHS))
1375 return false;
1376
1377 if (!this->emitToMemberPtr(E))
1378 return false;
1379
1380 if (classifyPrim(E) == PT_MemberPtr)
1381 return true;
1382
1383 if (!this->emitCastMemberPtrPtr(E))
1384 return false;
1385 return DiscardResult ? this->emitPopPtr(E) : true;
1386 }
1387
1388 // Typecheck the args.
1389 OptPrimType LT = classify(LHS);
1390 OptPrimType RT = classify(RHS);
1391 OptPrimType T = classify(E->getType());
1392
1393 // Special case for C++'s three-way/spaceship operator <=>, which
1394 // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
1395 // have a PrimType).
1396 if (!T && E->getOpcode() == BO_Cmp) {
1397 if (DiscardResult)
1398 return true;
1399 const ComparisonCategoryInfo *CmpInfo =
1400 Ctx.getASTContext().CompCategories.lookupInfoForType(E->getType());
1401 assert(CmpInfo);
1402
1403 // We need a temporary variable holding our return value.
1404 if (!Initializing) {
1405 UnsignedOrNone ResultIndex = this->allocateLocal(E);
1406 if (!this->emitGetPtrLocal(*ResultIndex, E))
1407 return false;
1408 }
1409
1410 if (!visit(LHS) || !visit(RHS))
1411 return false;
1412
1413 return this->emitCMP3(*LT, CmpInfo, E);
1414 }
1415
1416 if (!LT || !RT || !T)
1417 return false;
1418
1419 // Pointer arithmetic special case.
1420 if (E->getOpcode() == BO_Add || E->getOpcode() == BO_Sub) {
1421 if (isPtrType(*T) || (isPtrType(*LT) && isPtrType(*RT)))
1422 return this->VisitPointerArithBinOp(E);
1423 }
1424
1425 if (E->getOpcode() == BO_Assign)
1426 return this->visitAssignment(LHS, RHS, E);
1427
1428 if (!visit(LHS) || !visit(RHS))
1429 return false;
1430
1431 // For languages such as C, cast the result of one
1432 // of our comparision opcodes to T (which is usually int).
1433 auto MaybeCastToBool = [this, T, E](bool Result) {
1434 if (!Result)
1435 return false;
1436 if (DiscardResult)
1437 return this->emitPopBool(E);
1438 if (T != PT_Bool)
1439 return this->emitCast(PT_Bool, *T, E);
1440 return true;
1441 };
1442
1443 auto Discard = [this, T, E](bool Result) {
1444 if (!Result)
1445 return false;
1446 return DiscardResult ? this->emitPop(*T, E) : true;
1447 };
1448
1449 switch (E->getOpcode()) {
1450 case BO_EQ:
1451 return MaybeCastToBool(this->emitEQ(*LT, E));
1452 case BO_NE:
1453 return MaybeCastToBool(this->emitNE(*LT, E));
1454 case BO_LT:
1455 return MaybeCastToBool(this->emitLT(*LT, E));
1456 case BO_LE:
1457 return MaybeCastToBool(this->emitLE(*LT, E));
1458 case BO_GT:
1459 return MaybeCastToBool(this->emitGT(*LT, E));
1460 case BO_GE:
1461 return MaybeCastToBool(this->emitGE(*LT, E));
1462 case BO_Sub:
1463 if (E->getType()->isFloatingType())
1464 return Discard(this->emitSubf(getFPOptions(E), E));
1465 return Discard(this->emitSub(*T, E));
1466 case BO_Add:
1467 if (E->getType()->isFloatingType())
1468 return Discard(this->emitAddf(getFPOptions(E), E));
1469 return Discard(this->emitAdd(*T, E));
1470 case BO_Mul:
1471 if (E->getType()->isFloatingType())
1472 return Discard(this->emitMulf(getFPOptions(E), E));
1473 return Discard(this->emitMul(*T, E));
1474 case BO_Rem:
1475 return Discard(this->emitRem(*T, E));
1476 case BO_Div:
1477 if (E->getType()->isFloatingType())
1478 return Discard(this->emitDivf(getFPOptions(E), E));
1479 return Discard(this->emitDiv(*T, E));
1480 case BO_And:
1481 return Discard(this->emitBitAnd(*T, E));
1482 case BO_Or:
1483 return Discard(this->emitBitOr(*T, E));
1484 case BO_Shl:
1485 return Discard(this->emitShl(*LT, *RT, E));
1486 case BO_Shr:
1487 return Discard(this->emitShr(*LT, *RT, E));
1488 case BO_Xor:
1489 return Discard(this->emitBitXor(*T, E));
1490 case BO_LOr:
1491 case BO_LAnd:
1492 llvm_unreachable("Already handled earlier");
1493 default:
1494 return false;
1495 }
1496
1497 llvm_unreachable("Unhandled binary op");
1498}
1499
1500/// Perform addition/subtraction of a pointer and an integer or
1501/// subtraction of two pointers.
1502template <class Emitter>
1504 BinaryOperatorKind Op = E->getOpcode();
1505 const Expr *LHS = E->getLHS();
1506 const Expr *RHS = E->getRHS();
1507
1508 if ((Op != BO_Add && Op != BO_Sub) ||
1509 (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType()))
1510 return false;
1511
1512 OptPrimType LT = classify(LHS);
1513 OptPrimType RT = classify(RHS);
1514
1515 if (!LT || !RT)
1516 return false;
1517
1518 // Visit the given pointer expression and optionally convert to a PT_Ptr.
1519 auto visitAsPointer = [&](const Expr *E, PrimType T) -> bool {
1520 if (!this->visit(E))
1521 return false;
1522 if (T != PT_Ptr)
1523 return this->emitDecayPtr(T, PT_Ptr, E);
1524 return true;
1525 };
1526
1527 if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
1528 if (Op != BO_Sub)
1529 return false;
1530
1531 assert(E->getType()->isIntegerType());
1532 if (!visitAsPointer(RHS, *RT) || !visitAsPointer(LHS, *LT))
1533 return false;
1534
1535 QualType ElemType = LHS->getType()->getPointeeType();
1536 CharUnits ElemTypeSize;
1537 if (ElemType->isVoidType() || ElemType->isFunctionType())
1538 ElemTypeSize = CharUnits::One();
1539 else
1540 ElemTypeSize = Ctx.getASTContext().getTypeSizeInChars(ElemType);
1541
1542 PrimType IntT = classifyPrim(E->getType());
1543 if (!this->emitSubPtr(IntT, ElemTypeSize.getQuantity(), E))
1544 return false;
1545 return DiscardResult ? this->emitPop(IntT, E) : true;
1546 }
1547
1548 PrimType OffsetType;
1549 if (LHS->getType()->isIntegerType()) {
1550 if (!visitAsPointer(RHS, *RT))
1551 return false;
1552 if (!this->visit(LHS))
1553 return false;
1554 OffsetType = *LT;
1555 } else if (RHS->getType()->isIntegerType()) {
1556 if (!visitAsPointer(LHS, *LT))
1557 return false;
1558 if (!this->visit(RHS))
1559 return false;
1560 OffsetType = *RT;
1561 } else {
1562 return false;
1563 }
1564
1565 // Do the operation and optionally transform to
1566 // result pointer type.
1567 switch (Op) {
1568 case BO_Add:
1569 if (!this->emitAddOffset(OffsetType, E))
1570 return false;
1571 break;
1572 case BO_Sub:
1573 if (!this->emitSubOffset(OffsetType, E))
1574 return false;
1575 break;
1576 default:
1577 return false;
1578 }
1579
1580 PrimType ExprT = classifyPrim(E);
1581 if (ExprT != PT_Ptr) {
1582 if (!this->emitDecayPtr(PT_Ptr, ExprT, E))
1583 return false;
1584 }
1585
1586 if (DiscardResult)
1587 return this->emitPop(ExprT, E);
1588 return true;
1589}
1590
1591template <class Emitter>
1593 assert(E->isLogicalOp());
1594 BinaryOperatorKind Op = E->getOpcode();
1595 const Expr *LHS = E->getLHS();
1596 const Expr *RHS = E->getRHS();
1597
1598 if (Op == BO_LOr) {
1599 // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
1600 LabelTy LabelTrue = this->getLabel();
1601 LabelTy LabelEnd = this->getLabel();
1602
1603 if (!this->visitBool(LHS))
1604 return false;
1605 if (!this->jumpTrue(LabelTrue, E))
1606 return false;
1607
1608 if (!this->visitBool(RHS))
1609 return false;
1610 if (!this->jump(LabelEnd, E))
1611 return false;
1612
1613 this->emitLabel(LabelTrue);
1614 this->emitConstBool(true, E);
1615 this->fallthrough(LabelEnd);
1616 this->emitLabel(LabelEnd);
1617
1618 } else {
1619 assert(Op == BO_LAnd);
1620 // Logical AND.
1621 // Visit LHS. Only visit RHS if LHS was TRUE.
1622 LabelTy LabelFalse = this->getLabel();
1623 LabelTy LabelEnd = this->getLabel();
1624
1625 if (!this->visitBool(LHS))
1626 return false;
1627 if (!this->jumpFalse(LabelFalse, E))
1628 return false;
1629
1630 if (!this->visitBool(RHS))
1631 return false;
1632 if (!this->jump(LabelEnd, E))
1633 return false;
1634
1635 this->emitLabel(LabelFalse);
1636 this->emitConstBool(false, E);
1637 this->fallthrough(LabelEnd);
1638 this->emitLabel(LabelEnd);
1639 }
1640
1641 if (DiscardResult)
1642 return this->emitPopBool(E);
1643
1644 // For C, cast back to integer type.
1645 if (!E->getType()->isBooleanType()) {
1647 return this->emitCast(PT_Bool, T, E);
1648 }
1649 return true;
1650}
1651
1652template <class Emitter>
1654 // Prepare storage for result.
1655 if (!Initializing) {
1656 UnsignedOrNone LocalIndex = allocateTemporary(E);
1657 if (!LocalIndex)
1658 return false;
1659 if (!this->emitGetPtrLocal(*LocalIndex, E))
1660 return false;
1661 }
1662
1663 // Both LHS and RHS might _not_ be of complex type, but one of them
1664 // needs to be.
1665 const Expr *LHS = E->getLHS();
1666 const Expr *RHS = E->getRHS();
1667
1668 PrimType ResultElemT = this->classifyComplexElementType(E->getType());
1669 unsigned ResultOffset = ~0u;
1670 if (!DiscardResult)
1671 ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
1672
1673 // Save result pointer in ResultOffset
1674 if (!this->DiscardResult) {
1675 if (!this->emitDupPtr(E))
1676 return false;
1677 if (!this->emitSetLocal(PT_Ptr, ResultOffset, E))
1678 return false;
1679 }
1680 QualType LHSType = LHS->getType();
1681 if (const auto *AT = LHSType->getAs<AtomicType>())
1682 LHSType = AT->getValueType();
1683 QualType RHSType = RHS->getType();
1684 if (const auto *AT = RHSType->getAs<AtomicType>())
1685 RHSType = AT->getValueType();
1686
1687 bool LHSIsComplex = LHSType->isAnyComplexType();
1688 unsigned LHSOffset;
1689 bool RHSIsComplex = RHSType->isAnyComplexType();
1690
1691 // For ComplexComplex Mul, we have special ops to make their implementation
1692 // easier.
1693 BinaryOperatorKind Op = E->getOpcode();
1694 if (Op == BO_Mul && LHSIsComplex && RHSIsComplex) {
1695 assert(classifyPrim(LHSType->getAs<ComplexType>()->getElementType()) ==
1697 PrimType ElemT =
1699 if (!this->visit(LHS))
1700 return false;
1701 if (!this->visit(RHS))
1702 return false;
1703 if (!this->emitMulc(ElemT, E))
1704 return false;
1705 if (DiscardResult)
1706 return this->emitPopPtr(E);
1707 return true;
1708 }
1709
1710 if (Op == BO_Div && RHSIsComplex) {
1711 QualType ElemQT = RHSType->getAs<ComplexType>()->getElementType();
1712 PrimType ElemT = classifyPrim(ElemQT);
1713 // If the LHS is not complex, we still need to do the full complex
1714 // division, so just stub create a complex value and stub it out with
1715 // the LHS and a zero.
1716
1717 if (!LHSIsComplex) {
1718 // This is using the RHS type for the fake-complex LHS.
1719 UnsignedOrNone LocalIndex = allocateTemporary(RHS);
1720 if (!LocalIndex)
1721 return false;
1722 LHSOffset = *LocalIndex;
1723
1724 if (!this->emitGetPtrLocal(LHSOffset, E))
1725 return false;
1726
1727 if (!this->visit(LHS))
1728 return false;
1729 // real is LHS
1730 if (!this->emitInitElem(ElemT, 0, E))
1731 return false;
1732 // imag is zero
1733 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1734 return false;
1735 if (!this->emitInitElem(ElemT, 1, E))
1736 return false;
1737 } else {
1738 if (!this->visit(LHS))
1739 return false;
1740 }
1741
1742 if (!this->visit(RHS))
1743 return false;
1744 if (!this->emitDivc(ElemT, E))
1745 return false;
1746 if (DiscardResult)
1747 return this->emitPopPtr(E);
1748 return true;
1749 }
1750
1751 // Evaluate LHS and save value to LHSOffset.
1752 if (LHSType->isAnyComplexType()) {
1753 LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
1754 if (!this->visit(LHS))
1755 return false;
1756 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1757 return false;
1758 } else {
1759 PrimType LHST = classifyPrim(LHSType);
1760 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
1761 if (!this->visit(LHS))
1762 return false;
1763 if (!this->emitSetLocal(LHST, LHSOffset, E))
1764 return false;
1765 }
1766
1767 // Same with RHS.
1768 unsigned RHSOffset;
1769 if (RHSType->isAnyComplexType()) {
1770 RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
1771 if (!this->visit(RHS))
1772 return false;
1773 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1774 return false;
1775 } else {
1776 PrimType RHST = classifyPrim(RHSType);
1777 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
1778 if (!this->visit(RHS))
1779 return false;
1780 if (!this->emitSetLocal(RHST, RHSOffset, E))
1781 return false;
1782 }
1783
1784 // For both LHS and RHS, either load the value from the complex pointer, or
1785 // directly from the local variable. For index 1 (i.e. the imaginary part),
1786 // just load 0 and do the operation anyway.
1787 auto loadComplexValue = [this](bool IsComplex, bool LoadZero,
1788 unsigned ElemIndex, unsigned Offset,
1789 const Expr *E) -> bool {
1790 if (IsComplex) {
1791 if (!this->emitGetLocal(PT_Ptr, Offset, E))
1792 return false;
1793 return this->emitArrayElemPop(classifyComplexElementType(E->getType()),
1794 ElemIndex, E);
1795 }
1796 if (ElemIndex == 0 || !LoadZero)
1797 return this->emitGetLocal(classifyPrim(E->getType()), Offset, E);
1798 return this->visitZeroInitializer(classifyPrim(E->getType()), E->getType(),
1799 E);
1800 };
1801
1802 // Now we can get pointers to the LHS and RHS from the offsets above.
1803 for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) {
1804 // Result pointer for the store later.
1805 if (!this->DiscardResult) {
1806 if (!this->emitGetLocal(PT_Ptr, ResultOffset, E))
1807 return false;
1808 }
1809
1810 // The actual operation.
1811 switch (Op) {
1812 case BO_Add:
1813 if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1814 return false;
1815
1816 if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1817 return false;
1818 if (ResultElemT == PT_Float) {
1819 if (!this->emitAddf(getFPOptions(E), E))
1820 return false;
1821 } else {
1822 if (!this->emitAdd(ResultElemT, E))
1823 return false;
1824 }
1825 break;
1826 case BO_Sub:
1827 if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1828 return false;
1829
1830 if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1831 return false;
1832 if (ResultElemT == PT_Float) {
1833 if (!this->emitSubf(getFPOptions(E), E))
1834 return false;
1835 } else {
1836 if (!this->emitSub(ResultElemT, E))
1837 return false;
1838 }
1839 break;
1840 case BO_Mul:
1841 if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1842 return false;
1843
1844 if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1845 return false;
1846
1847 if (ResultElemT == PT_Float) {
1848 if (!this->emitMulf(getFPOptions(E), E))
1849 return false;
1850 } else {
1851 if (!this->emitMul(ResultElemT, E))
1852 return false;
1853 }
1854 break;
1855 case BO_Div:
1856 assert(!RHSIsComplex);
1857 if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1858 return false;
1859
1860 if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1861 return false;
1862
1863 if (ResultElemT == PT_Float) {
1864 if (!this->emitDivf(getFPOptions(E), E))
1865 return false;
1866 } else {
1867 if (!this->emitDiv(ResultElemT, E))
1868 return false;
1869 }
1870 break;
1871
1872 default:
1873 return false;
1874 }
1875
1876 if (!this->DiscardResult) {
1877 // Initialize array element with the value we just computed.
1878 if (!this->emitInitElemPop(ResultElemT, ElemIndex, E))
1879 return false;
1880 } else {
1881 if (!this->emitPop(ResultElemT, E))
1882 return false;
1883 // Remove the Complex temporary pointer we created ourselves at the
1884 // beginning of this function.
1885 if (!Initializing)
1886 return this->emitPopPtr(E);
1887 }
1888 }
1889 return this->emitFinishInit(E);
1890}
1891
1892template <class Emitter>
1894 const Expr *LHS = E->getLHS();
1895 const Expr *RHS = E->getRHS();
1896 assert(!E->isCommaOp() &&
1897 "Comma op should be handled in VisitBinaryOperator");
1898
1899 QualType LHSType = LHS->getType();
1900 if (const auto *AT = LHSType->getAs<AtomicType>())
1901 LHSType = AT->getValueType();
1902 QualType RHSType = RHS->getType();
1903 if (const auto *AT = RHSType->getAs<AtomicType>())
1904 RHSType = AT->getValueType();
1905 assert(E->getType()->isVectorType());
1906 assert(LHSType->isVectorType());
1907 assert(RHSType->isVectorType());
1908
1909 // We can only handle vectors with primitive element types.
1910 if (!canClassify(LHSType->castAs<VectorType>()->getElementType()))
1911 return false;
1912
1913 // Prepare storage for result.
1914 if (!Initializing && !E->isCompoundAssignmentOp() && !E->isAssignmentOp()) {
1915 UnsignedOrNone LocalIndex = allocateTemporary(E);
1916 if (!LocalIndex)
1917 return false;
1918 if (!this->emitGetPtrLocal(*LocalIndex, E))
1919 return false;
1920 }
1921
1922 const auto *VecTy = E->getType()->getAs<VectorType>();
1923 auto Op = E->isCompoundAssignmentOp()
1925 : E->getOpcode();
1926
1927 PrimType ElemT = this->classifyVectorElementType(LHSType);
1928 PrimType RHSElemT = this->classifyVectorElementType(RHSType);
1929 PrimType ResultElemT = this->classifyVectorElementType(E->getType());
1930
1931 if (E->getOpcode() == BO_Assign) {
1932 assert(Ctx.getASTContext().hasSameUnqualifiedType(
1933 LHSType->castAs<VectorType>()->getElementType(),
1934 RHSType->castAs<VectorType>()->getElementType()));
1935 if (!this->visit(LHS))
1936 return false;
1937 if (!this->visit(RHS))
1938 return false;
1939 if (!this->emitCopyArray(ElemT, 0, 0, VecTy->getNumElements(), E))
1940 return false;
1941 if (DiscardResult)
1942 return this->emitPopPtr(E);
1943 return true;
1944 }
1945
1946 // Evaluate LHS and save value to LHSOffset.
1947 unsigned LHSOffset =
1948 this->allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
1949 if (!this->visit(LHS))
1950 return false;
1951 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1952 return false;
1953
1954 // Evaluate RHS and save value to RHSOffset.
1955 unsigned RHSOffset =
1956 this->allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
1957 if (!this->visit(RHS))
1958 return false;
1959 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1960 return false;
1961
1962 if (E->isCompoundAssignmentOp() && !this->emitGetLocal(PT_Ptr, LHSOffset, E))
1963 return false;
1964
1965 // BitAdd/BitOr/BitXor/Shl/Shr doesn't support bool type, we need perform the
1966 // integer promotion.
1967 bool NeedIntPromot = ElemT == PT_Bool && (E->isBitwiseOp() || E->isShiftOp());
1968 QualType PromotTy;
1969 PrimType PromotT = PT_Bool;
1970 PrimType OpT = ElemT;
1971 if (NeedIntPromot) {
1972 PromotTy =
1973 Ctx.getASTContext().getPromotedIntegerType(Ctx.getASTContext().BoolTy);
1974 PromotT = classifyPrim(PromotTy);
1975 OpT = PromotT;
1976 }
1977
1978 auto getElem = [=](unsigned Offset, PrimType ElemT, unsigned Index) {
1979 if (!this->emitGetLocal(PT_Ptr, Offset, E))
1980 return false;
1981 if (!this->emitArrayElemPop(ElemT, Index, E))
1982 return false;
1983 if (E->isLogicalOp()) {
1984 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
1985 return false;
1986 if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
1987 return false;
1988 } else if (NeedIntPromot) {
1989 if (!this->emitPrimCast(ElemT, PromotT, PromotTy, E))
1990 return false;
1991 }
1992 return true;
1993 };
1994
1995#define EMIT_ARITH_OP(OP) \
1996 { \
1997 if (ElemT == PT_Float) { \
1998 if (!this->emit##OP##f(getFPOptions(E), E)) \
1999 return false; \
2000 } else { \
2001 if (!this->emit##OP(ElemT, E)) \
2002 return false; \
2003 } \
2004 break; \
2005 }
2006
2007 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
2008 if (!getElem(LHSOffset, ElemT, I))
2009 return false;
2010 if (!getElem(RHSOffset, RHSElemT, I))
2011 return false;
2012 switch (Op) {
2013 case BO_Add:
2015 case BO_Sub:
2017 case BO_Mul:
2019 case BO_Div:
2021 case BO_Rem:
2022 if (!this->emitRem(ElemT, E))
2023 return false;
2024 break;
2025 case BO_And:
2026 if (!this->emitBitAnd(OpT, E))
2027 return false;
2028 break;
2029 case BO_Or:
2030 if (!this->emitBitOr(OpT, E))
2031 return false;
2032 break;
2033 case BO_Xor:
2034 if (!this->emitBitXor(OpT, E))
2035 return false;
2036 break;
2037 case BO_Shl:
2038 if (!this->emitShl(OpT, RHSElemT, E))
2039 return false;
2040 break;
2041 case BO_Shr:
2042 if (!this->emitShr(OpT, RHSElemT, E))
2043 return false;
2044 break;
2045 case BO_EQ:
2046 if (!this->emitEQ(ElemT, E))
2047 return false;
2048 break;
2049 case BO_NE:
2050 if (!this->emitNE(ElemT, E))
2051 return false;
2052 break;
2053 case BO_LE:
2054 if (!this->emitLE(ElemT, E))
2055 return false;
2056 break;
2057 case BO_LT:
2058 if (!this->emitLT(ElemT, E))
2059 return false;
2060 break;
2061 case BO_GE:
2062 if (!this->emitGE(ElemT, E))
2063 return false;
2064 break;
2065 case BO_GT:
2066 if (!this->emitGT(ElemT, E))
2067 return false;
2068 break;
2069 case BO_LAnd:
2070 // a && b is equivalent to a!=0 & b!=0
2071 if (!this->emitBitAnd(ResultElemT, E))
2072 return false;
2073 break;
2074 case BO_LOr:
2075 // a || b is equivalent to a!=0 | b!=0
2076 if (!this->emitBitOr(ResultElemT, E))
2077 return false;
2078 break;
2079 default:
2080 return this->emitInvalid(E);
2081 }
2082
2083 // The result of the comparison is a vector of the same width and number
2084 // of elements as the comparison operands with a signed integral element
2085 // type.
2086 //
2087 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
2088 if (E->isComparisonOp()) {
2089 if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
2090 return false;
2091 if (!this->emitNeg(ResultElemT, E))
2092 return false;
2093 }
2094
2095 // If we performed an integer promotion, we need to cast the compute result
2096 // into result vector element type.
2097 if (NeedIntPromot &&
2098 !this->emitPrimCast(PromotT, ResultElemT, VecTy->getElementType(), E))
2099 return false;
2100
2101 // Initialize array element with the value we just computed.
2102 if (!this->emitInitElem(ResultElemT, I, E))
2103 return false;
2104 }
2105
2106 if (DiscardResult && E->isCompoundAssignmentOp() && !this->emitPopPtr(E))
2107 return false;
2108 return true;
2109}
2110
2111template <class Emitter>
2113 const Expr *LHS = E->getLHS();
2114 const Expr *RHS = E->getRHS();
2115 const ASTContext &ASTCtx = Ctx.getASTContext();
2116
2117 assert(LHS->getType()->isFixedPointType() ||
2118 RHS->getType()->isFixedPointType());
2119
2120 auto LHSSema = ASTCtx.getFixedPointSemantics(LHS->getType());
2121 auto LHSSemaInt = LHSSema.toOpaqueInt();
2122 auto RHSSema = ASTCtx.getFixedPointSemantics(RHS->getType());
2123 auto RHSSemaInt = RHSSema.toOpaqueInt();
2124
2125 if (!this->visit(LHS))
2126 return false;
2127 if (!LHS->getType()->isFixedPointType()) {
2128 if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()),
2129 LHSSemaInt, E))
2130 return false;
2131 }
2132
2133 if (!this->visit(RHS))
2134 return false;
2135 if (!RHS->getType()->isFixedPointType()) {
2136 if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()),
2137 RHSSemaInt, E))
2138 return false;
2139 }
2140
2141 // Convert the result to the target semantics.
2142 auto ConvertResult = [&](bool R) -> bool {
2143 if (!R)
2144 return false;
2145 auto ResultSema = ASTCtx.getFixedPointSemantics(E->getType()).toOpaqueInt();
2146 auto CommonSema = LHSSema.getCommonSemantics(RHSSema).toOpaqueInt();
2147 if (ResultSema != CommonSema)
2148 return this->emitCastFixedPoint(ResultSema, E);
2149 return true;
2150 };
2151
2152 auto MaybeCastToBool = [&](bool Result) {
2153 if (!Result)
2154 return false;
2155 PrimType T = classifyPrim(E);
2156 if (DiscardResult)
2157 return this->emitPop(T, E);
2158 if (T != PT_Bool)
2159 return this->emitCast(PT_Bool, T, E);
2160 return true;
2161 };
2162
2163 switch (E->getOpcode()) {
2164 case BO_EQ:
2165 return MaybeCastToBool(this->emitEQFixedPoint(E));
2166 case BO_NE:
2167 return MaybeCastToBool(this->emitNEFixedPoint(E));
2168 case BO_LT:
2169 return MaybeCastToBool(this->emitLTFixedPoint(E));
2170 case BO_LE:
2171 return MaybeCastToBool(this->emitLEFixedPoint(E));
2172 case BO_GT:
2173 return MaybeCastToBool(this->emitGTFixedPoint(E));
2174 case BO_GE:
2175 return MaybeCastToBool(this->emitGEFixedPoint(E));
2176 case BO_Add:
2177 return ConvertResult(this->emitAddFixedPoint(E));
2178 case BO_Sub:
2179 return ConvertResult(this->emitSubFixedPoint(E));
2180 case BO_Mul:
2181 return ConvertResult(this->emitMulFixedPoint(E));
2182 case BO_Div:
2183 return ConvertResult(this->emitDivFixedPoint(E));
2184 case BO_Shl:
2185 return ConvertResult(this->emitShiftFixedPoint(/*Left=*/true, E));
2186 case BO_Shr:
2187 return ConvertResult(this->emitShiftFixedPoint(/*Left=*/false, E));
2188
2189 default:
2190 return this->emitInvalid(E);
2191 }
2192
2193 llvm_unreachable("unhandled binop opcode");
2194}
2195
2196template <class Emitter>
2198 const Expr *SubExpr = E->getSubExpr();
2199 assert(SubExpr->getType()->isFixedPointType());
2200
2201 switch (E->getOpcode()) {
2202 case UO_Plus:
2203 return this->delegate(SubExpr);
2204 case UO_Minus:
2205 if (!this->visit(SubExpr))
2206 return false;
2207 if (!this->emitNegFixedPoint(E))
2208 return false;
2209 if (DiscardResult)
2210 return this->emitPopFixedPoint(E);
2211 return true;
2212 default:
2213 return false;
2214 }
2215
2216 llvm_unreachable("Unhandled unary opcode");
2217}
2218
2219template <class Emitter>
2221 const ImplicitValueInitExpr *E) {
2222 if (DiscardResult)
2223 return true;
2224
2225 QualType QT = E->getType();
2226
2227 if (OptPrimType T = classify(QT))
2228 return this->visitZeroInitializer(*T, QT, E);
2229
2230 if (QT->isRecordType()) {
2231 const RecordDecl *RD = QT->getAsRecordDecl();
2232 assert(RD);
2233 if (RD->isInvalidDecl())
2234 return false;
2235
2236 const Record *R = getRecord(QT);
2237 if (!R)
2238 return false;
2239
2240 assert(Initializing);
2241 return this->visitZeroRecordInitializer(R, E);
2242 }
2243
2244 if (QT->isIncompleteArrayType())
2245 return true;
2246
2247 if (QT->isArrayType())
2248 return this->visitZeroArrayInitializer(QT, E);
2249
2250 if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) {
2251 assert(Initializing);
2252 QualType ElemQT = ComplexTy->getElementType();
2253 PrimType ElemT = classifyPrim(ElemQT);
2254 for (unsigned I = 0; I < 2; ++I) {
2255 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2256 return false;
2257 if (!this->emitInitElem(ElemT, I, E))
2258 return false;
2259 }
2260 return true;
2261 }
2262
2263 if (const auto *VecT = E->getType()->getAs<VectorType>()) {
2264 unsigned NumVecElements = VecT->getNumElements();
2265 QualType ElemQT = VecT->getElementType();
2266 PrimType ElemT = classifyPrim(ElemQT);
2267
2268 for (unsigned I = 0; I < NumVecElements; ++I) {
2269 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2270 return false;
2271 if (!this->emitInitElem(ElemT, I, E))
2272 return false;
2273 }
2274 return true;
2275 }
2276
2277 if (const auto *MT = E->getType()->getAs<ConstantMatrixType>()) {
2278 unsigned NumElems = MT->getNumElementsFlattened();
2279 QualType ElemQT = MT->getElementType();
2280 PrimType ElemT = classifyPrim(ElemQT);
2281
2282 for (unsigned I = 0; I != NumElems; ++I) {
2283 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2284 return false;
2285 if (!this->emitInitElem(ElemT, I, E))
2286 return false;
2287 }
2288 return true;
2289 }
2290
2291 return false;
2292}
2293
2294template <class Emitter>
2296 if (E->getType()->isVoidType() || E->containsErrors())
2297 return false;
2298
2299 const Expr *LHS = E->getLHS();
2300 const Expr *RHS = E->getRHS();
2301 const Expr *Index = E->getIdx();
2302 const Expr *Base = E->getBase();
2303
2304 // C++17's rules require us to evaluate the LHS first, regardless of which
2305 // side is the base.
2306 bool Success = true;
2307 for (const Expr *SubExpr : {LHS, RHS}) {
2308 if (!this->visit(SubExpr)) {
2309 Success = false;
2310 continue;
2311 }
2312
2313 // Expand the base if this is a subscript on a
2314 // pointer expression.
2315 if (SubExpr == Base && Base->getType()->isPointerType()) {
2316 if (!this->emitExpandPtr(E))
2317 Success = false;
2318 }
2319 }
2320
2321 if (!Success)
2322 return false;
2323
2324 OptPrimType IndexT = classify(Index->getType());
2325 // In error-recovery cases, the index expression has a dependent type.
2326 if (!IndexT)
2327 return this->emitError(E);
2328 // If the index is first, we need to change that.
2329 if (LHS == Index) {
2330 if (!this->emitFlip(PT_Ptr, *IndexT, E))
2331 return false;
2332 }
2333
2334 if (!this->emitArrayElemPtrPop(*IndexT, E))
2335 return false;
2336 if (DiscardResult)
2337 return this->emitPopPtr(E);
2338
2339 if (E->isGLValue())
2340 return true;
2341
2343 return this->emitLoadPop(*T, E);
2344}
2345
2346template <class Emitter>
2348 const Expr *ArrayFiller, const Expr *E) {
2350
2351 QualType QT = E->getType();
2352 if (const auto *AT = QT->getAs<AtomicType>())
2353 QT = AT->getValueType();
2354
2355 if (QT->isVoidType()) {
2356 if (Inits.size() == 0)
2357 return true;
2358 return this->emitInvalid(E);
2359 }
2360
2361 // Primitive values. A discarded one can simply discard each initializer;
2362 // there is no object to establish.
2363 if (OptPrimType T = classify(QT)) {
2364 if (DiscardResult) {
2365 for (const Expr *Init : Inits) {
2366 if (!this->discard(Init))
2367 return false;
2368 }
2369 return true;
2370 }
2371 if (Inits.size() == 0)
2372 return this->visitZeroInitializer(*T, QT, E);
2373 assert(Inits.size() == 1);
2374 return this->delegate(Inits[0]);
2375 }
2376
2377 assert(!canClassify(E->getType()));
2378
2379 // A composite prvalue needs somewhere to live even when it is discarded: a
2380 // default member initializer may read subobjects initialized earlier in this
2381 // same list, so those have to actually be written and `this` has to denote
2382 // the object. Materialize one and initialize into it.
2383 if (DiscardResult && !Initializing) {
2384 UnsignedOrNone LocalIndex = allocateLocal(E);
2385 if (!LocalIndex)
2386 return false;
2387 if (!this->emitGetPtrLocal(*LocalIndex, E))
2388 return false;
2389 InitLinkScope<Emitter> ILS2(this, InitLink::Temp(*LocalIndex));
2390 return this->visitInitializerPop(E);
2391 }
2392
2393 if (QT->isRecordType()) {
2394 const Record *R = getRecord(QT);
2395
2396 if (Inits.size() == 1 && E->getType() == Inits[0]->getType())
2397 return this->delegate(Inits[0]);
2398
2399 if (!R)
2400 return false;
2401
2402 auto initPrimitiveField = [=](const Record::Field *FieldToInit,
2403 const Expr *Init, PrimType T,
2404 bool Activate = false) -> bool {
2407
2408 if (DefaultInit && !this->emitStartFieldInit(FieldToInit->Offset, Init))
2409 return false;
2410
2411 if (!this->visit(Init))
2412 return false;
2413
2414 if (DefaultInit && !this->emitEndInit(Init))
2415 return false;
2416
2417 bool BitField = FieldToInit->isBitField();
2418 if (BitField && Activate)
2419 return this->emitInitBitFieldActivate(T, FieldToInit->Offset,
2420 FieldToInit->bitWidth(), E);
2421 if (BitField)
2422 return this->emitInitBitField(T, FieldToInit->Offset,
2423 FieldToInit->bitWidth(), E);
2424 if (Activate)
2425 return this->emitInitFieldActivate(T, FieldToInit->Offset, E);
2426 return this->emitInitField(T, FieldToInit->Offset, E);
2427 };
2428
2429 auto initCompositeField = [=](const Record::Field *FieldToInit,
2430 const Expr *Init,
2431 bool Activate = false) -> bool {
2433 InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
2434
2435 // Non-primitive case. Get a pointer to the field-to-initialize
2436 // on the stack and recurse into visitInitializer().
2437 if (!this->emitGetPtrField(FieldToInit->Offset, Init))
2438 return false;
2439
2440 if (Activate && !this->emitActivate(E))
2441 return false;
2442
2443 if (!this->emitStartInit(Init))
2444 return false;
2445
2446 return this->visitInitializerPop(Init) && this->emitEndInit(Init);
2447 };
2448
2449 if (R->isUnion()) {
2450 if (Inits.size() == 0) {
2451 if (!this->visitZeroRecordInitializer(R, E))
2452 return false;
2453 } else {
2454 const Expr *Init = Inits[0];
2455 const FieldDecl *FToInit = nullptr;
2456 if (const auto *ILE = dyn_cast<InitListExpr>(E))
2457 FToInit = ILE->getInitializedFieldInUnion();
2458 else
2459 FToInit = cast<CXXParenListInitExpr>(E)->getInitializedFieldInUnion();
2460
2461 const Record::Field *FieldToInit = R->getField(FToInit);
2462 if (OptPrimType T = classify(Init)) {
2463 if (!initPrimitiveField(FieldToInit, Init, *T, /*Activate=*/true))
2464 return false;
2465 } else {
2466 if (!initCompositeField(FieldToInit, Init, /*Activate=*/true))
2467 return false;
2468 }
2469 }
2470 return this->emitFinishInit(E);
2471 }
2472
2473 assert(!R->isUnion());
2474 for (unsigned BI = 0; BI != R->getNumBases(); ++BI) {
2475 const Expr *Init = Inits[BI];
2476 const Record::Base *B = R->getBase(BI);
2478 InitLinkScope<Emitter> ILS(this, InitLink::Base(B->Offset));
2479 if (!this->emitGetPtrBase(B->Offset, Init))
2480 return false;
2481 if (!this->visitInitializerPop(Init))
2482 return false;
2483 }
2484
2485 unsigned FieldIndex = 0;
2486 for (unsigned FI = R->getNumBases(); FI != Inits.size();) {
2487 const Record::Field *FieldToInit = R->getField(FieldIndex);
2488 if (FieldToInit->isUnnamedBitField()) {
2489 ++FieldIndex;
2490 continue;
2491 }
2492
2493 const Expr *Init = Inits[FI];
2494 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
2495 // aren't supposed to be modified.
2496 if (isa<NoInitExpr>(Init)) {
2497 ++FieldIndex;
2498 ++FI;
2499 continue;
2500 }
2501
2502 if (OptPrimType T = classify(Init)) {
2503 if (!initPrimitiveField(FieldToInit, Init, *T))
2504 return false;
2505 } else if (!initCompositeField(FieldToInit, Init)) {
2506 return false;
2507 }
2508
2509 ++FI;
2510 ++FieldIndex;
2511 }
2512
2513 assert(R->getNumVirtualBases() == 0);
2514
2515 return this->emitFinishInit(E);
2516 }
2517
2518 if (QT->isArrayType()) {
2519 const ConstantArrayType *CAT =
2520 Ctx.getASTContext().getAsConstantArrayType(QT);
2521 uint64_t NumElems = CAT->getZExtSize();
2522
2523 if (Initializing &&
2524 (!InitializingDecl || InitializingDecl->hasLocalStorage()) &&
2525 !this->emitCheckArrayDestSize(NumElems, E))
2526 return false;
2527
2528 if (Inits.size() == 1 && QT == Inits[0]->getType())
2529 return this->delegate(Inits[0]);
2530
2531 OptPrimType InitT = classify(CAT->getElementType());
2532 unsigned ElementIndex = 0;
2533 for (const Expr *Init : Inits) {
2534 if (const auto *EmbedS =
2535 dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {
2536 PrimType TargetT = classifyPrim(Init->getType());
2537
2538 auto Eval = [&](const IntegerLiteral *IL, unsigned ElemIndex) {
2539 if (TargetT == PT_Float) {
2540 if (!this->emitConst(IL->getValue(), classifyPrim(IL), Init))
2541 return false;
2542 const auto *Sem = &Ctx.getFloatSemantics(CAT->getElementType());
2543 if (!this->emitCastIntegralFloating(classifyPrim(IL), Sem,
2544 getFPOptions(E), E))
2545 return false;
2546 } else {
2547 if (!this->emitConst(IL->getValue(), TargetT, Init))
2548 return false;
2549 }
2550 return this->emitInitElem(TargetT, ElemIndex, IL);
2551 };
2552 if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
2553 return false;
2554 } else if (isa<NoInitExpr>(Init)) {
2555 // If this is a child of a DesignatedInitUpdateExpr, skip elements which
2556 // aren't supposed to be modified.
2557 ++ElementIndex;
2558 } else {
2559 if (!this->visitArrayElemInit(ElementIndex, Init, InitT))
2560 return false;
2561 ++ElementIndex;
2562 }
2563 }
2564
2565 // Expand the filler expression.
2566 // FIXME: This should go away.
2567 if (ArrayFiller && !isa<NoInitExpr>(ArrayFiller)) {
2568 for (; ElementIndex != NumElems; ++ElementIndex) {
2569 if (!this->visitArrayElemInit(ElementIndex, ArrayFiller, InitT))
2570 return false;
2571 }
2572 }
2573
2574 return this->emitFinishInit(E);
2575 }
2576
2577 if (const auto *ComplexTy = QT->getAs<ComplexType>()) {
2578 unsigned NumInits = Inits.size();
2579
2580 if (NumInits == 1)
2581 return this->delegate(Inits[0]);
2582
2583 QualType ElemQT = ComplexTy->getElementType();
2584 PrimType ElemT = classifyPrim(ElemQT);
2585 if (NumInits == 0) {
2586 // Zero-initialize both elements.
2587 for (unsigned I = 0; I < 2; ++I) {
2588 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2589 return false;
2590 if (!this->emitInitElem(ElemT, I, E))
2591 return false;
2592 }
2593 } else if (NumInits == 2) {
2594 unsigned InitIndex = 0;
2595 for (const Expr *Init : Inits) {
2596 if (!this->visit(Init))
2597 return false;
2598
2599 if (!this->emitInitElem(ElemT, InitIndex, E))
2600 return false;
2601 ++InitIndex;
2602 }
2603 }
2604 return true;
2605 }
2606
2607 if (const auto *VecT = QT->getAs<VectorType>()) {
2608 unsigned NumVecElements = VecT->getNumElements();
2609 assert(NumVecElements >= Inits.size());
2610
2611 QualType ElemQT = VecT->getElementType();
2612 PrimType ElemT = classifyPrim(ElemQT);
2613
2614 // All initializer elements.
2615 unsigned InitIndex = 0;
2616 for (const Expr *Init : Inits) {
2617 if (!this->visit(Init))
2618 return false;
2619
2620 // If the initializer is of vector type itself, we have to deconstruct
2621 // that and initialize all the target fields from the initializer fields.
2622 if (const auto *InitVecT = Init->getType()->getAs<VectorType>()) {
2623 if (!this->emitCopyArray(ElemT, 0, InitIndex,
2624 InitVecT->getNumElements(), E))
2625 return false;
2626 InitIndex += InitVecT->getNumElements();
2627 } else {
2628 if (!this->emitInitElem(ElemT, InitIndex, E))
2629 return false;
2630 ++InitIndex;
2631 }
2632 }
2633
2634 assert(InitIndex <= NumVecElements);
2635
2636 // Fill the rest with zeroes.
2637 for (; InitIndex != NumVecElements; ++InitIndex) {
2638 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
2639 return false;
2640 if (!this->emitInitElem(ElemT, InitIndex, E))
2641 return false;
2642 }
2643 return true;
2644 }
2645
2646 if (const auto *MT = QT->getAs<ConstantMatrixType>()) {
2647 unsigned NumElems = MT->getNumElementsFlattened();
2648 assert(Inits.size() == NumElems);
2649
2650 QualType ElemQT = MT->getElementType();
2651 PrimType ElemT = classifyPrim(ElemQT);
2652
2653 // Matrix initializer list elements are in row-major order, which matches
2654 // the matrix APValue convention and therefore no index remapping is
2655 // required.
2656 for (unsigned I = 0; I != NumElems; ++I) {
2657 if (!this->visit(Inits[I]))
2658 return false;
2659 if (!this->emitInitElem(ElemT, I, E))
2660 return false;
2661 }
2662 return true;
2663 }
2664
2665 return false;
2666}
2667
2668/// Pointer to the array(not the element!) must be on the stack when calling
2669/// this.
2670template <class Emitter>
2671bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex, const Expr *Init,
2672 OptPrimType InitT) {
2673 if (InitT) {
2674 // Visit the primitive element like normal.
2675 if (!this->visit(Init))
2676 return false;
2677 return this->emitInitElem(*InitT, ElemIndex, Init);
2678 }
2679
2680 InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
2681 // Advance the pointer currently on the stack to the given
2682 // dimension.
2683 if (!this->emitConstUint32(ElemIndex, Init))
2684 return false;
2685 if (!this->emitArrayElemPtrUint32(Init))
2686 return false;
2687 return this->visitInitializerPop(Init);
2688}
2689
2690template <class Emitter>
2692 const FunctionDecl *FuncDecl,
2693 bool Activate, bool IsOperatorCall) {
2694 assert(VarScope->getKind() == ScopeKind::Call);
2695 llvm::BitVector NonNullArgs;
2696 if (FuncDecl && FuncDecl->hasAttr<NonNullAttr>())
2697 NonNullArgs = collectNonNullArgs(FuncDecl, Args);
2698
2699 bool ExplicitMemberFn = false;
2700 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl))
2701 ExplicitMemberFn = MD->isExplicitObjectMemberFunction();
2702
2703 unsigned ArgIndex = 0;
2704 for (const Expr *Arg : Args) {
2705 if (canClassify(Arg)) {
2706 if (!this->visit(Arg))
2707 return false;
2708 } else {
2709
2710 DeclOrExpr Source = Arg;
2711 if (FuncDecl) {
2712 // Try to use the parameter declaration instead of the argument
2713 // expression as a source.
2714 unsigned DeclIndex = ArgIndex - IsOperatorCall + ExplicitMemberFn;
2715 if (DeclIndex < FuncDecl->getNumParams())
2716 Source = FuncDecl->getParamDecl(ArgIndex - IsOperatorCall +
2717 ExplicitMemberFn);
2718 }
2719
2720 UnsignedOrNone LocalIndex =
2721 allocateLocal(Source, Arg->getType(), ScopeKind::Call);
2722 if (!LocalIndex)
2723 return false;
2724
2725 if (!this->emitGetPtrLocal(*LocalIndex, Arg))
2726 return false;
2727 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
2728 if (!this->visitInitializer(Arg))
2729 return false;
2730 }
2731
2732 if (ArgIndex == 1 && Activate) {
2733 if (!this->emitActivate(Arg))
2734 return false;
2735 }
2736
2737 if (!NonNullArgs.empty() && NonNullArgs[ArgIndex]) {
2738 PrimType ArgT = classify(Arg).value_or(PT_Ptr);
2739 if (ArgT == PT_Ptr) {
2740 if (!this->emitCheckNonNullArg(ArgT, Arg))
2741 return false;
2742 }
2743 }
2744
2745 ++ArgIndex;
2746 }
2747
2748 return true;
2749}
2750
2751template <class Emitter>
2753 return this->visitInitList(E->inits(), E->getArrayFiller(), E);
2754}
2755
2756template <class Emitter>
2761
2762template <class Emitter>
2767
2768template <class Emitter>
2770 if (!E->hasAPValueResult())
2771 return this->delegate(E->getSubExpr());
2772
2773 // Emitting an APValue never has side effects.
2774 if (DiscardResult)
2775 return true;
2776
2777 if (OptPrimType T = classify(E)) {
2778 // Try to emit the APValue directly, without visiting the subexpr.
2779 // This will only fail if we can't emit the APValue, so won't emit any
2780 // diagnostics or any double values.
2781 return this->visitAPValue(E->getAPValueResult(), *T, E);
2782 }
2783
2784 if (!Initializing) {
2785 UnsignedOrNone LocalIndex = allocateLocal(E);
2786 if (!LocalIndex)
2787 return false;
2788 if (!this->emitGetPtrLocal(*LocalIndex, E))
2789 return false;
2790 }
2791 return this->visitAPValueInitializer(E->getAPValueResult(), E, E->getType());
2792}
2793
2794template <class Emitter>
2796 auto It = E->begin();
2797 return this->visit(*It);
2798}
2799
2801 UnaryExprOrTypeTrait Kind) {
2802 // C++ [expr.alignof]p3:
2803 // When alignof is applied to a reference type, the result is the
2804 // alignment of the referenced type.
2805 if (const auto *Ref = T->getAs<ReferenceType>())
2806 T = Ref->getPointeeType();
2807
2808 if (T.getQualifiers().hasUnaligned())
2809 return CharUnits::One();
2810
2811 bool AlignOfReturnsPreferred =
2812 ASTCtx.getLangOpts().isCompatibleWith(LangOptions::ClangABI::Ver7);
2813 // __alignof is defined to return the preferred alignment.
2814 // Before 8, clang returned the preferred alignment for alignof and
2815 // _Alignof as well.
2816 if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
2817 return ASTCtx.toCharUnitsFromBits(ASTCtx.getPreferredTypeAlign(T));
2818
2819 return ASTCtx.getTypeAlignInChars(T);
2820}
2821
2822template <class Emitter>
2824 const UnaryExprOrTypeTraitExpr *E) {
2825
2826 UnaryExprOrTypeTrait Kind = E->getKind();
2827 const ASTContext &ASTCtx = Ctx.getASTContext();
2828
2829 if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) {
2831
2832 // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
2833 // the result is the size of the referenced type."
2834 if (const auto *Ref = ArgType->getAs<ReferenceType>())
2835 ArgType = Ref->getPointeeType();
2836
2837 CharUnits Size;
2838 if (ArgType->isVoidType() || ArgType->isFunctionType())
2839 Size = CharUnits::One();
2840 else {
2841 if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
2842 return this->emitInvalid(E);
2843
2844 if (Kind == UETT_SizeOf)
2845 Size = ASTCtx.getTypeSizeInChars(ArgType);
2846 else
2848 }
2849
2850 if (DiscardResult)
2851 return true;
2852
2853 return this->emitConst(Size.getQuantity(), E);
2854 }
2855
2856 if (Kind == UETT_CountOf) {
2857 QualType Ty = E->getTypeOfArgument();
2858 assert(Ty->isArrayType());
2859
2860 // We don't need to worry about array element qualifiers, so getting the
2861 // unsafe array type is fine.
2862 if (const auto *CAT =
2863 dyn_cast<ConstantArrayType>(Ty->getAsArrayTypeUnsafe())) {
2864 if (DiscardResult)
2865 return true;
2866 return this->emitConst(CAT->getSize(), E);
2867 }
2868
2869 assert(!Ty->isConstantSizeType());
2870
2871 // If it's a variable-length array type, we need to check whether it is a
2872 // multidimensional array. If so, we need to check the size expression of
2873 // the VLA to see if it's a constant size. If so, we can return that value.
2874 const auto *VAT = ASTCtx.getAsVariableArrayType(Ty);
2875 assert(VAT);
2876 if (VAT->getElementType()->isArrayType()) {
2877 std::optional<APSInt> Res =
2878 VAT->getSizeExpr()
2879 ? VAT->getSizeExpr()->getIntegerConstantExpr(ASTCtx)
2880 : std::nullopt;
2881 if (Res) {
2882 if (DiscardResult)
2883 return true;
2884 return this->emitConst(*Res, E);
2885 }
2886 }
2887 }
2888
2889 if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) {
2890 CharUnits Size;
2891
2892 if (E->isArgumentType()) {
2894
2895 Size = alignOfType(ArgType, ASTCtx, Kind);
2896 } else {
2897 // Argument is an expression, not a type.
2898 const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
2899
2900 if (Arg->getType()->isDependentType())
2901 return false;
2902
2903 // The kinds of expressions that we have special-case logic here for
2904 // should be kept up to date with the special checks for those
2905 // expressions in Sema.
2906
2907 // alignof decl is always accepted, even if it doesn't make sense: we
2908 // default to 1 in those cases.
2909 if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg))
2910 Size = ASTCtx.getDeclAlign(DRE->getDecl(),
2911 /*ForAlignOf=*/true);
2912 else if (const auto *ME = dyn_cast<MemberExpr>(Arg))
2913 Size = ASTCtx.getDeclAlign(ME->getMemberDecl(),
2914 /*ForAlignOf=*/true);
2915 else
2916 Size = alignOfType(Arg->getType(), ASTCtx, Kind);
2917 }
2918
2919 if (DiscardResult)
2920 return true;
2921
2922 return this->emitConst(Size.getQuantity(), E);
2923 }
2924
2925 if (Kind == UETT_VectorElements) {
2926 if (E->containsErrors())
2927 return false;
2928
2929 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
2930 return this->emitConst(VT->getNumElements(), E);
2932 return this->emitSizelessVectorElementSize(E);
2933 }
2934
2935 if (Kind == UETT_VecStep) {
2936 if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) {
2937 unsigned N = VT->getNumElements();
2938
2939 // The vec_step built-in functions that take a 3-component
2940 // vector return 4. (OpenCL 1.1 spec 6.11.12)
2941 if (N == 3)
2942 N = 4;
2943
2944 return this->emitConst(N, E);
2945 }
2946 return this->emitConst(1, E);
2947 }
2948
2949 if (Kind == UETT_OpenMPRequiredSimdAlign) {
2950 if (E->containsErrors())
2951 return false;
2952 assert(E->isArgumentType());
2953 unsigned Bits = ASTCtx.getOpenMPDefaultSimdAlign(E->getArgumentType());
2954
2955 return this->emitConst(ASTCtx.toCharUnitsFromBits(Bits).getQuantity(), E);
2956 }
2957
2958 if (Kind == UETT_PtrAuthTypeDiscriminator) {
2959 if (E->getArgumentType()->isDependentType())
2960 return this->emitInvalid(E);
2961
2962 return this->emitConst(
2963 const_cast<ASTContext &>(ASTCtx).getPointerAuthTypeDiscriminator(
2964 E->getArgumentType()),
2965 E);
2966 }
2967
2968 return false;
2969}
2970
2971template <class Emitter>
2973 // 'Base.Member'
2974 const Expr *Base = E->getBase();
2975 const ValueDecl *Member = E->getMemberDecl();
2976
2977 if (DiscardResult)
2978 return this->discard(Base);
2979
2980 if (const auto *VD = dyn_cast<VarDecl>(Member)) {
2981 // If the member is a VarDecl, this is a static variable.
2982 // We need to try to lazily evaluate its initializer here since the
2983 // variable might've been deserialized and not registered
2984 // as a global variable yet.
2985 if (VD->getInit() && !VD->getInit()->isValueDependent())
2986 VD->evaluateValue();
2987 if (auto GlobalIndex = P.getGlobal(VD)) {
2988 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2989 return false;
2990 if (Member->getType()->isReferenceType())
2991 return this->emitLoadPopPtr(E);
2992 return true;
2993 }
2994 return false;
2995 }
2996
2997 if (!isa<FieldDecl>(Member)) {
2998 // A non-static member function access only makes sense as part of the
2999 // enclosing call here. Don't try to evaluate it in isolation.
3000 if (const auto *MD = dyn_cast<CXXMethodDecl>(Member);
3001 MD && !MD->isStatic()) {
3002 return false;
3003 }
3004
3005 if (!this->discard(Base) && !this->emitSideEffect(E))
3006 return false;
3007
3008 return this->visitDeclRef(Member, E);
3009 }
3010
3011 if (!this->visit(Base))
3012 return false;
3013
3014 // Base above gives us a pointer on the stack.
3015 const auto *FD = cast<FieldDecl>(Member);
3016 const RecordDecl *RD = FD->getParent();
3017 const Record *R = getRecord(RD);
3018 if (!R)
3019 return false;
3020 const Record::Field *F = R->getField(FD);
3021
3022 // MemberExprs are almost always lvalues, in which case we don't need to
3023 // do the load. But sometimes they aren't.
3024 const auto maybeLoadValue = [&]() -> bool {
3025 if (E->isGLValue())
3026 return true;
3027 if (OptPrimType T = classify(E))
3028 return this->emitLoadPop(*T, E);
3029 return false;
3030 };
3031
3032 // Leave a pointer to the field on the stack.
3033 if (F->Decl->getType()->isReferenceType())
3034 return this->emitGetFieldPop(PT_Ptr, F->Offset, E) && maybeLoadValue();
3035 return this->emitGetPtrFieldPop(F->Offset, E) && maybeLoadValue();
3036}
3037
3038template <class Emitter>
3040 assert(!DiscardResult);
3041 // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
3042 // stand-alone, e.g. via EvaluateAsInt().
3043 if (!ArrayIndex)
3044 return false;
3045 return this->emitConst(*ArrayIndex, E);
3046}
3047
3048template <class Emitter>
3050 assert(Initializing);
3051 assert(!DiscardResult);
3052
3053 const Expr *Common = E->getCommonExpr();
3054 const Expr *SubExpr = E->getSubExpr();
3055 OptPrimType SubExprT = classify(SubExpr);
3056 size_t Size = E->getArraySize().getZExtValue();
3057 if (exceedsArraySizeLimit(Ctx.getLangOpts(), Size))
3058 return this->emitCheckArraySize(Size, E);
3059
3060 if (SubExprT) {
3061 // Unwrap the OpaqueValueExpr so we don't cache something we won't reuse.
3062 Common = cast<OpaqueValueExpr>(Common)->getSourceExpr();
3063
3064 if (!this->visit(Common))
3065 return false;
3066 return this->emitCopyArray(*SubExprT, 0, 0, Size, E);
3067 }
3068
3069 // We visit the common opaque expression here once so we have its value
3070 // cached.
3071 if (!this->discard(Common))
3072 return false;
3073
3074 // TODO: This compiles to quite a lot of bytecode if the array is larger.
3075 // Investigate compiling this to a loop.
3076
3077 // So, every iteration, we execute an assignment here
3078 // where the LHS is on the stack (the target array)
3079 // and the RHS is our SubExpr.
3080 for (size_t I = 0; I != Size; ++I) {
3081 ArrayIndexScope<Emitter> IndexScope(this, I);
3083
3084 if (!this->visitArrayElemInit(I, SubExpr, SubExprT))
3085 return false;
3086 if (!BS.destroyLocals())
3087 return false;
3088 }
3089 return true;
3090}
3091
3092template <class Emitter>
3094 const Expr *SourceExpr = E->getSourceExpr();
3095 if (!SourceExpr)
3096 return false;
3097
3098 if (Initializing) {
3099 assert(!DiscardResult);
3100 return this->visitInitializer(SourceExpr);
3101 }
3102
3103 PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr);
3104 if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end()) {
3105 if (DiscardResult)
3106 return true;
3107 return this->emitGetLocal(SubExprT, It->second, E);
3108 }
3109
3110 if (!this->visit(SourceExpr))
3111 return false;
3112
3113 // At this point we either have the evaluated source expression or a pointer
3114 // to an object on the stack. We want to create a local variable that stores
3115 // this value.
3116 unsigned LocalIndex = allocateLocalPrimitive(E, SubExprT, /*IsConst=*/true);
3117 if (!this->emitSetLocal(SubExprT, LocalIndex, E))
3118 return false;
3119
3120 // This is cleaned up when the local variable is destroyed.
3121 OpaqueExprs.insert({E, LocalIndex});
3122
3123 // Here the local variable is created but the value is removed from the stack,
3124 // so we put it back if the caller needs it.
3125 if (!DiscardResult)
3126 return this->emitGetLocal(SubExprT, LocalIndex, E);
3127 return true;
3128}
3129
3130template <class Emitter>
3132 const AbstractConditionalOperator *E) {
3133 const Expr *Condition = E->getCond();
3134 const Expr *TrueExpr = E->getTrueExpr();
3135 const Expr *FalseExpr = E->getFalseExpr();
3136
3137 if (std::optional<bool> BoolValue = getBoolValue(Condition)) {
3138 if (*BoolValue)
3139 return this->delegate(TrueExpr);
3140 return this->delegate(FalseExpr);
3141 }
3142
3143 bool IsBcpCall = false;
3144 if (const auto *CE = dyn_cast<CallExpr>(Condition->IgnoreParenCasts());
3145 CE && CE->getBuiltinCallee() == Builtin::BI__builtin_constant_p) {
3146 IsBcpCall = true;
3147 }
3148
3149 LabelTy LabelEnd = this->getLabel(); // Label after the operator.
3150 LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
3151
3152 if (IsBcpCall) {
3153 if (!this->emitPushIgnoreDiags(E))
3154 return false;
3155 }
3156
3157 if (!this->visitBool(Condition)) {
3158 // If the condition failed and we're checking for undefined behavior
3159 // (which only happens with EvalEmitter) check the TrueExpr and FalseExpr
3160 // as well.
3161 if (this->checkingForUndefinedBehavior()) {
3162 if (!this->discard(TrueExpr))
3163 return false;
3164 if (!this->discard(FalseExpr))
3165 return false;
3166 }
3167 return false;
3168 }
3169
3170 // The TrueExpr and FalseExpr of a conditional operator do _not_ create a
3171 // scope, which means the local variables created within them unconditionally
3172 // always exist. However, we need to later differentiate which branch was
3173 // taken and only destroy the varibles of the active branch. This is what the
3174 // "enabled" flags on local variables are used for.
3175 llvm::SaveAndRestore LAAA(this->VarScope->LocalsAlwaysEnabled,
3176 /*NewValue=*/false);
3177
3178 if (!this->jumpFalse(LabelFalse, E))
3179 return false;
3180 if (!this->delegate(TrueExpr))
3181 return false;
3182
3183 if (!this->jump(LabelEnd, E))
3184 return false;
3185 this->emitLabel(LabelFalse);
3186 if (!this->delegate(FalseExpr))
3187 return false;
3188
3189 this->fallthrough(LabelEnd);
3190 this->emitLabel(LabelEnd);
3191
3192 if (IsBcpCall)
3193 return this->emitPopIgnoreDiags(E);
3194 return true;
3195}
3196
3197template <class Emitter>
3199 if (DiscardResult)
3200 return true;
3201
3202 if (!Initializing)
3203 return this->emitGetStringPtr(E, E);
3204
3205 // We are initializing an array on the stack.
3206 const ConstantArrayType *CAT =
3207 Ctx.getASTContext().getAsConstantArrayType(E->getType());
3208 assert(CAT && "a string literal that's not a constant array?");
3209
3210 // If the initializer string is too long, a diagnostic has already been
3211 // emitted. Read only the array length from the string literal.
3212 unsigned ArraySize = CAT->getZExtSize();
3213 unsigned N = std::min(ArraySize, E->getLength());
3214 unsigned CharWidth = E->getCharByteWidth();
3215
3216 for (unsigned I = 0; I != N; ++I) {
3217 uint32_t CodeUnit = E->getCodeUnit(I);
3218
3219 if (CharWidth == 1) {
3220 this->emitConstSint8(CodeUnit, E);
3221 this->emitInitElemSint8(I, E);
3222 } else if (CharWidth == 2) {
3223 this->emitConstUint16(CodeUnit, E);
3224 this->emitInitElemUint16(I, E);
3225 } else if (CharWidth == 4) {
3226 this->emitConstUint32(CodeUnit, E);
3227 this->emitInitElemUint32(I, E);
3228 } else {
3229 llvm_unreachable("unsupported character width");
3230 }
3231 }
3232
3233 // Fill up the rest of the char array with NUL bytes.
3234 for (unsigned I = N; I != ArraySize; ++I) {
3235 if (CharWidth == 1) {
3236 this->emitConstSint8(0, E);
3237 this->emitInitElemSint8(I, E);
3238 } else if (CharWidth == 2) {
3239 this->emitConstUint16(0, E);
3240 this->emitInitElemUint16(I, E);
3241 } else if (CharWidth == 4) {
3242 this->emitConstUint32(0, E);
3243 this->emitInitElemUint32(I, E);
3244 } else {
3245 llvm_unreachable("unsupported character width");
3246 }
3247 }
3248
3249 return true;
3250}
3251
3252template <class Emitter>
3254 if (DiscardResult)
3255 return true;
3256 return this->emitDummyPtr(E, E);
3257}
3258
3259template <class Emitter>
3261 auto &A = Ctx.getASTContext();
3262 std::string Str;
3263 A.getObjCEncodingForType(E->getEncodedType(), Str);
3264 StringLiteral *SL =
3266 /*Pascal=*/false, E->getType(), E->getAtLoc());
3267 return this->delegate(SL);
3268}
3269
3270template <class Emitter>
3272 const SYCLUniqueStableNameExpr *E) {
3273 if (DiscardResult)
3274 return true;
3275
3276 assert(!Initializing);
3277
3278 auto &A = Ctx.getASTContext();
3279 std::string ResultStr = E->ComputeName(A);
3280
3281 QualType CharTy = A.CharTy.withConst();
3282 APInt Size(A.getTypeSize(A.getSizeType()), ResultStr.size() + 1);
3283 QualType ArrayTy = A.getConstantArrayType(CharTy, Size, nullptr,
3285
3286 StringLiteral *SL =
3288 /*Pascal=*/false, ArrayTy, E->getLocation());
3289 return this->emitGetStringPtr(SL, E);
3290}
3291
3292template <class Emitter>
3294 if (DiscardResult)
3295 return true;
3296 return this->emitConst(E->getValue(), E);
3297}
3298
3299template <class Emitter>
3301 const CompoundAssignOperator *E) {
3302
3303 const Expr *LHS = E->getLHS();
3304 const Expr *RHS = E->getRHS();
3305 QualType LHSType = LHS->getType();
3306 QualType LHSComputationType = E->getComputationLHSType();
3307 QualType ResultType = E->getComputationResultType();
3308 OptPrimType LT = classify(LHSComputationType);
3309 OptPrimType RT = classify(ResultType);
3310
3311 assert(ResultType->isFloatingType());
3312
3313 if (!LT || !RT)
3314 return false;
3315
3316 PrimType LHST = classifyPrim(LHSType);
3317
3318 if (isSideEffectFree(RHS)) {
3319 if (!visit(LHS))
3320 return false;
3321 if (!this->emitLoad(LHST, E))
3322 return false;
3323 // If necessary, convert LHS to its computation type.
3324 if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
3325 LHSComputationType, E))
3326 return false;
3327 if (!visit(RHS))
3328 return false;
3329
3330 } else {
3331 // C++17 onwards require that we evaluate the RHS first.
3332 // Compute RHS and save it in a temporary variable so we can
3333 // load it again later.
3334 if (!visit(RHS))
3335 return false;
3336
3337 unsigned TempOffset =
3338 this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
3339 if (!this->emitSetLocal(*RT, TempOffset, E))
3340 return false;
3341
3342 // First, visit LHS.
3343 if (!visit(LHS))
3344 return false;
3345 if (!this->emitLoad(LHST, E))
3346 return false;
3347
3348 // If necessary, convert LHS to its computation type.
3349 if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
3350 LHSComputationType, E))
3351 return false;
3352
3353 // Now load RHS.
3354 if (!this->emitGetLocal(*RT, TempOffset, E))
3355 return false;
3356 }
3357
3358 switch (E->getOpcode()) {
3359 case BO_AddAssign:
3360 if (!this->emitAddf(getFPOptions(E), E))
3361 return false;
3362 break;
3363 case BO_SubAssign:
3364 if (!this->emitSubf(getFPOptions(E), E))
3365 return false;
3366 break;
3367 case BO_MulAssign:
3368 if (!this->emitMulf(getFPOptions(E), E))
3369 return false;
3370 break;
3371 case BO_DivAssign:
3372 if (!this->emitDivf(getFPOptions(E), E))
3373 return false;
3374 break;
3375 default:
3376 return false;
3377 }
3378
3379 if (!this->emitPrimCast(classifyPrim(ResultType), LHST, LHS->getType(), E))
3380 return false;
3381
3382 if (DiscardResult)
3383 return this->emitStorePop(LHST, E);
3384 return this->emitStore(LHST, E);
3385}
3386
3387template <class Emitter>
3389 const CompoundAssignOperator *E) {
3390 BinaryOperatorKind Op = E->getOpcode();
3391 const Expr *LHS = E->getLHS();
3392 const Expr *RHS = E->getRHS();
3393 OptPrimType LT = classify(LHS->getType());
3394 OptPrimType RT = classify(RHS->getType());
3395
3396 if (Op != BO_AddAssign && Op != BO_SubAssign)
3397 return false;
3398
3399 if (!LT || !RT)
3400 return false;
3401
3402 if (!visit(LHS))
3403 return false;
3404
3405 if (!this->emitLoad(*LT, LHS))
3406 return false;
3407
3408 if (!visit(RHS))
3409 return false;
3410
3411 if (Op == BO_AddAssign) {
3412 if (!this->emitAddOffset(*RT, E))
3413 return false;
3414 } else {
3415 if (!this->emitSubOffset(*RT, E))
3416 return false;
3417 }
3418
3419 if (DiscardResult)
3420 return this->emitStorePopPtr(E);
3421 return this->emitStorePtr(E);
3422}
3423
3424template <class Emitter>
3426 const CompoundAssignOperator *E) {
3427 if (E->getType()->isVectorType())
3428 return VisitVectorBinOp(E);
3429
3430 const Expr *LHS = E->getLHS();
3431 const Expr *RHS = E->getRHS();
3432 OptPrimType LHSComputationT = classify(E->getComputationLHSType());
3433 OptPrimType LT = classify(LHS->getType());
3434 OptPrimType RT = classify(RHS->getType());
3435 OptPrimType ResultT = classify(E->getType());
3436
3437 if (!Ctx.getLangOpts().CPlusPlus14)
3438 return this->visit(RHS) && this->visit(LHS) && this->emitError(E);
3439
3440 if (!LT || !RT || !ResultT || !LHSComputationT)
3441 return false;
3442
3443 // Handle floating point operations separately here, since they
3444 // require special care.
3445 if (ResultT == PT_Float || RT == PT_Float)
3447
3448 if (E->getType()->isPointerType())
3450
3451 assert(!E->getType()->isPointerType() && "Handled above");
3452 assert(!E->getType()->isFloatingType() && "Handled above");
3453
3454 if (isSideEffectFree(RHS)) {
3455 if (!visit(LHS))
3456 return false;
3457 if (!this->emitLoad(*LT, E))
3458 return false;
3459 if (LT != LHSComputationT &&
3460 !this->emitIntegralCast(*LT, *LHSComputationT,
3461 E->getComputationLHSType(), E))
3462 return false;
3463 if (!visit(RHS))
3464 return false;
3465 } else {
3466 // C++17 onwards require that we evaluate the RHS first.
3467 // Compute RHS and save it in a temporary variable so we can
3468 // load it again later.
3469 // FIXME: Compound assignments are unsequenced in C, so we might
3470 // have to figure out how to reject them.
3471 if (!visit(RHS))
3472 return false;
3473
3474 unsigned TempOffset =
3475 this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
3476
3477 if (!this->emitSetLocal(*RT, TempOffset, E))
3478 return false;
3479
3480 // Get LHS pointer, load its value and cast it to the
3481 // computation type if necessary.
3482 if (!visit(LHS))
3483 return false;
3484 if (!this->emitLoad(*LT, E))
3485 return false;
3486 if (LT != LHSComputationT &&
3487 !this->emitIntegralCast(*LT, *LHSComputationT,
3488 E->getComputationLHSType(), E))
3489 return false;
3490
3491 // Get the RHS value on the stack.
3492 if (!this->emitGetLocal(*RT, TempOffset, E))
3493 return false;
3494 }
3495
3496 // Perform operation.
3497 switch (E->getOpcode()) {
3498 case BO_AddAssign:
3499 if (!this->emitAdd(*LHSComputationT, E))
3500 return false;
3501 break;
3502 case BO_SubAssign:
3503 if (!this->emitSub(*LHSComputationT, E))
3504 return false;
3505 break;
3506 case BO_MulAssign:
3507 if (!this->emitMul(*LHSComputationT, E))
3508 return false;
3509 break;
3510 case BO_DivAssign:
3511 if (!this->emitDiv(*LHSComputationT, E))
3512 return false;
3513 break;
3514 case BO_RemAssign:
3515 if (!this->emitRem(*LHSComputationT, E))
3516 return false;
3517 break;
3518 case BO_ShlAssign:
3519 if (!this->emitShl(*LHSComputationT, *RT, E))
3520 return false;
3521 break;
3522 case BO_ShrAssign:
3523 if (!this->emitShr(*LHSComputationT, *RT, E))
3524 return false;
3525 break;
3526 case BO_AndAssign:
3527 if (!this->emitBitAnd(*LHSComputationT, E))
3528 return false;
3529 break;
3530 case BO_XorAssign:
3531 if (!this->emitBitXor(*LHSComputationT, E))
3532 return false;
3533 break;
3534 case BO_OrAssign:
3535 if (!this->emitBitOr(*LHSComputationT, E))
3536 return false;
3537 break;
3538 default:
3539 llvm_unreachable("Unimplemented compound assign operator");
3540 }
3541
3542 // And now cast from LHSComputationT to ResultT.
3543 if (ResultT != LHSComputationT &&
3544 !this->emitIntegralCast(*LHSComputationT, *ResultT, E->getType(), E))
3545 return false;
3546
3547 // And store the result in LHS.
3548 if (DiscardResult) {
3549 if (LHS->refersToBitField())
3550 return this->emitStoreBitFieldPop(*ResultT, E);
3551 return this->emitStorePop(*ResultT, E);
3552 }
3553 if (LHS->refersToBitField())
3554 return this->emitStoreBitField(*ResultT, E);
3555 return this->emitStore(*ResultT, E);
3556}
3557
3558template <class Emitter>
3561 const Expr *SubExpr = E->getSubExpr();
3562
3563 return this->delegate(SubExpr) && ES.destroyLocals(E);
3564}
3565
3566template <class Emitter>
3568 const MaterializeTemporaryExpr *E) {
3569 if (Initializing) {
3570 // We already have a value, just initialize that.
3571 return this->delegate(E->getSubExpr());
3572 }
3573 // If we don't end up using the materialized temporary anyway, don't
3574 // bother creating it.
3575 if (DiscardResult)
3576 return this->discard(E->getSubExpr());
3577
3580 const Expr *Inner;
3581 if (!Ctx.getLangOpts().CPlusPlus11)
3582 Inner =
3583 E->getSubExpr()->skipRValueSubobjectAdjustments(CommaLHSs, Adjustments);
3584 else
3585 Inner = E->getSubExpr();
3586
3587 // If we passed any comma operators, evaluate their LHSs.
3588 for (const Expr *LHS : CommaLHSs) {
3589 if (!this->discard(LHS))
3590 return false;
3591 }
3592
3593 // FIXME: Find a test case where Adjustments matters.
3594
3595 // When we're extending a global variable *or* the storage duration of
3596 // the temporary is explicitly static, create a global variable.
3597 OptPrimType InnerT = classify(Inner);
3598 const ValueDecl *ExtendingDecl = E->getExtendingDecl();
3599 bool IsStatic = E->getStorageDuration() == SD_Static;
3600 if (IsStatic ||
3601 (ExtendingDecl && Context::shouldBeGloballyIndexed(ExtendingDecl))) {
3602
3603 if (this->constantFolding())
3604 return false;
3605
3606 UnsignedOrNone GlobalIndex = P.createGlobal(E, Inner->getType());
3607 if (!GlobalIndex)
3608 return false;
3609
3610 const LifetimeExtendedTemporaryDecl *TempDecl =
3612
3613 if (InnerT) {
3614 if (!this->visit(Inner))
3615 return false;
3616
3617 if (IsStatic) {
3618 assert(TempDecl);
3619 if (!this->emitInitGlobalTemp(*InnerT, *GlobalIndex, TempDecl, E))
3620 return false;
3621 } else {
3622 if (!this->emitInitGlobal(*InnerT, *GlobalIndex, E))
3623 return false;
3624 }
3625 return this->emitGetPtrGlobal(*GlobalIndex, E);
3626 }
3627
3628 if (!this->checkLiteralType(Inner))
3629 return false;
3630 // Non-primitive values.
3631 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3632 return false;
3633 if (!this->visitInitializer(Inner))
3634 return false;
3635 if (IsStatic) {
3636 assert(TempDecl);
3637 return this->emitInitGlobalTempComp(TempDecl, E);
3638 }
3639 return true;
3640 }
3641
3645
3646 // For everyhing else, use local variables.
3647 if (InnerT) {
3648 bool IsConst = Inner->getType().isConstQualified();
3649 bool IsVolatile = Inner->getType().isVolatileQualified();
3650 unsigned LocalIndex =
3651 allocateLocalPrimitive(E, *InnerT, IsConst, IsVolatile, VarScope);
3652 if (!this->VarScope->LocalsAlwaysEnabled &&
3653 !this->emitEnableLocal(LocalIndex, E))
3654 return false;
3655
3656 if (!this->visit(Inner))
3657 return false;
3658 if (!this->emitSetLocal(*InnerT, LocalIndex, E))
3659 return false;
3660
3661 return this->emitGetPtrLocal(LocalIndex, E);
3662 }
3663
3664 if (!this->checkLiteralType(Inner))
3665 return false;
3666
3667 if (UnsignedOrNone LocalIndex =
3668 allocateLocal(E, Inner->getType(), VarScope)) {
3669 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
3670
3671 if (!this->VarScope->LocalsAlwaysEnabled &&
3672 !this->emitEnableLocal(*LocalIndex, E))
3673 return false;
3674
3675 if (!this->emitGetPtrLocal(*LocalIndex, E))
3676 return false;
3677 return this->visitInitializer(Inner);
3678 }
3679 return false;
3680}
3681
3682template <class Emitter>
3684 const CXXBindTemporaryExpr *E) {
3685 const Expr *SubExpr = E->getSubExpr();
3686
3687 if (Initializing)
3688 return this->delegate(SubExpr);
3689
3690 // Make sure we create a temporary even if we're discarding, since that will
3691 // make sure we will also call the destructor.
3692
3693 if (!this->visit(SubExpr))
3694 return false;
3695
3696 if (DiscardResult)
3697 return this->emitPopPtr(E);
3698 return true;
3699}
3700
3701template <class Emitter>
3703 const Expr *Init = E->getInitializer();
3704 if (DiscardResult)
3705 return this->discard(Init);
3706
3707 if (Initializing) {
3708 // We already have a value, just initialize that.
3709 return this->visitInitializer(Init);
3710 }
3711
3712 OptPrimType T = classify(E->getType());
3713 if (E->isFileScope()) {
3714 // Avoid creating a variable if this is a primitive RValue anyway.
3715 if (T && !E->isLValue())
3716 return this->delegate(Init);
3717
3718 UnsignedOrNone GlobalIndex = P.createGlobal(E, E->getType());
3719 if (!GlobalIndex)
3720 return false;
3721
3722 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3723 return false;
3724
3725 // Since this is a global variable, we might've already seen,
3726 // don't do it again.
3727 if (P.isGlobalInitialized(*GlobalIndex))
3728 return true;
3729
3730 if (T) {
3731 if (!this->visit(Init))
3732 return false;
3733 return this->emitInitGlobal(*T, *GlobalIndex, E);
3734 }
3735
3736 return this->visitInitializer(Init);
3737 }
3738
3739 // Otherwise, use a local variable.
3740 if (T && !E->isLValue()) {
3741 // For primitive types, we just visit the initializer.
3742 return this->delegate(Init);
3743 }
3744
3745 unsigned LocalIndex;
3746 if (T)
3747 LocalIndex = this->allocateLocalPrimitive(Init, *T, /*IsConst=*/false);
3748 else if (UnsignedOrNone MaybeIndex = this->allocateLocal(Init))
3749 LocalIndex = *MaybeIndex;
3750 else
3751 return false;
3752
3753 if (!this->emitGetPtrLocal(LocalIndex, E))
3754 return false;
3755
3756 if (T)
3757 return this->visit(Init) && this->emitInit(*T, E);
3758 return this->visitInitializer(Init);
3759}
3760
3761template <class Emitter>
3763 if (DiscardResult)
3764 return true;
3765 if (E->isStoredAsBoolean()) {
3766 if (E->getType()->isBooleanType())
3767 return this->emitConstBool(E->getBoolValue(), E);
3768 return this->emitConst(E->getBoolValue(), E);
3769 }
3770 if (E->isStoredAsComparisonResult()) {
3771 const ComparisonCategoryInfo &CmpInfo =
3772 Ctx.getASTContext().CompCategories.getInfoForType(E->getType());
3773 const auto Result =
3774 ComparisonCategoryResult(E->getAPValue().getInt().getZExtValue());
3775 const Record *R = getRecord(E->getType());
3776 if (!R || R->getNumFields() == 0)
3777 return false;
3778 const Record::Field *Field = R->getField(0U);
3779 assert(Field->T);
3780 if (!this->emitConst(CmpInfo.getValueInfo(Result)->getIntValue(), *Field->T,
3781 E))
3782 return false;
3783 return this->emitInitField(*Field->T, Field->Offset, E);
3784 }
3785
3787 return this->visitAPValue(E->getAPValue(), T, E);
3788}
3789
3790template <class Emitter>
3792 if (DiscardResult)
3793 return true;
3794 return this->emitConst(E->getValue(), E);
3795}
3796
3797template <class Emitter>
3799 if (DiscardResult)
3800 return true;
3801
3802 assert(Initializing);
3803 const Record *R = P.getOrCreateRecord(E->getLambdaClass());
3804 if (!R)
3805 return false;
3806
3807 auto *CaptureInitIt = E->capture_init_begin();
3808 // Initialize all fields (which represent lambda captures) of the
3809 // record with their initializers.
3810 for (const Record::Field &F : R->fields()) {
3811 const Expr *Init = *CaptureInitIt;
3812 if (!Init || Init->containsErrors())
3813 continue;
3814 ++CaptureInitIt;
3815
3816 if (OptPrimType T = classify(Init)) {
3817 if (!this->visit(Init))
3818 return false;
3819
3820 if (!this->emitInitField(*T, F.Offset, E))
3821 return false;
3822 } else {
3823 if (!this->emitGetPtrField(F.Offset, E))
3824 return false;
3825
3826 if (!this->visitInitializerPop(Init))
3827 return false;
3828 }
3829 }
3830
3831 return true;
3832}
3833
3834template <class Emitter>
3836 if (DiscardResult)
3837 return true;
3838
3839 if (!Initializing)
3840 return this->emitGetStringPtr(E, E);
3841 return this->delegate(E->getFunctionName());
3842}
3843
3844template <class Emitter>
3846 if (E->getSubExpr() && !this->discard(E->getSubExpr()))
3847 return false;
3848
3849 return this->emitInvalid(E);
3850}
3851
3852template <class Emitter>
3854 const CXXReinterpretCastExpr *E) {
3855 const Expr *SubExpr = E->getSubExpr();
3856
3857 OptPrimType FromT = classify(SubExpr);
3858 OptPrimType ToT = classify(E);
3859
3860 if (!FromT || !ToT)
3861 return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);
3862
3863 if (FromT == PT_Ptr || ToT == PT_Ptr) {
3866 if (!this->emitInvalidCast(CastKind, /*Fatal=*/false, E))
3867 return false;
3868 if (E->getCastKind() == CK_LValueBitCast)
3869 return this->delegate(SubExpr);
3870 return this->VisitCastExpr(E);
3871 }
3872
3873 // Try to actually do the cast.
3874 bool Fatal = (ToT != FromT);
3875 if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
3876 return false;
3877
3878 return this->VisitCastExpr(E);
3879}
3880
3881template <class Emitter>
3883 if (!Ctx.getLangOpts().CPlusPlus20) {
3884 if (!this->emitInvalidCast(CastKind::Dynamic, /*Fatal=*/false, E))
3885 return false;
3886 }
3887
3888 if (E->getCastKind() != CK_Dynamic)
3889 return this->VisitCastExpr(E);
3890
3891 QualType DestType = E->getType();
3892 // "target type must be a reference or pointer type to a defined class"
3893 if (DestType->isRecordType()) {
3894 assert(E->isGLValue());
3895 } else {
3896 assert(DestType->isPointerOrReferenceType());
3897 assert(DestType->isVoidPointerType() ||
3898 DestType->getPointeeType()->isRecordType());
3899 DestType = DestType->getPointeeType();
3900 }
3901
3902 if (!this->visit(E->getSubExpr()))
3903 return false;
3904 if (!this->emitDynamicCast(DestType.getTypePtr(),
3905 /*IsReferenceCast=*/E->isGLValue(), E))
3906 return false;
3907
3908 if (DiscardResult)
3909 return this->emitPopPtr(E);
3910 return true;
3911}
3912
3913template <class Emitter>
3915 assert(E->getType()->isBooleanType());
3916
3917 if (DiscardResult)
3918 return true;
3919 return this->emitConstBool(E->getValue(), E);
3920}
3921
3922template <class Emitter>
3924 QualType T = E->getType();
3925 assert(!canClassify(T));
3926
3927 if (T->isRecordType()) {
3928 const CXXConstructorDecl *Ctor = E->getConstructor();
3929
3930 // If we're discarding a construct expression, we still need
3931 // to allocate a variable and call the constructor and destructor.
3932 if (DiscardResult) {
3933 if (Ctor->isTrivial())
3934 return true;
3935 assert(!Initializing);
3936 UnsignedOrNone LocalIndex = allocateLocal(E);
3937
3938 if (!LocalIndex)
3939 return false;
3940
3941 if (!this->emitGetPtrLocal(*LocalIndex, E))
3942 return false;
3943 }
3944
3945 // Trivial copy/move constructor. Avoid copy.
3946 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
3947 Ctor->isTrivial() &&
3948 E->getArg(0)->isTemporaryObject(Ctx.getASTContext(),
3949 T->castAsCXXRecordDecl()))
3950 return this->visitInitializer(E->getArg(0));
3951
3952 // Zero initialization.
3953 bool ZeroInit = E->requiresZeroInitialization();
3954 if (ZeroInit) {
3955 const Record *R = getRecord(E->getType());
3956 if (!R)
3957 return false;
3958
3959 if (!this->visitZeroRecordInitializer(R, E))
3960 return false;
3961
3962 // If the constructor is trivial anyway, we're done.
3963 if (Ctor->isTrivial())
3964 return true;
3965 }
3966
3967 // Trivial default constructors might never be implicitly defined by the
3968 // AST, so we need to special-case them here.
3969 if (Ctor->isTrivial() && Ctor->isDefaultConstructor()) {
3970 if (!this->emitDefaultInit(Ctor, E))
3971 return false;
3972 if (DiscardResult)
3973 return this->emitPopPtr(E);
3974 return true;
3975 }
3976
3977 // Avoid materializing a temporary for an elidable copy/move constructor.
3978 if (!ZeroInit && E->isElidable()) {
3979 const Expr *SrcObj = E->getArg(0);
3980 assert(SrcObj->isTemporaryObject(Ctx.getASTContext(), Ctor->getParent()));
3981 assert(Ctx.getASTContext().hasSameUnqualifiedType(E->getType(),
3982 SrcObj->getType()));
3983 if (const auto *ME = dyn_cast<MaterializeTemporaryExpr>(SrcObj)) {
3984 if (!this->emitCheckFunctionDecl(Ctor, E))
3985 return false;
3986 return this->visitInitializer(ME->getSubExpr());
3987 }
3988 }
3989
3990 const Function *Func = getFunction(Ctor);
3991
3992 if (!Func)
3993 return false;
3994
3995 assert(Func->hasThisPointer());
3996 assert(!Func->hasRVO());
3997
3998 // The This pointer is already on the stack because this is an initializer,
3999 // but we need to dup() so the call() below has its own copy.
4000 if (!this->emitDupPtr(E))
4001 return false;
4002
4003 // Constructor arguments.
4004 for (const auto *Arg : E->arguments()) {
4005 if (!this->visit(Arg))
4006 return false;
4007 }
4008
4009 if (Func->isVariadic()) {
4010 uint32_t VarArgSize = 0;
4011 unsigned NumParams = Func->getNumWrittenParams();
4012 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) {
4013 VarArgSize +=
4014 align(primSize(classify(E->getArg(I)->getType()).value_or(PT_Ptr)));
4015 }
4016 if (!this->emitCallVar(Func, VarArgSize, E))
4017 return false;
4018 } else {
4019 if (!this->emitCall(Func, 0, E)) {
4020 // When discarding, we don't need the result anyway, so clean up
4021 // the instance dup we did earlier in case surrounding code wants
4022 // to keep evaluating.
4023 if (DiscardResult)
4024 (void)this->emitPopPtr(E);
4025 return false;
4026 }
4027 }
4028
4029 if (DiscardResult)
4030 return this->emitPopPtr(E);
4031 return true;
4032 }
4033
4034 if (T->isArrayType()) {
4035 const Function *Func = getFunction(E->getConstructor());
4036 if (!Func)
4037 return false;
4038
4039 if (!this->emitDupPtr(E))
4040 return false;
4041
4042 std::function<bool(QualType)> initArrayDimension;
4043 initArrayDimension = [&](QualType T) -> bool {
4044 if (!T->isArrayType()) {
4045 // Constructor arguments.
4046 for (const auto *Arg : E->arguments()) {
4047 if (!this->visit(Arg))
4048 return false;
4049 }
4050
4051 return this->emitCall(Func, 0, E);
4052 }
4053
4054 const ConstantArrayType *CAT =
4055 Ctx.getASTContext().getAsConstantArrayType(T);
4056 if (!CAT)
4057 return false;
4058 QualType ElemTy = CAT->getElementType();
4059 uint64_t NumElems = CAT->getZExtSize();
4060 if (exceedsArraySizeLimit(Ctx.getLangOpts(), NumElems))
4061 return this->emitCheckArraySize(NumElems, E);
4062 for (uint64_t I = 0; I != NumElems; ++I) {
4063 if (!this->emitConstUint64(I, E))
4064 return false;
4065 if (!this->emitArrayElemPtrUint64(E))
4066 return false;
4067 if (!initArrayDimension(ElemTy))
4068 return false;
4069 }
4070 return this->emitPopPtr(E);
4071 };
4072
4073 return initArrayDimension(E->getType());
4074 }
4075
4076 return false;
4077}
4078
4079template <class Emitter>
4081 if (DiscardResult)
4082 return true;
4083
4084 const APValue Val =
4085 E->EvaluateInContext(Ctx.getASTContext(), SourceLocDefaultExpr);
4086
4087 // Things like __builtin_LINE().
4088 if (E->getType()->isIntegerType()) {
4089 assert(Val.isInt());
4090 const APSInt &I = Val.getInt();
4091 return this->emitConst(I, E);
4092 }
4093 // Otherwise, the APValue is an LValue, with only one element.
4094 // Theoretically, we don't need the APValue at all of course.
4095 assert(E->getType()->isPointerType());
4096 assert(Val.isLValue());
4097 const APValue::LValueBase &Base = Val.getLValueBase();
4098 if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>())
4099 return this->visit(LValueExpr);
4100
4101 // Otherwise, we have a decl (which is the case for
4102 // __builtin_source_location).
4103 assert(Base.is<const ValueDecl *>());
4104 assert(Val.getLValuePath().size() == 0);
4105 const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>();
4106 assert(BaseDecl);
4107
4108 auto *UGCD = cast<UnnamedGlobalConstantDecl>(BaseDecl);
4109
4110 UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(UGCD);
4111 if (!GlobalIndex)
4112 return false;
4113
4114 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
4115 return false;
4116
4117 const Record *R = getRecord(E->getType());
4118 const APValue &V = UGCD->getValue();
4119 for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) {
4120 const Record::Field *F = R->getField(I);
4121 const APValue &FieldValue = V.getStructField(I);
4122
4123 if (!this->visitAPValue(FieldValue, *F->T, E))
4124 return false;
4125 if (!this->emitInitField(*F->T, F->Offset, E))
4126 return false;
4127 }
4128
4129 // Leave the pointer to the global on the stack.
4130 return true;
4131}
4132
4133template <class Emitter>
4135 unsigned N = E->getNumComponents();
4136 if (N == 0)
4137 return false;
4138
4139 for (unsigned I = 0; I != N; ++I) {
4140 const OffsetOfNode &Node = E->getComponent(I);
4141 if (Node.getKind() == OffsetOfNode::Array) {
4142 const Expr *ArrayIndexExpr = E->getIndexExpr(Node.getArrayExprIndex());
4143 PrimType IndexT = classifyPrim(ArrayIndexExpr->getType());
4144
4145 if (DiscardResult) {
4146 if (!this->discard(ArrayIndexExpr))
4147 return false;
4148 continue;
4149 }
4150
4151 if (IndexT == PT_IntAP || IndexT == PT_IntAPS) {
4152 if (!this->visit(ArrayIndexExpr))
4153 return false;
4154 if (!this->emitCastAPToOffsetIndex(IndexT, E))
4155 return false;
4156 continue;
4157 }
4158 if (!this->visit(ArrayIndexExpr))
4159 return false;
4160 // Cast to Sint64.
4161 if (IndexT != PT_Sint64) {
4162 if (!this->emitCast(IndexT, PT_Sint64, E))
4163 return false;
4164 }
4165 }
4166 }
4167
4168 if (DiscardResult)
4169 return true;
4170
4172 return this->emitOffsetOf(T, E, E);
4173}
4174
4175template <class Emitter>
4177 const CXXScalarValueInitExpr *E) {
4178 QualType Ty = E->getType();
4179
4180 if (DiscardResult || Ty->isVoidType())
4181 return true;
4182
4183 if (OptPrimType T = classify(Ty))
4184 return this->visitZeroInitializer(*T, Ty, E);
4185
4186 if (Ty->isAnyComplexType() || Ty->isVectorType()) {
4187 if (!Initializing) {
4188 UnsignedOrNone LocalIndex = allocateLocal(E);
4189 if (!LocalIndex)
4190 return false;
4191 if (!this->emitGetPtrLocal(*LocalIndex, E))
4192 return false;
4193 }
4194
4195 QualType ElemQT;
4196 unsigned NumElems;
4197 if (const auto *CT = Ty->getAs<ComplexType>()) {
4198 NumElems = 2;
4199 ElemQT = CT->getElementType();
4200 } else {
4201 const auto *VT = Ty->castAs<VectorType>();
4202 NumElems = VT->getNumElements();
4203 ElemQT = VT->getElementType();
4204 }
4205
4206 PrimType ElemT = classifyPrim(ElemQT);
4207
4208 // Initialize all fields to 0.
4209 for (unsigned I = 0; I != NumElems; ++I) {
4210 if (!this->visitZeroInitializer(ElemT, ElemQT, E))
4211 return false;
4212 if (!this->emitInitElem(ElemT, I, E))
4213 return false;
4214 }
4215 return true;
4216 }
4217
4218 return false;
4219}
4220
4221template <class Emitter>
4223 return this->emitConst(E->getPackLength(), E);
4224}
4225
4226template <class Emitter>
4231
4232template <class Emitter>
4234 return this->delegate(E->getChosenSubExpr());
4235}
4236
4237template <class Emitter>
4239 if (DiscardResult)
4240 return true;
4241
4242 return this->emitConst(E->getValue(), E);
4243}
4244
4245template <class Emitter>
4247 const CXXInheritedCtorInitExpr *E) {
4248 const CXXConstructorDecl *Ctor = E->getConstructor();
4249
4250 if (Ctor->isTrivial())
4251 return true;
4252
4253 const Function *F = this->getFunction(Ctor);
4254 if (!F)
4255 return false;
4256 assert(!F->hasRVO());
4257 assert(F->hasThisPointer());
4258
4259 if (!this->emitDupPtr(SourceInfo{}))
4260 return false;
4261
4262 // Forward all arguments of the current function (which should be a
4263 // constructor itself) to the inherited ctor.
4264 // This is necessary because the calling code has pushed the pointer
4265 // of the correct base for us already, but the arguments need
4266 // to come after.
4267 unsigned ParamIndex = 0;
4268 for (const ParmVarDecl *PD : Ctor->parameters()) {
4269 PrimType PT = this->classify(PD->getType()).value_or(PT_Ptr);
4270
4271 if (!this->emitGetParam(PT, ParamIndex, E))
4272 return false;
4273 ++ParamIndex;
4274 }
4275
4276 return this->emitCall(F, 0, E);
4277}
4278
4279// FIXME: This function has become rather unwieldy, especially
4280// the part where we initialize an array allocation of dynamic size.
4281template <class Emitter>
4283 assert(classifyPrim(E->getType()) == PT_Ptr);
4284 const Expr *Init = E->getInitializer();
4285 QualType ElementType = E->getAllocatedType();
4286 OptPrimType ElemT = classify(ElementType);
4287 unsigned PlacementArgs = E->getNumPlacementArgs();
4288 const FunctionDecl *OperatorNew = E->getOperatorNew();
4289 const Expr *PlacementDest = nullptr;
4290 bool IsNoThrow = false;
4291
4292 if (E->containsErrors())
4293 return false;
4294
4295 if (PlacementArgs != 0) {
4296 // FIXME: There is no restriction on this, but it's not clear that any
4297 // other form makes any sense. We get here for cases such as:
4298 //
4299 // new (std::align_val_t{N}) X(int)
4300 //
4301 // (which should presumably be valid only if N is a multiple of
4302 // alignof(int), and in any case can't be deallocated unless N is
4303 // alignof(X) and X has new-extended alignment).
4304 if (PlacementArgs == 1) {
4305 const Expr *Arg1 = E->getPlacementArg(0);
4306 if (OperatorNew->isReservedGlobalPlacementOperator()) {
4307 if (!this->emitCheckPlacementNew(E, E))
4308 return false;
4309 PlacementDest = Arg1;
4310 } else if (
4311 Arg1->getType()->isNothrowT() &&
4312 OperatorNew
4313 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation()) {
4314 if (!this->discard(Arg1))
4315 return false;
4316 IsNoThrow = true;
4317 } else {
4318 // Any other placement list is invalid. This includes a user-declared
4319 // allocation function taking std::nothrow_t, e.g. by value.
4320 return this->emitInvalidNewDeleteExpr(E, E);
4321 }
4322 } else {
4323 // Always invalid.
4324 return this->emitInvalid(E);
4325 }
4326 } else if (!OperatorNew
4327 ->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
4328 return this->emitInvalidNewDeleteExpr(E, E);
4329
4330 const Descriptor *Desc;
4331 if (!PlacementDest) {
4332 if (ElemT) {
4333 if (E->isArray())
4334 Desc = nullptr; // We're not going to use it in this case.
4335 else
4336 Desc = P.createDescriptor(E, *ElemT);
4337 } else {
4338 Desc = P.createDescriptor(E, ElementType.getTypePtr(), /*IsConst=*/false,
4339 /*IsTemporary=*/false, /*IsMutable=*/false,
4340 /*IsVolatile=*/false, Init);
4341 }
4342 }
4343
4344 if (E->isArray()) {
4345 std::optional<const Expr *> ArraySizeExpr = E->getArraySize();
4346 if (!ArraySizeExpr)
4347 return false;
4348
4349 const Expr *Stripped = *ArraySizeExpr;
4350 for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);
4351 Stripped = ICE->getSubExpr())
4352 if (ICE->getCastKind() != CK_NoOp &&
4353 ICE->getCastKind() != CK_IntegralCast)
4354 break;
4355
4356 PrimType SizeT = classifyPrim(Stripped->getType());
4357
4358 // Save evaluated array size to a variable.
4359 unsigned ArrayLen =
4360 allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
4361 if (!this->visit(Stripped))
4362 return false;
4363 if (!this->emitSetLocal(SizeT, ArrayLen, E))
4364 return false;
4365
4366 if (PlacementDest) {
4367 if (!this->visit(PlacementDest))
4368 return false;
4369 if (!this->emitGetLocal(SizeT, ArrayLen, E))
4370 return false;
4371 if (!this->emitCheckNewTypeMismatchArray(SizeT, E, E))
4372 return false;
4373 } else {
4374 if (!this->emitGetLocal(SizeT, ArrayLen, E))
4375 return false;
4376
4377 if (ElemT) {
4378 // N primitive elements.
4379 if (!this->emitAllocN(SizeT, *ElemT, E, IsNoThrow, E))
4380 return false;
4381 } else {
4382 // N Composite elements.
4383 if (!this->emitAllocCN(SizeT, Desc, IsNoThrow, E))
4384 return false;
4385 }
4386 }
4387
4388 if (Init) {
4389 QualType InitType = Init->getType();
4390 size_t StaticInitElems = 0;
4391 const Expr *DynamicInit = nullptr;
4392 OptPrimType ElemT;
4393
4394 if (const ConstantArrayType *CAT =
4395 Ctx.getASTContext().getAsConstantArrayType(InitType)) {
4396 StaticInitElems = CAT->getZExtSize();
4397 // Initialize the first S element from the initializer.
4398 if (!this->visitInitializer(Init))
4399 return false;
4400
4401 if (const auto *ILE = dyn_cast<InitListExpr>(Init)) {
4402 if (ILE->hasArrayFiller())
4403 DynamicInit = ILE->getArrayFiller();
4404 else if (StaticInitElems > 0 && isa<StringLiteral>(ILE->getInit(0)))
4405 ElemT = classifyPrim(CAT->getElementType());
4406 }
4407 }
4408
4409 // The initializer initializes a certain number of elements, S.
4410 // However, the complete number of elements, N, might be larger than that.
4411 // In this case, we need to get an initializer for the remaining elements.
4412 // There are three cases:
4413 // 1) For the form 'new Struct[n];', the initializer is a
4414 // CXXConstructExpr and its type is an IncompleteArrayType.
4415 // 2) For the form 'new Struct[n]{1,2,3}', the initializer is an
4416 // InitListExpr and the initializer for the remaining elements
4417 // is the array filler.
4418 // 3) StringLiterals don't have an array filler, so we need to zero
4419 // the remaining elements.
4420
4421 if (DynamicInit || ElemT || InitType->isIncompleteArrayType()) {
4422 const Function *CtorFunc = nullptr;
4423 if (const auto *CE = dyn_cast<CXXConstructExpr>(Init)) {
4424 CtorFunc = getFunction(CE->getConstructor());
4425 if (!CtorFunc)
4426 return false;
4427 } else if (!DynamicInit && !ElemT)
4428 DynamicInit = Init;
4429
4430 LabelTy EndLabel = this->getLabel();
4431 LabelTy StartLabel = this->getLabel();
4432
4433 // In the nothrow case, the alloc above might have returned nullptr.
4434 // Don't call any constructors that case.
4435 if (IsNoThrow) {
4436 if (!this->emitDupPtr(E))
4437 return false;
4438 if (!this->emitIsNonNullPtr(E))
4439 return false;
4440 if (!this->jumpFalse(EndLabel, E))
4441 return false;
4442 }
4443
4444 // Create loop variables.
4445 unsigned Iter =
4446 allocateLocalPrimitive(Stripped, SizeT, /*IsConst=*/false);
4447 if (!this->emitConst(StaticInitElems, SizeT, E))
4448 return false;
4449 if (!this->emitSetLocal(SizeT, Iter, E))
4450 return false;
4451
4452 this->fallthrough(StartLabel);
4453 this->emitLabel(StartLabel);
4454 // Condition. Iter < ArrayLen?
4455 if (!this->emitGetLocal(SizeT, Iter, E))
4456 return false;
4457 if (!this->emitGetLocal(SizeT, ArrayLen, E))
4458 return false;
4459 if (!this->emitLT(SizeT, E))
4460 return false;
4461 if (!this->jumpFalse(EndLabel, E))
4462 return false;
4463
4464 // Pointer to the allocated array is already on the stack.
4465 if (!this->emitGetLocal(SizeT, Iter, E))
4466 return false;
4467 if (!this->emitArrayElemPtr(SizeT, E))
4468 return false;
4469
4470 if (isa_and_nonnull<ImplicitValueInitExpr>(DynamicInit) &&
4471 DynamicInit->getType()->isArrayType()) {
4472 QualType ElemType =
4473 DynamicInit->getType()->getAsArrayTypeUnsafe()->getElementType();
4474 if (OptPrimType InitT = classify(ElemType)) {
4475 if (!this->visitZeroInitializer(*InitT, ElemType, E))
4476 return false;
4477 if (!this->emitStorePop(*InitT, E))
4478 return false;
4479 } else {
4480 assert(ElemType->isArrayType());
4481 if (!this->visitZeroArrayInitializer(ElemType, E))
4482 return false;
4483 }
4484 } else if (DynamicInit) {
4485 if (OptPrimType InitT = classify(DynamicInit)) {
4486 if (!this->visit(DynamicInit))
4487 return false;
4488 if (!this->emitStorePop(*InitT, E))
4489 return false;
4490 } else {
4491 if (!this->visitInitializerPop(DynamicInit))
4492 return false;
4493 }
4494 } else if (ElemT) {
4495 if (!this->visitZeroInitializer(
4496 *ElemT, InitType->getAsArrayTypeUnsafe()->getElementType(),
4497 Init))
4498 return false;
4499 if (!this->emitStorePop(*ElemT, E))
4500 return false;
4501 } else {
4502 assert(CtorFunc);
4503 if (!this->emitCall(CtorFunc, 0, E))
4504 return false;
4505 }
4506
4507 // ++Iter;
4508 if (!this->emitGetPtrLocal(Iter, E))
4509 return false;
4510 if (!this->emitIncPop(SizeT, false, E))
4511 return false;
4512
4513 if (!this->jump(StartLabel, E))
4514 return false;
4515
4516 this->fallthrough(EndLabel);
4517 this->emitLabel(EndLabel);
4518 }
4519 }
4520 } else { // Non-array.
4521 if (PlacementDest) {
4522 if (!this->visit(PlacementDest))
4523 return false;
4524 if (!this->emitCheckNewTypeMismatch(E, E))
4525 return false;
4526
4527 } else {
4528 // Allocate just one element.
4529 if (!this->emitAlloc(Desc, E))
4530 return false;
4531 }
4532
4533 if (Init) {
4534 if (ElemT) {
4535 if (!this->visit(Init))
4536 return false;
4537
4538 if (!this->emitInit(*ElemT, E))
4539 return false;
4540 } else {
4541 // Composite.
4542 if (!this->visitInitializer(Init))
4543 return false;
4544 }
4545 }
4546 }
4547
4548 if (DiscardResult)
4549 return this->emitPopPtr(E);
4550
4551 return true;
4552}
4553
4554template <class Emitter>
4556 if (E->containsErrors())
4557 return false;
4558 const FunctionDecl *OperatorDelete = E->getOperatorDelete();
4559
4560 if (!OperatorDelete->isUsableAsGlobalAllocationFunctionInConstantEvaluation())
4561 return this->emitInvalidNewDeleteExpr(E, E);
4562
4563 // Arg must be an lvalue.
4564 if (!this->visit(E->getArgument()))
4565 return false;
4566
4567 return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E);
4568}
4569
4570template <class Emitter>
4572 if (DiscardResult)
4573 return true;
4574
4575 const Function *Func = nullptr;
4576 if (const Function *F = Ctx.getOrCreateObjCBlock(E))
4577 Func = F;
4578
4579 if (!Func)
4580 return false;
4581 return this->emitGetFnPtr(Func, E);
4582}
4583
4584template <class Emitter>
4586 if (E->containsErrors())
4587 return false;
4588
4589 const Type *TypeInfoType = E->getType().getTypePtr();
4590
4591 auto canonType = [](const Type *T) {
4592 return T->getCanonicalTypeUnqualified().getTypePtr();
4593 };
4594
4595 if (!E->isPotentiallyEvaluated()) {
4596 if (DiscardResult)
4597 return true;
4598
4599 if (E->isTypeOperand())
4600 return this->emitGetTypeid(
4601 canonType(E->getTypeOperand(Ctx.getASTContext()).getTypePtr()),
4602 TypeInfoType, E);
4603
4604 return this->emitGetTypeid(
4605 canonType(E->getExprOperand()->getType().getTypePtr()), TypeInfoType,
4606 E);
4607 }
4608
4609 // Otherwise, we need to evaluate the expression operand.
4610 assert(E->getExprOperand());
4611 assert(E->getExprOperand()->isLValue());
4612
4613 if (!Ctx.getLangOpts().CPlusPlus20 && !this->emitDiagTypeid(E))
4614 return false;
4615
4616 if (!this->visit(E->getExprOperand()))
4617 return false;
4618
4619 if (!this->emitGetTypeidPtr(TypeInfoType, E))
4620 return false;
4621 if (DiscardResult)
4622 return this->emitPopPtr(E);
4623 return true;
4624}
4625
4626template <class Emitter>
4628 const ObjCDictionaryLiteral *E) {
4630 return this->emitDummyPtr(E, E);
4631 return this->emitError(E);
4632}
4633
4634template <class Emitter>
4637 return this->emitDummyPtr(E, E);
4638 return this->emitError(E);
4639}
4640
4641template <class Emitter>
4643 if (DiscardResult)
4644 return true;
4645
4646 switch (E->getKind()) {
4647 case ReflectionKind::Null: {
4648 assert(false && "null reflection can't be constructed from parsing a "
4649 "reflection operand");
4650 return false;
4651 }
4652 case ReflectionKind::Type: {
4653 return this->emitReflectValue(E->getKind(), E->getOpaqueValue(), E);
4654 }
4655 }
4656
4657 assert(false && "unknown or unimplemented reflection entities");
4658 return false;
4659}
4660
4661template <class Emitter>
4663 assert(Ctx.getLangOpts().CPlusPlus);
4664 return this->emitConstBool(E->getValue(), E);
4665}
4666
4667template <class Emitter>
4669 if (DiscardResult)
4670 return true;
4671 assert(!Initializing);
4672
4673 const MSGuidDecl *GuidDecl = E->getGuidDecl();
4674 const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl();
4675 assert(RD);
4676 // If the definiton of the result type is incomplete, just return a dummy.
4677 // If (and when) that is read from, we will fail, but not now.
4678 if (!RD->isCompleteDefinition())
4679 return this->emitDummyPtr(GuidDecl, E);
4680
4681 UnsignedOrNone GlobalIndex = P.getOrCreateGlobal(GuidDecl);
4682 if (!GlobalIndex)
4683 return false;
4684 if (!this->emitGetPtrGlobal(*GlobalIndex, E))
4685 return false;
4686
4687 assert(this->getRecord(E->getType()));
4688
4689 const APValue &V = GuidDecl->getAsAPValue();
4690 if (V.getKind() == APValue::None)
4691 return true;
4692
4693 assert(V.isStruct());
4694 assert(V.getStructNumBases() == 0);
4695 if (!this->visitAPValueInitializer(V, E, E->getType()))
4696 return false;
4697
4698 return this->emitFinishInit(E);
4699}
4700
4701template <class Emitter>
4703 assert(classifyPrim(E->getType()) == PT_Bool);
4704 if (E->isValueDependent())
4705 return false;
4706 if (DiscardResult)
4707 return true;
4708 return this->emitConstBool(E->isSatisfied(), E);
4709}
4710
4711template <class Emitter>
4713 const ConceptSpecializationExpr *E) {
4714 assert(classifyPrim(E->getType()) == PT_Bool);
4715 if (DiscardResult)
4716 return true;
4717 return this->emitConstBool(E->isSatisfied(), E);
4718}
4719
4720template <class Emitter>
4725
4726template <class Emitter>
4728 // Diagnose a relocated BPF enumerator, i.e., of an enum with the
4729 // preserve_access_index attribute, which is not a constant expression. Return
4730 // true if diagnosed.
4731 auto getAccessIndexConstantDecl = [&]() -> const EnumConstantDecl * {
4732 const auto *DRE = dyn_cast<DeclRefExpr>(E->getSyntacticForm());
4733 if (!DRE)
4734 return nullptr;
4735 const auto *ECD = dyn_cast<EnumConstantDecl>(DRE->getDecl());
4736 if (!ECD)
4737 return nullptr;
4738 const auto *ED = dyn_cast<EnumDecl>(ECD->getDeclContext());
4739 if (ED && ED->hasAttr<BPFPreserveAccessIndexAttr>())
4740 return ECD;
4741 return nullptr;
4742 };
4743
4744 if (const EnumConstantDecl *ECD = getAccessIndexConstantDecl()) {
4745 const auto *ED = cast<EnumDecl>(ECD->getDeclContext());
4746 // An anonymous enum is only relocated through its typedef name.
4747 QualType EnumTy;
4748 if (ED->getDeclName())
4749 EnumTy = Ctx.getASTContext().getCanonicalTagType(ED);
4750 else if (const TypedefNameDecl *TD = ED->getTypedefNameForAnonDecl())
4751 EnumTy = Ctx.getASTContext().getTypeDeclType(TD);
4752 else
4753 return false;
4754
4755 return this->emitInvalidAccessIndexAttr(ECD, EnumTy.getTypePtr(), E);
4756 }
4757
4758 for (const Expr *SemE : E->semantics()) {
4759 if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) {
4760 if (SemE == E->getResultExpr())
4761 return false;
4762
4763 if (OVE->isUnique())
4764 continue;
4765
4766 if (!this->discard(OVE))
4767 return false;
4768 } else if (SemE == E->getResultExpr()) {
4769 if (!this->delegate(SemE))
4770 return false;
4771 } else {
4772 if (!this->discard(SemE))
4773 return false;
4774 }
4775 }
4776 return true;
4777}
4778
4779template <class Emitter>
4783
4784template <class Emitter>
4786 return this->emitError(E);
4787}
4788
4789template <class Emitter>
4791 assert(E->getType()->isVoidPointerType());
4792 if (DiscardResult)
4793 return true;
4794
4795 return this->emitDummyPtr(E, E);
4796}
4797
4798template <class Emitter>
4799bool Compiler<Emitter>::emitVectorConversion(const Expr *Src, const Expr *E) {
4800 if (Src->containsErrors())
4801 return false;
4802
4803 const auto *VT = E->getType()->castAs<VectorType>();
4804 QualType ElemType = VT->getElementType();
4805 PrimType ElemT = classifyPrim(ElemType);
4806 QualType SrcType = Src->getType();
4807 PrimType SrcElemT = classifyVectorElementType(SrcType);
4808
4809 if (!Initializing) {
4810 UnsignedOrNone LocalIndex = allocateLocal(E);
4811 if (!LocalIndex)
4812 return false;
4813 if (!this->emitGetPtrLocal(*LocalIndex, E))
4814 return false;
4815 }
4816
4817 unsigned SrcOffset =
4818 this->allocateLocalPrimitive(Src, PT_Ptr, /*IsConst=*/true);
4819 if (!this->visit(Src))
4820 return false;
4821 if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
4822 return false;
4823
4824 for (unsigned I = 0; I != VT->getNumElements(); ++I) {
4825 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
4826 return false;
4827 if (!this->emitArrayElemPop(SrcElemT, I, E))
4828 return false;
4829
4830 // Cast to the desired result element type.
4831 if (SrcElemT != ElemT) {
4832 if (!this->emitPrimCast(SrcElemT, ElemT, ElemType, E))
4833 return false;
4834 } else if (ElemType->isFloatingType() && SrcType != ElemType) {
4835 const auto *TargetSemantics = &Ctx.getFloatSemantics(ElemType);
4836 if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E))
4837 return false;
4838 }
4839 if (!this->emitInitElem(ElemT, I, E))
4840 return false;
4841 }
4842 return true;
4843}
4844
4845template <class Emitter>
4847 return emitVectorConversion(E->getSrcExpr(), E);
4848}
4849
4850template <class Emitter>
4852 // FIXME: Unary shuffle with mask not currently supported.
4853 if (E->getNumSubExprs() == 2)
4854 return this->emitInvalid(E);
4855
4856 assert(E->getNumSubExprs() > 2);
4857
4858 const Expr *Vecs[] = {E->getExpr(0), E->getExpr(1)};
4859 const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>();
4860 PrimType ElemT = classifyPrim(VT->getElementType());
4861 unsigned NumInputElems = VT->getNumElements();
4862 unsigned NumOutputElems = E->getNumSubExprs() - 2;
4863 assert(NumOutputElems > 0);
4864
4865 if (!Initializing) {
4866 UnsignedOrNone LocalIndex = allocateLocal(E);
4867 if (!LocalIndex)
4868 return false;
4869 if (!this->emitGetPtrLocal(*LocalIndex, E))
4870 return false;
4871 }
4872
4873 // Save both input vectors to a local variable.
4874 unsigned VectorOffsets[2];
4875 for (unsigned I = 0; I != 2; ++I) {
4876 VectorOffsets[I] =
4877 this->allocateLocalPrimitive(Vecs[I], PT_Ptr, /*IsConst=*/true);
4878 if (!this->visit(Vecs[I]))
4879 return false;
4880 if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
4881 return false;
4882 }
4883 for (unsigned I = 0; I != NumOutputElems; ++I) {
4884 APSInt ShuffleIndex = E->getShuffleMaskIdx(I);
4885 assert(ShuffleIndex >= -1);
4886 if (ShuffleIndex == -1)
4887 return this->emitInvalidShuffleVectorIndex(I, E);
4888
4889 assert(ShuffleIndex < (NumInputElems * 2));
4890 if (!this->emitGetLocal(PT_Ptr,
4891 VectorOffsets[ShuffleIndex >= NumInputElems], E))
4892 return false;
4893 unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems;
4894 if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E))
4895 return false;
4896
4897 if (!this->emitInitElem(ElemT, I, E))
4898 return false;
4899 }
4900
4901 if (DiscardResult)
4902 return this->emitPopPtr(E);
4903
4904 return true;
4905}
4906
4907template <class Emitter>
4909 const ExtVectorElementExpr *E) {
4910 const Expr *Base = E->getBase();
4911 assert(
4912 Base->getType()->isVectorType() ||
4913 Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType());
4914
4916 E->getEncodedElementAccess(Indices);
4917
4918 if (Indices.size() == 1) {
4919 if (!this->visit(Base))
4920 return false;
4921
4922 if (E->isGLValue()) {
4923 if (!this->emitConstUint32(Indices[0], E))
4924 return false;
4925 return this->emitArrayElemPtrPop(PT_Uint32, E);
4926 }
4927 // Else, also load the value.
4928 return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E);
4929 }
4930
4931 // Create a local variable for the base.
4932 unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true);
4933 if (!this->visit(Base))
4934 return false;
4935 if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
4936 return false;
4937
4938 // Now the vector variable for the return value.
4939 if (!Initializing) {
4940 UnsignedOrNone ResultIndex = allocateLocal(E);
4941 if (!ResultIndex)
4942 return false;
4943 if (!this->emitGetPtrLocal(*ResultIndex, E))
4944 return false;
4945 }
4946
4947 assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements());
4948
4949 PrimType ElemT =
4951 uint32_t DstIndex = 0;
4952 for (uint32_t I : Indices) {
4953 if (!this->emitGetLocal(PT_Ptr, BaseOffset, E))
4954 return false;
4955 if (!this->emitArrayElemPop(ElemT, I, E))
4956 return false;
4957 if (!this->emitInitElem(ElemT, DstIndex, E))
4958 return false;
4959 ++DstIndex;
4960 }
4961
4962 // Leave the result pointer on the stack.
4963 assert(!DiscardResult);
4964 return true;
4965}
4966
4967template <class Emitter>
4969 const Expr *SubExpr = E->getSubExpr();
4971 return this->discard(SubExpr) && this->emitInvalid(E);
4972
4973 if (DiscardResult)
4974 return true;
4975
4976 assert(classifyPrim(E) == PT_Ptr);
4977 return this->emitDummyPtr(E, E);
4978}
4979
4980template <class Emitter>
4982 const CXXStdInitializerListExpr *E) {
4983 const Expr *SubExpr = E->getSubExpr();
4985 Ctx.getASTContext().getAsConstantArrayType(SubExpr->getType());
4986 const Record *R = getRecord(E->getType());
4987 assert(SubExpr->isGLValue());
4988 assert(!canClassify(E->getType()));
4989
4990 if (!Initializing) {
4991 UnsignedOrNone LocalIndex = allocateLocal(E);
4992 if (!LocalIndex)
4993 return false;
4994 if (!this->emitGetPtrLocal(*LocalIndex, E))
4995 return false;
4996 }
4997
4998 if (!this->visit(SubExpr))
4999 return false;
5000 if (!this->emitConstUint8(0, E))
5001 return false;
5002 if (!this->emitArrayElemPtrPopUint8(E))
5003 return false;
5004 if (!this->emitInitFieldPtr(R->getField(0u)->Offset, E))
5005 return false;
5006
5007 PrimType SecondFieldT = *R->getField(1u)->T;
5008 if (isIntegerOrBoolType(SecondFieldT)) {
5009 if (!this->emitConst(ArrayType->getSize(), SecondFieldT, E))
5010 return false;
5011 if (!this->emitInitField(SecondFieldT, R->getField(1u)->Offset, E))
5012 return false;
5013 if (DiscardResult)
5014 return this->emitPopPtr(E);
5015 return true;
5016 }
5017 assert(SecondFieldT == PT_Ptr);
5018
5019 if (!this->emitGetFieldPtr(R->getField(0u)->Offset, E))
5020 return false;
5021 if (!this->emitExpandPtr(E))
5022 return false;
5023 if (!this->emitConst(ArrayType->getSize(), PT_Uint64, E))
5024 return false;
5025 if (!this->emitArrayElemPtrPop(PT_Uint64, E))
5026 return false;
5027
5028 if (!this->emitInitFieldPtr(R->getField(1u)->Offset, E))
5029 return false;
5030 if (DiscardResult)
5031 return this->emitPopPtr(E);
5032 return true;
5033}
5034
5035template <class Emitter>
5037 LocalScope<Emitter> BS(this);
5038 llvm::SaveAndRestore StmtExprSAR(this->InStmtExpr, true);
5039
5040 const CompoundStmt *CS = E->getSubStmt();
5041 const Stmt *Result = CS->body_back();
5042 for (const Stmt *S : CS->body()) {
5043 if (S != Result) {
5044 if (!this->visitStmt(S))
5045 return false;
5046 continue;
5047 }
5048
5049 assert(S == Result);
5050 if (const Expr *ResultExpr = dyn_cast<Expr>(S))
5051 return this->delegate(ResultExpr);
5052 if (!this->visitStmt(S))
5053 return false;
5054 return this->emitUnsupported(E);
5055 }
5056
5057 return BS.destroyLocals();
5058}
5059
5060template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) {
5061 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
5062 /*NewInitializing=*/false, /*ToLValue=*/false);
5063 return this->Visit(E);
5064}
5065
5066template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) {
5067 // We're basically doing:
5068 // OptionScope<Emitter> Scope(this, DicardResult, Initializing, ToLValue);
5069 // but that's unnecessary of course.
5070 return this->Visit(E);
5071}
5072
5074 if (const auto *PE = dyn_cast<ParenExpr>(E))
5075 return stripCheckedDerivedToBaseCasts(PE->getSubExpr());
5076
5077 if (const auto *CE = dyn_cast<CastExpr>(E);
5078 CE &&
5079 (CE->getCastKind() == CK_DerivedToBase || CE->getCastKind() == CK_NoOp))
5080 return stripCheckedDerivedToBaseCasts(CE->getSubExpr());
5081
5082 return E;
5083}
5084
5085static const Expr *stripDerivedToBaseCasts(const Expr *E) {
5086 if (const auto *PE = dyn_cast<ParenExpr>(E))
5087 return stripDerivedToBaseCasts(PE->getSubExpr());
5088
5089 if (const auto *CE = dyn_cast<CastExpr>(E);
5090 CE && (CE->getCastKind() == CK_DerivedToBase ||
5091 CE->getCastKind() == CK_UncheckedDerivedToBase ||
5092 CE->getCastKind() == CK_NoOp))
5093 return stripDerivedToBaseCasts(CE->getSubExpr());
5094
5095 return E;
5096}
5097
5098template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
5099 if (E->getType().isNull())
5100 return false;
5101
5102 if (E->getType()->isVoidType())
5103 return this->discard(E);
5104
5105 // Create local variable to hold the return value.
5106 if (!E->isGLValue() && !canClassify(E->getType())) {
5107 UnsignedOrNone LocalIndex = allocateLocal(
5109 if (!LocalIndex)
5110 return false;
5111
5112 if (!this->emitGetPtrLocal(*LocalIndex, E))
5113 return false;
5114 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
5115 return this->visitInitializer(E);
5116 }
5117
5118 // Otherwise,we have a primitive return value, produce the value directly
5119 // and push it on the stack.
5120 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
5121 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
5122 return this->Visit(E);
5123}
5124
5125template <class Emitter>
5127 assert(!canClassify(E->getType()));
5128
5129 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
5130 /*NewInitializing=*/true, /*ToLValue=*/false);
5131 return this->Visit(E) && this->emitFinishInit(E);
5132}
5133
5134template <class Emitter>
5136 assert(!canClassify(E->getType()));
5137
5138 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
5139 /*NewInitializing=*/true, /*ToLValue=*/false);
5140 return this->Visit(E) && this->emitFinishInitPop(E);
5141}
5142
5143template <class Emitter> bool Compiler<Emitter>::visitAsLValue(const Expr *E) {
5144 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
5145 /*NewInitializing=*/false, /*ToLValue=*/true);
5146 return this->Visit(E);
5147}
5148
5149template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
5150 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
5151 /*NewInitializing=*/false, /*ToLValue=*/ToLValue);
5152
5153 OptPrimType T = classify(E->getType());
5154 if (!T) {
5155 // Convert complex values to bool.
5156 if (E->getType()->isAnyComplexType()) {
5157 if (!this->visit(E))
5158 return false;
5159 return this->emitComplexBoolCast(E);
5160 }
5161 return false;
5162 }
5163
5164 if (!this->visit(E))
5165 return false;
5166
5167 if (T == PT_Bool)
5168 return true;
5169
5170 // Convert pointers to bool.
5171 if (T == PT_Ptr)
5172 return this->emitIsNonNullPtr(E);
5173
5174 // Or Floats.
5175 if (T == PT_Float)
5176 return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
5177
5178 // Or anything else we can.
5179 return this->emitCast(*T, PT_Bool, E);
5180}
5181
5182template <class Emitter>
5183bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
5184 const Expr *E) {
5185 if (const auto *AT = QT->getAs<AtomicType>())
5186 QT = AT->getValueType();
5187
5188 switch (T) {
5189 case PT_Bool:
5190 return this->emitZeroBool(E);
5191 case PT_Sint8:
5192 return this->emitZeroSint8(E);
5193 case PT_Uint8:
5194 return this->emitZeroUint8(E);
5195 case PT_Sint16:
5196 return this->emitZeroSint16(E);
5197 case PT_Uint16:
5198 return this->emitZeroUint16(E);
5199 case PT_Sint32:
5200 return this->emitZeroSint32(E);
5201 case PT_Uint32:
5202 return this->emitZeroUint32(E);
5203 case PT_Sint64:
5204 return this->emitZeroSint64(E);
5205 case PT_Uint64:
5206 return this->emitZeroUint64(E);
5207 case PT_IntAP:
5208 return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
5209 case PT_IntAPS:
5210 return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
5211 case PT_Ptr:
5212 return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT),
5213 nullptr, E);
5214 case PT_MemberPtr:
5215 return this->emitNullMemberPtr(0, nullptr, E);
5216 case PT_Float: {
5217 APFloat F = APFloat::getZero(Ctx.getFloatSemantics(QT));
5218 return this->emitFloat(F, E);
5219 }
5220 case PT_FixedPoint: {
5221 auto Sem = Ctx.getASTContext().getFixedPointSemantics(QT);
5222 return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
5223 }
5224 case PT_Reflect:
5225 return this->emitReflectValue(ReflectionKind::Null, nullptr, E);
5226 }
5227 llvm_unreachable("unknown primitive type");
5228}
5229
5230template <class Emitter>
5231bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
5232 const Expr *E,
5233 bool IsCompleteClass) {
5234 assert(E);
5235 assert(R);
5236 // Fields
5237 for (const Record::Field &Field : R->fields()) {
5238 if (Field.isUnnamedBitField())
5239 continue;
5240
5241 const Descriptor *D = Field.Desc;
5242 if (D->isPrimitive()) {
5243 QualType QT = D->getType();
5244 PrimType T = D->getPrimType();
5245 if (!this->visitZeroInitializer(T, QT, E))
5246 return false;
5247 if (R->isUnion()) {
5248 if (!this->emitInitFieldActivate(T, Field.Offset, E))
5249 return false;
5250 break;
5251 }
5252 if (!this->emitInitField(T, Field.Offset, E))
5253 return false;
5254 continue;
5255 }
5256
5257 if (!this->emitGetPtrField(Field.Offset, E))
5258 return false;
5259
5260 if (D->isPrimitiveArray()) {
5261 QualType ET = D->getElemQualType();
5262 PrimType T = D->getPrimType();
5263 for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
5264 if (!this->visitZeroInitializer(T, ET, E))
5265 return false;
5266 if (!this->emitInitElem(T, I, E))
5267 return false;
5268 }
5269 } else if (D->isCompositeArray()) {
5270 // Can't be a vector or complex field.
5271 if (!this->visitZeroArrayInitializer(D->getType(), E))
5272 return false;
5273 } else if (D->isRecord()) {
5274 if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
5275 return false;
5276 } else
5277 return false;
5278
5279 // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
5280 // object's first non-static named data member is zero-initialized
5281 if (R->isUnion()) {
5282 if (!this->emitFinishInitActivatePop(E))
5283 return false;
5284 break;
5285 }
5286 if (!this->emitFinishInitPop(E))
5287 return false;
5288 }
5289
5290 for (const Record::Base &B : R->bases()) {
5291 if (!this->emitGetPtrBase(B.Offset, E))
5292 return false;
5293 if (!this->visitZeroRecordInitializer(B.R, E, /*IsCompleteClass=*/false))
5294 return false;
5295 if (!this->emitFinishInitPop(E))
5296 return false;
5297 }
5298
5299 if (IsCompleteClass) {
5300 for (const Record::Base &B : R->virtual_bases()) {
5301 if (!this->emitGetPtrVirtBase(cast<CXXRecordDecl>(B.R->getDecl()), E))
5302 return false;
5303 if (!this->visitZeroRecordInitializer(B.R, E, /*IsCompleteClass=*/false))
5304 return false;
5305 if (!this->emitFinishInitPop(E))
5306 return false;
5307 }
5308 }
5309
5310 return true;
5311}
5312
5313template <class Emitter>
5314bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
5315 assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType());
5316 const ArrayType *AT = T->getAsArrayTypeUnsafe();
5317 QualType ElemType = AT->getElementType();
5318 size_t NumElems = cast<ConstantArrayType>(AT)->getZExtSize();
5319
5320 if (OptPrimType ElemT = classify(ElemType)) {
5321 for (size_t I = 0; I != NumElems; ++I) {
5322 if (!this->visitZeroInitializer(*ElemT, ElemType, E))
5323 return false;
5324 if (!this->emitInitElem(*ElemT, I, E))
5325 return false;
5326 }
5327 return true;
5328 }
5329 if (ElemType->isRecordType()) {
5330 const Record *R = getRecord(ElemType);
5331 if (!R)
5332 return false;
5333
5334 for (size_t I = 0; I != NumElems; ++I) {
5335 if (!this->emitConstUint32(I, E))
5336 return false;
5337 if (!this->emitArrayElemPtr(PT_Uint32, E))
5338 return false;
5339 if (!this->visitZeroRecordInitializer(R, E))
5340 return false;
5341 if (!this->emitPopPtr(E))
5342 return false;
5343 }
5344 return true;
5345 }
5346 if (ElemType->isArrayType()) {
5347 for (size_t I = 0; I != NumElems; ++I) {
5348 if (!this->emitConstUint32(I, E))
5349 return false;
5350 if (!this->emitArrayElemPtr(PT_Uint32, E))
5351 return false;
5352 if (!this->visitZeroArrayInitializer(ElemType, E))
5353 return false;
5354 if (!this->emitPopPtr(E))
5355 return false;
5356 }
5357 return true;
5358 }
5359
5360 return false;
5361}
5362
5363template <class Emitter>
5364bool Compiler<Emitter>::visitAssignment(const Expr *LHS, const Expr *RHS,
5365 const Expr *E) {
5366 if (!canClassify(E->getType()))
5367 return false;
5368
5369 bool NeedsFlip = !isSideEffectFree(RHS);
5370 if (!NeedsFlip) {
5371 if (!this->visit(LHS))
5372 return false;
5373 if (!this->visit(RHS))
5374 return false;
5375 } else {
5376 if (!this->visit(RHS))
5377 return false;
5378 if (!this->visit(LHS))
5379 return false;
5380 }
5381
5382 if (LHS->getType().isVolatileQualified())
5383 return this->emitInvalidStore(LHS->getType().getTypePtr(), E);
5384
5385 // We don't support assignments in C.
5386 if (!Ctx.getLangOpts().CPlusPlus && !this->emitInvalid(E))
5387 return false;
5388
5389 PrimType RHT = classifyPrim(RHS);
5390 bool Activates = refersToUnion(LHS);
5391 bool BitField = LHS->refersToBitField();
5392
5393 if (NeedsFlip && !this->emitFlip(PT_Ptr, RHT, E))
5394 return false;
5395
5396 if (DiscardResult) {
5397 if (BitField && Activates)
5398 return this->emitStoreBitFieldActivatePop(RHT, E);
5399 if (BitField)
5400 return this->emitStoreBitFieldPop(RHT, E);
5401 if (Activates)
5402 return this->emitStoreActivatePop(RHT, E);
5403 // Otherwise, regular non-activating store.
5404 return this->emitStorePop(RHT, E);
5405 }
5406
5407 auto maybeLoad = [&](bool Result) -> bool {
5408 if (!Result)
5409 return false;
5410 // Assignments aren't necessarily lvalues in C.
5411 // Load from them in that case.
5412 if (!E->isLValue())
5413 return this->emitLoadPop(RHT, E);
5414 return true;
5415 };
5416
5417 if (BitField && Activates)
5418 return maybeLoad(this->emitStoreBitFieldActivate(RHT, E));
5419 if (BitField)
5420 return maybeLoad(this->emitStoreBitField(RHT, E));
5421 if (Activates)
5422 return maybeLoad(this->emitStoreActivate(RHT, E));
5423 // Otherwise, regular non-activating store.
5424 return maybeLoad(this->emitStore(RHT, E));
5425}
5426
5427template <class Emitter>
5428template <typename T>
5429bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, SourceInfo Info) {
5430 switch (Ty) {
5431 case PT_Sint8:
5432 return this->emitConstSint8(Value, Info);
5433 case PT_Uint8:
5434 return this->emitConstUint8(Value, Info);
5435 case PT_Sint16:
5436 return this->emitConstSint16(Value, Info);
5437 case PT_Uint16:
5438 return this->emitConstUint16(Value, Info);
5439 case PT_Sint32:
5440 return this->emitConstSint32(Value, Info);
5441 case PT_Uint32:
5442 return this->emitConstUint32(Value, Info);
5443 case PT_Sint64:
5444 return this->emitConstSint64(Value, Info);
5445 case PT_Uint64:
5446 return this->emitConstUint64(Value, Info);
5447 case PT_Bool:
5448 return this->emitConstBool(Value, Info);
5449 case PT_Ptr:
5450 case PT_MemberPtr:
5451 case PT_Float:
5452 case PT_IntAP:
5453 case PT_IntAPS:
5454 case PT_FixedPoint:
5455 case PT_Reflect:
5456 llvm_unreachable("Invalid integral type");
5457 break;
5458 }
5459 llvm_unreachable("unknown primitive type");
5460}
5461
5462template <class Emitter>
5463template <typename T>
5464bool Compiler<Emitter>::emitConst(T Value, const Expr *E) {
5465 return this->emitConst(Value, classifyPrim(E->getType()), E);
5466}
5467
5468template <class Emitter>
5469bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
5470 SourceInfo Info) {
5471 if (Ty == PT_IntAPS)
5472 return this->emitConstIntAPS(Value, Info);
5473 if (Ty == PT_IntAP)
5474 return this->emitConstIntAP(Value, Info);
5475
5476 if (Value.isSigned())
5477 return this->emitConst(Value.getSExtValue(), Ty, Info);
5478 return this->emitConst(Value.getZExtValue(), Ty, Info);
5479}
5480
5481template <class Emitter>
5482bool Compiler<Emitter>::emitConst(const APInt &Value, PrimType Ty,
5483 SourceInfo Info) {
5484 if (Ty == PT_IntAPS)
5485 return this->emitConstIntAPS(Value, Info);
5486 if (Ty == PT_IntAP)
5487 return this->emitConstIntAP(Value, Info);
5488
5489 if (isSignedType(Ty))
5490 return this->emitConst(Value.getSExtValue(), Ty, Info);
5491 return this->emitConst(Value.getZExtValue(), Ty, Info);
5492}
5493
5494template <class Emitter>
5495bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
5496 return this->emitConst(Value, classifyPrim(E->getType()), E);
5497}
5498
5499template <class Emitter>
5501 bool IsConst,
5502 bool IsVolatile,
5503 ScopeKind SC) {
5504 // FIXME: There are cases where Src.isExpr() is wrong, e.g.
5505 // (int){12} in C. Consider using Expr::isTemporaryObject() instead
5506 // or isa<MaterializeTemporaryExpr>().
5507 Descriptor *D = P.createDescriptor(Src, Ty, nullptr, IsConst, Src.isExpr(),
5508 /*IsMutable=*/false, IsVolatile);
5509 Scope::Local Local = this->createLocal(D, this->VariablesAreConstexprUnknown);
5510 if (auto *VD = Src.asValueDecl())
5511 Locals.insert({VD, Local});
5512 VarScope->addForScopeKind(Local, SC);
5513 return Local.Offset;
5514}
5515
5516template <class Emitter>
5518 ScopeKind SC) {
5519 const ValueDecl *Key = nullptr;
5520 const Expr *Init = nullptr;
5521 bool IsTemporary = false;
5522 if (auto *VD = Src.asValueDecl()) {
5523 Key = VD;
5524
5525 if (const auto *VarD = dyn_cast<VarDecl>(VD))
5526 Init = VarD->getInit();
5527 }
5528 if (const auto *E = Src.asExpr()) {
5529 IsTemporary = true;
5530 if (Ty.isNull())
5531 Ty = E->getType();
5532 }
5533
5534 Descriptor *D = P.createDescriptor(
5535 Src, Ty.getTypePtr(), Ty.isConstQualified(), IsTemporary,
5536 /*IsMutable=*/false, /*IsVolatile=*/Ty.isVolatileQualified(), Init);
5537 if (!D)
5538 return std::nullopt;
5539
5540 Scope::Local Local = this->createLocal(D, this->VariablesAreConstexprUnknown);
5541 if (Key)
5542 Locals.insert({Key, Local});
5543 VarScope->addForScopeKind(Local, SC);
5544 return Local.Offset;
5545}
5546
5547template <class Emitter>
5549 QualType Ty = E->getType();
5550 assert(!Ty->isRecordType());
5551
5552 Descriptor *D = P.createDescriptor(E, Ty.getTypePtr(), Ty.isConstQualified(),
5553 /*IsTemporary=*/true);
5554
5555 if (!D)
5556 return std::nullopt;
5557
5558 Scope::Local Local = this->createLocal(D);
5560 assert(S);
5561 // Attach to topmost scope.
5562 while (S->getParent())
5563 S = S->getParent();
5564 assert(S && !S->getParent());
5565 S->addLocal(Local);
5566 return Local.Offset;
5567}
5568
5569template <class Emitter>
5571 if (const PointerType *PT = dyn_cast<PointerType>(Ty))
5572 return PT->getPointeeType()->getAsCanonical<RecordType>();
5573 return Ty->getAsCanonical<RecordType>();
5574}
5575
5576template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
5577 if (const auto *RecordTy = getRecordTy(Ty))
5578 return getRecord(RecordTy->getDecl()->getDefinitionOrSelf());
5579 return nullptr;
5580}
5581
5582template <class Emitter>
5584 return P.getOrCreateRecord(RD);
5585}
5586
5587template <class Emitter>
5589 return Ctx.getOrCreateFunction(FD);
5590}
5591
5592template <class Emitter>
5593bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
5594 assert(E);
5595 assert(!E->getType().isNull());
5597
5598 auto maybeDestroyLocals = [&]() -> bool {
5599 if (DestroyToplevelScope)
5600 return RootScope.destroyLocals() && this->emitCheckAllocations(E);
5601 return this->emitCheckAllocations(E);
5602 };
5603
5604 // Void expressions.
5605 if (E->getType()->isVoidType()) {
5606 if (!visit(E))
5607 return false;
5608 return this->emitRetVoid(E) && maybeDestroyLocals();
5609 }
5610
5611 // Expressions with a primitive return type.
5612 if (OptPrimType T = classify(E)) {
5613 if (!visit(E))
5614 return false;
5615
5616 return this->emitRet(*T, E) && maybeDestroyLocals();
5617 }
5618
5619 // Expressions with a composite return type.
5620 // For us, that means everything we don't
5621 // have a PrimType for.
5622 if (UnsignedOrNone LocalOffset = this->allocateLocal(E)) {
5623 InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalOffset));
5624 if (!this->emitGetPtrLocal(*LocalOffset, E))
5625 return false;
5626
5627 if (!visitInitializer(E))
5628 return false;
5629 // We are destroying the locals AFTER the Ret op.
5630 // The Ret op needs to copy the (alive) values, but the
5631 // destructors may still turn the entire expression invalid.
5632 return this->emitRetValue(E) && maybeDestroyLocals();
5633 }
5634
5635 return maybeDestroyLocals() && false;
5636}
5637
5638template <class Emitter>
5640 bool DestroyToplevelScope) {
5641 OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
5642 /*NewInitializing=*/false, /*ToLValue=*/true);
5643
5644 return this->visitExpr(E, DestroyToplevelScope);
5645}
5646
5647template <class Emitter>
5649
5650 auto R = this->visitVarDecl(VD, VD->getInit(), /*Toplevel=*/true);
5651
5652 if (R.notCreated())
5653 return R;
5654
5655 if (R)
5656 return true;
5657
5658 if (!R && Context::shouldBeGloballyIndexed(VD)) {
5659 if (auto GlobalIndex = P.getGlobal(VD)) {
5660 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5661 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5662
5664 GlobalBlock->invokeDtor();
5665 }
5666 }
5667
5668 return R;
5669}
5670
5671/// Toplevel visitDeclAndReturn().
5672/// We get here from evaluateAsInitializer().
5673/// We need to evaluate the initializer and return its value.
5674template <class Emitter>
5676 bool ConstantContext) {
5677 // We only create variables if we're evaluating in a constant context.
5678 // Otherwise, just evaluate the initializer and return it.
5679 if (!ConstantContext) {
5680 DeclScope<Emitter> LS(this, VD);
5681 if (!this->visit(Init))
5682 return false;
5683 return this->emitRet(classify(Init).value_or(PT_Ptr), VD) &&
5684 LS.destroyLocals() && this->emitCheckAllocations(VD);
5685 }
5686
5687 LocalScope<Emitter> VDScope(this);
5688 if (!this->visitVarDecl(VD, Init, /*Toplevel=*/true))
5689 return false;
5690
5691 OptPrimType VarT = classify(VD->getType());
5692 bool IsReference = VD->getType()->isReferenceType();
5694 auto GlobalIndex = P.getGlobal(VD);
5695 assert(GlobalIndex); // visitVarDecl() didn't return false.
5696 if (VarT) {
5697 if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
5698 return false;
5699 } else {
5700 if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
5701 return false;
5702 }
5703 } else {
5704 auto Local = Locals.find(VD);
5705 assert(Local != Locals.end()); // Same here.
5706 if (VarT) {
5707 if (IsReference) {
5708 if (!this->emitGetRefLocal(Local->second.Offset, VD))
5709 return false;
5710 } else if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
5711 return false;
5712 } else {
5713 if (!this->emitGetPtrLocal(Local->second.Offset, VD))
5714 return false;
5715 }
5716 }
5717
5718 // Return the value.
5719 if (!this->emitRet(VarT.value_or(PT_Ptr), VD)) {
5720 // If the Ret above failed and this is a global variable. Mark it as
5721 // uninitialized, even if everything else succeeded.
5723 auto GlobalIndex = P.getGlobal(VD);
5724 assert(GlobalIndex);
5725 Block *GlobalBlock = P.getGlobal(*GlobalIndex);
5726 auto &GD = GlobalBlock->getBlockDesc<GlobalInlineDescriptor>();
5727
5729 GlobalBlock->invokeDtor();
5730 }
5731 return false;
5732 }
5733
5734 return VDScope.destroyLocals() && this->emitCheckAllocations(VD);
5735}
5736
5737template <class Emitter>
5739 const Expr *Init,
5740 bool Toplevel) {
5741 QualType VarTy = VD->getType();
5742 // We don't know what to do with these, so just return false.
5743 if (VarTy.isNull())
5744 return false;
5745
5746 // This case is EvalEmitter-only. If we won't create any instructions for the
5747 // initializer anyway, don't bother creating the variable in the first place.
5748 if (!this->isActive())
5750
5751 OptPrimType VarT = classify(VD->getType());
5752
5753 if (Init && Init->isValueDependent())
5754 return false;
5755
5757 auto checkDecl = [&]() -> bool {
5758 bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
5759 return !NeedsOp || this->emitCheckDecl(VD, VD);
5760 };
5761
5763 UnsignedOrNone GlobalIndex = P.getGlobal(VD);
5764 if (GlobalIndex) {
5765 // The global was previously created but the initializer failed.
5766 if (!P.getGlobal(*GlobalIndex)->isInitialized())
5767 return false;
5768 // We've already seen and initialized this global.
5769 if (P.isGlobalInitialized(*GlobalIndex))
5770 return checkDecl();
5771 // The previous attempt at initialization might've been unsuccessful,
5772 // so let's try this one.
5773 } else if ((GlobalIndex =
5774 P.createGlobal(VD, Init, VariablesAreConstexprUnknown))) {
5775 } else {
5776 return false;
5777 }
5778 if (!Init)
5779 return true;
5780
5781 if (!checkDecl())
5782 return false;
5783
5784 if (VarT) {
5785 if (!this->visit(Init))
5786 return false;
5787
5788 return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
5789 }
5790
5791 if (!this->emitGetPtrGlobal(*GlobalIndex, Init))
5792 return false;
5793
5794 if (!this->emitStartInit(Init))
5795 return false;
5796
5797 if (!visitInitializer(Init))
5798 return false;
5799
5800 if (!this->emitEndInit(Init))
5801 return false;
5802
5803 return this->emitFinishInitGlobal(Init);
5804 }
5805 // Local variables.
5807
5808 if (VarT) {
5809 unsigned Offset = this->allocateLocalPrimitive(
5810 VD, *VarT, VarTy.isConstQualified(), VarTy.isVolatileQualified(),
5812
5813 if (!Init || Init->getType()->isVoidType())
5814 return true;
5815
5816 // If this is a toplevel declaration, create a scope for the
5817 // initializer.
5818 if (Toplevel) {
5820 if (!this->visit(Init))
5821 return false;
5822 return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
5823 }
5824 if (!this->visit(Init))
5825 return false;
5826
5827 if (VarTy->isReferenceType()) {
5828 // [C++26][decl.ref]
5829 // The object designated by such a glvalue can be outside its lifetime
5830 // Because a null pointer value or a pointer past the end of an object
5831 // does not point to an object, a reference in a well-defined program
5832 // cannot refer to such things;
5833 assert(classifyPrim(VarTy) == PT_Ptr);
5834 if (!this->emitCheckRefInit(Init))
5835 return false;
5836 }
5837
5838 return this->emitSetLocal(*VarT, Offset, VD);
5839 }
5840 // Local composite variables.
5841 if (UnsignedOrNone Offset =
5842 this->allocateLocal(VD, VarTy, ScopeKind::Block)) {
5843 if (!Init)
5844 return true;
5845
5846 if (!this->emitGetPtrLocal(*Offset, Init))
5847 return false;
5848
5849 return visitInitializerPop(Init);
5850 }
5851 return false;
5852}
5853
5854template <class Emitter>
5856 assert(!canClassify(VD->getType()));
5857
5859 // Create a local variable to use as the instance.
5860 QualType Ty = VD->getType();
5861 Descriptor *D =
5862 P.createDescriptor(VD, Ty.getTypePtr(), /*IsConst=*/Ty.isConstQualified(),
5863 /*IsTemporary=*/false, /*IsMutable=*/false,
5864 /*IsVolatile=*/Ty.isVolatileQualified(), nullptr);
5865 if (!D)
5866 return false;
5867
5868 // FIXME: Would be nice if we didn't allocate the descriptor at all in this
5869 // case.
5870 if (D->hasTrivialDtor())
5871 return true;
5872
5873 Scope::Local Local = this->createLocal(D);
5874 Locals.insert({VD, Local});
5875 VarScope->addForScopeKind(Local, ScopeKind::Block);
5876
5877 if (!this->emitGetPtrLocal(Local.Offset, VD))
5878 return false;
5879
5880 if (!this->visitAPValueInitializer(Value, VD, Ty))
5881 return false;
5882
5883 return this->emitDestructionPop(D, VD);
5884}
5885
5887public:
5889 explicit ParamFinder() {}
5890
5891 bool VisitDeclRefExpr(const DeclRefExpr *E) override {
5892 if (const auto *P = dyn_cast<ParmVarDecl>(E->getDecl()))
5893 FoundParams.insert(P);
5894 return true;
5895 }
5896};
5897
5898/// Evaluate the \p Condition as if it was in the body of \p Callee.
5899/// Specifically, all the parameters of the callee are available to use
5900/// for the condition, and their values are given by \p Args (and \p This).
5901///
5902// Since this is a somewhat niche feature, we're abusing a few other mechanisms
5903// to implement this.
5904//
5905// We don't create an actual function frame but instead register the parameters
5906// as local variables.
5907//
5908// So we evaluate something like:
5909//
5910// bool thisfunc() {
5911// auto Arg0 = Args[0];
5912// ...
5913// return Condition;
5914// }
5915//
5916template <class Emitter>
5919 const Expr *This,
5920 const Expr *Condition) {
5921 // Instead of evaluating all parameters and trying to ignore failure,
5922 // we collect all the parameters used in the condition and only evaluate
5923 // those. Note that we still ignore failure in the loop below because the
5924 // failure might be inconsequential in the end,
5925 // e.g. in the case of `true || x`.
5926 ParamFinder PF;
5928
5929 LocalScope<Emitter> ArgScope(this);
5930 for (const ParmVarDecl *PVD : PF.FoundParams) {
5931 unsigned ParamIndex = 0;
5932 for (const ParmVarDecl *P : Callee->parameters()) {
5933 if (P == PVD)
5934 break;
5935 ++ParamIndex;
5936 }
5937
5938 const Expr *Arg = Args[ParamIndex];
5939 const ParmVarDecl *Param = Callee->getParamDecl(ParamIndex);
5940 if (OptPrimType ParamT = classify(Param->getType())) {
5941 unsigned ArgOffset =
5942 allocateLocalPrimitive(Param, *ParamT, /*IsConst=*/true);
5943 if (!this->visit(Arg))
5944 continue;
5945 if (!this->emitSetLocal(*ParamT, ArgOffset, Arg))
5946 return false;
5947 } else {
5948 UnsignedOrNone ArgOffset = this->allocateLocal(Param, Param->getType());
5949 if (!ArgOffset)
5950 return false;
5951 if (!this->emitGetPtrLocal(*ArgOffset, Arg))
5952 return false;
5953 if (!this->visitInitializerPop(Arg))
5954 continue;
5955 }
5956 }
5957
5958 if (This) {
5959 // We abuse the init stack for this and tell it to use
5960 // either a local variable or another decl for the This pointer.
5961 this->InitStackActive = true;
5962
5963 if (This->getType()->isPointerType()) {
5964 // Nothing to do here, the evaluation will fail if the instance
5965 // pointer is used.
5966 } else if (const auto *DRE = dyn_cast<DeclRefExpr>(This)) {
5967 InitStack.push_back(InitLink::Decl(DRE->getDecl()));
5968 } else {
5969 assert(!canClassify(This->getType()));
5970 UnsignedOrNone ArgOffset = this->allocateLocal(This, This->getType());
5971 if (!ArgOffset)
5972 return false;
5973 if (!this->emitGetPtrLocal(*ArgOffset, This))
5974 return false;
5975 if (!this->visitInitializerPop(This))
5976 return false;
5977 this->InitStack.push_back(InitLink::Temp(*ArgOffset));
5978 }
5979 }
5980
5981 // Destruction of the argument values is part of the callee frame,
5982 // so we simply ignore them here.
5983 this->VarScope = nullptr;
5984
5985 LocalScope<Emitter> RetScope(this);
5986 if (!this->visit(Condition))
5987 return false;
5988 if (!RetScope.destroyLocals())
5989 return false;
5990
5991 // Result of the condition should be on the stack.
5992 return this->emitRet(PT_Bool, Condition);
5993}
5994
5995template <class Emitter>
5997 SourceInfo Info) {
5998 assert(!Val.isIndeterminate() && "Needs to be checked before");
5999 assert(!DiscardResult);
6000 if (Val.isInt())
6001 return this->emitConst(Val.getInt(), ValType, Info);
6002 if (Val.isFloat())
6003 return this->emitFloat(Val.getFloat(), Info);
6004
6005 if (Val.isMemberPointer()) {
6006 if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl()) {
6007 if (!this->emitGetMemberPtr(MemberDecl, Info))
6008 return false;
6009
6010 bool IsDerived = Val.isMemberPointerToDerivedMember();
6011 // Apply the member pointer path.
6012 for (const CXXRecordDecl *PathEntry : Val.getMemberPointerPath()) {
6013 if (!this->emitCopyMemberPtrPath(PathEntry, IsDerived, Info))
6014 return false;
6015 }
6016
6017 return true;
6018 }
6019 return this->emitNullMemberPtr(0, nullptr, Info);
6020 }
6021
6022 if (Val.isLValue()) {
6023 if (Val.isNullPointer())
6024 return this->emitNull(ValType, 0, nullptr, Info);
6025
6027 QualType EntryType;
6028 if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>()) {
6029 // Try to avoid evaluating the BaseExpr multiple times.
6030 if (UnsignedOrNone GlobalIndex = P.getGlobal(BaseExpr);
6031 GlobalIndex && P.isGlobalInitialized(*GlobalIndex)) {
6032 if (!this->emitGetPtrGlobal(*GlobalIndex, Info))
6033 return false;
6034 } else {
6035 if (!this->visit(BaseExpr))
6036 return false;
6037 }
6038 EntryType = BaseExpr->getType();
6039 } else if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) {
6040 if (!this->visitDeclRef(VD, Info.asExpr()))
6041 return false;
6042 EntryType = VD->getType();
6043 } else if (Base.is<TypeInfoLValue>()) {
6044 // The type_info object for the type the lvalue describes.
6045 const Type *OperandType = Base.get<TypeInfoLValue>()
6046 .getType()
6047 ->getCanonicalTypeUnqualified()
6048 .getTypePtr();
6049 EntryType = Base.getTypeInfoType();
6050 if (!this->emitGetTypeid(OperandType, EntryType.getTypePtr(), Info))
6051 return false;
6052 } else if (!Base) {
6053 // An integer cast to a pointer.
6054 const Expr *E = Info.asExpr();
6055 if (!E)
6056 return false;
6057 QualType PtrType = E->isGLValue()
6058 ? Ctx.getASTContext().getPointerType(E->getType())
6059 : E->getType();
6060 uint64_t Offset = Val.getLValueOffset().getQuantity();
6061 if (!this->emitConstUint64(Offset, Info))
6062 return false;
6063 if (!this->emitGetIntPtrUint64(PtrType.getTypePtr(), Info))
6064 return false;
6065 EntryType = PtrType->getPointeeType();
6066 } else {
6067 return false;
6068 }
6069
6070 if (Val.hasLValuePath()) {
6071 for (auto &Entry : Val.getLValuePath()) {
6072 if (EntryType->isArrayType()) {
6073 uint64_t Index = Entry.getAsArrayIndex();
6074 QualType ElemType =
6075 EntryType->getAsArrayTypeUnsafe()->getElementType();
6076 if (!this->emitConst(Index, PT_Uint64, Info))
6077 return false;
6078 if (!this->emitArrayElemPtrPop(PT_Uint64, Info))
6079 return false;
6080 EntryType = ElemType;
6081 } else {
6082 assert(EntryType->isRecordType());
6083 const Record *EntryRecord = getRecord(EntryType);
6084 if (!EntryRecord)
6085 return false;
6086
6087 const Decl *BaseOrMember = Entry.getAsBaseOrMember().getPointer();
6088 if (const auto *FD = dyn_cast<FieldDecl>(BaseOrMember)) {
6089 unsigned EntryOffset = EntryRecord->getField(FD)->Offset;
6090 if (!this->emitGetPtrFieldPop(EntryOffset, Info))
6091 return false;
6092 EntryType = FD->getType();
6093 } else {
6094 const auto *Base = cast<CXXRecordDecl>(BaseOrMember);
6095 if (const Record::Base *B = EntryRecord->getBaseOrNull(Base)) {
6096 if (!this->emitGetPtrBasePop(B->Offset, /*NullOK=*/false, Info))
6097 return false;
6098 } else {
6099 // Must be a virtual base.
6100 assert(EntryRecord->findVirtualBase(Base));
6101 if (!this->emitGetPtrVirtBasePop(Base, Info))
6102 return false;
6103 }
6104 EntryType = Ctx.getASTContext().getCanonicalTagType(Base);
6105 }
6106 }
6107 }
6108 }
6109
6110 if (isPtrType(ValType))
6111 return true;
6112
6113 // A pointer cast to an integer is stored as an lvalue; cast it the same
6114 // way the source expression does.
6115 if (ValType == PT_IntAP || ValType == PT_IntAPS) {
6116 const Expr *E = Info.asExpr();
6117 if (!E)
6118 return false;
6119 uint32_t BitWidth = Ctx.getBitWidth(E->getType());
6120 return ValType == PT_IntAP
6121 ? this->emitCastPointerIntegralAP(BitWidth, Info)
6122 : this->emitCastPointerIntegralAPS(BitWidth, Info);
6123 }
6124 return this->emitCastPointerIntegral(ValType, Info);
6125 }
6126
6127 return false;
6128}
6129
6130template <class Emitter>
6132 SourceInfo Info, QualType T,
6133 bool IsCompleteClass) {
6134 if (Val.isStruct()) {
6135 const Record *R = this->getRecord(T);
6136 assert(R);
6137
6138 assert(R->getNumBases() == Val.getStructNumBases());
6139 if (IsCompleteClass)
6140 assert(R->getNumVirtualBases() == Val.getStructNumVirtualBases());
6141
6142 for (unsigned I = 0, N = Val.getStructNumBases(); I != N; ++I) {
6143 const APValue &B = Val.getStructBase(I);
6144 if (B.isIndeterminate())
6145 continue;
6146 const Record::Base *RB = R->getBase(I);
6147 QualType BaseType = Ctx.getASTContext().getCanonicalTagType(RB->Decl);
6148
6149 if (!this->emitGetPtrBase(RB->Offset, Info))
6150 return false;
6151 if (!this->visitAPValueInitializer(B, Info, BaseType,
6152 /*IsCompleteClass=*/false))
6153 return false;
6154 if (!this->emitFinishInitPop(Info))
6155 return false;
6156 }
6157
6158 for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
6159 const APValue &F = Val.getStructField(I);
6160 if (F.isIndeterminate())
6161 continue;
6162 const Record::Field *RF = R->getField(I);
6163 QualType FieldType = RF->Decl->getType();
6164 // Fields.
6165 if (OptPrimType PT = RF->T) {
6166 if (!this->visitAPValue(F, *PT, Info))
6167 return false;
6168 if (!this->emitInitField(*PT, RF->Offset, Info))
6169 return false;
6170 } else {
6171 if (!this->emitGetPtrField(RF->Offset, Info))
6172 return false;
6173 if (!this->visitAPValueInitializer(F, Info, FieldType))
6174 return false;
6175 if (!this->emitFinishInitPop(Info))
6176 return false;
6177 }
6178 }
6179
6180 // Virtual Bases.
6181 if (IsCompleteClass) {
6182 for (unsigned I = 0, N = Val.getStructNumVirtualBases(); I != N; ++I) {
6183 const APValue &B = Val.getStructVirtualBase(I);
6184 if (B.isIndeterminate())
6185 continue;
6186 const Record::Base *RB = R->getVirtualBase(I);
6187 QualType BaseType = Ctx.getASTContext().getCanonicalTagType(RB->Decl);
6188
6189 if (!this->emitGetPtrVirtBase(cast<CXXRecordDecl>(RB->R->getDecl()),
6190 Info))
6191 return false;
6192 if (!this->visitAPValueInitializer(B, Info, BaseType,
6193 /*IsCompleteClass=*/false))
6194 return false;
6195 if (!this->emitFinishInitPop(Info))
6196 return false;
6197 }
6198 }
6199
6200 return true;
6201 }
6202 if (Val.isUnion()) {
6203 const FieldDecl *UnionField = Val.getUnionField();
6204 if (!UnionField)
6205 return true;
6206 const Record *R = this->getRecord(T);
6207 assert(R);
6208 const APValue &F = Val.getUnionValue();
6209 if (F.isIndeterminate())
6210 return true;
6211 const Record::Field *RF = R->getField(UnionField);
6212 QualType FieldType = RF->Decl->getType();
6213
6214 if (OptPrimType PT = RF->T) {
6215 if (!this->visitAPValue(F, *PT, Info))
6216 return false;
6217 if (RF->isBitField())
6218 return this->emitInitBitFieldActivate(*PT, RF->Offset, RF->bitWidth(),
6219 Info);
6220 return this->emitInitFieldActivate(*PT, RF->Offset, Info);
6221 }
6222
6223 if (!this->emitGetPtrField(RF->Offset, Info))
6224 return false;
6225 if (!this->emitActivate(Info))
6226 return false;
6227 if (!this->visitAPValueInitializer(F, Info, FieldType))
6228 return false;
6229 return this->emitPopPtr(Info);
6230 }
6231 if (Val.isArray()) {
6232 unsigned InitializedElems = Val.getArrayInitializedElts();
6233 const auto *ArrType = T->getAsArrayTypeUnsafe();
6234 QualType ElemType = ArrType->getElementType();
6235 OptPrimType ElemT = classify(ElemType);
6236
6237 for (unsigned A = 0, AN = Val.getArraySize(); A != AN; ++A) {
6238 const APValue &Elem = A >= InitializedElems
6239 ? Val.getArrayFiller()
6240 : Val.getArrayInitializedElt(A);
6241 if (Elem.isIndeterminate())
6242 continue;
6243
6244 if (ElemT) {
6245 if (!this->visitAPValue(Elem, *ElemT, Info))
6246 return false;
6247 if (!this->emitInitElem(*ElemT, A, Info))
6248 return false;
6249 } else {
6250 if (!this->emitConstUint32(A, Info))
6251 return false;
6252 if (!this->emitArrayElemPtrUint32(Info))
6253 return false;
6254 if (!this->visitAPValueInitializer(Elem, Info, ElemType))
6255 return false;
6256 if (!this->emitPopPtr(Info))
6257 return false;
6258 }
6259 }
6260 return true;
6261 }
6262 if (Val.isComplexFloat()) {
6263 assert(T->isComplexType());
6264 if (!this->emitFloat(Val.getComplexFloatReal(), Info))
6265 return false;
6266 if (!this->emitInitElemFloat(0, Info))
6267 return false;
6268 if (!this->emitFloat(Val.getComplexFloatImag(), Info))
6269 return false;
6270 if (!this->emitInitElemFloat(1, Info))
6271 return false;
6272 return true;
6273 }
6274 if (Val.isComplexInt()) {
6275 assert(T->isComplexIntegerType());
6276 PrimType IntT = classifyPrim(T->castAs<ComplexType>()->getElementType());
6277 if (!this->emitConst(Val.getComplexIntReal(), IntT, Info))
6278 return false;
6279 if (!this->emitInitElem(IntT, 0, Info))
6280 return false;
6281 if (!this->emitConst(Val.getComplexIntImag(), IntT, Info))
6282 return false;
6283 if (!this->emitInitElem(IntT, 1, Info))
6284 return false;
6285 return true;
6286 }
6287 if (Val.isVector()) {
6288 assert(T->isVectorType());
6289 PrimType ElemT = classifyPrim(T->castAs<VectorType>()->getElementType());
6290 for (unsigned I = 0, N = Val.getVectorLength(); I != N; ++I) {
6291 const APValue &Elem = Val.getVectorElt(I);
6292 if (Elem.isIndeterminate())
6293 continue;
6294 if (!this->visitAPValue(Elem, ElemT, Info))
6295 return false;
6296 if (!this->emitInitElem(ElemT, I, Info))
6297 return false;
6298 }
6299 return true;
6300 }
6301
6302 // TODO: Other types.
6303 return false;
6304}
6305
6306template <class Emitter>
6308 if (P.getGlobal(VD))
6309 return true;
6310
6311 UnsignedOrNone GlobalIndex =
6312 P.createGlobal(VD, /*Init=*/nullptr, /*IsConstexprUnknown=*/false,
6313 /*CanBeExtern=*/false);
6314 if (!GlobalIndex) {
6315 llvm_unreachable("Why didn't that work?");
6316 }
6317
6318 assert(canClassify(VD->getType()) &&
6319 "registerRedecl should only be called with primitive values");
6320
6321 PrimType T = classifyPrim(VD->getType());
6322 if (!visitAPValue(Val, T, VD))
6323 return false;
6324 return this->emitInitGlobal(T, *GlobalIndex, {});
6325}
6326
6327template <class Emitter>
6329 unsigned BuiltinID) {
6330 const ASTContext &ASTCtx = Ctx.getASTContext();
6331
6332 // BuiltinID is the raw ID baked into the bytecode. The "is constant
6333 // evaluated" gate needs the raw ID so that auxiliary-target IDs resolve into
6334 // the correct (aux-target) builtin records.
6335 if (!Ctx.getASTContext().BuiltinInfo.isConstantEvaluated(BuiltinID))
6336 return this->emitInvalid(E);
6337
6338 // Convert an auxiliary x86 target builtin ID to its canonical X86::BI* value
6339 // so the target-specific cases below (and the handlers they call) match. This
6340 // is a cheap integer operation (a single comparison for the common,
6341 // target-independent case); we deliberately avoid re-deriving the ID from the
6342 // call expression, which is comparatively slow.
6343 BuiltinID = ConvertBuiltinIDToX86BuiltinID(ASTCtx, BuiltinID);
6344
6345 if (BuiltinID == Builtin::BI__builtin_constant_p) {
6346 // Void argument is always invalid and harder to handle later.
6347 if (E->getArg(0)->getType()->isVoidType()) {
6348 if (DiscardResult)
6349 return true;
6350 return this->emitConst(0, E);
6351 }
6352
6353 if (!this->emitStartSpeculation(E))
6354 return false;
6355 LabelTy EndLabel = this->getLabel();
6356 if (!this->speculate(E, EndLabel))
6357 return false;
6358 if (!this->emitEndSpeculation(E))
6359 return false;
6360 this->fallthrough(EndLabel);
6361 if (DiscardResult)
6362 return this->emitPop(classifyPrim(E), E);
6363 return true;
6364 }
6365
6366 // For these, we're expected to ultimately return an APValue pointing
6367 // to the CallExpr. This is needed to get the correct codegen.
6368 if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
6369 BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
6370 BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
6371 BuiltinID == Builtin::BI__builtin_function_start) {
6372 if (DiscardResult)
6373 return true;
6374 return this->emitDummyPtr(E, E);
6375 }
6376
6378 OptPrimType ReturnT = classify(E);
6379
6380 // Non-primitive return type. Prepare storage.
6381 if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
6382 UnsignedOrNone LocalIndex = allocateLocal(E);
6383 if (!LocalIndex)
6384 return false;
6385 if (!this->emitGetPtrLocal(*LocalIndex, E))
6386 return false;
6387 }
6388
6389 // Prepare function arguments including special cases.
6390 switch (BuiltinID) {
6391 case Builtin::BI__builtin_object_size:
6392 case Builtin::BI__builtin_dynamic_object_size: {
6393 assert(E->getNumArgs() == 2);
6394 const Expr *Arg0 = E->getArg(0);
6395 if (Arg0->isGLValue()) {
6396 if (!this->visit(Arg0))
6397 return false;
6398
6399 } else {
6401 return false;
6402 }
6403 if (!this->visit(E->getArg(1)))
6404 return false;
6405
6406 } break;
6407 case Builtin::BI__assume:
6408 case Builtin::BI__builtin_assume:
6409 // Argument is not evaluated.
6410 break;
6411 case Builtin::BI__atomic_is_lock_free:
6412 case Builtin::BI__atomic_always_lock_free: {
6413 assert(E->getNumArgs() == 2);
6414 if (!this->visit(E->getArg(0)))
6415 return false;
6416 if (!this->visitAsLValue(E->getArg(1)))
6417 return false;
6418 } break;
6419
6420 default:
6421 if (!Context::isUnevaluatedBuiltin(BuiltinID)) {
6422 // Put arguments on the stack.
6423 for (const auto *Arg : E->arguments()) {
6424 if (!this->visit(Arg))
6425 return false;
6426 }
6427 }
6428 }
6429
6430 if (!this->emitCallBI(E, BuiltinID, E))
6431 return false;
6432
6433 if (DiscardResult && !ReturnType->isVoidType())
6434 return this->emitPop(ReturnT.value_or(PT_Ptr), E);
6435
6436 return true;
6437}
6438
6440 if (!MD || !MD->isDefaulted())
6441 return false;
6443 return false;
6444 assert(!MD->getParent()->isInvalidDecl());
6445 return MD->getParent()->isUnion() ||
6447}
6448
6449template <class Emitter>
6451 if (E->containsErrors())
6452 return false;
6453 const FunctionDecl *FuncDecl = E->getDirectCallee();
6454
6455 if (FuncDecl) {
6456 if (unsigned BuiltinID = FuncDecl->getBuiltinID())
6457 return VisitBuiltinCallExpr(E, BuiltinID);
6458
6459 // Calls to replaceable operator new/operator delete.
6461 if (FuncDecl->getDeclName().isAnyOperatorNew())
6462 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_new);
6463 assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete ||
6464 FuncDecl->getDeclName().getCXXOverloadedOperator() ==
6465 OO_Array_Delete);
6466 return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_delete);
6467 }
6468
6469 // Explicit calls to trivial destructors
6470 if (const auto *DD = dyn_cast<CXXDestructorDecl>(FuncDecl);
6471 DD && DD->isTrivial()) {
6472 const auto *MemberCall = cast<CXXMemberCallExpr>(E);
6473 if (!this->visit(MemberCall->getImplicitObjectArgument()))
6474 return false;
6475 return this->emitCheckDestruction(E) && this->emitEndLifetime(E) &&
6476 this->emitPopPtr(E);
6477 }
6478 }
6479
6480 // Don't generate code for method with invalid parent decls.
6481 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
6482 MD && MD->getParent()->isInvalidDecl())
6483 return false;
6484
6485 LocalScope<Emitter> CallScope(this, ScopeKind::Call);
6486 ArrayRef<const Expr *> Args(E->getArgs(), E->getNumArgs());
6487 bool ActivateLHS = false;
6488
6489 // Emit a special op for trivial copy/move operators.
6490 if (isTrivialMemoryOperation(dyn_cast_if_present<CXXMethodDecl>(FuncDecl))) {
6491 const Function *Func = getFunction(FuncDecl);
6492 if (!Func)
6493 return false;
6494
6495 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
6496 OCE && OCE->isAssignmentOp()) {
6497 const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl();
6498 ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor();
6499 }
6500 if (const auto *MCE = dyn_cast<CXXMemberCallExpr>(E))
6501 if (!this->visit(MCE->getImplicitObjectArgument()))
6502 return false;
6503
6504 if (!this->visitCallArgs(Args, FuncDecl, /*ActivateLHS=*/ActivateLHS,
6506 return false;
6507
6508 if (!this->emitTrivialCopy(ActivateLHS, Func, E))
6509 return false;
6510
6511 if (!DiscardResult)
6512 return CallScope.destroyLocals();
6513 return this->emitPopPtr(E) && CallScope.destroyLocals();
6514 }
6515
6516 QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
6518 bool HasRVO = !ReturnType->isVoidType() && !T;
6519
6520 if (HasRVO) {
6521 if (DiscardResult) {
6522 // If we need to discard the return value but the function returns its
6523 // value via an RVO pointer, we need to create one such pointer just
6524 // for this call.
6525 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
6526 if (!this->emitGetPtrLocal(*LocalIndex, E))
6527 return false;
6528 }
6529 } else {
6530 // We need the result. Prepare a pointer to return or
6531 // dup the current one.
6532 if (!Initializing) {
6533 if (UnsignedOrNone LocalIndex = allocateLocal(E)) {
6534 if (!this->emitGetPtrLocal(*LocalIndex, E))
6535 return false;
6536 }
6537 }
6538 if (!this->emitDupPtr(E))
6539 return false;
6540 }
6541 }
6542
6543 const Expr *ReversedArgs[2];
6544 bool IsAssignmentOperatorCall = false;
6545 if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
6546 OCE && OCE->isAssignmentOp()) {
6547 // Just like with regular assignments, we need to special-case assignment
6548 // operators here and evaluate the RHS (the second arg) before the LHS (the
6549 // first arg). We fix this by using a Flip op later.
6550 assert(Args.size() == 2);
6551 const CXXRecordDecl *LHSRecord = Args[0]->getType()->getAsCXXRecordDecl();
6552 ActivateLHS = LHSRecord && LHSRecord->hasTrivialDefaultConstructor();
6553 IsAssignmentOperatorCall = true;
6554 ReversedArgs[0] = Args[1];
6555 ReversedArgs[1] = Args[0];
6556 Args = ReversedArgs;
6557 }
6558
6559 // Calling a static operator will still
6560 // pass the instance, but we don't need it.
6561 // Discard it here.
6562 if (isa<CXXOperatorCallExpr>(E)) {
6563 if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
6564 MD && MD->isStatic()) {
6565 if (!this->discard(E->getArg(0)))
6566 return false;
6567 // Drop first arg.
6568 Args = Args.drop_front();
6569 }
6570 }
6571
6572 bool Devirtualized = false;
6573 UnsignedOrNone CalleeOffset = std::nullopt;
6574 // Add the (optional, implicit) This pointer.
6575 if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
6576 if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
6577 // If we end up creating a CallPtr op for this, we need the base of the
6578 // member pointer as the instance pointer, and later extract the function
6579 // decl as the function pointer.
6580 const Expr *Callee = E->getCallee();
6581 CalleeOffset =
6582 this->allocateLocalPrimitive(Callee, PT_MemberPtr, /*IsConst=*/true);
6583 if (!this->visit(Callee))
6584 return false;
6585 if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
6586 return false;
6587 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
6588 return false;
6589 if (!this->emitGetMemberPtrBase(E))
6590 return false;
6591 } else {
6592 const auto *InstancePtr = MC->getImplicitObjectArgument();
6593 if (isa_and_nonnull<CXXDestructorDecl>(CompilingFunction) ||
6594 isa_and_nonnull<CXXConstructorDecl>(CompilingFunction)) {
6595 const auto *Stripped = stripCheckedDerivedToBaseCasts(InstancePtr);
6596 if (isa<CXXThisExpr>(Stripped)) {
6597 FuncDecl =
6598 cast<CXXMethodDecl>(FuncDecl)->getCorrespondingMethodInClass(
6599 Stripped->getType()->getPointeeType()->castAsCXXRecordDecl());
6600 Devirtualized = true;
6601 if (!this->visit(Stripped))
6602 return false;
6603 } else {
6604 if (!this->visit(InstancePtr))
6605 return false;
6606 }
6607 } else {
6608 if (!this->visit(InstancePtr))
6609 return false;
6610 }
6611 }
6612 } else if (const auto *PD =
6613 dyn_cast<CXXPseudoDestructorExpr>(E->getCallee())) {
6614 if (!this->emitCheckPseudoDtor(E))
6615 return false;
6616 const Expr *Base = PD->getBase();
6617 // E.g. `using T = int; 0.~T();`.
6618 if (OptPrimType BaseT = classify(Base); !BaseT || BaseT != PT_Ptr)
6619 return this->discard(Base);
6620 if (!this->visit(Base))
6621 return false;
6622 return this->emitPseudoDtor(E);
6623 } else if (!FuncDecl) {
6624 const Expr *Callee = E->getCallee();
6625 CalleeOffset =
6626 this->allocateLocalPrimitive(Callee, PT_Ptr, /*IsConst=*/true);
6627 if (!this->visit(Callee))
6628 return false;
6629 if (!this->emitSetLocal(PT_Ptr, *CalleeOffset, E))
6630 return false;
6631 }
6632
6633 if (!this->visitCallArgs(Args, FuncDecl, ActivateLHS,
6635 return false;
6636
6637 // Undo the argument reversal we did earlier.
6638 if (IsAssignmentOperatorCall) {
6639 assert(Args.size() == 2);
6640 PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
6641 PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
6642 if (!this->emitFlip(Arg2T, Arg1T, E))
6643 return false;
6644 }
6645
6646 if (FuncDecl) {
6647 const Function *Func = getFunction(FuncDecl);
6648 if (!Func)
6649 return false;
6650
6651 // In error cases, the function may be called with fewer arguments than
6652 // parameters.
6653 if (E->getNumArgs() < Func->getNumWrittenParams())
6654 return false;
6655
6656 assert(HasRVO == Func->hasRVO());
6657
6658 bool HasQualifier = false;
6659 if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
6660 HasQualifier = ME->hasQualifier();
6661
6662 bool IsVirtual = false;
6663 if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
6664 IsVirtual = !Devirtualized && MD->isVirtual();
6665
6666 // In any case call the function. The return value will end up on the stack
6667 // and if the function has RVO, we already have the pointer on the stack to
6668 // write the result into.
6669 if (IsVirtual && !HasQualifier) {
6670 uint32_t VarArgSize = 0;
6671 unsigned NumParams =
6672 Func->getNumWrittenParams() +
6673 (isa<CXXOperatorCallExpr>(E) && Func->hasImplicitThisPointer());
6674 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
6675 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
6676
6677 if (!this->emitCallVirt(Func, VarArgSize, E))
6678 return false;
6679 } else if (Func->isVariadic()) {
6680 uint32_t VarArgSize = 0;
6681 unsigned NumParams =
6682 Func->getNumWrittenParams() +
6683 (isa<CXXOperatorCallExpr>(E) && Func->hasImplicitThisPointer());
6684 for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
6685 VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
6686 if (!this->emitCallVar(Func, VarArgSize, E))
6687 return false;
6688 } else {
6689 if (!this->emitCall(Func, 0, E))
6690 return false;
6691 }
6692 } else {
6693 // Indirect call. Visit the callee, which will leave a FunctionPointer on
6694 // the stack. Cleanup of the returned value if necessary will be done after
6695 // the function call completed.
6696
6697 // Sum the size of all args from the call expr.
6698 uint32_t ArgSize = 0;
6699 for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
6700 ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
6701
6702 // Get the callee, either from a member pointer or function pointer saved in
6703 // CalleeOffset.
6704 if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
6705 if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
6706 return false;
6707 if (!this->emitGetMemberPtrDecl(E))
6708 return false;
6709 } else {
6710 if (!this->emitGetLocal(PT_Ptr, *CalleeOffset, E))
6711 return false;
6712 }
6713 if (!this->emitCallPtr(ArgSize, E, E))
6714 return false;
6715 }
6716
6717 // Cleanup for discarded return values.
6718 if (DiscardResult && !ReturnType->isVoidType() && T)
6719 return this->emitPop(*T, E) && CallScope.destroyLocals();
6720
6721 return CallScope.destroyLocals();
6722}
6723
6724template <class Emitter>
6726 SourceLocScope<Emitter> SLS(this, E);
6727
6728 return this->delegate(E->getExpr());
6729}
6730
6731template <class Emitter>
6733 SourceLocScope<Emitter> SLS(this, E);
6734
6735 return this->delegate(E->getExpr());
6736}
6737
6738template <class Emitter>
6740 if (DiscardResult)
6741 return true;
6742
6743 return this->emitConstBool(E->getValue(), E);
6744}
6745
6746template <class Emitter>
6748 const CXXNullPtrLiteralExpr *E) {
6749 if (DiscardResult)
6750 return true;
6751
6752 uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
6753 return this->emitNullPtr(Val, nullptr, E);
6754}
6755
6756template <class Emitter>
6758 if (DiscardResult)
6759 return true;
6760
6761 assert(E->getType()->isIntegerType());
6762
6764 return this->emitZero(T, E);
6765}
6766
6767template <class Emitter>
6769 if (DiscardResult)
6770 return true;
6771
6772 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
6773 if (this->LambdaThisCapture.Offset > 0) {
6774 if (this->LambdaThisCapture.IsPtr)
6775 return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
6776 return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
6777 }
6778 }
6779
6780 // In some circumstances, the 'this' pointer does not actually refer to the
6781 // instance pointer of the current function frame, but e.g. to the declaration
6782 // currently being initialized. Here we emit the necessary instruction(s) for
6783 // this scenario.
6784 if (!InitStackActive || InitStack.empty())
6785 return this->emitThis(E);
6786
6787 // If our init stack is, for example:
6788 // 0 Stack: 3 (decl)
6789 // 1 Stack: 6 (init list)
6790 // 2 Stack: 1 (field)
6791 // 3 Stack: 6 (init list)
6792 // 4 Stack: 1 (field)
6793 //
6794 // We want to find the LAST element in it that's an init list,
6795 // which is marked with the K_InitList marker. The index right
6796 // before that points to an init list. We need to find the
6797 // elements before the K_InitList element that point to a base
6798 // (e.g. a decl or This), optionally followed by field, elem, etc.
6799 // In the example above, we want to emit elements [0..2].
6800 unsigned StartIndex = 0;
6801 unsigned EndIndex = 0;
6802 // Find the init list.
6803 for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
6804 if (InitStack[StartIndex].Kind == InitLink::K_DIE) {
6805 EndIndex = StartIndex;
6806 --StartIndex;
6807 break;
6808 }
6809 }
6810
6811 // Walk backwards to find the base.
6812 for (; StartIndex > 0; --StartIndex) {
6813 if (InitStack[StartIndex].Kind == InitLink::K_InitList)
6814 continue;
6815
6816 if (InitStack[StartIndex].Kind != InitLink::K_Field &&
6817 InitStack[StartIndex].Kind != InitLink::K_Elem &&
6818 InitStack[StartIndex].Kind != InitLink::K_Base &&
6819 InitStack[StartIndex].Kind != InitLink::K_DIE)
6820 break;
6821 }
6822
6823 if (StartIndex == 0 && EndIndex == 0)
6824 EndIndex = InitStack.size() - 1;
6825
6826 assert(InitStack[StartIndex].Kind == InitLink::K_Decl ||
6827 InitStack[StartIndex].Kind == InitLink::K_This ||
6828 InitStack[StartIndex].Kind == InitLink::K_Temp ||
6829 InitStack[StartIndex].Kind == InitLink::K_RVO);
6830
6831 // NOTE: This could be StartIndex < EndIndex, but we're also abusing the
6832 // InitStack mechanism in visitWithSubstitutions to have the This pointer
6833 // _just_ be a local variable.
6834 assert(StartIndex <= EndIndex);
6835
6836 // Emit the instructions.
6837 for (unsigned I = StartIndex; I != (EndIndex + 1); ++I) {
6838 if (InitStack[I].Kind == InitLink::K_InitList ||
6839 InitStack[I].Kind == InitLink::K_DIE)
6840 continue;
6841 if (!InitStack[I].template emit<Emitter>(this, E))
6842 return false;
6843 }
6844 return true;
6845}
6846
6847template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
6848 switch (S->getStmtClass()) {
6849 case Stmt::CompoundStmtClass:
6851 case Stmt::DeclStmtClass:
6852 return visitDeclStmt(cast<DeclStmt>(S), /*EvaluateConditionDecl=*/true);
6853 case Stmt::ReturnStmtClass:
6855 case Stmt::IfStmtClass:
6856 return visitIfStmt(cast<IfStmt>(S));
6857 case Stmt::WhileStmtClass:
6859 case Stmt::DoStmtClass:
6860 return visitDoStmt(cast<DoStmt>(S));
6861 case Stmt::ForStmtClass:
6862 return visitForStmt(cast<ForStmt>(S));
6863 case Stmt::CXXForRangeStmtClass:
6865 case Stmt::BreakStmtClass:
6867 case Stmt::ContinueStmtClass:
6869 case Stmt::SwitchStmtClass:
6871 case Stmt::CaseStmtClass:
6872 return visitCaseStmt(cast<CaseStmt>(S));
6873 case Stmt::DefaultStmtClass:
6875 case Stmt::AttributedStmtClass:
6877 case Stmt::CXXTryStmtClass:
6879 case Stmt::NullStmtClass:
6880 return true;
6881 // Always invalid statements.
6882 case Stmt::GCCAsmStmtClass:
6883 case Stmt::MSAsmStmtClass:
6884 case Stmt::GotoStmtClass:
6885 return this->emitInvalid(S);
6886 case Stmt::LabelStmtClass:
6887 return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
6888 case Stmt::CXXExpansionStmtInstantiationClass:
6891 default: {
6892 if (const auto *E = dyn_cast<Expr>(S))
6893 return this->discard(E);
6894 return false;
6895 }
6896 }
6897}
6898
6899template <class Emitter>
6902 for (const auto *InnerStmt : S->body())
6903 if (!visitStmt(InnerStmt))
6904 return false;
6905 return Scope.destroyLocals();
6906}
6907
6908template <class Emitter>
6909bool Compiler<Emitter>::maybeEmitDeferredVarInit(const VarDecl *VD) {
6910 if (auto *DD = dyn_cast_if_present<DecompositionDecl>(VD)) {
6911 for (auto *BD : DD->flat_bindings())
6912 if (auto *KD = BD->getHoldingVar();
6913 KD && !this->visitVarDecl(KD, KD->getInit()))
6914 return false;
6915 }
6916 return true;
6917}
6918
6920 assert(FD);
6921 assert(FD->getParent()->isUnion());
6922 const CXXRecordDecl *CXXRD =
6924 return !CXXRD || CXXRD->hasTrivialDefaultConstructor();
6925}
6926
6927template <class Emitter> bool Compiler<Emitter>::refersToUnion(const Expr *E) {
6928 for (;;) {
6929 if (const auto *ME = dyn_cast<MemberExpr>(E)) {
6930 if (const auto *FD = dyn_cast<FieldDecl>(ME->getMemberDecl());
6931 FD && FD->getParent()->isUnion() && hasTrivialDefaultCtorParent(FD))
6932 return true;
6933 E = ME->getBase();
6934 continue;
6935 }
6936
6937 if (const auto *ASE = dyn_cast<ArraySubscriptExpr>(E)) {
6938 E = ASE->getBase()->IgnoreImplicit();
6939 continue;
6940 }
6941
6942 if (const auto *ICE = dyn_cast<ImplicitCastExpr>(E);
6943 ICE && (ICE->getCastKind() == CK_NoOp ||
6944 ICE->getCastKind() == CK_DerivedToBase ||
6945 ICE->getCastKind() == CK_UncheckedDerivedToBase)) {
6946 E = ICE->getSubExpr();
6947 continue;
6948 }
6949
6950 if (const auto *This = dyn_cast<CXXThisExpr>(E)) {
6951 const auto *ThisRecord =
6952 This->getType()->getPointeeType()->getAsRecordDecl();
6953 if (!ThisRecord->isUnion())
6954 return false;
6955 // Otherwise, always activate if we're in the ctor.
6956 if (const auto *Ctor =
6957 dyn_cast_if_present<CXXConstructorDecl>(CompilingFunction))
6958 return Ctor->getParent() == ThisRecord;
6959 return false;
6960 }
6961
6962 break;
6963 }
6964 return false;
6965}
6966
6967template <class Emitter>
6969 bool EvaluateConditionDecl) {
6970 for (const auto *D : DS->decls()) {
6973 continue;
6974
6975 if (const auto *ESD = dyn_cast<CXXExpansionStmtDecl>(D)) {
6976 assert(ESD->getInstantiations() && "not expanded?");
6977 if (!this->visitStmt(ESD->getInstantiations()))
6978 return false;
6979 continue;
6980 }
6981
6982 const auto *VD = dyn_cast<VarDecl>(D);
6983 if (!VD)
6984 return false;
6985 if (!this->visitVarDecl(VD, VD->getInit()))
6986 return false;
6987
6988 // Register decomposition decl holding vars.
6989 if (EvaluateConditionDecl && !this->maybeEmitDeferredVarInit(VD))
6990 return false;
6991 }
6992
6993 return true;
6994}
6995
6996template <class Emitter>
6998 if (this->InStmtExpr)
6999 return this->emitUnsupported(RS);
7000
7001 if (const Expr *RE = RS->getRetValue()) {
7002 LocalScope<Emitter> RetScope(this);
7003 if (ReturnType) {
7004 // Primitive types are simply returned.
7005 if (!this->visit(RE))
7006 return false;
7007 this->emitCleanup();
7008 return this->emitRet(*ReturnType, RS);
7009 }
7010
7011 if (RE->getType()->isVoidType()) {
7012 if (!this->visit(RE))
7013 return false;
7014 } else {
7015 if (RE->containsErrors())
7016 return false;
7017
7019 // RVO - construct the value in the return location.
7020 if (!this->emitRVOPtr(RE))
7021 return false;
7022 if (!this->visitInitializerPop(RE))
7023 return false;
7024
7025 this->emitCleanup();
7026 return this->emitRetVoid(RS);
7027 }
7028 }
7029
7030 // Void return.
7031 this->emitCleanup();
7032 return this->emitRetVoid(RS);
7033}
7034
7035template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
7036 LocalScope<Emitter> IfScope(this);
7037
7038 auto visitChildStmt = [&](const Stmt *S) -> bool {
7039 LocalScope<Emitter> SScope(this);
7040 if (!visitStmt(S))
7041 return false;
7042 return SScope.destroyLocals();
7043 };
7044
7045 if (auto *CondInit = IS->getInit()) {
7046 if (!visitStmt(CondInit))
7047 return false;
7048 }
7049
7050 if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt()) {
7051 if (!visitDeclStmt(CondDecl))
7052 return false;
7053 }
7054
7055 // Save ourselves compiling some code and the jumps, etc. if the condition is
7056 // stataically known to be either true or false. We could look at more cases
7057 // here, but I think all the ones that actually happen are using a
7058 // ConstantExpr.
7059 if (std::optional<bool> BoolValue = getBoolValue(IS->getCond())) {
7060 if (*BoolValue)
7061 return visitChildStmt(IS->getThen());
7062 if (const Stmt *Else = IS->getElse())
7063 return visitChildStmt(Else);
7064 return true;
7065 }
7066
7067 // Otherwise, compile the condition.
7068 if (IS->isNonNegatedConsteval()) {
7069 if (!this->emitIsConstantContext(IS))
7070 return false;
7071 } else if (IS->isNegatedConsteval()) {
7072 if (!this->emitIsConstantContext(IS))
7073 return false;
7074 if (!this->emitInv(IS))
7075 return false;
7076 } else {
7078 if (!this->visitBool(IS->getCond()))
7079 return false;
7080 if (!CondScope.destroyLocals())
7081 return false;
7082 }
7083
7084 if (!this->maybeEmitDeferredVarInit(IS->getConditionVariable()))
7085 return false;
7086
7087 if (const Stmt *Else = IS->getElse()) {
7088 LabelTy LabelElse = this->getLabel();
7089 LabelTy LabelEnd = this->getLabel();
7090 if (!this->jumpFalse(LabelElse, IS))
7091 return false;
7092 if (!visitChildStmt(IS->getThen()))
7093 return false;
7094 if (!this->jump(LabelEnd, IS))
7095 return false;
7096 this->emitLabel(LabelElse);
7097 if (!visitChildStmt(Else))
7098 return false;
7099 this->emitLabel(LabelEnd);
7100 } else {
7101 LabelTy LabelEnd = this->getLabel();
7102 if (!this->jumpFalse(LabelEnd, IS))
7103 return false;
7104 if (!visitChildStmt(IS->getThen()))
7105 return false;
7106 this->emitLabel(LabelEnd);
7107 }
7108
7109 if (!IfScope.destroyLocals())
7110 return false;
7111
7112 return true;
7113}
7114
7115template <class Emitter>
7117 const Expr *Cond = S->getCond();
7118 const Stmt *Body = S->getBody();
7119
7120 LabelTy CondLabel = this->getLabel(); // Label before the condition.
7121 LabelTy EndLabel = this->getLabel(); // Label after the loop.
7122 LocalScope<Emitter> WholeLoopScope(this);
7123 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
7124
7125 this->fallthrough(CondLabel);
7126 this->emitLabel(CondLabel);
7127
7128 // Start of the loop body {
7129 LocalScope<Emitter> CondScope(this);
7130
7131 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
7132 if (!visitDeclStmt(CondDecl))
7133 return false;
7134 }
7135
7136 if (!this->visitBool(Cond))
7137 return false;
7138
7139 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
7140 return false;
7141
7142 if (!this->jumpFalse(EndLabel, S))
7143 return false;
7144
7145 if (!this->visitStmt(Body))
7146 return false;
7147
7148 if (!CondScope.destroyLocals())
7149 return false;
7150 // } End of loop body.
7151
7152 if (!this->jump(CondLabel, S))
7153 return false;
7154 this->fallthrough(EndLabel);
7155 this->emitLabel(EndLabel);
7156
7157 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
7158}
7159
7160template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
7161 const Expr *Cond = S->getCond();
7162 const Stmt *Body = S->getBody();
7163
7164 LabelTy StartLabel = this->getLabel();
7165 LabelTy EndLabel = this->getLabel();
7166 LabelTy CondLabel = this->getLabel();
7167 LocalScope<Emitter> WholeLoopScope(this);
7168 LoopScope<Emitter> LS(this, S, EndLabel, CondLabel);
7169
7170 this->fallthrough(StartLabel);
7171 this->emitLabel(StartLabel);
7172
7173 {
7174 LocalScope<Emitter> CondScope(this);
7175 if (!this->visitStmt(Body))
7176 return false;
7177 this->fallthrough(CondLabel);
7178 this->emitLabel(CondLabel);
7179 if (!this->visitBool(Cond))
7180 return false;
7181
7182 if (!CondScope.destroyLocals())
7183 return false;
7184 }
7185 if (!this->jumpTrue(StartLabel, S))
7186 return false;
7187
7188 this->fallthrough(EndLabel);
7189 this->emitLabel(EndLabel);
7190 return WholeLoopScope.destroyLocals();
7191}
7192
7193template <class Emitter>
7195 // for (Init; Cond; Inc) { Body }
7196 const Stmt *Init = S->getInit();
7197 const Expr *Cond = S->getCond();
7198 const Expr *Inc = S->getInc();
7199 const Stmt *Body = S->getBody();
7200
7201 LabelTy EndLabel = this->getLabel();
7202 LabelTy CondLabel = this->getLabel();
7203 LabelTy IncLabel = this->getLabel();
7204
7205 LocalScope<Emitter> WholeLoopScope(this);
7206 if (Init && !this->visitStmt(Init))
7207 return false;
7208
7209 // Start of the loop body {
7210 this->fallthrough(CondLabel);
7211 this->emitLabel(CondLabel);
7212
7213 LocalScope<Emitter> CondScope(this);
7214 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
7215 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt()) {
7216 if (!visitDeclStmt(CondDecl))
7217 return false;
7218 }
7219
7220 if (Cond) {
7221 if (!this->visitBool(Cond))
7222 return false;
7223 if (!this->jumpFalse(EndLabel, S))
7224 return false;
7225 }
7226 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
7227 return false;
7228
7229 if (Body && !this->visitStmt(Body))
7230 return false;
7231
7232 this->fallthrough(IncLabel);
7233 this->emitLabel(IncLabel);
7234 if (Inc && !this->discard(Inc))
7235 return false;
7236
7237 if (!CondScope.destroyLocals())
7238 return false;
7239 if (!this->jump(CondLabel, S))
7240 return false;
7241 // } End of loop body.
7242
7243 this->emitLabel(EndLabel);
7244 // If we jumped out of the loop above, we still need to clean up the condition
7245 // scope.
7246 return CondScope.destroyLocals() && WholeLoopScope.destroyLocals();
7247}
7248
7249template <class Emitter>
7251 const Stmt *Init = S->getInit();
7252 const Expr *Cond = S->getCond();
7253 const Expr *Inc = S->getInc();
7254 const Stmt *Body = S->getBody();
7255 const Stmt *BeginStmt = S->getBeginStmt();
7256 const Stmt *RangeStmt = S->getRangeStmt();
7257 const Stmt *EndStmt = S->getEndStmt();
7258
7259 LabelTy EndLabel = this->getLabel();
7260 LabelTy CondLabel = this->getLabel();
7261 LabelTy IncLabel = this->getLabel();
7262 LocalScope<Emitter> WholeLoopScope(this);
7263 LoopScope<Emitter> LS(this, S, EndLabel, IncLabel);
7264
7265 // Emit declarations needed in the loop.
7266 if (Init && !this->visitStmt(Init))
7267 return false;
7268 if (!this->visitStmt(RangeStmt))
7269 return false;
7270 if (!this->visitStmt(BeginStmt))
7271 return false;
7272 if (!this->visitStmt(EndStmt))
7273 return false;
7274
7275 LocalScope<Emitter> CondScope(this);
7276 // Now the condition as well as the loop variable assignment.
7277 this->fallthrough(CondLabel);
7278 this->emitLabel(CondLabel);
7279 if (!this->visitBool(Cond))
7280 return false;
7281 if (!this->jumpFalse(EndLabel, S))
7282 return false;
7283
7284 if (!this->visitDeclStmt(S->getLoopVarStmt(), /*EvaluateConditionDecl=*/true))
7285 return false;
7286
7287 // Body.
7288 {
7289 if (!this->visitStmt(Body))
7290 return false;
7291
7292 this->fallthrough(IncLabel);
7293 this->emitLabel(IncLabel);
7294 if (!this->discard(Inc))
7295 return false;
7296 }
7297
7298 if (!CondScope.destroyLocals())
7299 return false;
7300 if (!this->jump(CondLabel, S))
7301 return false;
7302
7303 this->fallthrough(EndLabel);
7304 this->emitLabel(EndLabel);
7305 return WholeLoopScope.destroyLocals();
7306}
7307
7308template <class Emitter>
7310 if (LabelInfoStack.empty())
7311 return this->emitInvalidTargetLabel(/*IsContinue=*/false, S);
7312
7313 OptLabelTy TargetLabel = std::nullopt;
7314 const VariableScope<Emitter> *BreakScope = nullptr;
7315
7316 if (!S->isNamed()) {
7317
7318 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
7319 if (LI.BreakLabel) {
7320 TargetLabel = *LI.BreakLabel;
7321 BreakScope = LI.BreakOrContinueScope;
7322 break;
7323 }
7324 }
7325 } else {
7326 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
7327 // May be null if the named loop is outside of the
7328 // current evaluation context, i.e. not a valid target.
7329 if (!TargetLoop)
7330 return this->emitInvalidTargetLabel(/*IsContinue=*/false, S);
7331
7332 for (const auto &LI : LabelInfoStack) {
7333 if (LI.Name == TargetLoop) {
7334 TargetLabel = *LI.BreakLabel;
7335 BreakScope = LI.BreakOrContinueScope;
7336 break;
7337 }
7338 }
7339 }
7340
7341 // Faulty break statement (e.g. label redefined or named loops disabled).
7342 if (!TargetLabel)
7343 return false;
7344
7345 for (VariableScope<Emitter> *C = this->VarScope; C != BreakScope;
7346 C = C->getParent()) {
7347 if (!C->destroyLocals())
7348 return false;
7349 }
7350
7351 return this->jump(*TargetLabel, S);
7352}
7353
7354template <class Emitter>
7356 if (LabelInfoStack.empty())
7357 return this->emitInvalidTargetLabel(/*IsContinue=*/true, S);
7358
7359 OptLabelTy TargetLabel = std::nullopt;
7360 const VariableScope<Emitter> *ContinueScope = nullptr;
7361
7362 if (!S->isNamed()) {
7363 for (const auto &LI : llvm::reverse(LabelInfoStack)) {
7364 if (LI.ContinueLabel) {
7365 TargetLabel = *LI.ContinueLabel;
7366 ContinueScope = LI.BreakOrContinueScope;
7367 break;
7368 }
7369 }
7370 } else {
7371 const Stmt *TargetLoop = S->getNamedLoopOrSwitch();
7372 // May be null if the named loop is outside of the
7373 // current evaluation context, i.e. not a valid target.
7374 if (!TargetLoop)
7375 return this->emitInvalidTargetLabel(/*IsContinue=*/true, S);
7376
7377 for (auto LI : LabelInfoStack) {
7378 if (LI.Name == TargetLoop) {
7379 TargetLabel = *LI.ContinueLabel;
7380 ContinueScope = LI.BreakOrContinueScope;
7381 break;
7382 }
7383 }
7384 }
7385
7386 if (!TargetLabel)
7387 return false;
7388
7389 for (VariableScope<Emitter> *C = VarScope; C != ContinueScope;
7390 C = C->getParent()) {
7391 if (!C->destroyLocals())
7392 return false;
7393 }
7394
7395 return this->jump(*TargetLabel, S);
7396}
7397
7398template <class Emitter>
7400 const Expr *Cond = S->getCond();
7401 if (Cond->containsErrors())
7402 return false;
7403
7404 PrimType CondT = this->classifyPrim(Cond->getType());
7405 LocalScope<Emitter> LS(this);
7406 llvm::SaveAndRestore StmtExprSAR(this->SwitchInStmtExpr, this->InStmtExpr);
7407
7408 LabelTy EndLabel = this->getLabel();
7409 UnsignedOrNone DefaultLabel = std::nullopt;
7410 unsigned CondVar =
7411 this->allocateLocalPrimitive(Cond, CondT, /*IsConst=*/true);
7412
7413 if (const auto *CondInit = S->getInit())
7414 if (!visitStmt(CondInit))
7415 return false;
7416
7417 if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
7418 if (!visitDeclStmt(CondDecl))
7419 return false;
7420
7421 // Initialize condition variable.
7422 if (!this->visit(Cond))
7423 return false;
7424 if (!this->emitSetLocal(CondT, CondVar, S))
7425 return false;
7426
7427 if (!this->maybeEmitDeferredVarInit(S->getConditionVariable()))
7428 return false;
7429
7431 // Create labels and comparison ops for all case statements.
7432 for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
7433 SC = SC->getNextSwitchCase()) {
7434 if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
7435 CaseLabels[SC] = this->getLabel();
7436
7437 if (CS->caseStmtIsGNURange()) {
7438 LabelTy EndOfRangeCheck = this->getLabel();
7439 const Expr *Low = CS->getLHS();
7440 const Expr *High = CS->getRHS();
7441 if (Low->isValueDependent() || High->isValueDependent())
7442 return false;
7443
7444 if (!this->emitGetLocal(CondT, CondVar, CS))
7445 return false;
7446 if (!this->visit(Low))
7447 return false;
7448 PrimType LT = this->classifyPrim(Low->getType());
7449 if (!this->emitGE(LT, S))
7450 return false;
7451 if (!this->jumpFalse(EndOfRangeCheck, S))
7452 return false;
7453
7454 if (!this->emitGetLocal(CondT, CondVar, CS))
7455 return false;
7456 if (!this->visit(High))
7457 return false;
7458 PrimType HT = this->classifyPrim(High->getType());
7459 if (!this->emitLE(HT, S))
7460 return false;
7461 if (!this->jumpTrue(CaseLabels[CS], S))
7462 return false;
7463 this->emitLabel(EndOfRangeCheck);
7464 continue;
7465 }
7466
7467 const Expr *Value = CS->getLHS();
7468 if (Value->isValueDependent())
7469 return false;
7470 PrimType ValueT = this->classifyPrim(Value->getType());
7471
7472 // Compare the case statement's value to the switch condition.
7473 if (!this->emitGetLocal(CondT, CondVar, CS))
7474 return false;
7475 if (!this->visit(Value))
7476 return false;
7477
7478 // Compare and jump to the case label.
7479 if (!this->emitEQ(ValueT, S))
7480 return false;
7481 if (!this->jumpTrue(CaseLabels[CS], S))
7482 return false;
7483 } else {
7484 assert(!DefaultLabel);
7485 DefaultLabel = this->getLabel();
7486 }
7487 }
7488
7489 // If none of the conditions above were true, fall through to the default
7490 // statement or jump after the switch statement.
7491 if (DefaultLabel) {
7492 if (!this->jump(*DefaultLabel, S))
7493 return false;
7494 } else {
7495 if (!this->jump(EndLabel, S))
7496 return false;
7497 }
7498
7499 SwitchScope<Emitter> SS(this, S, std::move(CaseLabels), EndLabel,
7500 DefaultLabel);
7501 if (!this->visitStmt(S->getBody()))
7502 return false;
7503 this->fallthrough(EndLabel);
7504 this->emitLabel(EndLabel);
7505
7506 return LS.destroyLocals();
7507}
7508
7509template <class Emitter>
7511 this->fallthrough(CaseLabels[S]);
7512 this->emitLabel(CaseLabels[S]);
7513
7514 // We can't jump from an outer switch statement to a case label
7515 // that's inside a StmtExpr.
7516 if (this->InStmtExpr && !this->SwitchInStmtExpr)
7517 return this->emitUnsupported(S);
7518
7519 return this->visitStmt(S->getSubStmt());
7520}
7521
7522template <class Emitter>
7524 if (LabelInfoStack.empty())
7525 return false;
7526
7527 LabelTy DefaultLabel;
7528 for (const LabelInfo &LI : llvm::reverse(LabelInfoStack)) {
7529 if (LI.DefaultLabel) {
7530 DefaultLabel = *LI.DefaultLabel;
7531 break;
7532 }
7533 }
7534
7535 this->emitLabel(DefaultLabel);
7536 return this->visitStmt(S->getSubStmt());
7537}
7538
7539template <class Emitter>
7541 const Stmt *SubStmt = S->getSubStmt();
7542
7543 bool IsMSVCConstexprAttr = isa<ReturnStmt>(SubStmt) &&
7545
7546 if (IsMSVCConstexprAttr && !this->emitPushMSVCCE(S))
7547 return false;
7548
7549 if (this->Ctx.getLangOpts().CXXAssumptions &&
7550 !this->Ctx.getLangOpts().MSVCCompat) {
7551 for (const Attr *A : S->getAttrs()) {
7552 auto *AA = dyn_cast<CXXAssumeAttr>(A);
7553 if (!AA)
7554 continue;
7555
7556 assert(isa<NullStmt>(SubStmt));
7557
7558 const Expr *Assumption = AA->getAssumption();
7559 if (Assumption->isValueDependent())
7560 return false;
7561
7562 if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
7563 continue;
7564
7565 // Evaluate assumption.
7566 if (!this->visitBool(Assumption))
7567 return false;
7568
7569 if (!this->emitAssume(Assumption))
7570 return false;
7571 }
7572 }
7573
7574 // Ignore other attributes.
7575 if (!this->visitStmt(SubStmt))
7576 return false;
7577
7578 if (IsMSVCConstexprAttr)
7579 return this->emitPopMSVCCE(S);
7580 return true;
7581}
7582
7583template <class Emitter>
7585 // Ignore all handlers.
7586 return this->visitStmt(S->getTryBlock());
7587}
7588
7589/// template for (auto x : {1, 2}) {}
7590///
7591/// This is not a loop from an AST perspective at all since it has already
7592/// been instantiated to a list of compound statements.
7593///
7594/// Since we can have control flow in those compound statements, we need to
7595/// handle it mostly like a loop though.
7596template <class Emitter>
7599 LocalScope<Emitter> WholeLoopScope(this, ScopeKind::Block);
7600
7601 for (const Stmt *PreambleStmt : S->getPreambleStmts()) {
7602 if (!this->visitDeclStmt(cast<DeclStmt>(PreambleStmt), true))
7603 return false;
7604 }
7605
7606 LabelTy EndLabel = this->getLabel();
7607 for (const Stmt *Instantiation : S->getInstantiations()) {
7608 LabelTy ContinueLabel = this->getLabel();
7609 LoopScope<Emitter> LS(this, S, EndLabel, ContinueLabel);
7610
7611 if (!this->visitStmt(Instantiation))
7612 return false;
7613 this->emitLabel(ContinueLabel);
7614 }
7615
7616 this->emitLabel(EndLabel);
7617
7618 return WholeLoopScope.destroyLocals();
7619}
7620
7621template <class Emitter>
7622bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
7623 assert(MD->isLambdaStaticInvoker());
7624 assert(MD->hasBody());
7625 assert(cast<CompoundStmt>(MD->getBody())->body_empty());
7626
7627 const CXXRecordDecl *ClosureClass = MD->getParent();
7628 const FunctionDecl *LambdaCallOp;
7629 assert(ClosureClass->captures().empty());
7630 if (ClosureClass->isGenericLambda()) {
7631 LambdaCallOp = ClosureClass->getLambdaCallOperator();
7632 assert(MD->isFunctionTemplateSpecialization() &&
7633 "A generic lambda's static-invoker function must be a "
7634 "template specialization");
7636 FunctionTemplateDecl *CallOpTemplate =
7637 LambdaCallOp->getDescribedFunctionTemplate();
7638 llvm::FoldingSetInsertToken InsertToken;
7639 const FunctionDecl *CorrespondingCallOpSpecialization =
7640 CallOpTemplate->findSpecialization(TAL->asArray(), InsertToken);
7641 assert(CorrespondingCallOpSpecialization);
7642 LambdaCallOp = CorrespondingCallOpSpecialization;
7643 } else {
7644 LambdaCallOp = ClosureClass->getLambdaCallOperator();
7645 }
7646 assert(ClosureClass->captures().empty());
7647 const Function *Func = this->getFunction(LambdaCallOp);
7648 if (!Func)
7649 return false;
7650 assert(Func->hasThisPointer());
7651 assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
7652
7653 if (Func->hasRVO()) {
7654 if (!this->emitRVOPtr(MD))
7655 return false;
7656 }
7657
7658 // The lambda call operator needs an instance pointer, but we don't have
7659 // one here, and we don't need one either because the lambda cannot have
7660 // any captures, as verified above. Emit a null pointer. This is then
7661 // special-cased when interpreting to not emit any misleading diagnostics.
7662 if (!this->emitNullPtr(0, nullptr, MD))
7663 return false;
7664
7665 // Forward all arguments from the static invoker to the lambda call operator.
7666 for (const ParmVarDecl *PVD : MD->parameters()) {
7667 auto It = this->Params.find(PVD);
7668 assert(It != this->Params.end());
7669
7670 // We do the lvalue-to-rvalue conversion manually here, so no need
7671 // to care about references.
7672 PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
7673 if (!this->emitGetParam(ParamType, It->second.Index, MD))
7674 return false;
7675 }
7676
7677 if (!this->emitCall(Func, 0, LambdaCallOp))
7678 return false;
7679
7680 this->emitCleanup();
7681 if (ReturnType)
7682 return this->emitRet(*ReturnType, MD);
7683
7684 // Nothing to do, since we emitted the RVO pointer above.
7685 return this->emitRetVoid(MD);
7686}
7687
7688template <class Emitter>
7689bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
7690 if (Ctx.getLangOpts().CPlusPlus23)
7691 return true;
7692
7693 if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
7694 return true;
7695
7696 return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
7697}
7698
7700 const Expr *InitExpr = Init->getInit();
7701
7702 if (!Init->isWritten() && !Init->isInClassMemberInitializer() &&
7703 !isa<CXXConstructExpr>(InitExpr))
7704 return true;
7705
7706 if (const auto *CE = dyn_cast<CXXConstructExpr>(InitExpr)) {
7707 const CXXConstructorDecl *Ctor = CE->getConstructor();
7708 if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
7709 Ctor->isTrivial())
7710 return true;
7711 }
7712
7713 return false;
7714}
7715
7716template <class Emitter>
7717bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
7718 assert(!ReturnType);
7719
7720 // Only start the lifetime of the instance pointer.
7721 if (!this->emitStartThisLifetime1(Ctor))
7722 return false;
7723
7724 auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
7725 const Expr *InitExpr,
7726 bool Activate = false) -> bool {
7727 // We don't know what to do with these, so just return false.
7728 if (InitExpr->getType().isNull())
7729 return false;
7730
7731 if (OptPrimType T = this->classify(InitExpr)) {
7732 if (Activate && !this->emitActivateThisField(FieldOffset, InitExpr))
7733 return false;
7734
7735 if (!this->visit(InitExpr))
7736 return false;
7737
7738 if (F->isBitField())
7739 return this->emitInitThisBitField(*T, FieldOffset, F->bitWidth(),
7740 InitExpr);
7741 return this->emitInitThisField(*T, FieldOffset, InitExpr);
7742 }
7743 // Non-primitive case. Get a pointer to the field-to-initialize
7744 // on the stack and call visitInitialzer() for it.
7745 InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
7746 if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
7747 return false;
7748
7749 if (Activate && !this->emitActivate(InitExpr))
7750 return false;
7751
7752 return this->visitInitializerPop(InitExpr);
7753 };
7754
7755 const RecordDecl *RD = Ctor->getParent();
7756 const Record *R = this->getRecord(RD);
7757 if (!R)
7758 return false;
7759 bool IsUnion = R->isUnion();
7760
7761 // Default union copy and move ctors are special.
7762 if (IsUnion && Ctor->isCopyOrMoveConstructor() && Ctor->isDefaulted()) {
7764
7765 // No special case for NumFields == 0 here, so the Memcpy op
7766 // below also does its checks in those cases.
7767
7768 assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
7769 if (!this->emitThis(Ctor))
7770 return false;
7771
7772 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, Ctor))
7773 return false;
7774
7775 return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
7776 this->emitRetVoid(Ctor);
7777 }
7778
7779 unsigned FieldInits = 0;
7780 InitLinkScope<Emitter> InitScope(this, InitLink::This());
7781 // First, initialize virtual bases if the records has them.
7782 if (R->getNumVirtualBases() > 0) {
7783 if (!this->emitThis(Ctor))
7784 return false;
7785 LabelTy AfterVirtBasesLabel = this->getLabel();
7786
7787 // If the instance pointer is a base class, skip the virtual bases.
7788 if (!this->emitIsBaseClass({}))
7789 return false;
7790 if (!this->jumpTrue(AfterVirtBasesLabel, {}))
7791 return false;
7792
7793 for (const auto *Init : Ctor->inits()) {
7794 LocalScope<Emitter> VirtLocalScope(this);
7795 if (const Type *Base = Init->getBaseClass();
7796 Base && Init->isBaseVirtual()) {
7797 const auto *BaseDecl = Base->castAsCXXRecordDecl();
7798 assert(R->findVirtualBase(BaseDecl));
7799 if (!this->emitGetPtrThisVirtBase(BaseDecl, Ctor))
7800 return false;
7801 if (!this->visitInitializerPop(Init->getInit()))
7802 return false;
7803 }
7804 }
7805
7806 this->fallthrough(AfterVirtBasesLabel);
7807 this->emitLabel(AfterVirtBasesLabel);
7808
7809 if (!this->emitPopPtr(Ctor))
7810 return false;
7811 }
7812
7813 for (const auto *Init : Ctor->inits()) {
7814 // Scope needed for the initializers.
7815 LocalScope<Emitter> Scope(this, ScopeKind::FullExpression);
7816
7817 const Expr *InitExpr = Init->getInit();
7818 if (const FieldDecl *Member = Init->getMember()) {
7819 const Record::Field *F = R->getField(Member);
7820
7823 if (!emitFieldInitializer(F, F->Offset, InitExpr, IsUnion))
7824 return false;
7825 ++FieldInits;
7826 } else if (const Type *Base = Init->getBaseClass()) {
7827 const auto *BaseDecl = Base->castAsCXXRecordDecl();
7828
7829 if (Init->isBaseVirtual()) {
7830 // See above.
7831 continue;
7832 } else {
7833 // Base class initializer.
7834 // Get This Base and call initializer on it.
7835 const Record::Base *B = R->getBase(BaseDecl);
7836 assert(B);
7837 if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
7838 return false;
7839 }
7840
7841 if (!this->visitInitializerPop(InitExpr))
7842 return false;
7843 } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
7846 unsigned ChainSize = IFD->getChainingSize();
7847 assert(ChainSize >= 2);
7848
7849 unsigned NestedFieldOffset = 0;
7850 const Record::Field *NestedField = nullptr;
7851 for (unsigned I = 0; I != ChainSize; ++I) {
7852 const auto *FD = cast<FieldDecl>(IFD->chain()[I]);
7853 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
7854 assert(FieldRecord);
7855
7856 NestedField = FieldRecord->getField(FD);
7857 assert(NestedField);
7858 IsUnion = IsUnion || FieldRecord->isUnion();
7859
7860 NestedFieldOffset += NestedField->Offset;
7861
7862 // Add a new InitChainLink for the record, but not for the final field.
7863 if (I != ChainSize - 1)
7864 InitStack.push_back(InitLink::Field(NestedField->Offset));
7865 }
7866 assert(NestedField);
7867
7869 if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr,
7870 IsUnion))
7871 return false;
7872
7873 // Mark all chain links as initialized.
7874 unsigned InitFieldOffset = 0;
7875 for (const NamedDecl *ND : IFD->chain().drop_back()) {
7876 const auto *FD = cast<FieldDecl>(ND);
7877 const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
7878 assert(FieldRecord);
7879 NestedField = FieldRecord->getField(FD);
7880 InitFieldOffset += NestedField->Offset;
7881 assert(NestedField);
7882 if (!this->emitGetPtrThisField(InitFieldOffset, InitExpr))
7883 return false;
7884 if (!this->emitFinishInitPop(InitExpr))
7885 return false;
7886 }
7887
7888 InitStack.pop_back_n(ChainSize - 1);
7889
7890 } else {
7891 assert(Init->isDelegatingInitializer());
7892 if (!this->emitThis(InitExpr))
7893 return false;
7894 if (!this->visitInitializerPop(Init->getInit()))
7895 return false;
7896 }
7897
7898 if (!Scope.destroyLocals())
7899 return false;
7900 }
7901
7902 if (FieldInits != R->getNumFields()) {
7903 assert(FieldInits < R->getNumFields());
7904 // Start the lifetime of all members.
7905 if (!this->emitStartThisLifetime(Ctor))
7906 return false;
7907 }
7908
7909 if (const Stmt *Body = Ctor->getBody()) {
7910 // Only emit the CtorCheck op for non-empty CompoundStmt bodies.
7911 // For non-CompoundStmts, always assume they are non-empty and emit it.
7912 if (const auto *CS = dyn_cast<CompoundStmt>(Body)) {
7913 if (!CS->body_empty() && !this->emitCtorCheck(SourceInfo{}))
7914 return false;
7915 } else {
7916 if (!this->emitCtorCheck(SourceInfo{}))
7917 return false;
7918 }
7919
7920 if (!visitStmt(Body))
7921 return false;
7922 }
7923
7924 return this->emitRetVoid(SourceInfo{});
7925}
7926
7927template <class Emitter>
7928bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
7929 const RecordDecl *RD = Dtor->getParent();
7930 const Record *R = this->getRecord(RD);
7931 if (!R)
7932 return false;
7933
7934 if (!Dtor->isTrivial() && Dtor->getBody()) {
7935 if (!this->visitStmt(Dtor->getBody()))
7936 return false;
7937 }
7938
7939 if (!this->emitThis(Dtor))
7940 return false;
7941
7942 if (!this->emitCheckDestruction(Dtor))
7943 return false;
7944
7945 assert(R);
7946 if (!R->isUnion()) {
7947
7949 // First, destroy all fields.
7950 for (const Record::Field &Field : llvm::reverse(R->fields())) {
7951 const Descriptor *D = Field.Desc;
7952 if (D->hasTrivialDtor())
7953 continue;
7954 if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
7955 return false;
7956 if (!this->emitDestructionPop(D, SourceInfo{}))
7957 return false;
7958 }
7959 }
7960
7961 for (const Record::Base &Base : llvm::reverse(R->bases())) {
7962 if (Base.R->hasTrivialDtor())
7963 continue;
7964 if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
7965 return false;
7966 if (!this->emitRecordDestructionPop(Base.R, {}))
7967 return false;
7968 }
7969
7970 if (R->getNumVirtualBases() > 0) {
7971 LabelTy EndLabel = this->getLabel();
7972 // If this is a base class, skip the virtual bases.
7973 if (!this->emitIsBaseClass({}))
7974 return false;
7975 if (!this->jumpTrue(EndLabel, {}))
7976 return false;
7977
7978 for (const Record::Base &Base : llvm::reverse(R->virtual_bases())) {
7979 if (Base.R->hasTrivialDtor())
7980 continue;
7981 if (!this->emitGetPtrVirtBase(cast<CXXRecordDecl>(Base.R->getDecl()),
7982 SourceInfo{}))
7983 return false;
7984 if (!this->emitRecordDestructionPop(Base.R, {}))
7985 return false;
7986 }
7987
7988 this->fallthrough(EndLabel);
7989 this->emitLabel(EndLabel);
7990 }
7991
7992 if (!this->emitMarkDestroyed(Dtor))
7993 return false;
7994
7995 return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
7996}
7997
7998template <class Emitter>
7999bool Compiler<Emitter>::compileUnionAssignmentOperator(
8000 const CXXMethodDecl *MD) {
8001 if (!this->emitThis(MD))
8002 return false;
8003
8004 if (!this->emitGetParam(PT_Ptr, /*ParamIndex=*/0, MD))
8005 return false;
8006
8007 return this->emitMemcpy(MD) && this->emitRet(PT_Ptr, MD);
8008}
8009
8010template <class Emitter>
8012 if (F->getReturnType()->isDependentType())
8013 return false;
8014
8015 // Classify the return type.
8016 ReturnType = this->classify(F->getReturnType());
8017
8018 this->CompilingFunction = F;
8019
8020 if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
8021 return this->compileConstructor(Ctor);
8022 if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
8023 return this->compileDestructor(Dtor);
8024
8025 // Emit custom code if this is a lambda static invoker.
8026 if (const auto *MD = dyn_cast<CXXMethodDecl>(F)) {
8027 const RecordDecl *RD = MD->getParent();
8028
8029 if (RD->isUnion() &&
8031 return this->compileUnionAssignmentOperator(MD);
8032
8033 if (MD->isLambdaStaticInvoker())
8034 return this->emitLambdaStaticInvokerBody(MD);
8035 }
8036
8037 // Regular functions.
8038 if (const auto *Body = F->getBody())
8039 if (!visitStmt(Body))
8040 return false;
8041
8042 // Emit a guard return to protect against a code path missing one.
8043 if (F->getReturnType()->isVoidType())
8044 return this->emitRetVoid(SourceInfo{});
8045 return this->emitNoRet(SourceInfo{});
8046}
8047
8048static uint32_t getBitWidth(const Expr *E) {
8049 assert(E->refersToBitField());
8050 const auto *ME = cast<MemberExpr>(E);
8051 const auto *FD = cast<FieldDecl>(ME->getMemberDecl());
8052 return FD->getBitWidthValue();
8053}
8054
8055template <class Emitter>
8057 if (E->containsErrors())
8058 return false;
8059
8060 const Expr *SubExpr = E->getSubExpr();
8061 if (SubExpr->getType()->isAnyComplexType())
8062 return this->VisitComplexUnaryOperator(E);
8063 if (SubExpr->getType()->isVectorType())
8064 return this->VisitVectorUnaryOperator(E);
8065 if (SubExpr->getType()->isFixedPointType())
8066 return this->VisitFixedPointUnaryOperator(E);
8067 OptPrimType T = classify(SubExpr->getType());
8068
8069 switch (E->getOpcode()) {
8070 case UO_PostInc: { // x++
8071 if (!Ctx.getLangOpts().CPlusPlus14)
8072 return this->emitInvalid(E);
8073 if (!T)
8074 return this->emitError(E);
8075
8076 if (!this->visit(SubExpr))
8077 return false;
8078
8079 if (T == PT_Ptr) {
8080 if (!this->emitIncPtr(E))
8081 return false;
8082
8083 return DiscardResult ? this->emitPopPtr(E) : true;
8084 }
8085
8086 if (T == PT_Float)
8087 return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
8088 : this->emitIncf(getFPOptions(E), E);
8089
8090 if (SubExpr->refersToBitField())
8091 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
8092 getBitWidth(SubExpr), E)
8093 : this->emitIncBitfield(*T, E->canOverflow(),
8094 getBitWidth(SubExpr), E);
8095
8096 return DiscardResult ? this->emitIncPop(*T, E->canOverflow(), E)
8097 : this->emitInc(*T, E->canOverflow(), E);
8098 }
8099 case UO_PostDec: { // x--
8100 if (!Ctx.getLangOpts().CPlusPlus14)
8101 return this->emitInvalid(E);
8102 if (!T)
8103 return this->emitError(E);
8104
8105 if (!this->visit(SubExpr))
8106 return false;
8107
8108 if (T == PT_Ptr) {
8109 if (!this->emitDecPtr(E))
8110 return false;
8111
8112 return DiscardResult ? this->emitPopPtr(E) : true;
8113 }
8114
8115 if (T == PT_Float)
8116 return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
8117 : this->emitDecf(getFPOptions(E), E);
8118
8119 if (SubExpr->refersToBitField()) {
8120 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
8121 getBitWidth(SubExpr), E)
8122 : this->emitDecBitfield(*T, E->canOverflow(),
8123 getBitWidth(SubExpr), E);
8124 }
8125
8126 return DiscardResult ? this->emitDecPop(*T, E->canOverflow(), E)
8127 : this->emitDec(*T, E->canOverflow(), E);
8128 }
8129 case UO_PreInc: { // ++x
8130 if (!Ctx.getLangOpts().CPlusPlus14)
8131 return this->emitInvalid(E);
8132 if (!T)
8133 return this->emitError(E);
8134
8135 if (!this->visit(SubExpr))
8136 return false;
8137
8138 if (T == PT_Ptr) {
8139 if (!this->emitLoadPtr(E))
8140 return false;
8141 if (!this->emitConstUint8(1, E))
8142 return false;
8143 if (!this->emitAddOffsetUint8(E))
8144 return false;
8145 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
8146 }
8147
8148 // Post-inc and pre-inc are the same if the value is to be discarded.
8149 if (DiscardResult) {
8150 if (T == PT_Float)
8151 return this->emitIncfPop(getFPOptions(E), E);
8152 if (SubExpr->refersToBitField())
8153 return DiscardResult ? this->emitIncPopBitfield(*T, E->canOverflow(),
8154 getBitWidth(SubExpr), E)
8155 : this->emitIncBitfield(*T, E->canOverflow(),
8156 getBitWidth(SubExpr), E);
8157 return this->emitIncPop(*T, E->canOverflow(), E);
8158 }
8159
8160 if (T == PT_Float) {
8161 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
8162 if (!this->emitLoadFloat(E))
8163 return false;
8164 APFloat F(TargetSemantics, 1);
8165 if (!this->emitFloat(F, E))
8166 return false;
8167
8168 if (!this->emitAddf(getFPOptions(E), E))
8169 return false;
8170 if (!this->emitStoreFloat(E))
8171 return false;
8172 } else if (SubExpr->refersToBitField()) {
8173 assert(isIntegerOrBoolType(*T));
8174 if (!this->emitPreIncBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
8175 E))
8176 return false;
8177 } else {
8178 assert(isIntegerOrBoolType(*T));
8179 if (!this->emitPreInc(*T, E->canOverflow(), E))
8180 return false;
8181 }
8182 return E->isGLValue() || this->emitLoadPop(*T, E);
8183 }
8184 case UO_PreDec: { // --x
8185 if (!Ctx.getLangOpts().CPlusPlus14)
8186 return this->emitInvalid(E);
8187 if (!T)
8188 return this->emitError(E);
8189
8190 if (!this->visit(SubExpr))
8191 return false;
8192
8193 if (T == PT_Ptr) {
8194 if (!this->emitLoadPtr(E))
8195 return false;
8196 if (!this->emitConstUint8(1, E))
8197 return false;
8198 if (!this->emitSubOffsetUint8(E))
8199 return false;
8200 return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
8201 }
8202
8203 // Post-dec and pre-dec are the same if the value is to be discarded.
8204 if (DiscardResult) {
8205 if (T == PT_Float)
8206 return this->emitDecfPop(getFPOptions(E), E);
8207 if (SubExpr->refersToBitField())
8208 return DiscardResult ? this->emitDecPopBitfield(*T, E->canOverflow(),
8209 getBitWidth(SubExpr), E)
8210 : this->emitDecBitfield(*T, E->canOverflow(),
8211 getBitWidth(SubExpr), E);
8212 return this->emitDecPop(*T, E->canOverflow(), E);
8213 }
8214
8215 if (T == PT_Float) {
8216 const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
8217 if (!this->emitLoadFloat(E))
8218 return false;
8219 APFloat F(TargetSemantics, 1);
8220 if (!this->emitFloat(F, E))
8221 return false;
8222
8223 if (!this->emitSubf(getFPOptions(E), E))
8224 return false;
8225 if (!this->emitStoreFloat(E))
8226 return false;
8227 } else if (SubExpr->refersToBitField()) {
8228 assert(isIntegerOrBoolType(*T));
8229 if (!this->emitPreDecBitfield(*T, E->canOverflow(), getBitWidth(SubExpr),
8230 E))
8231 return false;
8232 } else {
8233 assert(isIntegerOrBoolType(*T));
8234 if (!this->emitPreDec(*T, E->canOverflow(), E))
8235 return false;
8236 }
8237 return E->isGLValue() || this->emitLoadPop(*T, E);
8238 }
8239 case UO_LNot: // !x
8240 if (!T)
8241 return this->emitError(E);
8242
8243 if (DiscardResult)
8244 return this->discard(SubExpr);
8245
8246 if (!this->visitBool(SubExpr))
8247 return false;
8248
8249 if (!this->emitInv(E))
8250 return false;
8251
8252 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
8253 return this->emitCast(PT_Bool, ET, E);
8254 return true;
8255 case UO_Minus: // -x
8256 if (!T)
8257 return this->emitError(E);
8258
8259 if (!this->visit(SubExpr))
8260 return false;
8261 return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
8262 case UO_Plus: // +x
8263 if (!T)
8264 return this->emitError(E);
8265
8266 if (!this->visit(SubExpr)) // noop
8267 return false;
8268 return DiscardResult ? this->emitPop(*T, E) : true;
8269 case UO_AddrOf: // &x
8270 if (E->getType()->isMemberPointerType()) {
8271 // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
8272 // member can be formed.
8273 if (DiscardResult)
8274 return true;
8275 return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
8276 }
8277 // [C11 6.5.3.2p3]: if the operand of '&' is the result of a unary '*'
8278 // operator, neither operator is evaluated and the result is as if both
8279 // were omitted. So '&*q' is just 'q' with no dereference; delegate to the
8280 // pointer operand directly instead of to the '*' (which would emit a null
8281 // check), so that e.g. '&*(int *)0' is not rejected.
8282 if (!Ctx.getLangOpts().CPlusPlus) {
8283 const Expr *Sub = SubExpr->IgnoreParens();
8284
8285 if (const auto *Deref = dyn_cast<UnaryOperator>(Sub);
8286 Deref && Deref->getOpcode() == UO_Deref) {
8287 if (DiscardResult)
8288 return this->discard(Deref->getSubExpr());
8289 return this->visit(Deref->getSubExpr()) && this->emitAddrOf(E);
8290 }
8291 }
8292 // We should already have a pointer when we get here.
8293 if (DiscardResult)
8294 return this->discard(SubExpr);
8295 return this->delegate(SubExpr) && this->emitAddrOf(E);
8296 case UO_Deref: // *x
8297 if (DiscardResult)
8298 return this->discard(SubExpr);
8299
8300 if (!this->visit(SubExpr))
8301 return false;
8302
8303 if (!SubExpr->getType()->isFunctionPointerType() && !this->emitCheckNull(E))
8304 return false;
8305
8306 if (classifyPrim(SubExpr) == PT_Ptr)
8307 return this->emitNarrowPtr(E);
8308 return true;
8309
8310 case UO_Not: // ~x
8311 if (!T)
8312 return this->emitError(E);
8313
8314 if (!this->visit(SubExpr))
8315 return false;
8316 return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
8317 case UO_Real: // __real x
8318 if (!T)
8319 return false;
8320 return this->delegate(SubExpr);
8321 case UO_Imag: { // __imag x
8322 if (!T)
8323 return false;
8324 if (!this->discard(SubExpr))
8325 return false;
8326 return DiscardResult
8327 ? true
8328 : this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
8329 }
8330 case UO_Extension:
8331 return this->delegate(SubExpr);
8332 case UO_Coawait:
8333 assert(false && "Unhandled opcode");
8334 }
8335
8336 return false;
8337}
8338
8339template <class Emitter>
8341 const Expr *SubExpr = E->getSubExpr();
8342 assert(SubExpr->getType()->isAnyComplexType());
8343
8344 if (DiscardResult)
8345 return this->discard(SubExpr);
8346
8347 OptPrimType ResT = classify(E);
8348 auto prepareResult = [=]() -> bool {
8349 if (!ResT && !Initializing) {
8350 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
8351 if (!LocalIndex)
8352 return false;
8353 return this->emitGetPtrLocal(*LocalIndex, E);
8354 }
8355
8356 return true;
8357 };
8358
8359 // The offset of the temporary, if we created one.
8360 unsigned SubExprOffset = ~0u;
8361 auto createTemp = [=, &SubExprOffset]() -> bool {
8362 SubExprOffset =
8363 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
8364 if (!this->visit(SubExpr))
8365 return false;
8366 return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
8367 };
8368
8369 PrimType ElemT = classifyComplexElementType(SubExpr->getType());
8370 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
8371 if (!this->emitGetLocal(PT_Ptr, Offset, E))
8372 return false;
8373 return this->emitArrayElemPop(ElemT, Index, E);
8374 };
8375
8376 switch (E->getOpcode()) {
8377 case UO_Minus: // -x
8378 if (!prepareResult())
8379 return false;
8380 if (!createTemp())
8381 return false;
8382 for (unsigned I = 0; I != 2; ++I) {
8383 if (!getElem(SubExprOffset, I))
8384 return false;
8385 if (!this->emitNeg(ElemT, E))
8386 return false;
8387 if (!this->emitInitElem(ElemT, I, E))
8388 return false;
8389 }
8390 break;
8391
8392 case UO_Plus: // +x
8393 case UO_AddrOf: // &x
8394 case UO_Deref: // *x
8395 return this->delegate(SubExpr);
8396
8397 case UO_LNot:
8398 if (!this->visit(SubExpr))
8399 return false;
8400 if (!this->emitComplexBoolCast(SubExpr))
8401 return false;
8402 if (!this->emitInv(E))
8403 return false;
8404 if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
8405 return this->emitCast(PT_Bool, ET, E);
8406 return true;
8407
8408 case UO_Real:
8409 return this->emitComplexReal(SubExpr);
8410
8411 case UO_Imag:
8412 if (!this->visit(SubExpr))
8413 return false;
8414
8415 if (SubExpr->isLValue()) {
8416 if (!this->emitConstUint8(1, E))
8417 return false;
8418 return this->emitArrayElemPtrPopUint8(E);
8419 }
8420
8421 // Since our _Complex implementation does not map to a primitive type,
8422 // we sometimes have to do the lvalue-to-rvalue conversion here manually.
8423 return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
8424
8425 case UO_Not: // ~x
8426 if (!this->delegate(SubExpr))
8427 return false;
8428 // Negate the imaginary component.
8429 if (!this->emitArrayElem(ElemT, 1, E))
8430 return false;
8431 if (!this->emitNeg(ElemT, E))
8432 return false;
8433 if (!this->emitInitElem(ElemT, 1, E))
8434 return false;
8435 return DiscardResult ? this->emitPopPtr(E) : true;
8436
8437 case UO_Extension:
8438 return this->delegate(SubExpr);
8439
8440 default:
8441 return this->emitInvalid(E);
8442 }
8443
8444 return true;
8445}
8446
8447template <class Emitter>
8449 const Expr *SubExpr = E->getSubExpr();
8450 assert(SubExpr->getType()->isVectorType());
8451
8452 if (DiscardResult)
8453 return this->discard(SubExpr);
8454
8455 auto UnaryOp = E->getOpcode();
8456 if (UnaryOp == UO_Extension)
8457 return this->delegate(SubExpr);
8458
8459 if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
8460 UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
8461 return this->emitInvalid(E);
8462
8463 // Nothing to do here.
8464 if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
8465 return this->delegate(SubExpr);
8466
8467 if (!Initializing) {
8468 UnsignedOrNone LocalIndex = allocateLocal(SubExpr);
8469 if (!LocalIndex)
8470 return false;
8471 if (!this->emitGetPtrLocal(*LocalIndex, E))
8472 return false;
8473 }
8474
8475 // The offset of the temporary, if we created one.
8476 unsigned SubExprOffset =
8477 this->allocateLocalPrimitive(SubExpr, PT_Ptr, /*IsConst=*/true);
8478 if (!this->visit(SubExpr))
8479 return false;
8480 if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
8481 return false;
8482
8483 const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
8484 PrimType ElemT = classifyVectorElementType(SubExpr->getType());
8485 auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
8486 if (!this->emitGetLocal(PT_Ptr, Offset, E))
8487 return false;
8488 return this->emitArrayElemPop(ElemT, Index, E);
8489 };
8490
8491 switch (UnaryOp) {
8492 case UO_Minus:
8493 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
8494 if (!getElem(SubExprOffset, I))
8495 return false;
8496 if (!this->emitNeg(ElemT, E))
8497 return false;
8498 if (!this->emitInitElem(ElemT, I, E))
8499 return false;
8500 }
8501 break;
8502 case UO_LNot: { // !x
8503 // In C++, the logic operators !, &&, || are available for vectors. !v is
8504 // equivalent to v == 0.
8505 //
8506 // The result of the comparison is a vector of the same width and number of
8507 // elements as the comparison operands with a signed integral element type.
8508 //
8509 // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
8510 QualType ResultVecTy = E->getType();
8511 PrimType ResultVecElemT =
8512 classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
8513 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
8514 if (!getElem(SubExprOffset, I))
8515 return false;
8516 // operator ! on vectors returns -1 for 'truth', so negate it.
8517 if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
8518 return false;
8519 if (!this->emitInv(E))
8520 return false;
8521 if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
8522 return false;
8523 if (!this->emitNeg(ElemT, E))
8524 return false;
8525 if (ElemT != ResultVecElemT &&
8526 !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
8527 return false;
8528 if (!this->emitInitElem(ResultVecElemT, I, E))
8529 return false;
8530 }
8531 break;
8532 }
8533 case UO_Not: // ~x
8534 for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
8535 if (!getElem(SubExprOffset, I))
8536 return false;
8537 if (ElemT == PT_Bool) {
8538 if (!this->emitInv(E))
8539 return false;
8540 } else {
8541 if (!this->emitComp(ElemT, E))
8542 return false;
8543 }
8544 if (!this->emitInitElem(ElemT, I, E))
8545 return false;
8546 }
8547 break;
8548 default:
8549 llvm_unreachable("Unsupported unary operators should be handled up front");
8550 }
8551 return true;
8552}
8553
8554template <class Emitter>
8556 if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
8557 if (DiscardResult)
8558 return true;
8559 return this->emitConst(ECD->getInitVal(), E);
8560 }
8561 if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
8562 if (DiscardResult)
8563 return true;
8564 const Function *F = getFunction(FuncDecl);
8565 return F && this->emitGetFnPtr(F, E);
8566 }
8567 if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
8568 TPOD = TPOD->getFirstDecl();
8569 if (DiscardResult)
8570 return true;
8571 if (UnsignedOrNone GlobalIndex = P.getGlobal(TPOD))
8572 return this->emitGetPtrGlobal(*GlobalIndex, E);
8573
8574 if (UnsignedOrNone Index = P.getOrCreateGlobal(TPOD)) {
8575 if (OptPrimType T = classify(TPOD->getType())) {
8576 if (!this->visitAPValue(TPOD->getValue(), *T, E))
8577 return false;
8578 return this->emitInitGlobal(*T, *Index, E);
8579 }
8580
8581 if (!this->emitGetPtrGlobal(*Index, E))
8582 return false;
8583 if (!this->visitAPValueInitializer(TPOD->getValue(), E, TPOD->getType()))
8584 return false;
8585 return this->emitFinishInit(E);
8586 }
8587 return false;
8588 }
8589
8590 // References are implemented via pointers, so when we see a DeclRefExpr
8591 // pointing to a reference, we need to get its value directly (i.e. the
8592 // pointer to the actual value) instead of a pointer to the pointer to the
8593 // value.
8594 QualType DeclType = D->getType();
8595 bool IsReference = DeclType->isReferenceType();
8596
8597 auto maybePopPtr = [&]() -> bool {
8598 if (DiscardResult)
8599 return this->emitPopPtr(E);
8600 return true;
8601 };
8602
8603 // Function parameters.
8604 // Note that it's important to check them first since we might have a local
8605 // variable created for a ParmVarDecl as well.
8606 if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
8607 if (DiscardResult)
8608 return true;
8609
8610 if (Ctx.getLangOpts().CPlusPlus && !Ctx.getLangOpts().CPlusPlus11 &&
8611 !DeclType->isIntegralOrEnumerationType()) {
8612 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
8613 /*InitializerFailed=*/false, E);
8614 }
8615 if (auto It = this->Params.find(PVD); It != this->Params.end()) {
8616 if (IsReference || !It->second.IsPtr)
8617 return this->emitGetParam(classifyPrim(E), It->second.Index, E);
8618
8619 return this->emitGetPtrParam(It->second.Index, E);
8620 }
8621
8622 if (!Ctx.getLangOpts().CPlusPlus23 && IsReference && !Locals.contains(D))
8623 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
8624 /*InitializerFailed=*/false, E);
8625 }
8626
8627 // Local variables.
8628 if (auto It = Locals.find(D); It != Locals.end()) {
8629 const unsigned Offset = It->second.Offset;
8630 if (IsReference) {
8631 assert(classifyPrim(E) == PT_Ptr);
8632 return this->emitGetRefLocal(Offset, E) && maybePopPtr();
8633 }
8634 return this->emitGetPtrLocal(Offset, E) && maybePopPtr();
8635 }
8636 // Global variables.
8637 if (auto GlobalIndex = P.getGlobal(D)) {
8638 if (IsReference) {
8639 if (!Ctx.getLangOpts().CPlusPlus11)
8640 return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
8641 if (!Ctx.getLangOpts().CPlusPlus23)
8642 return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
8643
8644 return this->emitGetRefGlobal(*GlobalIndex, E) && maybePopPtr();
8645 }
8646
8647 return this->emitGetPtrGlobal(*GlobalIndex, E) && maybePopPtr();
8648 }
8649
8650 // In case we need to re-visit a declaration.
8651 auto revisit = [&](const VarDecl *VD,
8652 bool IsConstexprUnknown = true) -> bool {
8654 IsConstexprUnknown);
8655 if constexpr (std::is_same_v<Emitter, EvalEmitter>) {
8656 if (!this->emitPushCC(VD->hasConstantInitialization(), E))
8657 return false;
8658 }
8659 auto VarState = this->visitDecl(VD);
8660
8661 if constexpr (std::is_same_v<Emitter, EvalEmitter>) {
8662 if (!this->emitPopCC(E))
8663 return false;
8664 }
8665
8666 if (VarState.notCreated())
8667 return true;
8668 if (!VarState)
8669 return false;
8670 // Retry.
8671 return this->visitDeclRef(D, E);
8672 };
8673
8674 if constexpr (!std::is_same_v<Emitter, EvalEmitter>) {
8675 // Lambda captures.
8676 if (auto It = this->LambdaCaptures.find(D);
8677 It != this->LambdaCaptures.end()) {
8678 auto [Offset, IsPtr] = It->second;
8679
8680 if (IsPtr)
8681 return this->emitGetThisFieldPtr(Offset, E) && maybePopPtr();
8682 return this->emitGetPtrThisField(Offset, E) && maybePopPtr();
8683 }
8684 }
8685
8686 if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
8687 DRE && DRE->refersToEnclosingVariableOrCapture()) {
8688 if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture())
8689 return revisit(VD);
8690 }
8691
8692 if (const auto *BD = dyn_cast<BindingDecl>(D))
8693 return this->delegate(BD->getBinding());
8694
8695 // Avoid infinite recursion.
8696 if (D == InitializingDecl) {
8697 if (DiscardResult)
8698 return true;
8699 return this->emitDummyPtr(D, E);
8700 }
8701
8702 // Try to lazily visit (or emit dummy pointers for) declarations
8703 // we haven't seen yet.
8704 const auto *VD = dyn_cast<VarDecl>(D);
8705 if (!VD)
8706 return this->emitError(E);
8707
8708 // For C.
8709 if (!Ctx.getLangOpts().CPlusPlus) {
8710 if (VD->getInit() && !VD->getInit()->isValueDependent() &&
8711 DeclType.isConstant(Ctx.getASTContext()) && !VD->isWeak() &&
8712 VD->evaluateValue())
8713 return revisit(VD, /*IsConstexprUnknown=*/false);
8714
8715 if (DiscardResult)
8716 return true;
8717 return this->emitDummyPtr(D, E);
8718 }
8719
8720 // ... and C++.
8721 const auto typeShouldBeVisited = [&](QualType T) -> bool {
8722 if (T.isConstant(Ctx.getASTContext()))
8723 return true;
8724 return T->isReferenceType();
8725 };
8726
8727 if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
8728 typeShouldBeVisited(DeclType)) {
8729 if (const Expr *Init = VD->getAnyInitializer();
8730 Init && !Init->isValueDependent()) {
8731 // Whether or not the evaluation is successul doesn't really matter
8732 // here -- we will create a global variable in any case, and that
8733 // will have the state of initializer evaluation attached.
8735 (void)Init->EvaluateAsInitializer(Ctx.getASTContext(), VD, Result, true);
8736 return this->visitDeclRef(D, E);
8737 }
8738 return revisit(VD, !VD->isConstexpr() && DeclType->isReferenceType());
8739 }
8740
8741 // FIXME: The evaluateValue() check here is a little ridiculous, since
8742 // it will ultimately call into Context::evaluateAsInitializer(). In
8743 // other words, we're evaluating the initializer, just to know if we can
8744 // evaluate the initializer.
8745 if (VD->isLocalVarDecl() && typeShouldBeVisited(DeclType) && VD->getInit() &&
8746 !VD->getInit()->isValueDependent()) {
8747 if (VD->evaluateValue()) {
8748 bool IsConstexprUnknown = !DeclType.isConstant(Ctx.getASTContext()) &&
8749 !DeclType->isReferenceType();
8750 // Revisit the variable declaration, but make sure it's associated with a
8751 // different evaluation, so e.g. mutable reads don't work on it.
8752 EvalIDScope _(Ctx);
8753 return revisit(VD, IsConstexprUnknown);
8754 } else if (Ctx.getLangOpts().CPlusPlus23 && IsReference)
8755 return revisit(VD, /*IsConstexprUnknown=*/true);
8756
8757 if (IsReference)
8758 return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
8759 /*InitializerFailed=*/true, E);
8760 }
8761
8762 if (DiscardResult)
8763 return true;
8764 return this->emitDummyPtr(
8765 D, E, Ctx.getLangOpts().CPlusPlus23 && DeclType->isReferenceType());
8766}
8767
8768template <class Emitter>
8770 const auto *D = E->getDecl();
8771 return this->visitDeclRef(D, E);
8772}
8773
8774template <class Emitter>
8776 const DesignatedInitUpdateExpr *E) {
8777 if (!this->visitInitializer(E->getBase()))
8778 return false;
8779 return this->visitInitializer(E->getUpdater());
8780}
8781
8782template <class Emitter> bool Compiler<Emitter>::emitCleanup() {
8783 for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent()) {
8784 if (!C->destroyLocals())
8785 return false;
8786 }
8787 return true;
8788}
8789
8790template <class Emitter>
8791unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
8792 const QualType DerivedType) {
8793 const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
8794 if (const auto *R = Ty->getPointeeCXXRecordDecl())
8795 return R;
8796 return Ty->castAsCXXRecordDecl();
8797 };
8798 const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
8799 const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
8800
8801 return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
8802}
8803
8804/// Emit casts from a PrimType to another PrimType.
8805template <class Emitter>
8806bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
8807 QualType ToQT, const Expr *E) {
8808
8809 if (FromT == PT_Float) {
8810 // Floating to floating.
8811 if (ToT == PT_Float) {
8812 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
8813 return this->emitCastFP(ToSem, getRoundingMode(E), E);
8814 }
8815
8816 if (ToT == PT_IntAP)
8817 return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT),
8818 getFPOptions(E), E);
8819 if (ToT == PT_IntAPS)
8820 return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT),
8821 getFPOptions(E), E);
8822
8823 // Float to integral.
8824 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
8825 return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
8826 }
8827
8828 if (isIntegerOrBoolType(FromT) || FromT == PT_Bool) {
8829 if (ToT == PT_IntAP)
8830 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
8831 if (ToT == PT_IntAPS)
8832 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
8833
8834 // Integral to integral.
8835 if (isIntegerOrBoolType(ToT) || ToT == PT_Bool)
8836 return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
8837
8838 if (ToT == PT_Float) {
8839 // Integral to floating.
8840 const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
8841 return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
8842 }
8843 }
8844
8845 return false;
8846}
8847
8848template <class Emitter>
8849bool Compiler<Emitter>::emitIntegralCast(PrimType FromT, PrimType ToT,
8850 QualType ToQT, const Expr *E) {
8851 assert(FromT != ToT);
8852
8853 if (ToT == PT_IntAP)
8854 return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
8855 if (ToT == PT_IntAPS)
8856 return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
8857
8858 return this->emitCast(FromT, ToT, E);
8859}
8860
8861/// Emits __real(SubExpr)
8862template <class Emitter>
8863bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
8864 assert(SubExpr->getType()->isAnyComplexType());
8865
8866 if (DiscardResult)
8867 return this->discard(SubExpr);
8868
8869 if (!this->visit(SubExpr))
8870 return false;
8871 if (SubExpr->isLValue()) {
8872 if (!this->emitConstUint8(0, SubExpr))
8873 return false;
8874 return this->emitArrayElemPtrPopUint8(SubExpr);
8875 }
8876
8877 // Rvalue, load the actual element.
8878 return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
8879 0, SubExpr);
8880}
8881
8882template <class Emitter>
8883bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
8884 assert(!DiscardResult);
8885 PrimType ElemT = classifyComplexElementType(E->getType());
8886 // We emit the expression (__real(E) != 0 || __imag(E) != 0)
8887 // for us, that means (bool)E[0] || (bool)E[1]
8888 if (!this->emitArrayElem(ElemT, 0, E))
8889 return false;
8890 if (ElemT == PT_Float) {
8891 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
8892 return false;
8893 } else {
8894 if (!this->emitCast(ElemT, PT_Bool, E))
8895 return false;
8896 }
8897
8898 // We now have the bool value of E[0] on the stack.
8899 LabelTy LabelTrue = this->getLabel();
8900 if (!this->jumpTrue(LabelTrue, E))
8901 return false;
8902
8903 if (!this->emitArrayElemPop(ElemT, 1, E))
8904 return false;
8905 if (ElemT == PT_Float) {
8906 if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
8907 return false;
8908 } else {
8909 if (!this->emitCast(ElemT, PT_Bool, E))
8910 return false;
8911 }
8912 // Leave the boolean value of E[1] on the stack.
8913 LabelTy EndLabel = this->getLabel();
8914 this->jump(EndLabel, E);
8915
8916 this->emitLabel(LabelTrue);
8917 if (!this->emitPopPtr(E))
8918 return false;
8919 if (!this->emitConstBool(true, E))
8920 return false;
8921
8922 this->fallthrough(EndLabel);
8923 this->emitLabel(EndLabel);
8924
8925 return true;
8926}
8927
8928template <class Emitter>
8929bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
8930 const BinaryOperator *E) {
8931 assert(E->isComparisonOp());
8932 assert(!Initializing);
8933 if (DiscardResult)
8934 return this->discard(LHS) && this->discard(RHS);
8935
8936 PrimType ElemT;
8937 bool LHSIsComplex;
8938 unsigned LHSOffset;
8939 if (LHS->getType()->isAnyComplexType()) {
8940 LHSIsComplex = true;
8941 ElemT = classifyComplexElementType(LHS->getType());
8942 LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true);
8943 if (!this->visit(LHS))
8944 return false;
8945 if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
8946 return false;
8947 } else {
8948 LHSIsComplex = false;
8949 PrimType LHST = classifyPrim(LHS->getType());
8950 LHSOffset = this->allocateLocalPrimitive(LHS, LHST, /*IsConst=*/true);
8951 if (!this->visit(LHS))
8952 return false;
8953 if (!this->emitSetLocal(LHST, LHSOffset, E))
8954 return false;
8955 }
8956
8957 bool RHSIsComplex;
8958 unsigned RHSOffset;
8959 if (RHS->getType()->isAnyComplexType()) {
8960 RHSIsComplex = true;
8961 ElemT = classifyComplexElementType(RHS->getType());
8962 RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true);
8963 if (!this->visit(RHS))
8964 return false;
8965 if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
8966 return false;
8967 } else {
8968 RHSIsComplex = false;
8969 PrimType RHST = classifyPrim(RHS->getType());
8970 RHSOffset = this->allocateLocalPrimitive(RHS, RHST, /*IsConst=*/true);
8971 if (!this->visit(RHS))
8972 return false;
8973 if (!this->emitSetLocal(RHST, RHSOffset, E))
8974 return false;
8975 }
8976
8977 auto getElem = [&](unsigned LocalOffset, unsigned Index,
8978 bool IsComplex) -> bool {
8979 if (IsComplex) {
8980 if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
8981 return false;
8982 return this->emitArrayElemPop(ElemT, Index, E);
8983 }
8984 return this->emitGetLocal(ElemT, LocalOffset, E);
8985 };
8986
8987 for (unsigned I = 0; I != 2; ++I) {
8988 // Get both values.
8989 if (!getElem(LHSOffset, I, LHSIsComplex))
8990 return false;
8991 if (!getElem(RHSOffset, I, RHSIsComplex))
8992 return false;
8993 // And compare them.
8994 if (!this->emitEQ(ElemT, E))
8995 return false;
8996
8997 if (!this->emitCastBoolUint8(E))
8998 return false;
8999 }
9000
9001 // We now have two bool values on the stack. Compare those.
9002 if (!this->emitAddUint8(E))
9003 return false;
9004 if (!this->emitConstUint8(2, E))
9005 return false;
9006
9007 if (E->getOpcode() == BO_EQ) {
9008 if (!this->emitEQUint8(E))
9009 return false;
9010 } else if (E->getOpcode() == BO_NE) {
9011 if (!this->emitNEUint8(E))
9012 return false;
9013 } else
9014 return false;
9015
9016 // In C, this returns an int.
9017 if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
9018 return this->emitCast(PT_Bool, ResT, E);
9019 return true;
9020}
9021
9022/// When calling this, we have a pointer of the local-to-destroy
9023/// on the stack.
9024/// Emit destruction of record types (or arrays of record types).
9025template <class Emitter>
9026bool Compiler<Emitter>::emitRecordDestructionPop(const Record *R,
9027 SourceInfo Loc) {
9028 assert(R);
9029 assert(!R->hasTrivialDtor());
9030 const CXXDestructorDecl *Dtor = R->getDestructor();
9031 assert(Dtor);
9032 const Function *DtorFunc = getFunction(Dtor);
9033 if (!DtorFunc)
9034 return false;
9035 assert(DtorFunc->hasThisPointer());
9036 assert(DtorFunc->getNumParams() == 1);
9037 return this->emitCall(DtorFunc, 0, Loc);
9038}
9039/// When calling this, we have a pointer of the local-to-destroy
9040/// on the stack.
9041/// Emit destruction of record types (or arrays of record types).
9042template <class Emitter>
9043bool Compiler<Emitter>::emitDestructionPop(const Descriptor *Desc,
9044 SourceInfo Loc) {
9045 assert(Desc);
9046 assert(!Desc->hasTrivialDtor());
9047
9048 // Arrays.
9049 if (Desc->isArray()) {
9050 const Descriptor *ElemDesc = Desc->ElemDesc;
9051 assert(ElemDesc);
9052
9053 unsigned N = Desc->getNumElems();
9054 if (N == 0)
9055 return this->emitPopPtr(Loc);
9056
9057 for (ssize_t I = N - 1; I >= 1; --I) {
9058 if (!this->emitConstUint64(I, Loc))
9059 return false;
9060 if (!this->emitArrayElemPtrUint64(Loc))
9061 return false;
9062 if (!this->emitDestructionPop(ElemDesc, Loc))
9063 return false;
9064 }
9065 // Last iteration, removes the instance pointer from the stack.
9066 if (!this->emitConstUint64(0, Loc))
9067 return false;
9068 if (!this->emitArrayElemPtrPopUint64(Loc))
9069 return false;
9070 return this->emitDestructionPop(ElemDesc, Loc);
9071 }
9072
9073 assert(Desc->ElemRecord);
9074 assert(!Desc->ElemRecord->hasTrivialDtor());
9075 return this->emitRecordDestructionPop(Desc->ElemRecord, Loc);
9076}
9077
9078/// Create a dummy pointer for the given decl (or expr) and
9079/// push a pointer to it on the stack.
9080template <class Emitter>
9081bool Compiler<Emitter>::emitDummyPtr(DeclOrExpr D, const Expr *E, bool CU) {
9082 assert(!DiscardResult && "Should've been checked before");
9083 return this->emitGetOpaquePtr(D, CU, E);
9084}
9085
9086template <class Emitter>
9087bool Compiler<Emitter>::emitFloat(const APFloat &F, SourceInfo Info) {
9088 if (Floating::singleWord(F.getSemantics()))
9089 return this->emitConstFloat(Floating(F), Info);
9090
9091 APInt I = F.bitcastToAPInt();
9092 return this->emitConstFloat(
9093 Floating(const_cast<uint64_t *>(I.getRawData()),
9094 llvm::APFloatBase::SemanticsToEnum(F.getSemantics())),
9095 Info);
9096}
9097
9098// This function is constexpr if and only if To, From, and the types of
9099// all subobjects of To and From are types T such that...
9100// (3.1) - is_union_v<T> is false;
9101// (3.2) - is_pointer_v<T> is false;
9102// (3.3) - is_member_pointer_v<T> is false;
9103// (3.4) - is_volatile_v<T> is false; and
9104// (3.5) - T has no non-static data members of reference type
9105template <class Emitter>
9106bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
9107 const Expr *SubExpr = E->getSubExpr();
9108 QualType FromType = SubExpr->getType();
9109 QualType ToType = E->getType();
9110 OptPrimType ToT = classify(ToType);
9111
9112 assert(!ToType->isReferenceType());
9113
9114 // Prepare storage for the result in case we discard.
9115 if (DiscardResult && !Initializing && !ToT) {
9116 UnsignedOrNone LocalIndex = allocateLocal(E);
9117 if (!LocalIndex)
9118 return false;
9119 if (!this->emitGetPtrLocal(*LocalIndex, E))
9120 return false;
9121 }
9122
9123 // Get a pointer to the value-to-cast on the stack.
9124 // For CK_LValueToRValueBitCast, this is always an lvalue and
9125 // we later assume it to be one (i.e. a PT_Ptr). However,
9126 // we call this function for other utility methods where
9127 // a bitcast might be useful, so convert it to a PT_Ptr in that case.
9128 if (SubExpr->isGLValue() || FromType->isVectorType()) {
9129 if (!this->visit(SubExpr))
9130 return false;
9131 } else if (OptPrimType FromT = classify(SubExpr)) {
9132 unsigned TempOffset =
9133 allocateLocalPrimitive(SubExpr, *FromT, /*IsConst=*/true);
9134 if (!this->visit(SubExpr))
9135 return false;
9136 if (!this->emitSetLocal(*FromT, TempOffset, E))
9137 return false;
9138 if (!this->emitGetPtrLocal(TempOffset, E))
9139 return false;
9140 } else {
9141 return false;
9142 }
9143
9144 if (!ToT) {
9145 if (!this->emitBitCast(E))
9146 return false;
9147 return DiscardResult ? this->emitPopPtr(E) : true;
9148 }
9149 assert(ToT);
9150
9151 const llvm::fltSemantics *TargetSemantics = nullptr;
9152 if (ToT == PT_Float)
9153 TargetSemantics = &Ctx.getFloatSemantics(ToType);
9154
9155 // Conversion to a primitive type. FromType can be another
9156 // primitive type, or a record/array.
9157 bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
9158 ToType->isSpecificBuiltinType(BuiltinType::Char_U));
9159 uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
9160
9161 if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
9162 ResultBitWidth, TargetSemantics,
9163 ToType.getTypePtr(), E))
9164 return false;
9165
9166 if (DiscardResult)
9167 return this->emitPop(*ToT, E);
9168
9169 return true;
9170}
9171
9172/// Replicate a scalar value into every scalar element of an aggregate.
9173/// The scalar is stored in a local at \p SrcOffset and a pointer to the
9174/// destination must be on top of the interpreter stack. Each element receives
9175/// the scalar, cast to its own type.
9176template <class Emitter>
9177bool Compiler<Emitter>::emitHLSLAggregateSplat(PrimType SrcT,
9178 unsigned SrcOffset,
9179 QualType DestType,
9180 const Expr *E) {
9181 // Vectors and matrices are treated as flat sequences of elements.
9182 unsigned NumElems = 0;
9183 QualType ElemType;
9184 if (const auto *VT = DestType->getAs<VectorType>()) {
9185 NumElems = VT->getNumElements();
9186 ElemType = VT->getElementType();
9187 } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) {
9188 NumElems = MT->getNumElementsFlattened();
9189 ElemType = MT->getElementType();
9190 }
9191 if (NumElems > 0) {
9192 PrimType ElemT = classifyPrim(ElemType);
9193 for (unsigned I = 0; I != NumElems; ++I) {
9194 if (!this->emitGetLocal(SrcT, SrcOffset, E))
9195 return false;
9196 if (!this->emitPrimCast(SrcT, ElemT, ElemType, E))
9197 return false;
9198 if (!this->emitInitElem(ElemT, I, E))
9199 return false;
9200 }
9201 return true;
9202 }
9203
9204 // Arrays: primitive elements are filled directly; composite elements
9205 // require recursion into each sub-aggregate.
9206 if (const auto *AT = DestType->getAsArrayTypeUnsafe()) {
9207 const auto *CAT = cast<ConstantArrayType>(AT);
9208 QualType ArrElemType = CAT->getElementType();
9209 unsigned ArrSize = CAT->getZExtSize();
9210
9211 if (OptPrimType ElemT = classify(ArrElemType)) {
9212 for (unsigned I = 0; I != ArrSize; ++I) {
9213 if (!this->emitGetLocal(SrcT, SrcOffset, E))
9214 return false;
9215 if (!this->emitPrimCast(SrcT, *ElemT, ArrElemType, E))
9216 return false;
9217 if (!this->emitInitElem(*ElemT, I, E))
9218 return false;
9219 }
9220 } else {
9221 for (unsigned I = 0; I != ArrSize; ++I) {
9222 if (!this->emitConstUint32(I, E))
9223 return false;
9224 if (!this->emitArrayElemPtrUint32(E))
9225 return false;
9226 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, ArrElemType, E))
9227 return false;
9228 if (!this->emitFinishInitPop(E))
9229 return false;
9230 }
9231 }
9232 return true;
9233 }
9234
9235 // Records: fill base classes first, then named fields in declaration
9236 // order.
9237 if (DestType->isRecordType()) {
9238 const Record *R = getRecord(DestType);
9239 if (!R)
9240 return false;
9241
9242 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
9243 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
9244 const Record::Base *B = R->getBase(BS.getType());
9245 assert(B);
9246 if (!this->emitGetPtrBase(B->Offset, E))
9247 return false;
9248 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, BS.getType(), E))
9249 return false;
9250 if (!this->emitFinishInitPop(E))
9251 return false;
9252 }
9253 }
9254
9255 for (const Record::Field &F : R->fields()) {
9256 if (F.isUnnamedBitField())
9257 continue;
9258
9259 QualType FieldType = F.Decl->getType();
9260 if (OptPrimType FieldT = F.T) {
9261 if (!this->emitGetLocal(SrcT, SrcOffset, E))
9262 return false;
9263 if (!this->emitPrimCast(SrcT, *FieldT, FieldType, E))
9264 return false;
9265 if (F.isBitField()) {
9266 if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E))
9267 return false;
9268 } else {
9269 if (!this->emitInitField(*FieldT, F.Offset, E))
9270 return false;
9271 }
9272 } else {
9273 if (!this->emitGetPtrField(F.Offset, E))
9274 return false;
9275 if (!emitHLSLAggregateSplat(SrcT, SrcOffset, FieldType, E))
9276 return false;
9277 if (!this->emitPopPtr(E))
9278 return false;
9279 }
9280 }
9281 return true;
9282 }
9283
9284 return false;
9285}
9286
9287/// Return the total number of scalar elements in a type. This is used
9288/// to cap how many source elements are extracted during an elementwise cast,
9289/// so we never flatten more than the destination can hold.
9290template <class Emitter>
9291unsigned Compiler<Emitter>::countHLSLFlatElements(QualType Ty) {
9292 // Vector and matrix types are treated as flat sequences of elements.
9293 if (const auto *VT = Ty->getAs<VectorType>())
9294 return VT->getNumElements();
9295 if (const auto *MT = Ty->getAs<ConstantMatrixType>())
9296 return MT->getNumElementsFlattened();
9297 // Arrays: total count is array size * scalar elements per element.
9298 if (const auto *AT = Ty->getAsArrayTypeUnsafe()) {
9299 const auto *CAT = cast<ConstantArrayType>(AT);
9300 return CAT->getZExtSize() * countHLSLFlatElements(CAT->getElementType());
9301 }
9302 // Records: sum scalar element counts of base classes and named fields.
9303 if (Ty->isRecordType()) {
9304 const Record *R = getRecord(Ty);
9305 if (!R)
9306 return 0;
9307 unsigned Count = 0;
9308 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
9309 for (const CXXBaseSpecifier &BS : CXXRD->bases())
9310 Count += countHLSLFlatElements(BS.getType());
9311 }
9312 for (const Record::Field &F : R->fields()) {
9313 if (F.isUnnamedBitField())
9314 continue;
9315 Count += countHLSLFlatElements(F.Decl->getType());
9316 }
9317 return Count;
9318 }
9319 // Scalar primitive types contribute one element.
9320 if (canClassify(Ty))
9321 return 1;
9322 return 0;
9323}
9324
9325/// Walk a source aggregate and extract every scalar element into its own local
9326/// variable. The results are appended to \p Elements in declaration order,
9327/// stopping once \p MaxElements have been collected. A pointer to the
9328/// source aggregate must be stored in the local at \p SrcOffset.
9329template <class Emitter>
9330bool Compiler<Emitter>::emitHLSLFlattenAggregate(
9331 QualType SrcType, unsigned SrcOffset,
9332 SmallVectorImpl<HLSLFlatElement> &Elements, unsigned MaxElements,
9333 const Expr *E) {
9334
9335 // Save a scalar value from the stack into a new local and record it.
9336 auto saveToLocal = [&](PrimType T) -> bool {
9337 unsigned Offset = allocateLocalPrimitive(E, T, /*IsConst=*/true);
9338 if (!this->emitSetLocal(T, Offset, E))
9339 return false;
9340 Elements.push_back({Offset, T});
9341 return true;
9342 };
9343
9344 // Save a pointer from the stack into a new local for later use.
9345 auto savePtrToLocal = [&]() -> UnsignedOrNone {
9346 unsigned Offset = allocateLocalPrimitive(E, PT_Ptr, /*IsConst=*/true);
9347 if (!this->emitSetLocal(PT_Ptr, Offset, E))
9348 return std::nullopt;
9349 return Offset;
9350 };
9351
9352 // Vectors and matrices are flat sequences of elements.
9353 unsigned NumElems = 0;
9354 QualType ElemType;
9355 if (const auto *VT = SrcType->getAs<VectorType>()) {
9356 NumElems = VT->getNumElements();
9357 ElemType = VT->getElementType();
9358 } else if (const auto *MT = SrcType->getAs<ConstantMatrixType>()) {
9359 NumElems = MT->getNumElementsFlattened();
9360 ElemType = MT->getElementType();
9361 }
9362 if (NumElems > 0) {
9363 PrimType ElemT = classifyPrim(ElemType);
9364 for (unsigned I = 0; I != NumElems && Elements.size() < MaxElements; ++I) {
9365 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
9366 return false;
9367 if (!this->emitArrayElemPop(ElemT, I, E))
9368 return false;
9369 if (!saveToLocal(ElemT))
9370 return false;
9371 }
9372 return true;
9373 }
9374
9375 // Arrays: primitive elements are extracted directly; composite elements
9376 // require recursion into each sub-aggregate.
9377 if (const auto *AT = SrcType->getAsArrayTypeUnsafe()) {
9378 const auto *CAT = cast<ConstantArrayType>(AT);
9379 QualType ArrElemType = CAT->getElementType();
9380 unsigned ArrSize = CAT->getZExtSize();
9381
9382 if (OptPrimType ElemT = classify(ArrElemType)) {
9383 for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) {
9384 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
9385 return false;
9386 if (!this->emitArrayElemPop(*ElemT, I, E))
9387 return false;
9388 if (!saveToLocal(*ElemT))
9389 return false;
9390 }
9391 } else {
9392 for (unsigned I = 0; I != ArrSize && Elements.size() < MaxElements; ++I) {
9393 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
9394 return false;
9395 if (!this->emitConstUint32(I, E))
9396 return false;
9397 if (!this->emitArrayElemPtrPopUint32(E))
9398 return false;
9399 UnsignedOrNone ElemPtrOffset = savePtrToLocal();
9400 if (!ElemPtrOffset)
9401 return false;
9402 if (!emitHLSLFlattenAggregate(ArrElemType, *ElemPtrOffset, Elements,
9403 MaxElements, E))
9404 return false;
9405 }
9406 }
9407 return true;
9408 }
9409
9410 // Records: base classes come first, then named fields in declaration
9411 // order.
9412 if (SrcType->isRecordType()) {
9413 const Record *R = getRecord(SrcType);
9414 if (!R)
9415 return false;
9416
9417 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
9418 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
9419 if (Elements.size() >= MaxElements)
9420 break;
9421 const Record::Base *B = R->getBase(BS.getType());
9422 assert(B);
9423 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
9424 return false;
9425 if (!this->emitGetPtrBasePop(B->Offset, /*NullOK=*/false, E))
9426 return false;
9427 UnsignedOrNone BasePtrOffset = savePtrToLocal();
9428 if (!BasePtrOffset)
9429 return false;
9430 if (!emitHLSLFlattenAggregate(BS.getType(), *BasePtrOffset, Elements,
9431 MaxElements, E))
9432 return false;
9433 }
9434 }
9435
9436 for (const Record::Field &F : R->fields()) {
9437 if (Elements.size() >= MaxElements)
9438 break;
9439 if (F.isUnnamedBitField())
9440 continue;
9441
9442 QualType FieldType = F.Decl->getType();
9443 if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
9444 return false;
9445 if (!this->emitGetPtrFieldPop(F.Offset, E))
9446 return false;
9447
9448 if (OptPrimType FieldT = F.T) {
9449 if (!this->emitLoadPop(*FieldT, E))
9450 return false;
9451 if (!saveToLocal(*FieldT))
9452 return false;
9453 } else {
9454 UnsignedOrNone FieldPtrOffset = savePtrToLocal();
9455 if (!FieldPtrOffset)
9456 return false;
9457 if (!emitHLSLFlattenAggregate(FieldType, *FieldPtrOffset, Elements,
9458 MaxElements, E))
9459 return false;
9460 }
9461 }
9462 return true;
9463 }
9464
9465 return false;
9466}
9467
9468/// Populate an HLSL aggregate from a flat list of previously extracted source
9469/// elements, casting each to the corresponding destination element type.
9470/// \p ElemIdx tracks the current position in \p Elements and is advanced as
9471/// elements are consumed. A pointer to the destination must be on top of the
9472/// interpreter stack.
9473template <class Emitter>
9474bool Compiler<Emitter>::emitHLSLConstructAggregate(
9475 QualType DestType, ArrayRef<HLSLFlatElement> Elements, unsigned &ElemIdx,
9476 const Expr *E) {
9477
9478 // Consume the next source element, cast it, and leave it on the stack.
9479 auto loadAndCast = [&](PrimType DestT, QualType DestQT) -> bool {
9480 const auto &Src = Elements[ElemIdx++];
9481 if (!this->emitGetLocal(Src.Type, Src.LocalOffset, E))
9482 return false;
9483 return this->emitPrimCast(Src.Type, DestT, DestQT, E);
9484 };
9485
9486 // Vectors and matrices are flat sequences of elements.
9487 unsigned NumElems = 0;
9488 QualType ElemType;
9489 if (const auto *VT = DestType->getAs<VectorType>()) {
9490 NumElems = VT->getNumElements();
9491 ElemType = VT->getElementType();
9492 } else if (const auto *MT = DestType->getAs<ConstantMatrixType>()) {
9493 NumElems = MT->getNumElementsFlattened();
9494 ElemType = MT->getElementType();
9495 }
9496 if (NumElems > 0) {
9497 PrimType DestElemT = classifyPrim(ElemType);
9498 for (unsigned I = 0; I != NumElems; ++I) {
9499 if (!loadAndCast(DestElemT, ElemType))
9500 return false;
9501 if (!this->emitInitElem(DestElemT, I, E))
9502 return false;
9503 }
9504 return true;
9505 }
9506
9507 // Arrays: primitive elements are filled directly; composite elements
9508 // require recursion into each sub-aggregate.
9509 if (const auto *AT = DestType->getAsArrayTypeUnsafe()) {
9510 const auto *CAT = cast<ConstantArrayType>(AT);
9511 QualType ArrElemType = CAT->getElementType();
9512 unsigned ArrSize = CAT->getZExtSize();
9513
9514 if (OptPrimType ElemT = classify(ArrElemType)) {
9515 for (unsigned I = 0; I != ArrSize; ++I) {
9516 if (!loadAndCast(*ElemT, ArrElemType))
9517 return false;
9518 if (!this->emitInitElem(*ElemT, I, E))
9519 return false;
9520 }
9521 } else {
9522 for (unsigned I = 0; I != ArrSize; ++I) {
9523 if (!this->emitConstUint32(I, E))
9524 return false;
9525 if (!this->emitArrayElemPtrUint32(E))
9526 return false;
9527 if (!emitHLSLConstructAggregate(ArrElemType, Elements, ElemIdx, E))
9528 return false;
9529 if (!this->emitFinishInitPop(E))
9530 return false;
9531 }
9532 }
9533 return true;
9534 }
9535
9536 // Records: base classes come first, then named fields in declaration
9537 // order.
9538 if (DestType->isRecordType()) {
9539 const Record *R = getRecord(DestType);
9540 if (!R)
9541 return false;
9542
9543 if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(R->getDecl())) {
9544 for (const CXXBaseSpecifier &BS : CXXRD->bases()) {
9545 const Record::Base *B = R->getBase(BS.getType());
9546 assert(B);
9547 if (!this->emitGetPtrBase(B->Offset, E))
9548 return false;
9549 if (!emitHLSLConstructAggregate(BS.getType(), Elements, ElemIdx, E))
9550 return false;
9551 if (!this->emitFinishInitPop(E))
9552 return false;
9553 }
9554 }
9555
9556 for (const Record::Field &F : R->fields()) {
9557 if (F.isUnnamedBitField())
9558 continue;
9559
9560 QualType FieldType = F.Decl->getType();
9561 if (OptPrimType FieldT = F.T) {
9562 if (!loadAndCast(*FieldT, FieldType))
9563 return false;
9564 if (F.isBitField()) {
9565 if (!this->emitInitBitField(*FieldT, F.Offset, F.bitWidth(), E))
9566 return false;
9567 } else {
9568 if (!this->emitInitField(*FieldT, F.Offset, E))
9569 return false;
9570 }
9571 } else {
9572 if (!this->emitGetPtrField(F.Offset, E))
9573 return false;
9574 if (!emitHLSLConstructAggregate(FieldType, Elements, ElemIdx, E))
9575 return false;
9576 if (!this->emitPopPtr(E))
9577 return false;
9578 }
9579 }
9580 return true;
9581 }
9582
9583 return false;
9584}
9585
9586namespace clang {
9587namespace interp {
9588
9589template class Compiler<ByteCodeEmitter>;
9590template class Compiler<EvalEmitter>;
9591
9592} // namespace interp
9593} // namespace clang
static bool visitAPValue(const APValue &Value, llvm::function_ref< bool(const APValue &)> Visitor)
Definition APValue.cpp:24
#define V(N, I)
static bool emit(llvm::SmallVectorImpl< std::byte > &Code, const T &Val)
Helper to write bytecode and bail out if 32-bit offsets become invalid.
static void emitCleanup(CIRGenFunction &cgf, cir::CleanupScopeOp cleanupScope, EHScopeStack::Cleanup *cleanup, EHScopeStack::Cleanup::Flags flags, Address activeFlag)
static uint32_t getBitWidth(const Expr *E)
static CharUnits alignOfType(QualType T, const ASTContext &ASTCtx, UnaryExprOrTypeTrait Kind)
#define EMIT_ARITH_OP(OP)
static const Expr * stripDerivedToBaseCasts(const Expr *E)
static bool isTrivialMemoryOperation(const CXXMethodDecl *MD)
static const Expr * stripCheckedDerivedToBaseCasts(const Expr *E)
static bool hasTrivialDefaultCtorParent(const FieldDecl *FD)
static bool initNeedsOverridenLoc(const CXXCtorInitializer *Init)
llvm::APSInt APSInt
Definition Compiler.cpp:26
const Expr * ignorePointerCastsAndParens(const Expr *E)
A more selective version of E->IgnoreParenCasts for tryEvaluateBuiltinObjectSize. This ignores some c...
bool isReadByLvalueToRvalueConversion(const CXXRecordDecl *RD)
Determine whether a type would actually be read by an lvalue-to-rvalue conversion.
unsigned ConvertBuiltinIDToX86BuiltinID(const ASTContext &Ctx, unsigned BuiltinOp)
Convert a builtin ID to the canonical x86 builtin ID the constant evaluators dispatch on in their x86...
TokenType getType() const
Returns the token's type, e.g.
Result
Implement __builtin_bit_cast and related operations.
llvm::SmallPtrSet< const ParmVarDecl *, 1 > FoundParams
bool VisitDeclRefExpr(const DeclRefExpr *E) override
a trap message and trap category.
llvm::APInt getValue() const
APValue - This class implements a discriminated union of [uninitialized] [APSInt] [APFloat],...
Definition APValue.h:158
const LValueBase getLValueBase() const
Definition APValue.cpp:1137
APValue & getArrayInitializedElt(unsigned I)
Definition APValue.h:695
ArrayRef< LValuePathEntry > getLValuePath() const
Definition APValue.cpp:1157
APSInt & getInt()
Definition APValue.h:577
APValue & getStructField(unsigned i)
Definition APValue.h:740
const FieldDecl * getUnionField() const
Definition APValue.h:761
bool isVector() const
Definition APValue.h:555
APSInt & getComplexIntImag()
Definition APValue.h:615
unsigned getStructNumFields() const
Definition APValue.h:727
bool isComplexInt() const
Definition APValue.h:552
APValue & getStructVirtualBase(unsigned i)
Definition APValue.h:745
bool isArray() const
Definition APValue.h:557
bool isMemberPointerToDerivedMember() const
Definition APValue.cpp:1227
unsigned getArrayInitializedElts() const
Definition APValue.h:714
bool isFloat() const
Definition APValue.h:550
unsigned getStructNumBases() const
Definition APValue.h:723
unsigned getStructNumVirtualBases() const
Definition APValue.h:731
bool hasLValuePath() const
Definition APValue.cpp:1152
const ValueDecl * getMemberPointerDecl() const
Definition APValue.cpp:1220
APValue & getUnionValue()
Definition APValue.h:765
CharUnits & getLValueOffset()
Definition APValue.cpp:1147
bool isComplexFloat() const
Definition APValue.h:553
APValue & getVectorElt(unsigned I)
Definition APValue.h:651
APValue & getArrayFiller()
Definition APValue.h:706
unsigned getVectorLength() const
Definition APValue.h:659
bool isLValue() const
Definition APValue.h:554
bool isIndeterminate() const
Definition APValue.h:546
ArrayRef< const CXXRecordDecl * > getMemberPointerPath() const
Definition APValue.cpp:1234
bool isMemberPointer() const
Definition APValue.h:560
bool isInt() const
Definition APValue.h:549
unsigned getArraySize() const
Definition APValue.h:718
bool isUnion() const
Definition APValue.h:559
@ None
There is no such object (it's outside its lifetime).
Definition APValue.h:165
bool isStruct() const
Definition APValue.h:558
bool isNullPointer() const
Definition APValue.cpp:1173
APSInt & getComplexIntReal()
Definition APValue.h:607
APFloat & getComplexFloatImag()
Definition APValue.h:631
APFloat & getComplexFloatReal()
Definition APValue.h:623
APFloat & getFloat()
Definition APValue.h:591
APValue & getStructBase(unsigned i)
Definition APValue.h:735
Holds long-lived AST nodes (such as types and decls) that can be referred to throughout the semantic ...
Definition ASTContext.h:245
CharUnits getTypeAlignInChars(QualType T) const
Return the ABI-specified alignment of a (complete) type T, in characters.
unsigned getPreferredTypeAlign(QualType T) const
Return the "preferred" alignment of the specified type T for the current target, in bits.
const LangOptions & getLangOpts() const
unsigned getOpenMPDefaultSimdAlign(QualType T) const
Get default simd alignment of the specified complete type in bits.
TypeInfoChars getTypeInfoDataSizeInChars(QualType T) const
CharUnits getDeclAlign(const Decl *D, bool ForAlignof=false) const
Return a conservative estimate of the alignment of the specified decl D.
llvm::FixedPointSemantics getFixedPointSemantics(QualType Ty) const
CharUnits getTypeSizeInChars(QualType T) const
Return the size of the specified (complete) type T, in characters.
const VariableArrayType * getAsVariableArrayType(QualType T) const
CharUnits toCharUnitsFromBits(int64_t BitSize) const
Convert a size in bits to a size in characters.
AbstractConditionalOperator - An abstract base class for ConditionalOperator and BinaryConditionalOpe...
Definition Expr.h:4422
Expr * getCond() const
getCond - Return the expression representing the condition for the ?
Definition Expr.h:4600
Expr * getTrueExpr() const
getTrueExpr - Return the subexpression representing the value of the expression if the condition eval...
Definition Expr.h:4606
Expr * getFalseExpr() const
getFalseExpr - Return the subexpression representing the value of the expression if the condition eva...
Definition Expr.h:4612
AddrLabelExpr - The GNU address of label extension, representing &&label.
Definition Expr.h:4619
Represents the index of the current element of an array being initialized by an ArrayInitLoopExpr.
Definition Expr.h:6107
Represents a loop initializing the elements of an array.
Definition Expr.h:6054
llvm::APInt getArraySize() const
Definition Expr.h:6076
OpaqueValueExpr * getCommonExpr() const
Get the common subexpression shared by all initializations (the source array).
Definition Expr.h:6069
Expr * getSubExpr() const
Get the initializer to use for each array element.
Definition Expr.h:6074
ArraySubscriptExpr - [C99 6.5.2.1] Array Subscripting.
Definition Expr.h:2790
Expr * getLHS()
An array access can be written A[4] or 4[A] (both are equivalent).
Definition Expr.h:2819
An Embarcadero array type trait, as used in the implementation of __array_rank and __array_extent.
Definition ExprCXX.h:3011
uint64_t getValue() const
Definition ExprCXX.h:3059
Represents an array type, per C99 6.7.5.2 - Array Declarators.
Definition TypeBase.h:3820
QualType getElementType() const
Definition TypeBase.h:3832
Attr - This represents one attribute.
Definition Attr.h:47
Represents an attribute applied to a statement.
Definition Stmt.h:2214
Stmt * getSubStmt()
Definition Stmt.h:2250
ArrayRef< const Attr * > getAttrs() const
Definition Stmt.h:2246
Represents a C++ declaration that introduces decls from somewhere else.
Definition DeclCXX.h:3543
A builtin binary operation expression such as "x + y" or "x <= y".
Definition Expr.h:4107
static bool isLogicalOp(Opcode Opc)
Definition Expr.h:4240
Expr * getLHS() const
Definition Expr.h:4157
static bool isComparisonOp(Opcode Opc)
Definition Expr.h:4207
static bool isShiftOp(Opcode Opc)
Definition Expr.h:4195
static bool isCommaOp(Opcode Opc)
Definition Expr.h:4210
static Opcode getOpForCompoundAssignment(Opcode Opc)
Definition Expr.h:4254
Expr * getRHS() const
Definition Expr.h:4159
static bool isPtrMemOp(Opcode Opc)
predicates to categorize the respective opcodes.
Definition Expr.h:4184
static bool isAssignmentOp(Opcode Opc)
Definition Expr.h:4243
static bool isCompoundAssignmentOp(Opcode Opc)
Definition Expr.h:4248
Opcode getOpcode() const
Definition Expr.h:4152
static bool isBitwiseOp(Opcode Opc)
Definition Expr.h:4198
BlockExpr - Adaptor class for mixing a BlockDecl with expressions.
Definition Expr.h:6758
BreakStmt - This represents a break.
Definition Stmt.h:3148
Represents a C++2a __builtin_bit_cast(T, v) expression.
Definition ExprCXX.h:5530
Represents a base class of a C++ class.
Definition DeclCXX.h:146
Represents binding an expression to a temporary.
Definition ExprCXX.h:1498
const Expr * getSubExpr() const
Definition ExprCXX.h:1520
A boolean literal, per ([C++ lex.bool] Boolean literals).
Definition ExprCXX.h:728
bool getValue() const
Definition ExprCXX.h:745
Represents a call to a C++ constructor.
Definition ExprCXX.h:1553
bool isElidable() const
Whether this construction is elidable.
Definition ExprCXX.h:1622
Expr * getArg(unsigned Arg)
Return the specified argument.
Definition ExprCXX.h:1696
arg_range arguments()
Definition ExprCXX.h:1677
bool requiresZeroInitialization() const
Whether this construction first requires zero-initialization before the initializer is called.
Definition ExprCXX.h:1655
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will (ultimately) call.
Definition ExprCXX.h:1616
unsigned getNumArgs() const
Return the number of arguments to the constructor call.
Definition ExprCXX.h:1693
Represents a C++ constructor within a class.
Definition DeclCXX.h:2645
bool isDefaultConstructor() const
Whether this constructor is a default constructor (C++ [class.ctor]p5), which can be used to default-...
Definition DeclCXX.cpp:3049
bool isCopyOrMoveConstructor(unsigned &TypeQuals) const
Determine whether this is a copy or move constructor.
Definition DeclCXX.cpp:3069
Represents a C++ base or member initializer.
Definition DeclCXX.h:2410
A default argument (C++ [dcl.fct.default]).
Definition ExprCXX.h:1275
A use of a default initializer in a constructor or in aggregate initialization.
Definition ExprCXX.h:1382
Expr * getExpr()
Get the initialization expression that will be used.
Definition ExprCXX.cpp:1137
Represents a delete expression for memory deallocation and destructor calls, e.g.
Definition ExprCXX.h:2631
FunctionDecl * getOperatorDelete() const
Definition ExprCXX.h:2670
bool isArrayForm() const
Definition ExprCXX.h:2657
bool isGlobalDelete() const
Definition ExprCXX.h:2656
Represents a C++ destructor within a class.
Definition DeclCXX.h:2924
A C++ dynamic_cast expression (C++ [expr.dynamic.cast]).
Definition ExprCXX.h:486
Represents the code generated for an expanded expansion statement.
Definition StmtCXX.h:1028
ArrayRef< Stmt * > getInstantiations() const
Definition StmtCXX.h:1069
ArrayRef< Stmt * > getPreambleStmts() const
Definition StmtCXX.h:1073
CXXForRangeStmt - This represents C++0x [stmt.ranged]'s ranged for statement, represented as 'for (ra...
Definition StmtCXX.h:136
DeclStmt * getBeginStmt()
Definition StmtCXX.h:164
DeclStmt * getLoopVarStmt()
Definition StmtCXX.h:170
DeclStmt * getEndStmt()
Definition StmtCXX.h:167
DeclStmt * getRangeStmt()
Definition StmtCXX.h:163
Represents a call to an inherited base class constructor from an inheriting constructor.
Definition ExprCXX.h:1756
CXXConstructorDecl * getConstructor() const
Get the constructor that this expression will call.
Definition ExprCXX.h:1793
Represents a static or instance method of a struct/union/class.
Definition DeclCXX.h:2153
const CXXRecordDecl * getParent() const
Return the parent of this method declaration, which is the class in which this method is defined.
Definition DeclCXX.h:2296
bool isMoveAssignmentOperator() const
Determine whether this is a move assignment operator.
Definition DeclCXX.cpp:2751
bool isCopyAssignmentOperator() const
Determine whether this is a copy-assignment operator, regardless of whether it was declared implicitl...
Definition DeclCXX.cpp:2730
bool isLambdaStaticInvoker() const
Determine whether this is a lambda closure type's static member function that is used for the result ...
Definition DeclCXX.cpp:2895
Represents a new-expression for memory allocation and constructor calls, e.g: "new CXXNewExpr(foo)".
Definition ExprCXX.h:2360
bool isArray() const
Definition ExprCXX.h:2469
QualType getAllocatedType() const
Definition ExprCXX.h:2439
std::optional< Expr * > getArraySize()
This might return std::nullopt even if isArray() returns true, since there might not be an array size...
Definition ExprCXX.h:2474
Expr * getPlacementArg(unsigned I)
Definition ExprCXX.h:2508
unsigned getNumPlacementArgs() const
Definition ExprCXX.h:2499
FunctionDecl * getOperatorNew() const
Definition ExprCXX.h:2464
Expr * getInitializer()
The initializer of this new-expression.
Definition ExprCXX.h:2538
Represents a C++11 noexcept expression (C++ [expr.unary.noexcept]).
Definition ExprCXX.h:4363
bool getValue() const
Definition ExprCXX.h:4386
The null pointer literal (C++11 [lex.nullptr])
Definition ExprCXX.h:773
Represents a list-initialization with parenthesis.
Definition ExprCXX.h:5195
MutableArrayRef< Expr * > getInitExprs()
Definition ExprCXX.h:5235
Represents a C++ struct/union/class.
Definition DeclCXX.h:258
bool hasTrivialDefaultConstructor() const
Determine whether this class has a trivial default constructor (C++11 [class.ctor]p5).
Definition DeclCXX.h:1256
bool isGenericLambda() const
Determine whether this class describes a generic lambda function object (i.e.
Definition DeclCXX.cpp:1681
capture_const_range captures() const
Definition DeclCXX.h:1107
CXXMethodDecl * getLambdaCallOperator() const
Retrieve the lambda call operator of the closure type if this is a closure type.
Definition DeclCXX.cpp:1744
Represents a C++26 reflect expression [expr.reflect].
Definition ExprCXX.h:5562
ReflectionKind getKind() const
Definition ExprCXX.h:5599
const void * getOpaqueValue() const
Definition ExprCXX.h:5600
A C++ reinterpret_cast expression (C++ [expr.reinterpret.cast]).
Definition ExprCXX.h:531
A rewritten comparison expression that was originally written using operator syntax.
Definition ExprCXX.h:291
Expr * getSemanticForm()
Get an equivalent semantic form for this expression.
Definition ExprCXX.h:309
An expression "T()" which creates an rvalue of a non-class type T.
Definition ExprCXX.h:2201
Implicit construction of a std::initializer_list<T> object from an array temporary within list-initia...
Definition ExprCXX.h:805
Represents the this expression in C++.
Definition ExprCXX.h:1159
A C++ throw-expression (C++ [except.throw]).
Definition ExprCXX.h:1213
const Expr * getSubExpr() const
Definition ExprCXX.h:1233
CXXTryStmt - A C++ try block, including all handlers.
Definition StmtCXX.h:70
CompoundStmt * getTryBlock()
Definition StmtCXX.h:101
A C++ typeid expression (C++ [expr.typeid]), which gets the type_info that corresponds to the supplie...
Definition ExprCXX.h:853
bool isTypeOperand() const
Definition ExprCXX.h:889
QualType getTypeOperand(const ASTContext &Context) const
Retrieves the type operand of this typeid() expression after various required adjustments (removing r...
Definition ExprCXX.cpp:168
Expr * getExprOperand() const
Definition ExprCXX.h:900
bool isPotentiallyEvaluated() const
Determine whether this typeid has a type operand which is potentially evaluated, per C++11 [expr....
Definition ExprCXX.cpp:136
A Microsoft C++ __uuidof expression, which gets the _GUID that corresponds to the supplied type or ex...
Definition ExprCXX.h:1073
MSGuidDecl * getGuidDecl() const
Definition ExprCXX.h:1119
CallExpr - Represents a function call (C99 6.5.2.2, C++ [expr.call]).
Definition Expr.h:3012
Expr * getArg(unsigned Arg)
getArg - Return the specified argument.
Definition Expr.h:3216
FunctionDecl * getDirectCallee()
If the callee is a FunctionDecl, return it. Otherwise return null.
Definition Expr.h:3195
Expr * getCallee()
Definition Expr.h:3159
unsigned getNumArgs() const
getNumArgs - Return the number of actual arguments to this call.
Definition Expr.h:3203
Expr ** getArgs()
Retrieve the call arguments.
Definition Expr.h:3206
arg_range arguments()
Definition Expr.h:3264
QualType getCallReturnType(const ASTContext &Ctx) const
getCallReturnType - Get the return type of the call expr.
Definition Expr.cpp:1635
CaseStmt - Represent a case statement.
Definition Stmt.h:1931
Stmt * getSubStmt()
Definition Stmt.h:2044
CastExpr - Base class for type casts, including both implicit casts (ImplicitCastExpr) and explicit c...
Definition Expr.h:3745
path_iterator path_begin()
Definition Expr.h:3815
CastKind getCastKind() const
Definition Expr.h:3789
llvm::iterator_range< path_iterator > path()
Path through the class hierarchy taken by casts between base and derived classes (see implementation ...
Definition Expr.h:3832
const FieldDecl * getTargetUnionField() const
Definition Expr.h:3839
path_iterator path_end()
Definition Expr.h:3816
Expr * getSubExpr()
Definition Expr.h:3795
This is an opaque type for sizes expressed in character units.
Definition CharUnits.h:38
QuantityType getQuantity() const
Get the raw integer representation of this quantity.
Definition CharUnits.h:155
static CharUnits One()
Construct a CharUnits quantity of one.
Definition CharUnits.h:55
unsigned getValue() const
Definition Expr.h:1674
ChooseExpr - GNU builtin-in function __builtin_choose_expr.
Definition Expr.h:4917
Expr * getChosenSubExpr() const
getChosenSubExpr - Return the subexpression chosen according to the condition.
Definition Expr.h:4953
const ValueInfo * getValueInfo(ComparisonCategoryResult ValueKind) const
Complex values, per C99 6.2.5p11.
Definition TypeBase.h:3362
QualType getElementType() const
Definition TypeBase.h:3372
CompoundAssignOperator - For compound assignments (e.g.
Definition Expr.h:4369
QualType getComputationLHSType() const
Definition Expr.h:4403
QualType getComputationResultType() const
Definition Expr.h:4406
CompoundLiteralExpr - [C99 6.5.2.5].
Definition Expr.h:3674
bool isFileScope() const
Definition Expr.h:3706
const Expr * getInitializer() const
Definition Expr.h:3702
CompoundStmt - This represents a group of statements like { stmt stmt }.
Definition Stmt.h:1751
body_range body()
Definition Stmt.h:1814
Stmt * body_back()
Definition Stmt.h:1819
Represents the specialization of a concept - evaluates to a prvalue of type bool.
bool isSatisfied() const
Whether or not the concept with the given arguments was satisfied when the expression was created.
Represents the canonical version of C arrays with a specified constant size.
Definition TypeBase.h:3858
uint64_t getZExtSize() const
Return the size zero-extended as a uint64_t.
Definition TypeBase.h:3934
An expression that occurs in a constant context and the result of evaluating the expression.
Definition Expr.h:1127
APValue getAPValueResult() const
Definition Expr.cpp:419
bool hasAPValueResult() const
Definition Expr.h:1202
Represents a concrete matrix type with constant number of rows and columns.
Definition TypeBase.h:4622
ContinueStmt - This represents a continue.
Definition Stmt.h:3132
ConvertVectorExpr - Clang builtin function __builtin_convertvector This AST node provides support for...
Definition Expr.h:4788
Expr * getSrcExpr() const
getSrcExpr - Return the Expr to be converted.
Definition Expr.h:4878
DeclContext * getParent()
getParent - Returns the containing DeclContext.
Definition DeclBase.h:2106
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1315
ValueDecl * getDecl()
Definition Expr.h:1383
DeclStmt - Adaptor class for mixing declarations with statements and expressions.
Definition Stmt.h:1642
decl_range decls()
Definition Stmt.h:1690
Decl - This represents one declaration (or definition), e.g.
Definition DeclBase.h:86
bool isInvalidDecl() const
Definition DeclBase.h:597
bool hasAttr() const
Definition DeclBase.h:586
OverloadedOperatorKind getCXXOverloadedOperator() const
If this name is the name of an overloadable operator in C++ (e.g., operator+), retrieve the kind of o...
Stmt * getSubStmt()
Definition Stmt.h:2092
InitListExpr * getUpdater() const
Definition Expr.h:6022
DoStmt - This represents a 'do/while' stmt.
Definition Stmt.h:2843
Stmt * getBody()
Definition Stmt.h:2868
Expr * getCond()
Definition Stmt.h:2861
virtual bool TraverseStmt(MaybeConst< Stmt > *S)
Recursively visit a statement or expression, by dispatching to Traverse*() based on the argument's dy...
const Expr * getBase() const
Definition Expr.h:6667
Represents a reference to emded data.
Definition Expr.h:5204
ChildElementIter< false > begin()
Definition Expr.h:5310
An instance of this object exists for each enum constant that is defined.
Definition Decl.h:3604
Represents an expression – generally a full-expression – that introduces cleanups to be run at the en...
Definition ExprCXX.h:3715
This represents one expression.
Definition Expr.h:114
const Expr * skipRValueSubobjectAdjustments(SmallVectorImpl< const Expr * > &CommaLHS, SmallVectorImpl< SubobjectAdjustment > &Adjustments) const
Walk outwards from an expression we want to bind a reference to and find the expression whose lifetim...
Definition Expr.cpp:85
bool isGLValue() const
Definition Expr.h:289
bool isValueDependent() const
Determines whether the value of this expression depends on.
Definition Expr.h:179
Expr * IgnoreImplicit() LLVM_READONLY
Skip past any implicit AST nodes which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3117
bool containsErrors() const
Whether this expression contains subexpressions which had errors.
Definition Expr.h:248
Expr * IgnoreParens() LLVM_READONLY
Skip past any parentheses which might surround this expression until reaching a fixed point.
Definition Expr.cpp:3125
bool isPRValue() const
Definition Expr.h:287
bool isLValue() const
isLValue - True if this expression is an "l-value" according to the rules of the current language.
Definition Expr.h:286
bool HasSideEffects(const ASTContext &Ctx, bool IncludePossibleEffects=true) const
HasSideEffects - This routine returns true for all those expressions which have any effect other than...
Definition Expr.cpp:3728
bool isTemporaryObject(ASTContext &Ctx, const CXXRecordDecl *TempTy) const
Determine whether the result of this expression is a temporary object of the given class type.
Definition Expr.cpp:3292
bool refersToBitField() const
Returns true if this expression is a gl-value that potentially refers to a bit-field.
Definition Expr.h:481
QualType getType() const
Definition Expr.h:146
An expression trait intrinsic.
Definition ExprCXX.h:3084
ExtVectorElementExpr - This represents access to specific elements of a vector, and may occur on the ...
Definition Expr.h:6696
void getEncodedElementAccess(SmallVectorImpl< uint32_t > &Elts) const
getEncodedElementAccess - Encode the elements accessed into an llvm aggregate Constant of ConstantInt...
Definition Expr.cpp:4598
Represents a member of a struct/union/class.
Definition Decl.h:3341
const RecordDecl * getParent() const
Returns the parent of this field declaration, which is the struct in which this field is defined.
Definition Decl.h:3577
llvm::APInt getValue() const
Returns an internal integer representation of the literal.
Definition Expr.h:1620
llvm::APFloat getValue() const
Definition Expr.h:1711
ForStmt - This represents a 'for (init;cond;inc)' stmt.
Definition Stmt.h:2899
Stmt * getInit()
Definition Stmt.h:2914
VarDecl * getConditionVariable() const
Retrieve the variable declared in this "for" statement, if any.
Definition Stmt.cpp:1120
Stmt * getBody()
Definition Stmt.h:2943
Expr * getInc()
Definition Stmt.h:2942
Expr * getCond()
Definition Stmt.h:2941
DeclStmt * getConditionVariableDeclStmt()
If this ForStmt has a condition variable, return the faux DeclStmt associated with the creation of th...
Definition Stmt.h:2929
const Expr * getSubExpr() const
Definition Expr.h:1107
Represents a function declaration or definition.
Definition Decl.h:2082
const ParmVarDecl * getParamDecl(unsigned i) const
Definition Decl.h:2974
Stmt * getBody(const FunctionDecl *&Definition) const
Retrieve the body (definition) of the function.
Definition Decl.cpp:3285
bool isFunctionTemplateSpecialization() const
Determine whether this function is a function template specialization.
Definition Decl.cpp:4281
FunctionTemplateDecl * getDescribedFunctionTemplate() const
Retrieves the function template that is described by this function declaration.
Definition Decl.cpp:4269
unsigned getBuiltinID(bool ConsiderWrapperFunctions=false) const
Returns a value indicating whether this function corresponds to a builtin function.
Definition Decl.cpp:3836
QualType getReturnType() const
Definition Decl.h:3022
ArrayRef< ParmVarDecl * > parameters() const
Definition Decl.h:2947
bool isTrivial() const
Whether this function is "trivial" in some specialized C++ senses.
Definition Decl.h:2546
const TemplateArgumentList * getTemplateSpecializationArgs() const
Retrieve the template arguments used to produce this function template specialization from the primar...
Definition Decl.cpp:4405
bool isUsableAsGlobalAllocationFunctionInConstantEvaluation(UnsignedOrNone *AlignmentParam=nullptr, bool *IsNothrow=nullptr) const
Determines whether this function is one of the replaceable global allocation functions described in i...
Definition Decl.cpp:3500
bool isDefaulted() const
Whether this function is defaulted.
Definition Decl.h:2554
unsigned getNumParams() const
Return the number of parameters this function must have based on its FunctionType.
Definition Decl.cpp:3900
bool hasBody(const FunctionDecl *&Definition) const
Returns true if the function has a body.
Definition Decl.cpp:3205
Declaration of a template function.
FunctionDecl * findSpecialization(ArrayRef< TemplateArgument > Args, llvm::FoldingSetInsertToken &InsertToken)
Return the specialization with the provided arguments if it exists, otherwise return the insertion po...
GNUNullExpr - Implements the GNU __null extension, which is a name for a null pointer constant that h...
Definition Expr.h:4992
Represents a C11 generic selection.
Definition Expr.h:6268
Expr * getResultExpr()
Return the result expression of this controlling expression.
Definition Expr.h:6554
IfStmt - This represents an if/then/else.
Definition Stmt.h:2270
Stmt * getThen()
Definition Stmt.h:2359
Stmt * getInit()
Definition Stmt.h:2420
bool isNonNegatedConsteval() const
Definition Stmt.h:2455
Expr * getCond()
Definition Stmt.h:2347
bool isNegatedConsteval() const
Definition Stmt.h:2459
Stmt * getElse()
Definition Stmt.h:2368
DeclStmt * getConditionVariableDeclStmt()
If this IfStmt has a condition variable, return the faux DeclStmt associated with the creation of tha...
Definition Stmt.h:2403
VarDecl * getConditionVariable()
Retrieve the variable declared in this "if" statement, if any.
Definition Stmt.cpp:1068
ImaginaryLiteral - We support imaginary integer and floating point literals, like "1....
Definition Expr.h:1776
const Expr * getSubExpr() const
Definition Expr.h:1788
Represents an implicitly-generated value initialization of an object of a given type.
Definition Expr.h:6143
Represents a field injected from an anonymous union/struct into the parent scope.
Definition Decl.h:3648
Describes an C or C++ initializer list.
Definition Expr.h:5377
Expr * getArrayFiller()
If this initializer list initializes an array with more elements than there are initializers in the l...
Definition Expr.h:5491
ArrayRef< Expr * > inits() const
Definition Expr.h:5441
A C++ lambda expression, which produces a function object (of unspecified type) that can be invoked l...
Definition ExprCXX.h:1973
capture_init_iterator capture_init_begin()
Retrieve the first initialization argument for this lambda expression (which initializes the first ca...
Definition ExprCXX.h:2099
CXXRecordDecl * getLambdaClass() const
Retrieve the class that corresponds to the lambda.
Definition ExprCXX.cpp:1432
Keeps track of the various options that can be enabled, which controls the dialect of C or C++ that i...
bool isCompatibleWith(ClangABI Version) const
Implicit declaration of a temporary that was materialized by a MaterializeTemporaryExpr and lifetime-...
Definition DeclCXX.h:3355
bool isNamed() const
Definition Stmt.h:3101
const Stmt * getNamedLoopOrSwitch() const
If this is a named break/continue, get the loop or switch statement that this targets.
Definition Stmt.cpp:1535
A global _GUID constant.
Definition DeclCXX.h:4468
APValue & getAsAPValue() const
Get the value of this MSGuidDecl as an APValue.
Definition DeclCXX.cpp:3931
Represents a prvalue temporary that is written into memory so that a reference can bind to it.
Definition ExprCXX.h:4974
StorageDuration getStorageDuration() const
Retrieve the storage duration for the materialized temporary.
Definition ExprCXX.h:4999
Expr * getSubExpr() const
Retrieve the temporary-generating subexpression whose value will be materialized into a glvalue.
Definition ExprCXX.h:4991
ValueDecl * getExtendingDecl()
Get the declaration which triggered the lifetime-extension of this temporary, if any.
Definition ExprCXX.h:5024
LifetimeExtendedTemporaryDecl * getLifetimeExtendedTemporaryDecl()
Definition ExprCXX.h:5014
MemberExpr - [C99 6.5.2.3] Structure and Union Members.
Definition Expr.h:3433
ValueDecl * getMemberDecl() const
Retrieve the member declaration to which this expression refers.
Definition Expr.h:3516
Expr * getBase() const
Definition Expr.h:3510
A pointer to member type per C++ 8.3.3 - Pointers to members.
Definition TypeBase.h:3751
This represents a decl that may have a name.
Definition Decl.h:276
DeclarationName getDeclName() const
Get the actual, stored name of the declaration, which may be a special name.
Definition Decl.h:342
Represents a C++ namespace alias.
Definition DeclCXX.h:3248
ObjCArrayLiteral - used for objective-c array containers; as in: @["Hello", NSApp,...
Definition ExprObjC.h:219
ObjCBoolLiteralExpr - Objective-C Boolean Literal.
Definition ExprObjC.h:118
ObjCBoxedExpr - used for generalized expression boxing.
Definition ExprObjC.h:158
ObjCDictionaryLiteral - AST node to represent objective-c dictionary literals; as in:"name" : NSUserN...
Definition ExprObjC.h:341
ObjCEncodeExpr, used for @encode in Objective-C.
Definition ExprObjC.h:440
QualType getEncodedType() const
Definition ExprObjC.h:459
SourceLocation getAtLoc() const
Definition ExprObjC.h:454
bool isExpressibleAsConstantInitializer() const
Definition ExprObjC.h:67
ObjCStringLiteral, used for Objective-C string literals i.e.
Definition ExprObjC.h:83
OffsetOfExpr - [C99 7.17] - This represents an expression of the form offsetof(record-type,...
Definition Expr.h:2596
Expr * getIndexExpr(unsigned Idx)
Definition Expr.h:2655
const OffsetOfNode & getComponent(unsigned Idx) const
Definition Expr.h:2643
unsigned getNumComponents() const
Definition Expr.h:2651
Helper class for OffsetOfExpr.
Definition Expr.h:2490
unsigned getArrayExprIndex() const
For an array element node, returns the index into the array of expressions.
Definition Expr.h:2548
@ Array
An index into an array.
Definition Expr.h:2495
Kind getKind() const
Determine what kind of offsetof node this is.
Definition Expr.h:2544
OpaqueValueExpr - An expression referring to an opaque object of a fixed type and value class.
Definition Expr.h:1223
Expr * getSourceExpr() const
The source expression of an opaque value expression is the expression which originally generated the ...
Definition Expr.h:1273
Expr * getSelectedExpr() const
Definition ExprCXX.h:4693
ParenExpr - This represents a parenthesized expression, e.g.
Definition Expr.h:2251
const Expr * getSubExpr() const
Definition Expr.h:2268
Represents a parameter to a function.
Definition Decl.h:1832
PointerType - C99 6.7.5.1 - Pointer Declarators.
Definition TypeBase.h:3403
QualType getPointeeType() const
Definition TypeBase.h:3413
[C99 6.4.2.2] - A predefined identifier such as func.
Definition Expr.h:2074
StringLiteral * getFunctionName()
Definition Expr.h:2118
PseudoObjectExpr - An expression which accesses a pseudo-object l-value.
Definition Expr.h:6890
Expr * getResultExpr()
Return the result-bearing expression, or null if there is none.
Definition Expr.h:6938
ArrayRef< Expr * > semantics()
Definition Expr.h:6962
Expr * getSyntacticForm()
Return the syntactic form of this expression, i.e.
Definition Expr.h:6927
A (possibly-)qualified type.
Definition TypeBase.h:938
bool isVolatileQualified() const
Determine whether this type is volatile-qualified.
Definition TypeBase.h:8662
QualType withConst() const
Definition TypeBase.h:1175
bool isNull() const
Return true if this QualType doesn't point to a type yet.
Definition TypeBase.h:1005
const Type * getTypePtr() const
Retrieves a pointer to the underlying (unqualified) type.
Definition TypeBase.h:8578
bool isConstant(const ASTContext &Ctx) const
Definition TypeBase.h:1098
bool isConstQualified() const
Determine whether this type is const-qualified.
Definition TypeBase.h:8651
Represents a struct/union/class.
Definition Decl.h:4506
Frontend produces RecoveryExprs on semantic errors that prevent creating other well-formed expression...
Definition Expr.h:7589
Base for LValueReferenceType and RValueReferenceType.
Definition TypeBase.h:3678
C++2a [expr.prim.req]: A requires-expression provides a concise way to express requirements on templa...
bool isSatisfied() const
Whether or not the requires clause is satisfied.
ReturnStmt - This represents a return, optionally of an expression: return; return 4;.
Definition Stmt.h:3173
Expr * getRetValue()
Definition Stmt.h:3200
SourceLocation getLocation() const
Definition Expr.h:2224
std::string ComputeName(ASTContext &Context) const
Definition Expr.cpp:597
Scope - A scope is a transient data structure that is used while parsing the program.
Definition Scope.h:41
ShuffleVectorExpr - clang-specific builtin-in function __builtin_shufflevector.
Definition Expr.h:4712
llvm::APSInt getShuffleMaskIdx(unsigned N) const
Definition Expr.h:4764
unsigned getNumSubExprs() const
getNumSubExprs - Return the size of the SubExprs array.
Definition Expr.h:4745
Expr * getExpr(unsigned Index)
getExpr - Return the Expr at the specified index.
Definition Expr.h:4751
Represents an expression that computes the length of a parameter pack.
Definition ExprCXX.h:4495
unsigned getPackLength() const
Retrieve the length of the parameter pack.
Definition ExprCXX.h:4569
Represents a function call to one of __builtin_LINE(), __builtin_COLUMN(), __builtin_FUNCTION(),...
Definition Expr.h:5095
APValue EvaluateInContext(const ASTContext &Ctx, const Expr *DefaultExpr) const
Return the result of evaluating this SourceLocExpr in the specified (and possibly null) default argum...
Definition Expr.cpp:2319
Represents a C++11 static_assert declaration.
Definition DeclCXX.h:4183
StmtExpr - This is the GNU Statement Expression extension: ({int X=4; X;}).
Definition Expr.h:4664
CompoundStmt * getSubStmt()
Definition Expr.h:4681
Stmt - This represents one statement.
Definition Stmt.h:85
StmtClass getStmtClass() const
Definition Stmt.h:1504
StringLiteral - This represents a string literal expression, e.g.
Definition Expr.h:1844
unsigned getLength() const
Definition Expr.h:1969
uint32_t getCodeUnit(size_t I) const
Return the code unit at the given position.
Definition Expr.h:1931
static StringLiteral * Create(const ASTContext &Ctx, StringRef Str, StringLiteralKind Kind, bool Pascal, QualType Ty, ArrayRef< SourceLocation > Locs)
This is the "fully general" constructor that allows representation of strings formed from one or more...
Definition Expr.cpp:1198
unsigned getCharByteWidth() const
Definition Expr.h:1971
Represents a reference to a non-type template parameter that has been substituted with a template arg...
Definition ExprCXX.h:4718
const SwitchCase * getNextSwitchCase() const
Definition Stmt.h:1904
SwitchStmt - This represents a 'switch' stmt.
Definition Stmt.h:2520
Expr * getCond()
Definition Stmt.h:2583
Stmt * getBody()
Definition Stmt.h:2595
VarDecl * getConditionVariable()
Retrieve the variable declared in this "switch" statement, if any.
Definition Stmt.cpp:1186
Stmt * getInit()
Definition Stmt.h:2600
SwitchCase * getSwitchCaseList()
Definition Stmt.h:2651
DeclStmt * getConditionVariableDeclStmt()
If this SwitchStmt has a condition variable, return the faux DeclStmt associated with the creation of...
Definition Stmt.h:2634
Represents the declaration of a struct/union/class/enum.
Definition Decl.h:3898
bool isCompleteDefinition() const
Return true if this decl has its body fully specified.
Definition Decl.h:3999
bool isUnion() const
Definition Decl.h:4109
A template argument list.
ArrayRef< TemplateArgument > asArray() const
Produce this as an array ref.
Symbolic representation of typeid(T) for some type T.
Definition APValue.h:48
A type trait used in the implementation of various C++11 and Library TR1 trait templates.
Definition ExprCXX.h:2901
bool getBoolValue() const
Definition ExprCXX.h:2962
bool isStoredAsComparisonResult() const
Definition ExprCXX.h:2958
const APValue & getAPValue() const
Definition ExprCXX.h:2967
bool isStoredAsBoolean() const
Definition ExprCXX.h:2954
The base class of the type hierarchy.
Definition TypeBase.h:1879
bool isVoidType() const
Definition TypeBase.h:9200
bool isBooleanType() const
Definition TypeBase.h:9341
bool isLiteralType(const ASTContext &Ctx) const
Return true if this is a literal type (C++11 [basic.types]p10)
Definition Type.cpp:3241
bool isIncompleteArrayType() const
Definition TypeBase.h:8922
CXXRecordDecl * getAsCXXRecordDecl() const
Retrieves the CXXRecordDecl that this type refers to, either because the type is a RecordType or beca...
Definition Type.h:26
bool isNothrowT() const
Definition Type.cpp:3432
RecordDecl * getAsRecordDecl() const
Retrieves the RecordDecl this type refers to.
Definition Type.h:41
bool isVoidPointerType() const
Definition Type.cpp:845
bool isConstantSizeType() const
Return true if this is not a variable sized type, according to the rules of C99 6....
Definition Type.cpp:2647
bool isArrayType() const
Definition TypeBase.h:8914
bool isFunctionPointerType() const
Definition TypeBase.h:8882
bool isConstantMatrixType() const
Definition TypeBase.h:8982
bool isPointerType() const
Definition TypeBase.h:8815
bool isIntegerType() const
isIntegerType() does not include complex integers (a GCC extension).
Definition TypeBase.h:9248
const T * castAs() const
Member-template castAs<specific type>.
Definition TypeBase.h:9498
bool isReferenceType() const
Definition TypeBase.h:8839
bool isEnumeralType() const
Definition TypeBase.h:8946
QualType getPointeeType() const
If this is a pointer, ObjC object pointer, or block pointer, this returns the respective pointee.
Definition Type.cpp:885
bool isIntegralOrEnumerationType() const
Determine whether this type is an integral or enumeration type.
Definition TypeBase.h:9326
bool isSpecificBuiltinType(unsigned K) const
Test for a particular builtin type.
Definition TypeBase.h:9169
bool isDependentType() const
Whether this type is a dependent type, meaning that its definition somehow depends on a template para...
Definition TypeBase.h:2863
bool isAnyComplexType() const
Definition TypeBase.h:8950
bool isFixedPointType() const
Return true if this is a fixed point type according to ISO/IEC JTC1 SC22 WG14 N1169.
Definition TypeBase.h:9264
const Type * getBaseElementTypeUnsafe() const
Get the base element type of this type, potentially discarding type qualifiers.
Definition TypeBase.h:9384
bool isMemberPointerType() const
Definition TypeBase.h:8896
bool isAtomicType() const
Definition TypeBase.h:9007
EnumDecl * castAsEnumDecl() const
Definition Type.h:59
bool isStdByteType() const
Definition Type.cpp:3451
const ArrayType * getAsArrayTypeUnsafe() const
A variant of getAs<> for array types which silently discards qualifiers from the outermost type.
Definition TypeBase.h:9484
bool isPointerOrReferenceType() const
Definition TypeBase.h:8819
bool isFunctionType() const
Definition TypeBase.h:8811
bool isVectorType() const
Definition TypeBase.h:8954
bool isRealFloatingType() const
Floating point categories.
Definition Type.cpp:2535
const T * getAsCanonical() const
If this type is canonically the specified type, return its canonical type cast to that specified type...
Definition TypeBase.h:3002
bool isFloatingType() const
Definition Type.cpp:2519
const T * getAs() const
Member-template getAs<specific type>'.
Definition TypeBase.h:9431
bool isRecordType() const
Definition TypeBase.h:8942
bool isSizelessVectorType() const
Returns true for all scalable vector types.
Definition Type.cpp:2793
bool hasBooleanRepresentation() const
Determine whether this type has a boolean representation – i.e., it is a boolean type,...
Definition Type.cpp:2574
Base class for declarations which introduce a typedef-name.
Definition Decl.h:3743
UnaryExprOrTypeTraitExpr - expression with either a type or (unevaluated) expression operand.
Definition Expr.h:2694
QualType getArgumentType() const
Definition Expr.h:2737
QualType getTypeOfArgument() const
Gets the argument type, or the type of the argument expression, whichever is appropriate.
Definition Expr.h:2763
UnaryExprOrTypeTrait getKind() const
Definition Expr.h:2726
UnaryOperator - This represents the unary-expression's (except sizeof and alignof),...
Definition Expr.h:2313
Expr * getSubExpr() const
Definition Expr.h:2354
Opcode getOpcode() const
Definition Expr.h:2349
bool canOverflow() const
Returns true if the unary operator can cause an overflow.
Definition Expr.h:2367
Represents C++ using-directive.
Definition DeclCXX.h:3143
Represent the declaration of a variable (in which case it is an lvalue) a function (in which case it ...
Definition Decl.h:714
QualType getType() const
Definition Decl.h:725
bool isWeak() const
Determine whether this symbol is weakly-imported, or declared with the weak or weak-ref attr.
Definition Decl.cpp:5689
QualType getType() const
Definition Value.cpp:238
Represents a variable declaration or definition.
Definition Decl.h:934
bool isConstexpr() const
Whether this variable is (C++11) constexpr.
Definition Decl.h:1606
bool isInitCapture() const
Whether this variable is the implicit variable for a lambda init-capture.
Definition Decl.h:1615
bool isStaticDataMember() const
Determines whether this is a static data member.
Definition Decl.h:1308
bool hasGlobalStorage() const
Returns true for all variables that do not have local storage.
Definition Decl.h:1249
bool hasConstantInitialization() const
Determine whether this variable has constant initialization.
Definition Decl.cpp:2661
bool isStaticLocal() const
Returns true if a variable with function scope is a static local variable.
Definition Decl.h:1216
const Expr * getInit() const
Definition Decl.h:1393
const APValue * evaluateValue() const
Attempt to evaluate the value of the initializer attached to this declaration, and produce notes expl...
Definition Decl.cpp:2568
bool isLocalVarDecl() const
Returns true for local variable declarations other than parameters.
Definition Decl.h:1276
const Expr * getAnyInitializer() const
Get the initializer for this variable, no matter which declaration it is attached to.
Definition Decl.h:1383
Represents a GCC generic vector type.
Definition TypeBase.h:4398
unsigned getNumElements() const
Definition TypeBase.h:4413
QualType getElementType() const
Definition TypeBase.h:4412
WhileStmt - This represents a 'while' stmt.
Definition Stmt.h:2708
Expr * getCond()
Definition Stmt.h:2760
DeclStmt * getConditionVariableDeclStmt()
If this WhileStmt has a condition variable, return the faux DeclStmt associated with the creation of ...
Definition Stmt.h:2796
VarDecl * getConditionVariable()
Retrieve the variable declared in this "while" statement, if any.
Definition Stmt.cpp:1247
Stmt * getBody()
Definition Stmt.h:2772
ArrayIndexScope(Compiler< Emitter > *Ctx, uint64_t Index)
Definition Compiler.cpp:216
A memory block, either on the stack or in the heap.
Definition Block.h:68
void invokeDtor()
Invokes the Destructor.
Definition Block.h:181
Compilation context for expressions.
Definition Compiler.h:116
llvm::SmallVector< InitLink > InitStack
Definition Compiler.h:503
bool VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E)
bool VisitCXXDeleteExpr(const CXXDeleteExpr *E)
bool VisitOffsetOfExpr(const OffsetOfExpr *E)
bool visitContinueStmt(const ContinueStmt *S)
bool VisitCharacterLiteral(const CharacterLiteral *E)
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init, OptPrimType InitT)
Pointer to the array(not the element!) must be on the stack when calling this.
bool VisitCXXParenListInitExpr(const CXXParenListInitExpr *E)
bool VisitConceptSpecializationExpr(const ConceptSpecializationExpr *E)
bool visitInitializerPop(const Expr *E)
Similar, but will also pop the pointer.
bool VisitCompoundLiteralExpr(const CompoundLiteralExpr *E)
bool visitBool(const Expr *E)
Visits an expression and converts it to a boolean.
bool VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E)
PrimType classifyPrim(QualType Ty) const
Classifies a known primitive type.
Definition Compiler.h:297
bool VisitTypeTraitExpr(const TypeTraitExpr *E)
bool VisitLambdaExpr(const LambdaExpr *E)
bool VisitMemberExpr(const MemberExpr *E)
llvm::DenseMap< const OpaqueValueExpr *, unsigned > OpaqueExprs
OpaqueValueExpr to location mapping.
Definition Compiler.h:478
bool VisitBinaryOperator(const BinaryOperator *E)
bool visitCXXExpansionStmtInstantiation(const CXXExpansionStmtInstantiation *S)
template for (auto x : {1, 2}) {}
bool visitAttributedStmt(const AttributedStmt *S)
bool VisitPackIndexingExpr(const PackIndexingExpr *E)
bool VisitArraySubscriptExpr(const ArraySubscriptExpr *E)
bool VisitCallExpr(const CallExpr *E)
std::optional< uint64_t > ArrayIndex
Current argument index. Needed to emit ArrayInitIndexExpr.
Definition Compiler.h:484
bool VisitPseudoObjectExpr(const PseudoObjectExpr *E)
bool VisitCXXReinterpretCastExpr(const CXXReinterpretCastExpr *E)
bool visitAPValueInitializer(const APValue &Val, SourceInfo Info, QualType T, bool IsCompleteClass=true)
const Function * getFunction(const FunctionDecl *FD)
Returns a function for the given FunctionDecl.
bool VisitFixedPointBinOp(const BinaryOperator *E)
bool VisitCastExpr(const CastExpr *E)
Definition Compiler.cpp:447
bool VisitObjCEncodeExpr(const ObjCEncodeExpr *E)
bool VisitFixedPointUnaryOperator(const UnaryOperator *E)
bool VisitComplexUnaryOperator(const UnaryOperator *E)
llvm::DenseMap< const SwitchCase *, LabelTy > CaseMap
Definition Compiler.h:122
bool VisitBlockExpr(const BlockExpr *E)
bool VisitCXXScalarValueInitExpr(const CXXScalarValueInitExpr *E)
bool VisitLogicalBinOp(const BinaryOperator *E)
bool visitCompoundStmt(const CompoundStmt *S)
Context & Ctx
Current compilation context.
Definition Compiler.h:139
const VarDecl * InitializingDecl
Definition Compiler.h:501
bool visitDeclRef(const ValueDecl *D, const Expr *E)
Visit the given decl as if we have a reference to it.
bool visitBreakStmt(const BreakStmt *S)
bool visitExpr(const Expr *E, bool DestroyToplevelScope) override
bool visitForStmt(const ForStmt *S)
bool VisitDeclRefExpr(const DeclRefExpr *E)
bool VisitOpaqueValueExpr(const OpaqueValueExpr *E)
bool VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E)
bool visitAPValue(const APValue &Val, PrimType ValType, SourceInfo Info)
Visit an APValue.
bool VisitStmtExpr(const StmtExpr *E)
bool VisitBuiltinBitCastExpr(const BuiltinBitCastExpr *E)
bool VisitFixedPointLiteral(const FixedPointLiteral *E)
const FunctionDecl * CompilingFunction
Definition Compiler.h:514
bool VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E)
VarCreationState visitVarDecl(const VarDecl *VD, const Expr *Init, bool Toplevel=false)
Creates and initializes a variable from the given decl.
VariableScope< Emitter > * VarScope
Current scope.
Definition Compiler.h:481
bool visitDeclAndReturn(const VarDecl *VD, const Expr *Init, bool ConstantContext) override
Toplevel visitDeclAndReturn().
bool VisitCXXNewExpr(const CXXNewExpr *E)
bool VisitCompoundAssignOperator(const CompoundAssignOperator *E)
bool visit(const Expr *E) override
Evaluates an expression and places the result on the stack.
bool delegate(const Expr *E)
Just pass evaluation on to E.
bool visitLValueExpr(const Expr *E, bool DestroyToplevelScope) override
bool discard(const Expr *E)
Evaluates an expression for side effects and discards the result.
bool VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E)
CaseMap CaseLabels
Switch case mapping.
Definition Compiler.h:510
Record * getRecord(QualType Ty)
Returns a record from a record or pointer type.
bool VisitCXXReflectExpr(const CXXReflectExpr *E)
const RecordType * getRecordTy(QualType Ty)
Returns a record type from a record or pointer type.
bool VisitCXXStdInitializerListExpr(const CXXStdInitializerListExpr *E)
bool visitInitList(ArrayRef< const Expr * > Inits, const Expr *ArrayFiller, const Expr *E)
bool VisitSizeOfPackExpr(const SizeOfPackExpr *E)
bool VisitPredefinedExpr(const PredefinedExpr *E)
bool VisitSourceLocExpr(const SourceLocExpr *E)
bool visitDeclStmt(const DeclStmt *DS, bool EvaluateConditionDecl=false)
bool registerRedecl(const VarDecl *VD, const APValue &V)
bool emitCleanup()
Emits scope cleanup instructions.
bool VisitExtVectorElementExpr(const ExtVectorElementExpr *E)
bool VisitObjCStringLiteral(const ObjCStringLiteral *E)
bool VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E)
bool visitInitializer(const Expr *E)
Compiles an initializer.
bool visitDtorCall(const VarDecl *VD, const APValue &Value) override
const Expr * SourceLocDefaultExpr
DefaultInit- or DefaultArgExpr, needed for SourceLocExpr.
Definition Compiler.h:487
bool VisitObjCArrayLiteral(const ObjCArrayLiteral *E)
UnsignedOrNone OptLabelTy
Definition Compiler.h:121
bool VisitCXXBindTemporaryExpr(const CXXBindTemporaryExpr *E)
bool VisitPointerArithBinOp(const BinaryOperator *E)
Perform addition/subtraction of a pointer and an integer or subtraction of two pointers.
bool visitCallArgs(ArrayRef< const Expr * > Args, const FunctionDecl *FuncDecl, bool Activate, bool IsOperatorCall)
bool VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E)
bool visitDefaultStmt(const DefaultStmt *S)
bool VisitObjCDictionaryLiteral(const ObjCDictionaryLiteral *E)
bool visitWithSubstitutions(const FunctionDecl *Callee, ArrayRef< const Expr * > Args, const Expr *This, const Expr *Condition) override
Evaluate the Condition as if it was in the body of Callee.
typename Emitter::LabelTy LabelTy
Definition Compiler.h:119
VarCreationState visitDecl(const VarDecl *VD)
bool VisitCXXDynamicCastExpr(const CXXDynamicCastExpr *E)
bool visitStmt(const Stmt *S)
bool VisitExpressionTraitExpr(const ExpressionTraitExpr *E)
bool VisitVectorUnaryOperator(const UnaryOperator *E)
bool VisitCXXConstructExpr(const CXXConstructExpr *E)
bool VisitCXXNullPtrLiteralExpr(const CXXNullPtrLiteralExpr *E)
bool VisitObjCBoxedExpr(const ObjCBoxedExpr *E)
bool VisitDesignatedInitUpdateExpr(const DesignatedInitUpdateExpr *E)
bool VisitCXXInheritedCtorInitExpr(const CXXInheritedCtorInitExpr *E)
bool VisitRecoveryExpr(const RecoveryExpr *E)
bool VisitRequiresExpr(const RequiresExpr *E)
bool Initializing
Flag inidicating if we're initializing an already created variable.
Definition Compiler.h:500
bool visitReturnStmt(const ReturnStmt *RS)
bool VisitCXXThrowExpr(const CXXThrowExpr *E)
bool VisitSubstNonTypeTemplateParmExpr(const SubstNonTypeTemplateParmExpr *E)
bool VisitChooseExpr(const ChooseExpr *E)
bool visitFunc(const FunctionDecl *F) override
bool visitCXXForRangeStmt(const CXXForRangeStmt *S)
bool visitCaseStmt(const CaseStmt *S)
bool VisitComplexBinOp(const BinaryOperator *E)
llvm::DenseMap< const ValueDecl *, Scope::Local > Locals
Variable to storage mapping.
Definition Compiler.h:475
bool VisitAbstractConditionalOperator(const AbstractConditionalOperator *E)
bool VisitCXXTypeidExpr(const CXXTypeidExpr *E)
UnsignedOrNone allocateTemporary(const Expr *E)
bool VisitBuiltinCallExpr(const CallExpr *E, unsigned BuiltinID)
bool VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E)
bool VisitCXXRewrittenBinaryOperator(const CXXRewrittenBinaryOperator *E)
OptPrimType ReturnType
Type of the expression returned by the function.
Definition Compiler.h:507
bool VisitUnaryOperator(const UnaryOperator *E)
bool VisitFloatCompoundAssignOperator(const CompoundAssignOperator *E)
OptPrimType classify(const Expr *E) const
Definition Compiler.h:291
llvm::SmallVector< LabelInfo > LabelInfoStack
Stack of label information for loops and switch statements.
Definition Compiler.h:512
bool VisitGenericSelectionExpr(const GenericSelectionExpr *E)
bool visitDoStmt(const DoStmt *S)
bool VisitIntegerLiteral(const IntegerLiteral *E)
bool VisitInitListExpr(const InitListExpr *E)
bool VisitVectorBinOp(const BinaryOperator *E)
bool VisitStringLiteral(const StringLiteral *E)
bool VisitParenExpr(const ParenExpr *E)
bool VisitCXXNoexceptExpr(const CXXNoexceptExpr *E)
bool VisitShuffleVectorExpr(const ShuffleVectorExpr *E)
bool VisitPointerCompoundAssignOperator(const CompoundAssignOperator *E)
bool DiscardResult
Flag indicating if return value is to be discarded.
Definition Compiler.h:490
bool VisitEmbedExpr(const EmbedExpr *E)
UnsignedOrNone allocateLocal(DeclOrExpr Decl, QualType Ty=QualType(), ScopeKind=ScopeKind::Block)
Allocates a space storing a local given its type.
bool VisitConvertVectorExpr(const ConvertVectorExpr *E)
bool VisitCXXThisExpr(const CXXThisExpr *E)
bool VisitConstantExpr(const ConstantExpr *E)
bool VisitUnaryExprOrTypeTraitExpr(const UnaryExprOrTypeTraitExpr *E)
bool visitSwitchStmt(const SwitchStmt *S)
bool VisitCXXUuidofExpr(const CXXUuidofExpr *E)
bool VisitExprWithCleanups(const ExprWithCleanups *E)
bool visitAsLValue(const Expr *E)
unsigned allocateLocalPrimitive(DeclOrExpr Decl, PrimType Ty, bool IsConst, bool IsVolatile=false, ScopeKind SC=ScopeKind::Block)
Creates a local primitive value.
bool visitWhileStmt(const WhileStmt *S)
bool visitIfStmt(const IfStmt *IS)
bool VisitAddrLabelExpr(const AddrLabelExpr *E)
bool canClassify(const Expr *E) const
Definition Compiler.h:293
bool VisitFloatingLiteral(const FloatingLiteral *E)
Program & P
Program to link to.
Definition Compiler.h:141
bool VisitMaterializeTemporaryExpr(const MaterializeTemporaryExpr *E)
bool VisitGNUNullExpr(const GNUNullExpr *E)
bool VisitImaginaryLiteral(const ImaginaryLiteral *E)
bool VisitSYCLUniqueStableNameExpr(const SYCLUniqueStableNameExpr *E)
bool visitCXXTryStmt(const CXXTryStmt *S)
static bool isUnevaluatedBuiltin(unsigned ID)
Unevaluated builtins don't get their arguments put on the stack automatically.
Definition Context.cpp:843
static bool shouldBeGloballyIndexed(const ValueDecl *VD)
Returns whether we should create a global variable for the given ValueDecl.
Definition Context.h:175
Scope used to handle temporaries in toplevel variable declarations.
Definition Compiler.cpp:285
DeclScope(Compiler< Emitter > *Ctx, const VarDecl *VD)
Definition Compiler.cpp:287
Wrapper around fixed point types.
Definition FixedPoint.h:23
static FixedPoint zero(llvm::FixedPointSemantics Sem)
Definition FixedPoint.h:36
If a Floating is constructed from Memory, it DOES NOT OWN THAT MEMORY.
Definition Floating.h:35
bool singleWord() const
Definition Floating.h:107
Bytecode function.
Definition Function.h:98
bool hasThisPointer() const
Definition Function.h:230
bool hasRVO() const
Checks if the first argument is a RVO pointer.
Definition Function.h:160
InitLinkScope(Compiler< Emitter > *Ctx, InitLink &&Link)
Definition Compiler.cpp:251
Compiler< Emitter > * Ctx
Definition Compiler.cpp:258
InitStackScope(Compiler< Emitter > *Ctx, bool Active)
Definition Compiler.cpp:263
When generating code for e.g.
Definition Compiler.cpp:422
LocOverrideScope(Compiler< Emitter > *Ctx, SourceInfo NewValue, bool Enabled=true)
Definition Compiler.cpp:424
Generic scope for local variables.
Definition Compiler.cpp:121
UnsignedOrNone Idx
Index of the scope in the chain.
Definition Compiler.cpp:210
~LocalScope() override
Emit a Destroy op for this scope.
Definition Compiler.cpp:127
bool destroyLocals(const Expr *E=nullptr) override
Explicit destruction of local variables.
Definition Compiler.cpp:134
bool emitDestructors(const Expr *E=nullptr) override
Definition Compiler.cpp:156
void removeIfStoredOpaqueValue(const Scope::Local &Local)
Definition Compiler.cpp:202
void addLocal(Scope::Local Local) override
Definition Compiler.cpp:146
LocalScope(Compiler< Emitter > *Ctx, ScopeKind Kind=ScopeKind::Block)
Definition Compiler.cpp:123
Sets the context for break/continue statements.
Definition Compiler.cpp:361
typename Compiler< Emitter >::LabelTy LabelTy
Definition Compiler.cpp:363
typename Compiler< Emitter >::OptLabelTy OptLabelTy
Definition Compiler.cpp:364
typename Compiler< Emitter >::LabelInfo LabelInfo
Definition Compiler.cpp:365
LoopScope(Compiler< Emitter > *Ctx, const Stmt *Name, LabelTy BreakLabel, LabelTy ContinueLabel)
Definition Compiler.cpp:367
PrimType value_or(PrimType PT) const
Definition PrimType.h:90
Scope used to handle initialization methods.
Definition Compiler.cpp:305
OptionScope(Compiler< Emitter > *Ctx, bool NewDiscardResult, bool NewInitializing, bool NewToLValue)
Root constructor, compiling or discarding primitives.
Definition Compiler.cpp:308
Context to manage declaration lifetimes.
Definition Program.h:140
Structure/Class descriptor.
Definition Record.h:27
bool isUnion() const
Checks if the record is a union.
Definition Record.h:71
const Field * getField(unsigned I) const
Definition Record.h:100
const Base * getBaseOrNull(const RecordDecl *RD) const
Definition Record.cpp:56
bool hasTrivialDtor() const
Returns true for anonymous unions and records with no destructor or for those with a trivial destruct...
Definition Record.cpp:34
const Base * findVirtualBase(const RecordDecl *RD) const
Returns a virtual base descriptor.
Definition Record.cpp:75
Describes a scope block.
Definition Function.h:35
Describes the statement/declaration an opcode was generated from.
Definition Source.h:77
const Expr * asExpr() const
Definition Source.h:92
SourceLocScope(Compiler< Emitter > *Ctx, const Expr *DefaultExpr)
Definition Compiler.cpp:230
typename Compiler< Emitter >::LabelTy LabelTy
Definition Compiler.cpp:389
typename Compiler< Emitter >::OptLabelTy OptLabelTy
Definition Compiler.cpp:390
typename Compiler< Emitter >::LabelInfo LabelInfo
Definition Compiler.cpp:392
typename Compiler< Emitter >::CaseMap CaseMap
Definition Compiler.cpp:391
SwitchScope(Compiler< Emitter > *Ctx, const Stmt *Name, CaseMap &&CaseLabels, LabelTy BreakLabel, OptLabelTy DefaultLabel)
Definition Compiler.cpp:394
Scope chain managing the variable lifetimes.
Definition Compiler.cpp:64
void addForScopeKind(const Scope::Local &Local, ScopeKind Kind)
Like addExtended, but adds to the nearest scope of the given kind.
Definition Compiler.cpp:79
bool LocalsAlwaysEnabled
Whether locals added to this scope are enabled by default.
Definition Compiler.cpp:110
Compiler< Emitter > * Ctx
Compiler instance.
Definition Compiler.cpp:114
virtual bool emitDestructors(const Expr *E=nullptr)
Definition Compiler.cpp:102
VariableScope(Compiler< Emitter > *Ctx, ScopeKind Kind=ScopeKind::Block)
Definition Compiler.cpp:66
virtual bool destroyLocals(const Expr *E=nullptr)
Definition Compiler.cpp:103
virtual void addLocal(Scope::Local Local)
Definition Compiler.cpp:75
VariableScope * Parent
Link to the parent scope.
Definition Compiler.cpp:116
ScopeKind getKind() const
Definition Compiler.cpp:105
VariableScope * getParent() const
Definition Compiler.cpp:104
uint16_t getPointerAuthTypeDiscriminator(CodeGenModule &CGM, QualType FunctionType)
Return a type discriminator for the given function type.
bool Sub(InterpState &S, CodePtr OpPC)
Definition Interp.h:426
bool LT(InterpState &S, CodePtr OpPC)
Definition Interp.h:1505
static llvm::RoundingMode getRoundingMode(FPOptions FPO)
constexpr bool isSignedType(PrimType T)
Definition PrimType.h:61
bool Div(InterpState &S, CodePtr OpPC)
1) Pops the RHS from the stack.
Definition Interp.h:771
constexpr bool isPtrType(PrimType T)
Definition PrimType.h:57
constexpr size_t align(size_t Size)
Aligns a size to the pointer alignment.
Definition PrimType.h:218
bool This(InterpState &S, CodePtr OpPC)
Definition Interp.h:3186
constexpr bool isIntegerOrBoolType(PrimType T)
Definition PrimType.h:54
llvm::APFloat APFloat
Definition Floating.h:27
static void discard(InterpStack &Stk, PrimType T)
static bool isSideEffectFree(const Expr *E)
Check if E has side-effects.
Definition Compiler.cpp:44
llvm::APInt APInt
Definition FixedPoint.h:19
bool LE(InterpState &S, CodePtr OpPC)
Definition Interp.h:1512
PrimType
Enumeration of the primitive types of the VM.
Definition PrimType.h:35
static std::optional< bool > getBoolValue(const Expr *E)
Definition Compiler.cpp:31
static bool Activate(InterpState &S)
Definition Interp.h:2276
static bool exceedsArraySizeLimit(const LangOptions &LangOpts, uint64_t NumElems)
Whether the CheckArraySize op rejects an array with NumElems elements.
Definition Compiler.cpp:55
bool Init(InterpState &S, CodePtr OpPC)
Definition Interp.h:2381
bool DefaultInit(InterpState &S, CodePtr OpPC, const CXXConstructorDecl *Ctor)
Definition Interp.cpp:2794
bool Mul(InterpState &S, CodePtr OpPC)
Definition Interp.h:480
size_t primSize(PrimType Type)
Returns the size of a primitive type in bytes.
Definition PrimType.cpp:25
bool Inc(InterpState &S, CodePtr OpPC, bool CanOverflow)
1) Pops a pointer from the stack 2) Load the value from the pointer 3) Writes the value increased by ...
Definition Interp.h:960
bool Add(InterpState &S, CodePtr OpPC)
Definition Interp.h:397
llvm::BitVector collectNonNullArgs(const FunctionDecl *F, ArrayRef< const Expr * > Args)
constexpr bool isIntegerType(PrimType T)
Definition PrimType.h:55
llvm::APSInt APSInt
Definition FixedPoint.h:20
Top level wrappers for InstallAPI frontend operations.
bool isa(CodeGen::Address addr)
Definition Address.h:330
bool hasSpecificAttr(const Container &container)
@ Success
Annotation was successful.
Definition Parser.h:65
@ Link
'link' clause, allowed on 'declare' construct.
DynamicRecursiveASTVisitorBase< true > ConstDynamicRecursiveASTVisitor
ComparisonCategoryResult
An enumeration representing the possible results of a three-way comparison.
@ SD_Static
Static storage duration.
Definition Specifiers.h:345
@ SD_FullExpression
Full-expression storage duration (for temporaries).
Definition Specifiers.h:342
@ Result
The result type of a method or function.
Definition TypeBase.h:906
OptionalUnsigned< unsigned > UnsignedOrNone
const FunctionProtoType * T
U cast(CodeGen::Address addr)
Definition Address.h:327
int const char * function
Definition c++config.h:31
__builtin_elementwise_add_sat __builtin_elementwise_sub_sat __builtin_riscv_ssha_i32 __builtin_riscv_sshl_u32 uint64_t
#define true
Definition stdbool.h:25
llvm::APSInt getIntValue() const
Get the constant integer value used by this variable to represent the comparison category result type...
EvalResult is a struct with detailed info about an evaluated expression.
Definition Expr.h:667
A quantity in bits.
const ValueDecl * asValueDecl() const
Definition DeclOrExpr.h:35
const Expr * asExpr() const
Definition DeclOrExpr.h:33
Describes a memory block created by an allocation site.
Definition Descriptor.h:126
unsigned getNumElems() const
Returns the number of elements stored in the block.
Definition Descriptor.h:249
bool isPrimitive() const
Checks if the descriptor is of a primitive.
Definition Descriptor.h:263
QualType getElemQualType() const
bool hasTrivialDtor() const
Whether variables of this descriptor need their destructor called or not.
bool isCompositeArray() const
Checks if the descriptor is of an array of composites.
Definition Descriptor.h:256
QualType getType() const
const Descriptor *const ElemDesc
Descriptor of the array element.
Definition Descriptor.h:152
bool isPrimitiveArray() const
Checks if the descriptor is of an array of primitives.
Definition Descriptor.h:254
PrimType getPrimType() const
Definition Descriptor.h:234
bool isRecord() const
Checks if the descriptor is of a record.
Definition Descriptor.h:268
const Record *const ElemRecord
Pointer to the record, if block contains records.
Definition Descriptor.h:150
bool isArray() const
Checks if the descriptor is of an array.
Definition Descriptor.h:266
Descriptor used for global variables.
Definition Descriptor.h:48
Information about a local's storage.
Definition Function.h:38
State encapsulating if a the variable creation has been successful, unsuccessful, or no variable has ...
Definition Compiler.h:101
static VarCreationState NotCreated()
Definition Compiler.h:105