From patchwork Tue Feb 21 12:02:03 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Arthur Cohen X-Patchwork-Id: 60023 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:adf:eb09:0:0:0:0:0 with SMTP id s9csp1838237wrn; Tue, 21 Feb 2023 04:40:18 -0800 (PST) X-Google-Smtp-Source: AK7set+4wBt6kmr5E4NFSH5Kz72yGlODNfbRToudk3ENb6XmMYFShkPB3zcpNICXDTY5J/aamCJy X-Received: by 2002:a17:906:f8cf:b0:8b1:78b8:4208 with SMTP id lh15-20020a170906f8cf00b008b178b84208mr13727494ejb.1.1676983218705; Tue, 21 Feb 2023 04:40:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1676983218; cv=none; d=google.com; s=arc-20160816; b=zDjZlWCkdcp5MM0tAxdnkovfgetVlmuATz0JbasncUyx2nuEu84b3swImBt7lCbO4M o0m3VzmXvj+vr32Ro0d2OWc/QY+GMy6TQiM8p+9DB+eRkSPVqXgs9trqLDEX/oZCIjYv SGcV2sWQX1TUuKnn1x6EjTr4EdZ/AtJ2llxXGZJvHgIzmK3BBoPvgA6iM5obj5XbjbVG aSmsxSfdOmrQOOnYLIwxUcSyr/htatC/LyHgxzYN9aelH/xsKwZNs9efNi4DDBKrf8Ti gt2t5lxuQyEOmv24h8/FmLxWycpiw3nKp/uPa7IMjpp3d6csJnxPXu8KWVhoEUrqGpCu JKww== 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=BjAHtJds/dYXJHqzSlCFG7powDL7LnfaotJl7XWauHQ=; b=zLczB8U1RvsAczb4qR6sU7oeVhMXD+NrdzaQ98qwhL1pAE+XTCTIN6kuHfBacx9W2s xuTUA55NVxfgaNS3xye7t/cJYthDWzuLfEFvZN9KBhAH9crxTqZZtkqEHp5gOmCxw2av zRW6lBiRa+RoDbwxnmgUfGYY263wigqPlKrlB3KAd8rl6Avlc/8R/ZczIBeBftqReK1u /VVPnXup/T/zbz/dKX0MJxXx9J93MAWun9PWbVRIFkfL/CBndnLitj/Lq+cGcymot4s+ Fq8mh2esUh4hJm63ZcPoZHGUvAYmeDMpHPY1tOcTbPoBsCk+mNtOLL83mZpME2nefixU wIdw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@embecosm.com header.s=google header.b=QmdA0XRi; 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 z7-20020a17090674c700b008d2e6df3b6dsi8653653ejl.712.2023.02.21.04.40.18 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:40:18 -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=QmdA0XRi; 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 C55653995BD6 for ; Tue, 21 Feb 2023 12:13:58 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-wr1-x42f.google.com (mail-wr1-x42f.google.com [IPv6:2a00:1450:4864:20::42f]) by sourceware.org (Postfix) with ESMTPS id 285883881D00 for ; Tue, 21 Feb 2023 12:04:24 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 285883881D00 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-x42f.google.com with SMTP id c12so4164565wrw.1 for ; Tue, 21 Feb 2023 04:04:24 -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=BjAHtJds/dYXJHqzSlCFG7powDL7LnfaotJl7XWauHQ=; b=QmdA0XRisWXTUx4ygAp47xADIyE/L1xy3voPJjah9U8d8wh370oBneKfvfI4WX0FCV k3hvFG+rJRXw2l3bzeduoYzp7x3r8A0IXCohNTjzxAOTyM+q+ttfliqH7zJeujBfLFBH kwLZF1PXdNuIJHqMg9lsTaNcLxNtBlAxmSyxNAGjwhy7sXxRcEVAeq29ds4sc1JIJsey 6WSgC4DpZoYWQ+evfNdrSwODgD3DH+vyhlFRDskctBi2nNKirtN1WKNrgBSWvG2O5QLD pAfe5eayyRmVmMqJOT/JTwTTajHLO4gP4I0rqAUc/KMdDfl3TqxhlN9qwnPY+2i/CJ8+ xVVg== 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=BjAHtJds/dYXJHqzSlCFG7powDL7LnfaotJl7XWauHQ=; b=SwXUkRS3Sxq2+jo1kbGD/ign6P5K3xGz+SskSGrJajhO69/WYR9MrKWYzbHchL/0Yu kwcjxfsCYm80utkzKwNX+0sh4L9W2h1yXVn4+hQxKlJQh54pipjEutP38zMPhdrpUisj LoABKHJmrYN89tnRf9kMN+k4vF4aqiqSvkpDuJpYnENIP5utf/BRooOr5JhWgnUkeTmH dhyq7c1x/jSdnY1Uq6SjtNJaDfbpO+kLidSmxNXSsZ5UtvKhlZC1dhCIddgpl/gsZVTg GSHu88fyfCdJ4j2jCQcuy73H7Dxo5CU2fZxtBj+bTPDb3igGeVtfgcPQrGFltuFRNODu rjQQ== X-Gm-Message-State: AO0yUKWFNi39ioLvGC43qzt9iZjmsXJaW/Aa69G45LZXwpXHZvU9tf0b oH8bW+Mnty82nYZsI8O35A9HBEcW8VFIdaHGcw== X-Received: by 2002:a05:6000:c1:b0:2c5:5308:859c with SMTP id q1-20020a05600000c100b002c55308859cmr2351680wrx.18.1676981063456; Tue, 21 Feb 2023 04:04:23 -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.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 21 Feb 2023 04:04:23 -0800 (PST) From: arthur.cohen@embecosm.com To: gcc-patches@gcc.gnu.org Cc: gcc-rust@gcc.gnu.org, Jakub Dupak Subject: [committed 073/103] gccrs: ast: Dump where clause and recursively needed nodes Date: Tue, 21 Feb 2023 13:02:03 +0100 Message-Id: <20230221120230.596966-74-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.7 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.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?1758444355713773659?= X-GMAIL-MSGID: =?utf-8?q?1758444355713773659?= From: Jakub Dupak This is currently needed for lifetimes to use the existing infrastructure. gcc/rust/ChangeLog: * ast/rust-ast-dump.cc (Dump::visit): Add missing visitors. * ast/rust-ast-dump.h: Likewise. * ast/rust-ast.h: Add `get_lifetime_bounds` method. * ast/rust-item.h: Add missing getter for lifetimes. * ast/rust-type.h: Likewise. Signed-off-by: Jakub Dupak --- gcc/rust/ast/rust-ast-dump.cc | 140 +++++++++++++++++++++++++++++----- gcc/rust/ast/rust-ast-dump.h | 2 + gcc/rust/ast/rust-ast.h | 2 + gcc/rust/ast/rust-item.h | 2 + gcc/rust/ast/rust-type.h | 2 + 5 files changed, 131 insertions(+), 17 deletions(-) diff --git a/gcc/rust/ast/rust-ast-dump.cc b/gcc/rust/ast/rust-ast-dump.cc index 16c4a79dc6e..e5e051a46d8 100644 --- a/gcc/rust/ast/rust-ast-dump.cc +++ b/gcc/rust/ast/rust-ast-dump.cc @@ -227,6 +227,28 @@ Dump::visit (StructField &field) visit (field.get_field_type ()); } +// TODO is this unique by type? +void +Dump::visit (std::vector &for_lifetimes) +{ + // ForLifetimes : + // for GenericParams + // + // GenericParams : + // < > + // | < (GenericParam ,)* GenericParam ,? > + // + // GenericParam : + // OuterAttribute* ( LifetimeParam | TypeParam | ConstParam ) + // + // LifetimeParam : + // LIFETIME_OR_LABEL ( : LifetimeBounds )? + + stream << "for <"; + visit_items_joined_by_separator (for_lifetimes, " + "); + stream << "> "; +} + void Dump::visit (Token &tok) { @@ -258,11 +280,35 @@ Dump::visit (IdentifierExpr &ident_expr) void Dump::visit (Lifetime &lifetime) -{} +{ + // Syntax: + // Lifetime : + // LIFETIME_OR_LABEL + // | 'static + // | '_ + stream << lifetime.as_string (); +} void Dump::visit (LifetimeParam &lifetime_param) -{} +{ + // Syntax: + // LIFETIME_OR_LABEL ( : LifetimeBounds )? + // LifetimeBounds : + // ( Lifetime + )* Lifetime? + + // TODO what to do with outer attr? They are not mentioned in the reference. + + auto lifetime = lifetime_param.get_lifetime (); + visit (lifetime); + + if (lifetime_param.has_lifetime_bounds ()) + { + stream << ": "; + visit_items_joined_by_separator (lifetime_param.get_lifetime_bounds (), + " + "); + } +} void Dump::visit (ConstGenericParam &lifetime_param) @@ -804,13 +850,52 @@ Dump::visit (TypeParam ¶m) } } +void +Dump::visit (WhereClause &rule) +{ + // Syntax: + // where ( WhereClauseItem , )* WhereClauseItem ? + // WhereClauseItem : + // LifetimeWhereClauseItem + // | TypeBoundWhereClauseItem + + stream << " where\n"; + indentation.increment (); + visit_items_as_lines (rule.get_items (), ","); + indentation.decrement (); +} + void Dump::visit (LifetimeWhereClauseItem &item) -{} +{ + // Syntax: + // Lifetime : LifetimeBounds + // LifetimeBounds : + // ( Lifetime + )* Lifetime? + + visit (item.get_lifetime ()); + stream << ": "; + visit_items_joined_by_separator (item.get_lifetime_bounds (), " + "); +} void Dump::visit (TypeBoundWhereClauseItem &item) -{} +{ + // Syntax: + // ForLifetimes? Type : TypeParamBounds? + // TypeParamBounds : + // TypeParamBound ( + TypeParamBound )* +? + // TypeParamBound : + // Lifetime | TraitBound + + if (item.has_for_lifetimes ()) + visit (item.get_for_lifetimes ()); + + visit (item.get_type ()); + stream << ": "; + + visit_items_joined_by_separator (item.get_type_param_bounds (), " + "); +} void Dump::visit (Method &method) @@ -896,6 +981,12 @@ Dump::visit (UseDeclaration &use_decl) void Dump::visit (Function &function) { + // Syntax: + // FunctionQualifiers fn IDENTIFIER GenericParams? + // ( FunctionParameters? ) + // FunctionReturnType? WhereClause? + // ( BlockExpression | ; ) + visit (function.get_visibility ()); stream << "fn " << function.get_function_name (); @@ -913,6 +1004,9 @@ Dump::visit (Function &function) stream << " "; } + if (function.has_where_clause ()) + visit (function.get_where_clause ()); + auto &block = function.get_definition (); if (!block) stream << ';'; @@ -936,8 +1030,7 @@ Dump::visit (TypeAlias &type_alias) if (type_alias.has_generics ()) visit (type_alias.get_generic_params ()); if (type_alias.has_where_clause ()) - { - } // FIXME: WhereClause + visit (type_alias.get_where_clause ()); stream << " = "; visit (type_alias.get_type_aliased ()); stream << ";\n"; @@ -949,9 +1042,8 @@ Dump::visit (StructStruct &struct_item) stream << "struct " << struct_item.get_identifier (); if (struct_item.has_generics ()) visit (struct_item.get_generic_params ()); - - // FIXME: where-clause - + if (struct_item.has_where_clause ()) + visit (struct_item.get_where_clause ()); if (struct_item.is_unit_struct ()) stream << ";\n"; else @@ -964,8 +1056,8 @@ Dump::visit (TupleStruct &tuple_struct) stream << "struct " << tuple_struct.get_identifier (); if (tuple_struct.has_generics ()) visit (tuple_struct.get_generic_params ()); - - // FIXME: where-clause + if (tuple_struct.has_where_clause ()) + visit (tuple_struct.get_where_clause ()); stream << '('; visit_items_joined_by_separator (tuple_struct.get_fields (), ", "); @@ -1006,8 +1098,8 @@ Dump::visit (Enum &enum_item) stream << "enum " << enum_item.get_identifier (); if (enum_item.has_generics ()) visit (enum_item.get_generic_params ()); - - // FIXME: where-clause + if (enum_item.has_where_clause ()) + visit (enum_item.get_where_clause ()); visit_items_as_block (enum_item.get_variants (), ","); } @@ -1018,8 +1110,8 @@ Dump::visit (Union &union_item) stream << "union " << union_item.get_identifier (); if (union_item.has_generics ()) visit (union_item.get_generic_params ()); - - // FIXME: where-clause + if (union_item.has_where_clause ()) + visit (union_item.get_where_clause ()); visit_items_as_block (union_item.get_variants (), ","); } @@ -1137,7 +1229,9 @@ Dump::visit (InherentImpl &impl) visit (impl.get_type ()); - // FIXME: Handle where-clause + if (impl.has_where_clause ()) + visit (impl.get_where_clause ()); + // FIXME: Handle inner attributes visit_items_as_block (impl.get_impl_items (), ""); @@ -1451,7 +1545,19 @@ Dump::visit (ExprStmtWithBlock &stmt) // rust-type.h void Dump::visit (TraitBound &bound) -{} +{ + // Syntax: + // ?? ForLifetimes? TypePath + // | ( ?? ForLifetimes? TypePath ) + + if (bound.has_opening_question_mark ()) + stream << "? "; + + if (bound.has_for_lifetimes ()) + visit (bound.get_for_lifetimes ()); + + visit (bound.get_type_path ()); +} void Dump::visit (ImplTraitType &type) diff --git a/gcc/rust/ast/rust-ast-dump.h b/gcc/rust/ast/rust-ast-dump.h index 57419b75347..2bd3b31d09a 100644 --- a/gcc/rust/ast/rust-ast-dump.h +++ b/gcc/rust/ast/rust-ast-dump.h @@ -135,6 +135,8 @@ private: void visit (SimplePathSegment &segment); void visit (NamedFunctionParam ¶m); void visit (MacroRule &rule); + void visit (WhereClause &rule); + void visit (std::vector &for_lifetimes); // rust-ast.h void visit (Token &tok); diff --git a/gcc/rust/ast/rust-ast.h b/gcc/rust/ast/rust-ast.h index 3d602b1a379..aa86f2f9826 100644 --- a/gcc/rust/ast/rust-ast.h +++ b/gcc/rust/ast/rust-ast.h @@ -1310,6 +1310,8 @@ public: // Returns whether the lifetime param has any lifetime bounds. bool has_lifetime_bounds () const { return !lifetime_bounds.empty (); } + std::vector &get_lifetime_bounds () { return lifetime_bounds; } + // Returns whether the lifetime param has an outer attribute. bool has_outer_attribute () const { return !outer_attr.is_empty (); } diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 4e237f2e21e..96201d668de 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -236,6 +236,8 @@ public: // Returns whether the item has ForLifetimes bool has_for_lifetimes () const { return !for_lifetimes.empty (); } + std::vector &get_for_lifetimes () { return for_lifetimes; } + // Returns whether the item has type param bounds bool has_type_param_bounds () const { return !type_param_bounds.empty (); } diff --git a/gcc/rust/ast/rust-type.h b/gcc/rust/ast/rust-type.h index 0b91cafbfd1..05a78956b57 100644 --- a/gcc/rust/ast/rust-type.h +++ b/gcc/rust/ast/rust-type.h @@ -46,6 +46,8 @@ public: // Returns whether trait bound has "for" lifetimes bool has_for_lifetimes () const { return !for_lifetimes.empty (); } + std::vector &get_for_lifetimes () { return for_lifetimes; } + TraitBound (TypePath type_path, Location locus, bool in_parens = false, bool opening_question_mark = false, std::vector for_lifetimes