rbs 3.9.5 → 4.0.0

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 (330) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +74 -0
  3. data/.clangd +2 -0
  4. data/.github/dependabot.yml +14 -14
  5. data/.github/workflows/bundle-update.yml +60 -0
  6. data/.github/workflows/c-check.yml +58 -0
  7. data/.github/workflows/comments.yml +5 -3
  8. data/.github/workflows/dependabot.yml +2 -2
  9. data/.github/workflows/ruby.yml +40 -32
  10. data/.github/workflows/rust.yml +95 -0
  11. data/.github/workflows/typecheck.yml +2 -2
  12. data/.github/workflows/windows.yml +3 -3
  13. data/.gitignore +4 -0
  14. data/.rubocop.yml +2 -2
  15. data/.vscode/extensions.json +5 -0
  16. data/.vscode/settings.json +19 -0
  17. data/CHANGELOG.md +284 -0
  18. data/README.md +38 -1
  19. data/Rakefile +144 -29
  20. data/Steepfile +2 -0
  21. data/config.yml +634 -62
  22. data/core/array.rbs +307 -227
  23. data/core/basic_object.rbs +9 -8
  24. data/core/binding.rbs +0 -2
  25. data/core/builtin.rbs +2 -2
  26. data/core/class.rbs +6 -5
  27. data/core/comparable.rbs +55 -34
  28. data/core/complex.rbs +104 -78
  29. data/core/dir.rbs +61 -49
  30. data/core/encoding.rbs +12 -15
  31. data/core/enumerable.rbs +188 -87
  32. data/core/enumerator/arithmetic_sequence.rbs +70 -0
  33. data/core/enumerator.rbs +65 -2
  34. data/core/errno.rbs +11 -2
  35. data/core/errors.rbs +58 -29
  36. data/core/exception.rbs +13 -13
  37. data/core/fiber.rbs +74 -54
  38. data/core/file.rbs +280 -177
  39. data/core/file_test.rbs +3 -3
  40. data/core/float.rbs +257 -92
  41. data/core/gc.rbs +425 -281
  42. data/core/hash.rbs +1045 -739
  43. data/core/integer.rbs +135 -137
  44. data/core/io/buffer.rbs +53 -42
  45. data/core/io/wait.rbs +13 -35
  46. data/core/io.rbs +196 -148
  47. data/core/kernel.rbs +216 -155
  48. data/core/marshal.rbs +4 -4
  49. data/core/match_data.rbs +15 -13
  50. data/core/math.rbs +107 -66
  51. data/core/method.rbs +69 -33
  52. data/core/module.rbs +244 -106
  53. data/core/nil_class.rbs +7 -6
  54. data/core/numeric.rbs +74 -63
  55. data/core/object.rbs +9 -11
  56. data/core/object_space.rbs +30 -23
  57. data/core/pathname.rbs +1322 -0
  58. data/core/proc.rbs +95 -58
  59. data/core/process.rbs +222 -202
  60. data/core/ractor.rbs +371 -515
  61. data/core/random.rbs +21 -3
  62. data/core/range.rbs +159 -57
  63. data/core/rational.rbs +60 -89
  64. data/core/rbs/unnamed/argf.rbs +60 -53
  65. data/core/rbs/unnamed/env_class.rbs +19 -14
  66. data/core/rbs/unnamed/main_class.rbs +123 -0
  67. data/core/rbs/unnamed/random.rbs +11 -118
  68. data/core/regexp.rbs +258 -214
  69. data/core/ruby.rbs +53 -0
  70. data/core/ruby_vm.rbs +38 -34
  71. data/core/rubygems/config_file.rbs +5 -5
  72. data/core/rubygems/errors.rbs +4 -71
  73. data/core/rubygems/requirement.rbs +5 -5
  74. data/core/rubygems/rubygems.rbs +16 -82
  75. data/core/rubygems/version.rbs +2 -3
  76. data/core/set.rbs +490 -360
  77. data/core/signal.rbs +26 -16
  78. data/core/string.rbs +3303 -1365
  79. data/core/struct.rbs +27 -26
  80. data/core/symbol.rbs +41 -34
  81. data/core/thread.rbs +135 -74
  82. data/core/time.rbs +81 -50
  83. data/core/trace_point.rbs +41 -35
  84. data/core/true_class.rbs +2 -2
  85. data/core/unbound_method.rbs +24 -16
  86. data/core/warning.rbs +7 -7
  87. data/docs/aliases.md +79 -0
  88. data/docs/collection.md +3 -3
  89. data/docs/config.md +171 -0
  90. data/docs/encoding.md +56 -0
  91. data/docs/gem.md +0 -1
  92. data/docs/inline.md +576 -0
  93. data/docs/sigs.md +3 -3
  94. data/docs/syntax.md +46 -16
  95. data/docs/type_fingerprint.md +21 -0
  96. data/exe/rbs +1 -1
  97. data/ext/rbs_extension/ast_translation.c +1513 -0
  98. data/ext/rbs_extension/ast_translation.h +37 -0
  99. data/ext/rbs_extension/class_constants.c +185 -0
  100. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +22 -1
  101. data/ext/rbs_extension/compat.h +10 -0
  102. data/ext/rbs_extension/extconf.rb +25 -1
  103. data/ext/rbs_extension/legacy_location.c +294 -0
  104. data/ext/rbs_extension/legacy_location.h +82 -0
  105. data/ext/rbs_extension/main.c +468 -24
  106. data/ext/rbs_extension/rbs_extension.h +6 -21
  107. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  108. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  109. data/include/rbs/ast.h +1022 -0
  110. data/include/rbs/defines.h +86 -0
  111. data/include/rbs/lexer.h +206 -0
  112. data/include/rbs/location.h +40 -0
  113. data/include/rbs/parser.h +153 -0
  114. data/include/rbs/string.h +47 -0
  115. data/include/rbs/util/rbs_allocator.h +59 -0
  116. data/include/rbs/util/rbs_assert.h +20 -0
  117. data/include/rbs/util/rbs_buffer.h +83 -0
  118. data/include/rbs/util/rbs_constant_pool.h +6 -70
  119. data/include/rbs/util/rbs_encoding.h +282 -0
  120. data/include/rbs/util/rbs_unescape.h +24 -0
  121. data/include/rbs.h +9 -2
  122. data/lib/rbs/annotate/formatter.rb +3 -13
  123. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  124. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  125. data/lib/rbs/ast/annotation.rb +1 -1
  126. data/lib/rbs/ast/comment.rb +1 -1
  127. data/lib/rbs/ast/declarations.rb +10 -10
  128. data/lib/rbs/ast/members.rb +14 -14
  129. data/lib/rbs/ast/ruby/annotations.rb +409 -0
  130. data/lib/rbs/ast/ruby/comment_block.rb +245 -0
  131. data/lib/rbs/ast/ruby/declarations.rb +281 -0
  132. data/lib/rbs/ast/ruby/helpers/constant_helper.rb +28 -0
  133. data/lib/rbs/ast/ruby/helpers/location_helper.rb +15 -0
  134. data/lib/rbs/ast/ruby/members.rb +723 -0
  135. data/lib/rbs/ast/type_param.rb +24 -4
  136. data/lib/rbs/buffer.rb +105 -20
  137. data/lib/rbs/cli/diff.rb +16 -15
  138. data/lib/rbs/cli/validate.rb +63 -126
  139. data/lib/rbs/cli.rb +56 -24
  140. data/lib/rbs/collection/config/lockfile_generator.rb +14 -2
  141. data/lib/rbs/collection/sources/git.rb +1 -0
  142. data/lib/rbs/collection.rb +0 -1
  143. data/lib/rbs/definition.rb +6 -1
  144. data/lib/rbs/definition_builder/ancestor_builder.rb +121 -65
  145. data/lib/rbs/definition_builder/method_builder.rb +65 -30
  146. data/lib/rbs/definition_builder.rb +177 -20
  147. data/lib/rbs/diff.rb +7 -1
  148. data/lib/rbs/environment/class_entry.rb +69 -0
  149. data/lib/rbs/environment/module_entry.rb +66 -0
  150. data/lib/rbs/environment.rb +402 -214
  151. data/lib/rbs/environment_loader.rb +2 -8
  152. data/lib/rbs/errors.rb +31 -21
  153. data/lib/rbs/inline_parser/comment_association.rb +117 -0
  154. data/lib/rbs/inline_parser.rb +542 -0
  155. data/lib/rbs/location_aux.rb +36 -4
  156. data/lib/rbs/locator.rb +5 -1
  157. data/lib/rbs/method_type.rb +5 -3
  158. data/lib/rbs/namespace.rb +0 -7
  159. data/lib/rbs/parser_aux.rb +35 -7
  160. data/lib/rbs/prototype/helpers.rb +57 -0
  161. data/lib/rbs/prototype/rb.rb +3 -28
  162. data/lib/rbs/prototype/rbi.rb +3 -20
  163. data/lib/rbs/prototype/runtime.rb +10 -2
  164. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  165. data/lib/rbs/resolver/type_name_resolver.rb +116 -38
  166. data/lib/rbs/source.rb +99 -0
  167. data/lib/rbs/subtractor.rb +7 -4
  168. data/lib/rbs/test/type_check.rb +19 -2
  169. data/lib/rbs/type_name.rb +1 -8
  170. data/lib/rbs/types.rb +91 -79
  171. data/lib/rbs/unit_test/convertibles.rb +1 -0
  172. data/lib/rbs/unit_test/type_assertions.rb +35 -8
  173. data/lib/rbs/validator.rb +2 -2
  174. data/lib/rbs/version.rb +1 -1
  175. data/lib/rbs.rb +13 -2
  176. data/lib/rdoc/discover.rb +1 -1
  177. data/lib/rdoc_plugin/parser.rb +3 -3
  178. data/rbs.gemspec +4 -2
  179. data/rust/.gitignore +1 -0
  180. data/rust/Cargo.lock +378 -0
  181. data/rust/Cargo.toml +7 -0
  182. data/rust/ruby-rbs/Cargo.toml +22 -0
  183. data/rust/ruby-rbs/build.rs +764 -0
  184. data/rust/ruby-rbs/examples/locations.rs +60 -0
  185. data/rust/ruby-rbs/src/lib.rs +1 -0
  186. data/rust/ruby-rbs/src/node/mod.rs +742 -0
  187. data/rust/ruby-rbs/tests/sanity.rs +47 -0
  188. data/rust/ruby-rbs/vendor/rbs/config.yml +1 -0
  189. data/rust/ruby-rbs-sys/Cargo.toml +23 -0
  190. data/rust/ruby-rbs-sys/build.rs +204 -0
  191. data/rust/ruby-rbs-sys/src/lib.rs +50 -0
  192. data/rust/ruby-rbs-sys/vendor/rbs/include +1 -0
  193. data/rust/ruby-rbs-sys/vendor/rbs/src +1 -0
  194. data/rust/ruby-rbs-sys/wrapper.h +1 -0
  195. data/schema/typeParam.json +17 -1
  196. data/sig/ancestor_builder.rbs +1 -1
  197. data/sig/annotate/formatter.rbs +2 -2
  198. data/sig/annotate/rdoc_annotater.rbs +1 -1
  199. data/sig/ast/ruby/annotations.rbs +421 -0
  200. data/sig/ast/ruby/comment_block.rbs +127 -0
  201. data/sig/ast/ruby/declarations.rbs +158 -0
  202. data/sig/ast/ruby/helpers/constant_helper.rbs +11 -0
  203. data/sig/ast/ruby/helpers/location_helper.rbs +15 -0
  204. data/sig/ast/ruby/members.rbs +178 -0
  205. data/sig/buffer.rbs +63 -5
  206. data/sig/cli/diff.rbs +5 -11
  207. data/sig/cli/validate.rbs +12 -8
  208. data/sig/cli.rbs +18 -18
  209. data/sig/definition.rbs +6 -0
  210. data/sig/definition_builder.rbs +3 -1
  211. data/sig/environment/class_entry.rbs +50 -0
  212. data/sig/environment/module_entry.rbs +50 -0
  213. data/sig/environment.rbs +94 -87
  214. data/sig/errors.rbs +26 -20
  215. data/sig/inline_parser/comment_association.rbs +71 -0
  216. data/sig/inline_parser.rbs +124 -0
  217. data/sig/location.rbs +32 -7
  218. data/sig/locator.rbs +0 -2
  219. data/sig/manifest.yaml +0 -1
  220. data/sig/method_builder.rbs +9 -4
  221. data/sig/namespace.rbs +0 -5
  222. data/sig/parser.rbs +67 -13
  223. data/sig/prototype/helpers.rbs +2 -0
  224. data/sig/resolver/type_name_resolver.rbs +35 -7
  225. data/sig/source.rbs +48 -0
  226. data/sig/type_param.rbs +13 -8
  227. data/sig/typename.rbs +0 -5
  228. data/sig/types.rbs +10 -8
  229. data/sig/unit_test/spy.rbs +0 -8
  230. data/sig/unit_test/type_assertions.rbs +11 -0
  231. data/src/ast.c +1604 -0
  232. data/src/lexer.c +3194 -0
  233. data/src/lexer.re +154 -0
  234. data/src/lexstate.c +212 -0
  235. data/src/location.c +31 -0
  236. data/src/parser.c +4205 -0
  237. data/src/string.c +41 -0
  238. data/src/util/rbs_allocator.c +165 -0
  239. data/src/util/rbs_assert.c +19 -0
  240. data/src/util/rbs_buffer.c +54 -0
  241. data/src/util/rbs_constant_pool.c +18 -92
  242. data/src/util/rbs_encoding.c +21308 -0
  243. data/src/util/rbs_unescape.c +167 -0
  244. data/stdlib/bigdecimal/0/big_decimal.rbs +116 -98
  245. data/stdlib/bigdecimal-math/0/big_math.rbs +169 -8
  246. data/stdlib/cgi/0/core.rbs +9 -393
  247. data/stdlib/cgi/0/manifest.yaml +1 -0
  248. data/stdlib/cgi-escape/0/escape.rbs +171 -0
  249. data/stdlib/coverage/0/coverage.rbs +7 -4
  250. data/stdlib/date/0/date.rbs +92 -79
  251. data/stdlib/date/0/date_time.rbs +25 -24
  252. data/stdlib/delegate/0/delegator.rbs +10 -7
  253. data/stdlib/did_you_mean/0/did_you_mean.rbs +17 -16
  254. data/stdlib/digest/0/digest.rbs +110 -0
  255. data/stdlib/erb/0/erb.rbs +748 -347
  256. data/stdlib/etc/0/etc.rbs +55 -50
  257. data/stdlib/fileutils/0/fileutils.rbs +158 -139
  258. data/stdlib/forwardable/0/forwardable.rbs +13 -10
  259. data/stdlib/io-console/0/io-console.rbs +2 -2
  260. data/stdlib/json/0/json.rbs +226 -179
  261. data/stdlib/monitor/0/monitor.rbs +3 -3
  262. data/stdlib/net-http/0/net-http.rbs +162 -134
  263. data/stdlib/objspace/0/objspace.rbs +17 -34
  264. data/stdlib/open-uri/0/open-uri.rbs +48 -8
  265. data/stdlib/open3/0/open3.rbs +469 -10
  266. data/stdlib/openssl/0/openssl.rbs +475 -357
  267. data/stdlib/optparse/0/optparse.rbs +26 -17
  268. data/stdlib/pathname/0/pathname.rbs +11 -1381
  269. data/stdlib/pp/0/pp.rbs +9 -8
  270. data/stdlib/prettyprint/0/prettyprint.rbs +7 -7
  271. data/stdlib/pstore/0/pstore.rbs +35 -30
  272. data/stdlib/psych/0/psych.rbs +65 -12
  273. data/stdlib/psych/0/store.rbs +2 -4
  274. data/stdlib/pty/0/pty.rbs +9 -6
  275. data/stdlib/random-formatter/0/random-formatter.rbs +277 -0
  276. data/stdlib/rdoc/0/code_object.rbs +4 -3
  277. data/stdlib/rdoc/0/comment.rbs +2 -0
  278. data/stdlib/rdoc/0/options.rbs +76 -0
  279. data/stdlib/rdoc/0/parser.rbs +1 -1
  280. data/stdlib/rdoc/0/rdoc.rbs +7 -5
  281. data/stdlib/rdoc/0/store.rbs +2 -2
  282. data/stdlib/resolv/0/resolv.rbs +25 -68
  283. data/stdlib/ripper/0/ripper.rbs +25 -19
  284. data/stdlib/securerandom/0/manifest.yaml +2 -0
  285. data/stdlib/securerandom/0/securerandom.rbs +7 -20
  286. data/stdlib/shellwords/0/shellwords.rbs +2 -2
  287. data/stdlib/singleton/0/singleton.rbs +3 -0
  288. data/stdlib/socket/0/addrinfo.rbs +9 -9
  289. data/stdlib/socket/0/basic_socket.rbs +3 -3
  290. data/stdlib/socket/0/ip_socket.rbs +10 -8
  291. data/stdlib/socket/0/socket.rbs +23 -10
  292. data/stdlib/socket/0/tcp_server.rbs +1 -1
  293. data/stdlib/socket/0/tcp_socket.rbs +11 -3
  294. data/stdlib/socket/0/udp_socket.rbs +1 -1
  295. data/stdlib/socket/0/unix_server.rbs +1 -1
  296. data/stdlib/stringio/0/stringio.rbs +1177 -85
  297. data/stdlib/strscan/0/string_scanner.rbs +27 -25
  298. data/stdlib/tempfile/0/tempfile.rbs +25 -21
  299. data/stdlib/time/0/time.rbs +8 -6
  300. data/stdlib/timeout/0/timeout.rbs +63 -7
  301. data/stdlib/tsort/0/cyclic.rbs +3 -0
  302. data/stdlib/tsort/0/tsort.rbs +7 -6
  303. data/stdlib/uri/0/common.rbs +42 -20
  304. data/stdlib/uri/0/file.rbs +3 -3
  305. data/stdlib/uri/0/generic.rbs +26 -18
  306. data/stdlib/uri/0/http.rbs +2 -2
  307. data/stdlib/uri/0/ldap.rbs +2 -2
  308. data/stdlib/uri/0/mailto.rbs +3 -3
  309. data/stdlib/uri/0/rfc2396_parser.rbs +12 -12
  310. data/stdlib/zlib/0/deflate.rbs +4 -3
  311. data/stdlib/zlib/0/gzip_reader.rbs +6 -6
  312. data/stdlib/zlib/0/gzip_writer.rbs +14 -12
  313. data/stdlib/zlib/0/inflate.rbs +1 -1
  314. data/stdlib/zlib/0/need_dict.rbs +1 -1
  315. data/stdlib/zlib/0/zstream.rbs +1 -0
  316. metadata +121 -18
  317. data/ext/rbs_extension/lexer.c +0 -2728
  318. data/ext/rbs_extension/lexer.h +0 -179
  319. data/ext/rbs_extension/lexer.re +0 -147
  320. data/ext/rbs_extension/lexstate.c +0 -175
  321. data/ext/rbs_extension/location.c +0 -325
  322. data/ext/rbs_extension/location.h +0 -85
  323. data/ext/rbs_extension/parser.c +0 -2982
  324. data/ext/rbs_extension/parser.h +0 -18
  325. data/ext/rbs_extension/parserstate.c +0 -411
  326. data/ext/rbs_extension/parserstate.h +0 -163
  327. data/ext/rbs_extension/unescape.c +0 -32
  328. data/include/rbs/ruby_objs.h +0 -72
  329. data/src/constants.c +0 -153
  330. data/src/ruby_objs.c +0 -799
