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