rbs 3.6.1 → 3.9.5

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 (268) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/comments.yml +2 -2
  3. data/.github/workflows/dependabot.yml +1 -1
  4. data/.github/workflows/ruby.yml +34 -10
  5. data/.github/workflows/windows.yml +20 -3
  6. data/.gitignore +1 -0
  7. data/.rubocop.yml +26 -1
  8. data/CHANGELOG.md +241 -0
  9. data/Rakefile +54 -4
  10. data/config.yml +317 -0
  11. data/core/array.rbs +1756 -1591
  12. data/core/basic_object.rbs +38 -35
  13. data/core/comparable.rbs +1 -1
  14. data/core/complex.rbs +166 -94
  15. data/core/data.rbs +2 -2
  16. data/core/dir.rbs +2 -18
  17. data/core/encoding.rbs +12 -32
  18. data/core/enumerable.rbs +270 -266
  19. data/core/enumerator.rbs +14 -4
  20. data/core/env.rbs +1 -1
  21. data/core/errno.rbs +33 -16
  22. data/core/errors.rbs +6 -2
  23. data/core/exception.rbs +342 -167
  24. data/core/fiber.rbs +3 -2
  25. data/core/file.rbs +26 -75
  26. data/core/float.rbs +125 -72
  27. data/core/gc.rbs +158 -42
  28. data/core/hash.rbs +122 -143
  29. data/core/integer.rbs +79 -50
  30. data/core/io/buffer.rbs +49 -43
  31. data/core/io.rbs +108 -151
  32. data/core/kernel.rbs +341 -209
  33. data/core/match_data.rbs +76 -2
  34. data/core/math.rbs +0 -36
  35. data/core/method.rbs +2 -2
  36. data/core/module.rbs +32 -27
  37. data/core/nil_class.rbs +2 -2
  38. data/core/numeric.rbs +101 -104
  39. data/core/object.rbs +1 -5
  40. data/core/object_space/weak_key_map.rbs +3 -4
  41. data/core/object_space.rbs +3 -3
  42. data/core/proc.rbs +82 -14
  43. data/core/process.rbs +110 -58
  44. data/core/ractor.rbs +57 -4
  45. data/core/range.rbs +114 -87
  46. data/core/rational.rbs +0 -2
  47. data/core/rbs/unnamed/argf.rbs +237 -36
  48. data/core/rbs/unnamed/env_class.rbs +35 -53
  49. data/core/rbs/unnamed/random.rbs +1 -2
  50. data/core/regexp.rbs +10 -56
  51. data/core/ruby_vm.rbs +88 -9
  52. data/core/rubygems/config_file.rbs +3 -0
  53. data/core/rubygems/errors.rbs +3 -6
  54. data/core/rubygems/platform.rbs +0 -9
  55. data/core/rubygems/rubygems.rbs +3 -6
  56. data/core/rubygems/version.rbs +8 -8
  57. data/core/set.rbs +4 -16
  58. data/core/string.rbs +271 -264
  59. data/core/struct.rbs +6 -18
  60. data/core/symbol.rbs +14 -21
  61. data/core/thread.rbs +32 -35
  62. data/core/time.rbs +131 -50
  63. data/core/trace_point.rbs +124 -113
  64. data/core/true_class.rbs +0 -1
  65. data/core/unbound_method.rbs +1 -1
  66. data/core/warning.rbs +9 -2
  67. data/docs/architecture.md +1 -1
  68. data/docs/syntax.md +17 -10
  69. data/ext/rbs_extension/extconf.rb +11 -0
  70. data/ext/rbs_extension/location.c +61 -29
  71. data/ext/rbs_extension/location.h +4 -3
  72. data/ext/rbs_extension/main.c +23 -1
  73. data/ext/rbs_extension/parser.c +506 -517
  74. data/ext/rbs_extension/parserstate.c +109 -30
  75. data/ext/rbs_extension/parserstate.h +6 -4
  76. data/ext/rbs_extension/rbs_extension.h +1 -10
  77. data/{ext/rbs_extension → include/rbs}/constants.h +21 -19
  78. data/include/rbs/ruby_objs.h +72 -0
  79. data/include/rbs/util/rbs_constant_pool.h +219 -0
  80. data/include/rbs.h +7 -0
  81. data/lib/rbs/annotate/annotations.rb +3 -3
  82. data/lib/rbs/annotate/rdoc_source.rb +2 -2
  83. data/lib/rbs/ast/declarations.rb +9 -4
  84. data/lib/rbs/ast/directives.rb +10 -0
  85. data/lib/rbs/ast/members.rb +2 -0
  86. data/lib/rbs/ast/type_param.rb +2 -12
  87. data/lib/rbs/cli/diff.rb +3 -3
  88. data/lib/rbs/cli/validate.rb +2 -1
  89. data/lib/rbs/cli.rb +16 -16
  90. data/lib/rbs/collection/config/lockfile_generator.rb +58 -8
  91. data/lib/rbs/collection/config.rb +5 -3
  92. data/lib/rbs/collection/sources/rubygems.rb +1 -1
  93. data/lib/rbs/collection.rb +1 -0
  94. data/lib/rbs/definition.rb +51 -34
  95. data/lib/rbs/definition_builder/ancestor_builder.rb +5 -3
  96. data/lib/rbs/definition_builder.rb +83 -24
  97. data/lib/rbs/environment.rb +33 -18
  98. data/lib/rbs/environment_loader.rb +6 -1
  99. data/lib/rbs/errors.rb +24 -0
  100. data/lib/rbs/locator.rb +2 -0
  101. data/lib/rbs/method_type.rb +2 -0
  102. data/lib/rbs/namespace.rb +1 -0
  103. data/lib/rbs/parser_aux.rb +40 -3
  104. data/lib/rbs/prototype/rb.rb +20 -12
  105. data/lib/rbs/prototype/rbi.rb +11 -6
  106. data/lib/rbs/prototype/runtime/value_object_generator.rb +7 -5
  107. data/lib/rbs/prototype/runtime.rb +7 -5
  108. data/lib/rbs/subtractor.rb +3 -3
  109. data/lib/rbs/test/hook.rb +47 -42
  110. data/lib/rbs/test/type_check.rb +7 -5
  111. data/lib/rbs/type_name.rb +14 -9
  112. data/lib/rbs/types.rb +63 -14
  113. data/lib/rbs/unit_test/spy.rb +4 -2
  114. data/lib/rbs/unit_test/type_assertions.rb +19 -13
  115. data/lib/rbs/unit_test/with_aliases.rb +3 -1
  116. data/lib/rbs/validator.rb +7 -1
  117. data/lib/rbs/version.rb +1 -1
  118. data/lib/rbs/writer.rb +10 -5
  119. data/lib/rbs.rb +1 -0
  120. data/lib/rdoc_plugin/parser.rb +2 -2
  121. data/rbs.gemspec +6 -2
  122. data/sig/ancestor_graph.rbs +5 -5
  123. data/sig/annotate/rdoc_source.rbs +2 -0
  124. data/sig/cli.rbs +2 -0
  125. data/sig/collection/config/lockfile_generator.rbs +9 -1
  126. data/sig/declarations.rbs +10 -3
  127. data/sig/definition.rbs +80 -12
  128. data/sig/definition_builder.rbs +18 -4
  129. data/sig/directives.rbs +17 -1
  130. data/sig/environment.rbs +3 -1
  131. data/sig/errors.rbs +19 -0
  132. data/sig/namespace.rbs +2 -3
  133. data/sig/parser.rbs +5 -1
  134. data/sig/prototype/rb.rbs +1 -1
  135. data/sig/resolver/constant_resolver.rbs +2 -2
  136. data/sig/resolver/context.rbs +1 -1
  137. data/sig/subtractor.rbs +1 -1
  138. data/sig/test/type_check.rbs +2 -2
  139. data/sig/type_alias_dependency.rbs +2 -2
  140. data/sig/type_alias_regularity.rbs +6 -6
  141. data/sig/type_param.rbs +4 -4
  142. data/sig/typename.rbs +8 -5
  143. data/sig/types.rbs +1 -1
  144. data/sig/unit_test/spy.rbs +2 -0
  145. data/sig/unit_test/type_assertions.rbs +2 -0
  146. data/sig/use_map.rbs +1 -1
  147. data/sig/validator.rbs +6 -2
  148. data/sig/vendorer.rbs +1 -1
  149. data/sig/writer.rbs +1 -1
  150. data/{ext/rbs_extension → src}/constants.c +35 -36
  151. data/src/ruby_objs.c +799 -0
  152. data/src/util/rbs_constant_pool.c +342 -0
  153. data/stdlib/base64/0/base64.rbs +0 -9
  154. data/stdlib/benchmark/0/benchmark.rbs +11 -2
  155. data/stdlib/bigdecimal/0/big_decimal.rbs +26 -182
  156. data/stdlib/cgi/0/core.rbs +60 -3
  157. data/stdlib/cgi/0/manifest.yaml +1 -0
  158. data/stdlib/coverage/0/coverage.rbs +0 -3
  159. data/stdlib/csv/0/csv.rbs +18 -58
  160. data/stdlib/csv/0/manifest.yaml +1 -0
  161. data/stdlib/date/0/date.rbs +27 -42
  162. data/stdlib/did_you_mean/0/did_you_mean.rbs +1 -6
  163. data/stdlib/digest/0/digest.rbs +25 -2
  164. data/stdlib/erb/0/erb.rbs +0 -1
  165. data/stdlib/etc/0/etc.rbs +51 -34
  166. data/stdlib/fileutils/0/fileutils.rbs +3 -44
  167. data/stdlib/io-console/0/io-console.rbs +69 -15
  168. data/stdlib/ipaddr/0/ipaddr.rbs +16 -4
  169. data/stdlib/json/0/json.rbs +107 -120
  170. data/stdlib/logger/0/log_device.rbs +1 -1
  171. data/stdlib/logger/0/logger.rbs +3 -18
  172. data/stdlib/minitest/0/kernel.rbs +2 -2
  173. data/stdlib/minitest/0/minitest/abstract_reporter.rbs +4 -1
  174. data/stdlib/minitest/0/minitest/assertion.rbs +1 -0
  175. data/stdlib/minitest/0/minitest/assertions.rbs +58 -13
  176. data/stdlib/minitest/0/minitest/backtrace_filter.rbs +7 -0
  177. data/stdlib/minitest/0/minitest/bench_spec.rbs +8 -8
  178. data/stdlib/minitest/0/minitest/benchmark.rbs +17 -16
  179. data/stdlib/minitest/0/minitest/compress.rbs +13 -0
  180. data/stdlib/minitest/0/minitest/error_on_warning.rbs +3 -0
  181. data/stdlib/minitest/0/minitest/mock.rbs +9 -5
  182. data/stdlib/minitest/0/minitest/parallel/executor.rbs +4 -0
  183. data/stdlib/minitest/0/minitest/parallel/test/class_methods.rbs +0 -1
  184. data/stdlib/minitest/0/minitest/pride_io.rbs +8 -0
  185. data/stdlib/minitest/0/minitest/pride_lol.rbs +2 -0
  186. data/stdlib/minitest/0/minitest/progress_reporter.rbs +1 -1
  187. data/stdlib/minitest/0/minitest/reportable.rbs +2 -0
  188. data/stdlib/minitest/0/minitest/runnable.rbs +33 -1
  189. data/stdlib/minitest/0/minitest/spec/dsl/instance_methods.rbs +1 -1
  190. data/stdlib/minitest/0/minitest/spec/dsl.rbs +10 -6
  191. data/stdlib/minitest/0/minitest/spec.rbs +1 -1
  192. data/stdlib/minitest/0/minitest/statistics_reporter.rbs +5 -0
  193. data/stdlib/minitest/0/minitest/summary_reporter.rbs +0 -7
  194. data/stdlib/minitest/0/minitest/test/lifecycle_hooks.rbs +1 -1
  195. data/stdlib/minitest/0/minitest/test.rbs +7 -14
  196. data/stdlib/minitest/0/minitest/unexpected_error.rbs +2 -0
  197. data/stdlib/minitest/0/minitest/unexpected_warning.rbs +6 -0
  198. data/stdlib/minitest/0/minitest/unit.rbs +1 -2
  199. data/stdlib/minitest/0/minitest.rbs +41 -892
  200. data/stdlib/monitor/0/monitor.rbs +13 -4
  201. data/stdlib/net-http/0/net-http.rbs +42 -109
  202. data/stdlib/nkf/0/nkf.rbs +30 -0
  203. data/stdlib/objspace/0/objspace.rbs +1 -2
  204. data/stdlib/observable/0/observable.rbs +1 -1
  205. data/stdlib/open-uri/0/manifest.yaml +1 -0
  206. data/stdlib/open-uri/0/open-uri.rbs +52 -0
  207. data/stdlib/open3/0/open3.rbs +0 -8
  208. data/stdlib/openssl/0/manifest.yaml +1 -0
  209. data/stdlib/openssl/0/openssl.rbs +235 -143
  210. data/stdlib/optparse/0/optparse.rbs +58 -18
  211. data/stdlib/pathname/0/pathname.rbs +2 -8
  212. data/stdlib/pp/0/pp.rbs +3 -1
  213. data/stdlib/prettyprint/0/prettyprint.rbs +0 -4
  214. data/stdlib/pstore/0/pstore.rbs +0 -6
  215. data/stdlib/psych/0/core_ext.rbs +12 -0
  216. data/stdlib/psych/0/psych.rbs +15 -4
  217. data/stdlib/pty/0/pty.rbs +46 -4
  218. data/stdlib/rdoc/0/code_object.rbs +0 -4
  219. data/stdlib/rdoc/0/markup.rbs +10 -12
  220. data/stdlib/rdoc/0/rdoc.rbs +13 -8
  221. data/stdlib/resolv/0/resolv.rbs +21 -12
  222. data/stdlib/ripper/0/ripper.rbs +0 -2
  223. data/stdlib/securerandom/0/securerandom.rbs +7 -2
  224. data/stdlib/shellwords/0/shellwords.rbs +11 -12
  225. data/stdlib/singleton/0/singleton.rbs +0 -1
  226. data/stdlib/socket/0/addrinfo.rbs +1 -2
  227. data/stdlib/socket/0/basic_socket.rbs +0 -5
  228. data/stdlib/socket/0/socket.rbs +32 -27
  229. data/stdlib/socket/0/tcp_server.rbs +0 -3
  230. data/stdlib/socket/0/tcp_socket.rbs +36 -3
  231. data/stdlib/socket/0/udp_socket.rbs +0 -1
  232. data/stdlib/socket/0/unix_server.rbs +0 -3
  233. data/stdlib/socket/0/unix_socket.rbs +4 -2
  234. data/{core/string_io.rbs → stdlib/stringio/0/stringio.rbs} +1 -1
  235. data/stdlib/strscan/0/string_scanner.rbs +1265 -422
  236. data/stdlib/tempfile/0/tempfile.rbs +135 -28
  237. data/stdlib/time/0/time.rbs +48 -35
  238. data/stdlib/timeout/0/timeout.rbs +11 -8
  239. data/stdlib/tmpdir/0/tmpdir.rbs +10 -3
  240. data/stdlib/tsort/0/tsort.rbs +0 -4
  241. data/stdlib/uri/0/common.rbs +28 -30
  242. data/stdlib/uri/0/ftp.rbs +1 -1
  243. data/stdlib/uri/0/generic.rbs +22 -18
  244. data/stdlib/uri/0/http.rbs +2 -2
  245. data/stdlib/uri/0/rfc2396_parser.rbs +3 -0
  246. data/stdlib/zlib/0/buf_error.rbs +1 -70
  247. data/stdlib/zlib/0/data_error.rbs +1 -70
  248. data/stdlib/zlib/0/deflate.rbs +8 -72
  249. data/stdlib/zlib/0/error.rbs +1 -70
  250. data/stdlib/zlib/0/gzip_file/crc_error.rbs +2 -105
  251. data/stdlib/zlib/0/gzip_file/error.rbs +2 -105
  252. data/stdlib/zlib/0/gzip_file/length_error.rbs +2 -105
  253. data/stdlib/zlib/0/gzip_file/no_footer.rbs +2 -105
  254. data/stdlib/zlib/0/gzip_file.rbs +1 -71
  255. data/stdlib/zlib/0/gzip_reader.rbs +3 -74
  256. data/stdlib/zlib/0/gzip_writer.rbs +1 -70
  257. data/stdlib/zlib/0/inflate.rbs +4 -71
  258. data/stdlib/zlib/0/mem_error.rbs +1 -70
  259. data/stdlib/zlib/0/need_dict.rbs +1 -70
  260. data/stdlib/zlib/0/stream_end.rbs +1 -70
  261. data/stdlib/zlib/0/stream_error.rbs +1 -70
  262. data/stdlib/zlib/0/version_error.rbs +1 -70
  263. data/stdlib/zlib/0/zlib.rbs +0 -2
  264. data/stdlib/zlib/0/zstream.rbs +4 -72
  265. metadata +17 -13
  266. data/ext/rbs_extension/ruby_objs.c +0 -602
  267. data/ext/rbs_extension/ruby_objs.h +0 -51
  268. data/stdlib/minitest/0/manifest.yaml +0 -2
