sway_core/language/parsed/expression/
method_name.rs

1use crate::engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext};
2use crate::language::CallPath;
3use crate::type_system::TypeBinding;
4use crate::{Ident, TypeArgument, TypeId, TypeInfo};
5
6#[allow(clippy::large_enum_variant)]
7#[derive(Debug, Clone)]
8pub enum MethodName {
9    /// Represents a method lookup with a type somewhere in the path
10    /// like `a::b::C::d()` with `C` being the type.
11    FromType {
12        call_path_binding: TypeBinding<CallPath<(TypeInfo, Ident)>>,
13        method_name: Ident,
14    },
15    /// Represents a method lookup that does not contain any types in the path
16    /// something like a.b(c)
17    /// in this case, the first argument defines where to look for the method
18    FromModule { method_name: Ident },
19    /// something like a::b::c()
20    /// in this case, the path defines where the fn symbol is defined
21    /// used for things like std::ops::add(a, b).
22    /// in this case, the first argument determines the type to look for
23    FromTrait { call_path: CallPath },
24    /// Represents a method lookup with a fully qualified path.
25    /// like <S as Trait>::method()
26    FromQualifiedPathRoot {
27        ty: TypeArgument,
28        as_trait: TypeId,
29        method_name: Ident,
30    },
31}
32
33impl EqWithEngines for MethodName {}
34impl PartialEqWithEngines for MethodName {
35    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
36        match (self, other) {
37            (
38                MethodName::FromType {
39                    call_path_binding,
40                    method_name,
41                },
42                MethodName::FromType {
43                    call_path_binding: r_call_path_binding,
44                    method_name: r_method_name,
45                },
46            ) => call_path_binding.eq(r_call_path_binding, ctx) && method_name == r_method_name,
47            (
48                MethodName::FromModule { method_name },
49                MethodName::FromModule {
50                    method_name: r_method_name,
51                },
52            ) => method_name == r_method_name,
53            (
54                MethodName::FromTrait { call_path },
55                MethodName::FromTrait {
56                    call_path: r_call_path,
57                },
58            ) => call_path == r_call_path,
59            (
60                MethodName::FromQualifiedPathRoot {
61                    ty,
62                    as_trait,
63                    method_name,
64                },
65                MethodName::FromQualifiedPathRoot {
66                    ty: r_ty,
67                    as_trait: r_as_trait,
68                    method_name: r_method_name,
69                },
70            ) => ty.eq(r_ty, ctx) && as_trait.eq(r_as_trait) && method_name == r_method_name,
71            _ => false,
72        }
73    }
74}
75
76impl MethodName {
77    /// To be used for error messages and debug strings
78    pub fn easy_name(&self) -> Ident {
79        match self {
80            MethodName::FromType { method_name, .. } => method_name.clone(),
81            MethodName::FromTrait { call_path, .. } => call_path.suffix.clone(),
82            MethodName::FromModule { method_name, .. } => method_name.clone(),
83            MethodName::FromQualifiedPathRoot { method_name, .. } => method_name.clone(),
84        }
85    }
86}