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

Commit 3e93181

Browse files
Merge pull request #375 from xamarin/dynamic-self-indexer
Added test and a few fixes for Dynamic Self in subscripts
2 parents 40cccd1 + 96704f6 commit 3e93181

File tree

3 files changed

+67
-3
lines changed

3 files changed

+67
-3
lines changed

SwiftReflector/OverrideBuilder.cs

+11-2
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,13 @@ SLSubscript DefineOverrideSubscript (FunctionDeclaration getter, FunctionDeclara
781781

782782
var idents = new List<string> ();
783783
idents.Add ("self");
784+
785+
if (isProtocol) {
786+
SubstituteForSelf = isExistential ? "XamGlue.EveryProtocol" : OverriddenClass.ToFullyQualifiedNameWithGenerics ();
787+
} else {
788+
SubstituteForSelf = null;
789+
}
790+
784791
var getBlock = new SLCodeBlock (null);
785792

786793
{
@@ -809,6 +816,7 @@ SLSubscript DefineOverrideSubscript (FunctionDeclaration getter, FunctionDeclara
809816
var ifblock = new SLCodeBlock (null);
810817

811818
var marshal = new MarshalEngineSwiftToCSharp (Imports, idents, typeMapper);
819+
marshal.SubstituteForSelf = SubstituteForSelf;
812820
ifblock.AddRange (marshal.MarshalFunctionCall (getter,
813821
vtRef, VTableEntryIdentifier (index)));
814822

@@ -855,6 +863,7 @@ SLSubscript DefineOverrideSubscript (FunctionDeclaration getter, FunctionDeclara
855863
var ifblock = new SLCodeBlock (null);
856864

857865
var marshal = new MarshalEngineSwiftToCSharp (Imports, idents, typeMapper);
866+
marshal.SubstituteForSelf = SubstituteForSelf;
858867
ifblock.AddRange (marshal.MarshalFunctionCall (setter,
859868
vtRef, VTableEntryIdentifier (index + 1)));
860869

@@ -876,11 +885,11 @@ SLSubscript DefineOverrideSubscript (FunctionDeclaration getter, FunctionDeclara
876885
}
877886
SLType returnType = null;
878887
if (getter.IsTypeSpecGeneric (getter.ReturnTypeSpec)) {
879-
var ns = (NamedTypeSpec)getter.ReturnTypeSpec;
888+
var ns = (NamedTypeSpec)getter.ReturnTypeSpec.ReplaceName ("Self", SubstituteForSelf);
880889
var depthIndex = getter.GetGenericDepthAndIndex (ns.Name);
881890
returnType = new SLGenericReferenceType (depthIndex.Item1, depthIndex.Item2);
882891
} else {
883-
returnType = typeMapper.OverrideTypeSpecMapper.MapType (getter, Imports, getter.ReturnTypeSpec, true);
892+
returnType = typeMapper.OverrideTypeSpecMapper.MapType (getter, Imports, getter.ReturnTypeSpec.ReplaceName ("Self", SubstituteForSelf), true);
884893
}
885894

886895
return new SLSubscript (Visibility.Public, isProtocol ? FunctionKind.None : FunctionKind.Override, returnType,

SwiftReflector/SwiftXmlReflection/FunctionDeclaration.cs

+1-1
Original file line numberDiff line numberDiff line change
@@ -359,7 +359,7 @@ public override bool HasDynamicSelf {
359359

360360
public override bool HasDynamicSelfInReturnOnly {
361361
get {
362-
if (IsProperty)
362+
if (IsProperty && !IsSubscript)
363363
return false;
364364
if (TypeSpec.IsNullOrEmptyTuple (ReturnTypeSpec) || !ReturnTypeSpec.HasDynamicSelf)
365365
return false;

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

+55
Original file line numberDiff line numberDiff line change
@@ -210,5 +210,60 @@ public func whoProp<T> (a: T) where T: Identity4 {
210210
TestRunning.TestAndExecute (swiftCode, callingCode, "Got here.\n", otherClass: auxClass, platform: PlatformName.macOS);
211211

212212
}
213+
214+
215+
[Test]
216+
[Ignore ("This test takes out the swift compiler when compiling wrappers")]
217+
// issue opened here https://github.com/xamarin/binding-tools-for-swift/issues/374
218+
public void TestSelfSubscriptGet ()
219+
{
220+
var swiftCode = @"
221+
public protocol Identity5 {
222+
subscript (index: Int) -> Self {
223+
get
224+
}
225+
}
226+
227+
public func whoScript <T> (a: T) where T : Identity5 {
228+
a[7]
229+
}
230+
";
231+
232+
// public class Foo4 : IIdentity5<Foo4>
233+
// {
234+
// public Foo4 () { }
235+
// public Foo4 this [nint index] {
236+
// get {
237+
// Console.WriteLine ("got here " + index);
238+
// }
239+
// }
240+
// }
241+
var auxClass = new CSClass (CSVisibility.Public, "Foo4");
242+
var ifaceType = new CSSimpleType ("IIdentity5", false, auxClass.ToCSType ());
243+
auxClass.Inheritance.Add (new CSIdentifier (ifaceType.ToString ()));
244+
245+
var ctor = new CSMethod (CSVisibility.Public, CSMethodKind.None, null, auxClass.Name,
246+
new CSParameterList (), new CSCodeBlock ());
247+
248+
var indexIdent = new CSIdentifier ("index");
249+
250+
var getBody = CSCodeBlock.Create (CSFunctionCall.ConsoleWriteLine (CSConstant.Val (" got here ") + indexIdent));
251+
252+
var parameters = new CSParameterList (new CSParameter (new CSSimpleType ("nint"), indexIdent));
253+
254+
var indexer = new CSProperty (auxClass.ToCSType (), CSMethodKind.None, CSVisibility.Public, getBody,
255+
CSVisibility.Public, null, parameters);
256+
257+
auxClass.Constructors.Add (ctor);
258+
auxClass.Properties.Add (indexer);
259+
260+
var instName = new CSIdentifier ("inst");
261+
var instDecl = CSVariableDeclaration.VarLine (instName, new CSFunctionCall (auxClass.Name, new Dynamo.CommaListElementCollection<CSBaseExpression> (), true));
262+
var methodCall = CSFunctionCall.FunctionCallLine ("TopLevelEntities.WhoScript", false, instName);
263+
var callingCode = CSCodeBlock.Create (instDecl, methodCall);
264+
265+
TestRunning.TestAndExecute (swiftCode, callingCode, "got here 7\n", otherClass: auxClass, platform: PlatformName.macOS);
266+
267+
}
213268
}
214269
}

0 commit comments

Comments
 (0)