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

Fix mapping of "deprecated" attribute to "cfg" attribute #3426

Closed
wants to merge 3 commits into from
Closed
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
49 changes: 35 additions & 14 deletions crates/libs/bindgen/src/type_map.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
use super::*;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct TypeMap(HashMap<TypeName, HashSet<Type>>);
pub struct TypeMap {
types: HashMap<TypeName, HashSet<Type>>,
deprecated: bool,
// TODO: add arches
}

impl std::ops::Deref for TypeMap {
type Target = HashMap<TypeName, HashSet<Type>>;

fn deref(&self) -> &Self::Target {
&self.0
&self.types
}
}

impl TypeMap {
pub fn new() -> Self {
Self(HashMap::new())
Self {
types: HashMap::new(),
deprecated: false,
}
}

#[track_caller]
Expand Down Expand Up @@ -48,13 +55,13 @@ impl TypeMap {
}

pub fn insert(&mut self, ty: Type) -> bool {
self.0.entry(ty.type_name()).or_default().insert(ty)
self.types.entry(ty.type_name()).or_default().insert(ty)
}

fn combine_references(&mut self, other: &Self, references: &References) {
for (tn, types) in &other.0 {
for (tn, types) in &other.types {
if references.contains(*tn).is_none() {
let set = self.0.entry(*tn).or_default();
let set = self.types.entry(*tn).or_default();
types.iter().for_each(|ty| {
set.insert(ty.clone());
});
Expand All @@ -63,26 +70,38 @@ impl TypeMap {
}

pub fn combine(&mut self, other: &Self) {
for (name, types) in &other.0 {
let set = self.0.entry(*name).or_default();
for (name, types) in &other.types {
let set = self.types.entry(*name).or_default();
types.iter().for_each(|ty| {
set.insert(ty.clone());
});
}
}

pub fn is_deprecated(&self) -> bool {
self.deprecated
}

pub fn deprecated<R: HasAttributes>(&mut self, row: R) {
if row.has_attribute("DeprecatedAttribute") {
self.deprecated = true;
}
}

pub fn difference(&self, other: &Self) -> Self {
Self(
self.0
Self {
types: self
.types
.iter()
.filter(|(tn, _)| !other.0.contains_key(tn))
.filter(|(tn, _)| !other.types.contains_key(tn))
.map(|(tn, ty)| (*tn, ty.clone()))
.collect(),
)
deprecated: self.deprecated && !other.deprecated,
}
}

pub fn included(&self, config: &Config) -> bool {
self.0.iter().all(|(tn, _)| {
self.types.iter().all(|(tn, _)| {
// An empty namespace covers core types like `HRESULT`. This way we don't exclude methods
// that depend on core types that aren't explicitly included in the filter.
if tn.namespace().is_empty() {
Expand All @@ -102,6 +121,8 @@ impl TypeMap {
}

fn excluded(&self, filter: &Filter) -> bool {
self.0.iter().any(|(tn, _)| filter.excludes_type_name(*tn))
self.types
.iter()
.any(|(tn, _)| filter.excludes_type_name(*tn))
}
}
20 changes: 18 additions & 2 deletions crates/libs/bindgen/src/types/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,16 @@ impl Class {
} else {
let method_name = to_ident(interface.def.name());
let interface_type = interface.write_name(writer);
let cfg = quote! {};
let mut difference = TypeMap::new();

if writer.config.package {
let mut interface_dependendencies = TypeMap::new();
interface.dependencies(&mut interface_dependendencies);

difference = interface_dependendencies.difference(&dependencies);
}

let cfg = writer.write_cfg(self.def, type_name.namespace(), &difference, false);

Some(quote! {
#cfg
Expand Down Expand Up @@ -245,8 +254,15 @@ impl Class {
}

pub fn dependencies(&self, dependencies: &mut TypeMap) {
dependencies.deprecated(self.def);

for interface in self.required_interfaces() {
Type::Interface(interface).dependencies(dependencies);
if !matches!(
interface.kind,
InterfaceKind::Static | InterfaceKind::Composable
) {
Type::Interface(interface).dependencies(dependencies);
}
}
}

Expand Down
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/types/delegate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,7 @@ impl Delegate {
}

pub fn dependencies(&self, dependencies: &mut TypeMap) {
dependencies.deprecated(self.def);
dependencies.combine(&self.method().dependencies);

for ty in &self.generics {
Expand Down
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/types/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -548,6 +548,7 @@ impl Interface {
}

pub fn dependencies(&self, dependencies: &mut TypeMap) {
dependencies.deprecated(self.def);
Type::Object.dependencies(dependencies);

for interface in self.required_interfaces() {
Expand Down
1 change: 1 addition & 0 deletions crates/libs/bindgen/src/types/method.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ impl Method {

let mut dependencies = TypeMap::new();
signature.dependencies(&mut dependencies);
dependencies.deprecated(def);

Self {
def,
Expand Down
2 changes: 2 additions & 0 deletions crates/libs/bindgen/src/types/struct.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,8 @@ impl Struct {
}

pub fn dependencies(&self, dependencies: &mut TypeMap) {
dependencies.deprecated(self.def);

for field in self.def.fields() {
field.ty(None).dependencies(dependencies);
}
Expand Down
32 changes: 15 additions & 17 deletions crates/libs/bindgen/src/writer/cfg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,28 +12,26 @@ impl Writer {
let mut arches = BTreeSet::new();

for attribute in row.attributes() {
match attribute.name() {
"SupportedArchitectureAttribute" => {
if let Some((_, Value::I32(value))) = attribute.args().first() {
if value & 1 == 1 {
arches.insert("x86");
}
if value & 2 == 2 {
arches.insert("x86_64");
arches.insert("arm64ec");
}
if value & 4 == 4 {
arches.insert("aarch64");
}
if attribute.name() == "SupportedArchitectureAttribute" {
if let Some((_, Value::I32(value))) = attribute.args().first() {
if value & 1 == 1 {
arches.insert("x86");
}
if value & 2 == 2 {
arches.insert("x86_64");
arches.insert("arm64ec");
}
if value & 4 == 4 {
arches.insert("aarch64");
}
}
"DeprecatedAttribute" => {
features.insert("deprecated".to_string());
}
_ => {}
}
}

if dependencies.is_deprecated() {
features.insert("deprecated".to_string());
}

let mut tokens = quote! {};

match arches.len() {
Expand Down
Loading
Loading