Skip to content

Commit fa2c4c8

Browse files
committed
gccrs: Refactor CompileDrop and add DropBuilder
Refactor CompileDrop to keep Context as a member instead of passing it to each method. Move the drop candidate note/peek operations from Context to DropBuilder. This keeps the drop-specific API off the compile Context. gcc/rust/ChangeLog: * Make-lang.in: Add rust-compile-drop-builder.o. * backend/rust-compile-base.cc (HIRCompileBase::compile_function_body): Update CompileDrop calls. * backend/rust-compile-block.cc (CompileBlock::visit): Update CompileDrop calls. * backend/rust-compile-context.h (Context): Allow DropBuilder to access drop candidate storage and move drop candidate APIs to DropBuilder. * backend/rust-compile-drop.cc (CompileDrop::CompileDrop): Add constructor. (CompileDrop::type_has_drop_impl): Use stored Context member. (CompileDrop::compile_drop_call): Likewise. (CompileDrop::emit_current_scope_drop_calls): Use stored Context member and get drop candidates from DropBuilder. * backend/rust-compile-drop.h: Store Context as a member. * backend/rust-compile-pattern.cc (CompilePatternLet::visit): Use DropBuilder and update CompileDrop calls. * backend/rust-compile-drop-builder.cc: New file. * backend/rust-compile-drop-builder.h: New file. Signed-off-by: Lishin <lishin1008@gmail.com>
1 parent 035002f commit fa2c4c8

9 files changed

Lines changed: 118 additions & 28 deletions

gcc/rust/Make-lang.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ GRS_OBJS = \
225225
rust/rust-compile-struct-field-expr.o \
226226
rust/rust-constexpr.o \
227227
rust/rust-compile-base.o \
228+
rust/rust-compile-drop-builder.o \
228229
rust/rust-compile-drop.o \
229230
rust/rust-tree.o \
230231
rust/rust-compile-context.o \

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,7 +708,7 @@ HIRCompileBase::compile_function_body (tree fndecl,
708708
return_value = coercion_site (id, return_value, actual, expected,
709709
lvalue_locus, rvalue_locus);
710710

711-
CompileDrop::emit_current_scope_drop_calls (ctx);
711+
CompileDrop (ctx).emit_current_scope_drop_calls ();
712712

713713
tree return_stmt
714714
= Backend::return_statement (fndecl, return_value, locus);
@@ -732,7 +732,7 @@ HIRCompileBase::compile_function_body (tree fndecl,
732732
// errors should have occurred
733733
location_t locus = function_body.get_locus ();
734734
tree return_value = unit_expression (locus);
735-
CompileDrop::emit_current_scope_drop_calls (ctx);
735+
CompileDrop (ctx).emit_current_scope_drop_calls ();
736736
tree return_stmt
737737
= Backend::return_statement (fndecl, return_value, locus);
738738
ctx->add_statement (return_stmt);

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ CompileBlock::visit (HIR::BlockExpr &expr)
8585
expr.get_locus ());
8686
ctx->add_statement (assignment);
8787
}
88-
CompileDrop::emit_current_scope_drop_calls (ctx);
88+
CompileDrop (ctx).emit_current_scope_drop_calls ();
8989

9090
ctx->pop_block ();
9191
translated = new_block;

gcc/rust/backend/rust-compile-context.h

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@ struct CustomDeriveInfo
5151
std::vector<std::string> attributes;
5252
};
5353

