Skip to content

Commit be2afed

Browse files
powerboat9P-E-P
authored andcommitted
Create Rust::GGC::Ident
This should make it easier for us to hand identifiers off to the back end. gcc/rust/ChangeLog: * Make-lang.in (GRS_OBJS): Add rust-ggc.o. * backend/rust-compile-base.cc (HIRCompileBase::compile_function): Adjust call to Backend::function. (HIRCompileBase::compile_constant_item): Likewise and adjust initialization of Backend::typed_identifier. * backend/rust-compile-expr.cc (CompileExpr::visit): Adjust call to Backend::label. * backend/rust-compile-type.cc (TyTyResolveCompile::visit): Adjust initialization of Backend::typed_identifier. * rust-backend.h: Add includes. (Backend::GGC::Ident): Use Rust::GGC::Ident. (struct typed_identifier): Store name as a GGC::Ident rather than a std::string and adjust constructors. (named_type): Take GGC::Ident/tl::optional<GGC::Ident> rather than std::string. (global_variable): Likewise. (local_variable): Likewise. (parameter_variable): Likewise. (static_chain_variable): Likewise. (label): Likewise. (function): Likewise. * rust-gcc.cc (named_type): Likewise. (global_variable): Likewise. (local_variable): Likewise. (parameter_variable): Likewise. (static_chain_variable): Likewise. (label): Likewise. (function): Likewise. (function_defer_statement): Adjust call to Backend::label. (get_identifier_from_string): Remove function. (fill_in_fields): Handle adjustments to typed_identifier. * util/rust-ggc.cc: New file. * util/rust-ggc.h: New file. Signed-off-by: Owen Avery <[email protected]>
1 parent 9dffdef commit be2afed

File tree

8 files changed

+164
-65
lines changed

8 files changed

+164
-65
lines changed

gcc/rust/Make-lang.in

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,7 @@ GRS_OBJS = \
238238
rust/rust-punycode.o \
239239
rust/rust-unwrap-segment.o \
240240
rust/rust-edition.o \
241+
rust/rust-ggc.o \
241242
rust/rust-expand-format-args.o \
242243
rust/rust-lang-item.o \
243244
rust/rust-collect-lang-items.o \

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -689,7 +689,7 @@ HIRCompileBase::compile_function (
689689

690690
unsigned int flags = 0;
691691
tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name,
692-
"" /* asm_name */, flags, locus);
692+
tl::nullopt /* asm_name */, flags, locus);
693693

694694
setup_fndecl (fndecl, is_main_fn, fntype->has_substitutions_defined (),
695695
visibility, qualifiers, outer_attrs);
@@ -807,11 +807,12 @@ HIRCompileBase::compile_constant_item (
807807
// machineary that we already have. This means the best approach is to
808808
// make a _fake_ function with a block so it can hold onto temps then
809809
// use our constexpr code to fold it completely or error_mark_node
810-
Backend::typed_identifier receiver;
810+
Backend::typed_identifier receiver ("", NULL_TREE, UNKNOWN_LOCATION);
811811
tree compiled_fn_type = Backend::function_type (
812812
receiver, {}, {Backend::typed_identifier ("_", const_type, locus)}, NULL,
813813
locus);
814-
tree fndecl = Backend::function (compiled_fn_type, ident, "", 0, locus);
814+
tree fndecl
815+
= Backend::function (compiled_fn_type, ident, tl::nullopt, 0, locus);
815816
TREE_READONLY (fndecl) = 1;
816817

817818
tree enclosing_scope = NULL_TREE;

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -714,7 +714,8 @@ CompileExpr::visit (HIR::LoopExpr &expr)
714714
loop_label.get_lifetime ().get_mappings ().get_hirid (), label);
715715
}
716716

717-
tree loop_begin_label = Backend::label (fnctx.fndecl, "", expr.get_locus ());
717+
tree loop_begin_label
718+
= Backend::label (fnctx.fndecl, tl::nullopt, expr.get_locus ());
718719
tree loop_begin_label_decl
719720
= Backend::label_definition_statement (loop_begin_label);
720721
ctx->add_statement (loop_begin_label_decl);
@@ -756,7 +757,8 @@ CompileExpr::visit (HIR::WhileLoopExpr &expr)
756757
start_location, end_location);
757758
ctx->push_block (loop_block);
758759

