From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 100553858C33; Tue, 14 Mar 2023 11:44:25 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 100553858C33 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678794265; bh=QQxU3xKC4FU2a3T82nAg4RxfX4ydDPRAG16CulWV3FE=; h=From:To:Subject:Date:From; b=PFkExroC1OMvQgBHigjqnPd+bkjMCe+DyvFEKN6HDhLXiv/19GEPaO0PkWzGdKfwM flQRCSzIYek7WQ3pWNCw9595ms0XGy8H4VZS8Chu6WrXRplPvjzV0+OS6QJv4G3MI8 0bWtxLlxTKJr6ZwAV6DrPvDsNJoIYCWvbyFGXHOI= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] hir: Unify indentation approach with ast X-Act-Checkin: gcc X-Git-Author: Jakub Dupak X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: f31fc7b0f6fb22e84a27dc68f69cb0fca9707879 X-Git-Newrev: 048971cf4284e8eaf71060d3136495b1111793d9 Message-Id: <20230314114425.100553858C33@sourceware.org> Date: Tue, 14 Mar 2023 11:44:25 +0000 (GMT) List-Id: https://gcc.gnu.org/g:048971cf4284e8eaf71060d3136495b1111793d9 commit 048971cf4284e8eaf71060d3136495b1111793d9 Author: Jakub Dupak Date: Sat Mar 11 20:36:11 2023 +0100 hir: Unify indentation approach with ast gcc/rust/ChangeLog: * ast/rust-ast-dump.cc (Indent::Indent): Move to separate file. (operator<<): Move to separate file. (Indent::increment): Move to separate file. (Indent::decrement): Move to separate file. * ast/rust-ast-dump.h (class Indent): Move to separate file. * hir/rust-hir-dump.cc (Dump::Dump): Use new indentation object. (Dump::go): Use new indentation object. (Dump::visit): Use new indention object. * hir/rust-hir-dump.h: Use new indentation object. * util/rust-dump.h: New file. Moved Indentation from rust-ast-dump.cc Signed-off-by: Jakub Dupak Diff: --- gcc/rust/ast/rust-ast-dump.cc | 21 --------- gcc/rust/ast/rust-ast-dump.h | 16 +------ gcc/rust/hir/rust-hir-dump.cc | 102 +++++++++++++++++++++--------------------- gcc/rust/hir/rust-hir-dump.h | 4 +- gcc/rust/util/rust-dump.h | 49 ++++++++++++++++++++ 5 files changed, 103 insertions(+), 89 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 9315bc5292d..3792f960726 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -21,27 +21,6 @@ namespace Rust { namespace AST { -Indent::Indent () : tabs (0) {} - -std::ostream & -operator<< (std::ostream &stream, const Indent &indent) -{ - return stream << std::string (indent.tabs, '\t'); -} - -void -Indent::increment () -{ - tabs++; -} - -void -Indent::decrement () -{ - rust_assert (tabs != 0); - tabs--; -} - Dump::Dump (std::ostream &stream) : stream (stream), indentation (Indent ()) {} void diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index b68619d2862..4b76ec1a484 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -19,6 +19,7 @@ #include "rust-ast-visitor.h" #include "rust-ast.h" #include "rust-ast-full.h" +#include "rust-dump.h" #ifndef RUST_AST_DUMP_H #define RUST_AST_DUMP_H @@ -26,21 +27,6 @@ namespace Rust { namespace AST { -// TODO: We might want to reuse this class somewhere else -class Indent -{ -public: - Indent (); - - friend std::ostream &operator<< (std::ostream &stream, const Indent &indent); - - void increment (); - void decrement (); - -private: - size_t tabs; -}; - class Dump : public ASTVisitor { public: diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc index 9975a1e66b1..52c17b8a979 100644 --- a/gcc/rust/hir/rust-hir-dump.cc +++ b/gcc/rust/hir/rust-hir-dump.cc @@ -21,7 +21,7 @@ namespace Rust { namespace HIR { -Dump::Dump (std::ostream &stream) : stream (stream), indent (0) {} +Dump::Dump (std::ostream &stream) : stream (stream) {} void Dump::go (HIR::Crate &crate) @@ -30,37 +30,37 @@ Dump::go (HIR::Crate &crate) // inner attributes if (!crate.inner_attrs.empty ()) { - indent++; - stream << std::string (indent, indent_char); + indentation.increment(); + stream << indentation; stream << "inner_attrs: ["; for (auto &attr : crate.inner_attrs) stream << attr.as_string (); stream << "]," << std::endl; - indent--; + indentation.decrement(); } - indent++; - stream << std::string (indent, indent_char); + indentation.increment(); + stream << indentation; // stream << "items: ["; - stream << std::string (indent, indent_char); + stream << indentation; for (const auto &item : crate.items) { stream << std::endl; item->accept_vis (*this); } - stream << std::string (indent, indent_char); + stream << indentation; stream << "]," << std::endl; - indent--; + indentation.decrement(); // - indent++; - stream << std::string (indent, indent_char); + indentation.increment(); + stream << indentation; stream << "node_mappings: "; stream << crate.get_mappings ().as_string (); - indent--; + indentation.decrement(); stream << "\n}" << std::endl; } @@ -157,9 +157,9 @@ Dump::visit (ArithmeticOrLogicalExpr &aole) aole.visit_lhs (*this); stream << "\n"; - stream << std::string (indent, indent_char); + stream << indentation; stream << operator_str << "\n"; - stream << std::string (indent, indent_char); + stream << indentation; aole.visit_rhs (*this); } void @@ -236,7 +236,7 @@ void Dump::visit (BlockExpr &block_expr) { stream << "BlockExpr: ["; - indent++; + indentation.increment(); stream << std::endl; // TODO: inner attributes @@ -246,20 +246,20 @@ Dump::visit (BlockExpr &block_expr) auto &stmts = block_expr.get_statements (); for (auto &stmt : stmts) { - stream << std::string (indent, indent_char); + stream << indentation; stream << "Stmt: {\n"; - // stream << std::string (indent, indent_char); + // stream << indentation; stmt->accept_vis (*this); stream << "\n"; - stream << std::string (indent, indent_char); + stream << indentation; stream << "}\n"; } } // // TODO: print tail expression if exists - indent--; - stream << std::string (indent, indent_char); + indentation.decrement(); + stream << indentation; stream << "]"; } @@ -376,20 +376,20 @@ Dump::visit (UseDeclaration &) void Dump::visit (Function &func) { - indent++; - stream << std::string (indent, indent_char); + indentation.increment(); + stream << indentation; stream << "Function {" << std::endl; - indent++; + indentation.increment(); // function name - stream << std::string (indent, indent_char); + stream << indentation; stream << "func_name: "; auto func_name = func.get_function_name (); stream << func_name; stream << ",\n"; // return type - stream << std::string (indent, indent_char); + stream << indentation; stream << "return_type: "; if (func.has_return_type ()) { @@ -405,57 +405,57 @@ Dump::visit (Function &func) // function params if (func.has_function_params ()) { - stream << std::string (indent, indent_char); + stream << indentation; stream << "params: [\n"; - indent++; + indentation.increment(); auto &func_params = func.get_function_params (); for (const auto &item : func_params) { - stream << std::string (indent, indent_char); + stream << indentation; stream << item.as_string (); stream << ",\n"; } // parameter node mappings - stream << std::string (indent, indent_char); + stream << indentation; stream << "node_mappings: [\n"; for (const auto &item : func_params) { auto nmap = item.get_mappings (); - indent++; - stream << std::string (indent, indent_char); + indentation.increment(); + stream << indentation; auto pname = item.param_name->as_string (); stream << pname << ": "; stream << nmap.as_string () << ",\n"; - indent--; + indentation.decrement(); } - stream << std::string (indent, indent_char); + stream << indentation; stream << "],"; - indent--; + indentation.decrement(); stream << "\n"; - stream << std::string (indent, indent_char); + stream << indentation; stream << "],"; stream << "\n"; } // function body - stream << std::string (indent, indent_char); + stream << indentation; auto &func_body = func.get_definition (); func_body->accept_vis (*this); // func node mappings stream << "\n"; - stream << std::string (indent, indent_char); + stream << indentation; stream << "node_mappings: "; stream << func.get_impl_mappings ().as_string (); - indent--; + indentation.decrement(); stream << "\n"; - stream << std::string (indent, indent_char); + stream << indentation; stream << "}" << std::endl; // TODO: get function definition and visit block // stream << std::endl; - indent--; + indentation.decrement(); } void Dump::visit (TypeAlias &) @@ -590,12 +590,12 @@ Dump::visit (EmptyStmt &) void Dump::visit (LetStmt &let_stmt) { - indent++; + indentation.increment(); // TODO: outer attributes - stream << std::string (indent, indent_char); + stream << indentation; stream << "LetStmt: {\n"; - indent++; - stream << std::string (indent, indent_char); + indentation.increment(); + stream << indentation; auto var_pattern = let_stmt.get_pattern (); stream << var_pattern->as_string (); @@ -610,20 +610,20 @@ Dump::visit (LetStmt &let_stmt) if (let_stmt.has_init_expr ()) { stream << " = Expr: {\n "; - indent++; - stream << std::string (indent, indent_char); + indentation.increment(); + stream << indentation; auto expr = let_stmt.get_init_expr (); expr->accept_vis (*this); stream << "\n"; - stream << std::string (indent, indent_char); - indent--; + stream << indentation; + indentation.decrement(); stream << "}\n"; } - indent--; - stream << std::string (indent, indent_char); + indentation.decrement(); + stream << indentation; stream << "}\n"; - indent--; + indentation.decrement(); } void Dump::visit (ExprStmtWithoutBlock &expr_stmt) diff --git a/gcc/rust/hir/rust-hir-dump.h b/gcc/rust/hir/rust-hir-dump.h index c1ef9849b46..3f42d048135 100644 --- a/gcc/rust/hir/rust-hir-dump.h +++ b/gcc/rust/hir/rust-hir-dump.h @@ -22,6 +22,7 @@ #include "rust-hir-visitor.h" #include "rust-hir.h" #include "rust-hir-full.h" +#include "rust-dump.h" namespace Rust { namespace HIR { @@ -33,9 +34,8 @@ public: void go (HIR::Crate &crate); private: + Indent indentation; std::ostream &stream; - std::size_t indent; // current indentation level - char indent_char = '\t'; virtual void visit (Lifetime &) override; virtual void visit (LifetimeParam &) override; diff --git a/gcc/rust/util/rust-dump.h b/gcc/rust/util/rust-dump.h new file mode 100644 index 00000000000..7fd0b365b9f --- /dev/null +++ b/gcc/rust/util/rust-dump.h @@ -0,0 +1,49 @@ +// Copyright (C) 2021-2023 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +// Common definitions useful for textual dump of IRs (AST and HIR). + +#ifndef RUST_DUMP_H +#define RUST_DUMP_H + +namespace Rust { + +class Indent +{ +public: + Indent () = default; + + friend std::ostream &operator<< (std::ostream &stream, const Indent &indent) + { + return stream << std::string (indent.tabs, '\t'); + }; + + void increment () { tabs++; }; + + void decrement () + { + rust_assert (tabs != 0); + tabs--; + }; + +private: + size_t tabs = 0; +}; +} // namespace Rust + +#endif