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 */
22 typedef U32TYPE PADOFFSET;
25 typedef U64TYPE PADOFFSET;
28 #define NOT_IN_PAD ((PADOFFSET) -1)
32 SSize_t xpadl_max; /* max index for which array has space */
33 PAD ** xpadl_alloc; /* pointer to beginning of array of AVs */
34 U32 xpadl_id; /* Semi-unique ID, shared between clones */
35 U32 xpadl_outid; /* ID of outer pad */
39 /* a value that PL_cop_seqmax is guaranteed never to be,
40 * flagging that a lexical is being introduced, or has not yet left scope
42 #define PERL_PADSEQ_INTRO U32_MAX
45 /* B.xs needs these for the benefit of B::Deparse */
46 /* Low range end is exclusive (valid from the cop seq after this one) */
47 /* High range end is inclusive (valid up to this cop seq) */
49 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
50 # define COP_SEQ_RANGE_LOW(sv) \
51 (({ const SV *const _sv_cop_seq_range_low = (const SV *) (sv); \
52 assert(SvTYPE(_sv_cop_seq_range_low) == SVt_NV \
53 || SvTYPE(_sv_cop_seq_range_low) >= SVt_PVNV); \
54 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVAV); \
55 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVHV); \
56 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVCV); \
57 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVFM); \
58 assert(!isGV_with_GP(_sv_cop_seq_range_low)); \
59 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_low)))->xnv_u.xpad_cop_seq.xlow; \
61 # define COP_SEQ_RANGE_HIGH(sv) \
62 (({ const SV *const _sv_cop_seq_range_high = (const SV *) (sv); \
63 assert(SvTYPE(_sv_cop_seq_range_high) == SVt_NV \
64 || SvTYPE(_sv_cop_seq_range_high) >= SVt_PVNV); \
65 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVAV); \
66 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVHV); \
67 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVCV); \
68 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVFM); \
69 assert(!isGV_with_GP(_sv_cop_seq_range_high)); \
70 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_high)))->xnv_u.xpad_cop_seq.xhigh; \
72 # define PARENT_PAD_INDEX(sv) \
73 (({ const SV *const _sv_parent_pad_index = (const SV *) (sv); \
74 assert(SvTYPE(_sv_parent_pad_index) == SVt_NV \
75 || SvTYPE(_sv_parent_pad_index) >= SVt_PVNV); \
76 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVAV); \
77 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVHV); \
78 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVCV); \
79 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVFM); \
80 assert(!isGV_with_GP(_sv_parent_pad_index)); \
81 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_pad_index)))->xnv_u.xpad_cop_seq.xlow; \
83 # define PARENT_FAKELEX_FLAGS(sv) \
84 (({ const SV *const _sv_parent_fakelex_flags = (const SV *) (sv); \
85 assert(SvTYPE(_sv_parent_fakelex_flags) == SVt_NV \
86 || SvTYPE(_sv_parent_fakelex_flags) >= SVt_PVNV); \
87 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVAV); \
88 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVHV); \
89 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVCV); \
90 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVFM); \
91 assert(!isGV_with_GP(_sv_parent_fakelex_flags)); \
92 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_fakelex_flags)))->xnv_u.xpad_cop_seq.xhigh; \
95 # define COP_SEQ_RANGE_LOW(sv) \
96 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
97 # define COP_SEQ_RANGE_HIGH(sv) \
98 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
101 # define PARENT_PAD_INDEX(sv) \
102 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
103 # define PARENT_FAKELEX_FLAGS(sv) \
104 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
107 /* Flags set in the SvIVX field of FAKE namesvs */
109 #define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */
110 #define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */
112 /* flags for the pad_new() function */
114 #define padnew_CLONE 1 /* this pad is for a cloned CV */
115 #define padnew_SAVE 2 /* save old globals */
116 #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */
118 /* values for the pad_tidy() function */
121 padtidy_SUB, /* tidy up a pad for a sub, */
122 padtidy_SUBCLONE, /* a cloned sub, */
123 padtidy_FORMAT /* or a format */
126 /* flags for pad_add_name_pvn. */
128 #define padadd_OUR 0x01 /* our declaration. */
129 #define padadd_STATE 0x02 /* state declaration. */
130 #define padadd_NO_DUP_CHECK 0x04 /* skip warning on dups. */
131 #define padadd_STALEOK 0x08 /* allow stale lexical in active
132 * sub, but only one level up */
133 #define padadd_UTF8_NAME SVf_UTF8 /* name is UTF-8 encoded. */
135 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
136 * whether PL_comppad and PL_curpad are consistent and whether they have
140 # define pad_peg(label)
144 # define ASSERT_CURPAD_LEGAL(label) \
146 if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \
147 Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
148 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
151 # define ASSERT_CURPAD_ACTIVE(label) \
153 if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \
154 Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
155 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
157 # define ASSERT_CURPAD_LEGAL(label)
158 # define ASSERT_CURPAD_ACTIVE(label)
163 /* Note: the following three macros are actually defined in scope.h, but
164 * they are documented here for completeness, since they directly or
165 * indirectly affect pads.
167 =for apidoc m|void|SAVEPADSV |PADOFFSET po
168 Save a pad slot (used to restore after an iteration)
170 XXX DAPM it would make more sense to make the arg a PADOFFSET
171 =for apidoc m|void|SAVECLEARSV |SV **svp
172 Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my')
174 =for apidoc m|void|SAVECOMPPAD
175 save PL_comppad and PL_curpad
181 =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
182 Set the slot at offset C<po> in the current pad to C<sv>
184 =for apidoc m|void|PAD_SV |PADOFFSET po
185 Get the value at offset C<po> in the current pad
187 =for apidoc m|SV *|PAD_SVl |PADOFFSET po
188 Lightweight and lvalue version of C<PAD_SV>.
189 Get or set the value at offset C<po> in the current pad.
190 Unlike C<PAD_SV>, does not print diagnostics with -DX.
191 For internal use only.
193 =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
194 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
196 =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
197 Set the current pad to be pad C<n> in the padlist, saving
198 the previous current pad. NB currently this macro expands to a string too
199 long for some compilers, so it's best to replace it with
202 PAD_SET_CUR_NOSAVE(padlist,n);
205 =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n
206 like PAD_SET_CUR, but without the save
208 =for apidoc m|void|PAD_SAVE_SETNULLPAD
209 Save the current pad then set it to null.
211 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
212 Save the current pad to the local variable opad, then make the
213 current pad equal to npad
215 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
216 Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
221 #define PADLIST_ARRAY(pl) (pl)->xpadl_alloc
222 #define PADLIST_MAX(pl) (pl)->xpadl_max
223 #define PADLIST_REFCNT(pl) 1 /* reserved for future use */
226 # define PAD_SV(po) pad_sv(po)
227 # define PAD_SETSV(po,sv) pad_setsv(po,sv)
229 # define PAD_SV(po) (PL_curpad[po])
230 # define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
233 #define PAD_SVl(po) (PL_curpad[po])
235 #define PAD_BASE_SV(padlist, po) \
236 (PADLIST_ARRAY(padlist)[1]) \
237 ? AvARRAY(MUTABLE_AV((PADLIST_ARRAY(padlist)[1])))[po] \
241 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
242 PL_comppad = (PAD*) (PADLIST_ARRAY(padlist)[nth]); \
243 PL_curpad = AvARRAY(PL_comppad); \
244 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
245 "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \
246 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
249 #define PAD_SET_CUR(padlist,nth) \
251 PAD_SET_CUR_NOSAVE(padlist,nth);
254 #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \
255 PL_comppad = NULL; PL_curpad = NULL; \
256 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
258 #define PAD_SAVE_LOCAL(opad,npad) \
260 PL_comppad = (npad); \
261 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
262 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
263 "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \
264 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
266 #define PAD_RESTORE_LOCAL(opad) \
267 assert(!opad || !SvIS_FREED(opad)); \
269 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
270 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
271 "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \
272 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
276 =for apidoc m|void|CX_CURPAD_SAVE|struct context
277 Save the current pad in the given context block structure.
279 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
280 Access the SV at offset po in the saved current pad in the given
281 context block structure (can be used as an lvalue).
286 #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad
287 #define CX_CURPAD_SV(block,po) (AvARRAY(MUTABLE_AV(((block).oldcomppad)))[po])
291 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
292 Return the flags for the current compiling pad name
293 at offset C<po>. Assumes a valid slot entry.
295 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
296 Return the name of the current compiling pad name
297 at offset C<po>. Assumes a valid slot entry.
299 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
300 Return the type (stash) of the current compiling pad name at offset
301 C<po>. Must be a valid name. Returns null if not typed.
303 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
304 Return the stash associated with an C<our> variable.
305 Assumes the slot entry is a valid C<our> lexical.
307 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
308 The generation number of the name at offset C<po> in the current
309 compiling pad (lvalue). Note that C<SvUVX> is hijacked for this purpose.
311 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
312 Sets the generation number of the name at offset C<po> in the current
313 ling pad (lvalue) to C<gen>. Note that C<SvUV_set> is hijacked for this purpose.
319 #define PAD_COMPNAME_SV(po) (*av_fetch(PL_comppad_name, (po), FALSE))
320 #define PAD_COMPNAME_FLAGS(po) SvFLAGS(PAD_COMPNAME_SV(po))
321 #define PAD_COMPNAME_FLAGS_isOUR(po) SvPAD_OUR(PAD_COMPNAME_SV(po))
322 #define PAD_COMPNAME_PV(po) SvPV_nolen(PAD_COMPNAME_SV(po))
324 #define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
326 #define PAD_COMPNAME_OURSTASH(po) \
327 (SvOURSTASH(PAD_COMPNAME_SV(po)))
329 #define PAD_COMPNAME_GEN(po) ((STRLEN)SvUVX(AvARRAY(PL_comppad_name)[po]))
331 #define PAD_COMPNAME_GEN_set(po, gen) SvUV_set(AvARRAY(PL_comppad_name)[po], (UV)(gen))
335 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl|CLONE_PARAMS* param
336 Clone the state variables associated with running and compiling pads.
341 /* NB - we set PL_comppad to null unless it points at a value that
342 * has already been dup'ed, ie it points to part of an active padlist.
343 * Otherwise PL_comppad ends up being a leaked scalar in code like
345 * threads->create(sub { threads->create(sub {...} ) } );
346 * where the second thread dups the outer sub's comppad but not the
347 * sub's CV or padlist. */
349 #define PAD_CLONE_VARS(proto_perl, param) \
350 PL_comppad = av_dup(proto_perl->Icomppad, param); \
351 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
352 PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \
353 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
354 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
355 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
356 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
357 PL_padix = proto_perl->Ipadix; \
358 PL_padix_floor = proto_perl->Ipadix_floor; \
359 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
360 PL_cop_seqmax = proto_perl->Icop_seqmax;
363 =for apidoc Am|PADOFFSET|pad_add_name_pvs|const char *name|U32 flags|HV *typestash|HV *ourstash
365 Exactly like L</pad_add_name_pvn>, but takes a literal string instead
366 of a string/length pair.
371 #define pad_add_name_pvs(name,flags,typestash,ourstash) \
372 Perl_pad_add_name_pvn(aTHX_ STR_WITH_LEN(name), flags, typestash, ourstash)
375 =for apidoc Am|PADOFFSET|pad_findmy_pvs|const char *name|U32 flags
377 Exactly like L</pad_findmy_pvn>, but takes a literal string instead
378 of a string/length pair.
383 #define pad_findmy_pvs(name,flags) \
384 Perl_pad_findmy_pvn(aTHX_ STR_WITH_LEN(name), flags)
388 * c-indentation-style: bsd
390 * indent-tabs-mode: nil
393 * ex: set ts=8 sts=4 sw=4 et: