1use crate::ast::Expression;
2use crate::types::Symbol;
3use crate::types::{CompoundKind, SimpleKind, SubstitutionMap, Type};
4
5pub fn resolved_definition(expression: &Expression) -> Option<&str> {
6 match expression.unwrap_parens() {
7 Expression::Identifier { resolution, .. } => resolution.definition(),
8 Expression::DotAccess { resolution, .. } => resolution.definition(),
9 _ => None,
10 }
11}
12
13pub fn prelude_constructor(expression: &Expression) -> Option<(&str, &str)> {
15 let (owner, member) = resolved_definition(expression)?.rsplit_once('.')?;
16 let owner = match (owner, member) {
17 ("prelude", "Some" | "None") => "prelude.Option",
18 ("prelude", "Ok" | "Err") => "prelude.Result",
19 _ => owner,
20 };
21 Some((owner, member))
22}
23
24pub fn resolved_instantiation(expression: &Expression) -> Option<&SubstitutionMap> {
25 match expression.unwrap_parens() {
26 Expression::Identifier { resolution, .. } => resolution.instantiation(),
27 Expression::DotAccess { resolution, .. } => resolution.instantiation(),
28 _ => None,
29 }
30}
31
32#[derive(Debug, Clone, Copy)]
33pub enum ChannelOperation<'a> {
34 Receive {
35 channel: &'a Expression,
36 },
37 Send {
38 channel: &'a Expression,
39 value: &'a Expression,
40 },
41}
42
43impl<'a> ChannelOperation<'a> {
44 pub fn channel(self) -> &'a Expression {
45 match self {
46 Self::Receive { channel } | Self::Send { channel, .. } => channel,
47 }
48 }
49
50 pub fn value(self) -> Option<&'a Expression> {
51 match self {
52 Self::Receive { .. } => None,
53 Self::Send { value, .. } => Some(value),
54 }
55 }
56}
57
58pub fn channel_operation(expression: &Expression) -> Option<ChannelOperation<'_>> {
59 let Expression::Call {
60 expression: callee,
61 args,
62 ..
63 } = expression
64 else {
65 return None;
66 };
67
68 let (channel, method, operands): (&Expression, &str, &[Expression]) = match callee.as_ref() {
69 Expression::DotAccess {
70 expression: channel,
71 member,
72 ..
73 } => (channel, member, args),
74 Expression::Identifier { value, .. } if has_channel_ufcs_prefix(value) => {
75 let (channel, operands) = args.split_first()?;
76 (channel, value.rsplit('.').next()?, operands)
77 }
78 _ => return None,
79 };
80
81 match (method, operands) {
82 ("receive", []) => Some(ChannelOperation::Receive { channel }),
83 ("send", [value]) => Some(ChannelOperation::Send { channel, value }),
84 _ => None,
85 }
86}
87
88fn has_channel_ufcs_prefix(identifier: &str) -> bool {
89 let Some((prefix, _)) = identifier.rsplit_once('.') else {
90 return false;
91 };
92 matches!(
93 prefix.rsplit('.').next(),
94 Some("Channel" | "Sender" | "Receiver")
95 )
96}
97
98#[derive(Debug, Clone, Copy, PartialEq, Eq)]
99pub enum ReceiverCoercion {
100 AutoAddress,
102 AutoDeref,
104}
105
106#[derive(Debug, Clone, PartialEq, Eq)]
108pub enum DotAccessResolution {
109 Unresolved,
110 StructField {
112 is_exported: bool,
113 declaring_type: Option<Symbol>,
114 },
115 TupleStructField {
119 is_newtype: bool,
120 },
121 TupleElement,
123 PackageMember {
125 definition: Option<Symbol>,
126 instantiation: SubstitutionMap,
127 },
128 EnumVariant {
130 definition: Symbol,
131 instantiation: SubstitutionMap,
132 },
133 InstanceMethod {
135 is_exported: bool,
136 receiver_coercion: Option<ReceiverCoercion>,
137 definition: Option<Symbol>,
138 instantiation: SubstitutionMap,
139 },
140 InstanceMethodValue {
144 is_exported: bool,
145 is_pointer_receiver: bool,
146 definition: Option<Symbol>,
147 instantiation: SubstitutionMap,
148 },
149 StaticMethod {
151 is_exported: bool,
152 definition: Symbol,
153 instantiation: SubstitutionMap,
154 },
155}
156
157impl DotAccessResolution {
158 pub fn is_field_read(&self) -> bool {
159 matches!(
160 self,
161 Self::StructField { .. } | Self::TupleStructField { .. } | Self::TupleElement
162 )
163 }
164
165 pub fn declaring_type(&self) -> Option<&Symbol> {
166 match self {
167 Self::StructField { declaring_type, .. } => declaring_type.as_ref(),
168 _ => None,
169 }
170 }
171
172 pub fn receiver_coercion(&self) -> Option<ReceiverCoercion> {
173 match self {
174 Self::InstanceMethod {
175 receiver_coercion, ..
176 } => *receiver_coercion,
177 _ => None,
178 }
179 }
180
181 pub fn definition(&self) -> Option<&str> {
182 match self {
183 Self::PackageMember { definition, .. }
184 | Self::InstanceMethod { definition, .. }
185 | Self::InstanceMethodValue { definition, .. } => {
186 definition.as_ref().map(Symbol::as_str)
187 }
188 Self::EnumVariant { definition, .. } | Self::StaticMethod { definition, .. } => {
189 Some(definition)
190 }
191 Self::Unresolved
192 | Self::StructField { .. }
193 | Self::TupleStructField { .. }
194 | Self::TupleElement => None,
195 }
196 }
197
198 pub fn instantiation(&self) -> Option<&SubstitutionMap> {
199 match self {
200 Self::PackageMember { instantiation, .. }
201 | Self::EnumVariant { instantiation, .. }
202 | Self::InstanceMethod { instantiation, .. }
203 | Self::InstanceMethodValue { instantiation, .. }
204 | Self::StaticMethod { instantiation, .. } => Some(instantiation),
205 Self::Unresolved
206 | Self::StructField { .. }
207 | Self::TupleStructField { .. }
208 | Self::TupleElement => None,
209 }
210 }
211}
212
213#[derive(Debug, Clone, Copy, PartialEq, Eq)]
216pub enum NativeTypeKind {
217 Slice,
218 EnumeratedSlice,
219 Map,
220 Channel,
221 Sender,
222 Receiver,
223 String,
224 Array,
225}
226
227impl NativeTypeKind {
228 pub fn from_type(ty: &Type) -> Option<Self> {
229 Some(match ty.strip_refs() {
230 Type::Compound { kind, .. } => match kind {
231 CompoundKind::Slice => Self::Slice,
232 CompoundKind::EnumeratedSlice => Self::EnumeratedSlice,
233 CompoundKind::Map => Self::Map,
234 CompoundKind::Channel => Self::Channel,
235 CompoundKind::Sender => Self::Sender,
236 CompoundKind::Receiver => Self::Receiver,
237 CompoundKind::Ref | CompoundKind::VarArgs => return None,
238 },
239 Type::Simple(SimpleKind::String) => Self::String,
240 Type::Array { .. } => Self::Array,
241 _ => return None,
242 })
243 }
244
245 pub fn from_constructor_path(path: &str) -> Option<Self> {
246 match path {
247 "Channel.new" | "Channel.buffered" => Some(Self::Channel),
248 "Map.new" => Some(Self::Map),
249 "Slice.new" | "Slice.make" => Some(Self::Slice),
250 "Array.new" | "Array.from" => Some(Self::Array),
251 _ => None,
252 }
253 }
254
255 pub fn is_constructor_method(name: &str) -> bool {
256 matches!(name, "new" | "buffered" | "make")
257 }
258
259 pub fn from_name(name: &str) -> Option<Self> {
260 match name {
261 "Slice" => Some(Self::Slice),
262 "EnumeratedSlice" => Some(Self::EnumeratedSlice),
263 "Map" => Some(Self::Map),
264 "Channel" => Some(Self::Channel),
265 "Sender" => Some(Self::Sender),
266 "Receiver" => Some(Self::Receiver),
267 "string" => Some(Self::String),
268 "Array" => Some(Self::Array),
269 _ => None,
270 }
271 }
272
273 pub fn name(self) -> &'static str {
274 match self {
275 Self::Slice => "Slice",
276 Self::EnumeratedSlice => "EnumeratedSlice",
277 Self::Map => "Map",
278 Self::Channel => "Channel",
279 Self::Sender => "Sender",
280 Self::Receiver => "Receiver",
281 Self::String => "string",
282 Self::Array => "Array",
283 }
284 }
285}
286
287#[derive(Debug, Clone, Copy, PartialEq, Eq)]
289pub enum CallKind {
290 Unresolved,
292 Regular,
294 TupleStructConstructor,
296 AssertType,
298 Zero,
300 UfcsMethod,
302 NativeConstructor(NativeTypeKind),
304 NativeMethod(NativeTypeKind),
306 NativeMethodIdentifier(NativeTypeKind),
308 ReceiverMethodUfcs { is_public: bool },
310}