]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
Compatibility fix for perl 5.21.6
[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 = p->op_next) {
691    while (p->op_type == OP_NULL)
692     p = p->op_next;
693    lt_op_padxv_info oi;
694    if (p->op_type == OP_PADSV && lt_padxv_map_fetch(p, &oi))
695     lt_op_padxv_info_call(&oi, PAD_SV(base + i));
696   }
697
698   return roi.old_pp(aTHX);
699  }
700
701  return PL_op->op_ppaddr(aTHX);
702 }
703
704 #endif
705
706 /* ... Our ck_pad{any,sv} .................................................. */
707
708 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
709  * function, but are instead manually mutated from a padany. So we store
710  * the op entry in the op map in the padany check function, and we set their
711  * op_ppaddr member in our peephole optimizer replacement below. */
712
713 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
714
715 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
716  HV *stash;
717  SV *code;
718
719  o = lt_old_ck_padany(aTHX_ o);
720
721  stash = PL_in_my_stash;
722  if (stash && (code = lt_hint())) {
723   dMY_CXT;
724   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
725   SV *orig_meth = MY_CXT.default_meth; /* Guarded by lt_hint() */
726   SV *type_pkg  = NULL;
727   SV *type_meth = NULL;
728   int items;
729
730   dSP;
731
732   SvREADONLY_on(orig_pkg);
733
734   ENTER;
735   SAVETMPS;
736
737   PUSHMARK(SP);
738   EXTEND(SP, 2);
739   PUSHs(orig_pkg);
740   PUSHs(orig_meth);
741   PUTBACK;
742
743   items = call_sv(code, G_ARRAY);
744
745   SPAGAIN;
746   if (items > 2)
747    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
748   if (items == 0) {
749    SvREFCNT_dec(orig_pkg);
750    FREETMPS;
751    LEAVE;
752    goto skip;
753   } else {
754    SV *rsv;
755    if (items > 1) {
756     rsv = POPs;
757     if (SvOK(rsv)) {
758      type_meth = newSVsv(rsv);
759      SvREADONLY_on(type_meth);
760     }
761    }
762    rsv = POPs;
763    if (SvOK(rsv)) {
764     type_pkg = newSVsv(rsv);
765     SvREADONLY_on(type_pkg);
766    }
767   }
768   PUTBACK;
769
770   FREETMPS;
771   LEAVE;
772
773   if (!type_pkg) {
774    type_pkg = orig_pkg;
775    SvREFCNT_inc_simple_void_NN(orig_pkg);
776   }
777
778   if (!type_meth) {
779    type_meth = orig_meth;
780    SvREFCNT_inc_simple_void_NN(orig_meth);
781   }
782
783   lt_padxv_map_store(o, orig_pkg, type_pkg, type_meth, o->op_ppaddr);
784  } else {
785 skip:
786   lt_map_delete(o);
787  }
788
789  return o;
790 }
791
792 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
793
794 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
795  lt_map_delete(o);
796
797  return lt_old_ck_padsv(aTHX_ o);
798 }
799
800 /* ... Our peephole optimizer .............................................. */
801
802 #if LT_HAS_PERL(5, 17, 6)
803
804 STATIC int lt_maybe_padrange_setup(pTHX_ OP *o, const OP *start) {
805 #define lt_maybe_padrange_setup(O, S) lt_maybe_padrange_setup(aTHX_ (O), (S))
806  PADOFFSET i, count;
807  const OP *p;
808
809  count = o->op_private & OPpPADRANGE_COUNTMASK;
810
811  for (i = 0, p = start; i < count && p; ++i, p = p->op_next) {
812   if (p->op_type == OP_PADSV) {
813    /* In a padrange sequence, either all lexicals are typed, or none are.
814     * Thus we can stop at the first padsv op. However, note that these
815     * lexicals can need to call different methods in different packages. */
816    LT_LOCK(&lt_op_map_mutex);
817    if (ptable_fetch(lt_op_padxv_map, p)) {
818     LT_UNLOCK(&lt_op_map_mutex);
819     lt_padrange_map_store(o, start, o->op_ppaddr);
820     o->op_ppaddr = lt_pp_padrange;
821    } else {
822     LT_UNLOCK(&lt_op_map_mutex);
823    }
824    return 1;
825   }
826  }
827
828  return 0;
829 }
830
831 #endif
832
833 STATIC peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
834
835 STATIC void lt_peep_rec(pTHX_ OP *o, ptable *seen) {
836 #define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
837  for (; o; o = o->op_next) {
838   if (ptable_fetch(seen, o))
839    break;
840   ptable_seen_store(seen, o, o);
841
842   switch (o->op_type) {
843    case OP_PADSV:
844     if (o->op_ppaddr != lt_pp_padsv && o->op_private & OPpLVAL_INTRO) {
845      lt_op_padxv_info *oi;
846      LT_LOCK(&lt_op_map_mutex);
847      oi = ptable_fetch(lt_op_padxv_map, o);
848      if (oi) {
849       oi->old_pp   = o->op_ppaddr;
850       o->op_ppaddr = lt_pp_padsv;
851      }
852      LT_UNLOCK(&lt_op_map_mutex);
853     }
854     break;
855 #if LT_HAS_PERL(5, 17, 6)
856    case OP_PADRANGE:
857     /* We deal with special padrange ops later, in the aassign op they belong
858      * to. */
859     if (o->op_ppaddr != lt_pp_padrange && o->op_private & OPpLVAL_INTRO
860                                        && !(o->op_flags & OPf_SPECIAL)) {
861      /* A padrange op is guaranteed to have previously been a pushmark.
862       * Moreover, for non-special padrange ops (i.e. that aren't for
863       * my (...) = @_), the first original padxv is its sibling or nephew.
864       */
865      OP *kid = OP_SIBLING(o);
866      if (kid->op_type == OP_NULL && kid->op_flags & OPf_KIDS) {
867       kid = kUNOP->op_first;
868       if (kid->op_type == OP_NULL)
869        kid = OP_SIBLING(kid);
870      }
871      lt_maybe_padrange_setup(o, kid);
872     }
873     break;
874    case OP_AASSIGN: {
875     OP *op;
876     if (cBINOPo->op_first && cBINOPo->op_first->op_flags & OPf_KIDS
877                           && (op = cUNOPx(cBINOPo->op_first)->op_first)
878                           && op->op_type == OP_PADRANGE
879                           && op->op_ppaddr != lt_pp_padrange
880                           && op->op_private & OPpLVAL_INTRO
881                           && op->op_flags & OPf_SPECIAL) {
882      const OP *start = cUNOPx(cBINOPo->op_last)->op_first;
883      if (start->op_type == OP_PUSHMARK)
884       start = OP_SIBLING(start);
885      lt_maybe_padrange_setup(op, start);
886     }
887     break;
888    }
889 #endif
890 #if !LT_HAS_RPEEP
891    case OP_MAPWHILE:
892    case OP_GREPWHILE:
893    case OP_AND:
894    case OP_OR:
895    case OP_ANDASSIGN:
896    case OP_ORASSIGN:
897    case OP_COND_EXPR:
898    case OP_RANGE:
899 # if LT_HAS_PERL(5, 10, 0)
900    case OP_ONCE:
901    case OP_DOR:
902    case OP_DORASSIGN:
903 # endif
904     lt_peep_rec(cLOGOPo->op_other);
905     break;
906    case OP_ENTERLOOP:
907    case OP_ENTERITER:
908     lt_peep_rec(cLOOPo->op_redoop);
909     lt_peep_rec(cLOOPo->op_nextop);
910     lt_peep_rec(cLOOPo->op_lastop);
911     break;
912 # if LT_HAS_PERL(5, 9, 5)
913    case OP_SUBST:
914     lt_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
915     break;
916 # else
917    case OP_QR:
918    case OP_MATCH:
919    case OP_SUBST:
920     lt_peep_rec(cPMOPo->op_pmreplstart);
921     break;
922 # endif
923 #endif /* !LT_HAS_RPEEP */
924    default:
925     break;
926   }
927  }
928 }
929
930 STATIC void lt_peep(pTHX_ OP *o) {
931  dMY_CXT;
932  ptable *seen = MY_CXT.seen;
933
934  lt_old_peep(aTHX_ o);
935
936  if (seen) {
937   ptable_seen_clear(seen);
938   lt_peep_rec(o);
939   ptable_seen_clear(seen);
940  }
941 }
942
943 /* --- Interpreter setup/teardown ------------------------------------------ */
944
945
946 STATIC U32 lt_initialized = 0;
947
948 STATIC void lt_teardown(pTHX_ void *root) {
949  if (!lt_initialized)
950   return;
951
952 #if LT_MULTIPLICITY
953  if (aTHX != root)
954   return;
955 #endif
956
957  {
958   dMY_CXT;
959 #if LT_THREADSAFE
960   ptable_hints_free(MY_CXT.tbl);
961   MY_CXT.tbl          = NULL;
962 #endif
963   ptable_seen_free(MY_CXT.seen);
964   MY_CXT.seen         = NULL;
965   SvREFCNT_dec(MY_CXT.default_meth);
966   MY_CXT.default_meth = NULL;
967  }
968
969  lt_ck_restore(OP_PADANY, &lt_old_ck_padany);
970  lt_ck_restore(OP_PADSV,  &lt_old_ck_padsv);
971
972 #if LT_HAS_RPEEP
973  PL_rpeepp   = lt_old_peep;
974 #else
975  PL_peepp    = lt_old_peep;
976 #endif
977  lt_old_peep = 0;
978
979  lt_initialized = 0;
980 }
981
982 STATIC void lt_setup(pTHX) {
983 #define lt_setup() lt_setup(aTHX)
984  if (lt_initialized)
985   return;
986
987  {
988   MY_CXT_INIT;
989 #if LT_THREADSAFE
990   MY_CXT.tbl          = ptable_new();
991   MY_CXT.owner        = aTHX;
992 #endif
993   MY_CXT.seen         = ptable_new();
994   MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
995   SvREADONLY_on(MY_CXT.default_meth);
996  }
997
998  lt_ck_replace(OP_PADANY, lt_ck_padany, &lt_old_ck_padany);
999  lt_ck_replace(OP_PADSV,  lt_ck_padsv,  &lt_old_ck_padsv);
1000
1001 #if LT_HAS_RPEEP
1002  lt_old_peep = PL_rpeepp;
1003  PL_rpeepp   = lt_peep;
1004 #else
1005  lt_old_peep = PL_peepp;
1006  PL_peepp    = lt_peep;
1007 #endif
1008
1009 #if LT_MULTIPLICITY
1010  call_atexit(lt_teardown, aTHX);
1011 #else
1012  call_atexit(lt_teardown, NULL);
1013 #endif
1014
1015  lt_initialized = 1;
1016 }
1017
1018 STATIC U32 lt_booted = 0;
1019
1020 /* --- XS ------------------------------------------------------------------ */
1021
1022 MODULE = Lexical::Types      PACKAGE = Lexical::Types
1023
1024 PROTOTYPES: ENABLE
1025
1026 BOOT:
1027 {
1028  if (!lt_booted++) {
1029   HV *stash;
1030
1031   lt_op_padxv_map    = ptable_new();
1032 #if LT_HAS_PERL(5, 17, 6)
1033   lt_op_padrange_map = ptable_new();
1034 #endif
1035 #ifdef USE_ITHREADS
1036   MUTEX_INIT(&lt_op_map_mutex);
1037 #endif
1038
1039   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
1040
1041   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1042   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
1043   newCONSTSUB(stash, "LT_FORKSAFE",   newSVuv(LT_FORKSAFE));
1044  }
1045
1046  lt_setup();
1047 }
1048
1049 #if LT_THREADSAFE
1050
1051 void
1052 CLONE(...)
1053 PROTOTYPE: DISABLE
1054 PREINIT:
1055  ptable *t;
1056  ptable *s;
1057  SV     *cloned_default_meth;
1058  GV     *gv;
1059 PPCODE:
1060  {
1061   {
1062    lt_ptable_clone_ud ud;
1063    dMY_CXT;
1064
1065    t = ptable_new();
1066    lt_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1067    ptable_walk(MY_CXT.tbl, lt_ptable_clone, &ud);
1068    cloned_default_meth = lt_dup_inc(MY_CXT.default_meth, &ud);
1069    lt_ptable_clone_ud_deinit(ud);
1070   }
1071   s = ptable_new();
1072  }
1073  {
1074   MY_CXT_CLONE;
1075   MY_CXT.tbl          = t;
1076   MY_CXT.owner        = aTHX;
1077   MY_CXT.seen         = s;
1078   MY_CXT.default_meth = cloned_default_meth;
1079  }
1080  gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1081  if (gv) {
1082   CV *cv = GvCV(gv);
1083   if (!PL_endav)
1084    PL_endav = newAV();
1085   SvREFCNT_inc(cv);
1086   if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1087    SvREFCNT_dec(cv);
1088   sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &lt_endav_vtbl, NULL, 0);
1089  }
1090  XSRETURN(0);
1091
1092 void
1093 _THREAD_CLEANUP(...)
1094 PROTOTYPE: DISABLE
1095 PPCODE:
1096  lt_thread_cleanup(aTHX_ NULL);
1097  XSRETURN(0);
1098
1099 #endif
1100
1101 SV *
1102 _tag(SV *value)
1103 PROTOTYPE: $
1104 CODE:
1105  RETVAL = lt_tag(value);
1106 OUTPUT:
1107  RETVAL