Commit | Line | Data |
94caac6e |
1 | #include "EXTERN.h" |
2 | #include "perl.h" |
3 | #include "XSUB.h" |
b15aa864 |
4 | #include "hook_op_check.h" |
94caac6e |
5 | #undef printf |
e807ee50 |
6 | #include "stolen_chunk_of_toke.c" |
94caac6e |
7 | #include <stdio.h> |
8 | #include <string.h> |
9 | |
9673d7ca |
10 | #ifndef Newx |
11 | # define Newx(v,n,t) New(0,v,n,t) |
12 | #endif /* !Newx */ |
13 | |
e81bee92 |
14 | static int dd_debug = 0; |
c630715a |
15 | |
94caac6e |
16 | #define LEX_NORMAL 10 |
17 | #define LEX_INTERPNORMAL 9 |
18 | |
94caac6e |
19 | /* flag to trigger removal of temporary declaree sub */ |
20 | |
21 | static int in_declare = 0; |
22 | |
96f12726 |
23 | /* in 5.10, PL_parser will be NULL if we aren't parsing, and PL_lex_stuff |
24 | is a lookup into it - so if anything else we can use to tell, so we |
25 | need to be a bit more careful if PL_parser exists */ |
26 | |
27 | #define DD_AM_LEXING_CHECK (PL_lex_state == LEX_NORMAL || PL_lex_state == LEX_INTERPNORMAL) |
28 | |
f19b3507 |
29 | #if defined(PL_parser) || defined(PERL_5_9_PLUS) |
96f12726 |
30 | #define DD_HAVE_PARSER PL_parser |
31 | #define DD_HAVE_LEX_STUFF (PL_parser && PL_lex_stuff) |
32 | #define DD_AM_LEXING (PL_parser && DD_AM_LEXING_CHECK) |
33 | #else |
34 | #define DD_HAVE_PARSER 1 |
35 | #define DD_HAVE_LEX_STUFF PL_lex_stuff |
36 | #define DD_AM_LEXING DD_AM_LEXING_CHECK |
37 | #endif |
38 | |
72f20f69 |
39 | /* thing that decides whether we're dealing with a declarator */ |
40 | |
41 | int dd_is_declarator(pTHX_ char* name) { |
42 | HV* is_declarator; |
43 | SV** is_declarator_pack_ref; |
44 | HV* is_declarator_pack_hash; |
45 | SV** is_declarator_flag_ref; |
46 | int dd_flags; |
47 | |
48 | is_declarator = get_hv("Devel::Declare::declarators", FALSE); |
49 | |
50 | if (!is_declarator) |
51 | return -1; |
52 | |
569ac469 |
53 | /* $declarators{$current_package_name} */ |
54 | |
9603b8dc |
55 | if (!HvNAME(PL_curstash)) |
56 | return -1; |
57 | |
72f20f69 |
58 | is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash), |
59 | strlen(HvNAME(PL_curstash)), FALSE); |
60 | |
61 | if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref)) |
62 | return -1; /* not a hashref */ |
63 | |
64 | is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref); |
65 | |
569ac469 |
66 | /* $declarators{$current_package_name}{$name} */ |
67 | |
72f20f69 |
68 | is_declarator_flag_ref = hv_fetch( |
69 | is_declarator_pack_hash, name, |
70 | strlen(name), FALSE |
71 | ); |
72 | |
73 | /* requires SvIOK as well as TRUE since flags not being an int is useless */ |
74 | |
75 | if (!is_declarator_flag_ref |
76 | || !SvIOK(*is_declarator_flag_ref) |
77 | || !SvTRUE(*is_declarator_flag_ref)) |
78 | return -1; |
79 | |
80 | dd_flags = SvIVX(*is_declarator_flag_ref); |
81 | |
82 | return dd_flags; |
83 | } |
84 | |
569ac469 |
85 | /* callback thingy */ |
86 | |
a9bd9b5e |
87 | void dd_linestr_callback (pTHX_ char* type, char* name) { |
569ac469 |
88 | |
89 | char* linestr = SvPVX(PL_linestr); |
a9bd9b5e |
90 | int offset = PL_bufptr - linestr; |
569ac469 |
91 | |
569ac469 |
92 | dSP; |
93 | |
94 | ENTER; |
95 | SAVETMPS; |
96 | |
97 | PUSHMARK(SP); |
98 | XPUSHs(sv_2mortal(newSVpv(type, 0))); |
99 | XPUSHs(sv_2mortal(newSVpv(name, 0))); |
100 | XPUSHs(sv_2mortal(newSViv(offset))); |
101 | PUTBACK; |
102 | |
04a8a223 |
103 | call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD); |
569ac469 |
104 | |
569ac469 |
105 | FREETMPS; |
106 | LEAVE; |
107 | } |
108 | |
109 | char* dd_get_linestr(pTHX) { |
5f0b59d5 |
110 | if (!DD_HAVE_PARSER) { |
111 | return NULL; |
112 | } |
569ac469 |
113 | return SvPVX(PL_linestr); |
114 | } |
115 | |
116 | void dd_set_linestr(pTHX_ char* new_value) { |
117 | int new_len = strlen(new_value); |
569ac469 |
118 | |
ce9a252b |
119 | if (SvLEN(PL_linestr) < new_len) { |
f9eb22eb |
120 | croak("forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr)); |
ce9a252b |
121 | } |
122 | |
123 | SvGROW(PL_linestr, new_len); |
569ac469 |
124 | |
125 | memcpy(SvPVX(PL_linestr), new_value, new_len+1); |
126 | |
127 | SvCUR_set(PL_linestr, new_len); |
128 | |
129 | PL_bufend = SvPVX(PL_linestr) + new_len; |
130 | } |
131 | |
04a8a223 |
132 | char* dd_get_lex_stuff(pTHX) { |
96f12726 |
133 | return (DD_HAVE_LEX_STUFF ? SvPVX(PL_lex_stuff) : ""); |
04a8a223 |
134 | } |
135 | |
bd85a06e |
136 | void dd_clear_lex_stuff(pTHX) { |
96f12726 |
137 | if (DD_HAVE_PARSER) |
f05cbc90 |
138 | PL_lex_stuff = (SV*)NULL; |
04a8a223 |
139 | } |
140 | |
141 | char* dd_get_curstash_name(pTHX) { |
142 | return HvNAME(PL_curstash); |
143 | } |
144 | |
022eb0cc |
145 | int dd_get_linestr_offset(pTHX) { |
0da63271 |
146 | char* linestr; |
147 | if (!DD_HAVE_PARSER) { |
148 | return -1; |
149 | } |
150 | linestr = SvPVX(PL_linestr); |
022eb0cc |
151 | return PL_bufptr - linestr; |
152 | } |
153 | |
840ebcbb |
154 | char* dd_move_past_token (pTHX_ char* s) { |
923c07a8 |
155 | |
156 | /* |
157 | * buffer will be at the beginning of the declarator, -unless- the |
158 | * declarator is at EOL in which case it'll be the next useful line |
159 | * so we don't short-circuit out if we don't find the declarator |
160 | */ |
161 | |
162 | while (s < PL_bufend && isSPACE(*s)) s++; |
163 | if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf))) |
164 | s += strlen(PL_tokenbuf); |
165 | return s; |
166 | } |
167 | |
840ebcbb |
168 | int dd_toke_move_past_token (pTHX_ int offset) { |
04a8a223 |
169 | char* base_s = SvPVX(PL_linestr) + offset; |
170 | char* s = dd_move_past_token(aTHX_ base_s); |
171 | return s - base_s; |
172 | } |
173 | |
923c07a8 |
174 | int dd_toke_scan_word(pTHX_ int offset, int handle_package) { |
175 | char tmpbuf[sizeof PL_tokenbuf]; |
176 | char* base_s = SvPVX(PL_linestr) + offset; |
177 | STRLEN len; |
178 | char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len); |
179 | return s - base_s; |
180 | } |
181 | |
182 | int dd_toke_scan_str(pTHX_ int offset) { |
183 | char* base_s = SvPVX(PL_linestr) + offset; |
184 | char* s = scan_str(base_s, FALSE, FALSE); |
185 | return s - base_s; |
186 | } |
187 | |
188 | int dd_toke_skipspace(pTHX_ int offset) { |
189 | char* base_s = SvPVX(PL_linestr) + offset; |
190 | char* s = skipspace(base_s); |
191 | return s - base_s; |
192 | } |
193 | |
94caac6e |
194 | /* replacement PL_check rv2cv entry */ |
195 | |
6e67754a |
196 | STATIC OP *dd_ck_rv2cv(pTHX_ OP *o, void *user_data) { |
94caac6e |
197 | OP* kid; |
0ba8c7aa |
198 | int dd_flags; |
94caac6e |
199 | |
94caac6e |
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 | |
b7505981 |
258 | OP* dd_pp_entereval(pTHX) { |
259 | dSP; |
260 | dPOPss; |
261 | STRLEN len; |
262 | const char* s; |
263 | if (SvPOK(sv)) { |
e81bee92 |
264 | if (dd_debug) { |
265 | printf("mangling eval sv\n"); |
266 | } |
b7505981 |
267 | if (SvREADONLY(sv)) |
268 | sv = sv_2mortal(newSVsv(sv)); |
269 | s = SvPVX(sv); |
270 | len = SvCUR(sv); |
271 | if (!len || s[len-1] != ';') { |
272 | if (!(SvFLAGS(sv) & SVs_TEMP)) |
273 | sv = sv_2mortal(newSVsv(sv)); |
274 | sv_catpvn(sv, "\n;", 2); |
53e3ab32 |
275 | } |
b7505981 |
276 | SvGROW(sv, 8192); |
53e3ab32 |
277 | } |
b7505981 |
278 | PUSHs(sv); |
279 | return PL_ppaddr[OP_ENTEREVAL](aTHX); |
280 | } |
53e3ab32 |
281 | |
6e67754a |
282 | STATIC OP *dd_ck_entereval(pTHX_ OP *o, void *user_data) { |
d8e65fc8 |
283 | if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL]) |
284 | o->op_ppaddr = dd_pp_entereval; |
94caac6e |
285 | return o; |
286 | } |
287 | |
6a0bcf35 |
288 | static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen) |
289 | { |
290 | const I32 count = FILTER_READ(idx+1, sv, maxlen); |
291 | SvGROW(sv, 8192); /* please try not to have a line longer than this :) */ |
292 | /* filter_del(dd_filter_realloc); */ |
293 | return count; |
294 | } |
295 | |
6e67754a |
296 | STATIC OP *dd_ck_const(pTHX_ OP *o, void *user_data) { |
bedac9ff |
297 | int dd_flags; |
04a8a223 |
298 | char* name; |
bedac9ff |
299 | |
34335b63 |
300 | /* if this is set, we just grabbed a delimited string or something, |
301 | not a bareword, so NO TOUCHY */ |
302 | |
79abf18a |
303 | if (DD_HAVE_LEX_STUFF) |
34335b63 |
304 | return o; |
305 | |
bedac9ff |
306 | /* don't try and look this up if it's not a string const */ |
307 | if (!SvPOK(cSVOPo->op_sv)) |
308 | return o; |
309 | |
04a8a223 |
310 | name = SvPVX(cSVOPo->op_sv); |
311 | |
312 | dd_flags = dd_is_declarator(aTHX_ name); |
bedac9ff |
313 | |
a9fb5fb1 |
314 | if (dd_flags == -1) |
bedac9ff |
315 | return o; |
316 | |
a9bd9b5e |
317 | dd_linestr_callback(aTHX_ "const", name); |
bedac9ff |
318 | |
bedac9ff |
319 | return o; |
320 | } |
321 | |
94caac6e |
322 | static int initialized = 0; |
323 | |
324 | MODULE = Devel::Declare PACKAGE = Devel::Declare |
325 | |
326 | PROTOTYPES: DISABLE |
327 | |
328 | void |
329 | setup() |
330 | CODE: |
331 | if (!initialized++) { |
6e67754a |
332 | hook_op_check(OP_RV2CV, dd_ck_rv2cv, NULL); |
333 | hook_op_check(OP_ENTEREVAL, dd_ck_entereval, NULL); |
334 | hook_op_check(OP_CONST, dd_ck_const, NULL); |
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; |
e81bee92 |
415 | |
416 | BOOT: |
417 | if (getenv ("DD_DEBUG")) { |
418 | dd_debug = 1; |
419 | } |