optimization (#13) #15
@ -14,7 +14,7 @@ use inkwell::{
|
||||
use itertools::{chain, izip, zip, Itertools};
|
||||
use rustpython_parser::ast::{self, Boolop, Constant, Expr, ExprKind, Operator, StrRef};
|
||||
|
||||
pub fn assert_int_val<'ctx>(val: BasicValueEnum<'ctx>) -> IntValue<'ctx> {
|
||||
pub fn assert_int_val(val: BasicValueEnum<'_>) -> IntValue<'_> {
|
||||
if let BasicValueEnum::IntValue(v) = val {
|
||||
v
|
||||
} else {
|
||||
@ -22,7 +22,7 @@ pub fn assert_int_val<'ctx>(val: BasicValueEnum<'ctx>) -> IntValue<'ctx> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn assert_pointer_val<'ctx>(val: BasicValueEnum<'ctx>) -> PointerValue<'ctx> {
|
||||
pub fn assert_pointer_val(val: BasicValueEnum<'_>) -> PointerValue<'_> {
|
||||
if let BasicValueEnum::PointerValue(v) = val {
|
||||
v
|
||||
} else {
|
||||
@ -189,7 +189,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||
.args
|
||||
.iter()
|
||||
.map(|arg| FuncArg {
|
||||
name: arg.name.clone(),
|
||||
name: arg.name,
|
||||
ty: unifier.copy_from(&mut self.unifier, arg.ty, &mut type_cache),
|
||||
default_value: arg.default_value.clone(),
|
||||
})
|
||||
@ -645,7 +645,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||
args.iter().map(|arg| (None, self.gen_expr(arg).unwrap())).collect_vec();
|
||||
let kw_iter = keywords.iter().map(|kw| {
|
||||
(
|
||||
Some(kw.node.arg.as_ref().unwrap().clone()),
|
||||
Some(*kw.node.arg.as_ref().unwrap()),
|
||||
self.gen_expr(&kw.node.value).unwrap(),
|
||||
)
|
||||
});
|
||||
|
@ -320,7 +320,7 @@ pub fn gen_func<'ctx>(
|
||||
&arg.name.to_string(),
|
||||
);
|
||||
builder.build_store(alloca, param);
|
||||
var_assignment.insert(arg.name.clone(), alloca);
|
||||
var_assignment.insert(arg.name, alloca);
|
||||
}
|
||||
builder.build_unconditional_branch(body_bb);
|
||||
builder.position_at_end(body_bb);
|
||||
|
@ -25,7 +25,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
|
||||
ExprKind::Name { id, .. } => {
|
||||
self.var_assignment.get(id).cloned().unwrap_or_else(|| {
|
||||
let ptr = self.gen_var(pattern.custom.unwrap());
|
||||
self.var_assignment.insert(id.clone(), ptr);
|
||||
self.var_assignment.insert(*id, ptr);
|
||||
ptr
|
||||
})
|
||||
}
|
||||
|
@ -10,15 +10,12 @@ pub enum Location {
|
||||
Builtin,
|
||||
}
|
||||
|
||||
#[derive(Default)]
|
||||
pub struct FileRegistry {
|
||||
files: Vec<String>,
|
||||
}
|
||||
|
||||
impl FileRegistry {
|
||||
pub fn new() -> FileRegistry {
|
||||
FileRegistry { files: Vec::new() }
|
||||
}
|
||||
|
||||
pub fn add_file(&mut self, path: &str) -> FileID {
|
||||
let index = self.files.len() as u32;
|
||||
self.files.push(path.to_owned());
|
||||
|
@ -89,8 +89,8 @@ impl TopLevelComposer {
|
||||
|
||||
for (name, sig) in builtins {
|
||||
let fun_sig = unifier.add_ty(TypeEnum::TFunc(RefCell::new(sig)));
|
||||
built_in_ty.insert(name.clone(), fun_sig);
|
||||
built_in_id.insert(name.clone(), DefinitionId(definition_ast_list.len()));
|
||||
built_in_ty.insert(name, fun_sig);
|
||||
built_in_id.insert(name, DefinitionId(definition_ast_list.len()));
|
||||
definition_ast_list.push((
|
||||
Arc::new(RwLock::new(TopLevelDef::Function {
|
||||
name: name.into(),
|
||||
@ -161,7 +161,7 @@ impl TopLevelComposer {
|
||||
return Err("duplicate definition of class".into());
|
||||
}
|
||||
|
||||
let class_name = class_name.clone();
|
||||
let class_name = *class_name;
|
||||
let class_def_id = self.definition_ast_list.len();
|
||||
|
||||
// since later when registering class method, ast will still be used,
|
||||
@ -221,10 +221,10 @@ impl TopLevelComposer {
|
||||
// dummy method define here
|
||||
let dummy_method_type = self.unifier.get_fresh_var();
|
||||
class_method_name_def_ids.push((
|
||||
method_name.clone(),
|
||||
*method_name,
|
||||
RwLock::new(Self::make_top_level_function_def(
|
||||
global_class_method_name,
|
||||
method_name.clone(),
|
||||
*method_name,
|
||||
// later unify with parsed type
|
||||
dummy_method_type.0,
|
||||
resolver.clone(),
|
||||
@ -246,7 +246,7 @@ impl TopLevelComposer {
|
||||
for (name, _, id, ty, ..) in &class_method_name_def_ids {
|
||||
let mut class_def = class_def_ast.0.write();
|
||||
if let TopLevelDef::Class { methods, .. } = class_def.deref_mut() {
|
||||
methods.push((name.clone(), *ty, *id));
|
||||
methods.push((*name, *ty, *id));
|
||||
self.method_class.insert(*id, DefinitionId(class_def_id));
|
||||
} else {
|
||||
unreachable!()
|
||||
@ -705,7 +705,7 @@ impl TopLevelComposer {
|
||||
)?;
|
||||
|
||||
Ok(FuncArg {
|
||||
name: x.node.arg.clone(),
|
||||
name: x.node.arg,
|
||||
ty,
|
||||
default_value: Default::default(),
|
||||
})
|
||||
@ -809,7 +809,7 @@ impl TopLevelComposer {
|
||||
if let ast::StmtKind::ClassDef { name, bases, body, .. } = &class_ast {
|
||||
(
|
||||
*object_id,
|
||||
name.clone(),
|
||||
*name,
|
||||
bases,
|
||||
body,
|
||||
ancestors,
|
||||
@ -851,7 +851,7 @@ impl TopLevelComposer {
|
||||
let mut defined_paramter_name: HashSet<_> = HashSet::new();
|
||||
let zelf: StrRef = "self".into();
|
||||
let have_unique_fuction_parameter_name = args.args.iter().all(|x| {
|
||||
defined_paramter_name.insert(x.node.arg.clone())
|
||||
defined_paramter_name.insert(x.node.arg)
|
||||
&& (!keyword_list.contains(&x.node.arg) || x.node.arg == zelf)
|
||||
});
|
||||
if !have_unique_fuction_parameter_name {
|
||||
@ -1310,7 +1310,7 @@ impl TopLevelComposer {
|
||||
let unifier = &mut self.unifier;
|
||||
args.iter()
|
||||
.map(|a| FuncArg {
|
||||
name: a.name.clone(),
|
||||
name: a.name,
|
||||
ty: unifier.subst(a.ty, &subst).unwrap_or(a.ty),
|
||||
default_value: a.default_value.clone(),
|
||||
})
|
||||
@ -1328,7 +1328,7 @@ impl TopLevelComposer {
|
||||
if self_type.is_some() {
|
||||
result.insert("self".into());
|
||||
}
|
||||
result.extend(inst_args.iter().map(|x| x.name.clone()));
|
||||
result.extend(inst_args.iter().map(|x| x.name));
|
||||
result
|
||||
};
|
||||
let mut calls: HashMap<CodeLocation, CallId> = HashMap::new();
|
||||
@ -1356,7 +1356,7 @@ impl TopLevelComposer {
|
||||
if let Some(self_ty) = self_type {
|
||||
result.insert("self".into(), self_ty);
|
||||
}
|
||||
result.extend(inst_args.iter().map(|x| (x.name.clone(), x.ty)));
|
||||
result.extend(inst_args.iter().map(|x| (x.name, x.ty)));
|
||||
result
|
||||
},
|
||||
primitives: &self.primitives_ty,
|
||||
|
@ -296,7 +296,7 @@ impl TopLevelComposer {
|
||||
if let ast::ExprKind::Attribute { value, attr, .. } = &t.node {
|
||||
if let ast::ExprKind::Name { id, .. } = &value.node {
|
||||
if id == &"self".into() {
|
||||
result.insert(attr.clone());
|
||||
result.insert(*attr);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -14,7 +14,7 @@ impl<'a> Inferencer<'a> {
|
||||
match &pattern.node {
|
||||
ExprKind::Name { id, .. } => {
|
||||
if !defined_identifiers.contains(id) {
|
||||
defined_identifiers.insert(id.clone());
|
||||
defined_identifiers.insert(*id);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -58,7 +58,7 @@ impl<'a> Inferencer<'a> {
|
||||
ExprKind::Name { id, .. } => {
|
||||
if !defined_identifiers.contains(id) {
|
||||
if self.function_data.resolver.get_identifier_def(*id).is_some() {
|
||||
defined_identifiers.insert(id.clone());
|
||||
defined_identifiers.insert(*id);
|
||||
} else {
|
||||
return Err(format!(
|
||||
"unknown identifier {} (use before def?) at {}",
|
||||
@ -107,7 +107,7 @@ impl<'a> Inferencer<'a> {
|
||||
let mut defined_identifiers = defined_identifiers.clone();
|
||||
for arg in args.args.iter() {
|
||||
if !defined_identifiers.contains(&arg.node.arg) {
|
||||
defined_identifiers.insert(arg.node.arg.clone());
|
||||
defined_identifiers.insert(arg.node.arg);
|
||||
}
|
||||
}
|
||||
self.check_expr(body, &mut defined_identifiers)?;
|
||||
@ -167,7 +167,7 @@ impl<'a> Inferencer<'a> {
|
||||
|
||||
for ident in body_identifiers.iter() {
|
||||
if !defined_identifiers.contains(ident) && orelse_identifiers.contains(ident) {
|
||||
defined_identifiers.insert(ident.clone());
|
||||
defined_identifiers.insert(*ident);
|
||||
}
|
||||
}
|
||||
Ok(body_returned && orelse_returned)
|
||||
|
@ -164,7 +164,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
|
||||
ast::ExprKind::Name { id, .. } => {
|
||||
if !self.defined_identifiers.contains(id) {
|
||||
if self.function_data.resolver.get_identifier_def(*id).is_some() {
|
||||
self.defined_identifiers.insert(id.clone());
|
||||
self.defined_identifiers.insert(*id);
|
||||
} else {
|
||||
return Err(format!(
|
||||
"unknown identifier {} (use before def?) at {}",
|
||||
@ -220,7 +220,7 @@ impl<'a> Inferencer<'a> {
|
||||
match &pattern.node {
|
||||
ExprKind::Name { id, .. } => {
|
||||
if !self.defined_identifiers.contains(id) {
|
||||
self.defined_identifiers.insert(id.clone());
|
||||
self.defined_identifiers.insert(*id);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
@ -312,13 +312,13 @@ impl<'a> Inferencer<'a> {
|
||||
for arg in args.args.iter() {
|
||||
let name = &arg.node.arg;
|
||||
if !defined_identifiers.contains(name) {
|
||||
defined_identifiers.insert(name.clone());
|
||||
defined_identifiers.insert(*name);
|
||||
}
|
||||
}
|
||||
let fn_args: Vec<_> = args
|
||||
.args
|
||||
.iter()
|
||||
.map(|v| (v.node.arg.clone(), self.unifier.get_fresh_var().0))
|
||||
.map(|v| (v.node.arg, self.unifier.get_fresh_var().0))
|
||||
.collect();
|
||||
let mut variable_mapping = self.variable_mapping.clone();
|
||||
variable_mapping.extend(fn_args.iter().cloned());
|
||||
@ -337,7 +337,7 @@ impl<'a> Inferencer<'a> {
|
||||
let fun = FunSignature {
|
||||
args: fn_args
|
||||
.iter()
|
||||
.map(|(k, ty)| FuncArg { name: k.clone(), ty: *ty, default_value: None })
|
||||
.map(|(k, ty)| FuncArg { name: *k, ty: *ty, default_value: None })
|
||||
.collect(),
|
||||
ret,
|
||||
vars: Default::default(),
|
||||
@ -503,7 +503,7 @@ impl<'a> Inferencer<'a> {
|
||||
posargs: args.iter().map(|v| v.custom.unwrap()).collect(),
|
||||
kwargs: keywords
|
||||
.iter()
|
||||
.map(|v| (v.node.arg.as_ref().unwrap().clone(), v.custom.unwrap()))
|
||||
.map(|v| (*v.node.arg.as_ref().unwrap(), v.custom.unwrap()))
|
||||
.collect(),
|
||||
fun: RefCell::new(None),
|
||||
ret: sign.ret,
|
||||
@ -531,7 +531,7 @@ impl<'a> Inferencer<'a> {
|
||||
posargs: args.iter().map(|v| v.custom.unwrap()).collect(),
|
||||
kwargs: keywords
|
||||
.iter()
|
||||
.map(|v| (v.node.arg.as_ref().unwrap().clone(), v.custom.unwrap()))
|
||||
.map(|v| (*v.node.arg.as_ref().unwrap(), v.custom.unwrap()))
|
||||
.collect(),
|
||||
fun: RefCell::new(None),
|
||||
ret,
|
||||
|
@ -107,6 +107,12 @@ pub struct Unifier {
|
||||
var_id: u32,
|
||||
}
|
||||
|
||||
impl Default for Unifier {
|
||||
fn default() -> Self {
|
||||
Unifier::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl Unifier {
|
||||
/// Get an empty unifier
|
||||
pub fn new() -> Unifier {
|
||||
@ -454,7 +460,7 @@ impl Unifier {
|
||||
if let Some(ty) = fields2.get(key) {
|
||||
self.unify(*ty, *value)?;
|
||||
} else {
|
||||
fields2.insert(key.clone(), *value);
|
||||
fields2.insert(*key, *value);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -12,6 +12,12 @@ pub struct UnificationTable<V> {
|
||||
values: Vec<Option<V>>,
|
||||
}
|
||||
|
||||
impl<V> Default for UnificationTable<V> {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
impl<V> UnificationTable<V> {
|
||||
pub fn new() -> UnificationTable<V> {
|
||||
UnificationTable { parents: Vec::new(), ranks: Vec::new(), values: Vec::new() }
|
||||
|
@ -78,7 +78,7 @@ fn main() {
|
||||
"__main__".into(),
|
||||
).unwrap();
|
||||
|
||||
internal_resolver.add_id_def(name.clone(), def_id);
|
||||
internal_resolver.add_id_def(name, def_id);
|
||||
if let Some(ty) = ty {
|
||||
internal_resolver.add_id_type(name, ty);
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user