diff options
author | bors[bot] <26634292+bors[bot]@users.noreply.github.com> | 2021-12-16 13:08:09 +0000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-12-16 13:08:09 +0000 |
commit | de1ed2e805cc7de7ab29b5e183354bae86173669 (patch) | |
tree | df2bc819e07affe08e13d8f09f8448c00ae4722a /gcc | |
parent | 8507a68883438060b3b8e828069ffe20a084b85a (diff) | |
parent | 4d70990b838e3fbeedaf0918c2c87f46cdf0d689 (diff) | |
download | gcc-de1ed2e805cc7de7ab29b5e183354bae86173669.zip gcc-de1ed2e805cc7de7ab29b5e183354bae86173669.tar.gz gcc-de1ed2e805cc7de7ab29b5e183354bae86173669.tar.bz2 |
Merge #838
838: Add mssing mappings to HIR::Pattern r=philberty a=philberty
These mappings are missing within the HIR but are required
to complete typechecking of all patterns in match arms. As the
fields of structures must bind their associated field's types to new
names declared as part of the pattern, these mappings give access
to the associated name-resolved NodeId's to figure this out.
Co-authored-by: Philip Herron <philip.herron@embecosm.com>
Diffstat (limited to 'gcc')
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-pattern.cc | 30 | ||||
-rw-r--r-- | gcc/rust/hir/rust-ast-lower-pattern.h | 20 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-path.h | 10 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir-pattern.h | 250 | ||||
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.h | 2 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check-implitem.h | 16 | ||||
-rw-r--r-- | gcc/rust/typecheck/rust-hir-type-check.cc | 9 | ||||
-rw-r--r-- | gcc/rust/util/rust-hir-map.cc | 23 | ||||
-rw-r--r-- | gcc/rust/util/rust-hir-map.h | 42 |
9 files changed, 325 insertions, 77 deletions
diff --git a/gcc/rust/hir/rust-ast-lower-pattern.cc b/gcc/rust/hir/rust-ast-lower-pattern.cc index 4ff61cd..156f023 100644 --- a/gcc/rust/hir/rust-ast-lower-pattern.cc +++ b/gcc/rust/hir/rust-ast-lower-pattern.cc @@ -61,8 +61,13 @@ ASTLoweringPattern::visit (AST::TupleStructPattern &pattern) break; } + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + translated = new HIR::TupleStructPattern ( - *path, std::unique_ptr<HIR::TupleStructItems> (lowered)); + mapping, *path, std::unique_ptr<HIR::TupleStructItems> (lowered)); } void @@ -96,19 +101,38 @@ ASTLoweringPattern::visit (AST::StructPattern &pattern) AST::StructPatternFieldIdent &ident = static_cast<AST::StructPatternFieldIdent &> (*field.get ()); + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, ident.get_node_id (), + mappings->get_next_hir_id ( + crate_num), + UNKNOWN_LOCAL_DEFID); + f = new HIR::StructPatternFieldIdent ( - ident.get_identifier (), ident.is_ref (), + mapping, ident.get_identifier (), ident.is_ref (), ident.is_mut () ? Mutability::Mut : Mutability::Imm, ident.get_outer_attrs (), ident.get_locus ()); } break; } + // insert the reverse mappings and locations + auto crate_num = f->get_mappings ().get_crate_num (); + auto field_id = f->get_mappings ().get_hirid (); + auto field_node_id = f->get_mappings ().get_nodeid (); + mappings->insert_location (crate_num, field_id, f->get_locus ()); + mappings->insert_node_to_hir (crate_num, field_node_id, field_id); + + // add it to the lowered fields list fields.push_back (std::unique_ptr<HIR::StructPatternField> (f)); } + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + HIR::StructPatternElements elems (std::move (fields)); - translated = new HIR::StructPattern (*path, std::move (elems)); + translated = new HIR::StructPattern (mapping, *path, std::move (elems)); } } // namespace HIR diff --git a/gcc/rust/hir/rust-ast-lower-pattern.h b/gcc/rust/hir/rust-ast-lower-pattern.h index d8c03ce..bd25b83 100644 --- a/gcc/rust/hir/rust-ast-lower-pattern.h +++ b/gcc/rust/hir/rust-ast-lower-pattern.h @@ -33,16 +33,32 @@ public: { ASTLoweringPattern resolver; pattern->accept_vis (resolver); + rust_assert (resolver.translated != nullptr); + + resolver.mappings->insert_hir_pattern ( + resolver.translated->get_pattern_mappings ().get_crate_num (), + resolver.translated->get_pattern_mappings ().get_hirid (), + resolver.translated); + resolver.mappings->insert_location ( + resolver.translated->get_pattern_mappings ().get_crate_num (), + resolver.translated->get_pattern_mappings ().get_hirid (), + pattern->get_locus ()); + return resolver.translated; } void visit (AST::IdentifierPattern &pattern) override { + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, pattern.get_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + std::unique_ptr<Pattern> to_bind; translated - = new HIR::IdentifierPattern (pattern.get_ident (), pattern.get_locus (), - pattern.get_is_ref (), + = new HIR::IdentifierPattern (mapping, pattern.get_ident (), + pattern.get_locus (), pattern.get_is_ref (), pattern.get_is_mut () ? Mutability::Mut : Mutability::Imm, std::move (to_bind)); diff --git a/gcc/rust/hir/tree/rust-hir-path.h b/gcc/rust/hir/tree/rust-hir-path.h index 4364fb4..16e1be3 100644 --- a/gcc/rust/hir/tree/rust-hir-path.h +++ b/gcc/rust/hir/tree/rust-hir-path.h @@ -330,6 +330,11 @@ public: == 0; } + Analysis::NodeMapping get_pattern_mappings () const override final + { + return get_mappings (); + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -824,6 +829,11 @@ public: Location get_locus () { return locus; } + Analysis::NodeMapping get_pattern_mappings () const override final + { + return get_mappings (); + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/hir/tree/rust-hir-pattern.h b/gcc/rust/hir/tree/rust-hir-pattern.h index bdeeaed..fd4960f 100644 --- a/gcc/rust/hir/tree/rust-hir-pattern.h +++ b/gcc/rust/hir/tree/rust-hir-pattern.h @@ -24,38 +24,40 @@ namespace Rust { namespace HIR { + // Literal pattern HIR node (comparing to a literal) class LiteralPattern : public Pattern { Literal lit; - /* make literal have a type given by enum, etc. rustc uses an extended form of - * its literal token implementation */ - // TODO: literal representation - use LiteralExpr? or another thing? - - // Minus prefixed to literal (if integer or floating-point) bool has_minus; - // Actually, this might be a good place to use a template. - Location locus; + Analysis::NodeMapping mappings; public: std::string as_string () const override; // Constructor for a literal pattern - LiteralPattern (Literal lit, Location locus, bool has_minus = false) - : lit (std::move (lit)), has_minus (has_minus), locus (locus) + LiteralPattern (Analysis::NodeMapping mappings, Literal lit, Location locus, + bool has_minus = false) + : lit (std::move (lit)), has_minus (has_minus), locus (locus), + mappings (mappings) {} - LiteralPattern (std::string val, Literal::LitType type, Location locus, - bool has_minus = false) + LiteralPattern (Analysis::NodeMapping mappings, std::string val, + Literal::LitType type, Location locus, bool has_minus = false) : lit (Literal (std::move (val), type, PrimitiveCoreType::CORETYPE_STR)), - has_minus (has_minus), locus (locus) + has_minus (has_minus), locus (locus), mappings (mappings) {} Location get_locus () const { return locus; } void accept_vis (HIRVisitor &vis) override; + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -73,6 +75,7 @@ class IdentifierPattern : public Pattern Mutability mut; std::unique_ptr<Pattern> to_bind; Location locus; + Analysis::NodeMapping mappings; public: std::string as_string () const override; @@ -81,17 +84,18 @@ public: bool has_pattern_to_bind () const { return to_bind != nullptr; } // Constructor - IdentifierPattern (Identifier ident, Location locus, bool is_ref = false, + IdentifierPattern (Analysis::NodeMapping mappings, Identifier ident, + Location locus, bool is_ref = false, Mutability mut = Mutability::Imm, std::unique_ptr<Pattern> to_bind = nullptr) : variable_ident (std::move (ident)), is_ref (is_ref), mut (mut), - to_bind (std::move (to_bind)), locus (locus) + to_bind (std::move (to_bind)), locus (locus), mappings (mappings) {} // Copy constructor with clone IdentifierPattern (IdentifierPattern const &other) : variable_ident (other.variable_ident), is_ref (other.is_ref), - mut (other.mut), locus (other.locus) + mut (other.mut), locus (other.locus), mappings (other.mappings) { // fix to get prevent null pointer dereference if (other.to_bind != nullptr) @@ -105,6 +109,7 @@ public: is_ref = other.is_ref; mut = other.mut; locus = other.locus; + mappings = other.mappings; // fix to get prevent null pointer dereference if (other.to_bind != nullptr) @@ -123,6 +128,11 @@ public: void accept_vis (HIRVisitor &vis) override; + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + Identifier get_identifier () const { return variable_ident; } protected: @@ -138,16 +148,24 @@ protected: class WildcardPattern : public Pattern { Location locus; + Analysis::NodeMapping mappings; public: std::string as_string () const override { return std::string (1, '_'); } - WildcardPattern (Location locus) : locus (locus) {} + WildcardPattern (Analysis::NodeMapping mappings, Location locus) + : locus (locus), mappings (mappings) + {} Location get_locus () const { return locus; } void accept_vis (HIRVisitor &vis) override; + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -278,23 +296,27 @@ class RangePattern : public Pattern /* location only stored to avoid a dereference - lower pattern should give * correct location so maybe change in future */ Location locus; + Analysis::NodeMapping mappings; public: std::string as_string () const override; // Constructor - RangePattern (std::unique_ptr<RangePatternBound> lower, + RangePattern (Analysis::NodeMapping mappings, + std::unique_ptr<RangePatternBound> lower, std::unique_ptr<RangePatternBound> upper, Location locus, bool has_ellipsis_syntax = false) : lower (std::move (lower)), upper (std::move (upper)), - has_ellipsis_syntax (has_ellipsis_syntax), locus (locus) + has_ellipsis_syntax (has_ellipsis_syntax), locus (locus), + mappings (mappings) {} // Copy constructor with clone RangePattern (RangePattern const &other) : lower (other.lower->clone_range_pattern_bound ()), upper (other.upper->clone_range_pattern_bound ()), - has_ellipsis_syntax (other.has_ellipsis_syntax), locus (other.locus) + has_ellipsis_syntax (other.has_ellipsis_syntax), locus (other.locus), + mappings (other.mappings) {} // Overloaded assignment operator to clone @@ -304,6 +326,7 @@ public: upper = other.upper->clone_range_pattern_bound (); has_ellipsis_syntax = other.has_ellipsis_syntax; locus = other.locus; + mappings = other.mappings; return *this; } @@ -316,6 +339,11 @@ public: void accept_vis (HIRVisitor &vis) override; + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -332,20 +360,23 @@ class ReferencePattern : public Pattern Mutability mut; std::unique_ptr<Pattern> pattern; Location locus; + Analysis::NodeMapping mappings; public: std::string as_string () const override; - ReferencePattern (std::unique_ptr<Pattern> pattern, Mutability reference_mut, + ReferencePattern (Analysis::NodeMapping mappings, + std::unique_ptr<Pattern> pattern, Mutability reference_mut, bool ref_has_two_amps, Location locus) : has_two_amps (ref_has_two_amps), mut (reference_mut), - pattern (std::move (pattern)), locus (locus) + pattern (std::move (pattern)), locus (locus), mappings (mappings) {} // Copy constructor requires clone ReferencePattern (ReferencePattern const &other) : has_two_amps (other.has_two_amps), mut (other.mut), - pattern (other.pattern->clone_pattern ()), locus (other.locus) + pattern (other.pattern->clone_pattern ()), locus (other.locus), + mappings (other.mappings) {} // Overload assignment operator to clone @@ -355,6 +386,7 @@ public: mut = other.mut; has_two_amps = other.has_two_amps; locus = other.locus; + mappings = other.mappings; return *this; } @@ -367,6 +399,11 @@ public: void accept_vis (HIRVisitor &vis) override; + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -381,8 +418,16 @@ class StructPatternField { AST::AttrVec outer_attrs; Location locus; + Analysis::NodeMapping mappings; public: + enum ItemType + { + TUPLE_PAT, + IDENT_PAT, + IDENT + }; + virtual ~StructPatternField () {} // Unique pointer custom clone function @@ -393,14 +438,17 @@ public: } virtual std::string as_string () const; + virtual void accept_vis (HIRVisitor &vis) = 0; + virtual ItemType get_item_type () const = 0; Location get_locus () const { return locus; } - - virtual void accept_vis (HIRVisitor &vis) = 0; + Analysis::NodeMapping get_mappings () const { return mappings; }; protected: - StructPatternField (AST::AttrVec outer_attribs, Location locus) - : outer_attrs (std::move (outer_attribs)), locus (locus) + StructPatternField (Analysis::NodeMapping mappings, + AST::AttrVec outer_attribs, Location locus) + : outer_attrs (std::move (outer_attribs)), locus (locus), + mappings (mappings) {} // Clone function implementation as pure virtual method @@ -414,11 +462,11 @@ class StructPatternFieldTuplePat : public StructPatternField std::unique_ptr<Pattern> tuple_pattern; public: - StructPatternFieldTuplePat (TupleIndex index, + StructPatternFieldTuplePat (Analysis::NodeMapping mappings, TupleIndex index, std::unique_ptr<Pattern> tuple_pattern, AST::AttrVec outer_attribs, Location locus) - : StructPatternField (std::move (outer_attribs), locus), index (index), - tuple_pattern (std::move (tuple_pattern)) + : StructPatternField (mappings, std::move (outer_attribs), locus), + index (index), tuple_pattern (std::move (tuple_pattern)) {} // Copy constructor requires clone @@ -448,6 +496,8 @@ public: void accept_vis (HIRVisitor &vis) override; + ItemType get_item_type () const override final { return ItemType::TUPLE_PAT; } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -464,10 +514,10 @@ class StructPatternFieldIdentPat : public StructPatternField std::unique_ptr<Pattern> ident_pattern; public: - StructPatternFieldIdentPat (Identifier ident, + StructPatternFieldIdentPat (Analysis::NodeMapping mappings, Identifier ident, std::unique_ptr<Pattern> ident_pattern, AST::AttrVec outer_attrs, Location locus) - : StructPatternField (std::move (outer_attrs), locus), + : StructPatternField (mappings, std::move (outer_attrs), locus), ident (std::move (ident)), ident_pattern (std::move (ident_pattern)) {} @@ -498,6 +548,8 @@ public: void accept_vis (HIRVisitor &vis) override; + ItemType get_item_type () const override final { return ItemType::IDENT_PAT; } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -515,10 +567,11 @@ class StructPatternFieldIdent : public StructPatternField Identifier ident; public: - StructPatternFieldIdent (Identifier ident, bool is_ref, Mutability mut, + StructPatternFieldIdent (Analysis::NodeMapping mappings, Identifier ident, + bool is_ref, Mutability mut, AST::AttrVec outer_attrs, Location locus) - : StructPatternField (std::move (outer_attrs), locus), has_ref (is_ref), - mut (mut), ident (std::move (ident)) + : StructPatternField (mappings, std::move (outer_attrs), locus), + has_ref (is_ref), mut (mut), ident (std::move (ident)) {} std::string as_string () const override; @@ -527,6 +580,10 @@ public: void accept_vis (HIRVisitor &vis) override; + ItemType get_item_type () const override final { return ItemType::IDENT; } + + Identifier get_identifier () const { return ident; }; + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -586,6 +643,12 @@ public: } std::string as_string () const; + + std::vector<std::unique_ptr<StructPatternField> > & + get_struct_pattern_fields () + { + return fields; + } }; // Struct pattern HIR node representation @@ -593,12 +656,15 @@ class StructPattern : public Pattern { PathInExpression path; StructPatternElements elems; + Analysis::NodeMapping mappings; public: std::string as_string () const override; - StructPattern (PathInExpression struct_path, StructPatternElements elems) - : path (std::move (struct_path)), elems (std::move (elems)) + StructPattern (Analysis::NodeMapping mappings, PathInExpression struct_path, + StructPatternElements elems) + : path (std::move (struct_path)), elems (std::move (elems)), + mappings (mappings) {} bool has_struct_pattern_elems () const { return !elems.is_empty (); } @@ -607,6 +673,14 @@ public: void accept_vis (HIRVisitor &vis) override; + PathInExpression &get_path () { return path; } + StructPatternElements &get_struct_pattern_elems () { return elems; } + + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -620,6 +694,12 @@ protected: class TupleStructItems { public: + enum ItemType + { + RANGE, + NO_RANGE + }; + virtual ~TupleStructItems () {} // TODO: should this store location data? @@ -634,6 +714,8 @@ public: virtual void accept_vis (HIRVisitor &vis) = 0; + virtual ItemType get_item_type () const = 0; + protected: // pure virtual clone implementation virtual TupleStructItems *clone_tuple_struct_items_impl () const = 0; @@ -676,6 +758,14 @@ public: void accept_vis (HIRVisitor &vis) override; + std::vector<std::unique_ptr<Pattern> > &get_patterns () { return patterns; } + const std::vector<std::unique_ptr<Pattern> > &get_patterns () const + { + return patterns; + } + + ItemType get_item_type () const override final { return ItemType::NO_RANGE; } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -732,6 +822,27 @@ public: void accept_vis (HIRVisitor &vis) override; + std::vector<std::unique_ptr<Pattern> > &get_lower_patterns () + { + return lower_patterns; + } + const std::vector<std::unique_ptr<Pattern> > &get_lower_patterns () const + { + return lower_patterns; + } + + // TODO: seems kinda dodgy. Think of better way. + std::vector<std::unique_ptr<Pattern> > &get_upper_patterns () + { + return upper_patterns; + } + const std::vector<std::unique_ptr<Pattern> > &get_upper_patterns () const + { + return upper_patterns; + } + + ItemType get_item_type () const override final { return ItemType::RANGE; } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -746,6 +857,7 @@ class TupleStructPattern : public Pattern { PathInExpression path; std::unique_ptr<TupleStructItems> items; + Analysis::NodeMapping mappings; /* TOOD: should this store location data? current accessor uses path location * data */ @@ -753,14 +865,17 @@ class TupleStructPattern : public Pattern public: std::string as_string () const override; - TupleStructPattern (PathInExpression tuple_struct_path, + TupleStructPattern (Analysis::NodeMapping mappings, + PathInExpression tuple_struct_path, std::unique_ptr<TupleStructItems> items) - : path (std::move (tuple_struct_path)), items (std::move (items)) + : path (std::move (tuple_struct_path)), items (std::move (items)), + mappings (mappings) {} // Copy constructor required to clone TupleStructPattern (TupleStructPattern const &other) - : path (other.path), items (other.items->clone_tuple_struct_items ()) + : path (other.path), items (other.items->clone_tuple_struct_items ()), + mappings (other.mappings) {} // Operator overload assignment operator to clone @@ -768,6 +883,7 @@ public: { path = other.path; items = other.items->clone_tuple_struct_items (); + mappings = other.mappings; return *this; } @@ -780,6 +896,15 @@ public: void accept_vis (HIRVisitor &vis) override; + PathInExpression &get_path () { return path; } + + std::unique_ptr<TupleStructItems> &get_items () { return items; } + + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -955,10 +1080,9 @@ protected: // HIR node representing a tuple pattern class TuplePattern : public Pattern { - // bool has_tuple_pattern_items; std::unique_ptr<TuplePatternItems> items; - Location locus; + Analysis::NodeMapping mappings; public: std::string as_string () const override; @@ -966,13 +1090,15 @@ public: // Returns true if the tuple pattern has items bool has_tuple_pattern_items () const { return items != nullptr; } - TuplePattern (std::unique_ptr<TuplePatternItems> items, Location locus) - : items (std::move (items)), locus (locus) + TuplePattern (Analysis::NodeMapping mappings, + std::unique_ptr<TuplePatternItems> items, Location locus) + : items (std::move (items)), locus (locus), mappings (mappings) {} // Copy constructor requires clone TuplePattern (TuplePattern const &other) - : items (other.items->clone_tuple_pattern_items ()), locus (other.locus) + : items (other.items->clone_tuple_pattern_items ()), locus (other.locus), + mappings (other.mappings) {} // Overload assignment operator to clone @@ -980,6 +1106,7 @@ public: { items = other.items->clone_tuple_pattern_items (); locus = other.locus; + mappings = other.mappings; return *this; } @@ -988,6 +1115,11 @@ public: void accept_vis (HIRVisitor &vis) override; + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1002,6 +1134,7 @@ class GroupedPattern : public Pattern { std::unique_ptr<Pattern> pattern_in_parens; Location locus; + Analysis::NodeMapping mappings; public: std::string as_string () const override @@ -1009,14 +1142,16 @@ public: return "(" + pattern_in_parens->as_string () + ")"; } - GroupedPattern (std::unique_ptr<Pattern> pattern_in_parens, Location locus) - : pattern_in_parens (std::move (pattern_in_parens)), locus (locus) + GroupedPattern (Analysis::NodeMapping mappings, + std::unique_ptr<Pattern> pattern_in_parens, Location locus) + : pattern_in_parens (std::move (pattern_in_parens)), locus (locus), + mappings (mappings) {} // Copy constructor uses clone GroupedPattern (GroupedPattern const &other) : pattern_in_parens (other.pattern_in_parens->clone_pattern ()), - locus (other.locus) + locus (other.locus), mappings (other.mappings) {} // Overload assignment operator to clone @@ -1024,6 +1159,7 @@ public: { pattern_in_parens = other.pattern_in_parens->clone_pattern (); locus = other.locus; + mappings = other.mappings; return *this; } @@ -1036,6 +1172,11 @@ public: void accept_vis (HIRVisitor &vis) override; + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ @@ -1050,16 +1191,19 @@ class SlicePattern : public Pattern { std::vector<std::unique_ptr<Pattern> > items; Location locus; + Analysis::NodeMapping mappings; public: std::string as_string () const override; - SlicePattern (std::vector<std::unique_ptr<Pattern> > items, Location locus) - : items (std::move (items)), locus (locus) + SlicePattern (Analysis::NodeMapping mappings, + std::vector<std::unique_ptr<Pattern> > items, Location locus) + : items (std::move (items)), locus (locus), mappings (mappings) {} // Copy constructor with vector clone - SlicePattern (SlicePattern const &other) : locus (other.locus) + SlicePattern (SlicePattern const &other) + : locus (other.locus), mappings (other.mappings) { items.reserve (other.items.size ()); for (const auto &e : other.items) @@ -1070,6 +1214,7 @@ public: SlicePattern &operator= (SlicePattern const &other) { locus = other.locus; + mappings = other.mappings; items.reserve (other.items.size ()); for (const auto &e : other.items) @@ -1086,6 +1231,11 @@ public: void accept_vis (HIRVisitor &vis) override; + Analysis::NodeMapping get_pattern_mappings () const override final + { + return mappings; + } + protected: /* Use covariance to implement clone function as returning this object rather * than base */ diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index e834553..8b5f3bb 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -328,6 +328,8 @@ public: virtual void accept_vis (HIRVisitor &vis) = 0; + virtual Analysis::NodeMapping get_pattern_mappings () const = 0; + protected: // Clone pattern implementation as pure virtual method virtual Pattern *clone_pattern_impl () const = 0; diff --git a/gcc/rust/typecheck/rust-hir-type-check-implitem.h b/gcc/rust/typecheck/rust-hir-type-check-implitem.h index 64853f5..a860608 100644 --- a/gcc/rust/typecheck/rust-hir-type-check-implitem.h +++ b/gcc/rust/typecheck/rust-hir-type-check-implitem.h @@ -100,8 +100,14 @@ public: // get the name as well required for later on auto param_tyty = TypeCheckType::Resolve (param.get_type ().get ()); + // these are implicit mappings and not used + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, mappings->get_next_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + HIR::IdentifierPattern *param_pattern = new HIR::IdentifierPattern ( - param.get_param_name (), Location (), false, Mutability::Imm, + mapping, param.get_param_name (), Location (), false, Mutability::Imm, std::unique_ptr<HIR::Pattern> (nullptr)); params.push_back ( @@ -223,12 +229,18 @@ public: std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params; if (function.is_method ()) { + // these are implicit mappings and not used + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, mappings->get_next_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + // add the synthetic self param at the front, this is a placeholder for // compilation to know parameter names. The types are ignored but we // reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self_param (); HIR::IdentifierPattern *self_pattern = new HIR::IdentifierPattern ( - "self", self_param.get_locus (), self_param.is_ref (), + mapping, "self", self_param.get_locus (), self_param.is_ref (), self_param.get_mut (), std::unique_ptr<HIR::Pattern> (nullptr)); // might have a specified type diff --git a/gcc/rust/typecheck/rust-hir-type-check.cc b/gcc/rust/typecheck/rust-hir-type-check.cc index 4596973..d2da20e 100644 --- a/gcc/rust/typecheck/rust-hir-type-check.cc +++ b/gcc/rust/typecheck/rust-hir-type-check.cc @@ -276,12 +276,19 @@ TraitItemReference::get_type_from_fn (/*const*/ HIR::TraitItemFunc &fn) const std::vector<std::pair<HIR::Pattern *, TyTy::BaseType *> > params; if (function.is_method ()) { + // these are implicit mappings and not used + auto mappings = Analysis::Mappings::get (); + auto crate_num = mappings->get_current_crate (); + Analysis::NodeMapping mapping (crate_num, mappings->get_next_node_id (), + mappings->get_next_hir_id (crate_num), + UNKNOWN_LOCAL_DEFID); + // add the synthetic self param at the front, this is a placeholder // for compilation to know parameter names. The types are ignored // but we reuse the HIR identifier pattern which requires it HIR::SelfParam &self_param = function.get_self (); HIR::IdentifierPattern *self_pattern - = new HIR::IdentifierPattern ("self", self_param.get_locus (), + = new HIR::IdentifierPattern (mapping, "self", self_param.get_locus (), self_param.is_ref (), self_param.is_mut () ? Mutability::Mut : Mutability::Imm, diff --git a/gcc/rust/util/rust-hir-map.cc b/gcc/rust/util/rust-hir-map.cc index 9e5b509..4b71312 100644 --- a/gcc/rust/util/rust-hir-map.cc +++ b/gcc/rust/util/rust-hir-map.cc @@ -566,6 +566,29 @@ Mappings::lookup_hir_struct_field (CrateNum crateNum, HirId id) } void +Mappings::insert_hir_pattern (CrateNum crateNum, HirId id, + HIR::Pattern *pattern) +{ + hirPatternMappings[crateNum][id] = pattern; + nodeIdToHirMappings[crateNum][pattern->get_pattern_mappings ().get_nodeid ()] + = id; +} + +HIR::Pattern * +Mappings::lookup_hir_pattern (CrateNum crateNum, HirId id) +{ + auto it = hirPatternMappings.find (crateNum); + if (it == hirPatternMappings.end ()) + return nullptr; + + auto iy = it->second.find (id); + if (iy == it->second.end ()) + return nullptr; + + return iy->second; +} + +void Mappings::insert_local_defid_mapping (CrateNum crateNum, LocalDefId id, HIR::Item *item) { diff --git a/gcc/rust/util/rust-hir-map.h b/gcc/rust/util/rust-hir-map.h index 77a4caf..f1f723b 100644 --- a/gcc/rust/util/rust-hir-map.h +++ b/gcc/rust/util/rust-hir-map.h @@ -347,6 +347,9 @@ public: HIR::StructExprField *type); HIR::StructExprField *lookup_hir_struct_field (CrateNum crateNum, HirId id); + void insert_hir_pattern (CrateNum crateNum, HirId id, HIR::Pattern *pattern); + HIR::Pattern *lookup_hir_pattern (CrateNum crateNum, HirId id); + void walk_local_defids_for_crate (CrateNum crateNum, std::function<bool (HIR::Item *)> cb); @@ -479,43 +482,44 @@ private: std::map<CrateNum, HIR::Crate *> hirCrateMappings; std::map<DefId, HIR::Item *> defIdMappings; - std::map<CrateNum, std::map<LocalDefId, HIR::Item *> > localDefIdMappings; - std::map<CrateNum, std::map<HirId, HIR::Module *> > hirModuleMappings; - std::map<CrateNum, std::map<HirId, HIR::Item *> > hirItemMappings; - std::map<CrateNum, std::map<HirId, HIR::Type *> > hirTypeMappings; - std::map<CrateNum, std::map<HirId, HIR::Expr *> > hirExprMappings; - std::map<CrateNum, std::map<HirId, HIR::Stmt *> > hirStmtMappings; - std::map<CrateNum, std::map<HirId, HIR::FunctionParam *> > hirParamMappings; - std::map<CrateNum, std::map<HirId, HIR::StructExprField *> > + std::map<CrateNum, std::map<LocalDefId, HIR::Item *>> localDefIdMappings; + std::map<CrateNum, std::map<HirId, HIR::Module *>> hirModuleMappings; + std::map<CrateNum, std::map<HirId, HIR::Item *>> hirItemMappings; + std::map<CrateNum, std::map<HirId, HIR::Type *>> hirTypeMappings; + std::map<CrateNum, std::map<HirId, HIR::Expr *>> hirExprMappings; + std::map<CrateNum, std::map<HirId, HIR::Stmt *>> hirStmtMappings; + std::map<CrateNum, std::map<HirId, HIR::FunctionParam *>> hirParamMappings; + std::map<CrateNum, std::map<HirId, HIR::StructExprField *>> hirStructFieldMappings; - std::map<CrateNum, std::map<HirId, std::pair<HirId, HIR::ImplItem *> > > + std::map<CrateNum, std::map<HirId, std::pair<HirId, HIR::ImplItem *>>> hirImplItemMappings; - std::map<CrateNum, std::map<HirId, HIR::SelfParam *> > hirSelfParamMappings; + std::map<CrateNum, std::map<HirId, HIR::SelfParam *>> hirSelfParamMappings; std::map<HirId, HIR::ImplBlock *> hirImplItemsToImplMappings; - std::map<CrateNum, std::map<HirId, HIR::ImplBlock *> > hirImplBlockMappings; - std::map<CrateNum, std::map<HirId, HIR::TraitItem *> > hirTraitItemMappings; - std::map<CrateNum, std::map<HirId, HIR::ExternalItem *> > + std::map<CrateNum, std::map<HirId, HIR::ImplBlock *>> hirImplBlockMappings; + std::map<CrateNum, std::map<HirId, HIR::TraitItem *>> hirTraitItemMappings; + std::map<CrateNum, std::map<HirId, HIR::ExternalItem *>> hirExternItemMappings; - std::map<CrateNum, std::map<HirId, HIR::PathExprSegment *> > + std::map<CrateNum, std::map<HirId, HIR::PathExprSegment *>> hirPathSegMappings; - std::map<CrateNum, std::map<HirId, HIR::GenericParam *> > + std::map<CrateNum, std::map<HirId, HIR::GenericParam *>> hirGenericParamMappings; std::map<HirId, HIR::Trait *> hirTraitItemsToTraitMappings; + std::map<CrateNum, std::map<HirId, HIR::Pattern *>> hirPatternMappings; // this maps the lang=<item_type> to DefId mappings std::map<RustLangItem::ItemType, DefId> lang_item_mappings; // canonical paths - std::map<CrateNum, std::map<NodeId, const Resolver::CanonicalPath> > paths; + std::map<CrateNum, std::map<NodeId, const Resolver::CanonicalPath>> paths; // location info - std::map<CrateNum, std::map<NodeId, Location> > locations; + std::map<CrateNum, std::map<NodeId, Location>> locations; // reverse mappings - std::map<CrateNum, std::map<NodeId, HirId> > nodeIdToHirMappings; + std::map<CrateNum, std::map<NodeId, HirId>> nodeIdToHirMappings; // all hirid nodes - std::map<CrateNum, std::set<HirId> > hirNodesWithinCrate; + std::map<CrateNum, std::set<HirId>> hirNodesWithinCrate; // crate names std::map<CrateNum, std::string> crate_names; |