From a98f33e6d18110ed0311eb4a40c766a84772bb7d Mon Sep 17 00:00:00 2001 From: David Mak Date: Thu, 17 Oct 2024 15:57:33 +0800 Subject: [PATCH] [meta] Reorganize order of use declarations - Phase 2 Some more rules: - For module-level imports, prefer no prefix > super > crate. - Use crate instead of super if super refers to the crate-level module --- nac3artiq/src/codegen.rs | 2 +- nac3artiq/src/symbol_resolver.rs | 2 +- nac3core/src/codegen/builtin_fns.rs | 33 ++++++++++--------- nac3core/src/codegen/classes.rs | 2 +- nac3core/src/codegen/expr.rs | 40 ++++++++++++------------ nac3core/src/codegen/extern_fns.rs | 2 +- nac3core/src/codegen/generator.rs | 2 +- nac3core/src/codegen/llvm_intrinsics.rs | 2 +- nac3core/src/codegen/mod.rs | 5 ++- nac3core/src/codegen/numpy.rs | 33 ++++++++++--------- nac3core/src/codegen/test.rs | 12 +++---- nac3core/src/toplevel/builtins.rs | 4 +-- nac3core/src/toplevel/helper.rs | 6 ++-- nac3core/src/toplevel/mod.rs | 12 +++---- nac3core/src/toplevel/numpy.rs | 10 +++--- nac3core/src/toplevel/test.rs | 3 +- nac3core/src/toplevel/type_annotation.rs | 9 +++--- nac3core/src/typecheck/magic_methods.rs | 8 ++--- nac3core/src/typecheck/type_error.rs | 5 ++- nac3core/src/typecheck/typedef/mod.rs | 4 +-- nac3standalone/src/main.rs | 3 +- 21 files changed, 95 insertions(+), 104 deletions(-) diff --git a/nac3artiq/src/codegen.rs b/nac3artiq/src/codegen.rs index 413391cf8..7c43d8854 100644 --- a/nac3artiq/src/codegen.rs +++ b/nac3artiq/src/codegen.rs @@ -37,7 +37,7 @@ use nac3core::{ typecheck::typedef::{iter_type_vars, FunSignature, FuncArg, Type, TypeEnum, VarMap}, }; -use crate::{symbol_resolver::InnerResolver, timeline::TimeFns}; +use super::{symbol_resolver::InnerResolver, timeline::TimeFns}; /// The parallelism mode within a block. #[derive(Copy, Clone, Eq, PartialEq)] diff --git a/nac3artiq/src/symbol_resolver.rs b/nac3artiq/src/symbol_resolver.rs index 59f06bfc0..fd8ed0db1 100644 --- a/nac3artiq/src/symbol_resolver.rs +++ b/nac3artiq/src/symbol_resolver.rs @@ -37,7 +37,7 @@ use nac3core::{ }, }; -use crate::PrimitivePythonId; +use super::PrimitivePythonId; pub enum PrimitiveValue { I32(i32), diff --git a/nac3core/src/codegen/builtin_fns.rs b/nac3core/src/codegen/builtin_fns.rs index 66ac807c1..4260ebef2 100644 --- a/nac3core/src/codegen/builtin_fns.rs +++ b/nac3core/src/codegen/builtin_fns.rs @@ -5,24 +5,23 @@ use inkwell::{ }; use itertools::Itertools; -use crate::{ - codegen::{ - classes::{ - ArrayLikeValue, NDArrayValue, ProxyValue, RangeValue, TypedArrayLikeAccessor, - UntypedArrayLikeAccessor, UntypedArrayLikeMutator, - }, - expr::destructure_range, - extern_fns, irrt, - irrt::calculate_len_for_slice_range, - llvm_intrinsics, - macros::codegen_unreachable, - numpy, - numpy::ndarray_elementwise_unaryop_impl, - stmt::gen_for_callback_incrementing, - CodeGenContext, CodeGenerator, +use super::{ + classes::{ + ArrayLikeValue, NDArrayValue, ProxyValue, RangeValue, TypedArrayLikeAccessor, + UntypedArrayLikeAccessor, UntypedArrayLikeMutator, }, - toplevel::helper::PrimDef, - toplevel::numpy::unpack_ndarray_var_tys, + expr::destructure_range, + extern_fns, irrt, + irrt::calculate_len_for_slice_range, + llvm_intrinsics, + macros::codegen_unreachable, + numpy, + numpy::ndarray_elementwise_unaryop_impl, + stmt::gen_for_callback_incrementing, + CodeGenContext, CodeGenerator, +}; +use crate::{ + toplevel::{helper::PrimDef, numpy::unpack_ndarray_var_tys}, typecheck::typedef::{Type, TypeEnum}, }; diff --git a/nac3core/src/codegen/classes.rs b/nac3core/src/codegen/classes.rs index eee3bdf3b..8628aaa7e 100644 --- a/nac3core/src/codegen/classes.rs +++ b/nac3core/src/codegen/classes.rs @@ -5,7 +5,7 @@ use inkwell::{ AddressSpace, IntPredicate, }; -use crate::codegen::{ +use super::{ irrt::{call_ndarray_calc_size, call_ndarray_flatten_index}, llvm_intrinsics::call_int_umin, stmt::gen_for_callback_incrementing, diff --git a/nac3core/src/codegen/expr.rs b/nac3core/src/codegen/expr.rs index 65e5db2b1..8a0104207 100644 --- a/nac3core/src/codegen/expr.rs +++ b/nac3core/src/codegen/expr.rs @@ -18,27 +18,27 @@ use nac3parser::ast::{ Unaryop, }; -use crate::{ - codegen::{ - classes::{ - ArrayLikeIndexer, ArrayLikeValue, ListType, ListValue, NDArrayValue, ProxyType, - ProxyValue, RangeValue, TypedArrayLikeAccessor, UntypedArrayLikeAccessor, - }, - concrete_type::{ConcreteFuncArg, ConcreteTypeEnum, ConcreteTypeStore}, - gen_in_range_check, get_llvm_abi_type, get_llvm_type, get_va_count_arg_name, - irrt::*, - llvm_intrinsics::{ - call_expect, call_float_floor, call_float_pow, call_float_powi, call_int_smax, - call_int_umin, call_memcpy_generic, - }, - macros::codegen_unreachable, - need_sret, numpy, - stmt::{ - gen_for_callback_incrementing, gen_if_callback, gen_if_else_expr_callback, gen_raise, - gen_var, - }, - CodeGenContext, CodeGenTask, CodeGenerator, +use super::{ + classes::{ + ArrayLikeIndexer, ArrayLikeValue, ListType, ListValue, NDArrayValue, ProxyType, ProxyValue, + RangeValue, TypedArrayLikeAccessor, UntypedArrayLikeAccessor, }, + concrete_type::{ConcreteFuncArg, ConcreteTypeEnum, ConcreteTypeStore}, + gen_in_range_check, get_llvm_abi_type, get_llvm_type, get_va_count_arg_name, + irrt::*, + llvm_intrinsics::{ + call_expect, call_float_floor, call_float_pow, call_float_powi, call_int_smax, + call_int_umin, call_memcpy_generic, + }, + macros::codegen_unreachable, + need_sret, numpy, + stmt::{ + gen_for_callback_incrementing, gen_if_callback, gen_if_else_expr_callback, gen_raise, + gen_var, + }, + CodeGenContext, CodeGenTask, CodeGenerator, +}; +use crate::{ symbol_resolver::{SymbolValue, ValueEnum}, toplevel::{ helper::PrimDef, diff --git a/nac3core/src/codegen/extern_fns.rs b/nac3core/src/codegen/extern_fns.rs index 0db29f263..32cf37d8b 100644 --- a/nac3core/src/codegen/extern_fns.rs +++ b/nac3core/src/codegen/extern_fns.rs @@ -4,7 +4,7 @@ use inkwell::{ }; use itertools::Either; -use crate::codegen::CodeGenContext; +use super::CodeGenContext; /// Macro to generate extern function /// Both function return type and function parameter type are `FloatValue` diff --git a/nac3core/src/codegen/generator.rs b/nac3core/src/codegen/generator.rs index 77981dc58..5441cbbfd 100644 --- a/nac3core/src/codegen/generator.rs +++ b/nac3core/src/codegen/generator.rs @@ -6,8 +6,8 @@ use inkwell::{ use nac3parser::ast::{Expr, Stmt, StrRef}; +use super::{bool_to_i1, bool_to_i8, classes::ArraySliceValue, expr::*, stmt::*, CodeGenContext}; use crate::{ - codegen::{bool_to_i1, bool_to_i8, classes::ArraySliceValue, expr::*, stmt::*, CodeGenContext}, symbol_resolver::ValueEnum, toplevel::{DefinitionId, TopLevelDef}, typecheck::typedef::{FunSignature, Type}, diff --git a/nac3core/src/codegen/llvm_intrinsics.rs b/nac3core/src/codegen/llvm_intrinsics.rs index 2bfaad643..4b62a5c26 100644 --- a/nac3core/src/codegen/llvm_intrinsics.rs +++ b/nac3core/src/codegen/llvm_intrinsics.rs @@ -7,7 +7,7 @@ use inkwell::{ }; use itertools::Either; -use crate::codegen::CodeGenContext; +use super::CodeGenContext; /// Returns the string representation for the floating-point type `ft` when used in intrinsic /// functions. diff --git a/nac3core/src/codegen/mod.rs b/nac3core/src/codegen/mod.rs index 74a0244d8..02aa8ad18 100644 --- a/nac3core/src/codegen/mod.rs +++ b/nac3core/src/codegen/mod.rs @@ -37,6 +37,8 @@ use crate::{ typedef::{CallId, FuncArg, Type, TypeEnum, Unifier}, }, }; +use concrete_type::{ConcreteType, ConcreteTypeEnum, ConcreteTypeStore}; +pub use generator::{CodeGenerator, DefaultCodeGenerator}; pub mod builtin_fns; pub mod classes; @@ -52,9 +54,6 @@ pub mod stmt; #[cfg(test)] mod test; -use concrete_type::{ConcreteType, ConcreteTypeEnum, ConcreteTypeStore}; -pub use generator::{CodeGenerator, DefaultCodeGenerator}; - mod macros { /// Codegen-variant of [`std::unreachable`] which accepts an instance of [`CodeGenContext`] as /// its first argument to provide Python source information to indicate the codegen location diff --git a/nac3core/src/codegen/numpy.rs b/nac3core/src/codegen/numpy.rs index 421b159ec..ffe0d83d3 100644 --- a/nac3core/src/codegen/numpy.rs +++ b/nac3core/src/codegen/numpy.rs @@ -6,24 +6,23 @@ use inkwell::{ use nac3parser::ast::{Operator, StrRef}; -use crate::{ - codegen::{ - classes::{ - ArrayLikeIndexer, ArrayLikeValue, ListType, ListValue, NDArrayType, NDArrayValue, - ProxyType, ProxyValue, TypedArrayLikeAccessor, TypedArrayLikeAdapter, - TypedArrayLikeMutator, UntypedArrayLikeAccessor, UntypedArrayLikeMutator, - }, - expr::gen_binop_expr_with_values, - irrt::{ - calculate_len_for_slice_range, call_ndarray_calc_broadcast, - call_ndarray_calc_broadcast_index, call_ndarray_calc_nd_indices, - call_ndarray_calc_size, - }, - llvm_intrinsics::{self, call_memcpy_generic}, - macros::codegen_unreachable, - stmt::{gen_for_callback_incrementing, gen_for_range_callback, gen_if_else_expr_callback}, - CodeGenContext, CodeGenerator, +use super::{ + classes::{ + ArrayLikeIndexer, ArrayLikeValue, ListType, ListValue, NDArrayType, NDArrayValue, + ProxyType, ProxyValue, TypedArrayLikeAccessor, TypedArrayLikeAdapter, + TypedArrayLikeMutator, UntypedArrayLikeAccessor, UntypedArrayLikeMutator, }, + expr::gen_binop_expr_with_values, + irrt::{ + calculate_len_for_slice_range, call_ndarray_calc_broadcast, + call_ndarray_calc_broadcast_index, call_ndarray_calc_nd_indices, call_ndarray_calc_size, + }, + llvm_intrinsics::{self, call_memcpy_generic}, + macros::codegen_unreachable, + stmt::{gen_for_callback_incrementing, gen_for_range_callback, gen_if_else_expr_callback}, + CodeGenContext, CodeGenerator, +}; +use crate::{ symbol_resolver::ValueEnum, toplevel::{ helper::PrimDef, diff --git a/nac3core/src/codegen/test.rs b/nac3core/src/codegen/test.rs index b2799b04a..2bd02a712 100644 --- a/nac3core/src/codegen/test.rs +++ b/nac3core/src/codegen/test.rs @@ -15,13 +15,13 @@ use nac3parser::{ }; use parking_lot::RwLock; +use super::{ + classes::{ListType, NDArrayType, ProxyType, RangeType}, + concrete_type::ConcreteTypeStore, + CodeGenContext, CodeGenLLVMOptions, CodeGenTargetMachineOptions, CodeGenTask, CodeGenerator, + DefaultCodeGenerator, WithCall, WorkerRegistry, +}; use crate::{ - codegen::{ - classes::{ListType, NDArrayType, ProxyType, RangeType}, - concrete_type::ConcreteTypeStore, - CodeGenContext, CodeGenLLVMOptions, CodeGenTargetMachineOptions, CodeGenTask, - CodeGenerator, DefaultCodeGenerator, WithCall, WorkerRegistry, - }, symbol_resolver::{SymbolResolver, ValueEnum}, toplevel::{ composer::{ComposerConfig, TopLevelComposer}, diff --git a/nac3core/src/toplevel/builtins.rs b/nac3core/src/toplevel/builtins.rs index 4609e43b7..4d88b6eeb 100644 --- a/nac3core/src/toplevel/builtins.rs +++ b/nac3core/src/toplevel/builtins.rs @@ -11,7 +11,8 @@ use itertools::Either; use strum::IntoEnumIterator; use super::{ - helper::{debug_assert_prim_is_allowed, make_exception_fields, PrimDefDetails}, + helper::{debug_assert_prim_is_allowed, make_exception_fields, PrimDef, PrimDefDetails}, + numpy::make_ndarray_ty, *, }; use crate::{ @@ -22,7 +23,6 @@ use crate::{ stmt::exn_constructor, }, symbol_resolver::SymbolValue, - toplevel::{helper::PrimDef, numpy::make_ndarray_ty}, typecheck::typedef::{into_var_map, iter_type_vars, TypeVar, VarMap}, }; diff --git a/nac3core/src/toplevel/helper.rs b/nac3core/src/toplevel/helper.rs index f29487542..966131071 100644 --- a/nac3core/src/toplevel/helper.rs +++ b/nac3core/src/toplevel/helper.rs @@ -3,13 +3,11 @@ use std::convert::TryInto; use strum::IntoEnumIterator; use strum_macros::EnumIter; -use ast::ExprKind; -use nac3parser::ast::{Constant, Location}; +use nac3parser::ast::{Constant, ExprKind, Location}; -use super::*; +use super::{numpy::unpack_ndarray_var_tys, *}; use crate::{ symbol_resolver::SymbolValue, - toplevel::numpy::unpack_ndarray_var_tys, typecheck::typedef::{into_var_map, iter_type_vars, Mapping, TypeVarId, VarMap}, }; diff --git a/nac3core/src/toplevel/mod.rs b/nac3core/src/toplevel/mod.rs index e0479a881..cba2f5e7a 100644 --- a/nac3core/src/toplevel/mod.rs +++ b/nac3core/src/toplevel/mod.rs @@ -23,19 +23,19 @@ use crate::{ }, }, }; - -#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)] -pub struct DefinitionId(pub usize); +use composer::*; +use type_annotation::*; pub mod builtins; pub mod composer; pub mod helper; pub mod numpy; -pub mod type_annotation; -use composer::*; -use type_annotation::*; #[cfg(test)] mod test; +pub mod type_annotation; + +#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Copy, Hash, Debug)] +pub struct DefinitionId(pub usize); type GenCallCallback = dyn for<'ctx, 'a> Fn( &mut CodeGenContext<'ctx, 'a>, diff --git a/nac3core/src/toplevel/numpy.rs b/nac3core/src/toplevel/numpy.rs index 437abbf3d..1b9242ca7 100644 --- a/nac3core/src/toplevel/numpy.rs +++ b/nac3core/src/toplevel/numpy.rs @@ -1,11 +1,9 @@ use itertools::Itertools; -use crate::{ - toplevel::helper::PrimDef, - typecheck::{ - type_inferencer::PrimitiveStore, - typedef::{Type, TypeEnum, TypeVarId, Unifier, VarMap}, - }, +use super::helper::PrimDef; +use crate::typecheck::{ + type_inferencer::PrimitiveStore, + typedef::{Type, TypeEnum, TypeVarId, Unifier, VarMap}, }; /// Creates a `ndarray` [`Type`] with the given type arguments. diff --git a/nac3core/src/toplevel/test.rs b/nac3core/src/toplevel/test.rs index 077f6ab95..41dcf0dfd 100644 --- a/nac3core/src/toplevel/test.rs +++ b/nac3core/src/toplevel/test.rs @@ -9,11 +9,10 @@ use nac3parser::{ parser::parse_program, }; -use super::*; +use super::{helper::PrimDef, DefinitionId, *}; use crate::{ codegen::CodeGenContext, symbol_resolver::{SymbolResolver, ValueEnum}, - toplevel::{helper::PrimDef, DefinitionId}, typecheck::{ type_inferencer::PrimitiveStore, typedef::{into_var_map, Type, Unifier}, diff --git a/nac3core/src/toplevel/type_annotation.rs b/nac3core/src/toplevel/type_annotation.rs index f8b16f86c..a3af0b7f7 100644 --- a/nac3core/src/toplevel/type_annotation.rs +++ b/nac3core/src/toplevel/type_annotation.rs @@ -2,12 +2,11 @@ use strum::IntoEnumIterator; use nac3parser::ast::Constant; -use super::*; -use crate::{ - symbol_resolver::SymbolValue, - toplevel::helper::{PrimDef, PrimDefDetails}, - typecheck::typedef::VarMap, +use super::{ + helper::{PrimDef, PrimDefDetails}, + *, }; +use crate::{symbol_resolver::SymbolValue, typecheck::typedef::VarMap}; #[derive(Clone, Debug)] pub enum TypeAnnotation { diff --git a/nac3core/src/typecheck/magic_methods.rs b/nac3core/src/typecheck/magic_methods.rs index 322f7b9ed..60972f032 100644 --- a/nac3core/src/typecheck/magic_methods.rs +++ b/nac3core/src/typecheck/magic_methods.rs @@ -5,16 +5,16 @@ use strum::IntoEnumIterator; use nac3parser::ast::{Cmpop, Operator, StrRef, Unaryop}; +use super::{ + type_inferencer::*, + typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier, VarMap}, +}; use crate::{ symbol_resolver::SymbolValue, toplevel::{ helper::PrimDef, numpy::{make_ndarray_ty, unpack_ndarray_var_tys}, }, - typecheck::{ - type_inferencer::*, - typedef::{FunSignature, FuncArg, Type, TypeEnum, Unifier, VarMap}, - }, }; /// The variant of a binary operator. diff --git a/nac3core/src/typecheck/type_error.rs b/nac3core/src/typecheck/type_error.rs index 018464a92..3cf5bdc44 100644 --- a/nac3core/src/typecheck/type_error.rs +++ b/nac3core/src/typecheck/type_error.rs @@ -5,10 +5,9 @@ use itertools::Itertools; use nac3parser::ast::{Cmpop, Location, StrRef}; use super::{ - magic_methods::Binop, - typedef::{RecordKey, Type, Unifier}, + magic_methods::{Binop, HasOpInfo}, + typedef::{RecordKey, Type, TypeEnum, Unifier}, }; -use crate::typecheck::{magic_methods::HasOpInfo, typedef::TypeEnum}; #[derive(Debug, Clone)] pub enum TypeErrorKind { diff --git a/nac3core/src/typecheck/typedef/mod.rs b/nac3core/src/typecheck/typedef/mod.rs index 96fda4c2c..49cea04c3 100644 --- a/nac3core/src/typecheck/typedef/mod.rs +++ b/nac3core/src/typecheck/typedef/mod.rs @@ -14,14 +14,14 @@ use itertools::{repeat_n, Itertools}; use nac3parser::ast::{Cmpop, Location, StrRef, Unaryop}; use super::{ - magic_methods::{Binop, HasOpInfo}, + magic_methods::{Binop, HasOpInfo, OpInfo}, type_error::{TypeError, TypeErrorKind}, + type_inferencer::PrimitiveStore, unification_table::{UnificationKey, UnificationTable}, }; use crate::{ symbol_resolver::SymbolValue, toplevel::{helper::PrimDef, DefinitionId, TopLevelContext, TopLevelDef}, - typecheck::{magic_methods::OpInfo, type_inferencer::PrimitiveStore}, }; #[cfg(test)] diff --git a/nac3standalone/src/main.rs b/nac3standalone/src/main.rs index 71bfe8e5a..965d274e3 100644 --- a/nac3standalone/src/main.rs +++ b/nac3standalone/src/main.rs @@ -45,9 +45,10 @@ use nac3core::{ }, }; -mod basic_symbol_resolver; use basic_symbol_resolver::*; +mod basic_symbol_resolver; + /// Command-line argument parser definition. #[derive(Parser)] #[command(author, version, about, long_about = None)]