From patchwork Tue Feb 21 12:01:45 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 60045 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1842227wrn; Tue, 21 Feb 2023 04:49:39 -0800 (PST) X-Google-Smtp-Source: AK7set/vAO2/RCUhEJpZiVKsMtVE8IvrRIWzaQ7qBA/7M5AIMGJLVQfDnsaoM7ykQWU+MhkwGQyB X-Received: by 2002:a17:907:7f27:b0:8d5:f77:284d with SMTP id qf39-20020a1709077f2700b008d50f77284dmr6792123ejc.21.1676983779530; Tue, 21 Feb 2023 04:49:39 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676983779; cv=none; d=google.com; s=arc-20160816; b=TlGOrSnfm5NbTBR/NZTcxAHoDUlH8BPXzf2W5DFyK4vstO54YvYAAM/uegRv2BBgPL y7ikjkbVYhoUqWVo6xl0786qvBFDNBvFkys3wvAYVaTzRcIZyfkwzO3zKgsjXuOyVCZu FeYaMBkRqD+5sOZmTH98ZJX8bwHwVsFMjho+QWAVmm9Y25ZXEIJFiYjNQTTeNf7z9/eI pQ4r91w2U+iz4pzdcCuy/GRLysHHYchbpg00baJg0N0xoHlvujVeh6NoMSA9QjvCWXaX wgfLgd2wqXnNCYJ/WlXfAR9UiV2dPrNWikAWqj1BwNna2066yGYH+QKQYNG9uUCIrZs6 0t8w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender: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:dmarc-filter :delivered-to; bh=eQjqnCabeSQGQ4Q9UTx1rnkZ8Ka6kmB8wDytSJyo+3w=; b=NLLK1/u4e9/0Fo+mc8L8n7Z9H2khaJoCKZ4MofQzA/d/D9s6kBATY6HCoyGGaH8q6j 8BBaqHJL/nLW6cdm+Z6uvMTYcP0703FcgXmQTnik1fjRr9p5gFH+b371rMM1zqMxYWMp GKNJ52J5XPfZQi4e7+KvjZySH9NEX1T7j9S58wgHnOrVqFabSfZVzDsAuvs+mGDznRbf L5TuC+qUyCAIiIvWJV6n5iaDeknxEIihnK+byyGo+pFIQQAmljrjKnUVNis7KWZiQjno wkvULLyWqpWgMjYqKebKZUbLsPCw75B5qevgrwxvj9djsLU3xh5KmkHlYF43aHkKcyMU /HNw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=CsJ3R3oL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org" Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id n2-20020a17090673c200b008b2ab95c90asi16752129ejl.707.2023.02.21.04.49.39 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:49:39 -0800 (PST) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=CsJ3R3oL; spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 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 2BB0D396F9E0 for ; Tue, 21 Feb 2023 12:16:39 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x436.google.com (mail-wr1-x436.google.com [IPv6:2a00:1450:4864:20::436]) by sourceware.org (Postfix) with ESMTPS id E9905383905A for ; Tue, 21 Feb 2023 12:04:08 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E9905383905A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=embecosm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=embecosm.com Received: by mail-wr1-x436.google.com with SMTP id c5so4850235wrr.5 for ; Tue, 21 Feb 2023 04:04:08 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; 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=eQjqnCabeSQGQ4Q9UTx1rnkZ8Ka6kmB8wDytSJyo+3w=; b=CsJ3R3oLpAuzo4/QAUFEhSX8SuacaEVyHcS1qaywHIAnnlLoG3K+I/kvt59hXj0X59 q1YZPfRng/RzDwseirTun0dIvQVzo7Bo47S4sRY3ctAU0cK1MFab6pdmx8idwI86xNxY qIkDREPH5csP6AdlqID3xlHnbTLjxr8fMMA5a1bQ+jmeCf0/6R965K+5C9hJ2u0r1z5u +fpLVmHjDd+uGiCk7j6clliecWF3E8vw/wiSl9N/n4j216miyFq9dCt4kWxmdOsk5PYB GAlunffNCGfwHaWqsXurjh5m/hKX+L6MerlcLfGsxamZKhWL+xTtglDv6EfAsD2pzA3r aoBw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; 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=eQjqnCabeSQGQ4Q9UTx1rnkZ8Ka6kmB8wDytSJyo+3w=; b=MzmfvxTggq0kEwZZkTCaU2OKMDgqrLsga/G0lCQBFDYf02nHjh2gmrpiQLvrF+9TiC XnjaTznxrz8c9RhszIMAqMD1EvjFeX9hdBKcxbfKoJpSH64noBD62EDsVS2MdOpMuQAT 7kCvBuAXW5cGJ5TaFxdmMLEMCR/UxpyV94dDK23E1V+jdDj0CiAqykaejpNUoo40hA33 n8ooNT+GPkg26hRy67kuVLmuXbGr8zrJ/Qo1u22IkpjWfAv9BVRGlaKv35EBV9AFjKHz 2jk3xOLjhEITZEbgFJ8SmDKICAfisxU8vVc6DtoSfAY9IsB0eq1rIWfgHwSnXKxPHJwX Jajg== X-Gm-Message-State: AO0yUKVHvTi45xEm+iuD0ZUxzjphbzjxggZ4iz1lvfGcLVgf3sfvRU69 wXXCVkDXDG/s/rbHUxd/ShK3R9L4tqQMw8XPyg== X-Received: by 2002:adf:e184:0:b0:2c5:5ec7:43bc with SMTP id az4-20020adfe184000000b002c55ec743bcmr2422655wrb.18.1676981047586; Tue, 21 Feb 2023 04:04:07 -0800 (PST) Received: from platypus.localdomain ([62.23.166.218]) by smtp.gmail.com with ESMTPSA id c15-20020adffb4f000000b002c55b0e6ef1sm5013811wrs.4.2023.02.21.04.04.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:04:07 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, YizhePKU Subject: [committed 055/103] gccrs: Implement the inline visitor Date: Tue, 21 Feb 2023 13:01:45 +0100 Message-Id: <20230221120230.596966-56-arthur.cohen@embecosm.com> X-Mailer: git-send-email 2.39.1 In-Reply-To: <20230221120230.596966-1-arthur.cohen@embecosm.com> References: <20230221120230.596966-1-arthur.cohen@embecosm.com> MIME-Version: 1.0 X-Spam-Status: No, score=-14.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham 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.29 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 Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1758444943563319701?= X-GMAIL-MSGID: =?utf-8?q?1758444943563319701?= From: YizhePKU gcc/rust/ChangeLog: * util/rust-inline-visitor.h: New file. --- gcc/rust/util/rust-inline-visitor.h | 95 +++++++++++++++++++++++++++++ 1 file changed, 95 insertions(+) create mode 100644 gcc/rust/util/rust-inline-visitor.h diff --git a/gcc/rust/util/rust-inline-visitor.h b/gcc/rust/util/rust-inline-visitor.h new file mode 100644 index 00000000000..18920d9f93a --- /dev/null +++ b/gcc/rust/util/rust-inline-visitor.h @@ -0,0 +1,95 @@ +// Copyright (C) 2021-2022 Free Software Foundation, Inc. + +// This file is part of GCC. + +// GCC is free software; you can redistribute it and/or modify it under +// the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3, or (at your option) any later +// version. + +// GCC is distributed in the hope that it will be useful, but WITHOUT ANY +// WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License +// for more details. + +// You should have received a copy of the GNU General Public License +// along with GCC; see the file COPYING3. If not see +// . + +// An improved implementation of the inline visitor. +// Original idea from https://members.accu.org/index.php/articles/2021 + +#ifndef RUST_INLINE_VISITOR +#define RUST_INLINE_VISITOR + +#include +#include + +namespace Rust { + +// Wrapper for the target Visitor we're matching against. +// Consumes the final nullptr of the _args linked list. +template struct EmptyVisitor : TargetVisitor +{ + EmptyVisitor (std::nullptr_t ptr) {} + + using TargetVisitor::visit; +}; + +// Wrapper for a (possibly incomplete) Visitor. +template struct VisitorWrapper +{ + // Lambdas are stored in _args as a linked list and passed to the actual + // visitor when end_visitor() is called. + Args _args; + + // The actual visitor being created. + // Each visitor inherits from the last one and implements one more visit(). + template struct Visitor : BaseVisitor + { + F _f; + + Visitor (std::pair &&args) + : BaseVisitor (std::move (args.second)), _f (std::move (args.first)) + {} + + using BaseVisitor::visit; + virtual void visit (T &t) final override { _f (t); } + }; + + VisitorWrapper (Args &&args) : _args (std::move (args)) {} + + // Add another visit() method to the visitor. + // _args will be moved over, so don't keep the old wrapper around. + template + VisitorWrapper, std::pair> on (F &&f) + { + return VisitorWrapper, std::pair> ( + std::make_pair (std::move (f), std::move (_args))); + } + + // Returns the finished visitor. + // NOTE: The reference implementation has a bug that exposes this method even + // when BaseVisitor is still an abstract class. The C++11 standard states that + // "An abstract class shall not be used [...] as a function return type". GCC + // rejects the buggy code as expected, but Clang accepts the code as long as + // the method is not actually called. Maybe this is a bug in Clang? + template + typename std::enable_if::value, T>::type + end_visitor () + { + return T (std::move (_args)); + } +}; + +// The entry point. +template +VisitorWrapper, std::nullptr_t> +begin_visitor () +{ + return VisitorWrapper, std::nullptr_t> (nullptr); +} + +} // namespace Rust + +#endif