This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Integrate:
[perl5.git] / pad.h
CommitLineData
9755d405
JH
1/* pad.h
2 *
2c351e65 3 * Copyright (C) 2002, 2003, 2005, by Larry Wall and others
9755d405
JH
4 *
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.
7 *
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.
11 */
12
13
14
15
16/* a padlist is currently just an AV; but that might change,
17 * so hide the type. Ditto a pad. */
18
19typedef AV PADLIST;
d7afa7f5 20typedef AV PAD;
9755d405
JH
21
22
23/* offsets within a pad */
24
25#if PTRSIZE == 4
26typedef U32TYPE PADOFFSET;
27#else
28# if PTRSIZE == 8
29typedef U64TYPE PADOFFSET;
30# endif
31#endif
32#define NOT_IN_PAD ((PADOFFSET) -1)
aa38d90e
NC
33
34/* B.xs needs these for the benefit of B::Deparse */
35/* Low range end is exclusive (valid from the cop seq after this one) */
36#define COP_SEQ_RANGE_LOW(sv) U_32(SvNVX(sv))
37/* High range end is inclusive (valid up to this cop seq) */
38#define COP_SEQ_RANGE_HIGH(sv) U_32(SvUVX(sv))
39
40#define PARENT_PAD_INDEX(sv) U_32(SvNVX(sv))
41#define PARENT_FAKELEX_FLAGS(sv) U_32(SvUVX(sv))
42
43/* Flags set in the SvIVX field of FAKE namesvs */
44
45#define PAD_FAKELEX_ANON 1 /* the lex is declared in an ANON, or ... */
46#define PAD_FAKELEX_MULTI 2 /* the lex can be instantiated multiple times */
9755d405
JH
47
48/* flags for the pad_new() function */
49
50#define padnew_CLONE 1 /* this pad is for a cloned CV */
51#define padnew_SAVE 2 /* save old globals */
52#define padnew_SAVESUB 4 /* also save extra stuff for start of sub */
53
54/* values for the pad_tidy() function */
55
56typedef enum {
57 padtidy_SUB, /* tidy up a pad for a sub, */
58 padtidy_SUBCLONE, /* a cloned sub, */
59 padtidy_FORMAT /* or a format */
60} padtidy_type;
61
d7afa7f5
JH
62/* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
63 * whether PL_comppad and PL_curpad are consistent and whether they have
64 * active values */
9755d405 65
d7afa7f5
JH
66#ifdef DEBUGGING
67# define ASSERT_CURPAD_LEGAL(label) \
68 if (PL_comppad ? (AvARRAY(PL_comppad) != PL_curpad) : (PL_curpad != 0)) \
69 Perl_croak(aTHX_ "panic: illegal pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
70 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
71
72
73# define ASSERT_CURPAD_ACTIVE(label) \
74 if (!PL_comppad || (AvARRAY(PL_comppad) != PL_curpad)) \
75 Perl_croak(aTHX_ "panic: invalid pad in %s: 0x%"UVxf"[0x%"UVxf"]",\
76 label, PTR2UV(PL_comppad), PTR2UV(PL_curpad));
77#else
78# define ASSERT_CURPAD_LEGAL(label)
79# define ASSERT_CURPAD_ACTIVE(label)
80#endif
81
82
83
84/* Note: the following three macros are actually defined in scope.h, but
9755d405
JH
85 * they are documented here for completeness, since they directly or
86 * indirectly affect pads.
87
88=for apidoc m|void|SAVEPADSV |PADOFFSET po
89Save a pad slot (used to restore after an iteration)
90
d7afa7f5 91XXX DAPM it would make more sense to make the arg a PADOFFSET
9755d405 92=for apidoc m|void|SAVECLEARSV |SV **svp
8f53e8d5 93Clear the pointed to pad value on scope exit. (i.e. the runtime action of 'my')
9755d405
JH
94
95=for apidoc m|void|SAVECOMPPAD
96save PL_comppad and PL_curpad
97
9755d405
JH
98
99
100
101
102=for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
103Set the slot at offset C<po> in the current pad to C<sv>
104
105=for apidoc m|void|PAD_SV |PADOFFSET po
106Get the value at offset C<po> in the current pad
107
108=for apidoc m|SV *|PAD_SVl |PADOFFSET po
109Lightweight and lvalue version of C<PAD_SV>.
110Get or set the value at offset C<po> in the current pad.
111Unlike C<PAD_SV>, does not print diagnostics with -DX.
112For internal use only.
113
114=for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
115Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
116
117=for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
118Set the current pad to be pad C<n> in the padlist, saving
cc09671d
RB
119the previous current pad. NB currently this macro expands to a string too
120long for some compilers, so it's best to replace it with
121
122 SAVECOMPPAD();
123 PAD_SET_CUR_NOSAVE(padlist,n);
124
9755d405 125
73de1361
EM
126=for apidoc m|void|PAD_SET_CUR_NOSAVE |PADLIST padlist|I32 n
127like PAD_SET_CUR, but without the save
128
9755d405
JH
129=for apidoc m|void|PAD_SAVE_SETNULLPAD
130Save the current pad then set it to null.
131
d7afa7f5
JH
132=for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
133Save the current pad to the local variable opad, then make the
134current pad equal to npad
135
136=for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
137Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
9755d405
JH
138
139=cut
140*/
141
142#ifdef DEBUGGING
143# define PAD_SV(po) pad_sv(po)
144# define PAD_SETSV(po,sv) pad_setsv(po,sv)
145#else
146# define PAD_SV(po) (PL_curpad[po])
147# define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
148#endif
149
150#define PAD_SVl(po) (PL_curpad[po])
151
152#define PAD_BASE_SV(padlist, po) \
153 (AvARRAY(padlist)[1]) \
0e2d6244 154 ? AvARRAY((AV*)(AvARRAY(padlist)[1]))[po] : NULL;
9755d405
JH
155
156
eb527b6b
NC
157#define PAD_SET_CUR_NOSAVE(padlist,nth) \
158 PL_comppad = (PAD*) (AvARRAY(padlist)[nth]); \
d7afa7f5
JH
159 PL_curpad = AvARRAY(PL_comppad); \
160 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
161 "Pad 0x%"UVxf"[0x%"UVxf"] set_cur depth=%d\n", \
eb527b6b 162 PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
d7afa7f5
JH
163
164
eb527b6b 165#define PAD_SET_CUR(padlist,nth) \
73de1361 166 SAVECOMPPAD(); \
eb527b6b 167 PAD_SET_CUR_NOSAVE(padlist,nth);
73de1361
EM
168
169
d7afa7f5 170#define PAD_SAVE_SETNULLPAD() SAVECOMPPAD(); \
0e2d6244 171 PL_comppad = NULL; PL_curpad = NULL; \
d7afa7f5
JH
172 DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
173
174#define PAD_SAVE_LOCAL(opad,npad) \
175 opad = PL_comppad; \
176 PL_comppad = (npad); \
0e2d6244 177 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
d7afa7f5
JH
178 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
179 "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n", \
180 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
181
182#define PAD_RESTORE_LOCAL(opad) \
183 PL_comppad = opad; \
0e2d6244 184 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
d7afa7f5
JH
185 DEBUG_Xv(PerlIO_printf(Perl_debug_log, \
186 "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n", \
187 PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
9755d405
JH
188
189
190/*
191=for apidoc m|void|CX_CURPAD_SAVE|struct context
192Save the current pad in the given context block structure.
193
d7afa7f5 194=for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
9755d405
JH
195Access the SV at offset po in the saved current pad in the given
196context block structure (can be used as an lvalue).
197
198=cut
199*/
200
d7afa7f5
JH
201#define CX_CURPAD_SAVE(block) (block).oldcomppad = PL_comppad
202#define CX_CURPAD_SV(block,po) (AvARRAY((AV*)((block).oldcomppad))[po])
9755d405
JH
203
204
205/*
206=for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
207Return the flags for the current compiling pad name
208at offset C<po>. Assumes a valid slot entry.
209
210=for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
211Return the name of the current compiling pad name
212at offset C<po>. Assumes a valid slot entry.
213
214=for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
215Return the type (stash) of the current compiling pad name at offset
216C<po>. Must be a valid name. Returns null if not typed.
217
218=for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
219Return the stash associated with an C<our> variable.
220Assumes the slot entry is a valid C<our> lexical.
221
222=for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
223The generation number of the name at offset C<po> in the current
224compiling pad (lvalue). Note that C<SvCUR> is hijacked for this purpose.
225
a8dc4fe8
SP
226=for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
227Sets the generation number of the name at offset C<po> in the current
228ling pad (lvalue) to C<gen>. Note that C<SvCUR_set> is hijacked for this purpose.
229
9755d405 230=cut
a8dc4fe8 231
9755d405
JH
232*/
233
e70aec60
NC
234#define PAD_COMPNAME_SV(po) (*av_fetch(PL_comppad_name, (po), FALSE))
235#define PAD_COMPNAME_FLAGS(po) SvFLAGS(PAD_COMPNAME_SV(po))
62d59642 236#define PAD_COMPNAME_FLAGS_isOUR(po) (PAD_COMPNAME_FLAGS(po) & SVpad_OUR)
e70aec60 237#define PAD_COMPNAME_PV(po) SvPV_nolen(PAD_COMPNAME_SV(po))
9755d405 238
25e59d17 239#define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
9755d405
JH
240
241#define PAD_COMPNAME_OURSTASH(po) \
e70aec60 242 (SvOURSTASH(PAD_COMPNAME_SV(po)))
9755d405
JH
243
244#define PAD_COMPNAME_GEN(po) SvCUR(AvARRAY(PL_comppad_name)[po])
245
a8dc4fe8 246#define PAD_COMPNAME_GEN_set(po, gen) SvCUR_set(AvARRAY(PL_comppad_name)[po], gen)
9755d405
JH
247
248
249/*
250=for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param
251Clone a padlist.
252
253=for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl \
254|CLONE_PARAMS* param
255Clone the state variables associated with running and compiling pads.
256
257=cut
258*/
259
260
261#define PAD_DUP(dstpad, srcpad, param) \
262 if ((srcpad) && !AvREAL(srcpad)) { \
263 /* XXX padlists are real, but pretend to be not */ \
264 AvREAL_on(srcpad); \
265 (dstpad) = av_dup_inc((srcpad), param); \
266 AvREAL_off(srcpad); \
267 AvREAL_off(dstpad); \
268 } \
269 else \
270 (dstpad) = av_dup_inc((srcpad), param);
271
d7afa7f5
JH
272/* NB - we set PL_comppad to null unless it points at a value that
273 * has already been dup'ed, ie it points to part of an active padlist.
274 * Otherwise PL_comppad ends up being a leaked scalar in code like
275 * the following:
276 * threads->create(sub { threads->create(sub {...} ) } );
277 * where the second thread dups the outer sub's comppad but not the
278 * sub's CV or padlist. */
279
9755d405 280#define PAD_CLONE_VARS(proto_perl, param) \
c0fae1b3 281 PL_comppad = ptr_table_fetch(PL_ptr_table, proto_perl->Tcomppad); \
0e2d6244 282 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : NULL; \
9755d405
JH
283 PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \
284 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
285 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
9755d405
JH
286 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
287 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
288 PL_padix = proto_perl->Ipadix; \
289 PL_padix_floor = proto_perl->Ipadix_floor; \
290 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
291 PL_cop_seqmax = proto_perl->Icop_seqmax;