@@ -820,14 +820,24 @@ bool ByteCodeExprGen<Emitter>::VisitArrayInitLoopExpr(
820820 // Investigate compiling this to a loop.
821821
822822 const Expr *SubExpr = E->getSubExpr ();
823+ const Expr *CommonExpr = E->getCommonExpr ();
823824 size_t Size = E->getArraySize ().getZExtValue ();
824825 std::optional<PrimType> ElemT = classify (SubExpr->getType ());
825826
827+ // If the common expression is an opaque expression, we visit it
828+ // here once so we have its value cached.
829+ // FIXME: This might be necessary (or useful) for all expressions.
830+ if (isa<OpaqueValueExpr>(CommonExpr)) {
831+ if (!this ->discard (CommonExpr))
832+ return false ;
833+ }
834+
826835 // So, every iteration, we execute an assignment here
827836 // where the LHS is on the stack (the target array)
828837 // and the RHS is our SubExpr.
829838 for (size_t I = 0 ; I != Size; ++I) {
830839 ArrayIndexScope<Emitter> IndexScope (this , I);
840+ BlockScope<Emitter> BS (this );
831841
832842 if (ElemT) {
833843 if (!this ->visit (SubExpr))
@@ -856,7 +866,7 @@ bool ByteCodeExprGen<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
856866
857867 PrimType SubExprT = classify (E->getSourceExpr ()).value_or (PT_Ptr);
858868 if (auto It = OpaqueExprs.find (E); It != OpaqueExprs.end ())
859- return this ->emitGetLocal (SubExprT, It->getSecond () , E);
869+ return this ->emitGetLocal (SubExprT, It->second , E);
860870
861871 if (!this ->visit (E->getSourceExpr ()))
862872 return false ;
@@ -873,8 +883,10 @@ bool ByteCodeExprGen<Emitter>::VisitOpaqueValueExpr(const OpaqueValueExpr *E) {
873883
874884 // Here the local variable is created but the value is removed from the stack,
875885 // so we put it back, because the caller might need it.
876- if (!this ->emitGetLocal (SubExprT, *LocalIndex, E))
877- return false ;
886+ if (!DiscardResult) {
887+ if (!this ->emitGetLocal (SubExprT, *LocalIndex, E))
888+ return false ;
889+ }
878890
879891 // FIXME: Ideally the cached value should be cleaned up later.
880892 OpaqueExprs.insert ({E, *LocalIndex});
0 commit comments