Fixed the example showing parameter type constraints
[p5sagit/Function-Parameters.git] / Parameters.xs
index cc3c498..4a44068 100644 (file)
@@ -85,11 +85,10 @@ WARNINGS_ENABLE
 #define HINTK_FLAGS_   MY_PKG "/flags:"
 #define HINTK_SHIFT_   MY_PKG "/shift:"
 #define HINTK_ATTRS_   MY_PKG "/attrs:"
+#define HINTK_REIFY_   MY_PKG "/reify:"
 
 #define DEFSTRUCT(T) typedef struct T T; struct T
 
-#define UV_BITS (sizeof (UV) * CHAR_BIT)
-
 enum {
        FLAG_NAME_OK      = 0x01,
        FLAG_ANON_OK      = 0x02,
@@ -103,6 +102,7 @@ enum {
 
 DEFSTRUCT(KWSpec) {
        unsigned flags;
+       I32 reify_type;
        SV *shift;
        SV *attrs;
 };
@@ -121,7 +121,9 @@ static void sentinel_clear_void(pTHX_ void *p) {
        Resource **pp = p;
        while (*pp) {
                Resource *cur = *pp;
-               cur->destroy(aTHX_ cur->data);
+               if (cur->destroy) {
+                       cur->destroy(aTHX_ cur->data);
+               }
                cur->data = (void *)"no";
                cur->destroy = NULL;
                *pp = cur->next;
@@ -129,7 +131,7 @@ static void sentinel_clear_void(pTHX_ void *p) {
        }
 }
 
-static void sentinel_register(Sentinel sen, void *data, void (*destroy)(pTHX_ void *)) {
+static Resource *sentinel_register(Sentinel sen, void *data, void (*destroy)(pTHX_ void *)) {
        Resource *cur;
 
        Newx(cur, 1, Resource);
@@ -137,6 +139,12 @@ static void sentinel_register(Sentinel sen, void *data, void (*destroy)(pTHX_ vo
        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) {
@@ -149,105 +157,50 @@ static SV *sentinel_mortalize(Sentinel sen, SV *sv) {
        return sv;
 }
 
-static void my_safefree(void *p) {
-       Safefree(p);
-}
-
-#define SENTINEL_ALLOC(SEN, P, N, T) STMT_START { \
-       Newx(P, N, T); \
-       sentinel_register(SEN, P, my_safefree); \
-} STMT_END
-
-#define SENTINEL_MDUP(SEN, P, O, N, T) STMT_START { \
-       void *const _sentinel_mdup_tmp_ = (P); \
-       SENTINEL_ALLOC(SEN, P, N, T); \
-       memcpy(P, _sentinel_mdup_tmp_, O * sizeof (T)); \
-} STMT_END
 
-#define SENTINEL_REALLOC(SEN, P, N, T) STMT_START { \
-       assert((N) > 0); \
-       if (!(P)) { \
-               SENTINEL_ALLOC(SEN, P, N, T); \
-       } else { \
-               Resource **_sentinel_realloc_tmp_ = (SEN); \
-               for (;;) { \
-                       assert(*_sentinel_realloc_tmp_ != NULL); \
-                       if ((*_sentinel_realloc_tmp_)->data == (P)) { \
-                               Renew((*_sentinel_realloc_tmp_)->data, N, T); \
-                               (P) = (*_sentinel_realloc_tmp_)->data; \
-                               break; \
-                       } \
-                       _sentinel_realloc_tmp_ = &(*_sentinel_realloc_tmp_)->next; \
-               } \
-       } \
-} STMT_END
-
-static int kw_flags(pTHX_ Sentinel sen, const char *kw_ptr, STRLEN kw_len, KWSpec *spec) {
-       HV *hints;
-       SV *sv, **psv;
-       const char *p, *kw_active;
-       STRLEN kw_active_len;
-
-       spec->flags = 0;
-       spec->shift = sentinel_mortalize(sen, newSVpvs(""));
-       spec->attrs = sentinel_mortalize(sen, newSVpvs(""));
+#if HAVE_PERL_VERSION(5, 17, 2)
+ #define MY_OP_SLABBED(O) ((O)->op_slabbed)
+#else
+ #define MY_OP_SLABBED(O) 0
+#endif
 
-       if (!(hints = GvHV(PL_hintgv))) {
-               return FALSE;
-       }
-       if (!(psv = hv_fetchs(hints, HINTK_KEYWORDS, 0))) {
-               return FALSE;
-       }
-       sv = *psv;
-       kw_active = SvPV(sv, kw_active_len);
-       if (kw_active_len <= kw_len) {
-               return FALSE;
-       }
-       for (
-               p = kw_active;
-               (p = strchr(p, *kw_ptr)) &&
-               p < kw_active + kw_active_len - kw_len;
-               p++
-       ) {
-               if (
-                       (p == kw_active || p[-1] == ' ') &&
-                       p[kw_len] == ' ' &&
-                       memcmp(kw_ptr, p, kw_len) == 0
-               ) {
+DEFSTRUCT(OpGuard) {
+       OP *op;
+       bool needs_freed;
+};
 
-#define FETCH_HINTK_INTO(NAME, PTR, LEN, X) STMT_START { \
-       const char *fk_ptr_; \
-       STRLEN fk_len_; \
-       SV *fk_sv_; \
-       fk_sv_ = sentinel_mortalize(sen, newSVpvs(HINTK_ ## NAME)); \
-       sv_catpvn(fk_sv_, PTR, LEN); \
-       fk_ptr_ = SvPV(fk_sv_, fk_len_); \
-       if (!((X) = hv_fetch(hints, fk_ptr_, fk_len_, 0))) { \
-               croak("%s: internal error: $^H{'%.*s'} not set", MY_PKG, (int)fk_len_, fk_ptr_); \
-       } \
-} STMT_END
+static void op_guard_init(OpGuard *p) {
+       p->op = NULL;
+       p->needs_freed = FALSE;
+}
 
-                       FETCH_HINTK_INTO(FLAGS_, kw_ptr, kw_len, psv);
-                       spec->flags = SvIV(*psv);
+static OpGuard op_guard_transfer(OpGuard *p) {
+       OpGuard r = *p;
+       op_guard_init(p);
+       return r;
+}
 
-                       FETCH_HINTK_INTO(SHIFT_, kw_ptr, kw_len, psv);
-                       SvSetSV(spec->shift, *psv);
+static OP *op_guard_relinquish(OpGuard *p) {
+       OP *o = p->op;
+       op_guard_init(p);
+       return o;
+}
 
-                       FETCH_HINTK_INTO(ATTRS_, kw_ptr, kw_len, psv);
-                       SvSetSV(spec->attrs, *psv);
+static void op_guard_update(OpGuard *p, OP *o) {
+       p->op = o;
+       p->needs_freed = o && !MY_OP_SLABBED(o);
+}
 
-#undef FETCH_HINTK_INTO
-                       return TRUE;
-               }
+static void op_guard_clear(pTHX_ OpGuard *p) {
+       if (p->needs_freed) {
+               op_free(p->op);
        }
-       return FALSE;
 }
 
-
-static void free_ptr_op_void(pTHX_ void *vp) {
-       OP **pp = vp;
-       op_free(*pp);
-       Safefree(pp);
+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) {
@@ -275,24 +228,16 @@ enum {
 
 static void my_sv_cat_c(pTHX_ SV *sv, U32 c) {
        char ds[UTF8_MAXBYTES + 1], *d;
-       d = uvchr_to_utf8(ds, c);
+       d = (char *)uvchr_to_utf8((U8 *)ds, c);
        if (d - ds > 1) {
                sv_utf8_upgrade(sv);
        }
        sv_catpvn(sv, ds, d - ds);
 }
 
-static bool my_is_uni_xidfirst(pTHX_ UV c) {
-       U8 tmpbuf[UTF8_MAXBYTES + 1];
-       uvchr_to_utf8(tmpbuf, c);
-       return is_utf8_xidfirst(tmpbuf);
-}
 
-static bool my_is_uni_xidcont(pTHX_ UV c) {
-       U8 tmpbuf[UTF8_MAXBYTES + 1];
-       uvchr_to_utf8(tmpbuf, c);
-       return is_utf8_xidcont(tmpbuf);
-}
+#define MY_UNI_IDFIRST(C) isIDFIRST_uni(C)
+#define MY_UNI_IDCONT(C)  isALNUM_uni(C)
 
 static SV *my_scan_word(pTHX_ Sentinel sen, bool allow_package) {
        bool at_start, at_substart;
@@ -306,7 +251,7 @@ static SV *my_scan_word(pTHX_ Sentinel sen, bool allow_package) {
        c = lex_peek_unichar(0);
 
        while (c != -1) {
-               if (at_substart ? my_is_uni_xidfirst(aTHX_ c) : my_is_uni_xidcont(aTHX_ c)) {
+               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;
@@ -314,7 +259,7 @@ static SV *my_scan_word(pTHX_ Sentinel sen, bool allow_package) {
                } else if (allow_package && !at_substart && c == '\'') {
                        lex_read_unichar(0);
                        c = lex_peek_unichar(0);
-                       if (!my_is_uni_xidfirst(aTHX_ c)) {
+                       if (!MY_UNI_IDFIRST(c)) {
                                lex_stuff_pvs("'", 0);
                                break;
                        }
@@ -328,7 +273,7 @@ static SV *my_scan_word(pTHX_ Sentinel sen, bool allow_package) {
                        }
                        lex_read_unichar(0);
                        c = lex_peek_unichar(0);
-                       if (!my_is_uni_xidfirst(aTHX_ c)) {
+                       if (!MY_UNI_IDFIRST(c)) {
                                lex_stuff_pvs("::", 0);
                                break;
                        }
@@ -552,23 +497,39 @@ static SV *parse_type(pTHX_ Sentinel sen, const SV *declarator) {
        return t;
 }
 
-static SV *reify_type(pTHX_ Sentinel sen, const SV *declarator, SV *name) {
-       SV *t;
+static SV *reify_type(pTHX_ Sentinel sen, const SV *declarator, const KWSpec *spec, SV *name) {
+       AV *type_reifiers;
+       SV *t, *sv, **psv;
        int n;
        dSP;
 
+       type_reifiers = get_av(MY_PKG "::type_reifiers", 0);
+       assert(type_reifiers != NULL);
+
+       if (spec->reify_type < 0 || spec->reify_type > av_len(type_reifiers)) {
+               croak("In %"SVf": internal error: reify_type [%ld] out of range [%ld]", SVfARG(declarator), (long)spec->reify_type, (long)(av_len(type_reifiers) + 1));
+       }
+
+       psv = av_fetch(type_reifiers, spec->reify_type, 0);
+       assert(psv != NULL);
+       sv = *psv;
+
        ENTER;
        SAVETMPS;
 
        PUSHMARK(SP);
-       EXTEND(SP, 1);
+       EXTEND(SP, 2);
        PUSHs(name);
+       PUSHs(PL_curstname);
        PUTBACK;
 
-       n = call_pv("Moose::Util::TypeConstraints::find_or_parse_type_constraint", G_SCALAR);
+       n = call_sv(sv, G_SCALAR);
        SPAGAIN;
 
        assert(n == 1);
+       /* don't warn about n being unused if assert() is compiled out */
+       n = n;
+
        t = sentinel_mortalize(sen, SvREFCNT_inc(POPs));
 
        PUTBACK;
@@ -591,7 +552,7 @@ DEFSTRUCT(Param) {
 
 DEFSTRUCT(ParamInit) {
        Param param;
-       OP *init;
+       OpGuard init;
 };
 
 #define VEC(B) B ## _Vec
@@ -670,10 +631,7 @@ static void p_clear(pTHX_ Param *p) {
 
 static void pi_clear(pTHX_ ParamInit *pi) {
        p_clear(aTHX_ &pi->param);
-       if (pi->init) {
-               op_free(pi->init);
-               pi->init = NULL;
-       }
+       op_guard_clear(aTHX_ &pi->init);
 }
 
 DEFVECTOR_CLEAR(pv_clear, Param, p_clear);
@@ -767,13 +725,6 @@ static size_t count_named_params(const ParamSpec *ps) {
        return ps->named_required.used + ps->named_optional.used;
 }
 
-static void my_require(pTHX_ const char *file) {
-       require_pv(file);
-       if (SvTRUE(ERRSV)) {
-               croak_sv(ERRSV);
-       }
-}
-
 static SV *my_eval(pTHX_ Sentinel sen, I32 floor, OP *op) {
        SV *sv;
        CV *cv;
@@ -801,20 +752,17 @@ enum {
        PARAM_NAMED    = 0x02
 };
 
-/* *pinit must be NULL on entry.
- * caller must free *pinit on error.
- */
 static PADOFFSET parse_param(
        pTHX_
        Sentinel sen,
        const SV *declarator, const KWSpec *spec, ParamSpec *param_spec,
-       int *pflags, SV **pname, OP **pinit, SV **ptype
+       int *pflags, SV **pname, OpGuard *ginit, SV **ptype
 ) {
        I32 c;
        char sigil;
-       SV *name, *typeobj, *typename;
+       SV *name;
 
-       assert(!*pinit);
+       assert(!ginit->op);
        *pflags = 0;
        *ptype = NULL;
 
@@ -824,15 +772,22 @@ static PADOFFSET parse_param(
                if (c == '(') {
                        I32 floor;
                        OP *expr;
+                       Resource *expr_sentinel;
 
                        lex_read_unichar(0);
 
-                       floor = start_subparse(FALSE, CVf_ANON);
+                       floor = start_subparse(FALSE, 0);
+                       SAVEFREESV(PL_compcv);
+                       CvSPECIAL_on(PL_compcv);
 
                        if (!(expr = parse_fullexpr(PARSE_OPTIONAL))) {
                                croak("In %"SVf": invalid type expression", SVfARG(declarator));
                        }
-                       sentinel_register(sen, expr, free_op_void);
+                       if (MY_OP_SLABBED(expr)) {
+                               expr_sentinel = NULL;
+                       } else {
+                               expr_sentinel = sentinel_register(sen, expr, free_op_void);
+                       }
 
                        lex_read_space(0);
                        c = lex_peek_unichar(0);
@@ -842,17 +797,22 @@ static PADOFFSET parse_param(
                        lex_read_unichar(0);
                        lex_read_space(0);
 
+                       SvREFCNT_inc_simple_void(PL_compcv);
+                       if (expr_sentinel) {
+                               sentinel_disarm(expr_sentinel);
+                       }
                        *ptype = my_eval(aTHX_ sen, floor, expr);
+                       if (!SvROK(*ptype)) {
+                               *ptype = reify_type(aTHX_ sen, declarator, spec, *ptype);
+                       }
                        if (!sv_isobject(*ptype)) {
                                croak("In %"SVf": (%"SVf") doesn't look like a type object", SVfARG(declarator), SVfARG(*ptype));
                        }
 
                        c = lex_peek_unichar(0);
-               } else if (my_is_uni_xidfirst(aTHX_ c)) {
-                       typename = parse_type(aTHX_ sen, declarator);
-                       my_require(aTHX_ "Moose/Util/TypeConstraints.pm");
-                       typeobj = reify_type(aTHX_ sen, declarator, typename);
-                       *ptype = typeobj;
+               } else if (MY_UNI_IDFIRST(c)) {
+                       *ptype = parse_type(aTHX_ sen, declarator);
+                       *ptype = reify_type(aTHX_ sen, declarator, spec, *ptype);
 
                        c = lex_peek_unichar(0);
                }
@@ -899,7 +859,7 @@ static PADOFFSET parse_param(
                        param_spec->invocant.padoff = pad_add_name_sv(param_spec->invocant.name, 0, NULL, NULL);
                }
 
-               *pinit = parse_termexpr(0);
+               op_guard_update(ginit, parse_termexpr(0));
 
                lex_read_space(0);
                c = lex_peek_unichar(0);
@@ -953,7 +913,7 @@ static OP *mkconstpv(pTHX_ const char *p, size_t n) {
 
 static OP *mktypecheck(pTHX_ const SV *declarator, int nr, SV *name, PADOFFSET padoff, SV *type) {
        /* $type->check($value) or Carp::croak "...: " . $type->get_message($value) */
-       OP *chk, *cond, *err, *msg, *xcroak;
+       OP *chk, *err, *msg, *xcroak;
 
        err = mkconstsv(aTHX_ newSVpvf("In %"SVf": parameter %d (%"SVf"): ", SVfARG(declarator), nr, SVfARG(name)));
        {
@@ -1021,10 +981,11 @@ static void register_info(pTHX_ UV key, SV *declarator, const KWSpec *kws, const
                mPUSHu(key);
        }
        /* 1 */ {
-               size_t n;
+               STRLEN n;
                char *p = SvPV(declarator, n);
                char *q = memchr(p, ' ', n);
-               mPUSHp(p, q ? q - p : n);
+               SV *tmp = newSVpvn_utf8(p, q ? (size_t)(q - p) : n, SvUTF8(declarator));
+               mPUSHs(tmp);
        }
        if (!ps) {
                if (SvTRUE(kws->shift)) {
@@ -1153,13 +1114,17 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
        I32 floor_ix;
        int save_ix;
        SV *saw_name;
-       OP **prelude_sentinel;
+       OpGuard *prelude_sentinel;
        SV *proto;
-       OP **attrs_sentinel, *body;
+       OpGuard *attrs_sentinel;
+       OP *body;
        unsigned builtin_attrs;
        I32 c;
 
        declarator = sentinel_mortalize(sen, newSVpvn(keyword_ptr, keyword_len));
+       if (lex_bufutf8()) {
+               SvUTF8_on(declarator);
+       }
 
        lex_read_space(0);
 
@@ -1202,20 +1167,20 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
        save_ix = S_block_start(aTHX_ TRUE);
 
        /* initialize synthetic optree */
-       Newx(prelude_sentinel, 1, OP *);
-       *prelude_sentinel = NULL;
-       sentinel_register(sen, prelude_sentinel, free_ptr_op_void);
+       Newx(prelude_sentinel, 1, OpGuard);
+       op_guard_init(prelude_sentinel);
+       sentinel_register(sen, prelude_sentinel, free_op_guard_void);
 
        /* parameters */
        param_spec = NULL;
 
        c = lex_peek_unichar(0);
        if (c == '(') {
-               OP **init_sentinel;
+               OpGuard *init_sentinel;
 
-               Newx(init_sentinel, 1, OP *);
-               *init_sentinel = NULL;
-               sentinel_register(sen, init_sentinel, free_ptr_op_void);
+               Newx(init_sentinel, 1, OpGuard);
+               op_guard_init(init_sentinel);
+               sentinel_register(sen, init_sentinel, free_op_guard_void);
 
                Newx(param_spec, 1, ParamSpec);
                ps_init(param_spec);
@@ -1245,13 +1210,13 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                        croak("In %"SVf": named parameter %"SVf" can't be a%s", SVfARG(declarator), SVfARG(name), sigil == '@' ? "n array" : " hash");
                                }
                        } else if (flags & PARAM_INVOCANT) {
-                               if (*init_sentinel) {
+                               if (init_sentinel->op) {
                                        croak("In %"SVf": invocant %"SVf" can't have a default value", SVfARG(declarator), SVfARG(name));
                                }
                                if (sigil != '$') {
                                        croak("In %"SVf": invocant %"SVf" can't be a%s", SVfARG(declarator), SVfARG(name), sigil == '@' ? "n array" : " hash");
                                }
-                       } else if (sigil != '$' && *init_sentinel) {
+                       } else if (sigil != '$' && init_sentinel->op) {
                                croak("In %"SVf": %s %"SVf" can't have a default value", SVfARG(declarator), sigil == '@' ? "array" : "hash", SVfARG(name));
                        }
 
@@ -1260,7 +1225,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                croak("In %"SVf": I was expecting \")\" after \"%"SVf"\", not \"%"SVf"\"", SVfARG(declarator), SVfARG(param_spec->slurpy.name), SVfARG(name));
                        }
                        if (sigil != '$') {
-                               assert(!*init_sentinel);
+                               assert(!init_sentinel->op);
                                param_spec->slurpy.name = name;
                                param_spec->slurpy.padoff = padoff;
                                param_spec->slurpy.type = type;
@@ -1287,7 +1252,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                continue;
                        }
 
-                       if (*init_sentinel && !(spec->flags & FLAG_DEFAULT_ARGS)) {
+                       if (init_sentinel->op && !(spec->flags & FLAG_DEFAULT_ARGS)) {
                                croak("In %"SVf": default argument for %"SVf" not allowed here", SVfARG(declarator), SVfARG(name));
                        }
 
@@ -1300,33 +1265,33 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                        croak("In %"SVf": named parameter :%"SVf" not allowed here", SVfARG(declarator), SVfARG(name));
                                }
 
-                               if (*init_sentinel) {
+                               if (init_sentinel->op) {
                                        ParamInit *pi = piv_extend(&param_spec->named_optional);
                                        pi->param.name = name;
                                        pi->param.padoff = padoff;
                                        pi->param.type = type;
-                                       pi->init = *init_sentinel;
-                                       *init_sentinel = NULL;
+                                       pi->init = op_guard_transfer(init_sentinel);
                                        param_spec->named_optional.used++;
                                } else {
+                                       Param *p;
+
                                        if (param_spec->positional_optional.used) {
                                                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));
                                        }
 
-                                       Param *p = pv_extend(&param_spec->named_required);
+                                       p = pv_extend(&param_spec->named_required);
                                        p->name = name;
                                        p->padoff = padoff;
                                        p->type = type;
                                        param_spec->named_required.used++;
                                }
                        } else {
-                               if (*init_sentinel || param_spec->positional_optional.used) {
+                               if (init_sentinel->op || param_spec->positional_optional.used) {
                                        ParamInit *pi = piv_extend(&param_spec->positional_optional);
                                        pi->param.name = name;
                                        pi->param.padoff = padoff;
                                        pi->param.type = type;
-                                       pi->init = *init_sentinel;
-                                       *init_sentinel = NULL;
+                                       pi->init = op_guard_transfer(init_sentinel);
                                        param_spec->positional_optional.used++;
                                } else {
                                        Param *p = pv_extend(&param_spec->positional_required);
@@ -1340,7 +1305,6 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                }
                lex_read_unichar(0);
                lex_read_space(0);
-               *init_sentinel = NULL;
 
                if (!param_spec->invocant.name && SvTRUE(spec->shift)) {
                        if (ps_contains(aTHX_ param_spec, spec->shift)) {
@@ -1379,9 +1343,9 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
        }
 
        /* attributes */
-       Newx(attrs_sentinel, 1, OP *);
-       *attrs_sentinel = NULL;
-       sentinel_register(sen, attrs_sentinel, free_ptr_op_void);
+       Newx(attrs_sentinel, 1, OpGuard);
+       op_guard_init(attrs_sentinel);
+       sentinel_register(sen, attrs_sentinel, free_op_guard_void);
 
        if (c == ':' || c == '{') /* '}' - hi, vim */ {
 
@@ -1429,7 +1393,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                }
 
                                if (attr) {
-                                       *attrs_sentinel = op_append_elem(OP_LIST, *attrs_sentinel, mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(attr)));
+                                       op_guard_update(attrs_sentinel, op_append_elem(OP_LIST, attrs_sentinel->op, mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(attr))));
                                }
 
                                if (c == ':') {
@@ -1477,7 +1441,6 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
        /* check number of arguments */
        if (spec->flags & FLAG_CHECK_NARGS) {
                int amin, amax;
-               size_t named;
 
                amin = args_min(aTHX_ param_spec, spec);
                if (amin > 0) {
@@ -1505,7 +1468,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                        mkconstiv(aTHX_ amin));
                        chk = newLOGOP(OP_AND, 0, cond, err);
 
-                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, chk));
+                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, chk)));
                }
 
                amax = args_max(param_spec);
@@ -1538,7 +1501,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                        );
                        chk = newLOGOP(OP_AND, 0, cond, err);
 
-                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, chk));
+                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, chk)));
                }
 
                if (param_spec && (count_named_params(param_spec) || (param_spec->slurpy.name && SvPV_nolen(param_spec->slurpy.name)[0] == '%'))) {
@@ -1568,7 +1531,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                                 mkconstiv(aTHX_ 2)));
                        chk = newLOGOP(OP_AND, 0, cond, err);
 
-                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, chk));
+                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, chk)));
                }
        }
 
@@ -1584,7 +1547,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                        );
                        var = newASSIGNOP(OPf_STACKED, var, 0, newOP(OP_SHIFT, 0));
 
-                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, var));
+                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
                }
        } else {
                /* my $invocant = shift; */
@@ -1598,10 +1561,10 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                        );
                        var = newASSIGNOP(OPf_STACKED, var, 0, newOP(OP_SHIFT, 0));
 
-                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, var));
+                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
 
                        if (param_spec->invocant.type && (spec->flags & FLAG_CHECK_TARGS)) {
-                               *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, 0, &param_spec->invocant)));
+                               op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, 0, &param_spec->invocant))));
                        }
                }
 
