sway_core/language/parsed/expression/
method_name.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
use crate::engine_threading::{EqWithEngines, PartialEqWithEngines, PartialEqWithEnginesContext};
use crate::language::CallPath;
use crate::type_system::TypeBinding;
use crate::{Ident, TypeArgument, TypeId, TypeInfo};

#[allow(clippy::large_enum_variant)]
#[derive(Debug, Clone)]
pub enum MethodName {
    /// Represents a method lookup with a type somewhere in the path
    /// like `a::b::C::d()` with `C` being the type.
    FromType {
        call_path_binding: TypeBinding<CallPath<(TypeInfo, Ident)>>,
        method_name: Ident,
    },
    /// Represents a method lookup that does not contain any types in the path
    /// something like a.b(c)
    /// in this case, the first argument defines where to look for the method
    FromModule { method_name: Ident },
    /// something like a::b::c()
    /// in this case, the path defines where the fn symbol is defined
    /// used for things like core::ops::add(a, b).
    /// in this case, the first argument determines the type to look for
    FromTrait { call_path: CallPath },
    /// Represents a method lookup with a fully qualified path.
    /// like <S as Trait>::method()
    FromQualifiedPathRoot {
        ty: TypeArgument,
        as_trait: TypeId,
        method_name: Ident,
    },
}

impl EqWithEngines for MethodName {}
impl PartialEqWithEngines for MethodName {
    fn eq(&self, other: &Self, ctx: &PartialEqWithEnginesContext) -> bool {
        match (self, other) {
            (
                MethodName::FromType {
                    call_path_binding,
                    method_name,
                },
                MethodName::FromType {
                    call_path_binding: r_call_path_binding,
                    method_name: r_method_name,
                },
            ) => call_path_binding.eq(r_call_path_binding, ctx) && method_name == r_method_name,
            (
                MethodName::FromModule { method_name },
                MethodName::FromModule {
                    method_name: r_method_name,
                },
            ) => method_name == r_method_name,
            (
                MethodName::FromTrait { call_path },
                MethodName::FromTrait {
                    call_path: r_call_path,
                },
            ) => call_path == r_call_path,
            (
                MethodName::FromQualifiedPathRoot {
                    ty,
                    as_trait,
                    method_name,
                },
                MethodName::FromQualifiedPathRoot {
                    ty: r_ty,
                    as_trait: r_as_trait,
                    method_name: r_method_name,
                },
            ) => ty.eq(r_ty, ctx) && as_trait.eq(r_as_trait) && method_name == r_method_name,
            _ => false,
        }
    }
}

impl MethodName {
    /// To be used for error messages and debug strings
    pub fn easy_name(&self) -> Ident {
        match self {
            MethodName::FromType { method_name, .. } => method_name.clone(),
            MethodName::FromTrait { call_path, .. } => call_path.suffix.clone(),
            MethodName::FromModule { method_name, .. } => method_name.clone(),
            MethodName::FromQualifiedPathRoot { method_name, .. } => method_name.clone(),
        }
    }
}