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