From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id CA0073858CDA; Sun, 5 Mar 2023 11:41:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org CA0073858CDA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1678016514; bh=NXMseIDwUAIwbZuElSgtyk8sO3NzwO5IlaSOG7X48Qk=; h=From:To:Subject:Date:From; b=V2fXgkaITWjwdaPnRqmmC0p6IPw9d+uuBLtyxpj8hxd9V/ceUqIcquPh9gmx2pfQZ gy8OMPMo2v01J25Zu2EJDsyCQK5PwAoJjN4B0+DWuDLaE9+YeTXrWTcwfF9KmSs9Qi XjJ19Dtwn8ZJTe9nwuInMUvfJMuUHQVDvH093fhw= 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] added support for printing HIR dump of functions, statements, arithematic/logical expressions and li X-Act-Checkin: gcc X-Git-Author: Abdul Rafey X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: 6d25dac7370289ef8e44f1f8370572aebac6e8e4 X-Git-Newrev: 724d3e824d6f9f9efb7847b0d53704bf7cb33811 Message-Id: <20230305114154.CA0073858CDA@sourceware.org> Date: Sun, 5 Mar 2023 11:41:54 +0000 (GMT) List-Id: https://gcc.gnu.org/g:724d3e824d6f9f9efb7847b0d53704bf7cb33811 commit 724d3e824d6f9f9efb7847b0d53704bf7cb33811 Author: Abdul Rafey Date: Mon Feb 27 17:31:31 2023 +0530 added support for printing HIR dump of functions, statements, arithematic/logical expressions and literals. gcc/rust/ChangeLog: * hir/rust-hir-dump.cc (Dump::go): support inner attrs, crate items and node mappings (Dump::visit): support functions, arith/logical exprs, let stmts and literals Signed-off-by: Abdul Rafey Diff: --- gcc/rust/hir/rust-hir-dump.cc | 271 +++++++++++++++++++++++++++++++++--------- 1 file changed, 215 insertions(+), 56 deletions(-) diff --git a/gcc/rust/hir/rust-hir-dump.cc b/gcc/rust/hir/rust-hir-dump.cc index 10720c43294..d6b458b72d3 100644 --- a/gcc/rust/hir/rust-hir-dump.cc +++ b/gcc/rust/hir/rust-hir-dump.cc @@ -26,31 +26,24 @@ Dump::Dump (std::ostream &stream) : stream (stream), indent (0) {} void Dump::go (HIR::Crate &crate) { - stream << "Crate" - << " " - << "{" << std::endl; - // - - indent++; - stream << std::string (indent, indent_char); - stream << "inner_attrs" - << ":" - << " " - << "["; - for (auto &attr : crate.inner_attrs) - stream << attr.as_string (); - stream << "]" - << "," << std::endl; - indent--; + stream << "Crate {" << std::endl; + // inner attributes + if (!crate.inner_attrs.empty ()) + { + indent++; + stream << std::string (indent, indent_char); + stream << "inner_attrs: ["; + for (auto &attr : crate.inner_attrs) + stream << attr.as_string (); + stream << "]," << std::endl; + indent--; + } indent++; stream << std::string (indent, indent_char); // - stream << "items" - << ":" - << " " - << "["; + stream << "items: ["; stream << std::string (indent, indent_char); for (const auto &item : crate.items) @@ -59,22 +52,17 @@ Dump::go (HIR::Crate &crate) item->accept_vis (*this); } stream << std::string (indent, indent_char); - stream << "]" - << "," << std::endl; + stream << "]," << std::endl; indent--; // indent++; stream << std::string (indent, indent_char); - stream << "node_mappings" - << ":" - << " " - << "["; - // TODO: print crate mapping attrs - stream << "]" << std::endl; + stream << "node_mappings: "; + stream << crate.get_mappings ().as_string (); indent--; - stream << "}" << std::endl; + stream << "\n}" << std::endl; } void @@ -108,11 +96,8 @@ Dump::visit (QualifiedPathInType &) void Dump::visit (LiteralExpr &literal_expr) { - indent++; - stream << std::string (indent, indent_char); - stream << "( " + literal_expr.get_literal ().as_string () + " (" - + literal_expr.get_mappings ().as_string () + "))"; - stream << "\n"; + stream << literal_expr.get_literal ().as_string () << " " + << literal_expr.get_mappings ().as_string (); } void Dump::visit (BorrowExpr &) @@ -127,8 +112,56 @@ void Dump::visit (NegationExpr &) {} void -Dump::visit (ArithmeticOrLogicalExpr &) -{} +Dump::visit (ArithmeticOrLogicalExpr &aole) +{ + std::string operator_str; + operator_str.reserve (1); + + // which operator + switch (aole.get_expr_type ()) + { + case ArithmeticOrLogicalOperator::ADD: + operator_str = "+"; + break; + case ArithmeticOrLogicalOperator::SUBTRACT: + operator_str = "-"; + break; + case ArithmeticOrLogicalOperator::MULTIPLY: + operator_str = "*"; + break; + case ArithmeticOrLogicalOperator::DIVIDE: + operator_str = "/"; + break; + case ArithmeticOrLogicalOperator::MODULUS: + operator_str = "%"; + break; + case ArithmeticOrLogicalOperator::BITWISE_AND: + operator_str = "&"; + break; + case ArithmeticOrLogicalOperator::BITWISE_OR: + operator_str = "|"; + break; + case ArithmeticOrLogicalOperator::BITWISE_XOR: + operator_str = "^"; + break; + case ArithmeticOrLogicalOperator::LEFT_SHIFT: + operator_str = "<<"; + break; + case ArithmeticOrLogicalOperator::RIGHT_SHIFT: + operator_str = ">>"; + break; + default: + gcc_unreachable (); + break; + } + + aole.visit_lhs (*this); + stream << "\n"; + stream << std::string (indent, indent_char); + stream << operator_str << "\n"; + stream << std::string (indent, indent_char); + aole.visit_rhs (*this); +} void Dump::visit (ComparisonExpr &) {} @@ -200,17 +233,34 @@ void Dump::visit (ClosureExpr &) {} void -Dump::visit (BlockExpr &) +Dump::visit (BlockExpr &block_expr) { - stream << "BlockExpr" - << ":" - << " " - << "["; + stream << "BlockExpr: ["; indent++; - // TODO: print statements - // TODO: print tail expression if exists - stream << "]"; + stream << std::endl; + // TODO: inner attributes + + // statements + if (block_expr.has_statements ()) + { + auto &stmts = block_expr.get_statements (); + for (auto &stmt : stmts) + { + stream << std::string (indent, indent_char); + stream << "Stmt: {\n"; + // stream << std::string (indent, indent_char); + stmt->accept_vis (*this); + stream << "\n"; + stream << std::string (indent, indent_char); + stream << "}\n"; + } + } + + // // TODO: print tail expression if exists + indent--; + stream << std::string (indent, indent_char); + stream << "]"; } void @@ -324,19 +374,87 @@ void Dump::visit (UseDeclaration &) {} void -Dump::visit (Function &) +Dump::visit (Function &func) { indent++; stream << std::string (indent, indent_char); - stream << "Function" - << " "; - stream << "{" << std::endl; - // TODO: print function params + stream << "Function {" << std::endl; + indent++; + + // function name + stream << std::string (indent, indent_char); + stream << "func_name: "; + auto func_name = func.get_function_name (); + stream << func_name; + stream << ",\n"; + + // return type + stream << std::string (indent, indent_char); + stream << "return_type: "; + if (func.has_return_type ()) + { + auto &ret_type = func.get_return_type (); + stream << ret_type->as_string (); + stream << ",\n"; + } + else + { + stream << "void,\n"; + } + + // function params + if (func.has_function_params ()) + { + stream << std::string (indent, indent_char); + stream << "params: [\n"; + indent++; + auto &func_params = func.get_function_params (); + for (const auto &item : func_params) + { + stream << std::string (indent, indent_char); + stream << item.as_string (); + stream << ",\n"; + } + + // parameter node mappings + stream << std::string (indent, indent_char); + stream << "node_mappings: [\n"; + for (const auto &item : func_params) + { + auto nmap = item.get_mappings (); + indent++; + stream << std::string (indent, indent_char); + auto pname = item.param_name->as_string (); + stream << pname << ": "; + stream << nmap.as_string () << ",\n"; + indent--; + } + stream << std::string (indent, indent_char); + stream << "],"; + indent--; + stream << "\n"; + stream << std::string (indent, indent_char); + stream << "],"; + stream << "\n"; + } + + // function body + stream << std::string (indent, indent_char); + auto &func_body = func.get_definition (); + func_body->accept_vis (*this); + + // func node mappings + stream << "\n"; + stream << std::string (indent, indent_char); + stream << "node_mappings: "; + stream << func.get_impl_mappings ().as_string (); + indent--; + stream << "\n"; stream << std::string (indent, indent_char); stream << "}" << std::endl; // TODO: get function definition and visit block - stream << std::endl; + // stream << std::endl; indent--; } void @@ -402,8 +520,11 @@ void Dump::visit (LiteralPattern &) {} void -Dump::visit (IdentifierPattern &) -{} +Dump::visit (IdentifierPattern &ident) +{ + auto ident_name = ident.get_identifier (); + stream << ident_name; +} void Dump::visit (WildcardPattern &) {} @@ -464,11 +585,49 @@ void Dump::visit (EmptyStmt &) {} void -Dump::visit (LetStmt &) -{} +Dump::visit (LetStmt &let_stmt) +{ + indent++; + // TODO: outer attributes + stream << std::string (indent, indent_char); + stream << "LetStmt: {\n"; + indent++; + stream << std::string (indent, indent_char); + + auto var_pattern = let_stmt.get_pattern (); + stream << var_pattern->as_string (); + // return type + if (let_stmt.has_type ()) + { + auto ret_type = let_stmt.get_type (); + stream << ": " << ret_type->as_string (); + } + + // init expr + if (let_stmt.has_init_expr ()) + { + stream << " = Expr: {\n "; + indent++; + stream << std::string (indent, indent_char); + auto expr = let_stmt.get_init_expr (); + expr->accept_vis (*this); + stream << "\n"; + stream << std::string (indent, indent_char); + indent--; + stream << "}\n"; + } + indent--; + stream << std::string (indent, indent_char); + stream << "}\n"; + + indent--; +} void -Dump::visit (ExprStmtWithoutBlock &) -{} +Dump::visit (ExprStmtWithoutBlock &expr_stmt) +{ + auto expr = expr_stmt.get_expr (); + expr->accept_vis (*this); +} void Dump::visit (ExprStmtWithBlock &) {}