diff --git a/tool/src/c/formatter.rs b/tool/src/c/formatter.rs index 4d2f8411e..529c1236d 100644 --- a/tool/src/c/formatter.rs +++ b/tool/src/c/formatter.rs @@ -14,7 +14,7 @@ use std::borrow::Cow; /// /// This type may be used by other backends attempting to figure out the names /// of C types and methods. -pub(crate) struct CFormatter<'tcx> { +pub struct CFormatter<'tcx> { tcx: &'tcx TypeContext, is_for_cpp: bool, } diff --git a/tool/src/c/header.rs b/tool/src/c/header.rs index 5b6641144..8af3da194 100644 --- a/tool/src/c/header.rs +++ b/tool/src/c/header.rs @@ -15,7 +15,7 @@ static BASE_INCLUDES: &str = r#" /// This abstraction allows us to build up headers piece by piece without needing /// to precalculate things like the list of dependent headers or forward declarations #[derive(Default)] -pub(crate) struct Header { +pub struct Header { /// The path name used for the header file (for example Foo.h) pub path: String, /// A list of includes diff --git a/tool/src/c/mod.rs b/tool/src/c/mod.rs index 43dc651b8..322f65021 100644 --- a/tool/src/c/mod.rs +++ b/tool/src/c/mod.rs @@ -2,10 +2,10 @@ mod formatter; mod header; mod ty; -pub(crate) use self::formatter::CFormatter; +pub use self::formatter::CFormatter; pub(crate) use self::formatter::CAPI_NAMESPACE; pub(crate) use self::header::Header; -pub(crate) use self::ty::TyGenContext; +pub use self::ty::TyGenContext; use crate::{ErrorStore, FileMap}; use diplomat_core::hir; @@ -38,15 +38,15 @@ pub(crate) fn attr_support() -> BackendAttrSupport { a } +#[derive(askama::Template)] +#[template(path = "c/runtime.h.jinja", escape = "none")] +pub struct Runtime; + pub(crate) fn run(tcx: &hir::TypeContext) -> (FileMap, ErrorStore) { let files = FileMap::default(); let formatter = CFormatter::new(tcx, false); let errors = ErrorStore::default(); - #[derive(askama::Template)] - #[template(path = "c/runtime.h.jinja", escape = "none")] - struct Runtime; - files.add_file("diplomat_runtime.h".into(), Runtime.to_string()); for (id, ty) in tcx.all_types() { diff --git a/tool/src/c/ty.rs b/tool/src/c/ty.rs index 8f44ad103..be3e16929 100644 --- a/tool/src/c/ty.rs +++ b/tool/src/c/ty.rs @@ -68,18 +68,18 @@ struct CallbackAndStructDef { /// The context used for generating a particular type /// /// Also used by C++ generation code -pub(crate) struct TyGenContext<'cx, 'tcx> { - pub(crate) tcx: &'tcx TypeContext, - pub(crate) formatter: &'cx CFormatter<'tcx>, - pub(crate) errors: &'cx ErrorStore<'tcx, String>, - pub(crate) is_for_cpp: bool, - pub(crate) id: SymbolId, - pub(crate) decl_header_path: &'cx String, - pub(crate) impl_header_path: &'cx String, +pub struct TyGenContext<'cx, 'tcx> { + pub tcx: &'tcx TypeContext, + pub formatter: &'cx CFormatter<'tcx>, + pub errors: &'cx ErrorStore<'tcx, String>, + pub is_for_cpp: bool, + pub id: SymbolId, + pub decl_header_path: &'cx String, + pub impl_header_path: &'cx String, } impl<'cx, 'tcx> TyGenContext<'cx, 'tcx> { - pub(crate) fn gen_enum_def(&self, def: &'tcx hir::EnumDef) -> Header { + pub fn gen_enum_def(&self, def: &'tcx hir::EnumDef) -> Header { let mut decl_header = Header::new(self.decl_header_path.clone(), self.is_for_cpp); let ty_name = self.formatter.fmt_type_name(self.id.try_into().unwrap()); EnumTemplate { @@ -94,7 +94,7 @@ impl<'cx, 'tcx> TyGenContext<'cx, 'tcx> { decl_header } - pub(crate) fn gen_opaque_def(&self, _def: &'tcx hir::OpaqueDef) -> Header { + pub fn gen_opaque_def(&self, _def: &'tcx hir::OpaqueDef) -> Header { let mut decl_header = Header::new(self.decl_header_path.clone(), self.is_for_cpp); let ty_name = self.formatter.fmt_type_name(self.id.try_into().unwrap()); OpaqueTemplate { @@ -107,7 +107,7 @@ impl<'cx, 'tcx> TyGenContext<'cx, 'tcx> { decl_header } - pub(crate) fn gen_struct_def(&self, def: &'tcx hir::StructDef

) -> Header { + pub fn gen_struct_def(&self, def: &'tcx hir::StructDef

) -> Header { let mut decl_header = Header::new(self.decl_header_path.clone(), self.is_for_cpp); let ty_name = self.formatter.fmt_type_name(self.id.try_into().unwrap()); let mut fields = vec![]; @@ -133,7 +133,7 @@ impl<'cx, 'tcx> TyGenContext<'cx, 'tcx> { decl_header } - pub(crate) fn gen_trait_def(&self, def: &'tcx hir::TraitDef) -> Header { + pub fn gen_trait_def(&self, def: &'tcx hir::TraitDef) -> Header { let mut decl_header = Header::new(self.decl_header_path.clone(), self.is_for_cpp); let trt_name = self.formatter.fmt_trait_name(self.id.try_into().unwrap()); let mut method_sigs = vec![]; @@ -168,7 +168,7 @@ impl<'cx, 'tcx> TyGenContext<'cx, 'tcx> { decl_header } - pub(crate) fn gen_impl(&self, ty: hir::TypeDef<'tcx>) -> Header { + pub fn gen_impl(&self, ty: hir::TypeDef<'tcx>) -> Header { let mut impl_header = Header::new(self.impl_header_path.clone(), self.is_for_cpp); let mut methods = vec![]; let mut cb_structs_and_defs = vec![]; diff --git a/tool/src/lib.rs b/tool/src/lib.rs index 2b99f590d..f5cf60afb 100644 --- a/tool/src/lib.rs +++ b/tool/src/lib.rs @@ -2,7 +2,7 @@ // #![deny(non_exhaustive_omitted_patterns)] // diplomat_core uses non_exhaustive a lot; we should never miss its patterns // Backends -mod c; +pub mod c; mod cpp; mod dart; mod demo_gen; @@ -135,7 +135,7 @@ pub fn gen( /// This type abstracts over files being written to. #[derive(Default, Debug)] -struct FileMap { +pub struct FileMap { // The context types exist as a way to avoid passing around a billion different // parameters. However, passing them around as &mut self restricts the amount of // borrowing that can be done. We instead use a RefCell to guard the specifically mutable bits. @@ -143,11 +143,11 @@ struct FileMap { } impl FileMap { - fn take_files(self) -> HashMap { + pub fn take_files(self) -> HashMap { mem::take(&mut *self.files.borrow_mut()) } - fn add_file(&self, name: String, contents: String) { + pub fn add_file(&self, name: String, contents: String) { if self.files.borrow().get(&name).is_some() { panic!("File map already contains {}", name) } @@ -163,7 +163,7 @@ impl FileMap { /// once they go out of scope, so you don't have to worry about errors originating from code /// that does not set a context. #[derive(Default)] -struct ErrorStore<'tcx, E> { +pub struct ErrorStore<'tcx, E> { /// The stack of contexts reached so far context: RefCell>, errors: RefCell, E)>>, @@ -172,14 +172,14 @@ struct ErrorStore<'tcx, E> { impl<'tcx, E> ErrorStore<'tcx, E> { /// Set the context to a named type. Will return a scope guard that will automatically /// clear the context on drop. - fn set_context_ty<'a>(&'a self, ty: Cow<'tcx, str>) -> ErrorContextGuard<'a, 'tcx, E> { + pub fn set_context_ty<'a>(&'a self, ty: Cow<'tcx, str>) -> ErrorContextGuard<'a, 'tcx, E> { let new = ErrorContext { ty, method: None }; let old = mem::replace(&mut *self.context.borrow_mut(), new); ErrorContextGuard(self, old) } /// Set the context to a named method. Will return a scope guard that will automatically /// clear the context on drop. - fn set_context_method<'a>( + pub fn set_context_method<'a>( &'a self, ty: Cow<'tcx, str>, method: Cow<'tcx, str>, @@ -193,13 +193,13 @@ impl<'tcx, E> ErrorStore<'tcx, E> { ErrorContextGuard(self, old) } - fn push_error(&self, error: E) { + pub fn push_error(&self, error: E) { self.errors .borrow_mut() .push((self.context.borrow().clone(), error)); } - fn take_all(&self) -> Vec<(impl fmt::Display + 'tcx, E)> { + pub fn take_all(&self) -> Vec<(impl fmt::Display + 'tcx, E)> { mem::take(&mut self.errors.borrow_mut()) } } @@ -224,7 +224,7 @@ impl<'tcx> fmt::Display for ErrorContext<'tcx> { /// Scope guard terminating the context created `set_context_*` method on [`ErrorStore`] #[must_use] -struct ErrorContextGuard<'a, 'tcx, E>(&'a ErrorStore<'tcx, E>, ErrorContext<'tcx>); +pub struct ErrorContextGuard<'a, 'tcx, E>(&'a ErrorStore<'tcx, E>, ErrorContext<'tcx>); impl<'a, 'tcx, E> Drop for ErrorContextGuard<'a, 'tcx, E> { fn drop(&mut self) {