Skip to content

Commit

Permalink
tool-and-c-backend-pub - Make some features of the tool pub for exter… (
Browse files Browse the repository at this point in the history
#694)

* tool-and-c-backend-pub - Make some features of the tool pub for external backends

* tool-and-c-backend-pub - remove extra files
  • Loading branch information
jcrist1 authored Sep 20, 2024
1 parent d7ecae0 commit 588f6ff
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 31 deletions.
2 changes: 1 addition & 1 deletion tool/src/c/formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Expand Down
2 changes: 1 addition & 1 deletion tool/src/c/header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 6 additions & 6 deletions tool/src/c/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<String>) {
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() {
Expand Down
26 changes: 13 additions & 13 deletions tool/src/c/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand All @@ -107,7 +107,7 @@ impl<'cx, 'tcx> TyGenContext<'cx, 'tcx> {
decl_header
}

pub(crate) fn gen_struct_def<P: TyPosition>(&self, def: &'tcx hir::StructDef<P>) -> Header {
pub fn gen_struct_def<P: TyPosition>(&self, def: &'tcx hir::StructDef<P>) -> 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![];
Expand All @@ -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![];
Expand Down Expand Up @@ -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![];
Expand Down
20 changes: 10 additions & 10 deletions tool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -135,19 +135,19 @@ 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.
files: RefCell<HashMap<String, String>>,
}

impl FileMap {
fn take_files(self) -> HashMap<String, String> {
pub fn take_files(self) -> HashMap<String, String> {
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)
}
Expand All @@ -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<ErrorContext<'tcx>>,
errors: RefCell<Vec<(ErrorContext<'tcx>, E)>>,
Expand All @@ -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>,
Expand All @@ -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())
}
}
Expand All @@ -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) {
Expand Down

0 comments on commit 588f6ff

Please sign in to comment.