Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flatten out type_generation module, rename to gen #669

Merged
merged 2 commits into from
Sep 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use diplomat_core::hir::{
};
use std::fmt::Write;

use super::TyGenContext;
use super::gen::TyGenContext;

/// Check if an enum's values are consecutive. (i.e., if we start at 1, the next value is 2).
fn is_contiguous_enum(ty: &hir::EnumDef) -> bool {
Expand Down Expand Up @@ -369,7 +369,7 @@ impl<'jsctx, 'tcx> TyGenContext<'jsctx, 'tcx> {
/// We access [`super::MethodInfo`] to handle allocation and cleanup.
pub(super) fn gen_c_to_js_for_return_type(
&self,
method_info: &mut super::MethodInfo,
method_info: &mut super::gen::MethodInfo,
method: &Method,
) -> Option<Cow<'tcx, str>> {
let return_type = &method.output;
Expand Down
37 changes: 18 additions & 19 deletions tool/src/js/type_generation/mod.rs → tool/src/js/gen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,7 @@ use askama::{self, Template};
use super::formatter::JSFormatter;
use crate::ErrorStore;

mod converter;
use converter::{ForcePaddingStatus, JsToCConversionContext, StructBorrowContext};
use super::converter::{ForcePaddingStatus, JsToCConversionContext, StructBorrowContext};

/// Represents context for generating a Javascript class.
///
Expand Down Expand Up @@ -425,7 +424,7 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> {

let struct_borrow_info =
if let ParamBorrowInfo::Struct(param_info) = param_borrow_kind {
Some(converter::StructBorrowContext {
Some(super::converter::StructBorrowContext {
use_env: &method.lifetime_env,
param_info,
is_method: true,
Expand Down Expand Up @@ -499,7 +498,7 @@ impl<'ctx, 'tcx> TyGenContext<'ctx, 'tcx> {

/// Represents a parameter of a method. Used as part of [`MethodInfo`], exclusively in the method definition.
#[derive(Default)]
struct ParamInfo<'a> {
pub(super) struct ParamInfo<'a> {
ty: Cow<'a, str>,
name: Cow<'a, str>,
}
Expand All @@ -511,7 +510,7 @@ struct ParamInfo<'a> {
/// [`ParamInfo`] represents the conversion of the slice into C-friendly terms. This just represents an extra stage for Diplomat to convert whatever slice type we're given into a type that returns a `.ptr` and `.size` field.
///
/// See `DiplomatBuf` in `runtime.mjs` for more.
struct SliceParam<'a> {
pub(super) struct SliceParam<'a> {
name: Cow<'a, str>,
/// How to convert the JS type into a C slice.
slice_expr: String,
Expand All @@ -524,41 +523,41 @@ struct SliceParam<'a> {
#[template(path = "js/method.js.jinja", escape = "none")]
pub(super) struct MethodInfo<'info> {
/// Do we return the `()` type?
method_output_is_ffi_unit: bool,
pub method_output_is_ffi_unit: bool,
/// The declaration signature. Something like `static functionName() { /* ... */ }` versus `functionName() { /* ... */ }`
method_decl: String,
pub method_decl: String,

/// Native C method name
abi_name: String,
pub abi_name: String,

/// If we need to create a `CleanupArena` (see `runtime.mjs`) to free any [`SliceParam`]s that are present.
needs_slice_cleanup: bool,
pub needs_slice_cleanup: bool,
/// For calling .releaseToGarbageCollector on slices.
needs_slice_collection: bool,
pub needs_slice_collection: bool,

pub typescript: bool,

/// Represents all the parameters in the method definition (mostly for `.d.ts` generation, showing names and types).
parameters: Vec<ParamInfo<'info>>,
pub parameters: Vec<ParamInfo<'info>>,
/// See [`SliceParam`] for info on how this array is used.
slice_params: Vec<SliceParam<'info>>,
pub slice_params: Vec<SliceParam<'info>>,
/// Represents the Javascript needed to take the parameters from the method definition into C-friendly terms. See [`TyGenContext::gen_js_to_c_for_type`] for more.
param_conversions: Vec<Cow<'info, str>>,
pub param_conversions: Vec<Cow<'info, str>>,

/// The return type, for `.d.ts` files.
return_type: String,
pub return_type: String,
/// The JS expression used when this method returns.
return_expression: Option<Cow<'info, str>>,
pub return_expression: Option<Cow<'info, str>>,

/// Used for generating edge information when constructing items like Slices, Structs, and Opaque types. See [hir::methods::borrowing_param::BorrowedLifetimeInfo] for more.
method_lifetimes_map: BTreeMap<hir::Lifetime, BorrowedLifetimeInfo<'info>>,
pub method_lifetimes_map: BTreeMap<hir::Lifetime, BorrowedLifetimeInfo<'info>>,
/// We use this to access individual [`hir::Lifetimes`], which we then use to access the [`MethodInfo::method_lifetimes_map`].
lifetimes: Option<&'info LifetimeEnv>,
pub lifetimes: Option<&'info LifetimeEnv>,

/// Anything we need to allocate for [`MethodInfo::param_conversions`]
alloc_expressions: Vec<Cow<'info, str>>,
pub alloc_expressions: Vec<Cow<'info, str>>,
/// Anything from [`MethodInfo::alloc_expressions`] we need to clean up afterwards.
cleanup_expressions: Vec<Cow<'info, str>>,
pub cleanup_expressions: Vec<Cow<'info, str>>,
}

/// See [`TyGenContext::generate_special_method`].
Expand Down
7 changes: 4 additions & 3 deletions tool/src/js/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! JS code generation backend.
//!
//! This module specifically handles the overview of generating all the necessary `.mjs` and `.d.ts` files that [`type_generation`] creates content for.
//! This module specifically handles the overview of generating all the necessary `.mjs` and `.d.ts` files that [`gen`] creates content for.
use std::collections::BTreeSet;
use std::{borrow::Cow, cell::RefCell};

Expand All @@ -12,8 +12,9 @@ use askama::Template;
pub(crate) mod formatter;
use formatter::JSFormatter;

mod type_generation;
use type_generation::{MethodsInfo, TyGenContext};
mod gen;
use gen::{MethodsInfo, TyGenContext};
mod converter;

mod layout;

Expand Down
Loading