forked from M-Labs/nac3
1
0
Fork 0

added location -> call mapping

This allows code generation module to get function instantiation
parameter directly.
This commit is contained in:
pca006132 2021-07-30 11:01:11 +08:00
parent 832513e210
commit f2c5a9b352
2 changed files with 24 additions and 2 deletions

View File

@ -1,6 +1,6 @@
use std::cell::RefCell; use std::cell::RefCell;
use std::collections::HashMap; use std::collections::HashMap;
use std::convert::TryInto; use std::convert::{TryInto, From};
use std::iter::once; use std::iter::once;
use std::rc::Rc; use std::rc::Rc;
@ -17,6 +17,21 @@ use rustpython_parser::ast::{
#[cfg(test)] #[cfg(test)]
mod test; mod test;
#[derive(PartialEq, Eq, Hash, Copy, Clone, Debug)]
pub struct CodeLocation {
row: usize,
col: usize,
}
impl From<Location> for CodeLocation {
fn from(loc: Location) -> CodeLocation {
CodeLocation {
row: loc.row(),
col: loc.column()
}
}
}
pub struct PrimitiveStore { pub struct PrimitiveStore {
pub int32: Type, pub int32: Type,
pub int64: Type, pub int64: Type,
@ -37,6 +52,7 @@ pub struct Inferencer<'a> {
pub primitives: &'a PrimitiveStore, pub primitives: &'a PrimitiveStore,
pub virtual_checks: &'a mut Vec<(Type, Type)>, pub virtual_checks: &'a mut Vec<(Type, Type)>,
pub variable_mapping: HashMap<String, Type>, pub variable_mapping: HashMap<String, Type>,
pub calls: &'a mut HashMap<CodeLocation, Rc<Call>>,
} }
struct NaiveFolder(); struct NaiveFolder();
@ -215,6 +231,7 @@ impl<'a> Inferencer<'a> {
unifier: self.unifier, unifier: self.unifier,
primitives: self.primitives, primitives: self.primitives,
virtual_checks: self.virtual_checks, virtual_checks: self.virtual_checks,
calls: self.calls,
variable_mapping, variable_mapping,
}; };
let fun = FunSignature { let fun = FunSignature {
@ -257,6 +274,7 @@ impl<'a> Inferencer<'a> {
virtual_checks: self.virtual_checks, virtual_checks: self.virtual_checks,
variable_mapping, variable_mapping,
primitives: self.primitives, primitives: self.primitives,
calls: self.calls,
}; };
let elt = new_context.fold_expr(elt)?; let elt = new_context.fold_expr(elt)?;
let generator = generators.pop().unwrap(); let generator = generators.pop().unwrap();
@ -379,6 +397,7 @@ impl<'a> Inferencer<'a> {
fun: RefCell::new(None), fun: RefCell::new(None),
ret, ret,
}); });
self.calls.insert(location.into(), call.clone());
let call = self.unifier.add_ty(TypeEnum::TCall(vec![call].into())); let call = self.unifier.add_ty(TypeEnum::TCall(vec![call].into()));
self.unifier.unify(func.custom.unwrap(), call)?; self.unifier.unify(func.custom.unwrap(), call)?;

View File

@ -42,6 +42,7 @@ struct TestEnvironment {
pub id_to_name: HashMap<usize, String>, pub id_to_name: HashMap<usize, String>,
pub identifier_mapping: HashMap<String, Type>, pub identifier_mapping: HashMap<String, Type>,
pub virtual_checks: Vec<(Type, Type)>, pub virtual_checks: Vec<(Type, Type)>,
pub calls: HashMap<CodeLocation, Rc<Call>>,
} }
impl TestEnvironment { impl TestEnvironment {
@ -151,12 +152,13 @@ impl TestEnvironment {
function_data: FunctionData { function_data: FunctionData {
resolver, resolver,
bound_variables: Vec::new(), bound_variables: Vec::new(),
return_type: None return_type: None,
}, },
primitives, primitives,
id_to_name, id_to_name,
identifier_mapping, identifier_mapping,
virtual_checks: Vec::new(), virtual_checks: Vec::new(),
calls: HashMap::new(),
} }
} }
@ -167,6 +169,7 @@ impl TestEnvironment {
variable_mapping: Default::default(), variable_mapping: Default::default(),
primitives: &mut self.primitives, primitives: &mut self.primitives,
virtual_checks: &mut self.virtual_checks, virtual_checks: &mut self.virtual_checks,
calls: &mut self.calls,
} }
} }
} }