Skip to content
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
8 changes: 5 additions & 3 deletions ffi/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -511,9 +511,11 @@ packages place it in.

- **Names** are the C names without the prefix, in a namespace of it: `moq_track_Producer`
is `moq::track_Producer`, and its method `moq_track_Producer_append_group` is
`moq::track_Producer::append_group`. A keyword, or a macro GNU dialects predefine
(`unix`, `linux`, `i386`, as in moq-tokio's `tokio::unix_`, and each of them followed by
underscores, so `unix_` becomes `unix__`), takes a trailing `_` (`counter::new_`),
`moq::track_Producer::append_group`. A keyword, or a macro of a short list that GNU dialects predefine or the
C library's headers define (`unix`, `linux`, `i386`, as in moq-tokio's `tokio::unix_`,
and `errno`, `assert`, `offsetof`, `stdin`, `stdout`, and `stderr`, and each of them
followed by underscores, so `unix_` becomes `unix__`; any other macro, such as `EOF` or
`va_start`, is not escaped yet, see [ffi-apart](../quest/m1/ffi-apart.md)), takes a trailing `_` (`counter::new_`),
as does a member named as its class, one of its fields, or a member a trait gives it,
and a parameter named as a member or as anything in the namespace, which it would hide.
A name the escape leaves alone keeps it, and one escaped onto another's takes more `_`s
Expand Down
38 changes: 38 additions & 0 deletions ffi/fixtures/api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4794,6 +4794,44 @@ pub struct DoubleArray {
pub len: u32,
}

/// The value of `errno`, a macro `<cerrno>` defines: a C++ function of this name must not
/// be rewritten as `(*__errno_location ())`.
pub fn errno() -> i32 {
7
}

/// A function named as `assert`, which `<cassert>` defines as a macro.
pub fn assert(holds: bool) -> bool {
holds
}

/// A function named as `offsetof`, which `<cstddef>` defines as a macro.
pub fn offsetof(field: u8) -> u8 {
field
}

/// A record of fields named as macros a C library's headers define, which each binding
/// keeps apart.
pub mod macros {
/// A record of fields named as `stdin`, `stdout`, and `stderr`, macros on some C libraries.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Streams {
pub stdin: u8,
pub stdout: u8,
pub stderr: u8,
pub errno: u8,
}

/// The sum of `streams`' fields, and `errno`, a parameter named as the macro.
pub fn streamed(streams: Streams, errno: u8) -> u32 {
u32::from(streams.stdin)
+ u32::from(streams.stdout)
+ u32::from(streams.stderr)
+ u32::from(streams.errno)
+ u32::from(errno)
}
}

