Skip to content

Commit a395a81

Browse files
badumbatishP-E-P
authored andcommitted
Use new constructors and control flow for operand
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
1 parent 96a8914 commit a395a81

File tree

2 files changed

+53
-84
lines changed

2 files changed

+53
-84
lines changed

gcc/rust/ast/rust-expr.h

+38-60
Original file line numberDiff line numberDiff line change
@@ -4768,8 +4768,9 @@ struct InlineAsmRegOrRegClass
47684768
location_t locus;
47694769
};
47704770

4771-
struct InlineAsmOperand
4771+
class InlineAsmOperand
47724772
{
4773+
public:
47734774
enum RegisterType
47744775
{
47754776
In,
@@ -4952,75 +4953,52 @@ struct InlineAsmOperand
49524953
}
49534954
};
49544955

4955-
RegisterType register_type;
4956-
4957-
tl::optional<struct In> in;
4958-
tl::optional<struct Out> out;
4959-
tl::optional<struct InOut> in_out;
4960-
tl::optional<struct SplitInOut> split_in_out;
4961-
tl::optional<struct Const> cnst;
4962-
tl::optional<struct Sym> sym;
4963-
tl::optional<struct Label> label;
4964-
4965-
InlineAsmOperand () {}
49664956
InlineAsmOperand (const InlineAsmOperand &other)
49674957
: register_type (other.register_type), in (other.in), out (other.out),
49684958
in_out (other.in_out), split_in_out (other.split_in_out),
49694959
cnst (other.cnst), sym (other.sym)
49704960
{}
49714961

4972-
void set_in (const tl::optional<struct In> &reg)
4973-
{
4974-
this->register_type = In;
4975-
4976-
if (reg.has_value ())
4977-
this->in = reg.value ();
4978-
}
4979-
4980-
void set_out (const tl::optional<struct Out> &reg)
4981-
{
4982-
this->register_type = Out;
4983-
4984-
if (reg.has_value ())
4985-
this->out = reg.value ();
4986-
}
4987-
4988-
void set_in_out (const tl::optional<struct InOut> &reg)
4989-
{
4990-
this->register_type = InOut;
4991-
if (reg.has_value ())
4992-
this->in_out = reg.value ();
4993-
}
4994-
4995-
void set_split_in_out (const tl::optional<struct SplitInOut> &reg)
4996-
{
4997-
this->register_type = SplitInOut;
4998-
if (reg.has_value ())
4999-
this->split_in_out = reg.value ();
5000-
}
5001-
5002-
void set_cnst (const tl::optional<struct Const> &reg)
5003-
{
5004-
this->register_type = Const;
5005-
if (reg.has_value ())
5006-
this->cnst = reg.value ();
5007-
}
4962+
InlineAsmOperand (const struct In &reg) : register_type (In), in (reg) {}
4963+
InlineAsmOperand (const struct Out &reg) : register_type (Out), out (reg) {}
4964+
InlineAsmOperand (const struct InOut &reg)
4965+
: register_type (InOut), in_out (reg)
4966+
{}
4967+
InlineAsmOperand (const struct SplitInOut &reg)
4968+
: register_type (SplitInOut), split_in_out (reg)
4969+
{}
4970+
InlineAsmOperand (const struct Const &reg) : register_type (Const), cnst (reg)
4971+
{}
4972+
InlineAsmOperand (const struct Sym &reg) : register_type (Sym), sym (reg) {}
4973+
InlineAsmOperand (const struct Label &reg)
4974+
: register_type (Label), label (reg)
4975+
{}
50084976

5009-
void set_sym (const tl::optional<struct Sym> &reg)
5010-
{
5011-
this->register_type = Sym;
5012-
if (reg.has_value ())
5013-
this->sym = reg.value ();
5014-
}
4977+
location_t get_locus () const { return locus; }
4978+
RegisterType get_register_type () const { return register_type; }
4979+
4980+
// Potentially fail immediately if you don't use get_register_type() to
4981+
// inspect the RegisterType first before calling the following functions Check
4982+
// first
4983+
struct In get_in () const { return in.value (); }
4984+
struct Out get_out () const { return out.value (); }
4985+
struct InOut get_in_out () const { return in_out.value (); }
4986+
struct SplitInOut get_split_in_out () const { return split_in_out.value (); }
4987+
struct Const get_const () const { return cnst.value (); }
4988+
struct Sym get_sym () const { return sym.value (); }
4989+
struct Label get_label () const { return label.value (); }
50154990

5016-
void set_label (const tl::optional<struct Label> &reg)
5017-
{
5018-
this->register_type = Label;
5019-
if (reg.has_value ())
5020-
this->label = reg.value ();
5021-
}
4991+
private:
4992+
RegisterType register_type;
50224993

50234994
location_t locus;
4995+
tl::optional<struct In> in;
4996+
tl::optional<struct Out> out;
4997+
tl::optional<struct InOut> in_out;
4998+
tl::optional<struct SplitInOut> split_in_out;
4999+
tl::optional<struct Const> cnst;
5000+
tl::optional<struct Sym> sym;
5001+
tl::optional<struct Label> label;
50245002
};
50255003

50265004
struct InlineAsmPlaceHolder

gcc/rust/expand/rust-macro-builtins-asm.cc

+15-24
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,6 @@ parse_reg_operand (InlineAsmContext inline_asm_ctx)
193193
// None
194194
// };
195195
auto &parser = inline_asm_ctx.parser;
196-
AST::InlineAsmOperand reg_operand;
197196
auto token = parser.peek_current_token ();
198197
auto iden_token = parser.peek_current_token ();
199198

