Skip to content

Commit e6a6316

Browse files
committed
Generalize visibility attributes.
1 parent 62330d0 commit e6a6316

File tree

3 files changed

+33
-14
lines changed

3 files changed

+33
-14
lines changed

src/codegen/helpers.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use proc_macro2::{Ident, Span, TokenStream};
66
use quote::TokenStreamExt;
77

88
pub mod attributes {
9-
use crate::ir::comp::SpecialMemberKind;
9+
use crate::ir::{comp::SpecialMemberKind, function::Visibility};
1010
use proc_macro2::{Ident, Span, TokenStream};
1111
use std::str::FromStr;
1212

@@ -94,9 +94,16 @@ pub mod attributes {
9494
}
9595
}
9696

97-
pub fn is_private() -> TokenStream {
98-
quote! {
99-
#[bindgen_private]
97+
pub fn visibility(visibility: Visibility) -> TokenStream {
98+
match visibility {
99+
Visibility::Public => quote! {
100+
},
101+
Visibility::Protected => quote! {
102+
#[bindgen_visibility_protected]
103+
},
104+
Visibility::Private => quote! {
105+
#[bindgen_visibility_private]
106+
},
100107
}
101108
}
102109

src/codegen/mod.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4038,9 +4038,7 @@ impl CodeGenerator for Function {
40384038
attributes.push(attributes::is_pure_virtual());
40394039
}
40404040

4041-
if self.is_private() {
4042-
attributes.push(attributes::is_private());
4043-
}
4041+
attributes.push(attributes::visibility(self.visibility()));
40444042

40454043
let abi = match signature.abi() {
40464044
Abi::ThisCall if !ctx.options().rust_features().thiscall_abi => {

src/ir/function.rs

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,14 @@ pub enum Linkage {
7171
Internal,
7272
}
7373

74+
/// Visibility
75+
#[derive(Debug, Clone, Copy)]
76+
pub enum Visibility {
77+
Public,
78+
Protected,
79+
Private,
80+
}
81+
7482
/// A function declaration, with a signature, arguments, and argument names.
7583
///
7684
/// The argument names vector must be the same length as the ones in the
@@ -99,7 +107,7 @@ pub struct Function {
99107
special_member: Option<SpecialMemberKind>,
100108

101109
/// C++ visibility
102-
is_private: bool,
110+
visibility: Visibility,
103111
}
104112

105113
impl Function {
@@ -112,7 +120,7 @@ impl Function {
112120
kind: FunctionKind,
113121
linkage: Linkage,
114122
special_member: Option<SpecialMemberKind>,
115-
is_private: bool,
123+
visibility: Visibility,
116124
) -> Self {
117125
Function {
118126
name,
@@ -122,7 +130,7 @@ impl Function {
122130
kind,
123131
linkage,
124132
special_member,
125-
is_private,
133+
visibility,
126134
}
127135
}
128136

@@ -157,8 +165,8 @@ impl Function {
157165
}
158166

159167
/// Whether it is private
160-
pub fn is_private(&self) -> bool {
161-
self.is_private
168+
pub fn visibility(&self) -> Visibility {
169+
self.visibility
162170
}
163171
}
164172

@@ -613,7 +621,13 @@ impl ClangSubItemParser for Function {
613621
return Err(ParseError::Continue);
614622
}
615623

616-
let is_private = cursor.access_specifier() == CX_CXXPrivate;
624+
let visibility = if cursor.access_specifier() == CX_CXXPrivate {
625+
Visibility::Private
626+
} else if cursor.access_specifier() == CX_CXXProtected {
627+
Visibility::Protected
628+
} else {
629+
Visibility::Public
630+
};
617631

618632
if cursor.is_inlined_function() {
619633
if !context.options().generate_inline_functions {
@@ -674,7 +688,7 @@ impl ClangSubItemParser for Function {
674688
kind,
675689
linkage,
676690
special_member,
677-
is_private,
691+
visibility,
678692
);
679693
Ok(ParseResult::New(function, Some(cursor)))
680694
}

0 commit comments

Comments
 (0)