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