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