From bb580033c6c857771415723d704642ee7a238a12 Mon Sep 17 00:00:00 2001 From: Owen Avery Date: Fri, 10 Jan 2025 15:50:25 -0500 Subject: [PATCH] Remove dead code related to external functions gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Remove visitor for NamedFunctionParam. * ast/rust-ast-collector.h (TokenCollector::visit): Likewise. * ast/rust-ast-full-decls.h (class NamedFunctionParam): Remove forward declaration. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Remove visitor for NamedFunctionParam. * ast/rust-ast-visitor.h (DefaultASTVisitor::visit): Likewise. * ast/rust-ast.cc (NamedFunctionParam::as_string): Remove. * ast/rust-item.h (class NamedFunctionParam): Remove. (class ExternalFunctionItem): Remove. * parse/rust-parse-impl.h (Parser::parse_named_function_param): Remove. (Parser::parse_named_function_params): Remove. * parse/rust-parse.h (Parser::parse_named_function_param): Remove. (Parser::parse_named_function_params): Remove. Signed-off-by: Owen Avery --- gcc/rust/ast/rust-ast-collector.cc | 23 --- gcc/rust/ast/rust-ast-collector.h | 1 - gcc/rust/ast/rust-ast-full-decls.h | 1 - gcc/rust/ast/rust-ast-visitor.cc | 8 - gcc/rust/ast/rust-ast-visitor.h | 1 - gcc/rust/ast/rust-ast.cc | 16 -- gcc/rust/ast/rust-item.h | 297 ----------------------------- gcc/rust/parse/rust-parse-impl.h | 100 ---------- gcc/rust/parse/rust-parse.h | 4 - 9 files changed, 451 deletions(-) diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc index 8f66742d1196..da0384972ef8 100644 --- a/gcc/rust/ast/rust-ast-collector.cc +++ b/gcc/rust/ast/rust-ast-collector.cc @@ -250,29 +250,6 @@ TokenCollector::visit (Visibility &vis) } } -void -TokenCollector::visit (NamedFunctionParam ¶m) -{ - auto name = param.get_name (); - if (!param.is_variadic ()) - { - push ( - Rust::Token::make_identifier (param.get_locus (), std::move (name))); - push (Rust::Token::make (COLON, UNDEF_LOCATION)); - visit (param.get_type ()); - } - else - { - if (name != "") - { - push (Rust::Token::make_identifier (param.get_locus (), - std::move (name))); - push (Rust::Token::make (COLON, UNDEF_LOCATION)); - } - push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION)); - } -} - void TokenCollector::visit (std::vector> ¶ms) { diff --git a/gcc/rust/ast/rust-ast-collector.h b/gcc/rust/ast/rust-ast-collector.h index ceb161ccc139..b464907ab026 100644 --- a/gcc/rust/ast/rust-ast-collector.h +++ b/gcc/rust/ast/rust-ast-collector.h @@ -210,7 +210,6 @@ class TokenCollector : public ASTVisitor void visit (TupleField &field); void visit (StructField &field); void visit (SimplePathSegment &segment); - void visit (NamedFunctionParam ¶m); void visit (MacroRule &rule); void visit (WhereClause &rule); void visit (std::vector &for_lifetimes); diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h index fffeb1b34660..ae103da4401f 100644 --- a/gcc/rust/ast/rust-ast-full-decls.h +++ b/gcc/rust/ast/rust-ast-full-decls.h @@ -202,7 +202,6 @@ class TraitImpl; class ExternalItem; class ExternalTypeItem; class ExternalStaticItem; -class NamedFunctionParam; class ExternBlock; // rust-macro.h diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc index 228f81a647f2..28142f0421c9 100644 --- a/gcc/rust/ast/rust-ast-visitor.cc +++ b/gcc/rust/ast/rust-ast-visitor.cc @@ -1060,14 +1060,6 @@ DefaultASTVisitor::visit (AST::ExternalStaticItem &item) visit (item.get_type ()); } -void -DefaultASTVisitor::visit (AST::NamedFunctionParam ¶m) -{ - visit_outer_attrs (param); - if (!param.is_variadic ()) - visit (param.get_type ()); -} - void DefaultASTVisitor::visit (AST::ExternBlock &block) { diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h index 42513fe22d3c..eeb4e95826aa 100644 --- a/gcc/rust/ast/rust-ast-visitor.h +++ b/gcc/rust/ast/rust-ast-visitor.h @@ -428,7 +428,6 @@ class DefaultASTVisitor : public ASTVisitor virtual void visit (AST::WhereClause &where); virtual void visit (AST::StructField &field); virtual void visit (AST::TupleField &field); - virtual void visit (AST::NamedFunctionParam ¶m); virtual void visit (AST::MacroRule &rule); virtual void visit (AST::MacroInvocData &data); virtual void visit (AST::MacroTranscriber &transcriber); diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc index f32f0caa3176..d9197e3db059 100644 --- a/gcc/rust/ast/rust-ast.cc +++ b/gcc/rust/ast/rust-ast.cc @@ -2997,22 +2997,6 @@ ExternalStaticItem::as_string () const return str; } -std::string -NamedFunctionParam::as_string () const -{ - std::string str = append_attributes (outer_attrs, OUTER); - - if (has_name ()) - str += "\n" + name; - - if (is_variadic ()) - str += "..."; - else - str += "\n Type: " + param_type->as_string (); - - return str; -} - std::string TraitItemConst::as_string () const { diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h index 6c9789d5999b..a9ee37674c5e 100644 --- a/gcc/rust/ast/rust-item.h +++ b/gcc/rust/ast/rust-item.h @@ -3623,303 +3623,6 @@ class ExternalStaticItem : public ExternalItem } }; -// A named function parameter used in external functions -class NamedFunctionParam -{ - // bool has_name; // otherwise is _ - std::string name; - - std::unique_ptr param_type; - - // seemingly new since writing this node - std::vector outer_attrs; - - NodeId node_id; - location_t locus; - bool variadic; - -public: - /* Returns whether the named function parameter has a name (i.e. name is not - * '_'). */ - bool has_name () const { return name != "_" && name != ""; } - - bool has_outer_attrs () const { return !outer_attrs.empty (); } - - // Returns whether the named function parameter is in an error state. - bool is_error () const - { - // also if identifier is "" but that is probably more costly to compute - return param_type == nullptr && !variadic; - } - - bool is_variadic () const { return variadic; } - - std::string get_name () const { return name; } - - location_t get_locus () { return locus; } - - // Creates an error state named function parameter. - static NamedFunctionParam create_error () - { - return NamedFunctionParam ("", nullptr, {}, UNDEF_LOCATION); - } - - NamedFunctionParam (std::string name, std::unique_ptr param_type, - std::vector outer_attrs, location_t locus) - : name (std::move (name)), param_type (std::move (param_type)), - outer_attrs (std::move (outer_attrs)), - node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus), - variadic (false) - {} - - NamedFunctionParam (std::string name, std::vector outer_attrs, - location_t locus) - : name (std::move (name)), param_type (nullptr), - outer_attrs (std::move (outer_attrs)), - node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus), - variadic (true) - {} - - NamedFunctionParam (std::vector outer_attrs, location_t locus) - : name (""), param_type (nullptr), outer_attrs (std::move (outer_attrs)), - node_id (Analysis::Mappings::get ().get_next_node_id ()), locus (locus), - variadic (true) - {} - - // Copy constructor - NamedFunctionParam (NamedFunctionParam const &other) - : name (other.name), outer_attrs (other.outer_attrs), - variadic (other.variadic) - { - node_id = other.node_id; - // guard to prevent null dereference (only required if error state) - if (other.param_type != nullptr) - param_type = other.param_type->clone_type (); - else - param_type = nullptr; - } - - ~NamedFunctionParam () = default; - - // Overloaded assignment operator to clone - NamedFunctionParam &operator= (NamedFunctionParam const &other) - { - node_id = other.node_id; - name = other.name; - // has_name = other.has_name; - outer_attrs = other.outer_attrs; - - // guard to prevent null dereference (only required if error state) - if (other.param_type != nullptr) - param_type = other.param_type->clone_type (); - else - param_type = nullptr; - - return *this; - } - - // move constructors - NamedFunctionParam (NamedFunctionParam &&other) = default; - NamedFunctionParam &operator= (NamedFunctionParam &&other) = default; - - std::string as_string () const; - - // Based on idea that nane should never be empty. - void mark_for_strip () { param_type = nullptr; }; - bool is_marked_for_strip () const { return is_error (); }; - - // TODO: this mutable getter seems really dodgy. Think up better way. - std::vector &get_outer_attrs () { return outer_attrs; } - const std::vector &get_outer_attrs () const { return outer_attrs; } - - // TODO: is this better? Or is a "vis_block" better? - Type &get_type () - { - rust_assert (param_type != nullptr); - return *param_type; - } - - std::unique_ptr &get_type_ptr () - { - rust_assert (param_type != nullptr); - return param_type; - } - - NodeId get_node_id () const { return node_id; } -}; - -// A function item used in an extern block -class ExternalFunctionItem : public ExternalItem -{ - // bool has_outer_attrs; - std::vector outer_attrs; - - // bool has_visibility; - Visibility visibility; - - Identifier item_name; - location_t locus; - - // bool has_generics; - // Generics generic_params; - std::vector> generic_params; // inlined - - // bool has_return_type; - // FunctionReturnType return_type; - std::unique_ptr return_type; // inlined - - // bool has_where_clause; - WhereClause where_clause; - - std::vector function_params; - -public: - // Returns whether item has generic parameters. - bool has_generics () const { return !generic_params.empty (); } - - // Returns whether item has a return type (otherwise void). - bool has_return_type () const { return return_type != nullptr; } - - // Returns whether item has a where clause. - bool has_where_clause () const { return !where_clause.is_empty (); } - - // Returns whether item has outer attributes. - bool has_outer_attrs () const { return !outer_attrs.empty (); } - - // Returns whether item has non-default visibility. - bool has_visibility () const { return !visibility.is_error (); } - - // Returns whether item has variadic parameters. - bool is_variadic () const - { - return function_params.size () != 0 - && function_params.back ().is_variadic (); - } - - location_t get_locus () const { return locus; } - - Visibility &get_visibility () { return visibility; } - const Visibility &get_visibility () const { return visibility; } - - ExternalFunctionItem ( - Identifier item_name, - std::vector> generic_params, - std::unique_ptr return_type, WhereClause where_clause, - std::vector function_params, Visibility vis, - std::vector outer_attrs, location_t locus) - : ExternalItem (), outer_attrs (std::move (outer_attrs)), - visibility (std::move (vis)), item_name (std::move (item_name)), - locus (locus), generic_params (std::move (generic_params)), - return_type (std::move (return_type)), - where_clause (std::move (where_clause)), - function_params (std::move (function_params)) - { - // TODO: assert that if has variadic outer attrs, then has_variadics is - // true? - } - - // Copy constructor with clone - ExternalFunctionItem (ExternalFunctionItem const &other) - : ExternalItem (other.get_node_id ()), outer_attrs (other.outer_attrs), - visibility (other.visibility), item_name (other.item_name), - locus (other.locus), where_clause (other.where_clause), - function_params (other.function_params) - { - node_id = other.node_id; - // guard to prevent null pointer 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 ()); - } - - // Overloaded assignment operator with clone - ExternalFunctionItem &operator= (ExternalFunctionItem const &other) - { - outer_attrs = other.outer_attrs; - visibility = other.visibility; - item_name = other.item_name; - locus = other.locus; - where_clause = other.where_clause; - function_params = other.function_params; - node_id = other.node_id; - - // guard to prevent null pointer 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 ()); - - return *this; - } - - // move constructors - ExternalFunctionItem (ExternalFunctionItem &&other) = default; - ExternalFunctionItem &operator= (ExternalFunctionItem &&other) = default; - - std::string as_string () const override; - - void accept_vis (ASTVisitor &vis) override; - - // Based on idea that nane should never be empty. - void mark_for_strip () override { item_name = {""}; }; - bool is_marked_for_strip () const override { return item_name.empty (); }; - - // TODO: this mutable getter seems really dodgy. Think up better way. - std::vector &get_outer_attrs () { return outer_attrs; } - const std::vector &get_outer_attrs () const { return outer_attrs; } - - std::vector &get_function_params () - { - return function_params; - } - const std::vector &get_function_params () const - { - return function_params; - } - - std::vector> &get_generic_params () - { - return generic_params; - } - const std::vector> &get_generic_params () const - { - return generic_params; - } - - // TODO: is this better? Or is a "vis_block" better? - WhereClause &get_where_clause () { return where_clause; } - - // TODO: is this better? Or is a "vis_block" better? - Type &get_return_type () - { - rust_assert (has_return_type ()); - return *return_type; - } - - std::unique_ptr &get_return_type_ptr () - { - rust_assert (has_return_type ()); - return return_type; - } - - Identifier get_identifier () const { return item_name; }; - -protected: - /* Use covariance to implement clone function as returning this object - * rather than base */ - ExternalFunctionItem *clone_external_item_impl () const override - { - return new ExternalFunctionItem (*this); - } -}; - // An extern block AST node class ExternBlock : public VisItem { diff --git a/gcc/rust/parse/rust-parse-impl.h b/gcc/rust/parse/rust-parse-impl.h index 65d6a79f571c..17bad9fb2c4c 100644 --- a/gcc/rust/parse/rust-parse-impl.h +++ b/gcc/rust/parse/rust-parse-impl.h @@ -5936,106 +5936,6 @@ Parser::parse_extern_block (AST::Visibility vis, std::move (outer_attrs), locus)); } -template -AST::NamedFunctionParam -Parser::parse_named_function_param () -{ - AST::AttrVec outer_attrs = parse_outer_attributes (); - location_t locus = lexer.peek_token ()->get_locus (); - - if (lexer.peek_token ()->get_id () == ELLIPSIS) // Unnamed variadic - { - lexer.skip_token (); // Skip ellipsis - return AST::NamedFunctionParam (std::move (outer_attrs), locus); - } - - // parse identifier/_ - std::string name; - - const_TokenPtr t = lexer.peek_token (); - location_t name_location = t->get_locus (); - switch (t->get_id ()) - { - case IDENTIFIER: - name = t->get_str (); - lexer.skip_token (); - break; - case UNDERSCORE: - name = "_"; - lexer.skip_token (); - break; - default: - // this is not a function param, but not necessarily an error - return AST::NamedFunctionParam::create_error (); - } - - if (!skip_token (COLON)) - { - // skip after somewhere? - return AST::NamedFunctionParam::create_error (); - } - - if (lexer.peek_token ()->get_id () == ELLIPSIS) // Named variadic - { - lexer.skip_token (); // Skip ellipsis - return AST::NamedFunctionParam (std::move (name), std::move (outer_attrs), - locus); - } - - // parse (required) type - std::unique_ptr param_type = parse_type (); - if (param_type == nullptr) - { - Error error ( - lexer.peek_token ()->get_locus (), - "could not parse param type in extern block function declaration"); - add_error (std::move (error)); - - skip_after_semicolon (); - return AST::NamedFunctionParam::create_error (); - } - - return AST::NamedFunctionParam (std::move (name), std::move (param_type), - std::move (outer_attrs), name_location); -} - -template -template -std::vector -Parser::parse_named_function_params ( - EndTokenPred is_end_token) -{ - std::vector params; - if (is_end_token (lexer.peek_token ()->get_id ())) - return params; - - auto initial_param = parse_named_function_param (); - if (initial_param.is_error ()) - return params; - - params.push_back (std::move (initial_param)); - auto t = lexer.peek_token (); - while (t->get_id () == COMMA) - { - lexer.skip_token (); - if (is_end_token (lexer.peek_token ()->get_id ())) - break; - - auto param = parse_named_function_param (); - if (param.is_error ()) - { - Error error (lexer.peek_token ()->get_locus (), - "failed to parse param in c function params"); - add_error (error); - return std::vector (); - } - params.push_back (std::move (param)); - t = lexer.peek_token (); - } - params.shrink_to_fit (); - return params; -} - // Parses a single extern block item (static or function declaration). template std::unique_ptr diff --git a/gcc/rust/parse/rust-parse.h b/gcc/rust/parse/rust-parse.h index 95c0a0b94289..6c50ba9523d9 100644 --- a/gcc/rust/parse/rust-parse.h +++ b/gcc/rust/parse/rust-parse.h @@ -310,10 +310,6 @@ template class Parser AST::Lifetime lifetime_from_token (const_TokenPtr tok); std::unique_ptr parse_external_type_item (AST::Visibility vis, AST::AttrVec outer_attrs); - AST::NamedFunctionParam parse_named_function_param (); - template - std::vector - parse_named_function_params (EndTokenPred is_end_token); std::unique_ptr parse_type_alias (AST::Visibility vis, AST::AttrVec outer_attrs);