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

Support forward declarations for C-like languages #1028

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
5 changes: 5 additions & 0 deletions src/bindgen/bindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::io::{Read, Write};
use std::path;
use std::rc::Rc;

use indexmap::IndexMap;

use crate::bindgen::config::{Config, Language};
use crate::bindgen::ir::{
Constant, Function, ItemContainer, ItemMap, Path as BindgenPath, Static, Struct, Type, Typedef,
Expand All @@ -28,6 +30,7 @@ pub struct Bindings {
struct_map: ItemMap<Struct>,
typedef_map: ItemMap<Typedef>,
struct_fileds_memo: RefCell<HashMap<BindgenPath, Rc<Vec<String>>>>,
pub forward_declarations: IndexMap<BindgenPath, Type>,
pub globals: Vec<Static>,
pub constants: Vec<Constant>,
pub items: Vec<ItemContainer>,
Expand All @@ -45,6 +48,7 @@ impl Bindings {
config: Config,
struct_map: ItemMap<Struct>,
typedef_map: ItemMap<Typedef>,
forward_declarations: IndexMap<BindgenPath, Type>,
constants: Vec<Constant>,
globals: Vec<Static>,
items: Vec<ItemContainer>,
Expand All @@ -58,6 +62,7 @@ impl Bindings {
struct_map,
typedef_map,
struct_fileds_memo: Default::default(),
forward_declarations,
globals,
constants,
items,
Expand Down
1 change: 1 addition & 0 deletions src/bindgen/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ impl Builder {
Default::default(),
Default::default(),
Default::default(),
Default::default(),
true,
String::new(),
));
Expand Down
10 changes: 10 additions & 0 deletions src/bindgen/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1076,6 +1076,16 @@ impl Default for Config {
}

impl Config {
pub(crate) fn forward_declarations_enabled(&self) -> bool {
match self.language {
Language::C | Language::Cxx => {
// Cannot generate forward declarations if `Style::Type` is specified.
self.style.generate_tag()
}
_ => false,
}
}

pub(crate) fn cpp_compatible_c(&self) -> bool {
self.language == Language::C && self.cpp_compat
}
Expand Down
14 changes: 10 additions & 4 deletions src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,16 @@ use std::borrow::Cow;
use std::collections::HashMap;
use std::io::Write;

use indexmap::IndexSet;
use syn::ext::IdentExt;
use syn::UnOp;

use crate::bindgen::config::{Config, Language};
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{
AnnotationSet, Cfg, ConditionWrite, Documentation, GenericParams, Item, ItemContainer, Path,
Struct, ToCondition, Type,
AnnotationSet, Cfg, ConditionWrite, Documentation, GenericParams, GenericPath, Item,
ItemContainer, Path, Struct, ToCondition, Type,
};
use crate::bindgen::language_backend::LanguageBackend;
use crate::bindgen::library::Library;
Expand Down Expand Up @@ -560,8 +561,13 @@ impl Item for Constant {
&self.path
}

fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
self.ty.add_dependencies(library, out);
fn add_dependencies(
&self,
library: &Library,
out: &mut Dependencies,
ptr_types: &mut IndexSet<GenericPath>,
) {
self.ty.add_dependencies(library, out, ptr_types);
}

fn export_name(&self) -> &str {
Expand Down
19 changes: 15 additions & 4 deletions src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::io::Write;

use indexmap::IndexSet;
use syn::ext::IdentExt;

use crate::bindgen::config::{Config, Language};
Expand Down Expand Up @@ -254,9 +255,14 @@ impl EnumVariant {
}
}

fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
fn add_dependencies(
&self,
library: &Library,
out: &mut Dependencies,
ptr_types: &mut IndexSet<GenericPath>,
) {
if let VariantBody::Body { ref body, .. } = self.body {
body.add_dependencies(library, out);
body.add_dependencies(library, out, ptr_types);
}
}

Expand Down Expand Up @@ -634,9 +640,14 @@ impl Item for Enum {
out.insert_enum(library, self, monomorph, generic_values.to_owned());
}

fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
fn add_dependencies(
&self,
library: &Library,
out: &mut Dependencies,
ptr_types: &mut IndexSet<GenericPath>,
) {
for variant in &self.variants {
variant.add_dependencies(library, out);
variant.add_dependencies(library, out, ptr_types);
}
}
}
Expand Down
12 changes: 9 additions & 3 deletions src/bindgen/ir/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use std::collections::HashMap;

use indexmap::IndexSet;
use syn::ext::IdentExt;

use crate::bindgen::config::{Config, Language};
Expand Down Expand Up @@ -122,10 +123,15 @@ impl Function {
}
}

pub fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
self.ret.add_dependencies(library, out);
pub fn add_dependencies(
&self,
library: &Library,
out: &mut Dependencies,
ptr_types: &mut IndexSet<GenericPath>,
) {
self.ret.add_dependencies(library, out, ptr_types);
for arg in &self.args {
arg.ty.add_dependencies(library, out);
arg.ty.add_dependencies(library, out, ptr_types);
}
}

Expand Down
13 changes: 10 additions & 3 deletions src/bindgen/ir/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
* 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 indexmap::IndexSet;

use crate::bindgen::config::Config;
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{
AnnotationSet, Cfg, Documentation, GenericParams, Item, ItemContainer, Path, Type,
AnnotationSet, Cfg, Documentation, GenericParams, GenericPath, Item, ItemContainer, Path, Type,
};
use crate::bindgen::library::Library;

Expand Down Expand Up @@ -109,7 +111,12 @@ impl Item for Static {
GenericParams::empty()
}

fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
self.ty.add_dependencies(library, out);
fn add_dependencies(
&self,
library: &Library,
out: &mut Dependencies,
ptr_types: &mut IndexSet<GenericPath>,
) {
self.ty.add_dependencies(library, out, ptr_types);
}
}
17 changes: 13 additions & 4 deletions src/bindgen/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@
* 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 indexmap::IndexMap;
use indexmap::{IndexMap, IndexSet};
use std::mem;

use crate::bindgen::config::Config;
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{
AnnotationSet, Cfg, Constant, Documentation, Enum, GenericArgument, GenericParams, OpaqueItem,
Path, Static, Struct, Typedef, Union,
AnnotationSet, Cfg, Constant, Documentation, Enum, GenericArgument, GenericParams, GenericPath,
OpaqueItem, Path, Static, Struct, Typedef, Union,
};
use crate::bindgen::library::Library;
use crate::bindgen::monomorph::Monomorphs;
Expand Down Expand Up @@ -44,7 +44,16 @@ pub trait Item {
}

fn rename_for_config(&mut self, _config: &Config) {}
fn add_dependencies(&self, _library: &Library, _out: &mut Dependencies) {}
fn add_dependencies(
&self,
_library: &Library,
_out: &mut Dependencies,
// IndexSet is used instead of HashSet in order to keep the insertion order.
// When running `cargo test`, //tests/expectations/* will be updated randomly
// if HashSet is used.
_ptr_types: &mut IndexSet<GenericPath>,
) {
}
fn instantiate_monomorph(
&self,
_generics: &[GenericArgument],
Expand Down
7 changes: 5 additions & 2 deletions src/bindgen/ir/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@
* 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 indexmap::IndexSet;

use crate::bindgen::config::Config;
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{
AnnotationSet, Cfg, Documentation, GenericArgument, GenericParams, Item, ItemContainer, Path,
AnnotationSet, Cfg, Documentation, GenericArgument, GenericParams, GenericPath, Item,
ItemContainer, Path,
};
use crate::bindgen::library::Library;
use crate::bindgen::mangle;
Expand Down Expand Up @@ -98,7 +101,7 @@ impl Item for OpaqueItem {
config.export.rename(&mut self.export_name);
}

fn add_dependencies(&self, _: &Library, _: &mut Dependencies) {}
fn add_dependencies(&self, _: &Library, _: &mut Dependencies, _: &mut IndexSet<GenericPath>) {}

fn instantiate_monomorph(
&self,
Expand Down
24 changes: 17 additions & 7 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@

use std::io::Write;

use indexmap::IndexSet;
use syn::ext::IdentExt;

use crate::bindgen::config::{Config, Language, LayoutConfig};
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{
AnnotationSet, Cfg, Constant, Documentation, Field, GenericArgument, GenericParams, Item,
ItemContainer, Path, Repr, ReprAlign, ReprStyle, Type, Typedef,
AnnotationSet, Cfg, Constant, Documentation, Field, GenericArgument, GenericParams,
GenericPath, Item, ItemContainer, Path, Repr, ReprAlign, ReprStyle, Type, Typedef,
};
use crate::bindgen::library::Library;
use crate::bindgen::mangle;
Expand Down Expand Up @@ -372,7 +373,12 @@ impl Item for Struct {
}
}

fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
fn add_dependencies(
&self,
library: &Library,
out: &mut Dependencies,
ptr_types: &mut IndexSet<GenericPath>,
) {
let mut fields = self.fields.iter();

// If there is a tag field, skip it
Expand All @@ -381,13 +387,17 @@ impl Item for Struct {
}

for field in fields {
field
.ty
.add_dependencies_ignoring_generics(&self.generic_params, library, out);
field.ty.add_dependencies_ignoring_generics(
&self.generic_params,
library,
out,
ptr_types,
false,
);
}

for c in &self.associated_constants {
c.add_dependencies(library, out);
c.add_dependencies(library, out, ptr_types);
}
}

Expand Down
Loading