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