Skip to content

Commit

Permalink
Merge 7f7775b into a099f34
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj authored Mar 12, 2024
2 parents a099f34 + 7f7775b commit 83dbe54
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 46 deletions.
68 changes: 23 additions & 45 deletions sway-core/src/semantic_analysis/ast_node/declaration/auto_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,23 @@ where
Some(Self { ctx })
}

pub fn parse<T>(engines: &Engines, module_id: ModuleId, input: &str) -> T
pub fn parse<T>(engines: &Engines, module_id: Option<ModuleId>, input: &str) -> T
where
T: Parse,
{
// Uncomment this to see what is being generated
// println!("{}", input);

let handler = <_>::default();
let source_id = engines.se().get_autogenerated_source_id(module_id);
let source_id =
module_id.map(|module_id| engines.se().get_autogenerated_source_id(module_id));

let ts = sway_parse::lex(
&handler,
&std::sync::Arc::from(input),
0,
input.len(),
Some(source_id),
source_id,
)
.unwrap();
let mut p = sway_parse::Parser::new(&handler, &ts);
Expand Down Expand Up @@ -256,7 +258,7 @@ where
pub fn parse_item_fn_to_typed_ast_node(
&mut self,
engines: &Engines,
module_id: ModuleId,
module_id: Option<ModuleId>,
kind: FunctionDeclarationKind,
code: &str,
) -> Option<TyAstNode> {
Expand Down Expand Up @@ -307,7 +309,7 @@ where
fn parse_item_impl_to_typed_ast_node(
&mut self,
engines: &Engines,
module_id: ModuleId,
module_id: Option<ModuleId>,
code: &str,
) -> Option<TyAstNode> {
let mut ctx = crate::transform::to_parsed_lang::Context::new(
Expand Down Expand Up @@ -357,11 +359,7 @@ where
let implementing_for_decl_ref = decl.get_struct_decl_ref().unwrap();
let struct_decl = self.ctx.engines().de().get(implementing_for_decl_ref.id());

let module_id = struct_decl
.span()
.source_id()
.map(|sid| sid.module_id())
.expect("expected a valid module id");
let module_id = struct_decl.span().source_id().map(|sid| sid.module_id());

let abi_encode_body = self.generate_abi_encode_struct_body(engines, &struct_decl);
let abi_encode_code = self.generate_abi_encode_code(
Expand Down Expand Up @@ -397,11 +395,7 @@ where
let enum_decl_ref = decl.get_enum_decl_ref().unwrap();
let enum_decl = self.ctx.engines().de().get(enum_decl_ref.id());

let module_id = enum_decl
.span()
.source_id()
.map(|sid| sid.module_id())
.expect("expected a valid module id");
let module_id = enum_decl.span().source_id().map(|sid| sid.module_id());

let abi_decode_body = self.generate_abi_encode_enum_body(engines, &enum_decl);
let abi_decode_code = self.generate_abi_encode_code(
Expand Down Expand Up @@ -517,16 +511,9 @@ where
pub(crate) fn generate_contract_entry(
&mut self,
engines: &Engines,
module_id: Option<ModuleId>,
contract_fns: &[DeclRef<DeclId<TyFunctionDecl>>],
) -> Option<TyAstNode> {
let module_id = contract_fns
.first()
.expect("expected a valid contract function")
.decl_span()
.source_id()
.map(|sid| sid.module_id())
.expect("expected a valid module id");

let mut code = String::new();

let mut reads = false;
Expand Down Expand Up @@ -616,11 +603,7 @@ where
engines: &Engines,
decl: &TyFunctionDecl,
) -> Option<TyAstNode> {
let module_id = decl
.span
.source_id()
.map(|sid| sid.module_id())
.expect("expected a valid module id");
let module_id = decl.span.source_id().map(|sid| sid.module_id());

let args_types = itertools::intersperse(
decl.parameters
Expand All @@ -630,12 +613,6 @@ where
)
.collect::<String>();

let args_types = if args_types.is_empty() {
"()".into()
} else {
format!("({args_types},)")
};

let expanded_args = itertools::intersperse(
decl.parameters
.iter()
Expand All @@ -645,12 +622,17 @@ where
)
.collect::<String>();

let code = format!(
"pub fn __entry() -> bool {{
let args = decode_script_data::<{args_types}>();
main({expanded_args})
}}"
);
let code = if args_types.is_empty() {
"pub fn __entry() -> bool { main() }".to_string()
} else {
let args_types = format!("({args_types},)");
format!(
"pub fn __entry() -> bool {{
let args = decode_script_data::<{args_types}>();
main({expanded_args})
}}"
)
};

self.parse_item_fn_to_typed_ast_node(
engines,
Expand All @@ -665,11 +647,7 @@ where
engines: &Engines,
decl: &TyFunctionDecl,
) -> Option<TyAstNode> {
let module_id = decl
.span
.source_id()
.map(|sid| sid.module_id())
.expect("expected a valid module id");
let module_id = decl.span.source_id().map(|sid| sid.module_id());

let args_types = itertools::intersperse(
decl.parameters
Expand Down
6 changes: 5 additions & 1 deletion sway-core/src/semantic_analysis/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,7 +344,11 @@ impl ty::TyModule {
let mut fn_generator =
auto_impl::AutoImplAbiEncodeContext::new(&mut ctx).unwrap();
let node = fn_generator
.generate_contract_entry(engines, &contract_fns)
.generate_contract_entry(
engines,
parsed.span.source_id().map(|x| x.module_id()),
&contract_fns,
)
.unwrap();
all_nodes.push(node)
}
Expand Down

0 comments on commit 83dbe54

Please sign in to comment.