Skip to content
This repository was archived by the owner on Dec 12, 2024. It is now read-only.

Commit 7d2cea7

Browse files
author
Stephen Hawley
committed
Handle difference between static and instance properties with the same name
1 parent c5f3899 commit 7d2cea7

File tree

4 files changed

+49
-24
lines changed

4 files changed

+49
-24
lines changed

SwiftReflector/MethodWrapping.cs

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,15 +85,16 @@ public static string WrapperOperatorName (TypeMapper typeMapper, string moduleNa
8585
return $"{kXamPrefix}{classPrefix}{operatorType}{operatorName}";
8686
}
8787

88-
public static string WrapperName (SwiftClassName name, string methodName, bool isExtension)
88+
public static string WrapperName (SwiftClassName name, string methodName, bool isExtension, bool isStatic)
8989
{
90-
return WrapperName (name.ToFullyQualifiedName (false), methodName, isExtension);
90+
return WrapperName (name.ToFullyQualifiedName (false), methodName, isExtension, isStatic);
9191
}
9292

93-
public static string WrapperName (string fullyQualifiedClassName, string methodName, bool isExtension)
93+
public static string WrapperName (string fullyQualifiedClassName, string methodName, bool isExtension, bool isStatic)
9494
{
95-
string classPrefix = fullyQualifiedClassName.Replace ('.', 'D');
96-
return $"{kXamPrefix}{classPrefix}{(isExtension ? 'E' : 'D')}{methodName}";
95+
var classPrefix = fullyQualifiedClassName.Replace ('.', 'D');
96+
var separator = isExtension ? 'E' : (isStatic ? 'd' : 'D');
97+
return $"{kXamPrefix}{classPrefix}{separator}{methodName}";
9798
}
9899

99100
public static string WrapperCtorName (SwiftClassName name, bool isExtension)
@@ -102,9 +103,9 @@ public static string WrapperCtorName (SwiftClassName name, bool isExtension)
102103
return $"{kXamPrefix}{classPrefix}{(isExtension ? 'E' : 'D')}{name.Terminus}";
103104
}
104105

105-
public static string WrapperName (SwiftClassName name, string methodName, PropertyType propType, bool isSubScript, bool isExtension)
106+
public static string WrapperName (SwiftClassName name, string methodName, PropertyType propType, bool isSubScript, bool isExtension, bool isStatic)
106107
{
107-
return WrapperName (name.ToFullyQualifiedName (), methodName, propType, isSubScript, isExtension);
108+
return WrapperName (name.ToFullyQualifiedName (), methodName, propType, isSubScript, isExtension, isStatic);
108109
}
109110

110111
public static string EnumFactoryCaseWrapperName (SwiftClassName name, string caseName)
@@ -137,7 +138,7 @@ public static string EnumCaseFinderWrapperName (EnumDeclaration en)
137138
return string.Format ("{0}{1}ec", kXamPrefix, classPrefix);
138139
}
139140

140-
public static string WrapperName (string fullyQualifiedClassName, string methodName, PropertyType propType, bool isSubScript, bool isExtension)
141+
public static string WrapperName (string fullyQualifiedClassName, string methodName, PropertyType propType, bool isSubScript, bool isExtension, bool isStatic)
141142
{
142143
var lastIndex = fullyQualifiedClassName.LastIndexOf ('.');
143144
if (lastIndex >= 0) {
@@ -147,13 +148,13 @@ public static string WrapperName (string fullyQualifiedClassName, string methodN
147148
char propMarker = isSubScript ? 's' : 'p';
148149
switch (propType) {
149150
case PropertyType.Getter:
150-
propMarker = 'G';
151+
propMarker = isStatic ? 'g' : 'G';
151152
break;
152153
case PropertyType.Setter:
153-
propMarker = 'S';
154+
propMarker = isStatic ? 's' : 'S';
154155
break;
155156
case PropertyType.Materializer:
156-
propMarker = 'M';
157+
propMarker = isStatic ? 'm' : 'M';
157158
break;
158159
default:
159160
throw ErrorHelper.CreateError (ReflectorError.kWrappingBase + 0, $"unknown property type {propType.ToString ()} wrapping function {methodName} in {fullyQualifiedClassName}");
@@ -469,11 +470,11 @@ void WrapProperty (PropertyDeclaration decl, ModuleInventory modInventory, SLFil
469470
setter.ParameterLists [0].Add (parameter);
470471
}
471472

472-
var getWrapperName = WrapperName (decl.Module.Name, decl.Name, PropertyType.Getter, false, decl.IsExtension);
473+
var getWrapperName = WrapperName (decl.Module.Name, decl.Name, PropertyType.Getter, false, decl.IsExtension, decl.IsStatic);
473474
var getWrapper = MapTopLevelFuncToWrapperFunc (slfile.Imports, getter, getWrapperName);
474475
slfile.Functions.Add (getWrapper);
475476
if (setter != null) {
476-
var setWrapperName = WrapperName (decl.Module.Name, decl.Name, PropertyType.Setter, false, decl.IsExtension);
477+
var setWrapperName = WrapperName (decl.Module.Name, decl.Name, PropertyType.Setter, false, decl.IsExtension, decl.IsStatic);
477478
var setWrapper = MapTopLevelFuncToWrapperFunc (slfile.Imports, setter, setWrapperName);
478479
slfile.Functions.Add (setWrapper);
479480
}
@@ -1702,15 +1703,15 @@ SLFunc MapFuncDeclToWrapperFunc (SwiftClassName className, SLImportModules modul
17021703
} else if (funcDecl.IsSubscript) {
17031704
funcName = WrapperName (className, funcDecl.PropertyName,
17041705
(funcDecl.IsGetter ? PropertyType.Getter :
1705-
(funcDecl.IsSetter ? PropertyType.Setter : PropertyType.Materializer)), true, funcDecl.IsExtension);
1706+
(funcDecl.IsSetter ? PropertyType.Setter : PropertyType.Materializer)), true, funcDecl.IsExtension, funcDecl.IsStatic);
17061707
} else if (funcDecl.IsProperty) {
17071708
funcName = WrapperName (className, funcDecl.PropertyName,
17081709
(funcDecl.IsGetter ? PropertyType.Getter :
1709-
(funcDecl.IsSetter ? PropertyType.Setter : PropertyType.Materializer)), false, funcDecl.IsExtension);
1710+
(funcDecl.IsSetter ? PropertyType.Setter : PropertyType.Materializer)), false, funcDecl.IsExtension, funcDecl.IsStatic);
17101711
} else if (funcDecl.IsOperator) {
17111712
funcName = WrapperOperatorName (typeMapper, funcDecl.Parent.ToFullyQualifiedName (true), funcDecl.Name, funcDecl.OperatorType);
17121713
} else {
1713-
funcName = WrapperName (className, funcDecl.Name, funcDecl.IsExtension);
1714+
funcName = WrapperName (className, funcDecl.Name, funcDecl.IsExtension, funcDecl.IsStatic);
17141715
}
17151716

