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