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