objc2/macros/
__rewrite_self_param.rs

1/// Detect instance vs. class method.
2///
3/// Will add:
4/// ```ignore
5/// (builder_method:ident)
6/// (receiver:expr)
7/// (receiver_ty:ty)
8/// (params_prefix*)
9/// (params_rest*)
10/// ```
11#[doc(hidden)]
12#[macro_export]
13macro_rules! __rewrite_self_param {
14    {
15        ($($params:tt)*)
16
17        ($out_macro:path)
18        $($macro_args:tt)*
19    } => {
20        $crate::__rewrite_self_param_inner! {
21            // Duplicate params out so that we can match on `self`, while still
22            // using it as a function parameter
23            ($($params)*)
24            ($($params)*)
25
26            ($out_macro)
27            $($macro_args)*
28        }
29    };
30}
31
32#[doc(hidden)]
33#[macro_export]
34macro_rules! __rewrite_self_param_inner {
35    // Instance method
36    {
37        (&self $($__params_rest:tt)*)
38        (&$self:ident $(, $($params_rest:tt)*)?)
39
40        ($out_macro:path)
41        $($macro_args:tt)*
42    } => {
43        $out_macro! {
44            $($macro_args)*
45
46            (add_method)
47            ($self)
48            (&Self)
49            (
50                &$self,
51                _: $crate::runtime::Sel,
52            )
53            ($($($params_rest)*)?)
54        }
55    };
56    {
57        (&mut self $($__params_rest:tt)*)
58        (&mut $self:ident $(, $($params_rest:tt)*)?)
59
60        ($out_macro:path)
61        $($macro_args:tt)*
62    } => {
63        $out_macro! {
64            $($macro_args)*
65
66            (add_method)
67            ($self)
68            (&mut Self)
69            (
70                &mut $self,
71                _: $crate::runtime::Sel,
72            )
73            ($($($params_rest)*)?)
74        }
75    };
76    {
77        (self: $__self_ty:ty $(, $($__params_rest:tt)*)?)
78        ($self:ident: $self_ty:ty $(, $($params_rest:tt)*)?)
79
80        ($out_macro:path)
81        $($macro_args:tt)*
82    } => {
83        $out_macro! {
84            $($macro_args)*
85
86            (add_method)
87            ($self)
88            ($self_ty)
89            (
90                $self: $self_ty,
91                _: $crate::runtime::Sel,
92            )
93            ($($($params_rest)*)?)
94        }
95    };
96    {
97        (mut self: $__self_ty:ty $(, $($__params_rest:tt)*)?)
98        ($mut:ident $self:ident: $self_ty:ty $(, $($params_rest:tt)*)?)
99
100        ($out_macro:path)
101        $($macro_args:tt)*
102    } => {
103        $out_macro! {
104            $($macro_args)*
105
106            (add_method)
107            ($self)
108            ($self_ty)
109            (
110                $mut $self: $self_ty,
111                _: $crate::runtime::Sel,
112            )
113            ($($($params_rest)*)?)
114        }
115    };
116
117    // `this: Type` or `_this: Type` instance method
118    // Workaround for arbitrary self types being unstable
119    // https://doc.rust-lang.org/nightly/unstable-book/language-features/arbitrary-self-types.html
120    {
121        (mut this: $__self_ty:ty $(, $($__params_rest:tt)*)?)
122        ($mut:ident $this:ident: $this_ty:ty $(, $($params_rest:tt)*)?)
123
124        ($out_macro:path)
125        $($macro_args:tt)*
126    } => {
127        $out_macro! {
128            $($macro_args)*
129
130            (add_method)
131            ($this)
132            ($this_ty)
133            (
134                $mut $this: $this_ty,
135                _: $crate::runtime::Sel,
136            )
137            ($($($params_rest)*)?)
138        }
139    };
140    {
141        (this: $__self_ty:ty $(, $($__params_rest:tt)*)?)
142        ($this:ident: $this_ty:ty $(, $($params_rest:tt)*)?)
143
144        ($out_macro:path)
145        $($macro_args:tt)*
146    } => {
147        $out_macro! {
148            $($macro_args)*
149
150            (add_method)
151            ($this)
152            ($this_ty)
153            (
154                $this: $this_ty,
155                _: $crate::runtime::Sel,
156            )
157            ($($($params_rest)*)?)
158        }
159    };
160    {
161        (mut _this: $__self_ty:ty $(, $($__params_rest:tt)*)?)
162        ($mut:ident $this:ident: $this_ty:ty $(, $($params_rest:tt)*)?)
163
164        ($out_macro:path)
165        $($macro_args:tt)*
166    } => {
167        $out_macro! {
168            $($macro_args)*
169
170            (add_method)
171            ($this)
172            ($this_ty)
173            (
174                $mut $this: $this_ty,
175                _: $crate::runtime::Sel,
176            )
177            ($($($params_rest)*)?)
178        }
179    };
180    {
181        (_this: $__self_ty:ty $(, $($__params_rest:tt)*)?)
182        ($this:ident: $this_ty:ty $(, $($params_rest:tt)*)?)
183
184        ($out_macro:path)
185        $($macro_args:tt)*
186    } => {
187        $out_macro! {
188            $($macro_args)*
189
190            (add_method)
191            ($this)
192            ($this_ty)
193            (
194                $this: $this_ty,
195                _: $crate::runtime::Sel,
196            )
197            ($($($params_rest)*)?)
198        }
199    };
200
201    // Class method
202    {
203        ($($__params:tt)*)
204        ($($params_rest:tt)*)
205
206        ($out_macro:path)
207        $($macro_args:tt)*
208    } => {
209        $out_macro! {
210            $($macro_args)*
211
212            (add_class_method)
213            (<Self as $crate::ClassType>::class())
214            (&$crate::runtime::AnyClass)
215            (
216                _: &$crate::runtime::AnyClass,
217                _: $crate::runtime::Sel,
218            )
219            ($($params_rest)*)
220        }
221    };
222}