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