| 1 | /* vi: set ft=c : */ | ||
| 2 | |||
| 3 | #define make_croak_op(message) S_make_croak_op(aTHX_ message) | ||
| 4 | 0 | static OP *S_make_croak_op(pTHX_ SV *message) | |
| 5 | { | ||
| 6 | 0 | sv_catpvs(message, " at %s line %d.\n"); | |
| 7 | /* die sprintf($message, (caller)[1,2]) */ | ||
| 8 | 0 | return op_convert_list(OP_DIE, 0, | |
| 9 | op_convert_list(OP_SPRINTF, 0, | ||
| 10 | op_append_list(OP_LIST, | ||
| 11 | newSVOP(OP_CONST, 0, message), | ||
| 12 | newSLICEOP(0, | ||
| 13 | op_append_list(OP_LIST, | ||
| 14 | newSVOP(OP_CONST, 0, newSViv(1)), | ||
| 15 | newSVOP(OP_CONST, 0, newSViv(2))), | ||
| 16 | newOP(OP_CALLER, 0))))); | ||
| 17 | } | ||
| 18 | |||
| 19 | #if HAVE_PERL_VERSION(5, 26, 0) | ||
| 20 | # define HAVE_OP_ARGCHECK | ||
| 21 | |||
| 22 | # include "make_argcheck_aux.c.inc" | ||
| 23 | #endif | ||
| 24 | |||
| 25 | #define make_argcheck_ops(required, optional, slurpy, subname) S_make_argcheck_ops(aTHX_ required, optional, slurpy, subname) | ||
| 26 | 40 | static OP *S_make_argcheck_ops(pTHX_ int required, int optional, char slurpy, SV *subname) | |
| 27 | { | ||
| 28 | 40 | int params = required + optional; | |
| 29 | |||
| 30 | #ifdef HAVE_OP_ARGCHECK | ||
| 31 | 40 | UNOP_AUX_item *aux = make_argcheck_aux(params, optional, slurpy); | |
| 32 | |||
| 33 | 40 | return op_prepend_elem(OP_LINESEQ, newSTATEOP(0, NULL, NULL), | |
| 34 | op_prepend_elem(OP_LINESEQ, newUNOP_AUX(OP_ARGCHECK, 0, NULL, aux), NULL)); | ||
| 35 | #else | ||
| 36 | /* Older perls lack the convenience of OP_ARGCHECK so we'll have to build an | ||
| 37 | * optree ourselves. For now we only support required + optional, no slurpy | ||
| 38 | * | ||
| 39 | * This code heavily inspired by Perl_parse_subsignature() in toke.c from perl 5.24 | ||
| 40 | */ | ||
| 41 | |||
| 42 | OP *ret = NULL; | ||
| 43 | |||
| 44 | if(required > 0) { | ||
| 45 | SV *message = newSVpvf("Too few arguments for subroutine '%" SVf "'", subname); | ||
| 46 | /* @_ >= required or die ... */ | ||
| 47 | OP *checkop = | ||
| 48 | newSTATEOP(0, NULL, | ||
| 49 | newLOGOP(OP_OR, 0, | ||
| 50 | newBINOP(OP_GE, 0, | ||
| 51 | /* scalar @_ */ | ||
| 52 | op_contextualize(newUNOP(OP_RV2AV, 0, newGVOP(OP_GV, 0, PL_defgv)), G_SCALAR), | ||
| 53 | newSVOP(OP_CONST, 0, newSViv(required))), | ||
| 54 | make_croak_op(message))); | ||
| 55 | |||
| 56 | ret = op_append_list(OP_LINESEQ, ret, checkop); | ||
| 57 | } | ||
| 58 | |||
| 59 | if(!slurpy) { | ||
| 60 | SV *message = newSVpvf("Too many arguments for subroutine '%" SVf "'", subname); | ||
| 61 | /* @_ <= (required+optional) or die ... */ | ||
| 62 | OP *checkop = | ||
| 63 | newSTATEOP(0, NULL, | ||
| 64 | newLOGOP(OP_OR, 0, | ||
| 65 | newBINOP(OP_LE, 0, | ||
| 66 | /* scalar @_ */ | ||
| 67 | op_contextualize(newUNOP(OP_RV2AV, 0, newGVOP(OP_GV, 0, PL_defgv)), G_SCALAR), | ||
| 68 | newSVOP(OP_CONST, 0, newSViv(params))), | ||
| 69 | make_croak_op(message))); | ||
| 70 | |||
| 71 | ret = op_append_list(OP_LINESEQ, ret, checkop); | ||
| 72 | } | ||
| 73 | |||
| 74 | /* TODO: If slurpy is % then maybe complain about odd number of leftovers */ | ||
| 75 | |||
| 76 | return ret; | ||
| 77 | #endif | ||
| 78 | } |