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
use sway_error::error::CompileError;
use sway_types::Span;

use crate::{
    decl_engine::*,
    language::ty::{self, TyFunctionDeclaration},
};

#[derive(Debug, Eq, PartialEq, Hash, Clone)]
pub enum FunctionalDeclId {
    TraitFn(DeclId<ty::TyTraitFn>),
    Function(DeclId<ty::TyFunctionDeclaration>),
}

impl From<DeclId<ty::TyFunctionDeclaration>> for FunctionalDeclId {
    fn from(val: DeclId<ty::TyFunctionDeclaration>) -> Self {
        Self::Function(val)
    }
}
impl From<&DeclId<ty::TyFunctionDeclaration>> for FunctionalDeclId {
    fn from(val: &DeclId<ty::TyFunctionDeclaration>) -> Self {
        Self::Function(*val)
    }
}
impl From<&mut DeclId<ty::TyFunctionDeclaration>> for FunctionalDeclId {
    fn from(val: &mut DeclId<ty::TyFunctionDeclaration>) -> Self {
        Self::Function(*val)
    }
}

impl From<DeclId<ty::TyTraitFn>> for FunctionalDeclId {
    fn from(val: DeclId<ty::TyTraitFn>) -> Self {
        Self::TraitFn(val)
    }
}
impl From<&DeclId<ty::TyTraitFn>> for FunctionalDeclId {
    fn from(val: &DeclId<ty::TyTraitFn>) -> Self {
        Self::TraitFn(*val)
    }
}
impl From<&mut DeclId<ty::TyTraitFn>> for FunctionalDeclId {
    fn from(val: &mut DeclId<ty::TyTraitFn>) -> Self {
        Self::TraitFn(*val)
    }
}

impl std::fmt::Display for FunctionalDeclId {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::TraitFn(_) => {
                write!(f, "decl(trait function)",)
            }
            Self::Function(_) => {
                write!(f, "decl(function)",)
            }
        }
    }
}

impl TryFrom<DeclRefMixedFunctional> for DeclRefFunction {
    type Error = CompileError;
    fn try_from(value: DeclRefMixedFunctional) -> Result<Self, Self::Error> {
        match value.id {
            FunctionalDeclId::Function(id) => Ok(DeclRef {
                name: value.name,
                id,
                decl_span: value.decl_span,
            }),
            actually @ FunctionalDeclId::TraitFn(_) => Err(CompileError::DeclIsNotAFunction {
                actually: actually.to_string(),
                span: value.decl_span,
            }),
        }
    }
}
impl TryFrom<&DeclRefMixedFunctional> for DeclRefFunction {
    type Error = CompileError;
    fn try_from(value: &DeclRefMixedFunctional) -> Result<Self, Self::Error> {
        value.clone().try_into()
    }
}

impl TryFrom<FunctionalDeclId> for DeclId<TyFunctionDeclaration> {
    type Error = CompileError;
    fn try_from(value: FunctionalDeclId) -> Result<Self, Self::Error> {
        match value {
            FunctionalDeclId::Function(id) => Ok(id),
            actually @ FunctionalDeclId::TraitFn(_) => Err(CompileError::DeclIsNotAFunction {
                actually: actually.to_string(),
                span: Span::dummy(), // FIXME
            }),
        }
    }
}
impl TryFrom<&FunctionalDeclId> for DeclId<TyFunctionDeclaration> {
    type Error = CompileError;
    fn try_from(value: &FunctionalDeclId) -> Result<Self, Self::Error> {
        value.clone().try_into()
    }
}