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