Skip to content

Commit

Permalink
Remove dead code associated with AST::ExternalFunctionItem
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* ast/rust-ast-collector.cc (TokenCollector::visit):
	Remove dead code.
	* ast/rust-ast-collector.h: Likewise.
	* ast/rust-ast-full-decls.h (class ExternalFunctionItem):
	Likewise.
	* ast/rust-ast-visitor.cc (DefaultASTVisitor::visit):
	Likewise.
	* ast/rust-ast-visitor.h: Likewise.
	* ast/rust-ast.cc (ExternalFunctionItem::as_string): Likewise.
	(ExternalFunctionItem::accept_vis): Likewise.
	* checks/errors/rust-ast-validation.cc (ASTValidation::visit):
	Likewise.
	* checks/errors/rust-ast-validation.h: Likewise.
	* checks/errors/rust-feature-gate.h: Likewise.
	* expand/rust-cfg-strip.cc (CfgStrip::visit):
	Likewise.
	* expand/rust-cfg-strip.h: Likewise.
	* expand/rust-derive.h: Likewise.
	* expand/rust-expand-visitor.cc (ExpandVisitor::visit):
	Likewise.
	* expand/rust-expand-visitor.h: Likewise.
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::visit):
	Likewise.
	* hir/rust-ast-lower-base.h: Likewise.
	* metadata/rust-export-metadata.cc (ExportContext::emit_function):
	Likewise.
	* parse/rust-parse-impl.h: Likewise.
	* parse/rust-parse.h: Likewise.
	* resolve/rust-ast-resolve-base.cc (ResolverBase::visit):
	Likewise.
	* resolve/rust-ast-resolve-base.h: Likewise.
	* resolve/rust-default-resolver.cc (DefaultResolver::visit):
	Likewise.
	* resolve/rust-default-resolver.h: Likewise.
	* util/rust-attributes.cc (AttributeChecker::visit): Likewise.
	* util/rust-attributes.h: Likewise.

gcc/testsuite/ChangeLog:

	* rust/compile/extern_func_with_body.rs: New test.

Signed-off-by: 0xn4utilus <[email protected]>
  • Loading branch information
0xn4utilus authored and P-E-P committed Mar 5, 2024
1 parent 87f797f commit 53d8e04
Show file tree
Hide file tree
Showing 26 changed files with 7 additions and 335 deletions.
25 changes: 0 additions & 25 deletions gcc/rust/ast/rust-ast-collector.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2084,31 +2084,6 @@ TokenCollector::visit (ExternalStaticItem &item)
push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
}

void
TokenCollector::visit (ExternalFunctionItem &function)
{
visit_items_as_lines (function.get_outer_attrs ());
visit (function.get_visibility ());

auto id = function.get_identifier ().as_string ();

push (Rust::Token::make (FN_KW, function.get_locus ()));
push (Rust::Token::make_identifier (UNDEF_LOCATION, std::move (id)));
if (function.has_generics ())
visit (function.get_generic_params ());
push (Rust::Token::make (LEFT_PAREN, UNDEF_LOCATION));

visit_items_joined_by_separator (function.get_function_params ());

push (Rust::Token::make (RIGHT_PAREN, UNDEF_LOCATION));
if (function.has_return_type ())
{
push (Rust::Token::make (RETURN_TYPE, UNDEF_LOCATION));
visit (function.get_return_type ());
}
push (Rust::Token::make (SEMICOLON, UNDEF_LOCATION));
}

void
TokenCollector::visit (ExternBlock &block)
{
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 @@ -333,7 +333,6 @@ class TokenCollector : public ASTVisitor
void visit (TraitImpl &impl);
void visit (ExternalTypeItem &item);
void visit (ExternalStaticItem &item);
void visit (ExternalFunctionItem &item);
void visit (ExternBlock &block);

// rust-macro.h
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 @@ -203,7 +203,6 @@ class ExternalItem;
class ExternalTypeItem;
class ExternalStaticItem;
class NamedFunctionParam;
class ExternalFunctionItem;
class ExternBlock;

// rust-macro.h
Expand Down
18 changes: 0 additions & 18 deletions gcc/rust/ast/rust-ast-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1004,24 +1004,6 @@ DefaultASTVisitor::visit (AST::NamedFunctionParam &param)
visit (param.get_type ());
}

