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

Redo of IteratorProtocol with tests #768

Merged
merged 5 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 35 additions & 2 deletions SwiftReflector/NewClassCompiler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5406,6 +5406,28 @@ WrappingResult WrapModuleContents (
}
}

List<string> GetBoundGenericsForAssociatedTypes (TypeDeclaration decl, ProtocolDeclaration proto, CSUsingPackages use)
{
var aliases = decl.TypeAliases;
var result = new List<string> ();
var protoName = proto.ToFullyQualifiedName ();
foreach (var assoc in proto.AssociatedTypes) {
var possibleTypeName = $"{protoName}.{assoc.Name}";
foreach (var alias in aliases) {
var namedTypeSpec = alias.TypeSpec as NamedTypeSpec;
if (namedTypeSpec == null)
throw new NotImplementedException ("You should never get an alias name that is not a NamedTypeSpec");
var typeName = namedTypeSpec.NameWithoutModule;
if (typeName == assoc.Name || typeName == possibleTypeName) {
var ntb = TypeMapper.MapType (decl, alias.TargetTypeSpec, false);
var csType = ntb.ToCSType (use);
result.Add (csType.ToString ());
}
}
}
return result;
}

void AddInheritedProtocols (TypeDeclaration decl, CSClass cl, ClassContents classContents, string trimmedLibPath,
CSUsingPackages use, ErrorHandling errors)
{
Expand All @@ -5423,8 +5445,19 @@ void AddInheritedProtocols (TypeDeclaration decl, CSClass cl, ClassContents clas
string itemName = null;
if (iface.Namespace == typeof (SwiftError).Namespace && iface.TypeName == typeof (SwiftError).Name)
itemName = "ISwiftError";
else
itemName = iface.TypeName;
else {
var entity = TypeMapper.GetEntityForTypeSpec (inh.InheritedTypeSpec);
if (entity == null || entity.Type is ProtocolDeclaration proto && proto.AssociatedTypes.Count == 0) {
itemName = iface.TypeName;
} else {
var boundTypes = GetBoundGenericsForAssociatedTypes (decl, entity.Type as ProtocolDeclaration, use);
var sb = new StringBuilder ();
sb.Append (iface.TypeName).Append ('<');
sb.Append (boundTypes.InterleaveCommas ());
sb.Append ('>');
itemName = sb.ToString ();
}
}
return new CSIdentifier (itemName);
}));

Expand Down
18 changes: 13 additions & 5 deletions SwiftReflector/SwiftInterfaceReflector/SwiftInterfaceReflector.cs
Original file line number Diff line number Diff line change
Expand Up @@ -994,18 +994,26 @@ XElement HandleGenerics (Generic_parameter_clauseContext genericContext, Generic

public override void ExitTypealias_declaration ([NotNull] Typealias_declarationContext context)
{
// only care about top level typealiases
if (currentElement.Peek ().Name != kModule)
return;
var name = UnTick (context.typealias_name ().GetText ());
var generics = context.generic_parameter_clause ()?.GetText () ?? "";
var targetType = context.typealias_assignment ().type ().GetText ();
var access = ToAccess (context.access_level_modifier ());
var map = new XElement (kTypeAlias, new XAttribute (kName, name + generics),
new XAttribute (kAccessibility, access),
new XAttribute (kType, targetType));
if (access != null)
typeAliasMap.Add (map);
if (access != null) {
if (currentElement.Peek ().Name == kModule) {
typeAliasMap.Add (map);
} else {
var curr = currentElement.Peek ();
var aliaslist = curr.Element (kTypeAliases);
if (aliaslist == null) {
aliaslist = new XElement (kTypeAliases);
curr.Add (aliaslist);
}
aliaslist.Add (map);
}
}
}

XElement MakeConformanceWhere (string name, string from)
Expand Down
12 changes: 12 additions & 0 deletions SwiftReflector/SwiftXmlReflection/TypeDeclaration.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public TypeDeclaration ()
InnerEnums = new List<EnumDeclaration> ();
Members = new List<Member> ();
Inheritance = new List<Inheritance> ();
TypeAliases = new List<TypeAliasDeclaration> ();
}

public TypeKind Kind { get; set; }
Expand All @@ -31,6 +32,7 @@ public TypeDeclaration ()
public List<ClassDeclaration> InnerClasses { get; set; }
public List<StructDeclaration> InnerStructs { get; set; }
public List<EnumDeclaration> InnerEnums { get; set; }
public List<TypeAliasDeclaration> TypeAliases { get; set; }
public bool IsObjC { get; set; }
public bool IsFinal { get; set; }
public bool IsDeprecated { get; set; }
Expand Down Expand Up @@ -167,6 +169,10 @@ protected virtual void GatherXObjects (List<XObject> xobjects)
xobjects.Add (new XElement ("members", memcontents.ToArray ()));
List<XObject> inherits = new List<XObject> (Inheritance.Select (i => i.ToXElement ()));
xobjects.Add (new XElement ("inherits", inherits.ToArray ()));
if (TypeAliases.Count > 0) {
var aliases = new List<XObject> (TypeAliases.Select (a => a.ToXElement ()));
xobjects.Add (new XElement ("typealiases", aliases.ToArray ()));
}
}

