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