aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-08-11 12:50:52 +0100
committerPhilip Herron <philip.herron@embecosm.com>2021-08-11 12:54:25 +0100
commit5c828514d779745a329ee95d7c533db1d773972b (patch)
treea7756b9e7d369a3c90e2450d572074d9fd241e5d
parent06bfea60dd9bb2d1ecabc5a898f321a9114f6257 (diff)
downloadgcc-5c828514d779745a329ee95d7c533db1d773972b.zip
gcc-5c828514d779745a329ee95d7c533db1d773972b.tar.gz
gcc-5c828514d779745a329ee95d7c533db1d773972b.tar.bz2
Keep track of canonical paths in the mappings
We need to be able to lookup the canonical path for items to persue correct naming and name mangling in code generation. We allow for duplicates node-ids in the mapper so long as the paths are equal or assert and ignore the new path so long as it is shorter than the existing one. This is the case because we are inserting relative paths for names which will be shorter that the fully qualified path but also have the same NodeId
-rw-r--r--gcc/rust/resolve/rust-name-resolver.h3
-rw-r--r--gcc/rust/util/rust-canonical-path.h6
-rw-r--r--gcc/rust/util/rust-hir-map.h40
3 files changed, 48 insertions, 1 deletions
diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h
index b4e394b..1b26a46 100644
--- a/gcc/rust/resolve/rust-name-resolver.h
+++ b/gcc/rust/resolve/rust-name-resolver.h
@@ -62,6 +62,9 @@ public:
reverse_mappings.insert (std::pair<NodeId, CanonicalPath> (id, path));
decls_within_rib.insert (std::pair<NodeId, Location> (id, locus));
references[id] = {};
+
+ auto mappings = Analysis::Mappings::get ();
+ mappings->insert_canonical_path (mappings->get_current_crate (), id, path);
}
bool lookup_name (const CanonicalPath &ident, NodeId *id)
diff --git a/gcc/rust/util/rust-canonical-path.h b/gcc/rust/util/rust-canonical-path.h
index a2d6773..d6ba90d 100644
--- a/gcc/rust/util/rust-canonical-path.h
+++ b/gcc/rust/util/rust-canonical-path.h
@@ -110,17 +110,21 @@ public:
}
}
+ size_t size () const { return segs.size (); }
+
NodeId get_id () const
{
rust_assert (!segs.empty ());
return segs.back ().first;
}
- bool operator== (const CanonicalPath &b) const
+ bool is_equal (const CanonicalPath &b) const
{
return get ().compare (b.get ()) == 0;
}
+ bool operator== (const CanonicalPath &b) const { return is_equal (b); }
+
bool operator< (const CanonicalPath &b) const { return get () < b.get (); }
private:
diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h
index 6fbb4d7..5c839b0 100644
--- a/gcc/rust/util/rust-hir-map.h
+++ b/gcc/rust/util/rust-hir-map.h
@@ -22,6 +22,7 @@
#include "rust-system.h"
#include "rust-location.h"
#include "rust-mapping-common.h"
+#include "rust-canonical-path.h"
#include "rust-ast-full-decls.h"
#include "rust-hir-full-decls.h"
@@ -227,6 +228,42 @@ public:
return lookup->second;
}
+ void insert_canonical_path (CrateNum crate, NodeId id,
+ const Resolver::CanonicalPath path)
+ {
+ const Resolver::CanonicalPath *p = nullptr;
+ if (lookup_canonical_path (crate, id, &p))
+ {
+ // if we have already stored a canonical path this is ok so long as this
+ // new path is equal or is smaller that the existing one but in that
+ // case we ignore it.
+ if (p->is_equal (path))
+ return;
+ else
+ {
+ rust_assert (p->size () >= path.size ());
+ return;
+ }
+ }
+
+ paths[crate].emplace (id, std::move (path));
+ }
+
+ bool lookup_canonical_path (CrateNum crate, NodeId id,
+ const Resolver::CanonicalPath **path)
+ {
+ auto it = paths.find (crate);
+ if (it == paths.end ())
+ return false;
+
+ auto iy = it->second.find (id);
+ if (iy == it->second.end ())
+ return false;
+
+ *path = &iy->second;
+ return true;
+ }
+
private:
Mappings ();
@@ -263,6 +300,9 @@ private:
hirGenericParamMappings;
std::map<HirId, HIR::Trait *> hirTraitItemsToTraitMappings;
+ // canonical paths
+ std::map<CrateNum, std::map<NodeId, const Resolver::CanonicalPath> > paths;
+
// location info
std::map<CrateNum, std::map<NodeId, Location> > locations;