xref: /llvm-project/clang/lib/AST/ByteCode/Compiler.cpp (revision 58fa55c04baaaa645a0bf9e265154b7ea7caf0d8)
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 "ByteCodeEmitter.h"
11 #include "Context.h"
12 #include "FixedPoint.h"
13 #include "Floating.h"
14 #include "Function.h"
15 #include "InterpShared.h"
16 #include "PrimType.h"
17 #include "Program.h"
18 #include "clang/AST/Attr.h"
19 
20 using namespace clang;
21 using namespace clang::interp;
22 
23 using APSInt = llvm::APSInt;
24 
25 namespace clang {
26 namespace interp {
27 
28 /// Scope used to handle temporaries in toplevel variable declarations.
29 template <class Emitter> class DeclScope final : public LocalScope<Emitter> {
30 public:
31   DeclScope(Compiler<Emitter> *Ctx, const ValueDecl *VD)
32       : LocalScope<Emitter>(Ctx, VD), Scope(Ctx->P, VD),
33         OldInitializingDecl(Ctx->InitializingDecl) {
34     Ctx->InitializingDecl = VD;
35     Ctx->InitStack.push_back(InitLink::Decl(VD));
36   }
37 
38   void addExtended(const Scope::Local &Local) override {
39     return this->addLocal(Local);
40   }
41 
42   ~DeclScope() {
43     this->Ctx->InitializingDecl = OldInitializingDecl;
44     this->Ctx->InitStack.pop_back();
45   }
46 
47 private:
48   Program::DeclScope Scope;
49   const ValueDecl *OldInitializingDecl;
50 };
51 
52 /// Scope used to handle initialization methods.
53 template <class Emitter> class OptionScope final {
54 public:
55   /// Root constructor, compiling or discarding primitives.
56   OptionScope(Compiler<Emitter> *Ctx, bool NewDiscardResult,
57               bool NewInitializing)
58       : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult),
59         OldInitializing(Ctx->Initializing) {
60     Ctx->DiscardResult = NewDiscardResult;
61     Ctx->Initializing = NewInitializing;
62   }
63 
64   ~OptionScope() {
65     Ctx->DiscardResult = OldDiscardResult;
66     Ctx->Initializing = OldInitializing;
67   }
68 
69 private:
70   /// Parent context.
71   Compiler<Emitter> *Ctx;
72   /// Old discard flag to restore.
73   bool OldDiscardResult;
74   bool OldInitializing;
75 };
76 
77 template <class Emitter>
78 bool InitLink::emit(Compiler<Emitter> *Ctx, const Expr *E) const {
79   switch (Kind) {
80   case K_This:
81     return Ctx->emitThis(E);
82   case K_Field:
83     // We're assuming there's a base pointer on the stack already.
84     return Ctx->emitGetPtrFieldPop(Offset, E);
85   case K_Temp:
86     return Ctx->emitGetPtrLocal(Offset, E);
87   case K_Decl:
88     return Ctx->visitDeclRef(D, E);
89   case K_Elem:
90     if (!Ctx->emitConstUint32(Offset, E))
91       return false;
92     return Ctx->emitArrayElemPtrPopUint32(E);
93   case K_RVO:
94     return Ctx->emitRVOPtr(E);
95   case K_InitList:
96     return true;
97   default:
98     llvm_unreachable("Unhandled InitLink kind");
99   }
100   return true;
101 }
102 
103 /// Scope managing label targets.
104 template <class Emitter> class LabelScope {
105 public:
106   virtual ~LabelScope() {}
107 
108 protected:
109   LabelScope(Compiler<Emitter> *Ctx) : Ctx(Ctx) {}
110   /// Compiler instance.
111   Compiler<Emitter> *Ctx;
112 };
113 
114 /// Sets the context for break/continue statements.
115 template <class Emitter> class LoopScope final : public LabelScope<Emitter> {
116 public:
117   using LabelTy = typename Compiler<Emitter>::LabelTy;
118   using OptLabelTy = typename Compiler<Emitter>::OptLabelTy;
119 
120   LoopScope(Compiler<Emitter> *Ctx, LabelTy BreakLabel, LabelTy ContinueLabel)
121       : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
122         OldContinueLabel(Ctx->ContinueLabel),
123         OldBreakVarScope(Ctx->BreakVarScope),
124         OldContinueVarScope(Ctx->ContinueVarScope) {
125     this->Ctx->BreakLabel = BreakLabel;
126     this->Ctx->ContinueLabel = ContinueLabel;
127     this->Ctx->BreakVarScope = this->Ctx->VarScope;
128     this->Ctx->ContinueVarScope = this->Ctx->VarScope;
129   }
130 
131   ~LoopScope() {
132     this->Ctx->BreakLabel = OldBreakLabel;
133     this->Ctx->ContinueLabel = OldContinueLabel;
134     this->Ctx->ContinueVarScope = OldContinueVarScope;
135     this->Ctx->BreakVarScope = OldBreakVarScope;
136   }
137 
138 private:
139   OptLabelTy OldBreakLabel;
140   OptLabelTy OldContinueLabel;
141   VariableScope<Emitter> *OldBreakVarScope;
142   VariableScope<Emitter> *OldContinueVarScope;
143 };
144 
145 // Sets the context for a switch scope, mapping labels.
146 template <class Emitter> class SwitchScope final : public LabelScope<Emitter> {
147 public:
148   using LabelTy = typename Compiler<Emitter>::LabelTy;
149   using OptLabelTy = typename Compiler<Emitter>::OptLabelTy;
150   using CaseMap = typename Compiler<Emitter>::CaseMap;
151 
152   SwitchScope(Compiler<Emitter> *Ctx, CaseMap &&CaseLabels, LabelTy BreakLabel,
153               OptLabelTy DefaultLabel)
154       : LabelScope<Emitter>(Ctx), OldBreakLabel(Ctx->BreakLabel),
155         OldDefaultLabel(this->Ctx->DefaultLabel),
156         OldCaseLabels(std::move(this->Ctx->CaseLabels)),
157         OldLabelVarScope(Ctx->BreakVarScope) {
158     this->Ctx->BreakLabel = BreakLabel;
159     this->Ctx->DefaultLabel = DefaultLabel;
160     this->Ctx->CaseLabels = std::move(CaseLabels);
161     this->Ctx->BreakVarScope = this->Ctx->VarScope;
162   }
163 
164   ~SwitchScope() {
165     this->Ctx->BreakLabel = OldBreakLabel;
166     this->Ctx->DefaultLabel = OldDefaultLabel;
167     this->Ctx->CaseLabels = std::move(OldCaseLabels);
168     this->Ctx->BreakVarScope = OldLabelVarScope;
169   }
170 
171 private:
172   OptLabelTy OldBreakLabel;
173   OptLabelTy OldDefaultLabel;
174   CaseMap OldCaseLabels;
175   VariableScope<Emitter> *OldLabelVarScope;
176 };
177 
178 template <class Emitter> class StmtExprScope final {
179 public:
180   StmtExprScope(Compiler<Emitter> *Ctx) : Ctx(Ctx), OldFlag(Ctx->InStmtExpr) {
181     Ctx->InStmtExpr = true;
182   }
183 
184   ~StmtExprScope() { Ctx->InStmtExpr = OldFlag; }
185 
186 private:
187   Compiler<Emitter> *Ctx;
188   bool OldFlag;
189 };
190 
191 } // namespace interp
192 } // namespace clang
193 
194 template <class Emitter>
195 bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
196   const Expr *SubExpr = CE->getSubExpr();
197   switch (CE->getCastKind()) {
198 
199   case CK_LValueToRValue: {
200     if (DiscardResult)
201       return this->discard(SubExpr);
202 
203     std::optional<PrimType> SubExprT = classify(SubExpr->getType());
204     // Prepare storage for the result.
205     if (!Initializing && !SubExprT) {
206       std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
207       if (!LocalIndex)
208         return false;
209       if (!this->emitGetPtrLocal(*LocalIndex, CE))
210         return false;
211     }
212 
213     if (!this->visit(SubExpr))
214       return false;
215 
216     if (SubExprT)
217       return this->emitLoadPop(*SubExprT, CE);
218 
219     // If the subexpr type is not primitive, we need to perform a copy here.
220     // This happens for example in C when dereferencing a pointer of struct
221     // type.
222     return this->emitMemcpy(CE);
223   }
224 
225   case CK_DerivedToBaseMemberPointer: {
226     assert(classifyPrim(CE->getType()) == PT_MemberPtr);
227     assert(classifyPrim(SubExpr->getType()) == PT_MemberPtr);
228     const auto *FromMP = SubExpr->getType()->getAs<MemberPointerType>();
229     const auto *ToMP = CE->getType()->getAs<MemberPointerType>();
230 
231     unsigned DerivedOffset = collectBaseOffset(QualType(ToMP->getClass(), 0),
232                                                QualType(FromMP->getClass(), 0));
233 
234     if (!this->delegate(SubExpr))
235       return false;
236 
237     return this->emitGetMemberPtrBasePop(DerivedOffset, CE);
238   }
239 
240   case CK_BaseToDerivedMemberPointer: {
241     assert(classifyPrim(CE) == PT_MemberPtr);
242     assert(classifyPrim(SubExpr) == PT_MemberPtr);
243     const auto *FromMP = SubExpr->getType()->getAs<MemberPointerType>();
244     const auto *ToMP = CE->getType()->getAs<MemberPointerType>();
245 
246     unsigned DerivedOffset = collectBaseOffset(QualType(FromMP->getClass(), 0),
247                                                QualType(ToMP->getClass(), 0));
248 
249     if (!this->delegate(SubExpr))
250       return false;
251     return this->emitGetMemberPtrBasePop(-DerivedOffset, CE);
252   }
253 
254   case CK_UncheckedDerivedToBase:
255   case CK_DerivedToBase: {
256     if (!this->delegate(SubExpr))
257       return false;
258 
259     const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
260       if (const auto *PT = dyn_cast<PointerType>(Ty))
261         return PT->getPointeeType()->getAsCXXRecordDecl();
262       return Ty->getAsCXXRecordDecl();
263     };
264 
265     // FIXME: We can express a series of non-virtual casts as a single
266     // GetPtrBasePop op.
267     QualType CurType = SubExpr->getType();
268     for (const CXXBaseSpecifier *B : CE->path()) {
269       if (B->isVirtual()) {
270         if (!this->emitGetPtrVirtBasePop(extractRecordDecl(B->getType()), CE))
271           return false;
272         CurType = B->getType();
273       } else {
274         unsigned DerivedOffset = collectBaseOffset(B->getType(), CurType);
275         if (!this->emitGetPtrBasePop(DerivedOffset, CE))
276           return false;
277         CurType = B->getType();
278       }
279     }
280 
281     return true;
282   }
283 
284   case CK_BaseToDerived: {
285     if (!this->delegate(SubExpr))
286       return false;
287 
288     unsigned DerivedOffset =
289         collectBaseOffset(SubExpr->getType(), CE->getType());
290 
291     return this->emitGetPtrDerivedPop(DerivedOffset, CE);
292   }
293 
294   case CK_FloatingCast: {
295     // HLSL uses CK_FloatingCast to cast between vectors.
296     if (!SubExpr->getType()->isFloatingType() ||
297         !CE->getType()->isFloatingType())
298       return false;
299     if (DiscardResult)
300       return this->discard(SubExpr);
301     if (!this->visit(SubExpr))
302       return false;
303     const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
304     return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
305   }
306 
307   case CK_IntegralToFloating: {
308     if (DiscardResult)
309       return this->discard(SubExpr);
310     std::optional<PrimType> FromT = classify(SubExpr->getType());
311     if (!FromT)
312       return false;
313 
314     if (!this->visit(SubExpr))
315       return false;
316 
317     const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
318     return this->emitCastIntegralFloating(*FromT, TargetSemantics,
319                                           getFPOptions(CE), CE);
320   }
321 
322   case CK_FloatingToBoolean:
323   case CK_FloatingToIntegral: {
324     if (DiscardResult)
325       return this->discard(SubExpr);
326 
327     std::optional<PrimType> ToT = classify(CE->getType());
328 
329     if (!ToT)
330       return false;
331 
332     if (!this->visit(SubExpr))
333       return false;
334 
335     if (ToT == PT_IntAP)
336       return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(CE->getType()),
337                                               getFPOptions(CE), CE);
338     if (ToT == PT_IntAPS)
339       return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(CE->getType()),
340                                                getFPOptions(CE), CE);
341 
342     return this->emitCastFloatingIntegral(*ToT, getFPOptions(CE), CE);
343   }
344 
345   case CK_NullToPointer:
346   case CK_NullToMemberPointer: {
347     if (!this->discard(SubExpr))
348       return false;
349     if (DiscardResult)
350       return true;
351 
352     const Descriptor *Desc = nullptr;
353     const QualType PointeeType = CE->getType()->getPointeeType();
354     if (!PointeeType.isNull()) {
355       if (std::optional<PrimType> T = classify(PointeeType))
356         Desc = P.createDescriptor(SubExpr, *T);
357       else
358         Desc = P.createDescriptor(SubExpr, PointeeType.getTypePtr(),
359                                   std::nullopt, true, false,
360                                   /*IsMutable=*/false, nullptr);
361     }
362 
363     uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(CE->getType());
364     return this->emitNull(classifyPrim(CE->getType()), Val, Desc, CE);
365   }
366 
367   case CK_PointerToIntegral: {
368     if (DiscardResult)
369       return this->discard(SubExpr);
370 
371     if (!this->visit(SubExpr))
372       return false;
373 
374     // If SubExpr doesn't result in a pointer, make it one.
375     if (PrimType FromT = classifyPrim(SubExpr->getType()); FromT != PT_Ptr) {
376       assert(isPtrType(FromT));
377       if (!this->emitDecayPtr(FromT, PT_Ptr, CE))
378         return false;
379     }
380 
381     PrimType T = classifyPrim(CE->getType());
382     if (T == PT_IntAP)
383       return this->emitCastPointerIntegralAP(Ctx.getBitWidth(CE->getType()),
384                                              CE);
385     if (T == PT_IntAPS)
386       return this->emitCastPointerIntegralAPS(Ctx.getBitWidth(CE->getType()),
387                                               CE);
388     return this->emitCastPointerIntegral(T, CE);
389   }
390 
391   case CK_ArrayToPointerDecay: {
392     if (!this->visit(SubExpr))
393       return false;
394     if (!this->emitArrayDecay(CE))
395       return false;
396     if (DiscardResult)
397       return this->emitPopPtr(CE);
398     return true;
399   }
400 
401   case CK_IntegralToPointer: {
402     QualType IntType = SubExpr->getType();
403     assert(IntType->isIntegralOrEnumerationType());
404     if (!this->visit(SubExpr))
405       return false;
406     // FIXME: I think the discard is wrong since the int->ptr cast might cause a
407     // diagnostic.
408     PrimType T = classifyPrim(IntType);
409     if (DiscardResult)
410       return this->emitPop(T, CE);
411 
412     QualType PtrType = CE->getType();
413     const Descriptor *Desc;
414     if (std::optional<PrimType> T = classify(PtrType->getPointeeType()))
415       Desc = P.createDescriptor(SubExpr, *T);
416     else if (PtrType->getPointeeType()->isVoidType())
417       Desc = nullptr;
418     else
419       Desc = P.createDescriptor(CE, PtrType->getPointeeType().getTypePtr(),
420                                 Descriptor::InlineDescMD, true, false,
421                                 /*IsMutable=*/false, nullptr);
422 
423     if (!this->emitGetIntPtr(T, Desc, CE))
424       return false;
425 
426     PrimType DestPtrT = classifyPrim(PtrType);
427     if (DestPtrT == PT_Ptr)
428       return true;
429 
430     // In case we're converting the integer to a non-Pointer.
431     return this->emitDecayPtr(PT_Ptr, DestPtrT, CE);
432   }
433 
434   case CK_AtomicToNonAtomic:
435   case CK_ConstructorConversion:
436   case CK_FunctionToPointerDecay:
437   case CK_NonAtomicToAtomic:
438   case CK_NoOp:
439   case CK_UserDefinedConversion:
440   case CK_AddressSpaceConversion:
441   case CK_CPointerToObjCPointerCast:
442     return this->delegate(SubExpr);
443 
444   case CK_BitCast: {
445     // Reject bitcasts to atomic types.
446     if (CE->getType()->isAtomicType()) {
447       if (!this->discard(SubExpr))
448         return false;
449       return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, CE);
450     }
451 
452     if (DiscardResult)
453       return this->discard(SubExpr);
454 
455     QualType SubExprTy = SubExpr->getType();
456     std::optional<PrimType> FromT = classify(SubExprTy);
457     // Casts from integer/vector to vector.
458     if (CE->getType()->isVectorType())
459       return this->emitBuiltinBitCast(CE);
460 
461     std::optional<PrimType> ToT = classify(CE->getType());
462     if (!FromT || !ToT)
463       return false;
464 
465     assert(isPtrType(*FromT));
466     assert(isPtrType(*ToT));
467     if (FromT == ToT) {
468       if (CE->getType()->isVoidPointerType())
469         return this->delegate(SubExpr);
470 
471       if (!this->visit(SubExpr))
472         return false;
473       if (FromT == PT_Ptr)
474         return this->emitPtrPtrCast(SubExprTy->isVoidPointerType(), CE);
475       return true;
476     }
477 
478     if (!this->visit(SubExpr))
479       return false;
480     return this->emitDecayPtr(*FromT, *ToT, CE);
481   }
482 
483   case CK_LValueToRValueBitCast:
484     return this->emitBuiltinBitCast(CE);
485 
486   case CK_IntegralToBoolean:
487   case CK_FixedPointToBoolean:
488   case CK_BooleanToSignedIntegral:
489   case CK_IntegralCast: {
490     if (DiscardResult)
491       return this->discard(SubExpr);
492     std::optional<PrimType> FromT = classify(SubExpr->getType());
493     std::optional<PrimType> ToT = classify(CE->getType());
494 
495     if (!FromT || !ToT)
496       return false;
497 
498     if (!this->visit(SubExpr))
499       return false;
500 
501     // Possibly diagnose casts to enum types if the target type does not
502     // have a fixed size.
503     if (Ctx.getLangOpts().CPlusPlus && CE->getType()->isEnumeralType()) {
504       if (const auto *ET = CE->getType().getCanonicalType()->getAs<EnumType>();
505           ET && !ET->getDecl()->isFixed()) {
506         if (!this->emitCheckEnumValue(*FromT, ET->getDecl(), CE))
507           return false;
508       }
509     }
510 
511     auto maybeNegate = [&]() -> bool {
512       if (CE->getCastKind() == CK_BooleanToSignedIntegral)
513         return this->emitNeg(*ToT, CE);
514       return true;
515     };
516 
517     if (ToT == PT_IntAP)
518       return this->emitCastAP(*FromT, Ctx.getBitWidth(CE->getType()), CE) &&
519              maybeNegate();
520     if (ToT == PT_IntAPS)
521       return this->emitCastAPS(*FromT, Ctx.getBitWidth(CE->getType()), CE) &&
522              maybeNegate();
523 
524     if (FromT == ToT)
525       return true;
526     if (!this->emitCast(*FromT, *ToT, CE))
527       return false;
528 
529     return maybeNegate();
530   }
531 
532   case CK_PointerToBoolean:
533   case CK_MemberPointerToBoolean: {
534     PrimType PtrT = classifyPrim(SubExpr->getType());
535 
536     if (!this->visit(SubExpr))
537       return false;
538     return this->emitIsNonNull(PtrT, CE);
539   }
540 
541   case CK_IntegralComplexToBoolean:
542   case CK_FloatingComplexToBoolean: {
543     if (DiscardResult)
544       return this->discard(SubExpr);
545     if (!this->visit(SubExpr))
546       return false;
547     return this->emitComplexBoolCast(SubExpr);
548   }
549 
550   case CK_IntegralComplexToReal:
551   case CK_FloatingComplexToReal:
552     return this->emitComplexReal(SubExpr);
553 
554   case CK_IntegralRealToComplex:
555   case CK_FloatingRealToComplex: {
556     // We're creating a complex value here, so we need to
557     // allocate storage for it.
558     if (!Initializing) {
559       unsigned LocalIndex = allocateTemporary(CE);
560       if (!this->emitGetPtrLocal(LocalIndex, CE))
561         return false;
562     }
563 
564     // Init the complex value to {SubExpr, 0}.
565     if (!this->visitArrayElemInit(0, SubExpr))
566       return false;
567     // Zero-init the second element.
568     PrimType T = classifyPrim(SubExpr->getType());
569     if (!this->visitZeroInitializer(T, SubExpr->getType(), SubExpr))
570       return false;
571     return this->emitInitElem(T, 1, SubExpr);
572   }
573 
574   case CK_IntegralComplexCast:
575   case CK_FloatingComplexCast:
576   case CK_IntegralComplexToFloatingComplex:
577   case CK_FloatingComplexToIntegralComplex: {
578     assert(CE->getType()->isAnyComplexType());
579     assert(SubExpr->getType()->isAnyComplexType());
580     if (DiscardResult)
581       return this->discard(SubExpr);
582 
583     if (!Initializing) {
584       std::optional<unsigned> LocalIndex = allocateLocal(CE);
585       if (!LocalIndex)
586         return false;
587       if (!this->emitGetPtrLocal(*LocalIndex, CE))
588         return false;
589     }
590 
591     // Location for the SubExpr.
592     // Since SubExpr is of complex type, visiting it results in a pointer
593     // anyway, so we just create a temporary pointer variable.
594     unsigned SubExprOffset = allocateLocalPrimitive(
595         SubExpr, PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
596     if (!this->visit(SubExpr))
597       return false;
598     if (!this->emitSetLocal(PT_Ptr, SubExprOffset, CE))
599       return false;
600 
601     PrimType SourceElemT = classifyComplexElementType(SubExpr->getType());
602     QualType DestElemType =
603         CE->getType()->getAs<ComplexType>()->getElementType();
604     PrimType DestElemT = classifyPrim(DestElemType);
605     // Cast both elements individually.
606     for (unsigned I = 0; I != 2; ++I) {
607       if (!this->emitGetLocal(PT_Ptr, SubExprOffset, CE))
608         return false;
609       if (!this->emitArrayElemPop(SourceElemT, I, CE))
610         return false;
611 
612       // Do the cast.
613       if (!this->emitPrimCast(SourceElemT, DestElemT, DestElemType, CE))
614         return false;
615 
616       // Save the value.
617       if (!this->emitInitElem(DestElemT, I, CE))
618         return false;
619     }
620     return true;
621   }
622 
623   case CK_VectorSplat: {
624     assert(!classify(CE->getType()));
625     assert(classify(SubExpr->getType()));
626     assert(CE->getType()->isVectorType());
627 
628     if (DiscardResult)
629       return this->discard(SubExpr);
630 
631     if (!Initializing) {
632       std::optional<unsigned> LocalIndex = allocateLocal(CE);
633       if (!LocalIndex)
634         return false;
635       if (!this->emitGetPtrLocal(*LocalIndex, CE))
636         return false;
637     }
638 
639     const auto *VT = CE->getType()->getAs<VectorType>();
640     PrimType ElemT = classifyPrim(SubExpr->getType());
641     unsigned ElemOffset = allocateLocalPrimitive(
642         SubExpr, ElemT, /*IsConst=*/true, /*IsExtended=*/false);
643 
644     // Prepare a local variable for the scalar value.
645     if (!this->visit(SubExpr))
646       return false;
647     if (classifyPrim(SubExpr) == PT_Ptr && !this->emitLoadPop(ElemT, CE))
648       return false;
649 
650     if (!this->emitSetLocal(ElemT, ElemOffset, CE))
651       return false;
652 
653     for (unsigned I = 0; I != VT->getNumElements(); ++I) {
654       if (!this->emitGetLocal(ElemT, ElemOffset, CE))
655         return false;
656       if (!this->emitInitElem(ElemT, I, CE))
657         return false;
658     }
659 
660     return true;
661   }
662 
663   case CK_HLSLVectorTruncation: {
664     assert(SubExpr->getType()->isVectorType());
665     if (std::optional<PrimType> ResultT = classify(CE)) {
666       assert(!DiscardResult);
667       // Result must be either a float or integer. Take the first element.
668       if (!this->visit(SubExpr))
669         return false;
670       return this->emitArrayElemPop(*ResultT, 0, CE);
671     }
672     // Otherwise, this truncates from one vector type to another.
673     assert(CE->getType()->isVectorType());
674 
675     if (!Initializing) {
676       unsigned LocalIndex = allocateTemporary(CE);
677       if (!this->emitGetPtrLocal(LocalIndex, CE))
678         return false;
679     }
680     unsigned ToSize = CE->getType()->getAs<VectorType>()->getNumElements();
681     assert(SubExpr->getType()->getAs<VectorType>()->getNumElements() > ToSize);
682     if (!this->visit(SubExpr))
683       return false;
684     return this->emitCopyArray(classifyVectorElementType(CE->getType()), 0, 0,
685                                ToSize, CE);
686   };
687 
688   case CK_IntegralToFixedPoint: {
689     if (!this->visit(SubExpr))
690       return false;
691 
692     auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
693     uint32_t I;
694     std::memcpy(&I, &Sem, sizeof(Sem));
695     return this->emitCastIntegralFixedPoint(classifyPrim(SubExpr->getType()), I,
696                                             CE);
697   }
698   case CK_FloatingToFixedPoint: {
699     if (!this->visit(SubExpr))
700       return false;
701 
702     auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
703     uint32_t I;
704     std::memcpy(&I, &Sem, sizeof(Sem));
705     return this->emitCastFloatingFixedPoint(I, CE);
706   }
707   case CK_FixedPointToFloating: {
708     if (!this->visit(SubExpr))
709       return false;
710     const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
711     return this->emitCastFixedPointFloating(TargetSemantics, CE);
712   }
713   case CK_FixedPointToIntegral: {
714     if (!this->visit(SubExpr))
715       return false;
716     return this->emitCastFixedPointIntegral(classifyPrim(CE->getType()), CE);
717   }
718   case CK_FixedPointCast: {
719     if (!this->visit(SubExpr))
720       return false;
721     auto Sem = Ctx.getASTContext().getFixedPointSemantics(CE->getType());
722     uint32_t I;
723     std::memcpy(&I, &Sem, sizeof(Sem));
724     return this->emitCastFixedPoint(I, CE);
725   }
726 
727   case CK_ToVoid:
728     return discard(SubExpr);
729 
730   default:
731     return this->emitInvalid(CE);
732   }
733   llvm_unreachable("Unhandled clang::CastKind enum");
734 }
735 
736 template <class Emitter>
737 bool Compiler<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
738   if (DiscardResult)
739     return true;
740 
741   return this->emitConst(LE->getValue(), LE);
742 }
743 
744 template <class Emitter>
745 bool Compiler<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) {
746   if (DiscardResult)
747     return true;
748 
749   return this->emitConstFloat(E->getValue(), E);
750 }
751 
752 template <class Emitter>
753 bool Compiler<Emitter>::VisitImaginaryLiteral(const ImaginaryLiteral *E) {
754   assert(E->getType()->isAnyComplexType());
755   if (DiscardResult)
756     return true;
757 
758   if (!Initializing) {
759     unsigned LocalIndex = allocateTemporary(E);
760     if (!this->emitGetPtrLocal(LocalIndex, E))
761       return false;
762   }
763 
764   const Expr *SubExpr = E->getSubExpr();
765   PrimType SubExprT = classifyPrim(SubExpr->getType());
766 
767   if (!this->visitZeroInitializer(SubExprT, SubExpr->getType(), SubExpr))
768     return false;
769   if (!this->emitInitElem(SubExprT, 0, SubExpr))
770     return false;
771   return this->visitArrayElemInit(1, SubExpr);
772 }
773 
774 template <class Emitter>
775 bool Compiler<Emitter>::VisitFixedPointLiteral(const FixedPointLiteral *E) {
776   assert(E->getType()->isFixedPointType());
777   assert(classifyPrim(E) == PT_FixedPoint);
778 
779   if (DiscardResult)
780     return true;
781 
782   auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
783   APInt Value = E->getValue();
784   return this->emitConstFixedPoint(FixedPoint(Value, Sem), E);
785 }
786 
787 template <class Emitter>
788 bool Compiler<Emitter>::VisitParenExpr(const ParenExpr *E) {
789   return this->delegate(E->getSubExpr());
790 }
791 
792 template <class Emitter>
793 bool Compiler<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
794   // Need short-circuiting for these.
795   if (BO->isLogicalOp() && !BO->getType()->isVectorType())
796     return this->VisitLogicalBinOp(BO);
797 
798   const Expr *LHS = BO->getLHS();
799   const Expr *RHS = BO->getRHS();
800 
801   // Handle comma operators. Just discard the LHS
802   // and delegate to RHS.
803   if (BO->isCommaOp()) {
804     if (!this->discard(LHS))
805       return false;
806     if (RHS->getType()->isVoidType())
807       return this->discard(RHS);
808 
809     return this->delegate(RHS);
810   }
811 
812   if (BO->getType()->isAnyComplexType())
813     return this->VisitComplexBinOp(BO);
814   if (BO->getType()->isVectorType())
815     return this->VisitVectorBinOp(BO);
816   if ((LHS->getType()->isAnyComplexType() ||
817        RHS->getType()->isAnyComplexType()) &&
818       BO->isComparisonOp())
819     return this->emitComplexComparison(LHS, RHS, BO);
820   if (LHS->getType()->isFixedPointType() || RHS->getType()->isFixedPointType())
821     return this->VisitFixedPointBinOp(BO);
822 
823   if (BO->isPtrMemOp()) {
824     if (!this->visit(LHS))
825       return false;
826 
827     if (!this->visit(RHS))
828       return false;
829 
830     if (!this->emitToMemberPtr(BO))
831       return false;
832 
833     if (classifyPrim(BO) == PT_MemberPtr)
834       return true;
835 
836     if (!this->emitCastMemberPtrPtr(BO))
837       return false;
838     return DiscardResult ? this->emitPopPtr(BO) : true;
839   }
840 
841   // Typecheck the args.
842   std::optional<PrimType> LT = classify(LHS);
843   std::optional<PrimType> RT = classify(RHS);
844   std::optional<PrimType> T = classify(BO->getType());
845 
846   // Special case for C++'s three-way/spaceship operator <=>, which
847   // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
848   // have a PrimType).
849   if (!T && BO->getOpcode() == BO_Cmp) {
850     if (DiscardResult)
851       return true;
852     const ComparisonCategoryInfo *CmpInfo =
853         Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
854     assert(CmpInfo);
855 
856     // We need a temporary variable holding our return value.
857     if (!Initializing) {
858       std::optional<unsigned> ResultIndex = this->allocateLocal(BO);
859       if (!this->emitGetPtrLocal(*ResultIndex, BO))
860         return false;
861     }
862 
863     if (!visit(LHS) || !visit(RHS))
864       return false;
865 
866     return this->emitCMP3(*LT, CmpInfo, BO);
867   }
868 
869   if (!LT || !RT || !T)
870     return false;
871 
872   // Pointer arithmetic special case.
873   if (BO->getOpcode() == BO_Add || BO->getOpcode() == BO_Sub) {
874     if (isPtrType(*T) || (isPtrType(*LT) && isPtrType(*RT)))
875       return this->VisitPointerArithBinOp(BO);
876   }
877 
878   // Assignmentes require us to evalute the RHS first.
879   if (BO->getOpcode() == BO_Assign) {
880     if (!visit(RHS) || !visit(LHS))
881       return false;
882     if (!this->emitFlip(*LT, *RT, BO))
883       return false;
884   } else {
885     if (!visit(LHS) || !visit(RHS))
886       return false;
887   }
888 
889   // For languages such as C, cast the result of one
890   // of our comparision opcodes to T (which is usually int).
891   auto MaybeCastToBool = [this, T, BO](bool Result) {
892     if (!Result)
893       return false;
894     if (DiscardResult)
895       return this->emitPop(*T, BO);
896     if (T != PT_Bool)
897       return this->emitCast(PT_Bool, *T, BO);
898     return true;
899   };
900 
901   auto Discard = [this, T, BO](bool Result) {
902     if (!Result)
903       return false;
904     return DiscardResult ? this->emitPop(*T, BO) : true;
905   };
906 
907   switch (BO->getOpcode()) {
908   case BO_EQ:
909     return MaybeCastToBool(this->emitEQ(*LT, BO));
910   case BO_NE:
911     return MaybeCastToBool(this->emitNE(*LT, BO));
912   case BO_LT:
913     return MaybeCastToBool(this->emitLT(*LT, BO));
914   case BO_LE:
915     return MaybeCastToBool(this->emitLE(*LT, BO));
916   case BO_GT:
917     return MaybeCastToBool(this->emitGT(*LT, BO));
918   case BO_GE:
919     return MaybeCastToBool(this->emitGE(*LT, BO));
920   case BO_Sub:
921     if (BO->getType()->isFloatingType())
922       return Discard(this->emitSubf(getFPOptions(BO), BO));
923     return Discard(this->emitSub(*T, BO));
924   case BO_Add:
925     if (BO->getType()->isFloatingType())
926       return Discard(this->emitAddf(getFPOptions(BO), BO));
927     return Discard(this->emitAdd(*T, BO));
928   case BO_Mul:
929     if (BO->getType()->isFloatingType())
930       return Discard(this->emitMulf(getFPOptions(BO), BO));
931     return Discard(this->emitMul(*T, BO));
932   case BO_Rem:
933     return Discard(this->emitRem(*T, BO));
934   case BO_Div:
935     if (BO->getType()->isFloatingType())
936       return Discard(this->emitDivf(getFPOptions(BO), BO));
937     return Discard(this->emitDiv(*T, BO));
938   case BO_Assign:
939     if (DiscardResult)
940       return LHS->refersToBitField() ? this->emitStoreBitFieldPop(*T, BO)
941                                      : this->emitStorePop(*T, BO);
942     if (LHS->refersToBitField()) {
943       if (!this->emitStoreBitField(*T, BO))
944         return false;
945     } else {
946       if (!this->emitStore(*T, BO))
947         return false;
948     }
949     // Assignments aren't necessarily lvalues in C.
950     // Load from them in that case.
951     if (!BO->isLValue())
952       return this->emitLoadPop(*T, BO);
953     return true;
954   case BO_And:
955     return Discard(this->emitBitAnd(*T, BO));
956   case BO_Or:
957     return Discard(this->emitBitOr(*T, BO));
958   case BO_Shl:
959     return Discard(this->emitShl(*LT, *RT, BO));
960   case BO_Shr:
961     return Discard(this->emitShr(*LT, *RT, BO));
962   case BO_Xor:
963     return Discard(this->emitBitXor(*T, BO));
964   case BO_LOr:
965   case BO_LAnd:
966     llvm_unreachable("Already handled earlier");
967   default:
968     return false;
969   }
970 
971   llvm_unreachable("Unhandled binary op");
972 }
973 
974 /// Perform addition/subtraction of a pointer and an integer or
975 /// subtraction of two pointers.
976 template <class Emitter>
977 bool Compiler<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) {
978   BinaryOperatorKind Op = E->getOpcode();
979   const Expr *LHS = E->getLHS();
980   const Expr *RHS = E->getRHS();
981 
982   if ((Op != BO_Add && Op != BO_Sub) ||
983       (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType()))
984     return false;
985 
986   std::optional<PrimType> LT = classify(LHS);
987   std::optional<PrimType> RT = classify(RHS);
988 
989   if (!LT || !RT)
990     return false;
991 
992   // Visit the given pointer expression and optionally convert to a PT_Ptr.
993   auto visitAsPointer = [&](const Expr *E, PrimType T) -> bool {
994     if (!this->visit(E))
995       return false;
996     if (T != PT_Ptr)
997       return this->emitDecayPtr(T, PT_Ptr, E);
998     return true;
999   };
1000 
1001   if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
1002     if (Op != BO_Sub)
1003       return false;
1004 
1005     assert(E->getType()->isIntegerType());
1006     if (!visitAsPointer(RHS, *RT) || !visitAsPointer(LHS, *LT))
1007       return false;
1008 
1009     PrimType IntT = classifyPrim(E->getType());
1010     if (!this->emitSubPtr(IntT, E))
1011       return false;
1012     return DiscardResult ? this->emitPop(IntT, E) : true;
1013   }
1014 
1015   PrimType OffsetType;
1016   if (LHS->getType()->isIntegerType()) {
1017     if (!visitAsPointer(RHS, *RT))
1018       return false;
1019     if (!this->visit(LHS))
1020       return false;
1021     OffsetType = *LT;
1022   } else if (RHS->getType()->isIntegerType()) {
1023     if (!visitAsPointer(LHS, *LT))
1024       return false;
1025     if (!this->visit(RHS))
1026       return false;
1027     OffsetType = *RT;
1028   } else {
1029     return false;
1030   }
1031 
1032   // Do the operation and optionally transform to
1033   // result pointer type.
1034   if (Op == BO_Add) {
1035     if (!this->emitAddOffset(OffsetType, E))
1036       return false;
1037 
1038     if (classifyPrim(E) != PT_Ptr)
1039       return this->emitDecayPtr(PT_Ptr, classifyPrim(E), E);
1040     return true;
1041   } else if (Op == BO_Sub) {
1042     if (!this->emitSubOffset(OffsetType, E))
1043       return false;
1044 
1045     if (classifyPrim(E) != PT_Ptr)
1046       return this->emitDecayPtr(PT_Ptr, classifyPrim(E), E);
1047     return true;
1048   }
1049 
1050   return false;
1051 }
1052 
1053 template <class Emitter>
1054 bool Compiler<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) {
1055   assert(E->isLogicalOp());
1056   BinaryOperatorKind Op = E->getOpcode();
1057   const Expr *LHS = E->getLHS();
1058   const Expr *RHS = E->getRHS();
1059   std::optional<PrimType> T = classify(E->getType());
1060 
1061   if (Op == BO_LOr) {
1062     // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
1063     LabelTy LabelTrue = this->getLabel();
1064     LabelTy LabelEnd = this->getLabel();
1065 
1066     if (!this->visitBool(LHS))
1067       return false;
1068     if (!this->jumpTrue(LabelTrue))
1069       return false;
1070 
1071     if (!this->visitBool(RHS))
1072       return false;
1073     if (!this->jump(LabelEnd))
1074       return false;
1075 
1076     this->emitLabel(LabelTrue);
1077     this->emitConstBool(true, E);
1078     this->fallthrough(LabelEnd);
1079     this->emitLabel(LabelEnd);
1080 
1081   } else {
1082     assert(Op == BO_LAnd);
1083     // Logical AND.
1084     // Visit LHS. Only visit RHS if LHS was TRUE.
1085     LabelTy LabelFalse = this->getLabel();
1086     LabelTy LabelEnd = this->getLabel();
1087 
1088     if (!this->visitBool(LHS))
1089       return false;
1090     if (!this->jumpFalse(LabelFalse))
1091       return false;
1092 
1093     if (!this->visitBool(RHS))
1094       return false;
1095     if (!this->jump(LabelEnd))
1096       return false;
1097 
1098     this->emitLabel(LabelFalse);
1099     this->emitConstBool(false, E);
1100     this->fallthrough(LabelEnd);
1101     this->emitLabel(LabelEnd);
1102   }
1103 
1104   if (DiscardResult)
1105     return this->emitPopBool(E);
1106 
1107   // For C, cast back to integer type.
1108   assert(T);
1109   if (T != PT_Bool)
1110     return this->emitCast(PT_Bool, *T, E);
1111   return true;
1112 }
1113 
1114 template <class Emitter>
1115 bool Compiler<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
1116   // Prepare storage for result.
1117   if (!Initializing) {
1118     unsigned LocalIndex = allocateTemporary(E);
1119     if (!this->emitGetPtrLocal(LocalIndex, E))
1120       return false;
1121   }
1122 
1123   // Both LHS and RHS might _not_ be of complex type, but one of them
1124   // needs to be.
1125   const Expr *LHS = E->getLHS();
1126   const Expr *RHS = E->getRHS();
1127 
1128   PrimType ResultElemT = this->classifyComplexElementType(E->getType());
1129   unsigned ResultOffset = ~0u;
1130   if (!DiscardResult)
1131     ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, true, false);
1132 
1133   // Save result pointer in ResultOffset
1134   if (!this->DiscardResult) {
1135     if (!this->emitDupPtr(E))
1136       return false;
1137     if (!this->emitSetLocal(PT_Ptr, ResultOffset, E))
1138       return false;
1139   }
1140   QualType LHSType = LHS->getType();
1141   if (const auto *AT = LHSType->getAs<AtomicType>())
1142     LHSType = AT->getValueType();
1143   QualType RHSType = RHS->getType();
1144   if (const auto *AT = RHSType->getAs<AtomicType>())
1145     RHSType = AT->getValueType();
1146 
1147   bool LHSIsComplex = LHSType->isAnyComplexType();
1148   unsigned LHSOffset;
1149   bool RHSIsComplex = RHSType->isAnyComplexType();
1150 
1151   // For ComplexComplex Mul, we have special ops to make their implementation
1152   // easier.
1153   BinaryOperatorKind Op = E->getOpcode();
1154   if (Op == BO_Mul && LHSIsComplex && RHSIsComplex) {
1155     assert(classifyPrim(LHSType->getAs<ComplexType>()->getElementType()) ==
1156            classifyPrim(RHSType->getAs<ComplexType>()->getElementType()));
1157     PrimType ElemT =
1158         classifyPrim(LHSType->getAs<ComplexType>()->getElementType());
1159     if (!this->visit(LHS))
1160       return false;
1161     if (!this->visit(RHS))
1162       return false;
1163     return this->emitMulc(ElemT, E);
1164   }
1165 
1166   if (Op == BO_Div && RHSIsComplex) {
1167     QualType ElemQT = RHSType->getAs<ComplexType>()->getElementType();
1168     PrimType ElemT = classifyPrim(ElemQT);
1169     // If the LHS is not complex, we still need to do the full complex
1170     // division, so just stub create a complex value and stub it out with
1171     // the LHS and a zero.
1172 
1173     if (!LHSIsComplex) {
1174       // This is using the RHS type for the fake-complex LHS.
1175       LHSOffset = allocateTemporary(RHS);
1176 
1177       if (!this->emitGetPtrLocal(LHSOffset, E))
1178         return false;
1179 
1180       if (!this->visit(LHS))
1181         return false;
1182       // real is LHS
1183       if (!this->emitInitElem(ElemT, 0, E))
1184         return false;
1185       // imag is zero
1186       if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1187         return false;
1188       if (!this->emitInitElem(ElemT, 1, E))
1189         return false;
1190     } else {
1191       if (!this->visit(LHS))
1192         return false;
1193     }
1194 
1195     if (!this->visit(RHS))
1196       return false;
1197     return this->emitDivc(ElemT, E);
1198   }
1199 
1200   // Evaluate LHS and save value to LHSOffset.
1201   if (LHSType->isAnyComplexType()) {
1202     LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
1203     if (!this->visit(LHS))
1204       return false;
1205     if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1206       return false;
1207   } else {
1208     PrimType LHST = classifyPrim(LHSType);
1209     LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
1210     if (!this->visit(LHS))
1211       return false;
1212     if (!this->emitSetLocal(LHST, LHSOffset, E))
1213       return false;
1214   }
1215 
1216   // Same with RHS.
1217   unsigned RHSOffset;
1218   if (RHSType->isAnyComplexType()) {
1219     RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
1220     if (!this->visit(RHS))
1221       return false;
1222     if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1223       return false;
1224   } else {
1225     PrimType RHST = classifyPrim(RHSType);
1226     RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
1227     if (!this->visit(RHS))
1228       return false;
1229     if (!this->emitSetLocal(RHST, RHSOffset, E))
1230       return false;
1231   }
1232 
1233   // For both LHS and RHS, either load the value from the complex pointer, or
1234   // directly from the local variable. For index 1 (i.e. the imaginary part),
1235   // just load 0 and do the operation anyway.
1236   auto loadComplexValue = [this](bool IsComplex, bool LoadZero,
1237                                  unsigned ElemIndex, unsigned Offset,
1238                                  const Expr *E) -> bool {
1239     if (IsComplex) {
1240       if (!this->emitGetLocal(PT_Ptr, Offset, E))
1241         return false;
1242       return this->emitArrayElemPop(classifyComplexElementType(E->getType()),
1243                                     ElemIndex, E);
1244     }
1245     if (ElemIndex == 0 || !LoadZero)
1246       return this->emitGetLocal(classifyPrim(E->getType()), Offset, E);
1247     return this->visitZeroInitializer(classifyPrim(E->getType()), E->getType(),
1248                                       E);
1249   };
1250 
1251   // Now we can get pointers to the LHS and RHS from the offsets above.
1252   for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) {
1253     // Result pointer for the store later.
1254     if (!this->DiscardResult) {
1255       if (!this->emitGetLocal(PT_Ptr, ResultOffset, E))
1256         return false;
1257     }
1258 
1259     // The actual operation.
1260     switch (Op) {
1261     case BO_Add:
1262       if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1263         return false;
1264 
1265       if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1266         return false;
1267       if (ResultElemT == PT_Float) {
1268         if (!this->emitAddf(getFPOptions(E), E))
1269           return false;
1270       } else {
1271         if (!this->emitAdd(ResultElemT, E))
1272           return false;
1273       }
1274       break;
1275     case BO_Sub:
1276       if (!loadComplexValue(LHSIsComplex, true, ElemIndex, LHSOffset, LHS))
1277         return false;
1278 
1279       if (!loadComplexValue(RHSIsComplex, true, ElemIndex, RHSOffset, RHS))
1280         return false;
1281       if (ResultElemT == PT_Float) {
1282         if (!this->emitSubf(getFPOptions(E), E))
1283           return false;
1284       } else {
1285         if (!this->emitSub(ResultElemT, E))
1286           return false;
1287       }
1288       break;
1289     case BO_Mul:
1290       if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1291         return false;
1292 
1293       if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1294         return false;
1295 
1296       if (ResultElemT == PT_Float) {
1297         if (!this->emitMulf(getFPOptions(E), E))
1298           return false;
1299       } else {
1300         if (!this->emitMul(ResultElemT, E))
1301           return false;
1302       }
1303       break;
1304     case BO_Div:
1305       assert(!RHSIsComplex);
1306       if (!loadComplexValue(LHSIsComplex, false, ElemIndex, LHSOffset, LHS))
1307         return false;
1308 
1309       if (!loadComplexValue(RHSIsComplex, false, ElemIndex, RHSOffset, RHS))
1310         return false;
1311 
1312       if (ResultElemT == PT_Float) {
1313         if (!this->emitDivf(getFPOptions(E), E))
1314           return false;
1315       } else {
1316         if (!this->emitDiv(ResultElemT, E))
1317           return false;
1318       }
1319       break;
1320 
1321     default:
1322       return false;
1323     }
1324 
1325     if (!this->DiscardResult) {
1326       // Initialize array element with the value we just computed.
1327       if (!this->emitInitElemPop(ResultElemT, ElemIndex, E))
1328         return false;
1329     } else {
1330       if (!this->emitPop(ResultElemT, E))
1331         return false;
1332     }
1333   }
1334   return true;
1335 }
1336 
1337 template <class Emitter>
1338 bool Compiler<Emitter>::VisitVectorBinOp(const BinaryOperator *E) {
1339   assert(!E->isCommaOp() &&
1340          "Comma op should be handled in VisitBinaryOperator");
1341   assert(E->getType()->isVectorType());
1342   assert(E->getLHS()->getType()->isVectorType());
1343   assert(E->getRHS()->getType()->isVectorType());
1344 
1345   // Prepare storage for result.
1346   if (!Initializing && !E->isCompoundAssignmentOp()) {
1347     unsigned LocalIndex = allocateTemporary(E);
1348     if (!this->emitGetPtrLocal(LocalIndex, E))
1349       return false;
1350   }
1351 
1352   const Expr *LHS = E->getLHS();
1353   const Expr *RHS = E->getRHS();
1354   const auto *VecTy = E->getType()->getAs<VectorType>();
1355   auto Op = E->isCompoundAssignmentOp()
1356                 ? BinaryOperator::getOpForCompoundAssignment(E->getOpcode())
1357                 : E->getOpcode();
1358 
1359   PrimType ElemT = this->classifyVectorElementType(LHS->getType());
1360   PrimType RHSElemT = this->classifyVectorElementType(RHS->getType());
1361   PrimType ResultElemT = this->classifyVectorElementType(E->getType());
1362 
1363   // Evaluate LHS and save value to LHSOffset.
1364   unsigned LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
1365   if (!this->visit(LHS))
1366     return false;
1367   if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
1368     return false;
1369 
1370   // Evaluate RHS and save value to RHSOffset.
1371   unsigned RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
1372   if (!this->visit(RHS))
1373     return false;
1374   if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
1375     return false;
1376 
1377   if (E->isCompoundAssignmentOp() && !this->emitGetLocal(PT_Ptr, LHSOffset, E))
1378     return false;
1379 
1380   // BitAdd/BitOr/BitXor/Shl/Shr doesn't support bool type, we need perform the
1381   // integer promotion.
1382   bool NeedIntPromot = ElemT == PT_Bool && (E->isBitwiseOp() || E->isShiftOp());
1383   QualType PromotTy =
1384       Ctx.getASTContext().getPromotedIntegerType(Ctx.getASTContext().BoolTy);
1385   PrimType PromotT = classifyPrim(PromotTy);
1386   PrimType OpT = NeedIntPromot ? PromotT : ElemT;
1387 
1388   auto getElem = [=](unsigned Offset, PrimType ElemT, unsigned Index) {
1389     if (!this->emitGetLocal(PT_Ptr, Offset, E))
1390       return false;
1391     if (!this->emitArrayElemPop(ElemT, Index, E))
1392       return false;
1393     if (E->isLogicalOp()) {
1394       if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
1395         return false;
1396       if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
1397         return false;
1398     } else if (NeedIntPromot) {
1399       if (!this->emitPrimCast(ElemT, PromotT, PromotTy, E))
1400         return false;
1401     }
1402     return true;
1403   };
1404 
1405 #define EMIT_ARITH_OP(OP)                                                      \
1406   {                                                                            \
1407     if (ElemT == PT_Float) {                                                   \
1408       if (!this->emit##OP##f(getFPOptions(E), E))                              \
1409         return false;                                                          \
1410     } else {                                                                   \
1411       if (!this->emit##OP(ElemT, E))                                           \
1412         return false;                                                          \
1413     }                                                                          \
1414     break;                                                                     \
1415   }
1416 
1417   for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
1418     if (!getElem(LHSOffset, ElemT, I))
1419       return false;
1420     if (!getElem(RHSOffset, RHSElemT, I))
1421       return false;
1422     switch (Op) {
1423     case BO_Add:
1424       EMIT_ARITH_OP(Add)
1425     case BO_Sub:
1426       EMIT_ARITH_OP(Sub)
1427     case BO_Mul:
1428       EMIT_ARITH_OP(Mul)
1429     case BO_Div:
1430       EMIT_ARITH_OP(Div)
1431     case BO_Rem:
1432       if (!this->emitRem(ElemT, E))
1433         return false;
1434       break;
1435     case BO_And:
1436       if (!this->emitBitAnd(OpT, E))
1437         return false;
1438       break;
1439     case BO_Or:
1440       if (!this->emitBitOr(OpT, E))
1441         return false;
1442       break;
1443     case BO_Xor:
1444       if (!this->emitBitXor(OpT, E))
1445         return false;
1446       break;
1447     case BO_Shl:
1448       if (!this->emitShl(OpT, RHSElemT, E))
1449         return false;
1450       break;
1451     case BO_Shr:
1452       if (!this->emitShr(OpT, RHSElemT, E))
1453         return false;
1454       break;
1455     case BO_EQ:
1456       if (!this->emitEQ(ElemT, E))
1457         return false;
1458       break;
1459     case BO_NE:
1460       if (!this->emitNE(ElemT, E))
1461         return false;
1462       break;
1463     case BO_LE:
1464       if (!this->emitLE(ElemT, E))
1465         return false;
1466       break;
1467     case BO_LT:
1468       if (!this->emitLT(ElemT, E))
1469         return false;
1470       break;
1471     case BO_GE:
1472       if (!this->emitGE(ElemT, E))
1473         return false;
1474       break;
1475     case BO_GT:
1476       if (!this->emitGT(ElemT, E))
1477         return false;
1478       break;
1479     case BO_LAnd:
1480       // a && b is equivalent to a!=0 & b!=0
1481       if (!this->emitBitAnd(ResultElemT, E))
1482         return false;
1483       break;
1484     case BO_LOr:
1485       // a || b is equivalent to a!=0 | b!=0
1486       if (!this->emitBitOr(ResultElemT, E))
1487         return false;
1488       break;
1489     default:
1490       return this->emitInvalid(E);
1491     }
1492 
1493     // The result of the comparison is a vector of the same width and number
1494     // of elements as the comparison operands with a signed integral element
1495     // type.
1496     //
1497     // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
1498     if (E->isComparisonOp()) {
1499       if (!this->emitPrimCast(PT_Bool, ResultElemT, VecTy->getElementType(), E))
1500         return false;
1501       if (!this->emitNeg(ResultElemT, E))
1502         return false;
1503     }
1504 
1505     // If we performed an integer promotion, we need to cast the compute result
1506     // into result vector element type.
1507     if (NeedIntPromot &&
1508         !this->emitPrimCast(PromotT, ResultElemT, VecTy->getElementType(), E))
1509       return false;
1510 
1511     // Initialize array element with the value we just computed.
1512     if (!this->emitInitElem(ResultElemT, I, E))
1513       return false;
1514   }
1515 
1516   if (DiscardResult && E->isCompoundAssignmentOp() && !this->emitPopPtr(E))
1517     return false;
1518   return true;
1519 }
1520 
1521 template <class Emitter>
1522 bool Compiler<Emitter>::VisitFixedPointBinOp(const BinaryOperator *E) {
1523   const Expr *LHS = E->getLHS();
1524   const Expr *RHS = E->getRHS();
1525 
1526   assert(LHS->getType()->isFixedPointType() ||
1527          RHS->getType()->isFixedPointType());
1528 
1529   auto LHSSema = Ctx.getASTContext().getFixedPointSemantics(LHS->getType());
1530   auto RHSSema = Ctx.getASTContext().getFixedPointSemantics(RHS->getType());
1531 
1532   if (!this->visit(LHS))
1533     return false;
1534   if (!LHS->getType()->isFixedPointType()) {
1535     uint32_t I;
1536     std::memcpy(&I, &LHSSema, sizeof(llvm::FixedPointSemantics));
1537     if (!this->emitCastIntegralFixedPoint(classifyPrim(LHS->getType()), I, E))
1538       return false;
1539   }
1540 
1541   if (!this->visit(RHS))
1542     return false;
1543   if (!RHS->getType()->isFixedPointType()) {
1544     uint32_t I;
1545     std::memcpy(&I, &RHSSema, sizeof(llvm::FixedPointSemantics));
1546     if (!this->emitCastIntegralFixedPoint(classifyPrim(RHS->getType()), I, E))
1547       return false;
1548   }
1549 
1550   // Convert the result to the target semantics.
1551   auto ConvertResult = [&](bool R) -> bool {
1552     if (!R)
1553       return false;
1554     auto ResultSema = Ctx.getASTContext().getFixedPointSemantics(E->getType());
1555     auto CommonSema = LHSSema.getCommonSemantics(RHSSema);
1556     if (ResultSema != CommonSema) {
1557       uint32_t I;
1558       std::memcpy(&I, &ResultSema, sizeof(ResultSema));
1559       return this->emitCastFixedPoint(I, E);
1560     }
1561     return true;
1562   };
1563 
1564   auto MaybeCastToBool = [&](bool Result) {
1565     if (!Result)
1566       return false;
1567     PrimType T = classifyPrim(E);
1568     if (DiscardResult)
1569       return this->emitPop(T, E);
1570     if (T != PT_Bool)
1571       return this->emitCast(PT_Bool, T, E);
1572     return true;
1573   };
1574 
1575   switch (E->getOpcode()) {
1576   case BO_EQ:
1577     return MaybeCastToBool(this->emitEQFixedPoint(E));
1578   case BO_NE:
1579     return MaybeCastToBool(this->emitNEFixedPoint(E));
1580   case BO_LT:
1581     return MaybeCastToBool(this->emitLTFixedPoint(E));
1582   case BO_LE:
1583     return MaybeCastToBool(this->emitLEFixedPoint(E));
1584   case BO_GT:
1585     return MaybeCastToBool(this->emitGTFixedPoint(E));
1586   case BO_GE:
1587     return MaybeCastToBool(this->emitGEFixedPoint(E));
1588   case BO_Add:
1589     return ConvertResult(this->emitAddFixedPoint(E));
1590   case BO_Sub:
1591     return ConvertResult(this->emitSubFixedPoint(E));
1592   case BO_Mul:
1593     return ConvertResult(this->emitMulFixedPoint(E));
1594   case BO_Div:
1595     return ConvertResult(this->emitDivFixedPoint(E));
1596   case BO_Shl:
1597     return ConvertResult(this->emitShiftFixedPoint(/*Left=*/true, E));
1598   case BO_Shr:
1599     return ConvertResult(this->emitShiftFixedPoint(/*Left=*/false, E));
1600 
1601   default:
1602     return this->emitInvalid(E);
1603   }
1604 
1605   llvm_unreachable("unhandled binop opcode");
1606 }
1607 
1608 template <class Emitter>
1609 bool Compiler<Emitter>::VisitFixedPointUnaryOperator(const UnaryOperator *E) {
1610   const Expr *SubExpr = E->getSubExpr();
1611   assert(SubExpr->getType()->isFixedPointType());
1612 
1613   switch (E->getOpcode()) {
1614   case UO_Plus:
1615     return this->delegate(SubExpr);
1616   case UO_Minus:
1617     if (!this->visit(SubExpr))
1618       return false;
1619     return this->emitNegFixedPoint(E);
1620   default:
1621     return false;
1622   }
1623 
1624   llvm_unreachable("Unhandled unary opcode");
1625 }
1626 
1627 template <class Emitter>
1628 bool Compiler<Emitter>::VisitImplicitValueInitExpr(
1629     const ImplicitValueInitExpr *E) {
1630   QualType QT = E->getType();
1631 
1632   if (std::optional<PrimType> T = classify(QT))
1633     return this->visitZeroInitializer(*T, QT, E);
1634 
1635   if (QT->isRecordType()) {
1636     const RecordDecl *RD = QT->getAsRecordDecl();
1637     assert(RD);
1638     if (RD->isInvalidDecl())
1639       return false;
1640 
1641     if (const auto *CXXRD = dyn_cast<CXXRecordDecl>(RD);
1642         CXXRD && CXXRD->getNumVBases() > 0) {
1643       // TODO: Diagnose.
1644       return false;
1645     }
1646 
1647     const Record *R = getRecord(QT);
1648     if (!R)
1649       return false;
1650 
1651     assert(Initializing);
1652     return this->visitZeroRecordInitializer(R, E);
1653   }
1654 
1655   if (QT->isIncompleteArrayType())
1656     return true;
1657 
1658   if (QT->isArrayType())
1659     return this->visitZeroArrayInitializer(QT, E);
1660 
1661   if (const auto *ComplexTy = E->getType()->getAs<ComplexType>()) {
1662     assert(Initializing);
1663     QualType ElemQT = ComplexTy->getElementType();
1664     PrimType ElemT = classifyPrim(ElemQT);
1665     for (unsigned I = 0; I < 2; ++I) {
1666       if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1667         return false;
1668       if (!this->emitInitElem(ElemT, I, E))
1669         return false;
1670     }
1671     return true;
1672   }
1673 
1674   if (const auto *VecT = E->getType()->getAs<VectorType>()) {
1675     unsigned NumVecElements = VecT->getNumElements();
1676     QualType ElemQT = VecT->getElementType();
1677     PrimType ElemT = classifyPrim(ElemQT);
1678 
1679     for (unsigned I = 0; I < NumVecElements; ++I) {
1680       if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1681         return false;
1682       if (!this->emitInitElem(ElemT, I, E))
1683         return false;
1684     }
1685     return true;
1686   }
1687 
1688   return false;
1689 }
1690 
1691 template <class Emitter>
1692 bool Compiler<Emitter>::VisitArraySubscriptExpr(const ArraySubscriptExpr *E) {
1693   const Expr *LHS = E->getLHS();
1694   const Expr *RHS = E->getRHS();
1695   const Expr *Index = E->getIdx();
1696 
1697   if (DiscardResult)
1698     return this->discard(LHS) && this->discard(RHS);
1699 
1700   // C++17's rules require us to evaluate the LHS first, regardless of which
1701   // side is the base.
1702   bool Success = true;
1703   for (const Expr *SubExpr : {LHS, RHS}) {
1704     if (!this->visit(SubExpr))
1705       Success = false;
1706   }
1707 
1708   if (!Success)
1709     return false;
1710 
1711   PrimType IndexT = classifyPrim(Index->getType());
1712   // If the index is first, we need to change that.
1713   if (LHS == Index) {
1714     if (!this->emitFlip(PT_Ptr, IndexT, E))
1715       return false;
1716   }
1717 
1718   return this->emitArrayElemPtrPop(IndexT, E);
1719 }
1720 
1721 template <class Emitter>
1722 bool Compiler<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
1723                                       const Expr *ArrayFiller, const Expr *E) {
1724   InitLinkScope<Emitter> ILS(this, InitLink::InitList());
1725 
1726   QualType QT = E->getType();
1727   if (const auto *AT = QT->getAs<AtomicType>())
1728     QT = AT->getValueType();
1729 
1730   if (QT->isVoidType()) {
1731     if (Inits.size() == 0)
1732       return true;
1733     return this->emitInvalid(E);
1734   }
1735 
1736   // Handle discarding first.
1737   if (DiscardResult) {
1738     for (const Expr *Init : Inits) {
1739       if (!this->discard(Init))
1740         return false;
1741     }
1742     return true;
1743   }
1744 
1745   // Primitive values.
1746   if (std::optional<PrimType> T = classify(QT)) {
1747     assert(!DiscardResult);
1748     if (Inits.size() == 0)
1749       return this->visitZeroInitializer(*T, QT, E);
1750     assert(Inits.size() == 1);
1751     return this->delegate(Inits[0]);
1752   }
1753 
1754   if (QT->isRecordType()) {
1755     const Record *R = getRecord(QT);
1756 
1757     if (Inits.size() == 1 && E->getType() == Inits[0]->getType())
1758       return this->delegate(Inits[0]);
1759 
1760     auto initPrimitiveField = [=](const Record::Field *FieldToInit,
1761                                   const Expr *Init, PrimType T) -> bool {
1762       InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
1763       InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
1764       if (!this->visit(Init))
1765         return false;
1766 
1767       if (FieldToInit->isBitField())
1768         return this->emitInitBitField(T, FieldToInit, E);
1769       return this->emitInitField(T, FieldToInit->Offset, E);
1770     };
1771 
1772     auto initCompositeField = [=](const Record::Field *FieldToInit,
1773                                   const Expr *Init) -> bool {
1774       InitStackScope<Emitter> ISS(this, isa<CXXDefaultInitExpr>(Init));
1775       InitLinkScope<Emitter> ILS(this, InitLink::Field(FieldToInit->Offset));
1776 
1777       // Non-primitive case. Get a pointer to the field-to-initialize
1778       // on the stack and recurse into visitInitializer().
1779       if (!this->emitGetPtrField(FieldToInit->Offset, Init))
1780         return false;
1781       if (!this->visitInitializer(Init))
1782         return false;
1783       return this->emitPopPtr(E);
1784     };
1785 
1786     if (R->isUnion()) {
1787       if (Inits.size() == 0) {
1788         if (!this->visitZeroRecordInitializer(R, E))
1789           return false;
1790       } else {
1791         const Expr *Init = Inits[0];
1792         const FieldDecl *FToInit = nullptr;
1793         if (const auto *ILE = dyn_cast<InitListExpr>(E))
1794           FToInit = ILE->getInitializedFieldInUnion();
1795         else
1796           FToInit = cast<CXXParenListInitExpr>(E)->getInitializedFieldInUnion();
1797 
1798         const Record::Field *FieldToInit = R->getField(FToInit);
1799         if (std::optional<PrimType> T = classify(Init)) {
1800           if (!initPrimitiveField(FieldToInit, Init, *T))
1801             return false;
1802         } else {
1803           if (!initCompositeField(FieldToInit, Init))
1804             return false;
1805         }
1806       }
1807       return this->emitFinishInit(E);
1808     }
1809 
1810     assert(!R->isUnion());
1811     unsigned InitIndex = 0;
1812     for (const Expr *Init : Inits) {
1813       // Skip unnamed bitfields.
1814       while (InitIndex < R->getNumFields() &&
1815              R->getField(InitIndex)->Decl->isUnnamedBitField())
1816         ++InitIndex;
1817 
1818       if (std::optional<PrimType> T = classify(Init)) {
1819         const Record::Field *FieldToInit = R->getField(InitIndex);
1820         if (!initPrimitiveField(FieldToInit, Init, *T))
1821           return false;
1822         ++InitIndex;
1823       } else {
1824         // Initializer for a direct base class.
1825         if (const Record::Base *B = R->getBase(Init->getType())) {
1826           if (!this->emitGetPtrBase(B->Offset, Init))
1827             return false;
1828 
1829           if (!this->visitInitializer(Init))
1830             return false;
1831 
1832           if (!this->emitFinishInitPop(E))
1833             return false;
1834           // Base initializers don't increase InitIndex, since they don't count
1835           // into the Record's fields.
1836         } else {
1837           const Record::Field *FieldToInit = R->getField(InitIndex);
1838           if (!initCompositeField(FieldToInit, Init))
1839             return false;
1840           ++InitIndex;
1841         }
1842       }
1843     }
1844     return this->emitFinishInit(E);
1845   }
1846 
1847   if (QT->isArrayType()) {
1848     if (Inits.size() == 1 && QT == Inits[0]->getType())
1849       return this->delegate(Inits[0]);
1850 
1851     unsigned ElementIndex = 0;
1852     for (const Expr *Init : Inits) {
1853       if (const auto *EmbedS =
1854               dyn_cast<EmbedExpr>(Init->IgnoreParenImpCasts())) {
1855         PrimType TargetT = classifyPrim(Init->getType());
1856 
1857         auto Eval = [&](const Expr *Init, unsigned ElemIndex) {
1858           PrimType InitT = classifyPrim(Init->getType());
1859           if (!this->visit(Init))
1860             return false;
1861           if (InitT != TargetT) {
1862             if (!this->emitCast(InitT, TargetT, E))
1863               return false;
1864           }
1865           return this->emitInitElem(TargetT, ElemIndex, Init);
1866         };
1867         if (!EmbedS->doForEachDataElement(Eval, ElementIndex))
1868           return false;
1869       } else {
1870         if (!this->visitArrayElemInit(ElementIndex, Init))
1871           return false;
1872         ++ElementIndex;
1873       }
1874     }
1875 
1876     // Expand the filler expression.
1877     // FIXME: This should go away.
1878     if (ArrayFiller) {
1879       const ConstantArrayType *CAT =
1880           Ctx.getASTContext().getAsConstantArrayType(QT);
1881       uint64_t NumElems = CAT->getZExtSize();
1882 
1883       for (; ElementIndex != NumElems; ++ElementIndex) {
1884         if (!this->visitArrayElemInit(ElementIndex, ArrayFiller))
1885           return false;
1886       }
1887     }
1888 
1889     return this->emitFinishInit(E);
1890   }
1891 
1892   if (const auto *ComplexTy = QT->getAs<ComplexType>()) {
1893     unsigned NumInits = Inits.size();
1894 
1895     if (NumInits == 1)
1896       return this->delegate(Inits[0]);
1897 
1898     QualType ElemQT = ComplexTy->getElementType();
1899     PrimType ElemT = classifyPrim(ElemQT);
1900     if (NumInits == 0) {
1901       // Zero-initialize both elements.
1902       for (unsigned I = 0; I < 2; ++I) {
1903         if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1904           return false;
1905         if (!this->emitInitElem(ElemT, I, E))
1906           return false;
1907       }
1908     } else if (NumInits == 2) {
1909       unsigned InitIndex = 0;
1910       for (const Expr *Init : Inits) {
1911         if (!this->visit(Init))
1912           return false;
1913 
1914         if (!this->emitInitElem(ElemT, InitIndex, E))
1915           return false;
1916         ++InitIndex;
1917       }
1918     }
1919     return true;
1920   }
1921 
1922   if (const auto *VecT = QT->getAs<VectorType>()) {
1923     unsigned NumVecElements = VecT->getNumElements();
1924     assert(NumVecElements >= Inits.size());
1925 
1926     QualType ElemQT = VecT->getElementType();
1927     PrimType ElemT = classifyPrim(ElemQT);
1928 
1929     // All initializer elements.
1930     unsigned InitIndex = 0;
1931     for (const Expr *Init : Inits) {
1932       if (!this->visit(Init))
1933         return false;
1934 
1935       // If the initializer is of vector type itself, we have to deconstruct
1936       // that and initialize all the target fields from the initializer fields.
1937       if (const auto *InitVecT = Init->getType()->getAs<VectorType>()) {
1938         if (!this->emitCopyArray(ElemT, 0, InitIndex,
1939                                  InitVecT->getNumElements(), E))
1940           return false;
1941         InitIndex += InitVecT->getNumElements();
1942       } else {
1943         if (!this->emitInitElem(ElemT, InitIndex, E))
1944           return false;
1945         ++InitIndex;
1946       }
1947     }
1948 
1949     assert(InitIndex <= NumVecElements);
1950 
1951     // Fill the rest with zeroes.
1952     for (; InitIndex != NumVecElements; ++InitIndex) {
1953       if (!this->visitZeroInitializer(ElemT, ElemQT, E))
1954         return false;
1955       if (!this->emitInitElem(ElemT, InitIndex, E))
1956         return false;
1957     }
1958     return true;
1959   }
1960 
1961   return false;
1962 }
1963 
1964 /// Pointer to the array(not the element!) must be on the stack when calling
1965 /// this.
1966 template <class Emitter>
1967 bool Compiler<Emitter>::visitArrayElemInit(unsigned ElemIndex,
1968                                            const Expr *Init) {
1969   if (std::optional<PrimType> T = classify(Init->getType())) {
1970     // Visit the primitive element like normal.
1971     if (!this->visit(Init))
1972       return false;
1973     return this->emitInitElem(*T, ElemIndex, Init);
1974   }
1975 
1976   InitLinkScope<Emitter> ILS(this, InitLink::Elem(ElemIndex));
1977   // Advance the pointer currently on the stack to the given
1978   // dimension.
1979   if (!this->emitConstUint32(ElemIndex, Init))
1980     return false;
1981   if (!this->emitArrayElemPtrUint32(Init))
1982     return false;
1983   if (!this->visitInitializer(Init))
1984     return false;
1985   return this->emitFinishInitPop(Init);
1986 }
1987 
1988 template <class Emitter>
1989 bool Compiler<Emitter>::VisitInitListExpr(const InitListExpr *E) {
1990   return this->visitInitList(E->inits(), E->getArrayFiller(), E);
1991 }
1992 
1993 template <class Emitter>
1994 bool Compiler<Emitter>::VisitCXXParenListInitExpr(
1995     const CXXParenListInitExpr *E) {
1996   return this->visitInitList(E->getInitExprs(), E->getArrayFiller(), E);
1997 }
1998 
1999 template <class Emitter>
2000 bool Compiler<Emitter>::VisitSubstNonTypeTemplateParmExpr(
2001     const SubstNonTypeTemplateParmExpr *E) {
2002   return this->delegate(E->getReplacement());
2003 }
2004 
2005 template <class Emitter>
2006 bool Compiler<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
2007   std::optional<PrimType> T = classify(E->getType());
2008   if (T && E->hasAPValueResult()) {
2009     // Try to emit the APValue directly, without visiting the subexpr.
2010     // This will only fail if we can't emit the APValue, so won't emit any
2011     // diagnostics or any double values.
2012     if (DiscardResult)
2013       return true;
2014 
2015     if (this->visitAPValue(E->getAPValueResult(), *T, E))
2016       return true;
2017   }
2018   return this->delegate(E->getSubExpr());
2019 }
2020 
2021 template <class Emitter>
2022 bool Compiler<Emitter>::VisitEmbedExpr(const EmbedExpr *E) {
2023   auto It = E->begin();
2024   return this->visit(*It);
2025 }
2026 
2027 static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx,
2028                              UnaryExprOrTypeTrait Kind) {
2029   bool AlignOfReturnsPreferred =
2030       ASTCtx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7;
2031 
2032   // C++ [expr.alignof]p3:
2033   //     When alignof is applied to a reference type, the result is the
2034   //     alignment of the referenced type.
2035   if (const auto *Ref = T->getAs<ReferenceType>())
2036     T = Ref->getPointeeType();
2037 
2038   if (T.getQualifiers().hasUnaligned())
2039     return CharUnits::One();
2040 
2041   // __alignof is defined to return the preferred alignment.
2042   // Before 8, clang returned the preferred alignment for alignof and
2043   // _Alignof as well.
2044   if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
2045     return ASTCtx.toCharUnitsFromBits(ASTCtx.getPreferredTypeAlign(T));
2046 
2047   return ASTCtx.getTypeAlignInChars(T);
2048 }
2049 
2050 template <class Emitter>
2051 bool Compiler<Emitter>::VisitUnaryExprOrTypeTraitExpr(
2052     const UnaryExprOrTypeTraitExpr *E) {
2053   UnaryExprOrTypeTrait Kind = E->getKind();
2054   const ASTContext &ASTCtx = Ctx.getASTContext();
2055 
2056   if (Kind == UETT_SizeOf || Kind == UETT_DataSizeOf) {
2057     QualType ArgType = E->getTypeOfArgument();
2058 
2059     // C++ [expr.sizeof]p2: "When applied to a reference or a reference type,
2060     //   the result is the size of the referenced type."
2061     if (const auto *Ref = ArgType->getAs<ReferenceType>())
2062       ArgType = Ref->getPointeeType();
2063 
2064     CharUnits Size;
2065     if (ArgType->isVoidType() || ArgType->isFunctionType())
2066       Size = CharUnits::One();
2067     else {
2068       if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
2069         return false;
2070 
2071       if (Kind == UETT_SizeOf)
2072         Size = ASTCtx.getTypeSizeInChars(ArgType);
2073       else
2074         Size = ASTCtx.getTypeInfoDataSizeInChars(ArgType).Width;
2075     }
2076 
2077     if (DiscardResult)
2078       return true;
2079 
2080     return this->emitConst(Size.getQuantity(), E);
2081   }
2082 
2083   if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) {
2084     CharUnits Size;
2085 
2086     if (E->isArgumentType()) {
2087       QualType ArgType = E->getTypeOfArgument();
2088 
2089       Size = AlignOfType(ArgType, ASTCtx, Kind);
2090     } else {
2091       // Argument is an expression, not a type.
2092       const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
2093 
2094       // The kinds of expressions that we have special-case logic here for
2095       // should be kept up to date with the special checks for those
2096       // expressions in Sema.
2097 
2098       // alignof decl is always accepted, even if it doesn't make sense: we
2099       // default to 1 in those cases.
2100       if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg))
2101         Size = ASTCtx.getDeclAlign(DRE->getDecl(),
2102                                    /*RefAsPointee*/ true);
2103       else if (const auto *ME = dyn_cast<MemberExpr>(Arg))
2104         Size = ASTCtx.getDeclAlign(ME->getMemberDecl(),
2105                                    /*RefAsPointee*/ true);
2106       else
2107         Size = AlignOfType(Arg->getType(), ASTCtx, Kind);
2108     }
2109 
2110     if (DiscardResult)
2111       return true;
2112 
2113     return this->emitConst(Size.getQuantity(), E);
2114   }
2115 
2116   if (Kind == UETT_VectorElements) {
2117     if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>())
2118       return this->emitConst(VT->getNumElements(), E);
2119     assert(E->getTypeOfArgument()->isSizelessVectorType());
2120     return this->emitSizelessVectorElementSize(E);
2121   }
2122 
2123   if (Kind == UETT_VecStep) {
2124     if (const auto *VT = E->getTypeOfArgument()->getAs<VectorType>()) {
2125       unsigned N = VT->getNumElements();
2126 
2127       // The vec_step built-in functions that take a 3-component
2128       // vector return 4. (OpenCL 1.1 spec 6.11.12)
2129       if (N == 3)
2130         N = 4;
2131 
2132       return this->emitConst(N, E);
2133     }
2134     return this->emitConst(1, E);
2135   }
2136 
2137   if (Kind == UETT_OpenMPRequiredSimdAlign) {
2138     assert(E->isArgumentType());
2139     unsigned Bits = ASTCtx.getOpenMPDefaultSimdAlign(E->getArgumentType());
2140 
2141     return this->emitConst(ASTCtx.toCharUnitsFromBits(Bits).getQuantity(), E);
2142   }
2143 
2144   return false;
2145 }
2146 
2147 template <class Emitter>
2148 bool Compiler<Emitter>::VisitMemberExpr(const MemberExpr *E) {
2149   // 'Base.Member'
2150   const Expr *Base = E->getBase();
2151   const ValueDecl *Member = E->getMemberDecl();
2152 
2153   if (DiscardResult)
2154     return this->discard(Base);
2155 
2156   // MemberExprs are almost always lvalues, in which case we don't need to
2157   // do the load. But sometimes they aren't.
2158   const auto maybeLoadValue = [&]() -> bool {
2159     if (E->isGLValue())
2160       return true;
2161     if (std::optional<PrimType> T = classify(E))
2162       return this->emitLoadPop(*T, E);
2163     return false;
2164   };
2165 
2166   if (const auto *VD = dyn_cast<VarDecl>(Member)) {
2167     // I am almost confident in saying that a var decl must be static
2168     // and therefore registered as a global variable. But this will probably
2169     // turn out to be wrong some time in the future, as always.
2170     if (auto GlobalIndex = P.getGlobal(VD))
2171       return this->emitGetPtrGlobal(*GlobalIndex, E) && maybeLoadValue();
2172     return false;
2173   }
2174 
2175   if (!isa<FieldDecl>(Member)) {
2176     if (!this->discard(Base) && !this->emitSideEffect(E))
2177       return false;
2178 
2179     return this->visitDeclRef(Member, E);
2180   }
2181 
2182   if (Initializing) {
2183     if (!this->delegate(Base))
2184       return false;
2185   } else {
2186     if (!this->visit(Base))
2187       return false;
2188   }
2189 
2190   // Base above gives us a pointer on the stack.
2191   const auto *FD = cast<FieldDecl>(Member);
2192   const RecordDecl *RD = FD->getParent();
2193   const Record *R = getRecord(RD);
2194   if (!R)
2195     return false;
2196   const Record::Field *F = R->getField(FD);
2197   // Leave a pointer to the field on the stack.
2198   if (F->Decl->getType()->isReferenceType())
2199     return this->emitGetFieldPop(PT_Ptr, F->Offset, E) && maybeLoadValue();
2200   return this->emitGetPtrFieldPop(F->Offset, E) && maybeLoadValue();
2201 }
2202 
2203 template <class Emitter>
2204 bool Compiler<Emitter>::VisitArrayInitIndexExpr(const ArrayInitIndexExpr *E) {
2205   // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
2206   // stand-alone, e.g. via EvaluateAsInt().
2207   if (!ArrayIndex)
2208     return false;
2209   return this->emitConst(*ArrayIndex, E);
2210 }
2211 
2212 template <class Emitter>
2213 bool Compiler<Emitter>::VisitArrayInitLoopExpr(const ArrayInitLoopExpr *E) {
2214   assert(Initializing);
2215   assert(!DiscardResult);
2216 
2217   // We visit the common opaque expression here once so we have its value
2218   // cached.
2219   if (!this->discard(E->getCommonExpr()))
2220     return false;
2221 
2222   // TODO: This compiles to quite a lot of bytecode if the array is larger.
2223   //   Investigate compiling this to a loop.
2224   const Expr *SubExpr = E->getSubExpr();
2225   size_t Size = E->getArraySize().getZExtValue();
2226 
2227   // So, every iteration, we execute an assignment here
2228   // where the LHS is on the stack (the target array)
2229   // and the RHS is our SubExpr.
2230   for (size_t I = 0; I != Size; ++I) {
2231     ArrayIndexScope<Emitter> IndexScope(this, I);
2232     BlockScope<Emitter> BS(this);
2233 
2234     if (!this->visitArrayElemInit(I, SubExpr))
2235       return false;
2236     if (!BS.destroyLocals())
2237       return false;
2238   }
2239   return true;
2240 }
2241 
2242 template <class Emitter>
2243 bool Compiler<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
2244   const Expr *SourceExpr = E->getSourceExpr();
2245   if (!SourceExpr)
2246     return false;
2247 
2248   if (Initializing)
2249     return this->visitInitializer(SourceExpr);
2250 
2251   PrimType SubExprT = classify(SourceExpr).value_or(PT_Ptr);
2252   if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end())
2253     return this->emitGetLocal(SubExprT, It->second, E);
2254 
2255   if (!this->visit(SourceExpr))
2256     return false;
2257 
2258   // At this point we either have the evaluated source expression or a pointer
2259   // to an object on the stack. We want to create a local variable that stores
2260   // this value.
2261   unsigned LocalIndex = allocateLocalPrimitive(E, SubExprT, /*IsConst=*/true);
2262   if (!this->emitSetLocal(SubExprT, LocalIndex, E))
2263     return false;
2264 
2265   // Here the local variable is created but the value is removed from the stack,
2266   // so we put it back if the caller needs it.
2267   if (!DiscardResult) {
2268     if (!this->emitGetLocal(SubExprT, LocalIndex, E))
2269       return false;
2270   }
2271 
2272   // This is cleaned up when the local variable is destroyed.
2273   OpaqueExprs.insert({E, LocalIndex});
2274 
2275   return true;
2276 }
2277 
2278 template <class Emitter>
2279 bool Compiler<Emitter>::VisitAbstractConditionalOperator(
2280     const AbstractConditionalOperator *E) {
2281   const Expr *Condition = E->getCond();
2282   const Expr *TrueExpr = E->getTrueExpr();
2283   const Expr *FalseExpr = E->getFalseExpr();
2284 
2285   LabelTy LabelEnd = this->getLabel();   // Label after the operator.
2286   LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
2287 
2288   if (!this->visitBool(Condition))
2289     return false;
2290 
2291   if (!this->jumpFalse(LabelFalse))
2292     return false;
2293 
2294   {
2295     LocalScope<Emitter> S(this);
2296     if (!this->delegate(TrueExpr))
2297       return false;
2298     if (!S.destroyLocals())
2299       return false;
2300   }
2301 
2302   if (!this->jump(LabelEnd))
2303     return false;
2304 
2305   this->emitLabel(LabelFalse);
2306 
2307   {
2308     LocalScope<Emitter> S(this);
2309     if (!this->delegate(FalseExpr))
2310       return false;
2311     if (!S.destroyLocals())
2312       return false;
2313   }
2314 
2315   this->fallthrough(LabelEnd);
2316   this->emitLabel(LabelEnd);
2317 
2318   return true;
2319 }
2320 
2321 template <class Emitter>
2322 bool Compiler<Emitter>::VisitStringLiteral(const StringLiteral *E) {
2323   if (DiscardResult)
2324     return true;
2325 
2326   if (!Initializing) {
2327     unsigned StringIndex = P.createGlobalString(E);
2328     return this->emitGetPtrGlobal(StringIndex, E);
2329   }
2330 
2331   // We are initializing an array on the stack.
2332   const ConstantArrayType *CAT =
2333       Ctx.getASTContext().getAsConstantArrayType(E->getType());
2334   assert(CAT && "a string literal that's not a constant array?");
2335 
2336   // If the initializer string is too long, a diagnostic has already been
2337   // emitted. Read only the array length from the string literal.
2338   unsigned ArraySize = CAT->getZExtSize();
2339   unsigned N = std::min(ArraySize, E->getLength());
2340   size_t CharWidth = E->getCharByteWidth();
2341 
2342   for (unsigned I = 0; I != N; ++I) {
2343     uint32_t CodeUnit = E->getCodeUnit(I);
2344 
2345     if (CharWidth == 1) {
2346       this->emitConstSint8(CodeUnit, E);
2347       this->emitInitElemSint8(I, E);
2348     } else if (CharWidth == 2) {
2349       this->emitConstUint16(CodeUnit, E);
2350       this->emitInitElemUint16(I, E);
2351     } else if (CharWidth == 4) {
2352       this->emitConstUint32(CodeUnit, E);
2353       this->emitInitElemUint32(I, E);
2354     } else {
2355       llvm_unreachable("unsupported character width");
2356     }
2357   }
2358 
2359   // Fill up the rest of the char array with NUL bytes.
2360   for (unsigned I = N; I != ArraySize; ++I) {
2361     if (CharWidth == 1) {
2362       this->emitConstSint8(0, E);
2363       this->emitInitElemSint8(I, E);
2364     } else if (CharWidth == 2) {
2365       this->emitConstUint16(0, E);
2366       this->emitInitElemUint16(I, E);
2367     } else if (CharWidth == 4) {
2368       this->emitConstUint32(0, E);
2369       this->emitInitElemUint32(I, E);
2370     } else {
2371       llvm_unreachable("unsupported character width");
2372     }
2373   }
2374 
2375   return true;
2376 }
2377 
2378 template <class Emitter>
2379 bool Compiler<Emitter>::VisitObjCStringLiteral(const ObjCStringLiteral *E) {
2380   if (DiscardResult)
2381     return true;
2382   return this->emitDummyPtr(E, E);
2383 }
2384 
2385 template <class Emitter>
2386 bool Compiler<Emitter>::VisitObjCEncodeExpr(const ObjCEncodeExpr *E) {
2387   auto &A = Ctx.getASTContext();
2388   std::string Str;
2389   A.getObjCEncodingForType(E->getEncodedType(), Str);
2390   StringLiteral *SL =
2391       StringLiteral::Create(A, Str, StringLiteralKind::Ordinary,
2392                             /*Pascal=*/false, E->getType(), E->getAtLoc());
2393   return this->delegate(SL);
2394 }
2395 
2396 template <class Emitter>
2397 bool Compiler<Emitter>::VisitSYCLUniqueStableNameExpr(
2398     const SYCLUniqueStableNameExpr *E) {
2399   if (DiscardResult)
2400     return true;
2401 
2402   assert(!Initializing);
2403 
2404   auto &A = Ctx.getASTContext();
2405   std::string ResultStr = E->ComputeName(A);
2406 
2407   QualType CharTy = A.CharTy.withConst();
2408   APInt Size(A.getTypeSize(A.getSizeType()), ResultStr.size() + 1);
2409   QualType ArrayTy = A.getConstantArrayType(CharTy, Size, nullptr,
2410                                             ArraySizeModifier::Normal, 0);
2411 
2412   StringLiteral *SL =
2413       StringLiteral::Create(A, ResultStr, StringLiteralKind::Ordinary,
2414                             /*Pascal=*/false, ArrayTy, E->getLocation());
2415 
2416   unsigned StringIndex = P.createGlobalString(SL);
2417   return this->emitGetPtrGlobal(StringIndex, E);
2418 }
2419 
2420 template <class Emitter>
2421 bool Compiler<Emitter>::VisitCharacterLiteral(const CharacterLiteral *E) {
2422   if (DiscardResult)
2423     return true;
2424   return this->emitConst(E->getValue(), E);
2425 }
2426 
2427 template <class Emitter>
2428 bool Compiler<Emitter>::VisitFloatCompoundAssignOperator(
2429     const CompoundAssignOperator *E) {
2430 
2431   const Expr *LHS = E->getLHS();
2432   const Expr *RHS = E->getRHS();
2433   QualType LHSType = LHS->getType();
2434   QualType LHSComputationType = E->getComputationLHSType();
2435   QualType ResultType = E->getComputationResultType();
2436   std::optional<PrimType> LT = classify(LHSComputationType);
2437   std::optional<PrimType> RT = classify(ResultType);
2438 
2439   assert(ResultType->isFloatingType());
2440 
2441   if (!LT || !RT)
2442     return false;
2443 
2444   PrimType LHST = classifyPrim(LHSType);
2445 
2446   // C++17 onwards require that we evaluate the RHS first.
2447   // Compute RHS and save it in a temporary variable so we can
2448   // load it again later.
2449   if (!visit(RHS))
2450     return false;
2451 
2452   unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
2453   if (!this->emitSetLocal(*RT, TempOffset, E))
2454     return false;
2455 
2456   // First, visit LHS.
2457   if (!visit(LHS))
2458     return false;
2459   if (!this->emitLoad(LHST, E))
2460     return false;
2461 
2462   // If necessary, convert LHS to its computation type.
2463   if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
2464                           LHSComputationType, E))
2465     return false;
2466 
2467   // Now load RHS.
2468   if (!this->emitGetLocal(*RT, TempOffset, E))
2469     return false;
2470 
2471   switch (E->getOpcode()) {
2472   case BO_AddAssign:
2473     if (!this->emitAddf(getFPOptions(E), E))
2474       return false;
2475     break;
2476   case BO_SubAssign:
2477     if (!this->emitSubf(getFPOptions(E), E))
2478       return false;
2479     break;
2480   case BO_MulAssign:
2481     if (!this->emitMulf(getFPOptions(E), E))
2482       return false;
2483     break;
2484   case BO_DivAssign:
2485     if (!this->emitDivf(getFPOptions(E), E))
2486       return false;
2487     break;
2488   default:
2489     return false;
2490   }
2491 
2492   if (!this->emitPrimCast(classifyPrim(ResultType), LHST, LHS->getType(), E))
2493     return false;
2494 
2495   if (DiscardResult)
2496     return this->emitStorePop(LHST, E);
2497   return this->emitStore(LHST, E);
2498 }
2499 
2500 template <class Emitter>
2501 bool Compiler<Emitter>::VisitPointerCompoundAssignOperator(
2502     const CompoundAssignOperator *E) {
2503   BinaryOperatorKind Op = E->getOpcode();
2504   const Expr *LHS = E->getLHS();
2505   const Expr *RHS = E->getRHS();
2506   std::optional<PrimType> LT = classify(LHS->getType());
2507   std::optional<PrimType> RT = classify(RHS->getType());
2508 
2509   if (Op != BO_AddAssign && Op != BO_SubAssign)
2510     return false;
2511 
2512   if (!LT || !RT)
2513     return false;
2514 
2515   if (!visit(LHS))
2516     return false;
2517 
2518   if (!this->emitLoad(*LT, LHS))
2519     return false;
2520 
2521   if (!visit(RHS))
2522     return false;
2523 
2524   if (Op == BO_AddAssign) {
2525     if (!this->emitAddOffset(*RT, E))
2526       return false;
2527   } else {
2528     if (!this->emitSubOffset(*RT, E))
2529       return false;
2530   }
2531 
2532   if (DiscardResult)
2533     return this->emitStorePopPtr(E);
2534   return this->emitStorePtr(E);
2535 }
2536 
2537 template <class Emitter>
2538 bool Compiler<Emitter>::VisitCompoundAssignOperator(
2539     const CompoundAssignOperator *E) {
2540   if (E->getType()->isVectorType())
2541     return VisitVectorBinOp(E);
2542 
2543   const Expr *LHS = E->getLHS();
2544   const Expr *RHS = E->getRHS();
2545   std::optional<PrimType> LHSComputationT =
2546       classify(E->getComputationLHSType());
2547   std::optional<PrimType> LT = classify(LHS->getType());
2548   std::optional<PrimType> RT = classify(RHS->getType());
2549   std::optional<PrimType> ResultT = classify(E->getType());
2550 
2551   if (!Ctx.getLangOpts().CPlusPlus14)
2552     return this->visit(RHS) && this->visit(LHS) && this->emitError(E);
2553 
2554   if (!LT || !RT || !ResultT || !LHSComputationT)
2555     return false;
2556 
2557   // Handle floating point operations separately here, since they
2558   // require special care.
2559 
2560   if (ResultT == PT_Float || RT == PT_Float)
2561     return VisitFloatCompoundAssignOperator(E);
2562 
2563   if (E->getType()->isPointerType())
2564     return VisitPointerCompoundAssignOperator(E);
2565 
2566   assert(!E->getType()->isPointerType() && "Handled above");
2567   assert(!E->getType()->isFloatingType() && "Handled above");
2568 
2569   // C++17 onwards require that we evaluate the RHS first.
2570   // Compute RHS and save it in a temporary variable so we can
2571   // load it again later.
2572   // FIXME: Compound assignments are unsequenced in C, so we might
2573   //   have to figure out how to reject them.
2574   if (!visit(RHS))
2575     return false;
2576 
2577   unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
2578 
2579   if (!this->emitSetLocal(*RT, TempOffset, E))
2580     return false;
2581 
2582   // Get LHS pointer, load its value and cast it to the
2583   // computation type if necessary.
2584   if (!visit(LHS))
2585     return false;
2586   if (!this->emitLoad(*LT, E))
2587     return false;
2588   if (LT != LHSComputationT) {
2589     if (!this->emitCast(*LT, *LHSComputationT, E))
2590       return false;
2591   }
2592 
2593   // Get the RHS value on the stack.
2594   if (!this->emitGetLocal(*RT, TempOffset, E))
2595     return false;
2596 
2597   // Perform operation.
2598   switch (E->getOpcode()) {
2599   case BO_AddAssign:
2600     if (!this->emitAdd(*LHSComputationT, E))
2601       return false;
2602     break;
2603   case BO_SubAssign:
2604     if (!this->emitSub(*LHSComputationT, E))
2605       return false;
2606     break;
2607   case BO_MulAssign:
2608     if (!this->emitMul(*LHSComputationT, E))
2609       return false;
2610     break;
2611   case BO_DivAssign:
2612     if (!this->emitDiv(*LHSComputationT, E))
2613       return false;
2614     break;
2615   case BO_RemAssign:
2616     if (!this->emitRem(*LHSComputationT, E))
2617       return false;
2618     break;
2619   case BO_ShlAssign:
2620     if (!this->emitShl(*LHSComputationT, *RT, E))
2621       return false;
2622     break;
2623   case BO_ShrAssign:
2624     if (!this->emitShr(*LHSComputationT, *RT, E))
2625       return false;
2626     break;
2627   case BO_AndAssign:
2628     if (!this->emitBitAnd(*LHSComputationT, E))
2629       return false;
2630     break;
2631   case BO_XorAssign:
2632     if (!this->emitBitXor(*LHSComputationT, E))
2633       return false;
2634     break;
2635   case BO_OrAssign:
2636     if (!this->emitBitOr(*LHSComputationT, E))
2637       return false;
2638     break;
2639   default:
2640     llvm_unreachable("Unimplemented compound assign operator");
2641   }
2642 
2643   // And now cast from LHSComputationT to ResultT.
2644   if (ResultT != LHSComputationT) {
2645     if (!this->emitCast(*LHSComputationT, *ResultT, E))
2646       return false;
2647   }
2648 
2649   // And store the result in LHS.
2650   if (DiscardResult) {
2651     if (LHS->refersToBitField())
2652       return this->emitStoreBitFieldPop(*ResultT, E);
2653     return this->emitStorePop(*ResultT, E);
2654   }
2655   if (LHS->refersToBitField())
2656     return this->emitStoreBitField(*ResultT, E);
2657   return this->emitStore(*ResultT, E);
2658 }
2659 
2660 template <class Emitter>
2661 bool Compiler<Emitter>::VisitExprWithCleanups(const ExprWithCleanups *E) {
2662   LocalScope<Emitter> ES(this);
2663   const Expr *SubExpr = E->getSubExpr();
2664 
2665   return this->delegate(SubExpr) && ES.destroyLocals(E);
2666 }
2667 
2668 template <class Emitter>
2669 bool Compiler<Emitter>::VisitMaterializeTemporaryExpr(
2670     const MaterializeTemporaryExpr *E) {
2671   const Expr *SubExpr = E->getSubExpr();
2672 
2673   if (Initializing) {
2674     // We already have a value, just initialize that.
2675     return this->delegate(SubExpr);
2676   }
2677   // If we don't end up using the materialized temporary anyway, don't
2678   // bother creating it.
2679   if (DiscardResult)
2680     return this->discard(SubExpr);
2681 
2682   // When we're initializing a global variable *or* the storage duration of
2683   // the temporary is explicitly static, create a global variable.
2684   std::optional<PrimType> SubExprT = classify(SubExpr);
2685   bool IsStatic = E->getStorageDuration() == SD_Static;
2686   if (IsStatic) {
2687     std::optional<unsigned> GlobalIndex = P.createGlobal(E);
2688     if (!GlobalIndex)
2689       return false;
2690 
2691     const LifetimeExtendedTemporaryDecl *TempDecl =
2692         E->getLifetimeExtendedTemporaryDecl();
2693     if (IsStatic)
2694       assert(TempDecl);
2695 
2696     if (SubExprT) {
2697       if (!this->visit(SubExpr))
2698         return false;
2699       if (IsStatic) {
2700         if (!this->emitInitGlobalTemp(*SubExprT, *GlobalIndex, TempDecl, E))
2701           return false;
2702       } else {
2703         if (!this->emitInitGlobal(*SubExprT, *GlobalIndex, E))
2704           return false;
2705       }
2706       return this->emitGetPtrGlobal(*GlobalIndex, E);
2707     }
2708 
2709     if (!this->checkLiteralType(SubExpr))
2710       return false;
2711     // Non-primitive values.
2712     if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2713       return false;
2714     if (!this->visitInitializer(SubExpr))
2715       return false;
2716     if (IsStatic)
2717       return this->emitInitGlobalTempComp(TempDecl, E);
2718     return true;
2719   }
2720 
2721   // For everyhing else, use local variables.
2722   if (SubExprT) {
2723     unsigned LocalIndex = allocateLocalPrimitive(E, *SubExprT, /*IsConst=*/true,
2724                                                  /*IsExtended=*/true);
2725     if (!this->visit(SubExpr))
2726       return false;
2727     if (!this->emitSetLocal(*SubExprT, LocalIndex, E))
2728       return false;
2729     return this->emitGetPtrLocal(LocalIndex, E);
2730   } else {
2731 
2732     if (!this->checkLiteralType(SubExpr))
2733       return false;
2734 
2735     const Expr *Inner = E->getSubExpr()->skipRValueSubobjectAdjustments();
2736     if (std::optional<unsigned> LocalIndex =
2737             allocateLocal(E, Inner->getType(), E->getExtendingDecl())) {
2738       InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
2739       if (!this->emitGetPtrLocal(*LocalIndex, E))
2740         return false;
2741       return this->visitInitializer(SubExpr) && this->emitFinishInit(E);
2742     }
2743   }
2744   return false;
2745 }
2746 
2747 template <class Emitter>
2748 bool Compiler<Emitter>::VisitCXXBindTemporaryExpr(
2749     const CXXBindTemporaryExpr *E) {
2750   return this->delegate(E->getSubExpr());
2751 }
2752 
2753 template <class Emitter>
2754 bool Compiler<Emitter>::VisitCompoundLiteralExpr(const CompoundLiteralExpr *E) {
2755   const Expr *Init = E->getInitializer();
2756   if (DiscardResult)
2757     return this->discard(Init);
2758 
2759   if (Initializing) {
2760     // We already have a value, just initialize that.
2761     return this->visitInitializer(Init) && this->emitFinishInit(E);
2762   }
2763 
2764   std::optional<PrimType> T = classify(E->getType());
2765   if (E->isFileScope()) {
2766     // Avoid creating a variable if this is a primitive RValue anyway.
2767     if (T && !E->isLValue())
2768       return this->delegate(Init);
2769 
2770     if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) {
2771       if (!this->emitGetPtrGlobal(*GlobalIndex, E))
2772         return false;
2773 
2774       if (T) {
2775         if (!this->visit(Init))
2776           return false;
2777         return this->emitInitGlobal(*T, *GlobalIndex, E);
2778       }
2779 
2780       return this->visitInitializer(Init) && this->emitFinishInit(E);
2781     }
2782 
2783     return false;
2784   }
2785 
2786   // Otherwise, use a local variable.
2787   if (T && !E->isLValue()) {
2788     // For primitive types, we just visit the initializer.
2789     return this->delegate(Init);
2790   } else {
2791     unsigned LocalIndex;
2792 
2793     if (T)
2794       LocalIndex = this->allocateLocalPrimitive(Init, *T, false, false);
2795     else if (std::optional<unsigned> MaybeIndex = this->allocateLocal(Init))
2796       LocalIndex = *MaybeIndex;
2797     else
2798       return false;
2799 
2800     if (!this->emitGetPtrLocal(LocalIndex, E))
2801       return false;
2802 
2803     if (T) {
2804       if (!this->visit(Init)) {
2805         return false;
2806       }
2807       return this->emitInit(*T, E);
2808     } else {
2809       if (!this->visitInitializer(Init) || !this->emitFinishInit(E))
2810         return false;
2811     }
2812     return true;
2813   }
2814 
2815   return false;
2816 }
2817 
2818 template <class Emitter>
2819 bool Compiler<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) {
2820   if (DiscardResult)
2821     return true;
2822   if (E->getType()->isBooleanType())
2823     return this->emitConstBool(E->getValue(), E);
2824   return this->emitConst(E->getValue(), E);
2825 }
2826 
2827 template <class Emitter>
2828 bool Compiler<Emitter>::VisitArrayTypeTraitExpr(const ArrayTypeTraitExpr *E) {
2829   if (DiscardResult)
2830     return true;
2831   return this->emitConst(E->getValue(), E);
2832 }
2833 
2834 template <class Emitter>
2835 bool Compiler<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
2836   if (DiscardResult)
2837     return true;
2838 
2839   assert(Initializing);
2840   const Record *R = P.getOrCreateRecord(E->getLambdaClass());
2841 
2842   auto *CaptureInitIt = E->capture_init_begin();
2843   // Initialize all fields (which represent lambda captures) of the
2844   // record with their initializers.
2845   for (const Record::Field &F : R->fields()) {
2846     const Expr *Init = *CaptureInitIt;
2847     ++CaptureInitIt;
2848 
2849     if (!Init)
2850       continue;
2851 
2852     if (std::optional<PrimType> T = classify(Init)) {
2853       if (!this->visit(Init))
2854         return false;
2855 
2856       if (!this->emitInitField(*T, F.Offset, E))
2857         return false;
2858     } else {
2859       if (!this->emitGetPtrField(F.Offset, E))
2860         return false;
2861 
2862       if (!this->visitInitializer(Init))
2863         return false;
2864 
2865       if (!this->emitPopPtr(E))
2866         return false;
2867     }
2868   }
2869 
2870   return true;
2871 }
2872 
2873 template <class Emitter>
2874 bool Compiler<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) {
2875   if (DiscardResult)
2876     return true;
2877 
2878   if (!Initializing) {
2879     unsigned StringIndex = P.createGlobalString(E->getFunctionName(), E);
2880     return this->emitGetPtrGlobal(StringIndex, E);
2881   }
2882 
2883   return this->delegate(E->getFunctionName());
2884 }
2885 
2886 template <class Emitter>
2887 bool Compiler<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) {
2888   if (E->getSubExpr() && !this->discard(E->getSubExpr()))
2889     return false;
2890 
2891   return this->emitInvalid(E);
2892 }
2893 
2894 template <class Emitter>
2895 bool Compiler<Emitter>::VisitCXXReinterpretCastExpr(
2896     const CXXReinterpretCastExpr *E) {
2897   const Expr *SubExpr = E->getSubExpr();
2898 
2899   std::optional<PrimType> FromT = classify(SubExpr);
2900   std::optional<PrimType> ToT = classify(E);
2901 
2902   if (!FromT || !ToT)
2903     return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, E);
2904 
2905   if (FromT == PT_Ptr || ToT == PT_Ptr) {
2906     // Both types could be PT_Ptr because their expressions are glvalues.
2907     std::optional<PrimType> PointeeFromT;
2908     if (SubExpr->getType()->isPointerOrReferenceType())
2909       PointeeFromT = classify(SubExpr->getType()->getPointeeType());
2910     else
2911       PointeeFromT = classify(SubExpr->getType());
2912 
2913     std::optional<PrimType> PointeeToT;
2914     if (E->getType()->isPointerOrReferenceType())
2915       PointeeToT = classify(E->getType()->getPointeeType());
2916     else
2917       PointeeToT = classify(E->getType());
2918 
2919     bool Fatal = true;
2920     if (PointeeToT && PointeeFromT) {
2921       if (isIntegralType(*PointeeFromT) && isIntegralType(*PointeeToT))
2922         Fatal = false;
2923     }
2924 
2925     if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
2926       return false;
2927 
2928     if (E->getCastKind() == CK_LValueBitCast)
2929       return this->delegate(SubExpr);
2930     return this->VisitCastExpr(E);
2931   }
2932 
2933   // Try to actually do the cast.
2934   bool Fatal = (ToT != FromT);
2935   if (!this->emitInvalidCast(CastKind::Reinterpret, Fatal, E))
2936     return false;
2937 
2938   return this->VisitCastExpr(E);
2939 }
2940 
2941 template <class Emitter>
2942 bool Compiler<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
2943   assert(E->getType()->isBooleanType());
2944 
2945   if (DiscardResult)
2946     return true;
2947   return this->emitConstBool(E->getValue(), E);
2948 }
2949 
2950 template <class Emitter>
2951 bool Compiler<Emitter>::VisitCXXConstructExpr(const CXXConstructExpr *E) {
2952   QualType T = E->getType();
2953   assert(!classify(T));
2954 
2955   if (T->isRecordType()) {
2956     const CXXConstructorDecl *Ctor = E->getConstructor();
2957 
2958     // Trivial copy/move constructor. Avoid copy.
2959     if (Ctor->isDefaulted() && Ctor->isCopyOrMoveConstructor() &&
2960         Ctor->isTrivial() &&
2961         E->getArg(0)->isTemporaryObject(Ctx.getASTContext(),
2962                                         T->getAsCXXRecordDecl()))
2963       return this->visitInitializer(E->getArg(0));
2964 
2965     // If we're discarding a construct expression, we still need
2966     // to allocate a variable and call the constructor and destructor.
2967     if (DiscardResult) {
2968       if (Ctor->isTrivial())
2969         return true;
2970       assert(!Initializing);
2971       std::optional<unsigned> LocalIndex = allocateLocal(E);
2972 
2973       if (!LocalIndex)
2974         return false;
2975 
2976       if (!this->emitGetPtrLocal(*LocalIndex, E))
2977         return false;
2978     }
2979 
2980     // Zero initialization.
2981     if (E->requiresZeroInitialization()) {
2982       const Record *R = getRecord(E->getType());
2983 
2984       if (!this->visitZeroRecordInitializer(R, E))
2985         return false;
2986 
2987       // If the constructor is trivial anyway, we're done.
2988       if (Ctor->isTrivial())
2989         return true;
2990     }
2991 
2992     const Function *Func = getFunction(Ctor);
2993 
2994     if (!Func)
2995       return false;
2996 
2997     assert(Func->hasThisPointer());
2998     assert(!Func->hasRVO());
2999 
3000     //  The This pointer is already on the stack because this is an initializer,
3001     //  but we need to dup() so the call() below has its own copy.
3002     if (!this->emitDupPtr(E))
3003       return false;
3004 
3005     // Constructor arguments.
3006     for (const auto *Arg : E->arguments()) {
3007       if (!this->visit(Arg))
3008         return false;
3009     }
3010 
3011     if (Func->isVariadic()) {
3012       uint32_t VarArgSize = 0;
3013       unsigned NumParams = Func->getNumWrittenParams();
3014       for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I) {
3015         VarArgSize +=
3016             align(primSize(classify(E->getArg(I)->getType()).value_or(PT_Ptr)));
3017       }
3018       if (!this->emitCallVar(Func, VarArgSize, E))
3019         return false;
3020     } else {
3021       if (!this->emitCall(Func, 0, E)) {
3022         // When discarding, we don't need the result anyway, so clean up
3023         // the instance dup we did earlier in case surrounding code wants
3024         // to keep evaluating.
3025         if (DiscardResult)
3026           (void)this->emitPopPtr(E);
3027         return false;
3028       }
3029     }
3030 
3031     if (DiscardResult)
3032       return this->emitPopPtr(E);
3033     return this->emitFinishInit(E);
3034   }
3035 
3036   if (T->isArrayType()) {
3037     const ConstantArrayType *CAT =
3038         Ctx.getASTContext().getAsConstantArrayType(E->getType());
3039     if (!CAT)
3040       return false;
3041 
3042     size_t NumElems = CAT->getZExtSize();
3043     const Function *Func = getFunction(E->getConstructor());
3044     if (!Func || !Func->isConstexpr())
3045       return false;
3046 
3047     // FIXME(perf): We're calling the constructor once per array element here,
3048     //   in the old intepreter we had a special-case for trivial constructors.
3049     for (size_t I = 0; I != NumElems; ++I) {
3050       if (!this->emitConstUint64(I, E))
3051         return false;
3052       if (!this->emitArrayElemPtrUint64(E))
3053         return false;
3054 
3055       // Constructor arguments.
3056       for (const auto *Arg : E->arguments()) {
3057         if (!this->visit(Arg))
3058           return false;
3059       }
3060 
3061       if (!this->emitCall(Func, 0, E))
3062         return false;
3063     }
3064     return true;
3065   }
3066 
3067   return false;
3068 }
3069 
3070 template <class Emitter>
3071 bool Compiler<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) {
3072   if (DiscardResult)
3073     return true;
3074 
3075   const APValue Val =
3076       E->EvaluateInContext(Ctx.getASTContext(), SourceLocDefaultExpr);
3077 
3078   // Things like __builtin_LINE().
3079   if (E->getType()->isIntegerType()) {
3080     assert(Val.isInt());
3081     const APSInt &I = Val.getInt();
3082     return this->emitConst(I, E);
3083   }
3084   // Otherwise, the APValue is an LValue, with only one element.
3085   // Theoretically, we don't need the APValue at all of course.
3086   assert(E->getType()->isPointerType());
3087   assert(Val.isLValue());
3088   const APValue::LValueBase &Base = Val.getLValueBase();
3089   if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>())
3090     return this->visit(LValueExpr);
3091 
3092   // Otherwise, we have a decl (which is the case for
3093   // __builtin_source_location).
3094   assert(Base.is<const ValueDecl *>());
3095   assert(Val.getLValuePath().size() == 0);
3096   const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>();
3097   assert(BaseDecl);
3098 
3099   auto *UGCD = cast<UnnamedGlobalConstantDecl>(BaseDecl);
3100 
3101   std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(UGCD);
3102   if (!GlobalIndex)
3103     return false;
3104 
3105   if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3106     return false;
3107 
3108   const Record *R = getRecord(E->getType());
3109   const APValue &V = UGCD->getValue();
3110   for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) {
3111     const Record::Field *F = R->getField(I);
3112     const APValue &FieldValue = V.getStructField(I);
3113 
3114     PrimType FieldT = classifyPrim(F->Decl->getType());
3115 
3116     if (!this->visitAPValue(FieldValue, FieldT, E))
3117       return false;
3118     if (!this->emitInitField(FieldT, F->Offset, E))
3119       return false;
3120   }
3121 
3122   // Leave the pointer to the global on the stack.
3123   return true;
3124 }
3125 
3126 template <class Emitter>
3127 bool Compiler<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
3128   unsigned N = E->getNumComponents();
3129   if (N == 0)
3130     return false;
3131 
3132   for (unsigned I = 0; I != N; ++I) {
3133     const OffsetOfNode &Node = E->getComponent(I);
3134     if (Node.getKind() == OffsetOfNode::Array) {
3135       const Expr *ArrayIndexExpr = E->getIndexExpr(Node.getArrayExprIndex());
3136       PrimType IndexT = classifyPrim(ArrayIndexExpr->getType());
3137 
3138       if (DiscardResult) {
3139         if (!this->discard(ArrayIndexExpr))
3140           return false;
3141         continue;
3142       }
3143 
3144       if (!this->visit(ArrayIndexExpr))
3145         return false;
3146       // Cast to Sint64.
3147       if (IndexT != PT_Sint64) {
3148         if (!this->emitCast(IndexT, PT_Sint64, E))
3149           return false;
3150       }
3151     }
3152   }
3153 
3154   if (DiscardResult)
3155     return true;
3156 
3157   PrimType T = classifyPrim(E->getType());
3158   return this->emitOffsetOf(T, E, E);
3159 }
3160 
3161 template <class Emitter>
3162 bool Compiler<Emitter>::VisitCXXScalarValueInitExpr(
3163     const CXXScalarValueInitExpr *E) {
3164   QualType Ty = E->getType();
3165 
3166   if (DiscardResult || Ty->isVoidType())
3167     return true;
3168 
3169   if (std::optional<PrimType> T = classify(Ty))
3170     return this->visitZeroInitializer(*T, Ty, E);
3171 
3172   if (const auto *CT = Ty->getAs<ComplexType>()) {
3173     if (!Initializing) {
3174       std::optional<unsigned> LocalIndex = allocateLocal(E);
3175       if (!LocalIndex)
3176         return false;
3177       if (!this->emitGetPtrLocal(*LocalIndex, E))
3178         return false;
3179     }
3180 
3181     // Initialize both fields to 0.
3182     QualType ElemQT = CT->getElementType();
3183     PrimType ElemT = classifyPrim(ElemQT);
3184 
3185     for (unsigned I = 0; I != 2; ++I) {
3186       if (!this->visitZeroInitializer(ElemT, ElemQT, E))
3187         return false;
3188       if (!this->emitInitElem(ElemT, I, E))
3189         return false;
3190     }
3191     return true;
3192   }
3193 
3194   if (const auto *VT = Ty->getAs<VectorType>()) {
3195     // FIXME: Code duplication with the _Complex case above.
3196     if (!Initializing) {
3197       std::optional<unsigned> LocalIndex = allocateLocal(E);
3198       if (!LocalIndex)
3199         return false;
3200       if (!this->emitGetPtrLocal(*LocalIndex, E))
3201         return false;
3202     }
3203 
3204     // Initialize all fields to 0.
3205     QualType ElemQT = VT->getElementType();
3206     PrimType ElemT = classifyPrim(ElemQT);
3207 
3208     for (unsigned I = 0, N = VT->getNumElements(); I != N; ++I) {
3209       if (!this->visitZeroInitializer(ElemT, ElemQT, E))
3210         return false;
3211       if (!this->emitInitElem(ElemT, I, E))
3212         return false;
3213     }
3214     return true;
3215   }
3216 
3217   return false;
3218 }
3219 
3220 template <class Emitter>
3221 bool Compiler<Emitter>::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
3222   return this->emitConst(E->getPackLength(), E);
3223 }
3224 
3225 template <class Emitter>
3226 bool Compiler<Emitter>::VisitGenericSelectionExpr(
3227     const GenericSelectionExpr *E) {
3228   return this->delegate(E->getResultExpr());
3229 }
3230 
3231 template <class Emitter>
3232 bool Compiler<Emitter>::VisitChooseExpr(const ChooseExpr *E) {
3233   return this->delegate(E->getChosenSubExpr());
3234 }
3235 
3236 template <class Emitter>
3237 bool Compiler<Emitter>::VisitObjCBoolLiteralExpr(const ObjCBoolLiteralExpr *E) {
3238   if (DiscardResult)
3239     return true;
3240 
3241   return this->emitConst(E->getValue(), E);
3242 }
3243 
3244 template <class Emitter>
3245 bool Compiler<Emitter>::VisitCXXInheritedCtorInitExpr(
3246     const CXXInheritedCtorInitExpr *E) {
3247   const CXXConstructorDecl *Ctor = E->getConstructor();
3248   assert(!Ctor->isTrivial() &&
3249          "Trivial CXXInheritedCtorInitExpr, implement. (possible?)");
3250   const Function *F = this->getFunction(Ctor);
3251   assert(F);
3252   assert(!F->hasRVO());
3253   assert(F->hasThisPointer());
3254 
3255   if (!this->emitDupPtr(SourceInfo{}))
3256     return false;
3257 
3258   // Forward all arguments of the current function (which should be a
3259   // constructor itself) to the inherited ctor.
3260   // This is necessary because the calling code has pushed the pointer
3261   // of the correct base for  us already, but the arguments need
3262   // to come after.
3263   unsigned Offset = align(primSize(PT_Ptr)); // instance pointer.
3264   for (const ParmVarDecl *PD : Ctor->parameters()) {
3265     PrimType PT = this->classify(PD->getType()).value_or(PT_Ptr);
3266 
3267     if (!this->emitGetParam(PT, Offset, E))
3268       return false;
3269     Offset += align(primSize(PT));
3270   }
3271 
3272   return this->emitCall(F, 0, E);
3273 }
3274 
3275 template <class Emitter>
3276 bool Compiler<Emitter>::VisitCXXNewExpr(const CXXNewExpr *E) {
3277   assert(classifyPrim(E->getType()) == PT_Ptr);
3278   const Expr *Init = E->getInitializer();
3279   QualType ElementType = E->getAllocatedType();
3280   std::optional<PrimType> ElemT = classify(ElementType);
3281   unsigned PlacementArgs = E->getNumPlacementArgs();
3282   const FunctionDecl *OperatorNew = E->getOperatorNew();
3283   const Expr *PlacementDest = nullptr;
3284   bool IsNoThrow = false;
3285 
3286   if (PlacementArgs != 0) {
3287     // FIXME: There is no restriction on this, but it's not clear that any
3288     // other form makes any sense. We get here for cases such as:
3289     //
3290     //   new (std::align_val_t{N}) X(int)
3291     //
3292     // (which should presumably be valid only if N is a multiple of
3293     // alignof(int), and in any case can't be deallocated unless N is
3294     // alignof(X) and X has new-extended alignment).
3295     if (PlacementArgs == 1) {
3296       const Expr *Arg1 = E->getPlacementArg(0);
3297       if (Arg1->getType()->isNothrowT()) {
3298         if (!this->discard(Arg1))
3299           return false;
3300         IsNoThrow = true;
3301       } else {
3302         // Invalid unless we have C++26 or are in a std:: function.
3303         if (!this->emitInvalidNewDeleteExpr(E, E))
3304           return false;
3305 
3306         // If we have a placement-new destination, we'll later use that instead
3307         // of allocating.
3308         if (OperatorNew->isReservedGlobalPlacementOperator())
3309           PlacementDest = Arg1;
3310       }
3311     } else {
3312       // Always invalid.
3313       return this->emitInvalid(E);
3314     }
3315   } else if (!OperatorNew->isReplaceableGlobalAllocationFunction())
3316     return this->emitInvalidNewDeleteExpr(E, E);
3317 
3318   const Descriptor *Desc;
3319   if (!PlacementDest) {
3320     if (ElemT) {
3321       if (E->isArray())
3322         Desc = nullptr; // We're not going to use it in this case.
3323       else
3324         Desc = P.createDescriptor(E, *ElemT, Descriptor::InlineDescMD,
3325                                   /*IsConst=*/false, /*IsTemporary=*/false,
3326                                   /*IsMutable=*/false);
3327     } else {
3328       Desc = P.createDescriptor(
3329           E, ElementType.getTypePtr(),
3330           E->isArray() ? std::nullopt : Descriptor::InlineDescMD,
3331           /*IsConst=*/false, /*IsTemporary=*/false, /*IsMutable=*/false, Init);
3332     }
3333   }
3334 
3335   if (E->isArray()) {
3336     std::optional<const Expr *> ArraySizeExpr = E->getArraySize();
3337     if (!ArraySizeExpr)
3338       return false;
3339 
3340     const Expr *Stripped = *ArraySizeExpr;
3341     for (; auto *ICE = dyn_cast<ImplicitCastExpr>(Stripped);
3342          Stripped = ICE->getSubExpr())
3343       if (ICE->getCastKind() != CK_NoOp &&
3344           ICE->getCastKind() != CK_IntegralCast)
3345         break;
3346 
3347     PrimType SizeT = classifyPrim(Stripped->getType());
3348 
3349     if (PlacementDest) {
3350       if (!this->visit(PlacementDest))
3351         return false;
3352       if (!this->visit(Stripped))
3353         return false;
3354       if (!this->emitCheckNewTypeMismatchArray(SizeT, E, E))
3355         return false;
3356     } else {
3357       if (!this->visit(Stripped))
3358         return false;
3359 
3360       if (ElemT) {
3361         // N primitive elements.
3362         if (!this->emitAllocN(SizeT, *ElemT, E, IsNoThrow, E))
3363           return false;
3364       } else {
3365         // N Composite elements.
3366         if (!this->emitAllocCN(SizeT, Desc, IsNoThrow, E))
3367           return false;
3368       }
3369     }
3370 
3371     if (Init && !this->visitInitializer(Init))
3372       return false;
3373 
3374   } else {
3375     if (PlacementDest) {
3376       if (!this->visit(PlacementDest))
3377         return false;
3378       if (!this->emitCheckNewTypeMismatch(E, E))
3379         return false;
3380     } else {
3381       // Allocate just one element.
3382       if (!this->emitAlloc(Desc, E))
3383         return false;
3384     }
3385 
3386     if (Init) {
3387       if (ElemT) {
3388         if (!this->visit(Init))
3389           return false;
3390 
3391         if (!this->emitInit(*ElemT, E))
3392           return false;
3393       } else {
3394         // Composite.
3395         if (!this->visitInitializer(Init))
3396           return false;
3397       }
3398     }
3399   }
3400 
3401   if (DiscardResult)
3402     return this->emitPopPtr(E);
3403 
3404   return true;
3405 }
3406 
3407 template <class Emitter>
3408 bool Compiler<Emitter>::VisitCXXDeleteExpr(const CXXDeleteExpr *E) {
3409   const Expr *Arg = E->getArgument();
3410 
3411   const FunctionDecl *OperatorDelete = E->getOperatorDelete();
3412 
3413   if (!OperatorDelete->isReplaceableGlobalAllocationFunction())
3414     return this->emitInvalidNewDeleteExpr(E, E);
3415 
3416   // Arg must be an lvalue.
3417   if (!this->visit(Arg))
3418     return false;
3419 
3420   return this->emitFree(E->isArrayForm(), E->isGlobalDelete(), E);
3421 }
3422 
3423 template <class Emitter>
3424 bool Compiler<Emitter>::VisitBlockExpr(const BlockExpr *E) {
3425   if (DiscardResult)
3426     return true;
3427 
3428   const Function *Func = nullptr;
3429   if (auto F = Compiler<ByteCodeEmitter>(Ctx, P).compileObjCBlock(E))
3430     Func = F;
3431 
3432   if (!Func)
3433     return false;
3434   return this->emitGetFnPtr(Func, E);
3435 }
3436 
3437 template <class Emitter>
3438 bool Compiler<Emitter>::VisitCXXTypeidExpr(const CXXTypeidExpr *E) {
3439   const Type *TypeInfoType = E->getType().getTypePtr();
3440 
3441   if (!E->isPotentiallyEvaluated()) {
3442     if (DiscardResult)
3443       return true;
3444 
3445     if (E->isTypeOperand())
3446       return this->emitGetTypeid(
3447           E->getTypeOperand(Ctx.getASTContext()).getTypePtr(), TypeInfoType, E);
3448     return this->emitGetTypeid(E->getExprOperand()->getType().getTypePtr(),
3449                                TypeInfoType, E);
3450   }
3451 
3452   // Otherwise, we need to evaluate the expression operand.
3453   assert(E->getExprOperand());
3454   assert(E->getExprOperand()->isLValue());
3455 
3456   if (!Ctx.getLangOpts().CPlusPlus20 && !this->emitDiagTypeid(E))
3457     return false;
3458 
3459   if (!this->visit(E->getExprOperand()))
3460     return false;
3461 
3462   if (!this->emitGetTypeidPtr(TypeInfoType, E))
3463     return false;
3464   if (DiscardResult)
3465     return this->emitPopPtr(E);
3466   return true;
3467 }
3468 
3469 template <class Emitter>
3470 bool Compiler<Emitter>::VisitExpressionTraitExpr(const ExpressionTraitExpr *E) {
3471   assert(Ctx.getLangOpts().CPlusPlus);
3472   return this->emitConstBool(E->getValue(), E);
3473 }
3474 
3475 template <class Emitter>
3476 bool Compiler<Emitter>::VisitCXXUuidofExpr(const CXXUuidofExpr *E) {
3477   if (DiscardResult)
3478     return true;
3479   assert(!Initializing);
3480 
3481   const MSGuidDecl *GuidDecl = E->getGuidDecl();
3482   const RecordDecl *RD = GuidDecl->getType()->getAsRecordDecl();
3483   assert(RD);
3484   // If the definiton of the result type is incomplete, just return a dummy.
3485   // If (and when) that is read from, we will fail, but not now.
3486   if (!RD->isCompleteDefinition())
3487     return this->emitDummyPtr(GuidDecl, E);
3488 
3489   std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(GuidDecl);
3490   if (!GlobalIndex)
3491     return false;
3492   if (!this->emitGetPtrGlobal(*GlobalIndex, E))
3493     return false;
3494 
3495   assert(this->getRecord(E->getType()));
3496 
3497   const APValue &V = GuidDecl->getAsAPValue();
3498   if (V.getKind() == APValue::None)
3499     return true;
3500 
3501   assert(V.isStruct());
3502   assert(V.getStructNumBases() == 0);
3503   if (!this->visitAPValueInitializer(V, E))
3504     return false;
3505 
3506   return this->emitFinishInit(E);
3507 }
3508 
3509 template <class Emitter>
3510 bool Compiler<Emitter>::VisitRequiresExpr(const RequiresExpr *E) {
3511   assert(classifyPrim(E->getType()) == PT_Bool);
3512   if (DiscardResult)
3513     return true;
3514   return this->emitConstBool(E->isSatisfied(), E);
3515 }
3516 
3517 template <class Emitter>
3518 bool Compiler<Emitter>::VisitConceptSpecializationExpr(
3519     const ConceptSpecializationExpr *E) {
3520   assert(classifyPrim(E->getType()) == PT_Bool);
3521   if (DiscardResult)
3522     return true;
3523   return this->emitConstBool(E->isSatisfied(), E);
3524 }
3525 
3526 template <class Emitter>
3527 bool Compiler<Emitter>::VisitCXXRewrittenBinaryOperator(
3528     const CXXRewrittenBinaryOperator *E) {
3529   return this->delegate(E->getSemanticForm());
3530 }
3531 
3532 template <class Emitter>
3533 bool Compiler<Emitter>::VisitPseudoObjectExpr(const PseudoObjectExpr *E) {
3534 
3535   for (const Expr *SemE : E->semantics()) {
3536     if (auto *OVE = dyn_cast<OpaqueValueExpr>(SemE)) {
3537       if (SemE == E->getResultExpr())
3538         return false;
3539 
3540       if (OVE->isUnique())
3541         continue;
3542 
3543       if (!this->discard(OVE))
3544         return false;
3545     } else if (SemE == E->getResultExpr()) {
3546       if (!this->delegate(SemE))
3547         return false;
3548     } else {
3549       if (!this->discard(SemE))
3550         return false;
3551     }
3552   }
3553   return true;
3554 }
3555 
3556 template <class Emitter>
3557 bool Compiler<Emitter>::VisitPackIndexingExpr(const PackIndexingExpr *E) {
3558   return this->delegate(E->getSelectedExpr());
3559 }
3560 
3561 template <class Emitter>
3562 bool Compiler<Emitter>::VisitRecoveryExpr(const RecoveryExpr *E) {
3563   return this->emitError(E);
3564 }
3565 
3566 template <class Emitter>
3567 bool Compiler<Emitter>::VisitAddrLabelExpr(const AddrLabelExpr *E) {
3568   assert(E->getType()->isVoidPointerType());
3569 
3570   unsigned Offset = allocateLocalPrimitive(
3571       E->getLabel(), PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
3572 
3573   return this->emitGetLocal(PT_Ptr, Offset, E);
3574 }
3575 
3576 template <class Emitter>
3577 bool Compiler<Emitter>::VisitConvertVectorExpr(const ConvertVectorExpr *E) {
3578   assert(Initializing);
3579   const auto *VT = E->getType()->castAs<VectorType>();
3580   QualType ElemType = VT->getElementType();
3581   PrimType ElemT = classifyPrim(ElemType);
3582   const Expr *Src = E->getSrcExpr();
3583   QualType SrcType = Src->getType();
3584   PrimType SrcElemT = classifyVectorElementType(SrcType);
3585 
3586   unsigned SrcOffset = this->allocateLocalPrimitive(Src, PT_Ptr, true, false);
3587   if (!this->visit(Src))
3588     return false;
3589   if (!this->emitSetLocal(PT_Ptr, SrcOffset, E))
3590     return false;
3591 
3592   for (unsigned I = 0; I != VT->getNumElements(); ++I) {
3593     if (!this->emitGetLocal(PT_Ptr, SrcOffset, E))
3594       return false;
3595     if (!this->emitArrayElemPop(SrcElemT, I, E))
3596       return false;
3597 
3598     // Cast to the desired result element type.
3599     if (SrcElemT != ElemT) {
3600       if (!this->emitPrimCast(SrcElemT, ElemT, ElemType, E))
3601         return false;
3602     } else if (ElemType->isFloatingType() && SrcType != ElemType) {
3603       const auto *TargetSemantics = &Ctx.getFloatSemantics(ElemType);
3604       if (!this->emitCastFP(TargetSemantics, getRoundingMode(E), E))
3605         return false;
3606     }
3607     if (!this->emitInitElem(ElemT, I, E))
3608       return false;
3609   }
3610 
3611   return true;
3612 }
3613 
3614 template <class Emitter>
3615 bool Compiler<Emitter>::VisitShuffleVectorExpr(const ShuffleVectorExpr *E) {
3616   assert(Initializing);
3617   assert(E->getNumSubExprs() > 2);
3618 
3619   const Expr *Vecs[] = {E->getExpr(0), E->getExpr(1)};
3620   const VectorType *VT = Vecs[0]->getType()->castAs<VectorType>();
3621   PrimType ElemT = classifyPrim(VT->getElementType());
3622   unsigned NumInputElems = VT->getNumElements();
3623   unsigned NumOutputElems = E->getNumSubExprs() - 2;
3624   assert(NumOutputElems > 0);
3625 
3626   // Save both input vectors to a local variable.
3627   unsigned VectorOffsets[2];
3628   for (unsigned I = 0; I != 2; ++I) {
3629     VectorOffsets[I] = this->allocateLocalPrimitive(
3630         Vecs[I], PT_Ptr, /*IsConst=*/true, /*IsExtended=*/false);
3631     if (!this->visit(Vecs[I]))
3632       return false;
3633     if (!this->emitSetLocal(PT_Ptr, VectorOffsets[I], E))
3634       return false;
3635   }
3636   for (unsigned I = 0; I != NumOutputElems; ++I) {
3637     APSInt ShuffleIndex = E->getShuffleMaskIdx(Ctx.getASTContext(), I);
3638     assert(ShuffleIndex >= -1);
3639     if (ShuffleIndex == -1)
3640       return this->emitInvalidShuffleVectorIndex(I, E);
3641 
3642     assert(ShuffleIndex < (NumInputElems * 2));
3643     if (!this->emitGetLocal(PT_Ptr,
3644                             VectorOffsets[ShuffleIndex >= NumInputElems], E))
3645       return false;
3646     unsigned InputVectorIndex = ShuffleIndex.getZExtValue() % NumInputElems;
3647     if (!this->emitArrayElemPop(ElemT, InputVectorIndex, E))
3648       return false;
3649 
3650     if (!this->emitInitElem(ElemT, I, E))
3651       return false;
3652   }
3653 
3654   return true;
3655 }
3656 
3657 template <class Emitter>
3658 bool Compiler<Emitter>::VisitExtVectorElementExpr(
3659     const ExtVectorElementExpr *E) {
3660   const Expr *Base = E->getBase();
3661   assert(
3662       Base->getType()->isVectorType() ||
3663       Base->getType()->getAs<PointerType>()->getPointeeType()->isVectorType());
3664 
3665   SmallVector<uint32_t, 4> Indices;
3666   E->getEncodedElementAccess(Indices);
3667 
3668   if (Indices.size() == 1) {
3669     if (!this->visit(Base))
3670       return false;
3671 
3672     if (E->isGLValue()) {
3673       if (!this->emitConstUint32(Indices[0], E))
3674         return false;
3675       return this->emitArrayElemPtrPop(PT_Uint32, E);
3676     }
3677     // Else, also load the value.
3678     return this->emitArrayElemPop(classifyPrim(E->getType()), Indices[0], E);
3679   }
3680 
3681   // Create a local variable for the base.
3682   unsigned BaseOffset = allocateLocalPrimitive(Base, PT_Ptr, /*IsConst=*/true,
3683                                                /*IsExtended=*/false);
3684   if (!this->visit(Base))
3685     return false;
3686   if (!this->emitSetLocal(PT_Ptr, BaseOffset, E))
3687     return false;
3688 
3689   // Now the vector variable for the return value.
3690   if (!Initializing) {
3691     std::optional<unsigned> ResultIndex;
3692     ResultIndex = allocateLocal(E);
3693     if (!ResultIndex)
3694       return false;
3695     if (!this->emitGetPtrLocal(*ResultIndex, E))
3696       return false;
3697   }
3698 
3699   assert(Indices.size() == E->getType()->getAs<VectorType>()->getNumElements());
3700 
3701   PrimType ElemT =
3702       classifyPrim(E->getType()->getAs<VectorType>()->getElementType());
3703   uint32_t DstIndex = 0;
3704   for (uint32_t I : Indices) {
3705     if (!this->emitGetLocal(PT_Ptr, BaseOffset, E))
3706       return false;
3707     if (!this->emitArrayElemPop(ElemT, I, E))
3708       return false;
3709     if (!this->emitInitElem(ElemT, DstIndex, E))
3710       return false;
3711     ++DstIndex;
3712   }
3713 
3714   // Leave the result pointer on the stack.
3715   assert(!DiscardResult);
3716   return true;
3717 }
3718 
3719 template <class Emitter>
3720 bool Compiler<Emitter>::VisitObjCBoxedExpr(const ObjCBoxedExpr *E) {
3721   const Expr *SubExpr = E->getSubExpr();
3722   if (!E->isExpressibleAsConstantInitializer())
3723     return this->discard(SubExpr) && this->emitInvalid(E);
3724 
3725   if (DiscardResult)
3726     return true;
3727 
3728   assert(classifyPrim(E) == PT_Ptr);
3729   return this->emitDummyPtr(E, E);
3730 }
3731 
3732 template <class Emitter>
3733 bool Compiler<Emitter>::VisitCXXStdInitializerListExpr(
3734     const CXXStdInitializerListExpr *E) {
3735   const Expr *SubExpr = E->getSubExpr();
3736   const ConstantArrayType *ArrayType =
3737       Ctx.getASTContext().getAsConstantArrayType(SubExpr->getType());
3738   const Record *R = getRecord(E->getType());
3739   assert(Initializing);
3740   assert(SubExpr->isGLValue());
3741 
3742   if (!this->visit(SubExpr))
3743     return false;
3744   if (!this->emitConstUint8(0, E))
3745     return false;
3746   if (!this->emitArrayElemPtrPopUint8(E))
3747     return false;
3748   if (!this->emitInitFieldPtr(R->getField(0u)->Offset, E))
3749     return false;
3750 
3751   PrimType SecondFieldT = classifyPrim(R->getField(1u)->Decl->getType());
3752   if (isIntegralType(SecondFieldT)) {
3753     if (!this->emitConst(static_cast<APSInt>(ArrayType->getSize()),
3754                          SecondFieldT, E))
3755       return false;
3756     return this->emitInitField(SecondFieldT, R->getField(1u)->Offset, E);
3757   }
3758   assert(SecondFieldT == PT_Ptr);
3759 
3760   if (!this->emitGetFieldPtr(R->getField(0u)->Offset, E))
3761     return false;
3762   if (!this->emitExpandPtr(E))
3763     return false;
3764   if (!this->emitConst(static_cast<APSInt>(ArrayType->getSize()), PT_Uint64, E))
3765     return false;
3766   if (!this->emitArrayElemPtrPop(PT_Uint64, E))
3767     return false;
3768   return this->emitInitFieldPtr(R->getField(1u)->Offset, E);
3769 }
3770 
3771 template <class Emitter>
3772 bool Compiler<Emitter>::VisitStmtExpr(const StmtExpr *E) {
3773   BlockScope<Emitter> BS(this);
3774   StmtExprScope<Emitter> SS(this);
3775 
3776   const CompoundStmt *CS = E->getSubStmt();
3777   const Stmt *Result = CS->getStmtExprResult();
3778   for (const Stmt *S : CS->body()) {
3779     if (S != Result) {
3780       if (!this->visitStmt(S))
3781         return false;
3782       continue;
3783     }
3784 
3785     assert(S == Result);
3786     if (const Expr *ResultExpr = dyn_cast<Expr>(S))
3787       return this->delegate(ResultExpr);
3788     return this->emitUnsupported(E);
3789   }
3790 
3791   return BS.destroyLocals();
3792 }
3793 
3794 template <class Emitter> bool Compiler<Emitter>::discard(const Expr *E) {
3795   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
3796                              /*NewInitializing=*/false);
3797   return this->Visit(E);
3798 }
3799 
3800 template <class Emitter> bool Compiler<Emitter>::delegate(const Expr *E) {
3801   // We're basically doing:
3802   // OptionScope<Emitter> Scope(this, DicardResult, Initializing);
3803   // but that's unnecessary of course.
3804   return this->Visit(E);
3805 }
3806 
3807 template <class Emitter> bool Compiler<Emitter>::visit(const Expr *E) {
3808   if (E->getType().isNull())
3809     return false;
3810 
3811   if (E->getType()->isVoidType())
3812     return this->discard(E);
3813 
3814   // Create local variable to hold the return value.
3815   if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
3816       !classify(E->getType())) {
3817     std::optional<unsigned> LocalIndex = allocateLocal(E);
3818     if (!LocalIndex)
3819       return false;
3820 
3821     if (!this->emitGetPtrLocal(*LocalIndex, E))
3822       return false;
3823     InitLinkScope<Emitter> ILS(this, InitLink::Temp(*LocalIndex));
3824     return this->visitInitializer(E);
3825   }
3826 
3827   //  Otherwise,we have a primitive return value, produce the value directly
3828   //  and push it on the stack.
3829   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
3830                              /*NewInitializing=*/false);
3831   return this->Visit(E);
3832 }
3833 
3834 template <class Emitter>
3835 bool Compiler<Emitter>::visitInitializer(const Expr *E) {
3836   assert(!classify(E->getType()));
3837 
3838   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
3839                              /*NewInitializing=*/true);
3840   return this->Visit(E);
3841 }
3842 
3843 template <class Emitter> bool Compiler<Emitter>::visitBool(const Expr *E) {
3844   std::optional<PrimType> T = classify(E->getType());
3845   if (!T) {
3846     // Convert complex values to bool.
3847     if (E->getType()->isAnyComplexType()) {
3848       if (!this->visit(E))
3849         return false;
3850       return this->emitComplexBoolCast(E);
3851     }
3852     return false;
3853   }
3854 
3855   if (!this->visit(E))
3856     return false;
3857 
3858   if (T == PT_Bool)
3859     return true;
3860 
3861   // Convert pointers to bool.
3862   if (T == PT_Ptr || T == PT_FnPtr) {
3863     if (!this->emitNull(*T, 0, nullptr, E))
3864       return false;
3865     return this->emitNE(*T, E);
3866   }
3867 
3868   // Or Floats.
3869   if (T == PT_Float)
3870     return this->emitCastFloatingIntegralBool(getFPOptions(E), E);
3871 
3872   // Or anything else we can.
3873   return this->emitCast(*T, PT_Bool, E);
3874 }
3875 
3876 template <class Emitter>
3877 bool Compiler<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
3878                                              const Expr *E) {
3879   switch (T) {
3880   case PT_Bool:
3881     return this->emitZeroBool(E);
3882   case PT_Sint8:
3883     return this->emitZeroSint8(E);
3884   case PT_Uint8:
3885     return this->emitZeroUint8(E);
3886   case PT_Sint16:
3887     return this->emitZeroSint16(E);
3888   case PT_Uint16:
3889     return this->emitZeroUint16(E);
3890   case PT_Sint32:
3891     return this->emitZeroSint32(E);
3892   case PT_Uint32:
3893     return this->emitZeroUint32(E);
3894   case PT_Sint64:
3895     return this->emitZeroSint64(E);
3896   case PT_Uint64:
3897     return this->emitZeroUint64(E);
3898   case PT_IntAP:
3899     return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
3900   case PT_IntAPS:
3901     return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
3902   case PT_Ptr:
3903     return this->emitNullPtr(Ctx.getASTContext().getTargetNullPointerValue(QT),
3904                              nullptr, E);
3905   case PT_FnPtr:
3906     return this->emitNullFnPtr(0, nullptr, E);
3907   case PT_MemberPtr:
3908     return this->emitNullMemberPtr(0, nullptr, E);
3909   case PT_Float:
3910     return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E);
3911   case PT_FixedPoint: {
3912     auto Sem = Ctx.getASTContext().getFixedPointSemantics(E->getType());
3913     return this->emitConstFixedPoint(FixedPoint::zero(Sem), E);
3914   }
3915     llvm_unreachable("Implement");
3916   }
3917   llvm_unreachable("unknown primitive type");
3918 }
3919 
3920 template <class Emitter>
3921 bool Compiler<Emitter>::visitZeroRecordInitializer(const Record *R,
3922                                                    const Expr *E) {
3923   assert(E);
3924   assert(R);
3925   // Fields
3926   for (const Record::Field &Field : R->fields()) {
3927     if (Field.Decl->isUnnamedBitField())
3928       continue;
3929 
3930     const Descriptor *D = Field.Desc;
3931     if (D->isPrimitive()) {
3932       QualType QT = D->getType();
3933       PrimType T = classifyPrim(D->getType());
3934       if (!this->visitZeroInitializer(T, QT, E))
3935         return false;
3936       if (!this->emitInitField(T, Field.Offset, E))
3937         return false;
3938       if (R->isUnion())
3939         break;
3940       continue;
3941     }
3942 
3943     if (!this->emitGetPtrField(Field.Offset, E))
3944       return false;
3945 
3946     if (D->isPrimitiveArray()) {
3947       QualType ET = D->getElemQualType();
3948       PrimType T = classifyPrim(ET);
3949       for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
3950         if (!this->visitZeroInitializer(T, ET, E))
3951           return false;
3952         if (!this->emitInitElem(T, I, E))
3953           return false;
3954       }
3955     } else if (D->isCompositeArray()) {
3956       // Can't be a vector or complex field.
3957       if (!this->visitZeroArrayInitializer(D->getType(), E))
3958         return false;
3959     } else if (D->isRecord()) {
3960       if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
3961         return false;
3962     } else {
3963       assert(false);
3964     }
3965 
3966     if (!this->emitFinishInitPop(E))
3967       return false;
3968 
3969     // C++11 [dcl.init]p5: If T is a (possibly cv-qualified) union type, the
3970     // object's first non-static named data member is zero-initialized
3971     if (R->isUnion())
3972       break;
3973   }
3974 
3975   for (const Record::Base &B : R->bases()) {
3976     if (!this->emitGetPtrBase(B.Offset, E))
3977       return false;
3978     if (!this->visitZeroRecordInitializer(B.R, E))
3979       return false;
3980     if (!this->emitFinishInitPop(E))
3981       return false;
3982   }
3983 
3984   // FIXME: Virtual bases.
3985 
3986   return true;
3987 }
3988 
3989 template <class Emitter>
3990 bool Compiler<Emitter>::visitZeroArrayInitializer(QualType T, const Expr *E) {
3991   assert(T->isArrayType() || T->isAnyComplexType() || T->isVectorType());
3992   const ArrayType *AT = T->getAsArrayTypeUnsafe();
3993   QualType ElemType = AT->getElementType();
3994   size_t NumElems = cast<ConstantArrayType>(AT)->getZExtSize();
3995 
3996   if (std::optional<PrimType> ElemT = classify(ElemType)) {
3997     for (size_t I = 0; I != NumElems; ++I) {
3998       if (!this->visitZeroInitializer(*ElemT, ElemType, E))
3999         return false;
4000       if (!this->emitInitElem(*ElemT, I, E))
4001         return false;
4002     }
4003     return true;
4004   } else if (ElemType->isRecordType()) {
4005     const Record *R = getRecord(ElemType);
4006 
4007     for (size_t I = 0; I != NumElems; ++I) {
4008       if (!this->emitConstUint32(I, E))
4009         return false;
4010       if (!this->emitArrayElemPtr(PT_Uint32, E))
4011         return false;
4012       if (!this->visitZeroRecordInitializer(R, E))
4013         return false;
4014       if (!this->emitPopPtr(E))
4015         return false;
4016     }
4017     return true;
4018   } else if (ElemType->isArrayType()) {
4019     for (size_t I = 0; I != NumElems; ++I) {
4020       if (!this->emitConstUint32(I, E))
4021         return false;
4022       if (!this->emitArrayElemPtr(PT_Uint32, E))
4023         return false;
4024       if (!this->visitZeroArrayInitializer(ElemType, E))
4025         return false;
4026       if (!this->emitPopPtr(E))
4027         return false;
4028     }
4029     return true;
4030   }
4031 
4032   return false;
4033 }
4034 
4035 template <class Emitter>
4036 template <typename T>
4037 bool Compiler<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
4038   switch (Ty) {
4039   case PT_Sint8:
4040     return this->emitConstSint8(Value, E);
4041   case PT_Uint8:
4042     return this->emitConstUint8(Value, E);
4043   case PT_Sint16:
4044     return this->emitConstSint16(Value, E);
4045   case PT_Uint16:
4046     return this->emitConstUint16(Value, E);
4047   case PT_Sint32:
4048     return this->emitConstSint32(Value, E);
4049   case PT_Uint32:
4050     return this->emitConstUint32(Value, E);
4051   case PT_Sint64:
4052     return this->emitConstSint64(Value, E);
4053   case PT_Uint64:
4054     return this->emitConstUint64(Value, E);
4055   case PT_Bool:
4056     return this->emitConstBool(Value, E);
4057   case PT_Ptr:
4058   case PT_FnPtr:
4059   case PT_MemberPtr:
4060   case PT_Float:
4061   case PT_IntAP:
4062   case PT_IntAPS:
4063   case PT_FixedPoint:
4064     llvm_unreachable("Invalid integral type");
4065     break;
4066   }
4067   llvm_unreachable("unknown primitive type");
4068 }
4069 
4070 template <class Emitter>
4071 template <typename T>
4072 bool Compiler<Emitter>::emitConst(T Value, const Expr *E) {
4073   return this->emitConst(Value, classifyPrim(E->getType()), E);
4074 }
4075 
4076 template <class Emitter>
4077 bool Compiler<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
4078                                   const Expr *E) {
4079   if (Ty == PT_IntAPS)
4080     return this->emitConstIntAPS(Value, E);
4081   if (Ty == PT_IntAP)
4082     return this->emitConstIntAP(Value, E);
4083 
4084   if (Value.isSigned())
4085     return this->emitConst(Value.getSExtValue(), Ty, E);
4086   return this->emitConst(Value.getZExtValue(), Ty, E);
4087 }
4088 
4089 template <class Emitter>
4090 bool Compiler<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
4091   return this->emitConst(Value, classifyPrim(E->getType()), E);
4092 }
4093 
4094 template <class Emitter>
4095 unsigned Compiler<Emitter>::allocateLocalPrimitive(DeclTy &&Src, PrimType Ty,
4096                                                    bool IsConst,
4097                                                    bool IsExtended) {
4098   // Make sure we don't accidentally register the same decl twice.
4099   if (const auto *VD =
4100           dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
4101     assert(!P.getGlobal(VD));
4102     assert(!Locals.contains(VD));
4103     (void)VD;
4104   }
4105 
4106   // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
4107   //   (int){12} in C. Consider using Expr::isTemporaryObject() instead
4108   //   or isa<MaterializeTemporaryExpr>().
4109   Descriptor *D = P.createDescriptor(Src, Ty, Descriptor::InlineDescMD, IsConst,
4110                                      isa<const Expr *>(Src));
4111   Scope::Local Local = this->createLocal(D);
4112   if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
4113     Locals.insert({VD, Local});
4114   VarScope->add(Local, IsExtended);
4115   return Local.Offset;
4116 }
4117 
4118 template <class Emitter>
4119 std::optional<unsigned>
4120 Compiler<Emitter>::allocateLocal(DeclTy &&Src, QualType Ty,
4121                                  const ValueDecl *ExtendingDecl) {
4122   // Make sure we don't accidentally register the same decl twice.
4123   if ([[maybe_unused]] const auto *VD =
4124           dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
4125     assert(!P.getGlobal(VD));
4126     assert(!Locals.contains(VD));
4127   }
4128 
4129   const ValueDecl *Key = nullptr;
4130   const Expr *Init = nullptr;
4131   bool IsTemporary = false;
4132   if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
4133     Key = VD;
4134     Ty = VD->getType();
4135 
4136     if (const auto *VarD = dyn_cast<VarDecl>(VD))
4137       Init = VarD->getInit();
4138   }
4139   if (auto *E = Src.dyn_cast<const Expr *>()) {
4140     IsTemporary = true;
4141     if (Ty.isNull())
4142       Ty = E->getType();
4143   }
4144 
4145   Descriptor *D = P.createDescriptor(
4146       Src, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
4147       IsTemporary, /*IsMutable=*/false, Init);
4148   if (!D)
4149     return std::nullopt;
4150 
4151   Scope::Local Local = this->createLocal(D);
4152   if (Key)
4153     Locals.insert({Key, Local});
4154   if (ExtendingDecl)
4155     VarScope->addExtended(Local, ExtendingDecl);
4156   else
4157     VarScope->add(Local, false);
4158   return Local.Offset;
4159 }
4160 
4161 template <class Emitter>
4162 unsigned Compiler<Emitter>::allocateTemporary(const Expr *E) {
4163   QualType Ty = E->getType();
4164   assert(!Ty->isRecordType());
4165 
4166   Descriptor *D = P.createDescriptor(
4167       E, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
4168       /*IsTemporary=*/true, /*IsMutable=*/false, /*Init=*/nullptr);
4169   assert(D);
4170 
4171   Scope::Local Local = this->createLocal(D);
4172   VariableScope<Emitter> *S = VarScope;
4173   assert(S);
4174   // Attach to topmost scope.
4175   while (S->getParent())
4176     S = S->getParent();
4177   assert(S && !S->getParent());
4178   S->addLocal(Local);
4179   return Local.Offset;
4180 }
4181 
4182 template <class Emitter>
4183 const RecordType *Compiler<Emitter>::getRecordTy(QualType Ty) {
4184   if (const PointerType *PT = dyn_cast<PointerType>(Ty))
4185     return PT->getPointeeType()->getAs<RecordType>();
4186   return Ty->getAs<RecordType>();
4187 }
4188 
4189 template <class Emitter> Record *Compiler<Emitter>::getRecord(QualType Ty) {
4190   if (const auto *RecordTy = getRecordTy(Ty))
4191     return getRecord(RecordTy->getDecl());
4192   return nullptr;
4193 }
4194 
4195 template <class Emitter>
4196 Record *Compiler<Emitter>::getRecord(const RecordDecl *RD) {
4197   return P.getOrCreateRecord(RD);
4198 }
4199 
4200 template <class Emitter>
4201 const Function *Compiler<Emitter>::getFunction(const FunctionDecl *FD) {
4202   return Ctx.getOrCreateFunction(FD);
4203 }
4204 
4205 template <class Emitter>
4206 bool Compiler<Emitter>::visitExpr(const Expr *E, bool DestroyToplevelScope) {
4207   LocalScope<Emitter> RootScope(this);
4208 
4209   // If we won't destroy the toplevel scope, check for memory leaks first.
4210   if (!DestroyToplevelScope) {
4211     if (!this->emitCheckAllocations(E))
4212       return false;
4213   }
4214 
4215   auto maybeDestroyLocals = [&]() -> bool {
4216     if (DestroyToplevelScope)
4217       return RootScope.destroyLocals() && this->emitCheckAllocations(E);
4218     return this->emitCheckAllocations(E);
4219   };
4220 
4221   // Void expressions.
4222   if (E->getType()->isVoidType()) {
4223     if (!visit(E))
4224       return false;
4225     return this->emitRetVoid(E) && maybeDestroyLocals();
4226   }
4227 
4228   // Expressions with a primitive return type.
4229   if (std::optional<PrimType> T = classify(E)) {
4230     if (!visit(E))
4231       return false;
4232 
4233     return this->emitRet(*T, E) && maybeDestroyLocals();
4234   }
4235 
4236   // Expressions with a composite return type.
4237   // For us, that means everything we don't
4238   // have a PrimType for.
4239   if (std::optional<unsigned> LocalOffset = this->allocateLocal(E)) {
4240     if (!this->emitGetPtrLocal(*LocalOffset, E))
4241       return false;
4242 
4243     if (!visitInitializer(E))
4244       return false;
4245 
4246     if (!this->emitFinishInit(E))
4247       return false;
4248     // We are destroying the locals AFTER the Ret op.
4249     // The Ret op needs to copy the (alive) values, but the
4250     // destructors may still turn the entire expression invalid.
4251     return this->emitRetValue(E) && maybeDestroyLocals();
4252   }
4253 
4254   return maybeDestroyLocals() && this->emitCheckAllocations(E) && false;
4255 }
4256 
4257 template <class Emitter>
4258 VarCreationState Compiler<Emitter>::visitDecl(const VarDecl *VD) {
4259 
4260   auto R = this->visitVarDecl(VD, /*Toplevel=*/true);
4261 
4262   if (R.notCreated())
4263     return R;
4264 
4265   if (R)
4266     return true;
4267 
4268   if (!R && Context::shouldBeGloballyIndexed(VD)) {
4269     if (auto GlobalIndex = P.getGlobal(VD)) {
4270       Block *GlobalBlock = P.getGlobal(*GlobalIndex);
4271       GlobalInlineDescriptor &GD =
4272           *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
4273 
4274       GD.InitState = GlobalInitState::InitializerFailed;
4275       GlobalBlock->invokeDtor();
4276     }
4277   }
4278 
4279   return R;
4280 }
4281 
4282 /// Toplevel visitDeclAndReturn().
4283 /// We get here from evaluateAsInitializer().
4284 /// We need to evaluate the initializer and return its value.
4285 template <class Emitter>
4286 bool Compiler<Emitter>::visitDeclAndReturn(const VarDecl *VD,
4287                                            bool ConstantContext) {
4288   std::optional<PrimType> VarT = classify(VD->getType());
4289 
4290   // We only create variables if we're evaluating in a constant context.
4291   // Otherwise, just evaluate the initializer and return it.
4292   if (!ConstantContext) {
4293     DeclScope<Emitter> LS(this, VD);
4294     if (!this->visit(VD->getAnyInitializer()))
4295       return false;
4296     return this->emitRet(VarT.value_or(PT_Ptr), VD) && LS.destroyLocals() &&
4297            this->emitCheckAllocations(VD);
4298   }
4299 
4300   LocalScope<Emitter> VDScope(this, VD);
4301   if (!this->visitVarDecl(VD, /*Toplevel=*/true))
4302     return false;
4303 
4304   if (Context::shouldBeGloballyIndexed(VD)) {
4305     auto GlobalIndex = P.getGlobal(VD);
4306     assert(GlobalIndex); // visitVarDecl() didn't return false.
4307     if (VarT) {
4308       if (!this->emitGetGlobalUnchecked(*VarT, *GlobalIndex, VD))
4309         return false;
4310     } else {
4311       if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
4312         return false;
4313     }
4314   } else {
4315     auto Local = Locals.find(VD);
4316     assert(Local != Locals.end()); // Same here.
4317     if (VarT) {
4318       if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
4319         return false;
4320     } else {
4321       if (!this->emitGetPtrLocal(Local->second.Offset, VD))
4322         return false;
4323     }
4324   }
4325 
4326   // Return the value.
4327   if (!this->emitRet(VarT.value_or(PT_Ptr), VD)) {
4328     // If the Ret above failed and this is a global variable, mark it as
4329     // uninitialized, even everything else succeeded.
4330     if (Context::shouldBeGloballyIndexed(VD)) {
4331       auto GlobalIndex = P.getGlobal(VD);
4332       assert(GlobalIndex);
4333       Block *GlobalBlock = P.getGlobal(*GlobalIndex);
4334       GlobalInlineDescriptor &GD =
4335           *reinterpret_cast<GlobalInlineDescriptor *>(GlobalBlock->rawData());
4336 
4337       GD.InitState = GlobalInitState::InitializerFailed;
4338       GlobalBlock->invokeDtor();
4339     }
4340     return false;
4341   }
4342 
4343   return VDScope.destroyLocals() && this->emitCheckAllocations(VD);
4344 }
4345 
4346 template <class Emitter>
4347 VarCreationState Compiler<Emitter>::visitVarDecl(const VarDecl *VD,
4348                                                  bool Toplevel) {
4349   // We don't know what to do with these, so just return false.
4350   if (VD->getType().isNull())
4351     return false;
4352 
4353   // This case is EvalEmitter-only. If we won't create any instructions for the
4354   // initializer anyway, don't bother creating the variable in the first place.
4355   if (!this->isActive())
4356     return VarCreationState::NotCreated();
4357 
4358   const Expr *Init = VD->getInit();
4359   std::optional<PrimType> VarT = classify(VD->getType());
4360 
4361   if (Init && Init->isValueDependent())
4362     return false;
4363 
4364   if (Context::shouldBeGloballyIndexed(VD)) {
4365     auto checkDecl = [&]() -> bool {
4366       bool NeedsOp = !Toplevel && VD->isLocalVarDecl() && VD->isStaticLocal();
4367       return !NeedsOp || this->emitCheckDecl(VD, VD);
4368     };
4369 
4370     auto initGlobal = [&](unsigned GlobalIndex) -> bool {
4371       assert(Init);
4372 
4373       if (VarT) {
4374         if (!this->visit(Init))
4375           return checkDecl() && false;
4376 
4377         return checkDecl() && this->emitInitGlobal(*VarT, GlobalIndex, VD);
4378       }
4379 
4380       if (!checkDecl())
4381         return false;
4382 
4383       if (!this->emitGetPtrGlobal(GlobalIndex, Init))
4384         return false;
4385 
4386       if (!visitInitializer(Init))
4387         return false;
4388 
4389       if (!this->emitFinishInit(Init))
4390         return false;
4391 
4392       return this->emitPopPtr(Init);
4393     };
4394 
4395     DeclScope<Emitter> LocalScope(this, VD);
4396 
4397     // We've already seen and initialized this global.
4398     if (std::optional<unsigned> GlobalIndex = P.getGlobal(VD)) {
4399       if (P.getPtrGlobal(*GlobalIndex).isInitialized())
4400         return checkDecl();
4401 
4402       // The previous attempt at initialization might've been unsuccessful,
4403       // so let's try this one.
4404       return Init && checkDecl() && initGlobal(*GlobalIndex);
4405     }
4406 
4407     std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init);
4408 
4409     if (!GlobalIndex)
4410       return false;
4411 
4412     return !Init || (checkDecl() && initGlobal(*GlobalIndex));
4413   } else {
4414     InitLinkScope<Emitter> ILS(this, InitLink::Decl(VD));
4415 
4416     if (VarT) {
4417       unsigned Offset = this->allocateLocalPrimitive(
4418           VD, *VarT, VD->getType().isConstQualified());
4419       if (Init) {
4420         // If this is a toplevel declaration, create a scope for the
4421         // initializer.
4422         if (Toplevel) {
4423           LocalScope<Emitter> Scope(this);
4424           if (!this->visit(Init))
4425             return false;
4426           return this->emitSetLocal(*VarT, Offset, VD) && Scope.destroyLocals();
4427         } else {
4428           if (!this->visit(Init))
4429             return false;
4430           return this->emitSetLocal(*VarT, Offset, VD);
4431         }
4432       }
4433     } else {
4434       if (std::optional<unsigned> Offset = this->allocateLocal(VD)) {
4435         if (!Init)
4436           return true;
4437 
4438         if (!this->emitGetPtrLocal(*Offset, Init))
4439           return false;
4440 
4441         if (!visitInitializer(Init))
4442           return false;
4443 
4444         if (!this->emitFinishInit(Init))
4445           return false;
4446 
4447         return this->emitPopPtr(Init);
4448       }
4449       return false;
4450     }
4451     return true;
4452   }
4453 
4454   return false;
4455 }
4456 
4457 template <class Emitter>
4458 bool Compiler<Emitter>::visitAPValue(const APValue &Val, PrimType ValType,
4459                                      const Expr *E) {
4460   assert(!DiscardResult);
4461   if (Val.isInt())
4462     return this->emitConst(Val.getInt(), ValType, E);
4463   else if (Val.isFloat())
4464     return this->emitConstFloat(Val.getFloat(), E);
4465 
4466   if (Val.isLValue()) {
4467     if (Val.isNullPointer())
4468       return this->emitNull(ValType, 0, nullptr, E);
4469     APValue::LValueBase Base = Val.getLValueBase();
4470     if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
4471       return this->visit(BaseExpr);
4472     else if (const auto *VD = Base.dyn_cast<const ValueDecl *>()) {
4473       return this->visitDeclRef(VD, E);
4474     }
4475   } else if (Val.isMemberPointer()) {
4476     if (const ValueDecl *MemberDecl = Val.getMemberPointerDecl())
4477       return this->emitGetMemberPtr(MemberDecl, E);
4478     return this->emitNullMemberPtr(0, nullptr, E);
4479   }
4480 
4481   return false;
4482 }
4483 
4484 template <class Emitter>
4485 bool Compiler<Emitter>::visitAPValueInitializer(const APValue &Val,
4486                                                 const Expr *E) {
4487 
4488   if (Val.isStruct()) {
4489     const Record *R = this->getRecord(E->getType());
4490     assert(R);
4491     for (unsigned I = 0, N = Val.getStructNumFields(); I != N; ++I) {
4492       const APValue &F = Val.getStructField(I);
4493       const Record::Field *RF = R->getField(I);
4494 
4495       if (F.isInt() || F.isFloat() || F.isLValue() || F.isMemberPointer()) {
4496         PrimType T = classifyPrim(RF->Decl->getType());
4497         if (!this->visitAPValue(F, T, E))
4498           return false;
4499         if (!this->emitInitField(T, RF->Offset, E))
4500           return false;
4501       } else if (F.isArray()) {
4502         assert(RF->Desc->isPrimitiveArray());
4503         const auto *ArrType = RF->Decl->getType()->getAsArrayTypeUnsafe();
4504         PrimType ElemT = classifyPrim(ArrType->getElementType());
4505         assert(ArrType);
4506 
4507         if (!this->emitGetPtrField(RF->Offset, E))
4508           return false;
4509 
4510         for (unsigned A = 0, AN = F.getArraySize(); A != AN; ++A) {
4511           if (!this->visitAPValue(F.getArrayInitializedElt(A), ElemT, E))
4512             return false;
4513           if (!this->emitInitElem(ElemT, A, E))
4514             return false;
4515         }
4516 
4517         if (!this->emitPopPtr(E))
4518           return false;
4519       } else if (F.isStruct() || F.isUnion()) {
4520         if (!this->emitGetPtrField(RF->Offset, E))
4521           return false;
4522         if (!this->visitAPValueInitializer(F, E))
4523           return false;
4524         if (!this->emitPopPtr(E))
4525           return false;
4526       } else {
4527         assert(false && "I don't think this should be possible");
4528       }
4529     }
4530     return true;
4531   } else if (Val.isUnion()) {
4532     const FieldDecl *UnionField = Val.getUnionField();
4533     const Record *R = this->getRecord(UnionField->getParent());
4534     assert(R);
4535     const APValue &F = Val.getUnionValue();
4536     const Record::Field *RF = R->getField(UnionField);
4537     PrimType T = classifyPrim(RF->Decl->getType());
4538     if (!this->visitAPValue(F, T, E))
4539       return false;
4540     return this->emitInitField(T, RF->Offset, E);
4541   }
4542   // TODO: Other types.
4543 
4544   return false;
4545 }
4546 
4547 template <class Emitter>
4548 bool Compiler<Emitter>::VisitBuiltinCallExpr(const CallExpr *E,
4549                                              unsigned BuiltinID) {
4550   const Function *Func = getFunction(E->getDirectCallee());
4551   if (!Func)
4552     return false;
4553 
4554   // For these, we're expected to ultimately return an APValue pointing
4555   // to the CallExpr. This is needed to get the correct codegen.
4556   if (BuiltinID == Builtin::BI__builtin___CFStringMakeConstantString ||
4557       BuiltinID == Builtin::BI__builtin___NSStringMakeConstantString ||
4558       BuiltinID == Builtin::BI__builtin_ptrauth_sign_constant ||
4559       BuiltinID == Builtin::BI__builtin_function_start) {
4560     if (DiscardResult)
4561       return true;
4562     return this->emitDummyPtr(E, E);
4563   }
4564 
4565   QualType ReturnType = E->getType();
4566   std::optional<PrimType> ReturnT = classify(E);
4567 
4568   // Non-primitive return type. Prepare storage.
4569   if (!Initializing && !ReturnT && !ReturnType->isVoidType()) {
4570     std::optional<unsigned> LocalIndex = allocateLocal(E);
4571     if (!LocalIndex)
4572       return false;
4573     if (!this->emitGetPtrLocal(*LocalIndex, E))
4574       return false;
4575   }
4576 
4577   if (!Func->isUnevaluatedBuiltin()) {
4578     // Put arguments on the stack.
4579     for (const auto *Arg : E->arguments()) {
4580       if (!this->visit(Arg))
4581         return false;
4582     }
4583   }
4584 
4585   if (!this->emitCallBI(Func, E, BuiltinID, E))
4586     return false;
4587 
4588   if (DiscardResult && !ReturnType->isVoidType()) {
4589     assert(ReturnT);
4590     return this->emitPop(*ReturnT, E);
4591   }
4592 
4593   return true;
4594 }
4595 
4596 template <class Emitter>
4597 bool Compiler<Emitter>::VisitCallExpr(const CallExpr *E) {
4598   if (unsigned BuiltinID = E->getBuiltinCallee())
4599     return VisitBuiltinCallExpr(E, BuiltinID);
4600 
4601   const FunctionDecl *FuncDecl = E->getDirectCallee();
4602   // Calls to replaceable operator new/operator delete.
4603   if (FuncDecl && FuncDecl->isReplaceableGlobalAllocationFunction()) {
4604     if (FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_New ||
4605         FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Array_New) {
4606       return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_new);
4607     } else {
4608       assert(FuncDecl->getDeclName().getCXXOverloadedOperator() == OO_Delete);
4609       return VisitBuiltinCallExpr(E, Builtin::BI__builtin_operator_delete);
4610     }
4611   }
4612   // Explicit calls to trivial destructors
4613   if (const auto *DD = dyn_cast_if_present<CXXDestructorDecl>(FuncDecl);
4614       DD && DD->isTrivial())
4615     return true;
4616 
4617   QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
4618   std::optional<PrimType> T = classify(ReturnType);
4619   bool HasRVO = !ReturnType->isVoidType() && !T;
4620 
4621   if (HasRVO) {
4622     if (DiscardResult) {
4623       // If we need to discard the return value but the function returns its
4624       // value via an RVO pointer, we need to create one such pointer just
4625       // for this call.
4626       if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
4627         if (!this->emitGetPtrLocal(*LocalIndex, E))
4628           return false;
4629       }
4630     } else {
4631       // We need the result. Prepare a pointer to return or
4632       // dup the current one.
4633       if (!Initializing) {
4634         if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
4635           if (!this->emitGetPtrLocal(*LocalIndex, E))
4636             return false;
4637         }
4638       }
4639       if (!this->emitDupPtr(E))
4640         return false;
4641     }
4642   }
4643 
4644   SmallVector<const Expr *, 8> Args(
4645       llvm::ArrayRef(E->getArgs(), E->getNumArgs()));
4646 
4647   bool IsAssignmentOperatorCall = false;
4648   if (const auto *OCE = dyn_cast<CXXOperatorCallExpr>(E);
4649       OCE && OCE->isAssignmentOp()) {
4650     // Just like with regular assignments, we need to special-case assignment
4651     // operators here and evaluate the RHS (the second arg) before the LHS (the
4652     // first arg. We fix this by using a Flip op later.
4653     assert(Args.size() == 2);
4654     IsAssignmentOperatorCall = true;
4655     std::reverse(Args.begin(), Args.end());
4656   }
4657   // Calling a static operator will still
4658   // pass the instance, but we don't need it.
4659   // Discard it here.
4660   if (isa<CXXOperatorCallExpr>(E)) {
4661     if (const auto *MD = dyn_cast_if_present<CXXMethodDecl>(FuncDecl);
4662         MD && MD->isStatic()) {
4663       if (!this->discard(E->getArg(0)))
4664         return false;
4665       // Drop first arg.
4666       Args.erase(Args.begin());
4667     }
4668   }
4669 
4670   std::optional<unsigned> CalleeOffset;
4671   // Add the (optional, implicit) This pointer.
4672   if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
4673     if (!FuncDecl && classifyPrim(E->getCallee()) == PT_MemberPtr) {
4674       // If we end up creating a CallPtr op for this, we need the base of the
4675       // member pointer as the instance pointer, and later extract the function
4676       // decl as the function pointer.
4677       const Expr *Callee = E->getCallee();
4678       CalleeOffset =
4679           this->allocateLocalPrimitive(Callee, PT_MemberPtr, true, false);
4680       if (!this->visit(Callee))
4681         return false;
4682       if (!this->emitSetLocal(PT_MemberPtr, *CalleeOffset, E))
4683         return false;
4684       if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
4685         return false;
4686       if (!this->emitGetMemberPtrBase(E))
4687         return false;
4688     } else if (!this->visit(MC->getImplicitObjectArgument())) {
4689       return false;
4690     }
4691   } else if (!FuncDecl) {
4692     const Expr *Callee = E->getCallee();
4693     CalleeOffset = this->allocateLocalPrimitive(Callee, PT_FnPtr, true, false);
4694     if (!this->visit(Callee))
4695       return false;
4696     if (!this->emitSetLocal(PT_FnPtr, *CalleeOffset, E))
4697       return false;
4698   }
4699 
4700   llvm::BitVector NonNullArgs = collectNonNullArgs(FuncDecl, Args);
4701   // Put arguments on the stack.
4702   unsigned ArgIndex = 0;
4703   for (const auto *Arg : Args) {
4704     if (!this->visit(Arg))
4705       return false;
4706 
4707     // If we know the callee already, check the known parametrs for nullability.
4708     if (FuncDecl && NonNullArgs[ArgIndex]) {
4709       PrimType ArgT = classify(Arg).value_or(PT_Ptr);
4710       if (ArgT == PT_Ptr || ArgT == PT_FnPtr) {
4711         if (!this->emitCheckNonNullArg(ArgT, Arg))
4712           return false;
4713       }
4714     }
4715     ++ArgIndex;
4716   }
4717 
4718   // Undo the argument reversal we did earlier.
4719   if (IsAssignmentOperatorCall) {
4720     assert(Args.size() == 2);
4721     PrimType Arg1T = classify(Args[0]).value_or(PT_Ptr);
4722     PrimType Arg2T = classify(Args[1]).value_or(PT_Ptr);
4723     if (!this->emitFlip(Arg2T, Arg1T, E))
4724       return false;
4725   }
4726 
4727   if (FuncDecl) {
4728     const Function *Func = getFunction(FuncDecl);
4729     if (!Func)
4730       return false;
4731     assert(HasRVO == Func->hasRVO());
4732 
4733     bool HasQualifier = false;
4734     if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
4735       HasQualifier = ME->hasQualifier();
4736 
4737     bool IsVirtual = false;
4738     if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
4739       IsVirtual = MD->isVirtual();
4740 
4741     // In any case call the function. The return value will end up on the stack
4742     // and if the function has RVO, we already have the pointer on the stack to
4743     // write the result into.
4744     if (IsVirtual && !HasQualifier) {
4745       uint32_t VarArgSize = 0;
4746       unsigned NumParams =
4747           Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
4748       for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
4749         VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
4750 
4751       if (!this->emitCallVirt(Func, VarArgSize, E))
4752         return false;
4753     } else if (Func->isVariadic()) {
4754       uint32_t VarArgSize = 0;
4755       unsigned NumParams =
4756           Func->getNumWrittenParams() + isa<CXXOperatorCallExpr>(E);
4757       for (unsigned I = NumParams, N = E->getNumArgs(); I != N; ++I)
4758         VarArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
4759       if (!this->emitCallVar(Func, VarArgSize, E))
4760         return false;
4761     } else {
4762       if (!this->emitCall(Func, 0, E))
4763         return false;
4764     }
4765   } else {
4766     // Indirect call. Visit the callee, which will leave a FunctionPointer on
4767     // the stack. Cleanup of the returned value if necessary will be done after
4768     // the function call completed.
4769 
4770     // Sum the size of all args from the call expr.
4771     uint32_t ArgSize = 0;
4772     for (unsigned I = 0, N = E->getNumArgs(); I != N; ++I)
4773       ArgSize += align(primSize(classify(E->getArg(I)).value_or(PT_Ptr)));
4774 
4775     // Get the callee, either from a member pointer or function pointer saved in
4776     // CalleeOffset.
4777     if (isa<CXXMemberCallExpr>(E) && CalleeOffset) {
4778       if (!this->emitGetLocal(PT_MemberPtr, *CalleeOffset, E))
4779         return false;
4780       if (!this->emitGetMemberPtrDecl(E))
4781         return false;
4782     } else {
4783       if (!this->emitGetLocal(PT_FnPtr, *CalleeOffset, E))
4784         return false;
4785     }
4786     if (!this->emitCallPtr(ArgSize, E, E))
4787       return false;
4788   }
4789 
4790   // Cleanup for discarded return values.
4791   if (DiscardResult && !ReturnType->isVoidType() && T)
4792     return this->emitPop(*T, E);
4793 
4794   return true;
4795 }
4796 
4797 template <class Emitter>
4798 bool Compiler<Emitter>::VisitCXXDefaultInitExpr(const CXXDefaultInitExpr *E) {
4799   SourceLocScope<Emitter> SLS(this, E);
4800 
4801   return this->delegate(E->getExpr());
4802 }
4803 
4804 template <class Emitter>
4805 bool Compiler<Emitter>::VisitCXXDefaultArgExpr(const CXXDefaultArgExpr *E) {
4806   SourceLocScope<Emitter> SLS(this, E);
4807 
4808   const Expr *SubExpr = E->getExpr();
4809   if (std::optional<PrimType> T = classify(E->getExpr()))
4810     return this->visit(SubExpr);
4811 
4812   assert(Initializing);
4813   return this->visitInitializer(SubExpr);
4814 }
4815 
4816 template <class Emitter>
4817 bool Compiler<Emitter>::VisitCXXBoolLiteralExpr(const CXXBoolLiteralExpr *E) {
4818   if (DiscardResult)
4819     return true;
4820 
4821   return this->emitConstBool(E->getValue(), E);
4822 }
4823 
4824 template <class Emitter>
4825 bool Compiler<Emitter>::VisitCXXNullPtrLiteralExpr(
4826     const CXXNullPtrLiteralExpr *E) {
4827   if (DiscardResult)
4828     return true;
4829 
4830   uint64_t Val = Ctx.getASTContext().getTargetNullPointerValue(E->getType());
4831   return this->emitNullPtr(Val, nullptr, E);
4832 }
4833 
4834 template <class Emitter>
4835 bool Compiler<Emitter>::VisitGNUNullExpr(const GNUNullExpr *E) {
4836   if (DiscardResult)
4837     return true;
4838 
4839   assert(E->getType()->isIntegerType());
4840 
4841   PrimType T = classifyPrim(E->getType());
4842   return this->emitZero(T, E);
4843 }
4844 
4845 template <class Emitter>
4846 bool Compiler<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
4847   if (DiscardResult)
4848     return true;
4849 
4850   if (this->LambdaThisCapture.Offset > 0) {
4851     if (this->LambdaThisCapture.IsPtr)
4852       return this->emitGetThisFieldPtr(this->LambdaThisCapture.Offset, E);
4853     return this->emitGetPtrThisField(this->LambdaThisCapture.Offset, E);
4854   }
4855 
4856   // In some circumstances, the 'this' pointer does not actually refer to the
4857   // instance pointer of the current function frame, but e.g. to the declaration
4858   // currently being initialized. Here we emit the necessary instruction(s) for
4859   // this scenario.
4860   if (!InitStackActive)
4861     return this->emitThis(E);
4862 
4863   if (!InitStack.empty()) {
4864     // If our init stack is, for example:
4865     // 0 Stack: 3 (decl)
4866     // 1 Stack: 6 (init list)
4867     // 2 Stack: 1 (field)
4868     // 3 Stack: 6 (init list)
4869     // 4 Stack: 1 (field)
4870     //
4871     // We want to find the LAST element in it that's an init list,
4872     // which is marked with the K_InitList marker. The index right
4873     // before that points to an init list. We need to find the
4874     // elements before the K_InitList element that point to a base
4875     // (e.g. a decl or This), optionally followed by field, elem, etc.
4876     // In the example above, we want to emit elements [0..2].
4877     unsigned StartIndex = 0;
4878     unsigned EndIndex = 0;
4879     // Find the init list.
4880     for (StartIndex = InitStack.size() - 1; StartIndex > 0; --StartIndex) {
4881       if (InitStack[StartIndex].Kind == InitLink::K_InitList ||
4882           InitStack[StartIndex].Kind == InitLink::K_This) {
4883         EndIndex = StartIndex;
4884         --StartIndex;
4885         break;
4886       }
4887     }
4888 
4889     // Walk backwards to find the base.
4890     for (; StartIndex > 0; --StartIndex) {
4891       if (InitStack[StartIndex].Kind == InitLink::K_InitList)
4892         continue;
4893 
4894       if (InitStack[StartIndex].Kind != InitLink::K_Field &&
4895           InitStack[StartIndex].Kind != InitLink::K_Elem)
4896         break;
4897     }
4898 
4899     // Emit the instructions.
4900     for (unsigned I = StartIndex; I != EndIndex; ++I) {
4901       if (InitStack[I].Kind == InitLink::K_InitList)
4902         continue;
4903       if (!InitStack[I].template emit<Emitter>(this, E))
4904         return false;
4905     }
4906     return true;
4907   }
4908   return this->emitThis(E);
4909 }
4910 
4911 template <class Emitter> bool Compiler<Emitter>::visitStmt(const Stmt *S) {
4912   switch (S->getStmtClass()) {
4913   case Stmt::CompoundStmtClass:
4914     return visitCompoundStmt(cast<CompoundStmt>(S));
4915   case Stmt::DeclStmtClass:
4916     return visitDeclStmt(cast<DeclStmt>(S));
4917   case Stmt::ReturnStmtClass:
4918     return visitReturnStmt(cast<ReturnStmt>(S));
4919   case Stmt::IfStmtClass:
4920     return visitIfStmt(cast<IfStmt>(S));
4921   case Stmt::WhileStmtClass:
4922     return visitWhileStmt(cast<WhileStmt>(S));
4923   case Stmt::DoStmtClass:
4924     return visitDoStmt(cast<DoStmt>(S));
4925   case Stmt::ForStmtClass:
4926     return visitForStmt(cast<ForStmt>(S));
4927   case Stmt::CXXForRangeStmtClass:
4928     return visitCXXForRangeStmt(cast<CXXForRangeStmt>(S));
4929   case Stmt::BreakStmtClass:
4930     return visitBreakStmt(cast<BreakStmt>(S));
4931   case Stmt::ContinueStmtClass:
4932     return visitContinueStmt(cast<ContinueStmt>(S));
4933   case Stmt::SwitchStmtClass:
4934     return visitSwitchStmt(cast<SwitchStmt>(S));
4935   case Stmt::CaseStmtClass:
4936     return visitCaseStmt(cast<CaseStmt>(S));
4937   case Stmt::DefaultStmtClass:
4938     return visitDefaultStmt(cast<DefaultStmt>(S));
4939   case Stmt::AttributedStmtClass:
4940     return visitAttributedStmt(cast<AttributedStmt>(S));
4941   case Stmt::CXXTryStmtClass:
4942     return visitCXXTryStmt(cast<CXXTryStmt>(S));
4943   case Stmt::NullStmtClass:
4944     return true;
4945   // Always invalid statements.
4946   case Stmt::GCCAsmStmtClass:
4947   case Stmt::MSAsmStmtClass:
4948   case Stmt::GotoStmtClass:
4949     return this->emitInvalid(S);
4950   case Stmt::LabelStmtClass:
4951     return this->visitStmt(cast<LabelStmt>(S)->getSubStmt());
4952   default: {
4953     if (const auto *E = dyn_cast<Expr>(S))
4954       return this->discard(E);
4955     return false;
4956   }
4957   }
4958 }
4959 
4960 template <class Emitter>
4961 bool Compiler<Emitter>::visitCompoundStmt(const CompoundStmt *S) {
4962   BlockScope<Emitter> Scope(this);
4963   for (const auto *InnerStmt : S->body())
4964     if (!visitStmt(InnerStmt))
4965       return false;
4966   return Scope.destroyLocals();
4967 }
4968 
4969 template <class Emitter>
4970 bool Compiler<Emitter>::visitDeclStmt(const DeclStmt *DS) {
4971   for (const auto *D : DS->decls()) {
4972     if (isa<StaticAssertDecl, TagDecl, TypedefNameDecl, UsingEnumDecl,
4973             FunctionDecl>(D))
4974       continue;
4975 
4976     const auto *VD = dyn_cast<VarDecl>(D);
4977     if (!VD)
4978       return false;
4979     if (!this->visitVarDecl(VD))
4980       return false;
4981   }
4982 
4983   return true;
4984 }
4985 
4986 template <class Emitter>
4987 bool Compiler<Emitter>::visitReturnStmt(const ReturnStmt *RS) {
4988   if (this->InStmtExpr)
4989     return this->emitUnsupported(RS);
4990 
4991   if (const Expr *RE = RS->getRetValue()) {
4992     LocalScope<Emitter> RetScope(this);
4993     if (ReturnType) {
4994       // Primitive types are simply returned.
4995       if (!this->visit(RE))
4996         return false;
4997       this->emitCleanup();
4998       return this->emitRet(*ReturnType, RS);
4999     } else if (RE->getType()->isVoidType()) {
5000       if (!this->visit(RE))
5001         return false;
5002     } else {
5003       InitLinkScope<Emitter> ILS(this, InitLink::RVO());
5004       // RVO - construct the value in the return location.
5005       if (!this->emitRVOPtr(RE))
5006         return false;
5007       if (!this->visitInitializer(RE))
5008         return false;
5009       if (!this->emitPopPtr(RE))
5010         return false;
5011 
5012       this->emitCleanup();
5013       return this->emitRetVoid(RS);
5014     }
5015   }
5016 
5017   // Void return.
5018   this->emitCleanup();
5019   return this->emitRetVoid(RS);
5020 }
5021 
5022 template <class Emitter> bool Compiler<Emitter>::visitIfStmt(const IfStmt *IS) {
5023   if (auto *CondInit = IS->getInit())
5024     if (!visitStmt(CondInit))
5025       return false;
5026 
5027   if (const DeclStmt *CondDecl = IS->getConditionVariableDeclStmt())
5028     if (!visitDeclStmt(CondDecl))
5029       return false;
5030 
5031   // Compile condition.
5032   if (IS->isNonNegatedConsteval()) {
5033     if (!this->emitIsConstantContext(IS))
5034       return false;
5035   } else if (IS->isNegatedConsteval()) {
5036     if (!this->emitIsConstantContext(IS))
5037       return false;
5038     if (!this->emitInv(IS))
5039       return false;
5040   } else {
5041     if (!this->visitBool(IS->getCond()))
5042       return false;
5043   }
5044 
5045   if (const Stmt *Else = IS->getElse()) {
5046     LabelTy LabelElse = this->getLabel();
5047     LabelTy LabelEnd = this->getLabel();
5048     if (!this->jumpFalse(LabelElse))
5049       return false;
5050     {
5051       LocalScope<Emitter> ThenScope(this);
5052       if (!visitStmt(IS->getThen()))
5053         return false;
5054       if (!ThenScope.destroyLocals())
5055         return false;
5056     }
5057     if (!this->jump(LabelEnd))
5058       return false;
5059     this->emitLabel(LabelElse);
5060     {
5061       LocalScope<Emitter> ElseScope(this);
5062       if (!visitStmt(Else))
5063         return false;
5064       if (!ElseScope.destroyLocals())
5065         return false;
5066     }
5067     this->emitLabel(LabelEnd);
5068   } else {
5069     LabelTy LabelEnd = this->getLabel();
5070     if (!this->jumpFalse(LabelEnd))
5071       return false;
5072     {
5073       LocalScope<Emitter> ThenScope(this);
5074       if (!visitStmt(IS->getThen()))
5075         return false;
5076       if (!ThenScope.destroyLocals())
5077         return false;
5078     }
5079     this->emitLabel(LabelEnd);
5080   }
5081 
5082   return true;
5083 }
5084 
5085 template <class Emitter>
5086 bool Compiler<Emitter>::visitWhileStmt(const WhileStmt *S) {
5087   const Expr *Cond = S->getCond();
5088   const Stmt *Body = S->getBody();
5089 
5090   LabelTy CondLabel = this->getLabel(); // Label before the condition.
5091   LabelTy EndLabel = this->getLabel();  // Label after the loop.
5092   LoopScope<Emitter> LS(this, EndLabel, CondLabel);
5093 
5094   this->fallthrough(CondLabel);
5095   this->emitLabel(CondLabel);
5096 
5097   {
5098     LocalScope<Emitter> CondScope(this);
5099     if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5100       if (!visitDeclStmt(CondDecl))
5101         return false;
5102 
5103     if (!this->visitBool(Cond))
5104       return false;
5105     if (!this->jumpFalse(EndLabel))
5106       return false;
5107 
5108     if (!this->visitStmt(Body))
5109       return false;
5110 
5111     if (!CondScope.destroyLocals())
5112       return false;
5113   }
5114   if (!this->jump(CondLabel))
5115     return false;
5116   this->fallthrough(EndLabel);
5117   this->emitLabel(EndLabel);
5118 
5119   return true;
5120 }
5121 
5122 template <class Emitter> bool Compiler<Emitter>::visitDoStmt(const DoStmt *S) {
5123   const Expr *Cond = S->getCond();
5124   const Stmt *Body = S->getBody();
5125 
5126   LabelTy StartLabel = this->getLabel();
5127   LabelTy EndLabel = this->getLabel();
5128   LabelTy CondLabel = this->getLabel();
5129   LoopScope<Emitter> LS(this, EndLabel, CondLabel);
5130 
5131   this->fallthrough(StartLabel);
5132   this->emitLabel(StartLabel);
5133 
5134   {
5135     LocalScope<Emitter> CondScope(this);
5136     if (!this->visitStmt(Body))
5137       return false;
5138     this->fallthrough(CondLabel);
5139     this->emitLabel(CondLabel);
5140     if (!this->visitBool(Cond))
5141       return false;
5142 
5143     if (!CondScope.destroyLocals())
5144       return false;
5145   }
5146   if (!this->jumpTrue(StartLabel))
5147     return false;
5148 
5149   this->fallthrough(EndLabel);
5150   this->emitLabel(EndLabel);
5151   return true;
5152 }
5153 
5154 template <class Emitter>
5155 bool Compiler<Emitter>::visitForStmt(const ForStmt *S) {
5156   // for (Init; Cond; Inc) { Body }
5157   const Stmt *Init = S->getInit();
5158   const Expr *Cond = S->getCond();
5159   const Expr *Inc = S->getInc();
5160   const Stmt *Body = S->getBody();
5161 
5162   LabelTy EndLabel = this->getLabel();
5163   LabelTy CondLabel = this->getLabel();
5164   LabelTy IncLabel = this->getLabel();
5165   LoopScope<Emitter> LS(this, EndLabel, IncLabel);
5166 
5167   if (Init && !this->visitStmt(Init))
5168     return false;
5169 
5170   this->fallthrough(CondLabel);
5171   this->emitLabel(CondLabel);
5172 
5173   {
5174     LocalScope<Emitter> CondScope(this);
5175     if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5176       if (!visitDeclStmt(CondDecl))
5177         return false;
5178 
5179     if (Cond) {
5180       if (!this->visitBool(Cond))
5181         return false;
5182       if (!this->jumpFalse(EndLabel))
5183         return false;
5184     }
5185 
5186     if (Body && !this->visitStmt(Body))
5187       return false;
5188 
5189     this->fallthrough(IncLabel);
5190     this->emitLabel(IncLabel);
5191     if (Inc && !this->discard(Inc))
5192       return false;
5193 
5194     if (!CondScope.destroyLocals())
5195       return false;
5196   }
5197   if (!this->jump(CondLabel))
5198     return false;
5199 
5200   this->fallthrough(EndLabel);
5201   this->emitLabel(EndLabel);
5202   return true;
5203 }
5204 
5205 template <class Emitter>
5206 bool Compiler<Emitter>::visitCXXForRangeStmt(const CXXForRangeStmt *S) {
5207   const Stmt *Init = S->getInit();
5208   const Expr *Cond = S->getCond();
5209   const Expr *Inc = S->getInc();
5210   const Stmt *Body = S->getBody();
5211   const Stmt *BeginStmt = S->getBeginStmt();
5212   const Stmt *RangeStmt = S->getRangeStmt();
5213   const Stmt *EndStmt = S->getEndStmt();
5214   const VarDecl *LoopVar = S->getLoopVariable();
5215 
5216   LabelTy EndLabel = this->getLabel();
5217   LabelTy CondLabel = this->getLabel();
5218   LabelTy IncLabel = this->getLabel();
5219   LoopScope<Emitter> LS(this, EndLabel, IncLabel);
5220 
5221   // Emit declarations needed in the loop.
5222   if (Init && !this->visitStmt(Init))
5223     return false;
5224   if (!this->visitStmt(RangeStmt))
5225     return false;
5226   if (!this->visitStmt(BeginStmt))
5227     return false;
5228   if (!this->visitStmt(EndStmt))
5229     return false;
5230 
5231   // Now the condition as well as the loop variable assignment.
5232   this->fallthrough(CondLabel);
5233   this->emitLabel(CondLabel);
5234   if (!this->visitBool(Cond))
5235     return false;
5236   if (!this->jumpFalse(EndLabel))
5237     return false;
5238 
5239   if (!this->visitVarDecl(LoopVar))
5240     return false;
5241 
5242   // Body.
5243   {
5244     if (!this->visitStmt(Body))
5245       return false;
5246 
5247     this->fallthrough(IncLabel);
5248     this->emitLabel(IncLabel);
5249     if (!this->discard(Inc))
5250       return false;
5251   }
5252 
5253   if (!this->jump(CondLabel))
5254     return false;
5255 
5256   this->fallthrough(EndLabel);
5257   this->emitLabel(EndLabel);
5258   return true;
5259 }
5260 
5261 template <class Emitter>
5262 bool Compiler<Emitter>::visitBreakStmt(const BreakStmt *S) {
5263   if (!BreakLabel)
5264     return false;
5265 
5266   for (VariableScope<Emitter> *C = VarScope; C != BreakVarScope;
5267        C = C->getParent())
5268     C->emitDestruction();
5269   return this->jump(*BreakLabel);
5270 }
5271 
5272 template <class Emitter>
5273 bool Compiler<Emitter>::visitContinueStmt(const ContinueStmt *S) {
5274   if (!ContinueLabel)
5275     return false;
5276 
5277   for (VariableScope<Emitter> *C = VarScope;
5278        C && C->getParent() != ContinueVarScope; C = C->getParent())
5279     C->emitDestruction();
5280   return this->jump(*ContinueLabel);
5281 }
5282 
5283 template <class Emitter>
5284 bool Compiler<Emitter>::visitSwitchStmt(const SwitchStmt *S) {
5285   const Expr *Cond = S->getCond();
5286   PrimType CondT = this->classifyPrim(Cond->getType());
5287   LocalScope<Emitter> LS(this);
5288 
5289   LabelTy EndLabel = this->getLabel();
5290   OptLabelTy DefaultLabel = std::nullopt;
5291   unsigned CondVar = this->allocateLocalPrimitive(Cond, CondT, true, false);
5292 
5293   if (const auto *CondInit = S->getInit())
5294     if (!visitStmt(CondInit))
5295       return false;
5296 
5297   if (const DeclStmt *CondDecl = S->getConditionVariableDeclStmt())
5298     if (!visitDeclStmt(CondDecl))
5299       return false;
5300 
5301   // Initialize condition variable.
5302   if (!this->visit(Cond))
5303     return false;
5304   if (!this->emitSetLocal(CondT, CondVar, S))
5305     return false;
5306 
5307   CaseMap CaseLabels;
5308   // Create labels and comparison ops for all case statements.
5309   for (const SwitchCase *SC = S->getSwitchCaseList(); SC;
5310        SC = SC->getNextSwitchCase()) {
5311     if (const auto *CS = dyn_cast<CaseStmt>(SC)) {
5312       // FIXME: Implement ranges.
5313       if (CS->caseStmtIsGNURange())
5314         return false;
5315       CaseLabels[SC] = this->getLabel();
5316 
5317       const Expr *Value = CS->getLHS();
5318       PrimType ValueT = this->classifyPrim(Value->getType());
5319 
5320       // Compare the case statement's value to the switch condition.
5321       if (!this->emitGetLocal(CondT, CondVar, CS))
5322         return false;
5323       if (!this->visit(Value))
5324         return false;
5325 
5326       // Compare and jump to the case label.
5327       if (!this->emitEQ(ValueT, S))
5328         return false;
5329       if (!this->jumpTrue(CaseLabels[CS]))
5330         return false;
5331     } else {
5332       assert(!DefaultLabel);
5333       DefaultLabel = this->getLabel();
5334     }
5335   }
5336 
5337   // If none of the conditions above were true, fall through to the default
5338   // statement or jump after the switch statement.
5339   if (DefaultLabel) {
5340     if (!this->jump(*DefaultLabel))
5341       return false;
5342   } else {
5343     if (!this->jump(EndLabel))
5344       return false;
5345   }
5346 
5347   SwitchScope<Emitter> SS(this, std::move(CaseLabels), EndLabel, DefaultLabel);
5348   if (!this->visitStmt(S->getBody()))
5349     return false;
5350   this->emitLabel(EndLabel);
5351 
5352   return LS.destroyLocals();
5353 }
5354 
5355 template <class Emitter>
5356 bool Compiler<Emitter>::visitCaseStmt(const CaseStmt *S) {
5357   this->emitLabel(CaseLabels[S]);
5358   return this->visitStmt(S->getSubStmt());
5359 }
5360 
5361 template <class Emitter>
5362 bool Compiler<Emitter>::visitDefaultStmt(const DefaultStmt *S) {
5363   this->emitLabel(*DefaultLabel);
5364   return this->visitStmt(S->getSubStmt());
5365 }
5366 
5367 template <class Emitter>
5368 bool Compiler<Emitter>::visitAttributedStmt(const AttributedStmt *S) {
5369   if (this->Ctx.getLangOpts().CXXAssumptions &&
5370       !this->Ctx.getLangOpts().MSVCCompat) {
5371     for (const Attr *A : S->getAttrs()) {
5372       auto *AA = dyn_cast<CXXAssumeAttr>(A);
5373       if (!AA)
5374         continue;
5375 
5376       assert(isa<NullStmt>(S->getSubStmt()));
5377 
5378       const Expr *Assumption = AA->getAssumption();
5379       if (Assumption->isValueDependent())
5380         return false;
5381 
5382       if (Assumption->HasSideEffects(this->Ctx.getASTContext()))
5383         continue;
5384 
5385       // Evaluate assumption.
5386       if (!this->visitBool(Assumption))
5387         return false;
5388 
5389       if (!this->emitAssume(Assumption))
5390         return false;
5391     }
5392   }
5393 
5394   // Ignore other attributes.
5395   return this->visitStmt(S->getSubStmt());
5396 }
5397 
5398 template <class Emitter>
5399 bool Compiler<Emitter>::visitCXXTryStmt(const CXXTryStmt *S) {
5400   // Ignore all handlers.
5401   return this->visitStmt(S->getTryBlock());
5402 }
5403 
5404 template <class Emitter>
5405 bool Compiler<Emitter>::emitLambdaStaticInvokerBody(const CXXMethodDecl *MD) {
5406   assert(MD->isLambdaStaticInvoker());
5407   assert(MD->hasBody());
5408   assert(cast<CompoundStmt>(MD->getBody())->body_empty());
5409 
5410   const CXXRecordDecl *ClosureClass = MD->getParent();
5411   const CXXMethodDecl *LambdaCallOp = ClosureClass->getLambdaCallOperator();
5412   assert(ClosureClass->captures_begin() == ClosureClass->captures_end());
5413   const Function *Func = this->getFunction(LambdaCallOp);
5414   if (!Func)
5415     return false;
5416   assert(Func->hasThisPointer());
5417   assert(Func->getNumParams() == (MD->getNumParams() + 1 + Func->hasRVO()));
5418 
5419   if (Func->hasRVO()) {
5420     if (!this->emitRVOPtr(MD))
5421       return false;
5422   }
5423 
5424   // The lambda call operator needs an instance pointer, but we don't have
5425   // one here, and we don't need one either because the lambda cannot have
5426   // any captures, as verified above. Emit a null pointer. This is then
5427   // special-cased when interpreting to not emit any misleading diagnostics.
5428   if (!this->emitNullPtr(0, nullptr, MD))
5429     return false;
5430 
5431   // Forward all arguments from the static invoker to the lambda call operator.
5432   for (const ParmVarDecl *PVD : MD->parameters()) {
5433     auto It = this->Params.find(PVD);
5434     assert(It != this->Params.end());
5435 
5436     // We do the lvalue-to-rvalue conversion manually here, so no need
5437     // to care about references.
5438     PrimType ParamType = this->classify(PVD->getType()).value_or(PT_Ptr);
5439     if (!this->emitGetParam(ParamType, It->second.Offset, MD))
5440       return false;
5441   }
5442 
5443   if (!this->emitCall(Func, 0, LambdaCallOp))
5444     return false;
5445 
5446   this->emitCleanup();
5447   if (ReturnType)
5448     return this->emitRet(*ReturnType, MD);
5449 
5450   // Nothing to do, since we emitted the RVO pointer above.
5451   return this->emitRetVoid(MD);
5452 }
5453 
5454 template <class Emitter>
5455 bool Compiler<Emitter>::checkLiteralType(const Expr *E) {
5456   if (Ctx.getLangOpts().CPlusPlus23)
5457     return true;
5458 
5459   if (!E->isPRValue() || E->getType()->isLiteralType(Ctx.getASTContext()))
5460     return true;
5461 
5462   return this->emitCheckLiteralType(E->getType().getTypePtr(), E);
5463 }
5464 
5465 template <class Emitter>
5466 bool Compiler<Emitter>::compileConstructor(const CXXConstructorDecl *Ctor) {
5467   assert(!ReturnType);
5468 
5469   auto emitFieldInitializer = [&](const Record::Field *F, unsigned FieldOffset,
5470                                   const Expr *InitExpr) -> bool {
5471     // We don't know what to do with these, so just return false.
5472     if (InitExpr->getType().isNull())
5473       return false;
5474 
5475     if (std::optional<PrimType> T = this->classify(InitExpr)) {
5476       if (!this->visit(InitExpr))
5477         return false;
5478 
5479       if (F->isBitField())
5480         return this->emitInitThisBitField(*T, F, FieldOffset, InitExpr);
5481       return this->emitInitThisField(*T, FieldOffset, InitExpr);
5482     }
5483     // Non-primitive case. Get a pointer to the field-to-initialize
5484     // on the stack and call visitInitialzer() for it.
5485     InitLinkScope<Emitter> FieldScope(this, InitLink::Field(F->Offset));
5486     if (!this->emitGetPtrThisField(FieldOffset, InitExpr))
5487       return false;
5488 
5489     if (!this->visitInitializer(InitExpr))
5490       return false;
5491 
5492     return this->emitFinishInitPop(InitExpr);
5493   };
5494 
5495   const RecordDecl *RD = Ctor->getParent();
5496   const Record *R = this->getRecord(RD);
5497   if (!R)
5498     return false;
5499 
5500   if (R->isUnion() && Ctor->isCopyOrMoveConstructor()) {
5501     // union copy and move ctors are special.
5502     assert(cast<CompoundStmt>(Ctor->getBody())->body_empty());
5503     if (!this->emitThis(Ctor))
5504       return false;
5505 
5506     auto PVD = Ctor->getParamDecl(0);
5507     ParamOffset PO = this->Params[PVD]; // Must exist.
5508 
5509     if (!this->emitGetParam(PT_Ptr, PO.Offset, Ctor))
5510       return false;
5511 
5512     return this->emitMemcpy(Ctor) && this->emitPopPtr(Ctor) &&
5513            this->emitRetVoid(Ctor);
5514   }
5515 
5516   InitLinkScope<Emitter> InitScope(this, InitLink::This());
5517   for (const auto *Init : Ctor->inits()) {
5518     // Scope needed for the initializers.
5519     BlockScope<Emitter> Scope(this);
5520 
5521     const Expr *InitExpr = Init->getInit();
5522     if (const FieldDecl *Member = Init->getMember()) {
5523       const Record::Field *F = R->getField(Member);
5524 
5525       if (!emitFieldInitializer(F, F->Offset, InitExpr))
5526         return false;
5527     } else if (const Type *Base = Init->getBaseClass()) {
5528       const auto *BaseDecl = Base->getAsCXXRecordDecl();
5529       assert(BaseDecl);
5530 
5531       if (Init->isBaseVirtual()) {
5532         assert(R->getVirtualBase(BaseDecl));
5533         if (!this->emitGetPtrThisVirtBase(BaseDecl, InitExpr))
5534           return false;
5535 
5536       } else {
5537         // Base class initializer.
5538         // Get This Base and call initializer on it.
5539         const Record::Base *B = R->getBase(BaseDecl);
5540         assert(B);
5541         if (!this->emitGetPtrThisBase(B->Offset, InitExpr))
5542           return false;
5543       }
5544 
5545       if (!this->visitInitializer(InitExpr))
5546         return false;
5547       if (!this->emitFinishInitPop(InitExpr))
5548         return false;
5549     } else if (const IndirectFieldDecl *IFD = Init->getIndirectMember()) {
5550       assert(IFD->getChainingSize() >= 2);
5551 
5552       unsigned NestedFieldOffset = 0;
5553       const Record::Field *NestedField = nullptr;
5554       for (const NamedDecl *ND : IFD->chain()) {
5555         const auto *FD = cast<FieldDecl>(ND);
5556         const Record *FieldRecord = this->P.getOrCreateRecord(FD->getParent());
5557         assert(FieldRecord);
5558 
5559         NestedField = FieldRecord->getField(FD);
5560         assert(NestedField);
5561 
5562         NestedFieldOffset += NestedField->Offset;
5563       }
5564       assert(NestedField);
5565 
5566       if (!emitFieldInitializer(NestedField, NestedFieldOffset, InitExpr))
5567         return false;
5568     } else {
5569       assert(Init->isDelegatingInitializer());
5570       if (!this->emitThis(InitExpr))
5571         return false;
5572       if (!this->visitInitializer(Init->getInit()))
5573         return false;
5574       if (!this->emitPopPtr(InitExpr))
5575         return false;
5576     }
5577 
5578     if (!Scope.destroyLocals())
5579       return false;
5580   }
5581 
5582   if (const auto *Body = Ctor->getBody())
5583     if (!visitStmt(Body))
5584       return false;
5585 
5586   return this->emitRetVoid(SourceInfo{});
5587 }
5588 
5589 template <class Emitter>
5590 bool Compiler<Emitter>::compileDestructor(const CXXDestructorDecl *Dtor) {
5591   const RecordDecl *RD = Dtor->getParent();
5592   const Record *R = this->getRecord(RD);
5593   if (!R)
5594     return false;
5595 
5596   if (!Dtor->isTrivial() && Dtor->getBody()) {
5597     if (!this->visitStmt(Dtor->getBody()))
5598       return false;
5599   }
5600 
5601   if (!this->emitThis(Dtor))
5602     return false;
5603 
5604   assert(R);
5605   if (!R->isUnion()) {
5606     // First, destroy all fields.
5607     for (const Record::Field &Field : llvm::reverse(R->fields())) {
5608       const Descriptor *D = Field.Desc;
5609       if (!D->isPrimitive() && !D->isPrimitiveArray()) {
5610         if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
5611           return false;
5612         if (!this->emitDestruction(D, SourceInfo{}))
5613           return false;
5614         if (!this->emitPopPtr(SourceInfo{}))
5615           return false;
5616       }
5617     }
5618   }
5619 
5620   for (const Record::Base &Base : llvm::reverse(R->bases())) {
5621     if (Base.R->isAnonymousUnion())
5622       continue;
5623 
5624     if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
5625       return false;
5626     if (!this->emitRecordDestruction(Base.R, {}))
5627       return false;
5628     if (!this->emitPopPtr(SourceInfo{}))
5629       return false;
5630   }
5631 
5632   // FIXME: Virtual bases.
5633   return this->emitPopPtr(Dtor) && this->emitRetVoid(Dtor);
5634 }
5635 
5636 template <class Emitter>
5637 bool Compiler<Emitter>::visitFunc(const FunctionDecl *F) {
5638   // Classify the return type.
5639   ReturnType = this->classify(F->getReturnType());
5640 
5641   if (const auto *Ctor = dyn_cast<CXXConstructorDecl>(F))
5642     return this->compileConstructor(Ctor);
5643   if (const auto *Dtor = dyn_cast<CXXDestructorDecl>(F))
5644     return this->compileDestructor(Dtor);
5645 
5646   // Emit custom code if this is a lambda static invoker.
5647   if (const auto *MD = dyn_cast<CXXMethodDecl>(F);
5648       MD && MD->isLambdaStaticInvoker())
5649     return this->emitLambdaStaticInvokerBody(MD);
5650 
5651   // Regular functions.
5652   if (const auto *Body = F->getBody())
5653     if (!visitStmt(Body))
5654       return false;
5655 
5656   // Emit a guard return to protect against a code path missing one.
5657   if (F->getReturnType()->isVoidType())
5658     return this->emitRetVoid(SourceInfo{});
5659   return this->emitNoRet(SourceInfo{});
5660 }
5661 
5662 template <class Emitter>
5663 bool Compiler<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
5664   const Expr *SubExpr = E->getSubExpr();
5665   if (SubExpr->getType()->isAnyComplexType())
5666     return this->VisitComplexUnaryOperator(E);
5667   if (SubExpr->getType()->isVectorType())
5668     return this->VisitVectorUnaryOperator(E);
5669   if (SubExpr->getType()->isFixedPointType())
5670     return this->VisitFixedPointUnaryOperator(E);
5671   std::optional<PrimType> T = classify(SubExpr->getType());
5672 
5673   switch (E->getOpcode()) {
5674   case UO_PostInc: { // x++
5675     if (!Ctx.getLangOpts().CPlusPlus14)
5676       return this->emitInvalid(E);
5677     if (!T)
5678       return this->emitError(E);
5679 
5680     if (!this->visit(SubExpr))
5681       return false;
5682 
5683     if (T == PT_Ptr || T == PT_FnPtr) {
5684       if (!this->emitIncPtr(E))
5685         return false;
5686 
5687       return DiscardResult ? this->emitPopPtr(E) : true;
5688     }
5689 
5690     if (T == PT_Float) {
5691       return DiscardResult ? this->emitIncfPop(getFPOptions(E), E)
5692                            : this->emitIncf(getFPOptions(E), E);
5693     }
5694 
5695     return DiscardResult ? this->emitIncPop(*T, E) : this->emitInc(*T, E);
5696   }
5697   case UO_PostDec: { // x--
5698     if (!Ctx.getLangOpts().CPlusPlus14)
5699       return this->emitInvalid(E);
5700     if (!T)
5701       return this->emitError(E);
5702 
5703     if (!this->visit(SubExpr))
5704       return false;
5705 
5706     if (T == PT_Ptr || T == PT_FnPtr) {
5707       if (!this->emitDecPtr(E))
5708         return false;
5709 
5710       return DiscardResult ? this->emitPopPtr(E) : true;
5711     }
5712 
5713     if (T == PT_Float) {
5714       return DiscardResult ? this->emitDecfPop(getFPOptions(E), E)
5715                            : this->emitDecf(getFPOptions(E), E);
5716     }
5717 
5718     return DiscardResult ? this->emitDecPop(*T, E) : this->emitDec(*T, E);
5719   }
5720   case UO_PreInc: { // ++x
5721     if (!Ctx.getLangOpts().CPlusPlus14)
5722       return this->emitInvalid(E);
5723     if (!T)
5724       return this->emitError(E);
5725 
5726     if (!this->visit(SubExpr))
5727       return false;
5728 
5729     if (T == PT_Ptr || T == PT_FnPtr) {
5730       if (!this->emitLoadPtr(E))
5731         return false;
5732       if (!this->emitConstUint8(1, E))
5733         return false;
5734       if (!this->emitAddOffsetUint8(E))
5735         return false;
5736       return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
5737     }
5738 
5739     // Post-inc and pre-inc are the same if the value is to be discarded.
5740     if (DiscardResult) {
5741       if (T == PT_Float)
5742         return this->emitIncfPop(getFPOptions(E), E);
5743       return this->emitIncPop(*T, E);
5744     }
5745 
5746     if (T == PT_Float) {
5747       const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
5748       if (!this->emitLoadFloat(E))
5749         return false;
5750       if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
5751         return false;
5752       if (!this->emitAddf(getFPOptions(E), E))
5753         return false;
5754       if (!this->emitStoreFloat(E))
5755         return false;
5756     } else {
5757       assert(isIntegralType(*T));
5758       if (!this->emitLoad(*T, E))
5759         return false;
5760       if (!this->emitConst(1, E))
5761         return false;
5762       if (!this->emitAdd(*T, E))
5763         return false;
5764       if (!this->emitStore(*T, E))
5765         return false;
5766     }
5767     return E->isGLValue() || this->emitLoadPop(*T, E);
5768   }
5769   case UO_PreDec: { // --x
5770     if (!Ctx.getLangOpts().CPlusPlus14)
5771       return this->emitInvalid(E);
5772     if (!T)
5773       return this->emitError(E);
5774 
5775     if (!this->visit(SubExpr))
5776       return false;
5777 
5778     if (T == PT_Ptr || T == PT_FnPtr) {
5779       if (!this->emitLoadPtr(E))
5780         return false;
5781       if (!this->emitConstUint8(1, E))
5782         return false;
5783       if (!this->emitSubOffsetUint8(E))
5784         return false;
5785       return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
5786     }
5787 
5788     // Post-dec and pre-dec are the same if the value is to be discarded.
5789     if (DiscardResult) {
5790       if (T == PT_Float)
5791         return this->emitDecfPop(getFPOptions(E), E);
5792       return this->emitDecPop(*T, E);
5793     }
5794 
5795     if (T == PT_Float) {
5796       const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
5797       if (!this->emitLoadFloat(E))
5798         return false;
5799       if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
5800         return false;
5801       if (!this->emitSubf(getFPOptions(E), E))
5802         return false;
5803       if (!this->emitStoreFloat(E))
5804         return false;
5805     } else {
5806       assert(isIntegralType(*T));
5807       if (!this->emitLoad(*T, E))
5808         return false;
5809       if (!this->emitConst(1, E))
5810         return false;
5811       if (!this->emitSub(*T, E))
5812         return false;
5813       if (!this->emitStore(*T, E))
5814         return false;
5815     }
5816     return E->isGLValue() || this->emitLoadPop(*T, E);
5817   }
5818   case UO_LNot: // !x
5819     if (!T)
5820       return this->emitError(E);
5821 
5822     if (DiscardResult)
5823       return this->discard(SubExpr);
5824 
5825     if (!this->visitBool(SubExpr))
5826       return false;
5827 
5828     if (!this->emitInv(E))
5829       return false;
5830 
5831     if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
5832       return this->emitCast(PT_Bool, ET, E);
5833     return true;
5834   case UO_Minus: // -x
5835     if (!T)
5836       return this->emitError(E);
5837 
5838     if (!this->visit(SubExpr))
5839       return false;
5840     return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
5841   case UO_Plus: // +x
5842     if (!T)
5843       return this->emitError(E);
5844 
5845     if (!this->visit(SubExpr)) // noop
5846       return false;
5847     return DiscardResult ? this->emitPop(*T, E) : true;
5848   case UO_AddrOf: // &x
5849     if (E->getType()->isMemberPointerType()) {
5850       // C++11 [expr.unary.op]p3 has very strict rules on how the address of a
5851       // member can be formed.
5852       return this->emitGetMemberPtr(cast<DeclRefExpr>(SubExpr)->getDecl(), E);
5853     }
5854     // We should already have a pointer when we get here.
5855     return this->delegate(SubExpr);
5856   case UO_Deref: // *x
5857     if (DiscardResult) {
5858       // assert(false);
5859       return this->discard(SubExpr);
5860     }
5861 
5862     if (!this->visit(SubExpr))
5863       return false;
5864     if (classifyPrim(SubExpr) == PT_Ptr)
5865       return this->emitNarrowPtr(E);
5866     return true;
5867 
5868   case UO_Not: // ~x
5869     if (!T)
5870       return this->emitError(E);
5871 
5872     if (!this->visit(SubExpr))
5873       return false;
5874     return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
5875   case UO_Real: // __real x
5876     assert(T);
5877     return this->delegate(SubExpr);
5878   case UO_Imag: { // __imag x
5879     assert(T);
5880     if (!this->discard(SubExpr))
5881       return false;
5882     return this->visitZeroInitializer(*T, SubExpr->getType(), SubExpr);
5883   }
5884   case UO_Extension:
5885     return this->delegate(SubExpr);
5886   case UO_Coawait:
5887     assert(false && "Unhandled opcode");
5888   }
5889 
5890   return false;
5891 }
5892 
5893 template <class Emitter>
5894 bool Compiler<Emitter>::VisitComplexUnaryOperator(const UnaryOperator *E) {
5895   const Expr *SubExpr = E->getSubExpr();
5896   assert(SubExpr->getType()->isAnyComplexType());
5897 
5898   if (DiscardResult)
5899     return this->discard(SubExpr);
5900 
5901   std::optional<PrimType> ResT = classify(E);
5902   auto prepareResult = [=]() -> bool {
5903     if (!ResT && !Initializing) {
5904       std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
5905       if (!LocalIndex)
5906         return false;
5907       return this->emitGetPtrLocal(*LocalIndex, E);
5908     }
5909 
5910     return true;
5911   };
5912 
5913   // The offset of the temporary, if we created one.
5914   unsigned SubExprOffset = ~0u;
5915   auto createTemp = [=, &SubExprOffset]() -> bool {
5916     SubExprOffset = this->allocateLocalPrimitive(SubExpr, PT_Ptr, true, false);
5917     if (!this->visit(SubExpr))
5918       return false;
5919     return this->emitSetLocal(PT_Ptr, SubExprOffset, E);
5920   };
5921 
5922   PrimType ElemT = classifyComplexElementType(SubExpr->getType());
5923   auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
5924     if (!this->emitGetLocal(PT_Ptr, Offset, E))
5925       return false;
5926     return this->emitArrayElemPop(ElemT, Index, E);
5927   };
5928 
5929   switch (E->getOpcode()) {
5930   case UO_Minus:
5931     if (!prepareResult())
5932       return false;
5933     if (!createTemp())
5934       return false;
5935     for (unsigned I = 0; I != 2; ++I) {
5936       if (!getElem(SubExprOffset, I))
5937         return false;
5938       if (!this->emitNeg(ElemT, E))
5939         return false;
5940       if (!this->emitInitElem(ElemT, I, E))
5941         return false;
5942     }
5943     break;
5944 
5945   case UO_Plus:   // +x
5946   case UO_AddrOf: // &x
5947   case UO_Deref:  // *x
5948     return this->delegate(SubExpr);
5949 
5950   case UO_LNot:
5951     if (!this->visit(SubExpr))
5952       return false;
5953     if (!this->emitComplexBoolCast(SubExpr))
5954       return false;
5955     if (!this->emitInv(E))
5956       return false;
5957     if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
5958       return this->emitCast(PT_Bool, ET, E);
5959     return true;
5960 
5961   case UO_Real:
5962     return this->emitComplexReal(SubExpr);
5963 
5964   case UO_Imag:
5965     if (!this->visit(SubExpr))
5966       return false;
5967 
5968     if (SubExpr->isLValue()) {
5969       if (!this->emitConstUint8(1, E))
5970         return false;
5971       return this->emitArrayElemPtrPopUint8(E);
5972     }
5973 
5974     // Since our _Complex implementation does not map to a primitive type,
5975     // we sometimes have to do the lvalue-to-rvalue conversion here manually.
5976     return this->emitArrayElemPop(classifyPrim(E->getType()), 1, E);
5977 
5978   case UO_Not: // ~x
5979     if (!this->visit(SubExpr))
5980       return false;
5981     // Negate the imaginary component.
5982     if (!this->emitArrayElem(ElemT, 1, E))
5983       return false;
5984     if (!this->emitNeg(ElemT, E))
5985       return false;
5986     if (!this->emitInitElem(ElemT, 1, E))
5987       return false;
5988     return DiscardResult ? this->emitPopPtr(E) : true;
5989 
5990   case UO_Extension:
5991     return this->delegate(SubExpr);
5992 
5993   default:
5994     return this->emitInvalid(E);
5995   }
5996 
5997   return true;
5998 }
5999 
6000 template <class Emitter>
6001 bool Compiler<Emitter>::VisitVectorUnaryOperator(const UnaryOperator *E) {
6002   const Expr *SubExpr = E->getSubExpr();
6003   assert(SubExpr->getType()->isVectorType());
6004 
6005   if (DiscardResult)
6006     return this->discard(SubExpr);
6007 
6008   auto UnaryOp = E->getOpcode();
6009   if (UnaryOp == UO_Extension)
6010     return this->delegate(SubExpr);
6011 
6012   if (UnaryOp != UO_Plus && UnaryOp != UO_Minus && UnaryOp != UO_LNot &&
6013       UnaryOp != UO_Not && UnaryOp != UO_AddrOf)
6014     return this->emitInvalid(E);
6015 
6016   // Nothing to do here.
6017   if (UnaryOp == UO_Plus || UnaryOp == UO_AddrOf)
6018     return this->delegate(SubExpr);
6019 
6020   if (!Initializing) {
6021     std::optional<unsigned> LocalIndex = allocateLocal(SubExpr);
6022     if (!LocalIndex)
6023       return false;
6024     if (!this->emitGetPtrLocal(*LocalIndex, E))
6025       return false;
6026   }
6027 
6028   // The offset of the temporary, if we created one.
6029   unsigned SubExprOffset =
6030       this->allocateLocalPrimitive(SubExpr, PT_Ptr, true, false);
6031   if (!this->visit(SubExpr))
6032     return false;
6033   if (!this->emitSetLocal(PT_Ptr, SubExprOffset, E))
6034     return false;
6035 
6036   const auto *VecTy = SubExpr->getType()->getAs<VectorType>();
6037   PrimType ElemT = classifyVectorElementType(SubExpr->getType());
6038   auto getElem = [=](unsigned Offset, unsigned Index) -> bool {
6039     if (!this->emitGetLocal(PT_Ptr, Offset, E))
6040       return false;
6041     return this->emitArrayElemPop(ElemT, Index, E);
6042   };
6043 
6044   switch (UnaryOp) {
6045   case UO_Minus:
6046     for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
6047       if (!getElem(SubExprOffset, I))
6048         return false;
6049       if (!this->emitNeg(ElemT, E))
6050         return false;
6051       if (!this->emitInitElem(ElemT, I, E))
6052         return false;
6053     }
6054     break;
6055   case UO_LNot: { // !x
6056     // In C++, the logic operators !, &&, || are available for vectors. !v is
6057     // equivalent to v == 0.
6058     //
6059     // The result of the comparison is a vector of the same width and number of
6060     // elements as the comparison operands with a signed integral element type.
6061     //
6062     // https://gcc.gnu.org/onlinedocs/gcc/Vector-Extensions.html
6063     QualType ResultVecTy = E->getType();
6064     PrimType ResultVecElemT =
6065         classifyPrim(ResultVecTy->getAs<VectorType>()->getElementType());
6066     for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
6067       if (!getElem(SubExprOffset, I))
6068         return false;
6069       // operator ! on vectors returns -1 for 'truth', so negate it.
6070       if (!this->emitPrimCast(ElemT, PT_Bool, Ctx.getASTContext().BoolTy, E))
6071         return false;
6072       if (!this->emitInv(E))
6073         return false;
6074       if (!this->emitPrimCast(PT_Bool, ElemT, VecTy->getElementType(), E))
6075         return false;
6076       if (!this->emitNeg(ElemT, E))
6077         return false;
6078       if (ElemT != ResultVecElemT &&
6079           !this->emitPrimCast(ElemT, ResultVecElemT, ResultVecTy, E))
6080         return false;
6081       if (!this->emitInitElem(ResultVecElemT, I, E))
6082         return false;
6083     }
6084     break;
6085   }
6086   case UO_Not: // ~x
6087     for (unsigned I = 0; I != VecTy->getNumElements(); ++I) {
6088       if (!getElem(SubExprOffset, I))
6089         return false;
6090       if (ElemT == PT_Bool) {
6091         if (!this->emitInv(E))
6092           return false;
6093       } else {
6094         if (!this->emitComp(ElemT, E))
6095           return false;
6096       }
6097       if (!this->emitInitElem(ElemT, I, E))
6098         return false;
6099     }
6100     break;
6101   default:
6102     llvm_unreachable("Unsupported unary operators should be handled up front");
6103   }
6104   return true;
6105 }
6106 
6107 template <class Emitter>
6108 bool Compiler<Emitter>::visitDeclRef(const ValueDecl *D, const Expr *E) {
6109   if (DiscardResult)
6110     return true;
6111 
6112   if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
6113     return this->emitConst(ECD->getInitVal(), E);
6114   } else if (const auto *BD = dyn_cast<BindingDecl>(D)) {
6115     return this->visit(BD->getBinding());
6116   } else if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
6117     const Function *F = getFunction(FuncDecl);
6118     return F && this->emitGetFnPtr(F, E);
6119   } else if (const auto *TPOD = dyn_cast<TemplateParamObjectDecl>(D)) {
6120     if (std::optional<unsigned> Index = P.getOrCreateGlobal(D)) {
6121       if (!this->emitGetPtrGlobal(*Index, E))
6122         return false;
6123       if (std::optional<PrimType> T = classify(E->getType())) {
6124         if (!this->visitAPValue(TPOD->getValue(), *T, E))
6125           return false;
6126         return this->emitInitGlobal(*T, *Index, E);
6127       }
6128       return this->visitAPValueInitializer(TPOD->getValue(), E);
6129     }
6130     return false;
6131   }
6132 
6133   // References are implemented via pointers, so when we see a DeclRefExpr
6134   // pointing to a reference, we need to get its value directly (i.e. the
6135   // pointer to the actual value) instead of a pointer to the pointer to the
6136   // value.
6137   bool IsReference = D->getType()->isReferenceType();
6138 
6139   // Check for local/global variables and parameters.
6140   if (auto It = Locals.find(D); It != Locals.end()) {
6141     const unsigned Offset = It->second.Offset;
6142     if (IsReference)
6143       return this->emitGetLocal(PT_Ptr, Offset, E);
6144     return this->emitGetPtrLocal(Offset, E);
6145   } else if (auto GlobalIndex = P.getGlobal(D)) {
6146     if (IsReference) {
6147       if (!Ctx.getLangOpts().CPlusPlus11)
6148         return this->emitGetGlobal(classifyPrim(E), *GlobalIndex, E);
6149       return this->emitGetGlobalUnchecked(classifyPrim(E), *GlobalIndex, E);
6150     }
6151 
6152     return this->emitGetPtrGlobal(*GlobalIndex, E);
6153   } else if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
6154     if (auto It = this->Params.find(PVD); It != this->Params.end()) {
6155       if (IsReference || !It->second.IsPtr)
6156         return this->emitGetParam(classifyPrim(E), It->second.Offset, E);
6157 
6158       return this->emitGetPtrParam(It->second.Offset, E);
6159     }
6160 
6161     if (D->getType()->isReferenceType())
6162       return false; // FIXME: Do we need to emit InvalidDeclRef?
6163   }
6164 
6165   // In case we need to re-visit a declaration.
6166   auto revisit = [&](const VarDecl *VD) -> bool {
6167     auto VarState = this->visitDecl(VD);
6168 
6169     if (VarState.notCreated())
6170       return true;
6171     if (!VarState)
6172       return false;
6173     // Retry.
6174     return this->visitDeclRef(D, E);
6175   };
6176 
6177   // Handle lambda captures.
6178   if (auto It = this->LambdaCaptures.find(D);
6179       It != this->LambdaCaptures.end()) {
6180     auto [Offset, IsPtr] = It->second;
6181 
6182     if (IsPtr)
6183       return this->emitGetThisFieldPtr(Offset, E);
6184     return this->emitGetPtrThisField(Offset, E);
6185   } else if (const auto *DRE = dyn_cast<DeclRefExpr>(E);
6186              DRE && DRE->refersToEnclosingVariableOrCapture()) {
6187     if (const auto *VD = dyn_cast<VarDecl>(D); VD && VD->isInitCapture())
6188       return revisit(VD);
6189   }
6190 
6191   if (D != InitializingDecl) {
6192     // Try to lazily visit (or emit dummy pointers for) declarations
6193     // we haven't seen yet.
6194     if (Ctx.getLangOpts().CPlusPlus) {
6195       if (const auto *VD = dyn_cast<VarDecl>(D)) {
6196         const auto typeShouldBeVisited = [&](QualType T) -> bool {
6197           if (T.isConstant(Ctx.getASTContext()))
6198             return true;
6199           return T->isReferenceType();
6200         };
6201 
6202         // DecompositionDecls are just proxies for us.
6203         if (isa<DecompositionDecl>(VD))
6204           return revisit(VD);
6205 
6206         if ((VD->hasGlobalStorage() || VD->isStaticDataMember()) &&
6207             typeShouldBeVisited(VD->getType()))
6208           return revisit(VD);
6209 
6210         // FIXME: The evaluateValue() check here is a little ridiculous, since
6211         // it will ultimately call into Context::evaluateAsInitializer(). In
6212         // other words, we're evaluating the initializer, just to know if we can
6213         // evaluate the initializer.
6214         if (VD->isLocalVarDecl() && typeShouldBeVisited(VD->getType()) &&
6215             VD->getInit() && !VD->getInit()->isValueDependent()) {
6216 
6217           if (VD->evaluateValue())
6218             return revisit(VD);
6219 
6220           if (!D->getType()->isReferenceType())
6221             return this->emitDummyPtr(D, E);
6222 
6223           return this->emitInvalidDeclRef(cast<DeclRefExpr>(E),
6224                                           /*InitializerFailed=*/true, E);
6225         }
6226       }
6227     } else {
6228       if (const auto *VD = dyn_cast<VarDecl>(D);
6229           VD && VD->getAnyInitializer() &&
6230           VD->getType().isConstant(Ctx.getASTContext()) && !VD->isWeak())
6231         return revisit(VD);
6232     }
6233   }
6234 
6235   return this->emitDummyPtr(D, E);
6236 }
6237 
6238 template <class Emitter>
6239 bool Compiler<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
6240   const auto *D = E->getDecl();
6241   return this->visitDeclRef(D, E);
6242 }
6243 
6244 template <class Emitter> void Compiler<Emitter>::emitCleanup() {
6245   for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent())
6246     C->emitDestruction();
6247 }
6248 
6249 template <class Emitter>
6250 unsigned Compiler<Emitter>::collectBaseOffset(const QualType BaseType,
6251                                               const QualType DerivedType) {
6252   const auto extractRecordDecl = [](QualType Ty) -> const CXXRecordDecl * {
6253     if (const auto *R = Ty->getPointeeCXXRecordDecl())
6254       return R;
6255     return Ty->getAsCXXRecordDecl();
6256   };
6257   const CXXRecordDecl *BaseDecl = extractRecordDecl(BaseType);
6258   const CXXRecordDecl *DerivedDecl = extractRecordDecl(DerivedType);
6259 
6260   return Ctx.collectBaseOffset(BaseDecl, DerivedDecl);
6261 }
6262 
6263 /// Emit casts from a PrimType to another PrimType.
6264 template <class Emitter>
6265 bool Compiler<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
6266                                      QualType ToQT, const Expr *E) {
6267 
6268   if (FromT == PT_Float) {
6269     // Floating to floating.
6270     if (ToT == PT_Float) {
6271       const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
6272       return this->emitCastFP(ToSem, getRoundingMode(E), E);
6273     }
6274 
6275     if (ToT == PT_IntAP)
6276       return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(ToQT),
6277                                               getFPOptions(E), E);
6278     if (ToT == PT_IntAPS)
6279       return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(ToQT),
6280                                                getFPOptions(E), E);
6281 
6282     // Float to integral.
6283     if (isIntegralType(ToT) || ToT == PT_Bool)
6284       return this->emitCastFloatingIntegral(ToT, getFPOptions(E), E);
6285   }
6286 
6287   if (isIntegralType(FromT) || FromT == PT_Bool) {
6288     if (ToT == PT_IntAP)
6289       return this->emitCastAP(FromT, Ctx.getBitWidth(ToQT), E);
6290     if (ToT == PT_IntAPS)
6291       return this->emitCastAPS(FromT, Ctx.getBitWidth(ToQT), E);
6292 
6293     // Integral to integral.
6294     if (isIntegralType(ToT) || ToT == PT_Bool)
6295       return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
6296 
6297     if (ToT == PT_Float) {
6298       // Integral to floating.
6299       const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
6300       return this->emitCastIntegralFloating(FromT, ToSem, getFPOptions(E), E);
6301     }
6302   }
6303 
6304   return false;
6305 }
6306 
6307 /// Emits __real(SubExpr)
6308 template <class Emitter>
6309 bool Compiler<Emitter>::emitComplexReal(const Expr *SubExpr) {
6310   assert(SubExpr->getType()->isAnyComplexType());
6311 
6312   if (DiscardResult)
6313     return this->discard(SubExpr);
6314 
6315   if (!this->visit(SubExpr))
6316     return false;
6317   if (SubExpr->isLValue()) {
6318     if (!this->emitConstUint8(0, SubExpr))
6319       return false;
6320     return this->emitArrayElemPtrPopUint8(SubExpr);
6321   }
6322 
6323   // Rvalue, load the actual element.
6324   return this->emitArrayElemPop(classifyComplexElementType(SubExpr->getType()),
6325                                 0, SubExpr);
6326 }
6327 
6328 template <class Emitter>
6329 bool Compiler<Emitter>::emitComplexBoolCast(const Expr *E) {
6330   assert(!DiscardResult);
6331   PrimType ElemT = classifyComplexElementType(E->getType());
6332   // We emit the expression (__real(E) != 0 || __imag(E) != 0)
6333   // for us, that means (bool)E[0] || (bool)E[1]
6334   if (!this->emitArrayElem(ElemT, 0, E))
6335     return false;
6336   if (ElemT == PT_Float) {
6337     if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
6338       return false;
6339   } else {
6340     if (!this->emitCast(ElemT, PT_Bool, E))
6341       return false;
6342   }
6343 
6344   // We now have the bool value of E[0] on the stack.
6345   LabelTy LabelTrue = this->getLabel();
6346   if (!this->jumpTrue(LabelTrue))
6347     return false;
6348 
6349   if (!this->emitArrayElemPop(ElemT, 1, E))
6350     return false;
6351   if (ElemT == PT_Float) {
6352     if (!this->emitCastFloatingIntegral(PT_Bool, getFPOptions(E), E))
6353       return false;
6354   } else {
6355     if (!this->emitCast(ElemT, PT_Bool, E))
6356       return false;
6357   }
6358   // Leave the boolean value of E[1] on the stack.
6359   LabelTy EndLabel = this->getLabel();
6360   this->jump(EndLabel);
6361 
6362   this->emitLabel(LabelTrue);
6363   if (!this->emitPopPtr(E))
6364     return false;
6365   if (!this->emitConstBool(true, E))
6366     return false;
6367 
6368   this->fallthrough(EndLabel);
6369   this->emitLabel(EndLabel);
6370 
6371   return true;
6372 }
6373 
6374 template <class Emitter>
6375 bool Compiler<Emitter>::emitComplexComparison(const Expr *LHS, const Expr *RHS,
6376                                               const BinaryOperator *E) {
6377   assert(E->isComparisonOp());
6378   assert(!Initializing);
6379   assert(!DiscardResult);
6380 
6381   PrimType ElemT;
6382   bool LHSIsComplex;
6383   unsigned LHSOffset;
6384   if (LHS->getType()->isAnyComplexType()) {
6385     LHSIsComplex = true;
6386     ElemT = classifyComplexElementType(LHS->getType());
6387     LHSOffset = allocateLocalPrimitive(LHS, PT_Ptr, /*IsConst=*/true,
6388                                        /*IsExtended=*/false);
6389     if (!this->visit(LHS))
6390       return false;
6391     if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
6392       return false;
6393   } else {
6394     LHSIsComplex = false;
6395     PrimType LHST = classifyPrim(LHS->getType());
6396     LHSOffset = this->allocateLocalPrimitive(LHS, LHST, true, false);
6397     if (!this->visit(LHS))
6398       return false;
6399     if (!this->emitSetLocal(LHST, LHSOffset, E))
6400       return false;
6401   }
6402 
6403   bool RHSIsComplex;
6404   unsigned RHSOffset;
6405   if (RHS->getType()->isAnyComplexType()) {
6406     RHSIsComplex = true;
6407     ElemT = classifyComplexElementType(RHS->getType());
6408     RHSOffset = allocateLocalPrimitive(RHS, PT_Ptr, /*IsConst=*/true,
6409                                        /*IsExtended=*/false);
6410     if (!this->visit(RHS))
6411       return false;
6412     if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
6413       return false;
6414   } else {
6415     RHSIsComplex = false;
6416     PrimType RHST = classifyPrim(RHS->getType());
6417     RHSOffset = this->allocateLocalPrimitive(RHS, RHST, true, false);
6418     if (!this->visit(RHS))
6419       return false;
6420     if (!this->emitSetLocal(RHST, RHSOffset, E))
6421       return false;
6422   }
6423 
6424   auto getElem = [&](unsigned LocalOffset, unsigned Index,
6425                      bool IsComplex) -> bool {
6426     if (IsComplex) {
6427       if (!this->emitGetLocal(PT_Ptr, LocalOffset, E))
6428         return false;
6429       return this->emitArrayElemPop(ElemT, Index, E);
6430     }
6431     return this->emitGetLocal(ElemT, LocalOffset, E);
6432   };
6433 
6434   for (unsigned I = 0; I != 2; ++I) {
6435     // Get both values.
6436     if (!getElem(LHSOffset, I, LHSIsComplex))
6437       return false;
6438     if (!getElem(RHSOffset, I, RHSIsComplex))
6439       return false;
6440     // And compare them.
6441     if (!this->emitEQ(ElemT, E))
6442       return false;
6443 
6444     if (!this->emitCastBoolUint8(E))
6445       return false;
6446   }
6447 
6448   // We now have two bool values on the stack. Compare those.
6449   if (!this->emitAddUint8(E))
6450     return false;
6451   if (!this->emitConstUint8(2, E))
6452     return false;
6453 
6454   if (E->getOpcode() == BO_EQ) {
6455     if (!this->emitEQUint8(E))
6456       return false;
6457   } else if (E->getOpcode() == BO_NE) {
6458     if (!this->emitNEUint8(E))
6459       return false;
6460   } else
6461     return false;
6462 
6463   // In C, this returns an int.
6464   if (PrimType ResT = classifyPrim(E->getType()); ResT != PT_Bool)
6465     return this->emitCast(PT_Bool, ResT, E);
6466   return true;
6467 }
6468 
6469 /// When calling this, we have a pointer of the local-to-destroy
6470 /// on the stack.
6471 /// Emit destruction of record types (or arrays of record types).
6472 template <class Emitter>
6473 bool Compiler<Emitter>::emitRecordDestruction(const Record *R, SourceInfo Loc) {
6474   assert(R);
6475   assert(!R->isAnonymousUnion());
6476   const CXXDestructorDecl *Dtor = R->getDestructor();
6477   if (!Dtor || Dtor->isTrivial())
6478     return true;
6479 
6480   assert(Dtor);
6481   const Function *DtorFunc = getFunction(Dtor);
6482   if (!DtorFunc)
6483     return false;
6484   assert(DtorFunc->hasThisPointer());
6485   assert(DtorFunc->getNumParams() == 1);
6486   if (!this->emitDupPtr(Loc))
6487     return false;
6488   return this->emitCall(DtorFunc, 0, Loc);
6489 }
6490 /// When calling this, we have a pointer of the local-to-destroy
6491 /// on the stack.
6492 /// Emit destruction of record types (or arrays of record types).
6493 template <class Emitter>
6494 bool Compiler<Emitter>::emitDestruction(const Descriptor *Desc,
6495                                         SourceInfo Loc) {
6496   assert(Desc);
6497   assert(!Desc->isPrimitive());
6498   assert(!Desc->isPrimitiveArray());
6499 
6500   // Arrays.
6501   if (Desc->isArray()) {
6502     const Descriptor *ElemDesc = Desc->ElemDesc;
6503     assert(ElemDesc);
6504 
6505     // Don't need to do anything for these.
6506     if (ElemDesc->isPrimitiveArray())
6507       return true;
6508 
6509     // If this is an array of record types, check if we need
6510     // to call the element destructors at all. If not, try
6511     // to save the work.
6512     if (const Record *ElemRecord = ElemDesc->ElemRecord) {
6513       if (const CXXDestructorDecl *Dtor = ElemRecord->getDestructor();
6514           !Dtor || Dtor->isTrivial())
6515         return true;
6516     }
6517 
6518     for (ssize_t I = Desc->getNumElems() - 1; I >= 0; --I) {
6519       if (!this->emitConstUint64(I, Loc))
6520         return false;
6521       if (!this->emitArrayElemPtrUint64(Loc))
6522         return false;
6523       if (!this->emitDestruction(ElemDesc, Loc))
6524         return false;
6525       if (!this->emitPopPtr(Loc))
6526         return false;
6527     }
6528     return true;
6529   }
6530 
6531   assert(Desc->ElemRecord);
6532   if (Desc->ElemRecord->isAnonymousUnion())
6533     return true;
6534 
6535   return this->emitRecordDestruction(Desc->ElemRecord, Loc);
6536 }
6537 
6538 /// Create a dummy pointer for the given decl (or expr) and
6539 /// push a pointer to it on the stack.
6540 template <class Emitter>
6541 bool Compiler<Emitter>::emitDummyPtr(const DeclTy &D, const Expr *E) {
6542   assert(!DiscardResult && "Should've been checked before");
6543 
6544   unsigned DummyID = P.getOrCreateDummy(D);
6545 
6546   if (!this->emitGetPtrGlobal(DummyID, E))
6547     return false;
6548   if (E->getType()->isVoidType())
6549     return true;
6550 
6551   // Convert the dummy pointer to another pointer type if we have to.
6552   if (PrimType PT = classifyPrim(E); PT != PT_Ptr) {
6553     if (isPtrType(PT))
6554       return this->emitDecayPtr(PT_Ptr, PT, E);
6555     return false;
6556   }
6557   return true;
6558 }
6559 
6560 //  This function is constexpr if and only if To, From, and the types of
6561 //  all subobjects of To and From are types T such that...
6562 //  (3.1) - is_union_v<T> is false;
6563 //  (3.2) - is_pointer_v<T> is false;
6564 //  (3.3) - is_member_pointer_v<T> is false;
6565 //  (3.4) - is_volatile_v<T> is false; and
6566 //  (3.5) - T has no non-static data members of reference type
6567 template <class Emitter>
6568 bool Compiler<Emitter>::emitBuiltinBitCast(const CastExpr *E) {
6569   const Expr *SubExpr = E->getSubExpr();
6570   QualType FromType = SubExpr->getType();
6571   QualType ToType = E->getType();
6572   std::optional<PrimType> ToT = classify(ToType);
6573 
6574   assert(!ToType->isReferenceType());
6575 
6576   // Prepare storage for the result in case we discard.
6577   if (DiscardResult && !Initializing && !ToT) {
6578     std::optional<unsigned> LocalIndex = allocateLocal(E);
6579     if (!LocalIndex)
6580       return false;
6581     if (!this->emitGetPtrLocal(*LocalIndex, E))
6582       return false;
6583   }
6584 
6585   // Get a pointer to the value-to-cast on the stack.
6586   // For CK_LValueToRValueBitCast, this is always an lvalue and
6587   // we later assume it to be one (i.e. a PT_Ptr). However,
6588   // we call this function for other utility methods where
6589   // a bitcast might be useful, so convert it to a PT_Ptr in that case.
6590   if (SubExpr->isGLValue() || FromType->isVectorType()) {
6591     if (!this->visit(SubExpr))
6592       return false;
6593   } else if (std::optional<PrimType> FromT = classify(SubExpr)) {
6594     unsigned TempOffset = allocateLocalPrimitive(
6595         SubExpr, *FromT, /*IsConst=*/true, /*IsExtended=*/false);
6596     if (!this->visit(SubExpr))
6597       return false;
6598     if (!this->emitSetLocal(*FromT, TempOffset, E))
6599       return false;
6600     if (!this->emitGetPtrLocal(TempOffset, E))
6601       return false;
6602   } else {
6603     return false;
6604   }
6605 
6606   if (!ToT) {
6607     if (!this->emitBitCast(E))
6608       return false;
6609     return DiscardResult ? this->emitPopPtr(E) : true;
6610   }
6611   assert(ToT);
6612 
6613   const llvm::fltSemantics *TargetSemantics = nullptr;
6614   if (ToT == PT_Float)
6615     TargetSemantics = &Ctx.getFloatSemantics(ToType);
6616 
6617   // Conversion to a primitive type. FromType can be another
6618   // primitive type, or a record/array.
6619   bool ToTypeIsUChar = (ToType->isSpecificBuiltinType(BuiltinType::UChar) ||
6620                         ToType->isSpecificBuiltinType(BuiltinType::Char_U));
6621   uint32_t ResultBitWidth = std::max(Ctx.getBitWidth(ToType), 8u);
6622 
6623   if (!this->emitBitCastPrim(*ToT, ToTypeIsUChar || ToType->isStdByteType(),
6624                              ResultBitWidth, TargetSemantics, E))
6625     return false;
6626 
6627   if (DiscardResult)
6628     return this->emitPop(*ToT, E);
6629 
6630   return true;
6631 }
6632 
6633 namespace clang {
6634 namespace interp {
6635 
6636 template class Compiler<ByteCodeEmitter>;
6637 template class Compiler<EvalEmitter>;
6638 
6639 } // namespace interp
6640 } // namespace clang
6641