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