Commit | Line | Data |
---|---|---|
dd2155a4 DM |
1 | /* pad.c |
2 | * | |
1129b882 NC |
3 | * Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008 |
4 | * by Larry Wall and others | |
dd2155a4 DM |
5 | * |
6 | * You may distribute under the terms of either the GNU General Public | |
7 | * License or the Artistic License, as specified in the README file. | |
4ac71550 TC |
8 | */ |
9 | ||
10 | /* | |
11 | * 'Anyway: there was this Mr. Frodo left an orphan and stranded, as you | |
12 | * might say, among those queer Bucklanders, being brought up anyhow in | |
13 | * Brandy Hall. A regular warren, by all accounts. Old Master Gorbadoc | |
14 | * never had fewer than a couple of hundred relations in the place. | |
15 | * Mr. Bilbo never did a kinder deed than when he brought the lad back | |
16 | * to live among decent folk.' --the Gaffer | |
dd2155a4 | 17 | * |
4ac71550 | 18 | * [p.23 of _The Lord of the Rings_, I/i: "A Long-Expected Party"] |
dd2155a4 DM |
19 | */ |
20 | ||
21 | /* XXX DAPM | |
22 | * As of Sept 2002, this file is new and may be in a state of flux for | |
23 | * a while. I've marked things I intent to come back and look at further | |
24 | * with an 'XXX DAPM' comment. | |
25 | */ | |
26 | ||
27 | /* | |
28 | =head1 Pad Data Structures | |
29 | ||
cc76b5cc | 30 | =for apidoc Amx|PADLIST *|CvPADLIST|CV *cv |
166f8a29 | 31 | |
58a9b2fe | 32 | CV's can have CvPADLIST(cv) set to point to a PADLIST. This is the CV's |
cc76b5cc Z |
33 | scratchpad, which stores lexical variables and opcode temporary and |
34 | per-thread values. | |
dd2155a4 | 35 | |
58a9b2fe | 36 | For these purposes "formats" are a kind-of CV; eval""s are too (except they're |
dd2155a4 | 37 | not callable at will and are always thrown away after the eval"" is done |
58a9b2fe | 38 | executing). Require'd files are simply evals without any outer lexical |
b5c19bd7 | 39 | scope. |
dd2155a4 | 40 | |
eacbb379 | 41 | XSUBs do not have a CvPADLIST. dXSTARG fetches values from PL_curpad, |
dd2155a4 | 42 | but that is really the callers pad (a slot of which is allocated by |
eacbb379 DD |
43 | every entersub). Do not get or set CvPADLIST if a CV is an XSUB (as |
44 | determined by C<CvISXSUB()>), CvPADLIST slot is reused for a different | |
45 | internal purpose in XSUBs. | |
dd2155a4 | 46 | |
58a9b2fe | 47 | The PADLIST has a C array where pads are stored. |
dd2155a4 | 48 | |
9b7476d7 FC |
49 | The 0th entry of the PADLIST is a PADNAMELIST |
50 | which represents the "names" or rather | |
58a9b2fe | 51 | the "static type information" for lexicals. The individual elements of a |
9b7476d7 | 52 | PADNAMELIST are PADNAMEs. Future |
7a5eb04d | 53 | refactorings might stop the PADNAMELIST from being stored in the PADLIST's |
86d2498c | 54 | array, so don't rely on it. See L</PadlistNAMES>. |
dd2155a4 | 55 | |
58a9b2fe FC |
56 | The CvDEPTH'th entry of a PADLIST is a PAD (an AV) which is the stack frame |
57 | at that depth of recursion into the CV. The 0th slot of a frame AV is an | |
58 | AV which is @_. Other entries are storage for variables and op targets. | |
dd2155a4 | 59 | |
58a9b2fe | 60 | Iterating over the PADNAMELIST iterates over all possible pad |
272ce8bb | 61 | items. Pad slots for targets (SVs_PADTMP) |
307a54be FC |
62 | and GVs end up having &PL_padname_undef "names", while slots for constants |
63 | have &PL_padname_const "names" (see pad_alloc()). That &PL_padname_undef | |
64 | and &PL_padname_const are used is an implementation detail subject to | |
65 | change. To test for them, use C<!PadnamePV(name)> and C<PadnamePV(name) | |
66 | && !PadnameLEN(name)>, respectively. | |
dd2155a4 | 67 | |
307a54be | 68 | Only my/our variable slots get valid names. |
dd2155a4 DM |
69 | The rest are op targets/GVs/constants which are statically allocated |
70 | or resolved at compile time. These don't have names by which they | |
58a9b2fe | 71 | can be looked up from Perl code at run time through eval"" the way |
dd2155a4 DM |
72 | my/our variables can be. Since they can't be looked up by "name" |
73 | but only by their index allocated at compile time (which is usually | |
74 | in PL_op->op_targ), wasting a name SV for them doesn't make sense. | |
75 | ||
307a54be FC |
76 | The pad names in the PADNAMELIST have their PV holding the name of |
77 | the variable. The COP_SEQ_RANGE_LOW and _HIGH fields form a range | |
78 | (low+1..high inclusive) of cop_seq numbers for which the name is | |
79 | valid. During compilation, these fields may hold the special value | |
61f4bfbf | 80 | PERL_PADSEQ_INTRO to indicate various stages: |
0d311cdb DM |
81 | |
82 | COP_SEQ_RANGE_LOW _HIGH | |
83 | ----------------- ----- | |
84 | PERL_PADSEQ_INTRO 0 variable not yet introduced: { my ($x | |
85 | valid-seq# PERL_PADSEQ_INTRO variable in scope: { my ($x) | |
86 | valid-seq# valid-seq# compilation of scope complete: { my ($x) } | |
87 | ||
307a54be FC |
88 | For typed lexicals PadnameTYPE points at the type stash. For C<our> |
89 | lexicals, PadnameOURSTASH points at the stash of the associated global (so | |
90 | that duplicate C<our> declarations in the same package can be detected). | |
91 | PadnameGEN is sometimes used to store the generation number during | |
92 | compilation. | |
93 | ||
94 | If PadnameOUTER is set on the pad name, then that slot in the frame AV | |
95 | is a REFCNT'ed reference to a lexical from "outside". Such entries | |
96 | are sometimes referred to as 'fake'. In this case, the name does not | |
97 | use 'low' and 'high' to store a cop_seq range, since it is in scope | |
98 | throughout. Instead 'high' stores some flags containing info about | |
b5c19bd7 | 99 | the real lexical (is it declared in an anon, and is it capable of being |
307a54be | 100 | instantiated multiple times?), and for fake ANONs, 'low' contains the index |
b5c19bd7 DM |
101 | within the parent's pad where the lexical's value is stored, to make |
102 | cloning quicker. | |
dd2155a4 | 103 | |
58a9b2fe | 104 | If the 'name' is '&' the corresponding entry in the PAD |
dd2155a4 | 105 | is a CV representing a possible closure. |
dd2155a4 | 106 | |
71f882da DM |
107 | Note that formats are treated as anon subs, and are cloned each time |
108 | write is called (if necessary). | |
109 | ||
ab8e66c1 | 110 | The flag SVs_PADSTALE is cleared on lexicals each time the my() is executed, |
58a9b2fe FC |
111 | and set on scope exit. This allows the |
112 | 'Variable $x is not available' warning | |
e6e7068b DM |
113 | to be generated in evals, such as |
114 | ||
115 | { my $x = 1; sub f { eval '$x'} } f(); | |
116 | ||
c9956dca FC |
117 | For state vars, SVs_PADSTALE is overloaded to mean 'not yet initialised', |
118 | but this internal state is stored in a separate pad entry. | |
d1186544 | 119 | |
36c300bb | 120 | =for apidoc AmxU|PADNAMELIST *|PL_comppad_name |
cc76b5cc Z |
121 | |
122 | During compilation, this points to the array containing the names part | |
123 | of the pad for the currently-compiling code. | |
124 | ||
36c300bb | 125 | =for apidoc AmxU|PAD *|PL_comppad |
cc76b5cc Z |
126 | |
127 | During compilation, this points to the array containing the values | |
128 | part of the pad for the currently-compiling code. (At runtime a CV may | |
129 | have many such value arrays; at compile time just one is constructed.) | |
130 | At runtime, this points to the array containing the currently-relevant | |
131 | values for the pad for the currently-executing code. | |
132 | ||
133 | =for apidoc AmxU|SV **|PL_curpad | |
134 | ||
135 | Points directly to the body of the L</PL_comppad> array. | |
c14a2249 | 136 | (I.e., this is C<PAD_ARRAY(PL_comppad)>.) |
cc76b5cc | 137 | |
dd2155a4 DM |
138 | =cut |
139 | */ | |
140 | ||
141 | ||
142 | #include "EXTERN.h" | |
143 | #define PERL_IN_PAD_C | |
144 | #include "perl.h" | |
952306ac | 145 | #include "keywords.h" |
dd2155a4 | 146 | |
3441fb63 | 147 | #define COP_SEQ_RANGE_LOW_set(sv,val) \ |
0f94cb1f | 148 | STMT_START { (sv)->xpadn_low = (val); } STMT_END |
3441fb63 | 149 | #define COP_SEQ_RANGE_HIGH_set(sv,val) \ |
0f94cb1f | 150 | STMT_START { (sv)->xpadn_high = (val); } STMT_END |
809abb02 | 151 | |
0f94cb1f FC |
152 | #define PARENT_PAD_INDEX_set COP_SEQ_RANGE_LOW_set |
153 | #define PARENT_FAKELEX_FLAGS_set COP_SEQ_RANGE_HIGH_set | |
dd2155a4 | 154 | |
eacbb379 DD |
155 | #ifdef DEBUGGING |
156 | void | |
e69651e7 | 157 | Perl_set_padlist(CV * cv, PADLIST *padlist){ |
eacbb379 DD |
158 | PERL_ARGS_ASSERT_SET_PADLIST; |
159 | # if PTRSIZE == 8 | |
e5964223 | 160 | assert((Size_t)padlist != UINT64_C(0xEFEFEFEFEFEFEFEF)); |
eacbb379 | 161 | # elif PTRSIZE == 4 |
e5964223 | 162 | assert((Size_t)padlist != UINT64_C(0xEFEFEFEF)); |
eacbb379 DD |
163 | # else |
164 | # error unknown pointer size | |
165 | # endif | |
e5964223 | 166 | assert(!CvISXSUB(cv)); |
eacbb379 DD |
167 | ((XPVCV*)MUTABLE_PTR(SvANY(cv)))->xcv_padlist_u.xcv_padlist = padlist; |
168 | } | |
169 | #endif | |
5b16296c BF |
170 | |
171 | /* | |
cc76b5cc | 172 | =for apidoc Am|PADLIST *|pad_new|int flags |
dd2155a4 | 173 | |
cc76b5cc Z |
174 | Create a new padlist, updating the global variables for the |
175 | currently-compiling padlist to point to the new padlist. The following | |
176 | flags can be OR'ed together: | |
dd2155a4 DM |
177 | |
178 | padnew_CLONE this pad is for a cloned CV | |
cc76b5cc | 179 | padnew_SAVE save old globals on the save stack |
dd2155a4 DM |
180 | padnew_SAVESUB also save extra stuff for start of sub |
181 | ||
182 | =cut | |
183 | */ | |
184 | ||
185 | PADLIST * | |
c7c737cb | 186 | Perl_pad_new(pTHX_ int flags) |
dd2155a4 | 187 | { |
7261499d | 188 | PADLIST *padlist; |
9b7476d7 FC |
189 | PADNAMELIST *padname; |
190 | PAD *pad; | |
7261499d | 191 | PAD **ary; |
dd2155a4 | 192 | |
f3548bdc DM |
193 | ASSERT_CURPAD_LEGAL("pad_new"); |
194 | ||
dd2155a4 DM |
195 | /* XXX DAPM really need a new SAVEt_PAD which restores all or most |
196 | * vars (based on flags) rather than storing vals + addresses for | |
197 | * each individually. Also see pad_block_start. | |
198 | * XXX DAPM Try to see whether all these conditionals are required | |
199 | */ | |
200 | ||
201 | /* save existing state, ... */ | |
202 | ||
203 | if (flags & padnew_SAVE) { | |
3979c56f | 204 | SAVECOMPPAD(); |
dd2155a4 | 205 | if (! (flags & padnew_CLONE)) { |
cbacc9aa | 206 | SAVESPTR(PL_comppad_name); |
dd2155a4 | 207 | SAVEI32(PL_padix); |
b54c5e14 | 208 | SAVEI32(PL_constpadix); |
dd2155a4 DM |
209 | SAVEI32(PL_comppad_name_fill); |
210 | SAVEI32(PL_min_intro_pending); | |
211 | SAVEI32(PL_max_intro_pending); | |
8bbe96d7 | 212 | SAVEBOOL(PL_cv_has_eval); |
dd2155a4 | 213 | if (flags & padnew_SAVESUB) { |
f0cb02e3 | 214 | SAVEBOOL(PL_pad_reset_pending); |
dd2155a4 DM |
215 | } |
216 | } | |
217 | } | |
218 | /* XXX DAPM interestingly, PL_comppad_name_floor never seems to be | |
219 | * saved - check at some pt that this is okay */ | |
220 | ||
221 | /* ... create new pad ... */ | |
222 | ||
7261499d | 223 | Newxz(padlist, 1, PADLIST); |
dd2155a4 DM |
224 | pad = newAV(); |
225 | ||
226 | if (flags & padnew_CLONE) { | |
227 | /* XXX DAPM I dont know why cv_clone needs it | |
228 | * doing differently yet - perhaps this separate branch can be | |
229 | * dispensed with eventually ??? | |
230 | */ | |
231 | ||
e1ec3a88 | 232 | AV * const a0 = newAV(); /* will be @_ */ |
ad64d0ec | 233 | av_store(pad, 0, MUTABLE_SV(a0)); |
11ca45c0 | 234 | AvREIFY_only(a0); |
9ef8d569 | 235 | |
9b7476d7 | 236 | PadnamelistREFCNT(padname = PL_comppad_name)++; |
dd2155a4 DM |
237 | } |
238 | else { | |
b4db5868 | 239 | padlist->xpadl_id = PL_padlist_generation++; |
a0714e2c | 240 | av_store(pad, 0, NULL); |
9b7476d7 | 241 | padname = newPADNAMELIST(0); |
0f94cb1f | 242 | padnamelist_store(padname, 0, &PL_padname_undef); |
dd2155a4 DM |
243 | } |
244 | ||
7a6072a8 NC |
245 | /* Most subroutines never recurse, hence only need 2 entries in the padlist |
246 | array - names, and depth=1. The default for av_store() is to allocate | |
247 | 0..3, and even an explicit call to av_extend() with <3 will be rounded | |
248 | up, so we inline the allocation of the array here. */ | |
7261499d | 249 | Newx(ary, 2, PAD *); |
86d2498c FC |
250 | PadlistMAX(padlist) = 1; |
251 | PadlistARRAY(padlist) = ary; | |
9b7476d7 | 252 | ary[0] = (PAD *)padname; |
7261499d | 253 | ary[1] = pad; |
dd2155a4 DM |
254 | |
255 | /* ... then update state variables */ | |
256 | ||
403799bf NC |
257 | PL_comppad = pad; |
258 | PL_curpad = AvARRAY(pad); | |
dd2155a4 DM |
259 | |
260 | if (! (flags & padnew_CLONE)) { | |
9ef8d569 | 261 | PL_comppad_name = padname; |
dd2155a4 DM |
262 | PL_comppad_name_fill = 0; |
263 | PL_min_intro_pending = 0; | |
264 | PL_padix = 0; | |
b54c5e14 | 265 | PL_constpadix = 0; |
b5c19bd7 | 266 | PL_cv_has_eval = 0; |
dd2155a4 DM |
267 | } |
268 | ||
269 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
b5c19bd7 | 270 | "Pad 0x%"UVxf"[0x%"UVxf"] new: compcv=0x%"UVxf |
dd2155a4 | 271 | " name=0x%"UVxf" flags=0x%"UVxf"\n", |
b5c19bd7 | 272 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), PTR2UV(PL_compcv), |
dd2155a4 DM |
273 | PTR2UV(padname), (UV)flags |
274 | ) | |
275 | ); | |
276 | ||
277 | return (PADLIST*)padlist; | |
278 | } | |
279 | ||
dd2155a4 | 280 | |
c4528262 NC |
281 | /* |
282 | =head1 Embedding Functions | |
283 | ||
284 | =for apidoc cv_undef | |
285 | ||
72d33970 | 286 | Clear out all the active components of a CV. This can happen either |
c4528262 NC |
287 | by an explicit C<undef &foo>, or by the reference count going to zero. |
288 | In the former case, we keep the CvOUTSIDE pointer, so that any anonymous | |
289 | children can still follow the full lexical scope chain. | |
290 | ||
291 | =cut | |
292 | */ | |
293 | ||
294 | void | |
295 | Perl_cv_undef(pTHX_ CV *cv) | |
296 | { | |
b7acb0a3 FC |
297 | PERL_ARGS_ASSERT_CV_UNDEF; |
298 | cv_undef_flags(cv, 0); | |
299 | } | |
300 | ||
301 | void | |
302 | Perl_cv_undef_flags(pTHX_ CV *cv, U32 flags) | |
303 | { | |
52ec28d5 DD |
304 | CV cvbody;/*CV body will never be realloced inside this func, |
305 | so dont read it more than once, use fake CV so existing macros | |
306 | will work, the indirection and CV head struct optimized away*/ | |
307 | SvANY(&cvbody) = SvANY(cv); | |
c4528262 | 308 | |
b7acb0a3 | 309 | PERL_ARGS_ASSERT_CV_UNDEF_FLAGS; |
c4528262 NC |
310 | |
311 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
312 | "CV undef: cv=0x%"UVxf" comppad=0x%"UVxf"\n", | |
313 | PTR2UV(cv), PTR2UV(PL_comppad)) | |
314 | ); | |
315 | ||
52ec28d5 DD |
316 | if (CvFILE(&cvbody)) { |
317 | char * file = CvFILE(&cvbody); | |
318 | CvFILE(&cvbody) = NULL; | |
319 | if(CvDYNFILE(&cvbody)) | |
320 | Safefree(file); | |
c4528262 | 321 | } |
dd2155a4 | 322 | |
52ec28d5 DD |
323 | /* CvSLABBED_off(&cvbody); *//* turned off below */ |
324 | /* release the sub's body */ | |
325 | if (!CvISXSUB(&cvbody)) { | |
326 | if(CvROOT(&cvbody)) { | |
327 | assert(SvTYPE(cv) == SVt_PVCV || SvTYPE(cv) == SVt_PVFM); /*unsafe is safe */ | |
328 | if (CvDEPTHunsafe(&cvbody)) { | |
329 | assert(SvTYPE(cv) == SVt_PVCV); | |
330 | Perl_croak_nocontext("Can't undef active subroutine"); | |
331 | } | |
332 | ENTER; | |
333 | ||
334 | PAD_SAVE_SETNULLPAD(); | |
335 | ||
336 | if (CvSLABBED(&cvbody)) OpslabREFCNT_dec_padok(OpSLAB(CvROOT(&cvbody))); | |
337 | op_free(CvROOT(&cvbody)); | |
338 | CvROOT(&cvbody) = NULL; | |
339 | CvSTART(&cvbody) = NULL; | |
340 | LEAVE; | |
341 | } | |
342 | else if (CvSLABBED(&cvbody)) { | |
343 | if( CvSTART(&cvbody)) { | |
344 | ENTER; | |
345 | PAD_SAVE_SETNULLPAD(); | |
346 | ||
347 | /* discard any leaked ops */ | |
348 | if (PL_parser) | |
349 | parser_free_nexttoke_ops(PL_parser, (OPSLAB *)CvSTART(&cvbody)); | |
350 | opslab_force_free((OPSLAB *)CvSTART(&cvbody)); | |
351 | CvSTART(&cvbody) = NULL; | |
352 | ||
353 | LEAVE; | |
354 | } | |
7aef8e5b | 355 | #ifdef DEBUGGING |
52ec28d5 | 356 | else Perl_warn(aTHX_ "Slab leaked from cv %p", (void*)cv); |
8be227ab | 357 | #endif |
52ec28d5 DD |
358 | } |
359 | } | |
360 | else { /* dont bother checking if CvXSUB(cv) is true, less branching */ | |
361 | CvXSUB(&cvbody) = NULL; | |
362 | } | |
c4528262 | 363 | SvPOK_off(MUTABLE_SV(cv)); /* forget prototype */ |
2f14e398 | 364 | sv_unmagic((SV *)cv, PERL_MAGIC_checkcall); |
b7acb0a3 | 365 | if (!(flags & CV_UNDEF_KEEP_NAME)) { |
52ec28d5 DD |
366 | if (CvNAMED(&cvbody)) { |
367 | CvNAME_HEK_set(&cvbody, NULL); | |
368 | CvNAMED_off(&cvbody); | |
b7acb0a3 FC |
369 | } |
370 | else CvGV_set(cv, NULL); | |
371 | } | |
c4528262 | 372 | |
c2736fce NC |
373 | /* This statement and the subsequence if block was pad_undef(). */ |
374 | pad_peg("pad_undef"); | |
375 | ||
eacbb379 | 376 | if (!CvISXSUB(&cvbody) && CvPADLIST(&cvbody)) { |
c2736fce | 377 | I32 ix; |
52ec28d5 | 378 | const PADLIST *padlist = CvPADLIST(&cvbody); |
c2736fce NC |
379 | |
380 | /* Free the padlist associated with a CV. | |
381 | If parts of it happen to be current, we null the relevant PL_*pad* | |
382 | global vars so that we don't have any dangling references left. | |
383 | We also repoint the CvOUTSIDE of any about-to-be-orphaned inner | |
384 | subs to the outer of this cv. */ | |
385 | ||
386 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
387 | "Pad undef: cv=0x%"UVxf" padlist=0x%"UVxf" comppad=0x%"UVxf"\n", | |
388 | PTR2UV(cv), PTR2UV(padlist), PTR2UV(PL_comppad)) | |
389 | ); | |
390 | ||
391 | /* detach any '&' anon children in the pad; if afterwards they | |
392 | * are still live, fix up their CvOUTSIDEs to point to our outside, | |
393 | * bypassing us. */ | |
394 | /* XXX DAPM for efficiency, we should only do this if we know we have | |
395 | * children, or integrate this loop with general cleanup */ | |
396 | ||
397 | if (PL_phase != PERL_PHASE_DESTRUCT) { /* don't bother during global destruction */ | |
52ec28d5 DD |
398 | CV * const outercv = CvOUTSIDE(&cvbody); |
399 | const U32 seq = CvOUTSIDE_SEQ(&cvbody); | |
9b7476d7 | 400 | PADNAMELIST * const comppad_name = PadlistNAMES(padlist); |
cab74fca | 401 | PADNAME ** const namepad = PadnamelistARRAY(comppad_name); |
86d2498c | 402 | PAD * const comppad = PadlistARRAY(padlist)[1]; |
c2736fce | 403 | SV ** const curpad = AvARRAY(comppad); |
9b7476d7 | 404 | for (ix = PadnamelistMAX(comppad_name); ix > 0; ix--) { |
cab74fca FC |
405 | PADNAME * const name = namepad[ix]; |
406 | if (name && PadnamePV(name) && *PadnamePV(name) == '&') | |
c2736fce NC |
407 | { |
408 | CV * const innercv = MUTABLE_CV(curpad[ix]); | |
409 | U32 inner_rc = SvREFCNT(innercv); | |
410 | assert(inner_rc); | |
e09ac076 | 411 | assert(SvTYPE(innercv) != SVt_PVFM); |
c2736fce NC |
412 | |
413 | if (SvREFCNT(comppad) < 2) { /* allow for /(?{ sub{} })/ */ | |
414 | curpad[ix] = NULL; | |
fc2b2dca | 415 | SvREFCNT_dec_NN(innercv); |
c2736fce NC |
416 | inner_rc--; |
417 | } | |
418 | ||
419 | /* in use, not just a prototype */ | |
b03210bf FC |
420 | if (inner_rc && SvTYPE(innercv) == SVt_PVCV |
421 | && (CvOUTSIDE(innercv) == cv)) | |
422 | { | |
c2736fce NC |
423 | assert(CvWEAKOUTSIDE(innercv)); |
424 | /* don't relink to grandfather if he's being freed */ | |
425 | if (outercv && SvREFCNT(outercv)) { | |
426 | CvWEAKOUTSIDE_off(innercv); | |
427 | CvOUTSIDE(innercv) = outercv; | |
428 | CvOUTSIDE_SEQ(innercv) = seq; | |
429 | SvREFCNT_inc_simple_void_NN(outercv); | |
430 | } | |
431 | else { | |
432 | CvOUTSIDE(innercv) = NULL; | |
433 | } | |
434 | } | |
435 | } | |
436 | } | |
437 | } | |
438 | ||
86d2498c | 439 | ix = PadlistMAX(padlist); |
aa2f79cf | 440 | while (ix > 0) { |
86d2498c | 441 | PAD * const sv = PadlistARRAY(padlist)[ix--]; |
c2736fce | 442 | if (sv) { |
7261499d | 443 | if (sv == PL_comppad) { |
c2736fce NC |
444 | PL_comppad = NULL; |
445 | PL_curpad = NULL; | |
446 | } | |
fc2b2dca | 447 | SvREFCNT_dec_NN(sv); |
c2736fce | 448 | } |
aa2f79cf NC |
449 | } |
450 | { | |
9b7476d7 FC |
451 | PADNAMELIST * const names = PadlistNAMES(padlist); |
452 | if (names == PL_comppad_name && PadnamelistREFCNT(names) == 1) | |
aa2f79cf | 453 | PL_comppad_name = NULL; |
9b7476d7 | 454 | PadnamelistREFCNT_dec(names); |
c2736fce | 455 | } |
86d2498c | 456 | if (PadlistARRAY(padlist)) Safefree(PadlistARRAY(padlist)); |
7261499d | 457 | Safefree(padlist); |
eacbb379 | 458 | CvPADLIST_set(&cvbody, NULL); |
c2736fce | 459 | } |
db6e00bd DD |
460 | else if (CvISXSUB(&cvbody)) |
461 | CvHSCXT(&cvbody) = NULL; | |
eacbb379 | 462 | /* else is (!CvISXSUB(&cvbody) && !CvPADLIST(&cvbody)) {do nothing;} */ |
c2736fce | 463 | |
c4528262 NC |
464 | |
465 | /* remove CvOUTSIDE unless this is an undef rather than a free */ | |
52ec28d5 DD |
466 | if (!SvREFCNT(cv)) { |
467 | CV * outside = CvOUTSIDE(&cvbody); | |
468 | if(outside) { | |
469 | CvOUTSIDE(&cvbody) = NULL; | |
470 | if (!CvWEAKOUTSIDE(&cvbody)) | |
471 | SvREFCNT_dec_NN(outside); | |
472 | } | |
c4528262 | 473 | } |
52ec28d5 DD |
474 | if (CvCONST(&cvbody)) { |
475 | SvREFCNT_dec(MUTABLE_SV(CvXSUBANY(&cvbody).any_ptr)); | |
476 | /* CvCONST_off(cv); *//* turned off below */ | |
c4528262 NC |
477 | } |
478 | /* delete all flags except WEAKOUTSIDE and CVGV_RC, which indicate the | |
b7acb0a3 FC |
479 | * ref status of CvOUTSIDE and CvGV, and ANON, NAMED and |
480 | * LEXICAL, which are used to determine the sub's name. */ | |
52ec28d5 | 481 | CvFLAGS(&cvbody) &= (CVf_WEAKOUTSIDE|CVf_CVGV_RC|CVf_ANON|CVf_LEXICAL |
b7acb0a3 | 482 | |CVf_NAMED); |
c4528262 | 483 | } |
dd2155a4 | 484 | |
50dc2bd3 FC |
485 | /* |
486 | =for apidoc cv_forget_slab | |
487 | ||
488 | When a CV has a reference count on its slab (CvSLABBED), it is responsible | |
489 | for making sure it is freed. (Hence, no two CVs should ever have a | |
490 | reference count on the same slab.) The CV only needs to reference the slab | |
491 | during compilation. Once it is compiled and CvROOT attached, it has | |
492 | finished its job, so it can forget the slab. | |
493 | ||
494 | =cut | |
495 | */ | |
496 | ||
8be227ab FC |
497 | void |
498 | Perl_cv_forget_slab(pTHX_ CV *cv) | |
499 | { | |
500 | const bool slabbed = !!CvSLABBED(cv); | |
3107b51f | 501 | OPSLAB *slab = NULL; |
8be227ab FC |
502 | |
503 | PERL_ARGS_ASSERT_CV_FORGET_SLAB; | |
504 | ||
505 | if (!slabbed) return; | |
506 | ||
507 | CvSLABBED_off(cv); | |
508 | ||
3107b51f FC |
509 | if (CvROOT(cv)) slab = OpSLAB(CvROOT(cv)); |
510 | else if (CvSTART(cv)) slab = (OPSLAB *)CvSTART(cv); | |
7aef8e5b | 511 | #ifdef DEBUGGING |
eb212a1c | 512 | else if (slabbed) Perl_warn(aTHX_ "Slab leaked from cv %p", (void*)cv); |
7aef8e5b | 513 | #endif |
3107b51f | 514 | |
3107b51f | 515 | if (slab) { |
f3e29105 NC |
516 | #ifdef PERL_DEBUG_READONLY_OPS |
517 | const size_t refcnt = slab->opslab_refcnt; | |
518 | #endif | |
3107b51f | 519 | OpslabREFCNT_dec(slab); |
f3e29105 | 520 | #ifdef PERL_DEBUG_READONLY_OPS |
3107b51f | 521 | if (refcnt > 1) Slab_to_ro(slab); |
8be227ab | 522 | #endif |
f3e29105 | 523 | } |
7aef8e5b | 524 | } |
8be227ab | 525 | |
cc76b5cc | 526 | /* |
307a54be | 527 | =for apidoc m|PADOFFSET|pad_alloc_name|PADNAME *name|U32 flags|HV *typestash|HV *ourstash |
cc76b5cc | 528 | |
1a115e49 FC |
529 | Allocates a place in the currently-compiling |
530 | pad (via L<perlapi/pad_alloc>) and | |
307a54be FC |
531 | then stores a name for that entry. I<name> is adopted and |
532 | becomes the name entry; it must already contain the name | |
533 | string. I<typestash> and I<ourstash> and the C<padadd_STATE> | |
534 | flag get added to I<name>. None of the other | |
1a115e49 | 535 | processing of L<perlapi/pad_add_name_pvn> |
cc76b5cc Z |
536 | is done. Returns the offset of the allocated pad slot. |
537 | ||
538 | =cut | |
539 | */ | |
540 | ||
3291825f | 541 | static PADOFFSET |
e1c02f84 FC |
542 | S_pad_alloc_name(pTHX_ PADNAME *name, U32 flags, HV *typestash, |
543 | HV *ourstash) | |
3291825f | 544 | { |
3291825f NC |
545 | const PADOFFSET offset = pad_alloc(OP_PADSV, SVs_PADMY); |
546 | ||
cc76b5cc | 547 | PERL_ARGS_ASSERT_PAD_ALLOC_NAME; |
3291825f | 548 | |
cc76b5cc | 549 | ASSERT_CURPAD_ACTIVE("pad_alloc_name"); |
3291825f NC |
550 | |
551 | if (typestash) { | |
e1c02f84 | 552 | SvPAD_TYPED_on(name); |
0f94cb1f FC |
553 | PadnameTYPE(name) = |
554 | MUTABLE_HV(SvREFCNT_inc_simple_NN(MUTABLE_SV(typestash))); | |
3291825f NC |
555 | } |
556 | if (ourstash) { | |
e1c02f84 FC |
557 | SvPAD_OUR_on(name); |
558 | SvOURSTASH_set(name, ourstash); | |
3291825f NC |
559 | SvREFCNT_inc_simple_void_NN(ourstash); |
560 | } | |
59cfed7d | 561 | else if (flags & padadd_STATE) { |
e1c02f84 | 562 | SvPAD_STATE_on(name); |
3291825f NC |
563 | } |
564 | ||
0f94cb1f | 565 | padnamelist_store(PL_comppad_name, offset, name); |
dd5d1b89 FC |
566 | if (PadnameLEN(name) > 1) |
567 | PadnamelistMAXNAMED(PL_comppad_name) = offset; | |
3291825f NC |
568 | return offset; |
569 | } | |
570 | ||
dd2155a4 | 571 | /* |
cc76b5cc | 572 | =for apidoc Am|PADOFFSET|pad_add_name_pvn|const char *namepv|STRLEN namelen|U32 flags|HV *typestash|HV *ourstash |
dd2155a4 | 573 | |
cc76b5cc Z |
574 | Allocates a place in the currently-compiling pad for a named lexical |
575 | variable. Stores the name and other metadata in the name part of the | |
576 | pad, and makes preparations to manage the variable's lexical scoping. | |
577 | Returns the offset of the allocated pad slot. | |
dd2155a4 | 578 | |
cc76b5cc Z |
579 | I<namepv>/I<namelen> specify the variable's name, including leading sigil. |
580 | If I<typestash> is non-null, the name is for a typed lexical, and this | |
581 | identifies the type. If I<ourstash> is non-null, it's a lexical reference | |
582 | to a package variable, and this identifies the package. The following | |
583 | flags can be OR'ed together: | |
584 | ||
585 | padadd_OUR redundantly specifies if it's a package var | |
586 | padadd_STATE variable will retain value persistently | |
587 | padadd_NO_DUP_CHECK skip check for lexical shadowing | |
dd2155a4 DM |
588 | |
589 | =cut | |
590 | */ | |
591 | ||
dd2155a4 | 592 | PADOFFSET |
cc76b5cc Z |
593 | Perl_pad_add_name_pvn(pTHX_ const char *namepv, STRLEN namelen, |
594 | U32 flags, HV *typestash, HV *ourstash) | |
dd2155a4 | 595 | { |
3291825f | 596 | PADOFFSET offset; |
e1c02f84 | 597 | PADNAME *name; |
dd2155a4 | 598 | |
cc76b5cc | 599 | PERL_ARGS_ASSERT_PAD_ADD_NAME_PVN; |
7918f24d | 600 | |
2502ffdf | 601 | if (flags & ~(padadd_OUR|padadd_STATE|padadd_NO_DUP_CHECK)) |
cc76b5cc | 602 | Perl_croak(aTHX_ "panic: pad_add_name_pvn illegal flag bits 0x%" UVxf, |
cca43f78 NC |
603 | (UV)flags); |
604 | ||
0f94cb1f | 605 | name = newPADNAMEpvn(namepv, namelen); |
dd2155a4 | 606 | |
59cfed7d | 607 | if ((flags & padadd_NO_DUP_CHECK) == 0) { |
c2b36a6d | 608 | ENTER; |
0f94cb1f | 609 | SAVEFREEPADNAME(name); /* in case of fatal warnings */ |
2d12d04f | 610 | /* check for duplicate declaration */ |
e1c02f84 | 611 | pad_check_dup(name, flags & padadd_OUR, ourstash); |
0f94cb1f | 612 | PadnameREFCNT(name)++; |
c2b36a6d | 613 | LEAVE; |
2d12d04f NC |
614 | } |
615 | ||
2502ffdf | 616 | offset = pad_alloc_name(name, flags, typestash, ourstash); |
3291825f NC |
617 | |
618 | /* not yet introduced */ | |
e1c02f84 FC |
619 | COP_SEQ_RANGE_LOW_set(name, PERL_PADSEQ_INTRO); |
620 | COP_SEQ_RANGE_HIGH_set(name, 0); | |
3291825f NC |
621 | |
622 | if (!PL_min_intro_pending) | |
623 | PL_min_intro_pending = offset; | |
624 | PL_max_intro_pending = offset; | |
625 | /* if it's not a simple scalar, replace with an AV or HV */ | |
c1bf42f3 NC |
626 | assert(SvTYPE(PL_curpad[offset]) == SVt_NULL); |
627 | assert(SvREFCNT(PL_curpad[offset]) == 1); | |
cc76b5cc | 628 | if (namelen != 0 && *namepv == '@') |
c1bf42f3 | 629 | sv_upgrade(PL_curpad[offset], SVt_PVAV); |
cc76b5cc | 630 | else if (namelen != 0 && *namepv == '%') |
c1bf42f3 | 631 | sv_upgrade(PL_curpad[offset], SVt_PVHV); |
6d5c2147 FC |
632 | else if (namelen != 0 && *namepv == '&') |
633 | sv_upgrade(PL_curpad[offset], SVt_PVCV); | |
c1bf42f3 | 634 | assert(SvPADMY(PL_curpad[offset])); |
3291825f NC |
635 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
636 | "Pad addname: %ld \"%s\" new lex=0x%"UVxf"\n", | |
e1c02f84 | 637 | (long)offset, PadnamePV(name), |
cc76b5cc | 638 | PTR2UV(PL_curpad[offset]))); |
dd2155a4 DM |
639 | |
640 | return offset; | |
641 | } | |
642 | ||
cc76b5cc Z |
643 | /* |
644 | =for apidoc Am|PADOFFSET|pad_add_name_pv|const char *name|U32 flags|HV *typestash|HV *ourstash | |
dd2155a4 | 645 | |
cc76b5cc Z |
646 | Exactly like L</pad_add_name_pvn>, but takes a nul-terminated string |
647 | instead of a string/length pair. | |
dd2155a4 | 648 | |
cc76b5cc Z |
649 | =cut |
650 | */ | |
651 | ||
652 | PADOFFSET | |
653 | Perl_pad_add_name_pv(pTHX_ const char *name, | |
0e1b3a4b | 654 | const U32 flags, HV *typestash, HV *ourstash) |
cc76b5cc Z |
655 | { |
656 | PERL_ARGS_ASSERT_PAD_ADD_NAME_PV; | |
657 | return pad_add_name_pvn(name, strlen(name), flags, typestash, ourstash); | |
658 | } | |
dd2155a4 DM |
659 | |
660 | /* | |
cc76b5cc | 661 | =for apidoc Am|PADOFFSET|pad_add_name_sv|SV *name|U32 flags|HV *typestash|HV *ourstash |
dd2155a4 | 662 | |
cc76b5cc Z |
663 | Exactly like L</pad_add_name_pvn>, but takes the name string in the form |
664 | of an SV instead of a string/length pair. | |
665 | ||
666 | =cut | |
667 | */ | |
668 | ||
669 | PADOFFSET | |
670 | Perl_pad_add_name_sv(pTHX_ SV *name, U32 flags, HV *typestash, HV *ourstash) | |
671 | { | |
672 | char *namepv; | |
673 | STRLEN namelen; | |
674 | PERL_ARGS_ASSERT_PAD_ADD_NAME_SV; | |
2502ffdf | 675 | namepv = SvPVutf8(name, namelen); |
cc76b5cc Z |
676 | return pad_add_name_pvn(namepv, namelen, flags, typestash, ourstash); |
677 | } | |
678 | ||
679 | /* | |
680 | =for apidoc Amx|PADOFFSET|pad_alloc|I32 optype|U32 tmptype | |
681 | ||
682 | Allocates a place in the currently-compiling pad, | |
683 | returning the offset of the allocated pad slot. | |
684 | No name is initially attached to the pad slot. | |
685 | I<tmptype> is a set of flags indicating the kind of pad entry required, | |
686 | which will be set in the value SV for the allocated pad entry: | |
687 | ||
688 | SVs_PADMY named lexical variable ("my", "our", "state") | |
689 | SVs_PADTMP unnamed temporary store | |
325e1816 FC |
690 | SVf_READONLY constant shared between recursion levels |
691 | ||
692 | C<SVf_READONLY> has been supported here only since perl 5.20. To work with | |
c370bd2e FC |
693 | earlier versions as well, use C<SVf_READONLY|SVs_PADTMP>. C<SVf_READONLY> |
694 | does not cause the SV in the pad slot to be marked read-only, but simply | |
695 | tells C<pad_alloc> that it I<will> be made read-only (by the caller), or at | |
696 | least should be treated as such. | |
cc76b5cc Z |
697 | |
698 | I<optype> should be an opcode indicating the type of operation that the | |
699 | pad entry is to support. This doesn't affect operational semantics, | |
700 | but is used for debugging. | |
dd2155a4 DM |
701 | |
702 | =cut | |
703 | */ | |
704 | ||
705 | /* XXX DAPM integrate alloc(), add_name() and add_anon(), | |
706 | * or at least rationalise ??? */ | |
dd2155a4 DM |
707 | |
708 | PADOFFSET | |
709 | Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype) | |
710 | { | |
711 | SV *sv; | |
712 | I32 retval; | |
713 | ||
6136c704 | 714 | PERL_UNUSED_ARG(optype); |
f3548bdc DM |
715 | ASSERT_CURPAD_ACTIVE("pad_alloc"); |
716 | ||
dd2155a4 | 717 | if (AvARRAY(PL_comppad) != PL_curpad) |
5637ef5b NC |
718 | Perl_croak(aTHX_ "panic: pad_alloc, %p!=%p", |
719 | AvARRAY(PL_comppad), PL_curpad); | |
dd2155a4 DM |
720 | if (PL_pad_reset_pending) |
721 | pad_reset(); | |
c0683843 | 722 | if (tmptype == SVs_PADMY) { /* Not & because this ‘flag’ is 0. */ |
cc76b5cc | 723 | /* For a my, simply push a null SV onto the end of PL_comppad. */ |
235cc2e3 | 724 | sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE); |
dd2155a4 DM |
725 | retval = AvFILLp(PL_comppad); |
726 | } | |
727 | else { | |
cc76b5cc Z |
728 | /* For a tmp, scan the pad from PL_padix upwards |
729 | * for a slot which has no name and no active value. | |
b54c5e14 | 730 | * For a constant, likewise, but use PL_constpadix. |
cc76b5cc | 731 | */ |
9b7476d7 FC |
732 | PADNAME * const * const names = PadnamelistARRAY(PL_comppad_name); |
733 | const SSize_t names_fill = PadnamelistMAX(PL_comppad_name); | |
b54c5e14 FC |
734 | const bool konst = cBOOL(tmptype & SVf_READONLY); |
735 | retval = konst ? PL_constpadix : PL_padix; | |
dd2155a4 DM |
736 | for (;;) { |
737 | /* | |
833bf1cd FC |
738 | * Entries that close over unavailable variables |
739 | * in outer subs contain values not marked PADMY. | |
740 | * Thus we must skip, not just pad values that are | |
dd2155a4 | 741 | * marked as current pad values, but also those with names. |
1780e744 FC |
742 | * If pad_reset is enabled, ‘current’ means different |
743 | * things depending on whether we are allocating a con- | |
744 | * stant or a target. For a target, things marked PADTMP | |
745 | * can be reused; not so for constants. | |
dd2155a4 | 746 | */ |
5e6246a7 | 747 | PADNAME *pn; |
b54c5e14 | 748 | if (++retval <= names_fill && |
5e6246a7 | 749 | (pn = names[retval]) && PadnamePV(pn)) |
dd2155a4 | 750 | continue; |
b54c5e14 | 751 | sv = *av_fetch(PL_comppad, retval, TRUE); |
a90643eb | 752 | if (!(SvFLAGS(sv) & |
53de1311 | 753 | #ifdef USE_PAD_RESET |
145bf8ee | 754 | (konst ? SVs_PADTMP : 0)) |
a90643eb | 755 | #else |
145bf8ee | 756 | SVs_PADTMP |
a90643eb | 757 | #endif |
13381c39 | 758 | )) |
dd2155a4 DM |
759 | break; |
760 | } | |
b54c5e14 | 761 | if (konst) { |
0f94cb1f | 762 | padnamelist_store(PL_comppad_name, retval, &PL_padname_const); |
325e1816 FC |
763 | tmptype &= ~SVf_READONLY; |
764 | tmptype |= SVs_PADTMP; | |
765 | } | |
b54c5e14 | 766 | *(konst ? &PL_constpadix : &PL_padix) = retval; |
dd2155a4 DM |
767 | } |
768 | SvFLAGS(sv) |= tmptype; | |
769 | PL_curpad = AvARRAY(PL_comppad); | |
770 | ||
771 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
772 | "Pad 0x%"UVxf"[0x%"UVxf"] alloc: %ld for %s\n", | |
773 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long) retval, | |
774 | PL_op_name[optype])); | |
fd0854ff DM |
775 | #ifdef DEBUG_LEAKING_SCALARS |
776 | sv->sv_debug_optype = optype; | |
777 | sv->sv_debug_inpad = 1; | |
fd0854ff | 778 | #endif |
a212c8b5 | 779 | return (PADOFFSET)retval; |
dd2155a4 DM |
780 | } |
781 | ||
782 | /* | |
cc76b5cc | 783 | =for apidoc Am|PADOFFSET|pad_add_anon|CV *func|I32 optype |
dd2155a4 | 784 | |
cc76b5cc Z |
785 | Allocates a place in the currently-compiling pad (via L</pad_alloc>) |
786 | for an anonymous function that is lexically scoped inside the | |
787 | currently-compiling function. | |
788 | The function I<func> is linked into the pad, and its C<CvOUTSIDE> link | |
789 | to the outer scope is weakened to avoid a reference loop. | |
790 | ||
84eea980 FC |
791 | One reference count is stolen, so you may need to do C<SvREFCNT_inc(func)>. |
792 | ||
cc76b5cc Z |
793 | I<optype> should be an opcode indicating the type of operation that the |
794 | pad entry is to support. This doesn't affect operational semantics, | |
795 | but is used for debugging. | |
dd2155a4 DM |
796 | |
797 | =cut | |
798 | */ | |
799 | ||
800 | PADOFFSET | |
cc76b5cc | 801 | Perl_pad_add_anon(pTHX_ CV* func, I32 optype) |
dd2155a4 DM |
802 | { |
803 | PADOFFSET ix; | |
0f94cb1f | 804 | PADNAME * const name = newPADNAMEpvn("&", 1); |
7918f24d NC |
805 | |
806 | PERL_ARGS_ASSERT_PAD_ADD_ANON; | |
74a9453a | 807 | assert (SvTYPE(func) == SVt_PVCV); |
7918f24d | 808 | |
1dba731d | 809 | pad_peg("add_anon"); |
0d311cdb | 810 | /* These two aren't used; just make sure they're not equal to |
0f94cb1f FC |
811 | * PERL_PADSEQ_INTRO. They should be 0 by default. */ |
812 | assert(COP_SEQ_RANGE_LOW (name) != PERL_PADSEQ_INTRO); | |
813 | assert(COP_SEQ_RANGE_HIGH(name) != PERL_PADSEQ_INTRO); | |
cc76b5cc | 814 | ix = pad_alloc(optype, SVs_PADMY); |
9b7476d7 | 815 | padnamelist_store(PL_comppad_name, ix, name); |
f3548bdc | 816 | /* XXX DAPM use PL_curpad[] ? */ |
74a9453a | 817 | av_store(PL_comppad, ix, (SV*)func); |
7dafbf52 DM |
818 | |
819 | /* to avoid ref loops, we never have parent + child referencing each | |
820 | * other simultaneously */ | |
74a9453a | 821 | if (CvOUTSIDE(func)) { |
cc76b5cc Z |
822 | assert(!CvWEAKOUTSIDE(func)); |
823 | CvWEAKOUTSIDE_on(func); | |
fc2b2dca | 824 | SvREFCNT_dec_NN(CvOUTSIDE(func)); |
7dafbf52 | 825 | } |
dd2155a4 DM |
826 | return ix; |
827 | } | |
828 | ||
a70f21d0 FC |
829 | void |
830 | Perl_pad_add_weakref(pTHX_ CV* func) | |
831 | { | |
832 | const PADOFFSET ix = pad_alloc(OP_NULL, SVs_PADMY); | |
833 | PADNAME * const name = newPADNAMEpvn("&", 1); | |
834 | SV * const rv = newRV_inc((SV *)func); | |
835 | ||
836 | PERL_ARGS_ASSERT_PAD_ADD_WEAKREF; | |
837 | ||
838 | /* These two aren't used; just make sure they're not equal to | |
839 | * PERL_PADSEQ_INTRO. They should be 0 by default. */ | |
840 | assert(COP_SEQ_RANGE_LOW (name) != PERL_PADSEQ_INTRO); | |
841 | assert(COP_SEQ_RANGE_HIGH(name) != PERL_PADSEQ_INTRO); | |
842 | padnamelist_store(PL_comppad_name, ix, name); | |
843 | sv_rvweaken(rv); | |
844 | av_store(PL_comppad, ix, rv); | |
845 | } | |
846 | ||
dd2155a4 | 847 | /* |
13087dd8 | 848 | =for apidoc pad_check_dup |
dd2155a4 DM |
849 | |
850 | Check for duplicate declarations: report any of: | |
13087dd8 | 851 | |
dd2155a4 | 852 | * a my in the current scope with the same name; |
13087dd8 FC |
853 | * an our (anywhere in the pad) with the same name and the |
854 | same stash as C<ourstash> | |
855 | ||
856 | C<is_our> indicates that the name to check is an 'our' declaration. | |
dd2155a4 DM |
857 | |
858 | =cut | |
859 | */ | |
860 | ||
20381b50 | 861 | STATIC void |
e1c02f84 | 862 | S_pad_check_dup(pTHX_ PADNAME *name, U32 flags, const HV *ourstash) |
dd2155a4 | 863 | { |
0aaff5a1 | 864 | PADNAME **svp; |
dd2155a4 | 865 | PADOFFSET top, off; |
59cfed7d | 866 | const U32 is_our = flags & padadd_OUR; |
dd2155a4 | 867 | |
7918f24d NC |
868 | PERL_ARGS_ASSERT_PAD_CHECK_DUP; |
869 | ||
f3548bdc | 870 | ASSERT_CURPAD_ACTIVE("pad_check_dup"); |
35f82371 | 871 | |
59cfed7d | 872 | assert((flags & ~padadd_OUR) == 0); |
35f82371 | 873 | |
9b7476d7 | 874 | if (PadnamelistMAX(PL_comppad_name) < 0 || !ckWARN(WARN_MISC)) |
dd2155a4 DM |
875 | return; /* nothing to check */ |
876 | ||
9b7476d7 FC |
877 | svp = PadnamelistARRAY(PL_comppad_name); |
878 | top = PadnamelistMAX(PL_comppad_name); | |
dd2155a4 DM |
879 | /* check the current scope */ |
880 | /* XXX DAPM - why the (I32) cast - shouldn't we ensure they're the same | |
881 | * type ? */ | |
882 | for (off = top; (I32)off > PL_comppad_name_floor; off--) { | |
0aaff5a1 | 883 | PADNAME * const sv = svp[off]; |
53c1dcc0 | 884 | if (sv |
0aaff5a1 FC |
885 | && PadnameLEN(sv) == PadnameLEN(name) |
886 | && !PadnameOUTER(sv) | |
0d311cdb DM |
887 | && ( COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO |
888 | || COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) | |
0aaff5a1 | 889 | && memEQ(PadnamePV(sv), PadnamePV(name), PadnameLEN(name))) |
dd2155a4 | 890 | { |
00b1698f | 891 | if (is_our && (SvPAD_OUR(sv))) |
7f73a9f1 | 892 | break; /* "our" masking "our" */ |
4eb94d7c | 893 | /* diag_listed_as: "%s" variable %s masks earlier declaration in same %s */ |
dd2155a4 | 894 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
0aaff5a1 | 895 | "\"%s\" %s %"PNf" masks earlier declaration in same %s", |
12bd6ede | 896 | (is_our ? "our" : PL_parser->in_my == KEY_my ? "my" : "state"), |
0aaff5a1 FC |
897 | *PadnamePV(sv) == '&' ? "subroutine" : "variable", |
898 | PNfARG(sv), | |
2df5bdd7 DM |
899 | (COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO |
900 | ? "scope" : "statement")); | |
dd2155a4 DM |
901 | --off; |
902 | break; | |
903 | } | |
904 | } | |
905 | /* check the rest of the pad */ | |
906 | if (is_our) { | |
61c5492a | 907 | while (off > 0) { |
0aaff5a1 | 908 | PADNAME * const sv = svp[off]; |
53c1dcc0 | 909 | if (sv |
0aaff5a1 FC |
910 | && PadnameLEN(sv) == PadnameLEN(name) |
911 | && !PadnameOUTER(sv) | |
0d311cdb DM |
912 | && ( COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO |
913 | || COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) | |
73d95100 | 914 | && SvOURSTASH(sv) == ourstash |
0aaff5a1 | 915 | && memEQ(PadnamePV(sv), PadnamePV(name), PadnameLEN(name))) |
dd2155a4 DM |
916 | { |
917 | Perl_warner(aTHX_ packWARN(WARN_MISC), | |
0aaff5a1 | 918 | "\"our\" variable %"PNf" redeclared", PNfARG(sv)); |
624f69f5 | 919 | if ((I32)off <= PL_comppad_name_floor) |
7f73a9f1 RGS |
920 | Perl_warner(aTHX_ packWARN(WARN_MISC), |
921 | "\t(Did you mean \"local\" instead of \"our\"?)\n"); | |
dd2155a4 DM |
922 | break; |
923 | } | |
61c5492a NC |
924 | --off; |
925 | } | |
dd2155a4 DM |
926 | } |
927 | } | |
928 | ||
929 | ||
dd2155a4 | 930 | /* |
cc76b5cc | 931 | =for apidoc Am|PADOFFSET|pad_findmy_pvn|const char *namepv|STRLEN namelen|U32 flags |
dd2155a4 | 932 | |
cc76b5cc Z |
933 | Given the name of a lexical variable, find its position in the |
934 | currently-compiling pad. | |
935 | I<namepv>/I<namelen> specify the variable's name, including leading sigil. | |
936 | I<flags> is reserved and must be zero. | |
937 | If it is not in the current pad but appears in the pad of any lexically | |
938 | enclosing scope, then a pseudo-entry for it is added in the current pad. | |
939 | Returns the offset in the current pad, | |
940 | or C<NOT_IN_PAD> if no such lexical is in scope. | |
dd2155a4 DM |
941 | |
942 | =cut | |
943 | */ | |
944 | ||
945 | PADOFFSET | |
cc76b5cc | 946 | Perl_pad_findmy_pvn(pTHX_ const char *namepv, STRLEN namelen, U32 flags) |
dd2155a4 | 947 | { |
e1c02f84 | 948 | PADNAME *out_pn; |
b5c19bd7 | 949 | int out_flags; |
929a0744 | 950 | I32 offset; |
9b7476d7 | 951 | const PADNAMELIST *namelist; |
e1c02f84 | 952 | PADNAME **name_p; |
dd2155a4 | 953 | |
cc76b5cc | 954 | PERL_ARGS_ASSERT_PAD_FINDMY_PVN; |
7918f24d | 955 | |
cc76b5cc | 956 | pad_peg("pad_findmy_pvn"); |
f8f98e0a | 957 | |
2502ffdf | 958 | if (flags) |
cc76b5cc | 959 | Perl_croak(aTHX_ "panic: pad_findmy_pvn illegal flag bits 0x%" UVxf, |
f8f98e0a NC |
960 | (UV)flags); |
961 | ||
fbb889c8 | 962 | offset = pad_findlex(namepv, namelen, flags, |
e1c02f84 | 963 | PL_compcv, PL_cop_seqmax, 1, NULL, &out_pn, &out_flags); |
9f7d9405 | 964 | if ((PADOFFSET)offset != NOT_IN_PAD) |
929a0744 DM |
965 | return offset; |
966 | ||
f0727190 FC |
967 | /* Skip the ‘our’ hack for subroutines, as the warning does not apply. |
968 | */ | |
969 | if (*namepv == '&') return NOT_IN_PAD; | |
970 | ||
929a0744 DM |
971 | /* look for an our that's being introduced; this allows |
972 | * our $foo = 0 unless defined $foo; | |
973 | * to not give a warning. (Yes, this is a hack) */ | |
974 | ||
9b7476d7 FC |
975 | namelist = PadlistNAMES(CvPADLIST(PL_compcv)); |
976 | name_p = PadnamelistARRAY(namelist); | |
977 | for (offset = PadnamelistMAXNAMED(namelist); offset > 0; offset--) { | |
e1c02f84 FC |
978 | const PADNAME * const name = name_p[offset]; |
979 | if (name && PadnameLEN(name) == namelen | |
980 | && !PadnameOUTER(name) | |
981 | && (PadnameIsOUR(name)) | |
2502ffdf FC |
982 | && ( PadnamePV(name) == namepv |
983 | || memEQ(PadnamePV(name), namepv, namelen) ) | |
e1c02f84 | 984 | && COP_SEQ_RANGE_LOW(name) == PERL_PADSEQ_INTRO |
929a0744 DM |
985 | ) |
986 | return offset; | |
987 | } | |
988 | return NOT_IN_PAD; | |
dd2155a4 DM |
989 | } |
990 | ||
e1f795dc | 991 | /* |
cc76b5cc Z |
992 | =for apidoc Am|PADOFFSET|pad_findmy_pv|const char *name|U32 flags |
993 | ||
994 | Exactly like L</pad_findmy_pvn>, but takes a nul-terminated string | |
995 | instead of a string/length pair. | |
996 | ||
997 | =cut | |
998 | */ | |
999 | ||
1000 | PADOFFSET | |
1001 | Perl_pad_findmy_pv(pTHX_ const char *name, U32 flags) | |
1002 | { | |
1003 | PERL_ARGS_ASSERT_PAD_FINDMY_PV; | |
1004 | return pad_findmy_pvn(name, strlen(name), flags); | |
1005 | } | |
1006 | ||
1007 | /* | |
1008 | =for apidoc Am|PADOFFSET|pad_findmy_sv|SV *name|U32 flags | |
1009 | ||
1010 | Exactly like L</pad_findmy_pvn>, but takes the name string in the form | |
1011 | of an SV instead of a string/length pair. | |
1012 | ||
1013 | =cut | |
1014 | */ | |
1015 | ||
1016 | PADOFFSET | |
1017 | Perl_pad_findmy_sv(pTHX_ SV *name, U32 flags) | |
1018 | { | |
1019 | char *namepv; | |
1020 | STRLEN namelen; | |
1021 | PERL_ARGS_ASSERT_PAD_FINDMY_SV; | |
2502ffdf | 1022 | namepv = SvPVutf8(name, namelen); |
cc76b5cc Z |
1023 | return pad_findmy_pvn(namepv, namelen, flags); |
1024 | } | |
1025 | ||
1026 | /* | |
1027 | =for apidoc Amp|PADOFFSET|find_rundefsvoffset | |
1028 | ||
1029 | Find the position of the lexical C<$_> in the pad of the | |
1030 | currently-executing function. Returns the offset in the current pad, | |
1031 | or C<NOT_IN_PAD> if there is no lexical C<$_> in scope (in which case | |
1032 | the global one should be used instead). | |
1033 | L</find_rundefsv> is likely to be more convenient. | |
1034 | ||
1035 | =cut | |
1036 | */ | |
e1f795dc RGS |
1037 | |
1038 | PADOFFSET | |
29289021 | 1039 | Perl_find_rundefsvoffset(pTHX) |
e1f795dc | 1040 | { |
e1c02f84 | 1041 | PADNAME *out_pn; |
e1f795dc | 1042 | int out_flags; |
fbb889c8 | 1043 | return pad_findlex("$_", 2, 0, find_runcv(NULL), PL_curcop->cop_seq, 1, |
e1c02f84 | 1044 | NULL, &out_pn, &out_flags); |
e1f795dc | 1045 | } |
dd2155a4 | 1046 | |
dd2155a4 | 1047 | /* |
cc76b5cc Z |
1048 | =for apidoc Am|SV *|find_rundefsv |
1049 | ||
1050 | Find and return the variable that is named C<$_> in the lexical scope | |
1051 | of the currently-executing function. This may be a lexical C<$_>, | |
1052 | or will otherwise be the global one. | |
1053 | ||
1054 | =cut | |
1055 | */ | |
789bd863 VP |
1056 | |
1057 | SV * | |
1058 | Perl_find_rundefsv(pTHX) | |
1059 | { | |
e1c02f84 | 1060 | PADNAME *name; |
789bd863 VP |
1061 | int flags; |
1062 | PADOFFSET po; | |
1063 | ||
fbb889c8 | 1064 | po = pad_findlex("$_", 2, 0, find_runcv(NULL), PL_curcop->cop_seq, 1, |
e1c02f84 | 1065 | NULL, &name, &flags); |
789bd863 | 1066 | |
e1c02f84 | 1067 | if (po == NOT_IN_PAD || PadnameIsOUR(name)) |
789bd863 VP |
1068 | return DEFSV; |
1069 | ||
1070 | return PAD_SVl(po); | |
1071 | } | |
1072 | ||
c086f97a FC |
1073 | SV * |
1074 | Perl_find_rundefsv2(pTHX_ CV *cv, U32 seq) | |
1075 | { | |
e1c02f84 | 1076 | PADNAME *name; |
c086f97a FC |
1077 | int flags; |
1078 | PADOFFSET po; | |
1079 | ||
1080 | PERL_ARGS_ASSERT_FIND_RUNDEFSV2; | |
1081 | ||
1082 | po = pad_findlex("$_", 2, 0, cv, seq, 1, | |
e1c02f84 | 1083 | NULL, &name, &flags); |
c086f97a | 1084 | |
e1c02f84 | 1085 | if (po == NOT_IN_PAD || PadnameIsOUR(name)) |
c086f97a FC |
1086 | return DEFSV; |
1087 | ||
86d2498c | 1088 | return AvARRAY(PadlistARRAY(CvPADLIST(cv))[CvDEPTH(cv)])[po]; |
c086f97a FC |
1089 | } |
1090 | ||
789bd863 | 1091 | /* |
e1c02f84 | 1092 | =for apidoc m|PADOFFSET|pad_findlex|const char *namepv|STRLEN namelen|U32 flags|const CV* cv|U32 seq|int warn|SV** out_capture|PADNAME** out_name|int *out_flags |
dd2155a4 | 1093 | |
72d33970 | 1094 | Find a named lexical anywhere in a chain of nested pads. Add fake entries |
b5c19bd7 DM |
1095 | in the inner pads if it's found in an outer one. |
1096 | ||
1097 | Returns the offset in the bottom pad of the lex or the fake lex. | |
1098 | cv is the CV in which to start the search, and seq is the current cop_seq | |
72d33970 | 1099 | to match against. If warn is true, print appropriate warnings. The out_* |
b5c19bd7 | 1100 | vars return values, and so are pointers to where the returned values |
72d33970 | 1101 | should be stored. out_capture, if non-null, requests that the innermost |
e1c02f84 | 1102 | instance of the lexical is captured; out_name is set to the innermost |
307a54be FC |
1103 | matched pad name or fake pad name; out_flags returns the flags normally |
1104 | associated with the PARENT_FAKELEX_FLAGS field of a fake pad name. | |
b5c19bd7 DM |
1105 | |
1106 | Note that pad_findlex() is recursive; it recurses up the chain of CVs, | |
72d33970 FC |
1107 | then comes back down, adding fake entries |
1108 | as it goes. It has to be this way | |
307a54be | 1109 | because fake names in anon protoypes have to store in xlow the index into |
b5c19bd7 | 1110 | the parent pad. |
dd2155a4 DM |
1111 | |
1112 | =cut | |
1113 | */ | |
1114 | ||
b5c19bd7 DM |
1115 | /* the CV has finished being compiled. This is not a sufficient test for |
1116 | * all CVs (eg XSUBs), but suffices for the CVs found in a lexical chain */ | |
1117 | #define CvCOMPILED(cv) CvROOT(cv) | |
1118 | ||
71f882da | 1119 | /* the CV does late binding of its lexicals */ |
e07561e6 | 1120 | #define CvLATE(cv) (CvANON(cv) || CvCLONE(cv) || SvTYPE(cv) == SVt_PVFM) |
71f882da | 1121 | |
445f13ff | 1122 | static void |
e6df7a56 | 1123 | S_unavailable(pTHX_ PADNAME *name) |
445f13ff FC |
1124 | { |
1125 | /* diag_listed_as: Variable "%s" is not available */ | |
1126 | Perl_ck_warner(aTHX_ packWARN(WARN_CLOSURE), | |
e6df7a56 FC |
1127 | "%se \"%"PNf"\" is not available", |
1128 | *PadnamePV(name) == '&' | |
445f13ff FC |
1129 | ? "Subroutin" |
1130 | : "Variabl", | |
e6df7a56 | 1131 | PNfARG(name)); |
445f13ff | 1132 | } |
b5c19bd7 | 1133 | |
dd2155a4 | 1134 | STATIC PADOFFSET |
fbb889c8 | 1135 | S_pad_findlex(pTHX_ const char *namepv, STRLEN namelen, U32 flags, const CV* cv, U32 seq, |
e1c02f84 | 1136 | int warn, SV** out_capture, PADNAME** out_name, int *out_flags) |
dd2155a4 | 1137 | { |
b5c19bd7 DM |
1138 | I32 offset, new_offset; |
1139 | SV *new_capture; | |
1140 | SV **new_capturep; | |
b70d5558 | 1141 | const PADLIST * const padlist = CvPADLIST(cv); |
7ef30830 | 1142 | const bool staleok = !!(flags & padadd_STALEOK); |
dd2155a4 | 1143 | |
7918f24d NC |
1144 | PERL_ARGS_ASSERT_PAD_FINDLEX; |
1145 | ||
2502ffdf FC |
1146 | flags &= ~ padadd_STALEOK; /* one-shot flag */ |
1147 | if (flags) | |
2435e5d3 BF |
1148 | Perl_croak(aTHX_ "panic: pad_findlex illegal flag bits 0x%" UVxf, |
1149 | (UV)flags); | |
1150 | ||
b5c19bd7 | 1151 | *out_flags = 0; |
a3985cdc | 1152 | |
b5c19bd7 | 1153 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
cc76b5cc | 1154 | "Pad findlex cv=0x%"UVxf" searching \"%.*s\" seq=%d%s\n", |
4c760560 | 1155 | PTR2UV(cv), (int)namelen, namepv, (int)seq, |
cc76b5cc | 1156 | out_capture ? " capturing" : "" )); |
dd2155a4 | 1157 | |
b5c19bd7 | 1158 | /* first, search this pad */ |
dd2155a4 | 1159 | |
b5c19bd7 DM |
1160 | if (padlist) { /* not an undef CV */ |
1161 | I32 fake_offset = 0; | |
9b7476d7 FC |
1162 | const PADNAMELIST * const names = PadlistNAMES(padlist); |
1163 | PADNAME * const * const name_p = PadnamelistARRAY(names); | |
ee6cee0c | 1164 | |
9b7476d7 | 1165 | for (offset = PadnamelistMAXNAMED(names); offset > 0; offset--) { |
e1c02f84 FC |
1166 | const PADNAME * const name = name_p[offset]; |
1167 | if (name && PadnameLEN(name) == namelen | |
2502ffdf FC |
1168 | && ( PadnamePV(name) == namepv |
1169 | || memEQ(PadnamePV(name), namepv, namelen) )) | |
b5c19bd7 | 1170 | { |
e1c02f84 | 1171 | if (PadnameOUTER(name)) { |
b5c19bd7 | 1172 | fake_offset = offset; /* in case we don't find a real one */ |
6012dc80 DM |
1173 | continue; |
1174 | } | |
e1c02f84 | 1175 | if (PadnameIN_SCOPE(name, seq)) |
03414f05 | 1176 | break; |
ee6cee0c DM |
1177 | } |
1178 | } | |
1179 | ||
b5c19bd7 DM |
1180 | if (offset > 0 || fake_offset > 0 ) { /* a match! */ |
1181 | if (offset > 0) { /* not fake */ | |
1182 | fake_offset = 0; | |
e1c02f84 | 1183 | *out_name = name_p[offset]; /* return the name */ |
b5c19bd7 DM |
1184 | |
1185 | /* set PAD_FAKELEX_MULTI if this lex can have multiple | |
1186 | * instances. For now, we just test !CvUNIQUE(cv), but | |
1187 | * ideally, we should detect my's declared within loops | |
1188 | * etc - this would allow a wider range of 'not stayed | |
486ec47a | 1189 | * shared' warnings. We also treated already-compiled |
b5c19bd7 DM |
1190 | * lexes as not multi as viewed from evals. */ |
1191 | ||
1192 | *out_flags = CvANON(cv) ? | |
1193 | PAD_FAKELEX_ANON : | |
1194 | (!CvUNIQUE(cv) && ! CvCOMPILED(cv)) | |
1195 | ? PAD_FAKELEX_MULTI : 0; | |
1196 | ||
1197 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
809abb02 NC |
1198 | "Pad findlex cv=0x%"UVxf" matched: offset=%ld (%lu,%lu)\n", |
1199 | PTR2UV(cv), (long)offset, | |
e1c02f84 FC |
1200 | (unsigned long)COP_SEQ_RANGE_LOW(*out_name), |
1201 | (unsigned long)COP_SEQ_RANGE_HIGH(*out_name))); | |
b5c19bd7 DM |
1202 | } |
1203 | else { /* fake match */ | |
1204 | offset = fake_offset; | |
e1c02f84 FC |
1205 | *out_name = name_p[offset]; /* return the name */ |
1206 | *out_flags = PARENT_FAKELEX_FLAGS(*out_name); | |
b5c19bd7 | 1207 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
19a5c512 | 1208 | "Pad findlex cv=0x%"UVxf" matched: offset=%ld flags=0x%lx index=%lu\n", |
b5c19bd7 | 1209 | PTR2UV(cv), (long)offset, (unsigned long)*out_flags, |
e1c02f84 | 1210 | (unsigned long) PARENT_PAD_INDEX(*out_name) |
b5c19bd7 DM |
1211 | )); |
1212 | } | |
dd2155a4 | 1213 | |
b5c19bd7 | 1214 | /* return the lex? */ |
dd2155a4 | 1215 | |
b5c19bd7 | 1216 | if (out_capture) { |
dd2155a4 | 1217 | |
b5c19bd7 | 1218 | /* our ? */ |
e1c02f84 | 1219 | if (PadnameIsOUR(*out_name)) { |
a0714e2c | 1220 | *out_capture = NULL; |
b5c19bd7 DM |
1221 | return offset; |
1222 | } | |
ee6cee0c | 1223 | |
b5c19bd7 DM |
1224 | /* trying to capture from an anon prototype? */ |
1225 | if (CvCOMPILED(cv) | |
1226 | ? CvANON(cv) && CvCLONE(cv) && !CvCLONED(cv) | |
1227 | : *out_flags & PAD_FAKELEX_ANON) | |
1228 | { | |
a2a5de95 | 1229 | if (warn) |
445f13ff | 1230 | S_unavailable(aTHX_ |
f5658c36 | 1231 | *out_name); |
0727928e | 1232 | |
a0714e2c | 1233 | *out_capture = NULL; |
b5c19bd7 | 1234 | } |
ee6cee0c | 1235 | |
b5c19bd7 DM |
1236 | /* real value */ |
1237 | else { | |
1238 | int newwarn = warn; | |
1239 | if (!CvCOMPILED(cv) && (*out_flags & PAD_FAKELEX_MULTI) | |
e1c02f84 | 1240 | && !PadnameIsSTATE(name_p[offset]) |
b5c19bd7 DM |
1241 | && warn && ckWARN(WARN_CLOSURE)) { |
1242 | newwarn = 0; | |
2a9203e9 FC |
1243 | /* diag_listed_as: Variable "%s" will not stay |
1244 | shared */ | |
b5c19bd7 | 1245 | Perl_warner(aTHX_ packWARN(WARN_CLOSURE), |
2a9203e9 FC |
1246 | "%se \"%"UTF8f"\" will not stay shared", |
1247 | *namepv == '&' ? "Subroutin" : "Variabl", | |
8d98b5bc | 1248 | UTF8fARG(1, namelen, namepv)); |
b5c19bd7 | 1249 | } |
dd2155a4 | 1250 | |
b5c19bd7 DM |
1251 | if (fake_offset && CvANON(cv) |
1252 | && CvCLONE(cv) &&!CvCLONED(cv)) | |
1253 | { | |
e1c02f84 | 1254 | PADNAME *n; |
b5c19bd7 DM |
1255 | /* not yet caught - look further up */ |
1256 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
1257 | "Pad findlex cv=0x%"UVxf" chasing lex in outer pad\n", | |
1258 | PTR2UV(cv))); | |
e1c02f84 | 1259 | n = *out_name; |
fbb889c8 | 1260 | (void) pad_findlex(namepv, namelen, flags, CvOUTSIDE(cv), |
282e1742 | 1261 | CvOUTSIDE_SEQ(cv), |
e1c02f84 FC |
1262 | newwarn, out_capture, out_name, out_flags); |
1263 | *out_name = n; | |
b5c19bd7 | 1264 | return offset; |
dd2155a4 | 1265 | } |
b5c19bd7 | 1266 | |
86d2498c | 1267 | *out_capture = AvARRAY(PadlistARRAY(padlist)[ |
7261499d | 1268 | CvDEPTH(cv) ? CvDEPTH(cv) : 1])[offset]; |
b5c19bd7 DM |
1269 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
1270 | "Pad findlex cv=0x%"UVxf" found lex=0x%"UVxf"\n", | |
19a5c512 | 1271 | PTR2UV(cv), PTR2UV(*out_capture))); |
b5c19bd7 | 1272 | |
d1186544 | 1273 | if (SvPADSTALE(*out_capture) |
7ef30830 | 1274 | && (!CvDEPTH(cv) || !staleok) |
e1c02f84 | 1275 | && !PadnameIsSTATE(name_p[offset])) |
d1186544 | 1276 | { |
445f13ff | 1277 | S_unavailable(aTHX_ |
f5658c36 | 1278 | name_p[offset]); |
a0714e2c | 1279 | *out_capture = NULL; |
dd2155a4 DM |
1280 | } |
1281 | } | |
b5c19bd7 | 1282 | if (!*out_capture) { |
cc76b5cc | 1283 | if (namelen != 0 && *namepv == '@') |
ad64d0ec | 1284 | *out_capture = sv_2mortal(MUTABLE_SV(newAV())); |
cc76b5cc | 1285 | else if (namelen != 0 && *namepv == '%') |
ad64d0ec | 1286 | *out_capture = sv_2mortal(MUTABLE_SV(newHV())); |
6d5c2147 FC |
1287 | else if (namelen != 0 && *namepv == '&') |
1288 | *out_capture = sv_2mortal(newSV_type(SVt_PVCV)); | |
b5c19bd7 DM |
1289 | else |
1290 | *out_capture = sv_newmortal(); | |
1291 | } | |
dd2155a4 | 1292 | } |
b5c19bd7 DM |
1293 | |
1294 | return offset; | |
ee6cee0c | 1295 | } |
b5c19bd7 DM |
1296 | } |
1297 | ||
1298 | /* it's not in this pad - try above */ | |
1299 | ||
1300 | if (!CvOUTSIDE(cv)) | |
1301 | return NOT_IN_PAD; | |
9f7d9405 | 1302 | |
b5c19bd7 | 1303 | /* out_capture non-null means caller wants us to capture lex; in |
71f882da | 1304 | * addition we capture ourselves unless it's an ANON/format */ |
b5c19bd7 | 1305 | new_capturep = out_capture ? out_capture : |
4608196e | 1306 | CvLATE(cv) ? NULL : &new_capture; |
b5c19bd7 | 1307 | |
7ef30830 FC |
1308 | offset = pad_findlex(namepv, namelen, |
1309 | flags | padadd_STALEOK*(new_capturep == &new_capture), | |
1310 | CvOUTSIDE(cv), CvOUTSIDE_SEQ(cv), 1, | |
e1c02f84 | 1311 | new_capturep, out_name, out_flags); |
9f7d9405 | 1312 | if ((PADOFFSET)offset == NOT_IN_PAD) |
b5c19bd7 | 1313 | return NOT_IN_PAD; |
9f7d9405 | 1314 | |
b5c19bd7 DM |
1315 | /* found in an outer CV. Add appropriate fake entry to this pad */ |
1316 | ||
1317 | /* don't add new fake entries (via eval) to CVs that we have already | |
1318 | * finished compiling, or to undef CVs */ | |
1319 | if (CvCOMPILED(cv) || !padlist) | |
1320 | return 0; /* this dummy (and invalid) value isnt used by the caller */ | |
1321 | ||
1322 | { | |
0f94cb1f | 1323 | PADNAME *new_name = newPADNAMEouter(*out_name); |
9b7476d7 | 1324 | PADNAMELIST * const ocomppad_name = PL_comppad_name; |
53c1dcc0 | 1325 | PAD * const ocomppad = PL_comppad; |
9b7476d7 | 1326 | PL_comppad_name = PadlistNAMES(padlist); |
86d2498c | 1327 | PL_comppad = PadlistARRAY(padlist)[1]; |
b5c19bd7 DM |
1328 | PL_curpad = AvARRAY(PL_comppad); |
1329 | ||
3291825f | 1330 | new_offset |
e1c02f84 FC |
1331 | = pad_alloc_name(new_name, |
1332 | PadnameIsSTATE(*out_name) ? padadd_STATE : 0, | |
1333 | PadnameTYPE(*out_name), | |
1334 | PadnameOURSTASH(*out_name) | |
3291825f NC |
1335 | ); |
1336 | ||
3291825f NC |
1337 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
1338 | "Pad addname: %ld \"%.*s\" FAKE\n", | |
1339 | (long)new_offset, | |
e1c02f84 FC |
1340 | (int) PadnameLEN(new_name), |
1341 | PadnamePV(new_name))); | |
1342 | PARENT_FAKELEX_FLAGS_set(new_name, *out_flags); | |
b5c19bd7 | 1343 | |
e1c02f84 FC |
1344 | PARENT_PAD_INDEX_set(new_name, 0); |
1345 | if (PadnameIsOUR(new_name)) { | |
6f207bd3 | 1346 | NOOP; /* do nothing */ |
b5c19bd7 | 1347 | } |
71f882da | 1348 | else if (CvLATE(cv)) { |
b5c19bd7 | 1349 | /* delayed creation - just note the offset within parent pad */ |
e1c02f84 | 1350 | PARENT_PAD_INDEX_set(new_name, offset); |
b5c19bd7 DM |
1351 | CvCLONE_on(cv); |
1352 | } | |
1353 | else { | |
1354 | /* immediate creation - capture outer value right now */ | |
1355 | av_store(PL_comppad, new_offset, SvREFCNT_inc(*new_capturep)); | |
81df9f6f | 1356 | /* But also note the offset, as newMYSUB needs it */ |
e1c02f84 | 1357 | PARENT_PAD_INDEX_set(new_name, offset); |
b5c19bd7 DM |
1358 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
1359 | "Pad findlex cv=0x%"UVxf" saved captured sv 0x%"UVxf" at offset %ld\n", | |
1360 | PTR2UV(cv), PTR2UV(*new_capturep), (long)new_offset)); | |
dd2155a4 | 1361 | } |
e1c02f84 FC |
1362 | *out_name = new_name; |
1363 | *out_flags = PARENT_FAKELEX_FLAGS(new_name); | |
b5c19bd7 DM |
1364 | |
1365 | PL_comppad_name = ocomppad_name; | |
1366 | PL_comppad = ocomppad; | |
4608196e | 1367 | PL_curpad = ocomppad ? AvARRAY(ocomppad) : NULL; |
dd2155a4 | 1368 | } |
b5c19bd7 | 1369 | return new_offset; |
dd2155a4 DM |
1370 | } |
1371 | ||
fb8a9836 | 1372 | #ifdef DEBUGGING |
cc76b5cc | 1373 | |
dd2155a4 | 1374 | /* |
cc76b5cc | 1375 | =for apidoc Am|SV *|pad_sv|PADOFFSET po |
dd2155a4 | 1376 | |
cc76b5cc | 1377 | Get the value at offset I<po> in the current (compiling or executing) pad. |
dd2155a4 DM |
1378 | Use macro PAD_SV instead of calling this function directly. |
1379 | ||
1380 | =cut | |
1381 | */ | |
1382 | ||
dd2155a4 DM |
1383 | SV * |
1384 | Perl_pad_sv(pTHX_ PADOFFSET po) | |
1385 | { | |
f3548bdc | 1386 | ASSERT_CURPAD_ACTIVE("pad_sv"); |
dd2155a4 | 1387 | |
dd2155a4 DM |
1388 | if (!po) |
1389 | Perl_croak(aTHX_ "panic: pad_sv po"); | |
dd2155a4 DM |
1390 | DEBUG_X(PerlIO_printf(Perl_debug_log, |
1391 | "Pad 0x%"UVxf"[0x%"UVxf"] sv: %ld sv=0x%"UVxf"\n", | |
f3548bdc | 1392 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(PL_curpad[po])) |
dd2155a4 DM |
1393 | ); |
1394 | return PL_curpad[po]; | |
1395 | } | |
1396 | ||
dd2155a4 | 1397 | /* |
cc76b5cc | 1398 | =for apidoc Am|void|pad_setsv|PADOFFSET po|SV *sv |
dd2155a4 | 1399 | |
cc76b5cc | 1400 | Set the value at offset I<po> in the current (compiling or executing) pad. |
dd2155a4 DM |
1401 | Use the macro PAD_SETSV() rather than calling this function directly. |
1402 | ||
1403 | =cut | |
1404 | */ | |
1405 | ||
dd2155a4 DM |
1406 | void |
1407 | Perl_pad_setsv(pTHX_ PADOFFSET po, SV* sv) | |
1408 | { | |
7918f24d NC |
1409 | PERL_ARGS_ASSERT_PAD_SETSV; |
1410 | ||
f3548bdc | 1411 | ASSERT_CURPAD_ACTIVE("pad_setsv"); |
dd2155a4 DM |
1412 | |
1413 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
1414 | "Pad 0x%"UVxf"[0x%"UVxf"] setsv: %ld sv=0x%"UVxf"\n", | |
f3548bdc | 1415 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po, PTR2UV(sv)) |
dd2155a4 DM |
1416 | ); |
1417 | PL_curpad[po] = sv; | |
1418 | } | |
dd2155a4 | 1419 | |
cc76b5cc | 1420 | #endif /* DEBUGGING */ |
dd2155a4 DM |
1421 | |
1422 | /* | |
cc76b5cc | 1423 | =for apidoc m|void|pad_block_start|int full |
dd2155a4 | 1424 | |
e89fca5e | 1425 | Update the pad compilation state variables on entry to a new block. |
dd2155a4 DM |
1426 | |
1427 | =cut | |
1428 | */ | |
1429 | ||
1430 | /* XXX DAPM perhaps: | |
1431 | * - integrate this in general state-saving routine ??? | |
1432 | * - combine with the state-saving going on in pad_new ??? | |
1433 | * - introduce a new SAVE type that does all this in one go ? | |
1434 | */ | |
1435 | ||
1436 | void | |
1437 | Perl_pad_block_start(pTHX_ int full) | |
1438 | { | |
f3548bdc | 1439 | ASSERT_CURPAD_ACTIVE("pad_block_start"); |
dd2155a4 | 1440 | SAVEI32(PL_comppad_name_floor); |
9b7476d7 | 1441 | PL_comppad_name_floor = PadnamelistMAX(PL_comppad_name); |
dd2155a4 DM |
1442 | if (full) |
1443 | PL_comppad_name_fill = PL_comppad_name_floor; | |
1444 | if (PL_comppad_name_floor < 0) | |
1445 | PL_comppad_name_floor = 0; | |
1446 | SAVEI32(PL_min_intro_pending); | |
1447 | SAVEI32(PL_max_intro_pending); | |
1448 | PL_min_intro_pending = 0; | |
1449 | SAVEI32(PL_comppad_name_fill); | |
1450 | SAVEI32(PL_padix_floor); | |
1780e744 FC |
1451 | /* PL_padix_floor is what PL_padix is reset to at the start of each |
1452 | statement, by pad_reset(). We set it when entering a new scope | |
1453 | to keep things like this working: | |
1454 | print "$foo$bar", do { this(); that() . "foo" }; | |
1455 | We must not let "$foo$bar" and the later concatenation share the | |
1456 | same target. */ | |
dd2155a4 DM |
1457 | PL_padix_floor = PL_padix; |
1458 | PL_pad_reset_pending = FALSE; | |
1459 | } | |
1460 | ||
dd2155a4 | 1461 | /* |
25f5d540 | 1462 | =for apidoc Am|U32|intro_my |
dd2155a4 | 1463 | |
25f5d540 LM |
1464 | "Introduce" C<my> variables to visible status. This is called during parsing |
1465 | at the end of each statement to make lexical variables visible to subsequent | |
1466 | statements. | |
dd2155a4 DM |
1467 | |
1468 | =cut | |
1469 | */ | |
1470 | ||
1471 | U32 | |
1472 | Perl_intro_my(pTHX) | |
1473 | { | |
6a0435be | 1474 | PADNAME **svp; |
dd2155a4 | 1475 | I32 i; |
6012dc80 | 1476 | U32 seq; |
dd2155a4 | 1477 | |
f3548bdc | 1478 | ASSERT_CURPAD_ACTIVE("intro_my"); |
8635e3c2 FC |
1479 | if (PL_compiling.cop_seq) { |
1480 | seq = PL_compiling.cop_seq; | |
1481 | PL_compiling.cop_seq = 0; | |
1482 | } | |
1483 | else | |
1484 | seq = PL_cop_seqmax; | |
dd2155a4 | 1485 | if (! PL_min_intro_pending) |
8635e3c2 | 1486 | return seq; |
dd2155a4 | 1487 | |
9b7476d7 | 1488 | svp = PadnamelistARRAY(PL_comppad_name); |
dd2155a4 | 1489 | for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) { |
6a0435be | 1490 | PADNAME * const sv = svp[i]; |
53c1dcc0 | 1491 | |
6a0435be | 1492 | if (sv && PadnameLEN(sv) && !PadnameOUTER(sv) |
0d311cdb DM |
1493 | && COP_SEQ_RANGE_LOW(sv) == PERL_PADSEQ_INTRO) |
1494 | { | |
2df5bdd7 | 1495 | COP_SEQ_RANGE_HIGH_set(sv, PERL_PADSEQ_INTRO); /* Don't know scope end yet. */ |
809abb02 | 1496 | COP_SEQ_RANGE_LOW_set(sv, PL_cop_seqmax); |
dd2155a4 | 1497 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
809abb02 | 1498 | "Pad intromy: %ld \"%s\", (%lu,%lu)\n", |
6a0435be | 1499 | (long)i, PadnamePV(sv), |
809abb02 NC |
1500 | (unsigned long)COP_SEQ_RANGE_LOW(sv), |
1501 | (unsigned long)COP_SEQ_RANGE_HIGH(sv)) | |
dd2155a4 DM |
1502 | ); |
1503 | } | |
1504 | } | |
953c8b80 | 1505 | COP_SEQMAX_INC; |
dd2155a4 DM |
1506 | PL_min_intro_pending = 0; |
1507 | PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */ | |
1508 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
6012dc80 | 1509 | "Pad intromy: seq -> %ld\n", (long)(PL_cop_seqmax))); |
dd2155a4 | 1510 | |
6012dc80 | 1511 | return seq; |
dd2155a4 DM |
1512 | } |
1513 | ||
1514 | /* | |
cc76b5cc | 1515 | =for apidoc m|void|pad_leavemy |
dd2155a4 DM |
1516 | |
1517 | Cleanup at end of scope during compilation: set the max seq number for | |
1518 | lexicals in this scope and warn of any lexicals that never got introduced. | |
1519 | ||
1520 | =cut | |
1521 | */ | |
1522 | ||
6d5c2147 | 1523 | OP * |
dd2155a4 DM |
1524 | Perl_pad_leavemy(pTHX) |
1525 | { | |
1526 | I32 off; | |
6d5c2147 | 1527 | OP *o = NULL; |
9b7476d7 | 1528 | PADNAME * const * const svp = PadnamelistARRAY(PL_comppad_name); |
dd2155a4 DM |
1529 | |
1530 | PL_pad_reset_pending = FALSE; | |
1531 | ||
f3548bdc | 1532 | ASSERT_CURPAD_ACTIVE("pad_leavemy"); |
dd2155a4 DM |
1533 | if (PL_min_intro_pending && PL_comppad_name_fill < PL_min_intro_pending) { |
1534 | for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) { | |
01b9977c FC |
1535 | const PADNAME * const name = svp[off]; |
1536 | if (name && PadnameLEN(name) && !PadnameOUTER(name)) | |
9b387841 | 1537 | Perl_ck_warner_d(aTHX_ packWARN(WARN_INTERNAL), |
01b9977c FC |
1538 | "%"PNf" never introduced", |
1539 | PNfARG(name)); | |
dd2155a4 DM |
1540 | } |
1541 | } | |
1542 | /* "Deintroduce" my variables that are leaving with this scope. */ | |
9b7476d7 FC |
1543 | for (off = PadnamelistMAX(PL_comppad_name); |
1544 | off > PL_comppad_name_fill; off--) { | |
01b9977c FC |
1545 | PADNAME * const sv = svp[off]; |
1546 | if (sv && PadnameLEN(sv) && !PadnameOUTER(sv) | |
2df5bdd7 DM |
1547 | && COP_SEQ_RANGE_HIGH(sv) == PERL_PADSEQ_INTRO) |
1548 | { | |
809abb02 | 1549 | COP_SEQ_RANGE_HIGH_set(sv, PL_cop_seqmax); |
dd2155a4 | 1550 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
809abb02 | 1551 | "Pad leavemy: %ld \"%s\", (%lu,%lu)\n", |
01b9977c | 1552 | (long)off, PadnamePV(sv), |
809abb02 NC |
1553 | (unsigned long)COP_SEQ_RANGE_LOW(sv), |
1554 | (unsigned long)COP_SEQ_RANGE_HIGH(sv)) | |
dd2155a4 | 1555 | ); |
6d5c2147 FC |
1556 | if (!PadnameIsSTATE(sv) && !PadnameIsOUR(sv) |
1557 | && *PadnamePV(sv) == '&' && PadnameLEN(sv) > 1) { | |
1558 | OP *kid = newOP(OP_INTROCV, 0); | |
1559 | kid->op_targ = off; | |
1560 | o = op_prepend_elem(OP_LINESEQ, kid, o); | |
1561 | } | |
dd2155a4 DM |
1562 | } |
1563 | } | |
953c8b80 | 1564 | COP_SEQMAX_INC; |
dd2155a4 DM |
1565 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, |
1566 | "Pad leavemy: seq = %ld\n", (long)PL_cop_seqmax)); | |
6d5c2147 | 1567 | return o; |
dd2155a4 DM |
1568 | } |
1569 | ||
dd2155a4 | 1570 | /* |
cc76b5cc | 1571 | =for apidoc m|void|pad_swipe|PADOFFSET po|bool refadjust |
dd2155a4 DM |
1572 | |
1573 | Abandon the tmp in the current pad at offset po and replace with a | |
1574 | new one. | |
1575 | ||
1576 | =cut | |
1577 | */ | |
1578 | ||
1579 | void | |
1580 | Perl_pad_swipe(pTHX_ PADOFFSET po, bool refadjust) | |
1581 | { | |
f3548bdc | 1582 | ASSERT_CURPAD_LEGAL("pad_swipe"); |
dd2155a4 DM |
1583 | if (!PL_curpad) |
1584 | return; | |
1585 | if (AvARRAY(PL_comppad) != PL_curpad) | |
5637ef5b NC |
1586 | Perl_croak(aTHX_ "panic: pad_swipe curpad, %p!=%p", |
1587 | AvARRAY(PL_comppad), PL_curpad); | |
9100eeb1 Z |
1588 | if (!po || ((SSize_t)po) > AvFILLp(PL_comppad)) |
1589 | Perl_croak(aTHX_ "panic: pad_swipe po=%ld, fill=%ld", | |
1590 | (long)po, (long)AvFILLp(PL_comppad)); | |
dd2155a4 DM |
1591 | |
1592 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
1593 | "Pad 0x%"UVxf"[0x%"UVxf"] swipe: %ld\n", | |
1594 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po)); | |
1595 | ||
dd2155a4 DM |
1596 | if (refadjust) |
1597 | SvREFCNT_dec(PL_curpad[po]); | |
1598 | ||
9ad9869c DM |
1599 | |
1600 | /* if pad tmps aren't shared between ops, then there's no need to | |
1601 | * create a new tmp when an existing op is freed */ | |
53de1311 | 1602 | #ifdef USE_PAD_RESET |
561b68a9 | 1603 | PL_curpad[po] = newSV(0); |
dd2155a4 | 1604 | SvPADTMP_on(PL_curpad[po]); |
9ad9869c | 1605 | #else |
ce0d59fd | 1606 | PL_curpad[po] = NULL; |
97bf4a8d | 1607 | #endif |
325e1816 | 1608 | if (PadnamelistMAX(PL_comppad_name) != -1 |
4891fdfa | 1609 | && (PADOFFSET)PadnamelistMAX(PL_comppad_name) >= po) { |
ce0d59fd FC |
1610 | if (PadnamelistARRAY(PL_comppad_name)[po]) { |
1611 | assert(!PadnameLEN(PadnamelistARRAY(PL_comppad_name)[po])); | |
1612 | } | |
0f94cb1f | 1613 | PadnamelistARRAY(PL_comppad_name)[po] = &PL_padname_undef; |
325e1816 | 1614 | } |
b54c5e14 FC |
1615 | /* Use PL_constpadix here, not PL_padix. The latter may have been |
1616 | reset by pad_reset. We don’t want pad_alloc to have to scan the | |
1617 | whole pad when allocating a constant. */ | |
1618 | if ((I32)po < PL_constpadix) | |
1619 | PL_constpadix = po - 1; | |
dd2155a4 DM |
1620 | } |
1621 | ||
dd2155a4 | 1622 | /* |
cc76b5cc | 1623 | =for apidoc m|void|pad_reset |
dd2155a4 DM |
1624 | |
1625 | Mark all the current temporaries for reuse | |
1626 | ||
1627 | =cut | |
1628 | */ | |
1629 | ||
1780e744 FC |
1630 | /* pad_reset() causes pad temp TARGs (operator targets) to be shared |
1631 | * between OPs from different statements. During compilation, at the start | |
1632 | * of each statement pad_reset resets PL_padix back to its previous value. | |
1633 | * When allocating a target, pad_alloc begins its scan through the pad at | |
1634 | * PL_padix+1. */ | |
1f676739 | 1635 | static void |
82af08ae | 1636 | S_pad_reset(pTHX) |
dd2155a4 | 1637 | { |
53de1311 | 1638 | #ifdef USE_PAD_RESET |
dd2155a4 | 1639 | if (AvARRAY(PL_comppad) != PL_curpad) |
5637ef5b NC |
1640 | Perl_croak(aTHX_ "panic: pad_reset curpad, %p!=%p", |
1641 | AvARRAY(PL_comppad), PL_curpad); | |
dd2155a4 DM |
1642 | |
1643 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
1644 | "Pad 0x%"UVxf"[0x%"UVxf"] reset: padix %ld -> %ld", | |
1645 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), | |
1646 | (long)PL_padix, (long)PL_padix_floor | |
1647 | ) | |
1648 | ); | |
1649 | ||
284167a5 | 1650 | if (!TAINTING_get) { /* Can't mix tainted and non-tainted temporaries. */ |
dd2155a4 DM |
1651 | PL_padix = PL_padix_floor; |
1652 | } | |
1653 | #endif | |
1654 | PL_pad_reset_pending = FALSE; | |
1655 | } | |
1656 | ||
dd2155a4 | 1657 | /* |
cc76b5cc Z |
1658 | =for apidoc Amx|void|pad_tidy|padtidy_type type |
1659 | ||
1660 | Tidy up a pad at the end of compilation of the code to which it belongs. | |
1661 | Jobs performed here are: remove most stuff from the pads of anonsub | |
1662 | prototypes; give it a @_; mark temporaries as such. I<type> indicates | |
1663 | the kind of subroutine: | |
dd2155a4 | 1664 | |
cc76b5cc Z |
1665 | padtidy_SUB ordinary subroutine |
1666 | padtidy_SUBCLONE prototype for lexical closure | |
1667 | padtidy_FORMAT format | |
dd2155a4 DM |
1668 | |
1669 | =cut | |
1670 | */ | |
1671 | ||
1672 | /* XXX DAPM surely most of this stuff should be done properly | |
1673 | * at the right time beforehand, rather than going around afterwards | |
1674 | * cleaning up our mistakes ??? | |
1675 | */ | |
1676 | ||
1677 | void | |
1678 | Perl_pad_tidy(pTHX_ padtidy_type type) | |
1679 | { | |
27da23d5 | 1680 | dVAR; |
dd2155a4 | 1681 | |
f3548bdc | 1682 | ASSERT_CURPAD_ACTIVE("pad_tidy"); |
b5c19bd7 | 1683 | |
db21619c DM |
1684 | /* If this CV has had any 'eval-capable' ops planted in it: |
1685 | * i.e. it contains any of: | |
1686 | * | |
1687 | * * eval '...', | |
1688 | * * //ee, | |
1689 | * * use re 'eval'; /$var/ | |
1690 | * * /(?{..})/), | |
1691 | * | |
1692 | * Then any anon prototypes in the chain of CVs should be marked as | |
1693 | * cloneable, so that for example the eval's CV in | |
1694 | * | |
1695 | * sub { eval '$x' } | |
1696 | * | |
1697 | * gets the right CvOUTSIDE. If running with -d, *any* sub may | |
1698 | * potentially have an eval executed within it. | |
b5c19bd7 DM |
1699 | */ |
1700 | ||
1701 | if (PL_cv_has_eval || PL_perldb) { | |
e1ec3a88 | 1702 | const CV *cv; |
b5c19bd7 DM |
1703 | for (cv = PL_compcv ;cv; cv = CvOUTSIDE(cv)) { |
1704 | if (cv != PL_compcv && CvCOMPILED(cv)) | |
1705 | break; /* no need to mark already-compiled code */ | |
1706 | if (CvANON(cv)) { | |
1707 | DEBUG_Xv(PerlIO_printf(Perl_debug_log, | |
1708 | "Pad clone on cv=0x%"UVxf"\n", PTR2UV(cv))); | |
1709 | CvCLONE_on(cv); | |
1710 | } | |
00cc8743 | 1711 | CvHASEVAL_on(cv); |
b5c19bd7 DM |
1712 | } |
1713 | } | |
1714 | ||
eb8137a9 | 1715 | /* extend namepad to match curpad */ |
9b7476d7 FC |
1716 | if (PadnamelistMAX(PL_comppad_name) < AvFILLp(PL_comppad)) |
1717 | padnamelist_store(PL_comppad_name, AvFILLp(PL_comppad), NULL); | |
dd2155a4 DM |
1718 | |
1719 | if (type == padtidy_SUBCLONE) { | |
9b7476d7 | 1720 | PADNAME ** const namep = PadnamelistARRAY(PL_comppad_name); |
504618e9 | 1721 | PADOFFSET ix; |
b5c19bd7 | 1722 | |
dd2155a4 | 1723 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
dbfcda05 FC |
1724 | PADNAME *namesv; |
1725 | if (!namep[ix]) namep[ix] = &PL_padname_undef; | |
dd2155a4 | 1726 | |
dd2155a4 DM |
1727 | /* |
1728 | * The only things that a clonable function needs in its | |
3a6ce63a | 1729 | * pad are anonymous subs, constants and GVs. |
dd2155a4 DM |
1730 | * The rest are created anew during cloning. |
1731 | */ | |
b561f196 | 1732 | if (!PL_curpad[ix] || SvIMMORTAL(PL_curpad[ix])) |
3a6ce63a | 1733 | continue; |
ce0d59fd FC |
1734 | namesv = namep[ix]; |
1735 | if (!(PadnamePV(namesv) && | |
dbfcda05 | 1736 | (!PadnameLEN(namesv) || *PadnamePV(namesv) == '&'))) |
dd2155a4 DM |
1737 | { |
1738 | SvREFCNT_dec(PL_curpad[ix]); | |
a0714e2c | 1739 | PL_curpad[ix] = NULL; |
dd2155a4 DM |
1740 | } |
1741 | } | |
1742 | } | |
1743 | else if (type == padtidy_SUB) { | |
1744 | /* XXX DAPM this same bit of code keeps appearing !!! Rationalise? */ | |
53c1dcc0 | 1745 | AV * const av = newAV(); /* Will be @_ */ |
ad64d0ec | 1746 | av_store(PL_comppad, 0, MUTABLE_SV(av)); |
11ca45c0 | 1747 | AvREIFY_only(av); |
dd2155a4 DM |
1748 | } |
1749 | ||
4cee4ca8 | 1750 | if (type == padtidy_SUB || type == padtidy_FORMAT) { |
9b7476d7 | 1751 | PADNAME ** const namep = PadnamelistARRAY(PL_comppad_name); |
504618e9 | 1752 | PADOFFSET ix; |
dd2155a4 | 1753 | for (ix = AvFILLp(PL_comppad); ix > 0; ix--) { |
0f94cb1f | 1754 | if (!namep[ix]) namep[ix] = &PL_padname_undef; |
2347c8c0 | 1755 | if (!PL_curpad[ix] || SvIMMORTAL(PL_curpad[ix])) |
dd2155a4 | 1756 | continue; |
0f94cb1f | 1757 | if (SvPADMY(PL_curpad[ix]) && !PadnameOUTER(namep[ix])) { |
adf8f095 NC |
1758 | /* This is a work around for how the current implementation of |
1759 | ?{ } blocks in regexps interacts with lexicals. | |
1760 | ||
1761 | One of our lexicals. | |
1762 | Can't do this on all lexicals, otherwise sub baz() won't | |
1763 | compile in | |
1764 | ||
1765 | my $foo; | |
1766 | ||
1767 | sub bar { ++$foo; } | |
1768 | ||
1769 | sub baz { ++$foo; } | |
1770 | ||
1771 | because completion of compiling &bar calling pad_tidy() | |
1772 | would cause (top level) $foo to be marked as stale, and | |
1773 | "no longer available". */ | |
1774 | SvPADSTALE_on(PL_curpad[ix]); | |
1775 | } | |
dd2155a4 DM |
1776 | } |
1777 | } | |
f3548bdc | 1778 | PL_curpad = AvARRAY(PL_comppad); |
dd2155a4 DM |
1779 | } |
1780 | ||
dd2155a4 | 1781 | /* |
cc76b5cc | 1782 | =for apidoc m|void|pad_free|PADOFFSET po |
dd2155a4 | 1783 | |
8627550a | 1784 | Free the SV at offset po in the current pad. |
dd2155a4 DM |
1785 | |
1786 | =cut | |
1787 | */ | |
1788 | ||
1789 | /* XXX DAPM integrate with pad_swipe ???? */ | |
1790 | void | |
1791 | Perl_pad_free(pTHX_ PADOFFSET po) | |
1792 | { | |
53de1311 | 1793 | #ifndef USE_PAD_RESET |
ad9e6ae1 | 1794 | SV *sv; |
ff06c6b2 | 1795 | #endif |
f3548bdc | 1796 | ASSERT_CURPAD_LEGAL("pad_free"); |
dd2155a4 DM |
1797 | if (!PL_curpad) |
1798 | return; | |
1799 | if (AvARRAY(PL_comppad) != PL_curpad) | |
5637ef5b NC |
1800 | Perl_croak(aTHX_ "panic: pad_free curpad, %p!=%p", |
1801 | AvARRAY(PL_comppad), PL_curpad); | |
dd2155a4 DM |
1802 | if (!po) |
1803 | Perl_croak(aTHX_ "panic: pad_free po"); | |
1804 | ||
1805 | DEBUG_X(PerlIO_printf(Perl_debug_log, | |
1806 | "Pad 0x%"UVxf"[0x%"UVxf"] free: %ld\n", | |
1807 | PTR2UV(PL_comppad), PTR2UV(PL_curpad), (long)po) | |
1808 | ); | |
1809 | ||
53de1311 | 1810 | #ifndef USE_PAD_RESET |
ad9e6ae1 DM |
1811 | sv = PL_curpad[po]; |
1812 | if (sv && sv != &PL_sv_undef && !SvPADMY(sv)) | |
1813 | SvFLAGS(sv) &= ~SVs_PADTMP; | |
1814 | ||
dd2155a4 DM |
1815 | if ((I32)po < PL_padix) |
1816 | PL_padix = po - 1; | |
53d3c048 | 1817 | #endif |
dd2155a4 DM |
1818 | } |
1819 | ||
dd2155a4 | 1820 | /* |
cc76b5cc | 1821 | =for apidoc m|void|do_dump_pad|I32 level|PerlIO *file|PADLIST *padlist|int full |
dd2155a4 DM |
1822 | |
1823 | Dump the contents of a padlist | |
1824 | ||
1825 | =cut | |
1826 | */ | |
1827 | ||
1828 | void | |
1829 | Perl_do_dump_pad(pTHX_ I32 level, PerlIO *file, PADLIST *padlist, int full) | |
1830 | { | |
9b7476d7 | 1831 | const PADNAMELIST *pad_name; |
e1ec3a88 | 1832 | const AV *pad; |
01326933 | 1833 | PADNAME **pname; |
dd2155a4 | 1834 | SV **ppad; |
dd2155a4 DM |
1835 | I32 ix; |
1836 | ||
7918f24d NC |
1837 | PERL_ARGS_ASSERT_DO_DUMP_PAD; |
1838 | ||
dd2155a4 DM |
1839 | if (!padlist) { |
1840 | return; | |
1841 | } | |
9b7476d7 | 1842 | pad_name = PadlistNAMES(padlist); |
86d2498c | 1843 | pad = PadlistARRAY(padlist)[1]; |
9b7476d7 | 1844 | pname = PadnamelistARRAY(pad_name); |
dd2155a4 DM |
1845 | ppad = AvARRAY(pad); |
1846 | Perl_dump_indent(aTHX_ level, file, | |
1847 | "PADNAME = 0x%"UVxf"(0x%"UVxf") PAD = 0x%"UVxf"(0x%"UVxf")\n", | |
1848 | PTR2UV(pad_name), PTR2UV(pname), PTR2UV(pad), PTR2UV(ppad) | |
1849 | ); | |
1850 | ||
9b7476d7 | 1851 | for (ix = 1; ix <= PadnamelistMAX(pad_name); ix++) { |
01326933 | 1852 | const PADNAME *namesv = pname[ix]; |
325e1816 | 1853 | if (namesv && !PadnameLEN(namesv)) { |
a0714e2c | 1854 | namesv = NULL; |
dd2155a4 DM |
1855 | } |
1856 | if (namesv) { | |
01326933 | 1857 | if (PadnameOUTER(namesv)) |
ee6cee0c | 1858 | Perl_dump_indent(aTHX_ level+1, file, |
c0fd1b42 | 1859 | "%2d. 0x%"UVxf"<%lu> FAKE \"%s\" flags=0x%lx index=%lu\n", |
ee6cee0c DM |
1860 | (int) ix, |
1861 | PTR2UV(ppad[ix]), | |
1862 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0), | |
01326933 | 1863 | PadnamePV(namesv), |
809abb02 NC |
1864 | (unsigned long)PARENT_FAKELEX_FLAGS(namesv), |
1865 | (unsigned long)PARENT_PAD_INDEX(namesv) | |
b5c19bd7 | 1866 | |
ee6cee0c DM |
1867 | ); |
1868 | else | |
1869 | Perl_dump_indent(aTHX_ level+1, file, | |
809abb02 | 1870 | "%2d. 0x%"UVxf"<%lu> (%lu,%lu) \"%s\"\n", |
ee6cee0c DM |
1871 | (int) ix, |
1872 | PTR2UV(ppad[ix]), | |
1873 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0), | |
809abb02 NC |
1874 | (unsigned long)COP_SEQ_RANGE_LOW(namesv), |
1875 | (unsigned long)COP_SEQ_RANGE_HIGH(namesv), | |
01326933 | 1876 | PadnamePV(namesv) |
ee6cee0c | 1877 | ); |
dd2155a4 DM |
1878 | } |
1879 | else if (full) { | |
1880 | Perl_dump_indent(aTHX_ level+1, file, | |
1881 | "%2d. 0x%"UVxf"<%lu>\n", | |
1882 | (int) ix, | |
1883 | PTR2UV(ppad[ix]), | |
1884 | (unsigned long) (ppad[ix] ? SvREFCNT(ppad[ix]) : 0) | |
1885 | ); | |
1886 | } | |
1887 | } | |
1888 | } | |
1889 | ||
cc76b5cc | 1890 | #ifdef DEBUGGING |
dd2155a4 DM |
1891 | |
1892 | /* | |
cc76b5cc | 1893 | =for apidoc m|void|cv_dump|CV *cv|const char *title |
dd2155a4 DM |
1894 | |
1895 | dump the contents of a CV | |
1896 | ||
1897 | =cut | |
1898 | */ | |
1899 | ||
dd2155a4 | 1900 | STATIC void |
e1ec3a88 | 1901 | S_cv_dump(pTHX_ const CV *cv, const char *title) |
dd2155a4 | 1902 | { |
53c1dcc0 | 1903 | const CV * const outside = CvOUTSIDE(cv); |
b70d5558 | 1904 | PADLIST* const padlist = CvPADLIST(cv); |
dd2155a4 | 1905 | |
7918f24d NC |
1906 | PERL_ARGS_ASSERT_CV_DUMP; |
1907 | ||
dd2155a4 DM |
1908 | PerlIO_printf(Perl_debug_log, |
1909 | " %s: CV=0x%"UVxf" (%s), OUTSIDE=0x%"UVxf" (%s)\n", | |
1910 | title, | |
1911 | PTR2UV(cv), | |
1912 | (CvANON(cv) ? "ANON" | |
71f882da | 1913 | : (SvTYPE(cv) == SVt_PVFM) ? "FORMAT" |
dd2155a4 DM |
1914 | : (cv == PL_main_cv) ? "MAIN" |
1915 | : CvUNIQUE(cv) ? "UNIQUE" | |
1916 | : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"), | |
1917 | PTR2UV(outside), | |
1918 | (!outside ? "null" | |
1919 | : CvANON(outside) ? "ANON" | |
1920 | : (outside == PL_main_cv) ? "MAIN" | |
1921 | : CvUNIQUE(outside) ? "UNIQUE" | |
1922 | : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED")); | |
1923 | ||
1924 | PerlIO_printf(Perl_debug_log, | |
1925 | " PADLIST = 0x%"UVxf"\n", PTR2UV(padlist)); | |
1926 | do_dump_pad(1, Perl_debug_log, padlist, 1); | |
1927 | } | |
dd2155a4 | 1928 | |
cc76b5cc | 1929 | #endif /* DEBUGGING */ |
dd2155a4 DM |
1930 | |
1931 | /* | |
cc76b5cc | 1932 | =for apidoc Am|CV *|cv_clone|CV *proto |
dd2155a4 | 1933 | |
cc76b5cc Z |
1934 | Clone a CV, making a lexical closure. I<proto> supplies the prototype |
1935 | of the function: its code, pad structure, and other attributes. | |
1936 | The prototype is combined with a capture of outer lexicals to which the | |
1937 | code refers, which are taken from the currently-executing instance of | |
1938 | the immediately surrounding code. | |
dd2155a4 DM |
1939 | |
1940 | =cut | |
1941 | */ | |
1942 | ||
e10681aa FC |
1943 | static CV *S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside); |
1944 | ||
21195f4d | 1945 | static CV * |
5fab0186 | 1946 | S_cv_clone_pad(pTHX_ CV *proto, CV *cv, CV *outside, bool newcv) |
dd2155a4 | 1947 | { |
dd2155a4 | 1948 | I32 ix; |
b70d5558 | 1949 | PADLIST* const protopadlist = CvPADLIST(proto); |
9b7476d7 | 1950 | PADNAMELIST *const protopad_name = PadlistNAMES(protopadlist); |
86d2498c | 1951 | const PAD *const protopad = PadlistARRAY(protopadlist)[1]; |
39899bf0 | 1952 | PADNAME** const pname = PadnamelistARRAY(protopad_name); |
53c1dcc0 | 1953 | SV** const ppad = AvARRAY(protopad); |
9b7476d7 | 1954 | const I32 fname = PadnamelistMAX(protopad_name); |
e1ec3a88 | 1955 | const I32 fpad = AvFILLp(protopad); |
b5c19bd7 | 1956 | SV** outpad; |
71f882da | 1957 | long depth; |
e07561e6 | 1958 | bool subclones = FALSE; |
7918f24d | 1959 | |
dd2155a4 DM |
1960 | assert(!CvUNIQUE(proto)); |
1961 | ||
1b5aaca6 FC |
1962 | /* Anonymous subs have a weak CvOUTSIDE pointer, so its value is not |
1963 | * reliable. The currently-running sub is always the one we need to | |
1964 | * close over. | |
8d88fe29 FC |
1965 | * For my subs, the currently-running sub may not be the one we want. |
1966 | * We have to check whether it is a clone of CvOUTSIDE. | |
1b5aaca6 FC |
1967 | * Note that in general for formats, CvOUTSIDE != find_runcv. |
1968 | * Since formats may be nested inside closures, CvOUTSIDE may point | |
71f882da | 1969 | * to a prototype; we instead want the cloned parent who called us. |
af41786f | 1970 | */ |
71f882da | 1971 | |
e07561e6 | 1972 | if (!outside) { |
ebfebee4 | 1973 | if (CvWEAKOUTSIDE(proto)) |
71f882da | 1974 | outside = find_runcv(NULL); |
e07561e6 | 1975 | else { |
af41786f | 1976 | outside = CvOUTSIDE(proto); |
db4cf31d FC |
1977 | if ((CvCLONE(outside) && ! CvCLONED(outside)) |
1978 | || !CvPADLIST(outside) | |
b4db5868 | 1979 | || CvPADLIST(outside)->xpadl_id != protopadlist->xpadl_outid) { |
db4cf31d | 1980 | outside = find_runcv_where( |
a56015b9 | 1981 | FIND_RUNCV_padid_eq, PTR2IV(protopadlist->xpadl_outid), NULL |
70794f7b | 1982 | ); |
db4cf31d | 1983 | /* outside could be null */ |
5dff782d | 1984 | } |
e07561e6 | 1985 | } |
5dff782d | 1986 | } |
db4cf31d | 1987 | depth = outside ? CvDEPTH(outside) : 0; |
71f882da DM |
1988 | if (!depth) |
1989 | depth = 1; | |
b5c19bd7 | 1990 | |
dd2155a4 DM |
1991 | ENTER; |
1992 | SAVESPTR(PL_compcv); | |
e07561e6 | 1993 | PL_compcv = cv; |
5fab0186 | 1994 | if (newcv) SAVEFREESV(cv); /* in case of fatal warnings */ |
dd2155a4 | 1995 | |
a0d2bbd5 FC |
1996 | if (CvHASEVAL(cv)) |
1997 | CvOUTSIDE(cv) = MUTABLE_CV(SvREFCNT_inc_simple(outside)); | |
dd2155a4 | 1998 | |
cbacc9aa | 1999 | SAVESPTR(PL_comppad_name); |
9ef8d569 | 2000 | PL_comppad_name = protopad_name; |
eacbb379 | 2001 | CvPADLIST_set(cv, pad_new(padnew_CLONE|padnew_SAVE)); |
b4db5868 | 2002 | CvPADLIST(cv)->xpadl_id = protopadlist->xpadl_id; |
dd2155a4 | 2003 | |
b5c19bd7 | 2004 | av_fill(PL_comppad, fpad); |
dd2155a4 | 2005 | |
dd2155a4 DM |
2006 | PL_curpad = AvARRAY(PL_comppad); |
2007 | ||
db4cf31d | 2008 | outpad = outside && CvPADLIST(outside) |
86d2498c | 2009 | ? AvARRAY(PadlistARRAY(CvPADLIST(outside))[depth]) |
f2ead8b8 | 2010 | : NULL; |
b4db5868 | 2011 | if (outpad) CvPADLIST(cv)->xpadl_outid = CvPADLIST(outside)->xpadl_id; |
b5c19bd7 | 2012 | |
dd2155a4 | 2013 | for (ix = fpad; ix > 0; ix--) { |
39899bf0 | 2014 | PADNAME* const namesv = (ix <= fname) ? pname[ix] : NULL; |
a0714e2c | 2015 | SV *sv = NULL; |
325e1816 | 2016 | if (namesv && PadnameLEN(namesv)) { /* lexical */ |
f2047bf1 FC |
2017 | if (PadnameIsOUR(namesv)) { /* or maybe not so lexical */ |
2018 | NOOP; | |
2019 | } | |
2020 | else { | |
39899bf0 | 2021 | if (PadnameOUTER(namesv)) { /* lexical from outside? */ |
5aec98df FC |
2022 | /* formats may have an inactive, or even undefined, parent; |
2023 | but state vars are always available. */ | |
f2ead8b8 | 2024 | if (!outpad || !(sv = outpad[PARENT_PAD_INDEX(namesv)]) |
cae5dbbe | 2025 | || ( SvPADSTALE(sv) && !SvPAD_STATE(namesv) |
db4cf31d | 2026 | && (!outside || !CvDEPTH(outside))) ) { |
445f13ff | 2027 | S_unavailable(aTHX_ namesv); |
a0714e2c | 2028 | sv = NULL; |
71f882da | 2029 | } |
33894c1a | 2030 | else |
f84c484e | 2031 | SvREFCNT_inc_simple_void_NN(sv); |
dd2155a4 | 2032 | } |
71f882da | 2033 | if (!sv) { |
39899bf0 | 2034 | const char sigil = PadnamePV(namesv)[0]; |
e1ec3a88 | 2035 | if (sigil == '&') |
e07561e6 FC |
2036 | /* If there are state subs, we need to clone them, too. |
2037 | But they may need to close over variables we have | |
2038 | not cloned yet. So we will have to do a second | |
2039 | pass. Furthermore, there may be state subs clos- | |
2040 | ing over other state subs’ entries, so we have | |
2041 | to put a stub here and then clone into it on the | |
2042 | second pass. */ | |
6d5c2147 FC |
2043 | if (SvPAD_STATE(namesv) && !CvCLONED(ppad[ix])) { |
2044 | assert(SvTYPE(ppad[ix]) == SVt_PVCV); | |
2045 | subclones = 1; | |
2046 | sv = newSV_type(SVt_PVCV); | |
f3feca7a | 2047 | CvLEXICAL_on(sv); |
6d5c2147 FC |
2048 | } |
2049 | else if (PadnameLEN(namesv)>1 && !PadnameIsOUR(namesv)) | |
2050 | { | |
2051 | /* my sub */ | |
81df9f6f FC |
2052 | /* Just provide a stub, but name it. It will be |
2053 | upgrade to the real thing on scope entry. */ | |
f7cf2d13 | 2054 | dVAR; |
e1588866 | 2055 | U32 hash; |
39899bf0 FC |
2056 | PERL_HASH(hash, PadnamePV(namesv)+1, |
2057 | PadnameLEN(namesv) - 1); | |
81df9f6f | 2058 | sv = newSV_type(SVt_PVCV); |
cf748c3c FC |
2059 | CvNAME_HEK_set( |
2060 | sv, | |
39899bf0 FC |
2061 | share_hek(PadnamePV(namesv)+1, |
2062 | 1 - PadnameLEN(namesv), | |
e1588866 | 2063 | hash) |
cf748c3c | 2064 | ); |
f3feca7a | 2065 | CvLEXICAL_on(sv); |
6d5c2147 FC |
2066 | } |
2067 | else sv = SvREFCNT_inc(ppad[ix]); | |
e1ec3a88 | 2068 | else if (sigil == '@') |
ad64d0ec | 2069 | sv = MUTABLE_SV(newAV()); |
e1ec3a88 | 2070 | else if (sigil == '%') |
ad64d0ec | 2071 | sv = MUTABLE_SV(newHV()); |
dd2155a4 | 2072 | else |
561b68a9 | 2073 | sv = newSV(0); |
0d3b281c | 2074 | /* reset the 'assign only once' flag on each state var */ |
e07561e6 | 2075 | if (sigil != '&' && SvPAD_STATE(namesv)) |
0d3b281c | 2076 | SvPADSTALE_on(sv); |
dd2155a4 | 2077 | } |
f2047bf1 | 2078 | } |
dd2155a4 | 2079 | } |
4c894bf7 | 2080 | else if (namesv && PadnamePV(namesv)) { |
f84c484e | 2081 | sv = SvREFCNT_inc_NN(ppad[ix]); |
dd2155a4 DM |
2082 | } |
2083 | else { | |
561b68a9 | 2084 | sv = newSV(0); |
dd2155a4 | 2085 | SvPADTMP_on(sv); |
dd2155a4 | 2086 | } |
71f882da | 2087 | PL_curpad[ix] = sv; |
dd2155a4 DM |
2088 | } |
2089 | ||
e07561e6 FC |
2090 | if (subclones) |
2091 | for (ix = fpad; ix > 0; ix--) { | |
39899bf0 FC |
2092 | PADNAME * const name = (ix <= fname) ? pname[ix] : NULL; |
2093 | if (name && name != &PL_padname_undef && !PadnameOUTER(name) | |
2094 | && PadnamePV(name)[0] == '&' && PadnameIsSTATE(name)) | |
e07561e6 FC |
2095 | S_cv_clone(aTHX_ (CV *)ppad[ix], (CV *)PL_curpad[ix], cv); |
2096 | } | |
2097 | ||
5fab0186 | 2098 | if (newcv) SvREFCNT_inc_simple_void_NN(cv); |
e10681aa | 2099 | LEAVE; |
21195f4d | 2100 | |
1567c65a FC |
2101 | if (CvCONST(cv)) { |
2102 | /* Constant sub () { $x } closing over $x: | |
2103 | * The prototype was marked as a candiate for const-ization, | |
2104 | * so try to grab the current const value, and if successful, | |
2105 | * turn into a const sub: | |
2106 | */ | |
d8d6ddf8 FC |
2107 | SV* const_sv; |
2108 | OP *o = CvSTART(cv); | |
1567c65a | 2109 | assert(newcv); |
d8d6ddf8 FC |
2110 | for (; o; o = o->op_next) |
2111 | if (o->op_type == OP_PADSV) | |
2112 | break; | |
2113 | ASSUME(o->op_type == OP_PADSV); | |
2114 | const_sv = PAD_BASE_SV(CvPADLIST(cv), o->op_targ); | |
2115 | /* the candidate should have 1 ref from this pad and 1 ref | |
2116 | * from the parent */ | |
2117 | if (const_sv && SvREFCNT(const_sv) == 2) { | |
1567c65a | 2118 | const bool was_method = cBOOL(CvMETHOD(cv)); |
04472a84 | 2119 | bool copied = FALSE; |
d8d6ddf8 FC |
2120 | if (outside) { |
2121 | PADNAME * const pn = | |
2122 | PadlistNAMESARRAY(CvPADLIST(outside)) | |
2123 | [PARENT_PAD_INDEX(PadlistNAMESARRAY( | |
2124 | CvPADLIST(cv))[o->op_targ])]; | |
2125 | assert(PadnameOUTER(PadlistNAMESARRAY(CvPADLIST(cv)) | |
2126 | [o->op_targ])); | |
2127 | if (PadnameLVALUE(pn)) { | |
2128 | /* We have a lexical that is potentially modifiable | |
2129 | elsewhere, so making a constant will break clo- | |
2130 | sure behaviour. If this is a ‘simple lexical | |
2131 | op tree’, i.e., sub(){$x}, emit a deprecation | |
2132 | warning, but continue to exhibit the old behav- | |
2133 | iour of making it a constant based on the ref- | |
2134 | count of the candidate variable. | |
2135 | ||
2136 | A simple lexical op tree looks like this: | |
2137 | ||
2138 | leavesub | |
2139 | lineseq | |
2140 | nextstate | |
2141 | padsv | |
2142 | */ | |
e6dae479 | 2143 | if (OpSIBLING( |
d8d6ddf8 FC |
2144 | cUNOPx(cUNOPx(CvROOT(cv))->op_first)->op_first |
2145 | ) == o | |
e6dae479 | 2146 | && !OpSIBLING(o)) |
04472a84 | 2147 | { |
d8d6ddf8 FC |
2148 | Perl_ck_warner_d(aTHX_ |
2149 | packWARN(WARN_DEPRECATED), | |
2150 | "Constants from lexical " | |
2151 | "variables potentially " | |
2152 | "modified elsewhere are " | |
2153 | "deprecated"); | |
04472a84 FC |
2154 | /* We *copy* the lexical variable, and donate the |
2155 | copy to newCONSTSUB. Yes, this is ugly, and | |
2156 | should be killed. We need to do this for the | |
2157 | time being, however, because turning on SvPADTMP | |
2158 | on a lexical will have observable effects | |
2159 | elsewhere. */ | |
2160 | const_sv = newSVsv(const_sv); | |
2161 | copied = TRUE; | |
2162 | } | |
d8d6ddf8 FC |
2163 | else |
2164 | goto constoff; | |
2165 | } | |
2166 | } | |
04472a84 FC |
2167 | if (!copied) |
2168 | SvREFCNT_inc_simple_void_NN(const_sv); | |
2169 | /* If the lexical is not used elsewhere, it is safe to turn on | |
2170 | SvPADTMP, since it is only when it is used in lvalue con- | |
2171 | text that the difference is observable. */ | |
6dfba0aa | 2172 | SvREADONLY_on(const_sv); |
d8d6ddf8 | 2173 | SvPADTMP_on(const_sv); |
1567c65a | 2174 | SvREFCNT_dec_NN(cv); |
1567c65a FC |
2175 | cv = newCONSTSUB(CvSTASH(proto), NULL, const_sv); |
2176 | if (was_method) | |
2177 | CvMETHOD_on(cv); | |
2178 | } | |
2179 | else { | |
d8d6ddf8 | 2180 | constoff: |
1567c65a FC |
2181 | CvCONST_off(cv); |
2182 | } | |
2183 | } | |
2184 | ||
21195f4d | 2185 | return cv; |
e10681aa FC |
2186 | } |
2187 | ||
2188 | static CV * | |
2189 | S_cv_clone(pTHX_ CV *proto, CV *cv, CV *outside) | |
2190 | { | |
20b7effb | 2191 | #ifdef USE_ITHREADS |
c04ef36e | 2192 | dVAR; |
20b7effb | 2193 | #endif |
5fab0186 | 2194 | const bool newcv = !cv; |
c04ef36e | 2195 | |
e10681aa FC |
2196 | assert(!CvUNIQUE(proto)); |
2197 | ||
2198 | if (!cv) cv = MUTABLE_CV(newSV_type(SvTYPE(proto))); | |
2199 | CvFLAGS(cv) = CvFLAGS(proto) & ~(CVf_CLONE|CVf_WEAKOUTSIDE|CVf_CVGV_RC | |
2200 | |CVf_SLABBED); | |
2201 | CvCLONED_on(cv); | |
2202 | ||
2203 | CvFILE(cv) = CvDYNFILE(proto) ? savepv(CvFILE(proto)) | |
2204 | : CvFILE(proto); | |
2205 | if (CvNAMED(proto)) | |
2e800d79 | 2206 | CvNAME_HEK_set(cv, share_hek_hek(CvNAME_HEK(proto))); |
e10681aa FC |
2207 | else CvGV_set(cv,CvGV(proto)); |
2208 | CvSTASH_set(cv, CvSTASH(proto)); | |
2209 | OP_REFCNT_LOCK; | |
2210 | CvROOT(cv) = OpREFCNT_inc(CvROOT(proto)); | |
2211 | OP_REFCNT_UNLOCK; | |
2212 | CvSTART(cv) = CvSTART(proto); | |
2213 | CvOUTSIDE_SEQ(cv) = CvOUTSIDE_SEQ(proto); | |
2214 | ||
fdf416b6 | 2215 | if (SvPOK(proto)) { |
e10681aa | 2216 | sv_setpvn(MUTABLE_SV(cv), SvPVX_const(proto), SvCUR(proto)); |
fdf416b6 BF |
2217 | if (SvUTF8(proto)) |
2218 | SvUTF8_on(MUTABLE_SV(cv)); | |
2219 | } | |
e10681aa FC |
2220 | if (SvMAGIC(proto)) |
2221 | mg_copy((SV *)proto, (SV *)cv, 0, 0); | |
2222 | ||
21195f4d FC |
2223 | if (CvPADLIST(proto)) |
2224 | cv = S_cv_clone_pad(aTHX_ proto, cv, outside, newcv); | |
e10681aa | 2225 | |
dd2155a4 DM |
2226 | DEBUG_Xv( |
2227 | PerlIO_printf(Perl_debug_log, "\nPad CV clone\n"); | |
e10681aa | 2228 | if (CvOUTSIDE(cv)) cv_dump(CvOUTSIDE(cv), "Outside"); |
dd2155a4 DM |
2229 | cv_dump(proto, "Proto"); |
2230 | cv_dump(cv, "To"); | |
2231 | ); | |
2232 | ||
dd2155a4 DM |
2233 | return cv; |
2234 | } | |
2235 | ||
e07561e6 FC |
2236 | CV * |
2237 | Perl_cv_clone(pTHX_ CV *proto) | |
2238 | { | |
2239 | PERL_ARGS_ASSERT_CV_CLONE; | |
2240 | ||
fead5351 | 2241 | if (!CvPADLIST(proto)) Perl_croak(aTHX_ "panic: no pad in cv_clone"); |
e07561e6 FC |
2242 | return S_cv_clone(aTHX_ proto, NULL, NULL); |
2243 | } | |
2244 | ||
6d5c2147 FC |
2245 | /* Called only by pp_clonecv */ |
2246 | CV * | |
2247 | Perl_cv_clone_into(pTHX_ CV *proto, CV *target) | |
2248 | { | |
2249 | PERL_ARGS_ASSERT_CV_CLONE_INTO; | |
2250 | cv_undef(target); | |
2251 | return S_cv_clone(aTHX_ proto, target, NULL); | |
2252 | } | |
2253 | ||
fb094047 FC |
2254 | /* |
2255 | =for apidoc cv_name | |
2256 | ||
2257 | Returns an SV containing the name of the CV, mainly for use in error | |
2258 | reporting. The CV may actually be a GV instead, in which case the returned | |
7a275a2e FC |
2259 | SV holds the GV's name. Anything other than a GV or CV is treated as a |
2260 | string already holding the sub name, but this could change in the future. | |
fb094047 FC |
2261 | |
2262 | An SV may be passed as a second argument. If so, the name will be assigned | |
2263 | to it and it will be returned. Otherwise the returned SV will be a new | |
2264 | mortal. | |
2265 | ||
ecf05a58 FC |
2266 | If the I<flags> include CV_NAME_NOTQUAL, then the package name will not be |
2267 | included. If the first argument is neither a CV nor a GV, this flag is | |
2268 | ignored (subject to change). | |
2269 | ||
fb094047 FC |
2270 | =cut |
2271 | */ | |
2272 | ||
c5569a55 | 2273 | SV * |
ecf05a58 | 2274 | Perl_cv_name(pTHX_ CV *cv, SV *sv, U32 flags) |
c5569a55 FC |
2275 | { |
2276 | PERL_ARGS_ASSERT_CV_NAME; | |
2277 | if (!isGV_with_GP(cv) && SvTYPE(cv) != SVt_PVCV) { | |
2278 | if (sv) sv_setsv(sv,(SV *)cv); | |
2279 | return sv ? (sv) : (SV *)cv; | |
2280 | } | |
2281 | { | |
f3fb6cf3 | 2282 | SV * const retsv = sv ? (sv) : sv_newmortal(); |
c5569a55 FC |
2283 | if (SvTYPE(cv) == SVt_PVCV) { |
2284 | if (CvNAMED(cv)) { | |
ecf05a58 FC |
2285 | if (CvLEXICAL(cv) || flags & CV_NAME_NOTQUAL) |
2286 | sv_sethek(retsv, CvNAME_HEK(cv)); | |
c5569a55 FC |
2287 | else { |
2288 | sv_sethek(retsv, HvNAME_HEK(CvSTASH(cv))); | |
2289 | sv_catpvs(retsv, "::"); | |
f34d8cdd | 2290 | sv_cathek(retsv, CvNAME_HEK(cv)); |
c5569a55 FC |
2291 | } |
2292 | } | |
ecf05a58 | 2293 | else if (CvLEXICAL(cv) || flags & CV_NAME_NOTQUAL) |
c5569a55 FC |
2294 | sv_sethek(retsv, GvNAME_HEK(GvEGV(CvGV(cv)))); |
2295 | else gv_efullname3(retsv, CvGV(cv), NULL); | |
2296 | } | |
ecf05a58 | 2297 | else if (flags & CV_NAME_NOTQUAL) sv_sethek(retsv, GvNAME_HEK(cv)); |
c5569a55 FC |
2298 | else gv_efullname3(retsv,(GV *)cv,NULL); |
2299 | return retsv; | |
2300 | } | |
2301 | } | |
2302 | ||
dd2155a4 | 2303 | /* |
cc76b5cc | 2304 | =for apidoc m|void|pad_fixup_inner_anons|PADLIST *padlist|CV *old_cv|CV *new_cv |
dd2155a4 DM |
2305 | |
2306 | For any anon CVs in the pad, change CvOUTSIDE of that CV from | |
72d33970 | 2307 | old_cv to new_cv if necessary. Needed when a newly-compiled CV has to be |
7dafbf52 | 2308 | moved to a pre-existing CV struct. |
dd2155a4 DM |
2309 | |
2310 | =cut | |
2311 | */ | |
2312 | ||
2313 | void | |
2314 | Perl_pad_fixup_inner_anons(pTHX_ PADLIST *padlist, CV *old_cv, CV *new_cv) | |
2315 | { | |
2316 | I32 ix; | |
9b7476d7 | 2317 | PADNAMELIST * const comppad_name = PadlistNAMES(padlist); |
86d2498c | 2318 | AV * const comppad = PadlistARRAY(padlist)[1]; |
0f94cb1f | 2319 | PADNAME ** const namepad = PadnamelistARRAY(comppad_name); |
53c1dcc0 | 2320 | SV ** const curpad = AvARRAY(comppad); |
7918f24d NC |
2321 | |
2322 | PERL_ARGS_ASSERT_PAD_FIXUP_INNER_ANONS; | |
294a48e9 AL |
2323 | PERL_UNUSED_ARG(old_cv); |
2324 | ||
9b7476d7 | 2325 | for (ix = PadnamelistMAX(comppad_name); ix > 0; ix--) { |
307cbb9f FC |
2326 | const PADNAME *name = namepad[ix]; |
2327 | if (name && name != &PL_padname_undef && !PadnameIsOUR(name) | |
0f94cb1f | 2328 | && *PadnamePV(name) == '&') |
dd2155a4 | 2329 | { |
307cbb9f FC |
2330 | CV *innercv = MUTABLE_CV(curpad[ix]); |
2331 | if (UNLIKELY(PadnameOUTER(name))) { | |
2332 | CV *cv = new_cv; | |
2333 | PADNAME **names = namepad; | |
2334 | PADOFFSET i = ix; | |
2335 | while (PadnameOUTER(name)) { | |
2336 | cv = CvOUTSIDE(cv); | |
2337 | names = PadlistNAMESARRAY(CvPADLIST(cv)); | |
2338 | i = PARENT_PAD_INDEX(name); | |
2339 | name = names[i]; | |
2340 | } | |
2341 | innercv = (CV *)PadARRAY(PadlistARRAY(CvPADLIST(cv))[1])[i]; | |
2342 | } | |
2343 | if (SvTYPE(innercv) == SVt_PVCV) { | |
0f94cb1f FC |
2344 | /* XXX 0afba48f added code here to check for a proto CV |
2345 | attached to the pad entry by magic. But shortly there- | |
2346 | after 81df9f6f95 moved the magic to the pad name. The | |
2347 | code here was never updated, so it wasn’t doing anything | |
2348 | and got deleted when PADNAME became a distinct type. Is | |
2349 | there any bug as a result? */ | |
0afba48f | 2350 | if (CvOUTSIDE(innercv) == old_cv) { |
1f122f9b FC |
2351 | if (!CvWEAKOUTSIDE(innercv)) { |
2352 | SvREFCNT_dec(old_cv); | |
2353 | SvREFCNT_inc_simple_void_NN(new_cv); | |
2354 | } | |
0afba48f FC |
2355 | CvOUTSIDE(innercv) = new_cv; |
2356 | } | |
e09ac076 FC |
2357 | } |
2358 | else { /* format reference */ | |
2359 | SV * const rv = curpad[ix]; | |
2360 | CV *innercv; | |
2361 | if (!SvOK(rv)) continue; | |
2362 | assert(SvROK(rv)); | |
2363 | assert(SvWEAKREF(rv)); | |
2364 | innercv = (CV *)SvRV(rv); | |
2365 | assert(!CvWEAKOUTSIDE(innercv)); | |
2366 | SvREFCNT_dec(CvOUTSIDE(innercv)); | |
2367 | CvOUTSIDE(innercv) = (CV *)SvREFCNT_inc_simple_NN(new_cv); | |
2368 | } | |
dd2155a4 DM |
2369 | } |
2370 | } | |
2371 | } | |
2372 | ||
2373 | /* | |
cc76b5cc | 2374 | =for apidoc m|void|pad_push|PADLIST *padlist|int depth |
dd2155a4 DM |
2375 | |
2376 | Push a new pad frame onto the padlist, unless there's already a pad at | |
26019298 AL |
2377 | this depth, in which case don't bother creating a new one. Then give |
2378 | the new pad an @_ in slot zero. | |
dd2155a4 DM |
2379 | |
2380 | =cut | |
2381 | */ | |
2382 | ||
2383 | void | |
26019298 | 2384 | Perl_pad_push(pTHX_ PADLIST *padlist, int depth) |
dd2155a4 | 2385 | { |
7918f24d NC |
2386 | PERL_ARGS_ASSERT_PAD_PUSH; |
2387 | ||
86d2498c FC |
2388 | if (depth > PadlistMAX(padlist) || !PadlistARRAY(padlist)[depth]) { |
2389 | PAD** const svp = PadlistARRAY(padlist); | |
44f8325f AL |
2390 | AV* const newpad = newAV(); |
2391 | SV** const oldpad = AvARRAY(svp[depth-1]); | |
502c6561 | 2392 | I32 ix = AvFILLp((const AV *)svp[1]); |
9b7476d7 | 2393 | const I32 names_fill = PadnamelistMAX((PADNAMELIST *)svp[0]); |
a2ddd1d1 | 2394 | PADNAME ** const names = PadnamelistARRAY((PADNAMELIST *)svp[0]); |
26019298 AL |
2395 | AV *av; |
2396 | ||
dd2155a4 | 2397 | for ( ;ix > 0; ix--) { |
325e1816 | 2398 | if (names_fill >= ix && PadnameLEN(names[ix])) { |
a2ddd1d1 FC |
2399 | const char sigil = PadnamePV(names[ix])[0]; |
2400 | if (PadnameOUTER(names[ix]) | |
2401 | || PadnameIsSTATE(names[ix]) | |
fda94784 RGS |
2402 | || sigil == '&') |
2403 | { | |
dd2155a4 DM |
2404 | /* outer lexical or anon code */ |
2405 | av_store(newpad, ix, SvREFCNT_inc(oldpad[ix])); | |
2406 | } | |
2407 | else { /* our own lexical */ | |
26019298 AL |
2408 | SV *sv; |
2409 | if (sigil == '@') | |
ad64d0ec | 2410 | sv = MUTABLE_SV(newAV()); |
26019298 | 2411 | else if (sigil == '%') |
ad64d0ec | 2412 | sv = MUTABLE_SV(newHV()); |
dd2155a4 | 2413 | else |
561b68a9 | 2414 | sv = newSV(0); |
26019298 | 2415 | av_store(newpad, ix, sv); |
dd2155a4 DM |
2416 | } |
2417 | } | |
778f1807 | 2418 | else if (PadnamePV(names[ix])) { |
f84c484e | 2419 | av_store(newpad, ix, SvREFCNT_inc_NN(oldpad[ix])); |
dd2155a4 DM |
2420 | } |
2421 | else { | |
2422 | /* save temporaries on recursion? */ | |
561b68a9 | 2423 | SV * const sv = newSV(0); |
26019298 | 2424 | av_store(newpad, ix, sv); |
dd2155a4 DM |
2425 | SvPADTMP_on(sv); |
2426 | } | |
2427 | } | |
26019298 | 2428 | av = newAV(); |
ad64d0ec | 2429 | av_store(newpad, 0, MUTABLE_SV(av)); |
11ca45c0 | 2430 | AvREIFY_only(av); |
26019298 | 2431 | |
7261499d | 2432 | padlist_store(padlist, depth, newpad); |
dd2155a4 DM |
2433 | } |
2434 | } | |
b21dc031 | 2435 | |
d5b1589c NC |
2436 | #if defined(USE_ITHREADS) |
2437 | ||
2438 | # define av_dup_inc(s,t) MUTABLE_AV(sv_dup_inc((const SV *)s,t)) | |
2439 | ||
cc76b5cc | 2440 | /* |
b70d5558 | 2441 | =for apidoc padlist_dup |
cc76b5cc Z |
2442 | |
2443 | Duplicates a pad. | |
2444 | ||
2445 | =cut | |
2446 | */ | |
2447 | ||
b70d5558 FC |
2448 | PADLIST * |
2449 | Perl_padlist_dup(pTHX_ PADLIST *srcpad, CLONE_PARAMS *param) | |
d5b1589c | 2450 | { |
7261499d FC |
2451 | PADLIST *dstpad; |
2452 | bool cloneall; | |
2453 | PADOFFSET max; | |
2454 | ||
d5b1589c NC |
2455 | PERL_ARGS_ASSERT_PADLIST_DUP; |
2456 | ||
71c165d4 | 2457 | cloneall = cBOOL(param->flags & CLONEf_COPY_STACKS); |
86d2498c | 2458 | assert (SvREFCNT(PadlistARRAY(srcpad)[1]) == 1); |
7261499d | 2459 | |
86d2498c | 2460 | max = cloneall ? PadlistMAX(srcpad) : 1; |
7261499d FC |
2461 | |
2462 | Newx(dstpad, 1, PADLIST); | |
2463 | ptr_table_store(PL_ptr_table, srcpad, dstpad); | |
86d2498c FC |
2464 | PadlistMAX(dstpad) = max; |
2465 | Newx(PadlistARRAY(dstpad), max + 1, PAD *); | |
7261499d | 2466 | |
9b7476d7 FC |
2467 | PadlistARRAY(dstpad)[0] = (PAD *) |
2468 | padnamelist_dup(PadlistNAMES(srcpad), param); | |
2469 | PadnamelistREFCNT(PadlistNAMES(dstpad))++; | |
7261499d FC |
2470 | if (cloneall) { |
2471 | PADOFFSET depth; | |
9b7476d7 | 2472 | for (depth = 1; depth <= max; ++depth) |
86d2498c FC |
2473 | PadlistARRAY(dstpad)[depth] = |
2474 | av_dup_inc(PadlistARRAY(srcpad)[depth], param); | |
6de654a5 NC |
2475 | } else { |
2476 | /* CvDEPTH() on our subroutine will be set to 0, so there's no need | |
2477 | to build anything other than the first level of pads. */ | |
86d2498c | 2478 | I32 ix = AvFILLp(PadlistARRAY(srcpad)[1]); |
6de654a5 | 2479 | AV *pad1; |
9b7476d7 | 2480 | const I32 names_fill = PadnamelistMAX(PadlistNAMES(srcpad)); |
86d2498c | 2481 | const PAD *const srcpad1 = PadlistARRAY(srcpad)[1]; |
6de654a5 | 2482 | SV **oldpad = AvARRAY(srcpad1); |
3e020df5 | 2483 | PADNAME ** const names = PadnamelistARRAY(PadlistNAMES(dstpad)); |
6de654a5 NC |
2484 | SV **pad1a; |
2485 | AV *args; | |
6de654a5 | 2486 | |
6de654a5 NC |
2487 | pad1 = newAV(); |
2488 | ||
2489 | av_extend(pad1, ix); | |
86d2498c | 2490 | PadlistARRAY(dstpad)[1] = pad1; |
6de654a5 | 2491 | pad1a = AvARRAY(pad1); |
6de654a5 NC |
2492 | |
2493 | if (ix > -1) { | |
2494 | AvFILLp(pad1) = ix; | |
2495 | ||
2496 | for ( ;ix > 0; ix--) { | |
05d04d9c NC |
2497 | if (!oldpad[ix]) { |
2498 | pad1a[ix] = NULL; | |
ce0d59fd FC |
2499 | } else if (names_fill >= ix && names[ix] && |
2500 | PadnameLEN(names[ix])) { | |
3e020df5 FC |
2501 | const char sigil = PadnamePV(names[ix])[0]; |
2502 | if (PadnameOUTER(names[ix]) | |
2503 | || PadnameIsSTATE(names[ix]) | |
05d04d9c NC |
2504 | || sigil == '&') |
2505 | { | |
2506 | /* outer lexical or anon code */ | |
2507 | pad1a[ix] = sv_dup_inc(oldpad[ix], param); | |
2508 | } | |
2509 | else { /* our own lexical */ | |
adf8f095 NC |
2510 | if(SvPADSTALE(oldpad[ix]) && SvREFCNT(oldpad[ix]) > 1) { |
2511 | /* This is a work around for how the current | |
2512 | implementation of ?{ } blocks in regexps | |
2513 | interacts with lexicals. */ | |
05d04d9c NC |
2514 | pad1a[ix] = sv_dup_inc(oldpad[ix], param); |
2515 | } else { | |
2516 | SV *sv; | |
2517 | ||
2518 | if (sigil == '@') | |
2519 | sv = MUTABLE_SV(newAV()); | |
2520 | else if (sigil == '%') | |
2521 | sv = MUTABLE_SV(newHV()); | |
2522 | else | |
2523 | sv = newSV(0); | |
2524 | pad1a[ix] = sv; | |
05d04d9c NC |
2525 | } |
2526 | } | |
2527 | } | |
92154801 | 2528 | else if (( names_fill >= ix && names[ix] |
ce0d59fd | 2529 | && PadnamePV(names[ix]) )) { |
05d04d9c NC |
2530 | pad1a[ix] = sv_dup_inc(oldpad[ix], param); |
2531 | } | |
2532 | else { | |
2533 | /* save temporaries on recursion? */ | |
2534 | SV * const sv = newSV(0); | |
2535 | pad1a[ix] = sv; | |
2536 | ||
2537 | /* SvREFCNT(oldpad[ix]) != 1 for some code in threads.xs | |
2538 | FIXTHAT before merging this branch. | |
2539 | (And I know how to) */ | |
145bf8ee | 2540 | if (SvPADTMP(oldpad[ix])) |
05d04d9c NC |
2541 | SvPADTMP_on(sv); |
2542 | } | |
6de654a5 NC |
2543 | } |
2544 | ||
2545 | if (oldpad[0]) { | |
2546 | args = newAV(); /* Will be @_ */ | |
2547 | AvREIFY_only(args); | |
2548 | pad1a[0] = (SV *)args; | |
2549 | } | |
2550 | } | |
2551 | } | |
d5b1589c NC |
2552 | |
2553 | return dstpad; | |
2554 | } | |
2555 | ||
cc76b5cc | 2556 | #endif /* USE_ITHREADS */ |
d5b1589c | 2557 | |
7261499d | 2558 | PAD ** |
5aaab254 | 2559 | Perl_padlist_store(pTHX_ PADLIST *padlist, I32 key, PAD *val) |
7261499d | 2560 | { |
7261499d | 2561 | PAD **ary; |
86d2498c | 2562 | SSize_t const oldmax = PadlistMAX(padlist); |
7261499d FC |
2563 | |
2564 | PERL_ARGS_ASSERT_PADLIST_STORE; | |
2565 | ||
2566 | assert(key >= 0); | |
2567 | ||
86d2498c FC |
2568 | if (key > PadlistMAX(padlist)) { |
2569 | av_extend_guts(NULL,key,&PadlistMAX(padlist), | |
2570 | (SV ***)&PadlistARRAY(padlist), | |
2571 | (SV ***)&PadlistARRAY(padlist)); | |
2572 | Zero(PadlistARRAY(padlist)+oldmax+1, PadlistMAX(padlist)-oldmax, | |
7261499d FC |
2573 | PAD *); |
2574 | } | |
86d2498c | 2575 | ary = PadlistARRAY(padlist); |
7261499d FC |
2576 | SvREFCNT_dec(ary[key]); |
2577 | ary[key] = val; | |
2578 | return &ary[key]; | |
2579 | } | |
2580 | ||
66610fdd | 2581 | /* |
9b7476d7 FC |
2582 | =for apidoc newPADNAMELIST |
2583 | ||
2584 | Creates a new pad name list. C<max> is the highest index for which space | |
2585 | is allocated. | |
2586 | ||
2587 | =cut | |
2588 | */ | |
2589 | ||
2590 | PADNAMELIST * | |
a0e9f837 | 2591 | Perl_newPADNAMELIST(size_t max) |
9b7476d7 FC |
2592 | { |
2593 | PADNAMELIST *pnl; | |
2594 | Newx(pnl, 1, PADNAMELIST); | |
2595 | Newxz(PadnamelistARRAY(pnl), max+1, PADNAME *); | |
2596 | PadnamelistMAX(pnl) = -1; | |
2597 | PadnamelistREFCNT(pnl) = 1; | |
2598 | PadnamelistMAXNAMED(pnl) = 0; | |
2599 | pnl->xpadnl_max = max; | |
2600 | return pnl; | |
2601 | } | |
2602 | ||
2603 | /* | |
2604 | =for apidoc padnamelist_store | |
2605 | ||
2606 | Stores the pad name (which may be null) at the given index, freeing any | |
2607 | existing pad name in that slot. | |
2608 | ||
2609 | =cut | |
2610 | */ | |
2611 | ||
2612 | PADNAME ** | |
2613 | Perl_padnamelist_store(pTHX_ PADNAMELIST *pnl, SSize_t key, PADNAME *val) | |
2614 | { | |
2615 | PADNAME **ary; | |
2616 | ||
2617 | PERL_ARGS_ASSERT_PADNAMELIST_STORE; | |
2618 | ||
2619 | assert(key >= 0); | |
2620 | ||
2621 | if (key > pnl->xpadnl_max) | |
2622 | av_extend_guts(NULL,key,&pnl->xpadnl_max, | |
2623 | (SV ***)&PadnamelistARRAY(pnl), | |
2624 | (SV ***)&PadnamelistARRAY(pnl)); | |
2625 | if (PadnamelistMAX(pnl) < key) { | |
2626 | Zero(PadnamelistARRAY(pnl)+PadnamelistMAX(pnl)+1, | |
2627 | key-PadnamelistMAX(pnl), PADNAME *); | |
2628 | PadnamelistMAX(pnl) = key; | |
2629 | } | |
2630 | ary = PadnamelistARRAY(pnl); | |
0f94cb1f FC |
2631 | if (ary[key]) |
2632 | PadnameREFCNT_dec(ary[key]); | |
9b7476d7 FC |
2633 | ary[key] = val; |
2634 | return &ary[key]; | |
2635 | } | |
2636 | ||
2637 | /* | |
2638 | =for apidoc padnamelist_fetch | |
2639 | ||
2640 | Fetches the pad name from the given index. | |
2641 | ||
2642 | =cut | |
2643 | */ | |
2644 | ||
2645 | PADNAME * | |
a0e9f837 | 2646 | Perl_padnamelist_fetch(PADNAMELIST *pnl, SSize_t key) |
9b7476d7 FC |
2647 | { |
2648 | PERL_ARGS_ASSERT_PADNAMELIST_FETCH; | |
2649 | ASSUME(key >= 0); | |
2650 | ||
2651 | return key > PadnamelistMAX(pnl) ? NULL : PadnamelistARRAY(pnl)[key]; | |
2652 | } | |
2653 | ||
2654 | void | |
2655 | Perl_padnamelist_free(pTHX_ PADNAMELIST *pnl) | |
2656 | { | |
2657 | PERL_ARGS_ASSERT_PADNAMELIST_FREE; | |
2658 | if (!--PadnamelistREFCNT(pnl)) { | |
2659 | while(PadnamelistMAX(pnl) >= 0) | |
0f94cb1f FC |
2660 | { |
2661 | PADNAME * const pn = | |
2662 | PadnamelistARRAY(pnl)[PadnamelistMAX(pnl)--]; | |
2663 | if (pn) | |
2664 | PadnameREFCNT_dec(pn); | |
2665 | } | |
9b7476d7 FC |
2666 | Safefree(PadnamelistARRAY(pnl)); |
2667 | Safefree(pnl); | |
2668 | } | |
2669 | } | |
2670 | ||
2671 | #if defined(USE_ITHREADS) | |
2672 | ||
2673 | /* | |
2674 | =for apidoc padnamelist_dup | |
2675 | ||
2676 | Duplicates a pad name list. | |
2677 | ||
2678 | =cut | |
2679 | */ | |
2680 | ||
2681 | PADNAMELIST * | |
2682 | Perl_padnamelist_dup(pTHX_ PADNAMELIST *srcpad, CLONE_PARAMS *param) | |
2683 | { | |
2684 | PADNAMELIST *dstpad; | |
2685 | SSize_t max = PadnamelistMAX(srcpad); | |
2686 | ||
2687 | PERL_ARGS_ASSERT_PADNAMELIST_DUP; | |
2688 | ||
2689 | /* look for it in the table first */ | |
2690 | dstpad = (PADNAMELIST *)ptr_table_fetch(PL_ptr_table, srcpad); | |
2691 | if (dstpad) | |
2692 | return dstpad; | |
2693 | ||
2694 | dstpad = newPADNAMELIST(max); | |
2695 | PadnamelistREFCNT(dstpad) = 0; /* The caller will increment it. */ | |
2696 | PadnamelistMAXNAMED(dstpad) = PadnamelistMAXNAMED(srcpad); | |
2697 | PadnamelistMAX(dstpad) = max; | |
2698 | ||
2699 | ptr_table_store(PL_ptr_table, srcpad, dstpad); | |
2700 | for (; max >= 0; max--) | |
0f94cb1f | 2701 | if (PadnamelistARRAY(srcpad)[max]) { |
9b7476d7 | 2702 | PadnamelistARRAY(dstpad)[max] = |
0f94cb1f FC |
2703 | padname_dup(PadnamelistARRAY(srcpad)[max], param); |
2704 | PadnameREFCNT(PadnamelistARRAY(dstpad)[max])++; | |
2705 | } | |
9b7476d7 FC |
2706 | |
2707 | return dstpad; | |
2708 | } | |
2709 | ||
2710 | #endif /* USE_ITHREADS */ | |
2711 | ||
0f94cb1f FC |
2712 | /* |
2713 | =for apidoc newPADNAMEpvn | |
2714 | ||
2715 | Constructs and returns a new pad name. I<s> must be a UTF8 string. Do not | |
2716 | use this for pad names that point to outer lexicals. See | |
2717 | L</newPADNAMEouter>. | |
2718 | ||
2719 | =cut | |
2720 | */ | |
2721 | ||
2722 | PADNAME * | |
a0e9f837 | 2723 | Perl_newPADNAMEpvn(const char *s, STRLEN len) |
0f94cb1f FC |
2724 | { |
2725 | struct padname_with_str *alloc; | |
2726 | char *alloc2; /* for Newxz */ | |
2727 | PADNAME *pn; | |
2728 | PERL_ARGS_ASSERT_NEWPADNAMEPVN; | |
2729 | Newxz(alloc2, | |
2730 | STRUCT_OFFSET(struct padname_with_str, xpadn_str[0]) + len + 1, | |
2731 | char); | |
2732 | alloc = (struct padname_with_str *)alloc2; | |
2733 | pn = (PADNAME *)alloc; | |
2734 | PadnameREFCNT(pn) = 1; | |
2735 | PadnamePV(pn) = alloc->xpadn_str; | |
2736 | Copy(s, PadnamePV(pn), len, char); | |
2737 | *(PadnamePV(pn) + len) = '\0'; | |
2738 | PadnameLEN(pn) = len; | |
2739 | return pn; | |
2740 | } | |
2741 | ||
2742 | /* | |
2743 | =for apidoc newPADNAMEouter | |
2744 | ||
2745 | Constructs and returns a new pad name. Only use this function for names | |
2746 | that refer to outer lexicals. (See also L</newPADNAMEpvn>.) I<outer> is | |
2747 | the outer pad name that this one mirrors. The returned pad name has the | |
2748 | PADNAMEt_OUTER flag already set. | |
2749 | ||
2750 | =cut | |
2751 | */ | |
2752 | ||
2753 | PADNAME * | |
a0e9f837 | 2754 | Perl_newPADNAMEouter(PADNAME *outer) |
0f94cb1f FC |
2755 | { |
2756 | PADNAME *pn; | |
2757 | PERL_ARGS_ASSERT_NEWPADNAMEOUTER; | |
2758 | Newxz(pn, 1, PADNAME); | |
2759 | PadnameREFCNT(pn) = 1; | |
2760 | PadnamePV(pn) = PadnamePV(outer); | |
2761 | /* Not PadnameREFCNT(outer), because ‘outer’ may itself close over | |
2762 | another entry. The original pad name owns the buffer. */ | |
2763 | PadnameREFCNT(PADNAME_FROM_PV(PadnamePV(outer)))++; | |
2764 | PadnameFLAGS(pn) = PADNAMEt_OUTER; | |
2765 | PadnameLEN(pn) = PadnameLEN(outer); | |
2766 | return pn; | |
2767 | } | |
2768 | ||
2769 | void | |
2770 | Perl_padname_free(pTHX_ PADNAME *pn) | |
2771 | { | |
2772 | PERL_ARGS_ASSERT_PADNAME_FREE; | |
2773 | if (!--PadnameREFCNT(pn)) { | |
2774 | if (UNLIKELY(pn == &PL_padname_undef || pn == &PL_padname_const)) { | |
2775 | PadnameREFCNT(pn) = SvREFCNT_IMMORTAL; | |
2776 | return; | |
2777 | } | |
2778 | SvREFCNT_dec(PadnameTYPE(pn)); /* Takes care of protocv, too. */ | |
2779 | SvREFCNT_dec(PadnameOURSTASH(pn)); | |
2780 | if (PadnameOUTER(pn)) | |
2781 | PadnameREFCNT_dec(PADNAME_FROM_PV(PadnamePV(pn))); | |
2782 | Safefree(pn); | |
2783 | } | |
2784 | } | |
2785 | ||
2786 | #if defined(USE_ITHREADS) | |
2787 | ||
2788 | /* | |
2789 | =for apidoc padname_dup | |
2790 | ||
2791 | Duplicates a pad name. | |
2792 | ||
2793 | =cut | |
2794 | */ | |
2795 | ||
2796 | PADNAME * | |
2797 | Perl_padname_dup(pTHX_ PADNAME *src, CLONE_PARAMS *param) | |
2798 | { | |
2799 | PADNAME *dst; | |
2800 | ||
2801 | PERL_ARGS_ASSERT_PADNAME_DUP; | |
2802 | ||
2803 | /* look for it in the table first */ | |
2804 | dst = (PADNAME *)ptr_table_fetch(PL_ptr_table, src); | |
2805 | if (dst) | |
2806 | return dst; | |
2807 | ||
2808 | if (!PadnamePV(src)) { | |
2809 | dst = &PL_padname_undef; | |
2810 | ptr_table_store(PL_ptr_table, src, dst); | |
2811 | return dst; | |
2812 | } | |
2813 | ||
2814 | dst = PadnameOUTER(src) | |
2815 | ? newPADNAMEouter(padname_dup(PADNAME_FROM_PV(PadnamePV(src)), param)) | |
2816 | : newPADNAMEpvn(PadnamePV(src), PadnameLEN(src)); | |
2817 | ptr_table_store(PL_ptr_table, src, dst); | |
2818 | PadnameLEN(dst) = PadnameLEN(src); | |
2819 | PadnameFLAGS(dst) = PadnameFLAGS(src); | |
2820 | PadnameREFCNT(dst) = 0; /* The caller will increment it. */ | |
2821 | PadnameTYPE (dst) = (HV *)sv_dup_inc((SV *)PadnameTYPE(src), param); | |
2822 | PadnameOURSTASH(dst) = (HV *)sv_dup_inc((SV *)PadnameOURSTASH(src), | |
2823 | param); | |
2824 | dst->xpadn_low = src->xpadn_low; | |
2825 | dst->xpadn_high = src->xpadn_high; | |
2826 | dst->xpadn_gen = src->xpadn_gen; | |
2827 | return dst; | |
2828 | } | |
2829 | ||
2830 | #endif /* USE_ITHREADS */ | |
9b7476d7 FC |
2831 | |
2832 | /* | |
66610fdd RGS |
2833 | * Local variables: |
2834 | * c-indentation-style: bsd | |
2835 | * c-basic-offset: 4 | |
14d04a33 | 2836 | * indent-tabs-mode: nil |
66610fdd RGS |
2837 | * End: |
2838 | * | |
14d04a33 | 2839 | * ex: set ts=8 sts=4 sw=4 et: |
37442d52 | 2840 | */ |