@@ -243,12 +242,13 @@ parse_reg_operand (InlineAsmContext inline_asm_ctx)
243242
inline_asm_ctx = *result;
244243
break;
245244
}
246-
else if (result.error () == COMMITTED
247-
&& parse_func != parse_reg_operand_unexpected)
248-
return result;
249-
else if (result.error () == COMMITTED
250-
&& parse_func == parse_reg_operand_unexpected)
251-
return inline_asm_ctx;
245+
else if (result.error () == COMMITTED)
246+
{
247+
if (parse_func == parse_reg_operand_unexpected)
248+
return inline_asm_ctx;
249+
else
250+
return result;
251+
}
252252
}
253253

254254
auto &inline_asm = inline_asm_ctx.inline_asm;
@@ -297,7 +297,6 @@ parse_reg_operand_in (InlineAsmContext inline_asm_ctx)
297297
{
298298
// For the keyword IN, currently we count it as a seperate keyword called
299299
// Rust::IN search for #define RS_TOKEN_LIST in code base.
300-
AST::InlineAsmOperand reg_operand;
301300
auto &parser = inline_asm_ctx.parser;
302301
if (!inline_asm_ctx.is_global_asm () && parser.skip_token (IN))
303302
{
@@ -316,8 +315,7 @@ parse_reg_operand_in (InlineAsmContext inline_asm_ctx)
316315
// TODO: When we've succesfully parse an expr, remember to clone_expr()
317316
// instead of nullptr
318317
// struct AST::InlineAsmOperand::In in (reg, nullptr);
319-
// reg_operand.set_in (in);
320-
// inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
318+
// inline_asm_ctx.inline_asm.operands.push_back (in);
321319
return inline_asm_ctx;
322320
}
323321
return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
@@ -327,7 +325,6 @@ tl::expected<InlineAsmContext, InlineAsmParseError>
327325
parse_reg_operand_out (InlineAsmContext inline_asm_ctx)
328326
{
329327
auto &parser = inline_asm_ctx.parser;
330-
AST::InlineAsmOperand reg_operand;
331328
if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "out"))
332329
{
333330
auto reg = parse_reg (inline_asm_ctx);
@@ -342,8 +339,7 @@ parse_reg_operand_out (InlineAsmContext inline_asm_ctx)
342339
// instead of nullptr
343340
struct AST::InlineAsmOperand::Out out (reg, false, std::move (expr));
344341

345-
reg_operand.set_out (out);
346-
inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
342+
inline_asm_ctx.inline_asm.operands.push_back (out);
347343

348344
return inline_asm_ctx;
349345
}
@@ -373,7 +369,6 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx)
373369
auto &parser = inline_asm_ctx.parser;
374370
auto token = parser.peek_current_token ();
375371

376-
AST::InlineAsmOperand reg_operand;
377372
if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "inout"))
378373
{
379374
auto reg = parse_reg (inline_asm_ctx);
@@ -391,8 +386,7 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx)
391386
// TODO: Is error propogation our top priority, the ? in rust's asm.rs is
392387
// doing a lot of work.
393388
// TODO: Not sure how to use parse_expr
394-
auto exist_ident1 = check_identifier (parser, "");
395-
if (!exist_ident1)
389+
if (!check_identifier (parser, ""))
396390
rust_unreachable ();
397391
// auto expr = parse_format_string (inline_asm_ctx);
398392

@@ -402,10 +396,9 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx)
402396
{
403397
if (!parser.skip_token (UNDERSCORE))
404398
{
405-
auto exist_ident2 = check_identifier (parser, "");
406399
// auto result = parse_format_string (inline_asm_ctx);
407400

408-
if (!exist_ident2)
401+
if (!check_identifier (parser, ""))
409402
rust_unreachable ();
410403
// out_expr = parser.parse_expr();
411404
}
@@ -416,8 +409,8 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx)
416409
// expr, out_expr, late: false }
417410
// struct AST::InlineAsmOperand::SplitInOut split_in_out (reg,
418411
// false, nullptr,
419-
// nullptr); reg_operand.set_split_in_out (split_in_out);
420-
// inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
412+
// nullptr);
413+
// inline_asm_ctx.inline_asm.operands.push_back (split_in_out);
421414

422415
return inline_asm_ctx;
423416
}
@@ -427,8 +420,8 @@ parse_reg_operand_inout (InlineAsmContext inline_asm_ctx)
427420
// RUST VERSION: ast::InlineAsmOperand::InOut { reg, expr, late: false
428421
// }
429422
// struct AST::InlineAsmOperand::InOut inout (reg, false,
430-
// nullptr); reg_operand.set_in_out (inout);
431-
// inline_asm_ctx.inline_asm.operands.push_back (reg_operand);
423+
// nullptr);
424+
// inline_asm_ctx.inline_asm.operands.push_back (inout);
432425
return inline_asm_ctx;
433426
}
434427
}
@@ -440,12 +433,10 @@ tl::expected<InlineAsmContext, InlineAsmParseError>
440433
parse_reg_operand_const (InlineAsmContext inline_asm_ctx)
441434
{
442435
auto &parser = inline_asm_ctx.parser;
443-
AST::InlineAsmOperand reg_operand;
444436
if (parser.peek_current_token ()->get_id () == CONST)
445437
{
446438
// TODO: Please handle const with parse_expr instead.
447439
auto anon_const = parse_format_string (inline_asm_ctx);
448-
reg_operand.set_cnst (tl::nullopt);
449440
rust_unreachable ();
450441
return tl::unexpected<InlineAsmParseError> (COMMITTED);
451442
}

0 commit comments

Comments
 (0)