-
Notifications
You must be signed in to change notification settings - Fork 187
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
330 additions
and
138 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,148 @@ | ||
use fe_analyzer::context::Context as AnalyzerContext; | ||
use fe_analyzer::namespace::types::Array; | ||
use fe_parser::ast::{GenericArg, TypeDesc}; | ||
use indexmap::IndexMap; | ||
use std::hash::{Hash, Hasher}; | ||
|
||
#[derive(Default)] | ||
pub struct ModuleContext<'a> { | ||
analyzer_context: &'a AnalyzerContext, | ||
|
||
/// Tuples that were used inside of a module, | ||
/// and the generated name of the resulting struct. | ||
tuple_structs: IndexMap<TupleTypeDesc, String>, | ||
|
||
/// Holds fresh id for [`ModuleContext::make_unique_name`] | ||
fresh_id: u64, | ||
} | ||
|
||
impl ModuleContext<'_> { | ||
/// Makes a unique name from the given name, keeping it as readable as possible. | ||
pub fn make_unique_name(&mut self, name: &str) -> String { | ||
let id = self.fresh_id; | ||
self.fresh_id += 1; | ||
format!("${}_{}", name, id) | ||
} | ||
|
||
pub fn tuple_struct_name(&mut self, tuple: TypeDesc) -> &str { | ||
let entry = self.tuple_structs.entry(TupleTypeDesc(tuple)); | ||
let index = entry.index(); | ||
entry.or_insert_with(|| format!("__tuple_{}", index)) | ||
} | ||
|
||
pub fn iter_tuples(&self) -> impl Iterator<Item = (&TypeDesc, &str)> { | ||
self.tuple_structs | ||
.iter() | ||
.map(|(typ, name)| (&typ.0, name.as_str())) | ||
} | ||
} | ||
|
||
// This is contract context, but it's used all over so it has a short name. | ||
pub struct Context<'a, 'b> { | ||
pub module: &'a mut ModuleContext<'b>, | ||
/// List expressions that the contract uses | ||
array_literals: IndexMap<Array, String>, | ||
} | ||
|
||
impl Context<'_, '_> { | ||
pub fn array_literal_function(typ: Array) -> String { | ||
let name = list_expr_generator_fn_name(&typ); | ||
self.array_literals.insert(typ); | ||
name | ||
} | ||
} | ||
|
||
struct TupleTypeDesc(TypeDesc); | ||
|
||
impl PartialEq for TupleTypeDesc { | ||
fn eq(&self, other: &TupleTypeDesc) -> bool { | ||
types_equal(&self.0, &other.0) | ||
} | ||
} | ||
impl Eq for TupleTypeDesc {} | ||
|
||
impl Hash for TupleTypeDesc { | ||
fn hash<H: Hasher>(&self, state: &mut H) { | ||
hash_type_desc(&self.0, state) | ||
} | ||
} | ||
|
||
fn types_equal(left: &TypeDesc, right: &TypeDesc) -> bool { | ||
use TypeDesc::*; | ||
match (left, right) { | ||
(Unit, Unit) => true, | ||
(Base { base: lname }, Base { base: rname }) => lname == rname, | ||
( | ||
Array { | ||
typ: ltyp, | ||
dimension: ldim, | ||
}, | ||
Array { | ||
typ: rtyp, | ||
dimension: rdim, | ||
}, | ||
) => ldim == rdim && types_equal(<yp.kind, &rtyp.kind), | ||
(Tuple { items: left }, Tuple { items: right }) => { | ||
left.len() == right.len() | ||
&& left | ||
.iter() | ||
.zip(right.iter()) | ||
.all(|(lnode, rnode)| types_equal(&lnode.kind, &rnode.kind)) | ||
} | ||
( | ||
Generic { | ||
base: lbase, | ||
args: largs, | ||
}, | ||
Generic { | ||
base: rbase, | ||
args: rargs, | ||
}, | ||
) => { | ||
lbase.kind == rbase.kind | ||
&& largs.kind.len() == rargs.kind.len() | ||
&& largs | ||
.kind | ||
.iter() | ||
.zip(rargs.kind.iter()) | ||
.all(|(left, right)| match (left, right) { | ||
(GenericArg::Int(l), GenericArg::Int(r)) => l.kind == r.kind, | ||
(GenericArg::TypeDesc(l), GenericArg::TypeDesc(r)) => &l.kind == &r.kind, | ||
_ => false, | ||
}) | ||
} | ||
_ => false, | ||
} | ||
} | ||
|
||
fn hash_type_desc(typ: &TypeDesc, state: &mut impl Hasher) { | ||
use TypeDesc::*; | ||
match typ { | ||
Unit => state.write_u8(0), | ||
Base { base } => { | ||
state.write_u8(1); | ||
base.hash(state); | ||
} | ||
Array { typ, dimension } => { | ||
state.write_u8(2); | ||
hash_type_desc(&typ.kind, state); | ||
state.write_usize(*dimension); | ||
} | ||
Tuple { items } => { | ||
state.write_u8(3); | ||
for item in items { | ||
hash_type_desc(&item.kind, state); | ||
} | ||
} | ||
Generic { base, args } => { | ||
state.write_u8(4); | ||
base.kind.hash(state); | ||
for arg in &args.kind { | ||
match arg { | ||
GenericArg::Int(node) => state.write_usize(node.kind), | ||
GenericArg::TypeDesc(node) => hash_type_desc(&node.kind, state), | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.