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