rbs 4.0.0.dev.2 → 4.0.0.dev.3
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.
- checksums.yaml +4 -4
- data/.clang-format +74 -0
- data/.clangd +2 -0
- data/.github/workflows/c-check.yml +51 -0
- data/.github/workflows/dependabot.yml +1 -1
- data/.github/workflows/ruby.yml +0 -17
- data/.github/workflows/typecheck.yml +0 -2
- data/.gitignore +4 -0
- data/.rubocop.yml +1 -1
- data/.vscode/extensions.json +5 -0
- data/.vscode/settings.json +19 -0
- data/README.md +37 -0
- data/Rakefile +82 -0
- data/config.yml +1 -1
- data/core/enumerable.rbs +9 -0
- data/core/io.rbs +4 -4
- data/core/thread.rbs +0 -7
- data/ext/rbs_extension/ast_translation.c +1008 -1074
- data/ext/rbs_extension/class_constants.c +78 -74
- data/ext/rbs_extension/compat.h +3 -3
- data/ext/rbs_extension/extconf.rb +11 -1
- data/ext/rbs_extension/legacy_location.c +173 -172
- data/ext/rbs_extension/legacy_location.h +3 -3
- data/ext/rbs_extension/main.c +315 -273
- data/include/rbs/ast.h +11 -12
- data/include/rbs/defines.h +11 -12
- data/include/rbs/lexer.h +105 -105
- data/include/rbs/location.h +14 -14
- data/include/rbs/parser.h +21 -19
- data/include/rbs/string.h +3 -3
- data/include/rbs/util/rbs_allocator.h +14 -14
- data/include/rbs/util/rbs_constant_pool.h +3 -3
- data/include/rbs/util/rbs_encoding.h +1 -1
- data/lib/rbs/environment.rb +4 -0
- data/lib/rbs/namespace.rb +0 -7
- data/lib/rbs/parser_aux.rb +5 -0
- data/lib/rbs/type_name.rb +0 -7
- data/lib/rbs/types.rb +3 -1
- data/lib/rbs/unit_test/convertibles.rb +1 -0
- data/lib/rbs/version.rb +1 -1
- data/sig/environment.rbs +3 -0
- data/sig/namespace.rbs +0 -5
- data/sig/parser.rbs +20 -0
- data/sig/typename.rbs +0 -5
- data/sig/types.rbs +4 -1
- data/src/ast.c +216 -214
- data/src/lexer.c +2923 -2675
- data/src/lexstate.c +155 -155
- data/src/location.c +40 -40
- data/src/parser.c +2591 -2586
- data/src/string.c +2 -2
- data/src/util/rbs_allocator.c +7 -9
- data/src/util/rbs_assert.c +9 -9
- data/src/util/rbs_constant_pool.c +5 -7
- data/src/util/rbs_encoding.c +20095 -4056
- data/src/util/rbs_unescape.c +33 -32
- data/stdlib/json/0/json.rbs +9 -43
- data/stdlib/ripper/0/ripper.rbs +3 -0
- data/stdlib/socket/0/addrinfo.rbs +2 -2
- metadata +7 -2
@@ -10,307 +10,308 @@ rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 };
|
|
10
10
|
VALUE RBS_Location;
|
11
11
|
|
12
12
|
rbs_position_t rbs_loc_position(int char_pos) {
|
13
|
-
|
13
|
+
return (rbs_position_t) { 0, char_pos, -1, -1 };
|
14
14
|
}
|
15
15
|
|
16
16
|
rbs_position_t rbs_loc_position3(int char_pos, int line, int column) {
|
17
|
-
|
17
|
+
return (rbs_position_t) { 0, char_pos, line, column };
|
18
18
|
}
|
19
19
|
|
20
20
|
static rbs_loc_range rbs_new_loc_range(rbs_range_t rg) {
|
21
|
-
|
22
|
-
|
21
|
+
rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos };
|
22
|
+
return r;
|
23
23
|
}
|
24
24
|
|
25
25
|
static void check_children_max(unsigned short n) {
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
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
30
|
}
|
31
31
|
|
32
32
|
void rbs_loc_legacy_alloc_children(rbs_loc *loc, unsigned short cap) {
|
33
|
-
|
33
|
+
check_children_max(cap);
|
34
34
|
|
35
|
-
|
36
|
-
|
35
|
+
size_t s = RBS_LOC_CHILDREN_SIZE(cap);
|
36
|
+
loc->children = malloc(s);
|
37
37
|
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
38
|
+
*loc->children = (rbs_loc_children) {
|
39
|
+
.len = 0,
|
40
|
+
.required_p = 0,
|
41
|
+
.cap = cap,
|
42
|
+
.entries = { { 0 } },
|
43
|
+
};
|
44
44
|
}
|
45
45
|
|
46
46
|
static void check_children_cap(rbs_loc *loc) {
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
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
|
+
}
|
54
55
|
}
|
55
|
-
}
|
56
56
|
}
|
57
57
|
|
58
58
|
void rbs_loc_legacy_add_optional_child(rbs_loc *loc, rbs_constant_id_t name, rbs_range_t r) {
|
59
|
-
|
59
|
+
check_children_cap(loc);
|
60
60
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
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
66
|
}
|
67
67
|
|
68
68
|
void rbs_loc_legacy_add_required_child(rbs_loc *loc, rbs_constant_id_t name, rbs_range_t r) {
|
69
|
-
|
69
|
+
rbs_loc_legacy_add_optional_child(loc, name, r);
|
70
70
|
|
71
|
-
|
72
|
-
|
71
|
+
unsigned short last_index = loc->children->len - 1;
|
72
|
+
loc->children->required_p |= 1 << last_index;
|
73
73
|
}
|
74
74
|
|
75
75
|
void rbs_loc_init(rbs_loc *loc, VALUE buffer, rbs_loc_range rg) {
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
76
|
+
*loc = (rbs_loc) {
|
77
|
+
.buffer = buffer,
|
78
|
+
.rg = rg,
|
79
|
+
.children = NULL,
|
80
|
+
};
|
81
81
|
}
|
82
82
|
|
83
83
|
void rbs_loc_free(rbs_loc *loc) {
|
84
|
-
|
85
|
-
|
84
|
+
free(loc->children);
|
85
|
+
ruby_xfree(loc);
|
86
86
|
}
|
87
87
|
|
88
|
-
static void rbs_loc_mark(void *ptr)
|
89
|
-
|
90
|
-
|
91
|
-
rb_gc_mark(loc->buffer);
|
88
|
+
static void rbs_loc_mark(void *ptr) {
|
89
|
+
rbs_loc *loc = ptr;
|
90
|
+
rb_gc_mark(loc->buffer);
|
92
91
|
}
|
93
92
|
|
94
93
|
static size_t rbs_loc_memsize(const void *ptr) {
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
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
|
+
}
|
101
100
|
}
|
102
101
|
|
103
102
|
static rb_data_type_t location_type = {
|
104
|
-
|
105
|
-
|
106
|
-
|
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
|
107
108
|
};
|
108
109
|
|
109
110
|
static VALUE location_s_allocate(VALUE klass) {
|
110
|
-
|
111
|
-
|
111
|
+
rbs_loc *loc;
|
112
|
+
VALUE obj = TypedData_Make_Struct(klass, rbs_loc, &location_type, loc);
|
112
113
|
|
113
|
-
|
114
|
+
rbs_loc_init(loc, Qnil, RBS_LOC_NULL_RANGE);
|
114
115
|
|
115
|
-
|
116
|
+
return obj;
|
116
117
|
}
|
117
118
|
|
118
119
|
rbs_loc *rbs_check_location(VALUE obj) {
|
119
|
-
|
120
|
+
return rb_check_typeddata(obj, &location_type);
|
120
121
|
}
|
121
122
|
|
122
123
|
static VALUE location_initialize(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
|
123
|
-
|
124
|
+
rbs_loc *loc = rbs_check_location(self);
|
124
125
|
|
125
|
-
|
126
|
-
|
126
|
+
int start = FIX2INT(start_pos);
|
127
|
+
int end = FIX2INT(end_pos);
|
127
128
|
|
128
|
-
|
129
|
-
|
130
|
-
|
131
|
-
|
132
|
-
|
129
|
+
*loc = (rbs_loc) {
|
130
|
+
.buffer = buffer,
|
131
|
+
.rg = (rbs_loc_range) { start, end },
|
132
|
+
.children = NULL,
|
133
|
+
};
|
133
134
|
|
134
|
-
|
135
|
+
return Qnil;
|
135
136
|
}
|
136
137
|
|
137
138
|
static VALUE location_initialize_copy(VALUE self, VALUE other) {
|
138
|
-
|
139
|
-
|
140
|
-
|
141
|
-
|
142
|
-
|
143
|
-
|
144
|
-
|
145
|
-
|
146
|
-
|
147
|
-
|
148
|
-
|
149
|
-
|
150
|
-
|
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
|
+
}
|
151
152
|
|
152
|
-
|
153
|
+
return Qnil;
|
153
154
|
}
|
154
155
|
|
155
156
|
static VALUE location_buffer(VALUE self) {
|
156
|
-
|
157
|
-
|
157
|
+
rbs_loc *loc = rbs_check_location(self);
|
158
|
+
return loc->buffer;
|
158
159
|
}
|
159
160
|
|
160
161
|
static VALUE location_start_pos(VALUE self) {
|
161
|
-
|
162
|
-
|
162
|
+
rbs_loc *loc = rbs_check_location(self);
|
163
|
+
return INT2FIX(loc->rg.start);
|
163
164
|
}
|
164
165
|
|
165
166
|
static VALUE location_end_pos(VALUE self) {
|
166
|
-
|
167
|
-
|
167
|
+
rbs_loc *loc = rbs_check_location(self);
|
168
|
+
return INT2FIX(loc->rg.end);
|
168
169
|
}
|
169
170
|
|
170
171
|
static rbs_constant_id_t rbs_constant_pool_insert_ruby_symbol(VALUE symbol) {
|
171
|
-
|
172
|
+
VALUE name = rb_sym2str(symbol);
|
172
173
|
|
173
|
-
|
174
|
-
|
175
|
-
|
176
|
-
|
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));
|
177
178
|
}
|
178
179
|
|
179
180
|
static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
|
180
|
-
|
181
|
+
rbs_loc *loc = rbs_check_location(self);
|
181
182
|
|
182
|
-
|
183
|
-
|
184
|
-
|
183
|
+
rbs_range_t rg;
|
184
|
+
rg.start = rbs_loc_position(FIX2INT(start));
|
185
|
+
rg.end = rbs_loc_position(FIX2INT(end));
|
185
186
|
|
186
|
-
|
187
|
+
rbs_loc_legacy_add_required_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg);
|
187
188
|
|
188
|
-
|
189
|
+
return Qnil;
|
189
190
|
}
|
190
191
|
|
191
192
|
static VALUE location_add_optional_child(VALUE self, VALUE name, VALUE start, VALUE end) {
|
192
|
-
|
193
|
+
rbs_loc *loc = rbs_check_location(self);
|
193
194
|
|
194
|
-
|
195
|
-
|
196
|
-
|
195
|
+
rbs_range_t rg;
|
196
|
+
rg.start = rbs_loc_position(FIX2INT(start));
|
197
|
+
rg.end = rbs_loc_position(FIX2INT(end));
|
197
198
|
|
198
|
-
|
199
|
+
rbs_loc_legacy_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), rg);
|
199
200
|
|
200
|
-
|
201
|
+
return Qnil;
|
201
202
|
}
|
202
203
|
|
203
204
|
static VALUE location_add_optional_no_child(VALUE self, VALUE name) {
|
204
|
-
|
205
|
+
rbs_loc *loc = rbs_check_location(self);
|
205
206
|
|
206
|
-
|
207
|
+
rbs_loc_legacy_add_optional_child(loc, rbs_constant_pool_insert_ruby_symbol(name), NULL_RANGE);
|
207
208
|
|
208
|
-
|
209
|
+
return Qnil;
|
209
210
|
}
|
210
211
|
|
211
212
|
VALUE rbs_new_location(VALUE buffer, rbs_range_t rg) {
|
212
|
-
|
213
|
-
|
213
|
+
rbs_loc *loc;
|
214
|
+
VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
|
214
215
|
|
215
|
-
|
216
|
+
rbs_loc_init(loc, buffer, rbs_new_loc_range(rg));
|
216
217
|
|
217
|
-
|
218
|
+
return obj;
|
218
219
|
}
|
219
220
|
|
220
221
|
static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
|
221
|
-
|
222
|
-
|
222
|
+
rbs_loc *loc;
|
223
|
+
VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);
|
223
224
|
|
224
|
-
|
225
|
+
rbs_loc_init(loc, buffer, rg);
|
225
226
|
|
226
|
-
|
227
|
+
return obj;
|
227
228
|
}
|
228
229
|
|
229
230
|
static rbs_constant_id_t rbs_constant_pool_find_ruby_symbol(VALUE symbol) {
|
230
|
-
|
231
|
+
VALUE name = rb_sym2str(symbol);
|
231
232
|
|
232
|
-
|
233
|
+
return rbs_constant_pool_find(RBS_GLOBAL_CONSTANT_POOL, (const uint8_t *) RSTRING_PTR(name), RSTRING_LEN(name));
|
233
234
|
}
|
234
235
|
|
235
236
|
static VALUE location_aref(VALUE self, VALUE name) {
|
236
|
-
|
237
|
+
rbs_loc *loc = rbs_check_location(self);
|
237
238
|
|
238
|
-
|
239
|
+
rbs_constant_id_t id = rbs_constant_pool_find_ruby_symbol(name);
|
239
240
|
|
240
|
-
|
241
|
-
|
242
|
-
|
243
|
-
|
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;
|
244
245
|
|
245
|
-
|
246
|
-
|
247
|
-
|
248
|
-
|
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
|
+
}
|
249
252
|
}
|
250
|
-
}
|
251
253
|
}
|
252
|
-
}
|
253
254
|
|
254
|
-
|
255
|
-
|
255
|
+
VALUE string = rb_funcall(name, rb_intern("to_s"), 0);
|
256
|
+
rb_raise(rb_eRuntimeError, "Unknown child name given: %s", RSTRING_PTR(string));
|
256
257
|
}
|
257
258
|
|
258
259
|
static VALUE rbs_constant_to_ruby_symbol(rbs_constant_t *constant) {
|
259
|
-
|
260
|
+
return ID2SYM(rb_intern2((const char *) constant->start, constant->length));
|
260
261
|
}
|
261
262
|
|
262
263
|
static VALUE location_optional_keys(VALUE self) {
|
263
|
-
|
264
|
+
VALUE keys = rb_ary_new();
|
264
265
|
|
265
|
-
|
266
|
-
|
267
|
-
|
268
|
-
|
269
|
-
|
266
|
+
rbs_loc *loc = rbs_check_location(self);
|
267
|
+
rbs_loc_children *children = loc->children;
|
268
|
+
if (children == NULL) {
|
269
|
+
return keys;
|
270
|
+
}
|
270
271
|
|
271
|
-
|
272
|
-
|
273
|
-
|
274
|
-
|
275
|
-
|
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
|
+
}
|
276
278
|
}
|
277
|
-
}
|
278
279
|
|
279
|
-
|
280
|
+
return keys;
|
280
281
|
}
|
281
282
|
|
282
283
|
static VALUE location_required_keys(VALUE self) {
|
283
|
-
|
284
|
+
VALUE keys = rb_ary_new();
|
284
285
|
|
285
|
-
|
286
|
-
|
287
|
-
|
288
|
-
|
289
|
-
|
286
|
+
rbs_loc *loc = rbs_check_location(self);
|
287
|
+
rbs_loc_children *children = loc->children;
|
288
|
+
if (children == NULL) {
|
289
|
+
return keys;
|
290
|
+
}
|
290
291
|
|
291
|
-
|
292
|
-
|
293
|
-
|
294
|
-
|
295
|
-
|
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
|
+
}
|
296
298
|
}
|
297
|
-
}
|
298
299
|
|
299
|
-
|
300
|
+
return keys;
|
300
301
|
}
|
301
302
|
|
302
303
|
void rbs__init_location(void) {
|
303
|
-
|
304
|
-
|
305
|
-
|
306
|
-
|
307
|
-
|
308
|
-
|
309
|
-
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
315
|
-
|
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);
|
316
317
|
}
|
@@ -15,9 +15,9 @@ SUPPRESS_RUBY_HEADER_DIAGNOSTICS_END
|
|
15
15
|
extern VALUE RBS_Location;
|
16
16
|
|
17
17
|
typedef struct {
|
18
|
-
|
19
|
-
|
20
|
-
|
18
|
+
VALUE buffer;
|
19
|
+
rbs_loc_range rg;
|
20
|
+
rbs_loc_children *children; // NULL when no children is allocated
|
21
21
|
} rbs_loc;
|
22
22
|
|
23
23
|
/**
|