X-Git-Url: https://perl5.git.perl.org/perl5.git/blobdiff_plain/307cbb9fc37bc4e2ceb1d77690249dea0ccbc0ca..61b16eb90f32a2433d6de43e477a03b8d9fed039:/pad.c diff --git a/pad.c b/pad.c index 25513b7..9bb8cfe 100644 --- a/pad.c +++ b/pad.c @@ -38,10 +38,10 @@ not callable at will and are always thrown away after the eval"" is done executing). Require'd files are simply evals without any outer lexical scope. -XSUBs do not have a CvPADLIST. dXSTARG fetches values from PL_curpad, +XSUBs do not have a C. C fetches values from C, but that is really the callers pad (a slot of which is allocated by -every entersub). Do not get or set CvPADLIST if a CV is an XSUB (as -determined by C), CvPADLIST slot is reused for a different +every entersub). Do not get or set C if a CV is an XSUB (as +determined by C), C slot is reused for a different internal purpose in XSUBs. The PADLIST has a C array where pads are stored. @@ -55,43 +55,47 @@ array, so don't rely on it. See L. The CvDEPTH'th entry of a PADLIST is a PAD (an AV) which is the stack frame at that depth of recursion into the CV. The 0th slot of a frame AV is an -AV which is @_. Other entries are storage for variables and op targets. +AV which is C<@_>. Other entries are storage for variables and op targets. Iterating over the PADNAMELIST iterates over all possible pad -items. Pad slots for targets (SVs_PADTMP) +items. Pad slots for targets (C) and GVs end up having &PL_padname_undef "names", while slots for constants -have &PL_padname_const "names" (see pad_alloc()). That &PL_padname_undef -and &PL_padname_const are used is an implementation detail subject to -change. To test for them, use C and C, respectively. +have C<&PL_padname_const> "names" (see C>). That +C<&PL_padname_undef> +and C<&PL_padname_const> are used is an implementation detail subject to +change. To test for them, use C and +S>, respectively. -Only my/our variable slots get valid names. +Only C/C variable slots get valid names. The rest are op targets/GVs/constants which are statically allocated or resolved at compile time. These don't have names by which they can be looked up from Perl code at run time through eval"" the way -my/our variables can be. Since they can't be looked up by "name" +C/C variables can be. Since they can't be looked up by "name" but only by their index allocated at compile time (which is usually -in PL_op->op_targ), wasting a name SV for them doesn't make sense. +in Cop_targ>), wasting a name SV for them doesn't make sense. The pad names in the PADNAMELIST have their PV holding the name of -the variable. The COP_SEQ_RANGE_LOW and _HIGH fields form a range +the variable. The C and C<_HIGH> fields form a range (low+1..high inclusive) of cop_seq numbers for which the name is valid. During compilation, these fields may hold the special value PERL_PADSEQ_INTRO to indicate various stages: - COP_SEQ_RANGE_LOW _HIGH - ----------------- ----- - PERL_PADSEQ_INTRO 0 variable not yet introduced: { my ($x - valid-seq# PERL_PADSEQ_INTRO variable in scope: { my ($x) - valid-seq# valid-seq# compilation of scope complete: { my ($x) } - -For typed lexicals PadnameTYPE points at the type stash. For C -lexicals, PadnameOURSTASH points at the stash of the associated global (so + COP_SEQ_RANGE_LOW _HIGH + ----------------- ----- + PERL_PADSEQ_INTRO 0 variable not yet introduced: + { my ($x + valid-seq# PERL_PADSEQ_INTRO variable in scope: + { my ($x) + valid-seq# valid-seq# compilation of scope complete: + { my ($x) } + +For typed lexicals C points at the type stash. For C +lexicals, C points at the stash of the associated global (so that duplicate C declarations in the same package can be detected). -PadnameGEN is sometimes used to store the generation number during +C is sometimes used to store the generation number during compilation. -If PadnameOUTER is set on the pad name, then that slot in the frame AV +If C is set on the pad name, then that slot in the frame AV is a REFCNT'ed reference to a lexical from "outside". Such entries are sometimes referred to as 'fake'. In this case, the name does not use 'low' and 'high' to store a cop_seq range, since it is in scope @@ -101,20 +105,20 @@ instantiated multiple times?), and for fake ANONs, 'low' contains the index within the parent's pad where the lexical's value is stored, to make cloning quicker. -If the 'name' is '&' the corresponding entry in the PAD +If the 'name' is C<&> the corresponding entry in the PAD is a CV representing a possible closure. Note that formats are treated as anon subs, and are cloned each time write is called (if necessary). -The flag SVs_PADSTALE is cleared on lexicals each time the my() is executed, +The flag C is cleared on lexicals each time the C is executed, and set on scope exit. This allows the -'Variable $x is not available' warning +C<"Variable $x is not available"> warning to be generated in evals, such as { my $x = 1; sub f { eval '$x'} } f(); -For state vars, SVs_PADSTALE is overloaded to mean 'not yet initialised', +For state vars, C is overloaded to mean 'not yet initialised', but this internal state is stored in a separate pad entry. =for apidoc AmxU|PADNAMELIST *|PL_comppad_name @@ -159,7 +163,7 @@ Perl_set_padlist(CV * cv, PADLIST *padlist){ # if PTRSIZE == 8 assert((Size_t)padlist != UINT64_C(0xEFEFEFEFEFEFEFEF)); # elif PTRSIZE == 4 - assert((Size_t)padlist != UINT64_C(0xEFEFEFEF)); + assert((Size_t)padlist != 0xEFEFEFEF); # else # error unknown pointer size # endif @@ -285,7 +289,7 @@ Perl_pad_new(pTHX_ int flags) Clear out all the active components of a CV. This can happen either by an explicit C, or by the reference count going to zero. -In the former case, we keep the CvOUTSIDE pointer, so that any anonymous +In the former case, we keep the C pointer, so that any anonymous children can still follow the full lexical scope chain. =cut @@ -485,10 +489,10 @@ Perl_cv_undef_flags(pTHX_ CV *cv, U32 flags) /* =for apidoc cv_forget_slab -When a CV has a reference count on its slab (CvSLABBED), it is responsible +When a CV has a reference count on its slab (C), it is responsible for making sure it is freed. (Hence, no two CVs should ever have a reference count on the same slab.) The CV only needs to reference the slab -during compilation. Once it is compiled and CvROOT attached, it has +during compilation. Once it is compiled and C attached, it has finished its job, so it can forget the slab. =cut @@ -497,11 +501,12 @@ finished its job, so it can forget the slab. void Perl_cv_forget_slab(pTHX_ CV *cv) { - const bool slabbed = !!CvSLABBED(cv); + bool slabbed; OPSLAB *slab = NULL; - PERL_ARGS_ASSERT_CV_FORGET_SLAB; - + if (!cv) + return; + slabbed = cBOOL(CvSLABBED(cv)); if (!slabbed) return; CvSLABBED_off(cv); @@ -528,10 +533,10 @@ Perl_cv_forget_slab(pTHX_ CV *cv) Allocates a place in the currently-compiling pad (via L) and -then stores a name for that entry. I is adopted and +then stores a name for that entry. C is adopted and becomes the name entry; it must already contain the name -string. I and I and the C -flag get added to I. None of the other +string. C and C and the C +flag get added to C. None of the other processing of L is done. Returns the offset of the allocated pad slot. @@ -576,15 +581,15 @@ variable. Stores the name and other metadata in the name part of the pad, and makes preparations to manage the variable's lexical scoping. Returns the offset of the allocated pad slot. -I/I specify the variable's name, including leading sigil. -If I is non-null, the name is for a typed lexical, and this -identifies the type. If I is non-null, it's a lexical reference +C/C specify the variable's name, including leading sigil. +If C is non-null, the name is for a typed lexical, and this +identifies the type. If C is non-null, it's a lexical reference to a package variable, and this identifies the package. The following flags can be OR'ed together: - padadd_OUR redundantly specifies if it's a package var - padadd_STATE variable will retain value persistently - padadd_NO_DUP_CHECK skip check for lexical shadowing + padadd_OUR redundantly specifies if it's a package var + padadd_STATE variable will retain value persistently + padadd_NO_DUP_CHECK skip check for lexical shadowing =cut */ @@ -682,7 +687,7 @@ Perl_pad_add_name_sv(pTHX_ SV *name, U32 flags, HV *typestash, HV *ourstash) Allocates a place in the currently-compiling pad, returning the offset of the allocated pad slot. No name is initially attached to the pad slot. -I is a set of flags indicating the kind of pad entry required, +C is a set of flags indicating the kind of pad entry required, which will be set in the value SV for the allocated pad entry: SVs_PADMY named lexical variable ("my", "our", "state") @@ -695,7 +700,7 @@ does not cause the SV in the pad slot to be marked read-only, but simply tells C that it I be made read-only (by the caller), or at least should be treated as such. -I should be an opcode indicating the type of operation that the +C should be an opcode indicating the type of operation that the pad entry is to support. This doesn't affect operational semantics, but is used for debugging. @@ -785,12 +790,12 @@ Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype) Allocates a place in the currently-compiling pad (via L) for an anonymous function that is lexically scoped inside the currently-compiling function. -The function I is linked into the pad, and its C link +The function C is linked into the pad, and its C link to the outer scope is weakened to avoid a reference loop. One reference count is stolen, so you may need to do C. -I should be an opcode indicating the type of operation that the +C should be an opcode indicating the type of operation that the pad entry is to support. This doesn't affect operational semantics, but is used for debugging. @@ -849,11 +854,11 @@ Perl_pad_add_weakref(pTHX_ CV* func) Check for duplicate declarations: report any of: - * a my in the current scope with the same name; - * an our (anywhere in the pad) with the same name and the - same stash as C + * a 'my' in the current scope with the same name; + * an 'our' (anywhere in the pad) with the same name and the + same stash as 'ourstash' -C indicates that the name to check is an 'our' declaration. +C indicates that the name to check is an C<"our"> declaration. =cut */ @@ -932,8 +937,8 @@ S_pad_check_dup(pTHX_ PADNAME *name, U32 flags, const HV *ourstash) Given the name of a lexical variable, find its position in the currently-compiling pad. -I/I specify the variable's name, including leading sigil. -I is reserved and must be zero. +C/C specify the variable's name, including leading sigil. +C is reserved and must be zero. If it is not in the current pad but appears in the pad of any lexically enclosing scope, then a pseudo-entry for it is added in the current pad. Returns the offset in the current pad, @@ -959,6 +964,10 @@ Perl_pad_findmy_pvn(pTHX_ const char *namepv, STRLEN namelen, U32 flags) Perl_croak(aTHX_ "panic: pad_findmy_pvn illegal flag bits 0x%" UVxf, (UV)flags); + /* compilation errors can zero PL_compcv */ + if (!PL_compcv) + return NOT_IN_PAD; + offset = pad_findlex(namepv, namelen, flags, PL_compcv, PL_cop_seqmax, 1, NULL, &out_pn, &out_flags); if ((PADOFFSET)offset != NOT_IN_PAD) @@ -1095,18 +1104,18 @@ Find a named lexical anywhere in a chain of nested pads. Add fake entries in the inner pads if it's found in an outer one. Returns the offset in the bottom pad of the lex or the fake lex. -cv is the CV in which to start the search, and seq is the current cop_seq -to match against. If warn is true, print appropriate warnings. The out_* +C is the CV in which to start the search, and seq is the current C +to match against. If C is true, print appropriate warnings. The C* vars return values, and so are pointers to where the returned values -should be stored. out_capture, if non-null, requests that the innermost -instance of the lexical is captured; out_name is set to the innermost -matched pad name or fake pad name; out_flags returns the flags normally -associated with the PARENT_FAKELEX_FLAGS field of a fake pad name. +should be stored. C, if non-null, requests that the innermost +instance of the lexical is captured; C is set to the innermost +matched pad name or fake pad name; C returns the flags normally +associated with the C field of a fake pad name. -Note that pad_findlex() is recursive; it recurses up the chain of CVs, +Note that C is recursive; it recurses up the chain of CVs, then comes back down, adding fake entries as it goes. It has to be this way -because fake names in anon protoypes have to store in xlow the index into +because fake names in anon protoypes have to store in C the index into the parent pad. =cut @@ -1320,10 +1329,6 @@ S_pad_findlex(pTHX_ const char *namepv, STRLEN namelen, U32 flags, const CV* cv, return 0; /* this dummy (and invalid) value isnt used by the caller */ { - /* This relies on sv_setsv_flags() upgrading the destination to the same - type as the source, independent of the flags set, and on it being - "good" and only copying flag bits and pointers that it understands. - */ PADNAME *new_name = newPADNAMEouter(*out_name); PADNAMELIST * const ocomppad_name = PL_comppad_name; PAD * const ocomppad = PL_comppad; @@ -1378,8 +1383,8 @@ S_pad_findlex(pTHX_ const char *namepv, STRLEN namelen, U32 flags, const CV* cv, /* =for apidoc Am|SV *|pad_sv|PADOFFSET po -Get the value at offset I in the current (compiling or executing) pad. -Use macro PAD_SV instead of calling this function directly. +Get the value at offset C in the current (compiling or executing) pad. +Use macro C instead of calling this function directly. =cut */ @@ -1401,8 +1406,8 @@ Perl_pad_sv(pTHX_ PADOFFSET po) /* =for apidoc Am|void|pad_setsv|PADOFFSET po|SV *sv -Set the value at offset I in the current (compiling or executing) pad. -Use the macro PAD_SETSV() rather than calling this function directly. +Set the value at offset C in the current (compiling or executing) pad. +Use the macro C rather than calling this function directly. =cut */ @@ -1574,7 +1579,7 @@ Perl_pad_leavemy(pTHX) /* =for apidoc m|void|pad_swipe|PADOFFSET po|bool refadjust -Abandon the tmp in the current pad at offset po and replace with a +Abandon the tmp in the current pad at offset C and replace with a new one. =cut @@ -1663,7 +1668,7 @@ S_pad_reset(pTHX) Tidy up a pad at the end of compilation of the code to which it belongs. Jobs performed here are: remove most stuff from the pads of anonsub -prototypes; give it a @_; mark temporaries as such. I indicates +prototypes; give it a C<@_>; mark temporaries as such. C indicates the kind of subroutine: padtidy_SUB ordinary subroutine @@ -1935,7 +1940,7 @@ S_cv_dump(pTHX_ const CV *cv, const char *title) /* =for apidoc Am|CV *|cv_clone|CV *proto -Clone a CV, making a lexical closure. I supplies the prototype +Clone a CV, making a lexical closure. C supplies the prototype of the function: its code, pad structure, and other attributes. The prototype is combined with a capture of outer lexicals to which the code refers, which are taken from the currently-executing instance of @@ -1944,10 +1949,11 @@ the immediately surrounding code. =cut */ -static CV *S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside); +static CV *S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside, HV *cloned); static CV * -S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, bool newcv) +S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, HV *cloned, + bool newcv) { I32 ix; PADLIST* const protopadlist = CvPADLIST(proto); @@ -1959,7 +1965,8 @@ S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, bool newcv) const I32 fpad = AvFILLp(protopad); SV** outpad; long depth; - bool subclones = FALSE; + U32 subclones = 0; + bool trouble = FALSE; assert(!CvUNIQUE(proto)); @@ -2046,7 +2053,9 @@ S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, bool newcv) second pass. */ if (SvPAD_STATE(namesv) && !CvCLONED(ppad[ix])) { assert(SvTYPE(ppad[ix]) == SVt_PVCV); - subclones = 1; + subclones ++; + if (CvOUTSIDE(ppad[ix]) != proto) + trouble = TRUE; sv = newSV_type(SVt_PVCV); CvLEXICAL_on(sv); } @@ -2092,12 +2101,70 @@ S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, bool newcv) } if (subclones) - for (ix = fpad; ix > 0; ix--) { + { + if (trouble || cloned) { + /* Uh-oh, we have trouble! At least one of the state subs here + has its CvOUTSIDE pointer pointing somewhere unexpected. It + could be pointing to another state protosub that we are + about to clone. So we have to track which sub clones come + from which protosubs. If the CvOUTSIDE pointer for a parti- + cular sub points to something we have not cloned yet, we + delay cloning it. We must loop through the pad entries, + until we get a full pass with no cloning. If any uncloned + subs remain (probably nested inside anonymous or ‘my’ subs), + then they get cloned in a final pass. + */ + bool cloned_in_this_pass; + if (!cloned) + cloned = (HV *)sv_2mortal((SV *)newHV()); + do { + cloned_in_this_pass = FALSE; + for (ix = fpad; ix > 0; ix--) { + PADNAME * const name = + (ix <= fname) ? pname[ix] : NULL; + if (name && name != &PL_padname_undef + && !PadnameOUTER(name) && PadnamePV(name)[0] == '&' + && PadnameIsSTATE(name) && !CvCLONED(PL_curpad[ix])) + { + CV * const protokey = CvOUTSIDE(ppad[ix]); + CV ** const cvp = protokey == proto + ? &cv + : (CV **)hv_fetch(cloned, (char *)&protokey, + sizeof(CV *), 0); + if (cvp && *cvp) { + S_cv_clone(aTHX_ (CV *)ppad[ix], + (CV *)PL_curpad[ix], + *cvp, cloned); + (void)hv_store(cloned, (char *)&ppad[ix], + sizeof(CV *), + SvREFCNT_inc_simple_NN(PL_curpad[ix]), + 0); + subclones--; + cloned_in_this_pass = TRUE; + } + } + } + } while (cloned_in_this_pass); + if (subclones) + for (ix = fpad; ix > 0; ix--) { + PADNAME * const name = + (ix <= fname) ? pname[ix] : NULL; + if (name && name != &PL_padname_undef + && !PadnameOUTER(name) && PadnamePV(name)[0] == '&' + && PadnameIsSTATE(name) && !CvCLONED(PL_curpad[ix])) + S_cv_clone(aTHX_ (CV *)ppad[ix], + (CV *)PL_curpad[ix], + CvOUTSIDE(ppad[ix]), cloned); + } + } + else for (ix = fpad; ix > 0; ix--) { PADNAME * const name = (ix <= fname) ? pname[ix] : NULL; if (name && name != &PL_padname_undef && !PadnameOUTER(name) && PadnamePV(name)[0] == '&' && PadnameIsSTATE(name)) - S_cv_clone(aTHX_ (CV *)ppad[ix], (CV *)PL_curpad[ix], cv); + S_cv_clone(aTHX_ (CV *)ppad[ix], (CV *)PL_curpad[ix], cv, + NULL); } + } if (newcv) SvREFCNT_inc_simple_void_NN(cv); LEAVE; @@ -2190,7 +2257,7 @@ S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, bool newcv) } static CV * -S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside) +S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside, HV *cloned) { #ifdef USE_ITHREADS dVAR; @@ -2225,7 +2292,7 @@ S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside) mg_copy((SV *)proto, (SV *)cv, 0, 0); if (CvPADLIST(proto)) - cv = S_cv_clone_pad(aTHX_ proto, cv, outside, newcv); + cv = S_cv_clone_pad(aTHX_ proto, cv, outside, cloned, newcv); DEBUG_Xv( PerlIO_printf(Perl_debug_log, "\nPad CV clone\n"); @@ -2243,7 +2310,7 @@ Perl_cv_clone(pTHX_ CV *proto) PERL_ARGS_ASSERT_CV_CLONE; if (!CvPADLIST(proto)) Perl_croak(aTHX_ "panic: no pad in cv_clone"); - return S_cv_clone(aTHX_ proto, NULL, NULL); + return S_cv_clone(aTHX_ proto, NULL, NULL, NULL); } /* Called only by pp_clonecv */ @@ -2252,7 +2319,7 @@ Perl_cv_clone_into(pTHX_ CV *proto, CV *target) { PERL_ARGS_ASSERT_CV_CLONE_INTO; cv_undef(target); - return S_cv_clone(aTHX_ proto, target, NULL); + return S_cv_clone(aTHX_ proto, target, NULL, NULL); } /* @@ -2267,7 +2334,7 @@ An SV may be passed as a second argument. If so, the name will be assigned to it and it will be returned. Otherwise the returned SV will be a new mortal. -If the I include CV_NAME_NOTQUAL, then the package name will not be +If C include C, then the package name will not be included. If the first argument is neither a CV nor a GV, this flag is ignored (subject to change). @@ -2307,8 +2374,8 @@ Perl_cv_name(pTHX_ CV *cv, SV *sv, U32 flags) /* =for apidoc m|void|pad_fixup_inner_anons|PADLIST *padlist|CV *old_cv|CV *new_cv -For any anon CVs in the pad, change CvOUTSIDE of that CV from -old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be +For any anon CVs in the pad, change C of that CV from +C to C if necessary. Needed when a newly-compiled CV has to be moved to a pre-existing CV struct. =cut @@ -2379,7 +2446,7 @@ Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv) Push a new pad frame onto the padlist, unless there's already a pad at this depth, in which case don't bother creating a new one. Then give -the new pad an @_ in slot zero. +the new pad an C<@_> in slot zero. =cut */ @@ -2592,7 +2659,7 @@ is allocated. */ PADNAMELIST * -Perl_newPADNAMELIST(pTHX_ size_t max) +Perl_newPADNAMELIST(size_t max) { PADNAMELIST *pnl; Newx(pnl, 1, PADNAMELIST); @@ -2647,7 +2714,7 @@ Fetches the pad name from the given index. */ PADNAME * -Perl_padnamelist_fetch(pTHX_ PADNAMELIST *pnl, SSize_t key) +Perl_padnamelist_fetch(PADNAMELIST *pnl, SSize_t key) { PERL_ARGS_ASSERT_PADNAMELIST_FETCH; ASSUME(key >= 0); @@ -2716,15 +2783,15 @@ Perl_padnamelist_dup(pTHX_ PADNAMELIST *srcpad, CLONE_PARAMS *param) /* =for apidoc newPADNAMEpvn -Constructs and returns a new pad name. I must be a UTF8 string. Do not +Constructs and returns a new pad name. C must be a UTF8 string. Do not use this for pad names that point to outer lexicals. See -L. +C>. =cut */ PADNAME * -Perl_newPADNAMEpvn(pTHX_ const char *s, STRLEN len) +Perl_newPADNAMEpvn(const char *s, STRLEN len) { struct padname_with_str *alloc; char *alloc2; /* for Newxz */ @@ -2747,15 +2814,15 @@ Perl_newPADNAMEpvn(pTHX_ const char *s, STRLEN len) =for apidoc newPADNAMEouter Constructs and returns a new pad name. Only use this function for names -that refer to outer lexicals. (See also L.) I is +that refer to outer lexicals. (See also L.) C is the outer pad name that this one mirrors. The returned pad name has the -PADNAMEt_OUTER flag already set. +C flag already set. =cut */ PADNAME * -Perl_newPADNAMEouter(pTHX_ PADNAME *outer) +Perl_newPADNAMEouter(PADNAME *outer) { PADNAME *pn; PERL_ARGS_ASSERT_NEWPADNAMEOUTER; @@ -2834,11 +2901,5 @@ Perl_padname_dup(pTHX_ PADNAME *src, CLONE_PARAMS *param) #endif /* USE_ITHREADS */ /* - * Local variables: - * c-indentation-style: bsd - * c-basic-offset: 4 - * indent-tabs-mode: nil - * End: - * * ex: set ts=8 sts=4 sw=4 et: */