aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorbors[bot] <26634292+bors[bot]@users.noreply.github.com>2022-10-07 18:17:39 +0000
committerGitHub <noreply@github.com>2022-10-07 18:17:39 +0000
commit06fe912b70bc21dd11e7279e29200431a5b50aa2 (patch)
treefe08a6cfbb346397ac237953cd7bc6a24340e052
parentbef31ea273689021f0fc8168f417edf386060512 (diff)
parent93937bd6582f3de40a7aefb0c639461a907b3fe1 (diff)
downloadgcc-06fe912b70bc21dd11e7279e29200431a5b50aa2.zip
gcc-06fe912b70bc21dd11e7279e29200431a5b50aa2.tar.gz
gcc-06fe912b70bc21dd11e7279e29200431a5b50aa2.tar.bz2
Merge #1572
1572: AST: dump structs, unions and enums r=dafaust a=dafaust Co-authored-by: David Faust <david.faust@oracle.com>
-rw-r--r--gcc/rust/ast/rust-ast-dump.cc182
-rw-r--r--gcc/rust/ast/rust-ast-dump.h9
2 files changed, 170 insertions, 21 deletions
diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc
index bc4f7a3..7cbdfa2 100644
--- a/gcc/rust/ast/rust-ast-dump.cc
+++ b/gcc/rust/ast/rust-ast-dump.cc
@@ -141,6 +141,39 @@ Dump::emit_indented_string (const std::string &value,
}
void
+Dump::emit_generic_params (std::vector<std::unique_ptr<GenericParam>> &params)
+{
+ stream << "<";
+ for (size_t i = 0; i < params.size (); i++)
+ {
+ auto &param = params.at (i);
+ param->accept_vis (*this);
+
+ bool has_next = (i + 1) < params.size ();
+ if (has_next)
+ stream << ", ";
+ }
+ stream << ">";
+}
+
+void
+Dump::format_tuple_field (TupleField &field)
+{
+ // TODO: do we need to emit outer attrs here?
+ emit_visibility (field.get_visibility ());
+ field.get_field_type ()->accept_vis (*this);
+}
+
+void
+Dump::format_struct_field (StructField &field)
+{
+ // TODO: do we need to emit outer attrs here?
+ emit_visibility (field.get_visibility ());
+ stream << field.get_field_name () << ": ";
+ field.get_field_type ()->accept_vis (*this);
+}
+
+void
Dump::visit (Token &tok)
{}
@@ -679,19 +712,7 @@ Dump::visit (Function &function)
stream << "fn " << function.get_function_name ();
if (function.has_generics ())
- {
- stream << "<";
- for (size_t i = 0; i < function.get_generic_params ().size (); i++)
- {
- auto &param = function.get_generic_params ().at (i);
- param->accept_vis (*this);
-
- bool has_next = (i + 1) < function.get_generic_params ().size ();
- if (has_next)
- stream << ", ";
- }
- stream << ">";
- }
+ emit_generic_params (function.get_generic_params ());
stream << '(';
auto &params = function.get_function_params ();
@@ -729,35 +750,154 @@ Dump::visit (TypeAlias &type_alias)
void
Dump::visit (StructStruct &struct_item)
-{}
+{
+ stream << "struct " << struct_item.get_identifier ();
+ if (struct_item.has_generics ())
+ emit_generic_params (struct_item.get_generic_params ());
+
+ // FIXME: where-clause
+
+ stream << " {";
+
+ auto &fields = struct_item.get_fields ();
+
+ indentation.increment ();
+ for (auto &field : fields)
+ {
+ stream << '\n' << indentation;
+ format_struct_field (field);
+ stream << ',';
+ }
+ indentation.decrement ();
+
+ if (fields.size () > 0)
+ stream << '\n' << indentation;
+ stream << "}\n";
+}
void
Dump::visit (TupleStruct &tuple_struct)
-{}
+{
+ stream << "struct " << tuple_struct.get_identifier ();
+ if (tuple_struct.has_generics ())
+ emit_generic_params (tuple_struct.get_generic_params ());
+
+ // FIXME: where-clause
+
+ stream << '(';
+
+ auto &fields = tuple_struct.get_fields ();
+ if (fields.size () >= 1)
+ {
+ format_tuple_field (fields[0]);
+ for (size_t i = 1; i < fields.size (); i++)
+ {
+ stream << ", ";
+ format_tuple_field (fields[i]);
+ }
+ }
+ stream << ");\n";
+}
void
Dump::visit (EnumItem &item)
-{}
+{
+ stream << item.get_identifier ();
+}
void
Dump::visit (EnumItemTuple &item)
-{}
+{
+ stream << item.get_identifier () << '(';
+ auto &fields = item.get_tuple_fields ();
+ if (fields.size () >= 1)
+ {
+ format_tuple_field (fields[0]);
+ for (size_t i = 1; i < fields.size (); i++)
+ {
+ stream << ", ";
+ format_tuple_field (fields[i]);
+ }
+ }
+ stream << ')';
+}
void
Dump::visit (EnumItemStruct &item)
-{}
+{
+ stream << item.get_identifier () << " {";
+
+ auto &fields = item.get_struct_fields ();
+
+ indentation.increment ();
+ for (auto &field : fields)
+ {
+ stream << '\n' << indentation;
+ format_struct_field (field);
+ stream << ',';
+ }
+ indentation.decrement ();
+
+ if (fields.size () > 0)
+ stream << '\n' << indentation;
+ stream << '}';
+}
void
Dump::visit (EnumItemDiscriminant &item)
-{}
+{
+ stream << item.get_identifier () << " = ";
+ item.get_expr ()->accept_vis (*this);
+}
void
Dump::visit (Enum &enum_item)
-{}
+{
+ stream << "enum " << enum_item.get_identifier ();
+ if (enum_item.has_generics ())
+ emit_generic_params (enum_item.get_generic_params ());
+
+ // FIXME: where-clause
+
+ stream << " {";
+ auto &variants = enum_item.get_variants ();
+ if (variants.size () >= 1)
+ {
+ stream << '\n';
+ indentation.increment ();
+ for (auto &var : variants)
+ {
+ stream << indentation;
+ var->accept_vis (*this);
+ stream << ",\n";
+ }
+ indentation.decrement ();
+ }
+
+ stream << "}\n";
+}
void
Dump::visit (Union &union_item)
-{}
+{
+ stream << "union " << union_item.get_identifier ();
+ if (union_item.has_generics ())
+ emit_generic_params (union_item.get_generic_params ());
+
+ // FIXME: where-clause
+
+ stream << " {";
+ indentation.increment ();
+ for (auto &field : union_item.get_variants ())
+ {
+ stream << '\n' << indentation;
+ format_struct_field (field);
+ stream << ',';
+ }
+ indentation.decrement ();
+
+ stream << '\n' << indentation << "}\n";
+}
void
Dump::visit (ConstantItem &const_item)
diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h
index a5a99f2..2da2736 100644
--- a/gcc/rust/ast/rust-ast-dump.h
+++ b/gcc/rust/ast/rust-ast-dump.h
@@ -97,6 +97,15 @@ private:
std::ostream &emit_indented_string (const std::string &value,
const std::string &comment = "");
+ // Emit formatted string for generic parameters.
+ void emit_generic_params (std::vector<std::unique_ptr<GenericParam>> &params);
+
+ // Format a single field of a tuple.
+ void format_tuple_field (TupleField &field);
+
+ // Format a single field of a struct.
+ void format_struct_field (StructField &field);
+
// rust-ast.h
void visit (Token &tok);
void visit (DelimTokenTree &delim_tok_tree);