3 * Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008 by Larry Wall and others
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
8 * This file defines the types and macros associated with the API for
9 * manipulating scratchpads, which are used by perl to store lexical
10 * variables, op targets and constants.
14 =head1 Pad Data Structures
18 /* a padlist is currently just an AV; but that might change,
19 * so hide the type. Ditto a pad. */
25 /* offsets within a pad */
28 typedef U32TYPE PADOFFSET;
31 typedef U64TYPE PADOFFSET;
34 #define NOT_IN_PAD ((PADOFFSET) -1)
36 /* a value that PL_cop_seqmax is guaranteed never to be,
37 * flagging that a lexical is being introduced, or has not yet left scope
39 #define PERL_PADSEQ_INTRO U32_MAX
42 /* B.xs needs these for the benefit of B::Deparse */
43 /* Low range end is exclusive (valid from the cop seq after this one) */
44 /* High range end is inclusive (valid up to this cop seq) */
46 #if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
47 # define COP_SEQ_RANGE_LOW(sv) \
48 (({ const SV *const _sv_cop_seq_range_low = (const SV *) (sv); \
49 assert(SvTYPE(_sv_cop_seq_range_low) == SVt_NV \
50 || SvTYPE(_sv_cop_seq_range_low) >= SVt_PVNV); \
51 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVAV); \
52 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVHV); \
53 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVCV); \
54 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVFM); \
55 assert(!isGV_with_GP(_sv_cop_seq_range_low)); \
56 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_low)))->xnv_u.xpad_cop_seq.xlow; \
58 # define COP_SEQ_RANGE_HIGH(sv) \
59 (({ const SV *const _sv_cop_seq_range_high = (const SV *) (sv); \
60 assert(SvTYPE(_sv_cop_seq_range_high) == SVt_NV \
61 || SvTYPE(_sv_cop_seq_range_high) >= SVt_PVNV); \
62 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVAV); \
63 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVHV); \
64 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVCV); \
65 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVFM); \
66 assert(!isGV_with_GP(_sv_cop_seq_range_high)); \
67 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_high)))->xnv_u.xpad_cop_seq.xhigh; \
69 # define PARENT_PAD_INDEX(sv) \
70 (({ const SV *const _sv_parent_pad_index = (const SV *) (sv); \
71 assert(SvTYPE(_sv_parent_pad_index) == SVt_NV \
72 || SvTYPE(_sv_parent_pad_index) >= SVt_PVNV); \
73 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVAV); \
74 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVHV); \
75 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVCV); \
76 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVFM); \
77 assert(!isGV_with_GP(_sv_parent_pad_index)); \
78 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_pad_index)))->xnv_u.xpad_cop_seq.xlow; \
80 # define PARENT_FAKELEX_FLAGS(sv) \
81 (({ const SV *const _sv_parent_fakelex_flags = (const SV *) (sv); \
82 assert(SvTYPE(_sv_parent_fakelex_flags) == SVt_NV \
83 || SvTYPE(_sv_parent_fakelex_flags) >= SVt_PVNV); \
84 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVAV); \
85 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVHV); \
86 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVCV); \
87 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVFM); \
88 assert(!isGV_with_GP(_sv_parent_fakelex_flags)); \
89 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_fakelex_flags)))->xnv_u.xpad_cop_seq.xhigh; \
92 # define COP_SEQ_RANGE_LOW(sv) \
93 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
94 # define COP_SEQ_RANGE_HIGH(sv) \
95 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
98 # define PARENT_PAD_INDEX(sv) \
99 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
100 # define PARENT_FAKELEX_FLAGS(sv) \
101 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
104 /* Flags set in the SvIVX field of FAKE namesvs */
106 #define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */
107 #define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */
109 /* flags for the pad_new() function */
111 #define padnew_CLONE 1 /* this pad is for a cloned CV */
112 #define padnew_SAVE 2 /* save old globals */
113 #define padnew_SAVESUB 4 /* also save extra stuff for start of sub */
115 /* values for the pad_tidy() function */
118 padtidy_SUB, /* tidy up a pad for a sub, */
119 padtidy_SUBCLONE, /* a cloned sub, */
120 padtidy_FORMAT /* or a format */
123 /* flags for pad_add_name_pvn. SVf_UTF8 will also be valid in the future. */
125 #define padadd_OUR 0x01 /* our declaration. */
126 #define padadd_STATE 0x02 /* state declaration. */
127 #define padadd_NO_DUP_CHECK 0x04 /* skip warning on dups. */
129 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
130 * whether PL_comppad and PL_curpad are consistent and whether they have
134 # define pad_peg(label)
138 # define ASSERT_CURPAD_LEGAL(label) \
140 if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \
141 Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
142 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
145 # define ASSERT_CURPAD_ACTIVE(label) \
147 if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \
148 Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
149 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
151 # define ASSERT_CURPAD_LEGAL(label)
152 # define ASSERT_CURPAD_ACTIVE(label)
157 /* Note: the following three macros are actually defined in scope.h, but
158 * they are documented here for completeness, since they directly or
159 * indirectly affect pads.
161 =for apidoc m|void|SAVEPADSV |PADOFFSET po
162 Save a pad slot (used to restore after an iteration)
164 XXX DAPM it would make more sense to make the arg a PADOFFSET
165 =for apidoc m|void|SAVECLEARSV |SV **svp
166 Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my')
168 =for apidoc m|void|SAVECOMPPAD
169 save PL_comppad and PL_curpad
175 =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
176 Set the slot at offset C<po> in the current pad to C<sv>
178 =for apidoc m|void|PAD_SV |PADOFFSET po
179 Get the value at offset C<po> in the current pad
181 =for apidoc m|SV *|PAD_SVl |PADOFFSET po
182 Lightweight and lvalue version of C<PAD_SV>.
183 Get or set the value at offset C<po> in the current pad.
184 Unlike C<PAD_SV>, does not print diagnostics with -DX.
185 For internal use only.
187 =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
188 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
190 =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
191 Set the current pad to be pad C<n> in the padlist, saving
192 the previous current pad. NB currently this macro expands to a string too
193 long for some compilers, so it's best to replace it with
196 PAD_SET_CUR_NOSAVE(padlist,n);
199 =for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n
200 like PAD_SET_CUR, but without the save
202 =for apidoc m|void|PAD_SAVE_SETNULLPAD
203 Save the current pad then set it to null.
205 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
206 Save the current pad to the local variable opad, then make the
207 current pad equal to npad
209 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
210 Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
216 # define PAD_SV(po) pad_sv(po)
217 # define PAD_SETSV(po,sv) pad_setsv(po,sv)
219 # define PAD_SV(po) (PL_curpad[po])
220 # define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
223 #define PAD_SVl(po) (PL_curpad[po])
225 #define PAD_BASE_SV(padlist, po) \
226 (AvARRAY(padlist)[1]) \
227 ? AvARRAY(MUTABLE_AV((AvARRAY(padlist)[1])))[po] : NULL;
230 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
231 PL_comppad = (PAD*) (AvARRAY(padlist)[nth]); \
232 PL_curpad = AvARRAY(PL_comppad); \
233 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
234 "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \
235 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
238 #define PAD_SET_CUR(padlist,nth) \
240 PAD_SET_CUR_NOSAVE(padlist,nth);
243 #define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \
244 PL_comppad = NULL; PL_curpad = NULL; \
245 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
247 #define PAD_SAVE_LOCAL(opad,npad) \
249 PL_comppad = (npad); \
250 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
251 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
252 "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \
253 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
255 #define PAD_RESTORE_LOCAL(opad) \
256 assert(!opad || !SvIS_FREED(opad)); \
258 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
259 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
260 "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \
261 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
265 =for apidoc m|void|CX_CURPAD_SAVE|struct context
266 Save the current pad in the given context block structure.
268 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
269 Access the SV at offset po in the saved current pad in the given
270 context block structure (can be used as an lvalue).
275 #define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad
276 #define CX_CURPAD_SV(block,po) (AvARRAY(MUTABLE_AV(((block).oldcomppad)))[po])
280 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
281 Return the flags for the current compiling pad name
282 at offset C<po>. Assumes a valid slot entry.
284 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
285 Return the name of the current compiling pad name
286 at offset C<po>. Assumes a valid slot entry.
288 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
289 Return the type (stash) of the current compiling pad name at offset
290 C<po>. Must be a valid name. Returns null if not typed.
292 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
293 Return the stash associated with an C<our> variable.
294 Assumes the slot entry is a valid C<our> lexical.
296 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
297 The generation number of the name at offset C<po> in the current
298 compiling pad (lvalue). Note that C<SvUVX> is hijacked for this purpose.
300 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
301 Sets the generation number of the name at offset C<po> in the current
302 ling pad (lvalue) to C<gen>. Note that C<SvUV_set> is hijacked for this purpose.
308 #define PAD_COMPNAME_SV(po) (*av_fetch(PL_comppad_name, (po), FALSE))
309 #define PAD_COMPNAME_FLAGS(po) SvFLAGS(PAD_COMPNAME_SV(po))
310 #define PAD_COMPNAME_FLAGS_isOUR(po) SvPAD_OUR(PAD_COMPNAME_SV(po))
311 #define PAD_COMPNAME_PV(po) SvPV_nolen(PAD_COMPNAME_SV(po))
313 #define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
315 #define PAD_COMPNAME_OURSTASH(po) \
316 (SvOURSTASH(PAD_COMPNAME_SV(po)))
318 #define PAD_COMPNAME_GEN(po) ((STRLEN)SvUVX(AvARRAY(PL_comppad_name)[po]))
320 #define PAD_COMPNAME_GEN_set(po, gen) SvUV_set(AvARRAY(PL_comppad_name)[po], (UV)(gen))
324 =for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param
327 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl|CLONE_PARAMS* param
328 Clone the state variables associated with running and compiling pads.
334 #define PAD_DUP(dstpad, srcpad, param) \
335 if ((srcpad) && !AvREAL(srcpad)) { \
336 /* XXX padlists are real, but pretend to be not */ \
338 (dstpad) = av_dup_inc((srcpad), param); \
339 AvREAL_off(srcpad); \
340 AvREAL_off(dstpad); \
343 (dstpad) = av_dup_inc((srcpad), param);
345 /* NB - we set PL_comppad to null unless it points at a value that
346 * has already been dup'ed, ie it points to part of an active padlist.
347 * Otherwise PL_comppad ends up being a leaked scalar in code like
349 * threads->create(sub { threads->create(sub {...} ) } );
350 * where the second thread dups the outer sub's comppad but not the
351 * sub's CV or padlist. */
353 #define PAD_CLONE_VARS(proto_perl, param) \
354 PL_comppad = MUTABLE_AV(ptr_table_fetch(PL_ptr_table, proto_perl->Icomppad)); \
355 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
356 PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \
357 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
358 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
359 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
360 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
361 PL_padix = proto_perl->Ipadix; \
362 PL_padix_floor = proto_perl->Ipadix_floor; \
363 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
364 PL_cop_seqmax = proto_perl->Icop_seqmax;
367 =for apidoc Am|PADOFFSET|pad_add_name_pvs|const char *name|U32 flags|HV *typestash|HV *ourstash
369 Exactly like L</pad_add_name_pvn>, but takes a literal string instead
370 of a string/length pair.
375 #define pad_add_name_pvs(name,flags,typestash,ourstash) \
376 Perl_pad_add_name_pvn(aTHX_ STR_WITH_LEN(name), flags, typestash, ourstash)
379 =for apidoc Am|PADOFFSET|pad_findmy_pvs|const char *name|U32 flags
381 Exactly like L</pad_findmy_pvn>, but takes a literal string instead
382 of a string/length pair.
387 #define pad_findmy_pvs(name,flags) \
388 Perl_pad_findmy_pvn(aTHX_ STR_WITH_LEN(name), flags)
392 * c-indentation-style: bsd
394 * indent-tabs-mode: t
397 * ex: set ts=8 sts=4 sw=4 noet: