Skip to content

Commit bb6c311

Browse files
committed
Add option 'NoExport' to pp_def
1 parent bda65d4 commit bb6c311

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

lib/PDL/Ops.pd

+16-5
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,7 @@ EOF
128128
}
129129

130130
my $bitwise = delete $extra{Bitwise};
131+
my $export = delete $extra{Export};
131132
pp_def($name,
132133
Pars => 'a(); b(); [o]c();',
133134
OtherPars => 'int $swap',
@@ -136,6 +137,7 @@ EOF
136137
NoBadifNaN => 1,
137138
Inplace => [ 'a' ],
138139
Overload => [$op, $mutator, $bitwise],
140+
NoExport => !$export,
139141
Code => pp_line_numbers(__LINE__, <<EOF),
140142
PDL_IF_BAD(char anybad = 0;,)
141143
broadcastloop %{
@@ -184,6 +186,8 @@ ENDCODE
184186
$codestr = '$c() = ($GENERIC(c))'.$func.'($a(),$b());';
185187
}
186188
delete $extra{unsigned}; #remove the key so it doesn't get added in pp_def.
189+
my $export = delete $extra{Export};
190+
my $noinfix = delete $extra{NoInfix};
187191

188192
pp_def($name,
189193
HandleBad => 1,
@@ -192,7 +196,8 @@ ENDCODE
192196
OtherPars => 'int $swap',
193197
OtherParsDefaults => { swap => 0 },
194198
Inplace => [ 'a' ],
195-
Overload => [$funcov, $mutator],
199+
Overload => [$funcov, $mutator, undef, $noinfix],
200+
NoExport => !$export,
196201
Code => pp_line_numbers(__LINE__, <<EOF),
197202
PDL_IF_BAD(char anybad = 0;,)
198203
broadcastloop %{
@@ -232,6 +237,7 @@ sub ufunc {
232237
(map 'types('.$_->ppsym.') %{$b() = c'.$func.$_->floatsuffix.'($a());%}', @Ctypes),
233238
;
234239
}
240+
my $export = delete $extra{Export};
235241
# do not have to worry about propagation of the badflag when
236242
# inplace since only input ndarray is a, hence its badflag
237243
# won't change
@@ -242,6 +248,7 @@ sub ufunc {
242248
NoBadifNaN => 1,
243249
Inplace => 1,
244250
!$overload ? () : (Overload => $funcov),
251+
NoExport => !$export,
245252
Code => pp_line_numbers(__LINE__, <<EOF),
246253
PDL_IF_BAD(if ( \$ISBAD(a()) ) \$SETBAD(b()); else {,)
247254
$codestr
@@ -309,7 +316,7 @@ ufunc('bitnot','~',1,'unary bitwise negation',GenericTypes => $T);
309316

310317
# some standard binary functions
311318
bifunc('power',['pow','op**'],1,'raise ndarray C<$a> to the power C<$b>',GenericTypes => [@$C, @$F]);
312-
bifunc('atan2','atan2',0,'elementwise C<atan2> of two ndarrays',GenericTypes => $F);
319+
bifunc('atan2','atan2',0,'elementwise C<atan2> of two ndarrays',GenericTypes => $F, NoInfix => 1);
313320
bifunc('modulo',['MOD','op%'],1,'elementwise C<modulo> operation',unsigned=>1);
314321
bifunc('spaceship',['SPACE','op<=>'],0,'elementwise "<=>" operation');
315322

@@ -340,6 +347,7 @@ pp_def ( '_rabs',
340347
HandleBad => 1,
341348
NoBadifNaN => 1,
342349
Inplace => 1,
350+
NoExport => 1,
343351
Code => pp_line_numbers(__LINE__-1, qq{
344352
PDL_IF_BAD(if ( \$ISBAD(a()) ) \$SETBAD(b()); else,)
345353
$rabs_code
@@ -348,13 +356,14 @@ PDL_IF_BAD(if ( \$ISBAD(a()) ) \$SETBAD(b()); else,)
348356
PMFunc=>'',
349357
);
350358

351-
pp_export_nothing();
359+
# the following pp_def'ed functions will be exported
352360

353361
# make log10() work on scalars (returning scalars)
354362
# as well as ndarrays
355363
ufunc('log10','log10',0,'the base 10 logarithm', GenericTypes => $A,
356364
Exception => '$a() <= 0',
357365
NoTgmath => 1, # glibc for at least GCC 8.3.0 won't tgmath log10 though 7.1.0 did
366+
Export => 1,
358367
PMCode => <<'EOF',
359368
sub PDL::log10 {
360369
my ($x, $y) = @_;
@@ -390,12 +399,14 @@ PDL_IF_BAD(if (anybad) $PDLSTATESETBAD(b);,)
390399
sub cfunc {
391400
my ($name, $func, $make_real, $force_complex, $doc, $backcode, %extra) = @_;
392401
my $codestr = pp_line_numbers(__LINE__-1,"\$b() = $func(\$complexv());");
402+
my $export = delete $extra{Export};
393403
pp_def($name,
394404
GenericTypes=>$C,
395405
Pars => ($force_complex ? '!real ' : '').'complexv(); '.($make_real ? 'real' : '').' [o]b()',
396406
HandleBad => 1,
397407
NoBadifNaN => 1,
398408
(($make_real || $force_complex) ? () : (Inplace => 1)),
409+
NoExport => !$export,
399410
Code => pp_line_numbers(__LINE__-1, qq{
400411
PDL_IF_BAD(if ( \$ISBAD(complexv()) ) \$SETBAD(b()); else,)
401412
$codestr
@@ -414,8 +425,8 @@ PDL_IF_BAD(if ( \$ISBAD(complexv()) ) \$SETBAD(b()); else,)
414425
);
415426
}
416427

417-
cfunc('carg', 'carg', 1, 1, 'Returns the polar angle of a complex number.', undef);
418-
cfunc('conj', 'conj', 0, 0, 'complex conjugate.', undef);
428+
cfunc('carg', 'carg', 1, 1, 'Returns the polar angle of a complex number.', undef, Export => 1);
429+
cfunc('conj', 'conj', 0, 0, 'complex conjugate.', undef, Export => 1);
419430

420431
pp_def('czip',
421432
Pars => '!complex r(); !complex i(); complex [o]c()',

lib/PDL/PP.pm

+14-9
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ sub pp_def {
864864
}
865865
PDL::PP->printxs($obj{NewXSCode});
866866
pp_add_boot($obj{BootSetNewXS}) if $obj{BootSetNewXS};
867-
PDL::PP->pp_add_exported($name);
867+
PDL::PP->pp_add_exported($name) unless $obj{NoExport};
868868
PDL::PP::_pp_addpm_nolineno("\n".$obj{PdlDoc}."\n") if $obj{PdlDoc};
869869
PDL::PP::_pp_addpm_nolineno($obj{PMCode}) if defined $obj{PMCode};
870870
PDL::PP::_pp_addpm_nolineno($obj{PMFunc}."\n") if defined $obj{PMFunc};
@@ -1390,10 +1390,10 @@ $PDL::PP::deftbl =
13901390
}),
13911391
PDL::PP::Rule::Returns::EmptyString->new("InplaceCode", []),
13921392
PDL::PP::Rule->new("InplaceDocValues",
1393-
[qw(Name SignatureObj InplaceNormalised)],
1393+
[qw(Name SignatureObj InplaceNormalised Overload NoExport?)],
13941394
'doc describing usage inplace',
13951395
sub {
1396-
my ($name, $sig, $inplace) = @_;
1396+
my ($name, $sig, $inplace, $ovl, $noexport) = @_;
13971397
my @args = @{ $sig->args_callorder };
13981398
my %inplace_involved = map +($_=>1), my ($in, $out) = @$inplace;
13991399
my $meth_call = $args[0] eq $in;
@@ -1403,7 +1403,9 @@ $PDL::PP::deftbl =
14031403
!@args ? '' : "(@{[join ',', map qq{\$$_}, @args]})"
14041404
).";", []
14051405
];
1406-
push @vals, [ "$name(\$$in->inplace".(
1406+
my $op = defined($ovl) ? ref($ovl) ? $ovl->[0] : $ovl : '';
1407+
my $prefix = $noexport && $op ne $name ? "$::PDLOBJ\::" : "";
1408+
push @vals, [ "$prefix$name(\$$in->inplace".(
14071409
!@args ? '' : ",@{[join ',', map qq{\$$_}, @args]}"
14081410
).");", []];
14091411
$vals[0][1] = ["can be used inplace"];
@@ -1418,7 +1420,7 @@ $PDL::PP::deftbl =
14181420
my ($name, $sig, $ovl, $inplace) = @_;
14191421
confess "$name Overload given false value" if !$ovl;
14201422
$ovl = [$ovl] if !ref $ovl;
1421-
my ($op, $mutator, $bitwise) = @$ovl;
1423+
my ($op, $mutator, $bitwise, $noinfix) = @$ovl;
14221424
confess "$name Overload trying to define mutator but no inplace"
14231425
if $mutator && !$inplace;
14241426
my $one_arg = $sig->names_in == 1;
@@ -1463,7 +1465,8 @@ EOF
14631465
confess "$name error in Overload doc: !=1 output (@outs)" if @outs != 1;
14641466
my @ins = $sig->names_in;
14651467
my @vals = ["\$$outs[0] = ".(
1466-
!$one_arg ? "\$$ins[0] $op \$$ins[1]" :
1468+
!$one_arg ?
1469+
$noinfix ? "$op \$$ins[0], \$$ins[1]" : "\$$ins[0] $op \$$ins[1]" :
14671470
$op.($op =~ /[^a-z]/ ? '' : ' ')."\$$ins[0]"
14681471
).";",
14691472
["overloads the Perl '$op' operator"]
@@ -1475,12 +1478,12 @@ EOF
14751478

14761479
PDL::PP::Rule->new([qw(UsageDoc ParamDoc)],
14771480
[qw(Name Doc? SignatureObj OtherParsDefaults? ArgOrder?
1478-
OverloadDocValues InplaceDocValues ParamDesc? Lvalue?
1481+
OverloadDocValues InplaceDocValues ParamDesc? Lvalue? Overload NoExport?
14791482
)],
14801483
'generate "usage" section of doc',
14811484
sub {
14821485
my ($name, $doc, $sig, $otherdefaults, $argorder,
1483-
$overloadvals, $inplacevals, $paramdesc, $lvalue,
1486+
$overloadvals, $inplacevals, $paramdesc, $lvalue, $ovl, $noexport,
14841487
) = @_;
14851488
$otherdefaults ||= {};
14861489
$paramdesc ||= {};
@@ -1523,10 +1526,12 @@ EOF
15231526
push @argsets, [\@args, [], ['all arguments given']];
15241527
}
15251528
my @invocs = @$overloadvals;
1529+
my $op = defined($ovl) ? ref($ovl) ? $ovl->[0] : $ovl : '';
1530+
my $prefix = $noexport && $op ne $name ? "$::PDLOBJ\::" : "";
15261531
push @invocs, map [(!@{$_->[1]} ? '' :
15271532
@{$_->[1]} == 1 ? "\$$_->[1][0] = " :
15281533
"(".join(", ", map "\$$_", @{$_->[1]}).") = "
1529-
)."$name(".join(", ", map "\$$_", @{$_->[0]}).");",
1534+
)."$prefix$name(".join(", ", map "\$$_", @{$_->[0]}).");",
15301535
[@{$_->[2]}]], @argsets;
15311536
$argsets[0][2] = ['method call'];
15321537
$argsets[$_][2] = [] for 1..$#argsets; # they get the idea

lib/PDL/PP.pod

+16
Original file line numberDiff line numberDiff line change
@@ -1872,6 +1872,22 @@ Implements overloading of Perl operators. Documented automatically.
18721872
Added in PDL 2.099. Will overload in the current C<pp_bless> package,
18731873
which defaults to C<PDL>.
18741874

1875+
=head3 NoExport
1876+
1877+
=over 4
1878+
1879+
=item NoExport => 1
1880+
1881+
=back
1882+
1883+
A function that is defined by C<pp_def> will be automatically added
1884+
to the module's export list.
1885+
1886+
By specifying a C<true> value for C<NoExport> this behaviour can be
1887+
suppressed.
1888+
1889+
Added in PDL 2.xxx.
1890+
18751891
=head3 ParamDesc
18761892

18771893
# in Primitive.pd

0 commit comments

Comments
 (0)