Skip to content

Commit

Permalink
Refactor how superclasses and superprotocols are stored
Browse files Browse the repository at this point in the history
We need to know their "spatial" layout to do further analysis, i.e. need
to know the entire tree of which protocol needs which superprotocol.
  • Loading branch information
madsmtm committed Jan 15, 2025
1 parent 0715edd commit f67242b
Show file tree
Hide file tree
Showing 7 changed files with 302 additions and 224 deletions.
4 changes: 2 additions & 2 deletions crates/header-translator/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,9 +149,9 @@ pub struct ExternalData {
#[serde(rename = "thread-safety")]
#[serde(default)]
pub thread_safety: Option<String>,
#[serde(rename = "required-items")]
#[serde(rename = "super-items")]
#[serde(default)]
pub required_items: Vec<ItemIdentifier>,
pub super_items: Vec<ItemIdentifier>,
}

#[derive(Deserialize, Debug, Default, Clone, PartialEq, Eq)]
Expand Down
1 change: 1 addition & 0 deletions crates/header-translator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ mod method;
mod module;
mod name_translation;
mod objc2_utils;
mod protocol;
mod rust_type;
mod stmt;
mod thread_safety;
Expand Down
70 changes: 70 additions & 0 deletions crates/header-translator/src/protocol.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
use std::iter;

use clang::{Entity, EntityKind};

use crate::{immediate_children, Context, ItemIdentifier};

/// Parse the directly referenced protocols of a declaration.
pub(crate) fn parse_direct_protocols<'clang>(
entity: &Entity<'clang>,
_context: &Context<'_>,
) -> Vec<Entity<'clang>> {
let mut protocols = Vec::new();

#[allow(clippy::single_match)]
immediate_children(entity, |child, _span| match child.get_kind() {
EntityKind::ObjCProtocolRef => {
let child = child
.get_reference()
.expect("ObjCProtocolRef to reference entity");
if child == *entity {
error!(?entity, "recursive protocol");
} else {
protocols.push(child);
}
}
_ => {}
});

protocols
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ProtocolRef {
pub(crate) id: ItemIdentifier,
pub(crate) super_protocols: Vec<ProtocolRef>,
}

impl ProtocolRef {
pub(crate) fn super_protocols(entity: &Entity<'_>, context: &Context<'_>) -> Vec<Self> {
let mut super_protocols = Vec::new();

for entity in parse_direct_protocols(entity, context) {
super_protocols.push(ProtocolRef::from_entity(&entity, context));
}

super_protocols
}

pub(crate) fn from_entity(entity: &Entity<'_>, context: &Context<'_>) -> Self {
let mut super_protocols = Vec::new();

for entity in parse_direct_protocols(entity, context) {
super_protocols.push(ProtocolRef::from_entity(&entity, context));
}

Self {
id: context.replace_protocol_name(ItemIdentifier::new(&entity, context)),
super_protocols,
}
}

pub(crate) fn required_items(&self) -> Vec<ItemIdentifier> {
self.super_protocols
.iter()
.flat_map(|super_protocol| super_protocol.required_items())
.chain(iter::once(self.id.clone()))
.chain(iter::once(ItemIdentifier::objc("__macros__")))
.collect()
}
}
Loading

0 comments on commit f67242b

Please sign in to comment.