Skip to content

Commit

Permalink
Add static variable parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
madsmtm committed Oct 31, 2022
1 parent 2d381b0 commit e0ff895
Show file tree
Hide file tree
Showing 159 changed files with 10,906 additions and 471 deletions.
13 changes: 12 additions & 1 deletion header-translator/src/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ impl Expr {
res
}

pub fn parse(entity: &Entity<'_>, declaration_references: &[String]) -> Option<Self> {
pub fn parse_var(entity: &Entity<'_>) -> Option<Self> {
Self::parse(entity, &[])
}

fn parse(entity: &Entity<'_>, declaration_references: &[String]) -> Option<Self> {
let range = entity.get_range().expect("expr range");
let tokens = range.tokenize();

Expand Down Expand Up @@ -98,6 +102,13 @@ impl Expr {
}
}

s = s
.trim_start_matches("(NSBoxType)")
.trim_start_matches("(NSBezelStyle)")
.trim_start_matches("(NSEventSubtype)")
.trim_start_matches("(NSWindowButton)")
.to_string();

Some(Self { s })
}
}
Expand Down
3 changes: 3 additions & 0 deletions header-translator/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,9 @@ impl RustFile {
self.declared_types.insert(name.clone());
}
}
Stmt::VarDecl { name, .. } => {
self.declared_types.insert(name.clone());
}
Stmt::AliasDecl { name, .. } => {
self.declared_types.insert(name.clone());
}
Expand Down
40 changes: 40 additions & 0 deletions header-translator/src/rust_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -751,3 +751,43 @@ impl fmt::Display for RustTypeReturn {
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct RustTypeStatic {
inner: RustType,
}

impl RustTypeStatic {
pub fn parse(ty: Type<'_>) -> Self {
let inner = RustType::parse(ty, false, Nullability::Unspecified);

inner.visit_lifetime(|lifetime| {
if lifetime != Lifetime::Strong && lifetime != Lifetime::Unspecified {
panic!("unexpected lifetime in var {inner:?}");
}
});

Self { inner }
}
}

impl fmt::Display for RustTypeStatic {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.inner {
RustType::Id {
type_,
is_const: false,
lifetime: Lifetime::Strong | Lifetime::Unspecified,
nullability,
} => {
if *nullability == Nullability::NonNull {
write!(f, "&'static {type_}")
} else {
write!(f, "Option<&'static {type_}>")
}
}
ty @ RustType::Id { .. } => panic!("invalid static {ty:?}"),
ty => write!(f, "{ty}"),
}
}
}
69 changes: 59 additions & 10 deletions header-translator/src/stmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::config::{ClassData, Config};
use crate::expr::Expr;
use crate::method::Method;
use crate::property::Property;
use crate::rust_type::{GenericType, RustType};
use crate::rust_type::{GenericType, RustType, RustTypeStatic};
use crate::unexposed_macro::UnexposedMacro;

#[derive(Debug, Clone)]
Expand Down Expand Up @@ -208,6 +208,11 @@ pub enum Stmt {
kind: Option<UnexposedMacro>,
variants: Vec<(String, Expr)>,
},
VarDecl {
name: String,
ty: RustTypeStatic,
value: Option<Option<Expr>>,
},
/// typedef Type TypedefName;
AliasDecl { name: String, type_: RustType },
// /// typedef struct Name { fields } TypedefName;
Expand Down Expand Up @@ -429,20 +434,41 @@ impl Stmt {
})
}
EntityKind::VarDecl => {
// println!(
// "var: {:?}, {:?}, {:#?}, {:#?}",
// entity.get_display_name(),
// entity.get_name(),
// entity.has_attributes(),
// entity.get_children(),
// );
None
let name = entity.get_name().expect("var decl name");
let ty = entity.get_type().expect("var type");
let ty = RustTypeStatic::parse(ty);
let mut value = None;

entity.visit_children(|entity, _parent| {
match entity.get_kind() {
EntityKind::UnexposedAttr => {
if let Some(macro_) = UnexposedMacro::parse(&entity) {
panic!("unexpected attribute: {macro_:?}");
}
}
EntityKind::ObjCClassRef => {}
EntityKind::TypeRef => {}
kind if entity.is_expression() => {
if value.is_none() {
value = Some(Expr::parse_var(&entity));
} else {
panic!("got variable value twice")
}
}
_ => panic!("unknown typedef child in {name}: {entity:?}"),
};
EntityVisitResult::Continue
});

Some(Self::VarDecl { name, ty, value })
}
EntityKind::FunctionDecl => {
// println!(
// "function: {:?}, {:?}, {:#?}, {:#?}",
// "function: {:?}, {:?}, {:?}, {:?}, {:#?}, {:#?}",
// entity.get_display_name(),
// entity.get_name(),
// entity.is_static_method(),
// entity.is_inline_function(),
// entity.has_attributes(),
// entity.get_children(),
// );
Expand Down Expand Up @@ -591,6 +617,29 @@ impl fmt::Display for Stmt {
}
}
}
Self::VarDecl {
name,
ty,
value: None,
} => {
writeln!(f, r#"extern "C" {{"#)?;
writeln!(f, " static {name}: {ty};")?;
writeln!(f, "}}")?;
}
Self::VarDecl {
name,
ty,
value: Some(None),
} => {
writeln!(f, "static {name}: {ty} = todo;")?;
}
Self::VarDecl {
name,
ty,
value: Some(Some(expr)),
} => {
writeln!(f, "static {name}: {ty} = {expr};")?;
}
Self::AliasDecl { name, type_ } => {
writeln!(f, "pub type {name} = {type_};")?;
}
Expand Down
4 changes: 4 additions & 0 deletions icrate/src/AppKit/generated/NSAccessibility.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,10 @@ extern_methods!(
}
);

extern "C" {
static NSWorkspaceAccessibilityDisplayOptionsDidChangeNotification: &'static NSNotificationName;
}

extern_methods!(
/// NSAccessibilityAdditions
unsafe impl NSObject {
Expand Down
Loading

0 comments on commit e0ff895

Please sign in to comment.