2018-03-10 20:07:12 +11:00
|
|
|
#![recursion_limit = "256"]
|
2018-11-17 03:57:08 +11:00
|
|
|
extern crate heck;
|
|
|
|
extern crate itertools;
|
2018-06-24 20:09:37 +10:00
|
|
|
#[macro_use]
|
|
|
|
extern crate nom;
|
2018-04-01 18:52:21 +10:00
|
|
|
extern crate proc_macro2;
|
2018-03-10 07:45:58 +11:00
|
|
|
#[macro_use]
|
|
|
|
extern crate quote;
|
|
|
|
extern crate syn;
|
2018-06-04 19:26:14 +10:00
|
|
|
pub extern crate vk_parse;
|
2018-03-10 07:45:58 +11:00
|
|
|
pub extern crate vkxml;
|
|
|
|
|
2018-06-04 19:26:14 +10:00
|
|
|
use heck::{CamelCase, ShoutySnakeCase, SnakeCase};
|
2018-07-07 22:49:17 +10:00
|
|
|
use itertools::Itertools;
|
2018-07-30 06:39:45 +10:00
|
|
|
use proc_macro2::Term;
|
2018-03-10 20:47:02 +11:00
|
|
|
use quote::Tokens;
|
2018-07-30 06:39:45 +10:00
|
|
|
use std::collections::{HashMap, HashSet};
|
2018-07-21 20:56:16 +10:00
|
|
|
use std::path::Path;
|
2018-03-10 07:45:58 +11:00
|
|
|
use syn::Ident;
|
2018-07-21 20:56:16 +10:00
|
|
|
pub trait ExtensionExt {}
|
2018-06-24 20:09:37 +10:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub enum CType {
|
|
|
|
USize,
|
|
|
|
U32,
|
|
|
|
U64,
|
|
|
|
Float,
|
2018-08-03 23:39:25 +10:00
|
|
|
Bool32,
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
2018-07-21 20:56:16 +10:00
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
impl CType {
|
2018-07-30 16:54:03 +10:00
|
|
|
fn to_tokens(self) -> Tokens {
|
2018-06-24 20:09:37 +10:00
|
|
|
let term = match self {
|
|
|
|
CType::USize => Term::intern("usize"),
|
|
|
|
CType::U32 => Term::intern("u32"),
|
|
|
|
CType::U64 => Term::intern("u64"),
|
|
|
|
CType::Float => Term::intern("f32"),
|
2018-08-03 23:39:25 +10:00
|
|
|
CType::Bool32 => Term::intern("Bool32"),
|
2018-06-24 20:09:37 +10:00
|
|
|
};
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {#term}
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-03 20:34:14 +10:00
|
|
|
named!(ctype<&str, CType>,
|
2018-06-24 20:09:37 +10:00
|
|
|
alt!(
|
|
|
|
tag!("ULL") => { |_| CType::U64 } |
|
2018-07-21 20:56:16 +10:00
|
|
|
tag!("U") => { |_| CType::U32 }
|
2018-06-24 20:09:37 +10:00
|
|
|
)
|
|
|
|
);
|
|
|
|
named!(cexpr<&str, (CType, String)>,
|
|
|
|
alt!(
|
|
|
|
map!(cfloat, |f| (CType::Float, format!("{:.2}", f))) |
|
|
|
|
inverse_number
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
named!(inverse_number<&str, (CType, String)>,
|
|
|
|
do_parse!(
|
|
|
|
tag!("(")>>
|
|
|
|
tag!("~") >>
|
2018-08-03 20:34:14 +10:00
|
|
|
s: take_while1!(|c: char| c.is_digit(10)) >>
|
2018-06-24 20:09:37 +10:00
|
|
|
ctype: ctype >>
|
|
|
|
minus_num: opt!(
|
|
|
|
do_parse!(
|
|
|
|
tag!("-") >>
|
2018-08-03 20:34:14 +10:00
|
|
|
n: take_while1!(|c: char| c.is_digit(10)) >>
|
2018-06-24 20:09:37 +10:00
|
|
|
(n)
|
|
|
|
)
|
|
|
|
) >>
|
|
|
|
tag!(")") >>
|
|
|
|
(
|
|
|
|
{
|
|
|
|
let expr = if let Some(minus) = minus_num {
|
|
|
|
format!("!{}-{}", s, minus)
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
format!("!{}", s)
|
|
|
|
};
|
|
|
|
(ctype, expr)
|
|
|
|
}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
named!(cfloat<&str, f32>,
|
|
|
|
terminated!(nom::float_s, char!('f'))
|
|
|
|
);
|
|
|
|
|
|
|
|
pub fn define_handle_macro() -> Tokens {
|
|
|
|
quote! {
|
2018-11-17 03:25:20 +11:00
|
|
|
#[macro_export]
|
2018-06-24 20:09:37 +10:00
|
|
|
macro_rules! define_handle{
|
2018-08-02 10:32:16 +10:00
|
|
|
($name: ident, $ty: ident) => {
|
2018-08-03 20:34:14 +10:00
|
|
|
#[repr(transparent)]
|
2018-09-17 05:15:01 +10:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash)]
|
2018-08-02 10:32:16 +10:00
|
|
|
pub struct $name(*mut u8);
|
|
|
|
impl Default for $name {
|
2018-07-31 03:53:12 +10:00
|
|
|
fn default() -> $name {
|
|
|
|
$name::null()
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
2018-08-02 10:32:16 +10:00
|
|
|
impl Handle for $name {
|
|
|
|
const TYPE: ObjectType = ObjectType::$ty;
|
|
|
|
fn as_raw(self) -> u64 { self.0 as u64 }
|
|
|
|
fn from_raw(x: u64) -> Self { $name(x as _) }
|
|
|
|
}
|
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
unsafe impl Send for $name {}
|
|
|
|
unsafe impl Sync for $name {}
|
|
|
|
|
|
|
|
impl $name{
|
2018-07-31 03:53:12 +10:00
|
|
|
pub fn null() -> Self{
|
2018-08-02 10:32:16 +10:00
|
|
|
$name(::std::ptr::null_mut())
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 05:15:01 +10:00
|
|
|
|
|
|
|
impl fmt::Pointer for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Pointer::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Debug for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
fmt::Debug::fmt(&self.0, f)
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn handle_nondispatchable_macro() -> Tokens {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-17 03:25:20 +11:00
|
|
|
#[macro_export]
|
2018-06-24 20:09:37 +10:00
|
|
|
macro_rules! handle_nondispatchable {
|
2018-08-02 10:32:16 +10:00
|
|
|
($name: ident, $ty: ident) => {
|
2018-08-03 20:34:14 +10:00
|
|
|
#[repr(transparent)]
|
2018-07-31 03:53:12 +10:00
|
|
|
#[derive(Eq, PartialEq, Ord, PartialOrd, Clone, Copy, Hash, Default)]
|
2018-08-25 21:09:44 +10:00
|
|
|
pub struct $name(u64);
|
2018-08-02 10:32:16 +10:00
|
|
|
|
|
|
|
impl Handle for $name {
|
|
|
|
const TYPE: ObjectType = ObjectType::$ty;
|
|
|
|
fn as_raw(self) -> u64 { self.0 as u64 }
|
|
|
|
fn from_raw(x: u64) -> Self { $name(x as _) }
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
impl $name{
|
|
|
|
pub fn null() -> $name{
|
|
|
|
$name(0)
|
|
|
|
}
|
|
|
|
}
|
2018-09-17 05:15:01 +10:00
|
|
|
|
|
|
|
impl fmt::Pointer for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-06-24 20:09:37 +10:00
|
|
|
write!(f, "0x{:x}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-17 05:15:01 +10:00
|
|
|
impl fmt::Debug for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-06-24 20:09:37 +10:00
|
|
|
write!(f, "0x{:x}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 22:49:17 +10:00
|
|
|
pub fn vk_version_macros() -> Tokens {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-07 22:49:17 +10:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! vk_make_version {
|
|
|
|
($major:expr, $minor:expr, $patch:expr) => {
|
|
|
|
(($major as u32) << 22) | (($minor as u32) << 12) | $patch as u32
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! vk_version_major {
|
|
|
|
($major:expr) => {
|
2018-08-25 17:48:49 +10:00
|
|
|
($major as u32) >> 22
|
2018-07-07 22:49:17 +10:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! vk_version_minor {
|
|
|
|
($minor:expr) => {
|
2018-08-25 17:48:49 +10:00
|
|
|
(($minor as u32) >> 12) & 0x3ff
|
2018-07-07 22:49:17 +10:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! vk_version_patch {
|
|
|
|
($minor:expr) => {
|
2018-08-25 17:48:49 +10:00
|
|
|
($minor as u32) & 0xfff
|
2018-07-07 22:49:17 +10:00
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
pub fn vk_bitflags_wrapped_macro() -> Tokens {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-17 03:25:20 +11:00
|
|
|
#[macro_export]
|
2018-06-24 20:09:37 +10:00
|
|
|
macro_rules! vk_bitflags_wrapped {
|
|
|
|
($name: ident, $all: expr, $flag_type: ty) => {
|
|
|
|
|
|
|
|
impl Default for $name{
|
|
|
|
fn default() -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name(0)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
2018-09-17 05:15:01 +10:00
|
|
|
impl fmt::Debug for $name {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2018-07-11 21:18:22 +10:00
|
|
|
write!(f, "{}({:b})", stringify!($name), self.0)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl $name {
|
|
|
|
#[inline]
|
|
|
|
pub fn empty() -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name(0)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn all() -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name($all)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 04:59:55 +10:00
|
|
|
pub fn from_raw(x: $flag_type) -> Self { $name(x) }
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
#[inline]
|
2018-09-17 04:59:55 +10:00
|
|
|
pub fn as_raw(self) -> $flag_type { self.0 }
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_empty(self) -> bool {
|
|
|
|
self == $name::empty()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn is_all(self) -> bool {
|
|
|
|
self & $name::all() == $name::all()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
pub fn intersects(self, other: $name) -> bool {
|
|
|
|
self & other != $name::empty()
|
|
|
|
}
|
|
|
|
|
2018-09-17 04:59:55 +10:00
|
|
|
/// Returns whether `other` is a subset of `self`
|
2018-06-24 20:09:37 +10:00
|
|
|
#[inline]
|
2018-09-17 04:59:55 +10:00
|
|
|
pub fn contains(self, other: $name) -> bool {
|
2018-06-24 20:09:37 +10:00
|
|
|
self & other == other
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitOr for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn bitor(self, rhs: $name) -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name (self.0 | rhs.0 )
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitOrAssign for $name {
|
|
|
|
#[inline]
|
|
|
|
fn bitor_assign(&mut self, rhs: $name) {
|
|
|
|
*self = *self | rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitAnd for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn bitand(self, rhs: $name) -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name (self.0 & rhs.0)
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitAndAssign for $name {
|
|
|
|
#[inline]
|
|
|
|
fn bitand_assign(&mut self, rhs: $name) {
|
|
|
|
*self = *self & rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitXor for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn bitxor(self, rhs: $name) -> $name {
|
2018-07-11 21:18:22 +10:00
|
|
|
$name (self.0 ^ rhs.0 )
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::BitXorAssign for $name {
|
|
|
|
#[inline]
|
|
|
|
fn bitxor_assign(&mut self, rhs: $name) {
|
|
|
|
*self = *self ^ rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Sub for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn sub(self, rhs: $name) -> $name {
|
|
|
|
self & !rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::SubAssign for $name {
|
|
|
|
#[inline]
|
|
|
|
fn sub_assign(&mut self, rhs: $name) {
|
|
|
|
*self = *self - rhs
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl ::std::ops::Not for $name {
|
|
|
|
type Output = $name;
|
|
|
|
|
|
|
|
#[inline]
|
|
|
|
fn not(self) -> $name {
|
|
|
|
self ^ $name::all()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 20:54:31 +10:00
|
|
|
|
|
|
|
pub fn platform_specific_types() -> Tokens {
|
|
|
|
quote! {
|
|
|
|
pub type RROutput = c_ulong;
|
|
|
|
pub type VisualID = c_uint;
|
|
|
|
pub type Display = *const c_void;
|
|
|
|
pub type Window = c_ulong;
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type xcb_connection_t = *const c_void;
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type xcb_window_t = u32;
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type xcb_visualid_t = *const c_void;
|
|
|
|
pub type MirConnection = *const c_void;
|
|
|
|
pub type MirSurface = *const c_void;
|
|
|
|
pub type HINSTANCE = *const c_void;
|
|
|
|
pub type HWND = *const c_void;
|
|
|
|
#[allow(non_camel_case_types)]
|
2018-10-24 00:45:27 +11:00
|
|
|
pub type wl_display = c_void;
|
2018-07-07 20:54:31 +10:00
|
|
|
#[allow(non_camel_case_types)]
|
2018-10-24 00:45:27 +11:00
|
|
|
pub type wl_surface = c_void;
|
2018-07-07 20:54:31 +10:00
|
|
|
pub type HANDLE = *mut c_void;
|
|
|
|
pub type DWORD = c_ulong;
|
2018-08-29 03:01:16 +10:00
|
|
|
pub type LPCWSTR = *const u16;
|
2018-11-18 06:04:44 +11:00
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type zx_handle_t = u32;
|
2018-07-07 20:54:31 +10:00
|
|
|
|
2018-11-21 03:55:06 +11:00
|
|
|
// FIXME: Platform specific types that should come from a library id:0
|
2018-07-07 20:54:31 +10:00
|
|
|
// typedefs are only here so that the code compiles for now
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type SECURITY_ATTRIBUTES = ();
|
|
|
|
// Opage types
|
|
|
|
pub type ANativeWindow = c_void;
|
|
|
|
pub type AHardwareBuffer = c_void;
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum ConstVal {
|
|
|
|
U32(u32),
|
|
|
|
U64(u64),
|
|
|
|
Float(f32),
|
|
|
|
}
|
|
|
|
impl ConstVal {
|
|
|
|
pub fn bits(&self) -> u64 {
|
|
|
|
match self {
|
|
|
|
ConstVal::U64(n) => *n,
|
|
|
|
_ => panic!("Constval not supported"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-11 21:18:22 +10:00
|
|
|
pub trait ConstantExt {
|
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident;
|
|
|
|
fn to_tokens(&self) -> Tokens;
|
|
|
|
fn notation(&self) -> Option<&str>;
|
|
|
|
}
|
|
|
|
|
2018-07-21 20:56:16 +10:00
|
|
|
impl ConstantExt for vkxml::ExtensionEnum {
|
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident {
|
|
|
|
variant_ident(enum_name, &self.name)
|
|
|
|
}
|
|
|
|
fn to_tokens(&self) -> Tokens {
|
|
|
|
Constant::from_extension_enum(self).expect("").to_tokens()
|
|
|
|
}
|
|
|
|
fn notation(&self) -> Option<&str> {
|
|
|
|
self.notation.as_ref().map(|s| s.as_str())
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
|
2018-07-11 21:18:22 +10:00
|
|
|
impl ConstantExt for vkxml::Constant {
|
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident {
|
|
|
|
variant_ident(enum_name, &self.name)
|
|
|
|
}
|
|
|
|
fn to_tokens(&self) -> Tokens {
|
|
|
|
Constant::from_constant(self).to_tokens()
|
|
|
|
}
|
|
|
|
fn notation(&self) -> Option<&str> {
|
|
|
|
self.notation.as_ref().map(|s| s.as_str())
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 20:56:16 +10:00
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
#[derive(Debug)]
|
2018-04-01 18:52:21 +10:00
|
|
|
pub enum Constant {
|
|
|
|
Number(i32),
|
|
|
|
Hex(String),
|
|
|
|
BitPos(u32),
|
|
|
|
CExpr(vkxml::CExpression),
|
|
|
|
Text(String),
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
impl quote::ToTokens for ConstVal {
|
|
|
|
fn to_tokens(&self, tokens: &mut Tokens) {
|
|
|
|
match self {
|
|
|
|
ConstVal::U32(n) => n.to_tokens(tokens),
|
|
|
|
ConstVal::U64(n) => n.to_tokens(tokens),
|
|
|
|
ConstVal::Float(f) => f.to_tokens(tokens),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-04-01 18:52:21 +10:00
|
|
|
impl Constant {
|
2018-06-24 20:09:37 +10:00
|
|
|
// pub fn type(&self) -> Type {
|
|
|
|
|
|
|
|
// }
|
|
|
|
pub fn value(&self) -> Option<ConstVal> {
|
2018-04-01 18:52:21 +10:00
|
|
|
match *self {
|
2018-06-24 20:09:37 +10:00
|
|
|
Constant::Number(n) => Some(ConstVal::U64(n as u64)),
|
|
|
|
Constant::Hex(ref hex) => u64::from_str_radix(&hex, 16).ok().map(ConstVal::U64),
|
|
|
|
Constant::BitPos(pos) => Some(ConstVal::U64((1 << pos) as u64)),
|
2018-04-01 18:52:21 +10:00
|
|
|
_ => None,
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
pub fn ty(&self) -> CType {
|
|
|
|
match self {
|
|
|
|
Constant::Number(_) | Constant::Hex(_) => CType::USize,
|
|
|
|
Constant::CExpr(expr) => {
|
|
|
|
let (_, (ty, _)) = cexpr(expr).expect("Unable to parse cexpr");
|
|
|
|
ty
|
|
|
|
}
|
|
|
|
_ => unimplemented!(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-04-01 18:52:21 +10:00
|
|
|
pub fn to_tokens(&self) -> Tokens {
|
|
|
|
match *self {
|
|
|
|
Constant::Number(n) => {
|
|
|
|
let term = Term::intern(&n.to_string());
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {#term}
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
|
|
|
Constant::Hex(ref s) => {
|
2018-06-04 19:26:14 +10:00
|
|
|
let term = Term::intern(&format!("0x{}", s));
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {#term}
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
|
|
|
Constant::Text(ref text) => {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {#text}
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
Constant::CExpr(ref expr) => {
|
|
|
|
let (_, (_, rexpr)) = cexpr(expr).expect("Unable to parse cexpr");
|
|
|
|
let term = Term::intern(rexpr.as_str());
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {#term}
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
|
|
|
Constant::BitPos(pos) => {
|
|
|
|
let value = 1 << pos;
|
|
|
|
let term = Term::intern(&format!("0b{:b}", value));
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {#term}
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-21 20:56:16 +10:00
|
|
|
pub fn from_extension_enum(constant: &vkxml::ExtensionEnum) -> Option<Self> {
|
2018-07-30 16:54:03 +10:00
|
|
|
let number = constant.number.map(Constant::Number);
|
2018-07-11 21:18:22 +10:00
|
|
|
let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone()));
|
2018-07-30 16:54:03 +10:00
|
|
|
let bitpos = constant.bitpos.map(Constant::BitPos);
|
2018-07-11 21:18:22 +10:00
|
|
|
let expr = constant
|
|
|
|
.c_expression
|
|
|
|
.as_ref()
|
|
|
|
.map(|e| Constant::CExpr(e.clone()));
|
2018-07-21 20:56:16 +10:00
|
|
|
number.or(hex).or(bitpos).or(expr)
|
2018-07-11 21:18:22 +10:00
|
|
|
}
|
2018-07-30 16:54:03 +10:00
|
|
|
|
2018-04-01 18:52:21 +10:00
|
|
|
pub fn from_constant(constant: &vkxml::Constant) -> Self {
|
2018-07-30 16:54:03 +10:00
|
|
|
let number = constant.number.map(Constant::Number);
|
2018-04-01 18:52:21 +10:00
|
|
|
let hex = constant.hex.as_ref().map(|hex| Constant::Hex(hex.clone()));
|
2018-07-30 16:54:03 +10:00
|
|
|
let bitpos = constant.bitpos.map(Constant::BitPos);
|
2018-04-01 18:52:21 +10:00
|
|
|
let expr = constant
|
|
|
|
.c_expression
|
|
|
|
.as_ref()
|
|
|
|
.map(|e| Constant::CExpr(e.clone()));
|
|
|
|
number.or(hex).or(bitpos).or(expr).expect("")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-10 08:43:45 +11:00
|
|
|
pub trait FeatureExt {
|
|
|
|
fn version_string(&self) -> String;
|
|
|
|
}
|
|
|
|
impl FeatureExt for vkxml::Feature {
|
|
|
|
fn version_string(&self) -> String {
|
2018-07-07 20:54:31 +10:00
|
|
|
let mut version = format!("{}", self.version);
|
|
|
|
if version.len() == 1 {
|
|
|
|
version = format!("{}_0", version)
|
|
|
|
}
|
2018-03-10 08:43:45 +11:00
|
|
|
version.replace(".", "_")
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 20:54:31 +10:00
|
|
|
#[derive(Debug, Copy, Clone)]
|
|
|
|
pub enum FunctionType {
|
|
|
|
Static,
|
|
|
|
Entry,
|
|
|
|
Instance,
|
|
|
|
Device,
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
pub trait CommandExt {
|
|
|
|
/// Returns the ident in snake_case and without the 'vk' prefix.
|
2018-07-07 20:54:31 +10:00
|
|
|
fn function_type(&self) -> FunctionType;
|
2018-03-10 07:45:58 +11:00
|
|
|
///
|
|
|
|
/// Returns true if the command is a device level command. This is indicated by
|
|
|
|
/// the type of the first parameter.
|
|
|
|
fn command_ident(&self) -> Ident;
|
|
|
|
}
|
|
|
|
|
|
|
|
impl CommandExt for vkxml::Command {
|
|
|
|
fn command_ident(&self) -> Ident {
|
|
|
|
Ident::from(self.name[2..].to_snake_case().as_str())
|
|
|
|
}
|
|
|
|
|
2018-07-07 20:54:31 +10:00
|
|
|
fn function_type(&self) -> FunctionType {
|
2018-11-18 05:05:28 +11:00
|
|
|
let is_first_param_device = self
|
|
|
|
.param
|
2018-07-30 16:54:03 +10:00
|
|
|
.get(0)
|
2018-03-10 07:45:58 +11:00
|
|
|
.map(|field| match field.basetype.as_str() {
|
|
|
|
"VkDevice" | "VkCommandBuffer" | "VkQueue" => true,
|
|
|
|
_ => false,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.unwrap_or(false);
|
2018-07-07 20:54:31 +10:00
|
|
|
match self.name.as_str() {
|
|
|
|
"vkGetInstanceProcAddr" => FunctionType::Static,
|
|
|
|
"vkCreateInstance"
|
|
|
|
| "vkEnumerateInstanceLayerProperties"
|
2018-08-22 17:24:50 +10:00
|
|
|
| "vkEnumerateInstanceExtensionProperties"
|
|
|
|
| "vkEnumerateInstanceVersion" => FunctionType::Entry,
|
2018-07-07 20:54:31 +10:00
|
|
|
// This is actually not a device level function
|
|
|
|
"vkGetDeviceProcAddr" => FunctionType::Instance,
|
|
|
|
_ => {
|
|
|
|
if is_first_param_device {
|
|
|
|
FunctionType::Device
|
|
|
|
} else {
|
|
|
|
FunctionType::Instance
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub trait FieldExt {
|
|
|
|
/// Returns the name of the paramter that doesn't clash with Rusts resevered
|
|
|
|
/// keywords
|
|
|
|
fn param_ident(&self) -> Ident;
|
|
|
|
|
|
|
|
/// Returns the basetype ident and removes the 'Vk' prefix
|
2018-03-10 20:47:02 +11:00
|
|
|
fn type_tokens(&self) -> Tokens;
|
2018-06-24 20:09:37 +10:00
|
|
|
fn is_clone(&self) -> bool;
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
pub trait ToTokens {
|
2018-07-31 22:17:24 +10:00
|
|
|
fn to_tokens(&self, is_const: bool) -> Tokens;
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
impl ToTokens for vkxml::ReferenceType {
|
2018-07-31 22:17:24 +10:00
|
|
|
fn to_tokens(&self, is_const: bool) -> Tokens {
|
2018-06-24 20:09:37 +10:00
|
|
|
let ptr_name = match self {
|
2018-07-31 22:17:24 +10:00
|
|
|
vkxml::ReferenceType::Pointer => {
|
|
|
|
if is_const {
|
|
|
|
"*const"
|
|
|
|
} else {
|
|
|
|
"*mut"
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 20:54:31 +10:00
|
|
|
vkxml::ReferenceType::PointerToPointer => "*mut *mut",
|
2018-07-31 22:17:24 +10:00
|
|
|
vkxml::ReferenceType::PointerToConstPointer => {
|
|
|
|
if is_const {
|
|
|
|
"*const *const"
|
|
|
|
} else {
|
|
|
|
"*mut *const"
|
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
};
|
|
|
|
let ident = Term::intern(ptr_name);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-06-24 20:09:37 +10:00
|
|
|
#ident
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
fn name_to_tokens(type_name: &str) -> Ident {
|
2018-03-11 02:21:35 +11:00
|
|
|
let new_name = match type_name {
|
2018-08-25 21:09:44 +10:00
|
|
|
"uint8_t" => "u8",
|
|
|
|
"uint16_t" => "u16",
|
|
|
|
"uint32_t" => "u32",
|
|
|
|
"uint64_t" => "u64",
|
|
|
|
"int8_t" => "i8",
|
|
|
|
"int16_t" => "i16",
|
|
|
|
"int32_t" => "i32",
|
|
|
|
"int64_t" => "i64",
|
|
|
|
"size_t" => "usize",
|
2018-06-24 20:09:37 +10:00
|
|
|
"int" => "c_int",
|
2018-03-11 02:21:35 +11:00
|
|
|
"void" => "c_void",
|
|
|
|
"char" => "c_char",
|
2018-09-30 04:21:56 +10:00
|
|
|
"float" => "f32",
|
2018-03-11 02:21:35 +11:00
|
|
|
"long" => "c_ulong",
|
|
|
|
_ => {
|
2018-06-24 20:09:37 +10:00
|
|
|
if type_name.starts_with("Vk") {
|
2018-03-11 02:21:35 +11:00
|
|
|
&type_name[2..]
|
|
|
|
} else {
|
|
|
|
type_name
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2018-06-24 20:09:37 +10:00
|
|
|
let new_name = new_name.replace("FlagBits", "Flags");
|
2018-07-30 06:39:45 +10:00
|
|
|
Ident::from(new_name.as_str())
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
fn to_type_tokens(type_name: &str, reference: Option<&vkxml::ReferenceType>) -> Tokens {
|
|
|
|
let new_name = name_to_tokens(type_name);
|
2018-12-07 20:33:36 +11:00
|
|
|
let ptr_name = reference.map(|r| r.to_tokens(false)).unwrap_or(quote! {});
|
|
|
|
quote! {#ptr_name #new_name}
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
|
2018-03-10 07:45:58 +11:00
|
|
|
impl FieldExt for vkxml::Field {
|
2018-06-24 20:09:37 +10:00
|
|
|
fn is_clone(&self) -> bool {
|
|
|
|
true
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
fn param_ident(&self) -> Ident {
|
|
|
|
let name = self.name.as_ref().map(|s| s.as_str()).unwrap_or("field");
|
|
|
|
let name_corrected = match name {
|
|
|
|
"type" => "ty",
|
|
|
|
_ => name,
|
|
|
|
};
|
|
|
|
Ident::from(name_corrected.to_snake_case().as_str())
|
|
|
|
}
|
|
|
|
|
2018-03-10 20:47:02 +11:00
|
|
|
fn type_tokens(&self) -> Tokens {
|
2018-06-24 20:09:37 +10:00
|
|
|
let ty = name_to_tokens(&self.basetype);
|
2018-11-18 05:05:28 +11:00
|
|
|
let pointer = self
|
|
|
|
.reference
|
2018-06-24 20:09:37 +10:00
|
|
|
.as_ref()
|
2018-07-31 22:17:24 +10:00
|
|
|
.map(|r| r.to_tokens(self.is_const))
|
2018-12-07 20:33:36 +11:00
|
|
|
.unwrap_or(quote! {});
|
|
|
|
let pointer_ty = quote! {
|
2018-06-24 20:09:37 +10:00
|
|
|
#pointer #ty
|
|
|
|
};
|
|
|
|
let array = self.array.as_ref().and_then(|arraytype| match arraytype {
|
|
|
|
vkxml::ArrayType::Static => {
|
2018-11-18 05:05:28 +11:00
|
|
|
let size = self
|
|
|
|
.size
|
2018-06-24 20:09:37 +10:00
|
|
|
.as_ref()
|
2018-07-30 16:54:03 +10:00
|
|
|
.or_else(|| self.size_enumref.as_ref())
|
2018-06-24 20:09:37 +10:00
|
|
|
.expect("Should have size");
|
2018-08-03 20:34:14 +10:00
|
|
|
// Make sure we also rename the constant, that is
|
2018-08-01 17:22:28 +10:00
|
|
|
// used inside the static array
|
|
|
|
let size = constant_name(size);
|
|
|
|
let size = Term::intern(&size);
|
2018-12-07 20:33:36 +11:00
|
|
|
Some(quote! {
|
2018-06-24 20:09:37 +10:00
|
|
|
[#ty; #size]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
array.unwrap_or(pointer_ty)
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
2018-03-10 07:45:58 +11:00
|
|
|
pub type CommandMap<'a> = HashMap<vkxml::Identifier, &'a vkxml::Command>;
|
2018-03-10 20:07:12 +11:00
|
|
|
|
2018-11-17 03:57:08 +11:00
|
|
|
fn generate_function_pointers<'a>(
|
|
|
|
ident: Ident,
|
|
|
|
commands: &[&'a vkxml::Command],
|
|
|
|
fn_cache: &mut HashSet<&'a str>,
|
|
|
|
) -> quote::Tokens {
|
2018-03-10 20:07:12 +11:00
|
|
|
let names: Vec<_> = commands.iter().map(|cmd| cmd.command_ident()).collect();
|
|
|
|
let names_ref = &names;
|
2018-11-04 09:37:20 +11:00
|
|
|
let names_ref1 = &names;
|
|
|
|
let names_ref2 = &names;
|
|
|
|
let names_ref3 = &names;
|
2018-03-10 20:07:12 +11:00
|
|
|
let raw_names: Vec<_> = commands
|
|
|
|
.iter()
|
|
|
|
.map(|cmd| Ident::from(cmd.name.as_str()))
|
|
|
|
.collect();
|
|
|
|
let raw_names_ref = &raw_names;
|
|
|
|
let names_left = &names;
|
|
|
|
let names_right = &names;
|
|
|
|
|
2018-11-17 03:57:08 +11:00
|
|
|
let pfn_commands: Vec<_> = commands
|
|
|
|
.iter()
|
|
|
|
.filter(|cmd| {
|
|
|
|
let ident = cmd.name.as_str();
|
|
|
|
if !fn_cache.contains(ident) {
|
|
|
|
fn_cache.insert(ident);
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-11-17 03:57:08 +11:00
|
|
|
|
2018-03-10 20:47:02 +11:00
|
|
|
let params: Vec<Vec<(Ident, Tokens)>> = commands
|
2018-03-10 20:07:12 +11:00
|
|
|
.iter()
|
|
|
|
.map(|cmd| {
|
2018-11-18 05:05:28 +11:00
|
|
|
let params: Vec<_> = cmd
|
|
|
|
.param
|
2018-03-10 20:07:12 +11:00
|
|
|
.iter()
|
|
|
|
.map(|field| {
|
|
|
|
let name = field.param_ident();
|
2018-03-10 20:47:02 +11:00
|
|
|
let ty = field.type_tokens();
|
2018-03-10 20:07:12 +11:00
|
|
|
(name, ty)
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-03-10 20:07:12 +11:00
|
|
|
params
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-03-10 20:07:12 +11:00
|
|
|
|
|
|
|
let params_names: Vec<Vec<_>> = params
|
|
|
|
.iter()
|
|
|
|
.map(|inner_params| {
|
|
|
|
inner_params
|
|
|
|
.iter()
|
|
|
|
.map(|&(param_name, _)| param_name)
|
|
|
|
.collect()
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-03-10 20:07:12 +11:00
|
|
|
let param_names_ref = ¶ms_names;
|
|
|
|
let expanded_params: Vec<_> = params
|
|
|
|
.iter()
|
|
|
|
.map(|inner_params| {
|
2018-03-10 20:47:02 +11:00
|
|
|
let inner_params_iter = inner_params.iter().map(|&(ref param_name, ref param_ty)| {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {#param_name: #param_ty}
|
2018-03-10 20:07:12 +11:00
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-03-10 20:07:12 +11:00
|
|
|
#(#inner_params_iter,)*
|
|
|
|
}
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-11-04 09:37:20 +11:00
|
|
|
let expanded_params_unused: Vec<_> = params
|
|
|
|
.iter()
|
|
|
|
.map(|inner_params| {
|
|
|
|
let inner_params_iter = inner_params.iter().map(|&(ref param_name, ref param_ty)| {
|
|
|
|
let unused_name = Ident::from(format!("_{}", param_name).as_str());
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {#unused_name: #param_ty}
|
2018-11-04 09:37:20 +11:00
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-04 09:37:20 +11:00
|
|
|
#(#inner_params_iter,)*
|
|
|
|
}
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-03-10 20:07:12 +11:00
|
|
|
let expanded_params_ref = &expanded_params;
|
|
|
|
|
|
|
|
let return_types: Vec<_> = commands
|
|
|
|
.iter()
|
2018-03-10 20:47:02 +11:00
|
|
|
.map(|cmd| cmd.return_type.type_tokens())
|
2018-03-10 20:07:12 +11:00
|
|
|
.collect();
|
|
|
|
let return_types_ref = &return_types;
|
2018-11-17 03:57:08 +11:00
|
|
|
|
|
|
|
let pfn_names: Vec<_> = pfn_commands
|
|
|
|
.iter()
|
|
|
|
.map(|cmd| Ident::from(format!("PFN_{}", cmd.name.as_str())))
|
|
|
|
.collect();
|
|
|
|
let pfn_names_ref = &pfn_names;
|
|
|
|
|
|
|
|
let signature_params: Vec<Vec<_>> = pfn_commands
|
|
|
|
.iter()
|
|
|
|
.map(|cmd| {
|
2018-11-18 05:05:28 +11:00
|
|
|
let params: Vec<_> = cmd
|
|
|
|
.param
|
2018-11-17 03:57:08 +11:00
|
|
|
.iter()
|
|
|
|
.map(|field| {
|
|
|
|
let name = field.param_ident();
|
|
|
|
let ty = field.type_tokens();
|
|
|
|
quote! { #name: #ty }
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-11-17 03:57:08 +11:00
|
|
|
params
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-11-17 03:57:08 +11:00
|
|
|
let signature_params_ref = &signature_params;
|
|
|
|
|
|
|
|
let pfn_return_types: Vec<_> = pfn_commands
|
|
|
|
.iter()
|
|
|
|
.map(|cmd| cmd.return_type.type_tokens())
|
|
|
|
.collect();
|
|
|
|
let pfn_return_types_ref = &pfn_return_types;
|
|
|
|
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-17 03:57:08 +11:00
|
|
|
#(
|
|
|
|
#[allow(non_camel_case_types)]
|
|
|
|
pub type #pfn_names_ref = extern "system" fn(#(#signature_params_ref),*) -> #pfn_return_types_ref;
|
|
|
|
)*
|
|
|
|
|
2018-03-10 20:07:12 +11:00
|
|
|
pub struct #ident {
|
|
|
|
#(
|
2018-11-22 05:08:03 +11:00
|
|
|
pub #names_ref: extern "system" fn(#expanded_params_ref) -> #return_types_ref,
|
2018-03-10 20:07:12 +11:00
|
|
|
)*
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Send for #ident {}
|
|
|
|
unsafe impl Sync for #ident {}
|
|
|
|
|
|
|
|
impl ::std::clone::Clone for #ident {
|
2018-06-24 20:09:37 +10:00
|
|
|
fn clone(&self) -> Self {
|
2018-03-10 20:07:12 +11:00
|
|
|
#ident{
|
|
|
|
#(#names_left: self.#names_right,)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
impl #ident {
|
2018-11-04 09:37:20 +11:00
|
|
|
pub fn load<F>(mut _f: F) -> Self
|
2018-03-10 20:07:12 +11:00
|
|
|
where F: FnMut(&::std::ffi::CStr) -> *const c_void
|
|
|
|
{
|
2018-11-04 09:37:20 +11:00
|
|
|
#ident {
|
2018-03-10 20:07:12 +11:00
|
|
|
#(
|
|
|
|
#names_ref: unsafe {
|
2018-11-04 09:37:20 +11:00
|
|
|
|
|
|
|
extern "system" fn #names_ref1 (#expanded_params_unused) -> #return_types_ref {
|
2018-11-04 19:26:10 +11:00
|
|
|
panic!(concat!("Unable to load ", stringify!(#names_ref2)))
|
2018-11-04 09:37:20 +11:00
|
|
|
}
|
2018-03-10 20:07:12 +11:00
|
|
|
let raw_name = stringify!(#raw_names_ref);
|
2018-07-07 20:54:31 +10:00
|
|
|
let cname = ::std::ffi::CString::new(raw_name).unwrap();
|
|
|
|
let val = _f(&cname);
|
2018-03-10 20:07:12 +11:00
|
|
|
if val.is_null(){
|
2018-11-04 09:37:20 +11:00
|
|
|
#names_ref3
|
|
|
|
}
|
|
|
|
else{
|
|
|
|
::std::mem::transmute(val)
|
2018-03-10 20:07:12 +11:00
|
|
|
}
|
|
|
|
},
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#(
|
2018-07-07 20:54:31 +10:00
|
|
|
pub unsafe fn #names_ref(&self, #expanded_params_ref) -> #return_types_ref {
|
2018-03-10 20:07:12 +11:00
|
|
|
(self.#names_left)(#(#param_names_ref,)*)
|
|
|
|
}
|
|
|
|
)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
pub struct ExtensionConstant<'a> {
|
|
|
|
pub name: &'a str,
|
|
|
|
pub constant: Constant,
|
|
|
|
}
|
|
|
|
impl<'a> ConstantExt for ExtensionConstant<'a> {
|
|
|
|
fn variant_ident(&self, enum_name: &str) -> Ident {
|
|
|
|
variant_ident(enum_name, self.name)
|
|
|
|
}
|
|
|
|
fn to_tokens(&self) -> Tokens {
|
|
|
|
self.constant.to_tokens()
|
|
|
|
}
|
|
|
|
fn notation(&self) -> Option<&str> {
|
|
|
|
None
|
2018-07-21 20:56:16 +10:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
|
|
|
|
pub fn generate_extension_constants<'a>(
|
2018-07-31 03:53:12 +10:00
|
|
|
extension_name: &str,
|
|
|
|
extension_number: i64,
|
2018-11-18 03:27:18 +11:00
|
|
|
extension_items: &'a [vk_parse::ExtensionChild],
|
2018-07-30 06:39:45 +10:00
|
|
|
const_cache: &mut HashSet<&'a str>,
|
2018-08-20 12:40:28 +10:00
|
|
|
const_values: &mut HashMap<Ident, Vec<Ident>>,
|
2018-07-30 06:39:45 +10:00
|
|
|
) -> quote::Tokens {
|
2018-07-31 03:53:12 +10:00
|
|
|
let items = extension_items
|
2018-07-21 20:56:16 +10:00
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
2018-11-18 03:27:18 +11:00
|
|
|
vk_parse::ExtensionChild::Require { items, .. } => Some(items.iter()),
|
2018-07-21 20:56:16 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|iter| iter);
|
2018-07-30 06:39:45 +10:00
|
|
|
let enum_tokens = items.filter_map(|item| match item {
|
|
|
|
vk_parse::InterfaceItem::Enum(_enum) => {
|
|
|
|
use vk_parse::EnumSpec;
|
|
|
|
if const_cache.contains(_enum.name.as_str()) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let (constant, extends) = match &_enum.spec {
|
|
|
|
EnumSpec::Bitpos { bitpos, extends } => {
|
|
|
|
Some((Constant::BitPos(*bitpos as u32), extends.clone()))
|
|
|
|
}
|
|
|
|
EnumSpec::Offset {
|
|
|
|
offset,
|
|
|
|
extends,
|
|
|
|
extnumber,
|
2018-08-20 17:55:41 +10:00
|
|
|
dir: positive,
|
2018-07-30 06:39:45 +10:00
|
|
|
} => {
|
2018-07-30 16:54:03 +10:00
|
|
|
let ext_base = 1_000_000_000;
|
2018-07-30 06:39:45 +10:00
|
|
|
let ext_block_size = 1000;
|
2018-07-31 03:53:12 +10:00
|
|
|
let extnumber = extnumber.unwrap_or_else(|| extension_number);
|
2018-07-30 06:39:45 +10:00
|
|
|
let value = ext_base + (extnumber - 1) * ext_block_size + offset;
|
2018-08-20 17:55:41 +10:00
|
|
|
let value = if *positive { value } else { -value };
|
2018-07-30 06:39:45 +10:00
|
|
|
Some((Constant::Number(value as i32), Some(extends.clone())))
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
}?;
|
|
|
|
let extends = extends?;
|
|
|
|
let ext_constant = ExtensionConstant {
|
|
|
|
name: &_enum.name,
|
|
|
|
constant,
|
|
|
|
};
|
|
|
|
let ident = name_to_tokens(&extends);
|
2018-11-17 03:57:08 +11:00
|
|
|
const_values
|
|
|
|
.entry(ident.clone())
|
|
|
|
.or_insert_with(Vec::new)
|
2018-08-20 12:40:28 +10:00
|
|
|
.push(ext_constant.variant_ident(&extends));
|
2018-07-30 16:54:03 +10:00
|
|
|
let impl_block = bitflags_impl_block(ident, &extends, &[&ext_constant]);
|
2018-07-31 03:53:12 +10:00
|
|
|
let doc_string = format!("Generated from '{}'", extension_name);
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-07-30 06:39:45 +10:00
|
|
|
#[doc = #doc_string]
|
|
|
|
#impl_block
|
|
|
|
};
|
2018-07-21 20:56:16 +10:00
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
const_cache.insert(_enum.name.as_str());
|
|
|
|
Some(q)
|
|
|
|
}
|
|
|
|
_ => None,
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-30 06:39:45 +10:00
|
|
|
#(#enum_tokens)*
|
2018-07-11 21:18:22 +10:00
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
}
|
2018-11-17 03:57:08 +11:00
|
|
|
pub fn generate_extension_commands<'a>(
|
2018-07-31 03:53:12 +10:00
|
|
|
extension_name: &str,
|
2018-11-18 03:27:18 +11:00
|
|
|
items: &[vk_parse::ExtensionChild],
|
2018-11-17 03:57:08 +11:00
|
|
|
cmd_map: &CommandMap<'a>,
|
|
|
|
fn_cache: &mut HashSet<&'a str>,
|
2018-07-30 06:39:45 +10:00
|
|
|
) -> Tokens {
|
2018-07-31 03:53:12 +10:00
|
|
|
let commands = items
|
2018-03-11 02:21:35 +11:00
|
|
|
.iter()
|
2018-07-30 06:39:45 +10:00
|
|
|
.filter_map(|ext_item| match ext_item {
|
2018-11-18 03:27:18 +11:00
|
|
|
vk_parse::ExtensionChild::Require { items, .. } => {
|
2018-07-30 06:39:45 +10:00
|
|
|
Some(items.iter().filter_map(|item| match item {
|
2018-11-18 06:00:57 +11:00
|
|
|
vk_parse::InterfaceItem::Command { ref name, .. } => {
|
|
|
|
cmd_map.get(name).map(|c| *c)
|
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
_ => None,
|
|
|
|
}))
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
2018-07-30 06:39:45 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|iter| iter)
|
2018-07-11 21:18:22 +10:00
|
|
|
.collect_vec();
|
2018-07-31 03:53:12 +10:00
|
|
|
let name = format!("{}Fn", extension_name.to_camel_case());
|
2018-03-11 02:21:35 +11:00
|
|
|
let ident = Ident::from(&name[2..]);
|
2018-11-17 03:57:08 +11:00
|
|
|
generate_function_pointers(ident, &commands, fn_cache)
|
2018-07-30 06:39:45 +10:00
|
|
|
}
|
|
|
|
pub fn generate_extension<'a>(
|
|
|
|
extension: &'a vk_parse::Extension,
|
2018-11-17 03:57:08 +11:00
|
|
|
cmd_map: &CommandMap<'a>,
|
2018-07-30 06:39:45 +10:00
|
|
|
const_cache: &mut HashSet<&'a str>,
|
2018-11-17 03:57:08 +11:00
|
|
|
const_values: &mut HashMap<Ident, Vec<Ident>>,
|
|
|
|
fn_cache: &mut HashSet<&'a str>,
|
2018-07-30 06:39:45 +10:00
|
|
|
) -> Option<quote::Tokens> {
|
2018-07-31 04:23:25 +10:00
|
|
|
// Okay this is a little bit odd. We need to generate all extensions, even disabled ones,
|
|
|
|
// because otherwise some StructureTypes won't get generated. But we don't generate extensions
|
|
|
|
// that are reserved
|
|
|
|
if extension.name.contains("RESERVED") {
|
|
|
|
return None;
|
|
|
|
}
|
2018-07-31 03:53:12 +10:00
|
|
|
let extension_tokens = generate_extension_constants(
|
|
|
|
&extension.name,
|
|
|
|
extension.number.unwrap_or(0),
|
2018-11-18 03:27:18 +11:00
|
|
|
&extension.children,
|
2018-07-31 03:53:12 +10:00
|
|
|
const_cache,
|
2018-08-20 12:40:28 +10:00
|
|
|
const_values,
|
2018-07-31 03:53:12 +10:00
|
|
|
);
|
2018-11-18 03:27:18 +11:00
|
|
|
let fp = generate_extension_commands(&extension.name, &extension.children, cmd_map, fn_cache);
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-07-21 20:56:16 +10:00
|
|
|
#fp
|
2018-07-30 06:39:45 +10:00
|
|
|
#extension_tokens
|
|
|
|
};
|
|
|
|
Some(q)
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
pub fn generate_typedef(typedef: &vkxml::Typedef) -> Tokens {
|
|
|
|
let typedef_name = to_type_tokens(&typedef.name, None);
|
|
|
|
let typedef_ty = to_type_tokens(&typedef.basetype, None);
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-04-01 18:52:21 +10:00
|
|
|
pub type #typedef_name = #typedef_ty;
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
}
|
2018-11-18 19:29:17 +11:00
|
|
|
pub fn generate_bitmask(
|
|
|
|
bitmask: &vkxml::Bitmask,
|
|
|
|
bitflags_cache: &mut HashSet<Ident>,
|
|
|
|
) -> Option<Tokens> {
|
2018-06-04 19:26:14 +10:00
|
|
|
// Workaround for empty bitmask
|
2018-07-30 16:54:03 +10:00
|
|
|
if bitmask.name.is_empty() {
|
2018-06-04 19:26:14 +10:00
|
|
|
return None;
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
// If this enum has constants, then it will generated later in generate_enums.
|
|
|
|
if bitmask.enumref.is_some() {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-04-01 18:52:21 +10:00
|
|
|
let name = &bitmask.name[2..];
|
|
|
|
let ident = Ident::from(name);
|
2018-11-18 19:29:17 +11:00
|
|
|
if bitflags_cache.contains(&ident) {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
bitflags_cache.insert(ident.clone());
|
2018-12-07 20:33:36 +11:00
|
|
|
Some(quote! {
|
2018-08-03 20:34:14 +10:00
|
|
|
#[repr(transparent)]
|
2018-07-09 17:23:53 +10:00
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
2018-07-11 21:18:22 +10:00
|
|
|
pub struct #ident(Flags);
|
2018-06-24 20:09:37 +10:00
|
|
|
vk_bitflags_wrapped!(#ident, 0b0, Flags);
|
2018-06-04 19:26:14 +10:00
|
|
|
})
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
2018-07-09 16:49:28 +10:00
|
|
|
|
2018-04-01 18:52:21 +10:00
|
|
|
pub enum EnumType {
|
|
|
|
Bitflags(Tokens),
|
|
|
|
Enum(Tokens),
|
|
|
|
}
|
|
|
|
|
2018-07-11 21:18:22 +10:00
|
|
|
pub fn variant_ident(enum_name: &str, variant_name: &str) -> Ident {
|
2018-08-01 16:51:50 +10:00
|
|
|
let _name = enum_name.replace("FlagBits", "");
|
2018-11-21 03:55:06 +11:00
|
|
|
// TODO: Should be read from vk.xml id:2
|
|
|
|
// TODO: Also needs to be more robust, vendor names can be substrings from itself, id:4
|
2018-07-31 21:44:22 +10:00
|
|
|
// like NVX and NV
|
|
|
|
let vendors = ["_NVX", "_KHR", "_EXT", "_NV", "_AMD", "_ANDROID", "_GOOGLE"];
|
|
|
|
let mut struct_name = _name.to_shouty_snake_case();
|
2018-08-01 16:51:50 +10:00
|
|
|
let vendor = vendors
|
|
|
|
.into_iter()
|
|
|
|
.find(|&vendor| struct_name.contains(vendor))
|
|
|
|
.cloned()
|
|
|
|
.unwrap_or("");
|
|
|
|
struct_name = struct_name.replace(vendor, "");
|
2018-07-09 17:23:53 +10:00
|
|
|
let new_variant_name = variant_name.replace(&struct_name, "").replace("VK", "");
|
2018-07-11 21:18:22 +10:00
|
|
|
let new_variant_name = new_variant_name
|
|
|
|
.trim_matches('_')
|
|
|
|
.to_shouty_snake_case()
|
2018-08-01 16:51:50 +10:00
|
|
|
.replace("_BIT", "")
|
|
|
|
.replace(vendor, "");
|
2018-07-09 17:23:53 +10:00
|
|
|
let is_digit = new_variant_name
|
|
|
|
.chars()
|
|
|
|
.nth(0)
|
|
|
|
.map(|c| c.is_digit(10))
|
|
|
|
.unwrap_or(false);
|
|
|
|
if is_digit {
|
|
|
|
Ident::from(format!("TYPE_{}", new_variant_name).as_str())
|
|
|
|
} else {
|
|
|
|
Ident::from(new_variant_name)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-11 21:18:22 +10:00
|
|
|
pub fn bitflags_impl_block(
|
2018-07-30 16:54:03 +10:00
|
|
|
ident: Ident,
|
2018-07-30 06:39:45 +10:00
|
|
|
enum_name: &str,
|
2018-07-21 20:56:16 +10:00
|
|
|
constants: &[&impl ConstantExt],
|
2018-07-11 21:18:22 +10:00
|
|
|
) -> Tokens {
|
|
|
|
let variants = constants
|
|
|
|
.iter()
|
|
|
|
.map(|constant| {
|
2018-07-30 06:39:45 +10:00
|
|
|
let variant_ident = constant.variant_ident(enum_name);
|
2018-07-11 21:18:22 +10:00
|
|
|
let tokens = constant.to_tokens();
|
|
|
|
(variant_ident, tokens)
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect_vec();
|
2018-07-11 21:18:22 +10:00
|
|
|
|
|
|
|
let notations = constants.iter().map(|constant| {
|
|
|
|
constant.notation().map(|n| {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-11 21:18:22 +10:00
|
|
|
#[doc = #n]
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let variants =
|
|
|
|
variants
|
|
|
|
.iter()
|
|
|
|
.zip(notations.clone())
|
|
|
|
.map(|((variant_ident, value), ref notation)| {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-18 05:05:28 +11:00
|
|
|
#notation
|
|
|
|
pub const #variant_ident: Self = #ident(#value);
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-11 21:18:22 +10:00
|
|
|
impl #ident {
|
|
|
|
#(#variants)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
pub fn generate_enum<'a>(
|
|
|
|
_enum: &'a vkxml::Enumeration,
|
|
|
|
const_cache: &mut HashSet<&'a str>,
|
2018-11-17 03:57:08 +11:00
|
|
|
const_values: &mut HashMap<Ident, Vec<Ident>>,
|
2018-11-18 19:29:17 +11:00
|
|
|
bitflags_cache: &mut HashSet<Ident>,
|
2018-07-07 22:49:17 +10:00
|
|
|
) -> EnumType {
|
2018-04-01 18:52:21 +10:00
|
|
|
let name = &_enum.name[2..];
|
2018-06-06 00:47:21 +10:00
|
|
|
let _name = name.replace("FlagBits", "Flags");
|
2018-07-09 16:49:28 +10:00
|
|
|
let ident = Ident::from(_name.as_str());
|
2018-07-11 21:18:22 +10:00
|
|
|
let constants: Vec<_> = _enum
|
2018-07-09 17:23:53 +10:00
|
|
|
.elements
|
|
|
|
.iter()
|
2018-07-11 21:18:22 +10:00
|
|
|
.filter_map(|elem| match *elem {
|
|
|
|
vkxml::EnumerationElement::Enum(ref constant) => Some(constant),
|
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect_vec();
|
2018-08-20 12:40:28 +10:00
|
|
|
let values = const_values.entry(ident.clone()).or_insert_with(Vec::new);
|
2018-07-30 06:39:45 +10:00
|
|
|
for constant in &constants {
|
|
|
|
const_cache.insert(constant.name.as_str());
|
2018-08-20 12:40:28 +10:00
|
|
|
values.push(constant.variant_ident(&_enum.name));
|
2018-07-30 06:39:45 +10:00
|
|
|
}
|
2018-07-11 21:18:22 +10:00
|
|
|
|
2018-04-01 18:52:21 +10:00
|
|
|
if name.contains("Bit") {
|
2018-06-06 00:47:21 +10:00
|
|
|
let ident = Ident::from(_name.as_str());
|
2018-07-11 21:18:22 +10:00
|
|
|
let all_bits = constants
|
2018-06-06 00:47:21 +10:00
|
|
|
.iter()
|
2018-07-11 21:18:22 +10:00
|
|
|
.filter_map(|constant| Constant::from_constant(constant).value())
|
2018-06-24 20:09:37 +10:00
|
|
|
.fold(0, |acc, next| acc | next.bits());
|
2018-06-06 00:47:21 +10:00
|
|
|
let all_bits_term = Term::intern(&format!("0b{:b}", all_bits));
|
|
|
|
|
2018-07-30 16:54:03 +10:00
|
|
|
let impl_bitflags = bitflags_impl_block(ident, &_enum.name, &constants);
|
2018-11-18 19:29:17 +11:00
|
|
|
if bitflags_cache.contains(&ident) {
|
2018-12-07 20:33:36 +11:00
|
|
|
EnumType::Bitflags(quote! {})
|
2018-11-18 19:29:17 +11:00
|
|
|
} else {
|
|
|
|
bitflags_cache.insert(ident.clone());
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-11-18 19:29:17 +11:00
|
|
|
#[repr(transparent)]
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
|
|
|
pub struct #ident(pub(crate) Flags);
|
|
|
|
vk_bitflags_wrapped!(#ident, #all_bits_term, Flags);
|
|
|
|
#impl_bitflags
|
|
|
|
};
|
|
|
|
EnumType::Bitflags(q)
|
|
|
|
}
|
2018-04-01 18:52:21 +10:00
|
|
|
} else {
|
2018-07-30 16:54:03 +10:00
|
|
|
let impl_block = bitflags_impl_block(ident, &_enum.name, &constants);
|
2018-12-07 20:33:36 +11:00
|
|
|
let enum_quote = quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Default)]
|
2018-08-03 20:34:14 +10:00
|
|
|
#[repr(transparent)]
|
2018-07-30 06:39:45 +10:00
|
|
|
pub struct #ident(pub(crate) i32);
|
2018-08-21 16:56:33 +10:00
|
|
|
impl #ident {
|
|
|
|
pub fn from_raw(x: i32) -> Self { #ident(x) }
|
|
|
|
pub fn as_raw(self) -> i32 { self.0 }
|
|
|
|
}
|
2018-07-11 21:18:22 +10:00
|
|
|
#impl_block
|
2018-07-09 17:23:53 +10:00
|
|
|
};
|
|
|
|
let special_quote = match _name.as_str() {
|
2018-07-09 16:49:28 +10:00
|
|
|
//"StructureType" => generate_structure_type(&_name, _enum, create_info_constants),
|
2018-07-30 16:54:03 +10:00
|
|
|
"Result" => generate_result(ident, _enum),
|
2018-07-07 18:43:05 +10:00
|
|
|
_ => {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {}
|
2018-04-01 18:52:21 +10:00
|
|
|
}
|
|
|
|
};
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-07-09 17:23:53 +10:00
|
|
|
#enum_quote
|
|
|
|
#special_quote
|
|
|
|
|
|
|
|
};
|
2018-04-01 18:52:21 +10:00
|
|
|
EnumType::Enum(q)
|
|
|
|
}
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
2018-07-09 16:49:28 +10:00
|
|
|
|
2018-07-30 16:54:03 +10:00
|
|
|
pub fn generate_result(ident: Ident, _enum: &vkxml::Enumeration) -> Tokens {
|
2018-07-07 18:43:05 +10:00
|
|
|
let notation = _enum.elements.iter().filter_map(|elem| {
|
|
|
|
let (variant_name, notation) = match *elem {
|
|
|
|
vkxml::EnumerationElement::Enum(ref constant) => (
|
|
|
|
constant.name.as_str(),
|
|
|
|
constant.notation.as_ref().map(|s| s.as_str()).unwrap_or(""),
|
|
|
|
),
|
|
|
|
_ => {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2018-07-11 21:18:22 +10:00
|
|
|
let variant_ident = variant_ident(&_enum.name, variant_name);
|
2018-12-07 20:33:36 +11:00
|
|
|
Some(quote! {
|
2018-08-20 12:40:28 +10:00
|
|
|
#ident::#variant_ident => Some(#notation)
|
2018-07-07 18:43:05 +10:00
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2018-08-20 12:40:28 +10:00
|
|
|
let notation2 = notation.clone();
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-07 18:43:05 +10:00
|
|
|
impl ::std::error::Error for #ident {
|
|
|
|
fn description(&self) -> &str {
|
2018-08-20 12:40:28 +10:00
|
|
|
let name = match *self {
|
|
|
|
#(#notation),*,
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
name.unwrap_or("unknown error")
|
2018-07-07 18:43:05 +10:00
|
|
|
}
|
|
|
|
}
|
2018-08-20 12:40:28 +10:00
|
|
|
impl fmt::Display for #ident {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let name = match *self {
|
|
|
|
#(#notation2),*,
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(x) = name {
|
|
|
|
fmt.write_str(x)
|
|
|
|
} else {
|
|
|
|
write!(fmt, "{}", self.0)
|
2018-07-07 18:43:05 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
|
|
|
|
fn is_static_array(field: &vkxml::Field) -> bool {
|
|
|
|
field
|
|
|
|
.array
|
|
|
|
.as_ref()
|
|
|
|
.map(|ty| match ty {
|
|
|
|
vkxml::ArrayType::Static => true,
|
|
|
|
_ => false,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.unwrap_or(false)
|
2018-07-30 20:50:51 +10:00
|
|
|
}
|
2018-08-19 18:10:11 +10:00
|
|
|
pub fn derive_default(_struct: &vkxml::Struct) -> Option<Tokens> {
|
2018-07-31 03:53:12 +10:00
|
|
|
let name = name_to_tokens(&_struct.name);
|
|
|
|
let members = _struct.elements.iter().filter_map(|elem| match *elem {
|
|
|
|
vkxml::StructElement::Member(ref field) => Some(field),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
let is_structure_type = |field: &vkxml::Field| field.basetype == "VkStructureType";
|
|
|
|
|
2018-07-31 04:23:25 +10:00
|
|
|
// This are also pointers, and therefor also don't implement Default. The spec
|
|
|
|
// also doesn't mark them as pointers
|
|
|
|
let handles = ["LPCWSTR", "HANDLE", "HINSTANCE", "HWND"];
|
2018-07-31 03:53:12 +10:00
|
|
|
let contains_ptr = members.clone().any(|field| field.reference.is_some());
|
|
|
|
let contains_strucutre_type = members.clone().any(is_structure_type);
|
|
|
|
let contains_static_array = members.clone().any(is_static_array);
|
2018-10-08 04:09:29 +11:00
|
|
|
if !(contains_ptr || contains_strucutre_type || contains_static_array) {
|
2018-07-31 03:53:12 +10:00
|
|
|
return None;
|
|
|
|
};
|
|
|
|
let default_fields = members.clone().map(|field| {
|
|
|
|
let param_ident = field.param_ident();
|
|
|
|
if is_structure_type(field) {
|
|
|
|
let ty = field
|
|
|
|
.type_enums
|
|
|
|
.as_ref()
|
|
|
|
.and_then(|ty| ty.split(',').nth(0));
|
|
|
|
if let Some(variant) = ty {
|
|
|
|
let variant_ident = variant_ident("VkStructureType", variant);
|
|
|
|
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#param_ident: StructureType::#variant_ident
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#param_ident: unsafe { ::std::mem::zeroed() }
|
|
|
|
}
|
|
|
|
}
|
2018-08-23 06:53:17 +10:00
|
|
|
} else if let Some(ref reference) = field.reference {
|
|
|
|
match reference {
|
|
|
|
vkxml::ReferenceType::Pointer => {
|
|
|
|
if field.is_const {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null()
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vkxml::ReferenceType::PointerToPointer => {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
vkxml::ReferenceType::PointerToConstPointer => {
|
|
|
|
if field.is_const {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null()
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-23 06:53:17 +10:00
|
|
|
#param_ident: ::std::ptr::null_mut()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-11-17 03:57:08 +11:00
|
|
|
} else if is_static_array(field) || handles.contains(&field.basetype.as_str()) {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#param_ident: unsafe { ::std::mem::zeroed() }
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let ty = field.type_tokens();
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
#param_ident: #ty::default()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-07-31 03:53:12 +10:00
|
|
|
impl ::std::default::Default for #name {
|
|
|
|
fn default() -> #name {
|
|
|
|
#name {
|
|
|
|
#(
|
|
|
|
#default_fields
|
|
|
|
),*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(q)
|
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
pub fn derive_debug(_struct: &vkxml::Struct, union_types: &HashSet<&str>) -> Option<Tokens> {
|
|
|
|
let name = name_to_tokens(&_struct.name);
|
2018-07-07 20:54:31 +10:00
|
|
|
let members = _struct.elements.iter().filter_map(|elem| match *elem {
|
|
|
|
vkxml::StructElement::Member(ref field) => Some(field),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
let contains_pfn = members.clone().any(|field| {
|
|
|
|
field
|
|
|
|
.name
|
|
|
|
.as_ref()
|
|
|
|
.map(|n| n.contains("pfn"))
|
|
|
|
.unwrap_or(false)
|
|
|
|
});
|
2018-11-17 03:57:08 +11:00
|
|
|
let contains_static_array = members
|
|
|
|
.clone()
|
|
|
|
.any(|x| is_static_array(x) && x.basetype == "char");
|
2018-07-30 20:50:51 +10:00
|
|
|
let contains_union = members
|
|
|
|
.clone()
|
|
|
|
.any(|field| union_types.contains(field.basetype.as_str()));
|
|
|
|
if !(contains_union || contains_static_array || contains_pfn) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let debug_fields = members.clone().map(|field| {
|
|
|
|
let param_ident = field.param_ident();
|
|
|
|
let param_str = param_ident.as_ref();
|
2018-10-08 04:17:48 +11:00
|
|
|
let debug_value = if is_static_array(field) && field.basetype == "char" {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-30 20:50:51 +10:00
|
|
|
&unsafe {
|
|
|
|
::std::ffi::CStr::from_ptr(self.#param_ident.as_ptr() as *const i8)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if param_ident.as_ref().contains("pfn") {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-10-08 04:09:29 +11:00
|
|
|
&(self.#param_ident.map(|x| x as *const ()))
|
2018-07-30 20:50:51 +10:00
|
|
|
}
|
|
|
|
} else if union_types.contains(field.basetype.as_str()) {
|
|
|
|
quote!(&"union")
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-30 20:50:51 +10:00
|
|
|
&self.#param_ident
|
|
|
|
}
|
|
|
|
};
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-30 20:50:51 +10:00
|
|
|
.field(#param_str, #debug_value)
|
2018-07-07 20:54:31 +10:00
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
});
|
|
|
|
let name_str = name.as_ref();
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-09-17 05:15:01 +10:00
|
|
|
impl fmt::Debug for #name {
|
|
|
|
fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
|
2018-07-30 20:50:51 +10:00
|
|
|
fmt.debug_struct(#name_str)
|
|
|
|
#(#debug_fields)*
|
|
|
|
.finish()
|
|
|
|
}
|
2018-07-07 20:54:31 +10:00
|
|
|
}
|
|
|
|
};
|
2018-07-30 20:50:51 +10:00
|
|
|
Some(q)
|
|
|
|
}
|
2018-08-19 18:10:11 +10:00
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
pub fn derive_setters(_struct: &vkxml::Struct) -> Option<Tokens> {
|
2018-11-19 00:50:37 +11:00
|
|
|
if &_struct.name == "VkBaseInStructure" || &_struct.name == "VkBaseOutStructure" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
let name = name_to_tokens(&_struct.name);
|
2018-11-13 09:22:16 +11:00
|
|
|
let name_builder = name_to_tokens(&(_struct.name.clone() + "Builder"));
|
2018-10-20 07:16:35 +11:00
|
|
|
|
|
|
|
let members = _struct.elements.iter().filter_map(|elem| match *elem {
|
|
|
|
vkxml::StructElement::Member(ref field) => Some(field),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
2018-11-19 00:50:37 +11:00
|
|
|
let (has_next, is_next_const) = match members
|
|
|
|
.clone()
|
|
|
|
.find(|field| field.param_ident().to_string() == "p_next")
|
|
|
|
{
|
2018-12-07 20:33:36 +11:00
|
|
|
Some(p_next) => {
|
|
|
|
if p_next.type_tokens().to_string().starts_with("*const") {
|
|
|
|
(true, true)
|
|
|
|
} else {
|
|
|
|
(true, false)
|
|
|
|
}
|
|
|
|
}
|
2018-11-19 00:50:37 +11:00
|
|
|
None => (false, false),
|
|
|
|
};
|
|
|
|
|
2018-11-13 09:22:16 +11:00
|
|
|
let nofilter_count_members = [
|
|
|
|
"VkPipelineViewportStateCreateInfo.pViewports",
|
|
|
|
"VkPipelineViewportStateCreateInfo.pScissors",
|
|
|
|
"VkDescriptorSetLayoutBinding.pImmutableSamplers",
|
|
|
|
];
|
2018-11-17 03:57:08 +11:00
|
|
|
let filter_members: Vec<String> = members
|
|
|
|
.clone()
|
|
|
|
.filter_map(|field| {
|
|
|
|
let field_name = field.name.as_ref().unwrap();
|
|
|
|
|
|
|
|
// Associated _count members
|
|
|
|
if field.array.is_some() {
|
|
|
|
if let Some(ref array_size) = field.size {
|
2018-12-07 20:33:36 +11:00
|
|
|
if !array_size.starts_with("latexmath")
|
|
|
|
&& !nofilter_count_members
|
|
|
|
.iter()
|
|
|
|
.any(|n| *n == &(_struct.name.clone() + "." + field_name))
|
2018-11-17 03:57:08 +11:00
|
|
|
{
|
|
|
|
return Some((*array_size).clone());
|
|
|
|
}
|
2018-10-24 08:37:28 +11:00
|
|
|
}
|
|
|
|
}
|
2018-10-25 01:00:16 +11:00
|
|
|
|
2018-11-17 03:57:08 +11:00
|
|
|
// VkShaderModuleCreateInfo requiers a custom setter
|
|
|
|
if field_name == "codeSize" {
|
|
|
|
return Some(field_name.clone());
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
|
2018-11-17 03:57:08 +11:00
|
|
|
None
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-10-24 08:37:28 +11:00
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
let setters = members.clone().filter_map(|field| {
|
|
|
|
let param_ident = field.param_ident();
|
|
|
|
let param_ty_tokens = field.type_tokens();
|
2018-11-13 09:22:16 +11:00
|
|
|
let param_ty_string = param_ty_tokens.to_string();
|
2018-10-20 07:16:35 +11:00
|
|
|
|
|
|
|
let param_ident_string = param_ident.to_string();
|
|
|
|
if param_ident_string == "s_type" || param_ident_string == "p_next" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
let mut param_ident_short = param_ident_string.as_str();
|
|
|
|
if param_ident_string.starts_with("p_") {
|
|
|
|
param_ident_short = ¶m_ident_string[2..];
|
|
|
|
};
|
|
|
|
if param_ident_string.starts_with("pp_") {
|
|
|
|
param_ident_short = ¶m_ident_string[3..];
|
|
|
|
};
|
|
|
|
let param_ident_short = Term::intern(¶m_ident_short);
|
|
|
|
|
2018-11-13 09:22:16 +11:00
|
|
|
if let Some(name) = field.name.as_ref() {
|
|
|
|
// Fiter
|
2018-10-25 01:00:16 +11:00
|
|
|
if filter_members.iter().any(|n| *n == *name) {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unique cases
|
|
|
|
if name == "pCode" {
|
|
|
|
return Some(quote!{
|
2018-10-29 23:32:17 +11:00
|
|
|
pub fn code(mut self, code: &'a [u32]) -> #name_builder<'a> {
|
|
|
|
self.inner.code_size = code.len() * 4;
|
2018-10-25 01:00:16 +11:00
|
|
|
self.inner.p_code = code.as_ptr() as *const u32;
|
|
|
|
self
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if name == "pSampleMask" {
|
|
|
|
return Some(quote!{
|
|
|
|
pub fn sample_mask(mut self, sample_mask: &'a [SampleMask]) -> #name_builder<'a> {
|
|
|
|
self.inner.p_sample_mask = sample_mask.as_ptr() as *const SampleMask;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
});
|
2018-10-25 01:00:16 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-21 03:55:06 +11:00
|
|
|
// TODO: Improve in future when https://github.com/rust-lang/rust/issues/53667 is merged id:6
|
2018-11-17 03:25:20 +11:00
|
|
|
if param_ident_string.starts_with("p_") || param_ident_string.starts_with("pp_") {
|
2018-10-20 07:16:35 +11:00
|
|
|
if param_ty_string == "*const c_char" {
|
|
|
|
return Some(quote!{
|
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: &'a ::std::ffi::CStr) -> #name_builder<'a> {
|
|
|
|
self.inner.#param_ident = #param_ident_short.as_ptr();
|
|
|
|
self
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
});
|
2018-10-20 07:16:35 +11:00
|
|
|
}
|
|
|
|
|
|
|
|
if let Some(ref array_type) = field.array {
|
|
|
|
if let Some(ref array_size) = field.size {
|
|
|
|
if !array_size.starts_with("latexmath") {
|
2018-11-13 09:22:16 +11:00
|
|
|
let array_size_ident = Ident::from(array_size.to_snake_case().as_str());
|
2018-10-20 07:16:35 +11:00
|
|
|
if param_ty_string == "*const *const c_char" {
|
|
|
|
return Some(quote!{
|
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: &'a [*const c_char]) -> #name_builder<'a> {
|
|
|
|
self.inner.#param_ident = #param_ident_short.as_ptr();
|
2018-11-18 19:38:33 +11:00
|
|
|
self.inner.#array_size_ident = #param_ident_short.len() as _;
|
2018-10-20 07:16:35 +11:00
|
|
|
self
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
});
|
|
|
|
}
|
2018-10-20 07:16:35 +11:00
|
|
|
|
|
|
|
let slice_param_ty_tokens;
|
2018-12-09 21:52:28 +11:00
|
|
|
let ptr;
|
2018-10-20 07:16:35 +11:00
|
|
|
if param_ty_string.starts_with("*const ") {
|
2018-12-09 21:52:28 +11:00
|
|
|
let slice_type = ¶m_ty_string[7..];
|
|
|
|
if slice_type == "c_void" {
|
|
|
|
slice_param_ty_tokens = "&'a [u8]".to_string();
|
|
|
|
ptr = ".as_ptr() as *const c_void";
|
|
|
|
} else {
|
|
|
|
slice_param_ty_tokens = "&'a [".to_string() + slice_type + "]";
|
|
|
|
ptr = ".as_ptr()";
|
|
|
|
}
|
2018-10-20 07:16:35 +11:00
|
|
|
} else {
|
|
|
|
// *mut
|
2018-12-09 21:52:28 +11:00
|
|
|
let slice_type = ¶m_ty_string[5..];
|
|
|
|
if slice_type == "c_void" {
|
|
|
|
slice_param_ty_tokens = "&mut 'a [u8]".to_string();
|
|
|
|
ptr = ".as_mut_ptr() as *mut c_void";
|
|
|
|
} else {
|
|
|
|
slice_param_ty_tokens = "&'a mut [".to_string() + slice_type + "]";
|
|
|
|
ptr = ".as_mut_ptr()";
|
|
|
|
}
|
2018-10-20 07:16:35 +11:00
|
|
|
}
|
|
|
|
let slice_param_ty_tokens = Term::intern(&slice_param_ty_tokens);
|
2018-12-09 21:52:28 +11:00
|
|
|
let ptr = Term::intern(ptr);
|
2018-10-20 07:16:35 +11:00
|
|
|
|
|
|
|
match array_type {
|
|
|
|
vkxml::ArrayType::Dynamic => {
|
|
|
|
return Some(quote!{
|
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> {
|
2018-11-18 19:38:33 +11:00
|
|
|
self.inner.#array_size_ident = #param_ident_short.len() as _;
|
2018-12-09 21:52:28 +11:00
|
|
|
self.inner.#param_ident = #param_ident_short#ptr;
|
2018-10-20 07:16:35 +11:00
|
|
|
self
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
_ => {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-10-27 00:49:12 +11:00
|
|
|
if param_ty_string.starts_with("*const ") {
|
2018-10-20 07:16:35 +11:00
|
|
|
let slice_param_ty_tokens = "&'a ".to_string() + ¶m_ty_string[7..];
|
|
|
|
let slice_param_ty_tokens = Term::intern(&slice_param_ty_tokens);
|
|
|
|
return Some(quote!{
|
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: #slice_param_ty_tokens) -> #name_builder<'a> {
|
|
|
|
self.inner.#param_ident = #param_ident_short;
|
|
|
|
self
|
|
|
|
}
|
2018-11-13 09:22:16 +11:00
|
|
|
});
|
2018-10-20 07:16:35 +11:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-13 09:22:16 +11:00
|
|
|
if param_ty_string == "Bool32" {
|
|
|
|
return Some(quote!{
|
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: bool) -> #name_builder<'a> {
|
|
|
|
self.inner.#param_ident = #param_ident_short.into();
|
|
|
|
self
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
Some(quote!{
|
|
|
|
pub fn #param_ident_short(mut self, #param_ident_short: #param_ty_tokens) -> #name_builder<'a> {
|
|
|
|
self.inner.#param_ident = #param_ident_short;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2018-11-19 00:50:37 +11:00
|
|
|
let mut nexts = Vec::new();
|
2018-12-01 12:43:34 +11:00
|
|
|
let extends_name = name_to_tokens(&format!("Extends{}", name));
|
2018-11-19 00:50:37 +11:00
|
|
|
if let Some(extends) = &_struct.extends {
|
|
|
|
for target in extends.split(',') {
|
2018-12-01 12:52:23 +11:00
|
|
|
let target = match target {
|
|
|
|
// https://github.com/KhronosGroup/Vulkan-Docs/pull/870
|
|
|
|
"VkPhysicalDeviceProperties" => "VkPhysicalDeviceProperties2",
|
|
|
|
x => x,
|
|
|
|
};
|
2018-12-01 12:43:34 +11:00
|
|
|
let target_ident = name_to_tokens(&format!("Extends{}", name_to_tokens(target)));
|
2018-11-19 00:50:37 +11:00
|
|
|
nexts.push(quote! {
|
2018-12-01 12:43:34 +11:00
|
|
|
unsafe impl #target_ident for #name {}
|
2018-11-19 00:50:37 +11:00
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let next_function = if has_next {
|
|
|
|
if is_next_const {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-12-01 12:43:34 +11:00
|
|
|
pub fn next<T>(mut self, next: &'a T) -> #name_builder<'a> where T: #extends_name {
|
2018-11-19 00:50:37 +11:00
|
|
|
self.inner.p_next = next as *const T as *const c_void;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-12-01 12:43:34 +11:00
|
|
|
pub fn next<T>(mut self, next: &'a mut T) -> #name_builder<'a> where T: #extends_name {
|
2018-11-19 00:50:37 +11:00
|
|
|
self.inner.p_next = next as *mut T as *mut c_void;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {}
|
2018-11-19 00:50:37 +11:00
|
|
|
};
|
|
|
|
|
2018-12-01 12:52:23 +11:00
|
|
|
let next_trait = if has_next {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-12-01 12:52:23 +11:00
|
|
|
pub unsafe trait #extends_name {}
|
|
|
|
}
|
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {}
|
2018-12-01 12:52:23 +11:00
|
|
|
};
|
|
|
|
|
2018-12-07 20:33:36 +11:00
|
|
|
let q = quote! {
|
2018-10-20 07:16:35 +11:00
|
|
|
impl #name {
|
2018-11-19 00:50:37 +11:00
|
|
|
pub fn builder<'a>() -> #name_builder<'a> {
|
2018-10-20 07:16:35 +11:00
|
|
|
#name_builder {
|
|
|
|
inner: #name::default(),
|
|
|
|
marker: ::std::marker::PhantomData,
|
|
|
|
}
|
2018-11-19 00:50:37 +11:00
|
|
|
}
|
|
|
|
}
|
2018-10-20 07:16:35 +11:00
|
|
|
|
|
|
|
pub struct #name_builder<'a> {
|
|
|
|
inner: #name,
|
|
|
|
marker: ::std::marker::PhantomData<&'a ()>,
|
|
|
|
}
|
|
|
|
|
2018-12-01 12:52:23 +11:00
|
|
|
#next_trait
|
2018-11-19 00:50:37 +11:00
|
|
|
|
|
|
|
#(#nexts)*
|
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
impl<'a> ::std::ops::Deref for #name_builder<'a> {
|
|
|
|
type Target = #name;
|
|
|
|
|
|
|
|
fn deref(&self) -> &Self::Target {
|
|
|
|
&self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> #name_builder<'a> {
|
|
|
|
#(#setters)*
|
|
|
|
|
2018-11-19 00:50:37 +11:00
|
|
|
#next_function
|
|
|
|
|
2018-10-20 07:16:35 +11:00
|
|
|
pub fn build(self) -> #name {
|
|
|
|
self.inner
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
Some(q)
|
|
|
|
}
|
|
|
|
|
2018-08-20 15:30:10 +10:00
|
|
|
/// At the moment `Ash` doesn't properly derive all the necessary drives
|
|
|
|
/// like Eq, Hash etc.
|
|
|
|
/// To Address some cases, you can add the name of the struct that you
|
|
|
|
/// require and add the missing derives yourself.
|
|
|
|
pub fn manual_derives(_struct: &vkxml::Struct) -> Tokens {
|
|
|
|
match _struct.name.as_str() {
|
2018-12-07 20:33:36 +11:00
|
|
|
"VkExtent3D" | "VKExtent2D" => quote! {PartialEq, Eq, Hash,},
|
|
|
|
_ => quote! {},
|
2018-08-19 18:10:11 +10:00
|
|
|
}
|
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
pub fn generate_struct(_struct: &vkxml::Struct, union_types: &HashSet<&str>) -> Tokens {
|
|
|
|
let name = name_to_tokens(&_struct.name);
|
|
|
|
let members = _struct.elements.iter().filter_map(|elem| match *elem {
|
|
|
|
vkxml::StructElement::Member(ref field) => Some(field),
|
|
|
|
_ => None,
|
|
|
|
});
|
|
|
|
|
|
|
|
let params = members.clone().map(|field| {
|
|
|
|
let param_ident = field.param_ident();
|
|
|
|
let param_ty_tokens = field.type_tokens();
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {pub #param_ident: #param_ty_tokens}
|
2018-07-30 20:50:51 +10:00
|
|
|
});
|
|
|
|
|
|
|
|
let debug_tokens = derive_debug(_struct, union_types);
|
2018-08-19 18:10:11 +10:00
|
|
|
let default_tokens = derive_default(_struct);
|
2018-10-20 07:16:35 +11:00
|
|
|
let setter_tokens = derive_setters(_struct);
|
2018-08-20 15:30:10 +10:00
|
|
|
let manual_derive_tokens = manual_derives(_struct);
|
2018-07-31 03:53:12 +10:00
|
|
|
let dbg_str = if debug_tokens.is_none() {
|
|
|
|
quote!(Debug,)
|
|
|
|
} else {
|
|
|
|
quote!()
|
|
|
|
};
|
|
|
|
let default_str = if default_tokens.is_none() {
|
|
|
|
quote!(Default,)
|
|
|
|
} else {
|
|
|
|
quote!()
|
|
|
|
};
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-03-11 02:21:35 +11:00
|
|
|
#[repr(C)]
|
2018-08-20 15:30:10 +10:00
|
|
|
#[derive(Copy, Clone, #default_str #dbg_str #manual_derive_tokens)]
|
2018-03-11 02:21:35 +11:00
|
|
|
pub struct #name {
|
|
|
|
#(#params,)*
|
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
#debug_tokens
|
2018-07-31 03:53:12 +10:00
|
|
|
#default_tokens
|
2018-10-20 07:16:35 +11:00
|
|
|
#setter_tokens
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
|
|
|
|
pub fn generate_handle(handle: &vkxml::Handle) -> Option<Tokens> {
|
|
|
|
if handle.name == "" {
|
|
|
|
return None;
|
|
|
|
}
|
|
|
|
let tokens = match handle.ty {
|
|
|
|
vkxml::HandleType::Dispatch => {
|
|
|
|
let name = &handle.name[2..];
|
2018-08-02 10:32:16 +10:00
|
|
|
let ty = Ident::from(name.to_shouty_snake_case());
|
2018-06-24 20:09:37 +10:00
|
|
|
let name = Ident::from(name);
|
|
|
|
quote! {
|
2018-08-02 10:32:16 +10:00
|
|
|
define_handle!(#name, #ty);
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
vkxml::HandleType::NoDispatch => {
|
|
|
|
let name = &handle.name[2..];
|
2018-08-02 10:32:16 +10:00
|
|
|
let ty = Ident::from(name.to_shouty_snake_case());
|
2018-06-24 20:09:37 +10:00
|
|
|
let name = Ident::from(name);
|
|
|
|
quote! {
|
2018-08-02 10:32:16 +10:00
|
|
|
handle_nondispatchable!(#name, #ty);
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
Some(tokens)
|
|
|
|
}
|
|
|
|
fn generate_funcptr(fnptr: &vkxml::FunctionPointer) -> Tokens {
|
|
|
|
let name = Ident::from(fnptr.name.as_str());
|
|
|
|
let ret_ty_tokens = fnptr.return_type.type_tokens();
|
2018-07-11 21:18:22 +10:00
|
|
|
let params = fnptr.param.iter().map(|field| {
|
|
|
|
let ident = field.param_ident();
|
|
|
|
let type_tokens = field.type_tokens();
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-11 21:18:22 +10:00
|
|
|
#ident: #type_tokens
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-07 20:54:31 +10:00
|
|
|
#[allow(non_camel_case_types)]
|
2018-10-08 04:09:29 +11:00
|
|
|
pub type #name = Option<unsafe extern "system" fn(#(#params),*) -> #ret_ty_tokens>;
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
|
|
|
}
|
2018-07-07 18:43:44 +10:00
|
|
|
|
2018-06-24 20:09:37 +10:00
|
|
|
fn generate_union(union: &vkxml::Union) -> Tokens {
|
2018-07-07 18:43:44 +10:00
|
|
|
let name = to_type_tokens(&union.name, None);
|
|
|
|
let fields = union.elements.iter().map(|field| {
|
|
|
|
let name = field.param_ident();
|
|
|
|
let ty = field.type_tokens();
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-07 18:43:44 +10:00
|
|
|
pub #name: #ty
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-07 18:43:44 +10:00
|
|
|
#[repr(C)]
|
|
|
|
#[derive(Copy, Clone)]
|
|
|
|
pub union #name {
|
|
|
|
#(#fields),*
|
|
|
|
}
|
2018-07-31 03:53:12 +10:00
|
|
|
impl ::std::default::Default for #name {
|
|
|
|
fn default() -> #name {
|
|
|
|
unsafe { ::std::mem::zeroed() }
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 18:43:44 +10:00
|
|
|
}
|
2018-06-24 20:09:37 +10:00
|
|
|
}
|
2018-07-30 20:50:51 +10:00
|
|
|
pub fn generate_definition(
|
|
|
|
definition: &vkxml::DefinitionsElement,
|
|
|
|
union_types: &HashSet<&str>,
|
2018-11-18 19:29:17 +11:00
|
|
|
bitflags_cache: &mut HashSet<Ident>,
|
2018-07-30 20:50:51 +10:00
|
|
|
) -> Option<Tokens> {
|
2018-03-11 02:21:35 +11:00
|
|
|
match *definition {
|
2018-06-04 19:26:14 +10:00
|
|
|
vkxml::DefinitionsElement::Typedef(ref typedef) => Some(generate_typedef(typedef)),
|
2018-07-30 20:50:51 +10:00
|
|
|
vkxml::DefinitionsElement::Struct(ref _struct) => {
|
|
|
|
Some(generate_struct(_struct, union_types))
|
|
|
|
}
|
2018-11-18 19:29:17 +11:00
|
|
|
vkxml::DefinitionsElement::Bitmask(ref mask) => generate_bitmask(mask, bitflags_cache),
|
2018-06-24 20:09:37 +10:00
|
|
|
vkxml::DefinitionsElement::Handle(ref handle) => generate_handle(handle),
|
|
|
|
vkxml::DefinitionsElement::FuncPtr(ref fp) => Some(generate_funcptr(fp)),
|
|
|
|
vkxml::DefinitionsElement::Union(ref union) => Some(generate_union(union)),
|
2018-06-04 19:26:14 +10:00
|
|
|
_ => None,
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|
|
|
|
}
|
2018-11-17 03:57:08 +11:00
|
|
|
pub fn generate_feature<'a>(
|
|
|
|
feature: &vkxml::Feature,
|
|
|
|
commands: &CommandMap<'a>,
|
|
|
|
fn_cache: &mut HashSet<&'a str>,
|
|
|
|
) -> quote::Tokens {
|
2018-07-07 20:54:31 +10:00
|
|
|
let (static_commands, entry_commands, device_commands, instance_commands) = feature
|
2018-03-10 07:45:58 +11:00
|
|
|
.elements
|
|
|
|
.iter()
|
|
|
|
.flat_map(|feature| {
|
2018-07-30 16:54:03 +10:00
|
|
|
if let vkxml::FeatureElement::Require(ref spec) = feature {
|
2018-03-10 07:45:58 +11:00
|
|
|
spec.elements
|
|
|
|
.iter()
|
|
|
|
.filter_map(|feature_spec| {
|
2018-07-30 20:50:51 +10:00
|
|
|
if let vkxml::FeatureReference::CommandReference(ref cmd_ref) = feature_spec
|
2018-03-10 07:45:58 +11:00
|
|
|
{
|
|
|
|
Some(cmd_ref)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect()
|
2018-03-10 07:45:58 +11:00
|
|
|
} else {
|
|
|
|
vec![]
|
|
|
|
}
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.filter_map(|cmd_ref| commands.get(&cmd_ref.name))
|
2018-07-07 20:54:31 +10:00
|
|
|
.fold(
|
|
|
|
(Vec::new(), Vec::new(), Vec::new(), Vec::new()),
|
|
|
|
|mut acc, &cmd_ref| {
|
|
|
|
match cmd_ref.function_type() {
|
|
|
|
FunctionType::Static => {
|
|
|
|
acc.0.push(cmd_ref);
|
|
|
|
}
|
|
|
|
FunctionType::Entry => {
|
|
|
|
acc.1.push(cmd_ref);
|
|
|
|
}
|
|
|
|
FunctionType::Device => {
|
|
|
|
acc.2.push(cmd_ref);
|
|
|
|
}
|
|
|
|
FunctionType::Instance => {
|
|
|
|
acc.3.push(cmd_ref);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
acc
|
|
|
|
},
|
|
|
|
);
|
2018-03-10 08:43:45 +11:00
|
|
|
let version = feature.version_string();
|
2018-07-07 20:54:31 +10:00
|
|
|
let static_fn = if feature.version == 1.0 {
|
2018-11-17 03:57:08 +11:00
|
|
|
generate_function_pointers(Ident::from("StaticFn"), &static_commands, fn_cache)
|
2018-07-07 20:54:31 +10:00
|
|
|
} else {
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {}
|
2018-07-07 20:54:31 +10:00
|
|
|
};
|
|
|
|
let entry = generate_function_pointers(
|
|
|
|
Ident::from(format!("EntryFnV{}", version).as_str()),
|
|
|
|
&entry_commands,
|
2018-11-17 03:57:08 +11:00
|
|
|
fn_cache,
|
2018-07-07 20:54:31 +10:00
|
|
|
);
|
2018-03-10 08:43:45 +11:00
|
|
|
let instance = generate_function_pointers(
|
|
|
|
Ident::from(format!("InstanceFnV{}", version).as_str()),
|
|
|
|
&instance_commands,
|
2018-11-17 03:57:08 +11:00
|
|
|
fn_cache,
|
2018-03-10 08:43:45 +11:00
|
|
|
);
|
|
|
|
let device = generate_function_pointers(
|
|
|
|
Ident::from(format!("DeviceFnV{}", version).as_str()),
|
2018-03-10 20:07:12 +11:00
|
|
|
&device_commands,
|
2018-11-17 03:57:08 +11:00
|
|
|
fn_cache,
|
2018-03-10 08:43:45 +11:00
|
|
|
);
|
|
|
|
quote! {
|
2018-07-07 20:54:31 +10:00
|
|
|
#static_fn
|
|
|
|
#entry
|
2018-03-10 08:43:45 +11:00
|
|
|
#instance
|
|
|
|
#device
|
|
|
|
}
|
2018-03-10 07:45:58 +11:00
|
|
|
}
|
2018-08-01 17:22:28 +10:00
|
|
|
pub fn constant_name(name: &str) -> String {
|
|
|
|
name.replace("VK_", "")
|
|
|
|
}
|
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
pub fn generate_constant<'a>(
|
|
|
|
constant: &'a vkxml::Constant,
|
|
|
|
cache: &mut HashSet<&'a str>,
|
|
|
|
) -> Tokens {
|
|
|
|
cache.insert(constant.name.as_str());
|
2018-06-24 20:09:37 +10:00
|
|
|
let c = Constant::from_constant(constant);
|
2018-08-01 17:22:28 +10:00
|
|
|
let name = constant_name(&constant.name);
|
2018-08-01 17:12:39 +10:00
|
|
|
let ident = Ident::from(name.as_str());
|
2018-06-24 20:09:37 +10:00
|
|
|
let value = c.to_tokens();
|
2018-08-03 23:39:25 +10:00
|
|
|
let ty = if name == "TRUE" || name == "FALSE" {
|
|
|
|
CType::Bool32
|
|
|
|
} else {
|
|
|
|
c.ty()
|
|
|
|
};
|
|
|
|
let ty = ty.to_tokens();
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-06-24 20:09:37 +10:00
|
|
|
pub const #ident: #ty = #value;
|
|
|
|
}
|
|
|
|
}
|
2018-03-11 02:21:35 +11:00
|
|
|
|
2018-07-31 04:06:00 +10:00
|
|
|
pub fn generate_feature_extension<'a>(
|
|
|
|
registry: &'a vk_parse::Registry,
|
|
|
|
const_cache: &mut HashSet<&'a str>,
|
2018-11-17 03:57:08 +11:00
|
|
|
const_values: &mut HashMap<Ident, Vec<Ident>>,
|
2018-07-31 04:06:00 +10:00
|
|
|
) -> Tokens {
|
2018-11-18 06:00:57 +11:00
|
|
|
let constants = registry.0.iter().filter_map(|item| match item {
|
|
|
|
vk_parse::RegistryChild::Feature(feature) => Some(generate_extension_constants(
|
|
|
|
&feature.name,
|
|
|
|
0,
|
|
|
|
&feature.children,
|
|
|
|
const_cache,
|
|
|
|
const_values,
|
|
|
|
)),
|
|
|
|
_ => None,
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-07-31 04:06:00 +10:00
|
|
|
#(#constants)*
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-08-20 12:40:28 +10:00
|
|
|
pub fn generate_const_displays<'a>(const_values: &HashMap<Ident, Vec<Ident>>) -> Tokens {
|
2018-11-17 03:57:08 +11:00
|
|
|
let impls = const_values
|
|
|
|
.iter()
|
2018-08-20 12:40:28 +10:00
|
|
|
.filter(|(ty, _)| *ty != "Result")
|
|
|
|
.map(|(ty, values)| {
|
|
|
|
if ty.to_string().contains("Flags") {
|
|
|
|
let cases = values.iter().map(|value| {
|
|
|
|
let name = value.to_string();
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! { (#ty::#value.0, #name) }
|
2018-08-20 12:40:28 +10:00
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-20 12:40:28 +10:00
|
|
|
impl fmt::Display for #ty {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
const KNOWN: &[(Flags, &str)] = &[#(#cases),*];
|
|
|
|
display_flags(f, KNOWN, self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
let cases = values.iter().map(|value| {
|
|
|
|
let name = value.to_string();
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! { Self::#value => Some(#name), }
|
2018-08-20 12:40:28 +10:00
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-20 12:40:28 +10:00
|
|
|
impl fmt::Display for #ty {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
let name = match *self {
|
|
|
|
#(#cases)*
|
|
|
|
_ => None,
|
|
|
|
};
|
|
|
|
if let Some(x) = name {
|
|
|
|
f.write_str(x)
|
|
|
|
} else {
|
|
|
|
write!(f, "{}", self.0)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-08-20 12:40:28 +10:00
|
|
|
fn display_flags(f: &mut fmt::Formatter, known: &[(Flags, &'static str)], value: Flags) -> fmt::Result {
|
|
|
|
let mut first = true;
|
|
|
|
let mut accum = value;
|
|
|
|
for (bit, name) in known {
|
2018-08-21 17:24:01 +10:00
|
|
|
if *bit != 0 && accum & *bit == *bit {
|
2018-08-20 12:40:28 +10:00
|
|
|
if !first { f.write_str(" | ")?; }
|
|
|
|
f.write_str(name)?;
|
|
|
|
first = false;
|
|
|
|
accum &= !bit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if accum != 0 {
|
|
|
|
if !first { f.write_str(" | ")?; }
|
|
|
|
write!(f, "{:b}", accum)?;
|
|
|
|
}
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
#(#impls)*
|
|
|
|
}
|
|
|
|
}
|
2018-11-18 19:29:17 +11:00
|
|
|
pub fn generate_aliases_of_types<'a>(
|
|
|
|
types: &'a vk_parse::Types,
|
|
|
|
ty_cache: &mut HashSet<Ident>,
|
|
|
|
) -> Tokens {
|
|
|
|
let aliases = types
|
|
|
|
.children
|
|
|
|
.iter()
|
|
|
|
.filter_map(|child| match child {
|
|
|
|
vk_parse::TypesChild::Type(ty) => Some((ty.name.as_ref()?, ty.alias.as_ref()?)),
|
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.filter_map(|(name, alias)| {
|
2018-11-18 19:29:17 +11:00
|
|
|
let name_ident = name_to_tokens(name);
|
|
|
|
if ty_cache.contains(&name_ident) {
|
|
|
|
return None;
|
|
|
|
};
|
|
|
|
ty_cache.insert(name_ident.clone());
|
|
|
|
let alias_ident = name_to_tokens(alias);
|
2018-12-07 20:33:36 +11:00
|
|
|
let tokens = quote! {
|
2018-11-18 19:29:17 +11:00
|
|
|
pub type #name_ident = #alias_ident;
|
|
|
|
};
|
|
|
|
Some(tokens)
|
|
|
|
});
|
2018-12-07 20:33:36 +11:00
|
|
|
quote! {
|
2018-11-18 06:00:57 +11:00
|
|
|
#(#aliases)*
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 20:56:16 +10:00
|
|
|
pub fn write_source_code(path: &Path) {
|
2018-03-11 02:21:35 +11:00
|
|
|
use std::fs::File;
|
2018-06-04 19:26:14 +10:00
|
|
|
use std::io::Write;
|
2018-07-21 20:56:16 +10:00
|
|
|
let spec2 = vk_parse::parse_file(path);
|
|
|
|
let extensions: &Vec<vk_parse::Extension> = spec2
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
2018-11-18 03:27:18 +11:00
|
|
|
vk_parse::RegistryChild::Extensions(ref ext) => Some(&ext.children),
|
2018-07-21 20:56:16 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.nth(0)
|
2018-07-21 20:56:16 +10:00
|
|
|
.expect("extension");
|
2018-11-18 06:00:57 +11:00
|
|
|
let mut ty_cache = HashSet::new();
|
|
|
|
let aliases: Vec<_> = spec2
|
|
|
|
.0
|
|
|
|
.iter()
|
|
|
|
.filter_map(|item| match item {
|
2018-11-18 19:29:17 +11:00
|
|
|
vk_parse::RegistryChild::Types(ref ty) => {
|
|
|
|
Some(generate_aliases_of_types(ty, &mut ty_cache))
|
|
|
|
}
|
2018-11-18 06:00:57 +11:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-07-21 20:56:16 +10:00
|
|
|
|
|
|
|
let spec = vk_parse::parse_file_as_vkxml(path);
|
2018-11-18 05:05:28 +11:00
|
|
|
let commands: HashMap<vkxml::Identifier, &vkxml::Command> = spec
|
|
|
|
.elements
|
2018-03-11 02:21:35 +11:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Commands(ref cmds) => Some(cmds),
|
2018-03-11 02:21:35 +11:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|cmds| cmds.elements.iter().map(|cmd| (cmd.name.clone(), cmd)))
|
2018-03-11 02:21:35 +11:00
|
|
|
.collect();
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let features: Vec<&vkxml::Feature> = spec
|
|
|
|
.elements
|
2018-03-11 02:21:35 +11:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Features(ref features) => Some(features),
|
2018-03-11 02:21:35 +11:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|features| features.elements.iter())
|
2018-03-11 02:21:35 +11:00
|
|
|
.collect();
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let definitions: Vec<&vkxml::DefinitionsElement> = spec
|
|
|
|
.elements
|
2018-03-11 02:21:35 +11:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Definitions(ref definitions) => Some(definitions),
|
2018-03-11 02:21:35 +11:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|definitions| definitions.elements.iter())
|
2018-03-11 02:21:35 +11:00
|
|
|
.collect();
|
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let enums: Vec<&vkxml::Enumeration> = spec
|
|
|
|
.elements
|
2018-04-01 18:52:21 +10:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Enums(ref enums) => Some(enums),
|
2018-04-01 18:52:21 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|enums| {
|
2018-04-01 18:52:21 +10:00
|
|
|
enums.elements.iter().filter_map(|_enum| match *_enum {
|
|
|
|
vkxml::EnumsElement::Enumeration(ref e) => Some(e),
|
|
|
|
_ => None,
|
|
|
|
})
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect();
|
2018-04-01 18:52:21 +10:00
|
|
|
|
2018-11-18 05:05:28 +11:00
|
|
|
let constants: Vec<&vkxml::Constant> = spec
|
|
|
|
.elements
|
2018-04-01 18:52:21 +10:00
|
|
|
.iter()
|
|
|
|
.filter_map(|elem| match elem {
|
2018-07-30 16:54:03 +10:00
|
|
|
vkxml::RegistryElement::Constants(ref constants) => Some(constants),
|
2018-04-01 18:52:21 +10:00
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.flat_map(|constants| constants.elements.iter())
|
2018-04-01 18:52:21 +10:00
|
|
|
.collect();
|
|
|
|
|
2018-11-17 03:57:08 +11:00
|
|
|
let mut fn_cache = HashSet::new();
|
2018-11-18 19:29:17 +11:00
|
|
|
let mut bitflags_cache = HashSet::new();
|
2018-07-30 06:39:45 +10:00
|
|
|
let mut const_cache = HashSet::new();
|
2018-08-20 12:40:28 +10:00
|
|
|
|
|
|
|
let mut const_values: HashMap<Ident, Vec<Ident>> = HashMap::new();
|
|
|
|
|
2018-07-30 06:39:45 +10:00
|
|
|
let (enum_code, bitflags_code) = enums
|
|
|
|
.into_iter()
|
2018-11-18 19:29:17 +11:00
|
|
|
.map(|e| generate_enum(e, &mut const_cache, &mut const_values, &mut bitflags_cache))
|
2018-07-30 06:39:45 +10:00
|
|
|
.fold((Vec::new(), Vec::new()), |mut acc, elem| {
|
2018-04-01 18:52:21 +10:00
|
|
|
match elem {
|
|
|
|
EnumType::Enum(token) => acc.0.push(token),
|
|
|
|
EnumType::Bitflags(token) => acc.1.push(token),
|
|
|
|
};
|
|
|
|
acc
|
2018-07-30 06:39:45 +10:00
|
|
|
});
|
2018-08-23 06:53:17 +10:00
|
|
|
|
2018-07-07 22:49:17 +10:00
|
|
|
let constants_code: Vec<_> = constants
|
|
|
|
.iter()
|
2018-07-30 06:39:45 +10:00
|
|
|
.map(|constant| generate_constant(constant, &mut const_cache))
|
2018-07-07 22:49:17 +10:00
|
|
|
.collect();
|
2018-07-30 06:39:45 +10:00
|
|
|
let extension_code = extensions
|
|
|
|
.iter()
|
2018-11-17 03:57:08 +11:00
|
|
|
.filter_map(|ext| {
|
|
|
|
generate_extension(
|
|
|
|
ext,
|
|
|
|
&commands,
|
|
|
|
&mut const_cache,
|
|
|
|
&mut const_values,
|
|
|
|
&mut fn_cache,
|
|
|
|
)
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect_vec();
|
2018-04-01 18:52:21 +10:00
|
|
|
|
2018-07-30 20:50:51 +10:00
|
|
|
let union_types = definitions
|
|
|
|
.iter()
|
|
|
|
.filter_map(|def| match def {
|
|
|
|
vkxml::DefinitionsElement::Union(ref union) => Some(union.name.as_str()),
|
|
|
|
_ => None,
|
2018-12-07 20:33:36 +11:00
|
|
|
})
|
|
|
|
.collect::<HashSet<&str>>();
|
2018-07-30 20:50:51 +10:00
|
|
|
|
2018-06-06 00:47:21 +10:00
|
|
|
let definition_code: Vec<_> = definitions
|
|
|
|
.into_iter()
|
2018-11-18 19:29:17 +11:00
|
|
|
.filter_map(|def| generate_definition(def, &union_types, &mut bitflags_cache))
|
2018-06-06 00:47:21 +10:00
|
|
|
.collect();
|
2018-03-11 02:21:35 +11:00
|
|
|
|
|
|
|
let feature_code: Vec<_> = features
|
|
|
|
.iter()
|
2018-11-17 03:57:08 +11:00
|
|
|
.map(|feature| generate_feature(feature, &commands, &mut fn_cache))
|
2018-03-11 02:21:35 +11:00
|
|
|
.collect();
|
2018-11-17 03:57:08 +11:00
|
|
|
let feature_extensions_code =
|
|
|
|
generate_feature_extension(&spec2, &mut const_cache, &mut const_values);
|
2018-08-20 12:40:28 +10:00
|
|
|
|
|
|
|
let const_displays = generate_const_displays(&const_values);
|
2018-03-11 02:21:35 +11:00
|
|
|
|
2018-07-09 16:49:28 +10:00
|
|
|
let mut file = File::create("../ash/src/vk.rs").expect("vk");
|
2018-06-24 20:09:37 +10:00
|
|
|
let bitflags_macro = vk_bitflags_wrapped_macro();
|
|
|
|
let handle_nondispatchable_macro = handle_nondispatchable_macro();
|
|
|
|
let define_handle_macro = define_handle_macro();
|
2018-07-07 22:49:17 +10:00
|
|
|
let version_macros = vk_version_macros();
|
2018-07-07 20:54:31 +10:00
|
|
|
let platform_specific_types = platform_specific_types();
|
2018-12-07 20:33:36 +11:00
|
|
|
let source_code = quote! {
|
2018-08-20 12:40:28 +10:00
|
|
|
use std::fmt;
|
2018-08-29 03:01:16 +10:00
|
|
|
use std::os::raw::*;
|
2018-08-02 10:32:16 +10:00
|
|
|
|
|
|
|
pub trait Handle {
|
|
|
|
const TYPE: ObjectType;
|
|
|
|
fn as_raw(self) -> u64;
|
|
|
|
fn from_raw(u64) -> Self;
|
|
|
|
}
|
|
|
|
|
2018-07-09 17:23:53 +10:00
|
|
|
#version_macros
|
|
|
|
#platform_specific_types
|
2018-07-07 20:54:31 +10:00
|
|
|
#bitflags_macro
|
2018-07-09 17:23:53 +10:00
|
|
|
#handle_nondispatchable_macro
|
|
|
|
#define_handle_macro
|
|
|
|
#(#feature_code)*
|
|
|
|
#(#definition_code)*
|
2018-07-07 20:54:31 +10:00
|
|
|
#(#enum_code)*
|
2018-09-30 04:21:56 +10:00
|
|
|
#(#bitflags_code)*
|
2018-07-09 17:23:53 +10:00
|
|
|
#(#constants_code)*
|
2018-09-30 04:21:56 +10:00
|
|
|
#(#extension_code)*
|
|
|
|
#feature_extensions_code
|
2018-08-20 12:40:28 +10:00
|
|
|
#const_displays
|
2018-11-18 06:00:57 +11:00
|
|
|
#(#aliases)*
|
2018-03-11 02:21:35 +11:00
|
|
|
};
|
2018-07-30 06:39:45 +10:00
|
|
|
write!(&mut file, "{}", source_code).expect("Unable to write to file");
|
2018-03-11 02:21:35 +11:00
|
|
|
}
|