diff --git a/Cargo.lock b/Cargo.lock index 33e3e94d..3163be1a 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -665,9 +665,9 @@ dependencies = [ [[package]] name = "libz-sys" -version = "1.1.16" +version = "1.1.18" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5e143b5e666b2695d28f6bca6497720813f699c9602dd7f5cac91008b8ada7f9" +checksum = "c15da26e5af7e25c90b37a2d75cdbf940cf4a55316de9d84c679c9b8bfabf82e" dependencies = [ "cc", "libc", @@ -1231,9 +1231,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.65" +version = "2.0.66" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d2863d96a84c6439701d7a38f9de935ec562c8832cc55d1dde0f513b52fad106" +checksum = "c42f3f41a2de00b01c0aaad383c5a45241efc8b2d1eda5661812fda5f3cdcff5" dependencies = [ "proc-macro2", "quote", diff --git a/libcore/src/libbasic.rs b/libcore/src/libbasic.rs index 1a9b9153..06dce6a7 100644 --- a/libcore/src/libbasic.rs +++ b/libcore/src/libbasic.rs @@ -10,14 +10,13 @@ use super::{codegen::Opcode, error::*}; pub type RustlibFunc = fn(&mut DynaData) -> ErrorInfoResult<()>; -pub type ScopeAllocIdTy = usize; +pub type ScopeAllocId = usize; pub type TypeAllowNull = Option; // 定义函数的索引类型 -crate::impl_newtype_int!(FuncIdxTy, usize); +crate::impl_newtype_int!(FuncIdx, usize); crate::impl_newtype_int!(ClassIdxId, usize); -crate::impl_newtype_int!(VarIdxTy, usize); +crate::impl_newtype_int!(VarIdx, usize); -pub type TyIdxTy = ScopeAllocIdTy; type ConstPoolIndexTy = usize; #[derive(Clone, Debug)] @@ -61,7 +60,7 @@ pub type Argvs = Vec; #[derive(Clone, Debug)] pub struct RustFunction { pub name: String, - pub buildin_id: FuncIdxTy, + pub buildin_id: FuncIdx, pub ptr: RustlibFunc, pub io: IOType, } @@ -121,7 +120,7 @@ pub trait FunctionInterface: Downcast + Debug { fn get_io(&self) -> &IOType; fn get_name(&self) -> &str; fn get_io_mut(&mut self) -> &mut IOType; - fn get_func_id(&self) -> FuncIdxTy; + fn get_func_id(&self) -> FuncIdx; } impl_downcast!(FunctionInterface); @@ -157,7 +156,7 @@ impl FunctionInterface for RustFunction { &mut self.io } - fn get_func_id(&self) -> FuncIdxTy { + fn get_func_id(&self) -> FuncIdx { self.buildin_id } } @@ -176,12 +175,17 @@ impl OverrideWrapper { #[derive(Debug, Clone, Default)] pub struct RustClass { - pub members: HashMap, + /// 成员变量 + pub attribute_members: HashMap, + /// 成员函数 pub functions: HashMap, + /// 重载函数 pub overrides: HashMap, + /// 类ID pub id: ClassIdxId, + /// 类名 pub name: &'static str, - pub id_to_var: HashMap, + pub id_to_var: HashMap, } /// 约定,0号id是any类型 @@ -194,7 +198,7 @@ impl RustClass { storage: &mut ModuleStorage, ) -> ClassIdxId { let ret = RustClass { - members, + attribute_members: members, functions: functions.unwrap_or_default(), overrides: overrides.unwrap_or_default(), id: ClassIdxId(storage.class_table.len()), @@ -209,7 +213,7 @@ impl RustClass { } pub fn add_attr(&mut self, name: impl Into, ty: String) { - self.members.insert(name.into(), ty); + self.attribute_members.insert(name.into(), ty); } } @@ -265,9 +269,9 @@ impl ModuleStorage { } } - pub fn add_func(&mut self, f: RustlibFunc) -> FuncIdxTy { + pub fn add_func(&mut self, f: RustlibFunc) -> FuncIdx { self.func_table.push(f); - FuncIdxTy(self.func_table.len() - 1) + FuncIdx(self.func_table.len() - 1) } /// 获取类的个数 @@ -281,7 +285,7 @@ impl ModuleStorage { ClassIdxId(self.class_table.len() - 1) } - pub fn access_func(&self, id: FuncIdxTy) -> RustlibFunc { + pub fn access_func(&self, id: FuncIdx) -> RustlibFunc { self.func_table[*id] } diff --git a/src/compiler/ast.rs b/src/compiler/ast.rs index aeac125a..ee3aa4e8 100644 --- a/src/compiler/ast.rs +++ b/src/compiler/ast.rs @@ -439,7 +439,7 @@ impl<'a> ModuleUnit<'a> { fn load_var( &mut self, - idx: ScopeAllocIdTy, + idx: ScopeAllocId, name_token: ConstPoolIndexTy, istry: bool, ) -> AstError<()> { @@ -1019,7 +1019,7 @@ impl<'a> ModuleUnit<'a> { .token_lexer .borrow_mut() .add_id(func_item.get_name().to_owned()); - let func_extern_id = FuncIdxTy( + let func_extern_id = FuncIdx( *func_item.buildin_id + self.modules_info[&import_file_path], ); // println!("{}", func_extern_id); @@ -1039,7 +1039,7 @@ impl<'a> ModuleUnit<'a> { } Some(module) => { let tmp = self.token_lexer.borrow_mut().add_id(import_item_name); - let module_sym_idx: ScopeAllocIdTy = self.insert_sym_with_error(tmp)?; + let module_sym_idx: ScopeAllocId = self.insert_sym_with_error(tmp)?; self.import_module_sym(module); let sub_module = Rc::new(RefCell::new(SymScope::new(SymScopePrev::Prev( self.self_scope.clone(), diff --git a/src/compiler/ast/ast_base.rs b/src/compiler/ast/ast_base.rs index 9a38015e..b09c49e8 100644 --- a/src/compiler/ast/ast_base.rs +++ b/src/compiler/ast/ast_base.rs @@ -195,7 +195,7 @@ impl<'a> ModuleUnit<'a> { } /// 通过Scope中的ID获取类型名 - pub fn get_ty_name(&mut self, type_name: ScopeAllocIdTy) -> String { + pub fn get_ty_name(&mut self, type_name: ScopeAllocId) -> String { self.self_scope .borrow() .get_class(type_name) @@ -224,7 +224,7 @@ impl<'a> ModuleUnit<'a> { } /// 添加一个符号,在符号冲突的时候报出错误 - pub fn insert_sym_with_error(&mut self, name: ConstPoolIndexTy) -> AstError { + pub fn insert_sym_with_error(&mut self, name: ConstPoolIndexTy) -> AstError { match self.self_scope.borrow_mut().insert_sym(name) { Some(v) => Ok(v), None => self.gen_error(ErrorInfo::new( diff --git a/src/compiler/manager.rs b/src/compiler/manager.rs index 30b06fb4..a3c7b66f 100644 --- a/src/compiler/manager.rs +++ b/src/compiler/manager.rs @@ -5,7 +5,7 @@ use crate::{ compiler::{ast::ModuleUnit, linker::link, optimizer::optimize_module, Compiler, CompilerImpl}, }; use libcore::StaticData; -use libcore::{utils, FuncIdxTy}; +use libcore::{utils, FuncIdx}; use std::{ cell::RefCell, collections::{hash_map::IterMut, HashMap}, @@ -112,16 +112,16 @@ impl<'a> ModuleManager<'a> { // 对应模块不存在,返回None } - pub fn alloc_custom_function_id(&mut self) -> FuncIdxTy { + pub fn alloc_custom_function_id(&mut self) -> FuncIdx { let ret = self.global_custom_function_id; self.global_custom_function_id += 1; - FuncIdxTy(ret) + FuncIdx(ret) } - pub fn alloc_extern_function_id(&mut self) -> FuncIdxTy { + pub fn alloc_extern_function_id(&mut self) -> FuncIdx { let ret = self.global_extern_function_id; self.global_extern_function_id += 1; - FuncIdxTy(ret) + FuncIdx(ret) } /// 执行优化 diff --git a/src/compiler/scope.rs b/src/compiler/scope.rs index 6b4a51cf..b2eae02c 100644 --- a/src/compiler/scope.rs +++ b/src/compiler/scope.rs @@ -15,7 +15,7 @@ use std::{ pub struct CustomFunction { io: IOType, pub args_names: ArgsNameTy, - pub custom_id: FuncIdxTy, + pub custom_id: FuncIdx, name: String, } @@ -31,7 +31,7 @@ impl CustomFunction { io, args_names, name: name.into(), - custom_id: FuncIdxTy::default(), + custom_id: FuncIdx::default(), } } @@ -51,7 +51,7 @@ impl FunctionInterface for CustomFunction { &mut self.io } - fn get_func_id(&self) -> FuncIdxTy { + fn get_func_id(&self) -> FuncIdx { self.custom_id } } @@ -80,7 +80,7 @@ impl CustomType { } } - pub fn add_attr(&mut self, attrname: ScopeAllocIdTy, attrty: ClassIdxId) -> Option { + pub fn add_attr(&mut self, attrname: ScopeAllocId, attrty: ClassIdxId) -> Option { self.id_to_attr.insert(attrname, attrty) } @@ -197,20 +197,20 @@ pub struct SymScope { // 父作用域 pub prev_scope: RootOnlyInfo>>, // 管理符号之间的映射,由token在name pool中的id映射到符号表中的id - sym_map: HashMap, + sym_map: HashMap, // 当前作用域要分配的下一个ID,也就是当前作用域的最大id+1 - scope_sym_id: ScopeAllocIdTy, + scope_sym_id: ScopeAllocId, // ID到class id的映射 - types: HashMap, + types: HashMap, // ID到函数的映射 - funcs: HashMap>, + funcs: HashMap>, // id到变量类型的映射 - vars: HashMap, + vars: HashMap, // 由token id到模块的映射 // modules: HashMap, - vars_id: ScopeAllocIdTy, + vars_id: ScopeAllocId, // 作用域暂时储存的函数token - pub funcs_temp_store: Vec<(ScopeAllocIdTy, Vec<(Token, usize)>)>, + pub funcs_temp_store: Vec<(ScopeAllocId, Vec<(Token, usize)>)>, // 计算当前需要最少多大的空间来保存变量 pub var_sz: usize, // 保存当前环境下的function io,global环境无 @@ -222,7 +222,7 @@ pub struct SymScope { pub in_loop: bool, pub in_class: bool, pub is_pub: bool, - imported_modules: HashMap>>, + imported_modules: HashMap>>, root_scope: Option>>, } @@ -256,8 +256,8 @@ impl SymScope { pub fn add_custom_function( &mut self, - id: ScopeAllocIdTy, - function_id: FuncIdxTy, + id: ScopeAllocId, + function_id: FuncIdx, mut f: CustomFunction, body: Vec<(Token, usize)>, ) { @@ -270,14 +270,14 @@ impl SymScope { &mut self, sym_name: ConstPoolIndexTy, str_name: &str, - ) -> Result { + ) -> Result { match self.insert_sym(sym_name) { Some(v) => Ok(v), None => Err(symbol_redefined(str_name)), } } - pub fn add_imported_module(&mut self, id: ScopeAllocIdTy, son_modules: Rc>) { + pub fn add_imported_module(&mut self, id: ScopeAllocId, son_modules: Rc>) { self.imported_modules.insert(id, son_modules); } @@ -339,12 +339,12 @@ impl SymScope { // 在将类型全部添加进去之后,需要重新改写函数和类的输入和输出参数 let mut fobj = i.1.clone(); self.fix_func(fobj.get_io_mut(), libstorage, const_pool); - self.add_extern_func(idx, FuncIdxTy(prev_func_base + *fobj.buildin_id), fobj); + self.add_extern_func(idx, FuncIdx(prev_func_base + *fobj.buildin_id), fobj); } Ok(()) } - pub fn get_module(&self, id: ScopeAllocIdTy) -> Option>> { + pub fn get_module(&self, id: ScopeAllocId) -> Option>> { let t = self.imported_modules.get(&id); match t { None => match self.prev_scope { @@ -355,7 +355,7 @@ impl SymScope { } } - pub fn get_function(&self, id: ScopeAllocIdTy) -> Option> { + pub fn get_function(&self, id: ScopeAllocId) -> Option> { match self.funcs.get(&id) { Some(f) => Some(f.clone()), None => match self.prev_scope { @@ -392,7 +392,7 @@ impl SymScope { } /// 返回变量的类型,索引和内存地址 - pub fn get_var(&self, id: ScopeAllocIdTy) -> Option { + pub fn get_var(&self, id: ScopeAllocId) -> Option { match self.vars.get(&id) { Some(v) => Some(*v), None => match self.prev_scope { @@ -413,11 +413,11 @@ impl SymScope { } } - fn add_func(&mut self, id: ScopeAllocIdTy, f: Rc) { + fn add_func(&mut self, id: ScopeAllocId, f: Rc) { self.funcs.insert(id, f); } - fn add_extern_func(&mut self, id: ScopeAllocIdTy, function_id: FuncIdxTy, mut f: RustFunction) { + fn add_extern_func(&mut self, id: ScopeAllocId, function_id: FuncIdx, mut f: RustFunction) { f.buildin_id = function_id; self.add_func(id, Rc::new(f)) } @@ -427,7 +427,7 @@ impl SymScope { mut f: RustFunction, tokenid: usize, fname: &str, - extern_function_id: FuncIdxTy, + extern_function_id: FuncIdx, storage: &ModuleStorage, pool: &ValuePool, ) -> Result<(), ErrorInfo> { @@ -439,26 +439,21 @@ impl SymScope { } /// 返回变量的索引和内存地址 - pub fn add_var( - &mut self, - id: ScopeAllocIdTy, - ty: ClassIdxId, - var_sz: usize, - ) -> (VarIdxTy, usize) { + pub fn add_var(&mut self, id: ScopeAllocId, ty: ClassIdxId, var_sz: usize) -> (VarIdx, usize) { let ret_addr = self.var_sz; self.vars .insert(id, VarInfo::new(ty, self.vars_id, ret_addr)); let ret = self.vars_id; self.vars_id += 1; self.var_sz += var_sz; - (VarIdxTy(ret), ret_addr) + (VarIdx(ret), ret_addr) } pub fn get_var_table_sz(&self) -> usize { self.var_sz } - pub fn get_type_id(&self, id: ScopeAllocIdTy) -> Option { + pub fn get_type_id(&self, id: ScopeAllocId) -> Option { match self.types.get(&id) { None => match self.prev_scope { RootOnlyInfo::NonRoot(ref prev_scope) => prev_scope.borrow().get_type_id(id), @@ -489,7 +484,7 @@ impl SymScope { } } - pub fn get_class(&self, classid_idx: ScopeAllocIdTy) -> Option> { + pub fn get_class(&self, classid_idx: ScopeAllocId) -> Option> { let t = self.types.get(&classid_idx); match t { Some(t) => self.get_class_by_class_id(*t), @@ -500,7 +495,7 @@ impl SymScope { } } - fn alloc_type_id(&mut self, idx: ScopeAllocIdTy) -> Result { + fn alloc_type_id(&mut self, idx: ScopeAllocId) -> Result { let ret = match &mut self.prev_scope { RootOnlyInfo::NonRoot(ref prev_scope) => prev_scope.borrow_mut().alloc_type_id(idx), RootOnlyInfo::Root(data) => data.alloc_type_id(), @@ -518,7 +513,7 @@ impl SymScope { pub fn add_type( &mut self, - idx: ScopeAllocIdTy, + idx: ScopeAllocId, obj: Rc, ) -> Result { let ret = self.alloc_type_id(idx)?;