-
Notifications
You must be signed in to change notification settings - Fork 890
/
modules.rs
491 lines (451 loc) · 16.8 KB
/
modules.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
use std::borrow::Cow;
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use rustc_ast::ast;
use rustc_ast::attr::HasAttrs;
use rustc_ast::visit::Visitor;
use rustc_span::symbol::{self, sym, Symbol};
use thiserror::Error;
use crate::config::FileName;
use crate::formatting::{
attr::MetaVisitor,
items::is_mod_decl,
syntux::parser::{Directory, DirectoryOwnership, ModulePathSuccess, Parser, ParserError},
syntux::session::ParseSess,
utils::contains_skip,
};
mod visitor;
type FileModMap<'ast> = BTreeMap<FileName, Module<'ast>>;
lazy_static! {
static ref CFG_IF: Symbol = Symbol::intern("cfg_if");
}
/// Represents module with its inner attributes.
#[derive(Debug, Clone)]
pub(crate) struct Module<'a> {
ast_mod: Cow<'a, ast::Mod>,
inner_attr: Vec<ast::Attribute>,
}
impl<'a> Module<'a> {
pub(crate) fn new(ast_mod: Cow<'a, ast::Mod>, attrs: &[ast::Attribute]) -> Self {
let inner_attr = attrs
.iter()
.filter(|attr| attr.style == ast::AttrStyle::Inner)
.cloned()
.collect();
Module {
ast_mod,
inner_attr,
}
}
}
impl<'a> HasAttrs for Module<'a> {
fn attrs(&self) -> &[ast::Attribute] {
&self.inner_attr
}
fn visit_attrs(&mut self, f: impl FnOnce(&mut Vec<ast::Attribute>)) {
f(&mut self.inner_attr)
}
}
impl<'a> AsRef<ast::Mod> for Module<'a> {
fn as_ref(&self) -> &ast::Mod {
&self.ast_mod
}
}
/// Maps each module to the corresponding file.
pub(crate) struct ModResolver<'ast, 'sess> {
parse_sess: &'sess ParseSess,
directory: Directory,
file_map: FileModMap<'ast>,
recursive: bool,
}
/// Represents errors while trying to resolve modules.
#[error("failed to resolve mod `{module}`: {kind}")]
#[derive(Debug, Error)]
pub struct ModuleResolutionError {
pub(crate) module: String,
pub(crate) kind: ModuleResolutionErrorKind,
}
#[derive(Debug, Error)]
pub(crate) enum ModuleResolutionErrorKind {
/// Find a file that cannot be parsed.
#[error("cannot parse {file}")]
ParseError { file: PathBuf },
/// File cannot be found.
#[error("{file} does not exist")]
NotFound { file: PathBuf },
}
#[derive(Clone)]
enum SubModKind<'a, 'ast> {
/// `mod foo;`
External(PathBuf, DirectoryOwnership, Module<'ast>),
/// `mod foo;` with multiple sources.
MultiExternal(Vec<(PathBuf, DirectoryOwnership, Module<'ast>)>),
/// `mod foo {}`
Internal(&'a ast::Item),
}
impl<'ast, 'sess, 'c> ModResolver<'ast, 'sess> {
/// Creates a new `ModResolver`.
pub(crate) fn new(
parse_sess: &'sess ParseSess,
directory_ownership: DirectoryOwnership,
recursive: bool,
) -> Self {
ModResolver {
directory: Directory {
path: PathBuf::new(),
ownership: directory_ownership,
},
file_map: BTreeMap::new(),
parse_sess,
recursive,
}
}
/// Creates a map that maps a file name to the module in AST.
pub(crate) fn visit_crate(
mut self,
krate: &'ast ast::Crate,
) -> Result<FileModMap<'ast>, ModuleResolutionError> {
let root_filename = self.parse_sess.span_to_filename(krate.span);
self.directory.path = match root_filename {
FileName::Real(ref p) => p.parent().unwrap_or_else(|| Path::new("")).to_path_buf(),
_ => PathBuf::new(),
};
// Skip visiting sub modules when the input is from stdin.
if self.recursive {
self.visit_mod_from_ast(&krate.module)?;
}
self.file_map.insert(
root_filename,
Module::new(Cow::Borrowed(&krate.module), &krate.attrs),
);
Ok(self.file_map)
}
/// Visit `cfg_if` macro and look for module declarations.
fn visit_cfg_if(&mut self, item: Cow<'ast, ast::Item>) -> Result<(), ModuleResolutionError> {
let mut visitor = visitor::CfgIfVisitor::new(self.parse_sess);
visitor.visit_item(&item);
for module_item in visitor.mods() {
if let ast::ItemKind::Mod(ref sub_mod) = module_item.item.kind {
self.visit_sub_mod(
&module_item.item,
Module::new(Cow::Owned(sub_mod.clone()), &module_item.item.attrs),
)?;
}
}
Ok(())
}
/// Visit modules defined inside macro calls.
fn visit_mod_outside_ast(&mut self, module: ast::Mod) -> Result<(), ModuleResolutionError> {
for item in module.items {
if is_cfg_if(&item) {
self.visit_cfg_if(Cow::Owned(item.into_inner()))?;
continue;
}
if let ast::ItemKind::Mod(ref sub_mod) = item.kind {
self.visit_sub_mod(&item, Module::new(Cow::Owned(sub_mod.clone()), &item.attrs))?;
}
}
Ok(())
}
/// Visit modules from AST.
fn visit_mod_from_ast(&mut self, module: &'ast ast::Mod) -> Result<(), ModuleResolutionError> {
for item in &module.items {
if is_cfg_if(item) {
self.visit_cfg_if(Cow::Borrowed(item))?;
}
if let ast::ItemKind::Mod(ref sub_mod) = item.kind {
self.visit_sub_mod(item, Module::new(Cow::Borrowed(sub_mod), &item.attrs))?;
}
}
Ok(())
}
fn visit_sub_mod(
&mut self,
item: &'c ast::Item,
sub_mod: Module<'ast>,
) -> Result<(), ModuleResolutionError> {
let old_directory = self.directory.clone();
let sub_mod_kind = self.peek_sub_mod(item, &sub_mod)?;
if let Some(sub_mod_kind) = sub_mod_kind {
self.insert_sub_mod(sub_mod_kind.clone())?;
self.visit_sub_mod_inner(sub_mod, sub_mod_kind)?;
}
self.directory = old_directory;
Ok(())
}
/// Inspect the given sub-module which we are about to visit and returns its kind.
fn peek_sub_mod(
&self,
item: &'c ast::Item,
sub_mod: &Module<'ast>,
) -> Result<Option<SubModKind<'c, 'ast>>, ModuleResolutionError> {
if contains_skip(&item.attrs) {
return Ok(None);
}
if is_mod_decl(item) {
// mod foo;
// Look for an extern file.
self.find_external_module(item.ident, &item.attrs, sub_mod)
} else {
// An internal module (`mod foo { /* ... */ }`);
Ok(Some(SubModKind::Internal(item)))
}
}
fn insert_sub_mod(
&mut self,
sub_mod_kind: SubModKind<'c, 'ast>,
) -> Result<(), ModuleResolutionError> {
match sub_mod_kind {
SubModKind::External(mod_path, _, sub_mod) => {
self.file_map
.entry(FileName::Real(mod_path))
.or_insert(sub_mod);
}
SubModKind::MultiExternal(mods) => {
for (mod_path, _, sub_mod) in mods {
self.file_map
.entry(FileName::Real(mod_path))
.or_insert(sub_mod);
}
}
_ => {}
}
Ok(())
}
fn visit_sub_mod_inner(
&mut self,
sub_mod: Module<'ast>,
sub_mod_kind: SubModKind<'c, 'ast>,
) -> Result<(), ModuleResolutionError> {
match sub_mod_kind {
SubModKind::External(mod_path, directory_ownership, sub_mod) => {
let directory = Directory {
path: mod_path.parent().unwrap().to_path_buf(),
ownership: directory_ownership,
};
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))
}
SubModKind::Internal(ref item) => {
self.push_inline_mod_directory(item.ident, &item.attrs);
self.visit_sub_mod_after_directory_update(sub_mod, None)
}
SubModKind::MultiExternal(mods) => {
for (mod_path, directory_ownership, sub_mod) in mods {
let directory = Directory {
path: mod_path.parent().unwrap().to_path_buf(),
ownership: directory_ownership,
};
self.visit_sub_mod_after_directory_update(sub_mod, Some(directory))?;
}
Ok(())
}
}
}
fn visit_sub_mod_after_directory_update(
&mut self,
sub_mod: Module<'ast>,
directory: Option<Directory>,
) -> Result<(), ModuleResolutionError> {
if let Some(directory) = directory {
self.directory = directory;
}
match sub_mod.ast_mod {
Cow::Borrowed(sub_mod) => self.visit_mod_from_ast(sub_mod),
Cow::Owned(sub_mod) => self.visit_mod_outside_ast(sub_mod),
}
}
/// Find a file path in the filesystem which corresponds to the given module.
fn find_external_module(
&self,
mod_name: symbol::Ident,
attrs: &[ast::Attribute],
sub_mod: &Module<'ast>,
) -> Result<Option<SubModKind<'c, 'ast>>, ModuleResolutionError> {
let relative = match self.directory.ownership {
DirectoryOwnership::Owned { relative } => relative,
DirectoryOwnership::UnownedViaBlock | DirectoryOwnership::UnownedViaMod => None,
};
if let Some(path) = Parser::submod_path_from_attr(attrs, &self.directory.path) {
if self.parse_sess.is_file_parsed(&path) {
return Ok(None);
}
return match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.ast_mod.inner)
{
Ok(m) => Ok(Some(SubModKind::External(
path,
DirectoryOwnership::Owned { relative: None },
Module::new(Cow::Owned(m.0), &m.1),
))),
Err(ParserError::ParseError) => Err(ModuleResolutionError {
module: mod_name.to_string(),
kind: ModuleResolutionErrorKind::ParseError { file: path },
}),
Err(..) => Err(ModuleResolutionError {
module: mod_name.to_string(),
kind: ModuleResolutionErrorKind::NotFound { file: path },
}),
};
}
// Look for nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
let mut mods_outside_ast = self.find_mods_outside_of_ast(attrs, sub_mod);
match self
.parse_sess
.default_submod_path(mod_name, relative, &self.directory.path)
.result
{
Ok(ModulePathSuccess {
path, ownership, ..
}) => {
let outside_mods_empty = mods_outside_ast.is_empty();
let should_insert = !mods_outside_ast
.iter()
.any(|(outside_path, _, _)| outside_path == &path);
if self.parse_sess.is_file_parsed(&path) {
if outside_mods_empty {
return Ok(None);
} else {
if should_insert {
mods_outside_ast.push((path, ownership, sub_mod.clone()));
}
return Ok(Some(SubModKind::MultiExternal(mods_outside_ast)));
}
}
match Parser::parse_file_as_module(self.parse_sess, &path, sub_mod.ast_mod.inner) {
Ok(m) if outside_mods_empty => Ok(Some(SubModKind::External(
path,
ownership,
Module::new(Cow::Owned(m.0), &m.1),
))),
Ok(m) => {
mods_outside_ast.push((
path.clone(),
ownership,
Module::new(Cow::Owned(m.0), &m.1),
));
if should_insert {
mods_outside_ast.push((path, ownership, sub_mod.clone()));
}
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
}
Err(ParserError::ParseError) => Err(ModuleResolutionError {
module: mod_name.to_string(),
kind: ModuleResolutionErrorKind::ParseError { file: path },
}),
Err(..) if outside_mods_empty => Err(ModuleResolutionError {
module: mod_name.to_string(),
kind: ModuleResolutionErrorKind::NotFound { file: path },
}),
Err(..) => {
if should_insert {
mods_outside_ast.push((path, ownership, sub_mod.clone()));
}
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
}
}
}
Err(mut e) if !mods_outside_ast.is_empty() => {
e.cancel();
Ok(Some(SubModKind::MultiExternal(mods_outside_ast)))
}
Err(mut e) => {
e.cancel();
Err(ModuleResolutionError {
module: mod_name.to_string(),
kind: ModuleResolutionErrorKind::NotFound {
file: self.directory.path.clone(),
},
})
}
}
}
fn push_inline_mod_directory(&mut self, id: symbol::Ident, attrs: &[ast::Attribute]) {
if let Some(path) = find_path_value(attrs) {
self.directory.path.push(&*path.as_str());
self.directory.ownership = DirectoryOwnership::Owned { relative: None };
} else {
// We have to push on the current module name in the case of relative
// paths in order to ensure that any additional module paths from inline
// `mod x { ... }` come after the relative extension.
//
// For example, a `mod z { ... }` inside `x/y.rs` should set the current
// directory path to `/x/y/z`, not `/x/z` with a relative offset of `y`.
if let DirectoryOwnership::Owned { relative } = &mut self.directory.ownership {
if let Some(ident) = relative.take() {
// remove the relative offset
self.directory.path.push(&*ident.as_str());
}
}
self.directory.path.push(&*id.as_str());
}
}
fn find_mods_outside_of_ast(
&self,
attrs: &[ast::Attribute],
sub_mod: &Module<'ast>,
) -> Vec<(PathBuf, DirectoryOwnership, Module<'ast>)> {
// Filter nested path, like `#[cfg_attr(feature = "foo", path = "bar.rs")]`.
let mut path_visitor = visitor::PathVisitor::default();
for attr in attrs.iter() {
if let Some(meta) = attr.meta() {
path_visitor.visit_meta_item(&meta)
}
}
let mut result = vec![];
for path in path_visitor.paths() {
let mut actual_path = self.directory.path.clone();
actual_path.push(&path);
if !actual_path.exists() {
continue;
}
if self.parse_sess.is_file_parsed(&actual_path) {
// If the specified file is already parsed, then we just use that.
result.push((
actual_path,
DirectoryOwnership::Owned { relative: None },
sub_mod.clone(),
));
continue;
}
let m = match Parser::parse_file_as_module(
self.parse_sess,
&actual_path,
sub_mod.ast_mod.inner,
) {
Ok(m) => m,
Err(..) => continue,
};
result.push((
actual_path,
DirectoryOwnership::Owned { relative: None },
Module::new(Cow::Owned(m.0), &m.1),
))
}
result
}
}
fn path_value(attr: &ast::Attribute) -> Option<Symbol> {
if attr.check_name(sym::path) {
attr.value_str()
} else {
None
}
}
// N.B., even when there are multiple `#[path = ...]` attributes, we just need to
// examine the first one, since rustc ignores the second and the subsequent ones
// as unused attributes.
fn find_path_value(attrs: &[ast::Attribute]) -> Option<Symbol> {
attrs.iter().flat_map(path_value).next()
}
fn is_cfg_if(item: &ast::Item) -> bool {
match item.kind {
ast::ItemKind::MacCall(ref mac) => {
if let Some(first_segment) = mac.path.segments.first() {
if first_segment.ident.name == *CFG_IF {
return true;
}
}
false
}
_ => false,
}
}