get_linestr works, callback code works, set_linestr compiles but not tested
[p5sagit/Devel-Declare.git] / Declare.xs
1 #define PERL_CORE
2 #define PERL_NO_GET_CONTEXT
3 #include "EXTERN.h"
4 #include "perl.h"
5 #include "XSUB.h"
6 #undef printf
7 #include "stolen_chunk_of_toke.c"
8 #include <stdio.h>
9 #include <string.h>
10
11 #ifndef Newx
12 # define Newx(v,n,t) New(0,v,n,t)
13 #endif /* !Newx */
14
15 #if 1
16 #define DD_HAS_TRAITS
17 #endif
18
19 #if 0
20 #define DD_DEBUG
21 #endif
22
23 #define DD_HANDLE_NAME 1
24 #define DD_HANDLE_PROTO 2
25 #define DD_HANDLE_PACKAGE 8
26
27 #ifdef DD_DEBUG
28 #define DD_DEBUG_S printf("Buffer: %s\n", s);
29 #else
30 #define DD_DEBUG_S
31 #endif
32
33 #define LEX_NORMAL    10
34 #define LEX_INTERPNORMAL   9
35
36 /* flag to trigger removal of temporary declaree sub */
37
38 static int in_declare = 0;
39
40 /* thing that decides whether we're dealing with a declarator */
41
42 int dd_is_declarator(pTHX_ char* name) {
43   HV* is_declarator;
44   SV** is_declarator_pack_ref;
45   HV* is_declarator_pack_hash;
46   SV** is_declarator_flag_ref;
47   int dd_flags;
48
49   is_declarator = get_hv("Devel::Declare::declarators", FALSE);
50
51   if (!is_declarator)
52     return -1;
53
54   /* $declarators{$current_package_name} */
55
56   is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
57                              strlen(HvNAME(PL_curstash)), FALSE);
58
59   if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
60     return -1; /* not a hashref */
61
62   is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
63
64   /* $declarators{$current_package_name}{$name} */
65
66   is_declarator_flag_ref = hv_fetch(
67     is_declarator_pack_hash, name,
68     strlen(name), FALSE
69   );
70
71   /* requires SvIOK as well as TRUE since flags not being an int is useless */
72
73   if (!is_declarator_flag_ref
74         || !SvIOK(*is_declarator_flag_ref) 
75         || !SvTRUE(*is_declarator_flag_ref))
76     return -1;
77
78   dd_flags = SvIVX(*is_declarator_flag_ref);
79
80   return dd_flags;
81 }
82
83 /* callback thingy */
84
85 void dd_linestr_callback (pTHX_ char* type, char* name, char* s) {
86
87   char* linestr = SvPVX(PL_linestr);
88   int offset = s - linestr;
89
90   char* new_linestr;
91   int count;
92
93   dSP;
94
95   ENTER;
96   SAVETMPS;
97
98   PUSHMARK(SP);
99   XPUSHs(sv_2mortal(newSVpv(type, 0)));
100   XPUSHs(sv_2mortal(newSVpv(name, 0)));
101   XPUSHs(sv_2mortal(newSViv(offset)));
102   PUTBACK;
103
104   count = call_pv("Devel::Declare::linestr_callback", G_SCALAR);
105
106   SPAGAIN;
107
108   if (count != 1)
109     Perl_croak(aTHX_ "linestr_callback didn't return a value, bailing out");
110
111   printf("linestr_callback returned: %s\n", POPp);
112
113   PUTBACK;
114   FREETMPS;
115   LEAVE;
116 }
117
118 char* dd_get_linestr(pTHX) {
119   return SvPVX(PL_linestr);
120 }
121
122 void dd_set_linestr(pTHX_ char* new_value) {
123   int new_len = strlen(new_value);
124   char* old_linestr = SvPVX(PL_linestr);
125
126   SvGROW(PL_linestr, strlen(new_value));
127
128   if (SvPVX(PL_linestr) != old_linestr)
129     Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
130
131   memcpy(SvPVX(PL_linestr), new_value, new_len+1);
132
133   SvCUR_set(PL_linestr, new_len);
134
135   PL_bufend = SvPVX(PL_linestr) + new_len;
136 }
137
138 /* replacement PL_check rv2cv entry */
139
140 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
141
142 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
143   OP* kid;
144   char* s;
145   char* save_s;
146   char tmpbuf[sizeof PL_tokenbuf];
147   char found_name[sizeof PL_tokenbuf];
148   char* found_proto = NULL, *found_traits = NULL;
149   STRLEN len = 0;
150   int dd_flags;
151   char* cb_args[6];
152   dSP; /* define stack pointer for later call stuff */
153   char* retstr;
154   STRLEN n_a; /* for POPpx */
155
156   o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
157
158   if (in_declare) {
159     cb_args[0] = NULL;
160 #ifdef DD_DEBUG
161     printf("Deconstructing declare\n");
162     printf("PL_bufptr: %s\n", PL_bufptr);
163     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
164     printf("linestr: %s\n", SvPVX(PL_linestr));
165     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
166 #endif
167     call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
168     in_declare--;
169 #ifdef DD_DEBUG
170     printf("PL_bufptr: %s\n", PL_bufptr);
171     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
172     printf("linestr: %s\n", SvPVX(PL_linestr));
173     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
174     printf("actual len: %i\n", strlen(PL_bufptr));
175 #endif
176     return o;
177   }
178
179   kid = cUNOPo->op_first;
180
181   if (kid->op_type != OP_GV) /* not a GV so ignore */
182     return o;
183
184   if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
185     return o; /* not lexing? */
186
187   /* I was doing this, but the CONST wrap can't so it didn't gain anything
188   stash = GvSTASH(kGVOP_gv); */
189
190 #ifdef DD_DEBUG
191   printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
192 #endif
193
194   dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
195
196   if (dd_flags == -1)
197     return o;
198
199 #ifdef DD_DEBUG
200   printf("dd_flags are: %i\n", dd_flags);
201 #endif
202
203   s = PL_bufptr; /* copy the current buffer pointer */
204
205   DD_DEBUG_S
206
207 #ifdef DD_DEBUG
208   printf("PL_tokenbuf: %s\n", PL_tokenbuf);
209 #endif
210
211   /*
212    *   buffer will be at the beginning of the declarator, -unless- the
213    *   declarator is at EOL in which case it'll be the next useful line
214    *   so we don't short-circuit out if we don't find the declarator
215    */
216
217   while (s < PL_bufend && isSPACE(*s)) s++;
218   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
219     s += strlen(PL_tokenbuf);
220
221   DD_DEBUG_S
222
223   if (dd_flags & DD_HANDLE_NAME) {
224
225     /* find next word */
226
227     s = skipspace(s);
228
229     DD_DEBUG_S
230
231     /* kill the :: added in the ck_const */
232     if (*s == ':')
233       *s++ = ' ';
234     if (*s == ':')
235       *s++ = ' ';
236
237     /* arg 4 is allow_package */
238
239     s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
240
241     DD_DEBUG_S
242
243     if (len) {
244       strcpy(found_name, tmpbuf);
245 #ifdef DD_DEBUG
246       printf("Found %s\n", found_name);
247 #endif
248     }
249   }
250
251   if (dd_flags & DD_HANDLE_PROTO) {
252
253     s = skipspace(s);
254
255     if (*s == '(') { /* found a prototype-ish thing */
256       save_s = s;
257       s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
258 #ifdef DD_HAS_TRAITS
259       {
260           char *traitstart = s = skipspace(s);
261
262           while (*s && *s != '{') ++s;
263           if (*s) {
264               int tlen = s - traitstart;
265               Newx(found_traits, tlen+1, char);
266               Copy(traitstart, found_traits, tlen, char);
267               found_traits[tlen] = 0;
268 #ifdef DD_DEBUG
269               printf("found traits..... (%s)\n", found_traits);
270 #endif
271           }
272       }
273 #endif
274       
275       if (SvPOK(PL_lex_stuff)) {
276 #ifdef DD_DEBUG
277         printf("Found proto %s\n", SvPVX(PL_lex_stuff));
278 #endif
279         found_proto = SvPVX(PL_lex_stuff);
280         if (len) /* foo name () => foo name  X, only foo parsed so works */
281           *save_s++ = ' ';
282         else /* foo () => foo =X, TOKEN('&') won't handle foo X */
283           *save_s++ = '=';
284         *save_s++ = 'X';
285         while (save_s < s) {
286           *save_s++ = ' ';
287         }
288 #ifdef DD_DEBUG
289         printf("Curbuf %s\n", PL_bufptr);
290 #endif
291       }
292     }
293   }
294
295   if (!len)
296     found_name[0] = 0;
297
298 #ifdef DD_DEBUG
299   printf("Calling init_declare\n");
300 #endif
301   cb_args[0] = HvNAME(PL_curstash);
302   cb_args[1] = GvNAME(kGVOP_gv);
303   cb_args[2] = HvNAME(PL_curstash);
304   cb_args[3] = found_name;
305   cb_args[4] = found_proto;
306   cb_args[5] = found_traits;
307   cb_args[6] = NULL;
308
309   if (len && found_proto)
310     in_declare = 2;
311   else if (len || found_proto)
312     in_declare = 1;
313   if (found_proto)
314     PL_lex_stuff = Nullsv;
315   s = skipspace(s);
316 #ifdef DD_DEBUG
317   printf("cur buf: %s\n", s);
318   printf("bufend at: %i\n", PL_bufend - s);
319   printf("linestr: %s\n", SvPVX(PL_linestr));
320   printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
321 #endif
322   
323   if (*s++ == '{') {
324     call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
325     SPAGAIN;
326     retstr = POPpx;
327     PUTBACK;
328     if (retstr && strlen(retstr)) {
329       const char* old_start = SvPVX(PL_linestr);
330       int start_diff;
331       const int old_len = SvCUR(PL_linestr);
332 #ifdef DD_DEBUG
333       printf("Got string %s\n", retstr);
334 #endif
335       SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr)));
336       if (start_diff = SvPVX(PL_linestr) - old_start) {
337         Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
338       }
339       memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
340       memmove(s, retstr, strlen(retstr));
341       SvCUR_set(PL_linestr, old_len + strlen(retstr));
342       PL_bufend += strlen(retstr);
343 #ifdef DD_DEBUG
344   printf("cur buf: %s\n", s);
345   printf("PL_bufptr: %s\n", PL_bufptr);
346   printf("bufend at: %i\n", PL_bufend - s);
347   printf("linestr: %s\n", SvPVX(PL_linestr));
348   printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
349   printf("tokenbuf now: %s\n", PL_tokenbuf);
350 #endif
351     }
352   } else {
353     call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
354   }
355   return o;
356 }
357
358 STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op);
359
360 OP* dd_pp_entereval(pTHX) {
361   dSP;
362   dPOPss;
363   STRLEN len;
364   const char* s;
365   if (SvPOK(sv)) {
366 #ifdef DD_DEBUG
367     printf("mangling eval sv\n");
368 #endif
369     if (SvREADONLY(sv))
370       sv = sv_2mortal(newSVsv(sv));
371     s = SvPVX(sv);
372     len = SvCUR(sv);
373     if (!len || s[len-1] != ';') {
374       if (!(SvFLAGS(sv) & SVs_TEMP))
375         sv = sv_2mortal(newSVsv(sv));
376       sv_catpvn(sv, "\n;", 2);
377     }
378     SvGROW(sv, 8192);
379   }
380   PUSHs(sv);
381   return PL_ppaddr[OP_ENTEREVAL](aTHX);
382 }
383
384 STATIC OP *dd_ck_entereval(pTHX_ OP *o) {
385   o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */
386   if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
387     o->op_ppaddr = dd_pp_entereval;
388   return o;
389 }
390
391 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
392 {
393   const I32 count = FILTER_READ(idx+1, sv, maxlen);
394   SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
395   /* filter_del(dd_filter_realloc); */
396   return count;
397 }
398
399 STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op);
400
401 STATIC OP *dd_ck_const(pTHX_ OP *o) {
402   int dd_flags;
403   char* s;
404   char tmpbuf[sizeof PL_tokenbuf];
405   char found_name[sizeof PL_tokenbuf];
406   STRLEN len = 0;
407
408   o = dd_old_ck_const(aTHX_ o); /* let the original do its job */
409
410   /* don't try and look this up if it's not a string const */
411   if (!SvPOK(cSVOPo->op_sv))
412     return o;
413
414   dd_flags = dd_is_declarator(aTHX_ SvPVX(cSVOPo->op_sv));
415
416   if (dd_flags == -1)
417     return o;
418
419   if (!(dd_flags & DD_HANDLE_NAME))
420     return o; /* if we're not handling name, method intuiting not an issue */
421
422 #ifdef DD_DEBUG
423   printf("Think I found a declarator %s\n", PL_tokenbuf);
424   printf("linestr: %s\n", SvPVX(PL_linestr));
425 #endif
426
427   s = PL_bufptr;
428
429   while (s < PL_bufend && isSPACE(*s)) s++;
430   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
431     s += strlen(PL_tokenbuf);
432
433   /* dd_linestr_callback(aTHX_ "const", SvPVX(cSVOPo->op_sv), s); */
434
435   DD_DEBUG_S
436
437   /* find next word */
438
439   s = skipspace(s);
440
441   DD_DEBUG_S
442
443   /* arg 4 is allow_package */
444
445   s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
446
447   DD_DEBUG_S
448
449   if (len) {
450     const char* old_start = SvPVX(PL_linestr);
451     int start_diff;
452     const int old_len = SvCUR(PL_linestr);
453
454     strcpy(found_name, tmpbuf);
455 #ifdef DD_DEBUG
456     printf("Found %s\n", found_name);
457 #endif
458
459     s -= len;
460     SvGROW(PL_linestr, (STRLEN)(old_len + 2));
461     if (start_diff = SvPVX(PL_linestr) - old_start) {
462       Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
463     }
464     memmove(s+2, s, (PL_bufend - s)+1);
465     *s = ':';
466     s++;
467     *s = ':';
468     SvCUR_set(PL_linestr, old_len + 2);
469     PL_bufend += 2;
470   }
471   return o;  
472 }
473
474 static int initialized = 0;
475
476 MODULE = Devel::Declare  PACKAGE = Devel::Declare
477
478 PROTOTYPES: DISABLE
479
480 void
481 setup()
482   CODE:
483   if (!initialized++) {
484     dd_old_ck_rv2cv = PL_check[OP_RV2CV];
485     PL_check[OP_RV2CV] = dd_ck_rv2cv;
486     dd_old_ck_entereval = PL_check[OP_ENTEREVAL];
487     PL_check[OP_ENTEREVAL] = dd_ck_entereval;
488     dd_old_ck_const = PL_check[OP_CONST];
489     PL_check[OP_CONST] = dd_ck_const;
490   }
491   filter_add(dd_filter_realloc, NULL);
492
493 char*
494 get_linestr()
495   CODE:
496     RETVAL = dd_get_linestr(aTHX);
497   OUTPUT:
498     RETVAL