Commit | Line | Data |
94caac6e |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
4 | #undef printf |
e807ee50 |
5 | #include "stolen_chunk_of_toke.c" |
94caac6e |
6 | #include <stdio.h> |
7 | #include <string.h> |
8 | |
9673d7ca |
9 | #ifndef Newx |
10 | # define Newx(v,n,t) New(0,v,n,t) |
11 | #endif /* !Newx */ |
12 | |
53e3ab32 |
13 | #if 0 |
14 | #define DD_DEBUG |
15 | #endif |
c630715a |
16 | |
94caac6e |
17 | #define LEX_NORMAL 10 |
18 | #define LEX_INTERPNORMAL 9 |
19 | |
94caac6e |
20 | /* flag to trigger removal of temporary declaree sub */ |
21 | |
22 | static int in_declare = 0; |
23 | |
96f12726 |
24 | /* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff |
25 | is a lookup into it - so if anything else we can use to tell, so we |
26 | need to be a bit more careful if PL_parser exists */ |
27 | |
28 | #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL) |
29 | |
30 | #ifdef PL_parser |
31 | #define DD_HAVE_PARSER PL_parser |
32 | #define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff) |
33 | #define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK) |
34 | #else |
35 | #define DD_HAVE_PARSER 1 |
36 | #define DD_HAVE_LEX_STUFF PL_lex_stuff |
37 | #define DD_AM_LEXING DD_AM_LEXING_CHECK |
38 | #endif |
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 | |
569ac469 |
54 | /* $declarators{$current_package_name} */ |
55 | |
72f20f69 |
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 | |
569ac469 |
64 | /* $declarators{$current_package_name}{$name} */ |
65 | |
72f20f69 |
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 | |
569ac469 |
83 | /* callback thingy */ |
84 | |
a9bd9b5e |
85 | void dd_linestr_callback (pTHX_ char* type, char* name) { |
569ac469 |
86 | |
87 | char* linestr = SvPVX(PL_linestr); |
a9bd9b5e |
88 | int offset = PL_bufptr - linestr; |
569ac469 |
89 | |
569ac469 |
90 | dSP; |
91 | |
92 | ENTER; |
93 | SAVETMPS; |
94 | |
95 | PUSHMARK(SP); |
96 | XPUSHs(sv_2mortal(newSVpv(type, 0))); |
97 | XPUSHs(sv_2mortal(newSVpv(name, 0))); |
98 | XPUSHs(sv_2mortal(newSViv(offset))); |
99 | PUTBACK; |
100 | |
04a8a223 |
101 | call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD); |
569ac469 |
102 | |
569ac469 |
103 | FREETMPS; |
104 | LEAVE; |
105 | } |
106 | |
107 | char* dd_get_linestr(pTHX) { |
108 | return SvPVX(PL_linestr); |
109 | } |
110 | |
111 | void dd_set_linestr(pTHX_ char* new_value) { |
112 | int new_len = strlen(new_value); |
113 | char* old_linestr = SvPVX(PL_linestr); |
114 | |
ce9a252b |
115 | if (SvLEN(PL_linestr) < new_len) { |
f9eb22eb |
116 | croak("forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr)); |
ce9a252b |
117 | } |
118 | |
119 | SvGROW(PL_linestr, new_len); |
569ac469 |
120 | |
121 | memcpy(SvPVX(PL_linestr), new_value, new_len+1); |
122 | |
123 | SvCUR_set(PL_linestr, new_len); |
124 | |
125 | PL_bufend = SvPVX(PL_linestr) + new_len; |
126 | } |
127 | |
04a8a223 |
128 | char* dd_get_lex_stuff(pTHX) { |
96f12726 |
129 | return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : ""); |
04a8a223 |
130 | } |
131 | |
132 | char* dd_clear_lex_stuff(pTHX) { |
96f12726 |
133 | if (DD_HAVE_PARSER) |
f05cbc90 |
134 | PL_lex_stuff = (SV*)NULL; |
04a8a223 |
135 | } |
136 | |
137 | char* dd_get_curstash_name(pTHX) { |
138 | return HvNAME(PL_curstash); |
139 | } |
140 | |
022eb0cc |
141 | int dd_get_linestr_offset(pTHX) { |
142 | char* linestr = SvPVX(PL_linestr); |
143 | return PL_bufptr - linestr; |
144 | } |
145 | |
840ebcbb |
146 | char* dd_move_past_token (pTHX_ char* s) { |
923c07a8 |
147 | |
148 | /* |
149 | * buffer will be at the beginning of the declarator, -unless- the |
150 | * declarator is at EOL in which case it'll be the next useful line |
151 | * so we don't short-circuit out if we don't find the declarator |
152 | */ |
153 | |
154 | while (s < PL_bufend && isSPACE(*s)) s++; |
155 | if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf))) |
156 | s += strlen(PL_tokenbuf); |
157 | return s; |
158 | } |
159 | |
840ebcbb |
160 | int dd_toke_move_past_token (pTHX_ int offset) { |
04a8a223 |
161 | char* base_s = SvPVX(PL_linestr) + offset; |
162 | char* s = dd_move_past_token(aTHX_ base_s); |
163 | return s - base_s; |
164 | } |
165 | |
923c07a8 |
166 | int dd_toke_scan_word(pTHX_ int offset, int handle_package) { |
167 | char tmpbuf[sizeof PL_tokenbuf]; |
168 | char* base_s = SvPVX(PL_linestr) + offset; |
169 | STRLEN len; |
170 | char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len); |
171 | return s - base_s; |
172 | } |
173 | |
174 | int dd_toke_scan_str(pTHX_ int offset) { |
175 | char* base_s = SvPVX(PL_linestr) + offset; |
176 | char* s = scan_str(base_s, FALSE, FALSE); |
177 | return s - base_s; |
178 | } |
179 | |
180 | int dd_toke_skipspace(pTHX_ int offset) { |
181 | char* base_s = SvPVX(PL_linestr) + offset; |
182 | char* s = skipspace(base_s); |
183 | return s - base_s; |
184 | } |
185 | |
94caac6e |
186 | /* replacement PL_check rv2cv entry */ |
187 | |
bedac9ff |
188 | STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op); |
189 | |
94caac6e |
190 | STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { |
191 | OP* kid; |
0ba8c7aa |
192 | int dd_flags; |
9026391e |
193 | char* cb_args[6]; |
94caac6e |
194 | |
195 | o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */ |
196 | |
197 | if (in_declare) { |
840ebcbb |
198 | cb_args[0] = NULL; |
199 | #ifdef DD_DEBUG |
200 | printf("Deconstructing declare\n"); |
201 | printf("PL_bufptr: %s\n", PL_bufptr); |
202 | printf("bufend at: %i\n", PL_bufend - PL_bufptr); |
203 | printf("linestr: %s\n", SvPVX(PL_linestr)); |
204 | printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr)); |
205 | #endif |
206 | call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args); |
207 | #ifdef DD_DEBUG |
208 | printf("PL_bufptr: %s\n", PL_bufptr); |
209 | printf("bufend at: %i\n", PL_bufend - PL_bufptr); |
210 | printf("linestr: %s\n", SvPVX(PL_linestr)); |
211 | printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr)); |
212 | printf("actual len: %i\n", strlen(PL_bufptr)); |
213 | #endif |
94caac6e |
214 | return o; |
215 | } |
216 | |
217 | kid = cUNOPo->op_first; |
218 | |
219 | if (kid->op_type != OP_GV) /* not a GV so ignore */ |
220 | return o; |
221 | |
96f12726 |
222 | if (!DD_AM_LEXING) |
94caac6e |
223 | return o; /* not lexing? */ |
224 | |
840ebcbb |
225 | #ifdef DD_DEBUG |
226 | printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv)); |
227 | #endif |
228 | |
72f20f69 |
229 | dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv)); |
94caac6e |
230 | |
72f20f69 |
231 | if (dd_flags == -1) |
94caac6e |
232 | return o; |
233 | |
840ebcbb |
234 | #ifdef DD_DEBUG |
235 | printf("dd_flags are: %i\n", dd_flags); |
236 | #endif |
237 | |
238 | #ifdef DD_DEBUG |
239 | printf("PL_tokenbuf: %s\n", PL_tokenbuf); |
240 | #endif |
241 | |
a9bd9b5e |
242 | dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv)); |
94caac6e |
243 | |
53e3ab32 |
244 | return o; |
245 | } |
246 | |
bedac9ff |
247 | STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op); |
248 | |
b7505981 |
249 | OP* dd_pp_entereval(pTHX) { |
250 | dSP; |
251 | dPOPss; |
252 | STRLEN len; |
253 | const char* s; |
254 | if (SvPOK(sv)) { |
53e3ab32 |
255 | #ifdef DD_DEBUG |
b7505981 |
256 | printf("mangling eval sv\n"); |
53e3ab32 |
257 | #endif |
b7505981 |
258 | if (SvREADONLY(sv)) |
259 | sv = sv_2mortal(newSVsv(sv)); |
260 | s = SvPVX(sv); |
261 | len = SvCUR(sv); |
262 | if (!len || s[len-1] != ';') { |
263 | if (!(SvFLAGS(sv) & SVs_TEMP)) |
264 | sv = sv_2mortal(newSVsv(sv)); |
265 | sv_catpvn(sv, "\n;", 2); |
53e3ab32 |
266 | } |
b7505981 |
267 | SvGROW(sv, 8192); |
53e3ab32 |
268 | } |
b7505981 |
269 | PUSHs(sv); |
270 | return PL_ppaddr[OP_ENTEREVAL](aTHX); |
271 | } |
53e3ab32 |
272 | |
b7505981 |
273 | STATIC OP *dd_ck_entereval(pTHX_ OP *o) { |
274 | o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */ |
d8e65fc8 |
275 | if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL]) |
276 | o->op_ppaddr = dd_pp_entereval; |
94caac6e |
277 | return o; |
278 | } |
279 | |
6a0bcf35 |
280 | static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen) |
281 | { |
282 | const I32 count = FILTER_READ(idx+1, sv, maxlen); |
283 | SvGROW(sv, 8192); /* please try not to have a line longer than this :) */ |
284 | /* filter_del(dd_filter_realloc); */ |
285 | return count; |
286 | } |
287 | |
bedac9ff |
288 | STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op); |
289 | |
290 | STATIC OP *dd_ck_const(pTHX_ OP *o) { |
bedac9ff |
291 | int dd_flags; |
292 | char* s; |
04a8a223 |
293 | char* name; |
bedac9ff |
294 | |
295 | o = dd_old_ck_const(aTHX_ o); /* let the original do its job */ |
296 | |
34335b63 |
297 | /* if this is set, we just grabbed a delimited string or something, |
298 | not a bareword, so NO TOUCHY */ |
299 | |
79abf18a |
300 | if (DD_HAVE_LEX_STUFF) |
34335b63 |
301 | return o; |
302 | |
bedac9ff |
303 | /* don't try and look this up if it's not a string const */ |
304 | if (!SvPOK(cSVOPo->op_sv)) |
305 | return o; |
306 | |
04a8a223 |
307 | name = SvPVX(cSVOPo->op_sv); |
308 | |
309 | dd_flags = dd_is_declarator(aTHX_ name); |
bedac9ff |
310 | |
a9fb5fb1 |
311 | if (dd_flags == -1) |
bedac9ff |
312 | return o; |
313 | |
a9bd9b5e |
314 | dd_linestr_callback(aTHX_ "const", name); |
bedac9ff |
315 | |
bedac9ff |
316 | return o; |
317 | } |
318 | |
94caac6e |
319 | static int initialized = 0; |
320 | |
321 | MODULE = Devel::Declare PACKAGE = Devel::Declare |
322 | |
323 | PROTOTYPES: DISABLE |
324 | |
325 | void |
326 | setup() |
327 | CODE: |
328 | if (!initialized++) { |
329 | dd_old_ck_rv2cv = PL_check[OP_RV2CV]; |
330 | PL_check[OP_RV2CV] = dd_ck_rv2cv; |
b7505981 |
331 | dd_old_ck_entereval = PL_check[OP_ENTEREVAL]; |
332 | PL_check[OP_ENTEREVAL] = dd_ck_entereval; |
bedac9ff |
333 | dd_old_ck_const = PL_check[OP_CONST]; |
334 | PL_check[OP_CONST] = dd_ck_const; |
94caac6e |
335 | } |
6a0bcf35 |
336 | filter_add(dd_filter_realloc, NULL); |
569ac469 |
337 | |
338 | char* |
339 | get_linestr() |
340 | CODE: |
341 | RETVAL = dd_get_linestr(aTHX); |
342 | OUTPUT: |
343 | RETVAL |
923c07a8 |
344 | |
345 | void |
346 | set_linestr(char* new_value) |
347 | CODE: |
348 | dd_set_linestr(aTHX_ new_value); |
349 | |
04a8a223 |
350 | char* |
351 | get_lex_stuff() |
352 | CODE: |
353 | RETVAL = dd_get_lex_stuff(aTHX); |
354 | OUTPUT: |
355 | RETVAL |
356 | |
357 | void |
358 | clear_lex_stuff() |
359 | CODE: |
360 | dd_clear_lex_stuff(aTHX); |
361 | |
362 | char* |
363 | get_curstash_name() |
364 | CODE: |
365 | RETVAL = dd_get_curstash_name(aTHX); |
366 | OUTPUT: |
367 | RETVAL |
368 | |
923c07a8 |
369 | int |
022eb0cc |
370 | get_linestr_offset() |
371 | CODE: |
372 | RETVAL = dd_get_linestr_offset(aTHX); |
373 | OUTPUT: |
374 | RETVAL |
375 | |
376 | int |
923c07a8 |
377 | toke_scan_word(int offset, int handle_package) |
378 | CODE: |
379 | RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package); |
380 | OUTPUT: |
381 | RETVAL |
382 | |
383 | int |
04a8a223 |
384 | toke_move_past_token(int offset); |
385 | CODE: |
386 | RETVAL = dd_toke_move_past_token(aTHX_ offset); |
387 | OUTPUT: |
388 | RETVAL |
389 | |
390 | int |
923c07a8 |
391 | toke_scan_str(int offset); |
392 | CODE: |
393 | RETVAL = dd_toke_scan_str(aTHX_ offset); |
394 | OUTPUT: |
395 | RETVAL |
396 | |
397 | int |
398 | toke_skipspace(int offset) |
399 | CODE: |
400 | RETVAL = dd_toke_skipspace(aTHX_ offset); |
401 | OUTPUT: |
402 | RETVAL |
04a8a223 |
403 | |
404 | int |
405 | get_in_declare() |
406 | CODE: |
407 | RETVAL = in_declare; |
408 | OUTPUT: |
409 | RETVAL |
410 | |
411 | void |
412 | set_in_declare(int value) |
413 | CODE: |
414 | in_declare = value; |