]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
518fd8f1122c37f5d51966b6270c17f02c156f3e
[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 SvREFCNT_inc_simple_void_NN
43 # define SvREFCNT_inc_simple_void_NN(S) ((void) SvREFCNT_inc(S))
44 #endif
45
46 /* ... Thread safety and multiplicity ...................................... */
47
48 /* Safe unless stated otherwise in Makefile.PL */
49 #ifndef LT_FORKSAFE
50 # define LT_FORKSAFE 1
51 #endif
52
53 #ifndef LT_MULTIPLICITY
54 # if defined(MULTIPLICITY) || defined(PERL_IMPLICIT_CONTEXT)
55 #  define LT_MULTIPLICITY 1
56 # else
57 #  define LT_MULTIPLICITY 0
58 # endif
59 #endif
60
61 #ifndef tTHX
62 # define tTHX PerlInterpreter*
63 #endif
64
65 #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))
66 # define LT_THREADSAFE 1
67 # ifndef MY_CXT_CLONE
68 #  define MY_CXT_CLONE \
69     dMY_CXT_SV;                                                      \
70     my_cxt_t *my_cxtp = (my_cxt_t*)SvPVX(newSV(sizeof(my_cxt_t)-1)); \
71     Copy(INT2PTR(my_cxt_t*, SvUV(my_cxt_sv)), my_cxtp, 1, my_cxt_t); \
72     sv_setuv(my_cxt_sv, PTR2UV(my_cxtp))
73 # endif
74 #else
75 # define LT_THREADSAFE 0
76 # undef  dMY_CXT
77 # define dMY_CXT      dNOOP
78 # undef  MY_CXT
79 # define MY_CXT       lt_globaldata
80 # undef  START_MY_CXT
81 # define START_MY_CXT STATIC my_cxt_t MY_CXT;
82 # undef  MY_CXT_INIT
83 # define MY_CXT_INIT  NOOP
84 # undef  MY_CXT_CLONE
85 # define MY_CXT_CLONE NOOP
86 # undef  pMY_CXT
87 # define pMY_CXT
88 # undef  pMY_CXT_
89 # define pMY_CXT_
90 # undef  aMY_CXT
91 # define aMY_CXT
92 # undef  aMY_CXT_
93 # define aMY_CXT_
94 #endif
95
96 #if defined(OP_CHECK_MUTEX_LOCK) && defined(OP_CHECK_MUTEX_UNLOCK)
97 # define LT_CHECK_MUTEX_LOCK   OP_CHECK_MUTEX_LOCK
98 # define LT_CHECK_MUTEX_UNLOCK OP_CHECK_MUTEX_UNLOCK
99 #else
100 # define LT_CHECK_MUTEX_LOCK   OP_REFCNT_LOCK
101 # define LT_CHECK_MUTEX_UNLOCK OP_REFCNT_UNLOCK
102 #endif
103
104 typedef OP *(*lt_ck_t)(pTHX_ OP *);
105
106 #ifdef wrap_op_checker
107
108 # define lt_ck_replace(T, NC, OCP) wrap_op_checker((T), (NC), (OCP))
109
110 #else
111
112 STATIC void lt_ck_replace(pTHX_ OPCODE type, lt_ck_t new_ck, lt_ck_t *old_ck_p){
113 #define lt_ck_replace(T, NC, OCP) lt_ck_replace(aTHX_ (T), (NC), (OCP))
114  LT_CHECK_MUTEX_LOCK;
115  if (!*old_ck_p) {
116   *old_ck_p      = PL_check[type];
117   PL_check[type] = new_ck;
118  }
119  LT_CHECK_MUTEX_UNLOCK;
120 }
121
122 #endif
123
124 STATIC void lt_ck_restore(pTHX_ OPCODE type, lt_ck_t *old_ck_p) {
125 #define lt_ck_restore(T, OCP) lt_ck_restore(aTHX_ (T), (OCP))
126  LT_CHECK_MUTEX_LOCK;
127  if (*old_ck_p) {
128   PL_check[type] = *old_ck_p;
129   *old_ck_p      = 0;
130  }
131  LT_CHECK_MUTEX_UNLOCK;
132 }
133
134 /* --- Helpers ------------------------------------------------------------- */
135
136 /* ... Thread-safe hints ................................................... */
137
138 #if LT_WORKAROUND_REQUIRE_PROPAGATION
139
140 typedef struct {
141  SV *code;
142  IV  require_tag;
143 } lt_hint_t;
144
145 #define LT_HINT_STRUCT 1
146
147 #define LT_HINT_CODE(H) ((H)->code)
148
149 #define LT_HINT_FREE(H) { \
150  lt_hint_t *h = (H);      \
151  SvREFCNT_dec(h->code);   \
152  PerlMemShared_free(h);   \
153 }
154
155 #else  /*  LT_WORKAROUND_REQUIRE_PROPAGATION */
156
157 typedef SV lt_hint_t;
158
159 #define LT_HINT_STRUCT 0
160
161 #define LT_HINT_CODE(H) (H)
162
163 #define LT_HINT_FREE(H) SvREFCNT_dec(H);
164
165 #endif /* !LT_WORKAROUND_REQUIRE_PROPAGATION */
166
167 #if LT_THREADSAFE
168
169 #define PTABLE_NAME        ptable_hints
170 #define PTABLE_VAL_FREE(V) LT_HINT_FREE(V)
171
172 #define pPTBL  pTHX
173 #define pPTBL_ pTHX_
174 #define aPTBL  aTHX
175 #define aPTBL_ aTHX_
176
177 #include "ptable.h"
178
179 #define ptable_hints_store(T, K, V) ptable_hints_store(aTHX_ (T), (K), (V))
180 #define ptable_hints_free(T)        ptable_hints_free(aTHX_ (T))
181
182 #endif /* LT_THREADSAFE */
183
184 /* ... "Seen" pointer table ................................................ */
185
186 #define PTABLE_NAME        ptable_seen
187 #define PTABLE_VAL_FREE(V) NOOP
188
189 #include "ptable.h"
190
191 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
192 #define ptable_seen_store(T, K, V) ptable_seen_store(aPTBLMS_ (T), (K), (V))
193 #define ptable_seen_clear(T)       ptable_seen_clear(aPTBLMS_ (T))
194 #define ptable_seen_free(T)        ptable_seen_free(aPTBLMS_ (T))
195
196 /* ... Global data ......................................................... */
197
198 #define MY_CXT_KEY __PACKAGE__ "::_guts" XS_VERSION
199
200 typedef struct {
201 #if LT_THREADSAFE
202  ptable *tbl; /* It really is a ptable_hints */
203  tTHX    owner;
204 #endif
205  ptable *seen; /* It really is a ptable_seen */
206  SV     *default_meth;
207 } my_cxt_t;
208
209 START_MY_CXT
210
211 /* ... Cloning global data ................................................. */
212
213 #if LT_THREADSAFE
214
215 typedef struct {
216  ptable *tbl;
217 #if LT_HAS_PERL(5, 13, 2)
218  CLONE_PARAMS *params;
219 #else
220  CLONE_PARAMS params;
221 #endif
222 } lt_ptable_clone_ud;
223
224 #if LT_HAS_PERL(5, 13, 2)
225 # define lt_ptable_clone_ud_init(U, T, O) \
226    (U).tbl    = (T); \
227    (U).params = Perl_clone_params_new((O), aTHX)
228 # define lt_ptable_clone_ud_deinit(U) Perl_clone_params_del((U).params)
229 # define lt_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), (U)->params))
230 #else
231 # define lt_ptable_clone_ud_init(U, T, O) \
232    (U).tbl               = (T);     \
233    (U).params.stashes    = newAV(); \
234    (U).params.flags      = 0;       \
235    (U).params.proto_perl = (O)
236 # define lt_ptable_clone_ud_deinit(U) SvREFCNT_dec((U).params.stashes)
237 # define lt_dup_inc(S, U)             SvREFCNT_inc(sv_dup((S), &((U)->params)))
238 #endif
239
240 STATIC void lt_ptable_clone(pTHX_ ptable_ent *ent, void *ud_) {
241  lt_ptable_clone_ud *ud = ud_;
242  lt_hint_t *h1 = ent->val;
243  lt_hint_t *h2;
244
245 #if LT_HINT_STRUCT
246
247  h2              = PerlMemShared_malloc(sizeof *h2);
248  h2->code        = lt_dup_inc(h1->code, ud);
249 #if LT_WORKAROUND_REQUIRE_PROPAGATION
250  h2->require_tag = PTR2IV(lt_dup_inc(INT2PTR(SV *, h1->require_tag), ud));
251 #endif
252
253 #else /*   LT_HINT_STRUCT */
254
255  h2 = lt_dup_inc(h1, ud);
256
257 #endif /* !LT_HINT_STRUCT */
258
259  ptable_hints_store(ud->tbl, ent->key, h2);
260 }
261
262 #include "reap.h"
263
264 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
265  dMY_CXT;
266
267  ptable_hints_free(MY_CXT.tbl);
268  ptable_seen_free(MY_CXT.seen);
269 }
270
271 #endif /* LT_THREADSAFE */
272
273 /* ... Hint tags ........................................................... */
274
275 #if LT_WORKAROUND_REQUIRE_PROPAGATION
276
277 STATIC IV lt_require_tag(pTHX) {
278 #define lt_require_tag() lt_require_tag(aTHX)
279  const CV *cv, *outside;
280
281  cv = PL_compcv;
282
283  if (!cv) {
284   /* If for some reason the pragma is operational at run-time, try to discover
285    * the current cv in use. */
286   const PERL_SI *si;
287
288   for (si = PL_curstackinfo; si; si = si->si_prev) {
289    I32 cxix;
290
291    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
292     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
293
294     switch (CxTYPE(cx)) {
295      case CXt_SUB:
296      case CXt_FORMAT:
297       /* The propagation workaround is only needed up to 5.10.0 and at that
298        * time format and sub contexts were still identical. And even later the
299        * cv members offsets should have been kept the same. */
300       cv = cx->blk_sub.cv;
301       goto get_enclosing_cv;
302      case CXt_EVAL:
303       cv = cx->blk_eval.cv;
304       goto get_enclosing_cv;
305      default:
306       break;
307     }
308    }
309   }
310
311   cv = PL_main_cv;
312  }
313
314 get_enclosing_cv:
315  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
316   cv = outside;
317
318  return PTR2IV(cv);
319 }
320
321 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
322
323 STATIC SV *lt_tag(pTHX_ SV *value) {
324 #define lt_tag(V) lt_tag(aTHX_ (V))
325  lt_hint_t *h;
326  SV *code = NULL;
327
328  if (SvROK(value)) {
329   value = SvRV(value);
330   if (SvTYPE(value) >= SVt_PVCV) {
331    code = value;
332    SvREFCNT_inc_simple_void_NN(code);
333   }
334  }
335
336 #if LT_HINT_STRUCT
337  h = PerlMemShared_malloc(sizeof *h);
338  h->code        = code;
339 # if LT_WORKAROUND_REQUIRE_PROPAGATION
340  h->require_tag = lt_require_tag();
341 # endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
342 #else  /*  LT_HINT_STRUCT */
343  h = code;
344 #endif /* !LT_HINT_STRUCT */
345
346 #if LT_THREADSAFE
347  {
348   dMY_CXT;
349   /* We only need for the key to be an unique tag for looking up the value later
350    * Allocated memory provides convenient unique identifiers, so that's why we
351    * use the hint as the key itself. */
352   ptable_hints_store(MY_CXT.tbl, h, h);
353  }
354 #endif /* LT_THREADSAFE */
355
356  return newSViv(PTR2IV(h));
357 }
358
359 STATIC SV *lt_detag(pTHX_ const SV *hint) {
360 #define lt_detag(H) lt_detag(aTHX_ (H))
361  lt_hint_t *h;
362 #if LT_THREADSAFE
363  dMY_CXT;
364 #endif
365
366  if (!(hint && SvIOK(hint)))
367   return NULL;
368
369  h = INT2PTR(lt_hint_t *, SvIVX(hint));
370 #if LT_THREADSAFE
371  h = ptable_fetch(MY_CXT.tbl, h);
372 #endif /* LT_THREADSAFE */
373 #if LT_WORKAROUND_REQUIRE_PROPAGATION
374  if (lt_require_tag() != h->require_tag)
375   return NULL;
376 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
377
378  return LT_HINT_CODE(h);
379 }
380
381 STATIC U32 lt_hash = 0;
382
383 STATIC SV *lt_hint(pTHX) {
384 #define lt_hint() lt_hint(aTHX)
385  SV *hint;
386 #ifdef cop_hints_fetch_pvn
387  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, lt_hash,0);
388 #elif LT_HAS_PERL(5, 9, 5)
389  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
390                                        NULL,
391                                        __PACKAGE__, __PACKAGE_LEN__,
392                                        0,
393                                        lt_hash);
394 #else
395  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
396  if (!val)
397   return 0;
398  hint = *val;
399 #endif
400  return lt_detag(hint);
401 }
402
403 /* ... op => info map ...................................................... */
404
405 #define PTABLE_NAME        ptable_map
406 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
407
408 #include "ptable.h"
409
410 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
411 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
412 #define ptable_map_delete(T, K)   ptable_map_delete(aPTBLMS_ (T), (K))
413
414 STATIC ptable *lt_op_map = NULL;
415
416 #ifdef USE_ITHREADS
417
418 STATIC perl_mutex lt_op_map_mutex;
419
420 #define LT_LOCK(M)   MUTEX_LOCK(M)
421 #define LT_UNLOCK(M) MUTEX_UNLOCK(M)
422
423 #else /* USE_ITHREADS */
424
425 #define LT_LOCK(M)
426 #define LT_UNLOCK(M)
427
428 #endif /* !USE_ITHREADS */
429
430 typedef struct {
431 #ifdef MULTIPLICITY
432  STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
433  char *buf;
434 #else /* MULTIPLICITY */
435  SV *orig_pkg;
436  SV *type_pkg;
437  SV *type_meth;
438 #endif /* !MULTIPLICITY */
439  OP *(*old_pp_padsv)(pTHX);
440 } lt_op_info;
441
442 STATIC void lt_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*old_pp_padsv)(pTHX)) {
443 #define lt_map_store(O, OP, TP, TM, PP) lt_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
444  lt_op_info *oi;
445
446  LT_LOCK(&lt_op_map_mutex);
447
448  if (!(oi = ptable_fetch(lt_op_map, o))) {
449   oi = PerlMemShared_malloc(sizeof *oi);
450   ptable_map_store(lt_op_map, o, oi);
451 #ifdef MULTIPLICITY
452   oi->buf      = NULL;
453   oi->buf_size = 0;
454 #else /* MULTIPLICITY */
455  } else {
456   SvREFCNT_dec(oi->orig_pkg);
457   SvREFCNT_dec(oi->type_pkg);
458   SvREFCNT_dec(oi->type_meth);
459 #endif /* !MULTIPLICITY */
460  }
461
462 #ifdef MULTIPLICITY
463  {
464   STRLEN op_len       = SvCUR(orig_pkg);
465   STRLEN tp_len       = SvCUR(type_pkg);
466   STRLEN tm_len       = SvCUR(type_meth);
467   STRLEN new_buf_size = op_len + tp_len + tm_len;
468   char *buf;
469   if (new_buf_size > oi->buf_size) {
470    PerlMemShared_free(oi->buf);
471    oi->buf      = PerlMemShared_malloc(new_buf_size);
472    oi->buf_size = new_buf_size;
473   }
474   buf  = oi->buf;
475   Copy(SvPVX(orig_pkg),  buf, op_len, char);
476   buf += op_len;
477   Copy(SvPVX(type_pkg),  buf, tp_len, char);
478   buf += tp_len;
479   Copy(SvPVX(type_meth), buf, tm_len, char);
480   oi->orig_pkg_len  = op_len;
481   oi->type_pkg_len  = tp_len;
482   oi->type_meth_len = tm_len;
483   SvREFCNT_dec(orig_pkg);
484   SvREFCNT_dec(type_pkg);
485   SvREFCNT_dec(type_meth);
486  }
487 #else /* MULTIPLICITY */
488  oi->orig_pkg  = orig_pkg;
489  oi->type_pkg  = type_pkg;
490  oi->type_meth = type_meth;
491 #endif /* !MULTIPLICITY */
492
493  oi->old_pp_padsv = old_pp_padsv;
494
495  LT_UNLOCK(&lt_op_map_mutex);
496 }
497
498 STATIC const lt_op_info *lt_map_fetch(const OP *o, lt_op_info *oi) {
499  const lt_op_info *val;
500
501  LT_LOCK(&lt_op_map_mutex);
502
503  val = ptable_fetch(lt_op_map, o);
504  if (val) {
505   *oi = *val;
506   val = oi;
507  }
508
509  LT_UNLOCK(&lt_op_map_mutex);
510
511  return val;
512 }
513
514 STATIC void lt_map_delete(pTHX_ const OP *o) {
515 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
516  LT_LOCK(&lt_op_map_mutex);
517
518  ptable_map_delete(lt_op_map, o);
519
520  LT_UNLOCK(&lt_op_map_mutex);
521 }
522
523 /* --- Hooks --------------------------------------------------------------- */
524
525 /* ... Our pp_padsv ........................................................ */
526
527 STATIC OP *lt_pp_padsv(pTHX) {
528  lt_op_info oi;
529
530  if (lt_map_fetch(PL_op, &oi)) {
531   SV *orig_pkg, *type_pkg, *type_meth;
532   int items;
533   dSP;
534   dTARGET;
535
536   ENTER;
537   SAVETMPS;
538
539 #ifdef MULTIPLICITY
540   {
541    STRLEN op_len = oi.orig_pkg_len, tp_len = oi.type_pkg_len;
542    char *buf = oi.buf;
543    orig_pkg  = sv_2mortal(newSVpvn(buf, op_len));
544    SvREADONLY_on(orig_pkg);
545    buf      += op_len;
546    type_pkg  = sv_2mortal(newSVpvn(buf, tp_len));
547    SvREADONLY_on(type_pkg);
548    buf      += tp_len;
549    type_meth = sv_2mortal(newSVpvn(buf, oi.type_meth_len));
550    SvREADONLY_on(type_meth);
551   }
552 #else /* MULTIPLICITY */
553   orig_pkg  = oi.orig_pkg;
554   type_pkg  = oi.type_pkg;
555   type_meth = oi.type_meth;
556 #endif /* !MULTIPLICITY */
557
558   PUSHMARK(SP);
559   EXTEND(SP, 3);
560   PUSHs(type_pkg);
561   PUSHTARG;
562   PUSHs(orig_pkg);
563   PUTBACK;
564
565   items = call_sv(type_meth, G_ARRAY | G_METHOD);
566
567   SPAGAIN;
568   switch (items) {
569    case 0:
570     break;
571    case 1:
572     sv_setsv(TARG, POPs);
573     break;
574    default:
575     croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
576   }
577   PUTBACK;
578
579   FREETMPS;
580   LEAVE;
581
582   return oi.old_pp_padsv(aTHX);
583  }
584
585  return PL_op->op_ppaddr(aTHX);
586 }
587
588 /* ... Our ck_pad{any,sv} .................................................. */
589
590 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
591  * function, but are instead manually mutated from a padany. So we store
592  * the op entry in the op map in the padany check function, and we set their
593  * op_ppaddr member in our peephole optimizer replacement below. */
594
595 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
596
597 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
598  HV *stash;
599  SV *code;
600
601  o = lt_old_ck_padany(aTHX_ o);
602
603  stash = PL_in_my_stash;
604  if (stash && (code = lt_hint())) {
605   dMY_CXT;
606   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
607   SV *orig_meth = MY_CXT.default_meth;
608   SV *type_pkg  = NULL;
609   SV *type_meth = NULL;
610   int items;
611
612   dSP;
613
614   SvREADONLY_on(orig_pkg);
615
616   ENTER;
617   SAVETMPS;
618
619   PUSHMARK(SP);
620   EXTEND(SP, 2);
621   PUSHs(orig_pkg);
622   PUSHs(orig_meth);
623   PUTBACK;
624
625   items = call_sv(code, G_ARRAY);
626
627   SPAGAIN;
628   if (items > 2)
629    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
630   if (items == 0) {
631    SvREFCNT_dec(orig_pkg);
632    FREETMPS;
633    LEAVE;
634    goto skip;
635   } else {
636    SV *rsv;
637    if (items > 1) {
638     rsv = POPs;
639     if (SvOK(rsv)) {
640      type_meth = newSVsv(rsv);
641      SvREADONLY_on(type_meth);
642     }
643    }
644    rsv = POPs;
645    if (SvOK(rsv)) {
646     type_pkg = newSVsv(rsv);
647     SvREADONLY_on(type_pkg);
648    }
649   }
650   PUTBACK;
651
652   FREETMPS;
653   LEAVE;
654
655   if (!type_pkg) {
656    type_pkg = orig_pkg;
657    SvREFCNT_inc_simple_void_NN(orig_pkg);
658   }
659
660   if (!type_meth) {
661    type_meth = orig_meth;
662    SvREFCNT_inc_simple_void_NN(orig_meth);
663   }
664
665   lt_map_store(o, orig_pkg, type_pkg, type_meth, o->op_ppaddr);
666  } else {
667 skip:
668   lt_map_delete(o);
669  }
670
671  return o;
672 }
673
674 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
675
676 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
677  lt_map_delete(o);
678
679  return lt_old_ck_padsv(aTHX_ o);
680 }
681
682 /* ... Our peephole optimizer .............................................. */
683
684 STATIC peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
685
686 STATIC void lt_peep_rec(pTHX_ OP *o, ptable *seen) {
687 #define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
688  for (; o; o = o->op_next) {
689   lt_op_info *oi = NULL;
690
691   if (ptable_fetch(seen, o))
692    break;
693   ptable_seen_store(seen, o, o);
694
695   switch (o->op_type) {
696    case OP_PADSV:
697     if (o->op_ppaddr != lt_pp_padsv && o->op_private & OPpLVAL_INTRO) {
698      LT_LOCK(&lt_op_map_mutex);
699      oi = ptable_fetch(lt_op_map, o);
700      if (oi) {
701       oi->old_pp_padsv = o->op_ppaddr;
702       o->op_ppaddr     = lt_pp_padsv;
703      }
704      LT_UNLOCK(&lt_op_map_mutex);
705     }
706     break;
707 #if !LT_HAS_RPEEP
708    case OP_MAPWHILE:
709    case OP_GREPWHILE:
710    case OP_AND:
711    case OP_OR:
712    case OP_ANDASSIGN:
713    case OP_ORASSIGN:
714    case OP_COND_EXPR:
715    case OP_RANGE:
716 # if LT_HAS_PERL(5, 10, 0)
717    case OP_ONCE:
718    case OP_DOR:
719    case OP_DORASSIGN:
720 # endif
721     lt_peep_rec(cLOGOPo->op_other);
722     break;
723    case OP_ENTERLOOP:
724    case OP_ENTERITER:
725     lt_peep_rec(cLOOPo->op_redoop);
726     lt_peep_rec(cLOOPo->op_nextop);
727     lt_peep_rec(cLOOPo->op_lastop);
728     break;
729 # if LT_HAS_PERL(5, 9, 5)
730    case OP_SUBST:
731     lt_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
732     break;
733 # else
734    case OP_QR:
735    case OP_MATCH:
736    case OP_SUBST:
737     lt_peep_rec(cPMOPo->op_pmreplstart);
738     break;
739 # endif
740 #endif /* !LT_HAS_RPEEP */
741    default:
742     break;
743   }
744  }
745 }
746
747 STATIC void lt_peep(pTHX_ OP *o) {
748  dMY_CXT;
749  ptable *seen = MY_CXT.seen;
750
751  lt_old_peep(aTHX_ o);
752
753  ptable_seen_clear(seen);
754  lt_peep_rec(o);
755  ptable_seen_clear(seen);
756 }
757
758 /* --- Interpreter setup/teardown ------------------------------------------ */
759
760
761 STATIC U32 lt_initialized = 0;
762
763 STATIC void lt_teardown(pTHX_ void *root) {
764  if (!lt_initialized)
765   return;
766
767 #if LT_MULTIPLICITY
768  if (aTHX != root)
769   return;
770 #endif
771
772  {
773   dMY_CXT;
774 #if LT_THREADSAFE
775   ptable_hints_free(MY_CXT.tbl);
776 #endif
777   ptable_seen_free(MY_CXT.seen);
778   SvREFCNT_dec(MY_CXT.default_meth);
779  }
780
781  lt_ck_restore(OP_PADANY, &lt_old_ck_padany);
782  lt_ck_restore(OP_PADSV,  &lt_old_ck_padsv);
783
784 #if LT_HAS_RPEEP
785  PL_rpeepp   = lt_old_peep;
786 #else
787  PL_peepp    = lt_old_peep;
788 #endif
789  lt_old_peep = 0;
790
791  lt_initialized = 0;
792 }
793
794 STATIC void lt_setup(pTHX) {
795 #define lt_setup() lt_setup(aTHX)
796  if (lt_initialized)
797   return;
798
799  {
800   MY_CXT_INIT;
801 #if LT_THREADSAFE
802   MY_CXT.tbl          = ptable_new();
803   MY_CXT.owner        = aTHX;
804 #endif
805   MY_CXT.seen         = ptable_new();
806   MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
807   SvREADONLY_on(MY_CXT.default_meth);
808  }
809
810  lt_ck_replace(OP_PADANY, lt_ck_padany, &lt_old_ck_padany);
811  lt_ck_replace(OP_PADSV,  lt_ck_padsv,  &lt_old_ck_padsv);
812
813 #if LT_HAS_RPEEP
814  lt_old_peep = PL_rpeepp;
815  PL_rpeepp   = lt_peep;
816 #else
817  lt_old_peep = PL_peepp;
818  PL_peepp    = lt_peep;
819 #endif
820
821 #if LT_MULTIPLICITY
822  call_atexit(lt_teardown, aTHX);
823 #else
824  call_atexit(lt_teardown, NULL);
825 #endif
826
827  lt_initialized = 1;
828 }
829
830 STATIC U32 lt_booted = 0;
831
832 /* --- XS ------------------------------------------------------------------ */
833
834 MODULE = Lexical::Types      PACKAGE = Lexical::Types
835
836 PROTOTYPES: ENABLE
837
838 BOOT:
839 {
840  if (!lt_booted++) {
841   HV *stash;
842
843   lt_op_map = ptable_new();
844 #ifdef USE_ITHREADS
845   MUTEX_INIT(&lt_op_map_mutex);
846 #endif
847
848   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
849
850   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
851   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
852   newCONSTSUB(stash, "LT_FORKSAFE",   newSVuv(LT_FORKSAFE));
853  }
854
855  lt_setup();
856 }
857
858 #if LT_THREADSAFE
859
860 void
861 CLONE(...)
862 PROTOTYPE: DISABLE
863 PREINIT:
864  ptable *t;
865  ptable *s;
866  SV     *cloned_default_meth;
867 PPCODE:
868  {
869   {
870    lt_ptable_clone_ud ud;
871    dMY_CXT;
872
873    t = ptable_new();
874    lt_ptable_clone_ud_init(ud, t, MY_CXT.owner);
875    ptable_walk(MY_CXT.tbl, lt_ptable_clone, &ud);
876    cloned_default_meth = lt_dup_inc(MY_CXT.default_meth, &ud);
877    lt_ptable_clone_ud_deinit(ud);
878   }
879   s = ptable_new();
880  }
881  {
882   MY_CXT_CLONE;
883   MY_CXT.tbl          = t;
884   MY_CXT.owner        = aTHX;
885   MY_CXT.seen         = s;
886   MY_CXT.default_meth = cloned_default_meth;
887  }
888  reap(3, lt_thread_cleanup, NULL);
889  XSRETURN(0);
890
891 #endif
892
893 SV *
894 _tag(SV *value)
895 PROTOTYPE: $
896 CODE:
897  RETVAL = lt_tag(value);
898 OUTPUT:
899  RETVAL