Skip to content

Commit

Permalink
some formatting on macros
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Redondo committed Sep 17, 2023
1 parent 0868749 commit efd490c
Show file tree
Hide file tree
Showing 6 changed files with 107 additions and 108 deletions.
24 changes: 13 additions & 11 deletions rust/src/helpers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,11 @@ pub trait FetchIncrement {
fn fetch_increment(&mut self) -> Self;
}

macro_rules! impl_for_primitives{
macro_rules! impl_for_primitives {
($($ty:ty),* $(,)?) => {
$(
impl FetchIncrement for $ty{
fn fetch_increment(&mut self) -> Self{
impl FetchIncrement for $ty {
fn fetch_increment(&mut self) -> Self {
let val = *self;
*self += 1;
val
Expand Down Expand Up @@ -41,15 +41,17 @@ impl core::fmt::Display for TabPrinter {
}

macro_rules! nzu16 {
($val:expr) => {{
const __VAL: ::core::num::NonZeroU16 = {
const __LIT: u16 = $val;
[()][(__LIT == 0) as usize];
($val:expr) => {
{
const __VAL: ::core::num::NonZeroU16 = {
const __LIT: u16 = $val;
[()][(__LIT == 0) as usize];

unsafe { ::core::num::NonZeroU16::new_unchecked(__LIT) }
};
__VAL
}};
unsafe { ::core::num::NonZeroU16::new_unchecked(__LIT) }
};
__VAL
}
};
}

pub(crate) use nzu16;
Expand Down
12 changes: 6 additions & 6 deletions rust/src/irgen/visitor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{
},
};

macro_rules! def_visitors{
macro_rules! def_visitors {
(
$(
$vis:vis trait $trait:ident {
Expand Down Expand Up @@ -614,11 +614,11 @@ def_visitors! {
fn visit_struct(&mut self) -> Option<Box<dyn ConstructorDefVisitor + '_>>;
}

pub trait ConstructorDefVisitor{
pub trait ConstructorDefVisitor {
fn visit_field(&mut self) -> Option<Box<dyn FieldVisitor + '_>>;
}

pub trait FieldVisitor{
pub trait FieldVisitor {
fn visit_name(&mut self, name: &ty::FieldName);
fn visit_ty(&mut self) -> Option<Box<dyn TypeVisitor + '_>>;
}
Expand Down Expand Up @@ -664,16 +664,16 @@ def_visitors! {
fn visit_binary_expr(&mut self) -> Option<Box<dyn BinaryExprVisitor + '_>>;
}

pub trait TupleExprVisitor{
pub trait TupleExprVisitor {
fn visit_elem(&mut self) -> Option<Box<dyn ExprVisitor + '_>>;
}

pub trait ConstIntVisitor{
pub trait ConstIntVisitor {
fn visit_intty(&mut self) -> Option<Box<dyn IntTyVisitor + '_>>;
fn visit_value(&mut self, val: u128);
}

pub trait CastVisitor{
pub trait CastVisitor {
fn visit_inner(&mut self) -> Option<Box<dyn ExprVisitor + '_>>;
fn visit_cast_type(&mut self) -> Option<Box<dyn TypeVisitor + '_>>;
}
Expand Down
34 changes: 17 additions & 17 deletions rust/src/lang.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,35 +9,35 @@ pub enum LangItemTarget {
ImplBlock,
}

macro_rules! define_lang_items{
macro_rules! define_lang_items {
{
$($enum:ident: $name:ident @ $target:ident),* $(,)?
} => {
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq)]
#[repr(u32)]
pub enum LangItem{
pub enum LangItem {
$(
$enum
),*
}

impl LangItem{
pub fn from_name(x: &str) -> Option<Self>{
match x{
$(::core::stringify!($name) => Some(Self:: $enum),)*
impl LangItem {
pub fn from_name(x: &str) -> Option<Self> {
match x {
$(::core::stringify!($name) => Some(Self::$enum),)*
_ => None
}
}

pub const fn name(&self) -> &'static str{
match self{
$(Self:: $enum => ::core::stringify!($name)),*
pub const fn name(&self) -> &'static str {
match self {
$(Self::$enum => ::core::stringify!($name)),*
}
}

pub const fn target(&self) -> LangItemTarget{
match self{
$(Self:: $enum => LangItemTarget:: $target),*
pub const fn target(&self) -> LangItemTarget {
match self {
$(Self::$enum => LangItemTarget::$target),*
}
}
}
Expand All @@ -60,11 +60,11 @@ define_lang_items! {
Tuple: tuple @ ImplBlock,
Slice: slice @ ImplBlock,
Array: array @ ImplBlock,
I8: i8 @ ImplBlock,
I16: i16 @ ImplBlock,
I32: i32 @ ImplBlock,
I64: i64 @ ImplBlock,
I128: i128 @ ImplBlock,
I8: i8 @ ImplBlock,
I16: i16 @ ImplBlock,
I32: i32 @ ImplBlock,
I64: i64 @ ImplBlock,
I128: i128 @ ImplBlock,
ISize: isize @ ImplBlock,
U8: u8 @ ImplBlock,
U16: u16 @ ImplBlock,
Expand Down
40 changes: 22 additions & 18 deletions rust/src/lex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,32 +204,32 @@ impl fmt::Debug for LexemeBody {
}
}

macro_rules! punctuation{
macro_rules! punctuation {
{
$($tok:tt => $name:ident),* $(,)?
} => {
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Punctuation{
pub enum Punctuation {
$($name),*
}

impl Punctuation{
pub fn from_token(x: &str) -> Self{
match x{
pub fn from_token(x: &str) -> Self {
match x {
$(::core::stringify!($tok) => Self::$name,)*
_ => panic!("Not a punctuation token: {}", x)
_ => panic!("Not a punctuation token: {}", x),
}
}

pub const fn symbol(&self) -> &'static str{
match self{
pub const fn symbol(&self) -> &'static str {
match self {
$(Self::$name => ::core::stringify!($tok)),*
}
}
}

#[macro_export]
macro_rules! punct{
macro_rules! punct {
$(
[
$tok
Expand All @@ -239,7 +239,9 @@ macro_rules! punctuation{
)*

[$tt:tt] => {
{::core::compile_error!(::core::concat!("Not a punctuation token: ", ::core::stringify!($tt)))}
{
::core::compile_error!(::core::concat!("Not a punctuation token: ", ::core::stringify!($tt)))
}
};
}

Expand All @@ -252,27 +254,27 @@ macro_rules! keywords{
$($tok:tt => $name:ident),* $(,)?
} => {
#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
pub enum Keyword{
pub enum Keyword {
$($name),*
}

impl Keyword{
pub fn from_token(x: &str) -> Self{
match x{
impl Keyword {
pub fn from_token(x: &str) -> Self {
match x {
$(::core::stringify!($tok) => Self::$name,)*
_ => panic!("Not a keyword: {}", x)
_ => panic!("Not a keyword: {}", x),
}
}

pub const fn symbol(&self) -> &'static str{
match self{
pub const fn symbol(&self) -> &'static str {
match self {
$(Self::$name => ::core::stringify!($tok)),*
}
}
}

#[macro_export]
macro_rules! keyword{
macro_rules! keyword {
$(
[
$tok
Expand All @@ -282,7 +284,9 @@ macro_rules! keywords{
)*

[$tt:tt] => {
{::core::compile_error!(::core::concat!("Not a keyword: ", ::core::stringify!($tt)))}
{
::core::compile_error!(::core::concat!("Not a keyword: ", ::core::stringify!($tt)))
}
};
}

Expand Down
78 changes: 36 additions & 42 deletions rust/src/macro_parse/proc/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,31 @@ mod abi {
pub call: rustcall!(unsafe extern "rustcall" fn(*const (), A) -> R),
}

macro_rules! generate_vtable_impla {
macro_rules! generate_vtable_impls {
(impl vtable($tr:ident) for $vtable:ident{
for<$ty:ident> $vtable_val:expr
}) => {
unsafe impl<'a, A, R> AbiSafeVTable<dyn 'a + $tr<A, Output = R>> for $vtable<A, R> {}
unsafe impl<'a, A, R> AbiSafeVTable<dyn 'a + Send + $tr<A, Output = R>>
for $vtable<A, R>
{
}
{}
unsafe impl<'a, A, R> AbiSafeVTable<dyn 'a + Sync + $tr<A, Output = R>>
for $vtable<A, R>
{
}
{}
unsafe impl<'a, A, R> AbiSafeVTable<dyn 'a + Send + Sync + $tr<A, Output = R>>
for $vtable<A, R>
{
}
{}

unsafe impl<'a, A, R> TrustedVTable<dyn 'a + $tr<A, Output = R>> for $vtable<A, R> {}
unsafe impl<'a, A, R> TrustedVTable<dyn 'a + Send + $tr<A, Output = R>>
for $vtable<A, R>
{
}
{}
unsafe impl<'a, A, R> TrustedVTable<dyn 'a + Sync + $tr<A, Output = R>>
for $vtable<A, R>
{
}
{}
unsafe impl<'a, A, R> TrustedVTable<dyn 'a + Send + Sync + $tr<A, Output = R>>
for $vtable<A, R>
{
}
{}

unsafe impl<'a, A: 'static, R: 'static> AbiSafeTrait for dyn 'a + $tr<A, Output = R> {
type VTable = $vtable<A, R>;
Expand Down Expand Up @@ -149,76 +143,76 @@ mod abi {
}

rustcall! {
unsafe extern "rustcall" fn destroy<T>(p: *mut ()){
unsafe extern "rustcall" fn destroy<T>(p: *mut ()) {
core::ptr::drop_in_place(p as *mut T)
}
}

rustcall! {
unsafe extern "rustcall" fn call_once_unsized<T: LCRustFnOnce<A,Output=R>,A,R>(p: *mut (),arg0: A) -> R{
unsafe{(p as *mut T).read().call_once(arg0)}
unsafe extern "rustcall" fn call_once_unsized<T: LCRustFnOnce<A,Output=R>,A,R>(p: *mut (),arg0: A) -> R {
unsafe { (p as *mut T).read().call_once(arg0) }
}
}

rustcall! {
unsafe extern "rustcall" fn call_mut<T: LCRustFnMut<A,Output=R>,A,R>(p: *mut (),arg0: A) -> R{
unsafe{&mut *(p as *mut T)}.call_mut(arg0)
unsafe{ &mut *(p as *mut T) }.call_mut(arg0)
}
}

rustcall! {
unsafe extern "rustcall" fn call<T: LCRustFn<A,Output=R>,A,R>(p: *const (),arg0: A) -> R{
unsafe{&*(p as *const T)}.call(arg0)
unsafe{ &*(p as *const T) }.call(arg0)
}
}

generate_vtable_impla! {
impl vtable(LCRustFnOnce) for LCRustFnOnceVtable{
for<T> LCRustFnOnceVtable{
head: AbiSafeVTableHead{
generate_vtable_impls! {
impl vtable(LCRustFnOnce) for LCRustFnOnceVtable {
for<T> LCRustFnOnceVtable {
head: AbiSafeVTableHead {
size: core::mem::size_of::<T>(),
align: core::mem::align_of::<T>(),
destructor: Some(destroy::<T>),
reserved_deallocate: None
reserved_deallocate: None,
},
call_once_unsized: call_once_unsized::<T,_,_>
call_once_unsized: call_once_unsized::<T, _, _>,
}
}
}

generate_vtable_impla! {
impl vtable(LCRustFnMut) for LCRustFnMutVtable{
for<T> LCRustFnMutVtable{
fnonce: LCRustFnOnceVtable{
head: AbiSafeVTableHead{
generate_vtable_impls! {
impl vtable(LCRustFnMut) for LCRustFnMutVtable {
for<T> LCRustFnMutVtable {
fnonce: LCRustFnOnceVtable {
head: AbiSafeVTableHead {
size: core::mem::size_of::<T>(),
align: core::mem::align_of::<T>(),
destructor: Some(destroy::<T>),
reserved_deallocate: None
reserved_deallocate: None,
},
call_once_unsized: call_once_unsized::<T,_,_>
call_once_unsized: call_once_unsized::<T, _, _>,
},
call_mut: call_mut::<T,_,_>
call_mut: call_mut::<T, _, _>,
}
}
}

generate_vtable_impla! {
impl vtable(LCRustFn) for LCRustFnVtable{
for<T> LCRustFnVtable{
fnmut: LCRustFnMutVtable{
fnonce: LCRustFnOnceVtable{
head: AbiSafeVTableHead{
generate_vtable_impls! {
impl vtable(LCRustFn) for LCRustFnVtable {
for<T> LCRustFnVtable {
fnmut: LCRustFnMutVtable {
fnonce: LCRustFnOnceVtable {
head: AbiSafeVTableHead {
size: core::mem::size_of::<T>(),
align: core::mem::align_of::<T>(),
destructor: Some(destroy::<T>),
reserved_deallocate: None
reserved_deallocate: None,
},
call_once_unsized: call_once_unsized::<T,_,_>
call_once_unsized: call_once_unsized::<T, _, _>,
},
call_mut: call_mut::<T,_,_>
call_mut: call_mut::<T, _, _>,
},
call: call::<T,_,_>
call: call::<T, _, _>,
}
}
}
Expand Down
Loading

0 comments on commit efd490c

Please sign in to comment.