759-
tree loop_begin_label = Backend::label (fnctx.fndecl, "", expr.get_locus ());
760+
tree loop_begin_label
761+
= Backend::label (fnctx.fndecl, tl::nullopt, expr.get_locus ());
760762
tree loop_begin_label_decl
761763
= Backend::label_definition_statement (loop_begin_label);
762764
ctx->add_statement (loop_begin_label_decl);
@@ -1143,9 +1145,8 @@ CompileExpr::visit (HIR::MatchExpr &expr)
11431145
// setup the end label so the cases can exit properly
11441146
tree fndecl = fnctx.fndecl;
11451147
location_t end_label_locus = expr.get_locus (); // FIXME
1146-
tree end_label
1147-
= Backend::label (fndecl, "" /* empty creates an artificial label */,
1148-
end_label_locus);
1148+
// tl::nullopt creates an artificial label
1149+
tree end_label = Backend::label (fndecl, tl::nullopt, end_label_locus);
11491150
tree end_label_decl_statement
11501151
= Backend::label_definition_statement (end_label);
11511152

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ TyTyResolveCompile::visit (const TyTy::ClosureType &type)
189189
void
190190
TyTyResolveCompile::visit (const TyTy::FnType &type)
191191
{
192-
Backend::typed_identifier receiver;
192+
Backend::typed_identifier receiver ("", NULL_TREE, UNKNOWN_LOCATION);
193193
std::vector<Backend::typed_identifier> parameters;
194194
std::vector<Backend::typed_identifier> results;
195195

gcc/rust/rust-backend.h

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "rust-linemap.h"
2828
#include "rust-diagnostics.h"
2929
#include "util/rust-operators.h"
30+
#include "util/rust-ggc.h"
31+
#include "util/optional.h"
3032
#include "tree.h"
3133
#include "rust-gcc.h"
3234

@@ -42,21 +44,23 @@ class Bvariable;
4244

4345
namespace Backend {
4446

47+
namespace GGC {
48+
49+
using Rust::GGC::Ident;
50+
51+
} // namespace GGC
52+
4553
void init ();
4654

4755
// Name/type/location. Used for function parameters, struct fields,
4856
// interface methods.
4957
struct typed_identifier
5058
{
51-
std::string name;
59+
GGC::Ident name;
5260
tree type;
5361
location_t location;
5462

55-
typed_identifier () : name (), type (NULL_TREE), location (UNKNOWN_LOCATION)
56-
{}
57-
58-
typed_identifier (const std::string &a_name, tree a_type,
59-
location_t a_location)
63+
typed_identifier (GGC::Ident a_name, tree a_type, location_t a_location)
6064
: name (a_name), type (a_type), location (a_location)
6165
{}
6266
};
@@ -133,7 +137,7 @@ tree array_type (tree element_type, tree length);
133137
// created via placeholder_pointer_type, placeholder_struct_type, or
134138
// placeholder_array_type.. (It may be called for a pointer,
135139
// struct, or array type in a case like "type P *byte; type Q P".)
136-
tree named_type (const std::string &name, tree, location_t);
140+
tree named_type (GGC::Ident name, tree, location_t);
137141

138142
// Return the size of a type.
139143
int64_t type_size (tree);
@@ -314,8 +318,7 @@ void block_add_statements (tree, const std::vector<tree> &);
314318
// be put into a unique section if possible; this is intended to
315319
// permit the linker to garbage collect the variable if it is not
316320
// referenced. LOCATION is where the variable was defined.
317-
Bvariable *global_variable (const std::string &name,
318-
const std::string &asm_name, tree btype,
321+
Bvariable *global_variable (GGC::Ident name, GGC::Ident asm_name, tree btype,
319322
bool is_external, bool is_hidden,
320323
bool in_unique_section, location_t location);
321324

@@ -339,18 +342,18 @@ void global_variable_set_init (Bvariable *, tree);
339342
// the function, as otherwise the variable would be on the heap).
340343
// LOCATION is where the variable is defined. For each local variable
341344
// the frontend will call init_statement to set the initial value.
342-
Bvariable *local_variable (tree function, const std::string &name, tree type,
345+
Bvariable *local_variable (tree function, GGC::Ident name, tree type,
343346
Bvariable *decl_var, location_t location);
344347

345348
// Create a function parameter. This is an incoming parameter, not
346349
// a result parameter (result parameters are treated as local
347350
// variables). The arguments are as for local_variable.
348-
Bvariable *parameter_variable (tree function, const std::string &name,
349-
tree type, location_t location);
351+
Bvariable *parameter_variable (tree function, GGC::Ident name, tree type,
352+
location_t location);
350353

351354
// Create a static chain parameter. This is the closure parameter.
352-
Bvariable *static_chain_variable (tree function, const std::string &name,
353-
tree type, location_t location);
355+
Bvariable *static_chain_variable (tree function, GGC::Ident name, tree type,
356+
location_t location);
354357

355358
// Create a temporary variable. A temporary variable has no name,
356359
// just a type. We pass in FUNCTION and BLOCK in case they are
@@ -369,10 +372,10 @@ Bvariable *temporary_variable (tree fndecl, tree bind_tree, tree type,
369372

370373
// Labels.
371374

372-
// Create a new label. NAME will be empty if this is a label
375+
// Create a new label. NAME will be tl::nullopt if this is a label
373376
// created by the frontend for a loop construct. The location is
374377
// where the label is defined.
375-
tree label (tree, const std::string &name, location_t);
378+
tree label (tree, tl::optional<GGC::Ident> name, location_t);
376379

377380
// Create a statement which defines a label. This statement will be
378381
// put into the codestream at the point where the label should be
@@ -408,12 +411,12 @@ static const unsigned int function_does_not_return = 1 << 2;
408411
static const unsigned int function_in_unique_section = 1 << 3;
409412

410413
// Declare or define a function of FNTYPE.
411-
// NAME is the Go name of the function. ASM_NAME, if not the empty
412-
// string, is the name that should be used in the symbol table; this
414+
// NAME is the Go name of the function. ASM_NAME, if not tl::nullopt,
415+
// is the name that should be used in the symbol table; this
413416
// will be non-empty if a magic extern comment is used. FLAGS is
414417
// bit flags described above.
415-
tree function (tree fntype, const std::string &name,
416-
const std::string &asm_name, unsigned int flags, location_t);
418+
tree function (tree fntype, GGC::Ident name, tl::optional<GGC::Ident> asm_name,
419+
unsigned int flags, location_t);
417420

418421
// Create a statement that runs all deferred calls for FUNCTION. This should
419422
// be a statement that looks like this in C++:

gcc/rust/rust-gcc.cc

Lines changed: 25 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,6 @@ Bvariable::error_variable ()
9191

9292
// A helper function to create a GCC identifier from a C++ string.
9393

94-
static inline tree
95-
get_identifier_from_string (const std::string &str)
96-
{
97-
return get_identifier_with_length (str.data (), str.length ());
98-
}
99-
10094
namespace Backend {
10195

10296
// Define the built-in functions that are exposed to GCCRust.
@@ -609,7 +603,7 @@ fill_in_fields (tree fill, const std::vector<typed_identifier> &fields,
609603
tree *pp = &field_trees;
610604
for (const auto &p : fields)
611605
{
612-
tree name_tree = get_identifier_from_string (p.name);
606+
tree name_tree = p.name.as_tree ();
613607
tree type_tree = p.type;
614608
if (error_operand_p (type_tree))
615609
return error_mark_node;
@@ -675,7 +669,7 @@ fill_in_array (tree fill, tree element_type, tree length_tree)
675669
// Return a named version of a type.
676670

677671
tree
678-
named_type (const std::string &name, tree type, location_t location)
672+
named_type (GGC::Ident name, tree type, location_t location)
679673
{
680674
if (error_operand_p (type))
681675
return error_mark_node;
@@ -688,15 +682,14 @@ named_type (const std::string &name, tree type, location_t location)
688682
|| TREE_CODE (type) == COMPLEX_TYPE
689683
|| TREE_CODE (type) == BOOLEAN_TYPE))
690684
{
691-
tree decl = build_decl (BUILTINS_LOCATION, TYPE_DECL,
692-
get_identifier_from_string (name), type);
685+
tree decl
686+
= build_decl (BUILTINS_LOCATION, TYPE_DECL, name.as_tree (), type);
693687
TYPE_NAME (type) = decl;
694688
return type;
695689
}
696690

697691
tree copy = build_variant_type_copy (type);
698-
tree decl
699-
= build_decl (location, TYPE_DECL, get_identifier_from_string (name), copy);
692+
tree decl = build_decl (location, TYPE_DECL, name.as_tree (), copy);
700693
DECL_ORIGINAL_TYPE (decl) = type;
701694
TYPE_NAME (copy) = decl;
702695
return copy;
@@ -1924,9 +1917,9 @@ convert_tree (tree type_tree, tree expr_tree, location_t location)
19241917
// Make a global variable.
19251918

19261919
Bvariable *
1927-
global_variable (const std::string &var_name, const std::string &asm_name,
1928-
tree type_tree, bool is_external, bool is_hidden,
1929-
bool in_unique_section, location_t location)
1920+
global_variable (GGC::Ident var_name, GGC::Ident asm_name, tree type_tree,
1921+
bool is_external, bool is_hidden, bool in_unique_section,
1922+
location_t location)
19301923
{
19311924
if (error_operand_p (type_tree))
19321925
return Bvariable::error_variable ();
@@ -1936,20 +1929,19 @@ global_variable (const std::string &var_name, const std::string &asm_name,
19361929
if ((is_external || !is_hidden) && int_size_in_bytes (type_tree) == 0)
19371930
type_tree = non_zero_size_type (type_tree);
19381931

1939-
tree decl = build_decl (location, VAR_DECL,
1940-
get_identifier_from_string (var_name), type_tree);
1932+
tree decl = build_decl (location, VAR_DECL, var_name.as_tree (), type_tree);
19411933
if (is_external)
19421934
DECL_EXTERNAL (decl) = 1;
19431935
else
19441936
TREE_STATIC (decl) = 1;
19451937
if (!is_hidden)
19461938
{
19471939
TREE_PUBLIC (decl) = 1;
1948-
SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
1940+
SET_DECL_ASSEMBLER_NAME (decl, asm_name.as_tree ());
19491941
}
19501942
else
19511943
{
1952-
SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
1944+
SET_DECL_ASSEMBLER_NAME (decl, asm_name.as_tree ());
19531945
}
19541946

19551947
TREE_USED (decl) = 1;
@@ -1989,13 +1981,12 @@ global_variable_set_init (Bvariable *var, tree expr_tree)
19891981
// Make a local variable.
19901982

19911983
Bvariable *
1992-
local_variable (tree function, const std::string &name, tree type_tree,
1984+
local_variable (tree function, GGC::Ident name, tree type_tree,
19931985
Bvariable *decl_var, location_t location)
19941986
{
19951987
if (error_operand_p (type_tree))
19961988
return Bvariable::error_variable ();
1997-
tree decl = build_decl (location, VAR_DECL, get_identifier_from_string (name),
1998-
type_tree);
1989+
tree decl = build_decl (location, VAR_DECL, name.as_tree (), type_tree);
19991990
DECL_CONTEXT (decl) = function;
20001991

20011992
if (decl_var != NULL)
@@ -2010,13 +2001,12 @@ local_variable (tree function, const std::string &name, tree type_tree,
20102001
// Make a function parameter variable.
20112002

20122003
Bvariable *
2013-
parameter_variable (tree function, const std::string &name, tree type_tree,
2004+
parameter_variable (tree function, GGC::Ident name, tree type_tree,
20142005
location_t location)
20152006
{
20162007
if (error_operand_p (type_tree))
20172008
return Bvariable::error_variable ();
2018-
tree decl = build_decl (location, PARM_DECL,
2019-
get_identifier_from_string (name), type_tree);
2009+
tree decl = build_decl (location, PARM_DECL, name.as_tree (), type_tree);
20202010
DECL_CONTEXT (decl) = function;
20212011
DECL_ARG_TYPE (decl) = type_tree;
20222012

@@ -2027,13 +2017,12 @@ parameter_variable (tree function, const std::string &name, tree type_tree,
20272017
// Make a static chain variable.
20282018

20292019
Bvariable *
2030-
static_chain_variable (tree fndecl, const std::string &name, tree type_tree,
2020+
static_chain_variable (tree fndecl, GGC::Ident name, tree type_tree,
20312021
location_t location)
20322022
{
20332023
if (error_operand_p (type_tree))
20342024
return Bvariable::error_variable ();
2035-
tree decl = build_decl (location, PARM_DECL,
2036-
get_identifier_from_string (name), type_tree);
2025+
tree decl = build_decl (location, PARM_DECL, name.as_tree (), type_tree);
20372026
DECL_CONTEXT (decl) = fndecl;
20382027
DECL_ARG_TYPE (decl) = type_tree;
20392028
TREE_USED (decl) = 1;
@@ -2124,10 +2113,10 @@ temporary_variable (tree fndecl, tree bind_tree, tree type_tree, tree init_tree,
21242113
// Make a label.
21252114

21262115
tree
2127-
label (tree func_tree, const std::string &name, location_t location)
2116+
label (tree func_tree, tl::optional<GGC::Ident> name, location_t location)
21282117
{
21292118
tree decl;
2130-
if (name.empty ())
2119+
if (!name.has_value ())
21312120
{
21322121
if (DECL_STRUCT_FUNCTION (func_tree) == NULL)
21332122
push_struct_function (func_tree);
@@ -2140,7 +2129,7 @@ label (tree func_tree, const std::string &name, location_t location)
21402129
}
21412130
else
21422131
{
2143-
tree id = get_identifier_from_string (name);
2132+
tree id = name->as_tree ();
21442133
decl = build_decl (location, LABEL_DECL, id, void_type_node);
21452134
DECL_CONTEXT (decl) = func_tree;
21462135
}
@@ -2179,21 +2168,21 @@ label_address (tree label, location_t location)
21792168
// Declare or define a new function.
21802169

21812170
tree
2182-
function (tree functype, const std::string &name, const std::string &asm_name,
2171+
function (tree functype, GGC::Ident name, tl::optional<GGC::Ident> asm_name,
21832172
unsigned int flags, location_t location)
21842173
{
21852174
if (error_operand_p (functype))
21862175
return error_mark_node;
21872176

21882177
gcc_assert (FUNCTION_POINTER_TYPE_P (functype));
21892178
functype = TREE_TYPE (functype);
2190-
tree id = get_identifier_from_string (name);
2179+
tree id = name.as_tree ();
21912180
if (error_operand_p (id))
21922181
return error_mark_node;
21932182

21942183
tree decl = build_decl (location, FUNCTION_DECL, id, functype);
2195-
if (!asm_name.empty ())
2196-
SET_DECL_ASSEMBLER_NAME (decl, get_identifier_from_string (asm_name));
2184+
if (asm_name.has_value ())
2185+
SET_DECL_ASSEMBLER_NAME (decl, asm_name->as_tree ());
21972186

21982187
if ((flags & function_is_declaration) != 0)
21992188
DECL_EXTERNAL (decl) = 1;
@@ -2236,7 +2225,7 @@ function_defer_statement (tree function, tree undefer_tree, tree defer_tree,
22362225
push_cfun (DECL_STRUCT_FUNCTION (function));
22372226

22382227
tree stmt_list = NULL;
2239-
tree label = Backend::label (function, "", location);
2228+
tree label = Backend::label (function, tl::nullopt, location);
22402229
tree label_def = label_definition_statement (label);
22412230
append_to_statement_list (label_def, &stmt_list);
22422231

0 commit comments

Comments
 (0)