Skip to content

Commit

Permalink
Support outer attribute handling on trait items just like normal items
Browse files Browse the repository at this point in the history
This patch adds a proxy class ItemWrapper to be a proxy allowing us to use
the same code paths for attribute handling as we have with normal items. We
need this so we can grab the fn trait associated type lang item's. Which
are being missed currently.

Addresses #195
  • Loading branch information
philberty authored and CohenArthur committed Jan 16, 2023
1 parent 9eab71f commit 059cb84
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 15 deletions.
6 changes: 3 additions & 3 deletions gcc/rust/hir/rust-ast-lower-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -824,7 +824,7 @@ ASTLoweringBase::lower_qualifiers (const AST::FunctionQualifiers &qualifiers)
}

void
ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item)
{
for (const auto &attr : item.get_outer_attrs ())
{
Expand Down Expand Up @@ -855,7 +855,7 @@ ASTLoweringBase::handle_outer_attributes (const HIR::Item &item)
}

void
ASTLoweringBase::handle_doc_item_attribute (const HIR::Item &item,
ASTLoweringBase::handle_doc_item_attribute (const ItemWrapper &item,
const AST::Attribute &attr)
{
auto simple_doc_comment = attr.has_attr_input ()
Expand All @@ -878,7 +878,7 @@ ASTLoweringBase::handle_doc_item_attribute (const HIR::Item &item,
}

void
ASTLoweringBase::handle_lang_item_attribute (const HIR::Item &item,
ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item,
const AST::Attribute &attr)
{
auto &literal = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ());
Expand Down
30 changes: 27 additions & 3 deletions gcc/rust/hir/rust-ast-lower-base.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,30 @@
namespace Rust {
namespace HIR {

// proxy class so we can do attribute checking on items and trait items
class ItemWrapper
{
public:
ItemWrapper (const HIR::Item &item)
: mappings (item.get_mappings ()), locus (item.get_locus ()),
outer_attrs (item.get_outer_attrs ())
{}

ItemWrapper (const HIR::TraitItem &item)
: mappings (item.get_mappings ()), locus (item.get_trait_locus ()),
outer_attrs (item.get_outer_attrs ())
{}

const Analysis::NodeMapping &get_mappings () const { return mappings; }
Location get_locus () const { return locus; }
const AST::AttrVec &get_outer_attrs () const { return outer_attrs; }

private:
const Analysis::NodeMapping &mappings;
Location locus;
const AST::AttrVec &outer_attrs;
};

// base class to allow derivatives to overload as needed
class ASTLoweringBase : public AST::ASTVisitor
{
Expand Down Expand Up @@ -264,12 +288,12 @@ class ASTLoweringBase : public AST::ASTVisitor
HIR::FunctionQualifiers
lower_qualifiers (const AST::FunctionQualifiers &qualifiers);

void handle_outer_attributes (const HIR::Item &item);
void handle_outer_attributes (const ItemWrapper &item);

void handle_lang_item_attribute (const HIR::Item &item,
void handle_lang_item_attribute (const ItemWrapper &item,
const AST::Attribute &attr);

void handle_doc_item_attribute (const HIR::Item &item,
void handle_doc_item_attribute (const ItemWrapper &item,
const AST::Attribute &attr);

bool is_known_attribute (const std::string &attribute_path) const;
Expand Down
14 changes: 6 additions & 8 deletions gcc/rust/hir/rust-ast-lower-implitem.h
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,14 @@ class ASTLowerTraitItem : public ASTLoweringBase

if (resolver.translated != nullptr)
{
// FIXME
auto id = resolver.translated->get_mappings ().get_hirid ();
auto defid = resolver.translated->get_mappings ().get_defid ();
auto locus = resolver.translated->get_trait_locus ();

// auto id = resolver.translated->get_mappings ().get_hirid ();
// auto defid = resolver.translated->get_mappings ().get_defid ();
// auto locus = resolver.translated->get_locus ();

// resolver.handle_outer_attributes (*resolver.translated);
resolver.handle_outer_attributes (*resolver.translated);
resolver.mappings->insert_hir_trait_item (resolver.translated);
// resolver.mappings->insert_location (id, locus);
// resolver.mappings->insert_defid_mapping (defid, resolver.item_cast);
resolver.mappings->insert_location (id, locus);
resolver.mappings->insert_defid_mapping (defid, resolver.translated);
}

return resolver.translated;
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/hir/tree/rust-hir-item.h
Original file line number Diff line number Diff line change
Expand Up @@ -2394,6 +2394,8 @@ class TraitItemFunc : public TraitItem
return outer_attrs;
}

Location get_trait_locus () const override { return get_locus (); }

protected:
// Clone function implementation as (not pure) virtual method
TraitItemFunc *clone_trait_item_impl () const override
Expand Down Expand Up @@ -2480,6 +2482,8 @@ class TraitItemConst : public TraitItem
return outer_attrs;
}

Location get_trait_locus () const override { return get_locus (); }

protected:
// Clone function implementation as (not pure) virtual method
TraitItemConst *clone_trait_item_impl () const override
Expand Down Expand Up @@ -2567,6 +2571,8 @@ class TraitItemType : public TraitItem
return outer_attrs;
}

Location get_trait_locus () const override { return get_locus (); }

protected:
// Clone function implementation as (not pure) virtual method
TraitItemType *clone_trait_item_impl () const override
Expand Down
4 changes: 3 additions & 1 deletion gcc/rust/hir/tree/rust-hir.h
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,9 @@ class TraitItem : public Node

virtual const std::string trait_identifier () const = 0;

const Analysis::NodeMapping get_mappings () const { return mappings; }
const Analysis::NodeMapping &get_mappings () const { return mappings; }

virtual Location get_trait_locus () const = 0;

virtual TraitItemKind get_item_kind () const = 0;

Expand Down
24 changes: 24 additions & 0 deletions gcc/rust/util/rust-hir-map.cc
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,7 @@ Mappings::insert_defid_mapping (DefId id, HIR::Item *item)

rust_assert (lookup_defid (id) == nullptr);
rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
rust_assert (lookup_trait_item_defid (id) == nullptr);

defIdMappings[id] = item;
insert_local_defid_mapping (crate_num, local_def_id, item);
Expand All @@ -331,6 +332,29 @@ Mappings::lookup_defid (DefId id)
return it->second;
}

void
Mappings::insert_defid_mapping (DefId id, HIR::TraitItem *item)
{
CrateNum crate_num = id.crateNum;
LocalDefId local_def_id = id.localDefId;

rust_assert (lookup_defid (id) == nullptr);
rust_assert (lookup_local_defid (crate_num, local_def_id) == nullptr);
rust_assert (lookup_trait_item_defid (id) == nullptr);

defIdTraitItemMappings[id] = item;
}

HIR::TraitItem *
Mappings::lookup_trait_item_defid (DefId id)
{
auto it = defIdTraitItemMappings.find (id);
if (it == defIdTraitItemMappings.end ())
return nullptr;

return it->second;
}

void
Mappings::insert_hir_item (HIR::Item *item)
{
Expand Down
3 changes: 3 additions & 0 deletions gcc/rust/util/rust-hir-map.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,8 @@ class Mappings

void insert_defid_mapping (DefId id, HIR::Item *item);
HIR::Item *lookup_defid (DefId id);
void insert_defid_mapping (DefId id, HIR::TraitItem *item);
HIR::TraitItem *lookup_trait_item_defid (DefId id);

void insert_local_defid_mapping (CrateNum crateNum, LocalDefId id,
HIR::Item *item);
Expand Down Expand Up @@ -307,6 +309,7 @@ class Mappings
std::map<CrateNum, AST::Crate *> ast_crate_mappings;
std::map<CrateNum, HIR::Crate *> hir_crate_mappings;
std::map<DefId, HIR::Item *> defIdMappings;
std::map<DefId, HIR::TraitItem *> defIdTraitItemMappings;
std::map<CrateNum, std::map<LocalDefId, HIR::Item *>> localDefIdMappings;

std::map<HirId, HIR::Module *> hirModuleMappings;
Expand Down

0 comments on commit 059cb84

Please sign in to comment.