From patchwork Thu Aug 1 14:56:36 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 94987 Return-Path: X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id F3CBA384A42A for ; Thu, 1 Aug 2024 15:15:20 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by sourceware.org (Postfix) with ESMTPS id EC8C33860758 for ; Thu, 1 Aug 2024 14:59:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org EC8C33860758 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 EC8C33860758 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::533 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524371; cv=none; b=J8A7QtfDkba4tKjh83rfT+mtxROC+6/vg4JWO3WXy/z38hp2F7jud+l9IM/5h1ebGBIGlXNxdwbhbkToyMw6NGzwSnmTvjDM94OMP9WRAZh9n0gOf7JF/bZ1RYEhsxkZLqoqhd4IlRepVtAAFD23UfQFkS/huPd04zmN/k9zIpI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524371; c=relaxed/simple; bh=q8R3OtuxPmFqlCnu7gn2OliYawHtXbVY9foKSThErl4=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=aQ4MgTKYOLPeNYHJEiEVuia5C/yV0aCm2bLx5ZOEwobQqwK1MrnDtP0hYX+UowmBVC0oJNm8wTxMXKpe6h8J2KmvkcmlzsyyNb78BTBlOoMRZcTQgWrRcH6lxTXTWBMliRALshJf9t1Q35t1aOURTu5iMASp9fGK46/ZKg2pqlQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x533.google.com with SMTP id 4fb4d7f45d1cf-5a10835487fso10069771a12.1 for ; Thu, 01 Aug 2024 07:59:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524342; x=1723129142; darn=gcc.gnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=nErzoFCGZvmB5ni+2lBzG+uC2pIgGsJAz5YAiR3Jb9s=; b=F69A7GEu1SnGpf9RavftIsxhy+qlyC1CHlZqdclNfC6kscyhxv8vUbTTzBMNK4OP/g uPANtlw+WlSJ9FE6dfVLIFmoP9B2bYe1Hu4J3eZJlX5hVoQPojIGdGMBvNc408+H5xbF JaXD0bPbPITZDyxy3ZTyd4J4NQPTwHvSRleZcN6B5FKqvSQP2P+jZiohn00j1rP38yd/ alDRmpaiCoDAu5ar7gn//VAdkXcOtpkW9IZi5RuaYl7y7BKRmcyqOBuWst+JrcZuWGtc 63PVbkdCgrMRnPaiCQ1sLysXhrN4upcHOFgLF63lxAaqGnkFaNyfNZWdoA2q3Ms1a5ar jCWw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524342; x=1723129142; h=content-transfer-encoding:mime-version: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=nErzoFCGZvmB5ni+2lBzG+uC2pIgGsJAz5YAiR3Jb9s=; b=Q5Whg8luC5M/jec1EwZDG6y10JPLD6pcPECe3/ou2rL3Dm0epQIAWq0svayWgO6sdu Yg5bOZ07oM54eHnWLLW2EqM7I2TpJTYMaDcoCpdG3jixuWz71PfNpuDd4IEI/0ZjeGWt BDQd7nhyNx5iH9FZHvW9CQkpvXJ0a5ufmBom5n68PYqvACIwmGaMf5aoFbE7p0/AtMpG 0im/ZtRtrlGp7xq5InxmVTX7nRAyqrwL8rFLYMt4sDPLU1uCZi0BWtnqebEk30LXkj+L fttlXMXu86HvUsbeGsWAjKUqFP46nt6+L1QHr/dBCRFD6GJ+UJV/z6oP4q9kXiuI/4w6 POJg== X-Gm-Message-State: AOJu0YxJ+SfXiKa0qJC+ihifxPNiyvhzbtERcZKLElHYG83wtnhVN3gj eS9ZCxNw1oWaJegHCldJZj/KxTSoouHxVnJrjBmrQM+5qgXDNKFah/xm8MKdWd8CJ313RF1Or/V aeWdC X-Google-Smtp-Source: AGHT+IHqv34ZwJN5KoSotVjEEfLiF3MbQ1tNBki+K+nyB3C0azNubhFUB2KHA309Pbf1Gxcg2QtDsQ== X-Received: by 2002:aa7:cad6:0:b0:57c:7471:a0dd with SMTP id 4fb4d7f45d1cf-5b7f3bcfb2fmr485733a12.12.1722524342323; Thu, 01 Aug 2024 07:59:02 -0700 (PDT) Received: from platypus.lan ([2a04:cec2:9:dc84:3622:6733:ff49:ee91]) by smtp.gmail.com with ESMTPSA id 4fb4d7f45d1cf-5ac63590592sm10252456a12.25.2024.08.01.07.59.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:59:02 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, 0xn4utilus Subject: [PATCH 040/125] gccrs: Unify ASTValidation::visit for ExternalFunctionItem and Function Date: Thu, 1 Aug 2024 16:56:36 +0200 Message-ID: <20240801145809.366388-42-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240801145809.366388-2-arthur.cohen@embecosm.com> References: <20240801145809.366388-2-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.1 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 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: , Errors-To: gcc-patches-bounces~patchwork=sourceware.org@gcc.gnu.org From: 0xn4utilus gcc/rust/ChangeLog: * checks/errors/rust-ast-validation.cc (ASTValidation::visit): Add external function validation support. Add ErrorCode::E0130. * parse/rust-parse-impl.h (Parser::parse_function): Parse external functions from `parse_function`. (Parser::parse_external_item): Clang format. (Parser::parse_pattern): Clang format. * parse/rust-parse.h: Add default parameter `is_external` in `parse_function`. Signed-off-by: 0xn4utilus --- gcc/rust/checks/errors/rust-ast-validation.cc | 62 +++++++++++++++---- gcc/rust/parse/rust-parse-impl.h | 9 +-- gcc/rust/parse/rust-parse.h | 3 +- 3 files changed, 57 insertions(+), 17 deletions(-) diff --git a/gcc/rust/checks/errors/rust-ast-validation.cc b/gcc/rust/checks/errors/rust-ast-validation.cc index bf70ca5d96f..f5a97b0d350 100644 --- a/gcc/rust/checks/errors/rust-ast-validation.cc +++ b/gcc/rust/checks/errors/rust-ast-validation.cc @@ -122,23 +122,61 @@ ASTValidation::visit (AST::Function &function) function.get_self_param ()->get_locus (), "% parameter is only allowed in associated functions"); - if (!function.has_body ()) + if (function.is_external ()) { - if (context.back () == Context::INHERENT_IMPL - || context.back () == Context::TRAIT_IMPL) + if (function.has_body ()) + rust_error_at (function.get_locus (), "cannot have a body"); + + auto ¶ms = function.get_function_params (); + + if (params.size () == 1 && function.is_variadic ()) rust_error_at (function.get_locus (), - "associated function in % without body"); - else if (context.back () != Context::TRAIT) - rust_error_at (function.get_locus (), "free function without a body"); + "C-variadic function must be declared with at least one " + "named argument"); + + for (auto it = params.begin (); it != params.end (); it++) + { + if (it->get ()->is_variadic () && it + 1 != params.end ()) + rust_error_at ( + it->get ()->get_locus (), + "%<...%> must be the last argument of a C-variadic function"); + + // if functional parameter + if (!it->get ()->is_self () && !it->get ()->is_variadic ()) + { + auto param = static_cast (it->get ()); + auto kind = param->get_pattern ()->get_pattern_kind (); + + if (kind != AST::Pattern::Kind::Identifier + && kind != AST::Pattern::Kind::Wildcard) + rust_error_at (it->get ()->get_locus (), ErrorCode::E0130, + "pattern not allowed in foreign function"); + } + } } - auto &function_params = function.get_function_params (); - for (auto it = function_params.begin (); it != function_params.end (); it++) + else { - if (it->get ()->is_variadic ()) - rust_error_at (it->get ()->get_locus (), - "only foreign or % functions may " - "be C-variadic"); + if (!function.has_body ()) + { + if (context.back () == Context::INHERENT_IMPL + || context.back () == Context::TRAIT_IMPL) + rust_error_at (function.get_locus (), + "associated function in % without body"); + else if (context.back () != Context::TRAIT) + rust_error_at (function.get_locus (), + "free function without a body"); + } + auto &function_params = function.get_function_params (); + for (auto it = function_params.begin (); it != function_params.end (); + it++) + { + if (it->get ()->is_variadic ()) + rust_error_at ( + it->get ()->get_locus (), + "only foreign or % functions may " + "be C-variadic"); + } } AST::ContextualASTVisitor::visit (function); diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 9d9722e9714..c8a87a11766 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -2908,7 +2908,8 @@ Parser::parse_use_tree () template std::unique_ptr Parser::parse_function (AST::Visibility vis, - AST::AttrVec outer_attrs) + AST::AttrVec outer_attrs, + bool is_external) { location_t locus = lexer.peek_token ()->get_locus (); // Get qualifiers for function if they exist @@ -2992,7 +2993,7 @@ Parser::parse_function (AST::Visibility vis, std::move (generic_params), std::move (function_params), std::move (return_type), std::move (where_clause), std::move (body), std::move (vis), - std::move (outer_attrs), locus)); + std::move (outer_attrs), locus, false, is_external)); } // Parses function or method qualifiers (i.e. const, unsafe, and extern). @@ -6166,6 +6167,7 @@ Parser::parse_external_item () case FN_KW: return parse_external_function_item (std::move (vis), std::move (outer_attrs)); + case TYPE: return parse_external_type_item (std::move (vis), std::move (outer_attrs)); @@ -10474,8 +10476,7 @@ Parser::parse_pattern () { lexer.skip_token (); alts.push_back (parse_pattern_no_alt ()); - } - while (lexer.peek_token ()->get_id () == PIPE); + } while (lexer.peek_token ()->get_id () == PIPE); /* alternates */ return std::unique_ptr ( diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 8c8bf96eb8d..cdddfa6dff9 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -253,7 +253,8 @@ private: parse_use_decl (AST::Visibility vis, AST::AttrVec outer_attrs); std::unique_ptr parse_use_tree (); std::unique_ptr parse_function (AST::Visibility vis, - AST::AttrVec outer_attrs); + AST::AttrVec outer_attrs, + bool is_external = false); AST::FunctionQualifiers parse_function_qualifiers (); std::vector> parse_generic_params_in_angles ();