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