Esempio n. 1
0
        public void TestForFinality()
        {
            string simpleClass = "public class Garble { public final func finalmethod() { }; public func virtmethod() { } }";

            using (DisposableTempFile montyLib = new DisposableTempFile("libXython.dylib", false)) {
                Compiler.CompileStringToFileUsing(null, XCodeCompiler.SwiftcCustom, simpleClass, " -emit-library -module-name Xython", montyLib);
                var             errors    = new ErrorHandling();
                ModuleInventory inventory = ModuleInventory.FromFile(montyLib.Filename, errors);
                Utils.CheckErrors(errors);
                ClassContents cl = inventory.ClassesForName(new SwiftName("Xython", false)).FirstOrDefault();
                Assert.IsNotNull(cl);

                if (cl.WitnessTable == null || cl.WitnessTable.MangledNames == null ||
                    cl.WitnessTable.MangledNames.Count() == 0)
                {
                    return;
                }

                foreach (var oi in cl.Methods.Values)
                {
                    foreach (TLFunction f in oi.Functions)
                    {
                        if (f.MangledName.Contains("finalmethod"))
                        {
                            Assert.IsTrue(cl.IsFinal(f));
                        }
                        else if (f.MangledName.Contains("virtmethod"))
                        {
                            Assert.IsFalse(cl.IsFinal(f));
                        }
                    }
                }
            }
        }
        void CanFindThing(string code, Func <ClassDeclaration, bool> classFinder,
                          Func <FunctionDeclaration, bool> funcFinder,
                          Func <TLFunction, bool> tlVerifier)
        {
            CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind");

            var               errors = new ErrorHandling();
            ModuleInventory   mi     = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors);
            ModuleDeclaration mod    = compiler.ReflectToModules(new string [] { compiler.DirectoryPath },
                                                                 new string [] { compiler.DirectoryPath }, null, "CanFind") [0];

            ClassDeclaration classDecl = mod.AllClasses.FirstOrDefault(classFinder);

            Assert.IsNotNull(classDecl, "nominal type not found");

            FunctionDeclaration funcDecl = classDecl.AllMethodsNoCDTor().FirstOrDefault(funcFinder);

            Assert.IsNotNull(funcDecl, "func decl not found");

            // see the note in the implementation of CanFindThing above
            TLFunction func = XmlToTLFunctionMapper.ToTLFunction(funcDecl, mi, null);

            Assert.IsNotNull(func, "TLFunction not found");
            Assert.IsTrue(tlVerifier(func), "verifier failed");
        }
Esempio n. 3
0
        void FindsProperty(string code, Func <ClassDeclaration, bool> classFinder,
                           Func <PropertyDeclaration, bool> propFinder)
        {
            CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind");

            var               errors = new ErrorHandling();
            ModuleInventory   mi     = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors);
            ModuleDeclaration mod    = compiler.ReflectToModules(new string [] { compiler.DirectoryPath },
                                                                 new string [] { compiler.DirectoryPath }, null, "CanFind") [0];

            ClassDeclaration classDecl = mod.AllClasses.FirstOrDefault(classFinder);

            Assert.IsNotNull(classDecl, "null class");

            PropertyDeclaration propDecl = classDecl.Members.OfType <PropertyDeclaration> ().FirstOrDefault(propFinder);

            Assert.IsNotNull(propDecl, "null property");

            FunctionDeclaration getter = propDecl.GetGetter();

            Assert.IsNotNull(getter, "null getter");

            FunctionDeclaration setter = propDecl.GetSetter();

            Assert.IsNotNull(setter, "null setter");

            TLFunction tlgetter = XmlToTLFunctionMapper.ToTLFunction(getter, mi);

            Assert.IsNotNull(tlgetter, "null tlgetter");

            TLFunction tlsetter = XmlToTLFunctionMapper.ToTLFunction(setter, mi);

            Assert.IsNotNull(tlsetter, "null tlsetter");
        }
