Clean up mouse_tc_ScalarRef()
[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* sv) {
174     assert(sv);
175     if(SvROK(sv)){
176          sv = SvRV(sv);
177          return !SvOBJECT(sv) && (SvTYPE(sv) <= SVt_PVLV && !isGV(sv));
178     }
179     return FALSE;
180 }
181
182 int
183 mouse_tc_ArrayRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
184     assert(sv);
185     return IsArrayRef(sv);
186 }
187
188 int
189 mouse_tc_HashRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
190     assert(sv);
191     return IsHashRef(sv);
192 }
193
194 int
195 mouse_tc_CodeRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
196     assert(sv);
197     return IsCodeRef(sv);
198 }
199
200 int
201 mouse_tc_RegexpRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
202     assert(sv);
203     return SvRXOK(sv);
204 }
205
206 int
207 mouse_tc_GlobRef(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
208     assert(sv);
209     return SvROK(sv) && !SvOBJECT(SvRV(sv)) && isGV(SvRV(sv));
210 }
211
212 int
213 mouse_tc_FileHandle(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
214     GV* gv;
215     assert(sv);
216
217     /* see pp_fileno() in pp_sys.c and Scalar::Util::openhandle() */
218
219     gv = (GV*)(SvROK(sv) ? SvRV(sv) : sv);
220     if(isGV(gv) || SvTYPE(gv) == SVt_PVIO){
221         IO* const io = isGV(gv) ? GvIO(gv) : (IO*)gv;
222
223         if(io && ( IoIFP(io) || SvTIED_mg((SV*)io, PERL_MAGIC_tiedscalar) )){
224             return TRUE;
225         }
226     }
227
228     return is_an_instance_of("IO::Handle", sv);
229 }
230
231 int
232 mouse_tc_Object(pTHX_ SV* const data PERL_UNUSED_DECL, SV* const sv) {
233     assert(sv);
234     return SvROK(sv) && SvOBJECT(SvRV(sv)) && !SvRXOK(sv);
235 }
236
237 /* Parameterized type constraints */
238
239 static int
240 mouse_parameterized_ArrayRef(pTHX_ SV* const param, SV* const sv) {
241     if(IsArrayRef(sv)){
242         AV* const av  = (AV*)SvRV(sv);
243         I32 const len = av_len(av) + 1;
244         I32 i;
245         for(i = 0; i < len; i++){
246             SV* const value = *av_fetch(av, i, TRUE);
247             SvGETMAGIC(value);
248             if(!mouse_tc_check(aTHX_ param, value)){
249                 return FALSE;
250             }
251         }
252         return TRUE;
253     }
254     return FALSE;
255 }
256
257 static int
258 mouse_parameterized_HashRef(pTHX_ SV* const param, SV* const sv) {
259     if(mouse_tc_HashRef(aTHX_ NULL, sv)){
260         HV* const hv  = (HV*)SvRV(sv);
261         HE* he;
262
263         hv_iterinit(hv);
264         while((he = hv_iternext(hv))){
265             SV* const value = hv_iterval(hv, he);
266             SvGETMAGIC(value);
267             if(!mouse_tc_check(aTHX_ param, value)){
268                 hv_iterinit(hv); /* reset */
269                 return FALSE;
270             }
271         }
272         return TRUE;
273     }
274     return FALSE;
275 }
276
277 static int
278 mouse_parameterized_Maybe(pTHX_ SV* const param, SV* const sv) {
279     if(SvOK(sv)){
280         return mouse_tc_check(aTHX_ param, sv);
281     }
282     return TRUE;
283 }
284
285 static int
286 mouse_types_union_check(pTHX_ AV* const types, SV* const sv) {
287     I32 const len = AvFILLp(types) + 1;
288     I32 i;
289
290     for(i = 0; i < len; i++){
291         if(mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
292             return TRUE;
293         }
294     }
295
296     return FALSE;
297 }
298
299 static int
300 mouse_types_check(pTHX_ AV* const types, SV* const sv) {
301     I32 const len = AvFILLp(types) + 1;
302     I32 i;
303
304     ENTER;
305     SAVE_DEFSV;
306     DEFSV_set(sv);
307
308     for(i = 0; i < len; i++){
309         if(!mouse_tc_check(aTHX_ AvARRAY(types)[i], sv)){
310             LEAVE;
311             return FALSE;
312         }
313     }
314
315     LEAVE;
316
317     return TRUE;
318 }
319
320 /*
321  *  This class_type generator is taken from Scalar::Util::Instance
322  */
323
324 #define MY_CXT_KEY "Mouse::Util::TypeConstraints::_guts" XS_VERSION
325 typedef struct sui_cxt{
326     GV* universal_isa;
327     GV* universal_can;
328 } my_cxt_t;
329 START_MY_CXT
330
331 #define MG_klass_stash(mg) ((HV*)(mg)->mg_obj)
332 #define MG_klass_pv(mg)    ((mg)->mg_ptr)
333 #define MG_klass_len(mg)   ((mg)->mg_len)
334
335 static const char*
336 mouse_canonicalize_package_name(const char* name){
337
338     /* "::Foo" -> "Foo" */
339     if(name[0] == ':' && name[1] == ':'){
340         name += 2;
341     }
342
343     /* "main::main::main::Foo" -> "Foo" */
344     while(strnEQ(name, "main::", sizeof("main::")-1)){
345         name += sizeof("main::")-1;
346     }
347
348     return name;
349 }
350
351 static int
352 mouse_lookup_isa(pTHX_ HV* const instance_stash, const char* const klass_pv){
353     AV*  const linearized_isa = mro_get_linear_isa(instance_stash);
354     SV**       svp            = AvARRAY(linearized_isa);
355     SV** const end            = svp + AvFILLp(linearized_isa) + 1;
356
357     while(svp != end){
358         assert(SvPVX(*svp));
359         if(strEQ(klass_pv, mouse_canonicalize_package_name(SvPVX(*svp)))){
360             return TRUE;
361         }
362         svp++;
363     }
364     return FALSE;
365 }
366
367 #define find_method_pvn(a, b, c) mouse_stash_find_method(aTHX_ a, b, c)
368 #define find_method_pvs(a, b)    mouse_stash_find_method(aTHX_ a, STR_WITH_LEN(b))
369
370 static inline GV*
371 mouse_stash_find_method(pTHX_ HV* const stash, const char* const name, I32 const namelen){
372     GV** const gvp = (GV**)hv_fetch(stash, name, namelen, FALSE);
373     if(gvp && isGV(*gvp) && GvCV(*gvp)){ /* shortcut */
374         return *gvp;
375     }
376
377     return gv_fetchmeth_autoload(stash, name, namelen, 0);
378 }
379
380 int
381 mouse_is_an_instance_of(pTHX_ HV* const stash, SV* const instance){
382     assert(stash);
383     assert(SvTYPE(stash) == SVt_PVHV);
384
385     if(IsObject(instance)){
386         dMY_CXT;
387         HV* const instance_stash = SvSTASH(SvRV(instance));
388         GV* const instance_isa   = find_method_pvs(instance_stash, "isa");
389
390         /* the instance has no own isa method */
391         if(instance_isa == NULL || GvCV(instance_isa) == GvCV(MY_CXT.universal_isa)){
392             return stash == instance_stash
393                 || mouse_lookup_isa(aTHX_ instance_stash, HvNAME_get(stash));
394         }
395         /* the instance has its own isa method */
396         else {
397             int retval;
398             dSP;
399
400             ENTER;
401             SAVETMPS;
402
403             PUSHMARK(SP);
404             EXTEND(SP, 2);
405             PUSHs(instance);
406             mPUSHp(HvNAME_get(stash), HvNAMELEN_get(stash));
407             PUTBACK;
408
409             call_sv((SV*)instance_isa, G_SCALAR);
410
411             SPAGAIN;
412
413             retval = SvTRUEx(POPs);
414
415             PUTBACK;
416
417             FREETMPS;
418             LEAVE;
419
420             return retval;
421         }
422     }
423     return FALSE;
424 }
425
426 static int
427 mouse_is_an_instance_of_universal(pTHX_ SV* const data, SV* const sv){
428     PERL_UNUSED_ARG(data);
429     return SvROK(sv) && SvOBJECT(SvRV(sv));
430 }
431
432 static int
433 mouse_can_methods(pTHX_ AV* const methods, SV* const instance){
434     if(IsObject(instance)){
435         dMY_CXT;
436         HV* const mystash      = SvSTASH(SvRV(instance));
437         GV* const mycan        = find_method_pvs(mystash, "can");
438         bool const use_builtin = (mycan == NULL || GvCV(mycan) == GvCV(MY_CXT.universal_can)) ? TRUE : FALSE;
439         I32 const len           = AvFILLp(methods) + 1;
440         I32 i;
441         for(i = 0; i < len; i++){
442             SV* const name = MOUSE_av_at(methods, i);
443
444             if(use_builtin){
445                 if(!find_method_pvn(mystash, SvPVX(name), SvCUR(name))){
446                     return FALSE;
447                 }
448             }
449             else{
450                 bool ok;
451                 dSP;
452
453                 ENTER;
454                 SAVETMPS;
455
456                 PUSHMARK(SP);
457                 EXTEND(SP, 2);
458                 PUSHs(instance);
459                 PUSHs(sv_mortalcopy(name));
460                 PUTBACK;
461
462                 call_method("can", G_SCALAR);
463
464                 SPAGAIN;
465                 ok = SvTRUE(TOPs);
466                 (void)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         (void*)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     void* 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 = (void*)mouse_is_an_instance_of;
517
518     }
519     else{
520         param = NULL;
521         fptr = (void*)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     SvGETMAGIC(methods);
535     if(!IsArrayRef(methods)){
536         croak("You must pass an ARRAY ref method names");
537     }
538     av = (AV*)SvRV(methods);
539
540     len = av_len(av) + 1;
541     for(i = 0; i < len; i++){
542         SV* const name = *av_fetch(av, i, TRUE);
543         STRLEN pvlen;
544         const char* const pv = SvPV_const(name, pvlen);
545
546         av_push(param, newSVpvn_share(pv, pvlen, 0U));
547     }
548
549     return mouse_tc_generate(aTHX_ predicate_name, (check_fptr_t)mouse_can_methods, (SV*)param);
550 }
551
552
553 XS(XS_Mouse_constraint_check) {
554     dVAR;
555     dXSARGS;
556     MAGIC* const mg = (MAGIC*)XSANY.any_ptr;
557
558     if(items < 1){
559         croak("Too few arguments for type constraint check functions");
560     }
561
562     SvGETMAGIC( ST(0) );
563     ST(0) = boolSV( CALL_FPTR((check_fptr_t)mg->mg_ptr)(aTHX_ mg->mg_obj, ST(0)) );
564     XSRETURN(1);
565 }
566
567 static void
568 setup_my_cxt(pTHX_ pMY_CXT){
569     MY_CXT.universal_isa = gv_fetchpvs("UNIVERSAL::isa", GV_ADD, SVt_PVCV);
570     SvREFCNT_inc_simple_void_NN(MY_CXT.universal_isa);
571
572     MY_CXT.universal_can = gv_fetchpvs("UNIVERSAL::can", GV_ADD, SVt_PVCV);
573     SvREFCNT_inc_simple_void_NN(MY_CXT.universal_can);
574 }
575
576 #define DEFINE_TC(name) mouse_tc_generate(aTHX_ "Mouse::Util::TypeConstraints::" STRINGIFY(name), CAT2(mouse_tc_, name), NULL)
577
578 MODULE = Mouse::Util::TypeConstraints    PACKAGE = Mouse::Util::TypeConstraints
579
580 PROTOTYPES:   DISABLE
581 VERSIONCHECK: DISABLE
582
583 BOOT:
584 {
585     MY_CXT_INIT;
586     setup_my_cxt(aTHX_ aMY_CXT);
587
588     /* setup built-in type constraints */
589     DEFINE_TC(Any);
590     DEFINE_TC(Undef);
591     DEFINE_TC(Defined);
592     DEFINE_TC(Bool);
593     DEFINE_TC(Value);
594     DEFINE_TC(Ref);
595     DEFINE_TC(Str);
596     DEFINE_TC(Num);
597     DEFINE_TC(Int);
598     DEFINE_TC(ScalarRef);
599     DEFINE_TC(ArrayRef);
600     DEFINE_TC(HashRef);
601     DEFINE_TC(CodeRef);
602     DEFINE_TC(GlobRef);
603     DEFINE_TC(FileHandle);
604     DEFINE_TC(RegexpRef);
605     DEFINE_TC(Object);
606     DEFINE_TC(ClassName);
607     DEFINE_TC(RoleName);
608 }
609
610 #ifdef USE_ITHREADS
611
612 void
613 CLONE(...)
614 CODE:
615 {
616     MY_CXT_CLONE;
617     setup_my_cxt(aTHX_ aMY_CXT);
618     PERL_UNUSED_VAR(items);
619 }
620
621 #endif /* !USE_ITHREADS */
622
623 #define MOUSE_TC_MAYBE     0
624 #define MOUSE_TC_ARRAY_REF 1
625 #define MOUSE_TC_HASH_REF  2
626
627 CV*
628 _parameterize_ArrayRef_for(SV* param)
629 ALIAS:
630     _parameterize_ArrayRef_for = MOUSE_TC_ARRAY_REF
631     _parameterize_HashRef_for  = MOUSE_TC_HASH_REF
632     _parameterize_Maybe_for    = MOUSE_TC_MAYBE
633 CODE:
634 {
635     check_fptr_t fptr;
636     SV* const tc_code = mcall0s(param, "_compiled_type_constraint");
637     if(!IsCodeRef(tc_code)){
638         croak("_compiled_type_constraint didn't return a CODE reference");
639     }
640
641     switch(ix){
642     case MOUSE_TC_ARRAY_REF:
643         fptr = mouse_parameterized_ArrayRef;
644         break;
645     case MOUSE_TC_HASH_REF:
646         fptr = mouse_parameterized_HashRef;
647         break;
648     default: /* Maybe type */
649         fptr = mouse_parameterized_Maybe;
650     }
651     RETVAL = mouse_tc_generate(aTHX_ NULL, fptr, tc_code);
652 }
653 OUTPUT:
654     RETVAL
655
656 MODULE = Mouse::Util::TypeConstraints    PACKAGE = Mouse::Meta::TypeConstraint
657
658 BOOT:
659     INSTALL_SIMPLE_READER(TypeConstraint, name);
660     INSTALL_SIMPLE_READER(TypeConstraint, parent);
661     INSTALL_SIMPLE_READER(TypeConstraint, message);
662
663     INSTALL_SIMPLE_READER_WITH_KEY(TypeConstraint, _compiled_type_constraint, compiled_type_constraint);
664     INSTALL_SIMPLE_READER(TypeConstraint, _compiled_type_coercion); /* Mouse specific */
665
666     INSTALL_SIMPLE_PREDICATE_WITH_KEY(TypeConstraint, has_coercion, _compiled_type_coercion);
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     set_slots(self, "compiled_type_constraint", check);
743 }
744