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