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

Commit dbca458

Browse files
Merge pull request #768 from xamarin/swiftsequence
Redo of IteratorProtocol with tests
2 parents 1995a5b + f445ca9 commit dbca458

File tree

14 files changed

+437
-216
lines changed

14 files changed

+437
-216
lines changed

SwiftReflector/NewClassCompiler.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5406,6 +5406,28 @@ WrappingResult WrapModuleContents (
54065406
}
54075407
}
54085408

5409+
List<string> GetBoundGenericsForAssociatedTypes (TypeDeclaration decl, ProtocolDeclaration proto, CSUsingPackages use)
5410+
{
5411+
var aliases = decl.TypeAliases;
5412+
var result = new List<string> ();
5413+
var protoName = proto.ToFullyQualifiedName ();
5414+
foreach (var assoc in proto.AssociatedTypes) {
5415+
var possibleTypeName = $"{protoName}.{assoc.Name}";
5416+
foreach (var alias in aliases) {
5417+
var namedTypeSpec = alias.TypeSpec as NamedTypeSpec;
5418+
if (namedTypeSpec == null)
5419+
throw new NotImplementedException ("You should never get an alias name that is not a NamedTypeSpec");
5420+
var typeName = namedTypeSpec.NameWithoutModule;
5421+
if (typeName == assoc.Name || typeName == possibleTypeName) {
5422+
var ntb = TypeMapper.MapType (decl, alias.TargetTypeSpec, false);
5423+
var csType = ntb.ToCSType (use);
5424+
result.Add (csType.ToString ());
5425+
}
5426+
}
5427+
}
5428+
return result;
5429+
}
5430+
54095431
void AddInheritedProtocols (TypeDeclaration decl, CSClass cl, ClassContents classContents, string trimmedLibPath,
54105432
CSUsingPackages use, ErrorHandling errors)
54115433
{
@@ -5423,8 +5445,19 @@ void AddInheritedProtocols (TypeDeclaration decl, CSClass cl, ClassContents clas
54235445
string itemName = null;
54245446
if (iface.Namespace == typeof (SwiftError).Namespace && iface.TypeName == typeof (SwiftError).Name)
54255447
itemName = "ISwiftError";
5426-
else
5427-
itemName = iface.TypeName;
5448+
else {
5449+
var entity = TypeMapper.GetEntityForTypeSpec (inh.InheritedTypeSpec);
5450+
if (entity == null || entity.Type is ProtocolDeclaration proto && proto.AssociatedTypes.Count == 0) {
5451+
itemName = iface.TypeName;
5452+
} else {
5453+
var boundTypes = GetBoundGenericsForAssociatedTypes (decl, entity.Type as ProtocolDeclaration, use);
5454+
var sb = new StringBuilder ();
5455+
sb.Append (iface.TypeName).Append ('<');
5456+
sb.Append (boundTypes.InterleaveCommas ());
5457+
sb.Append ('>');
5458+
itemName = sb.ToString ();
5459+
}
5460+
}
54285461
return new CSIdentifier (itemName);
54295462
}));
54305463

SwiftReflector/SwiftInterfaceReflector/SwiftInterfaceReflector.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -994,18 +994,26 @@ XElement HandleGenerics (Generic_parameter_clauseContext genericContext, Generic
994994

995995
public override void ExitTypealias_declaration ([NotNull] Typealias_declarationContext context)
996996
{
997-
// only care about top level typealiases
998-
if (currentElement.Peek ().Name != kModule)
999-
return;
1000997
var name = UnTick (context.typealias_name ().GetText ());
1001998
var generics = context.generic_parameter_clause ()?.GetText () ?? "";
1002999
var targetType = context.typealias_assignment ().type ().GetText ();
10031000
var access = ToAccess (context.access_level_modifier ());
10041001
var map = new XElement (kTypeAlias, new XAttribute (kName, name + generics),
10051002
new XAttribute (kAccessibility, access),
10061003
new XAttribute (kType, targetType));
1007-
if (access != null)
1008-
typeAliasMap.Add (map);
1004+
if (access != null) {
1005+
if (currentElement.Peek ().Name == kModule) {
1006+
typeAliasMap.Add (map);
1007+
} else {
1008+
var curr = currentElement.Peek ();
1009+
var aliaslist = curr.Element (kTypeAliases);
1010+
if (aliaslist == null) {
1011+
aliaslist = new XElement (kTypeAliases);
1012+
curr.Add (aliaslist);
1013+
}
1014+
aliaslist.Add (map);
1015+
}
1016+
}
10091017
}
10101018

10111019
XElement MakeConformanceWhere (string name, string from)

SwiftReflector/SwiftXmlReflection/TypeDeclaration.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public TypeDeclaration ()
2323
InnerEnums = new List<EnumDeclaration> ();
2424
Members = new List<Member> ();
2525
Inheritance = new List<Inheritance> ();
26+
TypeAliases = new List<TypeAliasDeclaration> ();
2627
}
2728

2829
public TypeKind Kind { get; set; }
@@ -31,6 +32,7 @@ public TypeDeclaration ()
3132
public List<ClassDeclaration> InnerClasses { get; set; }
3233
public List<StructDeclaration> InnerStructs { get; set; }
3334
public List<EnumDeclaration> InnerEnums { get; set; }
35+
public List<TypeAliasDeclaration> TypeAliases { get; set; }
3436
public bool IsObjC { get; set; }
3537
public bool IsFinal { get; set; }
3638
public bool IsDeprecated { get; set; }
@@ -167,6 +169,10 @@ protected virtual void GatherXObjects (List<XObject> xobjects)
167169
xobjects.Add (new XElement ("members", memcontents.ToArray ()));
168170
List<XObject> inherits = new List<XObject> (Inheritance.Select (i => i.ToXElement ()));
169171
xobjects.Add (new XElement ("inherits", inherits.ToArray ()));
172+
if (TypeAliases.Count > 0) {
173+
var aliases = new List<XObject> (TypeAliases.Select (a => a.ToXElement ()));
174+
xobjects.Add (new XElement ("typealiases", aliases.ToArray ()));
175+
}
170176
}
171177