pub mod clash {
/// A pitch in hertz, whose name Pascal case cannot tell from `TONE`'s.
#[derive(Clone, Copy, Debug, PartialEq)]
Expand Down
14 changes: 9 additions & 5 deletions ffi/src/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2044,6 +2044,14 @@ pub(crate) fn distinct(names: impl IntoIterator<Item = String>) -> Vec<String> {
.collect()
}

/// Lowercase macros that would rewrite a name of the same spelling in a program including
/// the header: those GNU dialects predefine, as CMake's default `-std=gnu11` and
/// `-std=gnu++17` do, and those of the C library's headers, which C++'s own and a C
/// program's, such as `<errno.h>`, include.
pub(crate) const MACROS: &[&str] = &[
"assert", "errno", "i386", "linux", "offsetof", "stderr", "stdin", "stdout", "unix",
];

/// A name that C or C++, which includes the header, would read as a keyword with `_`
/// appended, and one starting with a digit with `_` prepended. Appended, since MSVC reads
/// `_inline`, `_asm`, and `_try` as keywords too; a name already ending in `_` takes
Expand Down Expand Up @@ -2148,18 +2156,14 @@ pub(crate) fn escape(name: &str) -> String {
"wchar_t",
"xor",
"xor_eq",
// Macros GNU dialects predefine, as CMake's default `-std=gnu11` and `-std=gnu++17` do.
"i386",
"linux",
"unix",
];
// `out0`, `out1`, ... are a returned tuple's.
let out = name
.strip_prefix("out")
.is_some_and(|n| !n.is_empty() && n.chars().all(|c| c.is_ascii_digit()));
match name.chars().next() {
Some(c) if c.is_ascii_digit() => format!("_{name}"),
_ if out || name.ends_with('_') || KEYWORDS.contains(&name) => format!("{name}_"),
_ if out || name.ends_with('_') || KEYWORDS.contains(&name) || MACROS.contains(&name) => format!("{name}_"),
_ => name.to_string(),
}
}
Expand Down
9 changes: 5 additions & 4 deletions ffi/src/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2535,12 +2535,10 @@ pub(crate) fn escape(name: &str) -> String {
"std",
"self",
];
// Macros GNU dialects predefine, as CMake's default `-std=gnu++17` does. Each name of
// one and trailing `_`s takes another, so `unix` and `unix_` stay apart.
const MACROS: &[&str] = &["i386", "linux", "unix"];
// Each name of a macro and trailing `_`s takes another, so `unix` and `unix_` stay apart.
match name.chars().next() {
Some(c) if c.is_ascii_digit() => format!("_{name}"),
_ if KEYWORDS.contains(&name) || MACROS.contains(&name.trim_end_matches('_')) => format!("{name}_"),
_ if KEYWORDS.contains(&name) || abi::MACROS.contains(&name.trim_end_matches('_')) => format!("{name}_"),
_ => name.to_string(),
}
}
Expand Down Expand Up @@ -2610,6 +2608,9 @@ mod tests {
assert_eq!(escape("unix_"), "unix__");
assert_eq!(escape("linux__"), "linux___");
assert_eq!(escape("unixes"), "unixes");
// And the C library's, which `<cerrno>` and `<cstddef>` define.
assert_eq!(escape("errno"), "errno_");
assert_eq!(escape("offsetof_"), "offsetof__");
// A keyword keeps its single escape, so a field `type_` stays `type_`.
assert_eq!(escape("type_"), "type_");
}
Expand Down
46 changes: 32 additions & 14 deletions ffi/src/glue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,9 +66,13 @@ fn dependency(krate: &str) -> String {
}

/// The name the crate depends on a crate the facade does not re-export by, apart from the
/// facade's even when they share a name.
/// facade's even when they share a name. `bytes` is the crate's own too, which cargo refuses
/// to depend on twice under two names, so it keeps its own.
fn reached(krate: &str) -> String {
format!("{krate}_reached")
match krate {
"bytes" => krate.to_string(),
_ => format!("{krate}_reached"),
}
}

/// The name the crate depends on a foreign type's crate by, apart from the facade's even
Expand Down Expand Up @@ -319,16 +323,28 @@ impl Crate<'_> {
r.krate
));
};
let line = format!("{} = {}\n", reached(&r.krate), paths.dependency(keys)?);
match r.targets == api.targets {
// The crate's own `bytes` keeps the default features the glue builds it with.
let line = format!(
"{} = {}\n",
reached(&r.krate),
paths.dependency(keys, r.krate != "bytes")?
);
// The glue's own `Bytes` is the facade's, on every target: cargo refuses one name
// from two sources, and a `path` or `git` source has one for all targets.
match r.targets == api.targets || r.krate == "bytes" {
true => every.push_str(&line),
false => (r.targets.iter()).for_each(|t| targeted.entry(t).or_default().push_str(&line)),
}
}
let targeted: String = (targeted.into_iter())
.map(|(target, lines)| format!("[target.{target:?}.dependencies]\n{lines}\n"))
.collect();
let dependencies = format!("{dependencies}{foreign}{every}");
// The crate's own `bytes`, unless the facade's stands for it.
let own = match api.reached.iter().any(|r| r.krate == "bytes") {
true => "",
false => "bytes = \"1\"\n",
};
let dependencies = format!("{dependencies}{foreign}{every}{own}");
// The facade's oldest toolchain is the crate's, which cargo sets as rustc compiles it,
// though no older than the crate's edition needs; cargo checks the value's form.
let rust_version = match std::env::var("CARGO_PKG_RUST_VERSION") {
Expand Down Expand Up @@ -356,7 +372,6 @@ impl Crate<'_> {
\n\
[dependencies]\n\
{dependencies}\
bytes = \"1\"\n\
jni-sys = {{ version = \"0.4\", optional = true }}\n\
pyo3-ffi = {{ version = \"0.30\", optional = true, features = [\"abi3-py311\", \"extension-module\"] }}\n\
smallvec = {{ version = \"1.16\", optional = true }}\n\
Expand Down Expand Up @@ -583,14 +598,17 @@ impl Paths {
Ok(relative(out, &lexical(&plain(&spelled))).unwrap_or_else(|| dir.display().to_string()))
}

