Line data Source code
1 : // Copyright (C) 2020-2026 Free Software Foundation, Inc.
2 :
3 : // This file is part of GCC.
4 :
5 : // GCC is free software; you can redistribute it and/or modify it under
6 : // the terms of the GNU General Public License as published by the Free
7 : // Software Foundation; either version 3, or (at your option) any later
8 : // version.
9 :
10 : // GCC is distributed in the hope that it will be useful, but WITHOUT ANY
11 : // WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 : // FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
13 : // for more details.
14 :
15 : // You should have received a copy of the GNU General Public License
16 : // along with GCC; see the file COPYING3. If not see
17 : // <http://www.gnu.org/licenses/>.
18 :
19 : #include "expected.h"
20 : #include "rust-macro-builtins-asm.h"
21 : #include "rust-ast-fragment.h"
22 : #include "rust-ast.h"
23 : #include "rust-fmt.h"
24 : #include "rust-stmt.h"
25 : #include "rust-parse.h"
26 :
27 : namespace Rust {
28 : std::set<std::string> potentially_nonpromoted_keywords
29 : = {"in", "out", "lateout", "inout", "inlateout", "const", "sym", "label"};
30 :
31 : // Helper function strips the beginning and ending double quotes from a
32 : // string.
33 : std::string
34 566 : strip_double_quotes (const std::string &str)
35 : {
36 566 : std::string result = str;
37 :
38 566 : rust_assert (!str.empty ());
39 :
40 566 : rust_assert (str.front () == '\"');
41 566 : rust_assert (str.back () == '\"');
42 :
43 : // we have to special case empty strings which just contain a set of quotes
44 : // so, if the string is "\"\"", just return ""
45 566 : if (result.size () == 2)
46 10 : return "";
47 :
48 556 : rust_assert (result.size () >= 3);
49 :
50 556 : result.erase (0, 1);
51 556 : result.erase (result.size () - 1, 1);
52 :
53 556 : return result;
54 566 : }
55 :
56 : tl::expected<InlineAsmContext, InlineAsmParseError>
57 3 : parse_clobber_abi (InlineAsmContext inline_asm_ctx)
58 : {
59 : // clobber_abi := "clobber_abi(" <abi> *("," <abi>) [","] ")"
60 : // PARSE EVERYTHING COMMITTEDLY IN THIS FUNCTION, WE CONFIRMED VIA clobber_abi
61 : // identifier keyword
62 3 : auto &parser = inline_asm_ctx.parser;
63 3 : auto last_token_id = inline_asm_ctx.last_token_id;
64 3 : auto &inline_asm = inline_asm_ctx.inline_asm;
65 3 : auto token = parser.peek_current_token ();
66 3 : if (!parser.skip_token (LEFT_PAREN))
67 : {
68 2 : token = parser.peek_current_token ();
69 :
70 : // TODO: Error reporting shifted to the left 1 character, I'm not sure
71 : // why.
72 2 : if (token->get_id () == last_token_id)
73 : {
74 1 : rust_error_at (token->get_locus (),
75 : "expected %<(%>, found end of macro arguments");
76 1 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
77 : }
78 : else
79 : {
80 1 : rust_error_at (token->get_locus (), "expected %<(%>, found %qs",
81 : token->get_token_description ());
82 : }
83 1 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
84 : }
85 :
86 1 : if (parser.skip_token (RIGHT_PAREN))
87 : {
88 1 : rust_error_at (
89 1 : parser.peek_current_token ()->get_locus (),
90 : "at least one abi must be provided as an argument to %<clobber_abi%>");
91 1 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
92 : }
93 :
94 0 : std::vector<AST::TupleClobber> new_abis;
95 :
96 0 : token = parser.peek_current_token ();
97 :
98 0 : while (token->get_id () != last_token_id && token->get_id () != RIGHT_PAREN)
99 : {
100 : // Check if it is a string literal or not, codename: <ABI> in ABNF
101 0 : if (token->get_id () == STRING_LITERAL)
102 : {
103 : // TODO: Caring for span in here.
104 0 : new_abis.emplace_back (token->as_string (), token->get_locus ());
105 : }
106 : else
107 : {
108 : // TODO: We encountered something that is not string literal, which
109 : // should be illegal, please emit the correct error
110 : // https://github.com/rust-lang/rust/blob/b92758a9aef1cef7b79e2b72c3d8ba113e547f89/compiler/rustc_builtin_macros/src/asm.rs#L387
111 0 : rust_unreachable ();
112 : }
113 :
114 0 : if (parser.skip_token (RIGHT_PAREN))
115 : {
116 : break;
117 : }
118 :
119 0 : if (!parser.skip_token (COMMA))
120 : {
121 : // TODO: If the skip of comma is unsuccessful, which should be
122 : // illegal, pleaes emit the correct error.
123 0 : rust_unreachable ();
124 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
125 : }
126 :
127 0 : token = parser.peek_current_token ();
128 : }
129 :
130 : // Done processing the local clobber abis, push that to the main Args in
131 : // argument
132 :
133 0 : for (auto abi : new_abis)
134 : {
135 0 : inline_asm.clobber_abi.push_back (abi);
136 0 : }
137 :
138 0 : return inline_asm_ctx;
139 0 : }
140 :
141 : tl::optional<AST::InlineAsmRegOrRegClass>
142 48 : parse_reg (InlineAsmContext &inline_asm_ctx)
143 : {
144 48 : using RegType = AST::InlineAsmRegOrRegClass::Type;
145 48 : auto &parser = inline_asm_ctx.parser;
146 :
147 48 : if (!parser.skip_token (LEFT_PAREN))
148 : {
149 : // TODO: we expect a left parenthesis here, please return the correct
150 : // error.
151 0 : rust_unreachable ();
152 : return tl::nullopt;
153 : }
154 :
155 : // after successful left parenthesis parsing, we should return ast of
156 : // InlineAsmRegOrRegClass of reg or reg class
157 48 : auto token = parser.peek_current_token ();
158 48 : AST::InlineAsmRegOrRegClass reg_class;
159 48 : if (parser.skip_token (IDENTIFIER))
160 : {
161 : // construct a InlineAsmRegOrRegClass
162 47 : reg_class.type = RegType::RegClass;
163 47 : reg_class.reg_class.Symbol = token->as_string ();
164 : }
165 1 : else if (parser.skip_token (STRING_LITERAL))
166 : {
167 : // TODO: there is STRING_LITERAL, and BYTE_STRING_LITERAL, should we check
168 : // for both?
169 :
170 : // construct a InlineAsmRegOrRegClass
171 : // parse_format_string
172 1 : reg_class.type = RegType::Reg;
173 1 : inline_asm_ctx.is_explicit = true;
174 1 : reg_class.reg_class.Symbol = token->as_string ();
175 : }
176 : else
177 : {
178 : // TODO: This should emit error
179 : // return
180 : // Err(p.dcx().create_err(errors::ExpectedRegisterClassOrExplicitRegister
181 : // {
182 : // span: p.token.span,
183 : // }));
184 0 : rust_unreachable ();
185 : }
186 48 : if (!parser.skip_token (RIGHT_PAREN))
187 : {
188 : // TODO: we expect a left parenthesis here, please return the correct
189 : // error.
190 0 : rust_unreachable ();
191 : return tl::nullopt;
192 : }
193 :
194 48 : return reg_class;
195 48 : }
196 :
197 : // From rustc
198 : tl::expected<InlineAsmContext, InlineAsmParseError>
199 80 : parse_reg_operand (InlineAsmContext inline_asm_ctx)
200 : {
201 : // let name = if p.token.is_ident() && p.look_ahead(1, |t| *t == token::Eq) {
202 : // let (ident, _) = p.token.ident().unwrap();
203 : // p.bump();
204 : // p.expect(&token::Eq)?;
205 : // allow_templates = false;
206 : // Some(ident.name)
207 : // } else {
208 : // None
209 : // };
210 80 : auto &parser = inline_asm_ctx.parser;
211 80 : auto token = parser.peek_current_token ();
212 80 : auto iden_token = parser.peek_current_token ();
213 :
214 80 : tl::optional<std::string> name = tl::nullopt;
215 80 : if (check_identifier (parser, ""))
216 : {
217 3 : auto equal_token = parser.peek_current_token ();
218 :
219 3 : if (parser.skip_token (EQUAL))
220 : {
221 3 : name = token->as_string ();
222 : }
223 : else
224 : {
225 0 : rust_error_at (token->get_locus (),
226 : "expected operand, %s, options, or "
227 : "additional template string",
228 : "clobber_abi");
229 0 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
230 : }
231 3 : }
232 :
233 80 : int slot = inline_asm_ctx.inline_asm.operands.size ();
234 :
235 : // Here is all parse_reg_operand functions we're using in a for loop
236 80 : auto parse_funcs = {parse_reg_operand_in, parse_reg_operand_out,
237 : parse_reg_operand_lateout, parse_reg_operand_inout,
238 : parse_reg_operand_const, parse_reg_operand_sym,
239 80 : parse_reg_operand_unexpected};
240 :
241 : // Loop over and execute the parsing functions, if the parser successfullly
242 : // parses or if the parser fails to parse while it has committed to a token,
243 : // we propogate the result.
244 80 : tl::expected<InlineAsmContext, InlineAsmParseError> parsing_operand (
245 80 : inline_asm_ctx);
246 306 : for (auto &parse_func : parse_funcs)
247 : {
248 306 : auto result = parsing_operand.and_then (parse_func);
249 :
250 : // Per rust's asm.rs's structure
251 : // After we've parse successfully, we break out and do a local validation
252 : // of named, positional & explicit register operands
253 :
254 306 : if (result.has_value ())
255 : {
256 48 : inline_asm_ctx = *result;
257 48 : break;
258 : }
259 258 : else if (result.error () == COMMITTED)
260 : {
261 32 : if (parse_func == parse_reg_operand_unexpected)
262 32 : return inline_asm_ctx;
263 : else
264 0 : return result;
265 : }
266 : }
267 :
268 48 : auto &inline_asm = inline_asm_ctx.inline_asm;
269 :
270 48 : token = inline_asm_ctx.parser.peek_current_token ();
271 48 : rust_debug_loc (token->get_locus (), "Here\n");
272 48 : if (inline_asm_ctx.is_explicit)
273 : {
274 1 : if (name != tl::nullopt)
275 : {
276 1 : rust_error_at (token->get_locus (),
277 : "explicit register arguments cannot have names");
278 1 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
279 : }
280 0 : inline_asm.reg_args.insert (slot);
281 : }
282 47 : else if (name != tl::nullopt)
283 : {
284 2 : if (inline_asm.named_args.find (name.value ())
285 2 : != inline_asm.named_args.end ())
286 : {
287 1 : rust_error_at (token->get_locus (), "duplicate argument named %qs",
288 1 : name.value ().c_str ());
289 1 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
290 : }
291 1 : inline_asm.named_args[name.value ()] = slot;
292 : }
293 : else
294 : {
295 45 : if (!inline_asm.named_args.empty () || !inline_asm.reg_args.empty ())
296 : {
297 : // positional arguments cannot follow named arguments or explicit
298 : // register arguments
299 0 : rust_error_at (token->get_locus (),
300 : "positional arguments cannot follow named arguments "
301 : "or explicit register arguments");
302 0 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
303 : }
304 : }
305 :
306 46 : return inline_asm_ctx;
307 160 : }
308 :
309 : tl::expected<InlineAsmContext, InlineAsmParseError>
310 80 : parse_reg_operand_in (InlineAsmContext inline_asm_ctx)
311 : {
312 : // For the keyword IN, currently we count it as a seperate keyword called
313 : // Rust::IN search for #define RS_TOKEN_LIST in code base.
314 80 : auto &parser = inline_asm_ctx.parser;
315 80 : location_t locus = parser.peek_current_token ()->get_locus ();
316 80 : if (!inline_asm_ctx.is_global_asm () && parser.skip_token (IN))
317 : {
318 20 : auto reg = parse_reg (inline_asm_ctx);
319 :
320 20 : if (parser.skip_token (UNDERSCORE))
321 : {
322 : // We are sure to be failing a test here, based on asm.rs
323 : // https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
324 0 : rust_unreachable ();
325 : // return tl::unexpected<InlineAsmParseError> (COMMITTED);
326 : }
327 :
328 20 : auto expr = parser.parse_expr ();
329 20 : rust_assert (expr);
330 :
331 : // TODO: When we've succesfully parse an expr, remember to clone_expr()
332 : // instead of nullptr
333 20 : AST::InlineAsmOperand::In in (reg, std::move (expr.value ()));
334 20 : inline_asm_ctx.inline_asm.operands.emplace_back (in, locus);
335 20 : return inline_asm_ctx;
336 60 : }
337 60 : return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
338 : }
339 :
340 : tl::expected<InlineAsmContext, InlineAsmParseError>
341 60 : parse_reg_operand_out (InlineAsmContext inline_asm_ctx)
342 : {
343 60 : auto &parser = inline_asm_ctx.parser;
344 60 : location_t locus = parser.peek_current_token ()->get_locus ();
345 120 : if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "out"))
346 : {
347 25 : auto reg = parse_reg (inline_asm_ctx);
348 25 : auto expr = parser.parse_expr ();
349 25 : rust_assert (expr);
350 :
351 : /*auto expr_ptr =
352 : std::make_unique<AST::Expr>(AST::LiteralExpr(Literal))*/
353 :
354 : // TODO: When we've succesfully parse an expr, remember to clone_expr()
355 : // instead of nullptr
356 25 : AST::InlineAsmOperand::Out out (reg, false, std::move (expr.value ()));
357 :
358 25 : inline_asm_ctx.inline_asm.operands.emplace_back (out, locus);
359 :
360 25 : return inline_asm_ctx;
361 75 : }
362 :
363 35 : return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
364 : }
365 :
366 : tl::expected<InlineAsmContext, InlineAsmParseError>
367 35 : parse_reg_operand_lateout (InlineAsmContext inline_asm_ctx)
368 : {
369 35 : auto &parser = inline_asm_ctx.parser;
370 35 : auto token = parser.peek_current_token ();
371 70 : if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "lateout"))
372 : {
373 0 : rust_error_at (token->get_locus (),
374 : "The lateout feature is not implemented");
375 0 : rust_unreachable ();
376 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
377 : }
378 :
379 35 : return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
380 35 : }
381 :
382 : tl::expected<InlineAsmContext, InlineAsmParseError>
383 35 : parse_reg_operand_inout (InlineAsmContext inline_asm_ctx)
384 : {
385 35 : auto &parser = inline_asm_ctx.parser;
386 35 : auto token = parser.peek_current_token ();
387 35 : location_t locus = token->get_locus ();
388 :
389 70 : if (!inline_asm_ctx.is_global_asm () && check_identifier (parser, "inout"))
390 : {
391 3 : auto reg = parse_reg (inline_asm_ctx);
392 :
393 3 : if (parser.skip_token (UNDERSCORE))
394 : {
395 : // We are sure to be failing a test here, based on asm.rs
396 : // https://github.com/rust-lang/rust/blob/a330e49593ee890f9197727a3a558b6e6b37f843/compiler/rustc_builtin_macros/src/asm.rs#L112
397 0 : rust_error_at (token->get_locus (),
398 : "The lateout feature is not implemented");
399 0 : rust_unreachable ();
400 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
401 : }
402 :
403 : // TODO: Is error propogation our top priority, the ? in rust's asm.rs is
404 : // doing a lot of work.
405 3 : auto in_expr = parser.parse_expr ();
406 3 : rust_assert (in_expr);
407 :
408 3 : if (parser.skip_token (MATCH_ARROW))
409 : {
410 2 : if (!parser.skip_token (UNDERSCORE))
411 : {
412 : // auto result = parse_format_string (inline_asm_ctx);
413 :
414 2 : auto out_expr = parser.parse_expr ();
415 2 : rust_assert (out_expr);
416 :
417 2 : AST::InlineAsmOperand::SplitInOut splitinout (
418 2 : reg, false, std::move (in_expr.value ()),
419 2 : std::move (out_expr.value ()));
420 :
421 2 : inline_asm_ctx.inline_asm.operands.emplace_back (splitinout,
422 : locus);
423 :
424 2 : return inline_asm_ctx;
425 4 : }
426 :
427 0 : rust_unreachable ();
428 :
429 : // TODO: Rembmer to pass in clone_expr() instead of nullptr
430 : // https://github.com/rust-lang/rust/blob/a3167859f2fd8ff2241295469876a2b687280bdc/compiler/rustc_builtin_macros/src/asm.rs#L135
431 : // RUST VERSION: ast::InlineAsmOperand::SplitInOut { reg, in_expr:
432 : // expr, out_expr, late: false }
433 : // struct AST::InlineAsmOperand::SplitInOut split_in_out (reg,
434 : // false, nullptr,
435 : // nullptr);
436 : // inline_asm_ctx.inline_asm.operands.push_back (split_in_out);
437 :
438 : return inline_asm_ctx;
439 : }
440 : else
441 : {
442 1 : AST::InlineAsmOperand::InOut inout (reg, false,
443 1 : std::move (in_expr.value ()));
444 1 : inline_asm_ctx.inline_asm.operands.emplace_back (inout, locus);
445 : // https://github.com/rust-lang/rust/blob/a3167859f2fd8ff2241295469876a2b687280bdc/compiler/rustc_builtin_macros/src/asm.rs#L137
446 : // RUST VERSION: ast::InlineAsmOperand::InOut { reg, expr, late: false
447 : // }
448 : // struct AST::InlineAsmOperand::InOut inout (reg, false,
449 : // nullptr);
450 : // inline_asm_ctx.inline_asm.operands.push_back (inout);
451 1 : return inline_asm_ctx;
452 1 : }
453 6 : }
454 :
455 32 : return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
456 35 : }
457 :
458 : tl::expected<InlineAsmContext, InlineAsmParseError>
459 32 : parse_reg_operand_const (InlineAsmContext inline_asm_ctx)
460 : {
461 32 : auto &parser = inline_asm_ctx.parser;
462 64 : if (parser.peek_current_token ()->get_id () == CONST)
463 : {
464 : // TODO: Please handle const with parse_expr instead.
465 0 : auto anon_const = parse_format_string (inline_asm_ctx);
466 0 : rust_unreachable ();
467 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
468 : }
469 :
470 32 : return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
471 : }
472 :
473 : tl::expected<InlineAsmContext, InlineAsmParseError>
474 32 : parse_reg_operand_sym (InlineAsmContext inline_asm_ctx)
475 : {
476 32 : auto &parser = inline_asm_ctx.parser;
477 :
478 32 : if (check_identifier (parser, "sym"))
479 : {
480 : // TODO: Please handle sym, which needs ExprKind::Path in Rust's asm.rs
481 0 : rust_unreachable ();
482 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
483 : }
484 32 : return tl::unexpected<InlineAsmParseError> (NONCOMMITED);
485 : }
486 :
487 : tl::expected<InlineAsmContext, InlineAsmParseError>
488 32 : parse_reg_operand_unexpected (InlineAsmContext inline_asm_ctx)
489 : {
490 32 : auto token = inline_asm_ctx.parser.peek_current_token ();
491 : // TODO: It is weird that we can't seem to match any identifier,
492 : // something must be wrong. consult compiler code in asm.rs or rust online
493 : // compiler.
494 : // rust_unreachable ();
495 :
496 : // rust_error_at (token->get_locus (), "ERROR RIGHT HERE");
497 32 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
498 32 : }
499 :
500 : void
501 9 : check_and_set (InlineAsmContext &inline_asm_ctx, AST::InlineAsm::Option option)
502 : {
503 9 : auto &parser = inline_asm_ctx.parser;
504 9 : auto &inline_asm = inline_asm_ctx.inline_asm;
505 9 : if (inline_asm.options.count (option) != 0)
506 : {
507 : // TODO: report an error of duplication
508 4 : rust_error_at (parser.peek_current_token ()->get_locus (),
509 : "the %qs option was already provided",
510 2 : AST::InlineAsm::option_to_string (option).c_str ());
511 2 : return;
512 : }
513 : else
514 : {
515 7 : inline_asm.options.insert (option);
516 : }
517 : }
518 : tl::expected<InlineAsmContext, InlineAsmParseError>
519 7 : parse_options (InlineAsmContext &inline_asm_ctx)
520 : {
521 7 : auto &parser = inline_asm_ctx.parser;
522 7 : bool is_global_asm = inline_asm_ctx.inline_asm.is_global_asm;
523 : // Parse everything commitedly
524 7 : if (!parser.skip_token (LEFT_PAREN))
525 : {
526 1 : auto local_token = parser.peek_current_token ();
527 1 : rust_error_at (local_token->get_locus (), "expected %qs, found %qs", "(",
528 1 : local_token->as_string ().c_str ());
529 1 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
530 1 : }
531 :
532 6 : auto token = parser.peek_current_token ();
533 17 : while (!parser.skip_token (RIGHT_PAREN))
534 : {
535 22 : if (!is_global_asm && check_identifier (parser, "pure"))
536 : {
537 0 : check_and_set (inline_asm_ctx, AST::InlineAsm::Option::PURE);
538 : }
539 22 : else if (!is_global_asm && check_identifier (parser, "nomem"))
540 : {
541 3 : check_and_set (inline_asm_ctx, AST::InlineAsm::Option::NOMEM);
542 : }
543 16 : else if (!is_global_asm && check_identifier (parser, "readonly"))
544 : {
545 0 : check_and_set (inline_asm_ctx, AST::InlineAsm::Option::READONLY);
546 : }
547 16 : else if (!is_global_asm && check_identifier (parser, "preserves_flags"))
548 : {
549 0 : check_and_set (inline_asm_ctx,
550 : AST::InlineAsm::Option::PRESERVES_FLAGS);
551 : }
552 16 : else if (!is_global_asm && check_identifier (parser, "noreturn"))
553 : {
554 3 : check_and_set (inline_asm_ctx, AST::InlineAsm::Option::NORETURN);
555 : }
556 10 : else if (!is_global_asm && check_identifier (parser, "nostack"))
557 : {
558 1 : check_and_set (inline_asm_ctx, AST::InlineAsm::Option::NOSTACK);
559 : }
560 8 : else if (!is_global_asm && check_identifier (parser, "may_unwind"))
561 : {
562 0 : check_and_set (inline_asm_ctx, AST::InlineAsm::Option::MAY_UNWIND);
563 : }
564 4 : else if (check_identifier (parser, "att_syntax"))
565 : {
566 1 : check_and_set (inline_asm_ctx, AST::InlineAsm::Option::ATT_SYNTAX);
567 : }
568 3 : else if (check_identifier (parser, "raw"))
569 : {
570 1 : check_and_set (inline_asm_ctx, AST::InlineAsm::Option::RAW);
571 : }
572 : else
573 : {
574 2 : rust_error_at (token->get_locus (),
575 : "expected one of %qs, %qs, %qs, %qs, %qs, %qs, %qs, "
576 : "%qs, %qs, or %qs, found %qs",
577 : ")", "att_syntax", "may_unwind", "nomem", "noreturn",
578 : "nostack", "preserves_flags", "pure", "raw",
579 2 : "readonly", token->as_string ().c_str ());
580 2 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
581 : }
582 9 : if (parser.skip_token (RIGHT_PAREN))
583 : {
584 : break;
585 : }
586 :
587 : // Parse comma as optional
588 5 : if (parser.skip_token (COMMA))
589 5 : continue;
590 : else
591 : {
592 0 : rust_unreachable ();
593 : token = parser.peek_current_token ();
594 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
595 : }
596 : }
597 :
598 : // TODO: Per rust asm.rs regarding options_spans
599 : // I'm guessing this has to do with some error reporting.
600 : // let new_span = span_start.to(p.prev_token.span);
601 : // args.options_spans.push(new_span);
602 :
603 4 : return inline_asm_ctx;
604 6 : }
605 :
606 : bool
607 481 : check_identifier (Parser<MacroInvocLexer> &p, std::string ident)
608 : {
609 481 : auto token = p.peek_current_token ();
610 :
611 481 : if (token->get_id () == IDENTIFIER)
612 : {
613 194 : auto str = token->as_string ();
614 :
615 : // For non-promoted keywords, we need to also check for them.
616 :
617 194 : if (str == ident)
618 : {
619 47 : p.skip_token ();
620 47 : return true;
621 : }
622 147 : if (ident == "")
623 : {
624 30 : if (potentially_nonpromoted_keywords.find (str)
625 30 : == potentially_nonpromoted_keywords.end ())
626 : {
627 3 : p.skip_token ();
628 3 : return true;
629 : }
630 : return false;
631 : }
632 194 : }
633 :
634 : return false;
635 481 : }
636 :
637 : tl::optional<std::string>
638 88 : parse_format_string (InlineAsmContext &inline_asm_ctx)
639 : {
640 88 : auto &parser = inline_asm_ctx.parser;
641 88 : auto last_token_id = inline_asm_ctx.last_token_id;
642 88 : auto token = parser.peek_current_token ();
643 :
644 88 : if (token->get_id () != last_token_id && token->get_id () == STRING_LITERAL)
645 : {
646 : // very nice, we got a supposedly formatted string.
647 46 : parser.skip_token ();
648 46 : return token->as_string ();
649 : }
650 : else
651 : {
652 42 : return tl::nullopt;
653 : }
654 88 : }
655 :
656 : tl::optional<AST::Fragment>
657 47 : MacroBuiltin::asm_handler (location_t invoc_locus, AST::MacroInvocData &invoc,
658 : AST::InvocKind semicolon, AST::AsmKind is_global_asm)
659 : {
660 47 : return parse_asm (invoc_locus, invoc, semicolon, is_global_asm);
661 : }
662 :
663 : tl::optional<AST::Fragment>
664 106 : MacroBuiltin::llvm_asm_handler (location_t invoc_locus,
665 : AST::MacroInvocData &invoc,
666 : AST::InvocKind semicolon,
667 : AST::AsmKind is_global_asm)
668 : {
669 106 : return parse_llvm_asm (invoc_locus, invoc, semicolon, is_global_asm);
670 : }
671 :
672 : tl::expected<InlineAsmContext, InlineAsmParseError>
673 45 : parse_asm_arg (InlineAsmContext inline_asm_ctx)
674 : {
675 45 : auto &parser = inline_asm_ctx.parser;
676 45 : auto last_token_id = inline_asm_ctx.last_token_id;
677 45 : auto token = parser.peek_current_token ();
678 45 : tl::optional<std::string> fm_string;
679 127 : while (token->get_id () != last_token_id)
680 : {
681 90 : token = parser.peek_current_token ();
682 :
683 90 : if (token->get_id () == COLON || token->get_id () == SCOPE_RESOLUTION)
684 : {
685 0 : rust_error_at (
686 : token->get_locus (),
687 : "the legacy LLVM-style %<asm!%> syntax is no longer supported");
688 8 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
689 : }
690 :
691 : // We accept a comma token here.
692 90 : if (token->get_id () != COMMA
693 90 : && inline_asm_ctx.consumed_comma_without_formatted_string)
694 : {
695 : // if it is not a comma, but we consumed it previously, this is fine
696 : // but we have to set it to false tho.
697 0 : inline_asm_ctx.consumed_comma_without_formatted_string = false;
698 : }
699 90 : else if (token->get_id () == COMMA
700 90 : && !inline_asm_ctx.consumed_comma_without_formatted_string)
701 : {
702 28 : inline_asm_ctx.consumed_comma_without_formatted_string = false;
703 28 : parser.skip_token ();
704 : }
705 62 : else if (token->get_id () == COMMA
706 62 : && inline_asm_ctx.consumed_comma_without_formatted_string)
707 : {
708 : // We consumed comma, and there happens to also be a comma
709 : // error should be: expected expression, found `,`
710 0 : rust_error_at (token->get_locus (), "expected expression, found %qs",
711 : ",");
712 0 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
713 90 : break;
714 : }
715 :
716 : // And if that token comma is also the trailing comma, we break
717 90 : token = parser.peek_current_token ();
718 90 : if (token->get_id () == COMMA && token->get_id () == last_token_id)
719 : {
720 0 : parser.skip_token ();
721 0 : break;
722 : }
723 :
724 : // Ok after the left paren is good, we better be parsing correctly
725 : // everything in here, which is operand in ABNF
726 :
727 : // Parse clobber abi, eat the identifier named "clobber_abi" if true
728 90 : if (check_identifier (parser, "clobber_abi"))
729 : {
730 3 : auto expected = parse_clobber_abi (inline_asm_ctx);
731 3 : if (expected.has_value ())
732 0 : continue;
733 3 : else if (expected.error () == COMMITTED)
734 3 : return expected;
735 :
736 : // The error type is definitely non-committed (we have checked above),
737 : // we are allowed to keep on parsing
738 : }
739 :
740 87 : if (check_identifier (parser, "options"))
741 : {
742 7 : auto expected = parse_options (inline_asm_ctx);
743 7 : if (expected.has_value ())
744 4 : continue;
745 3 : else if (expected.error () == COMMITTED)
746 3 : return expected;
747 :
748 : // The error type is definitely non-committed (we have checked above),
749 : // we are allowed to keep on parsing
750 : }
751 :
752 : // Ok after we have check that neither clobber_abi nor options works, the
753 : // only other logical choice is reg_operand
754 :
755 80 : auto expected = parse_reg_operand (inline_asm_ctx);
756 80 : if (expected.has_value ())
757 78 : continue;
758 2 : else if (expected.error () == COMMITTED)
759 2 : return expected;
760 :
761 : // Since parse_reg_operand is the last thing we've considered,
762 : // The non-committed parse error type means that we have exhausted our
763 : // search path
764 :
765 : // We then should return the error of COMMITTED, even though we have not
766 : // committed to anything So that the error bubbles up and we recover from
767 : // this error gracefully
768 0 : rust_error_at (token->get_locus (),
769 : "expected operand, %s, options, or additional "
770 : "template string",
771 : "clobber_abi");
772 0 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
773 : }
774 37 : return tl::expected<InlineAsmContext, InlineAsmParseError> (inline_asm_ctx);
775 45 : }
776 :
777 : // same split as the backend, these operands go in the output list
778 : static bool
779 90 : is_output_operand (const AST::InlineAsmOperand &operand)
780 : {
781 0 : switch (operand.get_register_type ())
782 : {
783 : case AST::InlineAsmOperand::RegisterType::Out:
784 : case AST::InlineAsmOperand::RegisterType::InOut:
785 : case AST::InlineAsmOperand::RegisterType::SplitInOut:
786 : return true;
787 18 : default:
788 0 : return false;
789 : }
790 : }
791 :
792 : // asm! numbers its operands as written, but the backend hands gcc all the
793 : // outputs first and then the inputs, so a placeholder has to be renumbered
794 : // to where its operand ends up.
795 : static std::vector<size_t>
796 37 : gcc_operand_numbers (const std::vector<AST::InlineAsmOperand> &operands)
797 : {
798 37 : size_t outputs = 0;
799 82 : for (auto &operand : operands)
800 72 : if (is_output_operand (operand))
801 27 : outputs++;
802 :
803 37 : std::vector<size_t> numbers;
804 37 : size_t out_seen = 0;
805 37 : size_t in_seen = 0;
806 82 : for (auto &operand : operands)
807 : {
808 45 : if (is_output_operand (operand))
809 27 : numbers.push_back (out_seen++);
810 : else
811 18 : numbers.push_back (outputs + in_seen++);
812 : }
813 37 : return numbers;
814 : }
815 :
816 : tl::expected<InlineAsmContext, InlineAsmParseError>
817 37 : expand_inline_asm_strings (InlineAsmContext inline_asm_ctx)
818 : {
819 37 : auto &inline_asm = inline_asm_ctx.inline_asm;
820 :
821 37 : auto str_vec = inline_asm.get_template_strs ();
822 37 : auto numbers = gcc_operand_numbers (inline_asm.get_operands ());
823 :
824 37 : decltype (str_vec) resulting_template_vec;
825 74 : for (auto &template_str : str_vec)
826 : {
827 : /*std::cout << template_str.symbol << std::endl;*/
828 :
829 37 : auto pieces = Fmt::Pieces::collect (template_str.symbol, false,
830 37 : Fmt::ffi::ParseMode::InlineAsm);
831 37 : auto &pieces_vec = pieces.get_pieces ();
832 :
833 37 : std::string transformed_template_str = "";
834 137 : for (size_t i = 0; i < pieces_vec.size (); i++)
835 : {
836 100 : auto &piece = pieces_vec[i];
837 100 : if (piece.tag == Fmt::ffi::Piece::Tag::String)
838 : {
839 112 : transformed_template_str += piece.string._0.to_string ();
840 : }
841 44 : else if (piece.tag == Fmt::ffi::Piece::Tag::NextArgument)
842 : {
843 : /* std::cout << " " << i << ": "*/
844 : /*<< piece.next_argument._0.to_string () << std::endl;*/
845 :
846 44 : auto next_argument = piece.next_argument._0;
847 44 : switch (piece.next_argument._0.position.tag)
848 : {
849 43 : case Fmt::ffi::Position::Tag::ArgumentImplicitlyIs:
850 43 : {
851 43 : auto idx = next_argument.position.argument_implicitly_is._0;
852 43 : if (idx >= numbers.size ())
853 : {
854 0 : rust_error_at (inline_asm.get_locus (),
855 : "invalid reference to argument at "
856 : "index %lu",
857 : (unsigned long) idx);
858 0 : break;
859 : }
860 : /*auto trait = next_argument.format;*/
861 : /*auto arg = arguments.at (idx);*/
862 :
863 : /* // FIXME(Arthur): This API sucks*/
864 : /* rust_assert (arg.get_kind ().kind*/
865 : /*== AST::FormatArgumentKind::Kind::Normal);*/
866 : /**/
867 : /* args.push_back ({arg.get_expr ().clone_expr (),
868 : * trait});*/
869 :
870 43 : transformed_template_str
871 86 : += "%" + std::to_string (numbers[idx]);
872 : // std::cout << "argument implicitly is: " << idx <<
873 : // std::endl; std::cout << "transformed template str is:"
874 : // << transformed_template_str << std::endl;
875 : /*std::cout << "trait: " << trait.to_string () <<
876 : * std::endl;*/
877 : /*std::cout << "arg: " << arg.to_string () << std::endl;*/
878 : }
879 43 : break;
880 1 : case Fmt::ffi::Position::Tag::ArgumentIs:
881 1 : {
882 1 : auto idx = next_argument.position.argument_is._0;
883 1 : if (idx >= numbers.size ())
884 : {
885 1 : rust_error_at (inline_asm.get_locus (),
886 : "invalid reference to argument at "
887 : "index %lu",
888 : (unsigned long) idx);
889 1 : break;
890 : }
891 0 : transformed_template_str
892 0 : += "%" + std::to_string (numbers[idx]);
893 0 : break;
894 : }
895 0 : case Fmt::ffi::Position::Tag::ArgumentNamed:
896 0 : rust_sorry_at (inline_asm.get_locus (),
897 : "unhandled argument position specifier");
898 0 : break;
899 : }
900 44 : }
901 : }
902 37 : template_str.symbol = transformed_template_str;
903 74 : }
904 :
905 37 : inline_asm.template_strs = str_vec;
906 37 : return inline_asm_ctx;
907 37 : }
908 :
909 : tl::optional<AST::Fragment>
910 47 : parse_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
911 : AST::InvocKind semicolon, AST::AsmKind is_global_asm)
912 : {
913 : // From the rule of asm.
914 : // We first parse all formatted strings. If we fail, then we return
915 : // tl::nullopt
916 :
917 : // We then parse the asm arguments. If we fail, then we return
918 : // tl::nullopt
919 :
920 : // We then validate. If we fail, then we return tl::nullopt
921 :
922 : // Done
923 47 : MacroInvocLexer lex (invoc.get_delim_tok_tree ().to_token_stream ());
924 47 : Parser<MacroInvocLexer> parser (lex);
925 47 : auto last_token_id = macro_end_token (invoc.get_delim_tok_tree (), parser);
926 :
927 47 : AST::InlineAsm inline_asm (invoc_locus,
928 47 : is_global_asm == AST::AsmKind::Global);
929 47 : auto inline_asm_ctx = InlineAsmContext (inline_asm, parser, last_token_id);
930 :
931 47 : auto resulting_context = parse_format_strings (inline_asm_ctx)
932 47 : .and_then (parse_asm_arg)
933 47 : .and_then (validate)
934 47 : .and_then (expand_inline_asm_strings);
935 :
936 : // TODO: I'm putting the validation here because the rust reference put
937 : // it here Per Arthur's advice we would actually do the validation in a
938 : // different stage. and visit on the InlineAsm AST instead of it's
939 : // context.
940 47 : if (resulting_context)
941 : {
942 37 : auto resulting_ctx = resulting_context.value ();
943 37 : auto node = resulting_ctx.inline_asm.clone_expr_without_block ();
944 :
945 37 : std::vector<AST::SingleASTNode> single_vec = {};
946 :
947 : // If the macro invocation has a semicolon (`asm!("...");`), then we
948 : // need to make it a statement. This way, it will be expanded
949 : // properly.
950 37 : if (semicolon == AST::InvocKind::Semicoloned)
951 35 : single_vec.emplace_back (AST::SingleASTNode (
952 70 : std::make_unique<AST::ExprStmt> (std::move (node), invoc_locus,
953 : semicolon
954 70 : == AST::InvocKind::Semicoloned)));
955 : else
956 2 : single_vec.emplace_back (AST::SingleASTNode (std::move (node)));
957 :
958 37 : AST::Fragment fragment_ast
959 : = AST::Fragment (single_vec,
960 37 : std::vector<std::unique_ptr<AST::Token>> ());
961 37 : return fragment_ast;
962 37 : }
963 : else
964 : {
965 10 : return tl::nullopt;
966 : }
967 94 : }
968 :
969 : tl::expected<InlineAsmContext, InlineAsmParseError>
970 47 : parse_format_strings (InlineAsmContext inline_asm_ctx)
971 : {
972 : // Parse the first ever formatted string, success or not, will skip 1
973 : // token
974 47 : auto &parser = inline_asm_ctx.parser;
975 47 : auto last_token_id = inline_asm_ctx.last_token_id;
976 47 : auto fm_string = parse_format_string (inline_asm_ctx);
977 :
978 47 : auto &inline_asm = inline_asm_ctx.inline_asm;
979 47 : auto token = parser.peek_current_token ();
980 47 : if (fm_string == tl::nullopt)
981 : {
982 1 : rust_error_at (parser.peek_current_token ()->get_locus (),
983 : "%s template must be a string literal", "asm");
984 1 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
985 : }
986 : else
987 : {
988 46 : auto template_str
989 : = AST::TupleTemplateStr (token->get_locus (),
990 92 : strip_double_quotes (fm_string.value ()));
991 46 : inline_asm.template_strs.push_back (template_str);
992 46 : }
993 :
994 : // formatted string stream
995 :
996 92 : while (parser.peek_current_token ()->get_id () != last_token_id)
997 : {
998 42 : if (!parser.skip_token (COMMA))
999 : {
1000 1 : rust_error_at (parser.peek_current_token ()->get_locus (),
1001 : "expected token %qs", ";");
1002 1 : return tl::unexpected<InlineAsmParseError> (COMMITTED);
1003 : }
1004 : // Ok after the comma is good, we better be parsing correctly
1005 : // everything in here, which is formatted string in ABNF
1006 41 : inline_asm_ctx.consumed_comma_without_formatted_string = false;
1007 :
1008 41 : token = parser.peek_current_token ();
1009 41 : fm_string = parse_format_string (inline_asm_ctx);
1010 41 : if (fm_string == tl::nullopt)
1011 : {
1012 41 : inline_asm_ctx.consumed_comma_without_formatted_string = true;
1013 41 : break;
1014 : }
1015 : else
1016 : {
1017 0 : auto template_str
1018 : = AST::TupleTemplateStr (token->get_locus (),
1019 0 : strip_double_quotes (fm_string.value ()));
1020 0 : inline_asm.template_strs.push_back (template_str);
1021 0 : }
1022 : }
1023 :
1024 45 : return inline_asm_ctx;
1025 47 : }
1026 :
1027 : // bool
1028 : // is_label (const std::string &potential_label)
1029 : // {
1030 :
1031 : // if (potential_label.empty () || potential_label.back () != ':')
1032 : // return false;
1033 :
1034 : // // Check if all characters before the last colon are digits
1035 : // for (size_t i = 0; i < potential_label.length () - 1; i++)
1036 : // {
1037 : // if (potential_label[i] < '0' || potential_label[i] > '9')
1038 : // return false;
1039 : // }
1040 :
1041 : // return true;
1042 : // }
1043 :
1044 : tl::expected<InlineAsmContext, InlineAsmParseError>
1045 37 : validate (InlineAsmContext inline_asm_ctx)
1046 : {
1047 37 : return tl::expected<InlineAsmContext, InlineAsmParseError> (inline_asm_ctx);
1048 : }
1049 :
1050 : tl::optional<LlvmAsmContext>
1051 106 : parse_llvm_template (LlvmAsmContext ctx)
1052 : {
1053 106 : auto &parser = ctx.parser;
1054 :
1055 106 : auto token = parser.peek_current_token ();
1056 :
1057 106 : if (token->get_id () == ctx.last_token_id
1058 106 : || token->get_id () != STRING_LITERAL)
1059 : {
1060 8 : return tl::nullopt;
1061 : }
1062 :
1063 : // TODO: improve string handling?
1064 196 : ctx.llvm_asm.set_template (
1065 98 : AST::TupleTemplateStr (token->get_locus (),
1066 294 : strip_double_quotes (token->as_string ())));
1067 :
1068 98 : ctx.parser.skip_token ();
1069 :
1070 98 : return ctx;
1071 106 : }
1072 :
1073 : tl::optional<LlvmAsmContext>
1074 98 : parse_llvm_arguments (LlvmAsmContext ctx)
1075 : {
1076 98 : auto &parser = ctx.parser;
1077 98 : enum State
1078 : {
1079 : Templates = 0,
1080 : Output,
1081 : Input,
1082 : Clobbers,
1083 : Options
1084 98 : } current_state
1085 : = State::Templates;
1086 :
1087 98 : while (parser.peek_current_token ()->get_id () != ctx.last_token_id
1088 1496 : && parser.peek_current_token ()->get_id () != END_OF_FILE)
1089 : {
1090 624 : if (parser.peek_current_token ()->get_id () == SCOPE_RESOLUTION)
1091 : {
1092 0 : parser.skip_token (SCOPE_RESOLUTION);
1093 0 : current_state = static_cast<State> (current_state + 2);
1094 : }
1095 : else
1096 : {
1097 312 : parser.skip_token (COLON);
1098 312 : current_state = static_cast<State> (current_state + 1);
1099 : }
1100 :
1101 312 : switch (current_state)
1102 : {
1103 98 : case State::Output:
1104 98 : parse_llvm_outputs (ctx);
1105 98 : break;
1106 98 : case State::Input:
1107 98 : parse_llvm_inputs (ctx);
1108 98 : break;
1109 90 : case State::Clobbers:
1110 90 : parse_llvm_clobbers (ctx);
1111 90 : break;
1112 26 : case State::Options:
1113 26 : parse_llvm_options (ctx);
1114 26 : break;
1115 0 : case State::Templates:
1116 0 : default:
1117 0 : rust_unreachable ();
1118 : }
1119 : }
1120 :
1121 98 : return ctx;
1122 : }
1123 :
1124 : void
1125 196 : parse_llvm_operands (LlvmAsmContext &ctx, std::vector<AST::LlvmOperand> &result)
1126 : {
1127 196 : auto &parser = ctx.parser;
1128 196 : auto token = parser.peek_current_token ();
1129 634 : while (token->get_id () != COLON && token->get_id () != SCOPE_RESOLUTION
1130 688 : && token->get_id () != ctx.last_token_id)
1131 : {
1132 242 : std::string constraint;
1133 242 : if (token->get_id () == STRING_LITERAL)
1134 : {
1135 242 : constraint = strip_double_quotes (token->as_string ());
1136 : }
1137 242 : parser.skip_token (STRING_LITERAL);
1138 242 : parser.skip_token (LEFT_PAREN);
1139 :
1140 242 : token = parser.peek_current_token ();
1141 :
1142 242 : ParseRestrictions restrictions;
1143 242 : restrictions.expr_can_be_null = true;
1144 242 : auto expr = parser.parse_expr ();
1145 242 : rust_assert (expr);
1146 :
1147 242 : parser.skip_token (RIGHT_PAREN);
1148 :
1149 242 : result.emplace_back (constraint, std::move (expr.value ()));
1150 :
1151 484 : if (parser.peek_current_token ()->get_id () == COMMA)
1152 72 : parser.skip_token (COMMA);
1153 :
1154 242 : token = parser.peek_current_token ();
1155 242 : }
1156 196 : }
1157 :
1158 : void
1159 98 : parse_llvm_outputs (LlvmAsmContext &ctx)
1160 : {
1161 98 : parse_llvm_operands (ctx, ctx.llvm_asm.get_outputs ());
1162 98 : }
1163 :
1164 : void
1165 98 : parse_llvm_inputs (LlvmAsmContext &ctx)
1166 : {
1167 98 : parse_llvm_operands (ctx, ctx.llvm_asm.get_inputs ());
1168 98 : }
1169 :
1170 : void
1171 90 : parse_llvm_clobbers (LlvmAsmContext &ctx)
1172 : {
1173 90 : auto &parser = ctx.parser;
1174 90 : auto token = parser.peek_current_token ();
1175 334 : while (token->get_id () != COLON && token->get_id () != ctx.last_token_id)
1176 : {
1177 154 : if (token->get_id () == STRING_LITERAL)
1178 : {
1179 462 : ctx.llvm_asm.get_clobbers ().push_back (
1180 308 : {strip_double_quotes (token->as_string ()), token->get_locus ()});
1181 :
1182 154 : parser.skip_token (STRING_LITERAL);
1183 : }
1184 :
1185 154 : parser.maybe_skip_token (COMMA);
1186 154 : token = parser.peek_current_token ();
1187 : }
1188 90 : }
1189 :
1190 : void
1191 26 : parse_llvm_options (LlvmAsmContext &ctx)
1192 : {
1193 26 : auto &parser = ctx.parser;
1194 26 : auto token = parser.peek_current_token ();
1195 :
1196 78 : while (token->get_id () != ctx.last_token_id)
1197 : {
1198 26 : if (token->get_id () == STRING_LITERAL)
1199 : {
1200 26 : auto token_str = strip_double_quotes (token->as_string ());
1201 :
1202 26 : if (token_str == "volatile")
1203 26 : ctx.llvm_asm.set_volatile (true);
1204 0 : else if (token_str == "alignstack")
1205 0 : ctx.llvm_asm.set_align_stack (true);
1206 0 : else if (token_str == "intel")
1207 0 : ctx.llvm_asm.set_dialect (AST::LlvmInlineAsm::Dialect::Intel);
1208 : else
1209 0 : rust_error_at (token->get_locus (),
1210 : "Unknown llvm assembly option %qs",
1211 : token_str.c_str ());
1212 26 : }
1213 26 : parser.skip_token (STRING_LITERAL);
1214 :
1215 26 : token = parser.peek_current_token ();
1216 :
1217 26 : if (token->get_id () == ctx.last_token_id)
1218 26 : continue;
1219 0 : parser.skip_token (COMMA);
1220 : }
1221 :
1222 26 : parser.skip_token ();
1223 26 : }
1224 :
1225 : tl::optional<AST::Fragment>
1226 106 : parse_llvm_asm (location_t invoc_locus, AST::MacroInvocData &invoc,
1227 : AST::InvocKind semicolon, AST::AsmKind is_global_asm)
1228 : {
1229 106 : MacroInvocLexer lex (invoc.get_delim_tok_tree ().to_token_stream ());
1230 106 : Parser<MacroInvocLexer> parser (lex);
1231 106 : auto last_token_id = macro_end_token (invoc.get_delim_tok_tree (), parser);
1232 :
1233 106 : AST::LlvmInlineAsm llvm_asm{invoc_locus};
1234 :
1235 106 : auto asm_ctx = LlvmAsmContext (llvm_asm, parser, last_token_id);
1236 :
1237 106 : tl::optional<LlvmAsmContext> resulting_context
1238 106 : = parse_llvm_template (asm_ctx).and_then (parse_llvm_arguments);
1239 :
1240 106 : if (resulting_context)
1241 : {
1242 98 : auto resulting_ctx = resulting_context.value ();
1243 98 : auto node = resulting_ctx.llvm_asm.clone_expr_without_block ();
1244 :
1245 98 : std::vector<AST::SingleASTNode> single_vec = {};
1246 :
1247 : // If the macro invocation has a semicolon (`asm!("...");`), then we
1248 : // need to make it a statement. This way, it will be expanded
1249 : // properly.
1250 98 : if (semicolon == AST::InvocKind::Semicoloned)
1251 : {
1252 98 : single_vec.emplace_back (
1253 98 : std::make_unique<AST::ExprStmt> (std::move (node), invoc_locus,
1254 196 : true /* has semicolon */));
1255 : }
1256 : else
1257 0 : single_vec.emplace_back (std::move (node));
1258 :
1259 98 : AST::Fragment fragment_ast
1260 : = AST::Fragment (single_vec,
1261 98 : std::vector<std::unique_ptr<AST::Token>> ());
1262 98 : return fragment_ast;
1263 98 : }
1264 8 : return tl::nullopt;
1265 212 : }
1266 :
1267 : } // namespace Rust
|