-
Notifications
You must be signed in to change notification settings - Fork 19
/
Parameters.xs
2769 lines (2309 loc) · 85.6 KB
/
Parameters.xs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright 2012, 2014, 2023 Lukas Mai.
This program is free software; you can redistribute it and/or modify it
under the terms of either: the GNU General Public License as published
by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.
*/
#ifdef __GNUC__
#if __GNUC__ >= 5
#define IF_HAVE_GCC_5(X) X
#endif
#if (__GNUC__ == 4 && __GNUC_MINOR__ >= 6) || __GNUC__ >= 5
#define PRAGMA_GCC_(X) _Pragma(#X)
#define PRAGMA_GCC(X) PRAGMA_GCC_(GCC X)
#endif
#endif
#ifndef IF_HAVE_GCC_5
#define IF_HAVE_GCC_5(X)
#endif
#ifndef PRAGMA_GCC
#define PRAGMA_GCC(X)
#endif
#ifdef DEVEL
#define WARNINGS_RESET PRAGMA_GCC(diagnostic pop)
#define WARNINGS_ENABLEW(X) PRAGMA_GCC(diagnostic error #X)
#define WARNINGS_ENABLE \
WARNINGS_ENABLEW(-Wall) \
WARNINGS_ENABLEW(-Wextra) \
WARNINGS_ENABLEW(-Wundef) \
WARNINGS_ENABLEW(-Wshadow) \
WARNINGS_ENABLEW(-Wbad-function-cast) \
WARNINGS_ENABLEW(-Wcast-align) \
WARNINGS_ENABLEW(-Wwrite-strings) \
WARNINGS_ENABLEW(-Wstrict-prototypes) \
WARNINGS_ENABLEW(-Wmissing-prototypes) \
WARNINGS_ENABLEW(-Winline) \
WARNINGS_ENABLEW(-Wdisabled-optimization) \
IF_HAVE_GCC_5(WARNINGS_ENABLEW(-Wnested-externs))
#else
#define WARNINGS_RESET
#define WARNINGS_ENABLE
#endif
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include <string.h>
#ifdef DEVEL
#undef NDEBUG
#include <assert.h>
#endif
#ifdef PERL_MAD
#error "MADness is not supported."
#endif
#define HAVE_PERL_VERSION(R, V, S) \
(PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
#if HAVE_PERL_VERSION(5, 19, 3)
#define IF_HAVE_PERL_5_19_3(YES, NO) YES
#else
#define IF_HAVE_PERL_5_19_3(YES, NO) NO
#endif
#ifndef SvREFCNT_dec_NN
#define SvREFCNT_dec_NN(SV) SvREFCNT_dec(SV)
#endif
#define MY_PKG "Function::Parameters"
/* 5.22+ shouldn't require any hax */
#if !HAVE_PERL_VERSION(5, 22, 0)
#if !HAVE_PERL_VERSION(5, 16, 0)
#include "hax/pad_alloc.c.inc"
#include "hax/pad_add_name_sv.c.inc"
#include "hax/pad_add_name_pvs.c.inc"
#ifndef padadd_NO_DUP_CHECK
#define padadd_NO_DUP_CHECK 0
#endif
#endif
#include "hax/newDEFSVOP.c.inc"
#include "hax/intro_my.c.inc"
#include "hax/block_start.c.inc"
#include "hax/block_end.c.inc"
#include "hax/op_convert_list.c.inc" /* < 5.22 */
#include "hax/STATIC_ASSERT_STMT.c.inc"
#endif
WARNINGS_ENABLE
#ifdef newSVpvf
#undef newSVpvf
#endif
#define newSVpvf @"perlapi says Perl_newSVpvf must be called explicitly (with aTHX_)"
#ifdef warner
#undef warner
#endif
#define warner @"perlapi says Perl_warner must be called explicitly (with aTHX_)"
#ifdef croak
#undef croak
#endif
#define croak @"perlapi says Perl_croak must be called explicitly (with aTHX_)"
#define HAVE_BUG_GH_15557 (HAVE_PERL_VERSION(5, 21, 7) && !HAVE_PERL_VERSION(5, 25, 5))
#define HINTK_CONFIG MY_PKG "/config"
#define HINTSK_FLAGS "flags"
#define HINTSK_SHIFT "shift"
#define HINTSK_SHIF2 "shift_types"
#define HINTSK_ATTRS "attrs"
#define HINTSK_REIFY "reify"
#define HINTSK_INSTL "instl"
#define DEFSTRUCT(T) typedef struct T T; struct T
#define VEC(B) B ## _Vec
#define DEFVECTOR(B) DEFSTRUCT(VEC(B)) { \
B (*data); \
size_t used, size; \
}
#define DEFVECTOR_INIT(N, B) static void N(VEC(B) *p) { \
p->used = 0; \
p->size = 23; \
Newx(p->data, p->size, B); \
} static void N(VEC(B) *)
#define DEFVECTOR_EXTEND(N, B) static B (*N(VEC(B) *p)) { \
assert(p->used <= p->size); \
if (p->used == p->size) { \
const size_t n = p->size / 2 * 3 + 1; \
Renew(p->data, n, B); \
p->size = n; \
} \
return &p->data[p->used]; \
} static B (*N(VEC(B) *))
#define DEFVECTOR_CLEAR_GENERIC(N, N_PARAM_, B, F, F_ARG_) static void N(N_PARAM_ VEC(B) *p) { \
while (p->used) { \
p->used--; \
F(F_ARG_ &p->data[p->used]); \
} \
Safefree(p->data); \
p->data = NULL; \
p->size = 0; \
} static void N(N_PARAM_ VEC(B) *)
#define DEFVECTOR_CLEAR(N, B, F) DEFVECTOR_CLEAR_GENERIC(N, , B, F, )
#define DEFVECTOR_CLEAR_THX(N, B, F) DEFVECTOR_CLEAR_GENERIC(N, pTHX_, B, F, aTHX_)
enum {
FLAG_NAME_OK = 0x001,
FLAG_ANON_OK = 0x002,
FLAG_DEFAULT_ARGS = 0x004,
FLAG_CHECK_NARGS = 0x008,
FLAG_INVOCANT = 0x010,
FLAG_NAMED_PARAMS = 0x020,
FLAG_TYPES_OK = 0x040,
FLAG_CHECK_TARGS = 0x080,
FLAG_RUNTIME = 0x100
};
DEFSTRUCT(SpecParam) {
SV *name;
SV *type;
};
DEFVECTOR(SpecParam);
DEFVECTOR_INIT(spv_init, SpecParam);
static void sp_clear(SpecParam *p) {
p->name = NULL;
p->type = NULL;
}
DEFVECTOR_CLEAR(spv_clear, SpecParam, sp_clear);
DEFVECTOR_EXTEND(spv_extend, SpecParam);
static void spv_push(VEC(SpecParam) *ps, SV *name, SV *type) {
SpecParam *p = spv_extend(ps);
p->name = name;
p->type = type;
ps->used++;
}
DEFSTRUCT(KWSpec) {
unsigned flags;
SV *reify_type;
VEC(SpecParam) shift;
SV *attrs;
SV *install_sub;
};
static void kws_free_void(pTHX_ void *p) {
KWSpec *const spec = p;
PERL_UNUSED_CONTEXT;
spv_clear(&spec->shift);
spec->attrs = NULL;
spec->install_sub = NULL;
Safefree(spec);
}
DEFSTRUCT(Resource) {
Resource *next;
void *data;
void (*destroy)(pTHX_ void *);
};
typedef Resource *Sentinel[1];
static void sentinel_clear_void(pTHX_ void *pv) {
Resource **pp = pv;
Resource *p = *pp;
Safefree(pp);
while (p) {
Resource *cur = p;
if (cur->destroy) {
cur->destroy(aTHX_ cur->data);
}
cur->data = (void *)"no";
cur->destroy = NULL;
p = cur->next;
Safefree(cur);
}
}
static Resource *sentinel_register(Sentinel sen, void *data, void (*destroy)(pTHX_ void *)) {
Resource *cur;
Newx(cur, 1, Resource);
cur->data = data;
cur->destroy = destroy;
cur->next = *sen;
*sen = cur;
return cur;
}
static void sentinel_disarm(Resource *p) {
p->destroy = NULL;
}
static void my_sv_refcnt_dec_void(pTHX_ void *p) {
SV *sv = p;
SvREFCNT_dec(sv);
}
static SV *sentinel_mortalize(Sentinel sen, SV *sv) {
sentinel_register(sen, sv, my_sv_refcnt_dec_void);
return sv;
}
DEFSTRUCT(RStore_U32) {
U32 *where;
U32 what;
};
static void resource_store_u32(pTHX_ void *p) {
PERL_UNUSED_CONTEXT;
RStore_U32 *rs = p;
*rs->where = rs->what;
Safefree(rs);
}
static void sentinel_save_u32(Sentinel sen, U32 *pu) {
RStore_U32 *rs;
Newx(rs, 1, RStore_U32);
rs->where = pu;
rs->what = *pu;
sentinel_register(sen, rs, resource_store_u32);
}
#if HAVE_PERL_VERSION(5, 17, 2)
#define MY_OP_SLABBED(O) ((O)->op_slabbed)
#else
#define MY_OP_SLABBED(O) 0
#endif
DEFSTRUCT(OpGuard) {
OP *op;
bool needs_freed;
};
static void op_guard_init(OpGuard *p) {
p->op = NULL;
p->needs_freed = FALSE;
}
static OpGuard op_guard_transfer(OpGuard *p) {
OpGuard r = *p;
op_guard_init(p);
return r;
}
static OP *op_guard_relinquish(OpGuard *p) {
OP *o = p->op;
op_guard_init(p);
return o;
}
static void op_guard_update(OpGuard *p, OP *o) {
p->op = o;
p->needs_freed = o && !MY_OP_SLABBED(o);
}
static void op_guard_clear(pTHX_ OpGuard *p) {
if (p->needs_freed) {
op_free(p->op);
}
}
static void free_op_guard_void(pTHX_ void *vp) {
OpGuard *p = vp;
op_guard_clear(aTHX_ p);
Safefree(p);
}
static void free_op_void(pTHX_ void *vp) {
OP *p = vp;
op_free(p);
}
#define sv_eq_pvs(SV, S) my_sv_eq_pvn(aTHX_ SV, "" S "", sizeof S - 1)
static int my_sv_eq_pvn(pTHX_ SV *sv, const char *p, STRLEN n) {
STRLEN sv_len;
const char *sv_p = SvPV(sv, sv_len);
return sv_len == n && memcmp(sv_p, p, n) == 0;
}
#ifndef newMETHOP
#define newMETHOP newUNOP
#endif
enum {
MY_ATTR_LVALUE = 0x01,
MY_ATTR_METHOD = 0x02,
MY_ATTR_SPECIAL = 0x04
};
static void my_sv_cat_c(pTHX_ SV *sv, U32 c) {
char ds[UTF8_MAXBYTES + 1], *d;
d = (char *)uvchr_to_utf8((U8 *)ds, c);
if (d - ds > 1) {
sv_utf8_upgrade(sv);
}
sv_catpvn(sv, ds, d - ds);
}
#define MY_UNI_IDFIRST(C) isIDFIRST_uni(C)
#define MY_UNI_IDCONT(C) isALNUM_uni(C)
#if HAVE_PERL_VERSION(5, 25, 9)
#define MY_UNI_IDFIRST_utf8(P, Z) isIDFIRST_utf8_safe((const unsigned char *)(P), (const unsigned char *)(Z))
#define MY_UNI_IDCONT_utf8(P, Z) isWORDCHAR_utf8_safe((const unsigned char *)(P), (const unsigned char *)(Z))
#else
#define MY_UNI_IDFIRST_utf8(P, Z) isIDFIRST_utf8((const unsigned char *)(P))
#define MY_UNI_IDCONT_utf8(P, Z) isALNUM_utf8((const unsigned char *)(P))
#endif
static SV *my_scan_word(pTHX_ Sentinel sen, bool allow_package) {
bool at_start, at_substart;
I32 c;
SV *sv = sentinel_mortalize(sen, newSVpvs(""));
if (lex_bufutf8()) {
SvUTF8_on(sv);
}
at_start = at_substart = TRUE;
c = lex_peek_unichar(0);
while (c != -1) {
if (at_substart ? MY_UNI_IDFIRST(c) : MY_UNI_IDCONT(c)) {
lex_read_unichar(0);
my_sv_cat_c(aTHX_ sv, c);
at_substart = FALSE;
c = lex_peek_unichar(0);
} else if (allow_package && !at_substart && c == '\'') {
lex_read_unichar(0);
c = lex_peek_unichar(0);
if (!MY_UNI_IDFIRST(c)) {
lex_stuff_pvs("'", 0);
break;
}
sv_catpvs(sv, "'");
at_substart = TRUE;
} else if (allow_package && (at_start || !at_substart) && c == ':') {
lex_read_unichar(0);
if (lex_peek_unichar(0) != ':') {
lex_stuff_pvs(":", 0);
break;
}
lex_read_unichar(0);
c = lex_peek_unichar(0);
if (!MY_UNI_IDFIRST(c)) {
lex_stuff_pvs("::", 0);
break;
}
sv_catpvs(sv, "::");
at_substart = TRUE;
} else {
break;
}
at_start = FALSE;
}
return SvCUR(sv) ? sv : NULL;
}
static SV *my_scan_parens_tail(pTHX_ Sentinel sen, bool keep_backslash) {
I32 c, nesting;
SV *sv;
line_t start;
start = CopLINE(PL_curcop);
sv = sentinel_mortalize(sen, newSVpvs(""));
if (lex_bufutf8()) {
SvUTF8_on(sv);
}
nesting = 0;
for (;;) {
c = lex_read_unichar(0);
if (c == EOF) {
CopLINE_set(PL_curcop, start);
return NULL;
}
if (c == '\\') {
c = lex_read_unichar(0);
if (c == EOF) {
CopLINE_set(PL_curcop, start);
return NULL;
}
if (keep_backslash || (c != '(' && c != ')')) {
sv_catpvs(sv, "\\");
}
} else if (c == '(') {
nesting++;
} else if (c == ')') {
if (!nesting) {
break;
}
nesting--;
}
my_sv_cat_c(aTHX_ sv, c);
}
return sv;
}
static void my_check_prototype(pTHX_ Sentinel sen, const SV *declarator, SV *proto) {
char *start, *r, *w, *end;
STRLEN len;
/* strip spaces */
start = SvPVbyte_force(proto, len);
end = start + len;
for (w = r = start; r < end; r++) {
if (!isSPACE(*r)) {
*w++ = *r;
}
}
*w = '\0';
SvCUR_set(proto, w - start);
end = w;
len = end - start;
if (!ckWARN(WARN_ILLEGALPROTO)) {
return;
}
/* check for bad characters */
if (strspn(start, "$@%*;[]&\\_+") != len) {
SV *dsv = sentinel_mortalize(sen, newSVpvs(""));
Perl_warner(
aTHX_
packWARN(WARN_ILLEGALPROTO),
"Illegal character in prototype for %"SVf" : %s",
SVfARG(declarator),
SvUTF8(proto)
? sv_uni_display(
dsv,
proto,
len,
UNI_DISPLAY_ISPRINT
)
: pv_pretty(dsv, start, len, 60, NULL, NULL,
PERL_PV_ESCAPE_NONASCII
)
);
return;
}
for (r = start; r < end; r++) {
switch (*r) {
default:
Perl_warner(
aTHX_
packWARN(WARN_ILLEGALPROTO),
"Illegal character in prototype for %"SVf" : %s",
SVfARG(declarator), r
);
return;
case '_':
if (r[1] && !strchr(";@%", r[1])) {
Perl_warner(
aTHX_
packWARN(WARN_ILLEGALPROTO),
"Illegal character after '_' in prototype for %"SVf" : %s",
SVfARG(declarator), r + 1
);
return;
}
break;
case '@':
case '%':
if (r[1]) {
Perl_warner(
aTHX_
packWARN(WARN_ILLEGALPROTO),
"prototype after '%c' for %"SVf": %s",
*r, SVfARG(declarator), r + 1
);
return;
}
break;
case '\\':
r++;
if (strchr("$@%&*", *r)) {
break;
}
if (*r == '[') {
r++;
for (; r < end && *r != ']'; r++) {
if (!strchr("$@%&*", *r)) {
break;
}
}
if (*r == ']' && r[-1] != '[') {
break;
}
}
Perl_warner(
aTHX_
packWARN(WARN_ILLEGALPROTO),
"Illegal character after '\\' in prototype for %"SVf" : %s",
SVfARG(declarator), r
);
return;
case '$':
case '*':
case '&':
case ';':
case '+':
break;
}
}
}
static SV *parse_type(pTHX_ Sentinel, const SV *, char);
static SV *parse_type_paramd(pTHX_ Sentinel sen, const SV *declarator, char prev) {
I32 c;
SV *t;
if (!(t = my_scan_word(aTHX_ sen, TRUE))) {
Perl_croak(aTHX_ "In %"SVf": missing type name after '%c'", SVfARG(declarator), prev);
}
lex_read_space(0);
c = lex_peek_unichar(0);
if (c == '[') {
do {
SV *u;
lex_read_unichar(0);
lex_read_space(0);
my_sv_cat_c(aTHX_ t, c);
u = parse_type(aTHX_ sen, declarator, c);
sv_catsv(t, u);
c = lex_peek_unichar(0);
} while (c == ',');
if (c != ']') {
Perl_croak(aTHX_ "In %"SVf": missing ']' after '%"SVf"'", SVfARG(declarator), SVfARG(t));
}
lex_read_unichar(0);
lex_read_space(0);
my_sv_cat_c(aTHX_ t, c);
}
return t;
}
static SV *parse_type_term(pTHX_ Sentinel sen, const SV *declarator, char prev) {
I32 c;
SV *t, *u;
t = sentinel_mortalize(sen, newSVpvs(""));
while ((c = lex_peek_unichar(0)) == '~') {
lex_read_unichar(0);
lex_read_space(0);
my_sv_cat_c(aTHX_ t, c);
prev = c;
}
if (c == '(') {
lex_read_unichar(0);
lex_read_space(0);
my_sv_cat_c(aTHX_ t, c);
u = parse_type(aTHX_ sen, declarator, c);
sv_catsv(t, u);
c = lex_peek_unichar(0);
if (c != ')') {
Perl_croak(aTHX_ "In %"SVf": missing ')' after '%"SVf"'", SVfARG(declarator), SVfARG(t));
}
my_sv_cat_c(aTHX_ t, c);
lex_read_unichar(0);
lex_read_space(0);
return t;
}
u = parse_type_paramd(aTHX_ sen, declarator, prev);
sv_catsv(t, u);
return t;
}
static SV *parse_type_alt(pTHX_ Sentinel sen, const SV *declarator, char prev) {
I32 c;
SV *t;
t = parse_type_term(aTHX_ sen, declarator, prev);
while ((c = lex_peek_unichar(0)) == '/') {
SV *u;
lex_read_unichar(0);
lex_read_space(0);
my_sv_cat_c(aTHX_ t, c);
u = parse_type_term(aTHX_ sen, declarator, c);
sv_catsv(t, u);
}
return t;
}
static SV *parse_type_intersect(pTHX_ Sentinel sen, const SV *declarator, char prev) {
I32 c;
SV *t;
t = parse_type_alt(aTHX_ sen, declarator, prev);
while ((c = lex_peek_unichar(0)) == '&') {
SV *u;
lex_read_unichar(0);
lex_read_space(0);
my_sv_cat_c(aTHX_ t, c);
u = parse_type_alt(aTHX_ sen, declarator, c);
sv_catsv(t, u);
}
return t;
}
static SV *parse_type(pTHX_ Sentinel sen, const SV *declarator, char prev) {
I32 c;
SV *t;
t = parse_type_intersect(aTHX_ sen, declarator, prev);
while ((c = lex_peek_unichar(0)) == '|') {
SV *u;
lex_read_unichar(0);
lex_read_space(0);
my_sv_cat_c(aTHX_ t, c);
u = parse_type_intersect(aTHX_ sen, declarator, c);
sv_catsv(t, u);
}
return t;
}
static SV *call_from_curstash(pTHX_ Sentinel sen, SV *sv, SV **args, size_t nargs, I32 flags) {
SV *r;
COP curcop_with_stash;
I32 want;
dSP;
assert(sv != NULL);
if ((flags & G_WANT) == 0) {
flags |= G_SCALAR;
}
want = flags & G_WANT;
ENTER;
SAVETMPS;
PUSHMARK(SP);
if (!args) {
flags |= G_NOARGS;
} else {
size_t i;
EXTEND(SP, (SSize_t)nargs);
for (i = 0; i < nargs; i++) {
PUSHs(args[i]);
}
}
PUTBACK;
assert(PL_curcop == &PL_compiling);
curcop_with_stash = PL_compiling;
CopSTASH_set(&curcop_with_stash, PL_curstash);
PL_curcop = &curcop_with_stash;
call_sv(sv, flags);
PL_curcop = &PL_compiling;
if (want == G_VOID) {
r = NULL;
} else {
assert(want == G_SCALAR);
SPAGAIN;
r = sentinel_mortalize(sen, SvREFCNT_inc(POPs));
PUTBACK;
}
FREETMPS;
LEAVE;
return r;
}
static SV *reify_type(pTHX_ Sentinel sen, const SV *declarator, const KWSpec *spec, SV *name) {
SV *t;
t = call_from_curstash(aTHX_ sen, spec->reify_type, &name, 1, 0);
if (!sv_isobject(t)) {
Perl_croak(aTHX_ "In %"SVf": invalid type '%"SVf"' (%"SVf" is not a type object)", SVfARG(declarator), SVfARG(name), SVfARG(t));
}
return t;
}
DEFSTRUCT(Param) {
SV *name;
PADOFFSET padoff;
SV *type;
};
typedef enum {
ICOND_EXISTS,
ICOND_DEFINED
} InitCond;
DEFSTRUCT(ParamInit) {
Param param;
OpGuard init;
InitCond cond;
};
DEFVECTOR(Param);
DEFVECTOR(ParamInit);
DEFSTRUCT(ParamSpec) {
size_t shift;
VEC(Param) positional_required;
VEC(ParamInit) positional_optional;
VEC(Param) named_required;
VEC(ParamInit) named_optional;
Param slurpy;
PADOFFSET rest_hash;
};
DEFVECTOR_INIT(pv_init, Param);
DEFVECTOR_INIT(piv_init, ParamInit);
static void p_init(Param *p) {
p->name = NULL;
p->padoff = NOT_IN_PAD;
p->type = NULL;
}
static void ps_init(ParamSpec *ps) {
ps->shift = 0;
pv_init(&ps->positional_required);
piv_init(&ps->positional_optional);
pv_init(&ps->named_required);
piv_init(&ps->named_optional);
p_init(&ps->slurpy);
ps->rest_hash = NOT_IN_PAD;
}
DEFVECTOR_EXTEND(pv_extend, Param);
DEFVECTOR_EXTEND(piv_extend, ParamInit);
static void pv_push(VEC(Param) *ps, SV *name, PADOFFSET padoff, SV *type) {
Param *p = pv_extend(ps);
p->name = name;
p->padoff = padoff;
p->type = type;
ps->used++;
}
static Param *pv_unshift(VEC(Param) *ps, size_t n) {
size_t i;
assert(ps->used <= ps->size);
if (ps->used + n > ps->size) {
const size_t n2 = ps->used + n + 10;
Renew(ps->data, n2, Param);
ps->size = n2;
}
Move(ps->data, ps->data + n, ps->used, Param);
for (i = 0; i < n; i++) {
p_init(&ps->data[i]);
}
ps->used += n;
return ps->data;
}
static void p_clear(Param *p) {
p->name = NULL;
p->padoff = NOT_IN_PAD;
p->type = NULL;
}
static void pi_clear(pTHX_ ParamInit *pi) {
p_clear(&pi->param);
op_guard_clear(aTHX_ &pi->init);
}
DEFVECTOR_CLEAR(pv_clear, Param, p_clear);
DEFVECTOR_CLEAR_THX(piv_clear, ParamInit, pi_clear);
static void ps_clear(pTHX_ ParamSpec *ps) {
pv_clear(&ps->positional_required);
piv_clear(aTHX_ &ps->positional_optional);
pv_clear(&ps->named_required);
piv_clear(aTHX_ &ps->named_optional);
p_clear(&ps->slurpy);
}
static int ps_contains(pTHX_ const ParamSpec *ps, SV *sv) {
size_t i, lim;
for (i = 0, lim = ps->positional_required.used; i < lim; i++) {
if (sv_eq(sv, ps->positional_required.data[i].name)) {
return 1;
}
}
for (i = 0, lim = ps->positional_optional.used; i < lim; i++) {
if (sv_eq(sv, ps->positional_optional.data[i].param.name)) {
return 1;
}
}
for (i = 0, lim = ps->named_required.used; i < lim; i++) {
if (sv_eq(sv, ps->named_required.data[i].name)) {
return 1;
}
}
for (i = 0, lim = ps->named_optional.used; i < lim; i++) {
if (sv_eq(sv, ps->named_optional.data[i].param.name)) {
return 1;
}
}
return 0;
}
static void ps_free_void(pTHX_ void *p) {
ps_clear(aTHX_ p);
Safefree(p);
}
static int args_min(const ParamSpec *ps) {
return ps->positional_required.used + ps->named_required.used * 2;
}
static int args_max(const ParamSpec *ps) {
if (ps->named_required.used || ps->named_optional.used || ps->slurpy.name) {
return -1;
}
return ps->positional_required.used + ps->positional_optional.used;
}
static size_t count_positional_params(const ParamSpec *ps) {
return ps->positional_required.used + ps->positional_optional.used;
}
static size_t count_named_params(const ParamSpec *ps) {
return ps->named_required.used + ps->named_optional.used;
}
static SV *my_eval(pTHX_ Sentinel sen, I32 floor_ix, OP *op) {
CV *cv;
cv = newATTRSUB(floor_ix, NULL, NULL, NULL, op);
return call_from_curstash(aTHX_ sen, (SV *)cv, NULL, 0, 0);
}
static OP *my_var_g(pTHX_ I32 type, I32 flags, PADOFFSET padoff) {
OP *var = newOP(type, flags);
var->op_targ = padoff;
return var;
}
static OP *my_var(pTHX_ I32 flags, PADOFFSET padoff) {
return my_var_g(aTHX_ OP_PADSV, flags, padoff);
}
static OP *mkhvelem(pTHX_ PADOFFSET h, OP *k) {
OP *hv = my_var_g(aTHX_ OP_PADHV, OPf_REF, h);
return newBINOP(OP_HELEM, 0, hv, k);
}
static OP *mkconstsv(pTHX_ SV *sv) {
return newSVOP(OP_CONST, 0, sv);
}
static OP *mkconstiv(pTHX_ IV i) {
return mkconstsv(aTHX_ newSViv(i));
}
static OP *mkconstpv(pTHX_ const char *p, size_t n) {
return mkconstsv(aTHX_ newSVpv(p, n));
}
#define mkconstpvs(S) mkconstpv(aTHX_ "" S "", sizeof S - 1)
static OP *mkcroak(pTHX_ OP *msg) {
OP *xcroak;
xcroak = newCVREF(
OPf_WANT_SCALAR,
mkconstsv(aTHX_ newSVpvs(MY_PKG "::_croak"))
);
xcroak = newUNOP(OP_ENTERSUB, OPf_STACKED, op_append_elem(OP_LIST, msg, xcroak));
return xcroak;
}
static OP *mktypecheckv(pTHX_ Sentinel sen, const SV *declarator, size_t nr, SV *name, PADOFFSET padoff, SV *type, int is_invocant) {
/* $type->can("has_coercion") && $type->has_coercion
* ? $type->check($value = $type->coerce($value)) or F:P::_croak "...: " . $type->get_message($value)
* : $type->check($value) or F:P::_croak "...: " . $type->get_message($value)
*/
OP *chk, *err, *msg, *xcroak;
bool has_coercion = FALSE, can_be_inlined = FALSE;
{
GV *can_has_coercion;
if ((can_has_coercion = gv_fetchmethod_autoload(SvSTASH(SvRV(type)), "has_coercion", TRUE))) {
SV *ret = call_from_curstash(aTHX_ sen, MUTABLE_SV(GvCV(can_has_coercion)), &type, 1, 0);
if (SvTRUE(ret)) {
has_coercion = TRUE;