Skip to content

Commit

Permalink
Merge 426baed into 64d5398
Browse files Browse the repository at this point in the history
  • Loading branch information
xunilrj authored Mar 12, 2024
2 parents 64d5398 + 426baed commit a0d134a
Showing 1 changed file with 24 additions and 42 deletions.
66 changes: 24 additions & 42 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 @@ -521,11 +515,8 @@ where
) -> 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");
.and_then(|x| x.decl_span().source_id())
.map(|x| x.module_id());

let mut code = String::new();

Expand Down Expand Up @@ -616,11 +607,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 +617,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 +626,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 +651,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

0 comments on commit a0d134a

Please sign in to comment.