FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
livecode/libfoundation/src/foundation-typeinfo.cpp at develop · livecode/livecode · GitHub
Uh oh!
There was an error while loading.
Please reload this page
.
This repository was archived by the owner on Aug 31, 2021. It is now read-only.
livecode
/
livecode
Public archive
Notifications
You must be signed in to change notification settings
Fork
223
Star
517
Code
Pull requests
189
Actions
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Security and quality
Insights
Expand file tree
Breadcrumbs
livecode
/
libfoundation
/
src
/
foundation-typeinfo.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
1431 lines (1151 loc) · 45.8 KB
Breadcrumbs
livecode
/
libfoundation
/
src
/
foundation-typeinfo.cpp
Copy path
File metadata and controls
1431 lines (1151 loc) · 45.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
Copyright (C) 2003-2015 LiveCode Ltd.
This file is part of LiveCode.
LiveCode is free software; you can redistribute it and/or modify it under
the terms of the GNU General Public License v3 as published by the Free
Software Foundation.
LiveCode is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License
along with LiveCode. If not see <http://www.gnu.org/licenses/>.
*/
#
include
<
foundation.h
>
#
include
<
foundation-auto.h
>
#
include
<
ffi.h
>
#
include
"
foundation-private.h
"
#
include
"
foundation-hash.h
"
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCAnyTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCNullTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCBooleanTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCNumberTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCStringTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCNameTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCDataTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCArrayTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCSetTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCListTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
kMCProperListTypeInfo
;
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCAnyTypeInfo
() {
return
kMCAnyTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCNullTypeInfo
() {
return
kMCNullTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCBooleanTypeInfo
() {
return
kMCBooleanTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCNumberTypeInfo
() {
return
kMCNumberTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCStringTypeInfo
() {
return
kMCStringTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCNameTypeInfo
() {
return
kMCNameTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCDataTypeInfo
() {
return
kMCDataTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCArrayTypeInfo
() {
return
kMCArrayTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCSetTypeInfo
() {
return
kMCSetTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCListTypeInfo
() {
return
kMCListTypeInfo
; }
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCProperListTypeInfo
() {
return
kMCProperListTypeInfo
; }
//
//////////////////////////////////////////////////////////////////////////////
static
intenum_t
__MCTypeInfoGetExtendedTypeCode
(MCTypeInfoRef self)
{
return
(self -> flags &
0xff
);
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCTypeInfoIsAlias
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
return
__MCTypeInfoGetExtendedTypeCode
(self) ==
kMCTypeInfoTypeIsAlias
;
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoIsNamed
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
return
__MCTypeInfoGetExtendedTypeCode
(self) ==
kMCTypeInfoTypeIsNamed
;
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoIsOptional
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
return
__MCTypeInfoGetExtendedTypeCode
(self) ==
kMCTypeInfoTypeIsOptional
;
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoIsHandler
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
return
__MCTypeInfoGetExtendedTypeCode
(self) ==
kMCValueTypeCodeHandler
;
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoIsRecord
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
return
__MCTypeInfoGetExtendedTypeCode
(self) ==
kMCValueTypeCodeRecord
;
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoIsError
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
return
__MCTypeInfoGetExtendedTypeCode
(self) ==
kMCValueTypeCodeError
;
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoIsForeign
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
return
__MCTypeInfoGetExtendedTypeCode
(self) ==
kMCTypeInfoTypeIsForeign
;
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoIsCustom
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
return
__MCTypeInfoGetExtendedTypeCode
(self) ==
kMCValueTypeCodeCustom
;
}
MC_DLLEXPORT_DEF
MCValueRef
MCTypeInfoGetDefault
(MCTypeInfoRef self)
{
__MCAssertIsTypeInfo
(self);
switch
(
__MCTypeInfoGetExtendedTypeCode
(self))
{
case
kMCValueTypeCodeNull
:
return
kMCNull
;
case
kMCValueTypeCodeBoolean
:
return
kMCFalse
;
case
kMCValueTypeCodeNumber
:
return
kMCZero
;
case
kMCValueTypeCodeName
:
return
kMCEmptyName
;
case
kMCValueTypeCodeString
:
return
kMCEmptyString
;
case
kMCValueTypeCodeData
:
return
kMCEmptyData
;
case
kMCValueTypeCodeArray
:
return
kMCEmptyArray
;
case
kMCValueTypeCodeList
:
return
kMCEmptyList
;
case
kMCValueTypeCodeSet
:
return
kMCEmptySet
;
case
kMCValueTypeCodeProperList
:
return
kMCEmptyProperList
;
case
kMCValueTypeCodeCustom
:
return
nil;
case
kMCValueTypeCodeRecord
:
return
nil;
case
kMCValueTypeCodeHandler
:
return
nil;
case
kMCValueTypeCodeTypeInfo
:
return
nil;
case
kMCValueTypeCodeError
:
return
nil;
case
kMCValueTypeCodeForeignValue
:
return
nil;
case
kMCTypeInfoTypeIsOptional
:
return
kMCNull
;
case
kMCTypeInfoTypeIsAlias
:
return
MCTypeInfoGetDefault
(self -> alias . typeinfo);
case
kMCTypeInfoTypeIsNamed
:
return
MCTypeInfoGetDefault
(self -> named . typeinfo);
default
:
return
nil;
}
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoResolve
(MCTypeInfoRef self, MCResolvedTypeInfo& r_resolution)
{
__MCAssertIsTypeInfo
(self);
intenum_t
t_ext_typecode;
t_ext_typecode =
__MCTypeInfoGetExtendedTypeCode
(self);
if
(t_ext_typecode ==
kMCTypeInfoTypeIsAlias
)
return
MCTypeInfoResolve
(self -> alias . typeinfo, r_resolution);
if
(t_ext_typecode ==
kMCTypeInfoTypeIsNamed
)
{
//
Attempt to resolve the binding, this will throw an error if it fails.
MCTypeInfoRef t_next_type =
nullptr
;
if
(!
MCNamedTypeInfoResolve
(self, t_next_type))
return
false
;
//
We've successfully resolved the type, so return this one as the resolution.
r_resolution . named_type = self;
r_resolution . type = t_next_type;
r_resolution . is_optional =
false
;
return
true
;
}
if
(t_ext_typecode ==
kMCTypeInfoTypeIsOptional
)
{
if
(!
MCTypeInfoResolve
(self -> optional . basetype, r_resolution))
return
false
;
r_resolution . is_optional =
true
;
return
true
;
}
//
Resolving any other form of type, returns the (un-named) naked typeinfo.
r_resolution . is_optional =
false
;
r_resolution . named_type = nil;
r_resolution . type = self;
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCTypeInfoConforms
(MCTypeInfoRef source, MCTypeInfoRef target)
{
//
We require that source is concrete for all but handler types (as handlers
//
have unnamed typeinfos which we need to compare with potentially named
//
handler type typeinfos).
MCAssert
(
MCTypeInfoIsNamed
(source) ||
MCTypeInfoIsHandler
(source) ||
MCTypeInfoIsOptional
(source));
//
If the two types are the same, they conform.
if
(source == target)
{
return
true
;
}
//
Resolve the source type.
MCResolvedTypeInfo t_resolved_source;
if
(!
MCTypeInfoResolve
(source, t_resolved_source))
{
MCAssert
(
false
);
return
false
;
}
//
We require that target is resolvable.
MCResolvedTypeInfo t_resolved_target;
if
(!
MCTypeInfoResolve
(target, t_resolved_target))
{
MCAssert
(
false
);
return
false
;
}
return
MCResolvedTypeInfoConforms
(t_resolved_source, t_resolved_target);
}
MC_DLLEXPORT_DEF
bool
MCResolvedTypeInfoConforms
(
const
MCResolvedTypeInfo& source,
const
MCResolvedTypeInfo& target)
{
//
If source and target are the same, we are done - as they are named types.
if
(source . named_type != nil &&
source . named_type == target . named_type)
return
true
;
//
If source is undefined, then target must be optional.
if
(source . named_type ==
kMCNullTypeInfo
)
return
target . is_optional;
//
If the target is any, then all is well.
if
(target . named_type ==
kMCAnyTypeInfo
)
return
true
;
//
If source is of foreign type then target must be the source's bridge type
//
the source type, or one of the source's supertypes.
if
(
MCTypeInfoIsForeign
(source . type))
{
//
If both sides are foreign, do they have a bridge type in common?
if
(
MCTypeInfoIsForeign
(target.
type
))
{
if
(source.
type
->
foreign
.
descriptor
.
bridgetype
!=
kMCNullTypeInfo
&&
source.
type
->
foreign
.
descriptor
.
bridgetype
== target.
type
->
foreign
.
descriptor
.
bridgetype
)
return
true
;
}
//
Check to see if the target is the source's bridge type.
if
(source . type -> foreign . descriptor . bridgetype !=
kMCNullTypeInfo
&&
target . named_type == source . type -> foreign . descriptor . bridgetype)
return
true
;
//
Now check to see if the target is one of the source's supertypes.
for
(MCTypeInfoRef t_supertype = source . type; t_supertype !=
kMCNullTypeInfo
; t_supertype =
__MCTypeInfoResolve
(t_supertype) -> foreign . descriptor . basetype)
if
(target . named_type == t_supertype)
return
true
;
return
false
;
}
//
If the target is of foreign type, then the source must be the target's
//
bridge type.
if
(
MCTypeInfoIsForeign
(target . type))
{
if
(target . type -> foreign . descriptor . bridgetype !=
kMCNullTypeInfo
&&
target . type -> foreign . descriptor . bridgetype == source . named_type)
return
true
;
return
false
;
}
//
If the source is of record type, then the target must be the same type.
if
(
MCTypeInfoIsRecord
(source . type))
{
return
false
;
}
//
If the source is of custom type, then the target must be the same type or
//
one of the source's super types.
if
(
MCTypeInfoIsCustom
(source . type))
{
//
Now check to see if the target is one of the source's supertypes.
for
(MCTypeInfoRef t_supertype = source . type; t_supertype !=
kMCNullTypeInfo
; t_supertype =
__MCTypeInfoResolve
(t_supertype) -> custom . base)
if
(target . named_type == t_supertype)
return
true
;
return
false
;
}
//
If the source is a handler type then we must check conformance with the
//
dst handler type.
if
(
MCTypeInfoIsHandler
(source . type))
{
//
If the other type is not a handler, then we are done.
if
(!
MCTypeInfoIsHandler
(target . type))
return
false
;
//
The number of parameters must conform.
if
(
MCHandlerTypeInfoGetParameterCount
(source . type) !=
MCHandlerTypeInfoGetParameterCount
(target . type))
return
false
;
//
The source return type must conform to the target (i.e. the return value
//
of the concrete handler, must be assignable to the return value of the
//
abstract handler).
if
(!
MCTypeInfoConforms
(
MCHandlerTypeInfoGetReturnType
(source . type),
MCHandlerTypeInfoGetReturnType
(target . type)))
return
false
;
//
The modes of each parameter must match, and conformance must correspond to the
//
mode.
for
(
uindex_t
i =
0
; i <
MCHandlerTypeInfoGetParameterCount
(source . type); i++)
{
if
(
MCHandlerTypeInfoGetParameterMode
(source . type, i) !=
MCHandlerTypeInfoGetParameterMode
(target . type, i))
return
false
;
//
Out parameters - source must conform to target.
if
(
MCHandlerTypeInfoGetParameterMode
(source . type, i) !=
kMCHandlerTypeFieldModeOut
)
{
if
(!
MCTypeInfoConforms
(
MCHandlerTypeInfoGetParameterType
(source . type, i),
MCHandlerTypeInfoGetParameterType
(target . type, i)))
return
false
;
}
//
In parameters - target must conform to source.
if
(
MCHandlerTypeInfoGetParameterMode
(source . type, i) !=
kMCHandlerTypeFieldModeIn
)
{
if
(!
MCTypeInfoConforms
(
MCHandlerTypeInfoGetParameterType
(target . type, i),
MCHandlerTypeInfoGetParameterType
(source . type, i)))
return
false
;
}
}
return
true
;
}
return
false
;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCBuiltinTypeInfoCreate
(MCValueTypeCode p_code, MCTypeInfoRef& r_typeinfo)
{
__MCTypeInfo *self;
if
(!
__MCValueCreate
(
kMCValueTypeCodeTypeInfo
, self))
return
false
;
self -> flags |= p_code &
0xff
;
if
(
MCValueInterAndRelease
(self, r_typeinfo))
return
true
;
MCValueRelease
(self);
return
false
;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCAliasTypeInfoCreate
(MCNameRef p_name, MCTypeInfoRef p_target, MCTypeInfoRef& r_typeinfo)
{
__MCAssertIsName
(p_name);
__MCAssertIsTypeInfo
(p_target);
__MCTypeInfo *self;
if
(!
__MCValueCreate
(
kMCValueTypeCodeTypeInfo
, self))
return
false
;
self -> flags |=
kMCTypeInfoTypeIsAlias
&
0xff
;
self -> alias . name =
MCValueRetain
(p_name);
self -> alias . typeinfo =
MCValueRetain
(p_target);
if
(
MCValueInterAndRelease
(self, r_typeinfo))
return
true
;
MCValueRelease
(self);
return
false
;
}
MC_DLLEXPORT_DEF
MCNameRef
MCAliasTypeInfoGetName
(MCTypeInfoRef self)
{
MCAssert
(
MCTypeInfoIsAlias
(self));
return
self -> alias . name;
}
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCAliasTypeInfoGetTarget
(MCTypeInfoRef self)
{
MCAssert
(
MCTypeInfoIsAlias
(self));
return
self -> alias . typeinfo;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCNamedTypeInfoCreate
(MCNameRef p_name, MCTypeInfoRef& r_typeinfo)
{
__MCAssertIsName
(p_name);
__MCTypeInfo *self;
if
(!
__MCValueCreate
(
kMCValueTypeCodeTypeInfo
, self))
return
false
;
self -> flags |=
kMCTypeInfoTypeIsNamed
&
0xff
;
self -> named . name =
MCValueRetain
(p_name);
//
Note that we don't do anything with the 'typeinfo' field of the named typeinfo.
//
This is because it does not form part of the uniqueness of the typeinfo, thus
//
when we inter we get an existing named typeinfo with the same name.
if
(
MCValueInterAndRelease
(self, r_typeinfo))
return
true
;
MCValueRelease
(self);
return
false
;
}
MC_DLLEXPORT_DEF
MCNameRef
MCNamedTypeInfoGetName
(MCTypeInfoRef self)
{
MCAssert
(
MCTypeInfoIsNamed
(self));
return
self -> named . name;
}
MC_DLLEXPORT_DEF
bool
MCNamedTypeInfoIsBound
(MCTypeInfoRef self)
{
MCAssert
(
MCTypeInfoIsNamed
(self));
return
self -> named . typeinfo != nil;
}
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCNamedTypeInfoGetBoundTypeInfo
(MCTypeInfoRef self)
{
MCAssert
(
MCTypeInfoIsNamed
(self));
return
self -> named . typeinfo;
}
MC_DLLEXPORT_DEF
bool
MCNamedTypeInfoBind
(MCTypeInfoRef self, MCTypeInfoRef p_target)
{
MCAssert
(
MCTypeInfoIsNamed
(self));
__MCAssertIsTypeInfo
(p_target);
if
(self -> named . typeinfo != nil)
return
MCErrorThrowGenericWithMessage
(
MCSTR
(
"
Can't bind typeinfo %{name}: already bound to %{self}
"
),
"
name
"
, p_target->
named
.
name
,
"
self
"
, self->
named
.
name
,
nullptr
);
self -> named . typeinfo =
MCValueRetain
(p_target);
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCNamedTypeInfoUnbind
(MCTypeInfoRef self)
{
MCAssert
(
MCTypeInfoIsNamed
(self));
if
(self -> named . typeinfo == nil)
return
MCErrorThrowGeneric
(
MCSTR
(
"
Can't unbind typeinfo: not bound
"
));
MCValueRelease
(self -> named . typeinfo);
self -> named . typeinfo = nil;
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCNamedTypeInfoResolve
(MCTypeInfoRef self, MCTypeInfoRef& r_bound_type)
{
MCAssert
(
MCTypeInfoIsNamed
(self));
if
(self -> named . typeinfo == nil)
return
MCErrorThrowGeneric
(
MCSTR
(
"
Can't resolve typeinfo: not bound
"
));
r_bound_type = self -> named . typeinfo;
return
true
;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCOptionalTypeInfoCreate
(MCTypeInfoRef p_base, MCTypeInfoRef& r_new_type)
{
__MCAssertIsTypeInfo
(p_base);
if
(
__MCTypeInfoGetExtendedTypeCode
(p_base) ==
kMCTypeInfoTypeIsOptional
)
{
r_new_type =
MCValueRetain
(p_base);
return
true
;
}
__MCTypeInfo *self;
if
(!
__MCValueCreate
(
kMCValueTypeCodeTypeInfo
, self))
return
false
;
self -> flags |=
kMCTypeInfoTypeIsOptional
;
self -> optional . basetype =
MCValueRetain
(p_base);
if
(
MCValueInterAndRelease
(self, r_new_type))
return
true
;
MCValueRelease
(self);
return
false
;
}
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCOptionalTypeInfoGetBaseTypeInfo
(MCTypeInfoRef p_base)
{
MCAssert
(
MCTypeInfoIsOptional
(p_base));
return
p_base -> optional . basetype;
}
//
//////////////////////////////////////////////////////////////////////////////
static
ffi_type *
__map_primitive_type
(MCForeignPrimitiveType p_type)
{
switch
(p_type)
{
case
kMCForeignPrimitiveTypeVoid
:
return
&ffi_type_void;
case
kMCForeignPrimitiveTypeBool
:
case
kMCForeignPrimitiveTypeUInt8
:
return
&ffi_type_uint8;
case
kMCForeignPrimitiveTypeSInt8
:
return
&ffi_type_sint8;
case
kMCForeignPrimitiveTypeUInt16
:
return
&ffi_type_uint16;
case
kMCForeignPrimitiveTypeSInt16
:
return
&ffi_type_sint16;
case
kMCForeignPrimitiveTypeUInt32
:
return
&ffi_type_uint32;
case
kMCForeignPrimitiveTypeSInt32
:
return
&ffi_type_sint32;
case
kMCForeignPrimitiveTypeUInt64
:
return
&ffi_type_uint64;
case
kMCForeignPrimitiveTypeSInt64
:
return
&ffi_type_sint64;
case
kMCForeignPrimitiveTypeFloat32
:
return
&ffi_type_float;
case
kMCForeignPrimitiveTypeFloat64
:
return
&ffi_type_double;
case
kMCForeignPrimitiveTypePointer
:
return
&ffi_type_pointer;
}
MCUnreachable
();
return
nil;
}
static
bool
__MCForeignTypeInfoComputeLayoutType
(MCTypeInfoRef self)
{
//
If the typeinfo has a layout size of size 1, then it is just a value.
if
(self -> foreign . descriptor . layout_size ==
1
)
self -> foreign . ffi_layout_type =
__map_primitive_type
(self -> foreign . descriptor . layout[
0
]);
else
{
ffi_type *t_type;
if
(!
MCMemoryNew
(t_type))
return
false
;
if
(!
MCMemoryNewArray
(self -> foreign . descriptor . layout_size +
1
, t_type -> elements))
{
MCMemoryDelete
(t_type);
return
false
;
}
t_type -> alignment =
0
;
t_type -> type =
FFI_TYPE_STRUCT
;
for
(
uindex_t
i =
0
; i < self -> foreign . descriptor . layout_size; i++)
t_type -> elements[i] =
__map_primitive_type
(self -> foreign . descriptor . layout[i]);
t_type -> elements[self -> foreign . descriptor . layout_size] =
NULL
;
self -> foreign . ffi_layout_type = t_type;
}
return
true
;
}
MC_DLLEXPORT_DEF
bool
MCForeignTypeInfoCreate
(
const
MCForeignTypeDescriptor *p_descriptor, MCTypeInfoRef& r_typeinfo)
{
MCAssert
(nil != p_descriptor);
__MCTypeInfo *self;
if
(!
__MCValueCreate
(
kMCValueTypeCodeTypeInfo
, self))
return
false
;
if
(!
MCMemoryNewArray
(p_descriptor -> layout_size, self -> foreign . descriptor . layout, self -> foreign . descriptor . layout_size))
{
MCValueRelease
(self);
return
false
;
}
self -> flags |=
kMCTypeInfoTypeIsForeign
;
self -> foreign . descriptor . size = p_descriptor -> size;
self -> foreign . descriptor . basetype =
MCValueRetain
(p_descriptor -> basetype);
self -> foreign . descriptor . bridgetype =
MCValueRetain
(p_descriptor -> bridgetype);
MCMemoryCopy
(self -> foreign . descriptor . layout, p_descriptor -> layout, p_descriptor -> layout_size *
sizeof
(self -> foreign . descriptor . layout[
0
]));
self -> foreign . descriptor . initialize = p_descriptor -> initialize;
self -> foreign . descriptor . finalize = p_descriptor -> finalize;
self -> foreign . descriptor .
defined
= p_descriptor ->
defined
;
self -> foreign . descriptor . move = p_descriptor -> move;
self -> foreign . descriptor . copy = p_descriptor -> copy;
self -> foreign . descriptor . equal = p_descriptor -> equal;
self -> foreign . descriptor . hash = p_descriptor -> hash;
self -> foreign . descriptor . doimport = p_descriptor -> doimport;
self -> foreign . descriptor . doexport = p_descriptor -> doexport;
self -> foreign . descriptor . describe = p_descriptor -> describe;
self -> foreign . descriptor . promotedtype =
MCValueRetain
(p_descriptor->
promotedtype
);
self -> foreign . descriptor . promote = p_descriptor -> promote;
if
(!
__MCForeignTypeInfoComputeLayoutType
(self))
{
MCValueRelease
(self);
return
false
;
}
if
(
MCValueInterAndRelease
(self, r_typeinfo))
return
true
;
MCValueRelease
(self);
return
false
;
}
MC_DLLEXPORT_DEF
const
MCForeignTypeDescriptor *
MCForeignTypeInfoGetDescriptor
(MCTypeInfoRef unresolved_self)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
(
MCTypeInfoIsForeign
(self));
return
&self -> foreign . descriptor;
}
MC_DLLEXPORT_DEF
void
*
MCForeignTypeInfoGetLayoutType
(MCTypeInfoRef unresolved_self)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
(
MCTypeInfoIsForeign
(self));
return
self -> foreign . ffi_layout_type;
}
//
//////////////////////////////////////////////////////////////////////////////
MC_DLLEXPORT_DEF
bool
MCRecordTypeInfoCreate
(
const
MCRecordTypeFieldInfo *p_fields,
index_t
p_field_count, MCTypeInfoRef& r_typeinfo)
{
MCAssert
(nil != p_fields || p_field_count ==
0
);
/*
If the p_field_count < 0 then the p_fields are expected to be
* terminated by a custodian with name = nil.
*/
if
(p_field_count <
0
)
for
(p_field_count =
0
; p_fields[p_field_count].
name
!= nil; ++p_field_count);
__MCTypeInfo *self;
if
(!
__MCValueCreate
(
kMCValueTypeCodeTypeInfo
, self))
return
false
;
if
(!
MCMemoryNewArray
(p_field_count, self -> record . fields))
{
MCMemoryDelete
(self);
return
false
;
}
self -> flags |=
kMCValueTypeCodeRecord
;
for
(
index_t
i =
0
; i < p_field_count; ++i)
{
__MCAssertIsName
(p_fields[i].
name
);
__MCAssertIsTypeInfo
(p_fields[i].
type
);
/*
Verify that the field names are all caselessly distinct.
* N.b. O(N^2) algorithm is inefficient, but will only be run
* in debug builds and will only happen once per type.
*/
for
(
index_t
j =
0
; j < i; ++j)
{
MCAssert
(!
MCNameIsEqualToCaseless
(p_fields[i] . name, p_fields[j] . name));
}
self -> record . fields[i] . name =
MCValueRetain
(p_fields[i] . name);
self -> record . fields[i] . type =
MCValueRetain
(p_fields[i] . type);
}
self -> record . field_count = p_field_count;
if
(
MCValueInterAndRelease
(self, r_typeinfo))
return
true
;
MCValueRelease
(self);
return
false
;
}
MC_DLLEXPORT_DEF
uindex_t
MCRecordTypeInfoGetFieldCount
(MCTypeInfoRef unresolved_self)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
(
MCTypeInfoIsRecord
(self));
return
__MCRecordTypeInfoGetFieldCount
(self);
}
uindex_t
__MCRecordTypeInfoGetFieldCount
(MCTypeInfoRef self)
{
return
self->
record
.
field_count
;
}
MC_DLLEXPORT_DEF
MCNameRef
MCRecordTypeInfoGetFieldName
(MCTypeInfoRef unresolved_self,
uindex_t
p_index)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
((self -> flags &
kMCTypeInfoTypeCodeMask
) ==
kMCValueTypeCodeRecord
);
MCAssert
(p_index < self->
record
.
field_count
);
return
self -> record . fields[p_index] . name;
}
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCRecordTypeInfoGetFieldType
(MCTypeInfoRef unresolved_self,
uindex_t
p_index)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
((self -> flags &
kMCTypeInfoTypeCodeMask
) ==
kMCValueTypeCodeRecord
);
MCAssert
(p_index < self->
record
.
field_count
);
return
self -> record . fields[p_index] . type;
}
//
//////////////////////////////////////////////////////////////////////////////
static
bool
MCCommonHandlerTypeInfoCreate
(
bool
p_is_foreign,
const
MCHandlerTypeFieldInfo *p_fields,
index_t
p_field_count, MCTypeInfoRef p_return_type, MCTypeInfoRef& r_typeinfo)
{
__MCAssertIsTypeInfo
(p_return_type);
MCAssert
(nil != p_fields ||
0
== p_field_count);
__MCTypeInfo *self;
if
(!
__MCValueCreate
(
kMCValueTypeCodeTypeInfo
, self))
return
false
;
/*
If the p_field_count < 0 then the p_fields are expected to be
* terminated by a custodian with name = nil.
*/
if
(p_field_count <
0
)
for
(p_field_count =
0
; p_fields[p_field_count].
type
!= nil; ++p_field_count);
if
(!
MCMemoryNewArray
(p_field_count, self -> handler . fields))
{
MCMemoryDelete
(self);
return
false
;
}
self -> flags |=
kMCValueTypeCodeHandler
;
if
(p_is_foreign)
self -> flags |=
kMCTypeInfoFlagHandlerIsForeign
;
for
(
index_t
i =
0
; i < p_field_count; ++i)
{
__MCAssertIsTypeInfo
(p_fields[i].
type
);
if
(p_fields[i].
mode
==
kMCHandlerTypeFieldModeVariadic
)
{
if
(i ==
0
|| p_field_count != i +
1
)
{
MCValueRelease
(self);
return
MCErrorThrowGeneric
(
MCSTR
(
"
Variadic parameter cannot be first, and must be last
"
));
}
p_field_count = i;
self->
flags
|=
kMCTypeInfoFlagHandlerIsVariadic
;
break
;
}
self -> handler . fields[i] . type =
MCValueRetain
(p_fields[i] . type);
self -> handler . fields[i] . mode = p_fields[i] . mode;
}
self -> handler . field_count = p_field_count;
self -> handler . return_type =
MCValueRetain
(p_return_type);
self -> handler . layout_args= nil;
self -> handler . layouts = nil;
if
(
MCValueInterAndRelease
(self, r_typeinfo))
return
true
;
MCValueRelease
(self);
return
false
;
}
MC_DLLEXPORT_DEF
bool
MCHandlerTypeInfoCreate
(
const
MCHandlerTypeFieldInfo *p_fields,
index_t
p_field_count, MCTypeInfoRef p_return_type, MCTypeInfoRef& r_typeinfo)
{
return
MCCommonHandlerTypeInfoCreate
(
false
, p_fields, p_field_count, p_return_type, r_typeinfo);
}
MC_DLLEXPORT_DEF
bool
MCForeignHandlerTypeInfoCreate
(
const
MCHandlerTypeFieldInfo *p_fields,
index_t
p_field_count, MCTypeInfoRef p_return_type, MCTypeInfoRef& r_typeinfo)
{
return
MCCommonHandlerTypeInfoCreate
(
true
, p_fields, p_field_count, p_return_type, r_typeinfo);
}
MC_DLLEXPORT_DEF
bool
MCHandlerTypeInfoIsForeign
(MCTypeInfoRef unresolved_self)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
(
MCTypeInfoIsHandler
(self));
return
(self -> flags &
kMCTypeInfoFlagHandlerIsForeign
) !=
0
;
}
MC_DLLEXPORT_DEF
bool
MCHandlerTypeInfoIsVariadic
(MCTypeInfoRef unresolved_self)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
(
MCTypeInfoIsHandler
(self));
return
(self -> flags &
kMCTypeInfoFlagHandlerIsVariadic
) !=
0
;
}
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCHandlerTypeInfoGetReturnType
(MCTypeInfoRef unresolved_self)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
((self -> flags &
kMCTypeInfoTypeCodeMask
) ==
kMCValueTypeCodeHandler
);
return
self -> handler . return_type;
}
MC_DLLEXPORT_DEF
uindex_t
MCHandlerTypeInfoGetParameterCount
(MCTypeInfoRef unresolved_self)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
((self -> flags &
kMCTypeInfoTypeCodeMask
) ==
kMCValueTypeCodeHandler
);
return
self -> handler . field_count;
}
MC_DLLEXPORT_DEF
MCHandlerTypeFieldMode
MCHandlerTypeInfoGetParameterMode
(MCTypeInfoRef unresolved_self,
uindex_t
p_index)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
((self -> flags &
kMCTypeInfoTypeCodeMask
) ==
kMCValueTypeCodeHandler
);
MCAssert
(self -> handler . field_count > p_index);
return
self -> handler . fields[p_index] . mode;
}
MC_DLLEXPORT_DEF
MCTypeInfoRef
MCHandlerTypeInfoGetParameterType
(MCTypeInfoRef unresolved_self,
uindex_t
p_index)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
((self -> flags &
kMCTypeInfoTypeCodeMask
) ==
kMCValueTypeCodeHandler
);
MCAssert
(self -> handler . field_count > p_index);
return
self -> handler . fields[p_index] . type;
}
bool
MCHandlerTypeInfoGetLayoutType
(MCTypeInfoRef unresolved_self,
int
p_abi,
void
*& r_cif)
{
MCTypeInfoRef self;
self =
__MCTypeInfoResolve
(unresolved_self);
MCAssert
((self -> flags &
kMCTypeInfoTypeCodeMask
) ==
kMCValueTypeCodeHandler
);
//
If a layout for the given ABI already exists, then return it.
for
(MCHandlerTypeLayout *t_layout = self -> handler . layouts; t_layout != nil; t_layout = t_layout -> next)
if
(t_layout -> abi == p_abi)
{
r_cif = &t_layout -> cif;
return
true
;
}
//
If we haven't computed the layout args yet, do so.
if
(self -> handler . layout_args == nil)
{
MCTypeInfoRef t_return_type;
t_return_type = self -> handler . return_type;
MCResolvedTypeInfo t_resolved_return_type;
if
(!
MCTypeInfoResolve
(t_return_type, t_resolved_return_type))
return
MCErrorThrowUnboundType
(t_return_type);
ffi_type *t_ffi_return_type;
if
(t_resolved_return_type.
named_type
!=
kMCNullTypeInfo
)
{
if
(
MCTypeInfoIsForeign
(t_resolved_return_type . type))
t_ffi_return_type = (ffi_type *)
MCForeignTypeInfoGetLayoutType
(t_resolved_return_type . type);
else
t_ffi_return_type = &ffi_type_pointer;
}
else
t_ffi_return_type = &ffi_type_void;
uindex_t
t_arity;
t_arity = self -> handler . field_count;
//
We need arity + 1 ffi_type slots, as we use the first slot to store
//
the return type (if any).
MCAutoPointer<ffi_type*[]> t_ffi_arg_types =
new
(std::nothrow) ffi_type*[t_arity +
1
];
if
(!t_ffi_arg_types)
return
false
;
t_ffi_arg_types[
0
] = t_ffi_return_type;
for
(
uindex_t
i =
0
; i < t_arity; i++)
{
MCTypeInfoRef t_type;
MCHandlerTypeFieldMode t_mode;
t_type = self -> handler . fields[i] . type;
t_mode = self -> handler . fields[i] . mode;
MCResolvedTypeInfo t_resolved_type;
if
(!
MCTypeInfoResolve
(t_type, t_resolved_type))
return
MCErrorThrowUnboundType
(t_type);
if
(t_mode ==
kMCHandlerTypeFieldModeIn
)
{
if
(
MCTypeInfoIsForeign
(t_resolved_type . type))
t_ffi_arg_types[i +
1
] = (ffi_type *)
MCForeignTypeInfoGetLayoutType
(t_resolved_type . type);
else
t_ffi_arg_types[i +
1
] = &ffi_type_pointer;
}
else
t_ffi_arg_types[i +
1
] = &ffi_type_pointer;
}
self -> handler . layout_args = t_ffi_arg_types.
Release
();
}
//
Now we must create a new layout object.
MCHandlerTypeLayout *t_layout;
if
(!
MCMemoryAllocate
(
sizeof
(MCHandlerTypeLayout) +
sizeof
(ffi_cif), t_layout))
return
false
;
t_layout -> abi = p_abi;
if
(
ffi_prep_cif
((ffi_cif *)&t_layout -> cif, (ffi_abi)p_abi, self -> handler . field_count, self -> handler . layout_args[
0
], self -> handler . layout_args +
1
) !=
FFI_OK
)
{
MCMemoryDeallocate
(t_layout);
return
MCErrorThrowGeneric
(
MCSTR
(
"
unexpected libffi failure
"
));
}
t_layout -> next = self -> handler . layouts;
self -> handler . layouts = t_layout;
View remainder of file in raw view
Back
|
FazBrowse Home
|
New Git URL