Skip to content

Commit

Permalink
WIP DO NOT MERGE THIS IS WORK IN PROGRESS
Browse files Browse the repository at this point in the history
Do not merge, prototype.

gcc/rust/ChangeLog:

	* ast/rust-ast.cc (ExternalFunctionItem::as_string):
	(NamedFunctionParam::as_string):
	* ast/rust-item.h (class ExternalFunctionItem):
	* expand/rust-cfg-strip.cc (CfgStrip::visit):
	* hir/rust-ast-lower-extern.h:
	* hir/tree/rust-hir-item.h:
	* metadata/rust-export-metadata.cc (ExportContext::emit_function):
	* parse/rust-parse-impl.h (Parser::parse_named_function_param):
	(Parser::parse_external_item):
	* parse/rust-parse.h:

Signed-off-by: Pierre-Emmanuel Patry <[email protected]>
  • Loading branch information
P-E-P committed Oct 20, 2023
1 parent bf6c4ec commit b6f3c9d
Show file tree
Hide file tree
Showing 7 changed files with 210 additions and 206 deletions.
25 changes: 7 additions & 18 deletions gcc/rust/ast/rust-ast.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2862,29 +2862,14 @@ ExternalFunctionItem::as_string () const

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

if (has_variadics)
{
str += "\n variadic outer attrs: ";
if (has_variadic_outer_attrs ())
{
for (const auto &attr : variadic_outer_attrs)
str += "\n " + attr.as_string ();
}
else
{
str += "none";
}
str += "\n ... (variadic)";
}
}

// add type on new line
Expand All @@ -2906,9 +2891,13 @@ NamedFunctionParam::as_string () const
{
std::string str = append_attributes (outer_attrs, OUTER);

str += "\n" + name;
if (has_name ())
str += "\n" + name;

str += "\n Type: " + param_type->as_string ();
if (is_variadic ())
str += "\n Type: " + param_type->as_string ();
else
str += "...";

return str;
}
Expand Down
45 changes: 22 additions & 23 deletions gcc/rust/ast/rust-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -4166,7 +4166,7 @@ class NamedFunctionParam
public:
/* Returns whether the named function parameter has a name (i.e. name is not
* '_'). */
bool has_name () const { return name != "_"; }
bool has_name () const { return name != "_" && name != ""; }

bool has_outer_attrs () const { return !outer_attrs.empty (); }

Expand All @@ -4177,6 +4177,8 @@ class NamedFunctionParam
return param_type == nullptr && !variadic;
}

bool is_variadic () const { return variadic; }

std::string get_name () const { return name; }

location_t get_locus () { return locus; }
Expand All @@ -4190,15 +4192,24 @@ class NamedFunctionParam
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)), variadic (false),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
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 (std::move (param_type)),
outer_attrs (std::move (outer_attrs)), variadic (true),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus)
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 (std::move (param_type)),
outer_attrs (std::move (outer_attrs)),
node_id (Analysis::Mappings::get ()->get_next_node_id ()), locus (locus),
variadic (true)
{}

// Copy constructor
Expand Down Expand Up @@ -4279,8 +4290,6 @@ class ExternalFunctionItem : public ExternalItem
WhereClause where_clause;

std::vector<NamedFunctionParam> function_params;
bool has_variadics;
std::vector<Attribute> variadic_outer_attrs;

public:
// Returns whether item has generic parameters.
Expand All @@ -4299,12 +4308,10 @@ class ExternalFunctionItem : public ExternalItem
bool has_visibility () const { return !visibility.is_error (); }

// Returns whether item has variadic parameters.
bool is_variadic () const { return has_variadics; }

// Returns whether item has outer attributes on its variadic parameters.
bool has_variadic_outer_attrs () const
bool is_variadic () const
{
return !variadic_outer_attrs.empty ();
return function_params.size () != 0
&& function_params.back ().is_variadic ();
}

