4 #include "hook_op_check.h"
6 #include "stolen_chunk_of_toke.c"
10 #define PERL_VERSION_DECIMAL(r,v,s) (r*1000000 + v*1000 + s)
11 #define PERL_DECIMAL_VERSION \
12 PERL_VERSION_DECIMAL(PERL_REVISION,PERL_VERSION,PERL_SUBVERSION)
13 #define PERL_VERSION_GE(r,v,s) \
14 (PERL_DECIMAL_VERSION >= PERL_VERSION_DECIMAL(r,v,s))
17 # define Newx(v,n,t) New(0,v,n,t)
20 #define DD_DEBUGf_UPDATED_LINESTR 1
21 #define DD_DEBUGf_TRACE 2
23 #define DD_DEBUG_UPDATED_LINESTR (dd_debug & DD_DEBUGf_UPDATED_LINESTR)
24 #define DD_DEBUG_TRACE (dd_debug & DD_DEBUGf_TRACE)
25 static int dd_debug = 0;
27 #define DD_CONST_VIA_RV2CV PERL_VERSION_GE(5,11,2)
29 #define DD_GROW_VIA_BLOCKHOOK PERL_VERSION_GE(5,13,3)
32 #define LEX_INTERPNORMAL 9
34 /* flag to trigger removal of temporary declaree sub */
36 static int in_declare = 0;
38 /* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
39 is a lookup into it - so if anything else we can use to tell, so we
40 need to be a bit more careful if PL_parser exists */
42 #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
44 #if defined(PL_parser) || defined(PERL_5_9_PLUS)
45 #define DD_HAVE_PARSER PL_parser
46 #define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
47 #define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
49 #define DD_HAVE_PARSER 1
50 #define DD_HAVE_LEX_STUFF PL_lex_stuff
51 #define DD_AM_LEXING DD_AM_LEXING_CHECK
54 /* thing that decides whether we're dealing with a declarator */
56 int dd_is_declarator(pTHX_ char* name) {
58 SV** is_declarator_pack_ref;
59 HV* is_declarator_pack_hash;
60 SV** is_declarator_flag_ref;
63 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
68 /* $declarators{$current_package_name} */
70 if (!HvNAME(PL_curstash))
73 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
74 strlen(HvNAME(PL_curstash)), FALSE);
76 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
77 return -1; /* not a hashref */
79 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
81 /* $declarators{$current_package_name}{$name} */
83 is_declarator_flag_ref = hv_fetch(
84 is_declarator_pack_hash, name,
88 /* requires SvIOK as well as TRUE since flags not being an int is useless */
90 if (!is_declarator_flag_ref
91 || !SvIOK(*is_declarator_flag_ref)
92 || !SvTRUE(*is_declarator_flag_ref))
95 dd_flags = SvIVX(*is_declarator_flag_ref);
100 /* callback thingy */
102 void dd_linestr_callback (pTHX_ char* type, char* name) {
104 char* linestr = SvPVX(PL_linestr);
105 int offset = PL_bufptr - linestr;
113 XPUSHs(sv_2mortal(newSVpv(type, 0)));
114 XPUSHs(sv_2mortal(newSVpv(name, 0)));
115 XPUSHs(sv_2mortal(newSViv(offset)));
118 call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
124 char* dd_get_linestr(pTHX) {
125 if (!DD_HAVE_PARSER) {
128 return SvPVX(PL_linestr);
131 void dd_set_linestr(pTHX_ char* new_value) {
132 unsigned int new_len = strlen(new_value);
134 if (SvLEN(PL_linestr) < new_len) {
135 croak("PL_linestr not long enough, was Devel::Declare loaded soon enough in %s",
136 CopFILE(&PL_compiling)
141 memcpy(SvPVX(PL_linestr), new_value, new_len+1);
143 SvCUR_set(PL_linestr, new_len);
145 PL_bufend = SvPVX(PL_linestr) + new_len;
147 if ( DD_DEBUG_UPDATED_LINESTR && PERLDB_LINE && PL_curstash != PL_debstash) {
148 // Cribbed from toke.c
149 SV * const sv = NEWSV(85,0);
151 sv_upgrade(sv, SVt_PVMG);
152 sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
155 av_store(CopFILEAV(&PL_compiling),(I32)CopLINE(&PL_compiling),sv);
159 char* dd_get_lex_stuff(pTHX) {
160 return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
163 void dd_clear_lex_stuff(pTHX) {
165 PL_lex_stuff = (SV*)NULL;
168 char* dd_get_curstash_name(pTHX) {
169 return HvNAME(PL_curstash);
172 int dd_get_linestr_offset(pTHX) {
174 if (!DD_HAVE_PARSER) {
177 linestr = SvPVX(PL_linestr);
178 return PL_bufptr - linestr;
181 char* dd_move_past_token (pTHX_ char* s) {
184 * buffer will be at the beginning of the declarator, -unless- the
185 * declarator is at EOL in which case it'll be the next useful line
186 * so we don't short-circuit out if we don't find the declarator
189 while (s < PL_bufend && isSPACE(*s)) s++;
190 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
191 s += strlen(PL_tokenbuf);
195 int dd_toke_move_past_token (pTHX_ int offset) {
196 char* base_s = SvPVX(PL_linestr) + offset;
197 char* s = dd_move_past_token(aTHX_ base_s);
201 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
202 char tmpbuf[sizeof PL_tokenbuf];
203 char* base_s = SvPVX(PL_linestr) + offset;
205 char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
209 int dd_toke_scan_ident(pTHX_ int offset) {
210 char tmpbuf[sizeof PL_tokenbuf];
211 char* base_s = SvPVX(PL_linestr) + offset;
212 char* s = scan_ident(base_s, PL_bufend, tmpbuf, sizeof tmpbuf, 0);
216 int dd_toke_scan_str(pTHX_ int offset) {
217 STRLEN remaining = sv_len(PL_linestr) - offset;
218 SV* line_copy = newSVsv(PL_linestr);
219 char* base_s = SvPVX(PL_linestr) + offset;
220 char* s = scan_str(base_s, FALSE, FALSE);
221 if (s != base_s && sv_len(PL_lex_stuff) > remaining) {
222 int ret = (s - SvPVX(PL_linestr)) + remaining;
223 sv_catsv(line_copy, PL_linestr);
224 dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
225 SvREFCNT_dec(line_copy);
231 int dd_toke_skipspace(pTHX_ int offset) {
232 char* base_s = SvPVX(PL_linestr) + offset;
233 char* s = skipspace_force(base_s);
237 static void call_done_declare(pTHX) {
240 if (DD_DEBUG_TRACE) {
241 printf("Deconstructing declare\n");
242 printf("PL_bufptr: %s\n", PL_bufptr);
243 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
244 printf("linestr: %s\n", SvPVX(PL_linestr));
245 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
253 call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
258 if (DD_DEBUG_TRACE) {
259 printf("PL_bufptr: %s\n", PL_bufptr);
260 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
261 printf("linestr: %s\n", SvPVX(PL_linestr));
262 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
263 printf("actual len: %i\n", strlen(PL_bufptr));
267 static int dd_handle_const(pTHX_ char *name);
269 /* replacement PL_check rv2cv entry */
271 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
275 PERL_UNUSED_VAR(user_data);
278 return o; /* not lexing? */
281 call_done_declare(aTHX);
285 kid = cUNOPo->op_first;
287 if (kid->op_type != OP_GV) /* not a GV so ignore */
290 if (DD_DEBUG_TRACE) {
291 printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
294 dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
299 if (DD_DEBUG_TRACE) {
300 printf("dd_flags are: %i\n", dd_flags);
301 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
304 #if DD_CONST_VIA_RV2CV
305 if (PL_expect != XOPERATOR) {
306 if (!dd_handle_const(aTHX_ GvNAME(kGVOP_gv)))
308 CopLINE(PL_curcop) = PL_copline;
309 /* The parser behaviour that we're simulating depends on what comes
310 after the declarator. */
311 if (*skipspace(PL_bufptr + strlen(GvNAME(kGVOP_gv))) != '(') {
313 call_done_declare(aTHX);
315 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
320 #endif /* DD_CONST_VIA_RV2CV */
322 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
327 #if DD_GROW_VIA_BLOCKHOOK
329 static void dd_block_start(pTHX_ int full)
331 PERL_UNUSED_VAR(full);
332 if (SvLEN(PL_linestr) < 8192)
333 (void) lex_grow_linestr(8192);
336 #else /* !DD_GROW_VIA_BLOCKHOOK */
338 OP* dd_pp_entereval(pTHX) {
345 if (PL_op->op_private & OPpEVAL_HAS_HH) {
351 if (DD_DEBUG_TRACE) {
352 printf("mangling eval sv\n");
355 sv = sv_2mortal(newSVsv(sv));
358 if (!len || s[len-1] != ';') {
359 if (!(SvFLAGS(sv) & SVs_TEMP))
360 sv = sv_2mortal(newSVsv(sv));
361 sv_catpvn(sv, "\n;", 2);
367 if (PL_op->op_private & OPpEVAL_HAS_HH) {
371 return PL_ppaddr[OP_ENTEREVAL](aTHX);
374 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
375 PERL_UNUSED_VAR(user_data);
377 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
378 o->op_ppaddr = dd_pp_entereval;
382 #endif /* !DD_GROW_VIA_BLOCKHOOK */
384 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
386 const I32 count = FILTER_READ(idx+1, sv, maxlen);
387 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
388 /* filter_del(dd_filter_realloc); */
392 static int dd_handle_const(pTHX_ char *name) {
393 switch (PL_lex_inwhat) {
406 if (strnEQ(PL_bufptr, "->", 2)) {
414 STRLEN old_offset = PL_bufptr - SvPVX(PL_linestr);
416 s = scan_word(s, buf, sizeof buf, FALSE, &len);
417 if (strnEQ(buf, name, len)) {
419 SV *inject = newSVpvn(SvPVX(PL_linestr), PL_bufptr - SvPVX(PL_linestr));
420 sv_catpvn(inject, buf, len);
423 sv_catpvn(inject, s, d - s);
425 if ((PL_bufend - d) >= 2 && strnEQ(d, "=>", 2)) {
430 dd_set_linestr(aTHX_ SvPV_nolen(inject));
431 PL_bufptr = SvPVX(PL_linestr) + old_offset;
432 SvREFCNT_dec (inject);
436 dd_linestr_callback(aTHX_ "const", name);
441 #if !DD_CONST_VIA_RV2CV
443 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
447 PERL_UNUSED_VAR(user_data);
449 if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
453 /* if this is set, we just grabbed a delimited string or something,
454 not a bareword, so NO TOUCHY */
456 if (DD_HAVE_LEX_STUFF)
459 /* don't try and look this up if it's not a string const */
460 if (!SvPOK(cSVOPo->op_sv))
463 name = SvPVX(cSVOPo->op_sv);
465 dd_flags = dd_is_declarator(aTHX_ name);
470 dd_handle_const(aTHX_ name);
475 #endif /* !DD_CONST_VIA_RV2CV */
477 static int initialized = 0;
479 MODULE = Devel::Declare PACKAGE = Devel::Declare
486 if (!initialized++) {
487 #if DD_GROW_VIA_BLOCKHOOK
489 #if PERL_VERSION_GE(5,13,6)
490 BhkENTRY_set(&bhk, bhk_start, dd_block_start);
492 BhkENTRY_set(&bhk, start, dd_block_start);
494 Perl_blockhook_register(aTHX_ &bhk);
495 #else /* !DD_GROW_VIA_BLOCKHOOK */
496 hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
497 #endif /* !DD_GROW_VIA_BLOCKHOOK */
498 hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
499 #if !DD_CONST_VIA_RV2CV
500 hook_op_check(OP_CONST, dd_ck_const, NULL);
501 #endif /* !DD_CONST_VIA_RV2CV */
503 filter_add(dd_filter_realloc, NULL);
508 RETVAL = dd_get_linestr(aTHX);
513 set_linestr(char* new_value)
515 dd_set_linestr(aTHX_ new_value);
520 RETVAL = dd_get_lex_stuff(aTHX);
527 dd_clear_lex_stuff(aTHX);
532 RETVAL = dd_get_curstash_name(aTHX);
539 RETVAL = dd_get_linestr_offset(aTHX);
544 toke_scan_word(int offset, int handle_package)
546 RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
551 toke_move_past_token(int offset);
553 RETVAL = dd_toke_move_past_token(aTHX_ offset);
558 toke_scan_str(int offset);
560 RETVAL = dd_toke_scan_str(aTHX_ offset);
565 toke_scan_ident(int offset)
567 RETVAL = dd_toke_scan_ident(aTHX_ offset);
572 toke_skipspace(int offset)
574 RETVAL = dd_toke_skipspace(aTHX_ offset);
586 set_in_declare(int value)
593 char *debug_str = getenv ("DD_DEBUG");
595 dd_debug = strtol (debug_str, &endptr, 10);
596 if (*endptr != '\0') {