-
Notifications
You must be signed in to change notification settings - Fork 15
Expand file tree
/
Copy pathTypes.cs
More file actions
739 lines (602 loc) · 19.8 KB
/
Types.cs
File metadata and controls
739 lines (602 loc) · 19.8 KB
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
// Copyright (c) Edgardo Zoppi. All Rights Reserved. Licensed under the MIT License. See License.txt in the project root for license information.
using Model.ThreeAddressCode.Values;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace Model.Types
{
public interface IMetadataReference
{
ISet<CustomAttribute> Attributes { get; }
}
public enum TypeKind
{
Unknown,
ValueType,
ReferenceType
}
public interface IType : IMetadataReference
{
TypeKind TypeKind { get; }
}
public interface IValueType : IType
{
}
public interface IReferenceType : IType
{
}
public static class PlatformTypes
{
private static readonly ICollection<BasicType> platformTypes = new List<BasicType>();
public static readonly UnknownType Unknown = UnknownType.Value;
public static readonly BasicType Void = New("mscorlib", "System", "Void", TypeKind.ValueType);
public static readonly BasicType Boolean = New("mscorlib", "System", "Boolean", TypeKind.ValueType);
public static readonly BasicType Char = New("mscorlib", "System", "Char", TypeKind.ValueType);
public static readonly BasicType String = New("mscorlib", "System", "String", TypeKind.ReferenceType);
public static readonly BasicType Byte = New("mscorlib", "System", "Byte", TypeKind.ValueType);
public static readonly BasicType SByte = New("mscorlib", "System", "SByte", TypeKind.ValueType);
public static readonly BasicType Int16 = New("mscorlib", "System", "Int16", TypeKind.ValueType);
public static readonly BasicType Int32 = New("mscorlib", "System", "Int32", TypeKind.ValueType);
public static readonly BasicType Int64 = New("mscorlib", "System", "Int64", TypeKind.ValueType);
public static readonly BasicType UInt16 = New("mscorlib", "System", "UInt16", TypeKind.ValueType);
public static readonly BasicType UInt32 = New("mscorlib", "System", "UInt32", TypeKind.ValueType);
public static readonly BasicType UInt64 = New("mscorlib", "System", "UInt64", TypeKind.ValueType);
public static readonly BasicType Decimal = New("mscorlib", "System", "Decimal", TypeKind.ValueType);
public static readonly BasicType Single = New("mscorlib", "System", "Single", TypeKind.ValueType);
public static readonly BasicType Double = New("mscorlib", "System", "Double", TypeKind.ValueType);
public static readonly BasicType Object = New("mscorlib", "System", "Object", TypeKind.ReferenceType);
public static readonly BasicType IntPtr = New("mscorlib", "System", "IntPtr", TypeKind.ValueType);
public static readonly BasicType UIntPtr = New("mscorlib", "System", "UIntPtr", TypeKind.ValueType);
public static readonly BasicType RuntimeMethodHandle = New("mscorlib", "System", "RuntimeMethodHandle", TypeKind.ValueType);
public static readonly BasicType RuntimeTypeHandle = New("mscorlib", "System", "RuntimeTypeHandle", TypeKind.ValueType);
public static readonly BasicType RuntimeFieldHandle = New("mscorlib", "System", "RuntimeFieldHandle", TypeKind.ValueType);
public static readonly BasicType ArrayLengthType = UInt32;
public static readonly BasicType SizeofType = UInt32;
public static readonly BasicType Int8 = SByte;
public static readonly BasicType UInt8 = Byte;
public static readonly BasicType Float32 = Single;
public static readonly BasicType Float64 = Double;
public static readonly BasicType Enum = New("mscorlib", "System", "Enum", TypeKind.ValueType);
public static readonly BasicType ValueType = New("mscorlib", "System", "ValueType", TypeKind.ValueType);
public static readonly BasicType MulticastDelegate = New("mscorlib", "System", "MulticastDelegate", TypeKind.ReferenceType);
public static readonly BasicType Delegate = New("mscorlib", "System", "Delegate", TypeKind.ReferenceType);
public static readonly BasicType PureAttribute = New("mscorlib", "System.Diagnostics.Contracts", "PureAttribute", TypeKind.ReferenceType);
public static readonly BasicType ICollection = New("mscorlib", "System.Collections", "ICollection", TypeKind.ReferenceType);
public static readonly BasicType IEnumerable = New("mscorlib", "System.Collections", "IEnumerable", TypeKind.ReferenceType);
public static readonly BasicType IEnumerator = New("mscorlib", "System.Collections", "IEnumerator", TypeKind.ReferenceType);
public static readonly BasicType GenericICollection = New("mscorlib", "System.Collections.Generic", "ICollection", TypeKind.ReferenceType, 1);
public static readonly BasicType Task = New("mscorlib", "System.Threading.Tasks", "Task", TypeKind.ReferenceType);
public static readonly BasicType GenericTask = New("mscorlib", "System.Threading.Tasks", "Task", TypeKind.ReferenceType, 1);
public static void Resolve(Host host)
{
foreach (var type in platformTypes)
{
type.Resolve(host);
}
}
private static BasicType New(string containingAssembly, string containingNamespace, string name, TypeKind kind, int genericParameterCount = 0)
{
var result = new BasicType(name, kind)
{
ContainingAssembly = new AssemblyReference(containingAssembly),
ContainingNamespace = containingNamespace,
GenericParameterCount = genericParameterCount
};
//for (ushort i = 0; i < genericParameterCount; ++i)
//{
// var typevar = new GenericParameterReference(GenericParameterKind.Type, i);
// result.GenericArguments.Add(typevar);
//}
platformTypes.Add(result);
return result;
}
}
public class UnknownType : IType
{
private static UnknownType value;
private UnknownType() { }
public static UnknownType Value
{
get
{
if (value == null) value = new UnknownType();
return value;
}
}
public TypeKind TypeKind
{
get { return TypeKind.Unknown; }
}
public ISet<CustomAttribute> Attributes
{
get { return null; }
}
public override string ToString()
{
return "Unknown";
}
}
public interface IBasicType : IType, IGenericReference
{
IAssemblyReference ContainingAssembly { get; }
string ContainingNamespace { get; }
//IBasicType ContainingType { get; }
string Name { get; }
string GenericName { get; }
//new int GenericParameterCount { get; }
IList<IType> GenericArguments { get; }
IBasicType GenericType { get; }
TypeDefinition ResolvedType { get; }
}
public class BasicType : IBasicType
{
private Func<TypeDefinition> ResolveType;
private TypeDefinition resolvedType;
public ISet<CustomAttribute> Attributes { get; private set; }
public TypeKind TypeKind { get; set; }
public IAssemblyReference ContainingAssembly { get; set; }
public string ContainingNamespace { get; set; }
public IBasicType ContainingType { get; set; }
public string Name { get; set; }
public int GenericParameterCount { get; set; }
public IList<IType> GenericArguments { get; private set; }
public IBasicType GenericType { get; set; }
public BasicType(string name, TypeKind kind = TypeKind.Unknown)
{
this.Name = name;
this.TypeKind = kind;
this.GenericArguments = new List<IType>();
this.Attributes = new HashSet<CustomAttribute>();
this.ResolveType = () =>
{
var msg = "Use Resolve method to bind this reference with some host. " +
"To bind all platform types use PlatformTypes.Resolve method.";
throw new InvalidOperationException(msg);
};
}
public string GenericName
{
get
{
var arguments = string.Empty;
if (this.GenericArguments.Count > 0)
{
arguments = string.Join(", ", this.GenericArguments);
arguments = string.Format("<{0}>", arguments);
}
else if (this.GenericParameterCount > 0)
{
arguments = string.Join(", T", Enumerable.Range(1, this.GenericParameterCount));
arguments = string.Format("<T{0}>", arguments);
}
//else if (this.GenericParameterCount > 0)
//{
// var startIndex = this.ContainingType.TotalGenericParameterCount();
// arguments = string.Join(", T", Enumerable.Range(startIndex, this.GenericParameterCount));
// arguments = string.Format("<T{0}>", arguments);
//}
return string.Format("{0}{1}", this.Name, arguments);
}
}
public TypeDefinition ResolvedType
{
get
{
if (resolvedType == null)
{
resolvedType = ResolveType();
}
return resolvedType;
}
}
//public TypeDefinition Resolve(Host host)
//{
// this.ResolvedType = host.ResolveReference(this);
// return this.ResolvedType;
//}
public void Resolve(Host host)
{
ResolveType = () => host.ResolveReference(this);
}
public override string ToString()
{
return this.GenericName;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as IBasicType;
// TODO: Maybe we should also compare the TypeKind?
var result = other != null &&
this.Name == other.Name &&
this.GenericParameterCount == other.GenericParameterCount &&
this.ContainingNamespace == other.ContainingNamespace &&
this.ContainingAssembly.Equals(other.ContainingAssembly) &&
this.GenericArguments.SequenceEqual(other.GenericArguments);
return result;
}
}
#region class IBasicType
//public class IBasicType : IType
//{
// public ISet<Attribute> Attributes { get; private set; }
// public TypeKind TypeKind { get; set; }
// public IAssemblyReference Assembly { get; set; }
// public string Namespace { get; set; }
// public string Name { get; set; }
// public IBasicType(string name, TypeKind kind = TypeKind.Unknown)
// {
// this.Name = name;
// this.TypeKind = kind;
// this.Attributes = new HashSet<Attribute>();
// }
// public virtual string FullName
// {
// get
// {
// var containingAssembly = string.Empty;
// var containingNamespace = string.Empty;
// if (this.Assembly != null)
// {
// containingAssembly = string.Format("[{0}]", this.Assembly.Name);
// }
// if (!string.IsNullOrEmpty(this.Namespace))
// {
// containingNamespace = string.Format("{0}.", this.Namespace);
// }
// return string.Format("{0}{1}{2}", containingAssembly, containingNamespace, this.Name);
// }
// }
// public override string ToString()
// {
// return this.Name;
// }
// public override int GetHashCode()
// {
// return this.Name.GetHashCode();
// }
// public override bool Equals(object obj)
// {
// var other = obj as IBasicType;
// // TODO: Maybe we should also compare the TypeKind?
// var result = other != null &&
// this.Assembly.Equals(other.Assembly) &&
// this.Namespace == other.Namespace &&
// this.Name == other.Name;
// return result;
// }
//}
#endregion
#region class GenericType
//public class GenericType : IBasicType
//{
// public int GenericParameterCount { get; set; }
// public GenericType(string name, TypeKind kind = TypeKind.Unknown)
// : base(name, kind)
// {
// }
// public string NonGenericFullName
// {
// get { return base.FullName; }
// }
// public override string FullName
// {
// get
// {
// var parameters = string.Empty;
// if (this.GenericParameterCount > 0)
// {
// parameters = string.Join(", T", Enumerable.Range(1, this.GenericParameterCount + 1));
// parameters = string.Format("<T{0}>", parameters);
// }
// return string.Format("{0}{1}", this.NonGenericFullName, parameters);
// }
// }
// public override string ToString()
// {
// var parameters = string.Empty;
// if (this.GenericParameterCount > 0)
// {
// parameters = string.Join(", T", Enumerable.Range(1, this.GenericParameterCount + 1));
// parameters = string.Format("<T{0}>", parameters);
// }
// return string.Format("{0}{1}", base.ToString(), parameters);
// }
// public override int GetHashCode()
// {
// return this.Name.GetHashCode();
// }
// public override bool Equals(object obj)
// {
// var other = obj as GenericType;
// var result = other != null &&
// base.Equals(other) &&
// this.GenericParameterCount == other.GenericParameterCount;
// return result;
// }
//}
#endregion
#region class SpecializedType
//public class SpecializedType : IType
//{
// public ISet<Attribute> Attributes { get; private set; }
// public GenericType GenericType { get; set; }
// public IList<IType> GenericArguments { get; private set; }
// public SpecializedType(GenericType genericType)
// {
// this.GenericType = genericType;
// this.GenericArguments = new List<IType>();
// this.Attributes = new HashSet<Attribute>();
// }
// public TypeKind TypeKind
// {
// get { return this.GenericType.TypeKind; }
// }
// public string NonGenericFullName
// {
// get { return this.GenericType.NonGenericFullName; }
// }
// public string FullName
// {
// get
// {
// var arguments = string.Empty;
// if (this.GenericArguments.Count > 0)
// {
// arguments = string.Join(", ", this.GenericArguments);
// arguments = string.Format("<{0}>", arguments);
// }
// return string.Format("{0}{1}", this.NonGenericFullName, arguments);
// }
// }
// public override string ToString()
// {
// var arguments = string.Empty;
// if (this.GenericArguments.Count > 0)
// {
// arguments = string.Join(", ", this.GenericArguments);
// arguments = string.Format("<{0}>", arguments);
// }
// return string.Format("{0}{1}", this.GenericType.Name, arguments);
// }
// public override int GetHashCode()
// {
// return this.GenericType.Name.GetHashCode();
// }
// public override bool Equals(object obj)
// {
// var other = obj as SpecializedType;
// var result = other != null &&
// this.GenericType.Equals(other.GenericType) &&
// this.GenericArguments.SequenceEqual(other.GenericArguments);
// return result;
// }
//}
#endregion
public enum GenericParameterKind
{
Type,
Method
}
public interface IGenericParameterReference : IType
{
IGenericReference GenericContainer { get; }
GenericParameterKind Kind { get; }
ushort Index { get; }
string Name { get; }
}
public class GenericParameterReference : IGenericParameterReference
{
public ISet<CustomAttribute> Attributes { get; private set; }
public IGenericReference GenericContainer { get; set; }
public GenericParameterKind Kind { get; set; }
public ushort Index { get; set; }
public GenericParameterReference(GenericParameterKind kind, ushort index)
{
this.Kind = kind;
this.Index = index;
this.Attributes = new HashSet<CustomAttribute>();
}
public string Name
{
get { return GetName(this.Kind, this.Index); }
}
public TypeKind TypeKind
{
get { return TypeKind.Unknown; }
}
public override string ToString()
{
return this.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as IGenericParameterReference;
// TODO: Maybe we should also compare the TypeKind?
var result = other != null &&
this.Kind == other.Kind &&
this.Index == other.Index;
return result;
}
private static string GetName(GenericParameterKind kind, ushort index)
{
string prefix;
switch (kind)
{
case GenericParameterKind.Type: prefix = "!"; break;
case GenericParameterKind.Method: prefix = "!!"; break;
default: throw kind.ToUnknownValueException();
}
return string.Format("{0}{1}", prefix, index);
}
}
public class GenericParameter : IGenericParameterReference
{
public ISet<CustomAttribute> Attributes { get; private set; }
public TypeKind TypeKind { get; set; }
public IGenericDefinition GenericContainer { get; set; }
public GenericParameterKind Kind { get; set; }
public ushort Index { get; set; }
public string Name { get; set; }
public GenericParameter(GenericParameterKind kind, ushort index, string name, TypeKind typeKind = TypeKind.Unknown)
{
this.Kind = kind;
this.Index = index;
this.Name = name;
this.TypeKind = typeKind;
this.Attributes = new HashSet<CustomAttribute>();
}
#region IGenericParameterReference members
IGenericReference IGenericParameterReference.GenericContainer
{
get { return this.GenericContainer; }
}
#endregion
public override string ToString()
{
return this.Name;
}
public override int GetHashCode()
{
return this.Name.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as IGenericParameterReference;
// TODO: Maybe we should also compare the TypeKind?
var result = other != null &&
this.Kind == other.Kind &&
this.Index == other.Index;
//this.Name == other.Name;
return result;
}
}
public class FunctionPointerType : IReferenceType
{
public ISet<CustomAttribute> Attributes { get; private set; }
public IType ReturnType { get; set; }
public IList<IMethodParameterReference> Parameters { get; private set; }
public bool IsStatic { get; set; }
// TODO: Not sure if we should add GenericParameterCount property.
public FunctionPointerType(IType returnType)
{
this.ReturnType = returnType;
this.Parameters = new List<IMethodParameterReference>();
this.Attributes = new HashSet<CustomAttribute>();
}
public FunctionPointerType(IMethodReference method)
: this(method.ReturnType)
{
this.IsStatic = method.IsStatic;
this.Parameters.AddRange(method.Parameters);
}
public TypeKind TypeKind
{
get { return TypeKind.ReferenceType; }
}
public override string ToString()
{
var result = new StringBuilder();
var parameters = string.Join(", ", this.Parameters);
if (this.IsStatic)
{
result.Append("static ");
}
result.Append(this.ReturnType);
result.AppendFormat("({0})", parameters);
return result.ToString();
}
public override int GetHashCode()
{
var result = this.ReturnType.GetHashCode() ^
this.IsStatic.GetHashCode() ^
this.Parameters.Aggregate(0, (hc, p) => hc ^ p.GetHashCode());
return result;
}
public override bool Equals(object obj)
{
var other = obj as FunctionPointerType;
var result = other != null &&
this.IsStatic == other.IsStatic &&
this.ReturnType.Equals(other.ReturnType) &&
this.Parameters.SequenceEqual(other.Parameters);
return result;
}
}
public class PointerType : IReferenceType
{
public ISet<CustomAttribute> Attributes { get; private set; }
public IType TargetType { get; set; }
public bool Managed { get; private set; }
public PointerType(IType targetType, bool managed = false)
{
this.TargetType = targetType;
this.Attributes = new HashSet<CustomAttribute>();
this.Managed = managed;
}
public TypeKind TypeKind
{
get { return TypeKind.ReferenceType; }
}
public override string ToString()
{
return string.Format("{0}*", this.TargetType);
}
public override int GetHashCode()
{
return this.TargetType.GetHashCode() ^ this.Managed.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as PointerType;
var result = other != null &&
this.TargetType.Equals(other.TargetType) &&
this.Managed.Equals(other.Managed);
return result;
}
}
public class ArrayType : IReferenceType
{
public ISet<CustomAttribute> Attributes { get; private set; }
public IType ElementsType { get; set; }
public uint Rank { get; set; }
public ArrayType(IType elementsType, uint rank = 1)
{
this.ElementsType = elementsType;
this.Rank = rank;
this.Attributes = new HashSet<CustomAttribute>();
}
public TypeKind TypeKind
{
get { return TypeKind.ReferenceType; }
}
public bool IsVector
{
get { return this.Rank == 1; }
}
public override string ToString()
{
var rank = new string(',', (int)this.Rank - 1);
return string.Format("{0}[{1}]", this.ElementsType, rank);
}
public override int GetHashCode()
{
return this.ElementsType.GetHashCode();
}
public override bool Equals(object obj)
{
var other = obj as ArrayType;
var result = other != null &&
this.ElementsType.Equals(other.ElementsType);
return result;
}
}
}