Skip to content

Commit 87936c8

Browse files
powerboat9philberty
authored andcommitted
Create LocalVariable
This should make it easier for us to move away from leaking pointers to Bvariable everywhere. Since LocalVariable has a single field of type tree, it should be the same size as a pointer to Bvariable, making the switch to LocalVariable wherever possible strictly an improvement. gcc/rust/ChangeLog: * backend/rust-compile-expr.cc (CompileExpr::visit): Implicitly convert LocalVariable to pointer to Bvariable. * rust-backend.h (local_variable): Return LocalVariable. (parameter_variable): Likewise. (static_chain_variable): Likewise. (temporary_variable): Likewise. * rust-gcc.cc (local_variable): Likewise. (parameter_variable): Likewise. (static_chain_variable): Likewise. (temporary_variable): Likewise. (LocalVariable::get_tree): New function. (LocalVariable::error_variable): Likewise. * rust-gcc.h (class LocalVariable): New class. Signed-off-by: Owen Avery <powerboat9.gamer@gmail.com>
1 parent 8e708ed commit 87936c8

File tree

4 files changed

+64
-23
lines changed

4 files changed

+64
-23
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ CompileExpr::visit (HIR::ArithmeticOrLogicalExpr &expr)
175175
}
176176

177177
auto receiver_tmp = NULL_TREE;
178-
auto receiver
178+
Bvariable *receiver
179179
= Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
180180
TREE_TYPE (lhs), lhs, true,
181181
expr.get_locus (), &receiver_tmp);
@@ -214,7 +214,7 @@ CompileExpr::visit (HIR::CompoundAssignmentExpr &expr)
214214
if (ctx->in_fn () && !ctx->const_context_p ())
215215
{
216216
auto tmp = NULL_TREE;
217-
auto receiver
217+
Bvariable *receiver
218218
= Backend::temporary_variable (ctx->peek_fn ().fndecl, NULL_TREE,
219219
TREE_TYPE (lhs), lhs, true,
220220
expr.get_locus (), &tmp);

gcc/rust/rust-backend.h

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -349,18 +349,18 @@ void global_variable_set_init (Bvariable *, tree);
349349
// the function, as otherwise the variable would be on the heap).
350350
// LOCATION is where the variable is defined. For each local variable
351351
// the frontend will call init_statement to set the initial value.
352-
Bvariable *local_variable (tree function, GGC::Ident name, tree type,
353-
Bvariable *decl_var, location_t location);
352+
LocalVariable local_variable (tree function, GGC::Ident name, tree type,
353+
Bvariable *decl_var, location_t location);
354354

355355
// Create a function parameter. This is an incoming parameter, not
356356
// a result parameter (result parameters are treated as local
357357
// variables). The arguments are as for local_variable.
358-
Bvariable *parameter_variable (tree function, GGC::Ident name, tree type,
359-
location_t location);
358+
LocalVariable parameter_variable (tree function, GGC::Ident name, tree type,
359+
location_t location);
360360

361361
// Create a static chain parameter. This is the closure parameter.
362-
Bvariable *static_chain_variable (tree function, GGC::Ident name, tree type,
363-
location_t location);
362+
LocalVariable static_chain_variable (tree function, GGC::Ident name, tree type,
363+
location_t location);
364364

365365
// Create a temporary variable. A temporary variable has no name,
366366
// just a type. We pass in FUNCTION and BLOCK in case they are
@@ -373,9 +373,9 @@ Bvariable *static_chain_variable (tree function, GGC::Ident name, tree type,
373373
// variable, and may not be very useful. This function should
374374
// return a variable which can be referenced later and should set
375375
// *PSTATEMENT to a statement which initializes the variable.
376-
Bvariable *temporary_variable (tree fndecl, tree bind_tree, tree type,
377-
tree init, bool address_is_taken,
378-
location_t location, tree *pstatement);
376+
LocalVariable temporary_variable (tree fndecl, tree bind_tree, tree type,
377+
tree init, bool address_is_taken,
378+
location_t location, tree *pstatement);
379379

380380
// Labels.
381381

gcc/rust/rust-gcc.cc

Lines changed: 29 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,23 @@ Bvariable::error_variable ()
8383
return new Bvariable (error_mark_node);
8484
}
8585

86+
// Get the tree of a variable for use as an expression
87+
tree
88+
LocalVariable::get_tree (location_t location) const
89+
{
90+
if (error_operand_p (t))
91+
return error_mark_node;
92+
93+
TREE_USED (t) = 1;
94+
return t;
95+
}
96+
97+
LocalVariable
98+
LocalVariable::error_variable ()
99+
{
100+
return LocalVariable (error_mark_node);
101+
}
102+
86103
// This file implements the interface between the Rust frontend proper
87104
// and the gcc IR. This implements specific instantiations of
88105
// abstract classes defined by the Rust frontend proper. The Rust
@@ -2014,12 +2031,12 @@ global_variable_set_init (Bvariable *var, tree expr_tree)
20142031

20152032
// Make a local variable.
20162033

2017-
Bvariable *
2034+
LocalVariable
20182035
local_variable (tree function, GGC::Ident name, tree type_tree,
20192036
Bvariable *decl_var, location_t location)
20202037
{
20212038
if (error_operand_p (type_tree))
2022-
return Bvariable::error_variable ();
2039+
return LocalVariable::error_variable ();
20232040
tree decl = build_decl (location, VAR_DECL, name.as_tree (), type_tree);
20242041
DECL_CONTEXT (decl) = function;
20252042

@@ -2029,33 +2046,33 @@ local_variable (tree function, GGC::Ident name, tree type_tree,
20292046
SET_DECL_VALUE_EXPR (decl, decl_var->get_decl ());
20302047
}
20312048
rust_preserve_from_gc (decl);
2032-
return new Bvariable (decl);
2049+
return LocalVariable (decl);
20332050
}
20342051

20352052
// Make a function parameter variable.
20362053

2037-
Bvariable *
2054+
LocalVariable
20382055
parameter_variable (tree function, GGC::Ident name, tree type_tree,
20392056
location_t location)
20402057
{
20412058
if (error_operand_p (type_tree))
2042-
return Bvariable::error_variable ();
2059+
return LocalVariable::error_variable ();
20432060
tree decl = build_decl (location, PARM_DECL, name.as_tree (), type_tree);
20442061
DECL_CONTEXT (decl) = function;
20452062
DECL_ARG_TYPE (decl) = type_tree;
20462063

20472064
rust_preserve_from_gc (decl);
2048-
return new Bvariable (decl);
2065+
return LocalVariable (decl);
20492066
}
20502067

20512068
// Make a static chain variable.
20522069

2053-
Bvariable *
2070+
LocalVariable
20542071
static_chain_variable (tree fndecl, GGC::Ident name, tree type_tree,
20552072
location_t location)
20562073
{
20572074
if (error_operand_p (type_tree))
2058-
return Bvariable::error_variable ();
2075+
return LocalVariable::error_variable ();
20592076
tree decl = build_decl (location, PARM_DECL, name.as_tree (), type_tree);
20602077
DECL_CONTEXT (decl) = fndecl;
20612078
DECL_ARG_TYPE (decl) = type_tree;
@@ -2076,12 +2093,12 @@ static_chain_variable (tree fndecl, GGC::Ident name, tree type_tree,
20762093
DECL_STATIC_CHAIN (fndecl) = 1;
20772094

20782095
rust_preserve_from_gc (decl);
2079-
return new Bvariable (decl);
2096+
return LocalVariable (decl);
20802097
}
20812098

20822099
// Make a temporary variable.
20832100

2084-
Bvariable *
2101+
LocalVariable
20852102
temporary_variable (tree fndecl, tree bind_tree, tree type_tree, tree init_tree,
20862103
bool is_address_taken, location_t location,
20872104
tree *pstatement)
@@ -2091,7 +2108,7 @@ temporary_variable (tree fndecl, tree bind_tree, tree type_tree, tree init_tree,
20912108
|| error_operand_p (fndecl))
20922109
{
20932110
*pstatement = error_mark_node;
2094-
return Bvariable::error_variable ();
2111+
return LocalVariable::error_variable ();
20952112
}
20962113

20972114
tree var;
@@ -2141,7 +2158,7 @@ temporary_variable (tree fndecl, tree bind_tree, tree type_tree, tree init_tree,
21412158
|| TREE_TYPE (init_tree) == void_type_node))
21422159
*pstatement = compound_statement (init_tree, *pstatement);
21432160

2144-
return new Bvariable (var);
2161+
return LocalVariable (var);
21452162
}
21462163

21472164
// Make a label.

gcc/rust/rust-gcc.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,4 +59,28 @@ class Bvariable
5959
tree orig_type_;
6060
};
6161

62+
// like Bvariable, but orig_type_ == nullptr always holds
63+
// could be any variable which isn't a zero-sized global
64+
class LocalVariable
65+
{
66+
public:
67+
LocalVariable (tree t) : t (t) {}
68+
69+
// Get the tree for use as an expression.
70+
tree get_tree (location_t) const;
71+
72+
// Get the actual decl;
73+
tree get_decl () const { return t; }
74+
75+
// Create an error variable. This is used for cases which should
76+
// not occur in a correct program, in order to keep the compilation
77+
// going without crashing.
78+
static LocalVariable error_variable ();
79+
80+
operator Bvariable * () const { return new Bvariable (t); }
81+
82+
private:
83+
tree t;
84+
};
85+
6286
#endif // RUST_GCC

0 commit comments

Comments
 (0)