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