prevent toke_scan_str moving backward in linestr
[p5sagit/Devel-Declare.git] / Declare.xs
1 #include "EXTERN.h"
2 #include "perl.h"
3 #include "XSUB.h"
4 #include "hook_op_check.h"
5 #undef printf
6 #include "stolen_chunk_of_toke.c"
7 #include <stdio.h>
8 #include <string.h>
9
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))
15
16 #ifndef Newx
17 # define Newx(v,n,t) New(0,v,n,t)
18 #endif /* !Newx */
19
20 #define DD_DEBUGf_UPDATED_LINESTR 1
21 #define DD_DEBUGf_TRACE 2
22
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;
26
27 #define DD_CONST_VIA_RV2CV PERL_VERSION_GE(5,11,2)
28
29 #define DD_GROW_VIA_BLOCKHOOK PERL_VERSION_GE(5,13,3)
30
31 #define LEX_NORMAL    10
32 #define LEX_INTERPNORMAL   9
33
34 /* please try not to have a line longer than this :) */
35
36 #define DD_PREFERRED_LINESTR_SIZE 16384
37
38 /* flag to trigger removal of temporary declaree sub */
39
40 static int in_declare = 0;
41
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 */
45
46 #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
47
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)
52 #else
53 #define DD_HAVE_PARSER 1
54 #define DD_HAVE_LEX_STUFF PL_lex_stuff
55 #define DD_AM_LEXING DD_AM_LEXING_CHECK
56 #endif
57
58 /* thing that decides whether we're dealing with a declarator */
59
60 int dd_is_declarator(pTHX_ char* name) {
61   HV* is_declarator;
62   SV** is_declarator_pack_ref;
63   HV* is_declarator_pack_hash;
64   SV** is_declarator_flag_ref;
65   int dd_flags;
66
67   is_declarator = get_hv("Devel::Declare::declarators", FALSE);
68
69   if (!is_declarator)
70     return -1;
71
72   /* $declarators{$current_package_name} */
73
74   if (!HvNAME(PL_curstash))
75     return -1;
76
77   is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
78                              strlen(HvNAME(PL_curstash)), FALSE);
79
80   if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
81     return -1; /* not a hashref */
82
83   is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
84
85   /* $declarators{$current_package_name}{$name} */
86
87   is_declarator_flag_ref = hv_fetch(
88     is_declarator_pack_hash, name,
89     strlen(name), FALSE
90   );
91
92   /* requires SvIOK as well as TRUE since flags not being an int is useless */
93
94   if (!is_declarator_flag_ref
95         || !SvIOK(*is_declarator_flag_ref)
96         || !SvTRUE(*is_declarator_flag_ref))
97     return -1;
98
99   dd_flags = SvIVX(*is_declarator_flag_ref);
100
101   return dd_flags;
102 }
103
104 /* callback thingy */
105
106 void dd_linestr_callback (pTHX_ char* type, char* name) {
107
108   char* linestr = SvPVX(PL_linestr);
109   int offset = PL_bufptr - linestr;
110
111   dSP;
112
113   ENTER;
114   SAVETMPS;
115
116   PUSHMARK(SP);
117   XPUSHs(sv_2mortal(newSVpv(type, 0)));
118   XPUSHs(sv_2mortal(newSVpv(name, 0)));
119   XPUSHs(sv_2mortal(newSViv(offset)));
120   PUTBACK;
121
122   call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
123
124   FREETMPS;
125   LEAVE;
126 }
127
128 char* dd_get_linestr(pTHX) {
129   if (!DD_HAVE_PARSER) {
130     return NULL;
131   }
132   return SvPVX(PL_linestr);
133 }
134
135 void dd_set_linestr(pTHX_ char* new_value) {
136   unsigned int new_len = strlen(new_value);
137
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)
141     );
142   }
143
144
145   memcpy(SvPVX(PL_linestr), new_value, new_len+1);
146
147   SvCUR_set(PL_linestr, new_len);
148
149   PL_bufend = SvPVX(PL_linestr) + new_len;
150
151   if ( DD_DEBUG_UPDATED_LINESTR && PERLDB_LINE && PL_curstash != PL_debstash) {
152     // Cribbed from toke.c
153     SV * const sv = NEWSV(85,0);
154
155     sv_upgrade(sv, SVt_PVMG);
156     sv_setpvn(sv,PL_bufptr,PL_bufend-PL_bufptr);
157     (void)SvIOK_on(sv);
158     SvIV_set(sv, 0);
159     av_store(CopFILEAV(&PL_compiling),(I32)CopLINE(&PL_compiling),sv);
160   }
161 }
162
163 char* dd_get_lex_stuff(pTHX) {
164   return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
165 }
166
167 void dd_clear_lex_stuff(pTHX) {
168   if (DD_HAVE_PARSER)
169     PL_lex_stuff = (SV*)NULL;
170 }
171
172 char* dd_get_curstash_name(pTHX) {
173   return HvNAME(PL_curstash);
174 }
175
176 int dd_get_linestr_offset(pTHX) {
177   char* linestr;
178   if (!DD_HAVE_PARSER) {
179     return -1;
180   }
181   linestr = SvPVX(PL_linestr);
182   return PL_bufptr - linestr;
183 }
184
185 char* dd_move_past_token (pTHX_ char* s) {
186
187   /*
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
191    */
192
193   while (s < PL_bufend && isSPACE(*s)) s++;
194   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
195     s += strlen(PL_tokenbuf);
196   return s;
197 }
198
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);
202   return s - base_s;
203 }
204
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;
208   STRLEN len;
209   char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
210   return s - base_s;
211 }
212
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);
217     return s - base_s;
218 }
219
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");
228   if (s <= base_s) {
229     s += SvCUR(line_copy);
230     sv_catsv(line_copy, PL_linestr);
231     dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
232   }
233   return s - base_s;
234 }
235
236 int dd_toke_skipspace(pTHX_ int offset) {
237   char* old_pvx = SvPVX(PL_linestr);
238   char* base_s = SvPVX(PL_linestr) + offset;
239   char* s = skipspace_force(base_s);
240   if(SvPVX(PL_linestr) != old_pvx)
241     croak("PL_linestr reallocated during skipspace, "
242       "Devel::Declare can't continue");
243   return s - base_s;
244 }
245
246 static void call_done_declare(pTHX) {
247   dSP;
248
249   if (DD_DEBUG_TRACE) {
250     printf("Deconstructing declare\n");
251     printf("PL_bufptr: %s\n", PL_bufptr);
252     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
253     printf("linestr: %s\n", SvPVX(PL_linestr));
254     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
255   }
256
257   ENTER;
258   SAVETMPS;
259
260   PUSHMARK(SP);
261
262   call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
263
264   FREETMPS;
265   LEAVE;
266
267   if (DD_DEBUG_TRACE) {
268     printf("PL_bufptr: %s\n", PL_bufptr);
269     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
270     printf("linestr: %s\n", SvPVX(PL_linestr));
271     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
272     printf("actual len: %i\n", strlen(PL_bufptr));
273   }
274 }
275
276 static int dd_handle_const(pTHX_ char *name);
277
278 /* replacement PL_check rv2cv entry */
279
280 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
281   OP* kid;
282   int dd_flags;
283
284   PERL_UNUSED_VAR(user_data);
285
286   if (!DD_AM_LEXING)
287     return o; /* not lexing? */
288
289   if (in_declare) {
290     call_done_declare(aTHX);
291     return o;
292   }
293
294   kid = cUNOPo->op_first;
295
296   if (kid->op_type != OP_GV) /* not a GV so ignore */
297     return o;
298
299   if (DD_DEBUG_TRACE) {
300     printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
301   }
302
303   dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
304
305   if (dd_flags == -1)
306     return o;
307
308   if (DD_DEBUG_TRACE) {
309     printf("dd_flags are: %i\n", dd_flags);
310     printf("PL_tokenbuf: %s\n", PL_tokenbuf);
311   }
312
313 #if DD_CONST_VIA_RV2CV
314   if (PL_expect != XOPERATOR) {
315     if (!dd_handle_const(aTHX_ GvNAME(kGVOP_gv)))
316       return o;
317     CopLINE(PL_curcop) = PL_copline;
318     /* The parser behaviour that we're simulating depends on what comes
319        after the declarator. */
320     if (*skipspace(PL_bufptr + strlen(GvNAME(kGVOP_gv))) != '(') {
321       if (in_declare) {
322         call_done_declare(aTHX);
323       } else {
324         dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
325       }
326     }
327     return o;
328   }
329 #endif /* DD_CONST_VIA_RV2CV */
330
331   dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
332
333   return o;
334 }
335
336 #if DD_GROW_VIA_BLOCKHOOK
337
338 static void dd_block_start(pTHX_ int full)
339 {
340   PERL_UNUSED_VAR(full);
341   if (SvLEN(PL_linestr) < DD_PREFERRED_LINESTR_SIZE)
342     (void) lex_grow_linestr(DD_PREFERRED_LINESTR_SIZE);
343 }
344
345 #else /* !DD_GROW_VIA_BLOCKHOOK */
346
347 OP* dd_pp_entereval(pTHX) {
348   dSP;
349   STRLEN len;
350   const char* s;
351   SV *sv;
352 #ifdef PERL_5_9_PLUS
353   SV *saved_hh;
354   if (PL_op->op_private & OPpEVAL_HAS_HH) {
355     saved_hh = POPs;
356   }
357 #endif
358   sv = POPs;
359   if (SvPOK(sv)) {
360     if (DD_DEBUG_TRACE) {
361       printf("mangling eval sv\n");
362     }
363     if (SvREADONLY(sv))
364       sv = sv_2mortal(newSVsv(sv));
365     s = SvPVX(sv);
366     len = SvCUR(sv);
367     if (!len || s[len-1] != ';') {
368       if (!(SvFLAGS(sv) & SVs_TEMP))
369         sv = sv_2mortal(newSVsv(sv));
370       sv_catpvn(sv, "\n;", 2);
371     }
372     SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
373   }
374   PUSHs(sv);
375 #ifdef PERL_5_9_PLUS
376   if (PL_op->op_private & OPpEVAL_HAS_HH) {
377     PUSHs(saved_hh);
378   }
379 #endif
380   return PL_ppaddr[OP_ENTEREVAL](aTHX);
381 }
382
383 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
384   PERL_UNUSED_VAR(user_data);
385
386   if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
387     o->op_ppaddr = dd_pp_entereval;
388   return o;
389 }
390
391 #endif /* !DD_GROW_VIA_BLOCKHOOK */
392
393 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
394 {
395   const I32 count = FILTER_READ(idx+1, sv, maxlen);
396   SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
397   /* filter_del(dd_filter_realloc); */
398   return count;
399 }
400
401 static int dd_handle_const(pTHX_ char *name) {
402   switch (PL_lex_inwhat) {
403     case OP_QR:
404     case OP_MATCH:
405     case OP_SUBST:
406     case OP_TRANS:
407     case OP_BACKTICK:
408     case OP_STRINGIFY:
409       return 0;
410       break;
411     default:
412       break;
413   }
414
415   if (strnEQ(PL_bufptr, "->", 2)) {
416     return 0;
417   }
418
419   {
420     char buf[256];
421     STRLEN len;
422     char *s = PL_bufptr;
423     STRLEN old_offset = PL_bufptr - SvPVX(PL_linestr);
424
425     s = scan_word(s, buf, sizeof buf, FALSE, &len);
426     if (strnEQ(buf, name, len)) {
427       char *d;
428       SV *inject = newSVpvn(SvPVX(PL_linestr), PL_bufptr - SvPVX(PL_linestr));
429       sv_catpvn(inject, buf, len);
430
431       d = peekspace(s);
432       sv_catpvn(inject, s, d - s);
433
434       if ((PL_bufend - d) >= 2 && strnEQ(d, "=>", 2)) {
435         return 0;
436       }
437
438       sv_catpv(inject, d);
439       dd_set_linestr(aTHX_ SvPV_nolen(inject));
440       PL_bufptr = SvPVX(PL_linestr) + old_offset;
441       SvREFCNT_dec (inject);
442     }
443   }
444
445   dd_linestr_callback(aTHX_ "const", name);
446
447   return 1;
448 }
449
450 #if !DD_CONST_VIA_RV2CV
451
452 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
453   int dd_flags;
454   char* name;
455
456   PERL_UNUSED_VAR(user_data);
457
458   if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
459     return o;
460   }
461
462   /* if this is set, we just grabbed a delimited string or something,
463      not a bareword, so NO TOUCHY */
464
465   if (DD_HAVE_LEX_STUFF)
466     return o;
467
468   /* don't try and look this up if it's not a string const */
469   if (!SvPOK(cSVOPo->op_sv))
470     return o;
471
472   name = SvPVX(cSVOPo->op_sv);
473
474   dd_flags = dd_is_declarator(aTHX_ name);
475
476   if (dd_flags == -1)
477     return o;
478
479   dd_handle_const(aTHX_ name);
480
481   return o;
482 }
483
484 #endif /* !DD_CONST_VIA_RV2CV */
485
486 static int initialized = 0;
487
488 MODULE = Devel::Declare  PACKAGE = Devel::Declare
489
490 PROTOTYPES: DISABLE
491
492 void
493 setup()
494   CODE:
495   if (!initialized++) {
496 #if DD_GROW_VIA_BLOCKHOOK
497     static BHK bhk;
498 #if PERL_VERSION_GE(5,13,6)
499     BhkENTRY_set(&bhk, bhk_start, dd_block_start);
500 #else /* <5.13.6 */
501     BhkENTRY_set(&bhk, start, dd_block_start);
502 #endif /* <5.13.6 */
503     Perl_blockhook_register(aTHX_ &bhk);
504 #else /* !DD_GROW_VIA_BLOCKHOOK */
505     hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
506 #endif /* !DD_GROW_VIA_BLOCKHOOK */
507     hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
508 #if !DD_CONST_VIA_RV2CV
509     hook_op_check(OP_CONST, dd_ck_const, NULL);
510 #endif /* !DD_CONST_VIA_RV2CV */
511   }
512   filter_add(dd_filter_realloc, NULL);
513
514 char*
515 get_linestr()
516   CODE:
517     RETVAL = dd_get_linestr(aTHX);
518   OUTPUT:
519     RETVAL
520
521 void
522 set_linestr(char* new_value)
523   CODE:
524     dd_set_linestr(aTHX_ new_value);
525
526 char*
527 get_lex_stuff()
528   CODE:
529     RETVAL = dd_get_lex_stuff(aTHX);
530   OUTPUT:
531     RETVAL
532
533 void
534 clear_lex_stuff()
535   CODE:
536     dd_clear_lex_stuff(aTHX);
537
538 char*
539 get_curstash_name()
540   CODE:
541     RETVAL = dd_get_curstash_name(aTHX);
542   OUTPUT:
543     RETVAL
544
545 int
546 get_linestr_offset()
547   CODE:
548     RETVAL = dd_get_linestr_offset(aTHX);
549   OUTPUT:
550     RETVAL
551
552 int
553 toke_scan_word(int offset, int handle_package)
554   CODE:
555     RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
556   OUTPUT:
557     RETVAL
558
559 int
560 toke_move_past_token(int offset);
561   CODE:
562     RETVAL = dd_toke_move_past_token(aTHX_ offset);
563   OUTPUT:
564     RETVAL
565
566 int
567 toke_scan_str(int offset);
568   CODE:
569     RETVAL = dd_toke_scan_str(aTHX_ offset);
570   OUTPUT:
571     RETVAL
572
573 int
574 toke_scan_ident(int offset)
575   CODE:
576     RETVAL = dd_toke_scan_ident(aTHX_ offset);
577   OUTPUT:
578     RETVAL
579
580 int
581 toke_skipspace(int offset)
582   CODE:
583     RETVAL = dd_toke_skipspace(aTHX_ offset);
584   OUTPUT:
585     RETVAL
586
587 int
588 get_in_declare()
589   CODE:
590     RETVAL = in_declare;
591   OUTPUT:
592     RETVAL
593
594 void
595 set_in_declare(int value)
596   CODE:
597     in_declare = value;
598
599 BOOT:
600 {
601   char *endptr;
602   char *debug_str = getenv ("DD_DEBUG");
603   if (debug_str) {
604     dd_debug = strtol (debug_str, &endptr, 10);
605     if (*endptr != '\0') {
606       dd_debug = 0;
607     }
608   }
609 }