@@ -1690,13 +1653,13 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                lhs->op_flags |= OPf_PARENS;
                                rhs = newAVREF(newGVOP(OP_GV, 0, PL_defgv));
 
-                               *prelude_sentinel = op_append_list(
-                                       OP_LINESEQ, *prelude_sentinel,
+                               op_guard_update(prelude_sentinel, op_append_list(
+                                       OP_LINESEQ, prelude_sentinel->op,
                                        newSTATEOP(
                                                0, NULL,
                                                newASSIGNOP(OPf_STACKED, lhs, 0, rhs)
                                        )
-                               );
+                               ));
                        }
                }
 
@@ -1722,9 +1685,8 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
 
                                nest = op_append_list(
                                        OP_LINESEQ, nest,
-                                       newASSIGNOP(OPf_STACKED, var, 0, cur->init)
+                                       newASSIGNOP(OPf_STACKED, var, 0, op_guard_relinquish(&cur->init))
                                );
-                               cur->init = NULL;
                                nest = newCONDOP(
                                        0,
                                        cond,
@@ -1733,10 +1695,10 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                );
                        }
 
-                       *prelude_sentinel = op_append_list(
-                               OP_LINESEQ, *prelude_sentinel,
+                       op_guard_update(prelude_sentinel, op_append_list(
+                               OP_LINESEQ, prelude_sentinel->op,
                                nest
-                       );
+                       ));
                }
 
                /* named parameters */
@@ -1778,7 +1740,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                );
                                var = newASSIGNOP(OPf_STACKED, var, 0, cond);
 
-                               *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, var));
+                               op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
                        }
 
                        for (i = 0, lim = param_spec->named_optional.used; i < lim; i++) {
@@ -1793,8 +1755,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                cond = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
                                cond = newUNOP(OP_EXISTS, 0, cond);
 
-                               cond = newCONDOP(0, cond, var, cur->init);
-                               cur->init = NULL;
+                               cond = newCONDOP(0, cond, var, op_guard_relinquish(&cur->init));
 
                                var = my_var(
                                        aTHX_
@@ -1803,7 +1764,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                );
                                var = newASSIGNOP(OPf_STACKED, var, 0, cond);
 
-                               *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, var));
+                               op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
                        }
 
                        if (!param_spec->slurpy.name) {
@@ -1840,7 +1801,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                        cond = newUNOP(OP_KEYS, 0, my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash));
                                        xcroak = newCONDOP(0, cond, xcroak, NULL);
 
-                                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, xcroak));
+                                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, xcroak)));
                                } else {
                                        OP *clear;
 
@@ -1851,7 +1812,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                                newNULLLIST()
                                        );
 
-                                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, clear));
+                                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, clear)));
                                }
                        } else if (param_spec->slurpy.padoff != param_spec->rest_hash) {
                                OP *var, *clear;
@@ -1867,7 +1828,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
 
                                var = newASSIGNOP(OPf_STACKED, var, 0, my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash));
 
