From patchwork Thu Aug 1 14:56:25 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 1967736 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=BFGdDOmh; dkim-atps=neutral Authentication-Results: legolas.ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; 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 [8.43.85.97]) (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 4WZXRL3CS8z1ybX for ; Fri, 2 Aug 2024 01:08:46 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id B135C385E003 for ; Thu, 1 Aug 2024 15:08:44 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-ed1-x52d.google.com (mail-ed1-x52d.google.com [IPv6:2a00:1450:4864:20::52d]) by sourceware.org (Postfix) with ESMTPS id 17209385DDC0 for ; Thu, 1 Aug 2024 14:58:55 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 17209385DDC0 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 17209385DDC0 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=2a00:1450:4864:20::52d ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524351; cv=none; b=usYtclNY2PtUonr9obXNVfd3b6jxPnVPwvHc+jOMXF+S4/+VdOwEDFSInDCoG91yHF2UHLZGEOP2a0AEi5kLr75GPznx5SiGXC5MaxOmwwz/ZUqiTHRnTz69eTgC4RMesGQul3935qFOUJOThxYpFshyEPd1f7nUnKmhhoaDTwI= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1722524351; c=relaxed/simple; bh=r4J810mx0LOFNgyqecQs/o7IA+shlLASXBRLIhochPY=; h=DKIM-Signature:From:To:Subject:Date:Message-ID:MIME-Version; b=n81VM6CRGAGGKnvgcbxgPn85ma+hnyG/IOtH6mXXAcCpU9VtKbtQrVF9mTQ19KBq/gNQP0DFPCZOMZNGa9qThAfB/18jX1Gy5Cd4MV1raBm3Y0/OgG5IOGhF2q3pgVQe/5lA0+x/9si8sdKlZaHdquU0SbrCGS8qCZrFp2P0S+0= ARC-Authentication-Results: i=1; server2.sourceware.org Received: by mail-ed1-x52d.google.com with SMTP id 4fb4d7f45d1cf-5ab2baf13d9so9935499a12.2 for ; Thu, 01 Aug 2024 07:58:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=embecosm.com; s=google; t=1722524333; x=1723129133; 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=me5qr7rhd0ZRXNMzoarhYJ+uz/u20rZVe+UY3+Fk3qs=; b=BFGdDOmhsjO/hMI7euySb1Qmlhd/iWaqdDwQFzs0l12sXPjWmOjiyy9+xAoXSIiliE Dnvz1415SgvD6q3l+zB2S8Ho2I6JocwGuARmmm90futoULn/AAprskW+AWm+hgh8OeAq J49VMnzVvI8tal+kAc/uoHqEsHim4UAvVNdEOE+yn0VXtH20Kmnx4Ax24PbGapCd/2V9 p7mkNOQgVwei/sWF7/l5ckdn1DZIQeGFTOseywZLZp5TifWOf9+aWoxsmho9BxNBl7vS Yyyz+NXE5ksKeLVo9wV/w8Av/W+kf2Uk5Pyok9lTJ7SD211a6K0aUdLhIKoLWS4bLEZ6 Vlgw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722524333; x=1723129133; 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=me5qr7rhd0ZRXNMzoarhYJ+uz/u20rZVe+UY3+Fk3qs=; b=eP5s7BOJu10GyVQh+JXldX+gRpZhfy+j4N5QTbilhVnZVfWP1W4TAv20iZq+wzGycd QMBQ3PV/DBQa+O1ti8fTDn+CD0NmQaA6+TT1eDml4HT5ZosgP119e4SSN+c/TkLnYYWM RHanliPuH4fVGg/vSXU43uDfZpofo8c/KNiXjKvGLFylV55VnfNc0wvi3u3E45e39DO+ QI2qyZ1dbvyNOBP+XGLrR7s6jv/FHAKIhqsxn9f0qB5UvNQzy0fQw+IN+/CDgmcnTkwG 6ZdiEbdzOE+V9O7Nji2yEXAA7zdTr6KrIk7bE1H7vfe/aIN+XbPrxDVAbWvg1PoPLQQ0 QK9g== X-Gm-Message-State: AOJu0YyE5k9JohwqCP+fctk9pB2O/Eh+TPiHpmxj2ydxJCzZm4C4Vy4c ChFdET+9rhjat1tR1tOQBgoDC4YfA/E4Ar9HoPUe5xd/ffrRkcUU1n8uakWPekHV1XOgqrVj9Dk zwOKA X-Google-Smtp-Source: AGHT+IHlBlwoxswPnOdntEJnp5EV3Cjr6F8O6yxG8ju4Q98TpJe8X5Ud2ybKGfFZB9Aa2EDJeCzcMw== X-Received: by 2002:aa7:c449:0:b0:5a2:6350:75ac with SMTP id 4fb4d7f45d1cf-5b7f35fed0dmr436092a12.8.1722524333475; Thu, 01 Aug 2024 07:58:53 -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.52 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 01 Aug 2024 07:58:52 -0700 (PDT) From: Arthur Cohen To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Arthur Cohen Subject: [PATCH 029/125] gccrs: macro-builtins: Add newline generic format_args!() handler Date: Thu, 1 Aug 2024 16:56:25 +0200 Message-ID: <20240801145809.366388-31-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, 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.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 gcc/rust/ChangeLog: * expand/rust-macro-builtins.cc (format_args_maker): New function. (try_expand_many_expr): Add comment about reworking function. (MacroBuiltin::format_args_handler): Add newline parameter. * expand/rust-macro-builtins.h: Likewise. --- gcc/rust/expand/rust-macro-builtins.cc | 69 +++++++++++++++++++++++--- gcc/rust/expand/rust-macro-builtins.h | 4 +- 2 files changed, 65 insertions(+), 8 deletions(-) diff --git a/gcc/rust/expand/rust-macro-builtins.cc b/gcc/rust/expand/rust-macro-builtins.cc index f103759acdd..9e6716c5975 100644 --- a/gcc/rust/expand/rust-macro-builtins.cc +++ b/gcc/rust/expand/rust-macro-builtins.cc @@ -17,6 +17,8 @@ // . #include "libproc_macro_internal/tokenstream.h" +#include "rust-ast-full-decls.h" +#include "rust-builtin-ast-nodes.h" #include "rust-token-converter.h" #include "rust-system.h" #include "rust-macro-builtins.h" @@ -78,6 +80,14 @@ const BiMap MacroBuiltin::builtins = {{ }}; +AST::MacroTranscriberFunc +format_args_maker (AST::FormatArgs::Newline nl) +{ + return [nl] (location_t loc, AST::MacroInvocData &invoc) { + return MacroBuiltin::format_args_handler (loc, invoc, nl); + }; +} + std::unordered_map MacroBuiltin::builtin_transcribers = { {"assert", MacroBuiltin::assert_handler}, @@ -92,10 +102,10 @@ std::unordered_map {"env", MacroBuiltin::env_handler}, {"cfg", MacroBuiltin::cfg_handler}, {"include", MacroBuiltin::include_handler}, - {"format_args", MacroBuiltin::format_args_handler}, + {"format_args", format_args_maker (AST::FormatArgs::Newline::No)}, + {"format_args_nl", format_args_maker (AST::FormatArgs::Newline::Yes)}, /* Unimplemented macro builtins */ {"option_env", MacroBuiltin::sorry}, - {"format_args_nl", MacroBuiltin::sorry}, {"concat_idents", MacroBuiltin::sorry}, {"module_path", MacroBuiltin::sorry}, {"asm", MacroBuiltin::sorry}, @@ -286,6 +296,8 @@ try_expand_many_expr (Parser &parser, and return the LiteralExpr for it. Allow for an optional trailing comma, but otherwise enforce that these are the only tokens. */ +// FIXME(Arthur): This function needs a rework - it should not emit errors, it +// should probably be smaller std::unique_ptr parse_single_string_literal (BuiltinMacro kind, AST::DelimTokenTree &invoc_token_tree, @@ -946,17 +958,31 @@ MacroBuiltin::stringify_handler (location_t invoc_locus, tl::optional MacroBuiltin::format_args_handler (location_t invoc_locus, - AST::MacroInvocData &invoc) + AST::MacroInvocData &invoc, + AST::FormatArgs::Newline nl) { + // Remove the delimiters from the macro invocation: + // the invoc data for `format_args!(fmt, arg1, arg2)` is `(fmt, arg1, arg2)`, + // so we pop the front and back to remove the parentheses (or curly brackets, + // or brackets) auto tokens = invoc.get_delim_tok_tree ().to_token_stream (); tokens.erase (tokens.begin ()); tokens.pop_back (); - std::stringstream stream; - for (const auto &tok : tokens) - stream << tok->as_string () << ' '; + auto append_newline = nl == AST::FormatArgs::Newline::Yes ? true : false; + auto fmt_arg + = parse_single_string_literal (append_newline ? BuiltinMacro::FormatArgsNl + : BuiltinMacro::FormatArgs, + invoc.get_delim_tok_tree (), invoc_locus, + invoc.get_expander ()); - rust_debug ("[ARTHU]: `%s`", stream.str ().c_str ()); + if (!fmt_arg->is_literal ()) + { + rust_sorry_at ( + invoc_locus, + "cannot yet use eager macro invocations as format strings"); + return AST::Fragment::create_empty (); + } // FIXME: We need to handle this // // if it is not a literal, it's an eager macro invocation - return it @@ -967,8 +993,37 @@ MacroBuiltin::format_args_handler (location_t invoc_locus, // token_tree.to_token_stream ()); // } + auto fmt_str = static_cast (*fmt_arg.get ()); + + // Switch on the format string to know if the string is raw or cooked + switch (fmt_str.get_lit_type ()) + { + // case AST::Literal::RAW_STRING: + case AST::Literal::STRING: + break; + case AST::Literal::CHAR: + case AST::Literal::BYTE: + case AST::Literal::BYTE_STRING: + case AST::Literal::INT: + case AST::Literal::FLOAT: + case AST::Literal::BOOL: + case AST::Literal::ERROR: + rust_unreachable (); + } + + std::stringstream stream; + for (const auto &tok : tokens) + stream << tok->as_string () << ' '; + + rust_debug ("[ARTHUR]: `%s`", stream.str ().c_str ()); + auto pieces = Fmt::Pieces::collect (stream.str ()); + // TODO: + // do the transformation into an AST::FormatArgs node + // return that + // expand it during lowering + return AST::Fragment::create_empty (); } diff --git a/gcc/rust/expand/rust-macro-builtins.h b/gcc/rust/expand/rust-macro-builtins.h index 1d6b30b5933..62961561716 100644 --- a/gcc/rust/expand/rust-macro-builtins.h +++ b/gcc/rust/expand/rust-macro-builtins.h @@ -20,6 +20,7 @@ #define RUST_MACRO_BUILTINS_H #include "rust-ast.h" +#include "rust-builtin-ast-nodes.h" #include "rust-ast-fragment.h" #include "rust-location.h" #include "bi-map.h" @@ -158,7 +159,8 @@ public: AST::MacroInvocData &invoc); static tl::optional - format_args_handler (location_t invoc_locus, AST::MacroInvocData &invoc); + format_args_handler (location_t invoc_locus, AST::MacroInvocData &invoc, + AST::FormatArgs::Newline nl); static tl::optional sorry (location_t invoc_locus, AST::MacroInvocData &invoc);