Don’t allow name after our/state sub

It was a mistake that this was ever allowed.
padcv op type
Add padcv to Opcode.pm
Allocate ‘our sub’ in the pad

Currently the name is only allocated there.  Nothing fetches it yet.

Notes on the implementation:

S_pending_ident contains the logic for determining whether $foo or
@foo refers to a lexical or package variable.

yylex defers to S_pending_ident if PL_pending_ident is set.

The KEY_sub case in yylex is changed to set PL_pending_ident instead
of using force_word.  For package variables (including our),
S_pending_ident returns a WORD token, which is the same thing that
force_word produces.  So *that* aspect of this change does not affect
the grammar.  However....

The barestmt rule’s SUB branch begins with ‘SUB startsub subname’.
startsub is a null rule that creates a new sub in PL_compcv via
start_subparse().  subname is defined in terms of WORD and also checks
whether this is a special block, turning on CvSPECIAL(PL_compcv) if
it is.  That flag has to be visible during compilation of the sub.

But for a lexical name, such as ‘our foo’, to be allocated in the
right pad, it has to come *before* startsub, i.e., ‘SUB subname
startsub’.

But subname needs to modify the sub that startsub created, set-
ting the flag.

So I copied (not moved, because MYSUB still uses it) the name-checking
code from the subname rule into the SUB branch of barestmt.  Now that
uses WORD directly instead of invoking subname.  That allows the code
there to set everything up in the right order.
Make &foo respect our sub

This changes &foo to go through S_pending_ident (by setting
PL_pending_ident, which causes yylex to defer to S_pending_ident for
the next token) the way $foo and %foo do.

This necessitated reducing the maximum identifier length of &foo from
252 to 251, making it match @foo, $foo, etc.  So somebody’s JAPH might
break. :-)
Use test.pl in lexsub.t

I thought cmd/ couldn’t use test.pl, but was mistaken.
lexsub.t: Fix a test

This is not testing what I meant it to test: that ‘sub d’ will respect
a preceding ‘our sub d;’.  If ‘sub d’ is in the same package, it makes
no difference, so the test tests nothing.

It turns out this does not work yet.
Make do sub() respect our declarations
Fix our sub with proto

yylex must emit exactly one token each time it is called.  Some-
times yylex needs to parse several tokens at once.  That’s what
the various force functions are for.  But that is also what
PL_pending_ident is for.

The various force_next, force_word, force_ident, etc., functions keep
a stack of tokens (PL_nextval/PL_nexttype) that yylex will check imme-
diately when called.

PL_pending_ident is used to track a single identifier that yylex will
hand off to S_pending_ident to handle.

S_pending_ident is the only piece of code for resolving an identi-
fier that could be lexical but could also be a package variable.
force_ident assumes it is looking for a package variable.

force_* takes precedence over PL_pending_ident.

All this means that, if an identifier needs to be looked up in the pad
on the next yylex invocation, it has to use PL_pending_ident, and the
force_* functions cannot be used at the same time.

Not realising that, when I made ‘our sub foo’ store the sub in the
pad I also made ‘our sub foo ($)’ into a syntax error, because it
was being parsed as ‘our sub ($) foo’ (the prototype being ‘forced’);
i.e., the pending tokens were being pulled out of the ‘queue’ in the
wrong order.  (I put queue in quotes, because one queue and one unre-
lated buffer together don’t exactly count as ‘a queue’.)

Changing PL_pending_ident to have precedence over the force stack
breaks ext/XS-APItest/t/swaptwostmts.t, because the statement-parsing
interface does not localise PL_pending_ident.  It could be changed to
do that, but I don’t think it is the right solution.

Having two separate pending token mechanisms makes things need-
lessly fragile.

This commit eliminates the PL_pending_ident mechanism and
modifies S_pending_ident (renaming it in the process to
S_force_ident_maybe_lex) to work with the force mechanism.  I was
going to merge it with force_ident, but the two make incompatible
assumptions that just complicate the code if merged.  S_pending_ident
needs the sigil in the same string buffer, to pass to the pad inter-
face.  force_ident needs to be able to work without a sigil present.

So now we only have one queue for pending tokens and the order is more
predictable.
Make ‘sub foo{}’ respect ‘our foo’

This commit switches all sub definitions, whether with ‘our’ or not,
to using S_force_ident_maybe_lex (formerly known as S_pending_ident).

This means that an unqualified (no our/my/state or package prefix)
‘sub foo’ declaration does a pad lookup, just like $foo.

It turns out that the vivification that I added to the then
S_pending_ident for CVs was unnecessary and actually buggy.  We
*don’t* want to autovivify GVs for CVs, because they might be con-
stants or forward declarations, which are stored in a simpler form.

I also had to change the subname rule used by MYSUB in perly.y, since
it can now be fed a PRIVATEREF, which it does not expect.  This may
prove to be temporary, but it keeps current tests passing.
toke.c:yylex:KEY_sub can use PL_tokenbuf to begin with

There is no need to allocate a separate ‘tmpbuf’ and then copy it into
PL_tokenbuf afterwards.
Let barewords look up our subs

These take precedence over built-in keywords (just as my $AUTOLOAD
shadows the package var), but not the keyword plugin, as the latter
takes precedence over labels, and these don’t.
lexsub.t: Add test name, test override from another pkg

The bareword logic in toke.c looks up GVs in various places.  This
tests that we are bypassing those correctly.
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.