Skip to main content

ctor/
parse.rs

1//! Parser for the `#[ctor]` macro.
2
3#[macro_export]
4#[doc(hidden)]
5macro_rules! __ctor_parse {
6    ( $($input:tt)* ) => {
7        $crate::__perform!(
8            ($($input)*),
9            $crate::__chain[
10                $crate::__parse_item[$crate::__ctor_features],
11                $crate::__extract_unsafe,
12                $crate::__ctor_parse_impl,
13            ]
14        );
15    };
16}
17
18#[macro_export]
19#[doc(hidden)]
20macro_rules! __ctor_parse_internal {
21    ( $features:path, $($input:tt)* ) => {
22        $crate::__perform!(
23            ($($input)*),
24            $crate::__chain[
25                $crate::__parse_item[$features],
26                $crate::__extract_unsafe,
27                $crate::__ctor_parse_impl,
28            ]
29        );
30    };
31}
32
33/// Parse a processed `ctor` item. This is intentionally verbose to avoid
34/// excessive nesting of macro calls in user code.
35#[macro_export]
36#[doc(hidden)]
37macro_rules! __ctor_parse_impl {
38    // Step 1: Feature check
39
40    ( @entry next=$next:path[$next_args:tt], input=(
41        features = (
42            anonymous = $anonymous:tt: $anonymous_spec:ident,
43            body_link_section = $body_link_section:tt: $body_link_section_spec:ident,
44            crate_path = $crate_path:tt: $crate_path_spec:ident,
45            export_name_prefix = $export_name_prefix:tt: $export_name_prefix_spec:ident,
46            link_section = $link_section:tt: $link_section_spec:ident,
47            naked = $naked:tt: $naked_spec:ident,
48            priority = $priority:tt: $priority_spec:ident,
49            priority_enabled = $priority_enabled:tt: $priority_enabled_spec:ident,
50            proc_macro = $proc_macro:tt: $proc_macro_spec:ident,
51            std = $std:tt: $std_spec:ident,
52            r#unsafe = $no_fail_on_missing_unsafe:tt: $no_fail_on_missing_unsafe_spec:ident,
53            used_linker = $used_linker:tt: $used_linker_spec:ident,
54        ),
55        self = $self:tt,
56        meta = $meta:tt,
57        unsafe = $unsafe:tt,
58        item = $item:tt
59    )) => {
60        // Process and validate the priority
61        $crate::__map_priority!(
62            @entry next=$crate::__ctor_parse_impl[[next=$next[$next_args], input=(
63                features = (
64                    anonymous = $anonymous,
65                    linker_options = (
66                        body_link_section = $body_link_section,
67                        export_name_prefix = $export_name_prefix,
68                        link_section = $link_section,
69                        used_linker = $used_linker,
70                    ),
71                    no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe,
72                ),
73                self = $self,
74                meta = $meta,
75                unsafe = $unsafe,
76                item = $item
77            )]], input=(
78                export_name_prefix = ($export_name_prefix: $export_name_prefix_spec),
79                link_section = ($link_section: $link_section_spec),
80                naked = ($naked: $naked_spec),
81                priority = ($priority: $priority_spec),
82                priority_enabled = ($priority_enabled: $priority_enabled_spec),
83            )
84        );
85    };
86
87    ( [next=$next:path[$next_args:tt], input=(
88        features = (
89            anonymous = $anonymous:tt,
90            linker_options = $linker_options:tt,
91            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
92        ),
93        self = $self:tt,
94        meta = $meta:tt,
95        unsafe = $unsafe:tt,
96        item = $item:tt
97    )], $priority:tt ) => {
98        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
99            features = (
100                anonymous = $anonymous,
101                linker_options = $linker_options,
102                no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe,
103                priority = $priority,
104            ),
105            self = $self,
106            meta = $meta,
107            unsafe = $unsafe,
108            item = $item
109        ));
110    };
111
112    // Step 2: Check function shape
113    ( @entry next=$next:path[$next_args:tt], input=(
114        features = (
115            anonymous = $anonymous:tt,
116            linker_options = $linker_options:tt,
117            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
118            priority = $priority:tt,
119        ),
120        self = $self:tt,
121        meta = $meta:tt,
122        unsafe = ($($unsafe:tt)?),
123        item = ($vis:vis $(unsafe)? $( extern $abi:literal )? fn $name:ident () $( -> () )? {
124            $($body:tt)*
125        })
126    ) ) => {
127        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
128            features = (
129                anonymous = $anonymous,
130                linker_options = $linker_options,
131                link_name = $name,
132                no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe,
133                priority = $priority,
134            ),
135            self = $self,
136            meta = $meta,
137            unsafe = ($($unsafe)?),
138            item = ($vis $($unsafe)? $( extern $abi )? fn $name () {
139                $($body)*
140            })
141        ));
142    };
143
144    ( @entry next=$next:path[$next_args:tt], input=(
145        features = (
146            anonymous = $anonymous:tt,
147            linker_options = $linker_options:tt,
148            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
149            priority = $priority:tt,
150        ),
151        self = $self:tt,
152        meta = $meta:tt,
153        unsafe = $unsafe:tt,
154        item = ($vis:vis static $ident:ident : $ty:ty = $(unsafe)? { $literal:literal };)
155    ) ) => {
156        compile_error!("Trivial const expressions are not supported. Remove the #[ctor] and use a regular `static`.");
157    };
158
159    // Allow dynamic #[ctor]s - a const expression determines the actual functions.
160    ( @entry next=$next:path[$next_args:tt], input=(
161        features = (
162            anonymous = $anonymous:tt,
163            linker_options = $linker_options:tt,
164            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
165            priority = $priority:tt,
166        ),
167        self = $self:tt,
168        meta = $meta:tt,
169        unsafe = $unsafe:tt,
170        item = ($vis:vis static $ident:ident : &[fn()] = const $body:block;)
171    ) ) => {
172        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
173            features = (
174                anonymous = $anonymous,
175                linker_options = $linker_options,
176                link_name = $ident,
177                no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe,
178                priority = $priority,
179            ),
180            self = $self,
181            meta = $meta,
182            unsafe = $unsafe,
183            item = ($vis static $ident : &[fn()] = const $body;)
184        ));
185    };
186
187    ( @entry next=$next:path[$next_args:tt], input=(
188        features = (
189            anonymous = $anonymous:tt,
190            linker_options = $linker_options:tt,
191            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
192            priority = $priority:tt,
193        ),
194        self = $self:tt,
195        meta = $meta:tt,
196        unsafe = $unsafe:tt,
197        item = ($vis:vis static $ident:ident : $ty:ty = $(unsafe)? const $body:block;)
198    ) ) => {
199        compile_error!("Static const expressions are not supported. Remove the #[ctor] and use a regular `static`.");
200    };
201
202    ( @entry next=$next:path[$next_args:tt], input=(
203        features = (
204            anonymous = $anonymous:tt,
205            linker_options = $linker_options:tt,
206            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
207            priority = $priority:tt,
208        ),
209        self = $self:tt,
210        meta = $meta:tt,
211        unsafe = $unsafe:tt,
212        item = ($vis:vis static $ident:ident : $ty:ty = $(unsafe)? $literal:literal;)
213    ) ) => {
214        compile_error!("Trivial const expressions are not supported. Remove the #[ctor] and use a regular `static`.");
215    };
216
217    ( @entry next=$next:path[$next_args:tt], input=(
218        features = (
219            anonymous = $anonymous:tt,
220            linker_options = $linker_options:tt,
221            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
222            priority = $priority:tt,
223        ),
224        self = $self:tt,
225        meta = $meta:tt,
226        unsafe = $unsafe:tt,
227        item = ($vis:vis static $ident:ident : &'static dyn $($rest:tt)*)
228    ) ) => {
229        compile_error!("&'static dyn types are not supported. Use a Box<dyn ...> instead.");
230    };
231
232    ( @entry next=$next:path[$next_args:tt], input=(
233        features = (
234            anonymous = $anonymous:tt,
235            linker_options = $linker_options:tt,
236            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
237            priority = $priority:tt,
238        ),
239        self = $self:tt,
240        meta = $meta:tt,
241        unsafe = $unsafe:tt,
242        item = ($vis:vis static $ident:ident : &'static (dyn $($dyn:tt)*) $($rest:tt)*)
243    ) ) => {
244        compile_error!("&'static dyn types are not supported. Use a Box<dyn ...> instead.");
245    };
246
247    ( @entry next=$next:path[$next_args:tt], input=(
248        features = (
249            anonymous = $anonymous:tt,
250            linker_options = $linker_options:tt,
251            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
252            priority = $priority:tt,
253        ),
254        self = $self:tt,
255        meta = $meta:tt,
256        unsafe = ($($unsafe:tt)?),
257        item = ($vis:vis static $ident:ident : & $lt:lifetime $ty:ty = $($body:tt)*)
258    ) ) => {
259        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
260            features = (
261                anonymous = $anonymous,
262                linker_options = $linker_options,
263                link_name = $ident,
264                no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe,
265                priority = $priority,
266            ),
267            self = $self,
268            meta = $meta,
269            unsafe = ($($unsafe)?),
270            item = ($vis static $ident : & $lt $ty = $($body)*)
271        ));
272    };
273
274    ( @entry next=$next:path[$next_args:tt], input=(
275        features = (
276            anonymous = $anonymous:tt,
277            linker_options = $linker_options:tt,
278            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
279            priority = $priority:tt,
280        ),
281        self = $self:tt,
282        meta = $meta:tt,
283        unsafe = ($($unsafe:tt)?),
284        item = ($vis:vis static $ident:ident : $ty:ty = $($body:tt)*)
285    ) ) => {
286        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
287            features = (
288                anonymous = $anonymous,
289                linker_options = $linker_options,
290                link_name = $ident,
291                no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe,
292                priority = $priority,
293            ),
294            self = $self,
295            meta = $meta,
296            unsafe = ($($unsafe)?),
297            item = ($vis static $ident : $ty = $($body)*)
298        ));
299    };
300
301    ( @entry next=$next:path[$next_args:tt], input=(
302        features = (
303            anonymous = $anonymous:tt,
304            linker_options = $linker_options:tt,
305            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
306            priority = $priority:tt,
307        ),
308        self = $self:tt,
309        meta = $meta:tt,
310        unsafe = $unsafe:tt,
311        item = ($item:item)
312    ) ) => {
313        compile_error!("Invalid ctor item. \
314            Expected a function with no args, \
315            return value, or type parameters or a static variable.\n\
316            Valid forms are:\n\
317             - [pub] [unsafe] [extern $abi] fn $name() { ... }\n\
318             - static $name : [&'static] $ty = [unsafe] { ... };");
319    };
320
321    // Step 3: Compute no_fail_on_missing_unsafe
322
323    // Compile error iff no_fail_on_missing_unsafe is not present AND unsafe is not present
324    ( @entry next=$next:path[$next_args:tt], input=(
325        features = (
326            anonymous = $anonymous:tt,
327            linker_options = $linker_options:tt,
328            link_name = $link_name:tt,
329            no_fail_on_missing_unsafe = (),
330            priority = $priority:tt,
331        ),
332        self = ( $macro_name:ident ),
333        meta = $meta:tt,
334        unsafe = (),
335        item = $item:tt
336    ) ) => {
337        compile_error!(concat!("Missing unsafe keyword in #[ctor] annotation. \
338        Use #[ctor(unsafe)]. This error can be suppressed by passing \
339        `--cfg linktime_no_fail_on_missing_unsafe` in `RUSTFLAGS` or placing this in your \
340        `config.toml` file.\n\n\
341        \n\
342        #[ctor]\n\
343        ^^^^^^^------- replace this with #[ctor(unsafe)]"));
344    };
345
346    ( @entry next=$next:path[$next_args:tt], input=(
347        features = (
348            anonymous = $anonymous:tt,
349            linker_options = $linker_options:tt,
350            link_name = $link_name:tt,
351            no_fail_on_missing_unsafe = (),
352            priority = $priority:tt,
353        ),
354        self = ($macro_name:ident ($($self:tt)*) ),
355        meta = $meta:tt,
356        unsafe = (),
357        item = $item:tt
358    ) ) => {
359        compile_error!(concat!("Missing unsafe keyword in #[ctor] annotation. \
360        Use #[ctor(unsafe, ", stringify!($($self)*), ")]. This error can be suppressed by passing \
361        `--cfg linktime_no_fail_on_missing_unsafe` in `RUSTFLAGS` or placing this in your \
362        `config.toml` file.\n\n\
363        \n\
364        #[ctor(", stringify!($($self)*), ")]\n\
365        ^------- replace this with #[ctor(unsafe, ", stringify!($($self)*), ")]\n"));
366    };
367
368    ( @entry next=$next:path[$next_args:tt], input=(
369        features = (
370            anonymous = $anonymous:tt,
371            linker_options = $linker_options:tt,
372            link_name = $link_name:tt,
373            no_fail_on_missing_unsafe = $no_fail_on_missing_unsafe:tt,
374            priority = $priority:tt,
375        ),
376        self = $self:tt,
377        meta = $meta:tt,
378        unsafe = $unsafe:tt,
379        item = $item:tt
380    ) ) => {
381        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
382            features = (
383                anonymous = $anonymous,
384                linker_options = $linker_options,
385                link_name = $link_name,
386                priority = $priority,
387            ),
388            meta = $meta,
389            unsafe = $unsafe,
390            item = $item
391        ));
392    };
393
394    // Step 4: Wrap in anonymous const
395    ( @entry next=$next:path[$next_args:tt], input=(
396        features = (
397            anonymous = (),
398            linker_options = $linker_options:tt,
399            link_name = $link_name:tt,
400            priority = $priority:tt,
401        ),
402        meta = $meta:tt,
403        unsafe = $unsafe:tt,
404        item = $item:tt
405    ) ) => {
406        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
407            features = (
408                linker_options = $linker_options,
409                link_name = $link_name,
410                priority = $priority,
411            ),
412            meta = $meta,
413            unsafe = $unsafe,
414            item = $item
415        ));
416    };
417    ( @entry next=$next:path[$next_args:tt], input=(
418        features = (
419            anonymous = anonymous,
420            linker_options = $linker_options:tt,
421            link_name = $link_name:tt,
422            priority = $priority:tt,
423        ),
424        meta = $meta:tt,
425        unsafe = $unsafe:tt,
426        item = $item:tt
427    ) ) => {
428        const _: () = {
429            $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
430                features = (
431                    linker_options = $linker_options,
432                    link_name = $link_name,
433                    priority = $priority,
434                ),
435                meta = $meta,
436                unsafe = $unsafe,
437                item = $item
438            ));
439        };
440    };
441
442    // Step 5: Compute used_linker
443    ( @entry next=$next:path[$next_args:tt], input=(
444        features = (
445            linker_options = (
446                body_link_section = $body_link_section:tt,
447                export_name_prefix = $export_name_prefix:tt,
448                link_section = $link_section:tt,
449                used_linker = (),
450            ),
451            link_name = $link_name:tt,
452            priority = $priority:tt,
453        ),
454        meta = $meta:tt,
455        unsafe = $unsafe:tt,
456        item = $item:tt
457    ) ) => {
458        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
459            features = (
460                linker_options = (
461                    body_link_section = $body_link_section,
462                    export_name_prefix = $export_name_prefix,
463                    link_section = $link_section,
464                    used_linker_meta = (#[used]),
465                ),
466                link_name = $link_name,
467                priority = $priority,
468            ),
469            meta = $meta,
470            unsafe = $unsafe,
471            item = $item
472        ));
473    };
474
475    ( @entry next=$next:path[$next_args:tt], input=(
476        features = (
477            linker_options = (
478                body_link_section = $body_link_section:tt,
479                export_name_prefix = $export_name_prefix:tt,
480                link_section = $link_section:tt,
481                used_linker = used_linker,
482            ),
483            link_name = $link_name:tt,
484            priority = $priority:tt,
485        ),
486        meta = $meta:tt,
487        unsafe = $unsafe:tt,
488        item = $item:tt
489    ) ) => {
490        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
491            features = (
492                linker_options = (
493                    body_link_section = $body_link_section,
494                    export_name_prefix = $export_name_prefix,
495                    link_section = $link_section,
496                    used_linker_meta = (#[used(linker)]),
497                ),
498                link_name = $link_name,
499                priority = $priority,
500            ),
501            meta = $meta,
502            unsafe = $unsafe,
503            item = $item
504        ));
505    };
506
507    // Step 6: Compute export name suffix
508
509    // No prefix, no computation
510    ( @entry next=$next:path[$next_args:tt], input=(
511        features = (
512            linker_options = (
513                body_link_section = $body_link_section:tt,
514                export_name_prefix = (),
515                link_section = $link_section:tt,
516                used_linker_meta = $used_linker_meta:tt,
517            ),
518            link_name = $link_name:tt,
519            priority = $priority:tt,
520        ),
521        meta = $meta:tt,
522        unsafe = $unsafe:tt,
523        item = $item:tt
524    ) ) => {
525        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
526            features = (
527                linker_options = (
528                    body_link_section = $body_link_section,
529                    export_name = (),
530                    link_section = $link_section,
531                    used_linker_meta = $used_linker_meta,
532                ),
533                priority = $priority,
534            ),
535            meta = $meta,
536            unsafe = $unsafe,
537            item = $item
538        ));
539    };
540
541    ( @entry next=$next:path[$next_args:tt], input=(
542        features = (
543            linker_options = (
544                body_link_section = $body_link_section:tt,
545                export_name_prefix = $export_name_prefix:tt,
546                link_section = $link_section:tt,
547                used_linker_meta = $used_linker_meta:tt,
548            ),
549            link_name = $link_name:tt,
550            priority = $priority:tt,
551        ),
552        meta = $meta:tt,
553        unsafe = $unsafe:tt,
554        item = $item:tt
555    ) ) => {
556        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
557            features = (
558                linker_options = (
559                    body_link_section = $body_link_section,
560                    export_name = (($export_name_prefix), ("_", env!("CARGO_PKG_NAME"), "_",
561                        ::core::module_path!(), "_",
562                        stringify!($link_name),
563                        "_L", line!(), "C", column!())),
564                    link_section = $link_section,
565                    used_linker_meta = $used_linker_meta,
566                ),
567                priority = $priority,
568            ),
569            meta = $meta,
570            unsafe = $unsafe,
571            item = $item
572        ));
573    };
574
575    // Step 7: Compute priority
576
577    // naked with no export name
578    ( @entry next=$next:path[$next_args:tt], input=(
579        features = (
580            linker_options = (
581                body_link_section = $body_link_section:tt,
582                export_name = (),
583                link_section = $link_section:tt,
584                used_linker_meta = $used_linker_meta:tt,
585            ),
586            priority = naked,
587        ),
588        meta = $meta:tt,
589        unsafe = $unsafe:tt,
590        item = $item:tt
591    ) ) => {
592        // UEFI has nothing to run a naked `.init_array` entry, so collect it at
593        // the default priority instead.
594        #[cfg(target_os = "uefi")]
595        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
596            link_args = (
597                body_link_section = $body_link_section,
598                export_name = (),
599                priority = 500,
600                used = $used_linker_meta,
601            ),
602            meta = $meta,
603            unsafe = $unsafe,
604            item = $item
605        ));
606
607        #[cfg(not(target_os = "uefi"))]
608        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
609            link_args = (
610                body_link_section = $body_link_section,
611                export_name = (),
612                link_section = ($link_section),
613                used = $used_linker_meta,
614            ),
615            meta = $meta,
616            unsafe = $unsafe,
617            item = $item
618        ));
619    };
620
621    // naked with export name
622    ( @entry next=$next:path[$next_args:tt], input=(
623        features = (
624            linker_options = (
625                body_link_section = $body_link_section:tt,
626                export_name = (($($prefix:tt)*), ($($suffix:tt)*)),
627                link_section = $link_section:tt,
628                used_linker_meta = $used_linker_meta:tt,
629            ),
630            priority = naked,
631        ),
632        meta = $meta:tt,
633        unsafe = $unsafe:tt,
634        item = $item:tt
635    ) ) => {
636        // AIX uses 80000000 as the priority
637        #[cfg(target_os = "aix")]
638        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
639            link_args = (
640                body_link_section = $body_link_section,
641                export_name = (concat!($($prefix)*, "80000000", $($suffix)*)),
642                link_section = ($link_section),
643                used = $used_linker_meta,
644            ),
645            meta = $meta,
646            unsafe = $unsafe,
647            item = $item
648        ));
649
650        #[cfg(not(target_os = "aix"))]
651        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
652            link_args = (
653                body_link_section = $body_link_section,
654                export_name = (concat!($($prefix)*, $($suffix)*)),
655                link_section = ($link_section),
656                used = $used_linker_meta,
657            ),
658            meta = $meta,
659            unsafe = $unsafe,
660            item = $item
661        ));
662    };
663
664    ( @entry next=$next:path[$next_args:tt], input=(
665        features = (
666            linker_options = (
667                body_link_section = $body_link_section:tt,
668                export_name = $export_name:tt,
669                link_section = $link_section:tt,
670                used_linker_meta = $used_linker_meta:tt,
671            ),
672            priority = $priority:tt,
673        ),
674        meta = $meta:tt,
675        unsafe = $unsafe:tt,
676        item = $item:tt
677    ) ) => {
678        // Apple can't order .init_array by priority, and UEFI never runs it at
679        // all. Both collect constructors into one section, then sort and run them.
680        #[cfg(any(target_vendor = "apple", target_os = "uefi"))]
681        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
682            link_args = (
683                body_link_section = $body_link_section,
684                export_name = $export_name,
685                priority = $priority,
686                used = $used_linker_meta,
687            ),
688            meta = $meta,
689            unsafe = $unsafe,
690            item = $item
691        ));
692
693        // Get a priority literal
694        #[cfg(not(any(target_vendor = "apple", target_os = "uefi")))]
695        $crate::__priority_to_literal!($crate::__ctor_parse_impl,[
696            @priority next=$next[$next_args],
697            features = (
698                body_link_section = $body_link_section,
699                export_name = $export_name,
700                link_section = $link_section,
701                used_linker_meta = $used_linker_meta,
702            ),
703            meta = $meta,
704            unsafe = $unsafe,
705            item = $item
706        ] = $priority);
707    };
708
709    ( @entry next=$next:path[$next_args:tt], input=(
710        features = (
711            linker_options = (
712                body_link_section = $body_link_section:tt,
713                export_name = $export_name:tt,
714                link_section = $link_section:tt,
715                used_linker_meta = $used_linker_meta:tt,
716            ),
717            priority = $priority:tt,
718        ),
719        meta = $meta:tt,
720        unsafe = $unsafe:tt,
721        item = $item:tt
722    ) ) => {
723        compile_error!(concat!("Invalid priority: ", stringify!($priority)));
724    };
725
726    ( [@priority next=$next:path[$next_args:tt],
727        features = (
728            body_link_section = $body_link_section:tt,
729            export_name = (),
730            link_section = $link_section:tt,
731            used_linker_meta = $used_linker_meta:tt,
732        ),
733        meta = $meta:tt,
734        unsafe = $unsafe:tt,
735        item = $item:tt
736    ], ($($priority:tt)*)) => {
737        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
738            link_args = (
739                body_link_section = $body_link_section,
740                export_name = (),
741                link_section = (concat!($link_section, ".", $($priority)*)),
742                used = $used_linker_meta,
743            ),
744            meta = $meta,
745            unsafe = $unsafe,
746            item = $item
747        ));
748    };
749
750    ( [@priority next=$next:path[$next_args:tt],
751        features = (
752            body_link_section = $body_link_section:tt,
753            export_name = (($($prefix:tt)*), ($($suffix:tt)*)),
754            link_section = $link_section:tt,
755            used_linker_meta = $used_linker_meta:tt,
756        ),
757        meta = $meta:tt,
758        unsafe = $unsafe:tt,
759        item = $item:tt
760    ], ($($priority:tt)*)) => {
761        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
762            link_args = (
763                body_link_section = $body_link_section,
764                export_name = (concat!($($prefix)*, $($priority)*, $($suffix)*)),
765                link_section = (concat!($link_section, ".", $($priority)*)),
766                used = $used_linker_meta,
767            ),
768            meta = $meta,
769            unsafe = $unsafe,
770            item = $item
771        ));
772    };
773
774    // Step 8: Compute body link section meta
775    ( @entry next=$next:path[$next_args:tt], input=(
776        link_args = (
777            body_link_section = $($body_link_section:literal)? $( () )?,
778            $($link_args:tt)*
779        ),
780        meta = $meta:tt,
781        unsafe = $unsafe:tt,
782        item = $item:tt
783    ) ) => {
784        $crate::__ctor_parse_impl!(@entry next=$next[$next_args], input=(
785            link_args = (
786                body_link_meta = ( $([link_section = $body_link_section])? ),
787                $($link_args)*
788            ),
789            body_link_meta = ( $([link_section = $body_link_section])? ),
790            meta = $meta,
791            unsafe = $unsafe,
792            item = $item
793        ));
794    };
795
796    // Step 9: Delegate on item type
797    ( @entry next=$next:path[$next_args:tt], input=(
798        link_args = $link_args:tt,
799        body_link_meta = ($($body_link_meta:tt)?),
800        meta = ($($meta:tt)*),
801        unsafe = ($($unsafe:tt)*),
802        item = ($vis:vis $(unsafe)? $( extern $abi:literal )? fn $name:ident () $( -> () )? {
803            $($body:tt)*
804        })
805    ) ) => {
806        $($meta)*
807        #[allow(dead_code)]
808        $vis $($unsafe)* $( extern $abi )? fn $name () {
809            // The outer function may be attached to a struct, so we generate an
810            // inner function that is freestanding and call it from both places.
811            #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
812            $(#[allow(unsafe_code)] #$body_link_meta)?
813            $($unsafe)* $( extern $abi )? fn __ctor_private_inner() {
814                $($body)*
815            }
816
817            $crate::__ctor_parse_impl!(@ctor $link_args body={ $($unsafe)* { __ctor_private_inner() } });
818            $($unsafe)* { __ctor_private_inner() }
819        }
820    };
821
822    ( @entry next=$next:path[$next_args:tt], input=(
823        link_args = $link_args:tt,
824        body_link_meta = ($($body_link_meta:tt)?),
825        meta = ($($meta:tt)*),
826        unsafe = ($($unsafe:tt)*),
827        item = ($vis:vis static $ident:ident : &[fn()] = const $body:block;)
828    ) ) => {
829        $($meta)*
830        $vis static $ident: &[fn()] = /*const*/ {
831            const __EXTERN_C_FNS: [extern "C" fn(); $ident.len()] = {
832                use ::core::mem::MaybeUninit;
833                let mut array: MaybeUninit<[extern "C" fn(); $ident.len()]> = MaybeUninit::uninit();
834                let mut array_ptr: *mut extern "C" fn() = array.as_mut_ptr() as _;
835
836                extern "C" fn bind_array<const N: usize>() {
837                    $ident[N]()
838                }
839
840                unsafe {
841                    let array_ptr = array.as_mut_ptr() as *mut extern "C" fn();
842                    const LEN: usize = $ident.len();
843                    if LEN > 0 { array_ptr.add(0).write(bind_array::<0>); }
844                    if LEN > 1 { array_ptr.add(1).write(bind_array::<1>); }
845                    if LEN > 2 { array_ptr.add(2).write(bind_array::<2>); }
846                    if LEN > 3 { array_ptr.add(3).write(bind_array::<3>); }
847                    if LEN > 4 { array_ptr.add(4).write(bind_array::<4>); }
848                    if LEN > 5 { array_ptr.add(5).write(bind_array::<5>); }
849                    if LEN > 6 { array_ptr.add(6).write(bind_array::<6>); }
850                    if LEN > 7 { array_ptr.add(7).write(bind_array::<7>); }
851                    if LEN > 8 { array_ptr.add(8).write(bind_array::<8>); }
852                    if LEN > 9 { array_ptr.add(9).write(bind_array::<9>); }
853                    if LEN > 10 { array_ptr.add(10).write(bind_array::<10>); }
854                    if LEN > 11 { array_ptr.add(11).write(bind_array::<11>); }
855                    if LEN > 12 { array_ptr.add(12).write(bind_array::<12>); }
856                    if LEN > 13 { array_ptr.add(13).write(bind_array::<13>); }
857                    if LEN > 14 { array_ptr.add(14).write(bind_array::<14>); }
858                    if LEN > 15 { array_ptr.add(15).write(bind_array::<15>); }
859                    if LEN > 16 {
860                        panic!("Unexpected array length, expected <= 16");
861                    }
862                }
863                unsafe { array.assume_init() }
864            };
865            $crate::__ctor_parse_impl!(@ctor $link_args fns=__EXTERN_C_FNS);
866
867            $body
868        };
869    };
870
871    ( @entry next=$next:path[$next_args:tt], input=(
872        link_args = $link_args:tt,
873        body_link_meta = ($($body_link_meta:tt)?),
874        meta = ($($meta:tt)*),
875        unsafe = ($($unsafe:tt)*),
876        item = ($vis:vis static $ident:ident : & $lt:lifetime $ty:ty = &$($body:tt)*)
877    ) ) => {
878        $($meta)*
879        $vis static $ident: & $lt $crate::statics::Static<$ty> = {
880            #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
881            $(#[allow(unsafe_code)] #$body_link_meta)?
882            fn init() -> $ty {
883                return $($body)*
884            }
885
886            static __STATIC_CTOR: $crate::statics::Static<$ty> = {
887                unsafe { $crate::statics::Static::<$ty>::new(init) }
888            };
889            &__STATIC_CTOR
890        };
891        $crate::__ctor_parse_impl!(@ctor $link_args body={ _ = &**$ident } );
892    };
893
894    ( @entry next=$next:path[$next_args:tt], input=(
895        link_args = $link_args:tt,
896        body_link_meta = ($($body_link_meta:tt)?),
897        meta = ($($meta:tt)*),
898        unsafe = ($($unsafe:tt)*),
899        item = ($vis:vis static $ident:ident : & $lt:lifetime $ty:ty = $($body:tt)*)
900    ) ) => {
901        $($meta)*
902        $vis static $ident: $crate::statics::Static<&'static $ty> = {
903            #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
904            $(#[allow(unsafe_code)] #$body_link_meta)?
905            fn init() -> &'static $ty {
906                return $($body)*
907            }
908            unsafe { $crate::statics::Static::<&'static $ty>::new(init) }
909        };
910        $crate::__ctor_parse_impl!(@ctor $link_args body={ _ = &*$ident } );
911    };
912
913    ( @entry next=$next:path[$next_args:tt], input=(
914        link_args = $link_args:tt,
915        body_link_meta = ($($body_link_meta:tt)?),
916        meta = ($($meta:tt)*),
917        unsafe = ($($unsafe:tt)*),
918        item = ($vis:vis static $ident:ident : $ty:ty = $($body:tt)*)
919    ) ) => {
920        $($meta)*
921        $vis static $ident: $crate::statics::Static<$ty> = {
922            #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
923            $(#[allow(unsafe_code)] #$body_link_meta)?
924            fn init() -> $ty {
925                return $($body)*
926            }
927            unsafe { $crate::statics::Static::<$ty>::new(init) }
928        };
929        $crate::__ctor_parse_impl!(@ctor $link_args body={ _ = &*$ident } );
930    };
931
932    // ctor definitions
933
934    // linux-style, one ctor
935    ( @ctor (
936        body_link_meta = ($($body_link_meta:tt)?),
937        export_name=(),
938        link_section=($($link_section:tt)*),
939        used=(#$used_linker_meta:tt),
940     ) body=$body:tt ) => {
941        const _: () = {
942            #[allow(unsafe_code)]
943            #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
944            #[link_section = $($link_section)*]
945            #$used_linker_meta
946            static __CTOR_PRIVATE_REF: unsafe extern "C" fn() = {
947                #[allow(unused_unsafe)]
948                $(#[allow(unsafe_code)] #$body_link_meta)?
949                extern "C" fn __ctor_private() {
950                    #[cfg(all(target_family = "wasm", target_os = "unknown"))]
951                    {
952                        static DISARMED: ::core::sync::atomic::AtomicBool = ::core::sync::atomic::AtomicBool::new(false);
953                        if DISARMED.swap(true, ::core::sync::atomic::Ordering::Relaxed) {
954                            return;
955                        }
956                    }
957                    $body
958                }
959                __ctor_private
960            };
961        };
962    };
963
964    // linux-style, multiple ctor
965    ( @ctor (
966        body_link_meta = ($($body_link_meta:tt)?),
967        export_name=(),
968        link_section=($($link_section:tt)*),
969        used=(#$used_linker_meta:tt),
970     ) fns=$ident:ident ) => {
971        #[allow(unsafe_code)]
972        #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
973        #[link_section = $($link_section)*]
974        #$used_linker_meta
975        static __CTOR_PRIVATE_REF: unsafe extern "C" fn() = {
976            #[allow(unused_unsafe)]
977            $(#[allow(unsafe_code)] #$body_link_meta)?
978            extern "C" fn __ctor_private() {
979                #[cfg(all(target_family = "wasm", target_os = "unknown"))]
980                {
981                    static DISARMED: ::core::sync::atomic::AtomicBool = ::core::sync::atomic::AtomicBool::new(false);
982                    if DISARMED.swap(true, ::core::sync::atomic::Ordering::Relaxed) {
983                        return;
984                    }
985                }
986                for f in $ident {
987                    f();
988                }
989            }
990            __ctor_private
991        };
992    };
993
994    // "collect"-style, one ctor
995    ( @ctor (
996        body_link_meta = ($($body_link_meta:tt)?),
997        export_name=(),
998        priority=$priority:tt,
999        used=(#$used_linker_meta:tt),
1000     ) body=$body:tt ) => {
1001        const _: () = {
1002            #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
1003            #[allow(unsafe_code, unused_unsafe)]
1004            $(#$body_link_meta)?
1005            extern "C" fn __ctor_private() {
1006                #[cfg(all(target_family = "wasm", target_os = "unknown"))]
1007                {
1008                    static DISARMED: ::core::sync::atomic::AtomicBool = ::core::sync::atomic::AtomicBool::new(false);
1009                    if DISARMED.swap(true, ::core::sync::atomic::Ordering::Relaxed) {
1010                        return;
1011                    }
1012                }
1013                $body
1014            }
1015
1016            $crate::__register_ctor!(priority = $priority, fn = __ctor_private);
1017        };
1018    };
1019
1020    // "collect"-style, multiple ctors
1021    ( @ctor (
1022        body_link_meta = ($($body_link_meta:tt)?),
1023        export_name=(),
1024        priority=$priority:tt,
1025        used=(#$used_linker_meta:tt),
1026     ) fns=$ident:ident ) => {
1027        const _: () = {
1028            $crate::__register_ctor!(priority = $priority, fn = (array $ident));
1029        };
1030    };
1031
1032    // AIX-style, one ctor
1033    ( @ctor (
1034        body_link_meta = ($($body_link_meta:tt)?),
1035        export_name=($($link_name:tt)*),
1036        link_section=$link_section:tt,
1037        used=(#$used_linker_meta:tt),
1038     ) body=$body:tt ) => {
1039        const _: () = {
1040            #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
1041            #[allow(unused_unsafe, unsafe_code)]
1042            #[no_mangle]
1043            #[export_name = $($link_name)*]
1044            $(#$body_link_meta)?
1045            extern "C" fn __ctor_private() {
1046                #[cfg(all(target_family = "wasm", target_os = "unknown"))]
1047                {
1048                    static DISARMED: ::core::sync::atomic::AtomicBool = ::core::sync::atomic::AtomicBool::new(false);
1049                    if DISARMED.swap(true, ::core::sync::atomic::Ordering::Relaxed) {
1050                        return;
1051                    }
1052                }
1053                $body
1054            }
1055        };
1056    };
1057
1058    // AIX-style, multiple ctor
1059    ( @ctor (
1060        body_link_meta = ($($body_link_meta:tt)?),
1061        export_name=($($link_name:tt)*),
1062        link_section=$link_section:tt,
1063        used=(#$used_linker_meta:tt),
1064     ) fns=$ident:ident ) => {
1065        const _: () = {
1066            #[cfg_attr(clippy, allow(unknown_lints, unsafe_attr_outside_unsafe))]
1067            #[allow(unused_unsafe, unsafe_code)]
1068            #[no_mangle]
1069            #[export_name = $($link_name)*]
1070            $(#$body_link_meta)?
1071            extern "C" fn __ctor_private() {
1072                #[cfg(all(target_family = "wasm", target_os = "unknown"))]
1073                {
1074                    static DISARMED: ::core::sync::atomic::AtomicBool = ::core::sync::atomic::AtomicBool::new(false);
1075                    if DISARMED.swap(true, ::core::sync::atomic::Ordering::Relaxed) {
1076                        return;
1077                    }
1078                }
1079                for f in $ident {
1080                    f();
1081                }
1082            }
1083        };
1084    };
1085
1086    (@ctor $features:tt body=$body:tt) => {
1087        compile_error!(concat!("Invalid ctor features: ", stringify!($features)));
1088    };
1089}
1090
1091/// Map the priority input to a priority value. This is somewhat complex because
1092/// the default for priority changes based on whether priority is enabled and
1093/// whether link options are specified.
1094#[macro_export]
1095#[doc(hidden)]
1096macro_rules! __map_priority {
1097    // Priority specified, priority not enabled
1098    ( @entry next=$next:path[$next_args:tt], input=(
1099        export_name_prefix = $enp:tt,
1100        link_section = $ls:tt,
1101        naked = $naked:tt,
1102        priority = ($priority:tt: value),
1103        priority_enabled = ((), $pe_spec:ident),
1104    ) ) => {
1105        // UEFI always collects, so it honors a priority without the feature.
1106        #[cfg(target_os = "uefi")]
1107        $next!($next_args, $priority);
1108
1109        #[cfg(not(target_os = "uefi"))]
1110        const _: () = {
1111            compile_error!(concat!("The crate \"priority\" feature was not enabled: `priority = ", stringify!($priority), "` is not supported."));
1112        };
1113    };
1114
1115    // Priority unspecified, link options not, priority not enabled => naked
1116    ( @entry next=$next:path[$next_args:tt], input=(
1117        export_name_prefix = ($enp:tt: default),
1118        link_section = ($ls:tt: default),
1119        naked = ($naked:tt: default),
1120        priority = ($priority:tt: default),
1121        priority_enabled = ((): $pe_spec:ident),
1122    ) ) => {
1123        $next!($next_args, naked);
1124    };
1125
1126    // Priority unspecified, link options not, priority enabled => default (500)
1127    ( @entry next=$next:path[$next_args:tt], input=(
1128        export_name_prefix = ($enp:tt: default),
1129        link_section = ($ls:tt: default),
1130        naked = ($naked:tt: default),
1131        priority = ($priority:tt: default),
1132        priority_enabled = (priority_enabled: $pe_spec:ident),
1133    ) ) => {
1134        $next!($next_args, 500);
1135    };
1136
1137    // Priority specified (or default) = early, link options not, priority enabled
1138    ( @entry next=$next:path[$next_args:tt], input=(
1139        export_name_prefix = ($enp:tt: default),
1140        link_section = ($ls:tt: default),
1141        naked = ($naked:tt: default),
1142        priority = (early: $p_spec:ident),
1143        priority_enabled = $pe:tt,
1144    ) ) => {
1145        $next!($next_args, 101);
1146    };
1147
1148    // Priority specified (or default) = default, link options not, priority enabled
1149    ( @entry next=$next:path[$next_args:tt], input=(
1150        export_name_prefix = ($enp:tt: default),
1151        link_section = ($ls:tt: default),
1152        naked = ($naked:tt: default),
1153        priority = (default: $p_spec:ident),
1154        priority_enabled = $pe:tt,
1155    ) ) => {
1156        $next!($next_args, 500);
1157    };
1158
1159    // Priority specified = late, link options not, priority enabled
1160    ( @entry next=$next:path[$next_args:tt], input=(
1161        export_name_prefix = ($enp:tt: default),
1162        link_section = ($ls:tt: default),
1163        naked = ($naked:tt: default),
1164        priority = (late: $p_spec:ident),
1165        priority_enabled = $pe:tt,
1166    ) ) => {
1167        #[cfg(all(target_os = "aix", not(target_vendor = "apple")))]
1168        $next!($next_args, 89999999);
1169
1170        #[cfg(all(not(target_os = "aix"), not(target_vendor = "apple"), not(target_os = "uefi")))]
1171        $next!($next_args, 65535);
1172
1173        #[cfg(any(target_vendor = "apple", target_os = "uefi"))]
1174        $next!($next_args, ($crate::collect::LATE));
1175    };
1176
1177    // Priority specified, link options not, priority enabled
1178    ( @entry next=$next:path[$next_args:tt], input=(
1179        export_name_prefix = ($enp:tt: default),
1180        link_section = ($ls:tt: default),
1181        naked = ($naked:tt: default),
1182        priority = ($priority:tt: $p_spec:ident),
1183        priority_enabled = $pe:tt,
1184    ) ) => {
1185        $next!($next_args, $priority);
1186    };
1187
1188    // Priority specified, link options specified
1189    ( @entry next=$next:path[$next_args:tt], input=(
1190        export_name_prefix = $enp:tt,
1191        link_section = $ls:tt,
1192        naked = $naked:tt,
1193        priority = ($priority:tt: value),
1194        priority_enabled = $pe:tt,
1195    ) ) => {
1196        compile_error!(concat!("Priority must not be specified if naked, export_name_prefix, or link_section are specified."));
1197    };
1198
1199    // Priority unspecified, link options specified -> naked
1200    ( @entry next=$next:path[$next_args:tt], input=(
1201        export_name_prefix = $enp:tt,
1202        link_section = $ls:tt,
1203        naked = $naked:tt,
1204        priority = ($priority:tt: default),
1205        priority_enabled = $pe:tt,
1206    ) ) => {
1207        $next!($next_args, naked);
1208    };
1209
1210    // Naked specified
1211    ( @entry next=$next:path[$next_args:tt], input=(
1212        export_name_prefix = $enp:tt,
1213        link_section = $ls:tt,
1214        naked = (naked: value),
1215        priority = $priority:tt,
1216        priority_enabled = $pe:tt,
1217    ) ) => {
1218        $next!($next_args, naked);
1219    };
1220
1221    ( @entry next=$next:path[$next_args:tt], input=$input:tt ) => {
1222        compile_error!(concat!("Unexpected priority input: ", stringify!($input)));
1223    };
1224}