Esempio n. 1
0
        public void MethodWithNameStartingWithSetCreatesProperty()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = new CsFundamentalType(typeof(int));

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsFundamentalType(typeof(void))
                }
            };

            setMethod.Add(new CsParameter
            {
                PublicType = paramType
            });


            var properties = propertyBuilder.CreateProperties(new[] { setMethod });

            Assert.True(properties.ContainsKey("Active"));
            var prop = properties["Active"];

            Assert.Equal(paramType, prop.PublicType);
        }
Esempio n. 2
0
        public void PropertyNotAttachedWhenSetterAllowPropertyIsFalse()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = new CsFundamentalType(typeof(int));

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsFundamentalType(typeof(void))
                },
                AllowProperty = false
            };

            setMethod.Add(new CsParameter
            {
                PublicType = paramType
            });

            var iface = new CsInterface();

            iface.Add(setMethod);

            var prop = new CsProperty("Active")
            {
                Setter = setMethod
            };

            propertyBuilder.AttachPropertyToParent(prop);

            Assert.Null(prop.Parent);
        }
Esempio n. 3
0
        public void SetterVisibilityInternal()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = new CsFundamentalType(typeof(int));

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsFundamentalType(typeof(void))
                },
                AllowProperty = true
            };

            setMethod.Add(new CsParameter
            {
                PublicType = paramType
            });

            var iface = new CsInterface();

            iface.Add(setMethod);

            var prop = new CsProperty("Active")
            {
                Setter = setMethod
            };

            propertyBuilder.AttachPropertyToParent(prop);

            Assert.Equal(Visibility.Internal, setMethod.Visibility);
        }
Esempio n. 4
0
        public void DoesNotGeneratePropertyIfGetterAndSetterMismatch()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = new CsFundamentalType(typeof(int));

            var getMethod = new CsMethod
            {
                Name        = "GetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType  = paramType,
                    MarshalType = paramType
                },
            };

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsFundamentalType(typeof(void))
                }
            };

            setMethod.Add(new CsParameter
            {
                PublicType = new CsFundamentalType(typeof(short))
            });

            var props = propertyBuilder.CreateProperties(new[] { getMethod, setMethod });

            Assert.Empty(props);
        }
Esempio n. 5
0
        public void SetterVisibilityInternal()
        {
            CppMethod cppSetMethod = new("SetActive")
            {
                Rule =
                {
                    Property = true
                }
            };

            var paramType = TypeRegistry.Int32;

            var setMethod = new CsMethod(cppSetMethod, cppSetMethod.Name)
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType = TypeRegistry.Void
                }
            };

            setMethod.Add(new CsParameter(null, null)
            {
                PublicType = paramType
            });

            var iface = new CsInterface(null, null);

            iface.Add(setMethod);

            var prop = new CsProperty(null, "Active", null, setMethod);

            PropertyBuilder.AttachPropertyToParent(prop);

            Assert.Equal(Visibility.Internal, setMethod.Visibility);
        }
Esempio n. 6
0
        public void GetterMethodReturningStatusCodeWithOutParamGeneratesProperty()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = new CsFundamentalType(typeof(int));

            var getMethod = new CsMethod
            {
                Name        = "GetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsStruct
                    {
                        Name = "SharpGen.Runtime.Result"
                    }
                }
            };

            getMethod.Add(new CsParameter
            {
                PublicType = paramType,
                Attribute  = CsParameterAttribute.Out
            });


            var properties = propertyBuilder.CreateProperties(new[] { getMethod });

            Assert.True(properties.ContainsKey("Active"));
            var prop = properties["Active"];

            Assert.True(prop.IsPropertyParam);
            Assert.Equal(paramType, prop.PublicType);
        }
Esempio n. 7
0
        public void MethodWithNameStartingWithSetCreatesProperty()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = TypeRegistry.Int32;

            var setMethod = new CsMethod(null, "SetActive")
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType = TypeRegistry.Void
                }
            };

            setMethod.Add(new CsParameter(null, null)
            {
                PublicType = paramType
            });

            var properties = propertyBuilder.CreateProperties(new[] { setMethod });

            Assert.True(properties.ContainsKey("Active"));
            var prop = properties["Active"];

            Assert.Equal(paramType, prop.PublicType);
        }
Esempio n. 8
0
        public void DoesNotGeneratePropertyIfGetterAndSetterMismatch()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = TypeRegistry.Int32;

            var getMethod = new CsMethod(null, "GetActive")
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType  = paramType,
                    MarshalType = paramType
                },
            };

            var setMethod = new CsMethod(null, "SetActive")
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType = TypeRegistry.Void
                }
            };

            setMethod.Add(new CsParameter(null, null)
            {
                PublicType = TypeRegistry.Int16
            });

            var props = propertyBuilder.CreateProperties(new[] { getMethod, setMethod });

            Assert.Empty(props);
        }
