Skip to content

Commit 786cc94

Browse files
authored
Merge pull request rust-lang#7 from benesch/comp-visibility
Track and emit visibility of structs and unions
2 parents 0ea6e78 + effb32e commit 786cc94

File tree

3 files changed

+27
-8
lines changed

3 files changed

+27
-8
lines changed

src/codegen/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1994,6 +1994,7 @@ impl CodeGenerator for CompInfo {
19941994
if unused_template_params {
19951995
attributes.push(attributes::discards_template_param());
19961996
}
1997+
attributes.push(attributes::visibility(self.visibility()));
19971998

19981999
if ctx.options().rust_features().repr_align {
19992000
if let Some(explicit) = explicit_align {

src/ir/comp.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ use super::analysis::Sizedness;
44
use super::annotations::Annotations;
55
use super::context::{BindgenContext, FunctionId, ItemId, TypeId, VarId};
66
use super::dot::DotAttributes;
7+
use super::function::Visibility;
78
use super::item::{IsOpaque, Item};
89
use super::layout::Layout;
910
use super::template::TemplateParameters;
@@ -1011,6 +1012,10 @@ pub struct CompInfo {
10111012
/// Whether this is a struct or a union.
10121013
kind: CompKind,
10131014

1015+
/// The visibility of this struct or union if it was declared inside of
1016+
/// another type. Top-level types always have public visibility.
1017+
visibility: Visibility,
1018+
10141019
/// The members of this struct or union.
10151020
fields: CompFields,
10161021

@@ -1085,6 +1090,7 @@ impl CompInfo {
10851090
pub fn new(kind: CompKind) -> Self {
10861091
CompInfo {
10871092
kind,
1093+
visibility: Visibility::Public,
10881094
fields: CompFields::default(),
10891095
template_params: vec![],
10901096
methods: vec![],
@@ -1202,6 +1208,11 @@ impl CompInfo {
12021208
}
12031209
}
12041210

1211+
/// Returns the visibility of the type.
1212+
pub fn visibility(&self) -> Visibility {
1213+
self.visibility
1214+
}
1215+
12051216
/// Returns whether we have a too large bitfield unit, in which case we may
12061217
/// not be able to derive some of the things we should be able to normally
12071218
/// derive.
@@ -1291,6 +1302,7 @@ impl CompInfo {
12911302
debug!("CompInfo::from_ty({:?}, {:?})", kind, cursor);
12921303

12931304
let mut ci = CompInfo::new(kind);
1305+
ci.visibility = Visibility::from(cursor.access_specifier());
12941306
ci.is_forward_declaration =
12951307
location.map_or(true, |cur| match cur.kind() {
12961308
CXCursor_ParmDecl => true,

src/ir/function.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use crate::clang;
1010
use crate::parse::{
1111
ClangItemParser, ClangSubItemParser, ParseError, ParseResult,
1212
};
13-
use clang_sys::{self, CXCallingConv};
13+
use clang_sys::{self, CXCallingConv, CX_CXXAccessSpecifier, CX_CXXPrivate, CX_CXXProtected};
1414
use proc_macro2;
1515
use quote;
1616
use quote::TokenStreamExt;
@@ -79,6 +79,18 @@ pub enum Visibility {
7979
Private,
8080
}
8181

82+
impl From<CX_CXXAccessSpecifier> for Visibility {
83+
fn from(access_specifier: CX_CXXAccessSpecifier) -> Self {
84+
if access_specifier == CX_CXXPrivate {
85+
Visibility::Private
86+
} else if access_specifier == CX_CXXProtected {
87+
Visibility::Protected
88+
} else {
89+
Visibility::Public
90+
}
91+
}
92+
}
93+
8294
/// A function declaration, with a signature, arguments, and argument names.
8395
///
8496
/// The argument names vector must be the same length as the ones in the
@@ -621,13 +633,7 @@ impl ClangSubItemParser for Function {
621633
return Err(ParseError::Continue);
622634
}
623635

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-
};
636+
let visibility = Visibility::from(cursor.access_specifier());
631637

632638
if cursor.is_inlined_function() {
633639
if !context.options().generate_inline_functions {

0 commit comments

Comments
 (0)