This repository has been archived by the owner on Oct 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 95
/
objcgenerator-postprocessor.cs
188 lines (149 loc) · 7.07 KB
/
objcgenerator-postprocessor.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
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
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using IKVM.Reflection;
using Type = IKVM.Reflection.Type;
namespace Embeddinator.ObjC {
// A set of post-processing steps needed to add hints
// to the input of the generation step
public partial class ObjCProcessor {
TypeMapper Mapper = new TypeMapper ();
protected IEnumerable<ProcessedMethod> PostProcessMethods (IEnumerable<ProcessedMethod> methods)
{
var equals = new HashSet<MethodInfo> ();
foreach (var m in methods) {
if (m.MethodType == MethodType.NSObjectProcotolIsEqual)
equals.Add (m.Method);
}
HashSet<MethodInfo> operatorToIgnore = new HashSet<MethodInfo> (OperatorOverloads.FindOperatorPairToIgnore (methods, equals));
foreach (var processedMethod in methods) {
var method = processedMethod.Method;
if (operatorToIgnore.Contains (method)) {
Delayed.Add (ErrorHelper.CreateWarning (1033, $"Method {method.Name} is not generated because another method exposes the operator with a friendly name"));
continue;
}
if (IsOperatorOrFriendlyVersion (method))
processedMethod.IsOperator = true;
ProcessPotentialName (processedMethod);
if (Mapper.CheckForDuplicateSelectors (processedMethod) == DuplicationStatus.Unresolvable) {
Delayed.Add (ErrorHelper.CreateWarning (1052, $"Element {method.Name} is not generated as its name conflicts with other elements on the same class."));
continue;
}
Mapper.Register (processedMethod);
processedMethod.Freeze ();
yield return processedMethod;
}
}
void ProcessPotentialName (ProcessedMethod processedMethod)
{
MethodInfo method = processedMethod.Method;
if (IsOperatorOrFriendlyVersion (method)) {
string nameOverride = OperatorOverloads.GetObjCName (processedMethod.Method.Name, processedMethod.Method.GetParameters ().Length);
if (nameOverride != null)
processedMethod.NameOverride = nameOverride;
}
string objCSignature = processedMethod.ObjCSignature;
if (RestrictedObjSelectors.IsImportantSelector (objCSignature)) {
string newName = "managed" + method.Name.PascalCase ();
processedMethod.NameOverride = newName;
Delayed.Add (ErrorHelper.CreateWarning (1051, $"Element {method.Name} is generated instead as {newName} because its name conflicts with an important objective-c selector."));
}
}
public bool IsOperatorOrFriendlyVersion (MethodInfo method)
{
return method.IsOperatorMethod () || OperatorOverloads.MatchesOperatorFriendlyName (method);
}
protected IEnumerable<ProcessedProperty> PostProcessProperties (IEnumerable<ProcessedProperty> properties)
{
foreach (ProcessedProperty processedProperty in properties) {
ProcessPotentialName (processedProperty);
if (Mapper.CheckForDuplicateSelectors (processedProperty) == DuplicationStatus.Unresolvable){
Delayed.Add (ErrorHelper.CreateWarning (1052, $"Element {processedProperty.Name} is not generated as its name conflicts with other elements on the same class."));
continue;
}
Mapper.Register (processedProperty);
processedProperty.Freeze ();
yield return processedProperty;
}
}
void ProcessPotentialName (ProcessedProperty processedProperty)
{
string getSignature = processedProperty.HasGetter ? processedProperty.GetMethod.ObjCSignature : "";
string setSignature = processedProperty.HasSetter ? processedProperty.SetMethod.ObjCSignature : "";
if (RestrictedObjSelectors.IsImportantSelector (getSignature) || RestrictedObjSelectors.IsImportantSelector (setSignature)) {
string newName = "managed" + processedProperty.Name.PascalCase ();
Delayed.Add (ErrorHelper.CreateWarning (1051, $"Element {processedProperty.Name} is generated instead as {newName} because its name conflicts with an important objective-c selector."));
processedProperty.NameOverride = newName;
}
}
protected IEnumerable<ProcessedProperty> PostProcessSubscriptProperties (IEnumerable<ProcessedProperty> properties)
{
foreach (ProcessedProperty processedProperty in properties) {
if (Mapper.CheckForDuplicateSelectors (processedProperty) == DuplicationStatus.Unresolvable) {
Delayed.Add (ErrorHelper.CreateWarning (1052, $"Element {processedProperty.Name} is not generated as its name conflicts with other elements on the same class."));
continue;
}
Mapper.Register (processedProperty);
yield return processedProperty;
}
}
void ProcessPotentialName (ProcessedFieldInfo processedField)
{
if (RestrictedObjSelectors.IsImportantSelector (processedField.GetterName) || RestrictedObjSelectors.IsImportantSelector (processedField.SetterName)) {
string newName = "managed" + processedField.Name.PascalCase ();
Delayed.Add (ErrorHelper.CreateWarning (1051, $"Element {processedField.Name} is generated instead as {newName} because its name conflicts with an important objective-c selector."));
processedField.NameOverride = newName;
}
}
protected IEnumerable<ProcessedFieldInfo> PostProcessFields (IEnumerable<ProcessedFieldInfo> fields)
{
foreach (ProcessedFieldInfo processedField in fields) {
ProcessPotentialName (processedField);
if (Mapper.CheckForDuplicateSelectors (processedField) == DuplicationStatus.Unresolvable) {
Delayed.Add (ErrorHelper.CreateWarning (1052, $"Element {processedField.Name} is not generated as its name conflicts with other elements on the same class."));
continue;
}
Mapper.Register (processedField);
yield return processedField;
}
}
protected IEnumerable<ProcessedConstructor> PostProcessConstructors (IEnumerable<ProcessedConstructor> constructors)
{
foreach (ProcessedConstructor processedConstructor in constructors) {
if (Mapper.CheckForDuplicateSelectors (processedConstructor) == DuplicationStatus.Unresolvable)
continue;
Mapper.Register (processedConstructor);
processedConstructor.Freeze ();
yield return processedConstructor;
}
}
}
static class RestrictedNames
{
static readonly HashSet<string> Names = new HashSet<string> { "static", "auto" };
public static bool IsRestrictedName (string name)
{
return Names.Contains (name);
}
}
static class RestrictedObjSelectors
{
static readonly HashSet<string> ImportantObjcSelectors = new HashSet<string> { "hash", "class", "superclass", "isEqual:", "self", "isKindOfClass:",
"isMemberOfClass:", "respondsToSelector:", "conformsToProtocol:", "description", "debugDescription", "performSelector:", "performSelector:withObject:",
"performSelector:withObject:withObject:", "isProxy", "retain", "release", "autorelease", "retainCount", "zone" };
static public bool IsImportantSelector (string selector)
{
if (selector.StartsWith ("get", StringComparison.Ordinal))
selector = selector.Substring (3).CamelCase ();
if (selector.StartsWith ("set", StringComparison.Ordinal)) {
selector = selector.Substring (3).CamelCase ();
int colonLocation = selector.IndexOf (':');
if (colonLocation > 0)
selector = selector.Substring (0, colonLocation);
}
return ImportantObjcSelectors.Contains (selector);
}
}
}