Skip to content

Commit

Permalink
Generate arrays with volatile.
Browse files Browse the repository at this point in the history
  • Loading branch information
flaviojs committed May 22, 2024
1 parent b7e744f commit b7b68ef
Show file tree
Hide file tree
Showing 13 changed files with 159 additions and 37 deletions.
13 changes: 8 additions & 5 deletions src/bindgen/cdecl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::io::Write;

use crate::bindgen::config::Layout;
use crate::bindgen::declarationtyperesolver::DeclarationType;
use crate::bindgen::ir::{ConstExpr, Function, GenericArgument, Type};
use crate::bindgen::ir::{ConstExpr, Function, GenericArgument, Type, TypeArray};
use crate::bindgen::language_backend::LanguageBackend;
use crate::bindgen::writer::{ListType, SourceWriter};
use crate::bindgen::{Config, Language};
Expand Down Expand Up @@ -76,7 +76,10 @@ impl CDecl {
t
),
};
let ptr_as_array = Type::Array(x.ty.clone(), ConstExpr::Value(length.to_string()));
let ptr_as_array = Type::Array(TypeArray::new(
x.ty.clone(),
ConstExpr::Value(length.to_string()),
));
cdecl.build_type(&ptr_as_array, x.is_const, config);
cdecl
}
Expand Down Expand Up @@ -167,10 +170,10 @@ impl CDecl {
});
self.build_type(x.ty.as_ref(), x.is_const, config);
}
Type::Array(ref t, ref constant) => {
let len = constant.as_str().to_owned();
Type::Array(ref x) => {
let len = x.len.as_str().to_owned();
self.declarators.push(CDeclarator::Array(len));
self.build_type(t, is_const, config);
self.build_type(x.ty.as_ref(), is_const, config);
}
Type::FuncPtr(ref x) => {
let args = x
Expand Down
6 changes: 3 additions & 3 deletions src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1171,13 +1171,13 @@ impl Enum {
for field in body.fields.iter().skip(skip_fields) {
out.new_line();
match field.ty {
Type::Array(ref ty, ref length) => {
Type::Array(ref x) => {
// arrays are not assignable in C++ so we
// need to manually copy the elements
write!(out, "for (int i = 0; i < {}; i++)", length.as_str());
write!(out, "for (int i = 0; i < {}; i++)", x.len.as_str());
out.open_brace();
write!(out, "::new (&result.{}.{}[i]) (", variant_name, field.name);
language_backend.write_type(out, ty);
language_backend.write_type(out, x.ty.as_ref());
write!(out, ")({}[i]);", arg_renamer(&field.name));
out.close_brace(false);
}
Expand Down
53 changes: 33 additions & 20 deletions src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,17 @@ impl TypePrimitive {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TypeArray {
pub ty: Box<Type>,
pub len: ConstExpr,
}
impl TypeArray {
pub fn new(ty: Box<Type>, len: ConstExpr) -> Self {
Self { ty, len }
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct TypeFuncPtr {
pub ret: Box<Type>,
Expand All @@ -453,7 +464,7 @@ pub enum Type {
Ptr(TypePtr),
Path(TypePath),
Primitive(TypePrimitive),
Array(Box<Type>, ConstExpr),
Array(TypeArray),
FuncPtr(TypeFuncPtr),
}

Expand Down Expand Up @@ -546,7 +557,7 @@ impl Type {
};

let len = ConstExpr::load(len)?;
Type::Array(Box::new(converted), len)
Type::Array(TypeArray::new(Box::new(converted), len))
}
syn::Type::BareFn(ref function) => {
let mut wildcard_counter = 0;
Expand Down Expand Up @@ -602,8 +613,8 @@ impl Type {
Type::Ptr(ref x) => x.is_volatile,
Type::Path(ref x) => x.is_volatile,
Type::Primitive(ref x) => x.is_volatile,
Type::Array(ref x) => x.ty.is_volatile(),
Type::FuncPtr(ref x) => x.is_volatile,
_ => false,
}
}

Expand All @@ -612,8 +623,8 @@ impl Type {
Type::Ptr(ref mut x) => x.is_volatile = is_volatile,
Type::Path(ref mut x) => x.is_volatile = is_volatile,
Type::Primitive(ref mut x) => x.is_volatile = is_volatile,
Type::Array(ref mut x) => x.ty.set_volatile(is_volatile),
Type::FuncPtr(ref mut x) => x.is_volatile = is_volatile,
_ => {}
}
}

Expand Down Expand Up @@ -782,7 +793,9 @@ impl Type {

fn visit_types(&mut self, mut visitor: impl FnMut(&mut Type)) {
match *self {
Type::Array(ref mut ty, ..) | Type::Ptr(TypePtr { ref mut ty, .. }) => visitor(ty),
Type::Array(TypeArray { ref mut ty, .. }) | Type::Ptr(TypePtr { ref mut ty, .. }) => {
visitor(ty)
}
Type::Path(ref mut x) => {
for generic in x.generic_path.generics_mut() {
match *generic {
Expand Down Expand Up @@ -851,10 +864,10 @@ impl Type {
})
}
Type::Primitive(ref x) => Type::Primitive(x.clone()),
Type::Array(ref ty, ref constant) => Type::Array(
Box::new(ty.specialize(mappings)),
constant.specialize(mappings),
),
Type::Array(ref x) => Type::Array(TypeArray::new(
Box::new(x.ty.specialize(mappings)),
x.len.specialize(mappings),
)),
Type::FuncPtr(ref x) => Type::FuncPtr(TypeFuncPtr {
ret: Box::new(x.ret.specialize(mappings)),
args: x
Expand Down Expand Up @@ -907,8 +920,8 @@ impl Type {
}
}
Type::Primitive(..) => {}
Type::Array(ref ty, _) => {
ty.add_dependencies_ignoring_generics(generic_params, library, out);
Type::Array(ref x) => {
x.ty.add_dependencies_ignoring_generics(generic_params, library, out);
}
Type::FuncPtr(ref x) => {
x.ret
Expand Down Expand Up @@ -942,8 +955,8 @@ impl Type {
}
}
Type::Primitive(..) => {}
Type::Array(ref ty, _) => {
ty.add_monomorphs(library, out);
Type::Array(ref x) => {
x.ty.add_monomorphs(library, out);
}
Type::FuncPtr(ref x) => {
x.ret.add_monomorphs(library, out);
Expand All @@ -963,9 +976,9 @@ impl Type {
x.generic_path.rename_for_config(config, generic_params);
}
Type::Primitive(..) => {}
Type::Array(ref mut ty, ref mut len) => {
ty.rename_for_config(config, generic_params);
len.rename_for_config(config);
Type::Array(ref mut x) => {
x.ty.rename_for_config(config, generic_params);
x.len.rename_for_config(config);
}
Type::FuncPtr(ref mut x) => {
x.ret.rename_for_config(config, generic_params);
Expand All @@ -985,8 +998,8 @@ impl Type {
x.generic_path.resolve_declaration_types(resolver);
}
Type::Primitive(..) => {}
Type::Array(ref mut ty, _) => {
ty.resolve_declaration_types(resolver);
Type::Array(ref mut x) => {
x.ty.resolve_declaration_types(resolver);
}
Type::FuncPtr(ref mut x) => {
x.ret.resolve_declaration_types(resolver);
Expand Down Expand Up @@ -1018,8 +1031,8 @@ impl Type {
}
}
Type::Primitive(..) => {}
Type::Array(ref mut ty, _) => {
ty.mangle_paths(monomorphs);
Type::Array(ref mut x) => {
x.ty.mangle_paths(monomorphs);
}
Type::FuncPtr(ref mut x) => {
x.ret.mangle_paths(monomorphs);
Expand Down
8 changes: 7 additions & 1 deletion tests/expectations/volatile.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ typedef volatile int c_vci;

typedef void (*volatile c_vfn)(void);

typedef volatile int c_va2[2];

typedef struct {
volatile int vi;
volatile c_vint vvi;
Expand All @@ -28,6 +30,7 @@ typedef struct {
int *volatile vnni;
volatile int vci;
void (*volatile vfn)(void);
volatile int va2[2];
} S;

typedef union {
Expand All @@ -39,6 +42,7 @@ typedef union {
int *volatile vnni;
volatile int vci;
void (*volatile vfn)(void);
volatile int va2[2];
} U;

extern volatile int g_vi;
Expand All @@ -57,4 +61,6 @@ extern volatile int g_vci;

extern void (*volatile g_vfn)(void);

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, S, U);
extern volatile int g_va2[2];

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, c_va2, S, U);
8 changes: 7 additions & 1 deletion tests/expectations/volatile.compat.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ typedef volatile int c_vci;

typedef void (*volatile c_vfn)(void);

typedef volatile int c_va2[2];

typedef struct {
volatile int vi;
volatile c_vint vvi;
Expand All @@ -28,6 +30,7 @@ typedef struct {
int *volatile vnni;
volatile int vci;
void (*volatile vfn)(void);
volatile int va2[2];
} S;

typedef union {
Expand All @@ -39,6 +42,7 @@ typedef union {
int *volatile vnni;
volatile int vci;
void (*volatile vfn)(void);
volatile int va2[2];
} U;

#ifdef __cplusplus
Expand All @@ -61,7 +65,9 @@ extern volatile int g_vci;

extern void (*volatile g_vfn)(void);

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, S, U);
extern volatile int g_va2[2];

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, c_va2, S, U);

#ifdef __cplusplus
} // extern "C"
Expand Down
8 changes: 7 additions & 1 deletion tests/expectations/volatile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ using c_vci = volatile int;

using c_vfn = void(*volatile )();

using c_va2 = volatile int[2];

struct S {
volatile int vi;
volatile c_vint vvi;
Expand All @@ -29,6 +31,7 @@ struct S {
int *volatile vnni;
volatile int vci;
void (*volatile vfn)();
volatile int va2[2];
};

union U {
Expand All @@ -40,6 +43,7 @@ union U {
int *volatile vnni;
volatile int vci;
void (*volatile vfn)();
volatile int va2[2];
};

extern "C" {
Expand All @@ -60,6 +64,8 @@ extern volatile int g_vci;

extern void (*volatile g_vfn)();

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, S, U);
extern volatile int g_va2[2];

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, c_va2, S, U);

} // extern "C"
8 changes: 7 additions & 1 deletion tests/expectations/volatile.pyx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ cdef extern from *:

ctypedef void (*c_vfn)();

ctypedef int c_va2[2];

ctypedef struct S:
int vi;
c_vint vvi;
Expand All @@ -31,6 +33,7 @@ cdef extern from *:
int *vnni;
int vci;
void (*vfn)();
int va2[2];

ctypedef union U:
int vi;
Expand All @@ -41,6 +44,7 @@ cdef extern from *:
int *vnni;
int vci;
void (*vfn)();
int va2[2];

extern int g_vi;

Expand All @@ -58,4 +62,6 @@ cdef extern from *:

extern void (*g_vfn)();

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, S, U);
extern int g_va2[2];

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, c_va2, S, U);
18 changes: 17 additions & 1 deletion tests/expectations/volatile_both.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ typedef volatile int c_vci;

typedef void (*volatile c_vfn)(void);

typedef volatile int c_va2[2];

typedef struct S {
volatile int vi;
volatile c_vint vvi;
Expand All @@ -28,6 +30,7 @@ typedef struct S {
int *volatile vnni;
volatile int vci;
void (*volatile vfn)(void);
volatile int va2[2];
} S;

typedef union U {
Expand All @@ -39,6 +42,7 @@ typedef union U {
int *volatile vnni;
volatile int vci;
void (*volatile vfn)(void);
volatile int va2[2];
} U;

extern volatile int g_vi;
Expand All @@ -57,4 +61,16 @@ extern volatile int g_vci;

extern void (*volatile g_vfn)(void);

void _export(c_vint, c_vvint, c_vpint, c_vpcint, c_vnzu32, c_vnni, c_vci, c_vfn, struct S, union U);
extern volatile int g_va2[2];

void _export(c_vint,
c_vvint,
c_vpint,
c_vpcint,
c_vnzu32,
c_vnni,
c_vci,
c_vfn,
c_va2,
struct S,
union U);
Loading

0 comments on commit b7b68ef

Please sign in to comment.