Skip to content

Commit d986132

Browse files
committed
Split generation from addition of properties
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent 9ea4289 commit d986132

File tree

2 files changed

+43
-71
lines changed

2 files changed

+43
-71
lines changed

src/Generator/Generators/CSharp/CSharpSources.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -629,7 +629,7 @@ private void GatherClassInternalFunctions(Class @class, bool includeCtors,
629629

630630
foreach (var method in @class.Methods)
631631
{
632-
if (ASTUtils.CheckIgnoreMethod(method))
632+
if (!method.IsGenerated || ASTUtils.CheckIgnoreMethod(method))
633633
continue;
634634

635635
if (method.IsConstructor)

src/Generator/Passes/GetterSetterToPropertyPass.cs

Lines changed: 42 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ public override bool VisitClassDecl(Class @class)
6868

6969
protected virtual IEnumerable<Property> GenerateProperties(Class @class)
7070
{
71-
var newProperties = new List<Property>();
71+
var properties = new List<Property>();
7272
foreach (Method method in @class.Methods.Where(
7373
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated &&
7474
m.SynthKind != FunctionSynthKind.DefaultValueOverload &&
@@ -78,39 +78,29 @@ protected virtual IEnumerable<Property> GenerateProperties(Class @class)
7878
if (IsGetter(method))
7979
{
8080
string name = GetPropertyName(method.Name);
81-
QualifiedType type = method.OriginalReturnType;
82-
Property property = GetProperty(method, name, type);
83-
if (!property.HasGetter)
84-
{
85-
property.GetMethod = method;
86-
property.QualifiedType = method.OriginalReturnType;
87-
newProperties.Add(property);
88-
}
89-
else
90-
method.GenerationKind = GenerationKind.Generate;
81+
GetProperty(properties, method, name, method.OriginalReturnType);
9182
continue;
9283
}
9384
if (IsSetter(method))
9485
{
9586
string name = GetPropertyNameFromSetter(method.Name);
96-
QualifiedType type = method.Parameters.First(p => p.Kind == ParameterKind.Regular).QualifiedType;
97-
Property property = GetProperty(method, name, type, true);
98-
property.SetMethod = method;
99-
newProperties.Add(property);
87+
QualifiedType type = method.Parameters.First(
88+
p => p.Kind == ParameterKind.Regular).QualifiedType;
89+
GetProperty(properties, method, name, type, true);
10090
}
10191
}
10292

103-
return CleanUp(@class, newProperties);
93+
return CleanUp(@class, properties);
10494
}
10595

106-
private IEnumerable<Property> CleanUp(Class @class, List<Property> newProperties)
96+
private IEnumerable<Property> CleanUp(Class @class, List<Property> properties)
10797
{
10898
if (!Options.UsePropertyDetectionHeuristics)
109-
return newProperties;
99+
return properties;
110100

111-
for (int i = newProperties.Count - 1; i >= 0; i--)
101+
for (int i = properties.Count - 1; i >= 0; i--)
112102
{
113-
Property property = newProperties[i];
103+
Property property = properties[i];
114104
if (property.HasSetter)
115105
continue;
116106

@@ -124,47 +114,46 @@ private IEnumerable<Property> CleanUp(Class @class, List<Property> newProperties
124114
{
125115
property.GetMethod.GenerationKind = GenerationKind.Generate;
126116
@class.Properties.Remove(property);
127-
newProperties.RemoveAt(i);
117+
properties.RemoveAt(i);
128118
}
129119
}
130120

131-
return newProperties;
121+
return properties;
132122
}
133123

134-
private static Property GetProperty(Method method, string name,
135-
QualifiedType type, bool isSetter = false)
124+
private static void GetProperty(List<Property> properties, Method method,
125+
string name, QualifiedType type, bool isSetter = false)
136126
{
137127
Type underlyingType = GetUnderlyingType(type);
138128
Class @class = (Class) method.Namespace;
139129

140-
Property property = @class.Properties.Find(
130+
Property property = properties.Find(
141131
p => p.Field == null &&
142132
((!isSetter && p.SetMethod?.IsStatic == method.IsStatic) ||
143-
(isSetter && p.GetMethod?.IsStatic == method.IsStatic)) &&
144-
((p.HasGetter && GetUnderlyingType(
145-
p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
146-
(p.HasSetter && GetUnderlyingType(
147-
p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType))) &&
148-
Match(p, name)) ??
149-
new Property { Name = name, QualifiedType = type };
150-
151-
if (property.Namespace == null)
152-
{
153-
property.Namespace = method.Namespace;
154-
property.Access = method.Access;
155-
@class.Properties.Add(property);
156-
}
133+
(isSetter && p.GetMethod?.IsStatic == method.IsStatic)) &&
134+
((p.HasGetter && GetUnderlyingType(
135+
p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
136+
(p.HasSetter && GetUnderlyingType(
137+
p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType))) &&
138+
Match(p, name));
139+
140+
if (property == null)
141+
properties.Add(property = new Property { Name = name, QualifiedType = type });
142+
143+
if (isSetter)
144+
property.SetMethod = method;
157145
else
158146
{
159-
property.Access = (AccessSpecifier) Math.Max(
160-
(int) (property.GetMethod ?? property.SetMethod).Access,
161-
(int) method.Access);
147+
property.GetMethod = method;
148+
property.QualifiedType = method.OriginalReturnType;
162149
}
163150

164-
method.GenerationKind = GenerationKind.Internal;
151+
property.Access = (AccessSpecifier) Math.Max(
152+
(int) (property.GetMethod ?? property.SetMethod).Access,
153+
(int) method.Access);
154+
165155
if (method.ExplicitInterfaceImpl != null)
166156
property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl;
167-
return property;
168157
}
169158

170159
private static bool Match(Property property, string name)
@@ -198,37 +187,30 @@ private static string RemovePrefix(string identifier)
198187
char.ToLowerInvariant(name[2]) + name.Substring(3) : name;
199188
}
200189

201-
private static void ProcessProperties(Class @class, IEnumerable<Property> newProperties)
190+
private static void ProcessProperties(Class @class, IEnumerable<Property> properties)
202191
{
203-
foreach (Property property in newProperties)
192+
foreach (Property property in properties)
204193
{
205194
ProcessOverridden(@class, property);
206195

207196
if (!property.HasGetter)
208-
{
209-
if (property.HasSetter)
210-
property.SetMethod.GenerationKind = GenerationKind.Generate;
211-
@class.Properties.Remove(property);
212197
continue;
213-
}
214198
if (!property.HasSetter &&
215199
@class.GetOverloads(property.GetMethod).Any(
216200
m => m != property.GetMethod && !m.Ignore))
217-
{
218-
property.GetMethod.GenerationKind = GenerationKind.Generate;
219-
@class.Properties.Remove(property);
220201
continue;
221-
}
222202

223-
Property conflict = newProperties.LastOrDefault(
203+
Property conflict = properties.LastOrDefault(
224204
p => p.Name == property.Name && p != property &&
225205
p.ExplicitInterfaceImpl == property.ExplicitInterfaceImpl);
226-
if (conflict != null && conflict.GetMethod != null)
227-
{
228-
conflict.GetMethod.GenerationKind = GenerationKind.Generate;
206+
if (conflict?.GetMethod != null)
229207
conflict.GetMethod = null;
230-
}
231208

209+
property.GetMethod.GenerationKind = GenerationKind.Internal;
210+
if (property.SetMethod != null)
211+
property.SetMethod.GenerationKind = GenerationKind.Internal;
212+
property.Namespace = @class;
213+
@class.Properties.Add(property);
232214
RenameConflictingMethods(@class, property);
233215
CombineComments(property);
234216
}
@@ -243,24 +225,14 @@ private static void ProcessOverridden(Class @class, Property property)
243225
if (baseProperty == null)
244226
{
245227
if (property.HasSetter)
246-
{
247-
property.SetMethod.GenerationKind = GenerationKind.Generate;
248228
property.SetMethod = null;
249-
}
250229
else
251-
{
252-
property.GetMethod.GenerationKind = GenerationKind.Generate;
253230
property.GetMethod = null;
254-
}
255231
}
256232
else if (!property.HasGetter && baseProperty.HasSetter)
257233
property.GetMethod = baseProperty.GetMethod;
258234
else if (!property.HasSetter || !baseProperty.HasSetter)
259-
{
260-
if (property.HasSetter)
261-
property.SetMethod.GenerationKind = GenerationKind.Generate;
262235
property.SetMethod = baseProperty.SetMethod;
263-
}
264236
}
265237

266238
private static void RenameConflictingMethods(Class @class, Property property)

0 commit comments

Comments
 (0)