Skip to content

Commit dd6f569

Browse files
committed
Define a newPADxVOP() convenience function
This function conveniently sets the ->op_targ field of the returned op, making it neater to use inline in larger trees of new*OP functions used to build optree fragments. This function is implemented as `static inline`, for speed and code-size reasons.
1 parent a357bb9 commit dd6f569

File tree

4 files changed

+38
-0
lines changed

4 files changed

+38
-0
lines changed

embed.fnc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1519,6 +1519,7 @@ AxpdRT |PADNAMELIST *|newPADNAMELIST|size_t max
15191519
#ifdef USE_ITHREADS
15201520
ApdR |OP* |newPADOP |I32 type|I32 flags|NN SV* sv
15211521
#endif
1522+
ApdRi |OP* |newPADxVOP |I32 type|I32 flags|PADOFFSET padix
15221523
ApdR |OP* |newPMOP |I32 type|I32 flags
15231524
ApdR |OP* |newPVOP |I32 type|I32 flags|NULLOK char* pv
15241525
ApdR |SV* |newRV |NN SV *const sv

embed.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -368,6 +368,7 @@
368368
#define newPADNAMELIST Perl_newPADNAMELIST
369369
#define newPADNAMEouter Perl_newPADNAMEouter
370370
#define newPADNAMEpvn Perl_newPADNAMEpvn
371+
#define newPADxVOP(a,b,c) Perl_newPADxVOP(aTHX_ a,b,c)
371372
#define newPMOP(a,b) Perl_newPMOP(aTHX_ a,b)
372373
#define newPROG(a) Perl_newPROG(aTHX_ a)
373374
#define newPVOP(a,b,c) Perl_newPVOP(aTHX_ a,b,c)

inline.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3086,6 +3086,36 @@ Perl_cx_popgiven(pTHX_ PERL_CONTEXT *cx)
30863086
SvREFCNT_dec(sv);
30873087
}
30883088

3089+
/*
3090+
=for apidoc newPADxVOP
3091+
3092+
Constructs, checks and returns an op containing a pad offset. C<type> is
3093+
the opcode, which should be one of C<OP_PADSV>, C<OP_PADAV>, C<OP_PADHV>
3094+
or C<OP_PADCV>. The returned op will have the C<op_targ> field set by
3095+
the C<padix> argument.
3096+
3097+
This is convenient when constructing a large optree in nested function
3098+
calls, as it avoids needing to store the pad op directly to set the
3099+
C<op_targ> field as a side-effect. For example
3100+
3101+
o = op_append_elem(OP_LINESEQ, o,
3102+
newPADxVOP(OP_PADSV, 0, padix));
3103+
3104+
=cut
3105+
*/
3106+
3107+
PERL_STATIC_INLINE OP *
3108+
Perl_newPADxVOP(pTHX_ I32 type, I32 flags, PADOFFSET padix)
3109+
{
3110+
PERL_ARGS_ASSERT_NEWPADXVOP;
3111+
3112+
assert(type == OP_PADSV || type == OP_PADAV || type == OP_PADHV
3113+
|| type == OP_PADCV);
3114+
OP *o = newOP(type, flags);
3115+
o->op_targ = padix;
3116+
return o;
3117+
}
3118+
30893119
/* ------------------ util.h ------------------------------------------- */
30903120

30913121
/*

proto.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2763,6 +2763,12 @@ PERL_CALLCONV PADNAME * Perl_newPADNAMEpvn(const char *s, STRLEN len)
27632763
#define PERL_ARGS_ASSERT_NEWPADNAMEPVN \
27642764
assert(s)
27652765

2766+
#ifndef PERL_NO_INLINE_FUNCTIONS
2767+
PERL_STATIC_INLINE OP* Perl_newPADxVOP(pTHX_ I32 type, I32 flags, PADOFFSET padix)
2768+
__attribute__warn_unused_result__;
2769+
#define PERL_ARGS_ASSERT_NEWPADXVOP
2770+
#endif
2771+
27662772
PERL_CALLCONV OP* Perl_newPMOP(pTHX_ I32 type, I32 flags)
27672773
__attribute__warn_unused_result__;
27682774
#define PERL_ARGS_ASSERT_NEWPMOP

0 commit comments

Comments
 (0)