54+
class DropBuilder;
55+
5456
class Context
5557
{
5658
public:
@@ -136,18 +138,6 @@ class Context
136138

137139
void add_statement (tree stmt) { statements.back ().push_back (stmt); }
138140

139-
std::vector<DropCandidate> &peek_block_drop_candidates ()
140-
{
141-
rust_assert (!block_drop_candidates.empty ());
142-
return block_drop_candidates.back ();
143-
}
144-
145-
void note_simple_drop_candidate (HirId hirid, location_t locus)
146-
{
147-
rust_assert (!block_drop_candidates.empty ());
148-
block_drop_candidates.back ().emplace_back (hirid, locus);
149-
}
150-
151141
void insert_var_decl (HirId id, ::Bvariable *decl)
152142
{
153143
compiled_var_decls[id] = decl;
@@ -421,6 +411,7 @@ class Context
421411
}
422412

423413
private:
414+
friend class DropBuilder;
424415
Context ();
425416

426417
Resolver::TypeCheckContext *tyctx;
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
// Copyright (C) 2026 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#include "rust-compile-drop-builder.h"
20+
#include "rust-compile-context.h"
21+
22+
namespace Rust {
23+
namespace Compile {
24+
25+
DropBuilder::DropBuilder (Context &ctx) : ctx (ctx) {}
26+
27+
void
28+
DropBuilder::note_simple_drop_candidate (HirId hirid, location_t locus)
29+
{
30+
rust_assert (!ctx.block_drop_candidates.empty ());
31+
ctx.block_drop_candidates.back ().emplace_back (hirid, locus);
32+
}
33+
34+
std::vector<DropCandidate> &
35+
DropBuilder::peek_block_drop_candidates ()
36+
{
37+
rust_assert (!ctx.block_drop_candidates.empty ());
38+
return ctx.block_drop_candidates.back ();
39+
}
40+
41+
} // namespace Compile
42+
} // namespace Rust
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// Copyright (C) 2026 Free Software Foundation, Inc.
2+
3+
// This file is part of GCC.
4+
5+
// GCC is free software; you can redistribute it and/or modify it under
6+
// the terms of the GNU General Public License as published by the Free
7+
// Software Foundation; either version 3, or (at your option) any later
8+
// version.
9+
10+
// GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11+
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
12+
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13+
// for more details.
14+
15+
// You should have received a copy of the GNU General Public License
16+
// along with GCC; see the file COPYING3. If not see
17+
// <http://www.gnu.org/licenses/>.
18+
19+
#ifndef RUST_COMPILE_DROP_BUILDER_H
20+
#define RUST_COMPILE_DROP_BUILDER_H
21+
22+
#include "rust-compile-drop-candidate.h"
23+
24+
namespace Rust {
25+
namespace Compile {
26+
27+
class Context;
28+
29+
class DropBuilder
30+
{
31+
public:
32+
DropBuilder (Context &ctx);
33+
34+
void note_simple_drop_candidate (HirId hirid, location_t locus);
35+
std::vector<DropCandidate> &peek_block_drop_candidates ();
36+
37+
private:
38+
Context &ctx;
39+
};
40+
41+
} // namespace Compile
42+
} // namespace Rust
43+
44+
#endif // RUST_COMPILE_DROP_BUILDER_H

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

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-compile-drop.h"
20+
#include "rust-compile-drop-builder.h"
2021
#include "rust-compile-base.h"
2122
#include "rust-compile-context.h"
2223
#include "rust-compile-implitem.h"
@@ -29,8 +30,10 @@
2930
namespace Rust {
3031
namespace Compile {
3132

33+
CompileDrop::CompileDrop (Context *ctx) : ctx (ctx) {}
34+
3235
bool
33-
CompileDrop::type_has_drop_impl (Context *ctx, TyTy::BaseType *ty)
36+
CompileDrop::type_has_drop_impl (TyTy::BaseType *ty)
3437
{
3538
auto drop_lang_item
3639
= ctx->get_mappings ().lookup_lang_item (LangItem::Kind::DROP);
@@ -53,8 +56,8 @@ CompileDrop::type_has_drop_impl (Context *ctx, TyTy::BaseType *ty)
5356

5457
// Find the Drop trait, look for the drop method, and build the function call.
5558
tree
56-
CompileDrop::compile_drop_call (Context *ctx, Bvariable *var,
57-
TyTy::BaseType *ty, location_t locus)
59+
CompileDrop::compile_drop_call (Bvariable *var, TyTy::BaseType *ty,
60+
location_t locus)
5861
{
5962
auto drop_lang = ctx->get_mappings ().lookup_lang_item (LangItem::Kind::DROP);
6063
if (!drop_lang.has_value ())
@@ -88,9 +91,10 @@ CompileDrop::compile_drop_call (Context *ctx, Bvariable *var,
8891
}
8992

9093
void
91-
CompileDrop::emit_current_scope_drop_calls (Context *ctx)
94+
CompileDrop::emit_current_scope_drop_calls ()
9295
{
93-
auto &drop_candidates = ctx->peek_block_drop_candidates ();
96+
DropBuilder drop_builder (*ctx);
97+
auto &drop_candidates = drop_builder.peek_block_drop_candidates ();
9498

9599
for (auto it = drop_candidates.rbegin (); it != drop_candidates.rend (); ++it)
96100
{
@@ -103,7 +107,7 @@ CompileDrop::emit_current_scope_drop_calls (Context *ctx)
103107
ok = ctx->lookup_var_decl (it->hirid, &var);
104108
rust_assert (ok);
105109

106-
tree drop_call = CompileDrop::compile_drop_call (ctx, var, ty, it->locus);
110+
tree drop_call = compile_drop_call (var, ty, it->locus);
107111
if (drop_call != NULL_TREE)
108112
ctx->add_statement (convert_to_void (drop_call, ICV_STATEMENT));
109113
}

gcc/rust/backend/rust-compile-drop.h

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,16 @@ namespace Compile {
2727
class CompileDrop
2828
{
2929
public:
30-
static bool type_has_drop_impl (Context *ctx, TyTy::BaseType *ty);
30+
CompileDrop (Context *ctx);
3131

32-
static tree compile_drop_call (Context *ctx, Bvariable *var,
33-
TyTy::BaseType *ty, location_t locus);
32+
bool type_has_drop_impl (TyTy::BaseType *ty);
3433

35-
static void emit_current_scope_drop_calls (Context *ctx);
34+
void emit_current_scope_drop_calls ();
35+
36+
private:
37+
tree compile_drop_call (Bvariable *var, TyTy::BaseType *ty, location_t locus);
38+
39+
Context *ctx;
3640
};
3741

3842
} // namespace Compile

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
// <http://www.gnu.org/licenses/>.
1818

1919
#include "rust-compile-pattern.h"
20+
#include "rust-compile-drop-builder.h"
2021
#include "print-tree.h"
2122
#include "rust-compile-drop.h"
2223
#include "rust-compile-expr.h"
@@ -1356,12 +1357,15 @@ CompilePatternLet::visit (HIR::IdentifierPattern &pattern)
13561357
drop_ty = ref_ty->get_base ();
13571358
}
13581359

1359-
if (!CompileDrop::type_has_drop_impl (ctx, drop_ty))
1360+
if (!CompileDrop (ctx).type_has_drop_impl (drop_ty))
13601361
return;
13611362

13621363
if (!pattern.has_subpattern () && !pattern.get_is_ref ())
1363-
ctx->note_simple_drop_candidate (pattern.get_mappings ().get_hirid (),
1364-
pattern.get_locus ());
1364+
{
1365+
DropBuilder drop_builder (*ctx);
1366+
drop_builder.note_simple_drop_candidate (
1367+
pattern.get_mappings ().get_hirid (), pattern.get_locus ());
1368+
}
13651369
else
13661370
rust_sorry_at (pattern.get_locus (),
13671371
"drop trait not supported for subpatterns and ref patterns");

0 commit comments

Comments
 (0)