prism 1.8.1 → 2.0.0.rc1

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 (241) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +90 -2
  3. data/Makefile +41 -12
  4. data/README.md +35 -97
  5. data/config.yml +174 -163
  6. data/docs/build_system.md +2 -2
  7. data/docs/configuration.md +5 -4
  8. data/docs/encoding.md +2 -2
  9. data/docs/fuzzing.md +4 -11
  10. data/docs/javascript.md +19 -4
  11. data/docs/mapping.md +1 -1
  12. data/docs/releasing.md +77 -5
  13. data/docs/relocation.md +2 -4
  14. data/docs/ripper_translation.md +8 -17
  15. data/docs/serialization.md +21 -24
  16. data/docs/testing.md +46 -29
  17. data/ext/prism/api_node.c +1461 -1401
  18. data/ext/prism/extconf.rb +3 -4
  19. data/ext/prism/extension.c +778 -422
  20. data/ext/prism/extension.h +3 -3
  21. data/include/prism/arena.h +37 -0
  22. data/include/prism/ast.h +2729 -612
  23. data/include/prism/buffer.h +52 -0
  24. data/include/prism/comments.h +55 -0
  25. data/include/prism/compiler/accel.h +24 -0
  26. data/include/prism/compiler/align.h +36 -0
  27. data/include/prism/compiler/assume.h +25 -0
  28. data/include/prism/compiler/exported.h +24 -0
  29. data/include/prism/compiler/fallthrough.h +22 -0
  30. data/include/prism/compiler/filesystem.h +32 -0
  31. data/include/prism/compiler/flex_array.h +19 -0
  32. data/include/prism/compiler/force_inline.h +21 -0
  33. data/include/prism/compiler/format.h +25 -0
  34. data/include/prism/compiler/inline.h +17 -0
  35. data/include/prism/compiler/nodiscard.h +22 -0
  36. data/include/prism/compiler/nonnull.h +18 -0
  37. data/include/prism/compiler/unused.h +18 -0
  38. data/include/prism/constant_pool.h +81 -0
  39. data/include/prism/diagnostic.h +38 -405
  40. data/include/prism/errors_format.h +53 -0
  41. data/include/prism/excludes.h +29 -0
  42. data/include/prism/integer.h +41 -0
  43. data/include/prism/internal/allocator.h +68 -0
  44. data/include/prism/internal/allocator_debug.h +88 -0
  45. data/include/prism/internal/arena.h +108 -0
  46. data/include/prism/internal/bit.h +42 -0
  47. data/include/prism/internal/buffer.h +91 -0
  48. data/include/prism/internal/char.h +145 -0
  49. data/include/prism/internal/comments.h +53 -0
  50. data/include/prism/internal/constant_pool.h +152 -0
  51. data/include/prism/internal/diagnostic.h +389 -0
  52. data/include/prism/{encoding.h → internal/encoding.h} +27 -68
  53. data/include/prism/internal/integer.h +68 -0
  54. data/include/prism/internal/isinf.h +16 -0
  55. data/include/prism/internal/line_offset_list.h +34 -0
  56. data/include/prism/{util/pm_list.h → internal/list.h} +11 -52
  57. data/include/prism/internal/magic_comments.h +23 -0
  58. data/include/prism/internal/memchr.h +15 -0
  59. data/include/prism/internal/node.h +38 -0
  60. data/include/prism/internal/options.h +232 -0
  61. data/include/prism/internal/parser.h +984 -0
  62. data/include/prism/internal/regexp.h +26 -0
  63. data/include/prism/internal/serialize.h +39 -0
  64. data/include/prism/internal/source.h +72 -0
  65. data/include/prism/{static_literals.h → internal/static_literals.h} +26 -47
  66. data/include/prism/internal/stringy.h +35 -0
  67. data/include/prism/{util/pm_strncasecmp.h → internal/strncasecmp.h} +3 -17
  68. data/include/prism/{util/pm_strpbrk.h → internal/strpbrk.h} +7 -20
  69. data/include/prism/internal/tokens.h +11 -0
  70. data/include/prism/json.h +32 -0
  71. data/include/prism/line_offset_list.h +61 -0
  72. data/include/prism/magic_comments.h +35 -0
  73. data/include/prism/node.h +18 -53
  74. data/include/prism/options.h +146 -280
  75. data/include/prism/parser.h +336 -868
  76. data/include/prism/prettyprint.h +6 -9
  77. data/include/prism/serialize.h +115 -0
  78. data/include/prism/source.h +155 -0
  79. data/include/prism/stream.h +28 -0
  80. data/include/prism/string_query.h +63 -0
  81. data/include/prism/stringy.h +72 -0
  82. data/include/prism/version.h +13 -4
  83. data/include/prism.h +43 -320
  84. data/lib/prism/compiler.rb +319 -305
  85. data/lib/prism/desugar_compiler.rb +105 -34
  86. data/lib/prism/dispatcher.rb +646 -473
  87. data/lib/prism/dot_visitor.rb +592 -511
  88. data/lib/prism/dsl.rb +391 -25
  89. data/lib/prism/ffi.rb +252 -90
  90. data/lib/prism/inspect_visitor.rb +357 -325
  91. data/lib/prism/lex_compat.rb +311 -228
  92. data/lib/prism/mutation_compiler.rb +313 -305
  93. data/lib/prism/node.rb +20284 -7038
  94. data/lib/prism/node_ext.rb +180 -187
  95. data/lib/prism/node_find.rb +215 -0
  96. data/lib/prism/parse_result/errors.rb +7 -1
  97. data/lib/prism/parse_result/newlines.rb +276 -8
  98. data/lib/prism/parse_result.rb +462 -139
  99. data/lib/prism/pattern.rb +65 -20
  100. data/lib/prism/reflection.rb +23 -12
  101. data/lib/prism/relocation.rb +149 -87
  102. data/lib/prism/serialize.rb +3442 -536
  103. data/lib/prism/string_query.rb +16 -1
  104. data/lib/prism/translation/parser/builder.rb +10 -2
  105. data/lib/prism/translation/parser/compiler.rb +44 -52
  106. data/lib/prism/translation/parser/lexer.rb +41 -92
  107. data/lib/prism/translation/parser.rb +1 -1
  108. data/lib/prism/translation/parser_current.rb +1 -1
  109. data/lib/prism/translation/ripper/filter.rb +53 -0
  110. data/lib/prism/translation/ripper/lexer.rb +92 -5
  111. data/lib/prism/translation/ripper/sexp.rb +2 -10
  112. data/lib/prism/translation/ripper/shim.rb +2 -0
  113. data/lib/prism/translation/ripper.rb +1005 -224
  114. data/lib/prism/translation/ruby_parser.rb +73 -316
  115. data/lib/prism/translation.rb +2 -0
  116. data/lib/prism/visitor.rb +632 -5
  117. data/lib/prism.rb +79 -22
  118. data/prism.gemspec +123 -61
  119. data/rbi/generated/prism/compiler.rbi +489 -0
  120. data/rbi/generated/prism/desugar_compiler.rbi +306 -0
  121. data/rbi/generated/prism/dispatcher.rbi +1138 -0
  122. data/rbi/generated/prism/dot_visitor.rbi +618 -0
  123. data/rbi/generated/prism/dsl.rbi +751 -0
  124. data/rbi/generated/prism/inspect_visitor.rbi +503 -0
  125. data/rbi/generated/prism/lex_compat.rbi +162 -0
  126. data/rbi/generated/prism/mutation_compiler.rbi +464 -0
  127. data/rbi/generated/prism/node.rbi +13681 -0
  128. data/rbi/generated/prism/node_ext.rbi +259 -0
  129. data/rbi/generated/prism/node_find.rbi +79 -0
  130. data/rbi/generated/prism/parse_result/errors.rbi +21 -0
  131. data/rbi/generated/prism/parse_result/newlines.rbi +208 -0
  132. data/rbi/generated/prism/parse_result.rbi +764 -0
  133. data/rbi/generated/prism/pattern.rbi +134 -0
  134. data/rbi/generated/prism/reflection.rbi +89 -0
  135. data/rbi/generated/prism/relocation.rbi +329 -0
  136. data/rbi/generated/prism/serialize.rbi +181 -0
  137. data/rbi/generated/prism/string_query.rbi +36 -0
  138. data/rbi/generated/prism/translation.rbi +7 -0
  139. data/rbi/generated/prism/visitor.rbi +650 -0
  140. data/rbi/generated/prism.rbi +86 -0
  141. data/rbi/prism/translation/parser_versions.rbi +5 -0
  142. data/rbi/rubyvm/node_find.rbi +14 -0
  143. data/sig/generated/prism/compiler.rbs +492 -0
  144. data/sig/generated/prism/desugar_compiler.rbs +305 -0
  145. data/sig/generated/prism/dispatcher.rbs +1292 -0
  146. data/sig/generated/prism/dot_visitor.rbs +635 -0
  147. data/sig/generated/prism/dsl.rbs +928 -0
  148. data/sig/generated/prism/inspect_visitor.rbs +509 -0
  149. data/sig/generated/prism/lex_compat.rbs +166 -0
  150. data/sig/{prism → generated/prism}/mutation_compiler.rbs +309 -4
  151. data/sig/generated/prism/node.rbs +20160 -0
  152. data/sig/generated/prism/node_ext.rbs +290 -0
  153. data/sig/generated/prism/node_find.rbs +90 -0
  154. data/sig/generated/prism/parse_result/errors.rbs +22 -0
  155. data/sig/generated/prism/parse_result/newlines.rbs +223 -0
  156. data/sig/generated/prism/parse_result.rbs +859 -0
  157. data/sig/generated/prism/pattern.rbs +156 -0
  158. data/sig/generated/prism/reflection.rbs +101 -0
  159. data/sig/generated/prism/relocation.rbs +428 -0
  160. data/sig/generated/prism/serialize.rbs +195 -0
  161. data/sig/generated/prism/string_query.rbs +36 -0
  162. data/sig/generated/prism/translation.rbs +8 -0
  163. data/sig/generated/prism/visitor.rbs +1111 -0
  164. data/sig/generated/prism.rbs +84 -0
  165. data/sig/manifest.yaml +2 -0
  166. data/src/arena.c +117 -0
  167. data/src/{util/pm_buffer.c → buffer.c} +44 -21
  168. data/src/{util/pm_char.c → char.c} +28 -57
  169. data/src/comments.c +454 -0
  170. data/src/constant_pool.c +406 -0
  171. data/src/diagnostic.c +93 -65
  172. data/src/encoding.c +8 -3
  173. data/src/errors_format.c +519 -0
  174. data/src/{util/pm_integer.c → integer.c} +33 -22
  175. data/src/json.c +5733 -0
  176. data/src/line_offset_list.c +100 -0
  177. data/src/list.c +24 -0
  178. data/src/{util/pm_memchr.c → memchr.c} +8 -6
  179. data/src/node.c +3504 -7674
  180. data/src/options.c +162 -49
  181. data/src/parser.c +349 -0
  182. data/src/prettyprint.c +421 -354
  183. data/src/prism.c +8035 -7019
  184. data/src/regexp.c +961 -45
  185. data/src/serialize.c +459 -424
  186. data/src/source.c +518 -0
  187. data/src/static_literals.c +111 -43
  188. data/src/string_query.c +166 -0
  189. data/src/stringy.c +91 -0
  190. data/src/{util/pm_strncasecmp.c → strncasecmp.c} +5 -4
  191. data/src/strpbrk.c +447 -0
  192. data/src/{token_type.c → tokens.c} +39 -18
  193. metadata +123 -62
  194. data/ext/prism/api_pack.c +0 -276
  195. data/include/prism/defines.h +0 -260
  196. data/include/prism/pack.h +0 -163
  197. data/include/prism/regexp.h +0 -43
  198. data/include/prism/util/pm_buffer.h +0 -236
  199. data/include/prism/util/pm_char.h +0 -204
  200. data/include/prism/util/pm_constant_pool.h +0 -218
  201. data/include/prism/util/pm_integer.h +0 -130
  202. data/include/prism/util/pm_memchr.h +0 -29
  203. data/include/prism/util/pm_newline_list.h +0 -113
  204. data/include/prism/util/pm_string.h +0 -200
  205. data/lib/prism/lex_ripper.rb +0 -64
  206. data/lib/prism/pack.rb +0 -230
  207. data/lib/prism/parse_result/comments.rb +0 -188
  208. data/rbi/prism/compiler.rbi +0 -12
  209. data/rbi/prism/dsl.rbi +0 -524
  210. data/rbi/prism/inspect_visitor.rbi +0 -12
  211. data/rbi/prism/node.rbi +0 -8747
  212. data/rbi/prism/node_ext.rbi +0 -107
  213. data/rbi/prism/parse_result.rbi +0 -404
  214. data/rbi/prism/reflection.rbi +0 -58
  215. data/rbi/prism/string_query.rbi +0 -12
  216. data/rbi/prism/visitor.rbi +0 -473
  217. data/rbi/prism.rbi +0 -66
  218. data/sig/prism/compiler.rbs +0 -9
  219. data/sig/prism/dispatcher.rbs +0 -19
  220. data/sig/prism/dot_visitor.rbs +0 -6
  221. data/sig/prism/dsl.rbs +0 -351
  222. data/sig/prism/inspect_visitor.rbs +0 -22
  223. data/sig/prism/lex_compat.rbs +0 -10
  224. data/sig/prism/node.rbs +0 -4039
  225. data/sig/prism/node_ext.rbs +0 -149
  226. data/sig/prism/pack.rbs +0 -43
  227. data/sig/prism/parse_result/comments.rbs +0 -38
  228. data/sig/prism/parse_result.rbs +0 -196
  229. data/sig/prism/pattern.rbs +0 -13
  230. data/sig/prism/reflection.rbs +0 -50
  231. data/sig/prism/relocation.rbs +0 -185
  232. data/sig/prism/serialize.rbs +0 -8
  233. data/sig/prism/string_query.rbs +0 -11
  234. data/sig/prism/visitor.rbs +0 -169
  235. data/sig/prism.rbs +0 -258
  236. data/src/pack.c +0 -509
  237. data/src/util/pm_constant_pool.c +0 -342
  238. data/src/util/pm_list.c +0 -49
  239. data/src/util/pm_newline_list.c +0 -125
  240. data/src/util/pm_string.c +0 -381
  241. data/src/util/pm_strpbrk.c +0 -206
