Skip to content

Commit

Permalink
WIP rework function parameters
Browse files Browse the repository at this point in the history
Make self param and variadic param param, introduce Param class and make
function parameters param too.

gcc/rust/ChangeLog:

	* ast/rust-ast-collector.cc (TokenCollector::visit):
	* ast/rust-ast-collector.h:
	* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit):
	* ast/rust-ast-visitor.h:
	* ast/rust-ast.cc (Function::Function):
	(Function::operator=):
	(Function::as_string):
	(FunctionParam::accept_vis):
	(SelfParam::accept_vis):
	(VariadicParam::accept_vis):
	(VariadicParam::as_string):
	(TraitItemFunc::TraitItemFunc):
	(TraitItemFunc::operator=):
	(TraitFunctionDecl::as_string):
	(TraitItemMethod::TraitItemMethod):
	(TraitItemMethod::operator=):
	(TraitMethodDecl::as_string):
	* ast/rust-ast.h (struct Visibility):
	(class VisItem):
	* ast/rust-expr.h:
	* ast/rust-item.h (class Param):
	(class SelfParam):
	(class FunctionParam):
	(class VariadicParam):
	(struct Visibility):
	(class VisItem):
	(class Function):
	(class TraitFunctionDecl):
	(class TraitMethodDecl):
	* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
	* checks/errors/rust-ast-validation.h:
	* checks/errors/rust-feature-gate.h:
	* expand/rust-cfg-strip.cc (CfgStrip::maybe_strip_function_params):
	(CfgStrip::visit):
	* expand/rust-cfg-strip.h:
	* expand/rust-derive.h:
	* expand/rust-expand-visitor.cc (ExpandVisitor::expand_function_params):
	(ExpandVisitor::visit):
	* expand/rust-expand-visitor.h:
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit):
	* hir/rust-ast-lower-base.h:
	* hir/rust-ast-lower-implitem.h:
	* hir/rust-ast-lower-item.cc (ASTLoweringItem::visit):
	* metadata/rust-export-metadata.cc (ExportContext::emit_function):
	* parse/rust-parse-impl.h (Parser::parse_function):
	(Parser::parse_function_params):
	(Parser::parse_function_param):
	(Parser::parse_trait_item):
	(Parser::parse_method):
	* parse/rust-parse.h:
	* resolve/rust-ast-resolve-base.cc (ResolverBase::visit):
	* resolve/rust-ast-resolve-base.h:
	* resolve/rust-ast-resolve-item.cc (ResolveTraitItems::visit):
	(ResolveItem::visit):
	* resolve/rust-ast-resolve-stmt.h:
	* resolve/rust-default-resolver.cc (DefaultResolver::visit):
	* resolve/rust-default-resolver.h:
	* resolve/rust-early-name-resolver.cc (EarlyNameResolver::visit):
	* resolve/rust-early-name-resolver.h:
	* util/rust-attributes.cc (AttributeChecker::visit):
	* util/rust-attributes.h:
  • Loading branch information
P-E-P committed Nov 12, 2023
1 parent fbbdc0a commit 55836bc
Show file tree
Hide file tree
Showing 33 changed files with 957 additions and 564 deletions.
12 changes: 12 additions & 0 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
// along with GCC; see the file COPYING3. If not see
// <http://www.gnu.org/licenses/>.
#include "rust-ast-collector.h"
#include "rust-item.h"

namespace Rust {
namespace AST {
Expand Down Expand Up @@ -190,6 +191,17 @@ TokenCollector::visit (FunctionParam &param)
}
}

void
TokenCollector::visit (VariadicParam &param)
{
if (param.has_pattern ())
{
visit (param.get_pattern ());
push (Rust::Token::make (COLON, UNDEF_LOCATION));
}
push (Rust::Token::make (ELLIPSIS, UNDEF_LOCATION));
}

void
TokenCollector::visit (Attribute &attrib)
{
Expand Down
1 change: 1 addition & 0 deletions gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ class TokenCollector : public ASTVisitor
void visit (Literal &lit, location_t locus = UNDEF_LOCATION);

void visit (FunctionParam &param);
void visit (VariadicParam &param);
void visit (Attribute &attrib);
void visit (Visibility &vis);
void visit (std::vector<std::unique_ptr<GenericParam>> &params);
Expand Down
24 changes: 16 additions & 8 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -707,12 +707,6 @@ void
DefaultASTVisitor::visit (AST::FunctionQualifiers &qualifiers)
{}

void
DefaultASTVisitor::visit (AST::SelfParam &self)
{
visit (self.get_lifetime ());
}

void
DefaultASTVisitor::visit (AST::WhereClause &where)
{
Expand All @@ -726,8 +720,18 @@ DefaultASTVisitor::visit (AST::FunctionParam &param)
if (param.has_name ())
visit (param.get_pattern ());

if (!param.is_variadic ())
visit (param.get_type ());
visit (param.get_type ());
}

void
DefaultASTVisitor::visit (AST::SelfParam &param)
{
visit_outer_attrs (param);

if (param.has_lifetime ())
visit (param.get_lifetime ());

visit (param.get_type ());
}

void
Expand Down Expand Up @@ -1438,6 +1442,10 @@ DefaultASTVisitor::visit (AST::BareFunctionType &type)
visit (type.get_return_type ());
}

