Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix function type inside impl block #49

Merged
merged 1 commit into from
Sep 20, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
40 changes: 38 additions & 2 deletions src/core/compilation_unit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ use cairo_lang_sierra::program::{
};
use cairo_lang_sierra::program_registry::ProgramRegistry;
use cairo_lang_starknet::abi::{
Contract, Item::Function as AbiFunction, Item::L1Handler as AbiL1Handler,
Contract, Item::Function as AbiFunction, Item::Interface as AbiInterface,
Item::L1Handler as AbiL1Handler,
};

pub struct CompilationUnit {
Expand Down Expand Up @@ -149,6 +150,11 @@ impl CompilationUnit {
// Set the function type
for f in self.functions.iter_mut() {
let full_name = f.name();
// This is needed to handle when a function is implemented inside an impl block
// it removes the impl name added
let mut full_name2: Vec<&str> = full_name.split("::").collect();
full_name2.remove(full_name2.len() - 2);
let full_name2 = full_name2.join("::");

// append_keys_and_data is a function implemented by the starknet::Event trait
if full_name.starts_with("core::") || full_name.ends_with("::append_keys_and_data") {
Expand All @@ -163,7 +169,9 @@ impl CompilationUnit {
} else if constructors.contains(&full_name) {
// Constructor
f.set_ty(Type::Constructor);
} else if external_functions.contains(&full_name) {
} else if external_functions.contains(&full_name)
|| external_functions.contains(&full_name2)
{
// External function, we need to check in the abi if it's view or external
let function_name = full_name.rsplit_once("::").unwrap().1;

Expand All @@ -189,6 +197,34 @@ impl CompilationUnit {
break;
}
}
AbiInterface(interface) => {
for item in interface.items.iter() {
match item {
AbiFunction(function) => {
// If multiple interfaces have the same function name then it could be wrong
if function.name == function_name {
match function.state_mutability {
cairo_lang_starknet::abi::StateMutability::External => {
f.set_ty(Type::External);
break;
}
cairo_lang_starknet::abi::StateMutability::View => {
f.set_ty(Type::View);
break;
}
}
}
}
AbiL1Handler(l1handler) => {
if l1handler.name == function_name {
f.set_ty(Type::L1Handler);
break;
}
}
_ => (),
}
}
}
_ => (),
}
}
Expand Down