Skip to content

Commit 87f797f

Browse files
0xn4utilusP-E-P
authored andcommitted
Update resolver to use AST::Function instead of AST::ExternalFunctionItem
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 <gyanendrabanjare8@gmail.com>
1 parent 0177e3c commit 87f797f

7 files changed

+39
-19
lines changed

gcc/rust/checks/errors/rust-feature-gate.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,8 @@ FeatureGate::visit (AST::MacroRulesDefinition &rules_def)
131131
void
132132
FeatureGate::visit (AST::Function &function)
133133
{
134-
check_rustc_attri (function.get_outer_attrs ());
134+
if (!function.is_external ())
135+
check_rustc_attri (function.get_outer_attrs ());
135136
}
136137

137138
void

gcc/rust/hir/rust-ast-lower-extern.h

+18-5
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ class ASTLoweringExternItem : public ASTLoweringBase
6565
item.get_outer_attrs (), item.get_locus ());
6666
}
6767

68-
void visit (AST::ExternalFunctionItem &function) override
68+
void visit (AST::Function &function) override
6969
{
7070
std::vector<std::unique_ptr<HIR::WhereClauseItem> > where_clause_items;
7171
HIR::WhereClause where_clause (std::move (where_clause_items));
@@ -88,12 +88,25 @@ class ASTLoweringExternItem : public ASTLoweringBase
8888
std::vector<HIR::NamedFunctionParam> function_params;
8989
for (auto it = begin; it != end; it++)
9090
{
91+
auto param = static_cast<AST::FunctionParam *> (it->get ());
92+
93+
if (param->is_variadic () || param->is_self ())
94+
continue;
95+
auto param_kind = param->get_pattern ()->get_pattern_kind ();
96+
97+
rust_assert (param_kind == AST::Pattern::Kind::Identifier
98+
|| param_kind == AST::Pattern::Kind::Wildcard);
99+
auto param_ident = static_cast<AST::IdentifierPattern *> (
100+
param->get_pattern ().get ());
101+
Identifier param_name = param_kind == AST::Pattern::Kind::Identifier
102+
? param_ident->get_ident ()
103+
: std::string ("_");
104+
91105
HIR::Type *param_type
92-
= ASTLoweringType::translate (it->get_type ().get ());
93-
Identifier param_name = it->get_name ();
106+
= ASTLoweringType::translate (param->get_type ().get ());
94107

95108
auto crate_num = mappings->get_current_crate ();
96-
Analysis::NodeMapping mapping (crate_num, it->get_node_id (),
109+
Analysis::NodeMapping mapping (crate_num, param->get_node_id (),
97110
mappings->get_next_hir_id (crate_num),
98111
mappings->get_next_localdef_id (
99112
crate_num));
@@ -109,7 +122,7 @@ class ASTLoweringExternItem : public ASTLoweringBase
109122
mappings->get_next_localdef_id (crate_num));
110123

111124
translated = new HIR::ExternalFunctionItem (
112-
mapping, function.get_identifier (), std::move (generic_params),
125+
mapping, function.get_function_name (), std::move (generic_params),
113126
std::unique_ptr<HIR::Type> (return_type), std::move (where_clause),
114127
std::move (function_params), is_variadic, std::move (vis),
115128
function.get_outer_attrs (), function.get_locus ());

gcc/rust/parse/rust-parse-impl.h

+4-3
Original file line numberDiff line numberDiff line change
@@ -6165,8 +6165,7 @@ Parser<ManagedTokenSource>::parse_external_item ()
61656165
std::move (outer_attrs), locus));
61666166
}
61676167
case FN_KW:
6168-
return parse_external_function_item (std::move (vis),
6169-
std::move (outer_attrs));
6168+
return parse_function (std::move (vis), std::move (outer_attrs), true);
61706169

61716170
case TYPE:
61726171
return parse_external_type_item (std::move (vis),
@@ -10476,7 +10475,9 @@ Parser<ManagedTokenSource>::parse_pattern ()
1047610475
{
1047710476
lexer.skip_token ();
1047810477
alts.push_back (parse_pattern_no_alt ());
10479-
} while (lexer.peek_token ()->get_id () == PIPE);
10478+
}
10479+
10480+
while (lexer.peek_token ()->get_id () == PIPE);
1048010481

1048110482
/* alternates */
1048210483
return std::unique_ptr<AST::Pattern> (

gcc/rust/resolve/rust-ast-resolve-implitem.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -189,11 +189,11 @@ class ResolveToplevelExternItem : public ResolverBase
189189
item->accept_vis (resolver);
190190
};
191191

192-
void visit (AST::ExternalFunctionItem &function) override
192+
void visit (AST::Function &function) override
193193
{
194194
auto decl
195195
= CanonicalPath::new_seg (function.get_node_id (),
196-
function.get_identifier ().as_string ());
196+
function.get_function_name ().as_string ());
197197
auto path = prefix.append (decl);
198198

199199
resolver->get_name_scope ().insert (

gcc/rust/resolve/rust-ast-resolve-item.cc

+10-6
Original file line numberDiff line numberDiff line change
@@ -1009,11 +1009,12 @@ ResolveExternItem::go (AST::ExternalItem *item, const CanonicalPath &prefix,
10091009
}
10101010

10111011
void
1012-
ResolveExternItem::visit (AST::ExternalFunctionItem &function)
1012+
ResolveExternItem::visit (AST::Function &function)
10131013
{
10141014
NodeId scope_node_id = function.get_node_id ();
1015-
auto decl = CanonicalPath::new_seg (function.get_node_id (),
1016-
function.get_identifier ().as_string ());
1015+
auto decl
1016+
= CanonicalPath::new_seg (function.get_node_id (),
1017+
function.get_function_name ().as_string ());
10171018
auto path = prefix.append (decl);
10181019
auto cpath = canonical_prefix.append (decl);
10191020

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

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

10451049
// done
10461050
resolver->get_name_scope ().pop ();

gcc/rust/resolve/rust-ast-resolve-item.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ class ResolveExternItem : public ResolverBase
111111
static void go (AST::ExternalItem *item, const CanonicalPath &prefix,
112112
const CanonicalPath &canonical_prefix);
113113

114-
void visit (AST::ExternalFunctionItem &function) override;
114+
void visit (AST::Function &function) override;
115115
void visit (AST::ExternalStaticItem &item) override;
116116

117117
private:

gcc/rust/resolve/rust-default-resolver.cc

+2-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ DefaultResolver::visit (AST::Function &function)
6262
if (p->is_variadic ())
6363
{
6464
auto param = static_cast<AST::VariadicParam *> (p.get ());
65-
param->get_pattern ()->accept_vis (*this);
65+
if (param->has_pattern ())
66+
param->get_pattern ()->accept_vis (*this);
6667
}
6768
else if (p->is_self ())
6869
{

0 commit comments

Comments
 (0)