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