Skip to content

Commit 4d8b1bc

Browse files
committed
expand: Allow built-in derives to generate multiple items
gcc/rust/ChangeLog: * expand/rust-derive.cc (DeriveVisitor::derive): Return a vector of items. * expand/rust-derive.h: Change return type. * expand/rust-expand-visitor.cc: Insert all generated items into the AST.
1 parent 771b0c3 commit 4d8b1bc

File tree

3 files changed

+22
-15
lines changed

3 files changed

+22
-15
lines changed

gcc/rust/expand/rust-derive.cc

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -31,34 +31,34 @@ DeriveVisitor::DeriveVisitor (location_t loc)
3131
: loc (loc), builder (Builder (loc))
3232
{}
3333

34-
std::unique_ptr<Item>
34+
std::vector<std::unique_ptr<Item>>
3535
DeriveVisitor::derive (Item &item, const Attribute &attr,
3636
BuiltinMacro to_derive)
3737
{
3838
switch (to_derive)
3939
{
4040
case BuiltinMacro::Clone:
41-
return DeriveClone (attr.get_locus ()).go (item);
41+
return vec (DeriveClone (attr.get_locus ()).go (item));
4242
case BuiltinMacro::Copy:
43-
return DeriveCopy (attr.get_locus ()).go (item);
43+
return vec (DeriveCopy (attr.get_locus ()).go (item));
4444
case BuiltinMacro::Debug:
4545
rust_warning_at (
4646
attr.get_locus (), 0,
4747
"derive(Debug) is not fully implemented yet and has no effect - only a "
4848
"stub implementation will be generated");
49-
return DeriveDebug (attr.get_locus ()).go (item);
49+
return vec (DeriveDebug (attr.get_locus ()).go (item));
5050
case BuiltinMacro::Default:
51-
return DeriveDefault (attr.get_locus ()).go (item);
51+
return vec (DeriveDefault (attr.get_locus ()).go (item));
5252
case BuiltinMacro::Eq:
53-
return DeriveEq (attr.get_locus ()).go (item);
53+
return vec (DeriveEq (attr.get_locus ()).go (item));
5454
case BuiltinMacro::PartialEq:
55-
return DerivePartialEq (attr.get_locus ()).go (item);
55+
return vec (DerivePartialEq (attr.get_locus ()).go (item));
5656
case BuiltinMacro::Ord:
5757
case BuiltinMacro::PartialOrd:
5858
case BuiltinMacro::Hash:
5959
default:
6060
rust_sorry_at (attr.get_locus (), "unimplemented builtin derive macro");
61-
return nullptr;
61+
return {};
6262
};
6363
}
6464

gcc/rust/expand/rust-derive.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,12 @@ namespace AST {
3434
class DeriveVisitor : public AST::ASTVisitor
3535
{
3636
public:
37-
static std::unique_ptr<Item> derive (Item &item, const Attribute &derive,
38-
BuiltinMacro to_derive);
37+
/**
38+
* Expand a built-in derive macro on an item. This may generate multiple items
39+
* which all need to be integrated to the existing AST
40+
*/
41+
static std::vector<std::unique_ptr<Item>>
42+
derive (Item &item, const Attribute &derive, BuiltinMacro to_derive);
3943

4044
protected:
4145
DeriveVisitor (location_t loc);

gcc/rust/expand/rust-expand-visitor.cc

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ ExpandVisitor::go (AST::Crate &crate)
4343
visit (crate);
4444
}
4545

46-
static std::unique_ptr<AST::Item>
46+
static std::vector<std::unique_ptr<AST::Item>>
4747
builtin_derive_item (AST::Item &item, const AST::Attribute &derive,
4848
BuiltinMacro to_derive)
4949
{
@@ -189,11 +189,12 @@ ExpandVisitor::expand_inner_items (
189189
to_derive.get ().as_string ());
190190
if (maybe_builtin.has_value ())
191191
{
192-
auto new_item
192+
auto new_items
193193
= builtin_derive_item (item, current,
194194
maybe_builtin.value ());
195195

196-
it = items.insert (it, std::move (new_item));
196+
for (auto &&new_item : new_items)
197+
it = items.insert (it, std::move (new_item));
197198
}
198199
else
199200
{
@@ -276,12 +277,14 @@ ExpandVisitor::expand_inner_stmts (AST::BlockExpr &expr)
276277
to_derive.get ().as_string ());
277278
if (maybe_builtin.has_value ())
278279
{
279-
auto new_item
280+
auto new_items
280281
= builtin_derive_item (item, current,
281282
maybe_builtin.value ());
283+
282284
// this inserts the derive *before* the item - is it a
283285
// problem?
284-
it = stmts.insert (it, std::move (new_item));
286+
for (auto &&new_item : new_items)
287+
it = stmts.insert (it, std::move (new_item));
285288
}
286289
else
287290
{

0 commit comments

Comments
 (0)