aboutsummaryrefslogtreecommitdiff
path: root/gcc/rust/ast
diff options
context:
space:
mode:
authorOwen Avery <powerboat9.gamer@gmail.com>2023-10-28 01:04:19 -0400
committerP-E-P <32375388+P-E-P@users.noreply.github.com>2023-11-09 21:02:22 +0000
commit9864d7fe8a6dd8abe1cf95e36e35b1762d4d34fe (patch)
tree75a272783d166df0bdd9cbb006a351df56425c6d /gcc/rust/ast
parent25652e25b53c1a47162256dcf644d2f19f9b0966 (diff)
downloadgcc-9864d7fe8a6dd8abe1cf95e36e35b1762d4d34fe.zip
gcc-9864d7fe8a6dd8abe1cf95e36e35b1762d4d34fe.tar.gz
gcc-9864d7fe8a6dd8abe1cf95e36e35b1762d4d34fe.tar.bz2
Replace AST::Method with existing AST::Function
gcc/rust/ChangeLog: * ast/rust-item.h (class Method): Remove. (Function::self_param): New. (Function::has_self_param): New. (Function::Function): Initialize self_param. (Function::operator=): Likewise. (Function::get_self_param): New. * ast/rust-ast.cc (Method::as_string): Remove. (Method::accept_vis): Remove. * ast/rust-ast-collector.cc (TokenCollector::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * ast/rust-ast-collector.h (TokenCollector::visit): Remove AST::Method visitor. * ast/rust-ast-full-decls.h (class Method): Remove. * ast/rust-ast-visitor.h (ASTVisitor::visit): Remove AST::Method visitor. (DefaultASTVisitor::visit): Likewise. * ast/rust-ast-visitor.cc (DefaultASTVisitor::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * checks/errors/rust-feature-gate.cc (FeatureGate::visit): Remove AST::Method visitor. * checks/errors/rust-feature-gate.h (FeatureGate::visit): Likewise.. * expand/rust-cfg-strip.cc (CfgStrip::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * expand/rust-cfg-strip.h (CfgStrip::visit): Remove AST::Method visitor. * expand/rust-derive-clone.cc (DeriveClone::clone_fn): Return AST::Function instead of AST::Method. * expand/rust-derive.h (DeriveVisitor::visit): Remove AST::Method visitor. * expand/rust-expand-visitor.cc (ExpandVisitor::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * expand/rust-expand-visitor.h: (ExpandVisitor::visit): Remove AST::Method visitor. * hir/rust-ast-lower-base.cc (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-base.h (ASTLoweringBase::visit): Likewise. * hir/rust-ast-lower-implitem.h (ASTLowerImplItem::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * parse/rust-parse-impl.h: Include optional.h. (Parser::parse_function): Adjust AST::Function construction. (Parser::parse_inherent_impl_function_or_method): Construct AST::Function instead of AST::Method, adjust AST::Function construction. (Parser::parse_trait_impl_function_or_method): Likewise. (Parser::parse_method): Return std::unique_ptr<AST::Function> instead of AST::Method. * parse/rust-parse.h (Parser::parse_method): Likewise. * resolve/rust-ast-resolve-base.cc (ResolverBase::visit): Remove AST::Method visitor. * resolve/rust-ast-resolve-base.h (ResolverBase::visit): Likewise. * resolve/rust-ast-resolve-implitem.h (ResolveToplevelImplItem::visit): Likewise. * resolve/rust-ast-resolve-item.cc (ResolveItem::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * resolve/rust-ast-resolve-item.h (ResolveItem::visit): Remove AST::Method visitor. * resolve/rust-default-resolver.cc (DefaultResolver::visit): Remove AST::Method visitor. * resolve/rust-default-resolver.h (DefaultResolver::visit): Likewise. * resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit): Remove AST::Method visitor, handle self_param in AST::Function visitor. * resolve/rust-early-name-resolver.h (EarlyNameResolver::visit): Remove AST::Method visitor. * resolve/rust-toplevel-name-resolver-2.0.cc (TopLevel::visit): Likewise. * resolve/rust-toplevel-name-resolver-2.0.h (TopLevel::visit): Likewise. * util/rust-attributes.cc (AttributeChecker::visit): Likewise. * util/rust-attributes.h (AttributeChecker::visit): Likewise. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
Diffstat (limited to 'gcc/rust/ast')
-rw-r--r--gcc/rust/ast/rust-ast-collector.cc43
-rw-r--r--gcc/rust/ast/rust-ast-collector.h1
-rw-r--r--gcc/rust/ast/rust-ast-full-decls.h1
-rw-r--r--gcc/rust/ast/rust-ast-visitor.cc19
-rw-r--r--gcc/rust/ast/rust-ast-visitor.h2
-rw-r--r--gcc/rust/ast/rust-ast.cc69
-rw-r--r--gcc/rust/ast/rust-item.h227
7 files changed, 28 insertions, 334 deletions
diff --git a/gcc/rust/ast/rust-ast-collector.cc b/gcc/rust/ast/rust-ast-collector.cc
index 9997523..8326072 100644
--- a/gcc/rust/ast/rust-ast-collector.cc
+++ b/gcc/rust/ast/rust-ast-collector.cc
@@ -1627,41 +1627,6 @@ TokenCollector::visit (TypeBoundWhereClauseItem &item)
}
void
-TokenCollector::visit (Method &method)
-{
- visit (method.get_visibility ());
- auto method_name = method.get_method_name ().as_string ();
- auto qualifiers = method.get_qualifiers ();
- visit (qualifiers);
-
- push (Rust::Token::make (FN_TOK, method.get_locus ()));
- push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (method_name)));
- push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
-
- visit (method.get_self_param ());
- if (!method.get_function_params ().empty ())
- {
- push (Rust::Token::make (COMMA, UNDEF_LOCATION));
- visit_items_joined_by_separator (method.get_function_params (), COMMA);
- }
-
- push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
-
- if (method.has_return_type ())
- {
- push (Rust::Token::make (RETURN_TYPE, UNDEF_LOCATION));
- visit (method.get_return_type ());
- }
-
- auto &block = method.get_definition ();
- if (!block)
- push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
- else
- visit (block);
- newline ();
-}
-
-void
TokenCollector::visit (Module &module)
{
// Syntax:
@@ -1817,6 +1782,14 @@ TokenCollector::visit (Function &function)
visit (function.get_generic_params ());
push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));
+
+ if (function.has_self_param ())
+ {
+ visit (function.get_self_param ());
+ if (!function.get_function_params ().empty ())
+ push (Rust::Token::make (COMMA, UNDEF_LOCATION));
+ }
+
visit_items_joined_by_separator (function.get_function_params ());
push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
diff --git a/gcc/rust/ast/rust-ast-collector.h b/gcc/rust/ast/rust-ast-collector.h
index 2dae423..4765528 100644
--- a/gcc/rust/ast/rust-ast-collector.h
+++ b/gcc/rust/ast/rust-ast-collector.h
@@ -266,7 +266,6 @@ public:
void visit (TypeParam &param);
void visit (LifetimeWhereClauseItem &item);
void visit (TypeBoundWhereClauseItem &item);
- void visit (Method &method);
void visit (Module &module);
void visit (ExternCrate &crate);
void visit (UseTreeGlob &use_tree);
diff --git a/gcc/rust/ast/rust-ast-full-decls.h b/gcc/rust/ast/rust-ast-full-decls.h
index 64b9b3d..9e961f9 100644
--- a/gcc/rust/ast/rust-ast-full-decls.h
+++ b/gcc/rust/ast/rust-ast-full-decls.h
@@ -171,7 +171,6 @@ class SelfParam;
class FunctionQualifiers;
class FunctionParam;
struct Visibility;
-class Method;
class VisItem;
class Module;
class ExternCrate;
diff --git a/gcc/rust/ast/rust-ast-visitor.cc b/gcc/rust/ast/rust-ast-visitor.cc
index 4b954fd..63f850f 100644
--- a/gcc/rust/ast/rust-ast-visitor.cc
+++ b/gcc/rust/ast/rust-ast-visitor.cc
@@ -729,23 +729,6 @@ DefaultASTVisitor::visit (AST::FunctionParam &param)
}
void
-DefaultASTVisitor::visit (AST::Method &method)
-{
- visit_outer_attrs (method);
- visit (method.get_visibility ());
- visit (method.get_qualifiers ());
- for (auto &generic : method.get_generic_params ())
- visit (generic);
- visit (method.get_self_param ());
- for (auto &param : method.get_function_params ())
- visit (param);
- if (method.has_return_type ())
- visit (method.get_return_type ());
- visit (method.get_where_clause ());
- visit (method.get_definition ());
-}
-
-void
DefaultASTVisitor::visit (AST::Module &module)
{
visit_outer_attrs (module);
@@ -794,6 +777,8 @@ DefaultASTVisitor::visit (AST::Function &function)
visit (function.get_qualifiers ());
for (auto &generic : function.get_generic_params ())
visit (generic);
+ if (function.has_self_param ())
+ visit (function.get_self_param ());
for (auto &param : function.get_function_params ())
visit (param);
if (function.has_return_type ())
diff --git a/gcc/rust/ast/rust-ast-visitor.h b/gcc/rust/ast/rust-ast-visitor.h
index 9402947..98eab19 100644
--- a/gcc/rust/ast/rust-ast-visitor.h
+++ b/gcc/rust/ast/rust-ast-visitor.h
@@ -132,7 +132,6 @@ public:
// virtual void visit(WhereClauseItem& item) = 0;
virtual void visit (LifetimeWhereClauseItem &item) = 0;
virtual void visit (TypeBoundWhereClauseItem &item) = 0;
- virtual void visit (Method &method) = 0;
virtual void visit (Module &module) = 0;
virtual void visit (ExternCrate &crate) = 0;
// virtual void visit(UseTree& use_tree) = 0;
@@ -308,7 +307,6 @@ protected:
virtual void visit (AST::TypeParam &param) override;
virtual void visit (AST::LifetimeWhereClauseItem &item) override;
virtual void visit (AST::TypeBoundWhereClauseItem &item) override;
- virtual void visit (AST::Method &method) override;
virtual void visit (AST::Module &module) override;
virtual void visit (AST::ExternCrate &crate) override;
virtual void visit (AST::UseTreeGlob &use_tree) override;
diff --git a/gcc/rust/ast/rust-ast.cc b/gcc/rust/ast/rust-ast.cc
index b67a13b..9c6862e 100644
--- a/gcc/rust/ast/rust-ast.cc
+++ b/gcc/rust/ast/rust-ast.cc
@@ -726,69 +726,6 @@ InherentImpl::as_string () const
}
std::string
-Method::as_string () const
-{
- std::string str ("Method: \n ");
-
- str += vis.as_string () + " " + qualifiers.as_string ();
-
- str += " fn " + method_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 method.");
- return "NULL_POINTER_MARK";
- }
-
- str += "\n " + param->as_string ();
- }
- }
-
- str += "\n Self param: " + self_param.as_string ();
-
- str += "\n Function params: ";
- if (function_params.empty ())
- {
- str += "none";
- }
- else
- {
- for (const auto &param : function_params)
- str += "\n " + param.as_string ();
- }
-
- 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";
-
- str += "\n Block expr (body): \n ";
- str += function_body->as_string ();
-
- return str;
-}
-
-std::string
StructStruct::as_string () const
{
std::string str = VisItem::as_string ();
@@ -4884,12 +4821,6 @@ TypeBoundWhereClauseItem::accept_vis (ASTVisitor &vis)
}
void
-Method::accept_vis (ASTVisitor &vis)
-{
- vis.visit (*this);
-}
-
-void
Module::accept_vis (ASTVisitor &vis)
{
vis.visit (*this);
diff --git a/gcc/rust/ast/rust-item.h b/gcc/rust/ast/rust-item.h
index 97bc8d7..0e38103 100644
--- a/gcc/rust/ast/rust-item.h
+++ b/gcc/rust/ast/rust-item.h
@@ -755,214 +755,6 @@ protected:
}
};
-// A method (function belonging to a type)
-class Method : public InherentImplItem, public TraitImplItem
-{
- std::vector<Attribute> outer_attrs;
- Visibility vis;
- FunctionQualifiers qualifiers;
- Identifier method_name;
- std::vector<std::unique_ptr<GenericParam>> generic_params;
- SelfParam self_param;
- std::vector<FunctionParam> function_params;
- std::unique_ptr<Type> return_type;
- WhereClause where_clause;
- std::unique_ptr<BlockExpr> function_body;
- location_t locus;
- NodeId node_id;
- bool is_default;
-
-public:
- // Returns whether the method is in an error state.
- bool is_error () const
- {
- return function_body == nullptr || method_name.empty ()
- || self_param.is_error ();
- }
-
- // Creates an error state method.
- static Method create_error ()
- {
- return Method ({""}, FunctionQualifiers (UNDEF_LOCATION, NONE, true),
- std::vector<std::unique_ptr<GenericParam>> (),
- SelfParam::create_error (), std::vector<FunctionParam> (),
- nullptr, WhereClause::create_empty (), nullptr,
- Visibility::create_error (), std::vector<Attribute> (), {});
- }
-
- // Returns whether the method has generic parameters.
- bool has_generics () const { return !generic_params.empty (); }
-
- // Returns whether the method has parameters.
- bool has_params () const { return !function_params.empty (); }
-
- // Returns whether the method has a return type (void otherwise).
- bool has_return_type () const { return return_type != nullptr; }
-
- // Returns whether the where clause exists (i.e. has items)
- bool has_where_clause () const { return !where_clause.is_empty (); }
-
- // Returns whether method has a non-default visibility.
- bool has_visibility () const { return !vis.is_error (); }
-
- // Mega-constructor with all possible fields
- Method (Identifier method_name, FunctionQualifiers qualifiers,
- std::vector<std::unique_ptr<GenericParam>> generic_params,
- SelfParam self_param, std::vector<FunctionParam> function_params,
- std::unique_ptr<Type> return_type, WhereClause where_clause,
- std::unique_ptr<BlockExpr> function_body, Visibility vis,
- std::vector<Attribute> outer_attrs, location_t locus,
- bool is_default = false)
- : outer_attrs (std::move (outer_attrs)), vis (std::move (vis)),
- qualifiers (std::move (qualifiers)),
- method_name (std::move (method_name)),
- generic_params (std::move (generic_params)),
- self_param (std::move (self_param)),
- function_params (std::move (function_params)),
- return_type (std::move (return_type)),
- where_clause (std::move (where_clause)),
- function_body (std::move (function_body)), locus (locus),
- node_id (Analysis::Mappings::get ()->get_next_node_id ()),
- is_default (is_default)
- {}
-
- // TODO: add constructor with less fields
-
- // Copy constructor with clone
- Method (Method const &other)
- : outer_attrs (other.outer_attrs), vis (other.vis),
- qualifiers (other.qualifiers), method_name (other.method_name),
- self_param (other.self_param), function_params (other.function_params),
- where_clause (other.where_clause), locus (other.locus),
- is_default (other.is_default)
- {
- // guard to prevent null dereference (always required)
- if (other.return_type != nullptr)
- return_type = other.return_type->clone_type ();
-
- // guard to prevent null dereference (only required if error state)
- if (other.function_body != nullptr)
- function_body = other.function_body->clone_block_expr ();
-
- generic_params.reserve (other.generic_params.size ());
- for (const auto &e : other.generic_params)
- generic_params.push_back (e->clone_generic_param ());
-
- node_id = other.node_id;
- }
-
- // Overloaded assignment operator to clone
- Method &operator= (Method const &other)
- {
- method_name = other.method_name;
- outer_attrs = other.outer_attrs;
- vis = other.vis;
- qualifiers = other.qualifiers;
- self_param = other.self_param;
- function_params = other.function_params;
- where_clause = other.where_clause;
- locus = other.locus;
- is_default = other.is_default;
-
- // guard to prevent null dereference (always required)
- if (other.return_type != nullptr)
- return_type = other.return_type->clone_type ();
- else
- return_type = nullptr;
-
- // guard to prevent null dereference (only required if error state)
- if (other.function_body != nullptr)
- function_body = other.function_body->clone_block_expr ();
- else
- function_body = nullptr;
-
- generic_params.reserve (other.generic_params.size ());
- for (const auto &e : other.generic_params)
- generic_params.push_back (e->clone_generic_param ());
-
- node_id = other.node_id;
-
- return *this;
- }
-
- // move constructors
- Method (Method &&other) = default;
- Method &operator= (Method &&other) = default;
-
- std::string as_string () const override;
-
- void accept_vis (ASTVisitor &vis) override;
-
- // Invalid if block is null, so base stripping on that.
- void mark_for_strip () override { function_body = nullptr; }
- bool is_marked_for_strip () const override
- {
- return function_body == nullptr;
- }
-
- // 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; }
-
- std::vector<FunctionParam> &get_function_params () { return function_params; }
- const std::vector<FunctionParam> &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<BlockExpr> &get_definition ()
- {
- rust_assert (function_body != nullptr);
- return function_body;
- }
-
- SelfParam &get_self_param () { return self_param; }
- const SelfParam &get_self_param () const { return self_param; }
-
- // TODO: is this better? Or is a "vis_block" better?
- std::unique_ptr<Type> &get_return_type ()
- {
- rust_assert (has_return_type ());
- return return_type;
- }
-
- // TODO: is this better? Or is a "vis_block" better?
- WhereClause &get_where_clause () { return where_clause; }
-
- Identifier get_method_name () const { return method_name; }
-
- NodeId get_node_id () const { return node_id; }
-
- location_t get_locus () const override final { return locus; }
-
- FunctionQualifiers get_qualifiers () const { return qualifiers; }
-
- FunctionQualifiers &get_qualifiers () { return qualifiers; }
-
- Visibility &get_visibility () { return vis; }
- const Visibility &get_visibility () const { return vis; }
-
-protected:
- /* Use covariance to implement clone function as returning this object
- * rather than base */
- Method *clone_associated_item_impl () const final override
- {
- return clone_method_impl ();
- }
-
- /*virtual*/ Method *clone_method_impl () const { return new Method (*this); }
-};
-
// Item that supports visibility - abstract base class
class VisItem : public Item
{
@@ -1580,6 +1372,7 @@ class Function : public VisItem, public InherentImplItem, public TraitImplItem
FunctionQualifiers qualifiers;
Identifier function_name;
std::vector<std::unique_ptr<GenericParam>> generic_params;
+ tl::optional<SelfParam> self_param;
std::vector<FunctionParam> function_params;
std::unique_ptr<Type> return_type;
WhereClause where_clause;
@@ -1602,9 +1395,12 @@ public:
// Returns whether function has a where clause.
bool has_where_clause () const { return !where_clause.is_empty (); }
+ bool has_self_param () const { return self_param.has_value (); }
+
// Mega-constructor with all possible fields
Function (Identifier function_name, FunctionQualifiers qualifiers,
std::vector<std::unique_ptr<GenericParam>> generic_params,
+ tl::optional<SelfParam> self_param,
std::vector<FunctionParam> function_params,
std::unique_ptr<Type> return_type, WhereClause where_clause,
std::unique_ptr<BlockExpr> function_body, Visibility vis,
@@ -1614,6 +1410,7 @@ public:
qualifiers (std::move (qualifiers)),
function_name (std::move (function_name)),
generic_params (std::move (generic_params)),
+ self_param (std::move (self_param)),
function_params (std::move (function_params)),
return_type (std::move (return_type)),
where_clause (std::move (where_clause)),
@@ -1626,7 +1423,7 @@ public:
// Copy constructor with clone
Function (Function const &other)
: VisItem (other), qualifiers (other.qualifiers),
- function_name (other.function_name),
+ function_name (other.function_name), self_param (other.self_param),
function_params (other.function_params),
where_clause (other.where_clause), locus (other.locus),
is_default (other.is_default)
@@ -1650,6 +1447,7 @@ public:
VisItem::operator= (other);
function_name = other.function_name;
qualifiers = other.qualifiers;
+ self_param = other.self_param;
function_params = other.function_params;
where_clause = other.where_clause;
// visibility = other.visibility->clone_visibility();
@@ -1735,6 +1533,17 @@ public:
return return_type;
}
+ SelfParam &get_self_param ()
+ {
+ rust_assert (has_self_param ());
+ return self_param.value ();
+ }
+ const SelfParam &get_self_param () const
+ {
+ rust_assert (has_self_param ());
+ return self_param.value ();
+ }
+
protected:
/* Use covariance to implement clone function as returning this object
* rather than base */