26
26
27
27
#include " fold-const.h"
28
28
#include " stringpool.h"
29
+ #include " attribs.h"
29
30
30
31
namespace Rust {
31
32
namespace Compile {
32
33
34
+ bool inline should_mangle_item (const tree fndecl)
35
+ {
36
+ return lookup_attribute (" no_mangle" , DECL_ATTRIBUTES (fndecl)) == NULL_TREE;
37
+ }
38
+
33
39
void
34
- HIRCompileBase::setup_attributes_on_fndecl (
35
- tree fndecl, bool is_main_entry_point, HIR::Visibility &visibility,
36
- 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)
37
44
{
38
45
// if its the main fn or pub visibility mark its as DECL_PUBLIC
39
46
// please see https://github.com/Rust-GCC/gccrs/pull/137
@@ -58,6 +65,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
58
65
bool is_cold = attr.get_path ().as_string ().compare (" cold" ) == 0 ;
59
66
bool is_link_section
60
67
= attr.get_path ().as_string ().compare (" link_section" ) == 0 ;
68
+ bool no_mangle = attr.get_path ().as_string ().compare (" no_mangle" ) == 0 ;
61
69
if (is_inline)
62
70
{
63
71
handle_inline_attribute_on_fndecl (fndecl, attr);
@@ -74,6 +82,10 @@ HIRCompileBase::setup_attributes_on_fndecl (
74
82
{
75
83
handle_link_section_attribute_on_fndecl (fndecl, attr);
76
84
}
85
+ else if (no_mangle)
86
+ {
87
+ handle_no_mangle_attribute_on_fndecl (fndecl, attr);
88
+ }
77
89
}
78
90
}
79
91
@@ -120,6 +132,21 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl (
120
132
set_decl_section_name (fndecl, msg_str.c_str ());
121
133
}
122
134
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
+
123
150
void
124
151
HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl,
125
152
const AST::Attribute &attr)
@@ -396,16 +423,21 @@ HIRCompileBase::compile_function (
396
423
// we don't mangle the main fn since we haven't implemented the main shim
397
424
bool is_main_fn = fn_name.compare (" main" ) == 0 ;
398
425
std::string asm_name = fn_name;
399
- if (!is_main_fn)
400
- asm_name = ctx->mangle_item (fntype, *canonical_path);
401
426
402
427
unsigned int flags = 0 ;
403
428
tree fndecl = ctx->get_backend ()->function (compiled_fn_type, ir_symbol_name,
404
- asm_name, flags, locus);
405
- setup_attributes_on_fndecl (fndecl, is_main_fn, visibility, qualifiers,
406
- outer_attrs);
429
+ " " /* asm_name */ , flags, locus);
430
+ setup_fndecl (fndecl, is_main_fn, visibility, qualifiers, outer_attrs);
407
431
setup_abi_options (fndecl, fntype->get_abi ());
408
432
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
+
409
441
// insert into the context
410
442
ctx->insert_function_decl (fntype, fndecl);
411
443
0 commit comments