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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
use crate::{
error::*,
language::{ty, CallPath},
type_system::*,
CompileResult, Ident, TypeInfo,
};
use super::{module::Module, namespace::Namespace, Path};
use sway_error::error::CompileError;
use sway_types::Spanned;
use std::collections::VecDeque;
#[derive(Clone, Debug, PartialEq)]
pub struct Root {
pub(crate) module: Module,
}
impl Root {
pub(crate) fn resolve_call_path(
&self,
mod_path: &Path,
call_path: &CallPath,
) -> CompileResult<&ty::TyDeclaration> {
let symbol_path: Vec<_> = mod_path
.iter()
.chain(&call_path.prefixes)
.cloned()
.collect();
self.resolve_symbol(&symbol_path, &call_path.suffix)
}
pub(crate) fn resolve_symbol(
&self,
mod_path: &Path,
symbol: &Ident,
) -> CompileResult<&ty::TyDeclaration> {
self.check_submodule(mod_path).flat_map(|module| {
let true_symbol = self[mod_path]
.use_aliases
.get(symbol.as_str())
.unwrap_or(symbol);
match module.use_synonyms.get(symbol) {
Some((src_path, _)) if mod_path != src_path => {
self.resolve_symbol(src_path, true_symbol)
}
_ => CompileResult::from(module.check_symbol(true_symbol)),
}
})
}
pub(crate) fn find_method_for_type(
&mut self,
mod_path: &Path,
mut type_id: TypeId,
method_prefix: &Path,
method_name: &Ident,
self_type: TypeId,
args_buf: &VecDeque<ty::TyExpression>,
) -> CompileResult<ty::TyFunctionDeclaration> {
let mut warnings = vec![];
let mut errors = vec![];
let local_module = check!(
self.check_submodule(mod_path),
return err(warnings, errors),
warnings,
errors
);
let local_methods = local_module.get_methods_for_type(type_id);
type_id.replace_self_type(self_type);
let type_id = check!(
resolve_type(
type_id,
&method_name.span(),
EnforceTypeArguments::No,
None,
self,
method_prefix
),
insert_type(TypeInfo::ErrorRecovery),
warnings,
errors
);
let type_module = check!(
self.check_submodule(method_prefix),
return err(warnings, errors),
warnings,
errors
);
let mut type_methods = type_module.get_methods_for_type(type_id);
let mut methods = local_methods;
methods.append(&mut type_methods);
match methods
.into_iter()
.find(|ty::TyFunctionDeclaration { name, .. }| name == method_name)
{
Some(o) => ok(o, warnings, errors),
None => {
if args_buf.get(0).map(|x| look_up_type_id(x.return_type))
!= Some(TypeInfo::ErrorRecovery)
{
errors.push(CompileError::MethodNotFound {
method_name: method_name.clone(),
type_name: type_id.to_string(),
});
}
err(warnings, errors)
}
}
}
}
impl std::ops::Deref for Root {
type Target = Module;
fn deref(&self) -> &Self::Target {
&self.module
}
}
impl std::ops::DerefMut for Root {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.module
}
}
impl From<Module> for Root {
fn from(module: Module) -> Self {
Root { module }
}
}
impl From<Namespace> for Root {
fn from(namespace: Namespace) -> Self {
namespace.root
}
}