Skip to content

Commit 1c2da73

Browse files
committed
generalize Item to expose documentation and generic params
1 parent 960c51d commit 1c2da73

File tree

10 files changed

+85
-37
lines changed

10 files changed

+85
-37
lines changed

src/bindgen/ir/constant.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -574,6 +574,10 @@ impl Item for Constant {
574574
&mut self.annotations
575575
}
576576

577+
fn documentation(&self) -> &Documentation {
578+
&self.documentation
579+
}
580+
577581
fn container(&self) -> ItemContainer {
578582
ItemContainer::Constant(self.clone())
579583
}
@@ -589,6 +593,10 @@ impl Item for Constant {
589593
fn resolve_declaration_types(&mut self, resolver: &DeclarationTypeResolver) {
590594
self.ty.resolve_declaration_types(resolver);
591595
}
596+
597+
fn generic_params(&self) -> Option<&GenericParams> {
598+
None
599+
}
592600
}
593601

594602
impl Constant {
@@ -671,7 +679,7 @@ impl Constant {
671679
value = fields.iter().next().unwrap().1
672680
}
673681

674-
language_backend.write_documentation(out, &self.documentation);
682+
language_backend.write_documentation(out, self.documentation());
675683

676684
let allow_constexpr = config.constant.allow_constexpr && self.value.can_be_constexpr();
677685
match config.language {

src/bindgen/ir/enumeration.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,7 +318,7 @@ impl Enum {
318318
}
319319

320320
pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
321-
if self.generic_params.len() > 0 {
321+
if self.is_generic() {
322322
return;
323323
}
324324

@@ -467,6 +467,10 @@ impl Item for Enum {
467467
&mut self.annotations
468468
}
469469

470+
fn documentation(&self) -> &Documentation {
471+
&self.documentation
472+
}
473+
470474
fn container(&self) -> ItemContainer {
471475
ItemContainer::Enum(self.clone())
472476
}
@@ -492,6 +496,10 @@ impl Item for Enum {
492496
}
493497
}
494498

499+
fn generic_params(&self) -> Option<&GenericParams> {
500+
Some(&self.generic_params)
501+
}
502+
495503
fn rename_for_config(&mut self, config: &Config) {
496504
config.export.rename(&mut self.export_name);
497505

src/bindgen/ir/global.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
use crate::bindgen::config::Config;
66
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
77
use crate::bindgen::dependencies::Dependencies;
8-
use crate::bindgen::ir::{AnnotationSet, Cfg, Documentation, Item, ItemContainer, Path, Type};
8+
use crate::bindgen::ir::{
9+
AnnotationSet, Cfg, Documentation, GenericParams, Item, ItemContainer, Path, Type,
10+
};
911
use crate::bindgen::library::Library;
1012

1113
#[derive(Debug, Clone)]
@@ -87,6 +89,10 @@ impl Item for Static {
8789
&mut self.annotations
8890
}
8991

92+
fn documentation(&self) -> &Documentation {
93+
&self.documentation
94+
}
95+
9096
fn container(&self) -> ItemContainer {
9197
ItemContainer::Static(self.clone())
9298
}
@@ -99,6 +105,10 @@ impl Item for Static {
99105
self.ty.resolve_declaration_types(resolver);
100106
}
101107

108+
fn generic_params(&self) -> Option<&GenericParams> {
109+
None
110+
}
111+
102112
fn add_dependencies(&self, library: &Library, out: &mut Dependencies) {
103113
self.ty.add_dependencies(library, out);
104114
}

src/bindgen/ir/item.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ use crate::bindgen::config::Config;
99
use crate::bindgen::declarationtyperesolver::DeclarationTypeResolver;
1010
use crate::bindgen::dependencies::Dependencies;
1111
use crate::bindgen::ir::{
12-
AnnotationSet, Cfg, Constant, Enum, GenericArgument, OpaqueItem, Path, Static, Struct, Typedef,
13-
Union,
12+
AnnotationSet, Cfg, Constant, Documentation, Enum, GenericArgument, GenericParams, OpaqueItem,
13+
Path, Static, Struct, Typedef, Union,
1414
};
1515
use crate::bindgen::library::Library;
1616
use crate::bindgen::monomorph::Monomorphs;
@@ -27,6 +27,7 @@ pub trait Item {
2727
fn cfg(&self) -> Option<&Cfg>;
2828
fn annotations(&self) -> &AnnotationSet;
2929
fn annotations_mut(&mut self) -> &mut AnnotationSet;
30+
fn documentation(&self) -> &Documentation;
3031

3132
fn container(&self) -> ItemContainer;
3233

@@ -36,6 +37,13 @@ pub trait Item {
3637
fn resolve_declaration_types(&mut self, _resolver: &DeclarationTypeResolver) {
3738
unimplemented!()
3839
}
40+
fn generic_params(&self) -> Option<&GenericParams>;
41+
42+
fn is_generic(&self) -> bool {
43+
self.generic_params()
44+
.is_some_and(|params| !params.is_empty())
45+
}
46+
3947
fn rename_for_config(&mut self, _config: &Config) {}
4048
fn add_dependencies(&self, _library: &Library, _out: &mut Dependencies) {}
4149
fn instantiate_monomorph(

src/bindgen/ir/opaque.rs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,10 @@ impl Item for OpaqueItem {
7878
&mut self.annotations
7979
}
8080

81+
fn documentation(&self) -> &Documentation {
82+
&self.documentation
83+
}
84+
8185
fn container(&self) -> ItemContainer {
8286
ItemContainer::OpaqueItem(self.clone())
8387
}
@@ -86,6 +90,10 @@ impl Item for OpaqueItem {
8690
resolver.add_struct(&self.path);
8791
}
8892

93+
fn generic_params(&self) -> Option<&GenericParams> {
94+
Some(&self.generic_params)
95+
}
96+
8997
fn rename_for_config(&mut self, config: &Config) {
9098
config.export.rename(&mut self.export_name);
9199
}
@@ -98,11 +106,7 @@ impl Item for OpaqueItem {
98106
library: &Library,
99107
out: &mut Monomorphs,
100108
) {
101-
assert!(
102-
!self.generic_params.is_empty(),
103-
"{} is not generic",
104-
self.path
105-
);
109+
assert!(self.is_generic(), "{} is not generic", self.path);
106110

107111
// We can be instantiated with less generic params because of default
108112
// template parameters, or because of empty types that we remove during

src/bindgen/ir/structure.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -150,10 +150,6 @@ impl Struct {
150150
}
151151
}
152152

153-
pub fn is_generic(&self) -> bool {
154-
self.generic_params.len() > 0
155-
}
156-
157153
/// Attempts to convert this struct to a typedef (only works for transparent structs).
158154
pub fn as_typedef(&self) -> Option<Typedef> {
159155
if self.is_transparent {
@@ -283,6 +279,10 @@ impl Item for Struct {
283279
&mut self.annotations
284280
}
285281

282+
fn documentation(&self) -> &Documentation {
283+
&self.documentation
284+
}
285+
286286
fn container(&self) -> ItemContainer {
287287
ItemContainer::Struct(self.clone())
288288
}
@@ -301,6 +301,10 @@ impl Item for Struct {
301301
}
302302
}
303303

304+
fn generic_params(&self) -> Option<&GenericParams> {
305+
Some(&self.generic_params)
306+
}
307+
304308
fn rename_for_config(&mut self, config: &Config) {
305309
// Rename the name of the struct
306310
if !(self.has_tag_field && config.language == Language::Cxx) {

src/bindgen/ir/typedef.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,12 @@ impl Typedef {
102102
}
103103
}
104104

105-
pub fn is_generic(&self) -> bool {
106-
self.generic_params.len() > 0
107-
}
108-
109105
pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
110106
// Generic structs can instantiate monomorphs only once they've been
111107
// instantiated. See `instantiate_monomorph` for more details.
112-
if self.is_generic() {
113-
return;
108+
if !self.is_generic() {
109+
self.aliased.add_monomorphs(library, out);
114110
}
115-
116-
self.aliased.add_monomorphs(library, out);
117111
}
118112

119113
pub fn mangle_paths(&mut self, monomorphs: &Monomorphs) {
@@ -142,6 +136,10 @@ impl Item for Typedef {
142136
&mut self.annotations
143137
}
144138

139+
fn documentation(&self) -> &Documentation {
140+
&self.documentation
141+
}
142+
145143
fn container(&self) -> ItemContainer {
146144
ItemContainer::Typedef(self.clone())
147145
}
@@ -154,6 +152,10 @@ impl Item for Typedef {
154152
self.aliased.resolve_declaration_types(resolver);
155153
}
156154

155+
fn generic_params(&self) -> Option<&GenericParams> {
156+
Some(&self.generic_params)
157+
}
158+
157159
fn rename_for_config(&mut self, config: &Config) {
158160
config.export.rename(&mut self.export_name);
159161
self.aliased.rename_for_config(config, &self.generic_params);

src/bindgen/ir/union.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,6 @@ impl Union {
100100
}
101101
}
102102

103-
pub fn is_generic(&self) -> bool {
104-
self.generic_params.len() > 0
105-
}
106-
107103
pub fn add_monomorphs(&self, library: &Library, out: &mut Monomorphs) {
108104
// Generic unions can instantiate monomorphs only once they've been
109105
// instantiated. See `instantiate_monomorph` for more details.
@@ -144,6 +140,10 @@ impl Item for Union {
144140
&mut self.annotations
145141
}
146142

143+
fn documentation(&self) -> &Documentation {
144+
&self.documentation
145+
}
146+
147147
fn container(&self) -> ItemContainer {
148148
ItemContainer::Union(self.clone())
149149
}
@@ -158,6 +158,10 @@ impl Item for Union {
158158
}
159159
}
160160

161+
fn generic_params(&self) -> Option<&GenericParams> {
162+
Some(&self.generic_params)
163+
}
164+
161165
fn rename_for_config(&mut self, config: &Config) {
162166
config.export.rename(&mut self.export_name);
163167
for field in &mut self.fields {

src/bindgen/library.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,11 @@ impl Library {
428428
}
429429

430430
// Remove structs and opaque items that are generic
431-
self.opaque_items.filter(|x| x.generic_params.len() > 0);
432-
self.structs.filter(|x| x.generic_params.len() > 0);
433-
self.unions.filter(|x| x.generic_params.len() > 0);
434-
self.enums.filter(|x| x.generic_params.len() > 0);
435-
self.typedefs.filter(|x| x.generic_params.len() > 0);
431+
self.opaque_items.filter(|x| x.is_generic());
432+
self.structs.filter(|x| x.is_generic());
433+
self.unions.filter(|x| x.is_generic());
434+
self.enums.filter(|x| x.is_generic());
435+
self.typedefs.filter(|x| x.is_generic());
436436

437437
// Mangle the paths that remain
438438
self.unions

src/bindgen/monomorph.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use std::collections::HashMap;
66
use std::mem;
77

88
use crate::bindgen::ir::{
9-
Enum, GenericArgument, GenericPath, OpaqueItem, Path, Struct, Typedef, Union,
9+
Enum, GenericArgument, GenericPath, Item, OpaqueItem, Path, Struct, Typedef, Union,
1010
};
1111
use crate::bindgen::library::Library;
1212

@@ -34,7 +34,7 @@ impl Monomorphs {
3434
) {
3535
let replacement_path = GenericPath::new(generic.path.clone(), arguments);
3636

37-
debug_assert!(generic.generic_params.len() > 0);
37+
debug_assert!(generic.is_generic());
3838
debug_assert!(!self.contains(&replacement_path));
3939

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

57-
debug_assert!(generic.generic_params.len() > 0);
57+
debug_assert!(generic.is_generic());
5858
debug_assert!(!self.contains(&replacement_path));
5959

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

77-
debug_assert!(generic.generic_params.len() > 0);
77+
debug_assert!(generic.is_generic());
7878
debug_assert!(!self.contains(&replacement_path));
7979

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

96-
debug_assert!(generic.generic_params.len() > 0);
96+
debug_assert!(generic.is_generic());
9797
debug_assert!(!self.contains(&replacement_path));
9898

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

113-
debug_assert!(generic.generic_params.len() > 0);
113+
debug_assert!(generic.is_generic());
114114
debug_assert!(!self.contains(&replacement_path));
115115

116116
self.replacements

0 commit comments

Comments
 (0)