1 /* This file is part of the indirect Perl module.
2 * See http://search.cpan.org/dist/indirect/ */
4 #define PERL_NO_GET_CONTEXT
10 # define SvPV_const SvPV
13 #ifndef SvPV_nolen_const
14 # define SvPV_nolen_const SvPV_nolen
18 # define SvPVX_const SvPVX
21 #ifndef sv_catpvn_nomg
22 # define sv_catpvn_nomg sv_catpvn
25 /* ... Hints ............................................................... */
27 STATIC U32 indirect_hash = 0;
29 STATIC IV indirect_hint(pTHX) {
30 #define indirect_hint() indirect_hint(aTHX)
31 SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
36 return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
39 /* ... op -> source position ............................................... */
41 STATIC HV *indirect_map = NULL;
42 STATIC const char *indirect_linestr = NULL;
44 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
46 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
47 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
49 const char *pl_linestr;
52 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
53 * In this case the linestr has temporarly changed, but the old buffer should
54 * still be alive somewhere. */
56 if (!PL_parser->lex_inwhat) {
57 pl_linestr = SvPVX_const(PL_parser->linestr);
58 if (indirect_linestr != pl_linestr) {
59 hv_clear(indirect_map);
60 indirect_linestr = pl_linestr;
65 SvUPGRADE(val, SVt_PVIV);
66 SvUVX(val) = PTR2UV(src);
69 if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
72 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
73 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
77 if (indirect_linestr != SvPVX_const(PL_parser->linestr))
80 val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
87 return INT2PTR(const char *, SvUVX(*val));
90 STATIC void indirect_map_delete(pTHX_ const OP *o) {
91 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
94 hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
97 STATIC void indirect_map_clean(pTHX_ const OP *o) {
98 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
99 if (o->op_flags & OPf_KIDS) {
100 const OP *kid = cUNOPo->op_first;
101 for (; kid; kid = kid->op_sibling) {
102 indirect_map_delete(kid);
103 indirect_map_clean(kid);
106 indirect_map_delete(o);
110 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
111 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
113 const char *p = NULL, *r = SvPV_const(sv, len);
120 p = strstr(p + 1, r);
126 /* ... ck_const ............................................................ */
128 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
130 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
131 o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
133 if (indirect_hint()) {
135 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
136 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
142 /* ... ck_rv2sv ............................................................ */
144 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
146 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
147 if (indirect_hint()) {
148 OP *op = cUNOPo->op_first;
150 const char *name = NULL, *s;
153 switch (op->op_type) {
156 GV *gv = cGVOPx_gv(op);
162 SV *sv = cSVOPx_sv(op);
163 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
164 name = SvPV_const(sv, len);
171 sv = sv_2mortal(newSVpvn("$", 1));
172 sv_catpvn_nomg(sv, name, len);
173 s = indirect_find(sv, PL_parser->oldbufptr);
174 if (!s) { /* If it failed, retry without the current stash */
175 const char *stash = HvNAME_get(PL_curstash);
176 STRLEN stashlen = HvNAMELEN_get(PL_curstash);
178 if ((len < stashlen + 2) || name != strstr(name, stash)) {
179 /* Failed again ? Try to remove main */
182 if ((len < stashlen + 2) || name != strstr(name, stash))
185 if (name[stashlen] != ':' || name[stashlen+1] != ':')
188 sv_setpvn(sv, "$", 1);
190 sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
191 s = indirect_find(sv, PL_parser->oldbufptr);
196 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
197 indirect_map_store(o, s, sv);
202 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
205 /* ... ck_padany ........................................................... */
207 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
209 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
210 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
212 if (indirect_hint()) {
214 const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
216 while (s < t && isSPACE(*s)) ++s;
217 while (t > s && isSPACE(*t)) --t;
218 sv = sv_2mortal(newSVpvn(s, t - s + 1));
220 indirect_map_store(o, s, sv);
226 /* ... ck_method ........................................................... */
228 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
230 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
231 if (indirect_hint()) {
232 OP *op = cUNOPo->op_first;
234 const char *s = indirect_map_fetch(op, &sv);
237 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
239 sv = sv_mortalcopy(sv);
240 s = indirect_find(sv, PL_parser->oldbufptr);
242 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
243 /* o may now be a method_named */
244 indirect_map_store(o, s, sv);
249 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
252 /* ... ck_entersub ......................................................... */
254 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
256 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
258 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
261 IV hint = indirect_hint();
263 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
271 if (!op->op_flags & OPf_KIDS)
274 } while (oo->op_type != OP_PUSHMARK);
277 if (om->op_type == OP_METHOD)
278 om = cUNOPx(om)->op_first;
279 else if (om->op_type != OP_METHOD_NAMED)
281 pm = indirect_map_fetch(om, &svm);
282 po = indirect_map_fetch(oo, &svo);
283 if (pm && po && pm < po) {
284 const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
286 croak(indirect_msg, psvm, psvo);
288 warn(indirect_msg, psvm, psvo);
291 indirect_map_clean(o);
297 STATIC U32 indirect_initialized = 0;
299 /* --- XS ------------------------------------------------------------------ */
301 MODULE = indirect PACKAGE = indirect
307 if (!indirect_initialized++) {
308 PERL_HASH(indirect_hash, "indirect", 8);
309 indirect_map = newHV();
310 indirect_old_ck_const = PL_check[OP_CONST];
311 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
312 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
313 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
314 indirect_old_ck_padany = PL_check[OP_PADANY];
315 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
316 indirect_old_ck_method = PL_check[OP_METHOD];
317 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
318 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
319 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);