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