Add Targets derive macro
[hiphop-php.git] / hphp / hack / src / hackc / hhbc / emit_opcodes_macro.rs
blob12eb220845d79d6bb620f61f236482042fbf7b92
1 // Copyright (c) Facebook, Inc. and its affiliates.
2 //
3 // This source code is licensed under the MIT license found in the
4 // LICENSE file in the "hack" directory of this source tree.
6 /// Emit the opcodes enum. Given input that looks like this:
7 ///
8 /// ```
9 /// #[macros::emit_opcodes]
10 /// enum MyCrazyOpcodes<'lifetime> { }
11 /// ```
12 ///
13 /// The result will look something like this:
14 ///
15 /// ```
16 /// enum MyCrazyOpcodes<'lifetime> {
17 ///     Jmp(Label),
18 ///     Nop,
19 ///     PopL(Local<'lifetime>),
20 /// }
21 /// ```
22 ///
23 /// If the 'Targets' derive is used then the Targets trait will be implemented
24 /// as well.
25 ///
26 /// See emit_opcodes::tests::test_basic() for a more detailed example output.
27 ///
28 #[proc_macro_attribute]
29 pub fn emit_opcodes(
30     _attrs: proc_macro::TokenStream,
31     input: proc_macro::TokenStream,
32 ) -> proc_macro::TokenStream {
33     match emit_opcodes::emit_opcodes(input.into(), hhbc::opcode_data()) {
34         Ok(res) => res.into(),
35         Err(err) => err.into_compile_error().into(),
36     }
39 #[proc_macro_derive(Targets)]
40 pub fn emit_targets(input: proc_macro::TokenStream) -> proc_macro::TokenStream {
41     match emit_opcodes::emit_impl_targets(input.into(), hhbc::opcode_data()) {
42         Ok(res) => res.into(),
43         Err(err) => err.into_compile_error().into(),
44     }