From 00539210bfad17fec1ddc0bd55b213ef3da0471e Mon Sep 17 00:00:00 2001 From: Arthur Cohen Date: Fri, 26 May 2023 15:36:23 +0200 Subject: [PATCH] gccrs: derive: Factor common fields inside the base visitor gcc/rust/ChangeLog: * expand/rust-derive.h: Store AstBuilder and location. * expand/rust-derive.cc (DeriveVisitor::DeriveVisitor): Update constructor. * expand/rust-derive-clone.h: Remove members now stored in `DeriveVisitor`. * expand/rust-derive-copy.h: Likewise. * expand/rust-derive-clone.cc (DeriveClone::DeriveClone): Update constructor. * expand/rust-derive-copy.cc (DeriveCopy::DeriveCopy): Likewise. --- gcc/rust/expand/rust-derive-clone.cc | 2 +- gcc/rust/expand/rust-derive-clone.h | 5 +---- gcc/rust/expand/rust-derive-copy.cc | 3 +-- gcc/rust/expand/rust-derive-copy.h | 2 -- gcc/rust/expand/rust-derive.cc | 4 ++++ gcc/rust/expand/rust-derive.h | 7 +++++++ 6 files changed, 14 insertions(+), 9 deletions(-) diff --git a/gcc/rust/expand/rust-derive-clone.cc b/gcc/rust/expand/rust-derive-clone.cc index 8529bac50193..2ffcafc957b1 100644 --- a/gcc/rust/expand/rust-derive-clone.cc +++ b/gcc/rust/expand/rust-derive-clone.cc @@ -88,7 +88,7 @@ DeriveClone::clone_impl (std::unique_ptr &&clone_fn, // TODO: Create new `make_qualified_call` helper function DeriveClone::DeriveClone (Location loc) - : loc (loc), expanded (nullptr), builder (AstBuilder (loc)) + : DeriveVisitor (loc), expanded (nullptr) {} std::unique_ptr diff --git a/gcc/rust/expand/rust-derive-clone.h b/gcc/rust/expand/rust-derive-clone.h index 6c82a6478851..e1fadc791634 100644 --- a/gcc/rust/expand/rust-derive-clone.h +++ b/gcc/rust/expand/rust-derive-clone.h @@ -20,7 +20,6 @@ #define RUST_DERIVE_CLONE_H #include "rust-derive.h" -#include "rust-ast-builder.h" namespace Rust { namespace AST { @@ -33,9 +32,7 @@ public: std::unique_ptr go (Item &item); private: - Location loc; - std::unique_ptr expanded; - AstBuilder builder; + std::unique_ptr expanded; /** * Create a call to "clone". For now, this creates a call to diff --git a/gcc/rust/expand/rust-derive-copy.cc b/gcc/rust/expand/rust-derive-copy.cc index d578849c06b8..bc5db626fbae 100644 --- a/gcc/rust/expand/rust-derive-copy.cc +++ b/gcc/rust/expand/rust-derive-copy.cc @@ -22,8 +22,7 @@ namespace Rust { namespace AST { -DeriveCopy::DeriveCopy (Location loc) - : loc (loc), builder (AstBuilder (loc)), expanded (nullptr) +DeriveCopy::DeriveCopy (Location loc) : DeriveVisitor (loc), expanded (nullptr) {} std::unique_ptr diff --git a/gcc/rust/expand/rust-derive-copy.h b/gcc/rust/expand/rust-derive-copy.h index dce1f5e9a950..0e1e2076066d 100644 --- a/gcc/rust/expand/rust-derive-copy.h +++ b/gcc/rust/expand/rust-derive-copy.h @@ -32,8 +32,6 @@ public: std::unique_ptr go (Item &); private: - Location loc; - AstBuilder builder; std::unique_ptr expanded; /** diff --git a/gcc/rust/expand/rust-derive.cc b/gcc/rust/expand/rust-derive.cc index 8f9b206da4be..1a7413b359b2 100644 --- a/gcc/rust/expand/rust-derive.cc +++ b/gcc/rust/expand/rust-derive.cc @@ -23,6 +23,10 @@ namespace Rust { namespace AST { +DeriveVisitor::DeriveVisitor (Location loc) + : loc (loc), builder (AstBuilder (loc)) +{} + std::unique_ptr DeriveVisitor::derive (Item &item, const Attribute &attr, BuiltinMacro to_derive) diff --git a/gcc/rust/expand/rust-derive.h b/gcc/rust/expand/rust-derive.h index 9a62c29e58f3..01f3f024712e 100644 --- a/gcc/rust/expand/rust-derive.h +++ b/gcc/rust/expand/rust-derive.h @@ -21,6 +21,7 @@ #include "rust-ast-full.h" #include "rust-ast-visitor.h" +#include "rust-ast-builder.h" #include "rust-macro-builtins.h" namespace Rust { @@ -36,6 +37,12 @@ public: static std::unique_ptr derive (Item &item, const Attribute &derive, BuiltinMacro to_derive); +protected: + DeriveVisitor (Location loc); + + Location loc; + AstBuilder builder; + private: // the 4 "allowed" visitors, which a derive-visitor can specify and override virtual void visit_struct (StructStruct &struct_item) = 0; -- 2.47.2