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