17161717
var funcBody = new SLCodeBlock (preMarshalCode);

SwiftReflector/NewClassCompiler.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -389,7 +389,7 @@ void CompileTLProp (PropertyDeclaration prop, Version swiftLangVersion, ModuleIn
389389
string piSetterRef = null;
390390
string syntheticClassName = prop.Module.Name + "." + CompilerNames.GlobalFunctionClassName;
391391

392-
string getWrapperName = MethodWrapping.WrapperName (prop.Module.Name, prop.Name, PropertyType.Getter, false, prop.IsExtension);
392+
string getWrapperName = MethodWrapping.WrapperName (prop.Module.Name, prop.Name, PropertyType.Getter, false, prop.IsExtension, prop.IsStatic);
393393
getterWrapper = FindTLPropWrapper (prop, getWrapperName, wrapper);
394394
var getterWrapperFunc = FindEquivalentFunctionDeclarationForWrapperFunction (getterWrapper, TypeMapper, wrapper);
395395

@@ -405,7 +405,7 @@ void CompileTLProp (PropertyDeclaration prop, Version swiftLangVersion, ModuleIn
405405

406406
if (!prop.IsLet && (prop.Storage != StorageKind.Computed ||
407407
(prop.Storage == StorageKind.Computed && setter != null))) {
408-
string setWrapperName = MethodWrapping.WrapperName (prop.Module.Name, prop.Name, PropertyType.Setter, false, prop.IsExtension);
408+
string setWrapperName = MethodWrapping.WrapperName (prop.Module.Name, prop.Name, PropertyType.Setter, false, prop.IsExtension, prop.IsStatic);
409409
setterWrapper = FindTLPropWrapper (prop, setWrapperName, wrapper);
410410
setterWrapperFunc = FindEquivalentFunctionDeclarationForWrapperFunction (setterWrapper, TypeMapper, wrapper);
411411

@@ -1146,9 +1146,9 @@ FunctionDeclaration FindSuperWrapper (FunctionDeclaration superFunc, WrappingRes
11461146
var parentName = PrefixInsertedBeforeName (superFunc.Parent, superFunc.IsProperty, namePrefix);
11471147
if (superFunc.IsProperty) {
11481148
wrapperName = MethodWrapping.WrapperName (parentName,
1149-
superFunc.PropertyName, superFunc.IsGetter ? PropertyType.Getter : PropertyType.Setter, superFunc.IsSubscript, false);
1149+
superFunc.PropertyName, superFunc.IsGetter ? PropertyType.Getter : PropertyType.Setter, superFunc.IsSubscript, false, superFunc.IsStatic);
11501150
} else {
1151-
wrapperName = MethodWrapping.WrapperName (parentName, superFunc.Name, false);
1151+
wrapperName = MethodWrapping.WrapperName (parentName, superFunc.Name, false, superFunc.IsStatic);
11521152
}
11531153
var referenceCode = wrapper.FunctionReferenceCodeMap.ReferenceCodeFor (superFunc);
11541154
if (referenceCode != null) {
@@ -5127,9 +5127,10 @@ TLFunction FindWrapperForExtension (FunctionDeclaration funcDeclToWrap, BaseDecl
51275127
if (funcDeclToWrap.IsProperty) {
51285128
wrapperName = MethodWrapping.WrapperName (extensionOn.ToFullyQualifiedName (true), funcDeclToWrap.PropertyName,
51295129
(funcDeclToWrap.IsGetter ? PropertyType.Getter :
5130-
(funcDeclToWrap.IsSetter ? PropertyType.Setter : PropertyType.Materializer)), false, funcDeclToWrap.IsExtension);
5130+
(funcDeclToWrap.IsSetter ? PropertyType.Setter : PropertyType.Materializer)), false, funcDeclToWrap.IsExtension,
5131+
funcDeclToWrap.IsStatic);
51315132
} else {
5132-
wrapperName = MethodWrapping.WrapperName (extensionOn.ToFullyQualifiedName (false), funcDeclToWrap.Name, true);
5133+
wrapperName = MethodWrapping.WrapperName (extensionOn.ToFullyQualifiedName (false), funcDeclToWrap.Name, true, funcDeclToWrap.IsStatic);
51335134
}
51345135
return FindWrapperForMethod (funcDeclToWrap, funcToWrap, wrapperName, wrapper);
51355136

@@ -5165,15 +5166,15 @@ TLFunction FindWrapperForMethod (FunctionDeclaration funcToWrap, TLFunction meth
51655166
var prop = methodToWrap.Signature as SwiftPropertyType;
51665167
if (prop == null)
51675168
throw ErrorHelper.CreateError (ReflectorError.kCantHappenBase + 34, $"Expected a SwiftPropertyType for method signature, but got {methodToWrap.Signature.GetType ().Name}");
5168-
var wrapperName = MethodWrapping.WrapperName (methodToWrap.Class.ClassName, methodToWrap.Name.Name, propType, prop.IsSubscript, funcToWrap.IsExtension);
5169+
var wrapperName = MethodWrapping.WrapperName (methodToWrap.Class.ClassName, methodToWrap.Name.Name, propType, prop.IsSubscript, funcToWrap.IsExtension, prop.IsStatic);
51695170
return FindWrapperForMethod (funcToWrap, methodToWrap, wrapperName, wrapper);
51705171
}
51715172

51725173

51735174
TLFunction FindWrapperForMethod (FunctionDeclaration funcToWrap, TLFunction methodToWrap, WrappingResult wrapper)
51745175
{
51755176
string wrapperName = methodToWrap.Operator == OperatorType.None ?
5176-
MethodWrapping.WrapperName (methodToWrap.Class.ClassName, methodToWrap.Name.Name, funcToWrap.IsExtension) :
5177+
MethodWrapping.WrapperName (methodToWrap.Class.ClassName, methodToWrap.Name.Name, funcToWrap.IsExtension, funcToWrap.IsStatic) :
51775178
MethodWrapping.WrapperOperatorName (TypeMapper, funcToWrap.Parent.ToFullyQualifiedName (true), methodToWrap.Name.Name, funcToWrap.OperatorType);
51785179
return FindWrapperForMethod (funcToWrap, methodToWrap, wrapperName, wrapper);
51795180
}

SwiftReflector/SwiftXmlReflection/PropertyDeclaration.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ FunctionDeclaration SearchForPropertyAccessor (string prefix)
6565
{
6666
var funcs = GetFunctionsToSearch ();
6767
return funcs.FirstOrDefault (f => f.IsProperty &&
68+
f.IsStatic == IsStatic &&
6869
f.Name.StartsWith (prefix, StringComparison.Ordinal) &&
6970
(f.Name.Length == prefix.Length + Name.Length) &&
7071
string.CompareOrdinal (f.Name, prefix.Length, Name, 0, Name.Length) == 0);

tests/tom-swifty-test/SwiftReflector/HomonymTests.cs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,5 +167,27 @@ public void TestOperatorHomonymSmokeTest ()
167167
var callingCode = CSCodeBlock.Create (printIt);
168168
TestRunning.TestAndExecute (swiftCode, callingCode, "success\n");
169169
}
170+
171+
[Test]
172+
public void TestPropNameConflict ()
173+
{
174+
var swiftCode = @"
175+
public class PropConflict {
176+
public init () { }
177+
public static var X:Int = 3
178+
public var X:Float = 4.0
179+
}
180+
";
181+
var classNameId = new CSIdentifier ("PropConflict");
182+
var propVarId = new CSIdentifier ("X");
183+
var propVarConflictId = new CSIdentifier ("X0");
184+
var propId = new CSIdentifier ("p");
185+
var propDecl = CSVariableDeclaration.VarLine (propId, new CSFunctionCall ("PropConflict", true));
186+
var printer1 = CSFunctionCall.ConsoleWriteLine (propId.Dot (propVarConflictId));
187+
var printer2 = CSFunctionCall.ConsoleWriteLine (classNameId.Dot (propVarId));
188+
var callingCode = CSCodeBlock.Create (propDecl, printer1, printer2);
189+
190+
TestRunning.TestAndExecute (swiftCode, callingCode, "4\n3\n");
191+
}
170192
}
171193
}

0 commit comments

Comments
 (0)