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