/// The inline table of the dependency whose keys are `keys`, without default features.
fn dependency(&self, keys: &str) -> Result<String, String> {
/// The inline table of the dependency whose keys are `keys`, without default features
/// when `bare`.
fn dependency(&self, keys: &str, bare: bool) -> Result<String, String> {
let mut table: toml::Table = toml::from_str(&format!("dependency = {{ {keys} }}"))
.map_err(|err| format!("reading the dependency `{keys}`: {err}"))?;
self.relocate(&mut table)?;
let dependency = table.get_mut("dependency").and_then(|d| d.as_table_mut());
let dependency = dependency.expect("the table written above");
dependency.insert("default-features".to_string(), false.into());
if bare {
dependency.insert("default-features".to_string(), false.into());
}
Ok(toml::Value::Table(dependency.clone()).to_string())
}

Expand Down Expand Up @@ -3511,15 +3529,15 @@ fn manifest_paths_are_relative_to_the_export() {
);
assert_eq!(
paths
.dependency(&format!(
"package = \"other\", path = {:?}",
other.display().to_string()
))
.dependency(
&format!("package = \"other\", path = {:?}", other.display().to_string()),
true
)
.unwrap(),
"{ default-features = false, package = \"other\", path = \"../source/other\" }"
);
assert_eq!(
paths.dependency("package = \"x\", version = \"=1.0.0\"").unwrap(),
paths.dependency("package = \"x\", version = \"=1.0.0\"", true).unwrap(),
"{ default-features = false, package = \"x\", version = \"=1.0.0\" }"
);
#[cfg(unix)]
Expand Down
9 changes: 6 additions & 3 deletions ffi/src/kotlin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -489,15 +489,16 @@ class Bytes private constructor(
/// The names the generated code uses unqualified, which no exported type or function may
/// take. The runtime's helper classes, which `_Native` nests, take a leading `__`, which
/// neither an exported type's name nor its `_<Name>` import alias in the glue has, so they
/// need no place here. The runtime's own are the C ABI's: `Native`, `Waker`, `Bytes`, `Mut`,
/// and `Bound` are claimed as `<prefix>_native`, `<prefix>_waker`, `<prefix>_bytes`,
/// `<prefix>_mut`, and `<prefix>_bound`.
/// need no place here. The runtime's own top-level classes do, as one in the package an
/// export of the same name would redeclare: `Bytes`, `Waker`, `Mut`, and `Bound`.
const TAKEN: &[&str] = &[
"Any",
"AutoCloseable",
"Boolean",
"Bound",
"Byte",
"ByteArray",
"Bytes",
"Double",
"DoubleArray",
"Duration",
Expand All @@ -511,6 +512,7 @@ const TAKEN: &[&str] = &[
"Ipv6Addr",
"Long",
"LongArray",
"Mut",
"PathBuf",
"Short",
"ShortArray",
Expand All @@ -527,6 +529,7 @@ const TAKEN: &[&str] = &[
"UShort",
"UShortArray",
"Unit",
"Waker",
"also",
"check",
"error",
Expand Down
15 changes: 14 additions & 1 deletion ffi/surface/api/c.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
#ifdef __cplusplus
extern "C" {
#endif
#define API_CONTRACT ((uint64_t)0x803114fce319dd8dULL)
#define API_CONTRACT ((uint64_t)0x06b4a9cf5dcdd72bULL)
uint64_t api_contract(void);
uint32_t api_target(void);
typedef int32_t api_status;
Expand Down Expand Up @@ -859,6 +859,12 @@ typedef struct api_Knob {
typedef struct api_polar_Turn {
uint8_t part;
} api_polar_Turn;
typedef struct api_macros_Streams {
uint8_t stdin_;
uint8_t stdout_;
uint8_t stderr_;
uint8_t errno_;
} api_macros_Streams;
typedef struct api_clash_TONE {
uint8_t db;
} api_clash_TONE;
Expand Down Expand Up @@ -1403,6 +1409,9 @@ API_MUST_USE api_buf api_polar_Point_debug(const api_polar_Point *self);
void api_polar_Turn_free(api_polar_Turn *self);
bool api_polar_Turn_eq(const api_polar_Turn *a, const api_polar_Turn *b);
API_MUST_USE api_buf api_polar_Turn_debug(const api_polar_Turn *self);
void api_macros_Streams_free(api_macros_Streams *self);
bool api_macros_Streams_eq(const api_macros_Streams *a, const api_macros_Streams *b);
API_MUST_USE api_buf api_macros_Streams_debug(const api_macros_Streams *self);
void api_clash_Tone_free(api_clash_Tone *self);
bool api_clash_Tone_eq(const api_clash_Tone *a, const api_clash_Tone *b);
API_MUST_USE api_buf api_clash_Tone_debug(const api_clash_Tone *self);
Expand Down Expand Up @@ -2442,6 +2451,9 @@ uint64_t api_booth_count(api_Booth booth, api_status *status);
api_Page api_page(void);
uint32_t api_beats(api_Beat beat);
int32_t api_same(double a, double b);
int32_t api_errno(void);
bool api_assert(bool holds);
uint8_t api_offsetof(uint8_t field);
API_MUST_USE bool api_return_values(api_Point point, bool fail, api_Point *point_out, api_buf *out0, api_option_tuple_u8_i16_u32_u64 *out1, api_vec_bytes *out2, api_Error *err);
API_MUST_USE bool api_return_moved(api_Counter *counter, bool fail, api_Counter **out, api_Error *err, api_status *status);
api_NonZeros api_non_zeros(void);
Expand Down Expand Up @@ -2484,6 +2496,7 @@ uint64_t api_crowded(api_Reading reading, api_Stamp a, api_Stamp b, uint64_t n0,
uint64_t api_crammed(api_Moment a, api_Moment b, uint64_t n0, uint64_t n1, uint64_t n2, uint64_t n3, uint64_t n4, uint64_t n5, uint64_t n6, uint64_t n7, uint64_t n8, uint64_t n9, uint64_t n10, uint64_t n11, uint64_t n12, uint64_t n13, uint64_t n14, uint64_t n15, uint64_t n16, uint64_t n17, uint64_t n18, uint64_t n19, uint64_t n20, uint64_t n21, uint64_t n22, uint64_t n23, uint64_t n24, uint64_t n25, uint64_t n26, uint64_t n27, uint64_t n28, uint64_t n29, uint64_t n30, uint64_t n31, uint64_t n32, uint64_t n33, uint64_t n34, uint64_t n35, uint64_t n36, uint64_t n37, uint64_t n38, uint64_t n39, uint64_t n40, uint64_t n41, uint64_t n42, uint64_t n43, uint64_t n44, uint64_t n45, uint64_t n46, uint64_t n47, uint64_t n48, uint64_t n49, uint64_t n50, uint64_t n51, uint64_t n52, uint64_t n53, uint64_t n54, uint64_t n55, uint64_t n56, uint64_t n57, uint64_t n58, uint64_t n59, uint64_t n60, uint64_t n61, uint64_t n62, uint64_t n63, uint64_t n64, uint64_t n65, uint64_t n66, uint64_t n67, uint64_t n68, uint64_t n69, uint64_t n70, uint64_t n71, uint64_t n72, uint64_t n73, uint64_t n74, uint64_t n75, uint64_t n76, uint64_t n77, uint64_t n78, uint64_t n79, uint64_t n80, uint64_t n81, uint64_t n82, uint64_t n83, uint64_t n84, uint64_t n85, uint64_t n86, uint64_t n87, uint64_t n88, uint64_t n89, uint64_t n90, uint64_t n91, uint64_t n92, uint64_t n93, uint64_t n94, uint64_t n95, uint64_t n96, uint64_t n97, uint64_t n98, uint64_t n99, uint64_t n100, uint64_t n101, uint64_t n102, uint64_t n103, uint64_t n104, uint64_t n105, uint64_t n106, uint64_t n107, uint64_t n108, uint64_t n109, uint64_t n110, uint64_t n111, uint64_t n112, uint64_t n113, uint64_t n114, uint64_t n115, uint64_t n116, uint64_t n117, uint64_t n118, uint64_t n119, uint64_t n120, uint64_t n121, uint64_t n122, uint64_t n123);
uint32_t api_jni_one(void);
api_option_polar_Point api_polar_point(uint32_t distance);
uint32_t api_macros_streamed(api_macros_Streams streams, uint8_t errno_);
API_MUST_USE api_clash_Any api_clash_any(api_buf type_url);
api_clash_Candidate api_clash_candidate(uint8_t at);
uint8_t api_clash_ignore(uint8_t arg0, uint8_t arg1, uint8_t arg2);
Expand Down
15 changes: 15 additions & 0 deletions ffi/surface/api/cpp.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -1201,6 +1201,15 @@ namespace api {
friend bool operator!=(const polar_Turn &a__, const polar_Turn &b_);
::api::buf debug() const;
};
struct macros_Streams {
::std::uint8_t stdin_;
::std::uint8_t stdout_;
::std::uint8_t stderr_;
::std::uint8_t errno_;
friend bool operator==(const macros_Streams &a__, const macros_Streams &b_);
friend bool operator!=(const macros_Streams &a__, const macros_Streams &b_);
::api::buf debug() const;
};
struct clash_Tone {
::std::uint16_t hz;
friend bool operator==(const clash_Tone &a__, const clash_Tone &b_);
Expand Down Expand Up @@ -1435,6 +1444,7 @@ namespace api {
inline bool operator==(const Knob &a__, const Knob &b_);
inline bool operator==(const polar_Point &a__, const polar_Point &b_);
inline bool operator==(const polar_Turn &a__, const polar_Turn &b_);
inline bool operator==(const macros_Streams &a__, const macros_Streams &b_);
inline bool operator==(const clash_Tone &a__, const clash_Tone &b_);
inline bool operator==(const clash_TONE &a__, const clash_TONE &b_);
inline bool operator==(const clash_Span &a__, const clash_Span &b_);
Expand Down Expand Up @@ -1750,6 +1760,9 @@ namespace api {
inline ::api::Page page();
inline ::std::uint32_t beats(::std::unique_ptr<::api::Beat> beat);
inline ::std::int32_t same(double a__, double b_);
inline ::std::int32_t errno_();
inline bool assert_(bool holds);
inline ::std::uint8_t offsetof_(::std::uint8_t field);
inline ::api::result<::std::tuple<::api::buf, ::std::optional<::std::tuple<::std::uint8_t, ::std::int16_t, ::std::uint32_t, ::std::uint64_t>>, ::std::vector<::api::buf>>, ::api::Error> return_values(::api::Point &point, bool fail);
inline ::api::result<::api::Counter, ::api::Error> return_moved(::api::Counter counter, bool fail);
inline ::api::NonZeros non_zeros();
Expand Down Expand Up @@ -1780,6 +1793,7 @@ namespace api {
inline ::std::uint64_t crammed(const ::api::Moment &a__, const ::api::Moment &b_, ::std::uint64_t n0, ::std::uint64_t n1, ::std::uint64_t n2, ::std::uint64_t n3, ::std::uint64_t n4, ::std::uint64_t n5, ::std::uint64_t n6, ::std::uint64_t n7, ::std::uint64_t n8, ::std::uint64_t n9, ::std::uint64_t n10, ::std::uint64_t n11, ::std::uint64_t n12, ::std::uint64_t n13, ::std::uint64_t n14, ::std::uint64_t n15, ::std::uint64_t n16, ::std::uint64_t n17, ::std::uint64_t n18, ::std::uint64_t n19, ::std::uint64_t n20, ::std::uint64_t n21, ::std::uint64_t n22, ::std::uint64_t n23, ::std::uint64_t n24, ::std::uint64_t n25, ::std::uint64_t n26, ::std::uint64_t n27, ::std::uint64_t n28, ::std::uint64_t n29, ::std::uint64_t n30, ::std::uint64_t n31, ::std::uint64_t n32, ::std::uint64_t n33, ::std::uint64_t n34, ::std::uint64_t n35, ::std::uint64_t n36, ::std::uint64_t n37, ::std::uint64_t n38, ::std::uint64_t n39, ::std::uint64_t n40, ::std::uint64_t n41, ::std::uint64_t n42, ::std::uint64_t n43, ::std::uint64_t n44, ::std::uint64_t n45, ::std::uint64_t n46, ::std::uint64_t n47, ::std::uint64_t n48, ::std::uint64_t n49, ::std::uint64_t n50, ::std::uint64_t n51, ::std::uint64_t n52, ::std::uint64_t n53, ::std::uint64_t n54, ::std::uint64_t n55, ::std::uint64_t n56, ::std::uint64_t n57, ::std::uint64_t n58, ::std::uint64_t n59, ::std::uint64_t n60, ::std::uint64_t n61, ::std::uint64_t n62, ::std::uint64_t n63, ::std::uint64_t n64, ::std::uint64_t n65, ::std::uint64_t n66, ::std::uint64_t n67, ::std::uint64_t n68, ::std::uint64_t n69, ::std::uint64_t n70, ::std::uint64_t n71, ::std::uint64_t n72, ::std::uint64_t n73, ::std::uint64_t n74, ::std::uint64_t n75, ::std::uint64_t n76, ::std::uint64_t n77, ::std::uint64_t n78, ::std::uint64_t n79, ::std::uint64_t n80, ::std::uint64_t n81, ::std::uint64_t n82, ::std::uint64_t n83, ::std::uint64_t n84, ::std::uint64_t n85, ::std::uint64_t n86, ::std::uint64_t n87, ::std::uint64_t n88, ::std::uint64_t n89, ::std::uint64_t n90, ::std::uint64_t n91, ::std::uint64_t n92, ::std::uint64_t n93, ::std::uint64_t n94, ::std::uint64_t n95, ::std::uint64_t n96, ::std::uint64_t n97, ::std::uint64_t n98, ::std::uint64_t n99, ::std::uint64_t n100, ::std::uint64_t n101, ::std::uint64_t n102, ::std::uint64_t n103, ::std::uint64_t n104, ::std::uint64_t n105, ::std::uint64_t n106, ::std::uint64_t n107, ::std::uint64_t n108, ::std::uint64_t n109, ::std::uint64_t n110, ::std::uint64_t n111, ::std::uint64_t n112, ::std::uint64_t n113, ::std::uint64_t n114, ::std::uint64_t n115, ::std::uint64_t n116, ::std::uint64_t n117, ::std::uint64_t n118, ::std::uint64_t n119, ::std::uint64_t n120, ::std::uint64_t n121, ::std::uint64_t n122, ::std::uint64_t n123);
inline ::std::uint32_t jni_one();
inline ::std::optional<::api::polar_Point> polar_point(::std::uint32_t distance);
inline ::std::uint32_t macros_streamed(const ::api::macros_Streams &streams, ::std::uint8_t errno_);
inline ::api::clash_Any clash_any(::api::input type_url);
inline ::api::clash_Candidate clash_candidate(::std::uint8_t at);
inline ::std::uint8_t clash_ignore(::std::uint8_t arg0, ::std::uint8_t arg1, ::std::uint8_t arg2);
Expand Down Expand Up @@ -2168,6 +2182,7 @@ namespace api {
struct Knob;
struct polar_Point;
struct polar_Turn;
struct macros_Streams;
struct clash_Tone;
struct clash_TONE;
struct clash_Span;
Expand Down
Loading