Debian Codegraph @

debian/rust-vendor/link-section/src/section_parse.rs

raw
1 //! Declarative `#[section(...)]` parsing pipeline.
2 //!
3 //! The `#[section]` attribute generates a unique, contentless, ZST struct which
4 //! behaves like a value when we add a deref to it.
5 //!
6 //! Note from author: I don't recall the original reason for using this over a
7 //! true static type - this might be an artifact from an earlier design.
8 //!
9 //! The submission macro is used to determine the insertion for items into the
10 //! section. The macro shares the same name and module path as the section
11 //! struct, which allows us to use a single path to access both items.
12 //!
13 //! We need a proc macro to submit anything more than a "this section is this
14 //! type" argument because of the arcane macro definition rules (ie: any
15 //! exportable macro will end up in the crate root, so we need unique names).
16
17 #[macro_export]
18 #[doc(hidden)]
19 macro_rules! __section_parse {
20 ($($input:tt)*) => {
21 $crate::__perform!(
22 ($($input)*),
23 $crate::__chain[
24 $crate::__parallel[
25 $crate::__parse_item[$crate::__section_features],
26 $crate::__chain[
27 $crate::__extract_type,
28 $crate::__parse_type,
29 ],
30 ],
31 $crate::__section_parse_impl,
32 ]
33 );
34 };
35 }
36
37 #[macro_export]
38 #[doc(hidden)]
39 macro_rules! __in_section_parse {
40 ($($input:tt)*) => {
41 $crate::__perform!(
42 ($($input)*),
43 $crate::__chain[
44 $crate::__parallel[
45 $crate::__parse_item[$crate::__in_section_features],
46 $crate::__extract_type_assign,
47 ],
48 $crate::__in_section_parse_impl,
49 ]
50 );
51 };
52 }
53
54 #[macro_export]
55 #[doc(hidden)]
56 macro_rules! __extract_type {
57 (@entry next=$next:path[$next_args:tt], input=($(#[$meta:meta])* $vis:vis static $ident:ident : $($type_rest:tt)*) ) => {
58 $next!($next_args, ($($type_rest)*));
59 };
60 }
61
62 #[macro_export]
63 #[doc(hidden)]
64 macro_rules! __extract_type_assign {
65 (@entry next=$next:path[$next_args:tt], input=($(#[$meta:meta])* $vis:vis static $ident:tt : $type:ty = $($value:tt)*) ) => {
66 $next!($next_args, ($type));
67 };
68 (@entry next=$next:path[$next_args:tt], input=($(#[$meta:meta])* $vis:vis const $ident:tt : $type:ty = $($value:tt)*) ) => {
69 $next!($next_args, ($type));
70 };
71 (@entry next=$next:path[$next_args:tt], input=($(#[$meta:meta])* $vis:vis fn $ident:ident $args:tt $( -> $ret:ty )? $body:block )) => {
72 $next!($next_args, (fn $args $( -> $ret )?));
73 };
74 }
75
76 #[macro_export]
77 #[doc(hidden)]
78 macro_rules! __section_parse_impl {
79 (
80 @entry next=$next:path[$next_args:tt],
81 input=(
82 features = (
83 aux = $aux:tt: $aux_spec:ident,
84 crate_path = $crate_path:tt: $crate_path_spec:ident,
85 name = $name:tt: $name_spec:ident,
86 proc_macro = $proc_macro:tt: $proc_macro_spec:ident,
87 type = $section_type:tt: $section_type_spec:ident,
88 unsafe = $unsafe:tt: $unsafe_spec:ident,
89 ),
90 self = ( $($inner:tt)* ),
91 meta = ($(#[$meta:meta])*),
92 item = ($vis:vis static $ident:ident : $($type_rest:tt)*)
93 type = ($type:ty)
94 prefix = $prefix:tt
95 final = $final:ident
96 generics = $generics:tt
97 )
98 ) => {
99 // Validate the section type matches the attribute type
100 $crate::__section_parse_impl!(@validate type=$section_type final=$final);
101 // Validate the generic types match the section type
102 $crate::__section_parse_impl!(@validate type=$section_type generics=$generics);
103
104 // Parse the aux path so we can take its final segment
105 $crate::__parse_type!(@entry next=$crate::__section_parse_impl[[
106 @types features = (
107 type=$section_type,
108 macro=(
109 proc_macro=$proc_macro,
110 ),
111 definition=(self=$($inner)* meta=$($meta)* item=$vis static $ident : $($type_rest)*),
112 ident=$ident,
113 name=$name,
114 generic=$generics,
115 unsafe=$unsafe,
116 ),
117 item = ($(#[$meta])* $vis static $ident: $type),
118 ]], input=$aux
119 );
120 };
121
122 (@validate type=untyped final=Section) => {};
123 (@validate type=$type:tt final=Section) => { compile_error!("Use #[section(untyped)] to create an untyped section"); };
124 (@validate type=typed final=TypedSection) => {};
125 (@validate type=$type:tt final=TypedSection) => { compile_error!("Use #[section(typed)] to create a typed section"); };
126 (@validate type=mutable final=TypedMutableSection) => {};
127 (@validate type=$type:tt final=TypedMutableSection) => { compile_error!("Use #[section(mutable)] to create a mutable typed section"); };
128 (@validate type=movable final=TypedMovableSection) => {};
129 (@validate type=$type:tt final=TypedMovableSection) => { compile_error!("Use #[section(movable)] to create a movable typed section"); };
130 (@validate type=reference final=TypedReferenceSection) => {};
131 (@validate type=$type:tt final=TypedReferenceSection) => { compile_error!("Use #[section(reference)] to create a reference section"); };
132 (@validate type=$type:tt final=$final:ident) => { compile_error!(concat!("Unexpected section type: ", stringify!($type), " for section type: ", stringify!($final))); };
133
134 (@validate type=() generics=$generics:tt) => {};
135 (@validate type=untyped generics=()) => {};
136 (@validate type=$type:tt generics=($generic:ty)) => {};
137 (@validate type=$type:tt generics=($($generics:tt)*)) => {
138 compile_error!(concat!("Unexpected generic types for section type: ", stringify!($type), ": ", stringify!($($generics)*)));
139 };
140
141 // Missing type (will have already errored)
142 ([@types features = (
143 type=(),
144 $($rest:tt)*
145 ),
146 item = $item:tt,], $type:tt) => {};
147
148 // No aux
149 ([@types features = (
150 type=$section_type:ident,
151 macro=$macro:tt,
152 definition=$definition:tt,
153 ident=$ident:ident,
154 name=$name:tt,
155 generic=$generics:tt,
156 unsafe=$unsafe:tt,
157 ),
158 item = $item:tt,], (
159 type = ()
160 )) => {
161 $crate::__parse_type!(@entry next=$crate::__section_parse_impl[[
162 @types features = (
163 type=$section_type,
164 macro=$macro,
165 definition=$definition,
166 ident=$ident,
167 aux=(),
168 generic=$generics,
169 unsafe=$unsafe,
170 ),
171 item = $item,
172 ]], input=$name);
173 };
174
175 // Yes aux
176 ([@types features = (
177 type=$section_type:ident,
178 macro=$macro:tt,
179 definition=$definition:tt,
180 ident=$ident:ident,
181 name=$name:tt,
182 generic=$generics:tt,
183 unsafe=$unsafe:tt,
184 ),
185 item = $item:tt,], (
186 type = $type:tt
187 prefix = ($(:: $name_prefix:ident)? $($name_rest:ident ::)*)
188 final = $aux:ident
189 generics = ()
190 )) => {
191 $crate::__parse_type!(@entry next=$crate::__section_parse_impl[[
192 @types features = (
193 type=$section_type,
194 macro=$macro,
195 definition=$definition,
196 ident=$ident,
197 aux=($(:: $name_prefix)? $($name_rest ::)* $aux),
198 generic=$generics,
199 unsafe=$unsafe,
200 ),
201 item = $item,
202 ]], input=$name);
203 };
204
205 // No name
206 ([@types features = (
207 type=$section_type:ident,
208 macro=$macro:tt,
209 definition=$definition:tt,
210 ident=$ident:ident,
211 aux=$aux:tt,
212 generic=$generics:tt,
213 unsafe=$unsafe:tt,
214 ),
215 item = $item:tt,], (
216 type = ()
217 )) => {
218 $crate::__section_parse_impl!(
219 @generate features = (
220 type=$section_type,
221 macro=$macro,
222 definition=$definition,
223 aux=$aux,
224 name=($ident),
225 generic=$generics,
226 unsafe=$unsafe,
227 ),
228 item = $item,
229 );
230 };
231
232 // Yes name
233 ([@types features = (
234 type=$section_type:ident,
235 macro=$macro:tt,
236 definition=$definition:tt,
237 ident=$ident:ident,
238 aux=$aux:tt,
239 generic=$generics:tt,
240 unsafe=$unsafe:tt,
241 ),
242 item = $item:tt,], (
243 type = $type:tt
244 prefix = ($(:: $name_prefix:ident)? $($name_rest:ident ::)*)
245 final = $name:ident
246 generics = ()
247 )) => {
248 $crate::__section_parse_impl!(
249 @generate features = (
250 type=$section_type,
251 macro=$macro,
252 definition=$definition,
253 aux=$aux,
254 name=($(:: $name_prefix)? $($name_rest ::)* $name),
255 generic=$generics,
256 unsafe=$unsafe,
257 ),
258 item = $item,
259 );
260 };
261
262 // Finalize name/aux w/unsafe
263 (@generate
264 features = (
265 type=$section_type:ident,
266 macro=(
267 proc_macro=$proc_macro:tt,
268 ),
269 definition=$definition:tt,
270 aux=$aux:tt,
271 name=$name:tt,
272 generic=$generic_ty:ty,
273 unsafe=unsafe,
274 ),
275 item = $item:tt,
276 ) => {
277 $crate::__section_parse_impl!(
278 @generate features = (
279 type=$section_type,
280 macro=(
281 proc_macro=$proc_macro,
282 unsafe=unsafe,
283 args=(
284 section=($name $aux unsafe),
285 ),
286 ),
287 name=($name $aux unsafe),
288 generic=$generic_ty,
289 ),
290 item = $item,
291 );
292 };
293
294 // Finalize name/aux w/unsafe
295 (@generate
296 features = (
297 type=$section_type:ident,
298 macro=(
299 proc_macro=$proc_macro:tt,
300 ),
301 definition=$definition:tt,
302 aux=$aux:tt,
303 name=$name:tt,
304 generic=$generic_ty:ty,
305 unsafe=$unsafe:tt,
306 ),
307 item = $item:tt,
308 ) => {
309 $crate::__section_parse_impl!(
310 @generate features = (
311 type=$section_type,
312 macro=(
313 proc_macro=$proc_macro,
314 unsafe=$unsafe,
315 args=(
316 section=($name $aux $definition),
317 ),
318 ),
319 name=($name $aux $definition),
320 generic=$generic_ty,
321 ),
322 item = $item,
323 );
324 };
325
326 // ... and go
327 (@generate
328 features = (
329 type=$section_type:ident,
330 macro=$macro:tt,
331 name=$name:tt,
332 generic=$generic_ty:ty,
333 ),
334 item = ($(#[$meta:meta])* $vis:vis static $ident:ident: $collection:ty),
335 ) => {
336 $(#[$meta])*
337 #[allow(non_camel_case_types)]
338 $vis struct $ident;
339
340 $crate::__section_declare_submission_macro!(
341 [$]
342 macro=$macro
343 type=$section_type
344 vis=$vis
345 name=$ident
346 );
347
348 impl $ident {
349 /// Get a `const` reference to the underlying section. In
350 /// non-const contexts, `deref` is sufficient.
351 pub const fn const_deref(&self) -> &'static $collection {
352 static SECTION: $collection = {
353 let section = $crate::__support::get_section!($section_type, name=$name, type=$generic_ty);
354 let name = $crate::__support::section_name!(
355 string item data bare $name
356 );
357 $crate::__support::validate_section_name(name);
358 unsafe { <$collection>::new(name, section) }
359 };
360 &SECTION
361 }
362 }
363
364 impl ::core::fmt::Debug for $ident {
365 fn fmt(&self, f: &mut ::core::fmt::Formatter<'_>) -> ::core::fmt::Result {
366 ::core::ops::Deref::deref(self).fmt(f)
367 }
368 }
369
370 impl ::core::ops::Deref for $ident {
371 type Target = $collection;
372 fn deref(&self) -> &Self::Target {
373 self.const_deref()
374 }
375 }
376
377 $crate::__section_parse_impl!(@slice $section_type $ident $name : ($collection) ($generic_ty));
378 };
379
380 (@slice untyped $ident:ident $name:tt $($rest:tt)*) => {
381 impl $crate::__support::IsUntypedSection for $ident {}
382
383 const _: () = {
384 // Ensure that untyped data sections are never empty.
385 $crate::__add_section_link_attribute!(
386 item data section $name
387 #[link_section = __]
388 static __LINK_SECTION_CONST_ITEM: u8 = 0;
389 );
390 };
391 };
392
393 (@slice $section_type:ident $ident:ident $name:tt : ($collection:ty) ($generic_ty:ty)) => {
394 impl $crate::__support::SectionItemType for $ident {
395 type Item = $generic_ty;
396 }
397 impl $crate::__support::SectionItemTyped<$generic_ty> for $ident {
398 type Item = $generic_ty;
399 }
400 impl $ident {
401 /// Get the section as a slice.
402 pub fn as_slice(&self) -> &[$generic_ty] {
403 self.const_deref().as_slice()
404 }
405 }
406 impl ::core::iter::IntoIterator for $ident {
407 type Item = &'static $generic_ty;
408 type IntoIter = ::core::slice::Iter<'static, $generic_ty>;
409 fn into_iter(self) -> Self::IntoIter {
410 self.const_deref().as_slice().iter()
411 }
412 }
413 };
414
415 // ( $($bad:tt)* ) => {
416 // compile_error!(concat!("link-section: unexpected arguments `", stringify!($($bad)*), "` for `#[section]`"));
417 // };
418 }
419
420 /// `in_section!` expects a macro alias named like the section static; declare it unless
421 /// `no_macro` was set on the attribute.
422 #[macro_export]
423 #[doc(hidden)]
424 #[allow(clippy::crate_in_macro_def)]
425 macro_rules! __section_declare_submission_macro {
426 // No proc macro or unsafe, so we do nothing.
427 ([$dollar:tt] macro=(proc_macro=(), $($mrest:tt)*) $($rest:tt)*) => {};
428 ([$dollar:tt] macro=(proc_macro=proc_macro, unsafe=unsafe, $($mrest:tt)*) $($rest:tt)*) => {};
429
430 // If aux is specified, we need to use a custom macro. Either we use the
431 // proc macro, or we use the unique name provided by the user.
432
433 // Proc macro available.
434 ([$dollar:tt] macro=(proc_macro=proc_macro, unsafe=(), args=($($arg_name:ident = $arg_value:tt,)*),) type=$section_type:ident vis=$vis:vis name=$name:ident) => {
435 $crate::__support::combine!(output=ident span=$name
436 prefix=(#[allow(non_snake_case)] mod) input=(__ $name __ __FILE__(of=$name) _L __LINE__(of=$name) _C __COLUMN__(of=$name) _H __HASH__(string=($($arg_name = $arg_value,)*)) __private_macro) suffix=({
437 $crate::__support::combine!(output=ident span=$name
438 prefix=(#[macro_export]
439 #[allow(non_snake_case)]
440 #[doc(hidden)]
441 macro_rules!) input=(__ $name __ __FILE__(of=$name) _L __LINE__(of=$name) _C __COLUMN__(of=$name) _H __HASH__(string=($($arg_name = $arg_value,)*)) __private_macro) suffix=({
442 (($dollar ($args:tt)*)) => {
443 $crate::__in_section_crate!((@v=0 ; (source=section) ; (type=$section_type) $(; ($arg_name = $arg_value) )* ; $dollar ($args)*));
444 };
445 })
446 );
447
448 $crate::__support::combine!(output=ident span=$name
449 prefix=(#[doc(hidden)]
450 pub use ) input=(__ $name __ __FILE__(of=$name) _L __LINE__(of=$name) _C __COLUMN__(of=$name) _H __HASH__(string=($($arg_name = $arg_value,)*)) __private_macro) suffix=(as __MACRO;)
451 );
452 })
453 );
454
455 $crate::__support::combine!(output=ident span=$name
456 prefix=(#[allow(unused_imports)] #[doc(hidden)]
457 $vis use ) input=(__ $name __ __FILE__(of=$name) _L __LINE__(of=$name) _C __COLUMN__(of=$name) _H __HASH__(string=($($arg_name = $arg_value,)*)) __private_macro) suffix=(::__MACRO as $name;)
458 );
459 };
460
461 ($($rest:tt)*) => {
462 compile_error!(concat!(
463 "link-section: unexpected arguments `",
464 stringify!($($rest)*),
465 "` for `#[section]` submission macro declaration"
466 ));
467 };
468 }
469
470 #[macro_export]
471 #[doc(hidden)]
472 macro_rules! __section_parse_internal {
473 ( $features:path, $($input:tt)* ) => {
474 $crate::__perform!(
475 ($($input)*),
476 $crate::__chain[
477 $crate::__parse_item[$features],
478 $crate::__section_parse_impl,
479 ]
480 );
481 };
482 }
483
484 #[macro_export]
485 #[doc(hidden)]
486 macro_rules! __in_section_parse_impl {
487 ( @entry next=$next:path[$next_args:tt], input=(
488 features = (
489 aux = $aux:tt: $aux_spec:ident,
490 name = $name:tt: $name_spec:ident,
491 section = $section:tt: $section_spec:ident,
492 section_type = $section_type:tt: $type_spec:ident,
493 unsafe = $unsafe:tt: $unsafe_spec:ident,
494 ),
495 self = ( $($inner:tt)* ),
496 meta = $meta:tt,
497 item = $item:tt
498 $type:ty
499 )) => {
500 $crate::__in_section_parse_impl!(@dispatch features=(
501 section = $section,
502 raw = ($aux $name $section_type $unsafe)
503 ) meta=$meta item=$item);
504 };
505
506 (@dispatch features=(
507 section = (),
508 raw = ($aux:tt $name:tt $section_type:tt $unsafe:tt)
509 ) meta=$meta:tt item=$item:tt) => {
510 // Raw, feed directly to __in_section_crate!
511 $crate::__in_section_crate!((@v=0 ; (source=in_section) ; (type=$section_type) ; (section=($name $aux unsafe)) ; (meta=$meta) ; (item=$item)));
512 };
513
514 (@dispatch features=(
515 section = $section:tt,
516 raw = $raw:tt
517 ) meta=$meta:tt item=$item:tt) => {
518 $crate::__parse_type!(@entry next=$crate::__in_section_parse_impl[[@dispatch meta=$meta item=$item]], input=$section);
519 };
520
521 ([@dispatch meta=$meta:tt item=$item:tt], (type=($section:path) prefix=$prefix:tt final=$final:ident generics=$generics:tt)) => {
522 $section!(((path=$section) ; (name=$final) ; (meta=$meta) ; (item=$item)));
523 };
524
525 ($($input:tt)*) => {
526 compile_error!(concat!("link-section: unexpected arguments `", stringify!($($input)*), "` for `#[in_section]`"));
527 };
528 }