]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/global_asm.rs
Auto merge of #60840 - tmandry:preserve-scope-in-generator-mir, r=cramertj
[rust.git] / src / libsyntax_ext / 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 errors::DiagnosticBuilder;
12
13 use syntax::ast;
14 use syntax::source_map::respan;
15 use syntax::ext::base::{self, *};
16 use syntax::feature_gate;
17 use syntax::parse::token;
18 use syntax::ptr::P;
19 use syntax::symbol::{Symbol, sym};
20 use syntax_pos::Span;
21 use syntax::tokenstream;
22 use smallvec::smallvec;
23
24 pub const MACRO: Symbol = sym::global_asm;
25
26 pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt<'_>,
27                               sp: Span,
28                               tts: &[tokenstream::TokenTree]) -> Box<dyn base::MacResult + 'cx> {
29     if !cx.ecfg.enable_global_asm() {
30         feature_gate::emit_feature_err(&cx.parse_sess,
31                                        MACRO,
32                                        sp,
33                                        feature_gate::GateIssue::Language,
34                                        feature_gate::EXPLAIN_GLOBAL_ASM);
35     }
36
37     match parse_global_asm(cx, sp, tts) {
38         Ok(Some(global_asm)) => {
39             MacEager::items(smallvec![P(ast::Item {
40                 ident: ast::Ident::with_empty_ctxt(Symbol::intern("")),
41                 attrs: Vec::new(),
42                 id: ast::DUMMY_NODE_ID,
43                 node: ast::ItemKind::GlobalAsm(P(global_asm)),
44                 vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
45                 span: sp,
46                 tokens: None,
47             })])
48         }
49         Ok(None) => DummyResult::any(sp),
50         Err(mut err) => {
51             err.emit();
52             DummyResult::any(sp)
53         }
54     }
55 }
56
57 fn parse_global_asm<'a>(
58     cx: &mut ExtCtxt<'a>,
59     sp: Span,
60     tts: &[tokenstream::TokenTree]
61 ) -> Result<Option<ast::GlobalAsm>, DiagnosticBuilder<'a>> {
62     let mut p = cx.new_parser_from_tts(tts);
63
64     if p.token == token::Eof {
65         let mut err = cx.struct_span_err(sp, "macro requires a string literal as an argument");
66         err.span_label(sp, "string literal required");
67         return Err(err);
68     }
69
70     let expr = p.parse_expr()?;
71     let (asm, _) = match expr_to_string(cx, expr, "inline assembly must be a string literal") {
72         Some((s, st)) => (s, st),
73         None => return Ok(None),
74     };
75
76     Ok(Some(ast::GlobalAsm {
77         asm,
78         ctxt: cx.backtrace(),
79     }))
80 }