3 * Copyright (c) 2002, Larry Wall
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.
16 /* a padlist is currently just an AV; but that might change,
17 * so hide the type. Ditto a pad. */
23 /* offsets within a pad */
26 typedef U32TYPE PADOFFSET;
29 typedef U64TYPE PADOFFSET;
32 #define NOT_IN_PAD ((PADOFFSET) -1)
35 /* flags for the pad_new() function */
38 padnew_CLONE = 1, /* this pad is for a cloned CV */
39 padnew_SAVE = 2, /* save old globals */
40 padnew_SAVESUB = 4, /* also save extra stuff for start of sub */
43 /* values for the pad_tidy() function */
46 padtidy_SUB, /* tidy up a pad for a sub, */
47 padtidy_SUBCLONE, /* a cloned sub, */
48 padtidy_FORMAT /* or a format */
52 /* Note: the following four macros are actually defined in scope.h, but
53 * they are documented here for completeness, since they directly or
54 * indirectly affect pads.
56 =for apidoc m|void|SAVEPADSV |PADOFFSET po
57 Save a pad slot (used to restore after an iteration)
59 =for apidoc m|void|SAVECLEARSV |SV **svp
60 Clear the pointed to pad value on scope exit. (ie the runtime action of 'my')
62 =for apidoc m|void|SAVECOMPPAD
63 save PL_comppad and PL_curpad
65 =for apidoc m|void|SAVEFREEOP |OP *o
66 Free the op on scope exit. At the same time, reset PL_curpad
71 =for apidoc m|SV *|PAD_SETSV |PADOFFSET po|SV* sv
72 Set the slot at offset C<po> in the current pad to C<sv>
74 =for apidoc m|void|PAD_SV |PADOFFSET po
75 Get the value at offset C<po> in the current pad
77 =for apidoc m|SV *|PAD_SVl |PADOFFSET po
78 Lightweight and lvalue version of C<PAD_SV>.
79 Get or set the value at offset C<po> in the current pad.
80 Unlike C<PAD_SV>, does not print diagnostics with -DX.
81 For internal use only.
83 =for apidoc m|SV *|PAD_BASE_SV |PADLIST padlist|PADOFFSET po
84 Get the value from slot C<po> in the base (DEPTH=1) pad of a padlist
86 =for apidoc m|void|PAD_SET_CUR |PADLIST padlist|I32 n
87 Set the current pad to be pad C<n> in the padlist, saving
88 the previous current pad.
90 =for apidoc m|void|PAD_SAVE_SETNULLPAD
91 Save the current pad then set it to null.
93 =for apidoc m|void|PAD_UPDATE_CURPAD
94 Set PL_curpad from the value of PL_comppad.
100 # define PAD_SV(po) pad_sv(po)
101 # define PAD_SETSV(po,sv) pad_setsv(po,sv)
103 # define PAD_SV(po) (PL_curpad[po])
104 # define PAD_SETSV(po,sv) PL_curpad[po] = (sv)
107 #define PAD_SVl(po) (PL_curpad[po])
109 #define PAD_BASE_SV(padlist, po) \
110 (AvARRAY(padlist)[1]) \
111 ? AvARRAY((AV*)(AvARRAY(padlist)[1]))[po] : Nullsv;
114 #define PAD_SET_CUR(padlist,n) \
115 SAVEVPTR(PL_curpad); \
116 PL_curpad = AvARRAY((AV*)*av_fetch((padlist),(n),FALSE))
118 #define PAD_SAVE_SETNULLPAD SAVEVPTR(PL_curpad); PL_curpad = 0;
120 #define PAD_UPDATE_CURPAD \
121 PL_curpad = PL_comppad ? AvARRAY(PL_comppad) : Null(PAD)
125 =for apidoc m|void|CX_CURPAD_SAVE|struct context
126 Save the current pad in the given context block structure.
128 =for apidoc m|PAD *|CX_CURPAD_SV|struct context|PADOFFSET po
129 Access the SV at offset po in the saved current pad in the given
130 context block structure (can be used as an lvalue).
135 #define CX_CURPAD_SAVE(block) (block).oldcurpad = PL_curpad
136 #define CX_CURPAD_SV(block,po) ((block).oldcurpad[po])
140 =for apidoc m|U32|PAD_COMPNAME_FLAGS|PADOFFSET po
141 Return the flags for the current compiling pad name
142 at offset C<po>. Assumes a valid slot entry.
144 =for apidoc m|char *|PAD_COMPNAME_PV|PADOFFSET po
145 Return the name of the current compiling pad name
146 at offset C<po>. Assumes a valid slot entry.
148 =for apidoc m|HV *|PAD_COMPNAME_TYPE|PADOFFSET po
149 Return the type (stash) of the current compiling pad name at offset
150 C<po>. Must be a valid name. Returns null if not typed.
152 =for apidoc m|HV *|PAD_COMPNAME_OURSTASH|PADOFFSET po
153 Return the stash associated with an C<our> variable.
154 Assumes the slot entry is a valid C<our> lexical.
156 =for apidoc m|STRLEN|PAD_COMPNAME_GEN|PADOFFSET po
157 The generation number of the name at offset C<po> in the current
158 compiling pad (lvalue). Note that C<SvCUR> is hijacked for this purpose.
163 #define PAD_COMPNAME_FLAGS(po) SvFLAGS(*av_fetch(PL_comppad_name, (po), FALSE))
164 #define PAD_COMPNAME_PV(po) SvPV_nolen(*av_fetch(PL_comppad_name, (po), FALSE))
166 /* XXX DAPM yuk - using av_fetch twice. Is there a better way? */
167 #define PAD_COMPNAME_TYPE(po) \
168 ((SvFLAGS(*av_fetch(PL_comppad_name, (po), FALSE)) & SVpad_TYPED) \
169 ? (SvSTASH(*av_fetch(PL_comppad_name, (po), FALSE))) : Nullhv)
171 #define PAD_COMPNAME_OURSTASH(po) \
172 (GvSTASH(*av_fetch(PL_comppad_name, (po), FALSE)))
174 #define PAD_COMPNAME_GEN(po) SvCUR(AvARRAY(PL_comppad_name)[po])
180 =for apidoc m|void|PAD_DUP|PADLIST dstpad|PADLIST srcpad|CLONE_PARAMS* param
183 =for apidoc m|void|PAD_CLONE_VARS|PerlInterpreter *proto_perl \
185 Clone the state variables associated with running and compiling pads.
191 #define PAD_DUP(dstpad, srcpad, param) \
192 if ((srcpad) && !AvREAL(srcpad)) { \
193 /* XXX padlists are real, but pretend to be not */ \
195 (dstpad) = av_dup_inc((srcpad), param); \
196 AvREAL_off(srcpad); \
197 AvREAL_off(dstpad); \
200 (dstpad) = av_dup_inc((srcpad), param);
202 #define PAD_CLONE_VARS(proto_perl, param) \
203 PL_comppad = av_dup(proto_perl->Icomppad, param); \
204 PL_comppad_name = av_dup(proto_perl->Icomppad_name, param); \
205 PL_comppad_name_fill = proto_perl->Icomppad_name_fill; \
206 PL_comppad_name_floor = proto_perl->Icomppad_name_floor; \
207 PL_curpad = (SV**)ptr_table_fetch(PL_ptr_table, \
208 proto_perl->Tcurpad); \
209 PL_min_intro_pending = proto_perl->Imin_intro_pending; \
210 PL_max_intro_pending = proto_perl->Imax_intro_pending; \
211 PL_padix = proto_perl->Ipadix; \
212 PL_padix_floor = proto_perl->Ipadix_floor; \
213 PL_pad_reset_pending = proto_perl->Ipad_reset_pending; \
214 PL_cop_seqmax = proto_perl->Icop_seqmax;