diff options
author | Marc Poulhiès <dkm@kataplop.net> | 2023-06-28 22:02:37 +0200 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-01-16 18:49:30 +0100 |
commit | 16510f1c74d30c2ce4d3134db52199484c3c79e2 (patch) | |
tree | 545bbd2c11bd46c26267c531600dca271575761e /gcc/rust/hir/tree/rust-hir.h | |
parent | 96065850ce78f2689c2a3c3d4816c7a7043d062b (diff) | |
download | gcc-16510f1c74d30c2ce4d3134db52199484c3c79e2.zip gcc-16510f1c74d30c2ce4d3134db52199484c3c79e2.tar.gz gcc-16510f1c74d30c2ce4d3134db52199484c3c79e2.tar.bz2 |
gccrs: factor out inner attributes in a class
Introduce WithInnerAttrs class that can be inherited instead of
duplicating the same data/method in all HIR classes.
gcc/rust/ChangeLog:
* hir/tree/rust-hir.h (class WithInnerAttrs): New.
(class Item): Adjust to new WithInnerAttrs class.
(struct Crate): Likewise.
* hir/rust-hir-dump.cc (Dump::go): Likewise.
(Dump::visit): Likewise.
* hir/tree/rust-hir-expr.h (class GroupedExpr): Likewise.
(class ArrayExpr): Likewise.
(class TupleExpr): Likewise.
(class StructExprStruct): Likewise.
(class BlockExpr): Likewise.
(class MatchExpr): Likewise.
* hir/tree/rust-hir-item.h (TypeParam::get_outer_attribute): New.
(class Module): Adjust to new WithInnerAttrs class.
(struct StructField): change struct to...
(class StructField): ... a class.
(class ImplBlock): Adjust to new WithInnerAttrs class.
(class ExternBlock): Likewise.
Signed-off-by: Marc Poulhiès <dkm@kataplop.net>
Diffstat (limited to 'gcc/rust/hir/tree/rust-hir.h')
-rw-r--r-- | gcc/rust/hir/tree/rust-hir.h | 41 |
1 files changed, 29 insertions, 12 deletions
diff --git a/gcc/rust/hir/tree/rust-hir.h b/gcc/rust/hir/tree/rust-hir.h index b3d3f7d..b4a9616 100644 --- a/gcc/rust/hir/tree/rust-hir.h +++ b/gcc/rust/hir/tree/rust-hir.h @@ -41,6 +41,30 @@ class HIRPatternVisitor; class HIRImplVisitor; class HIRTypeVisitor; +class WithOuterAttrs +{ +protected: + AST::AttrVec outer_attrs; + +public: + AST::AttrVec &get_outer_attrs () { return outer_attrs; } + const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } + + WithOuterAttrs (AST::AttrVec outer_attrs) + : outer_attrs (std::move (outer_attrs)){}; +}; + +class WithInnerAttrs +{ +protected: + AST::AttrVec inner_attrs; + +public: + AST::AttrVec get_inner_attrs () const { return inner_attrs; } + WithInnerAttrs (AST::AttrVec inner_attrs) + : inner_attrs (std::move (inner_attrs)){}; +}; + // forward decl for use in token tree method class Token; @@ -167,12 +191,9 @@ protected: }; // Rust "item" HIR node (declaration of top-level/module-level allowed stuff) -class Item : public Stmt +class Item : public Stmt, public WithOuterAttrs { - AST::AttrVec outer_attrs; - // TODO: should outer attrs be defined here or in each derived class? - public: enum class ItemKind { @@ -210,16 +231,13 @@ public: add_crate_name (std::vector<std::string> &names ATTRIBUTE_UNUSED) const {} - AST::AttrVec &get_outer_attrs () { return outer_attrs; } - const AST::AttrVec &get_outer_attrs () const { return outer_attrs; } - bool is_item () const override final { return true; } protected: // Constructor Item (Analysis::NodeMapping mappings, AST::AttrVec outer_attribs = AST::AttrVec ()) - : Stmt (std::move (mappings)), outer_attrs (std::move (outer_attribs)) + : Stmt (std::move (mappings)), WithOuterAttrs (std::move (outer_attribs)) {} // Clone function implementation as pure virtual method @@ -836,9 +854,8 @@ protected: }; // A crate HIR object - holds all the data for a single compilation unit -struct Crate +struct Crate : public WithInnerAttrs { - AST::AttrVec inner_attrs; // dodgy spacing required here /* TODO: is it better to have a vector of items here or a module (implicit * top-level one)? */ @@ -850,13 +867,13 @@ public: // Constructor Crate (std::vector<std::unique_ptr<Item> > items, AST::AttrVec inner_attrs, Analysis::NodeMapping mappings) - : inner_attrs (std::move (inner_attrs)), items (std::move (items)), + : WithInnerAttrs (std::move (inner_attrs)), items (std::move (items)), mappings (mappings) {} // Copy constructor with vector clone Crate (Crate const &other) - : inner_attrs (other.inner_attrs), mappings (other.mappings) + : WithInnerAttrs (other.inner_attrs), mappings (other.mappings) { items.reserve (other.items.size ()); for (const auto &e : other.items) |