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