Skip to content

Commit a969ab6

Browse files
committed
backend: address comments about no_mangle
1 parent af7622f commit a969ab6

File tree

2 files changed

+40
-31
lines changed

2 files changed

+40
-31
lines changed

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

Lines changed: 33 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -26,32 +26,21 @@
2626

2727
#include "fold-const.h"
2828
#include "stringpool.h"
29+
#include "attribs.h"
2930

3031
namespace Rust {
3132
namespace Compile {
3233

33-
bool
34-
should_mangle_item (const AST::AttrVec &attrs)
34+
bool inline should_mangle_item (const tree fndecl)
3535
{
36-
for (const auto &attr : attrs)
37-
{
38-
if (attr.get_path ().as_string ().compare ("no_mangle") == 0)
39-
{
40-
if (attr.has_attr_input ())
41-
rust_error_at (
42-
attr.get_locus (),
43-
"attribute %<no_mangle%> does not accept any arguments");
44-
return false;
45-
}
46-
}
47-
48-
return true;
36+
return lookup_attribute ("no_mangle", DECL_ATTRIBUTES (fndecl)) == NULL_TREE;
4937
}
5038

5139
void
52-
HIRCompileBase::setup_attributes_on_fndecl (
53-
tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility,
54-
const HIR::FunctionQualifiers &qualifiers, const AST::AttrVec &attrs)
40+
HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
41+
HIR::Visibility &visibility,
42+
const HIR::FunctionQualifiers &qualifiers,
43+
const AST::AttrVec &attrs)
5544
{
5645
// if its the main fn or pub visibility mark its as DECL_PUBLIC
5746
// please see https://github.com/Rust-GCC/gccrs/pull/137
@@ -95,8 +84,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
9584
}
9685
else if (no_mangle)
9786
{
98-
// we handled this in `should_mangle_item`
99-
continue;
87+
handle_no_mangle_attribute_on_fndecl (fndecl, attr);
10088
}
10189
}
10290
}
@@ -144,6 +132,21 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl (
144132
set_decl_section_name (fndecl, msg_str.c_str ());
145133
}
146134

135+
void
136+
HIRCompileBase::handle_no_mangle_attribute_on_fndecl (
137+
tree fndecl, const AST::Attribute &attr)
138+
{
139+
if (attr.has_attr_input ())
140+
{
141+
rust_error_at (attr.get_locus (),
142+
"attribute %<no_mangle%> does not accept any arguments");
143+
return;
144+
}
145+
146+
DECL_ATTRIBUTES (fndecl) = tree_cons (get_identifier ("no_mangle"), NULL_TREE,
147+
DECL_ATTRIBUTES (fndecl));
148+
}
149+
147150
void
148151
HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl,
149152
const AST::Attribute &attr)
@@ -420,19 +423,21 @@ HIRCompileBase::compile_function (
420423
// we don't mangle the main fn since we haven't implemented the main shim
421424
bool is_main_fn = fn_name.compare ("main") == 0;
422425
std::string asm_name = fn_name;
423-
// TODO(liushuyu): we should probably move this part to
424-
// `setup_attributes_on_fndecl` if possible
425-
bool should_mangle = should_mangle_item (outer_attrs);
426-
if (!is_main_fn && should_mangle)
427-
asm_name = ctx->mangle_item (fntype, *canonical_path);
428426

429427
unsigned int flags = 0;
430428
tree fndecl = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name,
431-
asm_name, flags, locus);
432-
setup_attributes_on_fndecl (fndecl, is_main_fn, visibility, qualifiers,
433-
outer_attrs);
429+
"" /* asm_name */, flags, locus);
430+
setup_fndecl (fndecl, is_main_fn, visibility, qualifiers, outer_attrs);
434431
setup_abi_options (fndecl, fntype->get_abi ());
435432

433+
// conditionally mangle the function name
434+
bool should_mangle = should_mangle_item (fndecl);
435+
if (!is_main_fn && should_mangle)
436+
asm_name = ctx->mangle_item (fntype, *canonical_path);
437+
SET_DECL_ASSEMBLER_NAME (fndecl,
438+
get_identifier_with_length (asm_name.data (),
439+
asm_name.length ()));
440+
436441
// insert into the context
437442
ctx->insert_function_decl (fntype, fndecl);
438443

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

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ class HIRCompileBase
7575
tree resolve_unsized_adjustment (Resolver::Adjustment &adjustment,
7676
tree expression, Location locus);
7777

78-
static void setup_attributes_on_fndecl (
79-
tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility,
80-
const HIR::FunctionQualifiers &qualifiers, const AST::AttrVec &attrs);
78+
static void setup_fndecl (tree fndecl, bool is_main_entry_point,
79+
HIR::Visibility &visibility,
80+
const HIR::FunctionQualifiers &qualifiers,
81+
const AST::AttrVec &attrs);
8182

8283
static void handle_inline_attribute_on_fndecl (tree fndecl,
8384
const AST::Attribute &attr);
@@ -92,6 +93,9 @@ class HIRCompileBase
9293
handle_link_section_attribute_on_fndecl (tree fndecl,
9394
const AST::Attribute &attr);
9495

96+
static void handle_no_mangle_attribute_on_fndecl (tree fndecl,
97+
const AST::Attribute &attr);
98+
9599
static void setup_abi_options (tree fndecl, ABI abi);
96100

97101
static tree address_expression (tree, Location);

0 commit comments

Comments
 (0)