Skip to content

Commit dd3de3f

Browse files
0xn4utilusphilberty
authored andcommitted
ast: collector: visit InlineAsm node during ast dump
gcc/rust/ChangeLog: * ast/rust-ast-collector.cc (TokenCollector::visit): Implement for InlineAsm. * ast/rust-ast-full-decls.h (enum class): Move InlineAsmOption enum inside InlineAsm. * ast/rust-expr.h (enum class): Likewise. (class InlineAsm): Likewise. * expand/rust-macro-builtins-asm.cc (check_and_set): Likewise. (parse_options): Likewise. * expand/rust-macro-builtins-asm.h (check_and_set): Likewise. * hir/tree/rust-hir-expr.cc (InlineAsm::InlineAsm): Likewise. * hir/tree/rust-hir-expr.h: Likewise. * typecheck/rust-hir-type-check-expr.cc (TypeCheckExpr::visit): Likewise.
1 parent 339415a commit dd3de3f

8 files changed

+143
-47
lines changed

gcc/rust/ast/rust-ast-collector.cc

Lines changed: 82 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1518,7 +1518,88 @@ TokenCollector::visit (AsyncBlockExpr &expr)
15181518

15191519
void
15201520
TokenCollector::visit (InlineAsm &expr)
1521-
{}
1521+
{
1522+
push (Rust::Token::make_identifier (expr.get_locus (), "asm"));
1523+
push (Rust::Token::make (EXCLAM, expr.get_locus ()));
1524+
push (Rust::Token::make (LEFT_PAREN, expr.get_locus ()));
1525+
1526+
for (auto &template_str : expr.get_template_strs ())
1527+
push (Rust::Token::make_string (template_str.get_locus (),
1528+
std::move (template_str.symbol)));
1529+
1530+
push (Rust::Token::make (COLON, expr.get_locus ()));
1531+
1532+
for (auto &operand : expr.get_operands ())
1533+
{
1534+
using RegisterType = AST::InlineAsmOperand::RegisterType;
1535+
switch (operand.get_register_type ())
1536+
{
1537+
case RegisterType::In: {
1538+
visit (operand.get_in ().expr);
1539+
break;
1540+
}
1541+
case RegisterType::Out: {
1542+
visit (operand.get_out ().expr);
1543+
break;
1544+
}
1545+
case RegisterType::InOut: {
1546+
visit (operand.get_in_out ().expr);
1547+
break;
1548+
}
1549+
case RegisterType::SplitInOut: {
1550+
auto split = operand.get_split_in_out ();
1551+
visit (split.in_expr);
1552+
visit (split.out_expr);
1553+
break;
1554+
}
1555+
case RegisterType::Const: {
1556+
visit (operand.get_const ().anon_const.expr);
1557+
break;
1558+
}
1559+
case RegisterType::Sym: {
1560+
visit (operand.get_sym ().expr);
1561+
break;
1562+
}
1563+
case RegisterType::Label: {
1564+
visit (operand.get_label ().expr);
1565+
break;
1566+
}
1567+
}
1568+
push (Rust::Token::make (COMMA, expr.get_locus ()));
1569+
}
1570+
push (Rust::Token::make (COLON, expr.get_locus ()));
1571+
1572+
for (auto &clobber : expr.get_clobber_abi ())
1573+
{
1574+
push (Rust::Token::make_string (expr.get_locus (),
1575+
std::move (clobber.symbol)));
1576+
push (Rust::Token::make (COMMA, expr.get_locus ()));
1577+
}
1578+
push (Rust::Token::make (COLON, expr.get_locus ()));
1579+
1580+
for (auto it = expr.named_args.begin (); it != expr.named_args.end (); ++it)
1581+
{
1582+
auto &arg = *it;
1583+
push (
1584+
Rust::Token::make_identifier (expr.get_locus (), arg.first.c_str ()));
1585+
push (Rust::Token::make (EQUAL, expr.get_locus ()));
1586+
push (Rust::Token::make_identifier (expr.get_locus (),
1587+
std::to_string (arg.second)));
1588+
1589+
push (Rust::Token::make (COMMA, expr.get_locus ()));
1590+
}
1591+
1592+
push (Rust::Token::make (COLON, expr.get_locus ()));
1593+
1594+
for (auto &option : expr.get_options ())
1595+
{
1596+
push (Rust::Token::make_identifier (
1597+
expr.get_locus (), InlineAsm::option_to_string (option).c_str ()));
1598+
push (Rust::Token::make (COMMA, expr.get_locus ()));
1599+
}
1600+
1601+
push (Rust::Token::make (RIGHT_PAREN, expr.get_locus ()));
1602+
}
15221603

15231604
void
15241605
TokenCollector::visit (LlvmInlineAsm &expr)

gcc/rust/ast/rust-ast-full-decls.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,6 @@ struct MatchCase;
145145
class MatchExpr;
146146
class AwaitExpr;
147147
class AsyncBlockExpr;
148-
enum class InlineAsmOption;
149148
struct AnonConst;
150149
struct InlineAsmRegOrRegClass;
151150
class InlineAsmOperand;

gcc/rust/ast/rust-expr.h

Lines changed: 43 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4822,20 +4822,6 @@ class AsyncBlockExpr : public ExprWithBlock
48224822
}
48234823
};
48244824

4825-
// Inline-assembly specific options
4826-
enum class InlineAsmOption
4827-
{
4828-
PURE = 1 << 0,
4829-
NOMEM = 1 << 1,
4830-
READONLY = 1 << 2,
4831-
PRESERVES_FLAGS = 1 << 3,
4832-
NORETURN = 1 << 4,
4833-
NOSTACK = 1 << 5,
4834-
ATT_SYNTAX = 1 << 6,
4835-
RAW = 1 << 7,
4836-
MAY_UNWIND = 1 << 8,
4837-
};
4838-
48394825
struct AnonConst
48404826
{
48414827
NodeId id;
@@ -5288,6 +5274,20 @@ struct TupleTemplateStr
52885274
// Inline Assembly Node
52895275
class InlineAsm : public ExprWithoutBlock
52905276
{
5277+
public:
5278+
enum class Option
5279+
{
5280+
PURE = 1 << 0,
5281+
NOMEM = 1 << 1,
5282+
READONLY = 1 << 2,
5283+
PRESERVES_FLAGS = 1 << 3,
5284+
NORETURN = 1 << 4,
5285+
NOSTACK = 1 << 5,
5286+
ATT_SYNTAX = 1 << 6,
5287+
RAW = 1 << 7,
5288+
MAY_UNWIND = 1 << 8,
5289+
};
5290+
52915291
private:
52925292
location_t locus;
52935293
// TODO: Not sure how outer_attrs plays with InlineAsm, I put it here in order
@@ -5311,7 +5311,7 @@ class InlineAsm : public ExprWithoutBlock
53115311
std::map<std::string, int> named_args;
53125312
std::set<int> reg_args;
53135313
std::vector<TupleClobber> clobber_abi;
5314-
std::set<InlineAsmOption> options;
5314+
std::set<InlineAsm::Option> options;
53155315

53165316
std::vector<location_t> line_spans;
53175317

@@ -5342,14 +5342,41 @@ class InlineAsm : public ExprWithoutBlock
53425342

53435343
std::vector<TupleClobber> get_clobber_abi () { return clobber_abi; }
53445344

5345-
std::set<InlineAsmOption> get_options () { return options; }
5345+
std::set<InlineAsm::Option> get_options () { return options; }
53465346

53475347
InlineAsm *clone_expr_without_block_impl () const override
53485348
{
53495349
return new InlineAsm (*this);
53505350
}
53515351

53525352
Expr::Kind get_expr_kind () const override { return Expr::Kind::InlineAsm; }
5353+
5354+
static std::string option_to_string (Option option)
5355+
{
5356+
switch (option)
5357+
{
5358+
case Option::PURE:
5359+
return "pure";
5360+
case Option::NOMEM:
5361+
return "nomem";
5362+
case Option::READONLY:
5363+
return "readonly";
5364+
case Option::PRESERVES_FLAGS:
5365+
return "preserves_flags";
5366+
case Option::NORETURN:
5367+
return "noreturn";
5368+
case Option::NOSTACK:
5369+
return "nostack";
5370+
case Option::ATT_SYNTAX:
5371+
return "att_syntax";
5372+
case Option::RAW:
5373+
return "raw";
5374+
case Option::MAY_UNWIND:
5375+
return "may_unwind";
5376+
default:
5377+
rust_unreachable ();
5378+
}
5379+
}
53535380
};
53545381

53555382
class LlvmInlineAsm : public ExprWithoutBlock

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

Lines changed: 12 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,6 @@
2525
#include "rust-parse.h"
2626

2727
namespace Rust {
28-
std::map<AST::InlineAsmOption, std::string> InlineAsmOptionMap{
29-
{AST::InlineAsmOption::PURE, "pure"},
30-
{AST::InlineAsmOption::NOMEM, "nomem"},
31-
{AST::InlineAsmOption::READONLY, "readonly"},
32-
{AST::InlineAsmOption::PRESERVES_FLAGS, "preserves_flags"},
33-
{AST::InlineAsmOption::NORETURN, "noreturn"},
34-
{AST::InlineAsmOption::NOSTACK, "nostack"},
35-
{AST::InlineAsmOption::MAY_UNWIND, "may_unwind"},
36-
{AST::InlineAsmOption::ATT_SYNTAX, "att_syntax"},
37-
{AST::InlineAsmOption::RAW, "raw"},
38-
};
39-
4028
std::set<std::string> potentially_nonpromoted_keywords
4129
= {"in", "out", "lateout", "inout", "inlateout", "const", "sym", "label"};
4230

@@ -500,7 +488,7 @@ parse_reg_operand_unexpected (InlineAsmContext inline_asm_ctx)
500488
}
501489

502490
void
503-
check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsmOption option)
491+
check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsm::Option option)
504492
{
505493
auto &parser = inline_asm_ctx.parser;
506494
auto &inline_asm = inline_asm_ctx.inline_asm;
@@ -509,7 +497,7 @@ check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsmOption option)
509497
// TODO: report an error of duplication
510498
rust_error_at (parser.peek_current_token ()->get_locus (),
511499
"the %qs option was already provided",
512-
InlineAsmOptionMap[option].c_str ());
500+
AST::InlineAsm::option_to_string (option).c_str ());
513501
return;
514502
}
515503
else
@@ -536,39 +524,40 @@ parse_options (InlineAsmContext &inline_asm_ctx)
536524
{
537525
if (!is_global_asm && check_identifier (parser, "pure"))
538526
{
539-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::PURE);
527+
check_and_set (inline_asm_ctx, AST::InlineAsm::Option::PURE);
540528
}
541529
else if (!is_global_asm && check_identifier (parser, "nomem"))
542530
{
543-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::NOMEM);
531+
check_and_set (inline_asm_ctx, AST::InlineAsm::Option::NOMEM);
544532
}
545533
else if (!is_global_asm && check_identifier (parser, "readonly"))
546534
{
547-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::READONLY);
535+
check_and_set (inline_asm_ctx, AST::InlineAsm::Option::READONLY);
548536
}
549537
else if (!is_global_asm && check_identifier (parser, "preserves_flags"))
550538
{
551-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::PRESERVES_FLAGS);
539+
check_and_set (inline_asm_ctx,
540+
AST::InlineAsm::Option::PRESERVES_FLAGS);
552541
}
553542
else if (!is_global_asm && check_identifier (parser, "noreturn"))
554543
{
555-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::NORETURN);
544+
check_and_set (inline_asm_ctx, AST::InlineAsm::Option::NORETURN);
556545
}
557546
else if (!is_global_asm && check_identifier (parser, "nostack"))
558547
{
559-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::NOSTACK);
548+
check_and_set (inline_asm_ctx, AST::InlineAsm::Option::NOSTACK);
560549
}
561550
else if (!is_global_asm && check_identifier (parser, "may_unwind"))
562551
{
563-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::MAY_UNWIND);
552+
check_and_set (inline_asm_ctx, AST::InlineAsm::Option::MAY_UNWIND);
564553
}
565554
else if (check_identifier (parser, "att_syntax"))
566555
{
567-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::ATT_SYNTAX);
556+
check_and_set (inline_asm_ctx, AST::InlineAsm::Option::ATT_SYNTAX);
568557
}
569558
else if (check_identifier (parser, "raw"))
570559
{
571-
check_and_set (inline_asm_ctx, AST::InlineAsmOption::RAW);
560+
check_and_set (inline_asm_ctx, AST::InlineAsm::Option::RAW);
572561
}
573562
else
574563
{

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ bool
151151
check_identifier (Parser<MacroInvocLexer> &parser, std::string ident);
152152

153153
void
154-
check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsmOption option);
154+
check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsm::Option option);
155155

