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