]> git.lizzy.rs Git - rust.git/blob - src/libsyntax_ext/global_asm.rs
Auto merge of #43710 - zackmdavis:field_init_shorthand_power_slam, r=Mark-Simulacrum
[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")` translates to
18 /// LLVM's `module asm "some assembly here"`. All of LLVM's caveats
19 /// therefore apply.
20
21 use syntax::ast;
22 use syntax::ext::base;
23 use syntax::ext::base::*;
24 use syntax::feature_gate;
25 use syntax::ptr::P;
26 use syntax::symbol::Symbol;
27 use syntax_pos::Span;
28 use syntax::tokenstream;
29
30 use syntax::util::small_vector::SmallVector;
31
32 pub const MACRO: &'static str = "global_asm";
33
34 pub fn expand_global_asm<'cx>(cx: &'cx mut ExtCtxt,
35                               sp: Span,
36                               tts: &[tokenstream::TokenTree]) -> Box<base::MacResult + 'cx> {
37     if !cx.ecfg.enable_global_asm() {
38         feature_gate::emit_feature_err(&cx.parse_sess,
39                                        MACRO,
40                                        sp,
41                                        feature_gate::GateIssue::Language,
42                                        feature_gate::EXPLAIN_GLOBAL_ASM);
43         return DummyResult::any(sp);
44     }
45
46     let mut p = cx.new_parser_from_tts(tts);
47     let (asm, _) = match expr_to_string(cx,
48                                         panictry!(p.parse_expr()),
49                                         "inline assembly must be a string literal") {
50         Some((s, st)) => (s, st),
51         None => return DummyResult::any(sp),
52     };
53
54     MacEager::items(SmallVector::one(P(ast::Item {
55         ident: ast::Ident::with_empty_ctxt(Symbol::intern("")),
56         attrs: Vec::new(),
57         id: ast::DUMMY_NODE_ID,
58         node: ast::ItemKind::GlobalAsm(P(ast::GlobalAsm {
59             asm,
60             ctxt: cx.backtrace(),
61         })),
62         vis: ast::Visibility::Inherited,
63         span: sp,
64         tokens: None,
65     })))
66 }