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