diff options
author | Philip Herron <philip.herron@embecosm.com> | 2021-07-05 17:25:24 +0100 |
---|---|---|
committer | Philip Herron <philip.herron@embecosm.com> | 2021-07-10 21:26:58 +0100 |
commit | 91aa2cba1ca4b481a9b3fed77054258a3fdc442d (patch) | |
tree | b9792149e3b68149c4efa0cafbc33aeb82949884 /gcc | |
parent | bb51a9de35e35ac4e411a323c1fde806fb54d142 (diff) | |
download | gcc-91aa2cba1ca4b481a9b3fed77054258a3fdc442d.zip gcc-91aa2cba1ca4b481a9b3fed77054258a3fdc442d.tar.gz gcc-91aa2cba1ca4b481a9b3fed77054258a3fdc442d.tar.bz2 |
Refactor CanonicalPath so we can iterate the segments
This adds the associated NodeId into each CanonicalPath segment which
references their respective TypePathSegment or PathExprSegment.
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-expr.h | 20 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-implitem.h | 3 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-pattern.h | 5 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-stmt.h | 4 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-toplevel.h | 13 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve-type.h | 26 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-resolve.cc | 12 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-ast-verify-assignee.h | 4 | ||||
-rw-r--r-- | gcc/rust/resolve/rust-name-resolver.h | 67 |
9 files changed, 102 insertions, 52 deletions
diff --git a/gcc/rust/resolve/rust-ast-resolve-expr.h b/gcc/rust/resolve/rust-ast-resolve-expr.h index bbd8818..d6f5fcd 100644 --- a/gcc/rust/resolve/rust-ast-resolve-expr.h +++ b/gcc/rust/resolve/rust-ast-resolve-expr.h @@ -117,8 +117,8 @@ public: void visit (AST::IdentifierExpr &expr) override { - if (resolver->get_name_scope ().lookup (CanonicalPath (expr.as_string ()), - &resolved_node)) + if (resolver->get_name_scope ().lookup ( + CanonicalPath::new_seg (expr.as_string ()), &resolved_node)) { resolver->insert_resolved_name (expr.get_node_id (), resolved_node); resolver->insert_new_definition (expr.get_node_id (), @@ -126,7 +126,7 @@ public: parent}); } else if (resolver->get_type_scope ().lookup ( - CanonicalPath (expr.as_string ()), &resolved_node)) + CanonicalPath::new_seg (expr.as_string ()), &resolved_node)) { resolver->insert_resolved_type (expr.get_node_id (), resolved_node); resolver->insert_new_definition (expr.get_node_id (), @@ -272,7 +272,7 @@ public: auto label_name = label.get_lifetime ().get_lifetime_name (); auto label_lifetime_node_id = label.get_lifetime ().get_node_id (); resolver->get_label_scope ().insert ( - CanonicalPath (label_name), label_lifetime_node_id, + CanonicalPath::new_seg (label_name), label_lifetime_node_id, label.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { rust_error_at (label.get_locus (), @@ -299,8 +299,9 @@ public: } NodeId resolved_node = UNKNOWN_NODEID; - if (!resolver->get_label_scope ().lookup ( - CanonicalPath (label.get_lifetime_name ()), &resolved_node)) + if (!resolver->get_label_scope ().lookup (CanonicalPath::new_seg ( + label.get_lifetime_name ()), + &resolved_node)) { rust_error_at (expr.get_label ().get_locus (), "failed to resolve label"); @@ -329,7 +330,7 @@ public: auto label_name = label.get_lifetime ().get_lifetime_name (); auto label_lifetime_node_id = label.get_lifetime ().get_node_id (); resolver->get_label_scope ().insert ( - CanonicalPath (label_name), label_lifetime_node_id, + CanonicalPath::new_seg (label_name), label_lifetime_node_id, label.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { rust_error_at (label.get_locus (), @@ -357,8 +358,9 @@ public: } NodeId resolved_node = UNKNOWN_NODEID; - if (!resolver->get_label_scope ().lookup ( - CanonicalPath (label.get_lifetime_name ()), &resolved_node)) + if (!resolver->get_label_scope ().lookup (CanonicalPath::new_seg ( + label.get_lifetime_name ()), + &resolved_node)) { rust_error_at (expr.get_label ().get_locus (), "failed to resolve label"); diff --git a/gcc/rust/resolve/rust-ast-resolve-implitem.h b/gcc/rust/resolve/rust-ast-resolve-implitem.h index 9b71dad..b622dcb 100644 --- a/gcc/rust/resolve/rust-ast-resolve-implitem.h +++ b/gcc/rust/resolve/rust-ast-resolve-implitem.h @@ -45,7 +45,8 @@ public: void visit (AST::TypeAlias &type) override { - auto path = prefix.append (CanonicalPath (type.get_new_type_name ())); + auto path + = prefix.append (CanonicalPath::new_seg (type.get_new_type_name ())); resolver->get_type_scope ().insert ( path, type.get_node_id (), type.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { diff --git a/gcc/rust/resolve/rust-ast-resolve-pattern.h b/gcc/rust/resolve/rust-ast-resolve-pattern.h index 0734908..b2d059e 100644 --- a/gcc/rust/resolve/rust-ast-resolve-pattern.h +++ b/gcc/rust/resolve/rust-ast-resolve-pattern.h @@ -44,7 +44,7 @@ public: void visit (AST::IdentifierPattern &pattern) override { if (resolver->get_name_scope ().lookup ( - CanonicalPath (pattern.get_ident ()), &resolved_node)) + CanonicalPath::new_seg (pattern.get_ident ()), &resolved_node)) { resolver->insert_resolved_name (pattern.get_node_id (), resolved_node); resolver->insert_new_definition (pattern.get_node_id (), @@ -72,7 +72,8 @@ public: { // if we have a duplicate id this then allows for shadowing correctly // as new refs to this decl will match back here so it is ok to overwrite - resolver->get_name_scope ().insert (CanonicalPath (pattern.get_ident ()), + resolver->get_name_scope ().insert (CanonicalPath::new_seg ( + pattern.get_ident ()), pattern.get_node_id (), pattern.get_locus ()); resolver->insert_new_definition (pattern.get_node_id (), diff --git a/gcc/rust/resolve/rust-ast-resolve-stmt.h b/gcc/rust/resolve/rust-ast-resolve-stmt.h index e68e7b9..b0cdeb2 100644 --- a/gcc/rust/resolve/rust-ast-resolve-stmt.h +++ b/gcc/rust/resolve/rust-ast-resolve-stmt.h @@ -67,7 +67,7 @@ public: void visit (AST::TupleStruct &struct_decl) override { - auto path = CanonicalPath (struct_decl.get_identifier ()); + auto path = CanonicalPath::new_seg (struct_decl.get_identifier ()); resolver->get_type_scope ().insert ( path, struct_decl.get_node_id (), struct_decl.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -99,7 +99,7 @@ public: void visit (AST::StructStruct &struct_decl) override { - auto path = CanonicalPath (struct_decl.get_identifier ()); + auto path = CanonicalPath::new_seg (struct_decl.get_identifier ()); resolver->get_type_scope ().insert ( path, struct_decl.get_node_id (), struct_decl.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { diff --git a/gcc/rust/resolve/rust-ast-resolve-toplevel.h b/gcc/rust/resolve/rust-ast-resolve-toplevel.h index 3dd81d8..44f5b22 100644 --- a/gcc/rust/resolve/rust-ast-resolve-toplevel.h +++ b/gcc/rust/resolve/rust-ast-resolve-toplevel.h @@ -41,7 +41,8 @@ public: void visit (AST::TypeAlias &alias) override { - auto path = prefix.append (CanonicalPath (alias.get_new_type_name ())); + auto path + = prefix.append (CanonicalPath::new_seg (alias.get_new_type_name ())); resolver->get_type_scope ().insert ( path, alias.get_node_id (), alias.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -53,7 +54,8 @@ public: void visit (AST::TupleStruct &struct_decl) override { - auto path = prefix.append (CanonicalPath (struct_decl.get_identifier ())); + auto path + = prefix.append (CanonicalPath::new_seg (struct_decl.get_identifier ())); resolver->get_type_scope ().insert ( path, struct_decl.get_node_id (), struct_decl.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -65,7 +67,8 @@ public: void visit (AST::StructStruct &struct_decl) override { - auto path = prefix.append (CanonicalPath (struct_decl.get_identifier ())); + auto path + = prefix.append (CanonicalPath::new_seg (struct_decl.get_identifier ())); resolver->get_type_scope ().insert ( path, struct_decl.get_node_id (), struct_decl.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -77,7 +80,7 @@ public: void visit (AST::StaticItem &var) override { - auto path = prefix.append (CanonicalPath (var.get_identifier ())); + auto path = prefix.append (CanonicalPath::new_seg (var.get_identifier ())); resolver->get_name_scope ().insert ( path, var.get_node_id (), var.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { @@ -170,7 +173,7 @@ public: void visit (AST::Trait &trait) override { CanonicalPath path - = prefix.append (CanonicalPath (trait.get_identifier ())); + = prefix.append (CanonicalPath::new_seg (trait.get_identifier ())); resolver->get_type_scope ().insert ( path, trait.get_node_id (), trait.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { diff --git a/gcc/rust/resolve/rust-ast-resolve-type.h b/gcc/rust/resolve/rust-ast-resolve-type.h index 19e7324..cf6962b 100644 --- a/gcc/rust/resolve/rust-ast-resolve-type.h +++ b/gcc/rust/resolve/rust-ast-resolve-type.h @@ -30,7 +30,7 @@ class ResolveConstantItemToCanonicalPath public: static CanonicalPath resolve (AST::ConstantItem &constant) { - return CanonicalPath (constant.get_identifier ()); + return CanonicalPath::new_seg (constant.get_identifier ()); } }; @@ -39,7 +39,7 @@ class ResolveFunctionItemToCanonicalPath public: static CanonicalPath resolve (AST::Function &function) { - return CanonicalPath (function.get_function_name ()); + return CanonicalPath::new_seg (function.get_function_name ()); } }; @@ -48,7 +48,7 @@ class ResolveMethodItemToCanonicalPath public: static CanonicalPath resolve (AST::Method &method) { - return CanonicalPath (method.get_method_name ()); + return CanonicalPath::new_seg (method.get_method_name ()); } }; @@ -57,7 +57,7 @@ class ResolveTraitItemFunctionToCanonicalPath public: static CanonicalPath resolve (AST::TraitItemFunc &function) { - return CanonicalPath ( + return CanonicalPath::new_seg ( function.get_trait_function_decl ().get_identifier ()); } }; @@ -67,7 +67,8 @@ class ResolveTraitItemMethodToCanonicalPath public: static CanonicalPath resolve (AST::TraitItemMethod &method) { - return CanonicalPath (method.get_trait_method_decl ().get_identifier ()); + return CanonicalPath::new_seg ( + method.get_trait_method_decl ().get_identifier ()); } }; @@ -76,7 +77,7 @@ class ResolveTraitItemConstToCanonicalPath public: static CanonicalPath resolve (AST::TraitItemConst &constant) { - return CanonicalPath (constant.get_identifier ()); + return CanonicalPath::new_seg (constant.get_identifier ()); } }; @@ -85,7 +86,7 @@ class ResolveTraitItemTypeToCanonicalPath public: static CanonicalPath resolve (AST::TraitItemType &type) { - return CanonicalPath (type.get_identifier ()); + return CanonicalPath::new_seg (type.get_identifier ()); } }; @@ -142,7 +143,8 @@ class ResolvePathSegmentToCanonicalPath public: static CanonicalPath resolve (AST::PathExprSegment &seg) { - CanonicalPath path = CanonicalPath (seg.get_ident_segment ().as_string ()); + CanonicalPath path + = CanonicalPath::new_seg (seg.get_ident_segment ().as_string ()); if (seg.has_generic_args ()) { bool ok = ResolveTypeToCanonicalPath::type_resolve_generic_args ( @@ -168,8 +170,8 @@ public: static CanonicalPath resolve (const CanonicalPath &trait_seg, const CanonicalPath &impl_type_seg) { - return CanonicalPath ("<" + impl_type_seg.get () + " as " + trait_seg.get () - + ">"); + return CanonicalPath::new_seg ("<" + impl_type_seg.get () + " as " + + trait_seg.get () + ">"); } }; @@ -316,8 +318,8 @@ public: // for now lets focus on handling the basics: like struct<T> { a:T, ....} resolver->get_type_scope ().insert ( - CanonicalPath (param.get_type_representation ()), param.get_node_id (), - param.get_locus (), false, + CanonicalPath::new_seg (param.get_type_representation ()), + param.get_node_id (), param.get_locus (), false, [&] (const CanonicalPath &, NodeId, Location locus) -> void { rust_error_at (param.get_locus (), "generic param redefined multiple times"); diff --git a/gcc/rust/resolve/rust-ast-resolve.cc b/gcc/rust/resolve/rust-ast-resolve.cc index fae3f77..d47371c 100644 --- a/gcc/rust/resolve/rust-ast-resolve.cc +++ b/gcc/rust/resolve/rust-ast-resolve.cc @@ -121,7 +121,8 @@ Resolver::insert_builtin_types (Rib *r) auto builtins = get_builtin_types (); for (auto &builtin : builtins) { - CanonicalPath builtin_path (builtin->as_string ()); + CanonicalPath builtin_path + = CanonicalPath::new_seg (builtin->as_string ()); r->insert_name (builtin_path, builtin->get_node_id (), Linemap::predeclared_location (), false, [] (const CanonicalPath &, NodeId, Location) -> void {}); @@ -401,7 +402,7 @@ ResolveTypeToCanonicalPath::canonicalize_generic_args (AST::GenericArgs &args) i++; } - return CanonicalPath ("<" + buf + ">"); + return CanonicalPath::new_seg ("<" + buf + ">"); } bool @@ -430,7 +431,7 @@ ResolveTypeToCanonicalPath::visit (AST::TypePathSegmentGeneric &seg) // ident seg CanonicalPath ident_seg - = CanonicalPath (seg.get_ident_segment ().as_string ()); + = CanonicalPath::new_seg (seg.get_ident_segment ().as_string ()); result = result.append (ident_seg); // generic args @@ -460,7 +461,7 @@ ResolveTypeToCanonicalPath::visit (AST::TypePathSegment &seg) } CanonicalPath ident_seg - = CanonicalPath (seg.get_ident_segment ().as_string ()); + = CanonicalPath::new_seg (seg.get_ident_segment ().as_string ()); result = result.append (ident_seg); } @@ -474,7 +475,8 @@ ResolvePath::resolve_path (AST::PathInExpression *expr) AST::PathIdentSegment &root_ident_seg = root_segment.get_ident_segment (); bool segment_is_type = false; - CanonicalPath root_seg_path (root_ident_seg.as_string ()); + CanonicalPath root_seg_path + = CanonicalPath::new_seg (root_ident_seg.as_string ()); // name scope first if (resolver->get_name_scope ().lookup (root_seg_path, &resolved_node)) diff --git a/gcc/rust/resolve/rust-ast-verify-assignee.h b/gcc/rust/resolve/rust-ast-verify-assignee.h index 13b6c91..11a5414 100644 --- a/gcc/rust/resolve/rust-ast-verify-assignee.h +++ b/gcc/rust/resolve/rust-ast-verify-assignee.h @@ -57,8 +57,8 @@ public: void visit (AST::IdentifierExpr &expr) override { - if (!resolver->get_name_scope ().lookup (CanonicalPath (expr.as_string ()), - &resolved_node)) + if (!resolver->get_name_scope ().lookup ( + CanonicalPath::new_seg (expr.as_string ()), &resolved_node)) return; ok = true; diff --git a/gcc/rust/resolve/rust-name-resolver.h b/gcc/rust/resolve/rust-name-resolver.h index b1f745e..d3b22a0 100644 --- a/gcc/rust/resolve/rust-name-resolver.h +++ b/gcc/rust/resolve/rust-name-resolver.h @@ -47,34 +47,73 @@ namespace Resolver { class CanonicalPath { public: - explicit CanonicalPath (std::string path) : path (path) {} - - CanonicalPath (const CanonicalPath &other) : path (other.path) {} + CanonicalPath (const CanonicalPath &other) : segs (other.segs) {} CanonicalPath &operator= (const CanonicalPath &other) { - path = other.path; + segs = other.segs; return *this; } - std::string get () const { return path; } + static CanonicalPath new_seg (const std::string &path) + { + rust_assert (!path.empty ()); + return CanonicalPath ({path}); + } - static CanonicalPath get_big_self () { return CanonicalPath ("Self"); } + 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); + buf += seg + (have_more ? "::" : ""); + } + return buf; + } - static CanonicalPath get_wee_self () { return CanonicalPath ("self"); } + static CanonicalPath get_big_self () + { + return CanonicalPath::new_seg ("Self"); + } - static CanonicalPath create_empty () + static CanonicalPath get_wee_self () { - return CanonicalPath (std::string ()); + return CanonicalPath::new_seg ("self"); } - bool is_error () const { return path.empty (); } + static CanonicalPath create_empty () { return CanonicalPath ({}); } + + bool is_error () const { return segs.size () == 0; } CanonicalPath append (const CanonicalPath &other) const { rust_assert (!other.is_error ()); - return is_error () ? CanonicalPath (other.get ()) - : CanonicalPath (append (other.get ())); + if (is_error ()) + return CanonicalPath (other.segs); + + std::vector<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_path (std::function<bool (const CanonicalPath &)> cb) const + { + std::vector<std::string> buf; + for (auto &seg : segs) + { + buf.push_back (seg); + if (!cb (CanonicalPath (buf))) + return; + } } bool operator== (const CanonicalPath &b) const @@ -85,9 +124,9 @@ public: bool operator< (const CanonicalPath &b) const { return get () < b.get (); } private: - std::string append (std::string elem) const { return path + "::" + elem; } + explicit CanonicalPath (std::vector<std::string> path) : segs (path) {} - std::string path; + std::vector<std::string> segs; }; class Rib |