/*
 * This file was generated automatically by ExtUtils::ParseXS version 3.51 from the
 * contents of XS.xs. Do not edit this file, edit XS.xs instead.
 *
 *    ANY CHANGES MADE HERE WILL BE LOST!
 *
 */

#line 1 "XS.xs"
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

#define NEED_SvRX
#include "ppport.h"

#if defined(cv_set_call_checker) && defined(XopENTRY_set)
# define USE_CUSTOM_OPS 1
#else
# define USE_CUSTOM_OPS 0
#endif

/* Boolean expression that considers an SV* named "ref" */
#define COND(expr) (SvROK(ref) && expr)

#define PLAIN         (!sv_isobject(ref))
#define REFTYPE(tail) (SvTYPE(SvRV(ref)) tail)
#define REFREF        (SvROK( SvRV(ref) ))

#define JUSTSCALAR (                            \
        REFTYPE(< SVt_PVAV)                     \
        && REFTYPE(!= SVt_PVGV)                 \
        && (SvTYPE(SvRV(ref)) != SVt_PVGV)      \
        && !REFREF                              \
        && !SvRXOK(ref)                         \
        )

#if PERL_VERSION >= 7
#define FORMATREF REFTYPE(== SVt_PVFM)
#else
#define FORMATREF (croak("is_formatref() isn't available on Perl 5.6.x and under"), 0)
#endif

#define FUNC_BODY(cond)                                 \
  {                                                     \
    SV *ref = TOPs;                                     \
    SvGETMAGIC(ref);                                    \
    SETs( COND(cond) ? &PL_sv_yes : &PL_sv_no );        \
  }