Esempio n. 4
0
        public void ArcClassStruct()
        {
            string swiftCode =
                "public final class Foo {\npublic var _nm:String\npublic init(name:String) {\n_nm = name }\n" +
                "deinit {\nprint(_nm)\n}\n}\n" +
                "public struct Bar {\n public var a:Foo\n public init(f:Foo) {\n a = f\n}\n }\n"
            ;

            swiftCode += TestRunning.CreateSwiftConsoleRedirect();

            using (TempDirectoryFilenameProvider provider = new TempDirectoryFilenameProvider(null, true)) {
                Utils.CompileSwift(swiftCode, provider);

                string libFileName = Path.Combine(provider.DirectoryPath, "libXython.dylib");
                var    errors      = new ErrorHandling();

                ModuleInventory.FromFile(libFileName, errors);
                Utils.CheckErrors(errors);

                Utils.CompileToCSharp(provider);

                CSUsingPackages use = new CSUsingPackages("System", "System.Runtime.InteropServices", "SwiftRuntimeLibrary");

                CSNamespace ns     = new CSNamespace("Xython");
                CSFile      csfile = CSFile.Create(use, ns);

                CSIdentifier inst  = new CSIdentifier("inst");
                CSLine       newer = CSVariableDeclaration.VarLine((CSSimpleType)"Foo", inst, CSFunctionCall.Ctor("Foo",
                                                                                                                  CSFunctionCall.Function("SwiftString.FromString", CSConstant.Val("nothing"))));

                CSIdentifier inst1  = new CSIdentifier("bar");
                CSLine       newer1 = CSVariableDeclaration.VarLine((CSSimpleType)"Bar", inst1, CSFunctionCall.Ctor("Bar", inst));

                CSLine disposer  = CSFunctionCall.FunctionCallLine(inst.Name + ".Dispose", false);
                CSLine disposer1 = CSFunctionCall.FunctionCallLine(inst1.Name + ".Dispose", false);

                CSCodeBlock mainBody = CSCodeBlock.Create(newer, newer1, disposer, disposer1);

                CSMethod main = new CSMethod(CSVisibility.Public, CSMethodKind.Static, CSSimpleType.Void,
                                             (CSIdentifier)"Main", new CSParameterList(new CSParameter(CSSimpleType.CreateArray("string"), "args")),
                                             mainBody);
                CSClass mainClass = new CSClass(CSVisibility.Public, "NameNotImportant", new CSMethod [] { main });
                ns.Block.Add(mainClass);

                string csOutFilename  = provider.ProvideFileFor(provider.UniqueName(null, "CSWrap", "cs"));
                var    exeOutFilename = provider.UniquePath(null, "CSWrap", "exe");

                CodeWriter.WriteToFile(csOutFilename, csfile);

                Compiler.CSCompile(provider.DirectoryPath, Directory.GetFiles(provider.DirectoryPath, "*.cs"), exeOutFilename);

                TestRunning.CopyTestReferencesTo(provider.DirectoryPath);

                string output = Compiler.RunWithMono(exeOutFilename, provider.DirectoryPath);
                Assert.AreEqual("nothing\n", output);
            }
        }
        public static ProtocolContents LocateProtocolContents(ModuleInventory modInventory, SwiftClassName className)
        {
            var contents = modInventory.Values.FirstOrDefault(mod => mod.Name.Equals(className.Module));

            if (contents == null)
            {
                return(null);
            }
            return(LocateProtocolContents(contents, className));
        }
        public static TLFunction ToTLFunction(FunctionDeclaration decl, ModuleInventory mi, TypeMapper typeMap)
        {
            var scn      = ToSwiftClassName(decl);
            var contents = mi.Values.FirstOrDefault(mc => mc.Name.Equals(scn.Module));

            if (contents == null)
            {
                return(null);
            }
            return(ToTLFunction(decl, contents, typeMap));
        }
        static void PropertyTestCore(string simpleClass)
        {
            using (TempDirectoryFilenameProvider provider = new TempDirectoryFilenameProvider()) {
                Utils.CompileSwift(simpleClass, provider);

                string          libFileName = Path.Combine(provider.DirectoryPath, "libXython.dylib");
                var             errors      = new ErrorHandling();
                ModuleInventory inventory   = ModuleInventory.FromFile(libFileName, errors);
                Utils.CheckErrors(errors);

                Utils.CompileToCSharp(provider);
            }
        }
Esempio n. 8
0
        public void SimpleConstructor()
        {
            string swiftcode = "public class None { public init() { } }";

            using (Stream stm = Compiler.CompileStringUsing(null, XCodeCompiler.SwiftcCustom, swiftcode, "")) {
                var             errors    = new ErrorHandling();
                ModuleInventory inventory = ModuleInventory.FromStream(stm, errors);
                Utils.CheckErrors(errors);
                Assert.AreEqual(1, inventory.Classes.Count());
                ClassContents cl = inventory.Classes.First();
                Assert.AreEqual("noname.None", cl.Name.ToFullyQualifiedName());
                Assert.AreEqual(2, cl.Constructors.Values.Count());
            }
        }
