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