Skip to content

Commit

Permalink
Merge 71fa9f8 into 0b440ea
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj authored Jul 22, 2024
2 parents 0b440ea + 71fa9f8 commit d3d6dc2
Show file tree
Hide file tree
Showing 49 changed files with 2,199 additions and 195 deletions.
784 changes: 784 additions & 0 deletions a.txt

Large diffs are not rendered by default.

12 changes: 12 additions & 0 deletions forc-plugins/forc-doc/src/render/item/type_anchor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,18 @@ pub(crate) fn render_type_anchor(
: format!("; {}]", len.val());
})
}
TypeInfo::Slice(ty_arg) => {
let inner = render_type_anchor(
(*render_plan.engines.te().get(ty_arg.type_id)).clone(),
render_plan,
current_module_info,
)?;
Ok(box_html! {
: "__slice[";
: inner;
: "]";
})
}
TypeInfo::Tuple(ty_args) => {
let mut rendered_args: Vec<_> = Vec::new();
for ty_arg in ty_args {
Expand Down
6 changes: 6 additions & 0 deletions sway-ast/src/intrinsics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ pub enum Intrinsic {
EncodeBufferEmpty, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_empty()
EncodeBufferAppend, // let buffer: (raw_ptr, u64, u64) = __encode_buffer_append(buffer, primitive data type)
EncodeBufferAsRawSlice, // let slice: raw_slice = __encode_buffer_as_raw_slice(buffer)
Slice, // let ref_to_slice = __slice(array, inclusive_start_index, exclusive_end_index)
SliceElem, // let ref_to_item = __slice_elem(ref to slice, index)
}

impl fmt::Display for Intrinsic {
Expand Down Expand Up @@ -85,6 +87,8 @@ impl fmt::Display for Intrinsic {
Intrinsic::EncodeBufferEmpty => "encode_buffer_empty",
Intrinsic::EncodeBufferAppend => "encode_buffer_append",
Intrinsic::EncodeBufferAsRawSlice => "encode_buffer_as_raw_slice",
Intrinsic::Slice => "slice",
Intrinsic::SliceElem => "slice_elem",
};
write!(f, "{s}")
}
Expand Down Expand Up @@ -133,6 +137,8 @@ impl Intrinsic {
"__encode_buffer_empty" => EncodeBufferEmpty,
"__encode_buffer_append" => EncodeBufferAppend,
"__encode_buffer_as_raw_slice" => EncodeBufferAsRawSlice,
"__slice" => Slice,
"__slice_elem" => SliceElem,
_ => return None,
})
}
Expand Down
8 changes: 8 additions & 0 deletions sway-core/src/abi_generation/abi_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,14 @@ impl TypeId {
};
format!("[{}; {}]", inner_type, count.val())
}
(TypeInfo::Slice(type_arg), TypeInfo::Slice(_)) => {
let inner_type = if ctx.abi_with_fully_specified_types {
type_engine.get(type_arg.type_id).abi_str(ctx, engines)
} else {
"_".to_string()
};
format!("[{}]", inner_type)
}
(TypeInfo::Custom { .. }, _) => {
format!("generic {}", self_abi_str)
}
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/abi_generation/evm_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ fn get_type_str(type_id: &TypeId, engines: &Engines, resolved_type_id: TypeId) -
assert_eq!(count.val(), resolved_count.val());
format!("[_; {}]", count.val())
}
(TypeInfo::Slice(_), TypeInfo::Slice(_)) => "__slice[_]".into(),
(TypeInfo::Custom { .. }, _) => {
format!("generic {}", abi_str(&type_engine.get(*type_id), engines))
}
Expand Down
41 changes: 41 additions & 0 deletions sway-core/src/abi_generation/fuel_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -406,6 +406,47 @@ impl TypeId {
unreachable!();
}
}
TypeInfo::Slice(..) => {
if let TypeInfo::Array(elem_ty, _) = &*type_engine.get(resolved_type_id) {
// The `program_abi::TypeDeclaration`s needed for the array element type
let elem_abi_ty = program_abi::TypeDeclaration {
type_id: elem_ty.initial_type_id.index(),
type_field: elem_ty.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines, false),
engines,
elem_ty.type_id,
),
components: elem_ty.initial_type_id.get_abi_type_components(
ctx,
engines,
types,
elem_ty.type_id,
),
type_parameters: elem_ty.initial_type_id.get_abi_type_parameters(
ctx,
engines,
types,
elem_ty.type_id,
),
};
types.push(elem_abi_ty);