Esempio n. 9
0
        Dictionary <string, List <string> > Wrap(ModuleDeclaration module,
                                                 ModuleInventory modInventory, TempDirectorySwiftClassFileProvider fileProvider,
                                                 TypeMapper typeMapper, string wrappingModuleName, out HashSet <string> referencedModules,
                                                 ErrorHandling errors)
        {
            var wrapper = new MethodWrapping(fileProvider, typeMapper, wrappingModuleName, errors);

            referencedModules        = wrapper.WrapModule(module, modInventory);
            FunctionReferenceCodeMap = wrapper.FunctionReferenceCodeMap;

            Dictionary <string, List <string> > wrappedResults = null;

            if (!wrapper.TryGetClassesForModule(module.Name, out wrappedResults))
            {
                return(new Dictionary <string, List <string> > ());
            }
            return(wrappedResults);
        }
Esempio n. 10
0
        void CanFindThing(string code, Func <FunctionDeclaration, bool> funcFinder,
                          Func <TLFunction, bool> tlVerifier)
        {
            CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind");

            var               errors = new ErrorHandling();
            ModuleInventory   mi     = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors);
            ModuleDeclaration mod    = compiler.ReflectToModules(new string [] { compiler.DirectoryPath },
                                                                 new string [] { compiler.DirectoryPath }, null, "CanFind") [0];

            FunctionDeclaration funcDecl = mod.Functions.FirstOrDefault(funcFinder);

            Assert.IsNotNull(funcDecl, "no function found");

            TLFunction func = XmlToTLFunctionMapper.ToTLFunction(funcDecl, mi);

            Assert.IsNotNull(func, $"failed to find TLFunction for {funcDecl.Name}");
            Assert.IsTrue(tlVerifier(func), "verifier failed");
        }
        void CanFindThing(string code, Func <FunctionDeclaration, bool> funcFinder,
                          Func <TLFunction, bool> tlVerifier)
        {
            CustomSwiftCompiler compiler = Utils.CompileSwift(code, moduleName: "CanFind");

            var               errors = new ErrorHandling();
            ModuleInventory   mi     = ModuleInventory.FromFile(Path.Combine(compiler.DirectoryPath, "libCanFind.dylib"), errors);
            ModuleDeclaration mod    = compiler.ReflectToModules(new string [] { compiler.DirectoryPath },
                                                                 new string [] { compiler.DirectoryPath }, null, "CanFind") [0];

            FunctionDeclaration funcDecl = mod.Functions.FirstOrDefault(funcFinder);

            Assert.IsNotNull(funcDecl, "no function found");

            // note: if you get an NRE from here then you are testing an argument that includes
            // an associated type path from a generic. Don't do that. You need much more infrastructure to
            // do that than you really want here.
            TLFunction func = XmlToTLFunctionMapper.ToTLFunction(funcDecl, mi, null);

            Assert.IsNotNull(func, $"failed to find TLFunction for {funcDecl.Name}");
            Assert.IsTrue(tlVerifier(func), "verifier failed");
        }
