naga/proc/overloads/
list.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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
//! An [`OverloadSet`] represented as a vector of rules.
//!
//! [`OverloadSet`]: crate::proc::overloads::OverloadSet

use crate::common::{DiagnosticDebug, ForDebug, ForDebugWithTypes};
use crate::ir;
use crate::proc::overloads::one_bits_iter::OneBitsIter;
use crate::proc::overloads::Rule;
use crate::proc::{GlobalCtx, TypeResolution};

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

/// A simple list of overloads.
///
/// Note that this type is not quite as general as it looks, in that
/// the implementation of `most_preferred` doesn't work for arbitrary
/// lists of overloads. See the documentation for [`List::rules`] for
/// details.
#[derive(Clone)]
pub(in crate::proc::overloads) struct List {
    /// A bitmask of which elements of `rules` are included in the set.
    members: u64,

    /// A list of type rules that are members of the set.
    ///
    /// These must be listed in order such that every rule in the list
    /// is always more preferred than all subsequent rules in the
    /// list. If there is no such arrangement of rules, then you
    /// cannot use `List` to represent the overload set.
    rules: Rc<Vec<Rule>>,
}

impl List {
    pub(in crate::proc::overloads) fn from_rules(rules: Vec<Rule>) -> List {
        List {
            members: len_to_full_mask(rules.len()),
            rules: Rc::new(rules),
        }
    }

    fn members(&self) -> impl Iterator<Item = (u64, &Rule)> {
        OneBitsIter::new(self.members).map(|mask| {
            let index = mask.trailing_zeros() as usize;
            (mask, &self.rules[index])
        })
    }

    fn filter<F>(&self, mut pred: F) -> List
    where
        F: FnMut(&Rule) -> bool,
    {
        let mut filtered_members = 0;
        for (mask, rule) in self.members() {
            if pred(rule) {
                filtered_members |= mask;
            }
        }

        List {
            members: filtered_members,
            rules: self.rules.clone(),
        }
    }
}

impl crate::proc::overloads::OverloadSet for List {
    fn is_empty(&self) -> bool {
        self.members == 0
    }

    fn min_arguments(&self) -> usize {
        self.members()
            .fold(None, |best, (_, rule)| {
                // This is different from `max_arguments` because
                // `<Option as PartialOrd>` doesn't work the way we'd like.
                let len = rule.arguments.len();
                Some(match best {
                    Some(best) => core::cmp::max(best, len),
                    None => len,
                })
            })
            .unwrap()
    }

    fn max_arguments(&self) -> usize {
        self.members()
            .fold(None, |n, (_, rule)| {
                core::cmp::max(n, Some(rule.arguments.len()))
            })
            .unwrap()
    }

    fn arg(&self, i: usize, arg_ty: &ir::TypeInner, types: &crate::UniqueArena<ir::Type>) -> Self {
        log::debug!("arg {i} of type {:?}", arg_ty.for_debug(types));
        self.filter(|rule| {
            if log::log_enabled!(log::Level::Debug) {
                log::debug!("    considering rule {:?}", rule.for_debug(types));
                match rule.arguments.get(i) {
                    Some(rule_ty) => {
                        let rule_ty = rule_ty.inner_with(types);
                        if arg_ty.equivalent(rule_ty, types) {
                            log::debug!("    types are equivalent");
                        } else {
                            match arg_ty.automatically_converts_to(rule_ty, types) {
                                Some((from, to)) => {
                                    log::debug!(
                                        "    converts automatically from {:?} to {:?}",
                                        from.for_debug(),
                                        to.for_debug()
                                    );
                                }
                                None => {
                                    log::debug!("    no conversion possible");
                                }
                            }
                        }
                    }
                    None => {
                        log::debug!("    argument index {i} out of bounds");
                    }
                }
            }
            rule.arguments.get(i).is_some_and(|rule_ty| {
                let rule_ty = rule_ty.inner_with(types);
                arg_ty.equivalent(rule_ty, types)
                    || arg_ty.automatically_converts_to(rule_ty, types).is_some()
            })
        })
    }

    fn concrete_only(self, types: &crate::UniqueArena<ir::Type>) -> Self {
        self.filter(|rule| {
            rule.arguments
                .iter()
                .all(|arg_ty| !arg_ty.inner_with(types).is_abstract(types))
        })
    }

    fn most_preferred(&self) -> Rule {
        // As documented for `Self::rules`, whatever rule is first is
        // the most preferred. `OverloadSet` documents this method to
        // panic if the set is empty.
        let (_, rule) = self.members().next().unwrap();
        rule.clone()
    }

    fn overload_list(&self, _gctx: &GlobalCtx<'_>) -> Vec<Rule> {
        self.members().map(|(_, rule)| rule.clone()).collect()
    }

    fn allowed_args(&self, i: usize, _gctx: &GlobalCtx<'_>) -> Vec<TypeResolution> {
        self.members()
            .map(|(_, rule)| rule.arguments[i].clone())
            .collect()
    }

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

const fn len_to_full_mask(n: usize) -> u64 {
    if n >= 64 {
        panic!("List::rules can only hold up to 63 rules");
    }

    (1_u64 << n) - 1
}

impl ForDebugWithTypes for &List {}

impl fmt::Debug for DiagnosticDebug<(&List, &crate::UniqueArena<ir::Type>)> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let (list, types) = self.0;

        f.debug_list()
            .entries(list.members().map(|(_mask, rule)| rule.for_debug(types)))
            .finish()
    }
}