Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Apr 2, 2022
1 parent 29a6130 commit e29bdd1
Show file tree
Hide file tree
Showing 6 changed files with 30 additions and 23 deletions.
21 changes: 14 additions & 7 deletions src/bindgen/cdecl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ impl CDecl {
"error generating cdecl for {:?}",
t
);

if config.language == Language::Zig {
self.type_name = p.to_repr_zig().to_string();
} else {
Expand Down Expand Up @@ -186,7 +186,6 @@ impl CDecl {
}

fn write<F: Write>(&self, out: &mut SourceWriter<F>, ident: Option<&str>, config: &Config) {

// Write the type-specifier and type-qualifier first
if !self.type_qualifers.is_empty() {
write!(out, "{} ", self.type_qualifers);
Expand All @@ -207,7 +206,7 @@ impl CDecl {
out.write_horizontal_source_list(&self.type_generic_args, ListType::Join(", "));
out.write(">");
}

// When we have an identifier, put a space between the type and the declarators
if ident.is_some() {
out.write(" ");
Expand Down Expand Up @@ -237,12 +236,16 @@ impl CDecl {
}
if is_const {
if config.language == Language::Zig {
out.write(config.style.zig_def());
write!(out, "{} ", config.style.zig_def());
} else {
out.write("const ");
}
}
if !is_nullable && !is_ref && config.language != Language::Cython && config.language != Language::Zig {
if !is_nullable
&& !is_ref
&& config.language != Language::Cython
&& config.language != Language::Zig
{
if let Some(attr) = &config.pointer.non_null_attribute {
write!(out, "{} ", attr);
}
Expand Down Expand Up @@ -291,7 +294,11 @@ impl CDecl {
if constant.is_empty() {
write!(out, "{}: [*]{}", self.type_qualifers, self.type_name);
} else {
write!(out, "{}: [{}]{}", self.type_qualifers, constant, self.type_name);
write!(
out,
"{}: [{}]{}",
self.type_qualifers, constant, self.type_name
);
}
} else {
write!(out, "[{}]", constant);
Expand All @@ -307,7 +314,7 @@ impl CDecl {
out.write("(");
if args.is_empty() && config.language == Language::C {
out.write("void");
}
}
if layout_vertical {
let align_length = out.line_length_for_align();
out.push_set_spaces(align_length);
Expand Down
4 changes: 2 additions & 2 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,9 @@ impl Style {

pub fn zig_def(self) -> &'static str {
if self.generate_tag() {
"pub const "
"pub const"
} else {
"pub extern "
"pub extern"
}
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -604,9 +604,8 @@ impl Constant {
// but still useful as documentation, so we write it as a comment.
write!(out, " {} # = ", name);
value.write(config, out);
},
}
Language::Zig if allow_comptime => {

if allow_comptime {
out.write("comptime ");
}
Expand All @@ -616,6 +615,7 @@ impl Constant {
self.ty.write(config, out);
write!(out, " {} = ", name);
value.write(config, out);
write!(out, ";");
}
}

Expand Down
10 changes: 6 additions & 4 deletions src/bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -247,8 +247,9 @@ impl Source for Function {
func.documentation.write(config, out);

if func.extern_decl {
if config.language != Language::Zig {
out.write("extern ");
out.write("extern ");
if config.language == Language::Zig {
out.write("fn");
}
} else {
if config.language == Language::Zig {
Expand Down Expand Up @@ -300,8 +301,9 @@ impl Source for Function {
func.documentation.write(config, out);

if func.extern_decl {
if config.language != Language::Zig {
out.write("extern ");
out.write("extern ");
if config.language == Language::Zig {
out.write("fn");
}
} else {
if config.language == Language::Zig {
Expand Down
12 changes: 5 additions & 7 deletions src/bindgen/ir/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ use std::io::Write;

use syn::ext::IdentExt;

use crate::bindgen::cdecl;
use crate::bindgen::config::Config;
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{AnnotationSet, Cfg, Documentation, Item, ItemContainer, Path, Type};
use crate::bindgen::library::Library;
use crate::bindgen::writer::{Source, SourceWriter};
use crate::bindgen::{cdecl, Language};

#[derive(Debug, Clone)]
pub struct Static {
Expand Down Expand Up @@ -108,12 +108,10 @@ impl Item for Static {

impl Source for Static {
fn write<F: Write>(&self, config: &Config, out: &mut SourceWriter<F>) {
if config.language != Language::Zig {
out.write("extern ");
if let Type::Ptr { is_const: true, .. } = self.ty {
} else if !self.mutable {
out.write("const ");
}
out.write("extern ");
if let Type::Ptr { is_const: true, .. } = self.ty {
} else if !self.mutable {
out.write("const ");
}
cdecl::write_field(out, &self.ty, &self.export_name, config);
out.write(";");
Expand Down
2 changes: 1 addition & 1 deletion src/bindgen/ir/typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,7 @@ impl Source for Typedef {
.write(config, out);
}
Language::Zig => {
write!(out, "pub const {} = ", self.export_name());
write!(out, "{}{} = ", config.style.zig_def(), self.export_name());
self.aliased.write(config, out);
}
}
Expand Down

0 comments on commit e29bdd1

Please sign in to comment.