FLAG_DEFAULT_ARGS = 0x04,
FLAG_CHECK_NARGS = 0x08,
FLAG_INVOCANT = 0x10,
- FLAG_NAMED_PARAMS = 0x20
+ FLAG_NAMED_PARAMS = 0x20,
+ FLAG_TYPES_OK = 0x40,
+ FLAG_CHECK_TARGS = 0x80
};
DEFSTRUCT(KWSpec) {
static int (*next_keyword_plugin)(pTHX_ char *, STRLEN, OP **);
-static int kw_flags(pTHX_ const char *kw_ptr, STRLEN kw_len, KWSpec *spec) {
+DEFSTRUCT(Resource) {
+ Resource *next;
+ void *data;
+ void (*destroy)(pTHX_ void *);
+};
+
+typedef Resource *Sentinel[1];
+
+static void sentinel_clear_void(pTHX_ void *p) {
+ Resource **pp = p;
+ while (*pp) {
+ Resource *cur = *pp;
+ if (cur->destroy) {
+ cur->destroy(aTHX_ cur->data);
+ }
+ cur->data = (void *)"no";
+ cur->destroy = NULL;
+ *pp = 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;
+}
+
+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 = sv_2mortal(newSVpvs(""));
- spec->attrs = sv_2mortal(newSVpvs(""));
+ spec->shift = sentinel_mortalize(sen, newSVpvs(""));
+ spec->attrs = sentinel_mortalize(sen, newSVpvs(""));
if (!(hints = GvHV(PL_hintgv))) {
return FALSE;
const char *fk_ptr_; \
STRLEN fk_len_; \
SV *fk_sv_; \
- fk_sv_ = sv_2mortal(newSVpvs(HINTK_ ## NAME)); \
+ 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))) { \
}
-static void free_ptr_op(pTHX_ void *vp) {
- OP **pp = vp;
- op_free(*pp);
- Safefree(pp);
+#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 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_ bool allow_package) {
+static SV *my_scan_word(pTHX_ Sentinel sen, bool allow_package) {
bool at_start, at_substart;
I32 c;
- SV *sv = sv_2mortal(newSVpvs(""));
+ SV *sv = sentinel_mortalize(sen, newSVpvs(""));
if (lex_bufutf8()) {
SvUTF8_on(sv);
}
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;
} 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;
}
}
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;
}
return SvCUR(sv) ? sv : NULL;
}
-static SV *my_scan_parens_tail(pTHX_ bool keep_backslash) {
+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 = sv_2mortal(newSVpvs(""));
+ sv = sentinel_mortalize(sen, newSVpvs(""));
if (lex_bufutf8()) {
SvUTF8_on(sv);
}
return sv;
}
-static void my_check_prototype(pTHX_ const SV *declarator, SV *proto) {
+static void my_check_prototype(pTHX_ Sentinel sen, const SV *declarator, SV *proto) {
char *start, *r, *w, *end;
STRLEN len;
/* check for bad characters */
if (strspn(start, "$@%*;[]&\\_+") != len) {
- SV *dsv = newSVpvs_flags("", SVs_TEMP);
+ SV *dsv = sentinel_mortalize(sen, newSVpvs(""));
warner(
packWARN(WARN_ILLEGALPROTO),
"Illegal character in prototype for %"SVf" : %s",
}
}
+static SV *parse_type(pTHX_ Sentinel, const SV *);
+
+static SV *parse_type_paramd(pTHX_ Sentinel sen, const SV *declarator) {
+ I32 c;
+ SV *t;
+
+ t = my_scan_word(aTHX_ sen, TRUE);
+ lex_read_space(0);
+
+ c = lex_peek_unichar(0);
+ if (c == '[') {
+ SV *u;
+
+ lex_read_unichar(0);
+ lex_read_space(0);
+ my_sv_cat_c(aTHX_ t, c);
+
+ u = parse_type(aTHX_ sen, declarator);
+ sv_catsv(t, u);
+
+ c = lex_peek_unichar(0);
+ if (c != ']') {
+ croak("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(pTHX_ Sentinel sen, const SV *declarator) {
+ I32 c;
+ SV *t;
+
+ t = parse_type_paramd(aTHX_ sen, declarator);
+
+ c = lex_peek_unichar(0);
+ while (c == '|') {
+ SV *u;
+
+ lex_read_unichar(0);
+ lex_read_space(0);
+
+ my_sv_cat_c(aTHX_ t, c);
+ u = parse_type_paramd(aTHX_ sen, declarator);
+ sv_catsv(t, u);
+
+ c = lex_peek_unichar(0);
+ }
+
+ return t;
+}
+
+static SV *reify_type(pTHX_ Sentinel sen, const SV *declarator, SV *name) {
+ SV *t;
+ int n;
+ dSP;
+
+ ENTER;
+ SAVETMPS;
+
+ PUSHMARK(SP);
+ EXTEND(SP, 1);
+ PUSHs(name);
+ PUTBACK;
+
+ n = call_pv("Moose::Util::TypeConstraints::find_or_create_isa_type_constraint", 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;
+ FREETMPS;
+ LEAVE;
+
+ if (!SvTRUE(t)) {
+ croak("In %"SVf": undefined type '%"SVf"'", SVfARG(declarator), SVfARG(name));
+ }
+
+ return t;
+}
+
DEFSTRUCT(Param) {
SV *name;
PADOFFSET padoff;
+ SV *type;
};
DEFSTRUCT(ParamInit) {
Param param;
- OP *init;
+ OpGuard init;
};
#define VEC(B) B ## _Vec
VEC(Param) named_required;
VEC(ParamInit) named_optional;
Param slurpy;
+ PADOFFSET rest_hash;
};
DEFVECTOR_INIT(pv_init, Param);
static void p_init(Param *p) {
p->name = NULL;
p->padoff = NOT_IN_PAD;
+ p->type = NULL;
}
static void ps_init(ParamSpec *ps) {
pv_init(&ps->named_required);
piv_init(&ps->named_optional);
p_init(&ps->slurpy);
+ ps->rest_hash = NOT_IN_PAD;
}
#define DEFVECTOR_EXTEND(N, B) static B (*N(VEC(B) *p)) { \
static void p_clear(pTHX_ Param *p) {
p->name = NULL;
p->padoff = NOT_IN_PAD;
+ p->type = NULL;
}
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);
return ps->named_required.used + ps->named_optional.used;
}
+static void my_require(pTHX_ const char *file) {
+ SV *err;
+ require_pv(file);
+ err = ERRSV;
+ if (SvTRUE(err)) {
+ croak_sv(err);
+ }
+}
+
+static SV *my_eval(pTHX_ Sentinel sen, I32 floor, OP *op) {
+ SV *sv;
+ CV *cv;
+ dSP;
+
+ cv = newATTRSUB(floor, NULL, NULL, NULL, op);
+
+ ENTER;
+ SAVETMPS;
+
+ PUSHMARK(SP);
+ call_sv((SV *)cv, G_SCALAR | G_NOARGS);
+ SPAGAIN;
+ sv = sentinel_mortalize(sen, SvREFCNT_inc(POPs));
+
+ PUTBACK;
+ FREETMPS;
+ LEAVE;
+
+ return sv;
+}
+
enum {
PARAM_INVOCANT = 0x01,
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
+ int *pflags, SV **pname, OpGuard *ginit, SV **ptype
) {
I32 c;
char sigil;
SV *name;
- assert(!*pinit);
+ assert(!ginit->op);
*pflags = 0;
+ *ptype = NULL;
c = lex_peek_unichar(0);
+ if (spec->flags & FLAG_TYPES_OK) {
+ if (c == '(') {
+ I32 floor;
+ OP *expr;
+ Resource *expr_sentinel;
+
+ lex_read_unichar(0);
+
+ 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));
+ }
+ 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);
+ if (c != ')') {
+ croak("In %"SVf": missing ')' after type expression", SVfARG(declarator));
+ }
+ 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);
+ *ptype = reify_type(aTHX_ sen, declarator, *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_UNI_IDFIRST(c)) {
+ *ptype = parse_type(aTHX_ sen, declarator);
+ my_require(aTHX_ "Moose/Util/TypeConstraints.pm");
+ *ptype = reify_type(aTHX_ sen, declarator, *ptype);
+
+ c = lex_peek_unichar(0);
+ }
+ }
+
if (c == ':') {
lex_read_unichar(0);
lex_read_space(0);
if (c == -1) {
croak("In %"SVf": unterminated parameter list", SVfARG(declarator));
}
+
if (!(c == '$' || c == '@' || c == '%')) {
croak("In %"SVf": unexpected '%c' in parameter list (expecting a sigil)", SVfARG(declarator), (int)c);
}
lex_read_unichar(0);
lex_read_space(0);
- if (!(name = my_scan_word(aTHX_ FALSE))) {
+ if (!(name = my_scan_word(aTHX_ sen, FALSE))) {
croak("In %"SVf": missing identifier after '%c'", SVfARG(declarator), sigil);
}
sv_insert(name, 0, 0, &sigil, 1);
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);
return my_var_g(aTHX_ OP_PADSV, flags, padoff);
}
-static SV *mkbits1(pTHX_ size_t n) {
- size_t bytes = n / 8, bits = n % 8;
- SV *sv = newSV(bytes + !!bits);
- char *p = SvPVX(sv), *q = p;
- while (bytes--) {
- *p++ = '\xff';
+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 *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, *err, *msg, *xcroak;
+
+ err = mkconstsv(aTHX_ newSVpvf("In %"SVf": parameter %d (%"SVf"): ", SVfARG(declarator), nr, SVfARG(name)));
+ {
+ OP *args = NULL;
+
+ args = op_append_elem(OP_LIST, args, mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(type)));
+ args = op_append_elem(
+ OP_LIST, args,
+ padoff == NOT_IN_PAD
+ ? S_newDEFSVOP(aTHX)
+ : my_var(aTHX_ 0, padoff)
+ );
+ args = op_append_elem(OP_LIST, args, newUNOP(OP_METHOD, 0, mkconstpvs("get_message")));
+
+ msg = args;
+ msg->op_type = OP_ENTERSUB;
+ msg->op_ppaddr = PL_ppaddr[OP_ENTERSUB];
+ msg->op_flags |= OPf_STACKED;
}
- if (bits) {
- *p++ = (1u << bits) - 1;
+
+ msg = newBINOP(OP_CONCAT, 0, err, msg);
+
+ xcroak = newCVREF(
+ OPf_WANT_SCALAR,
+ newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
+ );
+ xcroak = newUNOP(OP_ENTERSUB, OPf_STACKED, op_append_elem(OP_LIST, msg, xcroak));
+
+ {
+ OP *args = NULL;
+
+ args = op_append_elem(OP_LIST, args, mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(type)));
+ args = op_append_elem(
+ OP_LIST, args,
+ padoff == NOT_IN_PAD
+ ? S_newDEFSVOP(aTHX)
+ : my_var(aTHX_ 0, padoff)
+ );
+ args = op_append_elem(OP_LIST, args, newUNOP(OP_METHOD, 0, mkconstpvs("check")));
+
+ chk = args;
+ chk->op_type = OP_ENTERSUB;
+ chk->op_ppaddr = PL_ppaddr[OP_ENTERSUB];
+ chk->op_flags |= OPf_STACKED;
}
- *p = '\0';
- SvCUR_set(sv, p - q);
- SvPOK_on(sv);
- return sv;
+
+ chk = newLOGOP(OP_OR, 0, chk, xcroak);
+ return chk;
+}
+
+static OP *mktypecheckp(pTHX_ const SV *declarator, int nr, const Param *param) {
+ return mktypecheck(aTHX_ declarator, nr, param->name, param->padoff, param->type);
}
-static OP *mkvecbits(pTHX_ PADOFFSET padoff, size_t i) {
- OP *first, *mid, *last, *vec;
+static void register_info(pTHX_ UV key, SV *declarator, const KWSpec *kws, const ParamSpec *ps) {
+ dSP;
+
+ ENTER;
+ SAVETMPS;
+
+ PUSHMARK(SP);
+ EXTEND(SP, 10);
+
+ /* 0 */ {
+ mPUSHu(key);
+ }
+ /* 1 */ {
+ size_t n;
+ char *p = SvPV(declarator, n);
+ char *q = memchr(p, ' ', n);
+ mPUSHp(p, q ? (size_t)(q - p) : n);
+ }
+ if (!ps) {
+ if (SvTRUE(kws->shift)) {
+ PUSHs(kws->shift);
+ } else {
+ PUSHmortal;
+ }
+ PUSHmortal;
+ mPUSHs(newRV_noinc((SV *)newAV()));
+ mPUSHs(newRV_noinc((SV *)newAV()));
+ mPUSHs(newRV_noinc((SV *)newAV()));
+ mPUSHs(newRV_noinc((SV *)newAV()));
+ mPUSHp("@_", 2);
+ PUSHmortal;
+ } else {
+ /* 2, 3 */ {
+ if (ps->invocant.name) {
+ PUSHs(ps->invocant.name);
+ if (ps->invocant.type) {
+ PUSHs(ps->invocant.type);
+ } else {
+ PUSHmortal;
+ }
+ } else {
+ PUSHmortal;
+ PUSHmortal;
+ }
+ }
+ /* 4 */ {
+ size_t i, lim;
+ AV *av;
+
+ lim = ps->positional_required.used;
- last = newSVOP(OP_CONST, 0, newSViv(1));
- first = last;
+ av = newAV();
+ if (lim) {
+ av_extend(av, (lim - 1) * 2);
+ for (i = 0; i < lim; i++) {
+ Param *cur = &ps->positional_required.data[i];
+ av_push(av, SvREFCNT_inc_simple_NN(cur->name));
+ av_push(av, cur->type ? SvREFCNT_inc_simple_NN(cur->type) : &PL_sv_undef);
+ }
+ }
- mid = newSVOP(OP_CONST, 0, newSViv(i));
- mid->op_sibling = first;
- first = mid;
+ mPUSHs(newRV_noinc((SV *)av));
+ }
+ /* 5 */ {
+ size_t i, lim;
+ AV *av;
- mid = my_var(aTHX_ 0, padoff);
- mid->op_sibling = first;
+ lim = ps->positional_optional.used;
- first = newOP(OP_PUSHMARK, 0);
+ av = newAV();
+ if (lim) {
+ av_extend(av, (lim - 1) * 2);
+ for (i = 0; i < lim; i++) {
+ Param *cur = &ps->positional_optional.data[i].param;
+ av_push(av, SvREFCNT_inc_simple_NN(cur->name));
+ av_push(av, cur->type ? SvREFCNT_inc_simple_NN(cur->type) : &PL_sv_undef);
+ }
+ }
- vec = newLISTOP(OP_VEC, 0, first, mid);
- vec->op_targ = pad_alloc(OP_VEC, SVs_PADTMP);
- ((LISTOP *)vec)->op_last = last;
- op_null(((LISTOP *)vec)->op_first);
+ mPUSHs(newRV_noinc((SV *)av));
+ }
+ /* 6 */ {
+ size_t i, lim;
+ AV *av;
- return vec;
-}
+ lim = ps->named_required.used;
-static OP *mkargselem(pTHX_ OP *index) {
- OP *args = newAVREF(newGVOP(OP_GV, 0, PL_defgv));
- args->op_flags |= OPf_REF;
+ av = newAV();
+ if (lim) {
+ av_extend(av, (lim - 1) * 2);
+ for (i = 0; i < lim; i++) {
+ Param *cur = &ps->named_required.data[i];
+ av_push(av, SvREFCNT_inc_simple_NN(cur->name));
+ av_push(av, cur->type ? SvREFCNT_inc_simple_NN(cur->type) : &PL_sv_undef);
+ }
+ }
- return newBINOP(OP_AELEM, 0, args, index);
-}
+ mPUSHs(newRV_noinc((SV *)av));
+ }
+ /* 7 */ {
+ size_t i, lim;
+ AV *av;
+
+ lim = ps->named_optional.used;
+
+ av = newAV();
+ if (lim) {
+ av_extend(av, (lim - 1) * 2);
+ for (i = 0; i < lim; i++) {
+ Param *cur = &ps->named_optional.data[i].param;
+ av_push(av, SvREFCNT_inc_simple_NN(cur->name));
+ av_push(av, cur->type ? SvREFCNT_inc_simple_NN(cur->type) : &PL_sv_undef);
+ }
+ }
+
+ mPUSHs(newRV_noinc((SV *)av));
+ }
+ /* 8, 9 */ {
+ if (ps->slurpy.name) {
+ PUSHs(ps->slurpy.name);
+ if (ps->slurpy.type) {
+ PUSHs(ps->slurpy.type);
+ } else {
+ PUSHmortal;
+ }
+ } else {
+ PUSHmortal;
+ PUSHmortal;
+ }
+ }
+ }
+ PUTBACK;
+
+ call_pv(MY_PKG "::_register_info", G_VOID);
-static OP *mkargselemv(pTHX_ PADOFFSET v) {
- return mkargselem(aTHX_ my_var(aTHX_ 0, v));
+ FREETMPS;
+ LEAVE;
}
-static int parse_fun(pTHX_ OP **pop, const char *keyword_ptr, STRLEN keyword_len, const KWSpec *spec) {
+static int parse_fun(pTHX_ Sentinel sen, OP **pop, const char *keyword_ptr, STRLEN keyword_len, const KWSpec *spec) {
ParamSpec *param_spec;
SV *declarator;
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 = sv_2mortal(newSVpvn(keyword_ptr, keyword_len));
+ declarator = sentinel_mortalize(sen, newSVpvn(keyword_ptr, keyword_len));
lex_read_space(0);
/* function name */
saw_name = NULL;
- if ((spec->flags & FLAG_NAME_OK) && (saw_name = my_scan_word(aTHX_ TRUE))) {
+ if ((spec->flags & FLAG_NAME_OK) && (saw_name = my_scan_word(aTHX_ sen, TRUE))) {
if (PL_parser->expect != XSTATE) {
/* bail out early so we don't predeclare $saw_name */
save_ix = S_block_start(aTHX_ TRUE);
/* initialize synthetic optree */
- Newx(prelude_sentinel, 1, OP *);
- *prelude_sentinel = NULL;
- SAVEDESTRUCTOR_X(free_ptr_op, prelude_sentinel);
+ 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;
- SAVEDESTRUCTOR_X(free_ptr_op, init_sentinel);
+ 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);
- SAVEDESTRUCTOR_X(ps_free_void, param_spec);
+ sentinel_register(sen, param_spec, ps_free_void);
lex_read_unichar(0);
lex_read_space(0);
while ((c = lex_peek_unichar(0)) != ')') {
int flags;
- SV *name;
+ SV *name, *type;
char sigil;
PADOFFSET padoff;
- padoff = parse_param(aTHX_ declarator, spec, param_spec, &flags, &name, init_sentinel);
+ padoff = parse_param(aTHX_ sen, declarator, spec, param_spec, &flags, &name, init_sentinel, &type);
S_intro_my(aTHX);
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));
}
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;
continue;
}
}
param_spec->invocant.name = name;
param_spec->invocant.padoff = padoff;
+ param_spec->invocant.type = type;
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));
}
croak("In %"SVf": named parameter :%"SVf" not allowed here", SVfARG(declarator), SVfARG(name));
}
- if (*init_sentinel) {
+ if (init_sentinel->op) {
ParamInit *pi = piv_extend(¶m_spec->named_optional);
pi->param.name = name;
pi->param.padoff = padoff;
- pi->init = *init_sentinel;
- *init_sentinel = NULL;
+ pi->param.type = type;
+ 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(¶m_spec->named_required);
+ p = pv_extend(¶m_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(¶m_spec->positional_optional);
pi->param.name = name;
pi->param.padoff = padoff;
- pi->init = *init_sentinel;
- *init_sentinel = NULL;
+ pi->param.type = type;
+ pi->init = op_guard_transfer(init_sentinel);
param_spec->positional_optional.used++;
} else {
Param *p = pv_extend(¶m_spec->positional_required);
p->name = name;
p->padoff = padoff;
+ p->type = type;
param_spec->positional_required.used++;
}
}
}
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)) {
c = ':';
} else {
lex_read_unichar(0);
- if (!(proto = my_scan_parens_tail(aTHX_ FALSE))) {
+ if (!(proto = my_scan_parens_tail(aTHX_ sen, FALSE))) {
croak("In %"SVf": prototype not terminated", SVfARG(declarator));
}
- my_check_prototype(aTHX_ declarator, proto);
+ my_check_prototype(aTHX_ sen, declarator, proto);
lex_read_space(0);
c = lex_peek_unichar(0);
+ if (!(c == ':' || c == '{')) {
+ lex_stuff_pvs(":", 0);
+ c = ':';
+ }
}
}
/* attributes */
- Newx(attrs_sentinel, 1, OP *);
- *attrs_sentinel = NULL;
- SAVEDESTRUCTOR_X(free_ptr_op, attrs_sentinel);
+ Newx(attrs_sentinel, 1, OpGuard);
+ op_guard_init(attrs_sentinel);
+ sentinel_register(sen, attrs_sentinel, free_op_guard_void);
if (c == ':' || c == '{') /* '}' - hi, vim */ {
for (;;) {
SV *attr;
- if (!(attr = my_scan_word(aTHX_ FALSE))) {
+ if (!(attr = my_scan_word(aTHX_ sen, FALSE))) {
break;
}
} else {
SV *sv;
lex_read_unichar(0);
- if (!(sv = my_scan_parens_tail(aTHX_ TRUE))) {
+ if (!(sv = my_scan_parens_tail(aTHX_ sen, TRUE))) {
croak("In %"SVf": unterminated attribute parameter in attribute list", SVfARG(declarator));
}
sv_catpvs(attr, "(");
}
if (attr) {
- *attrs_sentinel = op_append_elem(OP_LIST, *attrs_sentinel, newSVOP(OP_CONST, 0, 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 == ':') {
newATTRSUB(
sub_ix,
- newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(saw_name)),
- proto ? newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(proto)) : NULL,
+ mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(saw_name)),
+ proto ? mkconstsv(aTHX_ SvREFCNT_inc_simple_NN(proto)) : NULL,
NULL,
NULL
);
/* 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) {
- OP *chk, *cond, *err, *croak;
-
- err = newSVOP(OP_CONST, 0,
- newSVpvf("Not enough arguments for %"SVf" (expected %d, got ", SVfARG(declarator), amin));
- err = newBINOP(OP_CONCAT, 0,
- err,
- newAVREF(newGVOP(OP_GV, 0, PL_defgv)));
- err = newBINOP(OP_CONCAT, 0,
- err,
- newSVOP(OP_CONST, 0, newSVpvs(")")));
-
- croak = newCVREF(OPf_WANT_SCALAR,
- newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV)));
+ OP *chk, *cond, *err, *xcroak;
+
+ err = mkconstsv(aTHX_ newSVpvf("Not enough arguments for %"SVf" (expected %d, got ", SVfARG(declarator), amin));
+ err = newBINOP(
+ OP_CONCAT, 0,
+ err,
+ newAVREF(newGVOP(OP_GV, 0, PL_defgv))
+ );
+ err = newBINOP(
+ OP_CONCAT, 0,
+ err,
+ mkconstpvs(")")
+ );
+
+ xcroak = newCVREF(OPf_WANT_SCALAR,
+ newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV)));
err = newUNOP(OP_ENTERSUB, OPf_STACKED,
- op_append_elem(OP_LIST, err, croak));
+ op_append_elem(OP_LIST, err, xcroak));
cond = newBINOP(OP_LT, 0,
- newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
- newSVOP(OP_CONST, 0, newSViv(amin)));
+ newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
+ 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);
if (amax >= 0) {
- OP *chk, *cond, *err, *croak;
-
- err = newSVOP(OP_CONST, 0,
- newSVpvf("Too many arguments for %"SVf" (expected %d, got ", SVfARG(declarator), amax));
- err = newBINOP(OP_CONCAT, 0,
- err,
- newAVREF(newGVOP(OP_GV, 0, PL_defgv)));
- err = newBINOP(OP_CONCAT, 0,
- err,
- newSVOP(OP_CONST, 0, newSVpvs(")")));
-
- croak = newCVREF(OPf_WANT_SCALAR,
- newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV)));
+ OP *chk, *cond, *err, *xcroak;
+
+ err = mkconstsv(aTHX_ newSVpvf("Too many arguments for %"SVf" (expected %d, got ", SVfARG(declarator), amax));
+ err = newBINOP(
+ OP_CONCAT, 0,
+ err,
+ newAVREF(newGVOP(OP_GV, 0, PL_defgv))
+ );
+ err = newBINOP(
+ OP_CONCAT, 0,
+ err,
+ mkconstpvs(")")
+ );
+
+ xcroak = newCVREF(
+ OPf_WANT_SCALAR,
+ newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
+ );
err = newUNOP(OP_ENTERSUB, OPf_STACKED,
- op_append_elem(OP_LIST, err, croak));
+ op_append_elem(OP_LIST, err, xcroak));
- cond = newBINOP(OP_GT, 0,
- newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
- newSVOP(OP_CONST, 0, newSViv(amax)));
+ cond = newBINOP(
+ OP_GT, 0,
+ newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
+ mkconstiv(aTHX_ amax)
+ );
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] == '%'))) {
- OP *chk, *cond, *err, *croak;
+ OP *chk, *cond, *err, *xcroak;
const UV fixed = count_positional_params(param_spec) + !!param_spec->invocant.name;
- err = newSVOP(OP_CONST, 0,
- newSVpvf("Odd number of paired arguments for %"SVf"", SVfARG(declarator)));
+ err = mkconstsv(aTHX_ newSVpvf("Odd number of paired arguments for %"SVf"", SVfARG(declarator)));
- croak = newCVREF(OPf_WANT_SCALAR,
- newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV)));
+ xcroak = newCVREF(
+ OPf_WANT_SCALAR,
+ newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
+ );
err = newUNOP(OP_ENTERSUB, OPf_STACKED,
- op_append_elem(OP_LIST, err, croak));
+ op_append_elem(OP_LIST, err, xcroak));
cond = newBINOP(OP_GT, 0,
- fixed
- ? newBINOP(OP_SUBTRACT, 0,
- newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
- newSVOP(OP_CONST, 0, newSVuv(fixed)))
- : newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
- newSVOP(OP_CONST, 0, newSViv(0)));
+ newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
+ mkconstiv(aTHX_ fixed));
cond = newLOGOP(OP_AND, 0,
- cond,
- newBINOP(OP_MODULO, 0,
- fixed
- ? newBINOP(OP_SUBTRACT, 0,
- newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
- newSVOP(OP_CONST, 0, newSVuv(fixed)))
- : newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
- newSVOP(OP_CONST, 0, newSViv(2))));
+ cond,
+ newBINOP(OP_MODULO, 0,
+ fixed
+ ? newBINOP(OP_SUBTRACT, 0,
+ newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
+ mkconstiv(aTHX_ fixed))
+ : newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
+ 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)));
}
}
);
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; */
);
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)) {
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, 0, ¶m_spec->invocant))));
+ }
}
/* my (...) = @_; */
lhs = op_append_elem(OP_LIST, lhs, var);
}
- if (param_spec->slurpy.name) {
- if (count_named_params(param_spec)) {
- OP *const var = my_var_g(
- aTHX_
- SvPV_nolen(param_spec->slurpy.name)[0] == '@' ? OP_PADAV : OP_PADHV,
- OPf_MOD | (OPpLVAL_INTRO << 8),
- param_spec->slurpy.padoff
- );
- *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, newSTATEOP(0, NULL, var));
+ {
+ PADOFFSET padoff;
+ I32 type;
+ bool slurpy_hash;
+
+ /*
+ * cases:
+ * 1) no named params
+ * 1.1) slurpy
+ * => put it in
+ * 1.2) no slurpy
+ * => nop
+ * 2) named params
+ * 2.1) no slurpy
+ * => synthetic %{rest}
+ * 2.2) slurpy is a hash
+ * => put it in
+ * 2.3) slurpy is an array
+ * => synthetic %{rest}
+ * remember to declare array later
+ */
+
+ slurpy_hash = param_spec->slurpy.name && SvPV_nolen(param_spec->slurpy.name)[0] == '%';
+ if (!count_named_params(param_spec)) {
+ if (param_spec->slurpy.name) {
+ padoff = param_spec->slurpy.padoff;
+ type = slurpy_hash ? OP_PADHV : OP_PADAV;
+ } else {
+ padoff = NOT_IN_PAD;
+ type = OP_PADSV;
+ }
+ } else if (slurpy_hash) {
+ padoff = param_spec->slurpy.padoff;
+ type = OP_PADHV;
} else {
+ padoff = param_spec->rest_hash = pad_add_name_pvs("%{rest}", 0, NULL, NULL);
+ type = OP_PADHV;
+ }
+
+ if (padoff != NOT_IN_PAD) {
OP *const var = my_var_g(
aTHX_
- SvPV_nolen(param_spec->slurpy.name)[0] == '@' ? OP_PADAV : OP_PADHV,
+ type,
OPf_WANT_LIST | (OPpLVAL_INTRO << 8),
- param_spec->slurpy.padoff
+ padoff
);
+
lhs = op_append_elem(OP_LIST, lhs, var);
+
+ if (type == OP_PADHV) {
+ param_spec->rest_hash = padoff;
+ }
}
}
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)
)
- );
+ ));
}
}
- /* default arguments */
+ /* default positional arguments */
{
size_t i, lim, req;
OP *nest;
cond = newBINOP(
OP_LT, 0,
newAVREF(newGVOP(OP_GV, 0, PL_defgv)),
- newSVOP(OP_CONST, 0, newSViv(req + i + 1))
+ mkconstiv(aTHX_ req + i + 1)
);
var = my_var(aTHX_ 0, cur->param.padoff);
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,
);
}
- *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 */
if (count_named_params(param_spec)) {
- int nameblock_ix;
- OP *nameblock;
- PADOFFSET vb, vc, vi, vk;
- int vb_is_str, vc_is_str;
- const size_t pos = count_positional_params(param_spec);
+ size_t i, lim;
- nameblock = NULL;
- nameblock_ix = S_block_start(aTHX_ TRUE);
+ assert(param_spec->rest_hash != NOT_IN_PAD);
- {
- OP *decl, *var;
+ for (i = 0, lim = param_spec->named_required.used; i < lim; i++) {
+ Param *cur = ¶m_spec->named_required.data[i];
+ size_t n;
+ char *p = SvPV(cur->name, n);
+ OP *var, *cond;
- decl = NULL;
+ cond = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
- vb_is_str = param_spec->named_required.used > UV_BITS;
- if (!param_spec->named_required.used || !(spec->flags & FLAG_CHECK_NARGS)) {
- vb = 0;
- } else {
- var = newOP(OP_PADSV, OPf_MOD | (OPpLVAL_INTRO << 8));
- var->op_targ = vb = pad_add_name_pvs("$__B", 0, NULL, NULL);
- var = newASSIGNOP(OPf_STACKED, var, 0, newSVOP(OP_CONST, 0, vb_is_str ? newSVpvs("") : newSVuv(0)));
- decl = op_append_list(OP_LINESEQ, decl, newSTATEOP(0, NULL, var));
- }
+ if (spec->flags & FLAG_CHECK_NARGS) {
+ OP *xcroak, *msg;
- vc_is_str = param_spec->named_optional.used > UV_BITS;
- if (!param_spec->named_optional.used) {
- vc = 0;
- } else {
- var = newOP(OP_PADSV, OPf_MOD | (OPpLVAL_INTRO << 8));
- var->op_targ = vc = pad_add_name_pvs("$__C", 0, NULL, NULL);
- var = newASSIGNOP(OPf_STACKED, var, 0, newSVOP(OP_CONST, 0, vc_is_str ? newSVpvs("") : newSVuv(0)));
- decl = op_append_list(OP_LINESEQ, decl, newSTATEOP(0, NULL, var));
- }
+ var = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
+ var = newUNOP(OP_DELETE, 0, var);
+
+ msg = mkconstsv(aTHX_ newSVpvf("In %"SVf": missing named parameter: %.*s", SVfARG(declarator), (int)(n - 1), p + 1));
+ xcroak = newCVREF(
+ OPf_WANT_SCALAR,
+ newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
+ );
+ xcroak = newUNOP(OP_ENTERSUB, OPf_STACKED, op_append_elem(OP_LIST, msg, xcroak));
+
+ cond = newUNOP(OP_EXISTS, 0, cond);
- var = newOP(OP_PADSV, OPf_MOD | (OPpLVAL_INTRO << 8));
- var->op_targ = vk = pad_add_name_pvs("$__K", 0, NULL, NULL);
- decl = op_append_list(OP_LINESEQ, decl, newSTATEOP(0, NULL, var));
+ cond = newCONDOP(0, cond, var, xcroak);
+ }
- var = newOP(OP_PADSV, OPf_MOD | (OPpLVAL_INTRO << 8));
- var->op_targ = vi = pad_add_name_pvs("$__I", 0, NULL, NULL);
- var = newASSIGNOP(OPf_STACKED, var, 0, newSVOP(OP_CONST, 0, newSViv(pos)));
- decl = op_append_list(OP_LINESEQ, decl, newSTATEOP(0, NULL, var));
+ var = my_var(
+ aTHX_
+ OPf_MOD | (OPpLVAL_INTRO << 8),
+ cur->padoff
+ );
+ var = newASSIGNOP(OPf_STACKED, var, 0, cond);
- //S_intro_my(aTHX);
- nameblock = op_append_list(OP_LINESEQ, nameblock, decl);
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
}
- {
- OP *loop;
+ for (i = 0, lim = param_spec->named_optional.used; i < lim; i++) {
+ ParamInit *cur = ¶m_spec->named_optional.data[i];
+ size_t n;
+ char *p = SvPV(cur->param.name, n);
+ OP *var, *cond;
- loop = NULL;
+ var = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
+ var = newUNOP(OP_DELETE, 0, var);
- loop = op_append_list(
- OP_LINESEQ,
- loop,
- newSTATEOP(
- 0, NULL,
- newASSIGNOP(
- OPf_STACKED,
- my_var(aTHX_ 0, vk),
- 0,
- mkargselemv(aTHX_ vi)
- )
- )
+ cond = mkhvelem(aTHX_ param_spec->rest_hash, mkconstpv(aTHX_ p + 1, n - 1));
+ cond = newUNOP(OP_EXISTS, 0, cond);
+
+ cond = newCONDOP(0, cond, var, op_guard_relinquish(&cur->init));
+
+ var = my_var(
+ aTHX_
+ OPf_MOD | (OPpLVAL_INTRO << 8),
+ cur->param.padoff
);
+ var = newASSIGNOP(OPf_STACKED, var, 0, cond);
- {
- OP *nest;
- size_t i;
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
+ }
- if (param_spec->slurpy.name) {
- if (SvPV_nolen(param_spec->slurpy.name)[0] == '@') {
- OP *first, *mid, *last;
-
- last = mkargselem(
- aTHX_
- newBINOP(
- OP_ADD, 0,
- my_var(aTHX_ 0, vi),
- newSVOP(OP_CONST, 0, newSViv(1))
- )
- );
- mid = last;
-
- first = my_var(aTHX_ 0, vk);
- first->op_sibling = mid;
- mid = first;
-
- first = my_var_g(aTHX_ OP_PADAV, 0, param_spec->slurpy.padoff);
- first->op_sibling = mid;
- mid = first;
-
- first = newOP(OP_PUSHMARK, 0);
- nest = newLISTOP(OP_PUSH, 0, first, mid);
- nest->op_targ = pad_alloc(OP_PUSH, SVs_PADTMP);
- ((LISTOP *)nest)->op_last = last;
- } else {
- nest = newASSIGNOP(
- OPf_STACKED,
- newBINOP(
- OP_HELEM, 0,
- my_var_g(aTHX_ OP_PADHV, 0, param_spec->slurpy.padoff),
- my_var(aTHX_ 0, vk)
- ),
- 0,
- mkargselem(
- aTHX_
- newBINOP(
- OP_ADD, 0,
- my_var(aTHX_ 0, vi),
- newSVOP(OP_CONST, 0, newSViv(1))
- )
- )
- );
- }
- } else if (spec->flags & FLAG_CHECK_NARGS) {
- OP *err, *croak;
-
- err = newSVOP(OP_CONST, 0,
- newSVpvf("In %"SVf": no such named parameter: ", SVfARG(declarator)));
- err = newBINOP(
- OP_CONCAT, 0,
- err,
- my_var(aTHX_ 0, vk)
- );
-
- croak = newCVREF(OPf_WANT_SCALAR,
- newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV)));
- nest = newUNOP(OP_ENTERSUB, OPf_STACKED,
- op_append_elem(OP_LIST, err, croak));
- } else {
- nest = NULL;
- }
+ if (!param_spec->slurpy.name) {
+ if (spec->flags & FLAG_CHECK_NARGS) {
+ /* croak if %{rest} */
+ OP *xcroak, *cond, *keys, *msg;
- for (i = param_spec->named_optional.used; i--; ) {
- Param *cur = ¶m_spec->named_optional.data[i].param;
- size_t dn;
- char *dp = SvPV(cur->name, dn);
- OP *vec;
-
- if (!(spec->flags & FLAG_CHECK_NARGS)) {
- vec = NULL;
- } else if (vc_is_str) {
- vec = newASSIGNOP(
- OPf_STACKED,
- mkvecbits(aTHX_ vc, i),
- 0,
- newSVOP(OP_CONST, 0, newSViv(1))
- );
- } else {
- vec = newASSIGNOP(
- OPf_STACKED,
- my_var(aTHX_ 0, vc),
- OP_BIT_OR,
- newSVOP(OP_CONST, 0, newSVuv((UV)1 << i))
- );
- }
+ keys = newUNOP(OP_KEYS, 0, my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash));
+ keys = newLISTOP(OP_SORT, 0, newOP(OP_PUSHMARK, 0), keys);
+ {
+ OP *first, *mid, *last;
- nest = newCONDOP(
- 0,
- newBINOP(
- OP_SEQ, 0,
- my_var(aTHX_ 0, vk),
- newSVOP(OP_CONST, 0, newSVpvn_utf8(dp + 1, dn - 1, SvUTF8(cur->name)))
- ),
- op_append_list(
- OP_LINESEQ,
- newASSIGNOP(
- OPf_STACKED,
- my_var(aTHX_ 0, cur->padoff),
- 0,
- mkargselem(
- aTHX_
- newBINOP(
- OP_ADD, 0,
- my_var(aTHX_ 0, vi),
- newSVOP(OP_CONST, 0, newSViv(1))
- )
- )
- ),
- vec
- ),
- nest
- );
- }
+ last = keys;
- for (i = param_spec->named_required.used; i--; ) {
- Param *cur = ¶m_spec->named_required.data[i];
- size_t dn;
- char *dp = SvPV(cur->name, dn);
- OP *vec;
-
- if (!(spec->flags & FLAG_CHECK_NARGS)) {
- vec = NULL;
- } else if (vb_is_str) {
- vec = newASSIGNOP(
- OPf_STACKED,
- mkvecbits(aTHX_ vb, i),
- 0,
- newSVOP(OP_CONST, 0, newSViv(1))
- );
- } else {
- vec = newASSIGNOP(
- OPf_STACKED,
- my_var(aTHX_ 0, vb),
- OP_BIT_OR,
- newSVOP(OP_CONST, 0, newSVuv((UV)1 << i))
- );
- }
+ mid = mkconstpvs(", ");
+ mid->op_sibling = last;
- nest = newCONDOP(
- 0,
- newBINOP(
- OP_SEQ, 0,
- my_var(aTHX_ 0, vk),
- newSVOP(OP_CONST, 0, newSVpvn_utf8(dp + 1, dn - 1, SvUTF8(cur->name)))
- ),
- op_append_list(
- OP_LINESEQ,
- newASSIGNOP(
- OPf_STACKED,
- my_var(aTHX_ 0, cur->padoff),
- 0,
- mkargselem(
- aTHX_
- newBINOP(
- OP_ADD, 0,
- my_var(aTHX_ 0, vi),
- newSVOP(OP_CONST, 0, newSViv(1))
- )
- )
- ),
- vec
- ),
- nest
- );
- }
+ first = newOP(OP_PUSHMARK, 0);
- loop = op_append_elem(OP_LINESEQ, loop, newSTATEOP(0, NULL, nest));
- }
+ keys = newLISTOP(OP_JOIN, 0, first, mid);
+ keys->op_targ = pad_alloc(OP_JOIN, SVs_PADTMP);
+ ((LISTOP *)keys)->op_last = last;
+ }
- loop = newWHILEOP(
- 0, 1,
- NULL,
- newBINOP(
- OP_LT, 0,
- my_var(aTHX_ 0, vi),
- newAVREF(newGVOP(OP_GV, 0, PL_defgv))
- ),
- loop,
- newASSIGNOP(
- OPf_STACKED,
- my_var(aTHX_ 0, vi),
- OP_ADD,
- newSVOP(OP_CONST, 0, newSViv(2))
- ),
- 0
- );
+ msg = mkconstsv(aTHX_ newSVpvf("In %"SVf": no such named parameter: ", SVfARG(declarator)));
+ msg = newBINOP(OP_CONCAT, 0, msg, keys);
- nameblock = op_append_list(OP_LINESEQ, nameblock, newSTATEOP(0, NULL, loop));
- }
+ xcroak = newCVREF(
+ OPf_WANT_SCALAR,
+ newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
+ );
+ xcroak = newUNOP(OP_ENTERSUB, OPf_STACKED, op_append_elem(OP_LIST, msg, xcroak));
- if (param_spec->named_required.used && (spec->flags & FLAG_CHECK_NARGS)) {
- OP *cond, *err, *croak, *join;
-
- {
- size_t i, lim;
- OP *first, *mid, *last;
-
- last = newNULLLIST();
- mid = last;
-
- for (i = param_spec->named_required.used; i--; ) {
- OP *cur;
- SV *sv = param_spec->named_required.data[i].name;
- size_t n;
- char *p = SvPV(sv, n);
- cur = newCONDOP(
- 0,
- vb_is_str
- ? mkvecbits(aTHX_ vb, i)
- : newBINOP(OP_BIT_AND, 0, my_var(aTHX_ 0, vb), newSVOP(OP_CONST, 0, newSVuv((UV)1 << i)))
- ,
- newNULLLIST(),
- newSVOP(OP_CONST, 0, newSVpvn_utf8(p + 1, n - 1, SvUTF8(sv)))
- );
- cur->op_sibling = mid;
- mid = cur;
- }
+ cond = newUNOP(OP_KEYS, 0, my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash));
+ xcroak = newCONDOP(0, cond, xcroak, NULL);
- first = newSVOP(OP_CONST, 0, newSVpvs(", "));
- first->op_sibling = mid;
- mid = first;
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, xcroak)));
+ } else {
+ OP *clear;
- first = newOP(OP_PUSHMARK, 0);
+ clear = newASSIGNOP(
+ OPf_STACKED,
+ my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash),
+ 0,
+ newNULLLIST()
+ );
- join = newLISTOP(OP_JOIN, 0, first, mid);
- join->op_targ = pad_alloc(OP_JOIN, SVs_PADTMP);
- ((LISTOP *)join)->op_last = last;
+ 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;
- err = newSVOP(
- OP_CONST, 0,
- newSVpvf("In %"SVf": missing named parameter(s): ", SVfARG(declarator))
- );
- err = newBINOP(OP_CONCAT, 0, err, join);
- croak = newCVREF(
- OPf_WANT_SCALAR,
- newGVOP(OP_GV, 0, gv_fetchpvs("Carp::croak", 0, SVt_PVCV))
+ assert(SvPV_nolen(param_spec->slurpy.name)[0] == '@');
+
+ var = my_var_g(
+ aTHX_
+ OP_PADAV,
+ OPf_MOD | (OPpLVAL_INTRO << 8),
+ param_spec->slurpy.padoff
);
- err = newUNOP(
- OP_ENTERSUB,
+
+ var = newASSIGNOP(OPf_STACKED, var, 0, my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash));
+
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, var)));
+
+ clear = newASSIGNOP(
OPf_STACKED,
- op_append_elem(OP_LIST, err, croak)
- );
- if (vb_is_str) {
- cond = newBINOP(
- OP_SNE, 0,
- my_var(aTHX_ 0, vb),
- newSVOP(OP_CONST, 0, mkbits1(aTHX_ param_spec->named_required.used))
- );
- } else {
- cond = newBINOP(
- OP_NE, 0,
- my_var(aTHX_ 0, vb),
- newSVOP(
- OP_CONST, 0,
- newSVuv(
- param_spec->named_required.used == UV_BITS
- ? ~(UV)0
- : ((UV)1 << param_spec->named_required.used) - 1
- )
- )
- );
- }
- err = newCONDOP(
+ my_var_g(aTHX_ OP_PADHV, 0, param_spec->rest_hash),
0,
- cond,
- err,
- NULL
+ newNULLLIST()
);
- nameblock = op_append_list(OP_LINESEQ, nameblock, err);
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, clear)));
}
+ }
- if (param_spec->named_optional.used) {
- size_t i, lim;
+ if (spec->flags & FLAG_CHECK_TARGS) {
+ size_t i, lim, base;
- for (i = 0, lim = param_spec->named_optional.used; i < lim; i++) {
- ParamInit *const cur = ¶m_spec->named_optional.data[i];
- OP *init, *cond;
+ base = 1;
+ for (i = 0, lim = param_spec->positional_required.used; i < lim; i++) {
+ Param *cur = ¶m_spec->positional_required.data[i];
- init = newASSIGNOP(
- OPf_STACKED,
- my_var(aTHX_ 0, cur->param.padoff),
- 0,
- cur->init
- );
- cur->init = NULL;
+ if (cur->type) {
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
+ }
+ }
+ base += i;
- cond = newUNOP(
- OP_NOT, OPf_SPECIAL,
- vc_is_str
- ? mkvecbits(aTHX_ vc, i)
- : newBINOP(OP_BIT_AND, 0, my_var(aTHX_ 0, vc), newSVOP(OP_CONST, 0, newSVuv((UV)1 << i)))
- );
+ for (i = 0, lim = param_spec->positional_optional.used; i < lim; i++) {
+ Param *cur = ¶m_spec->positional_optional.data[i].param;
+
+ if (cur->type) {
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
+ }
+ }
+ base += i;
+
+ for (i = 0, lim = param_spec->named_required.used; i < lim; i++) {
+ Param *cur = ¶m_spec->named_required.data[i];
+
+ if (cur->type) {
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
+ }
+ }
+ base += i;
- init = newCONDOP(0, cond, init, NULL);
+ for (i = 0, lim = param_spec->named_optional.used; i < lim; i++) {
+ Param *cur = ¶m_spec->named_optional.data[i].param;
- nameblock = op_append_list(OP_LINESEQ, nameblock, newSTATEOP(0, NULL, init));
+ if (cur->type) {
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, mktypecheckp(aTHX_ declarator, base + i, cur))));
}
}
+ base += i;
- nameblock = S_block_end(aTHX_ nameblock_ix, nameblock);
- nameblock = op_scope(nameblock);
+ if (param_spec->slurpy.type) {
+ /* $type->valid($_) or croak $type->get_message($_) for @rest / values %rest */
+ OP *check, *list, *loop;
- *prelude_sentinel = op_append_list(OP_LINESEQ, *prelude_sentinel, nameblock);
+ 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);
+ } else {
+ list = my_var_g(aTHX_ OP_PADHV, 0, param_spec->slurpy.padoff);
+ list = newUNOP(OP_VALUES, 0, list);
+ }
+
+ loop = newFOROP(0, NULL, list, check, NULL);
+
+ op_guard_update(prelude_sentinel, op_append_list(OP_LINESEQ, prelude_sentinel->op, newSTATEOP(0, NULL, loop)));
+ }
}
}
/* 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. */
{
- OP *const attrs = *attrs_sentinel;
- *attrs_sentinel = NULL;
+ CV *cv;
+ OP *const attrs = op_guard_relinquish(attrs_sentinel);
+
SvREFCNT_inc_simple_void(PL_compcv);
/* close outer block: '}' */
S_block_end(aTHX_ save_ix, body);
- if (!saw_name) {
- *pop = newANONATTRSUB(
- floor_ix,
- proto ? newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(proto)) : NULL,
- attrs,
- body
- );
- return KEYWORD_PLUGIN_EXPR;
- }
-
- newATTRSUB(
+ cv = newATTRSUB(
floor_ix,
- newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(saw_name)),
+ saw_name ? newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(saw_name)) : NULL,
proto ? newSVOP(OP_CONST, 0, SvREFCNT_inc_simple_NN(proto)) : NULL,
attrs,
body
);
- *pop = newOP(OP_NULL, 0);
- return KEYWORD_PLUGIN_STMT;
+
+ if (cv) {
+ register_info(aTHX_ PTR2UV(CvROOT(cv)), declarator, spec, param_spec);
+ }
+
+ if (saw_name) {
+ *pop = newOP(OP_NULL, 0);
+ return KEYWORD_PLUGIN_STMT;
+ }
+
+ *pop = newUNOP(
+ OP_REFGEN, 0,
+ newSVOP(
+ OP_ANONCODE, 0,
+ (SV *)cv
+ )
+ );
+ return KEYWORD_PLUGIN_EXPR;
}
}
static int my_keyword_plugin(pTHX_ char *keyword_ptr, STRLEN keyword_len, OP **op_ptr) {
KWSpec spec;
int ret;
+ Sentinel sen = { NULL };
+ ENTER;
SAVETMPS;
- if (kw_flags(aTHX_ keyword_ptr, keyword_len, &spec)) {
- ret = parse_fun(aTHX_ op_ptr, keyword_ptr, keyword_len, &spec);
+ SAVEDESTRUCTOR_X(sentinel_clear_void, sen);
+
+ if (kw_flags(aTHX_ sen, keyword_ptr, keyword_len, &spec)) {
+ ret = parse_fun(aTHX_ sen, op_ptr, keyword_ptr, keyword_len, &spec);
} else {
ret = next_keyword_plugin(aTHX_ keyword_ptr, keyword_len, op_ptr);
}
FREETMPS;
+ LEAVE;
return ret;
}
WARNINGS_RESET
-MODULE = Function::Parameters PACKAGE = Function::Parameters
+MODULE = Function::Parameters PACKAGE = Function::Parameters PREFIX = fp_
PROTOTYPES: ENABLE
+UV
+fp__cv_root(sv)
+ SV * sv
+ PREINIT:
+ CV *xcv;
+ HV *hv;
+ GV *gv;
+ CODE:
+ xcv = sv_2cv(sv, &hv, &gv, 0);
+ RETVAL = PTR2UV(xcv ? CvROOT(xcv) : NULL);
+ OUTPUT:
+ RETVAL
+
BOOT:
WARNINGS_ENABLE {
HV *const stash = gv_stashpvs(MY_PKG, GV_ADD);
newCONSTSUB(stash, "FLAG_CHECK_NARGS", newSViv(FLAG_CHECK_NARGS));
newCONSTSUB(stash, "FLAG_INVOCANT", newSViv(FLAG_INVOCANT));
newCONSTSUB(stash, "FLAG_NAMED_PARAMS", newSViv(FLAG_NAMED_PARAMS));
+ newCONSTSUB(stash, "FLAG_TYPES_OK", newSViv(FLAG_TYPES_OK));
+ newCONSTSUB(stash, "FLAG_CHECK_TARGS", newSViv(FLAG_CHECK_TARGS));
newCONSTSUB(stash, "HINTK_KEYWORDS", newSVpvs(HINTK_KEYWORDS));
newCONSTSUB(stash, "HINTK_FLAGS_", newSVpvs(HINTK_FLAGS_));
newCONSTSUB(stash, "HINTK_SHIFT_", newSVpvs(HINTK_SHIFT_));