Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add an option for converting usize/isize into size_t/ptrdiff_t #606

Merged
merged 1 commit into from
Oct 27, 2020
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
4 changes: 4 additions & 0 deletions docs.md
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,10 @@ rename_args = "PascalCase"
# default: "None"
sort_by = "Name"

# If this option is true `usize` and `isize` will be converted into `size_t` and `ptrdiff_t`
# instead of `uintptr_t` and `intptr_t` respectively.
usize_is_size_t = true

[struct]
# A rule to use to rename struct field names. The renaming assumes the input is
# the Rust standard snake_case, however it acccepts all the different rename_args
Expand Down
8 changes: 8 additions & 0 deletions src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,13 +169,21 @@ impl Bindings {
out.new_line();
out.write("#include <stdbool.h>");
out.new_line();
if self.config.usize_is_size_t {
out.write("#include <stddef.h>");
out.new_line();
}
out.write("#include <stdint.h>");
out.new_line();
out.write("#include <stdlib.h>");
out.new_line();
} else {
out.write("#include <cstdarg>");
out.new_line();
if self.config.usize_is_size_t {
out.write("#include <cstddef>");
out.new_line();
}
out.write("#include <cstdint>");
out.new_line();
out.write("#include <cstdlib>");
Expand Down
38 changes: 19 additions & 19 deletions src/bindgen/cdecl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,17 +51,17 @@ impl CDecl {
}
}

