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 /* thing that decides whether we're dealing with a declarator */
42 int dd_is_declarator(pTHX_ char* name) {
44 SV** is_declarator_pack_ref;
45 HV* is_declarator_pack_hash;
46 SV** is_declarator_flag_ref;
49 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
54 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
55 strlen(HvNAME(PL_curstash)), FALSE);
57 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
58 return -1; /* not a hashref */
60 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
62 is_declarator_flag_ref = hv_fetch(
63 is_declarator_pack_hash, name,
67 /* requires SvIOK as well as TRUE since flags not being an int is useless */
69 if (!is_declarator_flag_ref
70 || !SvIOK(*is_declarator_flag_ref)
71 || !SvTRUE(*is_declarator_flag_ref))
74 dd_flags = SvIVX(*is_declarator_flag_ref);
79 /* replacement PL_check rv2cv entry */
81 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
83 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
87 char tmpbuf[sizeof PL_tokenbuf];
88 char found_name[sizeof PL_tokenbuf];
89 char* found_proto = NULL, *found_traits = NULL;
93 dSP; /* define stack pointer for later call stuff */
95 STRLEN n_a; /* for POPpx */
97 o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
102 printf("Deconstructing declare\n");
103 printf("PL_bufptr: %s\n", PL_bufptr);
104 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
105 printf("linestr: %s\n", SvPVX(PL_linestr));
106 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
108 call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
111 printf("PL_bufptr: %s\n", PL_bufptr);
112 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
113 printf("linestr: %s\n", SvPVX(PL_linestr));
114 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
115 printf("actual len: %i\n", strlen(PL_bufptr));
120 kid = cUNOPo->op_first;
122 if (kid->op_type != OP_GV) /* not a GV so ignore */
125 if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
126 return o; /* not lexing? */
128 /* I was doing this, but the CONST wrap can't so it didn't gain anything
129 stash = GvSTASH(kGVOP_gv); */
132 printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
135 dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
141 printf("dd_flags are: %i\n", dd_flags);
144 s = PL_bufptr; /* copy the current buffer pointer */
149 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
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
158 while (s < PL_bufend && isSPACE(*s)) s++;
159 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
160 s += strlen(PL_tokenbuf);
164 if (dd_flags & DD_HANDLE_NAME) {
172 /* kill the :: added in the ck_const */
178 /* arg 4 is allow_package */
180 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
185 strcpy(found_name, tmpbuf);
187 printf("Found %s\n", found_name);
192 if (dd_flags & DD_HANDLE_PROTO) {
196 if (*s == '(') { /* found a prototype-ish thing */
198 s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
201 char *traitstart = s = skipspace(s);
203 while (*s && *s != '{') ++s;
205 int tlen = s - traitstart;
206 Newx(found_traits, tlen+1, char);
207 Copy(traitstart, found_traits, tlen, char);
208 found_traits[tlen] = 0;
210 printf("found traits..... (%s)\n", found_traits);
216 if (SvPOK(PL_lex_stuff)) {
218 printf("Found proto %s\n", SvPVX(PL_lex_stuff));
220 found_proto = SvPVX(PL_lex_stuff);
221 if (len) /* foo name () => foo name X, only foo parsed so works */
223 else /* foo () => foo =X, TOKEN('&') won't handle foo X */
230 printf("Curbuf %s\n", PL_bufptr);
240 printf("Calling init_declare\n");
242 cb_args[0] = HvNAME(PL_curstash);
243 cb_args[1] = GvNAME(kGVOP_gv);
244 cb_args[2] = HvNAME(PL_curstash);
245 cb_args[3] = found_name;
246 cb_args[4] = found_proto;
247 cb_args[5] = found_traits;
250 if (len && found_proto)
252 else if (len || found_proto)
255 PL_lex_stuff = Nullsv;
258 printf("cur buf: %s\n", s);
259 printf("bufend at: %i\n", PL_bufend - s);
260 printf("linestr: %s\n", SvPVX(PL_linestr));
261 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
265 call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
269 if (retstr && strlen(retstr)) {
270 const char* old_start = SvPVX(PL_linestr);
272 const int old_len = SvCUR(PL_linestr);
274 printf("Got string %s\n", retstr);
276 SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr)));
277 if (start_diff = SvPVX(PL_linestr) - old_start) {
278 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
280 memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
281 memmove(s, retstr, strlen(retstr));
282 SvCUR_set(PL_linestr, old_len + strlen(retstr));
283 PL_bufend += strlen(retstr);
285 printf("cur buf: %s\n", s);
286 printf("PL_bufptr: %s\n", PL_bufptr);
287 printf("bufend at: %i\n", PL_bufend - s);
288 printf("linestr: %s\n", SvPVX(PL_linestr));
289 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
290 printf("tokenbuf now: %s\n", PL_tokenbuf);
294 call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
299 STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op);
301 OP* dd_pp_entereval(pTHX) {
308 printf("mangling eval sv\n");
311 sv = sv_2mortal(newSVsv(sv));
314 if (!len || s[len-1] != ';') {
315 if (!(SvFLAGS(sv) & SVs_TEMP))
316 sv = sv_2mortal(newSVsv(sv));
317 sv_catpvn(sv, "\n;", 2);
322 return PL_ppaddr[OP_ENTEREVAL](aTHX);
325 STATIC OP *dd_ck_entereval(pTHX_ OP *o) {
326 o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */
327 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
328 o->op_ppaddr = dd_pp_entereval;
332 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
334 const I32 count = FILTER_READ(idx+1, sv, maxlen);
335 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
336 /* filter_del(dd_filter_realloc); */
340 STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op);
342 STATIC OP *dd_ck_const(pTHX_ OP *o) {
345 char tmpbuf[sizeof PL_tokenbuf];
346 char found_name[sizeof PL_tokenbuf];
349 o = dd_old_ck_const(aTHX_ o); /* let the original do its job */
351 /* don't try and look this up if it's not a string const */
352 if (!SvPOK(cSVOPo->op_sv))
355 dd_flags = dd_is_declarator(aTHX_ SvPVX(cSVOPo->op_sv));
360 if (!(dd_flags & DD_HANDLE_NAME))
361 return o; /* if we're not handling name, method intuiting not an issue */
364 printf("Think I found a declarator %s\n", PL_tokenbuf);
365 printf("linestr: %s\n", SvPVX(PL_linestr));
370 while (s < PL_bufend && isSPACE(*s)) s++;
371 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
372 s += strlen(PL_tokenbuf);
382 /* arg 4 is allow_package */
384 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
389 const char* old_start = SvPVX(PL_linestr);
391 const int old_len = SvCUR(PL_linestr);
393 strcpy(found_name, tmpbuf);
395 printf("Found %s\n", found_name);
399 SvGROW(PL_linestr, (STRLEN)(old_len + 2));
400 if (start_diff = SvPVX(PL_linestr) - old_start) {
401 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
403 memmove(s+2, s, (PL_bufend - s)+1);
407 SvCUR_set(PL_linestr, old_len + 2);
413 static int initialized = 0;
415 MODULE = Devel::Declare PACKAGE = Devel::Declare
422 if (!initialized++) {
423 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
424 PL_check[OP_RV2CV] = dd_ck_rv2cv;
425 dd_old_ck_entereval = PL_check[OP_ENTEREVAL];
426 PL_check[OP_ENTEREVAL] = dd_ck_entereval;
427 dd_old_ck_const = PL_check[OP_CONST];
428 PL_check[OP_CONST] = dd_ck_const;
430 filter_add(dd_filter_realloc, NULL);