2 #define PERL_NO_GET_CONTEXT
7 #include "stolen_chunk_of_toke.c"
12 # define Newx(v,n,t) New(0,v,n,t)
20 #define DD_DEBUG_S printf("Buffer: %s\n", s);
26 #define LEX_INTERPNORMAL 9
28 /* flag to trigger removal of temporary declaree sub */
30 static int in_declare = 0;
32 /* thing that decides whether we're dealing with a declarator */
34 int dd_is_declarator(pTHX_ char* name) {
36 SV** is_declarator_pack_ref;
37 HV* is_declarator_pack_hash;
38 SV** is_declarator_flag_ref;
41 is_declarator = get_hv("Devel::Declare::declarators", FALSE);
46 /* $declarators{$current_package_name} */
48 is_declarator_pack_ref = hv_fetch(is_declarator, HvNAME(PL_curstash),
49 strlen(HvNAME(PL_curstash)), FALSE);
51 if (!is_declarator_pack_ref || !SvROK(*is_declarator_pack_ref))
52 return -1; /* not a hashref */
54 is_declarator_pack_hash = (HV*) SvRV(*is_declarator_pack_ref);
56 /* $declarators{$current_package_name}{$name} */
58 is_declarator_flag_ref = hv_fetch(
59 is_declarator_pack_hash, name,
63 /* requires SvIOK as well as TRUE since flags not being an int is useless */
65 if (!is_declarator_flag_ref
66 || !SvIOK(*is_declarator_flag_ref)
67 || !SvTRUE(*is_declarator_flag_ref))
70 dd_flags = SvIVX(*is_declarator_flag_ref);
77 void dd_linestr_callback (pTHX_ char* type, char* name) {
79 char* linestr = SvPVX(PL_linestr);
80 int offset = PL_bufptr - linestr;
88 XPUSHs(sv_2mortal(newSVpv(type, 0)));
89 XPUSHs(sv_2mortal(newSVpv(name, 0)));
90 XPUSHs(sv_2mortal(newSViv(offset)));
93 call_pv("Devel::Declare::linestr_callback", G_VOID|G_DISCARD);
99 char* dd_get_linestr(pTHX) {
100 return SvPVX(PL_linestr);
103 void dd_set_linestr(pTHX_ char* new_value) {
104 int new_len = strlen(new_value);
105 char* old_linestr = SvPVX(PL_linestr);
107 SvGROW(PL_linestr, strlen(new_value));
109 if (SvPVX(PL_linestr) != old_linestr)
110 Perl_croak(aTHX_ "forced to realloc PL_linestr for line %s, bailing out before we crash harder", SvPVX(PL_linestr));
112 memcpy(SvPVX(PL_linestr), new_value, new_len+1);
114 SvCUR_set(PL_linestr, new_len);
116 PL_bufend = SvPVX(PL_linestr) + new_len;
119 char* dd_get_lex_stuff(pTHX) {
120 return SvPVX(PL_lex_stuff);
123 char* dd_clear_lex_stuff(pTHX) {
124 PL_lex_stuff = Nullsv;
127 char* dd_get_curstash_name(pTHX) {
128 return HvNAME(PL_curstash);
131 char* dd_move_past_token(pTHX_ char* s) {
134 * buffer will be at the beginning of the declarator, -unless- the
135 * declarator is at EOL in which case it'll be the next useful line
136 * so we don't short-circuit out if we don't find the declarator
139 while (s < PL_bufend && isSPACE(*s)) s++;
140 if (memEQ(s, PL_tokenbuf, strlen(PL_tokenbuf)))
141 s += strlen(PL_tokenbuf);
145 int dd_toke_move_past_token(pTHX_ int offset) {
146 char* base_s = SvPVX(PL_linestr) + offset;
147 char* s = dd_move_past_token(aTHX_ base_s);
151 int dd_toke_scan_word(pTHX_ int offset, int handle_package) {
152 char tmpbuf[sizeof PL_tokenbuf];
153 char* base_s = SvPVX(PL_linestr) + offset;
155 char* s = scan_word(base_s, tmpbuf, sizeof tmpbuf, handle_package, &len);
159 int dd_toke_scan_str(pTHX_ int offset) {
160 char* base_s = SvPVX(PL_linestr) + offset;
161 char* s = scan_str(base_s, FALSE, FALSE);
165 int dd_toke_skipspace(pTHX_ int offset) {
166 char* base_s = SvPVX(PL_linestr) + offset;
167 char* s = skipspace(base_s);
171 /* replacement PL_check rv2cv entry */
173 STATIC OP *(*dd_old_ck_rv2cv)(pTHX_ OP *op);
175 STATIC OP *dd_ck_rv2cv(pTHX_ OP *o) {
180 o = dd_old_ck_rv2cv(aTHX_ o); /* let the original do its job */
185 call_pv("Devel::Declare::done_declare", G_VOID|G_DISCARD);
190 kid = cUNOPo->op_first;
192 if (kid->op_type != OP_GV) /* not a GV so ignore */
195 if (PL_lex_state != LEX_NORMAL && PL_lex_state != LEX_INTERPNORMAL)
196 return o; /* not lexing? */
198 dd_flags = dd_is_declarator(aTHX_ GvNAME(kGVOP_gv));
203 dd_linestr_callback(aTHX_ "rv2cv", GvNAME(kGVOP_gv));
208 STATIC OP *(*dd_old_ck_entereval)(pTHX_ OP *op);
210 OP* dd_pp_entereval(pTHX) {
217 printf("mangling eval sv\n");
220 sv = sv_2mortal(newSVsv(sv));
223 if (!len || s[len-1] != ';') {
224 if (!(SvFLAGS(sv) & SVs_TEMP))
225 sv = sv_2mortal(newSVsv(sv));
226 sv_catpvn(sv, "\n;", 2);
231 return PL_ppaddr[OP_ENTEREVAL](aTHX);
234 STATIC OP *dd_ck_entereval(pTHX_ OP *o) {
235 o = dd_old_ck_entereval(aTHX_ o); /* let the original do its job */
236 if (o->op_ppaddr == PL_ppaddr[OP_ENTEREVAL])
237 o->op_ppaddr = dd_pp_entereval;
241 static I32 dd_filter_realloc(pTHX_ int idx, SV *sv, int maxlen)
243 const I32 count = FILTER_READ(idx+1, sv, maxlen);
244 SvGROW(sv, 8192); /* please try not to have a line longer than this :) */
245 /* filter_del(dd_filter_realloc); */
249 STATIC OP *(*dd_old_ck_const)(pTHX_ OP*op);
251 STATIC OP *dd_ck_const(pTHX_ OP *o) {
256 o = dd_old_ck_const(aTHX_ o); /* let the original do its job */
258 /* don't try and look this up if it's not a string const */
259 if (!SvPOK(cSVOPo->op_sv))
262 name = SvPVX(cSVOPo->op_sv);
264 dd_flags = dd_is_declarator(aTHX_ name);
269 dd_linestr_callback(aTHX_ "const", name);
274 static int initialized = 0;
276 MODULE = Devel::Declare PACKAGE = Devel::Declare
283 if (!initialized++) {
284 dd_old_ck_rv2cv = PL_check[OP_RV2CV];
285 PL_check[OP_RV2CV] = dd_ck_rv2cv;
286 dd_old_ck_entereval = PL_check[OP_ENTEREVAL];
287 PL_check[OP_ENTEREVAL] = dd_ck_entereval;
288 dd_old_ck_const = PL_check[OP_CONST];
289 PL_check[OP_CONST] = dd_ck_const;
291 filter_add(dd_filter_realloc, NULL);
296 RETVAL = dd_get_linestr(aTHX);
301 set_linestr(char* new_value)
303 dd_set_linestr(aTHX_ new_value);
308 RETVAL = dd_get_lex_stuff(aTHX);
315 dd_clear_lex_stuff(aTHX);
320 RETVAL = dd_get_curstash_name(aTHX);
325 toke_scan_word(int offset, int handle_package)
327 RETVAL = dd_toke_scan_word(aTHX_ offset, handle_package);
332 toke_move_past_token(int offset);
334 RETVAL = dd_toke_move_past_token(aTHX_ offset);
339 toke_scan_str(int offset);
341 RETVAL = dd_toke_scan_str(aTHX_ offset);
346 toke_skipspace(int offset)
348 RETVAL = dd_toke_skipspace(aTHX_ offset);
360 set_in_declare(int value)