static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftExistentialMetaType st, TypeMapper typeMap)
        {
            if (st == null)
            {
                return(false);
            }
            if (st.Protocol.Protocols.Count != 1)
            {
                return(false);
            }
            var protoClass = st.Protocol.Protocols [0];

            if (ts.Name == "Any.Type")
            {
                return(protoClass.ClassName.ToFullyQualifiedName() == "Swift.Any");
            }
            if (ts.Name == protoClass.ClassName.ToFullyQualifiedName())
            {
                return(true);
            }
            if (ts.Name.EndsWith(".Type", StringComparison.Ordinal))
            {
                var maybeClassName = ts.Name.Substring(0, ts.Name.Length - ".Type".Length);
                return(maybeClassName == protoClass.ClassName.ToFullyQualifiedName());
            }
            return(false);
        }
        SLBaseExpr MarshalNamedTypeSpec(BaseDeclaration declContext, string name, NamedTypeSpec spec)
        {
            if (typeMapper.GetEntityForTypeSpec(spec) == null)
            {
                throw new NotImplementedException($"Unknown type {name}:{spec.ToString ()} in context {declContext.ToFullyQualifiedName (true)}");
            }
            bool isClass = NamedSpecIsClass(spec);

            if (isClass || spec.IsInOut)
            {
                imports.AddIfNotPresent("XamGlue");
                return(new SLFunctionCall("toIntPtr", false,
                                          new SLArgument(new SLIdentifier("value"), new SLIdentifier(name), true)));
            }

            if (TypeSpec.IsBuiltInValueType(spec))
            {
                return(new SLIdentifier(name));
            }

            // at this point, the value is either an enum or a struct, not passed by reference which we need to copy
            // into a local which we can then pass by reference.

            var bindingName = new SLIdentifier(MarshalEngine.Uniqueify(name, identifiersUsed));

            identifiersUsed.Add(bindingName.Name);
            var decl = new SLDeclaration(false, bindingName, typeMapper.TypeSpecMapper.MapType(declContext, imports, spec, false),
                                         new SLIdentifier(name), Visibility.None, false);
            var varBinding = new SLLine(decl);

            preMarshalCode.Add(varBinding);
            return(new SLAddressOf(bindingName, false));
        }
        static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftBuiltInType st, TypeMapper typeMap)
        {
            if (st == null)
            {
                return(false);
            }
            if (ts.IsInOut != st.IsReference)
            {
                return(false);
            }
            switch (st.BuiltInType)
            {
            case CoreBuiltInType.Bool:
                return(ts.Name == "Swift.Bool");

            case CoreBuiltInType.Double:
                return(ts.Name == "Swift.Double");

            case CoreBuiltInType.Float:
                return(ts.Name == "Swift.Float");

            case CoreBuiltInType.Int:
                return(ts.Name == "Swift.Int");

            case CoreBuiltInType.UInt:
                return(ts.Name == "Swift.UInt");

            default:
                throw new ArgumentOutOfRangeException("st");
            }
        }
        static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftType st, TypeMapper typeMap)
        {
            switch (st.Type)
            {
            case CoreCompoundType.Scalar:
                return(TypeMatches(decl, ts, st as SwiftBuiltInType, typeMap));

            case CoreCompoundType.Class:
                return(TypeMatches(decl, ts, st as SwiftClassType, typeMap));

            case CoreCompoundType.MetaClass:
                if (st is SwiftExistentialMetaType exist)
                {
                    return(TypeMatches(decl, ts, exist, typeMap));
                }
                else
                {
                    return(TypeMatches(decl, ts, st as SwiftMetaClassType, typeMap));
                }

            case CoreCompoundType.BoundGeneric:
                return(TypeMatches(decl, ts, st as SwiftBoundGenericType, typeMap));

            case CoreCompoundType.ProtocolList:
                return(TypeMatches(decl, ts, st as SwiftProtocolListType, typeMap));

            case CoreCompoundType.GenericReference:
                return(TypeMatches(decl, ts, st as SwiftGenericArgReferenceType, typeMap));

            case CoreCompoundType.Struct:
            default:
                return(false);
            }
        }
        SLType MapType(BaseDeclaration declContext, SLImportModules modules, NamedTypeSpec spec)
        {
            SLType retval = null;

            if (spec.HasModule)
            {
                modules.AddIfNotPresent(spec.Module);
            }
            if (declContext.IsTypeSpecGeneric(spec) && !spec.ContainsGenericParameters)
            {
                Tuple <int, int> depthIndex = declContext.GetGenericDepthAndIndex(spec);
                retval = new SLGenericReferenceType(depthIndex.Item1, depthIndex.Item2);
            }
            else if (spec.ContainsGenericParameters)
            {
                retval = new SLBoundGenericType(spec.NameWithoutModule, spec.GenericParameters.Select(p => MapType(declContext, modules, p, false)));
            }
            else
            {
                retval = new SLSimpleType(spec.Name.NameWithoutModule());
            }

            if (spec.InnerType == null)
            {
                return(retval);
            }
            else
            {
                return(new SLCompoundType(retval, MapType(declContext, modules, spec.InnerType)));
            }
        }
        TypeSpec RebuildTypeWithGenericType(NamedTypeSpec type, out bool changed)
        {
            bool   nameChanged = false, genArgsChanged = false;
            string newName   = null;
            var    assocType = GetAssociatedTypeNamed(type.Name);

            if (assocType != null)
            {
                nameChanged = true;
                newName     = GenericTypeNameFor(assocType.Name);
            }
            TypeSpec [] newGenParms = type.GenericParameters.ToArray();
            if (type.GenericParameters != null)
            {
                newGenParms = new TypeSpec [type.GenericParameters.Count];
                for (int i = 0; i < newGenParms.Length; i++)
                {
                    bool genChanged;
                    newGenParms [i] = (RebuildTypeWithGenericType(type.GenericParameters [i], out genChanged));
                    genArgsChanged  = genArgsChanged || genChanged;
                }
            }
            changed = nameChanged || genArgsChanged;
            if (changed)
            {
                return(new NamedTypeSpec(newName, newGenParms));
            }
            return(type);
        }
        public void TestSimpleType()
        {
            NamedTypeSpec ns = TypeSpecParser.Parse("Swift.Int") as NamedTypeSpec;

            Assert.IsNotNull(ns);
            Assert.AreEqual("Swift.Int", ns.Name);
        }
 static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftMetaClassType st, TypeMapper typeMap)
 {
     if (st == null)
     {
         return(false);
     }
     return(ts.Name == st.Class.ClassName.ToFullyQualifiedName(true));
 }
        public void TestSingleTuple()
        {
            // single tuples get folded into their type
            NamedTypeSpec ns = TypeSpecParser.Parse("(Swift.Int)") as NamedTypeSpec;

            Assert.NotNull(ns);
            Assert.AreEqual("Swift.Int", ns.Name);
        }
        public void TestEmbeddedClass()
        {
            NamedTypeSpec ns = TypeSpecParser.Parse("Swift.Dictionary<Swift.String, T>.Index") as NamedTypeSpec;

            Assert.IsNotNull(ns);
            Assert.IsNotNull(ns.InnerType);
            Assert.AreEqual("Index", ns.InnerType.Name);
            Assert.AreEqual("Swift.Dictionary<Swift.String, T>.Index", ns.ToString());
        }
 static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftClassType ct, TypeMapper typeMap)
 {
     if (ct == null)
     {
         return(false);
     }
     return(ts.Name == ct.ClassName.ToFullyQualifiedName(true) ||
            ts.NameWithoutModule == ct.ClassName.ToFullyQualifiedName(false));
 }
