4 #include "hook_op_check.h"
6 #include "stolen_chunk_of_toke.c"
11 # define Newx(v,n,t) New(0,v,n,t)
14 static int dd_debug = 0;
17 #define LEX_INTERPNORMAL 9
19 /* flag to trigger removal of temporary declaree sub */
21 static int in_declare = 0;
23 /* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
24 is a lookup into it - so if anything else we can use to tell, so we
25 need to be a bit more careful if PL_parser exists */
27 #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
29 #if defined(PL_parser) || defined(PERL_5_9_PLUS)
30 #define DD_HAVE_PARSER PL_parser
31 #define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
32 #define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
34 #define DD_HAVE_PARSER 1
35 #define DD_HAVE_LEX_STUFF PL_lex_stuff
36 #define DD_AM_LEXING DD_AM_LEXING_CHECK
39 /* thing that decides whether we're dealing with a declarator */
41 int dd_is_declarator(pTHX_ char* name) {
43 SV** is_declarator_pack_ref;
44 HV* is_declarator_pack_hash;
45 SV** is_declarator_flag_ref;
48 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
53 /* $declarators{$current_package_name} */
55 if (!HvNAME(PL_curstash))
58 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
59 strlen(HvNAME(PL_curstash)), FALSE);
61 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
62 return -1; /* not a hashref */
64 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
66 /* $declarators{$current_package_name}{$name} */
68 is_declarator_flag_ref = hv_fetch(
69 is_declarator_pack_hash, name,
73 /* requires SvIOK as well as TRUE since flags not being an int is useless */
75 if (!is_declarator_flag_ref
76 || !SvIOK(*is_declarator_flag_ref)
77 || !SvTRUE(*is_declarator_flag_ref))
80 dd_flags = SvIVX(*is_declarator_flag_ref);
87 void dd_linestr_callback (pTHX_ char* type, char* name) {
89 char* linestr = SvPVX(PL_linestr);
90 int offset = PL_bufptr - linestr;
98 XPUSHs(sv_2mortal(newSVpv(type, 0)));
99 XPUSHs(sv_2mortal(newSVpv(name, 0)));
100 XPUSHs(sv_2mortal(newSViv(offset)));
103 call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
109 char* dd_get_lex_stuff(pTHX) {
110 return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
113 void dd_clear_lex_stuff(pTHX) {
115 PL_lex_stuff = (SV*)NULL;
118 char* dd_get_curstash_name(pTHX) {
119 return HvNAME(PL_curstash);
122 char* dd_move_past_token (pTHX_ char* s) {
125 * buffer will be at the beginning of the declarator, -unless- the
126 * declarator is at EOL in which case it'll be the next useful line
127 * so we don't short-circuit out if we don't find the declarator
130 while (s < PL_bufend && isSPACE(*s)) s++;
131 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
132 s += strlen(PL_tokenbuf);
136 int dd_toke_move_past_token (pTHX_ int offset) {
137 char* base_s = SvPVX(PL_linestr) + offset;
138 char* s = dd_move_past_token(aTHX_ base_s);
142 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
143 char tmpbuf[sizeof PL_tokenbuf];
144 char* base_s = SvPVX(PL_linestr) + offset;
146 char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
150 int dd_toke_scan_str(pTHX_ int offset) {
151 char* base_s = SvPVX(PL_linestr) + offset;
152 char* s = scan_str(base_s, FALSE, FALSE);
156 int dd_toke_skipspace(pTHX_ int offset) {
157 char* base_s = SvPVX(PL_linestr) + offset;
158 char* s = skipspace(base_s);
162 /* replacement PL_check rv2cv entry */
164 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
170 printf("Deconstructing declare\n");
171 printf("PL_bufptr: %s\n", PL_bufptr);
172 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
173 printf("linestr: %s\n", SvPVX(PL_linestr));
174 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
184 call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
190 printf("PL_bufptr: %s\n", PL_bufptr);
191 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
192 printf("linestr: %s\n", SvPVX(PL_linestr));
193 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
194 printf("actual len: %i\n", strlen(PL_bufptr));
199 kid = cUNOPo->op_first;
201 if (kid->op_type != OP_GV) /* not a GV so ignore */
205 return o; /* not lexing? */
208 printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
211 dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
217 printf("dd_flags are: %i\n", dd_flags);
218 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
221 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
226 OP* dd_pp_entereval(pTHX) {
233 printf("mangling eval sv\n");
236 sv = sv_2mortal(newSVsv(sv));
239 if (!len || s[len-1] != ';') {
240 if (!(SvFLAGS(sv) & SVs_TEMP))
241 sv = sv_2mortal(newSVsv(sv));
242 sv_catpvn(sv, "\n;", 2);
247 return PL_ppaddr[OP_ENTEREVAL](aTHX);
250 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
251 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
252 o->op_ppaddr = dd_pp_entereval;
256 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
258 const I32 count = FILTER_READ(idx+1, sv, maxlen);
259 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
260 /* filter_del(dd_filter_realloc); */
264 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
268 /* if this is set, we just grabbed a delimited string or something,
269 not a bareword, so NO TOUCHY */
271 if (DD_HAVE_LEX_STUFF)
274 /* don't try and look this up if it's not a string const */
275 if (!SvPOK(cSVOPo->op_sv))
278 name = SvPVX(cSVOPo->op_sv);
280 dd_flags = dd_is_declarator(aTHX_ name);
285 dd_linestr_callback(aTHX_ "const", name);
290 static int initialized = 0;
292 MODULE = Devel::Declare PACKAGE = Devel::Declare
299 if (!initialized++) {
300 hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
301 hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
302 hook_op_check(OP_CONST, dd_ck_const, NULL);
304 filter_add(dd_filter_realloc, NULL);
309 RETVAL = dd_get_lex_stuff(aTHX);
316 dd_clear_lex_stuff(aTHX);
321 RETVAL = dd_get_curstash_name(aTHX);
326 toke_scan_word(int offset, int handle_package)
328 RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
333 toke_move_past_token(int offset);
335 RETVAL = dd_toke_move_past_token(aTHX_ offset);
340 toke_scan_str(int offset);
342 RETVAL = dd_toke_scan_str(aTHX_ offset);
347 toke_skipspace(int offset)
349 RETVAL = dd_toke_skipspace(aTHX_ offset);
361 set_in_declare(int value)
366 if (getenv ("DD_DEBUG")) {