]> git.vpit.fr Git - perl/modules/Lexical-Types.git/blob - Types.xs
752e3f43d27aab4f5b1de8a8c59d1e70ab91f220
[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 STATIC void lt_thread_cleanup(pTHX_ void *ud) {
263  dMY_CXT;
264
265  ptable_hints_free(MY_CXT.tbl);
266  MY_CXT.tbl          = NULL;
267  ptable_seen_free(MY_CXT.seen);
268  MY_CXT.seen         = NULL;
269  SvREFCNT_dec(MY_CXT.default_meth);
270  MY_CXT.default_meth = NULL;
271 }
272
273 STATIC int lt_endav_free(pTHX_ SV *sv, MAGIC *mg) {
274  SAVEDESTRUCTOR_X(lt_thread_cleanup, NULL);
275
276  return 0;
277 }
278
279 STATIC MGVTBL lt_endav_vtbl = {
280  0,
281  0,
282  0,
283  0,
284  lt_endav_free
285 #if MGf_COPY
286  , 0
287 #endif
288 #if MGf_DUP
289  , 0
290 #endif
291 #if MGf_LOCAL
292  , 0
293 #endif
294 };
295
296 #endif /* LT_THREADSAFE */
297
298 /* ... Hint tags ........................................................... */
299
300 #if LT_WORKAROUND_REQUIRE_PROPAGATION
301
302 STATIC IV lt_require_tag(pTHX) {
303 #define lt_require_tag() lt_require_tag(aTHX)
304  const CV *cv, *outside;
305
306  cv = PL_compcv;
307
308  if (!cv) {
309   /* If for some reason the pragma is operational at run-time, try to discover
310    * the current cv in use. */
311   const PERL_SI *si;
312
313   for (si = PL_curstackinfo; si; si = si->si_prev) {
314    I32 cxix;
315
316    for (cxix = si->si_cxix; cxix >= 0; --cxix) {
317     const PERL_CONTEXT *cx = si->si_cxstack + cxix;
318
319     switch (CxTYPE(cx)) {
320      case CXt_SUB:
321      case CXt_FORMAT:
322       /* The propagation workaround is only needed up to 5.10.0 and at that
323        * time format and sub contexts were still identical. And even later the
324        * cv members offsets should have been kept the same. */
325       cv = cx->blk_sub.cv;
326       goto get_enclosing_cv;
327      case CXt_EVAL:
328       cv = cx->blk_eval.cv;
329       goto get_enclosing_cv;
330      default:
331       break;
332     }
333    }
334   }
335
336   cv = PL_main_cv;
337  }
338
339 get_enclosing_cv:
340  for (outside = CvOUTSIDE(cv); outside; outside = CvOUTSIDE(cv))
341   cv = outside;
342
343  return PTR2IV(cv);
344 }
345
346 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
347
348 STATIC SV *lt_tag(pTHX_ SV *value) {
349 #define lt_tag(V) lt_tag(aTHX_ (V))
350  lt_hint_t *h;
351  SV *code = NULL;
352
353  if (SvROK(value)) {
354   value = SvRV(value);
355   if (SvTYPE(value) >= SVt_PVCV) {
356    code = value;
357    SvREFCNT_inc_simple_void_NN(code);
358   }
359  }
360
361 #if LT_HINT_STRUCT
362  h = PerlMemShared_malloc(sizeof *h);
363  h->code        = code;
364 # if LT_WORKAROUND_REQUIRE_PROPAGATION
365  h->require_tag = lt_require_tag();
366 # endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
367 #else  /*  LT_HINT_STRUCT */
368  h = code;
369 #endif /* !LT_HINT_STRUCT */
370
371 #if LT_THREADSAFE
372  {
373   dMY_CXT;
374   /* We only need for the key to be an unique tag for looking up the value later
375    * Allocated memory provides convenient unique identifiers, so that's why we
376    * use the hint as the key itself. */
377   ptable_hints_store(MY_CXT.tbl, h, h);
378  }
379 #endif /* LT_THREADSAFE */
380
381  return newSViv(PTR2IV(h));
382 }
383
384 STATIC SV *lt_detag(pTHX_ const SV *hint) {
385 #define lt_detag(H) lt_detag(aTHX_ (H))
386  lt_hint_t *h;
387 #if LT_THREADSAFE
388  dMY_CXT;
389 #endif
390
391  if (!(hint && SvIOK(hint)))
392   return NULL;
393
394  h = INT2PTR(lt_hint_t *, SvIVX(hint));
395 #if LT_THREADSAFE
396  h = ptable_fetch(MY_CXT.tbl, h);
397 #endif /* LT_THREADSAFE */
398 #if LT_WORKAROUND_REQUIRE_PROPAGATION
399  if (lt_require_tag() != h->require_tag)
400   return NULL;
401 #endif /* LT_WORKAROUND_REQUIRE_PROPAGATION */
402
403  return LT_HINT_CODE(h);
404 }
405
406 STATIC U32 lt_hash = 0;
407
408 STATIC SV *lt_hint(pTHX) {
409 #define lt_hint() lt_hint(aTHX)
410  SV *hint;
411 #ifdef cop_hints_fetch_pvn
412  hint = cop_hints_fetch_pvn(PL_curcop, __PACKAGE__, __PACKAGE_LEN__, lt_hash,0);
413 #elif LT_HAS_PERL(5, 9, 5)
414  hint = Perl_refcounted_he_fetch(aTHX_ PL_curcop->cop_hints_hash,
415                                        NULL,
416                                        __PACKAGE__, __PACKAGE_LEN__,
417                                        0,
418                                        lt_hash);
419 #else
420  SV **val = hv_fetch(GvHV(PL_hintgv), __PACKAGE__, __PACKAGE_LEN__, 0);
421  if (!val)
422   return 0;
423  hint = *val;
424 #endif
425  return lt_detag(hint);
426 }
427
428 /* ... op => info map ...................................................... */
429
430 #define PTABLE_NAME        ptable_map
431 #define PTABLE_VAL_FREE(V) PerlMemShared_free(V)
432
433 #include "ptable.h"
434
435 /* PerlMemShared_free() needs the [ap]PTBLMS_? default values */
436 #define ptable_map_store(T, K, V) ptable_map_store(aPTBLMS_ (T), (K), (V))
437 #define ptable_map_delete(T, K)   ptable_map_delete(aPTBLMS_ (T), (K))
438
439 #ifdef USE_ITHREADS
440
441 STATIC perl_mutex lt_op_map_mutex;
442
443 #define LT_LOCK(M)   MUTEX_LOCK(M)
444 #define LT_UNLOCK(M) MUTEX_UNLOCK(M)
445
446 #else /* USE_ITHREADS */
447
448 #define LT_LOCK(M)
449 #define LT_UNLOCK(M)
450
451 #endif /* !USE_ITHREADS */
452
453 STATIC ptable *lt_op_padxv_map = NULL;
454
455 typedef struct {
456  OP *(*old_pp)(pTHX);
457 #ifdef MULTIPLICITY
458  STRLEN buf_size, orig_pkg_len, type_pkg_len, type_meth_len;
459  char *buf;
460 #else /* MULTIPLICITY */
461  SV *orig_pkg;
462  SV *type_pkg;
463  SV *type_meth;
464 #endif /* !MULTIPLICITY */
465 } lt_op_padxv_info;
466
467 STATIC void lt_op_padxv_info_call(pTHX_ const lt_op_padxv_info *oi, SV *sv) {
468 #define lt_op_padxv_info_call(O, S) lt_op_padxv_info_call(aTHX_ (O), (S))
469  SV *orig_pkg, *type_pkg, *type_meth;
470  int items;
471  dSP;
472
473  ENTER;
474  SAVETMPS;
475
476 #ifdef MULTIPLICITY
477  {
478   STRLEN op_len = oi->orig_pkg_len, tp_len = oi->type_pkg_len;
479   char *buf = oi->buf;
480   orig_pkg  = sv_2mortal(newSVpvn(buf, op_len));
481   SvREADONLY_on(orig_pkg);
482   buf      += op_len;
483   type_pkg  = sv_2mortal(newSVpvn(buf, tp_len));
484   SvREADONLY_on(type_pkg);
485   buf      += tp_len;
486   type_meth = sv_2mortal(newSVpvn(buf, oi->type_meth_len));
487   SvREADONLY_on(type_meth);
488  }
489 #else /* MULTIPLICITY */
490  orig_pkg  = oi->orig_pkg;
491  type_pkg  = oi->type_pkg;
492  type_meth = oi->type_meth;
493 #endif /* !MULTIPLICITY */
494
495  PUSHMARK(SP);
496  EXTEND(SP, 3);
497  PUSHs(type_pkg);
498  PUSHs(sv);
499  PUSHs(orig_pkg);
500  PUTBACK;
501
502  items = call_sv(type_meth, G_ARRAY | G_METHOD);
503
504  SPAGAIN;
505  switch (items) {
506   case 0:
507    break;
508   case 1:
509    sv_setsv(sv, POPs);
510    break;
511   default:
512    croak("Typed scalar initializer method should return zero or one scalar, but got %d", items);
513  }
514  PUTBACK;
515
516  FREETMPS;
517  LEAVE;
518
519  return;
520 }
521
522 STATIC void lt_padxv_map_store(pTHX_ const OP *o, SV *orig_pkg, SV *type_pkg, SV *type_meth, OP *(*old_pp)(pTHX)) {
523 #define lt_padxv_map_store(O, OP, TP, TM, PP) lt_padxv_map_store(aTHX_ (O), (OP), (TP), (TM), (PP))
524  lt_op_padxv_info *oi;
525
526  LT_LOCK(&lt_op_map_mutex);
527
528  if (!(oi = ptable_fetch(lt_op_padxv_map, o))) {
529   oi = PerlMemShared_malloc(sizeof *oi);
530   ptable_map_store(lt_op_padxv_map, o, oi);
531 #ifdef MULTIPLICITY
532   oi->buf      = NULL;
533   oi->buf_size = 0;
534 #else /* MULTIPLICITY */
535  } else {
536   SvREFCNT_dec(oi->orig_pkg);
537   SvREFCNT_dec(oi->type_pkg);
538   SvREFCNT_dec(oi->type_meth);
539 #endif /* !MULTIPLICITY */
540  }
541
542 #ifdef MULTIPLICITY
543  {
544   STRLEN op_len       = SvCUR(orig_pkg);
545   STRLEN tp_len       = SvCUR(type_pkg);
546   STRLEN tm_len       = SvCUR(type_meth);
547   STRLEN new_buf_size = op_len + tp_len + tm_len;
548   char *buf;
549   if (new_buf_size > oi->buf_size) {
550    PerlMemShared_free(oi->buf);
551    oi->buf      = PerlMemShared_malloc(new_buf_size);
552    oi->buf_size = new_buf_size;
553   }
554   buf  = oi->buf;
555   Copy(SvPVX(orig_pkg),  buf, op_len, char);
556   buf += op_len;
557   Copy(SvPVX(type_pkg),  buf, tp_len, char);
558   buf += tp_len;
559   Copy(SvPVX(type_meth), buf, tm_len, char);
560   oi->orig_pkg_len  = op_len;
561   oi->type_pkg_len  = tp_len;
562   oi->type_meth_len = tm_len;
563   SvREFCNT_dec(orig_pkg);
564   SvREFCNT_dec(type_pkg);
565   SvREFCNT_dec(type_meth);
566  }
567 #else /* MULTIPLICITY */
568  oi->orig_pkg  = orig_pkg;
569  oi->type_pkg  = type_pkg;
570  oi->type_meth = type_meth;
571 #endif /* !MULTIPLICITY */
572
573  oi->old_pp = old_pp;
574
575  LT_UNLOCK(&lt_op_map_mutex);
576 }
577
578 STATIC const lt_op_padxv_info *lt_padxv_map_fetch(const OP *o, lt_op_padxv_info *oi) {
579  const lt_op_padxv_info *val;
580
581  LT_LOCK(&lt_op_map_mutex);
582
583  val = ptable_fetch(lt_op_padxv_map, o);
584  if (val) {
585   *oi = *val;
586   val = oi;
587  }
588
589  LT_UNLOCK(&lt_op_map_mutex);
590
591  return val;
592 }
593
594 #if LT_HAS_PERL(5, 17, 6)
595
596 STATIC ptable *lt_op_padrange_map = NULL;
597
598 typedef struct {
599  OP *(*old_pp)(pTHX);
600  const OP *padxv_start;
601 } lt_op_padrange_info;
602
603 STATIC void lt_padrange_map_store(pTHX_ const OP *o, const OP *s, OP *(*old_pp)(pTHX)) {
604 #define lt_padrange_map_store(O, S, PP) lt_padrange_map_store(aTHX_ (O), (S), (PP))
605  lt_op_padrange_info *oi;
606
607  LT_LOCK(&lt_op_map_mutex);
608
609  if (!(oi = ptable_fetch(lt_op_padrange_map, o))) {
610   oi = PerlMemShared_malloc(sizeof *oi);
611   ptable_map_store(lt_op_padrange_map, o, oi);
612  }
613
614  oi->old_pp      = old_pp;
615  oi->padxv_start = s;
616
617  LT_UNLOCK(&lt_op_map_mutex);
618 }
619
620 STATIC const lt_op_padrange_info *lt_padrange_map_fetch(const OP *o, lt_op_padrange_info *oi) {
621  const lt_op_padrange_info *val;
622
623  LT_LOCK(&lt_op_map_mutex);
624
625  val = ptable_fetch(lt_op_padrange_map, o);
626  if (val) {
627   *oi = *val;
628   val = oi;
629  }
630
631  LT_UNLOCK(&lt_op_map_mutex);
632
633  return val;
634 }
635
636 #endif
637
638 STATIC void lt_map_delete(pTHX_ const OP *o) {
639 #define lt_map_delete(O) lt_map_delete(aTHX_ (O))
640  LT_LOCK(&lt_op_map_mutex);
641
642  ptable_map_delete(lt_op_padxv_map,    o);
643 #if LT_HAS_PERL(5, 17, 6)
644  ptable_map_delete(lt_op_padrange_map, o);
645 #endif
646
647  LT_UNLOCK(&lt_op_map_mutex);
648 }
649
650 /* --- Hooks --------------------------------------------------------------- */
651
652 /* ... Our pp_padsv ........................................................ */
653
654 STATIC OP *lt_pp_padsv(pTHX) {
655  lt_op_padxv_info oi;
656
657  if (lt_padxv_map_fetch(PL_op, &oi)) {
658   dTARGET;
659   lt_op_padxv_info_call(&oi, TARG);
660   return oi.old_pp(aTHX);
661  }
662
663  return PL_op->op_ppaddr(aTHX);
664 }
665
666 /* ... Our pp_padrange (on perl 5.17.6 and above) .......................... */
667
668 #if LT_HAS_PERL(5, 17, 6)
669
670 STATIC OP *lt_pp_padrange(pTHX) {
671  lt_op_padrange_info roi;
672
673  if (lt_padrange_map_fetch(PL_op, &roi)) {
674   PADOFFSET i, base, count;
675   const OP *p;
676
677   base  = PL_op->op_targ;
678   count = PL_op->op_private & OPpPADRANGE_COUNTMASK;
679
680   for (i = 0, p = roi.padxv_start; i < count && p; ++i, p = p->op_sibling) {
681    lt_op_padxv_info oi;
682    if (p->op_type == OP_PADSV && lt_padxv_map_fetch(p, &oi))
683     lt_op_padxv_info_call(&oi, PAD_SV(base + i));
684   }
685
686   return roi.old_pp(aTHX);
687  }
688
689  return PL_op->op_ppaddr(aTHX);
690 }
691
692 #endif
693
694 /* ... Our ck_pad{any,sv} .................................................. */
695
696 /* Sadly, the padsv OPs we are interested in don't trigger the padsv check
697  * function, but are instead manually mutated from a padany. So we store
698  * the op entry in the op map in the padany check function, and we set their
699  * op_ppaddr member in our peephole optimizer replacement below. */
700
701 STATIC OP *(*lt_old_ck_padany)(pTHX_ OP *) = 0;
702
703 STATIC OP *lt_ck_padany(pTHX_ OP *o) {
704  HV *stash;
705  SV *code;
706
707  o = lt_old_ck_padany(aTHX_ o);
708
709  stash = PL_in_my_stash;
710  if (stash && (code = lt_hint())) {
711   dMY_CXT;
712   SV *orig_pkg  = newSVpvn(HvNAME_get(stash), HvNAMELEN_get(stash));
713   SV *orig_meth = MY_CXT.default_meth;
714   SV *type_pkg  = NULL;
715   SV *type_meth = NULL;
716   int items;
717
718   dSP;
719
720   SvREADONLY_on(orig_pkg);
721
722   ENTER;
723   SAVETMPS;
724
725   PUSHMARK(SP);
726   EXTEND(SP, 2);
727   PUSHs(orig_pkg);
728   PUSHs(orig_meth);
729   PUTBACK;
730
731   items = call_sv(code, G_ARRAY);
732
733   SPAGAIN;
734   if (items > 2)
735    croak(__PACKAGE__ " mangler should return zero, one or two scalars, but got %d", items);
736   if (items == 0) {
737    SvREFCNT_dec(orig_pkg);
738    FREETMPS;
739    LEAVE;
740    goto skip;
741   } else {
742    SV *rsv;
743    if (items > 1) {
744     rsv = POPs;
745     if (SvOK(rsv)) {
746      type_meth = newSVsv(rsv);
747      SvREADONLY_on(type_meth);
748     }
749    }
750    rsv = POPs;
751    if (SvOK(rsv)) {
752     type_pkg = newSVsv(rsv);
753     SvREADONLY_on(type_pkg);
754    }
755   }
756   PUTBACK;
757
758   FREETMPS;
759   LEAVE;
760
761   if (!type_pkg) {
762    type_pkg = orig_pkg;
763    SvREFCNT_inc_simple_void_NN(orig_pkg);
764   }
765
766   if (!type_meth) {
767    type_meth = orig_meth;
768    SvREFCNT_inc_simple_void_NN(orig_meth);
769   }
770
771   lt_padxv_map_store(o, orig_pkg, type_pkg, type_meth, o->op_ppaddr);
772  } else {
773 skip:
774   lt_map_delete(o);
775  }
776
777  return o;
778 }
779
780 STATIC OP *(*lt_old_ck_padsv)(pTHX_ OP *) = 0;
781
782 STATIC OP *lt_ck_padsv(pTHX_ OP *o) {
783  lt_map_delete(o);
784
785  return lt_old_ck_padsv(aTHX_ o);
786 }
787
788 /* ... Our peephole optimizer .............................................. */
789
790 #if LT_HAS_PERL(5, 17, 6)
791
792 STATIC int lt_maybe_padrange_setup(pTHX_ OP *o, const OP *start) {
793 #define lt_maybe_padrange_setup(O, S) lt_maybe_padrange_setup(aTHX_ (O), (S))
794  PADOFFSET i, count;
795  const OP *p;
796
797  count = o->op_private & OPpPADRANGE_COUNTMASK;
798
799  for (i = 0, p = start; i < count && p; ++i, p = p->op_sibling) {
800   if (p->op_type == OP_PADSV) {
801    /* In a padrange sequence, either all lexicals are typed, or none are.
802     * Thus we can stop at the first padsv op. However, note that these
803     * lexicals can need to call different methods in different packages. */
804    LT_LOCK(&lt_op_map_mutex);
805    if (ptable_fetch(lt_op_padxv_map, p)) {
806     LT_UNLOCK(&lt_op_map_mutex);
807     lt_padrange_map_store(o, start, o->op_ppaddr);
808     o->op_ppaddr = lt_pp_padrange;
809    } else {
810     LT_UNLOCK(&lt_op_map_mutex);
811    }
812    return 1;
813   }
814  }
815
816  return 0;
817 }
818
819 #endif
820
821 STATIC peep_t lt_old_peep = 0; /* This is actually the rpeep past 5.13.5 */
822
823 STATIC void lt_peep_rec(pTHX_ OP *o, ptable *seen) {
824 #define lt_peep_rec(O) lt_peep_rec(aTHX_ (O), seen)
825  for (; o; o = o->op_next) {
826   if (ptable_fetch(seen, o))
827    break;
828   ptable_seen_store(seen, o, o);
829
830   switch (o->op_type) {
831    case OP_PADSV:
832     if (o->op_ppaddr != lt_pp_padsv && o->op_private & OPpLVAL_INTRO) {
833      lt_op_padxv_info *oi;
834      LT_LOCK(&lt_op_map_mutex);
835      oi = ptable_fetch(lt_op_padxv_map, o);
836      if (oi) {
837       oi->old_pp   = o->op_ppaddr;
838       o->op_ppaddr = lt_pp_padsv;
839      }
840      LT_UNLOCK(&lt_op_map_mutex);
841     }
842     break;
843 #if LT_HAS_PERL(5, 17, 6)
844    case OP_PADRANGE:
845     /* We deal with special padrange ops later, in the aassign op they belong
846      * to. */
847     if (o->op_ppaddr != lt_pp_padrange && o->op_private & OPpLVAL_INTRO
848                                        && !(o->op_flags & OPf_SPECIAL)) {
849      /* A padrange op is guaranteed to have previously been a pushmark.
850       * Moreover, for non-special padrange ops (i.e. that aren't for
851       * my (...) = @_), the original padxv ops are its siblings. */
852      lt_maybe_padrange_setup(o, o->op_sibling);
853     }
854     break;
855    case OP_AASSIGN: {
856     OP *op;
857     if (cBINOPo->op_first && cBINOPo->op_first->op_flags & OPf_KIDS
858                           && (op = cUNOPx(cBINOPo->op_first)->op_first)
859                           && op->op_type == OP_PADRANGE
860                           && op->op_ppaddr != lt_pp_padrange
861                           && op->op_private & OPpLVAL_INTRO
862                           && op->op_flags & OPf_SPECIAL) {
863      const OP *start = cUNOPx(cBINOPo->op_last)->op_first;
864      if (start->op_type == OP_PUSHMARK)
865       start = start->op_sibling;
866      lt_maybe_padrange_setup(op, start);
867     }
868     break;
869    }
870 #endif
871 #if !LT_HAS_RPEEP
872    case OP_MAPWHILE:
873    case OP_GREPWHILE:
874    case OP_AND:
875    case OP_OR:
876    case OP_ANDASSIGN:
877    case OP_ORASSIGN:
878    case OP_COND_EXPR:
879    case OP_RANGE:
880 # if LT_HAS_PERL(5, 10, 0)
881    case OP_ONCE:
882    case OP_DOR:
883    case OP_DORASSIGN:
884 # endif
885     lt_peep_rec(cLOGOPo->op_other);
886     break;
887    case OP_ENTERLOOP:
888    case OP_ENTERITER:
889     lt_peep_rec(cLOOPo->op_redoop);
890     lt_peep_rec(cLOOPo->op_nextop);
891     lt_peep_rec(cLOOPo->op_lastop);
892     break;
893 # if LT_HAS_PERL(5, 9, 5)
894    case OP_SUBST:
895     lt_peep_rec(cPMOPo->op_pmstashstartu.op_pmreplstart);
896     break;
897 # else
898    case OP_QR:
899    case OP_MATCH:
900    case OP_SUBST:
901     lt_peep_rec(cPMOPo->op_pmreplstart);
902     break;
903 # endif
904 #endif /* !LT_HAS_RPEEP */
905    default:
906     break;
907   }
908  }
909 }
910
911 STATIC void lt_peep(pTHX_ OP *o) {
912  dMY_CXT;
913  ptable *seen = MY_CXT.seen;
914
915  lt_old_peep(aTHX_ o);
916
917  ptable_seen_clear(seen);
918  lt_peep_rec(o);
919  ptable_seen_clear(seen);
920 }
921
922 /* --- Interpreter setup/teardown ------------------------------------------ */
923
924
925 STATIC U32 lt_initialized = 0;
926
927 STATIC void lt_teardown(pTHX_ void *root) {
928  if (!lt_initialized)
929   return;
930
931 #if LT_MULTIPLICITY
932  if (aTHX != root)
933   return;
934 #endif
935
936  {
937   dMY_CXT;
938 #if LT_THREADSAFE
939   ptable_hints_free(MY_CXT.tbl);
940   MY_CXT.tbl          = NULL;
941 #endif
942   ptable_seen_free(MY_CXT.seen);
943   MY_CXT.seen         = NULL;
944   SvREFCNT_dec(MY_CXT.default_meth);
945   MY_CXT.default_meth = NULL;
946  }
947
948  lt_ck_restore(OP_PADANY, &lt_old_ck_padany);
949  lt_ck_restore(OP_PADSV,  &lt_old_ck_padsv);
950
951 #if LT_HAS_RPEEP
952  PL_rpeepp   = lt_old_peep;
953 #else
954  PL_peepp    = lt_old_peep;
955 #endif
956  lt_old_peep = 0;
957
958  lt_initialized = 0;
959 }
960
961 STATIC void lt_setup(pTHX) {
962 #define lt_setup() lt_setup(aTHX)
963  if (lt_initialized)
964   return;
965
966  {
967   MY_CXT_INIT;
968 #if LT_THREADSAFE
969   MY_CXT.tbl          = ptable_new();
970   MY_CXT.owner        = aTHX;
971 #endif
972   MY_CXT.seen         = ptable_new();
973   MY_CXT.default_meth = newSVpvn("TYPEDSCALAR", 11);
974   SvREADONLY_on(MY_CXT.default_meth);
975  }
976
977  lt_ck_replace(OP_PADANY, lt_ck_padany, &lt_old_ck_padany);
978  lt_ck_replace(OP_PADSV,  lt_ck_padsv,  &lt_old_ck_padsv);
979
980 #if LT_HAS_RPEEP
981  lt_old_peep = PL_rpeepp;
982  PL_rpeepp   = lt_peep;
983 #else
984  lt_old_peep = PL_peepp;
985  PL_peepp    = lt_peep;
986 #endif
987
988 #if LT_MULTIPLICITY
989  call_atexit(lt_teardown, aTHX);
990 #else
991  call_atexit(lt_teardown, NULL);
992 #endif
993
994  lt_initialized = 1;
995 }
996
997 STATIC U32 lt_booted = 0;
998
999 /* --- XS ------------------------------------------------------------------ */
1000
1001 MODULE = Lexical::Types      PACKAGE = Lexical::Types
1002
1003 PROTOTYPES: ENABLE
1004
1005 BOOT:
1006 {
1007  if (!lt_booted++) {
1008   HV *stash;
1009
1010   lt_op_padxv_map    = ptable_new();
1011 #if LT_HAS_PERL(5, 17, 6)
1012   lt_op_padrange_map = ptable_new();
1013 #endif
1014 #ifdef USE_ITHREADS
1015   MUTEX_INIT(&lt_op_map_mutex);
1016 #endif
1017
1018   PERL_HASH(lt_hash, __PACKAGE__, __PACKAGE_LEN__);
1019
1020   stash = gv_stashpvn(__PACKAGE__, __PACKAGE_LEN__, 1);
1021   newCONSTSUB(stash, "LT_THREADSAFE", newSVuv(LT_THREADSAFE));
1022   newCONSTSUB(stash, "LT_FORKSAFE",   newSVuv(LT_FORKSAFE));
1023  }
1024
1025  lt_setup();
1026 }
1027
1028 #if LT_THREADSAFE
1029
1030 void
1031 CLONE(...)
1032 PROTOTYPE: DISABLE
1033 PREINIT:
1034  ptable *t;
1035  ptable *s;
1036  SV     *cloned_default_meth;
1037  GV     *gv;
1038 PPCODE:
1039  {
1040   {
1041    lt_ptable_clone_ud ud;
1042    dMY_CXT;
1043
1044    t = ptable_new();
1045    lt_ptable_clone_ud_init(ud, t, MY_CXT.owner);
1046    ptable_walk(MY_CXT.tbl, lt_ptable_clone, &ud);
1047    cloned_default_meth = lt_dup_inc(MY_CXT.default_meth, &ud);
1048    lt_ptable_clone_ud_deinit(ud);
1049   }
1050   s = ptable_new();
1051  }
1052  {
1053   MY_CXT_CLONE;
1054   MY_CXT.tbl          = t;
1055   MY_CXT.owner        = aTHX;
1056   MY_CXT.seen         = s;
1057   MY_CXT.default_meth = cloned_default_meth;
1058  }
1059  gv = gv_fetchpv(__PACKAGE__ "::_THREAD_CLEANUP", 0, SVt_PVCV);
1060  if (gv) {
1061   CV *cv = GvCV(gv);
1062   if (!PL_endav)
1063    PL_endav = newAV();
1064   SvREFCNT_inc(cv);
1065   if (!av_store(PL_endav, av_len(PL_endav) + 1, (SV *) cv))
1066    SvREFCNT_dec(cv);
1067   sv_magicext((SV *) PL_endav, NULL, PERL_MAGIC_ext, &lt_endav_vtbl, NULL, 0);
1068  }
1069  XSRETURN(0);
1070
1071 void
1072 _THREAD_CLEANUP(...)
1073 PROTOTYPE: DISABLE
1074 PPCODE:
1075  lt_thread_cleanup(aTHX_ NULL);
1076  XSRETURN(0);
1077
1078 #endif
1079
1080 SV *
1081 _tag(SV *value)
1082 PROTOTYPE: $
1083 CODE:
1084  RETVAL = lt_tag(value);
1085 OUTPUT:
1086  RETVAL