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