From patchwork Wed Feb 7 11:44:09 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 197921 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:7301:168b:b0:106:860b:bbdd with SMTP id ma11csp2205893dyb; Wed, 7 Feb 2024 04:57:49 -0800 (PST) X-Forwarded-Encrypted: i=3; AJvYcCWH9nZP/6JuiI2CgZ6ELyjgN8ERh4dVTJELK8/Y+y+1DZTQQ/w0IthA8s1Z1Kek47boyC7vcRNwVJd60k4YcnRol5Kd4g== X-Google-Smtp-Source: AGHT+IFBKtsQrEGoU6MA2Fy8ovE4epuCNM3f3dFC12ikR+8HpkiBMsM9Y/RliMtUC73YhgU5wvRN X-Received: by 2002:a05:620a:4713:b0:783:ccc5:b2b4 with SMTP id bs19-20020a05620a471300b00783ccc5b2b4mr7312322qkb.71.1707310669505; Wed, 07 Feb 2024 04:57:49 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1707310669; cv=pass; d=google.com; s=arc-20160816; b=MTakEHQkpR2/eCVUf/rBqP7trdmB3lKkC5FtYT67BtK6k57Xgz/hic0+nlXV1fVdsH a+aEVsgXM0TCVM4KNPBMwk/AcXSe5Ia5bQLy6OsSPJ0JTDfoMfIIfU1ichniYYBf2iD7 I1BnUWmkY05HjuEcTmbugEw7Qocw+lpE3vaM4eg9sYqhigsqTFw+0chiOlW7ymTnac1c YQBWnyxq9/RhKHyaW5tKQyxFWiLXaBfdSkn+qHERYoPGJOHwsS2sI4buF7cj5mhpDLxt 4iuRRpgE8RAGtedPmrHUwOFjVdlKG9QJp3e1dK7+INdTHJ2dwrVlqNK1fRXuRI2czgJZ kObw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:reply-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :mime-version:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=275pzFBZbtbGNKqcC26qikhiqLmdIMI55PSWmHZe7Lk=; fh=u4MHiZBWsvwlCYa14eiNT7uSiRHR1HxFrhGy77hyDw0=; b=uF5B+9/x2yPDCBEfBicpmt1w6R94OMTo/SOf+2HLzPeGBsEtaDCX/EpXLQ4u5lWXpu gL0uHn7yBOUEV/fu8Zr6Ebml/ohnZn/rTPLZNR8ckPmGW/Tmhu/xdnwJIFheRc/6HyWI uCk5zXRwy//CDc7zQmZ5sMU57Bt2ZN1dcTNQoLyg0WbOOzTc7oR9SakkgcBx9wQUK/UO lvmc7b6iI8wlMJtpH2fDn5iPjyXI2HzmYrpiEe8F9rmW9tnoPFoSX044logHUK0qJWpK gtFKQ1UHR8++lWwtpdlPvHn0Spbt8g7FeRlCAULqYEGwDyhhh6D6Hodq98MfO1oVNLYn 0v+Q==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=BqTk5e17; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" X-Forwarded-Encrypted: i=2; AJvYcCXZ1NRZgcTAyB1iDqsO7Ove/jOY/h2i9GXQTMqLhWs6tcWxWPDFvZVguJjJqfBEyYhzj9NMK7LzXut8NFpn2BXdlXefmQ== Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id s14-20020a05620a030e00b00783f739287csi1024023qkm.212.2024.02.07.04.57.49 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 07 Feb 2024 04:57:49 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=BqTk5e17; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3C5813857828 for ; Wed, 7 Feb 2024 12:57:49 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wm1-x333.google.com (mail-wm1-x333.google.com [IPv6:2a00:1450:4864:20::333]) by sourceware.org (Postfix) with ESMTPS id 0E3C13858001 for ; Wed, 7 Feb 2024 12:44:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 0E3C13858001 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 0E3C13858001 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::333 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707309880; cv=none; b=WmahVXDY+rcOZzpwSF0koHNpd/RbW+XWGJqWoSlQNw/xtz2spiz+4DB6PPuwaFx/OUlWVdpEcmsPf8LqA+na+rDN0R7SLJCL1muJ0EEno7Hn5Dn197tpmPBbyqyuhozPwfxbLjy7WOTwd8b7167HTOa25eoMLy1euTvrUZbiwho= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1707309880; c=relaxed/simple; bh=TkjHz7aPL9v3vq5OWNodpPcmmN04DJBTOXd1vQJxsew=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=et8vh4ux7qrZiLqkL7iMaTUz90R86Q3Jo7TNFDyqFpbU3FNmuI+AelvZdAJrTfLwcIeGXkS/p9ZZcO6LSgVJjmNrXbOcGa2goS+sag3v8QJnTwHowFd37yiNvnkDrOAP7/RTi7M8nC6Qpw2FXfjRy7VkWXrGQ8SYsoJ5C1xv+r0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-wm1-x333.google.com with SMTP id 5b1f17b1804b1-40efcb37373so5038575e9.2 for ; Wed, 07 Feb 2024 04:44:22 -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=FPSeg01cr4uLhEkEK/C/4msazQy0viS2FZkYte6AA69NT80aTrCXEp4pxG0Skzq6Hw fyyTyzjzJdtRfRHs2DNxCyyfEQsJaqwDD6oqdL/3q/8r/Sn1YlF/WWGpXZPspzM83uff x6Hd8EILFvE65v/ZNflyvw22ECan7UWlMYXLjsFf5D7f3nfBrkGsmZKC/asMV+smh5Tc EK3fnYgsMNDWt/747srdKZ9+68Chhs6wyVB+iGJQ5nlaIrmCl7VpnpnKrinn9oZwmedL AgoNMDkrr+zOYb0U4Fz+4UrcjpaDubdkgRUt9HdXdDFqdOLbSbhvvb+THuHpw/l5w7UG cUYA== X-Gm-Message-State: AOJu0YweEfVbnrm2nK+nIMUtbzJCOUKj8rW9HUtcxurhMJ+OPaeueNmL NnTXvvusfBvoZ6YgEWdhS4EucaHg7900BVrL11xkKmtL3pj42FiJb/h6Xy4Z1YrZuoyiPLi2xYE CfA== 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> MIME-Version: 1.0 X-Spam-Status: No, score=-13.5 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=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Reply-To: arthur.cohen@embecosm.com Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1790244992591885544 X-GMAIL-MSGID: 1790244992591885544 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