Exemple #12
0
        static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftGenericArgReferenceType genArg)
        {
            if (!decl.IsTypeSpecGeneric(ts))
            {
                return(false);
            }
            var depthAndIndex = decl.GetGenericDepthAndIndex(ts.Name);

            return(genArg.Depth == depthAndIndex.Item1 && genArg.Index == depthAndIndex.Item2);
        }
        public void TestProtocolListAlphabetical1()
        {
            var specs = new NamedTypeSpec [] {
                new NamedTypeSpec("🤡Foo"),
                new NamedTypeSpec("💩Foo"),
            };

            var protos = new ProtocolListTypeSpec(specs);

            Assert.AreEqual("💩Foo & 🤡Foo", protos.ToString(), "ToString mismatch");
        }
 static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftBoundGenericType st, TypeMapper typeMap)
 {
     if (st == null)
     {
         return(false);
     }
     if (!ts.ContainsGenericParameters)
     {
         return(false);
     }
     return(TypeMatches(decl, ts.GenericParameters, st.BoundTypes, typeMap));
 }
        public void TestArrayOfInt()
        {
            NamedTypeSpec ns = TypeSpecParser.Parse("Swift.Array<Swift.Int>") as NamedTypeSpec;

            Assert.NotNull(ns);
            Assert.AreEqual("Swift.Array", ns.Name);
            Assert.IsTrue(ns.ContainsGenericParameters);
            Assert.AreEqual(1, ns.GenericParameters.Count);
            ns = ns.GenericParameters [0] as NamedTypeSpec;
            Assert.NotNull(ns);
            Assert.AreEqual("Swift.Int", ns.Name);
        }
        public void TestFuncIntInt()
        {
            ClosureTypeSpec close = TypeSpecParser.Parse("Swift.Int -> Swift.Int") as ClosureTypeSpec;

            Assert.NotNull(close);
            NamedTypeSpec ns = close.Arguments as NamedTypeSpec;

            Assert.NotNull(ns);
            Assert.AreEqual("Swift.Int", ns.Name);
            ns = close.ReturnType as NamedTypeSpec;
            Assert.NotNull(ns);
            Assert.AreEqual("Swift.Int", ns.Name);
        }
        public void TestProtocolListAlphabetical()
        {
            var specs = new NamedTypeSpec [] {
                new NamedTypeSpec("Cfoo"),
                new NamedTypeSpec("Afoo"),
                new NamedTypeSpec("Dfoo"),
                new NamedTypeSpec("Bfoo")
            };

            var protos = new ProtocolListTypeSpec(specs);

            Assert.AreEqual("Afoo & Bfoo & Cfoo & Dfoo", protos.ToString(), "ToString mismatch");
        }
        public void TestGeneric()
        {
            NamedTypeSpec ns = TypeSpecParser.Parse("Swift.UnsafeMutablePointer<(Swift.Int, Error, Swift.Bool)>") as NamedTypeSpec;

            Assert.NotNull(ns);
            Assert.AreEqual("Swift.UnsafeMutablePointer", ns.Name);
            Assert.IsTrue(ns.ContainsGenericParameters);
            Assert.AreEqual(1, ns.GenericParameters.Count);
            var ts = ns.GenericParameters [0] as TupleTypeSpec;

            Assert.NotNull(ts);
            Assert.AreEqual(3, ts.Elements.Count);
        }
        public void TestDoubleTuple()
        {
            TupleTypeSpec tuple = TypeSpecParser.Parse("(Swift.Int, Swift.Float)") as TupleTypeSpec;

            Assert.IsNotNull(tuple);
            Assert.AreEqual(2, tuple.Elements.Count);
            NamedTypeSpec ns = tuple.Elements [0] as NamedTypeSpec;

            Assert.NotNull(ns);
            Assert.AreEqual("Swift.Int", ns.Name);
            ns = tuple.Elements [1] as NamedTypeSpec;
            Assert.NotNull(ns);
            Assert.AreEqual("Swift.Float", ns.Name);
        }
        public void TestDictionaryOfIntString()
        {
            NamedTypeSpec ns = TypeSpecParser.Parse("Swift.Dictionary<Swift.Int, Swift.String>") as NamedTypeSpec;

            Assert.NotNull(ns);
            Assert.AreEqual("Swift.Dictionary", ns.Name);
            Assert.IsTrue(ns.ContainsGenericParameters);
            Assert.AreEqual(2, ns.GenericParameters.Count);
            NamedTypeSpec ns1 = ns.GenericParameters [0] as NamedTypeSpec;

            Assert.NotNull(ns1);
            Assert.AreEqual("Swift.Int", ns1.Name);
            ns1 = ns.GenericParameters [1] as NamedTypeSpec;
            Assert.NotNull(ns1);
            Assert.AreEqual("Swift.String", ns1.Name);
        }
        void GetUniqueGenericTypeNamesFor(NamedTypeSpec candidate, HashSet <string> result)
        {
            var assocType = GetAssociatedTypeNamed(candidate.Name);

            if (assocType != null)
            {
                result.Add(GenericTypeNameFor(assocType.Name));
            }
            if (candidate.GenericParameters == null)
            {
                return;
            }
            foreach (var gen in candidate.GenericParameters)
            {
                GetUniqueGenericTypeNamesFor(gen, result);
            }
        }