@@ -1,342 +0,0 @@
1
- #include "prism/util/pm_constant_pool.h"
2
-
3
- /**
4
- * Initialize a list of constant ids.
5
- */
6
- void
7
- pm_constant_id_list_init(pm_constant_id_list_t *list) {
8
- list->ids = NULL;
9
- list->size = 0;
10
- list->capacity = 0;
11
- }
12
-
13
- /**
14
- * Initialize a list of constant ids with a given capacity.
15
- */
16
- void
17
- pm_constant_id_list_init_capacity(pm_constant_id_list_t *list, size_t capacity) {
18
- if (capacity) {
19
- list->ids = xcalloc(capacity, sizeof(pm_constant_id_t));
20
- if (list->ids == NULL) abort();
21
- } else {
22
- list->ids = NULL;
23
- }
24
-
25
- list->size = 0;
26
- list->capacity = capacity;
27
- }
28
-
29
- /**
30
- * Append a constant id to a list of constant ids. Returns false if any
31
- * potential reallocations fail.
32
- */
33
- bool
34
- pm_constant_id_list_append(pm_constant_id_list_t *list, pm_constant_id_t id) {
35
- if (list->size >= list->capacity) {
36
- list->capacity = list->capacity == 0 ? 8 : list->capacity * 2;
37
- list->ids = (pm_constant_id_t *) xrealloc(list->ids, sizeof(pm_constant_id_t) * list->capacity);
38
- if (list->ids == NULL) return false;
39
- }
40
-
41
- list->ids[list->size++] = id;
42
- return true;
43
- }
44
-
45
- /**
46
- * Insert a constant id into a list of constant ids at the specified index.
47
- */
48
- void
49
- pm_constant_id_list_insert(pm_constant_id_list_t *list, size_t index, pm_constant_id_t id) {
50
- assert(index < list->capacity);
51
- assert(list->ids[index] == PM_CONSTANT_ID_UNSET);
52
-
53
- list->ids[index] = id;
54
- list->size++;
55
- }
56
-
57
- /**
58
- * Checks if the current constant id list includes the given constant id.
59
- */
60
- bool
61
- pm_constant_id_list_includes(pm_constant_id_list_t *list, pm_constant_id_t id) {
62
- for (size_t index = 0; index < list->size; index++) {
63
- if (list->ids[index] == id) return true;
64
- }
65
- return false;
66
- }
67
-
68
- /**
69
- * Free the memory associated with a list of constant ids.
70
- */
71
- void
72
- pm_constant_id_list_free(pm_constant_id_list_t *list) {
73
- if (list->ids != NULL) {
74
- xfree(list->ids);
75
- }
76
- }
77
-
78
- /**
79
- * A relatively simple hash function (djb2) that is used to hash strings. We are
80
- * optimizing here for simplicity and speed.
81
- */
82
- static inline uint32_t
83
- pm_constant_pool_hash(const uint8_t *start, size_t length) {
84
- // This is a prime number used as the initial value for the hash function.
85
- uint32_t value = 5381;
86
-
87
- for (size_t index = 0; index < length; index++) {
88
- value = ((value << 5) + value) + start[index];
89
- }
90
-
91
- return value;
92
- }
93
-
94
- /**
95
- * https://graphics.stanford.edu/~seander/bithacks.html#RoundUpPowerOf2
96
- */
97
- static uint32_t
98
- next_power_of_two(uint32_t v) {
99
- // Avoid underflow in subtraction on next line.
100
- if (v == 0) {
101
- // 1 is the nearest power of 2 to 0 (2^0)
102
- return 1;
103
- }
104
- v--;
105
- v |= v >> 1;
106
- v |= v >> 2;
107
- v |= v >> 4;
108
- v |= v >> 8;
109
- v |= v >> 16;
110
- v++;
111
- return v;
112
- }
113
-
114
- #ifndef NDEBUG
115
- static bool
116
- is_power_of_two(uint32_t size) {
117
- return (size & (size - 1)) == 0;
118
- }
119
- #endif
120
-
121
- /**
122
- * Resize a constant pool to a given capacity.
123
- */
124
- static inline bool
125
- pm_constant_pool_resize(pm_constant_pool_t *pool) {
126
- assert(is_power_of_two(pool->capacity));
127
-
128
- uint32_t next_capacity = pool->capacity * 2;
129
- if (next_capacity < pool->capacity) return false;
130
-
131
- const uint32_t mask = next_capacity - 1;
132
- const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
133
-
134
- void *next = xcalloc(next_capacity, element_size);
135
- if (next == NULL) return false;
136
-
137
- pm_constant_pool_bucket_t *next_buckets = next;
138
- pm_constant_t *next_constants = (void *)(((char *) next) + next_capacity * sizeof(pm_constant_pool_bucket_t));
139
-
140
- // For each bucket in the current constant pool, find the index in the
141
- // next constant pool, and insert it.
142
- for (uint32_t index = 0; index < pool->capacity; index++) {
143
- pm_constant_pool_bucket_t *bucket = &pool->buckets[index];
144
-
145
- // If an id is set on this constant, then we know we have content here.
146
- // In this case we need to insert it into the next constant pool.
147
- if (bucket->id != PM_CONSTANT_ID_UNSET) {
148
- uint32_t next_index = bucket->hash & mask;
149
-
150
- // This implements linear scanning to find the next available slot
151
- // in case this index is already taken. We don't need to bother
152
- // comparing the values since we know that the hash is unique.
153
- while (next_buckets[next_index].id != PM_CONSTANT_ID_UNSET) {
154
- next_index = (next_index + 1) & mask;
155
- }
156
-
157
- // Here we copy over the entire bucket, which includes the id so
158
- // that they are consistent between resizes.
159
- next_buckets[next_index] = *bucket;
160
- }
161
- }
162
-
163
- // The constants are stable with respect to hash table resizes.
164
- memcpy(next_constants, pool->constants, pool->size * sizeof(pm_constant_t));
165
-
166
- // pool->constants and pool->buckets are allocated out of the same chunk
167
- // of memory, with the buckets coming first.
168
- xfree(pool->buckets);
169
- pool->constants = next_constants;
170
- pool->buckets = next_buckets;
171
- pool->capacity = next_capacity;
172
- return true;
173
- }
174
-
175
- /**
176
- * Initialize a new constant pool with a given capacity.
177
- */
178
- bool
179
- pm_constant_pool_init(pm_constant_pool_t *pool, uint32_t capacity) {
180
- const uint32_t maximum = (~((uint32_t) 0));
181
- if (capacity >= ((maximum / 2) + 1)) return false;
182
-
183
- capacity = next_power_of_two(capacity);
184
- const size_t element_size = sizeof(pm_constant_pool_bucket_t) + sizeof(pm_constant_t);
185
- void *memory = xcalloc(capacity, element_size);
186
- if (memory == NULL) return false;
187
-
188
- pool->buckets = memory;
189
- pool->constants = (void *)(((char *)memory) + capacity * sizeof(pm_constant_pool_bucket_t));
190
- pool->size = 0;
191
- pool->capacity = capacity;
192
- return true;
193
- }
194
-
195
- /**
196
- * Return a pointer to the constant indicated by the given constant id.
197
- */
198
- pm_constant_t *
199
- pm_constant_pool_id_to_constant(const pm_constant_pool_t *pool, pm_constant_id_t constant_id) {
200
- assert(constant_id != PM_CONSTANT_ID_UNSET && constant_id <= pool->size);
201
- return &pool->constants[constant_id - 1];
202
- }
203
-
204
- /**
205
- * Find a constant in a constant pool. Returns the id of the constant, or 0 if
206
- * the constant is not found.
207
- */
208
- pm_constant_id_t
209
- pm_constant_pool_find(const pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
210
- assert(is_power_of_two(pool->capacity));
211
- const uint32_t mask = pool->capacity - 1;
212
-
213
- uint32_t hash = pm_constant_pool_hash(start, length);
214
- uint32_t index = hash & mask;
215
- pm_constant_pool_bucket_t *bucket;
216
-
217
- while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
218
- pm_constant_t *constant = &pool->constants[bucket->id - 1];
219
- if ((constant->length == length) && memcmp(constant->start, start, length) == 0) {
220
- return bucket->id;
221
- }
222
-
223
- index = (index + 1) & mask;
224
- }
225
-
226
- return PM_CONSTANT_ID_UNSET;
227
- }
228
-
229
- /**
230
- * Insert a constant into a constant pool and return its index in the pool.
231
- */
232
- static inline pm_constant_id_t
233
- pm_constant_pool_insert(pm_constant_pool_t *pool, const uint8_t *start, size_t length, pm_constant_pool_bucket_type_t type) {
234
- if (pool->size >= (pool->capacity / 4 * 3)) {
235
- if (!pm_constant_pool_resize(pool)) return PM_CONSTANT_ID_UNSET;
236
- }
237
-
238
- assert(is_power_of_two(pool->capacity));
239
- const uint32_t mask = pool->capacity - 1;
240
-
241
- uint32_t hash = pm_constant_pool_hash(start, length);
242
- uint32_t index = hash & mask;
243
- pm_constant_pool_bucket_t *bucket;
244
-
245
- while (bucket = &pool->buckets[index], bucket->id != PM_CONSTANT_ID_UNSET) {
246
- // If there is a collision, then we need to check if the content is the
247
- // same as the content we are trying to insert. If it is, then we can
248
- // return the id of the existing constant.
249
- pm_constant_t *constant = &pool->constants[bucket->id - 1];
250
-
251
- if ((constant->length == length) && memcmp(constant->start, start, length) == 0) {
252
- // Since we have found a match, we need to check if this is
253
- // attempting to insert a shared or an owned constant. We want to
254
- // prefer shared constants since they don't require allocations.
255
- if (type == PM_CONSTANT_POOL_BUCKET_OWNED) {
256
- // If we're attempting to insert an owned constant and we have
257
- // an existing constant, then either way we don't want the given
258
- // memory. Either it's duplicated with the existing constant or
259
- // it's not necessary because we have a shared version.
260
- xfree((void *) start);
261
- } else if (bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
262
- // If we're attempting to insert a shared constant and the
263
- // existing constant is owned, then we can free the owned
264
- // constant and replace it with the shared constant.
265
- xfree((void *) constant->start);
266
- constant->start = start;
267
- bucket->type = (unsigned int) (type & 0x3);
268
- }
269
-
270
- return bucket->id;
271
- }
272
-
273
- index = (index + 1) & mask;
274
- }
275
-
276
- // IDs are allocated starting at 1, since the value 0 denotes a non-existent
277
- // constant.
278
- uint32_t id = ++pool->size;
279
- assert(pool->size < ((uint32_t) (1 << 30)));
280
-
281
- *bucket = (pm_constant_pool_bucket_t) {
282
- .id = (unsigned int) (id & 0x3fffffff),
283
- .type = (unsigned int) (type & 0x3),
284
- .hash = hash
285
- };
286
-
287
- pool->constants[id - 1] = (pm_constant_t) {
288
- .start = start,
289
- .length = length,
290
- };
291
-
292
- return id;
293
- }
294
-
295
- /**
296
- * Insert a constant into a constant pool. Returns the id of the constant, or
297
- * PM_CONSTANT_ID_UNSET if any potential calls to resize fail.
298
- */
299
- pm_constant_id_t
300
- pm_constant_pool_insert_shared(pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
301
- return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_DEFAULT);
302
- }
303
-
304
- /**
305
- * Insert a constant into a constant pool from memory that is now owned by the
306
- * constant pool. Returns the id of the constant, or PM_CONSTANT_ID_UNSET if any
307
- * potential calls to resize fail.
308
- */
309
- pm_constant_id_t
310
- pm_constant_pool_insert_owned(pm_constant_pool_t *pool, uint8_t *start, size_t length) {
311
- return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_OWNED);
312
- }
313
-
314
- /**
315
- * Insert a constant into a constant pool from memory that is constant. Returns
316
- * the id of the constant, or PM_CONSTANT_ID_UNSET if any potential calls to
317
- * resize fail.
318
- */
319
- pm_constant_id_t
320
- pm_constant_pool_insert_constant(pm_constant_pool_t *pool, const uint8_t *start, size_t length) {
321
- return pm_constant_pool_insert(pool, start, length, PM_CONSTANT_POOL_BUCKET_CONSTANT);
322
- }
323
-
324
- /**
325
- * Free the memory associated with a constant pool.
326
- */
327
- void
328
- pm_constant_pool_free(pm_constant_pool_t *pool) {
329
- // For each constant in the current constant pool, free the contents if the
330
- // contents are owned.
331
- for (uint32_t index = 0; index < pool->capacity; index++) {
332
- pm_constant_pool_bucket_t *bucket = &pool->buckets[index];
333
-
334
- // If an id is set on this constant, then we know we have content here.
335
- if (bucket->id != PM_CONSTANT_ID_UNSET && bucket->type == PM_CONSTANT_POOL_BUCKET_OWNED) {
336
- pm_constant_t *constant = &pool->constants[bucket->id - 1];
337
- xfree((void *) constant->start);
338
- }
339
- }
340
-
341
- xfree(pool->buckets);
342
- }
data/src/util/pm_list.c DELETED
@@ -1,49 +0,0 @@
1
- #include "prism/util/pm_list.h"
2
-
3
- /**
4
- * Returns true if the given list is empty.
5
- */
6
- PRISM_EXPORTED_FUNCTION bool
7
- pm_list_empty_p(pm_list_t *list) {
8
- return list->head == NULL;
9
- }
10
-
11
- /**
12
- * Returns the size of the list.
13
- */
14
- PRISM_EXPORTED_FUNCTION size_t
15
- pm_list_size(pm_list_t *list) {
16
- return list->size;
17
- }
18
-
19
- /**
20
- * Append a node to the given list.
21
- */
22
- void
23
- pm_list_append(pm_list_t *list, pm_list_node_t *node) {
24
- if (list->head == NULL) {
25
- list->head = node;
26
- } else {
27
- list->tail->next = node;
28
- }
29
-
30
- list->tail = node;
31
- list->size++;
32
- }
33
-
34
- /**
35
- * Deallocate the internal state of the given list.
36
- */
37
- PRISM_EXPORTED_FUNCTION void
38
- pm_list_free(pm_list_t *list) {
39
- pm_list_node_t *node = list->head;
40
- pm_list_node_t *next;
41
-
42
- while (node != NULL) {
43
- next = node->next;
44
- xfree(node);
45
- node = next;
46
- }
47
-
48
- list->size = 0;
49
- }
@@ -1,125 +0,0 @@
1
- #include "prism/util/pm_newline_list.h"
2
-
3
- /**
4
- * Initialize a new newline list with the given capacity. Returns true if the
5
- * allocation of the offsets succeeds, otherwise returns false.
6
- */
7
- bool
8
- pm_newline_list_init(pm_newline_list_t *list, const uint8_t *start, size_t capacity) {
9
- list->offsets = (size_t *) xcalloc(capacity, sizeof(size_t));
10
- if (list->offsets == NULL) return false;
11
-
12
- list->start = start;
13
-
14
- // This is 1 instead of 0 because we want to include the first line of the
15
- // file as having offset 0, which is set because of calloc.
16
- list->size = 1;
17
- list->capacity = capacity;
18
-
19
- return true;
20
- }
21
-
22
- /**
23
- * Clear out the newlines that have been appended to the list.
24
- */
25
- void
26
- pm_newline_list_clear(pm_newline_list_t *list) {
27
- list->size = 1;
28
- }
29
-
30
- /**
31
- * Append a new offset to the newline list. Returns true if the reallocation of
32
- * the offsets succeeds (if one was necessary), otherwise returns false.
33
- */
34
- bool
35
- pm_newline_list_append(pm_newline_list_t *list, const uint8_t *cursor) {
36
- if (list->size == list->capacity) {
37
- size_t *original_offsets = list->offsets;
38
-
39
- list->capacity = (list->capacity * 3) / 2;
40
- list->offsets = (size_t *) xcalloc(list->capacity, sizeof(size_t));
41
- if (list->offsets == NULL) return false;
42
-
43
- memcpy(list->offsets, original_offsets, list->size * sizeof(size_t));
44
- xfree(original_offsets);
45
- }
46
-
47
- assert(*cursor == '\n');
48
- assert(cursor >= list->start);
49
- size_t newline_offset = (size_t) (cursor - list->start + 1);
50
-
51
- assert(list->size == 0 || newline_offset > list->offsets[list->size - 1]);
52
- list->offsets[list->size++] = newline_offset;
53
-
54
- return true;
55
- }
56
-
57
- /**
58
- * Returns the line of the given offset. If the offset is not in the list, the
59
- * line of the closest offset less than the given offset is returned.
60
- */
61
- int32_t
62
- pm_newline_list_line(const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line) {
63
- assert(cursor >= list->start);
64
- size_t offset = (size_t) (cursor - list->start);
65
-
66
- size_t left = 0;
67
- size_t right = list->size - 1;
68
-
69
- while (left <= right) {
70
- size_t mid = left + (right - left) / 2;
71
-
72
- if (list->offsets[mid] == offset) {
73
- return ((int32_t) mid) + start_line;
74
- }
75
-
76
- if (list->offsets[mid] < offset) {
77
- left = mid + 1;
78
- } else {
79
- right = mid - 1;
80
- }
81
- }
82
-
83
- return ((int32_t) left) + start_line - 1;
84
- }
85
-
86
- /**
87
- * Returns the line and column of the given offset. If the offset is not in the
88
- * list, the line and column of the closest offset less than the given offset
89
- * are returned.
90
- */
91
- pm_line_column_t
92
- pm_newline_list_line_column(const pm_newline_list_t *list, const uint8_t *cursor, int32_t start_line) {
93
- assert(cursor >= list->start);
94
- size_t offset = (size_t) (cursor - list->start);
95
-
96
- size_t left = 0;
97
- size_t right = list->size - 1;
98
-
99
- while (left <= right) {
100
- size_t mid = left + (right - left) / 2;
101
-
102
- if (list->offsets[mid] == offset) {
103
- return ((pm_line_column_t) { ((int32_t) mid) + start_line, 0 });
104
- }
105
-
106
- if (list->offsets[mid] < offset) {
107
- left = mid + 1;
108
- } else {
109
- right = mid - 1;
110
- }
111
- }
112
-
113
- return ((pm_line_column_t) {
114
- .line = ((int32_t) left) + start_line - 1,
115
- .column = (uint32_t) (offset - list->offsets[left - 1])
116
- });
117
- }
118
-
119
- /**
120
- * Free the internal memory allocated for the newline list.
121
- */
122
- void
123
- pm_newline_list_free(pm_newline_list_t *list) {
124
- xfree(list->offsets);
125
- }