forked from mirror/vulkan-zig
Restructure spec-c-parse to registry/c-parse and spec-parse to registry/parse
This commit is contained in:
@@ -1,803 +1,116 @@
|
||||
const std = @import("std");
|
||||
const xml = @import("xml.zig");
|
||||
const mem = std.mem;
|
||||
const Allocator = mem.Allocator;
|
||||
const SegmentedList = std.SegmentedList;
|
||||
const StringHashMap = std.StringHashMap;
|
||||
|
||||
pub const Registry = struct {
|
||||
arena: std.heap.ArenaAllocator,
|
||||
|
||||
declarations: SegmentedList(Declaration, 0),
|
||||
declarations_by_name: StringHashMap(*Declaration),
|
||||
api_constants: SegmentedList(ApiConstant, 0),
|
||||
tags: SegmentedList(TagInfo, 0),
|
||||
extensions: SegmentedList(ExtensionInfo, 0),
|
||||
|
||||
fn init(allocator: *Allocator) !*Registry {
|
||||
// Use this construction to make sure that the extensions list contains a valid pointer to an allocator
|
||||
const registry = blk: {
|
||||
var arena = std.heap.ArenaAllocator.init(allocator);
|
||||
errdefer arena.deinit();
|
||||
|
||||
const registry = try arena.allocator.create(Registry);
|
||||
registry.* = .{
|
||||
.arena = arena,
|
||||
.declarations = undefined,
|
||||
.declarations_by_name = StringHashMap(*Declaration).init(allocator),
|
||||
.api_constants = undefined,
|
||||
.tags = undefined,
|
||||
.extensions = undefined
|
||||
};
|
||||
|
||||
break :blk registry;
|
||||
};
|
||||
|
||||
registry.declarations = SegmentedList(Declaration, 0).init(®istry.arena.allocator);
|
||||
registry.api_constants = SegmentedList(ApiConstant, 0).init(®istry.arena.allocator);
|
||||
registry.tags = SegmentedList(TagInfo, 0).init(®istry.arena.allocator);
|
||||
registry.extensions = SegmentedList(ExtensionInfo, 0).init(®istry.arena.allocator);
|
||||
|
||||
return registry;
|
||||
}
|
||||
|
||||
pub fn fromXml(allocator: *Allocator, root: *xml.Element) *Registry {
|
||||
std.debug.assert(mem.eql(u8, root.tag, "registry"));
|
||||
|
||||
var registry = Registry.init(allocator) catch unreachable;
|
||||
|
||||
processTypes(registry, root);
|
||||
processEnums(registry, root);
|
||||
processCommands(registry, root);
|
||||
processFeatures(registry, root);
|
||||
processTags(registry, root);
|
||||
processExtensions(registry, root);
|
||||
|
||||
return registry;
|
||||
}
|
||||
|
||||
pub fn deinit(self: Registry) void {
|
||||
self.declarations_by_name.deinit();
|
||||
|
||||
// Copy to stack so that the arena doesn't destroy itself
|
||||
var arena = self.arena;
|
||||
arena.deinit();
|
||||
}
|
||||
|
||||
fn addDefinition(self: *Registry, name: []const u8, definition: Definition) void {
|
||||
const ptr = self.declarations.addOne() catch unreachable;
|
||||
ptr.* = .{
|
||||
.name = name,
|
||||
.definition = definition
|
||||
};
|
||||
|
||||
if (self.declarations_by_name.put(name, ptr) catch unreachable) |existing| {
|
||||
std.debug.warn("Duplicate definition {}\n", .{existing.key});
|
||||
unreachable;
|
||||
}
|
||||
}
|
||||
|
||||
fn addApiConstant(self: *Registry, name: []const u8, expr: []const u8) void {
|
||||
self.api_constants.push(.{.name = name, .expr = expr}) catch unreachable;
|
||||
}
|
||||
|
||||
fn addTag(self: *Registry, name: []const u8, author: []const u8) void {
|
||||
self.tags.push(.{.name = name, .author = author}) catch unreachable;
|
||||
}
|
||||
|
||||
pub fn findDefinitionByName(self: *Registry, name: []const u8) ?*Definition {
|
||||
if (self.declarations_by_name.get(name)) |kv| {
|
||||
return &kv.value.definition;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
pub fn dump(self: *Registry) void {
|
||||
const indent = " " ** 4;
|
||||
|
||||
{
|
||||
std.debug.warn("Definitions:\n", .{});
|
||||
var it = self.declarations.iterator(0);
|
||||
while (it.next()) |decl| {
|
||||
std.debug.warn(indent ++ "{} ({})\n", .{decl.name, std.meta.tagName(decl.definition)});
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std.debug.warn("API constants:\n", .{});
|
||||
var it = self.api_constants.iterator(0);
|
||||
while (it.next()) |kv| {
|
||||
std.debug.warn(indent ++ "{} = {}\n", .{kv.name, kv.expr});
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std.debug.warn("Tags:\n", .{});
|
||||
var it = self.tags.iterator(0);
|
||||
while (it.next()) |tag| {
|
||||
std.debug.warn(indent ++ "{} ({})\n", .{tag.name, tag.author});
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
std.debug.warn("Extensions:\n", .{});
|
||||
var it = self.extensions.iterator(0);
|
||||
while (it.next()) |ext| {
|
||||
std.debug.warn(indent ++ "{}: {}, version {}\n", .{ext.number, ext.name, ext.version});
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
pub const ApiConstant = struct {
|
||||
name: []const u8,
|
||||
expr: []const u8
|
||||
};
|
||||
|
||||
pub const ExtensionInfo = struct {
|
||||
name: []const u8,
|
||||
number: u32,
|
||||
version: u32,
|
||||
};
|
||||
|
||||
pub const TagInfo = struct {
|
||||
name: []const u8,
|
||||
author: []const u8
|
||||
decls: []Declaration,
|
||||
api_constants: []ApiConstant,
|
||||
tags: []Tag,
|
||||
};
|
||||
|
||||
pub const Declaration = struct {
|
||||
name: []const u8,
|
||||
definition: Definition
|
||||
decl_type: TypeInfo,
|
||||
};
|
||||
|
||||
pub const Definition = union(enum) {
|
||||
Struct: ContainerInfo,
|
||||
Union: ContainerInfo,
|
||||
Enum: EnumInfo,
|
||||
Bitmask: BitmaskInfo,
|
||||
Handle: HandleInfo,
|
||||
FnPtr: CommandInfo,
|
||||
Command: CommandInfo,
|
||||
Alias: []const u8,
|
||||
BaseType: TypeInfo
|
||||
};
|
||||
|
||||
pub const HandleInfo = struct {
|
||||
dispatchable: bool
|
||||
};
|
||||
|
||||
pub const BitmaskInfo = struct {
|
||||
bits_enum: ?[]const u8
|
||||
};
|
||||
|
||||
// Type info of fields, function parameters, and return types.
|
||||
pub const TypeInfo = struct {
|
||||
const PointerSize = enum {
|
||||
One,
|
||||
Many, // The length is given by some expression which cannot be expressed in Zig
|
||||
ZeroTerminated
|
||||
};
|
||||
|
||||
const Pointer = struct {
|
||||
is_const: bool,
|
||||
size: PointerSize
|
||||
pub const ApiConstant = struct {
|
||||
pub const Value = union(enum) {
|
||||
expr: []const u8,
|
||||
alias: []const u8, // Alias of another API constant
|
||||
};
|
||||
|
||||
name: []const u8,
|
||||
pointers: []Pointer, // Outer-most pointer is the first element
|
||||
array_size: ?[]const u8,
|
||||
|
||||
fn fromXml(allocator: *Allocator, elem: *xml.Element) TypeInfo {
|
||||
var type_info = TypeInfo{
|
||||
.name = elem.getCharData("type").?,
|
||||
.pointers = &[_]Pointer{},
|
||||
.array_size = elem.getCharData("enum")
|
||||
};
|
||||
|
||||
// Find the element which contains the stars of the pointers
|
||||
var stars: ?[]const u8 = null;
|
||||
var child_it = elem.children.iterator(0);
|
||||
while (child_it.next()) |child| {
|
||||
if (child.* == .CharData and mem.indexOfScalar(u8, child.CharData, '*') != null) {
|
||||
stars = child.CharData;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (stars) |ptr_text| {
|
||||
const npointers = count(ptr_text, '*');
|
||||
|
||||
type_info.pointers = allocator.alloc(TypeInfo.Pointer, npointers) catch unreachable;
|
||||
|
||||
// Read the sizes of each pointer
|
||||
if (elem.getAttribute("len")) |lens| {
|
||||
var len_it = std.mem.split(lens, ",");
|
||||
for (type_info.pointers) |*ptr, i| {
|
||||
ptr.size = if (len_it.next()) |len| lenToPointerSize(len) else .One;
|
||||
}
|
||||
} else {
|
||||
for (type_info.pointers) |*ptr| {
|
||||
ptr.size = .One;
|
||||
}
|
||||
}
|
||||
|
||||
const pre = switch (elem.children.at(0).*) {
|
||||
.CharData => |char_data| char_data,
|
||||
else => ""
|
||||
};
|
||||
|
||||
type_info.parseConstness(pre, ptr_text);
|
||||
}
|
||||
|
||||
return type_info;
|
||||
}
|
||||
|
||||
fn fromFnPtrReturnTypeXml(allocator: *Allocator, elem: *xml.Element) TypeInfo {
|
||||
// In function pointers, the return type is not contained within a designated tag.
|
||||
// The first chardata of the type has the following structure: 'typedef <return type> (VKAPI_PTR *'
|
||||
// In order to parse the <return type>, strip everything from it until the last star and take the last word
|
||||
// to be the type. Then parse everything in front of the return type word and after the return type word
|
||||
const proto = elem.children.at(0).CharData;
|
||||
std.debug.assert(mem.startsWith(u8, proto, "typedef ") and mem.endsWith(u8, proto, " (VKAPI_PTR *"));
|
||||
const return_type = proto["typedef ".len .. proto.len - " (VKAPI_PTR *".len];
|
||||
|
||||
var first_star = return_type.len;
|
||||
var npointers: usize = 0;
|
||||
var i = return_type.len;
|
||||
while (i > 0) {
|
||||
i -= 1;
|
||||
if (return_type[i] == '*') {
|
||||
first_star = i;
|
||||
npointers += 1;
|
||||
}
|
||||
}
|
||||
|
||||
const name_start = if (mem.lastIndexOfScalar(u8, return_type[0 .. first_star], ' ')) |index| index + 1 else 0;
|
||||
var type_info = TypeInfo{
|
||||
.name = return_type[name_start .. first_star],
|
||||
.pointers = &[_]Pointer{},
|
||||
.array_size = null
|
||||
};
|
||||
|
||||
if (npointers > 0) {
|
||||
type_info.pointers = allocator.alloc(TypeInfo.Pointer, npointers) catch unreachable;
|
||||
for (type_info.pointers) |*ptr| {
|
||||
ptr.size = .One;
|
||||
}
|
||||
|
||||
type_info.parseConstness(return_type[0 .. name_start], return_type[first_star ..]);
|
||||
}
|
||||
|
||||
return type_info;
|
||||
}
|
||||
|
||||
fn fromFnPtrParamTypeXml(allocator: *Allocator, pre: []const u8, name: []const u8, post: []const u8) TypeInfo {
|
||||
// `pre` and `post` contain information that is shared with other types, seperated by commas. In
|
||||
// the case of `pre`, get everything after the comma (if present), and for `post`, everything before
|
||||
// and including the last star before the last. If there is no star, the segment contains no
|
||||
// useful information anyway. Note that the star should never appear *after* the comma (that wouldn't be
|
||||
// a valid C type).
|
||||
// Ex: <type>void</type>* pUserData, <type>void</type>* pMemory
|
||||
|
||||
const pre_start = if (mem.indexOfScalar(u8, pre, ',')) |index| index + 1 else pre.len;
|
||||
const post_end = if (mem.indexOfScalar(u8, post, '*')) |index| index + 1 else 0;
|
||||
const npointers = count(post[0 .. post_end], '*');
|
||||
|
||||
var type_info = TypeInfo{
|
||||
.name = name,
|
||||
.pointers = &[_]Pointer{},
|
||||
.array_size = null
|
||||
};
|
||||
|
||||
if (npointers > 0) {
|
||||
type_info.pointers = allocator.alloc(TypeInfo.Pointer, npointers) catch unreachable;
|
||||
for (type_info.pointers) |*ptr| {
|
||||
ptr.size = .One;
|
||||
}
|
||||
|
||||
type_info.parseConstness(pre[pre_start ..], post[0 .. post_end]);
|
||||
}
|
||||
|
||||
return type_info;
|
||||
}
|
||||
|
||||
fn parseConstness(self: *TypeInfo, pre: []const u8, post: []const u8) void {
|
||||
// Beware: the const of the inner pointer is given before the type name (in `pre`)
|
||||
// while the others are in the `post`.
|
||||
|
||||
// Check the outer pointers
|
||||
var const_it = std.mem.split(post, "*");
|
||||
var i: usize = self.pointers.len;
|
||||
while (i > 0) {
|
||||
i -= 1;
|
||||
const is_const = mem.indexOf(u8, const_it.next().?, "const") != null;
|
||||
self.pointers[i].is_const = is_const;
|
||||
}
|
||||
|
||||
// Check the inner-most pointer
|
||||
const first_const = mem.indexOf(u8, pre, "const") != null;
|
||||
self.pointers[self.pointers.len - 1].is_const = first_const;
|
||||
}
|
||||
|
||||
pub fn format(
|
||||
self: TypeInfo,
|
||||
comptime fmt: []const u8,
|
||||
options: std.fmt.FormatOptions,
|
||||
context: var,
|
||||
comptime Errors: type,
|
||||
output: fn (@TypeOf(context), []const u8) Errors!void
|
||||
) Errors!void {
|
||||
if (self.array_size) |array_size| {
|
||||
try std.fmt.format(context, Errors, output, "[{}]", .{array_size});
|
||||
}
|
||||
|
||||
for (self.pointers) |ptr| {
|
||||
switch (ptr.size) {
|
||||
.One => try output(context, "*"),
|
||||
.Many => try output(context, "[*]"),
|
||||
.ZeroTerminated => try output(context, "[*:0]")
|
||||
}
|
||||
|
||||
if (ptr.is_const) {
|
||||
try output(context, "const ");
|
||||
}
|
||||
}
|
||||
|
||||
if (self.name) |name| {
|
||||
try output(context, self.name);
|
||||
} else {
|
||||
try output(context, "@Type(.Opaque)");
|
||||
}
|
||||
}
|
||||
|
||||
fn lenToPointerSize(len: []const u8) PointerSize {
|
||||
if (mem.eql(u8, len, "null-terminated")) {
|
||||
return .ZeroTerminated;
|
||||
} else if (mem.eql(u8, len, "1")) {
|
||||
return .One;
|
||||
} else {
|
||||
return .Many;
|
||||
}
|
||||
}
|
||||
value: Value,
|
||||
};
|
||||
|
||||
pub const ContainerInfo = struct {
|
||||
const Member = struct {
|
||||
name: []const u8,
|
||||
type_info: TypeInfo
|
||||
};
|
||||
|
||||
members: SegmentedList(Member, 0),
|
||||
|
||||
fn init(allocator: *Allocator) ContainerInfo {
|
||||
return .{
|
||||
.members = SegmentedList(Member, 0).init(allocator)
|
||||
};
|
||||
}
|
||||
|
||||
fn fromXml(allocator: *Allocator, elem: *xml.Element) ContainerInfo {
|
||||
var s = ContainerInfo.init(allocator);
|
||||
|
||||
var members = elem.findChildrenByTag("member");
|
||||
while (members.next()) |member| {
|
||||
const member_name = member.getCharData("name").?;
|
||||
const type_info = TypeInfo.fromXml(allocator, member);
|
||||
|
||||
s.addMember(member_name, type_info);
|
||||
}
|
||||
|
||||
return s;
|
||||
}
|
||||
|
||||
fn addMember(self: *ContainerInfo, name: []const u8, type_info: TypeInfo) void {
|
||||
self.members.push(.{.name = name, .type_info = type_info}) catch unreachable;
|
||||
}
|
||||
pub const Tag = struct {
|
||||
name: []const u8,
|
||||
author: []const u8,
|
||||
};
|
||||
|
||||
pub const CommandInfo = struct {
|
||||
const Parameter = struct {
|
||||
pub const TypeInfo = union(enum) {
|
||||
container: Container,
|
||||
enumeration: Enum,
|
||||
bitmask: Bitmask,
|
||||
handle: Handle,
|
||||
command: Command,
|
||||
alias: []const u8, // Alias of another declaration
|
||||
pointer: Pointer,
|
||||
array: Array,
|
||||
opaque: void,
|
||||
foreign: Foreign
|
||||
};
|
||||
|
||||
pub const Container = struct {
|
||||
pub const Field = struct {
|
||||
name: []const u8,
|
||||
type_info: TypeInfo
|
||||
field_type: TypeInfo,
|
||||
bits: ?usize,
|
||||
};
|
||||
|
||||
parameters: SegmentedList(Parameter, 0),
|
||||
return_type_info: TypeInfo,
|
||||
fields: []Field,
|
||||
is_union: bool,
|
||||
};
|
||||
|
||||
pub const Enum = struct {
|
||||
pub const Value = union(enum) {
|
||||
bitpos: u5, // 1 << bitpos
|
||||
bit_vector: i32, // Combined flags & some vendor IDs
|
||||
int: i32,
|
||||
alias: struct {
|
||||
alias_name: []const u8,
|
||||
is_compat_alias: bool,
|
||||
}
|
||||
};
|
||||
|
||||
pub const Field = struct {
|
||||
name: []const u8,
|
||||
value: Value,
|
||||
};
|
||||
|
||||
fields: []Field,
|
||||
is_bitmask: bool,
|
||||
};
|
||||
|
||||
pub const Bitmask = struct {
|
||||
bits_enum: ?[]const u8,
|
||||
};
|
||||
|
||||
pub const Handle = struct {
|
||||
parent: ?[]const u8, // VkInstance has no parent
|
||||
is_dispatchable: bool,
|
||||
};
|
||||
|
||||
pub const Command = struct {
|
||||
pub const Param = struct {
|
||||
name: []const u8,
|
||||
param_type: TypeInfo,
|
||||
};
|
||||
|
||||
params: []Param,
|
||||
return_type: *TypeInfo,
|
||||
success_codes: []const []const u8,
|
||||
error_codes: []const []const u8,
|
||||
|
||||
fn init(allocator: *Allocator, return_type_info: TypeInfo) CommandInfo {
|
||||
return .{
|
||||
.parameters = SegmentedList(Parameter, 0).init(allocator),
|
||||
.return_type_info = return_type_info,
|
||||
.success_codes = &[_][]u8{},
|
||||
.error_codes = &[_][]u8{},
|
||||
};
|
||||
}
|
||||
|
||||
fn fromXml(allocator: *Allocator, elem: *xml.Element) CommandInfo {
|
||||
const return_type_info = TypeInfo.fromXml(allocator, elem.findChildByTag("proto").?);
|
||||
var cmd = CommandInfo.init(allocator, return_type_info);
|
||||
|
||||
if (elem.getAttribute("successcodes")) |codes| {
|
||||
cmd.success_codes = CommandInfo.splitResultCodes(allocator, codes);
|
||||
}
|
||||
|
||||
if (elem.getAttribute("errorcodes")) |codes| {
|
||||
cmd.error_codes = CommandInfo.splitResultCodes(allocator, codes);
|
||||
}
|
||||
|
||||
var parameters = elem.findChildrenByTag("param");
|
||||
while (parameters.next()) |param| {
|
||||
const param_name = param.getCharData("name").?;
|
||||
const type_info = TypeInfo.fromXml(allocator, param);
|
||||
|
||||
cmd.addParameter(param_name, type_info);
|
||||
}
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
fn fromFnPtrXml(allocator: *Allocator, elem: *xml.Element) CommandInfo {
|
||||
const return_type_info = TypeInfo.fromFnPtrReturnTypeXml(allocator, elem);
|
||||
var cmd = CommandInfo.init(allocator, return_type_info);
|
||||
|
||||
// The parameters of a function pointer are formulated a bit weird, which is why
|
||||
// the chardata surrounding a <type> is also required to parse it completely.
|
||||
// This loop assumes there are no other elements in a function pointers declatation.
|
||||
var i: usize = 3; // The first parameter's type is at offset 3
|
||||
while (i < elem.children.count() - 1) : (i += 2) {
|
||||
const pre = elem.children.at(i - 1).CharData;
|
||||
const type_name = elem.children.at(i).Element.children.at(0).CharData;
|
||||
const post = elem.children.at(i + 1).CharData;
|
||||
const type_info = TypeInfo.fromFnPtrParamTypeXml(allocator, pre, type_name, post);
|
||||
|
||||
// To find the type name, take everything until the first space before the last ) or ,.
|
||||
const name_end = mem.lastIndexOfAny(u8, post, "),").?;
|
||||
const name_start = mem.lastIndexOfScalar(u8, post[0 .. name_end], ' ').? + 1;
|
||||
const name = post[name_start .. name_end];
|
||||
|
||||
cmd.addParameter(name, type_info);
|
||||
}
|
||||
|
||||
return cmd;
|
||||
}
|
||||
|
||||
fn splitResultCodes(allocator: *Allocator, text: []const u8) []const []const u8 {
|
||||
const ncodes = 1 + count(text, ',');
|
||||
const codes = allocator.alloc([]const u8, ncodes) catch unreachable;
|
||||
|
||||
var it = mem.split(text, ",");
|
||||
|
||||
for (codes) |*code, i| {
|
||||
code.* = it.next().?;
|
||||
}
|
||||
|
||||
return codes;
|
||||
}
|
||||
|
||||
fn addParameter(self: *CommandInfo, name: []const u8, type_info: TypeInfo) void {
|
||||
self.parameters.push(.{.name = name, .type_info = type_info}) catch unreachable;
|
||||
}
|
||||
};
|
||||
|
||||
pub const EnumInfo = struct {
|
||||
const Value = union(enum) {
|
||||
Bitpos: u5, //log2(u32)
|
||||
HexValue: i32, // Combined flags and such
|
||||
Value: i32,
|
||||
Alias: []const u8,
|
||||
pub const Pointer = struct {
|
||||
pub const PointerSize = enum {
|
||||
one,
|
||||
many, // The length is given by some expression
|
||||
zero_terminated
|
||||
};
|
||||
|
||||
const Variant = struct {
|
||||
name: []const u8,
|
||||
value: Value
|
||||
};
|
||||
|
||||
variants: SegmentedList(Variant, 0),
|
||||
|
||||
fn init(allocator: *Allocator) EnumInfo {
|
||||
return .{
|
||||
.variants = SegmentedList(Variant, 0).init(allocator)
|
||||
};
|
||||
}
|
||||
|
||||
fn addVariant(self: *EnumInfo, name: []const u8, value: Value) void {
|
||||
// Sometimes a variant is added multiple times with different 'require' parts of an extension
|
||||
// so filter out any duplicates.
|
||||
var it = self.variants.iterator(0);
|
||||
while (it.next()) |variant| {
|
||||
if (mem.eql(u8, variant.name, name)) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
_ = self.variants.push(.{.name = name, .value = value}) catch unreachable;
|
||||
}
|
||||
|
||||
fn processVariantFromXml(self: *EnumInfo, variant: *xml.Element, ext_nr: ?u32) void {
|
||||
if (EnumInfo.isBackwardsCompatAlias(variant)) return;
|
||||
const name = variant.getAttribute("name").?;
|
||||
const value = blk: {
|
||||
// An enum variant's value could be defined by any of the following attributes:
|
||||
// - value: Straight up value of the enum variant, in either base 10 or 16 (prefixed with 0x).
|
||||
// - bitpos: Used for bitmasks, and can also be set in extensions.
|
||||
// - alias: The field is an alias of another variant within the same enum.
|
||||
// - offset: Used with features and extensions, where a non-bitpos value is added to an enum.
|
||||
// The value is given by `1e9 + (extr_nr - 1) * 1e3 + offset`, where `ext_nr` is either
|
||||
// given by the `extnumber` field (in the case of a feature), or given in the parent <extension>
|
||||
// tag. In the latter case its passed via the `ext_nr` parameter.
|
||||
if (variant.getAttribute("value")) |value_str| {
|
||||
if (mem.startsWith(u8, value_str, "0x")) {
|
||||
break :blk Value{.HexValue = std.fmt.parseInt(i32, value_str[2..], 16) catch unreachable};
|
||||
} else {
|
||||
break :blk Value{.Value = std.fmt.parseInt(i32, value_str, 10) catch unreachable};
|
||||
}
|
||||
} else if (variant.getAttribute("bitpos")) |bitpos_str| {
|
||||
break :blk Value{.Bitpos = std.fmt.parseInt(u5, bitpos_str, 10) catch unreachable};
|
||||
} else if (variant.getAttribute("alias")) |alias| {
|
||||
break :blk Value{.Alias = alias};
|
||||
} else if (variant.getAttribute("offset")) |offset_str| {
|
||||
const offset = std.fmt.parseInt(u32, offset_str, 10) catch unreachable;
|
||||
|
||||
const actual_ext_nr = ext_nr orelse blk: {
|
||||
const ext_nr_str = variant.getAttribute("extnumber").?;
|
||||
break :blk std.fmt.parseInt(u32, ext_nr_str, 10) catch unreachable;
|
||||
};
|
||||
|
||||
const abs_value = EnumInfo.extensionEnumInfoValue(actual_ext_nr, offset);
|
||||
const value = if (variant.getAttribute("dir")) |_| -@intCast(i32, abs_value) else @intCast(i32, abs_value);
|
||||
|
||||
break :blk Value{.Value = value};
|
||||
} else {
|
||||
unreachable;
|
||||
}
|
||||
};
|
||||
|
||||
self.addVariant(name, value);
|
||||
}
|
||||
|
||||
fn isBackwardsCompatAlias(variant: *xml.Element) bool {
|
||||
if (variant.getAttribute("comment")) |comment| {
|
||||
return mem.eql(u8, comment, "Backwards-compatible alias containing a typo") or
|
||||
mem.eql(u8, comment, "Deprecated name for backwards compatibility");
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
fn extensionEnumInfoValue(ext_nr: u32, offset: u32) u32 {
|
||||
const extension_value_base = 1000000000;
|
||||
const extension_block = 1000;
|
||||
return extension_value_base + (ext_nr - 1) * extension_block + offset;
|
||||
}
|
||||
is_const: bool,
|
||||
size: PointerSize,
|
||||
child: *TypeInfo,
|
||||
};
|
||||
|
||||
fn processTypes(registry: *Registry, root: *xml.Element) void {
|
||||
var types = root.findChildByTag("types").?;
|
||||
var it = types.findChildrenByTag("type");
|
||||
while (it.next()) |ty| {
|
||||
const category = ty.getAttribute("category") orelse continue;
|
||||
if (mem.eql(u8, category, "bitmask")) {
|
||||
processBitmaskType(registry, ty);
|
||||
} else if (mem.eql(u8, category, "enum")) {
|
||||
processEnumType(registry, ty);
|
||||
} else if (mem.eql(u8, category, "handle")) {
|
||||
processHandleType(registry, ty);
|
||||
} else if (mem.eql(u8, category, "struct") or mem.eql(u8, category, "union")) {
|
||||
processStructType(registry, ty);
|
||||
} else if (mem.eql(u8, category, "funcpointer")) {
|
||||
processFuncPointerType(registry, ty);
|
||||
} else if (mem.eql(u8, category, "basetype")) {
|
||||
processBaseType(registry, ty);
|
||||
} else if (mem.eql(u8, category, "define")) {
|
||||
processDefineType(registry, ty);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn processBitmaskType(registry: *Registry, ty: *xml.Element) void {
|
||||
if (ty.getAttribute("name")) |name| {
|
||||
const alias = ty.getAttribute("alias").?;
|
||||
registry.addDefinition(name, .{.Alias = alias});
|
||||
} else {
|
||||
const name = ty.getCharData("name").?;
|
||||
const info = BitmaskInfo {
|
||||
.bits_enum = ty.getAttribute("requires")
|
||||
};
|
||||
|
||||
registry.addDefinition(name, .{.Bitmask = info});
|
||||
}
|
||||
}
|
||||
|
||||
fn processHandleType(registry: *Registry, ty: *xml.Element) void {
|
||||
if (ty.getAttribute("alias")) |alias| {
|
||||
const name = ty.getAttribute("name").?;
|
||||
registry.addDefinition(name, .{.Alias = alias});
|
||||
} else {
|
||||
const define_type_str = ty.getCharData("type").?;
|
||||
const name = ty.getCharData("name").?;
|
||||
const info = HandleInfo {
|
||||
.dispatchable = std.mem.eql(u8, define_type_str, "VK_DEFINE_HANDLE")
|
||||
};
|
||||
|
||||
registry.addDefinition(name, .{.Handle = info});
|
||||
}
|
||||
}
|
||||
|
||||
fn processEnumType(registry: *Registry, ty: *xml.Element) void {
|
||||
const name = ty.getAttribute("name").?;
|
||||
const def: Definition = if (ty.getAttribute("alias")) |alias|
|
||||
.{.Alias = alias}
|
||||
else
|
||||
.{.Enum = EnumInfo.init(®istry.arena.allocator)};
|
||||
|
||||
registry.addDefinition(name, def);
|
||||
}
|
||||
|
||||
fn processStructType(registry: *Registry, ty: *xml.Element) void {
|
||||
const name = ty.getAttribute("name").?;
|
||||
const def = if (ty.getAttribute("alias")) |alias|
|
||||
Definition{.Alias = alias}
|
||||
else if (mem.eql(u8, ty.getAttribute("category").?, "union"))
|
||||
Definition{.Union = ContainerInfo.fromXml(®istry.arena.allocator, ty)}
|
||||
else
|
||||
Definition{.Struct = ContainerInfo.fromXml(®istry.arena.allocator, ty)};
|
||||
|
||||
registry.addDefinition(name, def);
|
||||
}
|
||||
|
||||
fn processFuncPointerType(registry: *Registry, ty: *xml.Element) void {
|
||||
const name = ty.getCharData("name").?;
|
||||
const cmd = CommandInfo.fromFnPtrXml(®istry.arena.allocator, ty);
|
||||
registry.addDefinition(name, .{.FnPtr = cmd});
|
||||
}
|
||||
|
||||
fn processBaseType(registry: *Registry, ty: *xml.Element) void {
|
||||
const special_names = [_][]const u8{ // handled in render.zig
|
||||
"ANativeWindow",
|
||||
"AHardwareBuffer",
|
||||
"CAMetalLayer"
|
||||
pub const Array = struct {
|
||||
pub const ArraySize = union(enum) {
|
||||
int: usize,
|
||||
alias: []const u8, // Field size is given by an api constant
|
||||
};
|
||||
|
||||
const name = ty.getCharData("name").?;
|
||||
size: ArraySize,
|
||||
child: *TypeInfo,
|
||||
};
|
||||
|
||||
for (special_names) |special| {
|
||||
if (mem.eql(u8, name, special)) return;
|
||||
}
|
||||
|
||||
const type_info = TypeInfo.fromXml(®istry.arena.allocator, ty);
|
||||
registry.addDefinition(name, .{.BaseType = type_info});
|
||||
}
|
||||
|
||||
fn processDefineType(registry: *Registry, ty: *xml.Element) void {
|
||||
if (ty.getCharData("name")) |name| {
|
||||
if (mem.eql(u8, name, "VK_HEADER_VERSION")) {
|
||||
registry.addApiConstant("VK_HEADER_VERSION", mem.trim(u8, ty.children.at(2).CharData, " "));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn processEnums(registry: *Registry, root: *xml.Element) void {
|
||||
var it = root.findChildrenByTag("enums");
|
||||
while (it.next()) |enums| {
|
||||
const name = enums.getAttribute("name").?;
|
||||
if (mem.eql(u8, name, "API Constants")) {
|
||||
processApiConstants(registry, enums);
|
||||
continue;
|
||||
}
|
||||
|
||||
// If the declaration hasn't been inserted in processEnumTypes,
|
||||
// its a bitmask enum that is not used, so ignore it
|
||||
const def = registry.findDefinitionByName(name) orelse continue;
|
||||
var enum_it = enums.findChildrenByTag("enum");
|
||||
while (enum_it.next()) |variant| {
|
||||
def.Enum.processVariantFromXml(variant, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn processApiConstants(registry: *Registry, enums: *xml.Element) void {
|
||||
var it = enums.findChildrenByTag("enum");
|
||||
while (it.next()) |constant| {
|
||||
const name = constant.getAttribute("name").?;
|
||||
const expr = constant.getAttribute("value") orelse constant.getAttribute("alias").?;
|
||||
registry.addApiConstant(name, expr);
|
||||
}
|
||||
}
|
||||
|
||||
fn processCommands(registry: *Registry, root: *xml.Element) void {
|
||||
var commands = root.findChildByTag("commands").?;
|
||||
var command_it = commands.findChildrenByTag("command");
|
||||
while (command_it.next()) |elem| {
|
||||
if (elem.getAttribute("alias")) |alias| {
|
||||
const name = elem.getAttribute("name").?;
|
||||
registry.addDefinition(name, .{.Alias = alias});
|
||||
} else {
|
||||
const name = elem.findChildByTag("proto").?.getCharData("name").?;
|
||||
const command = CommandInfo.fromXml(®istry.arena.allocator, elem);
|
||||
registry.addDefinition(name, .{.Command = command});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn processTags(registry: *Registry, root: *xml.Element) void {
|
||||
var tags = root.findChildByTag("tags").?;
|
||||
var it = tags.findChildrenByTag("tag");
|
||||
while (it.next()) |tag| {
|
||||
const name = tag.getAttribute("name").?;
|
||||
const author = tag.getAttribute("author").?;
|
||||
registry.addTag(name, author);
|
||||
}
|
||||
}
|
||||
|
||||
fn processExtensions(registry: *Registry, root: *xml.Element) void {
|
||||
var extensions = root.findChildByTag("extensions").?;
|
||||
var ext_it = extensions.findChildrenByTag("extension");
|
||||
while (ext_it.next()) |ext| {
|
||||
if (ext.getAttribute("supported")) |support| {
|
||||
if (mem.eql(u8, support, "disabled")) continue;
|
||||
}
|
||||
|
||||
processExtension(registry, ext);
|
||||
}
|
||||
}
|
||||
|
||||
fn processExtension(registry: *Registry, ext: *xml.Element) void {
|
||||
const ext_nr_str = ext.getAttribute("number").?;
|
||||
const ext_nr = std.fmt.parseInt(u32, ext_nr_str, 10) catch unreachable;
|
||||
|
||||
var version: ?u32 = null;
|
||||
|
||||
var req_it = ext.findChildrenByTag("require");
|
||||
while (req_it.next()) |req| {
|
||||
var it = req.findChildrenByTag("enum");
|
||||
while (it.next()) |variant| {
|
||||
if (variant.getAttribute("extends")) |enum_name| {
|
||||
// Some extensions define variants for other extensions,
|
||||
// these are also defined in those extensions, so just skip them
|
||||
if (variant.getAttribute("extnumber")) |_| continue;
|
||||
|
||||
const def = registry.findDefinitionByName(enum_name).?;
|
||||
def.Enum.processVariantFromXml(variant, ext_nr);
|
||||
} else if (variant.getAttribute("name")) |name| {
|
||||
if (mem.endsWith(u8, name, "_SPEC_VERSION")) {
|
||||
const version_str = variant.getAttribute("value").?;
|
||||
version = std.fmt.parseInt(u32, version_str, 10) catch unreachable;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
var ext_info = ExtensionInfo{
|
||||
.name = ext.getAttribute("name").?,
|
||||
.number = ext_nr,
|
||||
.version = version.?
|
||||
};
|
||||
|
||||
registry.extensions.push(ext_info) catch unreachable;
|
||||
}
|
||||
|
||||
fn processFeatures(registry: *Registry, root: *xml.Element) void {
|
||||
var feature_it = root.findChildrenByTag("feature");
|
||||
while (feature_it.next()) |feature| {
|
||||
var req_it = feature.findChildrenByTag("require");
|
||||
while (req_it.next()) |req| {
|
||||
var enum_it = req.findChildrenByTag("enum");
|
||||
while (enum_it.next()) |variant| {
|
||||
const enum_name = variant.getAttribute("extends") orelse continue;
|
||||
const def = registry.findDefinitionByName(enum_name).?;
|
||||
def.Enum.processVariantFromXml(variant, null);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn count(haystack: []const u8, needle: u8) usize {
|
||||
var total: usize = 0;
|
||||
for (haystack) |elem| {
|
||||
if (elem == needle) total += 1;
|
||||
}
|
||||
|
||||
return total;
|
||||
}
|
||||
pub const Foreign = struct {
|
||||
dependency: []const u8, // Either a header or vk_platform
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user