Skip to content

Commit b18a54d

Browse files
authored
Merge pull request #2131 from Shopify/designated_initializers
Designated initializers
2 parents 305389b + 7bda0bf commit b18a54d

File tree

3 files changed

+103
-54
lines changed

3 files changed

+103
-54
lines changed

ext/rbs_extension/location.c

Lines changed: 29 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,12 @@ void rbs_loc_alloc_children(rbs_loc *loc, unsigned short cap) {
3636
size_t s = RBS_LOC_CHILDREN_SIZE(cap);
3737
loc->children = malloc(s);
3838

39-
loc->children->len = 0;
40-
loc->children->required_p = 0;
41-
loc->children->cap = cap;
39+
*loc->children = (rbs_loc_children) {
40+
.len = 0,
41+
.required_p = 0,
42+
.cap = cap,
43+
.entries = { 0 },
44+
};
4245
}
4346

4447
static void check_children_cap(rbs_loc *loc) {
@@ -54,27 +57,28 @@ static void check_children_cap(rbs_loc *loc) {
5457
}
5558

5659
void rbs_loc_add_required_child(rbs_loc *loc, ID name, range r) {
57-
check_children_cap(loc);
58-
59-
unsigned short i = loc->children->len++;
60-
loc->children->entries[i].name = name;
61-
loc->children->entries[i].rg = rbs_new_loc_range(r);
60+
rbs_loc_add_optional_child(loc, name, r);
6261

63-
loc->children->required_p |= 1 << i;
62+
unsigned short last_index = loc->children->len - 1;
63+
loc->children->required_p |= 1 << last_index;
6464
}
6565

6666
void rbs_loc_add_optional_child(rbs_loc *loc, ID name, range r) {
6767
check_children_cap(loc);
6868

6969
unsigned short i = loc->children->len++;
70-
loc->children->entries[i].name = name;
71-
loc->children->entries[i].rg = rbs_new_loc_range(r);
70+
loc->children->entries[i] = (rbs_loc_entry) {
71+
.name = name,
72+
.rg = rbs_new_loc_range(r),
73+
};
7274
}
7375

7476
void rbs_loc_init(rbs_loc *loc, VALUE buffer, rbs_loc_range rg) {
75-
loc->buffer = buffer;
76-
loc->rg = rg;
77-
loc->children = NULL;
77+
*loc = (rbs_loc) {
78+
.buffer = buffer,
79+
.rg = rg,
80+
.children = NULL,
81+
};
7882
}
7983

8084
void rbs_loc_free(rbs_loc *loc) {
@@ -122,9 +126,11 @@ static VALUE location_initialize(VALUE self, VALUE buffer, VALUE start_pos, VALU
122126
int start = FIX2INT(start_pos);
123127
int end = FIX2INT(end_pos);
124128

125-
loc->buffer = buffer;
126-
loc->rg.start = start;
127-
loc->rg.end = end;
129+
*loc = (rbs_loc) {
130+
.buffer = buffer,
131+
.rg = (rbs_loc_range) { start, end },
132+
.children = NULL,
133+
};
128134

129135
return Qnil;
130136
}
@@ -133,8 +139,12 @@ static VALUE location_initialize_copy(VALUE self, VALUE other) {
133139
rbs_loc *self_loc = rbs_check_location(self);
134140
rbs_loc *other_loc = rbs_check_location(other);
135141

136-
self_loc->buffer = other_loc->buffer;
137-
self_loc->rg = other_loc->rg;
142+
*self_loc = (rbs_loc) {
143+
.buffer = other_loc->buffer,
144+
.rg = other_loc->rg,
145+
.children = NULL,
146+
};
147+
138148
if (other_loc->children != NULL) {
139149
rbs_loc_alloc_children(self_loc, other_loc->children->cap);
140150
memcpy(self_loc->children, other_loc->children, RBS_LOC_CHILDREN_SIZE(other_loc->children->cap));

ext/rbs_extension/parser.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -604,13 +604,15 @@ static VALUE parse_optional(parserstate *state) {
604604
}
605605

606606
static void initialize_method_params(method_params *params){
607-
params->required_positionals = EMPTY_ARRAY;
608-
params->optional_positionals = EMPTY_ARRAY;
609-
params->rest_positionals = Qnil;
610-
params->trailing_positionals = EMPTY_ARRAY;
611-
params->required_keywords = rb_hash_new();
612-
params->optional_keywords = rb_hash_new();
613-
params->rest_keywords = Qnil;
607+
*params = (method_params) {
608+
.required_positionals = EMPTY_ARRAY,
609+
.optional_positionals = EMPTY_ARRAY,
610+
.rest_positionals = Qnil,
611+
.trailing_positionals = EMPTY_ARRAY,
612+
.required_keywords = rb_hash_new(),
613+
.optional_keywords = rb_hash_new(),
614+
.rest_keywords = Qnil,
615+
};
614616
}
615617

616618
/*
@@ -1566,8 +1568,6 @@ static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, b
15661568
parser_advance(state);
15671569
parser_advance(state);
15681570
kind = SINGLETON_KIND;
1569-
rg->start = self_range.start;
1570-
rg->end = state->current_token.range.end;
15711571
} else if (
15721572
state->next_token2.type == pQUESTION
15731573
&& state->next_token.range.end.char_pos == state->next_token2.range.start.char_pos
@@ -1577,9 +1577,12 @@ static InstanceSingletonKind parse_instance_singleton_kind(parserstate *state, b
15771577
parser_advance(state);
15781578
parser_advance(state);
15791579
kind = INSTANCE_SINGLETON_KIND;
1580-
rg->start = self_range.start;
1581-
rg->end = state->current_token.range.end;
15821580
}
1581+
1582+
*rg = (range) {
1583+
.start = self_range.start,
1584+
.end = state->current_token.range.end,
1585+
};
15831586
} else {
15841587
*rg = NULL_RANGE;
15851588
}
@@ -2692,8 +2695,10 @@ static VALUE parse_namespace(parserstate *state, range *rg) {
26922695
bool is_absolute = false;
26932696

26942697
if (state->next_token.type == pCOLON2) {
2695-
rg->start = state->next_token.range.start;
2696-
rg->end = state->next_token.range.end;
2698+
*rg = (range) {
2699+
.start = state->next_token.range.start,
2700+
.end = state->next_token.range.end,
2701+
};
26972702
is_absolute = true;
26982703

26992704
parser_advance(state);

ext/rbs_extension/parserstate.c

Lines changed: 56 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,26 @@
44

55
id_table *alloc_empty_table(void) {
66
id_table *table = malloc(sizeof(id_table));
7-
table->size = 10;
8-
table->count = 0;
9-
table->ids = calloc(10, sizeof(ID));
7+
8+
*table = (id_table) {
9+
.size = 10,
10+
.count = 0,
11+
.ids = calloc(10, sizeof(ID)),
12+
.next = NULL,
13+
};
1014

1115
return table;
1216
}
1317

1418
id_table *alloc_reset_table(void) {
1519
id_table *table = malloc(sizeof(id_table));
16-
table->size = 0;
20+
21+
*table = (id_table) {
22+
.size = 0,
23+
.count = 0,
24+
.ids = NULL,
25+
.next = NULL,
26+
};
1727

1828
return table;
1929
}
@@ -182,15 +192,18 @@ VALUE get_comment(parserstate *state, int subject_line) {
182192
}
183193

184194
comment *alloc_comment(token comment_token, comment *last_comment) {
185-
comment *new_comment = calloc(1, sizeof(comment));
195+
comment *new_comment = malloc(sizeof(comment));
186196

187-
new_comment->next_comment = last_comment;
197+
*new_comment = (comment) {
198+
.start = comment_token.range.start,
199+
.end = comment_token.range.end,
188200

189-
new_comment->start = comment_token.range.start;
190-
new_comment->end = comment_token.range.end;
201+
.line_size = 0,
202+
.line_count = 0,
203+
.tokens = NULL,
191204

192-
new_comment->line_size = 0;
193-
new_comment->line_count = 0;
205+
.next_comment = last_comment,
206+
};
194207

195208
comment_insert_new_line(new_comment, comment_token);
196209

@@ -279,11 +292,25 @@ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos) {
279292
rb_raise(rb_eArgError, "negative position range: %d...%d", start_pos, end_pos);
280293
}
281294

282-
lexstate *lexer = calloc(1, sizeof(lexstate));
283-
lexer->string = string;
284-
lexer->current.line = 1;
285-
lexer->start_pos = start_pos;
286-
lexer->end_pos = end_pos;
295+
lexstate *lexer = malloc(sizeof(lexstate));
296+
297+
position start_position = (position) {
298+
.byte_pos = 0,
299+
.char_pos = 0,
300+
.line = 1,
301+
.column = 0,
302+
};
303+
304+
*lexer = (lexstate) {
305+
.string = string,
306+
.start_pos = start_pos,
307+
.end_pos = end_pos,
308+
.current = start_position,
309+
.start = { 0 },
310+
.first_token_of_line = false,
311+
.last_char = 0,
312+
};
313+
287314
skipn(lexer, start_pos);
288315
lexer->start = lexer->current;
289316
lexer->first_token_of_line = lexer->current.column == 0;
@@ -292,13 +319,20 @@ lexstate *alloc_lexer(VALUE string, int start_pos, int end_pos) {
292319
}
293320

294321
parserstate *alloc_parser(VALUE buffer, lexstate *lexer, int start_pos, int end_pos, VALUE variables) {
295-
parserstate *parser = calloc(1, sizeof(parserstate));
296-
parser->lexstate = lexer;
297-
parser->buffer = buffer;
298-
parser->current_token = NullToken;
299-
parser->next_token = NullToken;
300-
parser->next_token2 = NullToken;
301-
parser->next_token3 = NullToken;
322+
parserstate *parser = malloc(sizeof(parserstate));
323+
324+
*parser = (parserstate) {
325+
.lexstate = lexer,
326+
327+
.current_token = NullToken,
328+
.next_token = NullToken,
329+
.next_token2 = NullToken,
330+
.next_token3 = NullToken,
331+
.buffer = buffer,
332+
333+
.vars = NULL,
334+
.last_comment = NULL,
335+
};
302336

303337
parser_advance(parser);
304338
parser_advance(parser);

0 commit comments

Comments
 (0)