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 /* please try not to have a line longer than this :) */
36 #define DD_PREFERRED_LINESTR_SIZE 16384
38 /* flag to trigger removal of temporary declaree sub */
40 static int in_declare = 0;
42 /* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
43 is a lookup into it - so if anything else we can use to tell, so we
44 need to be a bit more careful if PL_parser exists */
46 #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
48 #if defined(PL_parser) || defined(PERL_5_9_PLUS)
49 #define DD_HAVE_PARSER PL_parser
50 #define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
51 #define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
53 #define DD_HAVE_PARSER 1
54 #define DD_HAVE_LEX_STUFF PL_lex_stuff
55 #define DD_AM_LEXING DD_AM_LEXING_CHECK
58 /* thing that decides whether we're dealing with a declarator */
60 int dd_is_declarator(pTHX_ char* name) {
62 SV** is_declarator_pack_ref;
63 HV* is_declarator_pack_hash;
64 SV** is_declarator_flag_ref;
67 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
72 /* $declarators{$current_package_name} */
74 if (!HvNAME(PL_curstash))
77 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
78 strlen(HvNAME(PL_curstash)), FALSE);
80 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
81 return -1; /* not a hashref */
83 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
85 /* $declarators{$current_package_name}{$name} */
87 is_declarator_flag_ref = hv_fetch(
88 is_declarator_pack_hash, name,
92 /* requires SvIOK as well as TRUE since flags not being an int is useless */
94 if (!is_declarator_flag_ref
95 || !SvIOK(*is_declarator_flag_ref)
96 || !SvTRUE(*is_declarator_flag_ref))
99 dd_flags = SvIVX(*is_declarator_flag_ref);
104 /* callback thingy */
106 void dd_linestr_callback (pTHX_ char* type, char* name) {
108 char* linestr = SvPVX(PL_linestr);
109 int offset = PL_bufptr - linestr;
117 XPUSHs(sv_2mortal(newSVpv(type, 0)));
118 XPUSHs(sv_2mortal(newSVpv(name, 0)));
119 XPUSHs(sv_2mortal(newSViv(offset)));
122 call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
128 char* dd_get_linestr(pTHX) {
129 if (!DD_HAVE_PARSER) {
132 return SvPVX(PL_linestr);
135 void dd_set_linestr(pTHX_ char* new_value) {
136 unsigned int new_len = strlen(new_value);
138 if (SvLEN(PL_linestr) < new_len) {
139 croak("PL_linestr not long enough, was Devel::Declare loaded soon enough in %s",
140 CopFILE(&PL_compiling)
145 memcpy(SvPVX(PL_linestr), new_value, new_len+1);
147 SvCUR_set(PL_linestr, new_len);
149 PL_bufend = SvPVX(PL_linestr) + new_len;
151 if ( DD_DEBUG_UPDATED_LINESTR && PERLDB_LINE && PL_curstash != PL_debstash) {
152 // Cribbed from toke.c
153 SV * const sv = NEWSV(85,0);
155 sv_upgrade(sv, SVt_PVMG);
156 sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
159 av_store(CopFILEAV(&PL_compiling),(I32)CopLINE(&PL_compiling),sv);
163 char* dd_get_lex_stuff(pTHX) {
164 return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
167 void dd_clear_lex_stuff(pTHX) {
169 PL_lex_stuff = (SV*)NULL;
172 char* dd_get_curstash_name(pTHX) {
173 return HvNAME(PL_curstash);
176 int dd_get_linestr_offset(pTHX) {
178 if (!DD_HAVE_PARSER) {
181 linestr = SvPVX(PL_linestr);
182 return PL_bufptr - linestr;
185 char* dd_move_past_token (pTHX_ char* s) {
188 * buffer will be at the beginning of the declarator, -unless- the
189 * declarator is at EOL in which case it'll be the next useful line
190 * so we don't short-circuit out if we don't find the declarator
193 while (s < PL_bufend && isSPACE(*s)) s++;
194 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
195 s += strlen(PL_tokenbuf);
199 int dd_toke_move_past_token (pTHX_ int offset) {
200 char* base_s = SvPVX(PL_linestr) + offset;
201 char* s = dd_move_past_token(aTHX_ base_s);
205 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
206 char tmpbuf[sizeof PL_tokenbuf];
207 char* base_s = SvPVX(PL_linestr) + offset;
209 char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
213 int dd_toke_scan_ident(pTHX_ int offset) {
214 char tmpbuf[sizeof PL_tokenbuf];
215 char* base_s = SvPVX(PL_linestr) + offset;
216 char* s = scan_ident(base_s, PL_bufend, tmpbuf, sizeof tmpbuf, 0);
220 int dd_toke_scan_str(pTHX_ int offset) {
221 char* old_pvx = SvPVX(PL_linestr);
222 SV* line_copy = sv_2mortal(newSVsv(PL_linestr));
223 char* base_s = SvPVX(PL_linestr) + offset;
224 char* s = scan_str(base_s, FALSE, FALSE);
225 if(SvPVX(PL_linestr) != old_pvx)
226 croak("PL_linestr reallocated during scan_str, "
227 "Devel::Declare can't continue");
231 s += SvCUR(line_copy);
232 sv_catsv(line_copy, PL_linestr);
233 dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
238 int dd_toke_skipspace(pTHX_ int offset) {
239 char* old_pvx = SvPVX(PL_linestr);
240 char* base_s = SvPVX(PL_linestr) + offset;
241 char* s = skipspace_force(base_s);
242 if(SvPVX(PL_linestr) != old_pvx)
243 croak("PL_linestr reallocated during skipspace, "
244 "Devel::Declare can't continue");
248 static void call_done_declare(pTHX) {
251 if (DD_DEBUG_TRACE) {
252 printf("Deconstructing declare\n");
253 printf("PL_bufptr: %s\n", PL_bufptr);
254 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
255 printf("linestr: %s\n", SvPVX(PL_linestr));
256 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
264 call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
269 if (DD_DEBUG_TRACE) {
270 printf("PL_bufptr: %s\n", PL_bufptr);
271 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
272 printf("linestr: %s\n", SvPVX(PL_linestr));
273 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
274 printf("actual len: %i\n", strlen(PL_bufptr));
278 static int dd_handle_const(pTHX_ char *name);
280 /* replacement PL_check rv2cv entry */
282 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
286 PERL_UNUSED_VAR(user_data);
289 return o; /* not lexing? */
292 call_done_declare(aTHX);
296 kid = cUNOPo->op_first;
298 if (kid->op_type != OP_GV) /* not a GV so ignore */
301 if (DD_DEBUG_TRACE) {
302 printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
305 dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
310 if (DD_DEBUG_TRACE) {
311 printf("dd_flags are: %i\n", dd_flags);
312 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
315 #if DD_CONST_VIA_RV2CV
316 if (PL_expect != XOPERATOR) {
317 if (!dd_handle_const(aTHX_ GvNAME(kGVOP_gv)))
319 CopLINE(PL_curcop) = PL_copline;
320 /* The parser behaviour that we're simulating depends on what comes
321 after the declarator. */
322 if (*skipspace(PL_bufptr + strlen(GvNAME(kGVOP_gv))) != '(') {
324 call_done_declare(aTHX);
326 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
331 #endif /* DD_CONST_VIA_RV2CV */
333 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
338 #if DD_GROW_VIA_BLOCKHOOK
340 static void dd_block_start(pTHX_ int full)
342 PERL_UNUSED_VAR(full);
343 if (SvLEN(PL_linestr) < DD_PREFERRED_LINESTR_SIZE)
344 (void) lex_grow_linestr(DD_PREFERRED_LINESTR_SIZE);
347 #else /* !DD_GROW_VIA_BLOCKHOOK */
349 OP* dd_pp_entereval(pTHX) {
356 if (PL_op->op_private & OPpEVAL_HAS_HH) {
362 if (DD_DEBUG_TRACE) {
363 printf("mangling eval sv\n");
366 sv = sv_2mortal(newSVsv(sv));
369 if (!len || s[len-1] != ';') {
370 if (!(SvFLAGS(sv) & SVs_TEMP))
371 sv = sv_2mortal(newSVsv(sv));
372 sv_catpvn(sv, "\n;", 2);
374 SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
378 if (PL_op->op_private & OPpEVAL_HAS_HH) {
382 return PL_ppaddr[OP_ENTEREVAL](aTHX);
385 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
386 PERL_UNUSED_VAR(user_data);
388 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
389 o->op_ppaddr = dd_pp_entereval;
393 #endif /* !DD_GROW_VIA_BLOCKHOOK */
395 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
397 const I32 count = FILTER_READ(idx+1, sv, maxlen);
398 SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
399 /* filter_del(dd_filter_realloc); */
403 static int dd_handle_const(pTHX_ char *name) {
404 switch (PL_lex_inwhat) {
417 if (strnEQ(PL_bufptr, "->", 2)) {
425 STRLEN old_offset = PL_bufptr - SvPVX(PL_linestr);
427 s = scan_word(s, buf, sizeof buf, FALSE, &len);
428 if (strnEQ(buf, name, len)) {
430 SV *inject = newSVpvn(SvPVX(PL_linestr), PL_bufptr - SvPVX(PL_linestr));
431 sv_catpvn(inject, buf, len);
434 sv_catpvn(inject, s, d - s);
436 if ((PL_bufend - d) >= 2 && strnEQ(d, "=>", 2)) {
441 dd_set_linestr(aTHX_ SvPV_nolen(inject));
442 PL_bufptr = SvPVX(PL_linestr) + old_offset;
443 SvREFCNT_dec (inject);
447 dd_linestr_callback(aTHX_ "const", name);
452 #if !DD_CONST_VIA_RV2CV
454 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
458 PERL_UNUSED_VAR(user_data);
460 if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
464 /* if this is set, we just grabbed a delimited string or something,
465 not a bareword, so NO TOUCHY */
467 if (DD_HAVE_LEX_STUFF)
470 /* don't try and look this up if it's not a string const */
471 if (!SvPOK(cSVOPo->op_sv))
474 name = SvPVX(cSVOPo->op_sv);
476 dd_flags = dd_is_declarator(aTHX_ name);
481 dd_handle_const(aTHX_ name);
486 #endif /* !DD_CONST_VIA_RV2CV */
488 STATIC void dd_initialize(pTHX) {
489 static int initialized = 0;
492 #if DD_GROW_VIA_BLOCKHOOK
494 #if PERL_VERSION_GE(5,13,6)
495 BhkENTRY_set(&bhk, bhk_start, dd_block_start);
497 BhkENTRY_set(&bhk, start, dd_block_start);
499 Perl_blockhook_register(aTHX_ &bhk);
500 #else /* !DD_GROW_VIA_BLOCKHOOK */
501 hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
502 #endif /* !DD_GROW_VIA_BLOCKHOOK */
503 hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
504 #if !DD_CONST_VIA_RV2CV
505 hook_op_check(OP_CONST, dd_ck_const, NULL);
506 #endif /* !DD_CONST_VIA_RV2CV */
510 MODULE = Devel::Declare PACKAGE = Devel::Declare
523 filter_add(dd_filter_realloc, NULL);
528 RETVAL = dd_get_linestr(aTHX);
533 set_linestr(char* new_value)
535 dd_set_linestr(aTHX_ new_value);
540 RETVAL = dd_get_lex_stuff(aTHX);
547 dd_clear_lex_stuff(aTHX);
552 RETVAL = dd_get_curstash_name(aTHX);
559 RETVAL = dd_get_linestr_offset(aTHX);
564 toke_scan_word(int offset, int handle_package)
566 RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
571 toke_move_past_token(int offset);
573 RETVAL = dd_toke_move_past_token(aTHX_ offset);
578 toke_scan_str(int offset);
582 len = dd_toke_scan_str(aTHX_ offset);
583 RETVAL = len ? newSViv(len) : &PL_sv_undef;
588 toke_scan_ident(int offset)
590 RETVAL = dd_toke_scan_ident(aTHX_ offset);
595 toke_skipspace(int offset)
597 RETVAL = dd_toke_skipspace(aTHX_ offset);
609 set_in_declare(int value)
616 char *debug_str = getenv ("DD_DEBUG");
618 dd_debug = strtol (debug_str, &endptr, 10);
619 if (*endptr != '\0') {