172178
#endregion
@@ -205,6 +211,12 @@ public static TypeDeclaration TypeFromXElement (TypeAliasFolder folder, XElement
205211
select SwiftReflector.SwiftXmlReflection.Inheritance.FromXElement (folder, inherit) as Inheritance;
206212
decl.Inheritance.AddRange (inherits);
207213
}
214+
var typealiases = elem.Element ("typealiases");
215+
if (typealiases != null) {
216+
var aliases = from alias in typealiases.Elements ()
217+
select TypeAliasDeclaration.FromXElement (module.Name, alias);
218+
decl.TypeAliases.AddRange (aliases);
219+
}
208220
EnumDeclaration edecl = decl as EnumDeclaration;
209221
if (edecl != null) {
210222
var enumElements = (from enumElement in elem.Element ("elements").Elements ()

SwiftRuntimeLibrary.Mac/SwiftRuntimeLibrary.Mac.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -259,9 +259,6 @@
259259
<Compile Include="..\SwiftRuntimeLibrary\SwiftTypeRegistry.cs">
260260
<Link>SwiftTypeRegistry.cs</Link>
261261
</Compile>
262-
<Compile Include="..\SwiftRuntimeLibrary\SwiftIteratorProtocol.cs">
263-
<Link>SwiftIteratorProtocol.cs</Link>
264-
</Compile>
265262
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\SwiftProtocolConformanceDescriptor.cs">
266263
<Link>SwiftMarshal\SwiftProtocolConformanceDescriptor.cs</Link>
267264
</Compile>
@@ -289,6 +286,12 @@
289286
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\SwiftThrowsAttribute.cs">
290287
<Link>SwiftMarshal\SwiftThrowsAttribute.cs</Link>
291288
</Compile>
289+
<Compile Include="..\SwiftRuntimeLibrary\ISwiftIteratorProtocol.cs">
290+
<Link>ISwiftIteratorProtocol.cs</Link>
291+
</Compile>
292+
<Compile Include="..\SwiftRuntimeLibrary\SwiftIteratorProtocolProxy.cs">
293+
<Link>SwiftIteratorProtocolProxy.cs</Link>
294+
</Compile>
292295
</ItemGroup>
293296
<ItemGroup>
294297
<Folder Include="SwiftMarshal\" />

SwiftRuntimeLibrary.iOS/SwiftRuntimeLibrary.iOS.csproj

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -264,9 +264,6 @@
264264
<Compile Include="..\SwiftRuntimeLibrary\SwiftTypeRegistry.cs">
265265
<Link>SwiftTypeRegistry.cs</Link>
266266
</Compile>
267-
<Compile Include="..\SwiftRuntimeLibrary\SwiftIteratorProtocol.cs">
268-
<Link>SwiftIteratorProtocol.cs</Link>
269-
</Compile>
270267
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\SwiftProtocolConformanceDescriptor.cs">
271268
<Link>SwiftMarshal\SwiftProtocolConformanceDescriptor.cs</Link>
272269
</Compile>
@@ -294,6 +291,12 @@
294291
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\SwiftThrowsAttribute.cs">
295292
<Link>SwiftMarshal\SwiftThrowsAttribute.cs</Link>
296293
</Compile>
294+
<Compile Include="..\SwiftRuntimeLibrary\ISwiftIteratorProtocol.cs">
295+
<Link>ISwiftIteratorProtocol.cs</Link>
296+
</Compile>
297+
<Compile Include="..\SwiftRuntimeLibrary\SwiftIteratorProtocolProxy.cs">
298+
<Link>SwiftIteratorProtocolProxy.cs</Link>
299+
</Compile>
297300
</ItemGroup>
298301
<Target Name="GeneratedCSCode" BeforeTargets="CoreCompile" Inputs="$(MSBuildProjectFullPath)" Outputs="GeneratedCode\BindingMetadata.iOS.cs">
299302
<Exec Command="make --directory=../SwiftRuntimeLibrary create-pinvokes-ios configuration=$(Configuration)" />
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// Copyright (c) Microsoft Corporation.
2+
// Licensed under the MIT License.
3+
4+
using System;
5+
using System.Runtime.InteropServices;
6+
using SwiftRuntimeLibrary;
7+
using SwiftRuntimeLibrary.SwiftMarshal;
8+
#if __IOS__ || __MACOS__ || __TVOS__ || __WATCHOS__
9+
using ObjCRuntime;
10+
#endif
11+
using System.Reflection;
12+
13+
namespace SwiftRuntimeLibrary {
14+
[SwiftProtocolType (typeof (SwiftIteratorProtocolProtocol<>), "libswiftCore.dylib", "$sStMp", true)]
15+
[SwiftTypeName ("Swift.IteratorProtocol")]
16+
public interface ISwiftIteratorProtocol<ATElement> {
17+
SwiftOptional<ATElement> Next ();
18+
}
19+
}

SwiftRuntimeLibrary/SwiftIteratorProtocol.cs

Lines changed: 0 additions & 126 deletions
This file was deleted.

0 commit comments

Comments
 (0)