This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
avoid stash pointers in optree under USE_ITHREADS
[perl5.git] / op.c
CommitLineData
a0d0e21e 1/* op.c
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 *
a0d0e21e
LW
8 */
9
10/*
11 * "You see: Mr. Drogo, he married poor Miss Primula Brandybuck. She was
12 * our Mr. Bilbo's first cousin on the mother's side (her mother being the
13 * youngest of the Old Took's daughters); and Mr. Drogo was his second
14 * cousin. So Mr. Frodo is his first *and* second cousin, once removed
15 * either way, as the saying is, if you follow me." --the Gaffer
79072805
LW
16 */
17
18#include "EXTERN.h"
864dbfa3 19#define PERL_IN_OP_C
79072805 20#include "perl.h"
77ca0c92 21#include "keywords.h"
79072805 22
b7dc083c 23/* #define PL_OP_SLAB_ALLOC */
7934575e
GS
24
25/* XXXXXX testing */
26#define OP_REFCNT_LOCK NOOP
27#define OP_REFCNT_UNLOCK NOOP
28#define OpREFCNT_set(o,n) NOOP
29#define OpREFCNT_dec(o) 0
30
b7dc083c
NIS
31#ifdef PL_OP_SLAB_ALLOC
32#define SLAB_SIZE 8192
33static char *PL_OpPtr = NULL;
34static int PL_OpSpace = 0;
35#define NewOp(m,var,c,type) do { if ((PL_OpSpace -= c*sizeof(type)) >= 0) \
36 var = (type *)(PL_OpPtr -= c*sizeof(type)); \
37 else \
38 var = (type *) Slab_Alloc(m,c*sizeof(type)); \
39 } while (0)
40
864dbfa3 41STATIC void *
cea2e8a9 42S_Slab_Alloc(pTHX_ int m, size_t sz)
b7dc083c
NIS
43{
44 Newz(m,PL_OpPtr,SLAB_SIZE,char);
45 PL_OpSpace = SLAB_SIZE - sz;
46 return PL_OpPtr += PL_OpSpace;
47}
76e3520e 48
b7dc083c
NIS
49#else
50#define NewOp(m, var, c, type) Newz(m, var, c, type)
51#endif
e50aee73 52/*
5dc0d613 53 * In the following definition, the ", Nullop" is just to make the compiler
a5f75d66 54 * think the expression is of the right type: croak actually does a Siglongjmp.
e50aee73 55 */
11343788 56#define CHECKOP(type,o) \
3280af22 57 ((PL_op_mask && PL_op_mask[type]) \
5dc0d613 58 ? ( op_free((OP*)o), \
cea2e8a9 59 Perl_croak(aTHX_ "%s trapped by operation mask", PL_op_desc[type]), \
28757baa 60 Nullop ) \
fc0dc3b3 61 : CALL_FPTR(PL_check[type])(aTHX_ (OP*)o))
e50aee73 62
c53d7c7d
HS
63#define PAD_MAX 999999999
64
76e3520e 65STATIC char*
cea2e8a9 66S_gv_ename(pTHX_ GV *gv)
4633a7c4 67{
2d8e6c8d 68 STRLEN n_a;
4633a7c4 69 SV* tmpsv = sv_newmortal();
46fc3d4c 70 gv_efullname3(tmpsv, gv, Nullch);
2d8e6c8d 71 return SvPV(tmpsv,n_a);
4633a7c4
LW
72}
73
76e3520e 74STATIC OP *
cea2e8a9 75S_no_fh_allowed(pTHX_ OP *o)
79072805 76{
cea2e8a9 77 yyerror(Perl_form(aTHX_ "Missing comma after first argument to %s function",
22c35a8c 78 PL_op_desc[o->op_type]));
11343788 79 return o;
79072805
LW
80}
81
76e3520e 82STATIC OP *
cea2e8a9 83S_too_few_arguments(pTHX_ OP *o, char *name)
79072805 84{
cea2e8a9 85 yyerror(Perl_form(aTHX_ "Not enough arguments for %s", name));
11343788 86 return o;
79072805
LW
87}
88
76e3520e 89STATIC OP *
cea2e8a9 90S_too_many_arguments(pTHX_ OP *o, char *name)
79072805 91{
cea2e8a9 92 yyerror(Perl_form(aTHX_ "Too many arguments for %s", name));
11343788 93 return o;
79072805
LW
94}
95
76e3520e 96STATIC void
cea2e8a9 97S_bad_type(pTHX_ I32 n, char *t, char *name, OP *kid)
8990e307 98{
cea2e8a9 99 yyerror(Perl_form(aTHX_ "Type of arg %d to %s must be %s (not %s)",
22c35a8c 100 (int)n, name, t, PL_op_desc[kid->op_type]));
8990e307
LW
101}
102
7a52d87a 103STATIC void
cea2e8a9 104S_no_bareword_allowed(pTHX_ OP *o)
7a52d87a 105{
5a844595
GS
106 qerror(Perl_mess(aTHX_
107 "Bareword \"%s\" not allowed while \"strict subs\" in use",
108 SvPV_nolen(cSVOPo->op_sv)));
7a52d87a
GS
109}
110
79072805
LW
111/* "register" allocation */
112
113PADOFFSET
864dbfa3 114Perl_pad_allocmy(pTHX_ char *name)
93a17b20 115{
11343788 116 dTHR;
a0d0e21e
LW
117 PADOFFSET off;
118 SV *sv;
119
834a4ddd 120 if (!(
77ca0c92 121 PL_in_my == KEY_our ||
834a4ddd
LW
122 isALPHA(name[1]) ||
123 (PL_hints & HINT_UTF8 && (name[1] & 0xc0) == 0xc0) ||
77ca0c92 124 name[1] == '_' && (int)strlen(name) > 2 ))
834a4ddd 125 {
c4d0567e 126 if (!isPRINT(name[1]) || strchr("\t\n\r\f", name[1])) {
2b92dfce
GS
127 /* 1999-02-27 mjd@plover.com */
128 char *p;
129 p = strchr(name, '\0');
130 /* The next block assumes the buffer is at least 205 chars
131 long. At present, it's always at least 256 chars. */
132 if (p-name > 200) {
133 strcpy(name+200, "...");
134 p = name+199;
135 }
136 else {
137 p[1] = '\0';
138 }
139 /* Move everything else down one character */
140 for (; p-name > 2; p--)
141 *p = *(p-1);
46fc3d4c 142 name[2] = toCTRL(name[1]);
143 name[1] = '^';
144 }
cea2e8a9 145 yyerror(Perl_form(aTHX_ "Can't use global %s in \"my\"",name));
a0d0e21e 146 }
599cee73 147 if (ckWARN(WARN_UNSAFE) && AvFILLp(PL_comppad_name) >= 0) {
3280af22
NIS
148 SV **svp = AvARRAY(PL_comppad_name);
149 for (off = AvFILLp(PL_comppad_name); off > PL_comppad_name_floor; off--) {
b1cb66bf 150 if ((sv = svp[off])
3280af22 151 && sv != &PL_sv_undef
c53d7c7d 152 && (SvIVX(sv) == PAD_MAX || SvIVX(sv) == 0)
b1cb66bf 153 && strEQ(name, SvPVX(sv)))
154 {
cea2e8a9 155 Perl_warner(aTHX_ WARN_UNSAFE,
77ca0c92
LW
156 "\"%s\" variable %s masks earlier declaration in same %s",
157 (PL_in_my == KEY_our ? "our" : "my"),
158 name,
159 (SvIVX(sv) == PAD_MAX ? "scope" : "statement"));
b1cb66bf 160 break;
161 }
162 }
163 }
a0d0e21e
LW
164 off = pad_alloc(OP_PADSV, SVs_PADMY);
165 sv = NEWSV(1102,0);
93a17b20
LW
166 sv_upgrade(sv, SVt_PVNV);
167 sv_setpv(sv, name);
3280af22 168 if (PL_in_my_stash) {
c750a3ec 169 if (*name != '$')
cea2e8a9 170 yyerror(Perl_form(aTHX_ "Can't declare class for non-scalar %s in \"my\"",
dce40276 171 name));
c750a3ec
MB
172 SvOBJECT_on(sv);
173 (void)SvUPGRADE(sv, SVt_PVMG);
3280af22
NIS
174 SvSTASH(sv) = (HV*)SvREFCNT_inc(PL_in_my_stash);
175 PL_sv_objcount++;
c750a3ec 176 }
77ca0c92
LW
177 if (PL_in_my == KEY_our)
178 SvFLAGS(sv) |= SVpad_OUR;
3280af22 179 av_store(PL_comppad_name, off, sv);
65202027 180 SvNVX(sv) = (NV)PAD_MAX;
8990e307 181 SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */
3280af22
NIS
182 if (!PL_min_intro_pending)
183 PL_min_intro_pending = off;
184 PL_max_intro_pending = off;
93a17b20 185 if (*name == '@')
3280af22 186 av_store(PL_comppad, off, (SV*)newAV());
93a17b20 187 else if (*name == '%')
3280af22
NIS
188 av_store(PL_comppad, off, (SV*)newHV());
189 SvPADMY_on(PL_curpad[off]);
93a17b20
LW
190 return off;
191}
192
2680586e
GS
193#define FINDLEX_NOSEARCH 1 /* don't search outer contexts */
194
76e3520e 195STATIC PADOFFSET
cea2e8a9 196S_pad_findlex(pTHX_ char *name, PADOFFSET newoff, U32 seq, CV* startcv,
864dbfa3 197 I32 cx_ix, I32 saweval, U32 flags)
93a17b20 198{
11343788 199 dTHR;
748a9306 200 CV *cv;
93a17b20
LW
201 I32 off;
202 SV *sv;
93a17b20 203 register I32 i;
c09156bb 204 register PERL_CONTEXT *cx;
93a17b20 205
748a9306 206 for (cv = startcv; cv; cv = CvOUTSIDE(cv)) {
4fdae800 207 AV *curlist = CvPADLIST(cv);
208 SV **svp = av_fetch(curlist, 0, FALSE);
748a9306 209 AV *curname;
4fdae800 210
3280af22 211 if (!svp || *svp == &PL_sv_undef)
4633a7c4 212 continue;
748a9306
LW
213 curname = (AV*)*svp;
214 svp = AvARRAY(curname);
93965878 215 for (off = AvFILLp(curname); off > 0; off--) {
748a9306 216 if ((sv = svp[off]) &&
3280af22 217 sv != &PL_sv_undef &&
748a9306 218 seq <= SvIVX(sv) &&
13826f2c 219 seq > I_32(SvNVX(sv)) &&
748a9306
LW
220 strEQ(SvPVX(sv), name))
221 {
5f05dabc 222 I32 depth;
223 AV *oldpad;
224 SV *oldsv;
225
226 depth = CvDEPTH(cv);
227 if (!depth) {
9607fc9c 228 if (newoff) {
229 if (SvFAKE(sv))
230 continue;
4fdae800 231 return 0; /* don't clone from inactive stack frame */
9607fc9c 232 }
5f05dabc 233 depth = 1;
234 }
235 oldpad = (AV*)*av_fetch(curlist, depth, FALSE);
236 oldsv = *av_fetch(oldpad, off, TRUE);
748a9306 237 if (!newoff) { /* Not a mere clone operation. */
9607fc9c 238 SV *namesv = NEWSV(1103,0);
748a9306 239 newoff = pad_alloc(OP_PADSV, SVs_PADMY);
9607fc9c 240 sv_upgrade(namesv, SVt_PVNV);
241 sv_setpv(namesv, name);
3280af22 242 av_store(PL_comppad_name, newoff, namesv);
65202027 243 SvNVX(namesv) = (NV)PL_curcop->cop_seq;
c53d7c7d 244 SvIVX(namesv) = PAD_MAX; /* A ref, intro immediately */
9607fc9c 245 SvFAKE_on(namesv); /* A ref, not a real var */
77ca0c92
LW
246 if (SvFLAGS(sv) & SVpad_OUR)/* An "our" variable */
247 SvFLAGS(namesv) |= SVpad_OUR;
87671ffc 248 if (SvOBJECT(sv)) { /* A typed var */
3d93dc8b
GS
249 SvOBJECT_on(namesv);
250 (void)SvUPGRADE(namesv, SVt_PVMG);
87671ffc 251 SvSTASH(namesv) = (HV*)SvREFCNT_inc((SV*)SvSTASH(sv));
3d93dc8b
GS
252 PL_sv_objcount++;
253 }
3280af22 254 if (CvANON(PL_compcv) || SvTYPE(PL_compcv) == SVt_PVFM) {
28757baa 255 /* "It's closures all the way down." */
3280af22 256 CvCLONE_on(PL_compcv);
54310121 257 if (cv == startcv) {
3280af22 258 if (CvANON(PL_compcv))
54310121 259 oldsv = Nullsv; /* no need to keep ref */
260 }
261 else {
28757baa 262 CV *bcv;
263 for (bcv = startcv;
264 bcv && bcv != cv && !CvCLONE(bcv);
6b35e009
GS
265 bcv = CvOUTSIDE(bcv))
266 {
28757baa 267 if (CvANON(bcv))
268 CvCLONE_on(bcv);
269 else {
6b35e009
GS
270 if (ckWARN(WARN_CLOSURE)
271 && !CvUNIQUE(bcv) && !CvUNIQUE(cv))
272 {
cea2e8a9 273 Perl_warner(aTHX_ WARN_CLOSURE,
44a8e56a 274 "Variable \"%s\" may be unavailable",
28757baa 275 name);
6b35e009 276 }
28757baa 277 break;
278 }
279 }
280 }
281 }
3280af22 282 else if (!CvUNIQUE(PL_compcv)) {
599cee73 283 if (ckWARN(WARN_CLOSURE) && !SvFAKE(sv) && !CvUNIQUE(cv))
cea2e8a9 284 Perl_warner(aTHX_ WARN_CLOSURE,
599cee73 285 "Variable \"%s\" will not stay shared", name);
5f05dabc 286 }
748a9306 287 }
3280af22 288 av_store(PL_comppad, newoff, SvREFCNT_inc(oldsv));
748a9306
LW
289 return newoff;
290 }
93a17b20
LW
291 }
292 }
293
2680586e
GS
294 if (flags & FINDLEX_NOSEARCH)
295 return 0;
296
93a17b20
LW
297 /* Nothing in current lexical context--try eval's context, if any.
298 * This is necessary to let the perldb get at lexically scoped variables.
299 * XXX This will also probably interact badly with eval tree caching.
300 */
301
748a9306 302 for (i = cx_ix; i >= 0; i--) {
93a17b20 303 cx = &cxstack[i];
6b35e009 304 switch (CxTYPE(cx)) {
93a17b20 305 default:
748a9306
LW
306 if (i == 0 && saweval) {
307 seq = cxstack[saweval].blk_oldcop->cop_seq;
2680586e 308 return pad_findlex(name, newoff, seq, PL_main_cv, -1, saweval, 0);
748a9306 309 }
93a17b20
LW
310 break;
311 case CXt_EVAL:
44a8e56a 312 switch (cx->blk_eval.old_op_type) {
313 case OP_ENTEREVAL:
6b35e009
GS
314 if (CxREALEVAL(cx))
315 saweval = i;
44a8e56a 316 break;
317 case OP_REQUIRE:
318 /* require must have its own scope */
319 return 0;
320 }
93a17b20
LW
321 break;
322 case CXt_SUB:
323 if (!saweval)
324 return 0;
325 cv = cx->blk_sub.cv;
3280af22 326 if (PL_debstash && CvSTASH(cv) == PL_debstash) { /* ignore DB'* scope */
748a9306 327 saweval = i; /* so we know where we were called from */
93a17b20 328 continue;
93a17b20 329 }
748a9306 330 seq = cxstack[saweval].blk_oldcop->cop_seq;
2680586e 331 return pad_findlex(name, newoff, seq, cv, i-1, saweval,FINDLEX_NOSEARCH);
93a17b20
LW
332 }
333 }
334
748a9306
LW
335 return 0;
336}
a0d0e21e 337
748a9306 338PADOFFSET
864dbfa3 339Perl_pad_findmy(pTHX_ char *name)
748a9306 340{
11343788 341 dTHR;
748a9306 342 I32 off;
54310121 343 I32 pendoff = 0;
748a9306 344 SV *sv;
3280af22
NIS
345 SV **svp = AvARRAY(PL_comppad_name);
346 U32 seq = PL_cop_seqmax;
6b35e009 347 PERL_CONTEXT *cx;
33b8ce05 348 CV *outside;
748a9306 349
11343788
MB
350#ifdef USE_THREADS
351 /*
352 * Special case to get lexical (and hence per-thread) @_.
353 * XXX I need to find out how to tell at parse-time whether use
354 * of @_ should refer to a lexical (from a sub) or defgv (global
355 * scope and maybe weird sub-ish things like formats). See
356 * startsub in perly.y. It's possible that @_ could be lexical
357 * (at least from subs) even in non-threaded perl.
358 */
359 if (strEQ(name, "@_"))
360 return 0; /* success. (NOT_IN_PAD indicates failure) */
361#endif /* USE_THREADS */
362
748a9306 363 /* The one we're looking for is probably just before comppad_name_fill. */
3280af22 364 for (off = AvFILLp(PL_comppad_name); off > 0; off--) {
a0d0e21e 365 if ((sv = svp[off]) &&
3280af22 366 sv != &PL_sv_undef &&
54310121 367 (!SvIVX(sv) ||
368 (seq <= SvIVX(sv) &&
369 seq > I_32(SvNVX(sv)))) &&
a0d0e21e
LW
370 strEQ(SvPVX(sv), name))
371 {
77ca0c92 372 if (SvIVX(sv) || SvFLAGS(sv) & SVpad_OUR)
54310121 373 return (PADOFFSET)off;
374 pendoff = off; /* this pending def. will override import */
a0d0e21e
LW
375 }
376 }
748a9306 377
33b8ce05
GS
378 outside = CvOUTSIDE(PL_compcv);
379
380 /* Check if if we're compiling an eval'', and adjust seq to be the
381 * eval's seq number. This depends on eval'' having a non-null
382 * CvOUTSIDE() while it is being compiled. The eval'' itself is
1aff0e91
GS
383 * identified by CvEVAL being true and CvGV being null. */
384 if (outside && CvEVAL(PL_compcv) && !CvGV(PL_compcv) && cxstack_ix >= 0) {
6b35e009
GS
385 cx = &cxstack[cxstack_ix];
386 if (CxREALEVAL(cx))
387 seq = cx->blk_oldcop->cop_seq;
388 }
389
748a9306 390 /* See if it's in a nested scope */
2680586e 391 off = pad_findlex(name, 0, seq, outside, cxstack_ix, 0, 0);
54310121 392 if (off) {
393 /* If there is a pending local definition, this new alias must die */
394 if (pendoff)
3280af22 395 SvIVX(AvARRAY(PL_comppad_name)[off]) = seq;
11343788 396 return off; /* pad_findlex returns 0 for failure...*/
54310121 397 }
11343788 398 return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */
93a17b20
LW
399}
400
401void
864dbfa3 402Perl_pad_leavemy(pTHX_ I32 fill)
93a17b20 403{
0453d815 404 dTHR;
93a17b20 405 I32 off;
3280af22 406 SV **svp = AvARRAY(PL_comppad_name);
93a17b20 407 SV *sv;
3280af22
NIS
408 if (PL_min_intro_pending && fill < PL_min_intro_pending) {
409 for (off = PL_max_intro_pending; off >= PL_min_intro_pending; off--) {
0453d815
PM
410 if ((sv = svp[off]) && sv != &PL_sv_undef && ckWARN_d(WARN_INTERNAL))
411 Perl_warner(aTHX_ WARN_INTERNAL, "%s never introduced", SvPVX(sv));
8990e307
LW
412 }
413 }
414 /* "Deintroduce" my variables that are leaving with this scope. */
3280af22 415 for (off = AvFILLp(PL_comppad_name); off > fill; off--) {
c53d7c7d 416 if ((sv = svp[off]) && sv != &PL_sv_undef && SvIVX(sv) == PAD_MAX)
3280af22 417 SvIVX(sv) = PL_cop_seqmax;
93a17b20
LW
418 }
419}
420
421PADOFFSET
864dbfa3 422Perl_pad_alloc(pTHX_ I32 optype, U32 tmptype)
79072805 423{
11343788 424 dTHR;
79072805
LW
425 SV *sv;
426 I32 retval;
427
3280af22 428 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 429 Perl_croak(aTHX_ "panic: pad_alloc");
3280af22 430 if (PL_pad_reset_pending)
a0d0e21e 431 pad_reset();
ed6116ce 432 if (tmptype & SVs_PADMY) {
79072805 433 do {
3280af22 434 sv = *av_fetch(PL_comppad, AvFILLp(PL_comppad) + 1, TRUE);
ed6116ce 435 } while (SvPADBUSY(sv)); /* need a fresh one */
3280af22 436 retval = AvFILLp(PL_comppad);
79072805
LW
437 }
438 else {
3280af22
NIS
439 SV **names = AvARRAY(PL_comppad_name);
440 SSize_t names_fill = AvFILLp(PL_comppad_name);
bbce6d69 441 for (;;) {
442 /*
443 * "foreach" index vars temporarily become aliases to non-"my"
444 * values. Thus we must skip, not just pad values that are
445 * marked as current pad values, but also those with names.
446 */
3280af22
NIS
447 if (++PL_padix <= names_fill &&
448 (sv = names[PL_padix]) && sv != &PL_sv_undef)
bbce6d69 449 continue;
3280af22 450 sv = *av_fetch(PL_comppad, PL_padix, TRUE);
743e66e6 451 if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY)) && !IS_PADGV(sv))
bbce6d69 452 break;
453 }
3280af22 454 retval = PL_padix;
79072805 455 }
8990e307 456 SvFLAGS(sv) |= tmptype;
3280af22 457 PL_curpad = AvARRAY(PL_comppad);
11343788 458#ifdef USE_THREADS
b900a521
JH
459 DEBUG_X(PerlIO_printf(Perl_debug_log,
460 "0x%"UVxf" Pad 0x%"UVxf" alloc %ld for %s\n",
461 PTR2UV(thr), PTR2UV(PL_curpad),
22c35a8c 462 (long) retval, PL_op_name[optype]));
11343788 463#else
b900a521
JH
464 DEBUG_X(PerlIO_printf(Perl_debug_log,
465 "Pad 0x%"UVxf" alloc %ld for %s\n",
466 PTR2UV(PL_curpad),
22c35a8c 467 (long) retval, PL_op_name[optype]));
11343788 468#endif /* USE_THREADS */
79072805
LW
469 return (PADOFFSET)retval;
470}
471
472SV *
864dbfa3 473Perl_pad_sv(pTHX_ PADOFFSET po)
79072805 474{
11343788
MB
475 dTHR;
476#ifdef USE_THREADS
b900a521
JH
477 DEBUG_X(PerlIO_printf(Perl_debug_log,
478 "0x%"UVxf" Pad 0x%"UVxf" sv %d\n",
479 PTR2UV(thr), PTR2UV(PL_curpad), po));
11343788 480#else
79072805 481 if (!po)
cea2e8a9 482 Perl_croak(aTHX_ "panic: pad_sv po");
b900a521
JH
483 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" sv %d\n",
484 PTR2UV(PL_curpad), po));
11343788 485#endif /* USE_THREADS */
3280af22 486 return PL_curpad[po]; /* eventually we'll turn this into a macro */
79072805
LW
487}
488
489void
864dbfa3 490Perl_pad_free(pTHX_ PADOFFSET po)
79072805 491{
11343788 492 dTHR;
3280af22 493 if (!PL_curpad)
a0d0e21e 494 return;
3280af22 495 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 496 Perl_croak(aTHX_ "panic: pad_free curpad");
79072805 497 if (!po)
cea2e8a9 498 Perl_croak(aTHX_ "panic: pad_free po");
11343788 499#ifdef USE_THREADS
b900a521
JH
500 DEBUG_X(PerlIO_printf(Perl_debug_log,
501 "0x%"UVxf" Pad 0x%"UVxf" free %d\n",
502 PTR2UV(thr), PTR2UV(PL_curpad), po));
11343788 503#else
b900a521
JH
504 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" free %d\n",
505 PTR2UV(PL_curpad), po));
11343788 506#endif /* USE_THREADS */
3280af22
NIS
507 if (PL_curpad[po] && PL_curpad[po] != &PL_sv_undef)
508 SvPADTMP_off(PL_curpad[po]);
509 if ((I32)po < PL_padix)
510 PL_padix = po - 1;
79072805
LW
511}
512
513void
864dbfa3 514Perl_pad_swipe(pTHX_ PADOFFSET po)
79072805 515{
11343788 516 dTHR;
3280af22 517 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 518 Perl_croak(aTHX_ "panic: pad_swipe curpad");
79072805 519 if (!po)
cea2e8a9 520 Perl_croak(aTHX_ "panic: pad_swipe po");
11343788 521#ifdef USE_THREADS
b900a521
JH
522 DEBUG_X(PerlIO_printf(Perl_debug_log,
523 "0x%"UVxf" Pad 0x%"UVxf" swipe %d\n",
524 PTR2UV(thr), PTR2UV(PL_curpad), po));
11343788 525#else
b900a521
JH
526 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" swipe %d\n",
527 PTR2UV(PL_curpad), po));
11343788 528#endif /* USE_THREADS */
3280af22
NIS
529 SvPADTMP_off(PL_curpad[po]);
530 PL_curpad[po] = NEWSV(1107,0);
531 SvPADTMP_on(PL_curpad[po]);
532 if ((I32)po < PL_padix)
533 PL_padix = po - 1;
79072805
LW
534}
535
d9bb4600
GS
536/* XXX pad_reset() is currently disabled because it results in serious bugs.
537 * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed
538 * on the stack by OPs that use them, there are several ways to get an alias
539 * to a shared TARG. Such an alias will change randomly and unpredictably.
540 * We avoid doing this until we can think of a Better Way.
541 * GSAR 97-10-29 */
79072805 542void
864dbfa3 543Perl_pad_reset(pTHX)
79072805 544{
d9bb4600 545#ifdef USE_BROKEN_PAD_RESET
11343788 546 dTHR;
79072805
LW
547 register I32 po;
548
6b88bc9c 549 if (AvARRAY(PL_comppad) != PL_curpad)
cea2e8a9 550 Perl_croak(aTHX_ "panic: pad_reset curpad");
11343788 551#ifdef USE_THREADS
b900a521
JH
552 DEBUG_X(PerlIO_printf(Perl_debug_log,
553 "0x%"UVxf" Pad 0x%"UVxf" reset\n",
554 PTR2UV(thr), PTR2UV(PL_curpad)));
11343788 555#else
b900a521
JH
556 DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%"UVxf" reset\n",
557 PTR2UV(PL_curpad)));
11343788 558#endif /* USE_THREADS */
6b88bc9c
GS
559 if (!PL_tainting) { /* Can't mix tainted and non-tainted temporaries. */
560 for (po = AvMAX(PL_comppad); po > PL_padix_floor; po--) {
561 if (PL_curpad[po] && !SvIMMORTAL(PL_curpad[po]))
562 SvPADTMP_off(PL_curpad[po]);
748a9306 563 }
6b88bc9c 564 PL_padix = PL_padix_floor;
79072805 565 }
d9bb4600 566#endif
3280af22 567 PL_pad_reset_pending = FALSE;
79072805
LW
568}
569
a863c7d1 570#ifdef USE_THREADS
54b9620d 571/* find_threadsv is not reentrant */
a863c7d1 572PADOFFSET
864dbfa3 573Perl_find_threadsv(pTHX_ const char *name)
a863c7d1
MB
574{
575 dTHR;
576 char *p;
577 PADOFFSET key;
554b3eca 578 SV **svp;
54b9620d 579 /* We currently only handle names of a single character */
533c011a 580 p = strchr(PL_threadsv_names, *name);
a863c7d1
MB
581 if (!p)
582 return NOT_IN_PAD;
533c011a 583 key = p - PL_threadsv_names;
2d8e6c8d 584 MUTEX_LOCK(&thr->mutex);
54b9620d 585 svp = av_fetch(thr->threadsv, key, FALSE);
2d8e6c8d
GS
586 if (svp)
587 MUTEX_UNLOCK(&thr->mutex);
588 else {
554b3eca 589 SV *sv = NEWSV(0, 0);
54b9620d 590 av_store(thr->threadsv, key, sv);
940cb80d 591 thr->threadsvp = AvARRAY(thr->threadsv);
2d8e6c8d 592 MUTEX_UNLOCK(&thr->mutex);
554b3eca
MB
593 /*
594 * Some magic variables used to be automagically initialised
595 * in gv_fetchpv. Those which are now per-thread magicals get
596 * initialised here instead.
597 */
598 switch (*name) {
54b9620d
MB
599 case '_':
600 break;
554b3eca
MB
601 case ';':
602 sv_setpv(sv, "\034");
54b9620d 603 sv_magic(sv, 0, 0, name, 1);
554b3eca 604 break;
c277df42
IZ
605 case '&':
606 case '`':
607 case '\'':
533c011a 608 PL_sawampersand = TRUE;
a3f914c5
GS
609 /* FALL THROUGH */
610 case '1':
611 case '2':
612 case '3':
613 case '4':
614 case '5':
615 case '6':
616 case '7':
617 case '8':
618 case '9':
c277df42 619 SvREADONLY_on(sv);
d8b5173a 620 /* FALL THROUGH */
067391ea
GS
621
622 /* XXX %! tied to Errno.pm needs to be added here.
623 * See gv_fetchpv(). */
624 /* case '!': */
625
54b9620d
MB
626 default:
627 sv_magic(sv, 0, 0, name, 1);
554b3eca 628 }
bf49b057 629 DEBUG_S(PerlIO_printf(Perl_error_log,
54b9620d 630 "find_threadsv: new SV %p for $%s%c\n",
554b3eca
MB
631 sv, (*name < 32) ? "^" : "",
632 (*name < 32) ? toCTRL(*name) : *name));
a863c7d1
MB
633 }
634 return key;
635}
636#endif /* USE_THREADS */
637
79072805
LW
638/* Destructor */
639
640void
864dbfa3 641Perl_op_free(pTHX_ OP *o)
79072805 642{
85e6fe83 643 register OP *kid, *nextkid;
acb36ea4 644 OPCODE type;
79072805 645
5dc0d613 646 if (!o || o->op_seq == (U16)-1)
79072805
LW
647 return;
648
7934575e
GS
649 if (o->op_private & OPpREFCOUNTED) {
650 switch (o->op_type) {
651 case OP_LEAVESUB:
652 case OP_LEAVESUBLV:
653 case OP_LEAVEEVAL:
654 case OP_LEAVE:
655 case OP_SCOPE:
656 case OP_LEAVEWRITE:
657 OP_REFCNT_LOCK;
658 if (OpREFCNT_dec(o)) {
659 OP_REFCNT_UNLOCK;
660 return;
661 }
662 OP_REFCNT_UNLOCK;
663 break;
664 default:
665 break;
666 }
667 }
668
11343788
MB
669 if (o->op_flags & OPf_KIDS) {
670 for (kid = cUNOPo->op_first; kid; kid = nextkid) {
85e6fe83 671 nextkid = kid->op_sibling; /* Get before next freeing kid */
79072805 672 op_free(kid);
85e6fe83 673 }
79072805 674 }
acb36ea4
GS
675 type = o->op_type;
676 if (type == OP_NULL)
677 type = o->op_targ;
678
679 /* COP* is not cleared by op_clear() so that we may track line
680 * numbers etc even after null() */
681 if (type == OP_NEXTSTATE || type == OP_SETSTATE || type == OP_DBSTATE)
682 cop_free((COP*)o);
683
684 op_clear(o);
685
686#ifdef PL_OP_SLAB_ALLOC
687 if ((char *) o == PL_OpPtr)
688 {
689 }
690#else
691 Safefree(o);
692#endif
693}
79072805 694
acb36ea4
GS
695STATIC void
696S_op_clear(pTHX_ OP *o)
697{
11343788 698 switch (o->op_type) {
acb36ea4
GS
699 case OP_NULL: /* Was holding old type, if any. */
700 case OP_ENTEREVAL: /* Was holding hints. */
701#ifdef USE_THREADS
702 case OP_THREADSV: /* Was holding index into thr->threadsv AV. */
703#endif
704 o->op_targ = 0;
a0d0e21e 705 break;
554b3eca 706#ifdef USE_THREADS
8dd3ba40
SM
707 case OP_ENTERITER:
708 if (!(o->op_flags & OPf_SPECIAL))
709 break;
710 /* FALL THROUGH */
554b3eca 711#endif /* USE_THREADS */
a6006777 712 default:
ac4c12e7 713 if (!(o->op_flags & OPf_REF)
0b94c7bb 714 || (PL_check[o->op_type] != MEMBER_TO_FPTR(Perl_ck_ftst)))
a6006777 715 break;
716 /* FALL THROUGH */
463ee0b2 717 case OP_GVSV:
79072805 718 case OP_GV:
a6006777 719 case OP_AELEMFAST:
350de78d 720#ifdef USE_ITHREADS
7f28abaa 721 if (PL_curpad) {
743e66e6
GS
722 GV *gv = cGVOPo;
723 pad_swipe(cPADOPo->op_padix);
724 /* No GvIN_PAD_off(gv) here, because other references may still
725 * exist on the pad */
726 SvREFCNT_dec(gv);
7f28abaa 727 }
350de78d
GS
728 cPADOPo->op_padix = 0;
729#else
7f28abaa 730 SvREFCNT_dec(cGVOPo);
7934575e 731 cSVOPo->op_sv = Nullsv;
350de78d 732#endif
79072805
LW
733 break;
734 case OP_CONST:
11343788 735 SvREFCNT_dec(cSVOPo->op_sv);
acb36ea4 736 cSVOPo->op_sv = Nullsv;
79072805 737 break;
748a9306
LW
738 case OP_GOTO:
739 case OP_NEXT:
740 case OP_LAST:
741 case OP_REDO:
11343788 742 if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS))
748a9306
LW
743 break;
744 /* FALL THROUGH */
a0d0e21e 745 case OP_TRANS:
acb36ea4 746 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
a0ed51b3 747 SvREFCNT_dec(cSVOPo->op_sv);
acb36ea4
GS
748 cSVOPo->op_sv = Nullsv;
749 }
750 else {
a0ed51b3 751 Safefree(cPVOPo->op_pv);
acb36ea4
GS
752 cPVOPo->op_pv = Nullch;
753 }
a0d0e21e
LW
754 break;
755 case OP_SUBST:
11343788 756 op_free(cPMOPo->op_pmreplroot);
acb36ea4 757 cPMOPo->op_pmreplroot = Nullop;
a0d0e21e 758 /* FALL THROUGH */
748a9306 759 case OP_PUSHRE:
a0d0e21e 760 case OP_MATCH:
8782bef2 761 case OP_QR:
c277df42 762 ReREFCNT_dec(cPMOPo->op_pmregexp);
acb36ea4 763 cPMOPo->op_pmregexp = (REGEXP*)NULL;
a0d0e21e 764 break;
79072805
LW
765 }
766
743e66e6 767 if (o->op_targ > 0) {
11343788 768 pad_free(o->op_targ);
743e66e6
GS
769 o->op_targ = 0;
770 }
79072805
LW
771}
772
76e3520e 773STATIC void
3eb57f73
HS
774S_cop_free(pTHX_ COP* cop)
775{
776 Safefree(cop->cop_label);
57843af0
GS
777#ifdef USE_ITHREADS
778 Safefree(CopFILE(cop)); /* XXXXX share in a pvtable? */
11faa288 779 Safefree(CopSTASHPV(cop)); /* XXXXX share in a pvtable? */
57843af0 780#else
11faa288 781 /* NOTE: COP.cop_stash is not refcounted */
cc49e20b 782 SvREFCNT_dec(CopFILEGV(cop));
57843af0 783#endif
0453d815 784 if (! specialWARN(cop->cop_warnings))
3eb57f73
HS
785 SvREFCNT_dec(cop->cop_warnings);
786}
787
788STATIC void
cea2e8a9 789S_null(pTHX_ OP *o)
8990e307 790{
acb36ea4
GS
791 if (o->op_type == OP_NULL)
792 return;
793 op_clear(o);
11343788
MB
794 o->op_targ = o->op_type;
795 o->op_type = OP_NULL;
22c35a8c 796 o->op_ppaddr = PL_ppaddr[OP_NULL];
8990e307
LW
797}
798
79072805
LW
799/* Contextualizers */
800
463ee0b2 801#define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o))
79072805
LW
802
803OP *
864dbfa3 804Perl_linklist(pTHX_ OP *o)
79072805
LW
805{
806 register OP *kid;
807
11343788
MB
808 if (o->op_next)
809 return o->op_next;
79072805
LW
810
811 /* establish postfix order */
11343788
MB
812 if (cUNOPo->op_first) {
813 o->op_next = LINKLIST(cUNOPo->op_first);
814 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
815 if (kid->op_sibling)
816 kid->op_next = LINKLIST(kid->op_sibling);
817 else
11343788 818 kid->op_next = o;
79072805
LW
819 }
820 }
821 else
11343788 822 o->op_next = o;
79072805 823
11343788 824 return o->op_next;
79072805
LW
825}
826
827OP *
864dbfa3 828Perl_scalarkids(pTHX_ OP *o)
79072805
LW
829{
830 OP *kid;
11343788
MB
831 if (o && o->op_flags & OPf_KIDS) {
832 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
833 scalar(kid);
834 }
11343788 835 return o;
79072805
LW
836}
837
76e3520e 838STATIC OP *
cea2e8a9 839S_scalarboolean(pTHX_ OP *o)
8990e307 840{
d008e5eb 841 if (o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) {
0f15f207 842 dTHR;
d008e5eb 843 if (ckWARN(WARN_SYNTAX)) {
57843af0 844 line_t oldline = CopLINE(PL_curcop);
a0d0e21e 845
d008e5eb 846 if (PL_copline != NOLINE)
57843af0 847 CopLINE_set(PL_curcop, PL_copline);
cea2e8a9 848 Perl_warner(aTHX_ WARN_SYNTAX, "Found = in conditional, should be ==");
57843af0 849 CopLINE_set(PL_curcop, oldline);
d008e5eb 850 }
a0d0e21e 851 }
11343788 852 return scalar(o);
8990e307
LW
853}
854
855OP *
864dbfa3 856Perl_scalar(pTHX_ OP *o)
79072805
LW
857{
858 OP *kid;
859
a0d0e21e 860 /* assumes no premature commitment */
3280af22 861 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 862 || o->op_type == OP_RETURN)
7e363e51 863 {
11343788 864 return o;
7e363e51 865 }
79072805 866
5dc0d613 867 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR;
79072805 868
11343788 869 switch (o->op_type) {
79072805 870 case OP_REPEAT:
11343788
MB
871 if (o->op_private & OPpREPEAT_DOLIST)
872 null(((LISTOP*)cBINOPo->op_first)->op_first);
873 scalar(cBINOPo->op_first);
8990e307 874 break;
79072805
LW
875 case OP_OR:
876 case OP_AND:
877 case OP_COND_EXPR:
11343788 878 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
8990e307 879 scalar(kid);
79072805 880 break;
a0d0e21e 881 case OP_SPLIT:
11343788 882 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
883 if (!kPMOP->op_pmreplroot)
884 deprecate("implicit split to @_");
885 }
886 /* FALL THROUGH */
79072805 887 case OP_MATCH:
8782bef2 888 case OP_QR:
79072805
LW
889 case OP_SUBST:
890 case OP_NULL:
8990e307 891 default:
11343788
MB
892 if (o->op_flags & OPf_KIDS) {
893 for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling)
8990e307
LW
894 scalar(kid);
895 }
79072805
LW
896 break;
897 case OP_LEAVE:
898 case OP_LEAVETRY:
5dc0d613 899 kid = cLISTOPo->op_first;
54310121 900 scalar(kid);
901 while (kid = kid->op_sibling) {
902 if (kid->op_sibling)
903 scalarvoid(kid);
904 else
905 scalar(kid);
906 }
3280af22 907 WITH_THR(PL_curcop = &PL_compiling);
54310121 908 break;
748a9306 909 case OP_SCOPE:
79072805 910 case OP_LINESEQ:
8990e307 911 case OP_LIST:
11343788 912 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
913 if (kid->op_sibling)
914 scalarvoid(kid);
915 else
916 scalar(kid);
917 }
3280af22 918 WITH_THR(PL_curcop = &PL_compiling);
79072805
LW
919 break;
920 }
11343788 921 return o;
79072805
LW
922}
923
924OP *
864dbfa3 925Perl_scalarvoid(pTHX_ OP *o)
79072805
LW
926{
927 OP *kid;
8990e307
LW
928 char* useless = 0;
929 SV* sv;
2ebea0a1
GS
930 U8 want;
931
acb36ea4
GS
932 if (o->op_type == OP_NEXTSTATE
933 || o->op_type == OP_SETSTATE
934 || o->op_type == OP_DBSTATE
935 || (o->op_type == OP_NULL && (o->op_targ == OP_NEXTSTATE
936 || o->op_targ == OP_SETSTATE
937 || o->op_targ == OP_DBSTATE)))
2ebea0a1
GS
938 {
939 dTHR;
940 PL_curcop = (COP*)o; /* for warning below */
941 }
79072805 942
54310121 943 /* assumes no premature commitment */
2ebea0a1
GS
944 want = o->op_flags & OPf_WANT;
945 if ((want && want != OPf_WANT_SCALAR) || PL_error_count
5dc0d613 946 || o->op_type == OP_RETURN)
7e363e51 947 {
11343788 948 return o;
7e363e51 949 }
79072805 950
b162f9ea 951 if ((o->op_private & OPpTARGET_MY)
7e363e51
GS
952 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
953 {
b162f9ea 954 return scalar(o); /* As if inside SASSIGN */
7e363e51 955 }
b162f9ea 956
5dc0d613 957 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID;
79072805 958
11343788 959 switch (o->op_type) {
79072805 960 default:
22c35a8c 961 if (!(PL_opargs[o->op_type] & OA_FOLDCONST))
8990e307 962 break;
36477c24 963 /* FALL THROUGH */
964 case OP_REPEAT:
11343788 965 if (o->op_flags & OPf_STACKED)
8990e307 966 break;
5d82c453
GA
967 goto func_ops;
968 case OP_SUBSTR:
969 if (o->op_private == 4)
970 break;
8990e307
LW
971 /* FALL THROUGH */
972 case OP_GVSV:
973 case OP_WANTARRAY:
974 case OP_GV:
975 case OP_PADSV:
976 case OP_PADAV:
977 case OP_PADHV:
978 case OP_PADANY:
979 case OP_AV2ARYLEN:
8990e307 980 case OP_REF:
a0d0e21e
LW
981 case OP_REFGEN:
982 case OP_SREFGEN:
8990e307
LW
983 case OP_DEFINED:
984 case OP_HEX:
985 case OP_OCT:
986 case OP_LENGTH:
8990e307
LW
987 case OP_VEC:
988 case OP_INDEX:
989 case OP_RINDEX:
990 case OP_SPRINTF:
991 case OP_AELEM:
992 case OP_AELEMFAST:
993 case OP_ASLICE:
8990e307
LW
994 case OP_HELEM:
995 case OP_HSLICE:
996 case OP_UNPACK:
997 case OP_PACK:
8990e307
LW
998 case OP_JOIN:
999 case OP_LSLICE:
1000 case OP_ANONLIST:
1001 case OP_ANONHASH:
1002 case OP_SORT:
1003 case OP_REVERSE:
1004 case OP_RANGE:
1005 case OP_FLIP:
1006 case OP_FLOP:
1007 case OP_CALLER:
1008 case OP_FILENO:
1009 case OP_EOF:
1010 case OP_TELL:
1011 case OP_GETSOCKNAME:
1012 case OP_GETPEERNAME:
1013 case OP_READLINK:
1014 case OP_TELLDIR:
1015 case OP_GETPPID:
1016 case OP_GETPGRP:
1017 case OP_GETPRIORITY:
1018 case OP_TIME:
1019 case OP_TMS:
1020 case OP_LOCALTIME:
1021 case OP_GMTIME:
1022 case OP_GHBYNAME:
1023 case OP_GHBYADDR:
1024 case OP_GHOSTENT:
1025 case OP_GNBYNAME:
1026 case OP_GNBYADDR:
1027 case OP_GNETENT:
1028 case OP_GPBYNAME:
1029 case OP_GPBYNUMBER:
1030 case OP_GPROTOENT:
1031 case OP_GSBYNAME:
1032 case OP_GSBYPORT:
1033 case OP_GSERVENT:
1034 case OP_GPWNAM:
1035 case OP_GPWUID:
1036 case OP_GGRNAM:
1037 case OP_GGRGID:
1038 case OP_GETLOGIN:
5d82c453 1039 func_ops:
11343788 1040 if (!(o->op_private & OPpLVAL_INTRO))
22c35a8c 1041 useless = PL_op_desc[o->op_type];
8990e307
LW
1042 break;
1043
1044 case OP_RV2GV:
1045 case OP_RV2SV:
1046 case OP_RV2AV:
1047 case OP_RV2HV:
11343788
MB
1048 if (!(o->op_private & OPpLVAL_INTRO) &&
1049 (!o->op_sibling || o->op_sibling->op_type != OP_READLINE))
8990e307
LW
1050 useless = "a variable";
1051 break;
79072805
LW
1052
1053 case OP_CONST:
11343788 1054 sv = cSVOPo->op_sv;
7a52d87a
GS
1055 if (cSVOPo->op_private & OPpCONST_STRICT)
1056 no_bareword_allowed(o);
1057 else {
d008e5eb
GS
1058 dTHR;
1059 if (ckWARN(WARN_VOID)) {
1060 useless = "a constant";
1061 if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0))
1062 useless = 0;
1063 else if (SvPOK(sv)) {
1064 if (strnEQ(SvPVX(sv), "di", 2) ||
1065 strnEQ(SvPVX(sv), "ds", 2) ||
1066 strnEQ(SvPVX(sv), "ig", 2))
1067 useless = 0;
1068 }
8990e307
LW
1069 }
1070 }
acb36ea4 1071 null(o); /* don't execute or even remember it */
79072805
LW
1072 break;
1073
1074 case OP_POSTINC:
11343788 1075 o->op_type = OP_PREINC; /* pre-increment is faster */
22c35a8c 1076 o->op_ppaddr = PL_ppaddr[OP_PREINC];
79072805
LW
1077 break;
1078
1079 case OP_POSTDEC:
11343788 1080 o->op_type = OP_PREDEC; /* pre-decrement is faster */
22c35a8c 1081 o->op_ppaddr = PL_ppaddr[OP_PREDEC];
79072805
LW
1082 break;
1083
79072805
LW
1084 case OP_OR:
1085 case OP_AND:
1086 case OP_COND_EXPR:
11343788 1087 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1088 scalarvoid(kid);
1089 break;
5aabfad6 1090
a0d0e21e 1091 case OP_NULL:
11343788 1092 if (o->op_flags & OPf_STACKED)
a0d0e21e 1093 break;
5aabfad6 1094 /* FALL THROUGH */
2ebea0a1
GS
1095 case OP_NEXTSTATE:
1096 case OP_DBSTATE:
79072805
LW
1097 case OP_ENTERTRY:
1098 case OP_ENTER:
1099 case OP_SCALAR:
11343788 1100 if (!(o->op_flags & OPf_KIDS))
79072805 1101 break;
54310121 1102 /* FALL THROUGH */
463ee0b2 1103 case OP_SCOPE:
79072805
LW
1104 case OP_LEAVE:
1105 case OP_LEAVETRY:
a0d0e21e 1106 case OP_LEAVELOOP:
79072805 1107 case OP_LINESEQ:
79072805 1108 case OP_LIST:
11343788 1109 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1110 scalarvoid(kid);
1111 break;
c90c0ff4 1112 case OP_ENTEREVAL:
5196be3e 1113 scalarkids(o);
c90c0ff4 1114 break;
5aabfad6 1115 case OP_REQUIRE:
c90c0ff4 1116 /* all requires must return a boolean value */
5196be3e
MB
1117 o->op_flags &= ~OPf_WANT;
1118 return scalar(o);
a0d0e21e 1119 case OP_SPLIT:
11343788 1120 if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) {
a0d0e21e
LW
1121 if (!kPMOP->op_pmreplroot)
1122 deprecate("implicit split to @_");
1123 }
1124 break;
79072805 1125 }
d008e5eb
GS
1126 if (useless) {
1127 dTHR;
1128 if (ckWARN(WARN_VOID))
cea2e8a9 1129 Perl_warner(aTHX_ WARN_VOID, "Useless use of %s in void context", useless);
d008e5eb 1130 }
11343788 1131 return o;
79072805
LW
1132}
1133
1134OP *
864dbfa3 1135Perl_listkids(pTHX_ OP *o)
79072805
LW
1136{
1137 OP *kid;
11343788
MB
1138 if (o && o->op_flags & OPf_KIDS) {
1139 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805
LW
1140 list(kid);
1141 }
11343788 1142 return o;
79072805
LW
1143}
1144
1145OP *
864dbfa3 1146Perl_list(pTHX_ OP *o)
79072805
LW
1147{
1148 OP *kid;
1149
a0d0e21e 1150 /* assumes no premature commitment */
3280af22 1151 if (!o || (o->op_flags & OPf_WANT) || PL_error_count
5dc0d613 1152 || o->op_type == OP_RETURN)
7e363e51 1153 {
11343788 1154 return o;
7e363e51 1155 }
79072805 1156
b162f9ea 1157 if ((o->op_private & OPpTARGET_MY)
7e363e51
GS
1158 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1159 {
b162f9ea 1160 return o; /* As if inside SASSIGN */
7e363e51 1161 }
b162f9ea 1162
5dc0d613 1163 o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST;
79072805 1164
11343788 1165 switch (o->op_type) {
79072805
LW
1166 case OP_FLOP:
1167 case OP_REPEAT:
11343788 1168 list(cBINOPo->op_first);
79072805
LW
1169 break;
1170 case OP_OR:
1171 case OP_AND:
1172 case OP_COND_EXPR:
11343788 1173 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
79072805
LW
1174 list(kid);
1175 break;
1176 default:
1177 case OP_MATCH:
8782bef2 1178 case OP_QR:
79072805
LW
1179 case OP_SUBST:
1180 case OP_NULL:
11343788 1181 if (!(o->op_flags & OPf_KIDS))
79072805 1182 break;
11343788
MB
1183 if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) {
1184 list(cBINOPo->op_first);
1185 return gen_constant_list(o);
79072805
LW
1186 }
1187 case OP_LIST:
11343788 1188 listkids(o);
79072805
LW
1189 break;
1190 case OP_LEAVE:
1191 case OP_LEAVETRY:
5dc0d613 1192 kid = cLISTOPo->op_first;
54310121 1193 list(kid);
1194 while (kid = kid->op_sibling) {
1195 if (kid->op_sibling)
1196 scalarvoid(kid);
1197 else
1198 list(kid);
1199 }
3280af22 1200 WITH_THR(PL_curcop = &PL_compiling);
54310121 1201 break;
748a9306 1202 case OP_SCOPE:
79072805 1203 case OP_LINESEQ:
11343788 1204 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
79072805
LW
1205 if (kid->op_sibling)
1206 scalarvoid(kid);
1207 else
1208 list(kid);
1209 }
3280af22 1210 WITH_THR(PL_curcop = &PL_compiling);
79072805 1211 break;
c90c0ff4 1212 case OP_REQUIRE:
1213 /* all requires must return a boolean value */
5196be3e
MB
1214 o->op_flags &= ~OPf_WANT;
1215 return scalar(o);
79072805 1216 }
11343788 1217 return o;
79072805
LW
1218}
1219
1220OP *
864dbfa3 1221Perl_scalarseq(pTHX_ OP *o)
79072805
LW
1222{
1223 OP *kid;
1224
11343788
MB
1225 if (o) {
1226 if (o->op_type == OP_LINESEQ ||
1227 o->op_type == OP_SCOPE ||
1228 o->op_type == OP_LEAVE ||
1229 o->op_type == OP_LEAVETRY)
463ee0b2 1230 {
0f15f207 1231 dTHR;
11343788 1232 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) {
ed6116ce 1233 if (kid->op_sibling) {
463ee0b2 1234 scalarvoid(kid);
ed6116ce 1235 }
463ee0b2 1236 }
3280af22 1237 PL_curcop = &PL_compiling;
79072805 1238 }
11343788 1239 o->op_flags &= ~OPf_PARENS;
3280af22 1240 if (PL_hints & HINT_BLOCK_SCOPE)
11343788 1241 o->op_flags |= OPf_PARENS;
79072805 1242 }
8990e307 1243 else
11343788
MB
1244 o = newOP(OP_STUB, 0);
1245 return o;
79072805
LW
1246}
1247
76e3520e 1248STATIC OP *
cea2e8a9 1249S_modkids(pTHX_ OP *o, I32 type)
79072805
LW
1250{
1251 OP *kid;
11343788
MB
1252 if (o && o->op_flags & OPf_KIDS) {
1253 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2 1254 mod(kid, type);
79072805 1255 }
11343788 1256 return o;
79072805
LW
1257}
1258
79072805 1259OP *
864dbfa3 1260Perl_mod(pTHX_ OP *o, I32 type)
79072805 1261{
11343788 1262 dTHR;
79072805
LW
1263 OP *kid;
1264 SV *sv;
2d8e6c8d 1265 STRLEN n_a;
79072805 1266
3280af22 1267 if (!o || PL_error_count)
11343788 1268 return o;
79072805 1269
b162f9ea 1270 if ((o->op_private & OPpTARGET_MY)
7e363e51
GS
1271 && (PL_opargs[o->op_type] & OA_TARGLEX))/* OPp share the meaning */
1272 {
b162f9ea 1273 return o;
7e363e51 1274 }
b162f9ea 1275
11343788 1276 switch (o->op_type) {
68dc0745 1277 case OP_UNDEF:
3280af22 1278 PL_modcount++;
5dc0d613 1279 return o;
a0d0e21e 1280 case OP_CONST:
11343788 1281 if (!(o->op_private & (OPpCONST_ARYBASE)))
a0d0e21e 1282 goto nomod;
3280af22
NIS
1283 if (PL_eval_start && PL_eval_start->op_type == OP_CONST) {
1284 PL_compiling.cop_arybase = (I32)SvIV(((SVOP*)PL_eval_start)->op_sv);
1285 PL_eval_start = 0;
a0d0e21e
LW
1286 }
1287 else if (!type) {
3280af22
NIS
1288 SAVEI32(PL_compiling.cop_arybase);
1289 PL_compiling.cop_arybase = 0;
a0d0e21e
LW
1290 }
1291 else if (type == OP_REFGEN)
1292 goto nomod;
1293 else
cea2e8a9 1294 Perl_croak(aTHX_ "That use of $[ is unsupported");
a0d0e21e 1295 break;
5f05dabc 1296 case OP_STUB:
5196be3e 1297 if (o->op_flags & OPf_PARENS)
5f05dabc 1298 break;
1299 goto nomod;
a0d0e21e
LW
1300 case OP_ENTERSUB:
1301 if ((type == OP_UNDEF || type == OP_REFGEN) &&
11343788
MB
1302 !(o->op_flags & OPf_STACKED)) {
1303 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1304 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1305 assert(cUNOPo->op_first->op_type == OP_NULL);
1306 null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */
79072805
LW
1307 break;
1308 }
cd06dffe
GS
1309 else { /* lvalue subroutine call */
1310 o->op_private |= OPpLVAL_INTRO;
1311 if (type == OP_GREPSTART || type == OP_ENTERSUB) {
1312 /* Backward compatibility mode: */
1313 o->op_private |= OPpENTERSUB_INARGS;
1314 break;
1315 }
1316 else { /* Compile-time error message: */
1317 OP *kid = cUNOPo->op_first;
1318 CV *cv;
1319 OP *okid;
1320
1321 if (kid->op_type == OP_PUSHMARK)
1322 goto skip_kids;
1323 if (kid->op_type != OP_NULL || kid->op_targ != OP_LIST)
1324 Perl_croak(aTHX_
1325 "panic: unexpected lvalue entersub "
1326 "args: type/targ %ld:%ld",
1327 (long)kid->op_type,kid->op_targ);
1328 kid = kLISTOP->op_first;
1329 skip_kids:
1330 while (kid->op_sibling)
1331 kid = kid->op_sibling;
1332 if (!(kid->op_type == OP_NULL && kid->op_targ == OP_RV2CV)) {
1333 /* Indirect call */
1334 if (kid->op_type == OP_METHOD_NAMED
1335 || kid->op_type == OP_METHOD)
1336 {
349fd7b7 1337 OP *newop;
cd06dffe
GS
1338
1339 if (kid->op_sibling || kid->op_next != kid) {
1340 yyerror("panic: unexpected optree near method call");
1341 break;
1342 }
1343
349fd7b7
GS
1344 NewOp(1101, newop, 1, OP);
1345 newop->op_type = OP_RV2CV;
1346 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
1347 newop->op_next = newop;
1348 kid->op_sibling = newop;
1349 newop->op_private |= OPpLVAL_INTRO;
cd06dffe
GS
1350 break;
1351 }
1352
1353 if (kid->op_type != OP_RV2CV)
1354 Perl_croak(aTHX_
1355 "panic: unexpected lvalue entersub "
1356 "entry via type/targ %ld:%ld",
1357 (long)kid->op_type,kid->op_targ);
1358 kid->op_private |= OPpLVAL_INTRO;
1359 break; /* Postpone until runtime */
1360 }
1361
1362 okid = kid;
1363 kid = kUNOP->op_first;
1364 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1365 kid = kUNOP->op_first;
1366 if (kid->op_type == OP_NULL)
1367 Perl_croak(aTHX_
1368 "Unexpected constant lvalue entersub "
1369 "entry via type/targ %ld:%ld",
1370 (long)kid->op_type,kid->op_targ);
1371 if (kid->op_type != OP_GV) {
1372 /* Restore RV2CV to check lvalueness */
1373 restore_2cv:
1374 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1375 okid->op_next = kid->op_next;
1376 kid->op_next = okid;
1377 }
1378 else
1379 okid->op_next = Nullop;
1380 okid->op_type = OP_RV2CV;
1381 okid->op_targ = 0;
1382 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1383 okid->op_private |= OPpLVAL_INTRO;
1384 break;
1385 }
1386
350de78d 1387 cv = GvCV(kGVOP);
cd06dffe
GS
1388 if (!cv)
1389 goto restore_2cv;
1390 if (CvLVALUE(cv))
1391 break;
1392 }
1393 }
79072805
LW
1394 /* FALL THROUGH */
1395 default:
a0d0e21e
LW
1396 nomod:
1397 /* grep, foreach, subcalls, refgen */
1398 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1399 break;
cea2e8a9 1400 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
638bc118 1401 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
cd06dffe
GS
1402 ? "do block"
1403 : (o->op_type == OP_ENTERSUB
1404 ? "non-lvalue subroutine call"
1405 : PL_op_desc[o->op_type])),
22c35a8c 1406 type ? PL_op_desc[type] : "local"));
11343788 1407 return o;
79072805 1408
a0d0e21e
LW
1409 case OP_PREINC:
1410 case OP_PREDEC:
1411 case OP_POW:
1412 case OP_MULTIPLY:
1413 case OP_DIVIDE:
1414 case OP_MODULO:
1415 case OP_REPEAT:
1416 case OP_ADD:
1417 case OP_SUBTRACT:
1418 case OP_CONCAT:
1419 case OP_LEFT_SHIFT:
1420 case OP_RIGHT_SHIFT:
1421 case OP_BIT_AND:
1422 case OP_BIT_XOR:
1423 case OP_BIT_OR:
1424 case OP_I_MULTIPLY:
1425 case OP_I_DIVIDE:
1426 case OP_I_MODULO:
1427 case OP_I_ADD:
1428 case OP_I_SUBTRACT:
11343788 1429 if (!(o->op_flags & OPf_STACKED))
a0d0e21e 1430 goto nomod;
3280af22 1431 PL_modcount++;
a0d0e21e
LW
1432 break;
1433
79072805 1434 case OP_COND_EXPR:
11343788 1435 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2 1436 mod(kid, type);
79072805
LW
1437 break;
1438
1439 case OP_RV2AV:
1440 case OP_RV2HV:
93af7a87 1441 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1442 Perl_croak(aTHX_ "Can't localize through a reference");
11343788 1443 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3280af22 1444 PL_modcount = 10000;
11343788 1445 return o; /* Treat \(@foo) like ordinary list. */
748a9306
LW
1446 }
1447 /* FALL THROUGH */
79072805 1448 case OP_RV2GV:
5dc0d613 1449 if (scalar_mod_type(o, type))
3fe9a6f1 1450 goto nomod;
11343788 1451 ref(cUNOPo->op_first, o->op_type);
79072805
LW
1452 /* FALL THROUGH */
1453 case OP_AASSIGN:
1454 case OP_ASLICE:
1455 case OP_HSLICE:
93a17b20
LW
1456 case OP_NEXTSTATE:
1457 case OP_DBSTATE:
a0d0e21e
LW
1458 case OP_REFGEN:
1459 case OP_CHOMP:
3280af22 1460 PL_modcount = 10000;
79072805 1461 break;
463ee0b2 1462 case OP_RV2SV:
11343788 1463 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1464 Perl_croak(aTHX_ "Can't localize through a reference");
aeea060c 1465 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1466 /* FALL THROUGH */
79072805 1467 case OP_GV:
463ee0b2 1468 case OP_AV2ARYLEN:
3280af22 1469 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1470 case OP_SASSIGN:
bf4b1e52
GS
1471 case OP_ANDASSIGN:
1472 case OP_ORASSIGN:
8990e307 1473 case OP_AELEMFAST:
3280af22 1474 PL_modcount++;
8990e307
LW
1475 break;
1476
748a9306
LW
1477 case OP_PADAV:
1478 case OP_PADHV:
3280af22 1479 PL_modcount = 10000;
5196be3e
MB
1480 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1481 return o; /* Treat \(@foo) like ordinary list. */
1482 if (scalar_mod_type(o, type))
3fe9a6f1 1483 goto nomod;
748a9306
LW
1484 /* FALL THROUGH */
1485 case OP_PADSV:
3280af22 1486 PL_modcount++;
748a9306 1487 if (!type)
cea2e8a9 1488 Perl_croak(aTHX_ "Can't localize lexical variable %s",
2d8e6c8d 1489 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
463ee0b2
LW
1490 break;
1491
554b3eca 1492#ifdef USE_THREADS
2faa37cc 1493 case OP_THREADSV:
533c011a 1494 PL_modcount++; /* XXX ??? */
554b3eca
MB
1495 break;
1496#endif /* USE_THREADS */
1497
748a9306
LW
1498 case OP_PUSHMARK:
1499 break;
a0d0e21e 1500
69969c6f
SB
1501 case OP_KEYS:
1502 if (type != OP_SASSIGN)
1503 goto nomod;
5d82c453
GA
1504 goto lvalue_func;
1505 case OP_SUBSTR:
1506 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1507 goto nomod;
5f05dabc 1508 /* FALL THROUGH */
a0d0e21e 1509 case OP_POS:
463ee0b2 1510 case OP_VEC:
5d82c453 1511 lvalue_func:
11343788
MB
1512 pad_free(o->op_targ);
1513 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
5dc0d613 1514 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
11343788
MB
1515 if (o->op_flags & OPf_KIDS)
1516 mod(cBINOPo->op_first->op_sibling, type);
463ee0b2 1517 break;
a0d0e21e 1518
463ee0b2
LW
1519 case OP_AELEM:
1520 case OP_HELEM:
11343788 1521 ref(cBINOPo->op_first, o->op_type);
68dc0745 1522 if (type == OP_ENTERSUB &&
5dc0d613
MB
1523 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1524 o->op_private |= OPpLVAL_DEFER;
3280af22 1525 PL_modcount++;
463ee0b2
LW
1526 break;
1527
1528 case OP_SCOPE:
1529 case OP_LEAVE:
1530 case OP_ENTER:
11343788
MB
1531 if (o->op_flags & OPf_KIDS)
1532 mod(cLISTOPo->op_last, type);
a0d0e21e
LW
1533 break;
1534
1535 case OP_NULL:
638bc118
GS
1536 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1537 goto nomod;
1538 else if (!(o->op_flags & OPf_KIDS))
463ee0b2 1539 break;
11343788
MB
1540 if (o->op_targ != OP_LIST) {
1541 mod(cBINOPo->op_first, type);
a0d0e21e
LW
1542 break;
1543 }
1544 /* FALL THROUGH */
463ee0b2 1545 case OP_LIST:
11343788 1546 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1547 mod(kid, type);
1548 break;
1549 }
11343788 1550 o->op_flags |= OPf_MOD;
a0d0e21e
LW
1551
1552 if (type == OP_AASSIGN || type == OP_SASSIGN)
11343788 1553 o->op_flags |= OPf_SPECIAL|OPf_REF;
a0d0e21e 1554 else if (!type) {
11343788
MB
1555 o->op_private |= OPpLVAL_INTRO;
1556 o->op_flags &= ~OPf_SPECIAL;
3280af22 1557 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1558 }
a0d0e21e 1559 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
11343788
MB
1560 o->op_flags |= OPf_REF;
1561 return o;
463ee0b2
LW
1562}
1563
864dbfa3 1564STATIC bool
cea2e8a9 1565S_scalar_mod_type(pTHX_ OP *o, I32 type)
3fe9a6f1 1566{
1567 switch (type) {
1568 case OP_SASSIGN:
5196be3e 1569 if (o->op_type == OP_RV2GV)
3fe9a6f1 1570 return FALSE;
1571 /* FALL THROUGH */
1572 case OP_PREINC:
1573 case OP_PREDEC:
1574 case OP_POSTINC:
1575 case OP_POSTDEC:
1576 case OP_I_PREINC:
1577 case OP_I_PREDEC:
1578 case OP_I_POSTINC:
1579 case OP_I_POSTDEC:
1580 case OP_POW:
1581 case OP_MULTIPLY:
1582 case OP_DIVIDE:
1583 case OP_MODULO:
1584 case OP_REPEAT:
1585 case OP_ADD:
1586 case OP_SUBTRACT:
1587 case OP_I_MULTIPLY:
1588 case OP_I_DIVIDE:
1589 case OP_I_MODULO:
1590 case OP_I_ADD:
1591 case OP_I_SUBTRACT:
1592 case OP_LEFT_SHIFT:
1593 case OP_RIGHT_SHIFT:
1594 case OP_BIT_AND:
1595 case OP_BIT_XOR:
1596 case OP_BIT_OR:
1597 case OP_CONCAT:
1598 case OP_SUBST:
1599 case OP_TRANS:
49e9fbe6
GS
1600 case OP_READ:
1601 case OP_SYSREAD:
1602 case OP_RECV:
bf4b1e52
GS
1603 case OP_ANDASSIGN:
1604 case OP_ORASSIGN:
3fe9a6f1 1605 return TRUE;
1606 default:
1607 return FALSE;
1608 }
1609}
1610
35cd451c 1611STATIC bool
cea2e8a9 1612S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
35cd451c
GS
1613{
1614 switch (o->op_type) {
1615 case OP_PIPE_OP:
1616 case OP_SOCKPAIR:
1617 if (argnum == 2)
1618 return TRUE;
1619 /* FALL THROUGH */
1620 case OP_SYSOPEN:
1621 case OP_OPEN:
ded8aa31 1622 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
35cd451c
GS
1623 case OP_SOCKET:
1624 case OP_OPEN_DIR:
1625 case OP_ACCEPT:
1626 if (argnum == 1)
1627 return TRUE;
1628 /* FALL THROUGH */
1629 default:
1630 return FALSE;
1631 }
1632}
1633
463ee0b2 1634OP *
864dbfa3 1635Perl_refkids(pTHX_ OP *o, I32 type)
463ee0b2
LW
1636{
1637 OP *kid;
11343788
MB
1638 if (o && o->op_flags & OPf_KIDS) {
1639 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1640 ref(kid, type);
1641 }
11343788 1642 return o;
463ee0b2
LW
1643}
1644
1645OP *
864dbfa3 1646Perl_ref(pTHX_ OP *o, I32 type)
463ee0b2
LW
1647{
1648 OP *kid;
463ee0b2 1649
3280af22 1650 if (!o || PL_error_count)
11343788 1651 return o;
463ee0b2 1652
11343788 1653 switch (o->op_type) {
a0d0e21e 1654 case OP_ENTERSUB:
e55aaa0e 1655 if ((type == OP_DEFINED || type == OP_LOCK) &&
11343788
MB
1656 !(o->op_flags & OPf_STACKED)) {
1657 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1658 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1659 assert(cUNOPo->op_first->op_type == OP_NULL);
1660 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1661 o->op_flags |= OPf_SPECIAL;
8990e307
LW
1662 }
1663 break;
aeea060c 1664
463ee0b2 1665 case OP_COND_EXPR:
11343788 1666 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2
LW
1667 ref(kid, type);
1668 break;
8990e307 1669 case OP_RV2SV:
35cd451c
GS
1670 if (type == OP_DEFINED)
1671 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1672 ref(cUNOPo->op_first, o->op_type);
4633a7c4
LW
1673 /* FALL THROUGH */
1674 case OP_PADSV:
5f05dabc 1675 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1676 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1677 : type == OP_RV2HV ? OPpDEREF_HV
1678 : OPpDEREF_SV);
11343788 1679 o->op_flags |= OPf_MOD;
a0d0e21e 1680 }
8990e307
LW
1681 break;
1682
2faa37cc 1683 case OP_THREADSV:
a863c7d1
MB
1684 o->op_flags |= OPf_MOD; /* XXX ??? */
1685 break;
1686
463ee0b2
LW
1687 case OP_RV2AV:
1688 case OP_RV2HV:
aeea060c 1689 o->op_flags |= OPf_REF;
8990e307 1690 /* FALL THROUGH */
463ee0b2 1691 case OP_RV2GV:
35cd451c
GS
1692 if (type == OP_DEFINED)
1693 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1694 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1695 break;
8990e307 1696
463ee0b2
LW
1697 case OP_PADAV:
1698 case OP_PADHV:
aeea060c 1699 o->op_flags |= OPf_REF;
79072805 1700 break;
aeea060c 1701
8990e307 1702 case OP_SCALAR:
79072805 1703 case OP_NULL:
11343788 1704 if (!(o->op_flags & OPf_KIDS))
463ee0b2 1705 break;
11343788 1706 ref(cBINOPo->op_first, type);
79072805
LW
1707 break;
1708 case OP_AELEM:
1709 case OP_HELEM:
11343788 1710 ref(cBINOPo->op_first, o->op_type);
5f05dabc 1711 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1712 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1713 : type == OP_RV2HV ? OPpDEREF_HV
1714 : OPpDEREF_SV);
11343788 1715 o->op_flags |= OPf_MOD;
8990e307 1716 }
79072805
LW
1717 break;
1718
463ee0b2 1719 case OP_SCOPE:
79072805
LW
1720 case OP_LEAVE:
1721 case OP_ENTER:
8990e307 1722 case OP_LIST:
11343788 1723 if (!(o->op_flags & OPf_KIDS))
79072805 1724 break;
11343788 1725 ref(cLISTOPo->op_last, type);
79072805 1726 break;
a0d0e21e
LW
1727 default:
1728 break;
79072805 1729 }
11343788 1730 return scalar(o);
8990e307 1731
79072805
LW
1732}
1733
09bef843
SB
1734STATIC OP *
1735S_dup_attrlist(pTHX_ OP *o)
1736{
1737 OP *rop = Nullop;
1738
1739 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1740 * where the first kid is OP_PUSHMARK and the remaining ones
1741 * are OP_CONST. We need to push the OP_CONST values.
1742 */
1743 if (o->op_type == OP_CONST)
1744 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1745 else {
1746 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1747 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1748 if (o->op_type == OP_CONST)
1749 rop = append_elem(OP_LIST, rop,
1750 newSVOP(OP_CONST, o->op_flags,
1751 SvREFCNT_inc(cSVOPo->op_sv)));
1752 }
1753 }
1754 return rop;
1755}
1756
1757STATIC void
1758S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1759{
1760 OP *modname; /* for 'use' */
1761 SV *stashsv;
1762
1763 /* fake up C<use attributes $pkg,$rv,@attrs> */
1764 ENTER; /* need to protect against side-effects of 'use' */
1765 SAVEINT(PL_expect);
1766 if (stash && HvNAME(stash))
1767 stashsv = newSVpv(HvNAME(stash), 0);
1768 else
1769 stashsv = &PL_sv_no;
1770#define ATTRSMODULE "attributes"
1771 modname = newSVOP(OP_CONST, 0,
1772 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1773 modname->op_private |= OPpCONST_BARE;
1774 /* that flag is required to make 'use' work right */
1775 utilize(1, start_subparse(FALSE, 0),
1776 Nullop, /* version */
1777 modname,
1778 prepend_elem(OP_LIST,
1779 newSVOP(OP_CONST, 0, stashsv),
1780 prepend_elem(OP_LIST,
1781 newSVOP(OP_CONST, 0, newRV(target)),
1782 dup_attrlist(attrs))));
1783 LEAVE;
1784}
1785
1786STATIC OP *
1787S_my_kid(pTHX_ OP *o, OP *attrs)
93a17b20
LW
1788{
1789 OP *kid;
93a17b20
LW
1790 I32 type;
1791
3280af22 1792 if (!o || PL_error_count)
11343788 1793 return o;
93a17b20 1794
11343788 1795 type = o->op_type;
93a17b20 1796 if (type == OP_LIST) {
11343788 1797 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
09bef843 1798 my_kid(kid, attrs);
dab48698 1799 } else if (type == OP_UNDEF) {
7766148a 1800 return o;
77ca0c92
LW
1801 } else if (type == OP_RV2SV || /* "our" declaration */
1802 type == OP_RV2AV ||
1803 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
1804 return o;
dab48698 1805 } else if (type != OP_PADSV &&
93a17b20
LW
1806 type != OP_PADAV &&
1807 type != OP_PADHV &&
1808 type != OP_PUSHMARK)
1809 {
cea2e8a9 1810 yyerror(Perl_form(aTHX_ "Can't declare %s in my", PL_op_desc[o->op_type]));
11343788 1811 return o;
93a17b20 1812 }
09bef843
SB
1813 else if (attrs && type != OP_PUSHMARK) {
1814 HV *stash;
1815 SV *padsv;
1816 SV **namesvp;
1817
1818 /* check for C<my Dog $spot> when deciding package */
1819 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1820 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1821 stash = SvSTASH(*namesvp);
1822 else
1823 stash = PL_curstash;
1824 padsv = PAD_SV(o->op_targ);
1825 apply_attrs(stash, padsv, attrs);
1826 }
11343788
MB
1827 o->op_flags |= OPf_MOD;
1828 o->op_private |= OPpLVAL_INTRO;
1829 return o;
93a17b20
LW
1830}
1831
1832OP *
09bef843
SB
1833Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1834{
1835 if (o->op_flags & OPf_PARENS)
1836 list(o);
1837 PL_in_my = FALSE;
1838 PL_in_my_stash = Nullhv;
1839 if (attrs)
1840 SAVEFREEOP(attrs);
1841 return my_kid(o, attrs);
1842}
1843
1844OP *
1845Perl_my(pTHX_ OP *o)
1846{
1847 return my_kid(o, Nullop);
1848}
1849
1850OP *
864dbfa3 1851Perl_sawparens(pTHX_ OP *o)
79072805
LW
1852{
1853 if (o)
1854 o->op_flags |= OPf_PARENS;
1855 return o;
1856}
1857
1858OP *
864dbfa3 1859Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
79072805 1860{
d008e5eb 1861 dTHR;
11343788 1862 OP *o;
79072805 1863
599cee73
PM
1864 if (ckWARN(WARN_UNSAFE) &&
1865 (left->op_type == OP_RV2AV ||
1866 left->op_type == OP_RV2HV ||
1867 left->op_type == OP_PADAV ||
1868 left->op_type == OP_PADHV)) {
22c35a8c 1869 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
599cee73
PM
1870 right->op_type == OP_TRANS)
1871 ? right->op_type : OP_MATCH];
dff6d3cd
GS
1872 const char *sample = ((left->op_type == OP_RV2AV ||
1873 left->op_type == OP_PADAV)
1874 ? "@array" : "%hash");
cea2e8a9 1875 Perl_warner(aTHX_ WARN_UNSAFE,
599cee73
PM
1876 "Applying %s to %s will act on scalar(%s)",
1877 desc, sample, sample);
2ae324a7 1878 }
1879
79072805
LW
1880 if (right->op_type == OP_MATCH ||
1881 right->op_type == OP_SUBST ||
1882 right->op_type == OP_TRANS) {
1883 right->op_flags |= OPf_STACKED;
1884 if (right->op_type != OP_MATCH)
463ee0b2 1885 left = mod(left, right->op_type);
79072805 1886 if (right->op_type == OP_TRANS)
11343788 1887 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
79072805 1888 else
11343788 1889 o = prepend_elem(right->op_type, scalar(left), right);
79072805 1890 if (type == OP_NOT)
11343788
MB
1891 return newUNOP(OP_NOT, 0, scalar(o));
1892 return o;
79072805
LW
1893 }
1894 else
1895 return bind_match(type, left,
1896 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1897}
1898
1899OP *
864dbfa3 1900Perl_invert(pTHX_ OP *o)
79072805 1901{
11343788
MB
1902 if (!o)
1903 return o;
79072805 1904 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
11343788 1905 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
79072805
LW
1906}
1907
1908OP *
864dbfa3 1909Perl_scope(pTHX_ OP *o)
79072805
LW
1910{
1911 if (o) {
3280af22 1912 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
463ee0b2
LW
1913 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
1914 o->op_type = OP_LEAVE;
22c35a8c 1915 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
463ee0b2
LW
1916 }
1917 else {
1918 if (o->op_type == OP_LINESEQ) {
1919 OP *kid;
1920 o->op_type = OP_SCOPE;
22c35a8c 1921 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
c3ed7a6a
GS
1922 kid = ((LISTOP*)o)->op_first;
1923 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
1924 null(kid);
463ee0b2
LW
1925 }
1926 else
748a9306 1927 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
463ee0b2 1928 }
79072805
LW
1929 }
1930 return o;
1931}
1932
b3ac6de7 1933void
864dbfa3 1934Perl_save_hints(pTHX)
b3ac6de7 1935{
3280af22
NIS
1936 SAVEI32(PL_hints);
1937 SAVESPTR(GvHV(PL_hintgv));
1938 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
1939 SAVEFREESV(GvHV(PL_hintgv));
b3ac6de7
IZ
1940}
1941
a0d0e21e 1942int
864dbfa3 1943Perl_block_start(pTHX_ int full)
79072805 1944{
11343788 1945 dTHR;
3280af22 1946 int retval = PL_savestack_ix;
b3ac6de7 1947
3280af22 1948 SAVEI32(PL_comppad_name_floor);
55497cff 1949 if (full) {
3280af22
NIS
1950 if ((PL_comppad_name_fill = AvFILLp(PL_comppad_name)) > 0)
1951 PL_comppad_name_floor = PL_comppad_name_fill;
55497cff 1952 else
3280af22
NIS
1953 PL_comppad_name_floor = 0;
1954 }
1955 SAVEI32(PL_min_intro_pending);
1956 SAVEI32(PL_max_intro_pending);
1957 PL_min_intro_pending = 0;
1958 SAVEI32(PL_comppad_name_fill);
1959 SAVEI32(PL_padix_floor);
1960 PL_padix_floor = PL_padix;
1961 PL_pad_reset_pending = FALSE;
b3ac6de7 1962 SAVEHINTS();
3280af22 1963 PL_hints &= ~HINT_BLOCK_SCOPE;
e24b16f9 1964 SAVEPPTR(PL_compiling.cop_warnings);
0453d815 1965 if (! specialWARN(PL_compiling.cop_warnings)) {
599cee73
PM
1966 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
1967 SAVEFREESV(PL_compiling.cop_warnings) ;
1968 }
1969
1970
a0d0e21e
LW
1971 return retval;
1972}
1973
1974OP*
864dbfa3 1975Perl_block_end(pTHX_ I32 floor, OP *seq)
a0d0e21e 1976{
11343788 1977 dTHR;
3280af22 1978 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
a0d0e21e 1979 OP* retval = scalarseq(seq);
a0d0e21e 1980 LEAVE_SCOPE(floor);
3280af22 1981 PL_pad_reset_pending = FALSE;
e24b16f9 1982 PL_compiling.op_private = PL_hints;
a0d0e21e 1983 if (needblockscope)
3280af22
NIS
1984 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
1985 pad_leavemy(PL_comppad_name_fill);
1986 PL_cop_seqmax++;
a0d0e21e
LW
1987 return retval;
1988}
1989
76e3520e 1990STATIC OP *
cea2e8a9 1991S_newDEFSVOP(pTHX)
54b9620d
MB
1992{
1993#ifdef USE_THREADS
1994 OP *o = newOP(OP_THREADSV, 0);
1995 o->op_targ = find_threadsv("_");
1996 return o;
1997#else
3280af22 1998 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
54b9620d
MB
1999#endif /* USE_THREADS */
2000}
2001
a0d0e21e 2002void
864dbfa3 2003Perl_newPROG(pTHX_ OP *o)
a0d0e21e 2004{
11343788 2005 dTHR;
3280af22 2006 if (PL_in_eval) {
b295d113
TH
2007 if (PL_eval_root)
2008 return;
faef0170
HS
2009 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2010 ((PL_in_eval & EVAL_KEEPERR)
2011 ? OPf_SPECIAL : 0), o);
3280af22 2012 PL_eval_start = linklist(PL_eval_root);
7934575e
GS
2013 PL_eval_root->op_private |= OPpREFCOUNTED;
2014 OpREFCNT_set(PL_eval_root, 1);
3280af22
NIS
2015 PL_eval_root->op_next = 0;
2016 peep(PL_eval_start);
a0d0e21e
LW
2017 }
2018 else {
5dc0d613 2019 if (!o)
a0d0e21e 2020 return;
3280af22
NIS
2021 PL_main_root = scope(sawparens(scalarvoid(o)));
2022 PL_curcop = &PL_compiling;
2023 PL_main_start = LINKLIST(PL_main_root);
7934575e
GS
2024 PL_main_root->op_private |= OPpREFCOUNTED;
2025 OpREFCNT_set(PL_main_root, 1);
3280af22
NIS
2026 PL_main_root->op_next = 0;
2027 peep(PL_main_start);
2028 PL_compcv = 0;
3841441e 2029
4fdae800 2030 /* Register with debugger */
84902520 2031 if (PERLDB_INTER) {
864dbfa3 2032 CV *cv = get_cv("DB::postponed", FALSE);
3841441e
CS
2033 if (cv) {
2034 dSP;
924508f0 2035 PUSHMARK(SP);
cc49e20b 2036 XPUSHs((SV*)CopFILEGV(&PL_compiling));
3841441e 2037 PUTBACK;
864dbfa3 2038 call_sv((SV*)cv, G_DISCARD);
3841441e
CS
2039 }
2040 }
79072805 2041 }
79072805
LW
2042}
2043
2044OP *
864dbfa3 2045Perl_localize(pTHX_ OP *o, I32 lex)
79072805
LW
2046{
2047 if (o->op_flags & OPf_PARENS)
2048 list(o);
8990e307 2049 else {
d008e5eb 2050 dTHR;
599cee73 2051 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
8990e307 2052 char *s;
834a4ddd 2053 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
a0d0e21e 2054 if (*s == ';' || *s == '=')
cea2e8a9 2055 Perl_warner(aTHX_ WARN_PARENTHESIS, "Parentheses missing around \"%s\" list",
599cee73 2056 lex ? "my" : "local");
8990e307
LW
2057 }
2058 }
3280af22
NIS
2059 PL_in_my = FALSE;
2060 PL_in_my_stash = Nullhv;
93a17b20
LW
2061 if (lex)
2062 return my(o);
2063 else
463ee0b2 2064 return mod(o, OP_NULL); /* a bit kludgey */
79072805
LW
2065}
2066
2067OP *
864dbfa3 2068Perl_jmaybe(pTHX_ OP *o)
79072805
LW
2069{
2070 if (o->op_type == OP_LIST) {
554b3eca
MB
2071 OP *o2;
2072#ifdef USE_THREADS
2faa37cc 2073 o2 = newOP(OP_THREADSV, 0);
54b9620d 2074 o2->op_targ = find_threadsv(";");
554b3eca
MB
2075#else
2076 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2077#endif /* USE_THREADS */
2078 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
79072805
LW
2079 }
2080 return o;
2081}
2082
2083OP *
864dbfa3 2084Perl_fold_constants(pTHX_ register OP *o)
79072805 2085{
11343788 2086 dTHR;
79072805
LW
2087 register OP *curop;
2088 I32 type = o->op_type;
748a9306 2089 SV *sv;
79072805 2090
22c35a8c 2091 if (PL_opargs[type] & OA_RETSCALAR)
79072805 2092 scalar(o);
b162f9ea 2093 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
ed6116ce 2094 o->op_targ = pad_alloc(type, SVs_PADTMP);
79072805 2095
eac055e9
GS
2096 /* integerize op, unless it happens to be C<-foo>.
2097 * XXX should pp_i_negate() do magic string negation instead? */
2098 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2099 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2100 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2101 {
22c35a8c 2102 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
eac055e9 2103 }
85e6fe83 2104
22c35a8c 2105 if (!(PL_opargs[type] & OA_FOLDCONST))
79072805
LW
2106 goto nope;
2107
de939608 2108 switch (type) {
7a52d87a
GS
2109 case OP_NEGATE:
2110 /* XXX might want a ck_negate() for this */
2111 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2112 break;
de939608
CS
2113 case OP_SPRINTF:
2114 case OP_UCFIRST:
2115 case OP_LCFIRST:
2116 case OP_UC:
2117 case OP_LC:
69dcf70c
MB
2118 case OP_SLT:
2119 case OP_SGT:
2120 case OP_SLE:
2121 case OP_SGE:
2122 case OP_SCMP:
2123
de939608
CS
2124 if (o->op_private & OPpLOCALE)
2125 goto nope;
2126 }
2127
3280af22 2128 if (PL_error_count)
a0d0e21e
LW
2129 goto nope; /* Don't try to run w/ errors */
2130
79072805 2131 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
11fa937b
GS
2132 if ((curop->op_type != OP_CONST ||
2133 (curop->op_private & OPpCONST_BARE)) &&
7a52d87a
GS
2134 curop->op_type != OP_LIST &&
2135 curop->op_type != OP_SCALAR &&
2136 curop->op_type != OP_NULL &&
2137 curop->op_type != OP_PUSHMARK)
2138 {
79072805
LW
2139 goto nope;
2140 }
2141 }
2142
2143 curop = LINKLIST(o);
2144 o->op_next = 0;
533c011a 2145 PL_op = curop;
cea2e8a9 2146 CALLRUNOPS(aTHX);
3280af22 2147 sv = *(PL_stack_sp--);
748a9306 2148 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
79072805 2149 pad_swipe(o->op_targ);
748a9306
LW
2150 else if (SvTEMP(sv)) { /* grab mortal temp? */
2151 (void)SvREFCNT_inc(sv);
2152 SvTEMP_off(sv);
85e6fe83 2153 }
79072805
LW
2154 op_free(o);
2155 if (type == OP_RV2GV)
b1cb66bf 2156 return newGVOP(OP_GV, 0, (GV*)sv);
748a9306 2157 else {
ee580363
GS
2158 /* try to smush double to int, but don't smush -2.0 to -2 */
2159 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2160 type != OP_NEGATE)
2161 {
748a9306 2162 IV iv = SvIV(sv);
65202027 2163 if ((NV)iv == SvNV(sv)) {
748a9306
LW
2164 SvREFCNT_dec(sv);
2165 sv = newSViv(iv);
2166 }
b1cb66bf 2167 else
2168 SvIOK_off(sv); /* undo SvIV() damage */
748a9306
LW
2169 }
2170 return newSVOP(OP_CONST, 0, sv);
2171 }
aeea060c 2172
79072805 2173 nope:
22c35a8c 2174 if (!(PL_opargs[type] & OA_OTHERINT))
79072805 2175 return o;
79072805 2176
3280af22 2177 if (!(PL_hints & HINT_INTEGER)) {
4bb9f687
GS
2178 if (type == OP_MODULO
2179 || type == OP_DIVIDE
2180 || !(o->op_flags & OPf_KIDS))
2181 {
85e6fe83 2182 return o;
4bb9f687 2183 }
85e6fe83
LW
2184
2185 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2186 if (curop->op_type == OP_CONST) {
b1cb66bf 2187 if (SvIOK(((SVOP*)curop)->op_sv))
85e6fe83
LW
2188 continue;
2189 return o;
2190 }
22c35a8c 2191 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
79072805
LW
2192 continue;
2193 return o;
2194 }
22c35a8c 2195 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
79072805
LW
2196 }
2197
79072805
LW
2198 return o;
2199}
2200
2201OP *
864dbfa3 2202Perl_gen_constant_list(pTHX_ register OP *o)
79072805 2203{
11343788 2204 dTHR;
79072805 2205 register OP *curop;
3280af22 2206 I32 oldtmps_floor = PL_tmps_floor;
79072805 2207
a0d0e21e 2208 list(o);
3280af22 2209 if (PL_error_count)
a0d0e21e
LW
2210 return o; /* Don't attempt to run with errors */
2211
533c011a 2212 PL_op = curop = LINKLIST(o);
a0d0e21e 2213 o->op_next = 0;
cea2e8a9
GS
2214 pp_pushmark();
2215 CALLRUNOPS(aTHX);
533c011a 2216 PL_op = curop;
cea2e8a9 2217 pp_anonlist();
3280af22 2218 PL_tmps_floor = oldtmps_floor;
79072805
LW
2219
2220 o->op_type = OP_RV2AV;
22c35a8c 2221 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
79072805 2222 curop = ((UNOP*)o)->op_first;
3280af22 2223 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
79072805 2224 op_free(curop);
79072805
LW
2225 linklist(o);
2226 return list(o);
2227}
2228
2229OP *
864dbfa3 2230Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
79072805
LW
2231{
2232 OP *kid;
a0d0e21e 2233 OP *last = 0;
79072805 2234
11343788
MB
2235 if (!o || o->op_type != OP_LIST)
2236 o = newLISTOP(OP_LIST, 0, o, Nullop);
748a9306 2237 else
5dc0d613 2238 o->op_flags &= ~OPf_WANT;
79072805 2239
22c35a8c 2240 if (!(PL_opargs[type] & OA_MARK))
11343788 2241 null(cLISTOPo->op_first);
8990e307 2242
11343788 2243 o->op_type = type;
22c35a8c 2244 o->op_ppaddr = PL_ppaddr[type];
11343788 2245 o->op_flags |= flags;
79072805 2246
11343788
MB
2247 o = CHECKOP(type, o);
2248 if (o->op_type != type)
2249 return o;
79072805 2250
11343788 2251 if (cLISTOPo->op_children < 7) {
79072805 2252 /* XXX do we really need to do this if we're done appending?? */
11343788 2253 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805 2254 last = kid;
11343788 2255 cLISTOPo->op_last = last; /* in case check substituted last arg */
79072805
LW
2256 }
2257
11343788 2258 return fold_constants(o);
79072805
LW
2259}
2260
2261/* List constructors */
2262
2263OP *
864dbfa3 2264Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2265{
2266 if (!first)
2267 return last;
8990e307
LW
2268
2269 if (!last)
79072805 2270 return first;
8990e307 2271
a0d0e21e
LW
2272 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2273 return newLISTOP(type, 0, first, last);
79072805 2274
a0d0e21e
LW
2275 if (first->op_flags & OPf_KIDS)
2276 ((LISTOP*)first)->op_last->op_sibling = last;
2277 else {
2278 first->op_flags |= OPf_KIDS;
2279 ((LISTOP*)first)->op_first = last;
2280 }
2281 ((LISTOP*)first)->op_last = last;
2282 ((LISTOP*)first)->op_children++;
2283 return first;
79072805
LW
2284}
2285
2286OP *
864dbfa3 2287Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
79072805
LW
2288{
2289 if (!first)
2290 return (OP*)last;
8990e307
LW
2291
2292 if (!last)
79072805 2293 return (OP*)first;
8990e307
LW
2294
2295 if (first->op_type != type)
79072805 2296 return prepend_elem(type, (OP*)first, (OP*)last);
8990e307
LW
2297
2298 if (last->op_type != type)
79072805
LW
2299 return append_elem(type, (OP*)first, (OP*)last);
2300
2301 first->op_last->op_sibling = last->op_first;
2302 first->op_last = last->op_last;
2303 first->op_children += last->op_children;
2304 if (first->op_children)
acb74605 2305 first->op_flags |= OPf_KIDS;
b7dc083c
NIS
2306
2307#ifdef PL_OP_SLAB_ALLOC
2308#else
2309 Safefree(last);
2310#endif
79072805
LW
2311 return (OP*)first;
2312}
2313
2314OP *
864dbfa3 2315Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2316{
2317 if (!first)
2318 return last;
8990e307
LW
2319
2320 if (!last)
79072805 2321 return first;
8990e307
LW
2322
2323 if (last->op_type == type) {
2324 if (type == OP_LIST) { /* already a PUSHMARK there */
2325 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2326 ((LISTOP*)last)->op_first->op_sibling = first;
2327 }
2328 else {
2329 if (!(last->op_flags & OPf_KIDS)) {
2330 ((LISTOP*)last)->op_last = first;
2331 last->op_flags |= OPf_KIDS;
2332 }
2333 first->op_sibling = ((LISTOP*)last)->op_first;
2334 ((LISTOP*)last)->op_first = first;
79072805 2335 }
79072805
LW
2336 ((LISTOP*)last)->op_children++;
2337 return last;
2338 }
2339
2340 return newLISTOP(type, 0, first, last);
2341}
2342
2343/* Constructors */
2344
2345OP *
864dbfa3 2346Perl_newNULLLIST(pTHX)
79072805 2347{
8990e307
LW
2348 return newOP(OP_STUB, 0);
2349}
2350
2351OP *
864dbfa3 2352Perl_force_list(pTHX_ OP *o)
8990e307 2353{
11343788
MB
2354 if (!o || o->op_type != OP_LIST)
2355 o = newLISTOP(OP_LIST, 0, o, Nullop);
2356 null(o);
2357 return o;
79072805
LW
2358}
2359
2360OP *
864dbfa3 2361Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2362{
2363 LISTOP *listop;
2364
b7dc083c 2365 NewOp(1101, listop, 1, LISTOP);
79072805
LW
2366
2367 listop->op_type = type;
22c35a8c 2368 listop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2369 listop->op_children = (first != 0) + (last != 0);
2370 listop->op_flags = flags;
79072805
LW
2371
2372 if (!last && first)
2373 last = first;
2374 else if (!first && last)
2375 first = last;
8990e307
LW
2376 else if (first)
2377 first->op_sibling = last;
79072805
LW
2378 listop->op_first = first;
2379 listop->op_last = last;
8990e307
LW
2380 if (type == OP_LIST) {
2381 OP* pushop;
2382 pushop = newOP(OP_PUSHMARK, 0);
2383 pushop->op_sibling = first;
2384 listop->op_first = pushop;
2385 listop->op_flags |= OPf_KIDS;
2386 if (!last)
2387 listop->op_last = pushop;
2388 }
2389 else if (listop->op_children)
2390 listop->op_flags |= OPf_KIDS;
79072805
LW
2391
2392 return (OP*)listop;
2393}
2394
2395OP *
864dbfa3 2396Perl_newOP(pTHX_ I32 type, I32 flags)
79072805 2397{
11343788 2398 OP *o;
b7dc083c 2399 NewOp(1101, o, 1, OP);
11343788 2400 o->op_type = type;
22c35a8c 2401 o->op_ppaddr = PL_ppaddr[type];
11343788 2402 o->op_flags = flags;
79072805 2403
11343788
MB
2404 o->op_next = o;
2405 o->op_private = 0 + (flags >> 8);
22c35a8c 2406 if (PL_opargs[type] & OA_RETSCALAR)
11343788 2407 scalar(o);
22c35a8c 2408 if (PL_opargs[type] & OA_TARGET)
11343788
MB
2409 o->op_targ = pad_alloc(type, SVs_PADTMP);
2410 return CHECKOP(type, o);
79072805
LW
2411}
2412
2413OP *
864dbfa3 2414Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
79072805
LW
2415{
2416 UNOP *unop;
2417
93a17b20 2418 if (!first)
aeea060c 2419 first = newOP(OP_STUB, 0);
22c35a8c 2420 if (PL_opargs[type] & OA_MARK)
8990e307 2421 first = force_list(first);
93a17b20 2422
b7dc083c 2423 NewOp(1101, unop, 1, UNOP);
79072805 2424 unop->op_type = type;
22c35a8c 2425 unop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2426 unop->op_first = first;
2427 unop->op_flags = flags | OPf_KIDS;
c07a80fd 2428 unop->op_private = 1 | (flags >> 8);
e50aee73 2429 unop = (UNOP*) CHECKOP(type, unop);
79072805
LW
2430 if (unop->op_next)
2431 return (OP*)unop;
2432
a0d0e21e 2433 return fold_constants((OP *) unop);
79072805
LW
2434}
2435
2436OP *
864dbfa3 2437Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2438{
2439 BINOP *binop;
b7dc083c 2440 NewOp(1101, binop, 1, BINOP);
79072805
LW
2441
2442 if (!first)
2443 first = newOP(OP_NULL, 0);
2444
2445 binop->op_type = type;
22c35a8c 2446 binop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2447 binop->op_first = first;
2448 binop->op_flags = flags | OPf_KIDS;
2449 if (!last) {
2450 last = first;
c07a80fd 2451 binop->op_private = 1 | (flags >> 8);
79072805
LW
2452 }
2453 else {
c07a80fd 2454 binop->op_private = 2 | (flags >> 8);
79072805
LW
2455 first->op_sibling = last;
2456 }
2457
e50aee73 2458 binop = (BINOP*)CHECKOP(type, binop);
b162f9ea 2459 if (binop->op_next || binop->op_type != type)
79072805
LW
2460 return (OP*)binop;
2461
7284ab6f 2462 binop->op_last = binop->op_first->op_sibling;
79072805 2463
a0d0e21e 2464 return fold_constants((OP *)binop);
79072805
LW
2465}
2466
a0ed51b3
LW
2467static int
2468utf8compare(const void *a, const void *b)
2469{
2470 int i;
2471 for (i = 0; i < 10; i++) {
2472 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2473 return -1;
2474 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2475 return 1;
2476 }
2477 return 0;
2478}
2479
79072805 2480OP *
864dbfa3 2481Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2482{
79072805
LW
2483 SV *tstr = ((SVOP*)expr)->op_sv;
2484 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
2485 STRLEN tlen;
2486 STRLEN rlen;
ec49126f 2487 register U8 *t = (U8*)SvPV(tstr, tlen);
2488 register U8 *r = (U8*)SvPV(rstr, rlen);
79072805
LW
2489 register I32 i;
2490 register I32 j;
a0ed51b3 2491 I32 del;
79072805 2492 I32 complement;
5d06d08e 2493 I32 squash;
79072805
LW
2494 register short *tbl;
2495
11343788 2496 complement = o->op_private & OPpTRANS_COMPLEMENT;
a0ed51b3 2497 del = o->op_private & OPpTRANS_DELETE;
5d06d08e 2498 squash = o->op_private & OPpTRANS_SQUASH;
79072805 2499
a0ed51b3 2500 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
79cb57f6 2501 SV* listsv = newSVpvn("# comment\n",10);
a0ed51b3
LW
2502 SV* transv = 0;
2503 U8* tend = t + tlen;
2504 U8* rend = r + rlen;
2505 I32 ulen;
2506 U32 tfirst = 1;
2507 U32 tlast = 0;
2508 I32 tdiff;
2509 U32 rfirst = 1;
2510 U32 rlast = 0;
2511 I32 rdiff;
2512 I32 diff;
2513 I32 none = 0;
2514 U32 max = 0;
2515 I32 bits;
2516 I32 grows = 0;
2517 I32 havefinal = 0;
2518 U32 final;
2519 HV *hv;
2520 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2521 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2522
2523 if (complement) {
2524 U8 tmpbuf[10];
2525 U8** cp;
2526 UV nextmin = 0;
2527 New(1109, cp, tlen, U8*);
2528 i = 0;
79cb57f6 2529 transv = newSVpvn("",0);
a0ed51b3
LW
2530 while (t < tend) {
2531 cp[i++] = t;
2532 t += UTF8SKIP(t);
2533 if (*t == 0xff) {
2534 t++;
2535 t += UTF8SKIP(t);
2536 }
2537 }
2538 qsort(cp, i, sizeof(U8*), utf8compare);
2539 for (j = 0; j < i; j++) {
2540 U8 *s = cp[j];
2541 UV val = utf8_to_uv(s, &ulen);
2542 s += ulen;
2543 diff = val - nextmin;
2544 if (diff > 0) {
2545 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2546 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2547 if (diff > 1) {
2548 t = uv_to_utf8(tmpbuf, val - 1);
2549 sv_catpvn(transv, "\377", 1);
dfe13c55 2550 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2551 }
2552 }
2553 if (*s == 0xff)
2554 val = utf8_to_uv(s+1, &ulen);
2555 if (val >= nextmin)
2556 nextmin = val + 1;
2557 }
2558 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2559 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2560 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2561 sv_catpvn(transv, "\377", 1);
dfe13c55
GS
2562 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2563 t = (U8*)SvPVX(transv);
a0ed51b3
LW
2564 tlen = SvCUR(transv);
2565 tend = t + tlen;
2566 }
2567 else if (!rlen && !del) {
2568 r = t; rlen = tlen; rend = tend;
4757a243
LW
2569 }
2570 if (!squash) {
2571 if (to_utf && from_utf) { /* only counting characters */
2572 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2573 o->op_private |= OPpTRANS_IDENTICAL;
2574 }
2575 else { /* straight latin-1 translation */
2576 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2577 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2578 o->op_private |= OPpTRANS_IDENTICAL;
2579 }
a0ed51b3
LW
2580 }
2581
2582 while (t < tend || tfirst <= tlast) {
2583 /* see if we need more "t" chars */
2584 if (tfirst > tlast) {
2585 tfirst = (I32)utf8_to_uv(t, &ulen);
2586 t += ulen;
2587 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2588 tlast = (I32)utf8_to_uv(++t, &ulen);
2589 t += ulen;
2590 }
2591 else
2592 tlast = tfirst;
2593 }
2594
2595 /* now see if we need more "r" chars */
2596 if (rfirst > rlast) {
2597 if (r < rend) {
2598 rfirst = (I32)utf8_to_uv(r, &ulen);
2599 r += ulen;
2600 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2601 rlast = (I32)utf8_to_uv(++r, &ulen);
2602 r += ulen;
2603 }
2604 else
2605 rlast = rfirst;
2606 }
2607 else {
2608 if (!havefinal++)
2609 final = rlast;
2610 rfirst = rlast = 0xffffffff;
2611 }
2612 }
2613
2614 /* now see which range will peter our first, if either. */
2615 tdiff = tlast - tfirst;
2616 rdiff = rlast - rfirst;
2617
2618 if (tdiff <= rdiff)
2619 diff = tdiff;
2620 else
2621 diff = rdiff;
2622
2623 if (rfirst == 0xffffffff) {
2624 diff = tdiff; /* oops, pretend rdiff is infinite */
2625 if (diff > 0)
cea2e8a9 2626 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\tXXXX\n", tfirst, tlast);
a0ed51b3 2627 else
cea2e8a9 2628 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\tXXXX\n", tfirst);
a0ed51b3
LW
2629 }
2630 else {
2631 if (diff > 0)
cea2e8a9 2632 Perl_sv_catpvf(aTHX_ listsv, "%04x\t%04x\t%04x\n", tfirst, tfirst + diff, rfirst);
a0ed51b3 2633 else
cea2e8a9 2634 Perl_sv_catpvf(aTHX_ listsv, "%04x\t\t%04x\n", tfirst, rfirst);
a0ed51b3
LW
2635
2636 if (rfirst + diff > max)
2637 max = rfirst + diff;
2638 rfirst += diff + 1;
2639 if (!grows) {
2640 if (rfirst <= 0x80)
2641 ;
2642 else if (rfirst <= 0x800)
2643 grows |= (tfirst < 0x80);
2644 else if (rfirst <= 0x10000)
2645 grows |= (tfirst < 0x800);
2646 else if (rfirst <= 0x200000)
2647 grows |= (tfirst < 0x10000);
2648 else if (rfirst <= 0x4000000)
2649 grows |= (tfirst < 0x200000);
2650 else if (rfirst <= 0x80000000)
2651 grows |= (tfirst < 0x4000000);
2652 }
2653 }
2654 tfirst += diff + 1;
2655 }
2656
2657 none = ++max;
2658 if (del)
2659 del = ++max;
2660
2661 if (max > 0xffff)
2662 bits = 32;
2663 else if (max > 0xff)
2664 bits = 16;
2665 else
2666 bits = 8;
2667
2668 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2669 SvREFCNT_dec(listsv);
2670 if (transv)
2671 SvREFCNT_dec(transv);
2672
2673 if (!del && havefinal)
2674 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2675
2676 if (grows && to_utf)
2677 o->op_private |= OPpTRANS_GROWS;
2678
2679 op_free(expr);
2680 op_free(repl);
2681 return o;
2682 }
2683
2684 tbl = (short*)cPVOPo->op_pv;
79072805
LW
2685 if (complement) {
2686 Zero(tbl, 256, short);
2687 for (i = 0; i < tlen; i++)
ec49126f 2688 tbl[t[i]] = -1;
79072805
LW
2689 for (i = 0, j = 0; i < 256; i++) {
2690 if (!tbl[i]) {
2691 if (j >= rlen) {
a0ed51b3 2692 if (del)
79072805
LW
2693 tbl[i] = -2;
2694 else if (rlen)
ec49126f 2695 tbl[i] = r[j-1];
79072805
LW
2696 else
2697 tbl[i] = i;
2698 }
2699 else
ec49126f 2700 tbl[i] = r[j++];
79072805
LW
2701 }
2702 }
2703 }
2704 else {
a0ed51b3 2705 if (!rlen && !del) {
79072805 2706 r = t; rlen = tlen;
5d06d08e 2707 if (!squash)
4757a243 2708 o->op_private |= OPpTRANS_IDENTICAL;
79072805
LW
2709 }
2710 for (i = 0; i < 256; i++)
2711 tbl[i] = -1;
2712 for (i = 0, j = 0; i < tlen; i++,j++) {
2713 if (j >= rlen) {
a0ed51b3 2714 if (del) {
ec49126f 2715 if (tbl[t[i]] == -1)
2716 tbl[t[i]] = -2;
79072805
LW
2717 continue;
2718 }
2719 --j;
2720 }
ec49126f 2721 if (tbl[t[i]] == -1)
2722 tbl[t[i]] = r[j];
79072805
LW
2723 }
2724 }
2725 op_free(expr);
2726 op_free(repl);
2727
11343788 2728 return o;
79072805
LW
2729}
2730
2731OP *
864dbfa3 2732Perl_newPMOP(pTHX_ I32 type, I32 flags)
79072805 2733{
11343788 2734 dTHR;
79072805
LW
2735 PMOP *pmop;
2736
b7dc083c 2737 NewOp(1101, pmop, 1, PMOP);
79072805 2738 pmop->op_type = type;
22c35a8c 2739 pmop->op_ppaddr = PL_ppaddr[type];
79072805 2740 pmop->op_flags = flags;
c07a80fd 2741 pmop->op_private = 0 | (flags >> 8);
79072805 2742
3280af22 2743 if (PL_hints & HINT_RE_TAINT)
b3eb6a9b 2744 pmop->op_pmpermflags |= PMf_RETAINT;
3280af22 2745 if (PL_hints & HINT_LOCALE)
b3eb6a9b
GS
2746 pmop->op_pmpermflags |= PMf_LOCALE;
2747 pmop->op_pmflags = pmop->op_pmpermflags;
36477c24 2748
79072805 2749 /* link into pm list */
3280af22
NIS
2750 if (type != OP_TRANS && PL_curstash) {
2751 pmop->op_pmnext = HvPMROOT(PL_curstash);
2752 HvPMROOT(PL_curstash) = pmop;
79072805
LW
2753 }
2754
2755 return (OP*)pmop;
2756}
2757
2758OP *
864dbfa3 2759Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2760{
5c0ca799 2761 dTHR;
79072805
LW
2762 PMOP *pm;
2763 LOGOP *rcop;
ce862d02 2764 I32 repl_has_vars = 0;
79072805 2765
11343788
MB
2766 if (o->op_type == OP_TRANS)
2767 return pmtrans(o, expr, repl);
79072805 2768
3280af22 2769 PL_hints |= HINT_BLOCK_SCOPE;
11343788 2770 pm = (PMOP*)o;
79072805
LW
2771
2772 if (expr->op_type == OP_CONST) {
463ee0b2 2773 STRLEN plen;
79072805 2774 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 2775 char *p = SvPV(pat, plen);
11343788 2776 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 2777 sv_setpvn(pat, "\\s+", 3);
463ee0b2 2778 p = SvPV(pat, plen);
79072805
LW
2779 pm->op_pmflags |= PMf_SKIPWHITE;
2780 }
cea2e8a9 2781 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
aeea060c 2782 if (strEQ("\\s+", pm->op_pmregexp->precomp))
85e6fe83 2783 pm->op_pmflags |= PMf_WHITE;
79072805
LW
2784 op_free(expr);
2785 }
2786 else {
3280af22
NIS
2787 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2788 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2789 ? OP_REGCRESET
2790 : OP_REGCMAYBE),0,expr);
463ee0b2 2791
b7dc083c 2792 NewOp(1101, rcop, 1, LOGOP);
79072805 2793 rcop->op_type = OP_REGCOMP;
22c35a8c 2794 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
79072805 2795 rcop->op_first = scalar(expr);
3280af22 2796 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2797 ? (OPf_SPECIAL | OPf_KIDS)
2798 : OPf_KIDS);
79072805 2799 rcop->op_private = 1;
11343788 2800 rcop->op_other = o;
79072805
LW
2801
2802 /* establish postfix order */
3280af22 2803 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
463ee0b2
LW
2804 LINKLIST(expr);
2805 rcop->op_next = expr;
2806 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2807 }
2808 else {
2809 rcop->op_next = LINKLIST(expr);
2810 expr->op_next = (OP*)rcop;
2811 }
79072805 2812
11343788 2813 prepend_elem(o->op_type, scalar((OP*)rcop), o);
79072805
LW
2814 }
2815
2816 if (repl) {
748a9306 2817 OP *curop;
0244c3a4 2818 if (pm->op_pmflags & PMf_EVAL) {
748a9306 2819 curop = 0;
57843af0
GS
2820 if (CopLINE(PL_curcop) < PL_multi_end)
2821 CopLINE_set(PL_curcop, PL_multi_end);
0244c3a4 2822 }
554b3eca 2823#ifdef USE_THREADS
2faa37cc 2824 else if (repl->op_type == OP_THREADSV
554b3eca 2825 && strchr("&`'123456789+",
533c011a 2826 PL_threadsv_names[repl->op_targ]))
554b3eca
MB
2827 {
2828 curop = 0;
2829 }
2830#endif /* USE_THREADS */
748a9306
LW
2831 else if (repl->op_type == OP_CONST)
2832 curop = repl;
79072805 2833 else {
79072805
LW
2834 OP *lastop = 0;
2835 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
22c35a8c 2836 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
554b3eca 2837#ifdef USE_THREADS
ce862d02
IZ
2838 if (curop->op_type == OP_THREADSV) {
2839 repl_has_vars = 1;
be949f6f 2840 if (strchr("&`'123456789+", curop->op_private))
ce862d02 2841 break;
554b3eca
MB
2842 }
2843#else
79072805 2844 if (curop->op_type == OP_GV) {
350de78d 2845 GV *gv = cGVOPx(curop);
ce862d02 2846 repl_has_vars = 1;
93a17b20 2847 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
2848 break;
2849 }
554b3eca 2850#endif /* USE_THREADS */
79072805
LW
2851 else if (curop->op_type == OP_RV2CV)
2852 break;
2853 else if (curop->op_type == OP_RV2SV ||
2854 curop->op_type == OP_RV2AV ||
2855 curop->op_type == OP_RV2HV ||
2856 curop->op_type == OP_RV2GV) {
2857 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2858 break;
2859 }
748a9306
LW
2860 else if (curop->op_type == OP_PADSV ||
2861 curop->op_type == OP_PADAV ||
2862 curop->op_type == OP_PADHV ||
554b3eca 2863 curop->op_type == OP_PADANY) {
ce862d02 2864 repl_has_vars = 1;
748a9306 2865 }
1167e5da
SM
2866 else if (curop->op_type == OP_PUSHRE)
2867 ; /* Okay here, dangerous in newASSIGNOP */
79072805
LW
2868 else
2869 break;
2870 }
2871 lastop = curop;
2872 }
748a9306 2873 }
ce862d02
IZ
2874 if (curop == repl
2875 && !(repl_has_vars
2876 && (!pm->op_pmregexp
2877 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
748a9306 2878 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4633a7c4 2879 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
11343788 2880 prepend_elem(o->op_type, scalar(repl), o);
748a9306
LW
2881 }
2882 else {
ce862d02
IZ
2883 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2884 pm->op_pmflags |= PMf_MAYBE_CONST;
2885 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2886 }
b7dc083c 2887 NewOp(1101, rcop, 1, LOGOP);
748a9306 2888 rcop->op_type = OP_SUBSTCONT;
22c35a8c 2889 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
748a9306
LW
2890 rcop->op_first = scalar(repl);
2891 rcop->op_flags |= OPf_KIDS;
2892 rcop->op_private = 1;
11343788 2893 rcop->op_other = o;
748a9306
LW
2894
2895 /* establish postfix order */
2896 rcop->op_next = LINKLIST(repl);
2897 repl->op_next = (OP*)rcop;
2898
2899 pm->op_pmreplroot = scalar((OP*)rcop);
2900 pm->op_pmreplstart = LINKLIST(rcop);
2901 rcop->op_next = 0;
79072805
LW
2902 }
2903 }
2904
2905 return (OP*)pm;
2906}
2907
2908OP *
864dbfa3 2909Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
79072805
LW
2910{
2911 SVOP *svop;
b7dc083c 2912 NewOp(1101, svop, 1, SVOP);
79072805 2913 svop->op_type = type;
22c35a8c 2914 svop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2915 svop->op_sv = sv;
2916 svop->op_next = (OP*)svop;
2917 svop->op_flags = flags;
22c35a8c 2918 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2919 scalar((OP*)svop);
22c35a8c 2920 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2921 svop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2922 return CHECKOP(type, svop);
79072805
LW
2923}
2924
2925OP *
350de78d
GS
2926Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
2927{
2928 PADOP *padop;
2929 NewOp(1101, padop, 1, PADOP);
2930 padop->op_type = type;
2931 padop->op_ppaddr = PL_ppaddr[type];
2932 padop->op_padix = pad_alloc(type, SVs_PADTMP);
2933 PL_curpad[padop->op_padix] = sv;
2934 padop->op_next = (OP*)padop;
2935 padop->op_flags = flags;
2936 if (PL_opargs[type] & OA_RETSCALAR)
2937 scalar((OP*)padop);
2938 if (PL_opargs[type] & OA_TARGET)
2939 padop->op_targ = pad_alloc(type, SVs_PADTMP);
2940 return CHECKOP(type, padop);
2941}
2942
2943OP *
864dbfa3 2944Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
79072805 2945{
11343788 2946 dTHR;
350de78d 2947#ifdef USE_ITHREADS
743e66e6 2948 GvIN_PAD_on(gv);
350de78d
GS
2949 return newPADOP(type, flags, SvREFCNT_inc(gv));
2950#else
7934575e 2951 return newSVOP(type, flags, SvREFCNT_inc(gv));
350de78d 2952#endif
79072805
LW
2953}
2954
2955OP *
864dbfa3 2956Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
79072805
LW
2957{
2958 PVOP *pvop;
b7dc083c 2959 NewOp(1101, pvop, 1, PVOP);
79072805 2960 pvop->op_type = type;
22c35a8c 2961 pvop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2962 pvop->op_pv = pv;
2963 pvop->op_next = (OP*)pvop;
2964 pvop->op_flags = flags;
22c35a8c 2965 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 2966 scalar((OP*)pvop);
22c35a8c 2967 if (PL_opargs[type] & OA_TARGET)
ed6116ce 2968 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2969 return CHECKOP(type, pvop);
79072805
LW
2970}
2971
79072805 2972void
864dbfa3 2973Perl_package(pTHX_ OP *o)
79072805 2974{
11343788 2975 dTHR;
93a17b20 2976 SV *sv;
79072805 2977
3280af22
NIS
2978 save_hptr(&PL_curstash);
2979 save_item(PL_curstname);
11343788 2980 if (o) {
463ee0b2
LW
2981 STRLEN len;
2982 char *name;
11343788 2983 sv = cSVOPo->op_sv;
463ee0b2 2984 name = SvPV(sv, len);
3280af22
NIS
2985 PL_curstash = gv_stashpvn(name,len,TRUE);
2986 sv_setpvn(PL_curstname, name, len);
11343788 2987 op_free(o);
93a17b20
LW
2988 }
2989 else {
3280af22
NIS
2990 sv_setpv(PL_curstname,"<none>");
2991 PL_curstash = Nullhv;
93a17b20 2992 }
7ad382f4 2993 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
2994 PL_copline = NOLINE;
2995 PL_expect = XSTATE;
79072805
LW
2996}
2997
85e6fe83 2998void
864dbfa3 2999Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
85e6fe83 3000{
a0d0e21e
LW
3001 OP *pack;
3002 OP *meth;
3003 OP *rqop;
3004 OP *imop;
b1cb66bf 3005 OP *veop;
78ca652e 3006 GV *gv;
85e6fe83 3007
a0d0e21e 3008 if (id->op_type != OP_CONST)
cea2e8a9 3009 Perl_croak(aTHX_ "Module name must be constant");
85e6fe83 3010
b1cb66bf 3011 veop = Nullop;
3012
3013 if(version != Nullop) {
3014 SV *vesv = ((SVOP*)version)->op_sv;
3015
3016 if (arg == Nullop && !SvNIOK(vesv)) {
3017 arg = version;
3018 }
3019 else {
3020 OP *pack;
b1cb66bf 3021
3022 if (version->op_type != OP_CONST || !SvNIOK(vesv))
cea2e8a9 3023 Perl_croak(aTHX_ "Version number must be constant number");
b1cb66bf 3024
3025 /* Make copy of id so we don't free it twice */
3026 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3027
3028 /* Fake up a method call to VERSION */
b1cb66bf 3029 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3030 append_elem(OP_LIST,
3031 prepend_elem(OP_LIST, pack, list(version)),
f5d5a27c
CS
3032 newSVOP(OP_METHOD_NAMED, 0,
3033 newSVpvn("VERSION", 7))));
b1cb66bf 3034 }
3035 }
aeea060c 3036
a0d0e21e 3037 /* Fake up an import/unimport */
4633a7c4
LW
3038 if (arg && arg->op_type == OP_STUB)
3039 imop = arg; /* no import on explicit () */
b1cb66bf 3040 else if(SvNIOK(((SVOP*)id)->op_sv)) {
3041 imop = Nullop; /* use 5.0; */
3042 }
4633a7c4
LW
3043 else {
3044 /* Make copy of id so we don't free it twice */
3045 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
4633a7c4 3046 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
a0d0e21e
LW
3047 append_elem(OP_LIST,
3048 prepend_elem(OP_LIST, pack, list(arg)),
f5d5a27c
CS
3049 newSVOP(OP_METHOD_NAMED, 0,
3050 aver ? newSVpvn("import", 6)
3051 : newSVpvn("unimport", 8))));
4633a7c4
LW
3052 }
3053
78ca652e
GS
3054 /* Fake up a require, handle override, if any */
3055 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3056 if (!(gv && GvIMPORTED_CV(gv)))
3057 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3058
3059 if (gv && GvIMPORTED_CV(gv)) {
3060 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3061 append_elem(OP_LIST, id,
3062 scalar(newUNOP(OP_RV2CV, 0,
3063 newGVOP(OP_GV, 0,
3064 gv))))));
3065 }
3066 else {
3067 rqop = newUNOP(OP_REQUIRE, 0, id);
3068 }
a0d0e21e
LW
3069
3070 /* Fake up the BEGIN {}, which does its thing immediately. */
09bef843 3071 newATTRSUB(floor,
79cb57f6 3072 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
4633a7c4 3073 Nullop,
09bef843 3074 Nullop,
a0d0e21e 3075 append_elem(OP_LINESEQ,
b1cb66bf 3076 append_elem(OP_LINESEQ,
3077 newSTATEOP(0, Nullch, rqop),
3078 newSTATEOP(0, Nullch, veop)),
a0d0e21e 3079 newSTATEOP(0, Nullch, imop) ));
85e6fe83 3080
c305c6a0 3081 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
3082 PL_copline = NOLINE;
3083 PL_expect = XSTATE;
85e6fe83
LW
3084}
3085
79072805 3086OP *
864dbfa3 3087Perl_dofile(pTHX_ OP *term)
78ca652e
GS
3088{
3089 OP *doop;
3090 GV *gv;
3091
3092 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3093 if (!(gv && GvIMPORTED_CV(gv)))
3094 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3095
3096 if (gv && GvIMPORTED_CV(gv)) {
3097 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3098 append_elem(OP_LIST, term,
3099 scalar(newUNOP(OP_RV2CV, 0,
3100 newGVOP(OP_GV, 0,
3101 gv))))));
3102 }
3103 else {
3104 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3105 }
3106 return doop;
3107}
3108
3109OP *
864dbfa3 3110Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
79072805
LW
3111{
3112 return newBINOP(OP_LSLICE, flags,
8990e307
LW
3113 list(force_list(subscript)),
3114 list(force_list(listval)) );
79072805
LW
3115}
3116
76e3520e 3117STATIC I32
cea2e8a9 3118S_list_assignment(pTHX_ register OP *o)
79072805 3119{
11343788 3120 if (!o)
79072805
LW
3121 return TRUE;
3122
11343788
MB
3123 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3124 o = cUNOPo->op_first;
79072805 3125
11343788 3126 if (o->op_type == OP_COND_EXPR) {
1a67a97c
SM
3127 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3128 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
79072805
LW
3129
3130 if (t && f)
3131 return TRUE;
3132 if (t || f)
3133 yyerror("Assignment to both a list and a scalar");
3134 return FALSE;
3135 }
3136
11343788
MB
3137 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3138 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3139 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
79072805
LW
3140 return TRUE;
3141
11343788 3142 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
93a17b20
LW
3143 return TRUE;
3144
11343788 3145 if (o->op_type == OP_RV2SV)
79072805
LW
3146 return FALSE;
3147
3148 return FALSE;
3149}
3150
3151OP *
864dbfa3 3152Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
79072805 3153{
11343788 3154 OP *o;
79072805 3155
a0d0e21e
LW
3156 if (optype) {
3157 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3158 return newLOGOP(optype, 0,
3159 mod(scalar(left), optype),
3160 newUNOP(OP_SASSIGN, 0, scalar(right)));
3161 }
3162 else {
3163 return newBINOP(optype, OPf_STACKED,
3164 mod(scalar(left), optype), scalar(right));
3165 }
3166 }
3167
79072805 3168 if (list_assignment(left)) {
6ee623d5 3169 dTHR;
3280af22
NIS
3170 PL_modcount = 0;
3171 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
463ee0b2 3172 left = mod(left, OP_AASSIGN);
3280af22
NIS
3173 if (PL_eval_start)
3174 PL_eval_start = 0;
748a9306 3175 else {
a0d0e21e
LW
3176 op_free(left);
3177 op_free(right);
3178 return Nullop;
3179 }
11343788 3180 o = newBINOP(OP_AASSIGN, flags,
8990e307
LW
3181 list(force_list(right)),
3182 list(force_list(left)) );
11343788 3183 o->op_private = 0 | (flags >> 8);
a0d0e21e 3184 if (!(left->op_private & OPpLVAL_INTRO)) {
79072805 3185 OP *curop;
11343788 3186 OP *lastop = o;
3280af22 3187 PL_generation++;
11343788 3188 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
22c35a8c 3189 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
79072805 3190 if (curop->op_type == OP_GV) {
350de78d 3191 GV *gv = cGVOPx(curop);
3280af22 3192 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
79072805 3193 break;
3280af22 3194 SvCUR(gv) = PL_generation;
79072805 3195 }
748a9306
LW
3196 else if (curop->op_type == OP_PADSV ||
3197 curop->op_type == OP_PADAV ||
3198 curop->op_type == OP_PADHV ||
3199 curop->op_type == OP_PADANY) {
3280af22 3200 SV **svp = AvARRAY(PL_comppad_name);
8e07c86e 3201 SV *sv = svp[curop->op_targ];
3280af22 3202 if (SvCUR(sv) == PL_generation)
748a9306 3203 break;
3280af22 3204 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
748a9306 3205 }
79072805
LW
3206 else if (curop->op_type == OP_RV2CV)
3207 break;
3208 else if (curop->op_type == OP_RV2SV ||
3209 curop->op_type == OP_RV2AV ||
3210 curop->op_type == OP_RV2HV ||
3211 curop->op_type == OP_RV2GV) {
3212 if (lastop->op_type != OP_GV) /* funny deref? */
3213 break;
3214 }
1167e5da
SM
3215 else if (curop->op_type == OP_PUSHRE) {
3216 if (((PMOP*)curop)->op_pmreplroot) {
3217 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3280af22 3218 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
1167e5da 3219 break;
3280af22 3220 SvCUR(gv) = PL_generation;
1167e5da
SM
3221 }
3222 }
79072805
LW
3223 else
3224 break;
3225 }
3226 lastop = curop;
3227 }
11343788
MB
3228 if (curop != o)
3229 o->op_private = OPpASSIGN_COMMON;
79072805 3230 }
c07a80fd 3231 if (right && right->op_type == OP_SPLIT) {
3232 OP* tmpop;
3233 if ((tmpop = ((LISTOP*)right)->op_first) &&
3234 tmpop->op_type == OP_PUSHRE)
3235 {
3236 PMOP *pm = (PMOP*)tmpop;
3237 if (left->op_type == OP_RV2AV &&
3238 !(left->op_private & OPpLVAL_INTRO) &&
11343788 3239 !(o->op_private & OPpASSIGN_COMMON) )
c07a80fd 3240 {
3241 tmpop = ((UNOP*)left)->op_first;
3242 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
743e66e6 3243 pm->op_pmreplroot = (OP*)cGVOPx(tmpop);
c07a80fd 3244 pm->op_pmflags |= PMf_ONCE;
11343788 3245 tmpop = cUNOPo->op_first; /* to list (nulled) */
c07a80fd 3246 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3247 tmpop->op_sibling = Nullop; /* don't free split */
3248 right->op_next = tmpop->op_next; /* fix starting loc */
11343788 3249 op_free(o); /* blow off assign */
54310121 3250 right->op_flags &= ~OPf_WANT;
a5f75d66 3251 /* "I don't know and I don't care." */
c07a80fd 3252 return right;
3253 }
3254 }
3255 else {
3280af22 3256 if (PL_modcount < 10000 &&
c07a80fd 3257 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3258 {
3259 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3260 if (SvIVX(sv) == 0)
3280af22 3261 sv_setiv(sv, PL_modcount+1);
c07a80fd 3262 }
3263 }
3264 }
3265 }
11343788 3266 return o;
79072805
LW
3267 }
3268 if (!right)
3269 right = newOP(OP_UNDEF, 0);
3270 if (right->op_type == OP_READLINE) {
3271 right->op_flags |= OPf_STACKED;
463ee0b2 3272 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805 3273 }
a0d0e21e 3274 else {
3280af22 3275 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
11343788 3276 o = newBINOP(OP_SASSIGN, flags,
463ee0b2 3277 scalar(right), mod(scalar(left), OP_SASSIGN) );
3280af22
NIS
3278 if (PL_eval_start)
3279 PL_eval_start = 0;
748a9306 3280 else {
11343788 3281 op_free(o);
a0d0e21e
LW
3282 return Nullop;
3283 }
3284 }
11343788 3285 return o;
79072805
LW
3286}
3287
3288OP *
864dbfa3 3289Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
79072805 3290{
11343788 3291 dTHR;
bbce6d69 3292 U32 seq = intro_my();
79072805
LW
3293 register COP *cop;
3294
b7dc083c 3295 NewOp(1101, cop, 1, COP);
57843af0 3296 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
8990e307 3297 cop->op_type = OP_DBSTATE;
22c35a8c 3298 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
8990e307
LW
3299 }
3300 else {
3301 cop->op_type = OP_NEXTSTATE;
22c35a8c 3302 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
8990e307 3303 }
79072805 3304 cop->op_flags = flags;
a0ed51b3 3305 cop->op_private = (PL_hints & HINT_UTF8);
ff0cee69 3306#ifdef NATIVE_HINTS
3307 cop->op_private |= NATIVE_HINTS;
3308#endif
e24b16f9 3309 PL_compiling.op_private = cop->op_private;
79072805
LW
3310 cop->op_next = (OP*)cop;
3311
463ee0b2
LW
3312 if (label) {
3313 cop->cop_label = label;
3280af22 3314 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 3315 }
bbce6d69 3316 cop->cop_seq = seq;
3280af22 3317 cop->cop_arybase = PL_curcop->cop_arybase;
0453d815 3318 if (specialWARN(PL_curcop->cop_warnings))
599cee73
PM
3319 cop->cop_warnings = PL_curcop->cop_warnings ;
3320 else
3321 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3322
79072805 3323
3280af22 3324 if (PL_copline == NOLINE)
57843af0 3325 CopLINE_set(cop, CopLINE(PL_curcop));
79072805 3326 else {
57843af0 3327 CopLINE_set(cop, PL_copline);
3280af22 3328 PL_copline = NOLINE;
79072805 3329 }
57843af0
GS
3330#ifdef USE_ITHREADS
3331 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXXXX share in a pvtable? */
3332#else
cc49e20b 3333 CopFILEGV_set(cop, (GV*)SvREFCNT_inc(CopFILEGV(PL_curcop)));
57843af0 3334#endif
11faa288 3335 CopSTASH_set(cop, PL_curstash);
79072805 3336
3280af22 3337 if (PERLDB_LINE && PL_curstash != PL_debstash) {
cc49e20b 3338 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3280af22 3339 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
a0d0e21e 3340 (void)SvIOK_on(*svp);
a5f75d66 3341 SvIVX(*svp) = 1;
93a17b20
LW
3342 SvSTASH(*svp) = (HV*)cop;
3343 }
3344 }
3345
11343788 3346 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
79072805
LW
3347}
3348
bbce6d69 3349/* "Introduce" my variables to visible status. */
3350U32
864dbfa3 3351Perl_intro_my(pTHX)
bbce6d69 3352{
3353 SV **svp;
3354 SV *sv;
3355 I32 i;
3356
3280af22
NIS
3357 if (! PL_min_intro_pending)
3358 return PL_cop_seqmax;
bbce6d69 3359
3280af22
NIS
3360 svp = AvARRAY(PL_comppad_name);
3361 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3362 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
c53d7c7d 3363 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
65202027 3364 SvNVX(sv) = (NV)PL_cop_seqmax;
bbce6d69 3365 }
3366 }
3280af22
NIS
3367 PL_min_intro_pending = 0;
3368 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3369 return PL_cop_seqmax++;
bbce6d69 3370}
3371
79072805 3372OP *
864dbfa3 3373Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
79072805 3374{
883ffac3
CS
3375 return new_logop(type, flags, &first, &other);
3376}
3377
3bd495df 3378STATIC OP *
cea2e8a9 3379S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
883ffac3 3380{
11343788 3381 dTHR;
79072805 3382 LOGOP *logop;
11343788 3383 OP *o;
883ffac3
CS
3384 OP *first = *firstp;
3385 OP *other = *otherp;
79072805 3386
a0d0e21e
LW
3387 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3388 return newBINOP(type, flags, scalar(first), scalar(other));
3389
8990e307 3390 scalarboolean(first);
79072805
LW
3391 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3392 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3393 if (type == OP_AND || type == OP_OR) {
3394 if (type == OP_AND)
3395 type = OP_OR;
3396 else
3397 type = OP_AND;
11343788 3398 o = first;
883ffac3 3399 first = *firstp = cUNOPo->op_first;
11343788
MB
3400 if (o->op_next)
3401 first->op_next = o->op_next;
3402 cUNOPo->op_first = Nullop;
3403 op_free(o);
79072805
LW
3404 }
3405 }
3406 if (first->op_type == OP_CONST) {
599cee73 3407 if (ckWARN(WARN_PRECEDENCE) && (first->op_private & OPpCONST_BARE))
cea2e8a9 3408 Perl_warner(aTHX_ WARN_PRECEDENCE, "Probable precedence problem on %s",
22c35a8c 3409 PL_op_desc[type]);
79072805
LW
3410 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3411 op_free(first);
883ffac3 3412 *firstp = Nullop;
79072805
LW
3413 return other;
3414 }
3415 else {
3416 op_free(other);
883ffac3 3417 *otherp = Nullop;
79072805
LW
3418 return first;
3419 }
3420 }
3421 else if (first->op_type == OP_WANTARRAY) {
3422 if (type == OP_AND)
3423 list(other);
3424 else
3425 scalar(other);
3426 }
599cee73 3427 else if (ckWARN(WARN_UNSAFE) && (first->op_flags & OPf_KIDS)) {
a6006777 3428 OP *k1 = ((UNOP*)first)->op_first;
3429 OP *k2 = k1->op_sibling;
3430 OPCODE warnop = 0;
3431 switch (first->op_type)
3432 {
3433 case OP_NULL:
3434 if (k2 && k2->op_type == OP_READLINE
3435 && (k2->op_flags & OPf_STACKED)
55d729e4 3436 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
72b16652 3437 {
a6006777 3438 warnop = k2->op_type;
72b16652 3439 }
a6006777 3440 break;
3441
3442 case OP_SASSIGN:
68dc0745 3443 if (k1->op_type == OP_READDIR
3444 || k1->op_type == OP_GLOB
72b16652 3445 || (k1->op_type == OP_NULL && k1->op_targ == OP_GLOB)
68dc0745 3446 || k1->op_type == OP_EACH)
72b16652
GS
3447 {
3448 warnop = ((k1->op_type == OP_NULL)
3449 ? k1->op_targ : k1->op_type);
3450 }
a6006777 3451 break;
3452 }
8ebc5c01 3453 if (warnop) {
57843af0
GS
3454 line_t oldline = CopLINE(PL_curcop);
3455 CopLINE_set(PL_curcop, PL_copline);
cea2e8a9 3456 Perl_warner(aTHX_ WARN_UNSAFE,
599cee73 3457 "Value of %s%s can be \"0\"; test with defined()",
22c35a8c 3458 PL_op_desc[warnop],
68dc0745 3459 ((warnop == OP_READLINE || warnop == OP_GLOB)
3460 ? " construct" : "() operator"));
57843af0 3461 CopLINE_set(PL_curcop, oldline);
8ebc5c01 3462 }
a6006777 3463 }
79072805
LW
3464
3465 if (!other)
3466 return first;
3467
a0d0e21e
LW
3468 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
3469 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
3470
b7dc083c 3471 NewOp(1101, logop, 1, LOGOP);
79072805
LW
3472
3473 logop->op_type = type;
22c35a8c 3474 logop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3475 logop->op_first = first;
3476 logop->op_flags = flags | OPf_KIDS;
3477 logop->op_other = LINKLIST(other);
c07a80fd 3478 logop->op_private = 1 | (flags >> 8);
79072805
LW
3479
3480 /* establish postfix order */
3481 logop->op_next = LINKLIST(first);
3482 first->op_next = (OP*)logop;
3483 first->op_sibling = other;
3484
11343788
MB
3485 o = newUNOP(OP_NULL, 0, (OP*)logop);
3486 other->op_next = o;
79072805 3487
11343788 3488 return o;
79072805
LW
3489}
3490
3491OP *
864dbfa3 3492Perl_newCONDOP(pTHX_ I32 flags, OP *first, OP *trueop, OP *falseop)
79072805 3493{
11343788 3494 dTHR;
1a67a97c
SM
3495 LOGOP *logop;
3496 OP *start;
11343788 3497 OP *o;
79072805 3498
b1cb66bf 3499 if (!falseop)
3500 return newLOGOP(OP_AND, 0, first, trueop);
3501 if (!trueop)
3502 return newLOGOP(OP_OR, 0, first, falseop);
79072805 3503
8990e307 3504 scalarboolean(first);
79072805
LW
3505 if (first->op_type == OP_CONST) {
3506 if (SvTRUE(((SVOP*)first)->op_sv)) {
3507 op_free(first);
b1cb66bf 3508 op_free(falseop);
3509 return trueop;
79072805
LW
3510 }
3511 else {
3512 op_free(first);
b1cb66bf 3513 op_free(trueop);
3514 return falseop;
79072805
LW
3515 }
3516 }
3517 else if (first->op_type == OP_WANTARRAY) {
b1cb66bf 3518 list(trueop);
3519 scalar(falseop);
79072805 3520 }
1a67a97c
SM
3521 NewOp(1101, logop, 1, LOGOP);
3522 logop->op_type = OP_COND_EXPR;
3523 logop->op_ppaddr = PL_ppaddr[OP_COND_EXPR];
3524 logop->op_first = first;
3525 logop->op_flags = flags | OPf_KIDS;
3526 logop->op_private = 1 | (flags >> 8);
3527 logop->op_other = LINKLIST(trueop);
3528 logop->op_next = LINKLIST(falseop);
79072805 3529
79072805
LW
3530
3531 /* establish postfix order */
1a67a97c
SM
3532 start = LINKLIST(first);
3533 first->op_next = (OP*)logop;
79072805 3534
b1cb66bf 3535 first->op_sibling = trueop;
3536 trueop->op_sibling = falseop;
1a67a97c 3537 o = newUNOP(OP_NULL, 0, (OP*)logop);
79072805 3538
1a67a97c 3539 trueop->op_next = falseop->op_next = o;
79072805 3540
1a67a97c 3541 o->op_next = start;
11343788 3542 return o;
79072805
LW
3543}
3544
3545OP *
864dbfa3 3546Perl_newRANGE(pTHX_ I32 flags, OP *left, OP *right)
79072805 3547{
b35b2403 3548 dTHR;
1a67a97c 3549 LOGOP *range;
79072805
LW
3550 OP *flip;
3551 OP *flop;
1a67a97c 3552 OP *leftstart;
11343788 3553 OP *o;
79072805 3554
1a67a97c 3555 NewOp(1101, range, 1, LOGOP);
79072805