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