New type GenericParam represents a generic parameter.

This commit is contained in:
Jason Orendorff
2022-05-07 16:16:42 -05:00
committed by Emilio Cobos Álvarez
parent 13c0a4a2e8
commit c732069b2e
28 changed files with 462 additions and 103 deletions
+2 -19
View File
@@ -383,7 +383,7 @@ impl Enum {
}
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 has_data = false;
@@ -616,24 +616,7 @@ impl Item for Enum {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
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<_>>();
let mappings = self.generic_params.call(self.path.name(), generic_values);
for variant in &self.variants {
if let VariantBody::Body { ref body, .. } = variant.body {
+96 -21
View File
@@ -3,30 +3,95 @@ use std::ops::Deref;
use syn::ext::IdentExt;
use crate::bindgen::cdecl;
use crate::bindgen::config::{Config, Language};
use crate::bindgen::declarationtyperesolver::{DeclarationType, DeclarationTypeResolver};
use crate::bindgen::ir::{ArrayLength, Path, Type};
use crate::bindgen::utilities::IterHelpers;
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)]
pub struct GenericParams(pub Vec<Path>);
pub struct GenericParams(pub Vec<GenericParam>);
impl GenericParams {
pub fn new(generics: &syn::Generics) -> Self {
GenericParams(
generics
.params
.iter()
.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()))
}
_ => None,
})
.collect(),
)
pub fn load(generics: &syn::Generics) -> Result<Self, String> {
let mut params = vec![];
for param in &generics.params {
if let Some(p) = GenericParam::load(param)? {
params.push(p);
}
}
Ok(GenericParams(params))
}
/// Associate each parameter with an argument.
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>(
@@ -41,9 +106,19 @@ impl GenericParams {
if i != 0 {
out.write(", ");
}
write!(out, "typename {}", item);
if with_default {
write!(out, " = void");
match item.ty {
GenericParamType::Type => {
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(">");
@@ -57,9 +132,9 @@ impl GenericParams {
}
impl Deref for GenericParams {
type Target = [Path];
type Target = [GenericParam];
fn deref(&self) -> &[Path] {
fn deref(&self) -> &[GenericParam] {
&self.0
}
}
@@ -168,7 +243,7 @@ impl GenericPath {
for generic in &mut self.generics {
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);
}
}
+1 -1
View File
@@ -35,7 +35,7 @@ impl OpaqueItem {
) -> Result<OpaqueItem, String> {
Ok(Self::new(
path,
GenericParams::new(generics),
GenericParams::load(generics)?,
Cfg::append(mod_cfg, Cfg::load(attrs)),
AnnotationSet::load(attrs).unwrap_or_else(|_| AnnotationSet::new()),
Documentation::load(attrs),
+2 -20
View File
@@ -103,7 +103,7 @@ impl Struct {
Ok(Struct::new(
path,
GenericParams::new(&item.generics),
GenericParams::load(&item.generics)?,
fields,
has_tag_field,
is_enum_variant_body,
@@ -370,25 +370,7 @@ impl Item for Struct {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
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 mappings = self.generic_params.call(self.path.name(), generic_values);
let monomorph = self.specialize(generic_values, &mappings, library.get_config());
out.insert_struct(library, self, monomorph, generic_values.to_owned());
}
+1 -1
View File
@@ -804,7 +804,7 @@ impl Type {
}
}
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 !out.items.contains(path) {
out.items.insert(path.clone());
+2 -19
View File
@@ -37,7 +37,7 @@ impl Typedef {
let path = Path::new(item.ident.unraw().to_string());
Ok(Typedef::new(
path,
GenericParams::new(&item.generics),
GenericParams::load(&item.generics)?,
x,
Cfg::append(mod_cfg, Cfg::load(&item.attrs)),
AnnotationSet::load(&item.attrs)?,
@@ -159,24 +159,7 @@ impl Item for Typedef {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
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 mappings = self.generic_params.call(self.path.name(), generic_values);
let mangled_path = mangle::mangle_path(
&self.path,
+2 -19
View File
@@ -62,7 +62,7 @@ impl Union {
Ok(Union::new(
path,
GenericParams::new(&item.generics),
GenericParams::load(&item.generics)?,
fields,
repr.align,
tuple_union,
@@ -227,24 +227,7 @@ impl Item for Union {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
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 mappings = self.generic_params.call(self.path.name(), generic_values);
let mangled_path = mangle::mangle_path(
&self.path,
+6 -3
View File
@@ -15,8 +15,8 @@ use crate::bindgen::cargo::{Cargo, PackageRef};
use crate::bindgen::config::{Config, ParseConfig};
use crate::bindgen::error::Error;
use crate::bindgen::ir::{
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParams, ItemMap,
OpaqueItem, Path, Static, Struct, Type, Typedef, Union,
AnnotationSet, Cfg, Constant, Documentation, Enum, Function, GenericParam, GenericParams,
ItemMap, OpaqueItem, Path, Static, Struct, Type, Typedef, Union,
};
use crate::bindgen::utilities::{SynAbiHelpers, SynAttributeHelpers, SynItemFnHelpers};
@@ -425,7 +425,10 @@ impl Parse {
pub fn add_std_types(&mut self) {
let mut add_opaque = |path: &str, generic_params: Vec<&str>| {
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(
path,
GenericParams(generic_params),
+17
View File
@@ -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
+17
View File
@@ -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
+21
View File
@@ -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"
+19
View File
@@ -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);
+17
View File
@@ -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
+19
View File
@@ -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);
+15
View File
@@ -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) {}
+17
View File
@@ -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
}
}