1use std::fmt::{self, Display, Formatter};
2
3use ecow::EcoString;
4
5use super::{PRELUDE_PACKAGE_ID, is_go_import, is_prelude_symbol};
6use crate::ast::Expression;
7use crate::types::Symbol;
8use crate::types::{CompoundKind, SimpleKind, SubstitutionMap, Type, TypeKind};
9
10#[derive(Debug, Clone, PartialEq, Eq, Hash)]
11pub struct MethodName {
12 pub owner: Symbol,
13 pub name: EcoString,
14}
15
16impl MethodName {
17 pub fn new(owner: Symbol, name: &str) -> Self {
18 Self {
19 owner,
20 name: name.into(),
21 }
22 }
23
24 pub fn is_go(&self) -> bool {
25 is_go_import(&self.owner)
26 }
27}
28
29impl Display for MethodName {
30 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
31 write!(f, "{}.{}", self.owner, self.name)
32 }
33}
34
35#[derive(Debug, Clone, PartialEq, Eq, Hash)]
36pub enum CalleeName {
37 Definition(Symbol),
38 Method(MethodName),
39}
40
41impl CalleeName {
42 pub fn definition(&self) -> Option<&Symbol> {
43 match self {
44 Self::Definition(name) => Some(name),
45 Self::Method(_) => None,
46 }
47 }
48
49 pub fn method(&self) -> Option<&MethodName> {
50 match self {
51 Self::Definition(_) => None,
52 Self::Method(method) => Some(method),
53 }
54 }
55
56 pub fn is_go(&self) -> bool {
57 match self {
58 Self::Definition(name) => is_go_import(name),
59 Self::Method(method) => method.is_go(),
60 }
61 }
62
63 pub fn is_prelude(&self) -> bool {
64 match self {
65 Self::Definition(name) => is_prelude_symbol(name),
66 Self::Method(method) => is_prelude_symbol(&method.owner),
67 }
68 }
69
70 pub fn qualified_name(&self) -> Symbol {
71 match self {
72 Self::Definition(name) => name.clone(),
73 Self::Method(method) => method.owner.with_segment(&method.name),
74 }
75 }
76
77 pub fn last_segment(&self) -> &str {
78 match self {
79 Self::Definition(name) => name.last_segment(),
80 Self::Method(method) => &method.name,
81 }
82 }
83}
84
85impl Display for CalleeName {
86 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
87 match self {
88 Self::Definition(name) => name.fmt(f),
89 Self::Method(method) => method.fmt(f),
90 }
91 }
92}
93
94pub fn resolved_callee(expression: &Expression) -> Option<CalleeName> {
95 match expression {
96 Expression::Identifier { resolution, .. } => resolution.callee_name(),
97 Expression::DotAccess { resolution, .. } => resolution.callee_name(),
98 _ => None,
99 }
100}
101
102pub fn resolved_definition_name(expression: &Expression) -> Option<&str> {
103 match expression {
104 Expression::Identifier { resolution, .. } => resolution.definition(),
105 Expression::DotAccess { resolution, .. } => resolution.defined_name().map(Symbol::as_str),
106 _ => None,
107 }
108}
109
110pub fn prelude_constructor(expression: &Expression) -> Option<(&str, &str)> {
112 let (owner, member) = resolved_definition_name(expression)?.rsplit_once('.')?;
113 let owner = match (owner, member) {
114 (PRELUDE_PACKAGE_ID, "Some" | "None") => "prelude.Option",
115 (PRELUDE_PACKAGE_ID, "Ok" | "Err") => "prelude.Result",
116 _ => owner,
117 };
118 Some((owner, member))
119}
120
121pub fn resolved_instantiation(expression: &Expression) -> Option<&SubstitutionMap> {
122 match expression {
123 Expression::Identifier { resolution, .. } => resolution.instantiation(),
124 Expression::DotAccess { resolution, .. } => resolution.instantiation(),
125 _ => None,
126 }
127}
128
129#[derive(Debug, Clone, Copy)]
130pub enum ChannelOperation<'a> {
131 Receive {
132 channel: &'a Expression,
133 },
134 Send {
135 channel: &'a Expression,
136 value: &'a Expression,
137 },
138}
139
140impl<'a> ChannelOperation<'a> {
141 pub fn channel(self) -> &'a Expression {
142 match self {
143 Self::Receive { channel } | Self::Send { channel, .. } => channel,
144 }
145 }
146
147 pub fn value(self) -> Option<&'a Expression> {
148 match self {
149 Self::Receive { .. } => None,
150 Self::Send { value, .. } => Some(value),
151 }
152 }
153}
154
155pub fn channel_operation(expression: &Expression) -> Option<ChannelOperation<'_>> {
156 let Expression::Call {
157 expression: callee,
158 args,
159 ..
160 } = expression
161 else {
162 return None;
163 };
164
165 let (channel, method, operands): (&Expression, &str, &[Expression]) = match callee.as_ref() {
166 Expression::DotAccess {
167 expression: channel,
168 member,
169 ..
170 } => (channel, member, args),
171 Expression::Identifier { value, .. } if has_channel_ufcs_prefix(value) => {
172 let (channel, operands) = args.split_first()?;
173 (channel, value.rsplit('.').next()?, operands)
174 }
175 _ => return None,
176 };
177
178 match (method, operands) {
179 ("receive", []) => Some(ChannelOperation::Receive { channel }),
180 ("send", [value]) => Some(ChannelOperation::Send { channel, value }),
181 _ => None,
182 }
183}
184
185fn has_channel_ufcs_prefix(identifier: &str) -> bool {
186 let Some((prefix, _)) = identifier.rsplit_once('.') else {
187 return false;
188 };
189 matches!(
190 prefix.rsplit('.').next(),
191 Some("Channel" | "Sender" | "Receiver")
192 )
193}
194
195#[derive(Debug, Clone, Copy, PartialEq, Eq)]
196pub enum ReceiverCoercion {
197 AutoAddress,
199 AutoDeref,
201}
202
203#[derive(Debug, Clone, PartialEq, Eq)]
205pub enum DotAccessResolution {
206 Unresolved,
207 StructField {
209 is_exported: bool,
210 declaring_type: Option<Symbol>,
211 },
212 TupleStructField {
216 index: usize,
217 is_newtype: bool,
218 },
219 TupleElement {
221 index: usize,
222 },
223 PackageMember {
225 definition: Option<Symbol>,
226 instantiation: SubstitutionMap,
227 },
228 EnumVariant {
230 definition: Symbol,
231 instantiation: SubstitutionMap,
232 },
233 InstanceMethod {
235 is_exported: bool,
236 receiver_coercion: Option<ReceiverCoercion>,
237 method: Option<MethodName>,
238 instantiation: SubstitutionMap,
239 },
240 InstanceMethodValue {
243 is_exported: bool,
244 method: Option<MethodName>,
245 instantiation: SubstitutionMap,
246 },
247 StaticMethod {
249 is_exported: bool,
250 definition: Symbol,
251 instantiation: SubstitutionMap,
252 },
253}
254
255impl DotAccessResolution {
256 pub fn reads_receiver(&self) -> bool {
257 matches!(
258 self,
259 Self::StructField { .. }
260 | Self::TupleStructField { .. }
261 | Self::TupleElement { .. }
262 | Self::InstanceMethod { .. }
263 | Self::InstanceMethodValue { .. }
264 )
265 }
266
267 pub fn is_field_read(&self) -> bool {
268 matches!(
269 self,
270 Self::StructField { .. } | Self::TupleStructField { .. } | Self::TupleElement { .. }
271 )
272 }
273
274 pub fn declaring_type(&self) -> Option<&Symbol> {
275 match self {
276 Self::StructField { declaring_type, .. } => declaring_type.as_ref(),
277 _ => None,
278 }
279 }
280
281 pub fn receiver_coercion(&self) -> Option<ReceiverCoercion> {
282 match self {
283 Self::InstanceMethod {
284 receiver_coercion, ..
285 } => *receiver_coercion,
286 _ => None,
287 }
288 }
289
290 pub fn defined_name(&self) -> Option<&Symbol> {
291 match self {
292 Self::PackageMember { definition, .. } => definition.as_ref(),
293 Self::EnumVariant { definition, .. } | Self::StaticMethod { definition, .. } => {
294 Some(definition)
295 }
296 Self::Unresolved
297 | Self::StructField { .. }
298 | Self::TupleStructField { .. }
299 | Self::TupleElement { .. }
300 | Self::InstanceMethod { .. }
301 | Self::InstanceMethodValue { .. } => None,
302 }
303 }
304
305 pub fn method(&self) -> Option<&MethodName> {
306 match self {
307 Self::InstanceMethod { method, .. } | Self::InstanceMethodValue { method, .. } => {
308 method.as_ref()
309 }
310 _ => None,
311 }
312 }
313
314 pub fn callee_name(&self) -> Option<CalleeName> {
315 match self.method() {
316 Some(method) => Some(CalleeName::Method(method.clone())),
317 None => self.defined_name().cloned().map(CalleeName::Definition),
318 }
319 }
320
321 pub fn instantiation(&self) -> Option<&SubstitutionMap> {
322 match self {
323 Self::PackageMember { instantiation, .. }
324 | Self::EnumVariant { instantiation, .. }
325 | Self::InstanceMethod { instantiation, .. }
326 | Self::InstanceMethodValue { instantiation, .. }
327 | Self::StaticMethod { instantiation, .. } => Some(instantiation),
328 Self::Unresolved
329 | Self::StructField { .. }
330 | Self::TupleStructField { .. }
331 | Self::TupleElement { .. } => None,
332 }
333 }
334}
335
336#[derive(Debug, Clone, Copy, PartialEq, Eq)]
339pub enum NativeTypeKind {
340 Slice,
341 EnumeratedSlice,
342 Map,
343 Channel,
344 Sender,
345 Receiver,
346 String,
347 Array,
348}
349
350impl NativeTypeKind {
351 pub fn from_type(ty: &Type) -> Option<Self> {
352 Some(match ty.strip_refs().kind() {
353 TypeKind::Compound { kind, .. } => match kind {
354 CompoundKind::Slice => Self::Slice,
355 CompoundKind::EnumeratedSlice => Self::EnumeratedSlice,
356 CompoundKind::Map => Self::Map,
357 CompoundKind::Channel => Self::Channel,
358 CompoundKind::Sender => Self::Sender,
359 CompoundKind::Receiver => Self::Receiver,
360 CompoundKind::Ref | CompoundKind::VarArgs => return None,
361 },
362 TypeKind::Simple(SimpleKind::String) => Self::String,
363 TypeKind::Array { .. } => Self::Array,
364 _ => return None,
365 })
366 }
367
368 pub fn from_constructor_path(path: &str) -> Option<Self> {
369 match path {
370 "Channel.new" | "Channel.buffered" => Some(Self::Channel),
371 "Map.new" => Some(Self::Map),
372 "Slice.new" | "Slice.make" => Some(Self::Slice),
373 "Array.new" | "Array.from" => Some(Self::Array),
374 _ => None,
375 }
376 }
377
378 pub fn is_constructor_method(name: &str) -> bool {
379 matches!(name, "new" | "buffered" | "make")
380 }
381
382 pub fn from_name(name: &str) -> Option<Self> {
383 match name {
384 "Slice" => Some(Self::Slice),
385 "EnumeratedSlice" => Some(Self::EnumeratedSlice),
386 "Map" => Some(Self::Map),
387 "Channel" => Some(Self::Channel),
388 "Sender" => Some(Self::Sender),
389 "Receiver" => Some(Self::Receiver),
390 "string" => Some(Self::String),
391 "Array" => Some(Self::Array),
392 _ => None,
393 }
394 }
395
396 pub fn name(self) -> &'static str {
397 match self {
398 Self::Slice => "Slice",
399 Self::EnumeratedSlice => "EnumeratedSlice",
400 Self::Map => "Map",
401 Self::Channel => "Channel",
402 Self::Sender => "Sender",
403 Self::Receiver => "Receiver",
404 Self::String => "string",
405 Self::Array => "Array",
406 }
407 }
408}