From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wm1-x32d.google.com (mail-wm1-x32d.google.com [IPv6:2a00:1450:4864:20::32d]) by sourceware.org (Postfix) with ESMTPS id 4C8003857C65 for ; Wed, 7 Feb 2024 12:44:21 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 4C8003857C65 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 4C8003857C65 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::32d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707309872; cv=none; b=uoCTPx8vdxaYE4pQ2eRzOrVm++KEsv+w8NEydhtz0honvnnwEcZ71thMGInrfB0dkT35YVWyR5TrD3MMJ19wJj428vCVspp7TJepSoqGTUPxnuuyIRSac74e4agYpCi1RZQNTXrZ/R3TWGs6e9qOqzLMIhw0kw9occIuPM9JYEY= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707309872; c=relaxed/simple; bh=TkjHz7aPL9v3vq5OWNodpPcmmN04DJBTOXd1vQJxsew=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=e5G/HkqVUs7XZz4rTL+MIgz/8vWFVOMhtTaq2a2zCQ8HfPAWscea9CxjDCIUUBtCfCG/mTGON7xNBZ8eueMDAgxaubD2+4+7c9V4R419AZ/p9+iJLc65i5bz75cOhRgcOe6Am/yuCjncbUvLE34RhUEZfMHL59CWckUHXY27cZ4= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x32d.google.com with SMTP id 5b1f17b1804b1-40ff28388a6so5085495e9.1 for ; Wed, 07 Feb 2024 04:44:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1707309860; x=1707914660; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:from:to:cc:subject :date:message-id:reply-to; bh=275pzFBZbtbGNKqcC26qikhiqLmdIMI55PSWmHZe7Lk=; b=BqTk5e17mRmSssVA9whRieYUP9bHcMHLXrwynwurYqoc4MWUNtThAm1tzQOdWGoZcu 1KAT0t7S6e5u2fEUWOSxR5d0xZKuIqI8Sdw2RVU6dIlkA0ui2E71ZouzH3MBye5EljM/ 2aQMmha1vzGNHLYbxgOqpIqRIWIvPtSOa4WltZVA5MeyMD0EIayMfvp8AuBCDf4KP4ft DmDKk4Znn76ZGcnuej+1/zmaW1w1Jz72ApQaYcHrWX10Qk4c9m3h6puWQtgci2IekRRB 9zSAm2Q4tJJ9fuqIt7XlWoT1x912qJpUQf/1jXxflKx7/invlLozIx70ggPekmWIfGTv qakQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1707309860; x=1707914660; h=content-transfer-encoding:mime-version:reply-to:references :in-reply-to:message-id:date:subject:cc:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=275pzFBZbtbGNKqcC26qikhiqLmdIMI55PSWmHZe7Lk=; b=Vw/hK9X2Dq+oFrLNKCsDN8Id52jWl//zb8IlMhOHi6cJdFj+Jh+WnA81vZZVKxs0PX W/TYcpNUTBiDbWRqBg9picHL3YHnMwZ1cc6WbHHA6cqlJCPZreCSerHuSvWnbUcpxS7v NdMzzVV1aTSGjrj/PYcR9ay6T59LZyuPSK9D7KnOVfEoH1ywnV3lc9X15xPwdVW3hU1z nyGyK8s3Zff97KQKUjd/70dZeuQ3IOztY9a0e5O7El2uKO7KIW3RlQWXoJ5auKGYdZvg DYWLu6oUK13fjzp4Zpy/YuKgTid0j7EcQUEJw7qL2SiHyVnSXs6V3SizXWn1sGmPhFhz RLkQ== X-Gm-Message-State: AOJu0YweATUwUZWEpAjNrz8tNzbcpy0eTm50ack0S522fHB3m0iOHmlP EC0cBM2J2XGcwr1KFmLGcXVl3uNDzmPDa/ptwNoA4OfysiT29JX4RPXryFDErbULqFWvHdV/vwy zWQ== X-Google-Smtp-Source: AGHT+IFMewyHvYdE/ixsMvKBClD4R9mACuobVRIJtcj3blRemdEMts1NBjh7v5cAKT/QnK3zXgaLww== X-Received: by 2002:a05:600c:1c01:b0:40e:b313:b8db with SMTP id j1-20020a05600c1c0100b0040eb313b8dbmr4726906wms.28.1707309860054; Wed, 07 Feb 2024 04:44:20 -0800 (PST) X-Forwarded-Encrypted: i=1; AJvYcCXzfd8ofodheRGppLdOQi5XxTjB1jXe9BuNhCzmD4VnYj33Y7paOCc45n/+/R1S9PERSYdD/R8yx71vKpyUValc7EylCsKt Received: from platypus.sou.embecosm-corp.com ([212.69.42.53]) by smtp.gmail.com with ESMTPSA id x19-20020a05600c2a5300b0040fbdd6f69bsm5044188wme.33.2024.02.07.04.44.19 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 04:44:19 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Kushal Pal Subject: [COMMITTED 23/25] gccrs: Parse trait functions as `AST::Function`. Date: Wed, 7 Feb 2024 12:44:09 +0100 Message-ID: <20240207114419.1100894-24-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20240207114419.1100894-2-arthur.cohen@embecosm.com> References: <20240207114419.1100894-2-arthur.cohen@embecosm.com> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-13.4 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,GIT_PATCH_0,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: From: Kushal Pal 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 --- 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::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> 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> 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 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 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 ( - 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 (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 macro_invoc -- 2.42.1