-// Copyright (C) 2020-2023 Free Software Foundation, Inc.
+// Copyright (C) 2020-2022 Free Software Foundation, Inc.
// This file is part of GCC.
// <http://www.gnu.org/licenses/>.
#include "rust-ast-dump.h"
+#include "rust-diagnostics.h"
namespace Rust {
namespace AST {
{
stream << indentation;
item->accept_vis (*this);
- stream << "\n";
+ stream << '\n';
}
}
void
Dump::emit_attrib (const Attribute &attrib)
{
- stream << "#";
- stream << "[";
+ stream << "#[";
for (size_t i = 0; i < attrib.get_path ().get_segments ().size (); i++)
{
}
void
+Dump::emit_visibility (const Visibility &vis)
+{
+ switch (vis.get_vis_type ())
+ {
+ case Visibility::PUB:
+ stream << "pub ";
+ break;
+ case Visibility::PUB_CRATE:
+ stream << "pub(crate) ";
+ break;
+ case Visibility::PUB_SELF:
+ stream << "pub(self) ";
+ break;
+ case Visibility::PUB_SUPER:
+ stream << "pub(super) ";
+ break;
+ case Visibility::PUB_IN_PATH:
+ stream << "pub(in " << vis.get_path ().as_string () << ") ";
+ break;
+ case Visibility::PRIV:
+ break;
+ }
+}
+
+std::ostream &
+Dump::emit_indented_string (const std::string &value,
+ const std::string &comment)
+{
+ return stream << indentation << value << comment;
+}
+
+void
Dump::visit (Token &tok)
{}
// rust-path.h
void
Dump::visit (PathInExpression &path)
-{}
+{
+ stream << path.as_string ();
+}
void
Dump::visit (TypePathSegment &segment)
void
Dump::visit (QualifiedPathInExpression &path)
-{}
+{
+ stream << path.as_string ();
+}
void
Dump::visit (QualifiedPathInType &path)
void
Dump::visit (ArithmeticOrLogicalExpr &expr)
{
- expr.get_left_expr ()->accept_vis (*this);
- stream << " ";
-
+ auto op = "";
switch (expr.get_expr_type ())
{
case ArithmeticOrLogicalOperator::ADD:
- stream << "+";
+ op = "+";
break;
case ArithmeticOrLogicalOperator::SUBTRACT:
- stream << "-";
+ op = "-";
break;
case ArithmeticOrLogicalOperator::MULTIPLY:
- stream << "*";
+ op = "*";
break;
case ArithmeticOrLogicalOperator::DIVIDE:
- stream << "/";
+ op = "/";
break;
case ArithmeticOrLogicalOperator::MODULUS:
- stream << "%";
+ op = "%";
break;
case ArithmeticOrLogicalOperator::BITWISE_AND:
- stream << "&";
+ op = "&";
break;
case ArithmeticOrLogicalOperator::BITWISE_OR:
- stream << "|";
+ op = "|";
break;
case ArithmeticOrLogicalOperator::BITWISE_XOR:
- stream << "^";
+ op = "^";
break;
case ArithmeticOrLogicalOperator::LEFT_SHIFT:
- stream << "<<";
+ op = "<<";
break;
case ArithmeticOrLogicalOperator::RIGHT_SHIFT:
- stream << ">>";
+ op = ">>";
break;
}
- stream << " ";
+ expr.get_left_expr ()->accept_vis (*this);
+ stream << " " << op << " ";
expr.get_right_expr ()->accept_vis (*this);
}
void
Dump::visit (CallExpr &expr)
-{}
+{
+ expr.get_function_expr ()->accept_vis (*this);
+ stream << '(';
+
+ indentation.increment ();
+
+ for (auto &arg : expr.get_params ())
+ {
+ stream << '\n' << indentation;
+ arg->accept_vis (*this);
+ stream << ',';
+ }
+
+ indentation.decrement ();
+
+ stream << '\n' << indentation << ')';
+}
void
Dump::visit (MethodCallExpr &expr)
{
stream << indentation;
stmt->accept_vis (*this);
- stream << ";\n";
+ stream << "; /* stmt */\n";
}
if (expr.has_tail_expr ())
{
stream << indentation;
expr.get_tail_expr ()->accept_vis (*this);
+ stream << " /* tail expr */";
}
indentation.decrement ();
void
Dump::visit (Method &method)
{
- stream << indentation << "fn " << method.get_method_name () << '(';
+ // FIXME: Do we really need to dump the indentation here?
+ stream << indentation;
+ emit_visibility (method.get_visibility ());
+ stream << "fn " << method.get_method_name () << '(';
auto &self = method.get_self_param ();
stream << self.as_string ();
void
Dump::visit (Function &function)
{
+ emit_visibility (function.get_visibility ());
stream << "fn " << function.get_function_name ();
if (function.has_generics ())
Dump::format_function_common (std::unique_ptr<Type> &return_type,
std::unique_ptr<BlockExpr> &block)
{
+ // FIXME: This should format the `<vis> fn <name> ( [args] )` as well
if (return_type)
{
stream << "-> ";
if (block)
{
if (return_type)
- stream << ' ';
- block->accept_vis (*this);
+ {
+ stream << ' ';
+ block->accept_vis (*this);
+ }
}
else
stream << ";\n";
Dump::visit (TraitItemMethod &item)
{
auto method = item.get_trait_method_decl ();
- stream << indentation << "fn " << method.get_identifier () << '(';
+
+ // FIXME: Do we really need to dump the indentation here?
+ stream << indentation;
+
+ // FIXME: Can we have visibility here?
+ // emit_visibility (method.get_visibility ());
+ stream << "fn " << method.get_identifier () << '(';
auto &self = method.get_self_param ();
stream << self.as_string ();
stream << "\n" << indentation;
}
+ emit_visibility (trait.get_visibility ());
+
stream << "trait " << trait.get_identifier ();
// Traits actually have an implicit Self thrown at the start so we must expect
impl.get_trait_path ().accept_vis (*this);
stream << " for ";
impl.get_type ()->accept_vis (*this);
-
stream << " {\n";
+
indentation.increment ();
for (auto &item : impl.get_impl_items ())
- item->accept_vis (*this);
+ {
+ stream << indentation;
+ item->accept_vis (*this);
+ }
indentation.decrement ();
stream << "\n}\n";
void
Dump::visit (ExternalFunctionItem &function)
{
+ emit_visibility (function.get_visibility ());
+
stream << "fn " << function.get_identifier () << '(';
for (size_t i = 0; i < function.get_function_params ().size (); i++)
stream << "extern ";
if (block.has_abi ())
- {
- stream << "\"";
- stream << block.get_abi ();
- stream << "\" ";
- }
+ stream << "\"" << block.get_abi () << "\" ";
stream << "{\n";
indentation.increment ();
void
Dump::visit (ExprStmtWithoutBlock &stmt)
-{}
+{
+ stmt.get_expr ()->accept_vis (*this);
+}
void
Dump::visit (ExprStmtWithBlock &stmt)
-{}
+{
+ stmt.get_expr ()->accept_vis (*this);
+}
// rust-type.h
void