From ba6180e3363d44ac43ec17096115da882ed279fe Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Tue, 9 Jun 2020 17:26:26 +0200 Subject: [PATCH] Basic basetype parsing --- generator/registry-new.zig | 7 ++++- generator/spec-parse.zig | 54 +++++++++++++++++++++++++++++++------- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/generator/registry-new.zig b/generator/registry-new.zig index f95df45..667500a 100644 --- a/generator/registry-new.zig +++ b/generator/registry-new.zig @@ -32,10 +32,11 @@ pub const TypeInfo = union(enum) { Handle: Handle, FnPtr: Command, Command: Command, - Alias: []const u8, + Alias: []const u8, // Alias of another declaration Pointer: Pointer, Array: Array, Opaque: void, + Foreign: Foreign }; pub const Container = struct { @@ -108,3 +109,7 @@ pub const Array = struct { size: ArraySize, child: *TypeInfo, }; + +pub const Foreign = struct { + dependency: []const u8, // Either a header or vk_platform +}; diff --git a/generator/spec-parse.zig b/generator/spec-parse.zig index baf3705..3aea8ba 100644 --- a/generator/spec-parse.zig +++ b/generator/spec-parse.zig @@ -53,17 +53,23 @@ fn parseTypes(allocator: *Allocator, out: []registry.Declaration, types_elem: *x var i: usize = 0; var it = types_elem.findChildrenByTag("type"); while (it.next()) |ty| { - // TODO: Handle foreign types - const category = ty.getAttribute("category") orelse continue; + out[i] = blk: { + const category = ty.getAttribute("category") orelse { + break :blk try parseForeigntype(ty); + }; - // Enums are handled later, in parseEnums. This also has the effect of filtering - // out any enums which have no elements, and should be unused by other parts of the API. - out[i] = if (mem.eql(u8, category, "bitmask")) - try parseBitmaskType(ty) - else if (mem.eql(u8, category, "handle")) - try parseHandleType(ty) - else - continue; + // Enums are handled later, in parseEnums. This also has the effect of filtering + // out any enums which have no elements, and should be unused by other parts of the API. + if (mem.eql(u8, category, "bitmask")) { + break :blk try parseBitmaskType(ty); + } else if (mem.eql(u8, category, "handle")) { + break :blk try parseHandleType(ty); + } else if (mem.eql(u8, category, "basetype")) { + break :blk try parseBaseType(ty); + } + + continue; + }; i += 1; } @@ -71,6 +77,19 @@ fn parseTypes(allocator: *Allocator, out: []registry.Declaration, types_elem: *x return i; } +fn parseForeigntype(ty: *xml.Element) !registry.Declaration { + const name = ty.getAttribute("name") orelse return error.InvalidRegistry; + const dependency = ty.getAttribute("requires") orelse if (mem.eql(u8, name, "int")) + "vk_platform" // for some reason, int doesn't depend on vk_platform (but the other c types do) + else + return error.InvalidRegistry; + + return registry.Declaration{ + .name = name, + .decl_type = .{.Foreign = .{.dependency = dependency}}, + }; +} + fn parseBitmaskType(ty: *xml.Element) !registry.Declaration { if (ty.getAttribute("name")) |name| { const alias = ty.getAttribute("alias") orelse return error.InvalidRegistry; @@ -114,6 +133,21 @@ fn parseHandleType(ty: *xml.Element) !registry.Declaration { } } +fn parseBaseType(ty: *xml.Element) !registry.Declaration { + const name = ty.getCharData("name") orelse return error.InvalidRegistry; + if (ty.getCharData("type")) |alias| { // TODO: Parse as full type? + return registry.Declaration{ + .name = name, + .decl_type = .{.Alias = alias}, + }; + } else { + return registry.Declaration{ + .name = name, + .decl_type = .{.Opaque = {}}, + }; + } +} + fn parseEnums(allocator: *Allocator, out: []registry.Declaration, root: *xml.Element) !usize { var i: usize = 0; var it = root.findChildrenByTag("enums");