]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
Add support for PERL_OP_PARENT
[perl/modules/Lexical-Types.git] / Types.xs
1 /* This file is part of the Lexical-Types Perl module.
2  * See http://search.cpan.org/dist/Lexical-Types/ */
3
4 #define PERL_NO_GET_CONTEXT
5 #include "EXTERN.h"
6 #include "perl.h"
7 #include "XSUB.h"
8
9 #define __PACKAGE__     "Lexical::Types"
10 #define __PACKAGE_LEN__ (sizeof(__PACKAGE__)-1)
11
12 /* --- Compatibility wrappers ---------------------------------------------- */
13
14 #define LT_HAS_PERL(R, V, S) (PERL_REVISION > (R) || (PERL_REVISION == (R) && (PERL_VERSION > (V) || (PERL_VERSION == (V) && (PERL_SUBVERSION >= (S))))))
15
16 #if LT_HAS_PERL(5, 10, 0) || defined(PL_parser)
17 # ifndef PL_in_my_stash
18 #  define PL_in_my_stash PL_parser->in_my_stash
19 # endif
20 #else
21 # ifndef PL_in_my_stash
22 #  define PL_in_my_stash PL_Iin_my_stash
23 # endif
24 #endif
25
26 #ifndef LT_WORKAROUND_REQUIRE_PROPAGATION
27 # define LT_WORKAROUND_REQUIRE_PROPAGATION !LT_HAS_PERL(5, 10, 1)
28 #endif
29
30 #ifndef LT_HAS_RPEEP
31 # define LT_HAS_RPEEP LT_HAS_PERL(5, 13, 5)
32 #endif
33
34 #ifndef HvNAME_get
35 # define HvNAME_get(H) HvNAME(H)
36 #endif
37
38 #ifndef HvNAMELEN_get
39 # define HvNAMELEN_get(H) strlen(HvNAME_get(H))
40 #endif
41
42 #ifndef OP_SIBLING
43 # define OP_SIBLING(O) ((O)->op_sibling)
44 #endif
45
46 #ifndef SvREFCNT_inc_simple_void_NN
47 # define SvREFCNT_inc_simple_void_NN(S) ((void) SvREFCNT_inc(S))
48 #endif
49
50 /* ... Thread safety and multiplicity ...................................... */
51
52 /* Safe unless stated otherwise in Makefile.PL */
53 #ifndef LT_FORKSAFE
54 # define LT_FORKSAFE 1
55 #endif
56
57 #ifndef LT_MULTIPLICITY
58 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
59 #  define LT_MULTIPLICITY 1
60 # else
61 #  define LT_MULTIPLICITY 0
62 # endif
63 #endif
64
65 #ifndef tTHX
66 # define tTHX PerlInterpreter*
67 #endif
68
69 #if LT_MULTIPLICITY && defined(USE_ITHREADS) && defined(dMY_CXT) && defined(MY_CXT) && defined(START_MY_CXT) && defined(MY_CXT_INIT) && (defined(MY_CXT_CLONE) || defined(dMY_CXT_SV))
70 # define LT_THREADSAFE 1
71 # ifndef MY_CXT_CLONE
72 #  define MY_CXT_CLONE \
73     dMY_CXT_SV;                                                      \
74     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
75     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
76     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
77 # endif
78 #else
79 # define LT_THREADSAFE 0
80 # undef  dMY_CXT
81 # define dMY_CXT      dNOOP
82 # undef  MY_CXT
83 # define MY_CXT       lt_globaldata
84 # undef  START_MY_CXT
85 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
86 # undef  MY_CXT_INIT
87 # define MY_CXT_INIT  NOOP
88 # undef  MY_CXT_CLONE
89 # define MY_CXT_CLONE NOOP
90 # undef  pMY_CXT
91 # define pMY_CXT
92 # undef  pMY_CXT_
93 # define pMY_CXT_
94 # undef  aMY_CXT
95 # define aMY_CXT
96 # undef  aMY_CXT_
97 # define aMY_CXT_
98 #endif
99
100 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
101 # define LT_CHECK_MUTEX_LOCK   OP_CHECK_MUTEX_LOCK
102 # define LT_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
103 #else
104 # define LT_CHECK_MUTEX_LOCK   OP_REFCNT_LOCK
105 # define LT_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
106 #endif
107
108 typedef OP *(*lt_ck_t)(pTHX_ OP *);
109
110 #ifdef wrap_op_checker
111
112 # define lt_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
113
114 #else
115
116 STATIC void lt_ck_replace(pTHX_ OPCODE type, lt_ck_t new_ck, lt_ck_t *old_ck_p){
117 #define lt_ck_replace(T, NC, OCP) lt_ck_replace(aTHX_ (T), (NC), (OCP))
118  LT_CHECK_MUTEX_LOCK;
119  if (!*old_ck_p) {
120   *old_ck_p      = PL_check[type];
121   PL_check[type] = new_ck;
122  }
123  LT_CHECK_MUTEX_UNLOCK;
124 }
125
126 #endif
127
128 STATIC void lt_ck_restore(pTHX_ OPCODE type, lt_ck_t *old_ck_p) {
129 #define lt_ck_restore(T, OCP) lt_ck_restore(aTHX_ (T), (OCP))
130  LT_CHECK_MUTEX_LOCK;
131  if (*old_ck_p) {
132   PL_check[type] = *old_ck_p;
133   *old_ck_p      = 0;
134  }
135  LT_CHECK_MUTEX_UNLOCK;
136 }
137
138 /* --- Helpers ------------------------------------------------------------- */
139
140 /* ... Thread-safe hints ................................................... */
141
142 #if LT_WORKAROUND_REQUIRE_PROPAGATION
143
144 typedef struct {
145  SV *code;
146  IV  require_tag;
147 } lt_hint_t;
148
149 #define LT_HINT_STRUCT 1
150
151 #define LT_HINT_CODE(H) ((H)->code)
152
153 #define LT_HINT_FREE(H) { \
154  lt_hint_t *h = (H);      \
155  SvREFCNT_dec(h->code);   \
156  PerlMemShared_free(h);   \
157 }
158
159 #else  /*  LT_WORKAROUND_REQUIRE_PROPAGATION */
160
161 typedef SV lt_hint_t;
162
163 #define LT_HINT_STRUCT 0
164
165 #define LT_HINT_CODE(H) (H)
166
167 #define LT_HINT_FREE(H) SvREFCNT_dec(H);
168
169 #endif /* !LT_WORKAROUND_REQUIRE_PROPAGATION */
170
171 #if LT_THREADSAFE
172
173 #define PTABLE_NAME        ptable_hints
174 #define PTABLE_VAL_FREE(V) LT_HINT_FREE(V)
175
176 #define pPTBL  pTHX
177 #define pPTBL_ pTHX_
178 #define aPTBL  aTHX
179 #define aPTBL_ aTHX_
180
181 #include "ptable.h"
182
183 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
184 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
185
186 #endif /* LT_THREADSAFE */
187
188 /* ... "Seen" pointer table ................................................ */
189
190 #define PTABLE_NAME        ptable_seen
191 #define PTABLE_VAL_FREE(V) NOOP
192
193 #include "ptable.h"
194
195 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
196 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
197 #define ptable_seen_clear(T)       ptable_seen_clear(aPTBLMS_ (T))
198 #define ptable_seen_free(T)        ptable_seen_free(aPTBLMS_ (T))
199
200 /* ... Global data ......................................................... */
201
202 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
203
204 typedef struct {
205 #if LT_THREADSAFE
206  ptable *tbl; /* It really is a ptable_hints */
207  tTHX    owner;
208 #endif
209  ptable *seen; /* It really is a ptable_seen */
210  SV     *default_meth;
211 } my_cxt_t;
212
213 START_MY_CXT
214
215 /* ... Cloning global data ................................................. */
216
217 #if LT_THREADSAFE
218
219 typedef struct {
220  ptable *tbl;
221 #if LT_HAS_PERL(5, 13, 2)
222  CLONE_PARAMS *params;
223 #else
224  CLONE_PARAMS params;
225 #endif
226 } lt_ptable_clone_ud;
227
228 #if LT_HAS_PERL(5, 13, 2)
229 # define lt_ptable_clone_ud_init(U, T, O) \
230    (U).tbl    = (T); \
231    (U).params = Perl_clone_params_new((O), aTHX)
232 # define lt_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
233 # define lt_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), (U)->params))
234 #else
235 # define lt_ptable_clone_ud_init(U, T, O) \
236    (U).tbl               = (T);     \
237    (U).params.stashes    = newAV(); \
238    (U).params.flags      = 0;       \
239    (U).params.proto_perl = (O)
240 # define lt_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
241 # define lt_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), &((U)->params)))
242 #endif
243
244 STATIC void lt_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
245  lt_ptable_clone_ud *ud = ud_;
246  lt_hint_t *h1 = ent->val;
247  lt_hint_t *h2;
248
249 #if LT_HINT_STRUCT
250
251  h2              = PerlMemShared_malloc(sizeof *h2);
252  h2->code        = lt_dup_inc(h1->code, ud);
253 #if LT_WORKAROUND_REQUIRE_PROPAGATION
254  h2->require_tag = PTR2IV(lt_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
255 #endif
256
257 #else /*   LT_HINT_STRUCT */
258
259  h2 = lt_dup_inc(h1, ud);
260
261 #endif /* !LT_HINT_STRUCT */
262
263  ptable_hints_store(ud->tbl, ent->key, h2);
264 }
265
266 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
267  dMY_CXT;
268
269  ptable_hints_free(MY_CXT.tbl);
270  MY_CXT.tbl          = NULL;
271  ptable_seen_free(MY_CXT.seen);
272  MY_CXT.seen         = NULL;
273  SvREFCNT_dec(MY_CXT.default_meth);
274  MY_CXT.default_meth = NULL;
275 }
276
277 STATIC int lt_endav_free(pTHX_ SV *sv, MAGIC *mg) {
278  SAVEDESTRUCTOR_X(lt_thread_cleanup, NULL);
279
280  return 0;
281 }
282
283 STATIC MGVTBL lt_endav_vtbl = {
284  0,
285  0,
286  0,
287  0,
288  lt_endav_free
289 #if MGf_COPY
290  , 0
291 #endif
292 #if MGf_DUP
293  , 0
294 #endif
295 #if MGf_LOCAL
296  , 0
297 #endif
298 };
299
300 #endif /* LT_THREADSAFE */
301
302 /* ... Hint tags ........................................................... */
303
304 #if LT_WORKAROUND_REQUIRE_PROPAGATION
305
306 STATIC IV lt_require_tag(pTHX) {
307 #define lt_require_tag() lt_require_tag(aTHX)
308  const CV *cv, *outside;
309
310  cv = PL_compcv;
311
312  if (!cv) {
313   /* If for some reason the pragma is operational at run-time, try to discover
314    * the current cv in use. */
315   const PERL_SI *si;
316
317   for (si = PL_curstackinfo; si; si = si->si_prev) {
318    I32 cxix;
319
320    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
321     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
322
323     switch (CxTYPE(cx)) {
324      case CXt_SUB:
325      case CXt_FORMAT:
326       /* The propagation workaround is only needed up to 5.10.0 and at that
327        * time format and sub contexts were still identical. And even later the
328        * cv members offsets should have been kept the same. */
329       cv = cx->blk_sub.cv;
330       goto get_enclosing_cv;
331      case CXt_EVAL:
332       cv = cx->blk_eval.cv;
333       goto get_enclosing_cv;
334      default:
335       break;
336     }
337    }
338   }
339
340   cv = PL_main_cv;
341  }
342
343 get_enclosing_cv:
344  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
345   cv = outside;
346
347  return PTR2IV(cv);
348 }
349
350 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
351
352 STATIC SV *lt_tag(pTHX_ SV *value) {
353 #define lt_tag(V) lt_tag(aTHX_ (V))
354  lt_hint_t *h;
355  SV        *code = NULL;
356 #if LT_THREADSAFE
357  dMY_CXT;
358
359  if (!MY_CXT.tbl)
360   return newSViv(0);
361 #endif /* LT_THREADSAFE */
362
363  if (SvROK(value)) {
364   value = SvRV(value);
365   if (SvTYPE(value) >= SVt_PVCV) {
366    code = value;
367    SvREFCNT_inc_simple_void_NN(code);
368   }
369  }
370
371 #if LT_HINT_STRUCT
372  h = PerlMemShared_malloc(sizeof *h);
373  h->code        = code;
374 # if LT_WORKAROUND_REQUIRE_PROPAGATION
375  h->require_tag = lt_require_tag();
376 # endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
377 #else  /*  LT_HINT_STRUCT */
378  h = code;
379 #endif /* !LT_HINT_STRUCT */
380
381 #if LT_THREADSAFE
382  /* We only need for the key to be an unique tag for looking up the value later
383   * Allocated memory provides convenient unique identifiers, so that's why we
384   * use the hint as the key itself. */
385  ptable_hints_store(MY_CXT.tbl, h, h);
386 #endif /* LT_THREADSAFE */
387
388  return newSViv(PTR2IV(h));
389 }
390
391 STATIC SV *lt_detag(pTHX_ const SV *hint) {
392 #define lt_detag(H) lt_detag(aTHX_ (H))
393  lt_hint_t *h;
394 #if LT_THREADSAFE
395  dMY_CXT;
396
397  if (!MY_CXT.tbl)
398   return NULL;
399 #endif /* LT_THREADSAFE */
400
401  if (!(hint && SvIOK(hint)))
402   return NULL;
403
404  h = INT2PTR(lt_hint_t *, SvIVX(hint));
405 #if LT_THREADSAFE
406  h = ptable_fetch(MY_CXT.tbl, h);
407 #endif /* LT_THREADSAFE */
408 #if LT_WORKAROUND_REQUIRE_PROPAGATION
409  if (lt_require_tag() != h->require_tag)
410   return NULL;
411 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
412
413  return LT_HINT_CODE(h);
414 }
415
416 STATIC U32 lt_hash = 0;
417
418 STATIC SV *lt_hint(pTHX) {
419 #define lt_hint() lt_hint(aTHX)
420  SV *hint;
421 #ifdef cop_hints_fetch_pvn
422  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, lt_hash,0);
423 #elif LT_HAS_PERL(5, 9, 5)
424  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
425                                        NULL,
426                                        __PACKAGE__, __PACKAGE_LEN__,
427                                        0,
428                                        lt_hash);
429 #else
430  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
431  if (!val)
432   return 0;
433  hint = *val;
434 #endif
435  return lt_detag(hint);
436 }
437
438 /* ... op => info map ...................................................... */
439
440 #define PTABLE_NAME        ptable_map
441 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
442
443 #include "ptable.h"
444
445 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
446 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
447 #define ptable_map_delete(T, K)   ptable_map_delete(aPTBLMS_ (T), (K))
448
449 #ifdef USE_ITHREADS
450
451 STATIC perl_mutex lt_op_map_mutex;
452
453 #define LT_LOCK(M)   MUTEX_LOCK(M)
454 #define LT_UNLOCK(M) MUTEX_UNLOCK(M)
455
456 #else /* USE_ITHREADS */
457
458 #define LT_LOCK(M)
459 #define LT_UNLOCK(M)
460
461 #endif /* !USE_ITHREADS */
462
463 STATIC ptable *lt_op_padxv_map = NULL;
464
465 typedef struct {
466  OP *(*old_pp)(pTHX);
467 #ifdef MULTIPLICITY
468  STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
469  char *buf;
470 #else /* MULTIPLICITY */
471  SV *orig_pkg;
472  SV *type_pkg;
473  SV *type_meth;
474 #endif /* !MULTIPLICITY */
475 } lt_op_padxv_info;
476
477 STATIC void lt_op_padxv_info_call(pTHX_ const lt_op_padxv_info *oi, SV *sv) {
478 #define lt_op_padxv_info_call(O, S) lt_op_padxv_info_call(aTHX_ (O), (S))
479  SV *orig_pkg, *type_pkg, *type_meth;
480  int items;
481  dSP;
482
483  ENTER;
484  SAVETMPS;
485
486 #ifdef MULTIPLICITY
487  {
488   STRLEN op_len = oi->orig_pkg_len, tp_len = oi->type_pkg_len;
489   char *buf = oi->buf;
490   orig_pkg  = sv_2mortal(newSVpvn(buf, op_len));
491   SvREADONLY_on(orig_pkg);
492   buf      += op_len;
493   type_pkg  = sv_2mortal(newSVpvn(buf, tp_len));
494   SvREADONLY_on(type_pkg);
495   buf      += tp_len;
496   type_meth = sv_2mortal(newSVpvn(buf, oi->type_meth_len));
497   SvREADONLY_on(type_meth);
498  }
499 #else /* MULTIPLICITY */
500  orig_pkg  = oi->orig_pkg;
501  type_pkg  = oi->type_pkg;
502  type_meth = oi->type_meth;
503 #endif /* !MULTIPLICITY */
504
505  PUSHMARK(SP);
506  EXTEND(SP, 3);
507  PUSHs(type_pkg);
508  PUSHs(sv);
509  PUSHs(orig_pkg);
510  PUTBACK;
511
512  items = call_sv(type_meth, G_ARRAY | G_METHOD);
513
514  SPAGAIN;
515  switch (items) {
516   case 0:
517    break;
518   case 1:
519    sv_setsv(sv, POPs);
520    break;
521   default:
522    croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
523  }
524  PUTBACK;
525
526  FREETMPS;
527  LEAVE;
528
529  return;
530 }
531
532 STATIC void lt_padxv_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*old_pp)(pTHX)) {
533 #define lt_padxv_map_store(O, OP, TP, TM, PP) lt_padxv_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
534  lt_op_padxv_info *oi;
535
536  LT_LOCK(&lt_op_map_mutex);
537
538  if (!(oi = ptable_fetch(lt_op_padxv_map, o))) {
539   oi = PerlMemShared_malloc(sizeof *oi);
540   ptable_map_store(lt_op_padxv_map, o, oi);
541 #ifdef MULTIPLICITY
542   oi->buf      = NULL;
543   oi->buf_size = 0;
544 #else /* MULTIPLICITY */
545  } else {
546   SvREFCNT_dec(oi->orig_pkg);
547   SvREFCNT_dec(oi->type_pkg);
548   SvREFCNT_dec(oi->type_meth);
549 #endif /* !MULTIPLICITY */
550  }
551
552 #ifdef MULTIPLICITY
553  {
554   STRLEN op_len       = SvCUR(orig_pkg);
555   STRLEN tp_len       = SvCUR(type_pkg);
556   STRLEN tm_len       = SvCUR(type_meth);
557   STRLEN new_buf_size = op_len + tp_len + tm_len;
558   char *buf;
559   if (new_buf_size > oi->buf_size) {
560    PerlMemShared_free(oi->buf);
561    oi->buf      = PerlMemShared_malloc(new_buf_size);
562    oi->buf_size = new_buf_size;
563   }
564   buf  = oi->buf;
565   Copy(SvPVX(orig_pkg),  buf, op_len, char);
566   buf += op_len;
567   Copy(SvPVX(type_pkg),  buf, tp_len, char);
568   buf += tp_len;
569   Copy(SvPVX(type_meth), buf, tm_len, char);
570   oi->orig_pkg_len  = op_len;
571   oi->type_pkg_len  = tp_len;
572   oi->type_meth_len = tm_len;
573   SvREFCNT_dec(orig_pkg);
574   SvREFCNT_dec(type_pkg);
575   SvREFCNT_dec(type_meth);
576  }
577 #else /* MULTIPLICITY */
578  oi->orig_pkg  = orig_pkg;
579  oi->type_pkg  = type_pkg;
580  oi->type_meth = type_meth;
581 #endif /* !MULTIPLICITY */
582
583  oi->old_pp = old_pp;
584
585  LT_UNLOCK(&lt_op_map_mutex);
586 }
587
588 STATIC const lt_op_padxv_info *lt_padxv_map_fetch(const OP *o, lt_op_padxv_info *oi) {
589  const lt_op_padxv_info *val;
590
591  LT_LOCK(&lt_op_map_mutex);
592
593  val = ptable_fetch(lt_op_padxv_map, o);
594  if (val) {
595   *oi = *val;
596   val = oi;
597  }
598
599  LT_UNLOCK(&lt_op_map_mutex);
600
601  return val;
602 }
603
604 #if LT_HAS_PERL(5, 17, 6)
605
606 STATIC ptable *lt_op_padrange_map = NULL;
607
608 typedef struct {
609  OP *(*old_pp)(pTHX);
610  const OP *padxv_start;
611 } lt_op_padrange_info;
612
613 STATIC void lt_padrange_map_store(pTHX_ const OP *o, const OP *s, OP *(*old_pp)(pTHX)) {
614 #define lt_padrange_map_store(O, S, PP) lt_padrange_map_store(aTHX_ (O), (S), (PP))
615  lt_op_padrange_info *oi;
616
617  LT_LOCK(&lt_op_map_mutex);
618
619  if (!(oi = ptable_fetch(lt_op_padrange_map, o))) {
620   oi = PerlMemShared_malloc(sizeof *oi);
621   ptable_map_store(lt_op_padrange_map, o, oi);
622  }
623
624  oi->old_pp      = old_pp;
625  oi->padxv_start = s;
626
627  LT_UNLOCK(&lt_op_map_mutex);
628 }
629
630 STATIC const lt_op_padrange_info *lt_padrange_map_fetch(const OP *o, lt_op_padrange_info *oi) {
631  const lt_op_padrange_info *val;
632
633  LT_LOCK(&lt_op_map_mutex);
634
635  val = ptable_fetch(lt_op_padrange_map, o);
636  if (val) {
637   *oi = *val;
638   val = oi;
639  }
640
641  LT_UNLOCK(&lt_op_map_mutex);
642
643  return val;
644 }
645
646 #endif
647
648 STATIC void lt_map_delete(pTHX_ const OP *o) {
649 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
650  LT_LOCK(&lt_op_map_mutex);
651
652  ptable_map_delete(lt_op_padxv_map,    o);
653 #if LT_HAS_PERL(5, 17, 6)
654  ptable_map_delete(lt_op_padrange_map, o);
655 #endif
656
657  LT_UNLOCK(&lt_op_map_mutex);
658 }
659
660 /* --- Hooks --------------------------------------------------------------- */
661
662 /* ... Our pp_padsv ........................................................ */
663
664 STATIC OP *lt_pp_padsv(pTHX) {
665  lt_op_padxv_info oi;
666
667  if (lt_padxv_map_fetch(PL_op, &oi)) {
668   dTARGET;
669   lt_op_padxv_info_call(&oi, TARG);
670   return oi.old_pp(aTHX);
671  }
672
673  return PL_op->op_ppaddr(aTHX);
674 }
675
676 /* ... Our pp_padrange (on perl 5.17.6 and above) .......................... */
677
678 #if LT_HAS_PERL(5, 17, 6)
679
680 STATIC OP *lt_pp_padrange(pTHX) {
681  lt_op_padrange_info roi;
682
683  if (lt_padrange_map_fetch(PL_op, &roi)) {
684   PADOFFSET i, base, count;
685   const OP *p;
686
687   base  = PL_op->op_targ;
688   count = PL_op->op_private & OPpPADRANGE_COUNTMASK;
689
690   for (i = 0, p = roi.padxv_start; i < count && p; ++i, p = OP_SIBLING(p)) {
691    lt_op_padxv_info oi;
692    if (p->op_type == OP_PADSV && lt_padxv_map_fetch(p, &oi))
693     lt_op_padxv_info_call(&oi, PAD_SV(base + i));
694   }
695
696   return roi.old_pp(aTHX);
697  }
698
699  return PL_op->op_ppaddr(aTHX);
700 }
701
702 #endif
703
704 /* ... Our ck_pad{any,sv} .................................................. */
705
706 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
707  * function, but are instead manually mutated from a padany. So we store
708  * the op entry in the op map in the padany check function, and we set their
709  * op_ppaddr member in our peephole optimizer replacement below. */
710
711 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
712
713 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
714  HV *stash;
715  SV *code;
716
717  o = lt_old_ck_padany(aTHX_ o);
718
719  stash = PL_in_my_stash;
720  if (stash && (code = lt_hint())) {
721   dMY_CXT;
722   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
723   SV *orig_meth = MY_CXT.default_meth; /* Guarded by lt_hint() */
724   SV *type_pkg  = NULL;
725   SV *type_meth = NULL;
726   int items;
727
728   dSP;
729
730   SvREADONLY_on(orig_pkg);
731
732   ENTER;
733   SAVETMPS;
734
735   PUSHMARK(SP);
736   EXTEND(SP, 2);
737   PUSHs(orig_pkg);
738   PUSHs(orig_meth);
739   PUTBACK;
740
741   items = call_sv(code, G_ARRAY);
742
743   SPAGAIN;
744   if (items > 2)
745    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
746   if (items == 0) {
747    SvREFCNT_dec(orig_pkg);
748    FREETMPS;
749    LEAVE;
750    goto skip;
751   } else {
752    SV *rsv;
753    if (items > 1) {
754     rsv = POPs;
755     if (SvOK(rsv)) {
756      type_meth = newSVsv(rsv);
757      SvREADONLY_on(type_meth);
758     }
759    }
760    rsv = POPs;
761    if (SvOK(rsv)) {
762     type_pkg = newSVsv(rsv);
763     SvREADONLY_on(type_pkg);
764    }
765   }
766   PUTBACK;
767
768   FREETMPS;
769   LEAVE;
770
771   if (!type_pkg) {
772    type_pkg = orig_pkg;
773    SvREFCNT_inc_simple_void_NN(orig_pkg);
774   }
775
776   if (!type_meth) {
777    type_meth = orig_meth;
778    SvREFCNT_inc_simple_void_NN(orig_meth);
779   }
780
781   lt_padxv_map_store(o, orig_pkg, type_pkg, type_meth, o->op_ppaddr);
782  } else {
783 skip:
784   lt_map_delete(o);
785  }
786
787  return o;
788 }
789
790 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
791
792 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
793  lt_map_delete(o);
794
795  return lt_old_ck_padsv(aTHX_ o);
796 }
797
798 /* ... Our peephole optimizer .............................................. */
799
800 #if LT_HAS_PERL(5, 17, 6)
801
802 STATIC int lt_maybe_padrange_setup(pTHX_ OP *o, const OP *start) {
803 #define lt_maybe_padrange_setup(O, S) lt_maybe_padrange_setup(aTHX_ (O), (S))
804  PADOFFSET i, count;
805  const OP *p;
806
807  count = o->op_private & OPpPADRANGE_COUNTMASK;
808
809  for (i = 0, p = start; i < count && p; ++i, p = OP_SIBLING(p)) {
810   if (p->op_type == OP_PADSV) {
811    /* In a padrange sequence, either all lexicals are typed, or none are.
812     * Thus we can stop at the first padsv op. However, note that these
813     * lexicals can need to call different methods in different packages. */
814    LT_LOCK(&lt_op_map_mutex);
815    if (ptable_fetch(lt_op_padxv_map, p)) {
816     LT_UNLOCK(&lt_op_map_mutex);
817     lt_padrange_map_store(o, start, o->op_ppaddr);
818     o->op_ppaddr = lt_pp_padrange;
819    } else {
820     LT_UNLOCK(&lt_op_map_mutex);
821    }
822    return 1;
823   }
824  }
825
826  return 0;
827 }
828
829 #endif
830
831 STATIC peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
832
833 STATIC void lt_peep_rec(pTHX_ OP *o, ptable *seen) {
834 #define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
835  for (; o; o = o->op_next) {
836   if (ptable_fetch(seen, o))
837    break;
838   ptable_seen_store(seen, o, o);
839
840   switch (o->op_type) {
841    case OP_PADSV:
842     if (o->op_ppaddr != lt_pp_padsv && o->op_private & OPpLVAL_INTRO) {
843      lt_op_padxv_info *oi;
844      LT_LOCK(&lt_op_map_mutex);
845      oi = ptable_fetch(lt_op_padxv_map, o);
846      if (oi) {
847       oi->old_pp   = o->op_ppaddr;
848       o->op_ppaddr = lt_pp_padsv;
849      }
850      LT_UNLOCK(&lt_op_map_mutex);
851     }
852     break;
853 #if LT_HAS_PERL(5, 17, 6)
854    case OP_PADRANGE:
855     /* We deal with special padrange ops later, in the aassign op they belong
856      * to. */
857     if (o->op_ppaddr != lt_pp_padrange && o->op_private & OPpLVAL_INTRO
858                                        && !(o->op_flags & OPf_SPECIAL)) {
859      /* A padrange op is guaranteed to have previously been a pushmark.
860       * Moreover, for non-special padrange ops (i.e. that aren't for
861       * my (...) = @_), the original padxv ops are its siblings. */
862      lt_maybe_padrange_setup(o, OP_SIBLING(o));
863     }
864     break;
865    case OP_AASSIGN: {
866     OP *op;
867     if (cBINOPo->op_first && cBINOPo->op_first->op_flags & OPf_KIDS
868                           && (op = cUNOPx(cBINOPo->op_first)->op_first)
869                           && op->op_type == OP_PADRANGE
870                           && op->op_ppaddr != lt_pp_padrange
871                           && op->op_private & OPpLVAL_INTRO
872                           && op->op_flags & OPf_SPECIAL) {
873      const OP *start = cUNOPx(cBINOPo->op_last)->op_first;
874      if (start->op_type == OP_PUSHMARK)
875       start = OP_SIBLING(start);
876      lt_maybe_padrange_setup(op, start);
877     }
878     break;
879    }
880 #endif
881 #if !LT_HAS_RPEEP
882    case OP_MAPWHILE:
883    case OP_GREPWHILE:
884    case OP_AND:
885    case OP_OR:
886    case OP_ANDASSIGN:
887    case OP_ORASSIGN:
888    case OP_COND_EXPR:
889    case OP_RANGE:
890 # if LT_HAS_PERL(5, 10, 0)
891    case OP_ONCE:
892    case OP_DOR:
893    case OP_DORASSIGN:
894 # endif
895     lt_peep_rec(cLOGOPo->op_other);
896     break;
897    case OP_ENTERLOOP:
898    case OP_ENTERITER:
899     lt_peep_rec(cLOOPo->op_redoop);
900     lt_peep_rec(cLOOPo->op_nextop);
901     lt_peep_rec(cLOOPo->op_lastop);
902     break;
903 # if LT_HAS_PERL(5, 9, 5)
904    case OP_SUBST:
905     lt_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
906     break;
907 # else
908    case OP_QR:
909    case OP_MATCH:
910    case OP_SUBST:
911     lt_peep_rec(cPMOPo->op_pmreplstart);
912     break;
913 # endif
914 #endif /* !LT_HAS_RPEEP */
915    default:
916     break;
917   }
918  }
919 }
920
921 STATIC void lt_peep(pTHX_ OP *o) {
922  dMY_CXT;
923  ptable *seen = MY_CXT.seen;
924
925  lt_old_peep(aTHX_ o);
926
927  if (seen) {
928   ptable_seen_clear(seen);
929   lt_peep_rec(o);
930   ptable_seen_clear(seen);
931  }
932 }
933
934 /* --- Interpreter setup/teardown ------------------------------------------ */
935
936
937 STATIC U32 lt_initialized = 0;
938
939 STATIC void lt_teardown(pTHX_ void *root) {
940  if (!lt_initialized)
941   return;
942
943 #if LT_MULTIPLICITY
944  if (aTHX != root)
945   return;
946 #endif
947
948  {
949   dMY_CXT;
950 #if LT_THREADSAFE
951   ptable_hints_free(MY_CXT.tbl);
952   MY_CXT.tbl          = NULL;
953 #endif
954   ptable_seen_free(MY_CXT.seen);
955   MY_CXT.seen         = NULL;
956   SvREFCNT_dec(MY_CXT.default_meth);
957   MY_CXT.default_meth = NULL;
958  }
959
960  lt_ck_restore(OP_PADANY, &lt_old_ck_padany);
961  lt_ck_restore(OP_PADSV,  &lt_old_ck_padsv);
962
963 #if LT_HAS_RPEEP
964  PL_rpeepp   = lt_old_peep;
965 #else
966  PL_peepp    = lt_old_peep;
967 #endif
968  lt_old_peep = 0;
969
970  lt_initialized = 0;
971 }
972
973 STATIC void lt_setup(pTHX) {
974 #define lt_setup() lt_setup(aTHX)
975  if (lt_initialized)
976   return;
977
978  {
979   MY_CXT_INIT;
980 #if LT_THREADSAFE
981   MY_CXT.tbl          = ptable_new();
982   MY_CXT.owner        = aTHX;
983 #endif
984   MY_CXT.seen         = ptable_new();
985   MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
986   SvREADONLY_on(MY_CXT.default_meth);
987  }
988
989  lt_ck_replace(OP_PADANY, lt_ck_padany, &lt_old_ck_padany);
990  lt_ck_replace(OP_PADSV,  lt_ck_padsv,  &lt_old_ck_padsv);
991
992 #if LT_HAS_RPEEP
993  lt_old_peep = PL_rpeepp;
994  PL_rpeepp   = lt_peep;
995 #else
996  lt_old_peep = PL_peepp;
997  PL_peepp    = lt_peep;
998 #endif
999
1000 #if LT_MULTIPLICITY
1001  call_atexit(lt_teardown, aTHX);
1002 #else
1003  call_atexit(lt_teardown, NULL);
1004 #endif
1005
1006  lt_initialized = 1;
1007 }
1008
1009 STATIC U32 lt_booted = 0;
1010
1011 /* --- XS ------------------------------------------------------------------ */
1012
1013 MODULE = Lexical::Types      PACKAGE = Lexical::Types
1014
1015 PROTOTYPES: ENABLE
1016
1017 BOOT:
1018 {
1019  if (!lt_booted++) {
1020   HV *stash;
1021
1022   lt_op_padxv_map    = ptable_new();
1023 #if LT_HAS_PERL(5, 17, 6)
1024   lt_op_padrange_map = ptable_new();
1025 #endif
1026 #ifdef USE_ITHREADS
1027   MUTEX_INIT(&lt_op_map_mutex);
1028 #endif
1029
1030   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
1031
1032   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1033   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
1034   newCONSTSUB(stash, "LT_FORKSAFE",   newSVuv(LT_FORKSAFE));
1035  }
1036
1037  lt_setup();
1038 }
1039
1040 #if LT_THREADSAFE
1041
1042 void
1043 CLONE(...)
1044 PROTOTYPE: DISABLE
1045 PREINIT:
1046  ptable *t;
1047  ptable *s;
1048  SV     *cloned_default_meth;
1049  GV     *gv;
1050 PPCODE:
1051  {
1052   {
1053    lt_ptable_clone_ud ud;
1054    dMY_CXT;
1055
1056    t = ptable_new();
1057    lt_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1058    ptable_walk(MY_CXT.tbl, lt_ptable_clone, &ud);
1059    cloned_default_meth = lt_dup_inc(MY_CXT.default_meth, &ud);
1060    lt_ptable_clone_ud_deinit(ud);
1061   }
1062   s = ptable_new();
1063  }
1064  {
1065   MY_CXT_CLONE;
1066   MY_CXT.tbl          = t;
1067   MY_CXT.owner        = aTHX;
1068   MY_CXT.seen         = s;
1069   MY_CXT.default_meth = cloned_default_meth;
1070  }
1071  gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1072  if (gv) {
1073   CV *cv = GvCV(gv);
1074   if (!PL_endav)
1075    PL_endav = newAV();
1076   SvREFCNT_inc(cv);
1077   if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1078    SvREFCNT_dec(cv);
1079   sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &lt_endav_vtbl, NULL, 0);
1080  }
1081  XSRETURN(0);
1082
1083 void
1084 _THREAD_CLEANUP(...)
1085 PROTOTYPE: DISABLE
1086 PPCODE:
1087  lt_thread_cleanup(aTHX_ NULL);
1088  XSRETURN(0);
1089
1090 #endif
1091
1092 SV *
1093 _tag(SV *value)
1094 PROTOTYPE: $
1095 CODE:
1096  RETVAL = lt_tag(value);
1097 OUTPUT:
1098  RETVAL