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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use crate::ast;
use crate::ast::Spanned;
use crate::compile::{CompileError, CompileErrorKind, CompileResult, Item, ModMeta, Visibility};
use crate::parse::Resolve;
use crate::query::Query;
use crate::worker::{ImportKind, Task, WildcardImport};
use crate::{Context, SourceId};
use std::collections::VecDeque;
use std::sync::Arc;
/// Import to process.
#[derive(Debug)]
pub(crate) struct Import {
pub(crate) kind: ImportKind,
pub(crate) module: Arc<ModMeta>,
pub(crate) visibility: Visibility,
pub(crate) item: Item,
pub(crate) source_id: SourceId,
pub(crate) ast: Box<ast::ItemUse>,
}
impl Import {
/// Lookup a local identifier in the current context and query.
fn lookup_local(&self, context: &Context, query: &Query, local: &str) -> Item {
let item = self.module.item.extended(local);
if let ImportKind::Local = self.kind {
if query.contains_prefix(&item) {
return item;
}
}
if context.contains_crate(local) {
return Item::with_crate(local);
}
item
}
/// Process the import, populating the unit.
pub(crate) fn process(
mut self,
context: &Context,
q: &mut Query,
add_task: &mut impl FnMut(Task),
) -> CompileResult<()> {
let (name, first, initial) = match self.kind {
ImportKind::Global => {
match self.ast.path.global {
Some(global) => match &self.ast.path.first {
ast::ItemUseSegment::PathSegment(ast::PathSegment::Ident(ident)) => {
let ident = ident.resolve(resolve_context!(q))?;
(Item::with_crate(ident), None, false)
}
_ => {
return Err(CompileError::new(
global.span(),
CompileErrorKind::UnsupportedGlobal,
));
}
},
// NB: defer non-local imports.
_ => {
self.kind = ImportKind::Local;
add_task(Task::ExpandImport(self));
return Ok(());
}
}
}
ImportKind::Local => (Item::new(), Some(&self.ast.path.first), true),
};
let mut queue = VecDeque::new();
queue.push_back((&self.ast.path, name, first, initial));
while let Some((path, mut name, first, mut initial)) = queue.pop_front() {
tracing::trace!("process one");
let mut it = first
.into_iter()
.chain(path.segments.iter().map(|(_, s)| s));
let complete = loop {
let segment = match it.next() {
Some(segment) => segment,
None => break None,
};
// Only the first ever segment loaded counts as the initial
// segment.
let initial = std::mem::take(&mut initial);
match segment {
ast::ItemUseSegment::PathSegment(segment) => match segment {
ast::PathSegment::Ident(ident) => {
let ident = ident.resolve(resolve_context!(q))?;
if !initial {
name.push(ident);
continue;
}
name = self.lookup_local(context, q, &*ident);
}
ast::PathSegment::SelfType(self_type) => {
return Err(CompileError::new(
self_type.span(),
CompileErrorKind::ExpectedLeadingPathSegment,
));
}
ast::PathSegment::SelfValue(self_value) => {
if !initial {
return Err(CompileError::new(
self_value.span(),
CompileErrorKind::ExpectedLeadingPathSegment,
));
}
name = self.module.item.clone();
}
ast::PathSegment::Crate(crate_token) => {
if !initial {
return Err(CompileError::new(
crate_token,
CompileErrorKind::ExpectedLeadingPathSegment,
));
}
name = Item::new();
}
ast::PathSegment::Super(super_token) => {
if initial {
name = self.module.item.clone();
}
name.pop().ok_or_else(|| {
CompileError::new(super_token, CompileErrorKind::UnsupportedSuper)
})?;
}
ast::PathSegment::Generics(arguments) => {
return Err(CompileError::new(
arguments,
CompileErrorKind::UnsupportedGenerics,
));
}
},
ast::ItemUseSegment::Wildcard(star_token) => {
let mut wildcard_import = WildcardImport {
visibility: self.visibility,
from: self.item.clone(),
name: name.clone(),
span: star_token.span(),
source_id: self.source_id,
module: self.module.clone(),
found: false,
};
wildcard_import.process_global(q, context)?;
add_task(Task::ExpandWildcardImport(wildcard_import));
break Some(star_token.span());
}
ast::ItemUseSegment::Group(group) => {
for (path, _) in group {
if let Some(global) = &path.global {
return Err(CompileError::new(
global.span(),
CompileErrorKind::UnsupportedGlobal,
));
}
queue.push_back((path, name.clone(), Some(&path.first), initial));
}
break Some(group.span());
}
}
};
if let Some(segment) = it.next() {
return Err(CompileError::new(
segment,
CompileErrorKind::IllegalUseSegment,
));
}
let alias = match &path.alias {
Some((_, ident)) => {
if let Some(span) = complete {
return Err(CompileError::new(
span.join(ident.span()),
CompileErrorKind::UseAliasNotSupported,
));
}
Some(*ident)
}
None => None,
};
if complete.is_none() {
q.insert_import(
self.source_id,
path.span(),
&self.module,
self.visibility,
self.item.clone(),
name,
alias,
false,
)?;
}
}
Ok(())
}
}