Skip to content

Commit c6970fe

Browse files
committed
Refactor how retain semantics are parsed
To make it easier to add an attribute like `#[method_family(init)]` in the future.
1 parent 4d6db79 commit c6970fe

File tree

5 files changed

+83
-44
lines changed

5 files changed

+83
-44
lines changed

crates/objc2/src/macros/__attribute_helpers.rs

Lines changed: 61 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,17 @@ macro_rules! __extract_and_apply_cfg_attributes {
6565
/// 1. The `method` or `method_id` attribute.
6666
/// (#[$method_or_method_id:ident($($sel:tt)*)])
6767
///
68-
/// 2. The `optional` attribute, if any.
68+
/// 2. The retain semantics, if any was present in the selector for
69+
/// `#[method_id(...)]`.
70+
///
71+
/// One of `New`, `Alloc`, `Init`, `CopyOrMutCopy` and `Other`.
72+
/// ($($retain_semantics:ident)?)
73+
///
74+
/// 3. The `optional` attribute, if any.
6975
/// ($(#[optional])?)
7076
///
71-
/// 3. The rest of the attributes.
72-
/// ($(#[$($m:tt)*])*)
77+
/// 4. The remaining attributes.
78+
/// ($(#[$($m_checked:tt)*])*)
7379
#[doc(hidden)]
7480
#[macro_export]
7581
macro_rules! __extract_custom_attributes {
@@ -82,9 +88,10 @@ macro_rules! __extract_custom_attributes {
8288
$crate::__extract_custom_attributes_inner! {
8389
($($m)*)
8490
// No already parsed attributes
85-
()
86-
()
87-
()
91+
() // method/method_id
92+
() // retain semantics
93+
() // optional
94+
() // checked
8895

8996
($out_macro)
9097
$($macro_args)*
@@ -101,6 +108,7 @@ macro_rules! __extract_custom_attributes_inner {
101108
()
102109
// And we found no `method` or `method_id` attributes
103110
()
111+
($($retain_semantics:tt)*)
104112
($($m_optional:tt)*)
105113
($($m_checked:tt)*)
106114

@@ -116,6 +124,7 @@ macro_rules! __extract_custom_attributes_inner {
116124
()
117125
// And we found a `method` or `method_id` attribute
118126
($($m_method:tt)*)
127+
($($retain_semantics:tt)*)
119128
($($m_optional:tt)*)
120129
($($m_checked:tt)*)
121130

@@ -127,6 +136,7 @@ macro_rules! __extract_custom_attributes_inner {
127136
$($macro_args)*
128137
// Append attributes to the end of the macro arguments
129138
($($m_method)*)
139+
($($retain_semantics)*)
130140
($($m_optional)*)
131141
($($m_checked)*)
132142
}
@@ -135,11 +145,12 @@ macro_rules! __extract_custom_attributes_inner {
135145
// `method` attribute
136146
{
137147
(
138-
#[method($($args:tt)*)]
148+
#[method($($sel:tt)*)]
139149
$($rest:tt)*
140150
)
141151
// If no existing `method` nor `method_id` attributes exist
142152
()
153+
($($retain_semantics:tt)*)
143154
($($m_optional:tt)*)
144155
($($m_checked:tt)*)
145156

@@ -149,7 +160,8 @@ macro_rules! __extract_custom_attributes_inner {
149160
$crate::__extract_custom_attributes_inner! {
150161
($($rest)*)
151162
// Add method attribute
152-
(#[method($($args)*)])
163+
(#[method($($sel)*)])
164+
($($retain_semantics)*)
153165
($($m_optional)*)
154166
($($m_checked)*)
155167

@@ -160,10 +172,11 @@ macro_rules! __extract_custom_attributes_inner {
160172
// Duplicate `method` attributes
161173
{
162174
(
163-
#[method($($args:tt)*)]
175+
#[method($($sel:tt)*)]
164176
$($rest:tt)*
165177
)
166178
($($m_method:tt)*)
179+
($($retain_semantics:tt)*)
167180
($($m_optional:tt)*)
168181
($($m_checked:tt)*)
169182

@@ -173,14 +186,42 @@ macro_rules! __extract_custom_attributes_inner {
173186
compile_error!("cannot specify the `method`/`method_id` attribute twice");
174187
};
175188

189+
// `method_id` attribute with retain semantics
190+
{
191+
(
192+
#[method_id(@__retain_semantics $retain_semantics:ident $($sel:tt)*)]
193+
$($rest:tt)*
194+
)
195+
// If no existing `method` nor `method_id` attributes exist
196+
()
197+
()
198+
($($m_optional:tt)*)
199+
($($m_checked:tt)*)
200+
201+
($out_macro:path)
202+
$($macro_args:tt)*
203+
} => {
204+
$crate::__extract_custom_attributes_inner! {
205+
($($rest)*)
206+
// Add method_id attribute
207+
(#[method_id($($sel)*)])
208+
($retain_semantics)
209+
($($m_optional)*)
210+
($($m_checked)*)
211+
212+
($out_macro)
213+
$($macro_args)*
214+
}
215+
};
176216
// `method_id` attribute
177217
{
178218
(
179-
#[method_id($($args:tt)*)]
219+
#[method_id($($sel:tt)*)]
180220
$($rest:tt)*
181221
)
182222
// If no existing `method` nor `method_id` attributes exist
183223
()
224+
($($retain_semantics:tt)*)
184225
($($m_optional:tt)*)
185226
($($m_checked:tt)*)
186227

@@ -190,21 +231,23 @@ macro_rules! __extract_custom_attributes_inner {
190231
$crate::__extract_custom_attributes_inner! {
191232
($($rest)*)
192233
// Add method_id attribute
193-
(#[method_id($($args)*)])
234+
(#[method_id($($sel)*)])
235+
($($retain_semantics)*)
194236
($($m_optional)*)
195237
($($m_checked)*)
196238

197239
($out_macro)
198240
$($macro_args)*
199241
}
200242
};
201-
// Duplicate `method` attributes
243+
// Duplicate `method_id` attributes
202244
{
203245
(
204-
#[method_id($($args:tt)*)]
246+
#[method_id($($sel:tt)*)]
205247
$($rest:tt)*
206248
)
207249
($($m_method:tt)*)
250+
($($retain_semantics:tt)*)
208251
($($m_optional:tt)*)
209252
($($m_checked:tt)*)
210253

@@ -221,6 +264,7 @@ macro_rules! __extract_custom_attributes_inner {
221264
$($rest:tt)*
222265
)
223266
($($m_method:tt)*)
267+
($($retain_semantics:tt)*)
224268
// If no existing `optional` attributes exist
225269
()
226270
($($m_checked:tt)*)
@@ -231,6 +275,7 @@ macro_rules! __extract_custom_attributes_inner {
231275
$crate::__extract_custom_attributes_inner! {
232276
($($rest)*)
233277
($($m_method)*)
278+
($($retain_semantics)*)
234279
// Add optional attribute
235280
(#[optional])
236281
($($m_checked)*)
@@ -246,6 +291,7 @@ macro_rules! __extract_custom_attributes_inner {
246291
$($rest:tt)*
247292
)
248293
($($m_method:tt)*)
294+
($($retain_semantics:tt)*)
249295
($($m_optional:tt)*)
250296
($($m_checked:tt)*)
251297

@@ -262,6 +308,7 @@ macro_rules! __extract_custom_attributes_inner {
262308
$($rest:tt)*
263309
)
264310
($($m_method:tt)*)
311+
($($retain_semantics:tt)*)
265312
($($m_optional:tt)*)
266313
($($m_checked:tt)*)
267314

@@ -271,6 +318,7 @@ macro_rules! __extract_custom_attributes_inner {
271318
$crate::__extract_custom_attributes_inner! {
272319
($($rest)*)
273320
($($m_method)*)
321+
($($retain_semantics)*)
274322
($($m_optional)*)
275323
(
276324
$($m_checked)*

crates/objc2/src/macros/__method_msg_send.rs

Lines changed: 3 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -157,47 +157,22 @@ macro_rules! __method_msg_send_id {
157157
// Selector with no arguments
158158
(
159159
($receiver:expr)
160-
($(@__retain_semantics $retain_semantics:ident)? $sel:ident)
160+
($sel:ident)
161161
()
162162

163163
()
164164
()
165-
// Possible to hit via. the MainThreadMarker branch
166-
($($already_parsed_retain_semantics:ident)?)
165+
($($retain_semantics:ident)?)
167166
) => {
168167
$crate::__msg_send_id_helper! {
169168
@(send_message_id)
170169
@($receiver)
171-
@($($retain_semantics)? $($already_parsed_retain_semantics)?)
170+
@($($retain_semantics)?)
172171
@($sel)
173172
@()
174173
}
175174
};
176175

177-
// Parse retain semantics
178-
//
179-
// Note: While this is not public, it is still a breaking change to change
180-
// this, since `icrate` relies on it.
181-
(
182-
($receiver:expr)
183-
(@__retain_semantics $retain_semantics:ident $($sel_rest:tt)*)
184-
($($args_rest:tt)*)
185-
186-
($($sel_parsed:tt)*)
187-
($($arg_parsed:tt)*)
188-
()
189-
) => {
190-
$crate::__method_msg_send_id! {
191-
($receiver)
192-
($($sel_rest)*)
193-
($($args_rest)*)
194-
195-
($($sel_parsed)*)
196-
($($arg_parsed)*)
197-
($retain_semantics)
198-
}
199-
};
200-
201176
// Skip using `MainThreadMarker` in the message send.
202177
//
203178
// This is a purely textual match, and using e.g.

crates/objc2/src/macros/declare_class.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -886,6 +886,7 @@ macro_rules! __declare_class_method_out {
886886
($($args_rest:tt)*)
887887

888888
($($m_method:tt)*)
889+
($($retain_semantics:tt)*)
889890
($($m_optional:tt)*)
890891
($($m_checked:tt)*)
891892
} => {
@@ -907,6 +908,7 @@ macro_rules! __declare_class_method_out {
907908
($($args_prefix)*)
908909

909910
($($m_method)*)
911+
($($retain_semantics)*)
910912
($($m_optional)*)
911913
($($m_checked)*)
912914
}
@@ -1010,6 +1012,7 @@ macro_rules! __declare_class_method_out_inner {
10101012
($($args_prefix:tt)*)
10111013

10121014
(#[method($($__sel:tt)*)])
1015+
()
10131016
($($__m_optional:tt)*)
10141017
($($m_checked:tt)*)
10151018

@@ -1041,6 +1044,7 @@ macro_rules! __declare_class_method_out_inner {
10411044
($($args_prefix:tt)*)
10421045

10431046
(#[method_id($($sel:tt)*)])
1047+
() // Specifying retain semantics is unsupported in declare_class! for now
10441048
($($__m_optional:tt)*)
10451049
($($m_checked:tt)*)
10461050

@@ -1083,6 +1087,7 @@ macro_rules! __declare_class_method_out_inner {
10831087
($($args_prefix:tt)*)
10841088

10851089
(#[method_id($($sel:tt)*)])
1090+
($($retain_semantics:tt)*)
10861091
($($__m_optional:tt)*)
10871092
($($m_checked:tt)*)
10881093

@@ -1127,6 +1132,7 @@ macro_rules! __declare_class_register_out {
11271132
($($args_rest:tt)*)
11281133

11291134
(#[method(dealloc)])
1135+
()
11301136
() // No optional
11311137
($($m_checked:tt)*)
11321138
} => {
@@ -1153,6 +1159,7 @@ macro_rules! __declare_class_register_out {
11531159
($($args_rest:tt)*)
11541160

11551161
(#[method($($sel:tt)*)])
1162+
()
11561163
() // No optional
11571164
($($m_checked:tt)*)
11581165
} => {
@@ -1186,6 +1193,7 @@ macro_rules! __declare_class_register_out {
11861193
($($args_rest:tt)*)
11871194

11881195
(#[method_id($($sel:tt)*)])
1196+
() // Retain semantics unsupported in declare_class!
11891197
() // No optional
11901198
($($m_checked:tt)*)
11911199
} => {
@@ -1219,6 +1227,7 @@ macro_rules! __declare_class_register_out {
12191227
($($args_rest:tt)*)
12201228

12211229
($($m_method:tt)*)
1230+
($($retain_semantics:tt)*)
12221231
($($m_optional:tt)*)
12231232
($($m_checked:tt)*)
12241233
} => {

crates/objc2/src/macros/extern_methods.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,7 @@ macro_rules! __extern_methods_method_out {
301301
($($args_rest:tt)*)
302302

303303
(#[method($($sel:tt)*)])
304+
()
304305
() // No `optional`
305306
($($m_checked:tt)*)
306307
} => {
@@ -335,6 +336,7 @@ macro_rules! __extern_methods_method_out {
335336
($($args_rest:tt)*)
336337

337338
(#[method_id($($sel:tt)*)])
339+
($($retain_semantics:tt)*)
338340
() // No `optional`
339341
($($m_checked:tt)*)
340342
} => {
@@ -352,7 +354,7 @@ macro_rules! __extern_methods_method_out {
352354

353355
()
354356
()
355-
()
357+
($($retain_semantics)*)
356358
}
357359
}
358360
}
@@ -370,6 +372,7 @@ macro_rules! __extern_methods_method_out {
370372
($($args_rest:tt)*)
371373

372374
($($m_method:tt)*)
375+
($($retain_semantics:tt)*)
373376
($($m_optional:tt)*)
374377
($($m_checked:tt)*)
375378
} => {

0 commit comments

Comments
 (0)