clarify len to be the number of list elements

This commit is contained in:
Folkert 2020-12-13 19:49:49 +01:00
parent c04c3e551a
commit 30e3f3bd25

View file

@ -477,17 +477,17 @@ pub fn build_exp_literal<'a, 'ctx, 'env>(
} else { } else {
let ctx = env.context; let ctx = env.context;
let builder = env.builder; let builder = env.builder;
let len_u64 = str_literal.len() as u64; let number_of_chars = str_literal.len() as u64;
let elem_bytes = CHAR_LAYOUT.stack_size(env.ptr_bytes) as u64; let elem_bytes = CHAR_LAYOUT.stack_size(env.ptr_bytes) as u64;
let ptr_bytes = env.ptr_bytes; let ptr_bytes = env.ptr_bytes;
let populate_str = |ptr| { let populate_str = |ptr| {
// Copy the elements from the list literal into the array // Copy the elements from the list literal into the array
for (index, char) in str_literal.as_bytes().iter().enumerate() { for (index, character) in str_literal.as_bytes().iter().enumerate() {
let val = env let val = env
.context .context
.i8_type() .i8_type()
.const_int(*char as u64, false) .const_int(*character as u64, false)
.as_basic_value_enum(); .as_basic_value_enum();
let index_val = ctx.i64_type().const_int(index as u64, false); let index_val = ctx.i64_type().const_int(index as u64, false);
let elem_ptr = let elem_ptr =
@ -554,14 +554,14 @@ pub fn build_exp_literal<'a, 'ctx, 'env>(
"small_str_array", "small_str_array",
) )
} else { } else {
let bytes_len = elem_bytes * len_u64; let number_of_elements = env.ptr_int().const_int(number_of_chars, false);
let len_type = env.ptr_int();
let len = len_type.const_int(bytes_len, false);
// NOTE we rely on CHAR_LAYOUT turning into a `i8` // NOTE we rely on CHAR_LAYOUT turning into a `i8`
let ptr = allocate_list(env, InPlace::Clone, &CHAR_LAYOUT, len); let ptr = allocate_list(env, InPlace::Clone, &CHAR_LAYOUT, number_of_elements);
let struct_type = collection(ctx, ptr_bytes); let struct_type = collection(ctx, ptr_bytes);
populate_str(ptr);
let mut struct_val; let mut struct_val;
// Store the pointer // Store the pointer
@ -576,11 +576,14 @@ pub fn build_exp_literal<'a, 'ctx, 'env>(
// Store the length // Store the length
struct_val = builder struct_val = builder
.build_insert_value(struct_val, len, Builtin::WRAPPER_LEN, "insert_len") .build_insert_value(
struct_val,
number_of_elements,
Builtin::WRAPPER_LEN,
"insert_len",
)
.unwrap(); .unwrap();
populate_str(ptr);
builder.build_bitcast( builder.build_bitcast(
struct_val.into_struct_value(), struct_val.into_struct_value(),
collection(ctx, ptr_bytes), collection(ctx, ptr_bytes),