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 /* ... Hints ............................................................... */
23 STATIC U32 indirect_hash = 0;
25 STATIC IV indirect_hint(pTHX) {
26 #define indirect_hint() indirect_hint(aTHX)
27 SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
32 return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
35 /* ... op -> source position ............................................... */
37 STATIC HV *indirect_map = NULL;
38 STATIC const char *indirect_linestr = NULL;
40 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
42 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
43 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
45 const char *pl_linestr;
48 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
49 * In this case the linestr has temporarly changed, but the old buffer should
50 * still be alive somewhere. */
52 if (!PL_parser->lex_inwhat) {
53 pl_linestr = SvPVX_const(PL_parser->linestr);
54 if (indirect_linestr != pl_linestr) {
55 hv_clear(indirect_map);
56 indirect_linestr = pl_linestr;
61 SvUPGRADE(val, SVt_PVIV);
62 SvUVX(val) = PTR2UV(src);
65 if (!hv_store(indirect_map, buf, OP2STR(o), val, 0))
69 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
70 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
74 if (indirect_linestr != SvPVX_const(PL_parser->linestr))
77 val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
84 return INT2PTR(const char *, SvUVX(*val));
87 STATIC void indirect_map_delete(pTHX_ const OP *o) {
88 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
91 hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
94 STATIC void indirect_map_clean(pTHX_ const OP *o) {
95 #define indirect_map_clean(O) indirect_map_clean(aTHX_ (O))
96 if (o->op_flags & OPf_KIDS) {
97 const OP *kid = cUNOPo->op_first;
98 for (; kid; kid = kid->op_sibling) {
99 indirect_map_delete(kid);
100 indirect_map_clean(kid);
103 indirect_map_delete(o);
107 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
108 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
110 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;
149 const char *name = NULL;
151 switch (op->op_type) {
154 GV *gv = cGVOPx_gv(op);
160 SV *sv = cSVOPx_sv(op);
161 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
162 name = SvPV_const(sv, len);
167 SV *sv = sv_2mortal(newSVpvn("$", 1));
168 sv_catpvn(sv, name, len);
169 o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
170 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
175 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
178 /* ... ck_padany ........................................................... */
180 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
182 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
183 o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
185 if (indirect_hint()) {
187 const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
189 while (s < t && isSPACE(*s)) ++s;
190 while (t > s && isSPACE(*t)) --t;
191 sv = sv_2mortal(newSVpvn(s, t - s + 1));
193 indirect_map_store(o, s, sv);
199 /* ... ck_method ........................................................... */
201 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
203 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
204 if (indirect_hint()) {
205 OP *op = cUNOPo->op_first;
207 const char *s = indirect_map_fetch(op, &sv);
210 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
212 sv = sv_mortalcopy(sv);
213 s = indirect_find(sv, PL_parser->oldbufptr);
215 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
216 /* o may now be a method_named */
217 indirect_map_store(o, s, sv);
222 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
225 /* ... ck_entersub ......................................................... */
227 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
229 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
231 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
234 IV hint = indirect_hint();
236 o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
244 if (!op->op_flags & OPf_KIDS)
247 } while (oo->op_type != OP_PUSHMARK);
250 if (om->op_type == OP_METHOD)
251 om = cUNOPx(om)->op_first;
252 else if (om->op_type != OP_METHOD_NAMED)
254 pm = indirect_map_fetch(om, &svm);
255 po = indirect_map_fetch(oo, &svo);
256 if (pm && po && pm < po) {
257 const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
259 croak(indirect_msg, psvm, psvo);
261 warn(indirect_msg, psvm, psvo);
264 indirect_map_clean(o);
270 STATIC U32 indirect_initialized = 0;
272 /* --- XS ------------------------------------------------------------------ */
274 MODULE = indirect PACKAGE = indirect
280 if (!indirect_initialized++) {
281 PERL_HASH(indirect_hash, "indirect", 8);
282 indirect_map = newHV();
283 indirect_old_ck_const = PL_check[OP_CONST];
284 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
285 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
286 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
287 indirect_old_ck_padany = PL_check[OP_PADANY];
288 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
289 indirect_old_ck_method = PL_check[OP_METHOD];
290 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
291 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
292 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);