Skip to content

Commit

Permalink
Generalize Item to expose documentation and generic params
Browse files Browse the repository at this point in the history
Closes #963
  • Loading branch information
scovich authored and emilio committed May 28, 2024
1 parent 1dda616 commit 9f63284
Show file tree
Hide file tree
Showing 11 changed files with 90 additions and 37 deletions.
10 changes: 9 additions & 1 deletion src/bindgen/ir/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,10 @@ impl Item for Constant {
&mut self.annotations
}

fn documentation(&self) -> &Documentation {
&self.documentation
}

fn container(&self) -> ItemContainer {
ItemContainer::Constant(self.clone())
}
Expand All @@ -589,6 +593,10 @@ impl Item for Constant {
fn resolve_declaration_types(&mut self, resolver: &DeclarationTypeResolver) {
self.ty.resolve_declaration_types(resolver);
}

fn generic_params(&self) -> &GenericParams {
GenericParams::empty()
}
}

impl Constant {
Expand Down Expand Up @@ -671,7 +679,7 @@ impl Constant {
value = fields.iter().next().unwrap().1
}

language_backend.write_documentation(out, &self.documentation);
language_backend.write_documentation(out, self.documentation());

let allow_constexpr = config.constant.allow_constexpr && self.value.can_be_constexpr();
match config.language {
Expand Down
10 changes: 9 additions & 1 deletion src/bindgen/ir/enumeration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,7 +318,7 @@ impl Enum {
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
if self.generic_params.len() > 0 {
if self.is_generic() {
return;
}

Expand Down Expand Up @@ -467,6 +467,10 @@ impl Item for Enum {
&mut self.annotations
}

fn documentation(&self) -> &Documentation {
&self.documentation
}

fn container(&self) -> ItemContainer {
ItemContainer::Enum(self.clone())
}
Expand All @@ -492,6 +496,10 @@ impl Item for Enum {
}
}

fn generic_params(&self) -> &GenericParams {
&self.generic_params
}

fn rename_for_config(&mut self, config: &Config) {
config.export.rename(&mut self.export_name);

Expand Down
6 changes: 6 additions & 0 deletions src/bindgen/ir/generic_path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,13 @@ impl GenericParam {
#[derive(Default, Debug, Clone)]
pub struct GenericParams(pub Vec<GenericParam>);

static EMPTY_GENERIC_PARAMS: GenericParams = GenericParams(Vec::new());
impl GenericParams {
/// An empty generic params, for convenience.
pub fn empty() -> &'static Self {
&EMPTY_GENERIC_PARAMS
}

pub fn load(generics: &syn::Generics) -> Result<Self, String> {
let mut params = vec![];
for param in &generics.params {
Expand Down
12 changes: 11 additions & 1 deletion src/bindgen/ir/global.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
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::ir::{
AnnotationSet, Cfg, Documentation, GenericParams, Item, ItemContainer, Path, Type,
};
use crate::bindgen::library::Library;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -87,6 +89,10 @@ impl Item for Static {
&mut self.annotations
}

fn documentation(&self) -> &Documentation {
&self.documentation
}

fn container(&self) -> ItemContainer {
ItemContainer::Static(self.clone())
}
Expand All @@ -99,6 +105,10 @@ impl Item for Static {
self.ty.resolve_declaration_types(resolver);
}

fn generic_params(&self) -> &GenericParams {
GenericParams::empty()
}

fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
self.ty.add_dependencies(library, out);
}
Expand Down
11 changes: 9 additions & 2 deletions src/bindgen/ir/item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use crate::bindgen::config::Config;
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
use crate::bindgen::dependencies::Dependencies;
use crate::bindgen::ir::{
AnnotationSet, Cfg, Constant, Enum, GenericArgument, OpaqueItem, Path, Static, Struct, Typedef,
Union,
AnnotationSet, Cfg, Constant, Documentation, Enum, GenericArgument, GenericParams, OpaqueItem,
Path, Static, Struct, Typedef, Union,
};
use crate::bindgen::library::Library;
use crate::bindgen::monomorph::Monomorphs;
Expand All @@ -27,6 +27,7 @@ pub trait Item {
fn cfg(&self) -> Option<&Cfg>;
fn annotations(&self) -> &AnnotationSet;
fn annotations_mut(&mut self) -> &mut AnnotationSet;
fn documentation(&self) -> &Documentation;

fn container(&self) -> ItemContainer;

Expand All @@ -36,6 +37,12 @@ pub trait Item {
fn resolve_declaration_types(&mut self, _resolver: &DeclarationTypeResolver) {
unimplemented!()
}
fn generic_params(&self) -> &GenericParams;

fn is_generic(&self) -> bool {
!self.generic_params().is_empty()
}

fn rename_for_config(&mut self, _config: &Config) {}
fn add_dependencies(&self, _library: &Library, _out: &mut Dependencies) {}
fn instantiate_monomorph(
Expand Down
14 changes: 9 additions & 5 deletions src/bindgen/ir/opaque.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,10 @@ impl Item for OpaqueItem {
&mut self.annotations
}

fn documentation(&self) -> &Documentation {
&self.documentation
}

fn container(&self) -> ItemContainer {
ItemContainer::OpaqueItem(self.clone())
}
Expand All @@ -86,6 +90,10 @@ impl Item for OpaqueItem {
resolver.add_struct(&self.path);
}

fn generic_params(&self) -> &GenericParams {
&self.generic_params
}

fn rename_for_config(&mut self, config: &Config) {
config.export.rename(&mut self.export_name);
}
Expand All @@ -98,11 +106,7 @@ impl Item for OpaqueItem {
library: &Library,
out: &mut Monomorphs,
) {
assert!(
!self.generic_params.is_empty(),
"{} is not generic",
self.path
);
assert!(self.is_generic(), "{} is not generic", self.path);

// We can be instantiated with less generic params because of default
// template parameters, or because of empty types that we remove during
Expand Down
12 changes: 8 additions & 4 deletions src/bindgen/ir/structure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,6 @@ impl Struct {
}
}

pub fn is_generic(&self) -> bool {
self.generic_params.len() > 0
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
// Generic structs can instantiate monomorphs only once they've been
// instantiated. See `instantiate_monomorph` for more details.
Expand Down Expand Up @@ -266,6 +262,10 @@ impl Item for Struct {
&mut self.annotations
}

fn documentation(&self) -> &Documentation {
&self.documentation
}

fn container(&self) -> ItemContainer {
ItemContainer::Struct(self.clone())
}
Expand All @@ -284,6 +284,10 @@ impl Item for Struct {
}
}

fn generic_params(&self) -> &GenericParams {
&self.generic_params
}

fn rename_for_config(&mut self, config: &Config) {
// Rename the name of the struct
if !(self.has_tag_field && config.language == Language::Cxx) {
Expand Down
18 changes: 10 additions & 8 deletions src/bindgen/ir/typedef.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,18 +89,12 @@ impl Typedef {
}
}

pub fn is_generic(&self) -> bool {
self.generic_params.len() > 0
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
// Generic structs can instantiate monomorphs only once they've been
// instantiated. See `instantiate_monomorph` for more details.
if self.is_generic() {
return;
if !self.is_generic() {
self.aliased.add_monomorphs(library, out);
}

self.aliased.add_monomorphs(library, out);
}

pub fn mangle_paths(&mut self, monomorphs: &Monomorphs) {
Expand Down Expand Up @@ -129,6 +123,10 @@ impl Item for Typedef {
&mut self.annotations
}

fn documentation(&self) -> &Documentation {
&self.documentation
}

fn container(&self) -> ItemContainer {
ItemContainer::Typedef(self.clone())
}
Expand All @@ -141,6 +139,10 @@ impl Item for Typedef {
self.aliased.resolve_declaration_types(resolver);
}

fn generic_params(&self) -> &GenericParams {
&self.generic_params
}

fn rename_for_config(&mut self, config: &Config) {
config.export.rename(&mut self.export_name);
self.aliased.rename_for_config(config, &self.generic_params);
Expand Down
12 changes: 8 additions & 4 deletions src/bindgen/ir/union.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,10 +100,6 @@ impl Union {
}
}

pub fn is_generic(&self) -> bool {
self.generic_params.len() > 0
}

pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
// Generic unions can instantiate monomorphs only once they've been
// instantiated. See `instantiate_monomorph` for more details.
Expand Down Expand Up @@ -144,6 +140,10 @@ impl Item for Union {
&mut self.annotations
}

fn documentation(&self) -> &Documentation {
&self.documentation
}

fn container(&self) -> ItemContainer {
ItemContainer::Union(self.clone())
}
Expand All @@ -158,6 +158,10 @@ impl Item for Union {
}
}

fn generic_params(&self) -> &GenericParams {
&self.generic_params
}

fn rename_for_config(&mut self, config: &Config) {
config.export.rename(&mut self.export_name);
for field in &mut self.fields {
Expand Down
10 changes: 5 additions & 5 deletions src/bindgen/library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,11 +428,11 @@ impl Library {
}

// Remove structs and opaque items that are generic
self.opaque_items.filter(|x| x.generic_params.len() > 0);
self.structs.filter(|x| x.generic_params.len() > 0);
self.unions.filter(|x| x.generic_params.len() > 0);
self.enums.filter(|x| x.generic_params.len() > 0);
self.typedefs.filter(|x| x.generic_params.len() > 0);
self.opaque_items.filter(|x| x.is_generic());
self.structs.filter(|x| x.is_generic());
self.unions.filter(|x| x.is_generic());
self.enums.filter(|x| x.is_generic());
self.typedefs.filter(|x| x.is_generic());

// Mangle the paths that remain
self.unions
Expand Down
12 changes: 6 additions & 6 deletions src/bindgen/monomorph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::collections::HashMap;
use std::mem;

use crate::bindgen::ir::{
Enum, GenericArgument, GenericPath, OpaqueItem, Path, Struct, Typedef, Union,
Enum, GenericArgument, GenericPath, Item, OpaqueItem, Path, Struct, Typedef, Union,
};
use crate::bindgen::library::Library;

Expand Down Expand Up @@ -34,7 +34,7 @@ impl Monomorphs {
) {
let replacement_path = GenericPath::new(generic.path.clone(), arguments);

debug_assert!(generic.generic_params.len() > 0);
debug_assert!(generic.is_generic());
debug_assert!(!self.contains(&replacement_path));

self.replacements
Expand All @@ -54,7 +54,7 @@ impl Monomorphs {
) {
let replacement_path = GenericPath::new(generic.path.clone(), arguments);

debug_assert!(generic.generic_params.len() > 0);
debug_assert!(generic.is_generic());
debug_assert!(!self.contains(&replacement_path));

self.replacements
Expand All @@ -74,7 +74,7 @@ impl Monomorphs {
) {
let replacement_path = GenericPath::new(generic.path.clone(), arguments);

debug_assert!(generic.generic_params.len() > 0);
debug_assert!(generic.is_generic());
debug_assert!(!self.contains(&replacement_path));

self.replacements
Expand All @@ -93,7 +93,7 @@ impl Monomorphs {
) {
let replacement_path = GenericPath::new(generic.path.clone(), arguments);

debug_assert!(generic.generic_params.len() > 0);
debug_assert!(generic.is_generic());
debug_assert!(!self.contains(&replacement_path));

self.replacements
Expand All @@ -110,7 +110,7 @@ impl Monomorphs {
) {
let replacement_path = GenericPath::new(generic.path.clone(), arguments);

debug_assert!(generic.generic_params.len() > 0);
debug_assert!(generic.is_generic());
debug_assert!(!self.contains(&replacement_path));

self.replacements
Expand Down

0 comments on commit 9f63284

Please sign in to comment.