use std::collections::HashSet;
use std::path::{Path, PathBuf};
use crate::preprocess::condition::MacroValues;
use super::compose::{IncludeHandler, fold};
use super::{Env, Skeleton};
use crate::cache::summary::SummaryGuards;
#[derive(Debug, Clone)]
pub struct ComposedFile {
pub path: PathBuf,
pub at_entry: Env,
pub at_exit: Env,
pub depth: usize,
pub unknown_guards: usize,
}
#[derive(Debug, Clone, Default)]
pub struct UnitComposition {
pub files: Vec<ComposedFile>,
pub unknown_guards: usize,
pub depth_exceeded: usize,
pub unresolved_includes: usize,
pub unknown: Vec<(PathBuf, usize, String)>,
pub skipped_includes: Vec<(PathBuf, usize, String, &'static str, String)>,
}
impl UnitComposition {
pub fn file(&self, path: &Path) -> Option<&ComposedFile> {
self.files.iter().find(|file| file.path == path)
}
}
pub fn compose_unit(
root: &Path,
incoming: &Env,
seed: &dyn MacroValues,
look_up: &mut impl FnMut(&Path) -> Option<(Skeleton, SummaryGuards)>,
) -> UnitComposition {
let mut recorder = Recording {
seed,
look_up,
files: Vec::new(),
seen: HashSet::new(),
unknown_guards: 0,
depth_exceeded: 0,
unresolved_includes: 0,
unknown: Vec::new(),
skipped_includes: Vec::new(),
};
let Some((skeleton, guards)) = (recorder.look_up)(root) else {
return UnitComposition::default();
};
recorder.seen.insert(root.to_path_buf());
recorder.files.push(ComposedFile {
path: root.to_path_buf(),
at_entry: incoming.clone(),
at_exit: incoming.clone(),
depth: 0,
unknown_guards: 0,
});
let composed = fold(&skeleton, &guards, incoming, seed, &mut recorder, 0);
recorder.unknown_guards += composed.unknown_guards;
recorder.depth_exceeded += composed.depth_exceeded;
recorder.unresolved_includes += composed.unresolved_includes;
recorder.files[0].at_exit = composed.final_env;
recorder.files[0].unknown_guards = composed.unknown_guards;
for (offset, what) in composed.unknown {
if recorder.unknown.len() < super::compose::UNKNOWN_EXAMPLES {
recorder.unknown.push((root.to_path_buf(), offset, what));
}
}
for (offset, what, visibility, condition) in composed.skipped_includes {
if recorder.skipped_includes.len() < super::compose::UNKNOWN_EXAMPLES {
recorder.skipped_includes.push((
root.to_path_buf(),
offset,
what,
visibility,
condition,
));
}
}
UnitComposition {
files: recorder.files,
unknown_guards: recorder.unknown_guards,
depth_exceeded: recorder.depth_exceeded,
unresolved_includes: recorder.unresolved_includes,
unknown: recorder.unknown,
skipped_includes: recorder.skipped_includes,
}
}
struct Recording<'a> {
seed: &'a dyn MacroValues,
look_up: &'a mut dyn FnMut(&Path) -> Option<(Skeleton, SummaryGuards)>,
files: Vec<ComposedFile>,
seen: HashSet<PathBuf>,
unknown_guards: usize,
depth_exceeded: usize,
unresolved_includes: usize,
unknown: Vec<(PathBuf, usize, String)>,
skipped_includes: Vec<(PathBuf, usize, String, &'static str, String)>,
}
impl IncludeHandler for Recording<'_> {
fn include(&mut self, path: &Path, env: &Env, depth: usize) -> Option<Env> {
let (skeleton, guards) = (self.look_up)(path)?;
let first = self.seen.insert(path.to_path_buf());
let composed = fold(&skeleton, &guards, env, self.seed, self, depth + 1);
self.unknown_guards += composed.unknown_guards;
self.depth_exceeded += composed.depth_exceeded;
self.unresolved_includes += composed.unresolved_includes;
if first {
self.files.push(ComposedFile {
path: path.to_path_buf(),
at_entry: env.clone(),
at_exit: composed.final_env.clone(),
depth,
unknown_guards: composed.unknown_guards,
});
}
for (offset, what) in &composed.unknown {
if self.unknown.len() < super::compose::UNKNOWN_EXAMPLES {
self.unknown
.push((path.to_path_buf(), *offset, what.clone()));
}
}
Some(composed.final_env)
}
}