From patchwork Thu Aug 1 14:56:08 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 1967721 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@legolas.ozlabs.org Authentication-Results: legolas.ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=embecosm.com header.i=@embecosm.com header.a=rsa-sha256 header.s=google header.b=WnOaPHiK; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=2620:52:3:1:0:246e:9693:128c; helo=server2.sourceware.org; envelope-from=gcc-patches-bounces~incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=patchwork.ozlabs.org) Received: from server2.sourceware.org (server2.sourceware.org [IPv6:2620:52:3:1:0:246e:9693:128c]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (secp384r1) server-digest SHA384) (No client certificate requested) by legolas.ozlabs.org (Postfix) with ESMTPS id 4WZXH21Kpmz1yZv for ; Fri, 2 Aug 2024 01:01:34 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 6825B386075C for ; Thu, 1 Aug 2024 15:01:32 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x536.google.com (mail-ed1-x536.google.com [IPv6:2a00:1450:4864:20::536]) by sourceware.org (Postfix) with ESMTPS id B7410385DDF1 for ; Thu, 1 Aug 2024 14:58:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B7410385DDF1 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 B7410385DDF1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::536 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524326; cv=none; b=wrEQa878Gq4mWE8MvH5Aub/9QXUxicumYAkdX2yO5rli3Ldru2ukNy4giiaIeIXNDdwO/vr1sfqtkD9GnAMrtXUtlXQAfXHTY1K+qLutiNLSH7oCRriYrEqCZot6EDmH4Zf5uac0om7qFJtb1niElvRn/Ite8AVDwZdHojHXhWE= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524326; c=relaxed/simple; bh=NqDhz126o2wJw5+tUPQHfX1SooR0zYs51Wv/LZXAvAE=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=VZKntiat1DST5Mv3UkWn7SUuxCysAHFvcXYr1cu4roBhKWHRZPSO+q7DRYBHeBgQaGYuCp2Zu4sIdyfXkUZxk/ynraduZeldRlOima0U+NJFvTGqdhl5nU3s/J0GW64dxtP1MVeuG8MlvLWmVEYqZ8oELNFiCF/nlvG8QpPm4w0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5ab2baf13d9so9935186a12.2 for ; Thu, 01 Aug 2024 07:58:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524320; x=1723129120; 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=8f/YQAb/55zzSldmsmDGYymebU4wjwYKYT6YAPqdsr8=; b=WnOaPHiKRoJTfO2kNQIC4DBbg+fv7F8enT+HEOA/AUpzheP1b9bZN2scIV6rgSVyrq b0n0XTj+IMb09nbr24m93/ARQ5Z23pGqhQCYpJPodM5ogBVfRFnXdEorQVKhfzUPYIMq CyWIsby/DXeLiT7ZqYqsg2kiONHCAlei/94+PHTl+YlL+JYB0zGJaKpBzn6y00ZY1GwO w9D2WUf9gAvSBJFYaIuL1mLOj3h4IHF6Zp9Wt67u4lgLqI/gC6+YmQ7TR2UB3805PhIw s+1+5RDFIGXaDxMU7guEhaRG5LQPTuV6L8BUr/qzSy7udR2ik6HybgjwjbCb9gUTg2ol t6rw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524320; x=1723129120; 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=8f/YQAb/55zzSldmsmDGYymebU4wjwYKYT6YAPqdsr8=; b=BUDfIHeY3lkYKDUpOozw5hUbkA0aVZQRfirQuPbuxngBg8jXoh+iTO2l3y+Y/x7rN3 kdC3S9Hspb6MBO75JSmMh7QW1X4zudFye1pCNRoPxHUMYumH5nQ+TISUkIdrR+EiheUd YVjzpnhM7MK6jkqSZr/lhVSVidYVvCvvFEswsjVB0l1Ks6tJtMaXbbbqI/PnYJjjY2rO 02CZW7SpvCccCE3a1jORHZDMqUK4mrv99wKWOYUSoYPdWAw4S/hIPt29ug9+zI036TFd KX7yz3PMJdhRor74TdTM4NQdsVFqJSIIRKVCHnaZCQzvPzcMCXWcq3QZVcoM+jkahrlD CchQ== X-Gm-Message-State: AOJu0YxXeHkI+XcbFX+vBoVSR9M3GxIZYXJvoyQDYbAwjoj7S9JMHNza xG8pBK+wjHQFcIxFQJBTVRU0jhcGknqDUYA6ILuD7AY37g9wLRgMaxTxpBnSZknVb7ExGXQyxy8 VSNDj X-Google-Smtp-Source: AGHT+IHLNs3C/wTN0ippuyV0f5W370Z2ZY6jR49iabT3pyz9qWyQq4tGPHmhJqw8bpHr8vGJ7lMTMQ== X-Received: by 2002:aa7:d34e:0:b0:5a7:448b:4434 with SMTP id 4fb4d7f45d1cf-5b7f3adacc6mr489911a12.9.1722524320072; Thu, 01 Aug 2024 07:58:40 -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.58.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:58:39 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [PATCH 012/125] gccrs: libformat_parser: Start experimenting with cbindgen Date: Thu, 1 Aug 2024 16:56:08 +0200 Message-ID: <20240801145809.366388-14-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.0 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~incoming=patchwork.ozlabs.org@gcc.gnu.org libgrust/ChangeLog: * libformat_parser/cbindgen.toml: New file. * libformat_parser/libformat-parser.h: New file. gcc/rust/ChangeLog: * ast/rust-fmt.h: Add remaining FFI types. --- gcc/rust/ast/rust-fmt.h | 4 +- libgrust/libformat_parser/cbindgen.toml | 0 libgrust/libformat_parser/libformat-parser.h | 224 +++++++++++++++++++ 3 files changed, 226 insertions(+), 2 deletions(-) create mode 100644 libgrust/libformat_parser/cbindgen.toml create mode 100644 libgrust/libformat_parser/libformat-parser.h diff --git a/gcc/rust/ast/rust-fmt.h b/gcc/rust/ast/rust-fmt.h index 0050977358f..27c1c3625d3 100644 --- a/gcc/rust/ast/rust-fmt.h +++ b/gcc/rust/ast/rust-fmt.h @@ -92,11 +92,11 @@ struct FormatSpec /// The `x` or `X` flag. (Only for `Debug`.) tl::optional debug_hex; /// The integer precision to use. - // Count <'a> precision; + Count precision; /// The span of the precision formatting flag (for diagnostics). tl::optional precision_span; /// The string width requested for the resulting format. - // Count <'a> width; + Count width; /// The span of the width formatting flag (for diagnostics). tl::optional width_span; /// The descriptor string representing the name of the format desired for diff --git a/libgrust/libformat_parser/cbindgen.toml b/libgrust/libformat_parser/cbindgen.toml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/libgrust/libformat_parser/libformat-parser.h b/libgrust/libformat_parser/libformat-parser.h new file mode 100644 index 00000000000..a4bc8a75494 --- /dev/null +++ b/libgrust/libformat_parser/libformat-parser.h @@ -0,0 +1,224 @@ +#include +#include +#include +#include +#include + +/// Enum of alignments which are supported. +enum class Alignment +{ + /// The value will be aligned to the left. + AlignLeft, + /// The value will be aligned to the right. + AlignRight, + /// The value will be aligned in the center. + AlignCenter, + /// The value will take on a default alignment. + AlignUnknown, +}; + +/// Enum for the debug hex flags. +enum class DebugHex +{ + /// The `x` flag in `{:x?}`. + Lower, + /// The `X` flag in `{:X?}`. + Upper, +}; + +/// Enum for the sign flags. +enum class Sign +{ + /// The `+` flag. + Plus, + /// The `-` flag. + Minus, +}; + +template struct Box; + +template struct Option; + +/// Enum describing where an argument for a format can be located. +struct Position +{ + enum class Tag + { + /// The argument is implied to be located at an index + ArgumentImplicitlyIs, + /// The argument is located at a specific index given in the format, + ArgumentIs, + /// The argument has a name. + ArgumentNamed, + }; + + struct ArgumentImplicitlyIs_Body + { + uintptr_t _0; + }; + + struct ArgumentIs_Body + { + uintptr_t _0; + }; + + struct ArgumentNamed_Body + { + const str *_0; + }; + + Tag tag; + union + { + ArgumentImplicitlyIs_Body argument_implicitly_is; + ArgumentIs_Body argument_is; + ArgumentNamed_Body argument_named; + }; +}; + +/// Range inside of a `Span` used for diagnostics when we only have access to +/// relative positions. +struct InnerSpan +{ + uintptr_t start; + uintptr_t end; +}; + +/// A count is used for the precision and width parameters of an integer, and +/// can reference either an argument or a literal integer. +struct Count +{ + enum class Tag + { + /// The count is specified explicitly. + CountIs, + /// The count is specified by the argument with the given name. + CountIsName, + /// The count is specified by the argument at the given index. + CountIsParam, + /// The count is specified by a star (like in `{:.*}`) that refers to the + /// argument at the given index. + CountIsStar, + /// The count is implied and cannot be explicitly specified. + CountImplied, + }; + + struct CountIs_Body + { + uintptr_t _0; + }; + + struct CountIsName_Body + { + const str *_0; + InnerSpan _1; + }; + + struct CountIsParam_Body + { + uintptr_t _0; + }; + + struct CountIsStar_Body + { + uintptr_t _0; + }; + + Tag tag; + union + { + CountIs_Body count_is; + CountIsName_Body count_is_name; + CountIsParam_Body count_is_param; + CountIsStar_Body count_is_star; + }; +}; + +/// Specification for the formatting of an argument in the format string. +struct FormatSpec +{ + /// Optionally specified character to fill alignment with. + Option fill; + /// Span of the optionally specified fill character. + Option fill_span; + /// Optionally specified alignment. + Alignment align; + /// The `+` or `-` flag. + Option sign; + /// The `#` flag. + bool alternate; + /// The `0` flag. + bool zero_pad; + /// The `x` or `X` flag. (Only for `Debug`.) + Option debug_hex; + /// The integer precision to use. + Count precision; + /// The span of the precision formatting flag (for diagnostics). + Option precision_span; + /// The string width requested for the resulting format. + Count width; + /// The span of the width formatting flag (for diagnostics). + Option width_span; + /// The descriptor string representing the name of the format desired for + /// this argument, this can be empty or any number of characters, although + /// it is required to be one word. + const str *ty; + /// The span of the descriptor string (for diagnostics). + Option ty_span; +}; + +/// Representation of an argument specification. +struct Argument +{ + /// Where to find this argument + Position position; + /// The span of the position indicator. Includes any whitespace in implicit + /// positions (`{ }`). + InnerSpan position_span; + /// How to format the argument + FormatSpec format; +}; + +/// A piece is a portion of the format string which represents the next part +/// to emit. These are emitted as a stream by the `Parser` class. +struct Piece +{ + enum class Tag + { + /// A literal string which should directly be emitted + String, + /// This describes that formatting should process the next argument (as + /// specified inside) for emission. + NextArgument, + }; + + struct String_Body + { + const str *_0; + }; + + struct NextArgument_Body + { + Box _0; + }; + + Tag tag; + union + { + String_Body string; + NextArgument_Body next_argument; + }; +}; + +struct PieceSlice +{ + const Piece *base_ptr; + uintptr_t len; +}; + +extern "C" { + +PieceSlice +collect_pieces (const char *input); + +} // extern "C"