aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2022-07-04 16:56:24 +0100
committerPhilip Herron <philip.herron@embecosm.com>2022-07-06 17:38:37 +0100
commit9507bdec30fb7f88a02765f83f29cce52a5cf8ad (patch)
tree8938c8193f1be566506eb871cdbfa7fe514365e0
parent29d594e263f0ccbcbd2babf43ff453c5188f4f2c (diff)
downloadgcc-9507bdec30fb7f88a02765f83f29cce52a5cf8ad.zip
gcc-9507bdec30fb7f88a02765f83f29cce52a5cf8ad.tar.gz
gcc-9507bdec30fb7f88a02765f83f29cce52a5cf8ad.tar.bz2
Support extern-blocks in ast-dumps
This allows us to support really basic expressions and extern blocks. These are used for the hello world version of importing metadata in crates.
-rw-r--r--gcc/rust/ast/rust-ast-dump.cc87
-rw-r--r--gcc/rust/ast/rust-ast-dump.h1
2 files changed, 81 insertions, 7 deletions
diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc
index 6bf2bee..a06dd72 100644
--- a/gcc/rust/ast/rust-ast-dump.cc
+++ b/gcc/rust/ast/rust-ast-dump.cc
@@ -52,6 +52,12 @@ Dump::go (AST::Crate &crate)
}
void
+Dump::go (AST::Item &item)
+{
+ item.accept_vis (*this);
+}
+
+void
Dump::format_function_param (FunctionParam &param)
{
param.get_pattern ()->accept_vis (*this);
@@ -73,7 +79,9 @@ Dump::visit (AttrInputMetaItemContainer &input)
void
Dump::visit (IdentifierExpr &ident_expr)
-{}
+{
+ stream << ident_expr.get_ident ();
+}
void
Dump::visit (Lifetime &lifetime)
@@ -121,7 +129,9 @@ Dump::visit (QualifiedPathInType &path)
// rust-expr.h
void
Dump::visit (LiteralExpr &expr)
-{}
+{
+ stream << expr.as_string ();
+}
void
Dump::visit (AttrInputLiteral &attr_input)
@@ -153,7 +163,24 @@ Dump::visit (NegationExpr &expr)
void
Dump::visit (ArithmeticOrLogicalExpr &expr)
-{}
+{
+ expr.get_left_expr ()->accept_vis (*this);
+ stream << " ";
+
+ switch (expr.get_expr_type ())
+ {
+ case ArithmeticOrLogicalOperator::ADD:
+ stream << "+";
+ break;
+
+ default:
+ gcc_unreachable ();
+ break;
+ }
+
+ stream << " ";
+ expr.get_right_expr ()->accept_vis (*this);
+}
void
Dump::visit (ComparisonExpr &expr)
@@ -257,7 +284,10 @@ Dump::visit (BlockExpr &expr)
}
if (expr.has_tail_expr ())
- expr.get_tail_expr ()->accept_vis (*this);
+ {
+ stream << indentation;
+ expr.get_tail_expr ()->accept_vis (*this);
+ }
indentation.decrement ();
stream << "\n" << indentation << "}\n";
@@ -649,12 +679,55 @@ Dump::visit (ExternalStaticItem &item)
{}
void
-Dump::visit (ExternalFunctionItem &item)
-{}
+Dump::visit (ExternalFunctionItem &function)
+{
+ stream << "fn " << function.get_identifier () << '(';
+
+ for (size_t i = 0; i < function.get_function_params ().size (); i++)
+ {
+ auto &param = function.get_function_params ().at (i);
+ bool has_next = (i + 1) < function.get_function_params ().size ();
+
+ stream << param.get_name () << ": ";
+ param.get_type ()->accept_vis (*this);
+
+ if (has_next)
+ stream << ", ";
+ }
+
+ stream << ')';
+ if (function.has_return_type ())
+ {
+ stream << "-> ";
+ function.get_return_type ()->accept_vis (*this);
+ }
+}
void
Dump::visit (ExternBlock &block)
-{}
+{
+ stream << "extern ";
+
+ if (block.has_abi ())
+ {
+ stream << "\"";
+ stream << block.get_abi ();
+ stream << "\" ";
+ }
+
+ stream << "{\n";
+ indentation.increment ();
+
+ for (auto &item : block.get_extern_items ())
+ {
+ stream << indentation;
+ item->accept_vis (*this);
+ stream << ";\n";
+ }
+
+ indentation.decrement ();
+ stream << "\n" << indentation << "}\n";
+}
// rust-macro.h
void
diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h
index 51c6f84..d74d887 100644
--- a/gcc/rust/ast/rust-ast-dump.h
+++ b/gcc/rust/ast/rust-ast-dump.h
@@ -50,6 +50,7 @@ public:
* Run the visitor on an entire crate and its items
*/
void go (AST::Crate &crate);
+ void go (AST::Item &item);
private:
std::ostream &stream;