Store state subs in the pad

In making ‘sub foo’ respect previous ‘our sub’ declarations in a
recent commit, I actually made ‘state sub foo’ into a syntax error.
(At the time, I patched up MYSUB in perly.y to keep the tests for ‘"my
sub" not yet implemented’ still working.)  Basically, it was creat-
ing an empty pad entry, but returning something that perly.y was not
expecting.

This commit adjusts the grammar to allow the SUB branch of barestmt to
accept a PRIVATEREF for its subname, in addition to a WORD.  It reuses
the subname rule that SUB used to use (before our subs were added),
gutting it to remove the special block handling, which SUB now tokes
care of.  That means the MYSUB rule will no longer turn on CvSPECIAL
on the PL_compcv that is going to be thrown away anyway.

The code for special blocks (BEGIN, END, etc.) that turns on CvSPECIAL
now checks for state subs and skips those.  It only applies to our
subs and package subs.

newMYSUB has now actually been written.  It basically duplicates
newATTRSUB, except for GV-specific things.  It does currently vivify a
GV and set CvGV, but I am hoping to change that later.  I also hope to
merge some of the code later, too.

I changed the prototype of newMYSUB to make it easier to use.  It is
not used anywhere on CPAN and has always simply died, so that should
be all right.
op.c:newMYSUB: disable stub optimisation

It will be a lot easier to get things working without this, for now.
It can be reënabled later.  It might not be worth it, though, as
AUTOLOADing will ignore lexical subs, and this optimisation is mainly
for AUTOLOAD stubs that are rarely used.
Look up state subs in the pad

This commit does just enough to get things compiling.  The padcv op
is still unimplemented (in fact, converting the padany to a padcv is
still not done), so you can’t actually run the code yet.

Bareword lookup in yylex now produces PRIVATEREF tokens for state
subs, so the grammar has been adjusted to accept a ‘subname’ in sub
calls (PRIVATEREF or WORD) where previously only a WORD was permitted.
Test state subs

Most of these tests are still to-do.  The previous commit got every-
thing compiling at least.  Then I went through putting eval{} around
all the dying tests and marking the failing tests as to-do.

At least this way I don’t have to do everything at once (even though
that was how I wrote the tests).

About the only thing that works is constant inlining, of all things.
Implement padcv

State subs can now be referenced and called.  Most of the tests in
lexsub.t are now passing.  I noticed mistakes in a couple of the
tests and corrected them.  In doing so I got an assertion failure
during compilation, so the tests in question I wrapped in a skipped
string eval.

State subs are now mostly working, but there are a few things to
clean up still.
Allow CVs to point to HEKs rather than GVs

This will allow named lexical subs to exist independent of GVs.
Lexical stubs should not AUTOLOAD

There is a feature that allows stubs to fall back to their GVs’
CVs when called.  If I reference a stub, e.g., \&bar, and then
bar is autoloaded, the AUTOLOAD sub assigning *bar = *foo or
*bar = sub {...}, I can still call the stub to which I have a refer-
ence, and it will fall back to the overloaded sub.

That is all fine and dandy, but it causes any stub that references a
GV via its CvGV pointer to call that GV’s CV.  If we name a lexical
sub by pointing its CvGV pointer at the GV whose name we want it to
have, then the lexical sub, if undefined, will try to fall back to an
autoloaded sub.

That causes things to gang agley in cases like this:

    use 5.01;
    sub foo { } # package sub
    state sub foo;
    foo(); # calls lexical sub; falls back to package sub

While we could fix this by flagging the sub and checking for the flag
in pp_entersub (as we do with anonymous subs), it is better simply to
use a HEK, instead of a GV.  Since a GV is quite heavyweight for stor-
ing just a name, I was going to do that anyway, eventually.  Doing it
now fixes a bug.
op.c:newMYSUB: inline var used only once

as of the previous commit
‘Undefined subroutine &foo called’ for lex subs

instead of just ‘Undefined subroutine called’ without the name.
Make pad_fixup_inner_anons cope with closed-over subs

When a sub starts being parsed, a new CV is created.  When it fin-
ishes, it is stored in its final location.  If there is a stub there
already, the pad is copied to the stub and the body attached thereto.

Since there may be closures inside the sub whose CvOUTSIDE
pointers point to the temporary CV used during compilation,
pad_fixup_inner_anons is called, to reassign all those
CvOUTSIDE pointers.

This happens in cases like this:

    sub f;
    sub f { sub { } }

When a sub closes over a lexical item in an outer sub, the inner sub
gets its own pad entry with the same value as the outer pad entry.

This means that, now that we have lexical subs (currently just state
subs), we can end up with a pad entry (&s) holding a sub whose
CvOUTSIDE does not point to the sub (f) that owns the pad:

    state sub s { }
    sub f { s() }

If the f sub has to reuse a stub, then pad_fixup_inner_anons gets to
see that, and complains bitterly:

$ ./perl -Ilib -E 'state sub s; sub f; sub f { s() }'
Assertion failed: (CvOUTSIDE(innercv) == old_cv), function Perl_pad_fixup_inner_anons, file pad.c, line 2095.
Abort trap
lexsub.t: Fix some tests

I got this working a few commits ago, but the tests mentioned the
wrong sub name.
Don’t say ‘variable &foo’ in warnings

It should be ‘subroutine &foo’.  (It could be ‘subroutine foo’, but we
use both forms elsewhere, and &foo is the easier to implement, the &
already being contained in the pad name.)
perldiag: closure referents → closure references

This goes back to 2ba9eb46.
Clone state subs in anon subs

Since state variables are not shared between closures, but only
between invocations of the same closure, state subs should behave
the same way.

This was a little tricky.  When we clone a sub, we now clone inner
state subs at the same time.  When walking through the pad, cloning
items, we cannot simply clone the inner sub when we see it, because it
may close over things we haven’t cloned yet:

    sub {
        state sub foo;
        my $x
        sub foo { $x }
    }

We can’t just delay cloning it and do it afterwards, because they may
be multiple subs closing over each other:

    sub {
       state sub foo;
       state sub bar;
       sub foo { \&bar }
       sub bar { \&foo }
    }

So *all* the entries in the new pad must be filled before any inner
subs can be cloned.

So what we do is put a stub in place of the cloned sub.   And then
in a second pass clone the inner subs, reusing the stubs from the
first pass.
lexsub.t: Fix another test

The problem with writing to-do tests is that it is very easy to get
the tests wrong, such that they continue to fail even when the prob-
lems they test for are fixed.
Remove & from redef warnings for lex subs

This is just for consistency with package subs.
dump.c: Dump CvNAME_HEK
op.c:newMYSUB: Pop scope after creating sub

I was popping the scope before creating the sub in order to expose the
parent pad, where the new sub is to be stored.

That can cause problems, since ops may still be created that get
attached to the new sub.  Those ops will end up using the parent sub’s
slab in that case.  If the parent sub does not finish compiling, due
to an error, it may clean out its slab, freeing ops that the inner sub
is using, so the inner sub, when freed, will try to free ops that are
no longer in allocated memory, as the slab is gone.  Most of the time,
the inner ops won’t have been reused for anything, so the op type will
still be OP_FREED, and op_free will do nothing (except a single bad
read).  But debugging builds detect that and fail an assertion.

Popping the scope afterwards actually does simplify things, surpris-
ingly enough.

I was able to produce this bug with a one-liner, but it did not fail
as part of the test suite.  So this fix includes no test.

Since the o variable in newMYSUB is a padop, it can only be freed when
its pad is active.  It is created before the sub, so it cannot be
freed until the scope has been popped, so it has to go at the bot-
tom.  If an error occurs during newMYSUB, opslab_force_free will take
care of it.
First stab at my sub

This does just enough to get things to compile.

They currently do weird things in edge cases, including ‘Bizarre
copy of CODE’.

‘my sub’ now produces a SUB token, and goes through the same grammar
rule as ‘state sub’ and just plain ‘sub’.  The separate MYSUB branch
of the barestmt rule will go soon, as it is now unused.
Add proto magic type

This will be used for storing the prototype CV of a ‘my’ sub.  The
clone needs to occupy the pad entry so that padcv ops will be able to
find it.  That means the clone has to displace its prototype.  In case
the same sub is called recursively, we still need to be able to access
the prototype.