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