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