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