location_t get_locus () const { return locus; }
Expand All @@ -4316,17 +4323,14 @@ class ExternalFunctionItem : public ExternalItem
Identifier item_name,
std::vector<std::unique_ptr<GenericParam>> generic_params,
std::unique_ptr<Type> return_type, WhereClause where_clause,
std::vector<NamedFunctionParam> function_params, bool has_variadics,
std::vector<Attribute> variadic_outer_attrs, Visibility vis,
std::vector<NamedFunctionParam> function_params, Visibility vis,
std::vector<Attribute> outer_attrs, location_t locus)
: ExternalItem (), outer_attrs (std::move (outer_attrs)),
visibility (std::move (vis)), item_name (std::move (item_name)),
locus (locus), generic_params (std::move (generic_params)),
return_type (std::move (return_type)),
where_clause (std::move (where_clause)),
function_params (std::move (function_params)),
has_variadics (has_variadics),
variadic_outer_attrs (std::move (variadic_outer_attrs))
function_params (std::move (function_params))
{
// TODO: assert that if has variadic outer attrs, then has_variadics is
// true?
Expand All @@ -4336,10 +4340,7 @@ class ExternalFunctionItem : public ExternalItem
ExternalFunctionItem (ExternalFunctionItem const &other)
: outer_attrs (other.outer_attrs), visibility (other.visibility),
item_name (other.item_name), locus (other.locus),
where_clause (other.where_clause),
function_params (other.function_params),
has_variadics (other.has_variadics),
variadic_outer_attrs (other.variadic_outer_attrs)
where_clause (other.where_clause), function_params (other.function_params)
{
node_id = other.node_id;
// guard to prevent null pointer dereference
Expand All @@ -4360,8 +4361,6 @@ class ExternalFunctionItem : public ExternalItem
locus = other.locus;
where_clause = other.where_clause;
function_params = other.function_params;
has_variadics = other.has_variadics;
variadic_outer_attrs = other.variadic_outer_attrs;
node_id = other.node_id;

// guard to prevent null pointer dereference
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/expand/rust-cfg-strip.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2588,7 +2588,8 @@ CfgStrip::visit (AST::ExternalFunctionItem &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 end = item.is_variadic () ? params.end () - 1 : params.end ();
for (auto it = params.begin (); it != end;)
{
auto &param = *it;

Expand Down
7 changes: 6 additions & 1 deletion gcc/rust/hir/rust-ast-lower-extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,11 @@ class ASTLoweringExternItem : public ASTLoweringBase
? ASTLoweringType::translate (function.get_return_type ().get ())
: nullptr;

bool is_variadic = function.is_variadic ();
if (is_variadic)
{
function.get_function_params ().pop_back ();
}
std::vector<HIR::NamedFunctionParam> function_params;
for (auto &param : function.get_function_params ())
{
Expand All @@ -105,7 +110,7 @@ class ASTLoweringExternItem : public ASTLoweringBase
translated = new HIR::ExternalFunctionItem (
mapping, function.get_identifier (), std::move (generic_params),
std::unique_ptr<HIR::Type> (return_type), std::move (where_clause),
std::move (function_params), function.is_variadic (), std::move (vis),
std::move (function_params), is_variadic, std::move (vis),
function.get_outer_attrs (), function.get_locus ());
}

Expand Down
11 changes: 6 additions & 5 deletions gcc/rust/metadata/rust-export-metadata.cc
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,12 @@ ExportContext::emit_function (const HIR::Function &fn)
function_params.push_back (std::move (p));
}

AST::ExternalItem *external_item = new AST::ExternalFunctionItem (
item_name, {} /* generic_params */, std::move (return_type),
where_clause, std::move (function_params), false /* has_variadics */,
{} /* variadic_outer_attrs */, vis, function.get_outer_attrs (),
function.get_locus ());
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 (
Expand Down
Loading

0 comments on commit b6f3c9d

Please sign in to comment.