-                               *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, var));
+                               op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
 
                                clear = newASSIGNOP(
                                        OPf_STACKED,
@@ -1876,7 +1837,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                        newNULLLIST()
                                );
 
-                               *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, clear));
+                               op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, clear)));
                        }
                }
 
@@ -1888,7 +1849,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                Param *cur = &param_spec->positional_required.data[i];
 
                                if (cur->type) {
-                                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur)));
+                                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
                                }
                        }
                        base += i;
@@ -1897,7 +1858,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                Param *cur = &param_spec->positional_optional.data[i].param;
 
                                if (cur->type) {
-                                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur)));
+                                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
                                }
                        }
                        base += i;
@@ -1906,7 +1867,7 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                Param *cur = &param_spec->named_required.data[i];
 
                                if (cur->type) {
-                                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur)));
+                                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
                                }
                        }
                        base += i;
@@ -1915,16 +1876,16 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                Param *cur = &param_spec->named_optional.data[i].param;
 
                                if (cur->type) {
-                                       *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur)));
+                                       op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
                                }
                        }
                        base += i;
 
                        if (param_spec->slurpy.type) {
                                /* $type->valid($_) or croak $type->get_message($_) for @rest / values %rest */
-                               OP *body, *list, *loop;
+                               OP *check, *list, *loop;
 
-                               body = mktypecheck(aTHX_ declarator, base, param_spec->slurpy.name, NOT_IN_PAD, param_spec->slurpy.type);
+                               check = mktypecheck(aTHX_ declarator, base, param_spec->slurpy.name, NOT_IN_PAD, param_spec->slurpy.type);
 
                                if (SvPV_nolen(param_spec->slurpy.name)[0] == '@') {
                                        list = my_var_g(aTHX_ OP_PADAV, 0, param_spec->slurpy.padoff);
@@ -1933,9 +1894,9 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                                        list = newUNOP(OP_VALUES, 0, list);
                                }
 
-                               loop = newFOROP(0, NULL, list, body, NULL);
+                               loop = newFOROP(0, NULL, list, check, NULL);
 
-                               *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, loop));
+                               op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, loop)));
                        }
                }
        }
