aboutsummaryrefslogtreecommitdiff
path: root/gcc
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-08-11 11:52:16 +0100
committerPhilip Herron <philip.herron@embecosm.com>2021-08-11 12:53:33 +0100
commit06bfea60dd9bb2d1ecabc5a898f321a9114f6257 (patch)
tree912eb298372f247fdc8cd9dd387659f91db0a59c /gcc
parentb1b59b12509ccbafc9169888b1417e0f459a54ba (diff)
downloadgcc-06bfea60dd9bb2d1ecabc5a898f321a9114f6257.zip
gcc-06bfea60dd9bb2d1ecabc5a898f321a9114f6257.tar.gz
gcc-06bfea60dd9bb2d1ecabc5a898f321a9114f6257.tar.bz2
Refactor and extract CanonicalPath into a common header
CanonicalPath class and the node types are a common type which when split from the hir-mapping class decouples the headers and cleans things up.
Diffstat (limited to 'gcc')
-rw-r--r--gcc/rust/resolve/rust-name-resolver.h107
-rw-r--r--gcc/rust/util/rust-canonical-path.h137
-rw-r--r--gcc/rust/util/rust-hir-map.h22
-rw-r--r--gcc/rust/util/rust-mapping-common.h48
4 files changed, 187 insertions, 127 deletions
diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h
index 5f01b94..b4e394b 100644
--- a/gcc/rust/resolve/rust-name-resolver.h
+++ b/gcc/rust/resolve/rust-name-resolver.h
@@ -20,118 +20,13 @@
#define RUST_NAME_RESOLVER_H
#include "rust-system.h"
+#include "rust-canonical-path.h"
#include "rust-hir-map.h"
#include "rust-hir-type-check.h"
namespace Rust {
namespace Resolver {
-// https://doc.rust-lang.org/reference/paths.html#canonical-paths
-//
-// struct X - path X
-// impl X { fn test - path X::test }
-//
-// struct X<T> - path X
-//
-// impl X<T> { fn test - path X::test}
-// impl X<i32> { fn test - path X<i32>::test }
-// impl X<f32> { fn test - path X<f32>::test }
-//
-// pub trait Trait { // ::a::Trait
-// fn f(&self); // ::a::Trait::f
-// }
-//
-// impl Trait for Struct {
-// fn f(&self) {} // <::a::Struct as ::a::Trait>::f
-// }
-class CanonicalPath
-{
-public:
- CanonicalPath (const CanonicalPath &other) : segs (other.segs) {}
-
- CanonicalPath &operator= (const CanonicalPath &other)
- {
- segs = other.segs;
- return *this;
- }
-
- static CanonicalPath new_seg (NodeId id, const std::string &path)
- {
- rust_assert (!path.empty ());
- return CanonicalPath ({std::pair<NodeId, std::string> (id, path)});
- }
-
- std::string get () const
- {
- std::string buf;
- for (size_t i = 0; i < segs.size (); i++)
- {
- bool have_more = (i + 1) < segs.size ();
- const std::string &seg = segs.at (i).second;
- buf += seg + (have_more ? "::" : "");
- }
- return buf;
- }
-
- static CanonicalPath get_big_self (NodeId id)
- {
- return CanonicalPath::new_seg (id, "Self");
- }
-
- static CanonicalPath create_empty () { return CanonicalPath ({}); }
-
- bool is_empty () const { return segs.size () == 0; }
-
- CanonicalPath append (const CanonicalPath &other) const
- {
- rust_assert (!other.is_empty ());
- if (is_empty ())
- return CanonicalPath (other.segs);
-
- std::vector<std::pair<NodeId, std::string>> copy (segs);
- for (auto &s : other.segs)
- copy.push_back (s);
-
- return CanonicalPath (copy);
- }
-
- // if we have the path A::B::C this will give a callback for each segment
- // example:
- // A
- // A::B
- // A::B::C
- void iterate (std::function<bool (const CanonicalPath &)> cb) const
- {
- std::vector<std::pair<NodeId, std::string>> buf;
- for (auto &seg : segs)
- {
- buf.push_back (seg);
- if (!cb (CanonicalPath (buf)))
- return;
- }
- }
-
- NodeId get_id () const
- {
- rust_assert (!segs.empty ());
- return segs.back ().first;
- }
-
- bool operator== (const CanonicalPath &b) const
- {
- return get ().compare (b.get ()) == 0;
- }
-
- bool operator< (const CanonicalPath &b) const { return get () < b.get (); }
-
-private:
- explicit CanonicalPath (std::vector<std::pair<NodeId, std::string>> path)
- : segs (path)
- {}
-
- std::vector<std::pair<NodeId, std::string>> segs;
-};
-
class Rib
{
public:
diff --git a/gcc/rust/util/rust-canonical-path.h b/gcc/rust/util/rust-canonical-path.h
new file mode 100644
index 0000000..a2d6773
--- /dev/null
+++ b/gcc/rust/util/rust-canonical-path.h
@@ -0,0 +1,137 @@
+// Copyright (C) 2020 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/>.
+
+#ifndef RUST_CANONICAL_PATH
+#define RUST_CANONICAL_PATH
+
+#include "rust-system.h"
+#include "rust-mapping-common.h"
+
+namespace Rust {
+namespace Resolver {
+
+// https://doc.rust-lang.org/reference/paths.html#canonical-paths
+//
+// struct X - path X
+// impl X { fn test - path X::test }
+//
+// struct X<T> - path X
+//
+// impl X<T> { fn test - path X::test}
+// impl X<i32> { fn test - path X<i32>::test }
+// impl X<f32> { fn test - path X<f32>::test }
+//
+// pub trait Trait { // ::a::Trait
+// fn f(&self); // ::a::Trait::f
+// }
+//
+// impl Trait for Struct {
+// fn f(&self) {} // <::a::Struct as ::a::Trait>::f
+// }
+class CanonicalPath
+{
+public:
+ CanonicalPath (const CanonicalPath &other) : segs (other.segs) {}
+
+ CanonicalPath &operator= (const CanonicalPath &other)
+ {
+ segs = other.segs;
+ return *this;
+ }
+
+ static CanonicalPath new_seg (NodeId id, const std::string &path)
+ {
+ rust_assert (!path.empty ());
+ return CanonicalPath ({std::pair<NodeId, std::string> (id, path)});
+ }
+
+ std::string get () const
+ {
+ std::string buf;
+ for (size_t i = 0; i < segs.size (); i++)
+ {
+ bool have_more = (i + 1) < segs.size ();
+ const std::string &seg = segs.at (i).second;
+ buf += seg + (have_more ? "::" : "");
+ }
+ return buf;
+ }
+
+ static CanonicalPath get_big_self (NodeId id)
+ {
+ return CanonicalPath::new_seg (id, "Self");
+ }
+
+ static CanonicalPath create_empty () { return CanonicalPath ({}); }
+
+ bool is_empty () const { return segs.size () == 0; }
+
+ CanonicalPath append (const CanonicalPath &other) const
+ {
+ rust_assert (!other.is_empty ());
+ if (is_empty ())
+ return CanonicalPath (other.segs);
+
+ std::vector<std::pair<NodeId, std::string>> copy (segs);
+ for (auto &s : other.segs)
+ copy.push_back (s);
+
+ return CanonicalPath (copy);
+ }
+
+ // if we have the path A::B::C this will give a callback for each segment
+ // example:
+ // A
+ // A::B
+ // A::B::C
+ void iterate (std::function<bool (const CanonicalPath &)> cb) const
+ {
+ std::vector<std::pair<NodeId, std::string>> buf;
+ for (auto &seg : segs)
+ {
+ buf.push_back (seg);
+ if (!cb (CanonicalPath (buf)))
+ return;
+ }
+ }
+
+ NodeId get_id () const
+ {
+ rust_assert (!segs.empty ());
+ return segs.back ().first;
+ }
+
+ bool operator== (const CanonicalPath &b) const
+ {
+ return get ().compare (b.get ()) == 0;
+ }
+
+ bool operator< (const CanonicalPath &b) const { return get () < b.get (); }
+
+private:
+ explicit CanonicalPath (std::vector<std::pair<NodeId, std::string>> path)
+ : segs (path)
+ {}
+
+ std::vector<std::pair<NodeId, std::string>> segs;
+};
+
+} // namespace Resolver
+} // namespace Rust
+
+#endif // RUST_CANONICAL_PATH
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index f689abf..6fbb4d7 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -21,32 +21,12 @@
#include "rust-system.h"
#include "rust-location.h"
+#include "rust-mapping-common.h"
#include "rust-ast-full-decls.h"
#include "rust-hir-full-decls.h"
namespace Rust {
-
-// refers to a Crate
-typedef uint32_t CrateNum;
-// refers to any node in the AST in current Crate
-typedef uint32_t NodeId;
-// refers to any node in the HIR for the current crate
-typedef uint32_t HirId;
-// refers to any top-level decl in HIR
-typedef uint32_t LocalDefId;
-// refers to <Crate><DefId>
-typedef uint64_t DefId;
-
-#define DEF_ID_CRATE_MASK 0xFFFFFFFF00000000
-#define DEF_ID_LOCAL_DEF_MASK 0x00000000FFFFFFFF
-
-#define UNKNOWN_CREATENUM ((uint32_t) (0))
-#define UNKNOWN_NODEID ((uint32_t) (0))
-#define UNKNOWN_HIRID ((uint32_t) (0))
-#define UNKNOWN_LOCAL_DEFID ((uint32_t) (0))
-#define UNKNOWN_DEFID ((uint64_t) (0))
-
namespace Analysis {
class NodeMapping
diff --git a/gcc/rust/util/rust-mapping-common.h b/gcc/rust/util/rust-mapping-common.h
new file mode 100644
index 0000000..45af11b
--- /dev/null
+++ b/gcc/rust/util/rust-mapping-common.h
@@ -0,0 +1,48 @@
+// Copyright (C) 2020 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/>.
+
+#ifndef RUST_MAPPING_COMMON
+#define RUST_MAPPING_COMMON
+
+#include "rust-system.h"
+
+namespace Rust {
+
+// refers to a Crate
+typedef uint32_t CrateNum;
+// refers to any node in the AST in current Crate
+typedef uint32_t NodeId;
+// refers to any node in the HIR for the current crate
+typedef uint32_t HirId;
+// refers to any top-level decl in HIR
+typedef uint32_t LocalDefId;
+// refers to <Crate><DefId>
+typedef uint64_t DefId;
+
+#define DEF_ID_CRATE_MASK 0xFFFFFFFF00000000
+#define DEF_ID_LOCAL_DEF_MASK 0x00000000FFFFFFFF
+
+#define UNKNOWN_CREATENUM ((uint32_t) (0))
+#define UNKNOWN_NODEID ((uint32_t) (0))
+#define UNKNOWN_HIRID ((uint32_t) (0))
+#define UNKNOWN_LOCAL_DEFID ((uint32_t) (0))
+#define UNKNOWN_DEFID ((uint64_t) (0))
+
+} // namespace Rust
+
+#endif // RUST_MAPPING_COMMON