156156
// From rustc
157157
WARN_UNUSED_RESULT

gcc/rust/hir/tree/rust-hir-expr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1476,7 +1476,7 @@ InlineAsm::InlineAsm (location_t locus, bool is_global_asm,
14761476
std::vector<AST::TupleTemplateStr> template_strs,
14771477
std::vector<HIR::InlineAsmOperand> operands,
14781478
std::vector<AST::TupleClobber> clobber_abi,
1479-
std::set<AST::InlineAsmOption> options,
1479+
std::set<AST::InlineAsm::Option> options,
14801480
Analysis::NodeMapping mappings,
14811481
AST::AttrVec outer_attribs)
14821482
: ExprWithoutBlock (std::move (mappings), std::move (outer_attribs)),

gcc/rust/hir/tree/rust-hir-expr.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3059,7 +3059,7 @@ class InlineAsm : public ExprWithoutBlock
30593059
std::vector<AST::TupleTemplateStr> template_strs;
30603060
std::vector<HIR::InlineAsmOperand> operands;
30613061
std::vector<AST::TupleClobber> clobber_abi;
3062-
std::set<AST::InlineAsmOption> options;
3062+
std::set<AST::InlineAsm::Option> options;
30633063

30643064
std::vector<location_t> line_spans;
30653065

