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