public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Add multiple formatting stub functions
@ 2023-04-06 21:34 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2023-04-06 21:34 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:f580c1f44c47b7b06d2a87ba4dfb3384082cc881

commit f580c1f44c47b7b06d2a87ba4dfb3384082cc881
Author: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>
Date:   Thu Mar 23 20:30:53 2023 +0100

    ast: Add multiple formatting stub functions
    
    Add multiple stub functions depending on the context. This will allow an
    easier unification of ast dump.
    
    gcc/rust/ChangeLog:
    
            * ast/rust-ast-tokenstream.cc (TokenStream::visit_as_line): Add
            formatting calls.
            (TokenStream::visit_items_as_block): Likewise.
            (TokenStream::newline): Add stub.
            (TokenStream::indentation): Add stub.
            (TokenStream::increment_indentation): Add stub.
            (TokenStream::decrement_indentation): Add stub.
            (TokenStream::visit): Add formatting calls.
            (TokenStream::visit_function_common): Likewise.
            * ast/rust-ast-tokenstream.h: Add stub prototypes.
    
    Signed-off-by: Pierre-Emmanuel Patry <pierre-emmanuel.patry@embecosm.com>

Diff:
---
 gcc/rust/ast/rust-ast-tokenstream.cc | 97 +++++++++++++++++++++++++++++++++---
 gcc/rust/ast/rust-ast-tokenstream.h  |  4 ++
 2 files changed, 95 insertions(+), 6 deletions(-)

diff --git a/gcc/rust/ast/rust-ast-tokenstream.cc b/gcc/rust/ast/rust-ast-tokenstream.cc
index ca64dd21d47..3150baaf1cc 100644
--- a/gcc/rust/ast/rust-ast-tokenstream.cc
+++ b/gcc/rust/ast/rust-ast-tokenstream.cc
@@ -67,9 +67,11 @@ template <typename T>
 void
 TokenStream::visit_as_line (T &item, std::vector<TokenPtr> trailing)
 {
+  indentation ();
   visit (item);
   for (auto &token : trailing)
     tokens.push_back (token);
+  newline ();
 }
 
 template <typename T>
@@ -88,8 +90,21 @@ TokenStream::visit_items_as_block (T &collection,
 				   TokenId left_brace, TokenId right_brace)
 {
   tokens.push_back (Rust::Token::make (left_brace, Location ()));
-  visit_items_as_lines (collection);
-  tokens.push_back (Rust::Token::make (right_brace, Location ()));
+  if (collection.empty ())
+    {
+      tokens.push_back (Rust::Token::make (right_brace, Location ()));
+      newline ();
+    }
+  else
+    {
+      newline ();
+      increment_indentation ();
+      visit_items_as_lines (collection);
+      decrement_indentation ();
+      indentation ();
+      tokens.push_back (Rust::Token::make (right_brace, Location ()));
+      newline ();
+    }
 }
 
 void
@@ -101,6 +116,22 @@ TokenStream::trailing_comma ()
     }
 }
 
