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