From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-x435.google.com (mail-wr1-x435.google.com [IPv6:2a00:1450:4864:20::435]) by sourceware.org (Postfix) with ESMTPS id E13813838156 for ; Tue, 21 Feb 2023 12:04:23 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E13813838156 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-x435.google.com with SMTP id l25so3759754wrb.3 for ; Tue, 21 Feb 2023 04:04:23 -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=ZRH2TGn+VN/wBx/asGvAiBuRmxzQitXb9KGw9D2juPTCv707Sap5huj2tOr3COM65Y vq4tY809PTYbxzKwZLVIN0wvWtz5jmJPp9Btxe+EpkcS6N3i6gWT9j49W5ripN+lZERf cR1LU8X1c4lNWV2/hgzljAZviVCJMia0tSW+Rm+wWTXemNRsBNWGkh2fP7OXICCrMPiT 5I7rGLftwnBmfkFWBrv1tGJn0Y59g1bzDE5+FFEr4mBiehGh8gc2EURN1uUAleCpsGIY 5ujbrVqjBHG8EdjGBTDf4y+r0el9jsnCm2vC6VcEP8cTI/ptHextbh2Gc8HshMeWa2Kf n4EA== X-Gm-Message-State: AO0yUKUsnfmW2zz7aSZ8VyzfiUeStGsPJdWtGniV65fL9aCHa/YzSHjf 5cRdtff9WCwzSYccPiG6Tryx0oUNdPAsmeqgwA== X-Google-Smtp-Source: AK7set/6Xs/48kAag/G1/Hjns1GJBmNgFIjXw1D/ZlYDWomytTZRjQUj2+T/+D9Q04Hjb1McGag0yA== 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> Reply-To: arthur.cohen@embecosm.com MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 -- 2.39.1