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