mirror of
https://github.com/carbon-language/carbon-lang.git
synced 2026-10-05 22:02:55 +01:00
Explorer: Remove PrintDepth and implement PrintIndent. (#3113)
This PR removes `PrintDepth` from statement and declaration. Implements `PrintIndent` for better indented formatting along with various changes to make printing of statements and declarations better. --------- Co-authored-by: Jon Ross-Perkins <jperkins@google.com>
This commit is contained in:
co-authored by
Jon Ross-Perkins
parent
05e061fe96
commit
289d05813e
@@ -14,105 +14,110 @@ using llvm::cast;
|
||||
|
||||
Declaration::~Declaration() = default;
|
||||
|
||||
void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
void Declaration::Print(llvm::raw_ostream& out) const { PrintIndent(0, out); }
|
||||
void Declaration::PrintIndent(int indent_num_spaces,
|
||||
llvm::raw_ostream& out) const {
|
||||
if (kind() != DeclarationKind::FunctionDeclaration &&
|
||||
kind() != DeclarationKind::DestructorDeclaration) {
|
||||
out.indent(indent_num_spaces);
|
||||
}
|
||||
|
||||
switch (kind()) {
|
||||
case DeclarationKind::NamespaceDeclaration:
|
||||
PrintID(out);
|
||||
out << ";";
|
||||
out << PrintAsID(*this) << ";";
|
||||
break;
|
||||
case DeclarationKind::InterfaceDeclaration:
|
||||
case DeclarationKind::ConstraintDeclaration: {
|
||||
const auto& iface_decl = cast<ConstraintTypeDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << PrintAsID(*this);
|
||||
out << " {\n";
|
||||
for (Nonnull<Declaration*> m : iface_decl.members()) {
|
||||
out << *m;
|
||||
out.indent(indent_num_spaces + 2) << *m << "\n";
|
||||
}
|
||||
out << "}\n";
|
||||
out.indent(indent_num_spaces) << "}";
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::ImplDeclaration: {
|
||||
const auto& impl_decl = cast<ImplDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << " {\n";
|
||||
out << PrintAsID(impl_decl) << " {\n";
|
||||
for (Nonnull<Declaration*> m : impl_decl.members()) {
|
||||
out << *m;
|
||||
m->PrintIndent(indent_num_spaces + 2, out);
|
||||
out << "\n";
|
||||
}
|
||||
out << "}\n";
|
||||
out.indent(indent_num_spaces) << "}";
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::MatchFirstDeclaration: {
|
||||
const auto& match_first_decl = cast<MatchFirstDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << " {\n";
|
||||
out << PrintAsID(match_first_decl) << " {\n";
|
||||
for (Nonnull<const ImplDeclaration*> m :
|
||||
match_first_decl.impl_declarations()) {
|
||||
out << *m;
|
||||
m->PrintIndent(indent_num_spaces + 2, out);
|
||||
out << "\n";
|
||||
}
|
||||
out << "}\n";
|
||||
out.indent(indent_num_spaces) << "}";
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::FunctionDeclaration:
|
||||
cast<FunctionDeclaration>(*this).PrintDepth(-1, out);
|
||||
cast<FunctionDeclaration>(*this).PrintIndent(indent_num_spaces, out);
|
||||
break;
|
||||
case DeclarationKind::DestructorDeclaration:
|
||||
cast<DestructorDeclaration>(*this).PrintDepth(-1, out);
|
||||
cast<DestructorDeclaration>(*this).PrintIndent(indent_num_spaces, out);
|
||||
break;
|
||||
case DeclarationKind::ClassDeclaration: {
|
||||
const auto& class_decl = cast<ClassDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << PrintAsID(class_decl);
|
||||
if (class_decl.type_params().has_value()) {
|
||||
out << **class_decl.type_params();
|
||||
}
|
||||
out << " {\n";
|
||||
for (Nonnull<Declaration*> m : class_decl.members()) {
|
||||
out << *m;
|
||||
m->PrintIndent(indent_num_spaces + 2, out);
|
||||
out << "\n";
|
||||
}
|
||||
out << "}\n";
|
||||
out.indent(indent_num_spaces) << "}";
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::MixinDeclaration: {
|
||||
const auto& mixin_decl = cast<MixinDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << "{\n";
|
||||
out << PrintAsID(mixin_decl) << "{\n";
|
||||
for (Nonnull<Declaration*> m : mixin_decl.members()) {
|
||||
out << *m;
|
||||
m->PrintIndent(indent_num_spaces + 2, out);
|
||||
out << "\n";
|
||||
}
|
||||
out << "}\n";
|
||||
out.indent(indent_num_spaces) << "}";
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::MixDeclaration: {
|
||||
const auto& mix_decl = cast<MixDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << PrintAsID(mix_decl);
|
||||
out << mix_decl.mixin() << ";";
|
||||
break;
|
||||
}
|
||||
case DeclarationKind::ChoiceDeclaration: {
|
||||
const auto& choice = cast<ChoiceDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << " {\n";
|
||||
out << PrintAsID(choice) << " {\n";
|
||||
for (Nonnull<const AlternativeSignature*> alt : choice.alternatives()) {
|
||||
out << *alt << ";\n";
|
||||
out.indent(indent_num_spaces + 2) << *alt << ";\n";
|
||||
}
|
||||
out << "}\n";
|
||||
out.indent(indent_num_spaces) << "}";
|
||||
break;
|
||||
}
|
||||
|
||||
case DeclarationKind::VariableDeclaration: {
|
||||
const auto& var = cast<VariableDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << PrintAsID(var);
|
||||
if (var.has_initializer()) {
|
||||
out << " = " << var.initializer();
|
||||
}
|
||||
out << ";\n";
|
||||
out << ";";
|
||||
break;
|
||||
}
|
||||
|
||||
case DeclarationKind::InterfaceExtendDeclaration:
|
||||
case DeclarationKind::InterfaceRequireDeclaration:
|
||||
case DeclarationKind::AssociatedConstantDeclaration: {
|
||||
PrintID(out);
|
||||
out << ";\n";
|
||||
out << PrintAsID(*this) << ";";
|
||||
break;
|
||||
}
|
||||
|
||||
@@ -123,14 +128,12 @@ void Declaration::Print(llvm::raw_ostream& out) const {
|
||||
|
||||
case DeclarationKind::AliasDeclaration: {
|
||||
const auto& alias = cast<AliasDeclaration>(*this);
|
||||
PrintID(out);
|
||||
out << " = " << alias.target() << ";\n";
|
||||
out << PrintAsID(alias) << " = " << alias.target() << ";";
|
||||
break;
|
||||
}
|
||||
|
||||
case DeclarationKind::ExtendBaseDeclaration: {
|
||||
PrintID(out);
|
||||
out << ";\n";
|
||||
out << PrintAsID(*this) << ";";
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -406,10 +409,11 @@ auto FunctionDeclaration::Create(Nonnull<Arena*> arena,
|
||||
virt_override);
|
||||
}
|
||||
|
||||
void CallableDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
void CallableDeclaration::PrintIndent(int indent_num_spaces,
|
||||
llvm::raw_ostream& out) const {
|
||||
auto name = GetName(*this);
|
||||
CARBON_CHECK(name) << "Unexpected missing name for `" << *this << "`.";
|
||||
out << "fn " << *name << " ";
|
||||
out.indent(indent_num_spaces) << "fn " << *name << " ";
|
||||
if (!deduced_parameters_.empty()) {
|
||||
out << "[";
|
||||
llvm::ListSeparator sep;
|
||||
@@ -418,13 +422,15 @@ void CallableDeclaration::PrintDepth(int depth, llvm::raw_ostream& out) const {
|
||||
}
|
||||
out << "]";
|
||||
}
|
||||
out << *param_pattern_ << return_term_;
|
||||
out << *param_pattern_;
|
||||
if (!return_term_.is_omitted()) {
|
||||
out << " " << return_term_;
|
||||
}
|
||||
if (body_) {
|
||||
out << " {\n";
|
||||
(*body_)->PrintDepth(depth, out);
|
||||
out << "\n}\n";
|
||||
out << "\n";
|
||||
(*body_)->PrintIndent(indent_num_spaces, out);
|
||||
} else {
|
||||
out << ";\n";
|
||||
out << ";";
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user