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 | 89 | if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) { |
9c82454c | 90 | dTHR; |
bde90e66 HS |
91 | SV *msg = sv_2mortal( |
92 | newSVpvf("(Did you mean $ or @ instead of %c?)\n", | |
93 | type == OP_ENTERSUB ? '&' : '%')); | |
94 | if (in_eval & 2) | |
95 | warn("%_", msg); | |
96 | else if (in_eval) | |
97 | sv_catsv(GvSV(errgv), msg); | |
98 | else | |
99 | PerlIO_write(PerlIO_stderr(), SvPVX(msg), SvCUR(msg)); | |
100 | } | |
a0d0e21e LW |
101 | } |
102 | } | |
103 | ||
79072805 LW |
104 | /* "register" allocation */ |
105 | ||
106 | PADOFFSET | |
8ac85365 | 107 | pad_allocmy(char *name) |
93a17b20 | 108 | { |
11343788 | 109 | dTHR; |
a0d0e21e LW |
110 | PADOFFSET off; |
111 | SV *sv; | |
112 | ||
113 | if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) { | |
46fc3d4c | 114 | if (!isPRINT(name[1])) { |
115 | name[3] = '\0'; | |
116 | name[2] = toCTRL(name[1]); | |
117 | name[1] = '^'; | |
118 | } | |
a0d0e21e LW |
119 | croak("Can't use global %s in \"my\"",name); |
120 | } | |
93965878 | 121 | if (dowarn && AvFILLp(comppad_name) >= 0) { |
b1cb66bf | 122 | SV **svp = AvARRAY(comppad_name); |
93965878 | 123 | for (off = AvFILLp(comppad_name); off > comppad_name_floor; off--) { |
b1cb66bf | 124 | if ((sv = svp[off]) |
125 | && sv != &sv_undef | |
126 | && SvIVX(sv) == 999999999 /* var is in open scope */ | |
127 | && strEQ(name, SvPVX(sv))) | |
128 | { | |
129 | warn("\"my\" variable %s masks earlier declaration in same scope", name); | |
130 | break; | |
131 | } | |
132 | } | |
133 | } | |
a0d0e21e LW |
134 | off = pad_alloc(OP_PADSV, SVs_PADMY); |
135 | sv = NEWSV(1102,0); | |
93a17b20 LW |
136 | sv_upgrade(sv, SVt_PVNV); |
137 | sv_setpv(sv, name); | |
c750a3ec MB |
138 | if (in_my_stash) { |
139 | if (*name != '$') | |
140 | croak("Can't declare class for non-scalar %s in \"my\"",name); | |
141 | SvOBJECT_on(sv); | |
142 | (void)SvUPGRADE(sv, SVt_PVMG); | |
143 | SvSTASH(sv) = (HV*)SvREFCNT_inc(in_my_stash); | |
144 | sv_objcount++; | |
145 | } | |
8990e307 | 146 | av_store(comppad_name, off, sv); |
748a9306 | 147 | SvNVX(sv) = (double)999999999; |
8990e307 LW |
148 | SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */ |
149 | if (!min_intro_pending) | |
150 | min_intro_pending = off; | |
151 | max_intro_pending = off; | |
93a17b20 | 152 | if (*name == '@') |
463ee0b2 | 153 | av_store(comppad, off, (SV*)newAV()); |
93a17b20 | 154 | else if (*name == '%') |
463ee0b2 | 155 | av_store(comppad, off, (SV*)newHV()); |
ed6116ce | 156 | SvPADMY_on(curpad[off]); |
93a17b20 LW |
157 | return off; |
158 | } | |
159 | ||
748a9306 | 160 | static PADOFFSET |
bbce6d69 | 161 | pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix) |
93a17b20 | 162 | { |
11343788 | 163 | dTHR; |
748a9306 | 164 | CV *cv; |
93a17b20 LW |
165 | I32 off; |
166 | SV *sv; | |
93a17b20 | 167 | register I32 i; |
c09156bb | 168 | register PERL_CONTEXT *cx; |
a0d0e21e | 169 | int saweval; |
93a17b20 | 170 | |
748a9306 | 171 | for (cv = startcv; cv; cv = CvOUTSIDE(cv)) { |
4fdae800 | 172 | AV *curlist = CvPADLIST(cv); |
173 | SV **svp = av_fetch(curlist, 0, FALSE); | |
748a9306 | 174 | AV *curname; |
4fdae800 | 175 | |
748a9306 | 176 | if (!svp || *svp == &sv_undef) |
4633a7c4 | 177 | continue; |
748a9306 LW |
178 | curname = (AV*)*svp; |
179 | svp = AvARRAY(curname); | |
93965878 | 180 | for (off = AvFILLp(curname); off > 0; off--) { |
748a9306 LW |
181 | if ((sv = svp[off]) && |
182 | sv != &sv_undef && | |
183 | seq <= SvIVX(sv) && | |
13826f2c | 184 | seq > I_32(SvNVX(sv)) && |
748a9306 LW |
185 | strEQ(SvPVX(sv), name)) |
186 | { | |
5f05dabc | 187 | I32 depth; |
188 | AV *oldpad; | |
189 | SV *oldsv; | |
190 | ||
191 | depth = CvDEPTH(cv); | |
192 | if (!depth) { | |
9607fc9c | 193 | if (newoff) { |
194 | if (SvFAKE(sv)) | |
195 | continue; | |
4fdae800 | 196 | return 0; /* don't clone from inactive stack frame */ |
9607fc9c | 197 | } |
5f05dabc | 198 | depth = 1; |
199 | } | |
200 | oldpad = (AV*)*av_fetch(curlist, depth, FALSE); | |
201 | oldsv = *av_fetch(oldpad, off, TRUE); | |
748a9306 | 202 | if (!newoff) { /* Not a mere clone operation. */ |
9607fc9c | 203 | SV *namesv = NEWSV(1103,0); |
748a9306 | 204 | newoff = pad_alloc(OP_PADSV, SVs_PADMY); |
9607fc9c | 205 | sv_upgrade(namesv, SVt_PVNV); |
206 | sv_setpv(namesv, name); | |
207 | av_store(comppad_name, newoff, namesv); | |
208 | SvNVX(namesv) = (double)curcop->cop_seq; | |
209 | SvIVX(namesv) = 999999999; /* A ref, intro immediately */ | |
210 | SvFAKE_on(namesv); /* A ref, not a real var */ | |
774d564b | 211 | if (CvANON(compcv) || SvTYPE(compcv) == SVt_PVFM) { |
28757baa | 212 | /* "It's closures all the way down." */ |
213 | CvCLONE_on(compcv); | |
54310121 | 214 | if (cv == startcv) { |
215 | if (CvANON(compcv)) | |
216 | oldsv = Nullsv; /* no need to keep ref */ | |
217 | } | |
218 | else { | |
28757baa | 219 | CV *bcv; |
220 | for (bcv = startcv; | |
221 | bcv && bcv != cv && !CvCLONE(bcv); | |
222 | bcv = CvOUTSIDE(bcv)) { | |
223 | if (CvANON(bcv)) | |
224 | CvCLONE_on(bcv); | |
225 | else { | |
774d564b | 226 | if (dowarn && !CvUNIQUE(cv)) |
44a8e56a | 227 | warn( |
228 | "Variable \"%s\" may be unavailable", | |
28757baa | 229 | name); |
230 | break; | |
231 | } | |
232 | } | |
233 | } | |
234 | } | |
44a8e56a | 235 | else if (!CvUNIQUE(compcv)) { |
9607fc9c | 236 | if (dowarn && !SvFAKE(sv) && !CvUNIQUE(cv)) |
44a8e56a | 237 | warn("Variable \"%s\" will not stay shared", name); |
5f05dabc | 238 | } |
748a9306 | 239 | } |
199100c8 | 240 | av_store(comppad, newoff, SvREFCNT_inc(oldsv)); |
748a9306 LW |
241 | return newoff; |
242 | } | |
93a17b20 LW |
243 | } |
244 | } | |
245 | ||
246 | /* Nothing in current lexical context--try eval's context, if any. | |
247 | * This is necessary to let the perldb get at lexically scoped variables. | |
248 | * XXX This will also probably interact badly with eval tree caching. | |
249 | */ | |
250 | ||
a0d0e21e | 251 | saweval = 0; |
748a9306 | 252 | for (i = cx_ix; i >= 0; i--) { |
93a17b20 LW |
253 | cx = &cxstack[i]; |
254 | switch (cx->cx_type) { | |
255 | default: | |
748a9306 LW |
256 | if (i == 0 && saweval) { |
257 | seq = cxstack[saweval].blk_oldcop->cop_seq; | |
258 | return pad_findlex(name, newoff, seq, main_cv, 0); | |
259 | } | |
93a17b20 LW |
260 | break; |
261 | case CXt_EVAL: | |
44a8e56a | 262 | switch (cx->blk_eval.old_op_type) { |
263 | case OP_ENTEREVAL: | |
264 | saweval = i; | |
265 | break; | |
266 | case OP_REQUIRE: | |
267 | /* require must have its own scope */ | |
268 | return 0; | |
269 | } | |
93a17b20 LW |
270 | break; |
271 | case CXt_SUB: | |
272 | if (!saweval) | |
273 | return 0; | |
274 | cv = cx->blk_sub.cv; | |
748a9306 LW |
275 | if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */ |
276 | saweval = i; /* so we know where we were called from */ | |
93a17b20 | 277 | continue; |
93a17b20 | 278 | } |
748a9306 LW |
279 | seq = cxstack[saweval].blk_oldcop->cop_seq; |
280 | return pad_findlex(name, newoff, seq, cv, i-1); | |
93a17b20 LW |
281 | } |
282 | } | |
283 | ||
748a9306 LW |
284 | return 0; |
285 | } | |
a0d0e21e | 286 | |
748a9306 | 287 | PADOFFSET |
8ac85365 | 288 | pad_findmy(char *name) |
748a9306 | 289 | { |
11343788 | 290 | dTHR; |
748a9306 | 291 | I32 off; |
54310121 | 292 | I32 pendoff = 0; |
748a9306 LW |
293 | SV *sv; |
294 | SV **svp = AvARRAY(comppad_name); | |
bbce6d69 | 295 | U32 seq = cop_seqmax; |
748a9306 | 296 | |
11343788 MB |
297 | #ifdef USE_THREADS |
298 | /* | |
299 | * Special case to get lexical (and hence per-thread) @_. | |
300 | * XXX I need to find out how to tell at parse-time whether use | |
301 | * of @_ should refer to a lexical (from a sub) or defgv (global | |
302 | * scope and maybe weird sub-ish things like formats). See | |
303 | * startsub in perly.y. It's possible that @_ could be lexical | |
304 | * (at least from subs) even in non-threaded perl. | |
305 | */ | |
306 | if (strEQ(name, "@_")) | |
307 | return 0; /* success. (NOT_IN_PAD indicates failure) */ | |
308 | #endif /* USE_THREADS */ | |
309 | ||
748a9306 | 310 | /* The one we're looking for is probably just before comppad_name_fill. */ |
93965878 | 311 | for (off = AvFILLp(comppad_name); off > 0; off--) { |
a0d0e21e LW |
312 | if ((sv = svp[off]) && |
313 | sv != &sv_undef && | |
54310121 | 314 | (!SvIVX(sv) || |
315 | (seq <= SvIVX(sv) && | |
316 | seq > I_32(SvNVX(sv)))) && | |
a0d0e21e LW |
317 | strEQ(SvPVX(sv), name)) |
318 | { | |
54310121 | 319 | if (SvIVX(sv)) |
320 | return (PADOFFSET)off; | |
321 | pendoff = off; /* this pending def. will override import */ | |
a0d0e21e LW |
322 | } |
323 | } | |
748a9306 LW |
324 | |
325 | /* See if it's in a nested scope */ | |
326 | off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix); | |
54310121 | 327 | if (off) { |
328 | /* If there is a pending local definition, this new alias must die */ | |
329 | if (pendoff) | |
330 | SvIVX(AvARRAY(comppad_name)[off]) = seq; | |
11343788 | 331 | return off; /* pad_findlex returns 0 for failure...*/ |
54310121 | 332 | } |
11343788 | 333 | return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */ |
93a17b20 LW |
334 | } |
335 | ||
336 | void | |
8ac85365 | 337 | pad_leavemy(I32 fill) |
93a17b20 LW |
338 | { |
339 | I32 off; | |
8990e307 | 340 | SV **svp = AvARRAY(comppad_name); |
93a17b20 | 341 | SV *sv; |
8990e307 LW |
342 | if (min_intro_pending && fill < min_intro_pending) { |
343 | for (off = max_intro_pending; off >= min_intro_pending; off--) { | |
a0d0e21e | 344 | if ((sv = svp[off]) && sv != &sv_undef) |
8990e307 LW |
345 | warn("%s never introduced", SvPVX(sv)); |
346 | } | |
347 | } | |
348 | /* "Deintroduce" my variables that are leaving with this scope. */ | |
93965878 | 349 | for (off = AvFILLp(comppad_name); off > fill; off--) { |
748a9306 | 350 | if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999) |
463ee0b2 | 351 | SvIVX(sv) = cop_seqmax; |
93a17b20 LW |
352 | } |
353 | } | |
354 | ||
355 | PADOFFSET | |
8ac85365 | 356 | pad_alloc(I32 optype, U32 tmptype) |
79072805 | 357 | { |
11343788 | 358 | dTHR; |
79072805 LW |
359 | SV *sv; |
360 | I32 retval; | |
361 | ||
362 | if (AvARRAY(comppad) != curpad) | |
463ee0b2 | 363 | croak("panic: pad_alloc"); |
a0d0e21e LW |
364 | if (pad_reset_pending) |
365 | pad_reset(); | |
ed6116ce | 366 | if (tmptype & SVs_PADMY) { |
79072805 | 367 | do { |
93965878 | 368 | sv = *av_fetch(comppad, AvFILLp(comppad) + 1, TRUE); |
ed6116ce | 369 | } while (SvPADBUSY(sv)); /* need a fresh one */ |
93965878 | 370 | retval = AvFILLp(comppad); |
79072805 LW |
371 | } |
372 | else { | |
bbce6d69 | 373 | SV **names = AvARRAY(comppad_name); |
93965878 | 374 | SSize_t names_fill = AvFILLp(comppad_name); |
bbce6d69 | 375 | for (;;) { |
376 | /* | |
377 | * "foreach" index vars temporarily become aliases to non-"my" | |
378 | * values. Thus we must skip, not just pad values that are | |
379 | * marked as current pad values, but also those with names. | |
380 | */ | |
381 | if (++padix <= names_fill && | |
382 | (sv = names[padix]) && sv != &sv_undef) | |
383 | continue; | |
384 | sv = *av_fetch(comppad, padix, TRUE); | |
385 | if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY))) | |
386 | break; | |
387 | } | |
79072805 LW |
388 | retval = padix; |
389 | } | |
8990e307 | 390 | SvFLAGS(sv) |= tmptype; |
79072805 | 391 | curpad = AvARRAY(comppad); |
11343788 | 392 | #ifdef USE_THREADS |
5dc0d613 MB |
393 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n", |
394 | (unsigned long) thr, (unsigned long) curpad, | |
395 | (long) retval, op_name[optype])); | |
11343788 | 396 | #else |
d9bb4600 GS |
397 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx alloc %ld for %s\n", |
398 | (unsigned long) curpad, | |
5dc0d613 | 399 | (long) retval, op_name[optype])); |
11343788 | 400 | #endif /* USE_THREADS */ |
79072805 LW |
401 | return (PADOFFSET)retval; |
402 | } | |
403 | ||
404 | SV * | |
8990e307 | 405 | pad_sv(PADOFFSET po) |
79072805 | 406 | { |
11343788 MB |
407 | dTHR; |
408 | #ifdef USE_THREADS | |
5dc0d613 MB |
409 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n", |
410 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 411 | #else |
79072805 | 412 | if (!po) |
463ee0b2 | 413 | croak("panic: pad_sv po"); |
d9bb4600 GS |
414 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx sv %d\n", |
415 | (unsigned long) curpad, po)); | |
11343788 | 416 | #endif /* USE_THREADS */ |
79072805 LW |
417 | return curpad[po]; /* eventually we'll turn this into a macro */ |
418 | } | |
419 | ||
420 | void | |
8990e307 | 421 | pad_free(PADOFFSET po) |
79072805 | 422 | { |
11343788 | 423 | dTHR; |
a0d0e21e LW |
424 | if (!curpad) |
425 | return; | |
79072805 | 426 | if (AvARRAY(comppad) != curpad) |
463ee0b2 | 427 | croak("panic: pad_free curpad"); |
79072805 | 428 | if (!po) |
463ee0b2 | 429 | croak("panic: pad_free po"); |
11343788 | 430 | #ifdef USE_THREADS |
5dc0d613 MB |
431 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n", |
432 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 433 | #else |
d9bb4600 GS |
434 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx free %d\n", |
435 | (unsigned long) curpad, po)); | |
11343788 | 436 | #endif /* USE_THREADS */ |
a0d0e21e | 437 | if (curpad[po] && curpad[po] != &sv_undef) |
ed6116ce | 438 | SvPADTMP_off(curpad[po]); |
a0d0e21e | 439 | if ((I32)po < padix) |
79072805 LW |
440 | padix = po - 1; |
441 | } | |
442 | ||
443 | void | |
8990e307 | 444 | pad_swipe(PADOFFSET po) |
79072805 | 445 | { |
11343788 | 446 | dTHR; |
79072805 | 447 | if (AvARRAY(comppad) != curpad) |
463ee0b2 | 448 | croak("panic: pad_swipe curpad"); |
79072805 | 449 | if (!po) |
463ee0b2 | 450 | croak("panic: pad_swipe po"); |
11343788 | 451 | #ifdef USE_THREADS |
5dc0d613 MB |
452 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n", |
453 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 454 | #else |
d9bb4600 GS |
455 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx swipe %d\n", |
456 | (unsigned long) curpad, po)); | |
11343788 | 457 | #endif /* USE_THREADS */ |
ed6116ce | 458 | SvPADTMP_off(curpad[po]); |
a0d0e21e LW |
459 | curpad[po] = NEWSV(1107,0); |
460 | SvPADTMP_on(curpad[po]); | |
461 | if ((I32)po < padix) | |
79072805 LW |
462 | padix = po - 1; |
463 | } | |
464 | ||
d9bb4600 GS |
465 | /* XXX pad_reset() is currently disabled because it results in serious bugs. |
466 | * It causes pad temp TARGs to be shared between OPs. Since TARGs are pushed | |
467 | * on the stack by OPs that use them, there are several ways to get an alias | |
468 | * to a shared TARG. Such an alias will change randomly and unpredictably. | |
469 | * We avoid doing this until we can think of a Better Way. | |
470 | * GSAR 97-10-29 */ | |
79072805 | 471 | void |
8ac85365 | 472 | pad_reset(void) |
79072805 | 473 | { |
d9bb4600 | 474 | #ifdef USE_BROKEN_PAD_RESET |
11343788 | 475 | dTHR; |
79072805 LW |
476 | register I32 po; |
477 | ||
478 | if (AvARRAY(comppad) != curpad) | |
463ee0b2 | 479 | croak("panic: pad_reset curpad"); |
11343788 | 480 | #ifdef USE_THREADS |
5dc0d613 MB |
481 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n", |
482 | (unsigned long) thr, (unsigned long) curpad)); | |
11343788 | 483 | #else |
d9bb4600 GS |
484 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad 0x%lx reset\n", |
485 | (unsigned long) curpad)); | |
11343788 | 486 | #endif /* USE_THREADS */ |
748a9306 LW |
487 | if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */ |
488 | for (po = AvMAX(comppad); po > padix_floor; po--) { | |
ff0cee69 | 489 | if (curpad[po] && !SvIMMORTAL(curpad[po])) |
748a9306 LW |
490 | SvPADTMP_off(curpad[po]); |
491 | } | |
492 | padix = padix_floor; | |
79072805 | 493 | } |
d9bb4600 | 494 | #endif |
a0d0e21e | 495 | pad_reset_pending = FALSE; |
79072805 LW |
496 | } |
497 | ||
a863c7d1 | 498 | #ifdef USE_THREADS |
54b9620d | 499 | /* find_threadsv is not reentrant */ |
a863c7d1 | 500 | PADOFFSET |
54b9620d | 501 | find_threadsv(char *name) |
a863c7d1 MB |
502 | { |
503 | dTHR; | |
504 | char *p; | |
505 | PADOFFSET key; | |
554b3eca | 506 | SV **svp; |
54b9620d MB |
507 | /* We currently only handle names of a single character */ |
508 | p = strchr(threadsv_names, *name); | |
a863c7d1 MB |
509 | if (!p) |
510 | return NOT_IN_PAD; | |
54b9620d MB |
511 | key = p - threadsv_names; |
512 | svp = av_fetch(thr->threadsv, key, FALSE); | |
554b3eca MB |
513 | if (!svp) { |
514 | SV *sv = NEWSV(0, 0); | |
54b9620d | 515 | av_store(thr->threadsv, key, sv); |
940cb80d | 516 | thr->threadsvp = AvARRAY(thr->threadsv); |
554b3eca MB |
517 | /* |
518 | * Some magic variables used to be automagically initialised | |
519 | * in gv_fetchpv. Those which are now per-thread magicals get | |
520 | * initialised here instead. | |
521 | */ | |
522 | switch (*name) { | |
54b9620d MB |
523 | case '_': |
524 | break; | |
554b3eca MB |
525 | case ';': |
526 | sv_setpv(sv, "\034"); | |
54b9620d | 527 | sv_magic(sv, 0, 0, name, 1); |
554b3eca | 528 | break; |
c277df42 IZ |
529 | case '&': |
530 | case '`': | |
531 | case '\'': | |
532 | sawampersand = TRUE; | |
533 | SvREADONLY_on(sv); | |
d8b5173a | 534 | /* FALL THROUGH */ |
54b9620d MB |
535 | default: |
536 | sv_magic(sv, 0, 0, name, 1); | |
554b3eca | 537 | } |
a863c7d1 | 538 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), |
54b9620d | 539 | "find_threadsv: new SV %p for $%s%c\n", |
554b3eca MB |
540 | sv, (*name < 32) ? "^" : "", |
541 | (*name < 32) ? toCTRL(*name) : *name)); | |
a863c7d1 MB |
542 | } |
543 | return key; | |
544 | } | |
545 | #endif /* USE_THREADS */ | |
546 | ||
79072805 LW |
547 | /* Destructor */ |
548 | ||
549 | void | |
8ac85365 | 550 | op_free(OP *o) |
79072805 | 551 | { |
85e6fe83 | 552 | register OP *kid, *nextkid; |
79072805 | 553 | |
5dc0d613 | 554 | if (!o || o->op_seq == (U16)-1) |
79072805 LW |
555 | return; |
556 | ||
11343788 MB |
557 | if (o->op_flags & OPf_KIDS) { |
558 | for (kid = cUNOPo->op_first; kid; kid = nextkid) { | |
85e6fe83 | 559 | nextkid = kid->op_sibling; /* Get before next freeing kid */ |
79072805 | 560 | op_free(kid); |
85e6fe83 | 561 | } |
79072805 LW |
562 | } |
563 | ||
11343788 | 564 | switch (o->op_type) { |
8990e307 | 565 | case OP_NULL: |
11343788 | 566 | o->op_targ = 0; /* Was holding old type, if any. */ |
8990e307 | 567 | break; |
a0d0e21e | 568 | case OP_ENTEREVAL: |
11343788 | 569 | o->op_targ = 0; /* Was holding hints. */ |
a0d0e21e | 570 | break; |
554b3eca | 571 | #ifdef USE_THREADS |
2faa37cc | 572 | case OP_THREADSV: |
54b9620d | 573 | o->op_targ = 0; /* Was holding index into thr->threadsv AV. */ |
554b3eca MB |
574 | break; |
575 | #endif /* USE_THREADS */ | |
a6006777 | 576 | default: |
5196be3e | 577 | if (!(o->op_flags & OPf_REF) || (check[o->op_type] != ck_ftst)) |
a6006777 | 578 | break; |
579 | /* FALL THROUGH */ | |
463ee0b2 | 580 | case OP_GVSV: |
79072805 | 581 | case OP_GV: |
a6006777 | 582 | case OP_AELEMFAST: |
11343788 | 583 | SvREFCNT_dec(cGVOPo->op_gv); |
8990e307 LW |
584 | break; |
585 | case OP_NEXTSTATE: | |
586 | case OP_DBSTATE: | |
5196be3e | 587 | Safefree(cCOPo->cop_label); |
11343788 | 588 | SvREFCNT_dec(cCOPo->cop_filegv); |
79072805 LW |
589 | break; |
590 | case OP_CONST: | |
11343788 | 591 | SvREFCNT_dec(cSVOPo->op_sv); |
79072805 | 592 | break; |
748a9306 LW |
593 | case OP_GOTO: |
594 | case OP_NEXT: | |
595 | case OP_LAST: | |
596 | case OP_REDO: | |
11343788 | 597 | if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS)) |
748a9306 LW |
598 | break; |
599 | /* FALL THROUGH */ | |
a0d0e21e | 600 | case OP_TRANS: |
11343788 | 601 | Safefree(cPVOPo->op_pv); |
a0d0e21e LW |
602 | break; |
603 | case OP_SUBST: | |
11343788 | 604 | op_free(cPMOPo->op_pmreplroot); |
a0d0e21e | 605 | /* FALL THROUGH */ |
748a9306 | 606 | case OP_PUSHRE: |
a0d0e21e | 607 | case OP_MATCH: |
c277df42 | 608 | ReREFCNT_dec(cPMOPo->op_pmregexp); |
a0d0e21e | 609 | break; |
79072805 LW |
610 | } |
611 | ||
11343788 MB |
612 | if (o->op_targ > 0) |
613 | pad_free(o->op_targ); | |
8990e307 | 614 | |
11343788 | 615 | Safefree(o); |
79072805 LW |
616 | } |
617 | ||
8990e307 | 618 | static void |
8ac85365 | 619 | null(OP *o) |
8990e307 | 620 | { |
54b9620d | 621 | if (o->op_type != OP_NULL && o->op_type != OP_THREADSV && o->op_targ > 0) |
11343788 MB |
622 | pad_free(o->op_targ); |
623 | o->op_targ = o->op_type; | |
624 | o->op_type = OP_NULL; | |
625 | o->op_ppaddr = ppaddr[OP_NULL]; | |
8990e307 LW |
626 | } |
627 | ||
79072805 LW |
628 | /* Contextualizers */ |
629 | ||
463ee0b2 | 630 | #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o)) |
79072805 LW |
631 | |
632 | OP * | |
8ac85365 | 633 | linklist(OP *o) |
79072805 LW |
634 | { |
635 | register OP *kid; | |
636 | ||
11343788 MB |
637 | if (o->op_next) |
638 | return o->op_next; | |
79072805 LW |
639 | |
640 | /* establish postfix order */ | |
11343788 MB |
641 | if (cUNOPo->op_first) { |
642 | o->op_next = LINKLIST(cUNOPo->op_first); | |
643 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { | |
79072805 LW |
644 | if (kid->op_sibling) |
645 | kid->op_next = LINKLIST(kid->op_sibling); | |
646 | else | |
11343788 | 647 | kid->op_next = o; |
79072805 LW |
648 | } |
649 | } | |
650 | else | |
11343788 | 651 | o->op_next = o; |
79072805 | 652 | |
11343788 | 653 | return o->op_next; |
79072805 LW |
654 | } |
655 | ||
656 | OP * | |
8ac85365 | 657 | scalarkids(OP *o) |
79072805 LW |
658 | { |
659 | OP *kid; | |
11343788 MB |
660 | if (o && o->op_flags & OPf_KIDS) { |
661 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
79072805 LW |
662 | scalar(kid); |
663 | } | |
11343788 | 664 | return o; |
79072805 LW |
665 | } |
666 | ||
a0d0e21e | 667 | static OP * |
8ac85365 | 668 | scalarboolean(OP *o) |
8990e307 LW |
669 | { |
670 | if (dowarn && | |
11343788 | 671 | o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) { |
0f15f207 | 672 | dTHR; |
a0d0e21e LW |
673 | line_t oldline = curcop->cop_line; |
674 | ||
675 | if (copline != NOLINE) | |
676 | curcop->cop_line = copline; | |
677 | warn("Found = in conditional, should be =="); | |
678 | curcop->cop_line = oldline; | |
679 | } | |
11343788 | 680 | return scalar(o); |
8990e307 LW |
681 | } |
682 | ||
683 | OP * | |
8ac85365 | 684 | scalar(OP *o) |
79072805 LW |
685 | { |
686 | OP *kid; | |
687 | ||
a0d0e21e | 688 | /* assumes no premature commitment */ |
5dc0d613 MB |
689 | if (!o || (o->op_flags & OPf_WANT) || error_count |
690 | || o->op_type == OP_RETURN) | |
11343788 | 691 | return o; |
79072805 | 692 | |
5dc0d613 | 693 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR; |
79072805 | 694 | |
11343788 | 695 | switch (o->op_type) { |
79072805 | 696 | case OP_REPEAT: |
11343788 MB |
697 | if (o->op_private & OPpREPEAT_DOLIST) |
698 | null(((LISTOP*)cBINOPo->op_first)->op_first); | |
699 | scalar(cBINOPo->op_first); | |
8990e307 | 700 | break; |
79072805 LW |
701 | case OP_OR: |
702 | case OP_AND: | |
703 | case OP_COND_EXPR: | |
11343788 | 704 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
8990e307 | 705 | scalar(kid); |
79072805 | 706 | break; |
a0d0e21e | 707 | case OP_SPLIT: |
11343788 | 708 | if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) { |
a0d0e21e LW |
709 | if (!kPMOP->op_pmreplroot) |
710 | deprecate("implicit split to @_"); | |
711 | } | |
712 | /* FALL THROUGH */ | |
79072805 LW |
713 | case OP_MATCH: |
714 | case OP_SUBST: | |
715 | case OP_NULL: | |
8990e307 | 716 | default: |
11343788 MB |
717 | if (o->op_flags & OPf_KIDS) { |
718 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) | |
8990e307 LW |
719 | scalar(kid); |
720 | } | |
79072805 LW |
721 | break; |
722 | case OP_LEAVE: | |
723 | case OP_LEAVETRY: | |
5dc0d613 | 724 | kid = cLISTOPo->op_first; |
54310121 | 725 | scalar(kid); |
726 | while (kid = kid->op_sibling) { | |
727 | if (kid->op_sibling) | |
728 | scalarvoid(kid); | |
729 | else | |
730 | scalar(kid); | |
731 | } | |
0f15f207 | 732 | WITH_THR(curcop = &compiling); |
54310121 | 733 | break; |
748a9306 | 734 | case OP_SCOPE: |
79072805 | 735 | case OP_LINESEQ: |
8990e307 | 736 | case OP_LIST: |
11343788 | 737 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
79072805 LW |
738 | if (kid->op_sibling) |
739 | scalarvoid(kid); | |
740 | else | |
741 | scalar(kid); | |
742 | } | |
0f15f207 | 743 | WITH_THR(curcop = &compiling); |
79072805 LW |
744 | break; |
745 | } | |
11343788 | 746 | return o; |
79072805 LW |
747 | } |
748 | ||
749 | OP * | |
8ac85365 | 750 | scalarvoid(OP *o) |
79072805 LW |
751 | { |
752 | OP *kid; | |
8990e307 LW |
753 | char* useless = 0; |
754 | SV* sv; | |
79072805 | 755 | |
54310121 | 756 | /* assumes no premature commitment */ |
5dc0d613 MB |
757 | if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || error_count |
758 | || o->op_type == OP_RETURN) | |
11343788 | 759 | return o; |
79072805 | 760 | |
5dc0d613 | 761 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; |
79072805 | 762 | |
11343788 | 763 | switch (o->op_type) { |
79072805 | 764 | default: |
11343788 | 765 | if (!(opargs[o->op_type] & OA_FOLDCONST)) |
8990e307 | 766 | break; |
36477c24 | 767 | /* FALL THROUGH */ |
768 | case OP_REPEAT: | |
11343788 | 769 | if (o->op_flags & OPf_STACKED) |
8990e307 LW |
770 | break; |
771 | /* FALL THROUGH */ | |
772 | case OP_GVSV: | |
773 | case OP_WANTARRAY: | |
774 | case OP_GV: | |
775 | case OP_PADSV: | |
776 | case OP_PADAV: | |
777 | case OP_PADHV: | |
778 | case OP_PADANY: | |
779 | case OP_AV2ARYLEN: | |
8990e307 | 780 | case OP_REF: |
a0d0e21e LW |
781 | case OP_REFGEN: |
782 | case OP_SREFGEN: | |
8990e307 LW |
783 | case OP_DEFINED: |
784 | case OP_HEX: | |
785 | case OP_OCT: | |
786 | case OP_LENGTH: | |
787 | case OP_SUBSTR: | |
788 | case OP_VEC: | |
789 | case OP_INDEX: | |
790 | case OP_RINDEX: | |
791 | case OP_SPRINTF: | |
792 | case OP_AELEM: | |
793 | case OP_AELEMFAST: | |
794 | case OP_ASLICE: | |
8990e307 LW |
795 | case OP_HELEM: |
796 | case OP_HSLICE: | |
797 | case OP_UNPACK: | |
798 | case OP_PACK: | |
8990e307 LW |
799 | case OP_JOIN: |
800 | case OP_LSLICE: | |
801 | case OP_ANONLIST: | |
802 | case OP_ANONHASH: | |
803 | case OP_SORT: | |
804 | case OP_REVERSE: | |
805 | case OP_RANGE: | |
806 | case OP_FLIP: | |
807 | case OP_FLOP: | |
808 | case OP_CALLER: | |
809 | case OP_FILENO: | |
810 | case OP_EOF: | |
811 | case OP_TELL: | |
812 | case OP_GETSOCKNAME: | |
813 | case OP_GETPEERNAME: | |
814 | case OP_READLINK: | |
815 | case OP_TELLDIR: | |
816 | case OP_GETPPID: | |
817 | case OP_GETPGRP: | |
818 | case OP_GETPRIORITY: | |
819 | case OP_TIME: | |
820 | case OP_TMS: | |
821 | case OP_LOCALTIME: | |
822 | case OP_GMTIME: | |
823 | case OP_GHBYNAME: | |
824 | case OP_GHBYADDR: | |
825 | case OP_GHOSTENT: | |
826 | case OP_GNBYNAME: | |
827 | case OP_GNBYADDR: | |
828 | case OP_GNETENT: | |
829 | case OP_GPBYNAME: | |
830 | case OP_GPBYNUMBER: | |
831 | case OP_GPROTOENT: | |
832 | case OP_GSBYNAME: | |
833 | case OP_GSBYPORT: | |
834 | case OP_GSERVENT: | |
835 | case OP_GPWNAM: | |
836 | case OP_GPWUID: | |
837 | case OP_GGRNAM: | |
838 | case OP_GGRGID: | |
839 | case OP_GETLOGIN: | |
11343788 MB |
840 | if (!(o->op_private & OPpLVAL_INTRO)) |
841 | useless = op_desc[o->op_type]; | |
8990e307 LW |
842 | break; |
843 | ||
844 | case OP_RV2GV: | |
845 | case OP_RV2SV: | |
846 | case OP_RV2AV: | |
847 | case OP_RV2HV: | |
11343788 MB |
848 | if (!(o->op_private & OPpLVAL_INTRO) && |
849 | (!o->op_sibling || o->op_sibling->op_type != OP_READLINE)) | |
8990e307 LW |
850 | useless = "a variable"; |
851 | break; | |
79072805 | 852 | |
93a17b20 | 853 | case OP_NEXTSTATE: |
8990e307 | 854 | case OP_DBSTATE: |
0f15f207 | 855 | WITH_THR(curcop = ((COP*)o)); /* for warning below */ |
93a17b20 LW |
856 | break; |
857 | ||
79072805 | 858 | case OP_CONST: |
11343788 | 859 | sv = cSVOPo->op_sv; |
8990e307 LW |
860 | if (dowarn) { |
861 | useless = "a constant"; | |
862 | if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0)) | |
863 | useless = 0; | |
864 | else if (SvPOK(sv)) { | |
865 | if (strnEQ(SvPVX(sv), "di", 2) || | |
a0d0e21e | 866 | strnEQ(SvPVX(sv), "ds", 2) || |
8990e307 LW |
867 | strnEQ(SvPVX(sv), "ig", 2)) |
868 | useless = 0; | |
869 | } | |
870 | } | |
11343788 | 871 | null(o); /* don't execute a constant */ |
8990e307 | 872 | SvREFCNT_dec(sv); /* don't even remember it */ |
79072805 LW |
873 | break; |
874 | ||
875 | case OP_POSTINC: | |
11343788 MB |
876 | o->op_type = OP_PREINC; /* pre-increment is faster */ |
877 | o->op_ppaddr = ppaddr[OP_PREINC]; | |
79072805 LW |
878 | break; |
879 | ||
880 | case OP_POSTDEC: | |
11343788 MB |
881 | o->op_type = OP_PREDEC; /* pre-decrement is faster */ |
882 | o->op_ppaddr = ppaddr[OP_PREDEC]; | |
79072805 LW |
883 | break; |
884 | ||
79072805 LW |
885 | case OP_OR: |
886 | case OP_AND: | |
887 | case OP_COND_EXPR: | |
11343788 | 888 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
79072805 LW |
889 | scalarvoid(kid); |
890 | break; | |
5aabfad6 | 891 | |
a0d0e21e | 892 | case OP_NULL: |
11343788 | 893 | if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE) |
0f15f207 | 894 | WITH_THR(curcop = ((COP*)o)); /* for warning below */ |
11343788 | 895 | if (o->op_flags & OPf_STACKED) |
a0d0e21e | 896 | break; |
5aabfad6 | 897 | /* FALL THROUGH */ |
79072805 LW |
898 | case OP_ENTERTRY: |
899 | case OP_ENTER: | |
900 | case OP_SCALAR: | |
11343788 | 901 | if (!(o->op_flags & OPf_KIDS)) |
79072805 | 902 | break; |
54310121 | 903 | /* FALL THROUGH */ |
463ee0b2 | 904 | case OP_SCOPE: |
79072805 LW |
905 | case OP_LEAVE: |
906 | case OP_LEAVETRY: | |
a0d0e21e | 907 | case OP_LEAVELOOP: |
79072805 | 908 | case OP_LINESEQ: |
79072805 | 909 | case OP_LIST: |
11343788 | 910 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
79072805 LW |
911 | scalarvoid(kid); |
912 | break; | |
c90c0ff4 | 913 | case OP_ENTEREVAL: |
5196be3e | 914 | scalarkids(o); |
c90c0ff4 | 915 | break; |
5aabfad6 | 916 | case OP_REQUIRE: |
c90c0ff4 | 917 | /* all requires must return a boolean value */ |
5196be3e MB |
918 | o->op_flags &= ~OPf_WANT; |
919 | return scalar(o); | |
a0d0e21e | 920 | case OP_SPLIT: |
11343788 | 921 | if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) { |
a0d0e21e LW |
922 | if (!kPMOP->op_pmreplroot) |
923 | deprecate("implicit split to @_"); | |
924 | } | |
925 | break; | |
79072805 | 926 | } |
8990e307 LW |
927 | if (useless && dowarn) |
928 | warn("Useless use of %s in void context", useless); | |
11343788 | 929 | return o; |
79072805 LW |
930 | } |
931 | ||
932 | OP * | |
8ac85365 | 933 | listkids(OP *o) |
79072805 LW |
934 | { |
935 | OP *kid; | |
11343788 MB |
936 | if (o && o->op_flags & OPf_KIDS) { |
937 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
79072805 LW |
938 | list(kid); |
939 | } | |
11343788 | 940 | return o; |
79072805 LW |
941 | } |
942 | ||
943 | OP * | |
8ac85365 | 944 | list(OP *o) |
79072805 LW |
945 | { |
946 | OP *kid; | |
947 | ||
a0d0e21e | 948 | /* assumes no premature commitment */ |
5dc0d613 MB |
949 | if (!o || (o->op_flags & OPf_WANT) || error_count |
950 | || o->op_type == OP_RETURN) | |
11343788 | 951 | return o; |
79072805 | 952 | |
5dc0d613 | 953 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST; |
79072805 | 954 | |
11343788 | 955 | switch (o->op_type) { |
79072805 LW |
956 | case OP_FLOP: |
957 | case OP_REPEAT: | |
11343788 | 958 | list(cBINOPo->op_first); |
79072805 LW |
959 | break; |
960 | case OP_OR: | |
961 | case OP_AND: | |
962 | case OP_COND_EXPR: | |
11343788 | 963 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
79072805 LW |
964 | list(kid); |
965 | break; | |
966 | default: | |
967 | case OP_MATCH: | |
968 | case OP_SUBST: | |
969 | case OP_NULL: | |
11343788 | 970 | if (!(o->op_flags & OPf_KIDS)) |
79072805 | 971 | break; |
11343788 MB |
972 | if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) { |
973 | list(cBINOPo->op_first); | |
974 | return gen_constant_list(o); | |
79072805 LW |
975 | } |
976 | case OP_LIST: | |
11343788 | 977 | listkids(o); |
79072805 LW |
978 | break; |
979 | case OP_LEAVE: | |
980 | case OP_LEAVETRY: | |
5dc0d613 | 981 | kid = cLISTOPo->op_first; |
54310121 | 982 | list(kid); |
983 | while (kid = kid->op_sibling) { | |
984 | if (kid->op_sibling) | |
985 | scalarvoid(kid); | |
986 | else | |
987 | list(kid); | |
988 | } | |
0f15f207 | 989 | WITH_THR(curcop = &compiling); |
54310121 | 990 | break; |
748a9306 | 991 | case OP_SCOPE: |
79072805 | 992 | case OP_LINESEQ: |
11343788 | 993 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
79072805 LW |
994 | if (kid->op_sibling) |
995 | scalarvoid(kid); | |
996 | else | |
997 | list(kid); | |
998 | } | |
0f15f207 | 999 | WITH_THR(curcop = &compiling); |
79072805 | 1000 | break; |
c90c0ff4 | 1001 | case OP_REQUIRE: |
1002 | /* all requires must return a boolean value */ | |
5196be3e MB |
1003 | o->op_flags &= ~OPf_WANT; |
1004 | return scalar(o); | |
79072805 | 1005 | } |
11343788 | 1006 | return o; |
79072805 LW |
1007 | } |
1008 | ||
1009 | OP * | |
8ac85365 | 1010 | scalarseq(OP *o) |
79072805 LW |
1011 | { |
1012 | OP *kid; | |
1013 | ||
11343788 MB |
1014 | if (o) { |
1015 | if (o->op_type == OP_LINESEQ || | |
1016 | o->op_type == OP_SCOPE || | |
1017 | o->op_type == OP_LEAVE || | |
1018 | o->op_type == OP_LEAVETRY) | |
463ee0b2 | 1019 | { |
0f15f207 | 1020 | dTHR; |
11343788 | 1021 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
ed6116ce | 1022 | if (kid->op_sibling) { |
463ee0b2 | 1023 | scalarvoid(kid); |
ed6116ce | 1024 | } |
463ee0b2 LW |
1025 | } |
1026 | curcop = &compiling; | |
79072805 | 1027 | } |
11343788 | 1028 | o->op_flags &= ~OPf_PARENS; |
85e6fe83 | 1029 | if (hints & HINT_BLOCK_SCOPE) |
11343788 | 1030 | o->op_flags |= OPf_PARENS; |
79072805 | 1031 | } |
8990e307 | 1032 | else |
11343788 MB |
1033 | o = newOP(OP_STUB, 0); |
1034 | return o; | |
79072805 LW |
1035 | } |
1036 | ||
a0d0e21e | 1037 | static OP * |
8ac85365 | 1038 | modkids(OP *o, I32 type) |
79072805 LW |
1039 | { |
1040 | OP *kid; | |
11343788 MB |
1041 | if (o && o->op_flags & OPf_KIDS) { |
1042 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
463ee0b2 | 1043 | mod(kid, type); |
79072805 | 1044 | } |
11343788 | 1045 | return o; |
79072805 LW |
1046 | } |
1047 | ||
79072805 | 1048 | OP * |
8ac85365 | 1049 | mod(OP *o, I32 type) |
79072805 | 1050 | { |
11343788 | 1051 | dTHR; |
79072805 LW |
1052 | OP *kid; |
1053 | SV *sv; | |
1054 | ||
11343788 MB |
1055 | if (!o || error_count) |
1056 | return o; | |
79072805 | 1057 | |
11343788 | 1058 | switch (o->op_type) { |
68dc0745 | 1059 | case OP_UNDEF: |
3e3baf6d | 1060 | modcount++; |
5dc0d613 | 1061 | return o; |
a0d0e21e | 1062 | case OP_CONST: |
11343788 | 1063 | if (!(o->op_private & (OPpCONST_ARYBASE))) |
a0d0e21e LW |
1064 | goto nomod; |
1065 | if (eval_start && eval_start->op_type == OP_CONST) { | |
1066 | compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv); | |
1067 | eval_start = 0; | |
1068 | } | |
1069 | else if (!type) { | |
1070 | SAVEI32(compiling.cop_arybase); | |
748a9306 | 1071 | compiling.cop_arybase = 0; |
a0d0e21e LW |
1072 | } |
1073 | else if (type == OP_REFGEN) | |
1074 | goto nomod; | |
1075 | else | |
1076 | croak("That use of $[ is unsupported"); | |
1077 | break; | |
5f05dabc | 1078 | case OP_STUB: |
5196be3e | 1079 | if (o->op_flags & OPf_PARENS) |
5f05dabc | 1080 | break; |
1081 | goto nomod; | |
a0d0e21e LW |
1082 | case OP_ENTERSUB: |
1083 | if ((type == OP_UNDEF || type == OP_REFGEN) && | |
11343788 MB |
1084 | !(o->op_flags & OPf_STACKED)) { |
1085 | o->op_type = OP_RV2CV; /* entersub => rv2cv */ | |
1086 | o->op_ppaddr = ppaddr[OP_RV2CV]; | |
1087 | assert(cUNOPo->op_first->op_type == OP_NULL); | |
1088 | null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */ | |
79072805 LW |
1089 | break; |
1090 | } | |
1091 | /* FALL THROUGH */ | |
1092 | default: | |
a0d0e21e LW |
1093 | nomod: |
1094 | /* grep, foreach, subcalls, refgen */ | |
1095 | if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) | |
1096 | break; | |
46fc3d4c | 1097 | yyerror(form("Can't modify %s in %s", |
5dc0d613 | 1098 | op_desc[o->op_type], |
46fc3d4c | 1099 | type ? op_desc[type] : "local")); |
11343788 | 1100 | return o; |
79072805 | 1101 | |
a0d0e21e LW |
1102 | case OP_PREINC: |
1103 | case OP_PREDEC: | |
1104 | case OP_POW: | |
1105 | case OP_MULTIPLY: | |
1106 | case OP_DIVIDE: | |
1107 | case OP_MODULO: | |
1108 | case OP_REPEAT: | |
1109 | case OP_ADD: | |
1110 | case OP_SUBTRACT: | |
1111 | case OP_CONCAT: | |
1112 | case OP_LEFT_SHIFT: | |
1113 | case OP_RIGHT_SHIFT: | |
1114 | case OP_BIT_AND: | |
1115 | case OP_BIT_XOR: | |
1116 | case OP_BIT_OR: | |
1117 | case OP_I_MULTIPLY: | |
1118 | case OP_I_DIVIDE: | |
1119 | case OP_I_MODULO: | |
1120 | case OP_I_ADD: | |
1121 | case OP_I_SUBTRACT: | |
11343788 | 1122 | if (!(o->op_flags & OPf_STACKED)) |
a0d0e21e LW |
1123 | goto nomod; |
1124 | modcount++; | |
1125 | break; | |
1126 | ||
79072805 | 1127 | case OP_COND_EXPR: |
11343788 | 1128 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
463ee0b2 | 1129 | mod(kid, type); |
79072805 LW |
1130 | break; |
1131 | ||
1132 | case OP_RV2AV: | |
1133 | case OP_RV2HV: | |
93af7a87 | 1134 | if (!type && cUNOPo->op_first->op_type != OP_GV) |
706a304b | 1135 | croak("Can't localize through a reference"); |
11343788 | 1136 | if (type == OP_REFGEN && o->op_flags & OPf_PARENS) { |
748a9306 | 1137 | modcount = 10000; |
11343788 | 1138 | return o; /* Treat \(@foo) like ordinary list. */ |
748a9306 LW |
1139 | } |
1140 | /* FALL THROUGH */ | |
79072805 | 1141 | case OP_RV2GV: |
5dc0d613 | 1142 | if (scalar_mod_type(o, type)) |
3fe9a6f1 | 1143 | goto nomod; |
11343788 | 1144 | ref(cUNOPo->op_first, o->op_type); |
79072805 LW |
1145 | /* FALL THROUGH */ |
1146 | case OP_AASSIGN: | |
1147 | case OP_ASLICE: | |
1148 | case OP_HSLICE: | |
93a17b20 LW |
1149 | case OP_NEXTSTATE: |
1150 | case OP_DBSTATE: | |
a0d0e21e LW |
1151 | case OP_REFGEN: |
1152 | case OP_CHOMP: | |
463ee0b2 | 1153 | modcount = 10000; |
79072805 | 1154 | break; |
463ee0b2 | 1155 | case OP_RV2SV: |
11343788 | 1156 | if (!type && cUNOPo->op_first->op_type != OP_GV) |
706a304b | 1157 | croak("Can't localize through a reference"); |
aeea060c | 1158 | ref(cUNOPo->op_first, o->op_type); |
463ee0b2 | 1159 | /* FALL THROUGH */ |
79072805 | 1160 | case OP_GV: |
463ee0b2 | 1161 | case OP_AV2ARYLEN: |
85aff577 | 1162 | hints |= HINT_BLOCK_SCOPE; |
463ee0b2 | 1163 | case OP_SASSIGN: |
8990e307 LW |
1164 | case OP_AELEMFAST: |
1165 | modcount++; | |
1166 | break; | |
1167 | ||
748a9306 LW |
1168 | case OP_PADAV: |
1169 | case OP_PADHV: | |
1170 | modcount = 10000; | |
5196be3e MB |
1171 | if (type == OP_REFGEN && o->op_flags & OPf_PARENS) |
1172 | return o; /* Treat \(@foo) like ordinary list. */ | |
1173 | if (scalar_mod_type(o, type)) | |
3fe9a6f1 | 1174 | goto nomod; |
748a9306 LW |
1175 | /* FALL THROUGH */ |
1176 | case OP_PADSV: | |
1177 | modcount++; | |
1178 | if (!type) | |
1179 | croak("Can't localize lexical variable %s", | |
11343788 | 1180 | SvPV(*av_fetch(comppad_name, o->op_targ, 4), na)); |
463ee0b2 LW |
1181 | break; |
1182 | ||
554b3eca | 1183 | #ifdef USE_THREADS |
2faa37cc | 1184 | case OP_THREADSV: |
554b3eca | 1185 | modcount++; /* XXX ??? */ |
554b3eca MB |
1186 | break; |
1187 | #endif /* USE_THREADS */ | |
1188 | ||
748a9306 LW |
1189 | case OP_PUSHMARK: |
1190 | break; | |
a0d0e21e | 1191 | |
69969c6f SB |
1192 | case OP_KEYS: |
1193 | if (type != OP_SASSIGN) | |
1194 | goto nomod; | |
5f05dabc | 1195 | /* FALL THROUGH */ |
a0d0e21e | 1196 | case OP_POS: |
463ee0b2 | 1197 | case OP_VEC: |
a0d0e21e | 1198 | case OP_SUBSTR: |
11343788 MB |
1199 | pad_free(o->op_targ); |
1200 | o->op_targ = pad_alloc(o->op_type, SVs_PADMY); | |
5dc0d613 | 1201 | assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL); |
11343788 MB |
1202 | if (o->op_flags & OPf_KIDS) |
1203 | mod(cBINOPo->op_first->op_sibling, type); | |
463ee0b2 | 1204 | break; |
a0d0e21e | 1205 | |
463ee0b2 LW |
1206 | case OP_AELEM: |
1207 | case OP_HELEM: | |
11343788 | 1208 | ref(cBINOPo->op_first, o->op_type); |
68dc0745 | 1209 | if (type == OP_ENTERSUB && |
5dc0d613 MB |
1210 | !(o->op_private & (OPpLVAL_INTRO | OPpDEREF))) |
1211 | o->op_private |= OPpLVAL_DEFER; | |
a0d0e21e | 1212 | modcount++; |
463ee0b2 LW |
1213 | break; |
1214 | ||
1215 | case OP_SCOPE: | |
1216 | case OP_LEAVE: | |
1217 | case OP_ENTER: | |
11343788 MB |
1218 | if (o->op_flags & OPf_KIDS) |
1219 | mod(cLISTOPo->op_last, type); | |
a0d0e21e LW |
1220 | break; |
1221 | ||
1222 | case OP_NULL: | |
11343788 | 1223 | if (!(o->op_flags & OPf_KIDS)) |
463ee0b2 | 1224 | break; |
11343788 MB |
1225 | if (o->op_targ != OP_LIST) { |
1226 | mod(cBINOPo->op_first, type); | |
a0d0e21e LW |
1227 | break; |
1228 | } | |
1229 | /* FALL THROUGH */ | |
463ee0b2 | 1230 | case OP_LIST: |
11343788 | 1231 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
463ee0b2 LW |
1232 | mod(kid, type); |
1233 | break; | |
1234 | } | |
11343788 | 1235 | o->op_flags |= OPf_MOD; |
a0d0e21e LW |
1236 | |
1237 | if (type == OP_AASSIGN || type == OP_SASSIGN) | |
11343788 | 1238 | o->op_flags |= OPf_SPECIAL|OPf_REF; |
a0d0e21e | 1239 | else if (!type) { |
11343788 MB |
1240 | o->op_private |= OPpLVAL_INTRO; |
1241 | o->op_flags &= ~OPf_SPECIAL; | |
ca48fea7 | 1242 | hints |= HINT_BLOCK_SCOPE; |
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; | |
924508f0 | 1575 | PUSHMARK(SP); |
3841441e CS |
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)) { |
6ee623d5 | 2423 | dTHR; |
463ee0b2 | 2424 | modcount = 0; |
748a9306 | 2425 | eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ |
463ee0b2 | 2426 | left = mod(left, OP_AASSIGN); |
748a9306 LW |
2427 | if (eval_start) |
2428 | eval_start = 0; | |
2429 | else { | |
a0d0e21e LW |
2430 | op_free(left); |
2431 | op_free(right); | |
2432 | return Nullop; | |
2433 | } | |
11343788 | 2434 | o = newBINOP(OP_AASSIGN, flags, |
8990e307 LW |
2435 | list(force_list(right)), |
2436 | list(force_list(left)) ); | |
11343788 | 2437 | o->op_private = 0 | (flags >> 8); |
a0d0e21e | 2438 | if (!(left->op_private & OPpLVAL_INTRO)) { |
748a9306 | 2439 | static int generation = 100; |
79072805 | 2440 | OP *curop; |
11343788 | 2441 | OP *lastop = o; |
79072805 | 2442 | generation++; |
11343788 | 2443 | for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { |
79072805 LW |
2444 | if (opargs[curop->op_type] & OA_DANGEROUS) { |
2445 | if (curop->op_type == OP_GV) { | |
2446 | GV *gv = ((GVOP*)curop)->op_gv; | |
2447 | if (gv == defgv || SvCUR(gv) == generation) | |
2448 | break; | |
2449 | SvCUR(gv) = generation; | |
2450 | } | |
748a9306 LW |
2451 | else if (curop->op_type == OP_PADSV || |
2452 | curop->op_type == OP_PADAV || | |
2453 | curop->op_type == OP_PADHV || | |
2454 | curop->op_type == OP_PADANY) { | |
2455 | SV **svp = AvARRAY(comppad_name); | |
8e07c86e | 2456 | SV *sv = svp[curop->op_targ]; |
748a9306 LW |
2457 | if (SvCUR(sv) == generation) |
2458 | break; | |
2459 | SvCUR(sv) = generation; /* (SvCUR not used any more) */ | |
2460 | } | |
79072805 LW |
2461 | else if (curop->op_type == OP_RV2CV) |
2462 | break; | |
2463 | else if (curop->op_type == OP_RV2SV || | |
2464 | curop->op_type == OP_RV2AV || | |
2465 | curop->op_type == OP_RV2HV || | |
2466 | curop->op_type == OP_RV2GV) { | |
2467 | if (lastop->op_type != OP_GV) /* funny deref? */ | |
2468 | break; | |
2469 | } | |
2470 | else | |
2471 | break; | |
2472 | } | |
2473 | lastop = curop; | |
2474 | } | |
11343788 MB |
2475 | if (curop != o) |
2476 | o->op_private = OPpASSIGN_COMMON; | |
79072805 | 2477 | } |
c07a80fd | 2478 | if (right && right->op_type == OP_SPLIT) { |
2479 | OP* tmpop; | |
2480 | if ((tmpop = ((LISTOP*)right)->op_first) && | |
2481 | tmpop->op_type == OP_PUSHRE) | |
2482 | { | |
2483 | PMOP *pm = (PMOP*)tmpop; | |
2484 | if (left->op_type == OP_RV2AV && | |
2485 | !(left->op_private & OPpLVAL_INTRO) && | |
11343788 | 2486 | !(o->op_private & OPpASSIGN_COMMON) ) |
c07a80fd | 2487 | { |
2488 | tmpop = ((UNOP*)left)->op_first; | |
2489 | if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) { | |
2490 | pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv; | |
2491 | pm->op_pmflags |= PMf_ONCE; | |
11343788 | 2492 | tmpop = cUNOPo->op_first; /* to list (nulled) */ |
c07a80fd | 2493 | tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */ |
2494 | tmpop->op_sibling = Nullop; /* don't free split */ | |
2495 | right->op_next = tmpop->op_next; /* fix starting loc */ | |
11343788 | 2496 | op_free(o); /* blow off assign */ |
54310121 | 2497 | right->op_flags &= ~OPf_WANT; |
a5f75d66 | 2498 | /* "I don't know and I don't care." */ |
c07a80fd | 2499 | return right; |
2500 | } | |
2501 | } | |
2502 | else { | |
2503 | if (modcount < 10000 && | |
2504 | ((LISTOP*)right)->op_last->op_type == OP_CONST) | |
2505 | { | |
2506 | SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv; | |
2507 | if (SvIVX(sv) == 0) | |
2508 | sv_setiv(sv, modcount+1); | |
2509 | } | |
2510 | } | |
2511 | } | |
2512 | } | |
11343788 | 2513 | return o; |
79072805 LW |
2514 | } |
2515 | if (!right) | |
2516 | right = newOP(OP_UNDEF, 0); | |
2517 | if (right->op_type == OP_READLINE) { | |
2518 | right->op_flags |= OPf_STACKED; | |
463ee0b2 | 2519 | return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right)); |
79072805 | 2520 | } |
a0d0e21e | 2521 | else { |
748a9306 | 2522 | eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ |
11343788 | 2523 | o = newBINOP(OP_SASSIGN, flags, |
463ee0b2 | 2524 | scalar(right), mod(scalar(left), OP_SASSIGN) ); |
748a9306 LW |
2525 | if (eval_start) |
2526 | eval_start = 0; | |
2527 | else { | |
11343788 | 2528 | op_free(o); |
a0d0e21e LW |
2529 | return Nullop; |
2530 | } | |
2531 | } | |
11343788 | 2532 | return o; |
79072805 LW |
2533 | } |
2534 | ||
2535 | OP * | |
8ac85365 | 2536 | newSTATEOP(I32 flags, char *label, OP *o) |
79072805 | 2537 | { |
11343788 | 2538 | dTHR; |
bbce6d69 | 2539 | U32 seq = intro_my(); |
79072805 LW |
2540 | register COP *cop; |
2541 | ||
2542 | Newz(1101, cop, 1, COP); | |
84902520 | 2543 | if (PERLDB_LINE && curcop->cop_line && curstash != debstash) { |
8990e307 LW |
2544 | cop->op_type = OP_DBSTATE; |
2545 | cop->op_ppaddr = ppaddr[ OP_DBSTATE ]; | |
2546 | } | |
2547 | else { | |
2548 | cop->op_type = OP_NEXTSTATE; | |
2549 | cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ]; | |
2550 | } | |
79072805 | 2551 | cop->op_flags = flags; |
c07a80fd | 2552 | cop->op_private = 0 | (flags >> 8); |
ff0cee69 | 2553 | #ifdef NATIVE_HINTS |
2554 | cop->op_private |= NATIVE_HINTS; | |
2555 | #endif | |
79072805 LW |
2556 | cop->op_next = (OP*)cop; |
2557 | ||
463ee0b2 LW |
2558 | if (label) { |
2559 | cop->cop_label = label; | |
85e6fe83 | 2560 | hints |= HINT_BLOCK_SCOPE; |
463ee0b2 | 2561 | } |
bbce6d69 | 2562 | cop->cop_seq = seq; |
a0d0e21e | 2563 | cop->cop_arybase = curcop->cop_arybase; |
79072805 LW |
2564 | |
2565 | if (copline == NOLINE) | |
2566 | cop->cop_line = curcop->cop_line; | |
2567 | else { | |
2568 | cop->cop_line = copline; | |
2569 | copline = NOLINE; | |
2570 | } | |
44a8e56a | 2571 | cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv); |
79072805 LW |
2572 | cop->cop_stash = curstash; |
2573 | ||
84902520 | 2574 | if (PERLDB_LINE && curstash != debstash) { |
93a17b20 LW |
2575 | SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE); |
2576 | if (svp && *svp != &sv_undef && !SvIOK(*svp)) { | |
a0d0e21e | 2577 | (void)SvIOK_on(*svp); |
a5f75d66 | 2578 | SvIVX(*svp) = 1; |
93a17b20 LW |
2579 | SvSTASH(*svp) = (HV*)cop; |
2580 | } | |
2581 | } | |
2582 | ||
11343788 | 2583 | return prepend_elem(OP_LINESEQ, (OP*)cop, o); |
79072805 LW |
2584 | } |
2585 | ||
bbce6d69 | 2586 | /* "Introduce" my variables to visible status. */ |
2587 | U32 | |
8ac85365 | 2588 | intro_my(void) |
bbce6d69 | 2589 | { |
2590 | SV **svp; | |
2591 | SV *sv; | |
2592 | I32 i; | |
2593 | ||
2594 | if (! min_intro_pending) | |
2595 | return cop_seqmax; | |
2596 | ||
2597 | svp = AvARRAY(comppad_name); | |
2598 | for (i = min_intro_pending; i <= max_intro_pending; i++) { | |
2599 | if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) { | |
2600 | SvIVX(sv) = 999999999; /* Don't know scope end yet. */ | |
2601 | SvNVX(sv) = (double)cop_seqmax; | |
2602 | } | |
2603 | } | |
2604 | min_intro_pending = 0; | |
2605 | comppad_name_fill = max_intro_pending; /* Needn't search higher */ | |
2606 | return cop_seqmax++; | |
2607 | } | |
2608 | ||
79072805 | 2609 | OP * |
8ac85365 | 2610 | newLOGOP(I32 type, I32 flags, OP *first, OP *other) |
79072805 | 2611 | { |
883ffac3 CS |
2612 | return new_logop(type, flags, &first, &other); |
2613 | } | |
2614 | ||
2615 | static OP * | |
2616 | new_logop(I32 type, I32 flags, OP** firstp, OP** otherp) | |
2617 | { | |
11343788 | 2618 | dTHR; |
79072805 | 2619 | LOGOP *logop; |
11343788 | 2620 | OP *o; |
883ffac3 CS |
2621 | OP *first = *firstp; |
2622 | OP *other = *otherp; | |
79072805 | 2623 | |
a0d0e21e LW |
2624 | if (type == OP_XOR) /* Not short circuit, but here by precedence. */ |
2625 | return newBINOP(type, flags, scalar(first), scalar(other)); | |
2626 | ||
8990e307 | 2627 | scalarboolean(first); |
79072805 LW |
2628 | /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */ |
2629 | if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) { | |
2630 | if (type == OP_AND || type == OP_OR) { | |
2631 | if (type == OP_AND) | |
2632 | type = OP_OR; | |
2633 | else | |
2634 | type = OP_AND; | |
11343788 | 2635 | o = first; |
883ffac3 | 2636 | first = *firstp = cUNOPo->op_first; |
11343788 MB |
2637 | if (o->op_next) |
2638 | first->op_next = o->op_next; | |
2639 | cUNOPo->op_first = Nullop; | |
2640 | op_free(o); | |
79072805 LW |
2641 | } |
2642 | } | |
2643 | if (first->op_type == OP_CONST) { | |
93a17b20 | 2644 | if (dowarn && (first->op_private & OPpCONST_BARE)) |
c07a80fd | 2645 | warn("Probable precedence problem on %s", op_desc[type]); |
79072805 LW |
2646 | if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) { |
2647 | op_free(first); | |
883ffac3 | 2648 | *firstp = Nullop; |
79072805 LW |
2649 | return other; |
2650 | } | |
2651 | else { | |
2652 | op_free(other); | |
883ffac3 | 2653 | *otherp = Nullop; |
79072805 LW |
2654 | return first; |
2655 | } | |
2656 | } | |
2657 | else if (first->op_type == OP_WANTARRAY) { | |
2658 | if (type == OP_AND) | |
2659 | list(other); | |
2660 | else | |
2661 | scalar(other); | |
2662 | } | |
a6006777 | 2663 | else if (dowarn && (first->op_flags & OPf_KIDS)) { |
2664 | OP *k1 = ((UNOP*)first)->op_first; | |
2665 | OP *k2 = k1->op_sibling; | |
2666 | OPCODE warnop = 0; | |
2667 | switch (first->op_type) | |
2668 | { | |
2669 | case OP_NULL: | |
2670 | if (k2 && k2->op_type == OP_READLINE | |
2671 | && (k2->op_flags & OPf_STACKED) | |
2672 | && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV)) | |
2673 | warnop = k2->op_type; | |
2674 | break; | |
2675 | ||
2676 | case OP_SASSIGN: | |
68dc0745 | 2677 | if (k1->op_type == OP_READDIR |
2678 | || k1->op_type == OP_GLOB | |
2679 | || k1->op_type == OP_EACH) | |
a6006777 | 2680 | warnop = k1->op_type; |
2681 | break; | |
2682 | } | |
8ebc5c01 | 2683 | if (warnop) { |
2684 | line_t oldline = curcop->cop_line; | |
2685 | curcop->cop_line = copline; | |
68dc0745 | 2686 | warn("Value of %s%s can be \"0\"; test with defined()", |
2687 | op_desc[warnop], | |
2688 | ((warnop == OP_READLINE || warnop == OP_GLOB) | |
2689 | ? " construct" : "() operator")); | |
2690 | curcop->cop_line = oldline; | |
8ebc5c01 | 2691 | } |
a6006777 | 2692 | } |
79072805 LW |
2693 | |
2694 | if (!other) | |
2695 | return first; | |
2696 | ||
a0d0e21e LW |
2697 | if (type == OP_ANDASSIGN || type == OP_ORASSIGN) |
2698 | other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */ | |
2699 | ||
79072805 LW |
2700 | Newz(1101, logop, 1, LOGOP); |
2701 | ||
2702 | logop->op_type = type; | |
2703 | logop->op_ppaddr = ppaddr[type]; | |
2704 | logop->op_first = first; | |
2705 | logop->op_flags = flags | OPf_KIDS; | |
2706 | logop->op_other = LINKLIST(other); | |
c07a80fd | 2707 | logop->op_private = 1 | (flags >> 8); |
79072805 LW |
2708 | |
2709 | /* establish postfix order */ | |
2710 | logop->op_next = LINKLIST(first); | |
2711 | first->op_next = (OP*)logop; | |
2712 | first->op_sibling = other; | |
2713 | ||
11343788 MB |
2714 | o = newUNOP(OP_NULL, 0, (OP*)logop); |
2715 | other->op_next = o; | |
79072805 | 2716 | |
11343788 | 2717 | return o; |
79072805 LW |
2718 | } |
2719 | ||
2720 | OP * | |
8ac85365 | 2721 | newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop) |
79072805 | 2722 | { |
11343788 | 2723 | dTHR; |
79072805 | 2724 | CONDOP *condop; |
11343788 | 2725 | OP *o; |
79072805 | 2726 | |
b1cb66bf | 2727 | if (!falseop) |
2728 | return newLOGOP(OP_AND, 0, first, trueop); | |
2729 | if (!trueop) | |
2730 | return newLOGOP(OP_OR, 0, first, falseop); | |
79072805 | 2731 | |
8990e307 | 2732 | scalarboolean(first); |
79072805 LW |
2733 | if (first->op_type == OP_CONST) { |
2734 | if (SvTRUE(((SVOP*)first)->op_sv)) { | |
2735 | op_free(first); | |
b1cb66bf | 2736 | op_free(falseop); |
2737 | return trueop; | |
79072805 LW |
2738 | } |
2739 | else { | |
2740 | op_free(first); | |
b1cb66bf | 2741 | op_free(trueop); |
2742 | return falseop; | |
79072805 LW |
2743 | } |
2744 | } | |
2745 | else if (first->op_type == OP_WANTARRAY) { | |
b1cb66bf | 2746 | list(trueop); |
2747 | scalar(falseop); | |
79072805 LW |
2748 | } |
2749 | Newz(1101, condop, 1, CONDOP); | |
2750 | ||
2751 | condop->op_type = OP_COND_EXPR; | |
2752 | condop->op_ppaddr = ppaddr[OP_COND_EXPR]; | |
2753 | condop->op_first = first; | |
2754 | condop->op_flags = flags | OPf_KIDS; | |
b1cb66bf | 2755 | condop->op_true = LINKLIST(trueop); |
2756 | condop->op_false = LINKLIST(falseop); | |
c07a80fd | 2757 | condop->op_private = 1 | (flags >> 8); |
79072805 LW |
2758 | |
2759 | /* establish postfix order */ | |
2760 | condop->op_next = LINKLIST(first); | |
2761 | first->op_next = (OP*)condop; | |
2762 | ||
b1cb66bf | 2763 | first->op_sibling = trueop; |
2764 | trueop->op_sibling = falseop; | |
11343788 | 2765 | o = newUNOP(OP_NULL, 0, (OP*)condop); |
79072805 | 2766 | |
5dc0d613 MB |
2767 | trueop->op_next = o; |
2768 | falseop->op_next = o; | |
79072805 | 2769 | |
11343788 | 2770 | return o; |
79072805 LW |
2771 | } |
2772 | ||
2773 | OP * | |
8ac85365 | 2774 | newRANGE(I32 flags, OP *left, OP *right) |
79072805 | 2775 | { |
b35b2403 | 2776 | dTHR; |
79072805 LW |
2777 | CONDOP *condop; |
2778 | OP *flip; | |
2779 | OP *flop; | |
11343788 | 2780 | OP *o; |
79072805 LW |
2781 | |
2782 | Newz(1101, condop, 1, CONDOP); | |
2783 | ||
2784 | condop->op_type = OP_RANGE; | |
2785 | condop->op_ppaddr = ppaddr[OP_RANGE]; | |
2786 | condop->op_first = left; | |
2787 | condop->op_flags = OPf_KIDS; | |
2788 | condop->op_true = LINKLIST(left); | |
2789 | condop->op_false = LINKLIST(right); | |
c07a80fd | 2790 | condop->op_private = 1 | (flags >> 8); |
79072805 LW |
2791 | |
2792 | left->op_sibling = right; | |
2793 | ||
2794 | condop->op_next = (OP*)condop; | |
2795 | flip = newUNOP(OP_FLIP, flags, (OP*)condop); | |
2796 | flop = newUNOP(OP_FLOP, 0, flip); | |
11343788 | 2797 | o = newUNOP(OP_NULL, 0, flop); |
79072805 LW |
2798 | linklist(flop); |
2799 | ||
2800 | left->op_next = flip; | |
2801 | right->op_next = flop; | |
2802 | ||
ed6116ce | 2803 | condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY); |
79072805 | 2804 | sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV); |
ed6116ce | 2805 | flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY); |
79072805 LW |
2806 | sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV); |
2807 | ||
2808 | flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; | |
2809 | flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; | |
2810 | ||
11343788 | 2811 | flip->op_next = o; |
79072805 | 2812 | if (!flip->op_private || !flop->op_private) |
11343788 | 2813 | linklist(o); /* blow off optimizer unless constant */ |
79072805 | 2814 | |
11343788 | 2815 | return o; |
79072805 LW |
2816 | } |
2817 | ||
2818 | OP * | |
8ac85365 | 2819 | newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block) |
79072805 | 2820 | { |
11343788 | 2821 | dTHR; |
463ee0b2 | 2822 | OP* listop; |
11343788 | 2823 | OP* o; |
463ee0b2 | 2824 | int once = block && block->op_flags & OPf_SPECIAL && |
a0d0e21e | 2825 | (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL); |
93a17b20 | 2826 | |
463ee0b2 LW |
2827 | if (expr) { |
2828 | if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv)) | |
2829 | return block; /* do {} while 0 does once */ | |
fb73857a | 2830 | if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB |
2831 | || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { | |
774d564b | 2832 | expr = newUNOP(OP_DEFINED, 0, |
54b9620d | 2833 | newASSIGNOP(0, newDEFSVOP(), 0, expr) ); |
774d564b | 2834 | } |
463ee0b2 | 2835 | } |
93a17b20 | 2836 | |
8990e307 | 2837 | listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0)); |
883ffac3 | 2838 | o = new_logop(OP_AND, 0, &expr, &listop); |
463ee0b2 | 2839 | |
883ffac3 CS |
2840 | if (listop) |
2841 | ((LISTOP*)listop)->op_last->op_next = LINKLIST(o); | |
79072805 | 2842 | |
11343788 MB |
2843 | if (once && o != listop) |
2844 | o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other; | |
79072805 | 2845 | |
11343788 MB |
2846 | if (o == listop) |
2847 | o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */ | |
748a9306 | 2848 | |
11343788 MB |
2849 | o->op_flags |= flags; |
2850 | o = scope(o); | |
2851 | o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/ | |
2852 | return o; | |
79072805 LW |
2853 | } |
2854 | ||
2855 | OP * | |
8ac85365 | 2856 | newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont) |
79072805 | 2857 | { |
11343788 | 2858 | dTHR; |
79072805 LW |
2859 | OP *redo; |
2860 | OP *next = 0; | |
2861 | OP *listop; | |
11343788 | 2862 | OP *o; |
79072805 LW |
2863 | OP *condop; |
2864 | ||
fb73857a | 2865 | if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB |
2866 | || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) { | |
748a9306 | 2867 | expr = newUNOP(OP_DEFINED, 0, |
54b9620d | 2868 | newASSIGNOP(0, newDEFSVOP(), 0, expr) ); |
748a9306 | 2869 | } |
79072805 LW |
2870 | |
2871 | if (!block) | |
2872 | block = newOP(OP_NULL, 0); | |
2873 | ||
2874 | if (cont) | |
2875 | next = LINKLIST(cont); | |
fb73857a | 2876 | if (expr) { |
79072805 | 2877 | cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0)); |
fb73857a | 2878 | if ((line_t)whileline != NOLINE) { |
2879 | copline = whileline; | |
2880 | cont = append_elem(OP_LINESEQ, cont, | |
2881 | newSTATEOP(0, Nullch, Nullop)); | |
2882 | } | |
2883 | } | |
79072805 | 2884 | |
463ee0b2 | 2885 | listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont); |
79072805 LW |
2886 | redo = LINKLIST(listop); |
2887 | ||
2888 | if (expr) { | |
883ffac3 CS |
2889 | copline = whileline; |
2890 | scalar(listop); | |
2891 | o = new_logop(OP_AND, 0, &expr, &listop); | |
11343788 | 2892 | if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) { |
85e6fe83 | 2893 | op_free(expr); /* oops, it's a while (0) */ |
463ee0b2 | 2894 | op_free((OP*)loop); |
883ffac3 | 2895 | return Nullop; /* listop already freed by new_logop */ |
463ee0b2 | 2896 | } |
883ffac3 CS |
2897 | if (listop) |
2898 | ((LISTOP*)listop)->op_last->op_next = condop = | |
2899 | (o == listop ? redo : LINKLIST(o)); | |
79072805 LW |
2900 | if (!next) |
2901 | next = condop; | |
2902 | } | |
2903 | else | |
11343788 | 2904 | o = listop; |
79072805 LW |
2905 | |
2906 | if (!loop) { | |
2907 | Newz(1101,loop,1,LOOP); | |
2908 | loop->op_type = OP_ENTERLOOP; | |
2909 | loop->op_ppaddr = ppaddr[OP_ENTERLOOP]; | |
2910 | loop->op_private = 0; | |
2911 | loop->op_next = (OP*)loop; | |
2912 | } | |
2913 | ||
11343788 | 2914 | o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o); |
79072805 LW |
2915 | |
2916 | loop->op_redoop = redo; | |
11343788 | 2917 | loop->op_lastop = o; |
79072805 LW |
2918 | |
2919 | if (next) | |
2920 | loop->op_nextop = next; | |
2921 | else | |
11343788 | 2922 | loop->op_nextop = o; |
79072805 | 2923 | |
11343788 MB |
2924 | o->op_flags |= flags; |
2925 | o->op_private |= (flags >> 8); | |
2926 | return o; | |
79072805 LW |
2927 | } |
2928 | ||
2929 | OP * | |
8990e307 | 2930 | newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont) |
79072805 LW |
2931 | { |
2932 | LOOP *loop; | |
fb73857a | 2933 | OP *wop; |
85e6fe83 | 2934 | int padoff = 0; |
4633a7c4 | 2935 | I32 iterflags = 0; |
79072805 | 2936 | |
79072805 | 2937 | if (sv) { |
85e6fe83 | 2938 | if (sv->op_type == OP_RV2SV) { /* symbol table variable */ |
748a9306 LW |
2939 | sv->op_type = OP_RV2GV; |
2940 | sv->op_ppaddr = ppaddr[OP_RV2GV]; | |
79072805 | 2941 | } |
85e6fe83 LW |
2942 | else if (sv->op_type == OP_PADSV) { /* private variable */ |
2943 | padoff = sv->op_targ; | |
2944 | op_free(sv); | |
2945 | sv = Nullop; | |
2946 | } | |
54b9620d MB |
2947 | else if (sv->op_type == OP_THREADSV) { /* per-thread variable */ |
2948 | padoff = sv->op_targ; | |
2949 | iterflags |= OPf_SPECIAL; | |
2950 | op_free(sv); | |
2951 | sv = Nullop; | |
2952 | } | |
79072805 | 2953 | else |
c07a80fd | 2954 | croak("Can't use %s for loop variable", op_desc[sv->op_type]); |
79072805 LW |
2955 | } |
2956 | else { | |
54b9620d MB |
2957 | #ifdef USE_THREADS |
2958 | padoff = find_threadsv("_"); | |
2959 | iterflags |= OPf_SPECIAL; | |
2960 | #else | |
79072805 | 2961 | sv = newGVOP(OP_GV, 0, defgv); |
54b9620d | 2962 | #endif |
79072805 | 2963 | } |
5f05dabc | 2964 | if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) { |
4633a7c4 LW |
2965 | expr = scalar(ref(expr, OP_ITER)); |
2966 | iterflags |= OPf_STACKED; | |
2967 | } | |
2968 | loop = (LOOP*)list(convert(OP_ENTERITER, iterflags, | |
a0d0e21e LW |
2969 | append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART), |
2970 | scalar(sv)))); | |
85e6fe83 LW |
2971 | assert(!loop->op_next); |
2972 | Renew(loop, 1, LOOP); | |
2973 | loop->op_targ = padoff; | |
fb73857a | 2974 | wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont); |
2975 | copline = forline; | |
2976 | return newSTATEOP(0, label, wop); | |
79072805 LW |
2977 | } |
2978 | ||
8990e307 | 2979 | OP* |
8ac85365 | 2980 | newLOOPEX(I32 type, OP *label) |
8990e307 | 2981 | { |
11343788 MB |
2982 | dTHR; |
2983 | OP *o; | |
8990e307 | 2984 | if (type != OP_GOTO || label->op_type == OP_CONST) { |
cdaebead MB |
2985 | /* "last()" means "last" */ |
2986 | if (label->op_type == OP_STUB && (label->op_flags & OPf_PARENS)) | |
2987 | o = newOP(type, OPf_SPECIAL); | |
2988 | else { | |
2989 | o = newPVOP(type, 0, savepv(label->op_type == OP_CONST | |
2990 | ? SvPVx(((SVOP*)label)->op_sv, na) | |
2991 | : "")); | |
2992 | } | |
8990e307 LW |
2993 | op_free(label); |
2994 | } | |
2995 | else { | |
a0d0e21e LW |
2996 | if (label->op_type == OP_ENTERSUB) |
2997 | label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN)); | |
11343788 | 2998 | o = newUNOP(type, OPf_STACKED, label); |
8990e307 | 2999 | } |
85e6fe83 | 3000 | hints |= HINT_BLOCK_SCOPE; |
11343788 | 3001 | return o; |
8990e307 LW |
3002 | } |
3003 | ||
79072805 | 3004 | void |
8ac85365 | 3005 | cv_undef(CV *cv) |
79072805 | 3006 | { |
11343788 MB |
3007 | dTHR; |
3008 | #ifdef USE_THREADS | |
e858de61 MB |
3009 | if (CvMUTEXP(cv)) { |
3010 | MUTEX_DESTROY(CvMUTEXP(cv)); | |
3011 | Safefree(CvMUTEXP(cv)); | |
3012 | CvMUTEXP(cv) = 0; | |
3013 | } | |
11343788 MB |
3014 | #endif /* USE_THREADS */ |
3015 | ||
a0d0e21e | 3016 | if (!CvXSUB(cv) && CvROOT(cv)) { |
11343788 MB |
3017 | #ifdef USE_THREADS |
3018 | if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr)) | |
3019 | croak("Can't undef active subroutine"); | |
3020 | #else | |
a0d0e21e LW |
3021 | if (CvDEPTH(cv)) |
3022 | croak("Can't undef active subroutine"); | |
11343788 | 3023 | #endif /* USE_THREADS */ |
8990e307 | 3024 | ENTER; |
a0d0e21e LW |
3025 | |
3026 | SAVESPTR(curpad); | |
3027 | curpad = 0; | |
3028 | ||
a5f75d66 | 3029 | if (!CvCLONED(cv)) |
748a9306 | 3030 | op_free(CvROOT(cv)); |
79072805 | 3031 | CvROOT(cv) = Nullop; |
8990e307 | 3032 | LEAVE; |
79072805 | 3033 | } |
1d5db326 | 3034 | SvPOK_off((SV*)cv); /* forget prototype */ |
44a8e56a | 3035 | CvFLAGS(cv) = 0; |
8e07c86e AD |
3036 | SvREFCNT_dec(CvGV(cv)); |
3037 | CvGV(cv) = Nullgv; | |
3038 | SvREFCNT_dec(CvOUTSIDE(cv)); | |
3039 | CvOUTSIDE(cv) = Nullcv; | |
3040 | if (CvPADLIST(cv)) { | |
8ebc5c01 | 3041 | /* may be during global destruction */ |
3042 | if (SvREFCNT(CvPADLIST(cv))) { | |
93965878 | 3043 | I32 i = AvFILLp(CvPADLIST(cv)); |
8ebc5c01 | 3044 | while (i >= 0) { |
3045 | SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE); | |
46fc3d4c | 3046 | SV* sv = svp ? *svp : Nullsv; |
3047 | if (!sv) | |
3048 | continue; | |
3049 | if (sv == (SV*)comppad_name) | |
3050 | comppad_name = Nullav; | |
3051 | else if (sv == (SV*)comppad) { | |
3052 | comppad = Nullav; | |
3053 | curpad = Null(SV**); | |
3054 | } | |
3055 | SvREFCNT_dec(sv); | |
8ebc5c01 | 3056 | } |
3057 | SvREFCNT_dec((SV*)CvPADLIST(cv)); | |
8e07c86e | 3058 | } |
8e07c86e AD |
3059 | CvPADLIST(cv) = Nullav; |
3060 | } | |
79072805 LW |
3061 | } |
3062 | ||
5f05dabc | 3063 | #ifdef DEBUG_CLOSURES |
3064 | static void | |
3065 | cv_dump(cv) | |
3066 | CV* cv; | |
3067 | { | |
3068 | CV *outside = CvOUTSIDE(cv); | |
3069 | AV* padlist = CvPADLIST(cv); | |
4fdae800 | 3070 | AV* pad_name; |
3071 | AV* pad; | |
3072 | SV** pname; | |
3073 | SV** ppad; | |
5f05dabc | 3074 | I32 ix; |
3075 | ||
fb73857a | 3076 | PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n", |
ab50184a CS |
3077 | cv, |
3078 | (CvANON(cv) ? "ANON" | |
3079 | : (cv == main_cv) ? "MAIN" | |
07055b4c | 3080 | : CvUNIQUE(outside) ? "UNIQUE" |
44a8e56a | 3081 | : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"), |
ab50184a CS |
3082 | outside, |
3083 | (!outside ? "null" | |
3084 | : CvANON(outside) ? "ANON" | |
3085 | : (outside == main_cv) ? "MAIN" | |
07055b4c | 3086 | : CvUNIQUE(outside) ? "UNIQUE" |
44a8e56a | 3087 | : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED")); |
5f05dabc | 3088 | |
4fdae800 | 3089 | if (!padlist) |
3090 | return; | |
3091 | ||
3092 | pad_name = (AV*)*av_fetch(padlist, 0, FALSE); | |
3093 | pad = (AV*)*av_fetch(padlist, 1, FALSE); | |
3094 | pname = AvARRAY(pad_name); | |
3095 | ppad = AvARRAY(pad); | |
3096 | ||
93965878 | 3097 | for (ix = 1; ix <= AvFILLp(pad_name); ix++) { |
5f05dabc | 3098 | if (SvPOK(pname[ix])) |
fb73857a | 3099 | PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n", |
4fdae800 | 3100 | ix, ppad[ix], |
3101 | SvFAKE(pname[ix]) ? "FAKE " : "", | |
3102 | SvPVX(pname[ix]), | |
ab50184a CS |
3103 | (long)I_32(SvNVX(pname[ix])), |
3104 | (long)SvIVX(pname[ix])); | |
5f05dabc | 3105 | } |
3106 | } | |
3107 | #endif /* DEBUG_CLOSURES */ | |
3108 | ||
3109 | static CV * | |
8ac85365 | 3110 | cv_clone2(CV *proto, CV *outside) |
748a9306 | 3111 | { |
11343788 | 3112 | dTHR; |
748a9306 LW |
3113 | AV* av; |
3114 | I32 ix; | |
3115 | AV* protopadlist = CvPADLIST(proto); | |
3116 | AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE); | |
3117 | AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE); | |
5f05dabc | 3118 | SV** pname = AvARRAY(protopad_name); |
3119 | SV** ppad = AvARRAY(protopad); | |
93965878 NIS |
3120 | I32 fname = AvFILLp(protopad_name); |
3121 | I32 fpad = AvFILLp(protopad); | |
748a9306 LW |
3122 | AV* comppadlist; |
3123 | CV* cv; | |
3124 | ||
07055b4c CS |
3125 | assert(!CvUNIQUE(proto)); |
3126 | ||
748a9306 LW |
3127 | ENTER; |
3128 | SAVESPTR(curpad); | |
3129 | SAVESPTR(comppad); | |
46fc3d4c | 3130 | SAVESPTR(comppad_name); |
748a9306 LW |
3131 | SAVESPTR(compcv); |
3132 | ||
3133 | cv = compcv = (CV*)NEWSV(1104,0); | |
fa83b5b6 | 3134 | sv_upgrade((SV *)cv, SvTYPE(proto)); |
a5f75d66 | 3135 | CvCLONED_on(cv); |
5f05dabc | 3136 | if (CvANON(proto)) |
3137 | CvANON_on(cv); | |
748a9306 | 3138 | |
11343788 | 3139 | #ifdef USE_THREADS |
12ca11f6 | 3140 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3141 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 MB |
3142 | CvOWNER(cv) = 0; |
3143 | #endif /* USE_THREADS */ | |
748a9306 | 3144 | CvFILEGV(cv) = CvFILEGV(proto); |
44a8e56a | 3145 | CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto)); |
748a9306 LW |
3146 | CvSTASH(cv) = CvSTASH(proto); |
3147 | CvROOT(cv) = CvROOT(proto); | |
3148 | CvSTART(cv) = CvSTART(proto); | |
5f05dabc | 3149 | if (outside) |
3150 | CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside); | |
748a9306 | 3151 | |
68dc0745 | 3152 | if (SvPOK(proto)) |
3153 | sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto)); | |
3154 | ||
46fc3d4c | 3155 | comppad_name = newAV(); |
3156 | for (ix = fname; ix >= 0; ix--) | |
3157 | av_store(comppad_name, ix, SvREFCNT_inc(pname[ix])); | |
748a9306 LW |
3158 | |
3159 | comppad = newAV(); | |
3160 | ||
3161 | comppadlist = newAV(); | |
3162 | AvREAL_off(comppadlist); | |
46fc3d4c | 3163 | av_store(comppadlist, 0, (SV*)comppad_name); |
b355b4e0 | 3164 | av_store(comppadlist, 1, (SV*)comppad); |
748a9306 | 3165 | CvPADLIST(cv) = comppadlist; |
93965878 | 3166 | av_fill(comppad, AvFILLp(protopad)); |
748a9306 LW |
3167 | curpad = AvARRAY(comppad); |
3168 | ||
3169 | av = newAV(); /* will be @_ */ | |
3170 | av_extend(av, 0); | |
3171 | av_store(comppad, 0, (SV*)av); | |
3172 | AvFLAGS(av) = AVf_REIFY; | |
3173 | ||
9607fc9c | 3174 | for (ix = fpad; ix > 0; ix--) { |
3175 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; | |
aa689395 | 3176 | if (namesv && namesv != &sv_undef) { |
3177 | char *name = SvPVX(namesv); /* XXX */ | |
3178 | if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */ | |
3179 | I32 off = pad_findlex(name, ix, SvIVX(namesv), | |
5f05dabc | 3180 | CvOUTSIDE(cv), cxstack_ix); |
3181 | if (!off) | |
3182 | curpad[ix] = SvREFCNT_inc(ppad[ix]); | |
3183 | else if (off != ix) | |
748a9306 LW |
3184 | croak("panic: cv_clone: %s", name); |
3185 | } | |
3186 | else { /* our own lexical */ | |
aa689395 | 3187 | SV* sv; |
5f05dabc | 3188 | if (*name == '&') { |
3189 | /* anon code -- we'll come back for it */ | |
3190 | sv = SvREFCNT_inc(ppad[ix]); | |
3191 | } | |
3192 | else if (*name == '@') | |
3193 | sv = (SV*)newAV(); | |
748a9306 | 3194 | else if (*name == '%') |
5f05dabc | 3195 | sv = (SV*)newHV(); |
748a9306 | 3196 | else |
5f05dabc | 3197 | sv = NEWSV(0,0); |
3198 | if (!SvPADBUSY(sv)) | |
3199 | SvPADMY_on(sv); | |
3200 | curpad[ix] = sv; | |
748a9306 LW |
3201 | } |
3202 | } | |
3203 | else { | |
aa689395 | 3204 | SV* sv = NEWSV(0,0); |
748a9306 | 3205 | SvPADTMP_on(sv); |
5f05dabc | 3206 | curpad[ix] = sv; |
748a9306 LW |
3207 | } |
3208 | } | |
3209 | ||
5f05dabc | 3210 | /* Now that vars are all in place, clone nested closures. */ |
3211 | ||
9607fc9c | 3212 | for (ix = fpad; ix > 0; ix--) { |
3213 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; | |
aa689395 | 3214 | if (namesv |
3215 | && namesv != &sv_undef | |
3216 | && !(SvFLAGS(namesv) & SVf_FAKE) | |
3217 | && *SvPVX(namesv) == '&' | |
5f05dabc | 3218 | && CvCLONE(ppad[ix])) |
3219 | { | |
3220 | CV *kid = cv_clone2((CV*)ppad[ix], cv); | |
3221 | SvREFCNT_dec(ppad[ix]); | |
3222 | CvCLONE_on(kid); | |
3223 | SvPADMY_on(kid); | |
3224 | curpad[ix] = (SV*)kid; | |
748a9306 LW |
3225 | } |
3226 | } | |
3227 | ||
5f05dabc | 3228 | #ifdef DEBUG_CLOSURES |
ab50184a CS |
3229 | PerlIO_printf(Perl_debug_log, "Cloned inside:\n"); |
3230 | cv_dump(outside); | |
3231 | PerlIO_printf(Perl_debug_log, " from:\n"); | |
5f05dabc | 3232 | cv_dump(proto); |
ab50184a | 3233 | PerlIO_printf(Perl_debug_log, " to:\n"); |
5f05dabc | 3234 | cv_dump(cv); |
3235 | #endif | |
3236 | ||
748a9306 LW |
3237 | LEAVE; |
3238 | return cv; | |
3239 | } | |
3240 | ||
3241 | CV * | |
8ac85365 | 3242 | cv_clone(CV *proto) |
5f05dabc | 3243 | { |
3244 | return cv_clone2(proto, CvOUTSIDE(proto)); | |
3245 | } | |
3246 | ||
3fe9a6f1 | 3247 | void |
8ac85365 | 3248 | cv_ckproto(CV *cv, GV *gv, char *p) |
3fe9a6f1 | 3249 | { |
3250 | if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) { | |
46fc3d4c | 3251 | SV* msg = sv_newmortal(); |
3fe9a6f1 | 3252 | SV* name = Nullsv; |
3253 | ||
3254 | if (gv) | |
46fc3d4c | 3255 | gv_efullname3(name = sv_newmortal(), gv, Nullch); |
3256 | sv_setpv(msg, "Prototype mismatch:"); | |
3257 | if (name) | |
fc36a67e | 3258 | sv_catpvf(msg, " sub %_", name); |
3fe9a6f1 | 3259 | if (SvPOK(cv)) |
46fc3d4c | 3260 | sv_catpvf(msg, " (%s)", SvPVX(cv)); |
3261 | sv_catpv(msg, " vs "); | |
3262 | if (p) | |
3263 | sv_catpvf(msg, "(%s)", p); | |
3264 | else | |
3265 | sv_catpv(msg, "none"); | |
fc36a67e | 3266 | warn("%_", msg); |
3fe9a6f1 | 3267 | } |
3268 | } | |
3269 | ||
760ac839 | 3270 | SV * |
8ac85365 | 3271 | cv_const_sv(CV *cv) |
760ac839 LW |
3272 | { |
3273 | OP *o; | |
54310121 | 3274 | SV *sv; |
aeea060c | 3275 | |
54310121 | 3276 | if (!cv || !SvPOK(cv) || SvCUR(cv)) |
3277 | return Nullsv; | |
760ac839 | 3278 | |
54310121 | 3279 | sv = Nullsv; |
3280 | for (o = CvSTART(cv); o; o = o->op_next) { | |
3281 | OPCODE type = o->op_type; | |
3282 | ||
3283 | if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK) | |
3284 | continue; | |
3285 | if (type == OP_LEAVESUB || type == OP_RETURN) | |
3286 | break; | |
3287 | if (sv) | |
3288 | return Nullsv; | |
3289 | if (type == OP_CONST) | |
5dc0d613 | 3290 | sv = cSVOPo->op_sv; |
54310121 | 3291 | else if (type == OP_PADSV) { |
e858de61 MB |
3292 | AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]); |
3293 | sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv; | |
5aabfad6 | 3294 | if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1)) |
54310121 | 3295 | return Nullsv; |
760ac839 | 3296 | } |
54310121 | 3297 | else |
3298 | return Nullsv; | |
760ac839 | 3299 | } |
5aabfad6 | 3300 | if (sv) |
3301 | SvREADONLY_on(sv); | |
760ac839 LW |
3302 | return sv; |
3303 | } | |
3304 | ||
748a9306 | 3305 | CV * |
8ac85365 | 3306 | newSUB(I32 floor, OP *o, OP *proto, OP *block) |
79072805 | 3307 | { |
11343788 | 3308 | dTHR; |
5dc0d613 | 3309 | char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch; |
44a8e56a | 3310 | GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV); |
3fe9a6f1 | 3311 | char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch; |
79072805 | 3312 | register CV *cv; |
a0d0e21e | 3313 | I32 ix; |
79072805 | 3314 | |
11343788 | 3315 | if (o) |
5dc0d613 | 3316 | SAVEFREEOP(o); |
3fe9a6f1 | 3317 | if (proto) |
3318 | SAVEFREEOP(proto); | |
3319 | ||
68dc0745 | 3320 | if (!name || GvCVGEN(gv)) |
3321 | cv = Nullcv; | |
3322 | else if (cv = GvCV(gv)) { | |
3fe9a6f1 | 3323 | cv_ckproto(cv, gv, ps); |
68dc0745 | 3324 | /* already defined (or promised)? */ |
3325 | if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { | |
3326 | SV* const_sv; | |
aa689395 | 3327 | if (!block) { |
3328 | /* just a "sub foo;" when &foo is already defined */ | |
3329 | SAVEFREESV(compcv); | |
3330 | goto done; | |
3331 | } | |
7bac28a0 | 3332 | /* ahem, death to those who redefine active sort subs */ |
e336de0d | 3333 | if (curstackinfo->si_type == SI_SORT && sortcop == CvSTART(cv)) |
7bac28a0 | 3334 | croak("Can't redefine active sort subroutine %s", name); |
68dc0745 | 3335 | const_sv = cv_const_sv(cv); |
2f34f9d4 IZ |
3336 | if (const_sv || dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv)) |
3337 | && HvNAME(GvSTASH(CvGV(cv))) | |
3338 | && strEQ(HvNAME(GvSTASH(CvGV(cv))), | |
3339 | "autouse"))) { | |
79072805 | 3340 | line_t oldline = curcop->cop_line; |
79072805 | 3341 | curcop->cop_line = copline; |
760ac839 | 3342 | warn(const_sv ? "Constant subroutine %s redefined" |
68dc0745 | 3343 | : "Subroutine %s redefined", name); |
79072805 LW |
3344 | curcop->cop_line = oldline; |
3345 | } | |
8990e307 | 3346 | SvREFCNT_dec(cv); |
68dc0745 | 3347 | cv = Nullcv; |
79072805 LW |
3348 | } |
3349 | } | |
a0d0e21e | 3350 | if (cv) { /* must reuse cv if autoloaded */ |
4633a7c4 | 3351 | cv_undef(cv); |
44a8e56a | 3352 | CvFLAGS(cv) = CvFLAGS(compcv); |
748a9306 | 3353 | CvOUTSIDE(cv) = CvOUTSIDE(compcv); |
e9a444f0 | 3354 | CvOUTSIDE(compcv) = 0; |
748a9306 | 3355 | CvPADLIST(cv) = CvPADLIST(compcv); |
4aa0a1f7 | 3356 | CvPADLIST(compcv) = 0; |
4633a7c4 LW |
3357 | if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */ |
3358 | CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv); | |
748a9306 | 3359 | SvREFCNT_dec(compcv); |
a0d0e21e LW |
3360 | } |
3361 | else { | |
748a9306 | 3362 | cv = compcv; |
44a8e56a | 3363 | if (name) { |
3364 | GvCV(gv) = cv; | |
3365 | GvCVGEN(gv) = 0; | |
3366 | sub_generation++; | |
3367 | } | |
a0d0e21e | 3368 | } |
44a8e56a | 3369 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
79072805 | 3370 | CvFILEGV(cv) = curcop->cop_filegv; |
8990e307 | 3371 | CvSTASH(cv) = curstash; |
11343788 MB |
3372 | #ifdef USE_THREADS |
3373 | CvOWNER(cv) = 0; | |
12ca11f6 | 3374 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3375 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 | 3376 | #endif /* USE_THREADS */ |
8990e307 | 3377 | |
3fe9a6f1 | 3378 | if (ps) |
3379 | sv_setpv((SV*)cv, ps); | |
4633a7c4 | 3380 | |
c07a80fd | 3381 | if (error_count) { |
3382 | op_free(block); | |
3383 | block = Nullop; | |
68dc0745 | 3384 | if (name) { |
3385 | char *s = strrchr(name, ':'); | |
3386 | s = s ? s+1 : name; | |
6d4c2119 CS |
3387 | if (strEQ(s, "BEGIN")) { |
3388 | char *not_safe = | |
3389 | "BEGIN not safe after errors--compilation aborted"; | |
3390 | if (in_eval & 4) | |
3391 | croak(not_safe); | |
3392 | else { | |
3393 | /* force display of errors found but not reported */ | |
38a03e6e MB |
3394 | sv_catpv(ERRSV, not_safe); |
3395 | croak("%s", SvPVx(ERRSV, na)); | |
6d4c2119 CS |
3396 | } |
3397 | } | |
68dc0745 | 3398 | } |
c07a80fd | 3399 | } |
a0d0e21e | 3400 | if (!block) { |
a0d0e21e LW |
3401 | copline = NOLINE; |
3402 | LEAVE_SCOPE(floor); | |
3403 | return cv; | |
3404 | } | |
3405 | ||
93965878 NIS |
3406 | if (AvFILLp(comppad_name) < AvFILLp(comppad)) |
3407 | av_store(comppad_name, AvFILLp(comppad), Nullsv); | |
a0d0e21e | 3408 | |
54310121 | 3409 | if (CvCLONE(cv)) { |
3410 | SV **namep = AvARRAY(comppad_name); | |
93965878 | 3411 | for (ix = AvFILLp(comppad); ix > 0; ix--) { |
54310121 | 3412 | SV *namesv; |
3413 | ||
3414 | if (SvIMMORTAL(curpad[ix])) | |
3415 | continue; | |
3416 | /* | |
3417 | * The only things that a clonable function needs in its | |
3418 | * pad are references to outer lexicals and anonymous subs. | |
3419 | * The rest are created anew during cloning. | |
3420 | */ | |
3421 | if (!((namesv = namep[ix]) != Nullsv && | |
3422 | namesv != &sv_undef && | |
3423 | (SvFAKE(namesv) || | |
3424 | *SvPVX(namesv) == '&'))) | |
3425 | { | |
3426 | SvREFCNT_dec(curpad[ix]); | |
3427 | curpad[ix] = Nullsv; | |
3428 | } | |
3429 | } | |
a0d0e21e | 3430 | } |
54310121 | 3431 | else { |
3432 | AV *av = newAV(); /* Will be @_ */ | |
3433 | av_extend(av, 0); | |
3434 | av_store(comppad, 0, (SV*)av); | |
3435 | AvFLAGS(av) = AVf_REIFY; | |
79072805 | 3436 | |
93965878 | 3437 | for (ix = AvFILLp(comppad); ix > 0; ix--) { |
54310121 | 3438 | if (SvIMMORTAL(curpad[ix])) |
3439 | continue; | |
3440 | if (!SvPADMY(curpad[ix])) | |
3441 | SvPADTMP_on(curpad[ix]); | |
3442 | } | |
3443 | } | |
79072805 | 3444 | |
a0d0e21e | 3445 | CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block)); |
79072805 LW |
3446 | CvSTART(cv) = LINKLIST(CvROOT(cv)); |
3447 | CvROOT(cv)->op_next = 0; | |
3448 | peep(CvSTART(cv)); | |
93a17b20 | 3449 | |
44a8e56a | 3450 | if (name) { |
3451 | char *s; | |
3452 | ||
84902520 | 3453 | if (PERLDB_SUBLINE && curstash != debstash) { |
46fc3d4c | 3454 | SV *sv = NEWSV(0,0); |
44a8e56a | 3455 | SV *tmpstr = sv_newmortal(); |
549bb64a | 3456 | GV *db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV); |
44a8e56a | 3457 | CV *cv; |
3458 | HV *hv; | |
3459 | ||
51790459 MB |
3460 | sv_setpvf(sv, "%_:%ld-%ld", |
3461 | GvSV(curcop->cop_filegv), | |
3462 | (long)subline, (long)curcop->cop_line); | |
44a8e56a | 3463 | gv_efullname3(tmpstr, gv, Nullch); |
3464 | hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0); | |
44a8e56a | 3465 | hv = GvHVn(db_postponed); |
9607fc9c | 3466 | if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr)) |
3467 | && (cv = GvCV(db_postponed))) { | |
44a8e56a | 3468 | dSP; |
924508f0 | 3469 | PUSHMARK(SP); |
44a8e56a | 3470 | XPUSHs(tmpstr); |
3471 | PUTBACK; | |
3472 | perl_call_sv((SV*)cv, G_DISCARD); | |
3473 | } | |
3474 | } | |
79072805 | 3475 | |
44a8e56a | 3476 | if ((s = strrchr(name,':'))) |
28757baa | 3477 | s++; |
3478 | else | |
3479 | s = name; | |
68dc0745 | 3480 | if (strEQ(s, "BEGIN")) { |
2ae324a7 | 3481 | I32 oldscope = scopestack_ix; |
28757baa | 3482 | ENTER; |
3483 | SAVESPTR(compiling.cop_filegv); | |
3484 | SAVEI16(compiling.cop_line); | |
28757baa | 3485 | save_svref(&rs); |
3486 | sv_setsv(rs, nrs); | |
3487 | ||
3488 | if (!beginav) | |
3489 | beginav = newAV(); | |
3490 | DEBUG_x( dump_sub(gv) ); | |
3491 | av_push(beginav, (SV *)cv); | |
3492 | GvCV(gv) = 0; | |
68dc0745 | 3493 | call_list(oldscope, beginav); |
a6006777 | 3494 | |
28757baa | 3495 | curcop = &compiling; |
3496 | LEAVE; | |
3497 | } | |
3498 | else if (strEQ(s, "END") && !error_count) { | |
3499 | if (!endav) | |
3500 | endav = newAV(); | |
3501 | av_unshift(endav, 1); | |
3502 | av_store(endav, 0, (SV *)cv); | |
3503 | GvCV(gv) = 0; | |
3504 | } | |
7d07dbc2 MB |
3505 | else if (strEQ(s, "INIT") && !error_count) { |
3506 | if (!initav) | |
3507 | initav = newAV(); | |
3508 | av_push(initav, SvREFCNT_inc(cv)); | |
ae77835f | 3509 | } |
79072805 | 3510 | } |
a6006777 | 3511 | |
aa689395 | 3512 | done: |
79072805 | 3513 | copline = NOLINE; |
8990e307 | 3514 | LEAVE_SCOPE(floor); |
a0d0e21e | 3515 | return cv; |
79072805 LW |
3516 | } |
3517 | ||
5476c433 JD |
3518 | void |
3519 | newCONSTSUB(HV *stash, char *name, SV *sv) | |
3520 | { | |
3521 | dTHR; | |
3522 | U32 oldhints = hints; | |
3523 | HV *old_cop_stash = curcop->cop_stash; | |
3524 | HV *old_curstash = curstash; | |
3525 | line_t oldline = curcop->cop_line; | |
3526 | curcop->cop_line = copline; | |
3527 | ||
3528 | hints &= ~HINT_BLOCK_SCOPE; | |
3529 | if(stash) | |
3530 | curstash = curcop->cop_stash = stash; | |
3531 | ||
3532 | newSUB( | |
be24f278 | 3533 | start_subparse(FALSE, 0), |
5476c433 JD |
3534 | newSVOP(OP_CONST, 0, newSVpv(name,0)), |
3535 | newSVOP(OP_CONST, 0, &sv_no), /* SvPV(&sv_no) == "" -- GMB */ | |
3536 | newSTATEOP(0, Nullch, newSVOP(OP_CONST, 0, sv)) | |
3537 | ); | |
3538 | ||
3539 | hints = oldhints; | |
3540 | curcop->cop_stash = old_cop_stash; | |
3541 | curstash = old_curstash; | |
3542 | curcop->cop_line = oldline; | |
3543 | } | |
3544 | ||
57d3b86d | 3545 | CV * |
aeea060c | 3546 | newXS(char *name, void (*subaddr) (CV *), char *filename) |
a0d0e21e | 3547 | { |
11343788 | 3548 | dTHR; |
44a8e56a | 3549 | GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV); |
79072805 | 3550 | register CV *cv; |
44a8e56a | 3551 | |
3552 | if (cv = (name ? GvCV(gv) : Nullcv)) { | |
3553 | if (GvCVGEN(gv)) { | |
3554 | /* just a cached method */ | |
3555 | SvREFCNT_dec(cv); | |
3556 | cv = 0; | |
3557 | } | |
3558 | else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { | |
3559 | /* already defined (or promised) */ | |
2f34f9d4 IZ |
3560 | if (dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv)) |
3561 | && HvNAME(GvSTASH(CvGV(cv))) | |
3562 | && strEQ(HvNAME(GvSTASH(CvGV(cv))), "autouse"))) { | |
a0d0e21e | 3563 | line_t oldline = curcop->cop_line; |
a0d0e21e LW |
3564 | curcop->cop_line = copline; |
3565 | warn("Subroutine %s redefined",name); | |
3566 | curcop->cop_line = oldline; | |
3567 | } | |
3568 | SvREFCNT_dec(cv); | |
3569 | cv = 0; | |
79072805 | 3570 | } |
79072805 | 3571 | } |
44a8e56a | 3572 | |
3573 | if (cv) /* must reuse cv if autoloaded */ | |
3574 | cv_undef(cv); | |
a0d0e21e LW |
3575 | else { |
3576 | cv = (CV*)NEWSV(1105,0); | |
3577 | sv_upgrade((SV *)cv, SVt_PVCV); | |
44a8e56a | 3578 | if (name) { |
3579 | GvCV(gv) = cv; | |
3580 | GvCVGEN(gv) = 0; | |
3581 | sub_generation++; | |
3582 | } | |
a0d0e21e | 3583 | } |
5196be3e | 3584 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
11343788 | 3585 | #ifdef USE_THREADS |
12ca11f6 | 3586 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3587 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 MB |
3588 | CvOWNER(cv) = 0; |
3589 | #endif /* USE_THREADS */ | |
79072805 | 3590 | CvFILEGV(cv) = gv_fetchfile(filename); |
a0d0e21e | 3591 | CvXSUB(cv) = subaddr; |
44a8e56a | 3592 | |
28757baa | 3593 | if (name) { |
3594 | char *s = strrchr(name,':'); | |
3595 | if (s) | |
3596 | s++; | |
3597 | else | |
3598 | s = name; | |
3599 | if (strEQ(s, "BEGIN")) { | |
3600 | if (!beginav) | |
3601 | beginav = newAV(); | |
44a8e56a | 3602 | av_push(beginav, (SV *)cv); |
3603 | GvCV(gv) = 0; | |
28757baa | 3604 | } |
3605 | else if (strEQ(s, "END")) { | |
3606 | if (!endav) | |
3607 | endav = newAV(); | |
3608 | av_unshift(endav, 1); | |
44a8e56a | 3609 | av_store(endav, 0, (SV *)cv); |
3610 | GvCV(gv) = 0; | |
28757baa | 3611 | } |
7d07dbc2 MB |
3612 | else if (strEQ(s, "INIT")) { |
3613 | if (!initav) | |
3614 | initav = newAV(); | |
3615 | av_push(initav, (SV *)cv); | |
ae77835f | 3616 | } |
28757baa | 3617 | } |
8990e307 | 3618 | else |
a5f75d66 | 3619 | CvANON_on(cv); |
44a8e56a | 3620 | |
a0d0e21e | 3621 | return cv; |
79072805 LW |
3622 | } |
3623 | ||
3624 | void | |
8ac85365 | 3625 | newFORM(I32 floor, OP *o, OP *block) |
79072805 | 3626 | { |
11343788 | 3627 | dTHR; |
79072805 LW |
3628 | register CV *cv; |
3629 | char *name; | |
3630 | GV *gv; | |
a0d0e21e | 3631 | I32 ix; |
79072805 | 3632 | |
11343788 MB |
3633 | if (o) |
3634 | name = SvPVx(cSVOPo->op_sv, na); | |
79072805 LW |
3635 | else |
3636 | name = "STDOUT"; | |
85e6fe83 | 3637 | gv = gv_fetchpv(name,TRUE, SVt_PVFM); |
a5f75d66 | 3638 | GvMULTI_on(gv); |
79072805 LW |
3639 | if (cv = GvFORM(gv)) { |
3640 | if (dowarn) { | |
3641 | line_t oldline = curcop->cop_line; | |
3642 | ||
3643 | curcop->cop_line = copline; | |
3644 | warn("Format %s redefined",name); | |
3645 | curcop->cop_line = oldline; | |
3646 | } | |
8990e307 | 3647 | SvREFCNT_dec(cv); |
79072805 | 3648 | } |
748a9306 | 3649 | cv = compcv; |
79072805 | 3650 | GvFORM(gv) = cv; |
44a8e56a | 3651 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
79072805 LW |
3652 | CvFILEGV(cv) = curcop->cop_filegv; |
3653 | ||
93965878 | 3654 | for (ix = AvFILLp(comppad); ix > 0; ix--) { |
44a8e56a | 3655 | if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix])) |
a0d0e21e LW |
3656 | SvPADTMP_on(curpad[ix]); |
3657 | } | |
3658 | ||
79072805 LW |
3659 | CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block)); |
3660 | CvSTART(cv) = LINKLIST(CvROOT(cv)); | |
3661 | CvROOT(cv)->op_next = 0; | |
3662 | peep(CvSTART(cv)); | |
11343788 | 3663 | op_free(o); |
79072805 | 3664 | copline = NOLINE; |
8990e307 | 3665 | LEAVE_SCOPE(floor); |
79072805 LW |
3666 | } |
3667 | ||
3668 | OP * | |
8ac85365 | 3669 | newANONLIST(OP *o) |
79072805 | 3670 | { |
93a17b20 | 3671 | return newUNOP(OP_REFGEN, 0, |
11343788 | 3672 | mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN)); |
79072805 LW |
3673 | } |
3674 | ||
3675 | OP * | |
8ac85365 | 3676 | newANONHASH(OP *o) |
79072805 | 3677 | { |
93a17b20 | 3678 | return newUNOP(OP_REFGEN, 0, |
11343788 | 3679 | mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN)); |
a0d0e21e LW |
3680 | } |
3681 | ||
3682 | OP * | |
8ac85365 | 3683 | newANONSUB(I32 floor, OP *proto, OP *block) |
a0d0e21e LW |
3684 | { |
3685 | return newUNOP(OP_REFGEN, 0, | |
4633a7c4 | 3686 | newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block))); |
79072805 LW |
3687 | } |
3688 | ||
3689 | OP * | |
8ac85365 | 3690 | oopsAV(OP *o) |
79072805 | 3691 | { |
ed6116ce LW |
3692 | switch (o->op_type) { |
3693 | case OP_PADSV: | |
3694 | o->op_type = OP_PADAV; | |
3695 | o->op_ppaddr = ppaddr[OP_PADAV]; | |
3696 | return ref(newUNOP(OP_RV2AV, 0, scalar(o)), OP_RV2AV); | |
3697 | ||
3698 | case OP_RV2SV: | |
79072805 LW |
3699 | o->op_type = OP_RV2AV; |
3700 | o->op_ppaddr = ppaddr[OP_RV2AV]; | |
3701 | ref(o, OP_RV2AV); | |
ed6116ce LW |
3702 | break; |
3703 | ||
3704 | default: | |
79072805 | 3705 | warn("oops: oopsAV"); |
ed6116ce LW |
3706 | break; |
3707 | } | |
79072805 LW |
3708 | return o; |
3709 | } | |
3710 | ||
3711 | OP * | |
8ac85365 | 3712 | oopsHV(OP *o) |
79072805 | 3713 | { |
ed6116ce LW |
3714 | switch (o->op_type) { |
3715 | case OP_PADSV: | |
3716 | case OP_PADAV: | |
3717 | o->op_type = OP_PADHV; | |
3718 | o->op_ppaddr = ppaddr[OP_PADHV]; | |
3719 | return ref(newUNOP(OP_RV2HV, 0, scalar(o)), OP_RV2HV); | |
3720 | ||
3721 | case OP_RV2SV: | |
3722 | case OP_RV2AV: | |
79072805 LW |
3723 | o->op_type = OP_RV2HV; |
3724 | o->op_ppaddr = ppaddr[OP_RV2HV]; | |
3725 | ref(o, OP_RV2HV); | |
ed6116ce LW |
3726 | break; |
3727 | ||
3728 | default: | |
79072805 | 3729 | warn("oops: oopsHV"); |
ed6116ce LW |
3730 | break; |
3731 | } | |
79072805 LW |
3732 | return o; |
3733 | } | |
3734 | ||
3735 | OP * | |
8ac85365 | 3736 | newAVREF(OP *o) |
79072805 | 3737 | { |
ed6116ce LW |
3738 | if (o->op_type == OP_PADANY) { |
3739 | o->op_type = OP_PADAV; | |
3740 | o->op_ppaddr = ppaddr[OP_PADAV]; | |
93a17b20 | 3741 | return o; |
ed6116ce | 3742 | } |
79072805 LW |
3743 | return newUNOP(OP_RV2AV, 0, scalar(o)); |
3744 | } | |
3745 | ||
3746 | OP * | |
8ac85365 | 3747 | newGVREF(I32 type, OP *o) |
79072805 | 3748 | { |
a0d0e21e LW |
3749 | if (type == OP_MAPSTART) |
3750 | return newUNOP(OP_NULL, 0, o); | |
748a9306 | 3751 | return ref(newUNOP(OP_RV2GV, OPf_REF, o), type); |
79072805 LW |
3752 | } |
3753 | ||
3754 | OP * | |
8ac85365 | 3755 | newHVREF(OP *o) |
79072805 | 3756 | { |
ed6116ce LW |
3757 | if (o->op_type == OP_PADANY) { |
3758 | o->op_type = OP_PADHV; | |
3759 | o->op_ppaddr = ppaddr[OP_PADHV]; | |
93a17b20 | 3760 | return o; |
ed6116ce | 3761 | } |
79072805 LW |
3762 | return newUNOP(OP_RV2HV, 0, scalar(o)); |
3763 | } | |
3764 | ||
3765 | OP * | |
8ac85365 | 3766 | oopsCV(OP *o) |
79072805 | 3767 | { |
463ee0b2 | 3768 | croak("NOT IMPL LINE %d",__LINE__); |
79072805 LW |
3769 | /* STUB */ |
3770 | return o; | |
3771 | } | |
3772 | ||
3773 | OP * | |
8ac85365 | 3774 | newCVREF(I32 flags, OP *o) |
79072805 | 3775 | { |
c07a80fd | 3776 | return newUNOP(OP_RV2CV, flags, scalar(o)); |
79072805 LW |
3777 | } |
3778 | ||
3779 | OP * | |
8ac85365 | 3780 | newSVREF(OP *o) |
79072805 | 3781 | { |
ed6116ce LW |
3782 | if (o->op_type == OP_PADANY) { |
3783 | o->op_type = OP_PADSV; | |
3784 | o->op_ppaddr = ppaddr[OP_PADSV]; | |
93a17b20 | 3785 | return o; |
ed6116ce | 3786 | } |
224a4551 MB |
3787 | else if (o->op_type == OP_THREADSV && !(o->op_flags & OPpDONE_SVREF)) { |
3788 | o->op_flags |= OPpDONE_SVREF; | |
a863c7d1 | 3789 | return o; |
224a4551 | 3790 | } |
79072805 LW |
3791 | return newUNOP(OP_RV2SV, 0, scalar(o)); |
3792 | } | |
3793 | ||
3794 | /* Check routines. */ | |
3795 | ||
3796 | OP * | |
8ac85365 | 3797 | ck_anoncode(OP *o) |
5f05dabc | 3798 | { |
178c6305 CS |
3799 | PADOFFSET ix; |
3800 | SV* name; | |
3801 | ||
3802 | name = NEWSV(1106,0); | |
3803 | sv_upgrade(name, SVt_PVNV); | |
3804 | sv_setpvn(name, "&", 1); | |
3805 | SvIVX(name) = -1; | |
3806 | SvNVX(name) = 1; | |
5dc0d613 | 3807 | ix = pad_alloc(o->op_type, SVs_PADMY); |
178c6305 | 3808 | av_store(comppad_name, ix, name); |
5dc0d613 MB |
3809 | av_store(comppad, ix, cSVOPo->op_sv); |
3810 | SvPADMY_on(cSVOPo->op_sv); | |
3811 | cSVOPo->op_sv = Nullsv; | |
3812 | cSVOPo->op_targ = ix; | |
3813 | return o; | |
5f05dabc | 3814 | } |
3815 | ||
3816 | OP * | |
8ac85365 | 3817 | ck_bitop(OP *o) |
55497cff | 3818 | { |
5dc0d613 MB |
3819 | o->op_private = hints; |
3820 | return o; | |
55497cff | 3821 | } |
3822 | ||
3823 | OP * | |
8ac85365 | 3824 | ck_concat(OP *o) |
79072805 | 3825 | { |
11343788 MB |
3826 | if (cUNOPo->op_first->op_type == OP_CONCAT) |
3827 | o->op_flags |= OPf_STACKED; | |
3828 | return o; | |
79072805 LW |
3829 | } |
3830 | ||
3831 | OP * | |
8ac85365 | 3832 | ck_spair(OP *o) |
79072805 | 3833 | { |
11343788 | 3834 | if (o->op_flags & OPf_KIDS) { |
79072805 | 3835 | OP* newop; |
a0d0e21e | 3836 | OP* kid; |
5dc0d613 MB |
3837 | OPCODE type = o->op_type; |
3838 | o = modkids(ck_fun(o), type); | |
11343788 | 3839 | kid = cUNOPo->op_first; |
a0d0e21e LW |
3840 | newop = kUNOP->op_first->op_sibling; |
3841 | if (newop && | |
3842 | (newop->op_sibling || | |
3843 | !(opargs[newop->op_type] & OA_RETSCALAR) || | |
3844 | newop->op_type == OP_PADAV || newop->op_type == OP_PADHV || | |
3845 | newop->op_type == OP_RV2AV || newop->op_type == OP_RV2HV)) { | |
aeea060c | 3846 | |
11343788 | 3847 | return o; |
a0d0e21e LW |
3848 | } |
3849 | op_free(kUNOP->op_first); | |
3850 | kUNOP->op_first = newop; | |
3851 | } | |
11343788 MB |
3852 | o->op_ppaddr = ppaddr[++o->op_type]; |
3853 | return ck_fun(o); | |
a0d0e21e LW |
3854 | } |
3855 | ||
3856 | OP * | |
8ac85365 | 3857 | ck_delete(OP *o) |
a0d0e21e | 3858 | { |
11343788 | 3859 | o = ck_fun(o); |
5dc0d613 | 3860 | o->op_private = 0; |
11343788 MB |
3861 | if (o->op_flags & OPf_KIDS) { |
3862 | OP *kid = cUNOPo->op_first; | |
5f05dabc | 3863 | if (kid->op_type == OP_HSLICE) |
5dc0d613 | 3864 | o->op_private |= OPpSLICE; |
5f05dabc | 3865 | else if (kid->op_type != OP_HELEM) |
3866 | croak("%s argument is not a HASH element or slice", | |
5dc0d613 | 3867 | op_desc[o->op_type]); |
a0d0e21e | 3868 | null(kid); |
79072805 | 3869 | } |
11343788 | 3870 | return o; |
79072805 LW |
3871 | } |
3872 | ||
3873 | OP * | |
8ac85365 | 3874 | ck_eof(OP *o) |
79072805 | 3875 | { |
11343788 | 3876 | I32 type = o->op_type; |
79072805 | 3877 | |
11343788 MB |
3878 | if (o->op_flags & OPf_KIDS) { |
3879 | if (cLISTOPo->op_first->op_type == OP_STUB) { | |
3880 | op_free(o); | |
3881 | o = newUNOP(type, OPf_SPECIAL, | |
d58bf5aa | 3882 | newGVOP(OP_GV, 0, gv_fetchpv("main::ARGV", TRUE, SVt_PVAV))); |
8990e307 | 3883 | } |
11343788 | 3884 | return ck_fun(o); |
79072805 | 3885 | } |
11343788 | 3886 | return o; |
79072805 LW |
3887 | } |
3888 | ||
3889 | OP * | |
8ac85365 | 3890 | ck_eval(OP *o) |
79072805 | 3891 | { |
85e6fe83 | 3892 | hints |= HINT_BLOCK_SCOPE; |
11343788 MB |
3893 | if (o->op_flags & OPf_KIDS) { |
3894 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
79072805 | 3895 | |
93a17b20 | 3896 | if (!kid) { |
11343788 MB |
3897 | o->op_flags &= ~OPf_KIDS; |
3898 | null(o); | |
79072805 LW |
3899 | } |
3900 | else if (kid->op_type == OP_LINESEQ) { | |
3901 | LOGOP *enter; | |
3902 | ||
11343788 MB |
3903 | kid->op_next = o->op_next; |
3904 | cUNOPo->op_first = 0; | |
3905 | op_free(o); | |
79072805 LW |
3906 | |
3907 | Newz(1101, enter, 1, LOGOP); | |
3908 | enter->op_type = OP_ENTERTRY; | |
3909 | enter->op_ppaddr = ppaddr[OP_ENTERTRY]; | |
3910 | enter->op_private = 0; | |
3911 | ||
3912 | /* establish postfix order */ | |
3913 | enter->op_next = (OP*)enter; | |
3914 | ||
11343788 MB |
3915 | o = prepend_elem(OP_LINESEQ, (OP*)enter, (OP*)kid); |
3916 | o->op_type = OP_LEAVETRY; | |
3917 | o->op_ppaddr = ppaddr[OP_LEAVETRY]; | |
3918 | enter->op_other = o; | |
3919 | return o; | |
79072805 | 3920 | } |
c7cc6f1c | 3921 | else |
473986ff | 3922 | scalar((OP*)kid); |
79072805 LW |
3923 | } |
3924 | else { | |
11343788 | 3925 | op_free(o); |
54b9620d | 3926 | o = newUNOP(OP_ENTEREVAL, 0, newDEFSVOP()); |
79072805 | 3927 | } |
11343788 MB |
3928 | o->op_targ = (PADOFFSET)hints; |
3929 | return o; | |
79072805 LW |
3930 | } |
3931 | ||
3932 | OP * | |
8ac85365 | 3933 | ck_exec(OP *o) |
79072805 LW |
3934 | { |
3935 | OP *kid; | |
11343788 MB |
3936 | if (o->op_flags & OPf_STACKED) { |
3937 | o = ck_fun(o); | |
3938 | kid = cUNOPo->op_first->op_sibling; | |
8990e307 LW |
3939 | if (kid->op_type == OP_RV2GV) |
3940 | null(kid); | |
79072805 | 3941 | } |
463ee0b2 | 3942 | else |
11343788 MB |
3943 | o = listkids(o); |
3944 | return o; | |
79072805 LW |
3945 | } |
3946 | ||
3947 | OP * | |
8ac85365 | 3948 | ck_exists(OP *o) |
5f05dabc | 3949 | { |
5196be3e MB |
3950 | o = ck_fun(o); |
3951 | if (o->op_flags & OPf_KIDS) { | |
3952 | OP *kid = cUNOPo->op_first; | |
5f05dabc | 3953 | if (kid->op_type != OP_HELEM) |
5196be3e | 3954 | croak("%s argument is not a HASH element", op_desc[o->op_type]); |
5f05dabc | 3955 | null(kid); |
3956 | } | |
5196be3e | 3957 | return o; |
5f05dabc | 3958 | } |
3959 | ||
3960 | OP * | |
8ac85365 | 3961 | ck_gvconst(register OP *o) |
79072805 LW |
3962 | { |
3963 | o = fold_constants(o); | |
3964 | if (o->op_type == OP_CONST) | |
3965 | o->op_type = OP_GV; | |
3966 | return o; | |
3967 | } | |
3968 | ||
3969 | OP * | |
8ac85365 | 3970 | ck_rvconst(register OP *o) |
79072805 | 3971 | { |
11343788 MB |
3972 | dTHR; |
3973 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
85e6fe83 | 3974 | |
11343788 | 3975 | o->op_private |= (hints & HINT_STRICT_REFS); |
79072805 | 3976 | if (kid->op_type == OP_CONST) { |
44a8e56a | 3977 | char *name; |
3978 | int iscv; | |
3979 | GV *gv; | |
3980 | ||
3981 | name = SvPV(kid->op_sv, na); | |
3982 | if ((hints & HINT_STRICT_REFS) && (kid->op_private & OPpCONST_BARE)) { | |
3983 | char *badthing = Nullch; | |
5dc0d613 | 3984 | switch (o->op_type) { |
44a8e56a | 3985 | case OP_RV2SV: |
3986 | badthing = "a SCALAR"; | |
3987 | break; | |
3988 | case OP_RV2AV: | |
3989 | badthing = "an ARRAY"; | |
3990 | break; | |
3991 | case OP_RV2HV: | |
3992 | badthing = "a HASH"; | |
3993 | break; | |
3994 | } | |
3995 | if (badthing) | |
3996 | croak( | |
3997 | "Can't use bareword (\"%s\") as %s ref while \"strict refs\" in use", | |
3998 | name, badthing); | |
3999 | } | |
93233ece CS |
4000 | /* |
4001 | * This is a little tricky. We only want to add the symbol if we | |
4002 | * didn't add it in the lexer. Otherwise we get duplicate strict | |
4003 | * warnings. But if we didn't add it in the lexer, we must at | |
4004 | * least pretend like we wanted to add it even if it existed before, | |
4005 | * or we get possible typo warnings. OPpCONST_ENTERED says | |
4006 | * whether the lexer already added THIS instance of this symbol. | |
4007 | */ | |
5196be3e | 4008 | iscv = (o->op_type == OP_RV2CV) * 2; |
93233ece | 4009 | do { |
44a8e56a | 4010 | gv = gv_fetchpv(name, |
748a9306 | 4011 | iscv | !(kid->op_private & OPpCONST_ENTERED), |
a0d0e21e LW |
4012 | iscv |
4013 | ? SVt_PVCV | |
11343788 | 4014 | : o->op_type == OP_RV2SV |
a0d0e21e | 4015 | ? SVt_PV |
11343788 | 4016 | : o->op_type == OP_RV2AV |
a0d0e21e | 4017 | ? SVt_PVAV |
11343788 | 4018 | : o->op_type == OP_RV2HV |
a0d0e21e LW |
4019 | ? SVt_PVHV |
4020 | : SVt_PVGV); | |
93233ece CS |
4021 | } while (!gv && !(kid->op_private & OPpCONST_ENTERED) && !iscv++); |
4022 | if (gv) { | |
4023 | kid->op_type = OP_GV; | |
4024 | SvREFCNT_dec(kid->op_sv); | |
4025 | kid->op_sv = SvREFCNT_inc(gv); | |
a0d0e21e | 4026 | } |
79072805 | 4027 | } |
11343788 | 4028 | return o; |
79072805 LW |
4029 | } |
4030 | ||
4031 | OP * | |
8ac85365 | 4032 | ck_ftst(OP *o) |
79072805 | 4033 | { |
11343788 MB |
4034 | dTHR; |
4035 | I32 type = o->op_type; | |
79072805 | 4036 | |
11343788 MB |
4037 | if (o->op_flags & OPf_REF) |
4038 | return o; | |
79072805 | 4039 | |
11343788 MB |
4040 | if (o->op_flags & OPf_KIDS) { |
4041 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
79072805 LW |
4042 | |
4043 | if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) { | |
a0d0e21e | 4044 | OP *newop = newGVOP(type, OPf_REF, |
85e6fe83 | 4045 | gv_fetchpv(SvPVx(kid->op_sv, na), TRUE, SVt_PVIO)); |
11343788 | 4046 | op_free(o); |
79072805 LW |
4047 | return newop; |
4048 | } | |
4049 | } | |
4050 | else { | |
11343788 | 4051 | op_free(o); |
79072805 | 4052 | if (type == OP_FTTTY) |
fb73857a | 4053 | return newGVOP(type, OPf_REF, gv_fetchpv("main::STDIN", TRUE, |
85e6fe83 | 4054 | SVt_PVIO)); |
79072805 | 4055 | else |
54b9620d | 4056 | return newUNOP(type, 0, newDEFSVOP()); |
79072805 | 4057 | } |
11343788 | 4058 | return o; |
79072805 LW |
4059 | } |
4060 | ||
4061 | OP * | |
8ac85365 | 4062 | ck_fun(OP *o) |
79072805 | 4063 | { |
11343788 | 4064 | dTHR; |
79072805 LW |
4065 | register OP *kid; |
4066 | OP **tokid; | |
4067 | OP *sibl; | |
4068 | I32 numargs = 0; | |
11343788 | 4069 | int type = o->op_type; |
a0d0e21e | 4070 | register I32 oa = opargs[type] >> OASHIFT; |
aeea060c | 4071 | |
11343788 | 4072 | if (o->op_flags & OPf_STACKED) { |
79072805 LW |
4073 | if ((oa & OA_OPTIONAL) && (oa >> 4) && !((oa >> 4) & OA_OPTIONAL)) |
4074 | oa &= ~OA_OPTIONAL; | |
4075 | else | |
11343788 | 4076 | return no_fh_allowed(o); |
79072805 LW |
4077 | } |
4078 | ||
11343788 MB |
4079 | if (o->op_flags & OPf_KIDS) { |
4080 | tokid = &cLISTOPo->op_first; | |
4081 | kid = cLISTOPo->op_first; | |
8990e307 LW |
4082 | if (kid->op_type == OP_PUSHMARK || |
4083 | kid->op_type == OP_NULL && kid->op_targ == OP_PUSHMARK) | |
4084 | { | |
79072805 LW |
4085 | tokid = &kid->op_sibling; |
4086 | kid = kid->op_sibling; | |
4087 | } | |
a0d0e21e | 4088 | if (!kid && opargs[type] & OA_DEFGV) |
54b9620d | 4089 | *tokid = kid = newDEFSVOP(); |
79072805 LW |
4090 | |
4091 | while (oa && kid) { | |
4092 | numargs++; | |
4093 | sibl = kid->op_sibling; | |
4094 | switch (oa & 7) { | |
4095 | case OA_SCALAR: | |
4096 | scalar(kid); | |
4097 | break; | |
4098 | case OA_LIST: | |
4099 | if (oa < 16) { | |
4100 | kid = 0; | |
4101 | continue; | |
4102 | } | |
4103 | else | |
4104 | list(kid); | |
4105 | break; | |
4106 | case OA_AVREF: | |
4107 | if (kid->op_type == OP_CONST && | |
4108 | (kid->op_private & OPpCONST_BARE)) { | |
463ee0b2 | 4109 | char *name = SvPVx(((SVOP*)kid)->op_sv, na); |
79072805 | 4110 | OP *newop = newAVREF(newGVOP(OP_GV, 0, |
85e6fe83 | 4111 | gv_fetchpv(name, TRUE, SVt_PVAV) )); |
463ee0b2 | 4112 | if (dowarn) |
ff0cee69 | 4113 | warn("Array @%s missing the @ in argument %ld of %s()", |
4114 | name, (long)numargs, op_desc[type]); | |
79072805 LW |
4115 | op_free(kid); |
4116 | kid = newop; | |
4117 | kid->op_sibling = sibl; | |
4118 | *tokid = kid; | |
4119 | } | |
8990e307 | 4120 | else if (kid->op_type != OP_RV2AV && kid->op_type != OP_PADAV) |
11343788 | 4121 | bad_type(numargs, "array", op_desc[o->op_type], kid); |
a0d0e21e | 4122 | mod(kid, type); |
79072805 LW |
4123 | break; |
4124 | case OA_HVREF: | |
4125 | if (kid->op_type == OP_CONST && | |
4126 | (kid->op_private & OPpCONST_BARE)) { | |
463ee0b2 | 4127 | char *name = SvPVx(((SVOP*)kid)->op_sv, na); |
79072805 | 4128 | OP *newop = newHVREF(newGVOP(OP_GV, 0, |
85e6fe83 | 4129 | gv_fetchpv(name, TRUE, SVt_PVHV) )); |
463ee0b2 | 4130 | if (dowarn) |
ff0cee69 | 4131 | warn("Hash %%%s missing the %% in argument %ld of %s()", |
4132 | name, (long)numargs, op_desc[type]); | |
79072805 LW |
4133 | op_free(kid); |
4134 | kid = newop; | |
4135 | kid->op_sibling = sibl; | |
4136 | *tokid = kid; | |
4137 | } | |
8990e307 | 4138 | else if (kid->op_type != OP_RV2HV && kid->op_type != OP_PADHV) |
11343788 | 4139 | bad_type(numargs, "hash", op_desc[o->op_type], kid); |
a0d0e21e | 4140 | mod(kid, type); |
79072805 LW |
4141 | break; |
4142 | case OA_CVREF: | |
4143 | { | |
a0d0e21e | 4144 | OP *newop = newUNOP(OP_NULL, 0, kid); |
79072805 LW |
4145 | kid->op_sibling = 0; |
4146 | linklist(kid); | |
4147 | newop->op_next = newop; | |
4148 | kid = newop; | |
4149 | kid->op_sibling = sibl; | |
4150 | *tokid = kid; | |
4151 | } | |
4152 | break; | |
4153 | case OA_FILEREF: | |
4154 | if (kid->op_type != OP_GV) { | |
4155 | if (kid->op_type == OP_CONST && | |
4156 | (kid->op_private & OPpCONST_BARE)) { | |
4157 | OP *newop = newGVOP(OP_GV, 0, | |
85e6fe83 LW |
4158 | gv_fetchpv(SvPVx(((SVOP*)kid)->op_sv, na), TRUE, |
4159 | SVt_PVIO) ); | |
79072805 LW |
4160 | op_free(kid); |
4161 | kid = newop; | |
4162 | } | |
4163 | else { | |
4164 | kid->op_sibling = 0; | |
4165 | kid = newUNOP(OP_RV2GV, 0, scalar(kid)); | |
4166 | } | |
4167 | kid->op_sibling = sibl; | |
4168 | *tokid = kid; | |
4169 | } | |
4170 | scalar(kid); | |
4171 | break; | |
4172 | case OA_SCALARREF: | |
a0d0e21e | 4173 | mod(scalar(kid), type); |
79072805 LW |
4174 | break; |
4175 | } | |
4176 | oa >>= 4; | |
4177 | tokid = &kid->op_sibling; | |
4178 | kid = kid->op_sibling; | |
4179 | } | |
11343788 | 4180 | o->op_private |= numargs; |
79072805 | 4181 | if (kid) |
11343788 MB |
4182 | return too_many_arguments(o,op_desc[o->op_type]); |
4183 | listkids(o); | |
79072805 | 4184 | } |
a0d0e21e | 4185 | else if (opargs[type] & OA_DEFGV) { |
11343788 | 4186 | op_free(o); |
54b9620d | 4187 | return newUNOP(type, 0, newDEFSVOP()); |
a0d0e21e LW |
4188 | } |
4189 | ||
79072805 LW |
4190 | if (oa) { |
4191 | while (oa & OA_OPTIONAL) | |
4192 | oa >>= 4; | |
4193 | if (oa && oa != OA_LIST) | |
11343788 | 4194 | return too_few_arguments(o,op_desc[o->op_type]); |
79072805 | 4195 | } |
11343788 | 4196 | return o; |
79072805 LW |
4197 | } |
4198 | ||
4199 | OP * | |
8ac85365 | 4200 | ck_glob(OP *o) |
79072805 | 4201 | { |
fb73857a | 4202 | GV *gv; |
4203 | ||
1f2bfc8a | 4204 | if ((o->op_flags & OPf_KIDS) && !cLISTOPo->op_first->op_sibling) |
54b9620d | 4205 | append_elem(OP_GLOB, o, newDEFSVOP()); |
fb73857a | 4206 | |
4207 | if (!((gv = gv_fetchpv("glob", FALSE, SVt_PVCV)) && GvIMPORTED_CV(gv))) | |
4208 | gv = gv_fetchpv("CORE::GLOBAL::glob", FALSE, SVt_PVCV); | |
b1cb66bf | 4209 | |
4210 | if (gv && GvIMPORTED_CV(gv)) { | |
46fc3d4c | 4211 | static int glob_index; |
4212 | ||
5196be3e | 4213 | append_elem(OP_GLOB, o, |
46fc3d4c | 4214 | newSVOP(OP_CONST, 0, newSViv(glob_index++))); |
1f2bfc8a MB |
4215 | o->op_type = OP_LIST; |
4216 | o->op_ppaddr = ppaddr[OP_LIST]; | |
4217 | cLISTOPo->op_first->op_type = OP_PUSHMARK; | |
4218 | cLISTOPo->op_first->op_ppaddr = ppaddr[OP_PUSHMARK]; | |
4219 | o = newUNOP(OP_ENTERSUB, OPf_STACKED, | |
aeea060c | 4220 | append_elem(OP_LIST, o, |
1f2bfc8a MB |
4221 | scalar(newUNOP(OP_RV2CV, 0, |
4222 | newGVOP(OP_GV, 0, gv))))); | |
d58bf5aa MB |
4223 | o = newUNOP(OP_NULL, 0, ck_subr(o)); |
4224 | o->op_targ = OP_GLOB; /* hint at what it used to be */ | |
4225 | return o; | |
b1cb66bf | 4226 | } |
4227 | gv = newGVgen("main"); | |
a0d0e21e | 4228 | gv_IOadd(gv); |
11343788 MB |
4229 | append_elem(OP_GLOB, o, newGVOP(OP_GV, 0, gv)); |
4230 | scalarkids(o); | |
4231 | return ck_fun(o); | |
79072805 LW |
4232 | } |
4233 | ||
4234 | OP * | |
8ac85365 | 4235 | ck_grep(OP *o) |
79072805 LW |
4236 | { |
4237 | LOGOP *gwop; | |
4238 | OP *kid; | |
11343788 | 4239 | OPCODE type = o->op_type == OP_GREPSTART ? OP_GREPWHILE : OP_MAPWHILE; |
79072805 | 4240 | |
11343788 | 4241 | o->op_ppaddr = ppaddr[OP_GREPSTART]; |
a0d0e21e | 4242 | Newz(1101, gwop, 1, LOGOP); |
aeea060c | 4243 | |
11343788 | 4244 | if (o->op_flags & OPf_STACKED) { |
a0d0e21e | 4245 | OP* k; |
11343788 MB |
4246 | o = ck_sort(o); |
4247 | kid = cLISTOPo->op_first->op_sibling; | |
4248 | for (k = cLISTOPo->op_first->op_sibling->op_next; k; k = k->op_next) { | |
a0d0e21e LW |
4249 | kid = k; |
4250 | } | |
4251 | kid->op_next = (OP*)gwop; | |
11343788 | 4252 | o->op_flags &= ~OPf_STACKED; |
93a17b20 | 4253 | } |
11343788 | 4254 | kid = cLISTOPo->op_first->op_sibling; |
a0d0e21e LW |
4255 | if (type == OP_MAPWHILE) |
4256 | list(kid); | |
4257 | else | |
4258 | scalar(kid); | |
11343788 | 4259 | o = ck_fun(o); |
79072805 | 4260 | if (error_count) |
11343788 | 4261 | return o; |
aeea060c | 4262 | kid = cLISTOPo->op_first->op_sibling; |
79072805 | 4263 | if (kid->op_type != OP_NULL) |
463ee0b2 | 4264 | croak("panic: ck_grep"); |
79072805 LW |
4265 | kid = kUNOP->op_first; |
4266 | ||
a0d0e21e LW |
4267 | gwop->op_type = type; |
4268 | gwop->op_ppaddr = ppaddr[type]; | |
11343788 | 4269 | gwop->op_first = listkids(o); |
79072805 LW |
4270 | gwop->op_flags |= OPf_KIDS; |
4271 | gwop->op_private = 1; | |
4272 | gwop->op_other = LINKLIST(kid); | |
a0d0e21e | 4273 | gwop->op_targ = pad_alloc(type, SVs_PADTMP); |
79072805 LW |
4274 | kid->op_next = (OP*)gwop; |
4275 | ||
11343788 | 4276 | kid = cLISTOPo->op_first->op_sibling; |
a0d0e21e | 4277 | if (!kid || !kid->op_sibling) |
11343788 | 4278 | return too_few_arguments(o,op_desc[o->op_type]); |
a0d0e21e LW |
4279 | for (kid = kid->op_sibling; kid; kid = kid->op_sibling) |
4280 | mod(kid, OP_GREPSTART); | |
4281 | ||
79072805 LW |
4282 | return (OP*)gwop; |
4283 | } | |
4284 | ||
4285 | OP * | |
8ac85365 | 4286 | ck_index(OP *o) |
79072805 | 4287 | { |
11343788 MB |
4288 | if (o->op_flags & OPf_KIDS) { |
4289 | OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ | |
79072805 | 4290 | if (kid && kid->op_type == OP_CONST) |
2779dcf1 | 4291 | fbm_compile(((SVOP*)kid)->op_sv, 0); |
79072805 | 4292 | } |
11343788 | 4293 | return ck_fun(o); |
79072805 LW |
4294 | } |
4295 | ||
4296 | OP * | |
8ac85365 | 4297 | ck_lengthconst(OP *o) |
79072805 LW |
4298 | { |
4299 | /* XXX length optimization goes here */ | |
11343788 | 4300 | return ck_fun(o); |
79072805 LW |
4301 | } |
4302 | ||
4303 | OP * | |
8ac85365 | 4304 | ck_lfun(OP *o) |
79072805 | 4305 | { |
5dc0d613 MB |
4306 | OPCODE type = o->op_type; |
4307 | return modkids(ck_fun(o), type); | |
79072805 LW |
4308 | } |
4309 | ||
4310 | OP * | |
8ac85365 | 4311 | ck_rfun(OP *o) |
8990e307 | 4312 | { |
5dc0d613 MB |
4313 | OPCODE type = o->op_type; |
4314 | return refkids(ck_fun(o), type); | |
8990e307 LW |
4315 | } |
4316 | ||
4317 | OP * | |
8ac85365 | 4318 | ck_listiob(OP *o) |
79072805 LW |
4319 | { |
4320 | register OP *kid; | |
aeea060c | 4321 | |
11343788 | 4322 | kid = cLISTOPo->op_first; |
79072805 | 4323 | if (!kid) { |
11343788 MB |
4324 | o = force_list(o); |
4325 | kid = cLISTOPo->op_first; | |
79072805 LW |
4326 | } |
4327 | if (kid->op_type == OP_PUSHMARK) | |
4328 | kid = kid->op_sibling; | |
11343788 | 4329 | if (kid && o->op_flags & OPf_STACKED) |
79072805 LW |
4330 | kid = kid->op_sibling; |
4331 | else if (kid && !kid->op_sibling) { /* print HANDLE; */ | |
4332 | if (kid->op_type == OP_CONST && kid->op_private & OPpCONST_BARE) { | |
11343788 | 4333 | o->op_flags |= OPf_STACKED; /* make it a filehandle */ |
748a9306 | 4334 | kid = newUNOP(OP_RV2GV, OPf_REF, scalar(kid)); |
11343788 MB |
4335 | cLISTOPo->op_first->op_sibling = kid; |
4336 | cLISTOPo->op_last = kid; | |
79072805 LW |
4337 | kid = kid->op_sibling; |
4338 | } | |
4339 | } | |
4340 | ||
4341 | if (!kid) | |
54b9620d | 4342 | append_elem(o->op_type, o, newDEFSVOP()); |
79072805 | 4343 | |
5dc0d613 | 4344 | o = listkids(o); |
bbce6d69 | 4345 | |
5dc0d613 | 4346 | o->op_private = 0; |
36477c24 | 4347 | #ifdef USE_LOCALE |
bbce6d69 | 4348 | if (hints & HINT_LOCALE) |
5dc0d613 | 4349 | o->op_private |= OPpLOCALE; |
bbce6d69 | 4350 | #endif |
4351 | ||
5dc0d613 | 4352 | return o; |
bbce6d69 | 4353 | } |
4354 | ||
4355 | OP * | |
8ac85365 | 4356 | ck_fun_locale(OP *o) |
bbce6d69 | 4357 | { |
5dc0d613 | 4358 | o = ck_fun(o); |
bbce6d69 | 4359 | |
5dc0d613 | 4360 | o->op_private = 0; |
36477c24 | 4361 | #ifdef USE_LOCALE |
bbce6d69 | 4362 | if (hints & HINT_LOCALE) |
5dc0d613 | 4363 | o->op_private |= OPpLOCALE; |
bbce6d69 | 4364 | #endif |
4365 | ||
5dc0d613 | 4366 | return o; |
bbce6d69 | 4367 | } |
4368 | ||
4369 | OP * | |
8ac85365 | 4370 | ck_scmp(OP *o) |
bbce6d69 | 4371 | { |
5dc0d613 | 4372 | o->op_private = 0; |
36477c24 | 4373 | #ifdef USE_LOCALE |
bbce6d69 | 4374 | if (hints & HINT_LOCALE) |
5dc0d613 | 4375 | o->op_private |= OPpLOCALE; |
bbce6d69 | 4376 | #endif |
36477c24 | 4377 | |
5dc0d613 | 4378 | return o; |
79072805 LW |
4379 | } |
4380 | ||
4381 | OP * | |
8ac85365 | 4382 | ck_match(OP *o) |
79072805 | 4383 | { |
5dc0d613 | 4384 | o->op_private |= OPpRUNTIME; |
11343788 | 4385 | return o; |
79072805 LW |
4386 | } |
4387 | ||
4388 | OP * | |
8ac85365 | 4389 | ck_null(OP *o) |
79072805 | 4390 | { |
11343788 | 4391 | return o; |
79072805 LW |
4392 | } |
4393 | ||
4394 | OP * | |
8ac85365 | 4395 | ck_repeat(OP *o) |
79072805 | 4396 | { |
11343788 MB |
4397 | if (cBINOPo->op_first->op_flags & OPf_PARENS) { |
4398 | o->op_private |= OPpREPEAT_DOLIST; | |
4399 | cBINOPo->op_first = force_list(cBINOPo->op_first); | |
79072805 LW |
4400 | } |
4401 | else | |
11343788 MB |
4402 | scalar(o); |
4403 | return o; | |
79072805 LW |
4404 | } |
4405 | ||
4406 | OP * | |
8ac85365 | 4407 | ck_require(OP *o) |
8990e307 | 4408 | { |
11343788 MB |
4409 | if (o->op_flags & OPf_KIDS) { /* Shall we supply missing .pm? */ |
4410 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
8990e307 LW |
4411 | |
4412 | if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) { | |
8990e307 | 4413 | char *s; |
a0d0e21e LW |
4414 | for (s = SvPVX(kid->op_sv); *s; s++) { |
4415 | if (*s == ':' && s[1] == ':') { | |
4416 | *s = '/'; | |
1aef975c | 4417 | Move(s+2, s+1, strlen(s+2)+1, char); |
a0d0e21e LW |
4418 | --SvCUR(kid->op_sv); |
4419 | } | |
8990e307 | 4420 | } |
a0d0e21e | 4421 | sv_catpvn(kid->op_sv, ".pm", 3); |
8990e307 LW |
4422 | } |
4423 | } | |
11343788 | 4424 | return ck_fun(o); |
8990e307 LW |
4425 | } |
4426 | ||
4427 | OP * | |
8ac85365 | 4428 | ck_retarget(OP *o) |
79072805 | 4429 | { |
463ee0b2 | 4430 | croak("NOT IMPL LINE %d",__LINE__); |
79072805 | 4431 | /* STUB */ |
11343788 | 4432 | return o; |
79072805 LW |
4433 | } |
4434 | ||
4435 | OP * | |
8ac85365 | 4436 | ck_select(OP *o) |
79072805 | 4437 | { |
c07a80fd | 4438 | OP* kid; |
11343788 MB |
4439 | if (o->op_flags & OPf_KIDS) { |
4440 | kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ | |
2304df62 | 4441 | if (kid && kid->op_sibling) { |
11343788 MB |
4442 | o->op_type = OP_SSELECT; |
4443 | o->op_ppaddr = ppaddr[OP_SSELECT]; | |
4444 | o = ck_fun(o); | |
4445 | return fold_constants(o); | |
79072805 LW |
4446 | } |
4447 | } | |
11343788 MB |
4448 | o = ck_fun(o); |
4449 | kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ | |
c07a80fd | 4450 | if (kid && kid->op_type == OP_RV2GV) |
4451 | kid->op_private &= ~HINT_STRICT_REFS; | |
11343788 | 4452 | return o; |
79072805 LW |
4453 | } |
4454 | ||
4455 | OP * | |
8ac85365 | 4456 | ck_shift(OP *o) |
79072805 | 4457 | { |
11343788 | 4458 | I32 type = o->op_type; |
79072805 | 4459 | |
11343788 | 4460 | if (!(o->op_flags & OPf_KIDS)) { |
6d4ff0d2 MB |
4461 | OP *argop; |
4462 | ||
11343788 | 4463 | op_free(o); |
6d4ff0d2 | 4464 | #ifdef USE_THREADS |
977336f5 | 4465 | if (!CvUNIQUE(compcv)) { |
6d4ff0d2 MB |
4466 | argop = newOP(OP_PADAV, OPf_REF); |
4467 | argop->op_targ = 0; /* curpad[0] is @_ */ | |
4468 | } | |
4469 | else { | |
4470 | argop = newUNOP(OP_RV2AV, 0, | |
4471 | scalar(newGVOP(OP_GV, 0, | |
4472 | gv_fetchpv("ARGV", TRUE, SVt_PVAV)))); | |
4473 | } | |
4474 | #else | |
4475 | argop = newUNOP(OP_RV2AV, 0, | |
977336f5 | 4476 | scalar(newGVOP(OP_GV, 0, !CvUNIQUE(compcv) ? |
6d4ff0d2 MB |
4477 | defgv : gv_fetchpv("ARGV", TRUE, SVt_PVAV)))); |
4478 | #endif /* USE_THREADS */ | |
4479 | return newUNOP(type, 0, scalar(argop)); | |
79072805 | 4480 | } |
11343788 | 4481 | return scalar(modkids(ck_fun(o), type)); |
79072805 LW |
4482 | } |
4483 | ||
4484 | OP * | |
8ac85365 | 4485 | ck_sort(OP *o) |
79072805 | 4486 | { |
5dc0d613 | 4487 | o->op_private = 0; |
36477c24 | 4488 | #ifdef USE_LOCALE |
bbce6d69 | 4489 | if (hints & HINT_LOCALE) |
5dc0d613 | 4490 | o->op_private |= OPpLOCALE; |
bbce6d69 | 4491 | #endif |
4492 | ||
11343788 MB |
4493 | if (o->op_flags & OPf_STACKED) { |
4494 | OP *kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ | |
463ee0b2 LW |
4495 | OP *k; |
4496 | kid = kUNOP->op_first; /* get past rv2gv */ | |
79072805 | 4497 | |
463ee0b2 | 4498 | if (kid->op_type == OP_SCOPE || kid->op_type == OP_LEAVE) { |
79072805 | 4499 | linklist(kid); |
463ee0b2 LW |
4500 | if (kid->op_type == OP_SCOPE) { |
4501 | k = kid->op_next; | |
4502 | kid->op_next = 0; | |
79072805 | 4503 | } |
463ee0b2 | 4504 | else if (kid->op_type == OP_LEAVE) { |
11343788 | 4505 | if (o->op_type == OP_SORT) { |
748a9306 LW |
4506 | null(kid); /* wipe out leave */ |
4507 | kid->op_next = kid; | |
463ee0b2 | 4508 | |
748a9306 LW |
4509 | for (k = kLISTOP->op_first->op_next; k; k = k->op_next) { |
4510 | if (k->op_next == kid) | |
4511 | k->op_next = 0; | |
4512 | } | |
463ee0b2 | 4513 | } |
748a9306 LW |
4514 | else |
4515 | kid->op_next = 0; /* just disconnect the leave */ | |
a0d0e21e | 4516 | k = kLISTOP->op_first; |
463ee0b2 | 4517 | } |
a0d0e21e LW |
4518 | peep(k); |
4519 | ||
11343788 | 4520 | kid = cLISTOPo->op_first->op_sibling; /* get past pushmark */ |
8990e307 | 4521 | null(kid); /* wipe out rv2gv */ |
11343788 | 4522 | if (o->op_type == OP_SORT) |
a0d0e21e LW |
4523 | kid->op_next = kid; |
4524 | else | |
4525 | kid->op_next = k; | |
11343788 | 4526 | o->op_flags |= OPf_SPECIAL; |
79072805 LW |
4527 | } |
4528 | } | |
bbce6d69 | 4529 | |
11343788 | 4530 | return o; |
79072805 LW |
4531 | } |
4532 | ||
4533 | OP * | |
8ac85365 | 4534 | ck_split(OP *o) |
79072805 LW |
4535 | { |
4536 | register OP *kid; | |
aeea060c | 4537 | |
11343788 MB |
4538 | if (o->op_flags & OPf_STACKED) |
4539 | return no_fh_allowed(o); | |
79072805 | 4540 | |
11343788 | 4541 | kid = cLISTOPo->op_first; |
8990e307 | 4542 | if (kid->op_type != OP_NULL) |
463ee0b2 | 4543 | croak("panic: ck_split"); |
8990e307 | 4544 | kid = kid->op_sibling; |
11343788 MB |
4545 | op_free(cLISTOPo->op_first); |
4546 | cLISTOPo->op_first = kid; | |
85e6fe83 | 4547 | if (!kid) { |
11343788 MB |
4548 | cLISTOPo->op_first = kid = newSVOP(OP_CONST, 0, newSVpv(" ", 1)); |
4549 | cLISTOPo->op_last = kid; /* There was only one element previously */ | |
85e6fe83 | 4550 | } |
79072805 LW |
4551 | |
4552 | if (kid->op_type != OP_MATCH) { | |
4553 | OP *sibl = kid->op_sibling; | |
463ee0b2 | 4554 | kid->op_sibling = 0; |
79072805 | 4555 | kid = pmruntime( newPMOP(OP_MATCH, OPf_SPECIAL), kid, Nullop); |
11343788 MB |
4556 | if (cLISTOPo->op_first == cLISTOPo->op_last) |
4557 | cLISTOPo->op_last = kid; | |
4558 | cLISTOPo->op_first = kid; | |
79072805 LW |
4559 | kid->op_sibling = sibl; |
4560 | } | |
4561 | ||
4562 | kid->op_type = OP_PUSHRE; | |
4563 | kid->op_ppaddr = ppaddr[OP_PUSHRE]; | |
4564 | scalar(kid); | |
4565 | ||
4566 | if (!kid->op_sibling) | |
54b9620d | 4567 | append_elem(OP_SPLIT, o, newDEFSVOP()); |
79072805 LW |
4568 | |
4569 | kid = kid->op_sibling; | |
4570 | scalar(kid); | |
4571 | ||
4572 | if (!kid->op_sibling) | |
11343788 | 4573 | append_elem(OP_SPLIT, o, newSVOP(OP_CONST, 0, newSViv(0))); |
79072805 LW |
4574 | |
4575 | kid = kid->op_sibling; | |
4576 | scalar(kid); | |
4577 | ||
4578 | if (kid->op_sibling) | |
11343788 | 4579 | return too_many_arguments(o,op_desc[o->op_type]); |
79072805 | 4580 | |
11343788 | 4581 | return o; |
79072805 LW |
4582 | } |
4583 | ||
4584 | OP * | |
8ac85365 | 4585 | ck_subr(OP *o) |
79072805 | 4586 | { |
11343788 MB |
4587 | dTHR; |
4588 | OP *prev = ((cUNOPo->op_first->op_sibling) | |
4589 | ? cUNOPo : ((UNOP*)cUNOPo->op_first))->op_first; | |
4590 | OP *o2 = prev->op_sibling; | |
4633a7c4 LW |
4591 | OP *cvop; |
4592 | char *proto = 0; | |
4593 | CV *cv = 0; | |
46fc3d4c | 4594 | GV *namegv = 0; |
4633a7c4 LW |
4595 | int optional = 0; |
4596 | I32 arg = 0; | |
4597 | ||
11343788 | 4598 | for (cvop = o2; cvop->op_sibling; cvop = cvop->op_sibling) ; |
4633a7c4 LW |
4599 | if (cvop->op_type == OP_RV2CV) { |
4600 | SVOP* tmpop; | |
11343788 | 4601 | o->op_private |= (cvop->op_private & OPpENTERSUB_AMPER); |
4633a7c4 LW |
4602 | null(cvop); /* disable rv2cv */ |
4603 | tmpop = (SVOP*)((UNOP*)cvop)->op_first; | |
4604 | if (tmpop->op_type == OP_GV) { | |
8ebc5c01 | 4605 | cv = GvCVu(tmpop->op_sv); |
5dc0d613 | 4606 | if (cv && SvPOK(cv) && !(o->op_private & OPpENTERSUB_AMPER)) { |
46fc3d4c | 4607 | namegv = CvANON(cv) ? (GV*)tmpop->op_sv : CvGV(cv); |
4608 | proto = SvPV((SV*)cv, na); | |
4609 | } | |
4633a7c4 LW |
4610 | } |
4611 | } | |
11343788 | 4612 | o->op_private |= (hints & HINT_STRICT_REFS); |
84902520 | 4613 | if (PERLDB_SUB && curstash != debstash) |
11343788 MB |
4614 | o->op_private |= OPpENTERSUB_DB; |
4615 | while (o2 != cvop) { | |
4633a7c4 LW |
4616 | if (proto) { |
4617 | switch (*proto) { | |
4618 | case '\0': | |
5dc0d613 | 4619 | return too_many_arguments(o, gv_ename(namegv)); |
4633a7c4 LW |
4620 | case ';': |
4621 | optional = 1; | |
4622 | proto++; | |
4623 | continue; | |
4624 | case '$': | |
4625 | proto++; | |
4626 | arg++; | |
11343788 | 4627 | scalar(o2); |
4633a7c4 LW |
4628 | break; |
4629 | case '%': | |
4630 | case '@': | |
11343788 | 4631 | list(o2); |
4633a7c4 LW |
4632 | arg++; |
4633 | break; | |
4634 | case '&': | |
4635 | proto++; | |
4636 | arg++; | |
11343788 | 4637 | if (o2->op_type != OP_REFGEN && o2->op_type != OP_UNDEF) |
5dc0d613 | 4638 | bad_type(arg, "block", gv_ename(namegv), o2); |
4633a7c4 LW |
4639 | break; |
4640 | case '*': | |
4641 | proto++; | |
4642 | arg++; | |
11343788 | 4643 | if (o2->op_type == OP_RV2GV) |
4633a7c4 LW |
4644 | goto wrapref; |
4645 | { | |
11343788 | 4646 | OP* kid = o2; |
69dcf70c | 4647 | OP* sib = kid->op_sibling; |
4633a7c4 | 4648 | kid->op_sibling = 0; |
69dcf70c MB |
4649 | o2 = newUNOP(OP_RV2GV, 0, kid); |
4650 | o2->op_sibling = sib; | |
6fa846a0 | 4651 | prev->op_sibling = o2; |
4633a7c4 LW |
4652 | } |
4653 | goto wrapref; | |
4654 | case '\\': | |
4655 | proto++; | |
4656 | arg++; | |
4657 | switch (*proto++) { | |
4658 | case '*': | |
11343788 | 4659 | if (o2->op_type != OP_RV2GV) |
5dc0d613 | 4660 | bad_type(arg, "symbol", gv_ename(namegv), o2); |
4633a7c4 LW |
4661 | goto wrapref; |
4662 | case '&': | |
11343788 | 4663 | if (o2->op_type != OP_RV2CV) |
5dc0d613 | 4664 | bad_type(arg, "sub", gv_ename(namegv), o2); |
4633a7c4 LW |
4665 | goto wrapref; |
4666 | case '$': | |
11343788 | 4667 | if (o2->op_type != OP_RV2SV && o2->op_type != OP_PADSV) |
5dc0d613 | 4668 | bad_type(arg, "scalar", gv_ename(namegv), o2); |
4633a7c4 LW |
4669 | goto wrapref; |
4670 | case '@': | |
11343788 | 4671 | if (o2->op_type != OP_RV2AV && o2->op_type != OP_PADAV) |
5dc0d613 | 4672 | bad_type(arg, "array", gv_ename(namegv), o2); |
4633a7c4 LW |
4673 | goto wrapref; |
4674 | case '%': | |
11343788 | 4675 | if (o2->op_type != OP_RV2HV && o2->op_type != OP_PADHV) |
5dc0d613 | 4676 | bad_type(arg, "hash", gv_ename(namegv), o2); |
4633a7c4 LW |
4677 | wrapref: |
4678 | { | |
11343788 | 4679 | OP* kid = o2; |
6fa846a0 | 4680 | OP* sib = kid->op_sibling; |
4633a7c4 | 4681 | kid->op_sibling = 0; |
6fa846a0 GS |
4682 | o2 = newUNOP(OP_REFGEN, 0, kid); |
4683 | o2->op_sibling = sib; | |
e858de61 | 4684 | prev->op_sibling = o2; |
4633a7c4 LW |
4685 | } |
4686 | break; | |
4687 | default: goto oops; | |
4688 | } | |
4689 | break; | |
b1cb66bf | 4690 | case ' ': |
4691 | proto++; | |
4692 | continue; | |
4633a7c4 LW |
4693 | default: |
4694 | oops: | |
4695 | croak("Malformed prototype for %s: %s", | |
46fc3d4c | 4696 | gv_ename(namegv), SvPV((SV*)cv, na)); |
4633a7c4 LW |
4697 | } |
4698 | } | |
4699 | else | |
11343788 MB |
4700 | list(o2); |
4701 | mod(o2, OP_ENTERSUB); | |
4702 | prev = o2; | |
4703 | o2 = o2->op_sibling; | |
4633a7c4 | 4704 | } |
fb73857a | 4705 | if (proto && !optional && |
4706 | (*proto && *proto != '@' && *proto != '%' && *proto != ';')) | |
5dc0d613 | 4707 | return too_few_arguments(o, gv_ename(namegv)); |
11343788 | 4708 | return o; |
79072805 LW |
4709 | } |
4710 | ||
4711 | OP * | |
8ac85365 | 4712 | ck_svconst(OP *o) |
8990e307 | 4713 | { |
11343788 MB |
4714 | SvREADONLY_on(cSVOPo->op_sv); |
4715 | return o; | |
8990e307 LW |
4716 | } |
4717 | ||
4718 | OP * | |
8ac85365 | 4719 | ck_trunc(OP *o) |
79072805 | 4720 | { |
11343788 MB |
4721 | if (o->op_flags & OPf_KIDS) { |
4722 | SVOP *kid = (SVOP*)cUNOPo->op_first; | |
79072805 | 4723 | |
a0d0e21e LW |
4724 | if (kid->op_type == OP_NULL) |
4725 | kid = (SVOP*)kid->op_sibling; | |
4726 | if (kid && | |
4727 | kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) | |
11343788 | 4728 | o->op_flags |= OPf_SPECIAL; |
79072805 | 4729 | } |
11343788 | 4730 | return ck_fun(o); |
79072805 LW |
4731 | } |
4732 | ||
463ee0b2 LW |
4733 | /* A peephole optimizer. We visit the ops in the order they're to execute. */ |
4734 | ||
79072805 | 4735 | void |
8ac85365 | 4736 | peep(register OP *o) |
79072805 | 4737 | { |
11343788 | 4738 | dTHR; |
79072805 | 4739 | register OP* oldop = 0; |
a0d0e21e | 4740 | if (!o || o->op_seq) |
79072805 | 4741 | return; |
a0d0e21e | 4742 | ENTER; |
462e5cf6 | 4743 | SAVEOP(); |
a0d0e21e LW |
4744 | SAVESPTR(curcop); |
4745 | for (; o; o = o->op_next) { | |
4746 | if (o->op_seq) | |
4747 | break; | |
c07a80fd | 4748 | if (!op_seqmax) |
4749 | op_seqmax++; | |
a0d0e21e LW |
4750 | op = o; |
4751 | switch (o->op_type) { | |
4752 | case OP_NEXTSTATE: | |
4753 | case OP_DBSTATE: | |
4754 | curcop = ((COP*)o); /* for warnings */ | |
c07a80fd | 4755 | o->op_seq = op_seqmax++; |
a0d0e21e LW |
4756 | break; |
4757 | ||
4758 | case OP_CONCAT: | |
4759 | case OP_CONST: | |
4760 | case OP_JOIN: | |
4761 | case OP_UC: | |
4762 | case OP_UCFIRST: | |
4763 | case OP_LC: | |
4764 | case OP_LCFIRST: | |
4765 | case OP_QUOTEMETA: | |
3c4f770c | 4766 | if (o->op_next && o->op_next->op_type == OP_STRINGIFY) |
a0d0e21e | 4767 | null(o->op_next); |
c07a80fd | 4768 | o->op_seq = op_seqmax++; |
a0d0e21e | 4769 | break; |
8990e307 | 4770 | case OP_STUB: |
54310121 | 4771 | if ((o->op_flags & OPf_WANT) != OPf_WANT_LIST) { |
c07a80fd | 4772 | o->op_seq = op_seqmax++; |
54310121 | 4773 | break; /* Scalar stub must produce undef. List stub is noop */ |
8990e307 | 4774 | } |
748a9306 | 4775 | goto nothin; |
79072805 | 4776 | case OP_NULL: |
748a9306 | 4777 | if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE) |
7766148a | 4778 | curcop = ((COP*)o); |
748a9306 | 4779 | goto nothin; |
79072805 | 4780 | case OP_SCALAR: |
93a17b20 | 4781 | case OP_LINESEQ: |
463ee0b2 | 4782 | case OP_SCOPE: |
748a9306 | 4783 | nothin: |
a0d0e21e LW |
4784 | if (oldop && o->op_next) { |
4785 | oldop->op_next = o->op_next; | |
79072805 LW |
4786 | continue; |
4787 | } | |
c07a80fd | 4788 | o->op_seq = op_seqmax++; |
79072805 LW |
4789 | break; |
4790 | ||
4791 | case OP_GV: | |
a0d0e21e | 4792 | if (o->op_next->op_type == OP_RV2SV) { |
5f05dabc | 4793 | if (!(o->op_next->op_private & OPpDEREF)) { |
a0d0e21e LW |
4794 | null(o->op_next); |
4795 | o->op_private |= o->op_next->op_private & OPpLVAL_INTRO; | |
4796 | o->op_next = o->op_next->op_next; | |
4797 | o->op_type = OP_GVSV; | |
4798 | o->op_ppaddr = ppaddr[OP_GVSV]; | |
8990e307 LW |
4799 | } |
4800 | } | |
a0d0e21e LW |
4801 | else if (o->op_next->op_type == OP_RV2AV) { |
4802 | OP* pop = o->op_next->op_next; | |
4803 | IV i; | |
8990e307 | 4804 | if (pop->op_type == OP_CONST && |
e858de61 | 4805 | (op = pop->op_next) && |
8990e307 | 4806 | pop->op_next->op_type == OP_AELEM && |
a0d0e21e | 4807 | !(pop->op_next->op_private & |
68dc0745 | 4808 | (OPpLVAL_INTRO|OPpLVAL_DEFER|OPpDEREF)) && |
a0d0e21e LW |
4809 | (i = SvIV(((SVOP*)pop)->op_sv) - compiling.cop_arybase) |
4810 | <= 255 && | |
8990e307 LW |
4811 | i >= 0) |
4812 | { | |
748a9306 | 4813 | SvREFCNT_dec(((SVOP*)pop)->op_sv); |
a0d0e21e | 4814 | null(o->op_next); |
8990e307 LW |
4815 | null(pop->op_next); |
4816 | null(pop); | |
a0d0e21e LW |
4817 | o->op_flags |= pop->op_next->op_flags & OPf_MOD; |
4818 | o->op_next = pop->op_next->op_next; | |
4819 | o->op_type = OP_AELEMFAST; | |
4820 | o->op_ppaddr = ppaddr[OP_AELEMFAST]; | |
4821 | o->op_private = (U8)i; | |
a6006777 | 4822 | GvAVn(((GVOP*)o)->op_gv); |
8990e307 | 4823 | } |
79072805 | 4824 | } |
c07a80fd | 4825 | o->op_seq = op_seqmax++; |
79072805 LW |
4826 | break; |
4827 | ||
af41f3ca MB |
4828 | case OP_PADAV: |
4829 | if (o->op_next->op_type == OP_RV2AV | |
4830 | && (o->op_next->op_flags && OPf_REF)) | |
4831 | { | |
4832 | null(o->op_next); | |
4833 | o->op_next = o->op_next->op_next; | |
4834 | } | |
4835 | break; | |
aeea060c | 4836 | |
af41f3ca MB |
4837 | case OP_PADHV: |
4838 | if (o->op_next->op_type == OP_RV2HV | |
4839 | && (o->op_next->op_flags && OPf_REF)) | |
4840 | { | |
4841 | null(o->op_next); | |
4842 | o->op_next = o->op_next->op_next; | |
4843 | } | |
4844 | break; | |
4845 | ||
a0d0e21e | 4846 | case OP_MAPWHILE: |
79072805 LW |
4847 | case OP_GREPWHILE: |
4848 | case OP_AND: | |
4849 | case OP_OR: | |
c07a80fd | 4850 | o->op_seq = op_seqmax++; |
fd4d1407 IZ |
4851 | while (cLOGOP->op_other->op_type == OP_NULL) |
4852 | cLOGOP->op_other = cLOGOP->op_other->op_next; | |
79072805 LW |
4853 | peep(cLOGOP->op_other); |
4854 | break; | |
4855 | ||
4856 | case OP_COND_EXPR: | |
c07a80fd | 4857 | o->op_seq = op_seqmax++; |
79072805 LW |
4858 | peep(cCONDOP->op_true); |
4859 | peep(cCONDOP->op_false); | |
4860 | break; | |
4861 | ||
4862 | case OP_ENTERLOOP: | |
c07a80fd | 4863 | o->op_seq = op_seqmax++; |
79072805 LW |
4864 | peep(cLOOP->op_redoop); |
4865 | peep(cLOOP->op_nextop); | |
4866 | peep(cLOOP->op_lastop); | |
4867 | break; | |
4868 | ||
4869 | case OP_MATCH: | |
4870 | case OP_SUBST: | |
c07a80fd | 4871 | o->op_seq = op_seqmax++; |
a0d0e21e | 4872 | peep(cPMOP->op_pmreplstart); |
79072805 LW |
4873 | break; |
4874 | ||
a0d0e21e | 4875 | case OP_EXEC: |
c07a80fd | 4876 | o->op_seq = op_seqmax++; |
a0d0e21e LW |
4877 | if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) { |
4878 | if (o->op_next->op_sibling && | |
20408e3c GS |
4879 | o->op_next->op_sibling->op_type != OP_EXIT && |
4880 | o->op_next->op_sibling->op_type != OP_WARN && | |
a0d0e21e LW |
4881 | o->op_next->op_sibling->op_type != OP_DIE) { |
4882 | line_t oldline = curcop->cop_line; | |
4883 | ||
4884 | curcop->cop_line = ((COP*)o->op_next)->cop_line; | |
4885 | warn("Statement unlikely to be reached"); | |
4886 | warn("(Maybe you meant system() when you said exec()?)\n"); | |
4887 | curcop->cop_line = oldline; | |
4888 | } | |
4889 | } | |
4890 | break; | |
aeea060c | 4891 | |
c750a3ec MB |
4892 | case OP_HELEM: { |
4893 | UNOP *rop; | |
4894 | SV *lexname; | |
4895 | GV **fields; | |
4896 | SV **svp, **indsvp; | |
4897 | I32 ind; | |
4898 | char *key; | |
4899 | STRLEN keylen; | |
aeea060c | 4900 | |
c750a3ec MB |
4901 | if (o->op_private & (OPpDEREF_HV|OPpDEREF_AV|OPpLVAL_INTRO) |
4902 | || ((BINOP*)o)->op_last->op_type != OP_CONST) | |
4903 | break; | |
4904 | rop = (UNOP*)((BINOP*)o)->op_first; | |
4905 | if (rop->op_type != OP_RV2HV || rop->op_first->op_type != OP_PADSV) | |
4906 | break; | |
4907 | lexname = *av_fetch(comppad_name, rop->op_first->op_targ, TRUE); | |
4908 | if (!SvOBJECT(lexname)) | |
4909 | break; | |
5196be3e | 4910 | fields = (GV**)hv_fetch(SvSTASH(lexname), "FIELDS", 6, FALSE); |
c750a3ec MB |
4911 | if (!fields || !GvHV(*fields)) |
4912 | break; | |
4913 | svp = &((SVOP*)((BINOP*)o)->op_last)->op_sv; | |
4914 | key = SvPV(*svp, keylen); | |
4915 | indsvp = hv_fetch(GvHV(*fields), key, keylen, FALSE); | |
4916 | if (!indsvp) { | |
4917 | croak("No such field \"%s\" in variable %s of type %s", | |
4918 | key, SvPV(lexname, na), HvNAME(SvSTASH(lexname))); | |
4919 | } | |
4920 | ind = SvIV(*indsvp); | |
4921 | if (ind < 1) | |
4922 | croak("Bad index while coercing array into hash"); | |
4923 | rop->op_type = OP_RV2AV; | |
4924 | rop->op_ppaddr = ppaddr[OP_RV2AV]; | |
4925 | o->op_type = OP_AELEM; | |
4926 | o->op_ppaddr = ppaddr[OP_AELEM]; | |
4927 | SvREFCNT_dec(*svp); | |
4928 | *svp = newSViv(ind); | |
4929 | break; | |
4930 | } | |
4931 | ||
79072805 | 4932 | default: |
c07a80fd | 4933 | o->op_seq = op_seqmax++; |
79072805 LW |
4934 | break; |
4935 | } | |
a0d0e21e | 4936 | oldop = o; |
79072805 | 4937 | } |
a0d0e21e | 4938 | LEAVE; |
79072805 | 4939 | } |