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