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