void
DefaultASTVisitor::visit (AST::VariadicParam &)
{}

void
ContextualASTVisitor::visit (AST::Crate &crate)
{
Expand Down
10 changes: 8 additions & 2 deletions gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
// full include not required - only forward decls
#include "rust-ast-full-decls.h"
#include "rust-ast.h"
#include "rust-item.h"
#include "rust-system.h"

namespace Rust {
Expand Down Expand Up @@ -129,6 +130,10 @@ class ASTVisitor

// rust-item.h
virtual void visit (TypeParam &param) = 0;
virtual void visit (SelfParam &param) = 0;
virtual void visit (FunctionParam &param) = 0;
virtual void visit (VariadicParam &param) = 0;

// virtual void visit(WhereClauseItem& item) = 0;
virtual void visit (LifetimeWhereClauseItem &item) = 0;
virtual void visit (TypeBoundWhereClauseItem &item) = 0;
Expand Down Expand Up @@ -386,6 +391,9 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::SliceType &type) override;
virtual void visit (AST::InferredType &type) override;
virtual void visit (AST::BareFunctionType &type) override;
virtual void visit (AST::SelfParam &self) override;
virtual void visit (AST::FunctionParam &param) override;
virtual void visit (AST::VariadicParam &param) override;

template <typename T> void visit (T &node);

Expand All @@ -406,9 +414,7 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::MatchArm &arm);
virtual void visit (AST::Visibility &vis);
virtual void visit (AST::FunctionQualifiers &qualifiers);
virtual void visit (AST::SelfParam &self);
virtual void visit (AST::WhereClause &where);
virtual void visit (AST::FunctionParam &param);
virtual void visit (AST::StructField &field);
virtual void visit (AST::TupleField &field);
virtual void visit (AST::TraitFunctionDecl &decl);
Expand Down
146 changes: 143 additions & 3 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1090,6 +1090,64 @@ Union::as_string () const
return str;
}

Function::Function (Function const &other)
: VisItem (other), qualifiers (other.qualifiers),
function_name (other.function_name), self_param (other.self_param),
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 ());

function_params.reserve (other.function_params.size ());
for (const auto &e : other.function_params)
function_params.push_back (e->clone_param ());
}

Function &
Function::operator= (Function const &other)
{
VisItem::operator= (other);
function_name = other.function_name;
qualifiers = other.qualifiers;
self_param = other.self_param;
where_clause = other.where_clause;
// visibility = other.visibility->clone_visibility();
// outer_attrs = other.outer_attrs;
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 ());

function_params.reserve (other.function_params.size ());
for (const auto &e : other.function_params)
function_params.push_back (e->clone_param ());

return *this;
}
std::string
Function::as_string () const
{
Expand Down Expand Up @@ -1149,7 +1207,7 @@ Function::as_string () const
str += "(";
for (; i != e; i++)
{
str += (*i).as_string ();
str += (*i)->as_string ();
if (e != i + 1)
str += ", ";
}
Expand Down Expand Up @@ -2245,6 +2303,33 @@ FunctionParam::as_string () const
return param_name->as_string () + " : " + type->as_string ();
}

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

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

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

std::string
VariadicParam::as_string () const
{
if (has_pattern ())
return get_pattern ()->as_string () + " : ...";
else
return "...";
}

std::string
FunctionQualifiers::as_string () const
{
Expand Down Expand Up @@ -3013,6 +3098,33 @@ 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
{
Expand Down Expand Up @@ -3062,7 +3174,7 @@ TraitFunctionDecl::as_string () const
if (has_params ())
{
for (const auto &param : function_params)
str += "\n " + param.as_string ();
str += "\n " + param->as_string ();
}
else
{
Expand All @@ -3084,6 +3196,34 @@ TraitFunctionDecl::as_string () const
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
{
Expand Down Expand Up @@ -3135,7 +3275,7 @@ TraitMethodDecl::as_string () const
if (has_params ())
{
for (const auto &param : function_params)
str += "\n " + param.as_string ();
str += "\n " + param->as_string ();
}
else
{
Expand Down
Loading

0 comments on commit 55836bc

Please sign in to comment.