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) { |
0da63271 |
142 | char* linestr; |
143 | if (!DD_HAVE_PARSER) { |
144 | return -1; |
145 | } |
146 | linestr = SvPVX(PL_linestr); |
022eb0cc |
147 | return PL_bufptr - linestr; |
148 | } |
149 | |
840ebcbb |
150 | char* dd_move_past_token (pTHX_ char* s) { |
923c07a8 |
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 | |
158 | while (s < PL_bufend && isSPACE(*s)) s++; |
159 | if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf))) |
160 | s += strlen(PL_tokenbuf); |
161 | return s; |
162 | } |
163 | |
840ebcbb |
164 | int dd_toke_move_past_token (pTHX_ int offset) { |
04a8a223 |
165 | char* base_s = SvPVX(PL_linestr) + offset; |
166 | char* s = dd_move_past_token(aTHX_ base_s); |
167 | return s - base_s; |
168 | } |
169 | |
923c07a8 |
170 | int dd_toke_scan_word(pTHX_ int offset, int handle_package) { |
171 | char tmpbuf[sizeof PL_tokenbuf]; |
172 | char* base_s = SvPVX(PL_linestr) + offset; |
173 | STRLEN len; |
174 | char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len); |
175 | return s - base_s; |
176 | } |
177 | |
178 | int dd_toke_scan_str(pTHX_ int offset) { |
179 | char* base_s = SvPVX(PL_linestr) + offset; |
180 | char* s = scan_str(base_s, FALSE, FALSE); |
181 | return s - base_s; |
182 | } |
183 | |
184 | int dd_toke_skipspace(pTHX_ int offset) { |
185 | char* base_s = SvPVX(PL_linestr) + offset; |
186 | char* s = skipspace(base_s); |
187 | return s - base_s; |
188 | } |
189 | |
94caac6e |
190 | /* replacement PL_check rv2cv entry */ |
191 | |
bedac9ff |
192 | STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op); |
193 | |
94caac6e |
194 | STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) { |
195 | OP* kid; |
0ba8c7aa |
196 | int dd_flags; |
94caac6e |
197 | |
198 | o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */ |
199 | |
200 | if (in_declare) { |
e81bee92 |
201 | if (dd_debug) { |
202 | printf("Deconstructing declare\n"); |
203 | printf("PL_bufptr: %s\n", PL_bufptr); |
204 | printf("bufend at: %i\n", PL_bufend - PL_bufptr); |
205 | printf("linestr: %s\n", SvPVX(PL_linestr)); |
206 | printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr)); |
207 | } |
001d53d0 |
208 | |
209 | dSP; |
210 | |
211 | ENTER; |
212 | SAVETMPS; |
213 | |
214 | PUSHMARK(SP); |
215 | |
216 | call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD); |
217 | |
218 | FREETMPS; |
219 | LEAVE; |
220 | |
e81bee92 |
221 | if (dd_debug) { |
222 | printf("PL_bufptr: %s\n", PL_bufptr); |
223 | printf("bufend at: %i\n", PL_bufend - PL_bufptr); |
224 | printf("linestr: %s\n", SvPVX(PL_linestr)); |
225 | printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr)); |
226 | printf("actual len: %i\n", strlen(PL_bufptr)); |
227 | } |
94caac6e |
228 | return o; |
229 | } |
230 | |
231 | kid = cUNOPo->op_first; |
232 | |
233 | if (kid->op_type != OP_GV) /* not a GV so ignore */ |
234 | return o; |
235 | |
96f12726 |
236 | if (!DD_AM_LEXING) |
94caac6e |
237 | return o; /* not lexing? */ |
238 | |
e81bee92 |
239 | if (dd_debug) { |
240 | printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv)); |
241 | } |
840ebcbb |
242 | |
72f20f69 |
243 | dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv)); |
94caac6e |
244 | |
72f20f69 |
245 | if (dd_flags == -1) |
94caac6e |
246 | return o; |
247 | |
e81bee92 |
248 | if (dd_debug) { |
249 | printf("dd_flags are: %i\n", dd_flags); |
250 | printf("PL_tokenbuf: %s\n", PL_tokenbuf); |
251 | } |
840ebcbb |
252 | |
a9bd9b5e |
253 | dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv)); |
94caac6e |
254 | |
53e3ab32 |
255 | return o; |
256 | } |
257 | |
bedac9ff |
258 | STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op); |
259 | |
b7505981 |
260 | OP* dd_pp_entereval(pTHX) { |
261 | dSP; |
262 | dPOPss; |
263 | STRLEN len; |
264 | const char* s; |
265 | if (SvPOK(sv)) { |
e81bee92 |
266 | if (dd_debug) { |
267 | printf("mangling eval sv\n"); |
268 | } |
b7505981 |
269 | if (SvREADONLY(sv)) |
270 | sv = sv_2mortal(newSVsv(sv)); |
271 | s = SvPVX(sv); |
272 | len = SvCUR(sv); |
273 | if (!len || s[len-1] != ';') { |
274 | if (!(SvFLAGS(sv) & SVs_TEMP)) |
275 | sv = sv_2mortal(newSVsv(sv)); |
276 | sv_catpvn(sv, "\n;", 2); |
53e3ab32 |
277 | } |
b7505981 |
278 | SvGROW(sv, 8192); |
53e3ab32 |
279 | } |
b7505981 |
280 | PUSHs(sv); |
281 | return PL_ppaddr[OP_ENTEREVAL](aTHX); |
282 | } |
53e3ab32 |
283 | |
b7505981 |
284 | STATIC OP *dd_ck_entereval(pTHX_ OP *o) { |
285 | o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */ |
d8e65fc8 |
286 | if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL]) |
287 | o->op_ppaddr = dd_pp_entereval; |
94caac6e |
288 | return o; |
289 | } |
290 | |
6a0bcf35 |
291 | static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen) |
292 | { |
293 | const I32 count = FILTER_READ(idx+1, sv, maxlen); |
294 | SvGROW(sv, 8192); /* please try not to have a line longer than this :) */ |
295 | /* filter_del(dd_filter_realloc); */ |
296 | return count; |
297 | } |
298 | |
bedac9ff |
299 | STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op); |
300 | |
301 | STATIC OP *dd_ck_const(pTHX_ OP *o) { |
bedac9ff |
302 | int dd_flags; |
04a8a223 |
303 | char* name; |
bedac9ff |
304 | |
305 | o = dd_old_ck_const(aTHX_ o); /* let the original do its job */ |
306 | |
34335b63 |
307 | /* if this is set, we just grabbed a delimited string or something, |
308 | not a bareword, so NO TOUCHY */ |
309 | |
79abf18a |
310 | if (DD_HAVE_LEX_STUFF) |
34335b63 |
311 | return o; |
312 | |
bedac9ff |
313 | /* don't try and look this up if it's not a string const */ |
314 | if (!SvPOK(cSVOPo->op_sv)) |
315 | return o; |
316 | |
04a8a223 |
317 | name = SvPVX(cSVOPo->op_sv); |
318 | |
319 | dd_flags = dd_is_declarator(aTHX_ name); |
bedac9ff |
320 | |
a9fb5fb1 |
321 | if (dd_flags == -1) |
bedac9ff |
322 | return o; |
323 | |
a9bd9b5e |
324 | dd_linestr_callback(aTHX_ "const", name); |
bedac9ff |
325 | |
bedac9ff |
326 | return o; |
327 | } |
328 | |
94caac6e |
329 | static int initialized = 0; |
330 | |
331 | MODULE = Devel::Declare PACKAGE = Devel::Declare |
332 | |
333 | PROTOTYPES: DISABLE |
334 | |
335 | void |
336 | setup() |
337 | CODE: |
338 | if (!initialized++) { |
339 | dd_old_ck_rv2cv = PL_check[OP_RV2CV]; |
340 | PL_check[OP_RV2CV] = dd_ck_rv2cv; |
b7505981 |
341 | dd_old_ck_entereval = PL_check[OP_ENTEREVAL]; |
342 | PL_check[OP_ENTEREVAL] = dd_ck_entereval; |
bedac9ff |
343 | dd_old_ck_const = PL_check[OP_CONST]; |
344 | PL_check[OP_CONST] = dd_ck_const; |
94caac6e |
345 | } |
6a0bcf35 |
346 | filter_add(dd_filter_realloc, NULL); |
569ac469 |
347 | |
348 | char* |
349 | get_linestr() |
350 | CODE: |
351 | RETVAL = dd_get_linestr(aTHX); |
352 | OUTPUT: |
353 | RETVAL |
923c07a8 |
354 | |
355 | void |
356 | set_linestr(char* new_value) |
357 | CODE: |
358 | dd_set_linestr(aTHX_ new_value); |
359 | |
04a8a223 |
360 | char* |
361 | get_lex_stuff() |
362 | CODE: |
363 | RETVAL = dd_get_lex_stuff(aTHX); |
364 | OUTPUT: |
365 | RETVAL |
366 | |
367 | void |
368 | clear_lex_stuff() |
369 | CODE: |
370 | dd_clear_lex_stuff(aTHX); |
371 | |
372 | char* |
373 | get_curstash_name() |
374 | CODE: |
375 | RETVAL = dd_get_curstash_name(aTHX); |
376 | OUTPUT: |
377 | RETVAL |
378 | |
923c07a8 |
379 | int |
022eb0cc |
380 | get_linestr_offset() |
381 | CODE: |
382 | RETVAL = dd_get_linestr_offset(aTHX); |
383 | OUTPUT: |
384 | RETVAL |
385 | |
386 | int |
923c07a8 |
387 | toke_scan_word(int offset, int handle_package) |
388 | CODE: |
389 | RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package); |
390 | OUTPUT: |
391 | RETVAL |
392 | |
393 | int |
04a8a223 |
394 | toke_move_past_token(int offset); |
395 | CODE: |
396 | RETVAL = dd_toke_move_past_token(aTHX_ offset); |
397 | OUTPUT: |
398 | RETVAL |
399 | |
400 | int |
923c07a8 |
401 | toke_scan_str(int offset); |
402 | CODE: |
403 | RETVAL = dd_toke_scan_str(aTHX_ offset); |
404 | OUTPUT: |
405 | RETVAL |
406 | |
407 | int |
408 | toke_skipspace(int offset) |
409 | CODE: |
410 | RETVAL = dd_toke_skipspace(aTHX_ offset); |
411 | OUTPUT: |
412 | RETVAL |
04a8a223 |
413 | |
414 | int |
415 | get_in_declare() |
416 | CODE: |
417 | RETVAL = in_declare; |
418 | OUTPUT: |
419 | RETVAL |
420 | |
421 | void |
422 | set_in_declare(int value) |
423 | CODE: |
424 | in_declare = value; |
e81bee92 |
425 | |
426 | BOOT: |
427 | if (getenv ("DD_DEBUG")) { |
428 | dd_debug = 1; |
429 | } |