diff options
author | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-04-29 13:47:25 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2022-05-04 16:01:44 +0200 |
commit | 1ea83c404e82d276524f8af3ac51bd23600bfd24 (patch) | |
tree | c73a9082aeec397c4b57658e79d1d15180dae4ea /gcc | |
parent | 285aabdd3b2d8ee7c158eee34241ba52a31b19d3 (diff) | |
download | gcc-1ea83c404e82d276524f8af3ac51bd23600bfd24.zip gcc-1ea83c404e82d276524f8af3ac51bd23600bfd24.tar.gz gcc-1ea83c404e82d276524f8af3ac51bd23600bfd24.tar.bz2 |
ast: Lower SimplePaths properly
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/Make-lang.in | 1 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.cc | 73 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-item.h | 9 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower.cc | 3 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-full-test.cc | 3 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-item.h | 9 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.h | 30 | ||||
-rw-r--r-- | gcc/rust/util/rust-hir-map.cc | 8 | ||||
-rw-r--r-- | gcc/rust/util/rust-hir-map.h | 16 |
9 files changed, 135 insertions, 17 deletions
diff --git a/gcc/rust/Make-lang.in b/gcc/rust/Make-lang.in index ae385e0..2aec8a9 100644 --- a/gcc/rust/Make-lang.in +++ b/gcc/rust/Make-lang.in @@ -85,6 +85,7 @@ GRS_OBJS = \ rust/rust-ast-lower.o \ rust/rust-ast-lower-base.o \ rust/rust-ast-lower-pattern.o \ + rust/rust-ast-lower-item.o \ rust/rust-name-resolver.o \ rust/rust-ast-resolve.o \ rust/rust-ast-resolve-base.o \ diff --git a/gcc/rust/hir/rust-ast-lower-item.cc b/gcc/rust/hir/rust-ast-lower-item.cc new file mode 100644 index 0000000..45072d3 --- /dev/null +++ b/gcc/rust/hir/rust-ast-lower-item.cc @@ -0,0 +1,73 @@ +// 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 +// <http://www.gnu.org/licenses/>. + +#include "rust-ast-lower-item.h" + +namespace Rust { +namespace HIR { + +HIR::SimplePath +ASTLoweringSimplePath::translate (const AST::SimplePath &path) +{ + ASTLoweringSimplePath resolver; + + return resolver.lower (path); +} + +HIR::SimplePathSegment +ASTLoweringSimplePath::lower (const AST::SimplePathSegment &segment) +{ + auto crate_num = mappings->get_current_crate (); + auto node_id = segment.get_node_id (); + + auto mapping = Analysis::NodeMapping (crate_num, node_id, + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + auto hir_seg = HIR::SimplePathSegment (mapping); + + mappings->insert_node_to_hir (crate_num, node_id, mapping.get_hirid ()); + mappings->insert_simple_path_segment (crate_num, node_id, &segment); + + return hir_seg; +} + +HIR::SimplePath +ASTLoweringSimplePath::lower (const AST::SimplePath &path) +{ + auto segments = std::vector<HIR::SimplePathSegment> (); + for (auto &segment : path.get_segments ()) + segments.emplace_back (lower (segment)); + + auto crate_num = mappings->get_current_crate (); + auto node_id = path.get_node_id (); + + auto mapping = Analysis::NodeMapping (crate_num, node_id, + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + + auto lowered = HIR::SimplePath (std::move (segments), mapping); + + mappings->insert_node_to_hir (crate_num, node_id, mapping.get_hirid ()); + mappings->insert_simple_path (crate_num, node_id, &path); + + return lowered; +} + +} // namespace HIR +} // namespace Rust diff --git a/gcc/rust/hir/rust-ast-lower-item.h b/gcc/rust/hir/rust-ast-lower-item.h index c926926..6da50fe 100644 --- a/gcc/rust/hir/rust-ast-lower-item.h +++ b/gcc/rust/hir/rust-ast-lower-item.h @@ -826,6 +826,15 @@ private: HIR::Item *translated; }; +class ASTLoweringSimplePath : public ASTLoweringBase +{ +public: + static HIR::SimplePath translate (const AST::SimplePath &path); + + HIR::SimplePathSegment lower (const AST::SimplePathSegment &segment); + HIR::SimplePath lower (const AST::SimplePath &path); +}; + } // namespace HIR } // namespace Rust diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc index bc613e1..a385051 100644 --- a/gcc/rust/hir/rust-ast-lower.cc +++ b/gcc/rust/hir/rust-ast-lower.cc @@ -46,7 +46,8 @@ translate_visibility (const AST::Visibility &vis) case AST::Visibility::PUB_CRATE: case AST::Visibility::PUB_SUPER: case AST::Visibility::PUB_IN_PATH: - return Visibility (Visibility::VisType::PUBLIC, vis.get_path ()); + return Visibility (Visibility::VisType::PUBLIC, + ASTLoweringSimplePath::translate (vis.get_path ())); break; } diff --git a/gcc/rust/hir/tree/rust-hir-full-test.cc b/gcc/rust/hir/tree/rust-hir-full-test.cc index a53210b..8ef1194 100644 --- a/gcc/rust/hir/tree/rust-hir-full-test.cc +++ b/gcc/rust/hir/tree/rust-hir-full-test.cc @@ -123,7 +123,8 @@ Visibility::as_string () const case PRIVATE: return std::string ("private"); case PUBLIC: - return std::string ("pub(in ") + path.as_string () + std::string (")"); + return std::string ("pub(in ") + path.get_mappings ().as_string () + + std::string (")"); default: gcc_unreachable (); } diff --git a/gcc/rust/hir/tree/rust-hir-item.h b/gcc/rust/hir/tree/rust-hir-item.h index b3dc025..c1dcf76 100644 --- a/gcc/rust/hir/tree/rust-hir-item.h +++ b/gcc/rust/hir/tree/rust-hir-item.h @@ -562,14 +562,13 @@ public: private: VisType vis_type; - AST::SimplePath path; + HIR::SimplePath path; // should this store location info? public: - // Creates a Visibility - TODO make constructor protected or private? Visibility (VisType vis_type, - AST::SimplePath path = AST::SimplePath::create_empty ()) + HIR::SimplePath path = HIR::SimplePath::create_error ()) : vis_type (vis_type), path (std::move (path)) {} @@ -582,7 +581,9 @@ public: // Creates an error visibility. static Visibility create_error () { - return Visibility (ERROR, AST::SimplePath::create_empty ()); + return Visibility (ERROR, + HIR::SimplePath ({}, + Analysis::NodeMapping::get_error ())); } VisType get_vis_type () const { return vis_type; } diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index aa3e29a..e3ad838 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -946,6 +946,36 @@ public: Location get_locus () { return locus; } }; + +class SimplePathSegment +{ + Analysis::NodeMapping mappings; + +public: + SimplePathSegment (Analysis::NodeMapping mappings) : mappings (mappings) {} + + const Analysis::NodeMapping &get_mappings () const { return mappings; } +}; + +class SimplePath +{ + std::vector<SimplePathSegment> segments; + Analysis::NodeMapping mappings; + +public: + SimplePath (std::vector<SimplePathSegment> segments, + Analysis::NodeMapping mappings) + : segments (std::move (segments)), mappings (mappings) + {} + + static HIR::SimplePath create_error () + { + return HIR::SimplePath ({}, Analysis::NodeMapping::get_error ()); + } + + const Analysis::NodeMapping &get_mappings () const { return mappings; } +}; + } // namespace HIR } // namespace Rust diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 4062ce7..66d3e41 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -428,7 +428,7 @@ Mappings::lookup_hir_path_expr_seg (CrateNum crateNum, HirId id) void Mappings::insert_simple_path_segment (CrateNum crateNum, HirId id, - AST::SimplePathSegment *path) + const AST::SimplePathSegment *path) { rust_assert (lookup_simple_path_segment (crateNum, id) == nullptr); @@ -437,7 +437,7 @@ Mappings::insert_simple_path_segment (CrateNum crateNum, HirId id, insert_location (crateNum, id, path->get_locus ()); } -AST::SimplePathSegment * +const AST::SimplePathSegment * Mappings::lookup_simple_path_segment (CrateNum crateNum, HirId id) { auto it = astSimplePathSegmentMappings.find (crateNum); @@ -453,7 +453,7 @@ Mappings::lookup_simple_path_segment (CrateNum crateNum, HirId id) void Mappings::insert_simple_path (CrateNum crateNum, HirId id, - AST::SimplePath *path) + const AST::SimplePath *path) { rust_assert (lookup_simple_path (crateNum, id) == nullptr); @@ -462,7 +462,7 @@ Mappings::insert_simple_path (CrateNum crateNum, HirId id, insert_location (crateNum, id, path->get_locus ()); } -AST::SimplePath * +const AST::SimplePath * Mappings::lookup_simple_path (CrateNum crateNum, HirId id) { auto it = astSimplePathMappings.find (crateNum); diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 61b8c26..0bb870b 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -161,12 +161,13 @@ public: HIR::PathExprSegment *lookup_hir_path_expr_seg (CrateNum crateNum, HirId id); void insert_simple_path_segment (CrateNum crateNum, HirId id, - AST::SimplePathSegment *path); - AST::SimplePathSegment *lookup_simple_path_segment (CrateNum crateNum, - HirId id); + const AST::SimplePathSegment *path); + const AST::SimplePathSegment *lookup_simple_path_segment (CrateNum crateNum, + HirId id); - void insert_simple_path (CrateNum crateNum, HirId id, AST::SimplePath *path); - AST::SimplePath *lookup_simple_path (CrateNum crateNum, HirId id); + void insert_simple_path (CrateNum crateNum, HirId id, + const AST::SimplePath *path); + const AST::SimplePath *lookup_simple_path (CrateNum crateNum, HirId id); void insert_hir_generic_param (CrateNum crateNum, HirId id, HIR::GenericParam *expr); @@ -351,8 +352,9 @@ private: std::map<CrateNum, std::map<HirId, HIR::TraitItem *>> hirTraitItemMappings; std::map<CrateNum, std::map<HirId, HIR::ExternalItem *>> hirExternItemMappings; - std::map<CrateNum, std::map<HirId, AST::SimplePath *>> astSimplePathMappings; - std::map<CrateNum, std::map<HirId, AST::SimplePathSegment *>> + std::map<CrateNum, std::map<HirId, const AST::SimplePath *>> + astSimplePathMappings; + std::map<CrateNum, std::map<HirId, const AST::SimplePathSegment *>> astSimplePathSegmentMappings; std::map<CrateNum, std::map<HirId, HIR::PathExprSegment *>> hirPathSegMappings; |