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