|
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 |
|
33 |
| -bool |
34 |
| -should_mangle_item (const AST::AttrVec &attrs) |
| 34 | +bool inline should_mangle_item (const tree fndecl) |
35 | 35 | {
|
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; |
49 | 37 | }
|
50 | 38 |
|
51 | 39 | 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) |
55 | 44 | {
|
56 | 45 | // if its the main fn or pub visibility mark its as DECL_PUBLIC
|
57 | 46 | // please see https://github.com/Rust-GCC/gccrs/pull/137
|
@@ -95,8 +84,7 @@ HIRCompileBase::setup_attributes_on_fndecl (
|
95 | 84 | }
|
96 | 85 | else if (no_mangle)
|
97 | 86 | {
|
98 |
| - // we handled this in `should_mangle_item` |
99 |
| - continue; |
| 87 | + handle_no_mangle_attribute_on_fndecl (fndecl, attr); |
100 | 88 | }
|
101 | 89 | }
|
102 | 90 | }
|
@@ -144,6 +132,21 @@ HIRCompileBase::handle_link_section_attribute_on_fndecl (
|
144 | 132 | set_decl_section_name (fndecl, msg_str.c_str ());
|
145 | 133 | }
|
146 | 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 | + |
147 | 150 | void
|
148 | 151 | HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl,
|
149 | 152 | const AST::Attribute &attr)
|
@@ -420,19 +423,21 @@ HIRCompileBase::compile_function (
|
420 | 423 | // we don't mangle the main fn since we haven't implemented the main shim
|
421 | 424 | bool is_main_fn = fn_name.compare ("main") == 0;
|
422 | 425 | 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); |
428 | 426 |
|
429 | 427 | unsigned int flags = 0;
|
430 | 428 | 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); |
434 | 431 | setup_abi_options (fndecl, fntype->get_abi ());
|
435 | 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 | + |
436 | 441 | // insert into the context
|
437 | 442 | ctx->insert_function_decl (fntype, fndecl);
|
438 | 443 |
|
|
0 commit comments