#![recursion_limit = "128"]
extern crate proc_macro;
use proc_macro::TokenStream;
use quote::{quote, TokenStreamExt};
use syn::parse::{Parse, ParseStream};
use syn::{parse_macro_input, DeriveInput, Generics, Ident, ItemFn, ItemType, LitStr};
struct TypeDefinition {
ident: Ident,
generics: Generics,
}
impl Parse for TypeDefinition {
fn parse(input: ParseStream) -> syn::Result<Self> {
if let Ok(d) = DeriveInput::parse(input) {
Ok(Self {
ident: d.ident,
generics: d.generics,
})
} else if let Ok(t) = ItemType::parse(input) {
Ok(Self {
ident: t.ident,
generics: t.generics,
})
} else {
Err(input.error("Input is not an alias, enum, struct or union definition"))
}
}
}
#[proc_macro_attribute]
pub fn unsafe_guid(args: TokenStream, input: TokenStream) -> TokenStream {
let guid_str = parse_macro_input!(args as LitStr).value();
let mut result: proc_macro2::TokenStream = input.clone().into();
let type_definition = parse_macro_input!(input as TypeDefinition);
if guid_str.len() != 36 {
panic!(
"\"{}\" is not a canonical GUID string (expected 36 bytes, found {})",
guid_str,
guid_str.len()
);
}
let mut guid_hex_iter = guid_str.split('-');
let mut next_guid_int = |expected_num_bits: usize| -> u64 {
let guid_hex_component = guid_hex_iter.next().unwrap();
if guid_hex_component.len() != expected_num_bits / 4 {
panic!(
"GUID component \"{}\" is not a {}-bit hexadecimal string",
guid_hex_component, expected_num_bits
);
}
match u64::from_str_radix(guid_hex_component, 16) {
Ok(number) => number,
_ => panic!(
"GUID component \"{}\" is not a hexadecimal number",
guid_hex_component
),
}
};
let time_low = next_guid_int(32) as u32;
let time_mid = next_guid_int(16) as u16;
let time_high_and_version = next_guid_int(16) as u16;
let clock_seq_and_variant = next_guid_int(16) as u16;
let node_64 = next_guid_int(48);
let node = [
(node_64 >> 40) as u8,
((node_64 >> 32) % 0x100) as u8,
((node_64 >> 24) % 0x100) as u8,
((node_64 >> 16) % 0x100) as u8,
((node_64 >> 8) % 0x100) as u8,
(node_64 % 0x100) as u8,
];
let ident = type_definition.ident.clone();
let (impl_generics, ty_generics, where_clause) = type_definition.generics.split_for_impl();
result.append_all(quote! {
unsafe impl #impl_generics crate::Identify for #ident #ty_generics #where_clause {
#[doc(hidden)]
#[allow(clippy::unreadable_literal)]
const GUID : crate::Guid = crate::Guid::from_values(
#time_low,
#time_mid,
#time_high_and_version,
#clock_seq_and_variant,
[#(#node),*],
);
}
});
result.into()
}
#[proc_macro_derive(Protocol)]
pub fn derive_protocol(item: TokenStream) -> TokenStream {
let item = parse_macro_input!(item as DeriveInput);
let ident = item.ident.clone();
let (impl_generics, ty_generics, where_clause) = item.generics.split_for_impl();
let result = quote! {
impl #impl_generics crate::proto::Protocol for #ident #ty_generics #where_clause {}
impl #impl_generics !Send for #ident #ty_generics #where_clause {}
impl #impl_generics !Sync for #ident #ty_generics #where_clause {}
};
result.into()
}
#[proc_macro_attribute]
pub fn entry(args: TokenStream, input: TokenStream) -> TokenStream {
if !args.is_empty() {
panic!("This attribute accepts no arguments");
}
let f = parse_macro_input!(input as ItemFn);
let entry_fn_ident = &f.sig.ident;
let result = quote!(
static _UEFI_ENTRY_POINT_TYPE_CHECK: extern "efiapi" fn(uefi::Handle, uefi::table::SystemTable<uefi::table::Boot>) -> uefi::Status = #entry_fn_ident;
#[no_mangle]
pub extern "efiapi" #f
);
result.into()
}