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