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