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