#define DECL_RUNTIME_FUNC(x, cond)                              \
    static void                                                 \
    THX_xsfunc_ ## x (pTHX_ CV *cv)                             \
    {                                                           \
        dXSARGS;                                                \
        if (items != 1)                                         \
            Perl_croak(aTHX_ "Usage: Ref::Util::XS::" #x "(ref)");  \
        FUNC_BODY(cond);                                        \
    }

#define DECL_XOP(x) \
    static XOP x ## _xop;

#define DECL_MAIN_FUNC(x, cond)                 \
    static OP *                                 \
    x ## _op(pTHX)                              \
    {                                           \
        dSP;                                    \
        FUNC_BODY(cond);                        \
        return NORMAL;                          \
    }

#define DECL_CALL_CHK_FUNC(x)                                                  \
    static OP *                                                                \
    THX_ck_entersub_args_ ## x(pTHX_ OP *entersubop, GV *namegv, SV *ckobj)    \
    {                                                                          \
        return call_checker_common(aTHX_ entersubop, namegv, ckobj, x ## _op); \
    }

#if !USE_CUSTOM_OPS

#define DECL(x, cond) DECL_RUNTIME_FUNC(x, cond)
#define INSTALL(x, ref) \
    newXSproto("Ref::Util::XS::" #x, THX_xsfunc_ ## x, __FILE__, "$");

#else

#define DECL(x, cond)                           \
    DECL_RUNTIME_FUNC(x, cond)                  \
    DECL_XOP(x)                                 \
    DECL_MAIN_FUNC(x, cond)                     \
    DECL_CALL_CHK_FUNC(x)

#define INSTALL(x, ref)                                               \
    {                                                                 \
        CV *cv;                                                       \
        XopENTRY_set(& x ##_xop, xop_name, #x);                       \
        XopENTRY_set(& x ##_xop, xop_desc, "'" ref "' ref check");    \
        XopENTRY_set(& x ##_xop, xop_class, OA_UNOP);                 \
        Perl_custom_op_register(aTHX_ x ##_op, & x ##_xop);           \
        cv = newXSproto_portable(                                     \
            "Ref::Util::XS::" #x, THX_xsfunc_ ## x, __FILE__, "$"         \
        );                                                            \
        cv_set_call_checker(cv, THX_ck_entersub_args_ ## x, (SV*)cv); \
    }

// This function extracts the args for the custom op, and deletes the remaining
// ops from memory, so they can then be replaced entirely by the custom op.
/*
    This is how the ops will look like:

    $ perl -MO=Concise -E'is_arrayref($foo)'
    7  <@> leave[1 ref] vKP/REFC ->(end)
    1     <0> enter ->2
    2     <;> nextstate(main 47 -e:1) v:%,{,469764096 ->3
    6     <1> entersub[t4] vKS/TARG ->7
    -        <1> ex-list K ->6
    3           <0> pushmark s ->4
    -           <1> ex-rv2sv sKM/1 ->5
    4              <#> gvsv[*foo] s ->5
    -           <1> ex-rv2cv sK ->-
    5              <#> gv[*is_arrayref] ->6
*/
static OP *
call_checker_common(pTHX_ OP *entersubop, GV *namegv, SV *ckobj, OP* (*op_ppaddr)(pTHX))
{
    OP *pushop = NULL;
    OP *arg = NULL;
    OP *newop = NULL;

    /* fix up argument structures */
    entersubop = ck_entersub_args_proto(entersubop, namegv, ckobj);

    /* extract the args for the custom op, and delete the remaining ops
       NOTE: this is the *single* arg version, multi-arg is more
       complicated, see Hash::SharedMem's THX_ck_entersub_args_hsm */

    /* These comments will visualize how the op tree look like after
       each operation. We usually start out with this: */
    /* --> entersub( list( push, arg1, cv ) ) */
    /* Though in rare cases it can also look like this: */
    /* --> entersub( push, arg1, cv ) */

    /* first, get the real pushop, after which comes the arg list */

    /* Cast the entersub op as an op with a single child */
    /* and get that child (the args list or pushop). */
    pushop = cUNOPx( entersubop )->op_first;

    /* At this point we're still not sure if it's the right op,
       (because it should normally be a list() with the push inside it)
       so we check whether it has siblings or not. The list() has no
       siblings */
    /* Go one layer deeper to get at the real pushop. */
    if( !OpHAS_SIBLING( pushop ) )
      /* Fetch the actual push op from inside the list() op */
      pushop = cUNOPx( pushop )->op_first;

    /* then extract the arg */
    /* Get a pointer to the first arg op */
    /* so we can attach it to the custom op later on. */
    /* Notice "ex-rv2sv" calls are optimized away. */
    arg = OpSIBLING( pushop );

    /* --> entersub( list( push, arg1, cv ) ) + ( arg1, cv ) */

    /* and prepare to delete the other ops */
    /* Replace the first op of the arg list with the last arg op
       (the cv op, i.e. pointer to original xs function),
       which allows recursive deletion of all unneeded ops
       while keeping the arg list. */
    OpMORESIB_set( pushop, OpSIBLING( arg ) );
    /* --> entersub( list( push, cv ) ) + ( arg1, cv ) */

    /* Remove the trailing cv op from the arg list,
       by declaring the arg to be the last sibling in the arg list. */
    OpLASTSIB_set( arg, NULL );
    /* --> entersub( list( push, cv ) ) */
    /* --> arg1                         */

    /* Recursively free entersubop + children,
       as it'll be replaced by the op we return. */
    op_free( entersubop );
    /* --> ( arg1 ) */

    /* create and return new op */
    newop = newUNOP( OP_NULL, 0, arg );
    /* can't do this in the new above, due to crashes pre-5.22 */
    newop->op_type   = OP_CUSTOM;
    newop->op_ppaddr = op_ppaddr;
    /* --> custom_op( arg1 ) */

    return newop;
}

#endif

DECL(is_ref,             1)
DECL(is_scalarref,       JUSTSCALAR)
DECL(is_arrayref,        REFTYPE(== SVt_PVAV))
DECL(is_hashref,         REFTYPE(== SVt_PVHV))
DECL(is_coderef,         REFTYPE(== SVt_PVCV))
DECL(is_globref,         REFTYPE(== SVt_PVGV))
DECL(is_formatref,       FORMATREF)
DECL(is_ioref,           REFTYPE(== SVt_PVIO))
DECL(is_regexpref,       SvRXOK(ref))
DECL(is_refref,          REFREF)

DECL(is_plain_ref,       PLAIN)
DECL(is_plain_scalarref, JUSTSCALAR && PLAIN)
DECL(is_plain_arrayref,  REFTYPE(== SVt_PVAV) && PLAIN)
DECL(is_plain_hashref,   REFTYPE(== SVt_PVHV) && PLAIN)
DECL(is_plain_coderef,   REFTYPE(== SVt_PVCV) && PLAIN)
DECL(is_plain_globref,   REFTYPE(== SVt_PVGV) && PLAIN)
DECL(is_plain_formatref, FORMATREF && PLAIN)
DECL(is_plain_ioref,     REFTYPE(== SVt_PVIO) && PLAIN)
DECL(is_plain_refref,    REFREF && PLAIN)

DECL(is_blessed_ref,       !PLAIN)
DECL(is_blessed_scalarref, JUSTSCALAR && !PLAIN)
DECL(is_blessed_arrayref,  REFTYPE(== SVt_PVAV) && !PLAIN)
DECL(is_blessed_hashref,   REFTYPE(== SVt_PVHV) && !PLAIN)
DECL(is_blessed_coderef,   REFTYPE(== SVt_PVCV) && !PLAIN)
DECL(is_blessed_globref,   REFTYPE(== SVt_PVGV) && !PLAIN)
DECL(is_blessed_formatref, FORMATREF && !PLAIN)
DECL(is_blessed_ioref,     REFTYPE(== SVt_PVIO) && !PLAIN)
DECL(is_blessed_refref,    REFREF && !PLAIN)

#line 231 "XS.c"
#ifndef PERL_UNUSED_VAR
#  define PERL_UNUSED_VAR(var) if (0) var = var
#endif

#ifndef dVAR
#  define dVAR		dNOOP
#endif


/* This stuff is not part of the API! You have been warned. */
#ifndef PERL_VERSION_DECIMAL
#  define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
#endif
#ifndef PERL_DECIMAL_VERSION
#  define PERL_DECIMAL_VERSION \
	  PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
#endif
#ifndef PERL_VERSION_GE
#  define PERL_VERSION_GE(r,v,s) \
	  (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
#endif
#ifndef PERL_VERSION_LE
#  define PERL_VERSION_LE(r,v,s) \
	  (PERL_DECIMAL_VERSION <= PERL_VERSION_DECIMAL(r,v,s))
#endif

/* XS_INTERNAL is the explicit static-linkage variant of the default
 * XS macro.
 *
 * XS_EXTERNAL is the same as XS_INTERNAL except it does not include
 * "STATIC", ie. it exports XSUB symbols. You probably don't want that
 * for anything but the BOOT XSUB.
 *
 * See XSUB.h in core!
 */


/* TODO: This might be compatible further back than 5.10.0. */
#if PERL_VERSION_GE(5, 10, 0) && PERL_VERSION_LE(5, 15, 1)
#  undef XS_EXTERNAL
#  undef XS_INTERNAL
#  if defined(__CYGWIN__) && defined(USE_DYNAMIC_LOADING)
#    define XS_EXTERNAL(name) __declspec(dllexport) XSPROTO(name)
#    define XS_INTERNAL(name) STATIC XSPROTO(name)
#  endif
#  if defined(__SYMBIAN32__)
#    define XS_EXTERNAL(name) EXPORT_C XSPROTO(name)
#    define XS_INTERNAL(name) EXPORT_C STATIC XSPROTO(name)
#  endif
#  ifndef XS_EXTERNAL
#    if defined(HASATTRIBUTE_UNUSED) && !defined(__cplusplus)
#      define XS_EXTERNAL(name) void name(pTHX_ CV* cv __attribute__unused__)
#      define XS_INTERNAL(name) STATIC void name(pTHX_ CV* cv __attribute__unused__)
#    else
#      ifdef __cplusplus
#        define XS_EXTERNAL(name) extern "C" XSPROTO(name)
#        define XS_INTERNAL(name) static XSPROTO(name)
#      else
#        define XS_EXTERNAL(name) XSPROTO(name)
#        define XS_INTERNAL(name) STATIC XSPROTO(name)
#      endif
#    endif
#  endif
#endif

/* perl >= 5.10.0 && perl <= 5.15.1 */


/* The XS_EXTERNAL macro is used for functions that must not be static
 * like the boot XSUB of a module. If perl didn't have an XS_EXTERNAL
 * macro defined, the best we can do is assume XS is the same.
 * Dito for XS_INTERNAL.
 */
#ifndef XS_EXTERNAL
#  define XS_EXTERNAL(name) XS(name)
#endif
#ifndef XS_INTERNAL
#  define XS_INTERNAL(name) XS(name)
#endif

/* Now, finally, after all this mess, we want an ExtUtils::ParseXS
 * internal macro that we're free to redefine for varying linkage due
 * to the EXPORT_XSUB_SYMBOLS XS keyword. This is internal, use
 * XS_EXTERNAL(name) or XS_INTERNAL(name) in your code if you need to!
 */

#undef XS_EUPXS
#if defined(PERL_EUPXS_ALWAYS_EXPORT)
#  define XS_EUPXS(name) XS_EXTERNAL(name)
#else
   /* default to internal */
#  define XS_EUPXS(name) XS_INTERNAL(name)
#endif

#ifndef PERL_ARGS_ASSERT_CROAK_XS_USAGE
#define PERL_ARGS_ASSERT_CROAK_XS_USAGE assert(cv); assert(params)

/* prototype to pass -Wmissing-prototypes */
STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params);

STATIC void
S_croak_xs_usage(const CV *const cv, const char *const params)
{
    const GV *const gv = CvGV(cv);

    PERL_ARGS_ASSERT_CROAK_XS_USAGE;

    if (gv) {
        const char *const gvname = GvNAME(gv);
        const HV *const stash = GvSTASH(gv);
        const char *const hvname = stash ? HvNAME(stash) : NULL;

        if (hvname)
	    Perl_croak_nocontext("Usage: %s::%s(%s)", hvname, gvname, params);
        else
	    Perl_croak_nocontext("Usage: %s(%s)", gvname, params);
    } else {
        /* Pants. I don't think that it should be possible to get here. */
	Perl_croak_nocontext("Usage: CODE(0x%" UVxf ")(%s)", PTR2UV(cv), params);
    }
}
#undef  PERL_ARGS_ASSERT_CROAK_XS_USAGE

#define croak_xs_usage        S_croak_xs_usage

#endif

/* NOTE: the prototype of newXSproto() is different in versions of perls,
 * so we define a portable version of newXSproto()
 */
#ifdef newXS_flags
#define newXSproto_portable(name, c_impl, file, proto) newXS_flags(name, c_impl, file, proto, 0)
#else
#define newXSproto_portable(name, c_impl, file, proto) (PL_Sv=(SV*)newXS(name, c_impl, file), sv_setpv(PL_Sv, proto), (CV*)PL_Sv)
#endif /* !defined(newXS_flags) */

#if PERL_VERSION_LE(5, 21, 5)
#  define newXS_deffile(a,b) Perl_newXS(aTHX_ a,b,file)
#else
#  define newXS_deffile(a,b) Perl_newXS_deffile(aTHX_ a,b)
#endif

#line 375 "XS.c"

XS_EUPXS(XS_Ref__Util__XS__using_custom_ops); /* prototype to pass -Wmissing-prototypes */
XS_EUPXS(XS_Ref__Util__XS__using_custom_ops)
{
    dVAR; dXSARGS;
    if (items != 0)
       croak_xs_usage(cv,  "");
    PERL_UNUSED_VAR(ax); /* -Wall */
    SP -= items;
    {
	SV *	RETVAL;
#line 258 "XS.xs"
        /* This is provided for the test suite; do not use it. */
        /* Use if-else below because ternary operator cannot build on Sun
           Studio 11 and 12. */
        if (USE_CUSTOM_OPS) {
            XSRETURN_YES;
        }
        else {
            XSRETURN_NO;
        }
#line 397 "XS.c"
	PUTBACK;
	return;
    }
}

#ifdef __cplusplus
extern "C" {
#endif
XS_EXTERNAL(boot_Ref__Util__XS); /* prototype to pass -Wmissing-prototypes */
XS_EXTERNAL(boot_Ref__Util__XS)
{
#if PERL_VERSION_LE(5, 21, 5)
    dVAR; dXSARGS;
#else
    dVAR; dXSBOOTARGSXSAPIVERCHK;
#endif
#if PERL_VERSION_LE(5, 8, 999) /* PERL_VERSION_LT is 5.33+ */
    char* file = __FILE__;
#else
    const char* file = __FILE__;
#endif

    PERL_UNUSED_VAR(file);

    PERL_UNUSED_VAR(cv); /* -W */
    PERL_UNUSED_VAR(items); /* -W */
#if PERL_VERSION_LE(5, 21, 5)
    XS_VERSION_BOOTCHECK;
#  ifdef XS_APIVERSION_BOOTCHECK
    XS_APIVERSION_BOOTCHECK;
#  endif
#endif

        newXS_deffile("Ref::Util::XS::_using_custom_ops", XS_Ref__Util__XS__using_custom_ops);

    /* Initialisation Section */

#line 226 "XS.xs"
    {
        INSTALL( is_ref, "" )
        INSTALL( is_scalarref, "SCALAR" )
        INSTALL( is_arrayref,  "ARRAY"  )
        INSTALL( is_hashref,   "HASH"   )
        INSTALL( is_coderef,   "CODE"   )
        INSTALL( is_regexpref, "REGEXP" )
        INSTALL( is_globref,   "GLOB"   )
        INSTALL( is_formatref, "FORMAT" )
        INSTALL( is_ioref,     "IO"     )
        INSTALL( is_refref,    "REF"    )
        INSTALL( is_plain_ref, "plain" )
        INSTALL( is_plain_scalarref, "plain SCALAR" )
        INSTALL( is_plain_arrayref,  "plain ARRAY"  )
        INSTALL( is_plain_hashref,   "plain HASH"   )
        INSTALL( is_plain_coderef,   "plain CODE"   )
        INSTALL( is_plain_globref,   "plain GLOB"   )
        INSTALL( is_plain_formatref,   "plain FORMAT"   )
        INSTALL( is_plain_refref,   "plain REF"   )
        INSTALL( is_blessed_ref, "blessed" )
        INSTALL( is_blessed_scalarref, "blessed SCALAR" )
        INSTALL( is_blessed_arrayref,  "blessed ARRAY"  )
        INSTALL( is_blessed_hashref,   "blessed HASH"   )
        INSTALL( is_blessed_coderef,   "blessed CODE"   )
        INSTALL( is_blessed_globref,   "blessed GLOB"   )
        INSTALL( is_blessed_formatref,   "blessed FORMAT"   )
        INSTALL( is_blessed_refref,   "blessed REF"   )
    }

#line 465 "XS.c"

    /* End of Initialisation Section */

#if PERL_VERSION_LE(5, 21, 5)
#  if PERL_VERSION_GE(5, 9, 0)
    if (PL_unitcheckav)
        call_list(PL_scopestack_ix, PL_unitcheckav);
#  endif
    XSRETURN_YES;
#else
    Perl_xs_boot_epilog(aTHX_ ax);
#endif
}

#ifdef __cplusplus
}
#endif
