This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlreapi.pod: grammar and other nits
[perl5.git] / pad.h
1 /*    pad.h
2  *
3  *    Copyright (C) 2002, 2003, 2005, 2006, 2007, 2008,
4  *    2009, 2010, 2011 by Larry Wall and others
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
14 /*
15 =head1 Pad Data Structures
16 */
17
18
19 /* offsets within a pad */
20
21 #if PTRSIZE == 4
22 typedef U32TYPE PADOFFSET;
23 #else
24 #   if PTRSIZE == 8
25 typedef U64TYPE PADOFFSET;
26 #   endif
27 #endif
28 #define NOT_IN_PAD ((PADOFFSET) -1)
29
30
31 struct padlist {
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 */
36 };
37
38
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
41  */
42 #define PERL_PADSEQ_INTRO  U32_MAX
43
44
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) */
48
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; \
60          }))
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; \
71          }))
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; \
82          }))
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; \
93          }))
94 #else
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))
99
100
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))
105 #endif
106
107 /* Flags set in the SvIVX field of FAKE namesvs */
108
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 */
111
112 /* flags for the pad_new() function */
113
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 */
117
118 /* values for the pad_tidy() function */
119
120 typedef enum {
121         padtidy_SUB,            /* tidy up a pad for a sub, */
122         padtidy_SUBCLONE,       /* a cloned sub, */
123         padtidy_FORMAT          /* or a format */
124 } padtidy_type;
125
126 /* flags for pad_add_name_pvn. */
127
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. */
134
135 /* ASSERT_CURPAD_LEGAL and ASSERT_CURPAD_ACTIVE respectively determine
136  * whether PL_comppad and PL_curpad are consistent and whether they have
137  * active values */
138
139 #ifndef PERL_MAD
140 #  define pad_peg(label)
141 #endif
142
143 #ifdef DEBUGGING
144 #  define ASSERT_CURPAD_LEGAL(label) \
145     pad_peg(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));
149
150
151 #  define ASSERT_CURPAD_ACTIVE(label) \
152     pad_peg(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));
156 #else
157 #  define ASSERT_CURPAD_LEGAL(label)
158 #  define ASSERT_CURPAD_ACTIVE(label)
159 #endif
160
161
162
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.
166
167 =for apidoc m|void|SAVEPADSV    |PADOFFSET po
168 Save a pad slot (used to restore after an iteration)
169
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')
173
174 =for apidoc m|void|SAVECOMPPAD
175 save PL_comppad and PL_curpad
176
177
178 =for apidoc Amx|PAD **|PadlistARRAY|PADLIST padlist
179 The C array of a padlist, containing the pads.  Only subscript it with
180 numbers >= 1, as the 0th entry is not guaranteed to remain usable.
181
182 =for apidoc Amx|SSize_t|PadlistMAX|PADLIST padlist
183 The index of the last pad in the padlist.
184
185 =for apidoc Amx|PADNAMELIST *|PadlistNAMES|PADLIST padlist
186 The names associated with pad entries.
187
188 =for apidoc Amx|PADNAME **|PadlistNAMESARRAY|PADLIST padlist
189 The C array of pad names.
190
191 =for apidoc Amx|SSize_t|PadlistNAMESMAX|PADLIST padlist
192 The index of the last pad name.
193
194 =for apidoc Amx|U32|PadlistREFCNT|PADLIST padlist
195 The reference count of the padlist.  Currently this is always 1.
196
197 =for apidoc Amx|PADNAME **|PadnamelistARRAY|PADNAMELIST pnl
198 The C array of pad names.
199
200 =for apidoc Amx|SSize_t|PadnamelistMAX|PADNAMELIST pnl
201 The index of the last pad name.
202
203 =for apidoc Amx|SV **|PadARRAY|PAD pad
204 The C array of pad entries.
205
206 =for apidoc Amx|SSize_t|PadMAX|PAD pad
207 The index of the last pad entry.
208
209 =for apidoc Amx|char *|PadnamePV|PADNAME pn     
210 The name stored in the pad name struct.  This returns NULL for a target or
211 GV slot.
212
213 =for apidoc Amx|STRLEN|PadnameLEN|PADNAME pn    
214 The length of the name.
215
216 =for apidoc Amx|bool|PadnameUTF8|PADNAME pn
217 Whether PadnamePV is in UTF8.
218
219 =for apidoc Amx|SV *|PadnameSV|PADNAME pn
220 Returns the pad name as an SV.  This is currently just C<pn>.  It will
221 begin returning a new mortal SV if pad names ever stop being SVs.
222
223 =for apidoc m|bool|PadnameIsOUR|PADNAME pn
224 Whether this is an "our" variable.
225
226 =for apidoc m|HV *|PadnameOURSTASH
227 The stash in which this "our" variable was declared.
228
229 =for apidoc m|bool|PadnameOUTER|PADNAME pn
230 Whether this entry belongs to an outer pad.
231
232 =for apidoc m|bool|PadnameIsSTATE|PADNAME pn
233 Whether this is a "state" variable.
234
235 =for apidoc m|HV *|PadnameTYPE|PADNAME pn
236 The stash associated with a typed lexical.  This returns the %Foo:: hash
237 for C<my Foo $bar>.
238
239
240 =for apidoc m|SV *|PAD_SETSV    |PADOFFSET po|SV* sv
241 Set the slot at offset C<po> in the current pad to C<sv>
242
243 =for apidoc m|void|PAD_SV       |PADOFFSET po
244 Get the value at offset C<po> in the current pad
245
246 =for apidoc m|SV *|PAD_SVl      |PADOFFSET po
247 Lightweight and lvalue version of C<PAD_SV>.
248 Get or set the value at offset C<po> in the current pad.
249 Unlike C<PAD_SV>, does not print diagnostics with -DX.
250 For internal use only.
251
252 =for apidoc m|SV *|PAD_BASE_SV  |PADLIST padlist|PADOFFSET po
253 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
254
255 =for apidoc m|void|PAD_SET_CUR  |PADLIST padlist|I32 n
256 Set the current pad to be pad C<n> in the padlist, saving
257 the previous current pad. NB currently this macro expands to a string too
258 long for some compilers, so it's best to replace it with
259
260     SAVECOMPPAD();
261     PAD_SET_CUR_NOSAVE(padlist,n);
262
263
264 =for apidoc m|void|PAD_SET_CUR_NOSAVE   |PADLIST padlist|I32 n
265 like PAD_SET_CUR, but without the save
266
267 =for apidoc m|void|PAD_SAVE_SETNULLPAD
268 Save the current pad then set it to null.
269
270 =for apidoc m|void|PAD_SAVE_LOCAL|PAD *opad|PAD *npad
271 Save the current pad to the local variable opad, then make the
272 current pad equal to npad
273
274 =for apidoc m|void|PAD_RESTORE_LOCAL|PAD *opad
275 Restore the old pad saved into the local variable opad by PAD_SAVE_LOCAL()
276
277 =cut
278 */
279
280 #define PadlistARRAY(pl)        (pl)->xpadl_alloc
281 #define PadlistMAX(pl)          (pl)->xpadl_max
282 #define PadlistNAMES(pl)        (*PadlistARRAY(pl))
283 #define PadlistNAMESARRAY(pl)   PadnamelistARRAY(PadlistNAMES(pl))
284 #define PadlistNAMESMAX(pl)     PadnamelistMAX(PadlistNAMES(pl))
285 #define PadlistREFCNT(pl)       1       /* reserved for future use */
286
287 #define PadnamelistARRAY(pnl)   AvARRAY(pnl)
288 #define PadnamelistMAX(pnl)     AvFILLp(pnl)
289
290 #define PadARRAY(pad)           AvARRAY(pad)
291 #define PadMAX(pad)             AvFILLp(pad)
292
293 #define PadnamePV(pn)           (SvPOKp(pn) ? SvPVX(pn) : NULL)
294 #define PadnameLEN(pn)          SvCUR(pn)
295 #define PadnameUTF8(pn)         !!SvUTF8(pn)
296 #define PadnameSV(pn)           pn
297 #define PadnameIsOUR(pn)        !!SvPAD_OUR(pn)
298 #define PadnameOURSTASH(pn)     SvOURSTASH(pn)
299 #define PadnameOUTER(pn)        !!SvFAKE(pn)
300 #define PadnameIsSTATE(pn)      !!SvPAD_STATE(pn)
301 #define PadnameTYPE(pn)         (SvPAD_TYPED(pn) ? SvSTASH(pn) : NULL)
302
303
304 #ifdef DEBUGGING
305 #  define PAD_SV(po)       pad_sv(po)
306 #  define PAD_SETSV(po,sv) pad_setsv(po,sv)
307 #else
308 #  define PAD_SV(po)       (PL_curpad[po])
309 #  define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
310 #endif
311
312 #define PAD_SVl(po)       (PL_curpad[po])
313
314 #define PAD_BASE_SV(padlist, po) \
315         (PadlistARRAY(padlist)[1])                                      \
316             ? AvARRAY(MUTABLE_AV((PadlistARRAY(padlist)[1])))[po] \
317             : NULL;
318
319
320 #define PAD_SET_CUR_NOSAVE(padlist,nth) \
321         PL_comppad = (PAD*) (PadlistARRAY(padlist)[nth]);       \
322         PL_curpad = AvARRAY(PL_comppad);                        \
323         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
324               "Pad 0x%"UVxf"[0x%"UVxf"] set_cur    depth=%d\n", \
325               PTR2UV(PL_comppad), PTR2UV(PL_curpad), (int)(nth)));
326
327
328 #define PAD_SET_CUR(padlist,nth) \
329         SAVECOMPPAD();                                          \
330         PAD_SET_CUR_NOSAVE(padlist,nth);
331
332
333 #define PAD_SAVE_SETNULLPAD()   SAVECOMPPAD(); \
334         PL_comppad = NULL; PL_curpad = NULL;    \
335         DEBUG_Xv(PerlIO_printf(Perl_debug_log, "Pad set_null\n"));
336
337 #define PAD_SAVE_LOCAL(opad,npad) \
338         opad = PL_comppad;                                      \
339         PL_comppad = (npad);                                    \
340         PL_curpad =  PL_comppad ? AvARRAY(PL_comppad) : NULL;   \
341         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
342               "Pad 0x%"UVxf"[0x%"UVxf"] save_local\n",          \
343               PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
344
345 #define PAD_RESTORE_LOCAL(opad) \
346         assert(!opad || !SvIS_FREED(opad));                                     \
347         PL_comppad = opad;                                              \
348         PL_curpad =  PL_comppad ? AvARRAY(PL_comppad) : NULL;   \
349         DEBUG_Xv(PerlIO_printf(Perl_debug_log,                  \
350               "Pad 0x%"UVxf"[0x%"UVxf"] restore_local\n",       \
351               PTR2UV(PL_comppad), PTR2UV(PL_curpad)));
352
353
354 /*
355 =for apidoc m|void|CX_CURPAD_SAVE|struct context
356 Save the current pad in the given context block structure.
357
358 =for apidoc m|SV *|CX_CURPAD_SV|struct context|PADOFFSET po
359 Access the SV at offset po in the saved current pad in the given
360 context block structure (can be used as an lvalue).
361
362 =cut
363 */
364
365 #define CX_CURPAD_SAVE(block)  (block).oldcomppad = PL_comppad
366 #define CX_CURPAD_SV(block,po) (AvARRAY(MUTABLE_AV(((block).oldcomppad)))[po])
367
368
369 /*
370 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
371 Return the flags for the current compiling pad name
372 at offset C<po>. Assumes a valid slot entry.
373
374 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
375 Return the name of the current compiling pad name
376 at offset C<po>. Assumes a valid slot entry.
377
378 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
379 Return the type (stash) of the current compiling pad name at offset
380 C<po>. Must be a valid name. Returns null if not typed.
381
382 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
383 Return the stash associated with an C<our> variable.
384 Assumes the slot entry is a valid C<our> lexical.
385
386 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
387 The generation number of the name at offset C<po> in the current
388 compiling pad (lvalue). Note that C<SvUVX> is hijacked for this purpose.
389
390 =for apidoc m|STRLEN|PAD_COMPNAME_GEN_set|PADOFFSET po|int gen
391 Sets the generation number of the name at offset C<po> in the current
392 ling pad (lvalue) to C<gen>.  Note that C<SvUV_set> is hijacked for this purpose.
393
394 =cut
395
396 */
397
398 #define PAD_COMPNAME(po)        PAD_COMPNAME_SV(po)
399 #define PAD_COMPNAME_SV(po) (*av_fetch(PL_comppad_name, (po), FALSE))
400 #define PAD_COMPNAME_FLAGS(po) SvFLAGS(PAD_COMPNAME_SV(po))
401 #define PAD_COMPNAME_FLAGS_isOUR(po) SvPAD_OUR(PAD_COMPNAME_SV(po))
402 #define PAD_COMPNAME_PV(po) SvPV_nolen(PAD_COMPNAME_SV(po))
403
404 #define PAD_COMPNAME_TYPE(po) pad_compname_type(po)
405
406 #define PAD_COMPNAME_OURSTASH(po) \
407     (SvOURSTASH(PAD_COMPNAME_SV(po)))
408
409 #define PAD_COMPNAME_GEN(po) ((STRLEN)SvUVX(AvARRAY(PL_comppad_name)[po]))
410
411 #define PAD_COMPNAME_GEN_set(po, gen) SvUV_set(AvARRAY(PL_comppad_name)[po], (UV)(gen))
412
413
414 /*
415 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl|CLONE_PARAMS* param
416 Clone the state variables associated with running and compiling pads.
417
418 =cut
419 */
420
421 /* NB - we set PL_comppad to null unless it points at a value that
422  * has already been dup'ed, ie it points to part of an active padlist.
423  * Otherwise PL_comppad ends up being a leaked scalar in code like
424  * the following:
425  *     threads->create(sub { threads->create(sub {...} ) } );
426  * where the second thread dups the outer sub's comppad but not the
427  * sub's CV or padlist. */
428
429 #define PAD_CLONE_VARS(proto_perl, param)                               \
430     PL_comppad                  = av_dup(proto_perl->Icomppad, param);  \
431     PL_curpad = PL_comppad ?  AvARRAY(PL_comppad) : NULL;               \
432     PL_comppad_name             = av_dup(proto_perl->Icomppad_name, param); \
433     PL_comppad_name_fill        = proto_perl->Icomppad_name_fill;       \
434     PL_comppad_name_floor       = proto_perl->Icomppad_name_floor;      \
435     PL_min_intro_pending        = proto_perl->Imin_intro_pending;       \
436     PL_max_intro_pending        = proto_perl->Imax_intro_pending;       \
437     PL_padix                    = proto_perl->Ipadix;                   \
438     PL_padix_floor              = proto_perl->Ipadix_floor;             \
439     PL_pad_reset_pending        = proto_perl->Ipad_reset_pending;       \
440     PL_cop_seqmax               = proto_perl->Icop_seqmax;
441
442 /*
443 =for apidoc Am|PADOFFSET|pad_add_name_pvs|const char *name|U32 flags|HV *typestash|HV *ourstash
444
445 Exactly like L</pad_add_name_pvn>, but takes a literal string instead
446 of a string/length pair.
447
448 =cut
449 */
450
451 #define pad_add_name_pvs(name,flags,typestash,ourstash) \
452     Perl_pad_add_name_pvn(aTHX_ STR_WITH_LEN(name), flags, typestash, ourstash)
453
454 /*
455 =for apidoc Am|PADOFFSET|pad_findmy_pvs|const char *name|U32 flags
456
457 Exactly like L</pad_findmy_pvn>, but takes a literal string instead
458 of a string/length pair.
459
460 =cut
461 */
462
463 #define pad_findmy_pvs(name,flags) \
464     Perl_pad_findmy_pvn(aTHX_ STR_WITH_LEN(name), flags)
465
466 /*
467  * Local variables:
468  * c-indentation-style: bsd
469  * c-basic-offset: 4
470  * indent-tabs-mode: nil
471  * End:
472  *
473  * ex: set ts=8 sts=4 sw=4 et:
474  */