aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust
diff options
context:
space:
mode:
authorPhilip Herron <philip.herron@embecosm.com>2021-07-08 15:32:14 +0100
committerPhilip Herron <philip.herron@embecosm.com>2021-07-10 21:32:26 +0100
commit8bfeef998ff340fef37f07e1a7a0366e73296380 (patch)
tree3a8decc5d1fcfcd3717501cb6e8f4be00e332540 /gcc/rust
parent97b917f8d287b362fabf7b8da0796237bb8e16d1 (diff)
downloadgcc-8bfeef998ff340fef37f07e1a7a0366e73296380.zip
gcc-8bfeef998ff340fef37f07e1a7a0366e73296380.tar.gz
gcc-8bfeef998ff340fef37f07e1a7a0366e73296380.tar.bz2
Add node-ids to type-path-segments in AST
We must track the type path segments ids within the Canonical path.
Diffstat (limited to 'gcc/rust')
-rw-r--r--gcc/rust/ast/rust-path.h16
-rw-r--r--gcc/rust/hir/rust-ast-lower-type.h11
-rw-r--r--gcc/rust/hir/rust-ast-lower.cc10
-rw-r--r--gcc/rust/hir/tree/rust-hir-path.h87
-rw-r--r--gcc/rust/typecheck/rust-hir-type-check-type.h4
5 files changed, 84 insertions, 44 deletions
diff --git a/gcc/rust/ast/rust-path.h b/gcc/rust/ast/rust-path.h
index 86da203..011ac3e 100644
--- a/gcc/rust/ast/rust-path.h
+++ b/gcc/rust/ast/rust-path.h
@@ -406,11 +406,6 @@ protected:
* ident-only segment) */
class TypePathSegment
{
- /* TODO: may have to unify TypePathSegment and PathExprSegment (which are
- * mostly the same anyway) in order to resolve goddamn syntax ambiguities. One
- * difference is that function on TypePathSegment is not allowed if
- * GenericArgs are, so could disallow that in constructor, which won't give
- * that much size overhead. */
PathIdentSegment ident_segment;
Location locus;
@@ -418,6 +413,7 @@ protected:
/* This is protected because it is only really used by derived classes, not
* the base. */
bool has_separating_scope_resolution;
+ NodeId node_id;
// Clone function implementation - not pure virtual as overrided by subclasses
virtual TypePathSegment *clone_type_path_segment_impl () const
@@ -437,14 +433,16 @@ public:
TypePathSegment (PathIdentSegment ident_segment,
bool has_separating_scope_resolution, Location locus)
: ident_segment (std::move (ident_segment)), locus (locus),
- has_separating_scope_resolution (has_separating_scope_resolution)
+ has_separating_scope_resolution (has_separating_scope_resolution),
+ node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
TypePathSegment (std::string segment_name,
bool has_separating_scope_resolution, Location locus)
: ident_segment (PathIdentSegment (std::move (segment_name))),
locus (locus),
- has_separating_scope_resolution (has_separating_scope_resolution)
+ has_separating_scope_resolution (has_separating_scope_resolution),
+ node_id (Analysis::Mappings::get ()->get_next_node_id ())
{}
virtual std::string as_string () const { return ident_segment.as_string (); }
@@ -454,7 +452,7 @@ public:
bool is_error () const { return ident_segment.is_error (); }
/* Returns whether segment is identifier only (as opposed to generic args or
- * function). Overriden in derived classes with other segments. */
+ * function). Overridden in derived classes with other segments. */
virtual bool is_ident_only () const { return true; }
Location get_locus () const { return locus; }
@@ -468,6 +466,8 @@ public:
}
PathIdentSegment get_ident_segment () { return ident_segment; };
+
+ NodeId get_node_id () const { return node_id; }
};
// Segment used in type path with generic args
diff --git a/gcc/rust/hir/rust-ast-lower-type.h b/gcc/rust/hir/rust-ast-lower-type.h
index 568a806..0b534d0 100644
--- a/gcc/rust/hir/rust-ast-lower-type.h
+++ b/gcc/rust/hir/rust-ast-lower-type.h
@@ -35,17 +35,22 @@ public:
{
ASTLowerTypePath resolver;
type.accept_vis (resolver);
-
rust_assert (resolver.translated != nullptr);
-
return resolver.translated;
}
+ void visit (AST::TypePathSegmentFunction &) override { gcc_unreachable (); }
+
void visit (AST::TypePathSegment &segment) override
{
+ auto crate_num = mappings->get_current_crate ();
+ auto hirid = mappings->get_next_hir_id (crate_num);
+ Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
+ UNKNOWN_LOCAL_DEFID);
+
HIR::PathIdentSegment ident (segment.get_ident_segment ().as_string ());
translated_segment
- = new HIR::TypePathSegment (ident,
+ = new HIR::TypePathSegment (std::move (mapping), ident,
segment.get_separating_scope_resolution (),
segment.get_locus ());
}
diff --git a/gcc/rust/hir/rust-ast-lower.cc b/gcc/rust/hir/rust-ast-lower.cc
index 01abd84..516b5ba 100644
--- a/gcc/rust/hir/rust-ast-lower.cc
+++ b/gcc/rust/hir/rust-ast-lower.cc
@@ -383,9 +383,15 @@ ASTLowerTypePath::visit (AST::TypePathSegmentGeneric &segment)
type_args.push_back (std::unique_ptr<HIR::Type> (t));
}
+ auto crate_num = mappings->get_current_crate ();
+ auto hirid = mappings->get_next_hir_id (crate_num);
+ Analysis::NodeMapping mapping (crate_num, segment.get_node_id (), hirid,
+ UNKNOWN_LOCAL_DEFID);
+
translated_segment = new HIR::TypePathSegmentGeneric (
- segment_name, has_separating_scope_resolution, std::move (lifetime_args),
- std::move (type_args), std::move (binding_args), segment.get_locus ());
+ std::move (mapping), segment_name, has_separating_scope_resolution,
+ std::move (lifetime_args), std::move (type_args), std::move (binding_args),
+ segment.get_locus ());
}
} // namespace HIR
diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h
index 84f3e96..5d9f965 100644
--- a/gcc/rust/hir/tree/rust-hir-path.h
+++ b/gcc/rust/hir/tree/rust-hir-path.h
@@ -351,19 +351,22 @@ protected:
* ident-only segment) */
class TypePathSegment
{
- /* TODO: may have to unify TypePathSegment and PathExprSegment (which are
- * mostly the same anyway) in order to resolve goddamn syntax ambiguities. One
- * difference is that function on TypePathSegment is not allowed if
- * GenericArgs are, so could disallow that in constructor, which won't give
- * that much size overhead. */
- PathIdentSegment ident_segment;
+public:
+ enum SegmentType
+ {
+ REG,
+ GENERIC,
+ FUNCTION
+ };
+private:
+ Analysis::NodeMapping mappings;
+ PathIdentSegment ident_segment;
Location locus;
protected:
- /* This is protected because it is only really used by derived classes, not
- * the base. */
bool has_separating_scope_resolution;
+ SegmentType type;
// Clone function implementation - not pure virtual as overrided by subclasses
virtual TypePathSegment *clone_type_path_segment_impl () const
@@ -374,23 +377,30 @@ protected:
public:
virtual ~TypePathSegment () {}
+ virtual SegmentType get_type () const { return SegmentType::REG; }
+
// Unique pointer custom clone function
std::unique_ptr<TypePathSegment> clone_type_path_segment () const
{
return std::unique_ptr<TypePathSegment> (clone_type_path_segment_impl ());
}
- TypePathSegment (PathIdentSegment ident_segment,
+ TypePathSegment (Analysis::NodeMapping mappings,
+ PathIdentSegment ident_segment,
bool has_separating_scope_resolution, Location locus)
- : ident_segment (std::move (ident_segment)), locus (locus),
- has_separating_scope_resolution (has_separating_scope_resolution)
+ : mappings (std::move (mappings)),
+ ident_segment (std::move (ident_segment)), locus (locus),
+ has_separating_scope_resolution (has_separating_scope_resolution),
+ type (SegmentType::REG)
{}
- TypePathSegment (std::string segment_name,
+ TypePathSegment (Analysis::NodeMapping mappings, std::string segment_name,
bool has_separating_scope_resolution, Location locus)
- : ident_segment (PathIdentSegment (std::move (segment_name))),
+ : mappings (std::move (mappings)),
+ ident_segment (PathIdentSegment (std::move (segment_name))),
locus (locus),
- has_separating_scope_resolution (has_separating_scope_resolution)
+ has_separating_scope_resolution (has_separating_scope_resolution),
+ type (SegmentType::REG)
{}
virtual std::string as_string () const { return ident_segment.as_string (); }
@@ -407,6 +417,10 @@ public:
// not pure virtual as class not abstract
virtual void accept_vis (HIRVisitor &vis);
+
+ const Analysis::NodeMapping &get_mappings () const { return mappings; }
+
+ const PathIdentSegment &get_ident_segment () const { return ident_segment; }
};
// Segment used in type path with generic args
@@ -420,22 +434,24 @@ public:
bool is_ident_only () const override { return false; }
// Constructor with PathIdentSegment and GenericArgs
- TypePathSegmentGeneric (PathIdentSegment ident_segment,
+ TypePathSegmentGeneric (Analysis::NodeMapping mappings,
+ PathIdentSegment ident_segment,
bool has_separating_scope_resolution,
GenericArgs generic_args, Location locus)
- : TypePathSegment (std::move (ident_segment),
+ : TypePathSegment (std::move (mappings), std::move (ident_segment),
has_separating_scope_resolution, locus),
generic_args (std::move (generic_args))
{}
// Constructor from segment name and all args
- TypePathSegmentGeneric (std::string segment_name,
+ TypePathSegmentGeneric (Analysis::NodeMapping mappings,
+ std::string segment_name,
bool has_separating_scope_resolution,
std::vector<Lifetime> lifetime_args,
std::vector<std::unique_ptr<Type> > type_args,
std::vector<GenericArgsBinding> binding_args,
Location locus)
- : TypePathSegment (std::move (segment_name),
+ : TypePathSegment (std::move (mappings), std::move (segment_name),
has_separating_scope_resolution, locus),
generic_args (GenericArgs (std::move (lifetime_args),
std::move (type_args),
@@ -446,7 +462,12 @@ public:
void accept_vis (HIRVisitor &vis) override;
- GenericArgs get_generic_args () { return generic_args; }
+ GenericArgs &get_generic_args () { return generic_args; }
+
+ virtual SegmentType get_type () const override final
+ {
+ return SegmentType::GENERIC;
+ }
protected:
// Use covariance to override base class method
@@ -544,19 +565,21 @@ class TypePathSegmentFunction : public TypePathSegment
public:
// Constructor with PathIdentSegment and TypePathFn
- TypePathSegmentFunction (PathIdentSegment ident_segment,
+ TypePathSegmentFunction (Analysis::NodeMapping mappings,
+ PathIdentSegment ident_segment,
bool has_separating_scope_resolution,
TypePathFunction function_path, Location locus)
- : TypePathSegment (std::move (ident_segment),
+ : TypePathSegment (std::move (mappings), std::move (ident_segment),
has_separating_scope_resolution, locus),
function_path (std::move (function_path))
{}
// Constructor with segment name and TypePathFn
- TypePathSegmentFunction (std::string segment_name,
+ TypePathSegmentFunction (Analysis::NodeMapping mappings,
+ std::string segment_name,
bool has_separating_scope_resolution,
TypePathFunction function_path, Location locus)
- : TypePathSegment (std::move (segment_name),
+ : TypePathSegment (std::move (mappings), std::move (segment_name),
has_separating_scope_resolution, locus),
function_path (std::move (function_path))
{}
@@ -567,6 +590,11 @@ public:
void accept_vis (HIRVisitor &vis) override;
+ virtual SegmentType get_type () const override final
+ {
+ return SegmentType::FUNCTION;
+ }
+
protected:
// Use covariance to override base class method
TypePathSegmentFunction *clone_type_path_segment_impl () const override
@@ -667,16 +695,15 @@ public:
size_t get_num_segments () const { return segments.size (); }
- void iterate_segments (std::function<bool (TypePathSegment *)> cb)
+ std::vector<std::unique_ptr<TypePathSegment> > &get_segments ()
{
- for (auto &seg : segments)
- {
- if (!cb (seg.get ()))
- return;
- }
+ return segments;
}
- TypePathSegment *get_final_segment () { return segments.back ().get (); }
+ std::unique_ptr<TypePathSegment> &get_final_segment ()
+ {
+ return segments.back ();
+ }
};
struct QualifiedPathType
diff --git a/gcc/rust/typecheck/rust-hir-type-check-type.h b/gcc/rust/typecheck/rust-hir-type-check-type.h
index 6081ec5..e9a3bde 100644
--- a/gcc/rust/typecheck/rust-hir-type-check-type.h
+++ b/gcc/rust/typecheck/rust-hir-type-check-type.h
@@ -1,3 +1,4 @@
+
// Copyright (C) 2020 Free Software Foundation, Inc.
// This file is part of GCC.
@@ -22,6 +23,7 @@
#include "rust-hir-type-check-base.h"
#include "rust-hir-full.h"
#include "rust-substitution-mapper.h"
+#include "rust-hir-path-probe.h"
namespace Rust {
namespace Resolver {
@@ -145,7 +147,7 @@ public:
TyTy::BaseType *path_type = lookup->clone ();
path_type->set_ref (path.get_mappings ().get_hirid ());
- HIR::TypePathSegment *final_seg = path.get_final_segment ();
+ HIR::TypePathSegment *final_seg = path.get_final_segment ().get ();
HIR::GenericArgs args
= TypeCheckResolveGenericArguments::resolve (final_seg);