2 #define PERL_NO_GET_CONTEXT
7 #include "stolen_chunk_of_toke.c"
12 # define Newx(v,n,t) New(0,v,n,t)
23 #define DD_HANDLE_NAME 1
24 #define DD_HANDLE_PROTO 2
25 #define DD_HANDLE_PACKAGE 8
28 #define DD_DEBUG_S printf("Buffer: %s\n", s);
34 #define LEX_INTERPNORMAL 9
36 /* flag to trigger removal of temporary declaree sub */
38 static int in_declare = 0;
40 /* replacement PL_check rv2cv entry */
42 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
44 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
48 char tmpbuf[sizeof PL_tokenbuf];
49 char found_name[sizeof PL_tokenbuf];
50 char* found_proto = NULL, *found_traits = NULL;
54 SV** is_declarator_pack_ref;
55 HV* is_declarator_pack_hash;
56 SV** is_declarator_flag_ref;
59 dSP; /* define stack pointer for later call stuff */
61 STRLEN n_a; /* for POPpx */
63 o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
68 printf("Deconstructing declare\n");
69 printf("PL_bufptr: %s\n", PL_bufptr);
70 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
71 printf("linestr: %s\n", SvPVX(PL_linestr));
72 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
74 call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
77 printf("PL_bufptr: %s\n", PL_bufptr);
78 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
79 printf("linestr: %s\n", SvPVX(PL_linestr));
80 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
81 printf("actual len: %i\n", strlen(PL_bufptr));
86 kid = cUNOPo->op_first;
88 if (kid->op_type != OP_GV) /* not a GV so ignore */
91 if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
92 return o; /* not lexing? */
94 stash = GvSTASH(kGVOP_gv);
97 printf("Checking GV %s -> %s\n", HvNAME(stash), GvNAME(kGVOP_gv));
100 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
105 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(stash),
106 strlen(HvNAME(stash)), FALSE);
108 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
109 return o; /* not a hashref */
111 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
113 is_declarator_flag_ref = hv_fetch(is_declarator_pack_hash, GvNAME(kGVOP_gv),
114 strlen(GvNAME(kGVOP_gv)), FALSE);
116 /* requires SvIOK as well as TRUE since flags not being an int is useless */
118 if (!is_declarator_flag_ref
119 || !SvIOK(*is_declarator_flag_ref)
120 || !SvTRUE(*is_declarator_flag_ref))
123 dd_flags = SvIVX(*is_declarator_flag_ref);
126 printf("dd_flags are: %i\n", dd_flags);
129 s = PL_bufptr; /* copy the current buffer pointer */
134 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
138 * buffer will be at the beginning of the declarator, -unless- the
139 * declarator is at EOL in which case it'll be the next useful line
140 * so we don't short-circuit out if we don't find the declarator
143 while (s < PL_bufend && isSPACE(*s)) s++;
144 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
145 s += strlen(PL_tokenbuf);
149 if (dd_flags & DD_HANDLE_NAME) {
157 /* kill the :: added in the ck_const */
163 /* arg 4 is allow_package */
165 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
170 strcpy(found_name, tmpbuf);
172 printf("Found %s\n", found_name);
177 if (dd_flags & DD_HANDLE_PROTO) {
181 if (*s == '(') { /* found a prototype-ish thing */
183 s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
186 char *traitstart = s = skipspace(s);
188 while (*s && *s != '{') ++s;
190 int tlen = s - traitstart;
191 Newx(found_traits, tlen+1, char);
192 Copy(traitstart, found_traits, tlen, char);
193 found_traits[tlen] = 0;
195 printf("found traits..... (%s)\n", found_traits);
201 if (SvPOK(PL_lex_stuff)) {
203 printf("Found proto %s\n", SvPVX(PL_lex_stuff));
205 found_proto = SvPVX(PL_lex_stuff);
206 if (len) /* foo name () => foo name X, only foo parsed so works */
208 else /* foo () => foo =X, TOKEN('&') won't handle foo X */
215 printf("Curbuf %s\n", PL_bufptr);
225 printf("Calling init_declare\n");
227 cb_args[0] = HvNAME(stash);
228 cb_args[1] = GvNAME(kGVOP_gv);
229 cb_args[2] = HvNAME(PL_curstash);
230 cb_args[3] = found_name;
231 cb_args[4] = found_proto;
232 cb_args[5] = found_traits;
235 if (len && found_proto)
237 else if (len || found_proto)
240 PL_lex_stuff = Nullsv;
243 printf("cur buf: %s\n", s);
244 printf("bufend at: %i\n", PL_bufend - s);
245 printf("linestr: %s\n", SvPVX(PL_linestr));
246 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
249 call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
253 if (retstr && strlen(retstr)) {
254 const char* old_start = SvPVX(PL_linestr);
256 const int old_len = SvCUR(PL_linestr);
258 printf("Got string %s\n", retstr);
260 SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr)));
261 if (start_diff = SvPVX(PL_linestr) - old_start) {
262 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
264 memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
265 memmove(s, retstr, strlen(retstr));
266 SvCUR_set(PL_linestr, old_len + strlen(retstr));
267 PL_bufend += strlen(retstr);
269 printf("cur buf: %s\n", s);
270 printf("PL_bufptr: %s\n", PL_bufptr);
271 printf("bufend at: %i\n", PL_bufend - s);
272 printf("linestr: %s\n", SvPVX(PL_linestr));
273 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
274 printf("tokenbuf now: %s\n", PL_tokenbuf);
278 call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
283 STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op);
285 OP* dd_pp_entereval(pTHX) {
292 printf("mangling eval sv\n");
295 sv = sv_2mortal(newSVsv(sv));
298 if (!len || s[len-1] != ';') {
299 if (!(SvFLAGS(sv) & SVs_TEMP))
300 sv = sv_2mortal(newSVsv(sv));
301 sv_catpvn(sv, "\n;", 2);
306 return PL_ppaddr[OP_ENTEREVAL](aTHX);
309 STATIC OP *dd_ck_entereval(pTHX_ OP *o) {
310 o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */
311 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
312 o->op_ppaddr = dd_pp_entereval;
316 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
318 const I32 count = FILTER_READ(idx+1, sv, maxlen);
319 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
320 /* filter_del(dd_filter_realloc); */
324 STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op);
326 STATIC OP *dd_ck_const(pTHX_ OP *o) {
328 SV** is_declarator_pack_ref;
329 HV* is_declarator_pack_hash;
330 SV** is_declarator_flag_ref;
333 char tmpbuf[sizeof PL_tokenbuf];
334 char found_name[sizeof PL_tokenbuf];
337 o = dd_old_ck_const(aTHX_ o); /* let the original do its job */
339 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
341 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
342 strlen(HvNAME(PL_curstash)), FALSE);
344 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
345 return o; /* not a hashref */
347 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
349 /* don't try and look this up if it's not a string const */
350 if (!SvPOK(cSVOPo->op_sv))
353 is_declarator_flag_ref = hv_fetch(
354 is_declarator_pack_hash, SvPVX(cSVOPo->op_sv),
355 strlen(SvPVX(cSVOPo->op_sv)), FALSE
358 /* requires SvIOK as well as TRUE since flags not being an int is useless */
360 if (!is_declarator_flag_ref
361 || !SvIOK(*is_declarator_flag_ref)
362 || !SvTRUE(*is_declarator_flag_ref))
365 dd_flags = SvIVX(*is_declarator_flag_ref);
367 if (!(dd_flags & DD_HANDLE_NAME))
368 return o; /* if we're not handling name, method intuiting not an issue */
371 printf("Think I found a declarator %s\n", PL_tokenbuf);
372 printf("linestr: %s\n", SvPVX(PL_linestr));
377 while (s < PL_bufend && isSPACE(*s)) s++;
378 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
379 s += strlen(PL_tokenbuf);
389 /* arg 4 is allow_package */
391 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
396 const char* old_start = SvPVX(PL_linestr);
398 const int old_len = SvCUR(PL_linestr);
400 strcpy(found_name, tmpbuf);
402 printf("Found %s\n", found_name);
406 SvGROW(PL_linestr, (STRLEN)(old_len + 2));
407 if (start_diff = SvPVX(PL_linestr) - old_start) {
408 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
410 memmove(s+2, s, (PL_bufend - s)+1);
414 SvCUR_set(PL_linestr, old_len + 2);
420 static int initialized = 0;
422 MODULE = Devel::Declare PACKAGE = Devel::Declare
429 if (!initialized++) {
430 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
431 PL_check[OP_RV2CV] = dd_ck_rv2cv;
432 dd_old_ck_entereval = PL_check[OP_ENTEREVAL];
433 PL_check[OP_ENTEREVAL] = dd_ck_entereval;
434 dd_old_ck_const = PL_check[OP_CONST];
435 PL_check[OP_CONST] = dd_ck_const;
437 filter_add(dd_filter_realloc, NULL);