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