This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
more cleanups for change#4539
[perl5.git] / cop.h
CommitLineData
a0d0e21e 1/* cop.h
79072805 2 *
4eb8286e 3 * Copyright (c) 1991-1999, Larry Wall
79072805
LW
4 *
5 * You may distribute under the terms of either the GNU General Public
6 * License or the Artistic License, as specified in the README file.
7 *
79072805
LW
8 */
9
79072805
LW
10struct cop {
11 BASEOP
a0d0e21e 12 char * cop_label; /* label for this construct */
79072805
LW
13 HV * cop_stash; /* package line was compiled in */
14 GV * cop_filegv; /* file the following line # is from */
a0d0e21e
LW
15 U32 cop_seq; /* parse sequence number */
16 I32 cop_arybase; /* array base this line was compiled with */
79072805 17 line_t cop_line; /* line # of this command */
599cee73 18 SV * cop_warnings; /* lexical warnings bitmask */
79072805
LW
19};
20
21#define Nullcop Null(COP*)
22
cc49e20b
GS
23#define CopFILEGV(c) (c)->cop_filegv
24#define CopFILEGV_set(c,gv) ((c)->cop_filegv = gv)
25#define CopFILESV(c) (CopFILEGV(c) ? GvSV(CopFILEGV(c)) : Nullsv)
26#define CopFILEAV(c) (CopFILEGV(c) ? GvAV(CopFILEGV(c)) : Nullav)
27#define CopFILE(c) (CopFILESV(c) ? SvPVX(CopFILESV(c)) : Nullch)
28#define CopLINE(c) ((c)->cop_line)
29#define CopLINE_set(c,l) ((c)->cop_line = (l))
30
79072805
LW
31/*
32 * Here we have some enormously heavy (or at least ponderous) wizardry.
33 */
34
35/* subroutine context */
36struct block_sub {
37 CV * cv;
38 GV * gv;
463ee0b2 39 GV * dfoutgv;
6d4ff0d2 40#ifndef USE_THREADS
79072805 41 AV * savearray;
6d4ff0d2 42#endif /* USE_THREADS */
79072805
LW
43 AV * argarray;
44 U16 olddepth;
45 U8 hasargs;
cd06dffe 46 U8 lval; /* XXX merge lval and hasargs? */
79072805
LW
47};
48
49#define PUSHSUB(cx) \
50 cx->blk_sub.cv = cv; \
79072805 51 cx->blk_sub.olddepth = CvDEPTH(cv); \
cd06dffe
GS
52 cx->blk_sub.hasargs = hasargs; \
53 cx->blk_sub.lval = PL_op->op_private & \
54 (OPpLVAL_INTRO|OPpENTERSUB_INARGS);
79072805
LW
55
56#define PUSHFORMAT(cx) \
57 cx->blk_sub.cv = cv; \
58 cx->blk_sub.gv = gv; \
4633a7c4 59 cx->blk_sub.hasargs = 0; \
3280af22 60 cx->blk_sub.dfoutgv = PL_defoutgv; \
4633a7c4 61 (void)SvREFCNT_inc(cx->blk_sub.dfoutgv)
79072805 62
6d4ff0d2
MB
63#ifdef USE_THREADS
64#define POPSAVEARRAY() NOOP
65#else
66#define POPSAVEARRAY() \
67 STMT_START { \
3280af22 68 SvREFCNT_dec(GvAV(PL_defgv)); \
a8bba7fa 69 GvAV(PL_defgv) = cx->blk_sub.savearray; \
6d4ff0d2
MB
70 } STMT_END
71#endif /* USE_THREADS */
72
b0d9ce38
GS
73#define POPSUB(cx,sv) \
74 STMT_START { \
a8bba7fa 75 if (cx->blk_sub.hasargs) { \
6d4ff0d2 76 POPSAVEARRAY(); \
d8b46c1b 77 /* abandon @_ if it got reified */ \
a8bba7fa
GS
78 if (AvREAL(cx->blk_sub.argarray)) { \
79 SSize_t fill = AvFILLp(cx->blk_sub.argarray); \
80 SvREFCNT_dec(cx->blk_sub.argarray); \
81 cx->blk_sub.argarray = newAV(); \
82 av_extend(cx->blk_sub.argarray, fill); \
83 AvFLAGS(cx->blk_sub.argarray) = AVf_REIFY; \
84 PL_curpad[0] = (SV*)cx->blk_sub.argarray; \
d8b46c1b 85 } \
79072805 86 } \
b0d9ce38
GS
87 sv = (SV*)cx->blk_sub.cv; \
88 if (sv && (CvDEPTH((CV*)sv) = cx->blk_sub.olddepth)) \
89 sv = Nullsv; \
90 } STMT_END
91
92#define LEAVESUB(sv) \
93 STMT_START { \
94 if (sv) \
95 SvREFCNT_dec(sv); \
96 } STMT_END
79072805
LW
97
98#define POPFORMAT(cx) \
4633a7c4
LW
99 setdefout(cx->blk_sub.dfoutgv); \
100 SvREFCNT_dec(cx->blk_sub.dfoutgv);
79072805
LW
101
102/* eval context */
103struct block_eval {
104 I32 old_in_eval;
105 I32 old_op_type;
106 char * old_name;
107 OP * old_eval_root;
748a9306 108 SV * cur_text;
79072805
LW
109};
110
8990e307 111#define PUSHEVAL(cx,n,fgv) \
3280af22 112 cx->blk_eval.old_in_eval = PL_in_eval; \
a8bba7fa 113 cx->blk_eval.old_op_type = PL_op->op_type; \
79072805 114 cx->blk_eval.old_name = n; \
a8bba7fa 115 cx->blk_eval.old_eval_root = PL_eval_root; \
3280af22 116 cx->blk_eval.cur_text = PL_linestr;
79072805
LW
117
118#define POPEVAL(cx) \
3280af22 119 PL_in_eval = cx->blk_eval.old_in_eval; \
79072805 120 optype = cx->blk_eval.old_op_type; \
3280af22 121 PL_eval_root = cx->blk_eval.old_eval_root;
79072805
LW
122
123/* loop context */
124struct block_loop {
125 char * label;
126 I32 resetsp;
127 OP * redo_op;
128 OP * next_op;
129 OP * last_op;
130 SV ** itervar;
131 SV * itersave;
5f05dabc 132 SV * iterlval;
79072805 133 AV * iterary;
89ea2908
GA
134 IV iterix;
135 IV itermax;
79072805
LW
136};
137
138#define PUSHLOOP(cx, ivar, s) \
38a230cb
GS
139 cx->blk_loop.label = PL_curcop->cop_label; \
140 cx->blk_loop.resetsp = s - PL_stack_base; \
79072805
LW
141 cx->blk_loop.redo_op = cLOOP->op_redoop; \
142 cx->blk_loop.next_op = cLOOP->op_nextop; \
143 cx->blk_loop.last_op = cLOOP->op_lastop; \
7a4c00b4 144 if (cx->blk_loop.itervar = (ivar)) \
44a8e56a
PP
145 cx->blk_loop.itersave = SvREFCNT_inc(*cx->blk_loop.itervar);\
146 cx->blk_loop.iterlval = Nullsv; \
147 cx->blk_loop.iterary = Nullav; \
148 cx->blk_loop.iterix = -1;
79072805
LW
149
150#define POPLOOP(cx) \
a8bba7fa
GS
151 SvREFCNT_dec(cx->blk_loop.iterlval); \
152 if (cx->blk_loop.itervar) { \
153 sv_2mortal(*(cx->blk_loop.itervar)); \
154 *(cx->blk_loop.itervar) = cx->blk_loop.itersave; \
44a8e56a 155 } \
a8bba7fa
GS
156 if (cx->blk_loop.iterary && cx->blk_loop.iterary != PL_curstack)\
157 SvREFCNT_dec(cx->blk_loop.iterary);
79072805
LW
158
159/* context common to subroutines, evals and loops */
160struct block {
161 I32 blku_oldsp; /* stack pointer to copy stuff down to */
162 COP * blku_oldcop; /* old curcop pointer */
163 I32 blku_oldretsp; /* return stack index */
164 I32 blku_oldmarksp; /* mark stack index */
165 I32 blku_oldscopesp; /* scope stack index */
166 PMOP * blku_oldpm; /* values of pattern match vars */
167 U8 blku_gimme; /* is this block running in list context? */
168
169 union {
170 struct block_sub blku_sub;
171 struct block_eval blku_eval;
172 struct block_loop blku_loop;
173 } blk_u;
174};
175#define blk_oldsp cx_u.cx_blk.blku_oldsp
176#define blk_oldcop cx_u.cx_blk.blku_oldcop
177#define blk_oldretsp cx_u.cx_blk.blku_oldretsp
178#define blk_oldmarksp cx_u.cx_blk.blku_oldmarksp
179#define blk_oldscopesp cx_u.cx_blk.blku_oldscopesp
180#define blk_oldpm cx_u.cx_blk.blku_oldpm
181#define blk_gimme cx_u.cx_blk.blku_gimme
182#define blk_sub cx_u.cx_blk.blk_u.blku_sub
183#define blk_eval cx_u.cx_blk.blk_u.blku_eval
184#define blk_loop cx_u.cx_blk.blk_u.blku_loop
185
186/* Enter a block. */
8990e307 187#define PUSHBLOCK(cx,t,sp) CXINC, cx = &cxstack[cxstack_ix], \
79072805 188 cx->cx_type = t, \
3280af22
NIS
189 cx->blk_oldsp = sp - PL_stack_base, \
190 cx->blk_oldcop = PL_curcop, \
1aff0e91 191 cx->blk_oldmarksp = PL_markstack_ptr - PL_markstack, \
3280af22 192 cx->blk_oldscopesp = PL_scopestack_ix, \
1aff0e91 193 cx->blk_oldretsp = PL_retstack_ix, \
3280af22 194 cx->blk_oldpm = PL_curpm, \
79072805 195 cx->blk_gimme = gimme; \
bf49b057 196 DEBUG_l( PerlIO_printf(Perl_debug_log, "Entering block %ld, type %s\n", \
1aff0e91 197 (long)cxstack_ix, PL_block_type[CxTYPE(cx)]); )
79072805
LW
198
199/* Exit a block (RETURN and LAST). */
a0d0e21e 200#define POPBLOCK(cx,pm) cx = &cxstack[cxstack_ix--], \
1aff0e91 201 newsp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
202 PL_curcop = cx->blk_oldcop, \
203 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
204 PL_scopestack_ix = cx->blk_oldscopesp, \
205 PL_retstack_ix = cx->blk_oldretsp, \
206 pm = cx->blk_oldpm, \
207 gimme = cx->blk_gimme; \
bf49b057 208 DEBUG_l( PerlIO_printf(Perl_debug_log, "Leaving block %ld, type %s\n", \
22c35a8c 209 (long)cxstack_ix+1,PL_block_type[CxTYPE(cx)]); )
79072805
LW
210
211/* Continue a block elsewhere (NEXT and REDO). */
3280af22 212#define TOPBLOCK(cx) cx = &cxstack[cxstack_ix], \
1aff0e91 213 PL_stack_sp = PL_stack_base + cx->blk_oldsp, \
3280af22
NIS
214 PL_markstack_ptr = PL_markstack + cx->blk_oldmarksp, \
215 PL_scopestack_ix = cx->blk_oldscopesp, \
2158dd9b
GS
216 PL_retstack_ix = cx->blk_oldretsp, \
217 PL_curpm = cx->blk_oldpm
79072805
LW
218
219/* substitution context */
220struct subst {
221 I32 sbu_iters;
222 I32 sbu_maxiters;
22e551b9 223 I32 sbu_rflags;
4633a7c4 224 I32 sbu_oldsave;
71be2cbc
PP
225 bool sbu_once;
226 bool sbu_rxtainted;
79072805
LW
227 char * sbu_orig;
228 SV * sbu_dstr;
229 SV * sbu_targ;
230 char * sbu_s;
231 char * sbu_m;
232 char * sbu_strend;
c90c0ff4 233 void * sbu_rxres;
c07a80fd 234 REGEXP * sbu_rx;
79072805
LW
235};
236#define sb_iters cx_u.cx_subst.sbu_iters
237#define sb_maxiters cx_u.cx_subst.sbu_maxiters
22e551b9 238#define sb_rflags cx_u.cx_subst.sbu_rflags
4633a7c4 239#define sb_oldsave cx_u.cx_subst.sbu_oldsave
71be2cbc
PP
240#define sb_once cx_u.cx_subst.sbu_once
241#define sb_rxtainted cx_u.cx_subst.sbu_rxtainted
79072805
LW
242#define sb_orig cx_u.cx_subst.sbu_orig
243#define sb_dstr cx_u.cx_subst.sbu_dstr
244#define sb_targ cx_u.cx_subst.sbu_targ
245#define sb_s cx_u.cx_subst.sbu_s
246#define sb_m cx_u.cx_subst.sbu_m
247#define sb_strend cx_u.cx_subst.sbu_strend
c90c0ff4 248#define sb_rxres cx_u.cx_subst.sbu_rxres
c07a80fd 249#define sb_rx cx_u.cx_subst.sbu_rx
79072805
LW
250
251#define PUSHSUBST(cx) CXINC, cx = &cxstack[cxstack_ix], \
252 cx->sb_iters = iters, \
253 cx->sb_maxiters = maxiters, \
22e551b9 254 cx->sb_rflags = r_flags, \
4633a7c4 255 cx->sb_oldsave = oldsave, \
71be2cbc
PP
256 cx->sb_once = once, \
257 cx->sb_rxtainted = rxtainted, \
79072805
LW
258 cx->sb_orig = orig, \
259 cx->sb_dstr = dstr, \
260 cx->sb_targ = targ, \
261 cx->sb_s = s, \
262 cx->sb_m = m, \
263 cx->sb_strend = strend, \
c90c0ff4 264 cx->sb_rxres = Null(void*), \
c07a80fd 265 cx->sb_rx = rx, \
c90c0ff4
PP
266 cx->cx_type = CXt_SUBST; \
267 rxres_save(&cx->sb_rxres, rx)
79072805 268
c90c0ff4
PP
269#define POPSUBST(cx) cx = &cxstack[cxstack_ix--]; \
270 rxres_free(&cx->sb_rxres)
79072805
LW
271
272struct context {
6b35e009 273 U32 cx_type; /* what kind of context this is */
79072805
LW
274 union {
275 struct block cx_blk;
276 struct subst cx_subst;
277 } cx_u;
278};
6b35e009
GS
279
280#define CXTYPEMASK 0xff
79072805
LW
281#define CXt_NULL 0
282#define CXt_SUB 1
283#define CXt_EVAL 2
284#define CXt_LOOP 3
285#define CXt_SUBST 4
286#define CXt_BLOCK 5
287
6b35e009
GS
288/* private flags for CXt_EVAL */
289#define CXp_REAL 0x00000100 /* truly eval'', not a lookalike */
290
291#define CxTYPE(c) ((c)->cx_type & CXTYPEMASK)
292#define CxREALEVAL(c) (((c)->cx_type & (CXt_EVAL|CXp_REAL)) == (CXt_EVAL|CXp_REAL))
293
79072805
LW
294#define CXINC (cxstack_ix < cxstack_max ? ++cxstack_ix : (cxstack_ix = cxinc()))
295
296/* "gimme" values */
a0d0e21e
LW
297#define G_SCALAR 0
298#define G_ARRAY 1
54310121 299#define G_VOID 128 /* skip this bit when adding flags below */
79072805 300
864dbfa3 301/* extra flags for Perl_call_* routines */
a0d0e21e
LW
302#define G_DISCARD 2 /* Call FREETMPS. */
303#define G_EVAL 4 /* Assume eval {} around subroutine call. */
304#define G_NOARGS 8 /* Don't construct a @_ array. */
54310121 305#define G_KEEPERR 16 /* Append errors to $@, don't overwrite it */
491527d0 306#define G_NODEBUG 32 /* Disable debugging at toplevel. */
e336de0d 307
faef0170
HS
308/* flag bits for PL_in_eval */
309#define EVAL_NULL 0 /* not in an eval */
310#define EVAL_INEVAL 1 /* some enclosing scope is an eval */
311#define EVAL_WARNONLY 2 /* used by yywarn() when calling yyerror() */
864dbfa3 312#define EVAL_KEEPERR 4 /* set by Perl_call_sv if G_KEEPERR */
faef0170 313
e336de0d
GS
314/* Support for switching (stack and block) contexts.
315 * This ensures magic doesn't invalidate local stack and cx pointers.
316 */
317
e788e7d3
GS
318#define PERLSI_UNKNOWN -1
319#define PERLSI_UNDEF 0
320#define PERLSI_MAIN 1
321#define PERLSI_MAGIC 2
322#define PERLSI_SORT 3
323#define PERLSI_SIGNAL 4
324#define PERLSI_OVERLOAD 5
325#define PERLSI_DESTROY 6
326#define PERLSI_WARNHOOK 7
327#define PERLSI_DIEHOOK 8
328#define PERLSI_REQUIRE 9
e336de0d
GS
329
330struct stackinfo {
331 AV * si_stack; /* stack for current runlevel */
332 PERL_CONTEXT * si_cxstack; /* context stack for runlevel */
333 I32 si_cxix; /* current context index */
334 I32 si_cxmax; /* maximum allocated index */
335 I32 si_type; /* type of runlevel */
336 struct stackinfo * si_prev;
337 struct stackinfo * si_next;
338 I32 * si_markbase; /* where markstack begins for us.
339 * currently used only with DEBUGGING,
340 * but not #ifdef-ed for bincompat */
341};
342
343typedef struct stackinfo PERL_SI;
344
3280af22
NIS
345#define cxstack (PL_curstackinfo->si_cxstack)
346#define cxstack_ix (PL_curstackinfo->si_cxix)
347#define cxstack_max (PL_curstackinfo->si_cxmax)
e336de0d
GS
348
349#ifdef DEBUGGING
3280af22 350# define SET_MARKBASE PL_curstackinfo->si_markbase = PL_markstack_ptr
e336de0d
GS
351#else
352# define SET_MARKBASE NOOP
353#endif
354
d3acc0f7 355#define PUSHSTACKi(type) \
e336de0d 356 STMT_START { \
3280af22 357 PERL_SI *next = PL_curstackinfo->si_next; \
e336de0d
GS
358 if (!next) { \
359 next = new_stackinfo(32, 2048/sizeof(PERL_CONTEXT) - 1); \
3280af22
NIS
360 next->si_prev = PL_curstackinfo; \
361 PL_curstackinfo->si_next = next; \
e336de0d
GS
362 } \
363 next->si_type = type; \
364 next->si_cxix = -1; \
365 AvFILLp(next->si_stack) = 0; \
3280af22
NIS
366 SWITCHSTACK(PL_curstack,next->si_stack); \
367 PL_curstackinfo = next; \
e336de0d
GS
368 SET_MARKBASE; \
369 } STMT_END
370
e788e7d3 371#define PUSHSTACK PUSHSTACKi(PERLSI_UNKNOWN)
d3acc0f7 372
3095d977
GS
373/* POPSTACK works with PL_stack_sp, so it may need to be bracketed by
374 * PUTBACK/SPAGAIN to flush/refresh any local SP that may be active */
d3acc0f7 375#define POPSTACK \
e336de0d 376 STMT_START { \
de616352 377 djSP; \
3280af22 378 PERL_SI *prev = PL_curstackinfo->si_prev; \
e336de0d 379 if (!prev) { \
bf49b057 380 PerlIO_printf(Perl_error_log, "panic: POPSTACK\n"); \
e336de0d
GS
381 my_exit(1); \
382 } \
3280af22 383 SWITCHSTACK(PL_curstack,prev->si_stack); \
e336de0d 384 /* don't free prev here, free them all at the END{} */ \
3280af22 385 PL_curstackinfo = prev; \
e336de0d
GS
386 } STMT_END
387
388#define POPSTACK_TO(s) \
389 STMT_START { \
3280af22 390 while (PL_curstack != s) { \
bac4b2ad 391 dounwind(-1); \
d3acc0f7 392 POPSTACK; \
bac4b2ad 393 } \
e336de0d 394 } STMT_END