Skip to content

Commit

Permalink
Update resolver to use AST::Function instead of `AST::ExternalFunct…
Browse files Browse the repository at this point in the history
…ionItem`

gcc/rust/ChangeLog:

	* checks/errors/rust-feature-gate.cc (FeatureGate::visit):
	Check if function is_external or not.
	* hir/rust-ast-lower-extern.h: Use AST::Function
	instead of AST::ExternalFunctionItem.
	* parse/rust-parse-impl.h (Parser::parse_external_item):
	Likewise.
	(Parser::parse_pattern): Fix clang format.
	* resolve/rust-ast-resolve-implitem.h: Likewise.
	* resolve/rust-ast-resolve-item.cc (ResolveExternItem::visit):
	Likewise.
	* resolve/rust-ast-resolve-item.h: Likewise.
	* resolve/rust-default-resolver.cc (DefaultResolver::visit):
	Check if param has_pattern before using get_pattern.

Signed-off-by: 0xn4utilus <[email protected]>
  • Loading branch information
0xn4utilus authored and P-E-P committed Mar 5, 2024
1 parent 0177e3c commit 87f797f
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 19 deletions.
3 changes: 2 additions & 1 deletion gcc/rust/checks/errors/rust-feature-gate.cc
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,8 @@ FeatureGate::visit (AST::MacroRulesDefinition &rules_def)
void
FeatureGate::visit (AST::Function &function)
{
check_rustc_attri (function.get_outer_attrs ());
if (!function.is_external ())
check_rustc_attri (function.get_outer_attrs ());
}

void
Expand Down
23 changes: 18 additions & 5 deletions gcc/rust/hir/rust-ast-lower-extern.h
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class ASTLoweringExternItem : public ASTLoweringBase
item.get_outer_attrs (), item.get_locus ());
}

void visit (AST::ExternalFunctionItem &function) override
void visit (AST::Function &function) override
{
std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
HIR::WhereClause where_clause (std::move (where_clause_items));
Expand All @@ -88,12 +88,25 @@ class ASTLoweringExternItem : public ASTLoweringBase
std::vector<HIR::NamedFunctionParam> function_params;
for (auto it = begin; it != end; it++)
{
auto param = static_cast<AST::FunctionParam *> (it->get ());

if (param->is_variadic () || param->is_self ())
continue;
auto param_kind = param->get_pattern ()->get_pattern_kind ();

rust_assert (param_kind == AST::Pattern::Kind::Identifier
|| param_kind == AST::Pattern::Kind::Wildcard);
auto param_ident = static_cast<AST::IdentifierPattern *> (
param->get_pattern ().get ());
Identifier param_name = param_kind == AST::Pattern::Kind::Identifier
? param_ident->get_ident ()
: std::string ("_");

HIR::Type *param_type
= ASTLoweringType::translate (it->get_type ().get ());
Identifier param_name = it->get_name ();
= ASTLoweringType::translate (param->get_type ().get ());

auto crate_num = mappings->get_current_crate ();
Analysis::NodeMapping mapping (crate_num, it->get_node_id (),
Analysis::NodeMapping mapping (crate_num, param->get_node_id (),
mappings->get_next_hir_id (crate_num),
mappings->get_next_localdef_id (
crate_num));
Expand All @@ -109,7 +122,7 @@ class ASTLoweringExternItem : public ASTLoweringBase
mappings->get_next_localdef_id (crate_num));

translated = new HIR::ExternalFunctionItem (
mapping, function.get_identifier (), std::move (generic_params),
mapping, function.get_function_name (), std::move (generic_params),
std::unique_ptr<HIR::Type> (return_type), std::move (where_clause),
std::move (function_params), is_variadic, std::move (vis),
function.get_outer_attrs (), function.get_locus ());
Expand Down
7 changes: 4 additions & 3 deletions gcc/rust/parse/rust-parse-impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -6165,8 +6165,7 @@ Parser<ManagedTokenSource>::parse_external_item ()
std::move (outer_attrs), locus));
}
case FN_KW:
return parse_external_function_item (std::move (vis),
std::move (outer_attrs));
return parse_function (std::move (vis), std::move (outer_attrs), true);

case TYPE:
return parse_external_type_item (std::move (vis),
Expand Down Expand Up @@ -10476,7 +10475,9 @@ Parser<ManagedTokenSource>::parse_pattern ()
{
lexer.skip_token ();
alts.push_back (parse_pattern_no_alt ());
} while (lexer.peek_token ()->get_id () == PIPE);
}

while (lexer.peek_token ()->get_id () == PIPE);

/* alternates */
return std::unique_ptr<AST::Pattern> (
Expand Down
4 changes: 2 additions & 2 deletions gcc/rust/resolve/rust-ast-resolve-implitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ class ResolveToplevelExternItem : public ResolverBase
item->accept_vis (resolver);
};

void visit (AST::ExternalFunctionItem &function) override
void visit (AST::Function &function) override
{
auto decl
= CanonicalPath::new_seg (function.get_node_id (),
function.get_identifier ().as_string ());
function.get_function_name ().as_string ());
auto path = prefix.append (decl);

resolver->get_name_scope ().insert (
Expand Down
16 changes: 10 additions & 6 deletions gcc/rust/resolve/rust-ast-resolve-item.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1009,11 +1009,12 @@ ResolveExternItem::go (AST::ExternalItem *item, const CanonicalPath &prefix,
}

void
ResolveExternItem::visit (AST::ExternalFunctionItem &function)
ResolveExternItem::visit (AST::Function &function)
{
NodeId scope_node_id = function.get_node_id ();
auto decl = CanonicalPath::new_seg (function.get_node_id (),
function.get_identifier ().as_string ());
auto decl
= CanonicalPath::new_seg (function.get_node_id (),
function.get_function_name ().as_string ());
auto path = prefix.append (decl);
auto cpath = canonical_prefix.append (decl);

Expand All @@ -1038,9 +1039,12 @@ ResolveExternItem::visit (AST::ExternalFunctionItem &function)

// we make a new scope so the names of parameters are resolved and shadowed
// correctly
for (auto &param : function.get_function_params ())
if (!param.is_variadic ())
ResolveType::go (param.get_type ().get ());
for (auto &it : function.get_function_params ())
if (!it->is_variadic ())
{
auto param = static_cast<AST::FunctionParam *> (it.get ());
ResolveType::go (param->get_type ().get ());
}

// done
resolver->get_name_scope ().pop ();
Expand Down
2 changes: 1 addition & 1 deletion gcc/rust/resolve/rust-ast-resolve-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class ResolveExternItem : public ResolverBase
static void go (AST::ExternalItem *item, const CanonicalPath &prefix,
const CanonicalPath &canonical_prefix);

void visit (AST::ExternalFunctionItem &function) override;
void visit (AST::Function &function) override;
void visit (AST::ExternalStaticItem &item) override;

private:
Expand Down
3 changes: 2 additions & 1 deletion gcc/rust/resolve/rust-default-resolver.cc
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ DefaultResolver::visit (AST::Function &function)
if (p->is_variadic ())
{
auto param = static_cast<AST::VariadicParam *> (p.get ());
param->get_pattern ()->accept_vis (*this);
if (param->has_pattern ())
param->get_pattern ()->accept_vis (*this);
}
else if (p->is_self ())
{
Expand Down

0 comments on commit 87f797f

Please sign in to comment.