@@ -3094,7 +3094,7 @@ class InlineAsm : public ExprWithoutBlock
30943094

30953095
std::vector<AST::TupleClobber> get_clobber_abi () { return clobber_abi; }
30963096

3097-
std::set<AST::InlineAsmOption> get_options () { return options; }
3097+
std::set<AST::InlineAsm::Option> get_options () { return options; }
30983098

30993099
bool is_simple_asm ()
31003100
{
@@ -3113,7 +3113,7 @@ class InlineAsm : public ExprWithoutBlock
31133113
std::vector<AST::TupleTemplateStr> template_strs,
31143114
std::vector<HIR::InlineAsmOperand> operands,
31153115
std::vector<AST::TupleClobber> clobber_abi,
3116-
std::set<AST::InlineAsmOption> options,
3116+
std::set<AST::InlineAsm::Option> options,
31173117
Analysis::NodeMapping mappings,
31183118
AST::AttrVec outer_attribs = AST::AttrVec ());
31193119
};

gcc/rust/typecheck/rust-hir-type-check-expr.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -837,7 +837,7 @@ TypeCheckExpr::visit (HIR::InlineAsm &expr)
837837
// NOTE: Hoise out if we have noreturn as an option
838838
// to return a never type
839839
// TODO : new keyword for memory seems sooooo shaky
840-
if (expr.options.count (AST::InlineAsmOption::NORETURN) == 1)
840+
if (expr.options.count (AST::InlineAsm::Option::NORETURN) == 1)
841841
infered = new TyTy::NeverType (expr.get_mappings ().get_hirid ());
842842
else
843843
infered = TyTy::TupleType::get_unit_type ();

0 commit comments

Comments
 (0)