@@ -0,0 +1,37 @@
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/ext/rbs_extension/ast_translation.h.erb */
6
+ /*----------------------------------------------------------------------------*/
7
+
8
+ #ifndef RBS_EXTENSION_AST_TRANSLATION_H
9
+ #define RBS_EXTENSION_AST_TRANSLATION_H
10
+
11
+ #include "compat.h"
12
+
13
+ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
14
+ #include "ruby.h"
15
+ #include "ruby/encoding.h"
16
+ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
17
+
18
+ #include "rbs/ast.h"
19
+ #include "rbs/location.h"
20
+
21
+ /// A bag of values needed when copying RBS C structs into Ruby objects.
22
+ typedef struct rbs_translation_context {
23
+ rbs_constant_pool_t *constant_pool;
24
+ VALUE buffer;
25
+ rb_encoding *encoding;
26
+ } rbs_translation_context_t;
27
+
28
+ rbs_translation_context_t rbs_translation_context_create(rbs_constant_pool_t *, VALUE buffer_string, rb_encoding *ruby_encoding);
29
+
30
+ VALUE rbs_node_list_to_ruby_array(rbs_translation_context_t, rbs_node_list_t *list);
31
+ VALUE rbs_hash_to_ruby_hash(rbs_translation_context_t, rbs_hash_t *hash);
32
+ VALUE rbs_struct_to_ruby_value(rbs_translation_context_t, rbs_node_t *instance);
33
+
34
+ extern VALUE EMPTY_ARRAY;
35
+ extern VALUE EMPTY_HASH;
36
+
37
+ #endif
@@ -0,0 +1,185 @@
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/ext/rbs_extension/class_constants.c.erb */
6
+ /*----------------------------------------------------------------------------*/
7
+
8
+ #include "rbs_extension.h"
9
+
10
+ VALUE RBS;
11
+ VALUE RBS_AST;
12
+ VALUE RBS_AST_Declarations;
13
+ VALUE RBS_AST_Directives;
14
+ VALUE RBS_AST_Members;
15
+ VALUE RBS_AST_Ruby;
16
+ VALUE RBS_AST_Ruby_Annotations;
17
+ VALUE RBS_Parser;
18
+ VALUE RBS_Types;
19
+ VALUE RBS_Types_Bases;
20
+
21
+ VALUE RBS_AST_Annotation;
22
+ VALUE RBS_AST_Comment;
23
+ VALUE RBS_AST_Declarations_Class;
24
+ VALUE RBS_AST_Declarations_Class_Super;
25
+ VALUE RBS_AST_Declarations_ClassAlias;
26
+ VALUE RBS_AST_Declarations_Constant;
27
+ VALUE RBS_AST_Declarations_Global;
28
+ VALUE RBS_AST_Declarations_Interface;
29
+ VALUE RBS_AST_Declarations_Module;
30
+ VALUE RBS_AST_Declarations_Module_Self;
31
+ VALUE RBS_AST_Declarations_ModuleAlias;
32
+ VALUE RBS_AST_Declarations_TypeAlias;
33
+ VALUE RBS_AST_Directives_Use;
34
+ VALUE RBS_AST_Directives_Use_SingleClause;
35
+ VALUE RBS_AST_Directives_Use_WildcardClause;
36
+ VALUE RBS_AST_Members_Alias;
37
+ VALUE RBS_AST_Members_AttrAccessor;
38
+ VALUE RBS_AST_Members_AttrReader;
39
+ VALUE RBS_AST_Members_AttrWriter;
40
+ VALUE RBS_AST_Members_ClassInstanceVariable;
41
+ VALUE RBS_AST_Members_ClassVariable;
42
+ VALUE RBS_AST_Members_Extend;
43
+ VALUE RBS_AST_Members_Include;
44
+ VALUE RBS_AST_Members_InstanceVariable;
45
+ VALUE RBS_AST_Members_MethodDefinition;
46
+ VALUE RBS_AST_Members_MethodDefinition_Overload;
47
+ VALUE RBS_AST_Members_Prepend;
48
+ VALUE RBS_AST_Members_Private;
49
+ VALUE RBS_AST_Members_Public;
50
+ VALUE RBS_AST_Ruby_Annotations_BlockParamTypeAnnotation;
51
+ VALUE RBS_AST_Ruby_Annotations_ClassAliasAnnotation;
52
+ VALUE RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation;
53
+ VALUE RBS_AST_Ruby_Annotations_DoubleSplatParamTypeAnnotation;
54
+ VALUE RBS_AST_Ruby_Annotations_InstanceVariableAnnotation;
55
+ VALUE RBS_AST_Ruby_Annotations_MethodTypesAnnotation;
56
+ VALUE RBS_AST_Ruby_Annotations_ModuleAliasAnnotation;
57
+ VALUE RBS_AST_Ruby_Annotations_NodeTypeAssertion;
58
+ VALUE RBS_AST_Ruby_Annotations_ParamTypeAnnotation;
59
+ VALUE RBS_AST_Ruby_Annotations_ReturnTypeAnnotation;
60
+ VALUE RBS_AST_Ruby_Annotations_SkipAnnotation;
61
+ VALUE RBS_AST_Ruby_Annotations_SplatParamTypeAnnotation;
62
+ VALUE RBS_AST_Ruby_Annotations_TypeApplicationAnnotation;
63
+ VALUE RBS_AST_TypeParam;
64
+ VALUE RBS_MethodType;
65
+ VALUE RBS_Namespace;
66
+ VALUE RBS_TypeName;
67
+ VALUE RBS_Types_Alias;
68
+ VALUE RBS_Types_Bases_Any;
69
+ VALUE RBS_Types_Bases_Bool;
70
+ VALUE RBS_Types_Bases_Bottom;
71
+ VALUE RBS_Types_Bases_Class;
72
+ VALUE RBS_Types_Bases_Instance;
73
+ VALUE RBS_Types_Bases_Nil;
74
+ VALUE RBS_Types_Bases_Self;
75
+ VALUE RBS_Types_Bases_Top;
76
+ VALUE RBS_Types_Bases_Void;
77
+ VALUE RBS_Types_Block;
78
+ VALUE RBS_Types_ClassInstance;
79
+ VALUE RBS_Types_ClassSingleton;
80
+ VALUE RBS_Types_Function;
81
+ VALUE RBS_Types_Function_Param;
82
+ VALUE RBS_Types_Interface;
83
+ VALUE RBS_Types_Intersection;
84
+ VALUE RBS_Types_Literal;
85
+ VALUE RBS_Types_Optional;
86
+ VALUE RBS_Types_Proc;
87
+ VALUE RBS_Types_Record;
88
+ VALUE RBS_Types_Tuple;
89
+ VALUE RBS_Types_Union;
90
+ VALUE RBS_Types_UntypedFunction;
91
+ VALUE RBS_Types_Variable;
92
+
93
+ VALUE RBS_ParsingError;
94
+
95
+ #define IMPORT_CONSTANT(var, parent, name) \
96
+ { \
97
+ var = rb_const_get(parent, rb_intern(name)); \
98
+ rb_gc_register_mark_object(var); \
99
+ }
100
+
101
+ void rbs__init_constants(void) {
102
+ IMPORT_CONSTANT(RBS, rb_cObject, "RBS");
103
+ IMPORT_CONSTANT(RBS_ParsingError, RBS, "ParsingError");
104
+
105
+ IMPORT_CONSTANT(RBS_AST, RBS, "AST");
106
+ IMPORT_CONSTANT(RBS_AST_Declarations, RBS_AST, "Declarations");
107
+ IMPORT_CONSTANT(RBS_AST_Directives, RBS_AST, "Directives");
108
+ IMPORT_CONSTANT(RBS_AST_Members, RBS_AST, "Members");
109
+ IMPORT_CONSTANT(RBS_AST_Ruby, RBS_AST, "Ruby");
110
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations, RBS_AST_Ruby, "Annotations");
111
+ IMPORT_CONSTANT(RBS_Types, RBS, "Types");
112
+ IMPORT_CONSTANT(RBS_Types_Bases, RBS_Types, "Bases");
113
+
114
+ IMPORT_CONSTANT(RBS_AST_Annotation, RBS_AST, "Annotation");
115
+ IMPORT_CONSTANT(RBS_AST_Comment, RBS_AST, "Comment");
116
+ IMPORT_CONSTANT(RBS_AST_Declarations_Class, RBS_AST_Declarations, "Class");
117
+ IMPORT_CONSTANT(RBS_AST_Declarations_Class_Super, RBS_AST_Declarations_Class, "Super");
118
+ IMPORT_CONSTANT(RBS_AST_Declarations_ClassAlias, RBS_AST_Declarations, "ClassAlias");
119
+ IMPORT_CONSTANT(RBS_AST_Declarations_Constant, RBS_AST_Declarations, "Constant");
120
+ IMPORT_CONSTANT(RBS_AST_Declarations_Global, RBS_AST_Declarations, "Global");
121
+ IMPORT_CONSTANT(RBS_AST_Declarations_Interface, RBS_AST_Declarations, "Interface");
122
+ IMPORT_CONSTANT(RBS_AST_Declarations_Module, RBS_AST_Declarations, "Module");
123
+ IMPORT_CONSTANT(RBS_AST_Declarations_Module_Self, RBS_AST_Declarations_Module, "Self");
124
+ IMPORT_CONSTANT(RBS_AST_Declarations_ModuleAlias, RBS_AST_Declarations, "ModuleAlias");
125
+ IMPORT_CONSTANT(RBS_AST_Declarations_TypeAlias, RBS_AST_Declarations, "TypeAlias");
126
+ IMPORT_CONSTANT(RBS_AST_Directives_Use, RBS_AST_Directives, "Use");
127
+ IMPORT_CONSTANT(RBS_AST_Directives_Use_SingleClause, RBS_AST_Directives_Use, "SingleClause");
128
+ IMPORT_CONSTANT(RBS_AST_Directives_Use_WildcardClause, RBS_AST_Directives_Use, "WildcardClause");
129
+ IMPORT_CONSTANT(RBS_AST_Members_Alias, RBS_AST_Members, "Alias");
130
+ IMPORT_CONSTANT(RBS_AST_Members_AttrAccessor, RBS_AST_Members, "AttrAccessor");
131
+ IMPORT_CONSTANT(RBS_AST_Members_AttrReader, RBS_AST_Members, "AttrReader");
132
+ IMPORT_CONSTANT(RBS_AST_Members_AttrWriter, RBS_AST_Members, "AttrWriter");
133
+ IMPORT_CONSTANT(RBS_AST_Members_ClassInstanceVariable, RBS_AST_Members, "ClassInstanceVariable");
134
+ IMPORT_CONSTANT(RBS_AST_Members_ClassVariable, RBS_AST_Members, "ClassVariable");
135
+ IMPORT_CONSTANT(RBS_AST_Members_Extend, RBS_AST_Members, "Extend");
136
+ IMPORT_CONSTANT(RBS_AST_Members_Include, RBS_AST_Members, "Include");
137
+ IMPORT_CONSTANT(RBS_AST_Members_InstanceVariable, RBS_AST_Members, "InstanceVariable");
138
+ IMPORT_CONSTANT(RBS_AST_Members_MethodDefinition, RBS_AST_Members, "MethodDefinition");
139
+ IMPORT_CONSTANT(RBS_AST_Members_MethodDefinition_Overload, RBS_AST_Members_MethodDefinition, "Overload");
140
+ IMPORT_CONSTANT(RBS_AST_Members_Prepend, RBS_AST_Members, "Prepend");
141
+ IMPORT_CONSTANT(RBS_AST_Members_Private, RBS_AST_Members, "Private");
142
+ IMPORT_CONSTANT(RBS_AST_Members_Public, RBS_AST_Members, "Public");
143
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_BlockParamTypeAnnotation, RBS_AST_Ruby_Annotations, "BlockParamTypeAnnotation");
144
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ClassAliasAnnotation, RBS_AST_Ruby_Annotations, "ClassAliasAnnotation");
145
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation, RBS_AST_Ruby_Annotations, "ColonMethodTypeAnnotation");
146
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_DoubleSplatParamTypeAnnotation, RBS_AST_Ruby_Annotations, "DoubleSplatParamTypeAnnotation");
147
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_InstanceVariableAnnotation, RBS_AST_Ruby_Annotations, "InstanceVariableAnnotation");
148
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_MethodTypesAnnotation, RBS_AST_Ruby_Annotations, "MethodTypesAnnotation");
149
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ModuleAliasAnnotation, RBS_AST_Ruby_Annotations, "ModuleAliasAnnotation");
150
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_NodeTypeAssertion, RBS_AST_Ruby_Annotations, "NodeTypeAssertion");
151
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ParamTypeAnnotation, RBS_AST_Ruby_Annotations, "ParamTypeAnnotation");
152
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_ReturnTypeAnnotation, RBS_AST_Ruby_Annotations, "ReturnTypeAnnotation");
153
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_SkipAnnotation, RBS_AST_Ruby_Annotations, "SkipAnnotation");
154
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_SplatParamTypeAnnotation, RBS_AST_Ruby_Annotations, "SplatParamTypeAnnotation");
155
+ IMPORT_CONSTANT(RBS_AST_Ruby_Annotations_TypeApplicationAnnotation, RBS_AST_Ruby_Annotations, "TypeApplicationAnnotation");
156
+ IMPORT_CONSTANT(RBS_AST_TypeParam, RBS_AST, "TypeParam");
157
+ IMPORT_CONSTANT(RBS_MethodType, RBS, "MethodType");
158
+ IMPORT_CONSTANT(RBS_Namespace, RBS, "Namespace");
159
+ IMPORT_CONSTANT(RBS_TypeName, RBS, "TypeName");
160
+ IMPORT_CONSTANT(RBS_Types_Alias, RBS_Types, "Alias");
161
+ IMPORT_CONSTANT(RBS_Types_Bases_Any, RBS_Types_Bases, "Any");
162
+ IMPORT_CONSTANT(RBS_Types_Bases_Bool, RBS_Types_Bases, "Bool");
163
+ IMPORT_CONSTANT(RBS_Types_Bases_Bottom, RBS_Types_Bases, "Bottom");
164
+ IMPORT_CONSTANT(RBS_Types_Bases_Class, RBS_Types_Bases, "Class");
165
+ IMPORT_CONSTANT(RBS_Types_Bases_Instance, RBS_Types_Bases, "Instance");
166
+ IMPORT_CONSTANT(RBS_Types_Bases_Nil, RBS_Types_Bases, "Nil");
167
+ IMPORT_CONSTANT(RBS_Types_Bases_Self, RBS_Types_Bases, "Self");
168
+ IMPORT_CONSTANT(RBS_Types_Bases_Top, RBS_Types_Bases, "Top");
169
+ IMPORT_CONSTANT(RBS_Types_Bases_Void, RBS_Types_Bases, "Void");
170
+ IMPORT_CONSTANT(RBS_Types_Block, RBS_Types, "Block");
171
+ IMPORT_CONSTANT(RBS_Types_ClassInstance, RBS_Types, "ClassInstance");
172
+ IMPORT_CONSTANT(RBS_Types_ClassSingleton, RBS_Types, "ClassSingleton");
173
+ IMPORT_CONSTANT(RBS_Types_Function, RBS_Types, "Function");
174
+ IMPORT_CONSTANT(RBS_Types_Function_Param, RBS_Types_Function, "Param");
175
+ IMPORT_CONSTANT(RBS_Types_Interface, RBS_Types, "Interface");
176
+ IMPORT_CONSTANT(RBS_Types_Intersection, RBS_Types, "Intersection");
177
+ IMPORT_CONSTANT(RBS_Types_Literal, RBS_Types, "Literal");
178
+ IMPORT_CONSTANT(RBS_Types_Optional, RBS_Types, "Optional");
179
+ IMPORT_CONSTANT(RBS_Types_Proc, RBS_Types, "Proc");
180
+ IMPORT_CONSTANT(RBS_Types_Record, RBS_Types, "Record");
181
+ IMPORT_CONSTANT(RBS_Types_Tuple, RBS_Types, "Tuple");
182
+ IMPORT_CONSTANT(RBS_Types_Union, RBS_Types, "Union");
183
+ IMPORT_CONSTANT(RBS_Types_UntypedFunction, RBS_Types, "UntypedFunction");
184
+ IMPORT_CONSTANT(RBS_Types_Variable, RBS_Types, "Variable");
185
+ }
@@ -2,18 +2,26 @@
2
2
  /* This file is generated by the templates/template.rb script and should not */
