33#define RBS_LOC_REQUIRED_P (loc , i ) ((loc)->children->required_p & (1 << (i)))
44#define RBS_LOC_OPTIONAL_P (loc , i ) (!RBS_LOC_REQUIRED_P((loc), (i)))
55#define RBS_LOC_CHILDREN_SIZE (cap ) (sizeof(rbs_loc_children) + sizeof(rbs_loc_entry) * ((cap) - 1))
6+ #define NULL_LOC_RANGE_P (rg ) ((rg).start == -1)
67
8+ rbs_loc_range RBS_LOC_NULL_RANGE = { -1 , -1 };
79VALUE RBS_Location ;
810
911position rbs_loc_position (int char_pos ) {
@@ -16,6 +18,11 @@ position rbs_loc_position3(int char_pos, int line, int column) {
1618 return pos ;
1719}
1820
21+ rbs_loc_range rbs_new_loc_range (range rg ) {
22+ rbs_loc_range r = { rg .start .char_pos , rg .end .char_pos };
23+ return r ;
24+ }
25+
1926static void check_children_max (unsigned short n ) {
2027 size_t max = sizeof (rbs_loc_entry_bitmap ) * 8 ;
2128 if (n > max ) {
@@ -51,7 +58,7 @@ void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
5158
5259 unsigned short i = loc -> children -> len ++ ;
5360 loc -> children -> entries [i ].name = name ;
54- loc -> children -> entries [i ].rg = r ;
61+ loc -> children -> entries [i ].rg = rbs_new_loc_range ( r ) ;
5562
5663 loc -> children -> required_p |= 1 << i ;
5764}
@@ -61,10 +68,10 @@ void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {
6168
6269 unsigned short i = loc -> children -> len ++ ;
6370 loc -> children -> entries [i ].name = name ;
64- loc -> children -> entries [i ].rg = r ;
71+ loc -> children -> entries [i ].rg = rbs_new_loc_range ( r ) ;
6572}
6673
67- void rbs_loc_init (rbs_loc * loc , VALUE buffer , range rg ) {
74+ void rbs_loc_init (rbs_loc * loc , VALUE buffer , rbs_loc_range rg ) {
6875 loc -> buffer = buffer ;
6976 loc -> rg = rg ;
7077 loc -> children = NULL ;
@@ -100,7 +107,7 @@ static VALUE location_s_allocate(VALUE klass) {
100107 rbs_loc * loc ;
101108 VALUE obj = TypedData_Make_Struct (klass , rbs_loc , & location_type , loc );
102109
103- rbs_loc_init (loc , Qnil , NULL_RANGE );
110+ rbs_loc_init (loc , Qnil , RBS_LOC_NULL_RANGE );
104111
105112 return obj ;
106113}
@@ -112,8 +119,8 @@ rbs_loc *rbs_check_location(VALUE obj) {
112119static VALUE location_initialize (VALUE self , VALUE buffer , VALUE start_pos , VALUE end_pos ) {
113120 rbs_loc * loc = rbs_check_location (self );
114121
115- position start = rbs_loc_position ( FIX2INT (start_pos ) );
116- position end = rbs_loc_position ( FIX2INT (end_pos ) );
122+ int start = FIX2INT (start_pos );
123+ int end = FIX2INT (end_pos );
117124
118125 loc -> buffer = buffer ;
119126 loc -> rg .start = start ;
@@ -143,38 +150,12 @@ static VALUE location_buffer(VALUE self) {
143150
144151static VALUE location_start_pos (VALUE self ) {
145152 rbs_loc * loc = rbs_check_location (self );
146- return INT2FIX (loc -> rg .start . char_pos );
153+ return INT2FIX (loc -> rg .start );
147154}
148155
149156static VALUE location_end_pos (VALUE self ) {
150157 rbs_loc * loc = rbs_check_location (self );
151- return INT2FIX (loc -> rg .end .char_pos );
152- }
153-
154- static VALUE location_start_loc (VALUE self ) {
155- rbs_loc * loc = rbs_check_location (self );
156-
157- if (loc -> rg .start .line >= 0 ) {
158- VALUE pair = rb_ary_new_capa (2 );
159- rb_ary_push (pair , INT2FIX (loc -> rg .start .line ));
160- rb_ary_push (pair , INT2FIX (loc -> rg .start .column ));
161- return pair ;
162- } else {
163- return Qnil ;
164- }
165- }
166-
167- static VALUE location_end_loc (VALUE self ) {
168- rbs_loc * loc = rbs_check_location (self );
169-
170- if (loc -> rg .end .line >= 0 ) {
171- VALUE pair = rb_ary_new_capa (2 );
172- rb_ary_push (pair , INT2FIX (loc -> rg .end .line ));
173- rb_ary_push (pair , INT2FIX (loc -> rg .end .column ));
174- return pair ;
175- } else {
176- return Qnil ;
177- }
158+ return INT2FIX (loc -> rg .end );
178159}
179160
180161static VALUE location_add_required_child (VALUE self , VALUE name , VALUE start , VALUE end ) {
@@ -213,6 +194,15 @@ VALUE rbs_new_location(VALUE buffer, range rg) {
213194 rbs_loc * loc ;
214195 VALUE obj = TypedData_Make_Struct (RBS_Location , rbs_loc , & location_type , loc );
215196
197+ rbs_loc_init (loc , buffer , rbs_new_loc_range (rg ));
198+
199+ return obj ;
200+ }
201+
202+ static VALUE rbs_new_location_from_loc_range (VALUE buffer , rbs_loc_range rg ) {
203+ rbs_loc * loc ;
204+ VALUE obj = TypedData_Make_Struct (RBS_Location , rbs_loc , & location_type , loc );
205+
216206 rbs_loc_init (loc , buffer , rg );
217207
218208 return obj ;
@@ -226,12 +216,12 @@ static VALUE location_aref(VALUE self, VALUE name) {
226216 if (loc -> children != NULL ) {
227217 for (unsigned short i = 0 ; i < loc -> children -> len ; i ++ ) {
228218 if (loc -> children -> entries [i ].name == id ) {
229- range result = loc -> children -> entries [i ].rg ;
219+ rbs_loc_range result = loc -> children -> entries [i ].rg ;
230220
231- if (RBS_LOC_OPTIONAL_P (loc , i ) && null_range_p (result )) {
221+ if (RBS_LOC_OPTIONAL_P (loc , i ) && NULL_LOC_RANGE_P (result )) {
232222 return Qnil ;
233223 } else {
234- return rbs_new_location (loc -> buffer , result );
224+ return rbs_new_location_from_loc_range (loc -> buffer , result );
235225 }
236226 }
237227 }
@@ -294,8 +284,6 @@ void rbs__init_location(void) {
294284 rb_define_method (RBS_Location , "buffer" , location_buffer , 0 );
295285 rb_define_method (RBS_Location , "start_pos" , location_start_pos , 0 );
296286 rb_define_method (RBS_Location , "end_pos" , location_end_pos , 0 );
297- rb_define_private_method (RBS_Location , "_start_loc" , location_start_loc , 0 );
298- rb_define_private_method (RBS_Location , "_end_loc" , location_end_loc , 0 );
299287 rb_define_method (RBS_Location , "_add_required_child" , location_add_required_child , 3 );
300288 rb_define_method (RBS_Location , "_add_optional_child" , location_add_optional_child , 3 );
301289 rb_define_method (RBS_Location , "_add_optional_no_child" , location_add_optional_no_child , 1 );
0 commit comments