public inbox for gcc-rust@gcc.gnu.org
 help / color / mirror / Atom feed
From: arthur.cohen@embecosm.com
To: gcc-patches@gcc.gnu.org
Cc: gcc-rust@gcc.gnu.org, Kushal Pal <kushalpal109@gmail.com>
Subject: [COMMITTED 23/25] gccrs: Parse trait functions as `AST::Function`.
Date: Wed,  7 Feb 2024 12:44:09 +0100	[thread overview]
Message-ID: <20240207114419.1100894-24-arthur.cohen@embecosm.com> (raw)
In-Reply-To: <20240207114419.1100894-2-arthur.cohen@embecosm.com>

From: Kushal Pal <kushalpal109@gmail.com>

To use AST::Function for trait functions, we can parse trait functions
using available parse_function().

gcc/rust/ChangeLog:

	* parse/rust-parse-impl.h (Parser::parse_trait_item):
	Use parse_function() to parse trait functions.

Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
---
 gcc/rust/parse/rust-parse-impl.h | 127 +------------------------------
 1 file changed, 2 insertions(+), 125 deletions(-)

diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index a7de948006b..ed264371db7 100644
--- a/gcc/rust/parse/rust-parse-impl.h
+++ b/gcc/rust/parse/rust-parse-impl.h
@@ -5108,132 +5108,9 @@ Parser<ManagedTokenSource>::parse_trait_item ()
     case ASYNC:
     case UNSAFE:
     case EXTERN_KW:
-      case FN_KW: {
-	/* function and method can't be disambiguated by lookahead alone
-	 * (without a lot of work and waste), so either make a
-	 * "parse_trait_function_or_method" or parse here mostly and pass in
-	 * most parameters (or if short enough, parse whole thing here). */
-	// parse function and method here
-
-	// parse function or method qualifiers
-	AST::FunctionQualifiers qualifiers = parse_function_qualifiers ();
-
-	skip_token (FN_KW);
-
-	// parse function or method name
-	const_TokenPtr ident_tok = expect_token (IDENTIFIER);
-	if (ident_tok == nullptr)
-	  return nullptr;
-
-	Identifier ident{ident_tok};
-
-	// parse generic params
-	std::vector<std::unique_ptr<AST::GenericParam>> generic_params
-	  = parse_generic_params_in_angles ();
-
-	if (!skip_token (LEFT_PAREN))
-	  {
-	    // skip after somewhere?
-	    return nullptr;
-	  }
-
-	/* now for function vs method disambiguation - method has opening
-	 * "self" param */
-	auto initial_param = parse_self_param ();
-	if (!initial_param.has_value () && initial_param.error () != NOT_SELF)
-	  return nullptr;
-	/* FIXME: ensure that self param doesn't accidently consume tokens for
-	 * a function */
-	bool is_method = false;
-	if (initial_param.has_value ())
-	  {
-	    if ((*initial_param)->is_self ())
-	      is_method = true;
-
-	    /* skip comma so function and method regular params can be parsed
-	     * in same way */
-	    if (lexer.peek_token ()->get_id () == COMMA)
-	      lexer.skip_token ();
-	  }
-
-	// parse trait function params
-	std::vector<std::unique_ptr<AST::Param>> function_params
-	  = parse_function_params (
-	    [] (TokenId id) { return id == RIGHT_PAREN; });
-
-	if (!skip_token (RIGHT_PAREN))
-	  {
-	    // skip after somewhere?
-	    return nullptr;
-	  }
-
-	if (initial_param.has_value ())
-	  function_params.insert (function_params.begin (),
-				  std::move (*initial_param));
-
-	// parse return type (optional)
-	std::unique_ptr<AST::Type> return_type = parse_function_return_type ();
-
-	// parse where clause (optional)
-	AST::WhereClause where_clause = parse_where_clause ();
-
-	// parse semicolon or function definition (in block)
-	const_TokenPtr t = lexer.peek_token ();
-	std::unique_ptr<AST::BlockExpr> definition = nullptr;
-	switch (t->get_id ())
-	  {
-	  case SEMICOLON:
-	    lexer.skip_token ();
-	    // definition is already nullptr, so don't need to change it
-	    break;
-	  case LEFT_CURLY:
-	    definition = parse_block_expr ();
-	    /* FIXME: are these outer attributes meant to be passed into the
-	     * block? */
-	    break;
-	  default:
-	    add_error (
-	      Error (t->get_locus (),
-		     "expected %<;%> or definiton at the end of trait %s "
-		     "definition - found %qs instead",
-		     is_method ? "method" : "function",
-		     t->get_token_description ()));
-
-	    // skip?
-	    return nullptr;
-	  }
+    case FN_KW:
+      return parse_function (std::move (vis), std::move (outer_attrs));
 
-	// do actual if instead of ternary for return value optimisation
-	if (is_method)
-	  {
-	    AST::TraitMethodDecl method_decl (std::move (ident),
-					      std::move (qualifiers),
-					      std::move (generic_params),
-					      std::move (function_params),
-					      std::move (return_type),
-					      std::move (where_clause));
-
-	    // TODO: does this (method_decl) need move?
-	    return std::unique_ptr<AST::TraitItemMethod> (
-	      new AST::TraitItemMethod (std::move (method_decl),
-					std::move (definition),
-					std::move (outer_attrs),
-					tok->get_locus ()));
-	  }
-	else
-	  {
-	    AST::TraitFunctionDecl function_decl (std::move (ident),
-						  std::move (qualifiers),
-						  std::move (generic_params),
-						  std::move (function_params),
-						  std::move (return_type),
-						  std::move (where_clause));
-
-	    return std::unique_ptr<AST::TraitItemFunc> (new AST::TraitItemFunc (
-	      std::move (function_decl), std::move (definition),
-	      std::move (outer_attrs), tok->get_locus ()));
-	  }
-      }
       default: {
 	// TODO: try and parse macro invocation semi - if fails, maybe error.
 	std::unique_ptr<AST::TraitItem> macro_invoc
-- 
2.42.1


  parent reply	other threads:[~2024-02-07 12:44 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-02-07 11:43 [COMMITTED 01/25] gccrs: Parse normal functions with `self` parameter correctly arthur.cohen
2024-02-07 11:43 ` [COMMITTED 02/25] gccrs: Implement quick-check for Unicode arthur.cohen
2024-02-07 11:43 ` [COMMITTED 03/25] gccrs: Typecheck: lifetime interning and resolution tool arthur.cohen
2024-02-07 11:43 ` [COMMITTED 04/25] gccrs: TyTy: Region (lifetime) representation arthur.cohen
2024-02-07 11:43 ` [COMMITTED 05/25] gccrs: HIR: Add mising getter arthur.cohen
2024-02-07 11:43 ` [COMMITTED 06/25] gccrs: Typecheck: add regions (lifetimes) to TyTy arthur.cohen
2024-02-07 11:43 ` [COMMITTED 07/25] gccrs: TyTy: Store region constraints arthur.cohen
2024-02-07 15:26   ` Bernhard Reutner-Fischer
2024-02-07 14:46     ` Arthur Cohen
2024-02-07 11:43 ` [COMMITTED 08/25] gccrs: TyTy: Store reference to type before any substitutions arthur.cohen
2024-02-07 11:43 ` [COMMITTED 09/25] gccrs: Set the default ABI to C for extern blocks and extern functions arthur.cohen
2024-02-07 11:43 ` [COMMITTED 10/25] gccrs: add testcase to prove issue has already been fixed arthur.cohen
2024-02-07 11:43 ` [COMMITTED 11/25] gccrs: add test cases to prove type inference is working arthur.cohen
2024-02-07 11:43 ` [COMMITTED 12/25] gccrs: Fix ICE accessing empty vector without check arthur.cohen
2024-02-07 11:43 ` [COMMITTED 13/25] gccrs: remove old generics hack to reuse generic symbols from previous seg arthur.cohen
2024-02-09 10:03   ` Jakub Jelinek
2024-02-15  9:10     ` [PATCH] gccrs: Avoid *.bak suffixed tests - use dg-skip-if instead Jakub Jelinek
2024-02-15 13:12       ` Arthur Cohen
2024-02-07 11:44 ` [COMMITTED 14/25] gccrs: remove similar hack in type paths as we had in path expressions arthur.cohen
2024-02-07 11:44 ` [COMMITTED 15/25] gccrs: refactor inference variable computation into a seperate method arthur.cohen
2024-02-07 11:44 ` [COMMITTED 16/25] gccrs: Move the Implementation of implitem lowering into its own file arthur.cohen
2024-02-07 11:44 ` [COMMITTED 17/25] gccrs: Add testcase to show issue is already fixed arthur.cohen
2024-02-07 11:44 ` [COMMITTED 18/25] gccrs: fix bug in pattern check for tuples arthur.cohen
2024-02-07 11:44 ` [COMMITTED 19/25] gccrs: Use AssociatedItem in place of TraitItem arthur.cohen
2024-02-07 11:44 ` [COMMITTED 20/25] gccrs: Add checks for Trait functions arthur.cohen
2024-02-07 11:44 ` [COMMITTED 21/25] gccrs: Add missing visitors for AST::Function arthur.cohen
2024-02-07 11:44 ` [COMMITTED 22/25] gccrs: Fix inconsistent formatting arthur.cohen
2024-02-07 11:44 ` arthur.cohen [this message]
2024-02-07 11:44 ` [COMMITTED 24/25] gccrs: Remove obsolete classes and functions arthur.cohen
2024-02-07 11:44 ` [COMMITTED 25/25] gccrs: Fix macro parsing for trait items arthur.cohen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240207114419.1100894-24-arthur.cohen@embecosm.com \
    --to=arthur.cohen@embecosm.com \
    --cc=gcc-patches@gcc.gnu.org \
    --cc=gcc-rust@gcc.gnu.org \
    --cc=kushalpal109@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).