rbs 3.9.5 → 3.10.0.pre.1

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 (171) hide show
  1. checksums.yaml +4 -4
  2. data/.clang-format +74 -0
  3. data/.clangd +2 -0
  4. data/.github/workflows/c-check.yml +54 -0
  5. data/.github/workflows/comments.yml +3 -3
  6. data/.github/workflows/ruby.yml +34 -19
  7. data/.github/workflows/typecheck.yml +1 -1
  8. data/.github/workflows/windows.yml +1 -1
  9. data/.gitignore +4 -0
  10. data/README.md +38 -1
  11. data/Rakefile +152 -23
  12. data/config.yml +190 -62
  13. data/core/array.rbs +44 -43
  14. data/core/dir.rbs +2 -2
  15. data/core/encoding.rbs +3 -2
  16. data/core/enumerable.rbs +89 -2
  17. data/core/errno.rbs +8 -0
  18. data/core/errors.rbs +28 -1
  19. data/core/exception.rbs +2 -2
  20. data/core/fiber.rbs +3 -3
  21. data/core/file.rbs +26 -11
  22. data/core/float.rbs +1 -1
  23. data/core/gc.rbs +422 -281
  24. data/core/hash.rbs +1024 -727
  25. data/core/io/wait.rbs +11 -33
  26. data/core/io.rbs +6 -4
  27. data/core/kernel.rbs +49 -43
  28. data/core/marshal.rbs +1 -1
  29. data/core/match_data.rbs +1 -1
  30. data/core/math.rbs +42 -3
  31. data/core/method.rbs +14 -6
  32. data/core/module.rbs +71 -11
  33. data/core/nil_class.rbs +3 -3
  34. data/core/numeric.rbs +8 -8
  35. data/core/object.rbs +3 -3
  36. data/core/object_space.rbs +13 -0
  37. data/{stdlib/pathname/0 → core}/pathname.rbs +253 -352
  38. data/core/proc.rbs +15 -8
  39. data/core/process.rbs +2 -2
  40. data/core/ractor.rbs +278 -437
  41. data/core/range.rbs +6 -7
  42. data/core/rbs/unnamed/argf.rbs +1 -1
  43. data/core/rbs/unnamed/env_class.rbs +1 -1
  44. data/core/rbs/unnamed/random.rbs +4 -2
  45. data/core/regexp.rbs +22 -17
  46. data/core/ruby_vm.rbs +6 -4
  47. data/core/rubygems/errors.rbs +3 -70
  48. data/core/rubygems/rubygems.rbs +11 -79
  49. data/core/set.rbs +439 -332
  50. data/core/string.rbs +2897 -1117
  51. data/core/struct.rbs +1 -1
  52. data/core/symbol.rbs +4 -4
  53. data/core/thread.rbs +83 -20
  54. data/core/time.rbs +35 -9
  55. data/core/unbound_method.rbs +14 -6
  56. data/docs/aliases.md +79 -0
  57. data/docs/collection.md +2 -2
  58. data/docs/gem.md +0 -1
  59. data/docs/sigs.md +3 -3
  60. data/ext/rbs_extension/ast_translation.c +1016 -0
  61. data/ext/rbs_extension/ast_translation.h +37 -0
  62. data/ext/rbs_extension/class_constants.c +157 -0
  63. data/{include/rbs/constants.h → ext/rbs_extension/class_constants.h} +7 -1
  64. data/ext/rbs_extension/compat.h +10 -0
  65. data/ext/rbs_extension/extconf.rb +25 -1
  66. data/ext/rbs_extension/legacy_location.c +317 -0
  67. data/ext/rbs_extension/legacy_location.h +45 -0
  68. data/ext/rbs_extension/main.c +365 -14
  69. data/ext/rbs_extension/rbs_extension.h +6 -21
  70. data/ext/rbs_extension/rbs_string_bridging.c +9 -0
  71. data/ext/rbs_extension/rbs_string_bridging.h +24 -0
  72. data/include/rbs/ast.h +687 -0
  73. data/include/rbs/defines.h +86 -0
  74. data/include/rbs/lexer.h +199 -0
  75. data/include/rbs/location.h +59 -0
  76. data/include/rbs/parser.h +135 -0
  77. data/include/rbs/string.h +49 -0
  78. data/include/rbs/util/rbs_allocator.h +59 -0
  79. data/include/rbs/util/rbs_assert.h +20 -0
  80. data/include/rbs/util/rbs_buffer.h +83 -0
  81. data/include/rbs/util/rbs_constant_pool.h +6 -67
  82. data/include/rbs/util/rbs_encoding.h +282 -0
  83. data/include/rbs/util/rbs_unescape.h +23 -0
  84. data/include/rbs.h +1 -2
  85. data/lib/rbs/annotate/formatter.rb +3 -13
  86. data/lib/rbs/annotate/rdoc_annotator.rb +3 -1
  87. data/lib/rbs/annotate/rdoc_source.rb +1 -1
  88. data/lib/rbs/cli/validate.rb +2 -2
  89. data/lib/rbs/cli.rb +1 -1
  90. data/lib/rbs/collection/config/lockfile_generator.rb +1 -0
  91. data/lib/rbs/definition_builder/ancestor_builder.rb +5 -5
  92. data/lib/rbs/environment.rb +64 -59
  93. data/lib/rbs/environment_loader.rb +1 -1
  94. data/lib/rbs/errors.rb +1 -1
  95. data/lib/rbs/parser_aux.rb +5 -0
  96. data/lib/rbs/resolver/constant_resolver.rb +2 -2
  97. data/lib/rbs/resolver/type_name_resolver.rb +124 -38
  98. data/lib/rbs/test/type_check.rb +13 -0
  99. data/lib/rbs/types.rb +3 -1
  100. data/lib/rbs/version.rb +1 -1
  101. data/lib/rbs.rb +1 -1
  102. data/lib/rdoc/discover.rb +1 -1
  103. data/lib/rdoc_plugin/parser.rb +3 -3
  104. data/sig/annotate/formatter.rbs +2 -2
  105. data/sig/annotate/rdoc_annotater.rbs +1 -1
  106. data/sig/environment.rbs +57 -6
  107. data/sig/manifest.yaml +0 -1
  108. data/sig/parser.rbs +20 -0
  109. data/sig/resolver/type_name_resolver.rbs +38 -7
  110. data/sig/types.rbs +4 -1
  111. data/src/ast.c +1256 -0
  112. data/src/lexer.c +2956 -0
  113. data/src/lexer.re +147 -0
  114. data/src/lexstate.c +205 -0
  115. data/src/location.c +71 -0
  116. data/src/parser.c +3495 -0
  117. data/src/string.c +90 -0
  118. data/src/util/rbs_allocator.c +152 -0
  119. data/src/util/rbs_assert.c +21 -0
  120. data/src/util/rbs_buffer.c +54 -0
  121. data/src/util/rbs_constant_pool.c +16 -86
  122. data/src/util/rbs_encoding.c +21308 -0
  123. data/src/util/rbs_unescape.c +131 -0
  124. data/stdlib/cgi/0/core.rbs +2 -396
  125. data/stdlib/cgi/0/manifest.yaml +1 -0
  126. data/stdlib/cgi-escape/0/escape.rbs +153 -0
  127. data/stdlib/coverage/0/coverage.rbs +3 -1
  128. data/stdlib/delegate/0/delegator.rbs +10 -7
  129. data/stdlib/erb/0/erb.rbs +737 -347
  130. data/stdlib/fileutils/0/fileutils.rbs +18 -13
  131. data/stdlib/forwardable/0/forwardable.rbs +3 -0
  132. data/stdlib/json/0/json.rbs +67 -48
  133. data/stdlib/net-http/0/net-http.rbs +3 -0
  134. data/stdlib/objspace/0/objspace.rbs +8 -3
  135. data/stdlib/open-uri/0/open-uri.rbs +40 -0
  136. data/stdlib/openssl/0/openssl.rbs +182 -149
  137. data/stdlib/optparse/0/optparse.rbs +3 -3
  138. data/stdlib/rdoc/0/code_object.rbs +2 -2
  139. data/stdlib/rdoc/0/comment.rbs +2 -0
  140. data/stdlib/rdoc/0/options.rbs +76 -0
  141. data/stdlib/rdoc/0/rdoc.rbs +7 -5
  142. data/stdlib/rdoc/0/store.rbs +1 -1
  143. data/stdlib/resolv/0/resolv.rbs +25 -68
  144. data/stdlib/ripper/0/ripper.rbs +5 -2
  145. data/stdlib/singleton/0/singleton.rbs +3 -0
  146. data/stdlib/socket/0/socket.rbs +13 -1
  147. data/stdlib/socket/0/tcp_socket.rbs +10 -2
  148. data/stdlib/stringio/0/stringio.rbs +412 -80
  149. data/stdlib/strscan/0/string_scanner.rbs +31 -31
  150. data/stdlib/tempfile/0/tempfile.rbs +1 -1
  151. data/stdlib/tsort/0/cyclic.rbs +3 -0
  152. data/stdlib/uri/0/common.rbs +11 -2
  153. data/stdlib/uri/0/file.rbs +1 -1
  154. data/stdlib/uri/0/generic.rbs +16 -15
  155. data/stdlib/uri/0/rfc2396_parser.rbs +6 -7
  156. data/stdlib/zlib/0/zstream.rbs +1 -0
  157. metadata +41 -18
  158. data/ext/rbs_extension/lexer.c +0 -2728
  159. data/ext/rbs_extension/lexer.h +0 -179
  160. data/ext/rbs_extension/lexer.re +0 -147
  161. data/ext/rbs_extension/lexstate.c +0 -175
  162. data/ext/rbs_extension/location.c +0 -325
  163. data/ext/rbs_extension/location.h +0 -85
  164. data/ext/rbs_extension/parser.c +0 -2982
  165. data/ext/rbs_extension/parser.h +0 -18
  166. data/ext/rbs_extension/parserstate.c +0 -411
  167. data/ext/rbs_extension/parserstate.h +0 -163
  168. data/ext/rbs_extension/unescape.c +0 -32
  169. data/include/rbs/ruby_objs.h +0 -72
  170. data/src/constants.c +0 -153
  171. 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,157 @@
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_Parser;
11
+
12
+ VALUE RBS;
13
+ VALUE RBS_AST;
14
+ VALUE RBS_AST_Declarations;
15
+ VALUE RBS_AST_Directives;
16
+ VALUE RBS_AST_Members;
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_TypeParam;
51
+ VALUE RBS_MethodType;
52
+ VALUE RBS_Namespace;
53
+ VALUE RBS_TypeName;
54
+ VALUE RBS_Types_Alias;
55
+ VALUE RBS_Types_Bases_Any;
56
+ VALUE RBS_Types_Bases_Bool;
57
+ VALUE RBS_Types_Bases_Bottom;
58
+ VALUE RBS_Types_Bases_Class;
59
+ VALUE RBS_Types_Bases_Instance;
60
+ VALUE RBS_Types_Bases_Nil;
61
+ VALUE RBS_Types_Bases_Self;
62
+ VALUE RBS_Types_Bases_Top;
63
+ VALUE RBS_Types_Bases_Void;
64
+ VALUE RBS_Types_Block;
65
+ VALUE RBS_Types_ClassInstance;
66
+ VALUE RBS_Types_ClassSingleton;
67
+ VALUE RBS_Types_Function;
68
+ VALUE RBS_Types_Function_Param;
69
+ VALUE RBS_Types_Interface;
70
+ VALUE RBS_Types_Intersection;
71
+ VALUE RBS_Types_Literal;
72
+ VALUE RBS_Types_Optional;
73
+ VALUE RBS_Types_Proc;
74
+ VALUE RBS_Types_Record;
75
+ VALUE RBS_Types_Tuple;
76
+ VALUE RBS_Types_Union;
77
+ VALUE RBS_Types_UntypedFunction;
78
+ VALUE RBS_Types_Variable;
79
+
80
+ VALUE RBS_ParsingError;
81
+
82
+ #define IMPORT_CONSTANT(var, parent, name) \
83
+ { \
84
+ var = rb_const_get(parent, rb_intern(name)); \
85
+ rb_gc_register_mark_object(var); \
86
+ }
87
+
88
+ void rbs__init_constants(void) {
89
+ IMPORT_CONSTANT(RBS, rb_cObject, "RBS");
90
+ IMPORT_CONSTANT(RBS_ParsingError, RBS, "ParsingError");
91
+
92
+ IMPORT_CONSTANT(RBS_AST, RBS, "AST");
93
+ IMPORT_CONSTANT(RBS_AST_Declarations, RBS_AST, "Declarations");
94
+ IMPORT_CONSTANT(RBS_AST_Directives, RBS_AST, "Directives");
95
+ IMPORT_CONSTANT(RBS_AST_Members, RBS_AST, "Members");
96
+ IMPORT_CONSTANT(RBS_Types, RBS, "Types");
97
+ IMPORT_CONSTANT(RBS_Types_Bases, RBS_Types, "Bases");
98
+
99
+ IMPORT_CONSTANT(RBS_AST_Annotation, RBS_AST, "Annotation");
100
+ IMPORT_CONSTANT(RBS_AST_Comment, RBS_AST, "Comment");
101
+ IMPORT_CONSTANT(RBS_AST_Declarations_Class, RBS_AST_Declarations, "Class");
102
+ IMPORT_CONSTANT(RBS_AST_Declarations_Class_Super, RBS_AST_Declarations_Class, "Super");
103
+ IMPORT_CONSTANT(RBS_AST_Declarations_ClassAlias, RBS_AST_Declarations, "ClassAlias");
104
+ IMPORT_CONSTANT(RBS_AST_Declarations_Constant, RBS_AST_Declarations, "Constant");
105
+ IMPORT_CONSTANT(RBS_AST_Declarations_Global, RBS_AST_Declarations, "Global");
106
+ IMPORT_CONSTANT(RBS_AST_Declarations_Interface, RBS_AST_Declarations, "Interface");
107
+ IMPORT_CONSTANT(RBS_AST_Declarations_Module, RBS_AST_Declarations, "Module");
108
+ IMPORT_CONSTANT(RBS_AST_Declarations_Module_Self, RBS_AST_Declarations_Module, "Self");
109
+ IMPORT_CONSTANT(RBS_AST_Declarations_ModuleAlias, RBS_AST_Declarations, "ModuleAlias");
110
+ IMPORT_CONSTANT(RBS_AST_Declarations_TypeAlias, RBS_AST_Declarations, "TypeAlias");
111
+ IMPORT_CONSTANT(RBS_AST_Directives_Use, RBS_AST_Directives, "Use");
112
+ IMPORT_CONSTANT(RBS_AST_Directives_Use_SingleClause, RBS_AST_Directives_Use, "SingleClause");
113
+ IMPORT_CONSTANT(RBS_AST_Directives_Use_WildcardClause, RBS_AST_Directives_Use, "WildcardClause");
114
+ IMPORT_CONSTANT(RBS_AST_Members_Alias, RBS_AST_Members, "Alias");
115
+ IMPORT_CONSTANT(RBS_AST_Members_AttrAccessor, RBS_AST_Members, "AttrAccessor");
116
+ IMPORT_CONSTANT(RBS_AST_Members_AttrReader, RBS_AST_Members, "AttrReader");
117
+ IMPORT_CONSTANT(RBS_AST_Members_AttrWriter, RBS_AST_Members, "AttrWriter");
118
+ IMPORT_CONSTANT(RBS_AST_Members_ClassInstanceVariable, RBS_AST_Members, "ClassInstanceVariable");
119
+ IMPORT_CONSTANT(RBS_AST_Members_ClassVariable, RBS_AST_Members, "ClassVariable");
120
+ IMPORT_CONSTANT(RBS_AST_Members_Extend, RBS_AST_Members, "Extend");
121
+ IMPORT_CONSTANT(RBS_AST_Members_Include, RBS_AST_Members, "Include");
122
+ IMPORT_CONSTANT(RBS_AST_Members_InstanceVariable, RBS_AST_Members, "InstanceVariable");
123
+ IMPORT_CONSTANT(RBS_AST_Members_MethodDefinition, RBS_AST_Members, "MethodDefinition");
124
+ IMPORT_CONSTANT(RBS_AST_Members_MethodDefinition_Overload, RBS_AST_Members_MethodDefinition, "Overload");
125
+ IMPORT_CONSTANT(RBS_AST_Members_Prepend, RBS_AST_Members, "Prepend");
126
+ IMPORT_CONSTANT(RBS_AST_Members_Private, RBS_AST_Members, "Private");
127
+ IMPORT_CONSTANT(RBS_AST_Members_Public, RBS_AST_Members, "Public");
128
+ IMPORT_CONSTANT(RBS_AST_TypeParam, RBS_AST, "TypeParam");
129
+ IMPORT_CONSTANT(RBS_MethodType, RBS, "MethodType");
130
+ IMPORT_CONSTANT(RBS_Namespace, RBS, "Namespace");
131
+ IMPORT_CONSTANT(RBS_TypeName, RBS, "TypeName");
132
+ IMPORT_CONSTANT(RBS_Types_Alias, RBS_Types, "Alias");
133
+ IMPORT_CONSTANT(RBS_Types_Bases_Any, RBS_Types_Bases, "Any");
134
+ IMPORT_CONSTANT(RBS_Types_Bases_Bool, RBS_Types_Bases, "Bool");
135
+ IMPORT_CONSTANT(RBS_Types_Bases_Bottom, RBS_Types_Bases, "Bottom");
136
+ IMPORT_CONSTANT(RBS_Types_Bases_Class, RBS_Types_Bases, "Class");
137
+ IMPORT_CONSTANT(RBS_Types_Bases_Instance, RBS_Types_Bases, "Instance");
138
+ IMPORT_CONSTANT(RBS_Types_Bases_Nil, RBS_Types_Bases, "Nil");
139
+ IMPORT_CONSTANT(RBS_Types_Bases_Self, RBS_Types_Bases, "Self");
140
+ IMPORT_CONSTANT(RBS_Types_Bases_Top, RBS_Types_Bases, "Top");
141
+ IMPORT_CONSTANT(RBS_Types_Bases_Void, RBS_Types_Bases, "Void");
142
+ IMPORT_CONSTANT(RBS_Types_Block, RBS_Types, "Block");
143
+ IMPORT_CONSTANT(RBS_Types_ClassInstance, RBS_Types, "ClassInstance");
144
+ IMPORT_CONSTANT(RBS_Types_ClassSingleton, RBS_Types, "ClassSingleton");
145
+ IMPORT_CONSTANT(RBS_Types_Function, RBS_Types, "Function");
146
+ IMPORT_CONSTANT(RBS_Types_Function_Param, RBS_Types_Function, "Param");
147
+ IMPORT_CONSTANT(RBS_Types_Interface, RBS_Types, "Interface");
148
+ IMPORT_CONSTANT(RBS_Types_Intersection, RBS_Types, "Intersection");
149
+ IMPORT_CONSTANT(RBS_Types_Literal, RBS_Types, "Literal");
150
+ IMPORT_CONSTANT(RBS_Types_Optional, RBS_Types, "Optional");
151
+ IMPORT_CONSTANT(RBS_Types_Proc, RBS_Types, "Proc");
152
+ IMPORT_CONSTANT(RBS_Types_Record, RBS_Types, "Record");
153
+ IMPORT_CONSTANT(RBS_Types_Tuple, RBS_Types, "Tuple");
154
+ IMPORT_CONSTANT(RBS_Types_Union, RBS_Types, "Union");
155
+ IMPORT_CONSTANT(RBS_Types_UntypedFunction, RBS_Types, "UntypedFunction");
156
+ IMPORT_CONSTANT(RBS_Types_Variable, RBS_Types, "Variable");
157
+ }
@@ -2,12 +2,18 @@
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;
@@ -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,317 @@
1
+ #include "legacy_location.h"
2
+ #include "rbs_extension.h"
3
+
4
+ #define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
5
+ #define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
6
+ #define RBS_LOC_CHILDREN_SIZE(cap) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
7
+ #define NULL_LOC_RANGE_P(rg) ((rg).start == -1)
8
+
9
+ rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 };
10
+ VALUE RBS_Location;
11
+
12
+ rbs_position_t rbs_loc_position(int char_pos) {
13
+ return (rbs_position_t) { 0, char_pos, -1, -1 };
14
+ }
15
+
16
+ rbs_position_t rbs_loc_position3(int char_pos, int line, int column) {
17
+ return (rbs_position_t) { 0, char_pos, line, column };
18
+ }
19
+
20
+ static rbs_loc_range rbs_new_loc_range(rbs_range_t rg) {
21
+ rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos };
22
+ return r;
23
+ }
24
+
25
+ static void check_children_max(unsigned short n) {
26
+ size_t max = sizeof(rbs_loc_entry_bitmap) * 8;
27
+ if (n > max) {
28
+ rb_raise(rb_eRuntimeError, "Too many children added to location: %d", n);
29
+ }
30
+ }
31
+
32
+ void rbs_loc_legacy_alloc_children(rbs_loc *loc, unsigned short cap) {
33
+ check_children_max(cap);
34
+
35
+ size_t s = RBS_LOC_CHILDREN_SIZE(cap);
36
+ loc->children = malloc(s);
37
+
38
+ *loc->children = (rbs_loc_children) {
39
+ .len = 0,
40
+ .required_p = 0,
41
+ .cap = cap,
42
+ .entries = { { 0 } },
43
+ };
44
+ }
45
+
46
+ static void check_children_cap(rbs_loc *loc) {
47
+ if (loc->children == NULL) {
48
+ rbs_loc_legacy_alloc_children(loc, 1);
49
+ } else {
50
+ if (loc->children->len == loc->children->cap) {
51
+ check_children_max(loc->children->cap + 1);
52
+ size_t s = RBS_LOC_CHILDREN_SIZE(++loc->children->cap);
53
+ loc->children = realloc(loc->children, s);
54
+ }
55
+ }
56
+ }
57
+
58
+ void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, rbs_range_t r) {
59
+ check_children_cap(loc);
60
+
61
+ unsigned short i = loc->children->len++;
62
+ loc->children->entries[i] = (rbs_loc_entry) {
63
+ .name = name,
64
+ .rg = rbs_new_loc_range(r),
65
+ };
66
+ }
67
+
68
+ void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, rbs_range_t r) {
69
+ rbs_loc_legacy_add_optional_child(loc, name, r);
70
+
71
+ unsigned short last_index = loc->children->len - 1;
72
+ loc->children->required_p |= 1 << last_index;
73
+ }
74
+
75
+ void rbs_loc_init(rbs_loc *loc, VALUE buffer, rbs_loc_range rg) {
76
+ *loc = (rbs_loc) {
77
+ .buffer = buffer,
78
+ .rg = rg,
79
+ .children = NULL,
80
+ };
81
+ }
82
+
83
+ void rbs_loc_free(rbs_loc *loc) {
84
+ free(loc->children);
85
+ ruby_xfree(loc);
86
+ }
87
+
88
+ static void rbs_loc_mark(void *ptr) {
89
+ rbs_loc *loc = ptr;
90
+ rb_gc_mark(loc->buffer);
91
+ }
92
+
93
+ static size_t rbs_loc_memsize(const void *ptr) {
94
+ const rbs_loc *loc = ptr;
95
+ if (loc->children == NULL) {
96
+ return sizeof(rbs_loc);
97
+ } else {
98
+ return sizeof(rbs_loc) + RBS_LOC_CHILDREN_SIZE(loc->children->cap);
99
+ }
100
+ }
101
+
102
+ static rb_data_type_t location_type = {
103
+ "RBS::Location",
104
+ { rbs_loc_mark, (RUBY_DATA_FUNC) rbs_loc_free, rbs_loc_memsize },
105
+ 0,
106
+ 0,
107
+ RUBY_TYPED_FREE_IMMEDIATELY
108
+ };
109
+
110
+ static VALUE location_s_allocate(VALUE klass) {
111
+ rbs_loc *loc;
112
+ VALUE obj = TypedData_Make_Struct(klass, rbs_loc, &location_type, loc);
113
+
114
+ rbs_loc_init(loc, Qnil, RBS_LOC_NULL_RANGE);
115
+
116
+ return obj;
117
+ }
118
+
119
+ rbs_loc *rbs_check_location(VALUE obj) {
120
+ return rb_check_typeddata(obj, &location_type);
121
+ }
122
+
123
+ static VALUE location_initialize(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
124
+ rbs_loc *loc = rbs_check_location(self);
125
+
126
+ int start = FIX2INT(start_pos);
127
+ int end = FIX2INT(end_pos);
128
+
129
+ *loc = (rbs_loc) {
130
+ .buffer = buffer,
131
+ .rg = (rbs_loc_range) { start, end },
132
+ .children = NULL,
133
+ };
134
+
135
+ return Qnil;
136
+ }
137
+
138
+ static VALUE location_initialize_copy(VALUE self, VALUE other) {
139
+ rbs_loc *self_loc = rbs_check_location(self);
140
+ rbs_loc *other_loc = rbs_check_location(other);
141
+
142
+ *self_loc = (rbs_loc) {
143
+ .buffer = other_loc->buffer,
144
+ .rg = other_loc->rg,
145
+ .children = NULL,
146
+ };
147
+
148
+ if (other_loc->children != NULL) {
149
+ rbs_loc_legacy_alloc_children(self_loc, other_loc->children->cap);
150
+ memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));
151
+ }
152
+
153
+ return Qnil;
154
+ }
155
+
156
+ static VALUE location_buffer(VALUE self) {
157
+ rbs_loc *loc = rbs_check_location(self);
158
+ return loc->buffer;
159
+ }
160
+
161
+ static VALUE location_start_pos(VALUE self) {
162
+ rbs_loc *loc = rbs_check_location(self);
163
+ return INT2FIX(loc->rg.start);
164
+ }
165
+
166
+ static VALUE location_end_pos(VALUE self) {
167
+ rbs_loc *loc = rbs_check_location(self);
168
+ return INT2FIX(loc->rg.end);
169
+ }
170
+
171
+ static rbs_constant_id_t rbs_constant_pool_insert_ruby_symbol(VALUE symbol) {
172
+ VALUE name = rb_sym2str(symbol);
173
+
174
+ // Constants inserted here will never be freed, but that's acceptable because:
175
+ // 1. Most symbols passed into here will be the ones already inserted into the constant pool by `parser.c`.
176
+ // 2. Methods like `add_required_child` and `add_optional_child` will usually only get called with a few different symbols.
177
+ return rbs_constant_pool_insert_constant(RBS_GLOBAL_CONSTANT_POOL, (const uint8_t *) RSTRING_PTR(name), RSTRING_LEN(name));
178
+ }
179
+
180
+ static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
181
+ rbs_loc *loc = rbs_check_location(self);
182
+
183
+ rbs_range_t rg;
184
+ rg.start = rbs_loc_position(FIX2INT(start));
185
+ rg.end = rbs_loc_position(FIX2INT(end));
186
+
187
+ rbs_loc_legacy_add_required_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg);
188
+
189
+ return Qnil;
190
+ }
191
+
192
+ static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VALUE end) {
193
+ rbs_loc *loc = rbs_check_location(self);
194
+
195
+ rbs_range_t rg;
196
+ rg.start = rbs_loc_position(FIX2INT(start));
197
+ rg.end = rbs_loc_position(FIX2INT(end));
198
+
199
+ rbs_loc_legacy_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg);
200
+
201
+ return Qnil;
202
+ }
203
+
204
+ static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
205
+ rbs_loc *loc = rbs_check_location(self);
206
+
207
+ rbs_loc_legacy_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), NULL_RANGE);
208
+
209
+ return Qnil;
210
+ }
211
+
212
+ VALUE rbs_new_location(VALUE buffer, rbs_range_t rg) {
213
+ rbs_loc *loc;
214
+ VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
215
+
216
+ rbs_loc_init(loc, buffer, rbs_new_loc_range(rg));
217
+
218
+ return obj;
219
+ }
220
+
221
+ static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
222
+ rbs_loc *loc;
223
+ VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
224
+
225
+ rbs_loc_init(loc, buffer, rg);
226
+
227
+ return obj;
228
+ }
229
+
230
+ static rbs_constant_id_t rbs_constant_pool_find_ruby_symbol(VALUE symbol) {
231
+ VALUE name = rb_sym2str(symbol);
232
+
233
+ return rbs_constant_pool_find(RBS_GLOBAL_CONSTANT_POOL, (const uint8_t *) RSTRING_PTR(name), RSTRING_LEN(name));
234
+ }
235
+
236
+ static VALUE location_aref(VALUE self, VALUE name) {
237
+ rbs_loc *loc = rbs_check_location(self);
238
+
239
+ rbs_constant_id_t id = rbs_constant_pool_find_ruby_symbol(name);
240
+
241
+ if (loc->children != NULL && id != RBS_CONSTANT_ID_UNSET) {
242
+ for (unsigned short i = 0; i < loc->children->len; i++) {
243
+ if (loc->children->entries[i].name == id) {
244
+ rbs_loc_range result = loc->children->entries[i].rg;
245
+
246
+ if (RBS_LOC_OPTIONAL_P(loc, i) && NULL_LOC_RANGE_P(result)) {
247
+ return Qnil;
248
+ } else {
249
+ return rbs_new_location_from_loc_range(loc->buffer, result);
250
+ }
251
+ }
252
+ }
253
+ }
254
+
255
+ VALUE string = rb_funcall(name, rb_intern("to_s"), 0);
256
+ rb_raise(rb_eRuntimeError, "Unknown child name given: %s", RSTRING_PTR(string));
257
+ }
258
+
259
+ static VALUE rbs_constant_to_ruby_symbol(rbs_constant_t *constant) {
260
+ return ID2SYM(rb_intern2((const char *) constant->start, constant->length));
261
+ }
262
+
263
+ static VALUE location_optional_keys(VALUE self) {
264
+ VALUE keys = rb_ary_new();
265
+
266
+ rbs_loc *loc = rbs_check_location(self);
267
+ rbs_loc_children *children = loc->children;
268
+ if (children == NULL) {
269
+ return keys;
270
+ }
271
+
272
+ for (unsigned short i = 0; i < children->len; i++) {
273
+ if (RBS_LOC_OPTIONAL_P(loc, i)) {
274
+ rbs_constant_t *key_id = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
275
+ VALUE key_sym = rbs_constant_to_ruby_symbol(key_id);
276
+ rb_ary_push(keys, key_sym);
277
+ }
278
+ }
279
+
280
+ return keys;
281
+ }
282
+
283
+ static VALUE location_required_keys(VALUE self) {
284
+ VALUE keys = rb_ary_new();
285
+
286
+ rbs_loc *loc = rbs_check_location(self);
287
+ rbs_loc_children *children = loc->children;
288
+ if (children == NULL) {
289
+ return keys;
290
+ }
291
+
292
+ for (unsigned short i = 0; i < children->len; i++) {
293
+ if (RBS_LOC_REQUIRED_P(loc, i)) {
294
+ rbs_constant_t *key_id = rbs_constant_pool_id_to_constant(RBS_GLOBAL_CONSTANT_POOL, children->entries[i].name);
295
+ VALUE key_sym = rbs_constant_to_ruby_symbol(key_id);
296
+ rb_ary_push(keys, key_sym);
297
+ }
298
+ }
299
+
300
+ return keys;
301
+ }
302
+
303
+ void rbs__init_location(void) {
304
+ RBS_Location = rb_define_class_under(RBS, "Location", rb_cObject);
305
+ rb_define_alloc_func(RBS_Location, location_s_allocate);
306
+ rb_define_private_method(RBS_Location, "initialize", location_initialize, 3);
307
+ rb_define_private_method(RBS_Location, "initialize_copy", location_initialize_copy, 1);
308
+ rb_define_method(RBS_Location, "buffer", location_buffer, 0);
309
+ rb_define_method(RBS_Location, "start_pos", location_start_pos, 0);
310
+ rb_define_method(RBS_Location, "end_pos", location_end_pos, 0);
311
+ rb_define_method(RBS_Location, "_add_required_child", location_add_required_child, 3);
312
+ rb_define_method(RBS_Location, "_add_optional_child", location_add_optional_child, 3);
313
+ rb_define_method(RBS_Location, "_add_optional_no_child", location_add_optional_no_child, 1);
314
+ rb_define_method(RBS_Location, "_optional_keys", location_optional_keys, 0);
315
+ rb_define_method(RBS_Location, "_required_keys", location_required_keys, 0);
316
+ rb_define_method(RBS_Location, "[]", location_aref, 1);
317
+ }
@@ -0,0 +1,45 @@
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
+ * RBS::Location class
14
+ * */
15
+ extern VALUE RBS_Location;
16
+
17
+ typedef struct {
18
+ VALUE buffer;
19
+ rbs_loc_range rg;
20
+ rbs_loc_children *children; // NULL when no children is allocated
21
+ } rbs_loc;
22
+
23
+ /**
24
+ * Returns new RBS::Location object, with given buffer and range.
25
+ * */
26
+ VALUE rbs_new_location(VALUE buffer, rbs_range_t rg);
27
+
28
+ /**
29
+ * Return rbs_loc associated with the RBS::Location object.
30
+ * */
31
+ rbs_loc *rbs_check_location(VALUE location);
32
+
33
+ /**
34
+ * Allocate memory for child locations.
35
+ *
36
+ * Do not call twice for the same location.
37
+ * */
38
+ void rbs_loc_legacy_alloc_children(rbs_loc *loc, unsigned short cap);
39
+
40
+ /**
41
+ * Define RBS::Location class.
42
+ * */
43
+ void rbs__init_location();
44
+
45
+ #endif