ndtypes 0.2.0dev4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (139) hide show
  1. checksums.yaml +7 -0
  2. data/CONTRIBUTING.md +50 -0
  3. data/Gemfile +2 -0
  4. data/History.md +0 -0
  5. data/README.md +19 -0
  6. data/Rakefile +125 -0
  7. data/ext/ruby_ndtypes/extconf.rb +55 -0
  8. data/ext/ruby_ndtypes/gc_guard.c +36 -0
  9. data/ext/ruby_ndtypes/gc_guard.h +12 -0
  10. data/ext/ruby_ndtypes/ndtypes/AUTHORS.txt +5 -0
  11. data/ext/ruby_ndtypes/ndtypes/INSTALL.txt +101 -0
  12. data/ext/ruby_ndtypes/ndtypes/LICENSE.txt +29 -0
  13. data/ext/ruby_ndtypes/ndtypes/MANIFEST.in +3 -0
  14. data/ext/ruby_ndtypes/ndtypes/Makefile.in +87 -0
  15. data/ext/ruby_ndtypes/ndtypes/README.rst +47 -0
  16. data/ext/ruby_ndtypes/ndtypes/config.guess +1530 -0
  17. data/ext/ruby_ndtypes/ndtypes/config.h.in +67 -0
  18. data/ext/ruby_ndtypes/ndtypes/config.sub +1782 -0
  19. data/ext/ruby_ndtypes/ndtypes/configure +5260 -0
  20. data/ext/ruby_ndtypes/ndtypes/configure.ac +161 -0
  21. data/ext/ruby_ndtypes/ndtypes/doc/Makefile +14 -0
  22. data/ext/ruby_ndtypes/ndtypes/doc/_static/copybutton.js +66 -0
  23. data/ext/ruby_ndtypes/ndtypes/doc/conf.py +26 -0
  24. data/ext/ruby_ndtypes/ndtypes/doc/grammar/grammar.rst +27 -0
  25. data/ext/ruby_ndtypes/ndtypes/doc/index.rst +56 -0
  26. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/context.rst +131 -0
  27. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/encodings.rst +68 -0
  28. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/fields-values.rst +175 -0
  29. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/functions.rst +72 -0
  30. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/index.rst +43 -0
  31. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/init.rst +48 -0
  32. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/io.rst +100 -0
  33. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/memory.rst +124 -0
  34. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/predicates.rst +110 -0
  35. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/typedef.rst +31 -0
  36. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/types.rst +594 -0
  37. data/ext/ruby_ndtypes/ndtypes/doc/libndtypes/util.rst +166 -0
  38. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/buffer-protocol.rst +27 -0
  39. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/index.rst +21 -0
  40. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/pattern-matching.rst +330 -0
  41. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/quickstart.rst +144 -0
  42. data/ext/ruby_ndtypes/ndtypes/doc/ndtypes/types.rst +544 -0
  43. data/ext/ruby_ndtypes/ndtypes/doc/releases/index.rst +35 -0
  44. data/ext/ruby_ndtypes/ndtypes/install-sh +527 -0
  45. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.in +271 -0
  46. data/ext/ruby_ndtypes/ndtypes/libndtypes/Makefile.vc +269 -0
  47. data/ext/ruby_ndtypes/ndtypes/libndtypes/alloc.c +230 -0
  48. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.c +268 -0
  49. data/ext/ruby_ndtypes/ndtypes/libndtypes/attr.h +109 -0
  50. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.in +73 -0
  51. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/Makefile.vc +70 -0
  52. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/README.txt +16 -0
  53. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.c +2179 -0
  54. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.h +134 -0
  55. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bpgrammar.y +428 -0
  56. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.c +2543 -0
  57. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.h +735 -0
  58. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/bplexer.l +176 -0
  59. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/export.c +543 -0
  60. data/ext/ruby_ndtypes/ndtypes/libndtypes/compat/import.c +110 -0
  61. data/ext/ruby_ndtypes/ndtypes/libndtypes/context.c +228 -0
  62. data/ext/ruby_ndtypes/ndtypes/libndtypes/copy.c +634 -0
  63. data/ext/ruby_ndtypes/ndtypes/libndtypes/encodings.c +116 -0
  64. data/ext/ruby_ndtypes/ndtypes/libndtypes/equal.c +288 -0
  65. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.c +3067 -0
  66. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.h +180 -0
  67. data/ext/ruby_ndtypes/ndtypes/libndtypes/grammar.y +417 -0
  68. data/ext/ruby_ndtypes/ndtypes/libndtypes/io.c +1658 -0
  69. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.c +2773 -0
  70. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.h +734 -0
  71. data/ext/ruby_ndtypes/ndtypes/libndtypes/lexer.l +222 -0
  72. data/ext/ruby_ndtypes/ndtypes/libndtypes/match.c +1132 -0
  73. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.c +2323 -0
  74. data/ext/ruby_ndtypes/ndtypes/libndtypes/ndtypes.h.in +893 -0
  75. data/ext/ruby_ndtypes/ndtypes/libndtypes/overflow.h +161 -0
  76. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.c +473 -0
  77. data/ext/ruby_ndtypes/ndtypes/libndtypes/parsefuncs.h +92 -0
  78. data/ext/ruby_ndtypes/ndtypes/libndtypes/parser.c +246 -0
  79. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.c +269 -0
  80. data/ext/ruby_ndtypes/ndtypes/libndtypes/seq.h +197 -0
  81. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.in +48 -0
  82. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/Makefile.vc +46 -0
  83. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/deserialize.c +1007 -0
  84. data/ext/ruby_ndtypes/ndtypes/libndtypes/serialize/serialize.c +442 -0
  85. data/ext/ruby_ndtypes/ndtypes/libndtypes/slice.h +42 -0
  86. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.c +238 -0
  87. data/ext/ruby_ndtypes/ndtypes/libndtypes/substitute.h +50 -0
  88. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.c +371 -0
  89. data/ext/ruby_ndtypes/ndtypes/libndtypes/symtable.h +100 -0
  90. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.in +55 -0
  91. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/Makefile.vc +45 -0
  92. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.c +82 -0
  93. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/alloc_fail.h +49 -0
  94. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/runtest.c +1657 -0
  95. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test.h +85 -0
  96. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_array.c +115 -0
  97. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_buffer.c +137 -0
  98. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_indent.c +201 -0
  99. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_match.c +2397 -0
  100. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_numba.c +57 -0
  101. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse.c +349 -0
  102. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_error.c +27839 -0
  103. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_parse_roundtrip.c +350 -0
  104. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_record.c +231 -0
  105. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typecheck.c +375 -0
  106. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/test_typedef.c +65 -0
  107. data/ext/ruby_ndtypes/ndtypes/libndtypes/tests/valgrind.supp +30 -0
  108. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/bench.c +79 -0
  109. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/indent.c +94 -0
  110. data/ext/ruby_ndtypes/ndtypes/libndtypes/tools/print_ast.c +96 -0
  111. data/ext/ruby_ndtypes/ndtypes/libndtypes/util.c +474 -0
  112. data/ext/ruby_ndtypes/ndtypes/libndtypes/values.c +228 -0
  113. data/ext/ruby_ndtypes/ndtypes/python/bench.py +49 -0
  114. data/ext/ruby_ndtypes/ndtypes/python/ndt_randtype.py +409 -0
  115. data/ext/ruby_ndtypes/ndtypes/python/ndt_support.py +14 -0
  116. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/__init__.py +70 -0
  117. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/_ndtypes.c +1332 -0
  118. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/docstrings.h +319 -0
  119. data/ext/ruby_ndtypes/ndtypes/python/ndtypes/pyndtypes.h +154 -0
  120. data/ext/ruby_ndtypes/ndtypes/python/test_ndtypes.py +1977 -0
  121. data/ext/ruby_ndtypes/ndtypes/setup.py +288 -0
  122. data/ext/ruby_ndtypes/ndtypes/vcbuild/INSTALL.txt +41 -0
  123. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest32.bat +15 -0
  124. data/ext/ruby_ndtypes/ndtypes/vcbuild/runtest64.bat +13 -0
  125. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild32.bat +38 -0
  126. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcbuild64.bat +38 -0
  127. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcclean.bat +13 -0
  128. data/ext/ruby_ndtypes/ndtypes/vcbuild/vcdistclean.bat +14 -0
  129. data/ext/ruby_ndtypes/ruby_ndtypes.c +1003 -0
  130. data/ext/ruby_ndtypes/ruby_ndtypes.h +37 -0
  131. data/ext/ruby_ndtypes/ruby_ndtypes_internal.h +28 -0
  132. data/lib/ndtypes.rb +45 -0
  133. data/lib/ndtypes/errors.rb +2 -0
  134. data/lib/ndtypes/version.rb +6 -0
  135. data/ndtypes.gemspec +47 -0
  136. data/spec/gc_table_spec.rb +10 -0
  137. data/spec/ndtypes_spec.rb +289 -0
  138. data/spec/spec_helper.rb +241 -0
  139. metadata +242 -0
