This repository has been archived by the owner on Apr 20, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 15
/
TypeDef.cs
3310 lines (2883 loc) · 126 KB
/
TypeDef.cs
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
//
// CLR AST type definitions
//
using System;
using System.Linq;
using Microsoft.LiveLabs.Extras;
namespace Microsoft.LiveLabs.CST
{
public enum TypeDefFlavor
{
Pointer,
CodePointer,
Array,
MultiDimArray,
Box,
Null,
Parameter,
Struct,
Void,
Number,
Handle,
Nullable,
Enum,
Delegate,
Class,
Object,
String,
Interface,
GenericIEnumerable
}
// ----------------------------------------------------------------------
// TypeDef
// ----------------------------------------------------------------------
public abstract class TypeDef
{
[CanBeNull] // currently always null
public readonly Location Loc;
[NotNull]
public readonly IImSeq<Annotation> Annotations;
[NotNull]
public readonly ISeq<CustomAttribute> CustomAttributes;
// Determined when checking resolvability
[CanBeNull] // null => not yet analyzed
protected Set<QualifiedTypeName> usedTypes;
// Determined when transpose
[CanBeNull] // null => not yet transposed
protected Set<QualifiedTypeName> usedByTypes;
[CanBeNull] // null => not yet transposed
protected Set<QualifiedMemberName> usedByMembers;
[CanBeNull] // null => not yet checked, or checked and is valid
public InvalidInfo Invalid;
public bool IsUsed;
protected TypeDef(IImSeq<Annotation> annotations, ISeq<CustomAttribute> customAttributes)
{
Annotations = annotations ?? Constants.EmptyAnnotations;
CustomAttributes = customAttributes ?? new Seq<CustomAttribute>();
}
public abstract TypeDefFlavor Flavor { get; }
public abstract TypeStyle Style { get; }
public abstract TypeName EffectiveName(Global global);
public abstract int Arity { get; }
public abstract IImSeq<ParameterTypeDef> Parameters { get; }
public abstract IImSeq<MemberDef> Members { get; }
public abstract bool HasMember(Signature signature);
public abstract MemberDef ResolveMember(Signature signature);
public MethodDef ResolveMethod(MethodSignature signature)
{
return (MethodDef)ResolveMember(signature);
}
public FieldDef ResolveField(FieldSignature signature)
{
return (FieldDef)ResolveMember(signature);
}
public PropertyDef ResolveProperty(PropertySignature signature)
{
return (PropertyDef)ResolveMember(signature);
}
public EventDef ResolveEvent(EventSignature signature)
{
return (EventDef)ResolveMember(signature);
}
public abstract MemberDef OuterPropertyOrEvent(MethodSignature signature);
public abstract TypeRef Extends { get; }
public abstract IImSeq<TypeRef> Implements { get; }
public abstract bool IsSealed { get; }
public abstract bool IsAbstract { get; }
public abstract bool IsModule { get; }
public bool IsAttributeType(Global global, AssemblyDef assemblyDef)
{
if (Extends == null)
return false;
if (Extends.Equals(global.AttributeRef))
return true;
var extAssemblyDef = default(AssemblyDef);
var extTypeDef = default(TypeDef);
if (Extends.PrimTryResolve(global, out extAssemblyDef, out extTypeDef))
return extTypeDef.IsAttributeType(global, extAssemblyDef);
return false;
}
// The method of a base type which method with signature overrides, or null if no such override.
// Result is from p.o.v. from inside this type.
public abstract PolymorphicMethodRef OverriddenMethod(MethodSignature signature);
//
// References and environments
//
public abstract TypeRef PrimReference(Global global, AssemblyDef assemblyDef, IImSeq<TypeRef> typeBoundArguments);
public QualifiedTypeName QualifiedTypeName(Global global, AssemblyDef assemblyDef)
{
return new QualifiedTypeName(assemblyDef.Name, EffectiveName(global));
}
public TypeRef SelfReference(AssemblyEnvironment assmEnv)
{
var typeBoundArguments = default(Seq<TypeRef>);
if (Arity > 0)
{
typeBoundArguments = new Seq<TypeRef>(Arity);
for (var i = 0; i < Arity; i++)
typeBoundArguments.Add(new ParameterTypeRef(ParameterFlavor.Type, i));
}
return PrimReference(assmEnv.Global, assmEnv.Assembly, typeBoundArguments);
}
//
// Validity
//
public IImSet<QualifiedTypeName> UsedTypes { get { return usedTypes; } }
public IImSet<QualifiedTypeName> UsedByTypes { get { return usedByTypes; } }
public IImSet<QualifiedMemberName> UsedByMembers { get { return usedByMembers; } }
internal virtual void AccumUsedTypeDefs(ValidityContext vctxt, AssemblyDef assemblyDef, bool includeAttributes)
{
if (usedTypes == null)
usedTypes = new Set<QualifiedTypeName>();
var ctxt = MessageContextBuilders.Type(vctxt.Global, assemblyDef, this);
foreach (var a in CustomAttributes)
a.Type.AccumUsedTypeDefs(vctxt, ctxt, usedTypes);
}
internal virtual bool MarkAsUsed(ValidityContext vctxt, AssemblyDef assemblyDef)
{
if (IsUsed || Invalid != null)
return false;
IsUsed = true;
if (usedTypes == null)
{
AccumUsedTypeDefs(vctxt, assemblyDef, vctxt.IncludeAttributes(assemblyDef, this));
if (Invalid != null)
return true;
}
foreach (var r in usedTypes)
{
var usedAssemblyDef = default(AssemblyDef);
var usedTypeDef = default(TypeDef);
if (r.PrimTryResolve(vctxt.Global, out usedAssemblyDef, out usedTypeDef))
usedTypeDef.MarkAsUsed(vctxt, usedAssemblyDef);
}
return true;
}
internal void UsedBy(QualifiedTypeName name)
{
if (usedByTypes == null)
usedByTypes = new Set<QualifiedTypeName>();
usedByTypes.Add(name);
}
internal void UsedBy(QualifiedMemberName name)
{
if (usedByMembers == null)
usedByMembers = new Set<QualifiedMemberName>();
usedByMembers.Add(name);
}
internal void TopologicalAllDeps(Global global, AssemblyDef assemblyDef, Set<QualifiedTypeName> visitedTypeDefs, Seq<QualifiedTypeName> sortedTypeDefs)
{
var self = QualifiedTypeName(global, assemblyDef);
if (visitedTypeDefs.Contains(self))
return;
visitedTypeDefs.Add(self);
if (usedTypes == null)
return;
foreach (var r in usedTypes)
{
var usedAssemblyDef = default(AssemblyDef);
var usedTypeDef = default(TypeDef);
if (r.PrimTryResolve(global, out usedAssemblyDef, out usedTypeDef))
{
usedTypeDef.UsedBy(self);
usedTypeDef.TopologicalAllDeps(global, usedAssemblyDef, visitedTypeDefs, sortedTypeDefs);
}
}
sortedTypeDefs.Add(self);
}
internal void UsedByTypesClosure(Global global, AssemblyDef assemblyDef, Set<QualifiedTypeName> visitedTypeDefs, Seq<QualifiedTypeName> scc)
{
if (usedByTypes == null)
return;
var self = QualifiedTypeName(global, assemblyDef);
if (!visitedTypeDefs.Contains(self))
{
visitedTypeDefs.Add(self);
scc.Add(self);
foreach (var r in usedByTypes)
{
var usedByAssemblyDef = default(AssemblyDef);
var usedByTypeDef = default(TypeDef);
if (r.PrimTryResolve(global, out usedByAssemblyDef, out usedByTypeDef))
usedByTypeDef.UsedByTypesClosure(global, usedByAssemblyDef, visitedTypeDefs, scc);
}
}
}
internal virtual void PropogateInvalidity(Global global, AssemblyDef assemblyDef)
{
if (Invalid == null)
return;
var self = PrimReference(global, assemblyDef, null);
if (usedByTypes != null)
{
foreach (var r in usedByTypes)
{
var usedByAssemblyDef = default(AssemblyDef);
var usedByTypeDef = default(TypeDef);
if (r.PrimTryResolve(global, out usedByAssemblyDef, out usedByTypeDef))
{
if (usedByTypeDef.Invalid == null)
usedByTypeDef.Invalid = new InvalidInfo(MessageContextBuilders.Type(global, self), Invalid);
}
}
}
if (usedByMembers != null)
{
foreach (var r in usedByMembers)
{
var usedByAssemblyDef = default(AssemblyDef);
var usedByTypeDef = default(TypeDef);
var usedByMemberDef = default(MemberDef);
if (r.PrimTryResolve(global, out usedByAssemblyDef, out usedByTypeDef, out usedByMemberDef))
{
if (usedByMemberDef.Invalid == null)
usedByMemberDef.Invalid = new InvalidInfo(MessageContextBuilders.Type(global, self), Invalid);
}
}
}
}
internal abstract void CheckValid(ValidityContext vctxt, TypeEnvironment typeEnv);
internal void TopologicalTypeInit(Global global, AssemblyDef assemblyDef, Set<QualifiedTypeName> visitedTypeDefs, Seq<QualifiedTypeName> sortedTypeDefs)
{
var self = QualifiedTypeName(global, assemblyDef);
if (visitedTypeDefs.Contains(self))
return;
visitedTypeDefs.Add(self);
if (!IsUsed || Invalid != null)
return;
var cctorDef = Members.OfType<MethodDef>().Where(m => m.IsStatic && m.IsConstructor).FirstOrDefault();
if (cctorDef != null)
{
var visitedMemberDefs = new Set<QualifiedMemberName>();
cctorDef.TopologicalTypeInit
(global, assemblyDef, this, visitedTypeDefs, sortedTypeDefs, visitedMemberDefs);
}
sortedTypeDefs.Add(self);
}
//
// Type checking
//
internal abstract TypeRef PrimExtends(RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments);
internal abstract IImSeq<TypeRef> PrimImplements(RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments);
protected virtual bool InCodePointer { get { return false; } }
// Is it valid to instantiate this type at given ground type arguments?
internal bool PrimAreValidArguments(ValidityContext vctxt, MessageContext ctxt, TypeRef originalType, RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments)
{
if (Parameters.Count > 0 && groundTypeBoundArguments.Count == 0)
// Type constructor is ok
return true;
if (Parameters.Count != groundTypeBoundArguments.Count)
{
vctxt.Log
(new InvalidTypeRef
(ctxt,
originalType,
String.Format
("Type has {0} type parameters but is given {1} type arguments",
Parameters.Count,
groundTypeBoundArguments.Count)));
return false;
}
return Parameters.All
(p =>
p.PrimIsValidParameterBinding
(vctxt, ctxt, originalType, groundEnv, groundTypeBoundArguments, Constants.EmptyTypeRefs, InCodePointer));
}
internal abstract bool PrimIsSameDefinition(AssemblyDef thisAssembly, AssemblyDef otherAssembly, TypeDef otherDef);
// Does this type, instantiated at the given ground type arguments, respect the given type parameter constraint?
internal abstract bool PrimInstanceRespectsParameterConstraint(ValidityContext vctxt, MessageContext ctxt, TypeRef originalType, RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments, ParameterConstraint constraint, bool inCodePointer);
internal virtual bool PrimInstanceIsAssignableTo
(RootEnvironment groundEnv,
AssemblyDef thisAssembly,
IImSeq<TypeRef> thisGroundTypeBoundArguments,
AssemblyDef otherAssembly,
TypeDef otherDef,
IImSeq<TypeRef> otherGroundTypeBoundArguments)
{
if (thisGroundTypeBoundArguments.Count != Arity || otherGroundTypeBoundArguments.Count != otherDef.Arity)
// No subtyping relation over higher-kinded types
return false;
if (PrimIsSameDefinition(thisAssembly, otherAssembly, otherDef))
{
// A type is assignable to itself only if all type arguments pairwise match according to
// the in-/co-/contra-variance constraints
var ps = Parameters;
for (var i = 0; i < ps.Count; i++)
{
var p = ps[i];
switch (p.Variance)
{
case ParameterVariance.Invariant:
if (!thisGroundTypeBoundArguments[i].PrimIsEquivalentTo(groundEnv, otherGroundTypeBoundArguments[i]))
return false;
break;
case ParameterVariance.Covariant:
if (!thisGroundTypeBoundArguments[i].PrimIsAssignableTo(groundEnv, otherGroundTypeBoundArguments[i]))
return false;
break;
case ParameterVariance.Contravariant:
if (!otherGroundTypeBoundArguments[i].PrimIsAssignableTo(groundEnv, thisGroundTypeBoundArguments[i]))
return false;
break;
default:
throw new ArgumentOutOfRangeException();
}
}
return true;
}
if (otherDef.Style is NullTypeStyle)
// A type is assignable to null only if it is a reference type
return Style is ReferenceTypeStyle;
if (otherDef.Style is InterfaceTypeStyle)
{
// A type can be assigned to an interface type if one of the interfaces it implements
// can be assigned to that interface type
if (PrimImplements(groundEnv, thisGroundTypeBoundArguments).Any
(groundImplements =>
{
var implEnv = groundImplements.Enter(groundEnv);
return implEnv.Type.PrimInstanceIsAssignableTo
(groundEnv,
implEnv.Assembly,
groundImplements.Arguments,
otherAssembly,
otherDef,
otherGroundTypeBoundArguments);
}))
return true;
}
// A type is assignable to its base type, which may be assignable to the r.h.s. type
var groundExtends = PrimExtends(groundEnv, thisGroundTypeBoundArguments);
if (groundExtends != null)
{
var extendsEnv = groundExtends.Enter(groundEnv);
if (extendsEnv.Type.PrimInstanceIsAssignableTo
(groundEnv,
extendsEnv.Assembly,
groundExtends.Arguments,
otherAssembly,
otherDef,
otherGroundTypeBoundArguments))
return true;
}
return false;
}
//
// Pretty printing
//
public abstract void AppendDefinition(CSTWriter w);
protected void AppendCustomAttributes(CSTWriter w)
{
foreach (var ca in CustomAttributes)
{
ca.Append(w);
w.EndLine();
}
}
public static void AppendArguments(CSTWriter w, IImSeq<TypeRef> arguments)
{
if (arguments != null && arguments.Count > 0)
{
w.Append('<');
for (var i = 0; i < arguments.Count; i++)
{
if (i > 0)
w.Append(',');
arguments[i].Append(w);
}
w.Append('>');
}
// else: reference to higher-kinded type constructor
}
public override string ToString()
{
return CSTWriter.WithAppendDebug(w => EffectiveName(w.Global).Append(w));
}
}
// ----------------------------------------------------------------------
// BuiltinTypeDef
// ----------------------------------------------------------------------
public abstract class BuiltinTypeDef : TypeDef
{
// INVARIANT: Custom attributes must be empty
protected BuiltinTypeDef(IImSeq<Annotation> annotations)
: base(annotations, null)
{
}
public override TypeRef PrimReference(Global global, AssemblyDef assemblyDef, IImSeq<TypeRef> typeBoundArguments)
{
return new NamedTypeRef
(new QualifiedTypeName(assemblyDef.Name, EffectiveName(global)), typeBoundArguments);
}
public abstract uint Tag { get; }
public override IImSeq<MemberDef> Members { get { return Constants.EmptyMemberDefs; } }
public override bool HasMember(Signature signature)
{
return false;
}
internal override void CheckValid(ValidityContext vctxt, TypeEnvironment typeEnv)
{
}
public override MemberDef ResolveMember(Signature signature)
{
return null;
}
public override MemberDef OuterPropertyOrEvent(MethodSignature signature)
{
return null;
}
public override PolymorphicMethodRef OverriddenMethod(MethodSignature signature)
{
throw new InvalidOperationException("no method with matching signature in type");
}
public override TypeRef Extends { get { return null; } }
public override IImSeq<TypeRef> Implements { get { return Constants.EmptyTypeRefs; } }
public override bool IsSealed { get { return true; } }
public override bool IsAbstract { get { return false; } }
public override bool IsModule { get { return false; } }
public abstract void AppendReference(CSTWriter w, IImSeq<TypeRef> typeBoundArguments);
}
// ----------------------------------------------------------------------
// PointerTypeDef
// ----------------------------------------------------------------------
public enum PointerFlavor
{
Unmanaged,
Managed
}
public class PointerTypeDef : BuiltinTypeDef
{
private readonly IImSeq<ParameterTypeDef> parameters;
public readonly PointerFlavor PointerFlavor;
public PointerTypeDef(IImSeq<Annotation> annotations, PointerFlavor pointerFlavor)
: base(annotations)
{
parameters = new Seq<ParameterTypeDef>
{
new ParameterTypeDef
(null,
null,
null,
null,
ParameterFlavor.Type,
0,
ParameterVariance.Invariant,
ParameterConstraint.Unconstrained)
};
PointerFlavor = pointerFlavor;
}
public override uint Tag
{
get
{
switch (PointerFlavor)
{
case PointerFlavor.Managed:
return 0x075372c9u;
case PointerFlavor.Unmanaged:
return 0xa4842004u;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public override TypeDefFlavor Flavor { get { return TypeDefFlavor.Pointer; } }
public override TypeStyle Style
{
get
{
switch (PointerFlavor)
{
case PointerFlavor.Unmanaged:
return TypeStyles.UnmanagedPointer;
case PointerFlavor.Managed:
return TypeStyles.ManagedPointer;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public override TypeName EffectiveName(Global global)
{
switch (PointerFlavor)
{
case PointerFlavor.Managed:
return global.ManagedPointerTypeConstructorRef.QualifiedTypeName.Type;
case PointerFlavor.Unmanaged:
return global.UnmanagedPointerTypeConstructorRef.QualifiedTypeName.Type;
default:
throw new ArgumentOutOfRangeException();
}
}
public override int Arity { get { return 1; } }
public override IImSeq<ParameterTypeDef> Parameters { get { return parameters; } }
internal override TypeRef PrimExtends(RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments)
{
return null;
}
internal override IImSeq<TypeRef> PrimImplements(RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments)
{
return Constants.EmptyTypeRefs;
}
internal override bool PrimIsSameDefinition(AssemblyDef thisAssembly, AssemblyDef otherAssembly, TypeDef otherDef)
{
var otherPointerDef = otherDef as PointerTypeDef;
return otherPointerDef != null && PointerFlavor == otherPointerDef.PointerFlavor;
}
internal override bool PrimInstanceRespectsParameterConstraint(ValidityContext vctxt, MessageContext ctxt, TypeRef originalType, RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments, ParameterConstraint constraint, bool inCodePointer)
{
if (inCodePointer)
return true;
else
{
vctxt.Log
(new InvalidTypeRef
(ctxt, originalType, "A pointer type cannot be used to instantiate a type parameter"));
return false;
}
}
private static bool PrimIsVoidPointer(RootEnvironment groundEnv, PointerTypeDef pointerTypeDef, IImSeq<TypeRef> groundTypeBoundArguments)
{
while (true)
{
if (pointerTypeDef.PointerFlavor != PointerFlavor.Unmanaged)
return false;
var elemEnv = groundTypeBoundArguments[0].Enter(groundEnv);
if (elemEnv.Type.Style is VoidTypeStyle)
return true;
pointerTypeDef = elemEnv.Type as PointerTypeDef;
if (pointerTypeDef == null)
return false;
groundTypeBoundArguments = groundTypeBoundArguments[0].Arguments;
}
}
internal override bool PrimInstanceIsAssignableTo(RootEnvironment groundEnv, AssemblyDef thisAssembly, IImSeq<TypeRef> thisGroundThisThisGroundTypeArguments, AssemblyDef otherAssembly, TypeDef otherDef, IImSeq<TypeRef> otherGroundGroundTypeArguments)
{
var otherPointerDef = otherDef as PointerTypeDef;
if (otherPointerDef == null || PointerFlavor != otherPointerDef.PointerFlavor)
return false;
// Any unmanaged pointer type may be assigned to Void*^n for n > 0
if (PrimIsVoidPointer(groundEnv, otherPointerDef, otherGroundGroundTypeArguments))
return true;
// No variance
return thisGroundThisThisGroundTypeArguments[0].PrimIsEquivalentTo(groundEnv, otherGroundGroundTypeArguments[0]);
}
public override void AppendDefinition(CSTWriter w)
{
switch (PointerFlavor)
{
case PointerFlavor.Managed:
w.Append("<builtin managed pointer type constructor>");
break;
case PointerFlavor.Unmanaged:
w.Append("<builtin unmanaged pointer type constructor>");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public override void AppendReference(CSTWriter w, IImSeq<TypeRef> typeBoundArguments)
{
if (w.Style == WriterStyle.Uniform || typeBoundArguments == null)
{
switch (PointerFlavor)
{
case PointerFlavor.Managed:
w.Global.ManagedPointerTypeConstructorRef.QualifiedTypeName.Append(w);
break;
case PointerFlavor.Unmanaged:
w.Global.UnmanagedPointerTypeConstructorRef.QualifiedTypeName.Append(w);
break;
default:
throw new ArgumentOutOfRangeException();
}
AppendArguments(w, typeBoundArguments);
}
else
{
typeBoundArguments[0].Append(w);
switch (PointerFlavor)
{
case PointerFlavor.Managed:
w.Append('&');
break;
case PointerFlavor.Unmanaged:
w.Append('*');
break;
default:
throw new ArgumentOutOfRangeException();
}
}
}
}
// ----------------------------------------------------------------------
// CodePointerTypeDef
// ----------------------------------------------------------------------
public enum CodePointerFlavor
{
Function,
Action
}
public class CodePointerTypeDef : BuiltinTypeDef
{
public readonly CodePointerFlavor CodePointerFlavor;
[NotNull]
private readonly IImSeq<ParameterTypeDef> parameters;
public CodePointerTypeDef(IImSeq<Annotation> annotations, CodePointerFlavor codePointerFlavor, int arity)
: base(annotations)
{
CodePointerFlavor = codePointerFlavor;
var parameters = new Seq<ParameterTypeDef>();
for (var i = 0; i < arity; i++)
parameters.Add(new ParameterTypeDef(null, null, null, null, ParameterFlavor.Type, i, ParameterVariance.Invariant, ParameterConstraint.Unconstrained));
this.parameters = parameters;
}
public override uint Tag
{
get
{
switch (CodePointerFlavor)
{
case CodePointerFlavor.Function:
return 0x69c8f04au;
case CodePointerFlavor.Action:
return 0x68fb6fafu;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public override TypeDefFlavor Flavor { get { return TypeDefFlavor.CodePointer; } }
public override TypeStyle Style
{
get
{
switch (CodePointerFlavor)
{
case CodePointerFlavor.Function:
return TypeStyles.Function;
case CodePointerFlavor.Action:
return TypeStyles.Action;
default:
throw new ArgumentOutOfRangeException();
}
}
}
public override TypeName EffectiveName(Global global)
{
return global.CodePointerTypeConstructorName(CodePointerFlavor, Arity).Type;
}
public override int Arity { get { return parameters.Count; } }
public override IImSeq<ParameterTypeDef> Parameters
{
get { return parameters; }
}
protected override bool InCodePointer { get { return true; } }
internal override TypeRef PrimExtends(RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments)
{
return null;
}
internal override IImSeq<TypeRef> PrimImplements(RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments)
{
return Constants.EmptyTypeRefs;
}
internal override bool PrimIsSameDefinition(AssemblyDef thisAssembly, AssemblyDef otherAssembly, TypeDef otherDef)
{
var otherCodePointerDef = otherDef as CodePointerTypeDef;
return otherCodePointerDef != null && CodePointerFlavor == otherCodePointerDef.CodePointerFlavor &&
parameters.Count == otherCodePointerDef.parameters.Count;
}
internal override bool PrimInstanceRespectsParameterConstraint(ValidityContext vctxt, MessageContext ctxt, TypeRef originalType, RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments, ParameterConstraint constraint, bool inCodePointer)
{
if (inCodePointer)
return true;
else
{
vctxt.Log
(new InvalidTypeRef
(ctxt, originalType, "A code pointer type cannot be used to instantiate a type parameter"));
return false;
}
}
public override void AppendDefinition(CSTWriter w)
{
switch (CodePointerFlavor)
{
case CodePointerFlavor.Function:
w.Append("<builtin function type constructor>");
break;
case CodePointerFlavor.Action:
w.Append("<builtin action type constructor>");
break;
default:
throw new ArgumentOutOfRangeException();
}
}
public override void AppendReference(CSTWriter w, IImSeq<TypeRef> typeBoundArguments)
{
if (w.Style == WriterStyle.Uniform || typeBoundArguments == null)
{
EffectiveName(w.Global).Append(w);
AppendArguments(w, typeBoundArguments);
}
else
{
w.Append("&(");
var last = default(int);
switch (CodePointerFlavor)
{
case CodePointerFlavor.Function:
last = 1;
break;
case CodePointerFlavor.Action:
last = 0;
break;
default:
throw new ArgumentOutOfRangeException();
}
for (var i = 0; i < typeBoundArguments.Count - last; i++)
{
if (i > 0)
w.Append(',');
typeBoundArguments[i].Append(w);
}
w.Append(')');
if (last > 0)
{
w.Append(':');
typeBoundArguments[typeBoundArguments.Count - last].Append(w);
}
}
}
}
// ----------------------------------------------------------------------
// ArrayTypeDef
// ----------------------------------------------------------------------
public class ArrayTypeDef : BuiltinTypeDef
{
private readonly TypeRef extends;
private readonly IImSeq<ParameterTypeDef> parameters;
public ArrayTypeDef(IImSeq<Annotation> annotations, Global global)
: base(annotations)
{
extends = global.ArrayRef;
parameters = new Seq<ParameterTypeDef>
{
new ParameterTypeDef
(null,
null,
null,
null,
ParameterFlavor.Type,
0,
ParameterVariance.Covariant,
ParameterConstraint.Unconstrained)
};
}
public override uint Tag { get { return 0x83f44239u; } }
public override TypeDefFlavor Flavor { get { return TypeDefFlavor.Array; } }
public override TypeStyle Style { get { return TypeStyles.Array; } }
public override TypeName EffectiveName(Global global)
{
return global.ArrayTypeConstructorRef.QualifiedTypeName.Type;
}
public override int Arity { get { return 1; } }
public override IImSeq<ParameterTypeDef> Parameters { get { return parameters; } }
public override TypeRef Extends { get { return extends; } }
internal override TypeRef PrimExtends(RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments)
{
return extends;
}
internal override IImSeq<TypeRef> PrimImplements(RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments)
{
return new Seq<TypeRef> { groundEnv.Global.IListTypeConstructorRef.ApplyTo(groundTypeBoundArguments[0]) };
}
internal override bool PrimIsSameDefinition(AssemblyDef thisAssembly, AssemblyDef otherAssembly, TypeDef otherDef)
{
return otherDef is ArrayTypeDef;
}
internal override bool PrimInstanceRespectsParameterConstraint(ValidityContext vctxt, MessageContext ctxt, TypeRef originalType, RootEnvironment groundEnv, IImSeq<TypeRef> groundTypeBoundArguments, ParameterConstraint constraint, bool inCodePointer)
{
switch (constraint)
{
case ParameterConstraint.Unconstrained:
case ParameterConstraint.ReferenceType:
return true;
case ParameterConstraint.NonNullableValueType:
case ParameterConstraint.DefaultConstructor:
vctxt.Log
(new InvalidTypeRef
(ctxt,
originalType,
"An array type cannot be used to instantiate a type parameter with a value-type or constructor constraint"));
return false;
default:
throw new ArgumentOutOfRangeException();
}
}
public override void AppendDefinition(CSTWriter w)
{
w.Append("<builtin array type constructor>");
}
public override void AppendReference(CSTWriter w, IImSeq<TypeRef> typeBoundArguments)
{
if (w.Style == WriterStyle.Uniform || typeBoundArguments == null)
{
w.Global.ArrayTypeConstructorRef.QualifiedTypeName.Append(w);
AppendArguments(w, typeBoundArguments);
}
else
{
typeBoundArguments[0].Append(w);
w.Append("[]");
}
}
}
// ----------------------------------------------------------------------
// MultiDimArrayTypeDef
// ----------------------------------------------------------------------
public class MultiDimArrayBound : IEquatable<MultiDimArrayBound>
{
public readonly int? Lower;
public readonly int? Size;
public MultiDimArrayBound(int? lower, int? size)
{
Lower = lower;
Size = size;
if (!Lower.HasValue && Size.HasValue)
// Implicit zero lower bound
Lower = 0;
}
public override bool Equals(object obj)
{
var other = obj as MultiDimArrayBound;
return other != null && Equals(other);
}