This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
shup up warnings about dNOOP from gcc with __attribute__ ((unused))
[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 }
e476b1b5 154 if (ckWARN(WARN_MISC) && 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 {
e476b1b5 166 Perl_warner(aTHX_ WARN_MISC,
33633739
GS
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) {
1127039a 176 while (off <= top) {
33633739
GS
177 if ((sv = svp[off])
178 && sv != &PL_sv_undef
179 && ((SvFLAGS(sv) & SVpad_OUR) && GvSTASH(sv) == ourstash)
180 && strEQ(name, SvPVX(sv)))
f472eb5c 181 {
e476b1b5 182 Perl_warner(aTHX_ WARN_MISC,
33633739 183 "\"our\" variable %s redeclared", name);
e476b1b5 184 Perl_warner(aTHX_ WARN_MISC,
33633739
GS
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);
ef75a179 207 GvSTASH(sv) = (HV*)SvREFCNT_inc(PL_curstash ? (SV*)PL_curstash : (SV*)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 {
87d7fd28 1415 UNOP *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
87d7fd28 1422 NewOp(1101, newop, 1, UNOP);
349fd7b7
GS
1423 newop->op_type = OP_RV2CV;
1424 newop->op_ppaddr = PL_ppaddr[OP_RV2CV];
87d7fd28
GS
1425 newop->op_first = Nullop;
1426 newop->op_next = (OP*)newop;
1427 kid->op_sibling = (OP*)newop;
349fd7b7 1428 newop->op_private |= OPpLVAL_INTRO;
cd06dffe
GS
1429 break;
1430 }
1431
1432 if (kid->op_type != OP_RV2CV)
1433 Perl_croak(aTHX_
1434 "panic: unexpected lvalue entersub "
1435 "entry via type/targ %ld:%ld",
1436 (long)kid->op_type,kid->op_targ);
1437 kid->op_private |= OPpLVAL_INTRO;
1438 break; /* Postpone until runtime */
1439 }
1440
1441 okid = kid;
1442 kid = kUNOP->op_first;
1443 if (kid->op_type == OP_NULL && kid->op_targ == OP_RV2SV)
1444 kid = kUNOP->op_first;
1445 if (kid->op_type == OP_NULL)
1446 Perl_croak(aTHX_
1447 "Unexpected constant lvalue entersub "
1448 "entry via type/targ %ld:%ld",
1449 (long)kid->op_type,kid->op_targ);
1450 if (kid->op_type != OP_GV) {
1451 /* Restore RV2CV to check lvalueness */
1452 restore_2cv:
1453 if (kid->op_next && kid->op_next != kid) { /* Happens? */
1454 okid->op_next = kid->op_next;
1455 kid->op_next = okid;
1456 }
1457 else
1458 okid->op_next = Nullop;
1459 okid->op_type = OP_RV2CV;
1460 okid->op_targ = 0;
1461 okid->op_ppaddr = PL_ppaddr[OP_RV2CV];
1462 okid->op_private |= OPpLVAL_INTRO;
1463 break;
1464 }
1465
638eceb6 1466 cv = GvCV(kGVOP_gv);
cd06dffe
GS
1467 if (!cv)
1468 goto restore_2cv;
1469 if (CvLVALUE(cv))
1470 break;
1471 }
1472 }
79072805
LW
1473 /* FALL THROUGH */
1474 default:
a0d0e21e
LW
1475 nomod:
1476 /* grep, foreach, subcalls, refgen */
1477 if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN)
1478 break;
cea2e8a9 1479 yyerror(Perl_form(aTHX_ "Can't modify %s in %s",
638bc118 1480 (o->op_type == OP_NULL && (o->op_flags & OPf_SPECIAL)
cd06dffe
GS
1481 ? "do block"
1482 : (o->op_type == OP_ENTERSUB
1483 ? "non-lvalue subroutine call"
1484 : PL_op_desc[o->op_type])),
22c35a8c 1485 type ? PL_op_desc[type] : "local"));
11343788 1486 return o;
79072805 1487
a0d0e21e
LW
1488 case OP_PREINC:
1489 case OP_PREDEC:
1490 case OP_POW:
1491 case OP_MULTIPLY:
1492 case OP_DIVIDE:
1493 case OP_MODULO:
1494 case OP_REPEAT:
1495 case OP_ADD:
1496 case OP_SUBTRACT:
1497 case OP_CONCAT:
1498 case OP_LEFT_SHIFT:
1499 case OP_RIGHT_SHIFT:
1500 case OP_BIT_AND:
1501 case OP_BIT_XOR:
1502 case OP_BIT_OR:
1503 case OP_I_MULTIPLY:
1504 case OP_I_DIVIDE:
1505 case OP_I_MODULO:
1506 case OP_I_ADD:
1507 case OP_I_SUBTRACT:
11343788 1508 if (!(o->op_flags & OPf_STACKED))
a0d0e21e 1509 goto nomod;
3280af22 1510 PL_modcount++;
a0d0e21e
LW
1511 break;
1512
79072805 1513 case OP_COND_EXPR:
11343788 1514 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2 1515 mod(kid, type);
79072805
LW
1516 break;
1517
1518 case OP_RV2AV:
1519 case OP_RV2HV:
93af7a87 1520 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1521 Perl_croak(aTHX_ "Can't localize through a reference");
11343788 1522 if (type == OP_REFGEN && o->op_flags & OPf_PARENS) {
3280af22 1523 PL_modcount = 10000;
11343788 1524 return o; /* Treat \(@foo) like ordinary list. */
748a9306
LW
1525 }
1526 /* FALL THROUGH */
79072805 1527 case OP_RV2GV:
5dc0d613 1528 if (scalar_mod_type(o, type))
3fe9a6f1 1529 goto nomod;
11343788 1530 ref(cUNOPo->op_first, o->op_type);
79072805
LW
1531 /* FALL THROUGH */
1532 case OP_AASSIGN:
1533 case OP_ASLICE:
1534 case OP_HSLICE:
93a17b20
LW
1535 case OP_NEXTSTATE:
1536 case OP_DBSTATE:
a0d0e21e
LW
1537 case OP_REFGEN:
1538 case OP_CHOMP:
3280af22 1539 PL_modcount = 10000;
79072805 1540 break;
463ee0b2 1541 case OP_RV2SV:
11343788 1542 if (!type && cUNOPo->op_first->op_type != OP_GV)
cea2e8a9 1543 Perl_croak(aTHX_ "Can't localize through a reference");
aeea060c 1544 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1545 /* FALL THROUGH */
79072805 1546 case OP_GV:
463ee0b2 1547 case OP_AV2ARYLEN:
3280af22 1548 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1549 case OP_SASSIGN:
bf4b1e52
GS
1550 case OP_ANDASSIGN:
1551 case OP_ORASSIGN:
8990e307 1552 case OP_AELEMFAST:
3280af22 1553 PL_modcount++;
8990e307
LW
1554 break;
1555
748a9306
LW
1556 case OP_PADAV:
1557 case OP_PADHV:
3280af22 1558 PL_modcount = 10000;
5196be3e
MB
1559 if (type == OP_REFGEN && o->op_flags & OPf_PARENS)
1560 return o; /* Treat \(@foo) like ordinary list. */
1561 if (scalar_mod_type(o, type))
3fe9a6f1 1562 goto nomod;
748a9306
LW
1563 /* FALL THROUGH */
1564 case OP_PADSV:
3280af22 1565 PL_modcount++;
748a9306 1566 if (!type)
cea2e8a9 1567 Perl_croak(aTHX_ "Can't localize lexical variable %s",
2d8e6c8d 1568 SvPV(*av_fetch(PL_comppad_name, o->op_targ, 4), n_a));
463ee0b2
LW
1569 break;
1570
554b3eca 1571#ifdef USE_THREADS
2faa37cc 1572 case OP_THREADSV:
533c011a 1573 PL_modcount++; /* XXX ??? */
554b3eca
MB
1574 break;
1575#endif /* USE_THREADS */
1576
748a9306
LW
1577 case OP_PUSHMARK:
1578 break;
a0d0e21e 1579
69969c6f
SB
1580 case OP_KEYS:
1581 if (type != OP_SASSIGN)
1582 goto nomod;
5d82c453
GA
1583 goto lvalue_func;
1584 case OP_SUBSTR:
1585 if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
1586 goto nomod;
5f05dabc 1587 /* FALL THROUGH */
a0d0e21e 1588 case OP_POS:
463ee0b2 1589 case OP_VEC:
5d82c453 1590 lvalue_func:
11343788
MB
1591 pad_free(o->op_targ);
1592 o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
5dc0d613 1593 assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
11343788
MB
1594 if (o->op_flags & OPf_KIDS)
1595 mod(cBINOPo->op_first->op_sibling, type);
463ee0b2 1596 break;
a0d0e21e 1597
463ee0b2
LW
1598 case OP_AELEM:
1599 case OP_HELEM:
11343788 1600 ref(cBINOPo->op_first, o->op_type);
68dc0745 1601 if (type == OP_ENTERSUB &&
5dc0d613
MB
1602 !(o->op_private & (OPpLVAL_INTRO | OPpDEREF)))
1603 o->op_private |= OPpLVAL_DEFER;
3280af22 1604 PL_modcount++;
463ee0b2
LW
1605 break;
1606
1607 case OP_SCOPE:
1608 case OP_LEAVE:
1609 case OP_ENTER:
11343788
MB
1610 if (o->op_flags & OPf_KIDS)
1611 mod(cLISTOPo->op_last, type);
a0d0e21e
LW
1612 break;
1613
1614 case OP_NULL:
638bc118
GS
1615 if (o->op_flags & OPf_SPECIAL) /* do BLOCK */
1616 goto nomod;
1617 else if (!(o->op_flags & OPf_KIDS))
463ee0b2 1618 break;
11343788
MB
1619 if (o->op_targ != OP_LIST) {
1620 mod(cBINOPo->op_first, type);
a0d0e21e
LW
1621 break;
1622 }
1623 /* FALL THROUGH */
463ee0b2 1624 case OP_LIST:
11343788 1625 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1626 mod(kid, type);
1627 break;
1628 }
11343788 1629 o->op_flags |= OPf_MOD;
a0d0e21e
LW
1630
1631 if (type == OP_AASSIGN || type == OP_SASSIGN)
11343788 1632 o->op_flags |= OPf_SPECIAL|OPf_REF;
a0d0e21e 1633 else if (!type) {
11343788
MB
1634 o->op_private |= OPpLVAL_INTRO;
1635 o->op_flags &= ~OPf_SPECIAL;
3280af22 1636 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 1637 }
a0d0e21e 1638 else if (type != OP_GREPSTART && type != OP_ENTERSUB)
11343788
MB
1639 o->op_flags |= OPf_REF;
1640 return o;
463ee0b2
LW
1641}
1642
864dbfa3 1643STATIC bool
cea2e8a9 1644S_scalar_mod_type(pTHX_ OP *o, I32 type)
3fe9a6f1
PP
1645{
1646 switch (type) {
1647 case OP_SASSIGN:
5196be3e 1648 if (o->op_type == OP_RV2GV)
3fe9a6f1
PP
1649 return FALSE;
1650 /* FALL THROUGH */
1651 case OP_PREINC:
1652 case OP_PREDEC:
1653 case OP_POSTINC:
1654 case OP_POSTDEC:
1655 case OP_I_PREINC:
1656 case OP_I_PREDEC:
1657 case OP_I_POSTINC:
1658 case OP_I_POSTDEC:
1659 case OP_POW:
1660 case OP_MULTIPLY:
1661 case OP_DIVIDE:
1662 case OP_MODULO:
1663 case OP_REPEAT:
1664 case OP_ADD:
1665 case OP_SUBTRACT:
1666 case OP_I_MULTIPLY:
1667 case OP_I_DIVIDE:
1668 case OP_I_MODULO:
1669 case OP_I_ADD:
1670 case OP_I_SUBTRACT:
1671 case OP_LEFT_SHIFT:
1672 case OP_RIGHT_SHIFT:
1673 case OP_BIT_AND:
1674 case OP_BIT_XOR:
1675 case OP_BIT_OR:
1676 case OP_CONCAT:
1677 case OP_SUBST:
1678 case OP_TRANS:
49e9fbe6
GS
1679 case OP_READ:
1680 case OP_SYSREAD:
1681 case OP_RECV:
bf4b1e52
GS
1682 case OP_ANDASSIGN:
1683 case OP_ORASSIGN:
3fe9a6f1
PP
1684 return TRUE;
1685 default:
1686 return FALSE;
1687 }
1688}
1689
35cd451c 1690STATIC bool
cea2e8a9 1691S_is_handle_constructor(pTHX_ OP *o, I32 argnum)
35cd451c
GS
1692{
1693 switch (o->op_type) {
1694 case OP_PIPE_OP:
1695 case OP_SOCKPAIR:
1696 if (argnum == 2)
1697 return TRUE;
1698 /* FALL THROUGH */
1699 case OP_SYSOPEN:
1700 case OP_OPEN:
ded8aa31 1701 case OP_SELECT: /* XXX c.f. SelectSaver.pm */
35cd451c
GS
1702 case OP_SOCKET:
1703 case OP_OPEN_DIR:
1704 case OP_ACCEPT:
1705 if (argnum == 1)
1706 return TRUE;
1707 /* FALL THROUGH */
1708 default:
1709 return FALSE;
1710 }
1711}
1712
463ee0b2 1713OP *
864dbfa3 1714Perl_refkids(pTHX_ OP *o, I32 type)
463ee0b2
LW
1715{
1716 OP *kid;
11343788
MB
1717 if (o && o->op_flags & OPf_KIDS) {
1718 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
463ee0b2
LW
1719 ref(kid, type);
1720 }
11343788 1721 return o;
463ee0b2
LW
1722}
1723
1724OP *
864dbfa3 1725Perl_ref(pTHX_ OP *o, I32 type)
463ee0b2
LW
1726{
1727 OP *kid;
463ee0b2 1728
3280af22 1729 if (!o || PL_error_count)
11343788 1730 return o;
463ee0b2 1731
11343788 1732 switch (o->op_type) {
a0d0e21e 1733 case OP_ENTERSUB:
afebc493 1734 if ((type == OP_EXISTS || type == OP_DEFINED || type == OP_LOCK) &&
11343788
MB
1735 !(o->op_flags & OPf_STACKED)) {
1736 o->op_type = OP_RV2CV; /* entersub => rv2cv */
22c35a8c 1737 o->op_ppaddr = PL_ppaddr[OP_RV2CV];
11343788
MB
1738 assert(cUNOPo->op_first->op_type == OP_NULL);
1739 null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */
1740 o->op_flags |= OPf_SPECIAL;
8990e307
LW
1741 }
1742 break;
aeea060c 1743
463ee0b2 1744 case OP_COND_EXPR:
11343788 1745 for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling)
463ee0b2
LW
1746 ref(kid, type);
1747 break;
8990e307 1748 case OP_RV2SV:
35cd451c
GS
1749 if (type == OP_DEFINED)
1750 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1751 ref(cUNOPo->op_first, o->op_type);
4633a7c4
LW
1752 /* FALL THROUGH */
1753 case OP_PADSV:
5f05dabc 1754 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1755 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1756 : type == OP_RV2HV ? OPpDEREF_HV
1757 : OPpDEREF_SV);
11343788 1758 o->op_flags |= OPf_MOD;
a0d0e21e 1759 }
8990e307
LW
1760 break;
1761
2faa37cc 1762 case OP_THREADSV:
a863c7d1
MB
1763 o->op_flags |= OPf_MOD; /* XXX ??? */
1764 break;
1765
463ee0b2
LW
1766 case OP_RV2AV:
1767 case OP_RV2HV:
aeea060c 1768 o->op_flags |= OPf_REF;
8990e307 1769 /* FALL THROUGH */
463ee0b2 1770 case OP_RV2GV:
35cd451c
GS
1771 if (type == OP_DEFINED)
1772 o->op_flags |= OPf_SPECIAL; /* don't create GV */
11343788 1773 ref(cUNOPo->op_first, o->op_type);
463ee0b2 1774 break;
8990e307 1775
463ee0b2
LW
1776 case OP_PADAV:
1777 case OP_PADHV:
aeea060c 1778 o->op_flags |= OPf_REF;
79072805 1779 break;
aeea060c 1780
8990e307 1781 case OP_SCALAR:
79072805 1782 case OP_NULL:
11343788 1783 if (!(o->op_flags & OPf_KIDS))
463ee0b2 1784 break;
11343788 1785 ref(cBINOPo->op_first, type);
79072805
LW
1786 break;
1787 case OP_AELEM:
1788 case OP_HELEM:
11343788 1789 ref(cBINOPo->op_first, o->op_type);
5f05dabc 1790 if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) {
5dc0d613
MB
1791 o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV
1792 : type == OP_RV2HV ? OPpDEREF_HV
1793 : OPpDEREF_SV);
11343788 1794 o->op_flags |= OPf_MOD;
8990e307 1795 }
79072805
LW
1796 break;
1797
463ee0b2 1798 case OP_SCOPE:
79072805
LW
1799 case OP_LEAVE:
1800 case OP_ENTER:
8990e307 1801 case OP_LIST:
11343788 1802 if (!(o->op_flags & OPf_KIDS))
79072805 1803 break;
11343788 1804 ref(cLISTOPo->op_last, type);
79072805 1805 break;
a0d0e21e
LW
1806 default:
1807 break;
79072805 1808 }
11343788 1809 return scalar(o);
8990e307 1810
79072805
LW
1811}
1812
09bef843
SB
1813STATIC OP *
1814S_dup_attrlist(pTHX_ OP *o)
1815{
1816 OP *rop = Nullop;
1817
1818 /* An attrlist is either a simple OP_CONST or an OP_LIST with kids,
1819 * where the first kid is OP_PUSHMARK and the remaining ones
1820 * are OP_CONST. We need to push the OP_CONST values.
1821 */
1822 if (o->op_type == OP_CONST)
1823 rop = newSVOP(OP_CONST, o->op_flags, SvREFCNT_inc(cSVOPo->op_sv));
1824 else {
1825 assert((o->op_type == OP_LIST) && (o->op_flags & OPf_KIDS));
1826 for (o = cLISTOPo->op_first; o; o=o->op_sibling) {
1827 if (o->op_type == OP_CONST)
1828 rop = append_elem(OP_LIST, rop,
1829 newSVOP(OP_CONST, o->op_flags,
1830 SvREFCNT_inc(cSVOPo->op_sv)));
1831 }
1832 }
1833 return rop;
1834}
1835
1836STATIC void
1837S_apply_attrs(pTHX_ HV *stash, SV *target, OP *attrs)
1838{
1839 OP *modname; /* for 'use' */
1840 SV *stashsv;
1841
1842 /* fake up C<use attributes $pkg,$rv,@attrs> */
1843 ENTER; /* need to protect against side-effects of 'use' */
1844 SAVEINT(PL_expect);
1845 if (stash && HvNAME(stash))
1846 stashsv = newSVpv(HvNAME(stash), 0);
1847 else
1848 stashsv = &PL_sv_no;
1849#define ATTRSMODULE "attributes"
1850 modname = newSVOP(OP_CONST, 0,
1851 newSVpvn(ATTRSMODULE, sizeof(ATTRSMODULE)-1));
1852 modname->op_private |= OPpCONST_BARE;
1853 /* that flag is required to make 'use' work right */
1854 utilize(1, start_subparse(FALSE, 0),
1855 Nullop, /* version */
1856 modname,
1857 prepend_elem(OP_LIST,
1858 newSVOP(OP_CONST, 0, stashsv),
1859 prepend_elem(OP_LIST,
1860 newSVOP(OP_CONST, 0, newRV(target)),
1861 dup_attrlist(attrs))));
1862 LEAVE;
1863}
1864
1865STATIC OP *
1866S_my_kid(pTHX_ OP *o, OP *attrs)
93a17b20
LW
1867{
1868 OP *kid;
93a17b20
LW
1869 I32 type;
1870
3280af22 1871 if (!o || PL_error_count)
11343788 1872 return o;
93a17b20 1873
11343788 1874 type = o->op_type;
93a17b20 1875 if (type == OP_LIST) {
11343788 1876 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
09bef843 1877 my_kid(kid, attrs);
dab48698 1878 } else if (type == OP_UNDEF) {
7766148a 1879 return o;
77ca0c92
LW
1880 } else if (type == OP_RV2SV || /* "our" declaration */
1881 type == OP_RV2AV ||
1882 type == OP_RV2HV) { /* XXX does this let anything illegal in? */
192587c2 1883 o->op_private |= OPpOUR_INTRO;
77ca0c92 1884 return o;
dab48698 1885 } else if (type != OP_PADSV &&
93a17b20
LW
1886 type != OP_PADAV &&
1887 type != OP_PADHV &&
1888 type != OP_PUSHMARK)
1889 {
eb64745e
GS
1890 yyerror(Perl_form(aTHX_ "Can't declare %s in \"%s\"",
1891 PL_op_desc[o->op_type],
1892 PL_in_my == KEY_our ? "our" : "my"));
11343788 1893 return o;
93a17b20 1894 }
09bef843
SB
1895 else if (attrs && type != OP_PUSHMARK) {
1896 HV *stash;
1897 SV *padsv;
1898 SV **namesvp;
1899
eb64745e
GS
1900 PL_in_my = FALSE;
1901 PL_in_my_stash = Nullhv;
1902
09bef843
SB
1903 /* check for C<my Dog $spot> when deciding package */
1904 namesvp = av_fetch(PL_comppad_name, o->op_targ, FALSE);
1905 if (namesvp && *namesvp && SvOBJECT(*namesvp) && HvNAME(SvSTASH(*namesvp)))
1906 stash = SvSTASH(*namesvp);
1907 else
1908 stash = PL_curstash;
1909 padsv = PAD_SV(o->op_targ);
1910 apply_attrs(stash, padsv, attrs);
1911 }
11343788
MB
1912 o->op_flags |= OPf_MOD;
1913 o->op_private |= OPpLVAL_INTRO;
1914 return o;
93a17b20
LW
1915}
1916
1917OP *
09bef843
SB
1918Perl_my_attrs(pTHX_ OP *o, OP *attrs)
1919{
1920 if (o->op_flags & OPf_PARENS)
1921 list(o);
09bef843
SB
1922 if (attrs)
1923 SAVEFREEOP(attrs);
eb64745e
GS
1924 o = my_kid(o, attrs);
1925 PL_in_my = FALSE;
1926 PL_in_my_stash = Nullhv;
1927 return o;
09bef843
SB
1928}
1929
1930OP *
1931Perl_my(pTHX_ OP *o)
1932{
1933 return my_kid(o, Nullop);
1934}
1935
1936OP *
864dbfa3 1937Perl_sawparens(pTHX_ OP *o)
79072805
LW
1938{
1939 if (o)
1940 o->op_flags |= OPf_PARENS;
1941 return o;
1942}
1943
1944OP *
864dbfa3 1945Perl_bind_match(pTHX_ I32 type, OP *left, OP *right)
79072805 1946{
d008e5eb 1947 dTHR;
11343788 1948 OP *o;
79072805 1949
e476b1b5 1950 if (ckWARN(WARN_MISC) &&
599cee73
PM
1951 (left->op_type == OP_RV2AV ||
1952 left->op_type == OP_RV2HV ||
1953 left->op_type == OP_PADAV ||
1954 left->op_type == OP_PADHV)) {
22c35a8c 1955 char *desc = PL_op_desc[(right->op_type == OP_SUBST ||
599cee73
PM
1956 right->op_type == OP_TRANS)
1957 ? right->op_type : OP_MATCH];
dff6d3cd
GS
1958 const char *sample = ((left->op_type == OP_RV2AV ||
1959 left->op_type == OP_PADAV)
1960 ? "@array" : "%hash");
e476b1b5 1961 Perl_warner(aTHX_ WARN_MISC,
599cee73
PM
1962 "Applying %s to %s will act on scalar(%s)",
1963 desc, sample, sample);
2ae324a7
PP
1964 }
1965
79072805
LW
1966 if (right->op_type == OP_MATCH ||
1967 right->op_type == OP_SUBST ||
1968 right->op_type == OP_TRANS) {
1969 right->op_flags |= OPf_STACKED;
1970 if (right->op_type != OP_MATCH)
463ee0b2 1971 left = mod(left, right->op_type);
79072805 1972 if (right->op_type == OP_TRANS)
11343788 1973 o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right);
79072805 1974 else
11343788 1975 o = prepend_elem(right->op_type, scalar(left), right);
79072805 1976 if (type == OP_NOT)
11343788
MB
1977 return newUNOP(OP_NOT, 0, scalar(o));
1978 return o;
79072805
LW
1979 }
1980 else
1981 return bind_match(type, left,
1982 pmruntime(newPMOP(OP_MATCH, 0), right, Nullop));
1983}
1984
1985OP *
864dbfa3 1986Perl_invert(pTHX_ OP *o)
79072805 1987{
11343788
MB
1988 if (!o)
1989 return o;
79072805 1990 /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */
11343788 1991 return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o));
79072805
LW
1992}
1993
1994OP *
864dbfa3 1995Perl_scope(pTHX_ OP *o)
79072805
LW
1996{
1997 if (o) {
3280af22 1998 if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || PL_tainting) {
463ee0b2
LW
1999 o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o);
2000 o->op_type = OP_LEAVE;
22c35a8c 2001 o->op_ppaddr = PL_ppaddr[OP_LEAVE];
463ee0b2
LW
2002 }
2003 else {
2004 if (o->op_type == OP_LINESEQ) {
2005 OP *kid;
2006 o->op_type = OP_SCOPE;
22c35a8c 2007 o->op_ppaddr = PL_ppaddr[OP_SCOPE];
c3ed7a6a
GS
2008 kid = ((LISTOP*)o)->op_first;
2009 if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE)
2010 null(kid);
463ee0b2
LW
2011 }
2012 else
748a9306 2013 o = newLISTOP(OP_SCOPE, 0, o, Nullop);
463ee0b2 2014 }
79072805
LW
2015 }
2016 return o;
2017}
2018
b3ac6de7 2019void
864dbfa3 2020Perl_save_hints(pTHX)
b3ac6de7 2021{
3280af22
NIS
2022 SAVEI32(PL_hints);
2023 SAVESPTR(GvHV(PL_hintgv));
2024 GvHV(PL_hintgv) = newHVhv(GvHV(PL_hintgv));
2025 SAVEFREESV(GvHV(PL_hintgv));
b3ac6de7
IZ
2026}
2027
a0d0e21e 2028int
864dbfa3 2029Perl_block_start(pTHX_ int full)
79072805 2030{
11343788 2031 dTHR;
3280af22 2032 int retval = PL_savestack_ix;
b3ac6de7 2033
3280af22 2034 SAVEI32(PL_comppad_name_floor);
43d4d5c6
GS
2035 PL_comppad_name_floor = AvFILLp(PL_comppad_name);
2036 if (full)
2037 PL_comppad_name_fill = PL_comppad_name_floor;
2038 if (PL_comppad_name_floor < 0)
2039 PL_comppad_name_floor = 0;
3280af22
NIS
2040 SAVEI32(PL_min_intro_pending);
2041 SAVEI32(PL_max_intro_pending);
2042 PL_min_intro_pending = 0;
2043 SAVEI32(PL_comppad_name_fill);
2044 SAVEI32(PL_padix_floor);
2045 PL_padix_floor = PL_padix;
2046 PL_pad_reset_pending = FALSE;
b3ac6de7 2047 SAVEHINTS();
3280af22 2048 PL_hints &= ~HINT_BLOCK_SCOPE;
7766f137 2049 SAVESPTR(PL_compiling.cop_warnings);
0453d815 2050 if (! specialWARN(PL_compiling.cop_warnings)) {
599cee73
PM
2051 PL_compiling.cop_warnings = newSVsv(PL_compiling.cop_warnings) ;
2052 SAVEFREESV(PL_compiling.cop_warnings) ;
2053 }
a0d0e21e
LW
2054 return retval;
2055}
2056
2057OP*
864dbfa3 2058Perl_block_end(pTHX_ I32 floor, OP *seq)
a0d0e21e 2059{
11343788 2060 dTHR;
3280af22 2061 int needblockscope = PL_hints & HINT_BLOCK_SCOPE;
a0d0e21e 2062 OP* retval = scalarseq(seq);
a0d0e21e 2063 LEAVE_SCOPE(floor);
3280af22 2064 PL_pad_reset_pending = FALSE;
e24b16f9 2065 PL_compiling.op_private = PL_hints;
a0d0e21e 2066 if (needblockscope)
3280af22
NIS
2067 PL_hints |= HINT_BLOCK_SCOPE; /* propagate out */
2068 pad_leavemy(PL_comppad_name_fill);
2069 PL_cop_seqmax++;
a0d0e21e
LW
2070 return retval;
2071}
2072
76e3520e 2073STATIC OP *
cea2e8a9 2074S_newDEFSVOP(pTHX)
54b9620d
MB
2075{
2076#ifdef USE_THREADS
2077 OP *o = newOP(OP_THREADSV, 0);
2078 o->op_targ = find_threadsv("_");
2079 return o;
2080#else
3280af22 2081 return newSVREF(newGVOP(OP_GV, 0, PL_defgv));
54b9620d
MB
2082#endif /* USE_THREADS */
2083}
2084
a0d0e21e 2085void
864dbfa3 2086Perl_newPROG(pTHX_ OP *o)
a0d0e21e 2087{
11343788 2088 dTHR;
3280af22 2089 if (PL_in_eval) {
b295d113
TH
2090 if (PL_eval_root)
2091 return;
faef0170
HS
2092 PL_eval_root = newUNOP(OP_LEAVEEVAL,
2093 ((PL_in_eval & EVAL_KEEPERR)
2094 ? OPf_SPECIAL : 0), o);
3280af22 2095 PL_eval_start = linklist(PL_eval_root);
7934575e
GS
2096 PL_eval_root->op_private |= OPpREFCOUNTED;
2097 OpREFCNT_set(PL_eval_root, 1);
3280af22
NIS
2098 PL_eval_root->op_next = 0;
2099 peep(PL_eval_start);
a0d0e21e
LW
2100 }
2101 else {
5dc0d613 2102 if (!o)
a0d0e21e 2103 return;
3280af22
NIS
2104 PL_main_root = scope(sawparens(scalarvoid(o)));
2105 PL_curcop = &PL_compiling;
2106 PL_main_start = LINKLIST(PL_main_root);
7934575e
GS
2107 PL_main_root->op_private |= OPpREFCOUNTED;
2108 OpREFCNT_set(PL_main_root, 1);
3280af22
NIS
2109 PL_main_root->op_next = 0;
2110 peep(PL_main_start);
2111 PL_compcv = 0;
3841441e 2112
4fdae800 2113 /* Register with debugger */
84902520 2114 if (PERLDB_INTER) {
864dbfa3 2115 CV *cv = get_cv("DB::postponed", FALSE);
3841441e
CS
2116 if (cv) {
2117 dSP;
924508f0 2118 PUSHMARK(SP);
cc49e20b 2119 XPUSHs((SV*)CopFILEGV(&PL_compiling));
3841441e 2120 PUTBACK;
864dbfa3 2121 call_sv((SV*)cv, G_DISCARD);
3841441e
CS
2122 }
2123 }
79072805 2124 }
79072805
LW
2125}
2126
2127OP *
864dbfa3 2128Perl_localize(pTHX_ OP *o, I32 lex)
79072805
LW
2129{
2130 if (o->op_flags & OPf_PARENS)
2131 list(o);
8990e307 2132 else {
d008e5eb 2133 dTHR;
599cee73 2134 if (ckWARN(WARN_PARENTHESIS) && PL_bufptr > PL_oldbufptr && PL_bufptr[-1] == ',') {
8990e307 2135 char *s;
834a4ddd 2136 for (s = PL_bufptr; *s && (isALNUM(*s) || (*s & 0x80) || strchr("@$%, ",*s)); s++) ;
a0d0e21e 2137 if (*s == ';' || *s == '=')
eb64745e
GS
2138 Perl_warner(aTHX_ WARN_PARENTHESIS,
2139 "Parentheses missing around \"%s\" list",
2140 lex ? (PL_in_my == KEY_our ? "our" : "my") : "local");
8990e307
LW
2141 }
2142 }
93a17b20 2143 if (lex)
eb64745e 2144 o = my(o);
93a17b20 2145 else
eb64745e
GS
2146 o = mod(o, OP_NULL); /* a bit kludgey */
2147 PL_in_my = FALSE;
2148 PL_in_my_stash = Nullhv;
2149 return o;
79072805
LW
2150}
2151
2152OP *
864dbfa3 2153Perl_jmaybe(pTHX_ OP *o)
79072805
LW
2154{
2155 if (o->op_type == OP_LIST) {
554b3eca
MB
2156 OP *o2;
2157#ifdef USE_THREADS
2faa37cc 2158 o2 = newOP(OP_THREADSV, 0);
54b9620d 2159 o2->op_targ = find_threadsv(";");
554b3eca
MB
2160#else
2161 o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))),
2162#endif /* USE_THREADS */
2163 o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o));
79072805
LW
2164 }
2165 return o;
2166}
2167
2168OP *
864dbfa3 2169Perl_fold_constants(pTHX_ register OP *o)
79072805 2170{
11343788 2171 dTHR;
79072805
LW
2172 register OP *curop;
2173 I32 type = o->op_type;
748a9306 2174 SV *sv;
79072805 2175
22c35a8c 2176 if (PL_opargs[type] & OA_RETSCALAR)
79072805 2177 scalar(o);
b162f9ea 2178 if (PL_opargs[type] & OA_TARGET && !o->op_targ)
ed6116ce 2179 o->op_targ = pad_alloc(type, SVs_PADTMP);
79072805 2180
eac055e9
GS
2181 /* integerize op, unless it happens to be C<-foo>.
2182 * XXX should pp_i_negate() do magic string negation instead? */
2183 if ((PL_opargs[type] & OA_OTHERINT) && (PL_hints & HINT_INTEGER)
2184 && !(type == OP_NEGATE && cUNOPo->op_first->op_type == OP_CONST
2185 && (cUNOPo->op_first->op_private & OPpCONST_BARE)))
2186 {
22c35a8c 2187 o->op_ppaddr = PL_ppaddr[type = ++(o->op_type)];
eac055e9 2188 }
85e6fe83 2189
22c35a8c 2190 if (!(PL_opargs[type] & OA_FOLDCONST))
79072805
LW
2191 goto nope;
2192
de939608 2193 switch (type) {
7a52d87a
GS
2194 case OP_NEGATE:
2195 /* XXX might want a ck_negate() for this */
2196 cUNOPo->op_first->op_private &= ~OPpCONST_STRICT;
2197 break;
de939608
CS
2198 case OP_SPRINTF:
2199 case OP_UCFIRST:
2200 case OP_LCFIRST:
2201 case OP_UC:
2202 case OP_LC:
69dcf70c
MB
2203 case OP_SLT:
2204 case OP_SGT:
2205 case OP_SLE:
2206 case OP_SGE:
2207 case OP_SCMP:
2208
de939608
CS
2209 if (o->op_private & OPpLOCALE)
2210 goto nope;
2211 }
2212
3280af22 2213 if (PL_error_count)
a0d0e21e
LW
2214 goto nope; /* Don't try to run w/ errors */
2215
79072805 2216 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
11fa937b
GS
2217 if ((curop->op_type != OP_CONST ||
2218 (curop->op_private & OPpCONST_BARE)) &&
7a52d87a
GS
2219 curop->op_type != OP_LIST &&
2220 curop->op_type != OP_SCALAR &&
2221 curop->op_type != OP_NULL &&
2222 curop->op_type != OP_PUSHMARK)
2223 {
79072805
LW
2224 goto nope;
2225 }
2226 }
2227
2228 curop = LINKLIST(o);
2229 o->op_next = 0;
533c011a 2230 PL_op = curop;
cea2e8a9 2231 CALLRUNOPS(aTHX);
3280af22 2232 sv = *(PL_stack_sp--);
748a9306 2233 if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */
79072805 2234 pad_swipe(o->op_targ);
748a9306
LW
2235 else if (SvTEMP(sv)) { /* grab mortal temp? */
2236 (void)SvREFCNT_inc(sv);
2237 SvTEMP_off(sv);
85e6fe83 2238 }
79072805
LW
2239 op_free(o);
2240 if (type == OP_RV2GV)
b1cb66bf 2241 return newGVOP(OP_GV, 0, (GV*)sv);
748a9306 2242 else {
ee580363
GS
2243 /* try to smush double to int, but don't smush -2.0 to -2 */
2244 if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
2245 type != OP_NEGATE)
2246 {
748a9306 2247 IV iv = SvIV(sv);
65202027 2248 if ((NV)iv == SvNV(sv)) {
748a9306
LW
2249 SvREFCNT_dec(sv);
2250 sv = newSViv(iv);
2251 }
b1cb66bf
PP
2252 else
2253 SvIOK_off(sv); /* undo SvIV() damage */
748a9306
LW
2254 }
2255 return newSVOP(OP_CONST, 0, sv);
2256 }
aeea060c 2257
79072805 2258 nope:
22c35a8c 2259 if (!(PL_opargs[type] & OA_OTHERINT))
79072805 2260 return o;
79072805 2261
3280af22 2262 if (!(PL_hints & HINT_INTEGER)) {
4bb9f687
GS
2263 if (type == OP_MODULO
2264 || type == OP_DIVIDE
2265 || !(o->op_flags & OPf_KIDS))
2266 {
85e6fe83 2267 return o;
4bb9f687 2268 }
85e6fe83
LW
2269
2270 for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) {
2271 if (curop->op_type == OP_CONST) {
b1cb66bf 2272 if (SvIOK(((SVOP*)curop)->op_sv))
85e6fe83
LW
2273 continue;
2274 return o;
2275 }
22c35a8c 2276 if (PL_opargs[curop->op_type] & OA_RETINTEGER)
79072805
LW
2277 continue;
2278 return o;
2279 }
22c35a8c 2280 o->op_ppaddr = PL_ppaddr[++(o->op_type)];
79072805
LW
2281 }
2282
79072805
LW
2283 return o;
2284}
2285
2286OP *
864dbfa3 2287Perl_gen_constant_list(pTHX_ register OP *o)
79072805 2288{
11343788 2289 dTHR;
79072805 2290 register OP *curop;
3280af22 2291 I32 oldtmps_floor = PL_tmps_floor;
79072805 2292
a0d0e21e 2293 list(o);
3280af22 2294 if (PL_error_count)
a0d0e21e
LW
2295 return o; /* Don't attempt to run with errors */
2296
533c011a 2297 PL_op = curop = LINKLIST(o);
a0d0e21e 2298 o->op_next = 0;
7d4045d4 2299 peep(curop);
cea2e8a9
GS
2300 pp_pushmark();
2301 CALLRUNOPS(aTHX);
533c011a 2302 PL_op = curop;
cea2e8a9 2303 pp_anonlist();
3280af22 2304 PL_tmps_floor = oldtmps_floor;
79072805
LW
2305
2306 o->op_type = OP_RV2AV;
22c35a8c 2307 o->op_ppaddr = PL_ppaddr[OP_RV2AV];
79072805 2308 curop = ((UNOP*)o)->op_first;
3280af22 2309 ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*PL_stack_sp--));
79072805 2310 op_free(curop);
79072805
LW
2311 linklist(o);
2312 return list(o);
2313}
2314
2315OP *
864dbfa3 2316Perl_convert(pTHX_ I32 type, I32 flags, OP *o)
79072805
LW
2317{
2318 OP *kid;
a0d0e21e 2319 OP *last = 0;
79072805 2320
11343788
MB
2321 if (!o || o->op_type != OP_LIST)
2322 o = newLISTOP(OP_LIST, 0, o, Nullop);
748a9306 2323 else
5dc0d613 2324 o->op_flags &= ~OPf_WANT;
79072805 2325
22c35a8c 2326 if (!(PL_opargs[type] & OA_MARK))
11343788 2327 null(cLISTOPo->op_first);
8990e307 2328
11343788 2329 o->op_type = type;
22c35a8c 2330 o->op_ppaddr = PL_ppaddr[type];
11343788 2331 o->op_flags |= flags;
79072805 2332
11343788
MB
2333 o = CHECKOP(type, o);
2334 if (o->op_type != type)
2335 return o;
79072805 2336
11343788 2337 if (cLISTOPo->op_children < 7) {
79072805 2338 /* XXX do we really need to do this if we're done appending?? */
11343788 2339 for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling)
79072805 2340 last = kid;
11343788 2341 cLISTOPo->op_last = last; /* in case check substituted last arg */
79072805
LW
2342 }
2343
11343788 2344 return fold_constants(o);
79072805
LW
2345}
2346
2347/* List constructors */
2348
2349OP *
864dbfa3 2350Perl_append_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2351{
2352 if (!first)
2353 return last;
8990e307
LW
2354
2355 if (!last)
79072805 2356 return first;
8990e307 2357
a0d0e21e
LW
2358 if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS)
2359 return newLISTOP(type, 0, first, last);
79072805 2360
a0d0e21e
LW
2361 if (first->op_flags & OPf_KIDS)
2362 ((LISTOP*)first)->op_last->op_sibling = last;
2363 else {
2364 first->op_flags |= OPf_KIDS;
2365 ((LISTOP*)first)->op_first = last;
2366 }
2367 ((LISTOP*)first)->op_last = last;
2368 ((LISTOP*)first)->op_children++;
2369 return first;
79072805
LW
2370}
2371
2372OP *
864dbfa3 2373Perl_append_list(pTHX_ I32 type, LISTOP *first, LISTOP *last)
79072805
LW
2374{
2375 if (!first)
2376 return (OP*)last;
8990e307
LW
2377
2378 if (!last)
79072805 2379 return (OP*)first;
8990e307
LW
2380
2381 if (first->op_type != type)
79072805 2382 return prepend_elem(type, (OP*)first, (OP*)last);
8990e307
LW
2383
2384 if (last->op_type != type)
79072805
LW
2385 return append_elem(type, (OP*)first, (OP*)last);
2386
2387 first->op_last->op_sibling = last->op_first;
2388 first->op_last = last->op_last;
2389 first->op_children += last->op_children;
2390 if (first->op_children)
acb74605 2391 first->op_flags |= OPf_KIDS;
b7dc083c
NIS
2392
2393#ifdef PL_OP_SLAB_ALLOC
2394#else
2395 Safefree(last);
2396#endif
79072805
LW
2397 return (OP*)first;
2398}
2399
2400OP *
864dbfa3 2401Perl_prepend_elem(pTHX_ I32 type, OP *first, OP *last)
79072805
LW
2402{
2403 if (!first)
2404 return last;
8990e307
LW
2405
2406 if (!last)
79072805 2407 return first;
8990e307
LW
2408
2409 if (last->op_type == type) {
2410 if (type == OP_LIST) { /* already a PUSHMARK there */
2411 first->op_sibling = ((LISTOP*)last)->op_first->op_sibling;
2412 ((LISTOP*)last)->op_first->op_sibling = first;
2413 }
2414 else {
2415 if (!(last->op_flags & OPf_KIDS)) {
2416 ((LISTOP*)last)->op_last = first;
2417 last->op_flags |= OPf_KIDS;
2418 }
2419 first->op_sibling = ((LISTOP*)last)->op_first;
2420 ((LISTOP*)last)->op_first = first;
79072805 2421 }
79072805
LW
2422 ((LISTOP*)last)->op_children++;
2423 return last;
2424 }
2425
2426 return newLISTOP(type, 0, first, last);
2427}
2428
2429/* Constructors */
2430
2431OP *
864dbfa3 2432Perl_newNULLLIST(pTHX)
79072805 2433{
8990e307
LW
2434 return newOP(OP_STUB, 0);
2435}
2436
2437OP *
864dbfa3 2438Perl_force_list(pTHX_ OP *o)
8990e307 2439{
11343788
MB
2440 if (!o || o->op_type != OP_LIST)
2441 o = newLISTOP(OP_LIST, 0, o, Nullop);
2442 null(o);
2443 return o;
79072805
LW
2444}
2445
2446OP *
864dbfa3 2447Perl_newLISTOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2448{
2449 LISTOP *listop;
2450
b7dc083c 2451 NewOp(1101, listop, 1, LISTOP);
79072805
LW
2452
2453 listop->op_type = type;
22c35a8c 2454 listop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2455 listop->op_children = (first != 0) + (last != 0);
2456 listop->op_flags = flags;
79072805
LW
2457
2458 if (!last && first)
2459 last = first;
2460 else if (!first && last)
2461 first = last;
8990e307
LW
2462 else if (first)
2463 first->op_sibling = last;
79072805
LW
2464 listop->op_first = first;
2465 listop->op_last = last;
8990e307
LW
2466 if (type == OP_LIST) {
2467 OP* pushop;
2468 pushop = newOP(OP_PUSHMARK, 0);
2469 pushop->op_sibling = first;
2470 listop->op_first = pushop;
2471 listop->op_flags |= OPf_KIDS;
2472 if (!last)
2473 listop->op_last = pushop;
2474 }
2475 else if (listop->op_children)
2476 listop->op_flags |= OPf_KIDS;
79072805
LW
2477
2478 return (OP*)listop;
2479}
2480
2481OP *
864dbfa3 2482Perl_newOP(pTHX_ I32 type, I32 flags)
79072805 2483{
11343788 2484 OP *o;
b7dc083c 2485 NewOp(1101, o, 1, OP);
11343788 2486 o->op_type = type;
22c35a8c 2487 o->op_ppaddr = PL_ppaddr[type];
11343788 2488 o->op_flags = flags;
79072805 2489
11343788
MB
2490 o->op_next = o;
2491 o->op_private = 0 + (flags >> 8);
22c35a8c 2492 if (PL_opargs[type] & OA_RETSCALAR)
11343788 2493 scalar(o);
22c35a8c 2494 if (PL_opargs[type] & OA_TARGET)
11343788
MB
2495 o->op_targ = pad_alloc(type, SVs_PADTMP);
2496 return CHECKOP(type, o);
79072805
LW
2497}
2498
2499OP *
864dbfa3 2500Perl_newUNOP(pTHX_ I32 type, I32 flags, OP *first)
79072805
LW
2501{
2502 UNOP *unop;
2503
93a17b20 2504 if (!first)
aeea060c 2505 first = newOP(OP_STUB, 0);
22c35a8c 2506 if (PL_opargs[type] & OA_MARK)
8990e307 2507 first = force_list(first);
93a17b20 2508
b7dc083c 2509 NewOp(1101, unop, 1, UNOP);
79072805 2510 unop->op_type = type;
22c35a8c 2511 unop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2512 unop->op_first = first;
2513 unop->op_flags = flags | OPf_KIDS;
c07a80fd 2514 unop->op_private = 1 | (flags >> 8);
e50aee73 2515 unop = (UNOP*) CHECKOP(type, unop);
79072805
LW
2516 if (unop->op_next)
2517 return (OP*)unop;
2518
a0d0e21e 2519 return fold_constants((OP *) unop);
79072805
LW
2520}
2521
2522OP *
864dbfa3 2523Perl_newBINOP(pTHX_ I32 type, I32 flags, OP *first, OP *last)
79072805
LW
2524{
2525 BINOP *binop;
b7dc083c 2526 NewOp(1101, binop, 1, BINOP);
79072805
LW
2527
2528 if (!first)
2529 first = newOP(OP_NULL, 0);
2530
2531 binop->op_type = type;
22c35a8c 2532 binop->op_ppaddr = PL_ppaddr[type];
79072805
LW
2533 binop->op_first = first;
2534 binop->op_flags = flags | OPf_KIDS;
2535 if (!last) {
2536 last = first;
c07a80fd 2537 binop->op_private = 1 | (flags >> 8);
79072805
LW
2538 }
2539 else {
c07a80fd 2540 binop->op_private = 2 | (flags >> 8);
79072805
LW
2541 first->op_sibling = last;
2542 }
2543
e50aee73 2544 binop = (BINOP*)CHECKOP(type, binop);
b162f9ea 2545 if (binop->op_next || binop->op_type != type)
79072805
LW
2546 return (OP*)binop;
2547
7284ab6f 2548 binop->op_last = binop->op_first->op_sibling;
79072805 2549
a0d0e21e 2550 return fold_constants((OP *)binop);
79072805
LW
2551}
2552
a0ed51b3
LW
2553static int
2554utf8compare(const void *a, const void *b)
2555{
2556 int i;
2557 for (i = 0; i < 10; i++) {
2558 if ((*(U8**)a)[i] < (*(U8**)b)[i])
2559 return -1;
2560 if ((*(U8**)a)[i] > (*(U8**)b)[i])
2561 return 1;
2562 }
2563 return 0;
2564}
2565
79072805 2566OP *
864dbfa3 2567Perl_pmtrans(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2568{
79072805
LW
2569 SV *tstr = ((SVOP*)expr)->op_sv;
2570 SV *rstr = ((SVOP*)repl)->op_sv;
463ee0b2
LW
2571 STRLEN tlen;
2572 STRLEN rlen;
ec49126f
PP
2573 register U8 *t = (U8*)SvPV(tstr, tlen);
2574 register U8 *r = (U8*)SvPV(rstr, rlen);
79072805
LW
2575 register I32 i;
2576 register I32 j;
a0ed51b3 2577 I32 del;
79072805 2578 I32 complement;
5d06d08e 2579 I32 squash;
79072805
LW
2580 register short *tbl;
2581
11343788 2582 complement = o->op_private & OPpTRANS_COMPLEMENT;
a0ed51b3 2583 del = o->op_private & OPpTRANS_DELETE;
5d06d08e 2584 squash = o->op_private & OPpTRANS_SQUASH;
79072805 2585
a0ed51b3 2586 if (o->op_private & (OPpTRANS_FROM_UTF|OPpTRANS_TO_UTF)) {
79cb57f6 2587 SV* listsv = newSVpvn("# comment\n",10);
a0ed51b3
LW
2588 SV* transv = 0;
2589 U8* tend = t + tlen;
2590 U8* rend = r + rlen;
2591 I32 ulen;
2592 U32 tfirst = 1;
2593 U32 tlast = 0;
2594 I32 tdiff;
2595 U32 rfirst = 1;
2596 U32 rlast = 0;
2597 I32 rdiff;
2598 I32 diff;
2599 I32 none = 0;
2600 U32 max = 0;
2601 I32 bits;
2602 I32 grows = 0;
2603 I32 havefinal = 0;
2604 U32 final;
2605 HV *hv;
2606 I32 from_utf = o->op_private & OPpTRANS_FROM_UTF;
2607 I32 to_utf = o->op_private & OPpTRANS_TO_UTF;
2608
2609 if (complement) {
806e7201 2610 U8 tmpbuf[UTF8_MAXLEN];
a0ed51b3
LW
2611 U8** cp;
2612 UV nextmin = 0;
2613 New(1109, cp, tlen, U8*);
2614 i = 0;
79cb57f6 2615 transv = newSVpvn("",0);
a0ed51b3
LW
2616 while (t < tend) {
2617 cp[i++] = t;
2618 t += UTF8SKIP(t);
2619 if (*t == 0xff) {
2620 t++;
2621 t += UTF8SKIP(t);
2622 }
2623 }
2624 qsort(cp, i, sizeof(U8*), utf8compare);
2625 for (j = 0; j < i; j++) {
2626 U8 *s = cp[j];
2627 UV val = utf8_to_uv(s, &ulen);
2628 s += ulen;
2629 diff = val - nextmin;
2630 if (diff > 0) {
2631 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2632 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2633 if (diff > 1) {
2634 t = uv_to_utf8(tmpbuf, val - 1);
2635 sv_catpvn(transv, "\377", 1);
dfe13c55 2636 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2637 }
2638 }
2639 if (*s == 0xff)
2640 val = utf8_to_uv(s+1, &ulen);
2641 if (val >= nextmin)
2642 nextmin = val + 1;
2643 }
2644 t = uv_to_utf8(tmpbuf,nextmin);
dfe13c55 2645 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
a0ed51b3
LW
2646 t = uv_to_utf8(tmpbuf, 0x7fffffff);
2647 sv_catpvn(transv, "\377", 1);
dfe13c55
GS
2648 sv_catpvn(transv, (char*)tmpbuf, t - tmpbuf);
2649 t = (U8*)SvPVX(transv);
a0ed51b3
LW
2650 tlen = SvCUR(transv);
2651 tend = t + tlen;
2652 }
2653 else if (!rlen && !del) {
2654 r = t; rlen = tlen; rend = tend;
4757a243
LW
2655 }
2656 if (!squash) {
2657 if (to_utf && from_utf) { /* only counting characters */
2658 if (t == r || (tlen == rlen && memEQ(t, r, tlen)))
2659 o->op_private |= OPpTRANS_IDENTICAL;
2660 }
2661 else { /* straight latin-1 translation */
2662 if (tlen == 4 && memEQ(t, "\0\377\303\277", 4) &&
2663 rlen == 4 && memEQ(r, "\0\377\303\277", 4))
2664 o->op_private |= OPpTRANS_IDENTICAL;
2665 }
a0ed51b3
LW
2666 }
2667
2668 while (t < tend || tfirst <= tlast) {
2669 /* see if we need more "t" chars */
2670 if (tfirst > tlast) {
2671 tfirst = (I32)utf8_to_uv(t, &ulen);
2672 t += ulen;
2673 if (t < tend && *t == 0xff) { /* illegal utf8 val indicates range */
2674 tlast = (I32)utf8_to_uv(++t, &ulen);
2675 t += ulen;
2676 }
2677 else
2678 tlast = tfirst;
2679 }
2680
2681 /* now see if we need more "r" chars */
2682 if (rfirst > rlast) {
2683 if (r < rend) {
2684 rfirst = (I32)utf8_to_uv(r, &ulen);
2685 r += ulen;
2686 if (r < rend && *r == 0xff) { /* illegal utf8 val indicates range */
2687 rlast = (I32)utf8_to_uv(++r, &ulen);
2688 r += ulen;
2689 }
2690 else
2691 rlast = rfirst;
2692 }
2693 else {
2694 if (!havefinal++)
2695 final = rlast;
2696 rfirst = rlast = 0xffffffff;
2697 }
2698 }
2699
2700 /* now see which range will peter our first, if either. */
2701 tdiff = tlast - tfirst;
2702 rdiff = rlast - rfirst;
2703
2704 if (tdiff <= rdiff)
2705 diff = tdiff;
2706 else
2707 diff = rdiff;
2708
2709 if (rfirst == 0xffffffff) {
2710 diff = tdiff; /* oops, pretend rdiff is infinite */
2711 if (diff > 0)
894356b3
GS
2712 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\tXXXX\n",
2713 (long)tfirst, (long)tlast);
a0ed51b3 2714 else
894356b3 2715 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\tXXXX\n", (long)tfirst);
a0ed51b3
LW
2716 }
2717 else {
2718 if (diff > 0)
894356b3
GS
2719 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t%04lx\t%04lx\n",
2720 (long)tfirst, (long)(tfirst + diff),
2721 (long)rfirst);
a0ed51b3 2722 else
894356b3
GS
2723 Perl_sv_catpvf(aTHX_ listsv, "%04lx\t\t%04lx\n",
2724 (long)tfirst, (long)rfirst);
a0ed51b3
LW
2725
2726 if (rfirst + diff > max)
2727 max = rfirst + diff;
2728 rfirst += diff + 1;
2729 if (!grows) {
2730 if (rfirst <= 0x80)
2731 ;
2732 else if (rfirst <= 0x800)
2733 grows |= (tfirst < 0x80);
2734 else if (rfirst <= 0x10000)
2735 grows |= (tfirst < 0x800);
2736 else if (rfirst <= 0x200000)
2737 grows |= (tfirst < 0x10000);
2738 else if (rfirst <= 0x4000000)
2739 grows |= (tfirst < 0x200000);
2740 else if (rfirst <= 0x80000000)
2741 grows |= (tfirst < 0x4000000);
2742 }
2743 }
2744 tfirst += diff + 1;
2745 }
2746
2747 none = ++max;
2748 if (del)
2749 del = ++max;
2750
2751 if (max > 0xffff)
2752 bits = 32;
2753 else if (max > 0xff)
2754 bits = 16;
2755 else
2756 bits = 8;
2757
2758 cSVOPo->op_sv = (SV*)swash_init("utf8", "", listsv, bits, none);
2759 SvREFCNT_dec(listsv);
2760 if (transv)
2761 SvREFCNT_dec(transv);
2762
2763 if (!del && havefinal)
2764 (void)hv_store((HV*)SvRV((cSVOPo->op_sv)), "FINAL", 5, newSViv((IV)final), 0);
2765
2766 if (grows && to_utf)
2767 o->op_private |= OPpTRANS_GROWS;
2768
2769 op_free(expr);
2770 op_free(repl);
2771 return o;
2772 }
2773
2774 tbl = (short*)cPVOPo->op_pv;
79072805
LW
2775 if (complement) {
2776 Zero(tbl, 256, short);
2777 for (i = 0; i < tlen; i++)
ec49126f 2778 tbl[t[i]] = -1;
79072805
LW
2779 for (i = 0, j = 0; i < 256; i++) {
2780 if (!tbl[i]) {
2781 if (j >= rlen) {
a0ed51b3 2782 if (del)
79072805
LW
2783 tbl[i] = -2;
2784 else if (rlen)
ec49126f 2785 tbl[i] = r[j-1];
79072805
LW
2786 else
2787 tbl[i] = i;
2788 }
2789 else
ec49126f 2790 tbl[i] = r[j++];
79072805
LW
2791 }
2792 }
2793 }
2794 else {
a0ed51b3 2795 if (!rlen && !del) {
79072805 2796 r = t; rlen = tlen;
5d06d08e 2797 if (!squash)
4757a243 2798 o->op_private |= OPpTRANS_IDENTICAL;
79072805
LW
2799 }
2800 for (i = 0; i < 256; i++)
2801 tbl[i] = -1;
2802 for (i = 0, j = 0; i < tlen; i++,j++) {
2803 if (j >= rlen) {
a0ed51b3 2804 if (del) {
ec49126f
PP
2805 if (tbl[t[i]] == -1)
2806 tbl[t[i]] = -2;
79072805
LW
2807 continue;
2808 }
2809 --j;
2810 }
ec49126f
PP
2811 if (tbl[t[i]] == -1)
2812 tbl[t[i]] = r[j];
79072805
LW
2813 }
2814 }
2815 op_free(expr);
2816 op_free(repl);
2817
11343788 2818 return o;
79072805
LW
2819}
2820
2821OP *
864dbfa3 2822Perl_newPMOP(pTHX_ I32 type, I32 flags)
79072805 2823{
11343788 2824 dTHR;
79072805
LW
2825 PMOP *pmop;
2826
b7dc083c 2827 NewOp(1101, pmop, 1, PMOP);
79072805 2828 pmop->op_type = type;
22c35a8c 2829 pmop->op_ppaddr = PL_ppaddr[type];
79072805 2830 pmop->op_flags = flags;
c07a80fd 2831 pmop->op_private = 0 | (flags >> 8);
79072805 2832
3280af22 2833 if (PL_hints & HINT_RE_TAINT)
b3eb6a9b 2834 pmop->op_pmpermflags |= PMf_RETAINT;
3280af22 2835 if (PL_hints & HINT_LOCALE)
b3eb6a9b
GS
2836 pmop->op_pmpermflags |= PMf_LOCALE;
2837 pmop->op_pmflags = pmop->op_pmpermflags;
36477c24 2838
79072805 2839 /* link into pm list */
3280af22
NIS
2840 if (type != OP_TRANS && PL_curstash) {
2841 pmop->op_pmnext = HvPMROOT(PL_curstash);
2842 HvPMROOT(PL_curstash) = pmop;
79072805
LW
2843 }
2844
2845 return (OP*)pmop;
2846}
2847
2848OP *
864dbfa3 2849Perl_pmruntime(pTHX_ OP *o, OP *expr, OP *repl)
79072805 2850{
5c0ca799 2851 dTHR;
79072805
LW
2852 PMOP *pm;
2853 LOGOP *rcop;
ce862d02 2854 I32 repl_has_vars = 0;
79072805 2855
11343788
MB
2856 if (o->op_type == OP_TRANS)
2857 return pmtrans(o, expr, repl);
79072805 2858
3280af22 2859 PL_hints |= HINT_BLOCK_SCOPE;
11343788 2860 pm = (PMOP*)o;
79072805
LW
2861
2862 if (expr->op_type == OP_CONST) {
463ee0b2 2863 STRLEN plen;
79072805 2864 SV *pat = ((SVOP*)expr)->op_sv;
463ee0b2 2865 char *p = SvPV(pat, plen);
11343788 2866 if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) {
93a17b20 2867 sv_setpvn(pat, "\\s+", 3);
463ee0b2 2868 p = SvPV(pat, plen);
79072805
LW
2869 pm->op_pmflags |= PMf_SKIPWHITE;
2870 }
393fec97 2871 if ((PL_hints & HINT_UTF8) || (SvUTF8(pat) && !(PL_hints & HINT_BYTE)))
7e2040f0 2872 pm->op_pmdynflags |= PMdf_UTF8;
cea2e8a9 2873 pm->op_pmregexp = CALLREGCOMP(aTHX_ p, p + plen, pm);
aeea060c 2874 if (strEQ("\\s+", pm->op_pmregexp->precomp))
85e6fe83 2875 pm->op_pmflags |= PMf_WHITE;
79072805
LW
2876 op_free(expr);
2877 }
2878 else {
393fec97
GS
2879 if (PL_hints & HINT_UTF8)
2880 pm->op_pmdynflags |= PMdf_UTF8;
3280af22
NIS
2881 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL))
2882 expr = newUNOP((!(PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2883 ? OP_REGCRESET
2884 : OP_REGCMAYBE),0,expr);
463ee0b2 2885
b7dc083c 2886 NewOp(1101, rcop, 1, LOGOP);
79072805 2887 rcop->op_type = OP_REGCOMP;
22c35a8c 2888 rcop->op_ppaddr = PL_ppaddr[OP_REGCOMP];
79072805 2889 rcop->op_first = scalar(expr);
3280af22 2890 rcop->op_flags |= ((PL_hints & HINT_RE_EVAL)
2cd61cdb
IZ
2891 ? (OPf_SPECIAL | OPf_KIDS)
2892 : OPf_KIDS);
79072805 2893 rcop->op_private = 1;
11343788 2894 rcop->op_other = o;
79072805
LW
2895
2896 /* establish postfix order */
3280af22 2897 if (pm->op_pmflags & PMf_KEEP || !(PL_hints & HINT_RE_EVAL)) {
463ee0b2
LW
2898 LINKLIST(expr);
2899 rcop->op_next = expr;
2900 ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
2901 }
2902 else {
2903 rcop->op_next = LINKLIST(expr);
2904 expr->op_next = (OP*)rcop;
2905 }
79072805 2906
11343788 2907 prepend_elem(o->op_type, scalar((OP*)rcop), o);
79072805
LW
2908 }
2909
2910 if (repl) {
748a9306 2911 OP *curop;
0244c3a4 2912 if (pm->op_pmflags & PMf_EVAL) {
748a9306 2913 curop = 0;
57843af0
GS
2914 if (CopLINE(PL_curcop) < PL_multi_end)
2915 CopLINE_set(PL_curcop, PL_multi_end);
0244c3a4 2916 }
554b3eca 2917#ifdef USE_THREADS
2faa37cc 2918 else if (repl->op_type == OP_THREADSV
554b3eca 2919 && strchr("&`'123456789+",
533c011a 2920 PL_threadsv_names[repl->op_targ]))
554b3eca
MB
2921 {
2922 curop = 0;
2923 }
2924#endif /* USE_THREADS */
748a9306
LW
2925 else if (repl->op_type == OP_CONST)
2926 curop = repl;
79072805 2927 else {
79072805
LW
2928 OP *lastop = 0;
2929 for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
22c35a8c 2930 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
554b3eca 2931#ifdef USE_THREADS
ce862d02
IZ
2932 if (curop->op_type == OP_THREADSV) {
2933 repl_has_vars = 1;
be949f6f 2934 if (strchr("&`'123456789+", curop->op_private))
ce862d02 2935 break;
554b3eca
MB
2936 }
2937#else
79072805 2938 if (curop->op_type == OP_GV) {
638eceb6 2939 GV *gv = cGVOPx_gv(curop);
ce862d02 2940 repl_has_vars = 1;
93a17b20 2941 if (strchr("&`'123456789+", *GvENAME(gv)))
79072805
LW
2942 break;
2943 }
554b3eca 2944#endif /* USE_THREADS */
79072805
LW
2945 else if (curop->op_type == OP_RV2CV)
2946 break;
2947 else if (curop->op_type == OP_RV2SV ||
2948 curop->op_type == OP_RV2AV ||
2949 curop->op_type == OP_RV2HV ||
2950 curop->op_type == OP_RV2GV) {
2951 if (lastop && lastop->op_type != OP_GV) /*funny deref?*/
2952 break;
2953 }
748a9306
LW
2954 else if (curop->op_type == OP_PADSV ||
2955 curop->op_type == OP_PADAV ||
2956 curop->op_type == OP_PADHV ||
554b3eca 2957 curop->op_type == OP_PADANY) {
ce862d02 2958 repl_has_vars = 1;
748a9306 2959 }
1167e5da
SM
2960 else if (curop->op_type == OP_PUSHRE)
2961 ; /* Okay here, dangerous in newASSIGNOP */
79072805
LW
2962 else
2963 break;
2964 }
2965 lastop = curop;
2966 }
748a9306 2967 }
ce862d02
IZ
2968 if (curop == repl
2969 && !(repl_has_vars
2970 && (!pm->op_pmregexp
2971 || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
748a9306 2972 pm->op_pmflags |= PMf_CONST; /* const for long enough */
4633a7c4 2973 pm->op_pmpermflags |= PMf_CONST; /* const for long enough */
11343788 2974 prepend_elem(o->op_type, scalar(repl), o);
748a9306
LW
2975 }
2976 else {
ce862d02
IZ
2977 if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
2978 pm->op_pmflags |= PMf_MAYBE_CONST;
2979 pm->op_pmpermflags |= PMf_MAYBE_CONST;
2980 }
b7dc083c 2981 NewOp(1101, rcop, 1, LOGOP);
748a9306 2982 rcop->op_type = OP_SUBSTCONT;
22c35a8c 2983 rcop->op_ppaddr = PL_ppaddr[OP_SUBSTCONT];
748a9306
LW
2984 rcop->op_first = scalar(repl);
2985 rcop->op_flags |= OPf_KIDS;
2986 rcop->op_private = 1;
11343788 2987 rcop->op_other = o;
748a9306
LW
2988
2989 /* establish postfix order */
2990 rcop->op_next = LINKLIST(repl);
2991 repl->op_next = (OP*)rcop;
2992
2993 pm->op_pmreplroot = scalar((OP*)rcop);
2994 pm->op_pmreplstart = LINKLIST(rcop);
2995 rcop->op_next = 0;
79072805
LW
2996 }
2997 }
2998
2999 return (OP*)pm;
3000}
3001
3002OP *
864dbfa3 3003Perl_newSVOP(pTHX_ I32 type, I32 flags, SV *sv)
79072805
LW
3004{
3005 SVOP *svop;
b7dc083c 3006 NewOp(1101, svop, 1, SVOP);
79072805 3007 svop->op_type = type;
22c35a8c 3008 svop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3009 svop->op_sv = sv;
3010 svop->op_next = (OP*)svop;
3011 svop->op_flags = flags;
22c35a8c 3012 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 3013 scalar((OP*)svop);
22c35a8c 3014 if (PL_opargs[type] & OA_TARGET)
ed6116ce 3015 svop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 3016 return CHECKOP(type, svop);
79072805
LW
3017}
3018
3019OP *
350de78d
GS
3020Perl_newPADOP(pTHX_ I32 type, I32 flags, SV *sv)
3021{
3022 PADOP *padop;
3023 NewOp(1101, padop, 1, PADOP);
3024 padop->op_type = type;
3025 padop->op_ppaddr = PL_ppaddr[type];
3026 padop->op_padix = pad_alloc(type, SVs_PADTMP);
7766f137 3027 SvREFCNT_dec(PL_curpad[padop->op_padix]);
350de78d 3028 PL_curpad[padop->op_padix] = sv;
7766f137 3029 SvPADTMP_on(sv);
350de78d
GS
3030 padop->op_next = (OP*)padop;
3031 padop->op_flags = flags;
3032 if (PL_opargs[type] & OA_RETSCALAR)
3033 scalar((OP*)padop);
3034 if (PL_opargs[type] & OA_TARGET)
3035 padop->op_targ = pad_alloc(type, SVs_PADTMP);
3036 return CHECKOP(type, padop);
3037}
3038
3039OP *
864dbfa3 3040Perl_newGVOP(pTHX_ I32 type, I32 flags, GV *gv)
79072805 3041{
11343788 3042 dTHR;
350de78d 3043#ifdef USE_ITHREADS
743e66e6 3044 GvIN_PAD_on(gv);
350de78d
GS
3045 return newPADOP(type, flags, SvREFCNT_inc(gv));
3046#else
7934575e 3047 return newSVOP(type, flags, SvREFCNT_inc(gv));
350de78d 3048#endif
79072805
LW
3049}
3050
3051OP *
864dbfa3 3052Perl_newPVOP(pTHX_ I32 type, I32 flags, char *pv)
79072805
LW
3053{
3054 PVOP *pvop;
b7dc083c 3055 NewOp(1101, pvop, 1, PVOP);
79072805 3056 pvop->op_type = type;
22c35a8c 3057 pvop->op_ppaddr = PL_ppaddr[type];
79072805
LW
3058 pvop->op_pv = pv;
3059 pvop->op_next = (OP*)pvop;
3060 pvop->op_flags = flags;
22c35a8c 3061 if (PL_opargs[type] & OA_RETSCALAR)
463ee0b2 3062 scalar((OP*)pvop);
22c35a8c 3063 if (PL_opargs[type] & OA_TARGET)
ed6116ce 3064 pvop->op_targ = pad_alloc(type, SVs_PADTMP);
e50aee73 3065 return CHECKOP(type, pvop);
79072805
LW
3066}
3067
79072805 3068void
864dbfa3 3069Perl_package(pTHX_ OP *o)
79072805 3070{
11343788 3071 dTHR;
93a17b20 3072 SV *sv;
79072805 3073
3280af22
NIS
3074 save_hptr(&PL_curstash);
3075 save_item(PL_curstname);
11343788 3076 if (o) {
463ee0b2
LW
3077 STRLEN len;
3078 char *name;
11343788 3079 sv = cSVOPo->op_sv;
463ee0b2 3080 name = SvPV(sv, len);
3280af22
NIS
3081 PL_curstash = gv_stashpvn(name,len,TRUE);
3082 sv_setpvn(PL_curstname, name, len);
11343788 3083 op_free(o);
93a17b20
LW
3084 }
3085 else {
3280af22
NIS
3086 sv_setpv(PL_curstname,"<none>");
3087 PL_curstash = Nullhv;
93a17b20 3088 }
7ad382f4 3089 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
3090 PL_copline = NOLINE;
3091 PL_expect = XSTATE;
79072805
LW
3092}
3093
85e6fe83 3094void
864dbfa3 3095Perl_utilize(pTHX_ int aver, I32 floor, OP *version, OP *id, OP *arg)
85e6fe83 3096{
a0d0e21e
LW
3097 OP *pack;
3098 OP *meth;
3099 OP *rqop;
3100 OP *imop;
b1cb66bf 3101 OP *veop;
78ca652e 3102 GV *gv;
85e6fe83 3103
a0d0e21e 3104 if (id->op_type != OP_CONST)
cea2e8a9 3105 Perl_croak(aTHX_ "Module name must be constant");
85e6fe83 3106
b1cb66bf
PP
3107 veop = Nullop;
3108
0f79a09d 3109 if (version != Nullop) {
b1cb66bf
PP
3110 SV *vesv = ((SVOP*)version)->op_sv;
3111
44dcb63b 3112 if (arg == Nullop && !SvNIOKp(vesv)) {
b1cb66bf
PP
3113 arg = version;
3114 }
3115 else {
3116 OP *pack;
0f79a09d 3117 SV *meth;
b1cb66bf 3118
44dcb63b 3119 if (version->op_type != OP_CONST || !SvNIOKp(vesv))
cea2e8a9 3120 Perl_croak(aTHX_ "Version number must be constant number");
b1cb66bf
PP
3121
3122 /* Make copy of id so we don't free it twice */
3123 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
3124
3125 /* Fake up a method call to VERSION */
0f79a09d
GS
3126 meth = newSVpvn("VERSION",7);
3127 sv_upgrade(meth, SVt_PVIV);
3128 SvIOK_on(meth);
3129 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
b1cb66bf
PP
3130 veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
3131 append_elem(OP_LIST,
0f79a09d
GS
3132 prepend_elem(OP_LIST, pack, list(version)),
3133 newSVOP(OP_METHOD_NAMED, 0, meth)));
b1cb66bf
PP
3134 }
3135 }
aeea060c 3136
a0d0e21e 3137 /* Fake up an import/unimport */
4633a7c4
LW
3138 if (arg && arg->op_type == OP_STUB)
3139 imop = arg; /* no import on explicit () */
44dcb63b 3140 else if (SvNIOKp(((SVOP*)id)->op_sv)) {
b1cb66bf
PP
3141 imop = Nullop; /* use 5.0; */
3142 }
4633a7c4 3143 else {
0f79a09d
GS
3144 SV *meth;
3145
4633a7c4
LW
3146 /* Make copy of id so we don't free it twice */
3147 pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv));
0f79a09d
GS
3148
3149 /* Fake up a method call to import/unimport */
3150 meth = aver ? newSVpvn("import",6) : newSVpvn("unimport", 8);;
3151 sv_upgrade(meth, SVt_PVIV);
3152 SvIOK_on(meth);
3153 PERL_HASH(SvUVX(meth), SvPVX(meth), SvCUR(meth));
4633a7c4 3154 imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL,
0f79a09d
GS
3155 append_elem(OP_LIST,
3156 prepend_elem(OP_LIST, pack, list(arg)),
3157 newSVOP(OP_METHOD_NAMED, 0, meth)));
4633a7c4
LW
3158 }
3159
78ca652e
GS
3160 /* Fake up a require, handle override, if any */
3161 gv = gv_fetchpv("require", FALSE, SVt_PVCV);
3162 if (!(gv && GvIMPORTED_CV(gv)))
3163 gv = gv_fetchpv("CORE::GLOBAL::require", FALSE, SVt_PVCV);
3164
3165 if (gv && GvIMPORTED_CV(gv)) {
3166 rqop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3167 append_elem(OP_LIST, id,
3168 scalar(newUNOP(OP_RV2CV, 0,
3169 newGVOP(OP_GV, 0,
3170 gv))))));
3171 }
3172 else {
3173 rqop = newUNOP(OP_REQUIRE, 0, id);
3174 }
a0d0e21e
LW
3175
3176 /* Fake up the BEGIN {}, which does its thing immediately. */
09bef843 3177 newATTRSUB(floor,
79cb57f6 3178 newSVOP(OP_CONST, 0, newSVpvn("BEGIN", 5)),
4633a7c4 3179 Nullop,
09bef843 3180 Nullop,
a0d0e21e 3181 append_elem(OP_LINESEQ,
b1cb66bf
PP
3182 append_elem(OP_LINESEQ,
3183 newSTATEOP(0, Nullch, rqop),
3184 newSTATEOP(0, Nullch, veop)),
a0d0e21e 3185 newSTATEOP(0, Nullch, imop) ));
85e6fe83 3186
c305c6a0 3187 PL_hints |= HINT_BLOCK_SCOPE;
3280af22
NIS
3188 PL_copline = NOLINE;
3189 PL_expect = XSTATE;
85e6fe83
LW
3190}
3191
79072805 3192OP *
864dbfa3 3193Perl_dofile(pTHX_ OP *term)
78ca652e
GS
3194{
3195 OP *doop;
3196 GV *gv;
3197
3198 gv = gv_fetchpv("do", FALSE, SVt_PVCV);
3199 if (!(gv && GvIMPORTED_CV(gv)))
3200 gv = gv_fetchpv("CORE::GLOBAL::do", FALSE, SVt_PVCV);
3201
3202 if (gv && GvIMPORTED_CV(gv)) {
3203 doop = ck_subr(newUNOP(OP_ENTERSUB, OPf_STACKED,
3204 append_elem(OP_LIST, term,
3205 scalar(newUNOP(OP_RV2CV, 0,
3206 newGVOP(OP_GV, 0,
3207 gv))))));
3208 }
3209 else {
3210 doop = newUNOP(OP_DOFILE, 0, scalar(term));
3211 }
3212 return doop;
3213}
3214
3215OP *
864dbfa3 3216Perl_newSLICEOP(pTHX_ I32 flags, OP *subscript, OP *listval)
79072805
LW
3217{
3218 return newBINOP(OP_LSLICE, flags,
8990e307
LW
3219 list(force_list(subscript)),
3220 list(force_list(listval)) );
79072805
LW
3221}
3222
76e3520e 3223STATIC I32
cea2e8a9 3224S_list_assignment(pTHX_ register OP *o)
79072805 3225{
11343788 3226 if (!o)
79072805
LW
3227 return TRUE;
3228
11343788
MB
3229 if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS)
3230 o = cUNOPo->op_first;
79072805 3231
11343788 3232 if (o->op_type == OP_COND_EXPR) {
1a67a97c
SM
3233 I32 t = list_assignment(cLOGOPo->op_first->op_sibling);
3234 I32 f = list_assignment(cLOGOPo->op_first->op_sibling->op_sibling);
79072805
LW
3235
3236 if (t && f)
3237 return TRUE;
3238 if (t || f)
3239 yyerror("Assignment to both a list and a scalar");
3240 return FALSE;
3241 }
3242
11343788
MB
3243 if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS ||
3244 o->op_type == OP_RV2AV || o->op_type == OP_RV2HV ||
3245 o->op_type == OP_ASLICE || o->op_type == OP_HSLICE)
79072805
LW
3246 return TRUE;
3247
11343788 3248 if (o->op_type == OP_PADAV || o->op_type == OP_PADHV)
93a17b20
LW
3249 return TRUE;
3250
11343788 3251 if (o->op_type == OP_RV2SV)
79072805
LW
3252 return FALSE;
3253
3254 return FALSE;
3255}
3256
3257OP *
864dbfa3 3258Perl_newASSIGNOP(pTHX_ I32 flags, OP *left, I32 optype, OP *right)
79072805 3259{
11343788 3260 OP *o;
79072805 3261
a0d0e21e
LW
3262 if (optype) {
3263 if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) {
3264 return newLOGOP(optype, 0,
3265 mod(scalar(left), optype),
3266 newUNOP(OP_SASSIGN, 0, scalar(right)));
3267 }
3268 else {
3269 return newBINOP(optype, OPf_STACKED,
3270 mod(scalar(left), optype), scalar(right));
3271 }
3272 }
3273
79072805 3274 if (list_assignment(left)) {
6ee623d5 3275 dTHR;
10c8fecd
GS
3276 OP *curop;
3277
3280af22
NIS
3278 PL_modcount = 0;
3279 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
463ee0b2 3280 left = mod(left, OP_AASSIGN);
3280af22
NIS
3281 if (PL_eval_start)
3282 PL_eval_start = 0;
748a9306 3283 else {
a0d0e21e
LW
3284 op_free(left);
3285 op_free(right);
3286 return Nullop;
3287 }
10c8fecd
GS
3288 curop = list(force_list(left));
3289 o = newBINOP(OP_AASSIGN, flags, list(force_list(right)), curop);
11343788 3290 o->op_private = 0 | (flags >> 8);
10c8fecd
GS
3291 for (curop = ((LISTOP*)curop)->op_first;
3292 curop; curop = curop->op_sibling)
3293 {
3294 if (curop->op_type == OP_RV2HV &&
3295 ((UNOP*)curop)->op_first->op_type != OP_GV) {
3296 o->op_private |= OPpASSIGN_HASH;
3297 break;
3298 }
3299 }
a0d0e21e 3300 if (!(left->op_private & OPpLVAL_INTRO)) {
11343788 3301 OP *lastop = o;
3280af22 3302 PL_generation++;
11343788 3303 for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) {
22c35a8c 3304 if (PL_opargs[curop->op_type] & OA_DANGEROUS) {
79072805 3305 if (curop->op_type == OP_GV) {
638eceb6 3306 GV *gv = cGVOPx_gv(curop);
3280af22 3307 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
79072805 3308 break;
3280af22 3309 SvCUR(gv) = PL_generation;
79072805 3310 }
748a9306
LW
3311 else if (curop->op_type == OP_PADSV ||
3312 curop->op_type == OP_PADAV ||
3313 curop->op_type == OP_PADHV ||
3314 curop->op_type == OP_PADANY) {
3280af22 3315 SV **svp = AvARRAY(PL_comppad_name);
8e07c86e 3316 SV *sv = svp[curop->op_targ];
3280af22 3317 if (SvCUR(sv) == PL_generation)
748a9306 3318 break;
3280af22 3319 SvCUR(sv) = PL_generation; /* (SvCUR not used any more) */
748a9306 3320 }
79072805
LW
3321 else if (curop->op_type == OP_RV2CV)
3322 break;
3323 else if (curop->op_type == OP_RV2SV ||
3324 curop->op_type == OP_RV2AV ||
3325 curop->op_type == OP_RV2HV ||
3326 curop->op_type == OP_RV2GV) {
3327 if (lastop->op_type != OP_GV) /* funny deref? */
3328 break;
3329 }
1167e5da
SM
3330 else if (curop->op_type == OP_PUSHRE) {
3331 if (((PMOP*)curop)->op_pmreplroot) {
3332 GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
3280af22 3333 if (gv == PL_defgv || SvCUR(gv) == PL_generation)
1167e5da 3334 break;
3280af22 3335 SvCUR(gv) = PL_generation;
1167e5da
SM
3336 }
3337 }
79072805
LW
3338 else
3339 break;
3340 }
3341 lastop = curop;
3342 }
11343788 3343 if (curop != o)
10c8fecd 3344 o->op_private |= OPpASSIGN_COMMON;
79072805 3345 }
c07a80fd
PP
3346 if (right && right->op_type == OP_SPLIT) {
3347 OP* tmpop;
3348 if ((tmpop = ((LISTOP*)right)->op_first) &&
3349 tmpop->op_type == OP_PUSHRE)
3350 {
3351 PMOP *pm = (PMOP*)tmpop;
3352 if (left->op_type == OP_RV2AV &&
3353 !(left->op_private & OPpLVAL_INTRO) &&
11343788 3354 !(o->op_private & OPpASSIGN_COMMON) )
c07a80fd
PP
3355 {
3356 tmpop = ((UNOP*)left)->op_first;
3357 if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) {
971a9dd3
GS
3358#ifdef USE_ITHREADS
3359 pm->op_pmreplroot = (OP*)cPADOPx(tmpop)->op_padix;
3360 cPADOPx(tmpop)->op_padix = 0; /* steal it */
3361#else
3362 pm->op_pmreplroot = (OP*)cSVOPx(tmpop)->op_sv;
3363 cSVOPx(tmpop)->op_sv = Nullsv; /* steal it */
3364#endif
c07a80fd 3365 pm->op_pmflags |= PMf_ONCE;
11343788 3366 tmpop = cUNOPo->op_first; /* to list (nulled) */
c07a80fd
PP
3367 tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */
3368 tmpop->op_sibling = Nullop; /* don't free split */
3369 right->op_next = tmpop->op_next; /* fix starting loc */
11343788 3370 op_free(o); /* blow off assign */
54310121 3371 right->op_flags &= ~OPf_WANT;
a5f75d66 3372 /* "I don't know and I don't care." */
c07a80fd
PP
3373 return right;
3374 }
3375 }
3376 else {
3280af22 3377 if (PL_modcount < 10000 &&
c07a80fd
PP
3378 ((LISTOP*)right)->op_last->op_type == OP_CONST)
3379 {
3380 SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv;
3381 if (SvIVX(sv) == 0)
3280af22 3382 sv_setiv(sv, PL_modcount+1);
c07a80fd
PP
3383 }
3384 }
3385 }
3386 }
11343788 3387 return o;
79072805
LW
3388 }
3389 if (!right)
3390 right = newOP(OP_UNDEF, 0);
3391 if (right->op_type == OP_READLINE) {
3392 right->op_flags |= OPf_STACKED;
463ee0b2 3393 return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right));
79072805 3394 }
a0d0e21e 3395 else {
3280af22 3396 PL_eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/
11343788 3397 o = newBINOP(OP_SASSIGN, flags,
463ee0b2 3398 scalar(right), mod(scalar(left), OP_SASSIGN) );
3280af22
NIS
3399 if (PL_eval_start)
3400 PL_eval_start = 0;
748a9306 3401 else {
11343788 3402 op_free(o);
a0d0e21e
LW
3403 return Nullop;
3404 }
3405 }
11343788 3406 return o;
79072805
LW
3407}
3408
3409OP *
864dbfa3 3410Perl_newSTATEOP(pTHX_ I32 flags, char *label, OP *o)
79072805 3411{
11343788 3412 dTHR;
bbce6d69 3413 U32 seq = intro_my();
79072805
LW
3414 register COP *cop;
3415
b7dc083c 3416 NewOp(1101, cop, 1, COP);
57843af0 3417 if (PERLDB_LINE && CopLINE(PL_curcop) && PL_curstash != PL_debstash) {
8990e307 3418 cop->op_type = OP_DBSTATE;
22c35a8c 3419 cop->op_ppaddr = PL_ppaddr[ OP_DBSTATE ];
8990e307
LW
3420 }
3421 else {
3422 cop->op_type = OP_NEXTSTATE;
22c35a8c 3423 cop->op_ppaddr = PL_ppaddr[ OP_NEXTSTATE ];
8990e307 3424 }
79072805 3425 cop->op_flags = flags;
393fec97 3426 cop->op_private = (PL_hints & HINT_BYTE);
ff0cee69
PP
3427#ifdef NATIVE_HINTS
3428 cop->op_private |= NATIVE_HINTS;
3429#endif
e24b16f9 3430 PL_compiling.op_private = cop->op_private;
79072805
LW
3431 cop->op_next = (OP*)cop;
3432
463ee0b2
LW
3433 if (label) {
3434 cop->cop_label = label;
3280af22 3435 PL_hints |= HINT_BLOCK_SCOPE;
463ee0b2 3436 }
bbce6d69 3437 cop->cop_seq = seq;
3280af22 3438 cop->cop_arybase = PL_curcop->cop_arybase;
0453d815 3439 if (specialWARN(PL_curcop->cop_warnings))
599cee73
PM
3440 cop->cop_warnings = PL_curcop->cop_warnings ;
3441 else
3442 cop->cop_warnings = newSVsv(PL_curcop->cop_warnings) ;
3443
79072805 3444
3280af22 3445 if (PL_copline == NOLINE)
57843af0 3446 CopLINE_set(cop, CopLINE(PL_curcop));
79072805 3447 else {
57843af0 3448 CopLINE_set(cop, PL_copline);
3280af22 3449 PL_copline = NOLINE;
79072805 3450 }
57843af0
GS
3451#ifdef USE_ITHREADS
3452 CopFILE_set(cop, CopFILE(PL_curcop)); /* XXXXX share in a pvtable? */
3453#else
cc49e20b 3454 CopFILEGV_set(cop, (GV*)SvREFCNT_inc(CopFILEGV(PL_curcop)));
57843af0 3455#endif
11faa288 3456 CopSTASH_set(cop, PL_curstash);
79072805 3457
3280af22 3458 if (PERLDB_LINE && PL_curstash != PL_debstash) {
cc49e20b 3459 SV **svp = av_fetch(CopFILEAV(PL_curcop), (I32)CopLINE(cop), FALSE);
3280af22 3460 if (svp && *svp != &PL_sv_undef && !SvIOK(*svp)) {
a0d0e21e 3461 (void)SvIOK_on(*svp);
57b2e452 3462 SvIVX(*svp) = PTR2IV(cop);
93a17b20
LW
3463 }
3464 }
3465
11343788 3466 return prepend_elem(OP_LINESEQ, (OP*)cop, o);
79072805
LW
3467}
3468
bbce6d69
PP
3469/* "Introduce" my variables to visible status. */
3470U32
864dbfa3 3471Perl_intro_my(pTHX)
bbce6d69
PP
3472{
3473 SV **svp;
3474 SV *sv;
3475 I32 i;
3476
3280af22
NIS
3477 if (! PL_min_intro_pending)
3478 return PL_cop_seqmax;
bbce6d69 3479
3280af22
NIS
3480 svp = AvARRAY(PL_comppad_name);
3481 for (i = PL_min_intro_pending; i <= PL_max_intro_pending; i++) {
3482 if ((sv = svp[i]) && sv != &PL_sv_undef && !SvIVX(sv)) {
c53d7c7d 3483 SvIVX(sv) = PAD_MAX; /* Don't know scope end yet. */
65202027 3484 SvNVX(sv) = (NV)PL_cop_seqmax;
bbce6d69
PP
3485 }
3486 }
3280af22
NIS
3487 PL_min_intro_pending = 0;
3488 PL_comppad_name_fill = PL_max_intro_pending; /* Needn't search higher */
3489 return PL_cop_seqmax++;
bbce6d69
PP
3490}
3491
79072805 3492OP *
864dbfa3 3493Perl_newLOGOP(pTHX_ I32 type, I32 flags, OP *first, OP *other)
79072805 3494{
883ffac3
CS
3495 return new_logop(type, flags, &first, &other);
3496}
3497
3bd495df 3498STATIC OP *
cea2e8a9 3499S_new_logop(pTHX_ I32 type, I32 flags, OP** firstp, OP** otherp)
883ffac3 3500{
11343788 3501 dTHR;
79072805 3502 LOGOP *logop;
11343788 3503 OP *o;
883ffac3
CS
3504 OP *first = *firstp;
3505 OP *other = *otherp;
79072805 3506
a0d0e21e
LW
3507 if (type == OP_XOR) /* Not short circuit, but here by precedence. */
3508 return newBINOP(type, flags, scalar(first), scalar(other));
3509
8990e307 3510 scalarboolean(first);
79072805
LW
3511 /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */
3512 if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) {
3513 if (type == OP_AND || type == OP_OR) {
3514 if (type == OP_AND)
3515 type = OP_OR;
3516 else
3517 type = OP_AND;
11343788 3518 o = first;
883ffac3 3519 first = *firstp = cUNOPo->op_first;
11343788
MB
3520 if (o->op_next)
3521 first->op_next = o->op_next;
3522 cUNOPo->op_first = Nullop;
3523 op_free(o);
79072805
LW
3524 }
3525 }
3526 if (first->op_type == OP_CONST) {
4673fc70 3527 if (ckWARN(WARN_BAREWORD) && (first->op_private & OPpCONST_BARE))
e476b1b5 3528 Perl_warner(aTHX_ WARN_BAREWORD, "Bareword found in conditional");
79072805
LW
3529 if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) {
3530 op_free(first);
883ffac3 3531 *firstp = Nullop;
79072805
LW
3532 return other;
3533 }
3534 else {
3535 op_free(other);
883ffac3 3536 *otherp = Nullop;
79072805
LW
3537 return first;
3538 }
3539 }
3540 else if (first->op_type == OP_WANTARRAY) {
3541 if (type == OP_AND)
3542 list(other);
3543 else
3544 scalar(other);
3545 }
e476b1b5 3546 else if (ckWARN(WARN_MISC) && (first->op_flags & OPf_KIDS)) {
a6006777
PP
3547 OP *k1 = ((UNOP*)first)->op_first;
3548 OP *k2 = k1->op_sibling;
3549 OPCODE warnop = 0;
3550 switch (first->op_type)
3551 {
3552 case OP_NULL:
3553 if (k2 && k2->op_type == OP_READLINE
3554 && (k2->op_flags & OPf_STACKED)
55d729e4 3555 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR))
72b16652