Skip to content

Commit 4a5e864

Browse files
committed
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (get_attributes): "removed checker function" (get_trait_name): "removed checker function" * util/rust-attributes.cc (Attributes::get_attributes): "added checker function" (Attributes::get_trait_name): "added checker function" * util/rust-attributes.h: "added functions definitions in the header file for access" Signed-off-by: Om Swaroop Nayak <96killerat96@gmail.com>
1 parent d28cae5 commit 4a5e864

File tree

3 files changed

+63
-61
lines changed

3 files changed

+63
-61
lines changed

gcc/rust/backend/rust-compile-base.cc

+2-61
Original file line numberDiff line numberDiff line change
@@ -158,74 +158,15 @@ HIRCompileBase::handle_attribute_proc_macro_attribute_on_fndecl (
158158
ctx->collect_attribute_proc_macro (fndecl);
159159
}
160160

161-
static std::vector<std::string>
162-
get_attributes (const AST::Attribute &attr)
163-
{
164-
std::vector<std::string> result;
165-
166-
rust_assert (attr.get_attr_input ().get_attr_input_type ()
167-
== Rust::AST::AttrInput::TOKEN_TREE);
168-
const auto &tt
169-
= static_cast<const AST::DelimTokenTree &> (attr.get_attr_input ());
170-
171-
// TODO: Should we rely on fixed index ? Should we search for the
172-
// attribute tokentree instead ?
173-
174-
// Derive proc macros have the following format:
175-
// #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))]
176-
// -~~~~~~~~ - ~~~~~~~~~~---------------------
177-
// ^0 ^1 ^2 ^3 ^4
178-
// - "attributes" is stored at position 3 in the token tree
179-
// - attribute are stored in the delimited token tree in position 4
180-
constexpr size_t attr_kw_pos = 3;
181-
constexpr size_t attribute_list_pos = 4;
182-
183-
if (tt.get_token_trees ().size () > attr_kw_pos)
184-
{
185-
rust_assert (tt.get_token_trees ()[attr_kw_pos]->as_string ()
186-
== "attributes");
187-
188-
auto attributes = static_cast<const AST::DelimTokenTree *> (
189-
tt.get_token_trees ()[attribute_list_pos].get ());
190-
191-
auto &token_trees = attributes->get_token_trees ();
192-
193-
for (auto i = token_trees.cbegin () + 1; // Skip opening parenthesis
194-
i < token_trees.cend ();
195-
i += 2) // Skip comma and closing parenthesis
196-
{
197-
result.push_back ((*i)->as_string ());
198-
}
199-
}
200-
return result;
201-
}
202-
203-
static std::string
204-
get_trait_name (const AST::Attribute &attr)
205-
{
206-
// Derive proc macros have the following format:
207-
// #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))]
208-
// -~~~~~~~~ - ~~~~~~~~~~---------------------
209-
// ^0 ^1 ^2 ^3 ^4
210-
// - The trait name is stored at position 1
211-
constexpr size_t trait_name_pos = 1;
212-
213-
rust_assert (attr.get_attr_input ().get_attr_input_type ()
214-
== Rust::AST::AttrInput::TOKEN_TREE);
215-
const auto &tt
216-
= static_cast<const AST::DelimTokenTree &> (attr.get_attr_input ());
217-
return tt.get_token_trees ()[trait_name_pos]->as_string ();
218-
}
219-
220161
void
221162
HIRCompileBase::handle_derive_proc_macro_attribute_on_fndecl (
222163
tree fndecl, const AST::Attribute &attr)
223164
{
224165
handle_proc_macro_common (fndecl, attr);
225166

226167
attr.get_attr_input ().parse_to_meta_item ();
227-
CustomDeriveInfo macro
228-
= {fndecl, get_trait_name (attr), get_attributes (attr)};
168+
CustomDeriveInfo macro = {fndecl, Analysis::Attributes::get_trait_name (attr),
169+
Analysis::Attributes::get_attributes (attr)};
229170
ctx->collect_derive_proc_macro (macro);
230171
}
231172

gcc/rust/util/rust-attributes.cc

+59
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,65 @@ Attributes::is_known (const std::string &attribute_path)
3838
return !lookup.is_error ();
3939
}
4040

41+
std::vector<std::string>
42+
Attributes::get_attributes (const AST::Attribute &attr)
43+
{
44+
std::vector<std::string> result;
45+
46+
rust_assert (attr.get_attr_input ().get_attr_input_type ()
47+
== Rust::AST::AttrInput::TOKEN_TREE);
48+
const auto &tt
49+
= static_cast<const AST::DelimTokenTree &> (attr.get_attr_input ());
50+
51+
// TODO: Should we rely on fixed index ? Should we search for the
52+
// attribute tokentree instead ?
53+
54+
// Derive proc macros have the following format:
55+
// #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))]
56+
// -~~~~~~~~ - ~~~~~~~~~~---------------------
57+
// ^0 ^1 ^2 ^3 ^4
58+
// - "attributes" is stored at position 3 in the token tree
59+
// - attribute are stored in the delimited token tree in position 4
60+
constexpr size_t attr_kw_pos = 3;
61+
constexpr size_t attribute_list_pos = 4;
62+
63+
if (tt.get_token_trees ().size () > attr_kw_pos)
64+
{
65+
rust_assert (tt.get_token_trees ()[attr_kw_pos]->as_string ()
66+
== "attributes");
67+
68+
auto attributes = static_cast<const AST::DelimTokenTree *> (
69+
tt.get_token_trees ()[attribute_list_pos].get ());
70+
71+
auto &token_trees = attributes->get_token_trees ();
72+
73+
for (auto i = token_trees.cbegin () + 1; // Skip opening parenthesis
74+
i < token_trees.cend ();
75+
i += 2) // Skip comma and closing parenthesis
76+
{
77+
result.push_back ((*i)->as_string ());
78+
}
79+
}
80+
return result;
81+
}
82+
83+
std::string
84+
Attributes::get_trait_name (const AST::Attribute &attr)
85+
{
86+
// Derive proc macros have the following format:
87+
// #[proc_macro_derive(TraitName, attributes(attr1, attr2, attr3))]
88+
// -~~~~~~~~ - ~~~~~~~~~~---------------------
89+
// ^0 ^1 ^2 ^3 ^4
90+
// - The trait name is stored at position 1
91+
constexpr size_t trait_name_pos = 1;
92+
93+
rust_assert (attr.get_attr_input ().get_attr_input_type ()
94+
== Rust::AST::AttrInput::TOKEN_TREE);
95+
const auto &tt
96+
= static_cast<const AST::DelimTokenTree &> (attr.get_attr_input ());
97+
return tt.get_token_trees ()[trait_name_pos]->as_string ();
98+
}
99+
41100
using Attrs = Values::Attributes;
42101

43102
// https://doc.rust-lang.org/stable/nightly-rustc/src/rustc_feature/builtin_attrs.rs.html#248

gcc/rust/util/rust-attributes.h

+2
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ class Attributes
2929
{
3030
public:
3131
static bool is_known (const std::string &attribute_path);
32+
static std::vector<std::string> get_attributes (const AST::Attribute &attr);
33+
static std::string get_trait_name (const AST::Attribute &attr);
3234
};
3335

3436
enum CompilerPass

0 commit comments

Comments
 (0)