From: badumbatish <tanghocle...@gmail.com>

gcc/rust/ChangeLog:

        * ast/rust-expr.h (struct InlineAsmOperand): changed to class
        (class InlineAsmOperand): Have appropriate constructor,
        and getter
        * expand/rust-macro-builtins-asm.cc (parse_reg_operand):
        Use the new implement constructors and new control flow.
        (parse_reg_operand_in): Likewise
        (parse_reg_operand_out): Likewise
        (parse_reg_operand_inout): Likewise
        (parse_reg_operand_const): Likewise
---
 gcc/rust/ast/rust-expr.h                   | 98 +++++++++-------------
 gcc/rust/expand/rust-macro-builtins-asm.cc | 39 ++++-----
 2 files changed, 53 insertions(+), 84 deletions(-)

diff --git a/gcc/rust/ast/rust-expr.h b/gcc/rust/ast/rust-expr.h
index 8a3baf7ca2b..749fdc05f2b 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<struct In> in;
-  tl::optional<struct Out> out;
-  tl::optional<struct InOut> in_out;
-  tl::optional<struct SplitInOut> split_in_out;
-  tl::optional<struct Const> cnst;
-  tl::optional<struct Sym> sym;
-  tl::optional<struct Label> 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<struct In> &reg)
-  {
-    this->register_type = In;
-
-    if (reg.has_value ())
-      this->in = reg.value ();
-  }
-
-  void set_out (const tl::optional<struct Out> &reg)
-  {
-    this->register_type = Out;
-
-    if (reg.has_value ())
-      this->out = reg.value ();
-  }
-
-  void set_in_out (const tl::optional<struct InOut> &reg)
-  {
-    this->register_type = InOut;
-    if (reg.has_value ())
-      this->in_out = reg.value ();
-  }
-
-  void set_split_in_out (const tl::optional<struct SplitInOut> &reg)
-  {
-    this->register_type = SplitInOut;
-    if (reg.has_value ())
-      this->split_in_out = reg.value ();
-  }
-
-  void set_cnst (const tl::optional<struct Const> &reg)
-  {
-    this->register_type = Const;
-    if (reg.has_value ())
-      this->cnst = reg.value ();
-  }
+  InlineAsmOperand (const struct In &reg) : register_type (In), in (reg) {}
+  InlineAsmOperand (const struct Out &reg) : register_type (Out), out (reg) {}
+  InlineAsmOperand (const struct InOut &reg)
+    : register_type (InOut), in_out (reg)
+  {}
+  InlineAsmOperand (const struct SplitInOut &reg)
+    : register_type (SplitInOut), split_in_out (reg)
+  {}
+  InlineAsmOperand (const struct Const &reg) : register_type (Const), cnst 
(reg)
+  {}
+  InlineAsmOperand (const struct Sym &reg) : register_type (Sym), sym (reg) {}
+  InlineAsmOperand (const struct Label &reg)
+    : register_type (Label), label (reg)
+  {}
 
-  void set_sym (const tl::optional<struct Sym> &reg)
-  {
-    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<struct Label> &reg)
-  {
-    this->register_type = Label;
-    if (reg.has_value ())
-      this->label = reg.value ();
-  }
+private:
+  RegisterType register_type;
 
   location_t locus;
+  tl::optional<struct In> in;
+  tl::optional<struct Out> out;
+  tl::optional<struct InOut> in_out;
+  tl::optional<struct SplitInOut> split_in_out;
+  tl::optional<struct Const> cnst;
+  tl::optional<struct Sym> sym;
+  tl::optional<struct Label> label;
 };
 
 struct InlineAsmPlaceHolder
diff --git a/gcc/rust/expand/rust-macro-builtins-asm.cc 
b/gcc/rust/expand/rust-macro-builtins-asm.cc
index d270621d1bf..7f0498fdef4 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<InlineAsmParseError> (NONCOMMITED);
@@ -327,7 +325,6 @@ tl::expected<InlineAsmContext, InlineAsmParseError>
 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<InlineAsmContext, InlineAsmParseError>
 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<InlineAsmParseError> (COMMITTED);
     }
-- 
2.45.2

Reply via email to