Skip to content

Commit

Permalink
attributes: Add class for sharing methods on attributes.
Browse files Browse the repository at this point in the history
gcc/rust/ChangeLog:

	* util/rust-attributes.h (class Attributes): New.
	* util/rust-attributes.cc: Implement Attributes::is_known().
	* ast/rust-collect-lang-items.cc (is_known_attribute): Remove.
	(get_lang_item_attr): Call Attributes::is_known() instead.
	* hir/rust-ast-lower-base.cc (ASTLoweringBase::handle_outer_attributes): Likewise.
	(ASTLoweringBase::is_known_attribute): Remove.
  • Loading branch information
CohenArthur committed Dec 4, 2024
1 parent 6b7e055 commit 7208108
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 20 deletions.
13 changes: 1 addition & 12 deletions gcc/rust/ast/rust-collect-lang-items.cc
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,14 @@
namespace Rust {
namespace AST {

// FIXME: Before merging: De-duplicate with function in rust-ast-lower-base.cc
bool
is_known_attribute (const std::string &attribute_path)
{
const auto &lookup
= Analysis::BuiltinAttributeMappings::get ()->lookup_builtin (
attribute_path);

return !lookup.is_error ();
}

template <typename T>
tl::optional<LangItem::Kind>
get_lang_item_attr (const T &maybe_lang_item)
{
for (const auto &attr : maybe_lang_item.get_outer_attrs ())
{
const auto &str_path = attr.get_path ().as_string ();
if (!is_known_attribute (str_path))
if (!Analysis::Attributes::is_known (str_path))
{
rust_error_at (attr.get_locus (), "unknown attribute");
continue;
Expand Down
10 changes: 2 additions & 8 deletions gcc/rust/hir/rust-ast-lower-base.cc
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "rust-diagnostics.h"
#include "rust-item.h"
#include "rust-system.h"
#include "rust-attributes.h"

namespace Rust {
namespace HIR {
Expand Down Expand Up @@ -751,7 +752,7 @@ ASTLoweringBase::handle_outer_attributes (const ItemWrapper &item)
for (const auto &attr : item.get_outer_attrs ())
{
const auto &str_path = attr.get_path ().as_string ();
if (!is_known_attribute (str_path))
if (!Analysis::Attributes::is_known (str_path))
{
rust_error_at (attr.get_locus (), "unknown attribute");
continue;
Expand Down Expand Up @@ -814,13 +815,6 @@ ASTLoweringBase::handle_lang_item_attribute (const ItemWrapper &item,
rust_error_at (attr.get_locus (), "unknown lang item");
}

bool
ASTLoweringBase::is_known_attribute (const std::string &attribute_path) const
{
const auto &lookup = attr_mappings->lookup_builtin (attribute_path);
return !lookup.is_error ();
}

bool
ASTLoweringBase::attribute_handled_in_another_pass (
const std::string &attribute_path) const
Expand Down
9 changes: 9 additions & 0 deletions gcc/rust/util/rust-attributes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,15 @@
namespace Rust {
namespace Analysis {

bool
Attributes::is_known (const std::string &attribute_path)
{
const auto &lookup
= BuiltinAttributeMappings::get ()->lookup_builtin (attribute_path);

return !lookup.is_error ();
}

using Attrs = Values::Attributes;

// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248
Expand Down
6 changes: 6 additions & 0 deletions gcc/rust/util/rust-attributes.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@
namespace Rust {
namespace Analysis {

class Attributes
{
public:
static bool is_known (const std::string &attribute_path);
};

enum CompilerPass
{
UNKNOWN,
Expand Down

0 comments on commit 7208108

Please sign in to comment.