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