Esempio n. 12
0
        public Tuple <string, HashSet <string> > CompileWrappers(string [] inputLibraryDirectories, string [] inputModuleDirectories,
                                                                 IEnumerable <ModuleDeclaration> modulesToCompile, ModuleInventory modInventory,
                                                                 List <string> targets, string wrappingModuleName, bool outputIsFramework)
        {
            wrappingModuleName = wrappingModuleName ?? kXamWrapPrefix;

            string outputLibraryName = BuildLibraryName(wrappingModuleName, outputIsFramework);

            string outputLibraryPath = Path.Combine(outputDirectory, outputLibraryName);

            string outputFrameworkPath    = Path.Combine(outputDirectory, Path.GetFileName(outputLibraryName) + ".framework");
            string outputFrameworkLibPath = Path.Combine(outputFrameworkPath, outputLibraryName);


            if (File.Exists(outputLibraryPath))
            {
                File.Delete(outputLibraryPath);
            }

            using (TempDirectorySwiftClassFileProvider fileProvider =
                       new TempDirectorySwiftClassFileProvider(Ex.ThrowOnNull(wrappingModuleName, "wrappingModuleName"), true)) {
                var allReferencedModules = new HashSet <string> ();
                foreach (ModuleDeclaration module in modulesToCompile)
                {
                    HashSet <string> referencedModules = null;
                    var wrappedClasses = Wrap(module, modInventory, fileProvider, typeMapper, wrappingModuleName, out referencedModules, errors);
                    this.wrappers.Add(module.Name, wrappedClasses);
                    allReferencedModules.Merge(referencedModules);
                }
                var inModuleNamesList = modulesToCompile.Select(mod => mod.Name).ToList();
                inModuleNamesList.Add("XamGlue");


                if (fileProvider.IsEmpty)
                {
                    return(new Tuple <string, HashSet <string> > (null, null));
                }

                var targetOutDirs = new List <string> ();
                for (int i = 0; i < targets.Count; i++)
                {
                    // each file goes to a unique output directory.
                    // first compile into the fileProvider, then move to
                    // fileProvider/tar-get-arch
                    string targetoutdir = Path.Combine(fileProvider.DirectoryPath, targets [i]);
                    targetOutDirs.Add(targetoutdir);
                    Directory.CreateDirectory(targetoutdir);

                    var locations = SwiftModuleFinder.GatherAllReferencedModules(allReferencedModules,
                                                                                 inputModuleDirectories, targets [i]);
                    try {
                        string [] inputModDirs = locations.Select(loc => loc.DirectoryPath).ToArray();
                        CompileAllFiles(fileProvider, wrappingModuleName, outputLibraryName, outputLibraryPath,
                                        inputModDirs, inputLibraryDirectories, inModuleNamesList.ToArray(),
                                        targets [i], outputIsFramework);
                    } catch (Exception e) {
                        throw ErrorHelper.CreateError(ReflectorError.kCantHappenBase + 66, e, $"Failed to compile the generated swift wrapper code.");
                    } finally {
                        locations.DisposeAll();
                    }

                    // move to arch directory
                    File.Copy(Path.Combine(fileProvider.DirectoryPath, outputLibraryName),
                              Path.Combine(targetoutdir, outputLibraryName), true);
                    File.Delete(Path.Combine(fileProvider.DirectoryPath, outputLibraryName));

                    File.Copy(Path.Combine(fileProvider.DirectoryPath, wrappingModuleName + ".swiftmodule"),
                              Path.Combine(targetoutdir, wrappingModuleName + ".swiftmodule"));
                    File.Delete(Path.Combine(fileProvider.DirectoryPath, wrappingModuleName + ".swiftmodule"));

                    File.Copy(Path.Combine(fileProvider.DirectoryPath, wrappingModuleName + ".swiftdoc"),
                              Path.Combine(targetoutdir, wrappingModuleName + ".swiftdoc"));
                    File.Delete(Path.Combine(fileProvider.DirectoryPath, wrappingModuleName + ".swiftdoc"));
                }
                if (targets.Count > 1)
                {
                    // lipo all the outputs back into the fileProvider
                    Lipo(targetOutDirs, fileProvider.DirectoryPath, outputLibraryName);
                    File.Copy(Path.Combine(fileProvider.DirectoryPath, outputLibraryName),
                              outputLibraryPath, true);
                    if (!IsMacOSLib(outputLibraryPath))
                    {
                        if (!Directory.Exists(outputFrameworkPath))
                        {
                            Directory.CreateDirectory(outputFrameworkPath);
                        }
                        File.Copy(outputLibraryPath, outputFrameworkLibPath, true);
                        InfoPList.MakeInfoPList(outputFrameworkLibPath, Path.Combine(outputFrameworkPath, "Info.plist"));
                    }
                }
                else
                {
                    File.Copy(Path.Combine(targetOutDirs [0], outputLibraryName),
                              outputLibraryPath, true);
                    if (!IsMacOSLib(outputLibraryPath))
                    {
                        if (!Directory.Exists(outputFrameworkPath))
                        {
                            Directory.CreateDirectory(outputFrameworkPath);
                        }
                        File.Copy(outputLibraryPath, outputFrameworkLibPath, true);
                        InfoPList.MakeInfoPList(outputFrameworkLibPath, Path.Combine(outputFrameworkPath, "Info.plist"));
                    }
                }
                for (int i = 0; i < targets.Count; i++)
                {
                    string arch      = targets [i].ClangTargetCpu();
                    string targetDir = Path.Combine(outputDirectory, arch);
                    Directory.CreateDirectory(targetDir);

                    File.Copy(Path.Combine(targetOutDirs [i], wrappingModuleName + ".swiftmodule"),
                              Path.Combine(targetDir, wrappingModuleName + ".swiftmodule"), true);
                    File.Copy(Path.Combine(targetOutDirs [i], wrappingModuleName + ".swiftdoc"),
                              Path.Combine(targetDir, wrappingModuleName + ".swiftdoc"), true);
                }
                foreach (string dirname in targetOutDirs)
                {
                    Directory.Delete(dirname, true);
                }
                if (retainSwiftFiles)
                {
                    CopySwiftFiles(fileProvider, Path.Combine(outputDirectory, wrappingModuleName + "Source"));
                }
                return(new Tuple <string, HashSet <string> > (outputLibraryPath, allReferencedModules));
            }
        }