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