@@ -1,19 +1,30 @@
1
1
  #include "rbs_extension.h"
2
+ #include "rbs/util/rbs_constant_pool.h"
2
3
 
3
4
  #define RESET_TABLE_P(table) (table->size == 0)
4
5
 
5
6
  id_table *alloc_empty_table(void) {
6
7
  id_table *table = malloc(sizeof(id_table));
7
- table->size = 10;
8
- table->count = 0;
9
- table->ids = calloc(10, sizeof(ID));
8
+
9
+ *table = (id_table) {
10
+ .size = 10,
11
+ .count = 0,
12
+ .ids = calloc(10, sizeof(rbs_constant_id_t)),
13
+ .next = NULL,
14
+ };
10
15
 
11
16
  return table;
12
17
  }
13
18
 
14
19
  id_table *alloc_reset_table(void) {
15
20
  id_table *table = malloc(sizeof(id_table));
16
- table->size = 0;
21
+
22
+ *table = (id_table) {
23
+ .size = 0,
24
+ .count = 0,
25
+ .ids = NULL,
26
+ .next = NULL,
27
+ };
17
28
 
18
29
  return table;
19
30
  }
@@ -51,7 +62,7 @@ void parser_pop_typevar_table(parserstate *state) {
51
62
  }
52
63
  }