3
3
  /* be modified manually. */
4
4
  /* To change the template see */
5
- /* templates/include/rbs/constants.h.erb */
5
+ /* templates/ext/rbs_extension/class_constants.h.erb */
6
6
  /*----------------------------------------------------------------------------*/
7
7
 
8
8
  #ifndef RBS__CONSTANTS_H
9
9
  #define RBS__CONSTANTS_H
10
10
 
11
+ #include "compat.h"
12
+
13
+ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
14
+ #include "ruby.h"
15
+ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
16
+
11
17
  extern VALUE RBS;
12
18
 
13
19
  extern VALUE RBS_AST;
14
20
  extern VALUE RBS_AST_Declarations;
15
21
  extern VALUE RBS_AST_Directives;
16
22
  extern VALUE RBS_AST_Members;
23
+ extern VALUE RBS_AST_Ruby;
24
+ extern VALUE RBS_AST_Ruby_Annotations;
17
25
  extern VALUE RBS_Types;
18
26
  extern VALUE RBS_Types_Bases;
19
27
  extern VALUE RBS_ParsingError;
@@ -47,6 +55,19 @@ extern VALUE RBS_AST_Members_MethodDefinition_Overload;
47
55
  extern VALUE RBS_AST_Members_Prepend;
48
56
  extern VALUE RBS_AST_Members_Private;
49
57
  extern VALUE RBS_AST_Members_Public;
58
+ extern VALUE RBS_AST_Ruby_Annotations_BlockParamTypeAnnotation;
59
+ extern VALUE RBS_AST_Ruby_Annotations_ClassAliasAnnotation;
60
+ extern VALUE RBS_AST_Ruby_Annotations_ColonMethodTypeAnnotation;
61
+ extern VALUE RBS_AST_Ruby_Annotations_DoubleSplatParamTypeAnnotation;
62
+ extern VALUE RBS_AST_Ruby_Annotations_InstanceVariableAnnotation;
63
+ extern VALUE RBS_AST_Ruby_Annotations_MethodTypesAnnotation;
64
+ extern VALUE RBS_AST_Ruby_Annotations_ModuleAliasAnnotation;
65
+ extern VALUE RBS_AST_Ruby_Annotations_NodeTypeAssertion;
66
+ extern VALUE RBS_AST_Ruby_Annotations_ParamTypeAnnotation;
67
+ extern VALUE RBS_AST_Ruby_Annotations_ReturnTypeAnnotation;
68
+ extern VALUE RBS_AST_Ruby_Annotations_SkipAnnotation;
69
+ extern VALUE RBS_AST_Ruby_Annotations_SplatParamTypeAnnotation;
70
+ extern VALUE RBS_AST_Ruby_Annotations_TypeApplicationAnnotation;
50
71
  extern VALUE RBS_AST_TypeParam;
51
72
  extern VALUE RBS_MethodType;
52
73
  extern VALUE RBS_Namespace;
@@ -0,0 +1,10 @@
1
+ #ifdef __clang__
2
+ #define SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN \
3
+ _Pragma("clang diagnostic push") \
4
+ _Pragma("clang diagnostic ignored \"-Wc2x-extensions\"")
5
+ #define SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END \
6
+ _Pragma("clang diagnostic pop")
7
+ #else
8
+ #define SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
9
+ #define SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
10
+ #endif
@@ -11,5 +11,29 @@ root_dir = File.expand_path('../../../', __FILE__)
11
11
  $srcs = Dir.glob("#{root_dir}/src/**/*.c") +
12
12
  Dir.glob("#{root_dir}/ext/rbs_extension/*.c")
13
13
 
14
- append_cflags ['-std=gnu99']
14
+ append_cflags [
15
+ '-std=gnu99',
16
+ '-Wimplicit-fallthrough',
17
+ '-Wunused-result',
18
+ '-Wc++-compat',
19
+ ]
20
+
21
+ if ENV['DEBUG']
22
+ append_cflags ['-O0', '-pg']
23
+ else
24
+ append_cflags ['-DNDEBUG']
25
+ end
26
+ if ENV["TEST_NO_C23"]
27
+ puts "Adding -Wc2x-extensions to CFLAGS"
28
+ $CFLAGS << " -Werror -Wc2x-extensions"
29
+ end
30
+
15
31
  create_makefile 'rbs_extension'
32
+
33
+ # Only generate compile_commands.json when compiling through Rake tasks
34
+ # This is to avoid adding extconf_compile_commands_json as a runtime dependency
35
+ if ENV["COMPILE_COMMANDS_JSON"]
36
+ require 'extconf_compile_commands_json'
37
+ ExtconfCompileCommandsJson.generate!
38
+ ExtconfCompileCommandsJson.symlink!
39
+ end
@@ -0,0 +1,294 @@
1
+ #include "legacy_location.h"
2
+ #include "rbs_extension.h"
3
+ #include "ruby/internal/symbol.h"
4
+
5
+ #define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
6
+ #define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
7
+ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
8
+ #define NULL_LOC_RANGE_P(rg) ((rg).start == -1)
9
+
10
+ rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 };
11
+ VALUE RBS_Location;
12
+
13
+ static void check_children_max(unsigned short n) {
14
+ size_t max = sizeof(rbs_loc_entry_bitmap) * 8;
15
+ if (n > max) {
16
+ rb_raise(rb_eRuntimeError, "Too many children added to location: %d", n);
17
+ }
18
+ }
19
+
20
+ void rbs_loc_legacy_alloc_children(rbs_loc *loc, unsigned short cap) {
21
+ check_children_max(cap);
22
+
23
+ size_t s = RBS_LOC_CHILDREN_SIZE(cap);
24
+ loc->children = (rbs_loc_children *) malloc(s);
25
+ *loc->children = (rbs_loc_children) {
26
+ .len = 0,
27
+ .required_p = 0,
28
+ .cap = cap,
29
+ .entries = { { 0 } },
30
+ };
31
+ }
32
+
33
+ static void check_children_cap(rbs_loc *loc) {
34
+ if (loc->children == NULL) {
35
+ rbs_loc_legacy_alloc_children(loc, 1);
36
+ } else {
37
+ if (loc->children->len == loc->children->cap) {
38
+ check_children_max(loc->children->cap + 1);
39
+ size_t s = RBS_LOC_CHILDREN_SIZE(++loc->children->cap);
40
+ loc->children = (rbs_loc_children *) realloc(loc->children, s);
41
+ }
42
+ }
43
+ }
44
+
45
+ void rbs_loc_legacy_add_optional_child(rbs_loc *loc, ID name, rbs_loc_range r) {
46
+ check_children_cap(loc);
47
+
48
+ unsigned short i = loc->children->len++;
49
+ loc->children->entries[i] = (rbs_loc_entry) {
50
+ .name = name,
51
+ .rg = r,
52
+ };
53
+ }
54
+
55
+ void rbs_loc_legacy_add_required_child(rbs_loc *loc, ID name, rbs_loc_range r) {
56
+ rbs_loc_legacy_add_optional_child(loc, name, r);
57
+
58
+ unsigned short last_index = loc->children->len - 1;
59
+ loc->children->required_p |= 1 << last_index;
60
+ }
61
+
62
+ void rbs_loc_init(rbs_loc *loc, VALUE buffer, rbs_loc_range rg) {
63
+ *loc = (rbs_loc) {
64
+ .buffer = buffer,
65
+ .rg = rg,
66
+ .children = NULL,
67
+ };
68
+ }
69
+
70
+ void rbs_loc_free(rbs_loc *loc) {
71
+ free(loc->children);
72
+ ruby_xfree(loc);
73
+ }
74
+
75
+ static void rbs_loc_mark(void *ptr) {
76
+ rbs_loc *loc = (rbs_loc *) ptr;
77
+ rb_gc_mark(loc->buffer);
78
+ }
79
+
80
+ static size_t rbs_loc_memsize(const void *ptr) {
81
+ const rbs_loc *loc = (const rbs_loc *) ptr;
82
+ if (loc->children == NULL) {
83
+ return sizeof(rbs_loc);
84
+ } else {
85
+ return sizeof(rbs_loc) + RBS_LOC_CHILDREN_SIZE(loc->children->cap);
86
+ }
87
+ }
88
+
89
+ static rb_data_type_t location_type = {
90
+ "RBS::Location",
91
+ { rbs_loc_mark, (RUBY_DATA_FUNC) rbs_loc_free, rbs_loc_memsize },
92
+ 0,
93
+ 0,
94
+ RUBY_TYPED_FREE_IMMEDIATELY
95
+ };
96
+
97
+ static VALUE location_s_allocate(VALUE klass) {
98
+ rbs_loc *loc;
99
+ VALUE obj = TypedData_Make_Struct(klass, rbs_loc, &location_type, loc);
100
+
101
+ rbs_loc_init(loc, Qnil, RBS_LOC_NULL_RANGE);
102
+
103
+ return obj;
104
+ }
105
+
106
+ rbs_loc *rbs_check_location(VALUE obj) {
107
+ return (rbs_loc *) rb_check_typeddata(obj, &location_type);
108
+ }
109
+
110
+ static VALUE location_initialize(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
111
+ rbs_loc *loc = rbs_check_location(self);
112
+
113
+ int start = FIX2INT(start_pos);
114
+ int end = FIX2INT(end_pos);
115
+
116
+ *loc = (rbs_loc) {
117
+ .buffer = buffer,
118
+ .rg = (rbs_loc_range) { start, end },
119
+ .children = NULL,
120
+ };
121
+
122
+ return Qnil;
123
+ }
124
+
125
+ static VALUE location_initialize_copy(VALUE self, VALUE other) {
126
+ rbs_loc *self_loc = rbs_check_location(self);
127
+ rbs_loc *other_loc = rbs_check_location(other);
128
+
129
+ *self_loc = (rbs_loc) {
130
+ .buffer = other_loc->buffer,
131
+ .rg = other_loc->rg,
132
+ .children = NULL,
133
+ };
134
+
135
+ if (other_loc->children != NULL) {
136
+ rbs_loc_legacy_alloc_children(self_loc, other_loc->children->cap);
137
+ memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));
138
+ }
139
+
140
+ return Qnil;
141
+ }
142
+
143
+ static VALUE location_buffer(VALUE self) {
144
+ rbs_loc *loc = rbs_check_location(self);
145
+ return loc->buffer;
146
+ }
147
+
148
+ static VALUE location_start_pos(VALUE self) {
149
+ rbs_loc *loc = rbs_check_location(self);
150
+ return INT2FIX(loc->rg.start);
151
+ }
152
+
153
+ static VALUE location_end_pos(VALUE self) {
154
+ rbs_loc *loc = rbs_check_location(self);
155
+ return INT2FIX(loc->rg.end);
156
+ }
157
+
158
+ static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
159
+ rbs_loc *loc = rbs_check_location(self);
160
+
161
+ rbs_loc_range rg = {
162
+ .start = FIX2INT(start),
163
+ .end = FIX2INT(end),
164
+ };
165
+
166
+ rbs_loc_legacy_add_required_child(loc, rb_sym2id(name), rg);
167
+
168
+ return Qnil;
169
+ }
170
+
171
+ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VALUE end) {
172
+ rbs_loc *loc = rbs_check_location(self);
173
+
174
+ rbs_loc_range rg = {
175
+ .start = FIX2INT(start),
176
+ .end = FIX2INT(end),
177
+ };
178
+
179
+ rbs_loc_legacy_add_optional_child(loc, rb_sym2id(name), rg);
180
+
181
+ return Qnil;
182
+ }
183
+
184
+ static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
185
+ rbs_loc *loc = rbs_check_location(self);
186
+
187
+ rbs_loc_legacy_add_optional_child(loc, rb_sym2id(name), RBS_LOC_NULL_RANGE);
188
+
189
+ return Qnil;
190
+ }
191
+
192
+ VALUE rbs_new_location(VALUE buffer, rbs_range_t rg) {
193
+ rbs_loc *loc;
194
+ VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
195
+
196
+ rbs_loc_init(loc, buffer, (rbs_loc_range) { rg.start.char_pos, rg.end.char_pos });
197
+
198
+ return obj;
199
+ }
200
+
201
+ VALUE rbs_new_location2(VALUE buffer, int start_char, int end_char) {
202
+ rbs_loc *loc;
203
+ VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
204
+
205
+ rbs_loc_init(loc, buffer, (rbs_loc_range) { .start = start_char, .end = end_char });
206
+
207
+ return obj;
208
+ }
209
+
210
+ static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
211
+ rbs_loc *loc;
212
+ VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
213
+
214
+ rbs_loc_init(loc, buffer, rg);
215
+
216
+ return obj;
217
+ }
218
+
219
+ static VALUE location_aref(VALUE self, VALUE name) {
220
+ rbs_loc *loc = rbs_check_location(self);
221
+
222
+ ID id = rb_sym2id(name);
223
+
224
+ if (loc->children != NULL) {
225
+ for (unsigned short i = 0; i < loc->children->len; i++) {
226
+ if (loc->children->entries[i].name == id) {
227
+ rbs_loc_range result = loc->children->entries[i].rg;
228
+
229
+ if (RBS_LOC_OPTIONAL_P(loc, i) && NULL_LOC_RANGE_P(result)) {
230
+ return Qnil;
231
+ } else {
232
+ return rbs_new_location_from_loc_range(loc->buffer, result);
233
+ }
234
+ }
235
+ }
236
+ }
237
+
238
+ VALUE string = rb_funcall(name, rb_intern("to_s"), 0);
239
+ rb_raise(rb_eRuntimeError, "Unknown child name given: %s", RSTRING_PTR(string));
240
+ }
241
+
242
+ static VALUE location_optional_keys(VALUE self) {
243
+ VALUE keys = rb_ary_new();
244
+
245
+ rbs_loc *loc = rbs_check_location(self);
246
+ rbs_loc_children *children = loc->children;
247
+ if (children == NULL) {
248
+ return keys;
249
+ }
250
+
251
+ for (unsigned short i = 0; i < children->len; i++) {
252
+ if (RBS_LOC_OPTIONAL_P(loc, i)) {
253
+ VALUE key_sym = rb_id2sym(children->entries[i].name);
254
+ rb_ary_push(keys, key_sym);
255
+ }
256
+ }
257
+
258
+ return keys;
259
+ }
260
+
261
+ static VALUE location_required_keys(VALUE self) {
262
+ VALUE keys = rb_ary_new();
263
+
264
+ rbs_loc *loc = rbs_check_location(self);
265
+ rbs_loc_children *children = loc->children;
266
+ if (children == NULL) {
267
+ return keys;
268
+ }
269
+
270
+ for (unsigned short i = 0; i < children->len; i++) {
271
+ if (RBS_LOC_REQUIRED_P(loc, i)) {
272
+ VALUE key_sym = rb_id2sym(children->entries[i].name);
273
+ rb_ary_push(keys, key_sym);
274
+ }
275
+ }
276
+
277
+ return keys;
278
+ }
279
+
280
+ void rbs__init_location(void) {
281
+ RBS_Location = rb_define_class_under(RBS, "Location", rb_cObject);
282
+ rb_define_alloc_func(RBS_Location, location_s_allocate);
283
+ rb_define_private_method(RBS_Location, "initialize", location_initialize, 3);
284
+ rb_define_private_method(RBS_Location, "initialize_copy", location_initialize_copy, 1);
285
+ rb_define_method(RBS_Location, "buffer", location_buffer, 0);
286
+ rb_define_method(RBS_Location, "_start_pos", location_start_pos, 0);
287
+ rb_define_method(RBS_Location, "_end_pos", location_end_pos, 0);
288
+ rb_define_method(RBS_Location, "_add_required_child", location_add_required_child, 3);
289
+ rb_define_method(RBS_Location, "_add_optional_child", location_add_optional_child, 3);
290
+ rb_define_method(RBS_Location, "_add_optional_no_child", location_add_optional_no_child, 1);
291
+ rb_define_method(RBS_Location, "_optional_keys", location_optional_keys, 0);
292
+ rb_define_method(RBS_Location, "_required_keys", location_required_keys, 0);
293
+ rb_define_method(RBS_Location, "[]", location_aref, 1);
294
+ }
@@ -0,0 +1,82 @@
1
+ #ifndef RBS_LOCATION_H
2
+ #define RBS_LOCATION_H
3
+
4
+ #include "compat.h"
5
+
6
+ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_BEGIN
7
+ #include "ruby.h"
8
+ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
9
+
10
+ #include "rbs.h"
11
+
12
+ /**
13
+ * Data structures for implementing RBS::Location class in Ruby.
14
+ *
15
+ * These structs support hierarchical locations, allowing sub-locations (children)
16
+ * to be stored under a main location. Each sub-location is identified by its
17
+ * name (Ruby Symbol ID).
18
+ */
19
+
20
+ /**
21
+ * RBS::Location class
22
+ * */
23
+ extern VALUE RBS_Location;
24
+
25
+ /**
26
+ * Range of character index for `rbs_loc` locations.
27
+ */
28
+ typedef struct {
29
+ int start;
30
+ int end;
31
+ } rbs_loc_range;
32
+
33
+ typedef struct {
34
+ ID name; /* Ruby ID for the name of the entry */
35
+ rbs_loc_range rg;
36
+ } rbs_loc_entry;
37
+
38
+ typedef unsigned int rbs_loc_entry_bitmap;
39
+
40
+ // The flexible array always allocates, but it's okay.
41
+ // This struct is not allocated when the `rbs_loc` doesn't have children.
42
+ typedef struct {
43
+ unsigned short len;
44
+ unsigned short cap;
45
+ rbs_loc_entry_bitmap required_p;
46
+ rbs_loc_entry entries[1];
47
+ } rbs_loc_children;
48
+
49
+ typedef struct {
50
+ VALUE buffer;
51
+ rbs_loc_range rg;
52
+ rbs_loc_children *children; // NULL when no children is allocated
53
+ } rbs_loc;
54
+
55
+ /**
56
+ * Returns new RBS::Location object, with given buffer and range.
57
+ * */
58
+ VALUE rbs_new_location(VALUE buffer, rbs_range_t rg);
59
+
60
+ VALUE rbs_new_location2(VALUE buffer, int start_char, int end_char);
61
+
62
+ /**
63
+ * Return rbs_loc associated with the RBS::Location object.
64
+ * */
65
+ rbs_loc *rbs_check_location(VALUE location);
66
+
67
+ /**
68
+ * Allocate memory for child locations.
69
+ *
70
+ * Do not call twice for the same location.
71
+ * */
72
+ void rbs_loc_legacy_alloc_children(rbs_loc *loc, unsigned short cap);
73
+
74
+ void rbs_loc_legacy_add_optional_child(rbs_loc *loc, ID name, rbs_loc_range r);
75
+ void rbs_loc_legacy_add_required_child(rbs_loc *loc, ID name, rbs_loc_range r);
76
+
77
+ /**
78
+ * Define RBS::Location class.
79
+ * */
80
+ void rbs__init_location();
81
+
82
+ #endif