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