This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Missing 'require' in auto-generated .pm by h2xs
[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) {
84902520 1404 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || 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 */
84902520 1488 if (PERLDB_INTER) {
3841441e 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
de939608
CS
1556 switch (type) {
1557 case OP_SPRINTF:
1558 case OP_UCFIRST:
1559 case OP_LCFIRST:
1560 case OP_UC:
1561 case OP_LC:
1562 if (o->op_private & OPpLOCALE)
1563 goto nope;
1564 }
1565
a0d0e21e
LW
1566 if (error_count)
1567 goto nope; /* Don't try to run w/ errors */
1568
79072805 1569 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
93a17b20
LW
1570 if (curop->op_type != OP_CONST &&
1571 curop->op_type != OP_LIST &&
1572 curop->op_type != OP_SCALAR &&
a0d0e21e 1573 curop->op_type != OP_NULL &&
93a17b20 1574 curop->op_type != OP_PUSHMARK) {
79072805
LW
1575 goto nope;
1576 }
1577 }
1578
1579 curop = LINKLIST(o);
1580 o->op_next = 0;
1581 op = curop;
8da795c6 1582 runops();
748a9306
LW
1583 sv = *(stack_sp--);
1584 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
79072805 1585 pad_swipe(o->op_targ);
748a9306
LW
1586 else if (SvTEMP(sv)) { /* grab mortal temp? */
1587 (void)SvREFCNT_inc(sv);
1588 SvTEMP_off(sv);
85e6fe83 1589 }
79072805
LW
1590 op_free(o);
1591 if (type == OP_RV2GV)
b1cb66bf 1592 return newGVOP(OP_GV, 0, (GV*)sv);
748a9306
LW
1593 else {
1594 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
1595 IV iv = SvIV(sv);
1596 if ((double)iv == SvNV(sv)) { /* can we smush double to int */
1597 SvREFCNT_dec(sv);
1598 sv = newSViv(iv);
1599 }
b1cb66bf
PP
1600 else
1601 SvIOK_off(sv); /* undo SvIV() damage */
748a9306
LW
1602 }
1603 return newSVOP(OP_CONST, 0, sv);
1604 }
79072805
LW
1605
1606 nope:
1607 if (!(opargs[type] & OA_OTHERINT))
1608 return o;
79072805 1609
85e6fe83 1610 if (!(hints & HINT_INTEGER)) {
a0d0e21e 1611 if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS))
85e6fe83
LW
1612 return o;
1613
1614 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
1615 if (curop->op_type == OP_CONST) {
b1cb66bf 1616 if (SvIOK(((SVOP*)curop)->op_sv))
85e6fe83
LW
1617 continue;
1618 return o;
1619 }
1620 if (opargs[curop->op_type] & OA_RETINTEGER)
79072805
LW
1621 continue;
1622 return o;
1623 }
a0d0e21e 1624 o->op_ppaddr = ppaddr[++(o->op_type)];
79072805
LW
1625 }
1626
79072805
LW
1627 return o;
1628}
1629
1630OP *
1631gen_constant_list(o)
1632register OP *o;
1633{
1634 register OP *curop;
79072805 1635 I32 oldtmps_floor = tmps_floor;
79072805 1636
a0d0e21e
LW
1637 list(o);
1638 if (error_count)
1639 return o; /* Don't attempt to run with errors */
1640
1641 op = curop = LINKLIST(o);
1642 o->op_next = 0;
1643 pp_pushmark();
8da795c6 1644 runops();
a0d0e21e
LW
1645 op = curop;
1646 pp_anonlist();
79072805 1647 tmps_floor = oldtmps_floor;
79072805
LW
1648
1649 o->op_type = OP_RV2AV;
1650 o->op_ppaddr = ppaddr[OP_RV2AV];
79072805 1651 curop = ((UNOP*)o)->op_first;
a0d0e21e 1652 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--));
79072805 1653 op_free(curop);
79072805
LW
1654 linklist(o);
1655 return list(o);
1656}
1657
1658OP *
1659convert(type, flags, op)
1660I32 type;
1661I32 flags;
1662OP* op;
1663{
1664 OP *kid;
a0d0e21e 1665 OP *last = 0;
79072805 1666
79072805
LW
1667 if (!op || op->op_type != OP_LIST)
1668 op = newLISTOP(OP_LIST, 0, op, Nullop);
748a9306 1669 else
54310121 1670 op->op_flags &= ~OPf_WANT;
79072805 1671
8990e307
LW
1672 if (!(opargs[type] & OA_MARK))
1673 null(cLISTOP->op_first);
1674
79072805
LW
1675 op->op_type = type;
1676 op->op_ppaddr = ppaddr[type];
1677 op->op_flags |= flags;
1678
e50aee73 1679 op = CHECKOP(type, op);
79072805
LW
1680 if (op->op_type != type)
1681 return op;
1682
1683 if (cLISTOP->op_children < 7) {
1684 /* XXX do we really need to do this if we're done appending?? */
1685 for (kid = cLISTOP->op_first; kid; kid = kid->op_sibling)
1686 last = kid;
1687 cLISTOP->op_last = last; /* in case check substituted last arg */
1688 }
1689
1690 return fold_constants(op);
1691}
1692
1693/* List constructors */
1694
1695OP *
1696append_elem(type, first, last)
1697I32 type;
1698OP* first;
1699OP* last;
1700{
1701 if (!first)
1702 return last;
8990e307
LW
1703
1704 if (!last)
79072805 1705 return first;
8990e307 1706
a0d0e21e
LW
1707 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
1708 return newLISTOP(type, 0, first, last);
79072805 1709
a0d0e21e
LW
1710 if (first->op_flags & OPf_KIDS)
1711 ((LISTOP*)first)->op_last->op_sibling = last;
1712 else {
1713 first->op_flags |= OPf_KIDS;
1714 ((LISTOP*)first)->op_first = last;
1715 }
1716 ((LISTOP*)first)->op_last = last;
1717 ((LISTOP*)first)->op_children++;
1718 return first;
79072805
LW
1719}
1720
1721OP *
1722append_list(type, first, last)
1723I32 type;
1724LISTOP* first;
1725LISTOP* last;
1726{
1727 if (!first)
1728 return (OP*)last;
8990e307
LW
1729
1730 if (!last)
79072805 1731 return (OP*)first;
8990e307
LW
1732
1733 if (first->op_type != type)
79072805 1734 return prepend_elem(type, (OP*)first, (OP*)last);
8990e307
LW
1735
1736 if (last->op_type != type)
79072805
LW
1737 return append_elem(type, (OP*)first, (OP*)last);
1738
1739 first->op_last->op_sibling = last->op_first;
1740 first->op_last = last->op_last;
1741 first->op_children += last->op_children;
1742 if (first->op_children)
1743 last->op_flags |= OPf_KIDS;
1744
1745 Safefree(last);
1746 return (OP*)first;
1747}
1748
1749OP *
1750prepend_elem(type, first, last)
1751I32 type;
1752OP* first;
1753OP* last;
1754{
1755 if (!first)
1756 return last;
8990e307
LW
1757
1758 if (!last)
79072805 1759 return first;
8990e307
LW
1760
1761 if (last->op_type == type) {
1762 if (type == OP_LIST) { /* already a PUSHMARK there */
1763 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
1764 ((LISTOP*)last)->op_first->op_sibling = first;
1765 }
1766 else {
1767 if (!(last->op_flags & OPf_KIDS)) {
1768 ((LISTOP*)last)->op_last = first;
1769 last->op_flags |= OPf_KIDS;
1770 }
1771 first->op_sibling = ((LISTOP*)last)->op_first;
1772 ((LISTOP*)last)->op_first = first;
79072805 1773 }
79072805
LW
1774 ((LISTOP*)last)->op_children++;
1775 return last;
1776 }
1777
1778 return newLISTOP(type, 0, first, last);
1779}
1780
1781/* Constructors */
1782
1783OP *
1784newNULLLIST()
1785{
8990e307
LW
1786 return newOP(OP_STUB, 0);
1787}
1788
1789OP *
1790force_list(op)
1791OP* op;
1792{
1793 if (!op || op->op_type != OP_LIST)
1794 op = newLISTOP(OP_LIST, 0, op, Nullop);
1795 null(op);
1796 return op;
79072805
LW
1797}
1798
1799OP *
1800newLISTOP(type, flags, first, last)
1801I32 type;
1802I32 flags;
1803OP* first;
1804OP* last;
1805{
1806 LISTOP *listop;
1807
1808 Newz(1101, listop, 1, LISTOP);
1809
1810 listop->op_type = type;
1811 listop->op_ppaddr = ppaddr[type];
1812 listop->op_children = (first != 0) + (last != 0);
1813 listop->op_flags = flags;
79072805
LW
1814
1815 if (!last && first)
1816 last = first;
1817 else if (!first && last)
1818 first = last;
8990e307
LW
1819 else if (first)
1820 first->op_sibling = last;
79072805
LW
1821 listop->op_first = first;
1822 listop->op_last = last;
8990e307
LW
1823 if (type == OP_LIST) {
1824 OP* pushop;
1825 pushop = newOP(OP_PUSHMARK, 0);
1826 pushop->op_sibling = first;
1827 listop->op_first = pushop;
1828 listop->op_flags |= OPf_KIDS;
1829 if (!last)
1830 listop->op_last = pushop;
1831 }
1832 else if (listop->op_children)
1833 listop->op_flags |= OPf_KIDS;
79072805
LW
1834
1835 return (OP*)listop;
1836}
1837
1838OP *
1839newOP(type, flags)
1840I32 type;
1841I32 flags;
1842{
1843 OP *op;
1844 Newz(1101, op, 1, OP);
1845 op->op_type = type;
1846 op->op_ppaddr = ppaddr[type];
1847 op->op_flags = flags;
1848
1849 op->op_next = op;
c07a80fd 1850 op->op_private = 0 + (flags >> 8);
79072805
LW
1851 if (opargs[type] & OA_RETSCALAR)
1852 scalar(op);
1853 if (opargs[type] & OA_TARGET)
ed6116ce 1854 op->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 1855 return CHECKOP(type, op);
79072805
LW
1856}
1857
1858OP *
1859newUNOP(type, flags, first)
1860I32 type;
1861I32 flags;
1862OP* first;
1863{
1864 UNOP *unop;
1865
93a17b20
LW
1866 if (!first)
1867 first = newOP(OP_STUB, 0);
8990e307
LW
1868 if (opargs[type] & OA_MARK)
1869 first = force_list(first);
93a17b20 1870
79072805
LW
1871 Newz(1101, unop, 1, UNOP);
1872 unop->op_type = type;
1873 unop->op_ppaddr = ppaddr[type];
1874 unop->op_first = first;
1875 unop->op_flags = flags | OPf_KIDS;
c07a80fd 1876 unop->op_private = 1 | (flags >> 8);
79072805 1877
e50aee73 1878 unop = (UNOP*) CHECKOP(type, unop);
79072805
LW
1879 if (unop->op_next)
1880 return (OP*)unop;
1881
a0d0e21e 1882 return fold_constants((OP *) unop);
79072805
LW
1883}
1884
1885OP *
1886newBINOP(type, flags, first, last)
1887I32 type;
1888I32 flags;
1889OP* first;
1890OP* last;
1891{
1892 BINOP *binop;
1893 Newz(1101, binop, 1, BINOP);
1894
1895 if (!first)
1896 first = newOP(OP_NULL, 0);
1897
1898 binop->op_type = type;
1899 binop->op_ppaddr = ppaddr[type];
1900 binop->op_first = first;
1901 binop->op_flags = flags | OPf_KIDS;
1902 if (!last) {
1903 last = first;
c07a80fd 1904 binop->op_private = 1 | (flags >> 8);
79072805
LW
1905 }
1906 else {
c07a80fd 1907 binop->op_private = 2 | (flags >> 8);
79072805
LW
1908 first->op_sibling = last;
1909 }
1910
e50aee73 1911 binop = (BINOP*)CHECKOP(type, binop);
79072805
LW
1912 if (binop->op_next)
1913 return (OP*)binop;
1914
1915 binop->op_last = last = binop->op_first->op_sibling;
1916
a0d0e21e 1917 return fold_constants((OP *)binop);
79072805
LW
1918}
1919
1920OP *
1921pmtrans(op, expr, repl)
1922OP *op;
1923OP *expr;
1924OP *repl;
1925{
79072805
LW
1926 SV *tstr = ((SVOP*)expr)->op_sv;
1927 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
1928 STRLEN tlen;
1929 STRLEN rlen;
ec49126f
PP
1930 register U8 *t = (U8*)SvPV(tstr, tlen);
1931 register U8 *r = (U8*)SvPV(rstr, rlen);
79072805
LW
1932 register I32 i;
1933 register I32 j;
79072805
LW
1934 I32 delete;
1935 I32 complement;
1936 register short *tbl;
1937
1938 tbl = (short*)cPVOP->op_pv;
1939 complement = op->op_private & OPpTRANS_COMPLEMENT;
1940 delete = op->op_private & OPpTRANS_DELETE;
748a9306 1941 /* squash = op->op_private & OPpTRANS_SQUASH; */
79072805
LW
1942
1943 if (complement) {
1944 Zero(tbl, 256, short);
1945 for (i = 0; i < tlen; i++)
ec49126f 1946 tbl[t[i]] = -1;
79072805
LW
1947 for (i = 0, j = 0; i < 256; i++) {
1948 if (!tbl[i]) {
1949 if (j >= rlen) {
1950 if (delete)
1951 tbl[i] = -2;
1952 else if (rlen)
ec49126f 1953 tbl[i] = r[j-1];
79072805
LW
1954 else
1955 tbl[i] = i;
1956 }
1957 else
ec49126f 1958 tbl[i] = r[j++];
79072805
LW
1959 }
1960 }
1961 }
1962 else {
1963 if (!rlen && !delete) {
1964 r = t; rlen = tlen;
1965 }
1966 for (i = 0; i < 256; i++)
1967 tbl[i] = -1;
1968 for (i = 0, j = 0; i < tlen; i++,j++) {
1969 if (j >= rlen) {
1970 if (delete) {
ec49126f
PP
1971 if (tbl[t[i]] == -1)
1972 tbl[t[i]] = -2;
79072805
LW
1973 continue;
1974 }
1975 --j;
1976 }
ec49126f
PP
1977 if (tbl[t[i]] == -1)
1978 tbl[t[i]] = r[j];
79072805
LW
1979 }
1980 }
1981 op_free(expr);
1982 op_free(repl);
1983
1984 return op;
1985}
1986
1987OP *
1988newPMOP(type, flags)
1989I32 type;
1990I32 flags;
1991{
1992 PMOP *pmop;
1993
1994 Newz(1101, pmop, 1, PMOP);
1995 pmop->op_type = type;
1996 pmop->op_ppaddr = ppaddr[type];
1997 pmop->op_flags = flags;
c07a80fd 1998 pmop->op_private = 0 | (flags >> 8);
79072805 1999
36477c24
PP
2000 if (hints & HINT_LOCALE)
2001 pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
2002
79072805 2003 /* link into pm list */
a0d0e21e 2004 if (type != OP_TRANS && curstash) {
79072805
LW
2005 pmop->op_pmnext = HvPMROOT(curstash);
2006 HvPMROOT(curstash) = pmop;
2007 }
2008
2009 return (OP*)pmop;
2010}
2011
2012OP *
2013pmruntime(op, expr, repl)
2014OP *op;
2015OP *expr;
2016OP *repl;
2017{
2018 PMOP *pm;
2019 LOGOP *rcop;
2020
2021 if (op->op_type == OP_TRANS)
2022 return pmtrans(op, expr, repl);
2023
3e3baf6d 2024 hints |= HINT_BLOCK_SCOPE;
79072805
LW
2025 pm = (PMOP*)op;
2026
2027 if (expr->op_type == OP_CONST) {
463ee0b2 2028 STRLEN plen;
79072805 2029 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 2030 char *p = SvPV(pat, plen);
79072805 2031 if ((op->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 2032 sv_setpvn(pat, "\\s+", 3);
463ee0b2 2033 p = SvPV(pat, plen);
79072805
LW
2034 pm->op_pmflags |= PMf_SKIPWHITE;
2035 }
e50aee73 2036 pm->op_pmregexp = pregcomp(p, p + plen, pm);
85e6fe83
LW
2037 if (strEQ("\\s+", pm->op_pmregexp->precomp))
2038 pm->op_pmflags |= PMf_WHITE;
79072805
LW
2039 hoistmust(pm);
2040 op_free(expr);
2041 }
2042 else {
463ee0b2
LW
2043 if (pm->op_pmflags & PMf_KEEP)
2044 expr = newUNOP(OP_REGCMAYBE,0,expr);
2045
79072805
LW
2046 Newz(1101, rcop, 1, LOGOP);
2047 rcop->op_type = OP_REGCOMP;
2048 rcop->op_ppaddr = ppaddr[OP_REGCOMP];
2049 rcop->op_first = scalar(expr);
2050 rcop->op_flags |= OPf_KIDS;
2051 rcop->op_private = 1;
2052 rcop->op_other = op;
2053
2054 /* establish postfix order */
463ee0b2
LW
2055 if (pm->op_pmflags & PMf_KEEP) {
2056 LINKLIST(expr);
2057 rcop->op_next = expr;
2058 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2059 }
2060 else {
2061 rcop->op_next = LINKLIST(expr);
2062 expr->op_next = (OP*)rcop;
2063 }
79072805 2064
463ee0b2 2065 prepend_elem(op->op_type, scalar((OP*)rcop), op);
79072805
LW
2066 }
2067
2068 if (repl) {
748a9306
LW
2069 OP *curop;
2070 if (pm->op_pmflags & PMf_EVAL)
2071 curop = 0;
2072 else if (repl->op_type == OP_CONST)
2073 curop = repl;
79072805 2074 else {
79072805
LW
2075 OP *lastop = 0;
2076 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
2077 if (opargs[curop->op_type] & OA_DANGEROUS) {
2078 if (curop->op_type == OP_GV) {
2079 GV *gv = ((GVOP*)curop)->op_gv;
93a17b20 2080 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
2081 break;
2082 }
2083 else if (curop->op_type == OP_RV2CV)
2084 break;
2085 else if (curop->op_type == OP_RV2SV ||
2086 curop->op_type == OP_RV2AV ||
2087 curop->op_type == OP_RV2HV ||
2088 curop->op_type == OP_RV2GV) {
2089 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2090 break;
2091 }
748a9306
LW
2092 else if (curop->op_type == OP_PADSV ||
2093 curop->op_type == OP_PADAV ||
2094 curop->op_type == OP_PADHV ||
2095 curop->op_type == OP_PADANY) {
2096 /* is okay */
2097 }
79072805
LW
2098 else
2099 break;
2100 }
2101 lastop = curop;
2102 }
748a9306
LW
2103 }
2104 if (curop == repl) {
2105 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4633a7c4 2106 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
748a9306
LW
2107 prepend_elem(op->op_type, scalar(repl), op);
2108 }
2109 else {
2110 Newz(1101, rcop, 1, LOGOP);
2111 rcop->op_type = OP_SUBSTCONT;
2112 rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
2113 rcop->op_first = scalar(repl);
2114 rcop->op_flags |= OPf_KIDS;
2115 rcop->op_private = 1;
2116 rcop->op_other = op;
2117
2118 /* establish postfix order */
2119 rcop->op_next = LINKLIST(repl);
2120 repl->op_next = (OP*)rcop;
2121
2122 pm->op_pmreplroot = scalar((OP*)rcop);
2123 pm->op_pmreplstart = LINKLIST(rcop);
2124 rcop->op_next = 0;
79072805
LW
2125 }
2126 }
2127
2128 return (OP*)pm;
2129}
2130
2131OP *
2132newSVOP(type, flags, sv)
2133I32 type;
2134I32 flags;
2135SV *sv;
2136{
2137 SVOP *svop;
2138 Newz(1101, svop, 1, SVOP);
2139 svop->op_type = type;
2140 svop->op_ppaddr = ppaddr[type];
2141 svop->op_sv = sv;
2142 svop->op_next = (OP*)svop;
2143 svop->op_flags = flags;
2144 if (opargs[type] & OA_RETSCALAR)
463ee0b2 2145 scalar((OP*)svop);
79072805 2146 if (opargs[type] & OA_TARGET)
ed6116ce 2147 svop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2148 return CHECKOP(type, svop);
79072805
LW
2149}
2150
2151OP *
2152newGVOP(type, flags, gv)
2153I32 type;
2154I32 flags;
2155GV *gv;
2156{
2157 GVOP *gvop;
2158 Newz(1101, gvop, 1, GVOP);
2159 gvop->op_type = type;
2160 gvop->op_ppaddr = ppaddr[type];
8990e307 2161 gvop->op_gv = (GV*)SvREFCNT_inc(gv);
79072805
LW
2162 gvop->op_next = (OP*)gvop;
2163 gvop->op_flags = flags;
2164 if (opargs[type] & OA_RETSCALAR)
463ee0b2 2165 scalar((OP*)gvop);
79072805 2166 if (opargs[type] & OA_TARGET)
ed6116ce 2167 gvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2168 return CHECKOP(type, gvop);
79072805
LW
2169}
2170
2171OP *
2172newPVOP(type, flags, pv)
2173I32 type;
2174I32 flags;
2175char *pv;
2176{
2177 PVOP *pvop;
2178 Newz(1101, pvop, 1, PVOP);
2179 pvop->op_type = type;
2180 pvop->op_ppaddr = ppaddr[type];
2181 pvop->op_pv = pv;
2182 pvop->op_next = (OP*)pvop;
2183 pvop->op_flags = flags;
2184 if (opargs[type] & OA_RETSCALAR)
463ee0b2 2185 scalar((OP*)pvop);
79072805 2186 if (opargs[type] & OA_TARGET)
ed6116ce 2187 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 2188 return CHECKOP(type, pvop);
79072805
LW
2189}
2190
79072805
LW
2191void
2192package(op)
2193OP *op;
2194{
93a17b20 2195 SV *sv;
79072805
LW
2196
2197 save_hptr(&curstash);
2198 save_item(curstname);
93a17b20 2199 if (op) {
463ee0b2
LW
2200 STRLEN len;
2201 char *name;
93a17b20 2202 sv = cSVOP->op_sv;
463ee0b2 2203 name = SvPV(sv, len);
b1cb66bf 2204 curstash = gv_stashpvn(name,len,TRUE);
463ee0b2 2205 sv_setpvn(curstname, name, len);
93a17b20
LW
2206 op_free(op);
2207 }
2208 else {
2209 sv_setpv(curstname,"<none>");
2210 curstash = Nullhv;
2211 }
79072805 2212 copline = NOLINE;
8990e307 2213 expect = XSTATE;
79072805
LW
2214}
2215
85e6fe83 2216void
b1cb66bf 2217utilize(aver, floor, version, id, arg)
85e6fe83 2218int aver;
c07a80fd 2219I32 floor;
b1cb66bf 2220OP *version;
85e6fe83
LW
2221OP *id;
2222OP *arg;
2223{
a0d0e21e
LW
2224 OP *pack;
2225 OP *meth;
2226 OP *rqop;
2227 OP *imop;
b1cb66bf 2228 OP *veop;
85e6fe83 2229
a0d0e21e
LW
2230 if (id->op_type != OP_CONST)
2231 croak("Module name must be constant");
85e6fe83 2232
b1cb66bf
PP
2233 veop = Nullop;
2234
2235 if(version != Nullop) {
2236 SV *vesv = ((SVOP*)version)->op_sv;
2237
2238 if (arg == Nullop && !SvNIOK(vesv)) {
2239 arg = version;
2240 }
2241 else {
2242 OP *pack;
2243 OP *meth;
2244
2245 if (version->op_type != OP_CONST || !SvNIOK(vesv))
2246 croak("Version number must be constant number");
2247
2248 /* Make copy of id so we don't free it twice */
2249 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
2250
2251 /* Fake up a method call to VERSION */
2252 meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7));
2253 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
2254 append_elem(OP_LIST,
2255 prepend_elem(OP_LIST, pack, list(version)),
2256 newUNOP(OP_METHOD, 0, meth)));
2257 }
2258 }
2259
a0d0e21e 2260 /* Fake up an import/unimport */
4633a7c4
LW
2261 if (arg && arg->op_type == OP_STUB)
2262 imop = arg; /* no import on explicit () */
b1cb66bf
PP
2263 else if(SvNIOK(((SVOP*)id)->op_sv)) {
2264 imop = Nullop; /* use 5.0; */
2265 }
4633a7c4
LW
2266 else {
2267 /* Make copy of id so we don't free it twice */
2268 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
4633a7c4
LW
2269 meth = newSVOP(OP_CONST, 0,
2270 aver
2271 ? newSVpv("import", 6)
2272 : newSVpv("unimport", 8)
2273 );
2274 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
a0d0e21e
LW
2275 append_elem(OP_LIST,
2276 prepend_elem(OP_LIST, pack, list(arg)),
2277 newUNOP(OP_METHOD, 0, meth)));
4633a7c4
LW
2278 }
2279
2280 /* Fake up a require */
2281 rqop = newUNOP(OP_REQUIRE, 0, id);
a0d0e21e
LW
2282
2283 /* Fake up the BEGIN {}, which does its thing immediately. */
c07a80fd 2284 newSUB(floor,
a0d0e21e 2285 newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)),
4633a7c4 2286 Nullop,
a0d0e21e 2287 append_elem(OP_LINESEQ,
b1cb66bf
PP
2288 append_elem(OP_LINESEQ,
2289 newSTATEOP(0, Nullch, rqop),
2290 newSTATEOP(0, Nullch, veop)),
a0d0e21e 2291 newSTATEOP(0, Nullch, imop) ));
85e6fe83 2292
85e6fe83
LW
2293 copline = NOLINE;
2294 expect = XSTATE;
2295}
2296
79072805
LW
2297OP *
2298newSLICEOP(flags, subscript, listval)
2299I32 flags;
2300OP *subscript;
2301OP *listval;
2302{
2303 return newBINOP(OP_LSLICE, flags,
8990e307
LW
2304 list(force_list(subscript)),
2305 list(force_list(listval)) );
79072805
LW
2306}
2307
2308static I32
2309list_assignment(op)
2310register OP *op;
2311{
2312 if (!op)
2313 return TRUE;
2314
2315 if (op->op_type == OP_NULL && op->op_flags & OPf_KIDS)
2316 op = cUNOP->op_first;
2317
2318 if (op->op_type == OP_COND_EXPR) {
2319 I32 t = list_assignment(cCONDOP->op_first->op_sibling);
2320 I32 f = list_assignment(cCONDOP->op_first->op_sibling->op_sibling);
2321
2322 if (t && f)
2323 return TRUE;
2324 if (t || f)
2325 yyerror("Assignment to both a list and a scalar");
2326 return FALSE;
2327 }
2328
2329 if (op->op_type == OP_LIST || op->op_flags & OPf_PARENS ||
2330 op->op_type == OP_RV2AV || op->op_type == OP_RV2HV ||
2331 op->op_type == OP_ASLICE || op->op_type == OP_HSLICE)
2332 return TRUE;
2333
93a17b20
LW
2334 if (op->op_type == OP_PADAV || op->op_type == OP_PADHV)
2335 return TRUE;
2336
79072805
LW
2337 if (op->op_type == OP_RV2SV)
2338 return FALSE;
2339
2340 return FALSE;
2341}
2342
2343OP *
a0d0e21e 2344newASSIGNOP(flags, left, optype, right)
79072805
LW
2345I32 flags;
2346OP *left;
a0d0e21e 2347I32 optype;
79072805
LW
2348OP *right;
2349{
2350 OP *op;
2351
a0d0e21e
LW
2352 if (optype) {
2353 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
2354 return newLOGOP(optype, 0,
2355 mod(scalar(left), optype),
2356 newUNOP(OP_SASSIGN, 0, scalar(right)));
2357 }
2358 else {
2359 return newBINOP(optype, OPf_STACKED,
2360 mod(scalar(left), optype), scalar(right));
2361 }
2362 }
2363
79072805 2364 if (list_assignment(left)) {
463ee0b2 2365 modcount = 0;
748a9306 2366 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
463ee0b2 2367 left = mod(left, OP_AASSIGN);
748a9306
LW
2368 if (eval_start)
2369 eval_start = 0;
2370 else {
a0d0e21e
LW
2371 op_free(left);
2372 op_free(right);
2373 return Nullop;
2374 }
79072805 2375 op = newBINOP(OP_AASSIGN, flags,
8990e307
LW
2376 list(force_list(right)),
2377 list(force_list(left)) );
c07a80fd 2378 op->op_private = 0 | (flags >> 8);
a0d0e21e 2379 if (!(left->op_private & OPpLVAL_INTRO)) {
748a9306 2380 static int generation = 100;
79072805
LW
2381 OP *curop;
2382 OP *lastop = op;
2383 generation++;
2384 for (curop = LINKLIST(op); curop != op; curop = LINKLIST(curop)) {
2385 if (opargs[curop->op_type] & OA_DANGEROUS) {
2386 if (curop->op_type == OP_GV) {
2387 GV *gv = ((GVOP*)curop)->op_gv;
2388 if (gv == defgv || SvCUR(gv) == generation)
2389 break;
2390 SvCUR(gv) = generation;
2391 }
748a9306
LW
2392 else if (curop->op_type == OP_PADSV ||
2393 curop->op_type == OP_PADAV ||
2394 curop->op_type == OP_PADHV ||
2395 curop->op_type == OP_PADANY) {
2396 SV **svp = AvARRAY(comppad_name);
8e07c86e 2397 SV *sv = svp[curop->op_targ];
748a9306
LW
2398 if (SvCUR(sv) == generation)
2399 break;
2400 SvCUR(sv) = generation; /* (SvCUR not used any more) */
2401 }
79072805
LW
2402 else if (curop->op_type == OP_RV2CV)
2403 break;
2404 else if (curop->op_type == OP_RV2SV ||
2405 curop->op_type == OP_RV2AV ||
2406 curop->op_type == OP_RV2HV ||
2407 curop->op_type == OP_RV2GV) {
2408 if (lastop->op_type != OP_GV) /* funny deref? */
2409 break;
2410 }
2411 else
2412 break;
2413 }
2414 lastop = curop;
2415 }
2416 if (curop != op)
2417 op->op_private = OPpASSIGN_COMMON;
2418 }
c07a80fd
PP
2419 if (right && right->op_type == OP_SPLIT) {
2420 OP* tmpop;
2421 if ((tmpop = ((LISTOP*)right)->op_first) &&
2422 tmpop->op_type == OP_PUSHRE)
2423 {
2424 PMOP *pm = (PMOP*)tmpop;
2425 if (left->op_type == OP_RV2AV &&
2426 !(left->op_private & OPpLVAL_INTRO) &&
2427 !(op->op_private & OPpASSIGN_COMMON) )
2428 {
2429 tmpop = ((UNOP*)left)->op_first;
2430 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
2431 pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv;
2432 pm->op_pmflags |= PMf_ONCE;
2433 tmpop = ((UNOP*)op)->op_first; /* to list (nulled) */
2434 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
2435 tmpop->op_sibling = Nullop; /* don't free split */
2436 right->op_next = tmpop->op_next; /* fix starting loc */
2437 op_free(op); /* blow off assign */
54310121 2438 right->op_flags &= ~OPf_WANT;
a5f75d66 2439 /* "I don't know and I don't care." */
c07a80fd
PP
2440 return right;
2441 }
2442 }
2443 else {
2444 if (modcount < 10000 &&
2445 ((LISTOP*)right)->op_last->op_type == OP_CONST)
2446 {
2447 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
2448 if (SvIVX(sv) == 0)
2449 sv_setiv(sv, modcount+1);
2450 }
2451 }
2452 }
2453 }
79072805
LW
2454 return op;
2455 }
2456 if (!right)
2457 right = newOP(OP_UNDEF, 0);
2458 if (right->op_type == OP_READLINE) {
2459 right->op_flags |= OPf_STACKED;
463ee0b2 2460 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805 2461 }
a0d0e21e 2462 else {
748a9306 2463 eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
79072805 2464 op = newBINOP(OP_SASSIGN, flags,
463ee0b2 2465 scalar(right), mod(scalar(left), OP_SASSIGN) );
748a9306
LW
2466 if (eval_start)
2467 eval_start = 0;
2468 else {
a0d0e21e
LW
2469 op_free(op);
2470 return Nullop;
2471 }
2472 }
79072805
LW
2473 return op;
2474}
2475
2476OP *
2477newSTATEOP(flags, label, op)
2478I32 flags;
2479char *label;
2480OP *op;
2481{
bbce6d69 2482 U32 seq = intro_my();
79072805
LW
2483 register COP *cop;
2484
2485 Newz(1101, cop, 1, COP);
84902520 2486 if (PERLDB_LINE && curcop->cop_line && curstash != debstash) {
8990e307
LW
2487 cop->op_type = OP_DBSTATE;
2488 cop->op_ppaddr = ppaddr[ OP_DBSTATE ];
2489 }
2490 else {
2491 cop->op_type = OP_NEXTSTATE;
2492 cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ];
2493 }
79072805 2494 cop->op_flags = flags;
c07a80fd 2495 cop->op_private = 0 | (flags >> 8);
ff0cee69
PP
2496#ifdef NATIVE_HINTS
2497 cop->op_private |= NATIVE_HINTS;
2498#endif
79072805
LW
2499 cop->op_next = (OP*)cop;
2500
463ee0b2
LW
2501 if (label) {
2502 cop->cop_label = label;
85e6fe83 2503 hints |= HINT_BLOCK_SCOPE;
463ee0b2 2504 }
bbce6d69 2505 cop->cop_seq = seq;
a0d0e21e 2506 cop->cop_arybase = curcop->cop_arybase;
79072805
LW
2507
2508 if (copline == NOLINE)
2509 cop->cop_line = curcop->cop_line;
2510 else {
2511 cop->cop_line = copline;
2512 copline = NOLINE;
2513 }
44a8e56a 2514 cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv);
79072805
LW
2515 cop->cop_stash = curstash;
2516
84902520 2517 if (PERLDB_LINE && curstash != debstash) {
93a17b20
LW
2518 SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE);
2519 if (svp && *svp != &sv_undef && !SvIOK(*svp)) {
a0d0e21e 2520 (void)SvIOK_on(*svp);
a5f75d66 2521 SvIVX(*svp) = 1;
93a17b20
LW
2522 SvSTASH(*svp) = (HV*)cop;
2523 }
2524 }
2525
79072805
LW
2526 return prepend_elem(OP_LINESEQ, (OP*)cop, op);
2527}
2528
bbce6d69
PP
2529/* "Introduce" my variables to visible status. */
2530U32
2531intro_my()
2532{
2533 SV **svp;
2534 SV *sv;
2535 I32 i;
2536
2537 if (! min_intro_pending)
2538 return cop_seqmax;
2539
2540 svp = AvARRAY(comppad_name);
2541 for (i = min_intro_pending; i <= max_intro_pending; i++) {
2542 if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) {
2543 SvIVX(sv) = 999999999; /* Don't know scope end yet. */
2544 SvNVX(sv) = (double)cop_seqmax;
2545 }
2546 }
2547 min_intro_pending = 0;
2548 comppad_name_fill = max_intro_pending; /* Needn't search higher */
2549 return cop_seqmax++;
2550}
2551
79072805
LW
2552OP *
2553newLOGOP(type, flags, first, other)
2554I32 type;
2555I32 flags;
2556OP* first;
2557OP* other;
2558{
2559 LOGOP *logop;
2560 OP *op;
2561
a0d0e21e
LW
2562 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
2563 return newBINOP(type, flags, scalar(first), scalar(other));
2564
8990e307 2565 scalarboolean(first);
79072805
LW
2566 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
2567 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
2568 if (type == OP_AND || type == OP_OR) {
2569 if (type == OP_AND)
2570 type = OP_OR;
2571 else
2572 type = OP_AND;
2573 op = first;
2574 first = cUNOP->op_first;
2575 if (op->op_next)
2576 first->op_next = op->op_next;
2577 cUNOP->op_first = Nullop;
2578 op_free(op);
2579 }
2580 }
2581 if (first->op_type == OP_CONST) {
93a17b20 2582 if (dowarn && (first->op_private & OPpCONST_BARE))
c07a80fd 2583 warn("Probable precedence problem on %s", op_desc[type]);
79072805
LW
2584 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
2585 op_free(first);
2586 return other;
2587 }
2588 else {
2589 op_free(other);
2590 return first;
2591 }
2592 }
2593 else if (first->op_type == OP_WANTARRAY) {
2594 if (type == OP_AND)
2595 list(other);
2596 else
2597 scalar(other);
2598 }
a6006777
PP
2599 else if (dowarn && (first->op_flags & OPf_KIDS)) {
2600 OP *k1 = ((UNOP*)first)->op_first;
2601 OP *k2 = k1->op_sibling;
2602 OPCODE warnop = 0;
2603 switch (first->op_type)
2604 {
2605 case OP_NULL:
2606 if (k2 && k2->op_type == OP_READLINE
2607 && (k2->op_flags & OPf_STACKED)
2608 && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV))
2609 warnop = k2->op_type;
2610 break;
2611
2612 case OP_SASSIGN:
68dc0745
PP
2613 if (k1->op_type == OP_READDIR
2614 || k1->op_type == OP_GLOB
2615 || k1->op_type == OP_EACH)
a6006777
PP
2616 warnop = k1->op_type;
2617 break;
2618 }
8ebc5c01
PP
2619 if (warnop) {
2620 line_t oldline = curcop->cop_line;
2621 curcop->cop_line = copline;
68dc0745
PP
2622 warn("Value of %s%s can be \"0\"; test with defined()",
2623 op_desc[warnop],
2624 ((warnop == OP_READLINE || warnop == OP_GLOB)
2625 ? " construct" : "() operator"));
2626 curcop->cop_line = oldline;
8ebc5c01 2627 }
a6006777 2628 }
79072805
LW
2629
2630 if (!other)
2631 return first;
2632
a0d0e21e
LW
2633 if (type == OP_ANDASSIGN || type == OP_ORASSIGN)
2634 other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */
2635
79072805
LW
2636 Newz(1101, logop, 1, LOGOP);
2637
2638 logop->op_type = type;
2639 logop->op_ppaddr = ppaddr[type];
2640 logop->op_first = first;
2641 logop->op_flags = flags | OPf_KIDS;
2642 logop->op_other = LINKLIST(other);
c07a80fd 2643 logop->op_private = 1 | (flags >> 8);
79072805
LW
2644
2645 /* establish postfix order */
2646 logop->op_next = LINKLIST(first);
2647 first->op_next = (OP*)logop;
2648 first->op_sibling = other;
2649
2650 op = newUNOP(OP_NULL, 0, (OP*)logop);
2651 other->op_next = op;
2652
2653 return op;
2654}
2655
2656OP *
b1cb66bf 2657newCONDOP(flags, first, trueop, falseop)
79072805
LW
2658I32 flags;
2659OP* first;
b1cb66bf
PP
2660OP* trueop;
2661OP* falseop;
79072805
LW
2662{
2663 CONDOP *condop;
2664 OP *op;
2665
b1cb66bf
PP
2666 if (!falseop)
2667 return newLOGOP(OP_AND, 0, first, trueop);
2668 if (!trueop)
2669 return newLOGOP(OP_OR, 0, first, falseop);
79072805 2670
8990e307 2671 scalarboolean(first);
79072805
LW
2672 if (first->op_type == OP_CONST) {
2673 if (SvTRUE(((SVOP*)first)->op_sv)) {
2674 op_free(first);
b1cb66bf
PP
2675 op_free(falseop);
2676 return trueop;
79072805
LW
2677 }
2678 else {
2679 op_free(first);
b1cb66bf
PP
2680 op_free(trueop);
2681 return falseop;
79072805
LW
2682 }
2683 }
2684 else if (first->op_type == OP_WANTARRAY) {
b1cb66bf
PP
2685 list(trueop);
2686 scalar(falseop);
79072805
LW
2687 }
2688 Newz(1101, condop, 1, CONDOP);
2689
2690 condop->op_type = OP_COND_EXPR;
2691 condop->op_ppaddr = ppaddr[OP_COND_EXPR];
2692 condop->op_first = first;
2693 condop->op_flags = flags | OPf_KIDS;
b1cb66bf
PP
2694 condop->op_true = LINKLIST(trueop);
2695 condop->op_false = LINKLIST(falseop);
c07a80fd 2696 condop->op_private = 1 | (flags >> 8);
79072805
LW
2697
2698 /* establish postfix order */
2699 condop->op_next = LINKLIST(first);
2700 first->op_next = (OP*)condop;
2701
b1cb66bf
PP
2702 first->op_sibling = trueop;
2703 trueop->op_sibling = falseop;
79072805
LW
2704 op = newUNOP(OP_NULL, 0, (OP*)condop);
2705
b1cb66bf
PP
2706 trueop->op_next = op;
2707 falseop->op_next = op;
79072805
LW
2708
2709 return op;
2710}
2711
2712OP *
2713newRANGE(flags, left, right)
2714I32 flags;
2715OP *left;
2716OP *right;
2717{
2718 CONDOP *condop;
2719 OP *flip;
2720 OP *flop;
2721 OP *op;
2722
2723 Newz(1101, condop, 1, CONDOP);
2724
2725 condop->op_type = OP_RANGE;
2726 condop->op_ppaddr = ppaddr[OP_RANGE];
2727 condop->op_first = left;
2728 condop->op_flags = OPf_KIDS;
2729 condop->op_true = LINKLIST(left);
2730 condop->op_false = LINKLIST(right);
c07a80fd 2731 condop->op_private = 1 | (flags >> 8);
79072805
LW
2732
2733 left->op_sibling = right;
2734
2735 condop->op_next = (OP*)condop;
2736 flip = newUNOP(OP_FLIP, flags, (OP*)condop);
2737 flop = newUNOP(OP_FLOP, 0, flip);
2738 op = newUNOP(OP_NULL, 0, flop);
2739 linklist(flop);
2740
2741 left->op_next = flip;
2742 right->op_next = flop;
2743
ed6116ce 2744 condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805 2745 sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV);
ed6116ce 2746 flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY);
79072805
LW
2747 sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV);
2748
2749 flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2750 flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0;
2751
2752 flip->op_next = op;
2753 if (!flip->op_private || !flop->op_private)
2754 linklist(op); /* blow off optimizer unless constant */
2755
2756 return op;
2757}
2758
2759OP *
2760newLOOPOP(flags, debuggable, expr, block)
2761I32 flags;
2762I32 debuggable;
2763OP *expr;
2764OP *block;
2765{
463ee0b2 2766 OP* listop;
93a17b20 2767 OP* op;
463ee0b2 2768 int once = block && block->op_flags & OPf_SPECIAL &&
a0d0e21e 2769 (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL);
93a17b20 2770
463ee0b2
LW
2771 if (expr) {
2772 if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv))
2773 return block; /* do {} while 0 does once */
774d564b
PP
2774 if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB) {
2775 expr = newUNOP(OP_DEFINED, 0,
2776 newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2777 }
463ee0b2 2778 }
93a17b20 2779
8990e307 2780 listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0));
93a17b20 2781 op = newLOGOP(OP_AND, 0, expr, listop);
463ee0b2 2782
79072805
LW
2783 ((LISTOP*)listop)->op_last->op_next = LINKLIST(op);
2784
463ee0b2 2785 if (once && op != listop)
79072805
LW
2786 op->op_next = ((LOGOP*)cUNOP->op_first)->op_other;
2787
748a9306
LW
2788 if (op == listop)
2789 op = newUNOP(OP_NULL, 0, op); /* or do {} while 1 loses outer block */
2790
79072805 2791 op->op_flags |= flags;
a0d0e21e 2792 op = scope(op);
748a9306 2793 op->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/
a0d0e21e 2794 return op;
79072805
LW
2795}
2796
2797OP *
2798newWHILEOP(flags, debuggable, loop, expr, block, cont)
2799I32 flags;
2800I32 debuggable;
2801LOOP *loop;
2802OP *expr;
2803OP *block;
2804OP *cont;
2805{
2806 OP *redo;
2807 OP *next = 0;
2808 OP *listop;
2809 OP *op;
2810 OP *condop;
2811
748a9306
LW
2812 if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB)) {
2813 expr = newUNOP(OP_DEFINED, 0,
2814 newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) );
2815 }
79072805
LW
2816
2817 if (!block)
2818 block = newOP(OP_NULL, 0);
2819
2820 if (cont)
2821 next = LINKLIST(cont);
2822 if (expr)
2823 cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0));
2824
463ee0b2 2825 listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont);
79072805
LW
2826 redo = LINKLIST(listop);
2827
2828 if (expr) {
2829 op = newLOGOP(OP_AND, 0, expr, scalar(listop));
85e6fe83
LW
2830 if (op == expr && op->op_type == OP_CONST && !SvTRUE(cSVOP->op_sv)) {
2831 op_free(expr); /* oops, it's a while (0) */
463ee0b2
LW
2832 op_free((OP*)loop);
2833 return Nullop; /* (listop already freed by newLOGOP) */
2834 }
79072805
LW
2835 ((LISTOP*)listop)->op_last->op_next = condop =
2836 (op == listop ? redo : LINKLIST(op));
2837 if (!next)
2838 next = condop;
2839 }
2840 else
2841 op = listop;
2842
2843 if (!loop) {
2844 Newz(1101,loop,1,LOOP);
2845 loop->op_type = OP_ENTERLOOP;
2846 loop->op_ppaddr = ppaddr[OP_ENTERLOOP];
2847 loop->op_private = 0;
2848 loop->op_next = (OP*)loop;
2849 }
2850
463ee0b2 2851 op = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, op);
79072805
LW
2852
2853 loop->op_redoop = redo;
2854 loop->op_lastop = op;
2855
2856 if (next)
2857 loop->op_nextop = next;
2858 else
2859 loop->op_nextop = op;
2860
2861 op->op_flags |= flags;
c07a80fd 2862 op->op_private |= (flags >> 8);
79072805
LW
2863 return op;
2864}
2865
2866OP *
a0d0e21e 2867#ifndef CAN_PROTOTYPE
79072805
LW
2868newFOROP(flags,label,forline,sv,expr,block,cont)
2869I32 flags;
2870char *label;
2871line_t forline;
2872OP* sv;
2873OP* expr;
2874OP*block;
2875OP*cont;
8990e307
LW
2876#else
2877newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont)
a0d0e21e 2878#endif /* CAN_PROTOTYPE */
79072805
LW
2879{
2880 LOOP *loop;
85e6fe83 2881 int padoff = 0;
4633a7c4 2882 I32 iterflags = 0;
79072805
LW
2883
2884 copline = forline;
2885 if (sv) {
85e6fe83 2886 if (sv->op_type == OP_RV2SV) { /* symbol table variable */
748a9306
LW
2887 sv->op_type = OP_RV2GV;
2888 sv->op_ppaddr = ppaddr[OP_RV2GV];
79072805 2889 }
85e6fe83
LW
2890 else if (sv->op_type == OP_PADSV) { /* private variable */
2891 padoff = sv->op_targ;
2892 op_free(sv);
2893 sv = Nullop;
2894 }
79072805 2895 else
c07a80fd 2896 croak("Can't use %s for loop variable", op_desc[sv->op_type]);
79072805
LW
2897 }
2898 else {
2899 sv = newGVOP(OP_GV, 0, defgv);
2900 }
5f05dabc 2901 if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
4633a7c4
LW
2902 expr = scalar(ref(expr, OP_ITER));
2903 iterflags |= OPf_STACKED;
2904 }
2905 loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
a0d0e21e
LW
2906 append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
2907 scalar(sv))));
85e6fe83
LW
2908 assert(!loop->op_next);
2909 Renew(loop, 1, LOOP);
2910 loop->op_targ = padoff;
2911 return newSTATEOP(0, label, newWHILEOP(flags, 1, loop,
2912 newOP(OP_ITER, 0), block, cont));
79072805
LW
2913}
2914
8990e307
LW
2915OP*
2916newLOOPEX(type, label)
2917I32 type;
2918OP* label;
2919{
2920 OP *op;
2921 if (type != OP_GOTO || label->op_type == OP_CONST) {
a0d0e21e
LW
2922 op = newPVOP(type, 0, savepv(
2923 label->op_type == OP_CONST
2924 ? SvPVx(((SVOP*)label)->op_sv, na)
2925 : "" ));
8990e307
LW
2926 op_free(label);
2927 }
2928 else {
a0d0e21e
LW
2929 if (label->op_type == OP_ENTERSUB)
2930 label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN));
8990e307
LW
2931 op = newUNOP(type, OPf_STACKED, label);
2932 }
85e6fe83 2933 hints |= HINT_BLOCK_SCOPE;
8990e307
LW
2934 return op;
2935}
2936
79072805 2937void
85e6fe83 2938cv_undef(cv)
79072805
LW
2939CV *cv;
2940{
a0d0e21e
LW
2941 if (!CvXSUB(cv) && CvROOT(cv)) {
2942 if (CvDEPTH(cv))
2943 croak("Can't undef active subroutine");
8990e307 2944 ENTER;
a0d0e21e
LW
2945
2946 SAVESPTR(curpad);
2947 curpad = 0;
2948
a5f75d66 2949 if (!CvCLONED(cv))
748a9306 2950 op_free(CvROOT(cv));
79072805 2951 CvROOT(cv) = Nullop;
8990e307 2952 LEAVE;
79072805 2953 }
1d5db326 2954 SvPOK_off((SV*)cv); /* forget prototype */
44a8e56a 2955 CvFLAGS(cv) = 0;
8e07c86e
AD
2956 SvREFCNT_dec(CvGV(cv));
2957 CvGV(cv) = Nullgv;
2958 SvREFCNT_dec(CvOUTSIDE(cv));
2959 CvOUTSIDE(cv) = Nullcv;
2960 if (CvPADLIST(cv)) {
8ebc5c01
PP
2961 /* may be during global destruction */
2962 if (SvREFCNT(CvPADLIST(cv))) {
2963 I32 i = AvFILL(CvPADLIST(cv));
2964 while (i >= 0) {
2965 SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE);
46fc3d4c
PP
2966 SV* sv = svp ? *svp : Nullsv;
2967 if (!sv)
2968 continue;
2969 if (sv == (SV*)comppad_name)
2970 comppad_name = Nullav;
2971 else if (sv == (SV*)comppad) {
2972 comppad = Nullav;
2973 curpad = Null(SV**);
2974 }
2975 SvREFCNT_dec(sv);
8ebc5c01
PP
2976 }
2977 SvREFCNT_dec((SV*)CvPADLIST(cv));
8e07c86e 2978 }
8e07c86e
AD
2979 CvPADLIST(cv) = Nullav;
2980 }
79072805
LW
2981}
2982
5f05dabc
PP
2983#ifdef DEBUG_CLOSURES
2984static void
2985cv_dump(cv)
2986CV* cv;
2987{
2988 CV *outside = CvOUTSIDE(cv);
2989 AV* padlist = CvPADLIST(cv);
4fdae800
PP
2990 AV* pad_name;
2991 AV* pad;
2992 SV** pname;
2993 SV** ppad;
5f05dabc
PP
2994 I32 ix;
2995
2996 PerlIO_printf(Perl_debug_log, "\tCV=0x%p (%s), OUTSIDE=0x%p (%s)\n",
ab50184a
CS
2997 cv,
2998 (CvANON(cv) ? "ANON"
2999 : (cv == main_cv) ? "MAIN"
07055b4c 3000 : CvUNIQUE(outside) ? "UNIQUE"
44a8e56a 3001 : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"),
ab50184a
CS
3002 outside,
3003 (!outside ? "null"
3004 : CvANON(outside) ? "ANON"
3005 : (outside == main_cv) ? "MAIN"
07055b4c 3006 : CvUNIQUE(outside) ? "UNIQUE"
44a8e56a 3007 : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED"));
5f05dabc 3008
4fdae800
PP
3009 if (!padlist)
3010 return;
3011
3012 pad_name = (AV*)*av_fetch(padlist, 0, FALSE);
3013 pad = (AV*)*av_fetch(padlist, 1, FALSE);
3014 pname = AvARRAY(pad_name);
3015 ppad = AvARRAY(pad);
3016
9607fc9c 3017 for (ix = 1; ix <= AvFILL(pad_name); ix++) {
5f05dabc 3018 if (SvPOK(pname[ix]))
4fdae800
PP
3019 PerlIO_printf(Perl_debug_log, "\t%4d. 0x%p (%s\"%s\" %ld-%ld)\n",
3020 ix, ppad[ix],
3021 SvFAKE(pname[ix]) ? "FAKE " : "",
3022 SvPVX(pname[ix]),
ab50184a
CS
3023 (long)I_32(SvNVX(pname[ix])),
3024 (long)SvIVX(pname[ix]));
5f05dabc
PP
3025 }
3026}
3027#endif /* DEBUG_CLOSURES */
3028
3029static CV *
3030cv_clone2(proto, outside)
748a9306 3031CV* proto;
5f05dabc 3032CV* outside;
748a9306
LW
3033{
3034 AV* av;
3035 I32 ix;
3036 AV* protopadlist = CvPADLIST(proto);
3037 AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE);
3038 AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE);
5f05dabc
PP
3039 SV** pname = AvARRAY(protopad_name);
3040 SV** ppad = AvARRAY(protopad);
9607fc9c
PP
3041 I32 fname = AvFILL(protopad_name);
3042 I32 fpad = AvFILL(protopad);
748a9306
LW
3043 AV* comppadlist;
3044 CV* cv;
3045
07055b4c
CS
3046 assert(!CvUNIQUE(proto));
3047
748a9306
LW
3048 ENTER;
3049 SAVESPTR(curpad);
3050 SAVESPTR(comppad);
46fc3d4c 3051 SAVESPTR(comppad_name);
748a9306
LW
3052 SAVESPTR(compcv);
3053
3054 cv = compcv = (CV*)NEWSV(1104,0);
fa83b5b6 3055 sv_upgrade((SV *)cv, SvTYPE(proto));
a5f75d66 3056 CvCLONED_on(cv);
5f05dabc
PP
3057 if (CvANON(proto))
3058 CvANON_on(cv);
748a9306
LW
3059
3060 CvFILEGV(cv) = CvFILEGV(proto);
44a8e56a 3061 CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto));
748a9306
LW
3062 CvSTASH(cv) = CvSTASH(proto);
3063 CvROOT(cv) = CvROOT(proto);
3064 CvSTART(cv) = CvSTART(proto);
5f05dabc
PP
3065 if (outside)
3066 CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside);
748a9306 3067
68dc0745
PP
3068 if (SvPOK(proto))
3069 sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto));
3070
46fc3d4c
PP
3071 comppad_name = newAV();
3072 for (ix = fname; ix >= 0; ix--)
3073 av_store(comppad_name, ix, SvREFCNT_inc(pname[ix]));
3074
748a9306
LW
3075 comppad = newAV();
3076
3077 comppadlist = newAV();
3078 AvREAL_off(comppadlist);
46fc3d4c 3079 av_store(comppadlist, 0, (SV*)comppad_name);
b355b4e0 3080 av_store(comppadlist, 1, (SV*)comppad);
748a9306 3081 CvPADLIST(cv) = comppadlist;
5f05dabc 3082 av_fill(comppad, AvFILL(protopad));
748a9306
LW
3083 curpad = AvARRAY(comppad);
3084
3085 av = newAV(); /* will be @_ */
3086 av_extend(av, 0);
3087 av_store(comppad, 0, (SV*)av);
3088 AvFLAGS(av) = AVf_REIFY;
3089
9607fc9c
PP
3090 for (ix = fpad; ix > 0; ix--) {
3091 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
aa689395
PP
3092 if (namesv && namesv != &sv_undef) {
3093 char *name = SvPVX(namesv); /* XXX */
3094 if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */
3095 I32 off = pad_findlex(name, ix, SvIVX(namesv),
5f05dabc
PP
3096 CvOUTSIDE(cv), cxstack_ix);
3097 if (!off)
3098 curpad[ix] = SvREFCNT_inc(ppad[ix]);
3099 else if (off != ix)
748a9306
LW
3100 croak("panic: cv_clone: %s", name);
3101 }
3102 else { /* our own lexical */
aa689395 3103 SV* sv;
5f05dabc
PP
3104 if (*name == '&') {
3105 /* anon code -- we'll come back for it */
3106 sv = SvREFCNT_inc(ppad[ix]);
3107 }
3108 else if (*name == '@')
3109 sv = (SV*)newAV();
748a9306 3110 else if (*name == '%')
5f05dabc 3111 sv = (SV*)newHV();
748a9306 3112 else
5f05dabc
PP
3113 sv = NEWSV(0,0);
3114 if (!SvPADBUSY(sv))
3115 SvPADMY_on(sv);
3116 curpad[ix] = sv;
748a9306
LW
3117 }
3118 }
3119 else {
aa689395 3120 SV* sv = NEWSV(0,0);
748a9306 3121 SvPADTMP_on(sv);
5f05dabc 3122 curpad[ix] = sv;
748a9306
LW
3123 }
3124 }
3125
5f05dabc
PP
3126 /* Now that vars are all in place, clone nested closures. */
3127
9607fc9c
PP
3128 for (ix = fpad; ix > 0; ix--) {
3129 SV* namesv = (ix <= fname) ? pname[ix] : Nullsv;
aa689395
PP
3130 if (namesv
3131 && namesv != &sv_undef
3132 && !(SvFLAGS(namesv) & SVf_FAKE)
3133 && *SvPVX(namesv) == '&'
5f05dabc
PP
3134 && CvCLONE(ppad[ix]))
3135 {
3136 CV *kid = cv_clone2((CV*)ppad[ix], cv);
3137 SvREFCNT_dec(ppad[ix]);
3138 CvCLONE_on(kid);
3139 SvPADMY_on(kid);
3140 curpad[ix] = (SV*)kid;
3141 }
3142 }
3143
3144#ifdef DEBUG_CLOSURES
ab50184a
CS
3145 PerlIO_printf(Perl_debug_log, "Cloned inside:\n");
3146 cv_dump(outside);
3147 PerlIO_printf(Perl_debug_log, " from:\n");
5f05dabc 3148 cv_dump(proto);
ab50184a 3149 PerlIO_printf(Perl_debug_log, " to:\n");
5f05dabc
PP
3150 cv_dump(cv);
3151#endif
3152
748a9306
LW
3153 LEAVE;
3154 return cv;
3155}
3156
5f05dabc
PP
3157CV *
3158cv_clone(proto)
3159CV* proto;
3160{
3161 return cv_clone2(proto, CvOUTSIDE(proto));
3162}
3163
3fe9a6f1
PP
3164void
3165cv_ckproto(cv, gv, p)
3166CV* cv;
3167GV* gv;
3168char* p;
3169{
3170 if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) {
46fc3d4c 3171 SV* msg = sv_newmortal();
3fe9a6f1
PP
3172 SV* name = Nullsv;
3173
3174 if (gv)
46fc3d4c
PP
3175 gv_efullname3(name = sv_newmortal(), gv, Nullch);
3176 sv_setpv(msg, "Prototype mismatch:");
3177 if (name)
fc36a67e 3178 sv_catpvf(msg, " sub %_", name);
3fe9a6f1 3179 if (SvPOK(cv))
46fc3d4c
PP
3180 sv_catpvf(msg, " (%s)", SvPVX(cv));
3181 sv_catpv(msg, " vs ");
3182 if (p)
3183 sv_catpvf(msg, "(%s)", p);
3184 else
3185 sv_catpv(msg, "none");
fc36a67e 3186 warn("%_", msg);
3fe9a6f1
PP
3187 }
3188}
3189
760ac839
LW
3190SV *
3191cv_const_sv(cv)
3fe9a6f1 3192CV* cv;
760ac839
LW
3193{
3194 OP *o;
54310121 3195 SV *sv;
760ac839 3196
54310121
PP
3197 if (!cv || !SvPOK(cv) || SvCUR(cv))
3198 return Nullsv;
760ac839 3199
54310121
PP
3200 sv = Nullsv;
3201 for (o = CvSTART(cv); o; o = o->op_next) {
3202 OPCODE type = o->op_type;
3203
3204 if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
3205 continue;
3206 if (type == OP_LEAVESUB || type == OP_RETURN)
3207 break;
3208 if (sv)
3209 return Nullsv;
3210 if (type == OP_CONST)
760ac839 3211 sv = ((SVOP*)o)->op_sv;
54310121
PP
3212 else if (type == OP_PADSV) {
3213 AV* pad = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
3214 sv = pad ? AvARRAY(pad)[o->op_targ] : Nullsv;
5aabfad6 3215 if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
54310121 3216 return Nullsv;
760ac839 3217 }
54310121
PP
3218 else
3219 return Nullsv;
760ac839 3220 }
5aabfad6
PP
3221 if (sv)
3222 SvREADONLY_on(sv);
760ac839
LW
3223 return sv;
3224}
3225
748a9306 3226CV *
4633a7c4 3227newSUB(floor,op,proto,block)
79072805
LW
3228I32 floor;
3229OP *op;
4633a7c4 3230OP *proto;
79072805
LW
3231OP *block;
3232{
44a8e56a
PP
3233 char *name = op ? SvPVx(cSVOP->op_sv, na) : Nullch;
3234 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
3fe9a6f1 3235 char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
79072805 3236 register CV *cv;
a0d0e21e 3237 I32 ix;
79072805 3238
44a8e56a 3239 if (op)
8ebc5c01 3240 SAVEFREEOP(op);
3fe9a6f1
PP
3241 if (proto)
3242 SAVEFREEOP(proto);
3243
68dc0745
PP
3244 if (!name || GvCVGEN(gv))
3245 cv = Nullcv;
3246 else if (cv = GvCV(gv)) {
3fe9a6f1 3247 cv_ckproto(cv, gv, ps);
68dc0745
PP
3248 /* already defined (or promised)? */
3249 if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3250 SV* const_sv;
aa689395
PP
3251 if (!block) {
3252 /* just a "sub foo;" when &foo is already defined */
3253 SAVEFREESV(compcv);
3254 goto done;
3255 }
7bac28a0
PP
3256 /* ahem, death to those who redefine active sort subs */
3257 if (curstack == sortstack && sortcop == CvSTART(cv))
3258 croak("Can't redefine active sort subroutine %s", name);
68dc0745 3259 const_sv = cv_const_sv(cv);
44a8e56a 3260 if (const_sv || dowarn) {
79072805 3261 line_t oldline = curcop->cop_line;
79072805 3262 curcop->cop_line = copline;
760ac839 3263 warn(const_sv ? "Constant subroutine %s redefined"
68dc0745 3264 : "Subroutine %s redefined", name);
79072805
LW
3265 curcop->cop_line = oldline;
3266 }
8990e307 3267 SvREFCNT_dec(cv);
68dc0745 3268 cv = Nullcv;
79072805
LW
3269 }
3270 }
a0d0e21e 3271 if (cv) { /* must reuse cv if autoloaded */
4633a7c4 3272 cv_undef(cv);
44a8e56a 3273 CvFLAGS(cv) = CvFLAGS(compcv);
748a9306 3274 CvOUTSIDE(cv) = CvOUTSIDE(compcv);
e9a444f0 3275 CvOUTSIDE(compcv) = 0;
748a9306 3276 CvPADLIST(cv) = CvPADLIST(compcv);
4aa0a1f7 3277 CvPADLIST(compcv) = 0;
4633a7c4
LW
3278 if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */
3279 CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv);
748a9306 3280 SvREFCNT_dec(compcv);
a0d0e21e
LW
3281 }
3282 else {
748a9306 3283 cv = compcv;
44a8e56a
PP
3284 if (name) {
3285 GvCV(gv) = cv;
3286 GvCVGEN(gv) = 0;
3287 sub_generation++;
3288 }
a0d0e21e 3289 }
44a8e56a 3290 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
79072805 3291 CvFILEGV(cv) = curcop->cop_filegv;
8990e307
LW
3292 CvSTASH(cv) = curstash;
3293
3fe9a6f1
PP
3294 if (ps)
3295 sv_setpv((SV*)cv, ps);
4633a7c4 3296
c07a80fd
PP
3297 if (error_count) {
3298 op_free(block);
3299 block = Nullop;
68dc0745
PP
3300 if (name) {
3301 char *s = strrchr(name, ':');
3302 s = s ? s+1 : name;
6d4c2119
CS
3303 if (strEQ(s, "BEGIN")) {
3304 char *not_safe =
3305 "BEGIN not safe after errors--compilation aborted";
3306 if (in_eval & 4)
3307 croak(not_safe);
3308 else {
3309 /* force display of errors found but not reported */
3310 sv_catpv(GvSV(errgv), not_safe);
3311 croak("%s", SvPVx(GvSV(errgv), na));
3312 }
3313 }
68dc0745 3314 }
c07a80fd 3315 }
a0d0e21e 3316 if (!block) {
a0d0e21e
LW
3317 copline = NOLINE;
3318 LEAVE_SCOPE(floor);
3319 return cv;
3320 }
3321
54310121
PP
3322 if (AvFILL(comppad_name) < AvFILL(comppad))
3323 av_store(comppad_name, AvFILL(comppad), Nullsv);
a0d0e21e 3324
54310121
PP
3325 if (CvCLONE(cv)) {
3326 SV **namep = AvARRAY(comppad_name);
3327 for (ix = AvFILL(comppad); ix > 0; ix--) {
3328 SV *namesv;
3329
3330 if (SvIMMORTAL(curpad[ix]))
3331 continue;
3332 /*
3333 * The only things that a clonable function needs in its
3334 * pad are references to outer lexicals and anonymous subs.
3335 * The rest are created anew during cloning.
3336 */
3337 if (!((namesv = namep[ix]) != Nullsv &&
3338 namesv != &sv_undef &&
3339 (SvFAKE(namesv) ||
3340 *SvPVX(namesv) == '&')))
3341 {
3342 SvREFCNT_dec(curpad[ix]);
3343 curpad[ix] = Nullsv;
3344 }
3345 }
a0d0e21e 3346 }
54310121
PP
3347 else {
3348 AV *av = newAV(); /* Will be @_ */
3349 av_extend(av, 0);
3350 av_store(comppad, 0, (SV*)av);
3351 AvFLAGS(av) = AVf_REIFY;
79072805 3352
54310121
PP
3353 for (ix = AvFILL(comppad); ix > 0; ix--) {
3354 if (SvIMMORTAL(curpad[ix]))
3355 continue;
3356 if (!SvPADMY(curpad[ix]))
3357 SvPADTMP_on(curpad[ix]);
3358 }
3359 }
79072805 3360
a0d0e21e 3361 CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block));
79072805
LW
3362 CvSTART(cv) = LINKLIST(CvROOT(cv));
3363 CvROOT(cv)->op_next = 0;
3364 peep(CvSTART(cv));
a6006777 3365
44a8e56a
PP
3366 if (name) {
3367 char *s;
3368
84902520 3369 if (PERLDB_SUBLINE && curstash != debstash) {
46fc3d4c 3370 SV *sv = NEWSV(0,0);
44a8e56a
PP
3371 SV *tmpstr = sv_newmortal();
3372 static GV *db_postponed;
3373 CV *cv;
3374 HV *hv;
3375
fc36a67e 3376 sv_setpvf(sv, "%_:%ld-%ld",
46fc3d4c
PP
3377 GvSV(curcop->cop_filegv),
3378 (long)subline, (long)curcop->cop_line);
44a8e56a
PP
3379 gv_efullname3(tmpstr, gv, Nullch);
3380 hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0);
3381 if (!db_postponed) {
6bbcb22c 3382 db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV);
44a8e56a
PP
3383 }
3384 hv = GvHVn(db_postponed);
9607fc9c
PP
3385 if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr))
3386 && (cv = GvCV(db_postponed))) {
44a8e56a
PP
3387 dSP;
3388 PUSHMARK(sp);
3389 XPUSHs(tmpstr);
3390 PUTBACK;
3391 perl_call_sv((SV*)cv, G_DISCARD);
3392 }
3393 }
3394
3395 if ((s = strrchr(name,':')))
28757baa
PP
3396 s++;
3397 else
3398 s = name;
68dc0745 3399 if (strEQ(s, "BEGIN")) {
2ae324a7 3400 I32 oldscope = scopestack_ix;
28757baa
PP
3401 ENTER;
3402 SAVESPTR(compiling.cop_filegv);
3403 SAVEI16(compiling.cop_line);
3404 SAVEI32(perldb);
3405 save_svref(&rs);
3406 sv_setsv(rs, nrs);
3407
3408 if (!beginav)
3409 beginav = newAV();
3410 DEBUG_x( dump_sub(gv) );
3411 av_push(beginav, (SV *)cv);
3412 GvCV(gv) = 0;
68dc0745 3413 call_list(oldscope, beginav);
a6006777 3414
28757baa
PP
3415 curcop = &compiling;
3416 LEAVE;
3417 }
3418 else if (strEQ(s, "END") && !error_count) {
3419 if (!endav)
3420 endav = newAV();
3421 av_unshift(endav, 1);
3422 av_store(endav, 0, (SV *)cv);
3423 GvCV(gv) = 0;
3424 }
93a17b20 3425 }
a6006777 3426
aa689395 3427 done:
a6006777
PP
3428 copline = NOLINE;
3429 LEAVE_SCOPE(floor);
a0d0e21e 3430 return cv;
79072805
LW
3431}
3432
a0d0e21e
LW
3433#ifdef DEPRECATED
3434CV *
463ee0b2 3435newXSUB(name, ix, subaddr, filename)
79072805
LW
3436char *name;
3437I32 ix;
3438I32 (*subaddr)();
3439char *filename;
3440{
a0d0e21e 3441 CV* cv = newXS(name, (void(*)())subaddr, filename);
a5f75d66 3442 CvOLDSTYLE_on(cv);
a0d0e21e
LW
3443 CvXSUBANY(cv).any_i32 = ix;
3444 return cv;
3445}
3446#endif
3447
3448CV *
3449newXS(name, subaddr, filename)
3450char *name;
3451void (*subaddr) _((CV*));
3452char *filename;
3453{
44a8e56a 3454 GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
79072805 3455 register CV *cv;
44a8e56a
PP
3456
3457 if (cv = (name ? GvCV(gv) : Nullcv)) {
3458 if (GvCVGEN(gv)) {
3459 /* just a cached method */
3460 SvREFCNT_dec(cv);
3461 cv = 0;
3462 }
3463 else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
3464 /* already defined (or promised) */
a0d0e21e
LW
3465 if (dowarn) {
3466 line_t oldline = curcop->cop_line;
a0d0e21e
LW
3467 curcop->cop_line = copline;
3468 warn("Subroutine %s redefined",name);
3469 curcop->cop_line = oldline;
3470 }
3471 SvREFCNT_dec(cv);
3472 cv = 0;
79072805 3473 }
79072805 3474 }
44a8e56a
PP
3475
3476 if (cv) /* must reuse cv if autoloaded */
3477 cv_undef(cv);
a0d0e21e
LW
3478 else {
3479 cv = (CV*)NEWSV(1105,0);
3480 sv_upgrade((SV *)cv, SVt_PVCV);
44a8e56a
PP
3481 if (name) {
3482 GvCV(gv) = cv;
3483 GvCVGEN(gv) = 0;
3484 sub_generation++;
3485 }
a0d0e21e 3486 }
44a8e56a 3487 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
79072805 3488 CvFILEGV(cv) = gv_fetchfile(filename);
a0d0e21e 3489 CvXSUB(cv) = subaddr;
44a8e56a 3490
28757baa
PP
3491 if (name) {
3492 char *s = strrchr(name,':');
3493 if (s)
3494 s++;
3495 else
3496 s = name;
3497 if (strEQ(s, "BEGIN")) {
3498 if (!beginav)
3499 beginav = newAV();
44a8e56a
PP
3500 av_push(beginav, (SV *)cv);
3501 GvCV(gv) = 0;
28757baa
PP
3502 }
3503 else if (strEQ(s, "END")) {
3504 if (!endav)
3505 endav = newAV();
3506 av_unshift(endav, 1);
44a8e56a
PP
3507 av_store(endav, 0, (SV *)cv);
3508 GvCV(gv) = 0;
28757baa
PP
3509 }
3510 }
44a8e56a 3511 else
a5f75d66 3512 CvANON_on(cv);
44a8e56a 3513
a0d0e21e 3514 return cv;
79072805
LW
3515}
3516
3517void
3518newFORM(floor,op,block)
3519I32 floor;
3520OP *op;
3521OP *block;
3522{
3523 register CV *cv;
3524 char *name;
3525 GV *gv;
a0d0e21e 3526 I32 ix;
79072805
LW
3527
3528 if (op)
463ee0b2 3529 name = SvPVx(cSVOP->op_sv, na);
79072805
LW
3530 else
3531 name = "STDOUT";
85e6fe83 3532 gv = gv_fetchpv(name,TRUE, SVt_PVFM);
a5f75d66 3533 GvMULTI_on(gv);
79072805
LW
3534 if (cv = GvFORM(gv)) {
3535 if (dowarn) {
3536 line_t oldline = curcop->cop_line;
3537
3538 curcop->cop_line = copline;
3539 warn("Format %s redefined",name);
3540 curcop->cop_line = oldline;
3541 }
8990e307 3542 SvREFCNT_dec(cv);
79072805 3543 }
748a9306 3544 cv = compcv;
79072805 3545 GvFORM(gv) = cv;
44a8e56a 3546 CvGV(cv) = (GV*)SvREFCNT_inc(gv);
79072805
LW
3547 CvFILEGV(cv) = curcop->cop_filegv;
3548
a0d0e21e 3549 for (ix = AvFILL(comppad); ix > 0; ix--) {
44a8e56a 3550 if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix]))
a0d0e21e
LW
3551 SvPADTMP_on(curpad[ix]);
3552 }
3553
79072805
LW
3554 CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block));
3555 CvSTART(cv) = LINKLIST(CvROOT(cv));
3556 CvROOT(cv)->op_next = 0;
3557 peep(CvSTART(cv));
79072805
LW
3558 op_free(op);
3559 copline = NOLINE;
8990e307 3560 LEAVE_SCOPE(floor);
79072805
LW
3561}
3562
3563OP *
79072805
LW
3564newANONLIST(op)
3565OP* op;
3566{
93a17b20 3567 return newUNOP(OP_REFGEN, 0,
a0d0e21e 3568 mod(list(convert(OP_ANONLIST, 0, op)), OP_REFGEN));
79072805
LW
3569}
3570
3571OP *
3572newANONHASH(op)
3573OP* op;
3574{
93a17b20 3575 return newUNOP(OP_REFGEN, 0,
a0d0e21e
LW
3576 mod(list(convert(OP_ANONHASH, 0, op)), OP_REFGEN));
3577}
3578
3579OP *
4633a7c4 3580newANONSUB(floor, proto, block)
a0d0e21e 3581I32 floor;
4633a7c4 3582OP *proto;
a0d0e21e
LW
3583OP *block;
3584{
3585 return newUNOP(OP_REFGEN, 0,
4633a7c4 3586 newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block)));
79072805
LW
3587}
3588
3589OP *
3590oopsAV(o)
3591OP *o;
3592{
ed6116ce
LW
3593 switch (o->op_type) {
3594 case OP_PADSV:
3595 o->op_type = OP_PADAV;
3596 o->op_ppaddr = ppaddr[OP_PADAV];
3597 return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV);
3598
3599 case OP_RV2SV:
79072805
LW
3600 o->op_type = OP_RV2AV;
3601 o->op_ppaddr = ppaddr[OP_RV2AV];
3602 ref(o, OP_RV2AV);
ed6116ce
LW
3603 break;
3604
3605 default:
79072805 3606 warn("oops: oopsAV");
ed6116ce
LW
3607 break;
3608 }
79072805
LW
3609 return o;
3610}
3611
3612OP *
3613oopsHV(o)
3614OP *o;
3615{
ed6116ce
LW
3616 switch (o->op_type) {
3617 case OP_PADSV:
3618 case OP_PADAV:
3619 o->op_type = OP_PADHV;
3620 o->op_ppaddr = ppaddr[OP_PADHV];
3621 return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV);
3622
3623 case OP_RV2SV:
3624 case OP_RV2AV:
79072805
LW
3625 o->op_type = OP_RV2HV;
3626 o->op_ppaddr = ppaddr[OP_RV2HV];
3627 ref(o, OP_RV2HV);
ed6116ce
LW
3628 break;
3629
3630 default:
79072805 3631 warn("oops: oopsHV");
ed6116ce
LW
3632 break;
3633 }
79072805
LW
3634 return o;
3635}
3636
3637OP *
3638newAVREF(o)
3639OP *o;
3640{
ed6116ce
LW
3641 if (o->op_type == OP_PADANY) {
3642 o->op_type = OP_PADAV;
3643 o->op_ppaddr = ppaddr[OP_PADAV];
93a17b20 3644 return o;
ed6116ce 3645 }
79072805
LW
3646 return newUNOP(OP_RV2AV, 0, scalar(o));
3647}
3648
3649OP *
a0d0e21e
LW
3650newGVREF(type,o)
3651I32 type;
79072805
LW
3652OP *o;
3653{
a0d0e21e
LW
3654 if (type == OP_MAPSTART)
3655 return newUNOP(OP_NULL, 0, o);
748a9306 3656 return ref(newUNOP(OP_RV2GV, OPf_REF, o), type);
79072805
LW
3657}
3658
3659OP *
3660newHVREF(o)
3661OP *o;
3662{
ed6116ce
LW
3663 if (o->op_type == OP_PADANY) {
3664 o->op_type = OP_PADHV;
3665 o->op_ppaddr = ppaddr[OP_PADHV];
93a17b20 3666 return o;
ed6116ce 3667 }
79072805
LW
3668 return newUNOP(OP_RV2HV, 0, scalar(o));
3669}
3670
3671OP *
3672oopsCV(o)
3673OP *o;
3674{
463ee0b2 3675 croak("NOT IMPL LINE %d",__LINE__);
79072805
LW
3676 /* STUB */
3677 return o;
3678}
3679
3680OP *
c07a80fd
PP
3681newCVREF(flags, o)
3682I32 flags;
79072805
LW
3683OP *o;
3684{
c07a80fd 3685 return newUNOP(OP_RV2CV, flags, scalar(o));
79072805
LW
3686}
3687
3688OP *
3689newSVREF(o)
3690OP *o;
3691{
ed6116ce
LW
3692 if (o->op_type == OP_PADANY) {
3693 o->op_type = OP_PADSV;
3694 o->op_ppaddr = ppaddr[OP_PADSV];
93a17b20 3695 return o;
ed6116ce 3696 }
79072805
LW
3697 return newUNOP(OP_RV2SV, 0, scalar(o));
3698}
3699
3700/* Check routines. */
3701
3702OP *
5f05dabc
PP
3703ck_anoncode(op)
3704OP *op;
3705{
178c6305
CS
3706 PADOFFSET ix;
3707 SV* name;
3708
3709 name = NEWSV(1106,0);
3710 sv_upgrade(name, SVt_PVNV);
3711 sv_setpvn(name, "&", 1);
3712 SvIVX(name) = -1;
3713 SvNVX(name) = 1;
3714 ix = pad_alloc(op->op_type, SVs_PADMY);
3715 av_store(comppad_name, ix, name);
5f05dabc
PP
3716 av_store(comppad, ix, cSVOP->op_sv);
3717 SvPADMY_on(cSVOP->op_sv);
3718 cSVOP->op_sv = Nullsv;
3719 cSVOP->op_targ = ix;
3720 return op;
3721}
3722
3723OP *
55497cff
PP
3724ck_bitop(op)
3725OP *op;
3726{
3727 op->op_private = hints;
3728 return op;
3729}
3730
3731OP *
79072805
LW
3732ck_concat(op)
3733OP *op;
3734{
3735 if (cUNOP->op_first->op_type == OP_CONCAT)
3736 op->op_flags |= OPf_STACKED;
3737 return op;
3738}
3739
3740OP *
a0d0e21e 3741ck_spair(op)
79072805
LW
3742OP *op;
3743{
3744 if (op->op_flags & OPf_KIDS) {
3745 OP* newop;
a0d0e21e 3746 OP* kid;
8ebc5c01
PP
3747 OPCODE type = op->op_type;
3748 op = modkids(ck_fun(op), type);
a0d0e21e
LW
3749 kid = cUNOP->op_first;
3750 newop = kUNOP->op_first->op_sibling;
3751 if (newop &&
3752 (newop->op_sibling ||
3753 !(opargs[newop->op_type] & OA_RETSCALAR) ||
3754 newop->op_type == OP_PADAV || newop->op_type == OP_PADHV ||
3755 newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) {
3756
79072805 3757 return op;
a0d0e21e
LW
3758 }
3759 op_free(kUNOP->op_first);
3760 kUNOP->op_first = newop;
3761 }
3762 op->op_ppaddr = ppaddr[++op->op_type];
3763 return ck_fun(op);
3764}
3765
3766OP *
3767ck_delete(op)
3768OP *op;
3769{
3770 op = ck_fun(op);
5f05dabc 3771 op->op_private = 0;
a0d0e21e
LW
3772 if (op->op_flags & OPf_KIDS) {
3773 OP *kid = cUNOP->op_first;
5f05dabc
PP
3774 if (kid->op_type == OP_HSLICE)
3775 op->op_private |= OPpSLICE;
3776 else if (kid->op_type != OP_HELEM)
3777 croak("%s argument is not a HASH element or slice",
3778 op_desc[op->op_type]);
a0d0e21e 3779 null(kid);
79072805 3780 }
79072805
LW
3781 return op;
3782}
3783
3784OP *
3785ck_eof(op)
3786OP *op;
3787{
3788 I32 type = op->op_type;
3789
8990e307
LW
3790 if (op->op_flags & OPf_KIDS) {
3791 if (cLISTOP->op_first->op_type == OP_STUB) {
3792 op_free(op);
3793 op = newUNOP(type, OPf_SPECIAL,
85e6fe83 3794 newGVOP(OP_GV, 0, gv_fetchpv("main'ARGV", TRUE, SVt_PVAV)));
8990e307 3795 }
79072805 3796 return ck_fun(op);
79072805
LW
3797 }
3798 return op;
3799}
3800
3801OP *
3802ck_eval(op)
3803OP *op;
3804{
85e6fe83 3805 hints |= HINT_BLOCK_SCOPE;
79072805
LW
3806 if (op->op_flags & OPf_KIDS) {
3807 SVOP *kid = (SVOP*)cUNOP->op_first;
3808
93a17b20
LW
3809 if (!kid) {
3810 op->op_flags &= ~OPf_KIDS;
8990e307 3811 null(op);
79072805
LW
3812 }
3813 else if (kid->op_type == OP_LINESEQ) {
3814 LOGOP *enter;
3815
3816 kid->op_next = op->op_next;
3817 cUNOP->op_first = 0;
3818 op_free(op);
3819
3820 Newz(1101, enter, 1, LOGOP);
3821 enter->op_type = OP_ENTERTRY;
3822 enter->op_ppaddr = ppaddr[OP_ENTERTRY];
3823 enter->op_private = 0;
3824
3825 /* establish postfix order */
3826 enter->op_next = (OP*)enter;
3827
463ee0b2 3828 op = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid);
79072805
LW
3829 op->op_type = OP_LEAVETRY;
3830 op->op_ppaddr = ppaddr[OP_LEAVETRY];
3831 enter->op_other = op;
3832 return op;
3833 }
3834 }
3835 else {
3836 op_free(op);
3837 op = newUNOP(OP_ENTEREVAL, 0, newSVREF(newGVOP(OP_GV, 0, defgv)));
3838 }
a0d0e21e 3839 op->op_targ = (PADOFFSET)hints;
79072805
LW
3840 return op;
3841}
3842
3843OP *
3844ck_exec(op)
3845OP *op;
3846{
3847 OP *kid;
79072805 3848 if (op->op_flags & OPf_STACKED) {
463ee0b2 3849 op = ck_fun(op);
79072805 3850 kid = cUNOP->op_first->op_sibling;
8990e307
LW
3851 if (kid->op_type == OP_RV2GV)
3852 null(kid);
79072805 3853 }
463ee0b2
LW
3854 else
3855 op = listkids(op);
79072805
LW
3856 return op;
3857}
3858
3859OP *
5f05dabc
PP
3860ck_exists(op)
3861OP *op;
3862{
3863 op = ck_fun(op);
3864 if (op->op_flags & OPf_KIDS) {
3865 OP *kid = cUNOP->op_first;
3866 if (kid->op_type != OP_HELEM)
3867 croak("%s argument is not a HASH element", op_desc[op->op_type]);
3868 null(kid);
3869 }
3870 return op;
3871}
3872
3873OP *
79072805
LW
3874ck_gvconst(o)
3875register OP *o;
3876{
3877 o = fold_constants(o);
3878 if (o->op_type == OP_CONST)
3879 o->op_type = OP_GV;
3880 return o;
3881}
3882
3883OP *
3884ck_rvconst(op)
3885register OP *op;
3886{
3887 SVOP *kid = (SVOP*)cUNOP->op_first;
85e6fe83 3888
c07a80fd 3889 op->op_private |= (hints & HINT_STRICT_REFS);
79072805 3890 if (kid->op_type == OP_CONST) {
44a8e56a
PP
3891 char *name;
3892 int iscv;
3893 GV *gv;
3894
3895 name = SvPV(kid->op_sv, na);
3896 if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) {
3897 char *badthing = Nullch;
3898 switch (op->op_type) {
3899 case OP_RV2SV:
3900 badthing = "a SCALAR";
3901 break;
3902 case OP_RV2AV:
3903 badthing = "an ARRAY";
3904 break;
3905 case OP_RV2HV:
3906 badthing = "a HASH";
3907 break;
3908 }
3909 if (badthing)
3910 croak(
3911 "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use",
3912 name, badthing);
3913 }
79072805 3914 kid->op_type = OP_GV;
44a8e56a 3915 iscv = (op->op_type == OP_RV2CV) * 2;
a0d0e21e 3916 for (gv = 0; !gv; iscv++) {
748a9306
LW
3917 /*
3918 * This is a little tricky. We only want to add the symbol if we
3919 * didn't add it in the lexer. Otherwise we get duplicate strict
3920 * warnings. But if we didn't add it in the lexer, we must at
3921 * least pretend like we wanted to add it even if it existed before,
3922 * or we get possible typo warnings. OPpCONST_ENTERED says
3923 * whether the lexer already added THIS instance of this symbol.
3924 */
44a8e56a 3925 gv = gv_fetchpv(name,
748a9306 3926 iscv | !(kid->op_private & OPpCONST_ENTERED),
a0d0e21e
LW
3927 iscv
3928 ? SVt_PVCV
3929 : op->op_type == OP_RV2SV
3930 ? SVt_PV
3931 : op->op_type == OP_RV2AV
3932 ? SVt_PVAV
3933 : op->op_type == OP_RV2HV