-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCacheBaseMethods.cs
More file actions
103 lines (82 loc) · 3.14 KB
/
Copy pathCacheBaseMethods.cs
File metadata and controls
103 lines (82 loc) · 3.14 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using JSIL.Ast;
using JSIL.Internal;
using Mono.Cecil;
namespace JSIL.Transforms {
public class BaseMethodCacher : JSAstVisitor {
public struct CachedMethodRecord {
public readonly JSMethod Method;
public readonly int Index;
public CachedMethodRecord (JSMethod method, int index) {
Method = method;
Index = index;
}
}
public readonly ITypeInfoSource TypeInfo;
public readonly Dictionary<QualifiedMemberIdentifier, CachedMethodRecord> CachedMethods;
public readonly TypeDefinition ThisType;
private int NextID = 0;
public BaseMethodCacher (ITypeInfoSource typeInfo, TypeDefinition thisType) {
TypeInfo = typeInfo;
ThisType = thisType;
CachedMethods = new Dictionary<QualifiedMemberIdentifier, CachedMethodRecord>(
new QualifiedMemberIdentifier.Comparer(TypeInfo)
);
}
private JSCachedMethod GetCachedMethod (JSMethod method) {
if (!IsCacheable(method))
return null;
var type = method.Reference.DeclaringType.Resolve();
if (type == null)
return null;
var identifier = new QualifiedMemberIdentifier(
new TypeIdentifier(type),
new MemberIdentifier(TypeInfo, method.Reference)
);
CachedMethodRecord record;
if (!CachedMethods.TryGetValue(identifier, out record))
CachedMethods.Add(identifier, record = new CachedMethodRecord(method, NextID++));
return new JSCachedMethod(
method.Reference, method.Method,
method.MethodTypes, method.GenericArguments,
record.Index
);
}
public bool IsCacheable (JSMethod method) {
if (method.Reference == null)
return false;
var type = method.Reference.DeclaringType;
// Same-type calls are excluded
if (TypeUtil.TypesAreEqual(type, ThisType))
return false;
// Exclude any type that isn't in our bases or interfaces
if (!TypeUtil.TypesAreAssignable(TypeInfo, type, ThisType))
return false;
// TODO: Exclude interfaces?
// Exclude generics
if (TypeUtil.ContainsGenericParameter(type))
return false;
if (TypeUtil.IsOpenType(type))
return false;
return true;
}
public void VisitNode (JSMethod method) {
var cm = GetCachedMethod(method);
if (cm != null) {
ParentNode.ReplaceChild(method, cm);
VisitReplacement(cm);
} else {
VisitChildren(method);
}
}
public void VisitNode (JSCachedMethod method) {
VisitChildren(method);
}
public void CacheMethodsForFunction (JSFunctionExpression function) {
Visit(function);
}
}
}