Skip to content

Commit 51978ad

Browse files
committed
Restore writeable properties starting with verbs
Signed-off-by: Dimitar Dobrev <dpldobrev@protonmail.com>
1 parent e11057d commit 51978ad

File tree

4 files changed

+63
-34
lines changed

4 files changed

+63
-34
lines changed

src/Generator/Passes/GetterSetterToPropertyPass.cs

Lines changed: 48 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -66,10 +66,10 @@ public override bool VisitClassDecl(Class @class)
6666
return false;
6767
}
6868

69-
protected virtual HashSet<Property> GenerateProperties(Class @class)
69+
protected virtual IEnumerable<Property> GenerateProperties(Class @class)
7070
{
71-
var newProperties = new HashSet<Property>();
72-
foreach (var method in @class.Methods.Where(
71+
var newProperties = new List<Property>();
72+
foreach (Method method in @class.Methods.Where(
7373
m => !m.IsConstructor && !m.IsDestructor && !m.IsOperator && m.IsGenerated &&
7474
m.SynthKind != FunctionSynthKind.DefaultValueOverload &&
7575
m.SynthKind != FunctionSynthKind.ComplementOperator &&
@@ -80,7 +80,7 @@ protected virtual HashSet<Property> GenerateProperties(Class @class)
8080
string name = GetPropertyName(method.Name);
8181
QualifiedType type = method.OriginalReturnType;
8282
Property property = GetProperty(method, name, type);
83-
if (property.GetMethod == null)
83+
if (!property.HasGetter)
8484
{
8585
property.GetMethod = method;
8686
property.QualifiedType = method.OriginalReturnType;
@@ -100,6 +100,34 @@ protected virtual HashSet<Property> GenerateProperties(Class @class)
100100
}
101101
}
102102

103+
return CleanUp(@class, newProperties);
104+
}
105+
106+
private IEnumerable<Property> CleanUp(Class @class, List<Property> newProperties)
107+
{
108+
if (!Options.UsePropertyDetectionHeuristics)
109+
return newProperties;
110+
111+
for (int i = newProperties.Count - 1; i >= 0; i--)
112+
{
113+
Property property = newProperties[i];
114+
if (property.HasSetter)
115+
continue;
116+
117+
string firstWord = GetFirstWord(property.GetMethod.Name);
118+
if (firstWord.Length < property.GetMethod.Name.Length &&
119+
Match(firstWord, new[] { "get", "is", "has" }))
120+
continue;
121+
122+
if (Match(firstWord, new[] { "to", "new" }) ||
123+
verbs.Contains(firstWord))
124+
{
125+
property.GetMethod.GenerationKind = GenerationKind.Generate;
126+
@class.Properties.Remove(property);
127+
newProperties.RemoveAt(i);
128+
}
129+
}
130+
103131
return newProperties;
104132
}
105133

@@ -111,11 +139,11 @@ private static Property GetProperty(Method method, string name,
111139
Property property = @class.Properties.Find(
112140
p => p.Field == null &&
113141
(p.Name == name ||
114-
(isSetter && p.GetMethod != null &&
142+
(isSetter && p.HasGetter &&
115143
GetReadWritePropertyName(p.GetMethod, name) == name)) &&
116-
((p.GetMethod != null &&
144+
((p.HasGetter &&
117145
GetUnderlyingType(p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
118-
(p.SetMethod != null &&
146+
(p.HasSetter &&
119147
GetUnderlyingType(p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType)))) ??
120148
new Property { Name = name, QualifiedType = type };
121149

@@ -139,20 +167,20 @@ private static Property GetProperty(Method method, string name,
139167
return property;
140168
}
141169

142-
private static void ProcessProperties(Class @class, HashSet<Property> newProperties)
170+
private static void ProcessProperties(Class @class, IEnumerable<Property> newProperties)
143171
{
144-
foreach (var property in newProperties)
172+
foreach (Property property in newProperties)
145173
{
146174
ProcessOverridden(@class, property);
147175

148-
if (property.GetMethod == null)
176+
if (!property.HasGetter)
149177
{
150-
if (property.SetMethod != null)
178+
if (property.HasSetter)
151179
property.SetMethod.GenerationKind = GenerationKind.Generate;
152180
@class.Properties.Remove(property);
153181
continue;
154182
}
155-
if (property.SetMethod == null &&
183+
if (!property.HasSetter &&
156184
@class.GetOverloads(property.GetMethod).Any(
157185
m => m != property.GetMethod && !m.Ignore))
158186
{
@@ -174,7 +202,7 @@ private static void ProcessOverridden(Class @class, Property property)
174202
Property baseProperty = GetBaseProperty(@class, property);
175203
if (baseProperty == null)
176204
{
177-
if (property.SetMethod != null)
205+
if (property.HasSetter)
178206
{
179207
property.SetMethod.GenerationKind = GenerationKind.Generate;
180208
property.SetMethod = null;
@@ -185,11 +213,11 @@ private static void ProcessOverridden(Class @class, Property property)
185213
property.GetMethod = null;
186214
}
187215
}
188-
else if (property.GetMethod == null && baseProperty.SetMethod != null)
216+
else if (!property.HasGetter && baseProperty.HasSetter)
189217
property.GetMethod = baseProperty.GetMethod;
190-
else if (property.SetMethod == null || baseProperty.SetMethod == null)
218+
else if (!property.HasSetter || !baseProperty.HasSetter)
191219
{
192-
if (property.SetMethod != null)
220+
if (property.HasSetter)
193221
property.SetMethod.GenerationKind = GenerationKind.Generate;
194222
property.SetMethod = baseProperty.SetMethod;
195223
}
@@ -325,24 +353,10 @@ private static string GetPropertyNameFromSetter(string name)
325353
return nameBuilder.ToString();
326354
}
327355

328-
private bool IsGetter(Method method)
329-
{
330-
if (method.IsDestructor ||
331-
method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void) ||
332-
method.Parameters.Any(p => p.Kind != ParameterKind.IndirectReturnType))
333-
return false;
334-
var firstWord = GetFirstWord(method.Name);
335-
336-
if (firstWord.Length < method.Name.Length &&
337-
Match(firstWord, new[] { "get", "is", "has" }))
338-
return true;
339-
340-
if (Options.UsePropertyDetectionHeuristics &&
341-
!Match(firstWord, new[] { "to", "new" }) && !verbs.Contains(firstWord))
342-
return true;
343-
344-
return false;
345-
}
356+
private bool IsGetter(Method method) =>
357+
!method.IsDestructor &&
358+
!method.OriginalReturnType.Type.IsPrimitiveType(PrimitiveType.Void) &&
359+
!method.Parameters.Any(p => p.Kind != ParameterKind.IndirectReturnType);
346360

347361
private static bool IsSetter(Method method)
348362
{

tests/Common/Common.Tests.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -524,6 +524,9 @@ public void TestProperties()
524524
Assert.That(prop.IsEmpty, Is.EqualTo(prop.Empty));
525525

526526
Assert.That(prop.VirtualGetter, Is.EqualTo(15));
527+
528+
Assert.That(prop.StartWithVerb, Is.EqualTo(25));
529+
prop.StartWithVerb = 5;
527530
}
528531
using (var prop = new HasOverridenSetter())
529532
{

tests/Common/Common.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -633,6 +633,15 @@ int TestProperties::virtualGetter()
633633
return 15;
634634
}
635635

636+
int TestProperties::startWithVerb()
637+
{
638+
return 25;
639+
}
640+
641+
void TestProperties::setStartWithVerb(int value)
642+
{
643+
}
644+
636645
HasOverridenSetter::HasOverridenSetter()
637646
{
638647
}

tests/Common/Common.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -616,6 +616,9 @@ struct DLL_API TestProperties
616616
bool empty();
617617

618618
virtual int virtualGetter();
619+
620+
int startWithVerb();
621+
void setStartWithVerb(int value);
619622
private:
620623
int FieldValue;
621624
double _refToPrimitiveInSetter;

0 commit comments

Comments
 (0)