This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Use PADLIST in more places
[perl5.git] / pad.h
CommitLineData
dd2155a4
DM
1/* pad.h
2 *
2eee27d7
SS
3 * Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008,
4 * 2009, 2010, 2011 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.
8 *
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.
12 */
13
cc76b5cc
Z
14/*
15=head1 Pad Data Structures
16*/
dd2155a4
DM
17
18
dd2155a4
DM
19/* offsets within a pad */
20
21#if PTRSIZE == 4
22typedef U32TYPE PADOFFSET;
23#else
24# if PTRSIZE == 8
25typedef U64TYPE PADOFFSET;
26# endif
27#endif
28#define NOT_IN_PAD ((PADOFFSET) -1)
809abb02 29
2df5bdd7
DM
30/* a value that PL_cop_seqmax is guaranteed never to be,
31 * flagging that a lexical is being introduced, or has not yet left scope
32 */
33#define PERL_PADSEQ_INTRO U32_MAX
34
35
7948fc08 36/* B.xs needs these for the benefit of B::Deparse */
809abb02 37/* Low range end is exclusive (valid from the cop seq after this one) */
809abb02 38/* High range end is inclusive (valid up to this cop seq) */
809abb02 39
3441fb63
NC
40#if defined (DEBUGGING) && defined(__GNUC__) && !defined(PERL_GCC_BRACE_GROUPS_FORBIDDEN)
41# define COP_SEQ_RANGE_LOW(sv) \
b1bc3f34 42 (({ const SV *const _sv_cop_seq_range_low = (const SV *) (sv); \
7948fc08
RU
43 assert(SvTYPE(_sv_cop_seq_range_low) == SVt_NV \
44 || SvTYPE(_sv_cop_seq_range_low) >= SVt_PVNV); \
45 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVAV); \
46 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVHV); \
47 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVCV); \
48 assert(SvTYPE(_sv_cop_seq_range_low) != SVt_PVFM); \
49 assert(!isGV_with_GP(_sv_cop_seq_range_low)); \
b1bc3f34 50 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_low)))->xnv_u.xpad_cop_seq.xlow; \
3441fb63
NC
51 }))
52# define COP_SEQ_RANGE_HIGH(sv) \
b1bc3f34 53 (({ const SV *const _sv_cop_seq_range_high = (const SV *) (sv); \
7948fc08
RU
54 assert(SvTYPE(_sv_cop_seq_range_high) == SVt_NV \
55 || SvTYPE(_sv_cop_seq_range_high) >= SVt_PVNV); \
56 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVAV); \
57 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVHV); \
58 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVCV); \
59 assert(SvTYPE(_sv_cop_seq_range_high) != SVt_PVFM); \
60 assert(!isGV_with_GP(_sv_cop_seq_range_high)); \
b1bc3f34 61 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_cop_seq_range_high)))->xnv_u.xpad_cop_seq.xhigh; \
3441fb63
NC
62 }))
63# define PARENT_PAD_INDEX(sv) \
b1bc3f34 64 (({ const SV *const _sv_parent_pad_index = (const SV *) (sv); \
7948fc08
RU
65 assert(SvTYPE(_sv_parent_pad_index) == SVt_NV \
66 || SvTYPE(_sv_parent_pad_index) >= SVt_PVNV); \
67 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVAV); \
68 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVHV); \
69 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVCV); \
70 assert(SvTYPE(_sv_parent_pad_index) != SVt_PVFM); \
71 assert(!isGV_with_GP(_sv_parent_pad_index)); \
b1bc3f34 72 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_pad_index)))->xnv_u.xpad_cop_seq.xlow; \
3441fb63
NC
73 }))
74# define PARENT_FAKELEX_FLAGS(sv) \
b1bc3f34 75 (({ const SV *const _sv_parent_fakelex_flags = (const SV *) (sv); \
7948fc08
RU
76 assert(SvTYPE(_sv_parent_fakelex_flags) == SVt_NV \
77 || SvTYPE(_sv_parent_fakelex_flags) >= SVt_PVNV); \
78 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVAV); \
79 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVHV); \
80 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVCV); \
81 assert(SvTYPE(_sv_parent_fakelex_flags) != SVt_PVFM); \
82 assert(!isGV_with_GP(_sv_parent_fakelex_flags)); \
b1bc3f34 83 ((XPVNV*) MUTABLE_PTR(SvANY(_sv_parent_fakelex_flags)))->xnv_u.xpad_cop_seq.xhigh; \
3441fb63
NC
84 }))
85#else
86# define COP_SEQ_RANGE_LOW(sv) \
87 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
88# define COP_SEQ_RANGE_HIGH(sv) \
89 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
90
91
92# define PARENT_PAD_INDEX(sv) \
93 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xlow))
94# define PARENT_FAKELEX_FLAGS(sv) \
95 (0 + (((XPVNV*) SvANY(sv))->xnv_u.xpad_cop_seq.xhigh))
96#endif
dd2155a4 97
6c5e080d 98/* Flags set in the SvIVX field of FAKE namesvs */
7948fc08 99
6c5e080d
NC
100#define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */
101#define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */
102
dd2155a4
DM
103/* flags for the pad_new() function */
104
c7c737cb
DM
105#define padnew_CLONE 1 /* this pad is for a cloned CV */
106#define padnew_SAVE 2 /* save old globals */
107#define padnew_SAVESUB 4 /* also save extra stuff for start of sub */
dd2155a4
DM
108
109/* values for the pad_tidy() function */
110
111typedef enum {
112 padtidy_SUB, /* tidy up a pad for a sub, */
113 padtidy_SUBCLONE, /* a cloned sub, */
114 padtidy_FORMAT /* or a format */
115} padtidy_type;
116
9f6ec8dd 117/* flags for pad_add_name_pvn. */
35f82371 118
9f6ec8dd
BF
119#define padadd_OUR 0x01 /* our declaration. */
120#define padadd_STATE 0x02 /* state declaration. */
121#define padadd_NO_DUP_CHECK 0x04 /* skip warning on dups. */
7ef30830
FC
122#define padadd_STALEOK 0x08 /* allow stale lexical in active
123 * sub, but only one level up */
9f6ec8dd 124#define padadd_UTF8_NAME SVf_UTF8 /* name is UTF-8 encoded. */
35f82371 125
f3548bdc
DM
126/* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
127 * whether PL_comppad and PL_curpad are consistent and whether they have
128 * active values */
dd2155a4 129
1dba731d
NC
130#ifndef PERL_MAD
131# define pad_peg(label)
132#endif
133
f3548bdc
DM
134#ifdef DEBUGGING
135# define ASSERT_CURPAD_LEGAL(label) \
1dba731d 136 pad_peg(label); \
f3548bdc
DM
137 if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \
138 Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
139 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
140
141
142# define ASSERT_CURPAD_ACTIVE(label) \
1dba731d 143 pad_peg(label); \
f3548bdc
DM
144 if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \
145 Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
146 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
147#else
148# define ASSERT_CURPAD_LEGAL(label)
149# define ASSERT_CURPAD_ACTIVE(label)
150#endif
151
152
153
154/* Note: the following three macros are actually defined in scope.h, but
dd2155a4
DM
155 * they are documented here for completeness, since they directly or
156 * indirectly affect pads.
157
158=for apidoc m|void|SAVEPADSV |PADOFFSET po
159Save a pad slot (used to restore after an iteration)
160
f3548bdc 161XXX DAPM it would make more sense to make the arg a PADOFFSET
dd2155a4 162=for apidoc m|void|SAVECLEARSV |SV **svp
eeb8d49e 163Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my')
dd2155a4
DM
164
165=for apidoc m|void|SAVECOMPPAD
166save PL_comppad and PL_curpad
167
dd2155a4
DM
168
169
170
171
172=for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
173Set the slot at offset C<po> in the current pad to C<sv>
174
175=for apidoc m|void|PAD_SV |PADOFFSET po
176Get the value at offset C<po> in the current pad
177
178=for apidoc m|SV *|PAD_SVl |PADOFFSET po
179Lightweight and lvalue version of C<PAD_SV>.
180Get or set the value at offset C<po> in the current pad.
181Unlike C<PAD_SV>, does not print diagnostics with -DX.
182For internal use only.
183
184=for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
185Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
186
187=for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
188Set the current pad to be pad C<n> in the padlist, saving
fd617465
DM
189the previous current pad. NB currently this macro expands to a string too
190long for some compilers, so it's best to replace it with
191
192 SAVECOMPPAD();
193 PAD_SET_CUR_NOSAVE(padlist,n);
194
dd2155a4 195
4e380990
DM
196=for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n
197like PAD_SET_CUR, but without the save
198
dd2155a4
DM
199=for apidoc m|void|PAD_SAVE_SETNULLPAD
200Save the current pad then set it to null.
201
f3548bdc
DM
202=for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
203Save the current pad to the local variable opad, then make the
204current pad equal to npad
205
206=for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
207Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
dd2155a4
DM
208
209=cut
210*/
211
212#ifdef DEBUGGING
213# define PAD_SV(po) pad_sv(po)
214# define PAD_SETSV(po,sv) pad_setsv(po,sv)
215#else
216# define PAD_SV(po) (PL_curpad[po])
217# define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
218#endif
219
220#define PAD_SVl(po) (PL_curpad[po])
221
222#define PAD_BASE_SV(padlist, po) \
223 (AvARRAY(padlist)[1]) \
a062e10d 224 ? AvARRAY(MUTABLE_AV((AvARRAY(padlist)[1])))[po] : NULL;
7948fc08 225
dd2155a4 226
de5e01c2
JH
227#define PAD_SET_CUR_NOSAVE(padlist,nth) \
228 PL_comppad = (PAD*) (AvARRAY(padlist)[nth]); \
f3548bdc
DM
229 PL_curpad = AvARRAY(PL_comppad); \
230 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
231 "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \
de5e01c2 232 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
f3548bdc
DM
233
234
de5e01c2 235#define PAD_SET_CUR(padlist,nth) \
4e380990 236 SAVECOMPPAD(); \
de5e01c2 237 PAD_SET_CUR_NOSAVE(padlist,nth);
4e380990
DM
238
239
f3548bdc 240#define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \
4608196e 241 PL_comppad = NULL; PL_curpad = NULL; \
f3548bdc
DM
242 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
243
244#define PAD_SAVE_LOCAL(opad,npad) \
245 opad = PL_comppad; \
246 PL_comppad = (npad); \
4608196e 247 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
f3548bdc
DM
248 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
249 "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \
250 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
251
252#define PAD_RESTORE_LOCAL(opad) \
8c63ea58
GG
253 assert(!opad || !SvIS_FREED(opad)); \
254 PL_comppad = opad; \
4608196e 255 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
f3548bdc
DM
256 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
257 "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \
258 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
dd2155a4
DM
259
260
261/*
262=for apidoc m|void|CX_CURPAD_SAVE|struct context
263Save the current pad in the given context block structure.
264
f3548bdc 265=for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
dd2155a4
DM
266Access the SV at offset po in the saved current pad in the given
267context block structure (can be used as an lvalue).
268
269=cut
270*/
271
f3548bdc 272#define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad
a062e10d 273#define CX_CURPAD_SV(block,po) (AvARRAY(MUTABLE_AV(((block).oldcomppad)))[po])
dd2155a4
DM
274
275
276/*
277=for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
278Return the flags for the current compiling pad name
279at offset C<po>. Assumes a valid slot entry.
280
281=for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
282Return the name of the current compiling pad name
283at offset C<po>. Assumes a valid slot entry.
284
285=for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
286Return the type (stash) of the current compiling pad name at offset
287C<po>. Must be a valid name. Returns null if not typed.
288
289=for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
290Return the stash associated with an C<our> variable.
291Assumes the slot entry is a valid C<our> lexical.
292
293=for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
294The generation number of the name at offset C<po> in the current
931b58fb 295compiling pad (lvalue). Note that C<SvUVX> is hijacked for this purpose.
dd2155a4 296
b162af07
SP
297=for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
298Sets the generation number of the name at offset C<po> in the current
931b58fb 299ling pad (lvalue) to C<gen>. Note that C<SvUV_set> is hijacked for this purpose.
b162af07 300
dd2155a4 301=cut
b162af07 302
dd2155a4
DM
303*/
304
f8503592
NC
305#define PAD_COMPNAME_SV(po) (*av_fetch(PL_comppad_name, (po), FALSE))
306#define PAD_COMPNAME_FLAGS(po) SvFLAGS(PAD_COMPNAME_SV(po))
1979170b 307#define PAD_COMPNAME_FLAGS_isOUR(po) SvPAD_OUR(PAD_COMPNAME_SV(po))
f8503592 308#define PAD_COMPNAME_PV(po) SvPV_nolen(PAD_COMPNAME_SV(po))
dd2155a4 309
b21dc031 310#define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
dd2155a4
DM
311
312#define PAD_COMPNAME_OURSTASH(po) \
73d95100 313 (SvOURSTASH(PAD_COMPNAME_SV(po)))
dd2155a4 314
931b58fb 315#define PAD_COMPNAME_GEN(po) ((STRLEN)SvUVX(AvARRAY(PL_comppad_name)[po]))
dd2155a4 316
931b58fb 317#define PAD_COMPNAME_GEN_set(po, gen) SvUV_set(AvARRAY(PL_comppad_name)[po], (UV)(gen))
dd2155a4
DM
318
319
320/*
d77cdebf 321=for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl|CLONE_PARAMS* param
dd2155a4
DM
322Clone the state variables associated with running and compiling pads.
323
324=cut
325*/
326
c5ab0850
DM
327/* NB - we set PL_comppad to null unless it points at a value that
328 * has already been dup'ed, ie it points to part of an active padlist.
329 * Otherwise PL_comppad ends up being a leaked scalar in code like
330 * the following:
331 * threads->create(sub { threads->create(sub {...} ) } );
332 * where the second thread dups the outer sub's comppad but not the
333 * sub's CV or padlist. */
f3548bdc 334
dd2155a4 335#define PAD_CLONE_VARS(proto_perl, param) \
253649d9 336 PL_comppad = av_dup(proto_perl->Icomppad, param); \
4608196e 337 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
dd2155a4
DM
338 PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \
339 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
340 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
dd2155a4
DM
341 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
342 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
343 PL_padix = proto_perl->Ipadix; \
344 PL_padix_floor = proto_perl->Ipadix_floor; \
345 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
346 PL_cop_seqmax = proto_perl->Icop_seqmax;
e9a8c099
MHM
347
348/*
cc76b5cc
Z
349=for apidoc Am|PADOFFSET|pad_add_name_pvs|const char *name|U32 flags|HV *typestash|HV *ourstash
350
351Exactly like L</pad_add_name_pvn>, but takes a literal string instead
352of a string/length pair.
353
354=cut
355*/
356
357#define pad_add_name_pvs(name,flags,typestash,ourstash) \
358 Perl_pad_add_name_pvn(aTHX_ STR_WITH_LEN(name), flags, typestash, ourstash)
359
360/*
361=for apidoc Am|PADOFFSET|pad_findmy_pvs|const char *name|U32 flags
362
363Exactly like L</pad_findmy_pvn>, but takes a literal string instead
364of a string/length pair.
365
366=cut
367*/
368
369#define pad_findmy_pvs(name,flags) \
370 Perl_pad_findmy_pvn(aTHX_ STR_WITH_LEN(name), flags)
371
372/*
e9a8c099
MHM
373 * Local variables:
374 * c-indentation-style: bsd
375 * c-basic-offset: 4
14d04a33 376 * indent-tabs-mode: nil
e9a8c099
MHM
377 * End:
378 *
14d04a33 379 * ex: set ts=8 sts=4 sw=4 et:
e9a8c099 380 */