From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 1643) id 48D2D38387FB; Wed, 8 Jun 2022 12:09:16 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 48D2D38387FB Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit From: Thomas Schwinge To: gcc-cvs@gcc.gnu.org Subject: [gcc/devel/rust/master] Refactor type name resolution into their own .cc file X-Act-Checkin: gcc X-Git-Author: Philip Herron X-Git-Refname: refs/heads/devel/rust/master X-Git-Oldrev: fbe22e87687c68357430e60361a8a124c81148cc X-Git-Newrev: 78cdc6ca310181ef4855841e9feefccb93f79816 Message-Id: <20220608120916.48D2D38387FB@sourceware.org> Date: Wed, 8 Jun 2022 12:09:16 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 08 Jun 2022 12:09:16 -0000 https://gcc.gnu.org/g:78cdc6ca310181ef4855841e9feefccb93f79816 commit 78cdc6ca310181ef4855841e9feefccb93f79816 Author: Philip Herron Date: Fri Feb 18 12:46:44 2022 +0000 Refactor type name resolution into their own .cc file Diff: --- gcc/rust/Make-lang.in | 1 + gcc/rust/resolve/rust-ast-resolve-type.cc | 201 ++++++++++++++++++++++++++++++ gcc/rust/resolve/rust-ast-resolve-type.h | 26 +--- gcc/rust/resolve/rust-ast-resolve.cc | 147 ---------------------- 4 files changed, 205 insertions(+), 170 deletions(-) diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index 883e133daec..6339ac08b2b 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -81,6 +81,7 @@ GRS_OBJS = \ rust/rust-ast-lower-pattern.o \ rust/rust-ast-resolve.o \ rust/rust-ast-resolve-pattern.o \ + rust/rust-ast-resolve-type.o \ rust/rust-hir-type-check.o \ rust/rust-tyty.o \ rust/rust-tyctx.o \ diff --git a/gcc/rust/resolve/rust-ast-resolve-type.cc b/gcc/rust/resolve/rust-ast-resolve-type.cc new file mode 100644 index 00000000000..271c6fa775c --- /dev/null +++ b/gcc/rust/resolve/rust-ast-resolve-type.cc @@ -0,0 +1,201 @@ +// Copyright (C) 2020-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 +// . + +#include "rust-ast-resolve-type.h" +#include "rust-ast-resolve-expr.h" + +namespace Rust { +namespace Resolver { + +// rust-ast-resolve-type.h + +std::string +ResolveTypeToCanonicalPath::canonicalize_generic_args (AST::GenericArgs &args) +{ + std::string buf; + + size_t i = 0; + size_t total = args.get_type_args ().size (); + + for (auto &ty_arg : args.get_type_args ()) + { + buf += ty_arg->as_string (); + if ((i + 1) < total) + buf += ","; + + i++; + } + + return "<" + buf + ">"; +} + +bool +ResolveTypeToCanonicalPath::type_resolve_generic_args (AST::GenericArgs &args) +{ + for (auto > : args.get_type_args ()) + { + ResolveType::go (gt.get (), UNKNOWN_NODEID); + // FIXME error handling here for inference variable since they do not have + // a node to resolve to + // if (resolved == UNKNOWN_NODEID) return false; + } + return true; +} + +void +ResolveTypeToCanonicalPath::visit (AST::TypePathSegmentGeneric &seg) +{ + if (seg.is_error ()) + { + failure_flag = true; + rust_error_at (seg.get_locus (), "segment has error: %s", + seg.as_string ().c_str ()); + return; + } + + if (!seg.has_generic_args ()) + { + auto ident_segment + = CanonicalPath::new_seg (seg.get_node_id (), + seg.get_ident_segment ().as_string ()); + result = result.append (ident_segment); + return; + } + + if (type_resolve_generic_args_flag) + { + bool ok = type_resolve_generic_args (seg.get_generic_args ()); + failure_flag = !ok; + } + + if (include_generic_args_flag) + { + std::string generics + = canonicalize_generic_args (seg.get_generic_args ()); + auto generic_segment + = CanonicalPath::new_seg (seg.get_node_id (), + seg.get_ident_segment ().as_string () + + "::" + generics); + result = result.append (generic_segment); + return; + } + + auto ident_segment + = CanonicalPath::new_seg (seg.get_node_id (), + seg.get_ident_segment ().as_string ()); + result = result.append (ident_segment); +} + +void +ResolveTypeToCanonicalPath::visit (AST::TypePathSegment &seg) +{ + if (seg.is_error ()) + { + failure_flag = true; + rust_error_at (seg.get_locus (), "segment has error: %s", + seg.as_string ().c_str ()); + return; + } + + CanonicalPath ident_seg + = CanonicalPath::new_seg (seg.get_node_id (), + seg.get_ident_segment ().as_string ()); + result = result.append (ident_seg); +} + +void +ResolveType::visit (AST::ArrayType &type) +{ + type.get_elem_type ()->accept_vis (*this); + // FIXME + // the capacity expr can contain block-expr with functions but these should be + // folded via constexpr code + ResolveExpr::go (type.get_size_expr ().get (), type.get_node_id (), + CanonicalPath::create_empty (), + CanonicalPath::create_empty ()); +} + +void +ResolveType::visit (AST::TraitObjectTypeOneBound &type) +{ + NodeId bound_resolved_id + = ResolveTypeBound::go (&type.get_trait_bound (), type.get_node_id ()); + ok = bound_resolved_id != UNKNOWN_NODEID; +} + +void +ResolveType::visit (AST::TraitObjectType &type) +{ + ok = true; + for (auto &bound : type.get_type_param_bounds ()) + { + /* NodeId bound_resolved_id = */ + ResolveTypeBound::go (bound.get (), type.get_node_id ()); + } +} + +void +ResolveTypeToCanonicalPath::visit (AST::ReferenceType &ref) +{ + auto inner_type + = ResolveTypeToCanonicalPath::resolve (*ref.get_type_referenced ().get (), + include_generic_args_flag, + type_resolve_generic_args_flag); + + std::string segment_string ("&"); + if (ref.get_has_mut ()) + segment_string += "mut "; + + segment_string += inner_type.get (); + + auto ident_seg = CanonicalPath::new_seg (ref.get_node_id (), segment_string); + result = result.append (ident_seg); +} + +void +ResolveType::visit (AST::ReferenceType &type) +{ + type.get_type_referenced ()->accept_vis (*this); + + if (canonical_path != nullptr && canonical_path->size () > 0) + { + std::string seg = canonical_path->get (); + *canonical_path = CanonicalPath::new_seg (type.get_node_id (), "&" + seg); + } +} + +void +ResolveType::visit (AST::RawPointerType &type) +{ + type.get_type_pointed_to ()->accept_vis (*this); + + if (canonical_path != nullptr && canonical_path->size () > 0) + { + std::string seg = canonical_path->get (); + *canonical_path = CanonicalPath::new_seg (type.get_node_id (), "*" + seg); + } +} + +void +ResolveType::visit (AST::InferredType &type) +{ + ok = true; +} + +} // namespace Resolver +} // namespace Rust diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h b/gcc/rust/resolve/rust-ast-resolve-type.h index f7bdc9efb4e..2649e12bdaa 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.h +++ b/gcc/rust/resolve/rust-ast-resolve-type.h @@ -382,31 +382,11 @@ public: void visit (AST::ArrayType &type) override; - void visit (AST::ReferenceType &type) override - { - type.get_type_referenced ()->accept_vis (*this); - - if (canonical_path != nullptr && canonical_path->size () > 0) - { - std::string seg = canonical_path->get (); - *canonical_path - = CanonicalPath::new_seg (type.get_node_id (), "&" + seg); - } - } + void visit (AST::ReferenceType &type) override; - void visit (AST::InferredType &type) override { ok = true; } + void visit (AST::InferredType &type) override; - void visit (AST::RawPointerType &type) override - { - type.get_type_pointed_to ()->accept_vis (*this); - - if (canonical_path != nullptr && canonical_path->size () > 0) - { - std::string seg = canonical_path->get (); - *canonical_path - = CanonicalPath::new_seg (type.get_node_id (), "*" + seg); - } - } + void visit (AST::RawPointerType &type) override; void visit (AST::TraitObjectTypeOneBound &type) override; diff --git a/gcc/rust/resolve/rust-ast-resolve.cc b/gcc/rust/resolve/rust-ast-resolve.cc index 3fb8b41d053..8f003e5fa5d 100644 --- a/gcc/rust/resolve/rust-ast-resolve.cc +++ b/gcc/rust/resolve/rust-ast-resolve.cc @@ -448,120 +448,6 @@ ResolveStructExprField::visit (AST::StructExprFieldIdentifier &field) ResolveExpr::go (&expr, field.get_node_id (), prefix, canonical_prefix); } -// rust-ast-resolve-type.h - -std::string -ResolveTypeToCanonicalPath::canonicalize_generic_args (AST::GenericArgs &args) -{ - std::string buf; - - size_t i = 0; - size_t total = args.get_type_args ().size (); - - for (auto &ty_arg : args.get_type_args ()) - { - buf += ty_arg->as_string (); - if ((i + 1) < total) - buf += ","; - - i++; - } - - return "<" + buf + ">"; -} - -bool -ResolveTypeToCanonicalPath::type_resolve_generic_args (AST::GenericArgs &args) -{ - for (auto > : args.get_type_args ()) - { - ResolveType::go (gt.get (), UNKNOWN_NODEID); - // FIXME error handling here for inference variable since they do not have - // a node to resolve to - // if (resolved == UNKNOWN_NODEID) return false; - } - return true; -} - -void -ResolveTypeToCanonicalPath::visit (AST::TypePathSegmentGeneric &seg) -{ - if (seg.is_error ()) - { - failure_flag = true; - rust_error_at (seg.get_locus (), "segment has error: %s", - seg.as_string ().c_str ()); - return; - } - - if (!seg.has_generic_args ()) - { - auto ident_segment - = CanonicalPath::new_seg (seg.get_node_id (), - seg.get_ident_segment ().as_string ()); - result = result.append (ident_segment); - return; - } - - if (type_resolve_generic_args_flag) - { - bool ok = type_resolve_generic_args (seg.get_generic_args ()); - failure_flag = !ok; - } - - if (include_generic_args_flag) - { - std::string generics - = canonicalize_generic_args (seg.get_generic_args ()); - auto generic_segment - = CanonicalPath::new_seg (seg.get_node_id (), - seg.get_ident_segment ().as_string () - + "::" + generics); - result = result.append (generic_segment); - return; - } - - auto ident_segment - = CanonicalPath::new_seg (seg.get_node_id (), - seg.get_ident_segment ().as_string ()); - result = result.append (ident_segment); -} - -void -ResolveTypeToCanonicalPath::visit (AST::TypePathSegment &seg) -{ - if (seg.is_error ()) - { - failure_flag = true; - rust_error_at (seg.get_locus (), "segment has error: %s", - seg.as_string ().c_str ()); - return; - } - - CanonicalPath ident_seg - = CanonicalPath::new_seg (seg.get_node_id (), - seg.get_ident_segment ().as_string ()); - result = result.append (ident_seg); -} - -void -ResolveTypeToCanonicalPath::visit (AST::ReferenceType &ref) -{ - auto inner_type - = ResolveTypeToCanonicalPath::resolve (*ref.get_type_referenced ().get (), - include_generic_args_flag, - type_resolve_generic_args_flag); - - std::string segment_string ("&"); - if (ref.get_has_mut ()) - segment_string += "mut "; - - segment_string += inner_type.get (); - - auto ident_seg = CanonicalPath::new_seg (ref.get_node_id (), segment_string); - result = result.append (ident_seg); -} - // rust-ast-resolve-expr.h void @@ -799,39 +685,6 @@ ResolvePath::resolve_segments (CanonicalPath prefix, size_t offs, } } -// rust-ast-resolve-type.h - -void -ResolveType::visit (AST::ArrayType &type) -{ - type.get_elem_type ()->accept_vis (*this); - // FIXME - // the capacity expr can contain block-expr with functions but these should be - // folded via constexpr code - ResolveExpr::go (type.get_size_expr ().get (), type.get_node_id (), - CanonicalPath::create_empty (), - CanonicalPath::create_empty ()); -} - -void -ResolveType::visit (AST::TraitObjectTypeOneBound &type) -{ - NodeId bound_resolved_id - = ResolveTypeBound::go (&type.get_trait_bound (), type.get_node_id ()); - ok = bound_resolved_id != UNKNOWN_NODEID; -} - -void -ResolveType::visit (AST::TraitObjectType &type) -{ - ok = true; - for (auto &bound : type.get_type_param_bounds ()) - { - /* NodeId bound_resolved_id = */ - ResolveTypeBound::go (bound.get (), type.get_node_id ()); - } -} - // rust-ast-resolve-item.h void