@@ -1946,18 +1907,16 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
        /* add '();' to make function return nothing by default */
        /* (otherwise the invisible parameter initialization can "leak" into
           the return value: fun ($x) {}->("asdf", 0) == 2) */
-       if (*prelude_sentinel) {
+       if (prelude_sentinel->op) {
                body = newSTATEOP(0, NULL, body);
        }
 
-       body = op_append_list(OP_LINESEQ, *prelude_sentinel, body);
-       *prelude_sentinel = NULL;
+       body = op_append_list(OP_LINESEQ, op_guard_relinquish(prelude_sentinel), body);
 
        /* it's go time. */
        {
                CV *cv;
-               OP *const attrs = *attrs_sentinel;
-               *attrs_sentinel = NULL;
+               OP *const attrs = op_guard_relinquish(attrs_sentinel);
 
                SvREFCNT_inc_simple_void(PL_compcv);
 
@@ -1972,7 +1931,9 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
                        body
                );
 
-               register_info(aTHX_ PTR2UV(CvROOT(cv)), declarator, spec, param_spec);
+               if (cv) {
+                       register_info(aTHX_ PTR2UV(CvROOT(cv)), declarator, spec, param_spec);
+               }
 
                if (saw_name) {
                        *pop = newOP(OP_NULL, 0);
@@ -1990,25 +1951,99 @@ static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRL
        }
 }
 
