naga/proc/overloads/
any_overload_set.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
//! Dynamically dispatched [`OverloadSet`]s.

use crate::common::DiagnosticDebug;
use crate::ir;
use crate::proc::overloads::{list, regular, OverloadSet, Rule};
use crate::proc::{GlobalCtx, TypeResolution};

use alloc::vec::Vec;
use core::fmt;

macro_rules! define_any_overload_set {
    { $( $module:ident :: $name:ident, )* } => {
        /// An [`OverloadSet`] that dynamically dispatches to concrete implementations.
        #[derive(Clone)]
        pub(in crate::proc::overloads) enum AnyOverloadSet {
            $(
                $name ( $module :: $name ),
            )*
        }

        $(
            impl From<$module::$name> for AnyOverloadSet {
                fn from(concrete: $module::$name) -> Self {
                    AnyOverloadSet::$name(concrete)
                }
            }
        )*

        impl OverloadSet for AnyOverloadSet {
            fn is_empty(&self) -> bool {
                match *self {
                    $(
                        AnyOverloadSet::$name(ref x) => x.is_empty(),
                    )*
                }
            }

            fn min_arguments(&self) -> usize {
                match *self {
                    $(
                        AnyOverloadSet::$name(ref x) => x.min_arguments(),
                    )*
                }
            }

            fn max_arguments(&self) -> usize {
                match *self {
                    $(
                        AnyOverloadSet::$name(ref x) => x.max_arguments(),
                    )*
                }
            }

            fn arg(
                &self,
                i: usize,
                ty: &ir::TypeInner,
                types: &crate::UniqueArena<ir::Type>,
            ) -> Self {
                match *self {
                    $(
                        AnyOverloadSet::$name(ref x) => AnyOverloadSet::$name(x.arg(i, ty, types)),
                    )*
                }
            }

            fn concrete_only(self, types: &crate::UniqueArena<ir::Type>) -> Self {
                match self {
                    $(
                        AnyOverloadSet::$name(x) => AnyOverloadSet::$name(x.concrete_only(types)),
                    )*
                }
            }

            fn most_preferred(&self) -> Rule {
                match *self {
                    $(
                        AnyOverloadSet::$name(ref x) => x.most_preferred(),
                    )*
                }
            }

            fn overload_list(&self, gctx: &GlobalCtx<'_>) -> Vec<Rule> {
                match *self {
                    $(
                        AnyOverloadSet::$name(ref x) => x.overload_list(gctx),
                    )*
                }
            }

            fn allowed_args(&self, i: usize, gctx: &GlobalCtx<'_>) -> Vec<TypeResolution> {
                match *self {
                    $(
                        AnyOverloadSet::$name(ref x) => x.allowed_args(i, gctx),
                    )*
                }
            }

            fn for_debug(&self, types: &crate::UniqueArena<ir::Type>) -> impl fmt::Debug {
                DiagnosticDebug((self, types))
            }
        }

        impl fmt::Debug for DiagnosticDebug<(&AnyOverloadSet, &crate::UniqueArena<ir::Type>)> {
            fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                let (set, types) = self.0;
                match *set {
                    $(
                        AnyOverloadSet::$name(ref x) => DiagnosticDebug((x, types)).fmt(f),
                    )*
                }
            }
        }
    }
}

define_any_overload_set! {
    list::List,
    regular::Regular,
}