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