+static int kw_flags_enter(pTHX_ Sentinel sen, const char *kw_ptr, STRLEN kw_len, KWSpec *spec) {
+       HV *hints;
+       SV *sv, **psv;
+       const char *p, *kw_active;
+       STRLEN kw_active_len;
+       bool kw_is_utf8;
+
+       if (!(hints = GvHV(PL_hintgv))) {
+               return FALSE;
+       }
+       if (!(psv = hv_fetchs(hints, HINTK_KEYWORDS, 0))) {
+               return FALSE;
+       }
+       sv = *psv;
+       kw_active = SvPV(sv, kw_active_len);
+       if (kw_active_len <= kw_len) {
+               return FALSE;
+       }
+
+       kw_is_utf8 = lex_bufutf8();
+
+       for (
+               p = kw_active;
+               (p = strchr(p, *kw_ptr)) &&
+               p < kw_active + kw_active_len - kw_len;
+               p++
+       ) {
+               if (
+                       (p == kw_active || p[-1] == ' ') &&
+                       p[kw_len] == ' ' &&
+                       memcmp(kw_ptr, p, kw_len) == 0
+               ) {
+                       ENTER;
+                       SAVETMPS;
+
+                       SAVEDESTRUCTOR_X(sentinel_clear_void, sen);
+
+                       spec->flags = 0;
+                       spec->reify_type = 0;
+                       spec->shift = sentinel_mortalize(sen, newSVpvs(""));
+                       spec->attrs = sentinel_mortalize(sen, newSVpvs(""));
+
+#define FETCH_HINTK_INTO(NAME, PTR, LEN, X) STMT_START { \
+       const char *fk_ptr_; \
+       STRLEN fk_len_; \
+       I32 fk_xlen_; \
+       SV *fk_sv_; \
+       fk_sv_ = sentinel_mortalize(sen, newSVpvs(HINTK_ ## NAME)); \
+       sv_catpvn(fk_sv_, PTR, LEN); \
+       fk_ptr_ = SvPV(fk_sv_, fk_len_); \
+       fk_xlen_ = fk_len_; \
+       if (kw_is_utf8) { \
+               fk_xlen_ = -fk_xlen_; \
+       } \
+       if (!((X) = hv_fetch(hints, fk_ptr_, fk_xlen_, 0))) { \
+               croak("%s: internal error: $^H{'%.*s'} not set", MY_PKG, (int)fk_len_, fk_ptr_); \
+       } \
+} STMT_END
+
+                       FETCH_HINTK_INTO(FLAGS_, kw_ptr, kw_len, psv);
+                       spec->flags = SvIV(*psv);
+
+                       FETCH_HINTK_INTO(REIFY_, kw_ptr, kw_len, psv);
+                       spec->reify_type = SvIV(*psv);
+
+                       FETCH_HINTK_INTO(SHIFT_, kw_ptr, kw_len, psv);
+                       SvSetSV(spec->shift, *psv);
+
+                       FETCH_HINTK_INTO(ATTRS_, kw_ptr, kw_len, psv);
+                       SvSetSV(spec->attrs, *psv);
+
+#undef FETCH_HINTK_INTO
+                       return TRUE;
+               }
+       }
+       return FALSE;
+}
+
 static int my_keyword_plugin(pTHX_ char *keyword_ptr, STRLEN keyword_len, OP **op_ptr) {
+       Sentinel sen = { NULL };
        KWSpec spec;
        int ret;
-       Sentinel sen = { NULL };
-
-       ENTER;
-       SAVETMPS;
 
-       SAVEDESTRUCTOR_X(sentinel_clear_void, sen);
-
-       if (kw_flags(aTHX_ sen, keyword_ptr, keyword_len, &spec)) {
+       if (kw_flags_enter(aTHX_ sen, keyword_ptr, keyword_len, &spec)) {
+               /* scope was entered, 'sen' and 'spec' are initialized */
                ret = parse_fun(aTHX_ sen, op_ptr, keyword_ptr, keyword_len, &spec);
+               FREETMPS;
+               LEAVE;
        } else {
+               /* not one of our keywords, no allocation done */
                ret = next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
        }
 
-       FREETMPS;
-       LEAVE;
-
        return ret;
 }
 
@@ -2021,12 +2056,12 @@ UV
 fp__cv_root(sv)
        SV * sv
        PREINIT:
-               CV *cv;
+               CV *xcv;
                HV *hv;
                GV *gv;
        CODE:
-               cv = sv_2cv(sv, &hv, &gv, 0);
-               RETVAL = PTR2UV(cv ? CvROOT(cv) : NULL);
+               xcv = sv_2cv(sv, &hv, &gv, 0);
+               RETVAL = PTR2UV(xcv ? CvROOT(xcv) : NULL);
        OUTPUT:
                RETVAL
 
@@ -2046,6 +2081,7 @@ WARNINGS_ENABLE {
        newCONSTSUB(stash, "HINTK_FLAGS_",   newSVpvs(HINTK_FLAGS_));
        newCONSTSUB(stash, "HINTK_SHIFT_",   newSVpvs(HINTK_SHIFT_));
        newCONSTSUB(stash, "HINTK_ATTRS_",   newSVpvs(HINTK_ATTRS_));
+       newCONSTSUB(stash, "HINTK_REIFY_",   newSVpvs(HINTK_REIFY_));
        /**/
        next_keyword_plugin = PL_keyword_plugin;
        PL_keyword_plugin = my_keyword_plugin;