+void
+TokenStream::newline ()
+{}
+
+void
+TokenStream::indentation ()
+{}
+
+void
+TokenStream::increment_indentation ()
+{}
+
+void
+TokenStream::decrement_indentation ()
+{}
+
 void
 TokenStream::visit (FunctionParam &param)
 {
@@ -350,10 +381,16 @@ TokenStream::visit (Token &tok)
 void
 TokenStream::visit (DelimTokenTree &delim_tok_tree)
 {
+  increment_indentation ();
+  newline ();
+  indentation ();
   for (auto &token : delim_tok_tree.to_token_stream ())
     {
       visit (token);
     }
+  decrement_indentation ();
+  newline ();
+  indentation ();
 }
 
 void
@@ -1155,13 +1192,21 @@ void
 TokenStream::visit (BlockExpr &expr)
 {
   tokens.push_back (Rust::Token::make (LEFT_CURLY, expr.get_locus ()));
+  newline ();
+  increment_indentation ();
 
-  visit_items_joined_by_separator (expr.get_statements (), SEMICOLON);
+  visit_items_as_lines (expr.get_statements (),
+			{Rust::Token::make (SEMICOLON, Location ())});
 
   if (expr.has_tail_expr ())
-    visit (expr.get_tail_expr ());
+    {
+      visit (expr.get_tail_expr ());
+      newline ();
+    }
 
+  decrement_indentation ();
   tokens.push_back (Rust::Token::make (RIGHT_CURLY, expr.get_locus ()));
+  newline ();
 }
 
 void
@@ -1319,6 +1364,7 @@ void
 TokenStream::visit (IfExprConseqElse &expr)
 {
   visit (static_cast<IfExpr &> (expr));
+  indentation ();
   tokens.push_back (Rust::Token::make (ELSE, expr.get_locus ()));
   visit (expr.get_else_block ());
 }
@@ -1327,6 +1373,7 @@ void
 TokenStream::visit (IfExprConseqIf &expr)
 {
   visit (static_cast<IfExpr &> (expr));
+  indentation ();
   tokens.push_back (Rust::Token::make (ELSE, expr.get_locus ()));
   // The "if" part of the "else if" is printed by the next visitor
   visit (expr.get_conseq_if_expr ());
@@ -1336,6 +1383,7 @@ void
 TokenStream::visit (IfExprConseqIfLet &expr)
 {
   visit (static_cast<IfExpr &> (expr));
+  indentation ();
   tokens.push_back (Rust::Token::make (ELSE, expr.get_locus ()));
   visit (expr.get_conseq_if_let_expr ());
 }
@@ -1358,6 +1406,7 @@ void
 TokenStream::visit (IfLetExprConseqElse &expr)
 {
   visit (static_cast<IfLetExpr &> (expr));
+  indentation ();
   tokens.push_back (Rust::Token::make (ELSE, expr.get_locus ()));
   visit (expr.get_else_block ());
 }
@@ -1366,6 +1415,7 @@ void
 TokenStream::visit (IfLetExprConseqIf &expr)
 {
   visit (static_cast<IfLetExpr &> (expr));
+  indentation ();
   tokens.push_back (Rust::Token::make (ELSE, expr.get_locus ()));
   visit (expr.get_conseq_if_expr ());
 }
@@ -1374,6 +1424,7 @@ void
 TokenStream::visit (IfLetExprConseqIfLet &expr)
 {
   visit (static_cast<IfLetExpr &> (expr));
+  indentation ();
   tokens.push_back (Rust::Token::make (ELSE, expr.get_locus ()));
   visit (expr.get_conseq_if_let_expr ());
 }
@@ -1469,7 +1520,10 @@ TokenStream::visit (WhereClause &rule)
   //  	| TypeBoundWhereClauseItem
 
   tokens.push_back (Rust::Token::make (WHERE, Location ()));
+  newline ();
+  increment_indentation ();
   visit_items_joined_by_separator (rule.get_items (), COMMA);
+  decrement_indentation ();
 }
 
 void
@@ -1534,6 +1588,7 @@ TokenStream::visit (Method &method)
     tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
   else
     visit (block);
+  newline ();
 }
 
 void
@@ -1555,15 +1610,21 @@ TokenStream::visit (Module &module)
   if (module.get_kind () == Module::UNLOADED)
     {
       tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+      newline ();
     }
   else /* Module::LOADED */
     {
       tokens.push_back (Rust::Token::make (LEFT_CURLY, Location ()));
+      newline ();
+      increment_indentation ();
 
       visit_items_as_lines (module.get_inner_attrs ());
       visit_items_as_lines (module.get_items ());
 
+      decrement_indentation ();
+
       tokens.push_back (Rust::Token::make (RIGHT_CURLY, Location ()));
+      newline ();
     }
 }
 
@@ -1583,6 +1644,7 @@ TokenStream::visit (ExternCrate &crate)
 	Rust::Token::make_identifier (Location (), std::move (as_clause)));
     }
   tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+  newline ();
 }
 
 void
