Skip to content

[clang][bytecode][NFC] Discard all CastExprs uniformly #126511

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 10, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 4 additions & 41 deletions clang/lib/AST/ByteCode/Compiler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -194,12 +194,12 @@ template <class Emitter> class StmtExprScope final {
template <class Emitter>
bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
const Expr *SubExpr = CE->getSubExpr();
switch (CE->getCastKind()) {

case CK_LValueToRValue: {
if (DiscardResult)
return this->discard(SubExpr);
if (DiscardResult)
return this->delegate(SubExpr);

switch (CE->getCastKind()) {
case CK_LValueToRValue: {
std::optional<PrimType> SubExprT = classify(SubExpr->getType());
// Prepare storage for the result.
if (!Initializing && !SubExprT) {
Expand Down Expand Up @@ -253,9 +253,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {

case CK_UncheckedDerivedToBase:
case CK_DerivedToBase: {
if (DiscardResult)
return this->discard(SubExpr);

if (!this->delegate(SubExpr))
return false;

Expand Down Expand Up @@ -285,9 +282,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
}

case CK_BaseToDerived: {
if (DiscardResult)
return this->discard(SubExpr);

if (!this->delegate(SubExpr))
return false;

Expand All @@ -302,17 +296,13 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
if (!SubExpr->getType()->isFloatingType() ||
!CE->getType()->isFloatingType())
return false;
if (DiscardResult)
return this->discard(SubExpr);
if (!this->visit(SubExpr))
return false;
const auto *TargetSemantics = &Ctx.getFloatSemantics(CE->getType());
return this->emitCastFP(TargetSemantics, getRoundingMode(CE), CE);
}

case CK_IntegralToFloating: {
if (DiscardResult)
return this->discard(SubExpr);
std::optional<PrimType> FromT = classify(SubExpr->getType());
if (!FromT)
return false;
Expand All @@ -327,8 +317,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {

case CK_FloatingToBoolean:
case CK_FloatingToIntegral: {
if (DiscardResult)
return this->discard(SubExpr);

std::optional<PrimType> ToT = classify(CE->getType());

Expand All @@ -352,9 +340,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
case CK_NullToMemberPointer: {
if (!this->discard(SubExpr))
return false;
if (DiscardResult)
return true;

const Descriptor *Desc = nullptr;
const QualType PointeeType = CE->getType()->getPointeeType();
if (!PointeeType.isNull()) {
Expand All @@ -371,9 +356,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
}

case CK_PointerToIntegral: {
if (DiscardResult)
return this->discard(SubExpr);

if (!this->visit(SubExpr))
return false;

Expand All @@ -399,8 +381,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
return false;
if (!this->emitArrayDecay(CE))
return false;
if (DiscardResult)
return this->emitPopPtr(CE);
return true;
}

Expand All @@ -412,9 +392,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
// FIXME: I think the discard is wrong since the int->ptr cast might cause a
// diagnostic.
PrimType T = classifyPrim(IntType);
if (DiscardResult)
return this->emitPop(T, CE);

QualType PtrType = CE->getType();
const Descriptor *Desc;
if (std::optional<PrimType> T = classify(PtrType->getPointeeType()))
Expand Down Expand Up @@ -454,10 +431,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
return false;
return this->emitInvalidCast(CastKind::Reinterpret, /*Fatal=*/true, CE);
}

if (DiscardResult)
return this->discard(SubExpr);

QualType SubExprTy = SubExpr->getType();
std::optional<PrimType> FromT = classify(SubExprTy);
// Casts from integer/vector to vector.
Expand Down Expand Up @@ -493,8 +466,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
case CK_FixedPointToBoolean:
case CK_BooleanToSignedIntegral:
case CK_IntegralCast: {
if (DiscardResult)
return this->discard(SubExpr);
std::optional<PrimType> FromT = classify(SubExpr->getType());
std::optional<PrimType> ToT = classify(CE->getType());

Expand Down Expand Up @@ -546,8 +517,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {

case CK_IntegralComplexToBoolean:
case CK_FloatingComplexToBoolean: {
if (DiscardResult)
return this->discard(SubExpr);
if (!this->visit(SubExpr))
return false;
return this->emitComplexBoolCast(SubExpr);
Expand Down Expand Up @@ -585,9 +554,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
case CK_FloatingComplexToIntegralComplex: {
assert(CE->getType()->isAnyComplexType());
assert(SubExpr->getType()->isAnyComplexType());
if (DiscardResult)
return this->discard(SubExpr);

if (!Initializing) {
std::optional<unsigned> LocalIndex = allocateLocal(CE);
if (!LocalIndex)
Expand Down Expand Up @@ -633,9 +599,6 @@ bool Compiler<Emitter>::VisitCastExpr(const CastExpr *CE) {
assert(classify(SubExpr->getType()));
assert(CE->getType()->isVectorType());

if (DiscardResult)
return this->discard(SubExpr);

if (!Initializing) {
std::optional<unsigned> LocalIndex = allocateLocal(CE);
if (!LocalIndex)
Expand Down