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