forked from M-Labs/nac3
Compare commits
1 Commits
master
...
refactor_c
Author | SHA1 | Date |
---|---|---|
abdul124 | e5893e0a65 |
|
@ -117,9 +117,9 @@ checksum = "1fd0f2584146f6f2ef48085050886acf353beff7305ebd1ae69500e27c67f64b"
|
|||
|
||||
[[package]]
|
||||
name = "cc"
|
||||
version = "1.1.15"
|
||||
version = "1.1.13"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "57b6a275aa2903740dc87da01c62040406b8812552e97129a63ea8850a17c6e6"
|
||||
checksum = "72db2f7947ecee9b03b510377e8bb9077afa27176fdbff55c51027e976fdcc48"
|
||||
dependencies = [
|
||||
"shlex",
|
||||
]
|
||||
|
@ -161,7 +161,7 @@ dependencies = [
|
|||
"heck 0.5.0",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -310,9 +310,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "fastrand"
|
||||
version = "2.1.1"
|
||||
version = "2.1.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8c02a5121d4ea3eb16a80748c74f5549a5665e4c21333c6098f283870fbdea6"
|
||||
checksum = "9fc0510504f03c51ada170672ac806f1f105a88aa97a5281117e1ddc3368e51a"
|
||||
|
||||
[[package]]
|
||||
name = "fixedbitset"
|
||||
|
@ -424,7 +424,7 @@ checksum = "4fa4d8d74483041a882adaa9a29f633253a66dde85055f0495c121620ac484b2"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -510,9 +510,9 @@ checksum = "bbd2bcb4c963f2ddae06a2efc7e9f3591312473c50c6685e1f298068316e66fe"
|
|||
|
||||
[[package]]
|
||||
name = "libc"
|
||||
version = "0.2.158"
|
||||
version = "0.2.157"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "d8adc4bb1803a324070e64a98ae98f38934d91957a99cfb3a43dcbc01bc56439"
|
||||
checksum = "374af5f94e54fa97cf75e945cce8a6b201e88a1a07e688b47dfd2a59c66dbd86"
|
||||
|
||||
[[package]]
|
||||
name = "libloading"
|
||||
|
@ -752,7 +752,7 @@ dependencies = [
|
|||
"phf_shared 0.11.2",
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -856,7 +856,7 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
"pyo3-macros-backend",
|
||||
"quote",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -869,14 +869,14 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
"pyo3-build-config",
|
||||
"quote",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "quote"
|
||||
version = "1.0.37"
|
||||
version = "1.0.36"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af"
|
||||
checksum = "0fa76aaf39101c457836aec0ce2316dbdc3ab723cdda1c6bd4e6ad4208acaca7"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
]
|
||||
|
@ -942,9 +942,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "redox_users"
|
||||
version = "0.4.6"
|
||||
version = "0.4.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "ba009ff324d1fc1b900bd1fdb31564febe58a8ccc8a6fdbb93b543d33b13ca43"
|
||||
checksum = "bd283d9651eeda4b2a83a43c1c91b266c40fd76ecd39a50a8c630ae69dc72891"
|
||||
dependencies = [
|
||||
"getrandom",
|
||||
"libredox",
|
||||
|
@ -989,9 +989,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "rustix"
|
||||
version = "0.38.35"
|
||||
version = "0.38.34"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a85d50532239da68e9addb745ba38ff4612a242c1c7ceea689c4bc7c2f43c36f"
|
||||
checksum = "70dc5ec042f7a43c4a73241207cecc9873a06d45debb38b329f8541d85c2730f"
|
||||
dependencies = [
|
||||
"bitflags",
|
||||
"errno",
|
||||
|
@ -1035,29 +1035,29 @@ checksum = "61697e0a1c7e512e84a621326239844a24d8207b4669b41bc18b32ea5cbf988b"
|
|||
|
||||
[[package]]
|
||||
name = "serde"
|
||||
version = "1.0.209"
|
||||
version = "1.0.208"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "99fce0ffe7310761ca6bf9faf5115afbc19688edd00171d81b1bb1b116c63e09"
|
||||
checksum = "cff085d2cb684faa248efb494c39b68e522822ac0de72ccf08109abde717cfb2"
|
||||
dependencies = [
|
||||
"serde_derive",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_derive"
|
||||
version = "1.0.209"
|
||||
version = "1.0.208"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "a5831b979fd7b5439637af1752d535ff49f4860c0f341d1baeb6faf0f4242170"
|
||||
checksum = "24008e81ff7613ed8e5ba0cfaf24e2c2f1e5b8a0495711e44fcd4882fca62bcf"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "serde_json"
|
||||
version = "1.0.127"
|
||||
version = "1.0.125"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "8043c06d9f82bd7271361ed64f415fe5e12a77fdb52e573e7f06a516dea329ad"
|
||||
checksum = "83c8e735a073ccf5be70aa8066aa984eaf2fa000db6c8d0100ae605b366d31ed"
|
||||
dependencies = [
|
||||
"itoa",
|
||||
"memchr",
|
||||
|
@ -1147,7 +1147,7 @@ dependencies = [
|
|||
"proc-macro2",
|
||||
"quote",
|
||||
"rustversion",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1163,9 +1163,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "syn"
|
||||
version = "2.0.76"
|
||||
version = "2.0.75"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "578e081a14e0cefc3279b0472138c513f37b41a08d5a3cca9b6e4e8ceb6cd525"
|
||||
checksum = "f6af063034fc1935ede7be0122941bafa9bacb949334d090b77ca98b5817c7d9"
|
||||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
|
@ -1232,7 +1232,7 @@ checksum = "a4558b58466b9ad7ca0f102865eccc95938dca1a74a856f2b57b6629050da261"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -1310,9 +1310,9 @@ checksum = "0336d538f7abc86d282a4189614dfaa90810dfc2c6f6427eaf88e16311dd225d"
|
|||
|
||||
[[package]]
|
||||
name = "unicode-xid"
|
||||
version = "0.2.5"
|
||||
version = "0.2.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "229730647fbc343e3a80e463c1db7f78f3855d3f3739bee0dda773c9a037c90a"
|
||||
checksum = "f962df74c8c05a667b5ee8bcf162993134c104e96440b663c8daa176dc772d8c"
|
||||
|
||||
[[package]]
|
||||
name = "unicode_names2"
|
||||
|
@ -1510,5 +1510,5 @@ checksum = "fa4f8080344d4671fb4e831a13ad1e68092748387dfc4f55e356242fae12ce3e"
|
|||
dependencies = [
|
||||
"proc-macro2",
|
||||
"quote",
|
||||
"syn 2.0.76",
|
||||
"syn 2.0.75",
|
||||
]
|
||||
|
|
|
@ -2,11 +2,11 @@
|
|||
"nodes": {
|
||||
"nixpkgs": {
|
||||
"locked": {
|
||||
"lastModified": 1724819573,
|
||||
"narHash": "sha256-GnR7/ibgIH1vhoy8cYdmXE6iyZqKqFxQSVkFgosBh6w=",
|
||||
"lastModified": 1723637854,
|
||||
"narHash": "sha256-med8+5DSWa2UnOqtdICndjDAEjxr5D7zaIiK4pn0Q7c=",
|
||||
"owner": "NixOS",
|
||||
"repo": "nixpkgs",
|
||||
"rev": "71e91c409d1e654808b2621f28a327acfdad8dc2",
|
||||
"rev": "c3aa7b8938b17aebd2deecf7be0636000d62a2b9",
|
||||
"type": "github"
|
||||
},
|
||||
"original": {
|
||||
|
|
|
@ -180,7 +180,9 @@
|
|||
clippy
|
||||
pre-commit
|
||||
rustfmt
|
||||
rust-analyzer
|
||||
];
|
||||
RUST_SRC_PATH = "${pkgs.rust.packages.stable.rustPlatform.rustLibSrc}";
|
||||
shellHook =
|
||||
''
|
||||
export DEMO_LINALG_STUB=${packages.x86_64-linux.demo-linalg-stub}/lib/liblinalg.a
|
||||
|
|
|
@ -2,7 +2,7 @@ use nac3core::{
|
|||
codegen::{
|
||||
classes::{
|
||||
ArrayLikeIndexer, ArrayLikeValue, ArraySliceValue, ListValue, NDArrayType,
|
||||
NDArrayValue, ProxyType, ProxyValue, RangeValue, UntypedArrayLikeAccessor,
|
||||
NDArrayValue, RangeValue, UntypedArrayLikeAccessor,
|
||||
},
|
||||
expr::{destructure_range, gen_call},
|
||||
irrt::call_ndarray_calc_size,
|
||||
|
@ -22,7 +22,7 @@ use inkwell::{
|
|||
module::Linkage,
|
||||
types::{BasicType, IntType},
|
||||
values::{BasicValueEnum, PointerValue, StructValue},
|
||||
AddressSpace, IntPredicate, OptimizationLevel,
|
||||
AddressSpace, IntPredicate,
|
||||
};
|
||||
|
||||
use pyo3::{
|
||||
|
@ -32,7 +32,6 @@ use pyo3::{
|
|||
|
||||
use crate::{symbol_resolver::InnerResolver, timeline::TimeFns};
|
||||
|
||||
use inkwell::values::IntValue;
|
||||
use itertools::Itertools;
|
||||
use std::{
|
||||
collections::{hash_map::DefaultHasher, HashMap},
|
||||
|
@ -487,10 +486,13 @@ fn format_rpc_arg<'ctx>(
|
|||
let buffer = ctx.builder.build_array_alloca(llvm_i8, buffer_size, "rpc.arg").unwrap();
|
||||
let buffer = ArraySliceValue::from_ptr_val(buffer, buffer_size, Some("rpc.arg"));
|
||||
|
||||
let ppdata = generator.gen_var_alloc(ctx, llvm_arg_ty.element_type(), None).unwrap();
|
||||
ctx.builder.build_store(ppdata, llvm_arg.data().base_ptr(ctx, generator)).unwrap();
|
||||
|
||||
call_memcpy_generic(
|
||||
ctx,
|
||||
buffer.base_ptr(ctx, generator),
|
||||
llvm_arg.ptr_to_data(ctx),
|
||||
ppdata,
|
||||
llvm_pdata_sizeof,
|
||||
llvm_i1.const_zero(),
|
||||
);
|
||||
|
@ -526,298 +528,6 @@ fn format_rpc_arg<'ctx>(
|
|||
arg_slot
|
||||
}
|
||||
|
||||
/// Formats an RPC return value to conform to the expected format required by NAC3.
|
||||
fn format_rpc_ret<'ctx>(
|
||||
generator: &mut dyn CodeGenerator,
|
||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||
ret_ty: Type,
|
||||
) -> Option<BasicValueEnum<'ctx>> {
|
||||
// -- receive value:
|
||||
// T result = {
|
||||
// void *ret_ptr = alloca(sizeof(T));
|
||||
// void *ptr = ret_ptr;
|
||||
// loop: int size = rpc_recv(ptr);
|
||||
// // Non-zero: Provide `size` bytes of extra storage for variable-length data.
|
||||
// if(size) { ptr = alloca(size); goto loop; }
|
||||
// else *(T*)ret_ptr
|
||||
// }
|
||||
|
||||
let llvm_i8 = ctx.ctx.i8_type();
|
||||
let llvm_i32 = ctx.ctx.i32_type();
|
||||
let llvm_i8_8 = ctx.ctx.struct_type(&[llvm_i8.array_type(8).into()], false);
|
||||
let llvm_pi8 = llvm_i8.ptr_type(AddressSpace::default());
|
||||
|
||||
let rpc_recv = ctx.module.get_function("rpc_recv").unwrap_or_else(|| {
|
||||
ctx.module.add_function("rpc_recv", llvm_i32.fn_type(&[llvm_pi8.into()], false), None)
|
||||
});
|
||||
|
||||
if ctx.unifier.unioned(ret_ty, ctx.primitives.none) {
|
||||
ctx.build_call_or_invoke(rpc_recv, &[llvm_pi8.const_null().into()], "rpc_recv");
|
||||
return None;
|
||||
}
|
||||
|
||||
let prehead_bb = ctx.builder.get_insert_block().unwrap();
|
||||
let current_function = prehead_bb.get_parent().unwrap();
|
||||
let head_bb = ctx.ctx.append_basic_block(current_function, "rpc.head");
|
||||
let alloc_bb = ctx.ctx.append_basic_block(current_function, "rpc.continue");
|
||||
let tail_bb = ctx.ctx.append_basic_block(current_function, "rpc.tail");
|
||||
|
||||
let llvm_ret_ty = ctx.get_llvm_abi_type(generator, ret_ty);
|
||||
|
||||
let result = match &*ctx.unifier.get_ty_immutable(ret_ty) {
|
||||
TypeEnum::TObj { obj_id, .. } if *obj_id == PrimDef::NDArray.id() => {
|
||||
let llvm_i1 = ctx.ctx.bool_type();
|
||||
let llvm_usize = generator.get_size_type(ctx.ctx);
|
||||
|
||||
// Round `val` up to its modulo `power_of_two`
|
||||
let round_up = |ctx: &mut CodeGenContext<'ctx, '_>,
|
||||
val: IntValue<'ctx>,
|
||||
power_of_two: IntValue<'ctx>| {
|
||||
debug_assert_eq!(
|
||||
val.get_type().get_bit_width(),
|
||||
power_of_two.get_type().get_bit_width()
|
||||
);
|
||||
|
||||
let llvm_val_t = val.get_type();
|
||||
|
||||
let max_rem = ctx
|
||||
.builder
|
||||
.build_int_sub(power_of_two, llvm_val_t.const_int(1, false), "")
|
||||
.unwrap();
|
||||
ctx.builder
|
||||
.build_and(
|
||||
ctx.builder.build_int_add(val, max_rem, "").unwrap(),
|
||||
ctx.builder.build_not(max_rem, "").unwrap(),
|
||||
"",
|
||||
)
|
||||
.unwrap()
|
||||
};
|
||||
|
||||
// Setup types
|
||||
let (elem_ty, ndims) = unpack_ndarray_var_tys(&mut ctx.unifier, ret_ty);
|
||||
let llvm_elem_ty = ctx.get_llvm_type(generator, elem_ty);
|
||||
let llvm_ret_ty = NDArrayType::new(generator, ctx.ctx, llvm_elem_ty);
|
||||
|
||||
// Allocate the resulting ndarray
|
||||
// A condition after format_rpc_ret ensures this will not be popped this off.
|
||||
let ndarray = llvm_ret_ty.new_value(generator, ctx, Some("rpc.result"));
|
||||
|
||||
// Setup ndims
|
||||
let ndims =
|
||||
if let TypeEnum::TLiteral { values, .. } = &*ctx.unifier.get_ty_immutable(ndims) {
|
||||
assert_eq!(values.len(), 1);
|
||||
|
||||
u64::try_from(values[0].clone()).unwrap()
|
||||
} else {
|
||||
unreachable!();
|
||||
};
|
||||
// Set `ndarray.ndims`
|
||||
ndarray.store_ndims(ctx, generator, llvm_usize.const_int(ndims, false));
|
||||
// Allocate `ndarray.shape` [size_t; ndims]
|
||||
ndarray.create_dim_sizes(ctx, llvm_usize, ndarray.load_ndims(ctx));
|
||||
|
||||
/*
|
||||
ndarray now:
|
||||
- .ndims: initialized
|
||||
- .shape: allocated but uninitialized .shape
|
||||
- .data: uninitialized
|
||||
*/
|
||||
|
||||
let llvm_usize_sizeof = ctx
|
||||
.builder
|
||||
.build_int_truncate_or_bit_cast(llvm_usize.size_of(), llvm_usize, "")
|
||||
.unwrap();
|
||||
let llvm_pdata_sizeof = ctx
|
||||
.builder
|
||||
.build_int_truncate_or_bit_cast(
|
||||
llvm_ret_ty.element_type().size_of().unwrap(),
|
||||
llvm_usize,
|
||||
"",
|
||||
)
|
||||
.unwrap();
|
||||
let llvm_elem_sizeof = ctx
|
||||
.builder
|
||||
.build_int_truncate_or_bit_cast(llvm_elem_ty.size_of().unwrap(), llvm_usize, "")
|
||||
.unwrap();
|
||||
|
||||
// Allocates a buffer for the initial RPC'ed object, which is guaranteed to be
|
||||
// (4 + 4 * ndims) bytes with 8-byte alignment
|
||||
let sizeof_dims =
|
||||
ctx.builder.build_int_mul(ndarray.load_ndims(ctx), llvm_usize_sizeof, "").unwrap();
|
||||
let unaligned_buffer_size =
|
||||
ctx.builder.build_int_add(sizeof_dims, llvm_pdata_sizeof, "").unwrap();
|
||||
let buffer_size = round_up(ctx, unaligned_buffer_size, llvm_usize.const_int(8, false));
|
||||
|
||||
let stackptr = call_stacksave(ctx, None);
|
||||
// Just to be absolutely sure, alloca in [i8 x 8] slices to force 8-byte alignment
|
||||
let buffer = ctx
|
||||
.builder
|
||||
.build_array_alloca(
|
||||
llvm_i8_8,
|
||||
ctx.builder
|
||||
.build_int_unsigned_div(buffer_size, llvm_usize.const_int(8, false), "")
|
||||
.unwrap(),
|
||||
"rpc.buffer",
|
||||
)
|
||||
.unwrap();
|
||||
let buffer = ctx
|
||||
.builder
|
||||
.build_bitcast(buffer, llvm_pi8, "")
|
||||
.map(BasicValueEnum::into_pointer_value)
|
||||
.unwrap();
|
||||
let buffer = ArraySliceValue::from_ptr_val(buffer, buffer_size, None);
|
||||
|
||||
// The first call to `rpc_recv` reads the top-level ndarray object: [pdata, shape]
|
||||
//
|
||||
// The returned value is the number of bytes for `ndarray.data`.
|
||||
let ndarray_nbytes = ctx
|
||||
.build_call_or_invoke(
|
||||
rpc_recv,
|
||||
&[buffer.base_ptr(ctx, generator).into()], // Reads [usize; ndims]. NOTE: We are allocated [size_t; ndims].
|
||||
"rpc.size.next",
|
||||
)
|
||||
.map(BasicValueEnum::into_int_value)
|
||||
.unwrap();
|
||||
|
||||
// debug_assert(ndarray_nbytes > 0)
|
||||
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
|
||||
ctx.make_assert(
|
||||
generator,
|
||||
ctx.builder
|
||||
.build_int_compare(
|
||||
IntPredicate::UGT,
|
||||
ndarray_nbytes,
|
||||
ndarray_nbytes.get_type().const_zero(),
|
||||
"",
|
||||
)
|
||||
.unwrap(),
|
||||
"0:AssertionError",
|
||||
"Unexpected RPC termination for ndarray - Expected data buffer next",
|
||||
[None, None, None],
|
||||
ctx.current_loc,
|
||||
);
|
||||
}
|
||||
|
||||
// Copy shape from the buffer to `ndarray.shape`.
|
||||
let pbuffer_dims =
|
||||
unsafe { buffer.ptr_offset_unchecked(ctx, generator, &llvm_pdata_sizeof, None) };
|
||||
|
||||
call_memcpy_generic(
|
||||
ctx,
|
||||
ndarray.dim_sizes().base_ptr(ctx, generator),
|
||||
pbuffer_dims,
|
||||
sizeof_dims,
|
||||
llvm_i1.const_zero(),
|
||||
);
|
||||
// Restore stack from before allocation of buffer
|
||||
call_stackrestore(ctx, stackptr);
|
||||
|
||||
// Allocate `ndarray.data`.
|
||||
// `ndarray.shape` must be initialized beforehand in this implementation
|
||||
// (for ndarray.create_data() to know how many elements to allocate)
|
||||
let num_elements =
|
||||
call_ndarray_calc_size(generator, ctx, &ndarray.dim_sizes(), (None, None));
|
||||
|
||||
// debug_assert(nelems * sizeof(T) >= ndarray_nbytes)
|
||||
if ctx.registry.llvm_options.opt_level == OptimizationLevel::None {
|
||||
let sizeof_data =
|
||||
ctx.builder.build_int_mul(num_elements, llvm_elem_sizeof, "").unwrap();
|
||||
|
||||
ctx.make_assert(
|
||||
generator,
|
||||
ctx.builder.build_int_compare(IntPredicate::UGE,
|
||||
sizeof_data,
|
||||
ndarray_nbytes,
|
||||
"",
|
||||
).unwrap(),
|
||||
"0:AssertionError",
|
||||
"Unexpected allocation size request for ndarray data - Expected up to {0} bytes, got {1} bytes",
|
||||
[Some(sizeof_data), Some(ndarray_nbytes), None],
|
||||
ctx.current_loc,
|
||||
);
|
||||
}
|
||||
|
||||
ndarray.create_data(ctx, llvm_elem_ty, num_elements);
|
||||
|
||||
let ndarray_data = ndarray.data().base_ptr(ctx, generator);
|
||||
let ndarray_data_i8 =
|
||||
ctx.builder.build_pointer_cast(ndarray_data, llvm_pi8, "").unwrap();
|
||||
|
||||
// NOTE: Currently on `prehead_bb`
|
||||
ctx.builder.build_unconditional_branch(head_bb).unwrap();
|
||||
|
||||
// Inserting into `head_bb`. Do `rpc_recv` for `data` recursively.
|
||||
ctx.builder.position_at_end(head_bb);
|
||||
|
||||
let phi = ctx.builder.build_phi(llvm_pi8, "rpc.ptr").unwrap();
|
||||
phi.add_incoming(&[(&ndarray_data_i8, prehead_bb)]);
|
||||
|
||||
let alloc_size = ctx
|
||||
.build_call_or_invoke(rpc_recv, &[phi.as_basic_value()], "rpc.size.next")
|
||||
.map(BasicValueEnum::into_int_value)
|
||||
.unwrap();
|
||||
|
||||
let is_done = ctx
|
||||
.builder
|
||||
.build_int_compare(IntPredicate::EQ, llvm_i32.const_zero(), alloc_size, "rpc.done")
|
||||
.unwrap();
|
||||
ctx.builder.build_conditional_branch(is_done, tail_bb, alloc_bb).unwrap();
|
||||
|
||||
ctx.builder.position_at_end(alloc_bb);
|
||||
// Align the allocation to sizeof(T)
|
||||
let alloc_size = round_up(ctx, alloc_size, llvm_elem_sizeof);
|
||||
let alloc_ptr = ctx
|
||||
.builder
|
||||
.build_array_alloca(
|
||||
llvm_elem_ty,
|
||||
ctx.builder.build_int_unsigned_div(alloc_size, llvm_elem_sizeof, "").unwrap(),
|
||||
"rpc.alloc",
|
||||
)
|
||||
.unwrap();
|
||||
let alloc_ptr =
|
||||
ctx.builder.build_pointer_cast(alloc_ptr, llvm_pi8, "rpc.alloc.ptr").unwrap();
|
||||
phi.add_incoming(&[(&alloc_ptr, alloc_bb)]);
|
||||
ctx.builder.build_unconditional_branch(head_bb).unwrap();
|
||||
|
||||
ctx.builder.position_at_end(tail_bb);
|
||||
ndarray.as_base_value().into()
|
||||
}
|
||||
|
||||
_ => {
|
||||
let slot = ctx.builder.build_alloca(llvm_ret_ty, "rpc.ret.slot").unwrap();
|
||||
let slotgen = ctx.builder.build_bitcast(slot, llvm_pi8, "rpc.ret.ptr").unwrap();
|
||||
ctx.builder.build_unconditional_branch(head_bb).unwrap();
|
||||
ctx.builder.position_at_end(head_bb);
|
||||
|
||||
let phi = ctx.builder.build_phi(llvm_pi8, "rpc.ptr").unwrap();
|
||||
phi.add_incoming(&[(&slotgen, prehead_bb)]);
|
||||
let alloc_size = ctx
|
||||
.build_call_or_invoke(rpc_recv, &[phi.as_basic_value()], "rpc.size.next")
|
||||
.unwrap()
|
||||
.into_int_value();
|
||||
let is_done = ctx
|
||||
.builder
|
||||
.build_int_compare(IntPredicate::EQ, llvm_i32.const_zero(), alloc_size, "rpc.done")
|
||||
.unwrap();
|
||||
|
||||
ctx.builder.build_conditional_branch(is_done, tail_bb, alloc_bb).unwrap();
|
||||
ctx.builder.position_at_end(alloc_bb);
|
||||
|
||||
let alloc_ptr =
|
||||
ctx.builder.build_array_alloca(llvm_pi8, alloc_size, "rpc.alloc").unwrap();
|
||||
let alloc_ptr =
|
||||
ctx.builder.build_bitcast(alloc_ptr, llvm_pi8, "rpc.alloc.ptr").unwrap();
|
||||
phi.add_incoming(&[(&alloc_ptr, alloc_bb)]);
|
||||
ctx.builder.build_unconditional_branch(head_bb).unwrap();
|
||||
|
||||
ctx.builder.position_at_end(tail_bb);
|
||||
ctx.builder.build_load(slot, "rpc.result").unwrap()
|
||||
}
|
||||
};
|
||||
|
||||
Some(result)
|
||||
}
|
||||
|
||||
fn rpc_codegen_callback_fn<'ctx>(
|
||||
ctx: &mut CodeGenContext<'ctx, '_>,
|
||||
obj: Option<(Type, ValueEnum<'ctx>)>,
|
||||
|
@ -953,14 +663,63 @@ fn rpc_codegen_callback_fn<'ctx>(
|
|||
// reclaim stack space used by arguments
|
||||
call_stackrestore(ctx, stackptr);
|
||||
|
||||
let result = format_rpc_ret(generator, ctx, fun.0.ret);
|
||||
// -- receive value:
|
||||
// T result = {
|
||||
// void *ret_ptr = alloca(sizeof(T));
|
||||
// void *ptr = ret_ptr;
|
||||
// loop: int size = rpc_recv(ptr);
|
||||
// // Non-zero: Provide `size` bytes of extra storage for variable-length data.
|
||||
// if(size) { ptr = alloca(size); goto loop; }
|
||||
// else *(T*)ret_ptr
|
||||
// }
|
||||
let rpc_recv = ctx.module.get_function("rpc_recv").unwrap_or_else(|| {
|
||||
ctx.module.add_function("rpc_recv", int32.fn_type(&[ptr_type.into()], false), None)
|
||||
});
|
||||
|
||||
if !result.is_some_and(|res| res.get_type().is_pointer_type()) {
|
||||
// An RPC returning an NDArray would not touch here.
|
||||
call_stackrestore(ctx, stackptr);
|
||||
if ctx.unifier.unioned(fun.0.ret, ctx.primitives.none) {
|
||||
ctx.build_call_or_invoke(rpc_recv, &[ptr_type.const_null().into()], "rpc_recv");
|
||||
return Ok(None);
|
||||
}
|
||||
|
||||
Ok(result)
|
||||
let prehead_bb = ctx.builder.get_insert_block().unwrap();
|
||||
let current_function = prehead_bb.get_parent().unwrap();
|
||||
let head_bb = ctx.ctx.append_basic_block(current_function, "rpc.head");
|
||||
let alloc_bb = ctx.ctx.append_basic_block(current_function, "rpc.continue");
|
||||
let tail_bb = ctx.ctx.append_basic_block(current_function, "rpc.tail");
|
||||
|
||||
let ret_ty = ctx.get_llvm_abi_type(generator, fun.0.ret);
|
||||
let need_load = !ret_ty.is_pointer_type();
|
||||
let slot = ctx.builder.build_alloca(ret_ty, "rpc.ret.slot").unwrap();
|
||||
let slotgen = ctx.builder.build_bitcast(slot, ptr_type, "rpc.ret.ptr").unwrap();
|
||||
ctx.builder.build_unconditional_branch(head_bb).unwrap();
|
||||
ctx.builder.position_at_end(head_bb);
|
||||
|
||||
let phi = ctx.builder.build_phi(ptr_type, "rpc.ptr").unwrap();
|
||||
phi.add_incoming(&[(&slotgen, prehead_bb)]);
|
||||
let alloc_size = ctx
|
||||
.build_call_or_invoke(rpc_recv, &[phi.as_basic_value()], "rpc.size.next")
|
||||
.unwrap()
|
||||
.into_int_value();
|
||||
let is_done = ctx
|
||||
.builder
|
||||
.build_int_compare(inkwell::IntPredicate::EQ, int32.const_zero(), alloc_size, "rpc.done")
|
||||
.unwrap();
|
||||
|
||||
ctx.builder.build_conditional_branch(is_done, tail_bb, alloc_bb).unwrap();
|
||||
ctx.builder.position_at_end(alloc_bb);
|
||||
|
||||
let alloc_ptr = ctx.builder.build_array_alloca(ptr_type, alloc_size, "rpc.alloc").unwrap();
|
||||
let alloc_ptr = ctx.builder.build_bitcast(alloc_ptr, ptr_type, "rpc.alloc.ptr").unwrap();
|
||||
phi.add_incoming(&[(&alloc_ptr, alloc_bb)]);
|
||||
ctx.builder.build_unconditional_branch(head_bb).unwrap();
|
||||
|
||||
ctx.builder.position_at_end(tail_bb);
|
||||
|
||||
let result = ctx.builder.build_load(slot, "rpc.result").unwrap();
|
||||
if need_load {
|
||||
call_stackrestore(ctx, stackptr);
|
||||
}
|
||||
Ok(Some(result))
|
||||
}
|
||||
|
||||
pub fn attributes_writeback(
|
||||
|
|
|
@ -1404,7 +1404,7 @@ impl<'ctx> NDArrayValue<'ctx> {
|
|||
|
||||
/// Returns the double-indirection pointer to the `data` array, as if by calling `getelementptr`
|
||||
/// on the field.
|
||||
pub fn ptr_to_data(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
||||
fn ptr_to_data(&self, ctx: &CodeGenContext<'ctx, '_>) -> PointerValue<'ctx> {
|
||||
let llvm_i32 = ctx.ctx.i32_type();
|
||||
let var_name = self.name.map(|v| format!("{v}.data.addr")).unwrap_or_default();
|
||||
|
||||
|
|
|
@ -2385,10 +2385,7 @@ pub fn gen_cmpop_expr_with_values<'ctx, G: CodeGenerator>(
|
|||
})
|
||||
.map(BasicValueEnum::into_int_value)?;
|
||||
|
||||
Ok(ctx.builder.build_not(
|
||||
generator.bool_to_i1(ctx, cmp),
|
||||
"",
|
||||
).unwrap())
|
||||
Ok(ctx.builder.build_not(cmp, "").unwrap())
|
||||
},
|
||||
|_, ctx| {
|
||||
let bb = ctx.builder.get_insert_block().unwrap();
|
||||
|
|
|
@ -174,14 +174,6 @@ pub fn gen_assign<'ctx, G: CodeGenerator>(
|
|||
}
|
||||
}
|
||||
let val = value.to_basic_value_enum(ctx, generator, target.custom.unwrap())?;
|
||||
|
||||
// Perform i1 <-> i8 conversion as needed
|
||||
let val = if ctx.unifier.unioned(target.custom.unwrap(), ctx.primitives.bool) {
|
||||
generator.bool_to_i8(ctx, val.into_int_value()).into()
|
||||
} else {
|
||||
val
|
||||
};
|
||||
|
||||
ctx.builder.build_store(ptr, val).unwrap();
|
||||
}
|
||||
};
|
||||
|
@ -1673,23 +1665,6 @@ pub fn gen_return<G: CodeGenerator>(
|
|||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
// Remap boolean return type into i1
|
||||
let value = value.map(|ret_val| {
|
||||
// The "return type" of a sret function is in the first parameter
|
||||
let expected_ty = if ctx.need_sret {
|
||||
func.get_type().get_param_types()[0]
|
||||
} else {
|
||||
func.get_type().get_return_type().unwrap()
|
||||
};
|
||||
|
||||
if matches!(expected_ty, BasicTypeEnum::IntType(ty) if ty.get_bit_width() == 1) {
|
||||
generator.bool_to_i1(ctx, ret_val.into_int_value()).into()
|
||||
} else {
|
||||
ret_val
|
||||
}
|
||||
});
|
||||
|
||||
if let Some(return_target) = ctx.return_target {
|
||||
if let Some(value) = value {
|
||||
ctx.builder.build_store(ctx.return_buffer.unwrap(), value).unwrap();
|
||||
|
@ -1700,6 +1675,25 @@ pub fn gen_return<G: CodeGenerator>(
|
|||
ctx.builder.build_store(ctx.return_buffer.unwrap(), value.unwrap()).unwrap();
|
||||
ctx.builder.build_return(None).unwrap();
|
||||
} else {
|
||||
// Remap boolean return type into i1
|
||||
let value = value.map(|v| {
|
||||
let expected_ty = func.get_type().get_return_type().unwrap();
|
||||
let ret_val = v.as_basic_value_enum();
|
||||
|
||||
if expected_ty.is_int_type() && ret_val.is_int_value() {
|
||||
let ret_type = expected_ty.into_int_type();
|
||||
let ret_val = ret_val.into_int_value();
|
||||
|
||||
if ret_type.get_bit_width() == 1 && ret_val.get_type().get_bit_width() != 1 {
|
||||
generator.bool_to_i1(ctx, ret_val)
|
||||
} else {
|
||||
ret_val
|
||||
}
|
||||
.into()
|
||||
} else {
|
||||
ret_val
|
||||
}
|
||||
});
|
||||
let value = value.as_ref().map(|v| v as &dyn BasicValue);
|
||||
ctx.builder.build_return(value).unwrap();
|
||||
}
|
||||
|
|
|
@ -439,7 +439,12 @@ pub fn parse_type_annotation<T>(
|
|||
} else {
|
||||
let obj_id = resolver.get_identifier_def(*id);
|
||||
if let Ok(obj_id) = obj_id {
|
||||
let def = top_level_defs[obj_id.0].read();
|
||||
let Some(top_level_def) = top_level_defs.get(obj_id.0) else {
|
||||
return Err(HashSet::from([format!(
|
||||
"NameError: '{id}' not found at {}", expr.location
|
||||
)]));
|
||||
};
|
||||
let def = top_level_def.read();
|
||||
if let TopLevelDef::Class { fields, methods, type_vars, .. } = &*def {
|
||||
if !type_vars.is_empty() {
|
||||
return Err(HashSet::from([format!(
|
||||
|
|
|
@ -52,6 +52,10 @@ pub type BuiltinFuncSpec = (StrRef, FunSignature, Arc<GenCall>);
|
|||
/// [`Unifier`].
|
||||
pub type BuiltinFuncCreator = dyn Fn(&PrimitiveStore, &mut Unifier) -> BuiltinFuncSpec;
|
||||
|
||||
fn report_error<T>(msg: &str, location: Location) -> Result<T, HashSet<String>> {
|
||||
Err(HashSet::from([format!("{msg} at {location}")]))
|
||||
}
|
||||
|
||||
impl TopLevelComposer {
|
||||
/// return a composer and things to make a "primitive" symbol resolver, so that the symbol
|
||||
/// resolver can later figure out primitive tye definitions when passed a primitive type name
|
||||
|
@ -389,8 +393,21 @@ impl TopLevelComposer {
|
|||
}
|
||||
|
||||
pub fn start_analysis(&mut self, inference: bool) -> Result<(), HashSet<String>> {
|
||||
self.analyze_top_level_class_type_var()?;
|
||||
self.analyze_top_level_class_bases()?;
|
||||
let unifier = self.unifier.borrow_mut();
|
||||
let primitives_store = &self.primitives_ty;
|
||||
let def_list = &self.definition_ast_list;
|
||||
// let class_def = def_list.iter().filter(|f| matches!(&*f.0.read(), TopLevelDef::Class { .. })).collect_vec();
|
||||
|
||||
|
||||
Self::analyze_top_level_class_bases(
|
||||
def_list,
|
||||
unifier,
|
||||
primitives_store,
|
||||
self.builtin_num,
|
||||
)?;
|
||||
|
||||
// self.analyze_top_level_class_type_var()?;
|
||||
self.analyze_top_level_class_bases2()?;
|
||||
self.analyze_top_level_class_fields_methods()?;
|
||||
self.analyze_top_level_function()?;
|
||||
if inference {
|
||||
|
@ -399,6 +416,206 @@ impl TopLevelComposer {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
fn analyze_bases(
|
||||
class_def: &Arc<RwLock<TopLevelDef>>,
|
||||
class_ast: &Option<Stmt>,
|
||||
temp_def_list:&[Arc<RwLock<TopLevelDef>>],
|
||||
unifier: &mut Unifier,
|
||||
primitives_store: &PrimitiveStore,
|
||||
) -> Result<(), HashSet<String>> {
|
||||
let mut class_def = class_def.write();
|
||||
let (class_def_id, class_ancestors, class_bases_ast, class_type_vars, class_resolver) = {
|
||||
let TopLevelDef::Class { object_id, ancestors, type_vars, resolver, .. } =
|
||||
&mut *class_def
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
let Some(ast::Located { node: ast::StmtKind::ClassDef { bases, .. }, .. }) = class_ast
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
(object_id, ancestors, bases, type_vars, resolver.as_ref().unwrap().as_ref())
|
||||
};
|
||||
|
||||
let mut is_generic = false;
|
||||
let mut has_base = false;
|
||||
// Check class bases for typevars
|
||||
for b in class_bases_ast {
|
||||
match &b.node {
|
||||
// analyze typevars bounded to the class,
|
||||
// only support things like `class A(Generic[T, V])`,
|
||||
// things like `class A(Generic[T, V, ImportedModule.T])` is not supported
|
||||
// i.e. only simple names are allowed in the subscript
|
||||
// should update the TopLevelDef::Class.typevars and the TypeEnum::TObj.params
|
||||
ast::ExprKind::Subscript { value, slice, .. } if matches!(&value.node, ast::ExprKind::Name { id, .. } if id == &"Generic".into()) =>
|
||||
{
|
||||
if is_generic {
|
||||
return report_error("only single Generic[...] is allowed", b.location);
|
||||
}
|
||||
is_generic = true;
|
||||
|
||||
let type_var_list: Vec<&ast::Expr<()>>;
|
||||
// if `class A(Generic[T, V, G])`
|
||||
if let ast::ExprKind::Tuple { elts, .. } = &slice.node {
|
||||
type_var_list = elts.iter().collect_vec();
|
||||
// `class A(Generic[T])`
|
||||
} else {
|
||||
type_var_list = vec![&**slice];
|
||||
}
|
||||
|
||||
let type_vars = type_var_list
|
||||
.into_iter()
|
||||
.map(|e| {
|
||||
class_resolver.parse_type_annotation(
|
||||
&temp_def_list,
|
||||
unifier,
|
||||
primitives_store,
|
||||
e,
|
||||
)
|
||||
})
|
||||
.collect::<Result<Vec<_>, _>>()?;
|
||||
|
||||
class_type_vars.extend(type_vars);
|
||||
}
|
||||
ast::ExprKind::Name { .. } | ast::ExprKind::Subscript { .. } => {
|
||||
if has_base {
|
||||
return report_error("a class definition can only have at most one base class declaration and one generic declaration", b.location);
|
||||
}
|
||||
has_base = true;
|
||||
// the function parse_ast_to make sure that no type var occurred in
|
||||
// bast_ty if it is a CustomClassKind
|
||||
let base_ty = parse_ast_to_type_annotation_kinds(
|
||||
class_resolver,
|
||||
temp_def_list,
|
||||
unifier,
|
||||
primitives_store,
|
||||
b,
|
||||
vec![(*class_def_id, class_type_vars.clone())]
|
||||
.into_iter()
|
||||
.collect::<HashMap<_, _>>(),
|
||||
)?;
|
||||
if let TypeAnnotation::CustomClass { .. } = &base_ty {
|
||||
class_ancestors.push(base_ty);
|
||||
} else {
|
||||
return report_error(
|
||||
"class base declaration can only be custom class",
|
||||
b.location,
|
||||
);
|
||||
}
|
||||
}
|
||||
// TODO: Report Error here
|
||||
_ => {
|
||||
println!("Type was => {}", b.node.name());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn analyze_ancestors(
|
||||
class_def: &Arc<RwLock<TopLevelDef>>,
|
||||
temp_def_list: &[Arc<RwLock<TopLevelDef>>],
|
||||
unifier: &mut Unifier,
|
||||
) {
|
||||
// Check if class has a direct parent
|
||||
{
|
||||
let mut class_def = class_def.write();
|
||||
let TopLevelDef::Class { ancestors, type_vars, object_id, .. } = &mut *class_def else {
|
||||
unreachable!()
|
||||
};
|
||||
println!("Type Vars: {:?}", type_vars);
|
||||
let mut anc_set = HashMap::new();
|
||||
|
||||
|
||||
if let Some(ancestor) = ancestors.first() {
|
||||
let TypeAnnotation::CustomClass { id, .. } = ancestor else { unreachable!() };
|
||||
let TopLevelDef::Class { ancestors: parent_ancestors, .. } =
|
||||
&*temp_def_list[id.0].read()
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
for anc in parent_ancestors.iter().skip(1) {
|
||||
let TypeAnnotation::CustomClass { id, .. } = anc else { unreachable!() };
|
||||
anc_set.insert(id, anc.clone());
|
||||
}
|
||||
ancestors.extend(anc_set.into_iter().map(|f| f.1).collect::<Vec<_>>());
|
||||
|
||||
}
|
||||
ancestors.insert(0, make_self_type_annotation(type_vars.as_slice(), *object_id));
|
||||
|
||||
}
|
||||
let class_def = class_def.read();
|
||||
println!("{:?}", class_def.to_string(unifier));
|
||||
|
||||
}
|
||||
|
||||
fn analyze_top_level_class_bases(
|
||||
def_list: &[DefAst],
|
||||
unifier: &mut Unifier,
|
||||
primitives_store: &PrimitiveStore,
|
||||
builtin_num: usize,
|
||||
) -> Result<(), HashSet<String>> {
|
||||
let mut errors = HashSet::new();
|
||||
let mut temp_def_list: Vec<Arc<RwLock<TopLevelDef>>> = def_list.iter().take(builtin_num).map(|f| f.0.clone()).collect_vec();
|
||||
|
||||
// Analyze class_bases
|
||||
for (class_def, class_ast) in def_list.iter().skip(builtin_num) {
|
||||
if class_ast.is_some() && matches!(&*class_def.read(), TopLevelDef::Class { .. }) {
|
||||
println!("Class Bases Analysis start");
|
||||
if let Err(e) = Self::analyze_bases(
|
||||
class_def,
|
||||
class_ast,
|
||||
&temp_def_list,
|
||||
unifier,
|
||||
primitives_store,
|
||||
) {
|
||||
errors.extend(e);
|
||||
}
|
||||
// Self::analyze_ancestors(class_def, &temp_def_list, unifier);
|
||||
|
||||
// special case classes that inherit from Exception
|
||||
let TopLevelDef::Class { ancestors: class_ancestors, loc, .. } = &*class_def.read()
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
if class_ancestors
|
||||
.iter()
|
||||
.any(|ann| matches!(ann, TypeAnnotation::CustomClass { id, .. } if id.0 == 7))
|
||||
{
|
||||
// if inherited from Exception, the body should be a pass
|
||||
let ast::StmtKind::ClassDef { body, .. } = &class_ast.as_ref().unwrap().node
|
||||
else {
|
||||
unreachable!()
|
||||
};
|
||||
for stmt in body {
|
||||
if matches!(
|
||||
stmt.node,
|
||||
ast::StmtKind::FunctionDef { .. } | ast::StmtKind::AnnAssign { .. }
|
||||
) {
|
||||
errors.extend(report_error("Classes inherited from exception should have no custom fields/methods", loc.unwrap()));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
temp_def_list.push(class_def.clone());
|
||||
}
|
||||
println!("Bases Analyzed");
|
||||
if !errors.is_empty() {
|
||||
return Err(errors)
|
||||
}
|
||||
println!("Startgin Ancestor Analysis");
|
||||
for (class_def, class_ast) in def_list.iter().skip(builtin_num) {
|
||||
if class_ast.is_some() && matches!(&*class_def.read(), TopLevelDef::Class { .. }) {
|
||||
// Self::analyze_ancestors(class_def, &temp_def_list);
|
||||
}
|
||||
}
|
||||
println!("Ancestors added");
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// step 1, analyze the type vars associated with top level class
|
||||
fn analyze_top_level_class_type_var(&mut self) -> Result<(), HashSet<String>> {
|
||||
let def_list = &self.definition_ast_list;
|
||||
|
@ -509,6 +726,13 @@ impl TopLevelComposer {
|
|||
errors.extend(e);
|
||||
}
|
||||
}
|
||||
for (def, _) in def_list.iter().skip(self.builtin_num){
|
||||
let def = &*def.read();
|
||||
println!("{}", format!("{}\n", def.to_string(unifier.borrow_mut())))
|
||||
}
|
||||
|
||||
|
||||
|
||||
if !errors.is_empty() {
|
||||
return Err(errors);
|
||||
}
|
||||
|
@ -518,7 +742,7 @@ impl TopLevelComposer {
|
|||
/// step 2, base classes.
|
||||
/// now that the type vars of all classes are done, handle base classes and
|
||||
/// put Self class into the ancestors list. We only allow single inheritance
|
||||
fn analyze_top_level_class_bases(&mut self) -> Result<(), HashSet<String>> {
|
||||
fn analyze_top_level_class_bases2(&mut self) -> Result<(), HashSet<String>> {
|
||||
if self.unifier.top_level.is_none() {
|
||||
let ctx = Arc::new(self.make_top_level_context());
|
||||
self.unifier.top_level = Some(ctx);
|
||||
|
@ -605,9 +829,9 @@ impl TopLevelComposer {
|
|||
if class_ast.is_none() {
|
||||
continue;
|
||||
}
|
||||
if let Err(e) = get_direct_parents(class_def, class_ast) {
|
||||
errors.extend(e);
|
||||
}
|
||||
// if let Err(e) = get_direct_parents(class_def, class_ast) {
|
||||
// errors.extend(e);
|
||||
// }
|
||||
}
|
||||
if !errors.is_empty() {
|
||||
return Err(errors);
|
||||
|
@ -657,6 +881,7 @@ impl TopLevelComposer {
|
|||
continue;
|
||||
}
|
||||
let mut class_def = class_def.write();
|
||||
println!("1) {}", class_def.to_string(unifier));
|
||||
let (class_ancestors, class_id, class_type_vars) = {
|
||||
if let TopLevelDef::Class { ancestors, object_id, type_vars, .. } = &mut *class_def
|
||||
{
|
||||
|
@ -669,6 +894,8 @@ impl TopLevelComposer {
|
|||
let ans = ancestors_store.get_mut(&class_id).unwrap();
|
||||
class_ancestors.append(ans);
|
||||
|
||||
// println!("1) {}", class_def.to_string(unifier));
|
||||
|
||||
// insert self type annotation to the front of the vector to maintain the order
|
||||
class_ancestors
|
||||
.insert(0, make_self_type_annotation(class_type_vars.as_slice(), class_id));
|
||||
|
@ -697,6 +924,14 @@ impl TopLevelComposer {
|
|||
}
|
||||
}
|
||||
|
||||
println!("\n\nSecond Phase:\n");
|
||||
for (def, _) in self.definition_ast_list.iter().skip(self.builtin_num){
|
||||
if matches!(&*def.read(), TopLevelDef::Class{..}) {
|
||||
let class_def = def.read();
|
||||
println!("{:?}", class_def.to_string(unifier));
|
||||
}
|
||||
}
|
||||
|
||||
// deal with ancestor of Exception object
|
||||
let TopLevelDef::Class { name, ancestors, object_id, .. } =
|
||||
&mut *self.definition_ast_list[7].0.write()
|
||||
|
|
|
@ -0,0 +1,15 @@
|
|||
---
|
||||
source: nac3core/src/toplevel/test.rs
|
||||
assertion_line: 576
|
||||
expression: res_vec
|
||||
|
||||
---
|
||||
[
|
||||
"Function {\nname: \"foo\",\nsig: \"fn[[a:11[0], b:tuple[T, 2]], 105[109, 3]]\",\nvar_id: []\n}\n",
|
||||
"Class {\nname: \"A\",\nancestors: [\"class_def_105[T, V]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[v:V], 4]\"), (\"fun\", \"fn[[a:T], V]\")],\ntype_vars: [\"T\", \"V\"]\n}\n",
|
||||
"Function {\nname: \"A.__init__\",\nsig: \"fn[[v:V], 4]\",\nvar_id: [TypeVarId(243)]\n}\n",
|
||||
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:T], V]\",\nvar_id: [TypeVarId(248)]\n}\n",
|
||||
"Function {\nname: \"gfun\",\nsig: \"fn[[a:105[11[2], 0]], 4]\",\nvar_id: []\n}\n",
|
||||
"Class {\nname: \"B\",\nancestors: [\"class_def_109\"],\nfields: [],\nmethods: [(\"__init__\", \"fn[[], 4]\")],\ntype_vars: []\n}\n",
|
||||
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], 4]\",\nvar_id: []\n}\n",
|
||||
]
|
|
@ -0,0 +1,15 @@
|
|||
---
|
||||
source: nac3core/src/toplevel/test.rs
|
||||
assertion_line: 576
|
||||
expression: res_vec
|
||||
|
||||
---
|
||||
[
|
||||
"Class {\nname: \"A\",\nancestors: [\"class_def_104[typevar229, typevar230]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[a:104[2, 3], b:107], 4]\"), (\"fun\", \"fn[[a:104[2, 3]], 104[3, 0]]\")],\ntype_vars: [\"typevar229\", \"typevar230\"]\n}\n",
|
||||
"Function {\nname: \"A.__init__\",\nsig: \"fn[[a:104[2, 3], b:107], 4]\",\nvar_id: []\n}\n",
|
||||
"Function {\nname: \"A.fun\",\nsig: \"fn[[a:104[2, 3]], 104[3, 0]]\",\nvar_id: []\n}\n",
|
||||
"Class {\nname: \"B\",\nancestors: [\"class_def_107\", \"class_def_104[1, 3]\"],\nfields: [\"a\", \"b\"],\nmethods: [(\"__init__\", \"fn[[], 4]\"), (\"fun\", \"fn[[a:104[2, 3]], 104[3, 0]]\"), (\"foo\", \"fn[[b:107], 107]\"), (\"bar\", \"fn[[a:104[11[107], 0]], tuple[104[virtual[104[107, 0]], 3], 107]]\")],\ntype_vars: []\n}\n",
|
||||
"Function {\nname: \"B.__init__\",\nsig: \"fn[[], 4]\",\nvar_id: []\n}\n",
|
||||
"Function {\nname: \"B.foo\",\nsig: \"fn[[b:107], 107]\",\nvar_id: []\n}\n",
|
||||
"Function {\nname: \"B.bar\",\nsig: \"fn[[a:104[11[107], 0]], tuple[104[virtual[104[107, 0]], 3], 107]]\",\nvar_id: []\n}\n",
|
||||
]
|
|
@ -0,0 +1,9 @@
|
|||
---
|
||||
source: nac3core/src/toplevel/test.rs
|
||||
assertion_line: 576
|
||||
expression: res_vec
|
||||
|
||||
---
|
||||
[
|
||||
"Class {\nname: \"A\",\nancestors: [\"class_def_104\"],\nfields: [],\nmethods: [],\ntype_vars: []\n}\n",
|
||||
]
|
|
@ -214,119 +214,119 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
|
|||
}
|
||||
}
|
||||
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A():
|
||||
a: int32
|
||||
def __init__(self):
|
||||
self.a = 3
|
||||
def fun(self, b: B):
|
||||
pass
|
||||
def foo(self, a: T, b: V):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B(C):
|
||||
def __init__(self):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class C(A):
|
||||
def __init__(self):
|
||||
pass
|
||||
def fun(self, b: B):
|
||||
a = 1
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
def foo(a: A):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
def ff(a: T) -> V:
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&[];
|
||||
"simple class compose"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class Generic_A(Generic[V], B):
|
||||
a: int64
|
||||
def __init__(self):
|
||||
self.a = 123123123123
|
||||
def fun(self, a: int32) -> V:
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B:
|
||||
aa: bool
|
||||
def __init__(self):
|
||||
self.aa = False
|
||||
def foo(self, b: T):
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&[];
|
||||
"generic class"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
def foo(a: list[int32], b: tuple[T, float]) -> A[B, bool]:
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class A(Generic[T, V]):
|
||||
a: T
|
||||
b: V
|
||||
def __init__(self, v: V):
|
||||
self.a = 1
|
||||
self.b = v
|
||||
def fun(self, a: T) -> V:
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
def gfun(a: A[list[float], int32]):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B:
|
||||
def __init__(self):
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&[];
|
||||
"list tuple generic"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A(Generic[T, V]):
|
||||
a: A[float, bool]
|
||||
b: B
|
||||
def __init__(self, a: A[float, bool], b: B):
|
||||
self.a = a
|
||||
self.b = b
|
||||
def fun(self, a: A[float, bool]) -> A[bool, int32]:
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B(A[int64, bool]):
|
||||
def __init__(self):
|
||||
pass
|
||||
def foo(self, b: B) -> B:
|
||||
pass
|
||||
def bar(self, a: A[list[B], int32]) -> tuple[A[virtual[A[B, int32]], bool], B]:
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&[];
|
||||
"self1"
|
||||
)]
|
||||
// #[test_case(
|
||||
// &[
|
||||
// indoc! {"
|
||||
// class A():
|
||||
// a: int32
|
||||
// def __init__(self):
|
||||
// self.a = 3
|
||||
// def fun(self, b: B):
|
||||
// pass
|
||||
// def foo(self, a: T, b: V):
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// class B(C):
|
||||
// def __init__(self):
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// class C(A):
|
||||
// def __init__(self):
|
||||
// pass
|
||||
// def fun(self, b: B):
|
||||
// a = 1
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// def foo(a: A):
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// def ff(a: T) -> V:
|
||||
// pass
|
||||
// "}
|
||||
// ],
|
||||
// &[];
|
||||
// "simple class compose"
|
||||
// )]
|
||||
// #[test_case(
|
||||
// &[
|
||||
// indoc! {"
|
||||
// class Generic_A(Generic[V], B):
|
||||
// a: int64
|
||||
// def __init__(self):
|
||||
// self.a = 123123123123
|
||||
// def fun(self, a: int32) -> V:
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// class B:
|
||||
// aa: bool
|
||||
// def __init__(self):
|
||||
// self.aa = False
|
||||
// def foo(self, b: T):
|
||||
// pass
|
||||
// "}
|
||||
// ],
|
||||
// &[];
|
||||
// "generic class"
|
||||
// )]
|
||||
// #[test_case(
|
||||
// &[
|
||||
// indoc! {"
|
||||
// def foo(a: list[int32], b: tuple[T, float]) -> A[B, bool]:
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// class A(Generic[T, V]):
|
||||
// a: T
|
||||
// b: V
|
||||
// def __init__(self, v: V):
|
||||
// self.a = 1
|
||||
// self.b = v
|
||||
// def fun(self, a: T) -> V:
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// def gfun(a: A[list[float], int32]):
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// class B:
|
||||
// def __init__(self):
|
||||
// pass
|
||||
// "}
|
||||
// ],
|
||||
// &[];
|
||||
// "list tuple generic"
|
||||
// )]
|
||||
// #[test_case(
|
||||
// &[
|
||||
// indoc! {"
|
||||
// class A(Generic[T, V]):
|
||||
// a: A[float, bool]
|
||||
// b: B
|
||||
// def __init__(self, a: A[float, bool], b: B):
|
||||
// self.a = a
|
||||
// self.b = b
|
||||
// def fun(self, a: A[float, bool]) -> A[bool, int32]:
|
||||
// pass
|
||||
// "},
|
||||
// indoc! {"
|
||||
// class B(A[int64, bool]):
|
||||
// def __init__(self):
|
||||
// pass
|
||||
// def foo(self, b: B) -> B:
|
||||
// pass
|
||||
// def bar(self, a: A[list[B], int32]) -> tuple[A[virtual[A[B, int32]], bool], B]:
|
||||
// pass
|
||||
// "}
|
||||
// ],
|
||||
// &[];
|
||||
// "self1"
|
||||
// )]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
|
@ -361,167 +361,8 @@ fn test_simple_function_analyze(source: &[&str], tys: &[&str], names: &[&str]) {
|
|||
&[];
|
||||
"inheritance_override"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A(Generic[T]):
|
||||
def __init__(self):
|
||||
pass
|
||||
def fun(self, a: A[T]) -> A[T]:
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&["application of type vars to generic class is not currently supported (at unknown:4:24)"];
|
||||
"err no type var in generic app"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A(B):
|
||||
def __init__(self):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B(A):
|
||||
def __init__(self):
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&["cyclic inheritance detected"];
|
||||
"cyclic1"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A(B[bool, int64]):
|
||||
def __init__(self):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B(Generic[V, T], C[int32]):
|
||||
def __init__(self):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class C(Generic[T], A):
|
||||
def __init__(self):
|
||||
pass
|
||||
"},
|
||||
],
|
||||
&["cyclic inheritance detected"];
|
||||
"cyclic2"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A:
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&["5: Class {\nname: \"A\",\ndef_id: DefinitionId(5),\nancestors: [CustomClassKind { id: DefinitionId(5), params: [] }],\nfields: [],\nmethods: [],\ntype_vars: []\n}"];
|
||||
"simple pass in class"
|
||||
)]
|
||||
#[test_case(
|
||||
&[indoc! {"
|
||||
class A:
|
||||
def __init__():
|
||||
pass
|
||||
"}],
|
||||
&["__init__ method must have a `self` parameter (at unknown:2:5)"];
|
||||
"err no self_1"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A(B, Generic[T], C):
|
||||
def __init__(self):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B:
|
||||
def __init__(self):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class C:
|
||||
def __init__(self):
|
||||
pass
|
||||
"}
|
||||
|
||||
],
|
||||
&["a class definition can only have at most one base class declaration and one generic declaration (at unknown:1:24)"];
|
||||
"err multiple inheritance"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A(Generic[T]):
|
||||
a: int32
|
||||
b: T
|
||||
c: A[int64]
|
||||
def __init__(self, t: T):
|
||||
self.a = 3
|
||||
self.b = T
|
||||
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B(Generic[V], A[float]):
|
||||
def __init__(self):
|
||||
pass
|
||||
def fun(self, a: int32, b: T) -> list[virtual[B[int32]]]:
|
||||
# override
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&["method fun has same name as ancestors' method, but incompatible type"];
|
||||
"err_incompatible_inheritance_method"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A(Generic[T]):
|
||||
a: int32
|
||||
b: T
|
||||
c: A[int64]
|
||||
def __init__(self, t: T):
|
||||
self.a = 3
|
||||
self.b = T
|
||||
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class B(Generic[V], A[float]):
|
||||
a: int32
|
||||
def __init__(self):
|
||||
pass
|
||||
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
|
||||
# override
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&["field `a` has already declared in the ancestor classes"];
|
||||
"err_incompatible_inheritance_field"
|
||||
)]
|
||||
#[test_case(
|
||||
&[
|
||||
indoc! {"
|
||||
class A:
|
||||
def __init__(self):
|
||||
pass
|
||||
"},
|
||||
indoc! {"
|
||||
class A:
|
||||
a: int32
|
||||
def __init__(self):
|
||||
pass
|
||||
"}
|
||||
],
|
||||
&["duplicate definition of class `A` (at unknown:1:1)"];
|
||||
"class same name"
|
||||
)]
|
||||
fn test_analyze(source: &[&str], res: &[&str]) {
|
||||
let print = false;
|
||||
let print = true;
|
||||
let mut composer =
|
||||
TopLevelComposer::new(Vec::new(), Vec::new(), ComposerConfig::default(), 64).0;
|
||||
|
||||
|
@ -545,6 +386,7 @@ fn test_analyze(source: &[&str], res: &[&str]) {
|
|||
match composer.register_top_level(ast, Some(resolver.clone()), "", false) {
|
||||
Ok(x) => x,
|
||||
Err(msg) => {
|
||||
println!("\nError in registry\n\n");
|
||||
if print {
|
||||
println!("{msg}");
|
||||
} else {
|
||||
|
@ -561,6 +403,7 @@ fn test_analyze(source: &[&str], res: &[&str]) {
|
|||
}
|
||||
|
||||
if let Err(msg) = composer.start_analysis(false) {
|
||||
println!("Error message\n");
|
||||
if print {
|
||||
println!("{}", msg.iter().sorted().join("\n----------\n"));
|
||||
} else {
|
||||
|
|
|
@ -97,7 +97,12 @@ pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
|
|||
Ok(TypeAnnotation::CustomClass { id: PrimDef::Exception.id(), params: Vec::default() })
|
||||
} else if let Ok(obj_id) = resolver.get_identifier_def(*id) {
|
||||
let type_vars = {
|
||||
let def_read = top_level_defs[obj_id.0].try_read();
|
||||
let Some(top_level_def) = top_level_defs.get(obj_id.0) else {
|
||||
return Err(HashSet::from([format!(
|
||||
"NameError: '{id}' not found at {}", expr.location
|
||||
)]));
|
||||
};
|
||||
let def_read = top_level_def.try_read();
|
||||
if let Some(def_read) = def_read {
|
||||
if let TopLevelDef::Class { type_vars, .. } = &*def_read {
|
||||
type_vars.clone()
|
||||
|
@ -152,7 +157,12 @@ pub fn parse_ast_to_type_annotation_kinds<T, S: std::hash::BuildHasher + Clone>(
|
|||
}
|
||||
let obj_id = resolver.get_identifier_def(*id)?;
|
||||
let type_vars = {
|
||||
let def_read = top_level_defs[obj_id.0].try_read();
|
||||
let Some(top_level_def) = top_level_defs.get(obj_id.0) else {
|
||||
return Err(HashSet::from([format!(
|
||||
"NameError: '{id}' not found at {}", expr.location
|
||||
)]));
|
||||
};
|
||||
let def_read = top_level_def.try_read();
|
||||
if let Some(def_read) = def_read {
|
||||
let TopLevelDef::Class { type_vars, .. } = &*def_read else {
|
||||
unreachable!("must be class here")
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
from __future__ import annotations
|
||||
T = TypeVar("T")
|
||||
V = TypeVar("V")
|
||||
class A(Generic[T]):
|
||||
a: int32
|
||||
b: T
|
||||
c: A[int64]
|
||||
def __init__(self, t: T):
|
||||
self.a = 3
|
||||
self.b = T
|
||||
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
|
||||
pass
|
||||
def foo(self, c: C):
|
||||
pass
|
||||
|
||||
class B(Generic[V], A[float]):
|
||||
d: C
|
||||
def __init__(self):
|
||||
pass
|
||||
def fun(self, a: int32, b: T) -> list[virtual[B[bool]]]:
|
||||
# override
|
||||
pass
|
||||
|
||||
class C(B[bool]):
|
||||
e: int64
|
||||
def __init__(self):
|
||||
pass
|
Loading…
Reference in New Issue