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