Skip to content

Commit 4a8b43b

Browse files
committed
[clang][Interp][NFC] Factor array element init into its own function
1 parent 4594d5b commit 4a8b43b

File tree

2 files changed

+28
-36
lines changed

2 files changed

+28
-36
lines changed

clang/lib/AST/Interp/ByteCodeExprGen.cpp

Lines changed: 27 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -614,6 +614,29 @@ bool ByteCodeExprGen<Emitter>::visitInitList(ArrayRef<const Expr *> Inits,
614614
return true;
615615
}
616616

617+
/// Pointer to the array(not the element!) must be on the stack when calling
618+
/// this.
619+
template <class Emitter>
620+
bool ByteCodeExprGen<Emitter>::visitArrayElemInit(unsigned ElemIndex,
621+
const Expr *Init) {
622+
if (std::optional<PrimType> T = classify(Init->getType())) {
623+
// Visit the primitive element like normal.
624+
if (!this->visit(Init))
625+
return false;
626+
return this->emitInitElem(*T, ElemIndex, Init);
627+
}
628+
629+
// Advance the pointer currently on the stack to the given
630+
// dimension.
631+
if (!this->emitConstUint32(ElemIndex, Init))
632+
return false;
633+
if (!this->emitArrayElemPtrUint32(Init))
634+
return false;
635+
if (!this->visitInitializer(Init))
636+
return false;
637+
return this->emitPopPtr(Init);
638+
}
639+
617640
template <class Emitter>
618641
bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
619642
// Handle discarding first.
@@ -642,25 +665,8 @@ bool ByteCodeExprGen<Emitter>::VisitInitListExpr(const InitListExpr *E) {
642665
// FIXME: Array fillers.
643666
unsigned ElementIndex = 0;
644667
for (const Expr *Init : E->inits()) {
645-
if (std::optional<PrimType> T = classify(Init->getType())) {
646-
// Visit the primitive element like normal.
647-
if (!this->visit(Init))
648-
return false;
649-
if (!this->emitInitElem(*T, ElementIndex, Init))
650-
return false;
651-
} else {
652-
// Advance the pointer currently on the stack to the given
653-
// dimension.
654-
if (!this->emitConstUint32(ElementIndex, Init))
655-
return false;
656-
if (!this->emitArrayElemPtrUint32(Init))
657-
return false;
658-
if (!this->visitInitializer(Init))
659-
return false;
660-
if (!this->emitPopPtr(Init))
661-
return false;
662-
}
663-
668+
if (!this->visitArrayElemInit(ElementIndex, Init))
669+
return false;
664670
++ElementIndex;
665671
}
666672
return true;
@@ -831,7 +837,6 @@ bool ByteCodeExprGen<Emitter>::VisitArrayInitLoopExpr(
831837
const Expr *SubExpr = E->getSubExpr();
832838
const Expr *CommonExpr = E->getCommonExpr();
833839
size_t Size = E->getArraySize().getZExtValue();
834-
std::optional<PrimType> ElemT = classify(SubExpr->getType());
835840

836841
// If the common expression is an opaque expression, we visit it
837842
// here once so we have its value cached.
@@ -848,22 +853,8 @@ bool ByteCodeExprGen<Emitter>::VisitArrayInitLoopExpr(
848853
ArrayIndexScope<Emitter> IndexScope(this, I);
849854
BlockScope<Emitter> BS(this);
850855

851-
if (ElemT) {
852-
if (!this->visit(SubExpr))
853-
return false;
854-
if (!this->emitInitElem(*ElemT, I, E))
855-
return false;
856-
} else {
857-
// Get to our array element and recurse into visitInitializer()
858-
if (!this->emitConstUint64(I, SubExpr))
859-
return false;
860-
if (!this->emitArrayElemPtrUint64(SubExpr))
861-
return false;
862-
if (!visitInitializer(SubExpr))
863-
return false;
864-
if (!this->emitPopPtr(E))
865-
return false;
866-
}
856+
if (!this->visitArrayElemInit(I, SubExpr))
857+
return false;
867858
}
868859
return true;
869860
}

clang/lib/AST/Interp/ByteCodeExprGen.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@ class ByteCodeExprGen : public ConstStmtVisitor<ByteCodeExprGen<Emitter>, bool>,
209209
}
210210

211211
bool visitInitList(ArrayRef<const Expr *> Inits, const Expr *E);
212+
bool visitArrayElemInit(unsigned ElemIndex, const Expr *Init);
212213

213214
/// Creates a local primitive value.
214215
unsigned allocateLocalPrimitive(DeclTy &&Decl, PrimType Ty, bool IsConst,

0 commit comments

Comments
 (0)