Skip to content

Commit

Permalink
Remove dead code (#1467)
Browse files Browse the repository at this point in the history
  • Loading branch information
kennykerr committed Jan 27, 2022
1 parent d6b88f4 commit 44f7804
Show file tree
Hide file tree
Showing 19 changed files with 27 additions and 143 deletions.
4 changes: 2 additions & 2 deletions crates/libs/bindgen/src/functions.rs
Expand Up @@ -38,10 +38,10 @@ pub fn gen_function(def: &MethodDef, gen: &Gen) -> TokenStream {
}
}

fn gen_function_if(entry: &TypeEntry, gen: &Gen) -> TokenStream {
fn gen_function_if(entry: &Vec<ElementType>, gen: &Gen) -> TokenStream {
let mut tokens = TokenStream::new();

for def in &entry.def {
for def in entry {
if let ElementType::MethodDef(def) = def {
tokens.combine(&gen_sys_function(def, gen));
}
Expand Down
10 changes: 1 addition & 9 deletions crates/libs/bindgen/src/gen.rs
Expand Up @@ -117,9 +117,6 @@ impl Gen<'_> {

fn add_namespace(&self, namespace: &'static str, namespaces: &mut BTreeSet<&'static str>) {
if !namespace.is_empty() && namespace != self.namespace {
//namespaces.insert(namespace);

// TODO: use the above instead to iclude parent dependencies
if !self.namespace.starts_with(format!("{}.", namespace).as_str()) {
namespaces.insert(namespace);
}
Expand Down Expand Up @@ -162,18 +159,13 @@ impl Gen<'_> {
}
TypeKind::Struct => {
def.fields().for_each(|field| self.field_requirements(&field, Some(def), namespaces, keys));

// TODO: needed?
if let Some(def) = def.is_convertible_to() {
self.add_namespace(def.type_name().namespace, namespaces);
}
}
TypeKind::Delegate => self.method_requirements(&def.invoke_method().signature(&[]), namespaces, keys),
_ => {}
}

if let Some(entry) = TypeReader::get().get_type_entry(def.type_name()) {
for def in &entry.def {
for def in entry {
if let ElementType::TypeDef(def) = def {
self.type_requirements(def, namespaces, keys);
}
Expand Down
3 changes: 0 additions & 3 deletions crates/libs/bindgen/src/helpers.rs
Expand Up @@ -396,8 +396,6 @@ fn gen_winrt_invoke_arg(param: &MethodParam, gen: &Gen) -> TokenStream {
let name = gen_param_name(&param.param);
let kind = gen_element_name(&param.signature.kind, gen);

// TODO: probably simplify this once the trait is called since the target type can be inferred safely

if param.signature.is_array {
let abi_size_name: TokenStream = format!("{}_array_size", param.param.name()).into();

Expand All @@ -411,7 +409,6 @@ fn gen_winrt_invoke_arg(param: &MethodParam, gen: &Gen) -> TokenStream {
} else if param.param.is_input() {
if param.signature.kind.is_primitive() {
quote! { #name }
// TODO: probably don't need the explicit type casts s here anymore since we have traits to auto deduce
} else if param.signature.is_const {
quote! { &*(#name as *const <#kind as ::windows::core::Abi>::Abi as *const <#kind as ::windows::core::DefaultType>::DefaultType) }
} else {
Expand Down
6 changes: 3 additions & 3 deletions crates/libs/bindgen/src/lib.rs
Expand Up @@ -37,7 +37,7 @@ pub fn gen_type(name: &str, gen: &Gen) -> String {
let reader = TypeReader::get();
let mut tokens = String::new();

for def in reader.get_type_entry(TypeName::parse(name)).iter().flat_map(|entry| entry.def.iter()) {
for def in reader.get_type_entry(TypeName::parse(name)).iter().flat_map(|entry| entry.iter()) {
tokens.push_str(gen_element_type(def, gen).as_str());
}

Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn gen_namespace_impl(gen: &Gen) -> String {
let mut tokens = TokenStream::new();

for entry in tree.types.values() {
for def in &entry.def {
for def in entry {
if let ElementType::TypeDef(def) = def {
let def = &def.clone().with_generics();
tokens.combine(&implements::gen(def, gen));
Expand All @@ -93,7 +93,7 @@ fn gen_non_sys_function_types(tree: &TypeTree, gen: &Gen) -> TokenStream {
let mut tokens = TokenStream::new();

for entry in tree.types.values() {
for def in &entry.def {
for def in entry {
tokens.combine(&gen_element_type(def, gen));
}
}
Expand Down
5 changes: 0 additions & 5 deletions crates/libs/bindgen/src/methods.rs
Expand Up @@ -49,8 +49,6 @@ pub fn gen_winrt_method(def: &TypeDef, kind: InterfaceKind, method: &MethodDef,
_ => quote! {},
};

// TODO: don't use Interface::vtable trait for winrt/winrt calls - just stamp out vtable type directly

let (vcall, vcall_none) = if let Some(return_sig) = &signature.return_sig {
if return_sig.is_array {
(
Expand Down Expand Up @@ -108,8 +106,6 @@ pub fn gen_winrt_method(def: &TypeDef, kind: InterfaceKind, method: &MethodDef,
}
}
}
// TODO: for composable generate a different signature that lets you provide an Option<Composable> so non
// derived cases can just provide None for the derived type
InterfaceKind::Static => {
quote! {
#cfg
Expand All @@ -133,7 +129,6 @@ pub fn gen_winrt_method(def: &TypeDef, kind: InterfaceKind, method: &MethodDef,
}
}
}
InterfaceKind::Extend | InterfaceKind::Overridable => unimplemented!(), // TODO: should remove these flags
}
}

Expand Down
1 change: 0 additions & 1 deletion crates/libs/bindgen/src/signatures.rs
Expand Up @@ -87,7 +87,6 @@ fn gen_sig_with_const(sig: &Signature, gen: &Gen, is_const: bool) -> TokenStream

let kind = gen_element_name(&sig.kind, gen);

// TODO: harmonize these across sys/win
if sig.kind.is_nullable() && !gen.sys {
tokens.combine(&quote! {
::core::option::Option<#kind>
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/bindgen/src/structs.rs
Expand Up @@ -26,8 +26,8 @@ fn gen_struct_with_name(def: &TypeDef, struct_name: &str, cfg: &Cfg, gen: &Gen)
let value = gen_guid(&guid, gen);
let guid = gen_element_name(&ElementType::GUID, gen);
return quote! { pub const #name: #guid = #value; };
// TODO: why this if about "Vtbl"?
} else if name.as_str().ends_with("Vtbl") {
// This just omits some useless struct declarations like `IDDVideoPortContainerVtbl`
return quote! {};
} else {
return quote! {
Expand Down
7 changes: 3 additions & 4 deletions crates/libs/macros/src/implement.rs
Expand Up @@ -50,7 +50,7 @@ pub fn gen(attributes: proc_macro::TokenStream, original_type: proc_macro::Token
let interface_ident = implement.to_ident();
let offset: TokenStream = format!("{}", enumerate).into();
quote! {
impl <#constraints> From<#original_ident::<#(#generics,)*>> for #interface_ident {
impl <#constraints> ::core::convert::From<#original_ident::<#(#generics,)*>> for #interface_ident {
fn from(this: #original_ident::<#(#generics,)*>) -> Self {
unsafe {
let this = #impl_ident::<#(#generics,)*>::new(this);
Expand Down Expand Up @@ -154,8 +154,7 @@ pub fn gen(attributes: proc_macro::TokenStream, original_type: proc_macro::Token
(inspectable, &mut (*this).base)
}
}
// tODO: From must be fully qualified
impl <#constraints> From<#original_ident::<#(#generics,)*>> for ::windows::core::IUnknown {
impl <#constraints> ::core::convert::From<#original_ident::<#(#generics,)*>> for ::windows::core::IUnknown {
fn from(this: #original_ident::<#(#generics,)*>) -> Self {
unsafe {
let this = #impl_ident::<#(#generics,)*>::new(this);
Expand All @@ -166,7 +165,7 @@ pub fn gen(attributes: proc_macro::TokenStream, original_type: proc_macro::Token
}
}
}
impl <#constraints> From<#original_ident::<#(#generics,)*>> for ::windows::core::IInspectable {
impl <#constraints> ::core::convert::From<#original_ident::<#(#generics,)*>> for ::windows::core::IInspectable {
fn from(this: #original_ident::<#(#generics,)*>) -> Self {
unsafe {
let this = #impl_ident::<#(#generics,)*>::new(this);
Expand Down
1 change: 0 additions & 1 deletion crates/libs/quote/Cargo.toml
@@ -1,5 +1,4 @@
[package]
#TODO: rename to windows_tokens and rename quote! macro to tokens!
name = "windows_quote"
version = "0.30.0"
authors = ["Microsoft"]
Expand Down
4 changes: 0 additions & 4 deletions crates/libs/reader/src/interface_kind.rs
Expand Up @@ -5,8 +5,4 @@ pub enum InterfaceKind {
Static,
Composable,
Base,

// TODO: only used by old gen
Extend,
Overridable,
}
4 changes: 1 addition & 3 deletions crates/libs/reader/src/method_signature.rs
@@ -1,5 +1,3 @@
// TODO: split the parsing code from teh gen code

use super::*;

pub struct MethodSignature {
Expand Down Expand Up @@ -85,7 +83,7 @@ impl MethodParam {

let flags = self.param.flags();

// TODO: NativeArrayInfo indicates and array parameter #479
// TODO: NativeArrayInfo indicates an array parameter #479
if flags.input() || !flags.output() || self.param.array_info() {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion crates/libs/reader/src/tables/type_def.rs
Expand Up @@ -204,7 +204,7 @@ impl TypeDef {
}

if let Some(entry) = TypeReader::get().get_type_entry(self.type_name()) {
for def in &entry.def {
for def in entry {
if let ElementType::TypeDef(def) = def {
def.features(features, keys);
}
Expand Down
22 changes: 0 additions & 22 deletions crates/libs/reader/src/type_name.rs
Expand Up @@ -106,25 +106,3 @@ impl core::fmt::Display for TypeName {
write!(fmt, "{}.{}", self.namespace, self.name)
}
}

#[cfg(test)]
mod tests {
use crate::*;

#[test]
fn test() {
let reader = TypeReader::get();
reader.get_type_entry(TypeName::WIN32_ERROR).unwrap(); // TODO: remove
reader.get_type_entry(TypeName::NTSTATUS).unwrap();
reader.get_type_entry(TypeName::BOOL).unwrap();
reader.get_type_entry(TypeName::PWSTR).unwrap();
reader.get_type_entry(TypeName::PSTR).unwrap();
reader.get_type_entry(TypeName::BSTR).unwrap();
reader.get_type_entry(TypeName::HANDLE).unwrap();
reader.get_type_entry(TypeName::SysStringLen).unwrap();
reader.get_type_entry(TypeName::SysAllocStringLen).unwrap();
reader.get_type_entry(TypeName::SysFreeString).unwrap();
reader.get_type_entry(TypeName::IRestrictedErrorInfo).unwrap();
reader.get_type_entry(TypeName::IDispatch).unwrap();
}
}
18 changes: 4 additions & 14 deletions crates/libs/reader/src/type_reader.rs
Expand Up @@ -10,7 +10,7 @@ pub struct TypeReader {
}

impl TypeReader {
pub fn get_mut() -> &'static mut Self {
pub fn get() -> &'static Self {
use std::{mem::MaybeUninit, sync::Once};
static ONCE: Once = Once::new();
static mut VALUE: MaybeUninit<TypeReader> = MaybeUninit::uninit();
Expand All @@ -21,11 +21,7 @@ impl TypeReader {
});

// This is safe because `call_once` has already been called.
unsafe { &mut *VALUE.as_mut_ptr() }
}

pub fn get() -> &'static Self {
Self::get_mut()
unsafe { &*VALUE.as_mut_ptr() }
}

/// Insert WinRT metadata at the given paths
Expand All @@ -37,7 +33,6 @@ impl TypeReader {
let files = workspace_winmds();
let mut nested = HashMap::<Row, BTreeMap<&'static str, TypeDef>>::new();
let mut types = TypeTree::from_namespace("");
types.include = true;

for file in files {
let row_count = file.type_def_table().row_count;
Expand Down Expand Up @@ -92,21 +87,16 @@ impl TypeReader {
Self { nested, types }
}

/// Get all the namespace names that the [`TypeReader`] knows about
pub fn namespaces(&'static self) -> Vec<&'static str> {
self.types.namespaces()
}

pub fn nested_types(&'static self, enclosing: &TypeDef) -> Option<&BTreeMap<&'static str, TypeDef>> {
self.nested.get(&enclosing.row)
}

pub fn get_type_entry<T: HasTypeName>(&'static self, type_name: T) -> Option<&TypeEntry> {
pub fn get_type_entry<T: HasTypeName>(&'static self, type_name: T) -> Option<&Vec<ElementType>> {
self.types.get_namespace(type_name.namespace()).and_then(|tree| tree.get_type(type_name.name()))
}

pub fn get_type<T: HasTypeName>(&'static self, type_name: T) -> Option<&ElementType> {
self.types.get_namespace(type_name.namespace()).and_then(|tree| tree.get_type(type_name.name())).and_then(|entry| entry.def.first())
self.types.get_namespace(type_name.namespace()).and_then(|tree| tree.get_type(type_name.name())).and_then(|entry| entry.first())
}

pub fn get_namespace(&self, namespace: &str) -> Option<&TypeTree> {
Expand Down
69 changes: 5 additions & 64 deletions crates/libs/reader/src/type_tree.rs
@@ -1,43 +1,21 @@
use super::*;
pub use std::collections::BTreeMap;

// TODO: remove all include/dependency tracking logic from reader

#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd)]
pub enum TypeInclude {
Full,
Minimal,
None,
}

impl Default for TypeInclude {
fn default() -> Self {
Self::None
}
}

#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Default)]
pub struct TypeEntry {
pub def: Vec<ElementType>,
pub include: TypeInclude,
}

// The TypeTree needs to use a BTreeMap rather than the fast HashMap because it affects code gen and we need
// the code gen to be stable.
pub struct TypeTree {
pub namespace: &'static str,
pub types: BTreeMap<&'static str, TypeEntry>,
pub types: BTreeMap<&'static str, Vec<ElementType>>,
pub namespaces: BTreeMap<&'static str, TypeTree>,
pub include: bool,
}

impl TypeTree {
pub fn from_namespace(namespace: &'static str) -> Self {
Self { namespace, types: BTreeMap::new(), namespaces: BTreeMap::new(), include: false }
Self { namespace, types: BTreeMap::new(), namespaces: BTreeMap::new() }
}

pub fn features(&self, features: &mut BTreeSet<&'static str>, keys: &mut std::collections::HashSet<Row>) {
self.types.values().flat_map(|entry| entry.def.iter()).for_each(|def| def.features(features, keys));
self.types.values().flat_map(|entry| entry.iter()).for_each(|def| def.features(features, keys));
}

pub fn insert_namespace(&mut self, namespace: &'static str, pos: usize) -> &mut Self {
Expand All @@ -50,55 +28,18 @@ impl TypeTree {
}

pub fn insert_type(&mut self, name: &'static str, def: ElementType) {
self.types.entry(name).or_default().def.push(def);
}

// TODO: slow method - remove or make this an iterator somehow?
pub fn namespaces(&self) -> Vec<&'static str> {
let mut namespaces = Vec::new();

for tree in self.namespaces.values() {
if !tree.types.is_empty() {
namespaces.push(tree.namespace)
}

namespaces.append(&mut tree.namespaces());
}

namespaces
self.types.entry(name).or_default().push(def);
}

pub fn get_type(&self, name: &str) -> Option<&TypeEntry> {
pub fn get_type(&self, name: &str) -> Option<&Vec<ElementType>> {
self.types.get(name)
}

pub fn get_type_mut(&mut self, name: &str) -> Option<&mut TypeEntry> {
self.types.get_mut(name)
}

pub fn get_namespace(&self, namespace: &str) -> Option<&Self> {
if let Some(next) = namespace.find('.') {
self.namespaces.get(&namespace[..next]).and_then(|child| child.get_namespace(&namespace[next + 1..]))
} else {
self.namespaces.get(namespace)
}
}

pub fn get_namespace_mut(&mut self, namespace: &str) -> Option<&mut Self> {
self.include = true;
if let Some(next) = namespace.find('.') {
self.namespaces.get_mut(&namespace[..next]).and_then(|child| child.get_namespace_mut(&namespace[next + 1..]))
} else {
self.namespaces.get_mut(namespace).map(|ns| {
ns.include = true;
ns
})
}
}

pub fn exclude_namespace(&mut self, namespace: &str) {
if let Some(tree) = self.get_namespace_mut(namespace) {
tree.include = false;
}
}
}
2 changes: 1 addition & 1 deletion crates/tools/api/src/main.rs
Expand Up @@ -9,7 +9,7 @@ fn main() {
let _ = std::fs::remove_dir_all(&output);
output.pop();

let reader = reader::TypeReader::get_mut();
let reader = reader::TypeReader::get();
let root = reader.types.get_namespace("Windows").unwrap();

let mut trees = Vec::new();
Expand Down

0 comments on commit 44f7804

Please sign in to comment.