public inbox for gcc-cvs@sourceware.org
help / color / mirror / Atom feed
* [gcc/devel/rust/master] ast: Lower SimplePaths properly
@ 2022-06-08 12:43 Thomas Schwinge
  0 siblings, 0 replies; only message in thread
From: Thomas Schwinge @ 2022-06-08 12:43 UTC (permalink / raw)
  To: gcc-cvs

https://gcc.gnu.org/g:1ea83c404e82d276524f8af3ac51bd23600bfd24

commit 1ea83c404e82d276524f8af3ac51bd23600bfd24
Author: Arthur Cohen <arthur.cohen@embecosm.com>
Date:   Fri Apr 29 13:47:25 2022 +0200

    ast: Lower SimplePaths properly

Diff:
---
 gcc/rust/Make-lang.in                   |  1 +
 gcc/rust/hir/rust-ast-lower-item.cc     | 73 +++++++++++++++++++++++++++++++++
 gcc/rust/hir/rust-ast-lower-item.h      |  9 ++++
 gcc/rust/hir/rust-ast-lower.cc          |  3 +-
 gcc/rust/hir/tree/rust-hir-full-test.cc |  3 +-
 gcc/rust/hir/tree/rust-hir-item.h       |  9 ++--
 gcc/rust/hir/tree/rust-hir-path.h       | 30 ++++++++++++++
 gcc/rust/util/rust-hir-map.cc           |  8 ++--
 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 ae385e0bec9..2aec8a95a5a 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 00000000000..45072d36a7a
--- /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 c926926c26e..6da50fe4262 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 bc613e12e49..a3850514b09 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 a53210ba768..8ef11945b13 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 b3dc025319f..c1dcf7651f5 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 aa3e29a99c0..e3ad8384252 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 4062ce7e4e2..66d3e415471 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 61b8c26c9b9..0bb870b4910 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;


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2022-06-08 12:43 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-08 12:43 [gcc/devel/rust/master] ast: Lower SimplePaths properly Thomas Schwinge

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).