void
DefaultASTVisitor::visit (AST::ExternalFunctionItem &item)
{
visit_outer_attrs (item);
visit (item.get_visibility ());
for (auto &generic : item.get_generic_params ())
visit (generic);

if (item.has_where_clause ())
visit (item.get_where_clause ());

for (auto &param : item.get_function_params ())
visit (param);

if (item.has_return_type ())
visit (item.get_return_type ());
}

void
DefaultASTVisitor::visit (AST::ExternBlock &block)
{
Expand Down
2 changes: 0 additions & 2 deletions gcc/rust/ast/rust-ast-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ class ASTVisitor
// virtual void visit(ExternalItem& item) = 0;
virtual void visit (ExternalTypeItem &type) = 0;
virtual void visit (ExternalStaticItem &item) = 0;
virtual void visit (ExternalFunctionItem &item) = 0;
virtual void visit (ExternBlock &block) = 0;

// rust-macro.h
Expand Down Expand Up @@ -338,7 +337,6 @@ class DefaultASTVisitor : public ASTVisitor
virtual void visit (AST::TraitImpl &impl) override;
virtual void visit (AST::ExternalTypeItem &item) override;
virtual void visit (AST::ExternalStaticItem &item) override;
virtual void visit (AST::ExternalFunctionItem &item) override;
virtual void visit (AST::ExternBlock &block) override;
virtual void visit (AST::MacroMatchFragment &match) override;
virtual void visit (AST::MacroMatchRepetition &match) override;
Expand Down
69 changes: 0 additions & 69 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2974,69 +2974,6 @@ ExternalStaticItem::as_string () const
return str;
}

std::string
ExternalFunctionItem::as_string () const
{
// outer attributes
std::string str = append_attributes (outer_attrs, OUTER);

// start visibility on new line and with a space
str += "\n" + visibility.as_string () + " ";

str += "fn ";

// add name
str += item_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 external function item.");
return "NULL_POINTER_MARK";
}

str += "\n " + param->as_string ();
}
}

// function params
str += "\n Function params: ";
if (function_params.empty ())
{
str += "none";
}
else
{
for (const auto &param : function_params)
str += "\n " + param.as_string ();
}

// add type on new line
str += "\n (return) Type: "
+ (has_return_type () ? return_type->as_string () : "()");

// where clause
str += "\n Where clause: ";
if (has_where_clause ())
str += where_clause.as_string ();
else
str += "none";

return str;
}

std::string
NamedFunctionParam::as_string () const
{
Expand Down Expand Up @@ -4866,12 +4803,6 @@ ExternalStaticItem::accept_vis (ASTVisitor &vis)
vis.visit (*this);
}

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

void
ExternBlock::accept_vis (ASTVisitor &vis)
{
Expand Down
19 changes: 0 additions & 19 deletions gcc/rust/checks/errors/rust-ast-validation.cc
Original file line number Diff line number Diff line change
Expand Up @@ -64,25 +64,6 @@ ASTValidation::visit (AST::ConstantItem &const_item)
AST::ContextualASTVisitor::visit (const_item);
}

void
ASTValidation::visit (AST::ExternalFunctionItem &item)
{
auto &params = item.get_function_params ();

if (params.size () == 1 && params[0].is_variadic ())
rust_error_at (
params[0].get_locus (),
"C-variadic function must be declared with at least one named argument");

for (auto it = params.begin (); it != params.end (); it++)
if (it->is_variadic () && it + 1 != params.end ())
rust_error_at (
it->get_locus (),
"%<...%> must be the last argument of a C-variadic function");

AST::ContextualASTVisitor::visit (item);
}