@@ -0,0 +1,230 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #include <stdio.h>
35
+ #include <stdlib.h>
36
+ #include <stdint.h>
37
+ #include <limits.h>
38
+ #include <assert.h>
39
+ #include "ndtypes.h"
40
+ #include "overflow.h"
41
+
42
+
43
+ #if defined(_MSC_VER)
44
+ #pragma warning(disable : 4232)
45
+ #else
46
+ #include "config.h"
47
+ #ifdef WITH_VALGRIND
48
+ #include <valgrind/memcheck.h>
49
+ #endif
50
+ #endif
51
+
52
+ #if SIZE_MAX > UINTPTR_MAX
53
+ #error "need SIZE_MAX <= UINTPTR_MAX"
54
+ #endif
55
+
56
+
57
+ /* Custom allocation and free functions */
58
+ void *(* ndt_mallocfunc)(size_t size) = malloc;
59
+ void *(* ndt_callocfunc)(size_t nmemb, size_t size) = calloc;
60
+ void *(* ndt_reallocfunc)(void *ptr, size_t size) = realloc;
61
+ void (* ndt_freefunc)(void *ptr) = free;
62
+
63
+
64
+ static inline int
65
+ ispower2(uint16_t n)
66
+ {
67
+ return n != 0 && (n & (n-1)) == 0;
68
+ }
69
+
70
+ static inline size_t
71
+ convert_req(int64_t nmemb, int64_t size, bool *overflow)
72
+ {
73
+ int64_t req;
74
+
75
+ assert(nmemb >= 0 && size >= 0);
76
+ if (nmemb == 0 || size == 0) {
77
+ return 1;
78
+ }
79
+
80
+ req = MULi64(nmemb, size, overflow);
81
+
82
+ #if SIZE_MAX < INT64_MAX
83
+ if (req > INT32_MAX) {
84
+ *overflow = 1;
85
+ return SIZE_MAX;
86
+ }
87
+ #endif
88
+
89
+ return (size_t)req;
90
+ }
91
+
92
+ /* malloc with overflow checking */
93
+ void *
94
+ ndt_alloc(int64_t nmemb, int64_t size)
95
+ {
96
+ bool overflow = 0;
97
+ size_t req;
98
+
99
+ req = convert_req(nmemb, size, &overflow);
100
+ if (overflow) {
101
+ return NULL;
102
+ }
103
+
104
+ return ndt_mallocfunc(req);
105
+ }
106
+
107
+ void *
108
+ ndt_alloc_size(size_t size)
109
+ {
110
+ return ndt_mallocfunc(size);
111
+ }
112
+
113
+ /* calloc */
114
+ void *
115
+ ndt_calloc(int64_t nmemb, int64_t size)
116
+ {
117
+ assert(nmemb >= 0 && size >= 0);
118
+ nmemb = nmemb == 0 ? 1 : nmemb;
119
+ size = size == 0 ? 1 : size;
120
+
121
+ #if SIZE_MAX < INT64_MAX
122
+ if (nmemb > INT32_MAX || size > INT32_MAX) {
123
+ return NULL;
124
+ }
125
+ #endif
126
+
127
+ return ndt_callocfunc((size_t)nmemb, (size_t)size);
128
+ }
129
+
130
+ /* realloc with overflow checking */
131
+ void *
132
+ ndt_realloc(void *ptr, int64_t nmemb, int64_t size)
133
+ {
134
+ bool overflow = 0;
135
+ size_t req;
136
+
137
+ req = convert_req(nmemb, size, &overflow);
138
+ if (overflow) {
139
+ return NULL;
140
+ }
141
+
142
+ return ndt_reallocfunc(ptr, req);
143
+ }
144
+
145
+ /* free */
146
+ void
147
+ ndt_free(void *ptr)
148
+ {
149
+ ndt_freefunc(ptr);
150
+ }
151
+
152
+ /* aligned calloc */
153
+ void *
154
+ ndt_aligned_calloc(uint16_t alignment, int64_t size)
155
+ {
156
+ bool overflow = 0;
157
+ uintptr_t uintptr, aligned;
158
+ int64_t req;
159
+ uint32_t extra;
160
+ void *ptr;
161
+
162
+ if (!ispower2(alignment)) {
163
+ return NULL;
164
+ }
165
+
166
+ if (alignment < MAX_ALIGN) {
167
+ alignment = MAX_ALIGN;
168
+ }
169
+
170
+ if (size == 0) {
171
+ size = 1;
172
+ }
173
+
174
+ extra = alignment - 1;
175
+ extra = extra + sizeof(uintptr_t);
176
+
177
+ req = ADDi64(size, extra, &overflow);
178
+ if (overflow) {
179
+ return NULL;
180
+ }
181
+
182
+ #if SIZE_MAX < INT64_MAX
183
+ if (req > INT32_MAX) {
184
+ return NULL;
185
+ }
186
+ #endif
187
+
188
+ ptr = ndt_callocfunc((size_t)req, 1);
189
+ if (ptr == NULL) {
190
+ return NULL;
191
+ }
192
+
193
+ uintptr = (uintptr_t)ptr;
194
+ aligned = uintptr + extra;
195
+ if (aligned < uintptr) {
196
+ free(ptr);
197
+ return NULL;
198
+ }
199
+
200
+ aligned = aligned - (aligned % alignment);
201
+ *((uintptr_t *)aligned - 1) = uintptr;
202
+
203
+ #ifdef WITH_VALGRIND
204
+ {
205
+ uintptr_t leading_noaccess = aligned - uintptr;
206
+ uintptr_t trailing_noaccess = req - (size + leading_noaccess);
207
+ VALGRIND_MAKE_MEM_NOACCESS(uintptr, leading_noaccess);
208
+ VALGRIND_MAKE_MEM_NOACCESS(aligned+size, trailing_noaccess);
209
+ }
210
+ #endif
211
+
212
+ return (void *)aligned;
213
+ }
214
+
215
+ void
216
+ ndt_aligned_free(void *aligned)
217
+ {
218
+ uintptr_t uintptr;
219
+
220
+ if (aligned == NULL) {
221
+ return;
222
+ }
223
+
224
+ #ifdef WITH_VALGRIND
225
+ VALGRIND_MAKE_MEM_DEFINED((uintptr_t *)aligned - 1, sizeof(uintptr_t));
226
+ #endif
227
+
228
+ uintptr = *((uintptr_t *)aligned - 1);
229
+ ndt_freefunc((void *)uintptr);
230
+ }
@@ -0,0 +1,268 @@
1
+ /*
2
+ * BSD 3-Clause License
3
+ *
4
+ * Copyright (c) 2017-2018, plures
5
+ * All rights reserved.
6
+ *
7
+ * Redistribution and use in source and binary forms, with or without
8
+ * modification, are permitted provided that the following conditions are met:
9
+ *
10
+ * 1. Redistributions of source code must retain the above copyright notice,
11
+ * this list of conditions and the following disclaimer.
12
+ *
13
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
14
+ * this list of conditions and the following disclaimer in the documentation
15
+ * and/or other materials provided with the distribution.
16
+ *
17
+ * 3. Neither the name of the copyright holder nor the names of its
18
+ * contributors may be used to endorse or promote products derived from
19
+ * this software without specific prior written permission.
20
+ *
21
+ * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22
+ * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
24
+ * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
25
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
27
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
28
+ * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29
+ * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30
+ * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31
+ */
32
+
33
+
34
+ #include <stdio.h>
35
+ #include <stdlib.h>
36
+ #include <string.h>
37
+ #include <stdarg.h>
38
+ #include <assert.h>
39
+ #include "ndtypes.h"
40
+ #include "seq.h"
41
+ #include "attr.h"
42
+
43
+
44
+ /*****************************************************************************/
45
+ /* Type attributes used in the parser */
46
+ /*****************************************************************************/
47
+
48
+ void
49
+ ndt_attr_del(ndt_attr_t *attr)
50
+ {
51
+ int64_t i;
52
+
53
+ if (attr == NULL) {
54
+ return;
55
+ }
56
+
57
+ ndt_free(attr->name);
58
+
59
+ switch (attr->tag) {
60
+ case AttrValue:
61
+ ndt_free(attr->AttrValue);
62
+ ndt_free(attr);
63
+ return;
64
+ case AttrList:
65
+ for (i = 0; i < attr->AttrList.len; i++) {
66
+ ndt_free(attr->AttrList.items[i]);
67
+ }
68
+ ndt_free(attr->AttrList.items);
69
+ ndt_free(attr);
70
+ return;
71
+ }
72
+
73
+ /* NOT REACHED: tags should be exhaustive. */
74
+ ndt_internal_error("invalid attribute");
75
+ }
76
+
77
+ void
78
+ ndt_attr_array_del(ndt_attr_t *attr, int64_t nattr)
79
+ {
80
+ int64_t i, k;
81
+
82
+ if (attr == NULL) {
83
+ return;
84
+ }
85
+
86
+ for (i = 0; i < nattr; i++) {
87
+ ndt_free(attr[i].name);
88
+
89
+ switch (attr[i].tag) {
90
+ case AttrValue:
91
+ ndt_free(attr[i].AttrValue);
92
+ continue;
93
+ case AttrList:
94
+ for (k = 0; k < attr[i].AttrList.len; k++) {
95
+ ndt_free(attr[i].AttrList.items[k]);
96
+ }
97
+ ndt_free(attr[i].AttrList.items);
98
+ continue;
99
+ }
100
+
101
+ /* NOT REACHED: tags should be exhaustive. */
102
+ ndt_internal_error("invalid attribute");
103
+ }
104
+
105
+ ndt_free(attr);
106
+ }
107
+
108
+ /* Attribute sequences */
109
+ NDT_SEQ_NEW(ndt_attr)
110
+ NDT_SEQ_DEL(ndt_attr)
111
+ NDT_SEQ_GROW(ndt_attr)
112
+ NDT_SEQ_APPEND(ndt_attr)
113
+ NDT_SEQ_FINALIZE(ndt_attr)
114
+
115
+
116
+ int
117
+ ndt_parse_attr(const attr_spec *spec, ndt_context_t *ctx,
118
+ const ndt_attr_seq_t *seq, ...)
119
+ {
120
+ va_list ap;
121
+ ndt_attr_t const *v[MAX_ATTR] = {NULL};
122
+ int found;
123
+ int64_t i, k;
124
+
125
+ if (seq->len < spec->min || seq->len > spec->max) {
126
+ ndt_err_format(ctx, NDT_InvalidArgumentError,
127
+ "too %s arguments", seq->len < spec->min ? "few" : "many");
128
+ return -1;
129
+ }
130
+
131
+ /* Quadratic time algorithm, we have a small number of args. */
132
+ for (i = 0; i < seq->len; i++) {
133
+ found = 0;
134
+ for (k = 0; k < spec->max; k++) {
135
+ if (strcmp(seq->ptr[i].name, spec->names[k]) == 0) {
136
+ if (v[k]) {
137
+ ndt_err_format(ctx, NDT_InvalidArgumentError,
138
+ "duplicate argument: %s", spec->names[k]);
139
+ return -1;
140
+ }
141
+ found = 1;
142
+ v[k] = &seq->ptr[i];
143
+ }
144
+ }
145
+ if (!found) {
146
+ ndt_err_format(ctx, NDT_InvalidArgumentError,
147
+ "invalid argument: %s", seq->ptr[i].name);
148
+ return -1;
149
+ }
150
+ }
151
+
152
+ for (i = 0; i < spec->min; i++) {
153
+ if (v[i] == NULL) {
154
+ ndt_err_format(ctx, NDT_InvalidArgumentError,
155
+ "missing required keyword: '%s'", spec->names[i]);
156
+ return -1;
157
+ }
158
+ }
159
+
160
+ va_start(ap, seq);
161
+ for (i = 0; i < spec->max; i++) {
162
+ void *ptr = va_arg(ap, void *);
163
+ if (v[i] == NULL) {
164
+ continue;
165
+ }
166
+
167
+ switch(spec->tags[i]) {
168
+ case AttrBool:
169
+ *(bool *)ptr = ndt_strtobool(v[i]->AttrValue, ctx);
170
+ goto endloop;
171
+ case AttrChar:
172
+ *(char *)ptr = ndt_strtochar(v[i]->AttrValue, ctx);
173
+ goto endloop;
174
+ case AttrInt8:
175
+ *(int8_t *)ptr = (int8_t)ndt_strtol(v[i]->AttrValue, INT8_MIN, INT8_MAX, ctx);
176
+ goto endloop;
177
+ case AttrInt16:
178
+ *(int16_t *)ptr = (int16_t)ndt_strtol(v[i]->AttrValue, INT16_MIN, INT16_MAX, ctx);
179
+ goto endloop;
180
+ case AttrInt32:
181
+ *(int32_t *)ptr = (int32_t)ndt_strtol(v[i]->AttrValue, INT32_MIN, INT32_MAX, ctx);
182
+ goto endloop;
183
+ case AttrInt64:
184
+ *(int64_t *)ptr = (int64_t)ndt_strtoll(v[i]->AttrValue, INT64_MIN, INT64_MAX, ctx);
185
+ goto endloop;
186
+ case AttrUint8:
187
+ *(uint8_t *)ptr = (uint8_t)ndt_strtoul(v[i]->AttrValue, UINT8_MAX, ctx);
188
+ goto endloop;
189
+ case AttrUint16:
190
+ *(uint16_t *)ptr = (uint16_t)ndt_strtoul(v[i]->AttrValue, UINT16_MAX, ctx);
191
+ goto endloop;
192
+ case AttrUint32:
193
+ *(uint32_t *)ptr = (uint32_t)ndt_strtoul(v[i]->AttrValue, UINT32_MAX, ctx);
194
+ goto endloop;
195
+ case AttrUint64:
196
+ *(uint64_t *)ptr = (uint64_t)ndt_strtoull(v[i]->AttrValue, UINT64_MAX, ctx);
197
+ goto endloop;
198
+ case AttrSize:
199
+ *(size_t *)ptr = (size_t)ndt_strtoull(v[i]->AttrValue, SIZE_MAX, ctx);
200
+ goto endloop;
201
+ case AttrFloat32:
202
+ *(float *)ptr = ndt_strtof(v[i]->AttrValue, ctx);
203
+ goto endloop;
204
+ case AttrFloat64:
205
+ *(double *)ptr = ndt_strtod(v[i]->AttrValue, ctx);
206
+ goto endloop;
207
+
208
+ case AttrCharOpt:
209
+ ((char_opt_t *)ptr)->tag = Some;
210
+ ((char_opt_t *)ptr)->Some = ndt_strtochar(v[i]->AttrValue, ctx);
211
+ goto endloop;
212
+
213
+ case AttrInt64Opt:
214
+ ((int64_opt_t *)ptr)->tag = Some;
215
+ ((int64_opt_t *)ptr)->Some = (int64_t)ndt_strtoll(v[i]->AttrValue, INT64_MIN, INT64_MAX, ctx);
216
+ goto endloop;
217
+
218
+ case AttrUint16Opt:
219
+ ((uint16_opt_t *)ptr)->tag = Some;
220
+ ((uint16_opt_t *)ptr)->Some = (uint16_t)ndt_strtoul(v[i]->AttrValue, UINT16_MAX, ctx);
221
+ goto endloop;
222
+
223
+ case AttrString: {
224
+ char *value = ndt_strdup(v[i]->AttrValue, ctx);
225
+ if (value == NULL) {
226
+ return -1;
227
+ }
228
+ *(char **)ptr = value;
229
+ goto endloop;
230
+ }
231
+
232
+ case AttrInt32List: {
233
+ int32_t *values = ndt_alloc(v[i]->AttrList.len, sizeof(int32_t));
234
+
235
+ if (values == NULL) {
236
+ ndt_err_format(ctx, NDT_MemoryError, "out of memory");
237
+ return -1;
238
+ }
239
+
240
+ for (k = 0; k < v[i]->AttrList.len; k++) {
241
+ values[k] = (int32_t)ndt_strtol(v[i]->AttrList.items[k], 0, INT32_MAX, ctx);
242
+ if (ndt_err_occurred(ctx)) {
243
+ ndt_free(values);
244
+ return -1;
245
+ }
246
+ }
247
+
248
+ *(int32_t **)ptr = values;
249
+
250
+ ptr = va_arg(ap, void *);
251
+ *(int64_t *)ptr = v[i]->AttrList.len;
252
+ i++;
253
+ goto endloop;
254
+ }
255
+ }
256
+
257
+ /* NOT REACHED: tags should be exhaustive */
258
+ ndt_err_format(ctx, NDT_RuntimeError, "invalid attribute tag", v[i]);
259
+
260
+ endloop:
261
+ if (ndt_err_occurred(ctx)) {
262
+ return -1;
263
+ }
264
+ }
265
+ va_end(ap);
266
+
267
+ return 0;
268
+ }