-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJSIdentifierTypes.cs
More file actions
897 lines (719 loc) · 26 KB
/
Copy pathJSIdentifierTypes.cs
File metadata and controls
897 lines (719 loc) · 26 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ICSharpCode.Decompiler.ILAst;
using JSIL.Internal;
using Mono.Cecil;
namespace JSIL.Ast {
public class JSStringIdentifier : JSIdentifier {
public readonly string Text;
private readonly bool _IsLValue;
public JSStringIdentifier (string text, TypeReference type = null, bool isLValue = false)
: base(type) {
Text = text;
_IsLValue = isLValue;
}
public override string Identifier {
get { return Text; }
}
public override bool IsLValue {
get {
return _IsLValue;
}
}
public override bool IsConstant {
get {
return true;
}
}
}
public class JSRawOutputIdentifier : JSIdentifier {
public readonly string Format;
public readonly object[] Arguments;
public JSRawOutputIdentifier (TypeReference type, string format, params object[] arguments)
: base(type) {
if (type == null)
throw new ArgumentNullException("type");
Format = format;
Arguments = arguments;
}
public override bool IsLValue {
get {
// HACK
return true;
}
}
public override string Identifier {
get {
if ((Arguments != null) && (Arguments.Length > 0))
return String.Format(Format, Arguments);
else
return Format;
}
}
public void WriteTo (JavascriptFormatter output) {
if ((Arguments != null) && (Arguments.Length > 0))
output.WriteRaw(Format, Arguments);
else
output.WriteRaw(Format);
}
}
public class JSNamespace : JSStringIdentifier {
public JSNamespace (string name)
: base(name) {
}
public override bool IsConstant {
get {
return true;
}
}
}
public class JSAssembly : JSIdentifier {
public readonly AssemblyDefinition Assembly;
public JSAssembly (AssemblyDefinition assembly) {
if (assembly == null)
throw new ArgumentNullException("assembly");
Assembly = assembly;
}
public override string Identifier {
get { return Assembly.FullName; }
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
return typeSystem.Object;
}
public override JSLiteral ToLiteral () {
return JSLiteral.New(Assembly);
}
public override bool IsConstant {
get {
return true;
}
}
}
public class JSReflectionAssembly : JSAssembly {
public JSReflectionAssembly (AssemblyDefinition assembly)
: base(assembly) {
}
public override JSLiteral ToLiteral () {
throw new InvalidOperationException();
}
public override bool IsConstant {
get {
return true;
}
}
}
public class JSType : JSIdentifier {
public readonly TypeReference Type;
string _cachedIdentifier = null;
public JSType (TypeReference type) {
if (type == null)
throw new ArgumentNullException("type");
Type = type;
}
public override string Identifier {
get {
if (_cachedIdentifier == null)
_cachedIdentifier = Util.DemangleCecilTypeName(Type.FullName);
return _cachedIdentifier;
}
}
public override bool HasGlobalStateDependency {
get {
return false;
}
}
public override bool IsConstant {
get {
return true;
}
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
return new TypeReference(
typeSystem.Boolean.Namespace, "RuntimeTypeHandle",
typeSystem.Boolean.Module, typeSystem.Boolean.Scope
);
}
public override JSLiteral ToLiteral () {
return JSLiteral.New(Type);
}
public static TypeReference ExtractType (JSExpression expression) {
while (expression != null) {
var pii = expression as JSPublicInterfaceOfExpression;
var jst = expression as JSType;
if (pii != null)
expression = pii.Inner;
else if (jst != null)
return jst.Type;
else
return null;
}
return null;
}
}
public class JSTypeReference : JSType {
public readonly TypeReference Context;
public JSTypeReference (TypeReference type, TypeReference context)
: base(type) {
Context = context;
}
}
public class JSCachedType : JSType {
public readonly int Index;
public JSCachedType (TypeReference type, int index)
: base(type) {
Index = index;
}
}
public class JSField : JSIdentifier {
public readonly FieldReference Reference;
public readonly FieldInfo Field;
public JSField (FieldReference reference, FieldInfo field) {
if (reference == null)
throw new ArgumentNullException("reference");
if (field == null)
throw new ArgumentNullException("field");
Reference = reference;
Field = field;
}
public override bool HasGlobalStateDependency {
get {
return Field.IsStatic;
}
}
public override string Identifier {
get { return Field.Name; }
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
return Field.ReturnType;
}
// FIXME: Is this right?
public override bool IsConstant {
get {
return true;
}
}
}
public class JSFieldOfExpression : JSField
{
public JSFieldOfExpression(FieldReference reference, FieldInfo field)
: base(reference, field)
{
}
}
public class JSProperty : JSIdentifier {
public readonly MemberReference Reference;
public readonly PropertyInfo Property;
public JSProperty (MemberReference reference, PropertyInfo property) {
if (reference == null)
throw new ArgumentNullException("reference");
if (property == null)
throw new ArgumentNullException("property");
Reference = reference;
Property = property;
}
public override bool HasGlobalStateDependency {
get {
return Property.IsStatic;
}
}
public override string Identifier {
get { return Property.Name; }
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
if (Property.ReturnType.IsGenericParameter)
return SubstituteTypeArgs(Property.Source, Property.ReturnType, Reference);
else
return Property.ReturnType;
}
// FIXME: Is this right?
public override bool IsConstant {
get {
return true;
}
}
}
public class JSMethod : JSIdentifier {
public readonly MethodTypeFactory MethodTypes;
public readonly IEnumerable<TypeReference> GenericArguments;
public readonly MethodReference Reference;
public readonly MethodInfo Method;
public JSMethod (
MethodReference reference, MethodInfo method, MethodTypeFactory methodTypes,
IEnumerable<TypeReference> genericArguments = null
) {
if (reference == null)
throw new ArgumentNullException("reference");
if (method == null)
throw new ArgumentNullException("method");
Reference = reference;
Method = method;
MethodTypes = methodTypes;
if (genericArguments == null) {
var gim = Reference as GenericInstanceMethod;
if (gim != null)
genericArguments = gim.GenericArguments;
}
GenericArguments = genericArguments;
}
public override string Identifier {
get { return Method.Name; }
}
public QualifiedMemberIdentifier QualifiedIdentifier {
get {
return new QualifiedMemberIdentifier(
Method.DeclaringType.Identifier, Method.Identifier
);
}
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
return MethodTypes.Get(Reference, typeSystem);
}
public override string ToString () {
return String.Format(
"<JSMethod {0}::{1}>",
Method.DeclaringType.Identifier,
Method.Name
);
}
public string GetNameForInstanceReference () {
// FIXME: Enable this so MultipleGenericInterfaces2.cs passes.
/*
// For methods that implement a method of a closed generic interface, we need to ensure we fully-qualify their name when necessary.
var declaringGit = Reference.DeclaringType as GenericInstanceType;
if ((declaringGit != null) && (declaringGit.ElementType.Resolve().IsInterface)) {
var parentTypeName = TypeUtil.GetLocalName(declaringGit.ElementType.Resolve());
parentTypeName = parentTypeName.Substring(0, parentTypeName.IndexOf("`"));
var genericArgsText = String.Join(",", from ga in declaringGit.GenericArguments select ga.FullName);
return String.Format("{0}<{1}>.{2}", parentTypeName, genericArgsText, Reference.Name);
} else
*/
return Method.GetName(false);
}
public JSCachedType[] CachedGenericArguments {
get;
private set;
}
internal void SetCachedGenericArguments (IEnumerable<JSCachedType> cachedTypes) {
var newCachedGenericArguments = cachedTypes.ToArray();
if (CachedGenericArguments != null) {
if (!CachedGenericArguments.SequenceEqual(newCachedGenericArguments))
throw new InvalidOperationException("Cached generic arguments already set and new set of cached types is different");
}
CachedGenericArguments = newCachedGenericArguments;
}
public override bool IsConstant {
get {
return true;
}
}
}
public class JSCachedMethod : JSMethod {
public readonly int Index;
public JSCachedMethod (
MethodReference reference, MethodInfo method, MethodTypeFactory methodTypes,
IEnumerable<TypeReference> genericArguments, int index
) : base (reference, method, methodTypes, genericArguments) {
Index = index;
}
}
[JSAstIgnoreInheritedMembers]
public class JSFakeMethod : JSIdentifier {
public readonly MethodTypeFactory MethodTypes;
public readonly string Name;
public readonly TypeReference ReturnType;
public readonly TypeReference[] ParameterTypes;
[JSAstTraverse(0)]
public readonly JSExpression[] GenericArguments;
public readonly bool Escape;
public JSFakeMethod (
string name, TypeReference returnType,
TypeReference[] parameterTypes, MethodTypeFactory methodTypes,
JSExpression[] genericArguments = null,
bool escape = false
) {
Name = name;
ReturnType = returnType;
ParameterTypes = parameterTypes ?? new TypeReference[0];
MethodTypes = methodTypes;
GenericArguments = genericArguments;
Escape = escape;
}
public override string Identifier {
get { return Name; }
}
public override void ReplaceChild (JSNode oldChild, JSNode newChild) {
if (GenericArguments == null)
return;
var oldExpression = oldChild as JSExpression;
var newExpression = newChild as JSExpression;
if (
(oldExpression != null) &&
((newExpression != null) == (newChild != null))
) {
for (var i = 0; i < GenericArguments.Length; i++) {
if (GenericArguments[i] == oldExpression)
GenericArguments[i] = newExpression;
}
}
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
return MethodTypes.Get(
ReturnType, ParameterTypes, typeSystem
);
}
public override bool IsConstant {
get {
return true;
}
}
}
[JSAstIgnoreInheritedMembers]
public class JSVariable : JSIdentifier {
public readonly MethodReference Function;
public readonly string Name;
protected readonly bool _IsReference;
[JSAstTraverse(0)]
public JSExpression DefaultValue;
public JSVariable (string name, TypeReference type, MethodReference function, JSExpression defaultValue = null)
: base(type) {
Name = name;
if (type == null)
throw new ArgumentNullException("type");
if (type is ByReferenceType) {
type = ((ByReferenceType)type).ElementType;
_IsReference = true;
} else {
_IsReference = false;
}
Function = function;
DefaultValue = defaultValue ?? new JSDefaultValueLiteral(type);
}
public override string Identifier {
get { return Name; }
}
public virtual bool IsReference {
get {
return _IsReference;
}
}
public virtual bool IsParameter {
get {
return false;
}
}
public virtual JSParameter GetParameter () {
throw new InvalidOperationException("Variable is not a parameter");
}
public virtual bool IsThis {
get {
return false;
}
}
public override bool IsLValue {
get {
return true;
}
}
public static JSVariable New (string name, TypeReference type, MethodReference function) {
return new JSVariable(name, type, function);
}
public static JSVariable New (ParameterReference parameter, MethodReference function) {
return new JSParameter(parameter.Name, parameter.ParameterType, function);
}
public virtual JSVariable Reference () {
return new JSVariableReference(this, Function);
}
public virtual JSVariable Dereference () {
if (IsReference)
return new JSVariableDereference(this, Function);
else
throw new InvalidOperationException("Dereferencing a non-reference");
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
return IdentifierType;
}
public override bool IsConstant {
get {
return false;
}
}
public override int GetHashCode () {
return Identifier.GetHashCode();
}
public override string ToString () {
var defaultValueText = "";
if (!DefaultValue.IsNull && !(DefaultValue is JSDefaultValueLiteral))
defaultValueText = String.Format(" := {0}", DefaultValue);
if (IsReference)
return String.Format("<{0} {1}{2}>", IdentifierType, Identifier, defaultValueText);
else if (IsThis)
return String.Format("<this {0}>", IdentifierType);
else if (IsParameter)
return String.Format("<parameter {0} {1}>", IdentifierType, Identifier);
else
return String.Format("<var {0} {1}{2}>", IdentifierType, Identifier, defaultValueText);
}
public override bool Equals (object obj) {
var rhs = obj as JSVariable;
if (rhs != null) {
if (rhs.Identifier != Identifier)
return false;
if (rhs.IsParameter != IsParameter)
return false;
else if (rhs.IsReference != IsReference)
return false;
else if (rhs.IsThis != IsThis)
return false;
else if (!TypeUtil.TypesAreEqual(IdentifierType, rhs.IdentifierType))
return false;
else
return true;
}
return EqualsImpl(obj, true);
}
public override void ReplaceChild (JSNode oldChild, JSNode newChild) {
if (newChild == this)
throw new InvalidOperationException("Direct cycle formed by replacement");
if (DefaultValue == oldChild)
DefaultValue = (JSExpression)newChild;
}
}
[JSAstIgnoreInheritedMembers]
public class JSClosureVariable : JSVariable {
internal JSClosureVariable (string name, TypeReference type, MethodReference function, JSExpression defaultValue = null)
: base (name, type, function, defaultValue) {
}
public static JSClosureVariable New (ILVariable variable, MethodReference function) {
return new JSClosureVariable(variable.Name, variable.Type, function);
}
}
[JSAstIgnoreInheritedMembers]
public class JSTemporaryVariable : JSVariable {
public JSTemporaryVariable (string name, TypeReference type, MethodReference function, JSExpression defaultValue = null)
: base (name, type, function, defaultValue) {
}
}
[JSAstIgnoreInheritedMembers]
public class JSParameter : JSVariable {
internal JSParameter (string name, TypeReference type, MethodReference function, bool escapeName = true)
: base(MaybeEscapeName(name, escapeName), type, function) {
}
public static string MaybeEscapeName (string name, bool actuallyEscape) {
if (!actuallyEscape)
return name;
if (name == "this")
return "@this";
return name;
}
public override bool IsParameter {
get {
return true;
}
}
public override JSParameter GetParameter () {
return this;
}
}
public class JSExceptionVariable : JSVariable {
public JSExceptionVariable (TypeSystem typeSystem, MethodReference function) :
base(
"$exception",
new TypeReference("System", "Exception", typeSystem.Object.Module, typeSystem.Object.Scope),
function
) {
}
public override bool IsConstant {
get {
return true;
}
}
public override bool IsParameter {
get {
return true;
}
}
}
[JSAstIgnoreInheritedMembers]
public class JSThisParameter : JSParameter {
public JSThisParameter (TypeReference type, MethodReference function) :
base("this", type, function, false)
{
}
public override bool IsThis {
get {
return true;
}
}
public override bool IsConstant {
get {
return true;
}
}
public static JSVariable New (TypeReference type, MethodReference function) {
if (type.IsValueType)
return new JSVariableReference(new JSThisParameter(type, function), function);
else
return new JSThisParameter(type, function);
}
}
public class JSVariableDereference : JSVariable {
public readonly JSVariable Referent;
public JSVariableDereference (JSVariable referent, MethodReference function)
: base(referent.Identifier, DeReferenceType(referent.IdentifierType), function) {
Referent = referent;
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
return DeReferenceType(Referent.GetActualType(typeSystem), true);
}
public override TypeReference IdentifierType {
get {
return DeReferenceType(Referent.IdentifierType, true);
}
}
public override bool IsReference {
get {
return DeReferenceType(Referent.IdentifierType, true) is ByReferenceType;
}
}
public override bool IsParameter {
get {
return Referent.IsParameter;
}
}
public override JSParameter GetParameter () {
return Referent.GetParameter();
}
public override bool IsThis {
get {
return Referent.IsThis;
}
}
public override JSVariable Reference () {
return Referent;
}
}
[JSAstIgnoreInheritedMembers]
public class JSIndirectVariable : JSVariable {
public readonly IDictionary<string, JSVariable> Variables;
public JSIndirectVariable (IDictionary<string, JSVariable> variables, string identifier, MethodReference function)
: base(identifier, variables[identifier].IdentifierType, function) {
Variables = variables;
}
private void CheckForMissingVariable () {
if (!Variables.ContainsKey(Identifier))
throw new KeyNotFoundException(String.Format("No variable named '{0}' currently exists.", Identifier));
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
CheckForMissingVariable();
return Variables[Identifier].GetActualType(typeSystem);
}
public override TypeReference IdentifierType {
get {
CheckForMissingVariable();
return Variables[Identifier].IdentifierType;
}
}
public override bool IsReference {
get {
CheckForMissingVariable();
return Variables[Identifier].IsReference;
}
}
public override bool IsParameter {
get {
CheckForMissingVariable();
return Variables[Identifier].IsParameter;
}
}
public override JSParameter GetParameter () {
CheckForMissingVariable();
return Variables[Identifier].GetParameter();
}
public override bool IsConstant {
get {
CheckForMissingVariable();
return Variables[Identifier].IsConstant;
}
}
public override bool IsThis {
get {
CheckForMissingVariable();
return Variables[Identifier].IsThis;
}
}
public override JSVariable Dereference () {
CheckForMissingVariable();
return Variables[Identifier].Dereference();
}
public override JSVariable Reference () {
CheckForMissingVariable();
return Variables[Identifier].Reference();
}
public JSVariable ActualVariable {
get {
CheckForMissingVariable();
return Variables[Identifier];
}
}
public override bool Equals (object obj) {
JSVariable variable;
if (Variables.TryGetValue(Identifier, out variable))
return variable.Equals(obj) || base.Equals(obj);
else {
variable = obj as JSVariable;
if ((variable != null) && (variable.Identifier == Identifier))
return true;
else
return base.Equals(obj);
}
}
public override int GetHashCode() {
return Identifier.GetHashCode();
}
public override string ToString () {
JSVariable variable;
if (Variables.TryGetValue(Identifier, out variable))
return String.Format("@{0}", variable);
else
return String.Format("@missing({0})", Identifier);
}
}
public class JSVariableReference : JSVariable {
public readonly JSVariable Referent;
public JSVariableReference (JSVariable referent, MethodReference function)
: base(referent.Identifier, new ByReferenceType(referent.IdentifierType), function) {
Referent = referent;
}
public override TypeReference GetActualType (TypeSystem typeSystem) {
return new ByReferenceType(Referent.GetActualType(typeSystem));
}
public override TypeReference IdentifierType {
get {
return new ByReferenceType(Referent.IdentifierType);
}
}
public override bool IsReference {
get {
return true;
}
}
public override JSParameter GetParameter () {
return Referent.GetParameter();
}
public override bool IsParameter {
get {
return Referent.IsParameter;
}
}
public override bool IsThis {
get {
return Referent.IsThis;
}
}
public override JSVariable Dereference () {
return Referent;
}
}
}