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