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