FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
unified-runtime/scripts/parse_specs.py at main · oneapi-src/unified-runtime · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
oneapi-src
/
unified-runtime
Public
Notifications
You must be signed in to change notification settings
Fork
120
Star
57
Code
Issues
109
Pull requests
0
Discussions
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Discussions
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
unified-runtime
/
scripts
/
parse_specs.py
Copy path
More file actions
More file actions
Latest commit
History
History
History
1218 lines (1016 loc) · 41.6 KB
Breadcrumbs
unified-runtime
/
scripts
/
parse_specs.py
Copy path
File metadata and controls
1218 lines (1016 loc) · 41.6 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
import
os
import
generate_ids
import
util
import
re
import
hashlib
import
json
import
copy
from
templates
.
helper
import
param_traits
,
type_traits
,
value_traits
import
ctypes
import
itertools
from
typing
import
Dict
,
List
,
Optional
,
Union
from
version
import
Version
default_version
=
Version
(
"0.12"
)
all_versions
=
[
Version
(
ver
)
for
ver
in
[
"0.6"
,
"0.7"
,
"0.8"
,
"0.9"
,
"0.10"
,
"0.11"
,
"0.12"
]
]
def
_preprocess
(
d
):
"""preprocess object"""
if
"enum"
==
d
[
"type"
]:
use_hex
=
False
next
=
0
for
etor
in
d
[
"etors"
]:
if
type_traits
.
is_flags
(
d
[
"name"
]):
etor
[
"name"
]
=
"%s_%s"
%
(
d
[
"name"
][:
-
3
].
upper
(),
etor
[
"name"
])
etor
[
"value"
]
=
etor
.
get
(
"value"
,
"$X_BIT(%s)"
%
next
)
next
=
int
(
value_traits
.
get_bit_count
(
etor
[
"value"
]))
+
1
else
:
etor
[
"name"
]
=
d
[
"name"
][:
-
1
].
upper
()
+
etor
[
"name"
]
use_hex
=
use_hex
or
value_traits
.
is_hex
(
etor
.
get
(
"value"
))
if
use_hex
:
etor
[
"value"
]
=
etor
.
get
(
"value"
,
"%s"
%
hex
(
next
))
next
=
int
(
etor
[
"value"
],
16
)
+
1
elif
not
value_traits
.
is_ver
(
etor
.
get
(
"value"
)):
etor
[
"value"
]
=
etor
.
get
(
"value"
,
"%s"
%
next
)
next
=
int
(
etor
[
"value"
])
+
1
return
d
def
_subt
(
name
,
tags
):
"""substitute tags"""
name
=
re
.
sub
(
r"(\w+)\(.*\)"
,
r"\1"
,
name
)
# removes '()' part of macros
for
key
,
value
in
tags
.
items
():
name
=
re
.
sub
(
re
.
escape
(
key
),
value
,
name
)
name
=
re
.
sub
(
re
.
escape
(
key
.
upper
()),
value
.
upper
(),
name
)
return
name
def
_get_line_nums
(
f
):
"""get the line number of each document"""
nums
=
[]
for
line_num
,
line
in
enumerate
(
util
.
textRead
(
f
)):
if
re
.
match
(
r"^\-\-\-.*"
,
line
):
nums
.
append
(
line_num
+
2
)
return
nums
def
_get_etor_value
(
value
,
prev
):
"""convert etor to int"""
if
value
:
if
value_traits
.
is_ver
(
value
):
return
(
value_traits
.
get_major_ver
(
value
)
<<
16
)
+
value_traits
.
get_minor_ver
(
value
)
elif
value_traits
.
is_bit
(
value
):
return
1
<<
value_traits
.
get_bit_count
(
value
)
elif
value_traits
.
is_hex
(
value
):
return
int
(
value
,
16
)
else
:
return
int
(
value
)
else
:
return
prev
+
1
def
_validate_doc
(
f
,
d
,
tags
,
line_num
,
meta
):
"""
validate documents meet some basic (easily detectable) requirements of code
generation
"""
def
is_iso
(
x
):
return
re
.
match
(
r"[_a-zA-Z][_a-zA-Z0-9]{0,30}"
,
x
)
def
__validate_ordinal
(
d
):
if
"ordinal"
in
d
:
if
not
isinstance
(
d
[
"ordinal"
],
str
):
raise
Exception
(
"'ordinal' must be a string: '%s'"
%
type
(
d
[
"ordinal"
]))
try
:
ordinal
=
str
(
int
(
d
[
"ordinal"
]))
except
BaseException
:
ordinal
=
None
if
ordinal
!=
d
[
"ordinal"
]:
raise
Exception
(
"'ordinal' invalid value: '%s'"
%
d
[
"ordinal"
])
def
__validate_version
(
d
,
prefix
=
""
,
base_version
=
default_version
):
if
"version"
in
d
:
if
not
isinstance
(
d
[
"version"
],
str
):
raise
Exception
(
prefix
+
"'version' must be a string: '%s'"
%
type
(
d
[
"version"
])
)
try
:
version
=
str
(
d
[
"version"
])
except
BaseException
:
version
=
None
if
version
!=
d
[
"version"
]:
raise
Exception
(
prefix
+
"'version' invalid value: '%s'"
%
d
[
"version"
])
return
Version
(
d
.
get
(
"version"
,
base_version
))
def
__validate_tag
(
d
,
key
,
tags
,
case
):
for
x
in
tags
:
if
d
[
key
].
startswith
(
x
.
upper
()
if
case
==
"upper"
else
x
):
return
x
return
None
def
__validate_desc
(
desc
:
Union
[
dict
,
str
],
prefix
:
str
):
if
isinstance
(
desc
,
dict
):
for
k
,
v
in
desc
.
items
():
if
not
isinstance
(
k
,
str
):
raise
Exception
(
prefix
+
"'version' must be a string: '%s'"
%
type
(
k
)
)
try
:
version
=
str
(
k
)
except
BaseException
:
version
=
None
if
version
!=
k
:
raise
Exception
(
prefix
+
"'version' invalid value: '%s'"
%
k
)
for
x
in
[
"[in]"
,
"[out]"
,
"[in,out]"
]:
if
v
.
startswith
(
x
):
return
x
else
:
for
x
in
[
"[in]"
,
"[out]"
,
"[in,out]"
]:
if
desc
.
startswith
(
x
):
return
x
return
None
def
__validate_name
(
d
,
key
,
tags
,
case
=
"lower"
,
prefix
=
""
):
if
not
isinstance
(
d
[
key
],
str
):
raise
Exception
(
prefix
+
"'%s' must be a string: '%s'"
%
(
key
,
type
(
d
[
key
]))
)
if
not
__validate_tag
(
d
,
key
,
tags
,
case
):
raise
Exception
(
prefix
+
"'%s' must start with {%s}: '%s'"
%
(
key
,
", "
.
join
(
[
x
.
upper
()
if
case
==
"upper"
else
x
for
x
in
tags
if
x
!=
"$OneApi"
]
),
d
[
key
],
)
)
name
=
_subt
(
d
[
key
],
tags
)
if
not
is_iso
(
name
):
raise
Exception
(
prefix
+
"'%s' must be ISO-C: '%s'"
%
(
key
,
d
[
key
]))
if
case
==
"upper"
and
not
name
.
isupper
():
raise
Exception
(
prefix
+
"%s' must be upper case: '%s'"
%
(
key
,
d
[
key
]))
elif
case
==
"lower"
and
not
name
.
islower
():
raise
Exception
(
prefix
+
"'%s' must be lower case: '%s'"
%
(
key
,
d
[
key
]))
elif
case
==
"camel"
and
(
name
.
isupper
()
or
name
.
islower
()):
raise
Exception
(
prefix
+
"'%s' must be camel case: '%s'"
%
(
key
,
d
[
key
]))
def
__validate_type
(
d
,
key
,
tags
):
__validate_name
(
d
,
key
,
tags
)
if
not
d
[
key
].
endswith
(
"_t"
):
raise
Exception
(
"'%s' must end with '_t': '%s'"
%
(
key
,
d
[
key
]))
def
__validate_handle
(
d
,
key
,
tags
):
__validate_type
(
d
,
key
,
tags
)
if
not
d
[
key
].
endswith
(
"handle_t"
):
raise
Exception
(
"'%s' must end with 'handle_t': '%s'"
%
(
key
,
d
[
key
]))
def
__validate_details
(
d
):
if
"details"
in
d
:
if
not
(
isinstance
(
d
[
"details"
],
list
)
or
isinstance
(
d
[
"details"
],
str
)):
raise
Exception
(
"'details' must be a string or a sequence"
)
if
isinstance
(
d
[
"details"
],
list
):
for
i
,
item
in
enumerate
(
d
[
"details"
]):
prefix
=
"'details'[%s] "
%
i
if
isinstance
(
item
,
dict
):
for
key
in
item
:
if
not
isinstance
(
key
,
str
):
raise
Exception
(
prefix
+
"must be a string: '%s'"
%
type
(
key
)
)
for
j
,
val
in
enumerate
(
item
[
key
]):
prefix2
=
prefix
[:
-
1
]
+
"[%s] "
%
j
if
not
isinstance
(
val
,
str
):
raise
Exception
(
prefix2
+
"must be a string: '%s'"
%
type
(
val
)
)
elif
not
isinstance
(
item
,
str
):
raise
Exception
(
prefix
+
"must be a string: '%s'"
%
type
(
item
))
def
extract_type
(
s
):
match
=
re
.
match
(
r"^\[(.+)\]\s"
,
s
)
if
match
:
return
match
.
group
(
1
)
else
:
return
None
def
__validate_etors
(
d
,
tags
):
if
"etors"
not
in
d
:
raise
Exception
(
"'enum' requires the following sequence of mappings: {`etors`}"
)
if
not
isinstance
(
d
[
"etors"
],
list
):
raise
Exception
(
"'etors' must be a sequence: '%s'"
%
type
(
d
[
"etors"
]))
typed
=
d
.
get
(
"typed_etors"
,
False
)
value
=
-
1
d_ver
=
Version
(
d
.
get
(
"version"
,
default_version
))
max_ver
=
d_ver
for
i
,
item
in
enumerate
(
d
[
"etors"
]):
prefix
=
"'etors'[%s] "
%
i
if
not
isinstance
(
item
,
dict
):
raise
Exception
(
prefix
+
"must be a mapping: %d, '%s'"
%
(
i
,
type
(
item
))
)
if
(
"desc"
not
in
item
)
or
(
"name"
not
in
item
):
raise
Exception
(
prefix
+
"requires the following scalar fields: {`desc`, `name`}"
)
if
"extend"
in
d
and
d
.
get
(
"extend"
)
and
"value"
not
in
item
:
raise
Exception
(
prefix
+
"must include a value for experimental features: {`value`: `0xabcd`}"
)
if
typed
:
ty
=
extract_type
(
item
[
"desc"
])
if
ty
is
None
:
raise
Exception
(
prefix
+
"typed etor "
+
item
[
"name"
]
+
" must begin with a type identifier: [type]"
)
type_name
=
_subt
(
ty
,
tags
)
if
not
is_iso
(
type_name
):
raise
Exception
(
prefix
+
"type "
+
str
(
ty
)
+
" in a typed etor "
+
item
[
"name"
]
+
" must be a valid ISO C identifier"
)
__validate_name
(
item
,
"name"
,
tags
,
case
=
"upper"
,
prefix
=
prefix
)
value
=
_get_etor_value
(
item
.
get
(
"value"
),
value
)
if
type_traits
.
is_flags
(
d
[
"name"
])
and
not
value_traits
.
is_bit
(
item
.
get
(
"value"
)
):
raise
Exception
(
prefix
+
"'value' must use BIT macro: %s"
%
value
)
elif
not
type_traits
.
is_flags
(
d
[
"name"
])
and
value_traits
.
is_bit
(
item
.
get
(
"value"
)
):
raise
Exception
(
prefix
+
"'value' must not use BIT macro: %s"
%
value
)
if
value
>=
0x7FFFFFFF
:
raise
Exception
(
prefix
+
"'value' must be less than 0x7fffffff: %s"
%
value
)
ver
=
__validate_version
(
item
,
prefix
=
prefix
,
base_version
=
d_ver
)
if
item
.
get
(
"value"
):
max_ver
=
ver
if
ver
<
max_ver
:
raise
Exception
(
prefix
+
"'version' must be increasing: %s"
%
item
[
"version"
]
)
max_ver
=
ver
def
__validate_base
(
d
):
namespace
=
re
.
sub
(
r"(\$[a-z])\w+"
,
r"\1"
,
d
[
"name"
])
valid_names
=
[
"%s_base_desc_t"
%
namespace
,
"%s_base_properties_t"
%
namespace
,
"%s_driver_extension_properties_t"
%
namespace
,
]
if
d
[
"name"
]
not
in
valid_names
:
if
type_traits
.
is_descriptor
(
d
[
"name"
])
and
not
d
.
get
(
"base"
,
""
).
endswith
(
"base_desc_t"
):
raise
Exception
(
"'base' must be '%s_base_desc_t': %s"
%
(
namespace
,
d
[
"name"
])
)
elif
type_traits
.
is_properties
(
d
[
"name"
])
and
not
d
.
get
(
"base"
,
""
).
endswith
(
"base_properties_t"
):
raise
Exception
(
"'base' must be '%s_base_properties_t': %s"
%
(
namespace
,
d
[
"name"
])
)
def
__validate_struct_range_members
(
name
,
members
,
meta
):
def
has_handle
(
members
,
meta
):
for
m
in
members
:
if
type_traits
.
is_handle
(
m
):
return
True
if
type_traits
.
is_struct
(
m
,
meta
):
return
has_handle
(
type_traits
.
get_struct_members
(
m
[
"type"
],
meta
),
meta
)
return
False
for
m
in
members
:
if
param_traits
.
is_range
(
m
)
and
type_traits
.
is_handle
(
m
[
"type"
]):
raise
Exception
(
f"struct range
{
name
}
must not contain range of object handles
{
m
[
'name'
]
}
"
)
if
type_traits
.
is_struct
(
m
[
"type"
],
meta
):
member_members
=
type_traits
.
get_struct_members
(
m
[
"type"
],
meta
)
# We can't handle a range of structs with handles within a range of structs
if
param_traits
.
is_range
(
m
)
and
has_handle
(
member_members
,
meta
):
raise
Exception
(
f"struct range
{
m
[
'name'
]
}
is already within struct range
{
name
}
, and must not contain an object handle"
)
# We keep passing the original name so we can report it in
# exception messages.
__validate_struct_range_members
(
name
,
member_members
,
meta
)
def
__validate_members
(
d
,
meta
):
if
"members"
not
in
d
:
raise
Exception
(
"'%s' requires the following sequence of mappings: {`members`}"
%
d
[
"type"
]
)
if
not
isinstance
(
d
[
"members"
],
list
):
raise
Exception
(
"'members' must be a sequence: '%s'"
%
type
(
d
[
"members"
]))
d_ver
=
Version
(
d
.
get
(
"version"
,
default_version
))
max_ver
=
d_ver
for
i
,
item
in
enumerate
(
d
[
"members"
]):
prefix
=
"'members'[%s] "
%
i
if
not
isinstance
(
item
,
dict
):
raise
Exception
(
prefix
+
"must be a mapping: %d, '%s'"
%
(
i
,
type
(
item
))
)
if
(
"desc"
not
in
item
)
or
(
"type"
not
in
item
)
or
(
"name"
not
in
item
):
raise
Exception
(
prefix
+
"requires the following scalar fields: {`desc`, 'type', `name`}"
)
annotation
=
__validate_desc
(
item
[
"desc"
],
prefix
)
if
not
annotation
:
raise
Exception
(
prefix
+
"'desc' must start with {'[in]', '[out]', '[in,out]'}"
)
if
item
[
"type"
].
endswith
(
"flag_t"
):
raise
Exception
(
prefix
+
"'type' must not be '*_flag_t': %s"
%
item
[
"type"
]
)
if
d
[
"type"
]
==
"union"
and
item
.
get
(
"tag"
)
is
None
:
raise
Exception
(
prefix
+
f"union member
{
item
[
'name'
]
}
must include a 'tag' annotation"
)
if
type_traits
.
is_struct
(
item
[
"type"
],
meta
)
and
param_traits
.
is_range
(
item
):
member_members
=
type_traits
.
get_struct_members
(
item
[
"type"
],
meta
)
__validate_struct_range_members
(
item
[
"name"
],
member_members
,
meta
)
if
type_traits
.
is_handle
(
item
[
"type"
])
and
param_traits
.
is_output
(
item
):
raise
Exception
(
prefix
+
f"struct member
{
item
[
'name'
]
}
is an object handle, so it must not be have the [out] tag"
)
ver
=
__validate_version
(
item
,
prefix
=
prefix
,
base_version
=
d_ver
)
if
ver
<
max_ver
:
raise
Exception
(
prefix
+
"'version' must be increasing: %s"
%
item
[
"version"
]
)
max_ver
=
ver
def
__validate_params
(
d
,
meta
):
if
"params"
not
in
d
:
raise
Exception
(
"'function' requires the following sequence of mappings: {`params`}"
)
if
not
isinstance
(
d
[
"params"
],
list
):
raise
Exception
(
"'params' must be a sequence: '%s'"
%
type
(
d
[
"params"
]))
d_ver
=
Version
(
d
.
get
(
"version"
,
default_version
))
max_ver
=
d_ver
min
:
Dict
[
str
,
Union
[
int
,
None
]]
=
{
"[in]"
:
None
,
"[out]"
:
None
,
"[in,out]"
:
None
,
}
for
i
,
item
in
enumerate
(
d
[
"params"
]):
prefix
=
"'params'[%s] "
%
i
if
not
isinstance
(
item
,
dict
):
raise
Exception
(
prefix
+
"must be a mapping: %d, '%s'"
%
(
i
,
type
(
item
))
)
if
(
"desc"
not
in
item
)
or
(
"type"
not
in
item
)
or
(
"name"
not
in
item
):
raise
Exception
(
prefix
+
"requires the following scalar fields: {`desc`, 'type', `name`}"
)
annotation
=
__validate_desc
(
item
[
"desc"
],
prefix
)
if
not
annotation
:
raise
Exception
(
prefix
+
"'desc' must start with {'[in]', '[out]', '[in,out]'}"
)
if
not
min
[
annotation
]:
min
[
annotation
]
=
i
if
min
[
"[out]"
]
and
(
"[in]"
==
annotation
or
"[in,out]"
==
annotation
):
raise
Exception
(
prefix
+
"'%s' must come before '[out]'"
%
annotation
)
if
(
d
.
get
(
"decl"
)
!=
"static"
and
i
==
0
and
not
type_traits
.
is_handle
(
item
[
"type"
])
):
raise
Exception
(
prefix
+
"'type' must be '*_handle_t': %s"
%
item
[
"type"
]
)
if
item
[
"type"
].
endswith
(
"flag_t"
):
raise
Exception
(
prefix
+
"'type' must not be '*_flag_t': %s"
%
item
[
"type"
]
)
if
(
type_traits
.
is_pointer
(
item
[
"type"
])
and
"_handle_t"
in
item
[
"type"
]
and
"[in]"
in
item
[
"desc"
]
):
if
not
param_traits
.
is_range
(
item
):
raise
Exception
(
prefix
+
"handle type must include a range(start, end) as part of 'desc'"
)
if
param_traits
.
is_bounds
(
item
):
has_queue
=
False
for
p
in
d
[
"params"
]:
if
re
.
match
(
r"hQueue$"
,
p
[
"name"
]):
has_queue
=
True
if
not
has_queue
:
raise
Exception
(
prefix
+
"bounds must only be used on entry points which take a `hQueue` parameter"
)
if
type_traits
.
is_struct
(
item
[
"type"
],
meta
)
and
param_traits
.
is_range
(
item
):
members
=
type_traits
.
get_struct_members
(
item
[
"type"
],
meta
)
__validate_struct_range_members
(
item
[
"name"
],
members
,
meta
)
ver
=
__validate_version
(
item
,
prefix
=
prefix
,
base_version
=
d_ver
)
if
ver
<
max_ver
:
raise
Exception
(
prefix
+
"'version' must be increasing: %s"
%
item
[
"version"
]
)
max_ver
=
ver
def
__validate_union_tag
(
d
):
if
d
.
get
(
"tag"
)
is
None
:
raise
Exception
(
f"
{
d
[
'name'
]
}
must include a 'tag' part of the union."
)
def
__validate_guard
(
d
):
if
"guard"
not
in
d
:
return
if
not
isinstance
(
d
[
"guard"
],
str
):
raise
Exception
(
"'guard' must be a string: '%s'"
%
type
(
d
[
"guard"
]))
if
not
re
.
match
(
r"[A-Z][A-Z0-9_]*$"
,
d
[
"guard"
]):
raise
Exception
(
"'guard' must a valid upper case C identifier: '%s'"
%
d
[
"guard"
]
)
try
:
if
"type"
not
in
d
:
raise
Exception
(
"every document must have 'type'"
)
if
not
isinstance
(
d
[
"type"
],
str
):
raise
Exception
(
"'type' must be a string: '%s'"
%
type
(
d
[
"type"
]))
if
"header"
==
d
[
"type"
]:
if
"desc"
not
in
d
:
raise
Exception
(
"'header' requires the following scalar fields: {`desc`}"
)
if
not
isinstance
(
d
[
"desc"
],
str
):
raise
Exception
(
"'desc' must be a string"
)
__validate_ordinal
(
d
)
__validate_version
(
d
)
__validate_guard
(
d
)
elif
"macro"
==
d
[
"type"
]:
if
(
"desc"
not
in
d
)
or
(
"name"
not
in
d
)
or
(
"value"
not
in
d
):
raise
Exception
(
"'macro' requires the following scalar fields: {`desc`, `name`, `value`}"
)
if
not
isinstance
(
d
[
"desc"
],
str
):
raise
Exception
(
"'desc' must be a string"
)
__validate_name
(
d
,
"name"
,
tags
,
case
=
"upper"
)
__validate_details
(
d
)
__validate_ordinal
(
d
)
__validate_version
(
d
)
__validate_guard
(
d
)
elif
"typedef"
==
d
[
"type"
]:
if
(
"desc"
not
in
d
)
or
(
"name"
not
in
d
)
or
(
"value"
not
in
d
):
raise
Exception
(
"'typedef' requires the following scalar fields: {`desc`, `name`, `value`}"
)
__validate_type
(
d
,
"name"
,
tags
)
__validate_details
(
d
)
__validate_ordinal
(
d
)
__validate_version
(
d
)
__validate_guard
(
d
)
elif
"handle"
==
d
[
"type"
]:
if
(
"desc"
not
in
d
)
or
(
"name"
not
in
d
):
raise
Exception
(
"'handle' requires the following scalar fields: {`desc`, `name`}"
)
__validate_handle
(
d
,
"name"
,
tags
)
__validate_details
(
d
)
__validate_ordinal
(
d
)
__validate_version
(
d
)
__validate_guard
(
d
)
elif
"enum"
==
d
[
"type"
]:
if
(
"desc"
not
in
d
)
or
(
"name"
not
in
d
):
raise
Exception
(
"'enum' requires the following scalar fields: {`desc`, `name`}"
)
__validate_type
(
d
,
"name"
,
tags
)
__validate_etors
(
d
,
tags
)
__validate_details
(
d
)
__validate_ordinal
(
d
)
__validate_version
(
d
)
__validate_guard
(
d
)
elif
"struct"
==
d
[
"type"
]
or
"union"
==
d
[
"type"
]:
if
(
"desc"
not
in
d
)
or
(
"name"
not
in
d
):
raise
Exception
(
"'%s' requires the following scalar fields: {`desc`, `name`}"
%
d
[
"type"
]
)
if
d
[
"type"
]
==
"union"
:
__validate_union_tag
(
d
)
__validate_type
(
d
,
"name"
,
tags
)
__validate_base
(
d
)
__validate_members
(
d
,
meta
)
__validate_details
(
d
)
__validate_ordinal
(
d
)
__validate_version
(
d
)
__validate_guard
(
d
)
elif
"function"
==
d
[
"type"
]:
if
(
"desc"
not
in
d
)
or
(
"name"
not
in
d
):
raise
Exception
(
"'function' requires the following scalar fields: {`desc`, `name`}"
)
if
"class"
in
d
:
__validate_name
(
{
"name"
:
d
[
"class"
]
+
d
[
"name"
]},
"name"
,
tags
,
case
=
"camel"
)
else
:
__validate_name
(
d
,
"name"
,
tags
,
case
=
"camel"
)
__validate_params
(
d
,
meta
)
__validate_details
(
d
)
__validate_ordinal
(
d
)
__validate_version
(
d
)
__validate_guard
(
d
)
elif
"class"
==
d
[
"type"
]:
if
(
"desc"
not
in
d
)
or
(
"name"
not
in
d
):
raise
Exception
(
"'class' requires the following scalar fields: {`desc`, `name`}"
)
__validate_name
(
d
,
"name"
,
tags
,
case
=
"camel"
)
__validate_details
(
d
)
__validate_ordinal
(
d
)
__validate_version
(
d
)
__validate_guard
(
d
)
return
True
except
Exception
as
msg
:
print
(
"Specification Validation Error:"
)
print
(
"%s(%s): %s!"
%
(
os
.
path
.
abspath
(
f
),
line_num
,
msg
))
print
(
"-- Function Info --"
)
print
(
d
)
raise
def
_filter_version
(
d
,
max_ver
:
Version
)
->
Optional
[
Dict
]:
"""filters object by version"""
ver
=
Version
(
d
.
get
(
"version"
,
default_version
))
if
ver
>
max_ver
:
return
None
def
__filter_desc
(
d
)
->
dict
:
if
"desc"
in
d
and
isinstance
(
d
[
"desc"
],
dict
):
for
k
,
v
in
d
[
"desc"
].
items
():
if
Version
(
k
)
<=
max_ver
:
d
[
"desc"
]
=
v
return
d
flt
=
[]
type
=
d
[
"type"
]
if
"enum"
==
type
:
for
e
in
d
[
"etors"
]:
ver
=
Version
(
e
.
get
(
"version"
,
default_version
))
if
ver
<=
max_ver
:
flt
.
append
(
__filter_desc
(
e
))
if
d
[
"name"
].
endswith
(
"version_t"
):
flt
.
append
(
{
"name"
:
d
[
"name"
][:
-
1
].
upper
()
+
"CURRENT"
,
"value"
:
flt
[
-
1
][
"value"
],
"desc"
:
"latest known version"
,
}
)
d
[
"etors"
]
=
flt
elif
"function"
==
type
:
for
p
in
d
[
"params"
]:
ver
=
Version
(
p
.
get
(
"version"
,
default_version
))
if
ver
<=
max_ver
:
flt
.
append
(
__filter_desc
(
p
))
d
[
"params"
]
=
flt
elif
"struct"
==
type
or
"union"
==
type
or
"class"
==
type
:
for
m
in
d
.
get
(
"members"
, []):
ver
=
Version
(
m
.
get
(
"version"
,
default_version
))
if
ver
<=
max_ver
:
flt
.
append
(
__filter_desc
(
m
))
d
[
"members"
]
=
flt
return
__filter_desc
(
d
)
def
_make_versions
(
d
,
max_ver
:
Version
)
->
List
[
Version
]:
"""creates docs per version"""
docs
=
[]
type
=
d
[
"type"
]
if
"function"
==
type
or
"struct"
==
type
:
for
ver
in
all_versions
:
if
ver
>
max_ver
:
break
dv
=
_filter_version
(
copy
.
deepcopy
(
d
),
ver
)
if
not
dv
:
continue
if
len
(
docs
)
>
0
:
if
dv
==
docs
[
-
1
]:
continue
dv
[
"name"
]
+=
ver
docs
.
append
(
dv
)
else
:
docs
.
append
(
d
)
return
docs
def
_generate_meta
(
d
,
ordinal
,
meta
):
"""generates meta-data on all objects"""
type
=
d
[
"type"
]
name
=
re
.
sub
(
r"(\w+)\(.*\)"
,
r"\1"
,
d
[
"name"
])
# removes '()' part of macros
# create dict if typename is not already known...
if
type
not
in
meta
:
meta
[
type
]
=
{}
if
"class"
not
in
meta
:
meta
[
"class"
]
=
{}
cls
=
d
.
get
(
"class"
)
if
cls
:
# create dict if class name is not already known...
if
cls
not
in
meta
[
"class"
]:
meta
[
"class"
][
cls
]
=
{}
meta
[
"class"
][
cls
][
"ordinal"
]
=
0
# create list if object-type is not already known for class...
if
type
not
in
meta
[
"class"
][
cls
]:
meta
[
"class"
][
cls
][
type
]
=
[]
# append if object-name is not already known for object-type in class...
if
name
not
in
meta
[
"class"
][
cls
][
type
]:
meta
[
"class"
][
cls
][
type
].
append
(
name
)
else
:
print
(
"Error - duplicate entries for %s found!"
%
name
)
raise
if
"class"
!=
type
:
# create list if name is not already known for type...
if
"function"
==
type
and
cls
:
name
=
cls
+
name
if
name
not
in
meta
[
type
]:
meta
[
type
][
name
]
=
{
"class"
:
""
}
# add values to list
if
"enum"
==
type
:
value
=
-
1
max_value
=
-
1
max_index
=
-
1
bit_mask
=
0
meta
[
type
][
name
][
"etors"
]
=
[]
for
idx
,
etor
in
enumerate
(
d
[
"etors"
]):
meta
[
type
][
name
][
"etors"
].
append
(
etor
[
"name"
])
value
=
_get_etor_value
(
etor
.
get
(
"value"
),
value
)
if
not
etor
.
get
(
"value"
):
etor
[
"value"
]
=
str
(
value
)
if
type_traits
.
is_flags
(
name
):
bit_mask
|=
value
if
value
>
max_value
:
max_value
=
value
max_index
=
idx
if
type_traits
.
is_flags
(
name
):
meta
[
type
][
name
][
"max"
]
=
(
hex
((
max_value
<<
1
)
-
1
)
if
max_value
else
"0"
)
if
bit_mask
!=
0
:
meta
[
type
][
name
][
"bit_mask"
]
=
hex
(
ctypes
.
c_uint32
(
~
bit_mask
).
value
)
else
:
meta
[
type
][
name
][
"max"
]
=
d
[
"etors"
][
max_index
][
"name"
]
elif
"macro"
==
type
:
meta
[
type
][
name
][
"values"
]
=
[]
if
"value"
in
d
:
meta
[
type
][
name
][
"values"
].
append
(
d
[
"value"
])
if
"altvalue"
in
d
:
meta
[
type
][
name
][
"values"
].
append
(
d
[
"altvalue"
])
elif
"function"
==
type
:
meta
[
type
][
name
][
"params"
]
=
[]
for
p
in
d
[
"params"
]:
meta
[
type
][
name
][
"params"
].
append
({
"type"
:
p
[
"type"
]})
elif
"struct"
==
type
or
"union"
==
type
:
meta
[
type
][
name
][
"members"
]
=
[]
for
m
in
d
[
"members"
]:
meta
[
type
][
name
][
"members"
].
append
(
{
"type"
:
m
[
"type"
],
"name"
:
m
[
"name"
],
"desc"
:
m
[
"desc"
],
"init"
:
m
.
get
(
"init"
),
}
)
if
cls
:
meta
[
type
][
name
][
"class"
]
=
cls
else
:
if
name
not
in
meta
[
"class"
]:
meta
[
"class"
][
name
]
=
{}
meta
[
"class"
][
name
][
"ordinal"
]
=
ordinal
if
"base"
in
d
:
base
=
d
[
"base"
]
if
"child"
not
in
meta
[
"class"
][
base
]:
meta
[
"class"
][
base
][
"child"
]
=
[]
meta
[
"class"
][
base
][
"child"
].
append
(
name
)
if
"handle"
not
in
meta
[
"class"
][
name
]:
meta
[
"class"
][
name
][
"handle"
]
=
[]
if
name
[:
2
]
==
base
[:
2
]:
meta
[
"class"
][
name
][
"handle"
].
extend
(
meta
[
"class"
][
base
][
"handle"
])
if
"members"
in
d
:
meta
[
"class"
][
name
][
"members"
]
=
[]
for
m
in
d
[
"members"
]:
meta
[
"class"
][
name
][
"members"
].
append
(
m
[
"type"
])
if
"owner"
in
d
:
owner
=
d
[
"owner"
]
meta
[
"class"
][
name
][
"owner"
]
=
owner
if
owner
not
in
meta
[
"class"
]:
meta
[
"class"
][
owner
]
=
{}
if
"owns"
not
in
meta
[
"class"
][
owner
]:
meta
[
"class"
][
owner
][
"owns"
]
=
[]
meta
[
"class"
][
owner
][
"owns"
].
append
(
name
)
return
meta
def
_generate_hash
(
obj
):
"""generates SHA512 string for the given object"""
# functions-only (for now)...
if
re
.
match
(
r"function"
,
obj
[
"type"
]):
hash
=
hashlib
.
sha256
()
# hashcode of function signature...
hash
.
update
(
obj
[
"name"
].
encode
())
for
p
in
obj
[
"params"
]:
hash
.
update
(
p
[
"type"
].
encode
())
# hashcode of class
if
"class"
in
obj
:
hash
.
update
(
obj
[
"class"
].
encode
())
# digest into string
obj
[
"hash"
]
=
hash
.
hexdigest
()
return
obj
def
_inline_base
(
obj
,
meta
):
"""generates structure members from base"""
if
re
.
match
(
r"struct|union"
,
obj
[
"type"
]):
base
=
obj
.
get
(
"base"
)
if
base
in
meta
[
"struct"
]:
for
i
,
m
in
enumerate
(
meta
[
"struct"
][
base
][
"members"
]):
m
=
copy
.
deepcopy
(
m
)
if
m
[
"name"
]
==
"stype"
:
m
[
"init"
]
=
re
.
sub
(
r"(\$[a-z]+)(\w+)_t"
,
r"\1_STRUCTURE_TYPE\2"
,
obj
[
"name"
]
).
upper
()
m
[
"desc"
]
+=
", must be %s"
%
m
[
"init"
]
obj
[
"members"
].
insert
(
i
,
m
)
return
obj
def
_generate_returns
(
obj
,
meta
):
"""generate complete return permutations"""
if
re
.
match
(
r"function"
,
obj
[
"type"
]):
# default results for all functions
rets
=
[
{
"$X_RESULT_SUCCESS"
: []},
{
"$X_RESULT_ERROR_UNINITIALIZED"
: []},
{
"$X_RESULT_ERROR_DEVICE_LOST"
: []},
{
"$X_RESULT_ERROR_ADAPTER_SPECIFIC"
: []},
]
# special function for appending to our list of dicts; avoiding duplicates
def
_append
(
lst
,
key
,
val
):
idx
=
next
((
i
for
i
,
v
in
enumerate
(
lst
)
if
v
.
get
(
key
)),
len
(
lst
))
if
idx
==
len
(
lst
):
rets
.
append
({
key
: []})
if
val
and
val
not
in
rets
[
idx
][
key
]:
rets
[
idx
][
key
].
append
(
val
)
def
append_nullchecks
(
param
,
accessor
:
str
):
if
type_traits
.
is_pointer
(
param
[
"type"
])
or
type_traits
.
is_array
(
param
[
"type"
]
):
_append
(
rets
,
"$X_RESULT_ERROR_INVALID_NULL_POINTER"
,
"`NULL == %s`"
%
accessor
,
)
elif
type_traits
.
is_funcptr
(
param
[
"type"
],
meta
):
_append
(
rets
,
"$X_RESULT_ERROR_INVALID_NULL_POINTER"
,
"`NULL == %s`"
%
accessor
,
)
elif
(
type_traits
.
is_handle
(
param
[
"type"
])
and
not
type_traits
.
is_ipc_handle
(
item
[
"type"
])
and
not
type_traits
.
is_native_handle
(
item
[
"type"
])
):
_append
(
rets
,
"$X_RESULT_ERROR_INVALID_NULL_HANDLE"
,
"`NULL == %s`"
%
accessor
,
)
def
append_enum_checks
(
param
,
accessor
:
str
):
typename
=
type_traits
.
base
(
param
[
"type"
])
assert
typename
prefix
=
"`"
if
param_traits
.
is_optional
(
item
):
prefix
=
"`NULL != %s && "
%
item
[
"name"
]
if
re
.
match
(
r"stype"
,
param
[
"name"
]):
_append
(
rets
,
"$X_RESULT_ERROR_UNSUPPORTED_VERSION"
,
prefix
+
"%s != %s`"
%
(
re
.
sub
(
r"(\$\w)_(.*)_t.*"
,
r"\1_STRUCTURE_TYPE_\2"
,
typename
).
upper
(),
accessor
,
),
)
else
:
if
(
type_traits
.
is_flags
(
param
[
"type"
])
and
"bit_mask"
in
meta
[
"enum"
][
typename
].
keys
()
):
_append
(
rets
,
"$X_RESULT_ERROR_INVALID_ENUMERATION"
,
prefix
+
"%s & %s`"
%
(
typename
.
upper
()[:
-
2
]
+
"_MASK"
,
accessor
),
)
else
:
_append
(
rets
,
"$X_RESULT_ERROR_INVALID_ENUMERATION"
,
prefix
+
"%s < %s`"
%
(
meta
[
"enum"
][
typename
][
"max"
],
accessor
),
)
# generate results based on parameters
for
item
in
obj
[
"params"
]:
if
param_traits
.
is_nocheck
(
item
):
continue
if
not
param_traits
.
is_optional
(
item
):
append_nullchecks
(
item
,
item
[
"name"
])
if
type_traits
.
is_enum
(
item
[
"type"
],
meta
)
and
not
type_traits
.
is_pointer
(
item
[
"type"
]
):
append_enum_checks
(
item
,
item
[
"name"
])
if
type_traits
.
is_descriptor
(
item
[
"type"
])
or
type_traits
.
is_properties
(
item
[
"type"
]
):
typename
=
type_traits
.
base
(
item
[
"type"
])
# walk each entry in the desc for pointers and enums
for
m
in
meta
[
"struct"
][
typename
][
"members"
]:
if
param_traits
.
is_nocheck
(
m
):
continue
if
not
param_traits
.
is_optional
(
m
):
append_nullchecks
(
m
,
"%s->%s"
%
(
item
[
"name"
],
m
[
"name"
]))
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL