public static IImmutableSet<Type> AggregateProvidableTypes(this IEnumerable<ITypeProvider> providers)
        {
            IImmutableSet<Type> currentTypes = new HashSet<Type>().ToImmutableHashSet();

            if (providers == null)
                return currentTypes;

            foreach (var provider in providers)
            {
                currentTypes = currentTypes.Merge(provider.ProvidableTypes);
            }

            return currentTypes;
        }
Beispiel #2
0
        public static IImmutableSet <Type> AggregateProvidableTypes(this IEnumerable <ITypeProvider> providers)
        {
            IImmutableSet <Type> currentTypes = new HashSet <Type>().ToImmutableHashSet();

            if (providers == null)
            {
                return(currentTypes);
            }

            foreach (var provider in providers)
            {
                currentTypes = currentTypes.Merge(provider.ProvidableTypes);
            }

            return(currentTypes);
        }
		private System.Type CreateUncachedProxyType(System.Type baseType, System.Type[] baseInterfaces)
		{
			AppDomain currentDomain = AppDomain.CurrentDomain;
			string typeName = string.Format("{0}Proxy", baseType.Name);
			string assemblyName = string.Format("{0}Assembly", typeName);
			string moduleName = string.Format("{0}Module", typeName);

			var name = new AssemblyName(assemblyName);
#if DEBUG
			AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
#else
      AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
#endif
			AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);

#if DEBUG
			ModuleBuilder moduleBuilder =
				assemblyBuilder.DefineDynamicModule(moduleName, string.Format("{0}.mod", moduleName), true);
#else
       ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
#endif

			TypeAttributes typeAttributes = TypeAttributes.AutoClass | TypeAttributes.Class |
			                                TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

			var interfaces = new HashSet<System.Type>();
			interfaces.Merge(baseInterfaces);

			// Use the proxy dummy as the base type 
			// since we're not inheriting from any class type
			System.Type parentType = baseType;
			if (baseType.IsInterface)
			{
				parentType = typeof (ProxyDummy);
				interfaces.Add(baseType);
			}

			// Add any inherited interfaces
			System.Type[] computedInterfaces = interfaces.ToArray();
			foreach (System.Type interfaceType in computedInterfaces)
			{
				interfaces.Merge(GetInterfaces(interfaceType));
			}

			// Add the ISerializable interface so that it can be implemented
			interfaces.Add(typeof (ISerializable));

			TypeBuilder typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());

			ConstructorBuilder defaultConstructor = DefineConstructor(typeBuilder);

			// Implement IProxy
			var implementor = new ProxyImplementor();
			implementor.ImplementProxy(typeBuilder);

			FieldInfo interceptorField = implementor.InterceptorField;
			
			// Provide a custom implementation of ISerializable
			// instead of redirecting it back to the interceptor
			foreach (MethodInfo method in GetProxiableMethods(baseType, interfaces).Where(method => method.DeclaringType != typeof(ISerializable)))
			{
				ProxyMethodBuilder.CreateProxiedMethod(interceptorField, method, typeBuilder);
			}

			// Make the proxy serializable
			AddSerializationSupport(baseType, baseInterfaces, typeBuilder, interceptorField, defaultConstructor);
			System.Type proxyType = typeBuilder.CreateType();

#if DEBUG_PROXY_OUTPUT
     assemblyBuilder.Save("generatedAssembly.dll");
#endif
			return proxyType;
		}
Beispiel #4
0
        private System.Type CreateUncachedProxyType(System.Type baseType, System.Type[] baseInterfaces)
        {
            AppDomain currentDomain = AppDomain.CurrentDomain;
            string    typeName      = string.Format("{0}Proxy", baseType.Name);
            string    assemblyName  = string.Format("{0}Assembly", typeName);
            string    moduleName    = string.Format("{0}Module", typeName);

            var name = new AssemblyName(assemblyName);

#if DEBUG
            AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;
#else
            AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
#endif
            AssemblyBuilder assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);

#if DEBUG
            ModuleBuilder moduleBuilder =
                assemblyBuilder.DefineDynamicModule(moduleName, string.Format("{0}.mod", moduleName), true);
#else
            ModuleBuilder moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
#endif

            TypeAttributes typeAttributes = TypeAttributes.AutoClass | TypeAttributes.Class |
                                            TypeAttributes.Public | TypeAttributes.BeforeFieldInit;

            var interfaces = new HashSet <System.Type>();
            interfaces.Merge(baseInterfaces);

            // Use the proxy dummy as the base type
            // since we're not inheriting from any class type
            System.Type parentType = baseType;
            if (baseType.IsInterface)
            {
                parentType = typeof(ProxyDummy);
                interfaces.Add(baseType);
            }

            // Add any inherited interfaces
            System.Type[] computedInterfaces = interfaces.ToArray();
            foreach (System.Type interfaceType in computedInterfaces)
            {
                interfaces.Merge(GetInterfaces(interfaceType));
            }

            // Add the ISerializable interface so that it can be implemented
            interfaces.Add(typeof(ISerializable));

            TypeBuilder typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());

            ConstructorBuilder defaultConstructor = DefineConstructor(typeBuilder);

            // Implement IProxy
            var implementor = new ProxyImplementor();
            implementor.ImplementProxy(typeBuilder);

            FieldInfo interceptorField = implementor.InterceptorField;

            // Provide a custom implementation of ISerializable
            // instead of redirecting it back to the interceptor
            foreach (MethodInfo method in GetProxiableMethods(baseType, interfaces).Where(method => method.DeclaringType != typeof(ISerializable)))
            {
                ProxyMethodBuilder.CreateProxiedMethod(interceptorField, method, typeBuilder);
            }

            // Make the proxy serializable
            AddSerializationSupport(baseType, baseInterfaces, typeBuilder, interceptorField, defaultConstructor);
            System.Type proxyType = typeBuilder.CreateType();

#if DEBUG_PROXY_OUTPUT
            assemblyBuilder.Save("generatedAssembly.dll");
#endif
            return(proxyType);
        }
Beispiel #5
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));
            }
        }
        private System.Type CreateUncachedProxyType(System.Type baseType, System.Type[] baseInterfaces)
        {
            var currentDomain = AppDomain.CurrentDomain;
            var typeName = string.Format("{0}Proxy", baseType.Name);
            var assemblyName = string.Format("{0}Assembly", typeName);
            var moduleName = string.Format("{0}Module", typeName);
            var name = new AssemblyName(assemblyName);
            //rippo: the [*]commented lines allow the serialize of the assembly containing the proxy
            //you can then run PEVERIFY to check the IL for type safety

            //[*]const AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;

            const AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
            var assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);
            
            //[*]var moduleBuilder = assemblyBuilder.DefineDynamicModule("generatedAssembly.dll", "generatedAssembly.dll", true);
            
            var moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
            const TypeAttributes typeAttributes =
                TypeAttributes.AutoClass |
                TypeAttributes.Class |
                TypeAttributes.Public |
                TypeAttributes.BeforeFieldInit;
            var interfaces = new HashSet<System.Type>();
            interfaces.Merge(baseInterfaces);
            var parentType = baseType;
            if (baseType.IsInterface)
            {
                parentType = typeof (ProxyDummy);
                interfaces.Add(baseType);
            }
            var computedInterfaces = interfaces.ToArray();
            foreach (var interfaceType in computedInterfaces)
            {
                interfaces.Merge(GetInterfaces(interfaceType));
            }
            interfaces.Add(typeof (ISerializable));
            var typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());
            var implementor = new ProxyImplementor();
            implementor.ImplementProxy(typeBuilder);
            var interceptorField = implementor.InterceptorField;
            AddSerializationSupport(typeBuilder);
            var constructors = baseType.GetConstructors();
            foreach (var constructorInfo in constructors)
            {
                var constructorBuilder = DefineConstructor(typeBuilder, constructorInfo);
                DefineSerializationConstructor(typeBuilder, interceptorField, constructorBuilder, constructorInfo);	
            }
            ImplementGetObjectData(baseType, baseInterfaces, typeBuilder, interceptorField);
            foreach (var method in GetProxiableMethods(baseType, interfaces).Where(method => method.DeclaringType != typeof(ISerializable)))
            {
                ProxyMethodBuilder.CreateProxiedMethod(interceptorField, method, typeBuilder);
            }
            var proxyType = typeBuilder.CreateType();
            
            //[*]assemblyBuilder.Save("generatedAssembly.dll");
            
            return proxyType;
        }
        private System.Type CreateUncachedProxyType(System.Type baseType, System.Type[] baseInterfaces)
        {
            var currentDomain = AppDomain.CurrentDomain;
            var typeName      = string.Format("{0}Proxy", baseType.Name);
            var assemblyName  = string.Format("{0}Assembly", typeName);
            var moduleName    = string.Format("{0}Module", typeName);
            var name          = new AssemblyName(assemblyName);
            //rippo: the [*]commented lines allow the serialize of the assembly containing the proxy
            //you can then run PEVERIFY to check the IL for type safety

            //[*]const AssemblyBuilderAccess access = AssemblyBuilderAccess.RunAndSave;

            const AssemblyBuilderAccess access = AssemblyBuilderAccess.Run;
            var assemblyBuilder = currentDomain.DefineDynamicAssembly(name, access);

            //[*]var moduleBuilder = assemblyBuilder.DefineDynamicModule("generatedAssembly.dll", "generatedAssembly.dll", true);

            var moduleBuilder = assemblyBuilder.DefineDynamicModule(moduleName);
            const TypeAttributes typeAttributes =
                TypeAttributes.AutoClass |
                TypeAttributes.Class |
                TypeAttributes.Public |
                TypeAttributes.BeforeFieldInit;
            var interfaces = new HashSet <System.Type>();

            interfaces.Merge(baseInterfaces);
            var parentType = baseType;

            if (baseType.IsInterface)
            {
                parentType = typeof(ProxyDummy);
                interfaces.Add(baseType);
            }
            var computedInterfaces = interfaces.ToArray();

            foreach (var interfaceType in computedInterfaces)
            {
                interfaces.Merge(GetInterfaces(interfaceType));
            }
            interfaces.Add(typeof(ISerializable));
            var typeBuilder = moduleBuilder.DefineType(typeName, typeAttributes, parentType, interfaces.ToArray());
            var implementor = new ProxyImplementor();

            implementor.ImplementProxy(typeBuilder);
            var interceptorField = implementor.InterceptorField;

            AddSerializationSupport(typeBuilder);
            var constructors = baseType.GetConstructors();

            foreach (var constructorInfo in constructors)
            {
                var constructorBuilder = DefineConstructor(typeBuilder, constructorInfo);
                DefineSerializationConstructor(typeBuilder, interceptorField, constructorBuilder, constructorInfo);
            }
            ImplementGetObjectData(baseType, baseInterfaces, typeBuilder, interceptorField);
            foreach (var method in GetProxiableMethods(baseType, interfaces).Where(method => method.DeclaringType != typeof(ISerializable)))
            {
                ProxyMethodBuilder.CreateProxiedMethod(interceptorField, method, typeBuilder);
            }
            var proxyType = typeBuilder.CreateType();

            //[*]assemblyBuilder.Save("generatedAssembly.dll");

            return(proxyType);
        }