]> git.lizzy.rs Git - rust.git/blob - src/librustc_builtin_macros/global_asm.rs
Rollup merge of #73715 - MaulingMonkey:pr-natvis-tuples, r=Amanieu
[rust.git] / src / librustc_builtin_macros / 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::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::source_map::respan;
18 use rustc_span::symbol::Ident;
19 use rustc_span::Span;
20 use smallvec::smallvec;
21
22 pub fn expand_global_asm<'cx>(
23     cx: &'cx mut ExtCtxt<'_>,
24     sp: Span,
25     tts: TokenStream,
26 ) -> Box<dyn base::MacResult + 'cx> {
27     match parse_global_asm(cx, sp, tts) {
28         Ok(Some(global_asm)) => MacEager::items(smallvec![P(ast::Item {
29             ident: Ident::invalid(),
30             attrs: Vec::new(),
31             id: ast::DUMMY_NODE_ID,
32             kind: ast::ItemKind::GlobalAsm(P(global_asm)),
33             vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
34             span: cx.with_def_site_ctxt(sp),
35             tokens: None,
36         })]),
37         Ok(None) => DummyResult::any(sp),
38         Err(mut err) => {
39             err.emit();
40             DummyResult::any(sp)
41         }
42     }
43 }
44
45 fn parse_global_asm<'a>(
46     cx: &mut ExtCtxt<'a>,
47     sp: Span,
48     tts: TokenStream,
49 ) -> Result<Option<ast::GlobalAsm>, DiagnosticBuilder<'a>> {
50     let mut p = cx.new_parser_from_tts(tts);
51
52     if p.token == token::Eof {
53         let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
54         err.span_label(sp, "string literal required");
55         return Err(err);
56     }
57
58     let expr = p.parse_expr()?;
59     let (asm, _) = match expr_to_string(cx, expr, "inline assembly must be a string literal") {
60         Some((s, st)) => (s, st),
61         None => return Ok(None),
62     };
63
64     Ok(Some(ast::GlobalAsm { asm }))
65 }