Move some error checking out of the define_* functions

This commit is contained in:
bjorn3
2020-09-30 12:40:26 +02:00
parent 6161acfba5
commit 7dcfb1b47b

View File

@@ -191,6 +191,16 @@ where
} }
Ok(()) Ok(())
} }
fn validate_for_define(&self) -> ModuleResult<()> {
if self.compiled.is_some() {
return Err(ModuleError::DuplicateDefinition(self.decl.name.clone()));
}
if !self.decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(self.decl.name.clone()));
}
Ok(())
}
} }
/// Information about a data object which can be accessed. /// Information about a data object which can be accessed.
@@ -226,6 +236,16 @@ where
"Can't change TLS data object to normal or in the opposite way", "Can't change TLS data object to normal or in the opposite way",
); );
} }
fn validate_for_define(&self) -> ModuleResult<()> {
if self.compiled.is_some() {
return Err(ModuleError::DuplicateDefinition(self.decl.name.clone()));
}
if !self.decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(self.decl.name.clone()));
}
Ok(())
}
} }
/// This provides a view to the state of a module which allows `ir::ExternalName`s to be translated /// This provides a view to the state of a module which allows `ir::ExternalName`s to be translated
@@ -553,12 +573,7 @@ where
); );
let CodeInfo { total_size, .. } = ctx.compile(self.backend.isa())?; let CodeInfo { total_size, .. } = ctx.compile(self.backend.isa())?;
let info = &self.contents.functions[func]; let info = &self.contents.functions[func];
if info.compiled.is_some() { info.validate_for_define()?;
return Err(ModuleError::DuplicateDefinition(info.decl.name.clone()));
}
if !info.decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(info.decl.name.clone()));
}
let compiled = self.backend.define_function( let compiled = self.backend.define_function(
func, func,
@@ -588,12 +603,7 @@ where
) -> ModuleResult<ModuleCompiledFunction> { ) -> ModuleResult<ModuleCompiledFunction> {
info!("defining function {} with bytes", func); info!("defining function {} with bytes", func);
let info = &self.contents.functions[func]; let info = &self.contents.functions[func];
if info.compiled.is_some() { info.validate_for_define()?;
return Err(ModuleError::DuplicateDefinition(info.decl.name.clone()));
}
if !info.decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(info.decl.name.clone()));
}
let total_size: u32 = match bytes.len().try_into() { let total_size: u32 = match bytes.len().try_into() {
Ok(total_size) => total_size, Ok(total_size) => total_size,
@@ -613,12 +623,7 @@ where
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> { pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
let compiled = { let compiled = {
let info = &self.contents.data_objects[data]; let info = &self.contents.data_objects[data];
if info.compiled.is_some() { info.validate_for_define()?;
return Err(ModuleError::DuplicateDefinition(info.decl.name.clone()));
}
if !info.decl.linkage.is_definable() {
return Err(ModuleError::InvalidImportDefinition(info.decl.name.clone()));
}
Some(self.backend.define_data( Some(self.backend.define_data(
data, data,
&info.decl.name, &info.decl.name,