Skip to content

Commit

Permalink
Omit unnecessary field from location range
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Apr 15, 2024
1 parent 858fc21 commit f752339
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 33 deletions.
61 changes: 30 additions & 31 deletions ext/rbs_extension/location.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

#define RBS_LOC_REQUIRED_P(loc, i) ((loc)->children->required_p & (1 << (i)))
#define RBS_LOC_OPTIONAL_P(loc, i) (!RBS_LOC_REQUIRED_P((loc), (i)))
#define NULL_LOC_RANGE_P(rg) ((rg).start == -1)

rbs_loc_range RBS_LOC_NULL_RANGE = { -1, -1 };
VALUE RBS_Location;

position rbs_loc_position(int char_pos) {
Expand All @@ -15,6 +17,11 @@ position rbs_loc_position3(int char_pos, int line, int column) {
return pos;
}

rbs_loc_range rbs_new_loc_range(range rg) {
rbs_loc_range r = { rg.start.char_pos, rg.end.char_pos };
return r;
}

static void check_children_max(unsigned short n) {
size_t max = sizeof(rbs_loc_entry_bitmap) * 8;
if (n > max) {
Expand Down Expand Up @@ -50,7 +57,7 @@ void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {

unsigned short i = loc->children->len++;
loc->children->entries[i].name = name;
loc->children->entries[i].rg = r;
loc->children->entries[i].rg = rbs_new_loc_range(r);

loc->children->required_p |= 1 << i;
}
Expand All @@ -60,10 +67,10 @@ void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {

unsigned short i = loc->children->len++;
loc->children->entries[i].name = name;
loc->children->entries[i].rg = r;
loc->children->entries[i].rg = rbs_new_loc_range(r);
}

void rbs_loc_init(rbs_loc *loc, VALUE buffer, range rg) {
void rbs_loc_init(rbs_loc *loc, VALUE buffer, rbs_loc_range rg) {
loc->buffer = buffer;
loc->rg = rg;
loc->children = NULL;
Expand Down Expand Up @@ -99,7 +106,7 @@ static VALUE location_s_allocate(VALUE klass) {
rbs_loc *loc;
VALUE obj = TypedData_Make_Struct(klass, rbs_loc, &location_type, loc);

rbs_loc_init(loc, Qnil, NULL_RANGE);
rbs_loc_init(loc, Qnil, RBS_LOC_NULL_RANGE);

return obj;
}
Expand All @@ -111,8 +118,8 @@ rbs_loc *rbs_check_location(VALUE obj) {
static VALUE location_initialize(VALUE self, VALUE buffer, VALUE start_pos, VALUE end_pos) {
rbs_loc *loc = rbs_check_location(self);

position start = rbs_loc_position(FIX2INT(start_pos));
position end = rbs_loc_position(FIX2INT(end_pos));
int start = FIX2INT(start_pos);
int end = FIX2INT(end_pos);

loc->buffer = buffer;
loc->rg.start = start;
Expand Down Expand Up @@ -142,38 +149,21 @@ static VALUE location_buffer(VALUE self) {

static VALUE location_start_pos(VALUE self) {
rbs_loc *loc = rbs_check_location(self);
return INT2FIX(loc->rg.start.char_pos);
return INT2FIX(loc->rg.start);
}

static VALUE location_end_pos(VALUE self) {
rbs_loc *loc = rbs_check_location(self);
return INT2FIX(loc->rg.end.char_pos);
return INT2FIX(loc->rg.end);
}

// TODO: remove it
static VALUE location_start_loc(VALUE self) {
rbs_loc *loc = rbs_check_location(self);

if (loc->rg.start.line >= 0) {
VALUE pair = rb_ary_new_capa(2);
rb_ary_push(pair, INT2FIX(loc->rg.start.line));
rb_ary_push(pair, INT2FIX(loc->rg.start.column));
return pair;
} else {
return Qnil;
}
return Qnil;
}

static VALUE location_end_loc(VALUE self) {
rbs_loc *loc = rbs_check_location(self);

if (loc->rg.end.line >= 0) {
VALUE pair = rb_ary_new_capa(2);
rb_ary_push(pair, INT2FIX(loc->rg.end.line));
rb_ary_push(pair, INT2FIX(loc->rg.end.column));
return pair;
} else {
return Qnil;
}
return Qnil;
}

static VALUE location_add_required_child(VALUE self, VALUE name, VALUE start, VALUE end) {
Expand Down Expand Up @@ -212,6 +202,15 @@ VALUE rbs_new_location(VALUE buffer, range rg) {
rbs_loc *loc;
VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);

rbs_loc_init(loc, buffer, rbs_new_loc_range(rg));

return obj;
}

static VALUE rbs_new_location_from_loc_range(VALUE buffer, rbs_loc_range rg) {
rbs_loc *loc;
VALUE obj = TypedData_Make_Struct(RBS_Location, rbs_loc, &location_type, loc);

rbs_loc_init(loc, buffer, rg);

return obj;
Expand All @@ -225,12 +224,12 @@ static VALUE location_aref(VALUE self, VALUE name) {
if (loc->children != NULL) {
for (unsigned short i = 0; i < loc->children->len; i++) {
if (loc->children->entries[i].name == id) {
range result = loc->children->entries[i].rg;
rbs_loc_range result = loc->children->entries[i].rg;

if (RBS_LOC_OPTIONAL_P(loc, i) && null_range_p(result)) {
if (RBS_LOC_OPTIONAL_P(loc, i) && NULL_LOC_RANGE_P(result)) {
return Qnil;
} else {
return rbs_new_location(loc->buffer, result);
return rbs_new_location_from_loc_range(loc->buffer, result);
}
}
}
Expand Down
9 changes: 7 additions & 2 deletions ext/rbs_extension/location.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,14 @@
* */
extern VALUE RBS_Location;

typedef struct {
int start;
int end;
} rbs_loc_range;

typedef struct {
ID name;
range rg;
rbs_loc_range rg;
} rbs_loc_entry;

typedef unsigned int rbs_loc_entry_bitmap;
Expand All @@ -25,7 +30,7 @@ typedef struct {

typedef struct {
VALUE buffer;
range rg;
rbs_loc_range rg;
rbs_loc_children *children;
} rbs_loc;

Expand Down

0 comments on commit f752339

Please sign in to comment.