void
ASTValidation::visit (AST::Union &item)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/checks/errors/rust-ast-validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class ASTValidation : public AST::ContextualASTVisitor
virtual void visit (AST::ConstantItem &const_item);
virtual void visit (AST::Lifetime &lifetime);
virtual void visit (AST::LoopLabel &label);
virtual void visit (AST::ExternalFunctionItem &item);
virtual void visit (AST::Union &item);
virtual void visit (AST::Function &function);
virtual void visit (AST::Trait &trait);
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/checks/errors/rust-feature-gate.h
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ class FeatureGate : public AST::DefaultASTVisitor
void visit (AST::Trait &trait) override {}
void visit (AST::ExternalTypeItem &item) override;
void visit (AST::ExternalStaticItem &item) override {}
void visit (AST::ExternalFunctionItem &item) override {}
void visit (AST::ExternBlock &block) override;
void visit (AST::MacroMatchFragment &match) override {}
void visit (AST::MacroMatchRepetition &match) override {}
Expand Down
56 changes: 0 additions & 56 deletions gcc/rust/expand/rust-cfg-strip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2195,62 +2195,6 @@ CfgStrip::visit (AST::ExternalStaticItem &item)
rust_error_at (type->get_locus (), "cannot strip type in this position");
}

void
CfgStrip::visit (AST::ExternalFunctionItem &item)
{
// strip test based on outer attrs
expand_cfg_attrs (item.get_outer_attrs ());
if (fails_cfg_with_expand (item.get_outer_attrs ()))
{
item.mark_for_strip ();
return;
}

AST::DefaultASTVisitor::visit (item);

/* strip function parameters if required - this is specifically
* allowed by spec */
auto &params = item.get_function_params ();
for (auto it = params.begin (); it != params.end ();)
{
auto &param = *it;

auto &param_attrs = param.get_outer_attrs ();
expand_cfg_attrs (param_attrs);
if (fails_cfg_with_expand (param_attrs))
{
it = params.erase (it);
continue;
}

if (!param.is_variadic ())
{
auto &type = param.get_type ();
if (type->is_marked_for_strip ())
rust_error_at (type->get_locus (),
"cannot strip type in this position");
}

// increment if nothing else happens
++it;
}
/* NOTE: these are extern function params, which may have different
* rules and restrictions to "normal" function params. So expansion
* handled separately. */

/* TODO: assuming that variadic nature cannot be stripped. If this
* is not true, then have code here to do so. */

if (item.has_return_type ())
{
auto &return_type = item.get_return_type ();

if (return_type->is_marked_for_strip ())
rust_error_at (return_type->get_locus (),
"cannot strip type in this position");
}
}

void
CfgStrip::visit (AST::ExternBlock &block)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/expand/rust-cfg-strip.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,6 @@ class CfgStrip : public AST::DefaultASTVisitor
void visit (AST::TraitImpl &impl) override;
void visit (AST::ExternalTypeItem &item) override;
void visit (AST::ExternalStaticItem &item) override;
void visit (AST::ExternalFunctionItem &item) override;
void visit (AST::ExternBlock &block) override;

// I don't think it would be possible to strip macros without expansion
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/expand/rust-derive.h
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,6 @@ class DeriveVisitor : public AST::ASTVisitor
virtual void visit (TraitImpl &impl) override final{};
virtual void visit (ExternalTypeItem &type) override final{};
virtual void visit (ExternalStaticItem &item) override final{};
virtual void visit (ExternalFunctionItem &item) override final{};
virtual void visit (ExternBlock &block) override final{};
virtual void visit (MacroMatchFragment &match) override final{};
virtual void visit (MacroMatchRepetition &match) override final{};
Expand Down
17 changes: 0 additions & 17 deletions gcc/rust/expand/rust-expand-visitor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -922,23 +922,6 @@ ExpandVisitor::visit (AST::ExternalStaticItem &static_item)
maybe_expand_type (static_item.get_type ());
}

