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)); |
79072805 | 43 | |
4633a7c4 | 44 | static char* |
8ac85365 | 45 | gv_ename(GV *gv) |
4633a7c4 LW |
46 | { |
47 | SV* tmpsv = sv_newmortal(); | |
46fc3d4c | 48 | gv_efullname3(tmpsv, gv, Nullch); |
4633a7c4 LW |
49 | return SvPV(tmpsv,na); |
50 | } | |
51 | ||
8990e307 | 52 | static OP * |
8ac85365 | 53 | no_fh_allowed(OP *o) |
79072805 | 54 | { |
46fc3d4c | 55 | yyerror(form("Missing comma after first argument to %s function", |
5196be3e | 56 | op_desc[o->op_type])); |
11343788 | 57 | return o; |
79072805 LW |
58 | } |
59 | ||
8990e307 | 60 | static OP * |
8ac85365 | 61 | too_few_arguments(OP *o, char *name) |
79072805 | 62 | { |
46fc3d4c | 63 | yyerror(form("Not enough arguments for %s", name)); |
11343788 | 64 | return o; |
79072805 LW |
65 | } |
66 | ||
8990e307 | 67 | static OP * |
8ac85365 | 68 | too_many_arguments(OP *o, char *name) |
79072805 | 69 | { |
46fc3d4c | 70 | yyerror(form("Too many arguments for %s", name)); |
11343788 | 71 | return o; |
79072805 LW |
72 | } |
73 | ||
3bc5dc61 | 74 | static void |
8ac85365 | 75 | bad_type(I32 n, char *t, char *name, OP *kid) |
8990e307 | 76 | { |
46fc3d4c | 77 | yyerror(form("Type of arg %d to %s must be %s (not %s)", |
78 | (int)n, name, t, op_desc[kid->op_type])); | |
8990e307 LW |
79 | } |
80 | ||
a0d0e21e | 81 | void |
8ac85365 | 82 | assertref(OP *o) |
a0d0e21e | 83 | { |
11343788 | 84 | int type = o->op_type; |
a0d0e21e | 85 | if (type != OP_AELEM && type != OP_HELEM) { |
46fc3d4c | 86 | yyerror(form("Can't use subscript on %s", op_desc[type])); |
8ebc5c01 | 87 | if (type == OP_ENTERSUB || type == OP_RV2HV || type == OP_PADHV) |
748a9306 | 88 | warn("(Did you mean $ or @ instead of %c?)\n", |
8ebc5c01 | 89 | type == OP_ENTERSUB ? '&' : '%'); |
a0d0e21e LW |
90 | } |
91 | } | |
92 | ||
79072805 LW |
93 | /* "register" allocation */ |
94 | ||
95 | PADOFFSET | |
8ac85365 | 96 | pad_allocmy(char *name) |
93a17b20 | 97 | { |
11343788 | 98 | dTHR; |
a0d0e21e LW |
99 | PADOFFSET off; |
100 | SV *sv; | |
101 | ||
102 | if (!(isALPHA(name[1]) || name[1] == '_' && (int)strlen(name) > 2)) { | |
46fc3d4c | 103 | if (!isPRINT(name[1])) { |
104 | name[3] = '\0'; | |
105 | name[2] = toCTRL(name[1]); | |
106 | name[1] = '^'; | |
107 | } | |
a0d0e21e LW |
108 | croak("Can't use global %s in \"my\"",name); |
109 | } | |
fb73857a | 110 | if (dowarn && AvFILL(comppad_name) >= 0) { |
b1cb66bf | 111 | SV **svp = AvARRAY(comppad_name); |
112 | for (off = AvFILL(comppad_name); off > comppad_name_floor; off--) { | |
113 | if ((sv = svp[off]) | |
114 | && sv != &sv_undef | |
115 | && SvIVX(sv) == 999999999 /* var is in open scope */ | |
116 | && strEQ(name, SvPVX(sv))) | |
117 | { | |
118 | warn("\"my\" variable %s masks earlier declaration in same scope", name); | |
119 | break; | |
120 | } | |
121 | } | |
122 | } | |
a0d0e21e LW |
123 | off = pad_alloc(OP_PADSV, SVs_PADMY); |
124 | sv = NEWSV(1102,0); | |
93a17b20 LW |
125 | sv_upgrade(sv, SVt_PVNV); |
126 | sv_setpv(sv, name); | |
c750a3ec MB |
127 | if (in_my_stash) { |
128 | if (*name != '$') | |
129 | croak("Can't declare class for non-scalar %s in \"my\"",name); | |
130 | SvOBJECT_on(sv); | |
131 | (void)SvUPGRADE(sv, SVt_PVMG); | |
132 | SvSTASH(sv) = (HV*)SvREFCNT_inc(in_my_stash); | |
133 | sv_objcount++; | |
134 | } | |
8990e307 | 135 | av_store(comppad_name, off, sv); |
748a9306 | 136 | SvNVX(sv) = (double)999999999; |
8990e307 LW |
137 | SvIVX(sv) = 0; /* Not yet introduced--see newSTATEOP */ |
138 | if (!min_intro_pending) | |
139 | min_intro_pending = off; | |
140 | max_intro_pending = off; | |
93a17b20 | 141 | if (*name == '@') |
463ee0b2 | 142 | av_store(comppad, off, (SV*)newAV()); |
93a17b20 | 143 | else if (*name == '%') |
463ee0b2 | 144 | av_store(comppad, off, (SV*)newHV()); |
ed6116ce | 145 | SvPADMY_on(curpad[off]); |
93a17b20 LW |
146 | return off; |
147 | } | |
148 | ||
748a9306 | 149 | static PADOFFSET |
e9a444f0 | 150 | #ifndef CAN_PROTOTYPE |
748a9306 | 151 | pad_findlex(name, newoff, seq, startcv, cx_ix) |
93a17b20 | 152 | char *name; |
748a9306 | 153 | PADOFFSET newoff; |
bbce6d69 | 154 | U32 seq; |
748a9306 LW |
155 | CV* startcv; |
156 | I32 cx_ix; | |
e9a444f0 | 157 | #else |
bbce6d69 | 158 | pad_findlex(char *name, PADOFFSET newoff, U32 seq, CV* startcv, I32 cx_ix) |
e9a444f0 | 159 | #endif |
93a17b20 | 160 | { |
11343788 | 161 | dTHR; |
748a9306 | 162 | CV *cv; |
93a17b20 LW |
163 | I32 off; |
164 | SV *sv; | |
93a17b20 | 165 | register I32 i; |
c09156bb | 166 | register PERL_CONTEXT *cx; |
a0d0e21e | 167 | int saweval; |
93a17b20 | 168 | |
748a9306 | 169 | for (cv = startcv; cv; cv = CvOUTSIDE(cv)) { |
4fdae800 | 170 | AV *curlist = CvPADLIST(cv); |
171 | SV **svp = av_fetch(curlist, 0, FALSE); | |
748a9306 | 172 | AV *curname; |
4fdae800 | 173 | |
748a9306 | 174 | if (!svp || *svp == &sv_undef) |
4633a7c4 | 175 | continue; |
748a9306 LW |
176 | curname = (AV*)*svp; |
177 | svp = AvARRAY(curname); | |
178 | for (off = AvFILL(curname); off > 0; off--) { | |
179 | if ((sv = svp[off]) && | |
180 | sv != &sv_undef && | |
181 | seq <= SvIVX(sv) && | |
13826f2c | 182 | seq > I_32(SvNVX(sv)) && |
748a9306 LW |
183 | strEQ(SvPVX(sv), name)) |
184 | { | |
5f05dabc | 185 | I32 depth; |
186 | AV *oldpad; | |
187 | SV *oldsv; | |
188 | ||
189 | depth = CvDEPTH(cv); | |
190 | if (!depth) { | |
9607fc9c | 191 | if (newoff) { |
192 | if (SvFAKE(sv)) | |
193 | continue; | |
4fdae800 | 194 | return 0; /* don't clone from inactive stack frame */ |
9607fc9c | 195 | } |
5f05dabc | 196 | depth = 1; |
197 | } | |
198 | oldpad = (AV*)*av_fetch(curlist, depth, FALSE); | |
199 | oldsv = *av_fetch(oldpad, off, TRUE); | |
748a9306 | 200 | if (!newoff) { /* Not a mere clone operation. */ |
9607fc9c | 201 | SV *namesv = NEWSV(1103,0); |
748a9306 | 202 | newoff = pad_alloc(OP_PADSV, SVs_PADMY); |
9607fc9c | 203 | sv_upgrade(namesv, SVt_PVNV); |
204 | sv_setpv(namesv, name); | |
205 | av_store(comppad_name, newoff, namesv); | |
206 | SvNVX(namesv) = (double)curcop->cop_seq; | |
207 | SvIVX(namesv) = 999999999; /* A ref, intro immediately */ | |
208 | SvFAKE_on(namesv); /* A ref, not a real var */ | |
774d564b | 209 | if (CvANON(compcv) || SvTYPE(compcv) == SVt_PVFM) { |
28757baa | 210 | /* "It's closures all the way down." */ |
211 | CvCLONE_on(compcv); | |
54310121 | 212 | if (cv == startcv) { |
213 | if (CvANON(compcv)) | |
214 | oldsv = Nullsv; /* no need to keep ref */ | |
215 | } | |
216 | else { | |
28757baa | 217 | CV *bcv; |
218 | for (bcv = startcv; | |
219 | bcv && bcv != cv && !CvCLONE(bcv); | |
220 | bcv = CvOUTSIDE(bcv)) { | |
221 | if (CvANON(bcv)) | |
222 | CvCLONE_on(bcv); | |
223 | else { | |
774d564b | 224 | if (dowarn && !CvUNIQUE(cv)) |
44a8e56a | 225 | warn( |
226 | "Variable \"%s\" may be unavailable", | |
28757baa | 227 | name); |
228 | break; | |
229 | } | |
230 | } | |
231 | } | |
232 | } | |
44a8e56a | 233 | else if (!CvUNIQUE(compcv)) { |
9607fc9c | 234 | if (dowarn && !SvFAKE(sv) && !CvUNIQUE(cv)) |
44a8e56a | 235 | warn("Variable \"%s\" will not stay shared", name); |
5f05dabc | 236 | } |
748a9306 | 237 | } |
199100c8 | 238 | av_store(comppad, newoff, SvREFCNT_inc(oldsv)); |
748a9306 LW |
239 | return newoff; |
240 | } | |
93a17b20 LW |
241 | } |
242 | } | |
243 | ||
244 | /* Nothing in current lexical context--try eval's context, if any. | |
245 | * This is necessary to let the perldb get at lexically scoped variables. | |
246 | * XXX This will also probably interact badly with eval tree caching. | |
247 | */ | |
248 | ||
a0d0e21e | 249 | saweval = 0; |
748a9306 | 250 | for (i = cx_ix; i >= 0; i--) { |
93a17b20 LW |
251 | cx = &cxstack[i]; |
252 | switch (cx->cx_type) { | |
253 | default: | |
748a9306 LW |
254 | if (i == 0 && saweval) { |
255 | seq = cxstack[saweval].blk_oldcop->cop_seq; | |
256 | return pad_findlex(name, newoff, seq, main_cv, 0); | |
257 | } | |
93a17b20 LW |
258 | break; |
259 | case CXt_EVAL: | |
44a8e56a | 260 | switch (cx->blk_eval.old_op_type) { |
261 | case OP_ENTEREVAL: | |
262 | saweval = i; | |
263 | break; | |
264 | case OP_REQUIRE: | |
265 | /* require must have its own scope */ | |
266 | return 0; | |
267 | } | |
93a17b20 LW |
268 | break; |
269 | case CXt_SUB: | |
270 | if (!saweval) | |
271 | return 0; | |
272 | cv = cx->blk_sub.cv; | |
748a9306 LW |
273 | if (debstash && CvSTASH(cv) == debstash) { /* ignore DB'* scope */ |
274 | saweval = i; /* so we know where we were called from */ | |
93a17b20 | 275 | continue; |
93a17b20 | 276 | } |
748a9306 LW |
277 | seq = cxstack[saweval].blk_oldcop->cop_seq; |
278 | return pad_findlex(name, newoff, seq, cv, i-1); | |
93a17b20 LW |
279 | } |
280 | } | |
281 | ||
748a9306 LW |
282 | return 0; |
283 | } | |
a0d0e21e | 284 | |
748a9306 | 285 | PADOFFSET |
8ac85365 | 286 | pad_findmy(char *name) |
748a9306 | 287 | { |
11343788 | 288 | dTHR; |
748a9306 | 289 | I32 off; |
54310121 | 290 | I32 pendoff = 0; |
748a9306 LW |
291 | SV *sv; |
292 | SV **svp = AvARRAY(comppad_name); | |
bbce6d69 | 293 | U32 seq = cop_seqmax; |
748a9306 | 294 | |
11343788 MB |
295 | #ifdef USE_THREADS |
296 | /* | |
297 | * Special case to get lexical (and hence per-thread) @_. | |
298 | * XXX I need to find out how to tell at parse-time whether use | |
299 | * of @_ should refer to a lexical (from a sub) or defgv (global | |
300 | * scope and maybe weird sub-ish things like formats). See | |
301 | * startsub in perly.y. It's possible that @_ could be lexical | |
302 | * (at least from subs) even in non-threaded perl. | |
303 | */ | |
304 | if (strEQ(name, "@_")) | |
305 | return 0; /* success. (NOT_IN_PAD indicates failure) */ | |
306 | #endif /* USE_THREADS */ | |
307 | ||
748a9306 | 308 | /* The one we're looking for is probably just before comppad_name_fill. */ |
4633a7c4 | 309 | for (off = AvFILL(comppad_name); off > 0; off--) { |
a0d0e21e LW |
310 | if ((sv = svp[off]) && |
311 | sv != &sv_undef && | |
54310121 | 312 | (!SvIVX(sv) || |
313 | (seq <= SvIVX(sv) && | |
314 | seq > I_32(SvNVX(sv)))) && | |
a0d0e21e LW |
315 | strEQ(SvPVX(sv), name)) |
316 | { | |
54310121 | 317 | if (SvIVX(sv)) |
318 | return (PADOFFSET)off; | |
319 | pendoff = off; /* this pending def. will override import */ | |
a0d0e21e LW |
320 | } |
321 | } | |
748a9306 LW |
322 | |
323 | /* See if it's in a nested scope */ | |
324 | off = pad_findlex(name, 0, seq, CvOUTSIDE(compcv), cxstack_ix); | |
54310121 | 325 | if (off) { |
326 | /* If there is a pending local definition, this new alias must die */ | |
327 | if (pendoff) | |
328 | SvIVX(AvARRAY(comppad_name)[off]) = seq; | |
11343788 | 329 | return off; /* pad_findlex returns 0 for failure...*/ |
54310121 | 330 | } |
11343788 | 331 | return NOT_IN_PAD; /* ...but we return NOT_IN_PAD for failure */ |
93a17b20 LW |
332 | } |
333 | ||
334 | void | |
8ac85365 | 335 | pad_leavemy(I32 fill) |
93a17b20 LW |
336 | { |
337 | I32 off; | |
8990e307 | 338 | SV **svp = AvARRAY(comppad_name); |
93a17b20 | 339 | SV *sv; |
8990e307 LW |
340 | if (min_intro_pending && fill < min_intro_pending) { |
341 | for (off = max_intro_pending; off >= min_intro_pending; off--) { | |
a0d0e21e | 342 | if ((sv = svp[off]) && sv != &sv_undef) |
8990e307 LW |
343 | warn("%s never introduced", SvPVX(sv)); |
344 | } | |
345 | } | |
346 | /* "Deintroduce" my variables that are leaving with this scope. */ | |
347 | for (off = AvFILL(comppad_name); off > fill; off--) { | |
748a9306 | 348 | if ((sv = svp[off]) && sv != &sv_undef && SvIVX(sv) == 999999999) |
463ee0b2 | 349 | SvIVX(sv) = cop_seqmax; |
93a17b20 LW |
350 | } |
351 | } | |
352 | ||
353 | PADOFFSET | |
8ac85365 | 354 | pad_alloc(I32 optype, U32 tmptype) |
79072805 | 355 | { |
11343788 | 356 | dTHR; |
79072805 LW |
357 | SV *sv; |
358 | I32 retval; | |
359 | ||
360 | if (AvARRAY(comppad) != curpad) | |
463ee0b2 | 361 | croak("panic: pad_alloc"); |
a0d0e21e LW |
362 | if (pad_reset_pending) |
363 | pad_reset(); | |
ed6116ce | 364 | if (tmptype & SVs_PADMY) { |
79072805 LW |
365 | do { |
366 | sv = *av_fetch(comppad, AvFILL(comppad) + 1, TRUE); | |
ed6116ce | 367 | } while (SvPADBUSY(sv)); /* need a fresh one */ |
79072805 LW |
368 | retval = AvFILL(comppad); |
369 | } | |
370 | else { | |
bbce6d69 | 371 | SV **names = AvARRAY(comppad_name); |
372 | SSize_t names_fill = AvFILL(comppad_name); | |
373 | for (;;) { | |
374 | /* | |
375 | * "foreach" index vars temporarily become aliases to non-"my" | |
376 | * values. Thus we must skip, not just pad values that are | |
377 | * marked as current pad values, but also those with names. | |
378 | */ | |
379 | if (++padix <= names_fill && | |
380 | (sv = names[padix]) && sv != &sv_undef) | |
381 | continue; | |
382 | sv = *av_fetch(comppad, padix, TRUE); | |
383 | if (!(SvFLAGS(sv) & (SVs_PADTMP|SVs_PADMY))) | |
384 | break; | |
385 | } | |
79072805 LW |
386 | retval = padix; |
387 | } | |
8990e307 | 388 | SvFLAGS(sv) |= tmptype; |
79072805 | 389 | curpad = AvARRAY(comppad); |
11343788 | 390 | #ifdef USE_THREADS |
5dc0d613 MB |
391 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx alloc %ld for %s\n", |
392 | (unsigned long) thr, (unsigned long) curpad, | |
393 | (long) retval, op_name[optype])); | |
11343788 | 394 | #else |
5dc0d613 MB |
395 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad alloc %ld for %s\n", |
396 | (long) retval, op_name[optype])); | |
11343788 | 397 | #endif /* USE_THREADS */ |
79072805 LW |
398 | return (PADOFFSET)retval; |
399 | } | |
400 | ||
401 | SV * | |
a0d0e21e | 402 | #ifndef CAN_PROTOTYPE |
79072805 LW |
403 | pad_sv(po) |
404 | PADOFFSET po; | |
8990e307 LW |
405 | #else |
406 | pad_sv(PADOFFSET po) | |
a0d0e21e | 407 | #endif /* CAN_PROTOTYPE */ |
79072805 | 408 | { |
11343788 MB |
409 | dTHR; |
410 | #ifdef USE_THREADS | |
5dc0d613 MB |
411 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx sv %d\n", |
412 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 413 | #else |
79072805 | 414 | if (!po) |
463ee0b2 | 415 | croak("panic: pad_sv po"); |
5dc0d613 | 416 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad sv %d\n", po)); |
11343788 | 417 | #endif /* USE_THREADS */ |
79072805 LW |
418 | return curpad[po]; /* eventually we'll turn this into a macro */ |
419 | } | |
420 | ||
421 | void | |
a0d0e21e | 422 | #ifndef CAN_PROTOTYPE |
79072805 LW |
423 | pad_free(po) |
424 | PADOFFSET po; | |
8990e307 LW |
425 | #else |
426 | pad_free(PADOFFSET po) | |
a0d0e21e | 427 | #endif /* CAN_PROTOTYPE */ |
79072805 | 428 | { |
11343788 | 429 | dTHR; |
a0d0e21e LW |
430 | if (!curpad) |
431 | return; | |
79072805 | 432 | if (AvARRAY(comppad) != curpad) |
463ee0b2 | 433 | croak("panic: pad_free curpad"); |
79072805 | 434 | if (!po) |
463ee0b2 | 435 | croak("panic: pad_free po"); |
11343788 | 436 | #ifdef USE_THREADS |
5dc0d613 MB |
437 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx free %d\n", |
438 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 439 | #else |
5dc0d613 | 440 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad free %d\n", po)); |
11343788 | 441 | #endif /* USE_THREADS */ |
a0d0e21e | 442 | if (curpad[po] && curpad[po] != &sv_undef) |
ed6116ce | 443 | SvPADTMP_off(curpad[po]); |
a0d0e21e | 444 | if ((I32)po < padix) |
79072805 LW |
445 | padix = po - 1; |
446 | } | |
447 | ||
448 | void | |
a0d0e21e | 449 | #ifndef CAN_PROTOTYPE |
79072805 LW |
450 | pad_swipe(po) |
451 | PADOFFSET po; | |
8990e307 LW |
452 | #else |
453 | pad_swipe(PADOFFSET po) | |
a0d0e21e | 454 | #endif /* CAN_PROTOTYPE */ |
79072805 | 455 | { |
11343788 | 456 | dTHR; |
79072805 | 457 | if (AvARRAY(comppad) != curpad) |
463ee0b2 | 458 | croak("panic: pad_swipe curpad"); |
79072805 | 459 | if (!po) |
463ee0b2 | 460 | croak("panic: pad_swipe po"); |
11343788 | 461 | #ifdef USE_THREADS |
5dc0d613 MB |
462 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx swipe %d\n", |
463 | (unsigned long) thr, (unsigned long) curpad, po)); | |
11343788 | 464 | #else |
5dc0d613 | 465 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad swipe %d\n", po)); |
11343788 | 466 | #endif /* USE_THREADS */ |
ed6116ce | 467 | SvPADTMP_off(curpad[po]); |
a0d0e21e LW |
468 | curpad[po] = NEWSV(1107,0); |
469 | SvPADTMP_on(curpad[po]); | |
470 | if ((I32)po < padix) | |
79072805 LW |
471 | padix = po - 1; |
472 | } | |
473 | ||
474 | void | |
8ac85365 | 475 | pad_reset(void) |
79072805 | 476 | { |
11343788 | 477 | dTHR; |
79072805 LW |
478 | register I32 po; |
479 | ||
480 | if (AvARRAY(comppad) != curpad) | |
463ee0b2 | 481 | croak("panic: pad_reset curpad"); |
11343788 | 482 | #ifdef USE_THREADS |
5dc0d613 MB |
483 | DEBUG_X(PerlIO_printf(Perl_debug_log, "0x%lx Pad 0x%lx reset\n", |
484 | (unsigned long) thr, (unsigned long) curpad)); | |
11343788 | 485 | #else |
760ac839 | 486 | DEBUG_X(PerlIO_printf(Perl_debug_log, "Pad reset\n")); |
11343788 | 487 | #endif /* USE_THREADS */ |
748a9306 LW |
488 | if (!tainting) { /* Can't mix tainted and non-tainted temporaries. */ |
489 | for (po = AvMAX(comppad); po > padix_floor; po--) { | |
ff0cee69 | 490 | if (curpad[po] && !SvIMMORTAL(curpad[po])) |
748a9306 LW |
491 | SvPADTMP_off(curpad[po]); |
492 | } | |
493 | padix = padix_floor; | |
79072805 | 494 | } |
a0d0e21e | 495 | pad_reset_pending = FALSE; |
79072805 LW |
496 | } |
497 | ||
a863c7d1 | 498 | #ifdef USE_THREADS |
554b3eca | 499 | /* find_thread_magical is not reentrant */ |
a863c7d1 | 500 | PADOFFSET |
e77eedc2 | 501 | find_thread_magical(char *name) |
a863c7d1 MB |
502 | { |
503 | dTHR; | |
504 | char *p; | |
505 | PADOFFSET key; | |
554b3eca | 506 | SV **svp; |
a863c7d1 MB |
507 | /* We currently only handle single character magicals */ |
508 | p = strchr(per_thread_magicals, *name); | |
509 | if (!p) | |
510 | return NOT_IN_PAD; | |
554b3eca MB |
511 | key = p - per_thread_magicals; |
512 | svp = av_fetch(thr->magicals, key, FALSE); | |
513 | if (!svp) { | |
514 | SV *sv = NEWSV(0, 0); | |
515 | av_store(thr->magicals, key, sv); | |
516 | /* | |
517 | * Some magic variables used to be automagically initialised | |
518 | * in gv_fetchpv. Those which are now per-thread magicals get | |
519 | * initialised here instead. | |
520 | */ | |
521 | switch (*name) { | |
522 | case ';': | |
523 | sv_setpv(sv, "\034"); | |
524 | break; | |
c277df42 IZ |
525 | case '&': |
526 | case '`': | |
527 | case '\'': | |
528 | sawampersand = TRUE; | |
529 | SvREADONLY_on(sv); | |
530 | break; | |
554b3eca | 531 | } |
a863c7d1 MB |
532 | sv_magic(sv, 0, 0, name, 1); |
533 | DEBUG_L(PerlIO_printf(PerlIO_stderr(), | |
554b3eca MB |
534 | "find_thread_magical: new SV %p for $%s%c\n", |
535 | sv, (*name < 32) ? "^" : "", | |
536 | (*name < 32) ? toCTRL(*name) : *name)); | |
a863c7d1 MB |
537 | } |
538 | return key; | |
539 | } | |
540 | #endif /* USE_THREADS */ | |
541 | ||
79072805 LW |
542 | /* Destructor */ |
543 | ||
544 | void | |
8ac85365 | 545 | op_free(OP *o) |
79072805 | 546 | { |
85e6fe83 | 547 | register OP *kid, *nextkid; |
79072805 | 548 | |
5dc0d613 | 549 | if (!o || o->op_seq == (U16)-1) |
79072805 LW |
550 | return; |
551 | ||
11343788 MB |
552 | if (o->op_flags & OPf_KIDS) { |
553 | for (kid = cUNOPo->op_first; kid; kid = nextkid) { | |
85e6fe83 | 554 | nextkid = kid->op_sibling; /* Get before next freeing kid */ |
79072805 | 555 | op_free(kid); |
85e6fe83 | 556 | } |
79072805 LW |
557 | } |
558 | ||
11343788 | 559 | switch (o->op_type) { |
8990e307 | 560 | case OP_NULL: |
11343788 | 561 | o->op_targ = 0; /* Was holding old type, if any. */ |
8990e307 | 562 | break; |
a0d0e21e | 563 | case OP_ENTEREVAL: |
11343788 | 564 | o->op_targ = 0; /* Was holding hints. */ |
a0d0e21e | 565 | break; |
554b3eca | 566 | #ifdef USE_THREADS |
2faa37cc | 567 | case OP_THREADSV: |
554b3eca MB |
568 | o->op_targ = 0; /* Was holding index into thr->magicals AV. */ |
569 | break; | |
570 | #endif /* USE_THREADS */ | |
a6006777 | 571 | default: |
5196be3e | 572 | if (!(o->op_flags & OPf_REF) || (check[o->op_type] != ck_ftst)) |
a6006777 | 573 | break; |
574 | /* FALL THROUGH */ | |
463ee0b2 | 575 | case OP_GVSV: |
79072805 | 576 | case OP_GV: |
a6006777 | 577 | case OP_AELEMFAST: |
11343788 | 578 | SvREFCNT_dec(cGVOPo->op_gv); |
8990e307 LW |
579 | break; |
580 | case OP_NEXTSTATE: | |
581 | case OP_DBSTATE: | |
5196be3e | 582 | Safefree(cCOPo->cop_label); |
11343788 | 583 | SvREFCNT_dec(cCOPo->cop_filegv); |
79072805 LW |
584 | break; |
585 | case OP_CONST: | |
11343788 | 586 | SvREFCNT_dec(cSVOPo->op_sv); |
79072805 | 587 | break; |
748a9306 LW |
588 | case OP_GOTO: |
589 | case OP_NEXT: | |
590 | case OP_LAST: | |
591 | case OP_REDO: | |
11343788 | 592 | if (o->op_flags & (OPf_SPECIAL|OPf_STACKED|OPf_KIDS)) |
748a9306 LW |
593 | break; |
594 | /* FALL THROUGH */ | |
a0d0e21e | 595 | case OP_TRANS: |
11343788 | 596 | Safefree(cPVOPo->op_pv); |
a0d0e21e LW |
597 | break; |
598 | case OP_SUBST: | |
11343788 | 599 | op_free(cPMOPo->op_pmreplroot); |
a0d0e21e | 600 | /* FALL THROUGH */ |
748a9306 | 601 | case OP_PUSHRE: |
a0d0e21e | 602 | case OP_MATCH: |
c277df42 | 603 | ReREFCNT_dec(cPMOPo->op_pmregexp); |
a0d0e21e | 604 | break; |
79072805 LW |
605 | } |
606 | ||
11343788 MB |
607 | if (o->op_targ > 0) |
608 | pad_free(o->op_targ); | |
8990e307 | 609 | |
11343788 | 610 | Safefree(o); |
79072805 LW |
611 | } |
612 | ||
8990e307 | 613 | static void |
8ac85365 | 614 | null(OP *o) |
8990e307 | 615 | { |
11343788 MB |
616 | if (o->op_type != OP_NULL && o->op_targ > 0) |
617 | pad_free(o->op_targ); | |
618 | o->op_targ = o->op_type; | |
619 | o->op_type = OP_NULL; | |
620 | o->op_ppaddr = ppaddr[OP_NULL]; | |
8990e307 LW |
621 | } |
622 | ||
79072805 LW |
623 | /* Contextualizers */ |
624 | ||
463ee0b2 | 625 | #define LINKLIST(o) ((o)->op_next ? (o)->op_next : linklist((OP*)o)) |
79072805 LW |
626 | |
627 | OP * | |
8ac85365 | 628 | linklist(OP *o) |
79072805 LW |
629 | { |
630 | register OP *kid; | |
631 | ||
11343788 MB |
632 | if (o->op_next) |
633 | return o->op_next; | |
79072805 LW |
634 | |
635 | /* establish postfix order */ | |
11343788 MB |
636 | if (cUNOPo->op_first) { |
637 | o->op_next = LINKLIST(cUNOPo->op_first); | |
638 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) { | |
79072805 LW |
639 | if (kid->op_sibling) |
640 | kid->op_next = LINKLIST(kid->op_sibling); | |
641 | else | |
11343788 | 642 | kid->op_next = o; |
79072805 LW |
643 | } |
644 | } | |
645 | else | |
11343788 | 646 | o->op_next = o; |
79072805 | 647 | |
11343788 | 648 | return o->op_next; |
79072805 LW |
649 | } |
650 | ||
651 | OP * | |
8ac85365 | 652 | scalarkids(OP *o) |
79072805 LW |
653 | { |
654 | OP *kid; | |
11343788 MB |
655 | if (o && o->op_flags & OPf_KIDS) { |
656 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
79072805 LW |
657 | scalar(kid); |
658 | } | |
11343788 | 659 | return o; |
79072805 LW |
660 | } |
661 | ||
a0d0e21e | 662 | static OP * |
8ac85365 | 663 | scalarboolean(OP *o) |
8990e307 LW |
664 | { |
665 | if (dowarn && | |
11343788 | 666 | o->op_type == OP_SASSIGN && cBINOPo->op_first->op_type == OP_CONST) { |
0f15f207 | 667 | dTHR; |
a0d0e21e LW |
668 | line_t oldline = curcop->cop_line; |
669 | ||
670 | if (copline != NOLINE) | |
671 | curcop->cop_line = copline; | |
672 | warn("Found = in conditional, should be =="); | |
673 | curcop->cop_line = oldline; | |
674 | } | |
11343788 | 675 | return scalar(o); |
8990e307 LW |
676 | } |
677 | ||
678 | OP * | |
8ac85365 | 679 | scalar(OP *o) |
79072805 LW |
680 | { |
681 | OP *kid; | |
682 | ||
a0d0e21e | 683 | /* assumes no premature commitment */ |
5dc0d613 MB |
684 | if (!o || (o->op_flags & OPf_WANT) || error_count |
685 | || o->op_type == OP_RETURN) | |
11343788 | 686 | return o; |
79072805 | 687 | |
5dc0d613 | 688 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_SCALAR; |
79072805 | 689 | |
11343788 | 690 | switch (o->op_type) { |
79072805 | 691 | case OP_REPEAT: |
11343788 MB |
692 | if (o->op_private & OPpREPEAT_DOLIST) |
693 | null(((LISTOP*)cBINOPo->op_first)->op_first); | |
694 | scalar(cBINOPo->op_first); | |
8990e307 | 695 | break; |
79072805 LW |
696 | case OP_OR: |
697 | case OP_AND: | |
698 | case OP_COND_EXPR: | |
11343788 | 699 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
8990e307 | 700 | scalar(kid); |
79072805 | 701 | break; |
a0d0e21e | 702 | case OP_SPLIT: |
11343788 | 703 | if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) { |
a0d0e21e LW |
704 | if (!kPMOP->op_pmreplroot) |
705 | deprecate("implicit split to @_"); | |
706 | } | |
707 | /* FALL THROUGH */ | |
79072805 LW |
708 | case OP_MATCH: |
709 | case OP_SUBST: | |
710 | case OP_NULL: | |
8990e307 | 711 | default: |
11343788 MB |
712 | if (o->op_flags & OPf_KIDS) { |
713 | for (kid = cUNOPo->op_first; kid; kid = kid->op_sibling) | |
8990e307 LW |
714 | scalar(kid); |
715 | } | |
79072805 LW |
716 | break; |
717 | case OP_LEAVE: | |
718 | case OP_LEAVETRY: | |
5dc0d613 | 719 | kid = cLISTOPo->op_first; |
54310121 | 720 | scalar(kid); |
721 | while (kid = kid->op_sibling) { | |
722 | if (kid->op_sibling) | |
723 | scalarvoid(kid); | |
724 | else | |
725 | scalar(kid); | |
726 | } | |
0f15f207 | 727 | WITH_THR(curcop = &compiling); |
54310121 | 728 | break; |
748a9306 | 729 | case OP_SCOPE: |
79072805 | 730 | case OP_LINESEQ: |
8990e307 | 731 | case OP_LIST: |
11343788 | 732 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
79072805 LW |
733 | if (kid->op_sibling) |
734 | scalarvoid(kid); | |
735 | else | |
736 | scalar(kid); | |
737 | } | |
0f15f207 | 738 | WITH_THR(curcop = &compiling); |
79072805 LW |
739 | break; |
740 | } | |
11343788 | 741 | return o; |
79072805 LW |
742 | } |
743 | ||
744 | OP * | |
8ac85365 | 745 | scalarvoid(OP *o) |
79072805 LW |
746 | { |
747 | OP *kid; | |
8990e307 LW |
748 | char* useless = 0; |
749 | SV* sv; | |
79072805 | 750 | |
54310121 | 751 | /* assumes no premature commitment */ |
5dc0d613 MB |
752 | if (!o || (o->op_flags & OPf_WANT) == OPf_WANT_LIST || error_count |
753 | || o->op_type == OP_RETURN) | |
11343788 | 754 | return o; |
79072805 | 755 | |
5dc0d613 | 756 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_VOID; |
79072805 | 757 | |
11343788 | 758 | switch (o->op_type) { |
79072805 | 759 | default: |
11343788 | 760 | if (!(opargs[o->op_type] & OA_FOLDCONST)) |
8990e307 | 761 | break; |
36477c24 | 762 | /* FALL THROUGH */ |
763 | case OP_REPEAT: | |
11343788 | 764 | if (o->op_flags & OPf_STACKED) |
8990e307 LW |
765 | break; |
766 | /* FALL THROUGH */ | |
767 | case OP_GVSV: | |
768 | case OP_WANTARRAY: | |
769 | case OP_GV: | |
770 | case OP_PADSV: | |
771 | case OP_PADAV: | |
772 | case OP_PADHV: | |
773 | case OP_PADANY: | |
774 | case OP_AV2ARYLEN: | |
8990e307 | 775 | case OP_REF: |
a0d0e21e LW |
776 | case OP_REFGEN: |
777 | case OP_SREFGEN: | |
8990e307 LW |
778 | case OP_DEFINED: |
779 | case OP_HEX: | |
780 | case OP_OCT: | |
781 | case OP_LENGTH: | |
782 | case OP_SUBSTR: | |
783 | case OP_VEC: | |
784 | case OP_INDEX: | |
785 | case OP_RINDEX: | |
786 | case OP_SPRINTF: | |
787 | case OP_AELEM: | |
788 | case OP_AELEMFAST: | |
789 | case OP_ASLICE: | |
8990e307 LW |
790 | case OP_HELEM: |
791 | case OP_HSLICE: | |
792 | case OP_UNPACK: | |
793 | case OP_PACK: | |
8990e307 LW |
794 | case OP_JOIN: |
795 | case OP_LSLICE: | |
796 | case OP_ANONLIST: | |
797 | case OP_ANONHASH: | |
798 | case OP_SORT: | |
799 | case OP_REVERSE: | |
800 | case OP_RANGE: | |
801 | case OP_FLIP: | |
802 | case OP_FLOP: | |
803 | case OP_CALLER: | |
804 | case OP_FILENO: | |
805 | case OP_EOF: | |
806 | case OP_TELL: | |
807 | case OP_GETSOCKNAME: | |
808 | case OP_GETPEERNAME: | |
809 | case OP_READLINK: | |
810 | case OP_TELLDIR: | |
811 | case OP_GETPPID: | |
812 | case OP_GETPGRP: | |
813 | case OP_GETPRIORITY: | |
814 | case OP_TIME: | |
815 | case OP_TMS: | |
816 | case OP_LOCALTIME: | |
817 | case OP_GMTIME: | |
818 | case OP_GHBYNAME: | |
819 | case OP_GHBYADDR: | |
820 | case OP_GHOSTENT: | |
821 | case OP_GNBYNAME: | |
822 | case OP_GNBYADDR: | |
823 | case OP_GNETENT: | |
824 | case OP_GPBYNAME: | |
825 | case OP_GPBYNUMBER: | |
826 | case OP_GPROTOENT: | |
827 | case OP_GSBYNAME: | |
828 | case OP_GSBYPORT: | |
829 | case OP_GSERVENT: | |
830 | case OP_GPWNAM: | |
831 | case OP_GPWUID: | |
832 | case OP_GGRNAM: | |
833 | case OP_GGRGID: | |
834 | case OP_GETLOGIN: | |
11343788 MB |
835 | if (!(o->op_private & OPpLVAL_INTRO)) |
836 | useless = op_desc[o->op_type]; | |
8990e307 LW |
837 | break; |
838 | ||
839 | case OP_RV2GV: | |
840 | case OP_RV2SV: | |
841 | case OP_RV2AV: | |
842 | case OP_RV2HV: | |
11343788 MB |
843 | if (!(o->op_private & OPpLVAL_INTRO) && |
844 | (!o->op_sibling || o->op_sibling->op_type != OP_READLINE)) | |
8990e307 LW |
845 | useless = "a variable"; |
846 | break; | |
79072805 | 847 | |
93a17b20 | 848 | case OP_NEXTSTATE: |
8990e307 | 849 | case OP_DBSTATE: |
0f15f207 | 850 | WITH_THR(curcop = ((COP*)o)); /* for warning below */ |
93a17b20 LW |
851 | break; |
852 | ||
79072805 | 853 | case OP_CONST: |
11343788 | 854 | sv = cSVOPo->op_sv; |
8990e307 LW |
855 | if (dowarn) { |
856 | useless = "a constant"; | |
857 | if (SvNIOK(sv) && (SvNV(sv) == 0.0 || SvNV(sv) == 1.0)) | |
858 | useless = 0; | |
859 | else if (SvPOK(sv)) { | |
860 | if (strnEQ(SvPVX(sv), "di", 2) || | |
a0d0e21e | 861 | strnEQ(SvPVX(sv), "ds", 2) || |
8990e307 LW |
862 | strnEQ(SvPVX(sv), "ig", 2)) |
863 | useless = 0; | |
864 | } | |
865 | } | |
11343788 | 866 | null(o); /* don't execute a constant */ |
8990e307 | 867 | SvREFCNT_dec(sv); /* don't even remember it */ |
79072805 LW |
868 | break; |
869 | ||
870 | case OP_POSTINC: | |
11343788 MB |
871 | o->op_type = OP_PREINC; /* pre-increment is faster */ |
872 | o->op_ppaddr = ppaddr[OP_PREINC]; | |
79072805 LW |
873 | break; |
874 | ||
875 | case OP_POSTDEC: | |
11343788 MB |
876 | o->op_type = OP_PREDEC; /* pre-decrement is faster */ |
877 | o->op_ppaddr = ppaddr[OP_PREDEC]; | |
79072805 LW |
878 | break; |
879 | ||
79072805 LW |
880 | case OP_OR: |
881 | case OP_AND: | |
882 | case OP_COND_EXPR: | |
11343788 | 883 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
79072805 LW |
884 | scalarvoid(kid); |
885 | break; | |
5aabfad6 | 886 | |
a0d0e21e | 887 | case OP_NULL: |
11343788 | 888 | if (o->op_targ == OP_NEXTSTATE || o->op_targ == OP_DBSTATE) |
0f15f207 | 889 | WITH_THR(curcop = ((COP*)o)); /* for warning below */ |
11343788 | 890 | if (o->op_flags & OPf_STACKED) |
a0d0e21e | 891 | break; |
5aabfad6 | 892 | /* FALL THROUGH */ |
79072805 LW |
893 | case OP_ENTERTRY: |
894 | case OP_ENTER: | |
895 | case OP_SCALAR: | |
11343788 | 896 | if (!(o->op_flags & OPf_KIDS)) |
79072805 | 897 | break; |
54310121 | 898 | /* FALL THROUGH */ |
463ee0b2 | 899 | case OP_SCOPE: |
79072805 LW |
900 | case OP_LEAVE: |
901 | case OP_LEAVETRY: | |
a0d0e21e | 902 | case OP_LEAVELOOP: |
79072805 | 903 | case OP_LINESEQ: |
79072805 | 904 | case OP_LIST: |
11343788 | 905 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
79072805 LW |
906 | scalarvoid(kid); |
907 | break; | |
c90c0ff4 | 908 | case OP_ENTEREVAL: |
5196be3e | 909 | scalarkids(o); |
c90c0ff4 | 910 | break; |
5aabfad6 | 911 | case OP_REQUIRE: |
c90c0ff4 | 912 | /* all requires must return a boolean value */ |
5196be3e MB |
913 | o->op_flags &= ~OPf_WANT; |
914 | return scalar(o); | |
a0d0e21e | 915 | case OP_SPLIT: |
11343788 | 916 | if ((kid = cLISTOPo->op_first) && kid->op_type == OP_PUSHRE) { |
a0d0e21e LW |
917 | if (!kPMOP->op_pmreplroot) |
918 | deprecate("implicit split to @_"); | |
919 | } | |
920 | break; | |
79072805 | 921 | } |
8990e307 LW |
922 | if (useless && dowarn) |
923 | warn("Useless use of %s in void context", useless); | |
11343788 | 924 | return o; |
79072805 LW |
925 | } |
926 | ||
927 | OP * | |
8ac85365 | 928 | listkids(OP *o) |
79072805 LW |
929 | { |
930 | OP *kid; | |
11343788 MB |
931 | if (o && o->op_flags & OPf_KIDS) { |
932 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
79072805 LW |
933 | list(kid); |
934 | } | |
11343788 | 935 | return o; |
79072805 LW |
936 | } |
937 | ||
938 | OP * | |
8ac85365 | 939 | list(OP *o) |
79072805 LW |
940 | { |
941 | OP *kid; | |
942 | ||
a0d0e21e | 943 | /* assumes no premature commitment */ |
5dc0d613 MB |
944 | if (!o || (o->op_flags & OPf_WANT) || error_count |
945 | || o->op_type == OP_RETURN) | |
11343788 | 946 | return o; |
79072805 | 947 | |
5dc0d613 | 948 | o->op_flags = (o->op_flags & ~OPf_WANT) | OPf_WANT_LIST; |
79072805 | 949 | |
11343788 | 950 | switch (o->op_type) { |
79072805 LW |
951 | case OP_FLOP: |
952 | case OP_REPEAT: | |
11343788 | 953 | list(cBINOPo->op_first); |
79072805 LW |
954 | break; |
955 | case OP_OR: | |
956 | case OP_AND: | |
957 | case OP_COND_EXPR: | |
11343788 | 958 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
79072805 LW |
959 | list(kid); |
960 | break; | |
961 | default: | |
962 | case OP_MATCH: | |
963 | case OP_SUBST: | |
964 | case OP_NULL: | |
11343788 | 965 | if (!(o->op_flags & OPf_KIDS)) |
79072805 | 966 | break; |
11343788 MB |
967 | if (!o->op_next && cUNOPo->op_first->op_type == OP_FLOP) { |
968 | list(cBINOPo->op_first); | |
969 | return gen_constant_list(o); | |
79072805 LW |
970 | } |
971 | case OP_LIST: | |
11343788 | 972 | listkids(o); |
79072805 LW |
973 | break; |
974 | case OP_LEAVE: | |
975 | case OP_LEAVETRY: | |
5dc0d613 | 976 | kid = cLISTOPo->op_first; |
54310121 | 977 | list(kid); |
978 | while (kid = kid->op_sibling) { | |
979 | if (kid->op_sibling) | |
980 | scalarvoid(kid); | |
981 | else | |
982 | list(kid); | |
983 | } | |
0f15f207 | 984 | WITH_THR(curcop = &compiling); |
54310121 | 985 | break; |
748a9306 | 986 | case OP_SCOPE: |
79072805 | 987 | case OP_LINESEQ: |
11343788 | 988 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
79072805 LW |
989 | if (kid->op_sibling) |
990 | scalarvoid(kid); | |
991 | else | |
992 | list(kid); | |
993 | } | |
0f15f207 | 994 | WITH_THR(curcop = &compiling); |
79072805 | 995 | break; |
c90c0ff4 | 996 | case OP_REQUIRE: |
997 | /* all requires must return a boolean value */ | |
5196be3e MB |
998 | o->op_flags &= ~OPf_WANT; |
999 | return scalar(o); | |
79072805 | 1000 | } |
11343788 | 1001 | return o; |
79072805 LW |
1002 | } |
1003 | ||
1004 | OP * | |
8ac85365 | 1005 | scalarseq(OP *o) |
79072805 LW |
1006 | { |
1007 | OP *kid; | |
1008 | ||
11343788 MB |
1009 | if (o) { |
1010 | if (o->op_type == OP_LINESEQ || | |
1011 | o->op_type == OP_SCOPE || | |
1012 | o->op_type == OP_LEAVE || | |
1013 | o->op_type == OP_LEAVETRY) | |
463ee0b2 | 1014 | { |
0f15f207 | 1015 | dTHR; |
11343788 | 1016 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) { |
ed6116ce | 1017 | if (kid->op_sibling) { |
463ee0b2 | 1018 | scalarvoid(kid); |
ed6116ce | 1019 | } |
463ee0b2 LW |
1020 | } |
1021 | curcop = &compiling; | |
79072805 | 1022 | } |
11343788 | 1023 | o->op_flags &= ~OPf_PARENS; |
85e6fe83 | 1024 | if (hints & HINT_BLOCK_SCOPE) |
11343788 | 1025 | o->op_flags |= OPf_PARENS; |
79072805 | 1026 | } |
8990e307 | 1027 | else |
11343788 MB |
1028 | o = newOP(OP_STUB, 0); |
1029 | return o; | |
79072805 LW |
1030 | } |
1031 | ||
a0d0e21e | 1032 | static OP * |
8ac85365 | 1033 | modkids(OP *o, I32 type) |
79072805 LW |
1034 | { |
1035 | OP *kid; | |
11343788 MB |
1036 | if (o && o->op_flags & OPf_KIDS) { |
1037 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
463ee0b2 | 1038 | mod(kid, type); |
79072805 | 1039 | } |
11343788 | 1040 | return o; |
79072805 LW |
1041 | } |
1042 | ||
463ee0b2 | 1043 | static I32 modcount; |
79072805 LW |
1044 | |
1045 | OP * | |
8ac85365 | 1046 | mod(OP *o, I32 type) |
79072805 | 1047 | { |
11343788 | 1048 | dTHR; |
79072805 LW |
1049 | OP *kid; |
1050 | SV *sv; | |
1051 | ||
11343788 MB |
1052 | if (!o || error_count) |
1053 | return o; | |
79072805 | 1054 | |
11343788 | 1055 | switch (o->op_type) { |
68dc0745 | 1056 | case OP_UNDEF: |
3e3baf6d | 1057 | modcount++; |
5dc0d613 | 1058 | return o; |
a0d0e21e | 1059 | case OP_CONST: |
11343788 | 1060 | if (!(o->op_private & (OPpCONST_ARYBASE))) |
a0d0e21e LW |
1061 | goto nomod; |
1062 | if (eval_start && eval_start->op_type == OP_CONST) { | |
1063 | compiling.cop_arybase = (I32)SvIV(((SVOP*)eval_start)->op_sv); | |
1064 | eval_start = 0; | |
1065 | } | |
1066 | else if (!type) { | |
1067 | SAVEI32(compiling.cop_arybase); | |
748a9306 | 1068 | compiling.cop_arybase = 0; |
a0d0e21e LW |
1069 | } |
1070 | else if (type == OP_REFGEN) | |
1071 | goto nomod; | |
1072 | else | |
1073 | croak("That use of $[ is unsupported"); | |
1074 | break; | |
5f05dabc | 1075 | case OP_STUB: |
5196be3e | 1076 | if (o->op_flags & OPf_PARENS) |
5f05dabc | 1077 | break; |
1078 | goto nomod; | |
a0d0e21e LW |
1079 | case OP_ENTERSUB: |
1080 | if ((type == OP_UNDEF || type == OP_REFGEN) && | |
11343788 MB |
1081 | !(o->op_flags & OPf_STACKED)) { |
1082 | o->op_type = OP_RV2CV; /* entersub => rv2cv */ | |
1083 | o->op_ppaddr = ppaddr[OP_RV2CV]; | |
1084 | assert(cUNOPo->op_first->op_type == OP_NULL); | |
1085 | null(((LISTOP*)cUNOPo->op_first)->op_first);/* disable pushmark */ | |
79072805 LW |
1086 | break; |
1087 | } | |
1088 | /* FALL THROUGH */ | |
1089 | default: | |
a0d0e21e LW |
1090 | nomod: |
1091 | /* grep, foreach, subcalls, refgen */ | |
1092 | if (type == OP_GREPSTART || type == OP_ENTERSUB || type == OP_REFGEN) | |
1093 | break; | |
46fc3d4c | 1094 | yyerror(form("Can't modify %s in %s", |
5dc0d613 | 1095 | op_desc[o->op_type], |
46fc3d4c | 1096 | type ? op_desc[type] : "local")); |
11343788 | 1097 | return o; |
79072805 | 1098 | |
a0d0e21e LW |
1099 | case OP_PREINC: |
1100 | case OP_PREDEC: | |
1101 | case OP_POW: | |
1102 | case OP_MULTIPLY: | |
1103 | case OP_DIVIDE: | |
1104 | case OP_MODULO: | |
1105 | case OP_REPEAT: | |
1106 | case OP_ADD: | |
1107 | case OP_SUBTRACT: | |
1108 | case OP_CONCAT: | |
1109 | case OP_LEFT_SHIFT: | |
1110 | case OP_RIGHT_SHIFT: | |
1111 | case OP_BIT_AND: | |
1112 | case OP_BIT_XOR: | |
1113 | case OP_BIT_OR: | |
1114 | case OP_I_MULTIPLY: | |
1115 | case OP_I_DIVIDE: | |
1116 | case OP_I_MODULO: | |
1117 | case OP_I_ADD: | |
1118 | case OP_I_SUBTRACT: | |
11343788 | 1119 | if (!(o->op_flags & OPf_STACKED)) |
a0d0e21e LW |
1120 | goto nomod; |
1121 | modcount++; | |
1122 | break; | |
1123 | ||
79072805 | 1124 | case OP_COND_EXPR: |
11343788 | 1125 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
463ee0b2 | 1126 | mod(kid, type); |
79072805 LW |
1127 | break; |
1128 | ||
1129 | case OP_RV2AV: | |
1130 | case OP_RV2HV: | |
93af7a87 | 1131 | if (!type && cUNOPo->op_first->op_type != OP_GV) |
706a304b | 1132 | croak("Can't localize through a reference"); |
11343788 | 1133 | if (type == OP_REFGEN && o->op_flags & OPf_PARENS) { |
748a9306 | 1134 | modcount = 10000; |
11343788 | 1135 | return o; /* Treat \(@foo) like ordinary list. */ |
748a9306 LW |
1136 | } |
1137 | /* FALL THROUGH */ | |
79072805 | 1138 | case OP_RV2GV: |
5dc0d613 | 1139 | if (scalar_mod_type(o, type)) |
3fe9a6f1 | 1140 | goto nomod; |
11343788 | 1141 | ref(cUNOPo->op_first, o->op_type); |
79072805 LW |
1142 | /* FALL THROUGH */ |
1143 | case OP_AASSIGN: | |
1144 | case OP_ASLICE: | |
1145 | case OP_HSLICE: | |
93a17b20 LW |
1146 | case OP_NEXTSTATE: |
1147 | case OP_DBSTATE: | |
a0d0e21e LW |
1148 | case OP_REFGEN: |
1149 | case OP_CHOMP: | |
463ee0b2 | 1150 | modcount = 10000; |
79072805 | 1151 | break; |
463ee0b2 | 1152 | case OP_RV2SV: |
11343788 | 1153 | if (!type && cUNOPo->op_first->op_type != OP_GV) |
706a304b | 1154 | croak("Can't localize through a reference"); |
aeea060c | 1155 | ref(cUNOPo->op_first, o->op_type); |
463ee0b2 | 1156 | /* FALL THROUGH */ |
79072805 | 1157 | case OP_GV: |
463ee0b2 LW |
1158 | case OP_AV2ARYLEN: |
1159 | case OP_SASSIGN: | |
8990e307 LW |
1160 | case OP_AELEMFAST: |
1161 | modcount++; | |
1162 | break; | |
1163 | ||
748a9306 LW |
1164 | case OP_PADAV: |
1165 | case OP_PADHV: | |
1166 | modcount = 10000; | |
5196be3e MB |
1167 | if (type == OP_REFGEN && o->op_flags & OPf_PARENS) |
1168 | return o; /* Treat \(@foo) like ordinary list. */ | |
1169 | if (scalar_mod_type(o, type)) | |
3fe9a6f1 | 1170 | goto nomod; |
748a9306 LW |
1171 | /* FALL THROUGH */ |
1172 | case OP_PADSV: | |
1173 | modcount++; | |
1174 | if (!type) | |
1175 | croak("Can't localize lexical variable %s", | |
11343788 | 1176 | SvPV(*av_fetch(comppad_name, o->op_targ, 4), na)); |
463ee0b2 LW |
1177 | break; |
1178 | ||
554b3eca | 1179 | #ifdef USE_THREADS |
2faa37cc | 1180 | case OP_THREADSV: |
554b3eca | 1181 | modcount++; /* XXX ??? */ |
554b3eca MB |
1182 | break; |
1183 | #endif /* USE_THREADS */ | |
1184 | ||
748a9306 LW |
1185 | case OP_PUSHMARK: |
1186 | break; | |
a0d0e21e | 1187 | |
69969c6f SB |
1188 | case OP_KEYS: |
1189 | if (type != OP_SASSIGN) | |
1190 | goto nomod; | |
5f05dabc | 1191 | /* FALL THROUGH */ |
a0d0e21e | 1192 | case OP_POS: |
463ee0b2 | 1193 | case OP_VEC: |
a0d0e21e | 1194 | case OP_SUBSTR: |
11343788 MB |
1195 | pad_free(o->op_targ); |
1196 | o->op_targ = pad_alloc(o->op_type, SVs_PADMY); | |
5dc0d613 | 1197 | assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL); |
11343788 MB |
1198 | if (o->op_flags & OPf_KIDS) |
1199 | mod(cBINOPo->op_first->op_sibling, type); | |
463ee0b2 | 1200 | break; |
a0d0e21e | 1201 | |
463ee0b2 LW |
1202 | case OP_AELEM: |
1203 | case OP_HELEM: | |
11343788 | 1204 | ref(cBINOPo->op_first, o->op_type); |
68dc0745 | 1205 | if (type == OP_ENTERSUB && |
5dc0d613 MB |
1206 | !(o->op_private & (OPpLVAL_INTRO | OPpDEREF))) |
1207 | o->op_private |= OPpLVAL_DEFER; | |
a0d0e21e | 1208 | modcount++; |
463ee0b2 LW |
1209 | break; |
1210 | ||
1211 | case OP_SCOPE: | |
1212 | case OP_LEAVE: | |
1213 | case OP_ENTER: | |
11343788 MB |
1214 | if (o->op_flags & OPf_KIDS) |
1215 | mod(cLISTOPo->op_last, type); | |
a0d0e21e LW |
1216 | break; |
1217 | ||
1218 | case OP_NULL: | |
11343788 | 1219 | if (!(o->op_flags & OPf_KIDS)) |
463ee0b2 | 1220 | break; |
11343788 MB |
1221 | if (o->op_targ != OP_LIST) { |
1222 | mod(cBINOPo->op_first, type); | |
a0d0e21e LW |
1223 | break; |
1224 | } | |
1225 | /* FALL THROUGH */ | |
463ee0b2 | 1226 | case OP_LIST: |
11343788 | 1227 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
463ee0b2 LW |
1228 | mod(kid, type); |
1229 | break; | |
1230 | } | |
11343788 | 1231 | o->op_flags |= OPf_MOD; |
a0d0e21e LW |
1232 | |
1233 | if (type == OP_AASSIGN || type == OP_SASSIGN) | |
11343788 | 1234 | o->op_flags |= OPf_SPECIAL|OPf_REF; |
a0d0e21e | 1235 | else if (!type) { |
11343788 MB |
1236 | o->op_private |= OPpLVAL_INTRO; |
1237 | o->op_flags &= ~OPf_SPECIAL; | |
463ee0b2 | 1238 | } |
a0d0e21e | 1239 | else if (type != OP_GREPSTART && type != OP_ENTERSUB) |
11343788 MB |
1240 | o->op_flags |= OPf_REF; |
1241 | return o; | |
463ee0b2 LW |
1242 | } |
1243 | ||
3fe9a6f1 | 1244 | static bool |
8ac85365 | 1245 | scalar_mod_type(OP *o, I32 type) |
3fe9a6f1 | 1246 | { |
1247 | switch (type) { | |
1248 | case OP_SASSIGN: | |
5196be3e | 1249 | if (o->op_type == OP_RV2GV) |
3fe9a6f1 | 1250 | return FALSE; |
1251 | /* FALL THROUGH */ | |
1252 | case OP_PREINC: | |
1253 | case OP_PREDEC: | |
1254 | case OP_POSTINC: | |
1255 | case OP_POSTDEC: | |
1256 | case OP_I_PREINC: | |
1257 | case OP_I_PREDEC: | |
1258 | case OP_I_POSTINC: | |
1259 | case OP_I_POSTDEC: | |
1260 | case OP_POW: | |
1261 | case OP_MULTIPLY: | |
1262 | case OP_DIVIDE: | |
1263 | case OP_MODULO: | |
1264 | case OP_REPEAT: | |
1265 | case OP_ADD: | |
1266 | case OP_SUBTRACT: | |
1267 | case OP_I_MULTIPLY: | |
1268 | case OP_I_DIVIDE: | |
1269 | case OP_I_MODULO: | |
1270 | case OP_I_ADD: | |
1271 | case OP_I_SUBTRACT: | |
1272 | case OP_LEFT_SHIFT: | |
1273 | case OP_RIGHT_SHIFT: | |
1274 | case OP_BIT_AND: | |
1275 | case OP_BIT_XOR: | |
1276 | case OP_BIT_OR: | |
1277 | case OP_CONCAT: | |
1278 | case OP_SUBST: | |
1279 | case OP_TRANS: | |
1280 | case OP_ANDASSIGN: /* may work later */ | |
1281 | case OP_ORASSIGN: /* may work later */ | |
1282 | return TRUE; | |
1283 | default: | |
1284 | return FALSE; | |
1285 | } | |
1286 | } | |
1287 | ||
463ee0b2 | 1288 | OP * |
8ac85365 | 1289 | refkids(OP *o, I32 type) |
463ee0b2 LW |
1290 | { |
1291 | OP *kid; | |
11343788 MB |
1292 | if (o && o->op_flags & OPf_KIDS) { |
1293 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) | |
463ee0b2 LW |
1294 | ref(kid, type); |
1295 | } | |
11343788 | 1296 | return o; |
463ee0b2 LW |
1297 | } |
1298 | ||
1299 | OP * | |
8ac85365 | 1300 | ref(OP *o, I32 type) |
463ee0b2 LW |
1301 | { |
1302 | OP *kid; | |
463ee0b2 | 1303 | |
11343788 MB |
1304 | if (!o || error_count) |
1305 | return o; | |
463ee0b2 | 1306 | |
11343788 | 1307 | switch (o->op_type) { |
a0d0e21e | 1308 | case OP_ENTERSUB: |
e55aaa0e | 1309 | if ((type == OP_DEFINED || type == OP_LOCK) && |
11343788 MB |
1310 | !(o->op_flags & OPf_STACKED)) { |
1311 | o->op_type = OP_RV2CV; /* entersub => rv2cv */ | |
1312 | o->op_ppaddr = ppaddr[OP_RV2CV]; | |
1313 | assert(cUNOPo->op_first->op_type == OP_NULL); | |
1314 | null(((LISTOP*)cUNOPo->op_first)->op_first); /* disable pushmark */ | |
1315 | o->op_flags |= OPf_SPECIAL; | |
8990e307 LW |
1316 | } |
1317 | break; | |
aeea060c | 1318 | |
463ee0b2 | 1319 | case OP_COND_EXPR: |
11343788 | 1320 | for (kid = cUNOPo->op_first->op_sibling; kid; kid = kid->op_sibling) |
463ee0b2 LW |
1321 | ref(kid, type); |
1322 | break; | |
8990e307 | 1323 | case OP_RV2SV: |
11343788 | 1324 | ref(cUNOPo->op_first, o->op_type); |
4633a7c4 LW |
1325 | /* FALL THROUGH */ |
1326 | case OP_PADSV: | |
5f05dabc | 1327 | if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { |
5dc0d613 MB |
1328 | o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV |
1329 | : type == OP_RV2HV ? OPpDEREF_HV | |
1330 | : OPpDEREF_SV); | |
11343788 | 1331 | o->op_flags |= OPf_MOD; |
a0d0e21e | 1332 | } |
8990e307 LW |
1333 | break; |
1334 | ||
2faa37cc | 1335 | case OP_THREADSV: |
a863c7d1 MB |
1336 | o->op_flags |= OPf_MOD; /* XXX ??? */ |
1337 | break; | |
1338 | ||
463ee0b2 LW |
1339 | case OP_RV2AV: |
1340 | case OP_RV2HV: | |
aeea060c | 1341 | o->op_flags |= OPf_REF; |
8990e307 | 1342 | /* FALL THROUGH */ |
463ee0b2 | 1343 | case OP_RV2GV: |
11343788 | 1344 | ref(cUNOPo->op_first, o->op_type); |
463ee0b2 | 1345 | break; |
8990e307 | 1346 | |
463ee0b2 LW |
1347 | case OP_PADAV: |
1348 | case OP_PADHV: | |
aeea060c | 1349 | o->op_flags |= OPf_REF; |
79072805 | 1350 | break; |
aeea060c | 1351 | |
8990e307 | 1352 | case OP_SCALAR: |
79072805 | 1353 | case OP_NULL: |
11343788 | 1354 | if (!(o->op_flags & OPf_KIDS)) |
463ee0b2 | 1355 | break; |
11343788 | 1356 | ref(cBINOPo->op_first, type); |
79072805 LW |
1357 | break; |
1358 | case OP_AELEM: | |
1359 | case OP_HELEM: | |
11343788 | 1360 | ref(cBINOPo->op_first, o->op_type); |
5f05dabc | 1361 | if (type == OP_RV2SV || type == OP_RV2AV || type == OP_RV2HV) { |
5dc0d613 MB |
1362 | o->op_private |= (type == OP_RV2AV ? OPpDEREF_AV |
1363 | : type == OP_RV2HV ? OPpDEREF_HV | |
1364 | : OPpDEREF_SV); | |
11343788 | 1365 | o->op_flags |= OPf_MOD; |
8990e307 | 1366 | } |
79072805 LW |
1367 | break; |
1368 | ||
463ee0b2 | 1369 | case OP_SCOPE: |
79072805 LW |
1370 | case OP_LEAVE: |
1371 | case OP_ENTER: | |
8990e307 | 1372 | case OP_LIST: |
11343788 | 1373 | if (!(o->op_flags & OPf_KIDS)) |
79072805 | 1374 | break; |
11343788 | 1375 | ref(cLISTOPo->op_last, type); |
79072805 | 1376 | break; |
a0d0e21e LW |
1377 | default: |
1378 | break; | |
79072805 | 1379 | } |
11343788 | 1380 | return scalar(o); |
8990e307 | 1381 | |
79072805 LW |
1382 | } |
1383 | ||
1384 | OP * | |
8ac85365 | 1385 | my(OP *o) |
93a17b20 LW |
1386 | { |
1387 | OP *kid; | |
93a17b20 LW |
1388 | I32 type; |
1389 | ||
11343788 MB |
1390 | if (!o || error_count) |
1391 | return o; | |
93a17b20 | 1392 | |
11343788 | 1393 | type = o->op_type; |
93a17b20 | 1394 | if (type == OP_LIST) { |
11343788 | 1395 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
93a17b20 LW |
1396 | my(kid); |
1397 | } | |
1398 | else if (type != OP_PADSV && | |
1399 | type != OP_PADAV && | |
1400 | type != OP_PADHV && | |
1401 | type != OP_PUSHMARK) | |
1402 | { | |
5dc0d613 | 1403 | yyerror(form("Can't declare %s in my", op_desc[o->op_type])); |
11343788 | 1404 | return o; |
93a17b20 | 1405 | } |
11343788 MB |
1406 | o->op_flags |= OPf_MOD; |
1407 | o->op_private |= OPpLVAL_INTRO; | |
1408 | return o; | |
93a17b20 LW |
1409 | } |
1410 | ||
1411 | OP * | |
8ac85365 | 1412 | sawparens(OP *o) |
79072805 LW |
1413 | { |
1414 | if (o) | |
1415 | o->op_flags |= OPf_PARENS; | |
1416 | return o; | |
1417 | } | |
1418 | ||
1419 | OP * | |
8ac85365 | 1420 | bind_match(I32 type, OP *left, OP *right) |
79072805 | 1421 | { |
11343788 | 1422 | OP *o; |
79072805 | 1423 | |
2ae324a7 | 1424 | if (dowarn && |
1425 | (left->op_type == OP_RV2AV || | |
1426 | left->op_type == OP_RV2HV || | |
1427 | left->op_type == OP_PADAV || | |
1428 | left->op_type == OP_PADHV)) { | |
1429 | char *desc = op_desc[(right->op_type == OP_SUBST || | |
1430 | right->op_type == OP_TRANS) | |
1431 | ? right->op_type : OP_MATCH]; | |
1432 | char *sample = ((left->op_type == OP_RV2AV || | |
1433 | left->op_type == OP_PADAV) | |
1434 | ? "@array" : "%hash"); | |
1435 | warn("Applying %s to %s will act on scalar(%s)", desc, sample, sample); | |
1436 | } | |
1437 | ||
79072805 LW |
1438 | if (right->op_type == OP_MATCH || |
1439 | right->op_type == OP_SUBST || | |
1440 | right->op_type == OP_TRANS) { | |
1441 | right->op_flags |= OPf_STACKED; | |
1442 | if (right->op_type != OP_MATCH) | |
463ee0b2 | 1443 | left = mod(left, right->op_type); |
79072805 | 1444 | if (right->op_type == OP_TRANS) |
11343788 | 1445 | o = newBINOP(OP_NULL, OPf_STACKED, scalar(left), right); |
79072805 | 1446 | else |
11343788 | 1447 | o = prepend_elem(right->op_type, scalar(left), right); |
79072805 | 1448 | if (type == OP_NOT) |
11343788 MB |
1449 | return newUNOP(OP_NOT, 0, scalar(o)); |
1450 | return o; | |
79072805 LW |
1451 | } |
1452 | else | |
1453 | return bind_match(type, left, | |
1454 | pmruntime(newPMOP(OP_MATCH, 0), right, Nullop)); | |
1455 | } | |
1456 | ||
1457 | OP * | |
8ac85365 | 1458 | invert(OP *o) |
79072805 | 1459 | { |
11343788 MB |
1460 | if (!o) |
1461 | return o; | |
79072805 | 1462 | /* XXX need to optimize away NOT NOT here? Or do we let optimizer do it? */ |
11343788 | 1463 | return newUNOP(OP_NOT, OPf_SPECIAL, scalar(o)); |
79072805 LW |
1464 | } |
1465 | ||
1466 | OP * | |
8ac85365 | 1467 | scope(OP *o) |
79072805 LW |
1468 | { |
1469 | if (o) { | |
84902520 | 1470 | if (o->op_flags & OPf_PARENS || PERLDB_NOOPT || tainting) { |
463ee0b2 LW |
1471 | o = prepend_elem(OP_LINESEQ, newOP(OP_ENTER, 0), o); |
1472 | o->op_type = OP_LEAVE; | |
1473 | o->op_ppaddr = ppaddr[OP_LEAVE]; | |
1474 | } | |
1475 | else { | |
1476 | if (o->op_type == OP_LINESEQ) { | |
1477 | OP *kid; | |
1478 | o->op_type = OP_SCOPE; | |
1479 | o->op_ppaddr = ppaddr[OP_SCOPE]; | |
1480 | kid = ((LISTOP*)o)->op_first; | |
748a9306 LW |
1481 | if (kid->op_type == OP_NEXTSTATE || kid->op_type == OP_DBSTATE){ |
1482 | SvREFCNT_dec(((COP*)kid)->cop_filegv); | |
8990e307 | 1483 | null(kid); |
748a9306 | 1484 | } |
463ee0b2 LW |
1485 | } |
1486 | else | |
748a9306 | 1487 | o = newLISTOP(OP_SCOPE, 0, o, Nullop); |
463ee0b2 | 1488 | } |
79072805 LW |
1489 | } |
1490 | return o; | |
1491 | } | |
1492 | ||
a0d0e21e | 1493 | int |
8ac85365 | 1494 | block_start(int full) |
79072805 | 1495 | { |
11343788 | 1496 | dTHR; |
a0d0e21e | 1497 | int retval = savestack_ix; |
55497cff | 1498 | SAVEI32(comppad_name_floor); |
1499 | if (full) { | |
1500 | if ((comppad_name_fill = AvFILL(comppad_name)) > 0) | |
1501 | comppad_name_floor = comppad_name_fill; | |
1502 | else | |
1503 | comppad_name_floor = 0; | |
1504 | } | |
1505 | SAVEI32(min_intro_pending); | |
1506 | SAVEI32(max_intro_pending); | |
a0d0e21e | 1507 | min_intro_pending = 0; |
55497cff | 1508 | SAVEI32(comppad_name_fill); |
1509 | SAVEI32(padix_floor); | |
a0d0e21e LW |
1510 | padix_floor = padix; |
1511 | pad_reset_pending = FALSE; | |
55497cff | 1512 | SAVEI32(hints); |
a0d0e21e LW |
1513 | hints &= ~HINT_BLOCK_SCOPE; |
1514 | return retval; | |
1515 | } | |
1516 | ||
1517 | OP* | |
8ac85365 | 1518 | block_end(I32 floor, OP *seq) |
a0d0e21e | 1519 | { |
11343788 | 1520 | dTHR; |
a0d0e21e LW |
1521 | int needblockscope = hints & HINT_BLOCK_SCOPE; |
1522 | OP* retval = scalarseq(seq); | |
a0d0e21e LW |
1523 | LEAVE_SCOPE(floor); |
1524 | pad_reset_pending = FALSE; | |
1525 | if (needblockscope) | |
1526 | hints |= HINT_BLOCK_SCOPE; /* propagate out */ | |
1527 | pad_leavemy(comppad_name_fill); | |
bbce6d69 | 1528 | cop_seqmax++; |
a0d0e21e LW |
1529 | return retval; |
1530 | } | |
1531 | ||
1532 | void | |
8ac85365 | 1533 | newPROG(OP *o) |
a0d0e21e | 1534 | { |
11343788 | 1535 | dTHR; |
a0d0e21e | 1536 | if (in_eval) { |
5dc0d613 | 1537 | eval_root = newUNOP(OP_LEAVEEVAL, ((in_eval & 4) ? OPf_SPECIAL : 0), o); |
a0d0e21e LW |
1538 | eval_start = linklist(eval_root); |
1539 | eval_root->op_next = 0; | |
1540 | peep(eval_start); | |
1541 | } | |
1542 | else { | |
5dc0d613 | 1543 | if (!o) |
a0d0e21e | 1544 | return; |
11343788 | 1545 | main_root = scope(sawparens(scalarvoid(o))); |
a0d0e21e LW |
1546 | curcop = &compiling; |
1547 | main_start = LINKLIST(main_root); | |
1548 | main_root->op_next = 0; | |
1549 | peep(main_start); | |
748a9306 | 1550 | compcv = 0; |
3841441e | 1551 | |
4fdae800 | 1552 | /* Register with debugger */ |
84902520 | 1553 | if (PERLDB_INTER) { |
3841441e | 1554 | CV *cv = perl_get_cv("DB::postponed", FALSE); |
3841441e CS |
1555 | if (cv) { |
1556 | dSP; | |
1557 | PUSHMARK(sp); | |
1558 | XPUSHs((SV*)compiling.cop_filegv); | |
1559 | PUTBACK; | |
1560 | perl_call_sv((SV*)cv, G_DISCARD); | |
1561 | } | |
1562 | } | |
79072805 | 1563 | } |
79072805 LW |
1564 | } |
1565 | ||
1566 | OP * | |
8ac85365 | 1567 | localize(OP *o, I32 lex) |
79072805 LW |
1568 | { |
1569 | if (o->op_flags & OPf_PARENS) | |
1570 | list(o); | |
8990e307 | 1571 | else { |
79072805 | 1572 | scalar(o); |
8990e307 LW |
1573 | if (dowarn && bufptr > oldbufptr && bufptr[-1] == ',') { |
1574 | char *s; | |
1575 | for (s = bufptr; *s && (isALNUM(*s) || strchr("@$%, ",*s)); s++) ; | |
a0d0e21e | 1576 | if (*s == ';' || *s == '=') |
8990e307 LW |
1577 | warn("Parens missing around \"%s\" list", lex ? "my" : "local"); |
1578 | } | |
1579 | } | |
93a17b20 | 1580 | in_my = FALSE; |
c750a3ec | 1581 | in_my_stash = Nullhv; |
93a17b20 LW |
1582 | if (lex) |
1583 | return my(o); | |
1584 | else | |
463ee0b2 | 1585 | return mod(o, OP_NULL); /* a bit kludgey */ |
79072805 LW |
1586 | } |
1587 | ||
1588 | OP * | |
8ac85365 | 1589 | jmaybe(OP *o) |
79072805 LW |
1590 | { |
1591 | if (o->op_type == OP_LIST) { | |
554b3eca MB |
1592 | OP *o2; |
1593 | #ifdef USE_THREADS | |
2faa37cc | 1594 | o2 = newOP(OP_THREADSV, 0); |
554b3eca MB |
1595 | o2->op_targ = find_thread_magical(";"); |
1596 | #else | |
1597 | o2 = newSVREF(newGVOP(OP_GV, 0, gv_fetchpv(";", TRUE, SVt_PV))), | |
1598 | #endif /* USE_THREADS */ | |
1599 | o = convert(OP_JOIN, 0, prepend_elem(OP_LIST, o2, o)); | |
79072805 LW |
1600 | } |
1601 | return o; | |
1602 | } | |
1603 | ||
1604 | OP * | |
8ac85365 | 1605 | fold_constants(register OP *o) |
79072805 | 1606 | { |
11343788 | 1607 | dTHR; |
79072805 LW |
1608 | register OP *curop; |
1609 | I32 type = o->op_type; | |
748a9306 | 1610 | SV *sv; |
79072805 LW |
1611 | |
1612 | if (opargs[type] & OA_RETSCALAR) | |
1613 | scalar(o); | |
1614 | if (opargs[type] & OA_TARGET) | |
ed6116ce | 1615 | o->op_targ = pad_alloc(type, SVs_PADTMP); |
79072805 | 1616 | |
85e6fe83 | 1617 | if ((opargs[type] & OA_OTHERINT) && (hints & HINT_INTEGER)) |
a0d0e21e | 1618 | o->op_ppaddr = ppaddr[type = ++(o->op_type)]; |
85e6fe83 | 1619 | |
79072805 LW |
1620 | if (!(opargs[type] & OA_FOLDCONST)) |
1621 | goto nope; | |
1622 | ||
de939608 CS |
1623 | switch (type) { |
1624 | case OP_SPRINTF: | |
1625 | case OP_UCFIRST: | |
1626 | case OP_LCFIRST: | |
1627 | case OP_UC: | |
1628 | case OP_LC: | |
1629 | if (o->op_private & OPpLOCALE) | |
1630 | goto nope; | |
1631 | } | |
1632 | ||
a0d0e21e LW |
1633 | if (error_count) |
1634 | goto nope; /* Don't try to run w/ errors */ | |
1635 | ||
79072805 | 1636 | for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { |
93a17b20 LW |
1637 | if (curop->op_type != OP_CONST && |
1638 | curop->op_type != OP_LIST && | |
1639 | curop->op_type != OP_SCALAR && | |
a0d0e21e | 1640 | curop->op_type != OP_NULL && |
93a17b20 | 1641 | curop->op_type != OP_PUSHMARK) { |
79072805 LW |
1642 | goto nope; |
1643 | } | |
1644 | } | |
1645 | ||
1646 | curop = LINKLIST(o); | |
1647 | o->op_next = 0; | |
1648 | op = curop; | |
8da795c6 | 1649 | runops(); |
748a9306 LW |
1650 | sv = *(stack_sp--); |
1651 | if (o->op_targ && sv == PAD_SV(o->op_targ)) /* grab pad temp? */ | |
79072805 | 1652 | pad_swipe(o->op_targ); |
748a9306 LW |
1653 | else if (SvTEMP(sv)) { /* grab mortal temp? */ |
1654 | (void)SvREFCNT_inc(sv); | |
1655 | SvTEMP_off(sv); | |
85e6fe83 | 1656 | } |
79072805 LW |
1657 | op_free(o); |
1658 | if (type == OP_RV2GV) | |
b1cb66bf | 1659 | return newGVOP(OP_GV, 0, (GV*)sv); |
748a9306 LW |
1660 | else { |
1661 | if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) { | |
1662 | IV iv = SvIV(sv); | |
1663 | if ((double)iv == SvNV(sv)) { /* can we smush double to int */ | |
1664 | SvREFCNT_dec(sv); | |
1665 | sv = newSViv(iv); | |
1666 | } | |
b1cb66bf | 1667 | else |
1668 | SvIOK_off(sv); /* undo SvIV() damage */ | |
748a9306 LW |
1669 | } |
1670 | return newSVOP(OP_CONST, 0, sv); | |
1671 | } | |
aeea060c | 1672 | |
79072805 LW |
1673 | nope: |
1674 | if (!(opargs[type] & OA_OTHERINT)) | |
1675 | return o; | |
79072805 | 1676 | |
85e6fe83 | 1677 | if (!(hints & HINT_INTEGER)) { |
a0d0e21e | 1678 | if (type == OP_DIVIDE || !(o->op_flags & OPf_KIDS)) |
85e6fe83 LW |
1679 | return o; |
1680 | ||
1681 | for (curop = ((UNOP*)o)->op_first; curop; curop = curop->op_sibling) { | |
1682 | if (curop->op_type == OP_CONST) { | |
b1cb66bf | 1683 | if (SvIOK(((SVOP*)curop)->op_sv)) |
85e6fe83 LW |
1684 | continue; |
1685 | return o; | |
1686 | } | |
1687 | if (opargs[curop->op_type] & OA_RETINTEGER) | |
79072805 LW |
1688 | continue; |
1689 | return o; | |
1690 | } | |
a0d0e21e | 1691 | o->op_ppaddr = ppaddr[++(o->op_type)]; |
79072805 LW |
1692 | } |
1693 | ||
79072805 LW |
1694 | return o; |
1695 | } | |
1696 | ||
1697 | OP * | |
8ac85365 | 1698 | gen_constant_list(register OP *o) |
79072805 | 1699 | { |
11343788 | 1700 | dTHR; |
79072805 | 1701 | register OP *curop; |
79072805 | 1702 | I32 oldtmps_floor = tmps_floor; |
79072805 | 1703 | |
a0d0e21e LW |
1704 | list(o); |
1705 | if (error_count) | |
1706 | return o; /* Don't attempt to run with errors */ | |
1707 | ||
1708 | op = curop = LINKLIST(o); | |
1709 | o->op_next = 0; | |
11343788 | 1710 | pp_pushmark(ARGS); |
8da795c6 | 1711 | runops(); |
a0d0e21e | 1712 | op = curop; |
11343788 | 1713 | pp_anonlist(ARGS); |
79072805 | 1714 | tmps_floor = oldtmps_floor; |
79072805 LW |
1715 | |
1716 | o->op_type = OP_RV2AV; | |
1717 | o->op_ppaddr = ppaddr[OP_RV2AV]; | |
79072805 | 1718 | curop = ((UNOP*)o)->op_first; |
a0d0e21e | 1719 | ((UNOP*)o)->op_first = newSVOP(OP_CONST, 0, SvREFCNT_inc(*stack_sp--)); |
79072805 | 1720 | op_free(curop); |
79072805 LW |
1721 | linklist(o); |
1722 | return list(o); | |
1723 | } | |
1724 | ||
1725 | OP * | |
8ac85365 | 1726 | convert(I32 type, I32 flags, OP *o) |
79072805 LW |
1727 | { |
1728 | OP *kid; | |
a0d0e21e | 1729 | OP *last = 0; |
79072805 | 1730 | |
11343788 MB |
1731 | if (!o || o->op_type != OP_LIST) |
1732 | o = newLISTOP(OP_LIST, 0, o, Nullop); | |
748a9306 | 1733 | else |
5dc0d613 | 1734 | o->op_flags &= ~OPf_WANT; |
79072805 | 1735 | |
8990e307 | 1736 | if (!(opargs[type] & OA_MARK)) |
11343788 | 1737 | null(cLISTOPo->op_first); |
8990e307 | 1738 | |
11343788 MB |
1739 | o->op_type = type; |
1740 | o->op_ppaddr = ppaddr[type]; | |
1741 | o->op_flags |= flags; | |
79072805 | 1742 | |
11343788 MB |
1743 | o = CHECKOP(type, o); |
1744 | if (o->op_type != type) | |
1745 | return o; | |
79072805 | 1746 | |
11343788 | 1747 | if (cLISTOPo->op_children < 7) { |
79072805 | 1748 | /* XXX do we really need to do this if we're done appending?? */ |
11343788 | 1749 | for (kid = cLISTOPo->op_first; kid; kid = kid->op_sibling) |
79072805 | 1750 | last = kid; |
11343788 | 1751 | cLISTOPo->op_last = last; /* in case check substituted last arg */ |
79072805 LW |
1752 | } |
1753 | ||
11343788 | 1754 | return fold_constants(o); |
79072805 LW |
1755 | } |
1756 | ||
1757 | /* List constructors */ | |
1758 | ||
1759 | OP * | |
8ac85365 | 1760 | append_elem(I32 type, OP *first, OP *last) |
79072805 LW |
1761 | { |
1762 | if (!first) | |
1763 | return last; | |
8990e307 LW |
1764 | |
1765 | if (!last) | |
79072805 | 1766 | return first; |
8990e307 | 1767 | |
a0d0e21e LW |
1768 | if (first->op_type != type || type==OP_LIST && first->op_flags & OPf_PARENS) |
1769 | return newLISTOP(type, 0, first, last); | |
79072805 | 1770 | |
a0d0e21e LW |
1771 | if (first->op_flags & OPf_KIDS) |
1772 | ((LISTOP*)first)->op_last->op_sibling = last; | |
1773 | else { | |
1774 | first->op_flags |= OPf_KIDS; | |
1775 | ((LISTOP*)first)->op_first = last; | |
1776 | } | |
1777 | ((LISTOP*)first)->op_last = last; | |
1778 | ((LISTOP*)first)->op_children++; | |
1779 | return first; | |
79072805 LW |
1780 | } |
1781 | ||
1782 | OP * | |
8ac85365 | 1783 | append_list(I32 type, LISTOP *first, LISTOP *last) |
79072805 LW |
1784 | { |
1785 | if (!first) | |
1786 | return (OP*)last; | |
8990e307 LW |
1787 | |
1788 | if (!last) | |
79072805 | 1789 | return (OP*)first; |
8990e307 LW |
1790 | |
1791 | if (first->op_type != type) | |
79072805 | 1792 | return prepend_elem(type, (OP*)first, (OP*)last); |
8990e307 LW |
1793 | |
1794 | if (last->op_type != type) | |
79072805 LW |
1795 | return append_elem(type, (OP*)first, (OP*)last); |
1796 | ||
1797 | first->op_last->op_sibling = last->op_first; | |
1798 | first->op_last = last->op_last; | |
1799 | first->op_children += last->op_children; | |
1800 | if (first->op_children) | |
1801 | last->op_flags |= OPf_KIDS; | |
1802 | ||
1803 | Safefree(last); | |
1804 | return (OP*)first; | |
1805 | } | |
1806 | ||
1807 | OP * | |
8ac85365 | 1808 | prepend_elem(I32 type, OP *first, OP *last) |
79072805 LW |
1809 | { |
1810 | if (!first) | |
1811 | return last; | |
8990e307 LW |
1812 | |
1813 | if (!last) | |
79072805 | 1814 | return first; |
8990e307 LW |
1815 | |
1816 | if (last->op_type == type) { | |
1817 | if (type == OP_LIST) { /* already a PUSHMARK there */ | |
1818 | first->op_sibling = ((LISTOP*)last)->op_first->op_sibling; | |
1819 | ((LISTOP*)last)->op_first->op_sibling = first; | |
1820 | } | |
1821 | else { | |
1822 | if (!(last->op_flags & OPf_KIDS)) { | |
1823 | ((LISTOP*)last)->op_last = first; | |
1824 | last->op_flags |= OPf_KIDS; | |
1825 | } | |
1826 | first->op_sibling = ((LISTOP*)last)->op_first; | |
1827 | ((LISTOP*)last)->op_first = first; | |
79072805 | 1828 | } |
79072805 LW |
1829 | ((LISTOP*)last)->op_children++; |
1830 | return last; | |
1831 | } | |
1832 | ||
1833 | return newLISTOP(type, 0, first, last); | |
1834 | } | |
1835 | ||
1836 | /* Constructors */ | |
1837 | ||
1838 | OP * | |
8ac85365 | 1839 | newNULLLIST(void) |
79072805 | 1840 | { |
8990e307 LW |
1841 | return newOP(OP_STUB, 0); |
1842 | } | |
1843 | ||
1844 | OP * | |
8ac85365 | 1845 | force_list(OP *o) |
8990e307 | 1846 | { |
11343788 MB |
1847 | if (!o || o->op_type != OP_LIST) |
1848 | o = newLISTOP(OP_LIST, 0, o, Nullop); | |
1849 | null(o); | |
1850 | return o; | |
79072805 LW |
1851 | } |
1852 | ||
1853 | OP * | |
8ac85365 | 1854 | newLISTOP(I32 type, I32 flags, OP *first, OP *last) |
79072805 LW |
1855 | { |
1856 | LISTOP *listop; | |
1857 | ||
1858 | Newz(1101, listop, 1, LISTOP); | |
1859 | ||
1860 | listop->op_type = type; | |
1861 | listop->op_ppaddr = ppaddr[type]; | |
1862 | listop->op_children = (first != 0) + (last != 0); | |
1863 | listop->op_flags = flags; | |
79072805 LW |
1864 | |
1865 | if (!last && first) | |
1866 | last = first; | |
1867 | else if (!first && last) | |
1868 | first = last; | |
8990e307 LW |
1869 | else if (first) |
1870 | first->op_sibling = last; | |
79072805 LW |
1871 | listop->op_first = first; |
1872 | listop->op_last = last; | |
8990e307 LW |
1873 | if (type == OP_LIST) { |
1874 | OP* pushop; | |
1875 | pushop = newOP(OP_PUSHMARK, 0); | |
1876 | pushop->op_sibling = first; | |
1877 | listop->op_first = pushop; | |
1878 | listop->op_flags |= OPf_KIDS; | |
1879 | if (!last) | |
1880 | listop->op_last = pushop; | |
1881 | } | |
1882 | else if (listop->op_children) | |
1883 | listop->op_flags |= OPf_KIDS; | |
79072805 LW |
1884 | |
1885 | return (OP*)listop; | |
1886 | } | |
1887 | ||
1888 | OP * | |
8ac85365 | 1889 | newOP(I32 type, I32 flags) |
79072805 | 1890 | { |
11343788 MB |
1891 | OP *o; |
1892 | Newz(1101, o, 1, OP); | |
1893 | o->op_type = type; | |
1894 | o->op_ppaddr = ppaddr[type]; | |
1895 | o->op_flags = flags; | |
79072805 | 1896 | |
11343788 MB |
1897 | o->op_next = o; |
1898 | o->op_private = 0 + (flags >> 8); | |
79072805 | 1899 | if (opargs[type] & OA_RETSCALAR) |
11343788 | 1900 | scalar(o); |
79072805 | 1901 | if (opargs[type] & OA_TARGET) |
11343788 MB |
1902 | o->op_targ = pad_alloc(type, SVs_PADTMP); |
1903 | return CHECKOP(type, o); | |
79072805 LW |
1904 | } |
1905 | ||
1906 | OP * | |
8ac85365 | 1907 | newUNOP(I32 type, I32 flags, OP *first) |
79072805 LW |
1908 | { |
1909 | UNOP *unop; | |
1910 | ||
93a17b20 | 1911 | if (!first) |
aeea060c | 1912 | first = newOP(OP_STUB, 0); |
8990e307 LW |
1913 | if (opargs[type] & OA_MARK) |
1914 | first = force_list(first); | |
93a17b20 | 1915 | |
79072805 LW |
1916 | Newz(1101, unop, 1, UNOP); |
1917 | unop->op_type = type; | |
1918 | unop->op_ppaddr = ppaddr[type]; | |
1919 | unop->op_first = first; | |
1920 | unop->op_flags = flags | OPf_KIDS; | |
c07a80fd | 1921 | unop->op_private = 1 | (flags >> 8); |
c277df42 IZ |
1922 | #if 1 |
1923 | if(type == OP_STUDY && first->op_type == OP_MATCH) { | |
1924 | first->op_type = OP_PUSHRE; | |
1925 | first->op_ppaddr = ppaddr[OP_PUSHRE]; | |
1926 | } | |
1927 | #endif | |
e50aee73 | 1928 | unop = (UNOP*) CHECKOP(type, unop); |
79072805 LW |
1929 | if (unop->op_next) |
1930 | return (OP*)unop; | |
1931 | ||
a0d0e21e | 1932 | return fold_constants((OP *) unop); |
79072805 LW |
1933 | } |
1934 | ||
1935 | OP * | |
8ac85365 | 1936 | newBINOP(I32 type, I32 flags, OP *first, OP *last) |
79072805 LW |
1937 | { |
1938 | BINOP *binop; | |
1939 | Newz(1101, binop, 1, BINOP); | |
1940 | ||
1941 | if (!first) | |
1942 | first = newOP(OP_NULL, 0); | |
1943 | ||
1944 | binop->op_type = type; | |
1945 | binop->op_ppaddr = ppaddr[type]; | |
1946 | binop->op_first = first; | |
1947 | binop->op_flags = flags | OPf_KIDS; | |
1948 | if (!last) { | |
1949 | last = first; | |
c07a80fd | 1950 | binop->op_private = 1 | (flags >> 8); |
79072805 LW |
1951 | } |
1952 | else { | |
c07a80fd | 1953 | binop->op_private = 2 | (flags >> 8); |
79072805 LW |
1954 | first->op_sibling = last; |
1955 | } | |
1956 | ||
e50aee73 | 1957 | binop = (BINOP*)CHECKOP(type, binop); |
79072805 LW |
1958 | if (binop->op_next) |
1959 | return (OP*)binop; | |
1960 | ||
1961 | binop->op_last = last = binop->op_first->op_sibling; | |
1962 | ||
a0d0e21e | 1963 | return fold_constants((OP *)binop); |
79072805 LW |
1964 | } |
1965 | ||
1966 | OP * | |
8ac85365 | 1967 | pmtrans(OP *o, OP *expr, OP *repl) |
79072805 | 1968 | { |
79072805 LW |
1969 | SV *tstr = ((SVOP*)expr)->op_sv; |
1970 | SV *rstr = ((SVOP*)repl)->op_sv; | |
463ee0b2 LW |
1971 | STRLEN tlen; |
1972 | STRLEN rlen; | |
ec49126f | 1973 | register U8 *t = (U8*)SvPV(tstr, tlen); |
1974 | register U8 *r = (U8*)SvPV(rstr, rlen); | |
79072805 LW |
1975 | register I32 i; |
1976 | register I32 j; | |
8ac85365 | 1977 | I32 Delete; |
79072805 LW |
1978 | I32 complement; |
1979 | register short *tbl; | |
1980 | ||
11343788 MB |
1981 | tbl = (short*)cPVOPo->op_pv; |
1982 | complement = o->op_private & OPpTRANS_COMPLEMENT; | |
8ac85365 | 1983 | Delete = o->op_private & OPpTRANS_DELETE; |
11343788 | 1984 | /* squash = o->op_private & OPpTRANS_SQUASH; */ |
79072805 LW |
1985 | |
1986 | if (complement) { | |
1987 | Zero(tbl, 256, short); | |
1988 | for (i = 0; i < tlen; i++) | |
ec49126f | 1989 | tbl[t[i]] = -1; |
79072805 LW |
1990 | for (i = 0, j = 0; i < 256; i++) { |
1991 | if (!tbl[i]) { | |
1992 | if (j >= rlen) { | |
8ac85365 | 1993 | if (Delete) |
79072805 LW |
1994 | tbl[i] = -2; |
1995 | else if (rlen) | |
ec49126f | 1996 | tbl[i] = r[j-1]; |
79072805 LW |
1997 | else |
1998 | tbl[i] = i; | |
1999 | } | |
2000 | else | |
ec49126f | 2001 | tbl[i] = r[j++]; |
79072805 LW |
2002 | } |
2003 | } | |
2004 | } | |
2005 | else { | |
8ac85365 | 2006 | if (!rlen && !Delete) { |
79072805 LW |
2007 | r = t; rlen = tlen; |
2008 | } | |
2009 | for (i = 0; i < 256; i++) | |
2010 | tbl[i] = -1; | |
2011 | for (i = 0, j = 0; i < tlen; i++,j++) { | |
2012 | if (j >= rlen) { | |
8ac85365 | 2013 | if (Delete) { |
ec49126f | 2014 | if (tbl[t[i]] == -1) |
2015 | tbl[t[i]] = -2; | |
79072805 LW |
2016 | continue; |
2017 | } | |
2018 | --j; | |
2019 | } | |
ec49126f | 2020 | if (tbl[t[i]] == -1) |
2021 | tbl[t[i]] = r[j]; | |
79072805 LW |
2022 | } |
2023 | } | |
2024 | op_free(expr); | |
2025 | op_free(repl); | |
2026 | ||
11343788 | 2027 | return o; |
79072805 LW |
2028 | } |
2029 | ||
2030 | OP * | |
8ac85365 | 2031 | newPMOP(I32 type, I32 flags) |
79072805 | 2032 | { |
11343788 | 2033 | dTHR; |
79072805 LW |
2034 | PMOP *pmop; |
2035 | ||
2036 | Newz(1101, pmop, 1, PMOP); | |
2037 | pmop->op_type = type; | |
2038 | pmop->op_ppaddr = ppaddr[type]; | |
2039 | pmop->op_flags = flags; | |
c07a80fd | 2040 | pmop->op_private = 0 | (flags >> 8); |
79072805 | 2041 | |
36477c24 | 2042 | if (hints & HINT_LOCALE) |
2043 | pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE); | |
2044 | ||
79072805 | 2045 | /* link into pm list */ |
a0d0e21e | 2046 | if (type != OP_TRANS && curstash) { |
79072805 LW |
2047 | pmop->op_pmnext = HvPMROOT(curstash); |
2048 | HvPMROOT(curstash) = pmop; | |
2049 | } | |
2050 | ||
2051 | return (OP*)pmop; | |
2052 | } | |
2053 | ||
2054 | OP * | |
8ac85365 | 2055 | pmruntime(OP *o, OP *expr, OP *repl) |
79072805 LW |
2056 | { |
2057 | PMOP *pm; | |
2058 | LOGOP *rcop; | |
2059 | ||
11343788 MB |
2060 | if (o->op_type == OP_TRANS) |
2061 | return pmtrans(o, expr, repl); | |
79072805 | 2062 | |
3e3baf6d | 2063 | hints |= HINT_BLOCK_SCOPE; |
11343788 | 2064 | pm = (PMOP*)o; |
79072805 LW |
2065 | |
2066 | if (expr->op_type == OP_CONST) { | |
463ee0b2 | 2067 | STRLEN plen; |
79072805 | 2068 | SV *pat = ((SVOP*)expr)->op_sv; |
463ee0b2 | 2069 | char *p = SvPV(pat, plen); |
11343788 | 2070 | if ((o->op_flags & OPf_SPECIAL) && strEQ(p, " ")) { |
93a17b20 | 2071 | sv_setpvn(pat, "\\s+", 3); |
463ee0b2 | 2072 | p = SvPV(pat, plen); |
79072805 LW |
2073 | pm->op_pmflags |= PMf_SKIPWHITE; |
2074 | } | |
e50aee73 | 2075 | pm->op_pmregexp = pregcomp(p, p + plen, pm); |
aeea060c | 2076 | if (strEQ("\\s+", pm->op_pmregexp->precomp)) |
85e6fe83 | 2077 | pm->op_pmflags |= PMf_WHITE; |
79072805 LW |
2078 | op_free(expr); |
2079 | } | |
2080 | else { | |
463ee0b2 LW |
2081 | if (pm->op_pmflags & PMf_KEEP) |
2082 | expr = newUNOP(OP_REGCMAYBE,0,expr); | |
2083 | ||
79072805 LW |
2084 | Newz(1101, rcop, 1, LOGOP); |
2085 | rcop->op_type = OP_REGCOMP; | |
2086 | rcop->op_ppaddr = ppaddr[OP_REGCOMP]; | |
2087 | rcop->op_first = scalar(expr); | |
2088 | rcop->op_flags |= OPf_KIDS; | |
2089 | rcop->op_private = 1; | |
11343788 | 2090 | rcop->op_other = o; |
79072805 LW |
2091 | |
2092 | /* establish postfix order */ | |
463ee0b2 LW |
2093 | if (pm->op_pmflags & PMf_KEEP) { |
2094 | LINKLIST(expr); | |
2095 | rcop->op_next = expr; | |
2096 | ((UNOP*)expr)->op_first->op_next = (OP*)rcop; | |
2097 | } | |
2098 | else { | |
2099 | rcop->op_next = LINKLIST(expr); | |
2100 | expr->op_next = (OP*)rcop; | |
2101 | } | |
79072805 | 2102 | |
11343788 | 2103 | prepend_elem(o->op_type, scalar((OP*)rcop), o); |
79072805 LW |
2104 | } |
2105 | ||
2106 | if (repl) { | |
748a9306 LW |
2107 | OP *curop; |
2108 | if (pm->op_pmflags & PMf_EVAL) | |
2109 | curop = 0; | |
554b3eca | 2110 | #ifdef USE_THREADS |
2faa37cc | 2111 | else if (repl->op_type == OP_THREADSV |
554b3eca MB |
2112 | && strchr("&`'123456789+", |
2113 | per_thread_magicals[repl->op_targ])) | |
2114 | { | |
2115 | curop = 0; | |
2116 | } | |
2117 | #endif /* USE_THREADS */ | |
748a9306 LW |
2118 | else if (repl->op_type == OP_CONST) |
2119 | curop = repl; | |
79072805 | 2120 | else { |
79072805 LW |
2121 | OP *lastop = 0; |
2122 | for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) { | |
2123 | if (opargs[curop->op_type] & OA_DANGEROUS) { | |
554b3eca | 2124 | #ifdef USE_THREADS |
2faa37cc | 2125 | if (curop->op_type == OP_THREADSV |
554b3eca MB |
2126 | && strchr("&`'123456789+", curop->op_private)) { |
2127 | break; | |
2128 | } | |
2129 | #else | |
79072805 LW |
2130 | if (curop->op_type == OP_GV) { |
2131 | GV *gv = ((GVOP*)curop)->op_gv; | |
93a17b20 | 2132 | if (strchr("&`'123456789+", *GvENAME(gv))) |
79072805 LW |
2133 | break; |
2134 | } | |
554b3eca | 2135 | #endif /* USE_THREADS */ |
79072805 LW |
2136 | else if (curop->op_type == OP_RV2CV) |
2137 | break; | |
2138 | else if (curop->op_type == OP_RV2SV || | |
2139 | curop->op_type == OP_RV2AV || | |
2140 | curop->op_type == OP_RV2HV || | |
2141 | curop->op_type == OP_RV2GV) { | |
2142 | if (lastop && lastop->op_type != OP_GV) /*funny deref?*/ | |
2143 | break; | |
2144 | } | |
748a9306 LW |
2145 | else if (curop->op_type == OP_PADSV || |
2146 | curop->op_type == OP_PADAV || | |
2147 | curop->op_type == OP_PADHV || | |
554b3eca | 2148 | curop->op_type == OP_PADANY) { |
748a9306 LW |
2149 | /* is okay */ |
2150 | } | |
79072805 LW |
2151 | else |
2152 | break; | |
2153 | } | |
2154 | lastop = curop; | |
2155 | } | |
748a9306 LW |
2156 | } |
2157 | if (curop == repl) { | |
2158 | pm->op_pmflags |= PMf_CONST; /* const for long enough */ | |
4633a7c4 | 2159 | pm->op_pmpermflags |= PMf_CONST; /* const for long enough */ |
11343788 | 2160 | prepend_elem(o->op_type, scalar(repl), o); |
748a9306 LW |
2161 | } |
2162 | else { | |
2163 | Newz(1101, rcop, 1, LOGOP); | |
2164 | rcop->op_type = OP_SUBSTCONT; | |
2165 | rcop->op_ppaddr = ppaddr[OP_SUBSTCONT]; | |
2166 | rcop->op_first = scalar(repl); | |
2167 | rcop->op_flags |= OPf_KIDS; | |
2168 | rcop->op_private = 1; | |
11343788 | 2169 | rcop->op_other = o; |
748a9306 LW |
2170 | |
2171 | /* establish postfix order */ | |
2172 | rcop->op_next = LINKLIST(repl); | |
2173 | repl->op_next = (OP*)rcop; | |
2174 | ||
2175 | pm->op_pmreplroot = scalar((OP*)rcop); | |
2176 | pm->op_pmreplstart = LINKLIST(rcop); | |
2177 | rcop->op_next = 0; | |
79072805 LW |
2178 | } |
2179 | } | |
2180 | ||
2181 | return (OP*)pm; | |
2182 | } | |
2183 | ||
2184 | OP * | |
8ac85365 | 2185 | newSVOP(I32 type, I32 flags, SV *sv) |
79072805 LW |
2186 | { |
2187 | SVOP *svop; | |
2188 | Newz(1101, svop, 1, SVOP); | |
2189 | svop->op_type = type; | |
2190 | svop->op_ppaddr = ppaddr[type]; | |
2191 | svop->op_sv = sv; | |
2192 | svop->op_next = (OP*)svop; | |
2193 | svop->op_flags = flags; | |
2194 | if (opargs[type] & OA_RETSCALAR) | |
463ee0b2 | 2195 | scalar((OP*)svop); |
79072805 | 2196 | if (opargs[type] & OA_TARGET) |
ed6116ce | 2197 | svop->op_targ = pad_alloc(type, SVs_PADTMP); |
e50aee73 | 2198 | return CHECKOP(type, svop); |
79072805 LW |
2199 | } |
2200 | ||
2201 | OP * | |
8ac85365 | 2202 | newGVOP(I32 type, I32 flags, GV *gv) |
79072805 | 2203 | { |
11343788 | 2204 | dTHR; |
79072805 LW |
2205 | GVOP *gvop; |
2206 | Newz(1101, gvop, 1, GVOP); | |
2207 | gvop->op_type = type; | |
2208 | gvop->op_ppaddr = ppaddr[type]; | |
8990e307 | 2209 | gvop->op_gv = (GV*)SvREFCNT_inc(gv); |
79072805 LW |
2210 | gvop->op_next = (OP*)gvop; |
2211 | gvop->op_flags = flags; | |
2212 | if (opargs[type] & OA_RETSCALAR) | |
463ee0b2 | 2213 | scalar((OP*)gvop); |
79072805 | 2214 | if (opargs[type] & OA_TARGET) |
ed6116ce | 2215 | gvop->op_targ = pad_alloc(type, SVs_PADTMP); |
e50aee73 | 2216 | return CHECKOP(type, gvop); |
79072805 LW |
2217 | } |
2218 | ||
2219 | OP * | |
8ac85365 | 2220 | newPVOP(I32 type, I32 flags, char *pv) |
79072805 LW |
2221 | { |
2222 | PVOP *pvop; | |
2223 | Newz(1101, pvop, 1, PVOP); | |
2224 | pvop->op_type = type; | |
2225 | pvop->op_ppaddr = ppaddr[type]; | |
2226 | pvop->op_pv = pv; | |
2227 | pvop->op_next = (OP*)pvop; | |
2228 | pvop->op_flags = flags; | |
2229 | if (opargs[type] & OA_RETSCALAR) | |
463ee0b2 | 2230 | scalar((OP*)pvop); |
79072805 | 2231 | if (opargs[type] & OA_TARGET) |
ed6116ce | 2232 | pvop->op_targ = pad_alloc(type, SVs_PADTMP); |
e50aee73 | 2233 | return CHECKOP(type, pvop); |
79072805 LW |
2234 | } |
2235 | ||
79072805 | 2236 | void |
8ac85365 | 2237 | package(OP *o) |
79072805 | 2238 | { |
11343788 | 2239 | dTHR; |
93a17b20 | 2240 | SV *sv; |
79072805 LW |
2241 | |
2242 | save_hptr(&curstash); | |
2243 | save_item(curstname); | |
11343788 | 2244 | if (o) { |
463ee0b2 LW |
2245 | STRLEN len; |
2246 | char *name; | |
11343788 | 2247 | sv = cSVOPo->op_sv; |
463ee0b2 | 2248 | name = SvPV(sv, len); |
b1cb66bf | 2249 | curstash = gv_stashpvn(name,len,TRUE); |
463ee0b2 | 2250 | sv_setpvn(curstname, name, len); |
11343788 | 2251 | op_free(o); |
93a17b20 LW |
2252 | } |
2253 | else { | |
2254 | sv_setpv(curstname,"<none>"); | |
2255 | curstash = Nullhv; | |
2256 | } | |
79072805 | 2257 | copline = NOLINE; |
8990e307 | 2258 | expect = XSTATE; |
79072805 LW |
2259 | } |
2260 | ||
85e6fe83 | 2261 | void |
8ac85365 | 2262 | utilize(int aver, I32 floor, OP *version, OP *id, OP *arg) |
85e6fe83 | 2263 | { |
a0d0e21e LW |
2264 | OP *pack; |
2265 | OP *meth; | |
2266 | OP *rqop; | |
2267 | OP *imop; | |
b1cb66bf | 2268 | OP *veop; |
85e6fe83 | 2269 | |
a0d0e21e LW |
2270 | if (id->op_type != OP_CONST) |
2271 | croak("Module name must be constant"); | |
85e6fe83 | 2272 | |
b1cb66bf | 2273 | veop = Nullop; |
2274 | ||
2275 | if(version != Nullop) { | |
2276 | SV *vesv = ((SVOP*)version)->op_sv; | |
2277 | ||
2278 | if (arg == Nullop && !SvNIOK(vesv)) { | |
2279 | arg = version; | |
2280 | } | |
2281 | else { | |
2282 | OP *pack; | |
2283 | OP *meth; | |
2284 | ||
2285 | if (version->op_type != OP_CONST || !SvNIOK(vesv)) | |
2286 | croak("Version number must be constant number"); | |
2287 | ||
2288 | /* Make copy of id so we don't free it twice */ | |
2289 | pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv)); | |
2290 | ||
2291 | /* Fake up a method call to VERSION */ | |
2292 | meth = newSVOP(OP_CONST, 0, newSVpv("VERSION", 7)); | |
2293 | veop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, | |
2294 | append_elem(OP_LIST, | |
2295 | prepend_elem(OP_LIST, pack, list(version)), | |
2296 | newUNOP(OP_METHOD, 0, meth))); | |
2297 | } | |
2298 | } | |
aeea060c | 2299 | |
a0d0e21e | 2300 | /* Fake up an import/unimport */ |
4633a7c4 LW |
2301 | if (arg && arg->op_type == OP_STUB) |
2302 | imop = arg; /* no import on explicit () */ | |
b1cb66bf | 2303 | else if(SvNIOK(((SVOP*)id)->op_sv)) { |
2304 | imop = Nullop; /* use 5.0; */ | |
2305 | } | |
4633a7c4 LW |
2306 | else { |
2307 | /* Make copy of id so we don't free it twice */ | |
2308 | pack = newSVOP(OP_CONST, 0, newSVsv(((SVOP*)id)->op_sv)); | |
4633a7c4 LW |
2309 | meth = newSVOP(OP_CONST, 0, |
2310 | aver | |
2311 | ? newSVpv("import", 6) | |
2312 | : newSVpv("unimport", 8) | |
2313 | ); | |
2314 | imop = convert(OP_ENTERSUB, OPf_STACKED|OPf_SPECIAL, | |
a0d0e21e LW |
2315 | append_elem(OP_LIST, |
2316 | prepend_elem(OP_LIST, pack, list(arg)), | |
2317 | newUNOP(OP_METHOD, 0, meth))); | |
4633a7c4 LW |
2318 | } |
2319 | ||
2320 | /* Fake up a require */ | |
2321 | rqop = newUNOP(OP_REQUIRE, 0, id); | |
a0d0e21e LW |
2322 | |
2323 | /* Fake up the BEGIN {}, which does its thing immediately. */ | |
c07a80fd | 2324 | newSUB(floor, |
a0d0e21e | 2325 | newSVOP(OP_CONST, 0, newSVpv("BEGIN", 5)), |
4633a7c4 | 2326 | Nullop, |
a0d0e21e | 2327 | append_elem(OP_LINESEQ, |
b1cb66bf | 2328 | append_elem(OP_LINESEQ, |
2329 | newSTATEOP(0, Nullch, rqop), | |
2330 | newSTATEOP(0, Nullch, veop)), | |
a0d0e21e | 2331 | newSTATEOP(0, Nullch, imop) )); |
85e6fe83 | 2332 | |
85e6fe83 LW |
2333 | copline = NOLINE; |
2334 | expect = XSTATE; | |
2335 | } | |
2336 | ||
79072805 | 2337 | OP * |
8ac85365 | 2338 | newSLICEOP(I32 flags, OP *subscript, OP *listval) |
79072805 LW |
2339 | { |
2340 | return newBINOP(OP_LSLICE, flags, | |
8990e307 LW |
2341 | list(force_list(subscript)), |
2342 | list(force_list(listval)) ); | |
79072805 LW |
2343 | } |
2344 | ||
2345 | static I32 | |
8ac85365 | 2346 | list_assignment(register OP *o) |
79072805 | 2347 | { |
11343788 | 2348 | if (!o) |
79072805 LW |
2349 | return TRUE; |
2350 | ||
11343788 MB |
2351 | if (o->op_type == OP_NULL && o->op_flags & OPf_KIDS) |
2352 | o = cUNOPo->op_first; | |
79072805 | 2353 | |
11343788 MB |
2354 | if (o->op_type == OP_COND_EXPR) { |
2355 | I32 t = list_assignment(cCONDOPo->op_first->op_sibling); | |
2356 | I32 f = list_assignment(cCONDOPo->op_first->op_sibling->op_sibling); | |
79072805 LW |
2357 | |
2358 | if (t && f) | |
2359 | return TRUE; | |
2360 | if (t || f) | |
2361 | yyerror("Assignment to both a list and a scalar"); | |
2362 | return FALSE; | |
2363 | } | |
2364 | ||
11343788 MB |
2365 | if (o->op_type == OP_LIST || o->op_flags & OPf_PARENS || |
2366 | o->op_type == OP_RV2AV || o->op_type == OP_RV2HV || | |
2367 | o->op_type == OP_ASLICE || o->op_type == OP_HSLICE) | |
79072805 LW |
2368 | return TRUE; |
2369 | ||
11343788 | 2370 | if (o->op_type == OP_PADAV || o->op_type == OP_PADHV) |
93a17b20 LW |
2371 | return TRUE; |
2372 | ||
11343788 | 2373 | if (o->op_type == OP_RV2SV) |
79072805 LW |
2374 | return FALSE; |
2375 | ||
2376 | return FALSE; | |
2377 | } | |
2378 | ||
2379 | OP * | |
8ac85365 | 2380 | newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right) |
79072805 | 2381 | { |
11343788 | 2382 | OP *o; |
79072805 | 2383 | |
a0d0e21e LW |
2384 | if (optype) { |
2385 | if (optype == OP_ANDASSIGN || optype == OP_ORASSIGN) { | |
2386 | return newLOGOP(optype, 0, | |
2387 | mod(scalar(left), optype), | |
2388 | newUNOP(OP_SASSIGN, 0, scalar(right))); | |
2389 | } | |
2390 | else { | |
2391 | return newBINOP(optype, OPf_STACKED, | |
2392 | mod(scalar(left), optype), scalar(right)); | |
2393 | } | |
2394 | } | |
2395 | ||
79072805 | 2396 | if (list_assignment(left)) { |
463ee0b2 | 2397 | modcount = 0; |
748a9306 | 2398 | eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ |
463ee0b2 | 2399 | left = mod(left, OP_AASSIGN); |
748a9306 LW |
2400 | if (eval_start) |
2401 | eval_start = 0; | |
2402 | else { | |
a0d0e21e LW |
2403 | op_free(left); |
2404 | op_free(right); | |
2405 | return Nullop; | |
2406 | } | |
11343788 | 2407 | o = newBINOP(OP_AASSIGN, flags, |
8990e307 LW |
2408 | list(force_list(right)), |
2409 | list(force_list(left)) ); | |
11343788 | 2410 | o->op_private = 0 | (flags >> 8); |
a0d0e21e | 2411 | if (!(left->op_private & OPpLVAL_INTRO)) { |
748a9306 | 2412 | static int generation = 100; |
79072805 | 2413 | OP *curop; |
11343788 | 2414 | OP *lastop = o; |
79072805 | 2415 | generation++; |
11343788 | 2416 | for (curop = LINKLIST(o); curop != o; curop = LINKLIST(curop)) { |
79072805 LW |
2417 | if (opargs[curop->op_type] & OA_DANGEROUS) { |
2418 | if (curop->op_type == OP_GV) { | |
2419 | GV *gv = ((GVOP*)curop)->op_gv; | |
2420 | if (gv == defgv || SvCUR(gv) == generation) | |
2421 | break; | |
2422 | SvCUR(gv) = generation; | |
2423 | } | |
748a9306 LW |
2424 | else if (curop->op_type == OP_PADSV || |
2425 | curop->op_type == OP_PADAV || | |
2426 | curop->op_type == OP_PADHV || | |
2427 | curop->op_type == OP_PADANY) { | |
2428 | SV **svp = AvARRAY(comppad_name); | |
8e07c86e | 2429 | SV *sv = svp[curop->op_targ]; |
748a9306 LW |
2430 | if (SvCUR(sv) == generation) |
2431 | break; | |
2432 | SvCUR(sv) = generation; /* (SvCUR not used any more) */ | |
2433 | } | |
79072805 LW |
2434 | else if (curop->op_type == OP_RV2CV) |
2435 | break; | |
2436 | else if (curop->op_type == OP_RV2SV || | |
2437 | curop->op_type == OP_RV2AV || | |
2438 | curop->op_type == OP_RV2HV || | |
2439 | curop->op_type == OP_RV2GV) { | |
2440 | if (lastop->op_type != OP_GV) /* funny deref? */ | |
2441 | break; | |
2442 | } | |
2443 | else | |
2444 | break; | |
2445 | } | |
2446 | lastop = curop; | |
2447 | } | |
11343788 MB |
2448 | if (curop != o) |
2449 | o->op_private = OPpASSIGN_COMMON; | |
79072805 | 2450 | } |
c07a80fd | 2451 | if (right && right->op_type == OP_SPLIT) { |
2452 | OP* tmpop; | |
2453 | if ((tmpop = ((LISTOP*)right)->op_first) && | |
2454 | tmpop->op_type == OP_PUSHRE) | |
2455 | { | |
2456 | PMOP *pm = (PMOP*)tmpop; | |
2457 | if (left->op_type == OP_RV2AV && | |
2458 | !(left->op_private & OPpLVAL_INTRO) && | |
11343788 | 2459 | !(o->op_private & OPpASSIGN_COMMON) ) |
c07a80fd | 2460 | { |
2461 | tmpop = ((UNOP*)left)->op_first; | |
2462 | if (tmpop->op_type == OP_GV && !pm->op_pmreplroot) { | |
2463 | pm->op_pmreplroot = (OP*)((GVOP*)tmpop)->op_gv; | |
2464 | pm->op_pmflags |= PMf_ONCE; | |
11343788 | 2465 | tmpop = cUNOPo->op_first; /* to list (nulled) */ |
c07a80fd | 2466 | tmpop = ((UNOP*)tmpop)->op_first; /* to pushmark */ |
2467 | tmpop->op_sibling = Nullop; /* don't free split */ | |
2468 | right->op_next = tmpop->op_next; /* fix starting loc */ | |
11343788 | 2469 | op_free(o); /* blow off assign */ |
54310121 | 2470 | right->op_flags &= ~OPf_WANT; |
a5f75d66 | 2471 | /* "I don't know and I don't care." */ |
c07a80fd | 2472 | return right; |
2473 | } | |
2474 | } | |
2475 | else { | |
2476 | if (modcount < 10000 && | |
2477 | ((LISTOP*)right)->op_last->op_type == OP_CONST) | |
2478 | { | |
2479 | SV *sv = ((SVOP*)((LISTOP*)right)->op_last)->op_sv; | |
2480 | if (SvIVX(sv) == 0) | |
2481 | sv_setiv(sv, modcount+1); | |
2482 | } | |
2483 | } | |
2484 | } | |
2485 | } | |
11343788 | 2486 | return o; |
79072805 LW |
2487 | } |
2488 | if (!right) | |
2489 | right = newOP(OP_UNDEF, 0); | |
2490 | if (right->op_type == OP_READLINE) { | |
2491 | right->op_flags |= OPf_STACKED; | |
463ee0b2 | 2492 | return newBINOP(OP_NULL, flags, mod(scalar(left), OP_SASSIGN), scalar(right)); |
79072805 | 2493 | } |
a0d0e21e | 2494 | else { |
748a9306 | 2495 | eval_start = right; /* Grandfathering $[ assignment here. Bletch.*/ |
11343788 | 2496 | o = newBINOP(OP_SASSIGN, flags, |
463ee0b2 | 2497 | scalar(right), mod(scalar(left), OP_SASSIGN) ); |
748a9306 LW |
2498 | if (eval_start) |
2499 | eval_start = 0; | |
2500 | else { | |
11343788 | 2501 | op_free(o); |
a0d0e21e LW |
2502 | return Nullop; |
2503 | } | |
2504 | } | |
11343788 | 2505 | return o; |
79072805 LW |
2506 | } |
2507 | ||
2508 | OP * | |
8ac85365 | 2509 | newSTATEOP(I32 flags, char *label, OP *o) |
79072805 | 2510 | { |
11343788 | 2511 | dTHR; |
bbce6d69 | 2512 | U32 seq = intro_my(); |
79072805 LW |
2513 | register COP *cop; |
2514 | ||
2515 | Newz(1101, cop, 1, COP); | |
84902520 | 2516 | if (PERLDB_LINE && curcop->cop_line && curstash != debstash) { |
8990e307 LW |
2517 | cop->op_type = OP_DBSTATE; |
2518 | cop->op_ppaddr = ppaddr[ OP_DBSTATE ]; | |
2519 | } | |
2520 | else { | |
2521 | cop->op_type = OP_NEXTSTATE; | |
2522 | cop->op_ppaddr = ppaddr[ OP_NEXTSTATE ]; | |
2523 | } | |
79072805 | 2524 | cop->op_flags = flags; |
c07a80fd | 2525 | cop->op_private = 0 | (flags >> 8); |
ff0cee69 | 2526 | #ifdef NATIVE_HINTS |
2527 | cop->op_private |= NATIVE_HINTS; | |
2528 | #endif | |
79072805 LW |
2529 | cop->op_next = (OP*)cop; |
2530 | ||
463ee0b2 LW |
2531 | if (label) { |
2532 | cop->cop_label = label; | |
85e6fe83 | 2533 | hints |= HINT_BLOCK_SCOPE; |
463ee0b2 | 2534 | } |
bbce6d69 | 2535 | cop->cop_seq = seq; |
a0d0e21e | 2536 | cop->cop_arybase = curcop->cop_arybase; |
79072805 LW |
2537 | |
2538 | if (copline == NOLINE) | |
2539 | cop->cop_line = curcop->cop_line; | |
2540 | else { | |
2541 | cop->cop_line = copline; | |
2542 | copline = NOLINE; | |
2543 | } | |
44a8e56a | 2544 | cop->cop_filegv = (GV*)SvREFCNT_inc(curcop->cop_filegv); |
79072805 LW |
2545 | cop->cop_stash = curstash; |
2546 | ||
84902520 | 2547 | if (PERLDB_LINE && curstash != debstash) { |
93a17b20 LW |
2548 | SV **svp = av_fetch(GvAV(curcop->cop_filegv),(I32)cop->cop_line, FALSE); |
2549 | if (svp && *svp != &sv_undef && !SvIOK(*svp)) { | |
a0d0e21e | 2550 | (void)SvIOK_on(*svp); |
a5f75d66 | 2551 | SvIVX(*svp) = 1; |
93a17b20 LW |
2552 | SvSTASH(*svp) = (HV*)cop; |
2553 | } | |
2554 | } | |
2555 | ||
11343788 | 2556 | return prepend_elem(OP_LINESEQ, (OP*)cop, o); |
79072805 LW |
2557 | } |
2558 | ||
bbce6d69 | 2559 | /* "Introduce" my variables to visible status. */ |
2560 | U32 | |
8ac85365 | 2561 | intro_my(void) |
bbce6d69 | 2562 | { |
2563 | SV **svp; | |
2564 | SV *sv; | |
2565 | I32 i; | |
2566 | ||
2567 | if (! min_intro_pending) | |
2568 | return cop_seqmax; | |
2569 | ||
2570 | svp = AvARRAY(comppad_name); | |
2571 | for (i = min_intro_pending; i <= max_intro_pending; i++) { | |
2572 | if ((sv = svp[i]) && sv != &sv_undef && !SvIVX(sv)) { | |
2573 | SvIVX(sv) = 999999999; /* Don't know scope end yet. */ | |
2574 | SvNVX(sv) = (double)cop_seqmax; | |
2575 | } | |
2576 | } | |
2577 | min_intro_pending = 0; | |
2578 | comppad_name_fill = max_intro_pending; /* Needn't search higher */ | |
2579 | return cop_seqmax++; | |
2580 | } | |
2581 | ||
79072805 | 2582 | OP * |
8ac85365 | 2583 | newLOGOP(I32 type, I32 flags, OP *first, OP *other) |
79072805 | 2584 | { |
11343788 | 2585 | dTHR; |
79072805 | 2586 | LOGOP *logop; |
11343788 | 2587 | OP *o; |
79072805 | 2588 | |
a0d0e21e LW |
2589 | if (type == OP_XOR) /* Not short circuit, but here by precedence. */ |
2590 | return newBINOP(type, flags, scalar(first), scalar(other)); | |
2591 | ||
8990e307 | 2592 | scalarboolean(first); |
79072805 LW |
2593 | /* optimize "!a && b" to "a || b", and "!a || b" to "a && b" */ |
2594 | if (first->op_type == OP_NOT && (first->op_flags & OPf_SPECIAL)) { | |
2595 | if (type == OP_AND || type == OP_OR) { | |
2596 | if (type == OP_AND) | |
2597 | type = OP_OR; | |
2598 | else | |
2599 | type = OP_AND; | |
11343788 MB |
2600 | o = first; |
2601 | first = cUNOPo->op_first; | |
2602 | if (o->op_next) | |
2603 | first->op_next = o->op_next; | |
2604 | cUNOPo->op_first = Nullop; | |
2605 | op_free(o); | |
79072805 LW |
2606 | } |
2607 | } | |
2608 | if (first->op_type == OP_CONST) { | |
93a17b20 | 2609 | if (dowarn && (first->op_private & OPpCONST_BARE)) |
c07a80fd | 2610 | warn("Probable precedence problem on %s", op_desc[type]); |
79072805 LW |
2611 | if ((type == OP_AND) == (SvTRUE(((SVOP*)first)->op_sv))) { |
2612 | op_free(first); | |
2613 | return other; | |
2614 | } | |
2615 | else { | |
2616 | op_free(other); | |
2617 | return first; | |
2618 | } | |
2619 | } | |
2620 | else if (first->op_type == OP_WANTARRAY) { | |
2621 | if (type == OP_AND) | |
2622 | list(other); | |
2623 | else | |
2624 | scalar(other); | |
2625 | } | |
a6006777 | 2626 | else if (dowarn && (first->op_flags & OPf_KIDS)) { |
2627 | OP *k1 = ((UNOP*)first)->op_first; | |
2628 | OP *k2 = k1->op_sibling; | |
2629 | OPCODE warnop = 0; | |
2630 | switch (first->op_type) | |
2631 | { | |
2632 | case OP_NULL: | |
2633 | if (k2 && k2->op_type == OP_READLINE | |
2634 | && (k2->op_flags & OPf_STACKED) | |
2635 | && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV)) | |
2636 | warnop = k2->op_type; | |
2637 | break; | |
2638 | ||
2639 | case OP_SASSIGN: | |
68dc0745 | 2640 | if (k1->op_type == OP_READDIR |
2641 | || k1->op_type == OP_GLOB | |
2642 | || k1->op_type == OP_EACH) | |
a6006777 | 2643 | warnop = k1->op_type; |
2644 | break; | |
2645 | } | |
8ebc5c01 | 2646 | if (warnop) { |
2647 | line_t oldline = curcop->cop_line; | |
2648 | curcop->cop_line = copline; | |
68dc0745 | 2649 | warn("Value of %s%s can be \"0\"; test with defined()", |
2650 | op_desc[warnop], | |
2651 | ((warnop == OP_READLINE || warnop == OP_GLOB) | |
2652 | ? " construct" : "() operator")); | |
2653 | curcop->cop_line = oldline; | |
8ebc5c01 | 2654 | } |
a6006777 | 2655 | } |
79072805 LW |
2656 | |
2657 | if (!other) | |
2658 | return first; | |
2659 | ||
a0d0e21e LW |
2660 | if (type == OP_ANDASSIGN || type == OP_ORASSIGN) |
2661 | other->op_private |= OPpASSIGN_BACKWARDS; /* other is an OP_SASSIGN */ | |
2662 | ||
79072805 LW |
2663 | Newz(1101, logop, 1, LOGOP); |
2664 | ||
2665 | logop->op_type = type; | |
2666 | logop->op_ppaddr = ppaddr[type]; | |
2667 | logop->op_first = first; | |
2668 | logop->op_flags = flags | OPf_KIDS; | |
2669 | logop->op_other = LINKLIST(other); | |
c07a80fd | 2670 | logop->op_private = 1 | (flags >> 8); |
79072805 LW |
2671 | |
2672 | /* establish postfix order */ | |
2673 | logop->op_next = LINKLIST(first); | |
2674 | first->op_next = (OP*)logop; | |
2675 | first->op_sibling = other; | |
2676 | ||
11343788 MB |
2677 | o = newUNOP(OP_NULL, 0, (OP*)logop); |
2678 | other->op_next = o; | |
79072805 | 2679 | |
11343788 | 2680 | return o; |
79072805 LW |
2681 | } |
2682 | ||
2683 | OP * | |
8ac85365 | 2684 | newCONDOP(I32 flags, OP *first, OP *trueop, OP *falseop) |
79072805 | 2685 | { |
11343788 | 2686 | dTHR; |
79072805 | 2687 | CONDOP *condop; |
11343788 | 2688 | OP *o; |
79072805 | 2689 | |
b1cb66bf | 2690 | if (!falseop) |
2691 | return newLOGOP(OP_AND, 0, first, trueop); | |
2692 | if (!trueop) | |
2693 | return newLOGOP(OP_OR, 0, first, falseop); | |
79072805 | 2694 | |
8990e307 | 2695 | scalarboolean(first); |
79072805 LW |
2696 | if (first->op_type == OP_CONST) { |
2697 | if (SvTRUE(((SVOP*)first)->op_sv)) { | |
2698 | op_free(first); | |
b1cb66bf | 2699 | op_free(falseop); |
2700 | return trueop; | |
79072805 LW |
2701 | } |
2702 | else { | |
2703 | op_free(first); | |
b1cb66bf | 2704 | op_free(trueop); |
2705 | return falseop; | |
79072805 LW |
2706 | } |
2707 | } | |
2708 | else if (first->op_type == OP_WANTARRAY) { | |
b1cb66bf | 2709 | list(trueop); |
2710 | scalar(falseop); | |
79072805 LW |
2711 | } |
2712 | Newz(1101, condop, 1, CONDOP); | |
2713 | ||
2714 | condop->op_type = OP_COND_EXPR; | |
2715 | condop->op_ppaddr = ppaddr[OP_COND_EXPR]; | |
2716 | condop->op_first = first; | |
2717 | condop->op_flags = flags | OPf_KIDS; | |
b1cb66bf | 2718 | condop->op_true = LINKLIST(trueop); |
2719 | condop->op_false = LINKLIST(falseop); | |
c07a80fd | 2720 | condop->op_private = 1 | (flags >> 8); |
79072805 LW |
2721 | |
2722 | /* establish postfix order */ | |
2723 | condop->op_next = LINKLIST(first); | |
2724 | first->op_next = (OP*)condop; | |
2725 | ||
b1cb66bf | 2726 | first->op_sibling = trueop; |
2727 | trueop->op_sibling = falseop; | |
11343788 | 2728 | o = newUNOP(OP_NULL, 0, (OP*)condop); |
79072805 | 2729 | |
5dc0d613 MB |
2730 | trueop->op_next = o; |
2731 | falseop->op_next = o; | |
79072805 | 2732 | |
11343788 | 2733 | return o; |
79072805 LW |
2734 | } |
2735 | ||
2736 | OP * | |
8ac85365 | 2737 | newRANGE(I32 flags, OP *left, OP *right) |
79072805 | 2738 | { |
b35b2403 | 2739 | dTHR; |
79072805 LW |
2740 | CONDOP *condop; |
2741 | OP *flip; | |
2742 | OP *flop; | |
11343788 | 2743 | OP *o; |
79072805 LW |
2744 | |
2745 | Newz(1101, condop, 1, CONDOP); | |
2746 | ||
2747 | condop->op_type = OP_RANGE; | |
2748 | condop->op_ppaddr = ppaddr[OP_RANGE]; | |
2749 | condop->op_first = left; | |
2750 | condop->op_flags = OPf_KIDS; | |
2751 | condop->op_true = LINKLIST(left); | |
2752 | condop->op_false = LINKLIST(right); | |
c07a80fd | 2753 | condop->op_private = 1 | (flags >> 8); |
79072805 LW |
2754 | |
2755 | left->op_sibling = right; | |
2756 | ||
2757 | condop->op_next = (OP*)condop; | |
2758 | flip = newUNOP(OP_FLIP, flags, (OP*)condop); | |
2759 | flop = newUNOP(OP_FLOP, 0, flip); | |
11343788 | 2760 | o = newUNOP(OP_NULL, 0, flop); |
79072805 LW |
2761 | linklist(flop); |
2762 | ||
2763 | left->op_next = flip; | |
2764 | right->op_next = flop; | |
2765 | ||
ed6116ce | 2766 | condop->op_targ = pad_alloc(OP_RANGE, SVs_PADMY); |
79072805 | 2767 | sv_upgrade(PAD_SV(condop->op_targ), SVt_PVNV); |
ed6116ce | 2768 | flip->op_targ = pad_alloc(OP_RANGE, SVs_PADMY); |
79072805 LW |
2769 | sv_upgrade(PAD_SV(flip->op_targ), SVt_PVNV); |
2770 | ||
2771 | flip->op_private = left->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; | |
2772 | flop->op_private = right->op_type == OP_CONST ? OPpFLIP_LINENUM : 0; | |
2773 | ||
11343788 | 2774 | flip->op_next = o; |
79072805 | 2775 | if (!flip->op_private || !flop->op_private) |
11343788 | 2776 | linklist(o); /* blow off optimizer unless constant */ |
79072805 | 2777 | |
11343788 | 2778 | return o; |
79072805 LW |
2779 | } |
2780 | ||
2781 | OP * | |
8ac85365 | 2782 | newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block) |
79072805 | 2783 | { |
11343788 | 2784 | dTHR; |
463ee0b2 | 2785 | OP* listop; |
11343788 | 2786 | OP* o; |
463ee0b2 | 2787 | int once = block && block->op_flags & OPf_SPECIAL && |
a0d0e21e | 2788 | (block->op_type == OP_ENTERSUB || block->op_type == OP_NULL); |
93a17b20 | 2789 | |
463ee0b2 LW |
2790 | if (expr) { |
2791 | if (once && expr->op_type == OP_CONST && !SvTRUE(((SVOP*)expr)->op_sv)) | |
2792 | return block; /* do {} while 0 does once */ | |
fb73857a | 2793 | if (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB |
2794 | || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) { | |
774d564b | 2795 | expr = newUNOP(OP_DEFINED, 0, |
2796 | newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) ); | |
2797 | } | |
463ee0b2 | 2798 | } |
93a17b20 | 2799 | |
8990e307 | 2800 | listop = append_elem(OP_LINESEQ, block, newOP(OP_UNSTACK, 0)); |
11343788 | 2801 | o = newLOGOP(OP_AND, 0, expr, listop); |
463ee0b2 | 2802 | |
11343788 | 2803 | ((LISTOP*)listop)->op_last->op_next = LINKLIST(o); |
79072805 | 2804 | |
11343788 MB |
2805 | if (once && o != listop) |
2806 | o->op_next = ((LOGOP*)cUNOPo->op_first)->op_other; | |
79072805 | 2807 | |
11343788 MB |
2808 | if (o == listop) |
2809 | o = newUNOP(OP_NULL, 0, o); /* or do {} while 1 loses outer block */ | |
748a9306 | 2810 | |
11343788 MB |
2811 | o->op_flags |= flags; |
2812 | o = scope(o); | |
2813 | o->op_flags |= OPf_SPECIAL; /* suppress POPBLOCK curpm restoration*/ | |
2814 | return o; | |
79072805 LW |
2815 | } |
2816 | ||
2817 | OP * | |
8ac85365 | 2818 | newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *block, OP *cont) |
79072805 | 2819 | { |
11343788 | 2820 | dTHR; |
79072805 LW |
2821 | OP *redo; |
2822 | OP *next = 0; | |
2823 | OP *listop; | |
11343788 | 2824 | OP *o; |
79072805 LW |
2825 | OP *condop; |
2826 | ||
fb73857a | 2827 | if (expr && (expr->op_type == OP_READLINE || expr->op_type == OP_GLOB |
2828 | || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) { | |
748a9306 LW |
2829 | expr = newUNOP(OP_DEFINED, 0, |
2830 | newASSIGNOP(0, newSVREF(newGVOP(OP_GV, 0, defgv)), 0, expr) ); | |
2831 | } | |
79072805 LW |
2832 | |
2833 | if (!block) | |
2834 | block = newOP(OP_NULL, 0); | |
2835 | ||
2836 | if (cont) | |
2837 | next = LINKLIST(cont); | |
fb73857a | 2838 | if (expr) { |
79072805 | 2839 | cont = append_elem(OP_LINESEQ, cont, newOP(OP_UNSTACK, 0)); |
fb73857a | 2840 | if ((line_t)whileline != NOLINE) { |
2841 | copline = whileline; | |
2842 | cont = append_elem(OP_LINESEQ, cont, | |
2843 | newSTATEOP(0, Nullch, Nullop)); | |
2844 | } | |
2845 | } | |
79072805 | 2846 | |
463ee0b2 | 2847 | listop = append_list(OP_LINESEQ, (LISTOP*)block, (LISTOP*)cont); |
79072805 LW |
2848 | redo = LINKLIST(listop); |
2849 | ||
2850 | if (expr) { | |
11343788 MB |
2851 | o = newLOGOP(OP_AND, 0, expr, scalar(listop)); |
2852 | if (o == expr && o->op_type == OP_CONST && !SvTRUE(cSVOPo->op_sv)) { | |
85e6fe83 | 2853 | op_free(expr); /* oops, it's a while (0) */ |
463ee0b2 LW |
2854 | op_free((OP*)loop); |
2855 | return Nullop; /* (listop already freed by newLOGOP) */ | |
2856 | } | |
aeea060c | 2857 | ((LISTOP*)listop)->op_last->op_next = condop = |
11343788 | 2858 | (o == listop ? redo : LINKLIST(o)); |
79072805 LW |
2859 | if (!next) |
2860 | next = condop; | |
2861 | } | |
2862 | else | |
11343788 | 2863 | o = listop; |
79072805 LW |
2864 | |
2865 | if (!loop) { | |
2866 | Newz(1101,loop,1,LOOP); | |
2867 | loop->op_type = OP_ENTERLOOP; | |
2868 | loop->op_ppaddr = ppaddr[OP_ENTERLOOP]; | |
2869 | loop->op_private = 0; | |
2870 | loop->op_next = (OP*)loop; | |
2871 | } | |
2872 | ||
11343788 | 2873 | o = newBINOP(OP_LEAVELOOP, 0, (OP*)loop, o); |
79072805 LW |
2874 | |
2875 | loop->op_redoop = redo; | |
11343788 | 2876 | loop->op_lastop = o; |
79072805 LW |
2877 | |
2878 | if (next) | |
2879 | loop->op_nextop = next; | |
2880 | else | |
11343788 | 2881 | loop->op_nextop = o; |
79072805 | 2882 | |
11343788 MB |
2883 | o->op_flags |= flags; |
2884 | o->op_private |= (flags >> 8); | |
2885 | return o; | |
79072805 LW |
2886 | } |
2887 | ||
2888 | OP * | |
a0d0e21e | 2889 | #ifndef CAN_PROTOTYPE |
79072805 LW |
2890 | newFOROP(flags,label,forline,sv,expr,block,cont) |
2891 | I32 flags; | |
2892 | char *label; | |
2893 | line_t forline; | |
2894 | OP* sv; | |
2895 | OP* expr; | |
2896 | OP*block; | |
2897 | OP*cont; | |
8990e307 LW |
2898 | #else |
2899 | newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont) | |
a0d0e21e | 2900 | #endif /* CAN_PROTOTYPE */ |
79072805 LW |
2901 | { |
2902 | LOOP *loop; | |
fb73857a | 2903 | OP *wop; |
85e6fe83 | 2904 | int padoff = 0; |
4633a7c4 | 2905 | I32 iterflags = 0; |
79072805 | 2906 | |
79072805 | 2907 | if (sv) { |
85e6fe83 | 2908 | if (sv->op_type == OP_RV2SV) { /* symbol table variable */ |
748a9306 LW |
2909 | sv->op_type = OP_RV2GV; |
2910 | sv->op_ppaddr = ppaddr[OP_RV2GV]; | |
79072805 | 2911 | } |
85e6fe83 LW |
2912 | else if (sv->op_type == OP_PADSV) { /* private variable */ |
2913 | padoff = sv->op_targ; | |
2914 | op_free(sv); | |
2915 | sv = Nullop; | |
2916 | } | |
79072805 | 2917 | else |
c07a80fd | 2918 | croak("Can't use %s for loop variable", op_desc[sv->op_type]); |
79072805 LW |
2919 | } |
2920 | else { | |
2921 | sv = newGVOP(OP_GV, 0, defgv); | |
2922 | } | |
5f05dabc | 2923 | if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) { |
4633a7c4 LW |
2924 | expr = scalar(ref(expr, OP_ITER)); |
2925 | iterflags |= OPf_STACKED; | |
2926 | } | |
2927 | loop = (LOOP*)list(convert(OP_ENTERITER, iterflags, | |
a0d0e21e LW |
2928 | append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART), |
2929 | scalar(sv)))); | |
85e6fe83 LW |
2930 | assert(!loop->op_next); |
2931 | Renew(loop, 1, LOOP); | |
2932 | loop->op_targ = padoff; | |
fb73857a | 2933 | wop = newWHILEOP(flags, 1, loop, forline, newOP(OP_ITER, 0), block, cont); |
2934 | copline = forline; | |
2935 | return newSTATEOP(0, label, wop); | |
79072805 LW |
2936 | } |
2937 | ||
8990e307 | 2938 | OP* |
8ac85365 | 2939 | newLOOPEX(I32 type, OP *label) |
8990e307 | 2940 | { |
11343788 MB |
2941 | dTHR; |
2942 | OP *o; | |
8990e307 | 2943 | if (type != OP_GOTO || label->op_type == OP_CONST) { |
11343788 | 2944 | o = newPVOP(type, 0, savepv( |
a0d0e21e LW |
2945 | label->op_type == OP_CONST |
2946 | ? SvPVx(((SVOP*)label)->op_sv, na) | |
2947 | : "" )); | |
8990e307 LW |
2948 | op_free(label); |
2949 | } | |
2950 | else { | |
a0d0e21e LW |
2951 | if (label->op_type == OP_ENTERSUB) |
2952 | label = newUNOP(OP_REFGEN, 0, mod(label, OP_REFGEN)); | |
11343788 | 2953 | o = newUNOP(type, OPf_STACKED, label); |
8990e307 | 2954 | } |
85e6fe83 | 2955 | hints |= HINT_BLOCK_SCOPE; |
11343788 | 2956 | return o; |
8990e307 LW |
2957 | } |
2958 | ||
79072805 | 2959 | void |
8ac85365 | 2960 | cv_undef(CV *cv) |
79072805 | 2961 | { |
11343788 MB |
2962 | dTHR; |
2963 | #ifdef USE_THREADS | |
e858de61 MB |
2964 | if (CvMUTEXP(cv)) { |
2965 | MUTEX_DESTROY(CvMUTEXP(cv)); | |
2966 | Safefree(CvMUTEXP(cv)); | |
2967 | CvMUTEXP(cv) = 0; | |
2968 | } | |
11343788 MB |
2969 | #endif /* USE_THREADS */ |
2970 | ||
a0d0e21e | 2971 | if (!CvXSUB(cv) && CvROOT(cv)) { |
11343788 MB |
2972 | #ifdef USE_THREADS |
2973 | if (CvDEPTH(cv) || (CvOWNER(cv) && CvOWNER(cv) != thr)) | |
2974 | croak("Can't undef active subroutine"); | |
2975 | #else | |
a0d0e21e LW |
2976 | if (CvDEPTH(cv)) |
2977 | croak("Can't undef active subroutine"); | |
11343788 | 2978 | #endif /* USE_THREADS */ |
8990e307 | 2979 | ENTER; |
a0d0e21e LW |
2980 | |
2981 | SAVESPTR(curpad); | |
2982 | curpad = 0; | |
2983 | ||
a5f75d66 | 2984 | if (!CvCLONED(cv)) |
748a9306 | 2985 | op_free(CvROOT(cv)); |
79072805 | 2986 | CvROOT(cv) = Nullop; |
8990e307 | 2987 | LEAVE; |
79072805 | 2988 | } |
1d5db326 | 2989 | SvPOK_off((SV*)cv); /* forget prototype */ |
44a8e56a | 2990 | CvFLAGS(cv) = 0; |
8e07c86e AD |
2991 | SvREFCNT_dec(CvGV(cv)); |
2992 | CvGV(cv) = Nullgv; | |
2993 | SvREFCNT_dec(CvOUTSIDE(cv)); | |
2994 | CvOUTSIDE(cv) = Nullcv; | |
2995 | if (CvPADLIST(cv)) { | |
8ebc5c01 | 2996 | /* may be during global destruction */ |
2997 | if (SvREFCNT(CvPADLIST(cv))) { | |
2998 | I32 i = AvFILL(CvPADLIST(cv)); | |
2999 | while (i >= 0) { | |
3000 | SV** svp = av_fetch(CvPADLIST(cv), i--, FALSE); | |
46fc3d4c | 3001 | SV* sv = svp ? *svp : Nullsv; |
3002 | if (!sv) | |
3003 | continue; | |
3004 | if (sv == (SV*)comppad_name) | |
3005 | comppad_name = Nullav; | |
3006 | else if (sv == (SV*)comppad) { | |
3007 | comppad = Nullav; | |
3008 | curpad = Null(SV**); | |
3009 | } | |
3010 | SvREFCNT_dec(sv); | |
8ebc5c01 | 3011 | } |
3012 | SvREFCNT_dec((SV*)CvPADLIST(cv)); | |
8e07c86e | 3013 | } |
8e07c86e AD |
3014 | CvPADLIST(cv) = Nullav; |
3015 | } | |
79072805 LW |
3016 | } |
3017 | ||
5f05dabc | 3018 | #ifdef DEBUG_CLOSURES |
3019 | static void | |
3020 | cv_dump(cv) | |
3021 | CV* cv; | |
3022 | { | |
3023 | CV *outside = CvOUTSIDE(cv); | |
3024 | AV* padlist = CvPADLIST(cv); | |
4fdae800 | 3025 | AV* pad_name; |
3026 | AV* pad; | |
3027 | SV** pname; | |
3028 | SV** ppad; | |
5f05dabc | 3029 | I32 ix; |
3030 | ||
fb73857a | 3031 | PerlIO_printf(Perl_debug_log, "\tCV=0x%lx (%s), OUTSIDE=0x%lx (%s)\n", |
ab50184a CS |
3032 | cv, |
3033 | (CvANON(cv) ? "ANON" | |
3034 | : (cv == main_cv) ? "MAIN" | |
07055b4c | 3035 | : CvUNIQUE(outside) ? "UNIQUE" |
44a8e56a | 3036 | : CvGV(cv) ? GvNAME(CvGV(cv)) : "UNDEFINED"), |
ab50184a CS |
3037 | outside, |
3038 | (!outside ? "null" | |
3039 | : CvANON(outside) ? "ANON" | |
3040 | : (outside == main_cv) ? "MAIN" | |
07055b4c | 3041 | : CvUNIQUE(outside) ? "UNIQUE" |
44a8e56a | 3042 | : CvGV(outside) ? GvNAME(CvGV(outside)) : "UNDEFINED")); |
5f05dabc | 3043 | |
4fdae800 | 3044 | if (!padlist) |
3045 | return; | |
3046 | ||
3047 | pad_name = (AV*)*av_fetch(padlist, 0, FALSE); | |
3048 | pad = (AV*)*av_fetch(padlist, 1, FALSE); | |
3049 | pname = AvARRAY(pad_name); | |
3050 | ppad = AvARRAY(pad); | |
3051 | ||
9607fc9c | 3052 | for (ix = 1; ix <= AvFILL(pad_name); ix++) { |
5f05dabc | 3053 | if (SvPOK(pname[ix])) |
fb73857a | 3054 | PerlIO_printf(Perl_debug_log, "\t%4d. 0x%lx (%s\"%s\" %ld-%ld)\n", |
4fdae800 | 3055 | ix, ppad[ix], |
3056 | SvFAKE(pname[ix]) ? "FAKE " : "", | |
3057 | SvPVX(pname[ix]), | |
ab50184a CS |
3058 | (long)I_32(SvNVX(pname[ix])), |
3059 | (long)SvIVX(pname[ix])); | |
5f05dabc | 3060 | } |
3061 | } | |
3062 | #endif /* DEBUG_CLOSURES */ | |
3063 | ||
3064 | static CV * | |
8ac85365 | 3065 | cv_clone2(CV *proto, CV *outside) |
748a9306 | 3066 | { |
11343788 | 3067 | dTHR; |
748a9306 LW |
3068 | AV* av; |
3069 | I32 ix; | |
3070 | AV* protopadlist = CvPADLIST(proto); | |
3071 | AV* protopad_name = (AV*)*av_fetch(protopadlist, 0, FALSE); | |
3072 | AV* protopad = (AV*)*av_fetch(protopadlist, 1, FALSE); | |
5f05dabc | 3073 | SV** pname = AvARRAY(protopad_name); |
3074 | SV** ppad = AvARRAY(protopad); | |
9607fc9c | 3075 | I32 fname = AvFILL(protopad_name); |
3076 | I32 fpad = AvFILL(protopad); | |
748a9306 LW |
3077 | AV* comppadlist; |
3078 | CV* cv; | |
3079 | ||
07055b4c CS |
3080 | assert(!CvUNIQUE(proto)); |
3081 | ||
748a9306 LW |
3082 | ENTER; |
3083 | SAVESPTR(curpad); | |
3084 | SAVESPTR(comppad); | |
46fc3d4c | 3085 | SAVESPTR(comppad_name); |
748a9306 LW |
3086 | SAVESPTR(compcv); |
3087 | ||
3088 | cv = compcv = (CV*)NEWSV(1104,0); | |
fa83b5b6 | 3089 | sv_upgrade((SV *)cv, SvTYPE(proto)); |
a5f75d66 | 3090 | CvCLONED_on(cv); |
5f05dabc | 3091 | if (CvANON(proto)) |
3092 | CvANON_on(cv); | |
748a9306 | 3093 | |
11343788 | 3094 | #ifdef USE_THREADS |
12ca11f6 | 3095 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3096 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 MB |
3097 | CvOWNER(cv) = 0; |
3098 | #endif /* USE_THREADS */ | |
748a9306 | 3099 | CvFILEGV(cv) = CvFILEGV(proto); |
44a8e56a | 3100 | CvGV(cv) = (GV*)SvREFCNT_inc(CvGV(proto)); |
748a9306 LW |
3101 | CvSTASH(cv) = CvSTASH(proto); |
3102 | CvROOT(cv) = CvROOT(proto); | |
3103 | CvSTART(cv) = CvSTART(proto); | |
5f05dabc | 3104 | if (outside) |
3105 | CvOUTSIDE(cv) = (CV*)SvREFCNT_inc(outside); | |
748a9306 | 3106 | |
68dc0745 | 3107 | if (SvPOK(proto)) |
3108 | sv_setpvn((SV*)cv, SvPVX(proto), SvCUR(proto)); | |
3109 | ||
46fc3d4c | 3110 | comppad_name = newAV(); |
3111 | for (ix = fname; ix >= 0; ix--) | |
3112 | av_store(comppad_name, ix, SvREFCNT_inc(pname[ix])); | |
748a9306 LW |
3113 | |
3114 | comppad = newAV(); | |
3115 | ||
3116 | comppadlist = newAV(); | |
3117 | AvREAL_off(comppadlist); | |
46fc3d4c | 3118 | av_store(comppadlist, 0, (SV*)comppad_name); |
b355b4e0 | 3119 | av_store(comppadlist, 1, (SV*)comppad); |
748a9306 | 3120 | CvPADLIST(cv) = comppadlist; |
5f05dabc | 3121 | av_fill(comppad, AvFILL(protopad)); |
748a9306 LW |
3122 | curpad = AvARRAY(comppad); |
3123 | ||
3124 | av = newAV(); /* will be @_ */ | |
3125 | av_extend(av, 0); | |
3126 | av_store(comppad, 0, (SV*)av); | |
3127 | AvFLAGS(av) = AVf_REIFY; | |
3128 | ||
9607fc9c | 3129 | for (ix = fpad; ix > 0; ix--) { |
3130 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; | |
aa689395 | 3131 | if (namesv && namesv != &sv_undef) { |
3132 | char *name = SvPVX(namesv); /* XXX */ | |
3133 | if (SvFLAGS(namesv) & SVf_FAKE) { /* lexical from outside? */ | |
3134 | I32 off = pad_findlex(name, ix, SvIVX(namesv), | |
5f05dabc | 3135 | CvOUTSIDE(cv), cxstack_ix); |
3136 | if (!off) | |
3137 | curpad[ix] = SvREFCNT_inc(ppad[ix]); | |
3138 | else if (off != ix) | |
748a9306 LW |
3139 | croak("panic: cv_clone: %s", name); |
3140 | } | |
3141 | else { /* our own lexical */ | |
aa689395 | 3142 | SV* sv; |
5f05dabc | 3143 | if (*name == '&') { |
3144 | /* anon code -- we'll come back for it */ | |
3145 | sv = SvREFCNT_inc(ppad[ix]); | |
3146 | } | |
3147 | else if (*name == '@') | |
3148 | sv = (SV*)newAV(); | |
748a9306 | 3149 | else if (*name == '%') |
5f05dabc | 3150 | sv = (SV*)newHV(); |
748a9306 | 3151 | else |
5f05dabc | 3152 | sv = NEWSV(0,0); |
3153 | if (!SvPADBUSY(sv)) | |
3154 | SvPADMY_on(sv); | |
3155 | curpad[ix] = sv; | |
748a9306 LW |
3156 | } |
3157 | } | |
3158 | else { | |
aa689395 | 3159 | SV* sv = NEWSV(0,0); |
748a9306 | 3160 | SvPADTMP_on(sv); |
5f05dabc | 3161 | curpad[ix] = sv; |
748a9306 LW |
3162 | } |
3163 | } | |
3164 | ||
5f05dabc | 3165 | /* Now that vars are all in place, clone nested closures. */ |
3166 | ||
9607fc9c | 3167 | for (ix = fpad; ix > 0; ix--) { |
3168 | SV* namesv = (ix <= fname) ? pname[ix] : Nullsv; | |
aa689395 | 3169 | if (namesv |
3170 | && namesv != &sv_undef | |
3171 | && !(SvFLAGS(namesv) & SVf_FAKE) | |
3172 | && *SvPVX(namesv) == '&' | |
5f05dabc | 3173 | && CvCLONE(ppad[ix])) |
3174 | { | |
3175 | CV *kid = cv_clone2((CV*)ppad[ix], cv); | |
3176 | SvREFCNT_dec(ppad[ix]); | |
3177 | CvCLONE_on(kid); | |
3178 | SvPADMY_on(kid); | |
3179 | curpad[ix] = (SV*)kid; | |
748a9306 LW |
3180 | } |
3181 | } | |
3182 | ||
5f05dabc | 3183 | #ifdef DEBUG_CLOSURES |
ab50184a CS |
3184 | PerlIO_printf(Perl_debug_log, "Cloned inside:\n"); |
3185 | cv_dump(outside); | |
3186 | PerlIO_printf(Perl_debug_log, " from:\n"); | |
5f05dabc | 3187 | cv_dump(proto); |
ab50184a | 3188 | PerlIO_printf(Perl_debug_log, " to:\n"); |
5f05dabc | 3189 | cv_dump(cv); |
3190 | #endif | |
3191 | ||
748a9306 LW |
3192 | LEAVE; |
3193 | return cv; | |
3194 | } | |
3195 | ||
3196 | CV * | |
8ac85365 | 3197 | cv_clone(CV *proto) |
5f05dabc | 3198 | { |
3199 | return cv_clone2(proto, CvOUTSIDE(proto)); | |
3200 | } | |
3201 | ||
3fe9a6f1 | 3202 | void |
8ac85365 | 3203 | cv_ckproto(CV *cv, GV *gv, char *p) |
3fe9a6f1 | 3204 | { |
3205 | if ((!p != !SvPOK(cv)) || (p && strNE(p, SvPVX(cv)))) { | |
46fc3d4c | 3206 | SV* msg = sv_newmortal(); |
3fe9a6f1 | 3207 | SV* name = Nullsv; |
3208 | ||
3209 | if (gv) | |
46fc3d4c | 3210 | gv_efullname3(name = sv_newmortal(), gv, Nullch); |
3211 | sv_setpv(msg, "Prototype mismatch:"); | |
3212 | if (name) | |
fc36a67e | 3213 | sv_catpvf(msg, " sub %_", name); |
3fe9a6f1 | 3214 | if (SvPOK(cv)) |
46fc3d4c | 3215 | sv_catpvf(msg, " (%s)", SvPVX(cv)); |
3216 | sv_catpv(msg, " vs "); | |
3217 | if (p) | |
3218 | sv_catpvf(msg, "(%s)", p); | |
3219 | else | |
3220 | sv_catpv(msg, "none"); | |
fc36a67e | 3221 | warn("%_", msg); |
3fe9a6f1 | 3222 | } |
3223 | } | |
3224 | ||
760ac839 | 3225 | SV * |
8ac85365 | 3226 | cv_const_sv(CV *cv) |
760ac839 LW |
3227 | { |
3228 | OP *o; | |
54310121 | 3229 | SV *sv; |
aeea060c | 3230 | |
54310121 | 3231 | if (!cv || !SvPOK(cv) || SvCUR(cv)) |
3232 | return Nullsv; | |
760ac839 | 3233 | |
54310121 | 3234 | sv = Nullsv; |
3235 | for (o = CvSTART(cv); o; o = o->op_next) { | |
3236 | OPCODE type = o->op_type; | |
3237 | ||
3238 | if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK) | |
3239 | continue; | |
3240 | if (type == OP_LEAVESUB || type == OP_RETURN) | |
3241 | break; | |
3242 | if (sv) | |
3243 | return Nullsv; | |
3244 | if (type == OP_CONST) | |
5dc0d613 | 3245 | sv = cSVOPo->op_sv; |
54310121 | 3246 | else if (type == OP_PADSV) { |
e858de61 MB |
3247 | AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]); |
3248 | sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv; | |
5aabfad6 | 3249 | if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1)) |
54310121 | 3250 | return Nullsv; |
760ac839 | 3251 | } |
54310121 | 3252 | else |
3253 | return Nullsv; | |
760ac839 | 3254 | } |
5aabfad6 | 3255 | if (sv) |
3256 | SvREADONLY_on(sv); | |
760ac839 LW |
3257 | return sv; |
3258 | } | |
3259 | ||
748a9306 | 3260 | CV * |
8ac85365 | 3261 | newSUB(I32 floor, OP *o, OP *proto, OP *block) |
79072805 | 3262 | { |
11343788 | 3263 | dTHR; |
5dc0d613 | 3264 | char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch; |
44a8e56a | 3265 | GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV); |
3fe9a6f1 | 3266 | char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch; |
79072805 | 3267 | register CV *cv; |
a0d0e21e | 3268 | I32 ix; |
79072805 | 3269 | |
11343788 | 3270 | if (o) |
5dc0d613 | 3271 | SAVEFREEOP(o); |
3fe9a6f1 | 3272 | if (proto) |
3273 | SAVEFREEOP(proto); | |
3274 | ||
68dc0745 | 3275 | if (!name || GvCVGEN(gv)) |
3276 | cv = Nullcv; | |
3277 | else if (cv = GvCV(gv)) { | |
3fe9a6f1 | 3278 | cv_ckproto(cv, gv, ps); |
68dc0745 | 3279 | /* already defined (or promised)? */ |
3280 | if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { | |
3281 | SV* const_sv; | |
aa689395 | 3282 | if (!block) { |
3283 | /* just a "sub foo;" when &foo is already defined */ | |
3284 | SAVEFREESV(compcv); | |
3285 | goto done; | |
3286 | } | |
7bac28a0 | 3287 | /* ahem, death to those who redefine active sort subs */ |
3288 | if (curstack == sortstack && sortcop == CvSTART(cv)) | |
3289 | croak("Can't redefine active sort subroutine %s", name); | |
68dc0745 | 3290 | const_sv = cv_const_sv(cv); |
44a8e56a | 3291 | if (const_sv || dowarn) { |
79072805 | 3292 | line_t oldline = curcop->cop_line; |
79072805 | 3293 | curcop->cop_line = copline; |
760ac839 | 3294 | warn(const_sv ? "Constant subroutine %s redefined" |
68dc0745 | 3295 | : "Subroutine %s redefined", name); |
79072805 LW |
3296 | curcop->cop_line = oldline; |
3297 | } | |
8990e307 | 3298 | SvREFCNT_dec(cv); |
68dc0745 | 3299 | cv = Nullcv; |
79072805 LW |
3300 | } |
3301 | } | |
a0d0e21e | 3302 | if (cv) { /* must reuse cv if autoloaded */ |
4633a7c4 | 3303 | cv_undef(cv); |
44a8e56a | 3304 | CvFLAGS(cv) = CvFLAGS(compcv); |
748a9306 | 3305 | CvOUTSIDE(cv) = CvOUTSIDE(compcv); |
e9a444f0 | 3306 | CvOUTSIDE(compcv) = 0; |
748a9306 | 3307 | CvPADLIST(cv) = CvPADLIST(compcv); |
4aa0a1f7 | 3308 | CvPADLIST(compcv) = 0; |
4633a7c4 LW |
3309 | if (SvREFCNT(compcv) > 1) /* XXX Make closures transit through stub. */ |
3310 | CvOUTSIDE(compcv) = (CV*)SvREFCNT_inc((SV*)cv); | |
748a9306 | 3311 | SvREFCNT_dec(compcv); |
a0d0e21e LW |
3312 | } |
3313 | else { | |
748a9306 | 3314 | cv = compcv; |
44a8e56a | 3315 | if (name) { |
3316 | GvCV(gv) = cv; | |
3317 | GvCVGEN(gv) = 0; | |
3318 | sub_generation++; | |
3319 | } | |
a0d0e21e | 3320 | } |
44a8e56a | 3321 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
79072805 | 3322 | CvFILEGV(cv) = curcop->cop_filegv; |
8990e307 | 3323 | CvSTASH(cv) = curstash; |
11343788 MB |
3324 | #ifdef USE_THREADS |
3325 | CvOWNER(cv) = 0; | |
12ca11f6 | 3326 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3327 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 | 3328 | #endif /* USE_THREADS */ |
8990e307 | 3329 | |
3fe9a6f1 | 3330 | if (ps) |
3331 | sv_setpv((SV*)cv, ps); | |
4633a7c4 | 3332 | |
c07a80fd | 3333 | if (error_count) { |
3334 | op_free(block); | |
3335 | block = Nullop; | |
68dc0745 | 3336 | if (name) { |
3337 | char *s = strrchr(name, ':'); | |
3338 | s = s ? s+1 : name; | |
6d4c2119 CS |
3339 | if (strEQ(s, "BEGIN")) { |
3340 | char *not_safe = | |
3341 | "BEGIN not safe after errors--compilation aborted"; | |
3342 | if (in_eval & 4) | |
3343 | croak(not_safe); | |
3344 | else { | |
3345 | /* force display of errors found but not reported */ | |
38a03e6e MB |
3346 | sv_catpv(ERRSV, not_safe); |
3347 | croak("%s", SvPVx(ERRSV, na)); | |
6d4c2119 CS |
3348 | } |
3349 | } | |
68dc0745 | 3350 | } |
c07a80fd | 3351 | } |
a0d0e21e | 3352 | if (!block) { |
a0d0e21e LW |
3353 | copline = NOLINE; |
3354 | LEAVE_SCOPE(floor); | |
3355 | return cv; | |
3356 | } | |
3357 | ||
54310121 | 3358 | if (AvFILL(comppad_name) < AvFILL(comppad)) |
3359 | av_store(comppad_name, AvFILL(comppad), Nullsv); | |
a0d0e21e | 3360 | |
54310121 | 3361 | if (CvCLONE(cv)) { |
3362 | SV **namep = AvARRAY(comppad_name); | |
3363 | for (ix = AvFILL(comppad); ix > 0; ix--) { | |
3364 | SV *namesv; | |
3365 | ||
3366 | if (SvIMMORTAL(curpad[ix])) | |
3367 | continue; | |
3368 | /* | |
3369 | * The only things that a clonable function needs in its | |
3370 | * pad are references to outer lexicals and anonymous subs. | |
3371 | * The rest are created anew during cloning. | |
3372 | */ | |
3373 | if (!((namesv = namep[ix]) != Nullsv && | |
3374 | namesv != &sv_undef && | |
3375 | (SvFAKE(namesv) || | |
3376 | *SvPVX(namesv) == '&'))) | |
3377 | { | |
3378 | SvREFCNT_dec(curpad[ix]); | |
3379 | curpad[ix] = Nullsv; | |
3380 | } | |
3381 | } | |
a0d0e21e | 3382 | } |
54310121 | 3383 | else { |
3384 | AV *av = newAV(); /* Will be @_ */ | |
3385 | av_extend(av, 0); | |
3386 | av_store(comppad, 0, (SV*)av); | |
3387 | AvFLAGS(av) = AVf_REIFY; | |
79072805 | 3388 | |
54310121 | 3389 | for (ix = AvFILL(comppad); ix > 0; ix--) { |
3390 | if (SvIMMORTAL(curpad[ix])) | |
3391 | continue; | |
3392 | if (!SvPADMY(curpad[ix])) | |
3393 | SvPADTMP_on(curpad[ix]); | |
3394 | } | |
3395 | } | |
79072805 | 3396 | |
a0d0e21e | 3397 | CvROOT(cv) = newUNOP(OP_LEAVESUB, 0, scalarseq(block)); |
79072805 LW |
3398 | CvSTART(cv) = LINKLIST(CvROOT(cv)); |
3399 | CvROOT(cv)->op_next = 0; | |
3400 | peep(CvSTART(cv)); | |
93a17b20 | 3401 | |
44a8e56a | 3402 | if (name) { |
3403 | char *s; | |
3404 | ||
84902520 | 3405 | if (PERLDB_SUBLINE && curstash != debstash) { |
46fc3d4c | 3406 | SV *sv = NEWSV(0,0); |
44a8e56a | 3407 | SV *tmpstr = sv_newmortal(); |
3408 | static GV *db_postponed; | |
3409 | CV *cv; | |
3410 | HV *hv; | |
3411 | ||
fc36a67e | 3412 | sv_setpvf(sv, "%_:%ld-%ld", |
46fc3d4c | 3413 | GvSV(curcop->cop_filegv), |
3414 | (long)subline, (long)curcop->cop_line); | |
44a8e56a | 3415 | gv_efullname3(tmpstr, gv, Nullch); |
3416 | hv_store(GvHV(DBsub), SvPVX(tmpstr), SvCUR(tmpstr), sv, 0); | |
3417 | if (!db_postponed) { | |
6bbcb22c | 3418 | db_postponed = gv_fetchpv("DB::postponed", GV_ADDMULTI, SVt_PVHV); |
44a8e56a | 3419 | } |
3420 | hv = GvHVn(db_postponed); | |
9607fc9c | 3421 | if (HvFILL(hv) > 0 && hv_exists(hv, SvPVX(tmpstr), SvCUR(tmpstr)) |
3422 | && (cv = GvCV(db_postponed))) { | |
44a8e56a | 3423 | dSP; |
3424 | PUSHMARK(sp); | |
3425 | XPUSHs(tmpstr); | |
3426 | PUTBACK; | |
3427 | perl_call_sv((SV*)cv, G_DISCARD); | |
3428 | } | |
3429 | } | |
79072805 | 3430 | |
44a8e56a | 3431 | if ((s = strrchr(name,':'))) |
28757baa | 3432 | s++; |
3433 | else | |
3434 | s = name; | |
68dc0745 | 3435 | if (strEQ(s, "BEGIN")) { |
2ae324a7 | 3436 | I32 oldscope = scopestack_ix; |
28757baa | 3437 | ENTER; |
3438 | SAVESPTR(compiling.cop_filegv); | |
3439 | SAVEI16(compiling.cop_line); | |
3440 | SAVEI32(perldb); | |
3441 | save_svref(&rs); | |
3442 | sv_setsv(rs, nrs); | |
3443 | ||
3444 | if (!beginav) | |
3445 | beginav = newAV(); | |
3446 | DEBUG_x( dump_sub(gv) ); | |
3447 | av_push(beginav, (SV *)cv); | |
3448 | GvCV(gv) = 0; | |
68dc0745 | 3449 | call_list(oldscope, beginav); |
a6006777 | 3450 | |
28757baa | 3451 | curcop = &compiling; |
3452 | LEAVE; | |
3453 | } | |
3454 | else if (strEQ(s, "END") && !error_count) { | |
3455 | if (!endav) | |
3456 | endav = newAV(); | |
3457 | av_unshift(endav, 1); | |
3458 | av_store(endav, 0, (SV *)cv); | |
3459 | GvCV(gv) = 0; | |
3460 | } | |
7d07dbc2 MB |
3461 | else if (strEQ(s, "INIT") && !error_count) { |
3462 | if (!initav) | |
3463 | initav = newAV(); | |
3464 | av_push(initav, SvREFCNT_inc(cv)); | |
ae77835f | 3465 | } |
79072805 | 3466 | } |
a6006777 | 3467 | |
aa689395 | 3468 | done: |
79072805 | 3469 | copline = NOLINE; |
8990e307 | 3470 | LEAVE_SCOPE(floor); |
a0d0e21e | 3471 | return cv; |
79072805 LW |
3472 | } |
3473 | ||
57d3b86d | 3474 | CV * |
aeea060c | 3475 | newXS(char *name, void (*subaddr) (CV *), char *filename) |
a0d0e21e | 3476 | { |
11343788 | 3477 | dTHR; |
44a8e56a | 3478 | GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV); |
79072805 | 3479 | register CV *cv; |
44a8e56a | 3480 | |
3481 | if (cv = (name ? GvCV(gv) : Nullcv)) { | |
3482 | if (GvCVGEN(gv)) { | |
3483 | /* just a cached method */ | |
3484 | SvREFCNT_dec(cv); | |
3485 | cv = 0; | |
3486 | } | |
3487 | else if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) { | |
3488 | /* already defined (or promised) */ | |
a0d0e21e LW |
3489 | if (dowarn) { |
3490 | line_t oldline = curcop->cop_line; | |
a0d0e21e LW |
3491 | curcop->cop_line = copline; |
3492 | warn("Subroutine %s redefined",name); | |
3493 | curcop->cop_line = oldline; | |
3494 | } | |
3495 | SvREFCNT_dec(cv); | |
3496 | cv = 0; | |
79072805 | 3497 | } |
79072805 | 3498 | } |
44a8e56a | 3499 | |
3500 | if (cv) /* must reuse cv if autoloaded */ | |
3501 | cv_undef(cv); | |
a0d0e21e LW |
3502 | else { |
3503 | cv = (CV*)NEWSV(1105,0); | |
3504 | sv_upgrade((SV *)cv, SVt_PVCV); | |
44a8e56a | 3505 | if (name) { |
3506 | GvCV(gv) = cv; | |
3507 | GvCVGEN(gv) = 0; | |
3508 | sub_generation++; | |
3509 | } | |
a0d0e21e | 3510 | } |
5196be3e | 3511 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
11343788 | 3512 | #ifdef USE_THREADS |
12ca11f6 | 3513 | New(666, CvMUTEXP(cv), 1, perl_mutex); |
11343788 | 3514 | MUTEX_INIT(CvMUTEXP(cv)); |
11343788 MB |
3515 | CvOWNER(cv) = 0; |
3516 | #endif /* USE_THREADS */ | |
79072805 | 3517 | CvFILEGV(cv) = gv_fetchfile(filename); |
a0d0e21e | 3518 | CvXSUB(cv) = subaddr; |
44a8e56a | 3519 | |
28757baa | 3520 | if (name) { |
3521 | char *s = strrchr(name,':'); | |
3522 | if (s) | |
3523 | s++; | |
3524 | else | |
3525 | s = name; | |
3526 | if (strEQ(s, "BEGIN")) { | |
3527 | if (!beginav) | |
3528 | beginav = newAV(); | |
44a8e56a | 3529 | av_push(beginav, (SV *)cv); |
3530 | GvCV(gv) = 0; | |
28757baa | 3531 | } |
3532 | else if (strEQ(s, "END")) { | |
3533 | if (!endav) | |
3534 | endav = newAV(); | |
3535 | av_unshift(endav, 1); | |
44a8e56a | 3536 | av_store(endav, 0, (SV *)cv); |
3537 | GvCV(gv) = 0; | |
28757baa | 3538 | } |
7d07dbc2 MB |
3539 | else if (strEQ(s, "INIT")) { |
3540 | if (!initav) | |
3541 | initav = newAV(); | |
3542 | av_push(initav, (SV *)cv); | |
ae77835f | 3543 | } |
28757baa | 3544 | } |
8990e307 | 3545 | else |
a5f75d66 | 3546 | CvANON_on(cv); |
44a8e56a | 3547 | |
a0d0e21e | 3548 | return cv; |
79072805 LW |
3549 | } |
3550 | ||
3551 | void | |
8ac85365 | 3552 | newFORM(I32 floor, OP *o, OP *block) |
79072805 | 3553 | { |
11343788 | 3554 | dTHR; |
79072805 LW |
3555 | register CV *cv; |
3556 | char *name; | |
3557 | GV *gv; | |
a0d0e21e | 3558 | I32 ix; |
79072805 | 3559 | |
11343788 MB |
3560 | if (o) |
3561 | name = SvPVx(cSVOPo->op_sv, na); | |
79072805 LW |
3562 | else |
3563 | name = "STDOUT"; | |
85e6fe83 | 3564 | gv = gv_fetchpv(name,TRUE, SVt_PVFM); |
a5f75d66 | 3565 | GvMULTI_on(gv); |
79072805 LW |
3566 | if (cv = GvFORM(gv)) { |
3567 | if (dowarn) { | |
3568 | line_t oldline = curcop->cop_line; | |
3569 | ||
3570 | curcop->cop_line = copline; | |
3571 | warn("Format %s redefined",name); | |
3572 | curcop->cop_line = oldline; | |
3573 | } | |
8990e307 | 3574 | SvREFCNT_dec(cv); |
79072805 | 3575 | } |
748a9306 | 3576 | cv = compcv; |
79072805 | 3577 | GvFORM(gv) = cv; |
44a8e56a | 3578 | CvGV(cv) = (GV*)SvREFCNT_inc(gv); |
79072805 LW |
3579 | CvFILEGV(cv) = curcop->cop_filegv; |
3580 | ||
a0d0e21e | 3581 | for (ix = AvFILL(comppad); ix > 0; ix--) { |
44a8e56a | 3582 | if (!SvPADMY(curpad[ix]) && !SvIMMORTAL(curpad[ix])) |
a0d0e21e LW |
3583 | SvPADTMP_on(curpad[ix]); |
3584 | } | |
3585 | ||
79072805 LW |
3586 | CvROOT(cv) = newUNOP(OP_LEAVEWRITE, 0, scalarseq(block)); |
3587 | CvSTART(cv) = LINKLIST(CvROOT(cv)); | |
3588 | CvROOT(cv)->op_next = 0; | |
3589 | peep(CvSTART(cv)); | |
11343788 | 3590 | op_free(o); |
79072805 | 3591 | copline = NOLINE; |
8990e307 | 3592 | LEAVE_SCOPE(floor); |
79072805 LW |
3593 | } |
3594 | ||
3595 | OP * | |
8ac85365 | 3596 | newANONLIST(OP *o) |
79072805 | 3597 | { |
93a17b20 | 3598 | return newUNOP(OP_REFGEN, 0, |
11343788 | 3599 | mod(list(convert(OP_ANONLIST, 0, o)), OP_REFGEN)); |
79072805 LW |
3600 | } |
3601 | ||
3602 | OP * | |
8ac85365 | 3603 | newANONHASH(OP *o) |
79072805 | 3604 | { |
93a17b20 | 3605 | return newUNOP(OP_REFGEN, 0, |
11343788 | 3606 | mod(list(convert(OP_ANONHASH, 0, o)), OP_REFGEN)); |
a0d0e21e LW |
3607 | } |
3608 | ||
3609 | OP * | |
8ac85365 | 3610 | newANONSUB(I32 floor, OP *proto, OP *block) |
a0d0e21e LW |
3611 | { |
3612 | return newUNOP(OP_REFGEN, 0, | |
4633a7c4 | 3613 | newSVOP(OP_ANONCODE, 0, (SV*)newSUB(floor, 0, proto, block))); |
79072805 LW |
3614 | } |
3615 | ||
3616 | OP * | |
8ac85365 | 3617 | oopsAV(OP *o) |
79072805 | 3618 | { |
ed6116ce LW |
3619 | switch (o->op_type) { |
3620 | case OP_PADSV: | |
3621 | o->op_type = OP_PADAV; | |
362 |