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