fixed some clippy warnings

This commit is contained in:
pca006132 2021-10-16 18:08:13 +08:00
parent 26076c37ba
commit c4259d14d1
13 changed files with 94 additions and 68 deletions

View File

@ -94,7 +94,7 @@ impl Nac3 {
})?;
let parser_result = parser::parse_program(&source)
.map_err(|e| exceptions::PySyntaxError::new_err(format!("parse error: {}", e)))?;
let resolver = Arc::new(Box::new(Resolver {
let resolver = Arc::new(Resolver {
id_to_type: self.builtins_ty.clone().into(),
id_to_def: self.builtins_def.clone().into(),
pyid_to_def: self.pyid_to_def.clone(),
@ -104,7 +104,7 @@ impl Nac3 {
class_names: Default::default(),
name_to_pyid: name_to_pyid.clone(),
module: obj,
}) as Box<dyn SymbolResolver + Send + Sync>);
}) as Arc<dyn SymbolResolver + Send + Sync>;
let mut name_to_def = HashMap::new();
let mut name_to_type = HashMap::new();
@ -334,7 +334,7 @@ impl Nac3 {
)
};
let mut synthesized = parse_program(&synthesized).unwrap();
let resolver = Arc::new(Box::new(Resolver {
let resolver = Arc::new(Resolver {
id_to_type: self.builtins_ty.clone().into(),
id_to_def: self.builtins_def.clone().into(),
pyid_to_def: self.pyid_to_def.clone(),
@ -344,7 +344,7 @@ impl Nac3 {
class_names: Default::default(),
name_to_pyid,
module: module.to_object(py),
}) as Box<dyn SymbolResolver + Send + Sync>);
}) as Arc<dyn SymbolResolver + Send + Sync>;
let (_, def_id, _) = self
.composer
.register_top_level(

View File

@ -159,7 +159,7 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
let instance = instance_to_stmt.get(&key).unwrap();
let unifiers = self.top_level.unifiers.read();
let (unifier, primitives) = &unifiers[instance.unifier_id];
let mut unifier = Unifier::from_shared_unifier(&unifier);
let mut unifier = Unifier::from_shared_unifier(unifier);
let mut type_cache = [
(self.primitives.int32, primitives.int32),

View File

@ -38,7 +38,7 @@ pub struct CodeGenContext<'ctx, 'a> {
pub module: Module<'ctx>,
pub top_level: &'a TopLevelContext,
pub unifier: Unifier,
pub resolver: Arc<Box<dyn SymbolResolver + Send + Sync>>,
pub resolver: Arc<dyn SymbolResolver + Send + Sync>,
pub var_assignment: HashMap<StrRef, PointerValue<'ctx>>,
pub type_cache: HashMap<Type, BasicTypeEnum<'ctx>>,
pub primitives: PrimitiveStore,
@ -190,7 +190,7 @@ pub struct CodeGenTask {
pub body: Arc<Vec<Stmt<Option<Type>>>>,
pub calls: Arc<HashMap<CodeLocation, CallId>>,
pub unifier: (SharedUnifier, PrimitiveStore),
pub resolver: Arc<Box<dyn SymbolResolver + Send + Sync>>,
pub resolver: Arc<dyn SymbolResolver + Send + Sync>,
}
fn get_llvm_type<'ctx>(
@ -287,7 +287,7 @@ pub fn gen_func<'ctx>(
.args
.iter()
.map(|arg| {
get_llvm_type(&context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty)
get_llvm_type(context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty)
})
.collect_vec();
@ -295,7 +295,7 @@ pub fn gen_func<'ctx>(
context.void_type().fn_type(&params, false)
} else {
get_llvm_type(
&context,
context,
&mut unifier,
top_level_ctx.as_ref(),
&mut type_cache,
@ -309,9 +309,9 @@ pub fn gen_func<'ctx>(
module.get_function(symbol).unwrap_or_else(|| module.add_function(symbol, fn_type, None));
if let Some(personality) = &top_level_ctx.personality_symbol {
let personality = module.get_function(&personality).unwrap_or_else(|| {
let personality = module.get_function(personality).unwrap_or_else(|| {
let ty = context.i32_type().fn_type(&[], true);
module.add_function(&personality, ty, None)
module.add_function(personality, ty, None)
});
fn_val.set_personality_function(personality);
}
@ -324,7 +324,7 @@ pub fn gen_func<'ctx>(
for (n, arg) in task.signature.args.iter().enumerate() {
let param = fn_val.get_nth_param(n as u32).unwrap();
let alloca = builder.build_alloca(
get_llvm_type(&context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty),
get_llvm_type(context, &mut unifier, top_level_ctx.as_ref(), &mut type_cache, arg.ty),
&arg.name.to_string(),
);
builder.build_store(alloca, param);
@ -334,7 +334,7 @@ pub fn gen_func<'ctx>(
builder.position_at_end(body_bb);
let mut code_gen_context = CodeGenContext {
ctx: &context,
ctx: context,
resolver: task.resolver,
top_level: top_level_ctx.as_ref(),
calls: task.calls,

View File

@ -96,22 +96,22 @@ impl<'ctx, 'a> CodeGenContext<'ctx, 'a> {
match &stmt.node {
StmtKind::Pass => {},
StmtKind::Expr { value } => {
self.gen_expr(&value);
self.gen_expr(value);
}
StmtKind::Return { value } => {
let value = value.as_ref().map(|v| self.gen_expr(&v).unwrap());
let value = value.as_ref().map(|v| self.gen_expr(v).unwrap());
let value = value.as_ref().map(|v| v as &dyn BasicValue);
self.builder.build_return(value);
return true;
}
StmtKind::AnnAssign { target, value, .. } => {
if let Some(value) = value {
let value = self.gen_expr(&value).unwrap();
let value = self.gen_expr(value).unwrap();
self.gen_assignment(target, value);
}
}
StmtKind::Assign { targets, value, .. } => {
let value = self.gen_expr(&value).unwrap();
let value = self.gen_expr(value).unwrap();
for target in targets.iter() {
self.gen_assignment(target, value);
}

View File

@ -66,11 +66,11 @@ fn test_primitives() {
let top_level = Arc::new(composer.make_top_level_context());
unifier.top_level = Some(top_level.clone());
let resolver = Arc::new(Box::new(Resolver {
let resolver = Arc::new(Resolver {
id_to_type: HashMap::new(),
id_to_def: RwLock::new(HashMap::new()),
class_names: Default::default(),
}) as Box<dyn SymbolResolver + Send + Sync>);
}) as Arc<dyn SymbolResolver + Send + Sync>;
let threads = ["test"];
let signature = FunSignature {
@ -229,13 +229,13 @@ fn test_simple_call() {
codegen_callback: None,
})));
let resolver = Box::new(Resolver {
let resolver = Resolver {
id_to_type: HashMap::new(),
id_to_def: RwLock::new(HashMap::new()),
class_names: Default::default(),
});
};
resolver.add_id_def("foo".into(), DefinitionId(foo_id));
let resolver = Arc::new(resolver as Box<dyn SymbolResolver + Send + Sync>);
let resolver = Arc::new(resolver) as Arc<dyn SymbolResolver + Send + Sync>;
if let TopLevelDef::Function { resolver: r, .. } =
&mut *top_level.definitions.read()[foo_id].write()

View File

@ -387,7 +387,7 @@ impl TopLevelComposer {
pub fn register_top_level(
&mut self,
ast: ast::Stmt<()>,
resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>,
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
mod_path: String,
) -> Result<(StrRef, DefinitionId, Option<Type>), String> {
let defined_names = &mut self.defined_names;
@ -720,7 +720,7 @@ impl TopLevelComposer {
// the function parse_ast_to make sure that no type var occured in
// bast_ty if it is a CustomClassKind
let base_ty = parse_ast_to_type_annotation_kinds(
class_resolver.as_ref(),
class_resolver,
&temp_def_list,
unifier,
&self.primitives_ty,
@ -926,7 +926,7 @@ impl TopLevelComposer {
.as_ref();
let type_annotation = parse_ast_to_type_annotation_kinds(
resolver.as_ref(),
resolver,
temp_def_list.as_slice(),
unifier,
primitives_store,
@ -975,7 +975,7 @@ impl TopLevelComposer {
let return_ty_annotation = {
let return_annotation = returns.as_ref();
parse_ast_to_type_annotation_kinds(
resolver.as_ref(),
resolver,
&temp_def_list,
unifier,
primitives_store,
@ -1132,7 +1132,7 @@ impl TopLevelComposer {
})?
.as_ref();
parse_ast_to_type_annotation_kinds(
class_resolver.as_ref(),
class_resolver,
temp_def_list,
unifier,
primitives,
@ -1178,7 +1178,7 @@ impl TopLevelComposer {
if let Some(result) = returns {
let result = result.as_ref();
let annotation = parse_ast_to_type_annotation_kinds(
class_resolver.as_ref(),
class_resolver,
temp_def_list,
unifier,
primitives,
@ -1253,8 +1253,8 @@ impl TopLevelComposer {
};
let annotation = parse_ast_to_type_annotation_kinds(
class_resolver.as_ref(),
&temp_def_list,
class_resolver,
temp_def_list,
unifier,
primitives,
annotation.as_ref(),

View File

@ -91,7 +91,7 @@ impl TopLevelComposer {
/// when first regitering, the type_vars, fields, methods, ancestors are invalid
pub fn make_top_level_class_def(
index: usize,
resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>,
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
name: StrRef,
constructor: Option<Type>,
) -> TopLevelDef {
@ -112,7 +112,7 @@ impl TopLevelComposer {
name: String,
simple_name: StrRef,
ty: Type,
resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>,
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
) -> TopLevelDef {
TopLevelDef::Function {
name,

View File

@ -91,7 +91,7 @@ pub enum TopLevelDef {
// ancestor classes, including itself.
ancestors: Vec<TypeAnnotation>,
// symbol resolver of the module defined the class, none if it is built-in type
resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>,
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
// constructor type
constructor: Option<Type>,
},
@ -116,7 +116,7 @@ pub enum TopLevelDef {
/// rigid type variables that would be substituted when the function is instantiated.
instance_to_stmt: HashMap<String, FunInstance>,
// symbol resolver of the module defined the class
resolver: Option<Arc<Box<dyn SymbolResolver + Send + Sync>>>,
resolver: Option<Arc<dyn SymbolResolver + Send + Sync>>,
// custom codegen callback
codegen_callback: Option<Arc<GenCall>>
},

View File

@ -35,7 +35,13 @@ impl ResolverInternal {
struct Resolver(Arc<ResolverInternal>);
impl SymbolResolver for Resolver {
fn get_symbol_type(&self, _: &mut Unifier, _: &[Arc<RwLock<TopLevelDef>>], _: &PrimitiveStore, str: StrRef) -> Option<Type> {
fn get_symbol_type(
&self,
_: &mut Unifier,
_: &[Arc<RwLock<TopLevelDef>>],
_: &PrimitiveStore,
str: StrRef,
) -> Option<Type> {
let ret = self.0.id_to_type.lock().get(&str).cloned();
if ret.is_none() {
// println!("unknown here resolver {}", str);
@ -138,9 +144,8 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
id_to_type: Default::default(),
class_names: Default::default(),
});
let resolver = Arc::new(
Box::new(Resolver(internal_resolver.clone())) as Box<dyn SymbolResolver + Send + Sync>
);
let resolver =
Arc::new(Resolver(internal_resolver.clone())) as Arc<dyn SymbolResolver + Send + Sync>;
for s in source {
let ast = parse_program(s).unwrap();
@ -148,7 +153,7 @@ fn test_simple_function_analyze(source: Vec<&str>, tys: Vec<&str>, names: Vec<&s
let (id, def_id, ty) =
composer.register_top_level(ast, Some(resolver.clone()), "".into()).unwrap();
internal_resolver.add_id_def(id.clone(), def_id);
internal_resolver.add_id_def(id, def_id);
if let Some(ty) = ty {
internal_resolver.add_id_type(id, ty);
}
@ -489,9 +494,8 @@ fn test_analyze(source: Vec<&str>, res: Vec<&str>) {
&mut composer.unifier,
print,
);
let resolver = Arc::new(
Box::new(Resolver(internal_resolver.clone())) as Box<dyn SymbolResolver + Send + Sync>
);
let resolver =
Arc::new(Resolver(internal_resolver.clone())) as Arc<dyn SymbolResolver + Send + Sync>;
for s in source {
let ast = parse_program(s).unwrap();
@ -685,8 +689,8 @@ fn test_inference(source: Vec<&str>, res: Vec<&str>) {
print,
);
let resolver = Arc::new(
Box::new(Resolver(internal_resolver.clone())) as Box<dyn SymbolResolver + Send + Sync>
);
Resolver(internal_resolver.clone())
) as Arc<dyn SymbolResolver + Send + Sync>;
for s in source {
let ast = parse_program(s).unwrap();
@ -705,7 +709,7 @@ fn test_inference(source: Vec<&str>, res: Vec<&str>) {
}
}
};
internal_resolver.add_id_def(id.clone(), def_id);
internal_resolver.add_id_def(id, def_id);
if let Some(ty) = ty {
internal_resolver.add_id_type(id, ty);
}
@ -754,7 +758,7 @@ fn make_internal_resolver_with_tvar(
id_to_type: tvars
.into_iter()
.map(|(name, range)| {
(name.clone(), {
(name, {
let (ty, id) = unifier.get_fresh_var_with_range(range.as_slice());
if print {
println!("{}: {:?}, tvar{}", name, ty, id);

View File

@ -38,7 +38,7 @@ pub struct PrimitiveStore {
}
pub struct FunctionData {
pub resolver: Arc<Box<dyn SymbolResolver + Send + Sync>>,
pub resolver: Arc<dyn SymbolResolver + Send + Sync>,
pub return_type: Option<Type>,
pub bound_variables: Vec<Type>,
}
@ -91,7 +91,7 @@ impl<'a> fold::Fold<()> for Inferencer<'a> {
let annotation_type = self.function_data.resolver.parse_type_annotation(
top_level_defs.as_slice(),
self.unifier,
&self.primitives,
self.primitives,
annotation.as_ref(),
)?;
self.unify(annotation_type, target.custom.unwrap(), &node.location)?;

View File

@ -67,11 +67,14 @@ impl TestEnvironment {
params: HashMap::new().into(),
});
if let TypeEnum::TObj { fields, .. } = &*unifier.get_ty(int32) {
let add_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
let add_ty = unifier.add_ty(TypeEnum::TFunc(
FunSignature {
args: vec![FuncArg { name: "other".into(), ty: int32, default_value: None }],
ret: int32,
vars: HashMap::new()
}.into()));
vars: HashMap::new(),
}
.into(),
));
fields.borrow_mut().insert("__add__".into(), add_ty);
}
let int64 = unifier.add_ty(TypeEnum::TObj {
@ -111,11 +114,11 @@ impl TestEnvironment {
let mut identifier_mapping = HashMap::new();
identifier_mapping.insert("None".into(), none);
let resolver = Arc::new(Box::new(Resolver {
let resolver = Arc::new(Resolver {
id_to_type: identifier_mapping.clone(),
id_to_def: Default::default(),
class_names: Default::default(),
}) as Box<dyn SymbolResolver + Send + Sync>);
}) as Arc<dyn SymbolResolver + Send + Sync>;
TestEnvironment {
top_level: TopLevelContext {
@ -147,11 +150,14 @@ impl TestEnvironment {
params: HashMap::new().into(),
});
if let TypeEnum::TObj { fields, .. } = &*unifier.get_ty(int32) {
let add_ty = unifier.add_ty(TypeEnum::TFunc(FunSignature {
let add_ty = unifier.add_ty(TypeEnum::TFunc(
FunSignature {
args: vec![FuncArg { name: "other".into(), ty: int32, default_value: None }],
ret: int32,
vars: HashMap::new()
}.into()));
vars: HashMap::new(),
}
.into(),
));
fields.borrow_mut().insert("__add__".into(), add_ty);
}
let int64 = unifier.add_ty(TypeEnum::TObj {
@ -308,7 +314,7 @@ impl TestEnvironment {
personality_symbol: None,
};
let resolver = Arc::new(Box::new(Resolver {
let resolver = Arc::new(Resolver {
id_to_type: identifier_mapping.clone(),
id_to_def: [
("Foo".into(), DefinitionId(5)),
@ -319,7 +325,7 @@ impl TestEnvironment {
.cloned()
.collect(),
class_names,
}) as Box<dyn SymbolResolver + Send + Sync>);
}) as Arc<dyn SymbolResolver + Send + Sync>;
TestEnvironment {
unifier,

View File

@ -1,8 +1,14 @@
use inkwell::values::BasicValueEnum;
use nac3core::{codegen::CodeGenContext, location::Location, symbol_resolver::{SymbolResolver, SymbolValue}, toplevel::{DefinitionId, TopLevelDef}, typecheck::{
use nac3core::{
codegen::CodeGenContext,
location::Location,
symbol_resolver::SymbolResolver,
toplevel::{DefinitionId, TopLevelDef},
typecheck::{
type_inferencer::PrimitiveStore,
typedef::{Type, Unifier},
}};
},
};
use parking_lot::{Mutex, RwLock};
use rustpython_parser::ast::StrRef;
use std::{collections::HashMap, sync::Arc};
@ -26,7 +32,13 @@ impl ResolverInternal {
pub struct Resolver(pub Arc<ResolverInternal>);
impl SymbolResolver for Resolver {
fn get_symbol_type(&self, _: &mut Unifier, _: &[Arc<RwLock<TopLevelDef>>], _: &PrimitiveStore, str: StrRef) -> Option<Type> {
fn get_symbol_type(
&self,
_: &mut Unifier,
_: &[Arc<RwLock<TopLevelDef>>],
_: &PrimitiveStore,
str: StrRef,
) -> Option<Type> {
let ret = self.0.id_to_type.lock().get(&str).cloned();
if ret.is_none() {
// println!("unknown here resolver {}", str);
@ -34,7 +46,11 @@ impl SymbolResolver for Resolver {
ret
}
fn get_symbol_value<'ctx, 'a>(&self, _: StrRef, _: &mut CodeGenContext<'ctx, 'a>) -> Option<BasicValueEnum<'ctx>> {
fn get_symbol_value<'ctx, 'a>(
&self,
_: StrRef,
_: &mut CodeGenContext<'ctx, 'a>,
) -> Option<BasicValueEnum<'ctx>> {
unimplemented!()
}

View File

@ -40,8 +40,8 @@ fn main() {
class_names: Default::default(),
}.into();
let resolver = Arc::new(
Box::new(Resolver(internal_resolver.clone())) as Box<dyn SymbolResolver + Send + Sync>
);
Resolver(internal_resolver.clone())
) as Arc<dyn SymbolResolver + Send + Sync>;
let setup_time = SystemTime::now();
println!("setup time: {}ms", setup_time.duration_since(start).unwrap().as_millis());