This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Tweak on #8234 (Subject: Re: [PATCH] Warn on use of reference as array elem):
[perl5.git] / scope.h
1 #define SAVEt_ITEM              0
2 #define SAVEt_SV                1
3 #define SAVEt_AV                2
4 #define SAVEt_HV                3
5 #define SAVEt_INT               4
6 #define SAVEt_LONG              5
7 #define SAVEt_I32               6
8 #define SAVEt_IV                7
9 #define SAVEt_SPTR              8
10 #define SAVEt_APTR              9
11 #define SAVEt_HPTR              10
12 #define SAVEt_PPTR              11
13 #define SAVEt_NSTAB             12
14 #define SAVEt_SVREF             13
15 #define SAVEt_GP                14
16 #define SAVEt_FREESV            15
17 #define SAVEt_FREEOP            16
18 #define SAVEt_FREEPV            17
19 #define SAVEt_CLEARSV           18
20 #define SAVEt_DELETE            19
21 #define SAVEt_DESTRUCTOR        20
22 #define SAVEt_REGCONTEXT        21
23 #define SAVEt_STACK_POS         22
24 #define SAVEt_I16               23
25 #define SAVEt_AELEM             24
26 #define SAVEt_HELEM             25
27 #define SAVEt_OP                26
28 #define SAVEt_HINTS             27
29 #define SAVEt_ALLOC             28
30 #define SAVEt_GENERIC_SVREF     29
31 #define SAVEt_DESTRUCTOR_X      30
32 #define SAVEt_VPTR              31
33 #define SAVEt_I8                32
34 #define SAVEt_COMPPAD           33
35 #define SAVEt_GENERIC_PVREF     34
36 #define SAVEt_PADSV             35
37
38 #ifndef SCOPE_SAVES_SIGNAL_MASK
39 #define SCOPE_SAVES_SIGNAL_MASK 0
40 #endif
41
42 #define SSCHECK(need) if (PL_savestack_ix + need > PL_savestack_max) savestack_grow()
43 #define SSPUSHINT(i) (PL_savestack[PL_savestack_ix++].any_i32 = (I32)(i))
44 #define SSPUSHLONG(i) (PL_savestack[PL_savestack_ix++].any_long = (long)(i))
45 #define SSPUSHIV(i) (PL_savestack[PL_savestack_ix++].any_iv = (IV)(i))
46 #define SSPUSHPTR(p) (PL_savestack[PL_savestack_ix++].any_ptr = (void*)(p))
47 #define SSPUSHDPTR(p) (PL_savestack[PL_savestack_ix++].any_dptr = (p))
48 #define SSPUSHDXPTR(p) (PL_savestack[PL_savestack_ix++].any_dxptr = (p))
49 #define SSPOPINT (PL_savestack[--PL_savestack_ix].any_i32)
50 #define SSPOPLONG (PL_savestack[--PL_savestack_ix].any_long)
51 #define SSPOPIV (PL_savestack[--PL_savestack_ix].any_iv)
52 #define SSPOPPTR (PL_savestack[--PL_savestack_ix].any_ptr)
53 #define SSPOPDPTR (PL_savestack[--PL_savestack_ix].any_dptr)
54 #define SSPOPDXPTR (PL_savestack[--PL_savestack_ix].any_dxptr)
55
56 /*
57 =for apidoc Ams||SAVETMPS
58 Opening bracket for temporaries on a callback.  See C<FREETMPS> and
59 L<perlcall>.
60
61 =for apidoc Ams||FREETMPS
62 Closing bracket for temporaries on a callback.  See C<SAVETMPS> and
63 L<perlcall>.
64
65 =for apidoc Ams||ENTER
66 Opening bracket on a callback.  See C<LEAVE> and L<perlcall>.
67
68 =for apidoc Ams||LEAVE
69 Closing bracket on a callback.  See C<ENTER> and L<perlcall>.
70
71 =cut
72 */
73
74 #define SAVETMPS save_int((int*)&PL_tmps_floor), PL_tmps_floor = PL_tmps_ix
75 #define FREETMPS if (PL_tmps_ix > PL_tmps_floor) free_tmps()
76
77 #ifdef DEBUGGING
78 #define ENTER                                                   \
79     STMT_START {                                                \
80         push_scope();                                           \
81         DEBUG_l(WITH_THR(Perl_deb(aTHX_ "ENTER scope %ld at %s:%d\n",   \
82                     PL_scopestack_ix, __FILE__, __LINE__)));    \
83     } STMT_END
84 #define LEAVE                                                   \
85     STMT_START {                                                \
86         DEBUG_l(WITH_THR(Perl_deb(aTHX_ "LEAVE scope %ld at %s:%d\n",   \
87                     PL_scopestack_ix, __FILE__, __LINE__)));    \
88         pop_scope();                                            \
89     } STMT_END
90 #else
91 #define ENTER push_scope()
92 #define LEAVE pop_scope()
93 #endif
94 #define LEAVE_SCOPE(old) if (PL_savestack_ix > old) leave_scope(old)
95
96 /*
97  * Not using SOFT_CAST on SAVESPTR, SAVEGENERICSV and SAVEFREESV
98  * because these are used for several kinds of pointer values
99  */
100 #define SAVEI8(i)       save_I8(SOFT_CAST(I8*)&(i))
101 #define SAVEI16(i)      save_I16(SOFT_CAST(I16*)&(i))
102 #define SAVEI32(i)      save_I32(SOFT_CAST(I32*)&(i))
103 #define SAVEINT(i)      save_int(SOFT_CAST(int*)&(i))
104 #define SAVEIV(i)       save_iv(SOFT_CAST(IV*)&(i))
105 #define SAVELONG(l)     save_long(SOFT_CAST(long*)&(l))
106 #define SAVESPTR(s)     save_sptr((SV**)&(s))
107 #define SAVEPPTR(s)     save_pptr(SOFT_CAST(char**)&(s))
108 #define SAVEVPTR(s)     save_vptr((void*)&(s))
109 #define SAVEPADSV(s)    save_padsv(s)
110 #define SAVEFREESV(s)   save_freesv((SV*)(s))
111 #define SAVEFREEOP(o)   save_freeop(SOFT_CAST(OP*)(o))
112 #define SAVEFREEPV(p)   save_freepv(SOFT_CAST(char*)(p))
113 #define SAVECLEARSV(sv) save_clearsv(SOFT_CAST(SV**)&(sv))
114 #define SAVEGENERICSV(s)        save_generic_svref((SV**)&(s))
115 #define SAVEGENERICPV(s)        save_generic_pvref((char**)&(s))
116 #define SAVEDELETE(h,k,l) \
117           save_delete(SOFT_CAST(HV*)(h), SOFT_CAST(char*)(k), (I32)(l))
118 #define SAVEDESTRUCTOR(f,p) \
119           save_destructor((DESTRUCTORFUNC_NOCONTEXT_t)(f), SOFT_CAST(void*)(p))
120
121 #define SAVEDESTRUCTOR_X(f,p) \
122           save_destructor_x((DESTRUCTORFUNC_t)(f), SOFT_CAST(void*)(p))
123
124 #define SAVESTACK_POS() \
125     STMT_START {                                \
126         SSCHECK(2);                             \
127         SSPUSHINT(PL_stack_sp - PL_stack_base); \
128         SSPUSHINT(SAVEt_STACK_POS);             \
129     } STMT_END
130
131 #define SAVEOP()        save_op()
132
133 #define SAVEHINTS() \
134     STMT_START {                                \
135         if (PL_hints & HINT_LOCALIZE_HH)        \
136             save_hints();                       \
137         else {                                  \
138             SSCHECK(2);                         \
139             SSPUSHINT(PL_hints);                \
140             SSPUSHINT(SAVEt_HINTS);             \
141         }                                       \
142     } STMT_END
143
144 #define SAVECOMPPAD() \
145     STMT_START {                                                \
146         if (PL_comppad && PL_curpad == AvARRAY(PL_comppad)) {   \
147             SSCHECK(2);                                         \
148             SSPUSHPTR((SV*)PL_comppad);                         \
149             SSPUSHINT(SAVEt_COMPPAD);                           \
150         }                                                       \
151         else {                                                  \
152             SAVEVPTR(PL_curpad);                                \
153             SAVESPTR(PL_comppad);                               \
154         }                                                       \
155     } STMT_END
156
157 #ifdef USE_ITHREADS
158 #  define SAVECOPSTASH(c)       SAVEPPTR(CopSTASHPV(c))
159 #  define SAVECOPSTASH_FREE(c)  SAVEGENERICPV(CopSTASHPV(c))
160 #  define SAVECOPFILE(c)        SAVEPPTR(CopFILE(c))
161 #  define SAVECOPFILE_FREE(c)   SAVEGENERICPV(CopFILE(c))
162 #else
163 #  define SAVECOPSTASH(c)       SAVESPTR(CopSTASH(c))
164 #  define SAVECOPSTASH_FREE(c)  SAVECOPSTASH(c) /* XXX not refcounted */
165 #  define SAVECOPFILE(c)        SAVESPTR(CopFILEGV(c))
166 #  define SAVECOPFILE_FREE(c)   SAVEGENERICSV(CopFILEGV(c))
167 #endif
168
169 #define SAVECOPLINE(c)          SAVEI16(CopLINE(c))
170
171 /* SSNEW() temporarily allocates a specified number of bytes of data on the
172  * savestack.  It returns an integer index into the savestack, because a
173  * pointer would get broken if the savestack is moved on reallocation.
174  * SSNEWa() works like SSNEW(), but also aligns the data to the specified
175  * number of bytes.  MEM_ALIGNBYTES is perhaps the most useful.  The
176  * alignment will be preserved therough savestack reallocation *only* if
177  * realloc returns data aligned to a size divisible by `align'!
178  *
179  * SSPTR() converts the index returned by SSNEW/SSNEWa() into a pointer.
180  */
181
182 #define SSNEW(size)             Perl_save_alloc(aTHX_ (size), 0)
183 #define SSNEWt(n,t)             SSNEW((n)*sizeof(t))
184 #define SSNEWa(size,align)      Perl_save_alloc(aTHX_ (size), \
185     (align - ((int)((caddr_t)&PL_savestack[PL_savestack_ix]) % align)) % align)
186 #define SSNEWat(n,t,align)      SSNEWa((n)*sizeof(t), align)
187
188 #define SSPTR(off,type)         ((type)  ((char*)PL_savestack + off))
189 #define SSPTRt(off,type)        ((type*) ((char*)PL_savestack + off))
190
191 /* A jmpenv packages the state required to perform a proper non-local jump.
192  * Note that there is a start_env initialized when perl starts, and top_env
193  * points to this initially, so top_env should always be non-null.
194  *
195  * Existence of a non-null top_env->je_prev implies it is valid to call
196  * longjmp() at that runlevel (we make sure start_env.je_prev is always
197  * null to ensure this).
198  *
199  * je_mustcatch, when set at any runlevel to TRUE, means eval ops must
200  * establish a local jmpenv to handle exception traps.  Care must be taken
201  * to restore the previous value of je_mustcatch before exiting the
202  * stack frame iff JMPENV_PUSH was not called in that stack frame.
203  * GSAR 97-03-27
204  */
205
206 struct jmpenv {
207     struct jmpenv *     je_prev;
208     Sigjmp_buf          je_buf;         /* only for use if !je_throw */
209     int                 je_ret;         /* last exception thrown */
210     bool                je_mustcatch;   /* need to call longjmp()? */
211 #ifdef PERL_FLEXIBLE_EXCEPTIONS
212     void                (*je_throw)(int v); /* last for bincompat */
213     bool                je_noset;       /* no need for setjmp() */
214 #endif
215 };
216
217 typedef struct jmpenv JMPENV;
218
219 #ifdef OP_IN_REGISTER
220 #define OP_REG_TO_MEM   PL_opsave = op
221 #define OP_MEM_TO_REG   op = PL_opsave
222 #else
223 #define OP_REG_TO_MEM   NOOP
224 #define OP_MEM_TO_REG   NOOP
225 #endif
226
227 /*
228  * How to build the first jmpenv.
229  *
230  * top_env needs to be non-zero. It points to an area
231  * in which longjmp() stuff is stored, as C callstack
232  * info there at least is thread specific this has to
233  * be per-thread. Otherwise a 'die' in a thread gives
234  * that thread the C stack of last thread to do an eval {}!
235  */
236
237 #define JMPENV_BOOTSTRAP \
238     STMT_START {                                \
239         Zero(&PL_start_env, 1, JMPENV);         \
240         PL_start_env.je_ret = -1;               \
241         PL_start_env.je_mustcatch = TRUE;       \
242         PL_top_env = &PL_start_env;             \
243     } STMT_END
244
245 #ifdef PERL_FLEXIBLE_EXCEPTIONS
246
247 /*
248  * These exception-handling macros are split up to
249  * ease integration with C++ exceptions.
250  *
251  * To use C++ try+catch to catch Perl exceptions, an extension author
252  * needs to first write an extern "C" function to throw an appropriate
253  * exception object; typically it will be or contain an integer,
254  * because Perl's internals use integers to track exception types:
255  *    extern "C" { static void thrower(int i) { throw i; } }
256  *
257  * Then (as shown below) the author needs to use, not the simple
258  * JMPENV_PUSH, but several of its constitutent macros, to arrange for
259  * the Perl internals to call thrower() rather than longjmp() to
260  * report exceptions:
261  *
262  *    dJMPENV;
263  *    JMPENV_PUSH_INIT(thrower);
264  *    try {
265  *        ... stuff that may throw exceptions ...
266  *    }
267  *    catch (int why) {  // or whatever matches thrower()
268  *        JMPENV_POST_CATCH;
269  *        EXCEPT_SET(why);
270  *        switch (why) {
271  *          ... // handle various Perl exception codes
272  *        }
273  *    }
274  *    JMPENV_POP;  // don't forget this!
275  */
276
277 /*
278  * Function that catches/throws, and its callback for the
279  *  body of protected processing.
280  */
281 typedef void *(CPERLscope(*protect_body_t)) (pTHX_ va_list);
282 typedef void *(CPERLscope(*protect_proc_t)) (pTHX_ volatile JMPENV *pcur_env,
283                                              int *, protect_body_t, ...);
284
285 #define dJMPENV JMPENV cur_env; \
286                 volatile JMPENV *pcur_env = ((cur_env.je_noset = 0),&cur_env)
287
288 #define JMPENV_PUSH_INIT_ENV(ce,THROWFUNC) \
289     STMT_START {                                        \
290         (ce).je_throw = (THROWFUNC);                    \
291         (ce).je_ret = -1;                               \
292         (ce).je_mustcatch = FALSE;                      \
293         (ce).je_prev = PL_top_env;                      \
294         PL_top_env = &(ce);                             \
295         OP_REG_TO_MEM;                                  \
296     } STMT_END
297
298 #define JMPENV_PUSH_INIT(THROWFUNC) JMPENV_PUSH_INIT_ENV(*(JMPENV*)pcur_env,THROWFUNC)
299
300 #define JMPENV_POST_CATCH_ENV(ce) \
301     STMT_START {                                        \
302         OP_MEM_TO_REG;                                  \
303         PL_top_env = &(ce);                             \
304     } STMT_END
305
306 #define JMPENV_POST_CATCH JMPENV_POST_CATCH_ENV(*(JMPENV*)pcur_env)
307
308 #define JMPENV_PUSH_ENV(ce,v) \
309     STMT_START {                                                \
310         if (!(ce).je_noset) {                                   \
311             DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n", \
312                              ce, PL_top_env));                  \
313             JMPENV_PUSH_INIT_ENV(ce,NULL);                      \
314             EXCEPT_SET_ENV(ce,PerlProc_setjmp((ce).je_buf, SCOPE_SAVES_SIGNAL_MASK));\
315             (ce).je_noset = 1;                                  \
316         }                                                       \
317         else                                                    \
318             EXCEPT_SET_ENV(ce,0);                               \
319         JMPENV_POST_CATCH_ENV(ce);                              \
320         (v) = EXCEPT_GET_ENV(ce);                               \
321     } STMT_END
322
323 #define JMPENV_PUSH(v) JMPENV_PUSH_ENV(*(JMPENV*)pcur_env,v)
324
325 #define JMPENV_POP_ENV(ce) \
326     STMT_START {                                                \
327         if (PL_top_env == &(ce))                                \
328             PL_top_env = (ce).je_prev;                          \
329     } STMT_END
330
331 #define JMPENV_POP  JMPENV_POP_ENV(*(JMPENV*)pcur_env)
332
333 #define JMPENV_JUMP(v) \
334     STMT_START {                                                \
335         OP_REG_TO_MEM;                                          \
336         if (PL_top_env->je_prev) {                              \
337             if (PL_top_env->je_throw)                           \
338                 PL_top_env->je_throw(v);                        \
339             else                                                \
340                 PerlProc_longjmp(PL_top_env->je_buf, (v));      \
341         }                                                       \
342         if ((v) == 2)                                           \
343             PerlProc_exit(STATUS_NATIVE_EXPORT);                \
344         PerlIO_printf(Perl_error_log, "panic: top_env\n");      \
345         PerlProc_exit(1);                                       \
346     } STMT_END
347
348 #define EXCEPT_GET_ENV(ce)      ((ce).je_ret)
349 #define EXCEPT_GET              EXCEPT_GET_ENV(*(JMPENV*)pcur_env)
350 #define EXCEPT_SET_ENV(ce,v)    ((ce).je_ret = (v))
351 #define EXCEPT_SET(v)           EXCEPT_SET_ENV(*(JMPENV*)pcur_env,v)
352
353 #else /* !PERL_FLEXIBLE_EXCEPTIONS */
354
355 #define dJMPENV         JMPENV cur_env
356
357 #define JMPENV_PUSH(v) \
358     STMT_START {                                                        \
359         DEBUG_l(Perl_deb(aTHX_ "Setting up jumplevel %p, was %p\n",     \
360                          &cur_env, PL_top_env));                        \
361         cur_env.je_prev = PL_top_env;                                   \
362         OP_REG_TO_MEM;                                                  \
363         cur_env.je_ret = PerlProc_setjmp(cur_env.je_buf, SCOPE_SAVES_SIGNAL_MASK);              \
364         OP_MEM_TO_REG;                                                  \
365         PL_top_env = &cur_env;                                          \
366         cur_env.je_mustcatch = FALSE;                                   \
367         (v) = cur_env.je_ret;                                           \
368     } STMT_END
369
370 #define JMPENV_POP \
371     STMT_START { PL_top_env = cur_env.je_prev; } STMT_END
372
373 #define JMPENV_JUMP(v) \
374     STMT_START {                                                \
375         OP_REG_TO_MEM;                                          \
376         if (PL_top_env->je_prev)                                \
377             PerlProc_longjmp(PL_top_env->je_buf, (v));          \
378         if ((v) == 2)                                           \
379             PerlProc_exit(STATUS_NATIVE_EXPORT);                \
380         PerlIO_printf(PerlIO_stderr(), "panic: top_env\n");     \
381         PerlProc_exit(1);                                       \
382     } STMT_END
383
384 #endif /* PERL_FLEXIBLE_EXCEPTIONS */
385
386 #define CATCH_GET               (PL_top_env->je_mustcatch)
387 #define CATCH_SET(v)            (PL_top_env->je_mustcatch = (v))