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 | |
e81bee92 |
13 | static int dd_debug = 0; |
c630715a |
14 | |
94caac6e |
15 | #define LEX_NORMAL 10 |
16 | #define LEX_INTERPNORMAL 9 |
17 | |
94caac6e |
18 | /* flag to trigger removal of temporary declaree sub */ |
19 | |
20 | static int in_declare = 0; |
21 | |
96f12726 |
22 | /* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff |
23 | is a lookup into it - so if anything else we can use to tell, so we |
24 | need to be a bit more careful if PL_parser exists */ |
25 | |
26 | #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL) |
27 | |
28 | #ifdef PL_parser |
29 | #define DD_HAVE_PARSER PL_parser |
30 | #define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff) |
31 | #define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK) |
32 | #else |
33 | #define DD_HAVE_PARSER 1 |
34 | #define DD_HAVE_LEX_STUFF PL_lex_stuff |
35 | #define DD_AM_LEXING DD_AM_LEXING_CHECK |
36 | #endif |
37 | |
72f20f69 |
38 | /* thing that decides whether we're dealing with a declarator */ |
39 | |
40 | int dd_is_declarator(pTHX_ char* name) { |
41 | HV* is_declarator; |
42 | SV** is_declarator_pack_ref; |
43 | HV* is_declarator_pack_hash; |
44 | SV** is_declarator_flag_ref; |
45 | int dd_flags; |
46 | |
47 | is_declarator = get_hv("Devel::Declare::declarators", FALSE); |
48 | |
49 | if (!is_declarator) |
50 | return -1; |
51 | |
569ac469 |
52 | /* $declarators{$current_package_name} */ |
53 | |
72f20f69 |
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 | |
569ac469 |
62 | /* $declarators{$current_package_name}{$name} */ |
63 | |
72f20f69 |
64 | is_declarator_flag_ref = hv_fetch( |
65 | is_declarator_pack_hash, name, |
66 | strlen(name), FALSE |
67 | ); |
68 | |
69 | /* requires SvIOK as well as TRUE since flags not being an int is useless */ |
70 | |
71 | if (!is_declarator_flag_ref |
72 | || !SvIOK(*is_declarator_flag_ref) |
73 | || !SvTRUE(*is_declarator_flag_ref)) |
74 | return -1; |
75 | |
76 | dd_flags = SvIVX(*is_declarator_flag_ref); |
77 | |
78 | return dd_flags; |
79 | } |
80 | |
569ac469 |
81 | /* callback thingy */ |
82 | |
a9bd9b5e |
83 | void dd_linestr_callback (pTHX_ char* type, char* name) { |
569ac469 |
84 | |
85 | char* linestr = SvPVX(PL_linestr); |
a9bd9b5e |
86 | int offset = PL_bufptr - linestr; |
569ac469 |
87 | |
569ac469 |
88 | dSP; |
89 | |
90 | ENTER; |
91 | SAVETMPS; |
92 | |
93 | PUSHMARK(SP); |
94 | XPUSHs(sv_2mortal(newSVpv(type, 0))); |
95 | XPUSHs(sv_2mortal(newSVpv(name, 0))); |
96 | XPUSHs(sv_2mortal(newSViv(offset))); |
97 | PUTBACK; |
98 | |
04a8a223 |
99 | call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD); |
569ac469 |
100 | |
569ac469 |
101 | FREETMPS; |
102 | LEAVE; |
103 | } |
104 | |
105 | char* dd_get_linestr(pTHX) { |
5f0b59d5 |
106 | if (!DD_HAVE_PARSER) { |
107 | return NULL; |
108 | } |
569ac469 |
109 | return SvPVX(PL_linestr); |
110 | } |
111 | |
112 | void dd_set_linestr(pTHX_ char* new_value) { |
113 | int new_len = strlen(new_value); |
569ac469 |
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 | |
bd85a06e |
132 | void 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; |
e81bee92 |
199 | if (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 | } |
840ebcbb |
206 | call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args); |
e81bee92 |
207 | if (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 | } |
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 | |
e81bee92 |
225 | if (dd_debug) { |
226 | printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv)); |
227 | } |
840ebcbb |
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 | |
e81bee92 |
234 | if (dd_debug) { |
235 | printf("dd_flags are: %i\n", dd_flags); |
236 | printf("PL_tokenbuf: %s\n", PL_tokenbuf); |
237 | } |
840ebcbb |
238 | |
a9bd9b5e |
239 | dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv)); |
94caac6e |
240 | |
53e3ab32 |
241 | return o; |
242 | } |
243 | |
bedac9ff |
244 | STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op); |
245 | |
b7505981 |
246 | OP* dd_pp_entereval(pTHX) { |
247 | dSP; |
248 | dPOPss; |
249 | STRLEN len; |
250 | const char* s; |
251 | if (SvPOK(sv)) { |
e81bee92 |
252 | if (dd_debug) { |
253 | printf("mangling eval sv\n"); |
254 | } |
b7505981 |
255 | if (SvREADONLY(sv)) |
256 | sv = sv_2mortal(newSVsv(sv)); |
257 | s = SvPVX(sv); |
258 | len = SvCUR(sv); |
259 | if (!len || s[len-1] != ';') { |
260 | if (!(SvFLAGS(sv) & SVs_TEMP)) |
261 | sv = sv_2mortal(newSVsv(sv)); |
262 | sv_catpvn(sv, "\n;", 2); |
53e3ab32 |
263 | } |
b7505981 |
264 | SvGROW(sv, 8192); |
53e3ab32 |
265 | } |
b7505981 |
266 | PUSHs(sv); |
267 | return PL_ppaddr[OP_ENTEREVAL](aTHX); |
268 | } |
53e3ab32 |
269 | |
b7505981 |
270 | STATIC OP *dd_ck_entereval(pTHX_ OP *o) { |
271 | o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */ |
d8e65fc8 |
272 | if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL]) |
273 | o->op_ppaddr = dd_pp_entereval; |
94caac6e |
274 | return o; |
275 | } |
276 | |
6a0bcf35 |
277 | static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen) |
278 | { |
279 | const I32 count = FILTER_READ(idx+1, sv, maxlen); |
280 | SvGROW(sv, 8192); /* please try not to have a line longer than this :) */ |
281 | /* filter_del(dd_filter_realloc); */ |
282 | return count; |
283 | } |
284 | |
bedac9ff |
285 | STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op); |
286 | |
287 | STATIC OP *dd_ck_const(pTHX_ OP *o) { |
bedac9ff |
288 | int dd_flags; |
04a8a223 |
289 | char* name; |
bedac9ff |
290 | |
291 | o = dd_old_ck_const(aTHX_ o); /* let the original do its job */ |
292 | |
34335b63 |
293 | /* if this is set, we just grabbed a delimited string or something, |
294 | not a bareword, so NO TOUCHY */ |
295 | |
79abf18a |
296 | if (DD_HAVE_LEX_STUFF) |
34335b63 |
297 | return o; |
298 | |
bedac9ff |
299 | /* don't try and look this up if it's not a string const */ |
300 | if (!SvPOK(cSVOPo->op_sv)) |
301 | return o; |
302 | |
04a8a223 |
303 | name = SvPVX(cSVOPo->op_sv); |
304 | |
305 | dd_flags = dd_is_declarator(aTHX_ name); |
bedac9ff |
306 | |
a9fb5fb1 |
307 | if (dd_flags == -1) |
bedac9ff |
308 | return o; |
309 | |
a9bd9b5e |
310 | dd_linestr_callback(aTHX_ "const", name); |
bedac9ff |
311 | |
bedac9ff |
312 | return o; |
313 | } |
314 | |
94caac6e |
315 | static int initialized = 0; |
316 | |
317 | MODULE = Devel::Declare PACKAGE = Devel::Declare |
318 | |
319 | PROTOTYPES: DISABLE |
320 | |
321 | void |
322 | setup() |
323 | CODE: |
324 | if (!initialized++) { |
325 | dd_old_ck_rv2cv = PL_check[OP_RV2CV]; |
326 | PL_check[OP_RV2CV] = dd_ck_rv2cv; |
b7505981 |
327 | dd_old_ck_entereval = PL_check[OP_ENTEREVAL]; |
328 | PL_check[OP_ENTEREVAL] = dd_ck_entereval; |
bedac9ff |
329 | dd_old_ck_const = PL_check[OP_CONST]; |
330 | PL_check[OP_CONST] = dd_ck_const; |
94caac6e |
331 | } |
6a0bcf35 |
332 | filter_add(dd_filter_realloc, NULL); |
569ac469 |
333 | |
334 | char* |
335 | get_linestr() |
336 | CODE: |
337 | RETVAL = dd_get_linestr(aTHX); |
338 | OUTPUT: |
339 | RETVAL |
923c07a8 |
340 | |
341 | void |
342 | set_linestr(char* new_value) |
343 | CODE: |
344 | dd_set_linestr(aTHX_ new_value); |
345 | |
04a8a223 |
346 | char* |
347 | get_lex_stuff() |
348 | CODE: |
349 | RETVAL = dd_get_lex_stuff(aTHX); |
350 | OUTPUT: |
351 | RETVAL |
352 | |
353 | void |
354 | clear_lex_stuff() |
355 | CODE: |
356 | dd_clear_lex_stuff(aTHX); |
357 | |
358 | char* |
359 | get_curstash_name() |
360 | CODE: |
361 | RETVAL = dd_get_curstash_name(aTHX); |
362 | OUTPUT: |
363 | RETVAL |
364 | |
923c07a8 |
365 | int |
022eb0cc |
366 | get_linestr_offset() |
367 | CODE: |
368 | RETVAL = dd_get_linestr_offset(aTHX); |
369 | OUTPUT: |
370 | RETVAL |
371 | |
372 | int |
923c07a8 |
373 | toke_scan_word(int offset, int handle_package) |
374 | CODE: |
375 | RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package); |
376 | OUTPUT: |
377 | RETVAL |
378 | |
379 | int |
04a8a223 |
380 | toke_move_past_token(int offset); |
381 | CODE: |
382 | RETVAL = dd_toke_move_past_token(aTHX_ offset); |
383 | OUTPUT: |
384 | RETVAL |
385 | |
386 | int |
923c07a8 |
387 | toke_scan_str(int offset); |
388 | CODE: |
389 | RETVAL = dd_toke_scan_str(aTHX_ offset); |
390 | OUTPUT: |
391 | RETVAL |
392 | |
393 | int |
394 | toke_skipspace(int offset) |
395 | CODE: |
396 | RETVAL = dd_toke_skipspace(aTHX_ offset); |
397 | OUTPUT: |
398 | RETVAL |
04a8a223 |
399 | |
400 | int |
401 | get_in_declare() |
402 | CODE: |
403 | RETVAL = in_declare; |
404 | OUTPUT: |
405 | RETVAL |
406 | |
407 | void |
408 | set_in_declare(int value) |
409 | CODE: |
410 | in_declare = value; |
e81bee92 |
411 | |
412 | BOOT: |
413 | if (getenv ("DD_DEBUG")) { |
414 | dd_debug = 1; |
415 | } |