Skip to content

Commit

Permalink
stash partial named function removal
Browse files Browse the repository at this point in the history
  • Loading branch information
powerboat9 committed Jan 10, 2025
1 parent 6a13100 commit 106fd95
Show file tree
Hide file tree
Showing 9 changed files with 0 additions and 279 deletions.
23 changes: 0 additions & 23 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -250,29 +250,6 @@ TokenCollector::visit (Visibility &vis)
}
}

void
TokenCollector::visit (NamedFunctionParam &param)
{
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<std::unique_ptr<GenericParam>> &params)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/ast/rust-ast-collector.h
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ class TokenCollector : public ASTVisitor
void visit (TupleField &field);
void visit (StructField &field);
void visit (SimplePathSegment &segment);
void visit (NamedFunctionParam &param);
void visit (MacroRule &rule);
void visit (WhereClause &rule);
void visit (std::vector<LifetimeParam> &for_lifetimes);
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/ast/rust-ast-full-decls.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,6 @@ class TraitImpl;
class ExternalItem;
class ExternalTypeItem;
class ExternalStaticItem;
class NamedFunctionParam;
class ExternBlock;

// rust-macro.h
Expand Down
8 changes: 0 additions & 8 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1069,14 +1069,6 @@ DefaultASTVisitor::visit (AST::ExternalStaticItem &item)
visit (item.get_type ());
}

void
DefaultASTVisitor::visit (AST::NamedFunctionParam &param)
{
visit_outer_attrs (param);
if (!param.is_variadic ())
visit (param.get_type ());
}

void
DefaultASTVisitor::visit (AST::ExternBlock &block)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 &param);
virtual void visit (AST::MacroRule &rule);
virtual void visit (AST::MacroInvocData &data);
virtual void visit (AST::MacroTranscriber &transcriber);
Expand Down
16 changes: 0 additions & 16 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
125 changes: 0 additions & 125 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -3623,131 +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<Type> param_type;

// seemingly new since writing this node
std::vector<Attribute> 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<Type> param_type,
std::vector<Attribute> 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<Attribute> 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<Attribute> 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<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?
Type &get_type ()
{
rust_assert (param_type != nullptr);
return *param_type;
}

std::unique_ptr<Type> &get_type_ptr ()
{
rust_assert (param_type != nullptr);
return param_type;
}

NodeId get_node_id () const { return node_id; }
};

// An extern block AST node
class ExternBlock : public VisItem
{
Expand Down
100 changes: 0 additions & 100 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -5936,106 +5936,6 @@ Parser<ManagedTokenSource>::parse_extern_block (AST::Visibility vis,
std::move (outer_attrs), locus));
}

template <typename ManagedTokenSource>
AST::NamedFunctionParam
Parser<ManagedTokenSource>::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<AST::Type> 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 <typename ManagedTokenSource>
template <typename EndTokenPred>
std::vector<AST::NamedFunctionParam>
Parser<ManagedTokenSource>::parse_named_function_params (
EndTokenPred is_end_token)
{
std::vector<AST::NamedFunctionParam> 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<AST::NamedFunctionParam> ();
}
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 <typename ManagedTokenSource>
std::unique_ptr<AST::ExternalItem>
Expand Down
4 changes: 0 additions & 4 deletions gcc/rust/parse/rust-parse.h
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,6 @@ template <typename ManagedTokenSource> class Parser
AST::Lifetime lifetime_from_token (const_TokenPtr tok);
std::unique_ptr<AST::ExternalTypeItem>
parse_external_type_item (AST::Visibility vis, AST::AttrVec outer_attrs);
AST::NamedFunctionParam parse_named_function_param ();
template <typename EndTokenPred>
std::vector<AST::NamedFunctionParam>
parse_named_function_params (EndTokenPred is_end_token);

std::unique_ptr<AST::TypeAlias> parse_type_alias (AST::Visibility vis,
AST::AttrVec outer_attrs);
Expand Down

0 comments on commit 106fd95

Please sign in to comment.