mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-09-24 18:40:16 +01:00
Simplify logic in StringifyTypeExpr using push_string (#4561)
Follow-on to #4511. --------- Co-authored-by: Josh L <josh11b@users.noreply.github.com>
This commit is contained in:
@@ -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,
|
||||
@@ -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});
|
||||
@@ -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:
|
||||
@@ -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 ";
|
||||
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());
|
||||
}
|
||||
|
||||
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): {
|
||||
@@ -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;
|
||||
@@ -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;
|
||||
@@ -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): {
|
||||
@@ -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 << ")";
|
||||
break;
|
||||
}
|
||||
steps.push_back(step.Next());
|
||||
push_inst_id(sem_ir.types().GetInstId(refs[step.index]));
|
||||
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(", ");
|
||||
}
|
||||
}
|
||||
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:
|
||||
|
||||
Reference in New Issue
Block a user