summaryrefslogtreecommitdiffstats
path: root/CompleteStatement.xs
blob: 503e5f2334fe826f290cc39bd046fbf2f7f073ce (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"

static BHK my_hooks;
static int depth;

static void
my_start_hook(pTHX_ int full)
{
    ++depth;
}

static void
my_end_hook(pTHX_ OP **o)
{
    --depth;
}

static void
reset_block_hooks(pTHX_ void *p)
{
    BhkDISABLE(&my_hooks, bhk_start);
    BhkDISABLE(&my_hooks, bhk_pre_end);
}

static void
call_parse()
{
    dSP;

    ENTER;
    PUSHMARK(SP);
    call_pv("Devel::CompleteStatement::_call_parse", G_DISCARD);
    LEAVE;
}

MODULE = Devel::CompleteStatement  PACKAGE = Devel::CompleteStatement

PROTOTYPES: DISABLE

void
_parse()
  PREINIT:
    OP *o;
  CODE:
    ENTER;

    SAVEI8(PL_in_eval);
    PL_in_eval = EVAL_INEVAL;

    if (o = parse_stmtseq(0))
        op_free(o);

    LEAVE;

SV *
complete_statement(str)
    SV *str
  PREINIT:
    CV *evalcv;
  CODE:
    ENTER;
    SAVETMPS;
    SAVEDESTRUCTOR_X(reset_block_hooks, NULL);

    /* most of this copied from Parse::Perl */

    /* populate PL_compiling and related state */
    SAVECOPFILE_FREE(&PL_compiling);
    {
        char filename[TYPE_DIGITS(long) + 10];
        sprintf(filename, "(eval %lu)", (unsigned long)++PL_evalseq);
        CopFILE_set(&PL_compiling, filename);
    }
    SAVECOPLINE(&PL_compiling);
    CopLINE_set(&PL_compiling, 1);
    SAVEI32(PL_subline);
    PL_subline = 1;
    save_item(PL_curstname);
    sv_setpv(PL_curstname,
            !PL_curstash ? "<none>" : HvNAME_get(PL_curstash));
    SAVECOPSTASH_FREE(&PL_compiling);
    CopSTASH_set(&PL_compiling, PL_curstash);

    SAVECOMPILEWARNINGS();

    PL_hints |= HINT_LOCALIZE_HH;
    SAVEHINTS();
        HINT_BLOCK_SCOPE;

    SAVEI32(PL_compiling.cop_hints);
    PL_compiling.cop_hints = PL_hints;

    SAVEVPTR(PL_curcop);
    PL_curcop = &PL_compiling;
    /* initialise PL_compcv and related state */
    SAVEGENERICSV(PL_compcv);
    PL_compcv = (CV*)newSV_type(SVt_PVCV);
    CvANON_on(PL_compcv);
    CvOUTSIDE(PL_compcv) = NULL;
    CvOUTSIDE_SEQ(PL_compcv) = 0;
    CvPADLIST(PL_compcv) = pad_new(padnew_SAVE);
    /* initialise other parser state */
    SAVEOP();
    PL_op = NULL;
    SAVEGENERICSV(PL_beginav);
    PL_beginav = newAV();
    SAVEGENERICSV(PL_unitcheckav);
    PL_unitcheckav = newAV();
    lex_start(str, NULL, 0);

    depth = 0;
    BhkENABLE(&my_hooks, bhk_start);
    BhkENABLE(&my_hooks, bhk_pre_end);

    call_parse();

    RETVAL = (PL_parser->bufptr != PL_parser->bufend)
        ? &PL_sv_undef
        : (depth == 0)
        ? &PL_sv_yes
        : &PL_sv_no;

    FREETMPS;
    LEAVE;
  OUTPUT:
    RETVAL

BOOT:
    BhkENTRY_set(&my_hooks, bhk_start, my_start_hook);
    BhkENTRY_set(&my_hooks, bhk_pre_end, my_end_hook);
    Perl_blockhook_register(aTHX_ &my_hooks);
    reset_block_hooks(aTHX_ NULL);