// Generate the JSON data for the array. This is basically a single
// `program_abi::TypeApplication` for the array element type
Some(vec![program_abi::TypeApplication {
name: "__slice_element".to_string(),
type_id: elem_ty.initial_type_id.index(),
type_arguments: elem_ty.initial_type_id.get_abi_type_arguments(
ctx,
engines,
types,
elem_ty.type_id,
),
}])
} else {
unreachable!();
}
}
TypeInfo::Tuple(_) => {
if let TypeInfo::Tuple(fields) = &*type_engine.get(resolved_type_id) {
// A list of all `program_abi::TypeDeclaration`s needed for the tuple fields
Expand Down
52 changes: 24 additions & 28 deletions sway-core/src/control_flow_analysis/dead_code_analysis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -970,6 +970,9 @@ fn get_struct_type_info_from_type_id(
TypeInfo::Array(type_arg, _) => {
get_struct_type_info_from_type_id(type_engine, decl_engine, type_arg.type_id)
}
TypeInfo::Slice(type_arg) => {
get_struct_type_info_from_type_id(type_engine, decl_engine, type_arg.type_id)
}
_ => Ok(None),
}
}
Expand Down Expand Up @@ -1792,35 +1795,28 @@ fn connect_expression<'eng: 'cfg, 'cfg>(
elem_type: _,
contents,
} => {
let mut element_diverge = false;
let nodes = contents
.iter()
.map(|elem| {
if !element_diverge
&& type_engine
.get(elem.return_type)
.is_uninhabited(engines.te(), engines.de())
{
element_diverge = true
}
connect_expression(
engines,
&elem.expression,
graph,
leaves,
exit_node,
"",
tree_type,
elem.span.clone(),
options,
)
})
.collect::<Result<Vec<_>, _>>()?;
if element_diverge {
Ok(vec![])
} else {
Ok(nodes.concat())
let mut last = leaves.to_vec();

for elem in contents.iter() {
last = connect_expression(
engines,
&elem.expression,
graph,
last.as_slice(),
None,
"",
tree_type,
elem.span.clone(),
options,
)?;

// If an element diverges, break the connections and return nothing
if last.is_empty() {
break;
}
}

Ok(last)
}
ArrayIndex { prefix, index } => {
let prefix_idx = connect_expression(
Expand Down
14 changes: 7 additions & 7 deletions sway-core/src/ir_generation/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use crate::{

use super::{
const_eval::{compile_const_decl, LookupEnv},
convert::convert_resolved_typeid,
convert::convert_resolved_type_id,
function::FnCompiler,
CompiledFunctionCache,
};
Expand Down Expand Up @@ -298,11 +298,11 @@ pub(crate) fn compile_configurables(
{
let decl = engines.de().get(decl_id);

let ty = convert_resolved_typeid(
let ty = convert_resolved_type_id(
engines.te(),
engines.de(),
context,
&decl.type_ascription.type_id,
decl.type_ascription.type_id,
&decl.type_ascription.span,
)
.unwrap();
Expand Down Expand Up @@ -520,11 +520,11 @@ fn compile_fn(
.iter()
.map(|param| {
// Convert to an IR type.
convert_resolved_typeid(
convert_resolved_type_id(
type_engine,
decl_engine,
context,
&param.type_argument.type_id,
param.type_argument.type_id,
&param.type_argument.span,
)
.map(|ty| {
Expand All @@ -544,11 +544,11 @@ fn compile_fn(
.collect::<Result<Vec<_>, CompileError>>()
.map_err(|err| vec![err])?;

let ret_type = convert_resolved_typeid(
let ret_type = convert_resolved_type_id(
type_engine,
decl_engine,
context,
&return_type.type_id,
return_type.type_id,
&return_type.span,
)
.map_err(|err| vec![err])?;
Expand Down
24 changes: 15 additions & 9 deletions sway-core/src/ir_generation/const_eval.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::{
};

use super::{
convert::{convert_literal_to_constant, convert_resolved_typeid},
convert::{convert_literal_to_constant, convert_resolved_type_id},
function::FnCompiler,
types::*,
};
Expand Down Expand Up @@ -1036,11 +1036,11 @@ fn const_eval_intrinsic(
}
Intrinsic::SizeOfType => {
let targ = &intrinsic.type_arguments[0];
let ir_type = convert_resolved_typeid(
let ir_type = convert_resolved_type_id(
lookup.engines.te(),
lookup.engines.de(),
lookup.context,
&targ.type_id,
targ.type_id,
&targ.span,
)
.map_err(|_| ConstEvalError::CompileError)?;
Expand All @@ -1052,11 +1052,11 @@ fn const_eval_intrinsic(
Intrinsic::SizeOfVal => {
let val = &intrinsic.arguments[0];
let type_id = val.return_type;
let ir_type = convert_resolved_typeid(
let ir_type = convert_resolved_type_id(
lookup.engines.te(),
lookup.engines.de(),
lookup.context,
&type_id,
type_id,
&val.span,
)
.map_err(|_| ConstEvalError::CompileError)?;
Expand All @@ -1067,11 +1067,11 @@ fn const_eval_intrinsic(
}
Intrinsic::SizeOfStr => {
let targ = &intrinsic.type_arguments[0];
let ir_type = convert_resolved_typeid(
let ir_type = convert_resolved_type_id(
lookup.engines.te(),
lookup.engines.de(),
lookup.context,
&targ.type_id,
targ.type_id,
&targ.span,
)
.map_err(|_| ConstEvalError::CompileError)?;
Expand All @@ -1084,11 +1084,11 @@ fn const_eval_intrinsic(
}
Intrinsic::AssertIsStrArray => {
let targ = &intrinsic.type_arguments[0];
let ir_type = convert_resolved_typeid(
let ir_type = convert_resolved_type_id(
lookup.engines.te(),
lookup.engines.de(),
lookup.context,
&targ.type_id,
targ.type_id,
&targ.span,
)
.map_err(|_| ConstEvalError::CompileError)?;
Expand Down Expand Up @@ -1284,6 +1284,8 @@ fn const_eval_intrinsic(
value: ConstantValue::RawUntypedSlice(bytes[0..(len as usize)].to_vec()),
}))
}
Intrinsic::Slice => todo!(),
Intrinsic::SliceElem => todo!(),
}
}

Expand Down Expand Up @@ -1322,6 +1324,7 @@ mod tests {
None,
));

dbg!(2);
let r = crate::compile_to_ast(
&handler,
&engines,
Expand All @@ -1332,12 +1335,15 @@ mod tests {
None,
);

dbg!(2);
let (errors, _warnings) = handler.consume();

dbg!(1);
if !errors.is_empty() {
panic!("{:#?}", errors);
}

dbg!(1);
let f = r.unwrap();
let f = f.typed.unwrap();

Expand Down
Loading

0 comments on commit d3d6dc2

Please sign in to comment.