Skip to content
Open
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
27 changes: 26 additions & 1 deletion physx-sys/pxbind/src/consumer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,31 @@ pub enum Item {
kind: Type,
},
ConstantExpr {
value: Option<String>,
#[serde(rename = "type")]
kind: Type,
},
CXXBoolLiteralExpr {
value: bool,
#[serde(rename = "type")]
kind: Type,
},
IntegerLiteral {
value: String,
#[serde(rename = "type")]
kind: Type,
},
FloatingLiteral {
value: String,
#[serde(rename = "type")]
kind: Type,
},
StringLiteral {
value: String,
#[serde(rename = "type")]
kind: Type,
},
UserDefinedLiteral {
value: String,
#[serde(rename = "type")]
kind: Type,
Expand All @@ -111,7 +136,7 @@ pub enum Item {
TemplateArgument {
#[serde(rename = "type")]
kind: Option<Type>,
value: Option<i32>,
value: Option<i64>,
},
RecordType {
#[serde(rename = "type")]
Expand Down
4 changes: 3 additions & 1 deletion physx-sys/pxbind/src/consumer/enums.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,9 @@ impl<'ast> super::AstConsumer<'ast> {
}
}

return value.parse().context("failed to parse enum constant");
if let Some(v) = value {
return v.parse().context("failed to parse enum constant");
}
}
_ => continue,
}
Expand Down
34 changes: 31 additions & 3 deletions physx-sys/pxbind/src/consumer/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ pub struct Function {
pub struct Param<'ast> {
pub name: Cow<'ast, str>,
pub kind: QualType<'ast>,
pub default_value: Option<String>,
}

impl<'ast> Param<'ast> {
Expand All @@ -27,6 +28,7 @@ impl<'ast> Param<'ast> {
is_pointee_const: is_const,
pointee: Box::new(rec_type),
},
default_value: None,
}
}
}
Expand Down Expand Up @@ -181,12 +183,34 @@ impl<'ast> super::AstConsumer<'ast> {
template_types: &[(&str, &TemplateArg<'ast>)],
func: &mut FuncBinding<'ast>,
) -> anyhow::Result<()> {
for (i, param) in node
for (i, (param, default_value)) in node
.inner
.iter()
.filter_map(|inn| {
if let Item::ParmVarDecl(param) = &inn.kind {
Some(param)
if inn.inner.len() > 0 {
let default_value: Option<String> = match &inn.inner[0].kind {
Item::IntegerLiteral { value, kind: _ } => Some(value.to_owned()),
Item::FloatingLiteral { value, kind: _ } => {
// Clang ast-dump float formatting is not human-friendly
// i.e. "1.01" formats as "1.00999999"
// rust format! fixes this
value.parse::<f32>().ok().map(|value| format!("{value}"))
}
Item::StringLiteral { value, kind: _ } => Some(value.to_owned()),
Item::UserDefinedLiteral { value, kind: _ } => Some(value.to_owned()),
Item::CXXBoolLiteralExpr { value, kind: _ } => Some(if *value {
"true".to_owned()
} else {
"false".to_owned()
}),
_ => None,
};

Some((param, default_value))
} else {
Some((param, None))
}
} else {
None
}
Expand All @@ -206,7 +230,11 @@ impl<'ast> super::AstConsumer<'ast> {
)
})?;

func.params.push(Param { name: pname, kind });
func.params.push(Param {
name: pname,
kind,
default_value: default_value,
});
}

Ok(())
Expand Down
14 changes: 13 additions & 1 deletion physx-sys/pxbind/src/generator/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,8 +190,20 @@ impl<'ast> FuncBinding<'ast> {
}

let indent = Indent(level);

let mut acc = String::new();

// write default values of params
let mut first_param = true;
for param in self.params.iter() {
if let Some(v) = &param.default_value {
if first_param {
writesln!(acc, "{indent}///");
first_param = false;
}
writesln!(acc, "{indent}/// {} = {}", param.name, v);
}
}

writes!(acc, "{indent}pub fn {}(", self.name);

for (i, param) in self.params.iter().enumerate() {
Expand Down