fn from_type(t: &Type) -> CDecl {
fn from_type(t: &Type, config: &Config) -> CDecl {
let mut cdecl = CDecl::new();
cdecl.build_type(t, false);
cdecl.build_type(t, false, config);
cdecl
}

fn from_func_arg(t: &Type, array_length: Option<&str>) -> CDecl {
fn from_func_arg(t: &Type, array_length: Option<&str>, config: &Config) -> CDecl {
let mut cdecl = CDecl::new();
let length = match array_length {
Some(l) => l,
None => return CDecl::from_type(t),
None => return CDecl::from_type(t, config),
};
let (ty, is_const) = match t {
Type::Ptr { ty, is_const, .. } => (ty, is_const),
Expand All @@ -71,33 +71,33 @@ impl CDecl {
),
};
let ptr_as_array = Type::Array(ty.clone(), ArrayLength::Value(length.to_string()));
cdecl.build_type(&ptr_as_array, *is_const);
cdecl.build_type(&ptr_as_array, *is_const, config);
cdecl
}

fn from_func(f: &Function, layout_vertical: bool) -> CDecl {
fn from_func(f: &Function, layout_vertical: bool, config: &Config) -> CDecl {
let mut cdecl = CDecl::new();
cdecl.build_func(f, layout_vertical);
cdecl.build_func(f, layout_vertical, config);
cdecl
}

fn build_func(&mut self, f: &Function, layout_vertical: bool) {
fn build_func(&mut self, f: &Function, layout_vertical: bool, config: &Config) {
let args = f
.args
.iter()
.map(|arg| {
(
arg.name.clone(),
CDecl::from_func_arg(&arg.ty, arg.array_length.as_deref()),
CDecl::from_func_arg(&arg.ty, arg.array_length.as_deref(), config),
)
})
.collect();
self.declarators
.push(CDeclarator::Func(args, layout_vertical));
self.build_type(&f.ret, false);
self.build_type(&f.ret, false, config);
}

fn build_type(&mut self, t: &Type, is_const: bool) {
fn build_type(&mut self, t: &Type, is_const: bool, config: &Config) {
match t {
Type::Path(ref generic) => {
if is_const {
Expand Down Expand Up @@ -138,7 +138,7 @@ impl CDecl {
"error generating cdecl for {:?}",
t
);
self.type_name = p.to_string();
self.type_name = p.to_repr_c(config).to_string();
}
Type::Ptr {
ref ty,
Expand All @@ -151,25 +151,25 @@ impl CDecl {
is_nullable: *is_nullable,
is_ref: *is_ref,
});
self.build_type(ty, *ptr_is_const);
self.build_type(ty, *ptr_is_const, config);
}
Type::Array(ref t, ref constant) => {
let len = constant.as_str().to_owned();
self.declarators.push(CDeclarator::Array(len));
self.build_type(t, is_const);
self.build_type(t, is_const, config);
}
Type::FuncPtr(ref ret, ref args) => {
let args = args
.iter()
.map(|(ref name, ref ty)| (name.clone(), CDecl::from_type(ty)))
.map(|(ref name, ref ty)| (name.clone(), CDecl::from_type(ty, config)))
.collect();
self.declarators.push(CDeclarator::Ptr {
is_const: false,
is_nullable: true,
is_ref: false,
});
self.declarators.push(CDeclarator::Func(args, false));
self.build_type(ret, false);
self.build_type(ret, false, config);
}
}
}
Expand Down Expand Up @@ -307,13 +307,13 @@ pub fn write_func<F: Write>(
layout_vertical: bool,
config: &Config,
) {
CDecl::from_func(f, layout_vertical).write(out, Some(f.path().name()), config);
CDecl::from_func(f, layout_vertical, config).write(out, Some(f.path().name()), config);
}

pub fn write_field<F: Write>(out: &mut SourceWriter<F>, t: &Type, ident: &str, config: &Config) {
CDecl::from_type(t).write(out, Some(ident), config);
CDecl::from_type(t, config).write(out, Some(ident), config);
}

pub fn write_type<F: Write>(out: &mut SourceWriter<F>, t: &Type, config: &Config) {
CDecl::from_type(t).write(out, None, config);
CDecl::from_type(t, config).write(out, None, config);
}
4 changes: 4 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -840,6 +840,9 @@ pub struct Config {
pub style: Style,
/// Default sort key for functions and constants.
pub sort_by: SortKey,
/// If this option is true `usize` and `isize` will be converted into `size_t` and `ptrdiff_t`
/// instead of `uintptr_t` and `intptr_t` respectively.
pub usize_is_size_t: bool,
/// The configuration options for parsing
pub parse: ParseConfig,
/// The configuration options for exporting
Expand Down Expand Up @@ -894,6 +897,7 @@ impl Default for Config {
language: Language::Cxx,
cpp_compat: false,
style: Style::Type,
usize_is_size_t: false,
sort_by: SortKey::None,
macro_expansion: Default::default(),
parse: ParseConfig::default(),
Expand Down
15 changes: 2 additions & 13 deletions src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{
AnnotationSet, AnnotationValue, Cfg, ConditionWrite, Documentation, GenericParams, GenericPath,
Item, ItemContainer, Path, Repr, ReprStyle, ReprType, Struct, ToCondition, Type,
Item, ItemContainer, Path, Repr, ReprStyle, Struct, ToCondition, Type,
};
use crate::bindgen::library::Library;
use crate::bindgen::mangle;
Expand Down Expand Up @@ -595,18 +595,7 @@ impl Item for Enum {

impl Source for Enum {
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
let size = self.repr.ty.map(|ty| match ty {
ReprType::USize => "uintptr_t",
ReprType::U64 => "uint64_t",
ReprType::U32 => "uint32_t",
ReprType::U16 => "uint16_t",
ReprType::U8 => "uint8_t",
ReprType::ISize => "intptr_t",
ReprType::I64 => "int64_t",
ReprType::I32 => "int32_t",
ReprType::I16 => "int16_t",
ReprType::I8 => "int8_t",
});
let size = self.repr.ty.map(|ty| ty.to_primitive().to_repr_c(config));

let condition = self.cfg.to_condition(config);
condition.write_before(config, out);
Expand Down
19 changes: 19 additions & 0 deletions src/bindgen/ir/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use crate::bindgen::ir::ty::PrimitiveType;

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ReprStyle {
Rust,
Expand Down Expand Up @@ -29,6 +31,23 @@ pub enum ReprType {
ISize,
}

impl ReprType {
pub(crate) fn to_primitive(self) -> PrimitiveType {
match self {
ReprType::U8 => PrimitiveType::UInt8,
ReprType::U16 => PrimitiveType::UInt16,
ReprType::U32 => PrimitiveType::UInt32,
ReprType::U64 => PrimitiveType::UInt64,
ReprType::USize => PrimitiveType::USize,
ReprType::I8 => PrimitiveType::Int8,
ReprType::I16 => PrimitiveType::Int16,
ReprType::I32 => PrimitiveType::Int32,
ReprType::I64 => PrimitiveType::Int64,
ReprType::ISize => PrimitiveType::ISize,
}
}
}

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ReprAlign {
Packed,
Expand Down
11 changes: 3 additions & 8 deletions src/bindgen/ir/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */

use std::fmt;
use std::io::Write;

use crate::bindgen::cdecl;
Expand Down Expand Up @@ -123,7 +122,7 @@ impl PrimitiveType {
}
}

pub fn to_repr_c(&self) -> &'static str {
pub fn to_repr_c(&self, config: &Config) -> &'static str {
match *self {
PrimitiveType::Void => "void",
PrimitiveType::Bool => "bool",
Expand All @@ -147,11 +146,13 @@ impl PrimitiveType {
PrimitiveType::UInt => "unsigned int",
PrimitiveType::ULong => "unsigned long",
PrimitiveType::ULongLong => "unsigned long long",
PrimitiveType::USize if config.usize_is_size_t => "size_t",
PrimitiveType::USize => "uintptr_t",
PrimitiveType::UInt8 => "uint8_t",
PrimitiveType::UInt16 => "uint16_t",
PrimitiveType::UInt32 => "uint32_t",
PrimitiveType::UInt64 => "uint64_t",
PrimitiveType::ISize if config.usize_is_size_t => "ptrdiff_t",
PrimitiveType::ISize => "intptr_t",
PrimitiveType::Int8 => "int8_t",
PrimitiveType::Int16 => "int16_t",
Expand All @@ -178,12 +179,6 @@ impl PrimitiveType {
}
}

impl fmt::Display for PrimitiveType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.to_repr_c())
}
}

// The `U` part of `[T; U]`
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ArrayLength {
Expand Down
1 change: 1 addition & 0 deletions template.toml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ line-endings = "LF" # also "CR", "CRLF", "Native"

style = "both"
sort_by = "Name" # default for `fn.sort_by` and `const.sort_by`
usize_is_size_t = true



Expand Down
21 changes: 21 additions & 0 deletions tests/expectations/size-types.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

enum IE {
IV,
};
typedef ptrdiff_t IE;

enum UE {
UV,
};
typedef size_t UE;

typedef size_t Usize;

typedef ptrdiff_t Isize;

void root(Usize, Isize, UE, IE);
41 changes: 41 additions & 0 deletions tests/expectations/size-types.compat.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>

enum IE
#ifdef __cplusplus
: ptrdiff_t
#endif // __cplusplus
{
IV,
};
#ifndef __cplusplus
typedef ptrdiff_t IE;
#endif // __cplusplus

enum UE
#ifdef __cplusplus
: size_t
#endif // __cplusplus
{
UV,
};
#ifndef __cplusplus
typedef size_t UE;
#endif // __cplusplus

typedef size_t Usize;

typedef ptrdiff_t Isize;

#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

void root(Usize, Isize, UE, IE);

#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
24 changes: 24 additions & 0 deletions tests/expectations/size-types.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <cstdarg>
#include <cstddef>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>

enum class IE : ptrdiff_t {
IV,
};

enum class UE : size_t {
UV,
};

using Usize = size_t;

using Isize = ptrdiff_t;

extern "C" {

void root(Usize, Isize, UE, IE);

} // extern "C"
15 changes: 15 additions & 0 deletions tests/rust/size-types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
type Usize = usize;
type Isize = isize;

#[repr(usize)]
enum UE {
UV,
}

#[repr(isize)]
enum IE {
IV,
}

#[no_mangle]
pub extern "C" fn root(_: Usize, _: Isize, _: UE, _: IE) {}
1 change: 1 addition & 0 deletions tests/rust/size-types.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
usize_is_size_t = true