]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/global_asm.rs
Auto merge of #53133 - Zoxc:gen-int, r=eddyb
[rust.git] / src / libsyntax_ext / global_asm.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /// Module-level assembly support.
12 ///
13 /// The macro defined here allows you to specify "top-level",
14 /// "file-scoped", or "module-level" assembly. These synonyms
15 /// all correspond to LLVM's module-level inline assembly instruction.
16 ///
17 /// For example, `global_asm!("some assembly here")` codegens to
18 /// LLVM's `module asm "some assembly here"`. All of LLVM's caveats
19 /// therefore apply.
20
21 use rustc_data_structures::small_vec::OneVector;
22
23 use syntax::ast;
24 use syntax::codemap::respan;
25 use syntax::ext::base;
26 use syntax::ext::base::*;
27 use syntax::feature_gate;
28 use syntax::ptr::P;
29 use syntax::symbol::Symbol;
30 use syntax_pos::Span;
31 use syntax::tokenstream;
32
33 pub const MACRO: &'static str = "global_asm";
34
35 pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt,
36                               sp: Span,
37                               tts: &[tokenstream::TokenTree]) -> Box<dyn base::MacResult + 'cx> {
38     if !cx.ecfg.enable_global_asm() {
39         feature_gate::emit_feature_err(&cx.parse_sess,
40                                        MACRO,
41                                        sp,
42                                        feature_gate::GateIssue::Language,
43                                        feature_gate::EXPLAIN_GLOBAL_ASM);
44         return DummyResult::any(sp);
45     }
46
47     let mut p = cx.new_parser_from_tts(tts);
48     let (asm, _) = match expr_to_string(cx,
49                                         panictry!(p.parse_expr()),
50                                         "inline assembly must be a string literal") {
51         Some((s, st)) => (s, st),
52         None => return DummyResult::any(sp),
53     };
54
55     MacEager::items(OneVector::one(P(ast::Item {
56         ident: ast::Ident::with_empty_ctxt(Symbol::intern("")),
57         attrs: Vec::new(),
58         id: ast::DUMMY_NODE_ID,
59         node: ast::ItemKind::GlobalAsm(P(ast::GlobalAsm {
60             asm,
61             ctxt: cx.backtrace(),
62         })),
63         vis: respan(sp.shrink_to_lo(), ast::VisibilityKind::Inherited),
64         span: sp,
65         tokens: None,
66     })))
67 }