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