Skip to content

Commit 5a137aa

Browse files
committed
ParseXS: refactor: sort out SCOPE object fields
This commit renames the ExtUtils::ParseXS class field xsub_SCOPE_enabled to file_SCOPE_enabled and adds a new field in the ExtUtils::ParseXS::Node::xsub class: SCOPE_enabled This is because SCOPE can be used either in file scope: SCOPE: ENABLE int foo(...) or in XSUB scope, int foo(...) SCOPE: ENABLE The file_SCOPE_enabled field records whether a SCOPE keyword has been encountered just before the XSUB, while the Node::xsub SCOPE_enabled field is initialised to the current value of file_SCOPE_enabled when XSUB parsing starts, and is updated if the SCOPE keyword is encountered within the XSUB.
1 parent ddf9e90 commit 5a137aa

File tree

2 files changed

+34
-10
lines changed

2 files changed

+34
-10
lines changed

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,9 @@ BEGIN {
235235

236236
'cur_xbody', # The Node::xbody currently being parsed
237237

238-
'xsub_SCOPE_enabled', # Bool: SCOPE ENABLEd
238+
'file_SCOPE_enabled', # Bool: the current state of the file-scope
239+
# (as opposed to
240+
# XSUB-scope) SCOPE keyword
239241
);
240242

241243
# do 'use fields', except: fields needs Hash::Util which is XS, which
@@ -563,9 +565,17 @@ EOM
563565
." followed by a statement on column one?)")
564566
if $self->{line}->[0] =~ /^\s/;
565567

566-
# Initialize some per-XSUB instance variables:
567-
568-
$self->{xsub_SCOPE_enabled} = 0;
568+
# The SCOPE keyword can appear both in file scope (just before an
569+
# XSUB) and as an XSUB keyword. This field maintains the state of the
570+
# former: reset it at the start of processing any file-scoped
571+
# keywords just before the XSUB (i.e. without any blank lines, e.g.
572+
# SCOPE: ENABLE
573+
# int
574+
# foo(...)
575+
# These semantics may not be particularly sensible, but they maintain
576+
# backwards compatibility for now.
577+
578+
$self->{file_SCOPE_enabled} = 0;
569579

570580
# Process next line
571581

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

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,9 @@ BEGIN { $build_subclass->('', # parent
265265
# "2": empty prototype
266266
# other: a specific prototype.
267267

268+
'SCOPE_enabled', # "SCOPE: ENABLE" seen, in either the
269+
# file or XSUB part of the XS file
270+
268271
)};
269272

270273

@@ -278,6 +281,10 @@ sub parse {
278281
# global PROTOTYPES default
279282
$self->{prototype} = $pxs->{PROTOTYPES_value};
280283

284+
# inherit any SCOPE: value that immediately preceded the XSUB
285+
# declaration
286+
$self->{SCOPE_enabled} = $pxs->{file_SCOPE_enabled};
287+
281288
# Parse the XSUB's declaration (return type, name, parameters)
282289

283290
my $decl = ExtUtils::ParseXS::Node::xsub_decl->new();
@@ -1115,7 +1122,9 @@ sub as_code {
11151122
}
11161123

11171124
if ($expr =~ m#/\*.*scope.*\*/#i) { # "scope" in C comments
1118-
$pxs->{xsub_SCOPE_enabled} = 1;
1125+
# XXX this really aught to be determined during parse rather
1126+
# than during code emitting.
1127+
$xsub->{SCOPE_enabled} = 1;
11191128
}
11201129

11211130
# Specify additional environment for when a template derived from a
@@ -1172,7 +1181,7 @@ sub as_code {
11721181
$else;
11731182
}
11741183
}
1175-
elsif ($pxs->{xsub_SCOPE_enabled} or $init_code !~ /^\s*\Q$var\E =/) {
1184+
elsif ($xsub->{SCOPE_enabled} or $init_code !~ /^\s*\Q$var\E =/) {
11761185
# The template is likely a full block rather than a '$var = ...'
11771186
# expression. Just terminate the variable declaration, and defer the
11781187
# initialisation.
@@ -2352,7 +2361,7 @@ sub as_code {
23522361
# Emit trailers for the body of the XSUB
23532362
# ----------------------------------------------------------------
23542363

2355-
if ($pxs->{xsub_SCOPE_enabled}) {
2364+
if ($xsub->{SCOPE_enabled}) {
23562365
# the matching opens were emitted in input_part->as_code()
23572366
print " $close_brace\n";
23582367
# PPCODE->as_code emits its own LEAVE and return, so this
@@ -2467,7 +2476,7 @@ sub as_code {
24672476
}
24682477

24692478
# The matching closes will be emitted in xbody->as_code()
2470-
print ExtUtils::ParseXS::Q(<<"EOF") if $pxs->{xsub_SCOPE_enabled};
2479+
print ExtUtils::ParseXS::Q(<<"EOF") if $xsub->{SCOPE_enabled};
24712480
| ENTER;
24722481
| $open_brace
24732482
EOF
@@ -3054,7 +3063,12 @@ sub parse {
30543063
if $pxs->{cur_xsub}->{seen_SCOPE};
30553064
$pxs->{cur_xsub}->{seen_SCOPE} = 1;
30563065

3057-
$pxs->{xsub_SCOPE_enabled} = $self->{enable};
3066+
# Note that currently this parse method can be called either while
3067+
# parsing an XSUB, or while processing file-scoped keywords
3068+
# just before an XSUB declaration. Sop potentially set both types of
3069+
# state.
3070+
$pxs->{cur_xsub}{SCOPE_enabled} = $self->{enable} if $pxs->{cur_xsub};
3071+
$pxs->{file_SCOPE_enabled} = $self->{enable};
30583072
1;
30593073
}
30603074

@@ -3567,7 +3581,7 @@ sub as_code {
35673581

35683582
$self->SUPER::as_code($pxs, $xsub, $xbody); # emit code block
35693583

3570-
print "\tLEAVE;\n" if $pxs->{xsub_SCOPE_enabled};
3584+
print "\tLEAVE;\n" if $pxs->{cur_xsub}{SCOPE_enabled};
35713585

35723586
# Suppress "statement is unreachable" warning on HPUX
35733587
print "#if defined(__HP_cc) || defined(__HP_aCC)\n",

0 commit comments

Comments
 (0)