diff options
Diffstat (limited to 'gcc/rust/backend/rust-compile-base.cc')
-rw-r--r-- | gcc/rust/backend/rust-compile-base.cc | 43 |
1 files changed, 36 insertions, 7 deletions
diff --git a/gcc/rust/backend/rust-compile-base.cc b/gcc/rust/backend/rust-compile-base.cc index 33e4c26..a705da7 100644 --- a/gcc/rust/backend/rust-compile-base.cc +++ b/gcc/rust/backend/rust-compile-base.cc @@ -22,6 +22,8 @@ #include "rust-compile-fnparam.h" #include "rust-compile-var-decl.h" +#include "rust-expr.h" // for AST::AttrInputLiteral + #include "fold-const.h" #include "stringpool.h" @@ -52,10 +54,16 @@ HIRCompileBase::setup_attributes_on_fndecl ( for (const auto &attr : attrs) { bool is_inline = attr.get_path ().as_string ().compare ("inline") == 0; + bool is_must_use + = attr.get_path ().as_string ().compare ("must_use") == 0; if (is_inline) { handle_inline_attribute_on_fndecl (fndecl, attr); } + else if (is_must_use) + { + handle_must_use_attribute_on_fndecl (fndecl, attr); + } } } @@ -108,6 +116,30 @@ HIRCompileBase::handle_inline_attribute_on_fndecl (tree fndecl, } void +HIRCompileBase::handle_must_use_attribute_on_fndecl (tree fndecl, + const AST::Attribute &attr) +{ + tree nodiscard = get_identifier ("nodiscard"); + tree value = NULL_TREE; + + if (attr.has_attr_input ()) + { + rust_assert (attr.get_attr_input ().get_attr_input_type () + == AST::AttrInput::AttrInputType::LITERAL); + + auto &literal + = static_cast<AST::AttrInputLiteral &> (attr.get_attr_input ()); + const auto &msg_str = literal.get_literal ().as_string (); + tree message = build_string (msg_str.size (), msg_str.c_str ()); + + value = tree_cons (nodiscard, message, NULL_TREE); + } + + DECL_ATTRIBUTES (fndecl) + = tree_cons (nodiscard, value, DECL_ATTRIBUTES (fndecl)); +} + +void HIRCompileBase::setup_abi_options (tree fndecl, ABI abi) { switch (abi) @@ -262,9 +294,8 @@ HIRCompileBase::compile_function_body (Context *ctx, tree fndecl, auto compiled_expr = CompileStmt::Compile (s.get (), ctx); if (compiled_expr != nullptr) { - tree compiled_stmt - = ctx->get_backend ()->expression_statement (fndecl, compiled_expr); - ctx->add_statement (compiled_stmt); + tree s = convert_to_void (compiled_expr, ICV_STATEMENT); + ctx->add_statement (s); } } @@ -289,10 +320,8 @@ HIRCompileBase::compile_function_body (Context *ctx, tree fndecl, } else { - tree final_stmt - = ctx->get_backend ()->expression_statement (fndecl, - compiled_expr); - ctx->add_statement (final_stmt); + // FIXME can this actually happen? + ctx->add_statement (compiled_expr); } } } |