|
| 1 | +use std::{cell::RefCell, collections::HashMap, path::PathBuf}; |
| 2 | + |
| 3 | +use itertools::Itertools; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + generators::{Generator, GeneratorOutput}, |
| 7 | + passes::Pass, |
| 8 | + rust_ast::{self, AstRef}, |
| 9 | + schema::{lower_ast_types, Schema, TypeDef}, |
| 10 | + Result, TypeId, |
| 11 | +}; |
| 12 | + |
| 13 | +#[derive(Default)] |
| 14 | +pub struct AstCodegen { |
| 15 | + files: Vec<PathBuf>, |
| 16 | + passes: Vec<Box<dyn Runner<Output = (), Context = EarlyCtx>>>, |
| 17 | + generators: Vec<Box<dyn Runner<Output = GeneratorOutput, Context = LateCtx>>>, |
| 18 | +} |
| 19 | + |
| 20 | +pub struct AstCodegenResult { |
| 21 | + pub schema: Schema, |
| 22 | + pub outputs: Vec<(/* generator name */ &'static str, /* output */ GeneratorOutput)>, |
| 23 | +} |
| 24 | + |
| 25 | +pub trait Runner { |
| 26 | + type Context; |
| 27 | + type Output; |
| 28 | + fn name(&self) -> &'static str; |
| 29 | + fn run(&mut self, ctx: &Self::Context) -> Result<Self::Output>; |
| 30 | +} |
| 31 | + |
| 32 | +pub struct EarlyCtx { |
| 33 | + ty_table: Vec<AstRef>, |
| 34 | + ident_table: HashMap<String, TypeId>, |
| 35 | + mods: RefCell<Vec<rust_ast::Module>>, |
| 36 | +} |
| 37 | + |
| 38 | +impl EarlyCtx { |
| 39 | + fn new(mods: Vec<rust_ast::Module>) -> Self { |
| 40 | + // worst case len |
| 41 | + let len = mods.iter().fold(0, |acc, it| acc + it.items.len()); |
| 42 | + let adts = mods.iter().flat_map(|it| it.items.iter()); |
| 43 | + |
| 44 | + let mut ty_table = Vec::with_capacity(len); |
| 45 | + let mut ident_table = HashMap::with_capacity(len); |
| 46 | + for adt in adts { |
| 47 | + if let Some(ident) = adt.borrow().ident() { |
| 48 | + let ident = ident.to_string(); |
| 49 | + let type_id = ty_table.len(); |
| 50 | + ty_table.push(AstRef::clone(adt)); |
| 51 | + ident_table.insert(ident, type_id); |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + Self { ty_table, ident_table, mods: RefCell::new(mods) } |
| 56 | + } |
| 57 | + |
| 58 | + pub fn chronological_idents(&self) -> impl Iterator<Item = &String> { |
| 59 | + self.ident_table.iter().sorted_by_key(|it| it.1).map(|it| it.0) |
| 60 | + } |
| 61 | + |
| 62 | + pub fn mods(&self) -> &RefCell<Vec<rust_ast::Module>> { |
| 63 | + &self.mods |
| 64 | + } |
| 65 | + |
| 66 | + pub fn find(&self, key: &String) -> Option<AstRef> { |
| 67 | + self.type_id(key).map(|id| AstRef::clone(&self.ty_table[id])) |
| 68 | + } |
| 69 | + |
| 70 | + pub fn type_id(&self, key: &String) -> Option<TypeId> { |
| 71 | + self.ident_table.get(key).copied() |
| 72 | + } |
| 73 | + |
| 74 | + pub fn ast_ref(&self, id: TypeId) -> AstRef { |
| 75 | + AstRef::clone(&self.ty_table[id]) |
| 76 | + } |
| 77 | + |
| 78 | + fn into_late_ctx(self) -> LateCtx { |
| 79 | + let schema = lower_ast_types(&self); |
| 80 | + |
| 81 | + LateCtx { schema } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +pub struct LateCtx { |
| 86 | + schema: Schema, |
| 87 | +} |
| 88 | + |
| 89 | +impl LateCtx { |
| 90 | + pub fn schema(&self) -> &Schema { |
| 91 | + &self.schema |
| 92 | + } |
| 93 | + |
| 94 | + pub fn type_def(&self, id: TypeId) -> Option<&TypeDef> { |
| 95 | + self.schema.get(id) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl AstCodegen { |
| 100 | + #[must_use] |
| 101 | + pub fn add_file<P>(mut self, path: P) -> Self |
| 102 | + where |
| 103 | + P: AsRef<str>, |
| 104 | + { |
| 105 | + self.files.push(path.as_ref().into()); |
| 106 | + self |
| 107 | + } |
| 108 | + |
| 109 | + #[must_use] |
| 110 | + pub fn pass<P>(mut self, pass: P) -> Self |
| 111 | + where |
| 112 | + P: Pass + Runner<Output = (), Context = EarlyCtx> + 'static, |
| 113 | + { |
| 114 | + self.passes.push(Box::new(pass)); |
| 115 | + self |
| 116 | + } |
| 117 | + |
| 118 | + #[must_use] |
| 119 | + pub fn gen<G>(mut self, generator: G) -> Self |
| 120 | + where |
| 121 | + G: Generator + Runner<Output = GeneratorOutput, Context = LateCtx> + 'static, |
| 122 | + { |
| 123 | + self.generators.push(Box::new(generator)); |
| 124 | + self |
| 125 | + } |
| 126 | + |
| 127 | + pub fn generate(self) -> Result<AstCodegenResult> { |
| 128 | + let modules = self |
| 129 | + .files |
| 130 | + .into_iter() |
| 131 | + .map(rust_ast::Module::from) |
| 132 | + .map(rust_ast::Module::load) |
| 133 | + .map_ok(rust_ast::Module::expand) |
| 134 | + .map_ok(|it| it.map(rust_ast::Module::analyze)) |
| 135 | + .collect::<Result<Result<Result<Vec<_>>>>>()???; |
| 136 | + |
| 137 | + // early passes |
| 138 | + let ctx = { |
| 139 | + let ctx = EarlyCtx::new(modules); |
| 140 | + _ = self |
| 141 | + .passes |
| 142 | + .into_iter() |
| 143 | + .map(|mut runner| runner.run(&ctx).map(|res| (runner.name(), res))) |
| 144 | + .collect::<Result<Vec<_>>>()?; |
| 145 | + ctx.into_late_ctx() |
| 146 | + }; |
| 147 | + |
| 148 | + let outputs = self |
| 149 | + .generators |
| 150 | + .into_iter() |
| 151 | + .map(|mut runner| runner.run(&ctx).map(|res| (runner.name(), res))) |
| 152 | + .collect::<Result<Vec<_>>>()?; |
| 153 | + |
| 154 | + Ok(AstCodegenResult { outputs, schema: ctx.schema }) |
| 155 | + } |
| 156 | +} |
0 commit comments