This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
Cast to signed before negating, to avoid compiler warnings
[perl5.git] / pp_ctl.c
index c442d0b..692fdeb 100644 (file)
--- a/pp_ctl.c
+++ b/pp_ctl.c
@@ -43,13 +43,20 @@ PP(pp_wantarray)
     dVAR;
     dSP;
     I32 cxix;
+    const PERL_CONTEXT *cx;
     EXTEND(SP, 1);
 
-    cxix = dopoptosub(cxstack_ix);
-    if (cxix < 0)
+    if (PL_op->op_private & OPpOFFBYONE) {
+       if (!(cx = caller_cx(1,NULL))) RETPUSHUNDEF;
+    }
+    else {
+      cxix = dopoptosub(cxstack_ix);
+      if (cxix < 0)
        RETPUSHUNDEF;
+      cx = &cxstack[cxix];
+    }
 
-    switch (cxstack[cxix].blk_gimme) {
+    switch (cx->blk_gimme) {
     case G_ARRAY:
        RETPUSHYES;
     case G_SCALAR:
@@ -1469,6 +1476,20 @@ Perl_is_lvalue_sub(pTHX)
        return 0;
 }
 
+/* only used by PUSHSUB */
+I32
+Perl_was_lvalue_sub(pTHX)
+{
+    dVAR;
+    const I32 cxix = dopoptosub(cxstack_ix-1);
+    assert(cxix >= 0);  /* We should only be called from inside subs */
+
+    if (CxLVAL(cxstack + cxix) && CvLVALUE(cxstack[cxix].blk_sub.cv))
+       return CxLVAL(cxstack + cxix);
+    else
+       return 0;
+}
+
 STATIC I32
 S_dopoptosub_at(pTHX_ const PERL_CONTEXT *cxstk, I32 startingblock)
 {
@@ -1636,8 +1657,8 @@ Perl_qerror(pTHX_ SV *err)
 
     if (PL_in_eval) {
        if (PL_in_eval & EVAL_KEEPERR) {
-               Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
-                              SvPV_nolen_const(err));
+               Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
+                                                    SVfARG(err));
        }
        else
            sv_catsv(ERRSV, err);
@@ -1742,20 +1763,21 @@ Perl_die_unwind(pTHX_ SV *msv)
            PL_curcop = oldcop;
 
            if (optype == OP_REQUIRE) {
-                const char* const msg = SvPVx_nolen_const(exceptsv);
                 (void)hv_store(GvHVn(PL_incgv),
-                               SvPVX_const(namesv), SvCUR(namesv),
+                               SvPVX_const(namesv),
+                               SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
                                &PL_sv_undef, 0);
                /* note that unlike pp_entereval, pp_require isn't
                 * supposed to trap errors. So now that we've popped the
                 * EVAL that pp_require pushed, and processed the error
                 * message, rethrow the error */
-               Perl_croak(aTHX_ "%sCompilation failed in require",
-                          *msg ? msg : "Unknown error\n");
+               Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
+                          SVfARG(exceptsv ? exceptsv : newSVpvs_flags("Unknown error\n",
+                                                                    SVs_TEMP)));
            }
            if (in_eval & EVAL_KEEPERR) {
-               Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %s",
-                              SvPV_nolen_const(exceptsv));
+               Perl_ck_warner(aTHX_ packWARN(WARN_MISC), "\t(in cleanup) %"SVf,
+                              SVfARG(exceptsv));
            }
            else {
                sv_setsv(ERRSV, exceptsv);
@@ -1848,13 +1870,17 @@ PP(pp_caller)
     register const PERL_CONTEXT *cx;
     const PERL_CONTEXT *dbcx;
     I32 gimme;
-    const char *stashname;
+    const HEK *stash_hek;
     I32 count = 0;
+    bool has_arg = MAXARG && TOPs;
 
-    if (MAXARG)
+    if (MAXARG) {
+      if (has_arg)
        count = POPi;
+      else (void)POPs;
+    }
 
-    cx = caller_cx(count, &dbcx);
+    cx = caller_cx(count + !!(PL_op->op_private & OPpOFFBYONE), &dbcx);
     if (!cx) {
        if (GIMME != G_ARRAY) {
            EXTEND(SP, 1);
@@ -1863,14 +1889,14 @@ PP(pp_caller)
        RETURN;
     }
 
-    stashname = CopSTASHPV(cx->blk_oldcop);
+    stash_hek = HvNAME_HEK((HV*)CopSTASH(cx->blk_oldcop));
     if (GIMME != G_ARRAY) {
         EXTEND(SP, 1);
-       if (!stashname)
+       if (!stash_hek)
            PUSHs(&PL_sv_undef);
        else {
            dTARGET;
-           sv_setpv(TARG, stashname);
+           sv_sethek(TARG, stash_hek);
            PUSHs(TARG);
        }
        RETURN;
@@ -1878,13 +1904,16 @@ PP(pp_caller)
 
     EXTEND(SP, 11);
 
-    if (!stashname)
+    if (!stash_hek)
        PUSHs(&PL_sv_undef);
-    else
-       mPUSHs(newSVpv(stashname, 0));
+    else {
+       dTARGET;
+       sv_sethek(TARG, stash_hek);
+       PUSHTARG;
+    }
     mPUSHs(newSVpv(OutCopFILE(cx->blk_oldcop), 0));
     mPUSHi((I32)CopLINE(cx->blk_oldcop));
-    if (!MAXARG)
+    if (!has_arg)
        RETURN;
     if (CxTYPE(cx) == CXt_SUB || CxTYPE(cx) == CXt_FORMAT) {
        GV * const cvgv = CvGV(dbcx->blk_sub.cv);
@@ -1936,8 +1965,7 @@ PP(pp_caller)
        AV * const ary = cx->blk_sub.argarray;
        const int off = AvARRAY(ary) - AvALLOC(ary);
 
-       if (!PL_dbargs)
-           Perl_init_dbargs(aTHX);
+       Perl_init_dbargs(aTHX);
 
        if (AvMAX(PL_dbargs) < AvFILLp(ary) + off)
            av_extend(PL_dbargs, AvFILLp(ary) + off);
@@ -1983,7 +2011,8 @@ PP(pp_reset)
 {
     dVAR;
     dSP;
-    const char * const tmps = (MAXARG < 1) ? (const char *)"" : POPpconstx;
+    const char * const tmps =
+       (MAXARG < 1 || (!TOPs && !POPs)) ? (const char *)"" : POPpconstx;
     sv_reset(tmps, CopSTASH(PL_curcop));
     PUSHs(&PL_sv_yes);
     RETURN;
@@ -2087,18 +2116,7 @@ PP(pp_enter)
 {
     dVAR; dSP;
     register PERL_CONTEXT *cx;
-    I32 gimme = OP_GIMME(PL_op, -1);
-
-    if (gimme == -1) {
-       if (cxstack_ix >= 0) {
-           /* If this flag is set, we're just inside a return, so we should
-            * store the caller's context */
-           gimme = (PL_op->op_flags & OPf_SPECIAL)
-               ? block_gimme()
-               : cxstack[cxstack_ix].blk_gimme;
-       } else
-           gimme = G_SCALAR;
-    }
+    I32 gimme = GIMME_V;
 
     ENTER_with_name("block");
 
@@ -2307,6 +2325,7 @@ S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
     if (gimme == G_SCALAR) {
        if (CxLVAL(cx) && !ref) {     /* Leave it as it is if we can. */
            SV *sv;
+           const char *what = NULL;
            if (MARK < SP) {
                assert(MARK+1 == SP);
                if ((SvPADTMP(TOPs) ||
@@ -2314,37 +2333,27 @@ S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
                       == SVf_READONLY
                    ) &&
                    !SvSMAGICAL(TOPs)) {
-                   LEAVE;
-                   cxstack_ix--;
-                   POPSUB(cx,sv);
-                   PL_curpm = newpm;
-                   LEAVESUB(sv);
-                   Perl_croak(aTHX_
-                       "Can't return %s from lvalue subroutine",
+                   what =
                        SvREADONLY(TOPs) ? (TOPs == &PL_sv_undef) ? "undef"
-                       : "a readonly value" : "a temporary");
-               }
-               else {                  /* Can be a localized value
-                   EXTEND_MORTAL(1);    * subject to deletion. */
-                   PL_tmps_stack[++PL_tmps_ix] = *SP;
-                   SvREFCNT_inc_void(*SP);
-                   *++newsp = *SP;
+                       : "a readonly value" : "a temporary";
                }
+               else goto copy_sv;
            }
            else {
                /* sub:lvalue{} will take us here. */
-               LEAVE;
-               cxstack_ix--;
-               POPSUB(cx,sv);
-               PL_curpm = newpm;
-               LEAVESUB(sv);
-               Perl_croak(aTHX_
-               /* diag_listed_as: Can't return %s from lvalue subroutine*/
-                         "Can't return undef from lvalue subroutine"
-               );
+               what = "undef";
            }
+           LEAVE;
+           cxstack_ix--;
+           POPSUB(cx,sv);
+           PL_curpm = newpm;
+           LEAVESUB(sv);
+           Perl_croak(aTHX_
+                     "Can't return %s from lvalue subroutine", what
+           );
        }
-       else if (MARK < SP) {
+       if (MARK < SP) {
+             copy_sv:
                if (cx->blk_sub.cv && CvDEPTH(cx->blk_sub.cv) > 1) {
                        *++newsp = SvREFCNT_inc(*SP);
                        FREETMPS;
@@ -2360,24 +2369,15 @@ S_return_lvalues(pTHX_ SV **mark, SV **sp, SV **newsp, I32 gimme,
            EXTEND(newsp,1);
            *++newsp = &PL_sv_undef;
        }
-       if (CxLVAL(cx) & OPpENTERSUB_DEREF) {
+       if (CxLVAL(cx) & OPpDEREF) {
            SvGETMAGIC(TOPs);
            if (!SvOK(TOPs)) {
-               U8 deref_type;
-               if (cx->blk_sub.retop->op_type == OP_RV2SV)
-                   deref_type = OPpDEREF_SV;
-               else if (cx->blk_sub.retop->op_type == OP_RV2AV)
-                   deref_type = OPpDEREF_AV;
-               else {
-                   assert(cx->blk_sub.retop->op_type == OP_RV2HV);
-                   deref_type = OPpDEREF_HV;
-               }
-               vivify_ref(TOPs, deref_type);
+               TOPs = vivify_ref(TOPs, CxLVAL(cx) & OPpDEREF);
            }
        }
     }
     else if (gimme == G_ARRAY) {
-       assert (!(CxLVAL(cx) & OPpENTERSUB_DEREF));
+       assert (!(CxLVAL(cx) & OPpDEREF));
        if (ref || !CxLVAL(cx))
            while (++MARK <= SP)
                *++newsp =
@@ -2422,7 +2422,6 @@ PP(pp_return)
     bool popsub2 = FALSE;
     bool clear_errsv = FALSE;
     bool lval = FALSE;
-    bool gmagic = FALSE;
     I32 gimme;
     SV **newsp;
     PMOP *newpm;
@@ -2465,7 +2464,6 @@ PP(pp_return)
        popsub2 = TRUE;
        lval = !!CvLVALUE(cx->blk_sub.cv);
        retop = cx->blk_sub.retop;
-       gmagic = CxLVAL(cx) & OPpENTERSUB_DEREF;
        cxstack_ix++; /* preserve cx entry on stack for use by POPSUB */
        break;
     case CXt_EVAL:
@@ -2481,7 +2479,8 @@ PP(pp_return)
        {
            /* Unassume the success we assumed earlier. */
            (void)hv_delete(GvHVn(PL_incgv),
-                           SvPVX_const(namesv), SvCUR(namesv),
+                           SvPVX_const(namesv),
+                            SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
                            G_DISCARD);
            DIE(aTHX_ "%"SVf" did not return a true value", SVfARG(namesv));
        }
@@ -2511,12 +2510,10 @@ PP(pp_return)
                        FREETMPS;
                        *++newsp = sv_mortalcopy(sv);
                        SvREFCNT_dec(sv);
-                       if (gmagic) SvGETMAGIC(sv);
                    }
                }
                else if (SvTEMP(*SP) && SvREFCNT(*SP) == 1) {
                    *++newsp = *SP;
-                   if (gmagic) SvGETMAGIC(*SP);
                }
                else
                    *++newsp = sv_mortalcopy(*SP);
@@ -2570,7 +2567,6 @@ PP(pp_leavesublv)
 
     POPBLOCK(cx,newpm);
     cxstack_ix++; /* temporarily protect top context */
-    assert(CvLVALUE(cx->blk_sub.cv));
 
     TAINT_NOT;
 
@@ -2822,8 +2818,9 @@ PP(pp_goto)
                    /* autoloaded stub? */
                    if (cv != GvCV(gv) && (cv = GvCV(gv)))
                        goto retry;
-                   autogv = gv_autoload4(GvSTASH(gv), GvNAME(gv),
-                                         GvNAMELEN(gv), FALSE);
+                   autogv = gv_autoload_pvn(GvSTASH(gv), GvNAME(gv),
+                                         GvNAMELEN(gv),
+                                          GvNAMEUTF8(gv) ? SVf_UTF8 : 0);
                    if (autogv && (cv = GvCV(autogv)))
                        goto retry;
                    tmpstr = sv_newmortal();
@@ -2926,6 +2923,7 @@ PP(pp_goto)
                        sub_crush_depth(cv);
                    pad_push(padlist, CvDEPTH(cv));
                }
+               PL_curcop = cx->blk_oldcop;
                SAVECOMPPAD();
                PAD_SET_CUR_NOSAVE(padlist, CvDEPTH(cv));
                if (CxHASARGS(cx))
@@ -3139,6 +3137,9 @@ PP(pp_exit)
 
     if (MAXARG < 1)
        anum = 0;
+    else if (!TOPs) {
+       anum = 0; (void)POPs;
+    }
     else {
        anum = SvIVx(POPs);
 #ifdef VMS
@@ -3477,6 +3478,7 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
     CvEVAL_on(PL_compcv);
     assert(CxTYPE(&cxstack[cxstack_ix]) == CXt_EVAL);
     cxstack[cxstack_ix].blk_eval.cv = PL_compcv;
+    cxstack[cxstack_ix].blk_gimme = gimme;
 
     CvOUTSIDE_SEQ(PL_compcv) = seq;
     CvOUTSIDE(PL_compcv) = MUTABLE_CV(SvREFCNT_inc_simple(outside));
@@ -3513,7 +3515,6 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
 
     PL_eval_root = NULL;
     PL_curcop = &PL_compiling;
-    CopARYBASE_set(PL_curcop, 0);
     if (saveop && (saveop->op_type != OP_REQUIRE) && (saveop->op_flags & OPf_SPECIAL))
        PL_in_eval |= EVAL_KEEPERR;
     else
@@ -3528,11 +3529,12 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
 
     if (yystatus || PL_parser->error_count || !PL_eval_root) {
        SV **newsp;                     /* Used by POPBLOCK. */
-       PERL_CONTEXT *cx = NULL;
+       PERL_CONTEXT *cx;
        I32 optype;                     /* Used by POPEVAL. */
-       SV *namesv = NULL;
-       const char *msg;
+       SV *namesv;
 
+       cx = NULL;
+       namesv = NULL;
        PERL_UNUSED_VAR(newsp);
        PERL_UNUSED_VAR(optype);
 
@@ -3554,7 +3556,6 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
        if (yystatus != 3)
            LEAVE_with_name("eval"); /* pp_entereval knows about this LEAVE.  */
 
-       msg = SvPVx_nolen_const(ERRSV);
        if (in_require) {
            if (!cx) {
                /* If cx is still NULL, it means that we didn't go in the
@@ -3564,21 +3565,26 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
                namesv = cx->blk_eval.old_namesv;
            }
            (void)hv_store(GvHVn(PL_incgv),
-                          SvPVX_const(namesv), SvCUR(namesv),
+                          SvPVX_const(namesv),
+                           SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
                           &PL_sv_undef, 0);
-           Perl_croak(aTHX_ "%sCompilation failed in require",
-                      *msg ? msg : "Unknown error\n");
+           Perl_croak(aTHX_ "%"SVf"Compilation failed in require",
+                      SVfARG(ERRSV
+                                ? ERRSV
+                                : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
        }
        else if (startop) {
            if (yystatus != 3) {
                POPBLOCK(cx,PL_curpm);
                POPEVAL(cx);
            }
-           Perl_croak(aTHX_ "%sCompilation failed in regexp",
-                      (*msg ? msg : "Unknown error\n"));
+           Perl_croak(aTHX_ "%"SVf"Compilation failed in regexp",
+                      SVfARG(ERRSV
+                                ? ERRSV
+                                : newSVpvs_flags("Unknown error\n", SVs_TEMP)));
        }
        else {
-           if (!*msg) {
+           if (!*(SvPVx_nolen_const(ERRSV))) {
                sv_setpvs(ERRSV, "Compilation error");
            }
        }
@@ -3592,15 +3598,6 @@ S_doeval(pTHX_ int gimme, OP** startop, CV* outside, U32 seq)
     } else
        SAVEFREEOP(PL_eval_root);
 
-    /* Set the context for this new optree.
-     * Propagate the context from the eval(). */
-    if ((gimme & G_WANT) == G_VOID)
-       scalarvoid(PL_eval_root);
-    else if ((gimme & G_WANT) == G_ARRAY)
-       list(PL_eval_root);
-    else
-       scalar(PL_eval_root);
-
     DEBUG_x(dump_eval());
 
     /* Register with debugger: */
@@ -3646,7 +3643,7 @@ S_check_type_and_open(pTHX_ SV *name)
     }
 
 #if !defined(PERLIO_IS_STDIO) && !defined(USE_SFIO)
-    return PerlIO_openn(aTHX_ NULL, PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
+    return PerlIO_openn(aTHX_ ":", PERL_SCRIPT_MODE, -1, 0, 0, NULL, 1, &name);
 #else
     return PerlIO_open(p, PERL_SCRIPT_MODE);
 #endif
@@ -4187,14 +4184,14 @@ PP(pp_entereval)
     SAVECOMPILEWARNINGS();
     PL_compiling.cop_warnings = DUP_WARNINGS(PL_curcop->cop_warnings);
     cophh_free(CopHINTHASH_get(&PL_compiling));
-    if (Perl_fetch_cop_label(aTHX_ PL_curcop, NULL, NULL)) {
+    if (Perl_cop_fetch_label(aTHX_ PL_curcop, NULL, NULL)) {
        /* The label, if present, is the first entry on the chain. So rather
           than writing a blank label in front of it (which involves an
           allocation), just use the next entry in the chain.  */
        PL_compiling.cop_hints_hash
            = cophh_copy(PL_curcop->cop_hints_hash->refcounted_he_next);
        /* Check the assumption that this removed the label.  */
-       assert(Perl_fetch_cop_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
+       assert(Perl_cop_fetch_label(aTHX_ &PL_compiling, NULL, NULL) == NULL);
     }
     else
        PL_compiling.cop_hints_hash = cophh_copy(PL_curcop->cop_hints_hash);
@@ -4278,7 +4275,8 @@ PP(pp_leaveeval)
     {
        /* Unassume the success we assumed earlier. */
        (void)hv_delete(GvHVn(PL_incgv),
-                       SvPVX_const(namesv), SvCUR(namesv),
+                       SvPVX_const(namesv),
+                        SvUTF8(namesv) ? -(I32)SvCUR(namesv) : (I32)SvCUR(namesv),
                        G_DISCARD);
        retop = Perl_die(aTHX_ "%"SVf" did not return a true value",
                               SVfARG(namesv));
@@ -4379,6 +4377,7 @@ PP(pp_entergiven)
     ENTER_with_name("given");
     SAVETMPS;
 
+    SAVECLEARSV(PAD_SVl(PL_op->op_targ));
     sv_setsv_mg(PAD_SV(PL_op->op_targ), POPs);
 
     PUSHBLOCK(cx, CXt_GIVEN, SP);
@@ -4456,14 +4455,14 @@ S_destroy_matcher(pTHX_ PMOP *matcher)
 PP(pp_smartmatch)
 {
     DEBUG_M(Perl_deb(aTHX_ "Starting smart match resolution\n"));
-    return do_smartmatch(NULL, NULL);
+    return do_smartmatch(NULL, NULL, 0);
 }
 
 /* This version of do_smartmatch() implements the
  * table of smart matches that is found in perlsyn.
  */
 STATIC OP *
-S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
+S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other, const bool copied)
 {
     dVAR;
     dSP;
@@ -4475,7 +4474,7 @@ S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
     /* Take care only to invoke mg_get() once for each argument.
      * Currently we do this by copying the SV if it's magical. */
     if (d) {
-       if (SvGMAGICAL(d))
+       if (!copied && SvGMAGICAL(d))
            d = sv_mortalcopy(d);
     }
     else
@@ -4786,7 +4785,7 @@ S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
                        
                        PUTBACK;
                        DEBUG_M(Perl_deb(aTHX_ "        recursively comparing array element...\n"));
-                       (void) do_smartmatch(seen_this, seen_other);
+                       (void) do_smartmatch(seen_this, seen_other, 0);
                        SPAGAIN;
                        DEBUG_M(Perl_deb(aTHX_ "        recursion finished\n"));
                        
@@ -4848,7 +4847,7 @@ S_do_smartmatch(pTHX_ HV *seen_this, HV *seen_other)
                    PUTBACK;
                    /* infinite recursion isn't supposed to happen here */
                    DEBUG_M(Perl_deb(aTHX_ "        recursively testing array element...\n"));
-                   (void) do_smartmatch(NULL, NULL);
+                   (void) do_smartmatch(NULL, NULL, 1);
                    SPAGAIN;
                    DEBUG_M(Perl_deb(aTHX_ "        recursion finished\n"));
                    if (SvTRUEx(POPs))
@@ -4998,8 +4997,7 @@ PP(pp_leavewhen)
        return cx->blk_loop.my_op->op_nextop;
     }
     else
-       /* RETURNOP calls PUTBACK which restores the old old sp */
-       return cx->blk_givwhen.leave_op;
+       RETURNOP(cx->blk_givwhen.leave_op);
 }
 
 PP(pp_continue)
@@ -5010,6 +5008,8 @@ PP(pp_continue)
     I32 gimme;
     SV **newsp;
     PMOP *newpm;
+
+    PERL_UNUSED_VAR(gimme);
     
     cxix = dopoptowhen(cxstack_ix); 
     if (cxix < 0)