Port to B::Hooks::Parser.
[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 #ifndef Newx
11 # define Newx(v,n,t) New(0,v,n,t)
12 #endif /* !Newx */
13
14 static int dd_debug = 0;
15
16 #define LEX_NORMAL    10
17 #define LEX_INTERPNORMAL   9
18
19 /* flag to trigger removal of temporary declaree sub */
20
21 static int in_declare = 0;
22
23 /* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff
24    is a lookup into it - so if anything else we can use to tell, so we
25    need to be a bit more careful if PL_parser exists */
26
27 #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL)
28
29 #if defined(PL_parser) || defined(PERL_5_9_PLUS)
30 #define DD_HAVE_PARSER PL_parser
31 #define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff)
32 #define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK)
33 #else
34 #define DD_HAVE_PARSER 1
35 #define DD_HAVE_LEX_STUFF PL_lex_stuff
36 #define DD_AM_LEXING DD_AM_LEXING_CHECK
37 #endif
38
39 /* thing that decides whether we're dealing with a declarator */
40
41 int dd_is_declarator(pTHX_ char* name) {
42   HV* is_declarator;
43   SV** is_declarator_pack_ref;
44   HV* is_declarator_pack_hash;
45   SV** is_declarator_flag_ref;
46   int dd_flags;
47
48   is_declarator = get_hv("Devel::Declare::declarators", FALSE);
49
50   if (!is_declarator)
51     return -1;
52
53   /* $declarators{$current_package_name} */
54
55   if (!HvNAME(PL_curstash))
56           return -1;
57
58   is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
59                              strlen(HvNAME(PL_curstash)), FALSE);
60
61   if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
62     return -1; /* not a hashref */
63
64   is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
65
66   /* $declarators{$current_package_name}{$name} */
67
68   is_declarator_flag_ref = hv_fetch(
69     is_declarator_pack_hash, name,
70     strlen(name), FALSE
71   );
72
73   /* requires SvIOK as well as TRUE since flags not being an int is useless */
74
75   if (!is_declarator_flag_ref
76         || !SvIOK(*is_declarator_flag_ref) 
77         || !SvTRUE(*is_declarator_flag_ref))
78     return -1;
79
80   dd_flags = SvIVX(*is_declarator_flag_ref);
81
82   return dd_flags;
83 }
84
85 /* callback thingy */
86
87 void dd_linestr_callback (pTHX_ char* type, char* name) {
88
89   char* linestr = SvPVX(PL_linestr);
90   int offset = PL_bufptr - linestr;
91
92   dSP;
93
94   ENTER;
95   SAVETMPS;
96
97   PUSHMARK(SP);
98   XPUSHs(sv_2mortal(newSVpv(type, 0)));
99   XPUSHs(sv_2mortal(newSVpv(name, 0)));
100   XPUSHs(sv_2mortal(newSViv(offset)));
101   PUTBACK;
102
103   call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
104
105   FREETMPS;
106   LEAVE;
107 }
108
109 char* dd_get_lex_stuff(pTHX) {
110   return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
111 }
112
113 void dd_clear_lex_stuff(pTHX) {
114   if (DD_HAVE_PARSER)
115     PL_lex_stuff = (SV*)NULL;
116 }
117
118 char* dd_get_curstash_name(pTHX) {
119   return HvNAME(PL_curstash);
120 }
121
122 char* dd_move_past_token (pTHX_ char* s) {
123
124   /*
125    *   buffer will be at the beginning of the declarator, -unless- the
126    *   declarator is at EOL in which case it'll be the next useful line
127    *   so we don't short-circuit out if we don't find the declarator
128    */
129
130   while (s < PL_bufend && isSPACE(*s)) s++;
131   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
132     s += strlen(PL_tokenbuf);
133   return s;
134 }
135
136 int dd_toke_move_past_token (pTHX_ int offset) {
137   char* base_s = SvPVX(PL_linestr) + offset;
138   char* s = dd_move_past_token(aTHX_ base_s);
139   return s - base_s;
140 }
141
142 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
143   char tmpbuf[sizeof PL_tokenbuf];
144   char* base_s = SvPVX(PL_linestr) + offset;
145   STRLEN len;
146   char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
147   return s - base_s;
148 }
149
150 int dd_toke_scan_str(pTHX_ int offset) {
151   char* base_s = SvPVX(PL_linestr) + offset;
152   char* s = scan_str(base_s, FALSE, FALSE);
153   return s - base_s;
154 }
155
156 int dd_toke_skipspace(pTHX_ int offset) {
157   char* base_s = SvPVX(PL_linestr) + offset;
158   char* s = skipspace(base_s);
159   return s - base_s;
160 }
161
162 /* replacement PL_check rv2cv entry */
163
164 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
165   OP* kid;
166   int dd_flags;
167
168   if (in_declare) {
169     if (dd_debug) {
170       printf("Deconstructing declare\n");
171       printf("PL_bufptr: %s\n", PL_bufptr);
172       printf("bufend at: %i\n", PL_bufend - PL_bufptr);
173       printf("linestr: %s\n", SvPVX(PL_linestr));
174       printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
175     }
176
177     dSP;
178   
179     ENTER;
180     SAVETMPS;
181   
182     PUSHMARK(SP);
183   
184     call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
185
186     FREETMPS;
187     LEAVE;
188
189     if (dd_debug) {
190       printf("PL_bufptr: %s\n", PL_bufptr);
191       printf("bufend at: %i\n", PL_bufend - PL_bufptr);
192       printf("linestr: %s\n", SvPVX(PL_linestr));
193       printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
194       printf("actual len: %i\n", strlen(PL_bufptr));
195     }
196     return o;
197   }
198
199   kid = cUNOPo->op_first;
200
201   if (kid->op_type != OP_GV) /* not a GV so ignore */
202     return o;
203
204   if (!DD_AM_LEXING)
205     return o; /* not lexing? */
206
207   if (dd_debug) {
208     printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
209   }
210
211   dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
212
213   if (dd_flags == -1)
214     return o;
215
216   if (dd_debug) {
217     printf("dd_flags are: %i\n", dd_flags);
218     printf("PL_tokenbuf: %s\n", PL_tokenbuf);
219   }
220
221   dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
222
223   return o;
224 }
225
226 OP* dd_pp_entereval(pTHX) {
227   dSP;
228   dPOPss;
229   STRLEN len;
230   const char* s;
231   if (SvPOK(sv)) {
232     if (dd_debug) {
233       printf("mangling eval sv\n");
234     }
235     if (SvREADONLY(sv))
236       sv = sv_2mortal(newSVsv(sv));
237     s = SvPVX(sv);
238     len = SvCUR(sv);
239     if (!len || s[len-1] != ';') {
240       if (!(SvFLAGS(sv) & SVs_TEMP))
241         sv = sv_2mortal(newSVsv(sv));
242       sv_catpvn(sv, "\n;", 2);
243     }
244     SvGROW(sv, 8192);
245   }
246   PUSHs(sv);
247   return PL_ppaddr[OP_ENTEREVAL](aTHX);
248 }
249
250 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
251   if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
252     o->op_ppaddr = dd_pp_entereval;
253   return o;
254 }
255
256 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
257 {
258   const I32 count = FILTER_READ(idx+1, sv, maxlen);
259   SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
260   /* filter_del(dd_filter_realloc); */
261   return count;
262 }
263
264 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
265   int dd_flags;
266   char* name;
267
268   /* if this is set, we just grabbed a delimited string or something,
269      not a bareword, so NO TOUCHY */
270
271   if (DD_HAVE_LEX_STUFF)
272     return o;
273
274   /* don't try and look this up if it's not a string const */
275   if (!SvPOK(cSVOPo->op_sv))
276     return o;
277
278   name = SvPVX(cSVOPo->op_sv);
279
280   dd_flags = dd_is_declarator(aTHX_ name);
281
282   if (dd_flags == -1)
283     return o;
284
285   dd_linestr_callback(aTHX_ "const", name);
286
287   return o;  
288 }
289
290 static int initialized = 0;
291
292 MODULE = Devel::Declare  PACKAGE = Devel::Declare
293
294 PROTOTYPES: DISABLE
295
296 void
297 setup()
298   CODE:
299   if (!initialized++) {
300     hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
301     hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
302     hook_op_check(OP_CONST, dd_ck_const, NULL);
303   }
304   filter_add(dd_filter_realloc, NULL);
305
306 char*
307 get_lex_stuff()
308   CODE:
309     RETVAL = dd_get_lex_stuff(aTHX);
310   OUTPUT:
311     RETVAL
312
313 void
314 clear_lex_stuff()
315   CODE:
316     dd_clear_lex_stuff(aTHX);
317
318 char*
319 get_curstash_name()
320   CODE:
321     RETVAL = dd_get_curstash_name(aTHX);
322   OUTPUT:
323     RETVAL
324
325 int
326 toke_scan_word(int offset, int handle_package)
327   CODE:
328     RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
329   OUTPUT:
330     RETVAL
331
332 int
333 toke_move_past_token(int offset);
334   CODE:
335     RETVAL = dd_toke_move_past_token(aTHX_ offset);
336   OUTPUT:
337     RETVAL
338
339 int
340 toke_scan_str(int offset);
341   CODE:
342     RETVAL = dd_toke_scan_str(aTHX_ offset);
343   OUTPUT:
344     RETVAL
345
346 int
347 toke_skipspace(int offset)
348   CODE:
349     RETVAL = dd_toke_skipspace(aTHX_ offset);
350   OUTPUT:
351     RETVAL
352
353 int
354 get_in_declare()
355   CODE:
356     RETVAL = in_declare;
357   OUTPUT:
358     RETVAL
359
360 void
361 set_in_declare(int value)
362   CODE:
363     in_declare = value;
364
365 BOOT:
366   if (getenv ("DD_DEBUG")) {
367     dd_debug = 1;
368   }