This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
File/Spec.pm needs trailing newline
[perl5.git] / op.c
diff --git a/op.c b/op.c
index 616b792..ecefb83 100644 (file)
--- a/op.c
+++ b/op.c
@@ -540,6 +540,11 @@ find_threadsv(char *name)
            sawampersand = TRUE;
            SvREADONLY_on(sv);
            /* FALL THROUGH */
+
+       /* XXX %! tied to Errno.pm needs to be added here.
+        * See gv_fetchpv(). */
+       /* case '!': */
+
        default:
            sv_magic(sv, 0, 0, name, 1); 
        }
@@ -614,6 +619,7 @@ op_free(OP *o)
        /* FALL THROUGH */
     case OP_PUSHRE:
     case OP_MATCH:
+    case OP_QR:
        ReREFCNT_dec(cPMOPo->op_pmregexp);
        break;
     }
@@ -720,6 +726,7 @@ scalar(OP *o)
        }
        /* FALL THROUGH */
     case OP_MATCH:
+    case OP_QR:
     case OP_SUBST:
     case OP_NULL:
     default:
@@ -777,6 +784,10 @@ scalarvoid(OP *o)
     case OP_REPEAT:
        if (o->op_flags & OPf_STACKED)
            break;
+       goto func_ops;
+    case OP_SUBSTR:
+       if (o->op_private == 4)
+           break;
        /* FALL THROUGH */
     case OP_GVSV:
     case OP_WANTARRAY:
@@ -793,7 +804,6 @@ scalarvoid(OP *o)
     case OP_HEX:
     case OP_OCT:
     case OP_LENGTH:
-    case OP_SUBSTR:
     case OP_VEC:
     case OP_INDEX:
     case OP_RINDEX:
@@ -846,6 +856,7 @@ scalarvoid(OP *o)
     case OP_GGRNAM:
     case OP_GGRGID:
     case OP_GETLOGIN:
+      func_ops:
        if (!(o->op_private & OPpLVAL_INTRO))
            useless = op_desc[o->op_type];
        break;
@@ -974,6 +985,7 @@ list(OP *o)
        break;
     default:
     case OP_MATCH:
+    case OP_QR:
     case OP_SUBST:
     case OP_NULL:
        if (!(o->op_flags & OPf_KIDS))
@@ -1201,10 +1213,14 @@ mod(OP *o, I32 type)
     case OP_KEYS:
        if (type != OP_SASSIGN)
            goto nomod;
+       goto lvalue_func;
+    case OP_SUBSTR:
+       if (o->op_private == 4) /* don't allow 4 arg substr as lvalue */
+           goto nomod;
        /* FALL THROUGH */
     case OP_POS:
     case OP_VEC:
-    case OP_SUBSTR:
+      lvalue_func:
        pad_free(o->op_targ);
        o->op_targ = pad_alloc(o->op_type, SVs_PADMY);
        assert(SvTYPE(PAD_SV(o->op_targ)) == SVt_NULL);
@@ -1248,6 +1264,7 @@ mod(OP *o, I32 type)
     else if (!type) {
        o->op_private |= OPpLVAL_INTRO;
        o->op_flags &= ~OPf_SPECIAL;
+       hints |= HINT_BLOCK_SCOPE;
     }
     else if (type != OP_GREPSTART && type != OP_ENTERSUB)
        o->op_flags |= OPf_REF;
@@ -1290,6 +1307,9 @@ scalar_mod_type(OP *o, I32 type)
     case OP_CONCAT:
     case OP_SUBST:
     case OP_TRANS:
+    case OP_READ:
+    case OP_SYSREAD:
+    case OP_RECV:
     case OP_ANDASSIGN: /* may work later */
     case OP_ORASSIGN:  /* may work later */
        return TRUE;
@@ -1504,11 +1524,21 @@ scope(OP *o)
     return o;
 }
 
