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 | ||
e50aee73 | 21 | /* |
5dc0d613 | 22 | * In the following definition, the ", Nullop" is just to make the compiler |
a5f75d66 | 23 | * think the expression is of the right type: croak actually does a Siglongjmp. |
e50aee73 | 24 | */ |
11343788 | 25 | #define CHECKOP(type,o) \ |
28757baa | 26 | ((op_mask && op_mask[type]) \ |
5dc0d613 | 27 | ? ( op_free((OP*)o), \ |
28757baa | 28 | croak("%s trapped by operation mask", op_desc[type]), \ |
29 | Nullop ) \ | |
11343788 | 30 | : (*check[type])((OP*)o)) |
e50aee73 | 31 | |
11343788 | 32 | static I32 list_assignment _((OP *o)); |
3bc5dc61 | 33 | static void bad_type _((I32 n, char *t, char *name, OP *kid)); |
11343788 MB |
34 | static OP *modkids _((OP *o, I32 type)); |
35 | static OP *no_fh_allowed _((OP *o)); | |
5dc0d613 | 36 | static bool scalar_mod_type _((OP *o, I32 type)); |
11343788 MB |
37 | static OP *scalarboolean _((OP *o)); |
38 | static OP *too_few_arguments _((OP *o, char* name)); | |
39 | static OP *too_many_arguments _((OP *o, char* name)); | |
40 | static void null _((OP* o)); | |
bbce6d69 | 41 | static PADOFFSET pad_findlex _((char* name, PADOFFSET newoff, U32 seq, |
748a9306 | 42 | CV* startcv, I32 cx_ix)); |
54b9620d | 43 | static OP *newDEFSVOP _((void)); |
79072805 | 44 | |
4633a7c4 | 45 | static char* |
8ac85365 | 46 | gv_ename(GV *gv) |
4633a7c4 LW |
47 | { |
48 | SV* tmpsv = sv_newmortal(); | |
46fc3d4c | 49 | gv_efullname3(tmpsv, gv, Nullch); |
4633a7c4 LW |
50 | return SvPV(tmpsv,na); |
51 | } | |
52 | ||
8990e307 | 53 | static OP * |
8ac85365 | 54 | no_fh_allowed(OP *o) |
79072805 | 55 | { |
46fc3d4c | 56 | yyerror(form("Missing comma after first argument to %s function", |
5196be3e | 57 | op_desc[o->op_type])); |
11343788 | 58 | return o; |
79072805 LW |
59 | } |
60 | ||
8990e307 | 61 | static OP * |
8ac85365 | 62 | too_few_arguments(OP *o, char *name) |
79072805 | 63 | { |
46fc3d4c | 64 | yyerror(form("Not enough arguments for %s", name)); |
11343788 | 65 | return o; |
79072805 LW |
66 | } |
67 | ||
8990e307 | 68 | static OP * |
8ac85365 | 69 | too_many_arguments(OP *o, char *name) |
79072805 | 70 | { |
46fc3d4c | 71 | yyerror(form("Too many arguments for %s", name)); |
11343788 | 72 | return o; |
79072805 LW |
73 | } |
74 | ||
3bc5dc61 | 75 | static void |
8ac85365 | 76 | bad_type(I32 n, char *t, char *name, OP *kid) |
8990e307 | 77 | { |
46fc3d4c | 78 | yyerror(form("Type of arg %d to %s must be %s (not %s)", |
79 | (int)n, name, t, op_desc[kid->op_type])); | |
8990e307 LW |
80 | } |
81 | ||
a0d0e21e | 82 | void |
8ac85365 | 83 | assertref(OP *o) |
a0d0e21e | 84 | { |
11343788 | 85 | int type = o->op_type; |
a0d0e21e | 86 | if (type != OP_AELEM && type != OP_HELEM) { |
46fc3d4c | 87 | yyerror(form("Can't use subscript on %s", op_desc[type])); |
8ebc5c01 | 88 | if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) |
748a9306 | 89 | warn("(Did you mean $ or @ instead of %c?)\n", |
8ebc5c01 | 90 | type == OP_ENTERSUB ? '&' : '%'); |
a0d0e21e LW |
91 | } |
92 | } | |
93 | ||
79072805 LW |
94 | /* "register" allocation */ |
95 | ||
96 | PADOFFSET | |
8ac85365 | 97 | pad_allocmy(char *name) |
93a17b20 | 98 | { |
11343788 | 99 | dTHR; |
a0d0e21e LW |
100 | PADOFFSET off; |
101 | SV *sv; | |
102 | ||
103 | if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) { | |
46fc3d4c | 104 | if (!isPRINT(name[1])) { |
105 | name[3] = '\0'; | |
106 | name[2] = toCTRL(name[1]); | |
107 | name[1] = '^'; | |
108 | } | |
a0d0e21e LW |
109 | croak("Can't use global %s in \"my\"",name); |
110 | } | |
93965878 | 111 | if (dowarn && AvFILLp(comppad_name) >= 0) { |
b1cb66bf | 112 | SV **svp = AvARRAY(comppad_name); |
93965878 | 113 | for (off = AvFILLp(comppad_name); off > comppad_name_floor; off--) { |
b1cb66bf | 114 | if ((sv = svp[off]) |
115 | && sv != &sv_undef | |
116 | && SvIVX(sv) == 999999999 /* var is in open scope */ | |
117 | && strEQ(name, SvPVX(sv))) | |
118 | { | |
119 | warn("\"my\" variable %s masks earlier declaration in same scope", name); | |
120 | break; | |
121 | } | |
122 | } | |
123 | } | |
a0d0e21e LW |
124 | off = pad_alloc(OP_PADSV, SVs_PADMY); |
125 | sv = NEWSV(1102,0); | |
93a17b20 LW |
126 | sv_upgrade(sv, SVt_PVNV); |
127 | sv_setpv(sv, name); | |
c750a3ec MB |
128 | if (in_my_stash) { |
129 | if (*name != '$') | |
130 | croak("Can't declare class for non-scalar %s in \"my\"",name); | |
131 | SvOBJECT_on(sv); | |
132 | (void)SvUPGRADE(sv, SVt_PVMG); | |
133 | SvSTASH(sv) = (HV*)SvREFCNT_inc(in_my_stash); | |
134 | sv_objcount++; | |
135 | } | |
8990e307 | 136 | av_store(comppad_name, off, sv); |
748a9306 | 137 | SvNVX(sv) = (double)999999999; |
8990e307 LW |
138 | SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */ |
139 | if (!min_intro_pending) | |
140 | min_intro_pending = off; | |
141 | max_intro_pending = off; | |
93a17b20 | 142 | if (*name == '@') |
463ee0b2 | 143 | av_store(comppad, off, (SV*)newAV()); |
93a17b20 | 144 | else if (*name == '%') |
463ee0b2 | 145 | av_store(comppad, off, (SV*)newHV()); |
ed6116ce | 146 | SvPADMY_on(curpad[off]); |
93a17b20 LW |
147 | return off; |
148 | } | |
149 | ||
748a9306 | 150 | static PADOFFSET |
e9a444f0 | 151 | #ifndef CAN_PROTOTYPE |
748a9306 | 152 | pad_findlex(name, newoff, seq, startcv, cx_ix) |
93a17b20 | 153 | char *name; |
748a9306 | 154 | PADOFFSET newoff; |
bbce6d69 | 155 | U32 seq; |
748a9306 LW |
156 | CV* startcv; |
157 | I32 cx_ix; | |
e9a444f0 | 158 | #else |
bbce6d69 | 159 | pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix) |
e9a444f0 | 160 | #endif |
93a17b20 | 161 | { |
11343788 | 162 | dTHR; |
748a9306 | 163 | CV *cv; |
93a17b20 LW |
164 | I32 off; |
165 | SV *sv; | |
93a17b20 | 166 | register I32 i; |
c09156bb | 167 | register PERL_CONTEXT *cx; |
a0d0e21e | 168 | int saweval; |
93a17b20 | 169 | |
748a9306 | 170 | for (cv = startcv; cv; cv = CvOUTSIDE(cv)) { |
4fdae800 | 171 | AV *curlist = CvPADLIST(cv); |
172 | SV **svp = av_fetch(curlist, 0, FALSE); | |
748a9306 | 173 | AV *curname; |
4fdae800 | 174 | |
748a9306 | 175 | if (!svp || *svp == &sv_undef) |
4633a7c4 | 176 | continue; |
748a9306 LW |
177 | curname = (AV*)*svp; |
178 | svp = AvARRAY(curname); | |
93965878 | 179 | for (off = AvFILLp(curname); off > 0; off--) { |
748a9306 LW |
180 | if ((sv = svp[off]) && |
181 | sv != &sv_undef && | |
182 | seq <= SvIVX(sv) && | |
13826f2c | 183 | seq > I_32(SvNVX(sv)) && |
748a9306 LW |
184 | strEQ(SvPVX(sv), name)) |
185 | { | |
5f05dabc | 186 | I32 depth; |
187 | AV *oldpad; | |
188 | SV *oldsv; | |
189 | ||
190 | depth = CvDEPTH(cv); | |
191 | if (!depth) { | |
9607fc9c | 192 | if (newoff) { |
193 | if (SvFAKE(sv)) | |
194 | continue; | |
4fdae800 | 195 | return 0; /* don't clone from inactive stack frame */ |
9607fc9c | 196 | } |
5f05dabc | 197 | depth = 1; |
198 | } | |
199 | oldpad = (AV*)*av_fetch(curlist, depth, FALSE); | |
200 | oldsv = *av_fetch(oldpad, off, TRUE); | |
748a9306 | 201 | if (!newoff) { /* Not a mere clone operation. */ |
9607fc9c | 202 | SV *namesv = NEWSV(1103,0); |
748a9306 | 203 | newoff = pad_alloc(OP_PADSV, SVs_PADMY); |
9607fc9c | 204 | sv_upgrade(namesv, SVt_PVNV); |
205 | sv_setpv(namesv, name); | |
206 | av_store(comppad_name, newoff, namesv); | |
207 | SvNVX(namesv) = (double)curcop->cop_seq; | |
208 | SvIVX(namesv) = 999999999; /* A ref, intro immediately */ | |
209 | SvFAKE_on(namesv); /* A ref, not a real var */ | |
774d564b | 210 | if (CvANON(compcv) || SvTYPE(compcv) == SVt_PVFM) { |
28757baa | 211 | /* "It's closures all the way down." */ |
212 | CvCLONE_on(compcv); | |
54310121 | 213 | if (cv == startcv) { |
214 | if (CvANON(compcv)) | |
215 | oldsv = Nullsv; /* no need to keep ref */ | |
216 | } | |
217 | else { | |
28757baa | 218 | CV *bcv; |
219 | for (bcv = startcv; | |
220 | bcv && bcv != cv && !CvCLONE(bcv); | |
221 | bcv = CvOUTSIDE(bcv)) { | |
222 | if (CvANON(bcv)) | |
223 | CvCLONE_on(bcv); | |
224 | else { | |
774d564b | 225 | if (dowarn && !CvUNIQUE(cv)) |
44a8e56a | 226 | warn( |
227 | "Variable \"%s\" may be unavailable", | |
28757baa | 228 | name); |
229 | break; | |
230 | } | |
231 | } | |
232 | } | |
233 | } | |
44a8e56a | 234 | else if (!CvUNIQUE(compcv)) { |
9607fc9c | 235 | if (dowarn && !SvFAKE(sv) && !CvUNIQUE(cv)) |
44a8e56a | 236 | warn("Variable \"%s\" will not stay shared", name); |
5f05dabc | 237 | } |
748a9306 | 238 | } |
199100c8 | 239 | av_store(comppad, newoff, SvREFCNT_inc(oldsv)); |
748a9306 LW |
240 | return newoff; |
241 | } | |
93a17b20 LW |
242 | } |
243 | } | |
244 | ||
245 | /* Nothing in current lexical context--try eval's context, if any. | |
246 | * This is necessary to let the perldb get at lexically scoped variables. | |
247 | * XXX This will also probably interact badly with eval tree caching. | |
248 | */ | |
249 | ||
a0d0e21e | 250 | saweval = 0; |
748a9306 | 251 | for (i = cx_ix; i >= 0; i--) { |
93a17b20 LW |
252 | cx = &cxstack[i]; |
253 | switch (cx->cx_type) { | |
254 | default: | |
748a9306 LW |
255 | if (i == 0 && saweval) { |
256 | seq = cxstack[saweval].blk_oldcop->cop_seq; | |
257 | return pad_findlex(name, newoff, seq, main_cv, 0); | |
258 | } | |
93a17b20 LW |
259 | break; |
260 | case CXt_EVAL: | |
44a8e56a | 261 | switch (cx->blk_eval.old_op_type) { |
262 | case OP_ENTEREVAL: | |
263 | saweval = i; | |
264 | break; | |
265 | case OP_REQUIRE: | |
266 | /* require must have its own scope */ | |
267 | return 0; | |
268 | } | |
93a17b20 LW |
269 | break; |
270 | case CXt_SUB: | |
271 | if (!saweval) | |
272 | return 0; | |
273 | cv = cx->blk_sub.cv; | |
748a9306 LW |
274 | if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */ |
275 | saweval = i; /* so we know where we were called from */ | |
93a17b20 | 276 | continue; |
93a17b20 | 277 | } |
748a9306 LW |
278 | seq = cxstack[saweval].blk_oldcop->cop_seq; |
279 | return pad_findlex(name, newoff, seq, cv, i-1); | |
93a17b20 LW |
280 | } |
281 | } | |
282 | ||
748a9306 LW |
283 | return 0; |
284 | } | |
a0d0e21e | 285 | |
748a9306 | 286 | PADOFFSET |
8ac85365 | 287 | pad_findmy(char *name) |
748a9306 | 288 | { |
11343788 | 289 | dTHR; |
748a9306 | 290 | I32 off; |
54310121 | 291 | I32 pendoff = 0; |
748a9306 LW |
292 | SV *sv; |
293 | SV **svp = AvARRAY(comppad_name); | |
bbce6d69 | 294 | U32 seq = cop_seqmax; |
748a9306 | 295 | |
11343788 MB |
296 | #ifdef USE_THREADS |
297 | /* | |
298 | * Special case to get lexical (and hence per-thread) @_. | |
299 | * XXX I need to find out how to tell at parse-time whether use | |
300 | * of @_ should refer to a lexical (from a sub) or defgv (global | |
301 | * scope and maybe weird sub-ish things like formats). See | |
302 | * startsub in perly.y. It's possible that @_ could be lexical | |
303 | * (at least from subs) even in non-threaded perl. | |
304 | */ | |
305 | if (strEQ(name, "@_")) | |
306 | return 0; /* success. (NOT_IN_PAD indicates failure) */ | |
307 | #endif /* USE_THREADS */ | |
308 | ||
748a9306 | 309 | /* The one we're looking for is probably just before comppad_name_fill. */ |
93965878 | 310 | for (off = AvFILLp(comppad_name); off > 0; off--) { |
a0d0e21e LW |
311 | if ((sv = svp[off]) && |
312 | sv != &sv_undef && | |
54310121 | 313 | (!SvIVX(sv) || |
314 | (seq <= SvIVX(sv) && | |
315 | seq > I_32(SvNVX(sv)))) && | |
a0d0e21e LW |
316 | strEQ(SvPVX(sv), name)) |
317 | { | |
54310121 | 318 | if (SvIVX(sv)) |
319 | return (PADOFFSET)off; | |
320 | pendoff = off; /* this pending def. will override import */ | |
a0d0e21e LW |
321 | } |
322 | } | |
748a9306 LW |
323 | |
324 | /* See if it's in a nested scope */ | |
325 | off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix); | |
54310121 | 326 | if (off) { |
327 | /* If there is a pending local definition, this new alias must die */ | |
328 | if (pendoff) | |
329 | SvIVX(AvARRAY(comppad_name)[off]) = seq; | |
11343788 | 330 | return off; /* pad_findlex returns 0 for failure...*/ |
54310121 | 331 | } |
11343788 | 332 | return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */ |
93a17b20 LW |
333 | } |
334 | ||
335 | void | |
8ac85365 | 336 | pad_leavemy(I32 fill) |
93a17b20 LW |
337 | { |
338 | I32 off; | |
8990e307 | 339 | SV **svp = AvARRAY(comppad_name); |
93a17b20 | 340 | SV *sv; |
8990e307 LW |
341 | if (min_intro_pending && fill < min_intro_pending) { |
342 | for (off = max_intro_pending; off >= min_intro_pending; off--) { | |
a0d0e21e | 343 | if ((sv = svp[off]) && sv != &sv_undef) |
8990e307 LW |
344 | warn("%s never introduced", SvPVX(sv)); |
345 | } | |
346 | } | |
347 | /* "Deintroduce" my variables that are leaving with this scope. */ | |
93965878 | 348 | for (off = AvFILLp(comppad_name); off > fill; off--) { |
748a9306 | 349 | if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999) |
463ee0b2 | 350 | SvIVX(sv) = cop_seqmax; |
93a17b20 LW |
351 | } |
352 | } | |
353 | ||
354 | PADOFFSET | |
8ac85365 | 355 | pad_alloc(I32 optype, U32 tmptype) |
79072805 | 356 | { |
11343788 | 357 | dTHR; |
79072805 LW |
358 | SV *sv; |
359 | I32 retval; | |
360 | ||
361 | if (AvARRAY(comppad) != curpad) | |
463ee0b2 | 362 | croak("panic: pad_alloc"); |
a0d0e21e LW |
363 | if (pad_reset_pending) |
364 | pad_reset(); | |
ed6116ce | 365 | if (tmptype & SVs_PADMY) { |
79072805 | 366 | do { |
93965878 | 367 | sv = *av_fetch(comppad, AvFILLp(comppad) + 1, TRUE); |
ed6116ce | 368 | } while (SvPADBUSY(sv)); /* need a fresh one */ |
93965878 | 369 | retval = AvFILLp(comppad); |
79072805 LW |
370 | } |
371 | else { | |
bbce6d69 | 372 | SV **names = AvARRAY(comppad_name); |
93965878 | 373 | SSize_t names_fill = AvFILLp(comppad_name); |
bbce6d69 | 374 | for (;;) { |
375 | /* | |
376 | * "foreach" index vars temporarily become aliases to non-"my" | |
377 | * values. Thus we must skip, not just pad values that are | |
378 | * marked as current pad values, but also those with names. | |
379 | */ | |
380 | if (++padix <= names_fill && | |
381 | (sv = names[padix]) && sv != &sv_undef) | |
382 | continue; | |
383 | sv = *av_fetch(comppad, padix, TRUE); | |
384 | if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY))) | |
385 | break; | |
386 | } | |
79072805 LW |
387 | retval = padix; |
388 | } | |
8990e307 | 389 | SvFLAGS(sv) |= tmptype; |
79072805 | 390 | curpad = AvARRAY(comppad); |
11343788 | 391 | #ifdef USE_THREADS |
5dc0d613 MB |
392 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n", |
393 | (unsigned long) thr, (unsigned long) curpad, | |
394 | (long) retval, op_name[optype])); | |
11343788 | 395 | #else |
d9bb4600 GS |
396 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n", |
397 | (unsigned long) curpad, | |
5dc0d613 | 398 | (long) retval, op_name[optype])); |
11343788 | 399 | #endif /* USE_THREADS */ |
79072805 LW |
400 | return (PADOFFSET)retval; |
401 | } | |
402 | ||
403 | SV * | |
a0d0e21e | 404 | #ifndef CAN_PROTOTYPE |
79072805 LW |
405 | pad_sv(po) |
406 | PADOFFSET po; | |
8990e307 LW |
407 | #else |
408 | pad_sv(PADOFFSET po) | |
a0d0e21e | 409 | #endif /* CAN_PROTOTYPE */ |
79072805 | 410 | { |
11343788 MB |
411 | dTHR; |
412 | #ifdef USE_THREADS | |
5dc0d613 MB |
413 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n", |
414 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 415 | #else |
79072805 | 416 | if (!po) |
463ee0b2 | 417 | croak("panic: pad_sv po"); |
d9bb4600 GS |
418 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n", |
419 | (unsigned long) curpad, po)); | |
11343788 | 420 | #endif /* USE_THREADS */ |
79072805 LW |
421 | return curpad[po]; /* eventually we'll turn this into a macro */ |
422 | } | |
423 | ||
424 | void | |
a0d0e21e | 425 | #ifndef CAN_PROTOTYPE |
79072805 LW |
426 | pad_free(po) |
427 | PADOFFSET po; | |
8990e307 LW |
428 | #else |
429 | pad_free(PADOFFSET po) | |
a0d0e21e | 430 | #endif /* CAN_PROTOTYPE */ |
79072805 | 431 | { |
11343788 | 432 | dTHR; |
a0d0e21e LW |
433 | if (!curpad) |
434 | return; | |
79072805 | 435 | if (AvARRAY(comppad) != curpad) |
463ee0b2 | 436 | croak("panic: pad_free curpad"); |
79072805 | 437 | if (!po) |
463ee0b2 | 438 | croak("panic: pad_free po"); |
11343788 | 439 | #ifdef USE_THREADS |
5dc0d613 MB |
440 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n", |
441 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 442 | #else |
d9bb4600 GS |
443 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n", |
444 | (unsigned long) curpad, po)); | |
11343788 | 445 | #endif /* USE_THREADS */ |
a0d0e21e | 446 | if (curpad[po] && curpad[po] != &sv_undef) |
ed6116ce | 447 | SvPADTMP_off(curpad[po]); |
a0d0e21e | 448 | if ((I32)po < padix) |
79072805 LW |
449 | padix = po - 1; |
450 | } | |
451 | ||
452 | void | |
a0d0e21e | 453 | #ifndef CAN_PROTOTYPE |
79072805 LW |
454 | pad_swipe(po) |
455 | PADOFFSET po; | |
8990e307 LW |
456 | #else |
457 | pad_swipe(PADOFFSET po) | |
a0d0e21e | 458 | #endif /* CAN_PROTOTYPE */ |
79072805 | 459 | { |
11343788 | 460 | dTHR; |
79072805 | 461 | if (AvARRAY(comppad) != curpad) |
463ee0b2 | 462 | croak("panic: pad_swipe curpad"); |
79072805 | 463 | if (!po) |
463ee0b2 | 464 | croak("panic: pad_swipe po"); |
11343788 | 465 | #ifdef USE_THREADS |
5dc0d613 MB |
466 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n", |
467 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 468 | #else |
d9bb4600 GS |
469 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n", |
470 | (unsigned long) curpad, po)); | |
11343788 | 471 | #endif /* USE_THREADS */ |
ed6116ce | 472 | SvPADTMP_off(curpad[po]); |
a0d0e21e LW |
473 | curpad[po] = NEWSV(1107,0); |
474 | SvPADTMP_on(curpad[po]); | |
475 | if ((I32)po < padix) | |
79072805 LW |
476 | padix = po - 1; |
477 | } | |
478 | ||
d9bb4600 GS |
479 | /* XXX pad_reset() is currently disabled because it results in serious bugs. |
480 | * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed | |
481 | * on the stack by OPs that use them, there are several ways to get an alias | |
482 | * to a shared TARG. Such an alias will change randomly and unpredictably. | |
483 | * We avoid doing this until we can think of a Better Way. | |
484 | * GSAR 97-10-29 */ | |
79072805 | 485 | void |
8ac85365 | 486 | pad_reset(void) |
79072805 | 487 | { |
d9bb4600 | 488 | #ifdef USE_BROKEN_PAD_RESET |
11343788 | 489 | dTHR; |
79072805 LW |
490 | register I32 po; |
491 | ||
492 | if (AvARRAY(comppad) != curpad) | |
463ee0b2 | 493 | croak("panic: pad_reset curpad"); |
11343788 | 494 | #ifdef USE_THREADS |
5dc0d613 MB |
495 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n", |
496 | (unsigned long) thr, (unsigned long) curpad)); | |
11343788 | 497 | #else |
d9bb4600 GS |
498 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n", |
499 | (unsigned long) curpad)); | |
11343788 | 500 | #endif /* USE_THREADS */ |
748a9306 LW |
501 | if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */ |
502 | for (po = AvMAX(comppad); po > padix_floor; po--) { | |
ff0cee69 | 503 | if (curpad[po] && !SvIMMORTAL(curpad[po])) |
748a9306 LW |
504 | SvPADTMP_off(curpad[po]); |
505 | } | |
506 | padix = padix_floor; | |
79072805 | 507 | } |
d9bb4600 | 508 | #endif |
a0d0e21e | 509 | pad_reset_pending = FALSE; |
79072805 LW |
510 | } |
511 | ||
a863c7d1 | 512 | #ifdef USE_THREADS |
54b9620d | 513 | /* find_threadsv is not reentrant */ |
a863c7d1 | 514 | PADOFFSET |
54b9620d | 515 | find_threadsv(char *name) |
a863c7d1 MB |
516 | { |
517 | dTHR; | |
518 | char *p; | |
519 | PADOFFSET key; | |
554b3eca | 520 | SV **svp; |
54b9620d MB |
521 | /* We currently only handle names of a single character */ |
522 | p = strchr(threadsv_names, *name); | |
a863c7d1 MB |
523 | if (!p) |
524 | return NOT_IN_PAD; | |
54b9620d MB |
525 | key = p - threadsv_names; |
526 | svp = av_fetch(thr->threadsv, key, FALSE); | |
554b3eca MB |
527 | if (!svp) { |
528 | SV *sv = NEWSV(0, 0); | |
54b9620d | 529 | av_store(thr->threadsv, key, sv); |
940cb80d | 530 | thr->threadsvp = AvARRAY(thr->threadsv); |
554b3eca MB |
531 | /* |
532 | * Some magic variables used to be automagically initialised | |
533 | * in gv_fetchpv. Those which are now per-thread magicals get | |
534 | * initialised here instead. | |
535 | */ | |
536 | switch (*name) { | |
54b9620d MB |
537 | case '_': |
538 | break; | |
554b3eca MB |
539 | case ';': |
540 | sv_setpv(sv, "\034"); | |
54b9620d | 541 | sv_magic(sv, 0, 0, name, 1); |
554b3eca | 542 | break; |
c277df42 IZ |
543 | case '&': |
544 | case '`': | |
545 | case '\'': | |
546 | sawampersand = TRUE; | |
547 | SvREADONLY_on(sv); | |
d8b5173a | 548 | /* FALL THROUGH */ |
54b9620d MB |
549 | default: |
550 | sv_magic(sv, 0, 0, name, 1); | |
554b3eca | 551 | } |
a863c7d1 | 552 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), |
54b9620d | 553 | "find_threadsv: new SV %p for $%s%c\n", |
554b3eca MB |
554 | sv, (*name < 32) ? "^" : "", |
555 | (*name < 32) ? toCTRL(*name) : *name)); | |
a863c7d1 MB |
556 | } |
557 | return key; | |
558 | } | |
559 | #endif /* USE_THREADS */ | |
560 | ||
79072805 LW |
561 | /* Destructor */ |
562 | ||
563 | void | |
8ac85365 | 564 | op_free(OP *o) |
79072805 | 565 | { |
85e6fe83 | 566 | register OP *kid, *nextkid; |
79072805 | 567 | |
5dc0d613 | 568 | if (!o || o->op_seq == (U16)-1) |
79072805 LW |
569 | return; |
570 | ||
11343788 MB |
571 | if (o->op_flags & OPf_KIDS) { |
572 | for (kid = cUNOPo->op_first; kid; kid = nextkid) { | |
85e6fe83 | 573 | nextkid = kid->op_sibling; /* Get before next freeing kid */ |
79072805 | 574 | op_free(kid); |
85e6fe83 | 575 | } |
79072805 LW |
576 | } |
577 | ||
11343788 | 578 | switch (o->op_type) { |
8990e307 | 579 | case OP_NULL: |
11343788 | 580 | o->op_targ = 0; /* Was holding old type, if any. */ |
8990e307 | 581 | break; |
a0d0e21e | 582 | case OP_ENTEREVAL: |
11343788 | 583 | o->op_targ = 0; /* Was holding hints. */ |
a0d0e21e | 584 | break; |
554b3eca | 585 | #ifdef USE_THREADS |
2faa37cc | 586 | case OP_THREADSV: |
54b9620d | 587 | o->op_targ = 0; /* Was holding index into thr->threadsv AV. */ |
554b3eca MB |
588 | break; |
589 | #endif /* USE_THREADS */ | |
a6006777 | 590 | default: |
5196be3e | 591 | if (!(o->op_flags & OPf_REF) || (check[o->op_type] != ck_ftst)) |
a6006777 | 592 | break; |
593 | /* FALL THROUGH */ | |
463ee0b2 | 594 | case OP_GVSV: |
79072805 | 595 | case OP_GV: |
a6006777 | 596 | case OP_AELEMFAST: |
11343788 | 597 | SvREFCNT_dec(cGVOPo->op_gv); |
8990e307 LW |
598 | break; |
599 | case OP_NEXTSTATE: | |
600 | case OP_DBSTATE: | |
5196be3e | 601 | Safefree(cCOPo->cop_label); |
11343788 | 602 | SvREFCNT_dec(cCOPo->cop_filegv); |
79072805 LW |
603 | break; |
604 | case OP_CONST: | |
11343788 | 605 | SvREFCNT_dec(cSVOPo->op_sv); |
79072805 | 606 | break; |
748a9306 LW |
607 | case OP_GOTO: |
608 | case OP_NEXT: | |
609 | case OP_LAST: | |
610 | case OP_REDO: | |
11343788 | 611 | if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS)) |
748a9306 LW |
612 | break; |
613 | /* FALL THROUGH */ | |
a0d0e21e | 614 | case OP_TRANS: |
11343788 | 615 | Safefree(cPVOPo->op_pv); |
a0d0e21e LW |
616 | break; |
617 | case OP_SUBST: | |
11343788 | 618 | op_free(cPMOPo->op_pmreplroot); |
a0d0e21e | 619 | /* FALL THROUGH */ |
748a9306 | 620 | case OP_PUSHRE: |
a0d0e21e | 621 | case OP_MATCH: |
c277df42 | 622 | ReREFCNT_dec(cPMOPo->op_pmregexp); |
a0d0e21e | 623 | break; |
79072805 LW |
624 | } |
625 | ||
11343788 MB |
626 | if (o->op_targ > 0) |
627 | pad_free(o->op_targ); | |
8990e307 | 628 | |
11343788 | 629 | Safefree(o); |
79072805 LW |
630 | } |
631 | ||
8990e307 | 632 | static void |
8ac85365 | 633 | null(OP *o) |
8990e307 | 634 | { |
54b9620d | 635 | if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0) |
11343788 MB |
636 | pad_free(o->op_targ); |
637 | o->op_targ = o->op_type; | |
638 | o->op_type = OP_NULL; | |
639 | o->op_ppaddr = ppaddr[OP_NULL]; | |
8990e307 LW |
640 | } |
641 | ||
79072805 LW |
642 | /* Contextualizers */ |
643 | ||
463ee0b2 | 644 | #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o)) |
79072805 LW |
645 | |
646 | OP * | |
8ac85365 | 647 | linklist(OP *o) |
79072805 LW |
648 | { |
649 | register OP *kid; | |
650 | ||
11343788 MB |
651 | if (o->op_next) |
652 | return o->op_next; | |
79072805 LW |
653 | |
654 | /* establish postfix order */ | |
11343788 MB |
655 | if (cUNOPo->op_first) { |
656 | o->op_next = LINKLIST(cUNOPo->op_first); | |
657 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { | |
79072805 LW |
658 | if (kid->op_sibling) |
659 | kid->op_next = LINKLIST(kid->op_sibling); | |
660 | else | |
11343788 | 661 | kid->op_next = o; |
79072805 LW |
662 | } |
663 | } | |
664 | else | |
11343788 | 665 | o->op_next = o; |
79072805 | 666 | |
11343788 | 667 | return o->op_next; |
79072805 LW |
668 | } |
669 | ||
670 | OP * | |
8ac85365 | 671 | scalarkids(OP *o) |
79072805 LW |
672 | { |
673 | OP *kid; | |
11343788 MB |
674 | if (o && o->op_flags & OPf_KIDS) { |
675 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
79072805 LW |
676 | scalar(kid); |
677 | } | |
11343788 | 678 | return o; |
79072805 LW |
679 | } |
680 | ||
a0d0e21e | 681 | static OP * |
8ac85365 | 682 | scalarboolean(OP *o) |
8990e307 LW |
683 | { |
684 | if (dowarn && | |
11343788 | 685 | o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) { |
0f15f207 | 686 | dTHR; |
a0d0e21e LW |
687 | line_t oldline = curcop->cop_line; |
688 | ||
689 | if (copline != NOLINE) | |
690 | curcop->cop_line = copline; | |
691 | warn("Found = in conditional, should be =="); | |
692 | curcop->cop_line = oldline; | |
693 | } | |
11343788 | 694 | return scalar(o); |
8990e307 LW |
695 | } |
696 | ||
697 | OP * | |
8ac85365 | 698 | scalar(OP *o) |
79072805 LW |
699 | { |
700 | OP *kid; | |
701 | ||
a0d0e21e | 702 | /* assumes no premature commitment */ |
5dc0d613 MB |
703 | if (!o || (o->op_flags & OPf_WANT) || error_count |
704 | || o->op_type == OP_RETURN) | |
11343788 | 705 | return o; |
79072805 | 706 | |
5dc0d613 | 707 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR; |
79072805 | 708 | |
11343788 | 709 | switch (o->op_type) { |
79072805 | 710 | case OP_REPEAT: |
11343788 MB |
711 | if (o->op_private & OPpREPEAT_DOLIST) |
712 | null(((LISTOP*)cBINOPo->op_first)->op_first); | |
713 | scalar(cBINOPo->op_first); | |
8990e307 | 714 | break; |
79072805 LW |
715 | case OP_OR: |
716 | case OP_AND: | |
717 | case OP_COND_EXPR: | |
11343788 | 718 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
8990e307 | 719 | scalar(kid); |
79072805 | 720 | break; |
a0d0e21e | 721 | case OP_SPLIT: |
11343788 | 722 | if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) { |
a0d0e21e LW |
723 | if (!kPMOP->op_pmreplroot) |
724 | deprecate("implicit split to @_"); | |
725 | } | |
726 | /* FALL THROUGH */ | |
79072805 LW |
727 | case OP_MATCH: |
728 | case OP_SUBST: | |
729 | case OP_NULL: | |
8990e307 | 730 | default: |
11343788 MB |
731 | if (o->op_flags & OPf_KIDS) { |
732 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) | |
8990e307 LW |
733 | scalar(kid); |
734 | } | |
79072805 LW |
735 | break; |
736 | case OP_LEAVE: | |
737 | case OP_LEAVETRY: | |
5dc0d613 | 738 | kid = cLISTOPo->op_first; |
54310121 | 739 | scalar(kid); |
740 | while (kid = kid->op_sibling) { | |
741 | if (kid->op_sibling) | |
742 | scalarvoid(kid); | |
743 | else | |
744 | scalar(kid); | |
745 | } | |
0f15f207 | 746 | WITH_THR(curcop = &compiling); |
54310121 | 747 | break; |
748a9306 | 748 | case OP_SCOPE: |
79072805 | 749 | case OP_LINESEQ: |
8990e307 | 750 | case OP_LIST: |
11343788 | 751 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
79072805 LW |
752 | if (kid->op_sibling) |
753 | scalarvoid(kid); | |
754 | else | |
755 | scalar(kid); | |
756 | } | |
0f15f207 | 757 | WITH_THR(curcop = &compiling); |
79072805 LW |
758 | break; |
759 | } | |
11343788 | 760 | return o; |
79072805 LW |
761 | } |
762 | ||
763 | OP * | |
8ac85365 | 764 | scalarvoid(OP *o) |
79072805 LW |
765 | { |
766 | OP *kid; | |
8990e307 LW |
767 | char* useless = 0; |
768 | SV* sv; | |
79072805 | 769 | |
54310121 | 770 | /* assumes no premature commitment */ |
5dc0d613 MB |
771 | if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || error_count |
772 | || o->op_type == OP_RETURN) | |
11343788 | 773 | return o; |
79072805 | 774 | |
5dc0d613 | 775 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; |
79072805 | 776 | |
11343788 | 777 | switch (o->op_type) { |
79072805 | 778 | default: |
11343788 | 779 | if (!(opargs[o->op_type] & OA_FOLDCONST)) |
8990e307 | 780 | break; |
36477c24 | 781 | /* FALL THROUGH */ |
782 | case OP_REPEAT: | |
11343788 | 783 | if (o->op_flags & OPf_STACKED) |
8990e307 LW |
784 | break; |
785 | /* FALL THROUGH */ | |
786 | case OP_GVSV: | |
787 | case OP_WANTARRAY: | |
788 | case OP_GV: | |
789 | case OP_PADSV: | |
790 | case OP_PADAV: | |
791 | case OP_PADHV: | |
792 | case OP_PADANY: | |
793 | case OP_AV2ARYLEN: | |
8990e307 | 794 | case OP_REF: |
a0d0e21e LW |
795 | case OP_REFGEN: |
796 | case OP_SREFGEN: | |
8990e307 LW |
797 | case OP_DEFINED: |
798 | case OP_HEX: | |
799 | case OP_OCT: | |
800 | case OP_LENGTH: | |
801 | case OP_SUBSTR: | |
802 | case OP_VEC: | |
803 | case OP_INDEX: | |
804 | case OP_RINDEX: | |
805 | case OP_SPRINTF: | |
806 | case OP_AELEM: | |
807 | case OP_AELEMFAST: | |
808 | case OP_ASLICE: | |
8990e307 LW |
809 | case OP_HELEM: |
810 | case OP_HSLICE: | |
811 | case OP_UNPACK: | |
812 | case OP_PACK: | |
8990e307 LW |
813 | case OP_JOIN: |
814 | case OP_LSLICE: | |
815 | case OP_ANONLIST: | |
816 | case OP_ANONHASH: | |
817 | case OP_SORT: | |
818 | case OP_REVERSE: | |
819 | case OP_RANGE: | |
820 | case OP_FLIP: | |
821 | case OP_FLOP: | |
822 | case OP_CALLER: | |
823 | case OP_FILENO: | |
824 | case OP_EOF: | |
825 | case OP_TELL: | |
826 | case OP_GETSOCKNAME: | |
827 | case OP_GETPEERNAME: | |
828 | case OP_READLINK: | |
829 | case OP_TELLDIR: | |
830 | case OP_GETPPID: | |
831 | case OP_GETPGRP: | |
832 | case OP_GETPRIORITY: | |
833 | case OP_TIME: | |
834 | case OP_TMS: | |
835 | case OP_LOCALTIME: | |
836 | case OP_GMTIME: | |
837 | case OP_GHBYNAME: | |
838 | case OP_GHBYADDR: | |
839 | case OP_GHOSTENT: | |
840 | case OP_GNBYNAME: | |
841 | case OP_GNBYADDR: | |
842 | case OP_GNETENT: | |
843 | case OP_GPBYNAME: | |
844 | case OP_GPBYNUMBER: | |
845 | case OP_GPROTOENT: | |
846 | case OP_GSBYNAME: | |
847 | case OP_GSBYPORT: | |
848 | case OP_GSERVENT: | |
849 | case OP_GPWNAM: | |
850 | case OP_GPWUID: | |
851 | case OP_GGRNAM: | |
852 | case OP_GGRGID: | |
853 | case OP_GETLOGIN: | |
11343788 MB |
854 | if (!(o->op_private & OPpLVAL_INTRO)) |
855 | useless = op_desc[o->op_type]; | |
8990e307 LW |
856 | break; |
857 | ||
858 | case OP_RV2GV: | |
859 | case OP_RV2SV: | |
860 | case OP_RV2AV: | |
861 | case OP_RV2HV: | |
11343788 MB |
862 | if (!(o->op_private & OPpLVAL_INTRO) && |
863 | (!o->op_sibling || o->op_sibling->op_type != OP_READLINE)) | |
8990e307 LW |
864 | useless = "a variable"; |
865 | break; | |
79072805 | 866 | |
93a17b20 | 867 | case OP_NEXTSTATE: |
8990e307 | 868 | case OP_DBSTATE: |
0f15f207 | 869 | WITH_THR(curcop = ((COP*)o)); /* for warning below */ |
93a17b20 LW |
870 | break; |
871 | ||
79072805 | 872 | case OP_CONST: |
11343788 | 873 | sv = cSVOPo->op_sv; |
8990e307 LW |
874 | if (dowarn) { |
875 | useless = "a constant"; | |
876 | if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0)) | |
877 | useless = 0; | |
878 | else if (SvPOK(sv)) { | |
879 | if (strnEQ(SvPVX(sv), "di", 2) || | |
a0d0e21e | 880 | strnEQ(SvPVX(sv), "ds", 2) || |
8990e307 LW |
881 | strnEQ(SvPVX(sv), "ig", 2)) |
882 | useless = 0; | |
883 | } | |
884 | } | |
11343788 | 885 | null(o); /* don't execute a constant */ |
8990e307 | 886 | SvREFCNT_dec(sv); /* don't even remember it */ |
79072805 LW |
887 | break; |
888 | ||
889 | case OP_POSTINC: | |
11343788 MB |
890 | o->op_type = OP_PREINC; /* pre-increment is faster */ |
891 | o->op_ppaddr = ppaddr[OP_PREINC]; | |
79072805 LW |
892 | break; |
893 | ||
894 | case OP_POSTDEC: | |
11343788 MB |
895 | o->op_type = OP_PREDEC; /* pre-decrement is faster */ |
896 | o->op_ppaddr = ppaddr[OP_PREDEC]; | |
79072805 LW |
897 | break; |
898 | ||
79072805 LW |
899 | case OP_OR: |
900 | case OP_AND: | |
901 | case OP_COND_EXPR: | |
11343788 | 902 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
79072805 LW |
903 | scalarvoid(kid); |
904 | break; | |
5aabfad6 | 905 | |
a0d0e21e | 906 | case OP_NULL: |
11343788 | 907 | if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE) |
0f15f207 | 908 | WITH_THR(curcop = ((COP*)o)); /* for warning below */ |
11343788 | 909 | if (o->op_flags & OPf_STACKED) |
a0d0e21e | 910 | break; |
5aabfad6 | 911 | /* FALL THROUGH */ |
79072805 LW |
912 | case OP_ENTERTRY: |
913 | case OP_ENTER: | |
914 | case OP_SCALAR: | |
11343788 | 915 | if (!(o->op_flags & OPf_KIDS)) |
79072805 | 916 | break; |
54310121 | 917 | /* FALL THROUGH */ |
463ee0b2 | 918 | case OP_SCOPE: |
79072805 LW |
919 | case OP_LEAVE: |
920 | case OP_LEAVETRY: | |
a0d0e21e | 921 | case OP_LEAVELOOP: |
79072805 | 922 | case OP_LINESEQ: |
79072805 | 923 | case OP_LIST: |
11343788 | 924 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
79072805 LW |
925 | scalarvoid(kid); |
926 | break; | |
c90c0ff4 | 927 | case OP_ENTEREVAL: |
5196be3e | 928 | scalarkids(o); |
c90c0ff4 | 929 | break; |
5aabfad6 | 930 | case OP_REQUIRE: |
c90c0ff4 | 931 | /* all requires must return a boolean value */ |
5196be3e MB |
932 | o->op_flags &= ~OPf_WANT; |
933 | return scalar(o); | |
a0d0e21e | 934 | case OP_SPLIT: |
11343788 | 935 | if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) { |
a0d0e21e LW |
936 | if (!kPMOP->op_pmreplroot) |
937 | deprecate("implicit split to @_"); | |
938 | } | |
939 | break; | |
79072805 | 940 | } |
8990e307 LW |
941 | if (useless && dowarn) |
942 | warn("Useless use of %s in void context", useless); | |
11343788 | 943 | return o; |
79072805 LW |
944 | } |
945 | ||
946 | OP * | |
8ac85365 | 947 | listkids(OP *o) |
79072805 LW |
948 | { |
949 | OP *kid; | |
11343788 MB |
950 | if (o && o->op_flags & OPf_KIDS) { |
951 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
79072805 LW |
952 | list(kid); |
953 | } | |
11343788 | 954 | return o; |
79072805 LW |
955 | } |
956 | ||
957 | OP * | |
8ac85365 | 958 | list(OP *o) |
79072805 LW |
959 | { |
960 | OP *kid; | |
961 | ||
a0d0e21e | 962 | /* assumes no premature commitment */ |
5dc0d613 MB |
963 | if (!o || (o->op_flags & OPf_WANT) || error_count |
964 | || o->op_type == OP_RETURN) | |
11343788 | 965 | return o; |
79072805 | 966 | |
5dc0d613 | 967 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST; |
79072805 | 968 | |
11343788 | 969 | switch (o->op_type) { |
79072805 LW |
970 | case OP_FLOP: |
971 | case OP_REPEAT: | |
11343788 | 972 | list(cBINOPo->op_first); |
79072805 LW |
973 | break; |
974 | case OP_OR: | |
975 | case OP_AND: | |
976 | case OP_COND_EXPR: | |
11343788 | 977 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
79072805 LW |
978 | list(kid); |
979 | break; | |
980 | default: | |
981 | case OP_MATCH: | |
982 | case OP_SUBST: | |
983 | case OP_NULL: | |
11343788 | 984 | if (!(o->op_flags & OPf_KIDS)) |
79072805 | 985 | break; |
11343788 MB |
986 | if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) { |
987 | list(cBINOPo->op_first); | |
988 | return gen_constant_list(o); | |
79072805 LW |
989 | } |
990 | case OP_LIST: | |
11343788 | 991 | listkids(o); |
79072805 LW |
992 | break; |
993 | case OP_LEAVE: | |
994 | case OP_LEAVETRY: | |
5dc0d613 | 995 | kid = cLISTOPo->op_first; |
54310121 | 996 | list(kid); |
997 | while (kid = kid->op_sibling) { | |
998 | if (kid->op_sibling) | |
999 | scalarvoid(kid); | |
1000 | else | |
1001 | list(kid); | |
1002 | } | |
0f15f207 | 1003 | WITH_THR(curcop = &compiling); |
54310121 | 1004 | break; |
748a9306 | 1005 | case OP_SCOPE: |
79072805 | 1006 | case OP_LINESEQ: |
11343788 | 1007 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
79072805 LW |
1008 | if (kid->op_sibling) |
1009 | scalarvoid(kid); | |
1010 | else | |
1011 | list(kid); | |
1012 | } | |
0f15f207 | 1013 | WITH_THR(curcop = &compiling); |
79072805 | 1014 | break; |
c90c0ff4 | 1015 | case OP_REQUIRE: |
1016 | /* all requires must return a boolean value */ | |
5196be3e MB |
1017 | o->op_flags &= ~OPf_WANT; |
1018 | return scalar(o); | |
79072805 | 1019 | } |
11343788 | 1020 | return o; |
79072805 LW |
1021 | } |
1022 | ||
1023 | OP * | |
8ac85365 | 1024 | scalarseq(OP *o) |
79072805 LW |
1025 | { |
1026 | OP *kid; | |
1027 | ||
11343788 MB |
1028 | if (o) { |
1029 | if (o->op_type == OP_LINESEQ || | |
1030 | o->op_type == OP_SCOPE || | |
1031 | o->op_type == OP_LEAVE || | |
1032 | o->op_type == OP_LEAVETRY) | |
463ee0b2 | 1033 | { |
0f15f207 | 1034 | dTHR; |
11343788 | 1035 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
ed6116ce | 1036 | if (kid->op_sibling) { |
463ee0b2 | 1037 | scalarvoid(kid); |
ed6116ce | 1038 | } |
463ee0b2 LW |
1039 | } |
1040 | curcop = &compiling; | |
79072805 | 1041 | } |
11343788 | 1042 | o->op_flags &= ~OPf_PARENS; |
85e6fe83 | 1043 | if (hints & HINT_BLOCK_SCOPE) |
11343788 | 1044 | o->op_flags |= OPf_PARENS; |
79072805 | 1045 | } |
8990e307 | 1046 | else |
11343788 MB |
1047 | o = newOP(OP_STUB, 0); |
1048 | return o; | |
79072805 LW |
1049 | } |
1050 | ||
a0d0e21e | 1051 | static OP * |
8ac85365 | 1052 | modkids(OP *o, I32 type) |
79072805 LW |
1053 | { |
1054 | OP *kid; | |
11343788 MB |
1055 | if (o && o->op_flags & OPf_KIDS) { |
1056 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
463ee0b2 | 1057 | mod(kid, type); |
79072805 | 1058 | } |
11343788 | 1059 | return o; |
79072805 LW |
1060 | } |
1061 | ||
463ee0b2 | 1062 | static I32 modcount; |
79072805 LW |
1063 | |
1064 | OP * | |
8ac85365 | 1065 | mod(OP *o, I32 type) |
79072805 | 1066 | { |
11343788 | 1067 | dTHR; |
79072805 LW |
1068 | OP *kid; |
1069 | SV *sv; | |
1070 | ||
11343788 MB |
1071 | if (!o || error_count) |
1072 | return o; | |
79072805 | 1073 | |
11343788 | 1074 | switch (o->op_type) { |
68dc0745 | 1075 | case OP_UNDEF: |
3e3baf6d | 1076 | modcount++; |
5dc0d613 | 1077 | return o; |
a0d0e21e | 1078 | case OP_CONST: |
11343788 | 1079 | if (!(o->op_private & (OPpCONST_ARYBASE))) |
a0d0e21e LW |
1080 | goto nomod; |
1081 | if (eval_start && eval_start->op_type == OP_CONST) { | |
1082 | compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv); | |
1083 | eval_start = 0; | |
1084 | } | |
1085 | else if (!type) { | |
1086 | SAVEI32(compiling.cop_arybase); | |
748a9306 | 1087 | compiling.cop_arybase = 0; |
a0d0e21e LW |
1088 | } |
1089 | else if (type == OP_REFGEN) | |
1090 | goto nomod; | |
1091 | else | |
1092 | croak("That use of $[ is unsupported"); | |
1093 | break; | |
5f05dabc | 1094 | case OP_STUB: |
5196be3e | 1095 | if (o->op_flags & OPf_PARENS) |
5f05dabc | 1096 | break; |
1097 | goto nomod; | |
a0d0e21e LW |
1098 | case OP_ENTERSUB: |
1099 | if ((type == OP_UNDEF || type == OP_REFGEN) && | |
11343788 MB |
1100 | !(o->op_flags & OPf_STACKED)) { |
1101 | o->op_type = OP_RV2CV; /* entersub => rv2cv */ | |
1102 | o->op_ppaddr = ppaddr[OP_RV2CV]; | |
1103 | assert(cUNOPo->op_first->op_type == OP_NULL); | |
1104 | null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */ | |
79072805 LW |
1105 | break; |
1106 | } | |
1107 | /* FALL THROUGH */ | |
1108 | default: | |
a0d0e21e LW |
1109 | nomod: |
1110 | /* grep, foreach, subcalls, refgen */ | |
1111 | if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) | |
1112 | break; | |
46fc3d4c | 1113 | yyerror(form("Can't modify %s in %s", |
5dc0d613 | 1114 | op_desc[o->op_type], |
46fc3d4c | 1115 | type ? op_desc[type] : "local")); |
11343788 | 1116 | return o; |
79072805 | 1117 | |
a0d0e21e LW |
1118 | case OP_PREINC: |
1119 | case OP_PREDEC: | |
1120 | case OP_POW: | |
1121 | case OP_MULTIPLY: | |
1122 | case OP_DIVIDE: | |
1123 | case OP_MODULO: | |
1124 | case OP_REPEAT: | |
1125 | case OP_ADD: | |
1126 | case OP_SUBTRACT: | |
1127 | case OP_CONCAT: | |
1128 | case OP_LEFT_SHIFT: | |
1129 | case OP_RIGHT_SHIFT: | |
1130 | case OP_BIT_AND: | |
1131 | case OP_BIT_XOR: | |
1132 | case OP_BIT_OR: | |
1133 | case OP_I_MULTIPLY: | |
1134 | case OP_I_DIVIDE: | |
1135 | case OP_I_MODULO: | |
1136 | case OP_I_ADD: | |
1137 | case OP_I_SUBTRACT: | |
11343788 | 1138 | if (!(o->op_flags & OPf_STACKED)) |
a0d0e21e LW |
1139 | goto nomod; |
1140 | modcount++; | |
1141 | break; | |
1142 | ||
79072805 | 1143 | case OP_COND_EXPR: |
11343788 | 1144 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
463ee0b2 | 1145 | mod(kid, type); |
79072805 LW |
1146 | break; |
1147 | ||
1148 | case OP_RV2AV: | |
1149 | case OP_RV2HV: | |
93af7a87 | 1150 | if (!type && cUNOPo->op_first->op_type != OP_GV) |
706a304b | 1151 | croak("Can't localize through a reference"); |
11343788 | 1152 | if (type == OP_REFGEN && o->op_flags & OPf_PARENS) { |
748a9306 | 1153 | modcount = 10000; |
11343788 | 1154 | return o; /* Treat \(@foo) like ordinary list. */ |
748a9306 LW |
1155 | } |
1156 | /* FALL THROUGH */ | |
79072805 | 1157 | case OP_RV2GV: |
5dc0d613 | 1158 | if (scalar_mod_type(o, type)) |
3fe9a6f1 | 1159 | goto nomod; |
11343788 | 1160 | ref(cUNOPo->op_first, o->op_type); |
79072805 LW |
1161 | /* FALL THROUGH */ |
1162 | case OP_AASSIGN: | |
1163 | case OP_ASLICE: | |
1164 | case OP_HSLICE: | |
93a17b20 LW |
1165 | case OP_NEXTSTATE: |
1166 | case OP_DBSTATE: | |
a0d0e21e LW |
1167 | case OP_REFGEN: |
1168 | case OP_CHOMP: | |
463ee0b2 | 1169 | modcount = 10000; |
79072805 | 1170 | break; |
463ee0b2 | 1171 | case OP_RV2SV: |
11343788 | 1172 | if (!type && cUNOPo->op_first->op_type != OP_GV) |
706a304b | 1173 | croak("Can't localize through a reference"); |
aeea060c | 1174 | ref(cUNOPo->op_first, o->op_type); |
463ee0b2 | 1175 | /* FALL THROUGH */ |
79072805 | 1176 | case OP_GV: |
463ee0b2 | 1177 | case OP_AV2ARYLEN: |
85aff577 | 1178 | hints |= HINT_BLOCK_SCOPE; |
463ee0b2 | 1179 | case OP_SASSIGN: |
8990e307 LW |
1180 | case OP_AELEMFAST: |
1181 | modcount++; | |
1182 | break; | |
1183 | ||
748a9306 LW |
1184 | case OP_PADAV: |
1185 | case OP_PADHV: | |
1186 | modcount = 10000; | |
5196be3e MB |
1187 | if (type == OP_REFGEN && o->op_flags & OPf_PARENS) |
1188 | return o; /* Treat \(@foo) like ordinary list. */ | |
1189 | if (scalar_mod_type(o, type)) | |
3fe9a6f1 | 1190 | goto nomod; |
748a9306 LW |
1191 | /* FALL THROUGH */ |
1192 | case OP_PADSV: | |
1193 | modcount++; | |
1194 | if (!type) | |
1195 | croak("Can't localize lexical variable %s", | |
11343788 | 1196 | SvPV(*av_fetch(comppad_name, o->op_targ, 4), na)); |
463ee0b2 LW |
1197 | break; |
1198 | ||
554b3eca | 1199 | #ifdef USE_THREADS |
2faa37cc | 1200 | case OP_THREADSV: |
554b3eca | 1201 | modcount++; /* XXX ??? */ |
554b3eca MB |
1202 | break; |
1203 | #endif /* USE_THREADS */ | |
1204 | ||
748a9306 LW |
1205 | case OP_PUSHMARK: |
1206 | break; | |
a0d0e21e | 1207 | |
69969c6f SB |
1208 | case OP_KEYS: |
1209 | if (type != OP_SASSIGN) | |
1210 | goto nomod; | |
5f05dabc | 1211 | /* FALL THROUGH */ |
a0d0e21e | 1212 | case OP_POS: |
463ee0b2 | 1213 | case OP_VEC: |
a0d0e21e | 1214 | case OP_SUBSTR: |
11343788 MB |
1215 | pad_free(o->op_targ); |
1216 | o->op_targ = pad_alloc(o->op_type, SVs_PADMY); | |
5dc0d613 | 1217 | assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL); |
11343788 MB |
1218 | if (o->op_flags & OPf_KIDS) |
1219 | mod(cBINOPo->op_first->op_sibling, type); | |
463ee0b2 | 1220 | break; |
a0d0e21e | 1221 | |
463ee0b2 LW |
1222 | case OP_AELEM: |
1223 | case OP_HELEM: | |
11343788 | 1224 | ref(cBINOPo->op_first, o->op_type); |
68dc0745 | 1225 | if (type == OP_ENTERSUB && |
5dc0d613 MB |
1226 | !(o->op_private & (OPpLVAL_INTRO | OPpDEREF))) |
1227 | o->op_private |= OPpLVAL_DEFER; | |
a0d0e21e | 1228 | modcount++; |
463ee0b2 LW |
1229 | break; |
1230 | ||
1231 | case OP_SCOPE: | |
1232 | case OP_LEAVE: | |
1233 | case OP_ENTER: | |
11343788 MB |
1234 | if (o->op_flags & OPf_KIDS) |
1235 | mod(cLISTOPo->op_last, type); | |
a0d0e21e LW |
1236 | break; |
1237 | ||
1238 | case OP_NULL: | |
11343788 | 1239 | if (!(o->op_flags & OPf_KIDS)) |
463ee0b2 | 1240 | break; |
11343788 MB |
1241 | if (o->op_targ != OP_LIST) { |
1242 | mod(cBINOPo->op_first, type); | |
a0d0e21e LW |
1243 | break; |
1244 | } | |
1245 | /* FALL THROUGH */ | |
463ee0b2 | 1246 | case OP_LIST: |
11343788 | 1247 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
463ee0b2 LW |
1248 | mod(kid, type); |
1249 | break; | |
1250 | } | |
11343788 | 1251 | o->op_flags |= OPf_MOD; |
a0d0e21e LW |
1252 | |
1253 | if (type == OP_AASSIGN || type == OP_SASSIGN) | |
11343788 | 1254 | o->op_flags |= OPf_SPECIAL|OPf_REF; |
a0d0e21e | 1255 | else if (!type) { |
11343788 MB |
1256 | o->op_private |= OPpLVAL_INTRO; |
1257 | o->op_flags &= ~OPf_SPECIAL; | |
463ee0b2 | 1258 | } |
a0d0e21e | 1259 | else if (type != OP_GREPSTART && type != OP_ENTERSUB) |
11343788 MB |
1260 | o->op_flags |= OPf_REF; |
1261 | return o; | |
463ee0b2 LW |
1262 | } |
1263 | ||
3fe9a6f1 | 1264 | static bool |
8ac85365 | 1265 | scalar_mod_type(OP *o, I32 type) |
3fe9a6f1 | 1266 | { |
1267 | switch (type) { | |
1268 | case OP_SASSIGN: | |
5196be3e | 1269 | if (o->op_type == OP_RV2GV) |
3fe9a6f1 | 1270 | return FALSE; |
1271 | /* FALL THROUGH */ | |
1272 | case OP_PREINC: | |
1273 | case OP_PREDEC: | |
1274 | case OP_POSTINC: | |
1275 | case OP_POSTDEC: | |
1276 | case OP_I_PREINC: | |
1277 | case OP_I_PREDEC: | |
1278 | case OP_I_POSTINC: | |
1279 | case OP_I_POSTDEC: | |
1280 | case OP_POW: | |
1281 | case OP_MULTIPLY: | |
1282 | case OP_DIVIDE: | |
1283 | case OP_MODULO: | |
1284 | case OP_REPEAT: | |
1285 | case OP_ADD: | |
1286 | case OP_SUBTRACT: | |
1287 | case OP_I_MULTIPLY: | |
1288 | case OP_I_DIVIDE: | |
1289 | case OP_I_MODULO: | |
1290 | case OP_I_ADD: | |
1291 | case OP_I_SUBTRACT: | |
1292 | case OP_LEFT_SHIFT: | |
1293 | case OP_RIGHT_SHIFT: | |
1294 | case OP_BIT_AND: | |
1295 | case OP_BIT_XOR: | |
1296 | case OP_BIT_OR: | |
1297 | case OP_CONCAT: | |
1298 | case OP_SUBST: | |
1299 | case OP_TRANS: | |
1300 | case OP_ANDASSIGN: /* may work later */ | |
1301 | case OP_ORASSIGN: /* may work later */ | |
1302 | return TRUE; | |
1303 | default: | |
1304 | return FALSE; | |
1305 | } | |
1306 | } | |
1307 | ||
463ee0b2 | 1308 | OP * |
8ac85365 | 1309 | refkids(OP *o, I32 type) |
463ee0b2 LW |
1310 | { |
1311 | OP *kid; | |
11343788 MB |
1312 | if (o && o->op_flags & OPf_KIDS) { |
1313 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
463ee0b2 LW |
1314 | ref(kid, type); |
1315 | } | |
11343788 | 1316 | return o; |
463ee0b2 LW |
1317 | } |
1318 | ||
1319 | OP * | |
8ac85365 | 1320 | ref(OP *o, I32 type) |
463ee0b2 LW |
1321 | { |
1322 | OP *kid; | |
463ee0b2 | 1323 | |
11343788 MB |
1324 | if (!o || error_count) |
1325 | return o; | |
463ee0b2 | 1326 | |
11343788 | 1327 | switch (o->op_type) { |
a0d0e21e | 1328 | case OP_ENTERSUB: |
e55aaa0e | 1329 | if ((type == OP_DEFINED || type == OP_LOCK) && |
11343788 MB |
1330 | !(o->op_flags & OPf_STACKED)) { |
1331 | o->op_type = OP_RV2CV; /* entersub => rv2cv */ | |
1332 | o->op_ppaddr = ppaddr[OP_RV2CV]; | |
1333 | assert(cUNOPo->op_first->op_type == OP_NULL); | |
1334 | null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */ | |
1335 | o->op_flags |= OPf_SPECIAL; | |
8990e307 LW |
1336 | } |
1337 | break; | |
aeea060c | 1338 | |
463ee0b2 | 1339 | case OP_COND_EXPR: |
11343788 | 1340 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
463ee0b2 LW |
1341 | ref(kid, type); |
1342 | break; | |
8990e307 | 1343 | case OP_RV2SV: |
11343788 | 1344 | ref(cUNOPo->op_first, o->op_type); |
4633a7c4 LW |
1345 | /* FALL THROUGH */ |
1346 | case OP_PADSV: | |
5f05dabc | 1347 | if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { |
5dc0d613 MB |
1348 | o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV |
1349 | : type == OP_RV2HV ? OPpDEREF_HV | |
1350 | : OPpDEREF_SV); | |
11343788 | 1351 | o->op_flags |= OPf_MOD; |
a0d0e21e | 1352 | } |
8990e307 LW |
1353 | break; |
1354 | ||
2faa37cc | 1355 | case OP_THREADSV: |
a863c7d1 MB |
1356 | o->op_flags |= OPf_MOD; /* XXX ??? */ |
1357 | break; | |
1358 | ||
463ee0b2 LW |
1359 | case OP_RV2AV: |
1360 | case OP_RV2HV: | |
aeea060c | 1361 | o->op_flags |= OPf_REF; |
8990e307 | 1362 | /* FALL THROUGH */ |
463ee0b2 | 1363 | case OP_RV2GV: |
11343788 | 1364 | ref(cUNOPo->op_first, o->op_type); |
463ee0b2 | 1365 | break; |
8990e307 | 1366 | |
463ee0b2 LW |
1367 | case OP_PADAV: |
1368 | case OP_PADHV: | |
aeea060c | 1369 | o->op_flags |= OPf_REF; |
79072805 | 1370 | break; |
aeea060c | 1371 | |
8990e307 | 1372 | case OP_SCALAR: |
79072805 | 1373 | case OP_NULL: |
11343788 | 1374 | if (!(o->op_flags & OPf_KIDS)) |
463ee0b2 | 1375 | break; |
11343788 | 1376 | ref(cBINOPo->op_first, type); |
79072805 LW |
1377 | break; |
1378 | case OP_AELEM: | |
1379 | case OP_HELEM: | |
11343788 | 1380 | ref(cBINOPo->op_first, o->op_type); |
5f05dabc | 1381 | if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { |
5dc0d613 MB |
1382 | o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV |
1383 | : type == OP_RV2HV ? OPpDEREF_HV | |
1384 | : OPpDEREF_SV); | |
11343788 | 1385 | o->op_flags |= OPf_MOD; |
8990e307 | 1386 | } |
79072805 LW |
1387 | break; |
1388 | ||
463ee0b2 | 1389 | case OP_SCOPE: |
79072805 LW |
1390 | case OP_LEAVE: |
1391 | case OP_ENTER: | |
8990e307 | 1392 | case OP_LIST: |
11343788 | 1393 | if (!(o->op_flags & OPf_KIDS)) |
79072805 | 1394 | break; |
11343788 | 1395 | ref(cLISTOPo->op_last, type); |
79072805 | 1396 | break; |
a0d0e21e LW |
1397 | default: |
1398 | break; | |
79072805 | 1399 | } |
11343788 | 1400 | return scalar(o); |
8990e307 | 1401 | |
79072805 LW |
1402 | } |
1403 | ||
1404 | OP * | |
8ac85365 | 1405 | my(OP *o) |
93a17b20 LW |
1406 | { |
1407 | OP *kid; | |
93a17b20 LW |
1408 | I32 type; |
1409 | ||
11343788 MB |
1410 | if (!o || error_count) |
1411 | return o; | |
93a17b20 | 1412 | |
11343788 | 1413 | type = o->op_type; |
93a17b20 | 1414 | if (type == OP_LIST) { |
11343788 | 1415 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
93a17b20 LW |
1416 | my(kid); |
1417 | } | |
1418 | else if (type != OP_PADSV && | |
1419 | type != OP_PADAV && | |
1420 | type != OP_PADHV && | |
1421 | type != OP_PUSHMARK) | |
1422 | { | |
5dc0d613 | 1423 | yyerror(form("Can't declare %s in my", op_desc[o->op_type])); |
11343788 | 1424 | return o; |
93a17b20 | 1425 | } |
11343788 MB |
1426 | o->op_flags |= OPf_MOD; |
1427 | o->op_private |= OPpLVAL_INTRO; | |
1428 | return o; | |
93a17b20 LW |
1429 | } |
1430 | ||
1431 | OP * | |
8ac85365 | 1432 | sawparens(OP *o) |
79072805 LW |
1433 | { |
1434 | if (o) | |
1435 | o->op_flags |= OPf_PARENS; | |
1436 | return o; | |
1437 | } | |
1438 | ||
1439 | OP * | |
8ac85365 | 1440 | bind_match(I32 type, OP *left, OP *right) |
79072805 | 1441 | { |
11343788 | 1442 | OP *o; |
79072805 | 1443 | |
2ae324a7 | 1444 | if (dowarn && |
1445 | (left->op_type == OP_RV2AV || | |
1446 | left->op_type == OP_RV2HV || | |
1447 | left->op_type == OP_PADAV || | |
1448 | left->op_type == OP_PADHV)) { | |
1449 | char *desc = op_desc[(right->op_type == OP_SUBST || | |
1450 | right->op_type == OP_TRANS) | |
1451 | ? right->op_type : OP_MATCH]; | |
1452 | char *sample = ((left->op_type == OP_RV2AV || | |
1453 | left->op_type == OP_PADAV) | |
1454 | ? "@array" : "%hash"); | |
1455 | warn("Applying %s to %s will act on scalar(%s)", desc, sample, sample); | |
1456 | } | |
1457 | ||
79072805 LW |
1458 | if (right->op_type == OP_MATCH || |
1459 | right->op_type == OP_SUBST || | |
1460 | right->op_type == OP_TRANS) { | |
1461 | right->op_flags |= OPf_STACKED; | |
1462 | if (right->op_type != OP_MATCH) | |
463ee0b2 | 1463 | left = mod(left, right->op_type); |
79072805 | 1464 | if (right->op_type == OP_TRANS) |
11343788 | 1465 | o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right); |
79072805 | 1466 | else |
11343788 | 1467 | o = prepend_elem(right->op_type, scalar(left), right); |
79072805 | 1468 | if (type == OP_NOT) |
11343788 MB |
1469 | return newUNOP(OP_NOT, 0, scalar(o)); |
1470 | return o; | |
79072805 LW |
1471 | } |
1472 | else | |
1473 | return bind_match(type, left, | |
1474 | pmruntime(newPMOP(OP_MATCH, 0), right, Nullop)); | |
1475 | } | |
1476 | ||
1477 | OP * | |
8ac85365 | 1478 | invert(OP *o) |
79072805 | 1479 | { |
11343788 MB |
1480 | if (!o) |
1481 | return o; | |
79072805 | 1482 | /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */ |
11343788 | 1483 | return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o)); |
79072805 LW |
1484 | } |
1485 | ||
1486 | OP * | |
8ac85365 | 1487 | scope(OP *o) |
79072805 LW |
1488 | { |
1489 | if (o) { | |
84902520 | 1490 | if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || tainting) { |
463ee0b2 LW |
1491 | o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o); |
1492 | o->op_type = OP_LEAVE; | |
1493 | o->op_ppaddr = ppaddr[OP_LEAVE]; | |
1494 | } | |
1495 | else { | |
1496 | if (o->op_type == OP_LINESEQ) { | |
1497 | OP *kid; | |
1498 | o->op_type = OP_SCOPE; | |
1499 | o->op_ppaddr = ppaddr[OP_SCOPE]; | |
1500 | kid = ((LISTOP*)o)->op_first; | |
748a9306 LW |
1501 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){ |
1502 | SvREFCNT_dec(((COP*)kid)->cop_filegv); | |
8990e307 | 1503 | null(kid); |
748a9306 | 1504 | } |
463ee0b2 LW |
1505 | } |
1506 | else | |
748a9306 | 1507 | o = newLISTOP(OP_SCOPE, 0, o, Nullop); |
463ee0b2 | 1508 | } |
79072805 LW |
1509 | } |
1510 | return o; | |
1511 | } | |
1512 | ||
a0d0e21e | 1513 | int |
8ac85365 | 1514 | block_start(int full) |
79072805 | 1515 | { |
11343788 | 1516 | dTHR; |
a0d0e21e | 1517 | int retval = savestack_ix; |
55497cff | 1518 | SAVEI32(comppad_name_floor); |
1519 | if (full) { | |
93965878 | 1520 | if ((comppad_name_fill = AvFILLp(comppad_name)) > 0) |
55497cff | 1521 | comppad_name_floor = comppad_name_fill; |
1522 | else | |
1523 | comppad_name_floor = 0; | |
1524 | } | |
1525 | SAVEI32(min_intro_pending); | |
1526 | SAVEI32(max_intro_pending); | |
a0d0e21e | 1527 | min_intro_pending = 0; |
55497cff | 1528 | SAVEI32(comppad_name_fill); |
1529 | SAVEI32(padix_floor); | |
a0d0e21e LW |
1530 | padix_floor = padix; |
1531 | pad_reset_pending = FALSE; | |
55497cff | 1532 | SAVEI32(hints); |
a0d0e21e LW |
1533 | hints &= ~HINT_BLOCK_SCOPE; |
1534 | return retval; | |
1535 | } | |
1536 | ||
1537 | OP* | |
8ac85365 | 1538 | block_end(I32 floor, OP *seq) |
a0d0e21e | 1539 | { |
11343788 | 1540 | dTHR; |
a0d0e21e LW |
1541 | int needblockscope = hints & HINT_BLOCK_SCOPE; |
1542 | OP* retval = scalarseq(seq); | |
a0d0e21e LW |
1543 | LEAVE_SCOPE(floor); |
1544 | pad_reset_pending = FALSE; | |
1545 | if (needblockscope) | |
1546 | hints |= HINT_BLOCK_SCOPE; /* propagate out */ | |
1547 | pad_leavemy(comppad_name_fill); | |
bbce6d69 | 1548 | cop_seqmax++; |
a0d0e21e LW |
1549 | return retval; |
1550 | } | |
1551 | ||
54b9620d MB |
1552 | static OP * |
1553 | newDEFSVOP(void) | |
1554 | { | |
1555 | #ifdef USE_THREADS | |
1556 | OP *o = newOP(OP_THREADSV, 0); | |
1557 | o->op_targ = find_threadsv("_"); | |
1558 | return o; | |
1559 | #else | |
1560 | return newSVREF(newGVOP(OP_GV, 0, defgv)); | |
1561 | #endif /* USE_THREADS */ | |
1562 | } | |
1563 | ||
a0d0e21e | 1564 | void |
8ac85365 | 1565 | newPROG(OP *o) |
a0d0e21e | 1566 | { |
11343788 | 1567 | dTHR; |
a0d0e21e | 1568 | if (in_eval) { |
5dc0d613 | 1569 | eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), o); |
a0d0e21e LW |
1570 | eval_start = linklist(eval_root); |
1571 | eval_root->op_next = 0; | |
1572 | peep(eval_start); | |
1573 | } | |
1574 | else { | |
5dc0d613 | 1575 | if (!o) |
a0d0e21e | 1576 | return; |
11343788 | 1577 | main_root = scope(sawparens(scalarvoid(o))); |
a0d0e21e LW |
1578 | curcop = &compiling; |
1579 | main_start = LINKLIST(main_root); | |
1580 | main_root->op_next = 0; | |
1581 | peep(main_start); | |
748a9306 | 1582 | compcv = 0; |
3841441e | 1583 | |
4fdae800 | 1584 | /* Register with debugger */ |
84902520 | 1585 | if (PERLDB_INTER) { |
3841441e | 1586 | CV *cv = perl_get_cv("DB::postponed", FALSE); |
3841441e CS |
1587 | if (cv) { |
1588 | dSP; | |
1589 | PUSHMARK(sp); | |
1590 | XPUSHs((SV*)compiling.cop_filegv); | |
1591 | PUTBACK; | |
1592 | perl_call_sv((SV*)cv, G_DISCARD); | |
1593 | } | |
1594 | } | |
79072805 | 1595 | } |
79072805 LW |
1596 | } |
1597 | ||
1598 | OP * | |
8ac85365 | 1599 | localize(OP *o, I32 lex) |
79072805 LW |
1600 | { |
1601 | if (o->op_flags & OPf_PARENS) | |
1602 | list(o); | |
8990e307 | 1603 | else { |
8990e307 LW |
1604 | if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') { |
1605 | char *s; | |
1606 | for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ; | |
a0d0e21e | 1607 | if (*s == ';' || *s == '=') |
8990e307 LW |
1608 | warn("Parens missing around \"%s\" list", lex ? "my" : "local"); |
1609 | } | |
1610 | } | |
93a17b20 | 1611 | in_my = FALSE; |
c750a3ec | 1612 | in_my_stash = Nullhv; |
93a17b20 LW |
1613 | if (lex) |
1614 | return my(o); | |
1615 | else | |
463ee0b2 | 1616 | return mod(o, OP_NULL); /* a bit kludgey */ |
79072805 LW |
1617 | } |
1618 | ||
1619 | OP * | |
8ac85365 | 1620 | jmaybe(OP *o) |
79072805 LW |
1621 | { |
1622 | if (o->op_type == OP_LIST) { | |
554b3eca MB |
1623 | OP *o2; |
1624 | #ifdef USE_THREADS | |
2faa37cc | 1625 | o2 = newOP(OP_THREADSV, 0); |
54b9620d | 1626 | o2->op_targ = find_threadsv(";"); |
554b3eca MB |
1627 | #else |
1628 | o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))), | |
1629 | #endif /* USE_THREADS */ | |
1630 | o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o)); | |
79072805 LW |
1631 | } |
1632 | return o; | |
1633 | } | |
1634 | ||
1635 | OP * | |
8ac85365 | 1636 | fold_constants(register OP *o) |
79072805 | 1637 | { |
11343788 | 1638 | dTHR; |
79072805 LW |
1639 | register OP *curop; |
1640 | I32 type = o->op_type; | |
748a9306 | 1641 | SV *sv; |
79072805 LW |
1642 | |
1643 | if (opargs[type] & OA_RETSCALAR) | |
1644 | scalar(o); | |
1645 | if (opargs[type] & OA_TARGET) | |
ed6116ce | 1646 | o->op_targ = pad_alloc(type, SVs_PADTMP); |
79072805 | 1647 | |
85e6fe83 | 1648 | if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER)) |
a0d0e21e | 1649 | o->op_ppaddr = ppaddr[type = ++(o->op_type)]; |
85e6fe83 | 1650 | |
79072805 LW |
1651 | if (!(opargs[type] & OA_FOLDCONST)) |
1652 | goto nope; | |
1653 | ||
de939608 CS |
1654 | switch (type) { |
1655 | case OP_SPRINTF: | |
1656 | case OP_UCFIRST: | |
1657 | case OP_LCFIRST: | |
1658 | case OP_UC: | |
1659 | case OP_LC: | |
69dcf70c MB |
1660 | case OP_SLT: |
1661 | case OP_SGT: | |
1662 | case OP_SLE: | |
1663 | case OP_SGE: | |
1664 | case OP_SCMP: | |
1665 | ||
de939608 CS |
1666 | if (o->op_private & OPpLOCALE) |
1667 | goto nope; | |
1668 | } | |
1669 | ||
a0d0e21e LW |
1670 | if (error_count) |
1671 | goto nope; /* Don't try to run w/ errors */ | |
1672 | ||
79072805 | 1673 | for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { |
93a17b20 LW |
1674 | if (curop->op_type != OP_CONST && |
1675 | curop->op_type != OP_LIST && | |
1676 | curop->op_type != OP_SCALAR && | |
a0d0e21e | 1677 | curop->op_type != OP_NULL && |
93a17b20 | 1678 | curop->op_type != OP_PUSHMARK) { |
79072805 LW |
1679 | goto nope; |
1680 | } | |
1681 | } | |
1682 | ||
1683 | curop = LINKLIST(o); | |
1684 | o->op_next = 0; | |
1685 | op = curop; | |
8da795c6 | 1686 | runops(); |
748a9306 LW |
1687 | sv = *(stack_sp--); |
1688 | if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */ | |
79072805 | 1689 | pad_swipe(o->op_targ); |
748a9306 LW |
1690 | else if (SvTEMP(sv)) { /* grab mortal temp? */ |
1691 | (void)SvREFCNT_inc(sv); | |
1692 | SvTEMP_off(sv); | |
85e6fe83 | 1693 | } |
79072805 LW |
1694 | op_free(o); |
1695 | if (type == OP_RV2GV) | |
b1cb66bf | 1696 | return newGVOP(OP_GV, 0, (GV*)sv); |
748a9306 LW |
1697 | else { |
1698 | if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) { | |
1699 | IV iv = SvIV(sv); | |
1700 | if ((double)iv == SvNV(sv)) { /* can we smush double to int */ | |
1701 | SvREFCNT_dec(sv); | |
1702 | sv = newSViv(iv); | |
1703 | } | |
b1cb66bf | 1704 | else |
1705 | SvIOK_off(sv); /* undo SvIV() damage */ | |
748a9306 LW |
1706 | } |
1707 | return newSVOP(OP_CONST, 0, sv); | |
1708 | } | |
aeea060c | 1709 | |
79072805 LW |
1710 | nope: |
1711 | if (!(opargs[type] & OA_OTHERINT)) | |
1712 | return o; | |
79072805 | 1713 | |
85e6fe83 | 1714 | if (!(hints & HINT_INTEGER)) { |
a0d0e21e | 1715 | if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS)) |
85e6fe83 LW |
1716 | return o; |
1717 | ||
1718 | for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) { | |
1719 | if (curop->op_type == OP_CONST) { | |
b1cb66bf | 1720 | if (SvIOK(((SVOP*)curop)->op_sv)) |
85e6fe83 LW |
1721 | continue; |
1722 | return o; | |
1723 | } | |
1724 | if (opargs[curop->op_type] & OA_RETINTEGER) | |
79072805 LW |
1725 | continue; |
1726 | return o; | |
1727 | } | |
a0d0e21e | 1728 | o->op_ppaddr = ppaddr[++(o->op_type)]; |
79072805 LW |
1729 | } |
1730 | ||
79072805 LW |
1731 | return o; |
1732 | } | |
1733 | ||
1734 | OP * | |
8ac85365 | 1735 | gen_constant_list(register OP *o) |
79072805 | 1736 | { |
11343788 | 1737 | dTHR; |
79072805 | 1738 | register OP *curop; |
79072805 | 1739 | I32 oldtmps_floor = tmps_floor; |
79072805 | 1740 | |
a0d0e21e LW |
1741 | list(o); |
1742 | if (error_count) | |
1743 | return o; /* Don't attempt to run with errors */ | |
1744 | ||
1745 | op = curop = LINKLIST(o); | |
1746 | o->op_next = 0; | |
11343788 | 1747 | pp_pushmark(ARGS); |
8da795c6 | 1748 | runops(); |
a0d0e21e | 1749 | op = curop; |
11343788 | 1750 | pp_anonlist(ARGS); |
79072805 | 1751 | tmps_floor = oldtmps_floor; |
79072805 LW |
1752 | |
1753 | o->op_type = OP_RV2AV; | |
1754 | o->op_ppaddr = ppaddr[OP_RV2AV]; | |
79072805 | 1755 | curop = ((UNOP*)o)->op_first; |
a0d0e21e | 1756 | ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--)); |
79072805 | 1757 | op_free(curop); |
79072805 LW |
1758 | linklist(o); |
1759 | return list(o); | |
1760 | } | |
1761 | ||
1762 | OP * | |
8ac85365 | 1763 | convert(I32 type, I32 flags, OP *o) |
79072805 LW |
1764 | { |
1765 | OP *kid; | |
a0d0e21e | 1766 | OP *last = 0; |
79072805 | 1767 | |
11343788 MB |
1768 | if (!o || o->op_type != OP_LIST) |
1769 | o = newLISTOP(OP_LIST, 0, o, Nullop); | |
748a9306 | 1770 | else |
5dc0d613 | 1771 | o->op_flags &= ~OPf_WANT; |
79072805 | 1772 | |
8990e307 | 1773 | if (!(opargs[type] & OA_MARK)) |
11343788 | 1774 | null(cLISTOPo->op_first); |
8990e307 | 1775 | |
11343788 MB |
1776 | o->op_type = type; |
1777 | o->op_ppaddr = ppaddr[type]; | |
1778 | o->op_flags |= flags; | |
79072805 | 1779 | |
11343788 MB |
1780 | o = CHECKOP(type, o); |
1781 | if (o->op_type != type) | |
1782 | return o; | |
79072805 | 1783 | |
11343788 | 1784 | if (cLISTOPo->op_children < 7) { |
79072805 | 1785 | /* XXX do we really need to do this if we're done appending?? */ |
11343788 | 1786 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
79072805 | 1787 | last = kid; |
11343788 | 1788 | cLISTOPo->op_last = last; /* in case check substituted last arg */ |
79072805 LW |
1789 | } |
1790 | ||
11343788 | 1791 | return fold_constants(o); |
79072805 LW |
1792 | } |
1793 | ||
1794 | /* List constructors */ | |
1795 | ||
1796 | OP * | |
8ac85365 | 1797 | append_elem(I32 type, OP *first, OP *last) |
79072805 LW |
1798 | { |
1799 | if (!first) | |
1800 | return last; | |
8990e307 LW |
1801 | |
1802 | if (!last) | |
79072805 | 1803 | return first; |
8990e307 | 1804 | |
a0d0e21e LW |
1805 | if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS) |
1806 | return newLISTOP(type, 0, first, last); | |
79072805 | 1807 | |
a0d0e21e LW |
1808 | if (first->op_flags & OPf_KIDS) |
1809 | ((LISTOP*)first)->op_last->op_sibling = last; | |
1810 | else { | |
1811 | first->op_flags |= OPf_KIDS; | |
1812 | ((LISTOP*)first)->op_first = last; | |
1813 | } | |
1814 | ((LISTOP*)first)->op_last = last; | |
1815 | ((LISTOP*)first)->op_children++; | |
1816 | return first; | |
79072805 LW |
1817 | } |
1818 | ||
1819 | OP * | |
8ac85365 | 1820 | append_list(I32 type, LISTOP *first, LISTOP *last) |
79072805 LW |
1821 | { |
1822 | if (!first) | |
1823 | return (OP*)last; | |
8990e307 LW |
1824 | |
1825 | if (!last) | |
79072805 | 1826 | return (OP*)first; |
8990e307 LW |
1827 | |
1828 | if (first->op_type != type) | |
79072805 | 1829 | return prepend_elem(type, (OP*)first, (OP*)last); |
8990e307 LW |
1830 | |
1831 | if (last->op_type != type) | |
79072805 LW |
1832 | return append_elem(type, (OP*)first, (OP*)last); |
1833 | ||
1834 | first->op_last->op_sibling = last->op_first; | |
1835 | first->op_last = last->op_last; | |
1836 | first->op_children += last->op_children; | |
1837 | if (first->op_children) | |
1838 | last->op_flags |= OPf_KIDS; | |
1839 | ||
1840 | Safefree(last); | |
1841 | return (OP*)first; | |
1842 | } | |
1843 | ||
1844 | OP * | |
8ac85365 | 1845 | prepend_elem(I32 type, OP *first, OP *last) |
79072805 LW |
1846 | { |
1847 | if (!first) | |
1848 | return last; | |
8990e307 LW |
1849 | |
1850 | if (!last) | |
79072805 | 1851 | return first; |
8990e307 LW |
1852 | |
1853 | if (last->op_type == type) { | |
1854 | if (type == OP_LIST) { /* already a PUSHMARK there */ | |
1855 | first->op_sibling = ((LISTOP*)last)->op_first->op_sibling; | |
1856 | ((LISTOP*)last)->op_first->op_sibling = first; | |
1857 | } | |
1858 | else { | |
1859 | if (!(last->op_flags & OPf_KIDS)) { | |
1860 | ((LISTOP*)last)->op_last = first; | |
1861 | last->op_flags |= OPf_KIDS; | |
1862 | } | |
1863 | first->op_sibling = ((LISTOP*)last)->op_first; | |
1864 | ((LISTOP*)last)->op_first = first; | |
79072805 | 1865 | } |
79072805 LW |
1866 | ((LISTOP*)last)->op_children++; |
1867 | return last; | |
1868 | } | |
1869 | ||
1870 | return newLISTOP(type, 0, first, last); | |
1871 | } | |
1872 | ||
1873 | /* Constructors */ | |
1874 | ||
1875 | OP * | |
8ac85365 | 1876 | newNULLLIST(void) |
79072805 | 1877 | { |
8990e307 LW |
1878 | return newOP(OP_STUB, 0); |
1879 | } | |
1880 | ||
1881 | OP * | |
8ac85365 | 1882 | force_list(OP *o) |
8990e307 | 1883 | { |
11343788 MB |
1884 | if (!o || o->op_type != OP_LIST) |
1885 | o = newLISTOP(OP_LIST, 0, o, Nullop); | |
1886 | null(o); | |
1887 | return o; | |
79072805 LW |
1888 | } |
1889 | ||
1890 | OP * | |
8ac85365 | 1891 | newLISTOP(I32 type, I32 flags, OP *first, OP *last) |
79072805 LW |
1892 | { |
1893 | LISTOP *listop; | |
1894 | ||
1895 | Newz(1101, listop, 1, LISTOP); | |
1896 | ||
1897 | listop->op_type = type; | |
1898 | listop->op_ppaddr = ppaddr[type]; | |
1899 | listop->op_children = (first != 0) + (last != 0); | |
1900 | listop->op_flags = flags; | |
79072805 LW |
1901 | |
1902 | if (!last && first) | |
1903 | last = first; | |
1904 | else if (!first && last) | |
1905 | first = last; | |
8990e307 LW |
1906 | else if (first) |
1907 | first->op_sibling = last; | |
79072805 LW |
1908 | listop->op_first = first; |
1909 | listop->op_last = last; | |
8990e307 LW |
1910 | if (type == OP_LIST) { |
1911 | OP* pushop; | |
1912 | pushop = newOP(OP_PUSHMARK, 0); | |
1913 | pushop->op_sibling = first; | |
1914 | listop->op_first = pushop; | |
1915 | listop->op_flags |= OPf_KIDS; | |
1916 | if (!last) | |
1917 | listop->op_last = pushop; | |
1918 | } | |
1919 | else if (listop->op_children) | |
1920 | listop->op_flags |= OPf_KIDS; | |
79072805 LW |
1921 | |
1922 | return (OP*)listop; | |
1923 | } | |
1924 | ||
1925 | OP * | |
8ac85365 | 1926 | newOP(I32 type, I32 flags) |
79072805 | 1927 | { |
11343788 MB |
1928 | OP *o; |
1929 | Newz(1101, o, 1, OP); | |
1930 | o->op_type = type; | |
1931 | o->op_ppaddr = ppaddr[type]; | |
1932 | o->op_flags = flags; | |
79072805 | 1933 | |
11343788 MB |
1934 | o->op_next = o; |
1935 | o->op_private = 0 + (flags >> 8); | |
79072805 | 1936 | if (opargs[type] & OA_RETSCALAR) |
11343788 | 1937 | scalar(o); |
79072805 | 1938 | if (opargs[type] & OA_TARGET) |
11343788 MB |
1939 | o->op_targ = pad_alloc(type, SVs_PADTMP); |
1940 | return CHECKOP(type, o); | |
79072805 LW |
1941 | } |
1942 | ||
1943 | OP * | |
8ac85365 | 1944 | newUNOP(I32 type, I32 flags, OP *first) |
79072805 LW |
1945 | { |
1946 | UNOP *unop; | |
1947 | ||
93a17b20 | 1948 | if (!first) |
aeea060c | 1949 | first = newOP(OP_STUB, 0); |
8990e307 LW |
1950 | if (opargs[type] & OA_MARK) |
1951 | first = force_list(first); | |
93a17b20 | 1952 | |
79072805 LW |
1953 | Newz(1101, unop, 1, UNOP); |
1954 | unop->op_type = type; | |
1955 | unop->op_ppaddr = ppaddr[type]; | |
1956 | unop->op_first = first; | |
1957 | unop->op_flags = flags | OPf_KIDS; | |
c07a80fd | 1958 | unop->op_private = 1 | (flags >> 8); |
c277df42 IZ |
1959 | #if 1 |
1960 | if(type == OP_STUDY && first->op_type == OP_MATCH) { | |
1961 | first->op_type = OP_PUSHRE; | |
1962 | first->op_ppaddr = ppaddr[OP_PUSHRE]; | |
1963 | } | |
1964 | #endif | |
e50aee73 | 1965 | unop = (UNOP*) CHECKOP(type, unop); |
79072805 LW |
1966 | if (unop->op_next) |
1967 | return (OP*)unop; | |
1968 | ||
a0d0e21e | 1969 | return fold_constants((OP *) unop); |
79072805 LW |
1970 | } |
1971 | ||
1972 | OP * | |
8ac85365 | 1973 | newBINOP(I32 type, I32 flags, OP *first, OP *last) |
79072805 LW |
1974 | { |
1975 | BINOP *binop; | |
1976 | Newz(1101, binop, 1, BINOP); | |
1977 | ||
1978 | if (!first) | |
1979 | first = newOP(OP_NULL, 0); | |
1980 | ||
1981 | binop->op_type = type; | |
1982 | binop->op_ppaddr = ppaddr[type]; | |
1983 | binop->op_first = first; | |
1984 | binop->op_flags = flags | OPf_KIDS; | |
1985 | if (!last) { | |
1986 | last = first; | |
c07a80fd | 1987 | binop->op_private = 1 | (flags >> 8); |
79072805 LW |
1988 | } |
1989 | else { | |
c07a80fd | 1990 | binop->op_private = 2 | (flags >> 8); |
79072805 LW |
1991 | first->op_sibling = last; |
1992 | } | |
1993 | ||
e50aee73 | 1994 | binop = (BINOP*)CHECKOP(type, binop); |
79072805 LW |
1995 | if (binop->op_next) |
1996 | return (OP*)binop; | |
1997 | ||
1998 | binop->op_last = last = binop->op_first->op_sibling; | |
1999 | ||
a0d0e21e | 2000 | return fold_constants((OP *)binop); |
79072805 LW |
2001 | } |
2002 | ||
2003 | OP * | |
8ac85365 | 2004 | pmtrans(OP *o, OP *expr, OP *repl) |
79072805 | 2005 | { |
79072805 LW |
2006 | SV *tstr = ((SVOP*)expr)->op_sv; |
2007 | SV *rstr = ((SVOP*)repl)->op_sv; | |
463ee0b2 LW |
2008 | STRLEN tlen; |
2009 | STRLEN rlen; | |
ec49126f | 2010 | register U8 *t = (U8*)SvPV(tstr, tlen); |
2011 | register U8 *r = (U8*)SvPV(rstr, rlen); | |
79072805 LW |
2012 | register I32 i; |
2013 | register I32 j; | |
8ac85365 | 2014 | I32 Delete; |
79072805 | 2015 | I32 complement; |
5d06d08e | 2016 | I32 squash; |
79072805 LW |
2017 | register short *tbl; |
2018 | ||
11343788 MB |
2019 | tbl = (short*)cPVOPo->op_pv; |
2020 | complement = o->op_private & OPpTRANS_COMPLEMENT; | |
8ac85365 | 2021 | Delete = o->op_private & OPpTRANS_DELETE; |
5d06d08e | 2022 | squash = o->op_private & OPpTRANS_SQUASH; |
79072805 LW |
2023 | |
2024 | if (complement) { | |
2025 | Zero(tbl, 256, short); | |
2026 | for (i = 0; i < tlen; i++) | |
ec49126f | 2027 | tbl[t[i]] = -1; |
79072805 LW |
2028 | for (i = 0, j = 0; i < 256; i++) { |
2029 | if (!tbl[i]) { | |
2030 | if (j >= rlen) { | |
8ac85365 | 2031 | if (Delete) |
79072805 LW |
2032 | tbl[i] = -2; |
2033 | else if (rlen) | |
ec49126f | 2034 | tbl[i] = r[j-1]; |
79072805 LW |
2035 | else |
2036 | tbl[i] = i; | |
2037 | } | |
2038 | else | |
ec49126f | 2039 | tbl[i] = r[j++]; |
79072805 LW |
2040 | } |
2041 | } | |
2042 | } | |
2043 | else { | |
8ac85365 | 2044 | if (!rlen && !Delete) { |
79072805 | 2045 | r = t; rlen = tlen; |
5d06d08e MB |
2046 | if (!squash) |
2047 | o->op_private |= OPpTRANS_COUNTONLY; | |
79072805 LW |
2048 | } |
2049 | for (i = 0; i < 256; i++) | |
2050 | tbl[i] = -1; | |
2051 | for (i = 0, j = 0; i < tlen; i++,j++) { | |
2052 | if (j >= rlen) { | |
8ac85365 | 2053 | if (Delete) { |
ec49126f | 2054 | if (tbl[t[i]] == -1) |
2055 | tbl[t[i]] = -2; | |
79072805 LW |
2056 | continue; |
2057 | } | |
2058 | --j; | |
2059 | } | |
ec49126f | 2060 | if (tbl[t[i]] == -1) |
2061 | tbl[t[i]] = r[j]; | |
79072805 LW |
2062 | } |
2063 | } | |
2064 | op_free(expr); | |
2065 | op_free(repl); | |
2066 | ||
11343788 | 2067 | return o; |
79072805 LW |
2068 | } |
2069 | ||
2070 | OP * | |
8ac85365 | 2071 | newPMOP(I32 type, I32 flags) |
79072805 | 2072 | { |
11343788 | 2073 | dTHR; |
79072805 LW |
2074 | PMOP *pmop; |
2075 | ||
2076 | Newz(1101, pmop, 1, PMOP); | |
2077 | pmop->op_type = type; | |
2078 | pmop->op_ppaddr = ppaddr[type]; | |
2079 | pmop->op_flags = flags; | |
c07a80fd | 2080 | pmop->op_private = 0 | (flags >> 8); |
79072805 | 2081 | |
36477c24 | 2082 | if (hints & HINT_LOCALE) |
2083 | pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE); | |
2084 | ||
79072805 | 2085 | /* link into pm list */ |
a0d0e21e | 2086 | if (type != OP_TRANS && curstash) { |
79072805 LW |
2087 | pmop->op_pmnext = HvPMROOT(curstash); |
2088 | HvPMROOT(curstash) = pmop; | |
2089 | } | |
2090 | ||
2091 | return (OP*)pmop; | |
2092 | } | |
2093 | ||
2094 | OP * | |
8ac85365 | 2095 | pmruntime(OP *o, OP *expr, OP *repl) |
79072805 LW |
2096 | { |
2097 | PMOP *pm; | |
2098 | LOGOP *rcop; | |
2099 | ||
11343788 MB |
2100 | if (o->op_type == OP_TRANS) |
2101 | return pmtrans(o, expr, repl); | |
79072805 | 2102 | |
3e3baf6d | 2103 | hints |= HINT_BLOCK_SCOPE; |
11343788 | 2104 | pm = (PMOP*)o; |
79072805 LW |
2105 | |
2106 | if (expr->op_type == OP_CONST) { | |
463ee0b2 | 2107 | STRLEN plen; |
79072805 | 2108 | SV *pat = ((SVOP*)expr)->op_sv; |
463ee0b2 | 2109 | char *p = SvPV(pat, plen); |
11343788 | 2110 | if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) { |
93a17b20 | 2111 | sv_setpvn(pat, "\\s+", 3); |
463ee0b2 | 2112 | p = SvPV(pat, plen); |
79072805 LW |
2113 | pm->op_pmflags |= PMf_SKIPWHITE; |
2114 | } | |
e50aee73 | 2115 | pm->op_pmregexp = pregcomp(p, p + plen, pm); |
aeea060c | 2116 | if (strEQ("\\s+", pm->op_pmregexp->precomp)) |
85e6fe83 | 2117 | pm->op_pmflags |= PMf_WHITE; |
79072805 LW |
2118 | op_free(expr); |
2119 | } | |
2120 | else { | |
463ee0b2 LW |
2121 | if (pm->op_pmflags & PMf_KEEP) |
2122 | expr = newUNOP(OP_REGCMAYBE,0,expr); | |
2123 | ||
79072805 LW |
2124 | Newz(1101, rcop, 1, LOGOP); |
2125 | rcop->op_type = OP_REGCOMP; | |
2126 | rcop->op_ppaddr = ppaddr[OP_REGCOMP]; | |
2127 | rcop->op_first = scalar(expr); | |
2128 | rcop->op_flags |= OPf_KIDS; | |
2129 | rcop->op_private = 1; | |
11343788 | 2130 | rcop->op_other = o; |
79072805 LW |
2131 | |
2132 | /* establish postfix order */ | |
463ee0b2 LW |
2133 | if (pm->op_pmflags & PMf_KEEP) { |
2134 | LINKLIST(expr); | |
2135 | rcop->op_next = expr; | |
2136 | ((UNOP*)expr)->op_first->op_next = (OP*)rcop; | |
2137 | } | |
2138 | else { | |
2139 | rcop->op_next = LINKLIST(expr); | |
2140 | expr->op_next = (OP*)rcop; | |
2141 | } | |
79072805 | 2142 | |
11343788 | 2143 | prepend_elem(o->op_type, scalar((OP*)rcop), o); |
79072805 LW |
2144 | } |
2145 | ||
2146 | if (repl) { | |
748a9306 LW |
2147 | OP *curop; |
2148 | if (pm->op_pmflags & PMf_EVAL) | |
2149 | curop = 0; | |
554b3eca | 2150 | #ifdef USE_THREADS |
2faa37cc | 2151 | else if (repl->op_type == OP_THREADSV |
554b3eca | 2152 | && strchr("&`'123456789+", |
54b9620d | 2153 | threadsv_names[repl->op_targ])) |
554b3eca MB |
2154 | { |
2155 | curop = 0; | |
2156 | } | |
2157 | #endif /* USE_THREADS */ | |
748a9306 LW |
2158 | else if (repl->op_type == OP_CONST) |
2159 | curop = repl; | |
79072805 | 2160 | else { |
79072805 LW |
2161 | OP *lastop = 0; |
2162 | for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) { | |
2163 | if (opargs[curop->op_type] & OA_DANGEROUS) { | |
554b3eca | 2164 | #ifdef USE_THREADS |
2faa37cc | 2165 | if (curop->op_type == OP_THREADSV |
554b3eca MB |
2166 | && strchr("&`'123456789+", curop->op_private)) { |
2167 | break; | |
2168 | } | |
2169 | #else | |
79072805 LW |
2170 | if (curop->op_type == OP_GV) { |
2171 | GV *gv = ((GVOP*)curop)->op_gv; | |
93a17b20 | 2172 | if (strchr("&`'123456789+", *GvENAME(gv))) |
79072805 LW |
2173 | break; |
2174 | } | |
554b3eca | 2175 | #endif /* USE_THREADS */ |
79072805 LW |
2176 | else if (curop->op_type == OP_RV2CV) |
2177 | break; | |
2178 | else if (curop->op_type == OP_RV2SV || | |
2179 | curop->op_type == OP_RV2AV || | |
2180 | curop->op_type == OP_RV2HV || | |
2181 | curop->op_type == OP_RV2GV) { | |
2182 | if (lastop && lastop->op_type != OP_GV) /*funny deref?*/ | |
2183 | break; | |
2184 | } | |
748a9306 LW |
2185 | else if (curop->op_type == OP_PADSV || |
2186 | curop->op_type == OP_PADAV || | |
2187 | curop->op_type == OP_PADHV || | |
554b3eca | 2188 | curop->op_type == OP_PADANY) { |
748a9306 LW |
2189 | /* is okay */ |
2190 | } | |
79072805 LW |
2191 | else |
2192 | break; | |
2193 | } | |
2194 | lastop = curop; | |
2195 | } | |
748a9306 LW |
2196 | } |
2197 | if (curop == repl) { | |
2198 | pm->op_pmflags |= PMf_CONST; /* const for long enough */ | |
4633a7c4 | 2199 | pm->op_pmpermflags |= PMf_CONST; /* const for long enough */ |
11343788 | 2200 | prepend_elem(o->op_type, scalar(repl), o); |
748a9306 LW |
2201 | } |
2202 | else { | |
2203 | Newz(1101, rcop, 1, LOGOP); | |
2204 | rcop->op_type = OP_SUBSTCONT; | |
2205 | rcop->op_ppaddr = ppaddr[OP_SUBSTCONT]; | |
2206 | rcop->op_first = scalar(repl); | |
2207 | rcop->op_flags |= OPf_KIDS; | |
2208 | rcop->op_private = 1; | |
11343788 | 2209 | rcop->op_other = o; |
748a9306 LW |
2210 | |
2211 | /* establish postfix order */ | |
2212 | rcop->op_next = LINKLIST(repl); | |
2213 | repl->op_next = (OP*)rcop; | |
2214 | ||
2215 | pm->op_pmreplroot = scalar((OP*)rcop); | |
2216 | pm->op_pmreplstart = LINKLIST(rcop); | |
2217 | rcop->op_next = 0; | |
79072805 LW |
2218 | } |
2219 | } | |
2220 | ||
2221 | return (OP*)pm; | |
2222 | } | |
2223 | ||
2224 | OP * | |
8ac85365 | 2225 | newSVOP(I32 type, I32 flags, SV *sv) |
79072805 LW |
2226 | { |
2227 | SVOP *svop; | |
2228 | Newz(1101, svop, 1, SVOP); | |
2229 | svop->op_type = type; | |
2230 | svop->op_ppaddr = ppaddr[type]; | |
2231 | svop->op_sv = sv; | |
2232 | svop->op_next = (OP*)svop; | |
2233 | svop->op_flags = flags; | |
2234 | if (opargs[type] & OA_RETSCALAR) | |
463ee0b2 | 2235 | scalar((OP*)svop); |
79072805 | 2236 | if (opargs[type] & OA_TARGET) |
ed6116ce | 2237 | svop->op_targ = pad_alloc(type, SVs_PADTMP); |
e50aee73 | 2238 | return CHECKOP(type, svop); |
79072805 LW |
2239 | } |
2240 | ||
2241 | OP * | |
8ac85365 | 2242 | newGVOP(I32 type, I32 flags, GV *gv) |
79072805 | 2243 | { |
11343788 | 2244 | dTHR; |
79072805 LW |
2245 | GVOP *gvop; |
2246 | Newz(1101, gvop, 1, GVOP); | |
2247 | gvop->op_type = type; | |
2248 | gvop->op_ppaddr = ppaddr[type]; | |
8990e307 | 2249 | gvop->op_gv = (GV*)SvREFCNT_inc(gv); |
79072805 LW |
2250 | gvop->op_next = (OP*)gvop; |
2251 | gvop->op_flags = flags; | |
2252 | if (opargs[type] & OA_RETSCALAR) | |
463ee0b2 | 2253 | scalar((OP*)gvop); |
79072805 | 2254 | if (opargs[type] & OA_TARGET) |
ed6116ce | 2255 | gvop->op_targ = pad_alloc(type, SVs_PADTMP); |
e50aee73 | 2256 | return CHECKOP(type, gvop); |
79072805 LW |
2257 | } |
2258 | ||
2259 | OP * | |
8ac85365 | 2260 | newPVOP(I32 type, I32 flags, char *pv) |
79072805 LW |
2261 | { |
2262 | PVOP *pvop; | |
2263 | Newz(1101, pvop, 1, PVOP); | |
2264 | pvop->op_type = type; | |
2265 | pvop->op_ppaddr = ppaddr[type]; | |
2266 | pvop->op_pv = pv; | |
2267 | pvop->op_next = (OP*)pvop; | |
2268 | pvop->op_flags = flags; | |
2269 | if (opargs[type] & OA_RETSCALAR) | |
463ee0b2 | 2270 | scalar((OP*)pvop); |
79072805 | 2271 | if (opargs[type] & OA_TARGET) |
ed6116ce | 2272 | pvop->op_targ = pad_alloc(type, SVs_PADTMP); |
e50aee73 | 2273 | return CHECKOP(type, pvop); |
79072805 LW |
2274 | } |
2275 | ||
79072805 | 2276 | void |
8ac85365 | 2277 | package(OP *o) |
79072805 | 2278 | { |
11343788 | 2279 | dTHR; |
93a17b20 | 2280 | SV *sv; |
79072805 LW |
2281 | |
2282 | save_hptr(&curstash); | |
2283 | save_item(curstname); | |
11343788 | 2284 | if (o) { |
463ee0b2 LW |
2285 | STRLEN len; |
2286 | char *name; | |
11343788 | 2287 | sv = cSVOPo->op_sv; |
463ee0b2 | 2288 | name = SvPV(sv, len); |
b1cb66bf | 2289 | curstash = gv_stashpvn(name,len,TRUE); |
463ee0b2 | 2290 | sv_setpvn(curstname, name, len); |
11343788 | 2291 | op_free(o); |
93a17b20 LW |
2292 | } |
2293 | else { | |
2294 | sv_setpv(curstname,"<none>"); | |
2295 | curstash = Nullhv; | |
2296 | } | |
79072805 | 2297 | copline = NOLINE; |
8990e307 | 2298 | expect = XSTATE; |
79072805 LW |
2299 | } |
2300 | ||
85e6fe83 | 2301 | void |
8ac85365 | 2302 | utilize(int aver, I32 floor, OP *version, OP *id, OP *arg) |
85e6fe83 | 2303 | { |
a0d0e21e LW |
2304 | OP *pack; |
2305 | OP *meth; | |
2306 | OP *rqop; | |
2307 | OP *imop; | |
b1cb66bf | 2308 | OP *veop; |
85e6fe83 | 2309 | |
a0d0e21e LW |
2310 | if (id->op_type != OP_CONST) |
2311 | croak("Module name must be constant"); | |
85e6fe83 | 2312 | |
b1cb66bf | 2313 | veop = Nullop; |
2314 | ||
2315 | if(version != Nullop) { | |
2316 | SV *vesv = ((SVOP*)version)->op_sv; | |
2317 | ||
2318 | if (arg == Nullop && !SvNIOK(vesv)) { | |
2319 | arg = version; | |
2320 | } | |
2321 | else { | |
2322 | OP *pack; | |
2323 | OP *meth; | |
2324 | ||
2325 | if (version->op_type != OP_CONST || !SvNIOK(vesv)) | |
2326 | croak("Version number must be constant number"); | |
2327 | ||
2328 | /* Make copy of id so we don't free it twice */ | |
2329 | pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv)); | |
2330 | ||
2331 | /* Fake up a method call to VERSION */ | |
2332 | meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7)); | |
2333 | veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, | |
2334 | append_elem(OP_LIST, | |
2335 | prepend_elem(OP_LIST, pack, list(version)), | |
2336 | newUNOP(OP_METHOD, 0, meth))); | |
2337 | } | |
2338 | } | |
aeea060c | 2339 | |
a0d0e21e | 2340 | /* Fake up an import/unimport */ |
4633a7c4 LW |
2341 | if (arg && arg->op_type == OP_STUB) |
2342 | imop = arg; /* no import on explicit () */ | |
b1cb66bf | 2343 | else if(SvNIOK(((SVOP*)id)->op_sv)) { |
2344 | imop = Nullop; /* use 5.0; */ | |
2345 | } | |
4633a7c4 LW |
2346 | else { |
2347 | /* Make copy of id so we don't free it twice */ | |
2348 | pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv)); | |
4633a7c4 LW |
2349 | meth = newSVOP(OP_CONST, 0, |
2350 | aver | |
2351 | ? newSVpv("import", 6) | |
2352 | : newSVpv("unimport", 8) | |
2353 | ); | |
2354 | imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, | |
a0d0e21e LW |
2355 | append_elem(OP_LIST, |
2356 | prepend_elem(OP_LIST, pack, list(arg)), | |
2357 | newUNOP(OP_METHOD, 0, meth))); | |
4633a7c4 LW |
2358 | } |
2359 | ||
2360 | /* Fake up a require */ | |
2361 | rqop = newUNOP(OP_REQUIRE, 0, id); | |
a0d0e21e LW |
2362 | |
2363 | /* Fake up the BEGIN {}, which does its thing immediately. */ | |
c07a80fd | 2364 | newSUB(floor, |
a0d0e21e | 2365 | newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)), |
4633a7c4 | 2366 | Nullop, |
a0d0e21e | 2367 | append_elem(OP_LINESEQ, |
b1cb66bf | 2368 | append_elem(OP_LINESEQ, |
2369 | newSTATEOP(0, Nullch, rqop), | |
2370 | newSTATEOP(0, Nullch, veop)), | |
a0d0e21e | 2371 | newSTATEOP(0, Nullch, imop) )); |
85e6fe83 | 2372 | |
85e6fe83 LW |
2373 | copline = NOLINE; |
2374 | expect = XSTATE; | |
2375 | } | |
2376 | ||
79072805 | 2377 | OP * |
8ac85365 | 2378 | newSLICEOP(I32 flags, OP *subscript, OP *listval) |
79072805 LW |
2379 | { |
2380 | return newBINOP(OP_LSLICE, flags, | |
8990e307 LW |
2381 | list(force_list(subscript)), |
2382 | list(force_list(listval)) ); | |
79072805 LW |
2383 | } |
2384 | ||
2385 | static I32 | |
8ac85365 | 2386 | list_assignment(register OP *o) |
79072805 | 2387 | { |
11343788 | 2388 | if (!o) |
79072805 LW |
2389 | return TRUE; |
2390 | ||
11343788 MB |
2391 | if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS) |
2392 | o = cUNOPo->op_first; | |
79072805 | 2393 | |
11343788 MB |
2394 | if (o->op_type == OP_COND_EXPR) { |
2395 | I32 t = list_assignment(cCONDOPo->op_first->op_sibling); | |
2396 | I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling); | |
79072805 LW |
2397 | |
2398 | if (t && f) | |
2399 | return TRUE; | |
2400 | if (t || f) | |
2401 | yyerror("Assignment to both a list and a scalar"); | |
2402 | return FALSE; | |
2403 | } | |
2404 | ||
11343788 MB |
2405 | if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS || |
2406 | o->op_type == OP_RV2AV || o->op_type == OP_RV2HV || | |
2407 | o->op_type == OP_ASLICE || o->op_type == OP_HSLICE) | |
79072805 LW |
2408 | return TRUE; |
2409 | ||
11343788 | 2410 | if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) |
93a17b20 LW |
2411 | return TRUE; |
2412 | ||
11343788 | 2413 | if (o->op_type == OP_RV2SV) |
79072805 LW |
2414 | return FALSE; |
2415 | ||
2416 | return FALSE; | |
2417 | } | |
2418 | ||
2419 | OP * | |
8ac85365 | 2420 | newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right) |
79072805 | 2421 | { |
11343788 | 2422 | OP *o; |
79072805 | 2423 | |
a0d0e21e LW |
2424 | if (optype) { |
2425 | if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) { | |
2426 | return newLOGOP(optype, 0, | |
2427 | mod(scalar(left), optype), | |
2428 | newUNOP(OP_SASSIGN, 0, scalar(right))); | |
2429 | } | |
2430 | else { | |
2431 | return newBINOP(optype, OPf_STACKED, | |
2432 | mod(scalar(left), optype), scalar(right)); | |
2433 | } | |
2434 | } | |
2435 | ||
79072805 | 2436 | if (list_assignment(left)) { |
463ee0b2 | 2437 | modcount = 0; |
748a9306 | 2438 | eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ |
463ee0b2 | 2439 | left = mod(left, OP_AASSIGN); |
748a9306 LW |
2440 | if (eval_start) |
2441 | eval_start = 0; | |
2442 | else { | |
a0d0e21e LW |
2443 | op_free(left); |
2444 | op_free(right); | |
2445 | return Nullop; | |
2446 | } | |
11343788 | 2447 | o = newBINOP(OP_AASSIGN, flags, |
8990e307 LW |
2448 | list(force_list(right)), |
2449 | list(force_list(left)) ); | |
11343788 | 2450 | o->op_private = 0 | (flags >> 8); |
a0d0e21e | 2451 | if (!(left->op_private & OPpLVAL_INTRO)) { |
748a9306 | 2452 | static int generation = 100; |
79072805 | 2453 | OP *curop; |
11343788 | 2454 | OP *lastop = o; |
79072805 | 2455 | generation++; |
11343788 | 2456 | for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { |
79072805 LW |
2457 | if (opargs[curop->op_type] & OA_DANGEROUS) { |
2458 | if (curop->op_type == OP_GV) { | |
2459 | GV *gv = ((GVOP*)curop)->op_gv; | |
2460 | if (gv == defgv || SvCUR(gv) == generation) | |
2461 | break; | |
2462 | SvCUR(gv) = generation; | |
2463 | } | |
748a9306 LW |
2464 | else if (curop->op_type == OP_PADSV || |
2465 | curop->op_type == OP_PADAV || | |
2466 | curop->op_type == OP_PADHV || | |
2467 | curop->op_type == OP_PADANY) { | |
2468 | SV **svp = AvARRAY(comppad_name); | |
8e07c86e | 2469 | SV *sv = svp[curop->op_targ]; |
748a9306 LW |
2470 | if (SvCUR(sv) == generation) |
2471 | break; | |
2472 | SvCUR(sv) = generation; /* (SvCUR not used any more) */ | |
2473 | } | |
79072805 LW |
2474 | else if (curop->op_type == OP_RV2CV) |
2475 | break; | |
2476 | else if (curop->op_type == OP_RV2SV || | |
2477 | curop->op_type == OP_RV2AV || | |
2478 | curop->op_type == OP_RV2HV || | |
2479 | curop->op_type == OP_RV2GV) { | |
2480 | if (lastop->op_type != OP_GV) /* funny deref? */ | |
2481 | break; | |
2482 | } | |
2483 | else | |
2484 | break; | |
2485 | } | |
2486 | lastop = curop; | |
2487 | } | |
11343788 MB |
2488 | if (curop != o) |
2489 | o->op_private = OPpASSIGN_COMMON; | |
79072805 | 2490 | } |
c07a80fd | 2491 | if (right && right->op_type == OP_SPLIT) { |
2492 | OP* tmpop; | |
2493 | if ((tmpop = ((LISTOP*)right)->op_first) && | |
2494 | tmpop->op_type == OP_PUSHRE) | |
2495 | { | |
2496 | PMOP *pm = (PMOP*)tmpop; | |
2497 | if (left->op_type == OP_RV2AV && | |
2498 | !(left->op_private & OPpLVAL_INTRO) && | |
11343788 | 2499 | !(o->op_private & OPpASSIGN_COMMON) ) |
c07a80fd | 2500 | { |
2501 | tmpop = ((UNOP*)left)->op_first; | |
2502 | if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) { | |
2503 | pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv; | |
2504 | pm->op_pmflags |= PMf_ONCE; | |
11343788 | 2505 | tmpop = cUNOPo->op_first; /* to list (nulled) */ |
c07a80fd | 2506 | tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */ |
2507 | tmpop->op_sibling = Nullop; /* don't free split */ | |
2508 | right->op_next = tmpop->op_next; /* fix starting loc */ | |
11343788 | 2509 | op_free(o); /* blow off assign */ |
54310121 | 2510 | right->op_flags &= ~OPf_WANT; |
a5f75d66 | 2511 | /* "I don't know and I don't care." */ |
c07a80fd | 2512 | return right; |
2513 | } | |
2514 | } | |
2515 | else { | |
2516 | if (modcount < 10000 && | |
2517 | ((LISTOP*)right)->op_last->op_type == OP_CONST) | |
2518 | { | |
2519 | SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv; | |
2520 | if (SvIVX(sv) == 0) | |
2521 | sv_setiv(sv, modcount+1); | |
2522 | } | |
2523 | } | |
2524 | } | |
2525 | } | |
11343788 | 2526 | return o; |
79072805 LW |
2527 | } |
2528 | if (!right) | |
2529 | right = newOP(OP_UNDEF, 0); | |
2530 | if (right->op_type == OP_READLINE) { | |
2531 | right->op_flags |= OPf_STACKED; | |
463ee0b2 | 2532 | return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right)); |
79072805 | 2533 | } |
a0d0e21e | 2534 | else { |
748a9306 | 2535 | eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ |
11343788 | 2536 | o = newBINOP(OP_SASSIGN, flags, |
463ee0b2 | 2537 | scalar(right), mod(scalar(left), OP_SASSIGN) ); |
748a9306 LW |
2538 | if (eval_start) |
2539 | eval_start = 0; | |
2540 | else { | |
11343788 | 2541 | op_free(o); |
a0d0e21e LW |
2542 | return Nullop; |
2543 | } | |
2544 | } | |
11343788 | 2545 | return o; |
79072805 LW |
2546 | } |
2547 | ||
2548 | OP * | |
8ac85365 | 2549 | newSTATEOP(I32 flags, char *label, OP *o) |
79072805 | 2550 | { |
11343788 | 2551 | dTHR; |
bbce6d69 | 2552 | U32 seq = intro_my(); |
79072805 LW |
2553 | register COP *cop; |
2554 | ||
2555 | Newz(1101, cop, 1, COP); | |
84902520 | 2556 | if (PERLDB_LINE && curcop->cop_line && curstash != debstash) { |
8990e307 LW |
2557 | cop->op_type = OP_DBSTATE; |
2558 | cop->op_ppaddr = ppaddr[ OP_DBSTATE ]; | |
2559 | } | |
2560 | else { | |
2561 | cop->op_type = OP_NEXTSTATE; | |
2562 | cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ]; | |
2563 | } | |
79072805 | 2564 | cop->op_flags = flags; |
c07a80fd | 2565 | cop->op_private = 0 | (flags >> 8); |
ff0cee69 | 2566 | #ifdef NATIVE_HINTS |
2567 | cop->op_private |= NATIVE_HINTS; | |
2568 | #endif | |
79072805 LW |
2569 | cop->op_next = (OP*)cop; |
2570 | ||
463ee0b2 LW |
2571 | if (label) { |
2572 | cop->cop_label = label; | |
85e6fe83 | 2573 | hints |= HINT_BLOCK_SCOPE; |
463ee0b2 | 2574 | } |
bbce6d69 | 2575 | cop->cop_seq = seq; |
a0d0e21e | 2576 | cop->cop_arybase = curcop->cop_arybase; |
79072805 LW |
2577 | |
2578 | if (copline == NOLINE) | |
2579 | cop->cop_line = curcop->cop_line; | |
2580 | else { | |
2581 | cop->cop_line = copline; | |
2582 | copline = NOLINE; | |
2583 | } | |
44a8e56a | 2584 | cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv); |
79072805 LW |
2585 | cop->cop_stash = curstash; |
2586 | ||
84902520 | 2587 | if (PERLDB_LINE && curstash != debstash) { |
93a17b20 LW |
2588 | SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE); |
2589 | if (svp && *svp != &sv_undef && !SvIOK(*svp)) { | |
a0d0e21e | 2590 | (void)SvIOK_on(*svp); |
a5f75d66 | 2591 | SvIVX(*svp) = 1; |
93a17b20 LW |
2592 | SvSTASH(*svp) = (HV*)cop; |
2593 | } | |
2594 | } | |
2595 | ||
11343788 | 2596 | return prepend_elem(OP_LINESEQ, (OP*)cop, o); |
79072805 LW |
2597 | } |
2598 | ||
bbce6d69 | 2599 | /* "Introduce" my variables to visible status. */ |
2600 | U32 | |
8ac85365 | 2601 | intro_my(void) |
bbce6d69 | 2602 | { |
2603 | SV **svp; | |
2604 | SV *sv; | |
2605 | I32 i; | |
2606 | ||
2607 | if (! min_intro_pending) | |
2608 | return cop_seqmax; | |
2609 | ||
2610 | svp = AvARRAY(comppad_name); | |
2611 | for (i = min_intro_pending; i <= max_intro_pending; i++) { | |
2612 | if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) { | |
2613 | SvIVX(sv) = 999999999; /* Don't know scope end yet. */ | |
2614 | SvNVX(sv) = (double)cop_seqmax; | |
2615 | } | |
2616 | } | |
2617 | min_intro_pending = 0; | |
2618 | comppad_name_fill = max_intro_pending; /* Needn't search higher */ | |
2619 | return cop_seqmax++; | |
2620 | } | |
2621 | ||
79072805 | 2622 | OP * |
8ac85365 | 2623 | newLOGOP(I32 type, I32 flags, OP *first, OP *other) |
79072805 | 2624 | { |
11343788 | 2625 | dTHR; |
79072805 | 2626 | LOGOP *logop; |
11343788 | 2627 | OP *o; |
79072805 | 2628 | |
a0d0e21e LW |
2629 | if (type == OP_XOR) /* Not short circuit, but here by precedence. */ |
2630 | return newBINOP(type, flags, scalar(first), scalar(other)); | |
2631 | ||
8990e307 | 2632 | scalarboolean(first); |
79072805 LW |
2633 | /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */ |
2634 | if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) { | |
2635 | if (type == OP_AND || type == OP_OR) { | |
2636 | if (type == OP_AND) | |
2637 | type = OP_OR; | |
2638 | else | |
2639 | type = OP_AND; | |
11343788 MB |
2640 | o = first; |
2641 | first = cUNOPo->op_first; | |
2642 | if (o->op_next) | |
2643 | first->op_next = o->op_next; | |
2644 | cUNOPo->op_first = Nullop; | |
2645 | op_free(o); | |
79072805 LW |
2646 | } |
2647 | } | |
2648 | if (first->op_type == OP_CONST) { | |
93a17b20 | 2649 | if (dowarn && (first->op_private & OPpCONST_BARE)) |
c07a80fd | 2650 | warn("Probable precedence problem on %s", op_desc[type]); |
79072805 LW |
2651 | if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) { |
2652 | op_free(first); | |
2653 | return other; | |
2654 | } | |
2655 | else { | |
2656 | op_free(other); | |
2657 | return first; | |
2658 | } | |
2659 | } | |
2660 | else if (first->op_type == OP_WANTARRAY) { | |
2661 | if (type == OP_AND) | |
2662 | list(other); | |
2663 | else | |
2664 | scalar(other); | |
2665 | } | |
a6006777 | 2666 | else if (dowarn && (first->op_flags & OPf_KIDS)) { |
2667 | OP *k1 = ((UNOP*)first)->op_first; | |
2668 | OP *k2 = k1->op_sibling; | |
2669 | OPCODE warnop = 0; | |
2670 | switch (first->op_type) | |
2671 | { | |
2672 | case OP_NULL: | |
2673 | if (k2 && k2->op_type == OP_READLINE | |
2674 | && (k2->op_flags & OPf_STACKED) | |
2675 | && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV)) | |
2676 | warnop = k2->op_type; | |
2677 | break; | |
2678 | ||
2679 | case OP_SASSIGN: | |
68dc0745 | 2680 | if (k1->op_type == OP_READDIR |
2681 | || k1->op_type == OP_GLOB | |
2682 | || k1->op_type == OP_EACH) | |
a6006777 | 2683 | warnop = k1->op_type; |
2684 | break; | |
2685 | } | |
8ebc5c01 | 2686 | if (warnop) { |
2687 | line_t oldline = curcop->cop_line; | |
2688 | curcop->cop_line = copline; | |
68dc0745 | 2689 | warn("Value of %s%s can be \"0\"; test with defined()", |
2690 | op_desc[warnop], | |
2691 | ((warnop == OP_READLINE || warnop == OP_GLOB) | |
2692 | ? " construct" : "() operator")); | |
2693 | curcop->cop_line = oldline; | |
8ebc5c01 | 2694 | } |
a6006777 | 2695 | } |
79072805 LW |
2696 | |
2697 | if (!other) | |
2698 | return first; | |
2699 | ||
a0d0e21e LW |
2700 | if (type == OP_ANDASSIGN || type == OP_ORASSIGN) |
2701 | other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */ | |
2702 | ||
79072805 LW |
2703 | Newz(1101, logop, 1, LOGOP); |
2704 | ||
2705 | logop->op_type = type; | |
2706 | logop->op_ppaddr = ppaddr[type]; | |
2707 | logop->op_first = first; | |
2708 | logop->op_flags = flags | OPf_KIDS; | |
2709 | logop->op_other = LINKLIST(other); | |
c07a80fd | 2710 | logop->op_private = 1 | (flags >> 8); |
79072805 LW |
2711 | |
2712 | /* establish postfix order */ | |
2713 | logop->op_next = LINKLIST(first); | |
2714 | first->op_next = (OP*)logop; | |
2715 | first->op_sibling = other; | |
2716 | ||
11343788 MB |
2717 | o = newUNOP(OP_NULL, 0, (OP*)logop); |
2718 | other->op_next = o; | |
79072805 | 2719 | |
11343788 | 2720 | return o; |
79072805 LW |
2721 | } |
2722 | ||
2723 | OP * | |
8ac85365 | 2724 | newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop) |
79072805 | 2725 | { |
11343788 | 2726 | dTHR; |
79072805 | 2727 | CONDOP *condop; |
11343788 | 2728 | OP *o; |
79072805 | 2729 | |
b1cb66bf | 2730 | if (!falseop) |
2731 | return newLOGOP(OP_AND, 0, first, trueop); | |
2732 | if (!trueop) | |
2733 | return newLOGOP(OP_OR, 0, first, falseop); | |
79072805 | 2734 | |
8990e307 | 2735 | scalarboolean(first); |
79072805 LW |
2736 | if (first->op_type == OP_CONST) { |
2737 | if (SvTRUE(((SVOP*)first)->op_sv)) { | |
2738 | op_free(first); | |
b1cb66bf | 2739 | op_free(falseop); |
2740 | return trueop; | |
79072805 LW |
2741 | } |
2742 | else { | |
2743 | op_free(first); | |
b1cb66bf | 2744 | op_free(trueop); |
2745 | return falseop; | |
79072805 LW |
2746 | } |
2747 | } | |
2748 | else if (first->op_type == OP_WANTARRAY) { | |
b1cb66bf | 2749 | list(trueop); |
2750 | scalar(falseop); | |
79072805 LW |
2751 | } |
2752 | Newz(1101, condop, 1, CONDOP); | |
2753 | ||
2754 | condop->op_type = OP_COND_EXPR; | |
2755 | condop->op_ppaddr = ppaddr[OP_COND_EXPR]; | |
2756 | condop->op_first = first; | |
2757 | condop->op_flags = flags | OPf_KIDS; | |
b1cb66bf | 2758 | condop->op_true = LINKLIST(trueop); |
2759 | condop->op_false = LINKLIST(falseop); | |
c07a80fd | 2760 | condop->op_private = 1 | (flags >> 8); |
79072805 LW |
2761 | |
2762 | /* establish postfix order */ | |
2763 | condop->op_next = LINKLIST(first); | |
2764 | first->op_next = (OP*)condop; | |
2765 | ||
b1cb66bf | 2766 | first->op_sibling = trueop; |
2767 | trueop->op_sibling = falseop; | |
11343788 | 2768 | o = newUNOP(OP_NULL, 0, (OP*)condop); |
79072805 | 2769 | |
5dc0d613 MB |
2770 | trueop->op_next = o; |
2771 | falseop->op_next = o; | |
79072805 | 2772 | |
11343788 | 2773 | return o; |
79072805 LW |
2774 | } |
2775 | ||
2776 | OP * | |
8ac85365 | 2777 | newRANGE(I32 flags, OP *left, OP *right) |
79072805 | 2778 | { |
b35b2403 | 2779 | dTHR; |
79072805 LW |
2780 | CONDOP *condop; |
2781 | OP *flip; | |
2782 | OP *flop; | |
11343788 | 2783 | OP *o; |
79072805 LW |
2784 | |
2785 | Newz(1101, condop, 1, CONDOP); | |
2786 | ||
2787 | condop->op_type = OP_RANGE; | |
2788 | condop->op_ppaddr = ppaddr[OP_RANGE]; | |
2789 | condop->op_first = left; | |
2790 | condop->op_flags = OPf_KIDS; | |
2791 | condop->op_true = LINKLIST(left); | |
2792 | condop->op_false = LINKLIST(right); | |
c07a80fd | 2793 | condop->op_private = 1 | (flags >> 8); |
79072805 LW |
2794 | |
2795 | left->op_sibling = right; | |
2796 | ||
2797 | condop->op_next = (OP*)condop; | |
2798 | flip = newUNOP(OP_FLIP, flags, (OP*)condop); | |
2799 | flop = newUNOP(OP_FLOP, 0, flip); | |
11343788 | 2800 | o = newUNOP(OP_NULL, 0, flop); |
79072805 LW |
2801 | linklist(flop); |
2802 | ||
2803 | left->op_next = flip; | |
2804 | right->op_next = flop; | |
2805 | ||
ed6116ce | 2806 | condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY); |
79072805 | 2807 | sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV); |
ed6116ce | 2808 | flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY); |
79072805 LW |
2809 | sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV); |
2810 | ||
2811 | flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; | |
2812 | flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; | |
2813 | ||
11343788 | 2814 | flip->op_next = o; |
79072805 | 2815 | if (!flip->op_private || !flop->op_private) |
11343788 | 2816 | linklist(o); /* blow off optimizer unless constant */ |
79072805 | 2817 | |
11343788 | 2818 | return o; |
79072805 LW |
2819 | } |
2820 | ||
2821 | OP * | |
8ac85365 | 2822 | newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block) |
79072805 | 2823 | { |
11343788 | 2824 | dTHR; |
463ee0b2 | 2825 | OP* listop; |
11343788 | 2826 | OP* o; |
463ee0b2 | 2827 | int once = block && block->op_flags & OPf_SPECIAL && |
a0d0e21e | 2828 | (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL); |
93a17b20 | 2829 | |
463ee0b2 LW |
2830 | if (expr) { |
2831 | if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv)) | |
2832 | return block; /* do {} while 0 does once */ | |
fb73857a | 2833 | if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB |
2834 | || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { | |
774d564b | 2835 | expr = newUNOP(OP_DEFINED, 0, |
54b9620d | 2836 | newASSIGNOP(0, newDEFSVOP(), 0, expr) ); |
774d564b | 2837 | } |
463ee0b2 | 2838 | } |
93a17b20 | 2839 | |
8990e307 | 2840 | listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0)); |
11343788 | 2841 | o = newLOGOP(OP_AND, 0, expr, listop); |
463ee0b2 | 2842 | |
11343788 | 2843 | ((LISTOP*)listop)->op_last->op_next = LINKLIST(o); |
79072805 | 2844 | |
11343788 MB |
2845 | if (once && o != listop) |
2846 | o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other; | |
79072805 | 2847 | |
11343788 MB |
2848 | if (o == listop) |
2849 | o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */ | |
748a9306 | 2850 | |
11343788 MB |
2851 | o->op_flags |= flags; |
2852 | o = scope(o); | |
2853 | o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/ | |
2854 | return o; | |
79072805 LW |
2855 | } |
2856 | ||
2857 | OP * | |
8ac85365 | 2858 | newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont) |
79072805 | 2859 | { |
11343788 | 2860 | dTHR; |
79072805 LW |
2861 | OP *redo; |
2862 | OP *next = 0; | |
2863 | OP *listop; | |
11343788 | 2864 | OP *o; |
79072805 LW |
2865 | OP *condop; |
2866 | ||
fb73857a | 2867 | if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB |
2868 | || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) { | |
748a9306 | 2869 | expr = newUNOP(OP_DEFINED, 0, |
54b9620d | 2870 | newASSIGNOP(0, newDEFSVOP(), 0, expr) ); |
748a9306 | 2871 | } |
79072805 LW |
2872 | |
2873 | if (!block) | |
2874 | block = newOP(OP_NULL, 0); | |
2875 | ||
2876 | if (cont) | |
2877 | next = LINKLIST(cont); | |
fb73857a | 2878 | if (expr) { |
79072805 | 2879 | cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0)); |
fb73857a | 2880 | if ((line_t)whileline != NOLINE) { |
2881 | copline = whileline; | |
2882 | cont = append_elem(OP_LINESEQ, cont, | |
2883 | newSTATEOP(0, Nullch, Nullop)); | |
2884 | } | |
2885 | } | |
79072805 | 2886 | |
463ee0b2 | 2887 | listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont); |
79072805 LW |
2888 | redo = LINKLIST(listop); |
2889 | ||
2890 | if (expr) { | |
11343788 MB |
2891 | o = newLOGOP(OP_AND, 0, expr, scalar(listop)); |
2892 | if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) { | |
85e6fe83 | 2893 | op_free(expr); /* oops, it's a while (0) */ |
463ee0b2 LW |
2894 | op_free((OP*)loop); |
2895 | return Nullop; /* (listop already freed by newLOGOP) */ | |
2896 | } | |
aeea060c | 2897 | ((LISTOP*)listop)->op_last->op_next = condop = |
11343788 | 2898 | (o == listop ? redo : LINKLIST(o)); |
79072805 LW |
2899 | if (!next) |
2900 | next = condop; | |
2901 | } | |
2902 | else | |
11343788 | 2903 | o = listop; |
79072805 LW |
2904 | |
2905 | if (!loop) { | |
2906 | Newz(1101,loop,1,LOOP); | |
2907 | loop->op_type = OP_ENTERLOOP; | |
2908 | loop->op_ppaddr = ppaddr[OP_ENTERLOOP]; | |
2909 | loop->op_private = 0; | |
2910 | loop->op_next = (OP*)loop; | |
2911 | } | |
2912 | ||
11343788 | 2913 | o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o); |
79072805 LW |
2914 | |
2915 | loop->op_redoop = redo; | |
11343788 | 2916 | loop->op_lastop = o; |
79072805 LW |
2917 | |
2918 | if (next) | |
2919 | loop->op_nextop = next; | |
2920 | else | |
11343788 | 2921 | loop->op_nextop = o; |
79072805 | 2922 | |
11343788 MB |
2923 | o->op_flags |= flags; |
2924 | o->op_private |= (flags >> 8); | |
2925 | return o; | |
79072805 LW |
2926 | } |
2927 | ||
2928 | OP * | |
a0d0e21e | 2929 | #ifndef CAN_PROTOTYPE |
79072805 LW |
2930 | newFOROP(flags,label,forline,sv,expr,block,cont) |
2931 | I32 flags; | |
2932 | char *label; | |
2933 | line_t forline; | |
2934 | OP* sv; | |
2935 | OP* expr; | |
2936 | OP*block; | |
2937 | OP*cont; | |
8990e307 LW |
2938 | #else |
2939 | newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont) | |
a0d0e21e | 2940 | #endif /* CAN_PROTOTYPE */ |
79072805 LW |
2941 | { |
2942 | LOOP *loop; | |
fb73857a | 2943 | OP *wop; |
85e6fe83 | 2944 | int padoff = 0; |
4633a7c4 | 2945 | I32 iterflags = 0; |
79072805 | 2946 | |
79072805 | 2947 | if (sv) { |
85e6fe83 | 2948 | if (sv->op_type == OP_RV2SV) { /* symbol table variable */ |
748a9306 LW |
2949 | sv->op_type = OP_RV2GV; |
2950 | sv->op_ppaddr = ppaddr[OP_RV2GV]; | |
79072805 | 2951 | } |
85e6fe83 LW |
2952 | else if (sv->op_type == OP_PADSV) { /* private variable */ |
2953 | padoff = sv->op_targ; | |
2954 | op_free(sv); | |
2955 | sv = Nullop; | |
2956 | } | |
54b9620d MB |
2957 | else if (sv->op_type == OP_THREADSV) { /* per-thread variable */ |
2958 | padoff = sv->op_targ; | |
2959 | iterflags |= OPf_SPECIAL; | |
2960 | op_free(sv); | |
2961 | sv = Nullop; | |
2962 | } | |
79072805 | 2963 | else |
c07a80fd | 2964 | croak("Can't use %s for loop variable", op_desc[sv->op_type]); |
79072805 LW |
2965 | } |
2966 | else { | |
54b9620d MB |
2967 | #ifdef USE_THREADS |
2968 | padoff = find_threadsv("_"); | |
2969 | iterflags |= OPf_SPECIAL; | |
2970 | #else | |
79072805 | 2971 | sv = newGVOP(OP_GV, 0, defgv); |
54b9620d | 2972 | #endif |
79072805 | 2973 | } |
5f05dabc | 2974 | if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) { |
4633a7c4 LW |
2975 | expr = scalar(ref(expr, OP_ITER)); |
2976 | iterflags |= OPf_STACKED; | |
2977 | } | |
2978 | loop = (LOOP*)list(convert(OP_ENTERITER, iterflags, | |
a0d0e21e LW |
2979 | append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART), |
2980 | scalar(sv)))); | |
85e6fe83 LW |
2981 | assert(!loop->op_next); |
2982 | Renew(loop, 1, LOOP); | |
2983 | loop->op_targ = padoff; | |
fb73857a | 2984 | wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont); |
2985 | copline = forline; | |
2986 | return newSTATEOP(0, label, wop); | |
79072805 LW |
2987 | } |
2988 | ||
8990e307 | 2989 | OP* |
8ac85365 | 2990 | newLOOPEX(I32 type, OP *label) |
8990e307 | 2991 | { |
11343788 MB |
2992 | dTHR; |
2993 | OP *o; | |
8990e307 | 2994 | if (type != OP_GOTO || label->op_type == OP_CONST) { |
cdaebead MB |
2995 | /* "last()" means "last" */ |
2996 | if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) | |
2997 | o = newOP(type, OPf_SPECIAL); | |
2998 | else { | |
2999 | o = newPVOP(type, 0, savepv(label->op_type == OP_CONST | |
3000 | ? SvPVx(((SVOP*)label)->op_sv, na) | |
3001 | : "")); | |
3002 | } | |
8990e307 LW |
3003 | op_free(label); |
3004 | } | |
3005 | else { | |
a0d0e21e LW |
3006 | if (label->op_type == OP_ENTERSUB) |
3007 | label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN)); | |
11343788 | 3008 | o = newUNOP(type, OPf_STACKED, label); |
8990e307 | 3009 | } |
85e6fe83 | 3010 | hints |= HINT_BLOCK_SCOPE; |
11343788 | 3011 | return o; |
8990e307 LW |
3012 | } |
3013 | ||
79072805 | 3014 | void |
8ac85365 | 3015 | cv_undef(CV *cv) |
79072805 | 3016 | { |
11343788 MB |
3017 | dTHR; |
3018 | #ifdef USE_THREADS | |
e858de61 MB |
3019 | if (CvMUTEXP(cv)) { |
3020 | MUTEX_DESTROY(CvMUTEXP(cv)); | |
3021 | Safefree(CvMUTEXP(cv)); | |
3022 | CvMUTEXP(cv) = 0; | |
3023 | } | |
11343788 MB |
3024 | #endif /* USE_THREADS */ |
3025 | ||
a0d0e21e | 3026 | if (!CvXSUB(cv) && CvROOT(cv)) { |
11343788 MB |
3027 | #ifdef USE_THREADS |
3028 | if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr)) | |
3029 | croak("Can't undef active subroutine"); | |
3030 | #else | |
a0d0e21e LW |
3031 | if (CvDEPTH(cv)) |
3032 | croak("Can't undef active subroutine"); | |
11343788 | 3033 | #endif /* USE_THREADS */ |
8990e307 | 3034 | ENTER; |
a0d0e21e LW |
3035 | |
3036 | SAVESPTR(curpad); | |
3037 | curpad = 0; | |
3038 | ||
a5f75d66 | 3039 | if (!CvCLONED(cv)) |
748a9306 | 3040 | op_free(CvROOT(cv)); |
79072805 | 3041 | CvROOT(cv) = Nullop; |
8990e307 | 3042 | LEAVE; |
79072805 | 3043 | } |
1d5db326 | 3044 | SvPOK_off((SV*)cv); /* forget prototype */ |
44a8e56a | 3045 | CvFLAGS(cv) = 0; |
8e07c86e AD |
3046 | SvREFCNT_dec(CvGV(cv)); |
3047 | CvGV(cv) = Nullgv; | |
3048 | SvREFCNT_dec(CvOUTSIDE(cv)); | |
3049 | CvOUTSIDE(cv) = Nullcv; | |
3050 | if (CvPADLIST(cv)) { | |
8ebc5c01 | 3051 | /* may be during global destruction */ |
3052 | if (SvREFCNT(CvPADLIST(cv))) { | |
93965878 | 3053 | I32 i = AvFILLp(CvPADLIST(cv)); |
8ebc5c01 | 3054 | while (i >= 0) { |
3055 | SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE); | |
46fc3d4c | 3056 | SV* sv = svp ? *svp : Nullsv; |
3057 | if (!sv) | |
3058 | continue; | |
3059 | if (sv == (SV*)comppad_name) | |
3060 | comppad_name = Nullav; | |
3061 | else if (sv == (SV*)comppad) { | |
3062 | comppad = Nullav; | |
3063 | curpad = Null(SV**); | |
3064 | } | |
3065 | SvREFCNT_dec(sv); | |
8ebc5c01 | 3066 | } |
3067 | SvREFCNT_dec((SV*)CvPADLIST(cv)); | |
8e07c86e | 3068 | } |
8e07c86e AD |
3069 | CvPADLIST(cv) = Nullav; |
3070 | } | |
79072805 LW |
3071 | } |
3072 | ||
5f05dabc | 3073 | #ifdef DEBUG_CLOSURES |
3074 | static void | |
3075 | cv_dump(cv) | |
3076 | CV* cv; | |
3077 | { | |
3078 | CV *outside = CvOUTSIDE(cv); | |
3079 | AV* padlist = CvPADLIST(cv); | |
4fdae800 | 3080 | AV* pad_name; |
3081 | AV* pad; | |
3082 | SV** pname; | |
3083 | SV** ppad; | |
5f05dabc | 3084 | I32 ix; |
3085 | ||
fb73857a | 3086 | PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n", |
ab50184a CS |
3087 | cv, |
3088 | (CvANON(cv) ? "ANON" | |
3089 | : (cv == main_cv) ? "MAIN" | |
07055b4c | 3090 | : CvUNIQUE(outside) ? "UNIQUE" |
44a8e56a | 3091 | : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"), |
ab50184a CS |
3092 | outside, |
3093 | (!outside ? "null" | |
3094 | : CvANON(outside) ? "ANON" | |
3095 | : (outside == main_cv) ? "MAIN" | |
07055b4c | 3096 | : CvUNIQUE(outside) ? "UNIQUE" |
44a8e56a | 3097 | : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED")); |
5f05dabc | 3098 | |
4fdae800 | 3099 | if (!padlist) |
3100 | return; | |
3101 | ||
3102 | pad_name = (AV*)*av_fetch(padlist, 0, FALSE); | |
3103 | pad = (AV*)*av_fetch(padlist, 1, FALSE); | |
3104 | pname = AvARRAY(pad_name); | |
3105 | ppad = AvARRAY(pad); | |
3106 | ||
93965878 | 3107 | for (ix = 1; ix <= AvFILLp(pad_name); ix++) { |
5f05dabc | 3108 | if (SvPOK(pname[ix])) |
fb73857a | 3109 | PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n", |
4fdae800 | 3110 | ix, ppad[ix], |
3111 | SvFAKE(pname[ix]) ? "FAKE " : "", | |
3112 | SvPVX(pname[ix]), | |
ab50184a CS |
3113 | (long)I_32(SvNVX(pname[ix])), |
3114 | (long)SvIVX(pname[ix])); | |
5f05dabc | 3115 | } |
3116 | } | |
3117 | #endif /* DEBUG_CLOSURES */ | |
3118 | ||
3119 | static CV * | |
8ac85365 | 3120 | cv_clone2(CV *proto, CV *outside) |
748a9306 | 3121 | { |
11343788 | 3122 | dTHR; |
748a9306 LW |
3123 | AV* av; |
3124 | I32 ix; | |
3125 | AV* protopadlist = CvPADLIST(proto); | |
3126 | AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE); | |
3127 | AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE); | |
5f05dabc | 3128 | SV** pname = AvARRAY(protopad_name); |
3129 | SV** ppad = AvARRAY(protopad); | |
93965878 NIS |
3130 | I32 fname = AvFILLp(protopad_name); |
3131 | I32 fpad = AvFILLp(protopad); | |
748a9306 LW |
3132 | AV* comppadlist; |
3133 | CV* cv; | |
3134 | ||
07055b4c CS |
3135 | assert(!CvUNIQUE(proto)); |
3136 | ||
748a9306 LW |
3137 | ENTER; |
3138 | SAVESPTR(curpad); | |
3139 | SAVESPTR(comppad); | |
46fc3d4c | 3140 | SAVESPTR(comppad_name); |
748a9306 LW |
3141 | SAVESPTR(compcv); |
3142 | ||
3143 | cv = compcv = (CV*)NEWSV(1104,0); | |
fa83b5b6 | 3144 | sv_upgrade((SV *)cv, SvTYPE(proto)); |
a5f75d66 | 3145 | CvCLONED_on(cv); |
5f05dabc | 3146 | if (CvANON(proto)) |
3147 | CvANON_on(cv); | |
748a9306 | 3148 | |
11343788 | 3149 | #ifdef USE_THREADS |
12ca11f6 | 3150 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3151 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 MB |
3152 | CvOWNER(cv) = 0; |
3153 | #endif /* USE_THREADS */ | |
748a9306 | 3154 | CvFILEGV(cv) = CvFILEGV(proto); |
44a8e56a | 3155 | CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto)); |
748a9306 LW |
3156 | CvSTASH(cv) = CvSTASH(proto); |
3157 | CvROOT(cv) = CvROOT(proto); | |
3158 | CvSTART(cv) = CvSTART(proto); | |
5f05dabc | 3159 | if (outside) |
3160 | CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside); | |
748a9306 | 3161 | |
68dc0745 | 3162 | if (SvPOK(proto)) |
3163 | sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto)); | |
3164 | ||
46fc3d4c | 3165 | comppad_name = newAV(); |
3166 | for (ix = fname; ix >= 0; ix--) | |
3167 | av_store(comppad_name, ix, SvREFCNT_inc(pname[ix])); | |
748a9306 LW |
3168 | |
3169 | comppad = newAV(); | |
3170 | ||
3171 | comppadlist = newAV(); | |
3172 | AvREAL_off(comppadlist); | |
46fc3d4c | 3173 | av_store(comppadlist, 0, (SV*)comppad_name); |
b355b4e0 | 3174 | av_store(comppadlist, 1, (SV*)comppad); |
748a9306 | 3175 | CvPADLIST(cv) = comppadlist; |
93965878 | 3176 | av_fill(comppad, AvFILLp(protopad)); |
748a9306 LW |
3177 | curpad = AvARRAY(comppad); |
3178 | ||
3179 | av = newAV(); /* will be @_ */ | |
3180 | av_extend(av, 0); | |
3181 | av_store(comppad, 0, (SV*)av); | |
3182 | AvFLAGS(av) = AVf_REIFY; | |
3183 | ||
9607fc9c | 3184 | for (ix = fpad; ix > 0; ix--) { |
3185 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; | |
aa689395 | 3186 | if (namesv && namesv != &sv_undef) { |
3187 | char *name = SvPVX(namesv); /* XXX */ | |
3188 | if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */ | |
3189 | I32 off = pad_findlex(name, ix, SvIVX(namesv), | |
5f05dabc | 3190 | CvOUTSIDE(cv), cxstack_ix); |
3191 | if (!off) | |
3192 | curpad[ix] = SvREFCNT_inc(ppad[ix]); | |
3193 | else if (off != ix) | |
748a9306 LW |
3194 | croak("panic: cv_clone: %s", name); |
3195 | } | |
3196 | else { /* our own lexical */ | |
aa689395 | 3197 | SV* sv; |
5f05dabc | 3198 | if (*name == '&') { |
3199 | /* anon code -- we'll come back for it */ | |
3200 | sv = SvREFCNT_inc(ppad[ix]); | |
3201 | } | |
3202 | else if (*name == '@') | |
3203 | sv = (SV*)newAV(); | |
748a9306 | 3204 | else if (*name == '%') |
5f05dabc | 3205 | sv = (SV*)newHV(); |
748a9306 | 3206 | else |
5f05dabc | 3207 | sv = NEWSV(0,0); |
3208 | if (!SvPADBUSY(sv)) | |
3209 | SvPADMY_on(sv); | |
3210 | curpad[ix] = sv; | |
748a9306 LW |
3211 | } |
3212 | } | |
3213 | else { | |
aa689395 | 3214 | SV* sv = NEWSV(0,0); |
748a9306 | 3215 | SvPADTMP_on(sv); |
5f05dabc | 3216 | curpad[ix] = sv; |
748a9306 LW |
3217 | } |
3218 | } | |
3219 | ||
5f05dabc | 3220 | /* Now that vars are all in place, clone nested closures. */ |
3221 | ||
9607fc9c | 3222 | for (ix = fpad; ix > 0; ix--) { |
3223 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; | |
aa689395 | 3224 | if (namesv |
3225 | && namesv != &sv_undef | |
3226 | && !(SvFLAGS(namesv) & SVf_FAKE) | |
3227 | && *SvPVX(namesv) == '&' | |
5f05dabc | 3228 | && CvCLONE(ppad[ix])) |
3229 | { | |
3230 | CV *kid = cv_clone2((CV*)ppad[ix], cv); | |
3231 | SvREFCNT_dec(ppad[ix]); | |
3232 | CvCLONE_on(kid); | |
3233 | SvPADMY_on(kid); | |
3234 | curpad[ix] = (SV*)kid; | |
748a9306 LW |
3235 | } |
3236 | } | |
3237 | ||
5f05dabc | 3238 | #ifdef DEBUG_CLOSURES |
ab50184a CS |
3239 | PerlIO_printf(Perl_debug_log, "Cloned inside:\n"); |
3240 | cv_dump(outside); | |
3241 | PerlIO_printf(Perl_debug_log, " from:\n"); | |
5f05dabc | 3242 | cv_dump(proto); |
ab50184a | 3243 | PerlIO_printf(Perl_debug_log, " to:\n"); |
5f05dabc | 3244 | cv_dump(cv); |
3245 | #endif | |
3246 | ||
748a9306 LW |
3247 | LEAVE; |
3248 | return cv; | |
3249 | } | |
3250 | ||
3251 | CV * | |
8ac85365 | 3252 | cv_clone(CV *proto) |
5f05dabc | 3253 | { |
3254 | return cv_clone2(proto, CvOUTSIDE(proto)); | |
3255 | } | |
3256 | ||
3fe9a6f1 | 3257 | void |
8ac85365 | 3258 | cv_ckproto(CV *cv, GV *gv, char *p) |
3fe9a6f1 | 3259 | { |
3260 | if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) { | |
46fc3d4c | 3261 | SV* msg = sv_newmortal(); |
3fe9a6f1 | 3262 | SV* name = Nullsv; |
3263 | ||
3264 | if (gv) | |
46fc3d4c | 3265 | gv_efullname3(name = sv_newmortal(), gv, Nullch); |
3266 | sv_setpv(msg, "Prototype mismatch:"); | |
3267 | if (name) | |
fc36a67e | 3268 | sv_catpvf(msg, " sub %_", name); |
3fe9a6f1 | 3269 | if (SvPOK(cv)) |
46fc3d4c | 3270 | sv_catpvf(msg, " (%s)", SvPVX(cv)); |
3271 | sv_catpv(msg, " vs "); | |
3272 | if (p) | |
3273 | sv_catpvf(msg, "(%s)", p); | |
3274 | else | |
3275 | sv_catpv(msg, "none"); | |
fc36a67e | 3276 | warn("%_", msg); |
3fe9a6f1 | 3277 | } |
3278 | } | |
3279 | ||
760ac839 | 3280 | SV * |
8ac85365 | 3281 | cv_const_sv(CV *cv) |
760ac839 LW |
3282 | { |
3283 | OP *o; | |
54310121 | 3284 | SV *sv; |
aeea060c | 3285 | |
54310121 | 3286 | if (!cv || !SvPOK(cv) || SvCUR(cv)) |
3287 | return Nullsv; | |
760ac839 | 3288 | |
54310121 | 3289 | sv = Nullsv; |
3290 | for (o = CvSTART(cv); o; o = o->op_next) { | |
3291 | OPCODE type = o->op_type; | |
3292 | ||
3293 | if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK) | |
3294 | continue; | |
3295 | if (type == OP_LEAVESUB || type == OP_RETURN) | |
3296 | break; | |
3297 | if (sv) | |
3298 | return Nullsv; | |
3299 | if (type == OP_CONST) | |
5dc0d613 | 3300 | sv = cSVOPo->op_sv; |
54310121 | 3301 | else if (type == OP_PADSV) { |
e858de61 MB |
3302 | AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]); |
3303 | sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv; | |
5aabfad6 | 3304 | if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1)) |
54310121 | 3305 | return Nullsv; |
760ac839 | 3306 | } |
54310121 | 3307 | else |
3308 | return Nullsv; | |
760ac839 | 3309 | } |
5aabfad6 | 3310 | if (sv) |
3311 | SvREADONLY_on(sv); | |
760ac839 LW |
3312 | return sv; |
3313 | } | |
3314 | ||
748a9306 | 3315 | CV * |
8ac85365 | 3316 | newSUB(I32 floor, OP *o, OP *proto, OP *block) |
79072805 | 3317 | { |
11343788 | 3318 | dTHR; |
5dc0d613 | 3319 | char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch; |
44a8e56a | 3320 | GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV); |
3fe9a6f1 | 3321 | char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch; |
79072805 | 3322 | register CV *cv; |
a0d0e21e | 3323 | I32 ix; |
79072805 | 3324 | |
11343788 | 3325 | if (o) |
5dc0d613 | 3326 | SAVEFREEOP(o); |
3fe9a6f1 | 3327 | if (proto) |
3328 | SAVEFREEOP(proto); | |
3329 | ||
68dc0745 | 3330 | if (!name || GvCVGEN(gv)) |
3331 | cv = Nullcv; | |
3332 | else if (cv = GvCV(gv)) { | |
3fe9a6f1 | 3333 | cv_ckproto(cv, gv, ps); |
68dc0745 | 3334 | /* already defined (or promised)? */ |
3335 | if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { | |
3336 | SV* const_sv; | |
aa689395 | 3337 | if (!block) { |
3338 | /* just a "sub foo;" when &foo is already defined */ | |
3339 | SAVEFREESV(compcv); | |
3340 | goto done; | |
3341 | } | |
7bac28a0 | 3342 | /* ahem, death to those who redefine active sort subs */ |
3343 | if (curstack == sortstack && sortcop == CvSTART(cv)) | |
3344 | croak("Can't redefine active sort subroutine %s", name); | |
68dc0745 | 3345 | const_sv = cv_const_sv(cv); |
44a8e56a | 3346 | if (const_sv || dowarn) { |
79072805 | 3347 | line_t oldline = curcop->cop_line; |
79072805 | 3348 | curcop->cop_line = copline; |
760ac839 | 3349 | warn(const_sv ? "Constant subroutine %s redefined" |
68dc0745 | 3350 | : "Subroutine %s redefined", name); |
79072805 LW |
3351 | curcop->cop_line = oldline; |
3352 | } | |
8990e307 | 3353 | SvREFCNT_dec(cv); |
68dc0745 | 3354 | cv = Nullcv; |
79072805 LW |
3355 | } |
3356 | } | |
a0d0e21e | 3357 | if (cv) { /* must reuse cv if autoloaded */ |
4633a7c4 | 3358 | cv_undef(cv); |
44a8e56a | 3359 | CvFLAGS(cv) = CvFLAGS(compcv); |
748a9306 | 3360 | CvOUTSIDE(cv) = CvOUTSIDE(compcv); |
e9a444f0 | 3361 | CvOUTSIDE(compcv) = 0; |
748a9306 | 3362 | CvPADLIST(cv) = CvPADLIST(compcv); |
4aa0a1f7 | 3363 | CvPADLIST(compcv) = 0; |
4633a7c4 LW |
3364 | if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */ |
3365 | CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv); | |
748a9306 | 3366 | SvREFCNT_dec(compcv); |
a0d0e21e LW |
3367 | } |
3368 | else { | |
748a9306 | 3369 | cv = compcv; |
44a8e56a | 3370 | if (name) { |
3371 | GvCV(gv) = cv; | |
3372 | GvCVGEN(gv) = 0; | |
3373 | sub_generation++; | |
3374 | } | |
a0d0e21e | 3375 | } |
44a8e56a | 3376 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
79072805 | 3377 | CvFILEGV(cv) = curcop->cop_filegv; |
8990e307 | 3378 | CvSTASH(cv) = curstash; |
11343788 MB |
3379 | #ifdef USE_THREADS |
3380 | CvOWNER(cv) = 0; | |
12ca11f6 | 3381 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3382 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 | 3383 | #endif /* USE_THREADS */ |
8990e307 | 3384 | |
3fe9a6f1 | 3385 | if (ps) |
3386 | sv_setpv((SV*)cv, ps); | |
4633a7c4 | 3387 | |
c07a80fd | 3388 | if (error_count) { |
3389 | op_free(block); | |
3390 | block = Nullop; | |
68dc0745 | 3391 | if (name) { |
3392 | char *s = strrchr(name, ':'); | |
3393 | s = s ? s+1 : name; | |
6d4c2119 CS |
3394 | if (strEQ(s, "BEGIN")) { |
3395 | char *not_safe = | |
3396 | "BEGIN not safe after errors--compilation aborted"; | |
3397 | if (in_eval & 4) | |
3398 | croak(not_safe); | |
3399 | else { | |
3400 | /* force display of errors found but not reported */ | |
38a03e6e MB |
3401 | sv_catpv(ERRSV, not_safe); |
3402 | croak("%s", SvPVx(ERRSV, na)); | |
6d4c2119 CS |
3403 | } |
3404 | } | |
68dc0745 | 3405 | } |
c07a80fd | 3406 | } |
a0d0e21e | 3407 | if (!block) { |
a0d0e21e LW |
3408 | copline = NOLINE; |
3409 | LEAVE_SCOPE(floor); | |
3410 | return cv; | |
3411 | } | |
3412 | ||
93965878 NIS |
3413 | if (AvFILLp(comppad_name) < AvFILLp(comppad)) |
3414 | av_store(comppad_name, AvFILLp(comppad), Nullsv); | |
a0d0e21e | 3415 | |
54310121 | 3416 | if (CvCLONE(cv)) { |
3417 | SV **namep = AvARRAY(comppad_name); | |
93965878 | 3418 | for (ix = AvFILLp(comppad); ix > 0; ix--) { |
54310121 | 3419 | SV *namesv; |
3420 | ||
3421 | if (SvIMMORTAL(curpad[ix])) | |
3422 | continue; | |
3423 | /* | |
3424 | * The only things that a clonable function needs in its | |
3425 | * pad are references to outer lexicals and anonymous subs. | |
3426 | * The rest are created anew during cloning. | |
3427 | */ | |
3428 | if (!((namesv = namep[ix]) != Nullsv && | |
3429 | namesv != &sv_undef && | |
3430 | (SvFAKE(namesv) || | |
3431 | *SvPVX(namesv) == '&'))) | |
3432 | { | |
3433 | SvREFCNT_dec(curpad[ix]); | |
3434 | curpad[ix] = Nullsv; | |
3435 | } | |
3436 | } | |
a0d0e21e | 3437 | } |
54310121 | 3438 | else { |
3439 | AV *av = newAV(); /* Will be @_ */ | |
3440 | av_extend(av, 0); | |
3441 | av_store(comppad, 0, (SV*)av); | |
3442 | AvFLAGS(av) = AVf_REIFY; | |
79072805 | 3443 | |
93965878 | 3444 | for (ix = AvFILLp(comppad); ix > 0; ix--) { |
54310121 | 3445 | if (SvIMMORTAL(curpad[ix])) |
3446 | continue; | |
3447 | if (!SvPADMY(curpad[ix])) | |
3448 | SvPADTMP_on(curpad[ix]); | |
3449 | } | |
3450 | } | |
79072805 | 3451 | |
a0d0e21e | 3452 | CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block)); |
79072805 LW |
3453 | CvSTART(cv) = LINKLIST(CvROOT(cv)); |
3454 | CvROOT(cv)->op_next = 0; | |
3455 | peep(CvSTART(cv)); | |
93a17b20 | 3456 | |
44a8e56a | 3457 | if (name) { |
3458 | char *s; | |
3459 | ||
84902520 | 3460 | if (PERLDB_SUBLINE && curstash != debstash) { |
46fc3d4c | 3461 | SV *sv = NEWSV(0,0); |
44a8e56a | 3462 | SV *tmpstr = sv_newmortal(); |
549bb64a | 3463 | GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV); |
44a8e56a | 3464 | CV *cv; |
3465 | HV *hv; | |
3466 | ||
51790459 MB |
3467 | sv_setpvf(sv, "%_:%ld-%ld", |
3468 | GvSV(curcop->cop_filegv), | |
3469 | (long)subline, (long)curcop->cop_line); | |
44a8e56a | 3470 | gv_efullname3(tmpstr, gv, Nullch); |
3471 | hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0); | |
44a8e56a | 3472 | hv = GvHVn(db_postponed); |
9607fc9c | 3473 | if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr)) |
3474 | && (cv = GvCV(db_postponed))) { | |
44a8e56a | 3475 | dSP; |
3476 | PUSHMARK(sp); | |
3477 | XPUSHs(tmpstr); | |
3478 | PUTBACK; | |
3479 | perl_call_sv((SV*)cv, G_DISCARD); | |
3480 | } | |
3481 | } | |
79072805 | 3482 | |
44a8e56a | 3483 | if ((s = strrchr(name,':'))) |
28757baa | 3484 | s++; |
3485 | else | |
3486 | s = name; | |
68dc0745 | 3487 | if (strEQ(s, "BEGIN")) { |
2ae324a7 | 3488 | I32 oldscope = scopestack_ix; |
28757baa | 3489 | ENTER; |
3490 | SAVESPTR(compiling.cop_filegv); | |
3491 | SAVEI16(compiling.cop_line); | |
3492 | SAVEI32(perldb); | |
3493 | save_svref(&rs); | |
3494 | sv_setsv(rs, nrs); | |
3495 | ||
3496 | if (!beginav) | |
3497 | beginav = newAV(); | |
3498 | DEBUG_x( dump_sub(gv) ); | |
3499 | av_push(beginav, (SV *)cv); | |
3500 | GvCV(gv) = 0; | |
68dc0745 | 3501 | call_list(oldscope, beginav); |
a6006777 | 3502 | |
28757baa | 3503 | curcop = &compiling; |
3504 | LEAVE; | |
3505 | } | |
3506 | else if (strEQ(s, "END") && !error_count) { | |
3507 | if (!endav) | |
3508 | endav = newAV(); | |
3509 | av_unshift(endav, 1); | |
3510 | av_store(endav, 0, (SV *)cv); | |
3511 | GvCV(gv) = 0; | |
3512 | } | |
7d07dbc2 MB |
3513 | else if (strEQ(s, "INIT") && !error_count) { |
3514 | if (!initav) | |
3515 | initav = newAV(); | |
3516 | av_push(initav, SvREFCNT_inc(cv)); | |
ae77835f | 3517 | } |
79072805 | 3518 | } |
a6006777 | 3519 | |
aa689395 | 3520 | done: |
79072805 | 3521 | copline = NOLINE; |
8990e307 | 3522 | LEAVE_SCOPE(floor); |
a0d0e21e | 3523 | return cv; |
79072805 LW |
3524 | } |
3525 | ||
57d3b86d | 3526 | CV * |
aeea060c | 3527 | newXS(char *name, void (*subaddr) (CV *), char *filename) |
a0d0e21e | 3528 | { |
11343788 | 3529 | dTHR; |
44a8e56a | 3530 | GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV); |
79072805 | 3531 | register CV *cv; |
44a8e56a | 3532 | |
3533 | if (cv = (name ? GvCV(gv) : Nullcv)) { | |
3534 | if (GvCVGEN(gv)) { | |
3535 | /* just a cached method */ | |
3536 | SvREFCNT_dec(cv); | |
3537 | cv = 0; | |
3538 | } | |
3539 | else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { | |
3540 | /* already defined (or promised) */ | |
a0d0e21e LW |
3541 | if (dowarn) { |
3542 | line_t oldline = curcop->cop_line; | |
a0d0e21e LW |
3543 | curcop->cop_line = copline; |
3544 | warn("Subroutine %s redefined",name); | |
3545 | curcop->cop_line = oldline; | |
3546 | } | |
3547 | SvREFCNT_dec(cv); | |
3548 | cv = 0; | |
79072805 | 3549 | } |
79072805 | 3550 | } |
44a8e56a | 3551 | |
3552 | if (cv) /* must reuse cv if autoloaded */ | |
3553 | cv_undef(cv); | |
a0d0e21e LW |
3554 | else { |
3555 | cv = (CV*)NEWSV(1105,0); | |
3556 | sv_upgrade((SV *)cv, SVt_PVCV); | |
44a8e56a | 3557 | if (name) { |
3558 | GvCV(gv) = cv; | |
3559 | GvCVGEN(gv) = 0; | |
3560 | sub_generation++; | |
3561 | } | |
a0d0e21e | 3562 | } |
5196be3e | 3563 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
11343788 | 3564 | #ifdef USE_THREADS |
12ca11f6 | 3565 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3566 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 MB |
3567 | CvOWNER(cv) = 0; |
3568 | #endif /* USE_THREADS */ | |
79072805 | 3569 | CvFILEGV(cv) = gv_fetchfile(filename); |
a0d0e21e | 3570 | CvXSUB(cv) = subaddr; |
44a8e56a | 3571 | |
28757baa | 3572 | if (name) { |
3573 | char *s = strrchr(name,':'); | |
3574 | if (s) | |
3575 | s++; | |
3576 | else | |
3577 | s = name; | |
3578 | if (strEQ(s, "BEGIN")) { | |
3579 | if (!beginav) | |
3580 | beginav = newAV(); | |
44a8e56a | 3581 | av_push(beginav, (SV *)cv); |
3582 | GvCV(gv) = 0; | |
28757baa | 3583 | } |
3584 | else if (strEQ(s, "END")) { | |
3585 | if (!endav) | |
3586 | endav = newAV(); | |
3587 | av_unshift(endav, 1); | |
44a8e56a | 3588 | av_store(endav, 0, (SV *)cv); |
3589 | GvCV(gv) = 0; | |
28757baa | 3590 | } |
7d07dbc2 MB |
3591 | else if (strEQ(s, "INIT")) { |
3592 | if (!initav) | |
3593 | initav = newAV(); | |
3594 | av_push(initav, (SV *)cv); | |
ae77835f | 3595 | } |
28757baa | 3596 | } |
8990e307 | 3597 | else |
a5f75d66 | 3598 | CvANON_on(cv); |
44a8e56a | 3599 | |
a0d0e21e | 3600 | return cv; |
79072805 LW |
3601 | } |
3602 | ||
3603 | void | |
8ac85365 | 3604 | newFORM(I32 floor, OP *o, OP *block) |
79072805 | 3605 | { |
11343788 | 3606 | dTHR; |
79072805 LW |
3607 | register CV *cv; |
3608 | char *name; | |
3609 | GV *gv; | |
a0d0e21e | 3610 | I32 ix; |
79072805 | 3611 | |
11343788 MB |
3612 | if (o) |
3613 | name = SvPVx(cSVOPo->op_sv, na); | |
79072805 LW |
3614 | else |
3615 | name = "STDOUT"; | |
85e6fe83 | 3616 | gv = gv_fetchpv(name,TRUE, SVt_PVFM); |
a5f75d66 | 3617 | GvMULTI_on(gv); |
79072805 LW |
3618 | if (cv = GvFORM(gv)) { |
3619 | if (dowarn) { | |
3620 | line_t oldline = curcop->cop_line; | |
3621 | ||
3622 | curcop->cop_line = copline; | |
3623 | warn("Format %s redefined",name); | |
3624 | curcop->cop_line = oldline; | |
3625 | } | |
8990e307 | 3626 | SvREFCNT_dec(cv); |
79072805 | 3627 | } |
748a9306 | 3628 | cv = compcv; |
79072805 | 3629 | GvFORM(gv) = cv; |
44a8e56a | 3630 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
79072805 LW |
3631 | CvFILEGV(cv) = curcop->cop_filegv; |
3632 | ||
93965878 | 3633 | for (ix = AvFILLp(comppad); ix > 0; ix--) { |
44a8e56a | 3634 | if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix])) |
a0d0e21e LW |
3635 | SvPADTMP_on(curpad[ix]); |
3636 | } | |
3637 | ||
79072805 LW |
3638 | CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block)); |
3639 | CvSTART(cv) = LINKLIST(CvROOT(cv)); | |
3640 | CvROOT(cv)->op_next = 0; | |
3641 | peep(CvSTART(cv)); | |
11343788 | 3642 | op_free(o); |
79072805 | 3643 | copline = NOLINE; |
8990e307 | 3644 | LEAVE_SCOPE(floor); |
79072805 LW |
3645 | } |
3646 | ||
3647 | OP * | |
8ac85365 | 3648 | newANONLIST(OP *o) |
79072805 | 3649 | { |
93a17b20 | 3650 | return newUNOP(OP_REFGEN, 0, |
11343788 | 3651 | mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN)); |
79072805 LW |
3652 | } |
3653 | ||
3654 | OP * | |
8ac85365 | 3655 | newANONHASH(OP *o) |
79072805 | 3656 | { |
93a17b20 | 3657 | return newUNOP(OP_REFGEN, 0, |
11343788 | 3658 | mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN)); |
a0d0e21e LW |
3659 | } |
3660 | ||
3661 | OP * | |
8ac85365 | 3662 | newANONSUB(I32 floor, OP *proto, OP *block) |
a0d0e21e LW |
3663 | { |
3664 | return newUNOP(OP_REFGEN, 0, | |
4633a7c4 | 3665 | newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block))); |
79072805 LW |
3666 | } |
3667 | ||
3668 | OP * | |
8ac85365 | 3669 | oopsAV(OP *o) |
79072805 | 3670 | { |
ed6116ce LW |
3671 | switch (o->op_type) { |
3672 | case OP_PADSV: | |
3673 | o->op_type = OP_PADAV; | |
3674 | o->op_ppaddr = ppaddr[OP_PADAV]; | |
3675 | return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV); | |
3676 | ||
3677 | case OP_RV2SV: | |
79072805 LW |
3678 | o->op_type = OP_RV2AV; |
3679 | o->op_ppaddr = ppaddr[OP_RV2AV]; | |
3680 | ref(o, OP_RV2AV); | |
ed6116ce LW |
3681 | break; |
3682 | ||
3683 | default: | |
79072805 | 3684 | warn("oops: oopsAV"); |
ed6116ce LW |
3685 | break; |
3686 | } | |
79072805 LW |
3687 | return o; |
3688 | } | |
3689 | ||
3690 | OP * | |
8ac85365 | 3691 | oopsHV(OP *o) |
79072805 | 3692 | { |
ed6116ce LW |
3693 | switch (o->op_type) { |
3694 | case OP_PADSV: | |
3695 | case OP_PADAV: | |
3696 | o->op_type = OP_PADHV; | |
3697 | o->op_ppaddr = ppaddr[OP_PADHV]; | |
3698 | return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV); | |
3699 | ||
3700 | case OP_RV2SV: | |
3701 | case OP_RV2AV: | |
79072805 LW |
3702 | o->op_type = OP_RV2HV; |
3703 | o->op_ppaddr = ppaddr[OP_RV2HV]; | |
3704 | ref(o, OP_RV2HV); | |
ed6116ce LW |
3705 | break; |
3706 | ||
3707 | default: | |
79072805 | 3708 | warn("oops: oopsHV"); |
ed6116ce LW |
3709 | break; |
3710 | } | |
79072805 LW |
3711 | return o; |
3712 | } | |
3713 | ||
3714 | OP * | |
8ac85365 | 3715 | newAVREF(OP *o) |
79072805 | 3716 | { |
ed6116ce LW |
3717 | if (o->op_type == OP_PADANY) { |
3718 | o->op_type = OP_PADAV; | |
3719 | o->op_ppaddr = ppaddr[OP_PADAV]; | |
93a17b20 | 3720 | return o; |
ed6116ce | 3721 | } |
79072805 LW |
3722 | return newUNOP(OP_RV2AV, 0, scalar(o)); |
3723 | } | |
3724 | ||
3725 | OP * | |
8ac85365 | 3726 | newGVREF(I32 type, OP *o) |
79072805 | 3727 | { |
a0d0e21e LW |
3728 | if (type == OP_MAPSTART) |
3729 | return newUNOP(OP_NULL, 0, o); | |
748a9306 | 3730 | return ref(newUNOP(OP_RV2GV, OPf_REF, o), type); |
79072805 LW |
3731 | } |
3732 | ||
3733 | OP * | |
8ac85365 | 3734 | newHVREF(OP *o) |
79072805 | 3735 | { |
ed6116ce LW |
3736 | if (o->op_type == OP_PADANY) { |
3737 | o->op_type = OP_PADHV; | |
3738 | o->op_ppaddr = ppaddr[OP_PADHV]; | |
93a17b20 | 3739 | return o; |
ed6116ce | 3740 | } |
79072805 LW |
3741 | return newUNOP(OP_RV2HV, 0, scalar(o)); |
3742 | } | |
3743 | ||
3744 | OP * | |
8ac85365 | 3745 | oopsCV(OP *o) |
79072805 | 3746 | { |
463ee0b2 | 3747 | croak("NOT IMPL LINE %d",__LINE__); |
79072805 LW |
3748 | /* STUB */ |
3749 | return o; | |
3750 | } | |
3751 | ||
3752 | OP * | |
8ac85365 | 3753 | newCVREF(I32 flags, OP *o) |
79072805 | 3754 | { |
c07a80fd | 3755 | return newUNOP(OP_RV2CV, flags, scalar(o)); |
79072805 LW |
3756 | } |
3757 | ||
3758 | OP * | |
8ac85365 | 3759 | newSVREF(OP *o) |
79072805 | 3760 | { |
ed6116ce LW |
3761 | if (o->op_type == OP_PADANY) { |
3762 | o->op_type = OP_PADSV; | |
3763 | o->op_ppaddr = ppaddr[OP_PADSV]; | |
93a17b20 | 3764 | return o; |
ed6116ce | 3765 | } |
224a4551 MB |
3766 | else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) { |
3767 | o->op_flags |= OPpDONE_SVREF; | |
a863c7d1 | 3768 | return o; |
224a4551 | 3769 | } |
79072805 LW |
3770 | return newUNOP(OP_RV2SV, 0, scalar(o)); |
3771 | } | |
3772 | ||
3773 | /* Check routines. */ | |
3774 | ||
3775 | OP * | |
8ac85365 | 3776 | ck_anoncode(OP *o) |
5f05dabc | 3777 | { |
178c6305 CS |
3778 | PADOFFSET ix; |
3779 | SV* name; | |
3780 | ||
3781 | name = NEWSV(1106,0); | |
3782 | sv_upgrade(name, SVt_PVNV); | |
3783 | sv_setpvn(name, "&", 1); | |
3784 | SvIVX(name) = -1; | |
3785 | SvNVX(name) = 1; | |
5dc0d613 | 3786 | ix = pad_alloc(o->op_type, SVs_PADMY); |
178c6305 | 3787 | av_store(comppad_name, ix, name); |
5dc0d613 MB |
3788 | av_store(comppad, ix, cSVOPo->op_sv); |
3789 | SvPADMY_on(cSVOPo->op_sv); | |
3790 | cSVOPo->op_sv = Nullsv; | |
3791 | cSVOPo->op_targ = ix; | |
3792 | return o; | |
5f05dabc | 3793 | } |
3794 | ||
3795 | OP * | |
8ac85365 | 3796 | ck_bitop(OP *o) |
55497cff | 3797 | { |
5dc0d613 MB |
3798 | o->op_private = hints; |
3799 | return o; | |
55497cff | 3800 | } |
3801 | ||
3802 | OP * | |
8ac85365 | 3803 | ck_concat(OP *o) |
79072805 | 3804 | { |
11343788 MB |
3805 | if (cUNOPo->op_first->op_type == OP_CONCAT) |
3806 | o->op_flags |= OPf_STACKED; | |
3807 | return o; | |
79072805 LW |
3808 | } |
3809 | ||
3810 | OP * | |
8ac85365 | 3811 | ck_spair(OP *o) |
79072805 | 3812 | { |
11343788 | 3813 | if (o->op_flags & OPf_KIDS) { |
79072805 | 3814 | OP* newop; |
a0d0e21e | 3815 | OP* kid; |
5dc0d613 MB |
3816 | OPCODE type = o->op_type; |
3817 | o = modkids(ck_fun(o), type); | |
11343788 | 3818 | kid = cUNOPo->op_first; |
a0d0e21e LW |
3819 | newop = kUNOP->op_first->op_sibling; |
3820 | if (newop && | |
3821 | (newop->op_sibling || | |
3822 | !(opargs[newop->op_type] & OA_RETSCALAR) || | |
3823 | newop->op_type == OP_PADAV || newop->op_type == OP_PADHV || | |
3824 | newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) { | |
aeea060c | 3825 | |
11343788 | 3826 | return o; |
a0d0e21e LW |
3827 | } |
3828 | op_free(kUNOP->op_first); | |
3829 | kUNOP->op_first = newop; | |
3830 | } | |
11343788 MB |
3831 | o->op_ppaddr = ppaddr[++o->op_type]; |
3832 | return ck_fun(o); | |
a0d0e21e LW |
3833 | } |
3834 | ||
3835 | OP * | |
8ac85365 | 3836 | ck_delete(OP *o) |
a0d0e21e | 3837 | { |
11343788 | 3838 | o = ck_fun(o); |
5dc0d613 | 3839 | o->op_private = 0; |
11343788 MB |
3840 | if (o->op_flags & OPf_KIDS) { |
3841 | OP *kid = cUNOPo->op_first; | |
5f05dabc | 3842 | if (kid->op_type == OP_HSLICE) |
5dc0d613 | 3843 | o->op_private |= OPpSLICE; |
5f05dabc | 3844 | else if (kid->op_type != OP_HELEM) |
3845 | croak("%s argument is not a HASH element or slice", | |
5dc0d613 | 3846 | op_desc[o->op_type]); |
a0d0e21e | 3847 | null(kid); |
79072805 | 3848 | } |
11343788 | 3849 | return o; |
79072805 LW |
3850 | } |
3851 | ||
3852 | OP * | |
8ac85365 | 3853 | ck_eof(OP *o) |
79072805 | 3854 | { |
11343788 | 3855 | I32 type = o->op_type; |
79072805 | 3856 | |
11343788 MB |
3857 | if (o->op_flags & OPf_KIDS) { |
3858 | if (cLISTOPo->op_first->op_type == OP_STUB) { | |
3859 | op_free(o); | |
3860 | o = newUNOP(type, OPf_SPECIAL, | |
d58bf5aa | 3861 | newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV))); |
8990e307 | 3862 | } |
11343788 | 3863 | return ck_fun(o); |
79072805 | 3864 | } |
11343788 | 3865 | return o; |
79072805 LW |
3866 | } |
3867 | ||
3868 | OP * | |
8ac85365 | 3869 | ck_eval(OP *o) |
79072805 | 3870 | { |
85e6fe83 | 3871 | hints |= HINT_BLOCK_SCOPE; |
11343788 MB |
3872 | if (o->op_flags & OPf_KIDS) { |
3873 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
79072805 | 3874 | |
93a17b20 | 3875 | if (!kid) { |
11343788 MB |
3876 | o->op_flags &= ~OPf_KIDS; |
3877 | null(o); | |
79072805 LW |
3878 | } |
3879 | else if (kid->op_type == OP_LINESEQ) { | |
3880 | LOGOP *enter; | |
3881 | ||
11343788 MB |
3882 | kid->op_next = o->op_next; |
3883 | cUNOPo->op_first = 0; | |
3884 | op_free(o); | |
79072805 LW |
3885 | |
3886 | Newz(1101, enter, 1, LOGOP); | |
3887 | enter->op_type = OP_ENTERTRY; | |
3888 | enter->op_ppaddr = ppaddr[OP_ENTERTRY]; | |
3889 | enter->op_private = 0; | |
3890 | ||
3891 | /* establish postfix order */ | |
3892 | enter->op_next = (OP*)enter; | |
3893 | ||
11343788 MB |
3894 | o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid); |
3895 | o->op_type = OP_LEAVETRY; | |
3896 | o->op_ppaddr = ppaddr[OP_LEAVETRY]; | |
3897 | enter->op_other = o; | |
3898 | return o; | |
79072805 | 3899 | } |
c7cc6f1c | 3900 | else |
473986ff | 3901 | scalar((OP*)kid); |
79072805 LW |
3902 | } |
3903 | else { | |
11343788 | 3904 | op_free(o); |
54b9620d | 3905 | o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP()); |
79072805 | 3906 | } |
11343788 MB |
3907 | o->op_targ = (PADOFFSET)hints; |
3908 | return o; | |
79072805 LW |
3909 | } |
3910 | ||
3911 | OP * | |
8ac85365 | 3912 | ck_exec(OP *o) |
79072805 LW |
3913 | { |
3914 | OP *kid; | |
11343788 MB |
3915 | if (o->op_flags & OPf_STACKED) { |
3916 | o = ck_fun(o); | |
3917 | kid = cUNOPo->op_first->op_sibling; | |
8990e307 LW |
3918 | if (kid->op_type == OP_RV2GV) |
3919 | null(kid); | |
79072805 | 3920 | } |
463ee0b2 | 3921 | else |
11343788 MB |
3922 | o = listkids(o); |
3923 | return o; | |
79072805 LW |
3924 | } |
3925 | ||
3926 | OP * | |
8ac85365 | 3927 | ck_exists(OP *o) |
5f05dabc | 3928 | { |
5196be3e MB |
3929 | o = ck_fun(o); |
3930 | if (o->op_flags & OPf_KIDS) { | |
3931 | OP *kid = cUNOPo->op_first; | |
5f05dabc | 3932 | if (kid->op_type != OP_HELEM) |
5196be3e | 3933 | croak("%s argument is not a HASH element", op_desc[o->op_type]); |
5f05dabc | 3934 | null(kid); |
3935 | } | |
5196be3e | 3936 | return o; |
5f05dabc | 3937 | } |
3938 | ||
3939 | OP * | |
8ac85365 | 3940 | ck_gvconst(register OP *o) |
79072805 LW |
3941 | { |
3942 | o = fold_constants(o); | |
3943 | if (o->op_type == OP_CONST) | |
3944 | o->op_type = OP_GV; | |
3945 | return o; | |
3946 | } | |
3947 | ||
3948 | OP * | |
8ac85365 | 3949 | ck_rvconst(register OP *o) |
79072805 | 3950 | { |
11343788 MB |
3951 | dTHR; |
3952 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
85e6fe83 | 3953 | |
11343788 | 3954 | o->op_private |= (hints & HINT_STRICT_REFS); |
79072805 | 3955 | if (kid->op_type == OP_CONST) { |
44a8e56a | 3956 | char *name; |
3957 | int iscv; | |
3958 | GV *gv; | |
3959 | ||
3960 | name = SvPV(kid->op_sv, na); | |
3961 | if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) { | |
3962 | char *badthing = Nullch; | |
5dc0d613 | 3963 | switch (o->op_type) { |
44a8e56a | 3964 | case OP_RV2SV: |
3965 | badthing = "a SCALAR"; | |
3966 | break; | |
3967 | case OP_RV2AV: | |
3968 | badthing = "an ARRAY"; | |
3969 | break; | |
3970 | case OP_RV2HV: | |
3971 | badthing = "a HASH"; | |
3972 | break; | |
3973 | } | |
3974 | if (badthing) | |
3975 | croak( | |
3976 | "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use", | |
3977 | name, badthing); | |
3978 | } | |
79072805 | 3979 | kid->op_type = OP_GV; |
5196be3e | 3980 | iscv = (o->op_type == OP_RV2CV) * 2; |
a0d0e21e | 3981 | for (gv = 0; !gv; iscv++) { |
748a9306 LW |
3982 | /* |
3983 | * This is a little tricky. We only want to add the symbol if we | |
3984 | * didn't add it in the lexer. Otherwise we get duplicate strict | |
3985 | * warnings. But if we didn't add it in the lexer, we must at | |
3986 | * least pretend like we wanted to add it even if it existed before, | |
3987 | * or we get possible typo warnings. OPpCONST_ENTERED says | |
3988 | * whether the lexer already added THIS instance of this symbol. | |
3989 | */ | |
44a8e56a | 3990 | gv = gv_fetchpv(name, |
748a9306 | 3991 | iscv | !(kid->op_private & OPpCONST_ENTERED), |
a0d0e21e LW |
3992 | iscv |
3993 | ? SVt_PVCV | |
11343788 | 3994 | : o->op_type == OP_RV2SV |
a0d0e21e | 3995 | ? SVt_PV |
11343788 | 3996 | : o->op_type == OP_RV2AV |
a0d0e21e | 3997 | ? SVt_PVAV |
11343788 | 3998 | : o->op_type == OP_RV2HV |
a0d0e21e LW |
3999 | ? SVt_PVHV |
4000 | : SVt_PVGV); | |
4001 | } | |
adbc6bb1 | 4002 | SvREFCNT_dec(kid->op_sv); |
a0d0e21e | 4003 | kid->op_sv = SvREFCNT_inc(gv); |
79072805 | 4004 | } |
11343788 | 4005 | return o; |
79072805 LW |
4006 | } |
4007 | ||
4008 | OP * | |
8ac85365 | 4009 | ck_ftst(OP *o) |
79072805 | 4010 | { |
11343788 MB |
4011 | dTHR; |
4012 | I32 type = o->op_type; | |
79072805 | 4013 | |
11343788 MB |
4014 | if (o->op_flags & OPf_REF) |
4015 | return o; | |
79072805 | 4016 | |
11343788 MB |
4017 | if (o->op_flags & OPf_KIDS) { |
4018 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
79072805 LW |
4019 | |
4020 | if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) { | |
a0d0e21e | 4021 | OP *newop = newGVOP(type, OPf_REF, |
85e6fe83 | 4022 | gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO)); |
11343788 | 4023 | op_free(o); |
79072805 LW |
4024 | return newop; |
4025 | } | |
4026 | } | |
4027 | else { | |
11343788 | 4028 | op_free(o); |
79072805 | 4029 | if (type == OP_FTTTY) |
fb73857a | 4030 | return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE, |
85e6fe83 | 4031 | SVt_PVIO)); |
79072805 | 4032 | else |
54b9620d | 4033 | return newUNOP(type, 0, newDEFSVOP()); |
79072805 | 4034 | } |
11343788 | 4035 | return o; |
79072805 LW |
4036 | } |
4037 | ||
4038 | OP * | |
8ac85365 | 4039 | ck_fun(OP *o) |
79072805 | 4040 | { |
11343788 | 4041 | dTHR; |
79072805 LW |
4042 | register OP *kid; |
4043 | OP **tokid; | |
4044 | OP *sibl; | |
4045 | I32 numargs = 0; | |
11343788 | 4046 | int type = o->op_type; |
a0d0e21e | 4047 | register I32 oa = opargs[type] >> OASHIFT; |
aeea060c | 4048 | |
11343788 | 4049 | if (o->op_flags & OPf_STACKED) { |
79072805 LW |
4050 | if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL)) |
4051 | oa &= ~OA_OPTIONAL; | |
4052 | else | |
11343788 | 4053 | return no_fh_allowed(o); |
79072805 LW |
4054 | } |
4055 | ||
11343788 MB |
4056 | if (o->op_flags & OPf_KIDS) { |
4057 | tokid = &cLISTOPo->op_first; | |
4058 | kid = cLISTOPo->op_first; | |
8990e307 LW |
4059 | if (kid->op_type == OP_PUSHMARK || |
4060 | kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK) | |
4061 | { | |
79072805 LW |
4062 | tokid = &kid->op_sibling; |
4063 | kid = kid->op_sibling; | |
4064 | } | |
a0d0e21e | 4065 | if (!kid && opargs[type] & OA_DEFGV) |
54b9620d | 4066 | *tokid = kid = newDEFSVOP(); |
79072805 LW |
4067 | |
4068 | while (oa && kid) { | |
4069 | numargs++; | |
4070 | sibl = kid->op_sibling; | |
4071 | switch (oa & 7) { | |
4072 | case OA_SCALAR: | |
4073 | scalar(kid); | |
4074 | break; | |
4075 | case OA_LIST: | |
4076 | if (oa < 16) { | |
4077 | kid = 0; | |
4078 | continue; | |
4079 | } | |
4080 | else | |
4081 | list(kid); | |
4082 | break; | |
4083 | case OA_AVREF: | |
4084 | if (kid->op_type == OP_CONST && | |
4085 | (kid->op_private & OPpCONST_BARE)) { | |
463ee0b2 | 4086 | char *name = SvPVx(((SVOP*)kid)->op_sv, na); |
79072805 | 4087 | OP *newop = newAVREF(newGVOP(OP_GV, 0, |
85e6fe83 | 4088 | gv_fetchpv(name, TRUE, SVt_PVAV) )); |
463ee0b2 | 4089 | if (dowarn) |
ff0cee69 | 4090 | warn("Array @%s missing the @ in argument %ld of %s()", |
4091 | name, (long)numargs, op_desc[type]); | |
79072805 LW |
4092 | op_free(kid); |
4093 | kid = newop; | |
4094 | kid->op_sibling = sibl; | |
4095 | *tokid = kid; | |
4096 | } | |
8990e307 | 4097 | else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) |
11343788 | 4098 | bad_type(numargs, "array", op_desc[o->op_type], kid); |
a0d0e21e | 4099 | mod(kid, type); |
79072805 LW |
4100 | break; |
4101 | case OA_HVREF: | |
4102 | if (kid->op_type == OP_CONST && | |
4103 | (kid->op_private & OPpCONST_BARE)) { | |
463ee0b2 | 4104 | char *name = SvPVx(((SVOP*)kid)->op_sv, na); |
79072805 | 4105 | OP *newop = newHVREF(newGVOP(OP_GV, 0, |
85e6fe83 | 4106 | gv_fetchpv(name, TRUE, SVt_PVHV) )); |
463ee0b2 | 4107 | if (dowarn) |
ff0cee69 | 4108 | warn("Hash %%%s missing the %% in argument %ld of %s()", |
4109 | name, (long)numargs, op_desc[type]); | |
79072805 LW |
4110 | op_free(kid); |
4111 | kid = newop; | |
4112 | kid->op_sibling = sibl; | |
4113 | *tokid = kid; | |
4114 | } | |
8990e307 | 4115 | else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV) |
11343788 | 4116 | bad_type(numargs, "hash", op_desc[o->op_type], kid); |
a0d0e21e | 4117 | mod(kid, type); |
79072805 LW |
4118 | break; |
4119 | case OA_CVREF: | |
4120 | { | |
a0d0e21e | 4121 | OP *newop = newUNOP(OP_NULL, 0, kid); |
79072805 LW |
4122 | kid->op_sibling = 0; |
4123 | linklist(kid); | |
4124 | newop->op_next = newop; | |
4125 | kid = newop; | |
4126 | kid->op_sibling = sibl; | |
4127 | *tokid = kid; | |
4128 | } | |
4129 | break; | |
4130 | case OA_FILEREF: | |
4131 | if (kid->op_type != OP_GV) { | |
4132 | if (kid->op_type == OP_CONST && | |
4133 | (kid->op_private & OPpCONST_BARE)) { | |
4134 | OP *newop = newGVOP(OP_GV, 0, | |
85e6fe83 LW |
4135 | gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE, |
4136 | SVt_PVIO) ); | |
79072805 LW |
4137 | op_free(kid); |
4138 | kid = newop; | |
4139 | } | |
4140 | else { | |
4141 | kid->op_sibling = 0; | |
4142 | kid = newUNOP(OP_RV2GV, 0, scalar(kid)); | |
4143 | } | |
4144 | kid->op_sibling = sibl; | |
4145 | *tokid = kid; | |
4146 | } | |
4147 | scalar(kid); | |
4148 | break; | |
4149 | case OA_SCALARREF: | |
a0d0e21e | 4150 | mod(scalar(kid), type); |
79072805 LW |
4151 | break; |
4152 | } | |
4153 | oa >>= 4; | |
4154 | tokid = &kid->op_sibling; | |
4155 | kid = kid->op_sibling; | |
4156 | } | |
11343788 | 4157 | o->op_private |= numargs; |
79072805 | 4158 | if (kid) |
11343788 MB |
4159 | return too_many_arguments(o,op_desc[o->op_type]); |
4160 | listkids(o); | |
79072805 | 4161 | } |
a0d0e21e | 4162 | else if (opargs[type] & OA_DEFGV) { |
11343788 | 4163 | op_free(o); |
54b9620d | 4164 | return newUNOP(type, 0, newDEFSVOP()); |
a0d0e21e LW |
4165 | } |
4166 | ||
79072805 LW |
4167 | if (oa) { |
4168 | while (oa & OA_OPTIONAL) | |
4169 | oa >>= 4; | |
4170 | if (oa && oa != OA_LIST) | |
11343788 | 4171 | return too_few_arguments(o,op_desc[o->op_type]); |
79072805 | 4172 | } |
11343788 | 4173 | return o; |
79072805 LW |
4174 | } |
4175 | ||
4176 | OP * | |
8ac85365 | 4177 | ck_glob(OP *o) |
79072805 | 4178 | { |
fb73857a | 4179 | GV *gv; |
4180 | ||
1f2bfc8a | 4181 | if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling) |
54b9620d | 4182 | append_elem(OP_GLOB, o, newDEFSVOP()); |
fb73857a | 4183 | |
4184 | if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv))) | |
4185 | gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV); | |
b1cb66bf | 4186 | |
4187 | if (gv && GvIMPORTED_CV(gv)) { | |
46fc3d4c | 4188 | static int glob_index; |
4189 | ||
5196be3e | 4190 | append_elem(OP_GLOB, o, |
46fc3d4c | 4191 | newSVOP(OP_CONST, 0, newSViv(glob_index++))); |
1f2bfc8a MB |
4192 | o->op_type = OP_LIST; |
4193 | o->op_ppaddr = ppaddr[OP_LIST]; | |
4194 | cLISTOPo->op_first->op_type = OP_PUSHMARK; | |
4195 | cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK]; | |
4196 | o = newUNOP(OP_ENTERSUB, OPf_STACKED, | |
aeea060c | 4197 | append_elem(OP_LIST, o, |
1f2bfc8a MB |
4198 | scalar(newUNOP(OP_RV2CV, 0, |
4199 | newGVOP(OP_GV, 0, gv))))); | |
d58bf5aa MB |
4200 | o = newUNOP(OP_NULL, 0, ck_subr(o)); |
4201 | o->op_targ = OP_GLOB; /* hint at what it used to be */ | |
4202 | return o; | |
b1cb66bf | 4203 | } |
4204 | gv = newGVgen("main"); | |
a0d0e21e | 4205 | gv_IOadd(gv); |
11343788 MB |
4206 | append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv)); |
4207 | scalarkids(o); | |
4208 | return ck_fun(o); | |
79072805 LW |
4209 | } |
4210 | ||
4211 | OP * | |
8ac85365 | 4212 | ck_grep(OP *o) |
79072805 LW |
4213 | { |
4214 | LOGOP *gwop; | |
4215 | OP *kid; | |
11343788 | 4216 | OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE; |
79072805 | 4217 | |
11343788 | 4218 | o->op_ppaddr = ppaddr[OP_GREPSTART]; |
a0d0e21e | 4219 | Newz(1101, gwop, 1, LOGOP); |
aeea060c | 4220 | |
11343788 | 4221 | if (o->op_flags & OPf_STACKED) { |
a0d0e21e | 4222 | OP* k; |
11343788 MB |
4223 | o = ck_sort(o); |
4224 | kid = cLISTOPo->op_first->op_sibling; | |
4225 | for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) { | |
a0d0e21e LW |
4226 | kid = k; |
4227 | } | |
4228 | kid->op_next = (OP*)gwop; | |
11343788 | 4229 | o->op_flags &= ~OPf_STACKED; |
93a17b20 | 4230 | } |
11343788 | 4231 | kid = cLISTOPo->op_first->op_sibling; |
a0d0e21e LW |
4232 | if (type == OP_MAPWHILE) |
4233 | list(kid); | |
4234 | else | |
4235 | scalar(kid); | |
11343788 | 4236 | o = ck_fun(o); |
79072805 | 4237 | if (error_count) |
11343788 | 4238 | return o; |
aeea060c | 4239 | kid = cLISTOPo->op_first->op_sibling; |
79072805 | 4240 | if (kid->op_type != OP_NULL) |
463ee0b2 | 4241 | croak("panic: ck_grep"); |
79072805 LW |
4242 | kid = kUNOP->op_first; |
4243 | ||
a0d0e21e LW |
4244 | gwop->op_type = type; |
4245 | gwop->op_ppaddr = ppaddr[type]; | |
11343788 | 4246 | gwop->op_first = listkids(o); |
79072805 LW |
4247 | gwop->op_flags |= OPf_KIDS; |
4248 | gwop->op_private = 1; | |
4249 | gwop->op_other = LINKLIST(kid); | |
a0d0e21e | 4250 | gwop->op_targ = pad_alloc(type, SVs_PADTMP); |
79072805 LW |
4251 | kid->op_next = (OP*)gwop; |
4252 | ||
11343788 | 4253 | kid = cLISTOPo->op_first->op_sibling; |
a0d0e21e | 4254 | if (!kid || !kid->op_sibling) |
11343788 | 4255 | return too_few_arguments(o,op_desc[o->op_type]); |
a0d0e21e LW |
4256 | for (kid = kid->op_sibling; kid; kid = kid->op_sibling) |
4257 | mod(kid, OP_GREPSTART); | |
4258 | ||
79072805 LW |
4259 | return (OP*)gwop; |
4260 | } | |
4261 | ||
4262 | OP * | |
8ac85365 | 4263 | ck_index(OP *o) |
79072805 | 4264 | { |
11343788 MB |
4265 | if (o->op_flags & OPf_KIDS) { |
4266 | OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ | |
79072805 | 4267 | if (kid && kid->op_type == OP_CONST) |
bbce6d69 | 4268 | fbm_compile(((SVOP*)kid)->op_sv); |
79072805 | 4269 | } |
11343788 | 4270 | return ck_fun(o); |
79072805 LW |
4271 | } |
4272 | ||
4273 | OP * | |
8ac85365 | 4274 | ck_lengthconst(OP *o) |
79072805 LW |
4275 | { |
4276 | /* XXX length optimization goes here */ | |
11343788 | 4277 | return ck_fun(o); |
79072805 LW |
4278 | } |
4279 | ||
4280 | OP * | |
8ac85365 | 4281 | ck_lfun(OP *o) |
79072805 | 4282 | { |
5dc0d613 MB |
4283 | OPCODE type = o->op_type; |
4284 | return modkids(ck_fun(o), type); | |
79072805 LW |
4285 | } |
4286 | ||
4287 | OP * | |
8ac85365 | 4288 | ck_rfun(OP *o) |
8990e307 | 4289 | { |
5dc0d613 MB |
4290 | OPCODE type = o->op_type; |
4291 | return refkids(ck_fun(o), type); | |
8990e307 LW |
4292 | } |
4293 | ||
4294 | OP * | |
8ac85365 | 4295 | ck_listiob(OP *o) |
79072805 LW |
4296 | { |
4297 | register OP *kid; | |
aeea060c | 4298 | |
11343788 | 4299 | kid = cLISTOPo->op_first; |
79072805 | 4300 | if (!kid) { |
11343788 MB |
4301 | o = force_list(o); |
4302 | kid = cLISTOPo->op_first; | |
79072805 LW |
4303 | } |
4304 | if (kid->op_type == OP_PUSHMARK) | |
4305 | kid = kid->op_sibling; | |
11343788 | 4306 | if (kid && o->op_flags & OPf_STACKED) |
79072805 LW |
4307 | kid = kid->op_sibling; |
4308 | else if (kid && !kid->op_sibling) { /* print HANDLE; */ | |
4309 | if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) { | |
11343788 | 4310 | o->op_flags |= OPf_STACKED; /* make it a filehandle */ |
748a9306 | 4311 | kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid)); |
11343788 MB |
4312 | cLISTOPo->op_first->op_sibling = kid; |
4313 | cLISTOPo->op_last = kid; | |
79072805 LW |
4314 | kid = kid->op_sibling; |
4315 | } | |
4316 | } | |
4317 | ||
4318 | if (!kid) | |
54b9620d | 4319 | append_elem(o->op_type, o, newDEFSVOP()); |
79072805 | 4320 | |
5dc0d613 | 4321 | o = listkids(o); |
bbce6d69 | 4322 | |
5dc0d613 | 4323 | o->op_private = 0; |
36477c24 | 4324 | #ifdef USE_LOCALE |
bbce6d69 | 4325 | if (hints & HINT_LOCALE) |
5dc0d613 | 4326 | o->op_private |= OPpLOCALE; |
bbce6d69 | 4327 | #endif |
4328 | ||
5dc0d613 | 4329 | return o; |
bbce6d69 | 4330 | } |
4331 | ||
4332 | OP * | |
8ac85365 | 4333 | ck_fun_locale(OP *o) |
bbce6d69 | 4334 | { |
5dc0d613 | 4335 | o = ck_fun(o); |
bbce6d69 | 4336 | |
5dc0d613 | 4337 | o->op_private = 0; |
36477c24 | 4338 | #ifdef USE_LOCALE |
bbce6d69 | 4339 | if (hints & HINT_LOCALE) |
5dc0d613 | 4340 | o->op_private |= OPpLOCALE; |
bbce6d69 | 4341 | #endif |
4342 | ||
5dc0d613 | 4343 | return o; |
bbce6d69 | 4344 | } |
4345 | ||
4346 | OP * | |
8ac85365 | 4347 | ck_scmp(OP *o) |
bbce6d69 | 4348 | { |
5dc0d613 | 4349 | o->op_private = 0; |
36477c24 | 4350 | #ifdef USE_LOCALE |
bbce6d69 | 4351 | if (hints & HINT_LOCALE) |
5dc0d613 | 4352 | o->op_private |= OPpLOCALE; |
bbce6d69 | 4353 | #endif |
36477c24 | 4354 | |
5dc0d613 | 4355 | return o; |
79072805 LW |
4356 | } |
4357 | ||
4358 | OP * | |
8ac85365 | 4359 | ck_match(OP *o) |
79072805 | 4360 | { |
5dc0d613 | 4361 | o->op_private |= OPpRUNTIME; |
11343788 | 4362 | return o; |
79072805 LW |
4363 | } |
4364 | ||
4365 | OP * | |
8ac85365 | 4366 | ck_null(OP *o) |
79072805 | 4367 | { |
11343788 | 4368 | return o; |
79072805 LW |
4369 | } |
4370 | ||
4371 | OP * | |
8ac85365 | 4372 | ck_repeat(OP *o) |
79072805 | 4373 | { |
11343788 MB |
4374 | if (cBINOPo->op_first->op_flags & OPf_PARENS) { |
4375 | o->op_private |= OPpREPEAT_DOLIST; | |
4376 | cBINOPo->op_first = force_list(cBINOPo->op_first); | |
79072805 LW |
4377 | } |
4378 | else | |
11343788 MB |
4379 | scalar(o); |
4380 | return o; | |
79072805 LW |
4381 | } |
4382 | ||
4383 | OP * | |
8ac85365 | 4384 | ck_require(OP *o) |
8990e307 | 4385 | { |
11343788 MB |
4386 | if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */ |
4387 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
8990e307 LW |
4388 | |
4389 | if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) { | |
8990e307 | 4390 | char *s; |
a0d0e21e LW |
4391 | for (s = SvPVX(kid->op_sv); *s; s++) { |
4392 | if (*s == ':' && s[1] == ':') { | |
4393 | *s = '/'; | |
1aef975c | 4394 | Move(s+2, s+1, strlen(s+2)+1, char); |
a0d0e21e LW |
4395 | --SvCUR(kid->op_sv); |
4396 | } | |
8990e307 | 4397 | } |
a0d0e21e | 4398 | sv_catpvn(kid->op_sv, ".pm", 3); |
8990e307 LW |
4399 | } |
4400 | } | |
11343788 | 4401 | return ck_fun(o); |
8990e307 LW |
4402 | } |
4403 | ||
4404 | OP * | |
8ac85365 | 4405 | ck_retarget(OP *o) |
79072805 | 4406 | { |
463ee0b2 | 4407 | croak("NOT IMPL LINE %d",__LINE__); |
79072805 | 4408 | /* STUB */ |
11343788 | 4409 | return o; |
79072805 LW |
4410 | } |
4411 | ||
4412 | OP * | |
8ac85365 | 4413 | ck_select(OP *o) |
79072805 | 4414 | { |
c07a80fd | 4415 | OP* kid; |
11343788 MB |
4416 | if (o->op_flags & OPf_KIDS) { |
4417 | kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ | |
2304df62 | 4418 | if (kid && kid->op_sibling) { |
11343788 MB |
4419 | o->op_type = OP_SSELECT; |
4420 | o->op_ppaddr = ppaddr[OP_SSELECT]; | |
4421 | o = ck_fun(o); | |
4422 | return fold_constants(o); | |
79072805 LW |
4423 | } |
4424 | } | |
11343788 MB |
4425 | o = ck_fun(o); |
4426 | kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ | |
c07a80fd | 4427 | if (kid && kid->op_type == OP_RV2GV) |
4428 | kid->op_private &= ~HINT_STRICT_REFS; | |
11343788 | 4429 | return o; |
79072805 LW |
4430 | } |
4431 | ||
4432 | OP * | |
8ac85365 | 4433 | ck_shift(OP *o) |
79072805 | 4434 | { |
11343788 | 4435 | I32 type = o->op_type; |
79072805 | 4436 | |
11343788 | 4437 | if (!(o->op_flags & OPf_KIDS)) { |
6d4ff0d2 MB |
4438 | OP *argop; |
4439 | ||
11343788 | 4440 | op_free(o); |
6d4ff0d2 | 4441 | #ifdef USE_THREADS |
977336f5 | 4442 | if (!CvUNIQUE(compcv)) { |
6d4ff0d2 MB |
4443 | argop = newOP(OP_PADAV, OPf_REF); |
4444 | argop->op_targ = 0; /* curpad[0] is @_ */ | |
4445 | } | |
4446 | else { | |
4447 | argop = newUNOP(OP_RV2AV, 0, | |
4448 | scalar(newGVOP(OP_GV, 0, | |
4449 | gv_fetchpv("ARGV", TRUE, SVt_PVAV)))); | |
4450 | } | |
4451 | #else | |
4452 | argop = newUNOP(OP_RV2AV, 0, | |
977336f5 | 4453 | scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ? |
6d4ff0d2 MB |
4454 | defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV)))); |
4455 | #endif /* USE_THREADS */ | |
4456 | return newUNOP(type, 0, scalar(argop)); | |
79072805 | 4457 | } |
11343788 | 4458 | return scalar(modkids(ck_fun(o), type)); |
79072805 LW |
4459 | } |
4460 | ||
4461 | OP * | |
8ac85365 | 4462 | ck_sort(OP *o) |
79072805 | 4463 | { |
5dc0d613 | 4464 | o->op_private = 0; |
36477c24 | 4465 | #ifdef USE_LOCALE |
bbce6d69 | 4466 | if (hints & HINT_LOCALE) |
5dc0d613 | 4467 | o->op_private |= OPpLOCALE; |
bbce6d69 | 4468 | #endif |
4469 | ||
11343788 MB |
4470 | if (o->op_flags & OPf_STACKED) { |
4471 | OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ | |
463ee0b2 LW |
4472 | OP *k; |
4473 | kid = kUNOP->op_first; /* get past rv2gv */ | |
79072805 | 4474 | |
463ee0b2 | 4475 | if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) { |
79072805 | 4476 | linklist(kid); |
463ee0b2 LW |
4477 | if (kid->op_type == OP_SCOPE) { |
4478 | k = kid->op_next; | |
4479 | kid->op_next = 0; | |
79072805 | 4480 | } |
463ee0b2 | 4481 | else if (kid->op_type == OP_LEAVE) { |
11343788 | 4482 | if (o->op_type == OP_SORT) { |
748a9306 LW |
4483 | null(kid); /* wipe out leave */ |
4484 | kid->op_next = kid; | |
463ee0b2 | 4485 | |
748a9306 LW |
4486 | for (k = kLISTOP->op_first->op_next; k; k = k->op_next) { |
4487 | if (k->op_next == kid) | |
4488 | k->op_next = 0; | |
4489 | } | |
463ee0b2 | 4490 | } |
748a9306 LW |
4491 | else |
4492 | kid->op_next = 0; /* just disconnect the leave */ | |
a0d0e21e | 4493 | k = kLISTOP->op_first; |
463ee0b2 | 4494 | } |
a0d0e21e LW |
4495 | peep(k); |
4496 | ||
11343788 | 4497 | kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ |
8990e307 | 4498 | null(kid); /* wipe out rv2gv */ |
11343788 | 4499 | if (o->op_type == OP_SORT) |
a0d0e21e LW |
4500 | kid->op_next = kid; |
4501 | else | |
4502 | kid->op_next = k; | |
11343788 | 4503 | o->op_flags |= OPf_SPECIAL; |
79072805 LW |
4504 | } |
4505 | } | |
bbce6d69 | 4506 | |
11343788 | 4507 | return o; |
79072805 LW |
4508 | } |
4509 | ||
4510 | OP * | |
8ac85365 | 4511 | ck_split(OP *o) |
79072805 LW |
4512 | { |
4513 | register OP *kid; | |
aeea060c | 4514 | |
11343788 MB |
4515 | if (o->op_flags & OPf_STACKED) |
4516 | return no_fh_allowed(o); | |
79072805 | 4517 | |
11343788 | 4518 | kid = cLISTOPo->op_first; |
8990e307 | 4519 | if (kid->op_type != OP_NULL) |
463ee0b2 | 4520 | croak("panic: ck_split"); |
8990e307 | 4521 | kid = kid->op_sibling; |
11343788 MB |
4522 | op_free(cLISTOPo->op_first); |
4523 | cLISTOPo->op_first = kid; | |
85e6fe83 | 4524 | if (!kid) { |
11343788 MB |
4525 | cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1)); |
4526 | cLISTOPo->op_last = kid; /* There was only one element previously */ | |
85e6fe83 | 4527 | } |
79072805 LW |
4528 | |
4529 | if (kid->op_type != OP_MATCH) { | |
4530 | OP *sibl = kid->op_sibling; | |
463ee0b2 | 4531 | kid->op_sibling = 0; |
79072805 | 4532 | kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop); |
11343788 MB |
4533 | if (cLISTOPo->op_first == cLISTOPo->op_last) |
4534 | cLISTOPo->op_last = kid; | |
4535 | cLISTOPo->op_first = kid; | |
79072805 LW |
4536 | kid->op_sibling = sibl; |
4537 | } | |
4538 | ||
4539 | kid->op_type = OP_PUSHRE; | |
4540 | kid->op_ppaddr = ppaddr[OP_PUSHRE]; | |
4541 | scalar(kid); | |
4542 | ||
4543 | if (!kid->op_sibling) | |
54b9620d | 4544 | append_elem(OP_SPLIT, o, newDEFSVOP()); |
79072805 LW |
4545 | |
4546 | kid = kid->op_sibling; | |
4547 | scalar(kid); | |
4548 | ||
4549 | if (!kid->op_sibling) | |
11343788 | 4550 | append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0))); |
79072805 LW |
4551 | |
4552 | kid = kid->op_sibling; | |
4553 | scalar(kid); | |
4554 | ||
4555 | if (kid->op_sibling) | |
11343788 | 4556 | return too_many_arguments(o,op_desc[o->op_type]); |
79072805 | 4557 | |
11343788 | 4558 | return o; |
79072805 LW |
4559 | } |
4560 | ||
4561 | OP * | |
8ac85365 | 4562 | ck_subr(OP *o) |
79072805 | 4563 | { |
11343788 MB |
4564 | dTHR; |
4565 | OP *prev = ((cUNOPo->op_first->op_sibling) | |
4566 | ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first; | |
4567 | OP *o2 = prev->op_sibling; | |
4633a7c4 LW |
4568 | OP *cvop; |
4569 | char *proto = 0; | |
4570 | CV *cv = 0; | |
46fc3d4c | 4571 | GV *namegv = 0; |
4633a7c4 LW |
4572 | int optional = 0; |
4573 | I32 arg = 0; | |
4574 | ||
11343788 | 4575 | for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ; |
4633a7c4 LW |
4576 | if (cvop->op_type == OP_RV2CV) { |
4577 | SVOP* tmpop; | |
11343788 | 4578 | o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER); |
4633a7c4 LW |
4579 | null(cvop); /* disable rv2cv */ |
4580 | tmpop = (SVOP*)((UNOP*)cvop)->op_first; | |
4581 | if (tmpop->op_type == OP_GV) { | |
8ebc5c01 | 4582 | cv = GvCVu(tmpop->op_sv); |
5dc0d613 | 4583 | if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) { |
46fc3d4c | 4584 | namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv); |
4585 | proto = SvPV((SV*)cv, na); | |
4586 | } | |
4633a7c4 LW |
4587 | } |
4588 | } | |
11343788 | 4589 | o->op_private |= (hints & HINT_STRICT_REFS); |
84902520 | 4590 | if (PERLDB_SUB && curstash != debstash) |
11343788 MB |
4591 | o->op_private |= OPpENTERSUB_DB; |
4592 | while (o2 != cvop) { | |
4633a7c4 LW |
4593 | if (proto) { |
4594 | switch (*proto) { | |
4595 | case '\0': | |
5dc0d613 | 4596 | return too_many_arguments(o, gv_ename(namegv)); |
4633a7c4 LW |
4597 | case ';': |
4598 | optional = 1; | |
4599 | proto++; | |
4600 | continue; | |
4601 | case '$': | |
4602 | proto++; | |
4603 | arg++; | |
11343788 | 4604 | scalar(o2); |
4633a7c4 LW |
4605 | break; |
4606 | case '%': | |
4607 | case '@': | |
11343788 | 4608 | list(o2); |
4633a7c4 LW |
4609 | arg++; |
4610 | break; | |
4611 | case '&': | |
4612 | proto++; | |
4613 | arg++; | |
11343788 | 4614 | if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF) |
5dc0d613 | 4615 | bad_type(arg, "block", gv_ename(namegv), o2); |
4633a7c4 LW |
4616 | break; |
4617 | case '*': | |
4618 | proto++; | |
4619 | arg++; | |
11343788 | 4620 | if (o2->op_type == OP_RV2GV) |
4633a7c4 LW |
4621 | goto wrapref; |
4622 | { | |
11343788 | 4623 | OP* kid = o2; |
69dcf70c | 4624 | OP* sib = kid->op_sibling; |
4633a7c4 | 4625 | kid->op_sibling = 0; |
69dcf70c MB |
4626 | o2 = newUNOP(OP_RV2GV, 0, kid); |
4627 | o2->op_sibling = sib; | |
6fa846a0 | 4628 | prev->op_sibling = o2; |
4633a7c4 LW |
4629 | } |
4630 | goto wrapref; | |
4631 | case '\\': | |
4632 | proto++; | |
4633 | arg++; | |
4634 | switch (*proto++) { | |
4635 | case '*': | |
11343788 | 4636 | if (o2->op_type != OP_RV2GV) |
5dc0d613 | 4637 | bad_type(arg, "symbol", gv_ename(namegv), o2); |
4633a7c4 LW |
4638 | goto wrapref; |
4639 | case '&': | |
11343788 | 4640 | if (o2->op_type != OP_RV2CV) |
5dc0d613 | 4641 | bad_type(arg, "sub", gv_ename(namegv), o2); |
4633a7c4 LW |
4642 | goto wrapref; |
4643 | case '$': | |
11343788 | 4644 | if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV) |
5dc0d613 | 4645 | bad_type(arg, "scalar", gv_ename(namegv), o2); |
4633a7c4 LW |
4646 | goto wrapref; |
4647 | case '@': | |
11343788 | 4648 | if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV) |
5dc0d613 | 4649 | bad_type(arg, "array", gv_ename(namegv), o2); |
4633a7c4 LW |
4650 | goto wrapref; |
4651 | case '%': | |
11343788 | 4652 | if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV) |
5dc0d613 | 4653 | bad_type(arg, "hash", gv_ename(namegv), o2); |
4633a7c4 LW |
4654 | wrapref: |
4655 | { | |
11343788 | 4656 | OP* kid = o2; |
6fa846a0 | 4657 | OP* sib = kid->op_sibling; |
4633a7c4 | 4658 | kid->op_sibling = 0; |
6fa846a0 GS |
4659 | o2 = newUNOP(OP_REFGEN, 0, kid); |
4660 | o2->op_sibling = sib; | |
e858de61 | 4661 | prev->op_sibling = o2; |
4633a7c4 LW |
4662 | } |
4663 | break; | |
4664 | default: goto oops; | |
4665 | } | |
4666 | break; | |
b1cb66bf | 4667 | case ' ': |
4668 | proto++; | |
4669 | continue; | |
4633a7c4 LW |
4670 | default: |
4671 | oops: | |
4672 | croak("Malformed prototype for %s: %s", | |
46fc3d4c | 4673 | gv_ename(namegv), SvPV((SV*)cv, na)); |
4633a7c4 LW |
4674 | } |
4675 | } | |
4676 | else | |
11343788 MB |
4677 | list(o2); |
4678 | mod(o2, OP_ENTERSUB); | |
4679 | prev = o2; | |
4680 | o2 = o2->op_sibling; | |
4633a7c4 | 4681 | } |
fb73857a | 4682 | if (proto && !optional && |
4683 | (*proto && *proto != '@' && *proto != '%' && *proto != ';')) | |
5dc0d613 | 4684 | return too_few_arguments(o, gv_ename(namegv)); |
11343788 | 4685 | return o; |
79072805 LW |
4686 | } |
4687 | ||
4688 | OP * | |
8ac85365 | 4689 | ck_svconst(OP *o) |
8990e307 | 4690 | { |
11343788 MB |
4691 | SvREADONLY_on(cSVOPo->op_sv); |
4692 | return o; | |
8990e307 LW |
4693 | } |
4694 | ||
4695 | OP * | |
8ac85365 | 4696 | ck_trunc(OP *o) |
79072805 | 4697 | { |
11343788 MB |
4698 | if (o->op_flags & OPf_KIDS) { |
4699 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
79072805 | 4700 | |
a0d0e21e LW |
4701 | if (kid->op_type == OP_NULL) |
4702 | kid = (SVOP*)kid->op_sibling; | |
4703 | if (kid && | |
4704 | kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) | |
11343788 | 4705 | o->op_flags |= OPf_SPECIAL; |
79072805 | 4706 | } |
11343788 | 4707 | return ck_fun(o); |
79072805 LW |
4708 | } |
4709 | ||
463ee0b2 LW |
4710 | /* A peephole optimizer. We visit the ops in the order they're to execute. */ |
4711 | ||
79072805 | 4712 | void |
8ac85365 | 4713 | peep(register OP *o) |
79072805 | 4714 | { |
11343788 | 4715 | dTHR; |
79072805 | 4716 | register OP* oldop = 0; |
a0d0e21e | 4717 | if (!o || o->op_seq) |
79072805 | 4718 | return; |
a0d0e21e | 4719 | ENTER; |
462e5cf6 | 4720 | SAVEOP(); |
a0d0e21e LW |
4721 | SAVESPTR(curcop); |
4722 | for (; o; o = o->op_next) { | |
4723 | if (o->op_seq) | |
4724 | break; | |
c07a80fd | 4725 | if (!op_seqmax) |
4726 | op_seqmax++; | |
a0d0e21e LW |
4727 | op = o; |
4728 | switch (o->op_type) { | |
4729 | case OP_NEXTSTATE: | |
4730 | case OP_DBSTATE: | |
4731 | curcop = ((COP*)o); /* for warnings */ | |
c07a80fd | 4732 | o->op_seq = op_seqmax++; |
a0d0e21e LW |
4733 | break; |
4734 | ||
4735 | case OP_CONCAT: | |
4736 | case OP_CONST: | |
4737 | case OP_JOIN: | |
4738 | case OP_UC: | |
4739 | case OP_UCFIRST: | |
4740 | case OP_LC: | |
4741 | case OP_LCFIRST: | |
4742 | case OP_QUOTEMETA: | |
3c4f770c | 4743 | if (o->op_next && o->op_next->op_type == OP_STRINGIFY) |
a0d0e21e | 4744 | null(o->op_next); |
c07a80fd | 4745 | o->op_seq = op_seqmax++; |
a0d0e21e | 4746 | break; |
8990e307 | 4747 | case OP_STUB: |
54310121 | 4748 | if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) { |
c07a80fd | 4749 | o->op_seq = op_seqmax++; |
54310121 | 4750 | break; /* Scalar stub must produce undef. List stub is noop */ |
8990e307 | 4751 | } |
748a9306 | 4752 | goto nothin; |
79072805 | 4753 | case OP_NULL: |
748a9306 LW |
4754 | if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE) |
4755 | curcop = ((COP*)op); | |
4756 | goto nothin; | |
79072805 | 4757 | case OP_SCALAR: |
93a17b20 | 4758 | case OP_LINESEQ: |
463ee0b2 | 4759 | case OP_SCOPE: |
748a9306 | 4760 | nothin: |
a0d0e21e LW |
4761 | if (oldop && o->op_next) { |
4762 | oldop->op_next = o->op_next; | |
79072805 LW |
4763 | continue; |
4764 | } | |
c07a80fd | 4765 | o->op_seq = op_seqmax++; |
79072805 LW |
4766 | break; |
4767 | ||
4768 | case OP_GV: | |
a0d0e21e | 4769 | if (o->op_next->op_type == OP_RV2SV) { |
5f05dabc | 4770 | if (!(o->op_next->op_private & OPpDEREF)) { |
a0d0e21e LW |
4771 | null(o->op_next); |
4772 | o->op_private |= o->op_next->op_private & OPpLVAL_INTRO; | |
4773 | o->op_next = o->op_next->op_next; | |
4774 | o->op_type = OP_GVSV; | |
4775 | o->op_ppaddr = ppaddr[OP_GVSV]; | |
8990e307 LW |
4776 | } |
4777 | } | |
a0d0e21e LW |
4778 | else if (o->op_next->op_type == OP_RV2AV) { |
4779 | OP* pop = o->op_next->op_next; | |
4780 | IV i; | |
8990e307 | 4781 | if (pop->op_type == OP_CONST && |
e858de61 | 4782 | (op = pop->op_next) && |
8990e307 | 4783 | pop->op_next->op_type == OP_AELEM && |
a0d0e21e | 4784 | !(pop->op_next->op_private & |
68dc0745 | 4785 | (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) && |
a0d0e21e LW |
4786 | (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase) |
4787 | <= 255 && | |
8990e307 LW |
4788 | i >= 0) |
4789 | { | |
748a9306 | 4790 | SvREFCNT_dec(((SVOP*)pop)->op_sv); |
a0d0e21e | 4791 | null(o->op_next); |
8990e307 LW |
4792 | null(pop->op_next); |
4793 | null(pop); | |
a0d0e21e LW |
4794 | o->op_flags |= pop->op_next->op_flags & OPf_MOD; |
4795 | o->op_next = pop->op_next->op_next; | |
4796 | o->op_type = OP_AELEMFAST; | |
4797 | o->op_ppaddr = ppaddr[OP_AELEMFAST]; | |
4798 | o->op_private = (U8)i; | |
a6006777 | 4799 | GvAVn(((GVOP*)o)->op_gv); |
8990e307 | 4800 | } |
79072805 | 4801 | } |
c07a80fd | 4802 | o->op_seq = op_seqmax++; |
79072805 LW |
4803 | break; |
4804 | ||
af41f3ca MB |
4805 | case OP_PADAV: |
4806 | if (o->op_next->op_type == OP_RV2AV | |
4807 | && (o->op_next->op_flags && OPf_REF)) | |
4808 | { | |
4809 | null(o->op_next); | |
4810 | o->op_next = o->op_next->op_next; | |
4811 | } | |
4812 | break; | |
aeea060c | 4813 | |
af41f3ca MB |
4814 | case OP_PADHV: |
4815 | if (o->op_next->op_type == OP_RV2HV | |
4816 | && (o->op_next->op_flags && OPf_REF)) | |
4817 | { | |
4818 | null(o->op_next); | |
4819 | o->op_next = o->op_next->op_next; | |
4820 | } | |
4821 | break; | |
4822 | ||
a0d0e21e | 4823 | case OP_MAPWHILE: |
79072805 LW |
4824 | case OP_GREPWHILE: |
4825 | case OP_AND: | |
4826 | case OP_OR: | |
c07a80fd | 4827 | o->op_seq = op_seqmax++; |
79072805 LW |
4828 | peep(cLOGOP->op_other); |
4829 | break; | |
4830 | ||
4831 | case OP_COND_EXPR: | |
c07a80fd | 4832 | o->op_seq = op_seqmax++; |
79072805 LW |
4833 | peep(cCONDOP->op_true); |
4834 | peep(cCONDOP->op_false); | |
4835 | break; | |
4836 | ||
4837 | case OP_ENTERLOOP: | |
c07a80fd | 4838 | o->op_seq = op_seqmax++; |
79072805 LW |
4839 | peep(cLOOP->op_redoop); |
4840 | peep(cLOOP->op_nextop); | |
4841 | peep(cLOOP->op_lastop); | |
4842 | break; | |
4843 | ||
4844 | case OP_MATCH: | |
4845 | case OP_SUBST: | |
c07a80fd | 4846 | o->op_seq = op_seqmax++; |
a0d0e21e | 4847 | peep(cPMOP->op_pmreplstart); |
79072805 LW |
4848 | break; |
4849 | ||
a0d0e21e | 4850 | case OP_EXEC: |
c07a80fd | 4851 | o->op_seq = op_seqmax++; |
a0d0e21e LW |
4852 | if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) { |
4853 | if (o->op_next->op_sibling && | |
4854 | o->op_next->op_sibling->op_type != OP_DIE) { | |
4855 | line_t oldline = curcop->cop_line; | |
4856 | ||
4857 | curcop->cop_line = ((COP*)o->op_next)->cop_line; | |
4858 | warn("Statement unlikely to be reached"); | |
4859 | warn("(Maybe you meant system() when you said exec()?)\n"); | |
4860 | curcop->cop_line = oldline; | |
4861 | } | |
4862 | } | |
4863 | break; | |
aeea060c | 4864 | |
c750a3ec MB |
4865 | case OP_HELEM: { |
4866 | UNOP *rop; | |
4867 | SV *lexname; | |
4868 | GV **fields; | |
4869 | SV **svp, **indsvp; | |
4870 | I32 ind; | |
4871 | char *key; | |
4872 | STRLEN keylen; | |
aeea060c | 4873 | |
c750a3ec MB |
4874 | if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO) |
4875 | || ((BINOP*)o)->op_last->op_type != OP_CONST) | |
4876 | break; | |
4877 | rop = (UNOP*)((BINOP*)o)->op_first; | |
4878 | if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV) | |
4879 | break; | |
4880 | lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE); | |
4881 | if (!SvOBJECT(lexname)) | |
4882 | break; | |
5196be3e | 4883 | fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE); |
c750a3ec MB |
4884 | if (!fields || !GvHV(*fields)) |
4885 | break; | |
4886 | svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv; | |
4887 | key = SvPV(*svp, keylen); | |
4888 | indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE); | |
4889 | if (!indsvp) { | |
4890 | croak("No such field \"%s\" in variable %s of type %s", | |
4891 | key, SvPV(lexname, na), HvNAME(SvSTASH(lexname))); | |
4892 | } | |
4893 | ind = SvIV(*indsvp); | |
4894 | if (ind < 1) | |
4895 | croak("Bad index while coercing array into hash"); | |
4896 | rop->op_type = OP_RV2AV; | |
4897 | rop->op_ppaddr = ppaddr[OP_RV2AV]; | |
4898 | o->op_type = OP_AELEM; | |
4899 | o->op_ppaddr = ppaddr[OP_AELEM]; | |
4900 | SvREFCNT_dec(*svp); | |
4901 | *svp = newSViv(ind); | |
4902 | break; | |
4903 | } | |
4904 | ||
79072805 | 4905 | default: |
c07a80fd | 4906 | o->op_seq = op_seqmax++; |
79072805 LW |
4907 | break; |
4908 | } | |
a0d0e21e | 4909 | oldop = o; |
79072805 | 4910 | } |
a0d0e21e | 4911 | LEAVE; |
79072805 | 4912 | } |