-
Notifications
You must be signed in to change notification settings - Fork 243
/
Copy pathmod.rs
400 lines (351 loc) · 14.1 KB
/
mod.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
use crate::graph::CrateId;
use crate::hir::def_collector::dc_crate::{CompilationError, DefCollector};
use crate::hir::Context;
use crate::node_interner::{FuncId, GlobalId, NodeInterner, StructId};
use crate::parse_program;
use crate::parser::{ParsedModule, ParserError};
use crate::token::{FunctionAttribute, SecondaryAttribute, TestScope};
use fm::{FileId, FileManager};
use noirc_arena::{Arena, Index};
use noirc_errors::Location;
use std::collections::{BTreeMap, HashMap};
mod module_def;
pub use module_def::*;
mod item_scope;
pub use item_scope::*;
mod module_data;
pub use module_data::*;
mod namespace;
pub use namespace::*;
/// The name that is used for a non-contract program's entry-point function.
pub const MAIN_FUNCTION: &str = "main";
// XXX: Ultimately, we want to constrain an index to be of a certain type just like in RA
/// Lets first check if this is offered by any external crate
/// XXX: RA has made this a crate on crates.io
#[derive(Debug, PartialEq, Eq, Copy, Clone, Hash, PartialOrd, Ord)]
pub struct LocalModuleId(pub Index);
impl LocalModuleId {
pub fn dummy_id() -> LocalModuleId {
LocalModuleId(Index::dummy())
}
}
#[derive(Debug, Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct ModuleId {
pub krate: CrateId,
pub local_id: LocalModuleId,
}
impl ModuleId {
pub fn dummy_id() -> ModuleId {
ModuleId { krate: CrateId::dummy_id(), local_id: LocalModuleId::dummy_id() }
}
pub fn module(self, def_maps: &DefMaps) -> &ModuleData {
&def_maps[&self.krate].modules()[self.local_id.0]
}
/// Returns this module's parent, if there's any.
pub fn parent(self, def_maps: &DefMaps) -> Option<ModuleId> {
let module_data = &def_maps[&self.krate].modules()[self.local_id.0];
module_data.parent.map(|local_id| ModuleId { krate: self.krate, local_id })
}
}
pub type DefMaps = BTreeMap<CrateId, CrateDefMap>;
/// Map of all modules and scopes defined within a crate.
///
/// The definitions of the crate are accessible indirectly via the scopes of each module.
#[derive(Debug)]
pub struct CrateDefMap {
pub(crate) root: LocalModuleId,
pub(crate) modules: Arena<ModuleData>,
pub(crate) krate: CrateId,
/// Maps an external dependency's name to its root module id.
pub(crate) extern_prelude: BTreeMap<String, ModuleId>,
}
impl CrateDefMap {
/// Collect all definitions in the crate
pub fn collect_defs(
crate_id: CrateId,
context: &mut Context,
debug_comptime_in_file: Option<&str>,
error_on_unused_imports: bool,
) -> Vec<(CompilationError, FileId)> {
// Check if this Crate has already been compiled
// XXX: There is probably a better alternative for this.
// Without this check, the compiler will panic as it does not
// expect the same crate to be processed twice. It would not
// make the implementation wrong, if the same crate was processed twice, it just makes it slow.
let mut errors: Vec<(CompilationError, FileId)> = vec![];
if context.def_map(&crate_id).is_some() {
return errors;
}
// First parse the root file.
let root_file_id = context.crate_graph[crate_id].root_file_id;
let (ast, parsing_errors) = context.parsed_file_results(root_file_id);
let ast = ast.into_sorted();
// Allocate a default Module for the root, giving it a ModuleId
let mut modules: Arena<ModuleData> = Arena::default();
let location = Location::new(Default::default(), root_file_id);
let root = modules.insert(ModuleData::new(
None,
location,
Vec::new(),
ast.inner_attributes.clone(),
false, // is contract
false, // is struct
));
let def_map = CrateDefMap {
root: LocalModuleId(root),
modules,
krate: crate_id,
extern_prelude: BTreeMap::new(),
};
// Now we want to populate the CrateDefMap using the DefCollector
errors.extend(DefCollector::collect_crate_and_dependencies(
def_map,
context,
ast,
root_file_id,
debug_comptime_in_file,
error_on_unused_imports,
));
errors.extend(
parsing_errors.iter().map(|e| (e.clone().into(), root_file_id)).collect::<Vec<_>>(),
);
errors
}
pub fn root(&self) -> LocalModuleId {
self.root
}
pub fn modules(&self) -> &Arena<ModuleData> {
&self.modules
}
pub fn modules_mut(&mut self) -> &mut Arena<ModuleData> {
&mut self.modules
}
pub fn krate(&self) -> CrateId {
self.krate
}
/// Find the main function for this crate
pub fn main_function(&self) -> Option<FuncId> {
let root_module = &self.modules()[self.root.0];
// This function accepts an Ident, so we attach a dummy span to
// "main". Equality is implemented only on the contents.
root_module.find_func_with_name(&MAIN_FUNCTION.into())
}
pub fn file_id(&self, module_id: LocalModuleId) -> FileId {
self.modules[module_id.0].location.file
}
/// Go through all modules in this crate, and find all functions in
/// each module with the #[test] attribute
pub fn get_all_test_functions<'a>(
&'a self,
interner: &'a NodeInterner,
) -> impl Iterator<Item = TestFunction> + 'a {
self.modules.iter().flat_map(|(_, module)| {
module.value_definitions().filter_map(|id| {
if let Some(func_id) = id.as_function() {
let attributes = interner.function_attributes(&func_id);
match attributes.function() {
Some(FunctionAttribute::Test(scope)) => {
let location = interner.function_meta(&func_id).name.location;
Some(TestFunction::new(func_id, scope.clone(), location))
}
_ => None,
}
} else {
None
}
})
})
}
/// Go through all modules in this crate, and find all functions in
/// each module with the #[export] attribute
pub fn get_all_exported_functions<'a>(
&'a self,
interner: &'a NodeInterner,
) -> impl Iterator<Item = FuncId> + 'a {
self.modules.iter().flat_map(|(_, module)| {
module.value_definitions().filter_map(|id| {
if let Some(func_id) = id.as_function() {
let attributes = interner.function_attributes(&func_id);
attributes.has_export().then_some(func_id)
} else {
None
}
})
})
}
/// Go through all modules in this crate, find all `contract ... { ... }` declarations,
/// and collect them all into a Vec.
pub fn get_all_contracts(&self, interner: &NodeInterner) -> Vec<Contract> {
self.modules
.iter()
.filter_map(|(id, module)| {
if module.is_contract {
let functions = module
.value_definitions()
.filter_map(|id| {
id.as_function().map(|function_id| {
let is_entry_point = interner
.function_attributes(&function_id)
.is_contract_entry_point();
ContractFunctionMeta { function_id, is_entry_point }
})
})
.collect();
let mut outputs =
ContractOutputs { structs: HashMap::new(), globals: HashMap::new() };
interner.get_all_globals().iter().for_each(|global_info| {
interner.global_attributes(&global_info.id).iter().for_each(|attr| {
if let SecondaryAttribute::Abi(tag) = attr {
if let Some(tagged) = outputs.globals.get_mut(tag) {
tagged.push(global_info.id);
} else {
outputs.globals.insert(tag.to_string(), vec![global_info.id]);
}
}
});
});
module.type_definitions().for_each(|id| {
if let ModuleDefId::TypeId(struct_id) = id {
interner.struct_attributes(&struct_id).iter().for_each(|attr| {
if let SecondaryAttribute::Abi(tag) = attr {
if let Some(tagged) = outputs.structs.get_mut(tag) {
tagged.push(struct_id);
} else {
outputs.structs.insert(tag.to_string(), vec![struct_id]);
}
}
});
}
});
let name = self.get_module_path(id, module.parent);
Some(Contract { name, location: module.location, functions, outputs })
} else {
None
}
})
.collect()
}
/// Find a child module's name by inspecting its parent.
/// Currently required as modules do not store their own names.
pub fn get_module_path(&self, child_id: Index, parent: Option<LocalModuleId>) -> String {
self.get_module_path_with_separator(child_id, parent, ".")
}
pub fn get_module_path_with_separator(
&self,
child_id: Index,
parent: Option<LocalModuleId>,
separator: &str,
) -> String {
if let Some(id) = parent {
let parent = &self.modules[id.0];
let name = parent
.children
.iter()
.find(|(_, id)| id.0 == child_id)
.map(|(name, _)| &name.0.contents)
.expect("Child module was not a child of the given parent module");
let parent_name = self.get_module_path_with_separator(id.0, parent.parent, separator);
if parent_name.is_empty() {
name.to_string()
} else {
format!("{parent_name}{separator}{name}")
}
} else {
String::new()
}
}
/// Return a topological ordering of each module such that any child modules
/// are before their parent modules. Sibling modules will respect the ordering
/// declared from their parent module (the `mod foo; mod bar;` declarations).
pub fn get_module_topological_order(&self) -> HashMap<LocalModuleId, usize> {
let mut ordering = HashMap::default();
self.topologically_sort_modules(self.root, &mut 0, &mut ordering);
ordering
}
fn topologically_sort_modules(
&self,
current: LocalModuleId,
index: &mut usize,
ordering: &mut HashMap<LocalModuleId, usize>,
) {
for child in &self.modules[current.0].child_declaration_order {
self.topologically_sort_modules(*child, index, ordering);
}
ordering.insert(current, *index);
*index += 1;
}
}
/// Specifies a contract function and extra metadata that
/// one can use when processing a contract function.
///
/// One of these is whether the contract function is an entry point.
/// The caller should only type-check these functions and not attempt
/// to create a circuit for them.
pub struct ContractFunctionMeta {
pub function_id: FuncId,
/// Indicates whether the function is an entry point
pub is_entry_point: bool,
}
pub struct ContractOutputs {
pub structs: HashMap<String, Vec<StructId>>,
pub globals: HashMap<String, Vec<GlobalId>>,
}
/// A 'contract' in Noir source code with a given name, functions and events.
/// This is not an AST node, it is just a convenient form to return for CrateDefMap::get_all_contracts.
pub struct Contract {
/// To keep `name` semi-unique, it is prefixed with the names of parent modules via CrateDefMap::get_module_path
pub name: String,
pub location: Location,
pub functions: Vec<ContractFunctionMeta>,
pub outputs: ContractOutputs,
}
/// Given a FileId, fetch the File, from the FileManager and parse it's content
pub fn parse_file(fm: &FileManager, file_id: FileId) -> (ParsedModule, Vec<ParserError>) {
let file_source = fm.fetch_file(file_id).expect("File does not exist");
parse_program(file_source)
}
impl std::ops::Index<LocalModuleId> for CrateDefMap {
type Output = ModuleData;
fn index(&self, local_module_id: LocalModuleId) -> &ModuleData {
&self.modules[local_module_id.0]
}
}
impl std::ops::IndexMut<LocalModuleId> for CrateDefMap {
fn index_mut(&mut self, local_module_id: LocalModuleId) -> &mut ModuleData {
&mut self.modules[local_module_id.0]
}
}
pub struct TestFunction {
id: FuncId,
scope: TestScope,
location: Location,
}
impl TestFunction {
fn new(id: FuncId, scope: TestScope, location: Location) -> Self {
TestFunction { id, scope, location }
}
/// Returns the function id of the test function
pub fn get_id(&self) -> FuncId {
self.id
}
pub fn file_id(&self) -> FileId {
self.location.file
}
/// Returns true if the test function has been specified to fail
/// This is done by annotating the function with `#[test(should_fail)]`
/// or `#[test(should_fail_with = "reason")]`
pub fn should_fail(&self) -> bool {
match self.scope {
TestScope::ShouldFailWith { .. } => true,
TestScope::None => false,
}
}
/// Returns the reason for the test function to fail if specified
/// by the user.
pub fn failure_reason(&self) -> Option<&str> {
match &self.scope {
TestScope::None => None,
TestScope::ShouldFailWith { reason } => reason.as_deref(),
}
}
}