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