-
1
-
2
-
3
-
4
-
5
-
6
-
7
-
8
-
9
-
10
-
11
-
12
-
13
-
14
-
15
-
16
-
17
-
18
-
19
-
20
-
21
-
22
-
23
-
24
-
25
-
26
-
27
-
28
-
29
-
30
-
31
-
32
-
33
-
34
-
35
-
36
-
37
-
38
-
39
-
40
-
41
-
42
-
43
-
44
-
45
-
46
-
47
-
48
-
49
-
50
-
51
-
52
-
53
-
54
-
55
-
56
-
57
-
58
-
59
-
60
-
61
-
62
-
63
-
64
-
65
-
66
-
67
-
68
-
69
-
70
-
71
-
72
-
73
-
74
-
75
-
76
-
77
-
78
-
79
-
80
-
81
-
82
-
83
-
84
-
85
-
86
-
87
-
88
-
89
-
90
-
91
-
92
-
93
-
94
-
95
-
96
-
97
-
98
-
99
-
100
-
101
-
102
-
103
-
104
-
105
-
106
-
107
-
108
-
109
-
110
-
111
-
112
-
113
-
114
-
115
-
116
-
117
-
118
-
119
-
120
-
121
-
122
-
123
-
124
-
125
-
126
-
127
-
128
-
129
-
130
-
131
-
132
-
133
-
134
-
135
-
136
// Copyright 2025 Shota FUJI
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// SPDX-License-Identifier: Apache-2.0
const std = @import("std");
items: []const Item,
pub const CBaseType = union(enum) {
int: u16,
uint: u16,
float: void,
double: void,
char: void,
bool: void,
void: void,
string: void,
custom: []const u8,
pub fn fromZigType(comptime T: type) CBaseType {
return switch (@typeInfo(T)) {
.int => |int| if (int.signedness == .signed) .{
.int = int.bits,
} else .{
.uint = int.bits,
},
.float => |float| switch (float.bits) {
32 => .float,
64 => .double,
else => @compileError(std.fmt.comptimePrint("{d} bits float is not exportable.", .{float.bits})),
},
.bool => .bool,
.@"enum" => .{ .custom = T.cname },
.@"struct" => .{ .custom = T.cname },
.void => .void,
.pointer => |pointer| switch (@typeInfo(pointer.child)) {
.@"struct" => .{ .custom = pointer.child.cname },
else => {
if (pointer.child == u8 and pointer.is_const and pointer.size == .many and std.builtin.Type.Pointer.sentinel(pointer) == 0) {
return .string;
}
@compileError(
std.fmt.comptimePrint("Pointer to non-struct type {s} is not exportable.", .{@typeName(T)}),
);
},
},
else => {
@compileError(
std.fmt.comptimePrint("Type {s} is not exportable.", .{@typeName(T)}),
);
},
};
}
};
pub const CType = union(enum) {
value: CBaseType,
pointer: struct {
type: CBaseType,
@"const": bool,
},
pub fn fromZigType(comptime T: type) CType {
return switch (@typeInfo(T)) {
.pointer => |pointer| {
if (pointer.child == u8 and
pointer.is_const and
pointer.size == .many and
std.builtin.Type.Pointer.sentinel(pointer) == 0)
{
return .{
.value = .string,
};
}
return .{ .pointer = .{
.type = CBaseType.fromZigType(pointer.child),
.@"const" = pointer.is_const,
} };
},
.optional => |optional| fromZigType(optional.child),
else => .{ .value = CBaseType.fromZigType(T) },
};
}
};
pub const EnumField = struct {
name: []const u8,
value: c_int,
};
pub const FunctionParameter = struct {
type: CType,
};
pub const Function = struct {
name: []const u8,
parameters: []const FunctionParameter,
return_type: CType,
};
pub const StructField = struct {
name: []const u8,
type: CType,
};
pub const Item = union(enum) {
@"enum": struct {
cname: []const u8,
fields: []const EnumField,
},
@"struct": struct {
cname: []const u8,
new_function: ?Function = null,
member_functions: []const Function,
fields: []const StructField,
},
@"opaque": struct {
cname: []const u8,
new_function: Function,
member_functions: []const Function,
},
};