stop using & prototypes at all
[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 #if 0
12 #define DD_DEBUG
13 #endif
14
15 #define DD_HANDLE_NAME 1
16 #define DD_HANDLE_PROTO 2
17 #define DD_HANDLE_PACKAGE 8
18
19 #ifdef DD_DEBUG
20 #define DD_DEBUG_S printf("Buffer: %s\n", s);
21 #else
22 #define DD_DEBUG_S
23 #endif
24
25 #define LEX_NORMAL    10
26 #define LEX_INTERPNORMAL   9
27
28 /* placeholders for PL_check entries we wrap */
29
30 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
31 STATIC OP *(*dd_old_ck_lineseq)(pTHX_ OP *op);
32
33 /* flag to trigger removal of temporary declaree sub */
34
35 static int in_declare = 0;
36
37 /* replacement PL_check rv2cv entry */
38
39 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
40   OP* kid;
41   char* s;
42   char* save_s;
43   char tmpbuf[sizeof PL_tokenbuf];
44   char found_name[sizeof PL_tokenbuf];
45   char* found_proto = NULL;
46   STRLEN len = 0;
47   HV *stash;
48   HV* is_declarator;
49   SV** is_declarator_pack_ref;
50   HV* is_declarator_pack_hash;
51   SV** is_declarator_flag_ref;
52   int dd_flags;
53   char* cb_args[6];
54   dSP; /* define stack pointer for later call stuff */
55   char* retstr;
56   STRLEN n_a; /* for POPpx */
57
58   o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
59
60   if (in_declare) {
61     cb_args[0] = NULL;
62     call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
63     in_declare--;
64     return o;
65   }
66
67   kid = cUNOPo->op_first;
68
69   if (kid->op_type != OP_GV) /* not a GV so ignore */
70     return o;
71
72   if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
73     return o; /* not lexing? */
74
75   stash = GvSTASH(kGVOP_gv);
76
77 #ifdef DD_DEBUG
78   printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv));
79 #endif
80
81   is_declarator = get_hv("Devel::Declare::declarators", FALSE);
82
83   if (!is_declarator)
84     return o;
85
86   is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash),
87                              strlen(HvNAME(stash)), FALSE);
88
89   if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
90     return o; /* not a hashref */
91
92   is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
93
94   is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv),
95                                 strlen(GvNAME(kGVOP_gv)), FALSE);
96
97   /* requires SvIOK as well as TRUE since flags not being an int is useless */
98
99   if (!is_declarator_flag_ref
100         || !SvIOK(*is_declarator_flag_ref) 
101         || !SvTRUE(*is_declarator_flag_ref))
102     return o;
103
104   dd_flags = SvIVX(*is_declarator_flag_ref);
105
106 #ifdef DD_DEBUG
107   printf("dd_flags are: %i\n", dd_flags);
108 #endif
109
110   s = PL_bufptr; /* copy the current buffer pointer */
111
112   DD_DEBUG_S
113
114 #ifdef DD_DEBUG
115   printf("PL_tokenbuf: %s\n", PL_tokenbuf);
116 #endif
117
118   /*
119    *   buffer will be at the beginning of the declarator, -unless- the
120    *   declarator is at EOL in which case it'll be the next useful line
121    *   so we don't short-circuit out if we don't find the declarator
122    */
123
124   while (s < PL_bufend && isSPACE(*s)) s++;
125   if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
126     s += strlen(PL_tokenbuf);
127
128   DD_DEBUG_S
129
130   if (dd_flags & DD_HANDLE_NAME) {
131
132     /* find next word */
133
134     s = skipspace(s);
135
136     DD_DEBUG_S
137
138     /* arg 4 is allow_package */
139
140     s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
141
142     DD_DEBUG_S
143
144     if (len) {
145       strcpy(found_name, tmpbuf);
146 #ifdef DD_DEBUG
147       printf("Found %s\n", found_name);
148 #endif
149     }
150   }
151
152   if (dd_flags & DD_HANDLE_PROTO) {
153
154     s = skipspace(s);
155
156     if (*s == '(') { /* found a prototype-ish thing */
157       save_s = s;
158       s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
159       if (SvPOK(PL_lex_stuff)) {
160 #ifdef DD_DEBUG
161         printf("Found proto %s\n", SvPVX(PL_lex_stuff));
162 #endif
163         found_proto = SvPVX(PL_lex_stuff);
164         if (len) /* foo name () => foo name  X, only foo parsed so works */
165           *save_s++ = ' ';
166         else /* foo () => foo =X, TOKEN('&') won't handle foo X */
167           *save_s++ = '=';
168         *save_s++ = 'X';
169         while (save_s < s) {
170           *save_s++ = ' ';
171         }
172 #ifdef DD_DEBUG
173         printf("Curbuf %s\n", PL_bufptr);
174 #endif
175       }
176     }
177   }
178
179   if (!len)
180     found_name[0] = 0;
181
182 #ifdef DD_DEBUG
183   printf("Calling init_declare\n");
184 #endif
185   cb_args[0] = HvNAME(stash);
186   cb_args[1] = GvNAME(kGVOP_gv);
187   cb_args[2] = HvNAME(PL_curstash);
188   cb_args[3] = found_name;
189   cb_args[4] = found_proto;
190   cb_args[5] = NULL;
191
192   if (len && found_proto)
193     in_declare = 2;
194   else if (len || found_proto)
195     in_declare = 1;
196   if (found_proto)
197     PL_lex_stuff = Nullsv;
198   s = skipspace(s);
199 #ifdef DD_DEBUG
200   printf("cur buf: %s\n", s);
201   printf("bufend at: %i\n", PL_bufend - s);
202   printf("linestr: %s\n", SvPVX(PL_linestr));
203   printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
204 #endif
205   if (*s++ == '{') {
206     call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
207     SPAGAIN;
208     retstr = POPpx;
209     PUTBACK;
210     if (retstr && strlen(retstr)) {
211 #ifdef DD_DEBUG
212       printf("Got string %s\n", retstr);
213 #endif
214       SvGROW(PL_linestr, strlen(retstr));
215       memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
216       memmove(s, retstr, strlen(retstr));
217       PL_bufend += strlen(retstr);
218 #ifdef DD_DEBUG
219   printf("cur buf: %s\n", s);
220   printf("bufend at: %i\n", PL_bufend - s);
221   printf("linestr: %s\n", SvPVX(PL_linestr));
222   printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
223 #endif
224     }
225   } else {
226     call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
227   }
228   return o;
229 }
230
231 STATIC OP *dd_ck_lineseq(pTHX_ OP *o) {
232   AV* pad_inject_list;
233   SV** to_inject_ref;
234   int i, pad_inject_list_last;
235
236   o = dd_old_ck_lineseq(aTHX_ o);
237
238   pad_inject_list = get_av("Devel::Declare::next_pad_inject", FALSE);
239   if (!pad_inject_list)
240     return o;
241
242   pad_inject_list_last = av_len(pad_inject_list);
243
244   if (pad_inject_list_last == -1)
245     return o;
246
247   for (i = 0; i <= pad_inject_list_last; i++) {
248     to_inject_ref = av_fetch(pad_inject_list, i, FALSE);
249     if (to_inject_ref && SvPOK(*to_inject_ref)) {
250 #ifdef DD_DEBUG
251   printf("Injecting %s into pad\n", SvPVX(*to_inject_ref));
252 #endif
253       allocmy(SvPVX(*to_inject_ref));
254     }
255   }
256
257   av_clear(pad_inject_list);
258
259   return o;
260 }
261
262 static int initialized = 0;
263
264 MODULE = Devel::Declare  PACKAGE = Devel::Declare
265
266 PROTOTYPES: DISABLE
267
268 void
269 setup()
270   CODE:
271   if (!initialized++) {
272     dd_old_ck_rv2cv = PL_check[OP_RV2CV];
273     PL_check[OP_RV2CV] = dd_ck_rv2cv;
274     dd_old_ck_lineseq = PL_check[OP_LINESEQ];
275     PL_check[OP_LINESEQ] = dd_ck_lineseq;
276   }
277
278 void
279 teardown()
280   CODE:
281   /* ensure we only uninit when number of teardown calls matches 
282      number of setup calls */
283   if (initialized && !--initialized) {
284     PL_check[OP_RV2CV] = dd_old_ck_rv2cv;
285   }