mirror of
https://github.com/roc-lang/roc.git
synced 2025-09-26 13:29:12 +00:00
moved all crates into seperate folder + related path fixes
This commit is contained in:
parent
12ef03bb86
commit
eee85fa45d
1063 changed files with 92 additions and 93 deletions
470
crates/compiler/load_internal/src/docs.rs
Normal file
470
crates/compiler/load_internal/src/docs.rs
Normal file
|
@ -0,0 +1,470 @@
|
|||
use crate::docs::DocEntry::DetachedDoc;
|
||||
use crate::docs::TypeAnnotation::{Apply, BoundVariable, Function, NoTypeAnn, Record, TagUnion};
|
||||
use crate::file::LoadedModule;
|
||||
use roc_can::scope::Scope;
|
||||
use roc_module::ident::ModuleName;
|
||||
use roc_module::symbol::IdentIds;
|
||||
use roc_parse::ast::AssignedField;
|
||||
use roc_parse::ast::{self, ExtractSpaces, TypeHeader};
|
||||
use roc_parse::ast::{CommentOrNewline, TypeDef, ValueDef};
|
||||
|
||||
// Documentation generation requirements
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Documentation {
|
||||
pub name: String,
|
||||
pub version: String,
|
||||
pub docs: String,
|
||||
pub modules: Vec<LoadedModule>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct ModuleDocumentation {
|
||||
pub name: String,
|
||||
pub entries: Vec<DocEntry>,
|
||||
pub scope: Scope,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum DocEntry {
|
||||
DocDef(DocDef),
|
||||
DetachedDoc(String),
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct DocDef {
|
||||
pub name: String,
|
||||
pub type_vars: Vec<String>,
|
||||
pub type_annotation: TypeAnnotation,
|
||||
pub docs: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum TypeAnnotation {
|
||||
TagUnion {
|
||||
tags: Vec<Tag>,
|
||||
extension: Box<TypeAnnotation>,
|
||||
},
|
||||
Function {
|
||||
args: Vec<TypeAnnotation>,
|
||||
output: Box<TypeAnnotation>,
|
||||
},
|
||||
ObscuredTagUnion,
|
||||
ObscuredRecord,
|
||||
BoundVariable(String),
|
||||
Apply {
|
||||
name: String,
|
||||
parts: Vec<TypeAnnotation>,
|
||||
},
|
||||
Record {
|
||||
fields: Vec<RecordField>,
|
||||
extension: Box<TypeAnnotation>,
|
||||
},
|
||||
Ability {
|
||||
members: Vec<AbilityMember>,
|
||||
},
|
||||
Wildcard,
|
||||
NoTypeAnn,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum RecordField {
|
||||
RecordField {
|
||||
name: String,
|
||||
type_annotation: TypeAnnotation,
|
||||
},
|
||||
OptionalField {
|
||||
name: String,
|
||||
type_annotation: TypeAnnotation,
|
||||
},
|
||||
LabelOnly {
|
||||
name: String,
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AbilityMember {
|
||||
pub name: String,
|
||||
pub type_annotation: TypeAnnotation,
|
||||
pub able_variables: Vec<(String, TypeAnnotation)>,
|
||||
pub docs: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct Tag {
|
||||
pub name: String,
|
||||
pub values: Vec<TypeAnnotation>,
|
||||
}
|
||||
|
||||
pub fn generate_module_docs(
|
||||
scope: Scope,
|
||||
module_name: ModuleName,
|
||||
parsed_defs: &roc_parse::ast::Defs,
|
||||
) -> ModuleDocumentation {
|
||||
let entries = generate_entry_docs(&scope.locals.ident_ids, parsed_defs);
|
||||
|
||||
ModuleDocumentation {
|
||||
name: module_name.as_str().to_string(),
|
||||
scope,
|
||||
entries,
|
||||
}
|
||||
}
|
||||
|
||||
fn detached_docs_from_comments_and_new_lines<'a>(
|
||||
comments_or_new_lines: impl Iterator<Item = &'a roc_parse::ast::CommentOrNewline<'a>>,
|
||||
) -> Vec<String> {
|
||||
let mut detached_docs: Vec<String> = Vec::new();
|
||||
|
||||
let mut docs = String::new();
|
||||
|
||||
for comment_or_new_line in comments_or_new_lines {
|
||||
match comment_or_new_line {
|
||||
CommentOrNewline::DocComment(doc_str) => {
|
||||
docs.push_str(doc_str);
|
||||
docs.push('\n');
|
||||
}
|
||||
|
||||
CommentOrNewline::LineComment(_) | CommentOrNewline::Newline => {
|
||||
if !docs.is_empty() {
|
||||
detached_docs.push(docs.clone());
|
||||
}
|
||||
|
||||
docs = String::new();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
detached_docs
|
||||
}
|
||||
|
||||
fn generate_entry_docs<'a>(
|
||||
ident_ids: &'a IdentIds,
|
||||
defs: &roc_parse::ast::Defs<'a>,
|
||||
) -> Vec<DocEntry> {
|
||||
use roc_parse::ast::Pattern;
|
||||
|
||||
let mut acc = Vec::with_capacity(defs.tags.len());
|
||||
let mut before_comments_or_new_lines: Option<&[CommentOrNewline]> = None;
|
||||
let mut scratchpad = Vec::new();
|
||||
|
||||
for (index, either_index) in defs.tags.iter().enumerate() {
|
||||
let spaces_before = &defs.spaces[defs.space_before[index].indices()];
|
||||
|
||||
scratchpad.clear();
|
||||
scratchpad.extend(
|
||||
before_comments_or_new_lines
|
||||
.take()
|
||||
.iter()
|
||||
.flat_map(|e| e.iter()),
|
||||
);
|
||||
scratchpad.extend(spaces_before);
|
||||
|
||||
let docs = comments_or_new_lines_to_docs(&scratchpad);
|
||||
|
||||
match either_index.split() {
|
||||
Err(value_index) => match &defs.value_defs[value_index.index()] {
|
||||
ValueDef::Annotation(loc_pattern, loc_ann) => {
|
||||
if let Pattern::Identifier(identifier) = loc_pattern.value {
|
||||
// Check if the definition is exposed
|
||||
if ident_ids.get_id(identifier).is_some() {
|
||||
let name = identifier.to_string();
|
||||
let doc_def = DocDef {
|
||||
name,
|
||||
type_annotation: type_to_docs(false, loc_ann.value),
|
||||
type_vars: Vec::new(),
|
||||
docs,
|
||||
};
|
||||
acc.push(DocEntry::DocDef(doc_def));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ValueDef::AnnotatedBody {
|
||||
ann_pattern,
|
||||
ann_type,
|
||||
..
|
||||
} => {
|
||||
if let Pattern::Identifier(identifier) = ann_pattern.value {
|
||||
// Check if the definition is exposed
|
||||
if ident_ids.get_id(identifier).is_some() {
|
||||
let doc_def = DocDef {
|
||||
name: identifier.to_string(),
|
||||
type_annotation: type_to_docs(false, ann_type.value),
|
||||
type_vars: Vec::new(),
|
||||
docs,
|
||||
};
|
||||
acc.push(DocEntry::DocDef(doc_def));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
ValueDef::Body(_, _) => (),
|
||||
|
||||
ValueDef::Expect(c) => todo!("documentation for tests {:?}", c),
|
||||
},
|
||||
Ok(type_index) => match &defs.type_defs[type_index.index()] {
|
||||
TypeDef::Alias {
|
||||
header: TypeHeader { name, vars },
|
||||
ann,
|
||||
} => {
|
||||
let mut type_vars = Vec::new();
|
||||
|
||||
for var in vars.iter() {
|
||||
if let Pattern::Identifier(ident_name) = var.value {
|
||||
type_vars.push(ident_name.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let doc_def = DocDef {
|
||||
name: name.value.to_string(),
|
||||
type_annotation: type_to_docs(false, ann.value),
|
||||
type_vars,
|
||||
docs,
|
||||
};
|
||||
acc.push(DocEntry::DocDef(doc_def));
|
||||
}
|
||||
|
||||
TypeDef::Opaque {
|
||||
header: TypeHeader { name, vars },
|
||||
..
|
||||
} => {
|
||||
let mut type_vars = Vec::new();
|
||||
|
||||
for var in vars.iter() {
|
||||
if let Pattern::Identifier(ident_name) = var.value {
|
||||
type_vars.push(ident_name.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let doc_def = DocDef {
|
||||
name: name.value.to_string(),
|
||||
type_annotation: TypeAnnotation::NoTypeAnn,
|
||||
type_vars,
|
||||
docs,
|
||||
};
|
||||
acc.push(DocEntry::DocDef(doc_def));
|
||||
}
|
||||
|
||||
TypeDef::Ability {
|
||||
header: TypeHeader { name, vars },
|
||||
members,
|
||||
..
|
||||
} => {
|
||||
let mut type_vars = Vec::new();
|
||||
|
||||
for var in vars.iter() {
|
||||
if let Pattern::Identifier(ident_name) = var.value {
|
||||
type_vars.push(ident_name.to_string());
|
||||
}
|
||||
}
|
||||
|
||||
let members = members
|
||||
.iter()
|
||||
.map(|mem| {
|
||||
let extracted = mem.name.value.extract_spaces();
|
||||
let (type_annotation, able_variables) =
|
||||
ability_member_type_to_docs(mem.typ.value);
|
||||
|
||||
AbilityMember {
|
||||
name: extracted.item.to_string(),
|
||||
type_annotation,
|
||||
able_variables,
|
||||
docs: comments_or_new_lines_to_docs(extracted.before),
|
||||
}
|
||||
})
|
||||
.collect();
|
||||
|
||||
let doc_def = DocDef {
|
||||
name: name.value.to_string(),
|
||||
type_annotation: TypeAnnotation::Ability { members },
|
||||
type_vars,
|
||||
docs,
|
||||
};
|
||||
acc.push(DocEntry::DocDef(doc_def));
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
let spaces_after = &defs.spaces[defs.space_after[index].indices()];
|
||||
before_comments_or_new_lines = Some(spaces_after);
|
||||
}
|
||||
|
||||
let it = before_comments_or_new_lines.iter().flat_map(|e| e.iter());
|
||||
|
||||
for detached_doc in detached_docs_from_comments_and_new_lines(it) {
|
||||
acc.push(DetachedDoc(detached_doc));
|
||||
}
|
||||
|
||||
acc
|
||||
}
|
||||
|
||||
fn type_to_docs(in_func_type_ann: bool, type_annotation: ast::TypeAnnotation) -> TypeAnnotation {
|
||||
match type_annotation {
|
||||
ast::TypeAnnotation::TagUnion { tags, ext } => {
|
||||
let mut tags_to_render: Vec<Tag> = Vec::new();
|
||||
|
||||
for tag in tags.iter() {
|
||||
if let Some(tag_ann) = tag_to_doc(in_func_type_ann, tag.value) {
|
||||
tags_to_render.push(tag_ann);
|
||||
}
|
||||
}
|
||||
|
||||
let extension = match ext {
|
||||
None => NoTypeAnn,
|
||||
Some(ext_type_ann) => type_to_docs(in_func_type_ann, ext_type_ann.value),
|
||||
};
|
||||
|
||||
TagUnion {
|
||||
tags: tags_to_render,
|
||||
extension: Box::new(extension),
|
||||
}
|
||||
}
|
||||
ast::TypeAnnotation::BoundVariable(var_name) => BoundVariable(var_name.to_string()),
|
||||
ast::TypeAnnotation::Apply(module_name, type_name, type_ann_parts) => {
|
||||
let mut name = String::new();
|
||||
|
||||
if !module_name.is_empty() {
|
||||
name.push_str(module_name);
|
||||
name.push('.');
|
||||
}
|
||||
|
||||
name.push_str(type_name);
|
||||
|
||||
let mut parts: Vec<TypeAnnotation> = Vec::new();
|
||||
|
||||
for type_ann_part in type_ann_parts {
|
||||
parts.push(type_to_docs(in_func_type_ann, type_ann_part.value));
|
||||
}
|
||||
|
||||
Apply { name, parts }
|
||||
}
|
||||
ast::TypeAnnotation::Record { fields, ext } => {
|
||||
let mut doc_fields = Vec::new();
|
||||
|
||||
for field in fields.items {
|
||||
if let Some(doc_field) = record_field_to_doc(in_func_type_ann, field.value) {
|
||||
doc_fields.push(doc_field);
|
||||
}
|
||||
}
|
||||
let extension = match ext {
|
||||
None => NoTypeAnn,
|
||||
Some(ext_type_ann) => type_to_docs(in_func_type_ann, ext_type_ann.value),
|
||||
};
|
||||
|
||||
Record {
|
||||
fields: doc_fields,
|
||||
extension: Box::new(extension),
|
||||
}
|
||||
}
|
||||
ast::TypeAnnotation::SpaceBefore(&sub_type_ann, _) => {
|
||||
type_to_docs(in_func_type_ann, sub_type_ann)
|
||||
}
|
||||
ast::TypeAnnotation::SpaceAfter(&sub_type_ann, _) => {
|
||||
type_to_docs(in_func_type_ann, sub_type_ann)
|
||||
}
|
||||
ast::TypeAnnotation::Function(ast_arg_anns, output_ann) => {
|
||||
let mut doc_arg_anns = Vec::new();
|
||||
|
||||
for ast_arg_ann in ast_arg_anns {
|
||||
doc_arg_anns.push(type_to_docs(true, ast_arg_ann.value));
|
||||
}
|
||||
|
||||
Function {
|
||||
args: doc_arg_anns,
|
||||
output: Box::new(type_to_docs(true, output_ann.value)),
|
||||
}
|
||||
}
|
||||
ast::TypeAnnotation::Wildcard => TypeAnnotation::Wildcard,
|
||||
_ => NoTypeAnn,
|
||||
}
|
||||
}
|
||||
|
||||
fn ability_member_type_to_docs(
|
||||
type_annotation: ast::TypeAnnotation,
|
||||
) -> (TypeAnnotation, Vec<(String, TypeAnnotation)>) {
|
||||
match type_annotation {
|
||||
ast::TypeAnnotation::Where(ta, has_clauses) => {
|
||||
let ta = type_to_docs(false, ta.value);
|
||||
let has_clauses = has_clauses
|
||||
.iter()
|
||||
.map(|hc| {
|
||||
let ast::HasClause { var, ability } = hc.value;
|
||||
(
|
||||
var.value.extract_spaces().item.to_string(),
|
||||
type_to_docs(false, ability.value),
|
||||
)
|
||||
})
|
||||
.collect();
|
||||
|
||||
(ta, has_clauses)
|
||||
}
|
||||
_ => (type_to_docs(false, type_annotation), vec![]),
|
||||
}
|
||||
}
|
||||
|
||||
fn record_field_to_doc(
|
||||
in_func_ann: bool,
|
||||
field: ast::AssignedField<'_, ast::TypeAnnotation>,
|
||||
) -> Option<RecordField> {
|
||||
match field {
|
||||
AssignedField::RequiredValue(name, _, type_ann) => Some(RecordField::RecordField {
|
||||
name: name.value.to_string(),
|
||||
type_annotation: type_to_docs(in_func_ann, type_ann.value),
|
||||
}),
|
||||
AssignedField::SpaceBefore(&sub_field, _) => record_field_to_doc(in_func_ann, sub_field),
|
||||
AssignedField::SpaceAfter(&sub_field, _) => record_field_to_doc(in_func_ann, sub_field),
|
||||
AssignedField::OptionalValue(name, _, type_ann) => Some(RecordField::OptionalField {
|
||||
name: name.value.to_string(),
|
||||
type_annotation: type_to_docs(in_func_ann, type_ann.value),
|
||||
}),
|
||||
AssignedField::LabelOnly(label) => Some(RecordField::LabelOnly {
|
||||
name: label.value.to_string(),
|
||||
}),
|
||||
AssignedField::Malformed(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
// The Option here represents if it is malformed.
|
||||
fn tag_to_doc(in_func_ann: bool, tag: ast::Tag) -> Option<Tag> {
|
||||
match tag {
|
||||
ast::Tag::Apply { name, args } => Some(Tag {
|
||||
name: name.value.to_string(),
|
||||
values: {
|
||||
let mut type_vars = Vec::new();
|
||||
|
||||
for arg in args {
|
||||
type_vars.push(type_to_docs(in_func_ann, arg.value));
|
||||
}
|
||||
|
||||
type_vars
|
||||
},
|
||||
}),
|
||||
ast::Tag::SpaceBefore(&sub_tag, _) => tag_to_doc(in_func_ann, sub_tag),
|
||||
ast::Tag::SpaceAfter(&sub_tag, _) => tag_to_doc(in_func_ann, sub_tag),
|
||||
ast::Tag::Malformed(_) => None,
|
||||
}
|
||||
}
|
||||
|
||||
fn comments_or_new_lines_to_docs<'a>(
|
||||
comments_or_new_lines: &'a [roc_parse::ast::CommentOrNewline<'a>],
|
||||
) -> Option<String> {
|
||||
let mut docs = String::new();
|
||||
|
||||
for comment_or_new_line in comments_or_new_lines.iter() {
|
||||
match comment_or_new_line {
|
||||
CommentOrNewline::DocComment(doc_str) => {
|
||||
docs.push_str(doc_str);
|
||||
docs.push('\n');
|
||||
}
|
||||
CommentOrNewline::Newline | CommentOrNewline::LineComment(_) => {
|
||||
docs = String::new();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if docs.is_empty() {
|
||||
None
|
||||
} else {
|
||||
Some(docs)
|
||||
}
|
||||
}
|
5194
crates/compiler/load_internal/src/file.rs
Normal file
5194
crates/compiler/load_internal/src/file.rs
Normal file
File diff suppressed because it is too large
Load diff
9
crates/compiler/load_internal/src/lib.rs
Normal file
9
crates/compiler/load_internal/src/lib.rs
Normal file
|
@ -0,0 +1,9 @@
|
|||
#![warn(clippy::dbg_macro)]
|
||||
// See github.com/rtfeldman/roc/issues/800 for discussion of the large_enum_variant check.
|
||||
#![allow(clippy::large_enum_variant)]
|
||||
pub mod docs;
|
||||
pub mod file;
|
||||
mod work;
|
||||
|
||||
#[cfg(target_family = "wasm")]
|
||||
mod wasm_system_time;
|
42
crates/compiler/load_internal/src/wasm_system_time.rs
Normal file
42
crates/compiler/load_internal/src/wasm_system_time.rs
Normal file
|
@ -0,0 +1,42 @@
|
|||
#![cfg(target_family = "wasm")]
|
||||
/*
|
||||
For the Web REPL (repl_www), we build the compiler as a Wasm module.
|
||||
SystemTime is the only thing in the compiler that would need a special implementation for this.
|
||||
There is a WASI implementation for it, but we are targeting the browser, not WASI!
|
||||
It's possible to write browser versions of WASI's low-level ABI but we'd rather avoid it.
|
||||
Instead we use these dummy implementations, which should just disappear at compile time.
|
||||
*/
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct SystemTime;
|
||||
|
||||
impl SystemTime {
|
||||
pub fn now() -> Self {
|
||||
SystemTime
|
||||
}
|
||||
pub fn duration_since(&self, _: SystemTime) -> Result<Duration, String> {
|
||||
Ok(Duration)
|
||||
}
|
||||
pub fn elapsed(&self) -> Result<Duration, String> {
|
||||
Ok(Duration)
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
pub struct Duration;
|
||||
|
||||
impl Duration {
|
||||
pub fn checked_sub(&self, _: Duration) -> Option<Duration> {
|
||||
Some(Duration)
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Duration {
|
||||
fn default() -> Self {
|
||||
Duration
|
||||
}
|
||||
}
|
||||
|
||||
impl std::ops::AddAssign for Duration {
|
||||
fn add_assign(&mut self, _: Duration) {}
|
||||
}
|
355
crates/compiler/load_internal/src/work.rs
Normal file
355
crates/compiler/load_internal/src/work.rs
Normal file
|
@ -0,0 +1,355 @@
|
|||
use roc_collections::all::{MutMap, MutSet};
|
||||
use roc_module::symbol::{ModuleId, PackageQualified};
|
||||
|
||||
use std::collections::hash_map::Entry;
|
||||
|
||||
/// NOTE the order of definition of the phases is used by the ord instance
|
||||
/// make sure they are ordered from first to last!
|
||||
#[derive(PartialEq, Eq, PartialOrd, Ord, Hash, Clone, Copy, Debug)]
|
||||
pub enum Phase {
|
||||
LoadHeader,
|
||||
Parse,
|
||||
CanonicalizeAndConstrain,
|
||||
SolveTypes,
|
||||
FindSpecializations,
|
||||
MakeSpecializations,
|
||||
}
|
||||
|
||||
/// NOTE keep up to date manually, from ParseAndGenerateConstraints to the highest phase we support
|
||||
const PHASES: [Phase; 6] = [
|
||||
Phase::LoadHeader,
|
||||
Phase::Parse,
|
||||
Phase::CanonicalizeAndConstrain,
|
||||
Phase::SolveTypes,
|
||||
Phase::FindSpecializations,
|
||||
Phase::MakeSpecializations,
|
||||
];
|
||||
|
||||
#[derive(Debug)]
|
||||
enum Status {
|
||||
NotStarted,
|
||||
Pending,
|
||||
Done,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Eq, Hash)]
|
||||
enum Job<'a> {
|
||||
Step(ModuleId, Phase),
|
||||
ResolveShorthand(&'a str),
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct Dependencies<'a> {
|
||||
waiting_for: MutMap<Job<'a>, MutSet<Job<'a>>>,
|
||||
notifies: MutMap<Job<'a>, MutSet<Job<'a>>>,
|
||||
status: MutMap<Job<'a>, Status>,
|
||||
|
||||
/// module -> modules to make specializations after, whether a module comes before us
|
||||
make_specializations_dependents: MutMap<ModuleId, (MutSet<ModuleId>, bool)>,
|
||||
}
|
||||
|
||||
impl<'a> Dependencies<'a> {
|
||||
/// Add all the dependencies for a module, return (module, phase) pairs that can make progress
|
||||
pub fn add_module(
|
||||
&mut self,
|
||||
module_id: ModuleId,
|
||||
dependencies: &MutSet<PackageQualified<'a, ModuleId>>,
|
||||
goal_phase: Phase,
|
||||
) -> MutSet<(ModuleId, Phase)> {
|
||||
use Phase::*;
|
||||
|
||||
let mut output = MutSet::default();
|
||||
|
||||
for dep in dependencies.iter() {
|
||||
let has_package_dependency = self.add_package_dependency(dep, Phase::LoadHeader);
|
||||
|
||||
let dep = *dep.as_inner();
|
||||
|
||||
if !has_package_dependency {
|
||||
// loading can start immediately on this dependency
|
||||
output.insert((dep, Phase::LoadHeader));
|
||||
}
|
||||
|
||||
// to parse and generate constraints, the headers of all dependencies must be loaded!
|
||||
// otherwise, we don't know whether an imported symbol is actually exposed
|
||||
self.add_dependency_help(module_id, dep, Phase::Parse, Phase::LoadHeader);
|
||||
|
||||
// to canonicalize a module, all its dependencies must be canonicalized
|
||||
self.add_dependency(module_id, dep, Phase::CanonicalizeAndConstrain);
|
||||
|
||||
// to typecheck a module, all its dependencies must be type checked already
|
||||
self.add_dependency(module_id, dep, Phase::SolveTypes);
|
||||
|
||||
if goal_phase >= FindSpecializations {
|
||||
self.add_dependency(module_id, dep, Phase::FindSpecializations);
|
||||
}
|
||||
|
||||
if goal_phase >= MakeSpecializations {
|
||||
self.add_dependency(dep, module_id, Phase::MakeSpecializations);
|
||||
|
||||
let dep_entry = self
|
||||
.make_specializations_dependents
|
||||
.entry(dep)
|
||||
.or_insert((MutSet::default(), false));
|
||||
dep_entry.1 = true;
|
||||
}
|
||||
}
|
||||
|
||||
// Add make specialization dependents
|
||||
let entry = self
|
||||
.make_specializations_dependents
|
||||
.entry(module_id)
|
||||
.or_insert((MutSet::default(), false));
|
||||
debug_assert!(entry.0.is_empty(), "already seen this dep");
|
||||
entry
|
||||
.0
|
||||
.extend(dependencies.iter().map(|dep| *dep.as_inner()));
|
||||
|
||||
// add dependencies for self
|
||||
// phase i + 1 of a file always depends on phase i being completed
|
||||
{
|
||||
let mut i = 0;
|
||||
while PHASES[i] < goal_phase {
|
||||
self.add_dependency_help(module_id, module_id, PHASES[i + 1], PHASES[i]);
|
||||
i += 1;
|
||||
}
|
||||
}
|
||||
|
||||
self.add_to_status(module_id, goal_phase);
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
fn add_to_status(&mut self, module_id: ModuleId, goal_phase: Phase) {
|
||||
for phase in PHASES.iter() {
|
||||
if *phase > goal_phase {
|
||||
break;
|
||||
}
|
||||
|
||||
if let Entry::Vacant(entry) = self.status.entry(Job::Step(module_id, *phase)) {
|
||||
entry.insert(Status::NotStarted);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Propagate a notification, return (module, phase) pairs that can make progress
|
||||
pub fn notify(&mut self, module_id: ModuleId, phase: Phase) -> MutSet<(ModuleId, Phase)> {
|
||||
self.notify_help(Job::Step(module_id, phase))
|
||||
}
|
||||
|
||||
/// Propagate a notification, return (module, phase) pairs that can make progress
|
||||
pub fn notify_package(&mut self, shorthand: &'a str) -> MutSet<(ModuleId, Phase)> {
|
||||
self.notify_help(Job::ResolveShorthand(shorthand))
|
||||
}
|
||||
|
||||
fn notify_help(&mut self, key: Job<'a>) -> MutSet<(ModuleId, Phase)> {
|
||||
self.status.insert(key.clone(), Status::Done);
|
||||
|
||||
let mut output = MutSet::default();
|
||||
|
||||
if let Some(to_notify) = self.notifies.get(&key) {
|
||||
for notify_key in to_notify {
|
||||
let mut is_empty = false;
|
||||
if let Some(waiting_for_pairs) = self.waiting_for.get_mut(notify_key) {
|
||||
waiting_for_pairs.remove(&key);
|
||||
is_empty = waiting_for_pairs.is_empty();
|
||||
}
|
||||
|
||||
if is_empty {
|
||||
self.waiting_for.remove(notify_key);
|
||||
|
||||
if let Job::Step(module, phase) = *notify_key {
|
||||
output.insert((module, phase));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
self.notifies.remove(&key);
|
||||
|
||||
output
|
||||
}
|
||||
|
||||
fn add_package_dependency(
|
||||
&mut self,
|
||||
module: &PackageQualified<'a, ModuleId>,
|
||||
next_phase: Phase,
|
||||
) -> bool {
|
||||
match module {
|
||||
PackageQualified::Unqualified(_) => {
|
||||
// no dependency, we can just start loading the file
|
||||
false
|
||||
}
|
||||
PackageQualified::Qualified(shorthand, module_id) => {
|
||||
let job = Job::ResolveShorthand(shorthand);
|
||||
let next_step = Job::Step(*module_id, next_phase);
|
||||
match self.status.get(&job) {
|
||||
None | Some(Status::NotStarted) | Some(Status::Pending) => {
|
||||
// this shorthand is not resolved, add a dependency
|
||||
{
|
||||
let entry = self
|
||||
.waiting_for
|
||||
.entry(next_step.clone())
|
||||
.or_insert_with(Default::default);
|
||||
|
||||
entry.insert(job.clone());
|
||||
}
|
||||
|
||||
{
|
||||
let entry = self.notifies.entry(job).or_insert_with(Default::default);
|
||||
|
||||
entry.insert(next_step);
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
Some(Status::Done) => {
|
||||
// shorthand is resolved; no dependency
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// A waits for B, and B will notify A when it completes the phase
|
||||
fn add_dependency(&mut self, a: ModuleId, b: ModuleId, phase: Phase) {
|
||||
self.add_dependency_help(a, b, phase, phase);
|
||||
}
|
||||
|
||||
/// phase_a of module a is waiting for phase_b of module_b
|
||||
fn add_dependency_help(&mut self, a: ModuleId, b: ModuleId, phase_a: Phase, phase_b: Phase) {
|
||||
// no need to wait if the dependency is already done!
|
||||
if let Some(Status::Done) = self.status.get(&Job::Step(b, phase_b)) {
|
||||
return;
|
||||
}
|
||||
|
||||
let key = Job::Step(a, phase_a);
|
||||
let value = Job::Step(b, phase_b);
|
||||
match self.waiting_for.get_mut(&key) {
|
||||
Some(existing) => {
|
||||
existing.insert(value);
|
||||
}
|
||||
None => {
|
||||
let mut set = MutSet::default();
|
||||
set.insert(value);
|
||||
self.waiting_for.insert(key, set);
|
||||
}
|
||||
}
|
||||
|
||||
let key = Job::Step(b, phase_b);
|
||||
let value = Job::Step(a, phase_a);
|
||||
match self.notifies.get_mut(&key) {
|
||||
Some(existing) => {
|
||||
existing.insert(value);
|
||||
}
|
||||
None => {
|
||||
let mut set = MutSet::default();
|
||||
set.insert(value);
|
||||
self.notifies.insert(key, set);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn solved_all(&self) -> bool {
|
||||
debug_assert_eq!(self.notifies.is_empty(), self.waiting_for.is_empty());
|
||||
|
||||
for status in self.status.values() {
|
||||
match status {
|
||||
Status::Done => {
|
||||
continue;
|
||||
}
|
||||
_ => {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub fn prepare_start_phase(&mut self, module_id: ModuleId, phase: Phase) -> PrepareStartPhase {
|
||||
match self.status.get_mut(&Job::Step(module_id, phase)) {
|
||||
Some(current @ Status::NotStarted) => {
|
||||
// start this phase!
|
||||
*current = Status::Pending;
|
||||
PrepareStartPhase::Continue
|
||||
}
|
||||
Some(Status::Pending) => {
|
||||
// don't start this task again!
|
||||
PrepareStartPhase::Done
|
||||
}
|
||||
Some(Status::Done) => {
|
||||
// don't start this task again, but tell those waiting for it they can continue
|
||||
let new = self.notify(module_id, phase);
|
||||
|
||||
PrepareStartPhase::Recurse(new)
|
||||
}
|
||||
None => match phase {
|
||||
Phase::LoadHeader => {
|
||||
// this is fine, mark header loading as pending
|
||||
self.status
|
||||
.insert(Job::Step(module_id, Phase::LoadHeader), Status::Pending);
|
||||
|
||||
PrepareStartPhase::Continue
|
||||
}
|
||||
_ => unreachable!(
|
||||
"Pair {:?} is not in dependencies.status, that should never happen!",
|
||||
(module_id, phase)
|
||||
),
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
/// Load the entire "make specializations" dependency graph and start from the top.
|
||||
pub fn reload_make_specialization_pass(&mut self) -> MutSet<(ModuleId, Phase)> {
|
||||
let mut output = MutSet::default();
|
||||
|
||||
let mut make_specializations_dependents = Default::default();
|
||||
std::mem::swap(
|
||||
&mut self.make_specializations_dependents,
|
||||
&mut make_specializations_dependents,
|
||||
);
|
||||
|
||||
for (&module, _) in make_specializations_dependents.iter() {
|
||||
let job = Job::Step(module, Phase::MakeSpecializations);
|
||||
let status = self.status.get_mut(&job).unwrap();
|
||||
debug_assert!(
|
||||
matches!(status, Status::Done),
|
||||
"all previous make specializations should be done before reloading"
|
||||
);
|
||||
*status = Status::NotStarted;
|
||||
}
|
||||
|
||||
// `add_dependency` borrows self as mut so we move `make_specializations_dependents` out
|
||||
// for our local use. `add_dependency` should never grow the make specializations
|
||||
// dependency graph.
|
||||
for (&module, (succ, has_pred)) in make_specializations_dependents.iter() {
|
||||
for &dependent in succ {
|
||||
self.add_dependency(dependent, module, Phase::MakeSpecializations);
|
||||
}
|
||||
|
||||
self.add_to_status(module, Phase::MakeSpecializations);
|
||||
if !has_pred {
|
||||
output.insert((module, Phase::MakeSpecializations));
|
||||
}
|
||||
}
|
||||
|
||||
std::mem::swap(
|
||||
&mut self.make_specializations_dependents,
|
||||
&mut make_specializations_dependents,
|
||||
);
|
||||
debug_assert!(
|
||||
make_specializations_dependents.is_empty(),
|
||||
"more modules were added to the graph"
|
||||
);
|
||||
|
||||
output
|
||||
}
|
||||
}
|
||||
|
||||
pub enum PrepareStartPhase {
|
||||
Continue,
|
||||
Done,
|
||||
Recurse(MutSet<(ModuleId, Phase)>),
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue