-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathgenerate.odin
521 lines (445 loc) · 12.3 KB
/
generate.odin
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
package odin_win32_generator
import "core:os"
import "core:io"
import "core:encoding/json"
import "core:fmt"
import "core:path/filepath"
import "core:strings"
import "core:thread"
import sync "core:sync/sync2"
Api_Type_Name_To_Api_Ref_Map :: struct {
top_level: map[string]map[Api_Ref]bool,
nested: map[string]map[Api_Ref]bool,
}
File :: struct {
pkg: ^Pkg,
full_path: string,
base_path: string,
api: string,
name: string,
data: []byte,
root: json.Value,
type_map: Api_Type_Name_To_Api_Ref_Map,
}
Api_Ref :: struct {
api: string,
name: string,
}
Pkg :: struct {
name: string,
files: [dynamic]^File,
deps: map[^Pkg]bool,
}
Generator :: struct {
files: [dynamic]^File,
file_map: map[string]^File,
pkgs: map[string]^Pkg, // Key: package name
api_type_map: map[string]^Api_Type_Name_To_Api_Ref_Map,
full_type_set: map[Api_Ref]bool,
api_cycle_type_set: map[Api_Ref][]Api_Ref,
api_to_file_map: map[string]^File,
api_to_pkg_map: map[string]^Pkg,
cycle_types: map[Api_Ref]json.Value,
type_in_pkg_types: map[string]bool,
apis_not_needed_for_types: map[string]bool,
type_names_to_remove: map[Api_Ref]bool,
procedures_to_ignore: map[Api_Ref]bool,
}
trim_at_first_dot :: proc(name: string) -> string {
n := strings.index_byte(name, '.');
if n < 0 {
n = len(name);
}
return name[:n];
}
get_api_name_from_file_name :: proc(name: string, allocator := context.allocator) -> string {
n := strings.last_index_byte(name, '.');
if n < 0 {
n = len(name);
}
return name[:n];
}
get_package_name_from_file_name :: proc(name: string, allocator := context.allocator) -> string {
n := strings.index_byte(name, '.');
if n < 0 {
n = len(name);
}
return strings.to_snake_case(name[:n], allocator);
}
is_anon_type :: proc(type_name: string) -> bool {
return strings.has_suffix(type_name, "_e__Struct") || strings.has_suffix(type_name, "_e__Union");
}
get_api_ref_top_level_type :: proc(type_obj: json.Object) -> string {
parents := type_obj["Parents"];
if parents != nil {
if array, ok := parents.(json.Array); ok && len(array) != 0 {
return array[0].(string);
}
}
return type_obj["Name"].(string);
}
get_json_api_refs :: proc(api_refs: ^map[Api_Ref]bool, json_obj: json.Value) {
switch v in json_obj {
case json.Object:
if name, _ := v["Kind"].(string); name == "ApiRef" {
key := Api_Ref{
api = v["Api"].(string),
name = get_api_ref_top_level_type(v),
};
api_refs[key] = true;
return;
}
for _, entry in v {
get_json_api_refs(api_refs, entry);
}
case json.Array:
for entry in v {
get_json_api_refs(api_refs, entry);
}
case json.Null, string, i64, f64, bool:
// ignore
}
}
get_nested_name :: proc(type_name: string, ref: Api_Ref) -> string {
type_names := strings.split(type_name, ".", context.temp_allocator);
ref_names := strings.split(ref.name, ".", context.temp_allocator);
i := 0;
for i < len(ref_names) && i < len(type_names) {
if ref_names[i] != type_names[len(type_names)-i-1] {
break;
}
i += 1;
}
b: [dynamic]byte;
j := 0;
for n in type_names {
if j > 0 {
append(&b, '.');
}
append(&b, n);
j += 1;
}
for n in ref_names[i:] {
if j > 0 {
append(&b, '.');
}
append(&b, n);
j += 1;
}
return string(b[:]);
}
apis_to_ignore := []string{
"AI.",
"Data.",
"Web.",
"System.Diagnostics.Debug",
};
do_ignore_api :: proc(api: string) -> bool {
for i in apis_to_ignore {
if strings.has_prefix(api, i) {
return true;
}
}
return false;
}
functions_to_ignore := []Api_Ref{
{"System.TpmBaseServices", "GetDeviceID"},
};
calculate_type_graph :: proc(g: ^Generator) {
Type_Node :: struct {
preds: map[Api_Ref]bool,
succs: map[Api_Ref]bool,
};
Type_Graph :: map[Api_Ref]^Type_Node;
graph: Type_Graph;
defer delete(graph);
for file in g.files {
table := &file.type_map;
for type_name, refs in table.top_level {
type_ref := Api_Ref{file.api, type_name};
node := new(Type_Node);
node.succs = refs;
graph[type_ref] = node;
}
for type_name, refs in table.nested {
type_ref := Api_Ref{file.api, type_name};
node := new(Type_Node);
node.succs = refs;
graph[type_ref] = node;
}
}
for file in g.files {
table := &file.type_map;
for type_name, refs in table.top_level {
type_ref := Api_Ref{file.api, type_name};
node := graph[type_ref];
assert(node != nil);
for succ in node.succs {
succ_node := graph[succ];
if succ_node != nil {
fmt.assertf(succ_node != nil, "%v", succ);
succ_node.preds[type_ref] = true;
}
}
}
for type_name, refs in table.nested {
type_ref := Api_Ref{file.api, type_name};
node := graph[type_ref];
assert(node != nil);
for succ in node.succs {
succ_node := graph[succ];
if succ_node != nil {
fmt.assertf(succ_node != nil, "%v", succ);
succ_node.preds[type_ref] = true;
}
}
}
}
for type_ref, node in graph {
if len(node.preds) == 0 {
g.type_names_to_remove[type_ref] = true;
}
}
fmt.println("TYPE GRAPH DONE");
}
main :: proc() {
defer fmt.println("[Done]");
g := &Generator{};
filepaths, err := filepath.glob("win32json/api/*.json");
assert(err == nil);
total_file_data_size := 0;
fmt.println("[Collect Files]");
// Collect file
for path in filepaths {
filename := filepath.base(path);
base_path := filename;
filename = filename[:strings.last_index_byte(filename, '.')];
if n := strings.index_byte(filename, '.'); n >= 0 {
filename = filename[n+1:];
}
filename, _ = strings.replace_all(filename, ".", "_");
filename = strings.to_snake_case(filename);
if do_ignore_api(base_path) {
continue;
}
data, ok := os.read_entire_file(path);
assert(ok);
total_file_data_size += len(data);
append(&g.files, new_clone(File{
full_path = path,
base_path = base_path,
name = filename,
data = data,
}));
}
// Sort files into packages
for file in g.files {
pkg_name := get_package_name_from_file_name(file.base_path, context.temp_allocator);
pkg, pkg_exists := g.pkgs[pkg_name];
if !pkg_exists {
pkg = new(Pkg);
pkg.name = pkg_name;
g.pkgs[strings.clone(pkg_name)] = pkg;
}
file.pkg = pkg;
append(&pkg.files, file);
file.api = get_api_name_from_file_name(file.base_path);
g.api_type_map[file.api] = &file.type_map;
g.api_to_file_map[file.api] = file;
g.api_to_pkg_map[file.api] = pkg;
}
fmt.println("[Parse Files]");
@static wg: sync.Wait_Group;
// Thread pool? Who needs that? ;)
sync.wait_group_add(&wg, len(g.files));
for file in g.files {
thread.run_with_poly_data(file, proc(file: ^File) {
root, err := json.parse(file.data, .JSON, /*parse integers*/true);
if err != nil {
fmt.eprintf("Error parsing file %s: %v\n", file.full_path, err);
os.exit(1);
}
file.root = root;
sync.wait_group_done(&wg);
});
}
sync.wait_group_wait(&wg);
fmt.println("[Loading Types]");
for file in g.files {
add_type_refs :: proc(type_map: ^Api_Type_Name_To_Api_Ref_Map, name_prefix: string, type_json: json.Value) {
type_obj := type_json.(json.Object);
kind := type_obj["Kind"].(string);
name := type_obj["Name"].(string);
full_name := strings.concatenate({name_prefix, name});
api_refs: map[Api_Ref]bool;
switch kind {
case "Struct", "Union":
get_json_api_refs(&api_refs, type_obj["Fields"]);
nested_types, _ := type_obj["NestedTypes"].(json.Array);
for nested_type in nested_types {
add_type_refs(type_map, strings.concatenate({full_name, "."}), nested_type);
}
case "Enum", "ComClassID":
get_json_api_refs(&api_refs, type_json);
assert(len(api_refs) == 0);
case "Com", "FunctionPointer", "NativeTypedef":
get_json_api_refs(&api_refs, type_json);
case:
panic(kind);
}
table: ^map[string]map[Api_Ref]bool;
table = &type_map.top_level;
if len(name_prefix) != 0 {
table = &type_map.nested;
}
if full_name in table^ {
t := &table[full_name];
for k, v in api_refs {
t[k] = v;
}
} else {
table[full_name] = api_refs;
}
}
root := file.root.(json.Object);
types := root["Types"].(json.Array);
for type in types {
add_type_refs(&file.type_map, "", type);
}
}
fmt.println("[Check Type Refs Exist]");
for file in g.files {
i := 0;
for type_name, refs in file.type_map.top_level {
i += 1;
if len(refs) == 0 {
continue;
}
for ref in refs {
if do_ignore_api(ref.api) {
// fmt.println(file.api, type_name);
g.type_names_to_remove[{file.api, type_name}] = true;
continue;
}
if !is_anon_type(ref.name) {
table := g.api_type_map[ref.api];
fmt.assertf(table != nil, "%s needed by %s", ref.api, type_name);
if ref.name not_in table.top_level {
nested_name := get_nested_name(type_name, ref);
defer delete(nested_name);
assert(nested_name in table.nested, "nested_name not found");
}
}
}
}
}
fmt.println("[Calculate Entity Count]");
total_const_count := 0;
total_type_count := 0;
total_proc_count := 0;
for file in g.files {
root := file.root.(json.Object);
constants, _ := root["Constants"].(json.Array);
types , _ := root["Types"].(json.Array);
functions, _ := root["Functions"].(json.Array);
total_const_count += len(constants);
total_type_count += len(types);
total_proc_count += len(functions);
}
fmt.println("[Constant Max Count]", total_const_count);
fmt.println("[Type Max Count]", total_type_count);
fmt.println("[Procedure Max Count]", total_proc_count);
fmt.println("[Calculate Type References]");
api_recursive_type_refs_table_pkg: map[^Pkg]map[string][dynamic][]Api_Ref;
api_recursive_type_refs_table_api: map[string]map[string][dynamic][]Api_Ref;
for file in g.files {
get_recursive_chains :: proc(
g: ^Generator,
table: ^Api_Type_Name_To_Api_Ref_Map,
handled: ^map[Api_Ref]bool,
refs: map[Api_Ref]bool,
result: ^[dynamic][]Api_Ref,
base_chain: []Api_Ref,
) {
for ref in refs {
table := g.api_type_map[ref.api];
if table == nil {
continue;
}
if ref in g.type_names_to_remove {
continue;
}
if !is_anon_type(ref.name) && ref.name in table.top_level {
next_chain := make([]Api_Ref, len(base_chain)+1);
copy(next_chain, base_chain);
next_chain[len(base_chain)] = ref;
ref_refs := table.top_level[ref.name];
if len(ref_refs) == 0 || ref in handled^ {
append(result, next_chain);
} else {
handled[ref] = true;
get_recursive_chains(g, table, handled, ref_refs, result, next_chain);
}
}
}
}
handled: map[Api_Ref]bool;
defer delete(handled);
recursive_type_refs_table: map[string][dynamic][]Api_Ref;
table := &file.type_map;
for type_name, refs in table.top_level {
if (Api_Ref{file.api, type_name} in g.type_names_to_remove) {
continue;
}
clear(&handled);
recursive_chains: [dynamic][]Api_Ref;
get_recursive_chains(g, table, &handled, refs, &recursive_chains, nil);
recursive_type_refs_table[type_name] = recursive_chains;
}
if file.pkg not_in api_recursive_type_refs_table_pkg {
api_recursive_type_refs_table_pkg[file.pkg] = make(map[string][dynamic][]Api_Ref);
}
prev, _ := &api_recursive_type_refs_table_pkg[file.pkg];
for k, v in recursive_type_refs_table {
prev[k] = v;
}
api_recursive_type_refs_table_api[file.api] = recursive_type_refs_table;
}
fmt.println("[Calculate Package Dependency Graph]");
for pkg, m in api_recursive_type_refs_table_pkg {
for type_name, refs_da in m do for refs in refs_da {
for ref in refs {
other_pkg := g.api_to_pkg_map[ref.api];
if other_pkg != nil && other_pkg != pkg {
pkg.deps[other_pkg] = true;
}
}
}
}
fmt.println("[Calculate Procedure References]");
for file in g.files {
file_obj := file.root.(json.Object);
functions, _ := file_obj["Functions"].(json.Array);
func_loop: for func in functions {
obj := func.(json.Object);
name := obj["Name"].(string);
proc_ref := Api_Ref{file.api, name};
refs: map[Api_Ref]bool;
defer delete(refs);
get_json_api_refs(&refs, obj["ReturnType"]);
get_json_api_refs(&refs, obj["Params"]);
for ref in refs {
if do_ignore_api(ref.api) || ref in g.type_names_to_remove {
// fmt.printf("\tIgnoring Procedure: %s %s\n", proc_ref.api, proc_ref.name);
g.procedures_to_ignore[proc_ref] = true;
continue func_loop;
}
}
}
}
for proc_ref in functions_to_ignore {
g.procedures_to_ignore[proc_ref] = true;
}
// fmt.println("[Writing Package Files]");
// write_to_odin(g);
}