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