-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTypeInformation.cs
More file actions
2418 lines (1950 loc) · 84.7 KB
/
Copy pathTypeInformation.cs
File metadata and controls
2418 lines (1950 loc) · 84.7 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
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
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using JSIL.Ast;
using JSIL.Meta;
using JSIL.Proxy;
using JSIL.Transforms;
using Mono.Cecil;
namespace JSIL.Internal {
public interface ITypeInfoSource {
ModuleInfo Get (ModuleDefinition module);
TypeInfo Get (TypeReference type);
TypeInfo GetExisting (TypeReference type);
TypeInfo GetExisting (TypeDefinition type);
TypeInfo GetExisting (TypeIdentifier type);
IMemberInfo Get (MemberReference member);
ArraySegment<ProxyInfo> GetProxies (TypeDefinition type);
void CacheProxyNames (MemberReference member);
bool TryGetProxyNames (TypeReference type, out ArraySegment<string> result);
ConcurrentCache<Tuple<string, string>, bool> AssignabilityCache {
get;
}
}
public static class TypeInfoSourceExtensions {
public static FieldInfo GetField (this ITypeInfoSource source, FieldReference field) {
return (FieldInfo)source.Get(field);
}
public static MethodInfo GetMethod (this ITypeInfoSource source, MethodReference method) {
return (MethodInfo)source.Get(method);
}
public static PropertyInfo GetProperty (this ITypeInfoSource source, PropertyReference property) {
return (PropertyInfo)source.Get(property);
}
}
public struct InterfaceToken {
public readonly TypeInfo Info;
public readonly TypeReference Reference;
public InterfaceToken (TypeInfo info, TypeReference reference) {
Info = info;
Reference = reference;
}
}
public struct RecursiveInterfaceToken {
public readonly TypeInfo ImplementingType;
public readonly InterfaceToken ImplementedInterface;
public RecursiveInterfaceToken (TypeInfo implementingType, InterfaceToken implementedInterface) {
ImplementingType = implementingType;
ImplementedInterface = implementedInterface;
}
}
public class RecursiveInterfaceTokenComparer : IEqualityComparer<RecursiveInterfaceToken> {
public bool Equals (RecursiveInterfaceToken x, RecursiveInterfaceToken y) {
return TypeUtil.TypesAreEqual(
x.ImplementedInterface.Reference,
y.ImplementedInterface.Reference,
true
);
}
public int GetHashCode (RecursiveInterfaceToken obj) {
return obj.ImplementedInterface.Info.GetHashCode();
}
}
public struct TypeIdentifier {
public class ComparerImpl : IEqualityComparer<TypeIdentifier> {
public bool Equals (TypeIdentifier x, TypeIdentifier y) {
return x.Equals(y);
}
public int GetHashCode (TypeIdentifier obj) {
return obj.GetHashCode();
}
}
public static readonly ComparerImpl Comparer = new ComparerImpl();
public readonly string Assembly;
public readonly string Namespace;
public readonly string DeclaringTypeName;
public readonly string Name;
public TypeIdentifier (TypeDefinition type) {
if (type == null)
throw new ArgumentNullException("type");
if (type.Module != null) {
var asm = type.Module.Assembly;
if (asm != null)
Assembly = asm.FullName;
else
Assembly = null;
} else {
Assembly = null;
}
Namespace = type.Namespace;
Name = type.Name;
var declaringType = type.DeclaringType;
if (declaringType != null)
DeclaringTypeName = declaringType.FullName;
else
DeclaringTypeName = null;
}
public bool Equals (TypeIdentifier rhs) {
if (!String.Equals(Name, rhs.Name))
return false;
if (!String.Equals(Namespace, rhs.Namespace))
return false;
if (!String.Equals(DeclaringTypeName, rhs.DeclaringTypeName))
return false;
if ((Assembly == null) || (rhs.Assembly == null))
return true;
else if (!String.Equals(Assembly, rhs.Assembly))
return false;
else
return true;
}
public override bool Equals (object obj) {
if (obj is TypeIdentifier)
return Equals((TypeIdentifier)obj);
return base.Equals(obj);
}
public override int GetHashCode () {
var result = Namespace.GetHashCode() ^ Name.GetHashCode();
if (DeclaringTypeName != null)
result ^= DeclaringTypeName.GetHashCode();
return result;
}
public override string ToString () {
var shortAssembly = Assembly;
if (!String.IsNullOrWhiteSpace(shortAssembly)) {
var firstComma = Assembly.IndexOf(",");
if (firstComma >= 0)
shortAssembly = Assembly.Substring(0, firstComma);
}
var hasShortAssembly = !String.IsNullOrWhiteSpace(shortAssembly);
return String.Format(
"{0}{1}{2}{3}{4}{5}{6}{7}",
hasShortAssembly ? "[" : "",
shortAssembly ?? "",
hasShortAssembly ? "]" : "",
Namespace, String.IsNullOrWhiteSpace(Namespace) ? "" : ".",
String.IsNullOrWhiteSpace(DeclaringTypeName) ? "" : "/",
DeclaringTypeName ?? "",
Name
);
}
}
public struct GenericTypeIdentifier
{
public readonly TypeIdentifier Type;
public readonly GenericTypeIdentifier[] Arguments;
public readonly int ArrayRank;
public GenericTypeIdentifier(TypeIdentifier type, IEnumerable<GenericTypeIdentifier> arguments, int arrayRank)
{
Type = type;
Arguments = arguments.ToArray();
ArrayRank = arrayRank;
}
public static GenericTypeIdentifier? Create(TypeReference type)
{
bool mapArraysToSystemArray = false;
while (type is ByReferenceType)
type = ((ByReferenceType)type).ElementType;
var resolved = TypeUtil.GetTypeDefinition(type, mapArraysToSystemArray);
if (resolved == null)
{
return null;
}
var at = type as ArrayType;
var git = type as GenericInstanceType;
IEnumerable<GenericTypeIdentifier> children;
if (git != null)
{
var childrenList = new GenericTypeIdentifier[git.GenericArguments.Count];
for (int i = 0; i < git.GenericArguments.Count; i++)
{
var child = Create(git.GenericArguments[i]);
if (child == null)
{
return null;
}
childrenList[i] = child.Value;
}
children = childrenList;
}
else
{
children = Enumerable.Empty<GenericTypeIdentifier>();
}
var identifier = new GenericTypeIdentifier(
new TypeIdentifier(resolved),
children,
(at != null) ? at.Rank : 0);
return identifier;
}
public bool Equals (GenericTypeIdentifier rhs) {
if (!Type.Equals(rhs.Type))
return false;
if (Arguments.Length != rhs.Arguments.Length)
return false;
if (ArrayRank != rhs.ArrayRank)
return false;
for (var i = 0; i < Arguments.Length; i++) {
if (!Arguments[i].Equals(rhs.Arguments[i]))
return false;
}
return true;
}
public override bool Equals (object obj) {
if (obj is GenericTypeIdentifier)
return Equals((GenericTypeIdentifier)obj);
else
return false;
}
public override int GetHashCode () {
return Type.GetHashCode() ^ Arguments.Length ^ ArrayRank;
}
private static string GetRankSuffix (int rank) {
if (rank <= 0)
return "";
else {
var result = "[";
for (var i = 1; i < rank; i++)
result += ",";
result += "]";
return result;
}
}
public override string ToString () {
return String.Format(
"{0}<{1}>",
(Type + GetRankSuffix(ArrayRank)),
String.Join(", ", Arguments)
);
}
}
public class ModuleInfo {
public readonly bool IsIgnored;
public readonly MetadataCollection Metadata;
public ModuleInfo (ModuleDefinition module) {
Metadata = new MetadataCollection(module);
IsIgnored = TypeInfo.IsIgnoredName(module.FullyQualifiedName) ||
Metadata.HasAttribute("JSIL.Meta.JSIgnore");
}
}
public class ProxyInfo {
public readonly string Name;
public readonly TypeDefinition Definition;
public readonly HashSet<TypeReference> ProxiedTypes = new HashSet<TypeReference>();
public readonly HashSet<string> ProxiedTypeNames = new HashSet<string>();
public readonly TypeReference[] Interfaces;
public readonly MetadataCollection Metadata;
public readonly JSProxyAttributePolicy AttributePolicy;
public readonly JSProxyMemberPolicy MemberPolicy;
public readonly JSProxyInterfacePolicy InterfacePolicy;
public readonly Dictionary<MemberIdentifier, FieldDefinition> Fields;
public readonly Dictionary<MemberIdentifier, PropertyDefinition> Properties;
public readonly Dictionary<MemberIdentifier, EventDefinition> Events;
public readonly Dictionary<MemberIdentifier, MethodDefinition> Methods;
public readonly MethodDefinition ExtraStaticConstructor;
public readonly bool IsInheritable;
internal int UsageCount;
internal readonly ConcurrentDictionary<MemberIdentifier, bool> MemberReplacedTable;
protected readonly ITypeInfoSource TypeInfo;
public ProxyInfo (ITypeInfoSource typeInfo, TypeDefinition proxyType) {
TypeInfo = typeInfo;
var comparer = new MemberIdentifier.Comparer(typeInfo);
Fields = new Dictionary<MemberIdentifier, FieldDefinition>(comparer);
Properties = new Dictionary<MemberIdentifier, PropertyDefinition>(comparer);
Events = new Dictionary<MemberIdentifier, EventDefinition>(comparer);
Methods = new Dictionary<MemberIdentifier, MethodDefinition>(comparer);
MemberReplacedTable = new ConcurrentDictionary<MemberIdentifier, bool>(comparer);
ExtraStaticConstructor = null;
Definition = proxyType;
Name = Definition.Name;
Metadata = new MetadataCollection(proxyType);
Interfaces = proxyType.Interfaces.ToArray();
IsInheritable = true;
var args = Metadata.GetAttributeParameters("JSIL.Proxy.JSProxy");
if (args == null)
throw new ArgumentNullException("JSProxy without arguments");
// Attribute parameter ordering is random. Awesome!
foreach (var arg in args) {
switch (arg.Type.FullName) {
case "JSIL.Proxy.JSProxyAttributePolicy":
AttributePolicy = (JSProxyAttributePolicy)arg.Value;
break;
case "JSIL.Proxy.JSProxyMemberPolicy":
MemberPolicy = (JSProxyMemberPolicy)arg.Value;
break;
case "JSIL.Proxy.JSProxyInterfacePolicy":
InterfacePolicy = (JSProxyInterfacePolicy)arg.Value;
break;
case "System.Type":
ProxiedTypes.Add((TypeReference)arg.Value);
break;
case "System.Type[]": {
var values = (CustomAttributeArgument[])arg.Value;
for (var i = 0; i < values.Length; i++)
ProxiedTypes.Add((TypeReference)values[i].Value);
break;
}
case "System.Boolean":
IsInheritable = (bool)arg.Value;
break;
case "System.String":
ProxiedTypeNames.Add((string)arg.Value);
break;
case "System.String[]": {
var values = (CustomAttributeArgument[])arg.Value;
foreach (var v in values)
ProxiedTypeNames.Add((string)v.Value);
break;
}
default:
throw new NotImplementedException(String.Format(
"Invalid argument to JSProxy attribute: {0}",
arg.Type.FullName
));
}
}
foreach (var field in proxyType.Fields) {
if (!TypeUtil.TypesAreEqual(field.DeclaringType, proxyType))
continue;
Fields.Add(new MemberIdentifier(typeInfo, field), field);
}
foreach (var property in proxyType.Properties) {
if (!TypeUtil.TypesAreEqual(property.DeclaringType, proxyType))
continue;
Properties.Add(new MemberIdentifier(typeInfo, property), property);
}
foreach (var evt in proxyType.Events) {
if (!TypeUtil.TypesAreEqual(evt.DeclaringType, proxyType))
continue;
Events.Add(new MemberIdentifier(typeInfo, evt), evt);
}
foreach (var method in proxyType.Methods) {
if (!TypeUtil.TypesAreEqual(method.DeclaringType, proxyType))
continue;
if ((method.Name == ".cctor") && method.CustomAttributes.Any((ca) => ca.AttributeType.FullName == "JSIL.Meta.JSExtraStaticConstructor")) {
ExtraStaticConstructor = method;
} else {
var key = new MemberIdentifier(typeInfo, method);
if (Methods.ContainsKey(key))
throw new InvalidOperationException("Proxy '" + proxyType.FullName + "' contains multiple matches for member '" + key.ToString());
else
Methods.Add(key, method);
}
}
}
public override string ToString () {
return Definition.FullName;
}
public bool GetMember<T> (MemberIdentifier member, out T result)
where T : class {
MethodDefinition method;
if (Methods.TryGetValue(member, out method) && ((result = method as T) != null))
return true;
FieldDefinition field;
if (Fields.TryGetValue(member, out field) && ((result = field as T) != null))
return true;
PropertyDefinition property;
if (Properties.TryGetValue(member, out property) && ((result = property as T) != null))
return true;
EventDefinition evt;
if (Events.TryGetValue(member, out evt) && ((result = evt as T) != null))
return true;
result = null;
return false;
}
public bool IsMatch (TypeDefinition type, bool? forcedInheritable) {
bool inheritable = forcedInheritable.GetValueOrDefault(IsInheritable);
foreach (var pt in ProxiedTypes) {
bool isMatch;
if (inheritable)
isMatch = TypeUtil.TypesAreAssignable(TypeInfo, pt, type);
else
isMatch = TypeUtil.TypesAreEqual(pt, type);
if (isMatch)
return true;
}
if (ProxiedTypeNames.Count > 0) {
if (ProxiedTypeNames.Contains(type.FullName))
return true;
if (inheritable)
foreach (var baseType in TypeUtil.AllBaseTypesOf(TypeUtil.GetTypeDefinition(type))) {
if (ProxiedTypeNames.Contains(baseType.FullName))
return true;
}
}
return false;
}
}
public class TypeInfo {
public readonly TypeIdentifier Identifier;
public readonly TypeDefinition Definition;
public readonly ITypeInfoSource Source;
public readonly TypeInfo DeclaringType;
public readonly TypeInfo BaseClass;
public readonly ArraySegment<InterfaceToken> Interfaces;
private ArraySegment<RecursiveInterfaceToken> _AllInterfacesRecursive;
// This needs to be mutable so we can introduce a constructed cctor later
public MethodDefinition StaticConstructor;
public readonly List<MethodInfo> ExtraStaticConstructors = new List<MethodInfo>();
public readonly HashSet<MethodDefinition> Constructors = new HashSet<MethodDefinition>();
public readonly MetadataCollection Metadata;
public readonly ArraySegment<ProxyInfo> Proxies;
public readonly MethodSignatureCollection MethodSignatures;
public readonly HashSet<MethodGroupInfo> MethodGroups = new HashSet<MethodGroupInfo>();
public readonly bool IsFlagsEnum;
public readonly EnumMemberInfo FirstEnumMember = null;
public readonly ConcurrentDictionary<long, EnumMemberInfo> ValueToEnumMember;
public readonly ConcurrentDictionary<string, EnumMemberInfo> EnumMembers;
public readonly ConcurrentDictionary<MemberIdentifier, IMemberInfo> Members;
public readonly List<FieldInfo> AddedFieldsFromProxies = new List<FieldInfo>();
public readonly bool IsProxy;
public readonly bool IsDelegate;
public readonly bool IsInterface;
public readonly bool IsImmutable;
public readonly string Replacement;
public readonly int UnstubbableMemberCount = 0;
// Matches JSIL runtime name escaping rules
public readonly string LocalName;
protected int _DerivedTypeCount = 0;
protected string _FullName = null;
protected bool _FullyInitialized = false;
protected bool _IsIgnored = false;
protected bool _IsExternal = false;
protected bool _IsStubOnly = false;
protected bool _IsUnstubbable = false;
protected bool _IsSuppressDeclaration = false;
protected bool _MethodGroupsInitialized = false;
protected List<NamedMethodSignature> DeferredMethodSignatureSetUpdates = new List<NamedMethodSignature>();
protected List<NamedMethodSignature> DeferredStaticMethodSignatureSetUpdates = new List<NamedMethodSignature>();
public TypeInfo (ITypeInfoSource source, ModuleInfo module, TypeDefinition type, TypeInfo declaringType, TypeInfo baseClass, TypeIdentifier identifier) {
Identifier = identifier;
DeclaringType = declaringType;
BaseClass = baseClass;
Source = source;
Definition = type;
bool isStatic = type.IsSealed && type.IsAbstract;
LocalName = TypeUtil.GetLocalName(type);
if (baseClass != null)
Interlocked.Increment(ref baseClass._DerivedTypeCount);
Proxies = source.GetProxies(type);
Metadata = new MetadataCollection(type);
MethodSignatures = new MethodSignatureCollection();
// Do this check before copying attributes from proxy types, since that will copy their JSProxy attribute
IsProxy = Metadata.HasAttribute("JSIL.Proxy.JSProxy");
IsDelegate = (type.BaseType != null) && (
(type.BaseType.FullName == "System.Delegate") ||
(type.BaseType.FullName == "System.MulticastDelegate")
);
IsInterface = type.IsInterface;
var interfaces = new HashSet<InterfaceToken>();
{
StringBuilder errorString = null;
foreach (var i in type.Interfaces) {
var resolved = i.Resolve();
if (resolved == null) {
Console.Error.WriteLine("Warning: Could not resolve interface reference '{0}' for type '{1}'!", i.FullName, type.FullName);
continue;
}
var ii = new InterfaceToken(source.GetExisting(i), i);
if (ii.Info == null) {
if (errorString == null) {
errorString = new StringBuilder();
errorString.AppendFormat(
"Missing type information for the following interface(s) of type '{0}':{1}",
type.FullName, Environment.NewLine
);
}
errorString.AppendLine(i.FullName);
} else {
interfaces.Add(ii);
}
}
if (errorString != null)
throw new InvalidDataException(errorString.ToString());
}
foreach (var proxy in Proxies.ToEnumerable()) {
if (!IsProxy)
Interlocked.Increment(ref proxy.UsageCount);
Metadata.Update(proxy.Metadata, proxy.AttributePolicy == JSProxyAttributePolicy.ReplaceAll);
if (proxy.InterfacePolicy == JSProxyInterfacePolicy.ReplaceNone) {
} else {
if (proxy.InterfacePolicy == JSProxyInterfacePolicy.ReplaceAll)
interfaces.Clear();
foreach (var i in proxy.Interfaces) {
var ii = source.Get(i);
interfaces.Add(new InterfaceToken(ii, i));
}
}
}
if (Metadata.HasAttribute("JSIL.Proxy.JSProxy") && !IsProxy)
Metadata.Remove("JSIL.Proxy.JSProxy");
// FIXME: Using ImmutableArrayPool here can leak.
Interfaces = new ArraySegment<InterfaceToken>(interfaces.ToArray());
_IsIgnored = module.IsIgnored ||
IsIgnoredName(type.Namespace) ||
IsIgnoredName(type.Name) ||
Metadata.HasAttribute("JSIL.Meta.JSIgnore") ||
Metadata.HasAttribute("System.Runtime.CompilerServices.UnsafeValueTypeAttribute") ||
Metadata.HasAttribute("System.Runtime.CompilerServices.NativeCppClassAttribute");
_IsExternal = Metadata.HasAttribute("JSIL.Meta.JSExternal");
if (Metadata.HasAttribute("JSIL.Meta.JSReplacement")) {
Replacement = (string)Metadata.GetAttributeParameters("JSIL.Meta.JSReplacement")[0].Value;
} else {
Replacement = null;
}
_IsStubOnly = Metadata.HasAttribute("JSIL.Meta.JSStubOnly");
_IsUnstubbable = Metadata.HasAttribute("JSIL.Meta.JSNeverStub");
_IsSuppressDeclaration = Metadata.HasAttribute("JSIL.Meta.JSSuppressTypeDeclaration");
if (_IsUnstubbable) {
_IsStubOnly = false;
_IsExternal = false;
}
if (baseClass != null)
_IsIgnored |= baseClass.IsIgnored;
{
var capacity = type.Fields.Count + type.Properties.Count + type.Events.Count + type.Methods.Count;
var comparer = new MemberIdentifier.Comparer(source);
Members = new ConcurrentDictionary<MemberIdentifier, IMemberInfo>(1, capacity, comparer);
}
foreach (var field in type.Fields)
AddMember(field);
foreach (var property in type.Properties) {
var pi = AddMember(property);
if (property.GetMethod != null)
AddMember(property.GetMethod, pi);
if (property.SetMethod != null)
AddMember(property.SetMethod, pi);
}
foreach (var evt in type.Events) {
var ei = AddMember(evt);
if (evt.AddMethod != null)
AddMember(evt.AddMethod, ei);
if (evt.RemoveMethod != null)
AddMember(evt.RemoveMethod, ei);
}
foreach (var method in type.Methods) {
if (method.Name == ".ctor")
Constructors.Add(method);
AddMember(method);
}
if (type.IsEnum) {
long enumValue = 0;
var capacity = type.Fields.Count;
ValueToEnumMember = new ConcurrentDictionary<long, EnumMemberInfo>(1, capacity);
EnumMembers = new ConcurrentDictionary<string, EnumMemberInfo>(1, capacity);
foreach (var field in type.Fields) {
// Skip 'value__'
if (field.IsRuntimeSpecialName)
continue;
if (field.HasConstant)
enumValue = Convert.ToInt64(field.Constant);
var info = new EnumMemberInfo(type, field.Name, enumValue);
if (FirstEnumMember == null)
FirstEnumMember = info;
ValueToEnumMember[enumValue] = info;
EnumMembers[field.Name] = info;
enumValue += 1;
}
IsFlagsEnum = Metadata.HasAttribute("System.FlagsAttribute");
}
foreach (var proxy in Proxies.ToEnumerable()) {
var seenMethods = new HashSet<MethodDefinition>();
foreach (var property in proxy.Properties.Values) {
var p = (PropertyInfo)AddProxyMember(proxy, property);
if (property.GetMethod != null) {
if (!property.CustomAttributes.Any(ShouldNeverReplace))
AddProxyMember(proxy, property.GetMethod, p);
seenMethods.Add(property.GetMethod);
}
if (property.SetMethod != null) {
if (!property.CustomAttributes.Any(ShouldNeverReplace))
AddProxyMember(proxy, property.SetMethod, p);
seenMethods.Add(property.SetMethod);
}
}
foreach (var evt in proxy.Events.Values) {
var e = (EventInfo)AddProxyMember(proxy, evt);
if (evt.AddMethod != null) {
if (!evt.CustomAttributes.Any(ShouldNeverReplace))
AddProxyMember(proxy, evt.AddMethod, e);
seenMethods.Add(evt.AddMethod);
}
if (evt.RemoveMethod != null) {
if (!evt.CustomAttributes.Any(ShouldNeverReplace))
AddProxyMember(proxy, evt.RemoveMethod, e);
seenMethods.Add(evt.RemoveMethod);
}
}
foreach (var field in proxy.Fields.Values) {
if (isStatic && !field.IsStatic)
continue;
AddProxyMember(proxy, field);
}
foreach (var method in proxy.Methods.Values) {
if (seenMethods.Contains(method))
continue;
if (isStatic && !method.IsStatic)
continue;
// The constructor may be compiler-generated, so only replace if it has the attribute.
if ((method.Name == ".ctor") && (method.Parameters.Count == 0)) {
if (!method.CustomAttributes.Any((ca) => ca.AttributeType.FullName == "JSIL.Proxy.JSReplaceConstructor"))
continue;
}
AddProxyMember(proxy, method);
}
if (proxy.ExtraStaticConstructor != null) {
var name = String.Format(".cctor{0}", ExtraStaticConstructors.Count + 2);
var escIdentifier = new MemberIdentifier(source, proxy.ExtraStaticConstructor, name);
var escInfo = new MethodInfo(this, escIdentifier, proxy.ExtraStaticConstructor, Proxies, proxy);
escInfo.ForcedNewName = name;
ExtraStaticConstructors.Add(escInfo);
}
if (proxy.MemberPolicy == JSProxyMemberPolicy.ReplaceAll) {
var previousMembers = Members.ToArray();
Members.Clear();
foreach (var member in previousMembers) {
if (member.Value.IsFromProxy)
Members.TryAdd(member.Key, member.Value);
}
}
}
if (
!IsInterface &&
!IsDelegate &&
!Definition.IsEnum &&
!Definition.IsAbstract &&
!Definition.IsPrimitive
) {
IsImmutable = Metadata.HasAttribute("JSIL.Meta.JSImmutable") ||
Members.Values.OfType<FieldInfo>().All((f) => f.IsStatic || f.IsImmutable);
}
DoDeferredMethodSignatureSetUpdate();
ValidateMembers();
UnstubbableMemberCount = Members.Count(m => m.Value.IsUnstubbable);
}
private void DoDeferredMethodSignatureSetUpdate () {
var selfAndBaseTypesRecursive = this.SelfAndBaseTypesRecursive.ToArray();
foreach (var t in selfAndBaseTypesRecursive) {
var ms = t.MethodSignatures;
foreach (var nms in DeferredMethodSignatureSetUpdates) {
var set = ms.GetOrCreateFor(nms.Name);
set.Add(nms);
}
if (t != this) {
foreach (var nms in t.DeferredMethodSignatureSetUpdates) {
var set = MethodSignatures.GetOrCreateFor(nms.Name);
set.Add(nms);
}
}
}
foreach (var nms in DeferredStaticMethodSignatureSetUpdates)
{
var set = MethodSignatures.GetOrCreateFor(nms.Name);
set.Add(nms);
}
}
public bool IsFullyInitialized {
get {
return _FullyInitialized;
}
}
public string ChangedName {
get {
var parms = Metadata.GetAttributeParameters("JSIL.Meta.JSChangeName");
if (parms != null)
return (string)parms[0].Value;
return null;
}
}
public IEnumerable<TypeInfo> SelfAndBaseTypesRecursive {
get {
yield return this;
var baseType = BaseClass;
while (baseType != null) {
yield return baseType;
baseType = baseType.BaseClass;
}
}
}
public int DerivedTypeCount {
get {
return _DerivedTypeCount;
}
}
public override string ToString () {
return Definition.FullName;
}
/// <summary>
/// All interfaces implemented by this type and its base types.
/// Does not include interfaces implemented by those interfaces.
/// </summary>
public ArraySegment<RecursiveInterfaceToken> AllInterfacesRecursive {
get {
if (_AllInterfacesRecursive.Array == null) {
var list = new List<RecursiveInterfaceToken>();
var types = SelfAndBaseTypesRecursive.Reverse();
foreach (var type in types)
foreach (var @interface in type.Interfaces.ToEnumerable())
list.Add(new RecursiveInterfaceToken(type, @interface));
// FIXME: Using ImmutableArrayPool here can leak.
_AllInterfacesRecursive = new ArraySegment<RecursiveInterfaceToken>(list
.Distinct(new RecursiveInterfaceTokenComparer())
.ToArray());
}
return _AllInterfacesRecursive;
}
}
internal void ConstructMethodGroups () {
if (_MethodGroupsInitialized)
return;
_MethodGroupsInitialized = true;
var methodGroups = (from kvp in Members where kvp.Key.Type == MemberIdentifier.MemberType.Method
let m = (MethodInfo)kvp.Value
group m by new {
m.Member.Name,
m.Member.GenericParameters.Count,
m.IsStatic
} into mg
where mg.Count() > 1
select mg).ToArray();
foreach (var mg in methodGroups) {
var filtered = (from m in mg where !m.IsIgnored &&
!m.Metadata.HasAttribute("JSIL.Meta.JSReplacement") &&
!m.Metadata.HasAttribute("JSIL.Meta.JSChangeName")
select m).ToArray();
if (filtered.Length <= 1)
continue;
var groupName = filtered.First().Name;
var mgi = new MethodGroupInfo(
this, filtered.ToArray(), groupName
);
foreach (var m in mg)
m.MethodGroup = mgi;
MethodGroups.Add(mgi);
}
}
public bool IsIgnored {
get {
if (_FullyInitialized)
return _IsIgnored;
if (Definition.DeclaringType != null) {
var dt = Source.GetExisting(Definition.DeclaringType);
if ((dt != null) && dt.IsIgnored)
return true;
}
return _IsIgnored;
}
}
public bool IsUnstubbable {
get {
// FIXME: Need GetExisting logic?
return _IsUnstubbable;
}
}
public bool IsExternal {
get {
if (_FullyInitialized)
return _IsExternal;
if (Definition.DeclaringType != null) {
var dt = Source.GetExisting(Definition.DeclaringType);
if ((dt != null) && dt.IsExternal)
return true;
}
return _IsExternal;
}
}
public bool IsStubOnly
{
get
{
if (_FullyInitialized)
return _IsStubOnly;
if (Definition.DeclaringType != null)
{
var dt = Source.GetExisting(Definition.DeclaringType);
if ((dt != null) && dt.IsStubOnly)
return true;
}
return _IsStubOnly;
}
}
public bool IsSuppressDeclaration
{
get
{
if (_FullyInitialized)
return _IsSuppressDeclaration;
/*if (Definition.DeclaringType != null)