support custom non-moose type constraints (#85851)
[p5sagit/Function-Parameters.git] / Parameters.xs
CommitLineData
db81d362 1/*
2Copyright 2012 Lukas Mai.
3
4This program is free software; you can redistribute it and/or modify it
5under the terms of either: the GNU General Public License as published
6by the Free Software Foundation; or the Artistic License.
7
8See http://dev.perl.org/licenses/ for more information.
9 */
10
11#ifdef __GNUC__
12 #if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ >= 5
13 #define PRAGMA_GCC_(X) _Pragma(#X)
14 #define PRAGMA_GCC(X) PRAGMA_GCC_(GCC X)
15 #endif
16#endif
17
18#ifndef PRAGMA_GCC
19 #define PRAGMA_GCC(X)
20#endif
21
22#ifdef DEVEL
23 #define WARNINGS_RESET PRAGMA_GCC(diagnostic pop)
24 #define WARNINGS_ENABLEW(X) PRAGMA_GCC(diagnostic warning #X)
25 #define WARNINGS_ENABLE \
26 WARNINGS_ENABLEW(-Wall) \
27 WARNINGS_ENABLEW(-Wextra) \
28 WARNINGS_ENABLEW(-Wundef) \
c3e72f35 29 /* WARNINGS_ENABLEW(-Wshadow) :-( */ \
db81d362 30 WARNINGS_ENABLEW(-Wbad-function-cast) \
31 WARNINGS_ENABLEW(-Wcast-align) \
32 WARNINGS_ENABLEW(-Wwrite-strings) \
33 /* WARNINGS_ENABLEW(-Wnested-externs) wtf? */ \
34 WARNINGS_ENABLEW(-Wstrict-prototypes) \
35 WARNINGS_ENABLEW(-Wmissing-prototypes) \
36 WARNINGS_ENABLEW(-Winline) \
37 WARNINGS_ENABLEW(-Wdisabled-optimization)
38
39#else
40 #define WARNINGS_RESET
1bc47886 41 #define WARNINGS_ENABLE
db81d362 42#endif
43
44
7dd35535 45#define PERL_NO_GET_CONTEXT
db81d362 46#include "EXTERN.h"
47#include "perl.h"
48#include "XSUB.h"
49
50#include <string.h>
51
63915d26 52
db81d362 53WARNINGS_ENABLE
54
db81d362 55
7dd35535 56#define HAVE_PERL_VERSION(R, V, S) \
57 (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
58
de013990 59#if HAVE_PERL_VERSION(5, 16, 0)
60 #define IF_HAVE_PERL_5_16(YES, NO) YES
61#else
62 #define IF_HAVE_PERL_5_16(YES, NO) NO
63#endif
64
e158cf8f 65#if 0
66 #if HAVE_PERL_VERSION(5, 17, 6)
67 #error "internal error: missing definition of KEY_my (your perl is too new)"
68 #elif HAVE_PERL_VERSION(5, 15, 8)
69 #define S_KEY_my 134
70 #elif HAVE_PERL_VERSION(5, 15, 6)
71 #define S_KEY_my 133
72 #elif HAVE_PERL_VERSION(5, 15, 5)
73 #define S_KEY_my 132
74 #elif HAVE_PERL_VERSION(5, 13, 0)
75 #define S_KEY_my 131
76 #else
77 #error "internal error: missing definition of KEY_my (your perl is too old)"
78 #endif
79#endif
80
63915d26 81
82#define MY_PKG "Function::Parameters"
83
84#define HINTK_KEYWORDS MY_PKG "/keywords"
85#define HINTK_FLAGS_ MY_PKG "/flags:"
86#define HINTK_SHIFT_ MY_PKG "/shift:"
87#define HINTK_ATTRS_ MY_PKG "/attrs:"
88
89#define DEFSTRUCT(T) typedef struct T T; struct T
90
0f875412 91#define UV_BITS (sizeof (UV) * CHAR_BIT)
63915d26 92
93enum {
94 FLAG_NAME_OK = 0x01,
95 FLAG_ANON_OK = 0x02,
96 FLAG_DEFAULT_ARGS = 0x04,
d8e5d540 97 FLAG_CHECK_NARGS = 0x08,
e158cf8f 98 FLAG_INVOCANT = 0x10,
51a483f8 99 FLAG_NAMED_PARAMS = 0x20,
100 FLAG_TYPES_OK = 0x40,
101 FLAG_CHECK_TARGS = 0x80
63915d26 102};
103
104DEFSTRUCT(KWSpec) {
105 unsigned flags;
b72eb6ee 106 SV *shift;
107 SV *attrs;
63915d26 108};
db81d362 109
110static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
111
e4648f19 112DEFSTRUCT(Resource) {
113 Resource *next;
114 void *data;
115 void (*destroy)(pTHX_ void *);
116};
117
118typedef Resource *Sentinel[1];
119
120static void sentinel_clear_void(pTHX_ void *p) {
121 Resource **pp = p;
122 while (*pp) {
123 Resource *cur = *pp;
a4c13d40 124 if (cur->destroy) {
125 cur->destroy(aTHX_ cur->data);
126 }
e4648f19 127 cur->data = (void *)"no";
128 cur->destroy = NULL;
129 *pp = cur->next;
130 Safefree(cur);
131 }
132}
133
a4c13d40 134static Resource *sentinel_register(Sentinel sen, void *data, void (*destroy)(pTHX_ void *)) {
e4648f19 135 Resource *cur;
136
137 Newx(cur, 1, Resource);
138 cur->data = data;
139 cur->destroy = destroy;
140 cur->next = *sen;
141 *sen = cur;
a4c13d40 142
143 return cur;
144}
145
146static void sentinel_disarm(Resource *p) {
147 p->destroy = NULL;
e4648f19 148}
149
150static void my_sv_refcnt_dec_void(pTHX_ void *p) {
151 SV *sv = p;
152 SvREFCNT_dec(sv);
153}
154
155static SV *sentinel_mortalize(Sentinel sen, SV *sv) {
156 sentinel_register(sen, sv, my_sv_refcnt_dec_void);
157 return sv;
158}
159
db81d362 160
ed95eeff 161#if HAVE_PERL_VERSION(5, 17, 2)
162 #define MY_OP_SLABBED(O) ((O)->op_slabbed)
163#else
164 #define MY_OP_SLABBED(O) 0
165#endif
166
167DEFSTRUCT(OpGuard) {
168 OP *op;
169 bool needs_freed;
170};
171
172static void op_guard_init(OpGuard *p) {
173 p->op = NULL;
174 p->needs_freed = FALSE;
175}
176
177static OpGuard op_guard_transfer(OpGuard *p) {
178 OpGuard r = *p;
179 op_guard_init(p);
180 return r;
181}
182
183static OP *op_guard_relinquish(OpGuard *p) {
184 OP *o = p->op;
185 op_guard_init(p);
186 return o;
187}
188
189static void op_guard_update(OpGuard *p, OP *o) {
190 p->op = o;
191 p->needs_freed = o && !MY_OP_SLABBED(o);
192}
193
194static void op_guard_clear(pTHX_ OpGuard *p) {
195 if (p->needs_freed) {
196 op_free(p->op);
197 }
198}
199
200static void free_op_guard_void(pTHX_ void *vp) {
201 OpGuard *p = vp;
202 op_guard_clear(aTHX_ p);
203 Safefree(p);
c311cef3 204}
205
51a483f8 206static void free_op_void(pTHX_ void *vp) {
207 OP *p = vp;
208 op_free(p);
209}
210
59016bfb 211#define sv_eq_pvs(SV, S) my_sv_eq_pvn(aTHX_ SV, "" S "", sizeof (S) - 1)
c311cef3 212
59016bfb 213static int my_sv_eq_pvn(pTHX_ SV *sv, const char *p, STRLEN n) {
c311cef3 214 STRLEN sv_len;
215 const char *sv_p = SvPV(sv, sv_len);
59016bfb 216 return memcmp(sv_p, p, n) == 0;
c311cef3 217}
218
219
220#include "padop_on_crack.c.inc"
221
222
52c18b0f 223static void my_require(pTHX_ const char *file) {
224 SV *err;
225 require_pv(file);
226 err = ERRSV;
227 if (SvTRUE(err)) {
228 croak_sv(err);
229 }
230}
231
c311cef3 232enum {
233 MY_ATTR_LVALUE = 0x01,
234 MY_ATTR_METHOD = 0x02,
235 MY_ATTR_SPECIAL = 0x04
236};
237
31534187 238static void my_sv_cat_c(pTHX_ SV *sv, U32 c) {
239 char ds[UTF8_MAXBYTES + 1], *d;
67fc1ddb 240 d = (char *)uvchr_to_utf8((U8 *)ds, c);
31534187 241 if (d - ds > 1) {
242 sv_utf8_upgrade(sv);
243 }
244 sv_catpvn(sv, ds, d - ds);
245}
246
31534187 247
e6d62383 248#define MY_UNI_IDFIRST(C) isIDFIRST_uni(C)
249#define MY_UNI_IDCONT(C) isALNUM_uni(C)
31534187 250
e4648f19 251static SV *my_scan_word(pTHX_ Sentinel sen, bool allow_package) {
31534187 252 bool at_start, at_substart;
253 I32 c;
e4648f19 254 SV *sv = sentinel_mortalize(sen, newSVpvs(""));
31534187 255 if (lex_bufutf8()) {
256 SvUTF8_on(sv);
257 }
258
259 at_start = at_substart = TRUE;
260 c = lex_peek_unichar(0);
261
262 while (c != -1) {
e6d62383 263 if (at_substart ? MY_UNI_IDFIRST(c) : MY_UNI_IDCONT(c)) {
31534187 264 lex_read_unichar(0);
265 my_sv_cat_c(aTHX_ sv, c);
266 at_substart = FALSE;
267 c = lex_peek_unichar(0);
268 } else if (allow_package && !at_substart && c == '\'') {
269 lex_read_unichar(0);
270 c = lex_peek_unichar(0);
e6d62383 271 if (!MY_UNI_IDFIRST(c)) {
31534187 272 lex_stuff_pvs("'", 0);
273 break;
274 }
275 sv_catpvs(sv, "'");
276 at_substart = TRUE;
277 } else if (allow_package && (at_start || !at_substart) && c == ':') {
278 lex_read_unichar(0);
279 if (lex_peek_unichar(0) != ':') {
280 lex_stuff_pvs(":", 0);
281 break;
282 }
283 lex_read_unichar(0);
284 c = lex_peek_unichar(0);
e6d62383 285 if (!MY_UNI_IDFIRST(c)) {
31534187 286 lex_stuff_pvs("::", 0);
287 break;
288 }
289 sv_catpvs(sv, "::");
290 at_substart = TRUE;
291 } else {
292 break;
293 }
294 at_start = FALSE;
295 }
296
297 return SvCUR(sv) ? sv : NULL;
298}
299
e4648f19 300static SV *my_scan_parens_tail(pTHX_ Sentinel sen, bool keep_backslash) {
31534187 301 I32 c, nesting;
302 SV *sv;
303 line_t start;
304
305 start = CopLINE(PL_curcop);
306
e4648f19 307 sv = sentinel_mortalize(sen, newSVpvs(""));
31534187 308 if (lex_bufutf8()) {
309 SvUTF8_on(sv);
310 }
311
312 nesting = 0;
313 for (;;) {
314 c = lex_read_unichar(0);
315 if (c == EOF) {
316 CopLINE_set(PL_curcop, start);
317 return NULL;
318 }
319
320 if (c == '\\') {
321 c = lex_read_unichar(0);
322 if (c == EOF) {
323 CopLINE_set(PL_curcop, start);
324 return NULL;
325 }
326 if (keep_backslash || (c != '(' && c != ')')) {
327 sv_catpvs(sv, "\\");
328 }
329 } else if (c == '(') {
330 nesting++;
331 } else if (c == ')') {
332 if (!nesting) {
333 break;
334 }
335 nesting--;
336 }
337
338 my_sv_cat_c(aTHX_ sv, c);
339 }
340
341 return sv;
342}
343
e4648f19 344static void my_check_prototype(pTHX_ Sentinel sen, const SV *declarator, SV *proto) {
31534187 345 char *start, *r, *w, *end;
346 STRLEN len;
347
348 /* strip spaces */
349 start = SvPV(proto, len);
350 end = start + len;
351
352 for (w = r = start; r < end; r++) {
353 if (!isSPACE(*r)) {
354 *w++ = *r;
355 }
356 }
357 *w = '\0';
358 SvCUR_set(proto, w - start);
359 end = w;
360 len = end - start;
361
362 if (!ckWARN(WARN_ILLEGALPROTO)) {
363 return;
364 }
365
366 /* check for bad characters */
367 if (strspn(start, "$@%*;[]&\\_+") != len) {
e4648f19 368 SV *dsv = sentinel_mortalize(sen, newSVpvs(""));
31534187 369 warner(
370 packWARN(WARN_ILLEGALPROTO),
371 "Illegal character in prototype for %"SVf" : %s",
372 SVfARG(declarator),
373 SvUTF8(proto)
374 ? sv_uni_display(
375 dsv,
376 proto,
377 len,
378 UNI_DISPLAY_ISPRINT
379 )
380 : pv_pretty(dsv, start, len, 60, NULL, NULL,
381 PERL_PV_ESCAPE_NONASCII
382 )
383 );
384 return;
385 }
386
387 for (r = start; r < end; r++) {
388 switch (*r) {
389 default:
390 warner(
391 packWARN(WARN_ILLEGALPROTO),
392 "Illegal character in prototype for %"SVf" : %s",
393 SVfARG(declarator), r
394 );
395 return;
396
397 case '_':
398 if (r[1] && !strchr(";@%", *r)) {
399 warner(
400 packWARN(WARN_ILLEGALPROTO),
401 "Illegal character after '_' in prototype for %"SVf" : %s",
402 SVfARG(declarator), r
403 );
404 return;
405 }
406 break;
407
408 case '@':
409 case '%':
410 if (r[1]) {
411 warner(
412 packWARN(WARN_ILLEGALPROTO),
413 "prototype after '%c' for %"SVf": %s",
414 *r, SVfARG(declarator), r + 1
415 );
416 return;
417 }
418 break;
419
420 case '\\':
421 r++;
422 if (strchr("$@%&*", *r)) {
423 break;
424 }
425 if (*r == '[') {
426 r++;
427 for (; r < end && *r != ']'; r++) {
428 if (!strchr("$@%&*", *r)) {
429 break;
430 }
431 }
432 if (*r == ']' && r[-1] != '[') {
433 break;
434 }
435 }
436 warner(
437 packWARN(WARN_ILLEGALPROTO),
438 "Illegal character after '\\' in prototype for %"SVf" : %s",
439 SVfARG(declarator), r
440 );
441 return;
442
443 case '$':
444 case '*':
445 case '&':
446 case ';':
447 case '+':
448 break;
449 }
450 }
451}
452
51a483f8 453static SV *parse_type(pTHX_ Sentinel, const SV *);
454
455static SV *parse_type_paramd(pTHX_ Sentinel sen, const SV *declarator) {
456 I32 c;
457 SV *t;
458
459 t = my_scan_word(aTHX_ sen, TRUE);
460 lex_read_space(0);
461
462 c = lex_peek_unichar(0);
463 if (c == '[') {
464 SV *u;
465
466 lex_read_unichar(0);
467 lex_read_space(0);
468 my_sv_cat_c(aTHX_ t, c);
469
470 u = parse_type(aTHX_ sen, declarator);
471 sv_catsv(t, u);
472
473 c = lex_peek_unichar(0);
474 if (c != ']') {
475 croak("In %"SVf": missing ']' after '%"SVf"'", SVfARG(declarator), SVfARG(t));
476 }
477 lex_read_unichar(0);
478 lex_read_space(0);
479
480 my_sv_cat_c(aTHX_ t, c);
481 }
482
483 return t;
484}
485
486static SV *parse_type(pTHX_ Sentinel sen, const SV *declarator) {
487 I32 c;
488 SV *t;
489
490 t = parse_type_paramd(aTHX_ sen, declarator);
491
492 c = lex_peek_unichar(0);
493 while (c == '|') {
494 SV *u;
495
496 lex_read_unichar(0);
497 lex_read_space(0);
498
499 my_sv_cat_c(aTHX_ t, c);
500 u = parse_type_paramd(aTHX_ sen, declarator);
501 sv_catsv(t, u);
502
503 c = lex_peek_unichar(0);
504 }
505
506 return t;
507}
508
509static SV *reify_type(pTHX_ Sentinel sen, const SV *declarator, SV *name) {
510 SV *t;
511 int n;
512 dSP;
513
52c18b0f 514 my_require(aTHX_ "Moose/Util/TypeConstraints.pm");
515
51a483f8 516 ENTER;
517 SAVETMPS;
518
519 PUSHMARK(SP);
520 EXTEND(SP, 1);
521 PUSHs(name);
522 PUTBACK;
523
700071de 524 n = call_pv("Moose::Util::TypeConstraints::find_or_create_isa_type_constraint", G_SCALAR);
51a483f8 525 SPAGAIN;
526
527 assert(n == 1);
a8492ba4 528 /* don't warn about n being unused if assert() is compiled out */
529 n = n;
530
51a483f8 531 t = sentinel_mortalize(sen, SvREFCNT_inc(POPs));
532
533 PUTBACK;
534 FREETMPS;
535 LEAVE;
536
537 if (!SvTRUE(t)) {
538 croak("In %"SVf": undefined type '%"SVf"'", SVfARG(declarator), SVfARG(name));
539 }
540
541 return t;
542}
543
e158cf8f 544
545DEFSTRUCT(Param) {
546 SV *name;
547 PADOFFSET padoff;
51a483f8 548 SV *type;
e158cf8f 549};
550
551DEFSTRUCT(ParamInit) {
552 Param param;
ed95eeff 553 OpGuard init;
e158cf8f 554};
555
556#define VEC(B) B ## _Vec
557
558#define DEFVECTOR(B) DEFSTRUCT(VEC(B)) { \
559 B (*data); \
560 size_t used, size; \
561}
562
563DEFVECTOR(Param);
564DEFVECTOR(ParamInit);
565
566#define DEFVECTOR_INIT(N, B) static void N(VEC(B) *p) { \
567 p->used = 0; \
568 p->size = 23; \
569 Newx(p->data, p->size, B); \
570} static void N(VEC(B) *)
571
572DEFSTRUCT(ParamSpec) {
573 Param invocant;
574 VEC(Param) positional_required;
575 VEC(ParamInit) positional_optional;
576 VEC(Param) named_required;
577 VEC(ParamInit) named_optional;
578 Param slurpy;
fc634bba 579 PADOFFSET rest_hash;
e158cf8f 580};
581
582DEFVECTOR_INIT(pv_init, Param);
583DEFVECTOR_INIT(piv_init, ParamInit);
584
585static void p_init(Param *p) {
586 p->name = NULL;
587 p->padoff = NOT_IN_PAD;
51a483f8 588 p->type = NULL;
e158cf8f 589}
590
591static void ps_init(ParamSpec *ps) {
592 p_init(&ps->invocant);
593 pv_init(&ps->positional_required);
594 piv_init(&ps->positional_optional);
595 pv_init(&ps->named_required);
596 piv_init(&ps->named_optional);
597 p_init(&ps->slurpy);
fc634bba 598 ps->rest_hash = NOT_IN_PAD;
e158cf8f 599}
600
601#define DEFVECTOR_EXTEND(N, B) static B (*N(VEC(B) *p)) { \
602 assert(p->used <= p->size); \
603 if (p->used == p->size) { \
604 const size_t n = p->size / 2 * 3 + 1; \
605 Renew(p->data, n, B); \
606 p->size = n; \
607 } \
608 return &p->data[p->used]; \
609} static B (*N(VEC(B) *))
610
611DEFVECTOR_EXTEND(pv_extend, Param);
612DEFVECTOR_EXTEND(piv_extend, ParamInit);
613
614#define DEFVECTOR_CLEAR(N, B, F) static void N(pTHX_ VEC(B) *p) { \
615 while (p->used) { \
616 p->used--; \
617 F(aTHX_ &p->data[p->used]); \
618 } \
619 Safefree(p->data); \
620 p->data = NULL; \
621 p->size = 0; \
622} static void N(pTHX_ VEC(B) *)
623
624static void p_clear(pTHX_ Param *p) {
625 p->name = NULL;
626 p->padoff = NOT_IN_PAD;
51a483f8 627 p->type = NULL;
e158cf8f 628}
629
630static void pi_clear(pTHX_ ParamInit *pi) {
631 p_clear(aTHX_ &pi->param);
ed95eeff 632 op_guard_clear(aTHX_ &pi->init);
e158cf8f 633}
634
635DEFVECTOR_CLEAR(pv_clear, Param, p_clear);
636DEFVECTOR_CLEAR(piv_clear, ParamInit, pi_clear);
637
638static void ps_clear(pTHX_ ParamSpec *ps) {
639 p_clear(aTHX_ &ps->invocant);
640
641 pv_clear(aTHX_ &ps->positional_required);
642 piv_clear(aTHX_ &ps->positional_optional);
643
644 pv_clear(aTHX_ &ps->named_required);
645 piv_clear(aTHX_ &ps->named_optional);
646
647 p_clear(aTHX_ &ps->slurpy);
648}
649
650static int ps_contains(pTHX_ const ParamSpec *ps, SV *sv) {
651 size_t i, lim;
652
653 if (ps->invocant.name && sv_eq(sv, ps->invocant.name)) {
654 return 1;
655 }
656
657 for (i = 0, lim = ps->positional_required.used; i < lim; i++) {
658 if (sv_eq(sv, ps->positional_required.data[i].name)) {
659 return 1;
660 }
661 }
662
663 for (i = 0, lim = ps->positional_optional.used; i < lim; i++) {
664 if (sv_eq(sv, ps->positional_optional.data[i].param.name)) {
665 return 1;
666 }
667 }
668
669 for (i = 0, lim = ps->named_required.used; i < lim; i++) {
670 if (sv_eq(sv, ps->named_required.data[i].name)) {
671 return 1;
672 }
673 }
674
675 for (i = 0, lim = ps->named_optional.used; i < lim; i++) {
676 if (sv_eq(sv, ps->named_optional.data[i].param.name)) {
677 return 1;
678 }
679 }
680
681 return 0;
682}
683
684static void ps_free_void(pTHX_ void *p) {
685 ps_clear(aTHX_ p);
686 Safefree(p);
687}
688
689static int args_min(pTHX_ const ParamSpec *ps, const KWSpec *ks) {
690 int n = 0;
691 if (!ps) {
692 return SvTRUE(ks->shift) ? 1 : 0;
693 }
694 if (ps->invocant.name) {
695 n++;
696 }
697 n += ps->positional_required.used;
698 n += ps->named_required.used * 2;
699 return n;
700}
701
702static int args_max(const ParamSpec *ps) {
703 int n = 0;
704 if (!ps) {
705 return -1;
706 }
707 if (ps->invocant.name) {
708 n++;
709 }
710 n += ps->positional_required.used;
711 n += ps->positional_optional.used;
712 if (ps->named_required.used || ps->named_optional.used || ps->slurpy.name) {
713 n = -1;
714 }
715 return n;
716}
717
718static size_t count_positional_params(const ParamSpec *ps) {
719 return ps->positional_required.used + ps->positional_optional.used;
720}
721
722static size_t count_named_params(const ParamSpec *ps) {
723 return ps->named_required.used + ps->named_optional.used;
724}
725
51a483f8 726static SV *my_eval(pTHX_ Sentinel sen, I32 floor, OP *op) {
727 SV *sv;
728 CV *cv;
729 dSP;
730
731 cv = newATTRSUB(floor, NULL, NULL, NULL, op);
732
733 ENTER;
734 SAVETMPS;
735
736 PUSHMARK(SP);
737 call_sv((SV *)cv, G_SCALAR | G_NOARGS);
738 SPAGAIN;
739 sv = sentinel_mortalize(sen, SvREFCNT_inc(POPs));
740
741 PUTBACK;
742 FREETMPS;
743 LEAVE;
744
745 return sv;
746}
747
e158cf8f 748enum {
749 PARAM_INVOCANT = 0x01,
750 PARAM_NAMED = 0x02
751};
752
e158cf8f 753static PADOFFSET parse_param(
754 pTHX_
e4648f19 755 Sentinel sen,
e158cf8f 756 const SV *declarator, const KWSpec *spec, ParamSpec *param_spec,
ed95eeff 757 int *pflags, SV **pname, OpGuard *ginit, SV **ptype
e158cf8f 758) {
759 I32 c;
760 char sigil;
a4c13d40 761 SV *name;
e158cf8f 762
ed95eeff 763 assert(!ginit->op);
e158cf8f 764 *pflags = 0;
51a483f8 765 *ptype = NULL;
e158cf8f 766
767 c = lex_peek_unichar(0);
768
51a483f8 769 if (spec->flags & FLAG_TYPES_OK) {
770 if (c == '(') {
771 I32 floor;
772 OP *expr;
a4c13d40 773 Resource *expr_sentinel;
51a483f8 774
775 lex_read_unichar(0);
776
a4c13d40 777 floor = start_subparse(FALSE, 0);
778 SAVEFREESV(PL_compcv);
779 CvSPECIAL_on(PL_compcv);
51a483f8 780
781 if (!(expr = parse_fullexpr(PARSE_OPTIONAL))) {
782 croak("In %"SVf": invalid type expression", SVfARG(declarator));
783 }
ed95eeff 784 if (MY_OP_SLABBED(expr)) {
785 expr_sentinel = NULL;
786 } else {
787 expr_sentinel = sentinel_register(sen, expr, free_op_void);
788 }
51a483f8 789
790 lex_read_space(0);
791 c = lex_peek_unichar(0);
792 if (c != ')') {
793 croak("In %"SVf": missing ')' after type expression", SVfARG(declarator));
794 }
795 lex_read_unichar(0);
796 lex_read_space(0);
797
a4c13d40 798 SvREFCNT_inc_simple_void(PL_compcv);
ed95eeff 799 if (expr_sentinel) {
800 sentinel_disarm(expr_sentinel);
801 }
51a483f8 802 *ptype = my_eval(aTHX_ sen, floor, expr);
52c18b0f 803 if (!SvROK(*ptype)) {
804 *ptype = reify_type(aTHX_ sen, declarator, *ptype);
805 }
51a483f8 806 if (!sv_isobject(*ptype)) {
807 croak("In %"SVf": (%"SVf") doesn't look like a type object", SVfARG(declarator), SVfARG(*ptype));
808 }
809
810 c = lex_peek_unichar(0);
e6d62383 811 } else if (MY_UNI_IDFIRST(c)) {
a4c13d40 812 *ptype = parse_type(aTHX_ sen, declarator);
a4c13d40 813 *ptype = reify_type(aTHX_ sen, declarator, *ptype);
51a483f8 814
815 c = lex_peek_unichar(0);
816 }
817 }
818
e158cf8f 819 if (c == ':') {
820 lex_read_unichar(0);
821 lex_read_space(0);
822
823 *pflags |= PARAM_NAMED;
824
825 c = lex_peek_unichar(0);
826 }
827
828 if (c == -1) {
829 croak("In %"SVf": unterminated parameter list", SVfARG(declarator));
830 }
51a483f8 831
e158cf8f 832 if (!(c == '$' || c == '@' || c == '%')) {
833 croak("In %"SVf": unexpected '%c' in parameter list (expecting a sigil)", SVfARG(declarator), (int)c);
834 }
835
836 sigil = c;
837
838 lex_read_unichar(0);
839 lex_read_space(0);
840
e4648f19 841 if (!(name = my_scan_word(aTHX_ sen, FALSE))) {
e158cf8f 842 croak("In %"SVf": missing identifier after '%c'", SVfARG(declarator), sigil);
843 }
844 sv_insert(name, 0, 0, &sigil, 1);
845 *pname = name;
846
847 lex_read_space(0);
848 c = lex_peek_unichar(0);
849
850 if (c == '=') {
851 lex_read_unichar(0);
852 lex_read_space(0);
853
854
855 if (!param_spec->invocant.name && SvTRUE(spec->shift)) {
856 param_spec->invocant.name = spec->shift;
857 param_spec->invocant.padoff = pad_add_name_sv(param_spec->invocant.name, 0, NULL, NULL);
858 }
859
ed95eeff 860 op_guard_update(ginit, parse_termexpr(0));
e158cf8f 861
862 lex_read_space(0);
863 c = lex_peek_unichar(0);
864 }
865
866 if (c == ':') {
867 *pflags |= PARAM_INVOCANT;
868 lex_read_unichar(0);
869 lex_read_space(0);
870 } else if (c == ',') {
871 lex_read_unichar(0);
872 lex_read_space(0);
873 } else if (c != ')') {
874 if (c == -1) {
875 croak("In %"SVf": unterminated parameter list", SVfARG(declarator));
876 }
877 croak("In %"SVf": unexpected '%c' in parameter list (expecting ',')", SVfARG(declarator), (int)c);
878 }
879
880 return pad_add_name_sv(*pname, IF_HAVE_PERL_5_16(padadd_NO_DUP_CHECK, 0), NULL, NULL);
881}
882
883static OP *my_var_g(pTHX_ I32 type, I32 flags, PADOFFSET padoff) {
884 OP *var = newOP(type, flags);
885 var->op_targ = padoff;
886 return var;
887}
888
889static OP *my_var(pTHX_ I32 flags, PADOFFSET padoff) {
890 return my_var_g(aTHX_ OP_PADSV, flags, padoff);
891}
892
fc634bba 893static OP *mkhvelem(pTHX_ PADOFFSET h, OP *k) {
894 OP *hv = my_var_g(aTHX_ OP_PADHV, OPf_REF, h);
895 return newBINOP(OP_HELEM, 0, hv, k);
896}
897
898static OP *mkconstsv(pTHX_ SV *sv) {
899 return newSVOP(OP_CONST, 0, sv);
900}
901
902static OP *mkconstiv(pTHX_ IV i) {
903 return mkconstsv(aTHX_ newSViv(i));
904}
905
906static OP *mkconstpv(pTHX_ const char *p, size_t n) {
907 return mkconstsv(aTHX_ newSVpv(p, n));
908}
909
910#define mkconstpvs(S) mkconstpv(aTHX_ "" S "", sizeof S - 1)
911
51a483f8 912static OP *mktypecheck(pTHX_ const SV *declarator, int nr, SV *name, PADOFFSET padoff, SV *type) {
913 /* $type->check($value) or Carp::croak "...: " . $type->get_message($value) */
a8492ba4 914 OP *chk, *err, *msg, *xcroak;
51a483f8 915
916 err = mkconstsv(aTHX_ newSVpvf("In %"SVf": parameter %d (%"SVf"): ", SVfARG(declarator), nr, SVfARG(name)));
917 {
918 OP *args = NULL;
919
920 args = op_append_elem(OP_LIST, args, mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(type)));
921 args = op_append_elem(
922 OP_LIST, args,
923 padoff == NOT_IN_PAD
924 ? S_newDEFSVOP(aTHX)
925 : my_var(aTHX_ 0, padoff)
926 );
927 args = op_append_elem(OP_LIST, args, newUNOP(OP_METHOD, 0, mkconstpvs("get_message")));
928
929 msg = args;
930 msg->op_type = OP_ENTERSUB;
931 msg->op_ppaddr = PL_ppaddr[OP_ENTERSUB];
932 msg->op_flags |= OPf_STACKED;
933 }
934
935 msg = newBINOP(OP_CONCAT, 0, err, msg);
936
937 xcroak = newCVREF(
938 OPf_WANT_SCALAR,
939 newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
940 );
941 xcroak = newUNOP(OP_ENTERSUB, OPf_STACKED, op_append_elem(OP_LIST, msg, xcroak));
942
943 {
944 OP *args = NULL;
945
946 args = op_append_elem(OP_LIST, args, mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(type)));
947 args = op_append_elem(
948 OP_LIST, args,
949 padoff == NOT_IN_PAD
950 ? S_newDEFSVOP(aTHX)
951 : my_var(aTHX_ 0, padoff)
952 );
953 args = op_append_elem(OP_LIST, args, newUNOP(OP_METHOD, 0, mkconstpvs("check")));
954
955 chk = args;
956 chk->op_type = OP_ENTERSUB;
957 chk->op_ppaddr = PL_ppaddr[OP_ENTERSUB];
958 chk->op_flags |= OPf_STACKED;
959 }
960
961 chk = newLOGOP(OP_OR, 0, chk, xcroak);
962 return chk;
963}
964
965static OP *mktypecheckp(pTHX_ const SV *declarator, int nr, const Param *param) {
966 return mktypecheck(aTHX_ declarator, nr, param->name, param->padoff, param->type);
967}
968
53c979f0 969static void register_info(pTHX_ UV key, SV *declarator, const KWSpec *kws, const ParamSpec *ps) {
970 dSP;
971
972 ENTER;
973 SAVETMPS;
974
975 PUSHMARK(SP);
51a483f8 976 EXTEND(SP, 10);
53c979f0 977
978 /* 0 */ {
979 mPUSHu(key);
980 }
981 /* 1 */ {
982 size_t n;
983 char *p = SvPV(declarator, n);
984 char *q = memchr(p, ' ', n);
79bce2d4 985 mPUSHp(p, q ? (size_t)(q - p) : n);
53c979f0 986 }
987 if (!ps) {
988 if (SvTRUE(kws->shift)) {
989 PUSHs(kws->shift);
990 } else {
991 PUSHmortal;
992 }
51a483f8 993 PUSHmortal;
53c979f0 994 mPUSHs(newRV_noinc((SV *)newAV()));
995 mPUSHs(newRV_noinc((SV *)newAV()));
996 mPUSHs(newRV_noinc((SV *)newAV()));
997 mPUSHs(newRV_noinc((SV *)newAV()));
998 mPUSHp("@_", 2);
51a483f8 999 PUSHmortal;
53c979f0 1000 } else {
51a483f8 1001 /* 2, 3 */ {
53c979f0 1002 if (ps->invocant.name) {
1003 PUSHs(ps->invocant.name);
51a483f8 1004 if (ps->invocant.type) {
1005 PUSHs(ps->invocant.type);
1006 } else {
1007 PUSHmortal;
1008 }
53c979f0 1009 } else {
1010 PUSHmortal;
51a483f8 1011 PUSHmortal;
53c979f0 1012 }
1013 }
51a483f8 1014 /* 4 */ {
53c979f0 1015 size_t i, lim;
1016 AV *av;
1017
1018 lim = ps->positional_required.used;
1019
1020 av = newAV();
1021 if (lim) {
51a483f8 1022 av_extend(av, (lim - 1) * 2);
53c979f0 1023 for (i = 0; i < lim; i++) {
51a483f8 1024 Param *cur = &ps->positional_required.data[i];
1025 av_push(av, SvREFCNT_inc_simple_NN(cur->name));
1026 av_push(av, cur->type ? SvREFCNT_inc_simple_NN(cur->type) : &PL_sv_undef);
53c979f0 1027 }
1028 }
1029
1030 mPUSHs(newRV_noinc((SV *)av));
1031 }
51a483f8 1032 /* 5 */ {
53c979f0 1033 size_t i, lim;
1034 AV *av;
1035
1036 lim = ps->positional_optional.used;
1037
1038 av = newAV();
1039 if (lim) {
51a483f8 1040 av_extend(av, (lim - 1) * 2);
53c979f0 1041 for (i = 0; i < lim; i++) {
51a483f8 1042 Param *cur = &ps->positional_optional.data[i].param;
1043 av_push(av, SvREFCNT_inc_simple_NN(cur->name));
1044 av_push(av, cur->type ? SvREFCNT_inc_simple_NN(cur->type) : &PL_sv_undef);
53c979f0 1045 }
1046 }
1047
1048 mPUSHs(newRV_noinc((SV *)av));
1049 }
51a483f8 1050 /* 6 */ {
53c979f0 1051 size_t i, lim;
1052 AV *av;
1053
1054 lim = ps->named_required.used;
1055
1056 av = newAV();
1057 if (lim) {
51a483f8 1058 av_extend(av, (lim - 1) * 2);
53c979f0 1059 for (i = 0; i < lim; i++) {
51a483f8 1060 Param *cur = &ps->named_required.data[i];
1061 av_push(av, SvREFCNT_inc_simple_NN(cur->name));
1062 av_push(av, cur->type ? SvREFCNT_inc_simple_NN(cur->type) : &PL_sv_undef);
53c979f0 1063 }
1064 }
1065
1066 mPUSHs(newRV_noinc((SV *)av));
1067 }
51a483f8 1068 /* 7 */ {
53c979f0 1069 size_t i, lim;
1070 AV *av;
1071
1072 lim = ps->named_optional.used;
1073
1074 av = newAV();
1075 if (lim) {
51a483f8 1076 av_extend(av, (lim - 1) * 2);
53c979f0 1077 for (i = 0; i < lim; i++) {
51a483f8 1078 Param *cur = &ps->named_optional.data[i].param;
1079 av_push(av, SvREFCNT_inc_simple_NN(cur->name));
1080 av_push(av, cur->type ? SvREFCNT_inc_simple_NN(cur->type) : &PL_sv_undef);
53c979f0 1081 }
1082 }
1083
1084 mPUSHs(newRV_noinc((SV *)av));
1085 }
51a483f8 1086 /* 8, 9 */ {
53c979f0 1087 if (ps->slurpy.name) {
1088 PUSHs(ps->slurpy.name);
51a483f8 1089 if (ps->slurpy.type) {
1090 PUSHs(ps->slurpy.type);
1091 } else {
1092 PUSHmortal;
1093 }
53c979f0 1094 } else {
1095 PUSHmortal;
51a483f8 1096 PUSHmortal;
53c979f0 1097 }
1098 }
1099 }
1100 PUTBACK;
1101
1102 call_pv(MY_PKG "::_register_info", G_VOID);
1103
1104 FREETMPS;
1105 LEAVE;
1106}
1107
e4648f19 1108static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRLEN keyword_len, const KWSpec *spec) {
e158cf8f 1109 ParamSpec *param_spec;
c311cef3 1110 SV *declarator;
1111 I32 floor_ix;
63915d26 1112 int save_ix;
c311cef3 1113 SV *saw_name;
ed95eeff 1114 OpGuard *prelude_sentinel;
c311cef3 1115 SV *proto;
ed95eeff 1116 OpGuard *attrs_sentinel;
1117 OP *body;
c311cef3 1118 unsigned builtin_attrs;
db81d362 1119 I32 c;
1120
e4648f19 1121 declarator = sentinel_mortalize(sen, newSVpvn(keyword_ptr, keyword_len));
db81d362 1122
db81d362 1123 lex_read_space(0);
1124
c311cef3 1125 builtin_attrs = 0;
1126
db81d362 1127 /* function name */
c311cef3 1128 saw_name = NULL;
e4648f19 1129 if ((spec->flags & FLAG_NAME_OK) && (saw_name = my_scan_word(aTHX_ sen, TRUE))) {
c311cef3 1130
1131 if (PL_parser->expect != XSTATE) {
1132 /* bail out early so we don't predeclare $saw_name */
1133 croak("In %"SVf": I was expecting a function body, not \"%"SVf"\"", SVfARG(declarator), SVfARG(saw_name));
1134 }
1135
db81d362 1136 sv_catpvs(declarator, " ");
c311cef3 1137 sv_catsv(declarator, saw_name);
1138
1139 if (
1140 sv_eq_pvs(saw_name, "BEGIN") ||
1141 sv_eq_pvs(saw_name, "END") ||
1142 sv_eq_pvs(saw_name, "INIT") ||
1143 sv_eq_pvs(saw_name, "CHECK") ||
1144 sv_eq_pvs(saw_name, "UNITCHECK")
1145 ) {
1146 builtin_attrs |= MY_ATTR_SPECIAL;
1147 }
1148
db81d362 1149 lex_read_space(0);
63915d26 1150 } else if (!(spec->flags & FLAG_ANON_OK)) {
31534187 1151 croak("I was expecting a function name, not \"%.*s\"", (int)(PL_parser->bufend - PL_parser->bufptr), PL_parser->bufptr);
db81d362 1152 } else {
1153 sv_catpvs(declarator, " (anon)");
1154 }
1155
63915d26 1156 /* we're a subroutine declaration */
c311cef3 1157 floor_ix = start_subparse(FALSE, saw_name ? 0 : CVf_ANON);
1158 SAVEFREESV(PL_compcv);
1159
63915d26 1160 /* create outer block: '{' */
1161 save_ix = S_block_start(aTHX_ TRUE);
1162
1e0f1595 1163 /* initialize synthetic optree */
ed95eeff 1164 Newx(prelude_sentinel, 1, OpGuard);
1165 op_guard_init(prelude_sentinel);
1166 sentinel_register(sen, prelude_sentinel, free_op_guard_void);
1e0f1595 1167
db81d362 1168 /* parameters */
e158cf8f 1169 param_spec = NULL;
63915d26 1170
db81d362 1171 c = lex_peek_unichar(0);
1172 if (c == '(') {
ed95eeff 1173 OpGuard *init_sentinel;
e158cf8f 1174
ed95eeff 1175 Newx(init_sentinel, 1, OpGuard);
1176 op_guard_init(init_sentinel);
1177 sentinel_register(sen, init_sentinel, free_op_guard_void);
e158cf8f 1178
1179 Newx(param_spec, 1, ParamSpec);
1180 ps_init(param_spec);
e4648f19 1181 sentinel_register(sen, param_spec, ps_free_void);
db81d362 1182
1183 lex_read_unichar(0);
1184 lex_read_space(0);
1185
e158cf8f 1186 while ((c = lex_peek_unichar(0)) != ')') {
1187 int flags;
51a483f8 1188 SV *name, *type;
e158cf8f 1189 char sigil;
1190 PADOFFSET padoff;
c311cef3 1191
51a483f8 1192 padoff = parse_param(aTHX_ sen, declarator, spec, param_spec, &flags, &name, init_sentinel, &type);
c311cef3 1193
e158cf8f 1194 S_intro_my(aTHX);
63915d26 1195
e158cf8f 1196 sigil = SvPV_nolen(name)[0];
db81d362 1197
e158cf8f 1198 /* internal consistency */
1199 if (flags & PARAM_NAMED) {
1200 if (flags & PARAM_INVOCANT) {
1201 croak("In %"SVf": invocant %"SVf" can't be a named parameter", SVfARG(declarator), SVfARG(name));
db81d362 1202 }
e158cf8f 1203 if (sigil != '$') {
1204 croak("In %"SVf": named parameter %"SVf" can't be a%s", SVfARG(declarator), SVfARG(name), sigil == '@' ? "n array" : " hash");
db81d362 1205 }
e158cf8f 1206 } else if (flags & PARAM_INVOCANT) {
ed95eeff 1207 if (init_sentinel->op) {
e158cf8f 1208 croak("In %"SVf": invocant %"SVf" can't have a default value", SVfARG(declarator), SVfARG(name));
db81d362 1209 }
e158cf8f 1210 if (sigil != '$') {
1211 croak("In %"SVf": invocant %"SVf" can't be a%s", SVfARG(declarator), SVfARG(name), sigil == '@' ? "n array" : " hash");
1212 }
ed95eeff 1213 } else if (sigil != '$' && init_sentinel->op) {
e158cf8f 1214 croak("In %"SVf": %s %"SVf" can't have a default value", SVfARG(declarator), sigil == '@' ? "array" : "hash", SVfARG(name));
1215 }
db81d362 1216
e158cf8f 1217 /* external constraints */
1218 if (param_spec->slurpy.name) {
1219 croak("In %"SVf": I was expecting \")\" after \"%"SVf"\", not \"%"SVf"\"", SVfARG(declarator), SVfARG(param_spec->slurpy.name), SVfARG(name));
1220 }
1221 if (sigil != '$') {
ed95eeff 1222 assert(!init_sentinel->op);
e158cf8f 1223 param_spec->slurpy.name = name;
1224 param_spec->slurpy.padoff = padoff;
51a483f8 1225 param_spec->slurpy.type = type;
e158cf8f 1226 continue;
1227 }
d8e5d540 1228
e158cf8f 1229 if (!(flags & PARAM_NAMED) && count_named_params(param_spec)) {
1230 croak("In %"SVf": positional parameter %"SVf" can't appear after named parameter %"SVf"", SVfARG(declarator), SVfARG(name), SVfARG((param_spec->named_required.used ? param_spec->named_required.data[0] : param_spec->named_optional.data[0].param).name));
1231 }
d8e5d540 1232
e158cf8f 1233 if (flags & PARAM_INVOCANT) {
1234 if (param_spec->invocant.name) {
1235 croak("In %"SVf": invalid double invocants %"SVf", %"SVf"", SVfARG(declarator), SVfARG(param_spec->invocant.name), SVfARG(name));
1236 }
1237 if (count_positional_params(param_spec) || count_named_params(param_spec)) {
1238 croak("In %"SVf": invocant %"SVf" must be first in parameter list", SVfARG(declarator), SVfARG(name));
63915d26 1239 }
e158cf8f 1240 if (!(spec->flags & FLAG_INVOCANT)) {
1241 croak("In %"SVf": invocant %"SVf" not allowed here", SVfARG(declarator), SVfARG(name));
1242 }
1243 param_spec->invocant.name = name;
1244 param_spec->invocant.padoff = padoff;
51a483f8 1245 param_spec->invocant.type = type;
e158cf8f 1246 continue;
1247 }
63915d26 1248
ed95eeff 1249 if (init_sentinel->op && !(spec->flags & FLAG_DEFAULT_ARGS)) {
e158cf8f 1250 croak("In %"SVf": default argument for %"SVf" not allowed here", SVfARG(declarator), SVfARG(name));
1251 }
1e0f1595 1252
e158cf8f 1253 if (ps_contains(aTHX_ param_spec, name)) {
1254 croak("In %"SVf": %"SVf" can't appear twice in the same parameter list", SVfARG(declarator), SVfARG(name));
1255 }
1e0f1595 1256
e158cf8f 1257 if (flags & PARAM_NAMED) {
1258 if (!(spec->flags & FLAG_NAMED_PARAMS)) {
1259 croak("In %"SVf": named parameter :%"SVf" not allowed here", SVfARG(declarator), SVfARG(name));
1e0f1595 1260 }
1261
ed95eeff 1262 if (init_sentinel->op) {
e158cf8f 1263 ParamInit *pi = piv_extend(&param_spec->named_optional);
1264 pi->param.name = name;
1265 pi->param.padoff = padoff;
51a483f8 1266 pi->param.type = type;
ed95eeff 1267 pi->init = op_guard_transfer(init_sentinel);
e158cf8f 1268 param_spec->named_optional.used++;
1269 } else {
67fc1ddb 1270 Param *p;
1271
e158cf8f 1272 if (param_spec->positional_optional.used) {
1273 croak("In %"SVf": can't combine optional positional (%"SVf") and required named (%"SVf") parameters", SVfARG(declarator), SVfARG(param_spec->positional_optional.data[0].param.name), SVfARG(name));
1274 }
d8e5d540 1275
67fc1ddb 1276 p = pv_extend(&param_spec->named_required);
e158cf8f 1277 p->name = name;
1278 p->padoff = padoff;
51a483f8 1279 p->type = type;
e158cf8f 1280 param_spec->named_required.used++;
1281 }
1282 } else {
ed95eeff 1283 if (init_sentinel->op || param_spec->positional_optional.used) {
e158cf8f 1284 ParamInit *pi = piv_extend(&param_spec->positional_optional);
1285 pi->param.name = name;
1286 pi->param.padoff = padoff;
51a483f8 1287 pi->param.type = type;
ed95eeff 1288 pi->init = op_guard_transfer(init_sentinel);
e158cf8f 1289 param_spec->positional_optional.used++;
1290 } else {
1291 Param *p = pv_extend(&param_spec->positional_required);
1292 p->name = name;
1293 p->padoff = padoff;
51a483f8 1294 p->type = type;
e158cf8f 1295 param_spec->positional_required.used++;
db81d362 1296 }
1297 }
1298
e158cf8f 1299 }
1300 lex_read_unichar(0);
1301 lex_read_space(0);
db81d362 1302
e158cf8f 1303 if (!param_spec->invocant.name && SvTRUE(spec->shift)) {
1304 if (ps_contains(aTHX_ param_spec, spec->shift)) {
1305 croak("In %"SVf": %"SVf" can't appear twice in the same parameter list", SVfARG(declarator), SVfARG(spec->shift));
db81d362 1306 }
e158cf8f 1307
1308 param_spec->invocant.name = spec->shift;
1309 param_spec->invocant.padoff = pad_add_name_sv(param_spec->invocant.name, 0, NULL, NULL);
db81d362 1310 }
1311 }
1312
1313 /* prototype */
c311cef3 1314 proto = NULL;
db81d362 1315 c = lex_peek_unichar(0);
1316 if (c == ':') {
1317 lex_read_unichar(0);
1318 lex_read_space(0);
1319
1320 c = lex_peek_unichar(0);
1321 if (c != '(') {
c311cef3 1322 lex_stuff_pvs(":", 0);
1323 c = ':';
db81d362 1324 } else {
31534187 1325 lex_read_unichar(0);
e4648f19 1326 if (!(proto = my_scan_parens_tail(aTHX_ sen, FALSE))) {
f34187b8 1327 croak("In %"SVf": prototype not terminated", SVfARG(declarator));
db81d362 1328 }
e4648f19 1329 my_check_prototype(aTHX_ sen, declarator, proto);
db81d362 1330 lex_read_space(0);
c311cef3 1331 c = lex_peek_unichar(0);
514bcaa6 1332 if (!(c == ':' || c == '{')) {
1333 lex_stuff_pvs(":", 0);
1334 c = ':';
1335 }
db81d362 1336 }
1337 }
1338
db81d362 1339 /* attributes */
ed95eeff 1340 Newx(attrs_sentinel, 1, OpGuard);
1341 op_guard_init(attrs_sentinel);
1342 sentinel_register(sen, attrs_sentinel, free_op_guard_void);
c311cef3 1343
63915d26 1344 if (c == ':' || c == '{') /* '}' - hi, vim */ {
c311cef3 1345
1346 /* kludge default attributes in */
1347 if (SvTRUE(spec->attrs) && SvPV_nolen(spec->attrs)[0] == ':') {
1348 lex_stuff_sv(spec->attrs, 0);
1349 c = ':';
1350 }
b72eb6ee 1351
db81d362 1352 if (c == ':') {
db81d362 1353 lex_read_unichar(0);
1354 lex_read_space(0);
db81d362 1355 c = lex_peek_unichar(0);
c311cef3 1356
1357 for (;;) {
1358 SV *attr;
1359
e4648f19 1360 if (!(attr = my_scan_word(aTHX_ sen, FALSE))) {
c311cef3 1361 break;
db81d362 1362 }
c311cef3 1363
db81d362 1364 lex_read_space(0);
1365 c = lex_peek_unichar(0);
c311cef3 1366
1367 if (c != '(') {
1368 if (sv_eq_pvs(attr, "lvalue")) {
1369 builtin_attrs |= MY_ATTR_LVALUE;
1370 attr = NULL;
1371 } else if (sv_eq_pvs(attr, "method")) {
1372 builtin_attrs |= MY_ATTR_METHOD;
1373 attr = NULL;
1374 }
1375 } else {
31534187 1376 SV *sv;
1377 lex_read_unichar(0);
e4648f19 1378 if (!(sv = my_scan_parens_tail(aTHX_ sen, TRUE))) {
c311cef3 1379 croak("In %"SVf": unterminated attribute parameter in attribute list", SVfARG(declarator));
1380 }
31534187 1381 sv_catpvs(attr, "(");
c311cef3 1382 sv_catsv(attr, sv);
31534187 1383 sv_catpvs(attr, ")");
c311cef3 1384
1385 lex_read_space(0);
1386 c = lex_peek_unichar(0);
1387 }
1388
1389 if (attr) {
ed95eeff 1390 op_guard_update(attrs_sentinel, op_append_elem(OP_LIST, attrs_sentinel->op, mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(attr))));
c311cef3 1391 }
1392
1393 if (c == ':') {
1394 lex_read_unichar(0);
1395 lex_read_space(0);
1396 c = lex_peek_unichar(0);
1397 }
db81d362 1398 }
1399 }
1400 }
1401
1402 /* body */
63915d26 1403 if (c != '{') /* '}' - hi, vim */ {
85bc3fbd 1404 croak("In %"SVf": I was expecting a function body, not \"%c\"", SVfARG(declarator), (int)c);
db81d362 1405 }
c311cef3 1406
63915d26 1407 /* surprise predeclaration! */
1408 if (saw_name) {
1409 /* 'sub NAME (PROTO);' to make name/proto known to perl before it
1410 starts parsing the body */
1411 const I32 sub_ix = start_subparse(FALSE, 0);
1412 SAVEFREESV(PL_compcv);
1413
1414 SvREFCNT_inc_simple_void(PL_compcv);
1415
1416 newATTRSUB(
1417 sub_ix,
fc634bba 1418 mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(saw_name)),
1419 proto ? mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(proto)) : NULL,
63915d26 1420 NULL,
1421 NULL
1422 );
1423 }
1424
c311cef3 1425 if (builtin_attrs & MY_ATTR_LVALUE) {
1426 CvLVALUE_on(PL_compcv);
db81d362 1427 }
c311cef3 1428 if (builtin_attrs & MY_ATTR_METHOD) {
1429 CvMETHOD_on(PL_compcv);
1430 }
1431 if (builtin_attrs & MY_ATTR_SPECIAL) {
1432 CvSPECIAL_on(PL_compcv);
db81d362 1433 }
1434
e158cf8f 1435 /* check number of arguments */
1e0f1595 1436 if (spec->flags & FLAG_CHECK_NARGS) {
e158cf8f 1437 int amin, amax;
abccbe86 1438
e158cf8f 1439 amin = args_min(aTHX_ param_spec, spec);
1440 if (amin > 0) {
51a483f8 1441 OP *chk, *cond, *err, *xcroak;
63915d26 1442
fc634bba 1443 err = mkconstsv(aTHX_ newSVpvf("Not enough arguments for %"SVf" (expected %d, got ", SVfARG(declarator), amin));
1444 err = newBINOP(
1445 OP_CONCAT, 0,
1446 err,
1447 newAVREF(newGVOP(OP_GV, 0, PL_defgv))
1448 );
1449 err = newBINOP(
1450 OP_CONCAT, 0,
1451 err,
1452 mkconstpvs(")")
1453 );
63915d26 1454
51a483f8 1455 xcroak = newCVREF(OPf_WANT_SCALAR,
1456 newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV)));
1e0f1595 1457 err = newUNOP(OP_ENTERSUB, OPf_STACKED,
51a483f8 1458 op_append_elem(OP_LIST, err, xcroak));
63915d26 1459
1e0f1595 1460 cond = newBINOP(OP_LT, 0,
5f50e017 1461 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
fc634bba 1462 mkconstiv(aTHX_ amin));
1e0f1595 1463 chk = newLOGOP(OP_AND, 0, cond, err);
63915d26 1464
ed95eeff 1465 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, chk)));
1e0f1595 1466 }
abccbe86 1467
e158cf8f 1468 amax = args_max(param_spec);
1469 if (amax >= 0) {
51a483f8 1470 OP *chk, *cond, *err, *xcroak;
63915d26 1471
fc634bba 1472 err = mkconstsv(aTHX_ newSVpvf("Too many arguments for %"SVf" (expected %d, got ", SVfARG(declarator), amax));
1473 err = newBINOP(
1474 OP_CONCAT, 0,
1475 err,
1476 newAVREF(newGVOP(OP_GV, 0, PL_defgv))
1477 );
1478 err = newBINOP(
1479 OP_CONCAT, 0,
1480 err,
1481 mkconstpvs(")")
1482 );
63915d26 1483
51a483f8 1484 xcroak = newCVREF(
fc634bba 1485 OPf_WANT_SCALAR,
1486 newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
1487 );
1e0f1595 1488 err = newUNOP(OP_ENTERSUB, OPf_STACKED,
51a483f8 1489 op_append_elem(OP_LIST, err, xcroak));
63915d26 1490
fc634bba 1491 cond = newBINOP(
1492 OP_GT, 0,
1493 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
1494 mkconstiv(aTHX_ amax)
1495 );
1e0f1595 1496 chk = newLOGOP(OP_AND, 0, cond, err);
63915d26 1497
ed95eeff 1498 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, chk)));
1e0f1595 1499 }
e158cf8f 1500
1501 if (param_spec && (count_named_params(param_spec) || (param_spec->slurpy.name && SvPV_nolen(param_spec->slurpy.name)[0] == '%'))) {
51a483f8 1502 OP *chk, *cond, *err, *xcroak;
e158cf8f 1503 const UV fixed = count_positional_params(param_spec) + !!param_spec->invocant.name;
63915d26 1504
fc634bba 1505 err = mkconstsv(aTHX_ newSVpvf("Odd number of paired arguments for %"SVf"", SVfARG(declarator)));
63915d26 1506
51a483f8 1507 xcroak = newCVREF(
fc634bba 1508 OPf_WANT_SCALAR,
1509 newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
1510 );
1e0f1595 1511 err = newUNOP(OP_ENTERSUB, OPf_STACKED,
51a483f8 1512 op_append_elem(OP_LIST, err, xcroak));
63915d26 1513
1e0f1595 1514 cond = newBINOP(OP_GT, 0,
5f50e017 1515 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
fc634bba 1516 mkconstiv(aTHX_ fixed));
e158cf8f 1517 cond = newLOGOP(OP_AND, 0,
fc634bba 1518 cond,
1519 newBINOP(OP_MODULO, 0,
1520 fixed
1521 ? newBINOP(OP_SUBTRACT, 0,
1522 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
1523 mkconstiv(aTHX_ fixed))
1524 : newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
1525 mkconstiv(aTHX_ 2)));
1e0f1595 1526 chk = newLOGOP(OP_AND, 0, cond, err);
63915d26 1527
ed95eeff 1528 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, chk)));
c311cef3 1529 }
1e0f1595 1530 }
c311cef3 1531
e158cf8f 1532 if (!param_spec) {
1533 /* my $invocant = shift; */
1534 if (SvTRUE(spec->shift)) {
1535 OP *var;
abccbe86 1536
e158cf8f 1537 var = my_var(
1538 aTHX_
1539 OPf_MOD | (OPpLVAL_INTRO << 8),
1540 pad_add_name_sv(spec->shift, 0, NULL, NULL)
1541 );
1542 var = newASSIGNOP(OPf_STACKED, var, 0, newOP(OP_SHIFT, 0));
c311cef3 1543
ed95eeff 1544 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
e158cf8f 1545 }
1546 } else {
1547 /* my $invocant = shift; */
1548 if (param_spec->invocant.name) {
1549 OP *var;
c311cef3 1550
e158cf8f 1551 var = my_var(
1552 aTHX_
1553 OPf_MOD | (OPpLVAL_INTRO << 8),
1554 param_spec->invocant.padoff
1555 );
1556 var = newASSIGNOP(OPf_STACKED, var, 0, newOP(OP_SHIFT, 0));
1e0f1595 1557
ed95eeff 1558 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
51a483f8 1559
1560 if (param_spec->invocant.type && (spec->flags & FLAG_CHECK_TARGS)) {
ed95eeff 1561 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, 0, &param_spec->invocant))));
51a483f8 1562 }
c311cef3 1563 }
1564
e158cf8f 1565 /* my (...) = @_; */
1566 {
1567 OP *lhs;
1568 size_t i, lim;
63915d26 1569
e158cf8f 1570 lhs = NULL;
63915d26 1571
e158cf8f 1572 for (i = 0, lim = param_spec->positional_required.used; i < lim; i++) {
1573 OP *const var = my_var(
1574 aTHX_
1575 OPf_WANT_LIST | (OPpLVAL_INTRO << 8),
1576 param_spec->positional_required.data[i].padoff
1577 );
1578 lhs = op_append_elem(OP_LIST, lhs, var);
1579 }
63915d26 1580
e158cf8f 1581 for (i = 0, lim = param_spec->positional_optional.used; i < lim; i++) {
1582 OP *const var = my_var(
1583 aTHX_
1584 OPf_WANT_LIST | (OPpLVAL_INTRO << 8),
1585 param_spec->positional_optional.data[i].param.padoff
1586 );
1587 lhs = op_append_elem(OP_LIST, lhs, var);
1588 }
63915d26 1589
fc634bba 1590 {
1591 PADOFFSET padoff;
1592 I32 type;
1593 bool slurpy_hash;
1594
1595 /*
1596 * cases:
1597 * 1) no named params
1598 * 1.1) slurpy
1599 * => put it in
1600 * 1.2) no slurpy
1601 * => nop
1602 * 2) named params
1603 * 2.1) no slurpy
1604 * => synthetic %{rest}
1605 * 2.2) slurpy is a hash
1606 * => put it in
1607 * 2.3) slurpy is an array
1608 * => synthetic %{rest}
1609 * remember to declare array later
1610 */
1611
1612 slurpy_hash = param_spec->slurpy.name && SvPV_nolen(param_spec->slurpy.name)[0] == '%';
1613 if (!count_named_params(param_spec)) {
1614 if (param_spec->slurpy.name) {
1615 padoff = param_spec->slurpy.padoff;
1616 type = slurpy_hash ? OP_PADHV : OP_PADAV;
1617 } else {
1618 padoff = NOT_IN_PAD;
1619 type = OP_PADSV;
1620 }
1621 } else if (slurpy_hash) {
1622 padoff = param_spec->slurpy.padoff;
1623 type = OP_PADHV;
e158cf8f 1624 } else {
fc634bba 1625 padoff = param_spec->rest_hash = pad_add_name_pvs("%{rest}", 0, NULL, NULL);
1626 type = OP_PADHV;
1627 }
1628
1629 if (padoff != NOT_IN_PAD) {
e158cf8f 1630 OP *const var = my_var_g(
1631 aTHX_
fc634bba 1632 type,
e158cf8f 1633 OPf_WANT_LIST | (OPpLVAL_INTRO << 8),
fc634bba 1634 padoff
e158cf8f 1635 );
fc634bba 1636
e158cf8f 1637 lhs = op_append_elem(OP_LIST, lhs, var);
fc634bba 1638
1639 if (type == OP_PADHV) {
1640 param_spec->rest_hash = padoff;
1641 }
e158cf8f 1642 }
1643 }
63915d26 1644
e158cf8f 1645 if (lhs) {
1646 OP *rhs;
1647 lhs->op_flags |= OPf_PARENS;
1648 rhs = newAVREF(newGVOP(OP_GV, 0, PL_defgv));
1649
ed95eeff 1650 op_guard_update(prelude_sentinel, op_append_list(
1651 OP_LINESEQ, prelude_sentinel->op,
e158cf8f 1652 newSTATEOP(
1653 0, NULL,
1654 newASSIGNOP(OPf_STACKED, lhs, 0, rhs)
1655 )
ed95eeff 1656 ));
e158cf8f 1657 }
1658 }
63915d26 1659
fc634bba 1660 /* default positional arguments */
e158cf8f 1661 {
1662 size_t i, lim, req;
1663 OP *nest;
63915d26 1664
e158cf8f 1665 nest = NULL;
63915d26 1666
e158cf8f 1667 req = param_spec->positional_required.used;
1668 for (i = 0, lim = param_spec->positional_optional.used; i < lim; i++) {
1669 ParamInit *cur = &param_spec->positional_optional.data[i];
1670 OP *var, *cond;
63915d26 1671
e158cf8f 1672 cond = newBINOP(
1673 OP_LT, 0,
1674 newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
fc634bba 1675 mkconstiv(aTHX_ req + i + 1)
e158cf8f 1676 );
63915d26 1677
e158cf8f 1678 var = my_var(aTHX_ 0, cur->param.padoff);
63915d26 1679
e158cf8f 1680 nest = op_append_list(
1681 OP_LINESEQ, nest,
ed95eeff 1682 newASSIGNOP(OPf_STACKED, var, 0, op_guard_relinquish(&cur->init))
e158cf8f 1683 );
e158cf8f 1684 nest = newCONDOP(
1685 0,
1686 cond,
1687 nest,
1688 NULL
1689 );
1690 }
1e0f1595 1691
ed95eeff 1692 op_guard_update(prelude_sentinel, op_append_list(
1693 OP_LINESEQ, prelude_sentinel->op,
e158cf8f 1694 nest
ed95eeff 1695 ));
c311cef3 1696 }
1697
e158cf8f 1698 /* named parameters */
1699 if (count_named_params(param_spec)) {
fc634bba 1700 size_t i, lim;
1701
1702 assert(param_spec->rest_hash != NOT_IN_PAD);
1703
1704 for (i = 0, lim = param_spec->named_required.used; i < lim; i++) {
1705 Param *cur = &param_spec->named_required.data[i];
1706 size_t n;
1707 char *p = SvPV(cur->name, n);
1708 OP *var, *cond;
1709
1710 cond = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
1711
1712 if (spec->flags & FLAG_CHECK_NARGS) {
51a483f8 1713 OP *xcroak, *msg;
fc634bba 1714
1715 var = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
1716 var = newUNOP(OP_DELETE, 0, var);
1717
1718 msg = mkconstsv(aTHX_ newSVpvf("In %"SVf": missing named parameter: %.*s", SVfARG(declarator), (int)(n - 1), p + 1));
51a483f8 1719 xcroak = newCVREF(
fc634bba 1720 OPf_WANT_SCALAR,
1721 newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
1722 );
51a483f8 1723 xcroak = newUNOP(OP_ENTERSUB, OPf_STACKED, op_append_elem(OP_LIST, msg, xcroak));
fc634bba 1724
1725 cond = newUNOP(OP_EXISTS, 0, cond);
1726
51a483f8 1727 cond = newCONDOP(0, cond, var, xcroak);
fc634bba 1728 }
1729
1730 var = my_var(
1731 aTHX_
1732 OPf_MOD | (OPpLVAL_INTRO << 8),
1733 cur->padoff
1734 );
1735 var = newASSIGNOP(OPf_STACKED, var, 0, cond);
1736
ed95eeff 1737 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
fc634bba 1738 }
1739
1740 for (i = 0, lim = param_spec->named_optional.used; i < lim; i++) {
1741 ParamInit *cur = &param_spec->named_optional.data[i];
1742 size_t n;
1743 char *p = SvPV(cur->param.name, n);
1744 OP *var, *cond;
1745
1746 var = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
1747 var = newUNOP(OP_DELETE, 0, var);
1748
1749 cond = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
1750 cond = newUNOP(OP_EXISTS, 0, cond);
1751
ed95eeff 1752 cond = newCONDOP(0, cond, var, op_guard_relinquish(&cur->init));
fc634bba 1753
1754 var = my_var(
1755 aTHX_
1756 OPf_MOD | (OPpLVAL_INTRO << 8),
1757 cur->param.padoff
1758 );
1759 var = newASSIGNOP(OPf_STACKED, var, 0, cond);
1760
ed95eeff 1761 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
fc634bba 1762 }
1763
1764 if (!param_spec->slurpy.name) {
1765 if (spec->flags & FLAG_CHECK_NARGS) {
1766 /* croak if %{rest} */
51a483f8 1767 OP *xcroak, *cond, *keys, *msg;
fc634bba 1768
1769 keys = newUNOP(OP_KEYS, 0, my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash));
1770 keys = newLISTOP(OP_SORT, 0, newOP(OP_PUSHMARK, 0), keys);
1771 {
1772 OP *first, *mid, *last;
1773
1774 last = keys;
1775
1776 mid = mkconstpvs(", ");
1777 mid->op_sibling = last;
1778
1779 first = newOP(OP_PUSHMARK, 0);
1780
1781 keys = newLISTOP(OP_JOIN, 0, first, mid);
1782 keys->op_targ = pad_alloc(OP_JOIN, SVs_PADTMP);
1783 ((LISTOP *)keys)->op_last = last;
1784 }
1785
1786 msg = mkconstsv(aTHX_ newSVpvf("In %"SVf": no such named parameter: ", SVfARG(declarator)));
1787 msg = newBINOP(OP_CONCAT, 0, msg, keys);
1788
51a483f8 1789 xcroak = newCVREF(
fc634bba 1790 OPf_WANT_SCALAR,
1791 newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
1792 );
51a483f8 1793 xcroak = newUNOP(OP_ENTERSUB, OPf_STACKED, op_append_elem(OP_LIST, msg, xcroak));
fc634bba 1794
1795 cond = newUNOP(OP_KEYS, 0, my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash));
51a483f8 1796 xcroak = newCONDOP(0, cond, xcroak, NULL);
fc634bba 1797
ed95eeff 1798 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, xcroak)));
fc634bba 1799 } else {
1800 OP *clear;
1801
1802 clear = newASSIGNOP(
1803 OPf_STACKED,
1804 my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash),
1805 0,
1806 newNULLLIST()
1807 );
1808
ed95eeff 1809 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, clear)));
fc634bba 1810 }
1811 } else if (param_spec->slurpy.padoff != param_spec->rest_hash) {
1812 OP *var, *clear;
1813
1814 assert(SvPV_nolen(param_spec->slurpy.name)[0] == '@');
1815
1816 var = my_var_g(
1817 aTHX_
1818 OP_PADAV,
1819 OPf_MOD | (OPpLVAL_INTRO << 8),
1820 param_spec->slurpy.padoff
1821 );
1822
1823 var = newASSIGNOP(OPf_STACKED, var, 0, my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash));
1824
ed95eeff 1825 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
fc634bba 1826
1827 clear = newASSIGNOP(
1828 OPf_STACKED,
1829 my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash),
1830 0,
1831 newNULLLIST()
1832 );
1833
ed95eeff 1834 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, clear)));
fc634bba 1835 }
1836 }
51a483f8 1837
1838 if (spec->flags & FLAG_CHECK_TARGS) {
1839 size_t i, lim, base;
1840
1841 base = 1;
1842 for (i = 0, lim = param_spec->positional_required.used; i < lim; i++) {
1843 Param *cur = &param_spec->positional_required.data[i];
1844
1845 if (cur->type) {
ed95eeff 1846 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
51a483f8 1847 }
1848 }
1849 base += i;
1850
1851 for (i = 0, lim = param_spec->positional_optional.used; i < lim; i++) {
1852 Param *cur = &param_spec->positional_optional.data[i].param;
1853
1854 if (cur->type) {
ed95eeff 1855 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
51a483f8 1856 }
1857 }
1858 base += i;
1859
1860 for (i = 0, lim = param_spec->named_required.used; i < lim; i++) {
1861 Param *cur = &param_spec->named_required.data[i];
1862
1863 if (cur->type) {
ed95eeff 1864 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
51a483f8 1865 }
1866 }
1867 base += i;
1868
1869 for (i = 0, lim = param_spec->named_optional.used; i < lim; i++) {
1870 Param *cur = &param_spec->named_optional.data[i].param;
1871
1872 if (cur->type) {
ed95eeff 1873 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
51a483f8 1874 }
1875 }
1876 base += i;
1877
1878 if (param_spec->slurpy.type) {
1879 /* $type->valid($_) or croak $type->get_message($_) for @rest / values %rest */
a8492ba4 1880 OP *check, *list, *loop;
51a483f8 1881
a8492ba4 1882 check = mktypecheck(aTHX_ declarator, base, param_spec->slurpy.name, NOT_IN_PAD, param_spec->slurpy.type);
51a483f8 1883
1884 if (SvPV_nolen(param_spec->slurpy.name)[0] == '@') {
1885 list = my_var_g(aTHX_ OP_PADAV, 0, param_spec->slurpy.padoff);
1886 } else {
1887 list = my_var_g(aTHX_ OP_PADHV, 0, param_spec->slurpy.padoff);
1888 list = newUNOP(OP_VALUES, 0, list);
1889 }
1890
a8492ba4 1891 loop = newFOROP(0, NULL, list, check, NULL);
51a483f8 1892
ed95eeff 1893 op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, loop)));
51a483f8 1894 }
1895 }
1e0f1595 1896 }
c311cef3 1897
1e0f1595 1898 /* finally let perl parse the actual subroutine body */
1899 body = parse_block(0);
c311cef3 1900
1e0f1595 1901 /* add '();' to make function return nothing by default */
1902 /* (otherwise the invisible parameter initialization can "leak" into
1903 the return value: fun ($x) {}->("asdf", 0) == 2) */
ed95eeff 1904 if (prelude_sentinel->op) {
1e0f1595 1905 body = newSTATEOP(0, NULL, body);
db81d362 1906 }
1907
ed95eeff 1908 body = op_append_list(OP_LINESEQ, op_guard_relinquish(prelude_sentinel), body);
1e0f1595 1909
c311cef3 1910 /* it's go time. */
1911 {
53c979f0 1912 CV *cv;
ed95eeff 1913 OP *const attrs = op_guard_relinquish(attrs_sentinel);
53c979f0 1914
c311cef3 1915 SvREFCNT_inc_simple_void(PL_compcv);
1916
63915d26 1917 /* close outer block: '}' */
1918 S_block_end(aTHX_ save_ix, body);
1919
53c979f0 1920 cv = newATTRSUB(
c311cef3 1921 floor_ix,
53c979f0 1922 saw_name ? newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(saw_name)) : NULL,
c311cef3 1923 proto ? newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(proto)) : NULL,
1924 attrs,
1925 body
1926 );
53c979f0 1927
2c1cb7bd 1928 if (cv) {
1929 register_info(aTHX_ PTR2UV(CvROOT(cv)), declarator, spec, param_spec);
1930 }
53c979f0 1931
1932 if (saw_name) {
1933 *pop = newOP(OP_NULL, 0);
1934 return KEYWORD_PLUGIN_STMT;
1935 }
1936
1937 *pop = newUNOP(
1938 OP_REFGEN, 0,
1939 newSVOP(
1940 OP_ANONCODE, 0,
1941 (SV *)cv
1942 )
1943 );
1944 return KEYWORD_PLUGIN_EXPR;
db81d362 1945 }
db81d362 1946}
1947
8216fac4 1948static int kw_flags_enter(pTHX_ Sentinel sen, const char *kw_ptr, STRLEN kw_len, KWSpec *spec) {
1949 HV *hints;
1950 SV *sv, **psv;
1951 const char *p, *kw_active;
1952 STRLEN kw_active_len;
1953
1954 if (!(hints = GvHV(PL_hintgv))) {
1955 return FALSE;
1956 }
1957 if (!(psv = hv_fetchs(hints, HINTK_KEYWORDS, 0))) {
1958 return FALSE;
1959 }
1960 sv = *psv;
1961 kw_active = SvPV(sv, kw_active_len);
1962 if (kw_active_len <= kw_len) {
1963 return FALSE;
1964 }
1965 for (
1966 p = kw_active;
1967 (p = strchr(p, *kw_ptr)) &&
1968 p < kw_active + kw_active_len - kw_len;
1969 p++
1970 ) {
1971 if (
1972 (p == kw_active || p[-1] == ' ') &&
1973 p[kw_len] == ' ' &&
1974 memcmp(kw_ptr, p, kw_len) == 0
1975 ) {
1976 ENTER;
1977 SAVETMPS;
1978
1979 SAVEDESTRUCTOR_X(sentinel_clear_void, sen);
1980
1981 spec->flags = 0;
1982 spec->shift = sentinel_mortalize(sen, newSVpvs(""));
1983 spec->attrs = sentinel_mortalize(sen, newSVpvs(""));
1984
1985#define FETCH_HINTK_INTO(NAME, PTR, LEN, X) STMT_START { \
1986 const char *fk_ptr_; \
1987 STRLEN fk_len_; \
1988 SV *fk_sv_; \
1989 fk_sv_ = sentinel_mortalize(sen, newSVpvs(HINTK_ ## NAME)); \
1990 sv_catpvn(fk_sv_, PTR, LEN); \
1991 fk_ptr_ = SvPV(fk_sv_, fk_len_); \
1992 if (!((X) = hv_fetch(hints, fk_ptr_, fk_len_, 0))) { \
1993 croak("%s: internal error: $^H{'%.*s'} not set", MY_PKG, (int)fk_len_, fk_ptr_); \
1994 } \
1995} STMT_END
1996
1997 FETCH_HINTK_INTO(FLAGS_, kw_ptr, kw_len, psv);
1998 spec->flags = SvIV(*psv);
1999
2000 FETCH_HINTK_INTO(SHIFT_, kw_ptr, kw_len, psv);
2001 SvSetSV(spec->shift, *psv);
2002
2003 FETCH_HINTK_INTO(ATTRS_, kw_ptr, kw_len, psv);
2004 SvSetSV(spec->attrs, *psv);
2005
2006#undef FETCH_HINTK_INTO
2007 return TRUE;
2008 }
2009 }
2010 return FALSE;
2011}
2012
db81d362 2013static int my_keyword_plugin(pTHX_ char *keyword_ptr, STRLEN keyword_len, OP **op_ptr) {
8216fac4 2014 Sentinel sen = { NULL };
63915d26 2015 KWSpec spec;
db81d362 2016 int ret;
2017
8216fac4 2018 if (kw_flags_enter(aTHX_ sen, keyword_ptr, keyword_len, &spec)) {
2019 /* scope was entered, 'sen' and 'spec' are initialized */
e4648f19 2020 ret = parse_fun(aTHX_ sen, op_ptr, keyword_ptr, keyword_len, &spec);
8216fac4 2021 FREETMPS;
2022 LEAVE;
db81d362 2023 } else {
8216fac4 2024 /* not one of our keywords, no allocation done */
7dd35535 2025 ret = next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
db81d362 2026 }
2027
db81d362 2028 return ret;
2029}
2030
2031WARNINGS_RESET
2032
53c979f0 2033MODULE = Function::Parameters PACKAGE = Function::Parameters PREFIX = fp_
db81d362 2034PROTOTYPES: ENABLE
2035
53c979f0 2036UV
2037fp__cv_root(sv)
2038 SV * sv
2039 PREINIT:
79bce2d4 2040 CV *xcv;
53c979f0 2041 HV *hv;
2042 GV *gv;
2043 CODE:
79bce2d4 2044 xcv = sv_2cv(sv, &hv, &gv, 0);
2045 RETVAL = PTR2UV(xcv ? CvROOT(xcv) : NULL);
53c979f0 2046 OUTPUT:
2047 RETVAL
2048
db81d362 2049BOOT:
2050WARNINGS_ENABLE {
2051 HV *const stash = gv_stashpvs(MY_PKG, GV_ADD);
426a4d69 2052 /**/
d8e5d540 2053 newCONSTSUB(stash, "FLAG_NAME_OK", newSViv(FLAG_NAME_OK));
2054 newCONSTSUB(stash, "FLAG_ANON_OK", newSViv(FLAG_ANON_OK));
63915d26 2055 newCONSTSUB(stash, "FLAG_DEFAULT_ARGS", newSViv(FLAG_DEFAULT_ARGS));
d8e5d540 2056 newCONSTSUB(stash, "FLAG_CHECK_NARGS", newSViv(FLAG_CHECK_NARGS));
2057 newCONSTSUB(stash, "FLAG_INVOCANT", newSViv(FLAG_INVOCANT));
e158cf8f 2058 newCONSTSUB(stash, "FLAG_NAMED_PARAMS", newSViv(FLAG_NAMED_PARAMS));
51a483f8 2059 newCONSTSUB(stash, "FLAG_TYPES_OK", newSViv(FLAG_TYPES_OK));
2060 newCONSTSUB(stash, "FLAG_CHECK_TARGS", newSViv(FLAG_CHECK_TARGS));
db81d362 2061 newCONSTSUB(stash, "HINTK_KEYWORDS", newSVpvs(HINTK_KEYWORDS));
d8e5d540 2062 newCONSTSUB(stash, "HINTK_FLAGS_", newSVpvs(HINTK_FLAGS_));
2063 newCONSTSUB(stash, "HINTK_SHIFT_", newSVpvs(HINTK_SHIFT_));
2064 newCONSTSUB(stash, "HINTK_ATTRS_", newSVpvs(HINTK_ATTRS_));
426a4d69 2065 /**/
db81d362 2066 next_keyword_plugin = PL_keyword_plugin;
2067 PL_keyword_plugin = my_keyword_plugin;
2068} WARNINGS_RESET