+void
+save_hints(void)
+{
+    SAVEI32(hints);
+    SAVESPTR(GvHV(hintgv));
+    GvHV(hintgv) = newHVhv(GvHV(hintgv));
+    SAVEFREESV(GvHV(hintgv));
+}
+
 int
 block_start(int full)
 {
     dTHR;
     int retval = savestack_ix;
+
     SAVEI32(comppad_name_floor);
     if (full) {
        if ((comppad_name_fill = AvFILLp(comppad_name)) > 0)
@@ -1523,7 +1553,7 @@ block_start(int full)
     SAVEI32(padix_floor);
     padix_floor = padix;
     pad_reset_pending = FALSE;
-    SAVEI32(hints);
+    SAVEHINTS();
     hints &= ~HINT_BLOCK_SCOPE;
     return retval;
 }
@@ -1689,9 +1719,12 @@ fold_constants(register OP *o)
     if (type == OP_RV2GV)
        return newGVOP(OP_GV, 0, (GV*)sv);
     else {
-       if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK) {
+       /* try to smush double to int, but don't smush -2.0 to -2 */
+       if ((SvFLAGS(sv) & (SVf_IOK|SVf_NOK|SVf_POK)) == SVf_NOK &&
+           type != OP_NEGATE)
+       {
            IV iv = SvIV(sv);
-           if ((double)iv == SvNV(sv)) {       /* can we smush double to int */
+           if ((double)iv == SvNV(sv)) {
                SvREFCNT_dec(sv);
                sv = newSViv(iv);
            }
@@ -1950,12 +1983,6 @@ newUNOP(I32 type, I32 flags, OP *first)
     unop->op_first = first;
     unop->op_flags = flags | OPf_KIDS;
     unop->op_private = 1 | (flags >> 8);
-#if 1
-    if(type == OP_STUDY && first->op_type == OP_MATCH) {
-       first->op_type = OP_PUSHRE;
-       first->op_ppaddr = ppaddr[OP_PUSHRE];
-    }
-#endif
     unop = (UNOP*) CHECKOP(type, unop);
     if (unop->op_next)
        return (OP*)unop;
@@ -2073,8 +2100,11 @@ newPMOP(I32 type, I32 flags)
     pmop->op_flags = flags;
     pmop->op_private = 0 | (flags >> 8);
 
+    if (hints & HINT_RE_TAINT)
+       pmop->op_pmpermflags |= PMf_RETAINT;
     if (hints & HINT_LOCALE)
-       pmop->op_pmpermflags = (pmop->op_pmflags |= PMf_LOCALE);
+       pmop->op_pmpermflags |= PMf_LOCALE;
+    pmop->op_pmflags = pmop->op_pmpermflags;
 
     /* link into pm list */
     if (type != OP_TRANS && curstash) {
@@ -2088,8 +2118,10 @@ newPMOP(I32 type, I32 flags)
 OP *
 pmruntime(OP *o, OP *expr, OP *repl)
 {
+    dTHR;
     PMOP *pm;
     LOGOP *rcop;
+    I32 repl_has_vars = 0;
 
     if (o->op_type == OP_TRANS)
        return pmtrans(o, expr, repl);
@@ -2106,25 +2138,29 @@ pmruntime(OP *o, OP *expr, OP *repl)
            p = SvPV(pat, plen);
            pm->op_pmflags |= PMf_SKIPWHITE;
        }
-       pm->op_pmregexp = pregcomp(p, p + plen, pm);
+       pm->op_pmregexp = CALLREGCOMP(p, p + plen, pm);
        if (strEQ("\\s+", pm->op_pmregexp->precomp))
            pm->op_pmflags |= PMf_WHITE;
        op_free(expr);
     }
     else {
-       if (pm->op_pmflags & PMf_KEEP)
-           expr = newUNOP(OP_REGCMAYBE,0,expr);
+       if (pm->op_pmflags & PMf_KEEP || !(hints & HINT_RE_EVAL))
+           expr = newUNOP((!(hints & HINT_RE_EVAL) 
+                           ? OP_REGCRESET
+                           : OP_REGCMAYBE),0,expr);
 
        Newz(1101, rcop, 1, LOGOP);
        rcop->op_type = OP_REGCOMP;
        rcop->op_ppaddr = ppaddr[OP_REGCOMP];
        rcop->op_first = scalar(expr);
-       rcop->op_flags |= OPf_KIDS;
+       rcop->op_flags |= ((hints & HINT_RE_EVAL) 
+                          ? (OPf_SPECIAL | OPf_KIDS)
+                          : OPf_KIDS);
        rcop->op_private = 1;
        rcop->op_other = o;
 
        /* establish postfix order */
-       if (pm->op_pmflags & PMf_KEEP) {
+       if (pm->op_pmflags & PMf_KEEP || !(hints & HINT_RE_EVAL)) {
            LINKLIST(expr);
            rcop->op_next = expr;
            ((UNOP*)expr)->op_first->op_next = (OP*)rcop;
@@ -2156,13 +2192,15 @@ pmruntime(OP *o, OP *expr, OP *repl)
            for (curop = LINKLIST(repl); curop!=repl; curop = LINKLIST(curop)) {
                if (opargs[curop->op_type] & OA_DANGEROUS) {
 #ifdef USE_THREADS
-                   if (curop->op_type == OP_THREADSV
-                       && strchr("&`'123456789+", curop->op_private)) {
-                       break;
+                   if (curop->op_type == OP_THREADSV) {
+                       repl_has_vars = 1;
+                       if (strchr("&`'123456789+", curop->op_private))
+                           break;
                    }
 #else
                    if (curop->op_type == OP_GV) {
                        GV *gv = ((GVOP*)curop)->op_gv;
+                       repl_has_vars = 1;
                        if (strchr("&`'123456789+", *GvENAME(gv)))
                            break;
                    }
@@ -2180,20 +2218,29 @@ pmruntime(OP *o, OP *expr, OP *repl)
                             curop->op_type == OP_PADAV ||
                             curop->op_type == OP_PADHV ||
                             curop->op_type == OP_PADANY) {
-                            /* is okay */
+                       repl_has_vars = 1;
                    }
+                   else if (curop->op_type == OP_PUSHRE)
+                       ; /* Okay here, dangerous in newASSIGNOP */
                    else
                        break;
                }
                lastop = curop;
            }
        }
-       if (curop == repl) {
+       if (curop == repl
+           && !(repl_has_vars 
+                && (!pm->op_pmregexp 
+                    || pm->op_pmregexp->reganch & ROPT_EVAL_SEEN))) {
            pm->op_pmflags |= PMf_CONST;        /* const for long enough */
            pm->op_pmpermflags |= PMf_CONST;    /* const for long enough */
            prepend_elem(o->op_type, scalar(repl), o);
        }
        else {
+           if (curop == repl && !pm->op_pmregexp) { /* Has variables. */
+               pm->op_pmflags |= PMf_MAYBE_CONST;
+               pm->op_pmpermflags |= PMf_MAYBE_CONST;
+           }
            Newz(1101, rcop, 1, LOGOP);
            rcop->op_type = OP_SUBSTCONT;
            rcop->op_ppaddr = ppaddr[OP_SUBSTCONT];
@@ -2428,6 +2475,7 @@ newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
     }
 
     if (list_assignment(left)) {
+       dTHR;
        modcount = 0;
        eval_start = right;     /* Grandfathering $[ assignment here.  Bletch.*/
        left = mod(left, OP_AASSIGN);
@@ -2473,6 +2521,14 @@ newASSIGNOP(I32 flags, OP *left, I32 optype, OP *right)
                        if (lastop->op_type != OP_GV)   /* funny deref? */
                            break;
                    }
+                   else if (curop->op_type == OP_PUSHRE) {
+                       if (((PMOP*)curop)->op_pmreplroot) {
+                           GV *gv = (GV*)((PMOP*)curop)->op_pmreplroot;
+                           if (gv == defgv || SvCUR(gv) == generation)
+                               break;
+                           SvCUR(gv) = generation;
+                       }       
+                   }
                    else
                        break;
                }
@@ -2675,7 +2731,7 @@ new_logop(I32 type, I32 flags, OP** firstp, OP** otherp)
        case OP_NULL:
            if (k2 && k2->op_type == OP_READLINE
                  && (k2->op_flags & OPf_STACKED)
-                 && (k1->op_type == OP_RV2SV || k1->op_type == OP_PADSV))
+                 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 
                warnop = k2->op_type;
            break;
 
@@ -2837,6 +2893,24 @@ newLOOPOP(I32 flags, I32 debuggable, OP *expr, OP *block)
            || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB)) {
            expr = newUNOP(OP_DEFINED, 0,
                newASSIGNOP(0, newDEFSVOP(), 0, expr) );
+       } else if (expr->op_flags & OPf_KIDS) {
+           OP *k1 = ((UNOP*)expr)->op_first;
+           OP *k2 = (k1) ? k1->op_sibling : NULL;
+           switch (expr->op_type) {
+             case OP_NULL: 
+               if (k2 && k2->op_type == OP_READLINE
+                     && (k2->op_flags & OPf_STACKED)
+                     && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 
+                   expr = newUNOP(OP_DEFINED, 0, expr);
+               break;                                
+
+             case OP_SASSIGN:
+               if (k1->op_type == OP_READDIR
+                     || k1->op_type == OP_GLOB
+                     || k1->op_type == OP_EACH)
+                   expr = newUNOP(OP_DEFINED, 0, expr);
+               break;
+           }
        }
     }
 
@@ -2872,6 +2946,24 @@ newWHILEOP(I32 flags, I32 debuggable, LOOP *loop, I32 whileline, OP *expr, OP *b
                 || (expr->op_type == OP_NULL && expr->op_targ == OP_GLOB))) {
        expr = newUNOP(OP_DEFINED, 0,
            newASSIGNOP(0, newDEFSVOP(), 0, expr) );
+    } else if (expr && (expr->op_flags & OPf_KIDS)) {
+       OP *k1 = ((UNOP*)expr)->op_first;
+       OP *k2 = (k1) ? k1->op_sibling : NULL;
+       switch (expr->op_type) {
+         case OP_NULL: 
+           if (k2 && k2->op_type == OP_READLINE
+                 && (k2->op_flags & OPf_STACKED)
+                 && ((k1->op_flags & OPf_WANT) == OPf_WANT_SCALAR)) 
+               expr = newUNOP(OP_DEFINED, 0, expr);
+           break;                                
+
+         case OP_SASSIGN:
+           if (k1->op_type == OP_READDIR
+                 || k1->op_type == OP_GLOB
+                 || k1->op_type == OP_EACH)
+               expr = newUNOP(OP_DEFINED, 0, expr);
+           break;
+       }
     }
 
     if (!block)
@@ -2968,12 +3060,44 @@ newFOROP(I32 flags,char *label,line_t forline,OP *sv,OP *expr,OP *block,OP *cont
 #endif
     }
     if (expr->op_type == OP_RV2AV || expr->op_type == OP_PADAV) {
-       expr = scalar(ref(expr, OP_ITER));
+       expr = mod(force_list(scalar(ref(expr, OP_ITER))), OP_GREPSTART);
+       iterflags |= OPf_STACKED;
+    }
+    else if (expr->op_type == OP_NULL &&
+             (expr->op_flags & OPf_KIDS) &&
+             ((BINOP*)expr)->op_first->op_type == OP_FLOP)
+    {
+       /* Basically turn for($x..$y) into the same as for($x,$y), but we
+        * set the STACKED flag to indicate that these values are to be
+        * treated as min/max values by 'pp_iterinit'.
+        */
+       UNOP* flip = (UNOP*)((UNOP*)((BINOP*)expr)->op_first)->op_first;
+       CONDOP* range = (CONDOP*) flip->op_first;
+       OP* left  = range->op_first;
+       OP* right = left->op_sibling;
+       LISTOP* listop;
+
+       range->op_flags &= ~OPf_KIDS;
+       range->op_first = Nullop;
+
+       listop = (LISTOP*)newLISTOP(OP_LIST, 0, left, right);
+       listop->op_first->op_next = range->op_true;
+       left->op_next = range->op_false;
+       right->op_next = (OP*)listop;
+       listop->op_next = listop->op_first;
+
+       op_free(expr);
+       expr = (OP*)(listop);
+        null(expr);
        iterflags |= OPf_STACKED;
     }
+    else {
+        expr = mod(force_list(expr), OP_GREPSTART);
+    }
+
+
     loop = (LOOP*)list(convert(OP_ENTERITER, iterflags,
-       append_elem(OP_LIST, mod(force_list(expr), OP_GREPSTART),
-                   scalar(sv))));
+                              append_elem(OP_LIST, expr, scalar(sv))));
     assert(!loop->op_next);
     Renew(loop, 1, LOOP);
     loop->op_targ = padoff;
@@ -3276,16 +3400,27 @@ cv_ckproto(CV *cv, GV *gv, char *p)
 SV *
 cv_const_sv(CV *cv)
 {
-    OP *o;
-    SV *sv;
-
     if (!cv || !SvPOK(cv) || SvCUR(cv))
        return Nullsv;
+    return op_const_sv(CvSTART(cv), cv);
+}
 
-    sv = Nullsv;
-    for (o = CvSTART(cv); o; o = o->op_next) {
+SV *
+op_const_sv(OP *o, CV *cv)
+{
+    SV *sv = Nullsv;
+
+    if(!o)
+       return Nullsv;
+    if(o->op_type == OP_LINESEQ && cLISTOPo->op_first) 
+       o = cLISTOPo->op_first->op_sibling;
+
+    for (; o; o = o->op_next) {
        OPCODE type = o->op_type;
-       
+
+       if(sv && o->op_next == o) 
+           return sv;
        if (type == OP_NEXTSTATE || type == OP_NULL || type == OP_PUSHMARK)
            continue;
        if (type == OP_LEAVESUB || type == OP_RETURN)
@@ -3294,7 +3429,7 @@ cv_const_sv(CV *cv)
            return Nullsv;
        if (type == OP_CONST)
            sv = cSVOPo->op_sv;
-       else if (type == OP_PADSV) {
+       else if (type == OP_PADSV && cv) {
            AV* padav = (AV*)(AvARRAY(CvPADLIST(cv))[1]);
            sv = padav ? AvARRAY(padav)[o->op_targ] : Nullsv;
            if (!sv || (!SvREADONLY(sv) && SvREFCNT(sv) > 1))
@@ -3313,9 +3448,10 @@ newSUB(I32 floor, OP *o, OP *proto, OP *block)
 {
     dTHR;
     char *name = o ? SvPVx(cSVOPo->op_sv, na) : Nullch;
-    GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
+    GV *gv = gv_fetchpv(name ? name : "__ANON__",
+                       GV_ADDMULTI | (block ? 0 : GV_NOINIT), SVt_PVCV);
     char *ps = proto ? SvPVx(((SVOP*)proto)->op_sv, na) : Nullch;
-    register CV *cv;
+    register CV *cv=0;
     I32 ix;
 
     if (o)
@@ -3323,6 +3459,23 @@ newSUB(I32 floor, OP *o, OP *proto, OP *block)
     if (proto)
        SAVEFREEOP(proto);
 
+    if (SvTYPE(gv) != SVt_PVGV) {      /* Prototype now, and had
+                                          maximum a prototype before. */
+       if (SvTYPE(gv) > SVt_NULL) {
+           if (!SvPOK((SV*)gv) && !(SvIOK((SV*)gv) && SvIVX((SV*)gv) == -1))
+               warn("Runaway prototype");
+           cv_ckproto((CV*)gv, NULL, ps);
+       }
+       if (ps)
+           sv_setpv((SV*)gv, ps);
+       else
+           sv_setiv((SV*)gv, -1);
+       SvREFCNT_dec(compcv);
+       cv = compcv = NULL;
+       sub_generation++;
+       goto noblock;
+    }
+
     if (!name || GvCVGEN(gv))
        cv = Nullcv;
     else if (cv = GvCV(gv)) {
@@ -3330,16 +3483,18 @@ newSUB(I32 floor, OP *o, OP *proto, OP *block)
        /* already defined (or promised)? */
        if (CvROOT(cv) || CvXSUB(cv) || GvASSUMECV(gv)) {
            SV* const_sv;
+           bool const_changed = TRUE;
            if (!block) {
                /* just a "sub foo;" when &foo is already defined */
                SAVEFREESV(compcv);
                goto done;
            }
            /* ahem, death to those who redefine active sort subs */
-           if (curstackinfo->si_type == SI_SORT && sortcop == CvSTART(cv))
+           if (curstackinfo->si_type == PERLSI_SORT && sortcop == CvSTART(cv))
                croak("Can't redefine active sort subroutine %s", name);
-           const_sv = cv_const_sv(cv);
-           if (const_sv || dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
+           if(const_sv = cv_const_sv(cv))
+               const_changed = sv_cmp(const_sv, op_const_sv(block, Nullcv));
+           if ((const_sv && const_changed) || dowarn && !(CvGV(cv) && GvSTASH(CvGV(cv))
                                        && HvNAME(GvSTASH(CvGV(cv)))
                                        && strEQ(HvNAME(GvSTASH(CvGV(cv))),
                                                 "autouse"))) {
@@ -3404,6 +3559,7 @@ newSUB(I32 floor, OP *o, OP *proto, OP *block)
        }
     }
     if (!block) {
+      noblock:
        copline = NOLINE;
        LEAVE_SCOPE(floor);
        return cv;
@@ -3488,7 +3644,6 @@ newSUB(I32 floor, OP *o, OP *proto, OP *block)
            ENTER;
            SAVESPTR(compiling.cop_filegv);
            SAVEI16(compiling.cop_line);
-           SAVEI32(perldb);
            save_svref(&rs);
            sv_setsv(rs, nrs);
 
@@ -3550,7 +3705,7 @@ newCONSTSUB(HV *stash, char *name, SV *sv)
 }
 
 CV *
-newXS(char *name, void (*subaddr) (CPERLproto_ CV *), char *filename)
+newXS(char *name, void (*subaddr) (CV * _CPERLproto), char *filename)
 {
     dTHR;
     GV *gv = gv_fetchpv(name ? name : "__ANON__", GV_ADDMULTI, SVt_PVCV);
@@ -4044,7 +4199,7 @@ ck_ftst(OP *o)
     if (o->op_flags & OPf_REF)
        return o;
 
-    if (o->op_flags & OPf_KIDS) {
+    if (o->op_flags & OPf_KIDS && cUNOPo->op_first->op_type != OP_STUB) {
        SVOP *kid = (SVOP*)cUNOPo->op_first;
 
        if (kid->op_type == OP_CONST && (kid->op_private & OPpCONST_BARE)) {
@@ -4834,7 +4989,7 @@ peep(register OP *o)
 
        case OP_PADAV:
            if (o->op_next->op_type == OP_RV2AV
-               && (o->op_next->op_flags && OPf_REF))
+               && (o->op_next->op_flags & OPf_REF))
            {
                null(o->op_next);
                o->op_next = o->op_next->op_next;
@@ -4843,7 +4998,7 @@ peep(register OP *o)
        
        case OP_PADHV:
            if (o->op_next->op_type == OP_RV2HV
-               && (o->op_next->op_flags && OPf_REF))
+               && (o->op_next->op_flags & OPf_REF))
            {
                null(o->op_next);
                o->op_next = o->op_next->op_next;
@@ -4873,6 +5028,7 @@ peep(register OP *o)
            peep(cLOOP->op_lastop);
            break;
 
+       case OP_QR:
        case OP_MATCH:
        case OP_SUBST:
            o->op_seq = op_seqmax++;
@@ -4883,6 +5039,8 @@ peep(register OP *o)
            o->op_seq = op_seqmax++;
            if (dowarn && o->op_next && o->op_next->op_type == OP_NEXTSTATE) {
                if (o->op_next->op_sibling &&
+                       o->op_next->op_sibling->op_type != OP_EXIT &&
+                       o->op_next->op_sibling->op_type != OP_WARN &&
                        o->op_next->op_sibling->op_type != OP_DIE) {
                    line_t oldline = curcop->cop_line;