Skip to content

Commit ee5c48e

Browse files
committed
ParseXS: refactor: add Node:IO_Param class
Add a ExtUtils::ParseXS::Node::IO_Param class as a subclass of the existing ExtUtils::ParseXS::Node::Param class. Then Param objects will be used solely to hold the details of a parameter which have been extracted from an XSUB's signature, while IO_Param objects contain a copy of that info, but augmented with any further info gleaned from INPUT or OUTPUT lines. For example with void foo(a) int a OUTPUT: a Then the Param object for 'a' will look something like: { arg_num => 1 var => 'a', } while the corresponding IO_Param object will look something like: { arg_num => 1, var => 'a', type => 'int', in_input => 1, in_output => 1, .... } All the code-emitting methods have been moved from Param to IO_Param, and the as_code() method has been renamed to as_input_code(), to better match the naming convention of the existing as_output_code() method: an IO_Param can generate code both to declare/initialise a var, and to update/return a var.
1 parent 2db64d2 commit ee5c48e

File tree

1 file changed

+54
-40
lines changed
  • dist/ExtUtils-ParseXS/lib/ExtUtils/ParseXS

1 file changed

+54
-40
lines changed

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

Lines changed: 54 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -856,7 +856,9 @@ sub parse {
856856
package ExtUtils::ParseXS::Node::Param;
857857

858858
# Node subclass which holds the state of one XSUB parameter, based on the
859-
# XSUB's signature and/or an INPUT line.
859+
# just the XSUB's signature. See also the Node::IO_Param subclass, which
860+
# augments the parameter declaration with info from INPUT and OUTPUT
861+
# lines.
860862

861863
BEGIN { $build_subclass->('', # parent
862864
# values derived from the XSUB's signature
@@ -875,19 +877,6 @@ BEGIN { $build_subclass->('', # parent
875877
'type', # The C type of the parameter
876878
'no_init', # don't initialise the parameter
877879

878-
# values derived from the XSUB's INPUT line
879-
'init_op', # initialisation type: one of =/+/;
880-
'init', # initialisation template code
881-
'is_addr', # INPUT var declared as '&foo'
882-
'is_alien', # var declared in INPUT line, but not in signature
883-
'in_input', # the parameter has appeared in an INPUT statement
884-
'defer', # deferred initialisation template code
885-
886-
# values derived from the XSUB's OUTPUT line
887-
'in_output', # the parameter has appeared in an OUTPUT statement
888-
'do_setmagic', # 'SETMAGIC: ENABLE' was active for this parameter
889-
'output_code', # the optional setting-code for this parameter
890-
891880
# derived values calculated later
892881
'proto', # overridden prototype char(s) (if any) from typemap
893882
)};
@@ -916,13 +905,37 @@ sub set_proto {
916905
}
917906

918907

919-
# $self->as_code():
908+
# ======================================================================
909+
910+
package ExtUtils::ParseXS::Node::IO_Param;
911+
912+
# Subclass of Node::Param which holds the state of one XSUB parameter,
913+
# based on the XSUB's signature, but also augmented by info from INPUT or
914+
# OUTPUT lines
915+
916+
BEGIN { $build_subclass->('Param', # parent
917+
# values derived from the XSUB's INPUT line
918+
'init_op', # initialisation type: one of =/+/;
919+
'init', # initialisation template code
920+
'is_addr', # INPUT var declared as '&foo'
921+
'is_alien', # var declared in INPUT line, but not in signature
922+
'in_input', # the parameter has appeared in an INPUT statement
923+
'defer', # deferred initialisation template code
924+
925+
# values derived from the XSUB's OUTPUT line
926+
'in_output', # the parameter has appeared in an OUTPUT statement
927+
'do_setmagic', # 'SETMAGIC: ENABLE' was active for this parameter
928+
'output_code', # the optional setting-code for this parameter
929+
)};
930+
931+
932+
# $self->as_input_code():
920933
#
921934
# Emit the param object as C code which declares and initialise the variable.
922935
# See also the as_output_code() method, which emits code to return the value
923936
# of that local var.
924937

925-
sub as_code {
938+
sub as_input_code {
926939
my __PACKAGE__ $self = shift;
927940
my ExtUtils::ParseXS $pxs = shift;
928941
my ExtUtils::ParseXS::Node::xsub $xsub = shift;
@@ -1789,19 +1802,20 @@ package ExtUtils::ParseXS::Node::Params;
17891802
# Node subclass which holds a list of the parameters for an XSUB
17901803
# (both directly found in the foo(....) signature, plus possibly synthetic
17911804
# ones such as THIS and RETVAL.
1792-
# It is a mainly a list of Node::Param children.
1805+
# It is a mainly a list of Node::Param or Node::IO_Param children.
17931806

17941807
BEGIN { $build_subclass->('', # parent
17951808

1796-
# inherited 'kids' field:
1797-
# Array ref of Node::Param objects representing
1798-
# the parameters of this XSUB - either the
1799-
# original ones as seen in the XSUB's signature,
1800-
# or per-xbody ones augmented by info from INPUT
1801-
# and OUTPUT sections.
1809+
# Inherited 'kids' field:
1810+
#
1811+
# Array ref of Node::Param or Node::IO_Param
1812+
# objects representing the parameters of this
1813+
# XSUB - either the original ones as seen in the
1814+
# XSUB's signature, or per-xbody ones augmented
1815+
# by info from INPUT and OUTPUT sections.
18021816

18031817
'names', # Hash ref mapping variable names to Node::Param
1804-
# objects
1818+
# or Node::IO_Param objects
18051819

18061820
'params_text', # The original text of the sig, e.g.
18071821
# 'param1, int param2 = 0'
@@ -2325,7 +2339,7 @@ sub parse {
23252339
$ioparams->{names} = {};
23262340

23272341
for my $op (@{$orig->{kids}}) {
2328-
my $p = ExtUtils::ParseXS::Node::Param->new($op);
2342+
my $p = ExtUtils::ParseXS::Node::IO_Param->new($op);
23292343
# don't copy the current proto state (from the most recent
23302344
# CASE) into the new CASE.
23312345
undef $p->{proto};
@@ -2495,8 +2509,8 @@ EOF
24952509

24962510
# Emit any 'char * CLASS' or 'Foo::Bar *THIS' declaration if needed
24972511

2498-
for my $param (grep $_->{is_synthetic}, @{$ioparams->{kids}}) {
2499-
$param->as_code($pxs, $xsub, $xbody);
2512+
for my $ioparam (grep $_->{is_synthetic}, @{$ioparams->{kids}}) {
2513+
$ioparam->as_input_code($pxs, $xsub, $xbody);
25002514
}
25012515

25022516
# Recent code emits a dXSTARG in a tighter scope and under
@@ -2510,7 +2524,7 @@ EOF
25102524
# Emit declaration/init code for any parameters which were
25112525
# declared with a type or length(foo). Do the length() ones first.
25122526

2513-
for my $param (
2527+
for my $ioparam (
25142528
grep $_->{is_ansi},
25152529
(
25162530
grep( $_->{is_length}, @{$ioparams->{kids}} ),
@@ -2519,7 +2533,7 @@ EOF
25192533
)
25202534

25212535
{
2522-
$param->as_code($pxs, $xsub, $xbody);
2536+
$ioparam->as_input_code($pxs, $xsub, $xbody);
25232537
}
25242538

25252539
# ----------------------------------------------------------------
@@ -3967,7 +3981,7 @@ package ExtUtils::ParseXS::Node::INPUT_line;
39673981
# Handle one line from an INPUT keyword block
39683982

39693983
BEGIN { $build_subclass->('keyline', # parent
3970-
'param', # The Param object associated with this INPUT line.
3984+
'param', # The IO_Param object associated with this INPUT line.
39713985

39723986
# The parsed components of this INPUT line:
39733987
'type', # char *
@@ -3984,18 +3998,18 @@ BEGIN { $build_subclass->('keyline', # parent
39843998
# INPUT_line object (which aren't further used for parsing or code
39853999
# generation)
39864000
#
3987-
# It also uses those values to create/update the Param object
4001+
# It also uses those values to create/update the IO_Param object
39884002
# associated with this variable. For example with
39894003
#
39904004
# void
39914005
# foo(a = 0)
39924006
# int a
39934007
#
3994-
# a Param object will already have been created with the name 'a' and
4008+
# a IO_Param object will already have been created with the name 'a' and
39954009
# default value '0' when the signature was parsed. Parsing the 'int a'
39964010
# line will set the INPUT_line object's fields to (type => 'int',
3997-
# name => 'a'), while the Param object will have its type field set to
3998-
# 'int'. The INPUT_line object also stores a ref to the Param object.
4011+
# name => 'a'), while the IO_Param object will have its type field set to
4012+
# 'int'. The INPUT_line object also stores a ref to the IO_Param object.
39994013
#
40004014

40014015
sub parse {
@@ -4062,7 +4076,7 @@ sub parse {
40624076

40634077
my $ioparams = $xbody->{ioparams};
40644078

4065-
my ExtUtils::ParseXS::Node::Param $param = $ioparams->{names}{$var_name};
4079+
my ExtUtils::ParseXS::Node::IO_Param $param = $ioparams->{names}{$var_name};
40664080

40674081
if (defined $param) {
40684082
# The var appeared in the signature too.
@@ -4106,7 +4120,7 @@ sub parse {
41064120
# general var declaration (which really should have been in a
41074121
# PREINIT section). Legal but nasty: flag is as 'alien'
41084122
$is_alien = 1;
4109-
$param = ExtUtils::ParseXS::Node::Param->new({
4123+
$param = ExtUtils::ParseXS::Node::IO_Param->new({
41104124
var => $var_name,
41114125
is_alien => 1,
41124126
});
@@ -4192,16 +4206,16 @@ sub as_code {
41924206
# Emit "type var" declaration and possibly various forms of
41934207
# initialiser code.
41944208

4195-
my $param = $self->{param};
4209+
my $ioparam = $self->{param};
41964210

41974211
# Synthetic params like THIS will be emitted later - they
41984212
# are treated like ANSI params, except the type can overridden
41994213
# within an INPUT statement
4200-
return if $param->{is_synthetic};
4214+
return if $ioparam->{is_synthetic};
42014215

42024216
# The param object contains data from both the INPUT line and
42034217
# the XSUB signature.
4204-
$param->as_code($pxs, $xsub, $xbody);
4218+
$ioparam->as_input_code($pxs, $xsub, $xbody);
42054219
}
42064220

42074221

@@ -4267,7 +4281,7 @@ sub parse {
42674281

42684282
$self->{name} = $outarg;
42694283

4270-
my ExtUtils::ParseXS::Node::Param $param =
4284+
my ExtUtils::ParseXS::Node::IO_Param $param =
42714285
$xbody->{ioparams}{names}{$outarg};
42724286
$self->{param} = $param;
42734287

0 commit comments

Comments
 (0)