Declare a function in this module.
(
&mut self,
name: &str,
linkage: Linkage,
signature: &ir::Signature,
)
| 730 | |
| 731 | /// Declare a function in this module. |
| 732 | pub fn declare_function( |
| 733 | &mut self, |
| 734 | name: &str, |
| 735 | linkage: Linkage, |
| 736 | signature: &ir::Signature, |
| 737 | ) -> ModuleResult<(FuncId, Linkage)> { |
| 738 | // TODO: Can we avoid allocating names so often? |
| 739 | use super::hash_map::Entry::*; |
| 740 | match self.names.entry(name.to_owned()) { |
| 741 | Occupied(entry) => match *entry.get() { |
| 742 | FuncOrDataId::Func(id) => { |
| 743 | let existing = &mut self.functions[id]; |
| 744 | existing.merge(id, linkage, signature)?; |
| 745 | Ok((id, existing.linkage)) |
| 746 | } |
| 747 | FuncOrDataId::Data(..) => { |
| 748 | Err(ModuleError::IncompatibleDeclaration(name.to_owned())) |
| 749 | } |
| 750 | }, |
| 751 | Vacant(entry) => { |
| 752 | let id = self.functions.push(FunctionDeclaration { |
| 753 | name: Some(name.to_owned()), |
| 754 | linkage, |
| 755 | signature: signature.clone(), |
| 756 | }); |
| 757 | entry.insert(FuncOrDataId::Func(id)); |
| 758 | Ok((id, self.functions[id].linkage)) |
| 759 | } |
| 760 | } |
| 761 | } |
| 762 | |
| 763 | /// Declare an anonymous function in this module. |
| 764 | pub fn declare_anonymous_function( |