Skip to content

Commit 1d219a6

Browse files
committed
mangle/link_name fixes
1 parent 511d309 commit 1d219a6

File tree

3 files changed

+43
-33
lines changed

3 files changed

+43
-33
lines changed

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

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@
3434
#include "rust-attribute-values.h"
3535
#include "rust-attributes.h"
3636
#include "rust-immutable-name-resolution-context.h"
37+
#include "rust-ggc.h"
3738

3839
#include "fold-const.h"
3940
#include "stringpool.h"
@@ -47,11 +48,43 @@
4748
namespace Rust {
4849
namespace Compile {
4950

50-
bool inline should_mangle_item (const tree fndecl)
51+
tl::optional<GGC::Ident> inline get_unmangled_name (const std::string &function_name, const AST::AttrVec &attrs)
5152
{
52-
return lookup_attribute (Values::Attributes::NO_MANGLE,
53-
DECL_ATTRIBUTES (fndecl))
54-
== NULL_TREE;
53+
tl::optional<GGC::Ident> set_name;
54+
for (auto &attr : attrs)
55+
{
56+
auto name = attr.get_path ().as_string ();
57+
if (name == Values::Attributes::NO_MANGLE)
58+
{
59+
// TODO: better errors
60+
rust_assert (!set_name.has_value ());
61+
set_name = function_name;
62+
63+
if (attr.has_attr_input ())
64+
{
65+
rust_error_at (attr.get_locus (),
66+
"attribute %<no_mangle%> does not accept any arguments");
67+
}
68+
69+
}
70+
else if (name == Values::Attributes::LINK_NAME)
71+
{
72+
// TODO: better errors
73+
rust_assert (!set_name.has_value ());
74+
75+
rust_assert (attr.has_attr_input ());
76+
auto &input = attr.get_attr_input ();
77+
78+
rust_assert (input.get_attr_input_type () == AST::AttrInput::AttrInputType::LITERAL);
79+
auto &literal = static_cast<AST::AttrInputLiteral &> (input).get_literal ();
80+
81+
rust_assert (literal.get_lit_type () == AST::Literal::LitType::STRING || literal.get_lit_type () == AST::Literal::LitType::RAW_STRING);
82+
// TODO: special handling for STRING/RAW_STRING?
83+
set_name = literal.as_string ();
84+
}
85+
}
86+
87+
return set_name;
5588
}
5689

5790
void
@@ -85,8 +118,6 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
85118
bool is_cold = attr.get_path ().as_string () == Values::Attributes::COLD;
86119
bool is_link_section
87120
= attr.get_path ().as_string () == Values::Attributes::LINK_SECTION;
88-
bool no_mangle
89-
= attr.get_path ().as_string () == Values::Attributes::NO_MANGLE;
90121
bool is_deprecated
91122
= attr.get_path ().as_string () == Values::Attributes::DEPRECATED;
92123
bool is_proc_macro
@@ -117,10 +148,6 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
117148
{
118149
handle_deprecated_attribute_on_fndecl (fndecl, attr);
119150
}
120-
else if (no_mangle)
121-
{
122-
handle_no_mangle_attribute_on_fndecl (fndecl, attr);
123-
}
124151
else if (is_proc_macro)
125152
{
126153
handle_bang_proc_macro_attribute_on_fndecl (fndecl, attr);
@@ -269,22 +296,6 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl (
269296
set_decl_section_name (fndecl, msg_str->c_str ());
270297
}
271298

272-
void
273-
HIRCompileBase::handle_no_mangle_attribute_on_fndecl (
274-
tree fndecl, const AST::Attribute &attr)
275-
{
276-
if (attr.has_attr_input ())
277-
{
278-
rust_error_at (attr.get_locus (),
279-
"attribute %<no_mangle%> does not accept any arguments");
280-
return;
281-
}
282-
283-
DECL_ATTRIBUTES (fndecl)
284-
= tree_cons (get_identifier (Values::Attributes::NO_MANGLE), NULL_TREE,
285-
DECL_ATTRIBUTES (fndecl));
286-
}
287-
288299
void
289300
HIRCompileBase::handle_deprecated_attribute_on_fndecl (
290301
tree fndecl, const AST::Attribute &attr)
@@ -702,7 +713,6 @@ HIRCompileBase::compile_function (
702713
/* So that 'MAIN_NAME_P' works. */
703714
main_identifier_node = get_identifier (ir_symbol_name.c_str ());
704715
}
705-
std::string asm_name = fn_name;
706716

707717
unsigned int flags = 0;
708718
tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name,
@@ -713,12 +723,10 @@ HIRCompileBase::compile_function (
713723
setup_abi_options (fndecl, get_abi (outer_attrs, qualifiers));
714724

715725
// conditionally mangle the function name
716-
bool should_mangle = should_mangle_item (fndecl);
717-
if (!is_main_fn && should_mangle)
718-
asm_name = ctx->mangle_item (fntype, canonical_path);
719-
SET_DECL_ASSEMBLER_NAME (fndecl,
720-
get_identifier_with_length (asm_name.data (),
721-
asm_name.length ()));
726+
auto unmangled = is_main_fn ? "main" : get_unmangled_name (fn_name, outer_attrs);
727+
GGC::Ident asm_name = unmangled ? *unmangled : ctx->mangle_item (fntype, canonical_path);
728+
729+
SET_DECL_ASSEMBLER_NAME (fndecl, asm_name.as_tree ());
722730

723731
// insert into the context
724732
ctx->insert_function_decl (fntype, fndecl);

gcc/rust/util/rust-attribute-values.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ class Attributes
3737
static constexpr auto &MUST_USE = "must_use";
3838
static constexpr auto &LANG = "lang";
3939
static constexpr auto &LINK_SECTION = "link_section";
40+
static constexpr auto &LINK_NAME = "link_name";
4041
static constexpr auto &NO_MANGLE = "no_mangle";
4142
static constexpr auto &REPR = "repr";
4243
static constexpr auto &RUSTC_BUILTIN_MACRO = "rustc_builtin_macro";

gcc/rust/util/rust-attributes.cc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,7 @@ static const BuiltinAttrDefinition __definitions[]
7979
{Attrs::MUST_USE, STATIC_ANALYSIS},
8080
{Attrs::LANG, HIR_LOWERING},
8181
{Attrs::LINK_SECTION, CODE_GENERATION},
82+
{Attrs::LINK_NAME, CODE_GENERATION},
8283
{Attrs::NO_MANGLE, CODE_GENERATION},
8384
{Attrs::REPR, CODE_GENERATION},
8485
{Attrs::RUSTC_BUILTIN_MACRO, EXPANSION},

0 commit comments

Comments
 (0)