diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h index 8a3baf7ca2b4..749fdc05f2ba 100644 --- a/gcc/rust/ast/rust-expr.h +++ b/gcc/rust/ast/rust-expr.h @@ -4768,8 +4768,9 @@ struct InlineAsmRegOrRegClass location_t locus; }; -struct InlineAsmOperand +class InlineAsmOperand { +public: enum RegisterType { In, @@ -4952,75 +4953,52 @@ struct InlineAsmOperand } }; - RegisterType register_type; - - tl::optional in; - tl::optional out; - tl::optional in_out; - tl::optional split_in_out; - tl::optional cnst; - tl::optional sym; - tl::optional label; - - InlineAsmOperand () {} InlineAsmOperand (const InlineAsmOperand &other) : register_type (other.register_type), in (other.in), out (other.out), in_out (other.in_out), split_in_out (other.split_in_out), cnst (other.cnst), sym (other.sym) {} - void set_in (const tl::optional ®) - { - this->register_type = In; - - if (reg.has_value ()) - this->in = reg.value (); - } - - void set_out (const tl::optional ®) - { - this->register_type = Out; - - if (reg.has_value ()) - this->out = reg.value (); - } - - void set_in_out (const tl::optional ®) - { - this->register_type = InOut; - if (reg.has_value ()) - this->in_out = reg.value (); - } - - void set_split_in_out (const tl::optional ®) - { - this->register_type = SplitInOut; - if (reg.has_value ()) - this->split_in_out = reg.value (); - } - - void set_cnst (const tl::optional ®) - { - this->register_type = Const; - if (reg.has_value ()) - this->cnst = reg.value (); - } + InlineAsmOperand (const struct In ®) : register_type (In), in (reg) {} + InlineAsmOperand (const struct Out ®) : register_type (Out), out (reg) {} + InlineAsmOperand (const struct InOut ®) + : register_type (InOut), in_out (reg) + {} + InlineAsmOperand (const struct SplitInOut ®) + : register_type (SplitInOut), split_in_out (reg) + {} + InlineAsmOperand (const struct Const ®) : register_type (Const), cnst (reg) + {} + InlineAsmOperand (const struct Sym ®) : register_type (Sym), sym (reg) {} + InlineAsmOperand (const struct Label ®) + : register_type (Label), label (reg) + {} - void set_sym (const tl::optional ®) - { - this->register_type = Sym; - if (reg.has_value ()) - this->sym = reg.value (); - } + location_t get_locus () const { return locus; } + RegisterType get_register_type () const { return register_type; } + + // Potentially fail immediately if you don't use get_register_type() to + // inspect the RegisterType first before calling the following functions Check + // first + struct In get_in () const { return in.value (); } + struct Out get_out () const { return out.value (); } + struct InOut get_in_out () const { return in_out.value (); } + struct SplitInOut get_split_in_out () const { return split_in_out.value (); } + struct Const get_const () const { return cnst.value (); } + struct Sym get_sym () const { return sym.value (); } + struct Label get_label () const { return label.value (); } - void set_label (const tl::optional ®) - { - this->register_type = Label; - if (reg.has_value ()) - this->label = reg.value (); - } +private: + RegisterType register_type; location_t locus; + tl::optional in; + tl::optional out; + tl::optional in_out; + tl::optional split_in_out; + tl::optional cnst; + tl::optional sym; + tl::optional label; }; struct InlineAsmPlaceHolder diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc b/gcc/rust/expand/rust-macro-builtins-asm.cc index e69845c5883a..d809eaaf4b41 100644 --- a/gcc/rust/expand/rust-macro-builtins-asm.cc +++ b/gcc/rust/expand/rust-macro-builtins-asm.cc @@ -193,7 +193,6 @@ parse_reg_operand (InlineAsmContext inline_asm_ctx) // None // }; auto &parser = inline_asm_ctx.parser; - AST::InlineAsmOperand reg_operand; auto token = parser.peek_current_token (); auto iden_token = parser.peek_current_token (); @@ -243,12 +242,13 @@ parse_reg_operand (InlineAsmContext inline_asm_ctx) inline_asm_ctx = *result; break; } - else if (result.error () == COMMITTED - && parse_func != parse_reg_operand_unexpected) - return result; - else if (result.error () == COMMITTED - && parse_func == parse_reg_operand_unexpected) - return inline_asm_ctx; + else if (result.error () == COMMITTED) + { + if (parse_func == parse_reg_operand_unexpected) + return inline_asm_ctx; + else + return result; + } } auto &inline_asm = inline_asm_ctx.inline_asm; @@ -297,7 +297,6 @@ parse_reg_operand_in (InlineAsmContext inline_asm_ctx) { // For the keyword IN, currently we count it as a seperate keyword called // Rust::IN search for #define RS_TOKEN_LIST in code base. - AST::InlineAsmOperand reg_operand; auto &parser = inline_asm_ctx.parser; if (!inline_asm_ctx.is_global_asm () && parser.skip_token (IN)) { @@ -316,8 +315,7 @@ parse_reg_operand_in (InlineAsmContext inline_asm_ctx) // TODO: When we've succesfully parse an expr, remember to clone_expr() // instead of nullptr // struct AST::InlineAsmOperand::In in (reg, nullptr); - // reg_operand.set_in (in); - // inline_asm_ctx.inline_asm.operands.push_back (reg_operand); + // inline_asm_ctx.inline_asm.operands.push_back (in); return inline_asm_ctx; } return tl::unexpected (NONCOMMITED); @@ -327,7 +325,6 @@ tl::expected parse_reg_operand_out (InlineAsmContext inline_asm_ctx) { auto &parser = inline_asm_ctx.parser; - AST::InlineAsmOperand reg_operand; if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "out")) { auto reg = parse_reg (inline_asm_ctx); @@ -342,8 +339,7 @@ parse_reg_operand_out (InlineAsmContext inline_asm_ctx) // instead of nullptr struct AST::InlineAsmOperand::Out out (reg, false, std::move (expr)); - reg_operand.set_out (out); - inline_asm_ctx.inline_asm.operands.push_back (reg_operand); + inline_asm_ctx.inline_asm.operands.push_back (out); return inline_asm_ctx; } @@ -373,7 +369,6 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx) auto &parser = inline_asm_ctx.parser; auto token = parser.peek_current_token (); - AST::InlineAsmOperand reg_operand; if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "inout")) { auto reg = parse_reg (inline_asm_ctx); @@ -391,8 +386,7 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx) // TODO: Is error propogation our top priority, the ? in rust's asm.rs is // doing a lot of work. // TODO: Not sure how to use parse_expr - auto exist_ident1 = check_identifier (parser, ""); - if (!exist_ident1) + if (!check_identifier (parser, "")) rust_unreachable (); // auto expr = parse_format_string (inline_asm_ctx); @@ -402,10 +396,9 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx) { if (!parser.skip_token (UNDERSCORE)) { - auto exist_ident2 = check_identifier (parser, ""); // auto result = parse_format_string (inline_asm_ctx); - if (!exist_ident2) + if (!check_identifier (parser, "")) rust_unreachable (); // out_expr = parser.parse_expr(); } @@ -416,8 +409,8 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx) // expr, out_expr, late: false } // struct AST::InlineAsmOperand::SplitInOut split_in_out (reg, // false, nullptr, - // nullptr); reg_operand.set_split_in_out (split_in_out); - // inline_asm_ctx.inline_asm.operands.push_back (reg_operand); + // nullptr); + // inline_asm_ctx.inline_asm.operands.push_back (split_in_out); return inline_asm_ctx; } @@ -427,8 +420,8 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx) // RUST VERSION: ast::InlineAsmOperand::InOut { reg, expr, late: false // } // struct AST::InlineAsmOperand::InOut inout (reg, false, - // nullptr); reg_operand.set_in_out (inout); - // inline_asm_ctx.inline_asm.operands.push_back (reg_operand); + // nullptr); + // inline_asm_ctx.inline_asm.operands.push_back (inout); return inline_asm_ctx; } } @@ -440,12 +433,10 @@ tl::expected parse_reg_operand_const (InlineAsmContext inline_asm_ctx) { auto &parser = inline_asm_ctx.parser; - AST::InlineAsmOperand reg_operand; if (parser.peek_current_token ()->get_id () == CONST) { // TODO: Please handle const with parse_expr instead. auto anon_const = parse_format_string (inline_asm_ctx); - reg_operand.set_cnst (tl::nullopt); rust_unreachable (); return tl::unexpected (COMMITTED); }