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