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