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 SvPVX_const SvPVX
13 /* ... Hints ............................................................... */
15 STATIC U32 indirect_hash = 0;
17 STATIC UV indirect_hint(pTHX) {
18 #define indirect_hint() indirect_hint(aTHX)
19 SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
24 return SvOK(id) ? SvUV(id) : 0;
27 /* ... op -> source position ............................................... */
29 STATIC HV *indirect_map = NULL;
30 STATIC const char *indirect_linestr = NULL;
32 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
33 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
35 const char *pl_linestr;
38 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
39 * In this case the linestr has temporarly changed, but the old buffer should
40 * still be alive somewhere. */
42 if (!PL_parser->lex_inwhat) {
43 pl_linestr = SvPVX_const(PL_parser->linestr);
44 if (indirect_linestr != pl_linestr) {
45 hv_clear(indirect_map);
46 indirect_linestr = pl_linestr;
51 SvUPGRADE(val, SVt_PVIV);
52 SvUVX(val) = PTR2UV(src);
53 if (!hv_store(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), val, 0))
57 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV ** const name) {
58 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
62 if (indirect_linestr != SvPVX(PL_parser->linestr))
65 val = hv_fetch(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), 0);
72 return INT2PTR(const char *, SvUVX(*val));
75 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
76 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
78 const char *p = NULL, *r = SvPV_const(sv, len);
94 /* ... ck_const ............................................................ */
96 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
98 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
99 if (indirect_hint()) {
101 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
102 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
105 return CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
108 /* ... ck_rv2sv ............................................................ */
110 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
112 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
113 if (indirect_hint()) {
114 OP *op = cUNOPo->op_first;
115 SV *name = cSVOPx_sv(op);
116 if (SvPOK(name) && (SvTYPE(name) >= SVt_PV)) {
117 SV *sv = sv_2mortal(newSVpvn("$", 1));
119 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
123 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
126 /* ... ck_padany ........................................................... */
128 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
130 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
131 if (indirect_hint()) {
133 const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
135 while (s < t && isSPACE(*s)) ++s;
136 while (t > s && isSPACE(*t)) --t;
137 sv = sv_2mortal(newSVpvn(s, t - s + 1));
139 indirect_map_store(o, s, sv);
142 return CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
145 /* ... ck_method ........................................................... */
147 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
149 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
150 if (indirect_hint()) {
151 OP *op = cUNOPo->op_first;
153 const char *s = indirect_map_fetch(op, &sv);
156 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
158 sv = sv_mortalcopy(sv);
159 s = indirect_find(sv, PL_parser->oldbufptr);
161 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
162 /* o may now be a method_named */
163 indirect_map_store(o, s, sv);
168 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
171 /* ... ck_entersub ......................................................... */
173 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
175 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
177 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
180 UV hint = indirect_hint();
186 while (op->op_type != OP_PUSHMARK)
187 op = (LISTOP *) op->op_first;
190 while (om->op_sibling)
192 if (om->op_type == OP_METHOD)
193 om = cUNOPx(om)->op_first;
194 else if (om->op_type != OP_METHOD_NAMED)
196 pm = indirect_map_fetch(om, &svm);
197 po = indirect_map_fetch(oo, &svo);
198 if (pm && po && pm < po)
199 ((hint == 2) ? croak : warn)(indirect_msg, SvPV_nolen(svm), SvPV_nolen(svo));
203 return CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
206 STATIC U32 indirect_initialized = 0;
208 /* --- XS ------------------------------------------------------------------ */
210 MODULE = indirect PACKAGE = indirect
216 if (!indirect_initialized++) {
217 PERL_HASH(indirect_hash, "indirect", 8);
218 indirect_map = newHV();
219 indirect_old_ck_const = PL_check[OP_CONST];
220 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
221 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
222 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
223 indirect_old_ck_padany = PL_check[OP_PADANY];
224 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
225 indirect_old_ck_method = PL_check[OP_METHOD];
226 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
227 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
228 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);