-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathlib.rs
215 lines (178 loc) · 6.64 KB
/
lib.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
#![doc = include_str!("../README.md")]
#![doc(
html_logo_url = "https://raw.githubusercontent.com/paradigmxyz/solar/main/assets/logo.png",
html_favicon_url = "https://raw.githubusercontent.com/paradigmxyz/solar/main/assets/favicon.ico"
)]
#![cfg_attr(feature = "nightly", feature(rustc_attrs), allow(internal_features))]
#![cfg_attr(docsrs, feature(doc_cfg, doc_auto_cfg))]
#[macro_use]
extern crate tracing;
use rayon::prelude::*;
use solar_data_structures::{trustme, OnDrop};
use solar_interface::{config::CompilerStage, Result, Session};
use thread_local::ThreadLocal;
use ty::Gcx;
// Convenience re-exports.
pub use ::thread_local;
pub use bumpalo;
pub use solar_ast as ast;
pub use solar_interface as interface;
mod ast_lowering;
mod ast_passes;
mod parse;
pub use parse::{ParsedSource, ParsedSources, ParsingContext};
pub mod builtins;
pub mod eval;
pub mod hir;
pub use hir::{Arena, Hir};
pub mod ty;
mod typeck;
mod emit;
pub mod stats;
/// Parses and semantically analyzes all the loaded sources, recursing into imports.
pub fn parse_and_resolve(pcx: ParsingContext<'_>) -> Result<()> {
let _ = parse_and_lower(pcx, None)?;
Ok(())
}
/// Parses and lowers to HIR, recursing into imports. If called with an external HIR arena then
/// returns the global compilation context (which can be used to access HIR).
pub fn parse_and_lower<'hir>(
pcx: ParsingContext<'hir>,
hir_arena: Option<&'hir ThreadLocal<Arena>>,
) -> Result<Option<Gcx<'hir>>> {
let sess = pcx.sess;
if pcx.sources.is_empty() {
let msg = "no files found";
let note = "if you wish to use the standard input, please specify `-` explicitly";
return Err(sess.dcx.err(msg).note(note).emit());
}
let ast_arenas = OnDrop::new(ThreadLocal::<ast::Arena>::new(), |mut arenas| {
debug!(asts_allocated = arenas.iter_mut().map(|a| a.allocated_bytes()).sum::<usize>());
debug_span!("dropping_ast_arenas").in_scope(|| drop(arenas));
});
let mut sources = pcx.parse(&ast_arenas);
sources.topo_sort();
if let Some(hir_arena) = hir_arena {
let (hir, symbol_resolver) = lower(sess, &sources, hir_arena.get_or_default())?;
let global_context =
OnDrop::new(ty::GlobalCtxt::new(sess, hir_arena, hir, symbol_resolver), |gcx| {
debug_span!("drop_gcx").in_scope(|| drop(gcx));
});
let gcx = ty::Gcx::new(unsafe { trustme::decouple_lt(&global_context) });
return Ok(Some(gcx));
}
if let Some(dump) = &sess.opts.unstable.dump {
if dump.kind.is_ast() {
dump_ast(sess, &sources, dump.paths.as_deref())?;
}
}
if sess.opts.unstable.ast_stats {
for source in sources.asts() {
stats::print_ast_stats(source, "AST STATS", "ast-stats");
}
}
if sess.opts.language.is_yul() || sess.stop_after(CompilerStage::Parsed) {
return Ok(None);
}
let hir_arena = OnDrop::new(ThreadLocal::<hir::Arena>::new(), |hir_arena| {
debug!(hir_allocated = hir_arena.get_or_default().allocated_bytes());
debug_span!("dropping_hir_arena").in_scope(|| drop(hir_arena));
});
let (hir, symbol_resolver) = lower(sess, &sources, hir_arena.get_or_default())?;
// Drop the ASTs and AST arenas in a separate thread.
sess.spawn({
// TODO: The transmute is required because `sources` borrows from `ast_arenas`,
// even though both are moved in the closure.
let sources =
unsafe { std::mem::transmute::<ParsedSources<'_>, ParsedSources<'static>>(sources) };
move || {
debug_span!("drop_asts").in_scope(|| drop(sources));
drop(ast_arenas);
}
});
let global_context =
OnDrop::new(ty::GlobalCtxt::new(sess, &hir_arena, hir, symbol_resolver), |gcx| {
debug_span!("drop_gcx").in_scope(|| drop(gcx));
});
let gcx = ty::Gcx::new(unsafe { trustme::decouple_lt(&global_context) });
analysis(gcx)?;
Ok(None)
}
/// Lowers the parsed ASTs into the HIR.
fn lower<'sess, 'hir>(
sess: &'sess Session,
sources: &ParsedSources<'_>,
arena: &'hir hir::Arena,
) -> Result<(hir::Hir<'hir>, ast_lowering::SymbolResolver<'sess>)> {
debug_span!("all_ast_passes").in_scope(|| {
sources.par_asts().for_each(|ast| {
ast_passes::run(sess, ast);
});
});
sess.dcx.has_errors()?;
Ok(ast_lowering::lower(sess, sources, arena))
}
#[instrument(level = "debug", skip_all)]
fn analysis(gcx: Gcx<'_>) -> Result<()> {
if let Some(dump) = &gcx.sess.opts.unstable.dump {
if dump.kind.is_hir() {
dump_hir(gcx, dump.paths.as_deref())?;
}
}
// Lower HIR types.
gcx.hir.par_item_ids().for_each(|id| {
let _ = gcx.type_of_item(id);
match id {
hir::ItemId::Struct(id) => _ = gcx.struct_field_types(id),
hir::ItemId::Contract(id) => _ = gcx.interface_functions(id),
_ => {}
}
});
gcx.sess.dcx.has_errors()?;
typeck::check(gcx);
gcx.sess.dcx.has_errors()?;
if !gcx.sess.opts.emit.is_empty() {
emit::emit(gcx);
gcx.sess.dcx.has_errors()?;
}
Ok(())
}
fn dump_ast(sess: &Session, sources: &ParsedSources<'_>, paths: Option<&[String]>) -> Result<()> {
if let Some(paths) = paths {
for path in paths {
if let Some(source) = sources.iter().find(|&s| match_file_name(&s.file.name, path)) {
println!("{source:#?}");
} else {
let msg = format!("`-Zdump=ast={path:?}` did not match any source file");
let note = format!(
"available source files: {}",
sources
.iter()
.map(|s| s.file.name.display().to_string())
.collect::<Vec<_>>()
.join(", ")
);
return Err(sess.dcx.err(msg).note(note).emit());
}
}
} else {
println!("{sources:#?}");
}
Ok(())
}
fn dump_hir(gcx: Gcx<'_>, paths: Option<&[String]>) -> Result<()> {
println!("{:#?}", gcx.hir);
if let Some(paths) = paths {
println!("\nPaths not yet implemented: {paths:#?}");
}
Ok(())
}
fn match_file_name(name: &solar_interface::source_map::FileName, path: &str) -> bool {
match name {
solar_interface::source_map::FileName::Real(path_buf) => {
path_buf.as_os_str() == path || path_buf.file_stem() == Some(path.as_ref())
}
solar_interface::source_map::FileName::Stdin => path == "stdin" || path == "<stdin>",
solar_interface::source_map::FileName::Custom(name) => path == name,
}
}