]> git.vpit.fr Git - perl/modules/indirect.git/blob - indirect.xs
Tidy up the new code in ck_rv2sv
[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 #ifndef sv_catpvn_nomg
22 # define sv_catpvn_nomg sv_catpvn
23 #endif
24
25 /* ... Hints ............................................................... */
26
27 STATIC U32 indirect_hash = 0;
28
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,
32                                          NULL,
33                                          "indirect", 8,
34                                          0,
35                                          indirect_hash);
36  return (id && SvOK(id) && SvIOK(id)) ? SvIV(id) : 0;
37 }
38
39 /* ... op -> source position ............................................... */
40
41 STATIC HV *indirect_map = NULL;
42 STATIC const char *indirect_linestr = NULL;
43
44 #define OP2STR(O) (sprintf(buf, "%u", PTR2UV(o)))
45
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))
48  char buf[32];
49  const char *pl_linestr;
50  SV *val;
51
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. */
55
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;
61   }
62  }
63
64  val = newSVsv(sv);
65  SvUPGRADE(val, SVt_PVIV);
66  SvUVX(val) = PTR2UV(src);
67  SvIOK_on(val);
68  SvIsUV_on(val);
69  if (!hv_store(indirect_map, buf, OP2STR(o), val, 0)) SvREFCNT_dec(val);
70 }
71
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))
74  char buf[32];
75  SV **val;
76
77  if (indirect_linestr != SvPVX_const(PL_parser->linestr))
78   return NULL;
79
80  val = hv_fetch(indirect_map, buf, OP2STR(o), 0);
81  if (!val) {
82   *name = NULL;
83   return NULL;
84  }
85
86  *name = *val;
87  return INT2PTR(const char *, SvUVX(*val));
88 }
89
90 STATIC void indirect_map_delete(pTHX_ const OP *o) {
91 #define indirect_map_delete(O) indirect_map_delete(aTHX_ (O))
92  char buf[32];
93
94  hv_delete(indirect_map, buf, OP2STR(o), G_DISCARD);
95 }
96
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);
104   }
105  } else {
106   indirect_map_delete(o);
107  }
108 }
109
110 STATIC const char *indirect_find(pTHX_ SV *sv, const char *s) {
111 #define indirect_find(N, S) indirect_find(aTHX_ (N), (S))
112  STRLEN len;
113  const char *p = NULL, *r = SvPV_const(sv, len);
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 *sv;
150   const char *name = NULL, *s;
151   STRLEN len;
152
153   switch (op->op_type) {
154    case OP_GV:
155    case OP_GVSV: {
156     GV *gv = cGVOPx_gv(op);
157     name = GvNAME(gv);
158     len  = GvNAMELEN(gv);
159     break;
160    }
161    default: {
162     SV *sv = cSVOPx_sv(op);
163     if (SvPOK(sv) && (SvTYPE(sv) >= SVt_PV))
164      name = SvPV_const(sv, len);
165     break;
166    }
167   }
168   if (!name)
169    goto done;
170
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);
177
178    if ((len < stashlen + 2) || name != strstr(name, stash)) {
179     /* Failed again ? Try to remove main */
180     stash = "main";
181     stashlen = 4;
182     if ((len < stashlen + 2) || name != strstr(name, stash))
183      goto done;
184    }
185    if (name[stashlen] != ':' || name[stashlen+1] != ':')
186     goto done;
187
188    sv_setpvn(sv, "$", 1);
189    stashlen += 2;
190    sv_catpvn_nomg(sv, name + stashlen, len - stashlen);
191    s = indirect_find(sv, PL_parser->oldbufptr);
192    if (!s)
193     goto done;
194   }
195
196   o = CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
197   indirect_map_store(o, s, sv);
198   return o;
199  }
200
201 done:
202  return CALL_FPTR(indirect_old_ck_rv2sv)(aTHX_ o);
203 }
204
205 /* ... ck_padany ........................................................... */
206
207 STATIC OP *(*indirect_old_ck_padany)(pTHX_ OP *) = 0;
208
209 STATIC OP *indirect_ck_padany(pTHX_ OP *o) {
210  o = CALL_FPTR(indirect_old_ck_padany)(aTHX_ o);
211
212  if (indirect_hint()) {
213   SV *sv;
214   const char *s = PL_parser->oldbufptr, *t = PL_parser->bufptr - 1;
215
216   while (s < t && isSPACE(*s)) ++s;
217   while (t > s && isSPACE(*t)) --t;
218   sv = sv_2mortal(newSVpvn(s, t - s + 1));
219
220   indirect_map_store(o, s, sv);
221  }
222
223  return o;
224 }
225
226 /* ... ck_method ........................................................... */
227
228 STATIC OP *(*indirect_old_ck_method)(pTHX_ OP *) = 0;
229
230 STATIC OP *indirect_ck_method(pTHX_ OP *o) {
231  if (indirect_hint()) {
232   OP *op = cUNOPo->op_first;
233   SV *sv;
234   const char *s = indirect_map_fetch(op, &sv);
235   if (!s) {
236    sv = cSVOPx_sv(op);
237    if (!SvPOK(sv) || (SvTYPE(sv) < SVt_PV))
238     goto done;
239    sv = sv_mortalcopy(sv);
240    s  = indirect_find(sv, PL_parser->oldbufptr);
241   }
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);
245   return o;
246  }
247
248 done:
249  return CALL_FPTR(indirect_old_ck_method)(aTHX_ o);
250 }
251
252 /* ... ck_entersub ......................................................... */
253
254 STATIC const char indirect_msg[] = "Indirect call of method \"%s\" on object \"%s\"";
255
256 STATIC OP *(*indirect_old_ck_entersub)(pTHX_ OP *) = 0;
257
258 STATIC OP *indirect_ck_entersub(pTHX_ OP *o) {
259  LISTOP *op;
260  OP *om, *oo;
261  IV hint = indirect_hint();
262
263  o = CALL_FPTR(indirect_old_ck_entersub)(aTHX_ o);
264
265  if (hint) {
266   const char *pm, *po;
267   SV *svm, *svo;
268   oo = o;
269   do {
270    op = (LISTOP *) oo;
271    if (!op->op_flags & OPf_KIDS)
272     goto done;
273    oo = op->op_first;
274   } while (oo->op_type != OP_PUSHMARK);
275   oo = oo->op_sibling;
276   om = op->op_last;
277   if (om->op_type == OP_METHOD)
278    om = cUNOPx(om)->op_first;
279   else if (om->op_type != OP_METHOD_NAMED)
280    goto done;
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);
285    if (hint == 2)
286     croak(indirect_msg, psvm, psvo);
287    else
288     warn(indirect_msg, psvm, psvo);
289   }
290 done:
291   indirect_map_clean(o);
292  }
293
294  return o;
295 }
296
297 STATIC U32 indirect_initialized = 0;
298
299 /* --- XS ------------------------------------------------------------------ */
300
301 MODULE = indirect      PACKAGE = indirect
302
303 PROTOTYPES: DISABLE
304
305 BOOT:
306 {
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);
320  }
321 }