ir: Deal with name conflicts correctly in declaration type resolution.

So, basically, make opaque items last, and make previous names override
them.

Fixes #649
This commit is contained in:
Emilio Cobos Álvarez
2021-01-26 12:41:58 +01:00
parent c47ee1516b
commit 750745831a
15 changed files with 260 additions and 38 deletions
+28 -29
View File
@@ -2,23 +2,9 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use std::collections::HashSet;
use crate::bindgen::ir::Path;
#[derive(Default)]
pub struct DeclarationTypeResolver {
structs: HashSet<Path>,
enums: HashSet<Path>,
unions: HashSet<Path>,
}
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub enum DeclarationType {
Struct,
Enum,
Union,
}
use std::collections::hash_map::Entry;
use std::collections::HashMap;
impl DeclarationType {
pub fn to_str(self) -> &'static str {
@@ -30,29 +16,42 @@ impl DeclarationType {
}
}
#[derive(Clone, Copy, PartialEq, PartialOrd, Eq, Ord, Debug, Hash)]
pub enum DeclarationType {
Struct,
Enum,
Union,
}
#[derive(Default)]
pub struct DeclarationTypeResolver {
types: HashMap<Path, Option<DeclarationType>>,
}
impl DeclarationTypeResolver {
fn insert(&mut self, path: &Path, ty: Option<DeclarationType>) {
if let Entry::Vacant(vacant_entry) = self.types.entry(path.clone()) {
vacant_entry.insert(ty);
}
}
pub fn add_enum(&mut self, path: &Path) {
self.enums.insert(path.clone());
self.insert(path, Some(DeclarationType::Enum));
}
pub fn add_struct(&mut self, path: &Path) {
self.structs.insert(path.clone());
self.insert(path, Some(DeclarationType::Struct));
}
pub fn add_union(&mut self, path: &Path) {
self.unions.insert(path.clone());
self.insert(path, Some(DeclarationType::Union));
}
pub fn add_none(&mut self, path: &Path) {
self.insert(path, None);
}
pub fn type_for(&self, path: &Path) -> Option<DeclarationType> {
// FIXME: don't look up by name, but by full path:
if self.structs.contains(path) {
Some(DeclarationType::Struct)
} else if self.enums.contains(path) {
Some(DeclarationType::Enum)
} else if self.unions.contains(path) {
Some(DeclarationType::Union)
} else {
None
}
*self.types.get(path)?
}
}
+9 -4
View File
@@ -476,12 +476,17 @@ impl Item for Enum {
}
fn collect_declaration_types(&self, resolver: &mut DeclarationTypeResolver) {
if self.tag.is_some() && self.repr.style == ReprStyle::C {
resolver.add_struct(&self.path);
} else if self.tag.is_some() && self.repr.style != ReprStyle::C {
resolver.add_union(&self.path);
if self.tag.is_some() {
if self.repr.style == ReprStyle::C {
resolver.add_struct(&self.path);
} else {
resolver.add_union(&self.path);
}
} else if self.repr.style == ReprStyle::C {
resolver.add_enum(&self.path);
} else {
// This is important to handle conflicting names with opaque items.
resolver.add_none(&self.path);
}
}
+3 -1
View File
@@ -266,7 +266,9 @@ impl Item for Struct {
}
fn collect_declaration_types(&self, resolver: &mut DeclarationTypeResolver) {
if !self.is_transparent {
if self.is_transparent {
resolver.add_none(&self.path);
} else {
resolver.add_struct(&self.path);
}
}
+4
View File
@@ -138,6 +138,10 @@ impl Item for Typedef {
self.aliased.rename_for_config(config, &self.generic_params);
}
fn collect_declaration_types(&self, resolver: &mut DeclarationTypeResolver) {
resolver.add_none(&self.path);
}
fn resolve_declaration_types(&mut self, resolver: &DeclarationTypeResolver) {
self.aliased.resolve_declaration_types(resolver);
}
+10 -4
View File
@@ -318,10 +318,6 @@ impl Library {
x.collect_declaration_types(&mut resolver);
});
self.opaque_items.for_all_items(|x| {
x.collect_declaration_types(&mut resolver);
});
self.enums.for_all_items(|x| {
x.collect_declaration_types(&mut resolver);
});
@@ -330,6 +326,16 @@ impl Library {
x.collect_declaration_types(&mut resolver);
});
self.typedefs.for_all_items(|x| {
x.collect_declaration_types(&mut resolver);
});
// NOTE: Intentionally last, so that in case there's an opaque type
// which is conflicting with a non-opaque one, the later wins.
self.opaque_items.for_all_items(|x| {
x.collect_declaration_types(&mut resolver);
});
self.enums
.for_all_items_mut(|x| x.resolve_declaration_types(&resolver));
@@ -0,0 +1,16 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
enum BindingType {
Buffer = 0,
NotBuffer = 1,
};
typedef uint32_t BindingType;
typedef struct BindGroupLayoutEntry {
BindingType ty;
} BindGroupLayoutEntry;
void root(struct BindGroupLayoutEntry entry);
@@ -0,0 +1,30 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
enum BindingType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
{
Buffer = 0,
NotBuffer = 1,
};
#ifndef __cplusplus
typedef uint32_t BindingType;
#endif // __cplusplus
typedef struct BindGroupLayoutEntry {
BindingType ty;
} BindGroupLayoutEntry;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(struct BindGroupLayoutEntry entry);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
@@ -0,0 +1,16 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
enum BindingType {
Buffer = 0,
NotBuffer = 1,
};
typedef uint32_t BindingType;
typedef struct {
BindingType ty;
} BindGroupLayoutEntry;
void root(BindGroupLayoutEntry entry);
@@ -0,0 +1,30 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
enum BindingType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
{
Buffer = 0,
NotBuffer = 1,
};
#ifndef __cplusplus
typedef uint32_t BindingType;
#endif // __cplusplus
typedef struct {
BindingType ty;
} BindGroupLayoutEntry;
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(BindGroupLayoutEntry entry);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
@@ -0,0 +1,20 @@
#include <cstdarg>
#include <cstdint>
#include <cstdlib>
#include <ostream>
#include <new>
enum class BindingType : uint32_t {
Buffer = 0,
NotBuffer = 1,
};
struct BindGroupLayoutEntry {
BindingType ty;
};
extern "C" {
void root(BindGroupLayoutEntry entry);
} // extern "C"
@@ -0,0 +1,17 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list
cdef extern from *:
cdef enum:
Buffer # = 0,
NotBuffer # = 1,
ctypedef uint32_t BindingType;
ctypedef struct BindGroupLayoutEntry:
BindingType ty;
void root(BindGroupLayoutEntry entry);
@@ -0,0 +1,16 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
enum BindingType {
Buffer = 0,
NotBuffer = 1,
};
typedef uint32_t BindingType;
struct BindGroupLayoutEntry {
BindingType ty;
};
void root(struct BindGroupLayoutEntry entry);
@@ -0,0 +1,30 @@
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
enum BindingType
#ifdef __cplusplus
: uint32_t
#endif // __cplusplus
{
Buffer = 0,
NotBuffer = 1,
};
#ifndef __cplusplus
typedef uint32_t BindingType;
#endif // __cplusplus
struct BindGroupLayoutEntry {
BindingType ty;
};
#ifdef __cplusplus
extern "C" {
#endif // __cplusplus
void root(struct BindGroupLayoutEntry entry);
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus
@@ -0,0 +1,17 @@
from libc.stdint cimport int8_t, int16_t, int32_t, int64_t, intptr_t
from libc.stdint cimport uint8_t, uint16_t, uint32_t, uint64_t, uintptr_t
cdef extern from *:
ctypedef bint bool
ctypedef struct va_list
cdef extern from *:
cdef enum:
Buffer # = 0,
NotBuffer # = 1,
ctypedef uint32_t BindingType;
cdef struct BindGroupLayoutEntry:
BindingType ty;
void root(BindGroupLayoutEntry entry);
+14
View File
@@ -0,0 +1,14 @@
mod uhoh {
enum BindingType { Buffer, NotBuffer }
}
#[repr(u32)]
pub enum BindingType { Buffer = 0, NotBuffer = 1 }
#[repr(C)]
pub struct BindGroupLayoutEntry {
pub ty: BindingType, // This is the repr(u32) enum
}
#[no_mangle]
pub extern "C" fn root(entry: BindGroupLayoutEntry) {}