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