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 /* $declarators{$current_package_name} */
56 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
57 strlen(HvNAME(PL_curstash)), FALSE);
59 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
60 return -1; /* not a hashref */
62 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
64 /* $declarators{$current_package_name}{$name} */
66 is_declarator_flag_ref = hv_fetch(
67 is_declarator_pack_hash, name,
71 /* requires SvIOK as well as TRUE since flags not being an int is useless */
73 if (!is_declarator_flag_ref
74 || !SvIOK(*is_declarator_flag_ref)
75 || !SvTRUE(*is_declarator_flag_ref))
78 dd_flags = SvIVX(*is_declarator_flag_ref);
85 void dd_linestr_callback (pTHX_ char* type, char* name, char* s) {
87 char* linestr = SvPVX(PL_linestr);
88 int offset = s - linestr;
99 XPUSHs(sv_2mortal(newSVpv(type, 0)));
100 XPUSHs(sv_2mortal(newSVpv(name, 0)));
101 XPUSHs(sv_2mortal(newSViv(offset)));
104 count = call_pv("Devel::Declare::linestr_callback", G_SCALAR);
109 Perl_croak(aTHX_ "linestr_callback didn't return a value, bailing out");
111 printf("linestr_callback returned: %s\n", POPp);
118 char* dd_get_linestr(pTHX) {
119 return SvPVX(PL_linestr);
122 void dd_set_linestr(pTHX_ char* new_value) {
123 int new_len = strlen(new_value);
124 char* old_linestr = SvPVX(PL_linestr);
126 SvGROW(PL_linestr, strlen(new_value));
128 if (SvPVX(PL_linestr) != old_linestr)
129 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
131 memcpy(SvPVX(PL_linestr), new_value, new_len+1);
133 SvCUR_set(PL_linestr, new_len);
135 PL_bufend = SvPVX(PL_linestr) + new_len;
138 /* replacement PL_check rv2cv entry */
140 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
142 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
146 char tmpbuf[sizeof PL_tokenbuf];
147 char found_name[sizeof PL_tokenbuf];
148 char* found_proto = NULL, *found_traits = NULL;
152 dSP; /* define stack pointer for later call stuff */
154 STRLEN n_a; /* for POPpx */
156 o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
161 printf("Deconstructing declare\n");
162 printf("PL_bufptr: %s\n", PL_bufptr);
163 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
164 printf("linestr: %s\n", SvPVX(PL_linestr));
165 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
167 call_argv("Devel::Declare::done_declare", G_VOID|G_DISCARD, cb_args);
170 printf("PL_bufptr: %s\n", PL_bufptr);
171 printf("bufend at: %i\n", PL_bufend - PL_bufptr);
172 printf("linestr: %s\n", SvPVX(PL_linestr));
173 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
174 printf("actual len: %i\n", strlen(PL_bufptr));
179 kid = cUNOPo->op_first;
181 if (kid->op_type != OP_GV) /* not a GV so ignore */
184 if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
185 return o; /* not lexing? */
187 /* I was doing this, but the CONST wrap can't so it didn't gain anything
188 stash = GvSTASH(kGVOP_gv); */
191 printf("Checking GV %s -> %s\n", HvNAME(GvSTASH(kGVOP_gv)), GvNAME(kGVOP_gv));
194 dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
200 printf("dd_flags are: %i\n", dd_flags);
203 s = PL_bufptr; /* copy the current buffer pointer */
208 printf("PL_tokenbuf: %s\n", PL_tokenbuf);
212 * buffer will be at the beginning of the declarator, -unless- the
213 * declarator is at EOL in which case it'll be the next useful line
214 * so we don't short-circuit out if we don't find the declarator
217 while (s < PL_bufend && isSPACE(*s)) s++;
218 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
219 s += strlen(PL_tokenbuf);
223 if (dd_flags & DD_HANDLE_NAME) {
231 /* kill the :: added in the ck_const */
237 /* arg 4 is allow_package */
239 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
244 strcpy(found_name, tmpbuf);
246 printf("Found %s\n", found_name);
251 if (dd_flags & DD_HANDLE_PROTO) {
255 if (*s == '(') { /* found a prototype-ish thing */
257 s = scan_str(s, FALSE, FALSE); /* no keep_quoted, no keep_delims */
260 char *traitstart = s = skipspace(s);
262 while (*s && *s != '{') ++s;
264 int tlen = s - traitstart;
265 Newx(found_traits, tlen+1, char);
266 Copy(traitstart, found_traits, tlen, char);
267 found_traits[tlen] = 0;
269 printf("found traits..... (%s)\n", found_traits);
275 if (SvPOK(PL_lex_stuff)) {
277 printf("Found proto %s\n", SvPVX(PL_lex_stuff));
279 found_proto = SvPVX(PL_lex_stuff);
280 if (len) /* foo name () => foo name X, only foo parsed so works */
282 else /* foo () => foo =X, TOKEN('&') won't handle foo X */
289 printf("Curbuf %s\n", PL_bufptr);
299 printf("Calling init_declare\n");
301 cb_args[0] = HvNAME(PL_curstash);
302 cb_args[1] = GvNAME(kGVOP_gv);
303 cb_args[2] = HvNAME(PL_curstash);
304 cb_args[3] = found_name;
305 cb_args[4] = found_proto;
306 cb_args[5] = found_traits;
309 if (len && found_proto)
311 else if (len || found_proto)
314 PL_lex_stuff = Nullsv;
317 printf("cur buf: %s\n", s);
318 printf("bufend at: %i\n", PL_bufend - s);
319 printf("linestr: %s\n", SvPVX(PL_linestr));
320 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
324 call_argv("Devel::Declare::init_declare", G_SCALAR, cb_args);
328 if (retstr && strlen(retstr)) {
329 const char* old_start = SvPVX(PL_linestr);
331 const int old_len = SvCUR(PL_linestr);
333 printf("Got string %s\n", retstr);
335 SvGROW(PL_linestr, (STRLEN)(old_len + strlen(retstr)));
336 if (start_diff = SvPVX(PL_linestr) - old_start) {
337 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
339 memmove(s+strlen(retstr), s, (PL_bufend - s)+1);
340 memmove(s, retstr, strlen(retstr));
341 SvCUR_set(PL_linestr, old_len + strlen(retstr));
342 PL_bufend += strlen(retstr);
344 printf("cur buf: %s\n", s);
345 printf("PL_bufptr: %s\n", PL_bufptr);
346 printf("bufend at: %i\n", PL_bufend - s);
347 printf("linestr: %s\n", SvPVX(PL_linestr));
348 printf("linestr len: %i\n", PL_bufend - SvPVX(PL_linestr));
349 printf("tokenbuf now: %s\n", PL_tokenbuf);
353 call_argv("Devel::Declare::init_declare", G_VOID|G_DISCARD, cb_args);
358 STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op);
360 OP* dd_pp_entereval(pTHX) {
367 printf("mangling eval sv\n");
370 sv = sv_2mortal(newSVsv(sv));
373 if (!len || s[len-1] != ';') {
374 if (!(SvFLAGS(sv) & SVs_TEMP))
375 sv = sv_2mortal(newSVsv(sv));
376 sv_catpvn(sv, "\n;", 2);
381 return PL_ppaddr[OP_ENTEREVAL](aTHX);
384 STATIC OP *dd_ck_entereval(pTHX_ OP *o) {
385 o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */
386 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
387 o->op_ppaddr = dd_pp_entereval;
391 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
393 const I32 count = FILTER_READ(idx+1, sv, maxlen);
394 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
395 /* filter_del(dd_filter_realloc); */
399 STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op);
401 STATIC OP *dd_ck_const(pTHX_ OP *o) {
404 char tmpbuf[sizeof PL_tokenbuf];
405 char found_name[sizeof PL_tokenbuf];
408 o = dd_old_ck_const(aTHX_ o); /* let the original do its job */
410 /* don't try and look this up if it's not a string const */
411 if (!SvPOK(cSVOPo->op_sv))
414 dd_flags = dd_is_declarator(aTHX_ SvPVX(cSVOPo->op_sv));
419 if (!(dd_flags & DD_HANDLE_NAME))
420 return o; /* if we're not handling name, method intuiting not an issue */
423 printf("Think I found a declarator %s\n", PL_tokenbuf);
424 printf("linestr: %s\n", SvPVX(PL_linestr));
429 while (s < PL_bufend && isSPACE(*s)) s++;
430 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
431 s += strlen(PL_tokenbuf);
433 /* dd_linestr_callback(aTHX_ "const", SvPVX(cSVOPo->op_sv), s); */
443 /* arg 4 is allow_package */
445 s = scan_word(s, tmpbuf, sizeof tmpbuf, dd_flags & DD_HANDLE_PACKAGE, &len);
450 const char* old_start = SvPVX(PL_linestr);
452 const int old_len = SvCUR(PL_linestr);
454 strcpy(found_name, tmpbuf);
456 printf("Found %s\n", found_name);
460 SvGROW(PL_linestr, (STRLEN)(old_len + 2));
461 if (start_diff = SvPVX(PL_linestr) - old_start) {
462 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
464 memmove(s+2, s, (PL_bufend - s)+1);
468 SvCUR_set(PL_linestr, old_len + 2);
474 static int initialized = 0;
476 MODULE = Devel::Declare PACKAGE = Devel::Declare
483 if (!initialized++) {
484 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
485 PL_check[OP_RV2CV] = dd_ck_rv2cv;
486 dd_old_ck_entereval = PL_check[OP_ENTEREVAL];
487 PL_check[OP_ENTEREVAL] = dd_ck_entereval;
488 dd_old_ck_const = PL_check[OP_CONST];
489 PL_check[OP_CONST] = dd_ck_const;
491 filter_add(dd_filter_realloc, NULL);
496 RETVAL = dd_get_linestr(aTHX);