Don't unnecessarily take &self for some ModuleDeclarations methods

This commit is contained in:
bjorn3
2020-11-09 14:53:15 +01:00
parent 79f6f72e78
commit 844a52e96a
3 changed files with 34 additions and 30 deletions

View File

@@ -29,6 +29,18 @@ impl From<FuncId> 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. /// A data object identifier for use in the `Module` interface.
#[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] #[derive(Copy, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct DataId(u32); pub struct DataId(u32);
@@ -44,6 +56,18 @@ impl From<DataId> 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. /// Linkage refers to where an entity is defined and who can see it.
#[derive(Copy, Clone, Debug, PartialEq, Eq)] #[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum Linkage { pub enum Linkage {
@@ -214,21 +238,10 @@ impl ModuleDeclarations {
self.functions.iter() self.functions.iter()
} }
/// Get the `FuncId` for the function named by `name`. /// Return whether `name` names a function, rather than a data object.
pub fn get_function_id(&self, name: &ir::ExternalName) -> FuncId { pub fn is_function(name: &ir::ExternalName) -> bool {
if let ir::ExternalName::User { namespace, index } = *name { if let ir::ExternalName::User { namespace, .. } = *name {
debug_assert_eq!(namespace, 0); 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)
} else { } else {
panic!("unexpected ExternalName kind {}", name) panic!("unexpected ExternalName kind {}", name)
} }
@@ -249,15 +262,6 @@ impl ModuleDeclarations {
&self.data_objects[data_id] &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. /// Declare a function in this module.
pub fn declare_function( pub fn declare_function(
&mut self, &mut self,

View File

@@ -477,11 +477,11 @@ impl ObjectModule {
fn get_symbol(&mut self, name: &ir::ExternalName) -> SymbolId { fn get_symbol(&mut self, name: &ir::ExternalName) -> SymbolId {
match *name { match *name {
ir::ExternalName::User { .. } => { ir::ExternalName::User { .. } => {
if self.declarations.is_function(name) { if ModuleDeclarations::is_function(name) {
let id = self.declarations.get_function_id(name); let id = FuncId::from_name(name);
self.functions[id].unwrap().0 self.functions[id].unwrap().0
} else { } else {
let id = self.declarations.get_data_id(name); let id = DataId::from_name(name);
self.data_objects[id].unwrap().0 self.data_objects[id].unwrap().0
} }
} }

View File

@@ -167,8 +167,8 @@ impl SimpleJITModule {
fn get_definition(&self, name: &ir::ExternalName) -> *const u8 { fn get_definition(&self, name: &ir::ExternalName) -> *const u8 {
match *name { match *name {
ir::ExternalName::User { .. } => { ir::ExternalName::User { .. } => {
let (name, linkage) = if self.declarations.is_function(name) { let (name, linkage) = if ModuleDeclarations::is_function(name) {
let func_id = self.declarations.get_function_id(name); let func_id = FuncId::from_name(name);
match &self.functions[func_id] { match &self.functions[func_id] {
Some(compiled) => return compiled.ptr, Some(compiled) => return compiled.ptr,
None => { None => {
@@ -177,7 +177,7 @@ impl SimpleJITModule {
} }
} }
} else { } else {
let data_id = self.declarations.get_data_id(name); let data_id = DataId::from_name(name);
match &self.data_objects[data_id] { match &self.data_objects[data_id] {
Some(compiled) => return compiled.ptr, Some(compiled) => return compiled.ptr,
None => { None => {