Skip to content

Commit 2c1e525

Browse files
committed
gcc/rust/ChangeLog:
* backend/rust-compile-base.cc (get_attributes): (get_trait_name): * util/rust-attributes.cc (Attributes::get_attributes): (Attributes::get_trait_name): * util/rust-attributes.h: Signed-off-by: Om Swaroop Nayak <96killerat96@gmail.com>
1 parent d28cae5 commit 2c1e525

File tree

3 files changed

+62
-59
lines changed

3 files changed

+62
-59
lines changed

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

+2-59
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
#include "rust-compile-implitem.h"
3434
#include "rust-attribute-values.h"
3535
#include "rust-immutable-name-resolution-context.h"
36+
#include "rust-attributes.h"
3637

3738
#include "fold-const.h"
3839
#include "stringpool.h"
@@ -158,64 +159,6 @@ HIRCompileBase::handle_attribute_proc_macro_attribute_on_fndecl (
158159
ctx->collect_attribute_proc_macro (fndecl);
159160
}
160161

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-
}
219162

220163
void
221164
HIRCompileBase::handle_derive_proc_macro_attribute_on_fndecl (
@@ -225,7 +168,7 @@ HIRCompileBase::handle_derive_proc_macro_attribute_on_fndecl (
225168

226169
attr.get_attr_input ().parse_to_meta_item ();
227170
CustomDeriveInfo macro
228-
= {fndecl, get_trait_name (attr), get_attributes (attr)};
171+
= {fndecl, Attributes::get_trait_name (attr), Attributes::get_attributes (attr)};
229172
ctx->collect_derive_proc_macro (macro);
230173
}
231174

gcc/rust/util/rust-attributes.cc

+58
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,64 @@ Attributes::is_known (const std::string &attribute_path)
3737

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

4199
using Attrs = Values::Attributes;
42100

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)