53
64
 
54
- void parser_insert_typevar(parserstate *state, ID id) {
65
+ void parser_insert_typevar(parserstate *state, rbs_constant_id_t id) {
55
66
  id_table *table = state->vars;
56
67
 
57
68
  if (RESET_TABLE_P(table)) {
@@ -60,17 +71,17 @@ void parser_insert_typevar(parserstate *state, ID id) {
60
71
 
61
72
  if (table->size == table->count) {
62
73
  // expand
63
- ID *ptr = table->ids;
74
+ rbs_constant_id_t *ptr = table->ids;
64
75
  table->size += 10;
65
- table->ids = calloc(table->size, sizeof(ID));
66
- memcpy(table->ids, ptr, sizeof(ID) * table->count);
76
+ table->ids = calloc(table->size, sizeof(rbs_constant_id_t));
77
+ memcpy(table->ids, ptr, sizeof(rbs_constant_id_t) * table->count);
67
78
  free(ptr);
68
79
  }
69
80
 
70
81
  table->ids[table->count++] = id;
71
82
  }
72
83
 
73
- bool parser_typevar_member(parserstate *state, ID id) {
84
+ bool parser_typevar_member(parserstate *state, rbs_constant_id_t id) {
74
85
  id_table *table = state->vars;
75
86
 
76
87
  while (table && !RESET_TABLE_P(table)) {
@@ -182,15 +193,18 @@ VALUE get_comment(parserstate *state, int subject_line) {
182
193
  }
183
194
 
184
195
  comment *alloc_comment(token comment_token, comment *last_comment) {
185
- comment *new_comment = calloc(1, sizeof(comment));
196
+ comment *new_comment = malloc(sizeof(comment));
186
197
 
187
- new_comment->next_comment = last_comment;
198
+ *new_comment = (comment) {
199
+ .start = comment_token.range.start,
200
+ .end = comment_token.range.end,
188
201
 
189
- new_comment->start = comment_token.range.start;
190
- new_comment->end = comment_token.range.end;
202
+ .line_size = 0,
203
+ .line_count = 0,
204
+ .tokens = NULL,
191
205
 
192
- new_comment->line_size = 0;
193
- new_comment->line_count = 0;
206
+ .next_comment = last_comment,
207
+ };
194
208
 
195
209
  comment_insert_new_line(new_comment, comment_token);
196
210
 
@@ -279,11 +293,25 @@ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos) {
279
293
  rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
280
294
  }
281
295
 
282
- lexstate *lexer = calloc(1, sizeof(lexstate));
283
- lexer->string = string;
284
- lexer->current.line = 1;
285
- lexer->start_pos = start_pos;
286
- lexer->end_pos = end_pos;
296
+ lexstate *lexer = malloc(sizeof(lexstate));
297
+
298
+ position start_position = (position) {
299
+ .byte_pos = 0,
300
+ .char_pos = 0,
301
+ .line = 1,
302
+ .column = 0,
303
+ };
304
+
305
+ *lexer = (lexstate) {
306
+ .string = string,
307
+ .start_pos = start_pos,
308
+ .end_pos = end_pos,
309
+ .current = start_position,
310
+ .start = { 0 },
311
+ .first_token_of_line = false,
312
+ .last_char = 0,
313
+ };
314
+
287
315
  skipn(lexer, start_pos);
288
316
  lexer->start = lexer->current;
289
317
  lexer->first_token_of_line = lexer->current.column == 0;
@@ -292,13 +320,42 @@ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos) {
292
320
  }
293
321
 
294
322
  parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_pos, VALUE variables) {
295
- parserstate *parser = calloc(1, sizeof(parserstate));
296
- parser->lexstate = lexer;
297
- parser->buffer = buffer;
298
- parser->current_token = NullToken;
299
- parser->next_token = NullToken;
300
- parser->next_token2 = NullToken;
301
- parser->next_token3 = NullToken;
323
+ parserstate *parser = malloc(sizeof(parserstate));
324
+
325
+ *parser = (parserstate) {
326
+ .lexstate = lexer,
327
+
328
+ .current_token = NullToken,
329
+ .next_token = NullToken,
330
+ .next_token2 = NullToken,
331
+ .next_token3 = NullToken,
332
+ .buffer = buffer,
333
+
334
+ .vars = NULL,
335
+ .last_comment = NULL,
336
+
337
+ .constant_pool = { 0 },
338
+ };
339
+
340
+ // The parser's constant pool is mainly used for storing the names of type variables, which usually aren't many.
341
+ // Below are some statistics gathered from the current test suite. We can see that 56% of parsers never add to their
342
+ // constant pool at all. The initial capacity needs to be a power of 2. Picking 2 means that we won't need to realloc
343
+ // in 85% of cases.
344
+ //
345
+ // TODO: recalculate these statistics based on a real world codebase, rather than the test suite.
346
+ //
347
+ // | Size | Count | Cumulative | % Coverage |
348
+ // |------|-------|------------|------------|
349
+ // | 0 | 7,862 | 7,862 | 56% |
350
+ // | 1 | 3,196 | 11,058 | 79% |
351
+ // | 2 | 778 | 12,719 | 85% |
352
+ // | 3 | 883 | 11,941 | 91% |
353
+ // | 4 | 478 | 13,197 | 95% |
354
+ // | 5 | 316 | 13,513 | 97% |
355
+ // | 6 | 288 | 13,801 | 99% |
356
+ // | 7 | 144 | 13,945 | 100% |
357
+ const size_t initial_pool_capacity = 2;
358
+ rbs_constant_pool_init(&parser->constant_pool, initial_pool_capacity);
302
359
 
303
360
  parser_advance(parser);
304
361
  parser_advance(parser);
@@ -306,6 +363,7 @@ parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_
306
363
 
307
364
  if (!NIL_P(variables)) {
308
365
  if (!RB_TYPE_P(variables, T_ARRAY)) {
366
+ free_parser(parser);
309
367
  rb_raise(rb_eTypeError,
310
368
  "wrong argument type %"PRIsVALUE" (must be array or nil)",
311
369
  rb_obj_class(variables));
@@ -314,19 +372,40 @@ parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_
314
372
  parser_push_typevar_table(parser, true);
315
373
 
316
374
  for (long i = 0; i < rb_array_len(variables); i++) {
317
- VALUE index = INT2FIX(i);
318
- VALUE symbol = rb_ary_aref(1, &index, variables);
319
- parser_insert_typevar(parser, SYM2ID(symbol));
375
+ VALUE symbol = rb_ary_entry(variables, i);
376
+ VALUE name = rb_sym2str(symbol);
377
+
378
+ rbs_constant_id_t id = rbs_constant_pool_insert_shared(
379
+ &parser->constant_pool,
380
+ (const uint8_t *) RSTRING_PTR(name),
381
+ RSTRING_LEN(name)
382
+ );
383
+
384
+ parser_insert_typevar(parser, id);
320
385
  }
321
386
  }
322
387
 
323
388
  return parser;
324
389
  }
325
390
 
391
+ void free_typevar_tables(id_table *table) {
392
+ while (table != NULL) {
393
+ id_table *next = table->next;
394
+ if (table->ids != NULL) {
395
+ free(table->ids);
396
+ }
397
+ free(table);
398
+ table = next;
399
+ }
400
+ }
401
+
326
402
  void free_parser(parserstate *parser) {
327
403
  free(parser->lexstate);
328
404
  if (parser->last_comment) {
329
405
  free_comment(parser->last_comment);
330
406
  }
407
+
408
+ free_typevar_tables(parser->vars);
409
+ rbs_constant_pool_free(&parser->constant_pool);
331
410
  free(parser);
332
411
  }
@@ -7,13 +7,13 @@
7
7
  #include "location.h"
8
8
 
9
9
  /**
10
- * id_table represents a set of IDs.
10
+ * id_table represents a set of RBS constant IDs.
11
11
  * This is used to manage the set of bound variables.
12
12
  * */
13
13
  typedef struct id_table {
14
14
  size_t size;
15
15
  size_t count;
16
- ID *ids;
16
+ rbs_constant_id_t *ids;
17
17
  struct id_table *next;
18
18
  } id_table;
19
19
 
@@ -55,6 +55,8 @@ typedef struct {
55
55
 
56
56
  id_table *vars; /* Known type variables */
57
57
  comment *last_comment; /* Last read comment */
58
+
59
+ rbs_constant_pool_t constant_pool;
58
60
  } parserstate;
59
61
 
60
62
  comment *alloc_comment(token comment_token, comment *last_comment);
@@ -84,14 +86,14 @@ void parser_pop_typevar_table(parserstate *state);
84
86
  /**
85
87
  * Insert new type variable into the latest table.
86
88
  * */
87
- void parser_insert_typevar(parserstate *state, ID id);
89
+ void parser_insert_typevar(parserstate *state, rbs_constant_id_t id);
88
90
 
89
91
  /**
90
92
  * Returns true if given type variable is recorded in the table.
91
93
  * If not found, it goes one table up, if it's not a reset table.
92
94
  * Or returns false, if it's a reset table.
93
95
  * */
94
- bool parser_typevar_member(parserstate *state, ID id);
96
+ bool parser_typevar_member(parserstate *state, rbs_constant_id_t id);
95
97
 
96
98
  /**
97
99
  * Allocate new lexstate object.
@@ -4,18 +4,9 @@
4
4
  #include "ruby/re.h"
5
5
  #include "ruby/encoding.h"
6
6
 
7
+ #include "rbs.h"
7
8
  #include "lexer.h"
8
9
  #include "parser.h"
9
- #include "constants.h"
10
- #include "ruby_objs.h"
11
-
12
- /**
13
- * Unescape escape sequences in the given string inplace:
14
- *
15
- * '\\n' => "\n"
16
- *
17
- * */
18
- void rbs_unescape_string(VALUE string, bool dq_string);
19
10
 
20
11
  /**
21
12
  * Receives `parserstate` and `range`, which represents a string token or symbol token, and returns a string VALUE.
@@ -1,31 +1,38 @@
1
+ /*----------------------------------------------------------------------------*/
2
+ /* This file is generated by the templates/template.rb script and should not */
3
+ /* be modified manually. */
4
+ /* To change the template see */
5
+ /* templates/include/rbs/constants.h.erb */
6
+ /*----------------------------------------------------------------------------*/
7
+
1
8
  #ifndef RBS__CONSTANTS_H
2
9
  #define RBS__CONSTANTS_H
3
10
 
4
11
  extern VALUE RBS;
5
12
 
6
13
  extern VALUE RBS_AST;
14
+ extern VALUE RBS_AST_Declarations;
15
+ extern VALUE RBS_AST_Directives;
16
+ extern VALUE RBS_AST_Members;
17
+ extern VALUE RBS_Types;
18
+ extern VALUE RBS_Types_Bases;
19
+ extern VALUE RBS_ParsingError;
20
+
7
21
  extern VALUE RBS_AST_Annotation;
8
22
  extern VALUE RBS_AST_Comment;
9
- extern VALUE RBS_AST_TypeParam;
10
-
11
- extern VALUE RBS_AST_Declarations;
12
- extern VALUE RBS_AST_Declarations_TypeAlias;
13
- extern VALUE RBS_AST_Declarations_Class_Super;
14
23
  extern VALUE RBS_AST_Declarations_Class;
24
+ extern VALUE RBS_AST_Declarations_Class_Super;
25
+ extern VALUE RBS_AST_Declarations_ClassAlias;
15
26
  extern VALUE RBS_AST_Declarations_Constant;
16
27
  extern VALUE RBS_AST_Declarations_Global;
17
28
  extern VALUE RBS_AST_Declarations_Interface;
18
- extern VALUE RBS_AST_Declarations_Module_Self;
19
29
  extern VALUE RBS_AST_Declarations_Module;
30
+ extern VALUE RBS_AST_Declarations_Module_Self;
20
31
  extern VALUE RBS_AST_Declarations_ModuleAlias;
21
- extern VALUE RBS_AST_Declarations_ClassAlias;
22
-
23
- extern VALUE RBS_AST_Directives;
32
+ extern VALUE RBS_AST_Declarations_TypeAlias;
24
33
  extern VALUE RBS_AST_Directives_Use;
25
34
  extern VALUE RBS_AST_Directives_Use_SingleClause;
26
35
  extern VALUE RBS_AST_Directives_Use_WildcardClause;
27
-
28
- extern VALUE RBS_AST_Members;
29
36
  extern VALUE RBS_AST_Members_Alias;
30
37
  extern VALUE RBS_AST_Members_AttrAccessor;
31
38
  extern VALUE RBS_AST_Members_AttrReader;
@@ -40,16 +47,11 @@ extern VALUE RBS_AST_Members_MethodDefinition_Overload;
40
47
  extern VALUE RBS_AST_Members_Prepend;
41
48
  extern VALUE RBS_AST_Members_Private;
42
49
  extern VALUE RBS_AST_Members_Public;
43
-
50
+ extern VALUE RBS_AST_TypeParam;
44
51
  extern VALUE RBS_MethodType;
45
52
  extern VALUE RBS_Namespace;
46
-
47
- extern VALUE RBS_ParsingError;
48
53
  extern VALUE RBS_TypeName;
49
-
50
- extern VALUE RBS_Types;
51
54
  extern VALUE RBS_Types_Alias;
52
- extern VALUE RBS_Types_Bases;
53
55
  extern VALUE RBS_Types_Bases_Any;
54
56
  extern VALUE RBS_Types_Bases_Bool;
55
57
  extern VALUE RBS_Types_Bases_Bottom;
@@ -62,9 +64,8 @@ extern VALUE RBS_Types_Bases_Void;
62
64
  extern VALUE RBS_Types_Block;
63
65
  extern VALUE RBS_Types_ClassInstance;
64
66
  extern VALUE RBS_Types_ClassSingleton;
65
- extern VALUE RBS_Types_Function_Param;
66
67
  extern VALUE RBS_Types_Function;
67
- extern VALUE RBS_Types_UntypedFunction;
68
+ extern VALUE RBS_Types_Function_Param;
68
69
  extern VALUE RBS_Types_Interface;
69
70
  extern VALUE RBS_Types_Intersection;
70
71
  extern VALUE RBS_Types_Literal;
@@ -73,6 +74,7 @@ extern VALUE RBS_Types_Proc;
73
74
  extern VALUE RBS_Types_Record;
74
75
  extern VALUE RBS_Types_Tuple;
75
76
  extern VALUE RBS_Types_Union;
77
+ extern VALUE RBS_Types_UntypedFunction;
76
78
  extern VALUE RBS_Types_Variable;
77
79
 
78
80
  void rbs__init_constants();
@@ -0,0 +1,72 @@
1
+ /*----------------------------------------------------------------------------*/
2
+ /* This file is generated by the templates/template.rb script and should not */
3
+ /* be modified manually. */
4
+ /* To change the template see */
5
+ /* templates/include/rbs/ruby_objs.h.erb */
6
+ /*----------------------------------------------------------------------------*/
7
+
8
+ #ifndef RBS__RUBY_OBJS_H
9
+ #define RBS__RUBY_OBJS_H
10
+
11
+ #include "ruby.h"
12
+
13
+ VALUE rbs_ast_annotation(VALUE string, VALUE location);
14
+ VALUE rbs_ast_comment(VALUE string, VALUE location);
15
+ VALUE rbs_ast_decl_class(VALUE name, VALUE type_params, VALUE super_class, VALUE members, VALUE annotations, VALUE location, VALUE comment);
16
+ VALUE rbs_ast_decl_class_super(VALUE name, VALUE args, VALUE location);
17
+ VALUE rbs_ast_decl_class_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment, VALUE annotations);
18
+ VALUE rbs_ast_decl_constant(VALUE name, VALUE type, VALUE location, VALUE comment, VALUE annotations);
19
+ VALUE rbs_ast_decl_global(VALUE name, VALUE type, VALUE location, VALUE comment, VALUE annotations);
20
+ VALUE rbs_ast_decl_interface(VALUE name, VALUE type_params, VALUE members, VALUE annotations, VALUE location, VALUE comment);
21
+ VALUE rbs_ast_decl_module(VALUE name, VALUE type_params, VALUE self_types, VALUE members, VALUE annotations, VALUE location, VALUE comment);
22
+ VALUE rbs_ast_decl_module_self(VALUE name, VALUE args, VALUE location);
23
+ VALUE rbs_ast_decl_module_alias(VALUE new_name, VALUE old_name, VALUE location, VALUE comment, VALUE annotations);
24
+ VALUE rbs_ast_decl_type_alias(VALUE name, VALUE type_params, VALUE type, VALUE annotations, VALUE location, VALUE comment);
25
+ VALUE rbs_ast_directives_use(VALUE clauses, VALUE location);
26
+ VALUE rbs_ast_directives_use_single_clause(VALUE type_name, VALUE new_name, VALUE location);
27
+ VALUE rbs_ast_directives_use_wildcard_clause(VALUE namespace, VALUE location);
28
+ VALUE rbs_ast_members_alias(VALUE new_name, VALUE old_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment);
29
+ VALUE rbs_ast_members_attr_accessor(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility);
30
+ VALUE rbs_ast_members_attr_reader(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility);
31
+ VALUE rbs_ast_members_attr_writer(VALUE name, VALUE type, VALUE ivar_name, VALUE kind, VALUE annotations, VALUE location, VALUE comment, VALUE visibility);
32
+ VALUE rbs_ast_members_class_instance_variable(VALUE name, VALUE type, VALUE location, VALUE comment);
33
+ VALUE rbs_ast_members_class_variable(VALUE name, VALUE type, VALUE location, VALUE comment);
34
+ VALUE rbs_ast_members_extend(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment);
35
+ VALUE rbs_ast_members_include(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment);
36
+ VALUE rbs_ast_members_instance_variable(VALUE name, VALUE type, VALUE location, VALUE comment);
37
+ VALUE rbs_ast_members_method_definition(VALUE name, VALUE kind, VALUE overloads, VALUE annotations, VALUE location, VALUE comment, VALUE overloading, VALUE visibility);
38
+ VALUE rbs_ast_members_method_definition_overload(VALUE annotations, VALUE method_type);
39
+ VALUE rbs_ast_members_prepend(VALUE name, VALUE args, VALUE annotations, VALUE location, VALUE comment);
40
+ VALUE rbs_ast_members_private(VALUE location);
41
+ VALUE rbs_ast_members_public(VALUE location);
42
+ VALUE rbs_ast_type_param(VALUE name, VALUE variance, VALUE upper_bound, VALUE default_type, VALUE unchecked, VALUE location);
43
+ VALUE rbs_method_type(VALUE type_params, VALUE type, VALUE block, VALUE location);
44
+ VALUE rbs_namespace(VALUE path, VALUE absolute);
45
+ VALUE rbs_type_name(VALUE namespace, VALUE name);
46
+ VALUE rbs_alias(VALUE name, VALUE args, VALUE location);
47
+ VALUE rbs_bases_any(VALUE todo, VALUE location);
48
+ VALUE rbs_bases_bool(VALUE location);
49
+ VALUE rbs_bases_bottom(VALUE location);
50
+ VALUE rbs_bases_class(VALUE location);
51
+ VALUE rbs_bases_instance(VALUE location);
52
+ VALUE rbs_bases_nil(VALUE location);
53
+ VALUE rbs_bases_self(VALUE location);
54
+ VALUE rbs_bases_top(VALUE location);
55
+ VALUE rbs_bases_void(VALUE location);
56
+ VALUE rbs_block(VALUE type, VALUE required, VALUE self_type);
57
+ VALUE rbs_class_instance(VALUE name, VALUE args, VALUE location);
58
+ VALUE rbs_class_singleton(VALUE name, VALUE location);
59
+ VALUE rbs_function(VALUE required_positionals, VALUE optional_positionals, VALUE rest_positionals, VALUE trailing_positionals, VALUE required_keywords, VALUE optional_keywords, VALUE rest_keywords, VALUE return_type);
60
+ VALUE rbs_function_param(VALUE type, VALUE name, VALUE location);
61
+ VALUE rbs_interface(VALUE name, VALUE args, VALUE location);
62
+ VALUE rbs_intersection(VALUE types, VALUE location);
63
+ VALUE rbs_literal(VALUE literal, VALUE location);
64
+ VALUE rbs_optional(VALUE type, VALUE location);
65
+ VALUE rbs_proc(VALUE type, VALUE block, VALUE location, VALUE self_type);
66
+ VALUE rbs_record(VALUE all_fields, VALUE location);
67
+ VALUE rbs_tuple(VALUE types, VALUE location);
68
+ VALUE rbs_union(VALUE types, VALUE location);
69
+ VALUE rbs_untyped_function(VALUE return_type);
70
+ VALUE rbs_variable(VALUE name, VALUE location);
71
+
72
+ #endif
@@ -0,0 +1,219 @@
1
+ /**
2
+ * @file rbs_constant_pool.h
3
+ *
4
+ * A data structure that stores a set of strings.
5
+ *
6
+ * Each string is assigned a unique id, which can be used to compare strings for
7
+ * equality. This comparison ends up being much faster than strcmp, since it
8
+ * only requires a single integer comparison.
9
+ */
10
+ #ifndef RBS_CONSTANT_POOL_H
11
+ #define RBS_CONSTANT_POOL_H
12
+
13
+ #include <assert.h>
14
+ #include <stdbool.h>
15
+ #include <stdint.h>
16
+ #include <stdlib.h>
17
+ #include <string.h>
18
+
19
+ /**
20
+ * When we allocate constants into the pool, we reserve 0 to mean that the slot
21
+ * is not yet filled. This constant is reused in other places to indicate the
22
+ * lack of a constant id.
23
+ */
24
+ #define RBS_CONSTANT_ID_UNSET 0
25
+
26
+ /**
27
+ * A constant id is a unique identifier for a constant in the constant pool.
28
+ */
29
+ typedef uint32_t rbs_constant_id_t;
30
+
31
+ /**
32
+ * A list of constant IDs. Usually used to represent a set of locals.
33
+ */
34
+ typedef struct {
35
+ /** The number of constant ids in the list. */
36
+ size_t size;
37
+
38
+ /** The number of constant ids that have been allocated in the list. */
39
+ size_t capacity;
40
+
41
+ /** The constant ids in the list. */
42
+ rbs_constant_id_t *ids;
43
+ } rbs_constant_id_list_t;
44
+
45
+ /**
46
+ * Initialize a list of constant ids.
47
+ *
48
+ * @param list The list to initialize.
49
+ */
50
+ void rbs_constant_id_list_init(rbs_constant_id_list_t *list);
51
+
52
+ /**
53
+ * Initialize a list of constant ids with a given capacity.
54
+ *
55
+ * @param list The list to initialize.
56
+ * @param capacity The initial capacity of the list.
57
+ */
58
+ void rbs_constant_id_list_init_capacity(rbs_constant_id_list_t *list, size_t capacity);
59
+
60
+ /**
61
+ * Append a constant id to a list of constant ids. Returns false if any
62
+ * potential reallocations fail.
63
+ *
64
+ * @param list The list to append to.
65
+ * @param id The id to append.
66
+ * @return Whether the append succeeded.
67
+ */
68
+ bool rbs_constant_id_list_append(rbs_constant_id_list_t *list, rbs_constant_id_t id);
69
+
70
+ /**
71
+ * Insert a constant id into a list of constant ids at the specified index.
72
+ *
73
+ * @param list The list to insert into.
74
+ * @param index The index at which to insert.
75
+ * @param id The id to insert.
76
+ */
77
+ void rbs_constant_id_list_insert(rbs_constant_id_list_t *list, size_t index, rbs_constant_id_t id);
78
+
79
+ /**
80
+ * Checks if the current constant id list includes the given constant id.
81
+ *
82
+ * @param list The list to check.
83
+ * @param id The id to check for.
84
+ * @return Whether the list includes the given id.
85
+ */
86
+ bool rbs_constant_id_list_includes(rbs_constant_id_list_t *list, rbs_constant_id_t id);
87
+
88
+ /**
89
+ * Free the memory associated with a list of constant ids.
90
+ *
91
+ * @param list The list to free.
92
+ */
93
+ void rbs_constant_id_list_free(rbs_constant_id_list_t *list);
94
+
95
+ /**
96
+ * The type of bucket in the constant pool hash map. This determines how the
97
+ * bucket should be freed.
98
+ */
99
+ typedef unsigned int rbs_constant_pool_bucket_type_t;
100
+
101
+ /** By default, each constant is a slice of the source. */
102
+ static const rbs_constant_pool_bucket_type_t RBS_CONSTANT_POOL_BUCKET_DEFAULT = 0;
103
+
104
+ /** An owned constant is one for which memory has been allocated. */
105
+ static const rbs_constant_pool_bucket_type_t RBS_CONSTANT_POOL_BUCKET_OWNED = 1;
106
+
107
+ /** A constant constant is known at compile time. */
108
+ static const rbs_constant_pool_bucket_type_t RBS_CONSTANT_POOL_BUCKET_CONSTANT = 2;
109
+
110
+ /** A bucket in the hash map. */
111
+ typedef struct {
112
+ /** The incremental ID used for indexing back into the pool. */
113
+ unsigned int id: 30;
114
+
115
+ /** The type of the bucket, which determines how to free it. */
116
+ rbs_constant_pool_bucket_type_t type: 2;
117
+
118
+ /** The hash of the bucket. */
119
+ uint32_t hash;
120
+ } rbs_constant_pool_bucket_t;
121
+
122
+ /** A constant in the pool which effectively stores a string. */
123
+ typedef struct {
124
+ /** A pointer to the start of the string. */
125
+ const uint8_t *start;
126
+
127
+ /** The length of the string. */
128
+ size_t length;
129
+ } rbs_constant_t;
130
+
131
+ /** The overall constant pool, which stores constants found while parsing. */
132
+ typedef struct {
133
+ /** The buckets in the hash map. */
134
+ rbs_constant_pool_bucket_t *buckets;
135
+
136
+ /** The constants that are stored in the buckets. */
137
+ rbs_constant_t *constants;
138
+
139
+ /** The number of buckets in the hash map. */
140
+ uint32_t size;
141
+
142
+ /** The number of buckets that have been allocated in the hash map. */
143
+ uint32_t capacity;
144
+ } rbs_constant_pool_t;
145
+
146
+ // A global constant pool for storing permenant keywords, such as the names of location children in `parser.c`.
147
+ extern rbs_constant_pool_t *RBS_GLOBAL_CONSTANT_POOL;
148
+
149
+ /**
150
+ * Initialize a new constant pool with a given capacity.
151
+ *
152
+ * @param pool The pool to initialize.
153
+ * @param capacity The initial capacity of the pool.
154
+ * @return Whether the initialization succeeded.
155
+ */
156
+ bool rbs_constant_pool_init(rbs_constant_pool_t *pool, uint32_t capacity);
157
+
158
+ /**
159
+ * Return a pointer to the constant indicated by the given constant id.
160
+ *
161
+ * @param pool The pool to get the constant from.
162
+ * @param constant_id The id of the constant to get.
163
+ * @return A pointer to the constant.
164
+ */
165
+ rbs_constant_t * rbs_constant_pool_id_to_constant(const rbs_constant_pool_t *pool, rbs_constant_id_t constant_id);
166
+
167
+ /**
168
+ * Find a constant in a constant pool. Returns the id of the constant, or 0 if
169
+ * the constant is not found.
170
+ *
171
+ * @param pool The pool to find the constant in.
172
+ * @param start A pointer to the start of the constant.
173
+ * @param length The length of the constant.
174
+ * @return The id of the constant.
175
+ */
176
+ rbs_constant_id_t rbs_constant_pool_find(const rbs_constant_pool_t *pool, const uint8_t *start, size_t length);
177
+
178
+ /**
179
+ * Insert a constant into a constant pool that is a slice of a source string.
180
+ * Returns the id of the constant, or 0 if any potential calls to resize fail.
181
+ *
182
+ * @param pool The pool to insert the constant into.
183
+ * @param start A pointer to the start of the constant.
184
+ * @param length The length of the constant.
185
+ * @return The id of the constant.
186
+ */
187
+ rbs_constant_id_t rbs_constant_pool_insert_shared(rbs_constant_pool_t *pool, const uint8_t *start, size_t length);
188
+
189
+ /**
190
+ * Insert a constant into a constant pool from memory that is now owned by the
191
+ * constant pool. Returns the id of the constant, or 0 if any potential calls to
192
+ * resize fail.
193
+ *
194
+ * @param pool The pool to insert the constant into.
195
+ * @param start A pointer to the start of the constant.
196
+ * @param length The length of the constant.
197
+ * @return The id of the constant.
198
+ */
199
+ rbs_constant_id_t rbs_constant_pool_insert_owned(rbs_constant_pool_t *pool, uint8_t *start, size_t length);
200
+
201
+ /**
202
+ * Insert a constant into a constant pool from memory that is constant. Returns
203
+ * the id of the constant, or 0 if any potential calls to resize fail.
204
+ *
205
+ * @param pool The pool to insert the constant into.
206
+ * @param start A pointer to the start of the constant.
207
+ * @param length The length of the constant.
208
+ * @return The id of the constant.
209
+ */
210
+ rbs_constant_id_t rbs_constant_pool_insert_constant(rbs_constant_pool_t *pool, const uint8_t *start, size_t length);
211
+
212
+ /**
213
+ * Free the memory associated with a constant pool.
214
+ *
215
+ * @param pool The pool to free.
216
+ */
217
+ void rbs_constant_pool_free(rbs_constant_pool_t *pool);
218
+
219
+ #endif
data/include/rbs.h ADDED
@@ -0,0 +1,7 @@
1
+ #ifndef RBS_H
2
+ #define RBS_H
3
+
4
+ #include "rbs/constants.h"
5
+ #include "rbs/ruby_objs.h"
6
+
7
+ #endif