Skip to content

Commit

Permalink
Simplify logic in StringifyTypeExpr using push_string (#4561)
Browse files Browse the repository at this point in the history
Follow-on to #4511.

---------

Co-authored-by: Josh L <[email protected]>
  • Loading branch information
josh11b and josh11b authored Nov 26, 2024
1 parent c0faa81 commit 01ea408
Showing 1 changed file with 47 additions and 70 deletions.
117 changes: 47 additions & 70 deletions toolchain/sem_ir/stringify_type.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ auto StringifyTypeExpr(const SemIR::File& outer_sem_ir, InstId outer_inst_id)
llvm::SmallVector<Step> steps = {Step{
.sem_ir = outer_sem_ir, .kind = Step::Inst, .inst_id = outer_inst_id}};

// Note: The `steps` worklist is a stack and so work is resolved in the
// reverse order from the order added.
auto push_string = [&](const char* string) {
steps.push_back({.sem_ir = outer_sem_ir,
.kind = Step::FixedString,
Expand All @@ -78,6 +80,8 @@ auto StringifyTypeExpr(const SemIR::File& outer_sem_ir, InstId outer_inst_id)

const auto& sem_ir = step.sem_ir;
// Helper for instructions with the current sem_ir.
// Note: The `steps` worklist is a stack and so work is resolved in the
// reverse order from the order added.
auto push_inst_id = [&](InstId inst_id) {
steps.push_back(
{.sem_ir = sem_ir, .kind = Step::Inst, .inst_id = inst_id});
Expand Down Expand Up @@ -141,17 +145,11 @@ auto StringifyTypeExpr(const SemIR::File& outer_sem_ir, InstId outer_inst_id)
break;
}
case CARBON_KIND(AssociatedEntityType inst): {
if (step.index == 0) {
out << "<associated ";
steps.push_back(step.Next());
push_inst_id(sem_ir.types().GetInstId(inst.entity_type_id));
} else if (step.index == 1) {
out << " in ";
steps.push_back(step.Next());
push_inst_id(sem_ir.types().GetInstId(inst.interface_type_id));
} else {
out << ">";
}
out << "<associated ";
push_string(">");
push_inst_id(sem_ir.types().GetInstId(inst.interface_type_id));
push_string(" in ");
push_inst_id(sem_ir.types().GetInstId(inst.entity_type_id));
break;
}
case BindAlias::Kind:
Expand All @@ -170,21 +168,17 @@ auto StringifyTypeExpr(const SemIR::File& outer_sem_ir, InstId outer_inst_id)
break;
}
case CARBON_KIND(ConstType inst): {
if (step.index == 0) {
out << "const ";

// Add parentheses if required.
auto inner_type_inst_id = sem_ir.types().GetInstId(inst.inner_id);
if (GetTypePrecedence(sem_ir.insts().Get(inner_type_inst_id).kind()) <
GetTypePrecedence(SemIR::ConstType::Kind)) {
out << "(";
steps.push_back(step.Next());
}
out << "const ";

push_inst_id(inner_type_inst_id);
} else if (step.index == 1) {
out << ")";
// Add parentheses if required.
auto inner_type_inst_id = sem_ir.types().GetInstId(inst.inner_id);
if (GetTypePrecedence(sem_ir.insts().Get(inner_type_inst_id).kind()) <
GetTypePrecedence(SemIR::ConstType::Kind)) {
out << "(";
push_string(")");
}

push_inst_id(inner_type_inst_id);
break;
}
case CARBON_KIND(FacetAccessType inst): {
Expand Down Expand Up @@ -232,15 +226,13 @@ auto StringifyTypeExpr(const SemIR::File& outer_sem_ir, InstId outer_inst_id)
}
case CARBON_KIND(FloatType inst): {
// TODO: Is this okay?
if (step.index == 1) {
out << ")";
} else if (auto width_value =
sem_ir.insts().TryGetAs<IntValue>(inst.bit_width_id)) {
if (auto width_value =
sem_ir.insts().TryGetAs<IntValue>(inst.bit_width_id)) {
out << "f";
sem_ir.ints().Get(width_value->int_id).print(out, /*isSigned=*/false);
} else {
out << "Core.Float(";
steps.push_back(step.Next());
push_string(")");
push_inst_id(inst.bit_width_id);
}
break;
Expand All @@ -263,15 +255,13 @@ auto StringifyTypeExpr(const SemIR::File& outer_sem_ir, InstId outer_inst_id)
break;
}
case CARBON_KIND(IntType inst): {
if (step.index == 1) {
out << ")";
} else if (auto width_value =
sem_ir.insts().TryGetAs<IntValue>(inst.bit_width_id)) {
if (auto width_value =
sem_ir.insts().TryGetAs<IntValue>(inst.bit_width_id)) {
out << (inst.int_kind.is_signed() ? "i" : "u");
sem_ir.ints().Get(width_value->int_id).print(out, /*isSigned=*/false);
} else {
out << (inst.int_kind.is_signed() ? "Core.Int(" : "Core.UInt(");
steps.push_back(step.Next());
push_string(")");
push_inst_id(inst.bit_width_id);
}
break;
Expand All @@ -281,12 +271,8 @@ auto StringifyTypeExpr(const SemIR::File& outer_sem_ir, InstId outer_inst_id)
break;
}
case CARBON_KIND(PointerType inst): {
if (step.index == 0) {
steps.push_back(step.Next());
push_inst_id(sem_ir.types().GetInstId(inst.pointee_id));
} else if (step.index == 1) {
out << "*";
}
push_string("*");
push_inst_id(sem_ir.types().GetInstId(inst.pointee_id));
break;
}
case CARBON_KIND(StructType inst): {
Expand All @@ -313,44 +299,35 @@ auto StringifyTypeExpr(const SemIR::File& outer_sem_ir, InstId outer_inst_id)
if (refs.empty()) {
out << "()";
break;
} else if (step.index == 0) {
out << "(";
} else if (step.index < static_cast<int>(refs.size())) {
out << ", ";
} else {
// A tuple of one element has a comma to disambiguate from an
// expression.
if (step.index == 1) {
out << ",";
}
out << "(";
push_string(")");
// A tuple of one element has a comma to disambiguate from an
// expression.
if (refs.size() == 1) {
push_string(",");
}
for (auto i : llvm::reverse(llvm::seq(refs.size()))) {
push_inst_id(sem_ir.types().GetInstId(refs[i]));
if (i > 0) {
push_string(", ");
}
out << ")";
break;
}
steps.push_back(step.Next());
push_inst_id(sem_ir.types().GetInstId(refs[step.index]));
break;
}
case CARBON_KIND(UnboundElementType inst): {
if (step.index == 0) {
out << "<unbound element of class ";
steps.push_back(step.Next());
push_inst_id(sem_ir.types().GetInstId(inst.class_type_id));
} else {
out << ">";
}
out << "<unbound element of class ";
push_string(">");
push_inst_id(sem_ir.types().GetInstId(inst.class_type_id));
break;
}
case CARBON_KIND(WhereExpr inst): {
if (step.index == 0) {
out << "<where restriction on ";
steps.push_back(step.Next());
TypeId type_id = sem_ir.insts().Get(inst.period_self_id).type_id();
push_inst_id(sem_ir.types().GetInstId(type_id));
// TODO: Also output restrictions from the inst block
// inst.requirements_id.
} else {
out << ">";
}
out << "<where restriction on ";
push_string(">");
TypeId type_id = sem_ir.insts().Get(inst.period_self_id).type_id();
push_inst_id(sem_ir.types().GetInstId(type_id));
// TODO: Also output restrictions from the inst block
// inst.requirements_id.
break;
}
case AdaptDecl::Kind:
Expand Down

0 comments on commit 01ea408

Please sign in to comment.