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