41a991b929212c9cd0f85243308978bb159c3377
[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   STRLEN remaining = sv_len(PL_linestr) - offset;
223   SV* line_copy = newSVsv(PL_linestr);
224   char* base_s = SvPVX(PL_linestr) + offset;
225   char* s = scan_str(base_s, FALSE, FALSE);
226   if(SvPVX(PL_linestr) != old_pvx)
227     croak("PL_linestr reallocated during scan_str, "
228       "Devel::Declare can't continue");
229   if (s != base_s && sv_len(PL_lex_stuff) > remaining) {
230     int ret = (s - SvPVX(PL_linestr)) + remaining;
231     sv_catsv(line_copy, PL_linestr);
232     dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
233     SvREFCNT_dec(line_copy);
234     return ret;
235   }
236   return s - base_s;
237 }
238
239 int dd_toke_skipspace(pTHX_ int offset) {
240   char* old_pvx = SvPVX(PL_linestr);
241   char* base_s = SvPVX(PL_linestr) + offset;
242   char* s = skipspace_force(base_s);
243   if(SvPVX(PL_linestr) != old_pvx)
244     croak("PL_linestr reallocated during skipspace, "
245       "Devel::Declare can't continue");
246   return s - base_s;
247 }
248
249 static void call_done_declare(pTHX) {
250   dSP;
251
252   if (DD_DEBUG_TRACE) {
253     printf("Deconstructing declare\n");
254     printf("PL_bufptr: %s\n", PL_bufptr);
255     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
256     printf("linestr: %s\n", SvPVX(PL_linestr));
257     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
258   }
259
260   ENTER;
261   SAVETMPS;
262
263   PUSHMARK(SP);
264
265   call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
266
267   FREETMPS;
268   LEAVE;
269
270   if (DD_DEBUG_TRACE) {
271     printf("PL_bufptr: %s\n", PL_bufptr);
272     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
273     printf("linestr: %s\n", SvPVX(PL_linestr));
274     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
275     printf("actual len: %i\n", strlen(PL_bufptr));
276   }
277 }
278
279 static int dd_handle_const(pTHX_ char *name);
280
281 /* replacement PL_check rv2cv entry */
282
283 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
284   OP* kid;
285   int dd_flags;
286
287   PERL_UNUSED_VAR(user_data);
288
289   if (!DD_AM_LEXING)
290     return o; /* not lexing? */
291
292   if (in_declare) {
293     call_done_declare(aTHX);
294     return o;
295   }
296
297   kid = cUNOPo->op_first;
298
299   if (kid->op_type != OP_GV) /* not a GV so ignore */
300     return o;
301
302   if (DD_DEBUG_TRACE) {
303     printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
304   }
305
306   dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
307
308   if (dd_flags == -1)
309     return o;
310
311   if (DD_DEBUG_TRACE) {
312     printf("dd_flags are: %i\n", dd_flags);
313     printf("PL_tokenbuf: %s\n", PL_tokenbuf);
314   }
315
316 #if DD_CONST_VIA_RV2CV
317   if (PL_expect != XOPERATOR) {
318     if (!dd_handle_const(aTHX_ GvNAME(kGVOP_gv)))
319       return o;
320     CopLINE(PL_curcop) = PL_copline;
321     /* The parser behaviour that we're simulating depends on what comes
322        after the declarator. */
323     if (*skipspace(PL_bufptr + strlen(GvNAME(kGVOP_gv))) != '(') {
324       if (in_declare) {
325         call_done_declare(aTHX);
326       } else {
327         dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
328       }
329     }
330     return o;
331   }
332 #endif /* DD_CONST_VIA_RV2CV */
333
334   dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
335
336   return o;
337 }
338
339 #if DD_GROW_VIA_BLOCKHOOK
340
341 static void dd_block_start(pTHX_ int full)
342 {
343   PERL_UNUSED_VAR(full);
344   if (SvLEN(PL_linestr) < DD_PREFERRED_LINESTR_SIZE)
345     (void) lex_grow_linestr(DD_PREFERRED_LINESTR_SIZE);
346 }
347
348 #else /* !DD_GROW_VIA_BLOCKHOOK */
349
350 OP* dd_pp_entereval(pTHX) {
351   dSP;
352   STRLEN len;
353   const char* s;
354   SV *sv;
355 #ifdef PERL_5_9_PLUS
356   SV *saved_hh;
357   if (PL_op->op_private & OPpEVAL_HAS_HH) {
358     saved_hh = POPs;
359   }
360 #endif
361   sv = POPs;
362   if (SvPOK(sv)) {
363     if (DD_DEBUG_TRACE) {
364       printf("mangling eval sv\n");
365     }
366     if (SvREADONLY(sv))
367       sv = sv_2mortal(newSVsv(sv));
368     s = SvPVX(sv);
369     len = SvCUR(sv);
370     if (!len || s[len-1] != ';') {
371       if (!(SvFLAGS(sv) & SVs_TEMP))
372         sv = sv_2mortal(newSVsv(sv));
373       sv_catpvn(sv, "\n;", 2);
374     }
375     SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
376   }
377   PUSHs(sv);
378 #ifdef PERL_5_9_PLUS
379   if (PL_op->op_private & OPpEVAL_HAS_HH) {
380     PUSHs(saved_hh);
381   }
382 #endif
383   return PL_ppaddr[OP_ENTEREVAL](aTHX);
384 }
385
386 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
387   PERL_UNUSED_VAR(user_data);
388
389   if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
390     o->op_ppaddr = dd_pp_entereval;
391   return o;
392 }
393
394 #endif /* !DD_GROW_VIA_BLOCKHOOK */
395
396 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
397 {
398   const I32 count = FILTER_READ(idx+1, sv, maxlen);
399   SvGROW(sv, DD_PREFERRED_LINESTR_SIZE);
400   /* filter_del(dd_filter_realloc); */
401   return count;
402 }
403
404 static int dd_handle_const(pTHX_ char *name) {
405   switch (PL_lex_inwhat) {
406     case OP_QR:
407     case OP_MATCH:
408     case OP_SUBST:
409     case OP_TRANS:
410     case OP_BACKTICK:
411     case OP_STRINGIFY:
412       return 0;
413       break;
414     default:
415       break;
416   }
417
418   if (strnEQ(PL_bufptr, "->", 2)) {
419     return 0;
420   }
421
422   {
423     char buf[256];
424     STRLEN len;
425     char *s = PL_bufptr;
426     STRLEN old_offset = PL_bufptr - SvPVX(PL_linestr);
427
428     s = scan_word(s, buf, sizeof buf, FALSE, &len);
429     if (strnEQ(buf, name, len)) {
430       char *d;
431       SV *inject = newSVpvn(SvPVX(PL_linestr), PL_bufptr - SvPVX(PL_linestr));
432       sv_catpvn(inject, buf, len);
433
434       d = peekspace(s);
435       sv_catpvn(inject, s, d - s);
436
437       if ((PL_bufend - d) >= 2 && strnEQ(d, "=>", 2)) {
438         return 0;
439       }
440
441       sv_catpv(inject, d);
442       dd_set_linestr(aTHX_ SvPV_nolen(inject));
443       PL_bufptr = SvPVX(PL_linestr) + old_offset;
444       SvREFCNT_dec (inject);
445     }
446   }
447
448   dd_linestr_callback(aTHX_ "const", name);
449
450   return 1;
451 }
452
453 #if !DD_CONST_VIA_RV2CV
454
455 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
456   int dd_flags;
457   char* name;
458
459   PERL_UNUSED_VAR(user_data);
460
461   if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
462     return o;
463   }
464
465   /* if this is set, we just grabbed a delimited string or something,
466      not a bareword, so NO TOUCHY */
467
468   if (DD_HAVE_LEX_STUFF)
469     return o;
470
471   /* don't try and look this up if it's not a string const */
472   if (!SvPOK(cSVOPo->op_sv))
473     return o;
474
475   name = SvPVX(cSVOPo->op_sv);
476
477   dd_flags = dd_is_declarator(aTHX_ name);
478
479   if (dd_flags == -1)
480     return o;
481
482   dd_handle_const(aTHX_ name);
483
484   return o;
485 }
486
487 #endif /* !DD_CONST_VIA_RV2CV */
488
489 static int initialized = 0;
490
491 MODULE = Devel::Declare  PACKAGE = Devel::Declare
492
493 PROTOTYPES: DISABLE
494
495 void
496 setup()
497   CODE:
498   if (!initialized++) {
499 #if DD_GROW_VIA_BLOCKHOOK
500     static BHK bhk;
501 #if PERL_VERSION_GE(5,13,6)
502     BhkENTRY_set(&bhk, bhk_start, dd_block_start);
503 #else /* <5.13.6 */
504     BhkENTRY_set(&bhk, start, dd_block_start);
505 #endif /* <5.13.6 */
506     Perl_blockhook_register(aTHX_ &bhk);
507 #else /* !DD_GROW_VIA_BLOCKHOOK */
508     hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
509 #endif /* !DD_GROW_VIA_BLOCKHOOK */
510     hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
511 #if !DD_CONST_VIA_RV2CV
512     hook_op_check(OP_CONST, dd_ck_const, NULL);
513 #endif /* !DD_CONST_VIA_RV2CV */
514   }
515   filter_add(dd_filter_realloc, NULL);
516
517 char*
518 get_linestr()
519   CODE:
520     RETVAL = dd_get_linestr(aTHX);
521   OUTPUT:
522     RETVAL
523
524 void
525 set_linestr(char* new_value)
526   CODE:
527     dd_set_linestr(aTHX_ new_value);
528
529 char*
530 get_lex_stuff()
531   CODE:
532     RETVAL = dd_get_lex_stuff(aTHX);
533   OUTPUT:
534     RETVAL
535
536 void
537 clear_lex_stuff()
538   CODE:
539     dd_clear_lex_stuff(aTHX);
540
541 char*
542 get_curstash_name()
543   CODE:
544     RETVAL = dd_get_curstash_name(aTHX);
545   OUTPUT:
546     RETVAL
547
548 int
549 get_linestr_offset()
550   CODE:
551     RETVAL = dd_get_linestr_offset(aTHX);
552   OUTPUT:
553     RETVAL
554
555 int
556 toke_scan_word(int offset, int handle_package)
557   CODE:
558     RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
559   OUTPUT:
560     RETVAL
561
562 int
563 toke_move_past_token(int offset);
564   CODE:
565     RETVAL = dd_toke_move_past_token(aTHX_ offset);
566   OUTPUT:
567     RETVAL
568
569 int
570 toke_scan_str(int offset);
571   CODE:
572     RETVAL = dd_toke_scan_str(aTHX_ offset);
573   OUTPUT:
574     RETVAL
575
576 int
577 toke_scan_ident(int offset)
578   CODE:
579     RETVAL = dd_toke_scan_ident(aTHX_ offset);
580   OUTPUT:
581     RETVAL
582
583 int
584 toke_skipspace(int offset)
585   CODE:
586     RETVAL = dd_toke_skipspace(aTHX_ offset);
587   OUTPUT:
588     RETVAL
589
590 int
591 get_in_declare()
592   CODE:
593     RETVAL = in_declare;
594   OUTPUT:
595     RETVAL
596
597 void
598 set_in_declare(int value)
599   CODE:
600     in_declare = value;
601
602 BOOT:
603 {
604   char *endptr;
605   char *debug_str = getenv ("DD_DEBUG");
606   if (debug_str) {
607     dd_debug = strtol (debug_str, &endptr, 10);
608     if (*endptr != '\0') {
609       dd_debug = 0;
610     }
611   }
612 }