-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathClassDef.cs
145 lines (126 loc) · 5.19 KB
/
ClassDef.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
using System;
using System.Collections.Generic;
using System.Linq;
using static System.Diagnostics.Debug;
using Mono.Cecil;
namespace Generator.Types
{
public class ClassDef : TypeDef
{
public override TypeKind Kind
{
get
{
return TypeKind.Class;
}
}
public override bool CanBeSkipped
{
get
{
return Type.Interfaces.Count == 0 && !statics.Any();
}
}
private TypeDefinition[] statics;
private string aliasedType;
private string[] factories;
private List<ClassMethodDef> methodWrappers = new List<ClassMethodDef>();
public ClassDef(TypeDefinition t) : base(t)
{
// this is required early to know whether the definition can be skipped
statics = GetStaticTypes().ToArray();
}
public override void CollectDependencies()
{
foreach (var staticType in statics)
{
AddDependency(Generator.GetTypeDefinition(staticType));
}
factories = GetFactoryTypes().Select(f => TypeHelpers.GetTypeName(Generator, this, f, TypeUsage.Alias)).ToArray();
if (Type.Interfaces.Count > 0)
{
var defaultInterface = TypeHelpers.GetDefaultInterface(Type);
aliasedType = TypeHelpers.GetTypeName(Generator, this, defaultInterface, TypeUsage.Alias);
}
var factoryMethods = GetFactoryTypes().OrderBy(f => f.FullName).SelectMany(f => Generator.GetTypeDefinition(f).Methods).ToArray();
var staticMethods = statics.OrderBy(s => s.FullName).SelectMany(s => Generator.GetTypeDefinition(s).Methods).ToArray();
foreach (var m in factoryMethods)
{
methodWrappers.Add(new ClassMethodDef(m, this, true));
}
foreach (var m in staticMethods)
{
methodWrappers.Add(new ClassMethodDef(m, this, false));
}
// fix name clashes in method wrappers (caused by overloads from different interfaces)
foreach (var nameToFix in methodWrappers.GroupBy(m => m.Name).Where(g => g.Count() > 1))
{
// we expect that there is a single base name (modulo suffix numbers) for all clashing interfaces
string baseName = nameToFix.Select(n => n.WrappedMethod.DeclaringType.Name.TrimEnd('1', '2', '3', '4', '5', '6', '7', '8', '9', '0')).Distinct().Single();
foreach (var m in nameToFix)
{
// so far this is always "2"
var nameSuffix = m.WrappedMethod.DeclaringType.Name.Replace(baseName, "");
m.FixupName(nameSuffix);
}
}
}
public override void Emit()
{
var classType = DefinitionName;
bool needClassID = false;
if (Type.Interfaces.Count > 0)
{
var dependsOnAssemblies = new List<string>(ForeignAssemblyDependencies.GroupBy(t => t.Module.Assembly.Name.Name).Select(g => g.Key));
var features = new FeatureConditions(dependsOnAssemblies);
string fullname = '"' + Type.FullName + '"';
Module.Append($@"
{ features.GetAttribute() }RT_CLASS!{{class { DefinitionName }: { aliasedType } [{ fullname }]}}");
if (!features.IsEmpty)
{
// if the aliased type is from a different assembly that is not included, just use IInspectable instead
// otherwise types depending on this class would transitively depend on the aliased type
Module.Append($@"
{ features.GetInvertedAttribute() }RT_CLASS!{{class { DefinitionName }: IInspectable [{ fullname }]}}");
}
foreach (var factory in factories.OrderBy(f => f))
{
needClassID = true;
Module.Append($@"
impl RtActivatable<{ factory }> for { classType } {{}}");
}
}
else
{
Assert(!factories.Any());
Module.Append($@"
RT_CLASS!{{static class { DefinitionName }}}");
}
foreach (var staticType in statics.OrderBy(t => t.FullName))
{
var staticName = Generator.GetTypeDefinition(staticType).DefinitionName;
needClassID = true;
Module.Append($@"
impl RtActivatable<{ staticName }> for { classType } {{}}");
}
if (IsDefaultActivatable())
{
needClassID = true;
Module.Append($@"
impl RtActivatable<IActivationFactory> for { classType } {{}}");
}
if (methodWrappers.Any())
{
Module.Append($@"
impl { DefinitionName } {{
{ String.Join("\r\n ", methodWrappers.Select(m => m.Emit())) }
}}");
}
if (needClassID)
{
Module.Append($@"
DEFINE_CLSID!({ classType }(&[{ NameHelpers.StringToUTF16WithZero(Type.FullName) }]) [CLSID_{ classType }]);");
}
}
}
}