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