6b188c3d28cd126658a25d9b377f22c127dc2832
[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_linestr(pTHX) {
110   if (!DD_HAVE_PARSER) {
111     return NULL;
112   }
113   return SvPVX(PL_linestr);
114 }
115
116 void dd_set_linestr(pTHX_ char* new_value) {
117   unsigned int new_len = strlen(new_value);
118
119   if (SvLEN(PL_linestr) < new_len) {
120     croak("PL_linestr not long enough, was Devel::Declare loaded soon enough in %s",
121       CopFILE(&PL_compiling)
122     );
123   }
124
125
126   memcpy(SvPVX(PL_linestr), new_value, new_len+1);
127
128   SvCUR_set(PL_linestr, new_len);
129
130   PL_bufend = SvPVX(PL_linestr) + new_len;
131 }
132
133 char* dd_get_lex_stuff(pTHX) {
134   return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : "");
135 }
136
137 void dd_clear_lex_stuff(pTHX) {
138   if (DD_HAVE_PARSER)
139     PL_lex_stuff = (SV*)NULL;
140 }
141
142 char* dd_get_curstash_name(pTHX) {
143   return HvNAME(PL_curstash);
144 }
145
146 int dd_get_linestr_offset(pTHX) {
147   char* linestr;
148   if (!DD_HAVE_PARSER) {
149     return -1;
150   }
151   linestr = SvPVX(PL_linestr);
152   return PL_bufptr - linestr;
153 }
154
155 char* dd_move_past_token (pTHX_ char* s) {
156
157   /*
158    *   buffer will be at the beginning of the declarator, -unless- the
159    *   declarator is at EOL in which case it'll be the next useful line
160    *   so we don't short-circuit out if we don't find the declarator
161    */
162
163   while (s < PL_bufend && isSPACE(*s)) s++;
164   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
165     s += strlen(PL_tokenbuf);
166   return s;
167 }
168
169 int dd_toke_move_past_token (pTHX_ int offset) {
170   char* base_s = SvPVX(PL_linestr) + offset;
171   char* s = dd_move_past_token(aTHX_ base_s);
172   return s - base_s;
173 }
174
175 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
176   char tmpbuf[sizeof PL_tokenbuf];
177   char* base_s = SvPVX(PL_linestr) + offset;
178   STRLEN len;
179   char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
180   return s - base_s;
181 }
182
183 int dd_toke_scan_ident(pTHX_ int offset) {
184     char tmpbuf[sizeof PL_tokenbuf];
185     char* base_s = SvPVX(PL_linestr) + offset;
186     char* s = scan_ident(base_s, PL_bufend, tmpbuf, sizeof tmpbuf, 0);
187     return s - base_s;
188 }
189
190 int dd_toke_scan_str(pTHX_ int offset) {
191   STRLEN remaining = sv_len(PL_linestr) - offset;
192   SV* line_copy = newSVsv(PL_linestr);
193   char* base_s = SvPVX(PL_linestr) + offset;
194   char* s = scan_str(base_s, FALSE, FALSE);
195   if (s != base_s && sv_len(PL_lex_stuff) > remaining) {
196     int ret = (s - SvPVX(PL_linestr)) + remaining;
197     sv_catsv(line_copy, PL_linestr);
198     dd_set_linestr(aTHX_ SvPV_nolen(line_copy));
199     SvREFCNT_dec(line_copy);
200     return ret;
201   }
202   return s - base_s;
203 }
204
205 int dd_toke_skipspace(pTHX_ int offset) {
206   char* base_s = SvPVX(PL_linestr) + offset;
207   char* s = skipspace(base_s);
208   return s - base_s;
209 }
210
211 /* replacement PL_check rv2cv entry */
212
213 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) {
214   dSP;
215   OP* kid;
216   int dd_flags;
217
218   PERL_UNUSED_VAR(user_data);
219
220   if (in_declare) {
221     if (dd_debug) {
222       printf("Deconstructing declare\n");
223       printf("PL_bufptr: %s\n", PL_bufptr);
224       printf("bufend at: %i\n", PL_bufend - PL_bufptr);
225       printf("linestr: %s\n", SvPVX(PL_linestr));
226       printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
227     }
228
229     ENTER;
230     SAVETMPS;
231
232     PUSHMARK(SP);
233
234     call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
235
236     FREETMPS;
237     LEAVE;
238
239     if (dd_debug) {
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       printf("actual len: %i\n", strlen(PL_bufptr));
245     }
246     return o;
247   }
248
249   kid = cUNOPo->op_first;
250
251   if (kid->op_type != OP_GV) /* not a GV so ignore */
252     return o;
253
254   if (!DD_AM_LEXING)
255     return o; /* not lexing? */
256
257   if (dd_debug) {
258     printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
259   }
260
261   dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
262
263   if (dd_flags == -1)
264     return o;
265
266   if (dd_debug) {
267     printf("dd_flags are: %i\n", dd_flags);
268     printf("PL_tokenbuf: %s\n", PL_tokenbuf);
269   }
270
271   dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
272
273   return o;
274 }
275
276 OP* dd_pp_entereval(pTHX) {
277   dSP;
278   STRLEN len;
279   const char* s;
280   SV *sv;
281 #ifdef PERL_5_9_PLUS
282   SV *saved_hh;
283   if (PL_op->op_private & OPpEVAL_HAS_HH) {
284     saved_hh = POPs;
285   }
286 #endif
287   sv = POPs;
288   if (SvPOK(sv)) {
289     if (dd_debug) {
290       printf("mangling eval sv\n");
291     }
292     if (SvREADONLY(sv))
293       sv = sv_2mortal(newSVsv(sv));
294     s = SvPVX(sv);
295     len = SvCUR(sv);
296     if (!len || s[len-1] != ';') {
297       if (!(SvFLAGS(sv) & SVs_TEMP))
298         sv = sv_2mortal(newSVsv(sv));
299       sv_catpvn(sv, "\n;", 2);
300     }
301     SvGROW(sv, 8192);
302   }
303   PUSHs(sv);
304 #ifdef PERL_5_9_PLUS
305   if (PL_op->op_private & OPpEVAL_HAS_HH) {
306     PUSHs(saved_hh);
307   }
308 #endif
309   return PL_ppaddr[OP_ENTEREVAL](aTHX);
310 }
311
312 STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) {
313   PERL_UNUSED_VAR(user_data);
314
315   if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
316     o->op_ppaddr = dd_pp_entereval;
317   return o;
318 }
319
320 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
321 {
322   const I32 count = FILTER_READ(idx+1, sv, maxlen);
323   SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
324   /* filter_del(dd_filter_realloc); */
325   return count;
326 }
327
328 STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) {
329   int dd_flags;
330   char* name;
331
332   PERL_UNUSED_VAR(user_data);
333
334   if (DD_HAVE_PARSER && PL_expect == XOPERATOR) {
335     return o;
336   }
337
338   /* if this is set, we just grabbed a delimited string or something,
339      not a bareword, so NO TOUCHY */
340
341   if (DD_HAVE_LEX_STUFF)
342     return o;
343
344   /* don't try and look this up if it's not a string const */
345   if (!SvPOK(cSVOPo->op_sv))
346     return o;
347
348   name = SvPVX(cSVOPo->op_sv);
349
350   dd_flags = dd_is_declarator(aTHX_ name);
351
352   if (dd_flags == -1)
353     return o;
354
355   switch (PL_lex_inwhat) {
356     case OP_QR:
357     case OP_MATCH:
358     case OP_SUBST:
359     case OP_TRANS:
360     case OP_BACKTICK:
361     case OP_STRINGIFY:
362       return o;
363       break;
364     default:
365       break;
366   }
367
368   dd_linestr_callback(aTHX_ "const", name);
369
370   return o;
371 }
372
373 static int initialized = 0;
374
375 MODULE = Devel::Declare  PACKAGE = Devel::Declare
376
377 PROTOTYPES: DISABLE
378
379 void
380 setup()
381   CODE:
382   if (!initialized++) {
383     hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL);
384     hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL);
385     hook_op_check(OP_CONST, dd_ck_const, NULL);
386   }
387   filter_add(dd_filter_realloc, NULL);
388
389 char*
390 get_linestr()
391   CODE:
392     RETVAL = dd_get_linestr(aTHX);
393   OUTPUT:
394     RETVAL
395
396 void
397 set_linestr(char* new_value)
398   CODE:
399     dd_set_linestr(aTHX_ new_value);
400
401 char*
402 get_lex_stuff()
403   CODE:
404     RETVAL = dd_get_lex_stuff(aTHX);
405   OUTPUT:
406     RETVAL
407
408 void
409 clear_lex_stuff()
410   CODE:
411     dd_clear_lex_stuff(aTHX);
412
413 char*
414 get_curstash_name()
415   CODE:
416     RETVAL = dd_get_curstash_name(aTHX);
417   OUTPUT:
418     RETVAL
419
420 int
421 get_linestr_offset()
422   CODE:
423     RETVAL = dd_get_linestr_offset(aTHX);
424   OUTPUT:
425     RETVAL
426
427 int
428 toke_scan_word(int offset, int handle_package)
429   CODE:
430     RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
431   OUTPUT:
432     RETVAL
433
434 int
435 toke_move_past_token(int offset);
436   CODE:
437     RETVAL = dd_toke_move_past_token(aTHX_ offset);
438   OUTPUT:
439     RETVAL
440
441 int
442 toke_scan_str(int offset);
443   CODE:
444     RETVAL = dd_toke_scan_str(aTHX_ offset);
445   OUTPUT:
446     RETVAL
447
448 int
449 toke_scan_ident(int offset)
450   CODE:
451     RETVAL = dd_toke_scan_ident(aTHX_ offset);
452   OUTPUT:
453     RETVAL
454
455 int
456 toke_skipspace(int offset)
457   CODE:
458     RETVAL = dd_toke_skipspace(aTHX_ offset);
459   OUTPUT:
460     RETVAL
461
462 int
463 get_in_declare()
464   CODE:
465     RETVAL = in_declare;
466   OUTPUT:
467     RETVAL
468
469 void
470 set_in_declare(int value)
471   CODE:
472     in_declare = value;
473
474 BOOT:
475   if (getenv ("DD_DEBUG")) {
476     dd_debug = 1;
477   }