Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a075c55

Browse files
committed
Manage rb_strterm_t without imemo
1 parent cb06b66 commit a075c55

File tree

7 files changed

+36
-74
lines changed

7 files changed

+36
-74
lines changed

gc.c

-1
Original file line numberDiff line numberDiff line change
@@ -7066,7 +7066,6 @@ gc_mark_imemo(rb_objspace_t *objspace, VALUE obj)
70667066
rb_ast_mark(&RANY(obj)->as.imemo.ast);
70677067
return;
70687068
case imemo_parser_strterm:
7069-
rb_strterm_mark(obj);
70707069
return;
70717070
case imemo_callinfo:
70727071
return;

internal/imemo.h

-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,6 @@ struct MEMO {
129129
typedef struct rb_imemo_tmpbuf_struct rb_imemo_tmpbuf_t;
130130
rb_imemo_tmpbuf_t *rb_imemo_tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
131131
struct vm_ifunc *rb_vm_ifunc_new(rb_block_call_func_t func, const void *data, int min_argc, int max_argc);
132-
void rb_strterm_mark(VALUE obj);
133132
static inline enum imemo_type imemo_type(VALUE imemo);
134133
static inline int imemo_type_p(VALUE imemo, enum imemo_type imemo_type);
135134
static inline bool imemo_throw_data_p(VALUE imemo);

internal/parse.h

-1
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,6 @@ typedef struct rb_strterm_heredoc_struct {
3636
uint8_t quote;
3737
uint8_t func;
3838
} rb_strterm_heredoc_t;
39-
STATIC_ASSERT(rb_strterm_heredoc_t, sizeof(rb_strterm_heredoc_t) <= 4 * SIZEOF_VALUE);
4039

4140
#define HERETERM_LENGTH_MAX UINT_MAX
4241

parse.y

+36-37
Original file line numberDiff line numberDiff line change
@@ -157,23 +157,6 @@ static NODE *reg_named_capture_assign(struct parser_params* p, VALUE regexp, con
157157
VALUE rb_io_gets_internal(VALUE io);
158158

159159
VALUE rb_node_case_when_optimizable_literal(const NODE *const node);
160-
161-
static int
162-
strterm_is_heredoc(VALUE strterm)
163-
{
164-
return ((rb_strterm_t *)strterm)->flags & STRTERM_HEREDOC;
165-
}
166-
167-
static VALUE
168-
new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0, int heredoc)
169-
{
170-
rb_strterm_t *imemo = (rb_strterm_t *)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
171-
if (heredoc) {
172-
imemo->flags |= STRTERM_HEREDOC;
173-
}
174-
175-
return (VALUE)imemo;
176-
}
177160
#endif /* !UNIVERSAL_PARSER */
178161

179162
static inline int
@@ -7625,6 +7608,30 @@ parser_str_new(struct parser_params *p, const char *ptr, long len, rb_encoding *
76257608
return str;
76267609
}
76277610

7611+
static int
7612+
strterm_is_heredoc(rb_strterm_t *strterm)
7613+
{
7614+
return strterm->flags & STRTERM_HEREDOC;
7615+
}
7616+
7617+
static rb_strterm_t *
7618+
new_strterm(struct parser_params *p, int func, int term, int paren)
7619+
{
7620+
rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7621+
strterm->u.literal.func = func;
7622+
strterm->u.literal.term = term;
7623+
strterm->u.literal.paren = paren;
7624+
return strterm;
7625+
}
7626+
7627+
static rb_strterm_t *
7628+
new_heredoc(struct parser_params *p)
7629+
{
7630+
rb_strterm_t *strterm = ZALLOC(rb_strterm_t);
7631+
strterm->flags |= STRTERM_HEREDOC;
7632+
return strterm;
7633+
}
7634+
76287635
#define peek(p,c) peek_n(p, (c), 0)
76297636
#define peek_n(p,c,n) (!lex_eol_n_p(p, n) && (c) == (unsigned char)(p)->lex.pcur[n])
76307637
#define peekc(p) peekc_n(p, 0)
@@ -7851,7 +7858,7 @@ tokadd_codepoint(struct parser_params *p, rb_encoding **encp,
78517858
int codepoint = (int)ruby_scan_hex(p->lex.pcur, wide ? p->lex.pend - p->lex.pcur : 4, &numlen);
78527859
p->lex.pcur += numlen;
78537860
if (p->lex.strterm == NULL ||
7854-
(strterm_is_heredoc((VALUE)p->lex.strterm)) ||
7861+
strterm_is_heredoc(p->lex.strterm) ||
78557862
(p->lex.strterm->u.literal.func != str_regexp)) {
78567863
if (wide ? (numlen == 0 || numlen > 6) : (numlen < 4)) {
78577864
literal_flush(p, p->lex.pcur);
@@ -8435,9 +8442,7 @@ tokadd_string(struct parser_params *p,
84358442
return c;
84368443
}
84378444

8438-
/* imemo_parser_strterm for literal */
8439-
#define NEW_STRTERM(func, term, paren) \
8440-
(rb_strterm_t *)new_strterm((VALUE)(func), (VALUE)(paren), (VALUE)(term), 0, 0)
8445+
#define NEW_STRTERM(func, term, paren) new_strterm(p, func, term, paren)
84418446

84428447
#ifdef RIPPER
84438448
static void
@@ -8548,6 +8553,7 @@ parser_peek_variable_name(struct parser_params *p)
85488553
static inline enum yytokentype
85498554
parser_string_term(struct parser_params *p, int func)
85508555
{
8556+
xfree(p->lex.strterm);
85518557
p->lex.strterm = 0;
85528558
if (func & STR_FUNC_REGEXP) {
85538559
set_yylval_num(regx_options(p));
@@ -8578,6 +8584,7 @@ parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
85788584
if (func & STR_FUNC_TERM) {
85798585
if (func & STR_FUNC_QWORDS) nextc(p); /* delayed term */
85808586
SET_LEX_STATE(EXPR_END);
8587+
xfree(p->lex.strterm);
85818588
p->lex.strterm = 0;
85828589
return func & STR_FUNC_REGEXP ? tREGEXP_END : tSTRING_END;
85838590
}
@@ -8624,6 +8631,7 @@ parse_string(struct parser_params *p, rb_strterm_literal_t *quote)
86248631
if (func & STR_FUNC_QWORDS) {
86258632
/* no content to add, bailing out here */
86268633
unterminated_literal("unterminated list meets end of file");
8634+
xfree(p->lex.strterm);
86278635
p->lex.strterm = 0;
86288636
return tSTRING_END;
86298637
}
@@ -8714,13 +8722,15 @@ heredoc_identifier(struct parser_params *p)
87148722
dispatch_scan_event(p, tHEREDOC_BEG);
87158723
lex_goto_eol(p);
87168724

8717-
p->lex.strterm = (rb_strterm_t *)new_strterm(0, 0, 0, p->lex.lastline, 1);
8725+
p->lex.strterm = new_heredoc(p);
87188726
rb_strterm_heredoc_t *here = &p->lex.strterm->u.heredoc;
87198727
here->offset = offset;
87208728
here->sourceline = p->ruby_sourceline;
8721-
here->length = (int)len;
8729+
here->length = (unsigned)len;
87228730
here->quote = quote;
87238731
here->func = func;
8732+
here->lastline = p->lex.lastline;
8733+
rb_ast_add_mark_object(p->ast, p->lex.lastline);
87248734

87258735
token_flush(p);
87268736
p->heredoc_indent = indent;
@@ -8732,6 +8742,7 @@ static void
87328742
heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
87338743
{
87348744
VALUE line;
8745+
rb_strterm_t *term = p->lex.strterm;
87358746

87368747
p->lex.strterm = 0;
87378748
line = here->lastline;
@@ -8744,6 +8755,7 @@ heredoc_restore(struct parser_params *p, rb_strterm_heredoc_t *here)
87448755
p->ruby_sourceline = (int)here->sourceline;
87458756
if (p->eofp) p->lex.nextline = Qnil;
87468757
p->eofp = 0;
8758+
xfree(term);
87478759
}
87488760

87498761
static int
@@ -9007,7 +9019,6 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
90079019
compile_error(p, "can't find string \"%.*s\" anywhere before EOF",
90089020
(int)len, eos);
90099021
token_flush(p);
9010-
p->lex.strterm = 0;
90119022
SET_LEX_STATE(EXPR_END);
90129023
return tSTRING_END;
90139024
}
@@ -9027,7 +9038,6 @@ here_document(struct parser_params *p, rb_strterm_heredoc_t *here)
90279038
restore:
90289039
heredoc_restore(p, &p->lex.strterm->u.heredoc);
90299040
token_flush(p);
9030-
p->lex.strterm = 0;
90319041
SET_LEX_STATE(EXPR_END);
90329042
return tSTRING_END;
90339043
}
@@ -10417,7 +10427,7 @@ parser_yylex(struct parser_params *p)
1041710427
int token_seen = p->token_seen;
1041810428

1041910429
if (p->lex.strterm) {
10420-
if (strterm_is_heredoc((VALUE)p->lex.strterm)) {
10430+
if (strterm_is_heredoc(p->lex.strterm)) {
1042110431
token_flush(p);
1042210432
return here_document(p, &p->lex.strterm->u.heredoc);
1042310433
}
@@ -15417,7 +15427,6 @@ rb_ruby_parser_mark(void *ptr)
1541715427
rb_gc_mark(p->lex.lastline);
1541815428
rb_gc_mark(p->lex.nextline);
1541915429
rb_gc_mark(p->ruby_sourcefile_string);
15420-
rb_gc_mark((VALUE)p->lex.strterm);
1542115430
rb_gc_mark((VALUE)p->ast);
1542215431
rb_gc_mark(p->case_labels);
1542315432
rb_gc_mark(p->delayed.token);
@@ -15710,16 +15719,6 @@ rb_parser_set_yydebug(VALUE self, VALUE flag)
1571015719
rb_ruby_parser_set_yydebug(p, RTEST(flag));
1571115720
return flag;
1571215721
}
15713-
15714-
void
15715-
rb_strterm_mark(VALUE obj)
15716-
{
15717-
rb_strterm_t *strterm = (rb_strterm_t*)obj;
15718-
if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
15719-
rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
15720-
rb_gc_mark(heredoc->lastline);
15721-
}
15722-
}
1572315722
#endif /* !UNIVERSAL_PARSER */
1572415723

1572515724
VALUE

ruby_parser.c

-29
Original file line numberDiff line numberDiff line change
@@ -473,39 +473,12 @@ zalloc(size_t elemsiz)
473473
return ruby_xcalloc(1, elemsiz);
474474
}
475475

476-
static VALUE
477-
new_strterm(VALUE v1, VALUE v2, VALUE v3, VALUE v0, int heredoc)
478-
{
479-
rb_strterm_t *imemo = (rb_strterm_t *)rb_imemo_new(imemo_parser_strterm, v1, v2, v3, v0);
480-
if (heredoc) {
481-
imemo->flags |= STRTERM_HEREDOC;
482-
}
483-
484-
return (VALUE)imemo;
485-
}
486-
487-
static int
488-
strterm_is_heredoc(VALUE strterm)
489-
{
490-
return ((rb_strterm_t *)strterm)->flags & STRTERM_HEREDOC;
491-
}
492-
493476
static void
494477
gc_guard(VALUE obj)
495478
{
496479
RB_GC_GUARD(obj);
497480
}
498481

499-
void
500-
rb_strterm_mark(VALUE obj)
501-
{
502-
rb_strterm_t *strterm = (rb_strterm_t*)obj;
503-
if (RBASIC(obj)->flags & STRTERM_HEREDOC) {
504-
rb_strterm_heredoc_t *heredoc = &strterm->u.heredoc;
505-
rb_gc_mark(heredoc->lastline);
506-
}
507-
}
508-
509482
static rb_imemo_tmpbuf_t *
510483
tmpbuf_parser_heap(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt)
511484
{
@@ -564,8 +537,6 @@ rb_parser_config_initialize(rb_parser_config_t *config)
564537
config->nonempty_memcpy = nonempty_memcpy;
565538
config->xmalloc_mul_add = rb_xmalloc_mul_add;
566539

567-
config->new_strterm = new_strterm;
568-
config->strterm_is_heredoc = strterm_is_heredoc;
569540
config->tmpbuf_parser_heap = tmpbuf_parser_heap;
570541
config->ast_new = ast_new;
571542

rubyparser.h

-3
Original file line numberDiff line numberDiff line change
@@ -1143,9 +1143,6 @@ typedef struct rb_parser_config_struct {
11431143
void *(*xmalloc_mul_add)(size_t x, size_t y, size_t z);
11441144

11451145
/* imemo */
1146-
// TODO: Should it return `rb_strterm_t *'?
1147-
VALUE (*new_strterm)(VALUE v1, VALUE v2, VALUE v3, VALUE v0, int heredoc);
1148-
int (*strterm_is_heredoc)(VALUE strterm);
11491146
rb_imemo_tmpbuf_t *(*tmpbuf_parser_heap)(void *buf, rb_imemo_tmpbuf_t *old_heap, size_t cnt);
11501147
rb_ast_t *(*ast_new)(VALUE nb);
11511148

universal_parser.c

-2
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,6 @@ struct rb_imemo_tmpbuf_struct {
111111
#undef MEMCPY
112112
#define MEMCPY(p1,p2,type,n) (p->config->nonempty_memcpy((p1), (p2), sizeof(type), (n)))
113113

114-
#define new_strterm p->config->new_strterm
115-
#define strterm_is_heredoc p->config->strterm_is_heredoc
116114
#define rb_imemo_tmpbuf_parser_heap p->config->tmpbuf_parser_heap
117115

118116
#define compile_callback p->config->compile_callback

0 commit comments

Comments
 (0)