@@ -1660,6 +1722,7 @@ TokenStream::visit (UseDeclaration &decl)
   tokens.push_back (Rust::Token::make (USE, decl.get_locus ()));
   visit (*decl.get_tree ());
   tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+  newline ();
 }
 
 void
@@ -1698,6 +1761,7 @@ TokenStream::visit (Function &function)
     tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
   else
     visit (block);
+  newline ();
 }
 
 void
@@ -1735,7 +1799,10 @@ TokenStream::visit (StructStruct &struct_item)
   if (struct_item.has_where_clause ())
     visit (struct_item.get_where_clause ());
   if (struct_item.is_unit_struct ())
-    tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+    {
+      tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+      newline ();
+    }
   else
     visit_items_as_block (struct_item.get_fields (),
 			  {Rust::Token::make (COMMA, Location ())});
@@ -1757,6 +1824,7 @@ TokenStream::visit (TupleStruct &tuple_struct)
   visit_items_joined_by_separator (tuple_struct.get_fields (), COMMA);
   tokens.push_back (Rust::Token::make (RIGHT_PAREN, Location ()));
   tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+  newline ();
 }
 
 void
@@ -1893,7 +1961,10 @@ TokenStream::visit_function_common (std::unique_ptr<Type> &return_type,
 	}
     }
   else
-    tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+    {
+      tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+      newline ();
+    }
 }
 
 void
@@ -1960,20 +2031,24 @@ void
 TokenStream::visit (TraitItemConst &item)
 {
   auto id = item.get_identifier ();
+  indentation ();
   tokens.push_back (Rust::Token::make (CONST, item.get_locus ()));
   tokens.push_back (Rust::Token::make_identifier (Location (), std::move (id)));
   tokens.push_back (Rust::Token::make (COLON, Location ()));
   visit (item.get_type ());
   tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+  newline ();
 }
 
 void
 TokenStream::visit (TraitItemType &item)
 {
   auto id = item.get_identifier ();
+  indentation ();
   tokens.push_back (Rust::Token::make (TYPE, item.get_locus ()));
   tokens.push_back (Rust::Token::make_identifier (Location (), std::move (id)));
   tokens.push_back (Rust::Token::make (SEMICOLON, Location ()));
+  newline ();
 }
 
 void
@@ -1982,6 +2057,8 @@ TokenStream::visit (Trait &trait)
   for (auto &attr : trait.get_outer_attrs ())
     {
       visit (attr);
+      newline ();
+      indentation ();
     }
 
   visit (trait.get_visibility ());
@@ -2026,13 +2103,21 @@ TokenStream::visit (TraitImpl &impl)
   tokens.push_back (Rust::Token::make (FOR, Location ()));
   visit (impl.get_type ());
   tokens.push_back (Rust::Token::make (LEFT_CURLY, Location ()));
+  newline ();
+
+  increment_indentation ();
 
   for (auto &item : impl.get_impl_items ())
     {
+      indentation ();
       visit (item);
     }
 
+  decrement_indentation ();
+  newline ();
+
   tokens.push_back (Rust::Token::make (RIGHT_CURLY, Location ()));
+  newline ();
 }
 
 void
diff --git a/gcc/rust/ast/rust-ast-tokenstream.h b/gcc/rust/ast/rust-ast-tokenstream.h
index 1c9328e7dbe..29be91eb1d9 100644
--- a/gcc/rust/ast/rust-ast-tokenstream.h
+++ b/gcc/rust/ast/rust-ast-tokenstream.h
@@ -88,6 +88,10 @@ private:
 			     TokenId right_brace = RIGHT_CURLY);
 
   void trailing_comma ();
+  void newline ();
+  void indentation ();
+  void increment_indentation ();
+  void decrement_indentation ();
   /**
    * Visit common items of functions: Parameters, return type, block
    */

^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-04-06 21:34 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-04-06 21:34 [gcc/devel/rust/master] ast: Add multiple formatting stub functions Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).