Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unify TraitItemMethod & TraitItemFunc with Function #2815

Merged
merged 7 commits into from
Feb 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 0 additions & 34 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1948,23 +1948,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 &param)
{
Expand All @@ -1987,23 +1970,6 @@ TokenCollector::visit (SelfParam &param)
}
}

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)
{
Expand Down
2 changes: 0 additions & 2 deletions gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -325,9 +325,7 @@ class TokenCollector : public ASTVisitor
void visit (Union &union_item);
void visit (ConstantItem &const_item);
void visit (StaticItem &static_item);
void visit (TraitItemFunc &item);
void visit (SelfParam &param);
void visit (TraitItemMethod &item);
void visit (TraitItemConst &item);
void visit (TraitItemType &item);
void visit (Trait &trait);
Expand Down
4 changes: 0 additions & 4 deletions gcc/rust/ast/rust-ast-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
45 changes: 0 additions & 45 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -911,51 +911,6 @@ DefaultASTVisitor::visit (AST::StaticItem &static_item)
visit (static_item.get_expr ());
}

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 &param : 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 &param : 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)
{
Expand Down
6 changes: 0 additions & 6 deletions gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,6 @@ class ASTVisitor
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;
Expand Down Expand Up @@ -330,8 +328,6 @@ class DefaultASTVisitor : public ASTVisitor
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;
Expand Down Expand Up @@ -420,8 +416,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::TraitFunctionDecl &decl);
virtual void visit (AST::TraitMethodDecl &decl);
virtual void visit (AST::NamedFunctionParam &param);
virtual void visit (AST::MacroRule &rule);
virtual void visit (AST::MacroInvocData &data);
Expand Down
209 changes: 0 additions & 209 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 &param : 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 &param : 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 &param : 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 &param : 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
{
Expand Down Expand Up @@ -5017,18 +4820,6 @@ StaticItem::accept_vis (ASTVisitor &vis)
vis.visit (*this);
}

void
TraitItemFunc::accept_vis (ASTVisitor &vis)
{
vis.visit (*this);
}

void
TraitItemMethod::accept_vis (ASTVisitor &vis)
{
vis.visit (*this);
}

void
TraitItemConst::accept_vis (ASTVisitor &vis)
{
Expand Down
6 changes: 3 additions & 3 deletions gcc/rust/ast/rust-ast.h
Original file line number Diff line number Diff line change
Expand Up @@ -1907,11 +1907,11 @@ class SingleASTNode : public Visitable
return std::move (item);
}

std::unique_ptr<TraitItem> take_trait_item ()
std::unique_ptr<AssociatedItem> take_trait_item ()
{
rust_assert (!is_error ());
return std::unique_ptr<TraitItem> (
static_cast<TraitItem *> (assoc_item.release ()));
return std::unique_ptr<AssociatedItem> (
static_cast<AssociatedItem *> (assoc_item.release ()));
}

std::unique_ptr<ExternalItem> take_external_item ()
Expand Down
Loading
Loading