New type GenericParam represents a generic parameter.
This commit is contained in:
committed by
Emilio Cobos Álvarez
parent
13c0a4a2e8
commit
c732069b2e
@@ -383,7 +383,7 @@ impl Enum {
|
|||||||
}
|
}
|
||||||
|
|
||||||
let path = Path::new(item.ident.unraw().to_string());
|
let path = Path::new(item.ident.unraw().to_string());
|
||||||
let generic_params = GenericParams::new(&item.generics);
|
let generic_params = GenericParams::load(&item.generics)?;
|
||||||
|
|
||||||
let mut variants = Vec::new();
|
let mut variants = Vec::new();
|
||||||
let mut has_data = false;
|
let mut has_data = false;
|
||||||
@@ -616,24 +616,7 @@ impl Item for Enum {
|
|||||||
library: &Library,
|
library: &Library,
|
||||||
out: &mut Monomorphs,
|
out: &mut Monomorphs,
|
||||||
) {
|
) {
|
||||||
assert!(
|
let mappings = self.generic_params.call(self.path.name(), generic_values);
|
||||||
self.generic_params.len() > 0,
|
|
||||||
"{} is not generic",
|
|
||||||
self.path.name()
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
self.generic_params.len() == generic_values.len(),
|
|
||||||
"{} has {} params but is being instantiated with {} values",
|
|
||||||
self.path.name(),
|
|
||||||
self.generic_params.len(),
|
|
||||||
generic_values.len(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mappings = self
|
|
||||||
.generic_params
|
|
||||||
.iter()
|
|
||||||
.zip(generic_values.iter())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
for variant in &self.variants {
|
for variant in &self.variants {
|
||||||
if let VariantBody::Body { ref body, .. } = variant.body {
|
if let VariantBody::Body { ref body, .. } = variant.body {
|
||||||
|
|||||||
@@ -3,30 +3,95 @@ use std::ops::Deref;
|
|||||||
|
|
||||||
use syn::ext::IdentExt;
|
use syn::ext::IdentExt;
|
||||||
|
|
||||||
|
use crate::bindgen::cdecl;
|
||||||
use crate::bindgen::config::{Config, Language};
|
use crate::bindgen::config::{Config, Language};
|
||||||
use crate::bindgen::declarationtyperesolver::{DeclarationType, DeclarationTypeResolver};
|
use crate::bindgen::declarationtyperesolver::{DeclarationType, DeclarationTypeResolver};
|
||||||
use crate::bindgen::ir::{ArrayLength, Path, Type};
|
use crate::bindgen::ir::{ArrayLength, Path, Type};
|
||||||
use crate::bindgen::utilities::IterHelpers;
|
use crate::bindgen::utilities::IterHelpers;
|
||||||
use crate::bindgen::writer::{Source, SourceWriter};
|
use crate::bindgen::writer::{Source, SourceWriter};
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub enum GenericParamType {
|
||||||
|
Type,
|
||||||
|
Const(Type),
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
|
pub struct GenericParam {
|
||||||
|
name: Path,
|
||||||
|
ty: GenericParamType,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl GenericParam {
|
||||||
|
pub fn new_type_param(name: &str) -> Self {
|
||||||
|
GenericParam {
|
||||||
|
name: Path::new(name),
|
||||||
|
ty: GenericParamType::Type,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn load(param: &syn::GenericParam) -> Result<Option<Self>, String> {
|
||||||
|
match *param {
|
||||||
|
syn::GenericParam::Type(syn::TypeParam { ref ident, .. }) => Ok(Some(GenericParam {
|
||||||
|
name: Path::new(ident.unraw().to_string()),
|
||||||
|
ty: GenericParamType::Type,
|
||||||
|
})),
|
||||||
|
|
||||||
|
syn::GenericParam::Lifetime(_) => Ok(None),
|
||||||
|
|
||||||
|
syn::GenericParam::Const(syn::ConstParam {
|
||||||
|
ref ident, ref ty, ..
|
||||||
|
}) => match Type::load(ty)? {
|
||||||
|
None => {
|
||||||
|
// A type that evaporates, like PhantomData.
|
||||||
|
Err(format!("unsupported const generic type: {:?}", ty))
|
||||||
|
}
|
||||||
|
Some(ty) => Ok(Some(GenericParam {
|
||||||
|
name: Path::new(ident.unraw().to_string()),
|
||||||
|
ty: GenericParamType::Const(ty),
|
||||||
|
})),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn name(&self) -> &Path {
|
||||||
|
&self.name
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default, Debug, Clone)]
|
#[derive(Default, Debug, Clone)]
|
||||||
pub struct GenericParams(pub Vec<Path>);
|
pub struct GenericParams(pub Vec<GenericParam>);
|
||||||
|
|
||||||
impl GenericParams {
|
impl GenericParams {
|
||||||
pub fn new(generics: &syn::Generics) -> Self {
|
pub fn load(generics: &syn::Generics) -> Result<Self, String> {
|
||||||
GenericParams(
|
let mut params = vec![];
|
||||||
generics
|
for param in &generics.params {
|
||||||
.params
|
if let Some(p) = GenericParam::load(param)? {
|
||||||
.iter()
|
params.push(p);
|
||||||
.filter_map(|x| match *x {
|
}
|
||||||
syn::GenericParam::Type(syn::TypeParam { ref ident, .. })
|
}
|
||||||
| syn::GenericParam::Const(syn::ConstParam { ref ident, .. }) => {
|
|
||||||
Some(Path::new(ident.unraw().to_string()))
|
Ok(GenericParams(params))
|
||||||
}
|
}
|
||||||
_ => None,
|
|
||||||
})
|
/// Associate each parameter with an argument.
|
||||||
.collect(),
|
pub fn call<'out>(
|
||||||
)
|
&'out self,
|
||||||
|
item_name: &str,
|
||||||
|
arguments: &'out [GenericArgument],
|
||||||
|
) -> Vec<(&'out Path, &'out GenericArgument)> {
|
||||||
|
assert!(self.len() > 0, "{} is not generic", item_name);
|
||||||
|
assert!(
|
||||||
|
self.len() == arguments.len(),
|
||||||
|
"{} has {} params but is being instantiated with {} values",
|
||||||
|
item_name,
|
||||||
|
self.len(),
|
||||||
|
arguments.len(),
|
||||||
|
);
|
||||||
|
self.iter()
|
||||||
|
.map(|param| param.name())
|
||||||
|
.zip(arguments.iter())
|
||||||
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn write_internal<F: Write>(
|
fn write_internal<F: Write>(
|
||||||
@@ -41,9 +106,19 @@ impl GenericParams {
|
|||||||
if i != 0 {
|
if i != 0 {
|
||||||
out.write(", ");
|
out.write(", ");
|
||||||
}
|
}
|
||||||
write!(out, "typename {}", item);
|
match item.ty {
|
||||||
if with_default {
|
GenericParamType::Type => {
|
||||||
write!(out, " = void");
|
write!(out, "typename {}", item.name);
|
||||||
|
if with_default {
|
||||||
|
write!(out, " = void");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
GenericParamType::Const(ref ty) => {
|
||||||
|
cdecl::write_field(out, ty, item.name.name(), config);
|
||||||
|
if with_default {
|
||||||
|
write!(out, " = 0");
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
out.write(">");
|
out.write(">");
|
||||||
@@ -57,9 +132,9 @@ impl GenericParams {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Deref for GenericParams {
|
impl Deref for GenericParams {
|
||||||
type Target = [Path];
|
type Target = [GenericParam];
|
||||||
|
|
||||||
fn deref(&self) -> &[Path] {
|
fn deref(&self) -> &[GenericParam] {
|
||||||
&self.0
|
&self.0
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -168,7 +243,7 @@ impl GenericPath {
|
|||||||
for generic in &mut self.generics {
|
for generic in &mut self.generics {
|
||||||
generic.rename_for_config(config, generic_params);
|
generic.rename_for_config(config, generic_params);
|
||||||
}
|
}
|
||||||
if !generic_params.contains(&self.path) {
|
if !generic_params.iter().any(|param| param.name == self.path) {
|
||||||
config.export.rename(&mut self.export_name);
|
config.export.rename(&mut self.export_name);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -35,7 +35,7 @@ impl OpaqueItem {
|
|||||||
) -> Result<OpaqueItem, String> {
|
) -> Result<OpaqueItem, String> {
|
||||||
Ok(Self::new(
|
Ok(Self::new(
|
||||||
path,
|
path,
|
||||||
GenericParams::new(generics),
|
GenericParams::load(generics)?,
|
||||||
Cfg::append(mod_cfg, Cfg::load(attrs)),
|
Cfg::append(mod_cfg, Cfg::load(attrs)),
|
||||||
AnnotationSet::load(attrs).unwrap_or_else(|_| AnnotationSet::new()),
|
AnnotationSet::load(attrs).unwrap_or_else(|_| AnnotationSet::new()),
|
||||||
Documentation::load(attrs),
|
Documentation::load(attrs),
|
||||||
|
|||||||
@@ -103,7 +103,7 @@ impl Struct {
|
|||||||
|
|
||||||
Ok(Struct::new(
|
Ok(Struct::new(
|
||||||
path,
|
path,
|
||||||
GenericParams::new(&item.generics),
|
GenericParams::load(&item.generics)?,
|
||||||
fields,
|
fields,
|
||||||
has_tag_field,
|
has_tag_field,
|
||||||
is_enum_variant_body,
|
is_enum_variant_body,
|
||||||
@@ -370,25 +370,7 @@ impl Item for Struct {
|
|||||||
library: &Library,
|
library: &Library,
|
||||||
out: &mut Monomorphs,
|
out: &mut Monomorphs,
|
||||||
) {
|
) {
|
||||||
assert!(
|
let mappings = self.generic_params.call(self.path.name(), generic_values);
|
||||||
self.generic_params.len() > 0,
|
|
||||||
"{} is not generic",
|
|
||||||
self.path
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
self.generic_params.len() == generic_values.len(),
|
|
||||||
"{} has {} params but is being instantiated with {} values",
|
|
||||||
self.path,
|
|
||||||
self.generic_params.len(),
|
|
||||||
generic_values.len(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mappings = self
|
|
||||||
.generic_params
|
|
||||||
.iter()
|
|
||||||
.zip(generic_values.iter())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let monomorph = self.specialize(generic_values, &mappings, library.get_config());
|
let monomorph = self.specialize(generic_values, &mappings, library.get_config());
|
||||||
out.insert_struct(library, self, monomorph, generic_values.to_owned());
|
out.insert_struct(library, self, monomorph, generic_values.to_owned());
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -804,7 +804,7 @@ impl Type {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
let path = generic.path();
|
let path = generic.path();
|
||||||
if !generic_params.contains(path) {
|
if !generic_params.iter().any(|param| param.name() == path) {
|
||||||
if let Some(items) = library.get_items(path) {
|
if let Some(items) = library.get_items(path) {
|
||||||
if !out.items.contains(path) {
|
if !out.items.contains(path) {
|
||||||
out.items.insert(path.clone());
|
out.items.insert(path.clone());
|
||||||
|
|||||||
@@ -37,7 +37,7 @@ impl Typedef {
|
|||||||
let path = Path::new(item.ident.unraw().to_string());
|
let path = Path::new(item.ident.unraw().to_string());
|
||||||
Ok(Typedef::new(
|
Ok(Typedef::new(
|
||||||
path,
|
path,
|
||||||
GenericParams::new(&item.generics),
|
GenericParams::load(&item.generics)?,
|
||||||
x,
|
x,
|
||||||
Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
|
Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
|
||||||
AnnotationSet::load(&item.attrs)?,
|
AnnotationSet::load(&item.attrs)?,
|
||||||
@@ -159,24 +159,7 @@ impl Item for Typedef {
|
|||||||
library: &Library,
|
library: &Library,
|
||||||
out: &mut Monomorphs,
|
out: &mut Monomorphs,
|
||||||
) {
|
) {
|
||||||
assert!(
|
let mappings = self.generic_params.call(self.path.name(), generic_values);
|
||||||
self.generic_params.len() > 0,
|
|
||||||
"{} is not generic",
|
|
||||||
self.path
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
self.generic_params.len() == generic_values.len(),
|
|
||||||
"{} has {} params but is being instantiated with {} values",
|
|
||||||
self.path,
|
|
||||||
self.generic_params.len(),
|
|
||||||
generic_values.len(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mappings = self
|
|
||||||
.generic_params
|
|
||||||
.iter()
|
|
||||||
.zip(generic_values.iter())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let mangled_path = mangle::mangle_path(
|
let mangled_path = mangle::mangle_path(
|
||||||
&self.path,
|
&self.path,
|
||||||
|
|||||||
+2
-19
@@ -62,7 +62,7 @@ impl Union {
|
|||||||
|
|
||||||
Ok(Union::new(
|
Ok(Union::new(
|
||||||
path,
|
path,
|
||||||
GenericParams::new(&item.generics),
|
GenericParams::load(&item.generics)?,
|
||||||
fields,
|
fields,
|
||||||
repr.align,
|
repr.align,
|
||||||
tuple_union,
|
tuple_union,
|
||||||
@@ -227,24 +227,7 @@ impl Item for Union {
|
|||||||
library: &Library,
|
library: &Library,
|
||||||
out: &mut Monomorphs,
|
out: &mut Monomorphs,
|
||||||
) {
|
) {
|
||||||
assert!(
|
let mappings = self.generic_params.call(self.path.name(), generic_values);
|
||||||
self.generic_params.len() > 0,
|
|
||||||
"{} is not generic",
|
|
||||||
self.path
|
|
||||||
);
|
|
||||||
assert!(
|
|
||||||
self.generic_params.len() == generic_values.len(),
|
|
||||||
"{} has {} params but is being instantiated with {} values",
|
|
||||||
self.path,
|
|
||||||
self.generic_params.len(),
|
|
||||||
generic_values.len(),
|
|
||||||
);
|
|
||||||
|
|
||||||
let mappings = self
|
|
||||||
.generic_params
|
|
||||||
.iter()
|
|
||||||
.zip(generic_values.iter())
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let mangled_path = mangle::mangle_path(
|
let mangled_path = mangle::mangle_path(
|
||||||
&self.path,
|
&self.path,
|
||||||
|
|||||||
@@ -15,8 +15,8 @@ use crate::bindgen::cargo::{Cargo, PackageRef};
|
|||||||
use crate::bindgen::config::{Config, ParseConfig};
|
use crate::bindgen::config::{Config, ParseConfig};
|
||||||
use crate::bindgen::error::Error;
|
use crate::bindgen::error::Error;
|
||||||
use crate::bindgen::ir::{
|
use crate::bindgen::ir::{
|
||||||
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemMap,
|
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParam, GenericParams,
|
||||||
OpaqueItem, Path, Static, Struct, Type, Typedef, Union,
|
ItemMap, OpaqueItem, Path, Static, Struct, Type, Typedef, Union,
|
||||||
};
|
};
|
||||||
use crate::bindgen::utilities::{SynAbiHelpers, SynAttributeHelpers, SynItemFnHelpers};
|
use crate::bindgen::utilities::{SynAbiHelpers, SynAttributeHelpers, SynItemFnHelpers};
|
||||||
|
|
||||||
@@ -425,7 +425,10 @@ impl Parse {
|
|||||||
pub fn add_std_types(&mut self) {
|
pub fn add_std_types(&mut self) {
|
||||||
let mut add_opaque = |path: &str, generic_params: Vec<&str>| {
|
let mut add_opaque = |path: &str, generic_params: Vec<&str>| {
|
||||||
let path = Path::new(path);
|
let path = Path::new(path);
|
||||||
let generic_params: Vec<_> = generic_params.into_iter().map(Path::new).collect();
|
let generic_params: Vec<_> = generic_params
|
||||||
|
.into_iter()
|
||||||
|
.map(GenericParam::new_type_param)
|
||||||
|
.collect();
|
||||||
self.opaque_items.try_insert(OpaqueItem::new(
|
self.opaque_items.try_insert(OpaqueItem::new(
|
||||||
path,
|
path,
|
||||||
GenericParams(generic_params),
|
GenericParams(generic_params),
|
||||||
|
|||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define TITLE_SIZE 80
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_40[40];
|
||||||
|
|
||||||
|
typedef struct Book {
|
||||||
|
CArrayString_TITLE_SIZE title;
|
||||||
|
CArrayString_40 author;
|
||||||
|
} Book;
|
||||||
|
|
||||||
|
void root(struct Book *a);
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define TITLE_SIZE 80
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_40[40];
|
||||||
|
|
||||||
|
typedef struct Book {
|
||||||
|
CArrayString_TITLE_SIZE title;
|
||||||
|
CArrayString_40 author;
|
||||||
|
} Book;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
void root(struct Book *a);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif // __cplusplus
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define TITLE_SIZE 80
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_40[40];
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
CArrayString_TITLE_SIZE title;
|
||||||
|
CArrayString_40 author;
|
||||||
|
} Book;
|
||||||
|
|
||||||
|
void root(Book *a);
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define TITLE_SIZE 80
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_40[40];
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
CArrayString_TITLE_SIZE title;
|
||||||
|
CArrayString_40 author;
|
||||||
|
} Book;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
void root(Book *a);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif // __cplusplus
|
||||||
@@ -0,0 +1,21 @@
|
|||||||
|
#include <cstdarg>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ostream>
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
constexpr static const uintptr_t TITLE_SIZE = 80;
|
||||||
|
|
||||||
|
template<uintptr_t CAP>
|
||||||
|
using CArrayString = int8_t[CAP];
|
||||||
|
|
||||||
|
struct Book {
|
||||||
|
CArrayString<TITLE_SIZE> title;
|
||||||
|
CArrayString<40> author;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
void root(Book *a);
|
||||||
|
|
||||||
|
} // extern "C"
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
|
||||||
|
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
|
||||||
|
cdef extern from *:
|
||||||
|
ctypedef bint bool
|
||||||
|
ctypedef struct va_list
|
||||||
|
|
||||||
|
cdef extern from *:
|
||||||
|
|
||||||
|
const uintptr_t TITLE_SIZE # = 80
|
||||||
|
|
||||||
|
ctypedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];
|
||||||
|
|
||||||
|
ctypedef int8_t CArrayString_40[40];
|
||||||
|
|
||||||
|
ctypedef struct Book:
|
||||||
|
CArrayString_TITLE_SIZE title;
|
||||||
|
CArrayString_40 author;
|
||||||
|
|
||||||
|
void root(Book *a);
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define TITLE_SIZE 80
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_40[40];
|
||||||
|
|
||||||
|
struct Book {
|
||||||
|
CArrayString_TITLE_SIZE title;
|
||||||
|
CArrayString_40 author;
|
||||||
|
};
|
||||||
|
|
||||||
|
void root(struct Book *a);
|
||||||
@@ -0,0 +1,25 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
#define TITLE_SIZE 80
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];
|
||||||
|
|
||||||
|
typedef int8_t CArrayString_40[40];
|
||||||
|
|
||||||
|
struct Book {
|
||||||
|
CArrayString_TITLE_SIZE title;
|
||||||
|
CArrayString_40 author;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
void root(struct Book *a);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif // __cplusplus
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
|
||||||
|
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
|
||||||
|
cdef extern from *:
|
||||||
|
ctypedef bint bool
|
||||||
|
ctypedef struct va_list
|
||||||
|
|
||||||
|
cdef extern from *:
|
||||||
|
|
||||||
|
const uintptr_t TITLE_SIZE # = 80
|
||||||
|
|
||||||
|
ctypedef int8_t CArrayString_TITLE_SIZE[TITLE_SIZE];
|
||||||
|
|
||||||
|
ctypedef int8_t CArrayString_40[40];
|
||||||
|
|
||||||
|
cdef struct Book:
|
||||||
|
CArrayString_TITLE_SIZE title;
|
||||||
|
CArrayString_40 author;
|
||||||
|
|
||||||
|
void root(Book *a);
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct ArrayVec_____u8__100 {
|
||||||
|
uint8_t *xs[100];
|
||||||
|
uint32_t len;
|
||||||
|
} ArrayVec_____u8__100;
|
||||||
|
|
||||||
|
int32_t push(struct ArrayVec_____u8__100 *v, uint8_t *elem);
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct ArrayVec_____u8__100 {
|
||||||
|
uint8_t *xs[100];
|
||||||
|
uint32_t len;
|
||||||
|
} ArrayVec_____u8__100;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
int32_t push(struct ArrayVec_____u8__100 *v, uint8_t *elem);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif // __cplusplus
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t *xs[100];
|
||||||
|
uint32_t len;
|
||||||
|
} ArrayVec_____u8__100;
|
||||||
|
|
||||||
|
int32_t push(ArrayVec_____u8__100 *v, uint8_t *elem);
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
uint8_t *xs[100];
|
||||||
|
uint32_t len;
|
||||||
|
} ArrayVec_____u8__100;
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
int32_t push(ArrayVec_____u8__100 *v, uint8_t *elem);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif // __cplusplus
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#include <cstdarg>
|
||||||
|
#include <cstdint>
|
||||||
|
#include <cstdlib>
|
||||||
|
#include <ostream>
|
||||||
|
#include <new>
|
||||||
|
|
||||||
|
template<typename T, uintptr_t CAP>
|
||||||
|
struct ArrayVec {
|
||||||
|
T xs[CAP];
|
||||||
|
uint32_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern "C" {
|
||||||
|
|
||||||
|
int32_t push(ArrayVec<uint8_t*, 100> *v, uint8_t *elem);
|
||||||
|
|
||||||
|
} // extern "C"
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
|
||||||
|
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
|
||||||
|
cdef extern from *:
|
||||||
|
ctypedef bint bool
|
||||||
|
ctypedef struct va_list
|
||||||
|
|
||||||
|
cdef extern from *:
|
||||||
|
|
||||||
|
ctypedef struct ArrayVec_____u8__100:
|
||||||
|
uint8_t *xs[100];
|
||||||
|
uint32_t len;
|
||||||
|
|
||||||
|
int32_t push(ArrayVec_____u8__100 *v, uint8_t *elem);
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct ArrayVec_____u8__100 {
|
||||||
|
uint8_t *xs[100];
|
||||||
|
uint32_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
int32_t push(struct ArrayVec_____u8__100 *v, uint8_t *elem);
|
||||||
@@ -0,0 +1,19 @@
|
|||||||
|
#include <stdarg.h>
|
||||||
|
#include <stdbool.h>
|
||||||
|
#include <stdint.h>
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
struct ArrayVec_____u8__100 {
|
||||||
|
uint8_t *xs[100];
|
||||||
|
uint32_t len;
|
||||||
|
};
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
extern "C" {
|
||||||
|
#endif // __cplusplus
|
||||||
|
|
||||||
|
int32_t push(struct ArrayVec_____u8__100 *v, uint8_t *elem);
|
||||||
|
|
||||||
|
#ifdef __cplusplus
|
||||||
|
} // extern "C"
|
||||||
|
#endif // __cplusplus
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
|
||||||
|
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
|
||||||
|
cdef extern from *:
|
||||||
|
ctypedef bint bool
|
||||||
|
ctypedef struct va_list
|
||||||
|
|
||||||
|
cdef extern from *:
|
||||||
|
|
||||||
|
cdef struct ArrayVec_____u8__100:
|
||||||
|
uint8_t *xs[100];
|
||||||
|
uint32_t len;
|
||||||
|
|
||||||
|
int32_t push(ArrayVec_____u8__100 *v, uint8_t *elem);
|
||||||
@@ -0,0 +1,15 @@
|
|||||||
|
#[repr(transparent)]
|
||||||
|
pub struct CArrayString<const CAP: usize> {
|
||||||
|
pub chars: [i8; CAP],
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const TITLE_SIZE: usize = 80;
|
||||||
|
|
||||||
|
#[repr(C)]
|
||||||
|
pub struct Book {
|
||||||
|
pub title: CArrayString<TITLE_SIZE>,
|
||||||
|
pub author: CArrayString<40>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub extern "C" fn root(a: *mut Book) {}
|
||||||
@@ -0,0 +1,17 @@
|
|||||||
|
#[repr(C)]
|
||||||
|
pub struct ArrayVec<T, const CAP: usize> {
|
||||||
|
// the `len` first elements of the array are initialized
|
||||||
|
xs: [T; CAP],
|
||||||
|
len: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[no_mangle]
|
||||||
|
pub unsafe extern "C" fn push(v: *mut ArrayVec<*mut u8, 100>, elem: *mut u8) -> i32 {
|
||||||
|
if (*v).len < 100 {
|
||||||
|
(*v).xs[(*v).len] = elem;
|
||||||
|
(*v).len += 1;
|
||||||
|
1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user