Skip to content

Commit bede822

Browse files
committed
Refactor mapping any lang items to be done during HIR lowering
This extracts a common way of handling outer attributes on Items to improve error handling and make lang item mappings more generic.
1 parent 87aeea2 commit bede822

File tree

4 files changed

+93
-25
lines changed

4 files changed

+93
-25
lines changed

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ class ASTLoweringBase : public AST::ASTVisitor
281281

282282
HIR::FunctionQualifiers
283283
lower_qualifiers (const AST::FunctionQualifiers &qualifiers);
284+
285+
void handle_outer_attributes (const HIR::Item &item);
286+
287+
void handle_lang_item_attribute (const HIR::Item &item,
288+
const AST::Attribute &attr);
289+
290+
static bool is_known_attribute (const std::string &attribute_path);
291+
292+
static bool
293+
attribute_handled_in_another_pass (const std::string &attribute_path);
284294
};
285295

286296
} // namespace HIR

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ class ASTLoweringItem : public ASTLoweringBase
4444
{
4545
ASTLoweringItem resolver;
4646
item->accept_vis (resolver);
47+
48+
if (resolver.translated != nullptr)
49+
resolver.handle_outer_attributes (*resolver.translated);
50+
4751
return resolver.translated;
4852
}
4953

gcc/rust/hir/rust-ast-lower.cc

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -609,5 +609,84 @@ ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers)
609609
has_extern, extern_abi);
610610
}
611611

612+
void
613+
ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
614+
{
615+
for (const auto &attr : item.get_outer_attrs ())
616+
{
617+
const auto &str_path = attr.get_path ().as_string ();
618+
if (!is_known_attribute (str_path))
619+
{
620+
rust_error_at (attr.get_locus (), "unknown attribute");
621+
continue;
622+
}
623+
624+
bool is_lang_item = str_path.compare ("lang") == 0
625+
&& attr.has_attr_input ()
626+
&& attr.get_attr_input ().get_attr_input_type ()
627+
== AST::AttrInput::AttrInputType::LITERAL;
628+
629+
if (is_lang_item)
630+
handle_lang_item_attribute (item, attr);
631+
else if (!attribute_handled_in_another_pass (str_path))
632+
{
633+
rust_error_at (attr.get_locus (), "unhandled attribute: [%s]",
634+
attr.get_path ().as_string ().c_str ());
635+
}
636+
}
637+
}
638+
639+
void
640+
ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item,
641+
const AST::Attribute &attr)
642+
{
643+
auto &literal = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
644+
const auto &lang_item_type_str = literal.get_literal ().as_string ();
645+
auto lang_item_type = Analysis::RustLangItem::Parse (lang_item_type_str);
646+
if (lang_item_type == Analysis::RustLangItem::ItemType::UNKNOWN)
647+
{
648+
rust_error_at (attr.get_locus (), "unknown lang item");
649+
return;
650+
}
651+
mappings->insert_lang_item (lang_item_type,
652+
item.get_mappings ().get_defid ());
653+
}
654+
655+
bool
656+
ASTLoweringBase::is_known_attribute (const std::string &attribute_path)
657+
{
658+
if (attribute_path.compare ("inline") == 0)
659+
return true;
660+
else if (attribute_path.compare ("cfg") == 0)
661+
return true;
662+
else if (attribute_path.compare ("cfg_attr") == 0)
663+
return true;
664+
else if (attribute_path.compare ("allow") == 0)
665+
return true;
666+
else if (attribute_path.compare ("lang") == 0)
667+
return true;
668+
669+
return false;
670+
}
671+
672+
bool
673+
ASTLoweringBase::attribute_handled_in_another_pass (
674+
const std::string &attribute_path)
675+
{
676+
// handled during code-generation
677+
if (attribute_path.compare ("inline") == 0)
678+
return true;
679+
680+
// handled during previous expansion pass
681+
else if (attribute_path.compare ("cfg") == 0)
682+
return true;
683+
else if (attribute_path.compare ("cfg_attr") == 0)
684+
return true;
685+
else if (attribute_path.compare ("allow") == 0)
686+
return true;
687+
688+
return false;
689+
}
690+
612691
} // namespace HIR
613692
} // namespace Rust

gcc/rust/typecheck/rust-hir-trait-resolve.h

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -207,31 +207,6 @@ class TraitResolver : public TypeCheckBase
207207
// loop of trying to resolve traits as required by the types
208208
tref->on_resolved ();
209209

210-
// does this have any lang-item attributes?
211-
for (auto &attr : trait_reference->get_outer_attrs ())
212-
{
213-
bool is_lang_item = attr.get_path ().as_string ().compare ("lang") == 0
214-
&& attr.has_attr_input ()
215-
&& attr.get_attr_input ().get_attr_input_type ()
216-
== AST::AttrInput::AttrInputType::LITERAL;
217-
if (is_lang_item)
218-
{
219-
auto &literal
220-
= static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
221-
const auto &lang_item_type_str
222-
= literal.get_literal ().as_string ();
223-
auto lang_item_type
224-
= Analysis::RustLangItem::Parse (lang_item_type_str);
225-
if (lang_item_type == Analysis::RustLangItem::ItemType::UNKNOWN)
226-
{
227-
rust_error_at (attr.get_locus (), "unknown lang item");
228-
return tref;
229-
}
230-
mappings->insert_lang_item (
231-
lang_item_type, trait_reference->get_mappings ().get_defid ());
232-
}
233-
}
234-
235210
return tref;
236211
}
237212

0 commit comments

Comments
 (0)