diff options
author | Kushal Pal <kushalpal109@gmail.com> | 2024-01-26 13:21:37 +0530 |
---|---|---|
committer | Arthur Cohen <arthur.cohen@embecosm.com> | 2024-02-07 12:40:24 +0100 |
commit | 1e114b62e6e4941e9d6ed69c005e5901b6b5f21b (patch) | |
tree | a98f374b0d1df3c237c276dc996a5b119db3d4e4 /gcc/rust/ast | |
parent | 6e9dad6a7943a37aba5b1a0609bb9e46741dd1c8 (diff) | |
download | gcc-1e114b62e6e4941e9d6ed69c005e5901b6b5f21b.zip gcc-1e114b62e6e4941e9d6ed69c005e5901b6b5f21b.tar.gz gcc-1e114b62e6e4941e9d6ed69c005e5901b6b5f21b.tar.bz2 |
gccrs: Remove obsolete classes and functions.
Trait functions now use AST::Function class, so classes
AST::TraitItemFunc, AST::TraitItemMethod, AST::TraitFunctionDecl,
AST::TraitMethodDecl and their related functions can be removed.
gcc/rust/ChangeLog:
* ast/rust-ast-collector.cc (TokenCollector::visit):
Remove obsolete classes and functions.
* ast/rust-ast-collector.h:
Likewise.
* ast/rust-ast-full-decls.h (class TraitFunctionDecl):
Likewise.
(class TraitItemFunc):
Likewise.
(class TraitMethodDecl):
Likewise.
(class TraitItemMethod):
Likewise.
* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit):
Likewise.
* ast/rust-ast-visitor.h:
Likewise.
* ast/rust-ast.cc (TraitItemFunc::TraitItemFunc):
Likewise.
(TraitItemFunc::operator=):
Likewise.
(TraitItemFunc::as_string):
Likewise.
(TraitFunctionDecl::as_string):
Likewise.
(TraitItemMethod::TraitItemMethod):
Likewise.
(TraitItemMethod::operator=):
Likewise.
(TraitItemMethod::as_string):
Likewise.
(TraitMethodDecl::as_string):
Likewise.
(TraitItemFunc::accept_vis):
Likewise.
(TraitItemMethod::accept_vis):
Likewise.
* ast/rust-item.h (class TraitFunctionDecl):
Likewise.
(class TraitItemFunc):
Likewise.
(class TraitMethodDecl):
Likewise.
(class TraitItemMethod):
Likewise.
* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
Likewise.
* checks/errors/rust-ast-validation.h:
Likewise.
* checks/errors/rust-feature-gate.h:
Likewise.
* expand/rust-cfg-strip.cc (CfgStrip::maybe_strip_trait_function_decl):
Likewise.
(CfgStrip::maybe_strip_trait_method_decl):
Likewise.
(CfgStrip::visit):
Likewise.
* expand/rust-cfg-strip.h:
Likewise.
* expand/rust-derive.h:
Likewise.
* expand/rust-expand-visitor.cc (ExpandVisitor::expand_trait_function_decl):
Likewise.
(ExpandVisitor::expand_trait_method_decl):
Likewise.
(ExpandVisitor::visit):
Likewise.
* expand/rust-expand-visitor.h:
Likewise.
* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit):
Likewise.
* hir/rust-ast-lower-base.h:
Likewise.
* hir/rust-ast-lower-implitem.cc (ASTLowerTraitItem::visit):
Likewise.
* hir/rust-ast-lower-implitem.h:
Likewise.
* resolve/rust-ast-resolve-base.cc (ResolverBase::visit):
Likewise.
* resolve/rust-ast-resolve-base.h:
Likewise.
* resolve/rust-ast-resolve-implitem.h (visit):
Likewise.
* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit):
Likewise.
* resolve/rust-ast-resolve-item.h:
Likewise.
* resolve/rust-default-resolver.cc (DefaultResolver::visit):
Likewise.
* resolve/rust-default-resolver.h:
Likewise.
* resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit):
Likewise.
* resolve/rust-toplevel-name-resolver-2.0.h:
Likewise.
* util/rust-attributes.cc (AttributeChecker::visit):
Likewise.
* util/rust-attributes.h:
Likewise.
Signed-off-by: Kushal Pal <kushalpal109@gmail.com>
Diffstat (limited to 'gcc/rust/ast')
-rw-r--r-- | gcc/rust/ast/rust-ast-collector.cc | 34 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-collector.h | 2 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-full-decls.h | 4 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-visitor.cc | 45 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast-visitor.h | 6 | ||||
-rw-r--r-- | gcc/rust/ast/rust-ast.cc | 209 | ||||
-rw-r--r-- | gcc/rust/ast/rust-item.h | 406 |
7 files changed, 0 insertions, 706 deletions
diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index d5a98f1..fb0e6f9 100644 --- a/gcc/rust/ast/rust-ast-collector.cc +++ b/gcc/rust/ast/rust-ast-collector.cc @@ -1949,23 +1949,6 @@ TokenCollector::visit_function_common (std::unique_ptr<Type> &return_type, } void -TokenCollector::visit (TraitItemFunc &item) -{ - auto func = item.get_trait_function_decl (); - auto id = func.get_identifier ().as_string (); - - push (Rust::Token::make (FN_KW, item.get_locus ())); - push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id))); - push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION)); - - visit_items_joined_by_separator (func.get_function_params ()); - - push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION)); - - visit_function_common (func.get_return_type (), item.get_definition ()); -} - -void TokenCollector::visit (SelfParam ¶m) { if (param.get_has_ref ()) @@ -1988,23 +1971,6 @@ TokenCollector::visit (SelfParam ¶m) } void -TokenCollector::visit (TraitItemMethod &item) -{ - auto method = item.get_trait_method_decl (); - auto id = method.get_identifier ().as_string (); - - push (Rust::Token::make (FN_KW, item.get_locus ())); - push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id))); - push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION)); - - visit_items_joined_by_separator (method.get_function_params (), COMMA); - - push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION)); - - visit_function_common (method.get_return_type (), item.get_definition ()); -} - -void TokenCollector::visit (TraitItemConst &item) { auto id = item.get_identifier ().as_string (); diff --git a/gcc/rust/ast/rust-ast-collector.h b/gcc/rust/ast/rust-ast-collector.h index 55c13d1..246411f 100644 --- a/gcc/rust/ast/rust-ast-collector.h +++ b/gcc/rust/ast/rust-ast-collector.h @@ -325,9 +325,7 @@ public: void visit (Union &union_item); void visit (ConstantItem &const_item); void visit (StaticItem &static_item); - void visit (TraitItemFunc &item); void visit (SelfParam ¶m); - void visit (TraitItemMethod &item); void visit (TraitItemConst &item); void visit (TraitItemType &item); void visit (Trait &trait); diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h index 0f95149..c96bbfb 100644 --- a/gcc/rust/ast/rust-ast-full-decls.h +++ b/gcc/rust/ast/rust-ast-full-decls.h @@ -193,10 +193,6 @@ class Enum; class Union; class ConstantItem; class StaticItem; -class TraitFunctionDecl; -class TraitItemFunc; -class TraitMethodDecl; -class TraitItemMethod; class TraitItemConst; class TraitItemType; class Trait; diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc index 5865703..cb7ba84 100644 --- a/gcc/rust/ast/rust-ast-visitor.cc +++ b/gcc/rust/ast/rust-ast-visitor.cc @@ -912,51 +912,6 @@ DefaultASTVisitor::visit (AST::StaticItem &static_item) } void -DefaultASTVisitor::visit (AST::TraitFunctionDecl &decl) -{ - visit (decl.get_qualifiers ()); - for (auto &generic : decl.get_generic_params ()) - visit (generic); - visit (decl.get_where_clause ()); - for (auto ¶m : decl.get_function_params ()) - visit (param); - if (decl.has_return_type ()) - visit (decl.get_return_type ()); -} - -void -DefaultASTVisitor::visit (AST::TraitItemFunc &item) -{ - visit_outer_attrs (item); - visit (item.get_trait_function_decl ()); - if (item.has_definition ()) - visit (item.get_definition ()); -} - -void -DefaultASTVisitor::visit (AST::TraitMethodDecl &decl) -{ - visit (decl.get_qualifiers ()); - for (auto &generic : decl.get_generic_params ()) - visit (generic); - visit (decl.get_where_clause ()); - visit (decl.get_self_param ()); - for (auto ¶m : decl.get_function_params ()) - visit (param); - if (decl.has_return_type ()) - visit (decl.get_return_type ()); -} - -void -DefaultASTVisitor::visit (AST::TraitItemMethod &item) -{ - visit_outer_attrs (item); - visit (item.get_trait_method_decl ()); - if (item.has_definition ()) - visit (item.get_definition ()); -} - -void DefaultASTVisitor::visit (AST::TraitItemConst &item) { visit_outer_attrs (item); diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h index 43cd750..6c9715e 100644 --- a/gcc/rust/ast/rust-ast-visitor.h +++ b/gcc/rust/ast/rust-ast-visitor.h @@ -156,8 +156,6 @@ public: virtual void visit (Union &union_item) = 0; virtual void visit (ConstantItem &const_item) = 0; virtual void visit (StaticItem &static_item) = 0; - virtual void visit (TraitItemFunc &item) = 0; - virtual void visit (TraitItemMethod &item) = 0; virtual void visit (TraitItemConst &item) = 0; virtual void visit (TraitItemType &item) = 0; virtual void visit (Trait &trait) = 0; @@ -330,8 +328,6 @@ protected: virtual void visit (AST::Union &union_item) override; virtual void visit (AST::ConstantItem &const_item) override; virtual void visit (AST::StaticItem &static_item) override; - virtual void visit (AST::TraitItemFunc &item) override; - virtual void visit (AST::TraitItemMethod &item) override; virtual void visit (AST::TraitItemConst &item) override; virtual void visit (AST::TraitItemType &item) override; virtual void visit (AST::Trait &trait) override; @@ -420,8 +416,6 @@ protected: virtual void visit (AST::WhereClause &where); virtual void visit (AST::StructField &field); virtual void visit (AST::TupleField &field); - virtual void visit (AST::TraitFunctionDecl &decl); - virtual void visit (AST::TraitMethodDecl &decl); virtual void visit (AST::NamedFunctionParam ¶m); virtual void visit (AST::MacroRule &rule); virtual void visit (AST::MacroInvocData &data); diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index 360a9ce..90fe926 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -3049,203 +3049,6 @@ NamedFunctionParam::as_string () const return str; } -TraitItemFunc::TraitItemFunc (TraitItemFunc const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), decl (other.decl) -{ - node_id = other.node_id; - - // guard to prevent null dereference - if (other.block_expr != nullptr) - block_expr = other.block_expr->clone_block_expr (); -} - -TraitItemFunc & -TraitItemFunc::operator= (TraitItemFunc const &other) -{ - TraitItem::operator= (other); - outer_attrs = other.outer_attrs; - decl = other.decl; - locus = other.locus; - node_id = other.node_id; - - // guard to prevent null dereference - if (other.block_expr != nullptr) - block_expr = other.block_expr->clone_block_expr (); - else - block_expr = nullptr; - - return *this; -} -std::string -TraitItemFunc::as_string () const -{ - std::string str = append_attributes (outer_attrs, OUTER); - - str += "\n" + decl.as_string (); - - str += "\n Definition (block expr): "; - if (has_definition ()) - str += block_expr->as_string (); - else - str += "none"; - - return str; -} - -std::string -TraitFunctionDecl::as_string () const -{ - std::string str - = qualifiers.as_string () + "fn " + function_name.as_string (); - - // generic params - str += "\n Generic params: "; - if (generic_params.empty ()) - { - str += "none"; - } - else - { - for (const auto ¶m : generic_params) - { - // DEBUG: null pointer check - if (param == nullptr) - { - rust_debug ( - "something really terrible has gone wrong - null pointer " - "generic param in trait function decl."); - return "NULL_POINTER_MARK"; - } - - str += "\n " + param->as_string (); - } - } - - str += "\n Function params: "; - if (has_params ()) - { - for (const auto ¶m : function_params) - str += "\n " + param->as_string (); - } - else - { - str += "none"; - } - - str += "\n Return type: "; - if (has_return_type ()) - str += return_type->as_string (); - else - str += "none (void)"; - - str += "\n Where clause: "; - if (has_where_clause ()) - str += where_clause.as_string (); - else - str += "none"; - - return str; -} - -TraitItemMethod::TraitItemMethod (TraitItemMethod const &other) - : TraitItem (other.locus), outer_attrs (other.outer_attrs), decl (other.decl) -{ - node_id = other.node_id; - - // guard to prevent null dereference - if (other.block_expr != nullptr) - block_expr = other.block_expr->clone_block_expr (); -} - -TraitItemMethod & -TraitItemMethod::operator= (TraitItemMethod const &other) -{ - TraitItem::operator= (other); - outer_attrs = other.outer_attrs; - decl = other.decl; - locus = other.locus; - node_id = other.node_id; - - // guard to prevent null dereference - if (other.block_expr != nullptr) - block_expr = other.block_expr->clone_block_expr (); - else - block_expr = nullptr; - - return *this; -} - -std::string -TraitItemMethod::as_string () const -{ - std::string str = append_attributes (outer_attrs, OUTER); - - str += "\n" + decl.as_string (); - - str += "\n Definition (block expr): "; - if (has_definition ()) - str += block_expr->as_string (); - else - str += "none"; - - return str; -} - -std::string -TraitMethodDecl::as_string () const -{ - std::string str - = qualifiers.as_string () + "fn " + function_name.as_string (); - - // generic params - str += "\n Generic params: "; - if (generic_params.empty ()) - { - str += "none"; - } - else - { - for (const auto ¶m : generic_params) - { - // DEBUG: null pointer check - if (param == nullptr) - { - rust_debug ( - "something really terrible has gone wrong - null pointer " - "generic param in trait function decl."); - return "NULL_POINTER_MARK"; - } - - str += "\n " + param->as_string (); - } - } - - str += "\n Function params: "; - if (has_params ()) - { - for (const auto ¶m : function_params) - str += "\n " + param->as_string (); - } - else - { - str += "none"; - } - - str += "\n Return type: "; - if (has_return_type ()) - str += return_type->as_string (); - else - str += "none (void)"; - - str += "\n Where clause: "; - if (has_where_clause ()) - str += where_clause.as_string (); - else - str += "none"; - - return str; -} - std::string TraitItemConst::as_string () const { @@ -5018,18 +4821,6 @@ StaticItem::accept_vis (ASTVisitor &vis) } void -TraitItemFunc::accept_vis (ASTVisitor &vis) -{ - vis.visit (*this); -} - -void -TraitItemMethod::accept_vis (ASTVisitor &vis) -{ - vis.visit (*this); -} - -void TraitItemConst::accept_vis (ASTVisitor &vis) { vis.visit (*this); diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 1defecc..0911719 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -2531,412 +2531,6 @@ protected: } }; -// Function declaration in traits -class TraitFunctionDecl -{ - // TODO: delete and replace with Function decl item? no as no body in this. - FunctionQualifiers qualifiers; - Identifier function_name; - - // bool has_generics; - // Generics generic_params; - std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined - - // bool has_params; - // FunctionParams function_params; - std::vector<std::unique_ptr<Param>> function_params; // inlined - - // bool has_return_type; - std::unique_ptr<Type> return_type; - - // bool has_where_clause; - WhereClause where_clause; - - // should this store location info? - -public: - // Returns whether function decl has generic parameters. - bool has_generics () const { return !generic_params.empty (); } - - // Returns whether function decl has regular parameters. - bool has_params () const { return !function_params.empty (); } - - // Returns whether function has return type (otherwise is void). - bool has_return_type () const { return return_type != nullptr; } - - // Returns whether function has a where clause. - bool has_where_clause () const { return !where_clause.is_empty (); } - - Identifier get_identifier () const { return function_name; } - - // Mega-constructor - TraitFunctionDecl (Identifier function_name, FunctionQualifiers qualifiers, - std::vector<std::unique_ptr<GenericParam>> generic_params, - std::vector<std::unique_ptr<Param>> function_params, - std::unique_ptr<Type> return_type, - WhereClause where_clause) - : qualifiers (std::move (qualifiers)), - function_name (std::move (function_name)), - generic_params (std::move (generic_params)), - function_params (std::move (function_params)), - return_type (std::move (return_type)), - where_clause (std::move (where_clause)) - {} - - // Copy constructor with clone - TraitFunctionDecl (TraitFunctionDecl const &other) - : qualifiers (other.qualifiers), function_name (other.function_name), - where_clause (other.where_clause) - { - // guard to prevent nullptr dereference - if (other.return_type != nullptr) - return_type = other.return_type->clone_type (); - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - function_params.reserve (other.function_params.size ()); - for (const auto &e : other.function_params) - function_params.push_back (e->clone_param ()); - } - - ~TraitFunctionDecl () = default; - - // Overloaded assignment operator with clone - TraitFunctionDecl &operator= (TraitFunctionDecl const &other) - { - function_name = other.function_name; - qualifiers = other.qualifiers; - where_clause = other.where_clause; - - // guard to prevent nullptr dereference - if (other.return_type != nullptr) - return_type = other.return_type->clone_type (); - else - return_type = nullptr; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - function_params.reserve (other.function_params.size ()); - for (const auto &e : other.function_params) - function_params.push_back (e->clone_param ()); - - return *this; - } - - // move constructors - TraitFunctionDecl (TraitFunctionDecl &&other) = default; - TraitFunctionDecl &operator= (TraitFunctionDecl &&other) = default; - - std::string as_string () const; - - // Invalid if function name is empty, so base stripping on that. - void mark_for_strip () { function_name = {""}; } - bool is_marked_for_strip () const { return function_name.empty (); } - - // TODO: this mutable getter seems really dodgy. Think up better way. - std::vector<std::unique_ptr<Param>> &get_function_params () - { - return function_params; - } - const std::vector<std::unique_ptr<Param>> &get_function_params () const - { - return function_params; - } - - std::vector<std::unique_ptr<GenericParam>> &get_generic_params () - { - return generic_params; - } - const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const - { - return generic_params; - } - - // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr<Type> &get_return_type () { return return_type; } - - // TODO: is this better? Or is a "vis_block" better? - WhereClause &get_where_clause () { return where_clause; } - - FunctionQualifiers get_qualifiers () const { return qualifiers; } - FunctionQualifiers &get_qualifiers () { return qualifiers; } -}; - -// Actual trait item function declaration within traits -class TraitItemFunc : public TraitItem -{ - std::vector<Attribute> outer_attrs; - TraitFunctionDecl decl; - std::unique_ptr<BlockExpr> block_expr; - -public: - // Returns whether function has a definition or is just a declaration. - bool has_definition () const { return block_expr != nullptr; } - - TraitItemFunc (TraitFunctionDecl decl, std::unique_ptr<BlockExpr> block_expr, - std::vector<Attribute> outer_attrs, location_t locus) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), - decl (std::move (decl)), block_expr (std::move (block_expr)) - {} - - // Copy constructor with clone - TraitItemFunc (TraitItemFunc const &other); - - // Overloaded assignment operator to clone - - TraitItemFunc &operator= (TraitItemFunc const &other); - - // move constructors - TraitItemFunc (TraitItemFunc &&other) = default; - TraitItemFunc &operator= (TraitItemFunc &&other) = default; - - std::string as_string () const override; - - void accept_vis (ASTVisitor &vis) override; - - // Invalid if trait decl is empty, so base stripping on that. - void mark_for_strip () override { decl.mark_for_strip (); } - bool is_marked_for_strip () const override - { - return decl.is_marked_for_strip (); - } - - // TODO: this mutable getter seems really dodgy. Think up better way. - std::vector<Attribute> &get_outer_attrs () { return outer_attrs; } - const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } - - // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr<BlockExpr> &get_definition () { return block_expr; } - - // TODO: is this better? Or is a "vis_block" better? - TraitFunctionDecl &get_trait_function_decl () - { - // TODO: maybe only allow access if not marked for strip? - return decl; - } - -protected: - // Clone function implementation as (not pure) virtual method - TraitItemFunc *clone_associated_item_impl () const override - { - return new TraitItemFunc (*this); - } -}; - -// Method declaration within traits -class TraitMethodDecl -{ - // TODO: delete and replace with Function decl item? no as no body. - FunctionQualifiers qualifiers; - Identifier function_name; - - // bool has_generics; - // Generics generic_params; - std::vector<std::unique_ptr<GenericParam>> generic_params; // inlined - - // bool has_params; - // FunctionParams function_params; - std::vector<std::unique_ptr<Param>> function_params; // inlined - - // bool has_return_type; - std::unique_ptr<Type> return_type; - - // bool has_where_clause; - WhereClause where_clause; - - // should this store location info? - -public: - // Returns whether method decl has generic parameters. - bool has_generics () const { return !generic_params.empty (); } - - // Returns whether method decl has regular parameters. - bool has_params () const { return !function_params.empty (); } - - // Returns whether method has return type (otherwise is void). - bool has_return_type () const { return return_type != nullptr; } - - // Returns whether method has a where clause. - bool has_where_clause () const { return !where_clause.is_empty (); } - - Identifier get_identifier () const { return function_name; } - - // Mega-constructor - TraitMethodDecl (Identifier function_name, FunctionQualifiers qualifiers, - std::vector<std::unique_ptr<GenericParam>> generic_params, - std::vector<std::unique_ptr<Param>> function_params, - std::unique_ptr<Type> return_type, WhereClause where_clause) - : qualifiers (std::move (qualifiers)), - function_name (std::move (function_name)), - generic_params (std::move (generic_params)), - function_params (std::move (function_params)), - return_type (std::move (return_type)), - where_clause (std::move (where_clause)) - {} - - // Copy constructor with clone - TraitMethodDecl (TraitMethodDecl const &other) - : qualifiers (other.qualifiers), function_name (other.function_name), - where_clause (other.where_clause) - { - // guard to prevent nullptr dereference - if (other.return_type != nullptr) - return_type = other.return_type->clone_type (); - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - function_params.reserve (other.function_params.size ()); - for (const auto &e : other.function_params) - function_params.push_back (e->clone_param ()); - } - - ~TraitMethodDecl () = default; - - // Overloaded assignment operator with clone - TraitMethodDecl &operator= (TraitMethodDecl const &other) - { - function_name = other.function_name; - qualifiers = other.qualifiers; - where_clause = other.where_clause; - - // guard to prevent nullptr dereference - if (other.return_type != nullptr) - return_type = other.return_type->clone_type (); - else - return_type = nullptr; - - generic_params.reserve (other.generic_params.size ()); - for (const auto &e : other.generic_params) - generic_params.push_back (e->clone_generic_param ()); - - function_params.reserve (other.function_params.size ()); - for (const auto &e : other.function_params) - function_params.push_back (e->clone_param ()); - - return *this; - } - - // move constructors - TraitMethodDecl (TraitMethodDecl &&other) = default; - TraitMethodDecl &operator= (TraitMethodDecl &&other) = default; - - std::string as_string () const; - - // Invalid if method name is empty, so base stripping on that. - void mark_for_strip () { function_name = {""}; } - bool is_marked_for_strip () const { return function_name.empty (); } - - // TODO: this mutable getter seems really dodgy. Think up better way. - std::vector<std::unique_ptr<Param>> &get_function_params () - { - return function_params; - } - const std::vector<std::unique_ptr<Param>> &get_function_params () const - { - return function_params; - } - - std::vector<std::unique_ptr<GenericParam>> &get_generic_params () - { - return generic_params; - } - const std::vector<std::unique_ptr<GenericParam>> &get_generic_params () const - { - return generic_params; - } - - // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr<Type> &get_return_type () { return return_type; } - - // TODO: is this better? Or is a "vis_block" better? - WhereClause &get_where_clause () { return where_clause; } - - bool has_self () const - { - return !function_params.empty () && function_params[0]->is_self (); - } - - std::unique_ptr<Param> &get_self_param () - { - rust_assert (has_self ()); - return function_params[0]; - } - const std::unique_ptr<Param> &get_self_param () const - { - rust_assert (has_self ()); - return function_params[0]; - } - - FunctionQualifiers get_qualifiers () const { return qualifiers; } - - FunctionQualifiers &get_qualifiers () { return qualifiers; } -}; - -// Actual trait item method declaration within traits -class TraitItemMethod : public TraitItem -{ - std::vector<Attribute> outer_attrs; - TraitMethodDecl decl; - std::unique_ptr<BlockExpr> block_expr; - -public: - // Returns whether method has a definition or is just a declaration. - bool has_definition () const { return block_expr != nullptr; } - - TraitItemMethod (TraitMethodDecl decl, std::unique_ptr<BlockExpr> block_expr, - std::vector<Attribute> outer_attrs, location_t locus) - : TraitItem (locus), outer_attrs (std::move (outer_attrs)), - decl (std::move (decl)), block_expr (std::move (block_expr)) - {} - - // Copy constructor with clone - TraitItemMethod (TraitItemMethod const &other); - // Overloaded assignment operator to clone - TraitItemMethod &operator= (TraitItemMethod const &other); - - // move constructors - TraitItemMethod (TraitItemMethod &&other) = default; - TraitItemMethod &operator= (TraitItemMethod &&other) = default; - - std::string as_string () const override; - - void accept_vis (ASTVisitor &vis) override; - - // Invalid if trait decl is empty, so base stripping on that. - void mark_for_strip () override { decl.mark_for_strip (); } - bool is_marked_for_strip () const override - { - return decl.is_marked_for_strip (); - } - - // TODO: this mutable getter seems really dodgy. Think up better way. - std::vector<Attribute> &get_outer_attrs () { return outer_attrs; } - const std::vector<Attribute> &get_outer_attrs () const { return outer_attrs; } - - // TODO: is this better? Or is a "vis_block" better? - TraitMethodDecl &get_trait_method_decl () - { - // TODO: maybe only allow access if not marked for strip? - return decl; - } - - // TODO: is this better? Or is a "vis_block" better? - std::unique_ptr<BlockExpr> &get_definition () { return block_expr; } - -protected: - // Clone function implementation as (not pure) virtual method - TraitItemMethod *clone_associated_item_impl () const override - { - return new TraitItemMethod (*this); - } -}; - // Constant item within traits class TraitItemConst : public TraitItem { |