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