3 * Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008,
4 * 2009, 2010, 2011 by Larry Wall and others
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.
9 * This file defines the types and macros associated with the API for
10 * manipulating scratchpads, which are used by perl to store lexical
11 * variables, op targets and constants.
14 /* offsets within a pad */
16 typedef SSize_t PADOFFSET; /* signed so that -1 is a valid value */
17 #define NOT_IN_PAD ((PADOFFSET) -1)
19 /* B.xs expects the first members of these two structs to line up
20 (xpadl_max with xpadnl_fill).
24 SSize_t xpadl_max; /* max index for which array has space */
26 PAD ** xpadlarr_alloc; /* Pointer to beginning of array of AVs.
27 Note that a 'padnamelist *' is stored
28 in the 0 index of the AV. */
31 PAD * pad_1; /* this slice of PAD * array always alloced */
32 PAD * pad_2; /* maybe unalloced */
33 } * xpadlarr_dbg; /* for use with a C debugger only */
35 U32 xpadl_id; /* Semi-unique ID, shared between clones */
36 U32 xpadl_outid; /* ID of outer pad */
40 SSize_t xpadnl_fill; /* max index in use */
41 PADNAME ** xpadnl_alloc; /* pointer to beginning of array */
42 SSize_t xpadnl_max; /* max index for which array has space */
43 PADOFFSET xpadnl_max_named; /* highest index with len > 0 */
47 /* PERL_PADNAME_MINIMAL uses less memory, but on some platforms
48 PERL_PADNAME_ALIGNED may be faster, so platform-specific hints can
49 define one or the other. */
50 #if defined(PERL_PADNAME_MINIMAL) && defined (PERL_PADNAME_ALIGNED)
51 # error PERL_PADNAME_MINIMAL and PERL_PADNAME_ALIGNED are exclusive
54 #if !defined(PERL_PADNAME_MINIMAL) && !defined(PERL_PADNAME_ALIGNED)
55 # define PERL_PADNAME_MINIMAL
58 #define _PADNAME_BASE \
60 HV * xpadn_ourstash; \
62 HV * xpadn_typestash; \
76 struct padname_with_str {
77 #ifdef PERL_PADNAME_MINIMAL
80 struct padname xpadn_padname;
87 #define PADNAME_FROM_PV(s) \
88 ((PADNAME *)((s) - STRUCT_OFFSET(struct padname_with_str, xpadn_str)))
91 /* a value that PL_cop_seqmax is guaranteed never to be,
92 * flagging that a lexical is being introduced, or has not yet left scope
94 #define PERL_PADSEQ_INTRO U32_MAX
95 #define COP_SEQMAX_INC \
97 (void)(PL_cop_seqmax == PERL_PADSEQ_INTRO && PL_cop_seqmax++))
100 /* B.xs needs these for the benefit of B::Deparse */
101 /* Low range end is exclusive (valid from the cop seq after this one) */
102 /* High range end is inclusive (valid up to this cop seq) */
104 #define COP_SEQ_RANGE_LOW(pn) (pn)->xpadn_low
105 #define COP_SEQ_RANGE_HIGH(pn) (pn)->xpadn_high
106 #define PARENT_PAD_INDEX(pn) (pn)->xpadn_low
107 #define PARENT_FAKELEX_FLAGS(pn) (pn)->xpadn_high
109 /* Flags set in the SvIVX field of FAKE namesvs */
111 #define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */
112 #define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */
114 /* flags for the pad_new() function */
116 #define padnew_CLONE 1 /* this pad is for a cloned CV */
117 #define padnew_SAVE 2 /* save old globals */
118 #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */
120 /* values for the pad_tidy() function */
123 padtidy_SUB, /* tidy up a pad for a sub, */
124 padtidy_SUBCLONE, /* a cloned sub, */
125 padtidy_FORMAT /* or a format */
128 /* flags for pad_add_name_pvn. */
130 #define padadd_OUR 0x01 /* our declaration. */
131 #define padadd_STATE 0x02 /* state declaration. */
132 #define padadd_NO_DUP_CHECK 0x04 /* skip warning on dups. */
133 #define padadd_STALEOK 0x08 /* allow stale lexical in active
134 * sub, but only one level up */
136 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
137 * whether PL_comppad and PL_curpad are consistent and whether they have
140 # define pad_peg(label)
143 # define ASSERT_CURPAD_LEGAL(label) \
145 if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \
146 Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%" UVxf "[0x%" UVxf "]",\
147 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
150 # define ASSERT_CURPAD_ACTIVE(label) \
152 if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \
153 Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%" UVxf "[0x%" UVxf "]",\
154 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
156 # define ASSERT_CURPAD_LEGAL(label)
157 # define ASSERT_CURPAD_ACTIVE(label)
162 /* Note: the following three macros are actually defined in scope.h, but
163 * they are documented here for completeness, since they directly or
164 * indirectly affect pads. */
167 =for apidoc m|void|SAVEPADSV |PADOFFSET po
168 Save a pad slot (used to restore after an iteration)
172 XXX DAPM it would make more sense to make the arg a PADOFFSET
174 =for apidoc m|void|SAVECLEARSV |SV **svp
175 Clear the pointed to pad value on scope exit. (i.e. the runtime action of
178 =for apidoc m|void|SAVECOMPPAD
179 save C<PL_comppad> and C<PL_curpad>
182 =for apidoc Amx|PAD **|PadlistARRAY|PADLIST * padlist
183 The C array of a padlist, containing the pads. Only subscript it with
184 numbers >= 1, as the 0th entry is not guaranteed to remain usable.
186 =for apidoc Amx|SSize_t|PadlistMAX|PADLIST * padlist
187 The index of the last allocated space in the padlist. Note that the last
188 pad may be in an earlier slot. Any entries following it will be C<NULL> in
191 =for apidoc Amx|PADNAMELIST *|PadlistNAMES|PADLIST * padlist
192 The names associated with pad entries.
194 =for apidoc Amx|PADNAME **|PadlistNAMESARRAY|PADLIST * padlist
195 The C array of pad names.
197 =for apidoc Amx|SSize_t|PadlistNAMESMAX|PADLIST * padlist
198 The index of the last pad name.
200 =for apidoc Amx|U32|PadlistREFCNT|PADLIST * padlist
201 The reference count of the padlist. Currently this is always 1.
203 =for apidoc Amx|PADNAME **|PadnamelistARRAY|PADNAMELIST * pnl
204 The C array of pad names.
206 =for apidoc Amx|SSize_t|PadnamelistMAX|PADNAMELIST * pnl
207 The index of the last pad name.
209 =for apidoc Amx|SSize_t|PadnamelistREFCNT|PADNAMELIST * pnl
210 The reference count of the pad name list.
212 =for apidoc Amx|void|PadnamelistREFCNT_dec|PADNAMELIST * pnl
213 Lowers the reference count of the pad name list.
215 =for apidoc Amx|SV **|PadARRAY|PAD * pad
216 The C array of pad entries.
218 =for apidoc Amx|SSize_t|PadMAX|PAD * pad
219 The index of the last pad entry.
221 =for apidoc Amx|char *|PadnamePV|PADNAME * pn
222 The name stored in the pad name struct. This returns C<NULL> for a target
225 =for apidoc Amx|STRLEN|PadnameLEN|PADNAME * pn
226 The length of the name.
228 =for apidoc Amx|bool|PadnameUTF8|PADNAME * pn
229 Whether PadnamePV is in UTF-8. Currently, this is always true.
231 =for apidoc Amx|SV *|PadnameSV|PADNAME * pn
232 Returns the pad name as a mortal SV.
234 =for apidoc m|bool|PadnameIsOUR|PADNAME * pn
235 Whether this is an "our" variable.
237 =for apidoc m|HV *|PadnameOURSTASH|PADNAME * pn
238 The stash in which this "our" variable was declared.
240 =for apidoc m|bool|PadnameOUTER|PADNAME * pn
241 Whether this entry belongs to an outer pad. Entries for which this is true
242 are often referred to as 'fake'.
244 =for apidoc m|bool|PadnameIsSTATE|PADNAME * pn
245 Whether this is a "state" variable.
247 =for apidoc m|HV *|PadnameTYPE|PADNAME * pn
248 The stash associated with a typed lexical. This returns the C<%Foo::> hash
251 =for apidoc Amx|SSize_t|PadnameREFCNT|PADNAME * pn
252 The reference count of the pad name.
254 =for apidoc Amx|PADNAME *|PadnameREFCNT_inc|PADNAME * pn
255 Increases the reference count of the pad name. Returns the pad name itself.
257 =for apidoc Amx|void|PadnameREFCNT_dec|PADNAME * pn
258 Lowers the reference count of the pad name.
261 =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
262 Set the slot at offset C<po> in the current pad to C<sv>
264 =for apidoc m|SV *|PAD_SV |PADOFFSET po
265 Get the value at offset C<po> in the current pad
267 =for apidoc m|SV *|PAD_SVl |PADOFFSET po
268 Lightweight and lvalue version of C<PAD_SV>.
269 Get or set the value at offset C<po> in the current pad.
270 Unlike C<PAD_SV>, does not print diagnostics with -DX.
271 For internal use only.
273 =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
274 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
276 =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
277 Set the current pad to be pad C<n> in the padlist, saving
278 the previous current pad. NB currently this macro expands to a string too
279 long for some compilers, so it's best to replace it with
282 PAD_SET_CUR_NOSAVE(padlist,n);
285 =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n
286 like PAD_SET_CUR, but without the save
288 =for apidoc m|void|PAD_SAVE_SETNULLPAD
289 Save the current pad then set it to null.
291 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
292 Save the current pad to the local variable C<opad>, then make the
293 current pad equal to C<npad>
295 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
296 Restore the old pad saved into the local variable C<opad> by C<PAD_SAVE_LOCAL()>
301 #define PadlistARRAY(pl) (pl)->xpadl_arr.xpadlarr_alloc
302 #define PadlistMAX(pl) (pl)->xpadl_max
303 #define PadlistNAMES(pl) *((PADNAMELIST **)PadlistARRAY(pl))
304 #define PadlistNAMESARRAY(pl) PadnamelistARRAY(PadlistNAMES(pl))
305 #define PadlistNAMESMAX(pl) PadnamelistMAX(PadlistNAMES(pl))
306 #define PadlistREFCNT(pl) 1 /* reserved for future use */
308 #define PadnamelistARRAY(pnl) (pnl)->xpadnl_alloc
309 #define PadnamelistMAX(pnl) (pnl)->xpadnl_fill
310 #define PadnamelistMAXNAMED(pnl) (pnl)->xpadnl_max_named
311 #define PadnamelistREFCNT(pnl) (pnl)->xpadnl_refcnt
312 #define PadnamelistREFCNT_dec(pnl) Perl_padnamelist_free(aTHX_ pnl)
314 #define PadARRAY(pad) AvARRAY(pad)
315 #define PadMAX(pad) AvFILLp(pad)
317 #define PadnamePV(pn) (pn)->xpadn_pv
318 #define PadnameLEN(pn) (pn)->xpadn_len
319 #define PadnameUTF8(pn) 1
320 #define PadnameSV(pn) \
321 newSVpvn_flags(PadnamePV(pn), PadnameLEN(pn), SVs_TEMP|SVf_UTF8)
322 #define PadnameFLAGS(pn) (pn)->xpadn_flags
323 #define PadnameIsOUR(pn) cBOOL((pn)->xpadn_ourstash)
324 #define PadnameOURSTASH(pn) (pn)->xpadn_ourstash
325 #define PadnameTYPE(pn) (pn)->xpadn_type_u.xpadn_typestash
326 #define PadnameHasTYPE(pn) cBOOL(PadnameTYPE(pn))
327 #define PadnamePROTOCV(pn) (pn)->xpadn_type_u.xpadn_protocv
328 #define PadnameREFCNT(pn) (pn)->xpadn_refcnt
329 #define PadnameREFCNT_inc(pn) Perl_padname_refcnt_inc(pn)
330 #define PadnameREFCNT_dec(pn) Perl_padname_free(aTHX_ pn)
331 #define PadnameOURSTASH_set(pn,s) (PadnameOURSTASH(pn) = (s))
332 #define PadnameTYPE_set(pn,s) (PadnameTYPE(pn) = (s))
333 #define PadnameOUTER(pn) (PadnameFLAGS(pn) & PADNAMEf_OUTER)
334 #define PadnameIsSTATE(pn) (PadnameFLAGS(pn) & PADNAMEf_STATE)
335 #define PadnameLVALUE(pn) (PadnameFLAGS(pn) & PADNAMEf_LVALUE)
337 #define PadnameLVALUE_on(pn) (PadnameFLAGS(pn) |= PADNAMEf_LVALUE)
338 #define PadnameIsSTATE_on(pn) (PadnameFLAGS(pn) |= PADNAMEf_STATE)
340 #define PADNAMEf_OUTER 0x01 /* outer lexical var */
341 #define PADNAMEf_STATE 0x02 /* state var */
342 #define PADNAMEf_LVALUE 0x04 /* used as lvalue */
343 #define PADNAMEf_TYPED 0x08 /* for B; unused by core */
344 #define PADNAMEf_OUR 0x10 /* for B; unused by core */
346 /* backward compatibility */
348 # define SvPAD_STATE PadnameIsSTATE
349 # define SvPAD_TYPED PadnameHasTYPE
350 # define SvPAD_OUR(pn) cBOOL(PadnameOURSTASH(pn))
351 # define SvPAD_STATE_on PadnameIsSTATE_on
352 # define SvPAD_TYPED_on(pn) (PadnameFLAGS(pn) |= PADNAMEf_TYPED)
353 # define SvPAD_OUR_on(pn) (PadnameFLAGS(pn) |= PADNAMEf_OUR)
354 # define SvOURSTASH PadnameOURSTASH
355 # define SvOURSTASH_set PadnameOURSTASH_set
356 # define SVpad_STATE PADNAMEf_STATE
357 # define SVpad_TYPED PADNAMEf_TYPED
358 # define SVpad_OUR PADNAMEf_OUR
359 # define PADNAMEt_OUTER PADNAMEf_OUTER
360 # define PADNAMEt_STATE PADNAMEf_STATE
361 # define PADNAMEt_LVALUE PADNAMEf_LVALUE
362 # define PADNAMEt_TYPED PADNAMEf_TYPED
363 # define PADNAMEt_OUR PADNAMEf_OUR
367 # define PAD_SV(po) pad_sv(po)
368 # define PAD_SETSV(po,sv) pad_setsv(po,sv)
370 # define PAD_SV(po) (PL_curpad[po])
371 # define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
374 #define PAD_SVl(po) (PL_curpad[po])
376 #define PAD_BASE_SV(padlist, po) \
377 (PadlistARRAY(padlist)[1]) \
378 ? AvARRAY(MUTABLE_AV((PadlistARRAY(padlist)[1])))[po] \
382 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
383 PL_comppad = (PAD*) (PadlistARRAY(padlist)[nth]); \
384 PL_curpad = AvARRAY(PL_comppad); \
385 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
386 "Pad 0x%" UVxf "[0x%" UVxf "] set_cur depth=%d\n", \
387 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
390 #define PAD_SET_CUR(padlist,nth) \
392 PAD_SET_CUR_NOSAVE(padlist,nth);
395 #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \
396 PL_comppad = NULL; PL_curpad = NULL; \
397 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
399 #define PAD_SAVE_LOCAL(opad,npad) \
401 PL_comppad = (npad); \
402 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
403 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
404 "Pad 0x%" UVxf "[0x%" UVxf "] save_local\n", \
405 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
407 #define PAD_RESTORE_LOCAL(opad) \
408 assert(!opad || !SvIS_FREED(opad)); \
410 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
411 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
412 "Pad 0x%" UVxf "[0x%" UVxf "] restore_local\n", \
413 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
417 =for apidoc m|void|CX_CURPAD_SAVE|struct context
418 Save the current pad in the given context block structure.
420 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
421 Access the SV at offset C<po> in the saved current pad in the given
422 context block structure (can be used as an lvalue).
427 #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad
428 #define CX_CURPAD_SV(block,po) (AvARRAY(MUTABLE_AV(((block).oldcomppad)))[po])
432 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
433 Return the flags for the current compiling pad name
434 at offset C<po>. Assumes a valid slot entry.
436 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
437 Return the name of the current compiling pad name
438 at offset C<po>. Assumes a valid slot entry.
440 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
441 Return the type (stash) of the current compiling pad name at offset
442 C<po>. Must be a valid name. Returns null if not typed.
444 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
445 Return the stash associated with an C<our> variable.
446 Assumes the slot entry is a valid C<our> lexical.
448 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
449 The generation number of the name at offset C<po> in the current
450 compiling pad (lvalue).
452 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
453 Sets the generation number of the name at offset C<po> in the current
454 ling pad (lvalue) to C<gen>.
459 #define PAD_COMPNAME(po) PAD_COMPNAME_SV(po)
460 #define PAD_COMPNAME_SV(po) (PadnamelistARRAY(PL_comppad_name)[(po)])
461 #define PAD_COMPNAME_FLAGS(po) PadnameFLAGS(PAD_COMPNAME(po))
462 #define PAD_COMPNAME_FLAGS_isOUR(po) PadnameIsOUR(PAD_COMPNAME_SV(po))
463 #define PAD_COMPNAME_PV(po) PadnamePV(PAD_COMPNAME(po))
465 #define PAD_COMPNAME_TYPE(po) PadnameTYPE(PAD_COMPNAME(po))
467 #define PAD_COMPNAME_OURSTASH(po) (PadnameOURSTASH(PAD_COMPNAME_SV(po)))
469 #define PAD_COMPNAME_GEN(po) \
470 ((STRLEN)PadnamelistARRAY(PL_comppad_name)[po]->xpadn_gen)
472 #define PAD_COMPNAME_GEN_set(po, gen) \
473 (PadnamelistARRAY(PL_comppad_name)[po]->xpadn_gen = (gen))
477 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl|CLONE_PARAMS* param
478 Clone the state variables associated with running and compiling pads.
483 /* NB - we set PL_comppad to null unless it points at a value that
484 * has already been dup'ed, ie it points to part of an active padlist.
485 * Otherwise PL_comppad ends up being a leaked scalar in code like
487 * threads->create(sub { threads->create(sub {...} ) } );
488 * where the second thread dups the outer sub's comppad but not the
489 * sub's CV or padlist. */
491 #define PAD_CLONE_VARS(proto_perl, param) \
492 PL_comppad = av_dup(proto_perl->Icomppad, param); \
493 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
495 padnamelist_dup(proto_perl->Icomppad_name, param); \
496 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
497 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
498 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
499 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
500 PL_padix = proto_perl->Ipadix; \
501 PL_padix_floor = proto_perl->Ipadix_floor; \
502 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
503 PL_cop_seqmax = proto_perl->Icop_seqmax;
506 =for apidoc Am|PADOFFSET|pad_add_name_pvs|"name"|U32 flags|HV *typestash|HV *ourstash
508 Exactly like L</pad_add_name_pvn>, but takes a literal string
509 instead of a string/length pair.
514 #define pad_add_name_pvs(name,flags,typestash,ourstash) \
515 Perl_pad_add_name_pvn(aTHX_ STR_WITH_LEN(name), flags, typestash, ourstash)
518 =for apidoc Am|PADOFFSET|pad_findmy_pvs|"name"|U32 flags
520 Exactly like L</pad_findmy_pvn>, but takes a literal string
521 instead of a string/length pair.
526 #define pad_findmy_pvs(name,flags) \
527 Perl_pad_findmy_pvn(aTHX_ STR_WITH_LEN(name), flags)
529 struct suspended_compcv
532 STRLEN padix, constpadix;
533 STRLEN comppad_name_fill;
534 STRLEN min_intro_pending, max_intro_pending;
535 bool cv_has_eval, pad_reset_pending;
538 #define resume_compcv_final(buffer) Perl_resume_compcv(aTHX_ buffer, false)
539 #define resume_compcv_and_save(buffer) Perl_resume_compcv(aTHX_ buffer, true)
542 * ex: set ts=8 sts=4 sw=4 et: