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