diff --git a/physx-sys/pxbind/src/consumer.rs b/physx-sys/pxbind/src/consumer.rs index bf1e767c..1b35b358 100644 --- a/physx-sys/pxbind/src/consumer.rs +++ b/physx-sys/pxbind/src/consumer.rs @@ -100,6 +100,31 @@ pub enum Item { kind: Type, }, ConstantExpr { + value: Option, + #[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, @@ -111,7 +136,7 @@ pub enum Item { TemplateArgument { #[serde(rename = "type")] kind: Option, - value: Option, + value: Option, }, RecordType { #[serde(rename = "type")] diff --git a/physx-sys/pxbind/src/consumer/enums.rs b/physx-sys/pxbind/src/consumer/enums.rs index 27726019..4f21dbd7 100644 --- a/physx-sys/pxbind/src/consumer/enums.rs +++ b/physx-sys/pxbind/src/consumer/enums.rs @@ -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, } diff --git a/physx-sys/pxbind/src/consumer/functions.rs b/physx-sys/pxbind/src/consumer/functions.rs index 3b5f9ef1..92190f6b 100644 --- a/physx-sys/pxbind/src/consumer/functions.rs +++ b/physx-sys/pxbind/src/consumer/functions.rs @@ -15,6 +15,7 @@ pub struct Function { pub struct Param<'ast> { pub name: Cow<'ast, str>, pub kind: QualType<'ast>, + pub default_value: Option, } impl<'ast> Param<'ast> { @@ -27,6 +28,7 @@ impl<'ast> Param<'ast> { is_pointee_const: is_const, pointee: Box::new(rec_type), }, + default_value: None, } } } @@ -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 = 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::().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 } @@ -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(()) diff --git a/physx-sys/pxbind/src/generator/functions.rs b/physx-sys/pxbind/src/generator/functions.rs index 20c92c6c..ad233527 100644 --- a/physx-sys/pxbind/src/generator/functions.rs +++ b/physx-sys/pxbind/src/generator/functions.rs @@ -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) = ¶m.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() {