Esempio n. 9
0
        public void PropertyNotAttachedWhenSetterAllowPropertyIsFalse()
        {
            CppMethod cppSetMethod = new("SetActive")
            {
                Rule =
                {
                    Property = false
                }
            };

            var paramType = TypeRegistry.Int32;

            var setMethod = new CsMethod(cppSetMethod, cppSetMethod.Name)
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType = TypeRegistry.Void
                }
            };

            setMethod.Add(new CsParameter(null, null)
            {
                PublicType = paramType
            });

            var iface = new CsInterface(null, null);

            iface.Add(setMethod);

            var prop = new CsProperty(null, "Active", null, setMethod);

            PropertyBuilder.AttachPropertyToParent(prop);

            Assert.Null(prop.Parent);
        }
Esempio n. 10
0
        public void GetterMethodReturningStatusCodeWithOutParamGeneratesProperty()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = TypeRegistry.Int32;

            var getMethod = new CsMethod(null, "GetActive")
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType = new CsStruct(null, "SharpGen.Runtime.Result")
                }
            };

            getMethod.Add(new CsParameter(null, null)
            {
                PublicType = paramType,
                Attribute  = CsParameterAttribute.Out
            });


            var properties = propertyBuilder.CreateProperties(new[] { getMethod });

            Assert.True(properties.ContainsKey("Active"));
            var prop = properties["Active"];

            Assert.True(prop.IsPropertyParam);
            Assert.Equal(paramType, prop.PublicType);
        }
Esempio n. 11
0
        public void SetOnlyPropertyAttachedToSetterType()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider("SharpGen.Runtime"));

            var paramType = new CsFundamentalType(typeof(int));

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsFundamentalType(typeof(void))
                },
                AllowProperty = true
            };

            setMethod.Add(new CsParameter
            {
                PublicType = paramType
            });

            var iface = new CsInterface();

            iface.Add(setMethod);

            var prop = new CsProperty("Active")
            {
                Setter = setMethod
            };

            propertyBuilder.AttachPropertyToParent(prop);

            Assert.Equal(iface, prop.Parent);
        }
Esempio n. 12
0
        public void MethodWithNameStartingWithSetAndReturningResultGeneratesProperty()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = new CsFundamentalType(typeof(int));

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsStruct {
                        Name = "SharpGen.Runtime.Result"
                    }
                }
            };

            setMethod.Add(new CsParameter
            {
                PublicType = paramType
            });

            var properties = propertyBuilder.CreateProperties(new[] { setMethod });

            Assert.True(properties.ContainsKey("Active"), "Property not created");
            var prop = properties["Active"];

            Assert.Equal(paramType, prop.PublicType);
        }
Esempio n. 13
0
        public void MethodWithNameStartingWithSetAndReturningResultGeneratesProperty()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = TypeRegistry.Int32;

            var setMethod = new CsMethod(null, "SetActive")
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType = new CsStruct(null, "SharpGen.Runtime.Result")
                }
            };

            setMethod.Add(new CsParameter(null, null)
            {
                PublicType = paramType
            });

            var properties = propertyBuilder.CreateProperties(new[] { setMethod });

            Assert.True(properties.ContainsKey("Active"), "Property not created");
            var prop = properties["Active"];

            Assert.Equal(paramType, prop.PublicType);
        }
Esempio n. 14
0
        public void DoesNotGeneratePropertyIfOverloaded()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider("SharpGen.Runtime"));

            var paramType = new CsFundamentalType(typeof(int));

            var getMethod = new CsMethod
            {
                Name        = "GetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType  = paramType,
                    MarshalType = paramType
                },
            };

            var getMethodOverload = new CsMethod
            {
                Name        = "GetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType  = paramType,
                    MarshalType = paramType
                },
            };

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsFundamentalType(typeof(void))
                }
            };

            setMethod.Add(new CsParameter
            {
                PublicType = paramType
            });

            Assert.Empty(propertyBuilder.CreateProperties(new[] { getMethod, setMethod, getMethodOverload }));
            Assert.Empty(propertyBuilder.CreateProperties(new[] { getMethod, getMethodOverload, setMethod }));
        }
