Commit | Line | Data |
---|---|---|
a0d0e21e | 1 | /* cop.h |
79072805 | 2 | * |
1129b882 | 3 | * Copyright (C) 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, |
8622e0e2 | 4 | * 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009 by Larry Wall and others |
79072805 LW |
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 | * | |
8622e0e2 | 9 | * Control ops (cops) are one of the two ops OP_NEXTSTATE and OP_DBSTATE, |
775e5718 | 10 | * that (loosely speaking) are statement separators. |
3ffa1527 JH |
11 | * They hold information important for lexical state and error reporting. |
12 | * At run time, PL_curcop is set to point to the most recently executed cop, | |
a3985cdc | 13 | * and thus can be used to determine our current state. |
79072805 LW |
14 | */ |
15 | ||
0d2925a6 | 16 | /* A jmpenv packages the state required to perform a proper non-local jump. |
1c98cc53 DM |
17 | * Note that there is a PL_start_env initialized when perl starts, and |
18 | * PL_top_env points to this initially, so PL_top_env should always be | |
19 | * non-null. | |
0d2925a6 | 20 | * |
1c98cc53 DM |
21 | * Existence of a non-null PL_top_env->je_prev implies it is valid to call |
22 | * longjmp() at that runlevel (we make sure PL_start_env.je_prev is always | |
0d2925a6 DM |
23 | * null to ensure this). |
24 | * | |
25 | * je_mustcatch, when set at any runlevel to TRUE, means eval ops must | |
26 | * establish a local jmpenv to handle exception traps. Care must be taken | |
27 | * to restore the previous value of je_mustcatch before exiting the | |
28 | * stack frame iff JMPENV_PUSH was not called in that stack frame. | |
29 | * GSAR 97-03-27 | |
30 | */ | |
31 | ||
062c5cf9 TC |
32 | MSVC_DIAG_IGNORE(4324) |
33 | ||
0d2925a6 DM |
34 | struct jmpenv { |
35 | struct jmpenv * je_prev; | |
caa674f3 | 36 | Sigjmp_buf je_buf; /* uninit if je_prev is NULL */ |
0d2925a6 | 37 | int je_ret; /* last exception thrown */ |
5bfa3d72 | 38 | bool je_mustcatch; /* longjmp()s must be caught locally */ |
a68090fe | 39 | U16 je_old_delaymagic; /* saved PL_delaymagic */ |
9449f0d6 | 40 | SSize_t je_old_stack_hwm; |
0d2925a6 DM |
41 | }; |
42 | ||
062c5cf9 TC |
43 | MSVC_DIAG_RESTORE |
44 | ||
0d2925a6 DM |
45 | typedef struct jmpenv JMPENV; |
46 | ||
9449f0d6 DM |
47 | #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY |
48 | # define JE_OLD_STACK_HWM_zero PL_start_env.je_old_stack_hwm = 0 | |
49 | # define JE_OLD_STACK_HWM_save(je) \ | |
50 | (je).je_old_stack_hwm = PL_curstackinfo->si_stack_hwm | |
51 | # define JE_OLD_STACK_HWM_restore(je) \ | |
978b1859 DM |
52 | if (PL_curstackinfo->si_stack_hwm < (je).je_old_stack_hwm) \ |
53 | PL_curstackinfo->si_stack_hwm = (je).je_old_stack_hwm | |
9449f0d6 DM |
54 | #else |
55 | # define JE_OLD_STACK_HWM_zero NOOP | |
56 | # define JE_OLD_STACK_HWM_save(je) NOOP | |
57 | # define JE_OLD_STACK_HWM_restore(je) NOOP | |
58 | #endif | |
59 | ||
0d2925a6 DM |
60 | /* |
61 | * How to build the first jmpenv. | |
62 | * | |
63 | * top_env needs to be non-zero. It points to an area | |
64 | * in which longjmp() stuff is stored, as C callstack | |
65 | * info there at least is thread specific this has to | |
66 | * be per-thread. Otherwise a 'die' in a thread gives | |
67 | * that thread the C stack of last thread to do an eval {}! | |
68 | */ | |
69 | ||
70 | #define JMPENV_BOOTSTRAP \ | |
71 | STMT_START { \ | |
1f4fbd3b MS |
72 | PERL_POISON_EXPR(PoisonNew(&PL_start_env, 1, JMPENV));\ |
73 | PL_top_env = &PL_start_env; \ | |
74 | PL_start_env.je_prev = NULL; \ | |
75 | PL_start_env.je_ret = -1; \ | |
76 | PL_start_env.je_mustcatch = TRUE; \ | |
77 | PL_start_env.je_old_delaymagic = 0; \ | |
9449f0d6 | 78 | JE_OLD_STACK_HWM_zero; \ |
0d2925a6 DM |
79 | } STMT_END |
80 | ||
81 | /* | |
82 | * PERL_FLEXIBLE_EXCEPTIONS | |
0bbec57d | 83 | * |
0d2925a6 DM |
84 | * All the flexible exceptions code has been removed. |
85 | * See the following threads for details: | |
86 | * | |
0bbec57d MM |
87 | * Message-Id: 20040713143217.GB1424@plum.flirble.org |
88 | * https://www.nntp.perl.org/group/perl.perl5.porters/2004/07/msg93041.html | |
89 | * | |
0d2925a6 | 90 | * Joshua's original patches (which weren't applied) and discussion: |
0bbec57d | 91 | * |
0d2925a6 DM |
92 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01396.html |
93 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01489.html | |
94 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01491.html | |
95 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg01608.html | |
96 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02144.html | |
97 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1998-02/msg02998.html | |
0bbec57d | 98 | * |
0d2925a6 | 99 | * Chip's reworked patch and discussion: |
0bbec57d | 100 | * |
0d2925a6 | 101 | * http://www.xray.mpe.mpg.de/mailing-lists/perl5-porters/1999-03/msg00520.html |
0bbec57d | 102 | * |
0d2925a6 DM |
103 | * The flaw in these patches (which went unnoticed at the time) was |
104 | * that they moved some code that could potentially die() out of the | |
105 | * region protected by the setjmp()s. This caused exceptions within | |
106 | * END blocks and such to not be handled by the correct setjmp(). | |
c77726bb | 107 | * |
0d2925a6 DM |
108 | * The original patches that introduces flexible exceptions were: |
109 | * | |
47ef154c DB |
110 | * https://github.com/Perl/perl5/commit/312caa8e97f1c7ee342a9895c2f0e749625b4929 |
111 | * https://github.com/Perl/perl5/commit/14dd3ad8c9bf82cf09798a22cc89a9862dfd6d1a | |
c77726bb | 112 | * |
0d2925a6 DM |
113 | */ |
114 | ||
115 | #define dJMPENV JMPENV cur_env | |
116 | ||
112f4f94 | 117 | #define JMPENV_PUSH(v) \ |
0d2925a6 | 118 | STMT_START { \ |
112f4f94 | 119 | DEBUG_l({ \ |
aeff225d | 120 | int i = 0; \ |
112f4f94 | 121 | JMPENV *p = PL_top_env; \ |
1f4fbd3b | 122 | while (p) { i++; p = p->je_prev; } \ |
f31da5f4 YO |
123 | Perl_deb(aTHX_ "JMPENV_PUSH pre level=%d in %s at %s:%d\n", \ |
124 | i, SAFE_FUNCTION__, __FILE__, __LINE__); \ | |
112f4f94 | 125 | }); \ |
1f4fbd3b | 126 | cur_env.je_prev = PL_top_env; \ |
9449f0d6 | 127 | JE_OLD_STACK_HWM_save(cur_env); \ |
f9e2b18f TC |
128 | /* setjmp() is callable in limited contexts which does not */ \ |
129 | /* include assignment, so switch() instead */ \ | |
130 | switch (PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK)) { \ | |
131 | case 0: cur_env.je_ret = 0; break; \ | |
132 | case 1: cur_env.je_ret = 1; break; \ | |
133 | case 2: cur_env.je_ret = 2; break; \ | |
134 | case 3: cur_env.je_ret = 3; break; \ | |
135 | default: Perl_croak(aTHX_ "panic: unexpected setjmp() result\n"); \ | |
136 | } \ | |
9449f0d6 | 137 | JE_OLD_STACK_HWM_restore(cur_env); \ |
1f4fbd3b MS |
138 | PL_top_env = &cur_env; \ |
139 | cur_env.je_mustcatch = FALSE; \ | |
140 | cur_env.je_old_delaymagic = PL_delaymagic; \ | |
112f4f94 | 141 | DEBUG_l({ \ |
aeff225d YO |
142 | int i = 0; \ |
143 | JMPENV *p = PL_top_env; \ | |
144 | while (p) { i++; p = p->je_prev; } \ | |
f31da5f4 YO |
145 | Perl_deb(aTHX_ "JMPENV_PUSH level=%d ret=%d in %s at %s:%d\n", \ |
146 | i, cur_env.je_ret, SAFE_FUNCTION__, __FILE__, __LINE__); \ | |
112f4f94 | 147 | }); \ |
aeff225d | 148 | (v) = cur_env.je_ret; \ |
0d2925a6 DM |
149 | } STMT_END |
150 | ||
151 | #define JMPENV_POP \ | |
152 | STMT_START { \ | |
112f4f94 | 153 | DEBUG_l({ \ |
1f4fbd3b MS |
154 | int i = -1; JMPENV *p = PL_top_env; \ |
155 | while (p) { i++; p = p->je_prev; } \ | |
f31da5f4 YO |
156 | Perl_deb(aTHX_ "JMPENV_POP level=%d in %s at %s:%d\n", \ |
157 | i, SAFE_FUNCTION__, __FILE__, __LINE__);}) \ | |
1f4fbd3b MS |
158 | assert(PL_top_env == &cur_env); \ |
159 | PL_delaymagic = cur_env.je_old_delaymagic; \ | |
160 | PL_top_env = cur_env.je_prev; \ | |
0d2925a6 DM |
161 | } STMT_END |
162 | ||
163 | #define JMPENV_JUMP(v) \ | |
164 | STMT_START { \ | |
112f4f94 | 165 | DEBUG_l({ \ |
1f4fbd3b MS |
166 | int i = -1; JMPENV *p = PL_top_env; \ |
167 | while (p) { i++; p = p->je_prev; } \ | |
f31da5f4 YO |
168 | Perl_deb(aTHX_ "JMPENV_JUMP(%d) level=%d in %s at %s:%d\n", \ |
169 | (int)(v), i, SAFE_FUNCTION__, __FILE__, __LINE__);}) \ | |
f9e2b18f TC |
170 | if (PL_top_env->je_prev) { \ |
171 | assert((v) >= 0 && (v) <= 3); \ | |
1f4fbd3b | 172 | PerlProc_longjmp(PL_top_env->je_buf, (v)); \ |
f9e2b18f | 173 | } \ |
1f4fbd3b MS |
174 | if ((v) == 2) \ |
175 | PerlProc_exit(STATUS_EXIT); \ | |
112f4f94 | 176 | PerlIO_printf(PerlIO_stderr(), "panic: top_env, v=%d\n", (int)(v)); \ |
1f4fbd3b | 177 | PerlProc_exit(1); \ |
0d2925a6 DM |
178 | } STMT_END |
179 | ||
180 | #define CATCH_GET (PL_top_env->je_mustcatch) | |
1c98cc53 DM |
181 | #define CATCH_SET(v) \ |
182 | STMT_START { \ | |
112f4f94 | 183 | DEBUG_l( \ |
1f4fbd3b | 184 | Perl_deb(aTHX_ \ |
f31da5f4 | 185 | "JUMPLEVEL set catch %d => %d (for %p) in %s at %s:%d\n", \ |
112f4f94 | 186 | PL_top_env->je_mustcatch, (v), (void*)PL_top_env, \ |
f31da5f4 | 187 | SAFE_FUNCTION__, __FILE__, __LINE__);) \ |
1f4fbd3b | 188 | PL_top_env->je_mustcatch = (v); \ |
1c98cc53 | 189 | } STMT_END |
0d2925a6 | 190 | |
20439bc7 | 191 | /* |
3f620621 | 192 | =for apidoc_section $COP |
20439bc7 Z |
193 | */ |
194 | ||
195 | typedef struct refcounted_he COPHH; | |
196 | ||
197 | #define COPHH_KEY_UTF8 REFCOUNTED_HE_KEY_UTF8 | |
d36f8b33 | 198 | #define COPHH_EXISTS REFCOUNTED_HE_EXISTS |
20439bc7 Z |
199 | |
200 | /* | |
1607e393 KW |
201 | =for apidoc Amx|SV *|cophh_fetch_pv |const COPHH *cophh|const char *key |U32 hash|U32 flags |
202 | =for apidoc_item|SV *|cophh_fetch_pvn|const COPHH *cophh|const char *key|STRLEN keylen|U32 hash|U32 flags | |
699ef60a KW |
203 | =for apidoc_item|SV *|cophh_fetch_pvs|const COPHH *cophh| "key" |U32 flags |
204 | =for apidoc_item|SV *|cophh_fetch_sv |const COPHH *cophh| SV *key |U32 hash|U32 flags | |
205 | ||
206 | These look up the entry in the cop hints hash C<cophh> with the key specified by | |
207 | C<key> (and C<keylen> in the C<pvn> form), returning that value as a mortal | |
208 | scalar copy, or C<&PL_sv_placeholder> if there is no value associated with the | |
209 | key. | |
210 | ||
211 | The forms differ in how the key is specified. | |
212 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
213 | In the C<pvs> form, the key is a C language string literal. | |
214 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
215 | the string, which hence, may contain embedded-NUL characters. | |
216 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
217 | using C<L</SvPV_const>>. | |
218 | ||
219 | C<hash> is a precomputed hash of the key string, or zero if it has not been | |
220 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
221 | automatically at compile time. | |
222 | ||
223 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. | |
224 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
225 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
226 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
227 | the octets. | |
20439bc7 | 228 | |
5af38e47 KW |
229 | =for apidoc Amnh||COPHH_KEY_UTF8 |
230 | ||
20439bc7 | 231 | =cut |
9d0ade3b | 232 | |
20439bc7 Z |
233 | */ |
234 | ||
9d0ade3b KW |
235 | #define cophh_fetch_pvn(cophh, key, keylen, hash, flags) \ |
236 | Perl_refcounted_he_fetch_pvn(aTHX_ cophh, key, keylen, hash, \ | |
237 | (flags & COPHH_KEY_UTF8)) | |
20439bc7 | 238 | |
9d0ade3b KW |
239 | #define cophh_fetch_pvs(cophh, key, flags) \ |
240 | Perl_refcounted_he_fetch_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, \ | |
241 | (flags & COPHH_KEY_UTF8)) | |
20439bc7 | 242 | |
9d0ade3b KW |
243 | #define cophh_fetch_pv(cophh, key, hash, flags) \ |
244 | Perl_refcounted_he_fetch_pv(aTHX_ cophh, key, hash, \ | |
245 | (flags & COPHH_KEY_UTF8)) | |
20439bc7 | 246 | |
9d0ade3b KW |
247 | #define cophh_fetch_sv(cophh, key, hash, flags) \ |
248 | Perl_refcounted_he_fetch_sv(aTHX_ cophh, key, hash, \ | |
249 | (flags & COPHH_KEY_UTF8)) | |
20439bc7 Z |
250 | |
251 | /* | |
c4f35413 | 252 | =for apidoc Amx|bool|cophh_exists_pvn|const COPHH *cophh|const char *key|STRLEN keylen|U32 hash|U32 flags |
d36f8b33 | 253 | |
c4f35413 KW |
254 | These look up the hint entry in the cop C<cop> with the key specified by |
255 | C<key> (and C<keylen> in the C<pvn> form), returning true if a value exists, | |
256 | and false otherwise. | |
d36f8b33 | 257 | |
c4f35413 KW |
258 | The forms differ in how the key is specified. |
259 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
260 | In the C<pvs> form, the key is a C language string literal. | |
261 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
262 | the string, which hence, may contain embedded-NUL characters. | |
263 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
264 | using C<L</SvPV_const>>. | |
d36f8b33 | 265 | |
c4f35413 KW |
266 | C<hash> is a precomputed hash of the key string, or zero if it has not been |
267 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
268 | automatically at compile time. | |
d36f8b33 | 269 | |
c4f35413 KW |
270 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. |
271 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
272 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
273 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
274 | the octets. | |
d36f8b33 LT |
275 | |
276 | =cut | |
277 | */ | |
278 | ||
c4f35413 KW |
279 | #define cophh_exists_pvn(cophh, key, keylen, hash, flags) \ |
280 | cBOOL(Perl_refcounted_he_fetch_pvn(aTHX_ cophh, key, keylen, hash, flags | COPHH_EXISTS)) | |
281 | ||
d36f8b33 LT |
282 | #define cophh_exists_pvs(cophh, key, flags) \ |
283 | cBOOL(Perl_refcounted_he_fetch_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, flags | COPHH_EXISTS)) | |
284 | ||
d36f8b33 LT |
285 | #define cophh_exists_pv(cophh, key, hash, flags) \ |
286 | cBOOL(Perl_refcounted_he_fetch_pv(aTHX_ cophh, key, hash, flags | COPHH_EXISTS)) | |
287 | ||
d36f8b33 LT |
288 | #define cophh_exists_sv(cophh, key, hash, flags) \ |
289 | cBOOL(Perl_refcounted_he_fetch_sv(aTHX_ cophh, key, hash, flags | COPHH_EXISTS)) | |
290 | ||
291 | /* | |
ffd08bbf | 292 | =for apidoc Amx|HV *|cophh_2hv|const COPHH *cophh|U32 flags |
20439bc7 Z |
293 | |
294 | Generates and returns a standard Perl hash representing the full set of | |
2d7f6611 | 295 | key/value pairs in the cop hints hash C<cophh>. C<flags> is currently |
20439bc7 Z |
296 | unused and must be zero. |
297 | ||
298 | =cut | |
299 | */ | |
300 | ||
301 | #define cophh_2hv(cophh, flags) \ | |
302 | Perl_refcounted_he_chain_2hv(aTHX_ cophh, flags) | |
303 | ||
304 | /* | |
ffd08bbf | 305 | =for apidoc Amx|COPHH *|cophh_copy|COPHH *cophh |
20439bc7 | 306 | |
2d7f6611 | 307 | Make and return a complete copy of the cop hints hash C<cophh>. |
20439bc7 Z |
308 | |
309 | =cut | |
310 | */ | |
311 | ||
312 | #define cophh_copy(cophh) Perl_refcounted_he_inc(aTHX_ cophh) | |
313 | ||
314 | /* | |
ffd08bbf | 315 | =for apidoc Amx|void|cophh_free|COPHH *cophh |
20439bc7 | 316 | |
2d7f6611 | 317 | Discard the cop hints hash C<cophh>, freeing all resources associated |
20439bc7 Z |
318 | with it. |
319 | ||
320 | =cut | |
321 | */ | |
322 | ||
323 | #define cophh_free(cophh) Perl_refcounted_he_free(aTHX_ cophh) | |
324 | ||
325 | /* | |
ffd08bbf | 326 | =for apidoc Amx|COPHH *|cophh_new_empty |
20439bc7 Z |
327 | |
328 | Generate and return a fresh cop hints hash containing no entries. | |
329 | ||
330 | =cut | |
331 | */ | |
332 | ||
333 | #define cophh_new_empty() ((COPHH *)NULL) | |
334 | ||
335 | /* | |
1607e393 KW |
336 | =for apidoc Amx|COPHH *|cophh_store_pv |COPHH *cophh|const char *key |U32 hash|SV *value|U32 flags |
337 | =for apidoc_item|COPHH *|cophh_store_pvn|COPHH *cophh|const char *key|STRLEN keylen|U32 hash|SV *value|U32 flags | |
51505c5b KW |
338 | =for apidoc_item|COPHH *|cophh_store_pvs|COPHH *cophh| "key" |SV *value|U32 flags |
339 | =for apidoc_item|COPHH *|cophh_store_sv |COPHH *cophh| SV *key |U32 hash|SV *value|U32 flags | |
20439bc7 | 340 | |
51505c5b KW |
341 | These store a value, associated with a key, in the cop hints hash C<cophh>, |
342 | and return the modified hash. The returned hash pointer is in general | |
20439bc7 Z |
343 | not the same as the hash pointer that was passed in. The input hash is |
344 | consumed by the function, and the pointer to it must not be subsequently | |
345 | used. Use L</cophh_copy> if you need both hashes. | |
346 | ||
2d7f6611 | 347 | C<value> is the scalar value to store for this key. C<value> is copied |
51505c5b KW |
348 | by these functions, which thus do not take ownership of any reference |
349 | to it, and hence later changes to the scalar will not be reflected in the value | |
350 | visible in the cop hints hash. Complex types of scalar will not be stored with | |
351 | referential integrity, but will be coerced to strings. | |
352 | ||
353 | The forms differ in how the key is specified. In all forms, the key is pointed | |
354 | to by C<key>. | |
355 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
356 | In the C<pvs> form, the key is a C language string literal. | |
357 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
358 | the string, which hence, may contain embedded-NUL characters. | |
359 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
360 | using C<L</SvPV_const>>. | |
361 | ||
362 | C<hash> is a precomputed hash of the key string, or zero if it has not been | |
363 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
364 | automatically at compile time. | |
365 | ||
366 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. | |
367 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
368 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
369 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
370 | the octets. | |
20439bc7 Z |
371 | |
372 | =cut | |
373 | */ | |
374 | ||
51505c5b KW |
375 | #define cophh_store_pvn(cophh, key, keylen, hash, value, flags) \ |
376 | Perl_refcounted_he_new_pvn(aTHX_ cophh, key, keylen, hash, value, flags) | |
20439bc7 Z |
377 | |
378 | #define cophh_store_pvs(cophh, key, value, flags) \ | |
379 | Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, value, flags) | |
380 | ||
20439bc7 Z |
381 | #define cophh_store_pv(cophh, key, hash, value, flags) \ |
382 | Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, value, flags) | |
383 | ||
20439bc7 Z |
384 | #define cophh_store_sv(cophh, key, hash, value, flags) \ |
385 | Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, value, flags) | |
386 | ||
387 | /* | |
1607e393 KW |
388 | =for apidoc Amx|COPHH *|cophh_delete_pv |COPHH *cophh|const char *key |U32 hash|U32 flags |
389 | =for apidoc_item|COPHH *|cophh_delete_pvn|COPHH *cophh|const char *key|STRLEN keylen|U32 hash|U32 flags | |
226b86f1 KW |
390 | =for apidoc_item|COPHH *|cophh_delete_pvs|COPHH *cophh| "key" |U32 flags |
391 | =for apidoc_item|COPHH *|cophh_delete_sv |COPHH *cophh| SV *key |U32 hash|U32 flags | |
20439bc7 | 392 | |
226b86f1 KW |
393 | These delete a key and its associated value from the cop hints hash C<cophh>, |
394 | and return the modified hash. The returned hash pointer is in general | |
20439bc7 Z |
395 | not the same as the hash pointer that was passed in. The input hash is |
396 | consumed by the function, and the pointer to it must not be subsequently | |
397 | used. Use L</cophh_copy> if you need both hashes. | |
398 | ||
226b86f1 KW |
399 | The forms differ in how the key is specified. In all forms, the key is pointed |
400 | to by C<key>. | |
401 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
402 | In the C<pvs> form, the key is a C language string literal. | |
403 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
404 | the string, which hence, may contain embedded-NUL characters. | |
405 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
406 | using C<L</SvPV_const>>. | |
20439bc7 | 407 | |
226b86f1 KW |
408 | C<hash> is a precomputed hash of the key string, or zero if it has not been |
409 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
410 | automatically at compile time. | |
20439bc7 | 411 | |
226b86f1 KW |
412 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. |
413 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
414 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
415 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
416 | the octets. | |
20439bc7 Z |
417 | |
418 | =cut | |
419 | */ | |
420 | ||
226b86f1 KW |
421 | #define cophh_delete_pvn(cophh, key, keylen, hash, flags) \ |
422 | Perl_refcounted_he_new_pvn(aTHX_ cophh, key, keylen, hash, \ | |
423 | (SV *)NULL, flags) | |
424 | ||
20439bc7 Z |
425 | #define cophh_delete_pvs(cophh, key, flags) \ |
426 | Perl_refcounted_he_new_pvn(aTHX_ cophh, STR_WITH_LEN(key), 0, \ | |
1f4fbd3b | 427 | (SV *)NULL, flags) |
20439bc7 | 428 | |
20439bc7 Z |
429 | #define cophh_delete_pv(cophh, key, hash, flags) \ |
430 | Perl_refcounted_he_new_pv(aTHX_ cophh, key, hash, (SV *)NULL, flags) | |
431 | ||
20439bc7 Z |
432 | #define cophh_delete_sv(cophh, key, hash, flags) \ |
433 | Perl_refcounted_he_new_sv(aTHX_ cophh, key, hash, (SV *)NULL, flags) | |
0d2925a6 | 434 | |
5ac1e9b2 | 435 | #include "mydtrace.h" |
0d2925a6 | 436 | |
88df5f01 FC |
437 | struct cop { |
438 | BASEOP | |
439 | /* On LP64 putting this here takes advantage of the fact that BASEOP isn't | |
440 | an exact multiple of 8 bytes to save structure padding. */ | |
441 | line_t cop_line; /* line # of this command */ | |
442 | /* label for this construct is now stored in cop_hints_hash */ | |
57843af0 | 443 | #ifdef USE_ITHREADS |
88df5f01 | 444 | PADOFFSET cop_stashoff; /* offset into PL_stashpad, for the |
1f4fbd3b | 445 | package the line was compiled in */ |
6760f691 | 446 | char * cop_file; /* rcpv containing name of file this command is from */ |
57843af0 | 447 | #else |
88df5f01 | 448 | HV * cop_stash; /* package line was compiled in */ |
6600fe70 | 449 | GV * cop_filegv; /* name of GV file this command is from */ |
57843af0 | 450 | #endif |
88df5f01 FC |
451 | U32 cop_hints; /* hints bits from pragmata */ |
452 | U32 cop_seq; /* parse sequence number */ | |
f8552c1a YO |
453 | char * cop_warnings; /* Lexical warnings bitmask vector. |
454 | Refcounted shared copy of ${^WARNING_BITS}. | |
455 | This pointer either points at one of the | |
456 | magic values for warnings, or it points | |
457 | at a buffer constructed with rcpv_new(). | |
458 | Use the RCPV_LEN() macro to get its length. | |
459 | */ | |
88df5f01 FC |
460 | /* compile time state of %^H. See the comment in op.c for how this is |
461 | used to recreate a hash to return from caller. */ | |
20439bc7 | 462 | COPHH * cop_hints_hash; |
9f601cf3 TC |
463 | /* for now just a bitmask stored here. |
464 | If we get sufficient features this may become a pointer. | |
465 | How these flags are stored is subject to change without | |
466 | notice. Use the macros to test for features. | |
467 | */ | |
468 | U32 cop_features; | |
79072805 LW |
469 | }; |
470 | ||
add0fa58 KW |
471 | /* |
472 | =for apidoc Am|const char *|CopFILE|const COP * c | |
473 | Returns the name of the file associated with the C<COP> C<c> | |
474 | ||
6760f691 YO |
475 | =for apidoc Am|const char *|CopFILE_LEN|const COP * c |
476 | Returns the length of the file associated with the C<COP> C<c> | |
477 | ||
6abc6edf | 478 | =for apidoc Am|line_t|CopLINE|const COP * c |
add0fa58 KW |
479 | Returns the line number in the source code associated with the C<COP> C<c> |
480 | ||
481 | =for apidoc Am|AV *|CopFILEAV|const COP * c | |
9c913148 TC |
482 | Returns the AV associated with the C<COP> C<c>, creating it if necessary. |
483 | ||
484 | =for apidoc Am|AV *|CopFILEAVn|const COP * c | |
485 | Returns the AV associated with the C<COP> C<c>, returning NULL if it | |
486 | doesn't already exist. | |
add0fa58 KW |
487 | |
488 | =for apidoc Am|SV *|CopFILESV|const COP * c | |
489 | Returns the SV associated with the C<COP> C<c> | |
490 | ||
491 | =for apidoc Am|void|CopFILE_set|COP * c|const char * pv | |
492 | Makes C<pv> the name of the file associated with the C<COP> C<c> | |
493 | ||
6760f691 YO |
494 | =for apidoc Am|void|CopFILE_setn|COP * c|const char * pv|STRLEN len |
495 | Makes C<pv> the name of the file associated with the C<COP> C<c> | |
496 | ||
497 | =for apidoc Am|void|CopFILE_copy|COP * dst|COP * src | |
498 | Efficiently copies the cop file name from one COP to another. Wraps | |
499 | the required logic to do a refcounted copy under threads or not. | |
500 | ||
501 | =for apidoc Am|void|CopFILE_free|COP * c | |
502 | Frees the file data in a cop. Under the hood this is a refcounting | |
503 | operation. | |
504 | ||
add0fa58 KW |
505 | =for apidoc Am|GV *|CopFILEGV|const COP * c |
506 | Returns the GV associated with the C<COP> C<c> | |
507 | ||
508 | =for apidoc CopFILEGV_set | |
509 | Available only on unthreaded perls. Makes C<pv> the name of the file | |
510 | associated with the C<COP> C<c> | |
511 | ||
7f02c139 KW |
512 | =for apidoc Am|HV *|CopSTASH|const COP * c |
513 | Returns the stash associated with C<c>. | |
514 | ||
515 | =for apidoc Am|bool|CopSTASH_eq|const COP * c|const HV * hv | |
516 | Returns a boolean as to whether or not C<hv> is the stash associated with C<c>. | |
517 | ||
518 | =for apidoc Am|bool|CopSTASH_set|COP * c|HV * hv | |
519 | Set the stash associated with C<c> to C<hv>. | |
520 | ||
521 | =for apidoc Am|char *|CopSTASHPV|const COP * c | |
522 | Returns the package name of the stash associated with C<c>, or C<NULL> if no | |
523 | associated stash | |
524 | ||
525 | =for apidoc Am|void|CopSTASHPV_set|COP * c|const char * pv | |
526 | Set the package name of the stash associated with C<c>, to the NUL-terminated C | |
527 | string C<p>, creating the package if necessary. | |
528 | ||
add0fa58 KW |
529 | =cut |
530 | */ | |
531 | ||
6760f691 YO |
532 | /* |
533 | =for apidoc Am|RCPV *|RCPVx|char *pv | |
534 | Returns the RCPV structure (struct rcpv) for a refcounted | |
535 | string pv created with C<rcpv_new()>. | |
624f6f53 YO |
536 | No checks are performed to ensure that C<pv> was actually allocated |
537 | with C<rcpv_new()>, it is the callers responsibility to ensure that | |
538 | this is the case. | |
6760f691 YO |
539 | |
540 | =for apidoc Am|RCPV *|RCPV_REFCOUNT|char *pv | |
541 | Returns the refcount for a pv created with C<rcpv_new()>. | |
624f6f53 YO |
542 | No checks are performed to ensure that C<pv> was actually allocated |
543 | with C<rcpv_new()>, it is the callers responsibility to ensure that | |
544 | this is the case. | |
545 | ||
546 | =for apidoc Am|RCPV *|RCPV_REFCNT_inc|char *pv | |
547 | Increments the refcount for a C<char *> pointer which was created | |
548 | with a call to C<rcpv_new()>. Same as calling rcpv_copy(). | |
549 | No checks are performed to ensure that C<pv> was actually allocated | |
550 | with C<rcpv_new()>, it is the callers responsibility to ensure that | |
551 | this is the case. | |
552 | ||
553 | =for apidoc Am|RCPV *|RCPV_REFCNT_dec|char *pv | |
554 | Decrements the refcount for a C<char *> pointer which was created | |
555 | with a call to C<rcpv_new()>. Same as calling rcpv_free(). | |
556 | No checks are performed to ensure that C<pv> was actually allocated | |
557 | with C<rcpv_new()>, it is the callers responsibility to ensure that | |
558 | this is the case. | |
6760f691 YO |
559 | |
560 | =for apidoc Am|RCPV *|RCPV_LEN|char *pv | |
a511d7dc YO |
561 | Returns the length of a pv created with C<rcpv_new()>. |
562 | Note that this reflects the length of the string from the callers | |
563 | point of view, it does not include the mandatory null which is | |
564 | always injected at the end of the string by rcpv_new(). | |
624f6f53 YO |
565 | No checks are performed to ensure that C<pv> was actually allocated |
566 | with C<rcpv_new()>, it is the callers responsibility to ensure that | |
567 | this is the case. | |
6760f691 YO |
568 | |
569 | =cut | |
570 | */ | |
571 | ||
572 | struct rcpv { | |
573 | STRLEN refcount; /* UV would mean a 64 refcnt on | |
574 | 32 bit builds with -Duse64bitint */ | |
a511d7dc YO |
575 | STRLEN len; /* length of string including mandatory |
576 | null byte at end */ | |
6760f691 YO |
577 | char pv[1]; |
578 | }; | |
579 | typedef struct rcpv RCPV; | |
580 | ||
a511d7dc YO |
581 | #define RCPVf_USE_STRLEN (1 << 0) |
582 | #define RCPVf_NO_COPY (1 << 1) | |
583 | #define RCPVf_ALLOW_EMPTY (1 << 2) | |
584 | ||
6760f691 YO |
585 | #define RCPVx(pv_arg) ((RCPV *)((pv_arg) - STRUCT_OFFSET(struct rcpv, pv))) |
586 | #define RCPV_REFCOUNT(pv) (RCPVx(pv)->refcount) | |
a511d7dc | 587 | #define RCPV_LEN(pv) (RCPVx(pv)->len-1) /* len always includes space for a null */ |
624f6f53 YO |
588 | #define RCPV_REFCNT_inc(pv) rcpv_copy(pv) |
589 | #define RCPV_REFCNT_dec(pv) rcpv_free(pv) | |
6760f691 | 590 | |
57843af0 | 591 | #ifdef USE_ITHREADS |
a8899380 | 592 | |
6760f691 YO |
593 | # define CopFILE(c) ((c)->cop_file) |
594 | # define CopFILE_LEN(c) (CopFILE(c) ? RCPV_LEN(CopFILE(c)) : 0) | |
1dc74fdb | 595 | # define CopFILEGV(c) (CopFILE(c) \ |
1f4fbd3b | 596 | ? gv_fetchfile(CopFILE(c)) : NULL) |
0bbec57d | 597 | |
6760f691 YO |
598 | # define CopFILE_set_x(c,pv) ((c)->cop_file = rcpv_new((pv),0,RCPVf_USE_STRLEN)) |
599 | # define CopFILE_setn_x(c,pv,l) ((c)->cop_file = rcpv_new((pv),(l),0)) | |
600 | # define CopFILE_free_x(c) ((c)->cop_file = rcpv_free((c)->cop_file)) | |
601 | # define CopFILE_copy_x(dst,src) ((dst)->cop_file = rcpv_copy((src)->cop_file)) | |
602 | ||
603 | /* change condition to 1 && to enable this debugging */ | |
604 | # define CopFILE_debug(c,t,rk) \ | |
605 | if (0 && (c)->cop_file) \ | |
606 | PerlIO_printf(Perl_debug_log, \ | |
607 | "%-14s THX:%p OP:%p PV:%p rc: " \ | |
608 | "%6zu fn: '%.*s' at %s line %d\n", \ | |
609 | (t), aTHX, (c), (c)->cop_file, \ | |
610 | RCPV_REFCOUNT((c)->cop_file)-rk, \ | |
611 | (int)RCPV_LEN((c)->cop_file), \ | |
612 | (c)->cop_file,__FILE__,__LINE__) \ | |
613 | ||
614 | ||
615 | # define CopFILE_set(c,pv) \ | |
616 | STMT_START { \ | |
617 | CopFILE_set_x(c,pv); \ | |
618 | CopFILE_debug(c,"CopFILE_set", 0); \ | |
619 | } STMT_END | |
620 | ||
621 | # define CopFILE_setn(c,pv,l) \ | |
622 | STMT_START { \ | |
623 | CopFILE_setn_x(c,pv,l); \ | |
624 | CopFILE_debug(c,"CopFILE_setn", 0); \ | |
625 | } STMT_END | |
626 | ||
627 | # define CopFILE_copy(dst,src) \ | |
628 | STMT_START { \ | |
629 | CopFILE_copy_x((dst),(src)); \ | |
630 | CopFILE_debug((dst),"CopFILE_copy", 0); \ | |
631 | } STMT_END | |
632 | ||
633 | # define CopFILE_free(c) \ | |
634 | STMT_START { \ | |
635 | CopFILE_debug((c),"CopFILE_free", 1); \ | |
636 | CopFILE_free_x(c); \ | |
637 | } STMT_END | |
638 | ||
1dc74fdb FC |
639 | |
640 | # define CopFILESV(c) (CopFILE(c) \ | |
1f4fbd3b | 641 | ? GvSV(gv_fetchfile(CopFILE(c))) : NULL) |
1dc74fdb | 642 | # define CopFILEAV(c) (CopFILE(c) \ |
1f4fbd3b | 643 | ? GvAV(gv_fetchfile(CopFILE(c))) : NULL) |
1dc74fdb | 644 | # define CopFILEAVx(c) (assert_(CopFILE(c)) \ |
1f4fbd3b | 645 | GvAV(gv_fetchfile(CopFILE(c)))) |
9c913148 | 646 | # define CopFILEAVn(c) (cop_file_avn(c)) |
d4d03940 FC |
647 | # define CopSTASH(c) PL_stashpad[(c)->cop_stashoff] |
648 | # define CopSTASH_set(c,hv) ((c)->cop_stashoff = (hv) \ | |
1f4fbd3b MS |
649 | ? alloccopstash(hv) \ |
650 | : 0) | |
a8899380 TC |
651 | |
652 | #else /* Above: yes threads; Below no threads */ | |
653 | ||
57843af0 | 654 | # define CopFILEGV(c) ((c)->cop_filegv) |
f4dd75d9 | 655 | # define CopFILEGV_set(c,gv) ((c)->cop_filegv = (GV*)SvREFCNT_inc(gv)) |
1dc74fdb | 656 | # define CopFILE_set(c,pv) CopFILEGV_set((c), gv_fetchfile(pv)) |
6760f691 | 657 | # define CopFILE_copy(dst,src) CopFILEGV_set((dst),CopFILEGV(src)) |
1dc74fdb FC |
658 | # define CopFILE_setn(c,pv,l) CopFILEGV_set((c), gv_fetchfile_flags((pv),(l),0)) |
659 | # define CopFILESV(c) (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : NULL) | |
660 | # define CopFILEAV(c) (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : NULL) | |
661 | # ifdef DEBUGGING | |
662 | # define CopFILEAVx(c) (assert(CopFILEGV(c)), GvAV(CopFILEGV(c))) | |
663 | # else | |
664 | # define CopFILEAVx(c) (GvAV(CopFILEGV(c))) | |
665 | # endif | |
9c913148 | 666 | # define CopFILEAVn(c) (CopFILEGV(c) ? GvAVn(CopFILEGV(c)) : NULL) |
6600fe70 | 667 | # define CopFILE(c) (CopFILEGV(c) /* +2 for '_<' */ \ |
1f4fbd3b | 668 | ? GvNAME(CopFILEGV(c))+2 : NULL) |
6760f691 YO |
669 | # define CopFILE_LEN(c) (CopFILEGV(c) /* -2 for '_<' */ \ |
670 | ? GvNAMELEN(CopFILEGV(c))-2 : 0) | |
57843af0 | 671 | # define CopSTASH(c) ((c)->cop_stash) |
f4dd75d9 | 672 | # define CopSTASH_set(c,hv) ((c)->cop_stash = (hv)) |
a0714e2c | 673 | # define CopFILE_free(c) (SvREFCNT_dec(CopFILEGV(c)),(CopFILEGV(c) = NULL)) |
05ec9bb3 | 674 | |
57843af0 | 675 | #endif /* USE_ITHREADS */ |
20439bc7 | 676 | |
d4d03940 FC |
677 | #define CopSTASHPV(c) (CopSTASH(c) ? HvNAME_get(CopSTASH(c)) : NULL) |
678 | /* cop_stash is not refcounted */ | |
679 | #define CopSTASHPV_set(c,pv) CopSTASH_set((c), gv_stashpv(pv,GV_ADD)) | |
680 | #define CopSTASH_eq(c,hv) (CopSTASH(c) == (hv)) | |
d4d03940 | 681 | |
20439bc7 Z |
682 | #define CopHINTHASH_get(c) ((COPHH*)((c)->cop_hints_hash)) |
683 | #define CopHINTHASH_set(c,h) ((c)->cop_hints_hash = (h)) | |
684 | ||
a73e2f42 TC |
685 | #define CopFEATURES_setfrom(dst, src) ((dst)->cop_features = (src)->cop_features) |
686 | ||
20439bc7 | 687 | /* |
1607e393 KW |
688 | =for apidoc Am|SV *|cop_hints_fetch_pv |const COP *cop|const char *key |U32 hash|U32 flags |
689 | =for apidoc_item|SV *|cop_hints_fetch_pvn|const COP *cop|const char *key|STRLEN keylen|U32 hash|U32 flags | |
690 | =for apidoc_item|SV *|cop_hints_fetch_pvs|const COP *cop| "key" |U32 flags | |
c7c0c00c | 691 | =for apidoc_item|SV *|cop_hints_fetch_sv |const COP *cop| SV *key |U32 hash|U32 flags |
20439bc7 | 692 | |
c7c0c00c KW |
693 | These look up the hint entry in the cop C<cop> with the key specified by |
694 | C<key> (and C<keylen> in the C<pvn> form), returning that value as a mortal | |
695 | scalar copy, or C<&PL_sv_placeholder> if there is no value associated with the | |
696 | key. | |
20439bc7 | 697 | |
c7c0c00c KW |
698 | The forms differ in how the key is specified. |
699 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
700 | In the C<pvs> form, the key is a C language string literal. | |
701 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
702 | the string, which hence, may contain embedded-NUL characters. | |
703 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
704 | using C<L</SvPV_const>>. | |
20439bc7 | 705 | |
c7c0c00c KW |
706 | C<hash> is a precomputed hash of the key string, or zero if it has not been |
707 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
708 | automatically at compile time. | |
20439bc7 | 709 | |
c7c0c00c KW |
710 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. |
711 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
712 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
713 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
714 | the octets. | |
20439bc7 Z |
715 | |
716 | =cut | |
717 | */ | |
718 | ||
c7c0c00c KW |
719 | #define cop_hints_fetch_pvn(cop, key, keylen, hash, flags) \ |
720 | cophh_fetch_pvn(CopHINTHASH_get(cop), key, keylen, hash, flags) | |
721 | ||
20439bc7 Z |
722 | #define cop_hints_fetch_pvs(cop, key, flags) \ |
723 | cophh_fetch_pvs(CopHINTHASH_get(cop), key, flags) | |
724 | ||
20439bc7 Z |
725 | #define cop_hints_fetch_pv(cop, key, hash, flags) \ |
726 | cophh_fetch_pv(CopHINTHASH_get(cop), key, hash, flags) | |
727 | ||
20439bc7 Z |
728 | #define cop_hints_fetch_sv(cop, key, hash, flags) \ |
729 | cophh_fetch_sv(CopHINTHASH_get(cop), key, hash, flags) | |
730 | ||
731 | /* | |
1607e393 KW |
732 | =for apidoc Am|bool|cop_hints_exists_pv |const COP *cop|const char *key|U32 hash |U32 flags |
733 | =for apidoc_item|bool|cop_hints_exists_pvn|const COP *cop|const char *key|STRLEN keylen|U32 hash|U32 flags | |
d809c335 KW |
734 | =for apidoc_item|bool|cop_hints_exists_pvs|const COP *cop| "key" |U32 flags |
735 | =for apidoc_item|bool|cop_hints_exists_sv |const COP *cop| SV *key |U32 hash|U32 flags | |
bbb58664 | 736 | |
d809c335 KW |
737 | These look up the hint entry in the cop C<cop> with the key specified by |
738 | C<key> (and C<keylen> in the C<pvn> form), returning true if a value exists, | |
739 | and false otherwise. | |
bbb58664 | 740 | |
d809c335 KW |
741 | The forms differ in how the key is specified. In all forms, the key is pointed |
742 | to by C<key>. | |
743 | In the plain C<pv> form, the key is a C language NUL-terminated string. | |
744 | In the C<pvs> form, the key is a C language string literal. | |
745 | In the C<pvn> form, an additional parameter, C<keylen>, specifies the length of | |
746 | the string, which hence, may contain embedded-NUL characters. | |
747 | In the C<sv> form, C<*key> is an SV, and the key is the PV extracted from that. | |
748 | using C<L</SvPV_const>>. | |
bbb58664 | 749 | |
d809c335 KW |
750 | C<hash> is a precomputed hash of the key string, or zero if it has not been |
751 | precomputed. This parameter is omitted from the C<pvs> form, as it is computed | |
752 | automatically at compile time. | |
bbb58664 | 753 | |
d809c335 KW |
754 | The only flag currently used from the C<flags> parameter is C<COPHH_KEY_UTF8>. |
755 | It is illegal to set this in the C<sv> form. In the C<pv*> forms, it specifies | |
756 | whether the key octets are interpreted as UTF-8 (if set) or as Latin-1 (if | |
757 | cleared). The C<sv> form uses the underlying SV to determine the UTF-8ness of | |
758 | the octets. | |
bbb58664 LT |
759 | |
760 | =cut | |
761 | */ | |
762 | ||
d809c335 KW |
763 | #define cop_hints_exists_pvn(cop, key, keylen, hash, flags) \ |
764 | cophh_exists_pvn(CopHINTHASH_get(cop), key, keylen, hash, flags) | |
765 | ||
bbb58664 LT |
766 | #define cop_hints_exists_pvs(cop, key, flags) \ |
767 | cophh_exists_pvs(CopHINTHASH_get(cop), key, flags) | |
768 | ||
bbb58664 LT |
769 | #define cop_hints_exists_pv(cop, key, hash, flags) \ |
770 | cophh_exists_pv(CopHINTHASH_get(cop), key, hash, flags) | |
771 | ||
bbb58664 LT |
772 | #define cop_hints_exists_sv(cop, key, hash, flags) \ |
773 | cophh_exists_sv(CopHINTHASH_get(cop), key, hash, flags) | |
774 | ||
775 | /* | |
20439bc7 Z |
776 | =for apidoc Am|HV *|cop_hints_2hv|const COP *cop|U32 flags |
777 | ||
778 | Generates and returns a standard Perl hash representing the full set of | |
2d7f6611 | 779 | hint entries in the cop C<cop>. C<flags> is currently unused and must |
20439bc7 Z |
780 | be zero. |
781 | ||
782 | =cut | |
783 | */ | |
784 | ||
785 | #define cop_hints_2hv(cop, flags) \ | |
786 | cophh_2hv(CopHINTHASH_get(cop), flags) | |
787 | ||
33d29c13 | 788 | /* |
71b454dc KW |
789 | =for apidoc Am|const char *|CopLABEL |COP *const cop |
790 | =for apidoc_item|const char *|CopLABEL_len |COP *const cop|STRLEN *len | |
791 | =for apidoc_item|const char *|CopLABEL_len_flags|COP *const cop|STRLEN *len|U32 *flags | |
33d29c13 | 792 | |
71b454dc | 793 | These return the label attached to a cop. |
33d29c13 | 794 | |
71b454dc KW |
795 | C<CopLABEL_len> and C<CopLABEL_len_flags> additionally store the number of |
796 | bytes comprising the returned label into C<*len>. | |
33d29c13 | 797 | |
71b454dc KW |
798 | C<CopLABEL_len_flags> additionally returns the UTF-8ness of the returned label, |
799 | by setting C<*flags> to 0 or C<SVf_UTF8>. | |
33d29c13 KW |
800 | |
801 | =cut | |
802 | */ | |
803 | ||
aebc0cbe | 804 | #define CopLABEL(c) Perl_cop_fetch_label(aTHX_ (c), NULL, NULL) |
5db1eb8d BF |
805 | #define CopLABEL_len(c,len) Perl_cop_fetch_label(aTHX_ (c), len, NULL) |
806 | #define CopLABEL_len_flags(c,len,flags) Perl_cop_fetch_label(aTHX_ (c), len, flags) | |
dca6062a | 807 | #define CopLABEL_alloc(pv) ((pv)?savepv(pv):NULL) |
57843af0 | 808 | |
ed094faf | 809 | #define CopSTASH_ne(c,hv) (!CopSTASH_eq(c,hv)) |
cc49e20b | 810 | #define CopLINE(c) ((c)->cop_line) |
57843af0 GS |
811 | #define CopLINE_inc(c) (++CopLINE(c)) |
812 | #define CopLINE_dec(c) (--CopLINE(c)) | |
813 | #define CopLINE_set(c,l) (CopLINE(c) = (l)) | |
cc49e20b | 814 | |
248c2a4d | 815 | /* OutCopFILE() is CopFILE for output (caller, die, warn, etc.) */ |
e37778c2 | 816 | #define OutCopFILE(c) CopFILE(c) |
248c2a4d | 817 | |
d5ec2987 | 818 | #define CopHINTS_get(c) ((c)->cop_hints + 0) |
623e6609 | 819 | #define CopHINTS_set(c, h) STMT_START { \ |
1f4fbd3b MS |
820 | (c)->cop_hints = (h); \ |
821 | } STMT_END | |
623e6609 | 822 | |
79072805 LW |
823 | /* |
824 | * Here we have some enormously heavy (or at least ponderous) wizardry. | |
825 | */ | |
826 | ||
827 | /* subroutine context */ | |
828 | struct block_sub { | |
f9c764c5 | 829 | OP * retop; /* op to execute on exit from sub */ |
5b6f7443 | 830 | I32 old_cxsubix; /* previous value of si_cxsubix */ |
adcbf118 | 831 | /* Above here is the same for sub, format and eval. */ |
9aa350eb | 832 | PAD *prevcomppad; /* the caller's PL_comppad */ |
8e663997 | 833 | CV * cv; |
f9c764c5 | 834 | /* Above here is the same for sub and format. */ |
bb172083 | 835 | I32 olddepth; |
3073d04f | 836 | AV *savearray; |
f9c764c5 NC |
837 | }; |
838 | ||
839 | ||
840 | /* format context */ | |
841 | struct block_format { | |
f39bc417 | 842 | OP * retop; /* op to execute on exit from sub */ |
5b6f7443 | 843 | I32 old_cxsubix; /* previous value of si_cxsubix */ |
adcbf118 | 844 | /* Above here is the same for sub, format and eval. */ |
9aa350eb | 845 | PAD *prevcomppad; /* the caller's PL_comppad */ |
8e663997 | 846 | CV * cv; |
f9c764c5 NC |
847 | /* Above here is the same for sub and format. */ |
848 | GV * gv; | |
849 | GV * dfoutgv; | |
79072805 LW |
850 | }; |
851 | ||
4ebe6e95 DM |
852 | /* return a pointer to the current context */ |
853 | ||
854 | #define CX_CUR() (&cxstack[cxstack_ix]) | |
855 | ||
dfe0f39b DM |
856 | /* free all savestack items back to the watermark of the specified context */ |
857 | ||
e992140c | 858 | #define CX_LEAVE_SCOPE(cx) LEAVE_SCOPE(cx->blk_oldsaveix) |
dfe0f39b | 859 | |
5da525e9 DM |
860 | #ifdef DEBUGGING |
861 | /* on debugging builds, poison cx afterwards so we know no code | |
862 | * uses it - because after doing cxstack_ix--, any ties, exceptions etc | |
863 | * may overwrite the current stack frame */ | |
864 | # define CX_POP(cx) \ | |
4ebe6e95 | 865 | assert(CX_CUR() == cx); \ |
5da525e9 DM |
866 | cxstack_ix--; \ |
867 | cx = NULL; | |
868 | #else | |
869 | # define CX_POP(cx) cxstack_ix--; | |
870 | #endif | |
871 | ||
490576d1 | 872 | #define CX_PUSHSUB_GET_LVALUE_MASK(func) \ |
1f4fbd3b MS |
873 | /* If the context is indeterminate, then only the lvalue */ \ |
874 | /* flags that the caller also has are applicable. */ \ | |
875 | ( \ | |
876 | (PL_op->op_flags & OPf_WANT) \ | |
877 | ? OPpENTERSUB_LVAL_MASK \ | |
878 | : !(PL_op->op_private & OPpENTERSUB_LVAL_MASK) \ | |
879 | ? 0 : (U8)func(aTHX) \ | |
880 | ) | |
4587c532 | 881 | |
fad386cb | 882 | /* Restore old @_ */ |
b405d38b | 883 | #define CX_POP_SAVEARRAY(cx) \ |
6d4ff0d2 | 884 | STMT_START { \ |
f2b9631d | 885 | AV *cx_pop_savearray_av = GvAV(PL_defgv); \ |
1f4fbd3b | 886 | GvAV(PL_defgv) = cx->blk_sub.savearray; \ |
656457d0 | 887 | cx->blk_sub.savearray = NULL; \ |
f2b9631d | 888 | SvREFCNT_dec(cx_pop_savearray_av); \ |
6d4ff0d2 | 889 | } STMT_END |
6d4ff0d2 | 890 | |
ecf8e9dd | 891 | /* junk in @_ spells trouble when cloning CVs and in pp_caller(), so don't |
2c204b7a DM |
892 | * leave any (a fast av_clear(ary), basically). |
893 | * New code should probably be using Perl_clear_defarray_simple() | |
894 | * and/or Perl_clear_defarray() | |
895 | */ | |
8e09340b GS |
896 | #define CLEAR_ARGARRAY(ary) \ |
897 | STMT_START { \ | |
1f4fbd3b | 898 | AvFILLp(ary) = -1; \ |
2c204b7a | 899 | av_remove_offset(ary); \ |
8e09340b | 900 | } STMT_END |
7766f137 | 901 | |
7e27c4a5 | 902 | |
79072805 LW |
903 | /* eval context */ |
904 | struct block_eval { | |
8e663997 | 905 | OP * retop; /* op to execute on exit from eval */ |
5b6f7443 | 906 | I32 old_cxsubix; /* previous value of si_cxsubix */ |
8e663997 | 907 | /* Above here is the same for sub, format and eval. */ |
0f79a09d | 908 | SV * old_namesv; |
79072805 | 909 | OP * old_eval_root; |
748a9306 | 910 | SV * cur_text; |
2090ab20 | 911 | CV * cv; |
abd70938 | 912 | JMPENV * cur_top_env; /* value of PL_top_env when eval CX created */ |
79072805 LW |
913 | }; |
914 | ||
17347a51 NC |
915 | /* If we ever need more than 512 op types, change the shift from 7. |
916 | blku_gimme is actually also only 2 bits, so could be merged with something. | |
917 | */ | |
918 | ||
4c57ced5 DM |
919 | /* blk_u16 bit usage for eval contexts: */ |
920 | ||
f0814129 | 921 | #define CxOLD_IN_EVAL(cx) (((cx)->blk_u16) & 0x3F) /* saved PL_in_eval */ |
4c57ced5 DM |
922 | #define CxEVAL_TXT_REFCNTED(cx) (((cx)->blk_u16) & 0x40) /* cur_text rc++ */ |
923 | #define CxOLD_OP_TYPE(cx) (((cx)->blk_u16) >> 7) /* type of eval op */ | |
85a64632 | 924 | |
79072805 LW |
925 | /* loop context */ |
926 | struct block_loop { | |
022eaa24 | 927 | LOOP * my_op; /* My op, that contains redo, next and last ops. */ |
df530c37 | 928 | union { /* different ways of locating the iteration variable */ |
1f4fbd3b MS |
929 | SV **svp; /* for lexicals: address of pad slot */ |
930 | GV *gv; /* for package vars */ | |
df530c37 | 931 | } itervar_u; |
f0bb9bf7 | 932 | SV *itersave; /* the original iteration var */ |
493b0a3c | 933 | union { |
1f4fbd3b MS |
934 | struct { /* CXt_LOOP_ARY, C<for (@ary)> */ |
935 | AV *ary; /* array being iterated over */ | |
936 | IV ix; /* index relative to base of array */ | |
937 | } ary; | |
938 | struct { /* CXt_LOOP_LIST, C<for (list)> */ | |
79dad5ac | 939 | SSize_t basesp; /* first element of list on stack */ |
1f4fbd3b MS |
940 | IV ix; /* index relative to basesp */ |
941 | } stack; | |
942 | struct { /* CXt_LOOP_LAZYIV, C<for (1..9)> */ | |
943 | IV cur; | |
944 | IV end; | |
945 | } lazyiv; | |
946 | struct { /* CXt_LOOP_LAZYSV C<for ('a'..'z')> */ | |
947 | SV * cur; | |
0c6362ad | 948 | SV * end; /* maximum value (or minimum in reverse) */ |
1f4fbd3b | 949 | } lazysv; |
d01136d6 | 950 | } state_u; |
7766f137 | 951 | #ifdef USE_ITHREADS |
b52de964 | 952 | PAD *oldcomppad; /* needed to map itervar_u.svp during thread clone */ |
f83b46a0 | 953 | #endif |
b52de964 | 954 | }; |
f83b46a0 | 955 | |
9af0df0b DM |
956 | #define CxITERVAR(c) \ |
957 | (CxPADLOOP(c) \ | |
958 | ? (c)->blk_loop.itervar_u.svp \ | |
959 | : ((c)->cx_type & CXp_FOR_GV) \ | |
960 | ? &GvSV((c)->blk_loop.itervar_u.gv) \ | |
961 | : (SV **)&(c)->blk_loop.itervar_u.gv) | |
f83b46a0 | 962 | |
a8b89476 TC |
963 | #define CxLABEL(c) (CopLABEL((c)->blk_oldcop)) |
964 | #define CxLABEL_len(c,len) (CopLABEL_len((c)->blk_oldcop, len)) | |
965 | #define CxLABEL_len_flags(c,len,flags) ((const char *)CopLABEL_len_flags((c)->blk_oldcop, len, flags)) | |
ae423868 | 966 | #define CxHASARGS(c) (((c)->cx_type & CXp_HASARGS) == CXp_HASARGS) |
d9203e03 DM |
967 | |
968 | /* CxLVAL(): the lval flags of the call site: the relevant flag bits from | |
969 | * the op_private field of the calling pp_entersub (or its caller's caller | |
970 | * if the caller's lvalue context isn't known): | |
971 | * OPpLVAL_INTRO: sub used in lvalue context, e.g. f() = 1; | |
972 | * OPpENTERSUB_INARGS (in conjunction with OPpLVAL_INTRO): the | |
973 | * function is being used as a sub arg or as a referent, e.g. | |
974 | * g(...,f(),...) or $r = \f() | |
975 | * OPpDEREF: 2-bit mask indicating e.g. f()->[0]. | |
976 | * Note the contrast with CvLVALUE(), which is a property of the sub | |
977 | * rather than the call site. | |
978 | */ | |
fb1a3d54 | 979 | #define CxLVAL(c) (0 + ((U8)((c)->blk_u16))) |
1956db7e | 980 | |
7766f137 | 981 | |
79072805 | 982 | |
7896dde7 Z |
983 | /* given/when context */ |
984 | struct block_givwhen { | |
1f4fbd3b | 985 | OP *leave_op; |
7896dde7 | 986 | SV *defsv_save; /* the original $_ */ |
0d863452 RH |
987 | }; |
988 | ||
0d863452 | 989 | |
c5369ccc | 990 | |
79072805 LW |
991 | /* context common to subroutines, evals and loops */ |
992 | struct block { | |
446d0787 | 993 | U8 blku_type; /* what kind of context this is */ |
2e0df0e8 | 994 | U8 blku_gimme; /* is this block running in list context? */ |
17347a51 | 995 | U16 blku_u16; /* used by block_sub and block_eval (so far) */ |
e992140c DM |
996 | I32 blku_oldsaveix; /* saved PL_savestack_ix */ |
997 | /* all the fields above must be aligned with same-sized fields as sbu */ | |
c0588928 | 998 | SSize_t blku_oldsp; /* current sp floor: where nextstate pops to */ |
f284fecd | 999 | COP * blku_oldcop; /* old curcop pointer */ |
79072805 | 1000 | PMOP * blku_oldpm; /* values of pattern match vars */ |
bb3300d1 | 1001 | SSize_t blku_old_tmpsfloor; /* saved PL_tmps_floor */ |
f284fecd | 1002 | I32 blku_oldscopesp; /* scope stack index */ |
c0588928 | 1003 | I32 blku_oldmarksp; /* mark stack index */ |
79072805 LW |
1004 | |
1005 | union { | |
1f4fbd3b MS |
1006 | struct block_sub blku_sub; |
1007 | struct block_format blku_format; | |
1008 | struct block_eval blku_eval; | |
1009 | struct block_loop blku_loop; | |
1010 | struct block_givwhen blku_givwhen; | |
79072805 LW |
1011 | } blk_u; |
1012 | }; | |
1013 | #define blk_oldsp cx_u.cx_blk.blku_oldsp | |
1014 | #define blk_oldcop cx_u.cx_blk.blku_oldcop | |
79072805 LW |
1015 | #define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp |
1016 | #define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp | |
1017 | #define blk_oldpm cx_u.cx_blk.blku_oldpm | |
1018 | #define blk_gimme cx_u.cx_blk.blku_gimme | |
446d0787 | 1019 | #define blk_u16 cx_u.cx_blk.blku_u16 |
e992140c | 1020 | #define blk_oldsaveix cx_u.cx_blk.blku_oldsaveix |
ce8bb8d8 | 1021 | #define blk_old_tmpsfloor cx_u.cx_blk.blku_old_tmpsfloor |
79072805 | 1022 | #define blk_sub cx_u.cx_blk.blk_u.blku_sub |
f9c764c5 | 1023 | #define blk_format cx_u.cx_blk.blk_u.blku_format |
79072805 LW |
1024 | #define blk_eval cx_u.cx_blk.blk_u.blku_eval |
1025 | #define blk_loop cx_u.cx_blk.blk_u.blku_loop | |
7896dde7 | 1026 | #define blk_givwhen cx_u.cx_blk.blk_u.blku_givwhen |
79072805 | 1027 | |
5e691bc6 | 1028 | #define CX_DEBUG(cx, action) \ |
d9f81b50 | 1029 | DEBUG_l( \ |
f31da5f4 | 1030 | Perl_deb(aTHX_ "CX %ld %s %s (scope %ld,%ld) (save %ld,%ld) in %s at %s:%d\n",\ |
1f4fbd3b MS |
1031 | (long)cxstack_ix, \ |
1032 | action, \ | |
1033 | PL_block_type[CxTYPE(cx)], \ | |
1034 | (long)PL_scopestack_ix, \ | |
1035 | (long)(cx->blk_oldscopesp), \ | |
1036 | (long)PL_savestack_ix, \ | |
1037 | (long)(cx->blk_oldsaveix), \ | |
f31da5f4 | 1038 | SAFE_FUNCTION__, __FILE__, __LINE__)); |
1c98cc53 | 1039 | |
ed8ff0f3 | 1040 | |
79072805 LW |
1041 | |
1042 | /* substitution context */ | |
1043 | struct subst { | |
e992140c | 1044 | U8 sbu_type; /* same as blku_type */ |
1ed74d04 | 1045 | U8 sbu_rflags; |
e992140c DM |
1046 | U16 sbu_rxtainted; |
1047 | I32 sbu_oldsaveix; /* same as blku_oldsaveix */ | |
a7f94955 | 1048 | /* all the fields above must be aligned with same-sized fields as blk_u */ |
3c6ef0a5 FC |
1049 | SSize_t sbu_iters; |
1050 | SSize_t sbu_maxiters; | |
79072805 LW |
1051 | char * sbu_orig; |
1052 | SV * sbu_dstr; | |
1053 | SV * sbu_targ; | |
1054 | char * sbu_s; | |
1055 | char * sbu_m; | |
1056 | char * sbu_strend; | |
c90c0ff4 | 1057 | void * sbu_rxres; |
c07a80fd | 1058 | REGEXP * sbu_rx; |
79072805 | 1059 | }; |
a0412c00 KW |
1060 | |
1061 | #ifdef PERL_CORE | |
1062 | ||
79072805 LW |
1063 | #define sb_iters cx_u.cx_subst.sbu_iters |
1064 | #define sb_maxiters cx_u.cx_subst.sbu_maxiters | |
22e551b9 | 1065 | #define sb_rflags cx_u.cx_subst.sbu_rflags |
71be2cbc | 1066 | #define sb_rxtainted cx_u.cx_subst.sbu_rxtainted |
79072805 LW |
1067 | #define sb_orig cx_u.cx_subst.sbu_orig |
1068 | #define sb_dstr cx_u.cx_subst.sbu_dstr | |
1069 | #define sb_targ cx_u.cx_subst.sbu_targ | |
1070 | #define sb_s cx_u.cx_subst.sbu_s | |
1071 | #define sb_m cx_u.cx_subst.sbu_m | |
1072 | #define sb_strend cx_u.cx_subst.sbu_strend | |
c90c0ff4 | 1073 | #define sb_rxres cx_u.cx_subst.sbu_rxres |
c07a80fd | 1074 | #define sb_rx cx_u.cx_subst.sbu_rx |
79072805 | 1075 | |
490576d1 | 1076 | # define CX_PUSHSUBST(cx) CXINC, cx = CX_CUR(), \ |
1f4fbd3b MS |
1077 | cx->blk_oldsaveix = oldsave, \ |
1078 | cx->sb_iters = iters, \ | |
1079 | cx->sb_maxiters = maxiters, \ | |
1080 | cx->sb_rflags = r_flags, \ | |
1081 | cx->sb_rxtainted = rxtainted, \ | |
1082 | cx->sb_orig = orig, \ | |
1083 | cx->sb_dstr = dstr, \ | |
1084 | cx->sb_targ = targ, \ | |
1085 | cx->sb_s = s, \ | |
1086 | cx->sb_m = m, \ | |
1087 | cx->sb_strend = strend, \ | |
1088 | cx->sb_rxres = NULL, \ | |
1089 | cx->sb_rx = rx, \ | |
1090 | cx->cx_type = CXt_SUBST | (once ? CXp_ONCE : 0); \ | |
1091 | rxres_save(&cx->sb_rxres, rx); \ | |
1092 | (void)ReREFCNT_inc(rx); \ | |
cf69025f | 1093 | SvREFCNT_inc_void_NN(targ) |
79072805 | 1094 | |
b405d38b | 1095 | # define CX_POPSUBST(cx) \ |
77189b8c DM |
1096 | STMT_START { \ |
1097 | REGEXP *re; \ | |
e4592eb4 | 1098 | assert(CxTYPE(cx) == CXt_SUBST); \ |
1f4fbd3b MS |
1099 | rxres_free(&cx->sb_rxres); \ |
1100 | re = cx->sb_rx; \ | |
1101 | cx->sb_rx = NULL; \ | |
1102 | ReREFCNT_dec(re); \ | |
77189b8c DM |
1103 | SvREFCNT_dec_NN(cx->sb_targ); \ |
1104 | } STMT_END | |
9c105995 NC |
1105 | #endif |
1106 | ||
1107 | #define CxONCE(cx) ((cx)->cx_type & CXp_ONCE) | |
79072805 LW |
1108 | |
1109 | struct context { | |
79072805 | 1110 | union { |
1f4fbd3b MS |
1111 | struct block cx_blk; |
1112 | struct subst cx_subst; | |
79072805 LW |
1113 | } cx_u; |
1114 | }; | |
2e0df0e8 | 1115 | #define cx_type cx_u.cx_subst.sbu_type |
6b35e009 | 1116 | |
76753e7f NC |
1117 | /* If you re-order these, there is also an array of uppercase names in perl.h |
1118 | and a static array of context names in pp_ctl.c */ | |
3701055e | 1119 | #define CXTYPEMASK 0xf |
79646418 | 1120 | #define CXt_NULL 0 /* currently only used for sort BLOCK */ |
7896dde7 | 1121 | #define CXt_WHEN 1 |
76753e7f | 1122 | #define CXt_BLOCK 2 |
7896dde7 Z |
1123 | /* When micro-optimising :-) keep GIVEN next to the LOOPs, as these 5 share a |
1124 | jump table in pp_ctl.c | |
1125 | The first 4 don't have a 'case' in at least one switch statement in pp_ctl.c | |
1126 | */ | |
1127 | #define CXt_GIVEN 3 | |
1128 | ||
1129 | /* be careful of the ordering of these five. Macros like CxTYPE_is_LOOP, | |
93661e56 | 1130 | * CxFOREACH compare ranges */ |
c349b9a0 DM |
1131 | #define CXt_LOOP_ARY 4 /* for (@ary) { ...; } */ |
1132 | #define CXt_LOOP_LAZYSV 5 /* for ('a'..'z') { ...; } */ | |
1133 | #define CXt_LOOP_LAZYIV 6 /* for (1..9) { ...; } */ | |
1134 | #define CXt_LOOP_LIST 7 /* for (1,2,3) { ...; } */ | |
1135 | #define CXt_LOOP_PLAIN 8 /* while (...) { ...; } | |
1136 | or plain block { ...; } */ | |
93661e56 DM |
1137 | #define CXt_SUB 9 |
1138 | #define CXt_FORMAT 10 | |
a1325b90 | 1139 | #define CXt_EVAL 11 /* eval'', eval{}, try{} */ |
93661e56 | 1140 | #define CXt_SUBST 12 |
f79e2ff9 | 1141 | #define CXt_DEFER 13 |
76753e7f | 1142 | /* SUBST doesn't feature in all switch statements. */ |
79072805 | 1143 | |
ae423868 | 1144 | /* private flags for CXt_SUB and CXt_FORMAT */ |
79646418 DM |
1145 | #define CXp_MULTICALL 0x10 /* part of a multicall (so don't tear down |
1146 | context on exit). (not CXt_FORMAT) */ | |
1f27d788 | 1147 | #define CXp_HASARGS 0x20 |
b0065247 DM |
1148 | #define CXp_SUB_RE 0x40 /* code called within regex, i.e. (?{}) */ |
1149 | #define CXp_SUB_RE_FAKE 0x80 /* fake sub CX for (?{}) in current scope */ | |
ae423868 | 1150 | |
6b35e009 | 1151 | /* private flags for CXt_EVAL */ |
1f27d788 | 1152 | #define CXp_REAL 0x20 /* truly eval'', not a lookalike */ |
99dbf645 | 1153 | #define CXp_EVALBLOCK 0x40 /* eval{}, not eval'' or similar */ |
a1325b90 | 1154 | #define CXp_TRY 0x80 /* try {} block */ |
6b35e009 | 1155 | |
7766f137 | 1156 | /* private flags for CXt_LOOP */ |
28e0cf42 | 1157 | |
7896dde7 Z |
1158 | /* this is only set in conjunction with CXp_FOR_GV */ |
1159 | #define CXp_FOR_DEF 0x10 /* foreach using $_ */ | |
28e0cf42 | 1160 | /* these 3 are mutually exclusive */ |
d39c26a6 | 1161 | #define CXp_FOR_LVREF 0x20 /* foreach using \$var */ |
9af0df0b DM |
1162 | #define CXp_FOR_GV 0x40 /* foreach using package var */ |
1163 | #define CXp_FOR_PAD 0x80 /* foreach using lexical var */ | |
28e0cf42 | 1164 | |
9af0df0b | 1165 | #define CxPADLOOP(c) ((c)->cx_type & CXp_FOR_PAD) |
e846cb92 | 1166 | |
1ed74d04 NC |
1167 | /* private flags for CXt_SUBST */ |
1168 | #define CXp_ONCE 0x10 /* What was sbu_once in struct subst */ | |
7766f137 | 1169 | |
6b35e009 | 1170 | #define CxTYPE(c) ((c)->cx_type & CXTYPEMASK) |
7896dde7 | 1171 | #define CxTYPE_is_LOOP(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \ |
113ebcf5 | 1172 | && CxTYPE(cx) <= CXt_LOOP_PLAIN) |
79646418 | 1173 | #define CxMULTICALL(c) ((c)->cx_type & CXp_MULTICALL) |
0588a150 | 1174 | #define CxREALEVAL(c) (((c)->cx_type & (CXTYPEMASK|CXp_REAL)) \ |
1f4fbd3b | 1175 | == (CXt_EVAL|CXp_REAL)) |
99dbf645 | 1176 | #define CxEVALBLOCK(c) (((c)->cx_type & (CXTYPEMASK|CXp_EVALBLOCK)) \ |
1f4fbd3b | 1177 | == (CXt_EVAL|CXp_EVALBLOCK)) |
a1325b90 PE |
1178 | #define CxTRY(c) (((c)->cx_type & (CXTYPEMASK|CXp_TRY)) \ |
1179 | == (CXt_EVAL|CXp_TRY)) | |
113ebcf5 DM |
1180 | #define CxFOREACH(c) ( CxTYPE(cx) >= CXt_LOOP_ARY \ |
1181 | && CxTYPE(cx) <= CXt_LOOP_LIST) | |
6b35e009 | 1182 | |
e5e291f5 PE |
1183 | /* private flags for CXt_DEFER */ |
1184 | #define CXp_FINALLY 0x20 /* `finally` block; semantically identical | |
1185 | * but matters for diagnostic messages */ | |
1186 | ||
99dbf645 PE |
1187 | /* deprecated old name before real try/catch was added */ |
1188 | #define CXp_TRYBLOCK CXp_EVALBLOCK | |
1189 | #define CxTRYBLOCK(c) CxEVALBLOCK(c) | |
1190 | ||
109bf713 | 1191 | #define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc())) |
79072805 | 1192 | |
eb7e169e PE |
1193 | #define G_SCALAR 2 |
1194 | #define G_LIST 3 | |
1195 | #define G_VOID 1 | |
1196 | #define G_WANT 3 | |
1197 | ||
1198 | #ifndef PERL_CORE | |
1199 | /* name prior to 5.31.1 */ | |
1200 | # define G_ARRAY G_LIST | |
1201 | #endif | |
79072805 | 1202 | |
864dbfa3 | 1203 | /* extra flags for Perl_call_* routines */ |
66ff4fb5 | 1204 | #define G_DISCARD 0x4 /* Call FREETMPS. |
1f4fbd3b MS |
1205 | Don't change this without consulting the |
1206 | hash actions codes defined in hv.h */ | |
66ff4fb5 TC |
1207 | #define G_EVAL 0x8 /* Assume eval {} around subroutine call. */ |
1208 | #define G_NOARGS 0x10 /* Don't construct a @_ array. */ | |
1209 | #define G_KEEPERR 0x20 /* Warn for errors, don't overwrite $@ */ | |
1210 | #define G_NODEBUG 0x40 /* Disable debugging at toplevel. */ | |
1211 | #define G_METHOD 0x80 /* Calling method. */ | |
1212 | #define G_FAKINGEVAL 0x100 /* Faking an eval context for call_sv or | |
1f4fbd3b | 1213 | fold_constants. */ |
66ff4fb5 | 1214 | #define G_UNDEF_FILL 0x200 /* Fill the stack with &PL_sv_undef |
1f4fbd3b MS |
1215 | A special case for UNSHIFT in |
1216 | Perl_magic_methcall(). */ | |
66ff4fb5 | 1217 | #define G_WRITING_TO_STDERR 0x400 /* Perl_write_to_stderr() is calling |
1f4fbd3b | 1218 | Perl_magic_methcall(). */ |
66ff4fb5 TC |
1219 | #define G_RE_REPARSING 0x800 /* compiling a run-time /(?{..})/ */ |
1220 | #define G_METHOD_NAMED 0x1000 /* calling named method, eg without :: or ' */ | |
1221 | #define G_RETHROW 0x2000 /* eval_sv(): re-throw any error */ | |
9537c882 | 1222 | #define G_USEHINTS 0x4000 /* eval_sv(): use current hints/features */ |
e336de0d | 1223 | |
faef0170 HS |
1224 | /* flag bits for PL_in_eval */ |
1225 | #define EVAL_NULL 0 /* not in an eval */ | |
1226 | #define EVAL_INEVAL 1 /* some enclosing scope is an eval */ | |
1227 | #define EVAL_WARNONLY 2 /* used by yywarn() when calling yyerror() */ | |
864dbfa3 | 1228 | #define EVAL_KEEPERR 4 /* set by Perl_call_sv if G_KEEPERR */ |
6dc8a9e4 | 1229 | #define EVAL_INREQUIRE 8 /* The code is being required. */ |
a1941760 | 1230 | #define EVAL_RE_REPARSING 0x10 /* eval_sv() called with G_RE_REPARSING */ |
4c57ced5 | 1231 | /* if adding extra bits, make sure they can fit in CxOLD_OP_TYPE() */ |
faef0170 | 1232 | |
e336de0d GS |
1233 | /* Support for switching (stack and block) contexts. |
1234 | * This ensures magic doesn't invalidate local stack and cx pointers. | |
ec834f46 KW |
1235 | * Which one to use (or add) is mostly, but not completely arbitrary: See |
1236 | * http://nntp.perl.org/group/perl.perl5.porters/257169 | |
e336de0d GS |
1237 | */ |
1238 | ||
e788e7d3 GS |
1239 | #define PERLSI_UNKNOWN -1 |
1240 | #define PERLSI_UNDEF 0 | |
1241 | #define PERLSI_MAIN 1 | |
1242 | #define PERLSI_MAGIC 2 | |
1243 | #define PERLSI_SORT 3 | |
1244 | #define PERLSI_SIGNAL 4 | |
1245 | #define PERLSI_OVERLOAD 5 | |
1246 | #define PERLSI_DESTROY 6 | |
1247 | #define PERLSI_WARNHOOK 7 | |
1248 | #define PERLSI_DIEHOOK 8 | |
1249 | #define PERLSI_REQUIRE 9 | |
e1a10f35 | 1250 | #define PERLSI_MULTICALL 10 |
b6d9446c | 1251 | #define PERLSI_REGCOMP 11 |
e336de0d GS |
1252 | |
1253 | struct stackinfo { | |
1254 | AV * si_stack; /* stack for current runlevel */ | |
1255 | PERL_CONTEXT * si_cxstack; /* context stack for runlevel */ | |
9bd87817 NC |
1256 | struct stackinfo * si_prev; |
1257 | struct stackinfo * si_next; | |
e336de0d GS |
1258 | I32 si_cxix; /* current context index */ |
1259 | I32 si_cxmax; /* maximum allocated index */ | |
5b6f7443 | 1260 | I32 si_cxsubix; /* topmost sub/eval/format */ |
e336de0d | 1261 | I32 si_type; /* type of runlevel */ |
ce2f7c3b | 1262 | I32 si_markoff; /* offset where markstack begins for us. |
1f4fbd3b MS |
1263 | * currently used only with DEBUGGING, |
1264 | * but not #ifdef-ed for bincompat */ | |
ba1af285 DM |
1265 | |
1266 | #ifdef PERL_RC_STACK | |
1267 | /* index of first entry in the argument | |
1268 | stack which is not ref-counted. If | |
1269 | set to 0 (default), all stack | |
1270 | elements are ref-counted */ | |
1271 | I32 si_stack_nonrc_base; | |
1272 | #endif | |
1273 | ||
1fced1a2 | 1274 | #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY |
87058c31 DM |
1275 | /* high water mark: for checking if the stack was correctly extended / |
1276 | * tested for extension by each pp function */ | |
1277 | SSize_t si_stack_hwm; | |
1278 | #endif | |
1279 | ||
e336de0d GS |
1280 | }; |
1281 | ||
55d39ade KW |
1282 | /* |
1283 | =for apidoc Ay||PERL_SI | |
1284 | Use this typedef to declare variables that are to hold C<struct stackinfo>. | |
1285 | ||
1286 | =cut | |
1287 | */ | |
e336de0d GS |
1288 | typedef struct stackinfo PERL_SI; |
1289 | ||
3280af22 NIS |
1290 | #define cxstack (PL_curstackinfo->si_cxstack) |
1291 | #define cxstack_ix (PL_curstackinfo->si_cxix) | |
1292 | #define cxstack_max (PL_curstackinfo->si_cxmax) | |
e336de0d GS |
1293 | |
1294 | #ifdef DEBUGGING | |
53175c60 | 1295 | # define SET_MARK_OFFSET \ |
ce2f7c3b | 1296 | PL_curstackinfo->si_markoff = PL_markstack_ptr - PL_markstack |
e336de0d | 1297 | #else |
53175c60 | 1298 | # define SET_MARK_OFFSET NOOP |
e336de0d GS |
1299 | #endif |
1300 | ||
87058c31 | 1301 | #if defined DEBUGGING && !defined DEBUGGING_RE_ONLY |
45d6bfc0 | 1302 | # define PUSHSTACK_INIT_HWM(si) ((si)->si_stack_hwm = 0) |
87058c31 DM |
1303 | #else |
1304 | # define PUSHSTACK_INIT_HWM(si) NOOP | |
1305 | #endif | |
1306 | ||
d724a264 DM |
1307 | /* for backcompat; use push_stackinfo() instead */ |
1308 | ||
d3acc0f7 | 1309 | #define PUSHSTACKi(type) \ |
d724a264 DM |
1310 | STMT_START { \ |
1311 | PL_stack_sp = sp; \ | |
026c6b9c | 1312 | push_stackinfo(type, 0);\ |
d724a264 | 1313 | sp = PL_stack_sp ; \ |
e336de0d GS |
1314 | } STMT_END |
1315 | ||
e788e7d3 | 1316 | #define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN) |
d3acc0f7 | 1317 | |
d724a264 DM |
1318 | |
1319 | /* for backcompat; use pop_stackinfo() instead. | |
1320 | * | |
1321 | * POPSTACK works with PL_stack_sp, so it may need to be bracketed by | |
3095d977 | 1322 | * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */ |
d724a264 DM |
1323 | |
1324 | #define POPSTACK pop_stackinfo() | |
1325 | ||
e336de0d GS |
1326 | |
1327 | #define POPSTACK_TO(s) \ | |
1328 | STMT_START { \ | |
1f4fbd3b MS |
1329 | while (PL_curstack != s) { \ |
1330 | dounwind(-1); \ | |
ba1af285 | 1331 | rpp_obliterate_stack_to(0); \ |
1f4fbd3b MS |
1332 | POPSTACK; \ |
1333 | } \ | |
e336de0d | 1334 | } STMT_END |
923e4eb5 | 1335 | |
d57dfc83 | 1336 | /* |
3f620621 | 1337 | =for apidoc_section $utility |
d57dfc83 KW |
1338 | =for apidoc Amn|bool|IN_PERL_COMPILETIME |
1339 | Returns 1 if this macro is being called during the compilation phase of the | |
1340 | program; otherwise 0; | |
ed8ff0f3 | 1341 | |
d57dfc83 KW |
1342 | =for apidoc Amn|bool|IN_PERL_RUNTIME |
1343 | Returns 1 if this macro is being called during the execution phase of the | |
1344 | program; otherwise 0; | |
ed8ff0f3 | 1345 | |
d57dfc83 KW |
1346 | =cut |
1347 | */ | |
1348 | #define IN_PERL_COMPILETIME cBOOL(PL_curcop == &PL_compiling) | |
1349 | #define IN_PERL_RUNTIME cBOOL(PL_curcop != &PL_compiling) | |
ed8ff0f3 | 1350 | |
9850bf21 | 1351 | /* |
3f620621 | 1352 | =for apidoc_section $multicall |
9850bf21 | 1353 | |
3734d2f5 | 1354 | =for apidoc Amn;||dMULTICALL |
72d33970 | 1355 | Declare local variables for a multicall. See L<perlcall/LIGHTWEIGHT CALLBACKS>. |
9850bf21 | 1356 | |
3734d2f5 | 1357 | =for apidoc Am;||PUSH_MULTICALL|CV* the_cv |
9850bf21 | 1358 | Opening bracket for a lightweight callback. |
0eda4409 | 1359 | See L<perlcall/LIGHTWEIGHT CALLBACKS>. |
9850bf21 | 1360 | |
3734d2f5 | 1361 | =for apidoc Amn;||MULTICALL |
72d33970 | 1362 | Make a lightweight callback. See L<perlcall/LIGHTWEIGHT CALLBACKS>. |
9850bf21 | 1363 | |
3734d2f5 | 1364 | =for apidoc Amn;||POP_MULTICALL |
9850bf21 | 1365 | Closing bracket for a lightweight callback. |
0eda4409 | 1366 | See L<perlcall/LIGHTWEIGHT CALLBACKS>. |
9850bf21 RH |
1367 | |
1368 | =cut | |
1369 | */ | |
1370 | ||
1371 | #define dMULTICALL \ | |
79503099 | 1372 | OP *multicall_cop; \ |
35095fd0 | 1373 | bool multicall_oldcatch |
9850bf21 | 1374 | |
82f35e8b | 1375 | #define PUSH_MULTICALL(the_cv) \ |
b0065247 | 1376 | PUSH_MULTICALL_FLAGS(the_cv, 0) |
81ed78b2 | 1377 | |
b0065247 DM |
1378 | /* Like PUSH_MULTICALL, but allows you to specify extra flags |
1379 | * for the CX stack entry (this isn't part of the public API) */ | |
81ed78b2 | 1380 | |
b0065247 | 1381 | #define PUSH_MULTICALL_FLAGS(the_cv, flags) \ |
9850bf21 | 1382 | STMT_START { \ |
79503099 | 1383 | PERL_CONTEXT *cx; \ |
1f4fbd3b MS |
1384 | CV * const _nOnclAshIngNamE_ = the_cv; \ |
1385 | CV * const cv = _nOnclAshIngNamE_; \ | |
1386 | PADLIST * const padlist = CvPADLIST(cv); \ | |
1387 | multicall_oldcatch = CATCH_GET; \ | |
1388 | CATCH_SET(TRUE); \ | |
1389 | PUSHSTACKi(PERLSI_MULTICALL); \ | |
1390 | cx = cx_pushblock((CXt_SUB|CXp_MULTICALL|flags), (U8)gimme, \ | |
fd9b7244 | 1391 | PL_stack_sp, PL_savestack_ix); \ |
79503099 | 1392 | cx_pushsub(cx, cv, NULL, 0); \ |
1f4fbd3b | 1393 | SAVEOP(); \ |
b0065247 DM |
1394 | if (!(flags & CXp_SUB_RE_FAKE)) \ |
1395 | CvDEPTH(cv)++; \ | |
1f4fbd3b MS |
1396 | if (CvDEPTH(cv) >= 2) \ |
1397 | Perl_pad_push(aTHX_ padlist, CvDEPTH(cv)); \ | |
1398 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \ | |
1399 | multicall_cop = CvSTART(cv); \ | |
9850bf21 RH |
1400 | } STMT_END |
1401 | ||
1402 | #define MULTICALL \ | |
1403 | STMT_START { \ | |
1f4fbd3b MS |
1404 | PL_op = multicall_cop; \ |
1405 | CALLRUNOPS(aTHX); \ | |
9850bf21 RH |
1406 | } STMT_END |
1407 | ||
1408 | #define POP_MULTICALL \ | |
1409 | STMT_START { \ | |
79503099 | 1410 | PERL_CONTEXT *cx; \ |
1f4fbd3b MS |
1411 | cx = CX_CUR(); \ |
1412 | CX_LEAVE_SCOPE(cx); \ | |
a73d8813 | 1413 | cx_popsub_common(cx); \ |
4df352a8 | 1414 | gimme = cx->blk_gimme; \ |
7e27c4a5 | 1415 | PERL_UNUSED_VAR(gimme); /* for API */ \ |
1f4fbd3b MS |
1416 | cx_popblock(cx); \ |
1417 | CX_POP(cx); \ | |
1418 | POPSTACK; \ | |
1419 | CATCH_SET(multicall_oldcatch); \ | |
1420 | SPAGAIN; \ | |
9850bf21 | 1421 | } STMT_END |
b3ca2e83 | 1422 | |
81ed78b2 DM |
1423 | /* Change the CV of an already-pushed MULTICALL CxSUB block. |
1424 | * (this isn't part of the public API) */ | |
1425 | ||
b0065247 | 1426 | #define CHANGE_MULTICALL_FLAGS(the_cv, flags) \ |
81ed78b2 | 1427 | STMT_START { \ |
1f4fbd3b MS |
1428 | CV * const _nOnclAshIngNamE_ = the_cv; \ |
1429 | CV * const cv = _nOnclAshIngNamE_; \ | |
1430 | PADLIST * const padlist = CvPADLIST(cv); \ | |
79503099 | 1431 | PERL_CONTEXT *cx = CX_CUR(); \ |
1f4fbd3b | 1432 | assert(CxMULTICALL(cx)); \ |
a73d8813 | 1433 | cx_popsub_common(cx); \ |
1f4fbd3b | 1434 | cx->cx_type = (CXt_SUB|CXp_MULTICALL|flags); \ |
79503099 | 1435 | cx_pushsub(cx, cv, NULL, 0); \ |
b0065247 DM |
1436 | if (!(flags & CXp_SUB_RE_FAKE)) \ |
1437 | CvDEPTH(cv)++; \ | |
1f4fbd3b MS |
1438 | if (CvDEPTH(cv) >= 2) \ |
1439 | Perl_pad_push(aTHX_ padlist, CvDEPTH(cv)); \ | |
1440 | PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv)); \ | |
1441 | multicall_cop = CvSTART(cv); \ | |
81ed78b2 | 1442 | } STMT_END |
b3ca2e83 | 1443 | /* |
14d04a33 | 1444 | * ex: set ts=8 sts=4 sw=4 et: |
b3ca2e83 | 1445 | */ |