34
34
#include " rust-attribute-values.h"
35
35
#include " rust-attributes.h"
36
36
#include " rust-immutable-name-resolution-context.h"
37
+ #include " rust-ggc.h"
37
38
38
39
#include " fold-const.h"
39
40
#include " stringpool.h"
47
48
namespace Rust {
48
49
namespace Compile {
49
50
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 )
51
52
{
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;
55
88
}
56
89
57
90
void
@@ -85,8 +118,6 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
85
118
bool is_cold = attr.get_path ().as_string () == Values::Attributes::COLD;
86
119
bool is_link_section
87
120
= attr.get_path ().as_string () == Values::Attributes::LINK_SECTION;
88
- bool no_mangle
89
- = attr.get_path ().as_string () == Values::Attributes::NO_MANGLE;
90
121
bool is_deprecated
91
122
= attr.get_path ().as_string () == Values::Attributes::DEPRECATED;
92
123
bool is_proc_macro
@@ -117,10 +148,6 @@ HIRCompileBase::setup_fndecl (tree fndecl, bool is_main_entry_point,
117
148
{
118
149
handle_deprecated_attribute_on_fndecl (fndecl, attr);
119
150
}
120
- else if (no_mangle)
121
- {
122
- handle_no_mangle_attribute_on_fndecl (fndecl, attr);
123
- }
124
151
else if (is_proc_macro)
125
152
{
126
153
handle_bang_proc_macro_attribute_on_fndecl (fndecl, attr);
@@ -269,22 +296,6 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl (
269
296
set_decl_section_name (fndecl, msg_str->c_str ());
270
297
}
271
298
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
-
288
299
void
289
300
HIRCompileBase::handle_deprecated_attribute_on_fndecl (
290
301
tree fndecl, const AST::Attribute &attr)
@@ -702,7 +713,6 @@ HIRCompileBase::compile_function (
702
713
/* So that 'MAIN_NAME_P' works. */
703
714
main_identifier_node = get_identifier (ir_symbol_name.c_str ());
704
715
}
705
- std::string asm_name = fn_name;
706
716
707
717
unsigned int flags = 0 ;
708
718
tree fndecl = Backend::function (compiled_fn_type, ir_symbol_name,
@@ -713,12 +723,10 @@ HIRCompileBase::compile_function (
713
723
setup_abi_options (fndecl, get_abi (outer_attrs, qualifiers));
714
724
715
725
// 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 ());
722
730
723
731
// insert into the context
724
732
ctx->insert_function_decl (fntype, fndecl);
0 commit comments