Esempio n. 15
0
        public void DoesNotGeneratePropertyIfGetterAndSetterMismatch_ParameterizedGetter()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = new CsFundamentalType(typeof(int));

            var getMethod = new CsMethod
            {
                Name        = "GetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsStruct
                    {
                        Name = "SharpGen.Runtime.Result"
                    }
                }
            };

            getMethod.Add(new CsParameter
            {
                PublicType = paramType,
                Attribute  = CsParameterAttribute.Out
            });

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsFundamentalType(typeof(void))
                }
            };

            setMethod.Add(new CsParameter
            {
                PublicType = new CsFundamentalType(typeof(short))
            });

            var props = propertyBuilder.CreateProperties(new[] { getMethod, setMethod });

            Assert.Empty(props);
        }
Esempio n. 16
0
        public void DoesNotGeneratePropertyIfGetterAndSetterMismatch_ParameterizedGetter()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider());

            var paramType = TypeRegistry.Int32;

            var getMethod = new CsMethod(null, "GetActive")
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType = new CsStruct(null, "SharpGen.Runtime.Result")
                }
            };

            getMethod.Add(new CsParameter(null, null)
            {
                PublicType = paramType,
                Attribute  = CsParameterAttribute.Out
            });

            var setMethod = new CsMethod(null, "SetActive")
            {
                ReturnValue = new CsReturnValue(null)
                {
                    PublicType = TypeRegistry.Void
                }
            };

            setMethod.Add(new CsParameter(null, null)
            {
                PublicType = TypeRegistry.Int16
            });

            var props = propertyBuilder.CreateProperties(new[] { getMethod, setMethod });

            Assert.Empty(props);
        }
Esempio n. 17
0
        public void GeneratePropertyIfGetterAndSetterMatch()
        {
            var propertyBuilder = new PropertyBuilder(new GlobalNamespaceProvider("SharpGen.Runtime"));

            var paramType = new CsFundamentalType(typeof(int));

            var getMethod = new CsMethod
            {
                Name        = "GetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType  = paramType,
                    MarshalType = paramType
                },
            };

            var setMethod = new CsMethod
            {
                Name        = "SetActive",
                ReturnValue = new CsReturnValue
                {
                    PublicType = new CsFundamentalType(typeof(void))
                }
            };

            setMethod.Add(new CsParameter
            {
                PublicType = paramType
            });

            var props = propertyBuilder.CreateProperties(new[] { getMethod, setMethod });

            Assert.Single(props);
            Assert.NotNull(props["Active"].Getter);
            Assert.NotNull(props["Active"].Setter);
        }
Esempio n. 18
0
        /// <summary>
        /// Processes the specified method.
        /// </summary>
        /// <param name="method">The method.</param>
        private void Process(CsMethod method)
        {
            var cppMethod = (CppMethod)method.CppElement;

            method.Name   = NamingRules.Rename(cppMethod);
            method.Offset = cppMethod.Offset;

            // For methods, the tag "type" is only used for return type
            // So we are overriding the return type here
            var tag = cppMethod.GetTagOrDefault <MappingRule>();

            if (tag.MappingType != null)
            {
                cppMethod.ReturnType.Tag = new MappingRule {
                    MappingType = tag.MappingType
                }
            }
            ;

            // Apply any offset to the method's vtable
            method.Offset += tag.LayoutOffsetTranslate;

            // Get the inferred return type
            method.ReturnType = Manager.GetCsType <CsMarshalBase>(cppMethod.ReturnType);

            // Hide return type only if it is a HRESULT and AlwaysReturnHResult is false
            if (method.CheckReturnType && method.ReturnType.PublicType != null &&
                method.ReturnType.PublicType.QualifiedName == Manager.GlobalNamespace.GetTypeName("Result"))
            {
                method.HideReturnType = !method.AlwaysReturnHResult;
            }

            // Iterates on parameters to convert them to C# parameters
            foreach (var cppParameter in cppMethod.Parameters)
            {
                var cppAttribute = cppParameter.Attribute;
                var paramTag     = cppParameter.GetTagOrDefault <MappingRule>();

                bool hasArray   = cppParameter.IsArray || ((cppAttribute & ParamAttribute.Buffer) != 0);
                bool hasParams  = (cppAttribute & ParamAttribute.Params) == ParamAttribute.Params;
                bool isOptional = (cppAttribute & ParamAttribute.Optional) != 0;

                var paramMethod = Manager.GetCsType <CsParameter>(cppParameter);

                paramMethod.Name = NamingRules.Rename(cppParameter);

                bool hasPointer = paramMethod.HasPointer;

                var publicType  = paramMethod.PublicType;
                var marshalType = paramMethod.MarshalType;

                CsParameterAttribute parameterAttribute = CsParameterAttribute.In;

                if (hasArray)
                {
                    hasPointer = true;
                }

                // --------------------------------------------------------------------------------
                // Pointer - Handle special cases
                // --------------------------------------------------------------------------------
                if (hasPointer)
                {
                    marshalType = Manager.ImportType(typeof(IntPtr));

                    // --------------------------------------------------------------------------------
                    // Handling Parameter Interface
                    // --------------------------------------------------------------------------------
                    if (publicType is CsInterface)
                    {
                        // Force Interface** to be ParamAttribute.Out when None
                        if (cppAttribute == ParamAttribute.In)
                        {
                            if (cppParameter.Pointer == "**")
                            {
                                cppAttribute = ParamAttribute.Out;
                            }
                        }

                        if ((cppAttribute & ParamAttribute.In) != 0 || (cppAttribute & ParamAttribute.InOut) != 0)
                        {
                            parameterAttribute = CsParameterAttribute.In;

                            // Force all array of interface to support null
                            if (hasArray)
                            {
                                isOptional = true;
                            }

                            // If Interface is a callback, use IntPtr as a public marshalling type
                            CsInterface publicCsInterface = (CsInterface)publicType;
                            if (publicCsInterface.IsCallback)
                            {
                                publicType = Manager.ImportType(typeof(IntPtr));
                                // By default, set the Visibility to internal for methods using callbacks
                                // as we need to provide user method. Don't do this on functions as they
                                // are already hidden by the container
                                if (!(method is CsFunction))
                                {
                                    method.Visibility = Visibility.Internal;
                                    method.Name       = method.Name + "_";
                                }
                            }
                        }
                        //else if ((cppParameter.Attribute & ParamAttribute.InOut) != 0)
                        //    parameterAttribute = method.ParameterAttribute.Ref;
                        else if ((cppAttribute & ParamAttribute.Out) != 0)
                        {
                            parameterAttribute = CsParameterAttribute.Out;
                        }
                    }
                    else
                    {
                        // If a pointer to array of bool are handle as array of int
                        if (paramMethod.IsBoolToInt && (cppAttribute & ParamAttribute.Buffer) != 0)
                        {
                            publicType = Manager.ImportType(typeof(int));
                        }

                        // --------------------------------------------------------------------------------
                        // Handling Parameter Interface
                        // --------------------------------------------------------------------------------


                        if ((cppAttribute & ParamAttribute.In) != 0)
                        {
                            parameterAttribute = publicType.Type == typeof(IntPtr) || publicType.Name == Manager.GlobalNamespace.GetTypeName("FunctionCallback") ||
                                                 publicType.Type == typeof(string)
                                                     ? CsParameterAttribute.In
                                                     : CsParameterAttribute.RefIn;
                        }
                        else if ((cppAttribute & ParamAttribute.InOut) != 0)
                        {
                            if ((cppAttribute & ParamAttribute.Optional) != 0)
                            {
                                publicType         = Manager.ImportType(typeof(IntPtr));
                                parameterAttribute = CsParameterAttribute.In;
                            }
                            else
                            {
                                parameterAttribute = CsParameterAttribute.Ref;
                            }
                        }
                        else if ((cppAttribute & ParamAttribute.Out) != 0)
                        {
                            parameterAttribute = CsParameterAttribute.Out;
                        }

                        // Handle void* with Buffer attribute
                        if (cppParameter.TypeName == "void" && (cppAttribute & ParamAttribute.Buffer) != 0)
                        {
                            hasArray           = false;
                            parameterAttribute = CsParameterAttribute.In;
                        }
                        else if (publicType.Type == typeof(string) && (cppAttribute & ParamAttribute.Out) != 0)
                        {
                            publicType         = Manager.ImportType(typeof(IntPtr));
                            parameterAttribute = CsParameterAttribute.In;
                            hasArray           = false;
                        }
                        else if (publicType is CsStruct &&
                                 (parameterAttribute == CsParameterAttribute.Out || hasArray || parameterAttribute == CsParameterAttribute.RefIn || parameterAttribute == CsParameterAttribute.Ref))
                        {
                            // Set IsOut on structure to generate proper marshalling
                            (publicType as CsStruct).IsOut = true;
                        }
                    }
                }

                paramMethod.HasPointer  = hasPointer;
                paramMethod.Attribute   = parameterAttribute;
                paramMethod.IsArray     = hasArray;
                paramMethod.HasParams   = hasParams;
                paramMethod.HasPointer  = hasPointer;
                paramMethod.PublicType  = publicType ?? throw new ArgumentException("Public type cannot be null");
                paramMethod.MarshalType = marshalType;
                paramMethod.IsOptional  = isOptional;

                // Force IsString to be only string (due to Buffer attribute)
                if (paramMethod.IsString)
                {
                    paramMethod.IsArray = false;
                }

                method.Add(paramMethod);
            }
        }
    }