]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Test 'package A; sub foo; foo A->new' that gets deparsed as 'A->foo->new'
[perl/modules/indirect.git] / indirect.xs
1 /* This file is part of the indirect Perl module.
2  * See http://search.cpan.org/dist/indirect/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #ifndef SvPV_const
10 # define SvPV_const SvPV
11 #endif
12
13 #ifndef SvPV_nolen_const
14 # define SvPV_nolen_const SvPV_nolen
15 #endif
16
17 #ifndef SvPVX_const
18 # define SvPVX_const SvPVX
19 #endif
20
21 /* ... Hints ............................................................... */
22
23 STATIC U32 indirect_hash = 0;
24
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,
28                                          NULL,
29                                          "indirect", 8,
30                                          0,
31                                          indirect_hash);
32  return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
33 }
34
35 /* ... op -> source position ............................................... */
36
37 STATIC HV *indirect_map = NULL;
38 STATIC const char *indirect_linestr = NULL;
39
40 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
41
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))
44  char buf[32];
45  const char *pl_linestr;
46  SV *val;
47
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. */
51
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;
57   }
58  }
59
60  val = newSVsv(sv);
61  SvUPGRADE(val, SVt_PVIV);
62  SvUVX(val) = PTR2UV(src);
63  SvIOK_on(val);
64  SvIsUV_on(val);
65  if (!hv_store(indirect_map, buf, OP2STR(o), val, 0))
66   SvREFCNT_dec(val);
67 }
68
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))
71  char buf[32];
72  SV **val;
73
74  if (indirect_linestr != SvPVX_const(PL_parser->linestr))
75   return NULL;
76
77  val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
78  if (!val) {
79   *name = NULL;
80   return NULL;
81  }
82
83  *name = *val;
84  return INT2PTR(const char *, SvUVX(*val));
85 }
86
87 STATIC void indirect_map_delete(pTHX_ const OP *o) {
88 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
89  char buf[32];
90
91  hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
92 }
93
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);
101   }
102  } else {
103   indirect_map_delete(o);
104  }
105 }
106
107 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
108 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
109  STRLEN len;
110  const char *p = NULL, *r = SvPV_const(sv, len);
111
112  if (!len)
113   return s;
114
115  p = strstr(s, r);
116  while (p) {
117   p += len;
118   if (!isALNUM(*p))
119    break;
120   p = strstr(p + 1, r);
121  }
122
123  return p;
124 }
125
126 /* ... ck_const ............................................................ */
127
128 STATIC OP *(*indirect_old_ck_const)(pTHX_ OP *) = 0;
129
130 STATIC OP *indirect_ck_const(pTHX_ OP *o) {
131  o = CALL_FPTR(indirect_old_ck_const)(aTHX_ o);
132
133  if (indirect_hint()) {
134   SV *sv = cSVOPo_sv;
135   if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
136    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
137  }
138
139  return o;
140 }
141
142 /* ... ck_rv2sv ............................................................ */
143
144 STATIC OP *(*indirect_old_ck_rv2sv)(pTHX_ OP *) = 0;
145
146 STATIC OP *indirect_ck_rv2sv(pTHX_ OP *o) {
147  if (indirect_hint()) {
148   OP *op = cUNOPo->op_first;
149   SV *name = cSVOPx_sv(op);
150   if (SvPOK(name) && (SvTYPE(name) >= SVt_PV)) {
151    SV *sv = sv_2mortal(newSVpvn("$", 1));
152    sv_catsv(sv, name);
153    o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
154    indirect_map_store(o, indirect_find(sv, PL_parser->oldbufptr), sv);
155    return o;
156   }
157  }
158
159  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
160 }
161
162 /* ... ck_padany ........................................................... */
163
164 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
165
166 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
167  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
168
169  if (indirect_hint()) {
170   SV *sv;
171   const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
172
173   while (s < t && isSPACE(*s)) ++s;
174   while (t > s && isSPACE(*t)) --t;
175   sv = sv_2mortal(newSVpvn(s, t - s + 1));
176
177   indirect_map_store(o, s, sv);
178  }
179
180  return o;
181 }
182
183 /* ... ck_method ........................................................... */
184
185 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
186
187 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
188  if (indirect_hint()) {
189   OP *op = cUNOPo->op_first;
190   SV *sv;
191   const char *s = indirect_map_fetch(op, &sv);
192   if (!s) {
193    sv = cSVOPx_sv(op);
194    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
195     goto done;
196    sv = sv_mortalcopy(sv);
197    s  = indirect_find(sv, PL_parser->oldbufptr);
198   }
199   o = CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
200   /* o may now be a method_named */
201   indirect_map_store(o, s, sv);
202   return o;
203  }
204
205 done:
206  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
207 }
208
209 /* ... ck_entersub ......................................................... */
210
211 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
212
213 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
214
215 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
216  LISTOP *op;
217  OP *om, *oo;
218  IV hint = indirect_hint();
219
220  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
221
222  if (hint) {
223   const char *pm, *po;
224   SV *svm, *svo;
225   oo = o;
226   do {
227    op = (LISTOP *) oo;
228    if (!op->op_flags & OPf_KIDS)
229     goto done;
230    oo = op->op_first;
231   } while (oo->op_type != OP_PUSHMARK);
232   oo = oo->op_sibling;
233   om = op->op_last;
234   if (om->op_type == OP_METHOD)
235    om = cUNOPx(om)->op_first;
236   else if (om->op_type != OP_METHOD_NAMED)
237    goto done;
238   pm = indirect_map_fetch(om, &svm);
239   po = indirect_map_fetch(oo, &svo);
240   if (pm && po && pm < po) {
241    const char *psvm = SvPV_nolen_const(svm), *psvo = SvPV_nolen_const(svo);
242    if (hint == 2)
243     croak(indirect_msg, psvm, psvo);
244    else
245     warn(indirect_msg, psvm, psvo);
246   }
247 done:
248   indirect_map_clean(o);
249  }
250
251  return o;
252 }
253
254 STATIC U32 indirect_initialized = 0;
255
256 /* --- XS ------------------------------------------------------------------ */
257
258 MODULE = indirect      PACKAGE = indirect
259
260 PROTOTYPES: DISABLE
261
262 BOOT:
263 {
264  if (!indirect_initialized++) {
265   PERL_HASH(indirect_hash, "indirect", 8);
266   indirect_map = newHV();
267   indirect_old_ck_const    = PL_check[OP_CONST];
268   PL_check[OP_CONST]       = MEMBER_TO_FPTR(indirect_ck_const);
269   indirect_old_ck_rv2sv    = PL_check[OP_RV2SV];
270   PL_check[OP_RV2SV]       = MEMBER_TO_FPTR(indirect_ck_rv2sv);
271   indirect_old_ck_padany   = PL_check[OP_PADANY];
272   PL_check[OP_PADANY]      = MEMBER_TO_FPTR(indirect_ck_padany);
273   indirect_old_ck_method   = PL_check[OP_METHOD];
274   PL_check[OP_METHOD]      = MEMBER_TO_FPTR(indirect_ck_method);
275   indirect_old_ck_entersub = PL_check[OP_ENTERSUB];
276   PL_check[OP_ENTERSUB]    = MEMBER_TO_FPTR(indirect_ck_entersub);
277  }
278 }