1 //===--- ByteCodeExprGen.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 "ByteCodeExprGen.h"
10 #include "ByteCodeEmitter.h"
11 #include "ByteCodeGenError.h"
12 #include "ByteCodeStmtGen.h"
13 #include "Context.h"
14 #include "Floating.h"
15 #include "Function.h"
16 #include "PrimType.h"
17 #include "Program.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 VariableScope<Emitter> {
29 public:
30   DeclScope(ByteCodeExprGen<Emitter> *Ctx, const ValueDecl *VD)
31       : VariableScope<Emitter>(Ctx), Scope(Ctx->P, VD),
32         OldGlobalDecl(Ctx->GlobalDecl) {
33     Ctx->GlobalDecl = Context::shouldBeGloballyIndexed(VD);
34   }
35 
36   void addExtended(const Scope::Local &Local) override {
37     return this->addLocal(Local);
38   }
39 
40   ~DeclScope() { this->Ctx->GlobalDecl = OldGlobalDecl; }
41 
42 private:
43   Program::DeclScope Scope;
44   bool OldGlobalDecl;
45 };
46 
47 /// Scope used to handle initialization methods.
48 template <class Emitter> class OptionScope final {
49 public:
50   /// Root constructor, compiling or discarding primitives.
51   OptionScope(ByteCodeExprGen<Emitter> *Ctx, bool NewDiscardResult,
52               bool NewInitializing)
53       : Ctx(Ctx), OldDiscardResult(Ctx->DiscardResult),
54         OldInitializing(Ctx->Initializing) {
55     Ctx->DiscardResult = NewDiscardResult;
56     Ctx->Initializing = NewInitializing;
57   }
58 
59   ~OptionScope() {
60     Ctx->DiscardResult = OldDiscardResult;
61     Ctx->Initializing = OldInitializing;
62   }
63 
64 private:
65   /// Parent context.
66   ByteCodeExprGen<Emitter> *Ctx;
67   /// Old discard flag to restore.
68   bool OldDiscardResult;
69   bool OldInitializing;
70 };
71 
72 } // namespace interp
73 } // namespace clang
74 
75 template <class Emitter>
76 bool ByteCodeExprGen<Emitter>::VisitCastExpr(const CastExpr *CE) {
77   const Expr *SubExpr = CE->getSubExpr();
78   switch (CE->getCastKind()) {
79 
80   case CK_LValueToRValue: {
81     return dereference(
82         SubExpr, DerefKind::Read,
83         [](PrimType) {
84           // Value loaded - nothing to do here.
85           return true;
86         },
87         [this, CE](PrimType T) {
88           // Pointer on stack - dereference it.
89           if (!this->emitLoadPop(T, CE))
90             return false;
91           return DiscardResult ? this->emitPop(T, CE) : true;
92         });
93   }
94 
95   case CK_UncheckedDerivedToBase:
96   case CK_DerivedToBase: {
97     if (!this->visit(SubExpr))
98       return false;
99 
100     unsigned DerivedOffset = collectBaseOffset(getRecordTy(CE->getType()),
101                                                getRecordTy(SubExpr->getType()));
102 
103     return this->emitGetPtrBasePop(DerivedOffset, CE);
104   }
105 
106   case CK_BaseToDerived: {
107     if (!this->visit(SubExpr))
108       return false;
109 
110     unsigned DerivedOffset = collectBaseOffset(getRecordTy(SubExpr->getType()),
111                                                getRecordTy(CE->getType()));
112 
113     return this->emitGetPtrDerivedPop(DerivedOffset, CE);
114   }
115 
116   case CK_FloatingCast: {
117     if (!this->visit(SubExpr))
118       return false;
119     const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
120     return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
121   }
122 
123   case CK_IntegralToFloating: {
124     std::optional<PrimType> FromT = classify(SubExpr->getType());
125     if (!FromT)
126       return false;
127 
128     if (!this->visit(SubExpr))
129       return false;
130 
131     const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
132     llvm::RoundingMode RM = getRoundingMode(CE);
133     return this->emitCastIntegralFloating(*FromT, TargetSemantics, RM, CE);
134   }
135 
136   case CK_FloatingToBoolean:
137   case CK_FloatingToIntegral: {
138     std::optional<PrimType> ToT = classify(CE->getType());
139 
140     if (!ToT)
141       return false;
142 
143     if (!this->visit(SubExpr))
144       return false;
145 
146     if (ToT == PT_IntAP)
147       return this->emitCastFloatingIntegralAP(Ctx.getBitWidth(CE->getType()),
148                                               CE);
149     if (ToT == PT_IntAPS)
150       return this->emitCastFloatingIntegralAPS(Ctx.getBitWidth(CE->getType()),
151                                                CE);
152 
153     return this->emitCastFloatingIntegral(*ToT, CE);
154   }
155 
156   case CK_NullToPointer:
157     if (DiscardResult)
158       return true;
159     return this->emitNull(classifyPrim(CE->getType()), CE);
160 
161   case CK_PointerToIntegral: {
162     // TODO: Discard handling.
163     if (!this->visit(SubExpr))
164       return false;
165 
166     PrimType T = classifyPrim(CE->getType());
167     return this->emitCastPointerIntegral(T, CE);
168   }
169 
170   case CK_ArrayToPointerDecay: {
171     if (!this->visit(SubExpr))
172       return false;
173     if (!this->emitArrayDecay(CE))
174       return false;
175     if (DiscardResult)
176       return this->emitPopPtr(CE);
177     return true;
178   }
179 
180   case CK_AtomicToNonAtomic:
181   case CK_ConstructorConversion:
182   case CK_FunctionToPointerDecay:
183   case CK_NonAtomicToAtomic:
184   case CK_NoOp:
185   case CK_UserDefinedConversion:
186   case CK_BitCast:
187     return this->delegate(SubExpr);
188 
189   case CK_IntegralToBoolean:
190   case CK_IntegralCast: {
191     if (DiscardResult)
192       return this->discard(SubExpr);
193     std::optional<PrimType> FromT = classify(SubExpr->getType());
194     std::optional<PrimType> ToT = classify(CE->getType());
195 
196     if (!FromT || !ToT)
197       return false;
198 
199     if (!this->visit(SubExpr))
200       return false;
201 
202     if (ToT == PT_IntAP)
203       return this->emitCastAP(*FromT, Ctx.getBitWidth(CE->getType()), CE);
204     if (ToT == PT_IntAPS)
205       return this->emitCastAPS(*FromT, Ctx.getBitWidth(CE->getType()), CE);
206 
207     if (FromT == ToT)
208       return true;
209     return this->emitCast(*FromT, *ToT, CE);
210   }
211 
212   case CK_PointerToBoolean: {
213     PrimType PtrT = classifyPrim(SubExpr->getType());
214 
215     // Just emit p != nullptr for this.
216     if (!this->visit(SubExpr))
217       return false;
218 
219     if (!this->emitNull(PtrT, CE))
220       return false;
221 
222     return this->emitNE(PtrT, CE);
223   }
224 
225   case CK_IntegralComplexToBoolean:
226   case CK_FloatingComplexToBoolean: {
227     std::optional<PrimType> ElemT =
228         classifyComplexElementType(SubExpr->getType());
229     if (!ElemT)
230       return false;
231     // We emit the expression (__real(E) != 0 || __imag(E) != 0)
232     // for us, that means (bool)E[0] || (bool)E[1]
233     if (!this->visit(SubExpr))
234       return false;
235     if (!this->emitConstUint8(0, CE))
236       return false;
237     if (!this->emitArrayElemPtrUint8(CE))
238       return false;
239     if (!this->emitLoadPop(*ElemT, CE))
240       return false;
241     if (*ElemT == PT_Float) {
242       if (!this->emitCastFloatingIntegral(PT_Bool, CE))
243         return false;
244     } else {
245       if (!this->emitCast(*ElemT, PT_Bool, CE))
246         return false;
247     }
248 
249     // We now have the bool value of E[0] on the stack.
250     LabelTy LabelTrue = this->getLabel();
251     if (!this->jumpTrue(LabelTrue))
252       return false;
253 
254     if (!this->emitConstUint8(1, CE))
255       return false;
256     if (!this->emitArrayElemPtrPopUint8(CE))
257       return false;
258     if (!this->emitLoadPop(*ElemT, CE))
259       return false;
260     if (*ElemT == PT_Float) {
261       if (!this->emitCastFloatingIntegral(PT_Bool, CE))
262         return false;
263     } else {
264       if (!this->emitCast(*ElemT, PT_Bool, CE))
265         return false;
266     }
267     // Leave the boolean value of E[1] on the stack.
268     LabelTy EndLabel = this->getLabel();
269     this->jump(EndLabel);
270 
271     this->emitLabel(LabelTrue);
272     if (!this->emitPopPtr(CE))
273       return false;
274     if (!this->emitConstBool(true, CE))
275       return false;
276 
277     this->fallthrough(EndLabel);
278     this->emitLabel(EndLabel);
279 
280     return true;
281   }
282 
283   case CK_ToVoid:
284     return discard(SubExpr);
285 
286   default:
287     assert(false && "Cast not implemented");
288   }
289   llvm_unreachable("Unhandled clang::CastKind enum");
290 }
291 
292 template <class Emitter>
293 bool ByteCodeExprGen<Emitter>::VisitIntegerLiteral(const IntegerLiteral *LE) {
294   if (DiscardResult)
295     return true;
296 
297   return this->emitConst(LE->getValue(), LE);
298 }
299 
300 template <class Emitter>
301 bool ByteCodeExprGen<Emitter>::VisitFloatingLiteral(const FloatingLiteral *E) {
302   if (DiscardResult)
303     return true;
304 
305   return this->emitConstFloat(E->getValue(), E);
306 }
307 
308 template <class Emitter>
309 bool ByteCodeExprGen<Emitter>::VisitParenExpr(const ParenExpr *E) {
310   return this->delegate(E->getSubExpr());
311 }
312 
313 template <class Emitter>
314 bool ByteCodeExprGen<Emitter>::VisitBinaryOperator(const BinaryOperator *BO) {
315   // Need short-circuiting for these.
316   if (BO->isLogicalOp())
317     return this->VisitLogicalBinOp(BO);
318 
319   if (BO->getType()->isAnyComplexType())
320     return this->VisitComplexBinOp(BO);
321 
322   const Expr *LHS = BO->getLHS();
323   const Expr *RHS = BO->getRHS();
324 
325   if (BO->isPtrMemOp())
326     return this->visit(RHS);
327 
328   // Typecheck the args.
329   std::optional<PrimType> LT = classify(LHS->getType());
330   std::optional<PrimType> RT = classify(RHS->getType());
331   std::optional<PrimType> T = classify(BO->getType());
332 
333   // Deal with operations which have composite or void types.
334   if (BO->isCommaOp()) {
335     if (!this->discard(LHS))
336       return false;
337     if (RHS->getType()->isVoidType())
338       return this->discard(RHS);
339 
340     return this->delegate(RHS);
341   }
342 
343   // Special case for C++'s three-way/spaceship operator <=>, which
344   // returns a std::{strong,weak,partial}_ordering (which is a class, so doesn't
345   // have a PrimType).
346   if (!T) {
347     if (DiscardResult)
348       return true;
349     const ComparisonCategoryInfo *CmpInfo =
350         Ctx.getASTContext().CompCategories.lookupInfoForType(BO->getType());
351     assert(CmpInfo);
352 
353     // We need a temporary variable holding our return value.
354     if (!Initializing) {
355       std::optional<unsigned> ResultIndex = this->allocateLocal(BO, false);
356       if (!this->emitGetPtrLocal(*ResultIndex, BO))
357         return false;
358     }
359 
360     if (!visit(LHS) || !visit(RHS))
361       return false;
362 
363     return this->emitCMP3(*LT, CmpInfo, BO);
364   }
365 
366   if (!LT || !RT || !T)
367     return this->bail(BO);
368 
369   // Pointer arithmetic special case.
370   if (BO->getOpcode() == BO_Add || BO->getOpcode() == BO_Sub) {
371     if (T == PT_Ptr || (LT == PT_Ptr && RT == PT_Ptr))
372       return this->VisitPointerArithBinOp(BO);
373   }
374 
375   if (!visit(LHS) || !visit(RHS))
376     return false;
377 
378   // For languages such as C, cast the result of one
379   // of our comparision opcodes to T (which is usually int).
380   auto MaybeCastToBool = [this, T, BO](bool Result) {
381     if (!Result)
382       return false;
383     if (DiscardResult)
384       return this->emitPop(*T, BO);
385     if (T != PT_Bool)
386       return this->emitCast(PT_Bool, *T, BO);
387     return true;
388   };
389 
390   auto Discard = [this, T, BO](bool Result) {
391     if (!Result)
392       return false;
393     return DiscardResult ? this->emitPop(*T, BO) : true;
394   };
395 
396   switch (BO->getOpcode()) {
397   case BO_EQ:
398     return MaybeCastToBool(this->emitEQ(*LT, BO));
399   case BO_NE:
400     return MaybeCastToBool(this->emitNE(*LT, BO));
401   case BO_LT:
402     return MaybeCastToBool(this->emitLT(*LT, BO));
403   case BO_LE:
404     return MaybeCastToBool(this->emitLE(*LT, BO));
405   case BO_GT:
406     return MaybeCastToBool(this->emitGT(*LT, BO));
407   case BO_GE:
408     return MaybeCastToBool(this->emitGE(*LT, BO));
409   case BO_Sub:
410     if (BO->getType()->isFloatingType())
411       return Discard(this->emitSubf(getRoundingMode(BO), BO));
412     return Discard(this->emitSub(*T, BO));
413   case BO_Add:
414     if (BO->getType()->isFloatingType())
415       return Discard(this->emitAddf(getRoundingMode(BO), BO));
416     return Discard(this->emitAdd(*T, BO));
417   case BO_Mul:
418     if (BO->getType()->isFloatingType())
419       return Discard(this->emitMulf(getRoundingMode(BO), BO));
420     return Discard(this->emitMul(*T, BO));
421   case BO_Rem:
422     return Discard(this->emitRem(*T, BO));
423   case BO_Div:
424     if (BO->getType()->isFloatingType())
425       return Discard(this->emitDivf(getRoundingMode(BO), BO));
426     return Discard(this->emitDiv(*T, BO));
427   case BO_Assign:
428     if (DiscardResult)
429       return LHS->refersToBitField() ? this->emitStoreBitFieldPop(*T, BO)
430                                      : this->emitStorePop(*T, BO);
431     return LHS->refersToBitField() ? this->emitStoreBitField(*T, BO)
432                                    : this->emitStore(*T, BO);
433   case BO_And:
434     return Discard(this->emitBitAnd(*T, BO));
435   case BO_Or:
436     return Discard(this->emitBitOr(*T, BO));
437   case BO_Shl:
438     return Discard(this->emitShl(*LT, *RT, BO));
439   case BO_Shr:
440     return Discard(this->emitShr(*LT, *RT, BO));
441   case BO_Xor:
442     return Discard(this->emitBitXor(*T, BO));
443   case BO_LOr:
444   case BO_LAnd:
445     llvm_unreachable("Already handled earlier");
446   default:
447     return this->bail(BO);
448   }
449 
450   llvm_unreachable("Unhandled binary op");
451 }
452 
453 /// Perform addition/subtraction of a pointer and an integer or
454 /// subtraction of two pointers.
455 template <class Emitter>
456 bool ByteCodeExprGen<Emitter>::VisitPointerArithBinOp(const BinaryOperator *E) {
457   BinaryOperatorKind Op = E->getOpcode();
458   const Expr *LHS = E->getLHS();
459   const Expr *RHS = E->getRHS();
460 
461   if ((Op != BO_Add && Op != BO_Sub) ||
462       (!LHS->getType()->isPointerType() && !RHS->getType()->isPointerType()))
463     return false;
464 
465   std::optional<PrimType> LT = classify(LHS);
466   std::optional<PrimType> RT = classify(RHS);
467 
468   if (!LT || !RT)
469     return false;
470 
471   if (LHS->getType()->isPointerType() && RHS->getType()->isPointerType()) {
472     if (Op != BO_Sub)
473       return false;
474 
475     assert(E->getType()->isIntegerType());
476     if (!visit(RHS) || !visit(LHS))
477       return false;
478 
479     return this->emitSubPtr(classifyPrim(E->getType()), E);
480   }
481 
482   PrimType OffsetType;
483   if (LHS->getType()->isIntegerType()) {
484     if (!visit(RHS) || !visit(LHS))
485       return false;
486     OffsetType = *LT;
487   } else if (RHS->getType()->isIntegerType()) {
488     if (!visit(LHS) || !visit(RHS))
489       return false;
490     OffsetType = *RT;
491   } else {
492     return false;
493   }
494 
495   if (Op == BO_Add)
496     return this->emitAddOffset(OffsetType, E);
497   else if (Op == BO_Sub)
498     return this->emitSubOffset(OffsetType, E);
499 
500   return this->bail(E);
501 }
502 
503 template <class Emitter>
504 bool ByteCodeExprGen<Emitter>::VisitLogicalBinOp(const BinaryOperator *E) {
505   assert(E->isLogicalOp());
506   BinaryOperatorKind Op = E->getOpcode();
507   const Expr *LHS = E->getLHS();
508   const Expr *RHS = E->getRHS();
509   std::optional<PrimType> T = classify(E->getType());
510 
511   if (Op == BO_LOr) {
512     // Logical OR. Visit LHS and only evaluate RHS if LHS was FALSE.
513     LabelTy LabelTrue = this->getLabel();
514     LabelTy LabelEnd = this->getLabel();
515 
516     if (!this->visitBool(LHS))
517       return false;
518     if (!this->jumpTrue(LabelTrue))
519       return false;
520 
521     if (!this->visitBool(RHS))
522       return false;
523     if (!this->jump(LabelEnd))
524       return false;
525 
526     this->emitLabel(LabelTrue);
527     this->emitConstBool(true, E);
528     this->fallthrough(LabelEnd);
529     this->emitLabel(LabelEnd);
530 
531   } else {
532     assert(Op == BO_LAnd);
533     // Logical AND.
534     // Visit LHS. Only visit RHS if LHS was TRUE.
535     LabelTy LabelFalse = this->getLabel();
536     LabelTy LabelEnd = this->getLabel();
537 
538     if (!this->visitBool(LHS))
539       return false;
540     if (!this->jumpFalse(LabelFalse))
541       return false;
542 
543     if (!this->visitBool(RHS))
544       return false;
545     if (!this->jump(LabelEnd))
546       return false;
547 
548     this->emitLabel(LabelFalse);
549     this->emitConstBool(false, E);
550     this->fallthrough(LabelEnd);
551     this->emitLabel(LabelEnd);
552   }
553 
554   if (DiscardResult)
555     return this->emitPopBool(E);
556 
557   // For C, cast back to integer type.
558   assert(T);
559   if (T != PT_Bool)
560     return this->emitCast(PT_Bool, *T, E);
561   return true;
562 }
563 
564 template <class Emitter>
565 bool ByteCodeExprGen<Emitter>::VisitComplexBinOp(const BinaryOperator *E) {
566   assert(Initializing);
567 
568   const Expr *LHS = E->getLHS();
569   const Expr *RHS = E->getRHS();
570   PrimType LHSElemT = *this->classifyComplexElementType(LHS->getType());
571   PrimType RHSElemT = *this->classifyComplexElementType(RHS->getType());
572 
573   unsigned LHSOffset = this->allocateLocalPrimitive(LHS, PT_Ptr, true, false);
574   unsigned RHSOffset = this->allocateLocalPrimitive(RHS, PT_Ptr, true, false);
575   unsigned ResultOffset = ~0u;
576   if (!this->DiscardResult)
577     ResultOffset = this->allocateLocalPrimitive(E, PT_Ptr, true, false);
578 
579   assert(LHSElemT == RHSElemT);
580 
581   // Save result pointer in ResultOffset
582   if (!this->DiscardResult) {
583     if (!this->emitDupPtr(E))
584       return false;
585     if (!this->emitSetLocal(PT_Ptr, ResultOffset, E))
586       return false;
587   }
588 
589   // Evaluate LHS and save value to LHSOffset.
590   if (!this->visit(LHS))
591     return false;
592   if (!this->emitSetLocal(PT_Ptr, LHSOffset, E))
593     return false;
594 
595   // Same with RHS.
596   if (!this->visit(RHS))
597     return false;
598   if (!this->emitSetLocal(PT_Ptr, RHSOffset, E))
599     return false;
600 
601   // Now we can get pointers to the LHS and RHS from the offsets above.
602   BinaryOperatorKind Op = E->getOpcode();
603   for (unsigned ElemIndex = 0; ElemIndex != 2; ++ElemIndex) {
604     // Result pointer for the store later.
605     if (!this->DiscardResult) {
606       if (!this->emitGetLocal(PT_Ptr, ResultOffset, E))
607         return false;
608     }
609 
610     if (!this->emitGetLocal(PT_Ptr, LHSOffset, E))
611       return false;
612     if (!this->emitConstUint8(ElemIndex, E))
613       return false;
614     if (!this->emitArrayElemPtrPopUint8(E))
615       return false;
616     if (!this->emitLoadPop(LHSElemT, E))
617       return false;
618 
619     if (!this->emitGetLocal(PT_Ptr, RHSOffset, E))
620       return false;
621     if (!this->emitConstUint8(ElemIndex, E))
622       return false;
623     if (!this->emitArrayElemPtrPopUint8(E))
624       return false;
625     if (!this->emitLoadPop(RHSElemT, E))
626       return false;
627 
628     // The actual operation.
629     switch (Op) {
630     case BO_Add:
631       if (LHSElemT == PT_Float) {
632         if (!this->emitAddf(getRoundingMode(E), E))
633           return false;
634       } else {
635         if (!this->emitAdd(LHSElemT, E))
636           return false;
637       }
638       break;
639     case BO_Sub:
640       if (LHSElemT == PT_Float) {
641         if (!this->emitSubf(getRoundingMode(E), E))
642           return false;
643       } else {
644         if (!this->emitSub(LHSElemT, E))
645           return false;
646       }
647       break;
648 
649     default:
650       return false;
651     }
652 
653     if (!this->DiscardResult) {
654       // Initialize array element with the value we just computed.
655       if (!this->emitInitElemPop(LHSElemT, ElemIndex, E))
656         return false;
657     } else {
658       if (!this->emitPop(LHSElemT, E))
659         return false;
660     }
661   }
662   return true;
663 }
664 
665 template <class Emitter>
666 bool ByteCodeExprGen<Emitter>::VisitImplicitValueInitExpr(const ImplicitValueInitExpr *E) {
667   QualType QT = E->getType();
668 
669   if (std::optional<PrimType> T = classify(QT))
670     return this->visitZeroInitializer(*T, QT, E);
671 
672   if (QT->isRecordType())
673     return false;
674 
675   if (QT->isIncompleteArrayType())
676     return true;
677 
678   if (QT->isArrayType()) {
679     const ArrayType *AT = QT->getAsArrayTypeUnsafe();
680     assert(AT);
681     const auto *CAT = cast<ConstantArrayType>(AT);
682     size_t NumElems = CAT->getSize().getZExtValue();
683     PrimType ElemT = classifyPrim(CAT->getElementType());
684 
685     for (size_t I = 0; I != NumElems; ++I) {
686       if (!this->visitZeroInitializer(ElemT, CAT->getElementType(), E))
687         return false;
688       if (!this->emitInitElem(ElemT, I, E))
689         return false;
690     }
691 
692     return true;
693   }
694 
695   return false;
696 }
697 
698 template <class Emitter>
699 bool ByteCodeExprGen<Emitter>::VisitArraySubscriptExpr(
700     const ArraySubscriptExpr *E) {
701   const Expr *Base = E->getBase();
702   const Expr *Index = E->getIdx();
703 
704   if (DiscardResult)
705     return this->discard(Base) && this->discard(Index);
706 
707   // Take pointer of LHS, add offset from RHS.
708   // What's left on the stack after this is a pointer.
709   if (!this->visit(Base))
710     return false;
711 
712   if (!this->visit(Index))
713     return false;
714 
715   PrimType IndexT = classifyPrim(Index->getType());
716   return this->emitArrayElemPtrPop(IndexT, E);
717 }
718 
719 template <class Emitter>
720 bool ByteCodeExprGen<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
721                                              const Expr *E) {
722   assert(E->getType()->isRecordType());
723   const Record *R = getRecord(E->getType());
724 
725   unsigned InitIndex = 0;
726   for (const Expr *Init : Inits) {
727     if (!this->emitDupPtr(E))
728       return false;
729 
730     if (std::optional<PrimType> T = classify(Init)) {
731       const Record::Field *FieldToInit = R->getField(InitIndex);
732       if (!this->visit(Init))
733         return false;
734 
735       if (FieldToInit->isBitField()) {
736         if (!this->emitInitBitField(*T, FieldToInit, E))
737           return false;
738       } else {
739         if (!this->emitInitField(*T, FieldToInit->Offset, E))
740           return false;
741       }
742 
743       if (!this->emitPopPtr(E))
744         return false;
745       ++InitIndex;
746     } else {
747       // Initializer for a direct base class.
748       if (const Record::Base *B = R->getBase(Init->getType())) {
749         if (!this->emitGetPtrBasePop(B->Offset, Init))
750           return false;
751 
752         if (!this->visitInitializer(Init))
753           return false;
754 
755         if (!this->emitInitPtrPop(E))
756           return false;
757         // Base initializers don't increase InitIndex, since they don't count
758         // into the Record's fields.
759       } else {
760         const Record::Field *FieldToInit = R->getField(InitIndex);
761         // Non-primitive case. Get a pointer to the field-to-initialize
762         // on the stack and recurse into visitInitializer().
763         if (!this->emitGetPtrField(FieldToInit->Offset, Init))
764           return false;
765 
766         if (!this->visitInitializer(Init))
767           return false;
768 
769         if (!this->emitPopPtr(E))
770           return false;
771         ++InitIndex;
772       }
773     }
774   }
775   return true;
776 }
777 
778 /// Pointer to the array(not the element!) must be on the stack when calling
779 /// this.
780 template <class Emitter>
781 bool ByteCodeExprGen<Emitter>::visitArrayElemInit(unsigned ElemIndex,
782                                                   const Expr *Init) {
783   if (std::optional<PrimType> T = classify(Init->getType())) {
784     // Visit the primitive element like normal.
785     if (!this->visit(Init))
786       return false;
787     return this->emitInitElem(*T, ElemIndex, Init);
788   }
789 
790   // Advance the pointer currently on the stack to the given
791   // dimension.
792   if (!this->emitConstUint32(ElemIndex, Init))
793     return false;
794   if (!this->emitArrayElemPtrUint32(Init))
795     return false;
796   if (!this->visitInitializer(Init))
797     return false;
798   return this->emitPopPtr(Init);
799 }
800 
801 template <class Emitter>
802 bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
803   // Handle discarding first.
804   if (DiscardResult) {
805     for (const Expr *Init : E->inits()) {
806       if (!this->discard(Init))
807         return false;
808     }
809     return true;
810   }
811 
812   // Primitive values.
813   if (std::optional<PrimType> T = classify(E->getType())) {
814     assert(!DiscardResult);
815     if (E->getNumInits() == 0)
816       return this->visitZeroInitializer(*T, E->getType(), E);
817     assert(E->getNumInits() == 1);
818     return this->delegate(E->inits()[0]);
819   }
820 
821   QualType T = E->getType();
822   if (T->isRecordType())
823     return this->visitInitList(E->inits(), E);
824 
825   if (T->isArrayType()) {
826     // FIXME: Array fillers.
827     unsigned ElementIndex = 0;
828     for (const Expr *Init : E->inits()) {
829       if (!this->visitArrayElemInit(ElementIndex, Init))
830         return false;
831       ++ElementIndex;
832     }
833     return true;
834   }
835 
836   if (T->isAnyComplexType()) {
837     unsigned NumInits = E->getNumInits();
838     QualType ElemQT = E->getType()->getAs<ComplexType>()->getElementType();
839     PrimType ElemT = classifyPrim(ElemQT);
840     if (NumInits == 0) {
841       // Zero-initialize both elements.
842       for (unsigned I = 0; I < 2; ++I) {
843         if (!this->visitZeroInitializer(ElemT, ElemQT, E))
844           return false;
845         if (!this->emitInitElem(ElemT, I, E))
846           return false;
847       }
848     } else if (NumInits == 2) {
849       unsigned InitIndex = 0;
850       for (const Expr *Init : E->inits()) {
851         if (!this->visit(Init))
852           return false;
853 
854         if (!this->emitInitElem(ElemT, InitIndex, E))
855           return false;
856         ++InitIndex;
857       }
858     }
859     return true;
860   }
861 
862   return false;
863 }
864 
865 template <class Emitter>
866 bool ByteCodeExprGen<Emitter>::VisitCXXParenListInitExpr(
867     const CXXParenListInitExpr *E) {
868   if (DiscardResult) {
869     for (const Expr *Init : E->getInitExprs()) {
870       if (!this->discard(Init))
871         return false;
872     }
873     return true;
874   }
875 
876   assert(E->getType()->isRecordType());
877   return this->visitInitList(E->getInitExprs(), E);
878 }
879 
880 template <class Emitter>
881 bool ByteCodeExprGen<Emitter>::VisitSubstNonTypeTemplateParmExpr(
882     const SubstNonTypeTemplateParmExpr *E) {
883   return this->delegate(E->getReplacement());
884 }
885 
886 template <class Emitter>
887 bool ByteCodeExprGen<Emitter>::VisitConstantExpr(const ConstantExpr *E) {
888   // Try to emit the APValue directly, without visiting the subexpr.
889   // This will only fail if we can't emit the APValue, so won't emit any
890   // diagnostics or any double values.
891   std::optional<PrimType> T = classify(E->getType());
892   if (T && E->hasAPValueResult() &&
893       this->visitAPValue(E->getAPValueResult(), *T, E))
894     return true;
895 
896   return this->delegate(E->getSubExpr());
897 }
898 
899 static CharUnits AlignOfType(QualType T, const ASTContext &ASTCtx,
900                              UnaryExprOrTypeTrait Kind) {
901   bool AlignOfReturnsPreferred =
902       ASTCtx.getLangOpts().getClangABICompat() <= LangOptions::ClangABI::Ver7;
903 
904   // C++ [expr.alignof]p3:
905   //     When alignof is applied to a reference type, the result is the
906   //     alignment of the referenced type.
907   if (const auto *Ref = T->getAs<ReferenceType>())
908     T = Ref->getPointeeType();
909 
910   // __alignof is defined to return the preferred alignment.
911   // Before 8, clang returned the preferred alignment for alignof and
912   // _Alignof as well.
913   if (Kind == UETT_PreferredAlignOf || AlignOfReturnsPreferred)
914     return ASTCtx.toCharUnitsFromBits(ASTCtx.getPreferredTypeAlign(T));
915 
916   return ASTCtx.getTypeAlignInChars(T);
917 }
918 
919 template <class Emitter>
920 bool ByteCodeExprGen<Emitter>::VisitUnaryExprOrTypeTraitExpr(
921     const UnaryExprOrTypeTraitExpr *E) {
922   UnaryExprOrTypeTrait Kind = E->getKind();
923   ASTContext &ASTCtx = Ctx.getASTContext();
924 
925   if (Kind == UETT_SizeOf) {
926     QualType ArgType = E->getTypeOfArgument();
927     CharUnits Size;
928     if (ArgType->isVoidType() || ArgType->isFunctionType())
929       Size = CharUnits::One();
930     else {
931       if (ArgType->isDependentType() || !ArgType->isConstantSizeType())
932         return false;
933 
934       Size = ASTCtx.getTypeSizeInChars(ArgType);
935     }
936 
937     if (DiscardResult)
938       return true;
939 
940     return this->emitConst(Size.getQuantity(), E);
941   }
942 
943   if (Kind == UETT_AlignOf || Kind == UETT_PreferredAlignOf) {
944     CharUnits Size;
945 
946     if (E->isArgumentType()) {
947       QualType ArgType = E->getTypeOfArgument();
948 
949       Size = AlignOfType(ArgType, ASTCtx, Kind);
950     } else {
951       // Argument is an expression, not a type.
952       const Expr *Arg = E->getArgumentExpr()->IgnoreParens();
953 
954       // The kinds of expressions that we have special-case logic here for
955       // should be kept up to date with the special checks for those
956       // expressions in Sema.
957 
958       // alignof decl is always accepted, even if it doesn't make sense: we
959       // default to 1 in those cases.
960       if (const auto *DRE = dyn_cast<DeclRefExpr>(Arg))
961         Size = ASTCtx.getDeclAlign(DRE->getDecl(),
962                                    /*RefAsPointee*/ true);
963       else if (const auto *ME = dyn_cast<MemberExpr>(Arg))
964         Size = ASTCtx.getDeclAlign(ME->getMemberDecl(),
965                                    /*RefAsPointee*/ true);
966       else
967         Size = AlignOfType(Arg->getType(), ASTCtx, Kind);
968     }
969 
970     if (DiscardResult)
971       return true;
972 
973     return this->emitConst(Size.getQuantity(), E);
974   }
975 
976   return false;
977 }
978 
979 template <class Emitter>
980 bool ByteCodeExprGen<Emitter>::VisitMemberExpr(const MemberExpr *E) {
981   // 'Base.Member'
982   const Expr *Base = E->getBase();
983 
984   if (DiscardResult)
985     return this->discard(Base);
986 
987   if (!this->visit(Base))
988     return false;
989 
990   // Base above gives us a pointer on the stack.
991   // TODO: Implement non-FieldDecl members.
992   const ValueDecl *Member = E->getMemberDecl();
993   if (const auto *FD = dyn_cast<FieldDecl>(Member)) {
994     const RecordDecl *RD = FD->getParent();
995     const Record *R = getRecord(RD);
996     const Record::Field *F = R->getField(FD);
997     // Leave a pointer to the field on the stack.
998     if (F->Decl->getType()->isReferenceType())
999       return this->emitGetFieldPop(PT_Ptr, F->Offset, E);
1000     return this->emitGetPtrField(F->Offset, E);
1001   }
1002 
1003   return false;
1004 }
1005 
1006 template <class Emitter>
1007 bool ByteCodeExprGen<Emitter>::VisitArrayInitIndexExpr(
1008     const ArrayInitIndexExpr *E) {
1009   // ArrayIndex might not be set if a ArrayInitIndexExpr is being evaluated
1010   // stand-alone, e.g. via EvaluateAsInt().
1011   if (!ArrayIndex)
1012     return false;
1013   return this->emitConst(*ArrayIndex, E);
1014 }
1015 
1016 template <class Emitter>
1017 bool ByteCodeExprGen<Emitter>::VisitArrayInitLoopExpr(
1018     const ArrayInitLoopExpr *E) {
1019   assert(Initializing);
1020   assert(!DiscardResult);
1021   // TODO: This compiles to quite a lot of bytecode if the array is larger.
1022   //   Investigate compiling this to a loop.
1023 
1024   const Expr *SubExpr = E->getSubExpr();
1025   const Expr *CommonExpr = E->getCommonExpr();
1026   size_t Size = E->getArraySize().getZExtValue();
1027 
1028   // If the common expression is an opaque expression, we visit it
1029   // here once so we have its value cached.
1030   // FIXME: This might be necessary (or useful) for all expressions.
1031   if (isa<OpaqueValueExpr>(CommonExpr)) {
1032     if (!this->discard(CommonExpr))
1033       return false;
1034   }
1035 
1036   // So, every iteration, we execute an assignment here
1037   // where the LHS is on the stack (the target array)
1038   // and the RHS is our SubExpr.
1039   for (size_t I = 0; I != Size; ++I) {
1040     ArrayIndexScope<Emitter> IndexScope(this, I);
1041     BlockScope<Emitter> BS(this);
1042 
1043     if (!this->visitArrayElemInit(I, SubExpr))
1044       return false;
1045   }
1046   return true;
1047 }
1048 
1049 template <class Emitter>
1050 bool ByteCodeExprGen<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
1051   if (Initializing)
1052     return this->visitInitializer(E->getSourceExpr());
1053 
1054   PrimType SubExprT = classify(E->getSourceExpr()).value_or(PT_Ptr);
1055   if (auto It = OpaqueExprs.find(E); It != OpaqueExprs.end())
1056     return this->emitGetLocal(SubExprT, It->second, E);
1057 
1058   if (!this->visit(E->getSourceExpr()))
1059     return false;
1060 
1061   // At this point we either have the evaluated source expression or a pointer
1062   // to an object on the stack. We want to create a local variable that stores
1063   // this value.
1064   std::optional<unsigned> LocalIndex =
1065       allocateLocalPrimitive(E, SubExprT, /*IsConst=*/true);
1066   if (!LocalIndex)
1067     return false;
1068   if (!this->emitSetLocal(SubExprT, *LocalIndex, E))
1069     return false;
1070 
1071   // Here the local variable is created but the value is removed from the stack,
1072   // so we put it back, because the caller might need it.
1073   if (!DiscardResult) {
1074     if (!this->emitGetLocal(SubExprT, *LocalIndex, E))
1075       return false;
1076   }
1077 
1078   // FIXME: Ideally the cached value should be cleaned up later.
1079   OpaqueExprs.insert({E, *LocalIndex});
1080 
1081   return true;
1082 }
1083 
1084 template <class Emitter>
1085 bool ByteCodeExprGen<Emitter>::VisitAbstractConditionalOperator(
1086     const AbstractConditionalOperator *E) {
1087   const Expr *Condition = E->getCond();
1088   const Expr *TrueExpr = E->getTrueExpr();
1089   const Expr *FalseExpr = E->getFalseExpr();
1090 
1091   LabelTy LabelEnd = this->getLabel();   // Label after the operator.
1092   LabelTy LabelFalse = this->getLabel(); // Label for the false expr.
1093 
1094   if (!this->visitBool(Condition))
1095     return false;
1096 
1097   if (!this->jumpFalse(LabelFalse))
1098     return false;
1099 
1100   if (!this->delegate(TrueExpr))
1101     return false;
1102   if (!this->jump(LabelEnd))
1103     return false;
1104 
1105   this->emitLabel(LabelFalse);
1106 
1107   if (!this->delegate(FalseExpr))
1108     return false;
1109 
1110   this->fallthrough(LabelEnd);
1111   this->emitLabel(LabelEnd);
1112 
1113   return true;
1114 }
1115 
1116 template <class Emitter>
1117 bool ByteCodeExprGen<Emitter>::VisitStringLiteral(const StringLiteral *E) {
1118   if (DiscardResult)
1119     return true;
1120 
1121   if (!Initializing) {
1122     unsigned StringIndex = P.createGlobalString(E);
1123     return this->emitGetPtrGlobal(StringIndex, E);
1124   }
1125 
1126   // We are initializing an array on the stack.
1127   const ConstantArrayType *CAT =
1128       Ctx.getASTContext().getAsConstantArrayType(E->getType());
1129   assert(CAT && "a string literal that's not a constant array?");
1130 
1131   // If the initializer string is too long, a diagnostic has already been
1132   // emitted. Read only the array length from the string literal.
1133   unsigned ArraySize = CAT->getSize().getZExtValue();
1134   unsigned N = std::min(ArraySize, E->getLength());
1135   size_t CharWidth = E->getCharByteWidth();
1136 
1137   for (unsigned I = 0; I != N; ++I) {
1138     uint32_t CodeUnit = E->getCodeUnit(I);
1139 
1140     if (CharWidth == 1) {
1141       this->emitConstSint8(CodeUnit, E);
1142       this->emitInitElemSint8(I, E);
1143     } else if (CharWidth == 2) {
1144       this->emitConstUint16(CodeUnit, E);
1145       this->emitInitElemUint16(I, E);
1146     } else if (CharWidth == 4) {
1147       this->emitConstUint32(CodeUnit, E);
1148       this->emitInitElemUint32(I, E);
1149     } else {
1150       llvm_unreachable("unsupported character width");
1151     }
1152   }
1153 
1154   // Fill up the rest of the char array with NUL bytes.
1155   for (unsigned I = N; I != ArraySize; ++I) {
1156     if (CharWidth == 1) {
1157       this->emitConstSint8(0, E);
1158       this->emitInitElemSint8(I, E);
1159     } else if (CharWidth == 2) {
1160       this->emitConstUint16(0, E);
1161       this->emitInitElemUint16(I, E);
1162     } else if (CharWidth == 4) {
1163       this->emitConstUint32(0, E);
1164       this->emitInitElemUint32(I, E);
1165     } else {
1166       llvm_unreachable("unsupported character width");
1167     }
1168   }
1169 
1170   return true;
1171 }
1172 
1173 template <class Emitter>
1174 bool ByteCodeExprGen<Emitter>::VisitCharacterLiteral(
1175     const CharacterLiteral *E) {
1176   if (DiscardResult)
1177     return true;
1178   return this->emitConst(E->getValue(), E);
1179 }
1180 
1181 template <class Emitter>
1182 bool ByteCodeExprGen<Emitter>::VisitFloatCompoundAssignOperator(
1183     const CompoundAssignOperator *E) {
1184 
1185   const Expr *LHS = E->getLHS();
1186   const Expr *RHS = E->getRHS();
1187   QualType LHSType = LHS->getType();
1188   QualType LHSComputationType = E->getComputationLHSType();
1189   QualType ResultType = E->getComputationResultType();
1190   std::optional<PrimType> LT = classify(LHSComputationType);
1191   std::optional<PrimType> RT = classify(ResultType);
1192 
1193   assert(ResultType->isFloatingType());
1194 
1195   if (!LT || !RT)
1196     return false;
1197 
1198   PrimType LHST = classifyPrim(LHSType);
1199 
1200   // C++17 onwards require that we evaluate the RHS first.
1201   // Compute RHS and save it in a temporary variable so we can
1202   // load it again later.
1203   if (!visit(RHS))
1204     return false;
1205 
1206   unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
1207   if (!this->emitSetLocal(*RT, TempOffset, E))
1208     return false;
1209 
1210   // First, visit LHS.
1211   if (!visit(LHS))
1212     return false;
1213   if (!this->emitLoad(LHST, E))
1214     return false;
1215 
1216   // If necessary, convert LHS to its computation type.
1217   if (!this->emitPrimCast(LHST, classifyPrim(LHSComputationType),
1218                           LHSComputationType, E))
1219     return false;
1220 
1221   // Now load RHS.
1222   if (!this->emitGetLocal(*RT, TempOffset, E))
1223     return false;
1224 
1225   llvm::RoundingMode RM = getRoundingMode(E);
1226   switch (E->getOpcode()) {
1227   case BO_AddAssign:
1228     if (!this->emitAddf(RM, E))
1229       return false;
1230     break;
1231   case BO_SubAssign:
1232     if (!this->emitSubf(RM, E))
1233       return false;
1234     break;
1235   case BO_MulAssign:
1236     if (!this->emitMulf(RM, E))
1237       return false;
1238     break;
1239   case BO_DivAssign:
1240     if (!this->emitDivf(RM, E))
1241       return false;
1242     break;
1243   default:
1244     return false;
1245   }
1246 
1247   if (!this->emitPrimCast(classifyPrim(ResultType), LHST, LHS->getType(), E))
1248     return false;
1249 
1250   if (DiscardResult)
1251     return this->emitStorePop(LHST, E);
1252   return this->emitStore(LHST, E);
1253 }
1254 
1255 template <class Emitter>
1256 bool ByteCodeExprGen<Emitter>::VisitPointerCompoundAssignOperator(
1257     const CompoundAssignOperator *E) {
1258   BinaryOperatorKind Op = E->getOpcode();
1259   const Expr *LHS = E->getLHS();
1260   const Expr *RHS = E->getRHS();
1261   std::optional<PrimType> LT = classify(LHS->getType());
1262   std::optional<PrimType> RT = classify(RHS->getType());
1263 
1264   if (Op != BO_AddAssign && Op != BO_SubAssign)
1265     return false;
1266 
1267   if (!LT || !RT)
1268     return false;
1269   assert(*LT == PT_Ptr);
1270 
1271   if (!visit(LHS))
1272     return false;
1273 
1274   if (!this->emitLoadPtr(LHS))
1275     return false;
1276 
1277   if (!visit(RHS))
1278     return false;
1279 
1280   if (Op == BO_AddAssign)
1281     this->emitAddOffset(*RT, E);
1282   else
1283     this->emitSubOffset(*RT, E);
1284 
1285   if (DiscardResult)
1286     return this->emitStorePopPtr(E);
1287   return this->emitStorePtr(E);
1288 }
1289 
1290 template <class Emitter>
1291 bool ByteCodeExprGen<Emitter>::VisitCompoundAssignOperator(
1292     const CompoundAssignOperator *E) {
1293 
1294   const Expr *LHS = E->getLHS();
1295   const Expr *RHS = E->getRHS();
1296   std::optional<PrimType> LHSComputationT =
1297       classify(E->getComputationLHSType());
1298   std::optional<PrimType> LT = classify(LHS->getType());
1299   std::optional<PrimType> RT = classify(E->getComputationResultType());
1300   std::optional<PrimType> ResultT = classify(E->getType());
1301 
1302   if (!LT || !RT || !ResultT || !LHSComputationT)
1303     return false;
1304 
1305   // Handle floating point operations separately here, since they
1306   // require special care.
1307 
1308   if (ResultT == PT_Float || RT == PT_Float)
1309     return VisitFloatCompoundAssignOperator(E);
1310 
1311   if (E->getType()->isPointerType())
1312     return VisitPointerCompoundAssignOperator(E);
1313 
1314   assert(!E->getType()->isPointerType() && "Handled above");
1315   assert(!E->getType()->isFloatingType() && "Handled above");
1316 
1317   // C++17 onwards require that we evaluate the RHS first.
1318   // Compute RHS and save it in a temporary variable so we can
1319   // load it again later.
1320   // FIXME: Compound assignments are unsequenced in C, so we might
1321   //   have to figure out how to reject them.
1322   if (!visit(RHS))
1323     return false;
1324 
1325   unsigned TempOffset = this->allocateLocalPrimitive(E, *RT, /*IsConst=*/true);
1326 
1327   if (!this->emitSetLocal(*RT, TempOffset, E))
1328     return false;
1329 
1330   // Get LHS pointer, load its value and cast it to the
1331   // computation type if necessary.
1332   if (!visit(LHS))
1333     return false;
1334   if (!this->emitLoad(*LT, E))
1335     return false;
1336   if (*LT != *LHSComputationT) {
1337     if (!this->emitCast(*LT, *LHSComputationT, E))
1338       return false;
1339   }
1340 
1341   // Get the RHS value on the stack.
1342   if (!this->emitGetLocal(*RT, TempOffset, E))
1343     return false;
1344 
1345   // Perform operation.
1346   switch (E->getOpcode()) {
1347   case BO_AddAssign:
1348     if (!this->emitAdd(*LHSComputationT, E))
1349       return false;
1350     break;
1351   case BO_SubAssign:
1352     if (!this->emitSub(*LHSComputationT, E))
1353       return false;
1354     break;
1355   case BO_MulAssign:
1356     if (!this->emitMul(*LHSComputationT, E))
1357       return false;
1358     break;
1359   case BO_DivAssign:
1360     if (!this->emitDiv(*LHSComputationT, E))
1361       return false;
1362     break;
1363   case BO_RemAssign:
1364     if (!this->emitRem(*LHSComputationT, E))
1365       return false;
1366     break;
1367   case BO_ShlAssign:
1368     if (!this->emitShl(*LHSComputationT, *RT, E))
1369       return false;
1370     break;
1371   case BO_ShrAssign:
1372     if (!this->emitShr(*LHSComputationT, *RT, E))
1373       return false;
1374     break;
1375   case BO_AndAssign:
1376     if (!this->emitBitAnd(*LHSComputationT, E))
1377       return false;
1378     break;
1379   case BO_XorAssign:
1380     if (!this->emitBitXor(*LHSComputationT, E))
1381       return false;
1382     break;
1383   case BO_OrAssign:
1384     if (!this->emitBitOr(*LHSComputationT, E))
1385       return false;
1386     break;
1387   default:
1388     llvm_unreachable("Unimplemented compound assign operator");
1389   }
1390 
1391   // And now cast from LHSComputationT to ResultT.
1392   if (*ResultT != *LHSComputationT) {
1393     if (!this->emitCast(*LHSComputationT, *ResultT, E))
1394       return false;
1395   }
1396 
1397   // And store the result in LHS.
1398   if (DiscardResult) {
1399     if (LHS->refersToBitField())
1400       return this->emitStoreBitFieldPop(*ResultT, E);
1401     return this->emitStorePop(*ResultT, E);
1402   }
1403   if (LHS->refersToBitField())
1404     return this->emitStoreBitField(*ResultT, E);
1405   return this->emitStore(*ResultT, E);
1406 }
1407 
1408 template <class Emitter>
1409 bool ByteCodeExprGen<Emitter>::VisitExprWithCleanups(
1410     const ExprWithCleanups *E) {
1411   const Expr *SubExpr = E->getSubExpr();
1412 
1413   assert(E->getNumObjects() == 0 && "TODO: Implement cleanups");
1414 
1415   return this->delegate(SubExpr);
1416 }
1417 
1418 template <class Emitter>
1419 bool ByteCodeExprGen<Emitter>::VisitMaterializeTemporaryExpr(
1420     const MaterializeTemporaryExpr *E) {
1421   const Expr *SubExpr = E->getSubExpr();
1422 
1423   if (Initializing) {
1424     // We already have a value, just initialize that.
1425     return this->visitInitializer(SubExpr);
1426   }
1427   // If we don't end up using the materialized temporary anyway, don't
1428   // bother creating it.
1429   if (DiscardResult)
1430     return this->discard(SubExpr);
1431 
1432   // When we're initializing a global variable *or* the storage duration of
1433   // the temporary is explicitly static, create a global variable.
1434   std::optional<PrimType> SubExprT = classify(SubExpr);
1435   bool IsStatic = E->getStorageDuration() == SD_Static;
1436   if (GlobalDecl || IsStatic) {
1437     std::optional<unsigned> GlobalIndex = P.createGlobal(E);
1438     if (!GlobalIndex)
1439       return false;
1440 
1441     const LifetimeExtendedTemporaryDecl *TempDecl =
1442         E->getLifetimeExtendedTemporaryDecl();
1443     if (IsStatic)
1444       assert(TempDecl);
1445 
1446     if (SubExprT) {
1447       if (!this->visit(SubExpr))
1448         return false;
1449       if (IsStatic) {
1450         if (!this->emitInitGlobalTemp(*SubExprT, *GlobalIndex, TempDecl, E))
1451           return false;
1452       } else {
1453         if (!this->emitInitGlobal(*SubExprT, *GlobalIndex, E))
1454           return false;
1455       }
1456       return this->emitGetPtrGlobal(*GlobalIndex, E);
1457     }
1458 
1459     // Non-primitive values.
1460     if (!this->emitGetPtrGlobal(*GlobalIndex, E))
1461       return false;
1462     if (!this->visitInitializer(SubExpr))
1463       return false;
1464     if (IsStatic)
1465       return this->emitInitGlobalTempComp(TempDecl, E);
1466     return true;
1467   }
1468 
1469   // For everyhing else, use local variables.
1470   if (SubExprT) {
1471     if (std::optional<unsigned> LocalIndex = allocateLocalPrimitive(
1472             SubExpr, *SubExprT, /*IsConst=*/true, /*IsExtended=*/true)) {
1473       if (!this->visit(SubExpr))
1474         return false;
1475       this->emitSetLocal(*SubExprT, *LocalIndex, E);
1476       return this->emitGetPtrLocal(*LocalIndex, E);
1477     }
1478   } else {
1479     if (std::optional<unsigned> LocalIndex =
1480             allocateLocal(SubExpr, /*IsExtended=*/true)) {
1481       if (!this->emitGetPtrLocal(*LocalIndex, E))
1482         return false;
1483       return this->visitInitializer(SubExpr);
1484     }
1485   }
1486   return false;
1487 }
1488 
1489 template <class Emitter>
1490 bool ByteCodeExprGen<Emitter>::VisitCXXBindTemporaryExpr(
1491     const CXXBindTemporaryExpr *E) {
1492   return this->delegate(E->getSubExpr());
1493 }
1494 
1495 template <class Emitter>
1496 bool ByteCodeExprGen<Emitter>::VisitCompoundLiteralExpr(
1497     const CompoundLiteralExpr *E) {
1498   const Expr *Init = E->getInitializer();
1499   if (Initializing) {
1500     // We already have a value, just initialize that.
1501     return this->visitInitializer(Init);
1502   }
1503 
1504   std::optional<PrimType> T = classify(E->getType());
1505   if (E->isFileScope()) {
1506     if (std::optional<unsigned> GlobalIndex = P.createGlobal(E)) {
1507       if (classify(E->getType()))
1508         return this->visit(Init);
1509       if (!this->emitGetPtrGlobal(*GlobalIndex, E))
1510         return false;
1511       return this->visitInitializer(Init);
1512     }
1513   }
1514 
1515   // Otherwise, use a local variable.
1516   if (T) {
1517     // For primitive types, we just visit the initializer.
1518     return this->delegate(Init);
1519   } else {
1520     if (std::optional<unsigned> LocalIndex = allocateLocal(Init)) {
1521       if (!this->emitGetPtrLocal(*LocalIndex, E))
1522         return false;
1523       if (!this->visitInitializer(Init))
1524         return false;
1525       if (DiscardResult)
1526         return this->emitPopPtr(E);
1527       return true;
1528     }
1529   }
1530 
1531   return false;
1532 }
1533 
1534 template <class Emitter>
1535 bool ByteCodeExprGen<Emitter>::VisitTypeTraitExpr(const TypeTraitExpr *E) {
1536   if (DiscardResult)
1537     return true;
1538   return this->emitConstBool(E->getValue(), E);
1539 }
1540 
1541 template <class Emitter>
1542 bool ByteCodeExprGen<Emitter>::VisitLambdaExpr(const LambdaExpr *E) {
1543   assert(Initializing);
1544   const Record *R = P.getOrCreateRecord(E->getLambdaClass());
1545 
1546   auto *CaptureInitIt = E->capture_init_begin();
1547   // Initialize all fields (which represent lambda captures) of the
1548   // record with their initializers.
1549   for (const Record::Field &F : R->fields()) {
1550     const Expr *Init = *CaptureInitIt;
1551     ++CaptureInitIt;
1552 
1553     if (std::optional<PrimType> T = classify(Init)) {
1554       if (!this->visit(Init))
1555         return false;
1556 
1557       if (!this->emitSetField(*T, F.Offset, E))
1558         return false;
1559     } else {
1560       if (!this->emitDupPtr(E))
1561         return false;
1562 
1563       if (!this->emitGetPtrField(F.Offset, E))
1564         return false;
1565 
1566       if (!this->visitInitializer(Init))
1567         return false;
1568 
1569       if (!this->emitPopPtr(E))
1570         return false;
1571     }
1572   }
1573 
1574   return true;
1575 }
1576 
1577 template <class Emitter>
1578 bool ByteCodeExprGen<Emitter>::VisitPredefinedExpr(const PredefinedExpr *E) {
1579   if (DiscardResult)
1580     return true;
1581 
1582   assert(!Initializing);
1583   return this->visit(E->getFunctionName());
1584 }
1585 
1586 template <class Emitter>
1587 bool ByteCodeExprGen<Emitter>::VisitCXXThrowExpr(const CXXThrowExpr *E) {
1588   if (E->getSubExpr() && !this->discard(E->getSubExpr()))
1589     return false;
1590 
1591   return this->emitInvalid(E);
1592 }
1593 
1594 template <class Emitter>
1595 bool ByteCodeExprGen<Emitter>::VisitCXXReinterpretCastExpr(
1596     const CXXReinterpretCastExpr *E) {
1597   if (!this->discard(E->getSubExpr()))
1598     return false;
1599 
1600   return this->emitInvalidCast(CastKind::Reinterpret, E);
1601 }
1602 
1603 template <class Emitter>
1604 bool ByteCodeExprGen<Emitter>::VisitCXXNoexceptExpr(const CXXNoexceptExpr *E) {
1605   assert(E->getType()->isBooleanType());
1606 
1607   if (DiscardResult)
1608     return true;
1609   return this->emitConstBool(E->getValue(), E);
1610 }
1611 
1612 template <class Emitter>
1613 bool ByteCodeExprGen<Emitter>::VisitCXXConstructExpr(
1614     const CXXConstructExpr *E) {
1615   QualType T = E->getType();
1616   assert(!classify(T));
1617 
1618   if (T->isRecordType()) {
1619     const CXXConstructorDecl *Ctor = E->getConstructor();
1620 
1621     // Trivial zero initialization.
1622     if (E->requiresZeroInitialization() && Ctor->isTrivial()) {
1623       const Record *R = getRecord(E->getType());
1624       return this->visitZeroRecordInitializer(R, E);
1625     }
1626 
1627     const Function *Func = getFunction(Ctor);
1628 
1629     if (!Func)
1630       return false;
1631 
1632     assert(Func->hasThisPointer());
1633     assert(!Func->hasRVO());
1634 
1635     // If we're discarding a construct expression, we still need
1636     // to allocate a variable and call the constructor and destructor.
1637     if (DiscardResult) {
1638       assert(!Initializing);
1639       std::optional<unsigned> LocalIndex =
1640           allocateLocal(E, /*IsExtended=*/true);
1641 
1642       if (!LocalIndex)
1643         return false;
1644 
1645       if (!this->emitGetPtrLocal(*LocalIndex, E))
1646         return false;
1647     }
1648 
1649     //  The This pointer is already on the stack because this is an initializer,
1650     //  but we need to dup() so the call() below has its own copy.
1651     if (!this->emitDupPtr(E))
1652       return false;
1653 
1654     // Constructor arguments.
1655     for (const auto *Arg : E->arguments()) {
1656       if (!this->visit(Arg))
1657         return false;
1658     }
1659 
1660     if (!this->emitCall(Func, E))
1661       return false;
1662 
1663     // Immediately call the destructor if we have to.
1664     if (DiscardResult) {
1665       if (!this->emitPopPtr(E))
1666         return false;
1667     }
1668     return true;
1669   }
1670 
1671   if (T->isArrayType()) {
1672     const ConstantArrayType *CAT =
1673         Ctx.getASTContext().getAsConstantArrayType(E->getType());
1674     assert(CAT);
1675     size_t NumElems = CAT->getSize().getZExtValue();
1676     const Function *Func = getFunction(E->getConstructor());
1677     if (!Func || !Func->isConstexpr())
1678       return false;
1679 
1680     // FIXME(perf): We're calling the constructor once per array element here,
1681     //   in the old intepreter we had a special-case for trivial constructors.
1682     for (size_t I = 0; I != NumElems; ++I) {
1683       if (!this->emitConstUint64(I, E))
1684         return false;
1685       if (!this->emitArrayElemPtrUint64(E))
1686         return false;
1687 
1688       // Constructor arguments.
1689       for (const auto *Arg : E->arguments()) {
1690         if (!this->visit(Arg))
1691           return false;
1692       }
1693 
1694       if (!this->emitCall(Func, E))
1695         return false;
1696     }
1697     return true;
1698   }
1699 
1700   return false;
1701 }
1702 
1703 template <class Emitter>
1704 bool ByteCodeExprGen<Emitter>::VisitSourceLocExpr(const SourceLocExpr *E) {
1705   if (DiscardResult)
1706     return true;
1707 
1708   const APValue Val =
1709       E->EvaluateInContext(Ctx.getASTContext(), SourceLocDefaultExpr);
1710 
1711   // Things like __builtin_LINE().
1712   if (E->getType()->isIntegerType()) {
1713     assert(Val.isInt());
1714     const APSInt &I = Val.getInt();
1715     return this->emitConst(I, E);
1716   }
1717   // Otherwise, the APValue is an LValue, with only one element.
1718   // Theoretically, we don't need the APValue at all of course.
1719   assert(E->getType()->isPointerType());
1720   assert(Val.isLValue());
1721   const APValue::LValueBase &Base = Val.getLValueBase();
1722   if (const Expr *LValueExpr = Base.dyn_cast<const Expr *>())
1723     return this->visit(LValueExpr);
1724 
1725   // Otherwise, we have a decl (which is the case for
1726   // __builtin_source_location).
1727   assert(Base.is<const ValueDecl *>());
1728   assert(Val.getLValuePath().size() == 0);
1729   const auto *BaseDecl = Base.dyn_cast<const ValueDecl *>();
1730   assert(BaseDecl);
1731 
1732   auto *UGCD = cast<UnnamedGlobalConstantDecl>(BaseDecl);
1733 
1734   std::optional<unsigned> GlobalIndex = P.getOrCreateGlobal(UGCD);
1735   if (!GlobalIndex)
1736     return false;
1737 
1738   if (!this->emitGetPtrGlobal(*GlobalIndex, E))
1739     return false;
1740 
1741   const Record *R = getRecord(E->getType());
1742   const APValue &V = UGCD->getValue();
1743   for (unsigned I = 0, N = R->getNumFields(); I != N; ++I) {
1744     const Record::Field *F = R->getField(I);
1745     const APValue &FieldValue = V.getStructField(I);
1746 
1747     PrimType FieldT = classifyPrim(F->Decl->getType());
1748 
1749     if (!this->visitAPValue(FieldValue, FieldT, E))
1750       return false;
1751     if (!this->emitInitField(FieldT, F->Offset, E))
1752       return false;
1753   }
1754 
1755   // Leave the pointer to the global on the stack.
1756   return true;
1757 }
1758 
1759 template <class Emitter>
1760 bool ByteCodeExprGen<Emitter>::VisitOffsetOfExpr(const OffsetOfExpr *E) {
1761   unsigned N = E->getNumComponents();
1762   if (N == 0)
1763     return false;
1764 
1765   for (unsigned I = 0; I != N; ++I) {
1766     const OffsetOfNode &Node = E->getComponent(I);
1767     if (Node.getKind() == OffsetOfNode::Array) {
1768       const Expr *ArrayIndexExpr = E->getIndexExpr(Node.getArrayExprIndex());
1769       PrimType IndexT = classifyPrim(ArrayIndexExpr->getType());
1770 
1771       if (DiscardResult) {
1772         if (!this->discard(ArrayIndexExpr))
1773           return false;
1774         continue;
1775       }
1776 
1777       if (!this->visit(ArrayIndexExpr))
1778         return false;
1779       // Cast to Sint64.
1780       if (IndexT != PT_Sint64) {
1781         if (!this->emitCast(IndexT, PT_Sint64, E))
1782           return false;
1783       }
1784     }
1785   }
1786 
1787   if (DiscardResult)
1788     return true;
1789 
1790   PrimType T = classifyPrim(E->getType());
1791   return this->emitOffsetOf(T, E, E);
1792 }
1793 
1794 template <class Emitter>
1795 bool ByteCodeExprGen<Emitter>::VisitCXXScalarValueInitExpr(
1796     const CXXScalarValueInitExpr *E) {
1797   QualType Ty = E->getType();
1798 
1799   if (Ty->isVoidType())
1800     return true;
1801 
1802   return this->visitZeroInitializer(classifyPrim(Ty), Ty, E);
1803 }
1804 
1805 template <class Emitter>
1806 bool ByteCodeExprGen<Emitter>::VisitSizeOfPackExpr(const SizeOfPackExpr *E) {
1807   return this->emitConst(E->getPackLength(), E);
1808 }
1809 
1810 template <class Emitter> bool ByteCodeExprGen<Emitter>::discard(const Expr *E) {
1811   if (E->containsErrors())
1812     return false;
1813 
1814   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/true,
1815                              /*NewInitializing=*/false);
1816   return this->Visit(E);
1817 }
1818 
1819 template <class Emitter>
1820 bool ByteCodeExprGen<Emitter>::delegate(const Expr *E) {
1821   if (E->containsErrors())
1822     return false;
1823 
1824   // We're basically doing:
1825   // OptionScope<Emitter> Scope(this, DicardResult, Initializing);
1826   // but that's unnecessary of course.
1827   return this->Visit(E);
1828 }
1829 
1830 template <class Emitter> bool ByteCodeExprGen<Emitter>::visit(const Expr *E) {
1831   if (E->containsErrors())
1832     return false;
1833 
1834   if (E->getType()->isVoidType())
1835     return this->discard(E);
1836 
1837   // Create local variable to hold the return value.
1838   if (!E->isGLValue() && !E->getType()->isAnyComplexType() &&
1839       !classify(E->getType())) {
1840     std::optional<unsigned> LocalIndex = allocateLocal(E, /*IsExtended=*/true);
1841     if (!LocalIndex)
1842       return false;
1843 
1844     if (!this->emitGetPtrLocal(*LocalIndex, E))
1845       return false;
1846     return this->visitInitializer(E);
1847   }
1848 
1849   //  Otherwise,we have a primitive return value, produce the value directly
1850   //  and push it on the stack.
1851   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
1852                              /*NewInitializing=*/false);
1853   return this->Visit(E);
1854 }
1855 
1856 template <class Emitter>
1857 bool ByteCodeExprGen<Emitter>::visitInitializer(const Expr *E) {
1858   assert(!classify(E->getType()));
1859 
1860   if (E->containsErrors())
1861     return false;
1862 
1863   OptionScope<Emitter> Scope(this, /*NewDiscardResult=*/false,
1864                              /*NewInitializing=*/true);
1865   return this->Visit(E);
1866 }
1867 
1868 template <class Emitter>
1869 bool ByteCodeExprGen<Emitter>::visitBool(const Expr *E) {
1870   std::optional<PrimType> T = classify(E->getType());
1871   if (!T)
1872     return false;
1873 
1874   if (!this->visit(E))
1875     return false;
1876 
1877   if (T == PT_Bool)
1878     return true;
1879 
1880   // Convert pointers to bool.
1881   if (T == PT_Ptr || T == PT_FnPtr) {
1882     if (!this->emitNull(*T, E))
1883       return false;
1884     return this->emitNE(*T, E);
1885   }
1886 
1887   // Or Floats.
1888   if (T == PT_Float)
1889     return this->emitCastFloatingIntegralBool(E);
1890 
1891   // Or anything else we can.
1892   return this->emitCast(*T, PT_Bool, E);
1893 }
1894 
1895 template <class Emitter>
1896 bool ByteCodeExprGen<Emitter>::visitZeroInitializer(PrimType T, QualType QT,
1897                                                     const Expr *E) {
1898   switch (T) {
1899   case PT_Bool:
1900     return this->emitZeroBool(E);
1901   case PT_Sint8:
1902     return this->emitZeroSint8(E);
1903   case PT_Uint8:
1904     return this->emitZeroUint8(E);
1905   case PT_Sint16:
1906     return this->emitZeroSint16(E);
1907   case PT_Uint16:
1908     return this->emitZeroUint16(E);
1909   case PT_Sint32:
1910     return this->emitZeroSint32(E);
1911   case PT_Uint32:
1912     return this->emitZeroUint32(E);
1913   case PT_Sint64:
1914     return this->emitZeroSint64(E);
1915   case PT_Uint64:
1916     return this->emitZeroUint64(E);
1917   case PT_IntAP:
1918     return this->emitZeroIntAP(Ctx.getBitWidth(QT), E);
1919   case PT_IntAPS:
1920     return this->emitZeroIntAPS(Ctx.getBitWidth(QT), E);
1921   case PT_Ptr:
1922     return this->emitNullPtr(E);
1923   case PT_FnPtr:
1924     return this->emitNullFnPtr(E);
1925   case PT_Float: {
1926     return this->emitConstFloat(APFloat::getZero(Ctx.getFloatSemantics(QT)), E);
1927   }
1928   }
1929   llvm_unreachable("unknown primitive type");
1930 }
1931 
1932 template <class Emitter>
1933 bool ByteCodeExprGen<Emitter>::visitZeroRecordInitializer(const Record *R,
1934                                                           const Expr *E) {
1935   assert(E);
1936   assert(R);
1937   // Fields
1938   for (const Record::Field &Field : R->fields()) {
1939     const Descriptor *D = Field.Desc;
1940     if (D->isPrimitive()) {
1941       QualType QT = D->getType();
1942       PrimType T = classifyPrim(D->getType());
1943       if (!this->visitZeroInitializer(T, QT, E))
1944         return false;
1945       if (!this->emitInitField(T, Field.Offset, E))
1946         return false;
1947       continue;
1948     }
1949 
1950     // TODO: Add GetPtrFieldPop and get rid of this dup.
1951     if (!this->emitDupPtr(E))
1952       return false;
1953     if (!this->emitGetPtrField(Field.Offset, E))
1954       return false;
1955 
1956     if (D->isPrimitiveArray()) {
1957       QualType ET = D->getElemQualType();
1958       PrimType T = classifyPrim(ET);
1959       for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
1960         if (!this->visitZeroInitializer(T, ET, E))
1961           return false;
1962         if (!this->emitInitElem(T, I, E))
1963           return false;
1964       }
1965     } else if (D->isCompositeArray()) {
1966       const Record *ElemRecord = D->ElemDesc->ElemRecord;
1967       assert(D->ElemDesc->ElemRecord);
1968       for (uint32_t I = 0, N = D->getNumElems(); I != N; ++I) {
1969         if (!this->emitConstUint32(I, E))
1970           return false;
1971         if (!this->emitArrayElemPtr(PT_Uint32, E))
1972           return false;
1973         if (!this->visitZeroRecordInitializer(ElemRecord, E))
1974           return false;
1975         if (!this->emitPopPtr(E))
1976           return false;
1977       }
1978     } else if (D->isRecord()) {
1979       if (!this->visitZeroRecordInitializer(D->ElemRecord, E))
1980         return false;
1981     } else {
1982       assert(false);
1983     }
1984 
1985     if (!this->emitPopPtr(E))
1986       return false;
1987   }
1988 
1989   for (const Record::Base &B : R->bases()) {
1990     if (!this->emitGetPtrBase(B.Offset, E))
1991       return false;
1992     if (!this->visitZeroRecordInitializer(B.R, E))
1993       return false;
1994     if (!this->emitInitPtrPop(E))
1995       return false;
1996   }
1997 
1998   // FIXME: Virtual bases.
1999 
2000   return true;
2001 }
2002 
2003 template <class Emitter>
2004 bool ByteCodeExprGen<Emitter>::dereference(
2005     const Expr *LV, DerefKind AK, llvm::function_ref<bool(PrimType)> Direct,
2006     llvm::function_ref<bool(PrimType)> Indirect) {
2007   if (std::optional<PrimType> T = classify(LV->getType())) {
2008     if (!LV->refersToBitField()) {
2009       // Only primitive, non bit-field types can be dereferenced directly.
2010       if (const auto *DE = dyn_cast<DeclRefExpr>(LV)) {
2011         if (!DE->getDecl()->getType()->isReferenceType()) {
2012           if (const auto *PD = dyn_cast<ParmVarDecl>(DE->getDecl()))
2013             return dereferenceParam(LV, *T, PD, AK, Direct, Indirect);
2014           if (const auto *VD = dyn_cast<VarDecl>(DE->getDecl()))
2015             return dereferenceVar(LV, *T, VD, AK, Direct, Indirect);
2016         }
2017       }
2018     }
2019 
2020     if (!visit(LV))
2021       return false;
2022     return Indirect(*T);
2023   }
2024 
2025   if (LV->getType()->isAnyComplexType())
2026     return visit(LV);
2027 
2028   return false;
2029 }
2030 
2031 template <class Emitter>
2032 bool ByteCodeExprGen<Emitter>::dereferenceParam(
2033     const Expr *LV, PrimType T, const ParmVarDecl *PD, DerefKind AK,
2034     llvm::function_ref<bool(PrimType)> Direct,
2035     llvm::function_ref<bool(PrimType)> Indirect) {
2036   auto It = this->Params.find(PD);
2037   if (It != this->Params.end()) {
2038     unsigned Idx = It->second.Offset;
2039     switch (AK) {
2040     case DerefKind::Read:
2041       return DiscardResult ? true : this->emitGetParam(T, Idx, LV);
2042 
2043     case DerefKind::Write:
2044       if (!Direct(T))
2045         return false;
2046       if (!this->emitSetParam(T, Idx, LV))
2047         return false;
2048       return DiscardResult ? true : this->emitGetPtrParam(Idx, LV);
2049 
2050     case DerefKind::ReadWrite:
2051       if (!this->emitGetParam(T, Idx, LV))
2052         return false;
2053       if (!Direct(T))
2054         return false;
2055       if (!this->emitSetParam(T, Idx, LV))
2056         return false;
2057       return DiscardResult ? true : this->emitGetPtrParam(Idx, LV);
2058     }
2059     return true;
2060   }
2061 
2062   // If the param is a pointer, we can dereference a dummy value.
2063   if (!DiscardResult && T == PT_Ptr && AK == DerefKind::Read) {
2064     if (auto Idx = P.getOrCreateDummy(PD))
2065       return this->emitGetPtrGlobal(*Idx, PD);
2066     return false;
2067   }
2068 
2069   // Value cannot be produced - try to emit pointer and do stuff with it.
2070   return visit(LV) && Indirect(T);
2071 }
2072 
2073 template <class Emitter>
2074 bool ByteCodeExprGen<Emitter>::dereferenceVar(
2075     const Expr *LV, PrimType T, const VarDecl *VD, DerefKind AK,
2076     llvm::function_ref<bool(PrimType)> Direct,
2077     llvm::function_ref<bool(PrimType)> Indirect) {
2078   auto It = Locals.find(VD);
2079   if (It != Locals.end()) {
2080     const auto &L = It->second;
2081     switch (AK) {
2082     case DerefKind::Read:
2083       if (!this->emitGetLocal(T, L.Offset, LV))
2084         return false;
2085       return DiscardResult ? this->emitPop(T, LV) : true;
2086 
2087     case DerefKind::Write:
2088       if (!Direct(T))
2089         return false;
2090       if (!this->emitSetLocal(T, L.Offset, LV))
2091         return false;
2092       return DiscardResult ? true : this->emitGetPtrLocal(L.Offset, LV);
2093 
2094     case DerefKind::ReadWrite:
2095       if (!this->emitGetLocal(T, L.Offset, LV))
2096         return false;
2097       if (!Direct(T))
2098         return false;
2099       if (!this->emitSetLocal(T, L.Offset, LV))
2100         return false;
2101       return DiscardResult ? true : this->emitGetPtrLocal(L.Offset, LV);
2102     }
2103   } else if (auto Idx = P.getGlobal(VD)) {
2104     switch (AK) {
2105     case DerefKind::Read:
2106       if (!this->emitGetGlobal(T, *Idx, LV))
2107         return false;
2108       return DiscardResult ? this->emitPop(T, LV) : true;
2109 
2110     case DerefKind::Write:
2111       if (!Direct(T))
2112         return false;
2113       if (!this->emitSetGlobal(T, *Idx, LV))
2114         return false;
2115       return DiscardResult ? true : this->emitGetPtrGlobal(*Idx, LV);
2116 
2117     case DerefKind::ReadWrite:
2118       if (!this->emitGetGlobal(T, *Idx, LV))
2119         return false;
2120       if (!Direct(T))
2121         return false;
2122       if (!this->emitSetGlobal(T, *Idx, LV))
2123         return false;
2124       return DiscardResult ? true : this->emitGetPtrGlobal(*Idx, LV);
2125     }
2126   }
2127 
2128   // If the declaration is a constant value, emit it here even
2129   // though the declaration was not evaluated in the current scope.
2130   // The access mode can only be read in this case.
2131   if (!DiscardResult && AK == DerefKind::Read) {
2132     if (VD->hasLocalStorage() && VD->hasInit() && !VD->isConstexpr()) {
2133       QualType VT = VD->getType();
2134       if (VT.isConstQualified() && VT->isFundamentalType())
2135         return this->visit(VD->getInit());
2136     }
2137   }
2138 
2139   // Value cannot be produced - try to emit pointer.
2140   return visit(LV) && Indirect(T);
2141 }
2142 
2143 template <class Emitter>
2144 template <typename T>
2145 bool ByteCodeExprGen<Emitter>::emitConst(T Value, PrimType Ty, const Expr *E) {
2146   switch (Ty) {
2147   case PT_Sint8:
2148     return this->emitConstSint8(Value, E);
2149   case PT_Uint8:
2150     return this->emitConstUint8(Value, E);
2151   case PT_Sint16:
2152     return this->emitConstSint16(Value, E);
2153   case PT_Uint16:
2154     return this->emitConstUint16(Value, E);
2155   case PT_Sint32:
2156     return this->emitConstSint32(Value, E);
2157   case PT_Uint32:
2158     return this->emitConstUint32(Value, E);
2159   case PT_Sint64:
2160     return this->emitConstSint64(Value, E);
2161   case PT_Uint64:
2162     return this->emitConstUint64(Value, E);
2163   case PT_IntAP:
2164   case PT_IntAPS:
2165     assert(false);
2166     return false;
2167   case PT_Bool:
2168     return this->emitConstBool(Value, E);
2169   case PT_Ptr:
2170   case PT_FnPtr:
2171   case PT_Float:
2172     llvm_unreachable("Invalid integral type");
2173     break;
2174   }
2175   llvm_unreachable("unknown primitive type");
2176 }
2177 
2178 template <class Emitter>
2179 template <typename T>
2180 bool ByteCodeExprGen<Emitter>::emitConst(T Value, const Expr *E) {
2181   return this->emitConst(Value, classifyPrim(E->getType()), E);
2182 }
2183 
2184 template <class Emitter>
2185 bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, PrimType Ty,
2186                                          const Expr *E) {
2187   if (Value.isSigned())
2188     return this->emitConst(Value.getSExtValue(), Ty, E);
2189   return this->emitConst(Value.getZExtValue(), Ty, E);
2190 }
2191 
2192 template <class Emitter>
2193 bool ByteCodeExprGen<Emitter>::emitConst(const APSInt &Value, const Expr *E) {
2194   return this->emitConst(Value, classifyPrim(E->getType()), E);
2195 }
2196 
2197 template <class Emitter>
2198 unsigned ByteCodeExprGen<Emitter>::allocateLocalPrimitive(DeclTy &&Src,
2199                                                           PrimType Ty,
2200                                                           bool IsConst,
2201                                                           bool IsExtended) {
2202   // Make sure we don't accidentally register the same decl twice.
2203   if (const auto *VD =
2204           dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
2205     assert(!P.getGlobal(VD));
2206     assert(!Locals.contains(VD));
2207   }
2208 
2209   // FIXME: There are cases where Src.is<Expr*>() is wrong, e.g.
2210   //   (int){12} in C. Consider using Expr::isTemporaryObject() instead
2211   //   or isa<MaterializeTemporaryExpr>().
2212   Descriptor *D = P.createDescriptor(Src, Ty, Descriptor::InlineDescMD, IsConst,
2213                                      Src.is<const Expr *>());
2214   Scope::Local Local = this->createLocal(D);
2215   if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>()))
2216     Locals.insert({VD, Local});
2217   VarScope->add(Local, IsExtended);
2218   return Local.Offset;
2219 }
2220 
2221 template <class Emitter>
2222 std::optional<unsigned>
2223 ByteCodeExprGen<Emitter>::allocateLocal(DeclTy &&Src, bool IsExtended) {
2224   // Make sure we don't accidentally register the same decl twice.
2225   if ([[maybe_unused]]  const auto *VD =
2226           dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
2227     assert(!P.getGlobal(VD));
2228     assert(!Locals.contains(VD));
2229   }
2230 
2231   QualType Ty;
2232   const ValueDecl *Key = nullptr;
2233   const Expr *Init = nullptr;
2234   bool IsTemporary = false;
2235   if (auto *VD = dyn_cast_if_present<ValueDecl>(Src.dyn_cast<const Decl *>())) {
2236     Key = VD;
2237     Ty = VD->getType();
2238 
2239     if (const auto *VarD = dyn_cast<VarDecl>(VD))
2240       Init = VarD->getInit();
2241   }
2242   if (auto *E = Src.dyn_cast<const Expr *>()) {
2243     IsTemporary = true;
2244     Ty = E->getType();
2245   }
2246 
2247   Descriptor *D = P.createDescriptor(
2248       Src, Ty.getTypePtr(), Descriptor::InlineDescMD, Ty.isConstQualified(),
2249       IsTemporary, /*IsMutable=*/false, Init);
2250   if (!D)
2251     return {};
2252 
2253   Scope::Local Local = this->createLocal(D);
2254   if (Key)
2255     Locals.insert({Key, Local});
2256   VarScope->add(Local, IsExtended);
2257   return Local.Offset;
2258 }
2259 
2260 template <class Emitter>
2261 const RecordType *ByteCodeExprGen<Emitter>::getRecordTy(QualType Ty) {
2262   if (const PointerType *PT = dyn_cast<PointerType>(Ty))
2263     return PT->getPointeeType()->getAs<RecordType>();
2264   return Ty->getAs<RecordType>();
2265 }
2266 
2267 template <class Emitter>
2268 Record *ByteCodeExprGen<Emitter>::getRecord(QualType Ty) {
2269   if (const auto *RecordTy = getRecordTy(Ty))
2270     return getRecord(RecordTy->getDecl());
2271   return nullptr;
2272 }
2273 
2274 template <class Emitter>
2275 Record *ByteCodeExprGen<Emitter>::getRecord(const RecordDecl *RD) {
2276   return P.getOrCreateRecord(RD);
2277 }
2278 
2279 template <class Emitter>
2280 const Function *ByteCodeExprGen<Emitter>::getFunction(const FunctionDecl *FD) {
2281   return Ctx.getOrCreateFunction(FD);
2282 }
2283 
2284 template <class Emitter>
2285 bool ByteCodeExprGen<Emitter>::visitExpr(const Expr *E) {
2286   ExprScope<Emitter> RootScope(this);
2287   // Void expressions.
2288   if (E->getType()->isVoidType()) {
2289     if (!visit(E))
2290       return false;
2291     return this->emitRetVoid(E);
2292   }
2293 
2294   // Expressions with a primitive return type.
2295   if (std::optional<PrimType> T = classify(E)) {
2296     if (!visit(E))
2297       return false;
2298     return this->emitRet(*T, E);
2299   }
2300 
2301   // Expressions with a composite return type.
2302   // For us, that means everything we don't
2303   // have a PrimType for.
2304   if (std::optional<unsigned> LocalOffset = this->allocateLocal(E)) {
2305     if (!this->visitLocalInitializer(E, *LocalOffset))
2306       return false;
2307 
2308     if (!this->emitGetPtrLocal(*LocalOffset, E))
2309       return false;
2310     return this->emitRetValue(E);
2311   }
2312 
2313   return false;
2314 }
2315 
2316 /// Toplevel visitDecl().
2317 /// We get here from evaluateAsInitializer().
2318 /// We need to evaluate the initializer and return its value.
2319 template <class Emitter>
2320 bool ByteCodeExprGen<Emitter>::visitDecl(const VarDecl *VD) {
2321   assert(!VD->isInvalidDecl() && "Trying to constant evaluate an invalid decl");
2322 
2323   // Create and initialize the variable.
2324   if (!this->visitVarDecl(VD))
2325     return false;
2326 
2327   std::optional<PrimType> VarT = classify(VD->getType());
2328   // Get a pointer to the variable
2329   if (Context::shouldBeGloballyIndexed(VD)) {
2330     auto GlobalIndex = P.getGlobal(VD);
2331     assert(GlobalIndex); // visitVarDecl() didn't return false.
2332     if (VarT) {
2333       if (!this->emitGetGlobal(*VarT, *GlobalIndex, VD))
2334         return false;
2335     } else {
2336       if (!this->emitGetPtrGlobal(*GlobalIndex, VD))
2337         return false;
2338     }
2339   } else {
2340     auto Local = Locals.find(VD);
2341     assert(Local != Locals.end()); // Same here.
2342     if (VarT) {
2343       if (!this->emitGetLocal(*VarT, Local->second.Offset, VD))
2344         return false;
2345     } else {
2346       if (!this->emitGetPtrLocal(Local->second.Offset, VD))
2347         return false;
2348     }
2349   }
2350 
2351   // Return the value
2352   if (VarT)
2353     return this->emitRet(*VarT, VD);
2354   return this->emitRetValue(VD);
2355 }
2356 
2357 template <class Emitter>
2358 bool ByteCodeExprGen<Emitter>::visitVarDecl(const VarDecl *VD) {
2359   // We don't know what to do with these, so just return false.
2360   if (VD->getType().isNull())
2361     return false;
2362 
2363   const Expr *Init = VD->getInit();
2364   std::optional<PrimType> VarT = classify(VD->getType());
2365 
2366   if (Context::shouldBeGloballyIndexed(VD)) {
2367     // We've already seen and initialized this global.
2368     if (P.getGlobal(VD))
2369       return true;
2370 
2371     std::optional<unsigned> GlobalIndex = P.createGlobal(VD, Init);
2372 
2373     if (!GlobalIndex)
2374       return this->bail(VD);
2375 
2376     assert(Init);
2377     {
2378       DeclScope<Emitter> LocalScope(this, VD);
2379 
2380       if (VarT) {
2381         if (!this->visit(Init))
2382           return false;
2383         return this->emitInitGlobal(*VarT, *GlobalIndex, VD);
2384       }
2385       return this->visitGlobalInitializer(Init, *GlobalIndex);
2386     }
2387   } else {
2388     VariableScope<Emitter> LocalScope(this);
2389     if (VarT) {
2390       unsigned Offset = this->allocateLocalPrimitive(
2391           VD, *VarT, VD->getType().isConstQualified());
2392       if (Init) {
2393         // Compile the initializer in its own scope.
2394         ExprScope<Emitter> Scope(this);
2395         if (!this->visit(Init))
2396           return false;
2397 
2398         return this->emitSetLocal(*VarT, Offset, VD);
2399       }
2400     } else {
2401       if (std::optional<unsigned> Offset = this->allocateLocal(VD)) {
2402         if (Init)
2403           return this->visitLocalInitializer(Init, *Offset);
2404       }
2405     }
2406     return true;
2407   }
2408 
2409   return false;
2410 }
2411 
2412 template <class Emitter>
2413 bool ByteCodeExprGen<Emitter>::visitAPValue(const APValue &Val,
2414                                             PrimType ValType, const Expr *E) {
2415   assert(!DiscardResult);
2416   if (Val.isInt())
2417     return this->emitConst(Val.getInt(), ValType, E);
2418 
2419   if (Val.isLValue()) {
2420     APValue::LValueBase Base = Val.getLValueBase();
2421     if (const Expr *BaseExpr = Base.dyn_cast<const Expr *>())
2422       return this->visit(BaseExpr);
2423   }
2424 
2425   return false;
2426 }
2427 
2428 template <class Emitter>
2429 bool ByteCodeExprGen<Emitter>::VisitBuiltinCallExpr(const CallExpr *E) {
2430   const Function *Func = getFunction(E->getDirectCallee());
2431   if (!Func)
2432     return false;
2433 
2434   if (!Func->isUnevaluatedBuiltin()) {
2435     // Put arguments on the stack.
2436     for (const auto *Arg : E->arguments()) {
2437       if (!this->visit(Arg))
2438         return false;
2439     }
2440   }
2441 
2442   if (!this->emitCallBI(Func, E, E))
2443     return false;
2444 
2445   QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
2446   if (DiscardResult && !ReturnType->isVoidType()) {
2447     PrimType T = classifyPrim(ReturnType);
2448     return this->emitPop(T, E);
2449   }
2450 
2451   return true;
2452 }
2453 
2454 template <class Emitter>
2455 bool ByteCodeExprGen<Emitter>::VisitCallExpr(const CallExpr *E) {
2456   if (E->getBuiltinCallee())
2457     return VisitBuiltinCallExpr(E);
2458 
2459   QualType ReturnType = E->getCallReturnType(Ctx.getASTContext());
2460   std::optional<PrimType> T = classify(ReturnType);
2461   bool HasRVO = !ReturnType->isVoidType() && !T;
2462 
2463   if (HasRVO) {
2464     if (DiscardResult) {
2465       // If we need to discard the return value but the function returns its
2466       // value via an RVO pointer, we need to create one such pointer just
2467       // for this call.
2468       if (std::optional<unsigned> LocalIndex = allocateLocal(E)) {
2469         if (!this->emitGetPtrLocal(*LocalIndex, E))
2470           return false;
2471       }
2472     } else {
2473       assert(Initializing);
2474       if (!this->emitDupPtr(E))
2475         return false;
2476     }
2477   }
2478 
2479   // Add the (optional, implicit) This pointer.
2480   if (const auto *MC = dyn_cast<CXXMemberCallExpr>(E)) {
2481     if (!this->visit(MC->getImplicitObjectArgument()))
2482       return false;
2483   }
2484 
2485   // Put arguments on the stack.
2486   for (const auto *Arg : E->arguments()) {
2487     if (!this->visit(Arg))
2488       return false;
2489   }
2490 
2491   if (const FunctionDecl *FuncDecl = E->getDirectCallee()) {
2492     const Function *Func = getFunction(FuncDecl);
2493     if (!Func)
2494       return false;
2495     // If the function is being compiled right now, this is a recursive call.
2496     // In that case, the function can't be valid yet, even though it will be
2497     // later.
2498     // If the function is already fully compiled but not constexpr, it was
2499     // found to be faulty earlier on, so bail out.
2500     if (Func->isFullyCompiled() && !Func->isConstexpr())
2501       return false;
2502 
2503     assert(HasRVO == Func->hasRVO());
2504 
2505     bool HasQualifier = false;
2506     if (const auto *ME = dyn_cast<MemberExpr>(E->getCallee()))
2507       HasQualifier = ME->hasQualifier();
2508 
2509     bool IsVirtual = false;
2510     if (const auto *MD = dyn_cast<CXXMethodDecl>(FuncDecl))
2511       IsVirtual = MD->isVirtual();
2512 
2513     // In any case call the function. The return value will end up on the stack
2514     // and if the function has RVO, we already have the pointer on the stack to
2515     // write the result into.
2516     if (IsVirtual && !HasQualifier) {
2517       if (!this->emitCallVirt(Func, E))
2518         return false;
2519     } else {
2520       if (!this->emitCall(Func, E))
2521         return false;
2522     }
2523   } else {
2524     // Indirect call. Visit the callee, which will leave a FunctionPointer on
2525     // the stack. Cleanup of the returned value if necessary will be done after
2526     // the function call completed.
2527     if (!this->visit(E->getCallee()))
2528       return false;
2529 
2530     if (!this->emitCallPtr(E))
2531       return false;
2532   }
2533 
2534   // Cleanup for discarded return values.
2535   if (DiscardResult && !ReturnType->isVoidType() && T)
2536     return this->emitPop(*T, E);
2537 
2538   return true;
2539 }
2540 
2541 template <class Emitter>
2542 bool ByteCodeExprGen<Emitter>::VisitCXXDefaultInitExpr(
2543     const CXXDefaultInitExpr *E) {
2544   SourceLocScope<Emitter> SLS(this, E);
2545   if (Initializing)
2546     return this->visitInitializer(E->getExpr());
2547 
2548   assert(classify(E->getType()));
2549   return this->visit(E->getExpr());
2550 }
2551 
2552 template <class Emitter>
2553 bool ByteCodeExprGen<Emitter>::VisitCXXDefaultArgExpr(
2554     const CXXDefaultArgExpr *E) {
2555   SourceLocScope<Emitter> SLS(this, E);
2556 
2557   const Expr *SubExpr = E->getExpr();
2558   if (std::optional<PrimType> T = classify(E->getExpr()))
2559     return this->visit(SubExpr);
2560 
2561   assert(Initializing);
2562   return this->visitInitializer(SubExpr);
2563 }
2564 
2565 template <class Emitter>
2566 bool ByteCodeExprGen<Emitter>::VisitCXXBoolLiteralExpr(
2567     const CXXBoolLiteralExpr *E) {
2568   if (DiscardResult)
2569     return true;
2570 
2571   return this->emitConstBool(E->getValue(), E);
2572 }
2573 
2574 template <class Emitter>
2575 bool ByteCodeExprGen<Emitter>::VisitCXXNullPtrLiteralExpr(
2576     const CXXNullPtrLiteralExpr *E) {
2577   if (DiscardResult)
2578     return true;
2579 
2580   return this->emitNullPtr(E);
2581 }
2582 
2583 template <class Emitter>
2584 bool ByteCodeExprGen<Emitter>::VisitGNUNullExpr(const GNUNullExpr *E) {
2585   if (DiscardResult)
2586     return true;
2587 
2588   assert(E->getType()->isIntegerType());
2589 
2590   PrimType T = classifyPrim(E->getType());
2591   return this->emitZero(T, E);
2592 }
2593 
2594 template <class Emitter>
2595 bool ByteCodeExprGen<Emitter>::VisitCXXThisExpr(const CXXThisExpr *E) {
2596   if (DiscardResult)
2597     return true;
2598 
2599   if (this->LambdaThisCapture > 0)
2600     return this->emitGetThisFieldPtr(this->LambdaThisCapture, E);
2601 
2602   return this->emitThis(E);
2603 }
2604 
2605 template <class Emitter>
2606 bool ByteCodeExprGen<Emitter>::VisitUnaryOperator(const UnaryOperator *E) {
2607   const Expr *SubExpr = E->getSubExpr();
2608   std::optional<PrimType> T = classify(SubExpr->getType());
2609 
2610   switch (E->getOpcode()) {
2611   case UO_PostInc: { // x++
2612     if (!this->visit(SubExpr))
2613       return false;
2614 
2615     if (T == PT_Ptr) {
2616       if (!this->emitIncPtr(E))
2617         return false;
2618 
2619       return DiscardResult ? this->emitPopPtr(E) : true;
2620     }
2621 
2622     if (T == PT_Float) {
2623       return DiscardResult ? this->emitIncfPop(getRoundingMode(E), E)
2624                            : this->emitIncf(getRoundingMode(E), E);
2625     }
2626 
2627     return DiscardResult ? this->emitIncPop(*T, E) : this->emitInc(*T, E);
2628   }
2629   case UO_PostDec: { // x--
2630     if (!this->visit(SubExpr))
2631       return false;
2632 
2633     if (T == PT_Ptr) {
2634       if (!this->emitDecPtr(E))
2635         return false;
2636 
2637       return DiscardResult ? this->emitPopPtr(E) : true;
2638     }
2639 
2640     if (T == PT_Float) {
2641       return DiscardResult ? this->emitDecfPop(getRoundingMode(E), E)
2642                            : this->emitDecf(getRoundingMode(E), E);
2643     }
2644 
2645     return DiscardResult ? this->emitDecPop(*T, E) : this->emitDec(*T, E);
2646   }
2647   case UO_PreInc: { // ++x
2648     if (!this->visit(SubExpr))
2649       return false;
2650 
2651     if (T == PT_Ptr) {
2652       if (!this->emitLoadPtr(E))
2653         return false;
2654       if (!this->emitConstUint8(1, E))
2655         return false;
2656       if (!this->emitAddOffsetUint8(E))
2657         return false;
2658       return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
2659     }
2660 
2661     // Post-inc and pre-inc are the same if the value is to be discarded.
2662     if (DiscardResult) {
2663       if (T == PT_Float)
2664         return this->emitIncfPop(getRoundingMode(E), E);
2665       return this->emitIncPop(*T, E);
2666     }
2667 
2668     if (T == PT_Float) {
2669       const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
2670       if (!this->emitLoadFloat(E))
2671         return false;
2672       if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
2673         return false;
2674       if (!this->emitAddf(getRoundingMode(E), E))
2675         return false;
2676       return this->emitStoreFloat(E);
2677     }
2678     if (!this->emitLoad(*T, E))
2679       return false;
2680     if (!this->emitConst(1, E))
2681       return false;
2682     if (!this->emitAdd(*T, E))
2683       return false;
2684     return this->emitStore(*T, E);
2685   }
2686   case UO_PreDec: { // --x
2687     if (!this->visit(SubExpr))
2688       return false;
2689 
2690     if (T == PT_Ptr) {
2691       if (!this->emitLoadPtr(E))
2692         return false;
2693       if (!this->emitConstUint8(1, E))
2694         return false;
2695       if (!this->emitSubOffsetUint8(E))
2696         return false;
2697       return DiscardResult ? this->emitStorePopPtr(E) : this->emitStorePtr(E);
2698     }
2699 
2700     // Post-dec and pre-dec are the same if the value is to be discarded.
2701     if (DiscardResult) {
2702       if (T == PT_Float)
2703         return this->emitDecfPop(getRoundingMode(E), E);
2704       return this->emitDecPop(*T, E);
2705     }
2706 
2707     if (T == PT_Float) {
2708       const auto &TargetSemantics = Ctx.getFloatSemantics(E->getType());
2709       if (!this->emitLoadFloat(E))
2710         return false;
2711       if (!this->emitConstFloat(llvm::APFloat(TargetSemantics, 1), E))
2712         return false;
2713       if (!this->emitSubf(getRoundingMode(E), E))
2714         return false;
2715       return this->emitStoreFloat(E);
2716     }
2717     if (!this->emitLoad(*T, E))
2718       return false;
2719     if (!this->emitConst(1, E))
2720       return false;
2721     if (!this->emitSub(*T, E))
2722       return false;
2723     return this->emitStore(*T, E);
2724   }
2725   case UO_LNot: // !x
2726     if (DiscardResult)
2727       return this->discard(SubExpr);
2728 
2729     if (!this->visitBool(SubExpr))
2730       return false;
2731 
2732     if (!this->emitInvBool(E))
2733       return false;
2734 
2735     if (PrimType ET = classifyPrim(E->getType()); ET != PT_Bool)
2736       return this->emitCast(PT_Bool, ET, E);
2737     return true;
2738   case UO_Minus: // -x
2739     if (!this->visit(SubExpr))
2740       return false;
2741     return DiscardResult ? this->emitPop(*T, E) : this->emitNeg(*T, E);
2742   case UO_Plus:  // +x
2743     if (!this->visit(SubExpr)) // noop
2744       return false;
2745     return DiscardResult ? this->emitPop(*T, E) : true;
2746   case UO_AddrOf: // &x
2747     // We should already have a pointer when we get here.
2748     return this->delegate(SubExpr);
2749   case UO_Deref:  // *x
2750     return dereference(
2751         SubExpr, DerefKind::Read,
2752         [](PrimType) {
2753           llvm_unreachable("Dereferencing requires a pointer");
2754           return false;
2755         },
2756         [this, E](PrimType T) {
2757           return DiscardResult ? this->emitPop(T, E) : true;
2758         });
2759   case UO_Not:    // ~x
2760     if (!this->visit(SubExpr))
2761       return false;
2762     return DiscardResult ? this->emitPop(*T, E) : this->emitComp(*T, E);
2763   case UO_Real: { // __real x
2764     assert(!T);
2765     if (!this->visit(SubExpr))
2766       return false;
2767     if (!this->emitConstUint8(0, E))
2768       return false;
2769     if (!this->emitArrayElemPtrPopUint8(E))
2770       return false;
2771 
2772     // Since our _Complex implementation does not map to a primitive type,
2773     // we sometimes have to do the lvalue-to-rvalue conversion here manually.
2774     if (!SubExpr->isLValue())
2775       return this->emitLoadPop(classifyPrim(E->getType()), E);
2776     return true;
2777   }
2778   case UO_Imag: { // __imag x
2779     assert(!T);
2780     if (!this->visit(SubExpr))
2781       return false;
2782     if (!this->emitConstUint8(1, E))
2783       return false;
2784     if (!this->emitArrayElemPtrPopUint8(E))
2785       return false;
2786 
2787     // Since our _Complex implementation does not map to a primitive type,
2788     // we sometimes have to do the lvalue-to-rvalue conversion here manually.
2789     if (!SubExpr->isLValue())
2790       return this->emitLoadPop(classifyPrim(E->getType()), E);
2791     return true;
2792   }
2793   case UO_Extension:
2794     return this->delegate(SubExpr);
2795   case UO_Coawait:
2796     assert(false && "Unhandled opcode");
2797   }
2798 
2799   return false;
2800 }
2801 
2802 template <class Emitter>
2803 bool ByteCodeExprGen<Emitter>::VisitDeclRefExpr(const DeclRefExpr *E) {
2804   if (DiscardResult)
2805     return true;
2806 
2807   const auto *D = E->getDecl();
2808 
2809   if (const auto *ECD = dyn_cast<EnumConstantDecl>(D)) {
2810     return this->emitConst(ECD->getInitVal(), E);
2811   } else if (const auto *BD = dyn_cast<BindingDecl>(D)) {
2812     return this->visit(BD->getBinding());
2813   } else if (const auto *FuncDecl = dyn_cast<FunctionDecl>(D)) {
2814     const Function *F = getFunction(FuncDecl);
2815     return F && this->emitGetFnPtr(F, E);
2816   }
2817 
2818   // References are implemented via pointers, so when we see a DeclRefExpr
2819   // pointing to a reference, we need to get its value directly (i.e. the
2820   // pointer to the actual value) instead of a pointer to the pointer to the
2821   // value.
2822   bool IsReference = D->getType()->isReferenceType();
2823 
2824   // Check for local/global variables and parameters.
2825   if (auto It = Locals.find(D); It != Locals.end()) {
2826     const unsigned Offset = It->second.Offset;
2827 
2828     if (IsReference)
2829       return this->emitGetLocal(PT_Ptr, Offset, E);
2830     return this->emitGetPtrLocal(Offset, E);
2831   } else if (auto GlobalIndex = P.getGlobal(D)) {
2832     if (IsReference)
2833       return this->emitGetGlobalPtr(*GlobalIndex, E);
2834 
2835     return this->emitGetPtrGlobal(*GlobalIndex, E);
2836   } else if (const auto *PVD = dyn_cast<ParmVarDecl>(D)) {
2837     if (auto It = this->Params.find(PVD); It != this->Params.end()) {
2838       if (IsReference || !It->second.IsPtr)
2839         return this->emitGetParamPtr(It->second.Offset, E);
2840 
2841       return this->emitGetPtrParam(It->second.Offset, E);
2842     }
2843   }
2844 
2845   // Handle lambda captures.
2846   if (auto It = this->LambdaCaptures.find(D);
2847       It != this->LambdaCaptures.end()) {
2848     auto [Offset, IsPtr] = It->second;
2849 
2850     if (IsPtr)
2851       return this->emitGetThisFieldPtr(Offset, E);
2852     return this->emitGetPtrThisField(Offset, E);
2853   }
2854 
2855   // Lazily visit global declarations we haven't seen yet.
2856   // This happens in C.
2857   if (!Ctx.getLangOpts().CPlusPlus) {
2858     if (const auto *VD = dyn_cast<VarDecl>(D);
2859         VD && VD->hasGlobalStorage() && VD->getAnyInitializer() &&
2860         VD->getType().isConstQualified()) {
2861       if (!this->visitVarDecl(VD))
2862         return false;
2863       // Retry.
2864       return this->VisitDeclRefExpr(E);
2865     }
2866 
2867     if (std::optional<unsigned> I = P.getOrCreateDummy(D))
2868       return this->emitGetPtrGlobal(*I, E);
2869   }
2870 
2871   return this->emitInvalidDeclRef(E, E);
2872 }
2873 
2874 template <class Emitter>
2875 void ByteCodeExprGen<Emitter>::emitCleanup() {
2876   for (VariableScope<Emitter> *C = VarScope; C; C = C->getParent())
2877     C->emitDestruction();
2878 }
2879 
2880 template <class Emitter>
2881 unsigned
2882 ByteCodeExprGen<Emitter>::collectBaseOffset(const RecordType *BaseType,
2883                                             const RecordType *DerivedType) {
2884   const auto *FinalDecl = cast<CXXRecordDecl>(BaseType->getDecl());
2885   const RecordDecl *CurDecl = DerivedType->getDecl();
2886   const Record *CurRecord = getRecord(CurDecl);
2887   assert(CurDecl && FinalDecl);
2888 
2889   unsigned OffsetSum = 0;
2890   for (;;) {
2891     assert(CurRecord->getNumBases() > 0);
2892     // One level up
2893     for (const Record::Base &B : CurRecord->bases()) {
2894       const auto *BaseDecl = cast<CXXRecordDecl>(B.Decl);
2895 
2896       if (BaseDecl == FinalDecl || BaseDecl->isDerivedFrom(FinalDecl)) {
2897         OffsetSum += B.Offset;
2898         CurRecord = B.R;
2899         CurDecl = BaseDecl;
2900         break;
2901       }
2902     }
2903     if (CurDecl == FinalDecl)
2904       break;
2905   }
2906 
2907   assert(OffsetSum > 0);
2908   return OffsetSum;
2909 }
2910 
2911 /// Emit casts from a PrimType to another PrimType.
2912 template <class Emitter>
2913 bool ByteCodeExprGen<Emitter>::emitPrimCast(PrimType FromT, PrimType ToT,
2914                                             QualType ToQT, const Expr *E) {
2915 
2916   if (FromT == PT_Float) {
2917     // Floating to floating.
2918     if (ToT == PT_Float) {
2919       const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
2920       return this->emitCastFP(ToSem, getRoundingMode(E), E);
2921     }
2922 
2923     // Float to integral.
2924     if (isIntegralType(ToT) || ToT == PT_Bool)
2925       return this->emitCastFloatingIntegral(ToT, E);
2926   }
2927 
2928   if (isIntegralType(FromT) || FromT == PT_Bool) {
2929     // Integral to integral.
2930     if (isIntegralType(ToT) || ToT == PT_Bool)
2931       return FromT != ToT ? this->emitCast(FromT, ToT, E) : true;
2932 
2933     if (ToT == PT_Float) {
2934       // Integral to floating.
2935       const llvm::fltSemantics *ToSem = &Ctx.getFloatSemantics(ToQT);
2936       return this->emitCastIntegralFloating(FromT, ToSem, getRoundingMode(E),
2937                                             E);
2938     }
2939   }
2940 
2941   return false;
2942 }
2943 
2944 /// When calling this, we have a pointer of the local-to-destroy
2945 /// on the stack.
2946 /// Emit destruction of record types (or arrays of record types).
2947 /// FIXME: Handle virtual destructors.
2948 template <class Emitter>
2949 bool ByteCodeExprGen<Emitter>::emitRecordDestruction(const Descriptor *Desc) {
2950   assert(Desc);
2951   assert(!Desc->isPrimitive());
2952   assert(!Desc->isPrimitiveArray());
2953 
2954   // Arrays.
2955   if (Desc->isArray()) {
2956     const Descriptor *ElemDesc = Desc->ElemDesc;
2957     assert(ElemDesc);
2958 
2959     // Don't need to do anything for these.
2960     if (ElemDesc->isPrimitiveArray())
2961       return this->emitPopPtr(SourceInfo{});
2962 
2963     // If this is an array of record types, check if we need
2964     // to call the element destructors at all. If not, try
2965     // to save the work.
2966     if (const Record *ElemRecord = ElemDesc->ElemRecord) {
2967       if (const CXXDestructorDecl *Dtor = ElemRecord->getDestructor();
2968           !Dtor || Dtor->isTrivial())
2969         return this->emitPopPtr(SourceInfo{});
2970     }
2971 
2972     for (ssize_t I = Desc->getNumElems() - 1; I >= 0; --I) {
2973       if (!this->emitConstUint64(I, SourceInfo{}))
2974         return false;
2975       if (!this->emitArrayElemPtrUint64(SourceInfo{}))
2976         return false;
2977       if (!this->emitRecordDestruction(ElemDesc))
2978         return false;
2979     }
2980     return this->emitPopPtr(SourceInfo{});
2981   }
2982 
2983   const Record *R = Desc->ElemRecord;
2984   assert(R);
2985   // First, destroy all fields.
2986   for (const Record::Field &Field : llvm::reverse(R->fields())) {
2987     const Descriptor *D = Field.Desc;
2988     if (!D->isPrimitive() && !D->isPrimitiveArray()) {
2989       if (!this->emitDupPtr(SourceInfo{}))
2990         return false;
2991       if (!this->emitGetPtrField(Field.Offset, SourceInfo{}))
2992         return false;
2993       if (!this->emitRecordDestruction(D))
2994         return false;
2995     }
2996   }
2997 
2998   // FIXME: Unions need to be handled differently here. We don't want to
2999   //   call the destructor of its members.
3000 
3001   // Now emit the destructor and recurse into base classes.
3002   if (const CXXDestructorDecl *Dtor = R->getDestructor();
3003       Dtor && !Dtor->isTrivial()) {
3004     if (const Function *DtorFunc = getFunction(Dtor)) {
3005       assert(DtorFunc->hasThisPointer());
3006       assert(DtorFunc->getNumParams() == 1);
3007       if (!this->emitDupPtr(SourceInfo{}))
3008         return false;
3009       if (!this->emitCall(DtorFunc, SourceInfo{}))
3010         return false;
3011     }
3012   }
3013 
3014   for (const Record::Base &Base : llvm::reverse(R->bases())) {
3015     if (!this->emitGetPtrBase(Base.Offset, SourceInfo{}))
3016       return false;
3017     if (!this->emitRecordDestruction(Base.Desc))
3018       return false;
3019   }
3020   // FIXME: Virtual bases.
3021 
3022   // Remove the instance pointer.
3023   return this->emitPopPtr(SourceInfo{});
3024 }
3025 
3026 namespace clang {
3027 namespace interp {
3028 
3029 template class ByteCodeExprGen<ByteCodeEmitter>;
3030 template class ByteCodeExprGen<EvalEmitter>;
3031 
3032 } // namespace interp
3033 } // namespace clang
3034