cairo_lang_sierra/extensions/
mod.rspub mod core;
pub mod error;
pub mod lib_func;
pub mod modules;
pub mod type_specialization_context;
pub mod types;
use num_bigint::BigInt;
pub use self::error::{ExtensionError, SpecializationError};
pub use self::lib_func::{
ConcreteLibfunc, GenericLibfunc, GenericLibfuncEx, NamedLibfunc, NoGenericArgsGenericLibfunc,
OutputVarReferenceInfo, SignatureBasedConcreteLibfunc,
};
pub use self::modules::*;
use self::type_specialization_context::TypeSpecializationContext;
pub use self::types::{
ConcreteType, GenericType, GenericTypeEx, NamedType, NoGenericArgsGenericType,
};
use crate::ids::{ConcreteTypeId, FunctionId};
use crate::program::GenericArg;
fn args_as_single_value(args: &[GenericArg]) -> Result<BigInt, SpecializationError> {
match args {
[GenericArg::Value(c)] => Ok(c.clone()),
[_] => Err(SpecializationError::UnsupportedGenericArg),
_ => Err(SpecializationError::WrongNumberOfGenericArgs),
}
}
pub fn args_as_single_type(args: &[GenericArg]) -> Result<ConcreteTypeId, SpecializationError> {
match args {
[GenericArg::Type(ty)] => Ok(ty.clone()),
[_] => Err(SpecializationError::UnsupportedGenericArg),
_ => Err(SpecializationError::WrongNumberOfGenericArgs),
}
}
fn args_as_two_types(
args: &[GenericArg],
) -> Result<(ConcreteTypeId, ConcreteTypeId), SpecializationError> {
match args {
[GenericArg::Type(ty0), GenericArg::Type(ty1)] => Ok((ty0.clone(), ty1.clone())),
[_, _] => Err(SpecializationError::UnsupportedGenericArg),
_ => Err(SpecializationError::WrongNumberOfGenericArgs),
}
}
fn args_as_single_user_func(args: &[GenericArg]) -> Result<FunctionId, SpecializationError> {
match args {
[GenericArg::UserFunc(function_id)] => Ok(function_id.clone()),
[_] => Err(SpecializationError::UnsupportedGenericArg),
_ => Err(SpecializationError::WrongNumberOfGenericArgs),
}
}
fn extract_type_generic_args<T: NamedType>(
context: &dyn TypeSpecializationContext,
ty: &ConcreteTypeId,
) -> Result<Vec<GenericArg>, SpecializationError> {
let long_id = context.get_type_info(ty.clone())?.long_id;
if long_id.generic_id != T::ID {
Err(SpecializationError::UnsupportedGenericArg)
} else {
Ok(long_id.generic_args)
}
}
#[cfg(test)]
mod test;