|
| 1 | +use std::rc::Rc; |
| 2 | + |
| 3 | +use fe_mir::ir::{self, FunctionId, TypeId}; |
| 4 | +use fe_new_abi::{ |
| 5 | + function::{AbiFunction, AbiFunctionType}, |
| 6 | + types::{AbiTupleField, AbiType}, |
| 7 | +}; |
| 8 | + |
| 9 | +use crate::db::CodegenDb; |
| 10 | + |
| 11 | +pub fn abi_function(db: &dyn CodegenDb, function: FunctionId) -> Rc<AbiFunction> { |
| 12 | + // We use a legalized signature. |
| 13 | + let sig = db.codegen_legalized_signature(function); |
| 14 | + |
| 15 | + let name = function.name(db.upcast()); |
| 16 | + let args = sig |
| 17 | + .params |
| 18 | + .iter() |
| 19 | + .map(|param| (param.name.to_string(), db.codegen_abi_type(param.ty))) |
| 20 | + .collect(); |
| 21 | + let ret_ty = sig.return_type.map(|ty| db.codegen_abi_type(ty)); |
| 22 | + |
| 23 | + AbiFunction::new(AbiFunctionType::Function, name.to_string(), args, ret_ty).into() |
| 24 | +} |
| 25 | + |
| 26 | +pub fn abi_type(db: &dyn CodegenDb, ty: TypeId) -> AbiType { |
| 27 | + if ty.is_zero_sized(db.upcast()) { |
| 28 | + unreachable!("zero-sized type must be removed in legalization"); |
| 29 | + } |
| 30 | + |
| 31 | + let ty = ty.data(db.upcast()); |
| 32 | + |
| 33 | + match ty.as_ref() { |
| 34 | + ir::Type::I8 => AbiType::Int(8), |
| 35 | + ir::Type::I16 => AbiType::Int(16), |
| 36 | + ir::Type::I32 => AbiType::Int(32), |
| 37 | + ir::Type::I64 => AbiType::Int(64), |
| 38 | + ir::Type::I128 => AbiType::Int(128), |
| 39 | + ir::Type::I256 => AbiType::Int(256), |
| 40 | + ir::Type::U8 => AbiType::UInt(8), |
| 41 | + ir::Type::U16 => AbiType::UInt(16), |
| 42 | + ir::Type::U32 => AbiType::UInt(32), |
| 43 | + ir::Type::U64 => AbiType::UInt(64), |
| 44 | + ir::Type::U128 => AbiType::UInt(128), |
| 45 | + ir::Type::U256 => AbiType::UInt(256), |
| 46 | + ir::Type::Bool => AbiType::Bool, |
| 47 | + ir::Type::Address => AbiType::Address, |
| 48 | + ir::Type::Unit => unreachable!("zero-sized type must be removed in legalization"), |
| 49 | + ir::Type::Array(def) => { |
| 50 | + let elem_ty = db.codegen_abi_type(def.elem_ty); |
| 51 | + let len = def.len; |
| 52 | + AbiType::Array { |
| 53 | + elem_ty: elem_ty.into(), |
| 54 | + len, |
| 55 | + } |
| 56 | + } |
| 57 | + ir::Type::Tuple(def) => { |
| 58 | + let fields = def |
| 59 | + .items |
| 60 | + .iter() |
| 61 | + .enumerate() |
| 62 | + .map(|(i, item)| { |
| 63 | + let field_ty = db.codegen_abi_type(*item); |
| 64 | + AbiTupleField::new(format!("{}", i), field_ty) |
| 65 | + }) |
| 66 | + .collect(); |
| 67 | + |
| 68 | + AbiType::Tuple(fields) |
| 69 | + } |
| 70 | + ir::Type::Struct(def) => { |
| 71 | + let fields = def |
| 72 | + .fields |
| 73 | + .iter() |
| 74 | + .map(|(name, ty)| { |
| 75 | + let ty = db.codegen_abi_type(*ty); |
| 76 | + AbiTupleField::new(name.to_string(), ty) |
| 77 | + }) |
| 78 | + .collect(); |
| 79 | + |
| 80 | + AbiType::Tuple(fields) |
| 81 | + } |
| 82 | + ir::Type::Event(_) | ir::Type::Contract(_) => unreachable!(), |
| 83 | + ir::Type::Map(_) => todo!("map type can't be used in parameter or return type"), |
| 84 | + } |
| 85 | +} |
0 commit comments