use associated const

This commit is contained in:
Folkert 2021-05-09 00:28:06 +02:00
parent fea8ab47cc
commit e7f59ea62d
2 changed files with 30 additions and 14 deletions

View file

@ -24,7 +24,7 @@ pub fn proc_spec(proc: &Proc) -> Result<FuncDef> {
// introduce the arguments // introduce the arguments
let mut argument_layouts = Vec::new(); let mut argument_layouts = Vec::new();
for (i, (layout, symbol)) in proc.args.iter().enumerate() { for (i, (layout, symbol)) in proc.args.iter().enumerate() {
let value_id = builder.add_get_tuple_field(block, morphic_lib::ARG_VALUE_ID, i as u32)?; let value_id = builder.add_get_tuple_field(block, ValueId::ARGUMENT, i as u32)?;
env.symbols.insert(*symbol, value_id); env.symbols.insert(*symbol, value_id);
argument_layouts.push(*layout); argument_layouts.push(*layout);

View file

@ -91,10 +91,12 @@ pub struct TypeNameBuf(Vec<u8>);
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] #[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct ValueId(pub u32); pub struct ValueId(pub u32);
/// Value id 0 always refers to the argument to the current function. impl ValueId {
/// The `ValueId` that refers to the argument to the current function.
/// ///
/// Every function takes exactly one argument. Multiple arguments can be represented via tuples. /// Every function takes exactly one argument. Multiple arguments can be represented via tuples.
pub const ARG_VALUE_ID: ValueId = ValueId(0); pub const ARGUMENT: Self = ValueId(0);
}
/// A reference to an arena-allocated join point in a function body. /// A reference to an arena-allocated join point in a function body.
/// ///
@ -171,12 +173,16 @@ pub trait TypeContext {
fn add_bag_type(&mut self, item_type: TypeId) -> Result<TypeId>; fn add_bag_type(&mut self, item_type: TypeId) -> Result<TypeId>;
} }
#[derive(Debug, Default)] #[derive(Debug)]
struct SeqGen { struct SeqGen {
next: u32, next: u32,
} }
impl SeqGen { impl SeqGen {
fn new() -> Self {
Self { next: 0 }
}
fn next(&mut self) -> u32 { fn next(&mut self) -> u32 {
let result = self.next; let result = self.next;
self.next += 1; self.next += 1;
@ -208,7 +214,7 @@ impl Default for TypeDefBuilder {
impl TypeDefBuilder { impl TypeDefBuilder {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
tid_gen: SeqGen::default(), tid_gen: SeqGen::new(),
} }
} }
@ -304,16 +310,16 @@ impl Default for FuncDefBuilder {
impl FuncDefBuilder { impl FuncDefBuilder {
pub fn new() -> Self { pub fn new() -> Self {
let mut result = Self { let mut result = Self {
tid_gen: SeqGen::default(), tid_gen: SeqGen::new(),
bid_gen: SeqGen::default(), bid_gen: SeqGen::new(),
vid_gen: SeqGen::default(), vid_gen: SeqGen::new(),
jid_gen: SeqGen::default(), jid_gen: SeqGen::new(),
blocks: BTreeMap::new(), blocks: BTreeMap::new(),
join_points: BTreeMap::new(), join_points: BTreeMap::new(),
callee_spec_vars: BTreeMap::new(), callee_spec_vars: BTreeMap::new(),
update_mode_vars: BTreeSet::new(), update_mode_vars: BTreeSet::new(),
}; };
// Generate `ARG_VALUE_ID` // Generate `ValueId::ARGUMENT`
result.vid_gen.next(); result.vid_gen.next();
result result
} }
@ -799,12 +805,17 @@ pub struct ModDef {
func_defs: BTreeMap<FuncNameBuf, FuncDef>, func_defs: BTreeMap<FuncNameBuf, FuncDef>,
} }
#[derive(Default)]
pub struct ModDefBuilder { pub struct ModDefBuilder {
type_names: BTreeSet<TypeNameBuf>, type_names: BTreeSet<TypeNameBuf>,
func_defs: BTreeMap<FuncNameBuf, FuncDef>, func_defs: BTreeMap<FuncNameBuf, FuncDef>,
} }
impl Default for ModDefBuilder {
fn default() -> Self {
Self::new()
}
}
impl ModDefBuilder { impl ModDefBuilder {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {
@ -842,12 +853,17 @@ pub struct Program {
entry_points: BTreeMap<EntryPointNameBuf, (ModNameBuf, FuncNameBuf)>, entry_points: BTreeMap<EntryPointNameBuf, (ModNameBuf, FuncNameBuf)>,
} }
#[derive(Default)]
pub struct ProgramBuilder { pub struct ProgramBuilder {
mods: BTreeMap<ModNameBuf, ModDef>, mods: BTreeMap<ModNameBuf, ModDef>,
entry_points: BTreeMap<EntryPointNameBuf, (ModNameBuf, FuncNameBuf)>, entry_points: BTreeMap<EntryPointNameBuf, (ModNameBuf, FuncNameBuf)>,
} }
impl Default for ProgramBuilder {
fn default() -> Self {
Self::new()
}
}
impl ProgramBuilder { impl ProgramBuilder {
pub fn new() -> Self { pub fn new() -> Self {
Self { Self {