Skip to content

Commit e592a2b

Browse files
committed
Fix properties when a setter precedes a getter
Signed-off-by: Dimitar Dobrev <[email protected]>
1 parent ed6f8f1 commit e592a2b

File tree

4 files changed

+57
-22
lines changed

4 files changed

+57
-22
lines changed

src/Generator/Passes/GetterSetterToPropertyPass.cs

+39-19
Original file line numberDiff line numberDiff line change
@@ -136,15 +136,16 @@ private static Property GetProperty(Method method, string name,
136136
{
137137
Type underlyingType = GetUnderlyingType(type);
138138
Class @class = (Class) method.Namespace;
139+
139140
Property property = @class.Properties.Find(
140141
p => p.Field == null &&
141-
((!isSetter && p.HasSetter && p.Name == name) ||
142-
(isSetter && p.HasGetter &&
143-
GetReadWritePropertyName(p.GetMethod, name) == name)) &&
144-
((p.HasGetter &&
145-
GetUnderlyingType(p.GetMethod.OriginalReturnType).Equals(underlyingType)) ||
146-
(p.HasSetter &&
147-
GetUnderlyingType(p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType)))) ??
142+
((!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)) ??
148149
new Property { Name = name, QualifiedType = type };
149150

150151
if (property.Namespace == null)
@@ -160,13 +161,43 @@ private static Property GetProperty(Method method, string name,
160161
(int) method.Access);
161162
}
162163

163-
property.Name = property.OriginalName = name;
164164
method.GenerationKind = GenerationKind.Internal;
165165
if (method.ExplicitInterfaceImpl != null)
166166
property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl;
167167
return property;
168168
}
169169

170+
private static bool Match(Property property, string name)
171+
{
172+
if (string.IsNullOrEmpty(name))
173+
return false;
174+
175+
if (property.Name == name)
176+
return true;
177+
178+
if (property.Name == RemovePrefix(name))
179+
return true;
180+
181+
if (RemovePrefix(property.Name) == name)
182+
{
183+
property.Name = property.OriginalName = name;
184+
return true;
185+
}
186+
187+
return property.SetMethod != null &&
188+
GetPropertyNameFromSetter(property.SetMethod.Name) == name;
189+
}
190+
191+
private static string RemovePrefix(string identifier)
192+
{
193+
if (string.IsNullOrEmpty(identifier))
194+
return identifier;
195+
196+
string name = GetPropertyName(identifier);
197+
return name.StartsWith("is", StringComparison.Ordinal) && name != "is" ?
198+
char.ToLowerInvariant(name[2]) + name.Substring(3) : name;
199+
}
200+
170201
private static void ProcessProperties(Class @class, IEnumerable<Property> newProperties)
171202
{
172203
foreach (Property property in newProperties)
@@ -275,17 +306,6 @@ private static void RenameConflictingMethods(Class @class, Property property)
275306
}
276307
}
277308

278-
private static string GetReadWritePropertyName(INamedDecl getter, string afterSet)
279-
{
280-
string name = GetPropertyName(getter.Name);
281-
if (name != afterSet && name.StartsWith("is", StringComparison.Ordinal) &&
282-
name != "is")
283-
{
284-
name = char.ToLowerInvariant(name[2]) + name.Substring(3);
285-
}
286-
return name;
287-
}
288-
289309
private static Type GetUnderlyingType(QualifiedType type)
290310
{
291311
TagType tagType = type.Type as TagType;

src/Generator/Passes/MultipleInheritancePass.cs

+6-3
Original file line numberDiff line numberDiff line change
@@ -118,16 +118,19 @@ where property.IsDeclared
118118

119119
if (@interface.Bases.Count == 0)
120120
{
121+
QualifiedType intPtr = new QualifiedType(
122+
new BuiltinType(PrimitiveType.IntPtr));
121123
var instance = new Property
122-
{
124+
{
123125
Namespace = @interface,
124126
Name = Helpers.InstanceIdentifier,
125-
QualifiedType = new QualifiedType(new BuiltinType(PrimitiveType.IntPtr)),
127+
QualifiedType = intPtr,
126128
GetMethod = new Method
127129
{
128130
Name = Helpers.InstanceIdentifier,
129131
SynthKind = FunctionSynthKind.InterfaceInstance,
130-
Namespace = @interface
132+
Namespace = @interface,
133+
OriginalReturnType = intPtr
131134
}
132135
};
133136

tests/Common/Common.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -642,6 +642,15 @@ void TestProperties::setStartWithVerb(int value)
642642
{
643643
}
644644

645+
void TestProperties::setSetterBeforeGetter(bool value)
646+
{
647+
}
648+
649+
bool TestProperties::isSetterBeforeGetter()
650+
{
651+
return true;
652+
}
653+
645654
bool TestProperties::contains(char c)
646655
{
647656
return true;

tests/Common/Common.h

+3
Original file line numberDiff line numberDiff line change
@@ -620,6 +620,9 @@ struct DLL_API TestProperties
620620
int startWithVerb();
621621
void setStartWithVerb(int value);
622622

623+
void setSetterBeforeGetter(bool value);
624+
bool isSetterBeforeGetter();
625+
623626
bool contains(char c);
624627
bool contains(const char* str);
625628
private:

0 commit comments

Comments
 (0)