Optimization for duck_type
[gitmo/Mouse.git] / xs-src / MouseTypeConstraints.xs
1 /*
2  *   full definition of built-in type constraints (ware in Moose::Util::TypeConstraints::OptimizedConstraints)
3  */
4
5 #include "mouse.h"
6
7 #if PERL_BCDVERSION >= 0x5008005
8 #define LooksLikeNumber(sv) looks_like_number(sv)
9 #else
10 #define LooksLikeNumber(sv) ( SvPOKp(sv) ? looks_like_number(sv) : SvNIOKp(sv) )
11 #endif
12
13 #ifndef SvRXOK
14 #define SvRXOK(sv) (SvROK(sv) && SvMAGICAL(SvRV(sv)) && mg_find(SvRV(sv), PERL_MAGIC_qr))
15 #endif
16
17 typedef int (*check_fptr_t)(pTHX_ SV* const data, SV* const sv);
18
19 int
20 mouse_tc_check(pTHX_ SV* const tc_code, SV* const sv) {
21     CV* const cv = (CV*)SvRV(tc_code);
22     assert(SvTYPE(cv) == SVt_PVCV);
23
24     if(CvXSUB(cv) == XS_Mouse_constraint_check){ /* built-in type constraints */
25         MAGIC* const mg = (MAGIC*)CvXSUBANY(cv).any_ptr;
26
27         assert(CvXSUBANY(cv).any_ptr != NULL);
28         assert(mg->mg_ptr            != NULL);
29
30         /* call the check function directly, skipping call_sv() */
31         return CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, sv);
32     }
33     else { /* custom */
34         int ok;
35         dSP;
36
37         ENTER;
38         SAVETMPS;
39
40         PUSHMARK(SP);
41         XPUSHs(sv);
42         PUTBACK;
43
44         call_sv(tc_code, G_SCALAR);
45
46         SPAGAIN;
47         ok = SvTRUEx(POPs);
48         PUTBACK;
49
50         FREETMPS;
51         LEAVE;
52
53         return ok;
54     }
55 }
56
57 /*
58     The following type check functions return an integer, not a bool, to keep them simple,
59     so if you assign these return value to bool variable, you must use "expr ? TRUE : FALSE".
60 */
61
62 int
63 mouse_tc_Any(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv PERL_UNUSED_DECL) {
64     assert(sv);
65     return TRUE;
66 }
67
68 int
69 mouse_tc_Bool(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
70     assert(sv);
71
72     if(SvTRUE(sv)){
73         if(SvIOKp(sv)){
74             return SvIVX(sv) == 1;
75         }
76         else if(SvNOKp(sv)){
77             return SvNVX(sv) == 1.0;
78         }
79         else if(SvPOKp(sv)){ /* "1" */
80             return SvCUR(sv) == 1 && SvPVX(sv)[0] == '1';
81         }
82         else{
83             return FALSE;
84         }
85     }
86     else{
87         /* any false value must be boolean */
88         return TRUE;
89     }
90 }
91
92 int
93 mouse_tc_Undef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
94     assert(sv);
95     return !SvOK(sv);
96 }
97
98 int
99 mouse_tc_Defined(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
100     assert(sv);
101     return SvOK(sv);
102 }
103
104 int
105 mouse_tc_Value(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
106     assert(sv);
107     return SvOK(sv) && !SvROK(sv);
108 }
109
110 int
111 mouse_tc_Num(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
112     assert(sv);
113     return LooksLikeNumber(sv);
114 }
115
116 int
117 mouse_tc_Int(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
118     assert(sv);
119     if(SvIOKp(sv)){
120         return TRUE;
121     }
122     else if(SvNOKp(sv)){
123         NV const nv = SvNVX(sv);
124         return nv > 0 ? (nv == (NV)(UV)nv) : (nv == (NV)(IV)nv);
125     }
126     else if(SvPOKp(sv)){
127         int const num_type = grok_number(SvPVX(sv), SvCUR(sv), NULL);
128         if(num_type){
129             return !(num_type & IS_NUMBER_NOT_INT);
130         }
131     }
132     return FALSE;
133 }
134
135 int
136 mouse_tc_Str(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
137     assert(sv);
138     return SvOK(sv) && !SvROK(sv) && !isGV(sv);
139 }
140
141 int
142 mouse_tc_ClassName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv){ 
143     assert(sv);
144     return is_class_loaded(sv);
145 }
146
147 int
148 mouse_tc_RoleName(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
149     assert(sv);
150     if(is_class_loaded(sv)){
151         int ok;
152
153         ENTER;
154         SAVETMPS;
155
156         ok =  is_an_instance_of("Mouse::Meta::Role", get_metaclass(sv));
157
158         FREETMPS;
159         LEAVE;
160
161         return ok;
162     }
163     return FALSE;
164 }
165
166 int
167 mouse_tc_Ref(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
168     assert(sv);
169     return SvROK(sv);
170 }
171
172 int
173 mouse_tc_ScalarRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
174     assert(sv);
175     return SvROK(sv) && !SvOBJECT(SvRV(sv)) && (SvTYPE(SvRV(sv)) <= SVt_PVLV && !isGV(SvRV(sv)));
176 }
177
178 int
179 mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
180     assert(sv);
181     return IsArrayRef(sv);
182 }
183
184 int
185 mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
186     assert(sv);
187     return IsHashRef(sv);
188 }
189
190 int
191 mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
192     assert(sv);
193     return IsCodeRef(sv);
194 }
195
196 int
197 mouse_tc_RegexpRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
198     assert(sv);
199     return SvRXOK(sv);
200 }
201
202 int
203 mouse_tc_GlobRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
204     assert(sv);
205     return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv));
206 }
207
208 int
209 mouse_tc_FileHandle(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
210     GV* gv;
211     assert(sv);
212
213     /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */
214
215     gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv);
216     if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){
217         IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv;
218
219         if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){
220             return TRUE;
221         }
222     }
223
224     return is_an_instance_of("IO::Handle", sv);
225 }
226
227 int
228 mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
229     assert(sv);
230     return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv);
231 }
232
233 /* Parameterized type constraints */
234
235 static int
236 mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
237     if(IsArrayRef(sv)){
238         AV* const av  = (AV*)SvRV(sv);
239         I32 const len = av_len(av) + 1;
240         I32 i;
241         for(i = 0; i < len; i++){
242             SV* const value = *av_fetch(av, i, TRUE);
243             SvGETMAGIC(value);
244             if(!mouse_tc_check(aTHX_ param, value)){
245                 return FALSE;
246             }
247         }
248         return TRUE;
249     }
250     return FALSE;
251 }
252
253 static int
254 mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
255     if(mouse_tc_HashRef(aTHX_ NULL, sv)){
256         HV* const hv  = (HV*)SvRV(sv);
257         HE* he;
258
259         hv_iterinit(hv);
260         while((he = hv_iternext(hv))){
261             SV* const value = hv_iterval(hv, he);
262             SvGETMAGIC(value);
263             if(!mouse_tc_check(aTHX_ param, value)){
264                 hv_iterinit(hv); /* reset */
265                 return FALSE;
266             }
267         }
268         return TRUE;
269     }
270     return FALSE;
271 }
272
273 static int
274 mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) {
275     if(SvOK(sv)){
276         return mouse_tc_check(aTHX_ param, sv);
277     }
278     return TRUE;
279 }
280
281 static int
282 mouse_types_union_check(pTHX_ AV* const types, SV* const sv) {
283     I32 const len = AvFILLp(types) + 1;
284     I32 i;
285
286     for(i = 0; i < len; i++){
287         if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
288             return TRUE;
289         }
290     }
291
292     return FALSE;
293 }
294
295 static int
296 mouse_types_check(pTHX_ AV* const types, SV* const sv) {
297     I32 const len = AvFILLp(types) + 1;
298     I32 i;
299
300     ENTER;
301     SAVE_DEFSV;
302     DEFSV_set(sv);
303
304     for(i = 0; i < len; i++){
305         if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
306             LEAVE;
307             return FALSE;
308         }
309     }
310
311     LEAVE;
312
313     return TRUE;
314 }
315
316 /*
317  *  This class_type generator is taken from Scalar::Util::Instance
318  */
319
320 #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
321 typedef struct sui_cxt{
322     GV* universal_isa;
323     GV* universal_can;
324 } my_cxt_t;
325 START_MY_CXT
326
327 #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj)
328 #define MG_klass_pv(mg)    ((mg)->mg_ptr)
329 #define MG_klass_len(mg)   ((mg)->mg_len)
330
331 static const char*
332 mouse_canonicalize_package_name(const char* name){
333
334     /* "::Foo" -> "Foo" */
335     if(name[0] == ':' && name[1] == ':'){
336         name += 2;
337     }
338
339     /* "main::main::main::Foo" -> "Foo" */
340     while(strnEQ(name, "main::", sizeof("main::")-1)){
341         name += sizeof("main::")-1;
342     }
343
344     return name;
345 }
346
347 static int
348 mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){
349     AV*  const linearized_isa = mro_get_linear_isa(instance_stash);
350     SV**       svp            = AvARRAY(linearized_isa);
351     SV** const end            = svp + AvFILLp(linearized_isa) + 1;
352
353     while(svp != end){
354         assert(SvPVX(*svp));
355         if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){
356             return TRUE;
357         }
358         svp++;
359     }
360     return FALSE;
361 }
362
363 #define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c)
364 #define find_method_pvs(a, b)    mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b))
365
366 static inline GV*
367 mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){
368     GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE);
369     if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */
370         return *gvp;
371     }
372
373     return gv_fetchmeth_autoload(stash, name, namelen, 0);
374 }
375
376 int
377 mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
378     assert(stash);
379     assert(SvTYPE(stash) == SVt_PVHV);
380
381     if(IsObject(instance)){
382         dMY_CXT;
383         HV* const instance_stash = SvSTASH(SvRV(instance));
384         GV* const instance_isa   = find_method_pvs(instance_stash, "isa");
385
386         /* the instance has no own isa method */
387         if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){
388             return stash == instance_stash
389                 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
390         }
391         /* the instance has its own isa method */
392         else {
393             int retval;
394             dSP;
395
396             ENTER;
397             SAVETMPS;
398
399             PUSHMARK(SP);
400             EXTEND(SP, 2);
401             PUSHs(instance);
402             mPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
403             PUTBACK;
404
405             call_sv((SV*)instance_isa, G_SCALAR);
406
407             SPAGAIN;
408
409             retval = SvTRUEx(POPs);
410
411             PUTBACK;
412
413             FREETMPS;
414             LEAVE;
415
416             return retval;
417         }
418     }
419     return FALSE;
420 }
421
422 static int
423 mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){
424     PERL_UNUSED_ARG(data);
425     return SvROK(sv) && SvOBJECT(SvRV(sv));
426 }
427
428 static int
429 mouse_can_methods(pTHX_ AV* const methods, SV* const instance){
430     if(IsObject(instance)){
431         dMY_CXT;
432         HV* const mystash      = SvSTASH(SvRV(instance));
433         GV* const mycan        = find_method_pvs(mystash, "can");
434         bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE;
435         I32 const len           = AvFILLp(methods) + 1;
436         I32 i;
437         for(i = 0; i < len; i++){
438             SV* const name = MOUSE_av_at(methods, i);
439
440             if(use_builtin){
441                 if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){
442                     return FALSE;
443                 }
444             }
445             else{
446                 bool ok;
447                 dSP;
448
449                 ENTER;
450                 SAVETMPS;
451
452                 PUSHMARK(SP);
453                 EXTEND(SP, 2);
454                 PUSHs(instance);
455                 PUSHs(sv_mortalcopy(name));
456                 PUTBACK;
457
458                 call_method("can", G_SCALAR);
459
460                 SPAGAIN;
461                 ok = SvTRUE(TOPs);
462                 (void)POPs;
463                 PUTBACK;
464
465                 FREETMPS;
466                 LEAVE;
467
468                 if(!ok){
469                     return FALSE;
470                 }
471             }
472         }
473         return TRUE;
474     }
475     return FALSE;
476 }
477
478 static MGVTBL mouse_util_type_constraints_vtbl; /* not used, only for identity */
479
480 static CV*
481 mouse_tc_generate(pTHX_ const char* const name, check_fptr_t const fptr, SV* const param) {
482     CV* xsub;
483
484     xsub = newXS(name, XS_Mouse_constraint_check, __FILE__);
485     CvXSUBANY(xsub).any_ptr = sv_magicext(
486         (SV*)xsub,
487         param,       /* mg_obj: refcnt will be increased */
488         PERL_MAGIC_ext,
489         &mouse_util_type_constraints_vtbl,
490         (void*)fptr, /* mg_ptr */
491         0            /* mg_len: 0 for static data */
492     );
493
494     if(!name){
495         sv_2mortal((SV*)xsub);
496     }
497
498     return xsub;
499 }
500
501 CV*
502 mouse_generate_isa_predicate_for(pTHX_ SV* const klass, const char* const predicate_name){
503     STRLEN klass_len;
504     const char* klass_pv = SvPV_const(klass, klass_len);
505     SV*   param;
506     void* fptr;
507
508     klass_pv = mouse_canonicalize_package_name(klass_pv);
509
510     if(strNE(klass_pv, "UNIVERSAL")){
511         param = (SV*)gv_stashpvn(klass_pv, klass_len, GV_ADD);
512         fptr = (void*)mouse_is_an_instance_of;
513
514     }
515     else{
516         param = NULL;
517         fptr = (void*)mouse_is_an_instance_of_universal;
518     }
519
520     return mouse_tc_generate(aTHX_ predicate_name, fptr, param);
521 }
522
523 CV*
524 mouse_generate_can_predicate_for(pTHX_ SV* const methods, const char* const predicate_name){
525     AV* av;
526     AV* const param = newAV_mortal();
527     I32 len;
528     I32 i;
529
530     SvGETMAGIC(methods);
531     if(!IsArrayRef(methods)){
532         croak("You must pass an ARRAY ref method names");
533     }
534     av = (AV*)SvRV(methods);
535
536     len = av_len(av) + 1;
537     for(i = 0; i < len; i++){
538         SV* const name = *av_fetch(av, i, TRUE);
539         STRLEN pvlen;
540         const char* const pv = SvPV_const(name, pvlen);
541
542         av_push(param, newSVpvn_share(pv, pvlen, 0U));
543     }
544
545     return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param);
546 }
547
548
549 XS(XS_Mouse_constraint_check) {
550     dVAR;
551     dXSARGS;
552     MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
553
554     if(items < 1){
555         croak("Too few arguments for type constraint check functions");
556     }
557
558     SvGETMAGIC( ST(0) );
559     ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) );
560     XSRETURN(1);
561 }
562
563 static void
564 setup_my_cxt(pTHX_ pMY_CXT){
565     MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
566     SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
567
568     MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV);
569     SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can);
570 }
571
572 #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
573
574 MODULE = Mouse::Util::TypeConstraints    PACKAGE = Mouse::Util::TypeConstraints
575
576 PROTOTYPES:   DISABLE
577 VERSIONCHECK: DISABLE
578
579 BOOT:
580 {
581     MY_CXT_INIT;
582     setup_my_cxt(aTHX_ aMY_CXT);
583
584     /* setup built-in type constraints */
585     DEFINE_TC(Any);
586     DEFINE_TC(Undef);
587     DEFINE_TC(Defined);
588     DEFINE_TC(Bool);
589     DEFINE_TC(Value);
590     DEFINE_TC(Ref);
591     DEFINE_TC(Str);
592     DEFINE_TC(Num);
593     DEFINE_TC(Int);
594     DEFINE_TC(ScalarRef);
595     DEFINE_TC(ArrayRef);
596     DEFINE_TC(HashRef);
597     DEFINE_TC(CodeRef);
598     DEFINE_TC(GlobRef);
599     DEFINE_TC(FileHandle);
600     DEFINE_TC(RegexpRef);
601     DEFINE_TC(Object);
602     DEFINE_TC(ClassName);
603     DEFINE_TC(RoleName);
604 }
605
606 #ifdef USE_ITHREADS
607
608 void
609 CLONE(...)
610 CODE:
611 {
612     MY_CXT_CLONE;
613     setup_my_cxt(aTHX_ aMY_CXT);
614     PERL_UNUSED_VAR(items);
615 }
616
617 #endif /* !USE_ITHREADS */
618
619 #define MOUSE_TC_MAYBE     0
620 #define MOUSE_TC_ARRAY_REF 1
621 #define MOUSE_TC_HASH_REF  2
622
623 CV*
624 _parameterize_ArrayRef_for(SV* param)
625 ALIAS:
626     _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF
627     _parameterize_HashRef_for  = MOUSE_TC_HASH_REF
628     _parameterize_Maybe_for    = MOUSE_TC_MAYBE
629 CODE:
630 {
631     check_fptr_t fptr;
632     SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
633     if(!IsCodeRef(tc_code)){
634         croak("_compiled_type_constraint didn't return a CODE reference");
635     }
636
637     switch(ix){
638     case MOUSE_TC_ARRAY_REF:
639         fptr = mouse_parameterized_ArrayRef;
640         break;
641     case MOUSE_TC_HASH_REF:
642         fptr = mouse_parameterized_HashRef;
643         break;
644     default: /* Maybe type */
645         fptr = mouse_parameterized_Maybe;
646     }
647     RETVAL = mouse_tc_generate(aTHX_ NULL, fptr, tc_code);
648 }
649 OUTPUT:
650     RETVAL
651
652 MODULE = Mouse::Util::TypeConstraints    PACKAGE = Mouse::Meta::TypeConstraint
653
654 BOOT:
655     INSTALL_SIMPLE_READER(TypeConstraint, name);
656     INSTALL_SIMPLE_READER(TypeConstraint, parent);
657     INSTALL_SIMPLE_READER(TypeConstraint, message);
658
659     INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
660     INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
661
662     INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
663
664 void
665 compile_type_constraint(SV* self)
666 CODE:
667 {
668     AV* const checks = newAV_mortal();
669     SV* check; /* check function */
670     SV* parent;
671     SV* types_ref;
672
673     for(parent = get_slots(self, "parent"); parent; parent = get_slots(parent, "parent")){
674         check = get_slots(parent, "hand_optimized_type_constraint");
675         if(check && SvOK(check)){
676             if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
677                 croak("Not a CODE reference");
678             }
679             av_unshift(checks, 1);
680             av_store(checks, 0, newSVsv(check));
681             break; /* a hand optimized constraint must include all the parent */
682         }
683
684         check = get_slots(parent, "constraint");
685         if(check && SvOK(check)){
686             if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
687                 croak("Not a CODE reference");
688             }
689             av_unshift(checks, 1);
690             av_store(checks, 0, newSVsv(check));
691         }
692     }
693
694     check = get_slots(self, "constraint");
695     if(check && SvOK(check)){
696         if(!mouse_tc_CodeRef(aTHX_ NULL, check)){
697             croak("Not a CODE reference");
698         }
699         av_push(checks, newSVsv(check));
700     }
701
702     types_ref = get_slots(self, "type_constraints");
703     if(types_ref && SvOK(types_ref)){ /* union type */
704         AV* types;
705         AV* union_checks;
706         CV* union_check;
707         I32 len;
708         I32 i;
709
710         if(!IsArrayRef(types_ref)){
711             croak("Not an ARRAY reference");
712         }
713         types = (AV*)SvRV(types_ref);
714         len = av_len(types) + 1;
715
716         union_checks = newAV_mortal();
717
718         for(i = 0; i < len; i++){
719             SV* const tc = *av_fetch(types, i, TRUE);
720             SV* const c  = get_slots(tc, "compiled_type_constraint");
721             if(!(c && mouse_tc_CodeRef(aTHX_ NULL, c))){
722                 sv_dump(self);
723                 croak("'%"SVf"' has no compiled type constraint", self);
724             }
725             av_push(union_checks, newSVsv(c));
726         }
727
728         union_check = mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_union_check, (SV*)union_checks);
729         av_push(checks, newRV_inc((SV*)union_check));
730     }
731
732     if(AvFILLp(checks) < 0){
733         check = newRV_inc((SV*)get_cv("Mouse::Util::TypeConstraints::Any", TRUE));
734     }
735     else{
736         check = newRV_inc((SV*)mouse_tc_generate(aTHX_ NULL, (check_fptr_t)mouse_types_check, (SV*)checks));
737     }
738     set_slots(self, "compiled_type_constraint", check);
739 }
740