1 #define PERL_NO_GET_CONTEXT
7 # define SvPVX_const SvPVX
10 STATIC U32 indirect_initialized = 0;
11 STATIC U32 indirect_hash = 0;
13 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
15 STATIC HV *indirect_map = NULL;
16 STATIC const char *indirect_linestr = NULL;
18 STATIC UV indirect_hint(pTHX) {
19 #define indirect_hint() indirect_hint(aTHX)
20 SV *id = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
25 return SvOK(id) ? SvUV(id) : 0;
28 STATIC void indirect_map_store(pTHX_ const OP *o, const char *src, SV *sv) {
29 #define indirect_map_store(O, S, N) indirect_map_store(aTHX_ (O), (S), (N))
31 const char *pl_linestr;
34 /* When lex_inwhat is set, we're in a quotelike environment (qq, qr, but not q)
35 * In this case the linestr has temporarly changed, but the old buffer should
36 * still be alive somewhere. */
38 if (!PL_parser->lex_inwhat) {
39 pl_linestr = SvPVX_const(PL_parser->linestr);
40 if (indirect_linestr != pl_linestr) {
41 hv_clear(indirect_map);
42 indirect_linestr = pl_linestr;
47 SvUPGRADE(val, SVt_PVIV);
48 SvUVX(val) = PTR2UV(src);
49 if (!hv_store(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), val, 0))
53 STATIC const char *indirect_map_fetch(pTHX_ const OP *o, SV **name) {
54 #define indirect_map_fetch(O, S) indirect_map_fetch(aTHX_ (O), (S))
58 if (indirect_linestr != SvPVX(PL_parser->linestr))
61 val = hv_fetch(indirect_map, buf, sprintf(buf, "%u", PTR2UV(o)), 0);
68 return INT2PTR(const char *, SvUVX(*val));
71 STATIC UV indirect_intuit(const char *meth, const char *obj) {
73 int indirect = 0, quotelike = 0;
75 for (s = meth; s < obj; ++s) {
107 else if (quotelike == 2 && !isALNUM(*s))
117 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
118 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
120 const char *p = NULL, *r = SvPV_const(sv, len);
130 p = strstr(p + 1, r);
136 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
138 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
139 if (indirect_hint()) {
141 if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
142 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
145 return CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
148 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
150 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
151 if (indirect_hint()) {
152 OP *op = cUNOPo->op_first;
153 SV *name = cSVOPx_sv(op);
154 if (SvPOK(name) && (SvTYPE(name) >= SVt_PV)) {
155 SV *sv = sv_2mortal(newSVpvn("$", 1));
157 indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
161 return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
164 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
166 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
167 if (indirect_hint()) {
169 const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
171 while (s < t && isSPACE(*s)) ++s;
172 while (t > s && isSPACE(*t)) --t;
173 sv = sv_2mortal(newSVpvn(s, t - s + 1));
175 indirect_map_store(o, s, sv);
178 return CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
181 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
183 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
184 if (indirect_hint()) {
185 OP *op = cUNOPo->op_first;
187 const char *s = indirect_map_fetch(op, &sv);
190 if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
192 sv = sv_mortalcopy(sv);
193 s = indirect_find(sv, PL_parser->oldbufptr);
195 o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
196 /* o may now be a method_named */
197 indirect_map_store(o, s, sv);
202 return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
205 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
207 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
210 UV hint = indirect_hint();
216 while (op->op_type != OP_PUSHMARK)
217 op = (LISTOP *) op->op_first;
220 while (om->op_sibling)
222 if (om->op_type == OP_METHOD)
223 om = cUNOPx(om)->op_first;
224 pm = indirect_map_fetch(om, &svm);
225 po = indirect_map_fetch(oo, &svo);
226 if (pm && po && pm < po && indirect_intuit(pm, po))
227 ((hint == 2) ? croak : warn)(indirect_msg, SvPV_nolen(svm), SvPV_nolen(svo));
230 return CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
233 MODULE = indirect PACKAGE = indirect
239 if (!indirect_initialized++) {
240 PERL_HASH(indirect_hash, "indirect", 8);
241 indirect_map = newHV();
242 indirect_old_ck_const = PL_check[OP_CONST];
243 PL_check[OP_CONST] = MEMBER_TO_FPTR(indirect_ck_const);
244 indirect_old_ck_rv2sv = PL_check[OP_RV2SV];
245 PL_check[OP_RV2SV] = MEMBER_TO_FPTR(indirect_ck_rv2sv);
246 indirect_old_ck_padany = PL_check[OP_PADANY];
247 PL_check[OP_PADANY] = MEMBER_TO_FPTR(indirect_ck_padany);
248 indirect_old_ck_method = PL_check[OP_METHOD];
249 PL_check[OP_METHOD] = MEMBER_TO_FPTR(indirect_ck_method);
250 indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
251 PL_check[OP_ENTERSUB] = MEMBER_TO_FPTR(indirect_ck_entersub);