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