ce8d01bc4d3874c582ee968bd971db0f051a6b82
[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 char* dd_move_past_token (pTHX_ char* s) {
139
140   /*
141    *   buffer will be at the beginning of the declarator, -unless- the
142    *   declarator is at EOL in which case it'll be the next useful line
143    *   so we don't short-circuit out if we don't find the declarator
144    */
145
146   while (s < PL_bufend && isSPACE(*s)) s++;
147   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
148     s += strlen(PL_tokenbuf);
149   return s;
150 }
151
152 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
153   char tmpbuf[sizeof PL_tokenbuf];
154   char* base_s = SvPVX(PL_linestr) + offset;
155   STRLEN len;
156   char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
157   return s - base_s;
158 }
159
160 int dd_toke_scan_str(pTHX_ int offset) {
161   char* base_s = SvPVX(PL_linestr) + offset;
162   char* s = scan_str(base_s, FALSE, FALSE);
163   return s - base_s;
164 }
165
166 int dd_toke_skipspace(pTHX_ int offset) {
167   char* base_s = SvPVX(PL_linestr) + offset;
168   char* s = skipspace(base_s);
169   return s - base_s;
170 }
171
172 /* replacement PL_check rv2cv entry */
173
174 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
175
176 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
177   OP* kid;
178   char* s;
179   char* save_s;
180   char tmpbuf[sizeof PL_tokenbuf];
181   char found_name[sizeof PL_tokenbuf];
182   char* found_proto = NULL, *found_traits = NULL;
183   STRLEN len = 0;
184   int dd_flags;
185   char* cb_args[6];
186   dSP; /* define stack pointer for later call stuff */
187   char* retstr;
188   STRLEN n_a; /* for POPpx */
189
190   o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
191
192   if (in_declare) {
193     cb_args[0] = NULL;
194 #ifdef DD_DEBUG
195     printf("Deconstructing declare\n");
196     printf("PL_bufptr: %s\n", PL_bufptr);
197     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
198     printf("linestr: %s\n", SvPVX(PL_linestr));
199     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
200 #endif
201     call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
202     in_declare--;
203 #ifdef DD_DEBUG
204     printf("PL_bufptr: %s\n", PL_bufptr);
205     printf("bufend at: %i\n", PL_bufend - PL_bufptr);
206     printf("linestr: %s\n", SvPVX(PL_linestr));
207     printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
208     printf("actual len: %i\n", strlen(PL_bufptr));
209 #endif
210     return o;
211   }
212
213   kid = cUNOPo->op_first;
214
215   if (kid->op_type != OP_GV) /* not a GV so ignore */
216     return o;
217
218   if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
219     return o; /* not lexing? */
220
221   /* I was doing this, but the CONST wrap can't so it didn't gain anything
222   stash = GvSTASH(kGVOP_gv); */
223
224 #ifdef DD_DEBUG
225   printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
226 #endif
227
228   dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
229
230   if (dd_flags == -1)
231     return o;
232
233 #ifdef DD_DEBUG
234   printf("dd_flags are: %i\n", dd_flags);
235 #endif
236
237   s = PL_bufptr; /* copy the current buffer pointer */
238
239   DD_DEBUG_S
240
241 #ifdef DD_DEBUG
242   printf("PL_tokenbuf: %s\n", PL_tokenbuf);
243 #endif
244
245   s = dd_move_past_token(aTHX_ s);
246
247   DD_DEBUG_S
248
249   if (dd_flags & DD_HANDLE_NAME) {
250
251     /* find next word */
252
253     s = skipspace(s);
254
255     DD_DEBUG_S
256
257     /* kill the :: added in the ck_const */
258     if (*s == ':')
259       *s++ = ' ';
260     if (*s == ':')
261       *s++ = ' ';
262
263     /* arg 4 is allow_package */
264
265     s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
266
267     DD_DEBUG_S
268
269     if (len) {
270       strcpy(found_name, tmpbuf);
271 #ifdef DD_DEBUG
272       printf("Found %s\n", found_name);
273 #endif
274     }
275   }
276
277   if (dd_flags & DD_HANDLE_PROTO) {
278
279     s = skipspace(s);
280
281     if (*s == '(') { /* found a prototype-ish thing */
282       save_s = s;
283       s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
284 #ifdef DD_HAS_TRAITS
285       {
286           char *traitstart = s = skipspace(s);
287
288           while (*s && *s != '{') ++s;
289           if (*s) {
290               int tlen = s - traitstart;
291               Newx(found_traits, tlen+1, char);
292               Copy(traitstart, found_traits, tlen, char);
293               found_traits[tlen] = 0;
294 #ifdef DD_DEBUG
295               printf("found traits..... (%s)\n", found_traits);
296 #endif
297           }
298       }
299 #endif
300       
301       if (SvPOK(PL_lex_stuff)) {
302 #ifdef DD_DEBUG
303         printf("Found proto %s\n", SvPVX(PL_lex_stuff));
304 #endif
305         found_proto = SvPVX(PL_lex_stuff);
306         if (len) /* foo name () => foo name  X, only foo parsed so works */
307           *save_s++ = ' ';
308         else /* foo () => foo =X, TOKEN('&') won't handle foo X */
309           *save_s++ = '=';
310         *save_s++ = 'X';
311         while (save_s < s) {
312           *save_s++ = ' ';
313         }
314 #ifdef DD_DEBUG
315         printf("Curbuf %s\n", PL_bufptr);
316 #endif
317       }
318     }
319   }
320
321   if (!len)
322     found_name[0] = 0;
323
324 #ifdef DD_DEBUG
325   printf("Calling init_declare\n");
326 #endif
327   cb_args[0] = HvNAME(PL_curstash);
328   cb_args[1] = GvNAME(kGVOP_gv);
329   cb_args[2] = HvNAME(PL_curstash);
330   cb_args[3] = found_name;
331   cb_args[4] = found_proto;
332   cb_args[5] = found_traits;
333   cb_args[6] = NULL;
334
335   if (len && found_proto)
336     in_declare = 2;
337   else if (len || found_proto)
338     in_declare = 1;
339   if (found_proto)
340     PL_lex_stuff = Nullsv;
341   s = skipspace(s);
342 #ifdef DD_DEBUG
343   printf("cur buf: %s\n", s);
344   printf("bufend at: %i\n", PL_bufend - s);
345   printf("linestr: %s\n", SvPVX(PL_linestr));
346   printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
347 #endif
348   
349   if (*s++ == '{') {
350     call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
351     SPAGAIN;
352     retstr = POPpx;
353     PUTBACK;
354     if (retstr && strlen(retstr)) {
355       const char* old_start = SvPVX(PL_linestr);
356       int start_diff;
357       const int old_len = SvCUR(PL_linestr);
358 #ifdef DD_DEBUG
359       printf("Got string %s\n", retstr);
360 #endif
361       SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr)));
362       if (start_diff = SvPVX(PL_linestr) - old_start) {
363         Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
364       }
365       memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
366       memmove(s, retstr, strlen(retstr));
367       SvCUR_set(PL_linestr, old_len + strlen(retstr));
368       PL_bufend += strlen(retstr);
369 #ifdef DD_DEBUG
370   printf("cur buf: %s\n", s);
371   printf("PL_bufptr: %s\n", PL_bufptr);
372   printf("bufend at: %i\n", PL_bufend - s);
373   printf("linestr: %s\n", SvPVX(PL_linestr));
374   printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
375   printf("tokenbuf now: %s\n", PL_tokenbuf);
376 #endif
377     }
378   } else {
379     call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
380   }
381   return o;
382 }
383
384 STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op);
385
386 OP* dd_pp_entereval(pTHX) {
387   dSP;
388   dPOPss;
389   STRLEN len;
390   const char* s;
391   if (SvPOK(sv)) {
392 #ifdef DD_DEBUG
393     printf("mangling eval sv\n");
394 #endif
395     if (SvREADONLY(sv))
396       sv = sv_2mortal(newSVsv(sv));
397     s = SvPVX(sv);
398     len = SvCUR(sv);
399     if (!len || s[len-1] != ';') {
400       if (!(SvFLAGS(sv) & SVs_TEMP))
401         sv = sv_2mortal(newSVsv(sv));
402       sv_catpvn(sv, "\n;", 2);
403     }
404     SvGROW(sv, 8192);
405   }
406   PUSHs(sv);
407   return PL_ppaddr[OP_ENTEREVAL](aTHX);
408 }
409
410 STATIC OP *dd_ck_entereval(pTHX_ OP *o) {
411   o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */
412   if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
413     o->op_ppaddr = dd_pp_entereval;
414   return o;
415 }
416
417 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
418 {
419   const I32 count = FILTER_READ(idx+1, sv, maxlen);
420   SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
421   /* filter_del(dd_filter_realloc); */
422   return count;
423 }
424
425 STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op);
426
427 STATIC OP *dd_ck_const(pTHX_ OP *o) {
428   int dd_flags;
429   char* s;
430   char tmpbuf[sizeof PL_tokenbuf];
431   char found_name[sizeof PL_tokenbuf];
432   STRLEN len = 0;
433
434   o = dd_old_ck_const(aTHX_ o); /* let the original do its job */
435
436   /* don't try and look this up if it's not a string const */
437   if (!SvPOK(cSVOPo->op_sv))
438     return o;
439
440   dd_flags = dd_is_declarator(aTHX_ SvPVX(cSVOPo->op_sv));
441
442   if (dd_flags == -1)
443     return o;
444
445   if (!(dd_flags & DD_HANDLE_NAME))
446     return o; /* if we're not handling name, method intuiting not an issue */
447
448 #ifdef DD_DEBUG
449   printf("Think I found a declarator %s\n", PL_tokenbuf);
450   printf("linestr: %s\n", SvPVX(PL_linestr));
451 #endif
452
453   s = PL_bufptr;
454
455   s = dd_move_past_token(aTHX_ s);
456
457   /* dd_linestr_callback(aTHX_ "const", SvPVX(cSVOPo->op_sv), s); */
458
459   DD_DEBUG_S
460
461   /* find next word */
462
463   s = skipspace(s);
464
465   DD_DEBUG_S
466
467   /* arg 4 is allow_package */
468
469   s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
470
471   DD_DEBUG_S
472
473   if (len) {
474     const char* old_start = SvPVX(PL_linestr);
475     int start_diff;
476     const int old_len = SvCUR(PL_linestr);
477
478     strcpy(found_name, tmpbuf);
479 #ifdef DD_DEBUG
480     printf("Found %s\n", found_name);
481 #endif
482
483     s -= len;
484     SvGROW(PL_linestr, (STRLEN)(old_len + 2));
485     if (start_diff = SvPVX(PL_linestr) - old_start) {
486       Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
487     }
488     memmove(s+2, s, (PL_bufend - s)+1);
489     *s = ':';
490     s++;
491     *s = ':';
492     SvCUR_set(PL_linestr, old_len + 2);
493     PL_bufend += 2;
494   }
495   return o;  
496 }
497
498 static int initialized = 0;
499
500 MODULE = Devel::Declare  PACKAGE = Devel::Declare
501
502 PROTOTYPES: DISABLE
503
504 void
505 setup()
506   CODE:
507   if (!initialized++) {
508     dd_old_ck_rv2cv = PL_check[OP_RV2CV];
509     PL_check[OP_RV2CV] = dd_ck_rv2cv;
510     dd_old_ck_entereval = PL_check[OP_ENTEREVAL];
511     PL_check[OP_ENTEREVAL] = dd_ck_entereval;
512     dd_old_ck_const = PL_check[OP_CONST];
513     PL_check[OP_CONST] = dd_ck_const;
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 int
530 toke_scan_word(int offset, int handle_package)
531   CODE:
532     RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
533   OUTPUT:
534     RETVAL
535
536 int
537 toke_scan_str(int offset);
538   CODE:
539     RETVAL = dd_toke_scan_str(aTHX_ offset);
540   OUTPUT:
541     RETVAL
542
543 int
544 toke_skipspace(int offset)
545   CODE:
546     RETVAL = dd_toke_skipspace(aTHX_ offset);
547   OUTPUT:
548     RETVAL