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