Exemple #22
0
        void TestFuncReturning(string declaredType, string value, string expectedType)
        {
            string            code   = String.Format("public func foo() -> {0} {{ return {1} }}", declaredType, value);
            ModuleDeclaration module = ReflectToModules(code, "SomeModule").Find(m => m.Name == "SomeModule");

            Assert.IsNotNull(module);
            Assert.AreEqual(1, module.Functions.Count());
            FunctionDeclaration func = module.Functions.First();

            Assert.IsNotNull(func);
            Assert.AreEqual(func.Name, "foo");
            Assert.AreEqual(expectedType, func.ReturnTypeName);
            Assert.AreEqual(1, func.ParameterLists.Count);
            Assert.AreEqual(0, func.ParameterLists [0].Count);
            NamedTypeSpec ns = func.ReturnTypeSpec as NamedTypeSpec;

            Assert.NotNull(ns);
            Assert.AreEqual(expectedType, ns.Name);
        }
Exemple #23
0
        SLType MapType(BaseDeclaration declContext, SLImportModules modules, NamedTypeSpec spec)
        {
            SLType retval = null;

            if (spec.HasModule(declContext, this.parent))
            {
                modules.AddIfNotPresent(spec.Module);
            }
            if (declContext.IsTypeSpecGeneric(spec) && !spec.ContainsGenericParameters)
            {
                Tuple <int, int> depthIndex = declContext.GetGenericDepthAndIndex(spec);
                retval = new SLGenericReferenceType(depthIndex.Item1, depthIndex.Item2);
            }
            else if (spec.ContainsGenericParameters)
            {
                retval = new SLBoundGenericType(spec.NameWithoutModule, spec.GenericParameters.Select(p => MapType(declContext, modules, p, false)));
            }
            else
            {
                if (declContext.IsProtocolWithAssociatedTypesFullPath(spec, parent))
                {
                    // for T.AssocType
                    var genPart    = spec.Module;
                    var depthIndex = declContext.GetGenericDepthAndIndex(genPart);
                    var newGenPart = new SLGenericReferenceType(depthIndex.Item1, depthIndex.Item2);
                    retval = new SLSimpleType($"{newGenPart}.{spec.NameWithoutModule}");
                }
                else
                {
                    retval = new SLSimpleType(spec.NameWithoutModule);
                }
            }

            if (spec.InnerType == null)
            {
                return(retval);
            }
            else
            {
                return(new SLCompoundType(retval, MapType(declContext, modules, spec.InnerType)));
            }
        }
        public void TestProtocolListNotMatch()
        {
            var specs1 = new NamedTypeSpec [] {
                new NamedTypeSpec("Cfoo"),
                new NamedTypeSpec("Afoo"),
                new NamedTypeSpec("Dfoo"),
                new NamedTypeSpec("Bfoo")
            };

            var specs2 = new NamedTypeSpec [] {
                new NamedTypeSpec("Afoo"),
                new NamedTypeSpec("Efoo"),
                new NamedTypeSpec("Cfoo"),
                new NamedTypeSpec("Bfoo")
            };

            var protos1 = new ProtocolListTypeSpec(specs1);
            var protos2 = new ProtocolListTypeSpec(specs2);

            Assert.IsFalse(protos1.Equals(protos2), "lists match?!");
        }
 static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftGenericArgReferenceType genArg, TypeMapper typeMap)
 {
     if (genArg.HasAssociatedTypePath)
     {
         if (!decl.IsProtocolWithAssociatedTypesFullPath(ts, typeMap))
         {
             return(false);
         }
         var parts = ts.Name.Split('.');
         // parts will have the generic part at 0, genArg will not
         if (parts.Length != genArg.AssociatedTypePath.Count + 1)
         {
             return(false);
         }
         var depthAndIndex = decl.GetGenericDepthAndIndex(parts [0]);
         if (genArg.Depth != depthAndIndex.Item1 || genArg.Index != depthAndIndex.Item2)
         {
             return(false);
         }
         for (int i = 0; i < genArg.AssociatedTypePath.Count; i++)
         {
             if (genArg.AssociatedTypePath [i] != parts [i + 1])
             {
                 return(false);
             }
         }
         return(true);
     }
     else
     {
         if (!decl.IsTypeSpecGeneric(ts))
         {
             return(false);
         }
         var depthAndIndex = decl.GetGenericDepthAndIndex(ts.Name);
         return(genArg.Depth == depthAndIndex.Item1 && genArg.Index == depthAndIndex.Item2);
     }
 }
        SLBaseExpr MarshalGenericTypeSpec(BaseDeclaration declContext, string name, NamedTypeSpec spec, int depth, int index)
        {
            // given Foo(T x)
            // the vtable entry should be something like
            // foo : ((@convention(c)(UnsafeRawPointer)->())
            //
            // UnsafeMutablePointer<T> xPtr = UnsafeMutablePointer<T>.alloc(1);
            // pointerToX.initialize(x)
            // vtable.foo(toIntPtr(pointerToX))
            // pointerToX.deinitialize(1)
            // pointerToX.deallocate()
            imports.AddIfNotPresent("XamGlue");
            var xPtr = new SLIdentifier(MarshalEngine.Uniqueify(name + "Ptr", identifiersUsed));

            identifiersUsed.Add(xPtr.Name);
            var xPtrDecl = new SLDeclaration(true, xPtr, null,
                                             new SLFunctionCall(String.Format("UnsafeMutablePointer<{0}>.allocate", SLGenericReferenceType.DefaultNamer(depth, index)),
                                                                false, new SLArgument(new SLIdentifier("capacity"), SLConstant.Val(1), true)),
                                             Visibility.None);
            var xPtrBinding = new SLLine(xPtrDecl);

            preMarshalCode.Add(xPtrBinding);
            var xPtrInit = SLFunctionCall.FunctionCallLine(xPtr.Name + ".initialize",
                                                           new SLArgument(new SLIdentifier("to"), new SLIdentifier(name), true));

            preMarshalCode.Add(xPtrInit);

            var xPtrDeinit = SLFunctionCall.FunctionCallLine(xPtr.Name + ".deinitialize",
                                                             new SLArgument(new SLIdentifier("count"), SLConstant.Val(1), true));

            var xPtrDalloc = SLFunctionCall.FunctionCallLine(xPtr.Name + ".deallocate");

            postMarshalCode.Add(xPtrDeinit);
            postMarshalCode.Add(xPtrDalloc);

            return(new SLFunctionCall("toIntPtr", false, new SLArgument(new SLIdentifier("value"), xPtr, true)));
        }
        bool NamedTypesMatch(FunctionDeclaration protoFunc, NamedTypeSpec protoType, FunctionDeclaration classFunc, NamedTypeSpec classType)
        {
            if (!GenericParametersMatch(protoFunc, protoType.GenericParameters, classFunc, classType.GenericParameters))
            {
                return(false);
            }

            var assoc = protocol.AssociatedTypeDeclarationFromNamedTypeSpec(protoType);

            if (assoc != null)
            {
                if (!wrapperClass.IsTypeSpecGenericReference(classType))
                {
                    return(false);
                }
                var depthIndex = wrapperClass.GetGenericDepthAndIndex(classType);
                var assocIndex = protocol.AssociatedTypes.IndexOf(assoc);
                return(assocIndex == depthIndex.Item2);
            }
            else
            {
                return(protoType.Name == classType.Name);
            }
        }
        static bool TypeMatches(FunctionDeclaration decl, NamedTypeSpec ts, SwiftProtocolListType protList, TypeMapper typeMap)
        {
            if (protList == null)
            {
                return(false);
            }
            if (protList.Protocols.Count == 1 && !ts.IsProtocolList)
            {
                return(TypeMatches(decl, ts, protList.Protocols [0], typeMap));
            }

            if (protList.Protocols.Count != ts.GenericParameters.Count || !ts.IsProtocolList)
            {
                return(false);
            }
            for (int i = 0; i < ts.GenericParameters.Count; i++)
            {
                if (!TypeMatches(decl, ts.GenericParameters [i], protList.Protocols [i], typeMap))
                {
                    return(false);
                }
            }
            return(true);
        }
 bool NamedSpecIsClass(NamedTypeSpec spec)
 {
     return(spec != null && typeMapper.GetEntityTypeForSwiftClassName(spec.Name) == EntityType.Class);
 }