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.
15 =head1 Pad Data Structures
19 /* offsets within a pad */
21 typedef SSize_t PADOFFSET; /* signed so that -1 is a valid value */
22 #define NOT_IN_PAD ((PADOFFSET) -1)
24 /* B.xs expects the first members of these two structs to line up
25 (xpadl_max with xpadnl_fill).
29 SSize_t xpadl_max; /* max index for which array has space */
31 PAD ** xpadlarr_alloc; /* Pointer to beginning of array of AVs.
32 index 0 is a padnamelist * */
35 PAD * pad_1; /* this slice of PAD * array always alloced */
36 PAD * pad_2; /* maybe unalloced */
37 } * xpadlarr_dbg; /* for use with a C debugger only */
39 U32 xpadl_id; /* Semi-unique ID, shared between clones */
40 U32 xpadl_outid; /* ID of outer pad */
44 SSize_t xpadnl_fill; /* max index in use */
45 PADNAME ** xpadnl_alloc; /* pointer to beginning of array */
46 SSize_t xpadnl_max; /* max index for which array has space */
47 PADOFFSET xpadnl_max_named; /* highest index with len > 0 */
51 /* PERL_PADNAME_MINIMAL uses less memory, but on some platforms
52 PERL_PADNAME_ALIGNED may be faster, so platform-specific hints can
53 define one or the other. */
54 #if defined(PERL_PADNAME_MINIMAL) && defined (PERL_PADNAME_ALIGNED)
55 # error PERL_PADNAME_MINIMAL and PERL_PADNAME_ALIGNED are exclusive
58 #if !defined(PERL_PADNAME_MINIMAL) && !defined(PERL_PADNAME_ALIGNED)
59 # define PERL_PADNAME_MINIMAL
62 #define _PADNAME_BASE \
64 HV * xpadn_ourstash; \
66 HV * xpadn_typestash; \
80 struct padname_with_str {
81 #ifdef PERL_PADNAME_MINIMAL
84 struct padname xpadn_padname;
91 #define PADNAME_FROM_PV(s) \
92 ((PADNAME *)((s) - STRUCT_OFFSET(struct padname_with_str, xpadn_str)))
95 /* a value that PL_cop_seqmax is guaranteed never to be,
96 * flagging that a lexical is being introduced, or has not yet left scope
98 #define PERL_PADSEQ_INTRO U32_MAX
99 #define COP_SEQMAX_INC \
101 (void)(PL_cop_seqmax == PERL_PADSEQ_INTRO && PL_cop_seqmax++))
104 /* B.xs needs these for the benefit of B::Deparse */
105 /* Low range end is exclusive (valid from the cop seq after this one) */
106 /* High range end is inclusive (valid up to this cop seq) */
108 #define COP_SEQ_RANGE_LOW(pn) (pn)->xpadn_low
109 #define COP_SEQ_RANGE_HIGH(pn) (pn)->xpadn_high
110 #define PARENT_PAD_INDEX(pn) (pn)->xpadn_low
111 #define PARENT_FAKELEX_FLAGS(pn) (pn)->xpadn_high
113 /* Flags set in the SvIVX field of FAKE namesvs */
115 #define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */
116 #define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */
118 /* flags for the pad_new() function */
120 #define padnew_CLONE 1 /* this pad is for a cloned CV */
121 #define padnew_SAVE 2 /* save old globals */
122 #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */
124 /* values for the pad_tidy() function */
127 padtidy_SUB, /* tidy up a pad for a sub, */
128 padtidy_SUBCLONE, /* a cloned sub, */
129 padtidy_FORMAT /* or a format */
132 /* flags for pad_add_name_pvn. */
134 #define padadd_OUR 0x01 /* our declaration. */
135 #define padadd_STATE 0x02 /* state declaration. */
136 #define padadd_NO_DUP_CHECK 0x04 /* skip warning on dups. */
137 #define padadd_STALEOK 0x08 /* allow stale lexical in active
138 * sub, but only one level up */
140 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
141 * whether PL_comppad and PL_curpad are consistent and whether they have
144 # define pad_peg(label)
147 # define ASSERT_CURPAD_LEGAL(label) \
149 if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \
150 Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%" UVxf "[0x%" UVxf "]",\
151 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
154 # define ASSERT_CURPAD_ACTIVE(label) \
156 if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \
157 Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%" UVxf "[0x%" UVxf "]",\
158 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
160 # define ASSERT_CURPAD_LEGAL(label)
161 # define ASSERT_CURPAD_ACTIVE(label)
166 /* Note: the following three macros are actually defined in scope.h, but
167 * they are documented here for completeness, since they directly or
168 * indirectly affect pads.
170 =for apidoc m|void|SAVEPADSV |PADOFFSET po
171 Save a pad slot (used to restore after an iteration)
173 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
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|void|PadnameREFCNT_dec|PADNAME pn
255 Lowers the reference count of the pad name.
258 =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
259 Set the slot at offset C<po> in the current pad to C<sv>
261 =for apidoc m|SV *|PAD_SV |PADOFFSET po
262 Get the value at offset C<po> in the current pad
264 =for apidoc m|SV *|PAD_SVl |PADOFFSET po
265 Lightweight and lvalue version of C<PAD_SV>.
266 Get or set the value at offset C<po> in the current pad.
267 Unlike C<PAD_SV>, does not print diagnostics with -DX.
268 For internal use only.
270 =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
271 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
273 =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
274 Set the current pad to be pad C<n> in the padlist, saving
275 the previous current pad. NB currently this macro expands to a string too
276 long for some compilers, so it's best to replace it with
279 PAD_SET_CUR_NOSAVE(padlist,n);
282 =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n
283 like PAD_SET_CUR, but without the save
285 =for apidoc m|void|PAD_SAVE_SETNULLPAD
286 Save the current pad then set it to null.
288 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
289 Save the current pad to the local variable C<opad>, then make the
290 current pad equal to C<npad>
292 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
293 Restore the old pad saved into the local variable C<opad> by C<PAD_SAVE_LOCAL()>
298 #define PadlistARRAY(pl) (pl)->xpadl_arr.xpadlarr_alloc
299 #define PadlistMAX(pl) (pl)->xpadl_max
300 #define PadlistNAMES(pl) *((PADNAMELIST **)PadlistARRAY(pl))
301 #define PadlistNAMESARRAY(pl) PadnamelistARRAY(PadlistNAMES(pl))
302 #define PadlistNAMESMAX(pl) PadnamelistMAX(PadlistNAMES(pl))
303 #define PadlistREFCNT(pl) 1 /* reserved for future use */
305 #define PadnamelistARRAY(pnl) (pnl)->xpadnl_alloc
306 #define PadnamelistMAX(pnl) (pnl)->xpadnl_fill
307 #define PadnamelistMAXNAMED(pnl) (pnl)->xpadnl_max_named
308 #define PadnamelistREFCNT(pnl) (pnl)->xpadnl_refcnt
309 #define PadnamelistREFCNT_dec(pnl) Perl_padnamelist_free(aTHX_ pnl)
311 #define PadARRAY(pad) AvARRAY(pad)
312 #define PadMAX(pad) AvFILLp(pad)
314 #define PadnamePV(pn) (pn)->xpadn_pv
315 #define PadnameLEN(pn) (pn)->xpadn_len
316 #define PadnameUTF8(pn) 1
317 #define PadnameSV(pn) \
318 newSVpvn_flags(PadnamePV(pn), PadnameLEN(pn), SVs_TEMP|SVf_UTF8)
319 #define PadnameFLAGS(pn) (pn)->xpadn_flags
320 #define PadnameIsOUR(pn) (!!(pn)->xpadn_ourstash)
321 #define PadnameOURSTASH(pn) (pn)->xpadn_ourstash
322 #define PadnameTYPE(pn) (pn)->xpadn_type_u.xpadn_typestash
323 #define PadnamePROTOCV(pn) (pn)->xpadn_type_u.xpadn_protocv
324 #define PadnameREFCNT(pn) (pn)->xpadn_refcnt
325 #define PadnameREFCNT_dec(pn) Perl_padname_free(aTHX_ pn)
326 #define PadnameOURSTASH_set(pn,s) (PadnameOURSTASH(pn) = (s))
327 #define PadnameTYPE_set(pn,s) (PadnameTYPE(pn) = (s))
328 #define PadnameOUTER(pn) (PadnameFLAGS(pn) & PADNAMEt_OUTER)
329 #define PadnameIsSTATE(pn) (PadnameFLAGS(pn) & PADNAMEt_STATE)
330 #define PadnameLVALUE(pn) (PadnameFLAGS(pn) & PADNAMEt_LVALUE)
332 #define PadnameLVALUE_on(pn) (PadnameFLAGS(pn) |= PADNAMEt_LVALUE)
333 #define PadnameIsSTATE_on(pn) (PadnameFLAGS(pn) |= PADNAMEt_STATE)
335 #define PADNAMEt_OUTER 1 /* outer lexical var */
336 #define PADNAMEt_STATE 2 /* state var */
337 #define PADNAMEt_LVALUE 4 /* used as lvalue */
338 #define PADNAMEt_TYPED 8 /* for B; unused by core */
339 #define PADNAMEt_OUR 16 /* for B; unused by core */
341 /* backward compatibility */
342 #define SvPAD_STATE PadnameIsSTATE
343 #define SvPAD_TYPED(pn) (!!PadnameTYPE(pn))
344 #define SvPAD_OUR(pn) (!!PadnameOURSTASH(pn))
345 #define SvPAD_STATE_on PadnameIsSTATE_on
346 #define SvPAD_TYPED_on(pn) (PadnameFLAGS(pn) |= PADNAMEt_TYPED)
347 #define SvPAD_OUR_on(pn) (PadnameFLAGS(pn) |= PADNAMEt_OUR)
348 #define SvOURSTASH PadnameOURSTASH
349 #define SvOURSTASH_set PadnameOURSTASH_set
350 #define SVpad_STATE PADNAMEt_STATE
351 #define SVpad_TYPED PADNAMEt_TYPED
352 #define SVpad_OUR PADNAMEt_OUR
355 # define PAD_SV(po) pad_sv(po)
356 # define PAD_SETSV(po,sv) pad_setsv(po,sv)
358 # define PAD_SV(po) (PL_curpad[po])
359 # define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
362 #define PAD_SVl(po) (PL_curpad[po])
364 #define PAD_BASE_SV(padlist, po) \
365 (PadlistARRAY(padlist)[1]) \
366 ? AvARRAY(MUTABLE_AV((PadlistARRAY(padlist)[1])))[po] \
370 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
371 PL_comppad = (PAD*) (PadlistARRAY(padlist)[nth]); \
372 PL_curpad = AvARRAY(PL_comppad); \
373 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
374 "Pad 0x%" UVxf "[0x%" UVxf "] set_cur depth=%d\n", \
375 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
378 #define PAD_SET_CUR(padlist,nth) \
380 PAD_SET_CUR_NOSAVE(padlist,nth);
383 #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \
384 PL_comppad = NULL; PL_curpad = NULL; \
385 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
387 #define PAD_SAVE_LOCAL(opad,npad) \
389 PL_comppad = (npad); \
390 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
391 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
392 "Pad 0x%" UVxf "[0x%" UVxf "] save_local\n", \
393 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
395 #define PAD_RESTORE_LOCAL(opad) \
396 assert(!opad || !SvIS_FREED(opad)); \
398 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
399 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
400 "Pad 0x%" UVxf "[0x%" UVxf "] restore_local\n", \
401 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
405 =for apidoc m|void|CX_CURPAD_SAVE|struct context
406 Save the current pad in the given context block structure.
408 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
409 Access the SV at offset C<po> in the saved current pad in the given
410 context block structure (can be used as an lvalue).
415 #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad
416 #define CX_CURPAD_SV(block,po) (AvARRAY(MUTABLE_AV(((block).oldcomppad)))[po])
420 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
421 Return the flags for the current compiling pad name
422 at offset C<po>. Assumes a valid slot entry.
424 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
425 Return the name of the current compiling pad name
426 at offset C<po>. Assumes a valid slot entry.
428 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
429 Return the type (stash) of the current compiling pad name at offset
430 C<po>. Must be a valid name. Returns null if not typed.
432 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
433 Return the stash associated with an C<our> variable.
434 Assumes the slot entry is a valid C<our> lexical.
436 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
437 The generation number of the name at offset C<po> in the current
438 compiling pad (lvalue).
440 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
441 Sets the generation number of the name at offset C<po> in the current
442 ling pad (lvalue) to C<gen>.
447 #define PAD_COMPNAME(po) PAD_COMPNAME_SV(po)
448 #define PAD_COMPNAME_SV(po) (PadnamelistARRAY(PL_comppad_name)[(po)])
449 #define PAD_COMPNAME_FLAGS(po) PadnameFLAGS(PAD_COMPNAME(po))
450 #define PAD_COMPNAME_FLAGS_isOUR(po) SvPAD_OUR(PAD_COMPNAME_SV(po))
451 #define PAD_COMPNAME_PV(po) PadnamePV(PAD_COMPNAME(po))
453 #define PAD_COMPNAME_TYPE(po) PadnameTYPE(PAD_COMPNAME(po))
455 #define PAD_COMPNAME_OURSTASH(po) \
456 (SvOURSTASH(PAD_COMPNAME_SV(po)))
458 #define PAD_COMPNAME_GEN(po) \
459 ((STRLEN)PadnamelistARRAY(PL_comppad_name)[po]->xpadn_gen)
461 #define PAD_COMPNAME_GEN_set(po, gen) \
462 (PadnamelistARRAY(PL_comppad_name)[po]->xpadn_gen = (gen))
466 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl|CLONE_PARAMS* param
467 Clone the state variables associated with running and compiling pads.
472 /* NB - we set PL_comppad to null unless it points at a value that
473 * has already been dup'ed, ie it points to part of an active padlist.
474 * Otherwise PL_comppad ends up being a leaked scalar in code like
476 * threads->create(sub { threads->create(sub {...} ) } );
477 * where the second thread dups the outer sub's comppad but not the
478 * sub's CV or padlist. */
480 #define PAD_CLONE_VARS(proto_perl, param) \
481 PL_comppad = av_dup(proto_perl->Icomppad, param); \
482 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
484 padnamelist_dup(proto_perl->Icomppad_name, param); \
485 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
486 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
487 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
488 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
489 PL_padix = proto_perl->Ipadix; \
490 PL_padix_floor = proto_perl->Ipadix_floor; \
491 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
492 PL_cop_seqmax = proto_perl->Icop_seqmax;
495 =for apidoc Am|PADOFFSET|pad_add_name_pvs|const char *name|U32 flags|HV *typestash|HV *ourstash
497 Exactly like L</pad_add_name_pvn>, but takes a C<NUL>-terminated literal string
498 instead of a string/length pair.
503 #define pad_add_name_pvs(name,flags,typestash,ourstash) \
504 Perl_pad_add_name_pvn(aTHX_ STR_WITH_LEN(name), flags, typestash, ourstash)
507 =for apidoc Am|PADOFFSET|pad_findmy_pvs|const char *name|U32 flags
509 Exactly like L</pad_findmy_pvn>, but takes a C<NUL>-terminated literal string
510 instead of a string/length pair.
515 #define pad_findmy_pvs(name,flags) \
516 Perl_pad_findmy_pvn(aTHX_ STR_WITH_LEN(name), flags)
519 * ex: set ts=8 sts=4 sw=4 et: