예제 #1
0
        /**
         * Returns true if should generate binding for facet
         *
         * white list contains System.Threading.Tasks.Task
         * black list contains System.Runtime.CompilerServices
         * System.Runtime.CompilerServices.ConfiguredTaskAwaitable`1<System.Threading.Tasks.Task`1+TResult>
         *
         * we need to ensure that every component of the constructed type is valid.
         */
        public bool GenerateFacetBinding(CodeFacet facet)
        {
            // confirm member binding
            if (facet.GetType() == typeof(MethodFacet) || facet.GetType() == typeof(PropertyFacet) || facet.GetType() == typeof(FieldFacet))
            {
                // the accessor form for generic types requires that we use the RootType
                string accessor = CodeFacet.RootType(facet.Parent.Type) + ":" + facet.Name;
                if (!GenerateMemberBinding(accessor))
                {
                    return(false);
                }
            }

            // initial type test
            string type;

            if (facet.TypeNamespace != null)
            {
                type = facet.TypeNamespace + "." + facet.NameFromType;
            }
            else
            {
                type = facet.NameFromType;
            }

            // hmm... would not be clearer to reference facet.Type in the above
            if (facet.Type != type)
            {
                // we do see warnings here so a bit more investigation is required
                //Console.WriteLine($"Warning: facet.Type {facet.Type} != {type}");
            }

            if (!GenerateTypeBinding(type))
            {
                return(false);
            }

            // generic parameter type tests
            foreach (string t in facet.GenericArgumentTypes)
            {
                if (!GenerateTypeBinding(t))
                {
                    return(false);
                }
            }

            return(true);
        }
예제 #2
0
        public void WriteFacet(CodeFacet facet)
        {
            Type ft = facet.GetType();

            if (ft == typeof(EnumerationFacet))
            {
                WriteEnumeration((EnumerationFacet)facet);
            }
            else if (ft == typeof(InterfaceFacet))
            {
                WriteInterface((InterfaceFacet)facet);

                // provide access to the properties and methods of an explicit interface
                WriteInterfaceClass((InterfaceFacet)facet);

                // for protocol definitions we don't want to overwrite the class header so we append a suffix.
                facet.OutputFileNameSuffix = ".Protocol";
                WriteInterface((InterfaceFacet)facet);
                facet.OutputFileNameSuffix = "";
            }
            else if (ft == typeof(StructFacet))
            {
                WriteStruct((StructFacet)facet);
            }
            else if (ft == typeof(ClassFacet))
            {
                WriteClass((ClassFacet)facet);
            }
        }
예제 #3
0
        /// <summary>
        /// Returns all ObjC forward class and protocol declarations required to fully represent the facet.
        /// </summary>
        /// <param name="facet"></param>
        /// <returns></returns>
        public List <string> ObjCForwardDeclarations(CodeFacet facet)
        {
            List <string> imports = new List <string>();

            if (!Config.GenerateFacetBinding(facet))
            {
                return(imports);
            }

            // objC type
            // note that a constructor has no return type
            string objCType = facet.ObjCFacet.Type;

            if (!string.IsNullOrEmpty(objCType))
            {
                // if the type is a generic parameter
                if (facet.IsGenericParameterOrRef())
                {
                    objCType = "System_Object";
                }

                string import = String.Format("@class {0};", objCType);
                imports.Add(import);

                if (facet.IsArray == true)
                {
                    import = String.Format("@class {0};", facet.ObjCFacet.BaseType);
                    imports.Add(import);
                }

                if (facet.GetType() == typeof(InterfaceFacet) || facet.IsInterface)
                {
                    import = String.Format("@protocol {0}_;", objCType);
                    imports.Add(import);

                    import = String.Format("@protocol {0};", objCType);
                    imports.Add(import);
                }
            }

            // forward declare objC facet types for all children
            List <CodeFacet> children = facet.Children();

            foreach (CodeFacet child in children)
            {
                imports.AddRange(ObjCForwardDeclarations(child));
            }

            // return a distinct list to remove duplicates
            imports = imports.Distinct().ToList();
            imports.Sort();
            return(imports);
        }
