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