Skip to content

Commit 1be2f76

Browse files
committed
ParseXS: refactor: rm xsub_deferred_code_lines
Remove the following field from the ExtUtils::ParseXS class: xsub_deferred_code_lines and replace it with this new field in the ExtUtils::ParseXS::Node::input_part class: deferred_code_lines This field contains initialisation lines which have been deferred to after any declarations emitted by INPUT and/or PREINIT lines. As such, it can be local to just the code generation phase (i.e. calling as_code()) during processing the input part of the xbody of an xsub - so store the field in the input_part node. Also, add input_part, init_part etc fields to the Node::xbody object. These are aliases to make access to its children easier - i.e. these were all already accessible as $xbody->{kids}{N}, but are now accessible as, e.g. $xbody->{input_part} too.
1 parent c07b5e5 commit 1be2f76

File tree

2 files changed

+32
-15
lines changed

2 files changed

+32
-15
lines changed

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS.pm

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -256,10 +256,6 @@ BEGIN {
256256

257257
# Per-XSUB code-emitting state:
258258

259-
'xsub_deferred_code_lines', # A multi-line string containing lines of
260-
# code to be emitted *after* all INPUT and
261-
# PREINIT keywords have been processed.
262-
263259
'xsub_stack_was_reset', # An XSprePUSH was emitted, so return values
264260
# should be PUSHed rather than just set.
265261
);

dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS/Node.pm

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -915,7 +915,7 @@ sub as_code {
915915

916916
print "\tSTRLEN\tSTRLEN_length_of_$name;\n";
917917
# defer this line until after all the other declarations
918-
$pxs->{xsub_deferred_code_lines} .=
918+
$pxs->{cur_xbody}{input_part}{deferred_code_lines} .=
919919
"\n\tXSauto_length_of_$name = STRLEN_length_of_$name;\n";
920920

921921
# this var will be declared using the normal typemap mechanism below
@@ -1125,7 +1125,7 @@ sub as_code {
11251125
if ($default eq 'NO_INIT') {
11261126
# for foo(a, b = NO_INIT), add code to initialise later only if
11271127
# an arg was supplied.
1128-
$pxs->{xsub_deferred_code_lines}
1128+
$pxs->{cur_xbody}{input_part}{deferred_code_lines}
11291129
.= sprintf "\n\tif (items >= %d) {\n%s;\n\t}\n",
11301130
$arg_num, $init_code;
11311131
}
@@ -1135,7 +1135,7 @@ sub as_code {
11351135
my $else = ($init_code =~ /\S/) ? "\telse {\n$init_code;\n\t}\n" : "";
11361136

11371137
$default =~ s/"/\\"/g; # escape double quotes
1138-
$pxs->{xsub_deferred_code_lines}
1138+
$pxs->{cur_xbody}{input_part}{deferred_code_lines}
11391139
.= sprintf "\n\tif (items < %d)\n\t %s = %s;\n%s",
11401140
$arg_num,
11411141
$var,
@@ -1153,7 +1153,8 @@ sub as_code {
11531153

11541154
print ";\n";
11551155

1156-
$pxs->{xsub_deferred_code_lines} .= sprintf "\n%s;\n", $init_code
1156+
$pxs->{cur_xbody}{input_part}{deferred_code_lines}
1157+
.= sprintf "\n%s;\n", $init_code
11571158
if $init_code =~ /\S/;
11581159
}
11591160
else {
@@ -1167,7 +1168,7 @@ sub as_code {
11671168
}
11681169

11691170
if (defined $defer) {
1170-
$pxs->{xsub_deferred_code_lines}
1171+
$pxs->{cur_xbody}{input_part}{deferred_code_lines}
11711172
.= $pxs->eval_input_typemap_code("qq\a$defer\a", $eval_vars) . "\n";
11721173
}
11731174
}
@@ -2236,6 +2237,18 @@ BEGIN { $build_subclass->('', # parent
22362237
# info from any INPUT and OUTPUT sections (which can
22372238
# vary between different CASEs)
22382239

2240+
2241+
# Objects representing the various parts of an xbody. These
2242+
# are aliases of the same objects in @{$self->{kids}} for easier
2243+
# access.
2244+
'input_part',
2245+
'init_part',
2246+
'code_part',
2247+
'output_part',
2248+
'cleanup_part',
2249+
2250+
# Misc parse state
2251+
22392252
'seen_RETVAL_in_CODE', # Bool: have seen 'RETVAL' within a CODE block
22402253
'seen_autocall', # Bool: this xbody has an autocall node
22412254
'OUTPUT_SETMAGIC_state', # Bool: most recent value of SETMAGIC in an
@@ -2291,6 +2304,7 @@ sub parse {
22912304
my $kid = "ExtUtils::ParseXS::Node::$part"->new();
22922305
if ($kid->parse($pxs)) {
22932306
push @{$self->{kids}}, $kid;
2307+
$self->{$part} = $kid;
22942308
}
22952309
}
22962310

@@ -2344,6 +2358,12 @@ EOF
23442358
package ExtUtils::ParseXS::Node::input_part;
23452359

23462360
BEGIN { $build_subclass->('', # parent
2361+
2362+
# Used during code generation:
2363+
# a multi-line string containing lines of code to be emitted *after*
2364+
# all INPUT and PREINIT keywords have been processed.
2365+
'deferred_code_lines',
2366+
23472367
)};
23482368

23492369

@@ -2353,10 +2373,6 @@ sub parse {
23532373

23542374
$self->SUPER::parse($pxs); # set file/line_no
23552375

2356-
# First, initialize variables manipulated by INPUT_handler().
2357-
$pxs->{xsub_deferred_code_lines} = ""; # lines to be emitted after
2358-
# PREINIT/INPUT
2359-
23602376
$pxs->{xsub_stack_was_reset} = 0; # XSprePUSH not yet emitted
23612377

23622378
# Process any implicit INPUT section.
@@ -2423,6 +2439,11 @@ sub as_code {
24232439

24242440
my $ioparams = $xbody->{ioparams};
24252441

2442+
# Lines to be emitted after PREINIT/INPUT. This may get populated
2443+
# by the as_code() methods we call of our kids.
2444+
$self->{deferred_code_lines} = "";
2445+
2446+
24262447
if ($self->{kids}) {
24272448
$_->as_code($pxs, $xsub, $xbody) for @{$self->{kids}};
24282449
}
@@ -2472,7 +2493,7 @@ EOF
24722493
# have been done. This is typically INPUT typemaps which don't
24732494
# start with a simple '$var =' and so would not have been emitted
24742495
# at the variable declaration stage.
2475-
print $pxs->{xsub_deferred_code_lines};
2496+
print $self->{deferred_code_lines};
24762497
}
24772498

24782499

@@ -4005,7 +4026,7 @@ sub parse {
40054026
# "; extra code" or "+ extra code" :
40064027
# append the extra code (after passing through eval) after all the
40074028
# INPUT and PREINIT blocks have been processed, indirectly using
4008-
# the $pxs->{xsub_deferred_code_lines} mechanism.
4029+
# the $input_part->{deferred_code_lines} mechanism.
40094030
# In addition, for '+', also generate the normal initialisation
40104031
# code from the standard typemap - assuming that it's a real
40114032
# parameter that appears in the signature as well as the INPUT

0 commit comments

Comments
 (0)