Skip to content
Open
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
4 changes: 2 additions & 2 deletions cc_bindings_from_rs/cc_bindings_from_rs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use std::rc::Rc;
use cmdline::Cmdline;
use code_gen_utils::CcInclude;
use error_report::{ErrorReport, ErrorReporting, FatalErrors, ReportFatalError};
use generate_bindings::{Database, IncludeGuard};
use generate_bindings::{BindingsGenerator, IncludeGuard};
use kythe_metadata::cc_embed_provenance_map;
use run_compiler::run_compiler;
use token_stream_printer::{
Expand All @@ -58,7 +58,7 @@ fn new_db<'tcx>(
tcx: TyCtxt<'tcx>,
errors: Rc<dyn ErrorReporting>,
fatal_errors: Rc<dyn ReportFatalError>,
) -> Database<'tcx> {
) -> BindingsGenerator<'tcx> {
let mut crate_name_to_include_paths = <HashMap<Rc<str>, Vec<CcInclude>>>::new();
for (crate_name, include_path) in &cmdline.crate_headers {
let paths = crate_name_to_include_paths.entry(crate_name.as_str().into()).or_default();
Expand Down
3 changes: 1 addition & 2 deletions cc_bindings_from_rs/generate_bindings/database/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use std::collections::HashMap;
use std::rc::Rc;

memoized::query_group! {
pub trait BindingsGenerator<'tcx> {
pub struct BindingsGenerator<'tcx> {
#[input]
/// Compilation context for the crate that the bindings should be generated
/// for.
Expand Down Expand Up @@ -282,5 +282,4 @@ memoized::query_group! {
/// Implementation: cc_bindings_from_rs/generate_bindings/generate_struct_and_union.rs?q=function:local_from_trait_impls_by_argument
fn from_trait_impls_by_argument(&self, crate_num: CrateNum) -> Rc<HashMap<Ty<'tcx>, Vec<DefId>>>;
}
pub struct Database;
}
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ pub struct FullyQualifiedName {
}

fn format_ns_path_for_cc(
db: &dyn BindingsGenerator<'_>,
db: &BindingsGenerator<'_>,
ns: &NamespaceQualifier,
) -> Result<TokenStream> {
let idents =
Expand All @@ -73,7 +73,7 @@ fn format_ns_path_for_cc(
}

impl FullyQualifiedName {
pub fn format_for_cc(&self, db: &dyn BindingsGenerator<'_>) -> Result<TokenStream> {
pub fn format_for_cc(&self, db: &BindingsGenerator<'_>) -> Result<TokenStream> {
if let Some(path) = self.unqualified.cpp_type {
let path = format_cc_type_name(path.as_str())?;
return Ok(path);
Expand Down
2 changes: 1 addition & 1 deletion cc_bindings_from_rs/generate_bindings/database/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ mod adt_core_bindings;
pub use adt_core_bindings::{AdtCoreBindings, NoMoveOrAssign};
pub mod cpp_type;
mod db;
pub use db::{BindingsGenerator, Database};
pub use db::BindingsGenerator;
mod fine_grained_feature;
pub use fine_grained_feature::FineGrainedFeature;
mod fully_qualified_name;
Expand Down
53 changes: 22 additions & 31 deletions cc_bindings_from_rs/generate_bindings/format_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ use rustc_span::symbol::Symbol;
use std::rc::Rc;

/// Implementation of `BindingsGenerator::format_top_level_ns_for_crate`.
pub fn format_top_level_ns_for_crate(
db: &dyn BindingsGenerator<'_>,
krate: CrateNum,
) -> Rc<[Symbol]> {
pub fn format_top_level_ns_for_crate(db: &BindingsGenerator<'_>, krate: CrateNum) -> Rc<[Symbol]> {
let mut crate_name = if krate == db.source_crate_num() {
"self".to_string()
} else {
Expand All @@ -55,12 +52,12 @@ pub fn format_top_level_ns_for_crate(
}
}

pub fn format_cc_ident_symbol(db: &dyn BindingsGenerator, ident: Symbol) -> Result<Ident> {
pub fn format_cc_ident_symbol(db: &BindingsGenerator, ident: Symbol) -> Result<Ident> {
format_cc_ident(db, ident.as_str())
}

/// Implementation of `BindingsGenerator::format_cc_ident`.
pub fn format_cc_ident(db: &dyn BindingsGenerator, ident: &str) -> Result<Ident> {
pub fn format_cc_ident(db: &BindingsGenerator, ident: &str) -> Result<Ident> {
// TODO(b/254104998): Check whether the crate where the identifier is defined is
// enabled for the feature. Right now if the dep enables the feature but the
// current crate doesn't, we will escape the identifier in the dep but
Expand All @@ -74,7 +71,7 @@ pub fn format_cc_ident(db: &dyn BindingsGenerator, ident: &str) -> Result<Ident>
}

pub fn format_pointer_or_reference_ty_for_cc<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
pointee: SugaredTy<'tcx>,
mutability: Mutability,
pointer_sigil: TokenStream,
Expand All @@ -93,7 +90,7 @@ pub fn format_pointer_or_reference_ty_for_cc<'tcx>(
}

pub fn format_slice_pointer_for_cc<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
slice_ty: SugaredTy<'tcx>,
mutability: rustc_middle::mir::Mutability,
) -> Result<CcSnippet> {
Expand All @@ -119,12 +116,12 @@ pub fn format_slice_pointer_for_cc<'tcx>(
}

/// Returns a CcSnippet referencing `rs_std::StrRef` and its include path.
pub fn format_str_ref_for_cc(db: &dyn BindingsGenerator<'_>) -> CcSnippet {
pub fn format_str_ref_for_cc(db: &BindingsGenerator<'_>) -> CcSnippet {
CcSnippet::with_include(quote! { rs_std::StrRef }, db.support_header("rs_std/str_ref.h"))
}

pub fn format_transparent_pointee_or_reference_for_cc<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
referent_ty: Ty<'tcx>,
mutability: rustc_middle::mir::Mutability,
pointer_sigil: TokenStream,
Expand All @@ -146,7 +143,7 @@ pub fn format_transparent_pointee_or_reference_for_cc<'tcx>(

/// Implementation of `BindingsGenerator::format_ty_for_cc`.
pub fn format_ty_for_cc<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: SugaredTy<'tcx>,
location: TypeLocation,
) -> Result<CcSnippet> {
Expand Down Expand Up @@ -543,7 +540,7 @@ fn treat_ref_as_ptr<'tcx>(

/// Returns the C++ return type.
pub fn format_ret_ty_for_cc<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
sig_mid: &ty::FnSig<'tcx>,
) -> Result<CcSnippet> {
let output_ty = SugaredTy::fn_output(sig_mid);
Expand Down Expand Up @@ -589,7 +586,7 @@ pub struct CcParamTy {

/// Returns the C++ parameter types.
pub fn format_param_types_for_cc<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
sig_mid: &ty::FnSig<'tcx>,
has_self_param: bool,
) -> Result<Vec<CcParamTy>> {
Expand All @@ -614,7 +611,7 @@ pub fn format_param_types_for_cc<'tcx>(
}

fn try_ty_as_maybe_uninit<'tcx>(
db: &dyn BindingsGenerator<'_>,
db: &BindingsGenerator<'_>,
ty: &Ty<'tcx>,
) -> Option<GenericArg<'tcx>> {
if let ty::TyKind::Adt(adt, substs) = ty.kind() {
Expand All @@ -627,7 +624,7 @@ fn try_ty_as_maybe_uninit<'tcx>(

/// Format a supported `repr(transparent)` pointee type
pub fn format_transparent_pointee<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: &Ty<'tcx>,
) -> Result<TokenStream> {
let Some(generic_arg) = try_ty_as_maybe_uninit(db, ty) else {
Expand All @@ -642,7 +639,7 @@ fn has_non_lifetime_substs(substs: &[ty::GenericArg]) -> bool {
}

fn format_fn_ptr_for_rs<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
binder_with_fn_sig_tys: ty::Binder<ty::FnSigTys<TyCtxt<'tcx>>>,
fn_header: ty::FnHeader<TyCtxt<'tcx>>,
) -> Result<TokenStream> {
Expand Down Expand Up @@ -698,10 +695,7 @@ fn format_fn_ptr_for_rs<'tcx>(
/// than just `SomeStruct`.
//
// TODO(b/259724276): This function's results should be memoized.
pub fn format_ty_for_rs<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
ty: Ty<'tcx>,
) -> Result<TokenStream> {
pub fn format_ty_for_rs<'tcx>(db: &BindingsGenerator<'tcx>, ty: Ty<'tcx>) -> Result<TokenStream> {
Ok(match ty.kind() {
ty::TyKind::Bool
| ty::TyKind::Float(_)
Expand Down Expand Up @@ -806,7 +800,7 @@ pub fn format_ty_for_rs<'tcx>(
}

pub fn format_region_as_cc_lifetime<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
region: &ty::Region<'tcx>,
prereqs: &mut CcPrerequisites,
) -> TokenStream {
Expand Down Expand Up @@ -885,10 +879,7 @@ fn layout_pointer_like(from: &Layout, data_layout: &TargetDataLayout) -> bool {
}

/// Returns an error if `ty` is not pointer-like.
pub fn ensure_ty_is_pointer_like<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
ty: Ty<'tcx>,
) -> Result<()> {
pub fn ensure_ty_is_pointer_like<'tcx>(db: &BindingsGenerator<'tcx>, ty: Ty<'tcx>) -> Result<()> {
if let ty::TyKind::Adt(adt, _) = ty.kind() {
if !adt.repr().transparent() {
bail!("Can't convert {ty} to a C++ pointer as it's not `repr(transparent)`");
Expand All @@ -908,7 +899,7 @@ pub fn ensure_ty_is_pointer_like<'tcx>(
}

pub fn crubit_abi_type_from_ty<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: Ty<'tcx>,
) -> Result<CrubitAbiTypeWithCcPrereqs> {
Ok(CrubitAbiTypeWithCcPrereqs::from(match ty.kind() {
Expand Down Expand Up @@ -1027,7 +1018,7 @@ pub enum BridgedBuiltin {

impl BridgedBuiltin {
/// Determines if an AdtDef is for a Result or Option or neither.
pub fn new(db: &dyn BindingsGenerator<'_>, adt: AdtDef<'_>) -> Option<Self> {
pub fn new(db: &BindingsGenerator<'_>, adt: AdtDef<'_>) -> Option<Self> {
let variant = adt.variants().iter().next()?;

match db.tcx().lang_items().from_def_id(variant.def_id) {
Expand All @@ -1042,7 +1033,7 @@ impl BridgedBuiltin {
/// Returns an error is `crubit_abi_type_from_ty` fails for any of the generic args.
pub fn crubit_abi_type<'tcx>(
self,
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
substs: &[GenericArg<'tcx>],
) -> Result<CrubitAbiTypeWithCcPrereqs> {
match self {
Expand Down Expand Up @@ -1081,7 +1072,7 @@ impl BridgedBuiltin {
/// Returns a CrubitAbiType for a manually annotated composable bridged ADT.
/// May return an error is `crubit_abi_type_from_ty` fails for any of the generic args.
fn crubit_abi_type_from_bridged_adt<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
abi_rust: Symbol,
abi_cpp: Symbol,
substs: &[GenericArg<'tcx>],
Expand Down Expand Up @@ -1115,7 +1106,7 @@ fn crubit_abi_type_from_bridged_adt<'tcx>(
/// Returns None if the type is not manually annotated as bridged.
/// Returns an error if getting the bridging attributes fails.
fn is_manually_annotated_bridged_adt<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: Ty<'tcx>,
) -> Result<Option<BridgedType>> {
// We take a `Ty` instead of adt + substs directly so we can use `Ty` in error messages.
Expand Down Expand Up @@ -1198,7 +1189,7 @@ fn is_manually_annotated_bridged_adt<'tcx>(
/// is configured. An error is returned if the type is a pointer or reference or
/// the attribute could not be parsed or is in an invalid state.
pub fn is_bridged_type<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: Ty<'tcx>,
) -> Result<Option<BridgedType>> {
match ty.kind() {
Expand Down
14 changes: 7 additions & 7 deletions cc_bindings_from_rs/generate_bindings/generate_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl FunctionKind {
}

fn thunk_name(
db: &dyn BindingsGenerator,
db: &BindingsGenerator,
def_id: DefId,
export_name: Option<Symbol>,
needs_thunk: bool,
Expand Down Expand Up @@ -129,7 +129,7 @@ fn ident_for_each(prefix: &str, n: usize) -> Vec<Ident> {
/// Returns a `TokenStream` containing an expression that evaluates to the
/// C-ABI-compatible version of the type.
fn cc_param_to_c_abi<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
cc_ident: Ident,
ty: SugaredTy<'tcx>,
post_analysis_typing_env: ty::TypingEnv<'tcx>,
Expand Down Expand Up @@ -243,7 +243,7 @@ struct ReturnConversion {
}

fn format_ty_for_cc_amending_prereqs<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: SugaredTy<'tcx>,
prereqs: &mut CcPrerequisites,
) -> Result<TokenStream> {
Expand All @@ -254,7 +254,7 @@ fn format_ty_for_cc_amending_prereqs<'tcx>(
}

fn cc_return_value_from_c_abi<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ident: Ident,
ty: SugaredTy<'tcx>,
prereqs: &mut CcPrerequisites,
Expand Down Expand Up @@ -477,7 +477,7 @@ struct RefsToCheckForAliasing<'a, 'tcx> {
/// C++ does not have this requirement, so we insert checks in the generated bindings to ensure that
/// this requirement is not violated.
fn refs_to_check_for_aliasing<'tcx, 'a>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
params: &'a [Param<'tcx>],
) -> Option<RefsToCheckForAliasing<'a, 'tcx>> {
let tcx = db.tcx();
Expand Down Expand Up @@ -543,7 +543,7 @@ impl ThunkSelfParameter {
/// Generates the wrapping code to call a thunk and return its result.
/// This can be checking parameter invariants or creating a slot to pass as an output pointer.
pub(crate) fn generate_thunk_call<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
def_id: DefId,
thunk_name: Ident,
rs_return_type: SugaredTy<'tcx>,
Expand Down Expand Up @@ -656,7 +656,7 @@ pub(crate) fn generate_thunk_call<'tcx>(
}

/// Implementation of `BindingsGenerator::generate_function`.
pub fn generate_function(db: &dyn BindingsGenerator<'_>, def_id: DefId) -> Result<ApiSnippets> {
pub fn generate_function(db: &BindingsGenerator<'_>, def_id: DefId) -> Result<ApiSnippets> {
let tcx = db.tcx();
ensure!(
!tcx.generics_of(def_id).requires_monomorphization(tcx),
Expand Down
16 changes: 8 additions & 8 deletions cc_bindings_from_rs/generate_bindings/generate_function_thunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ fn array_c_abi_c_type<'tcx>(tcx: ty::TyCtxt<'tcx>, inner_ty: ty::Ty<'tcx>) -> Re

/// Formats a C++ declaration of a C-ABI-compatible-function wrapper around a Rust function.
pub fn generate_thunk_decl<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
sig_mid: &ty::FnSig<'tcx>,
thunk_name: &Ident,
has_self_param: bool,
Expand Down Expand Up @@ -180,7 +180,7 @@ pub fn generate_thunk_decl<'tcx>(
/// Expects an exising local of type `cpp_type` named `local_name` and shadows it
/// with a local of type `ty` named `local_name`.
fn convert_bridged_type_from_c_abi_to_rust<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: ty::Ty<'tcx>,
bridged_type: &BridgedType,
local_name: &Ident,
Expand Down Expand Up @@ -234,7 +234,7 @@ fn convert_bridged_type_from_c_abi_to_rust<'tcx>(
/// Converts a local named `local_name` from its C ABI-compatible type
/// `*const [*const core::ffi::c_void; <tuple_tys.len()>]` to a tuple of Rust types.
fn convert_tuple_from_c_abi_to_rust<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
tuple_tys: &[ty::Ty<'tcx>],
local_name: &Ident,
extern_c_decls: &mut BTreeSet<ExternCDecl>,
Expand Down Expand Up @@ -263,7 +263,7 @@ fn convert_tuple_from_c_abi_to_rust<'tcx>(
/// Returns code to convert a local named `local_name` from its C ABI-compatible type to its Rust
/// type.
fn convert_value_from_c_abi_to_rust<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: ty::Ty<'tcx>,
local_name: &Ident,
extern_c_decls: &mut BTreeSet<ExternCDecl>,
Expand All @@ -290,7 +290,7 @@ fn convert_value_from_c_abi_to_rust<'tcx>(
}

fn c_abi_for_param_type<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
ty: ty::Ty<'tcx>,
) -> Result<TokenStream> {
let tcx = db.tcx();
Expand Down Expand Up @@ -378,7 +378,7 @@ fn add_extern_c_decl(

/// Writes a Rust value out into the memory pointed to a `*mut c_void` pointed to by `c_ptr`.
fn write_rs_value_to_c_abi_ptr<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
rs_value: &Ident,
c_ptr: &Ident,
rs_type: ty::Ty<'tcx>,
Expand Down Expand Up @@ -496,7 +496,7 @@ where
/// - `<::crate_name::some_module::SomeStruct as
/// ::core::default::Default>::default`
pub fn generate_thunk_impl<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
fn_def_id: DefId,
sig: &ty::FnSig<'tcx>,
thunk_name: &str,
Expand Down Expand Up @@ -628,7 +628,7 @@ pub struct TraitThunks {
}

pub fn generate_trait_thunks<'tcx>(
db: &dyn BindingsGenerator<'tcx>,
db: &BindingsGenerator<'tcx>,
trait_id: DefId,
// We do not support other generic args, yet.
type_args: &[Ty<'tcx>],
Expand Down
Loading