File Coverage

XSParseSublike.h
Criterion Covered Total %
statement 16 20 80.0
branch 10 20 50.0
condition n/a
subroutine n/a
pod n/a
total 26 40 65.0


line stmt bran cond sub pod time code
1             #ifndef __XS_PARSE_SUBLIKE_H__
2             #define __XS_PARSE_SUBLIKE_H__
3              
4             #define XSPARSESUBLIKE_ABI_VERSION 4
5              
6             struct XSParseSublikeContext {
7             SV *name; /* may be NULL for anon subs */
8             /* STAGE pre_subparse */
9             OP *attrs; /* may be NULL */
10             /* STAGE post_blockstart */
11             OP *body;
12             /* STAGE pre_blockend */
13             CV *cv;
14             /* STAGE post_newcv */
15              
16             U32 actions;
17              
18             /* Unused by XS::Parse::Sublike itself but can be handy for modules to store
19             * data in between stages */
20             HV *moddata;
21             };
22              
23             enum {
24             XS_PARSE_SUBLIKE_FLAG_FILTERATTRS = 1<<0,
25             XS_PARSE_SUBLIKE_FLAG_BODY_OPTIONAL = 1<<1,
26             XS_PARSE_SUBLIKE_FLAG_PREFIX = 1<<2,
27              
28             /* Back-compat flags we hope to remove in the next ABI version */
29             XS_PARSE_SUBLIKE_COMPAT_FLAG_DYNAMIC_ACTIONS = 1<<15,
30             };
31              
32             enum {
33             XS_PARSE_SUBLIKE_PART_NAME = 1<<0,
34             XS_PARSE_SUBLIKE_PART_ATTRS = 1<<1,
35             XS_PARSE_SUBLIKE_PART_SIGNATURE = 1<<2,
36             XS_PARSE_SUBLIKE_PART_BODY = 1<<3,
37             };
38              
39             enum {
40             XS_PARSE_SUBLIKE_ACTION_CVf_ANON = (1<<0), /* should start_subparse() take CVf_ANON ? */
41             XS_PARSE_SUBLIKE_ACTION_SET_CVNAME = (1<<1), /* do we set a CvNAME? */
42             XS_PARSE_SUBLIKE_ACTION_INSTALL_SYMBOL = (1<<2), /* do we install the new CV into the symbol table? */
43             XS_PARSE_SUBLIKE_ACTION_REFGEN_ANONCODE = (1<<3), /* do we emit OP_REFGEN of OP_ANONCODE, or simply OP_NULL ? */
44             XS_PARSE_SUBLIKE_ACTION_RET_EXPR = (1<<4), /* do we return KEYWORD_PLUGIN_EXPR, or KEYWORD_PLUGIN_STMT ? */
45             };
46              
47             struct XSParseSublikeHooks {
48             U16 flags;
49             U8 require_parts;
50             U8 skip_parts;
51              
52             /* These two hooks are ANDed together; both must pass, if present */
53             const char *permit_hintkey;
54             bool (*permit)(pTHX_ void *hookdata);
55              
56             void (*pre_subparse) (pTHX_ struct XSParseSublikeContext *ctx, void *hookdata);
57             void (*post_blockstart)(pTHX_ struct XSParseSublikeContext *ctx, void *hookdata);
58             void (*pre_blockend) (pTHX_ struct XSParseSublikeContext *ctx, void *hookdata);
59             void (*post_newcv) (pTHX_ struct XSParseSublikeContext *ctx, void *hookdata);
60              
61             /* if flags & XS_PARSE_SUBLIKE_FLAG_FILTERATTRS */
62             bool (*filter_attr) (pTHX_ struct XSParseSublikeContext *ctx, SV *attr, SV *val, void *hookdata);
63             };
64              
65             static int (*parse_xs_parse_sublike_func)(pTHX_ const struct XSParseSublikeHooks *hooks, void *hookdata, OP **op_ptr);
66             #define xs_parse_sublike(hooks, hookdata, op_ptr) S_xs_parse_sublike(aTHX_ hooks, hookdata, op_ptr)
67             static int S_xs_parse_sublike(pTHX_ const struct XSParseSublikeHooks *hooks, void *hookdata, OP **op_ptr)
68             {
69             if(!parse_xs_parse_sublike_func)
70             croak("Must call boot_xs_parse_sublike() first");
71              
72             return (*parse_xs_parse_sublike_func)(aTHX_ hooks, hookdata, op_ptr);
73             }
74              
75             static void (*register_xs_parse_sublike_func)(pTHX_ const char *kw, const struct XSParseSublikeHooks *hooks, void *hookdata);
76             #define register_xs_parse_sublike(kw, hooks, hookdata) S_register_xs_parse_sublike(aTHX_ kw, hooks, hookdata)
77             static void S_register_xs_parse_sublike(pTHX_ const char *kw, const struct XSParseSublikeHooks *hooks, void *hookdata)
78             {
79 44 50         if(!register_xs_parse_sublike_func)
80 0           croak("Must call boot_xs_parse_sublike() first");
81              
82 44           return (*register_xs_parse_sublike_func)(aTHX_ kw, hooks, hookdata);
83             }
84              
85             static int (*parseany_xs_parse_sublike_func)(pTHX_ const struct XSParseSublikeHooks *hooks, void *hookdata, OP **op_ptr);
86             #define xs_parse_sublike_any(hooks, hookdata, op_ptr) S_xs_parse_sublike_any(aTHX_ hooks, hookdata, op_ptr)
87             static int S_xs_parse_sublike_any(pTHX_ const struct XSParseSublikeHooks *hooks, void *hookdata, OP **op_ptr)
88             {
89             if(!parseany_xs_parse_sublike_func)
90             croak("Must call boot_xs_parse_sublike() first");
91              
92             return (*parseany_xs_parse_sublike_func)(aTHX_ hooks, hookdata, op_ptr);
93             }
94              
95             #define boot_xs_parse_sublike(ver) S_boot_xs_parse_sublike(aTHX_ ver)
96 44           static void S_boot_xs_parse_sublike(pTHX_ double ver) {
97             SV **svp;
98 44 50         SV *versv = ver ? newSVnv(ver) : NULL;
99              
100 44           load_module(PERL_LOADMOD_NOIMPORT, newSVpvs("XS::Parse::Sublike"), versv, NULL);
101              
102 44           svp = hv_fetchs(PL_modglobal, "XS::Parse::Sublike/ABIVERSION_MIN", 0);
103 44 50         if(!svp)
104 0           croak("XS::Parse::Sublike ABI minimum version missing");
105 44 50         int abi_ver = SvIV(*svp);
106 44 50         if(abi_ver > XSPARSESUBLIKE_ABI_VERSION)
107 0           croak("XS::Parse::Sublike ABI version mismatch - library supports >= %d, compiled for %d",
108             abi_ver, XSPARSESUBLIKE_ABI_VERSION);
109              
110 44           svp = hv_fetchs(PL_modglobal, "XS::Parse::Sublike/ABIVERSION_MAX", 0);
111 44 50         abi_ver = SvIV(*svp);
112 44 50         if(abi_ver < XSPARSESUBLIKE_ABI_VERSION)
113 0           croak("XS::Parse::Sublike ABI version mismatch - library supports <= %d, compiled for %d",
114             abi_ver, XSPARSESUBLIKE_ABI_VERSION);
115              
116 44 50         parse_xs_parse_sublike_func = INT2PTR(int (*)(pTHX_ const struct XSParseSublikeHooks *, void *, OP**),
117             SvUV(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/parse()@4", 0)));
118              
119 44 50         register_xs_parse_sublike_func = INT2PTR(void (*)(pTHX_ const char *, const struct XSParseSublikeHooks *, void *),
120             SvUV(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/register()@4", 0)));
121              
122 44 50         parseany_xs_parse_sublike_func = INT2PTR(int (*)(pTHX_ const struct XSParseSublikeHooks *, void *, OP**),
123             SvUV(*hv_fetchs(PL_modglobal, "XS::Parse::Sublike/parseany()@4", 0)));
124 44           }
125              
126             #endif