diff --git a/cranelift/module/src/module.rs b/cranelift/module/src/module.rs index 7112c1e86c..eb4e668b98 100644 --- a/cranelift/module/src/module.rs +++ b/cranelift/module/src/module.rs @@ -29,6 +29,18 @@ impl From for ir::ExternalName { } } +impl FuncId { + /// Get the `FuncId` for the function named by `name`. + pub fn from_name(name: &ir::ExternalName) -> FuncId { + if let ir::ExternalName::User { namespace, index } = *name { + debug_assert_eq!(namespace, 0); + FuncId::from_u32(index) + } else { + panic!("unexpected ExternalName kind {}", name) + } + } +} + /// A data object identifier for use in the `Module` interface. #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct DataId(u32); @@ -44,6 +56,18 @@ impl From for ir::ExternalName { } } +impl DataId { + /// Get the `DataId` for the data object named by `name`. + pub fn from_name(name: &ir::ExternalName) -> DataId { + if let ir::ExternalName::User { namespace, index } = *name { + debug_assert_eq!(namespace, 1); + DataId::from_u32(index) + } else { + panic!("unexpected ExternalName kind {}", name) + } + } +} + /// Linkage refers to where an entity is defined and who can see it. #[derive(Copy, Clone, Debug, PartialEq, Eq)] pub enum Linkage { @@ -214,21 +238,10 @@ impl ModuleDeclarations { self.functions.iter() } - /// Get the `FuncId` for the function named by `name`. - pub fn get_function_id(&self, name: &ir::ExternalName) -> FuncId { - if let ir::ExternalName::User { namespace, index } = *name { - debug_assert_eq!(namespace, 0); - FuncId::from_u32(index) - } else { - panic!("unexpected ExternalName kind {}", name) - } - } - - /// Get the `DataId` for the data object named by `name`. - pub fn get_data_id(&self, name: &ir::ExternalName) -> DataId { - if let ir::ExternalName::User { namespace, index } = *name { - debug_assert_eq!(namespace, 1); - DataId::from_u32(index) + /// Return whether `name` names a function, rather than a data object. + pub fn is_function(name: &ir::ExternalName) -> bool { + if let ir::ExternalName::User { namespace, .. } = *name { + namespace == 0 } else { panic!("unexpected ExternalName kind {}", name) } @@ -249,15 +262,6 @@ impl ModuleDeclarations { &self.data_objects[data_id] } - /// Return whether `name` names a function, rather than a data object. - pub fn is_function(&self, name: &ir::ExternalName) -> bool { - if let ir::ExternalName::User { namespace, .. } = *name { - namespace == 0 - } else { - panic!("unexpected ExternalName kind {}", name) - } - } - /// Declare a function in this module. pub fn declare_function( &mut self, diff --git a/cranelift/object/src/backend.rs b/cranelift/object/src/backend.rs index e27ebcde04..853289d3e2 100644 --- a/cranelift/object/src/backend.rs +++ b/cranelift/object/src/backend.rs @@ -477,11 +477,11 @@ impl ObjectModule { fn get_symbol(&mut self, name: &ir::ExternalName) -> SymbolId { match *name { ir::ExternalName::User { .. } => { - if self.declarations.is_function(name) { - let id = self.declarations.get_function_id(name); + if ModuleDeclarations::is_function(name) { + let id = FuncId::from_name(name); self.functions[id].unwrap().0 } else { - let id = self.declarations.get_data_id(name); + let id = DataId::from_name(name); self.data_objects[id].unwrap().0 } } diff --git a/cranelift/simplejit/src/backend.rs b/cranelift/simplejit/src/backend.rs index a436a3cd23..727fecd10a 100644 --- a/cranelift/simplejit/src/backend.rs +++ b/cranelift/simplejit/src/backend.rs @@ -167,8 +167,8 @@ impl SimpleJITModule { fn get_definition(&self, name: &ir::ExternalName) -> *const u8 { match *name { ir::ExternalName::User { .. } => { - let (name, linkage) = if self.declarations.is_function(name) { - let func_id = self.declarations.get_function_id(name); + let (name, linkage) = if ModuleDeclarations::is_function(name) { + let func_id = FuncId::from_name(name); match &self.functions[func_id] { Some(compiled) => return compiled.ptr, None => { @@ -177,7 +177,7 @@ impl SimpleJITModule { } } } else { - let data_id = self.declarations.get_data_id(name); + let data_id = DataId::from_name(name); match &self.data_objects[data_id] { Some(compiled) => return compiled.ptr, None => {