예제 #4
0
        //
        // OutputImplementedProtocolSuffix()
        //
        // Return a class declaration suffix based on the outputType
        //
        string OutputImplementedProtocolSuffix(CodeFacet facet)
        {
            string value = "";
            if (OutputFileType == OutputType.Interface)
            {

                // class or interface facet
                if (facet is InterfaceFacet)
                {
                    var interfaceFacet = (InterfaceFacet)facet;
                    IList<ImplementedInterfaceFacet> implementedInterfaces = interfaceFacet.ImplementedInterfaces;

                    if (implementedInterfaces.Count > 0)
                    {
                        // we may wish to naively filter out system interfaces while full
                        // system code generation is pending.
                        // this will hopefully let us usefully represent user implemented interfaces.
                        if (Config.FilterSystemInterfaces)
                        {
                            var interfaces = new List<ImplementedInterfaceFacet>();
                            foreach (
                                ImplementedInterfaceFacet implementedInterfaceFacet in implementedInterfaces)
                            {
                                string interfaceType = implementedInterfaceFacet.Type;
                                bool isNaiveSystemType = interfaceType.StartsWith("System.",
                                    StringComparison.OrdinalIgnoreCase);

                                if (!isNaiveSystemType)
                                {
                                    interfaces.Add(implementedInterfaceFacet);
                                }
                            }

                            implementedInterfaces = interfaces;
                        }

                        if (implementedInterfaces.Count > 0)
                        {
                            // cast
                            IList<CodeFacet> codeFacets = implementedInterfaces.Cast<CodeFacet>().ToList();

                            // facet is interface facet type?
                            if (facet.GetType() == typeof(InterfaceFacet)) {
                                // insert the interface's own type as we are defining the protocols
                                // that will be used to define a class representing the interface
                                codeFacets.Insert(0, facet);
                            }


                            value = " <";
                            int i = 0;
                            foreach (CodeFacet codeFacet in codeFacets)
                            {
                                if (i++ > 0) value += ", ";
                                value += ObjCIdentifierFromManagedIdentifier(codeFacet.Type);
                            }

 
                            value += ">";
                        }
                    }
                }

              
            }
            return value;
        }
예제 #5
0
        //
        // OutputImplementedProtocolSuffix()
        //
        // Return a class declaration suffix based on the outputType
        //
        string OutputImplementedProtocolSuffix(CodeFacet facet)
        {
            string value = "";

            if (OutputFileType == OutputType.Interface)
            {
                // class or interface facet
                if (facet is InterfaceFacet)
                {
                    var interfaceFacet = (InterfaceFacet)facet;
                    IList <ImplementedInterfaceFacet> implementedInterfaces = interfaceFacet.ImplementedInterfaces;

                    // heed required type binding
                    implementedInterfaces = implementedInterfaces.Where(f => Config.GenerateFacetBinding(f)).ToList();

                    if (implementedInterfaces.Count > 0)
                    {
                        // we may wish to naively filter out system interfaces while full
                        // system code generation is pending.
                        // this will hopefully let us usefully represent user implemented interfaces.
                        if (Config.FilterSystemInterfaces)
                        {
                            var interfaces = new List <ImplementedInterfaceFacet>();
                            foreach (ImplementedInterfaceFacet implementedInterfaceFacet in implementedInterfaces)
                            {
                                string interfaceType      = implementedInterfaceFacet.Type;
                                bool   isNativeSystemType = interfaceType.StartsWith("System.", StringComparison.OrdinalIgnoreCase);

                                if (!isNativeSystemType)
                                {
                                    interfaces.Add(implementedInterfaceFacet);
                                }
                            }

                            implementedInterfaces = interfaces;
                        }

                        if (implementedInterfaces.Count > 0)
                        {
                            // cast
                            IList <CodeFacet> codeFacets = implementedInterfaces.Cast <CodeFacet>().ToList();

                            // facet is interface facet type?
                            if (facet.GetType() == typeof(InterfaceFacet))
                            {
                                // insert the interface's own type as we are defining the protocols
                                // that will be used to define a class representing the interface
                                codeFacets.Insert(0, facet);
                            }

                            value = " <";
                            int i = 0;
                            foreach (CodeFacet codeFacet in codeFacets)
                            {
                                if (i++ > 0)
                                {
                                    value += ", ";
                                }
                                value += (ObjCIdentifierFromManagedIdentifier(codeFacet.Type) + "_");
                            }


                            value += ">";
                        }
                    }
                }
            }
            return(value);
        }