Introduce a ModuleResult alias for Result<T, ModuleError>.

This follows the pattern used by cretonne-codegen, cretonne-wasm, and
others.
This commit is contained in:
Dan Gohman
2018-05-19 21:41:05 -07:00
parent 6971ae1c26
commit 43bd3cb2a3
5 changed files with 20 additions and 15 deletions

View File

@@ -5,7 +5,7 @@ use cretonne_codegen::binemit::{Addend, CodeOffset, NullTrapSink, Reloc, RelocSi
use cretonne_codegen::isa::TargetIsa;
use cretonne_codegen::{self, binemit, ir};
use cretonne_module::{Backend, DataContext, DataDescription, Init, Linkage, ModuleError,
ModuleNamespace};
ModuleNamespace, ModuleResult};
use faerie;
use failure::Error;
use std::fs::File;
@@ -52,7 +52,7 @@ impl FaerieBuilder {
format: BinaryFormat,
collect_traps: FaerieTrapCollection,
libcall_names: Box<Fn(ir::LibCall) -> String>,
) -> Result<Self, ModuleError> {
) -> ModuleResult<Self> {
if !isa.flags().is_pic() {
return Err(ModuleError::Backend(
"faerie requires TargetIsa be PIC".to_owned(),
@@ -149,7 +149,7 @@ impl Backend for FaerieBackend {
ctx: &cretonne_codegen::Context,
namespace: &ModuleNamespace<Self>,
code_size: u32,
) -> Result<FaerieCompiledFunction, ModuleError> {
) -> ModuleResult<FaerieCompiledFunction> {
let mut code: Vec<u8> = Vec::with_capacity(code_size as usize);
code.resize(code_size as usize, 0);
@@ -198,7 +198,7 @@ impl Backend for FaerieBackend {
name: &str,
data_ctx: &DataContext,
namespace: &ModuleNamespace<Self>,
) -> Result<FaerieCompiledData, ModuleError> {
) -> ModuleResult<FaerieCompiledData> {
let &DataDescription {
writable: _writable,
ref init,

View File

@@ -8,6 +8,7 @@ use DataContext;
use Linkage;
use ModuleError;
use ModuleNamespace;
use ModuleResult;
/// A `Backend` implements the functionality needed to support a `Module`.
pub trait Backend
@@ -57,7 +58,7 @@ where
ctx: &Context,
namespace: &ModuleNamespace<Self>,
code_size: u32,
) -> Result<Self::CompiledFunction, ModuleError>;
) -> ModuleResult<Self::CompiledFunction>;
/// Define a zero-initialized data object of the given size.
///
@@ -67,7 +68,7 @@ where
name: &str,
data_ctx: &DataContext,
namespace: &ModuleNamespace<Self>,
) -> Result<Self::CompiledData, ModuleError>;
) -> ModuleResult<Self::CompiledData>;
/// Write the address of `what` into the data for `data` at `offset`. `data` must refer to a
/// defined data object.

View File

@@ -33,7 +33,8 @@ mod module;
pub use backend::Backend;
pub use data_context::{DataContext, DataDescription, Init, Writability};
pub use module::{DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleError, ModuleNamespace};
pub use module::{DataId, FuncId, FuncOrDataId, Linkage, Module, ModuleError, ModuleNamespace,
ModuleResult};
/// This replaces `std` in builds with `core`.
#[cfg(not(feature = "std"))]

View File

@@ -139,6 +139,9 @@ pub enum ModuleError {
Backend(String),
}
/// A convenient alias for a `Result` that uses `ModuleError` as the error type.
pub type ModuleResult<T> = Result<T, ModuleError>;
/// A function belonging to a `Module`.
struct ModuleFunction<B>
where
@@ -347,7 +350,7 @@ where
name: &str,
linkage: Linkage,
signature: &ir::Signature,
) -> Result<FuncId, ModuleError> {
) -> ModuleResult<FuncId> {
// TODO: Can we avoid allocating names so often?
use std::collections::hash_map::Entry::*;
match self.names.entry(name.to_owned()) {
@@ -385,7 +388,7 @@ where
name: &str,
linkage: Linkage,
writable: bool,
) -> Result<DataId, ModuleError> {
) -> ModuleResult<DataId> {
// TODO: Can we avoid allocating names so often?
use std::collections::hash_map::Entry::*;
match self.names.entry(name.to_owned()) {
@@ -457,7 +460,7 @@ where
}
/// Define a function, producing the function body from the given `Context`.
pub fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> Result<(), ModuleError> {
pub fn define_function(&mut self, func: FuncId, ctx: &mut Context) -> ModuleResult<()> {
let compiled = {
let code_size = ctx.compile(self.backend.isa()).map_err(|e| {
dbg!(
@@ -489,7 +492,7 @@ where
}
/// Define a function, producing the data contents from the given `DataContext`.
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> Result<(), ModuleError> {
pub fn define_data(&mut self, data: DataId, data_ctx: &DataContext) -> ModuleResult<()> {
let compiled = {
let info = &self.contents.data_objects[data];
if !info.compiled.is_none() {

View File

@@ -3,8 +3,8 @@
use cretonne_codegen::binemit::{Addend, CodeOffset, NullTrapSink, Reloc, RelocSink};
use cretonne_codegen::isa::TargetIsa;
use cretonne_codegen::{self, ir, settings};
use cretonne_module::{Backend, DataContext, DataDescription, Init, Linkage, ModuleError,
ModuleNamespace, Writability};
use cretonne_module::{Backend, DataContext, DataDescription, Init, Linkage, ModuleNamespace,
ModuleResult, Writability};
use cretonne_native;
use libc;
use memory::Memory;
@@ -118,7 +118,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
ctx: &cretonne_codegen::Context,
_namespace: &ModuleNamespace<Self>,
code_size: u32,
) -> Result<Self::CompiledFunction, ModuleError> {
) -> ModuleResult<Self::CompiledFunction> {
let size = code_size as usize;
let ptr = self.code_memory
.allocate(size)
@@ -141,7 +141,7 @@ impl<'simple_jit_backend> Backend for SimpleJITBackend {
_name: &str,
data: &DataContext,
_namespace: &ModuleNamespace<Self>,
) -> Result<Self::CompiledData, ModuleError> {
) -> ModuleResult<Self::CompiledData> {
let &DataDescription {
writable,
ref init,