aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorKushal Pal <kushalpal109@gmail.com>2024-01-26 11:50:05 +0530
committerArthur Cohen <arthur.cohen@embecosm.com>2024-02-07 12:40:24 +0100
commit6e9dad6a7943a37aba5b1a0609bb9e46741dd1c8 (patch)
tree3c62ee38a70526e9d31790c42d15b8645dbc45b7 /gcc/rust
parentffeb72c41fd0efa71f0a1995ea7146323f640b15 (diff)
downloadgcc-6e9dad6a7943a37aba5b1a0609bb9e46741dd1c8.zip
gcc-6e9dad6a7943a37aba5b1a0609bb9e46741dd1c8.tar.gz
gcc-6e9dad6a7943a37aba5b1a0609bb9e46741dd1c8.tar.bz2
gccrs: Parse trait functions as `AST::Function`.
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>
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/parse/rust-parse-impl.h127
1 files changed, 2 insertions, 125 deletions
diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h
index a7de948..ed26437 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