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_linestr(pTHX) {
110 if (!DD_HAVE_PARSER) {
113 return SvPVX(PL_linestr);
116 void dd_set_linestr(pTHX_ char* new_value) {
117 unsigned int new_len = strlen(new_value);
119 if (SvLEN(PL_linestr) < new_len) {
120 croak("PL_linestr not long enough, was Devel::Declare loaded soon enough in %s",
121 CopFILE(&PL_compiling)
126 memcpy(SvPVX(PL_linestr), new_value, new_len+1);
128 SvCUR_set(PL_linestr, new_len);
130 PL_bufend = SvPVX(PL_linestr) + new_len;
133 char* dd_get_lex_stuff(pTHX) {
134 return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
137 void dd_clear_lex_stuff(pTHX) {
139 PL_lex_stuff = (SV*)NULL;
142 char* dd_get_curstash_name(pTHX) {
143 return HvNAME(PL_curstash);
146 int dd_get_linestr_offset(pTHX) {
148 if (!DD_HAVE_PARSER) {
151 linestr = SvPVX(PL_linestr);
152 return PL_bufptr - linestr;
155 char* dd_move_past_token (pTHX_ char* s) {
158 * buffer will be at the beginning of the declarator, -unless- the
159 * declarator is at EOL in which case it'll be the next useful line
160 * so we don't short-circuit out if we don't find the declarator
163 while (s < PL_bufend && isSPACE(*s)) s++;
164 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
165 s += strlen(PL_tokenbuf);
169 int dd_toke_move_past_token (pTHX_ int offset) {
170 char* base_s = SvPVX(PL_linestr) + offset;
171 char* s = dd_move_past_token(aTHX_ base_s);
175 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
176 char tmpbuf[sizeof PL_tokenbuf];
177 char* base_s = SvPVX(PL_linestr) + offset;
179 char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
183 int dd_toke_scan_ident(pTHX_ int offset) {
184 char tmpbuf[sizeof PL_tokenbuf];
185 char* base_s = SvPVX(PL_linestr) + offset;
186 char* s = scan_ident(base_s, PL_bufend, tmpbuf, sizeof tmpbuf, 0);
190 int dd_toke_scan_str(pTHX_ int offset) {
191 STRLEN remaining = sv_len(PL_linestr) - offset;
192 SV* line_copy = newSVsv(PL_linestr);
193 char* base_s = SvPVX(PL_linestr) + offset;
194 char* s = scan_str(base_s, FALSE, FALSE);
195 if (s != base_s && sv_len(PL_lex_stuff) > remaining) {
196 int ret = (s - SvPVX(PL_linestr)) + remaining;
197 sv_catsv(line_copy, PL_linestr);
198 dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
199 SvREFCNT_dec(line_copy);
205 int dd_toke_skipspace(pTHX_ int offset) {
206 char* base_s = SvPVX(PL_linestr) + offset;
207 char* s = skipspace(base_s);
211 /* replacement PL_check rv2cv entry */
213 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
218 PERL_UNUSED_VAR(user_data);
222 printf("Deconstructing declare\n");
223 printf("PL_bufptr: %s\n", PL_bufptr);
224 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
225 printf("linestr: %s\n", SvPVX(PL_linestr));
226 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
234 call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
240 printf("PL_bufptr: %s\n", PL_bufptr);
241 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
242 printf("linestr: %s\n", SvPVX(PL_linestr));
243 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
244 printf("actual len: %i\n", strlen(PL_bufptr));
249 kid = cUNOPo->op_first;
251 if (kid->op_type != OP_GV) /* not a GV so ignore */
255 return o; /* not lexing? */
258 printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
261 dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
267 printf("dd_flags are: %i\n", dd_flags);
268 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
271 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
276 OP* dd_pp_entereval(pTHX) {
283 if (PL_op->op_private & OPpEVAL_HAS_HH) {
290 printf("mangling eval sv\n");
293 sv = sv_2mortal(newSVsv(sv));
296 if (!len || s[len-1] != ';') {
297 if (!(SvFLAGS(sv) & SVs_TEMP))
298 sv = sv_2mortal(newSVsv(sv));
299 sv_catpvn(sv, "\n;", 2);
305 if (PL_op->op_private & OPpEVAL_HAS_HH) {
309 return PL_ppaddr[OP_ENTEREVAL](aTHX);
312 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
313 PERL_UNUSED_VAR(user_data);
315 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
316 o->op_ppaddr = dd_pp_entereval;
320 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
322 const I32 count = FILTER_READ(idx+1, sv, maxlen);
323 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
324 /* filter_del(dd_filter_realloc); */
328 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
332 PERL_UNUSED_VAR(user_data);
334 if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
338 /* if this is set, we just grabbed a delimited string or something,
339 not a bareword, so NO TOUCHY */
341 if (DD_HAVE_LEX_STUFF)
344 /* don't try and look this up if it's not a string const */
345 if (!SvPOK(cSVOPo->op_sv))
348 name = SvPVX(cSVOPo->op_sv);
350 dd_flags = dd_is_declarator(aTHX_ name);
355 switch (PL_lex_inwhat) {
368 dd_linestr_callback(aTHX_ "const", name);
373 static int initialized = 0;
375 MODULE = Devel::Declare PACKAGE = Devel::Declare
382 if (!initialized++) {
383 hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
384 hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
385 hook_op_check(OP_CONST, dd_ck_const, NULL);
387 filter_add(dd_filter_realloc, NULL);
392 RETVAL = dd_get_linestr(aTHX);
397 set_linestr(char* new_value)
399 dd_set_linestr(aTHX_ new_value);
404 RETVAL = dd_get_lex_stuff(aTHX);
411 dd_clear_lex_stuff(aTHX);
416 RETVAL = dd_get_curstash_name(aTHX);
423 RETVAL = dd_get_linestr_offset(aTHX);
428 toke_scan_word(int offset, int handle_package)
430 RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
435 toke_move_past_token(int offset);
437 RETVAL = dd_toke_move_past_token(aTHX_ offset);
442 toke_scan_str(int offset);
444 RETVAL = dd_toke_scan_str(aTHX_ offset);
449 toke_scan_ident(int offset)
451 RETVAL = dd_toke_scan_ident(aTHX_ offset);
456 toke_skipspace(int offset)
458 RETVAL = dd_toke_skipspace(aTHX_ offset);
470 set_in_declare(int value)
475 if (getenv ("DD_DEBUG")) {