]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_builtin_macros/src/global_asm.rs
Rollup merge of #80573 - jyn514:tool-lints, r=GuillaumeGomez
[rust.git] / compiler / rustc_builtin_macros / src / global_asm.rs
1 //! Module-level assembly support.
2 //!
3 //! The macro defined here allows you to specify "top-level",
4 //! "file-scoped", or "module-level" assembly. These synonyms
5 //! all correspond to LLVM's module-level inline assembly instruction.
6 //!
7 //! For example, `global_asm!("some assembly here")` codegens to
8 //! LLVM's `module asm "some assembly here"`. All of LLVM's caveats
9 //! therefore apply.
10
11 use rustc_ast as ast;
12 use rustc_ast::ptr::P;
13 use rustc_ast::token;
14 use rustc_ast::tokenstream::TokenStream;
15 use rustc_errors::DiagnosticBuilder;
16 use rustc_expand::base::{self, *};
17 use rustc_span::symbol::Ident;
18 use rustc_span::Span;
19 use smallvec::smallvec;
20
21 pub fn expand_global_asm<'cx>(
22     cx: &'cx mut ExtCtxt<'_>,
23     sp: Span,
24     tts: TokenStream,
25 ) -> Box<dyn base::MacResult + 'cx> {
26     match parse_global_asm(cx, sp, tts) {
27         Ok(Some(global_asm)) => MacEager::items(smallvec![P(ast::Item {
28             ident: Ident::invalid(),
29             attrs: Vec::new(),
30             id: ast::DUMMY_NODE_ID,
31             kind: ast::ItemKind::GlobalAsm(P(global_asm)),
32             vis: ast::Visibility {
33                 span: sp.shrink_to_lo(),
34                 kind: ast::VisibilityKind::Inherited,
35                 tokens: None,
36             },
37             span: cx.with_def_site_ctxt(sp),
38             tokens: None,
39         })]),
40         Ok(None) => DummyResult::any(sp),
41         Err(mut err) => {
42             err.emit();
43             DummyResult::any(sp)
44         }
45     }
46 }
47
48 fn parse_global_asm<'a>(
49     cx: &mut ExtCtxt<'a>,
50     sp: Span,
51     tts: TokenStream,
52 ) -> Result<Option<ast::GlobalAsm>, DiagnosticBuilder<'a>> {
53     let mut p = cx.new_parser_from_tts(tts);
54
55     if p.token == token::Eof {
56         let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
57         err.span_label(sp, "string literal required");
58         return Err(err);
59     }
60
61     let expr = p.parse_expr()?;
62     let (asm, _) = match expr_to_string(cx, expr, "inline assembly must be a string literal") {
63         Some((s, st)) => (s, st),
64         None => return Ok(None),
65     };
66
67     Ok(Some(ast::GlobalAsm { asm }))
68 }