Skip to content

Commit

Permalink
Adds abi_with_fully_specified_types to abi_str ctx.
Browse files Browse the repository at this point in the history
```
    __log(1u32);         // hashed: "u32"
    __log((1u32, 1u64)); // hashed: "(u32, u64)"
    __log([0u64]);       // hashed: "[u64; 1]"
    __log(Some(0u64));   // hashed: "enum std::option::Option<u64>"
    __log(SS{
        ss: 1u64
    });                  // hashed: "struct SS<u64>"
```
  • Loading branch information
esdrubal committed May 15, 2024
1 parent c8cbe78 commit bf67753
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 20 deletions.
57 changes: 52 additions & 5 deletions sway-core/src/abi_generation/abi_str.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::{language::CallPath, Engines, TypeArgument, TypeId, TypeInfo};
pub struct AbiStrContext {
pub program_name: Option<String>,
pub abi_with_callpaths: bool,
pub abi_with_fully_specified_types: bool,
}

impl TypeId {
Expand Down Expand Up @@ -36,13 +37,24 @@ impl TypeId {
assert_eq!(fields.len(), resolved_fields.len());
let field_strs = fields
.iter()
.map(|_| "_".to_string())
.map(|f| {
if ctx.abi_with_fully_specified_types {
type_engine.get(f.type_id).abi_str(ctx, engines)
} else {
"_".to_string()
}
})
.collect::<Vec<String>>();
format!("({})", field_strs.join(", "))
}
(TypeInfo::Array(_, count), TypeInfo::Array(_, resolved_count)) => {
(TypeInfo::Array(type_arg, count), TypeInfo::Array(_, resolved_count)) => {
assert_eq!(count.val(), resolved_count.val());
format!("[_; {}]", count.val())
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, count.val())
}
(TypeInfo::Custom { .. }, _) => {
format!("generic {}", type_engine.get(*self).abi_str(ctx, engines))
Expand All @@ -57,6 +69,7 @@ impl TypeInfo {
pub fn abi_str(&self, ctx: &AbiStrContext, engines: &Engines) -> String {
use TypeInfo::*;
let decl_engine = engines.de();
let type_engine = engines.te();
match self {
Unknown => "unknown".into(),
Never => "never".into(),
Expand Down Expand Up @@ -91,11 +104,45 @@ impl TypeInfo {
ErrorRecovery(_) => "unknown due to error".into(),
Enum(decl_ref) => {
let decl = decl_engine.get_enum(decl_ref);
format!("enum {}", call_path_display(ctx, &decl.call_path))
let type_params =
if !ctx.abi_with_fully_specified_types || decl.type_parameters.is_empty() {
"".into()
} else {
format!(
"<{}>",
decl.type_parameters
.iter()
.map(|p| type_engine.get(p.type_id).abi_str(ctx, engines))
.collect::<Vec<_>>()
.join(",")
)
};
format!(
"enum {}{}",
call_path_display(ctx, &decl.call_path),
type_params
)
}
Struct(decl_ref) => {
let decl = decl_engine.get_struct(decl_ref);
format!("struct {}", call_path_display(ctx, &decl.call_path))
let type_params =
if !ctx.abi_with_fully_specified_types || decl.type_parameters.is_empty() {
"".into()
} else {
format!(
"<{}>",
decl.type_parameters
.iter()
.map(|p| type_engine.get(p.type_id).abi_str(ctx, engines))
.collect::<Vec<_>>()
.join(",")
)
};
format!(
"struct {}{}",
call_path_display(ctx, &decl.call_path),
type_params
)
}
ContractCaller { abi_name, .. } => {
format!("contract caller {abi_name}")
Expand Down
43 changes: 28 additions & 15 deletions sway-core/src/abi_generation/fuel_abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,11 @@ pub struct AbiContext<'a> {
}

impl<'a> AbiContext<'a> {
fn to_str_context(&self, engines: &Engines) -> AbiStrContext {
fn to_str_context(
&self,
engines: &Engines,
abi_with_fully_specified_types: bool,
) -> AbiStrContext {
AbiStrContext {
program_name: self
.program
Expand All @@ -25,6 +29,7 @@ impl<'a> AbiContext<'a> {
.module_id(engines)
.read(engines, |m| m.name.clone().map(|v| v.as_str().to_string())),
abi_with_callpaths: self.abi_with_callpaths,
abi_with_fully_specified_types,
}
}
}
Expand Down Expand Up @@ -96,7 +101,11 @@ fn generate_logged_types(
.iter()
.map(|(_, type_id)| program_abi::TypeDeclaration {
type_id: type_id.index(),
type_field: type_id.get_abi_type_str(&ctx.to_str_context(engines), engines, *type_id),
type_field: type_id.get_abi_type_str(
&ctx.to_str_context(engines, false),
engines,
*type_id,
),
components: type_id.get_abi_type_components(ctx, engines, types, *type_id),
type_parameters: type_id.get_abi_type_parameters(ctx, engines, types, *type_id),
})
Expand Down Expand Up @@ -146,7 +155,11 @@ fn generate_messages_types(
.iter()
.map(|(_, type_id)| program_abi::TypeDeclaration {
type_id: type_id.index(),
type_field: type_id.get_abi_type_str(&ctx.to_str_context(engines), engines, *type_id),
type_field: type_id.get_abi_type_str(
&ctx.to_str_context(engines, false),
engines,
*type_id,
),
components: type_id.get_abi_type_components(ctx, engines, types, *type_id),
type_parameters: type_id.get_abi_type_parameters(ctx, engines, types, *type_id),
})
Expand Down Expand Up @@ -186,7 +199,7 @@ fn generate_configurables(
}| program_abi::TypeDeclaration {
type_id: type_ascription.type_id.index(),
type_field: type_ascription.type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
type_ascription.type_id,
),
Expand Down Expand Up @@ -281,7 +294,7 @@ impl TypeId {
.map(|x| program_abi::TypeDeclaration {
type_id: x.type_argument.initial_type_id.index(),
type_field: x.type_argument.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
x.type_argument.type_id,
),
Expand Down Expand Up @@ -329,7 +342,7 @@ impl TypeId {
.map(|x| program_abi::TypeDeclaration {
type_id: x.type_argument.initial_type_id.index(),
type_field: x.type_argument.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
x.type_argument.type_id,
),
Expand Down Expand Up @@ -373,7 +386,7 @@ impl TypeId {
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),
&ctx.to_str_context(engines, false),
engines,
elem_ty.type_id,
),
Expand Down Expand Up @@ -416,7 +429,7 @@ impl TypeId {
.map(|x| program_abi::TypeDeclaration {
type_id: x.initial_type_id.index(),
type_field: x.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
x.type_id,
),
Expand Down Expand Up @@ -465,7 +478,7 @@ impl TypeId {
.map(|(v, p)| program_abi::TypeDeclaration {
type_id: v.initial_type_id.index(),
type_field: v.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
p.type_id,
),
Expand Down Expand Up @@ -530,7 +543,7 @@ impl TypeId {
.map(|(v, p)| program_abi::TypeDeclaration {
type_id: v.initial_type_id.index(),
type_field: v.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
p.type_id,
),
Expand Down Expand Up @@ -567,7 +580,7 @@ impl TypeId {
.map(|v| program_abi::TypeDeclaration {
type_id: v.type_id.index(),
type_field: v.type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
v.type_id,
),
Expand Down Expand Up @@ -607,7 +620,7 @@ impl TypeId {
.map(|v| program_abi::TypeDeclaration {
type_id: v.type_id.index(),
type_field: v.type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
v.type_id,
),
Expand Down Expand Up @@ -656,7 +669,7 @@ impl TyFunctionDecl {
.map(|x| program_abi::TypeDeclaration {
type_id: x.type_argument.initial_type_id.index(),
type_field: x.type_argument.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
x.type_argument.type_id,
),
Expand All @@ -679,7 +692,7 @@ impl TyFunctionDecl {
let output_type = program_abi::TypeDeclaration {
type_id: self.return_type.initial_type_id.index(),
type_field: self.return_type.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
self.return_type.type_id,
),
Expand Down Expand Up @@ -763,7 +776,7 @@ impl TypeParameter {
let type_parameter = program_abi::TypeDeclaration {
type_id: self.initial_type_id.index(),
type_field: self.initial_type_id.get_abi_type_str(
&ctx.to_str_context(engines),
&ctx.to_str_context(engines, false),
engines,
self.type_id,
),
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/language/ty/expression/intrinsic_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ impl CollectTypesMetadata for TyIntrinsicFunctionKind {
&AbiStrContext {
program_name: Some(ctx.program_name.clone()),
abi_with_callpaths: true,
abi_with_fully_specified_types: true,
},
ctx.engines,
logged_type,
Expand Down

0 comments on commit bf67753

Please sign in to comment.