#endregion
Expand Down Expand Up @@ -205,6 +211,12 @@ public static TypeDeclaration TypeFromXElement (TypeAliasFolder folder, XElement
select SwiftReflector.SwiftXmlReflection.Inheritance.FromXElement (folder, inherit) as Inheritance;
decl.Inheritance.AddRange (inherits);
}
var typealiases = elem.Element ("typealiases");
if (typealiases != null) {
var aliases = from alias in typealiases.Elements ()
select TypeAliasDeclaration.FromXElement (module.Name, alias);
decl.TypeAliases.AddRange (aliases);
}
EnumDeclaration edecl = decl as EnumDeclaration;
if (edecl != null) {
var enumElements = (from enumElement in elem.Element ("elements").Elements ()
Expand Down
9 changes: 6 additions & 3 deletions SwiftRuntimeLibrary.Mac/SwiftRuntimeLibrary.Mac.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -259,9 +259,6 @@
<Compile Include="..\SwiftRuntimeLibrary\SwiftTypeRegistry.cs">
<Link>SwiftTypeRegistry.cs</Link>
</Compile>
<Compile Include="..\SwiftRuntimeLibrary\SwiftIteratorProtocol.cs">
<Link>SwiftIteratorProtocol.cs</Link>
</Compile>
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\SwiftProtocolConformanceDescriptor.cs">
<Link>SwiftMarshal\SwiftProtocolConformanceDescriptor.cs</Link>
</Compile>
Expand Down Expand Up @@ -289,6 +286,12 @@
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\SwiftThrowsAttribute.cs">
<Link>SwiftMarshal\SwiftThrowsAttribute.cs</Link>
</Compile>
<Compile Include="..\SwiftRuntimeLibrary\ISwiftIteratorProtocol.cs">
<Link>ISwiftIteratorProtocol.cs</Link>
</Compile>
<Compile Include="..\SwiftRuntimeLibrary\SwiftIteratorProtocolProxy.cs">
<Link>SwiftIteratorProtocolProxy.cs</Link>
</Compile>
</ItemGroup>
<ItemGroup>
<Folder Include="SwiftMarshal\" />
Expand Down
9 changes: 6 additions & 3 deletions SwiftRuntimeLibrary.iOS/SwiftRuntimeLibrary.iOS.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,6 @@
<Compile Include="..\SwiftRuntimeLibrary\SwiftTypeRegistry.cs">
<Link>SwiftTypeRegistry.cs</Link>
</Compile>
<Compile Include="..\SwiftRuntimeLibrary\SwiftIteratorProtocol.cs">
<Link>SwiftIteratorProtocol.cs</Link>
</Compile>
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\SwiftProtocolConformanceDescriptor.cs">
<Link>SwiftMarshal\SwiftProtocolConformanceDescriptor.cs</Link>
</Compile>
Expand Down Expand Up @@ -294,6 +291,12 @@
<Compile Include="..\SwiftRuntimeLibrary\SwiftMarshal\SwiftThrowsAttribute.cs">
<Link>SwiftMarshal\SwiftThrowsAttribute.cs</Link>
</Compile>
<Compile Include="..\SwiftRuntimeLibrary\ISwiftIteratorProtocol.cs">
<Link>ISwiftIteratorProtocol.cs</Link>
</Compile>
<Compile Include="..\SwiftRuntimeLibrary\SwiftIteratorProtocolProxy.cs">
<Link>SwiftIteratorProtocolProxy.cs</Link>
</Compile>
</ItemGroup>
<Target Name="GeneratedCSCode" BeforeTargets="CoreCompile" Inputs="$(MSBuildProjectFullPath)" Outputs="GeneratedCode\BindingMetadata.iOS.cs">
<Exec Command="make --directory=../SwiftRuntimeLibrary create-pinvokes-ios configuration=$(Configuration)" />
Expand Down
19 changes: 19 additions & 0 deletions SwiftRuntimeLibrary/ISwiftIteratorProtocol.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.

using System;
using System.Runtime.InteropServices;
using SwiftRuntimeLibrary;
using SwiftRuntimeLibrary.SwiftMarshal;
#if __IOS__ || __MACOS__ || __TVOS__ || __WATCHOS__
using ObjCRuntime;
#endif
using System.Reflection;

namespace SwiftRuntimeLibrary {
[SwiftProtocolType (typeof (SwiftIteratorProtocolProtocol<>), "libswiftCore.dylib", "$sStMp", true)]
[SwiftTypeName ("Swift.IteratorProtocol")]
public interface ISwiftIteratorProtocol<ATElement> {
SwiftOptional<ATElement> Next ();
}
}
126 changes: 0 additions & 126 deletions SwiftRuntimeLibrary/SwiftIteratorProtocol.cs

This file was deleted.

Loading