void
ExpandVisitor::visit (AST::ExternalFunctionItem &item)
{
for (auto &param : item.get_generic_params ())
visit (param);

for (auto &param : item.get_function_params ())
if (!param.is_variadic ())
maybe_expand_type (param.get_type ());

if (item.has_return_type ())
maybe_expand_type (item.get_return_type ());

if (item.has_where_clause ())
expand_where_clause (item.get_where_clause ());
}

void
ExpandVisitor::visit (AST::ExternBlock &block)
{
Expand Down
1 change: 0 additions & 1 deletion gcc/rust/expand/rust-expand-visitor.h
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,6 @@ class ExpandVisitor : public AST::DefaultASTVisitor
void visit (AST::TraitImpl &impl) override;
void visit (AST::ExternalTypeItem &item) override;
void visit (AST::ExternalStaticItem &item) override;
void visit (AST::ExternalFunctionItem &item) override;
void visit (AST::ExternBlock &block) override;

// I don't think it would be possible to strip macros without expansion
Expand Down
3 changes: 0 additions & 3 deletions gcc/rust/hir/rust-ast-lower-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -345,9 +345,6 @@ void
ASTLoweringBase::visit (AST::ExternalStaticItem &)
{}
void
ASTLoweringBase::visit (AST::ExternalFunctionItem &)
{}
void
ASTLoweringBase::visit (AST::ExternBlock &)
{}

Expand Down
1 change: 0 additions & 1 deletion gcc/rust/hir/rust-ast-lower-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,6 @@ class ASTLoweringBase : public AST::ASTVisitor
// virtual void visit(ExternalItem& item);
virtual void visit (AST::ExternalTypeItem &item);
virtual void visit (AST::ExternalStaticItem &item);
virtual void visit (AST::ExternalFunctionItem &item);
virtual void visit (AST::ExternBlock &block);

// rust-macro.h
Expand Down
41 changes: 2 additions & 39 deletions gcc/rust/metadata/rust-export-metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -93,46 +93,9 @@ ExportContext::emit_function (const HIR::Function &fn)
// FIXME assert that this is actually an AST::Function
AST::Function &function = static_cast<AST::Function &> (vis_item);

// we can emit an extern block with abi of "rust"
Identifier item_name = function.get_function_name ();

// always empty for extern linkage
AST::WhereClause where_clause = AST::WhereClause::create_empty ();
std::vector<std::unique_ptr<AST::GenericParam>> generic_params;

AST::Visibility vis = function.get_visibility ();
std::unique_ptr<AST::Type> return_type
= std::unique_ptr<AST::Type> (nullptr);
if (function.has_return_type ())
{
return_type = function.get_return_type ()->clone_type ();
}

std::vector<AST::NamedFunctionParam> function_params;
for (auto &p : function.get_function_params ())
{
if (p->is_variadic () || p->is_self ())
rust_unreachable ();
auto param = static_cast<AST::FunctionParam *> (p.get ());
std::string name = param->get_pattern ()->as_string ();
std::unique_ptr<AST::Type> param_type
= param->get_type ()->clone_type ();

AST::NamedFunctionParam np (name, std::move (param_type), {},
param->get_locus ());
function_params.push_back (std::move (np));
}

AST::ExternalItem *external_item
= new AST::ExternalFunctionItem (item_name, {} /* generic_params */,
std::move (return_type), where_clause,
std::move (function_params), vis,
function.get_outer_attrs (),
function.get_locus ());

std::vector<std::unique_ptr<AST::ExternalItem>> external_items;
external_items.push_back (
std::unique_ptr<AST::ExternalItem> (external_item));
external_items.push_back (std::unique_ptr<AST::ExternalItem> (
static_cast<AST::ExternalItem *> (&function)));

AST::ExternBlock extern_block (get_string_from_abi (Rust::ABI::RUST),
std::move (external_items),
Expand Down
Loading

0 comments on commit 53d8e04

Please sign in to comment.