public void BoolToIntSetForAllIntegerTypes(PrimitiveTypeCode integerType)
        {
            var cppMarshallable = new CppParameter("param")
            {
                TypeName       = "Integer",
                ArrayDimension = "0",
                IsArray        = true,
                Rule           =
                {
                    MappingType = "bool"
                }
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("bool", TypeRegistry.Boolean);
            typeRegistry.BindType("Integer", TypeRegistry.ImportPrimitiveType(integerType));

            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

            Assert.Equal(TypeRegistry.Boolean, csMarshallable.PublicType);
            Assert.Equal(TypeRegistry.ImportPrimitiveType(integerType), csMarshallable.MarshalType);
            Assert.True(csMarshallable.IsBoolToInt);
            Assert.False(csMarshallable.HasPointer);
            Assert.False(csMarshallable.IsArray);
            Assert.Equal(0, csMarshallable.ArrayDimensionValue);
        }
    public void BoolToIntSetForAllIntegerTypes(PrimitiveTypeCode integerType)
    {
        var cppMarshallable = new CppParameter("param")
        {
            TypeName       = "Integer",
            ArrayDimension = "0",
            Rule           =
            {
                MappingType = "bool"
            }
        };

        var integerPrimitiveType = TypeRegistry.ImportPrimitiveType(integerType);

        TypeRegistry.BindType("bool", TypeRegistry.Boolean);
        TypeRegistry.BindType("Integer", integerPrimitiveType);


        var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

        Assert.Equal(TypeRegistry.Boolean, csMarshallable.PublicType);
        Assert.Equal(integerPrimitiveType, csMarshallable.MarshalType);
        Assert.True(csMarshallable.IsBoolToInt);
        Assert.False(csMarshallable.HasPointer);
        Assert.False(csMarshallable.IsArray);
        Assert.Equal(0, csMarshallable.ArrayDimensionValue);
    }
        public void BoundTypeForArrayType(string elementType)
        {
            var cppMarshallable = new CppParameter("param")
            {
                TypeName       = elementType,
                IsArray        = true,
                ArrayDimension = "ANYSIZE_ARRAY"
            };

            var         typeRegistry      = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());
            CsInterface dynamicStringType = new(null, "DynamicString");

            typeRegistry.BindType(elementType + "[ANYSIZE_ARRAY]", dynamicStringType);

            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

            Assert.Equal(dynamicStringType, csMarshallable.PublicType);
            Assert.Equal(dynamicStringType, csMarshallable.MarshalType);
            Assert.Equal(0, csMarshallable.ArrayDimensionValue);
            Assert.False(csMarshallable.IsArray);
            Assert.False(csMarshallable.IsString);
            Assert.False(csMarshallable.IsWideChar);
        }
Esempio n. 4
0
        /// <summary>
        /// Renames the specified C++ parameter.
        /// </summary>
        /// <param name="cppParameter">The C++ parameter.</param>
        /// <returns>The C# name of this parameter.</returns>
        public string Rename(CppParameter cppParameter)
        {
            string oldName = cppParameter.Name;
            string name    = RenameCore(cppParameter, null);

            bool hasPointer = !string.IsNullOrEmpty(cppParameter.Pointer) &&
                              (cppParameter.Pointer.Contains("*") || cppParameter.Pointer.Contains("&"));

            if (hasPointer)
            {
                if (oldName.StartsWith("pp"))
                {
                    name = name.Substring(2) + "Out";
                }
                else if (oldName.StartsWith("p"))
                {
                    name = name.Substring(1) + "Ref";
                }
            }
            if (char.IsDigit(name[0]))
            {
                name = "arg" + name;
            }


            name = new string(name[0], 1).ToLower() + name.Substring(1);

            return(RecordRename(cppParameter, UnKeyword(name)));
        }
        public static CSharpElement ConvertParameter(CSharpConverter converter, CppParameter cppParam, int index, CSharpElement context)
        {
            var parent     = ((CSharpElement)(context as CSharpMethod) ?? (context as CSharpDelegate));
            var parameters = (context as CSharpMethod)?.Parameters ?? (context as CSharpDelegate)?.Parameters;

            if (parameters == null)
            {
                return(null);
            }

            var csParamName = string.IsNullOrEmpty(cppParam.Name) ? "arg" + index : converter.GetCSharpName(cppParam, context);
            var csParam     = new CSharpParameter(csParamName)
            {
                CppElement = cppParam, Parent = parent
            };

            parameters.Add(csParam);

            var csParamType = converter.GetCSharpType(cppParam.Type, csParam);

            csParam.Index         = index;
            csParam.ParameterType = csParamType;

            return(csParam);
        }
        public void NativeTypeTakesPrecedenceOverMarshalTypeForMappedType()
        {
            var cppMarshallable = new CppParameter("param")
            {
                TypeName       = "short",
                ArrayDimension = "0",
                IsArray        = true,
                Rule           =
                {
                    MappingType = "bool"
                }
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("bool", TypeRegistry.Boolean, TypeRegistry.Int32);
            typeRegistry.BindType("short", TypeRegistry.Int16);

            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

            Assert.Equal(TypeRegistry.Boolean, csMarshallable.PublicType);
            Assert.Equal(TypeRegistry.Int16, csMarshallable.MarshalType);
            Assert.False(csMarshallable.HasPointer);
            Assert.False(csMarshallable.IsArray);
            Assert.Equal(0, csMarshallable.ArrayDimensionValue);
        }
        private ParameterSyntax BuildParameter(CppParameter cppParameter, bool convertConstCharPtr, bool useFunctionPointers = false)
        {
            var name = Identifier(GetManagedName(cppParameter.Name));

            if (TypeMap.TryResolveType(cppParameter.Type, out var typeInfo))
            {
                if (convertConstCharPtr && typeInfo.CppType.IsConstCharPtr())
                {
                    return(Parameter(name)
                           .WithType(SyntaxBuilder.BuildReadOnlySpanName(SyntaxKind.CharKeyword))
                           .AddModifiers(Token(SyntaxKind.InKeyword)));
                }

                if (useFunctionPointers && typeInfo.IsFunction)
                {
                    return(Parameter(name).WithType(SyntaxBuilder.IntPtrName));
                }

                return(Parameter(name).WithType(typeInfo.TypeSyntax));
            }

            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }
            throw new NotSupportedException();
        }
Esempio n. 8
0
    private static string PostprocessParameterName(CppParameter element, string name, bool isFinal)
    {
        name = FixDigitName(name, "arg");

        if (!isFinal && !char.IsLower(name[0]))
        {
            name = char.ToLower(name[0]) + name.Substring(1);
        }

        return(UnKeyword(name));
    }
    public void ParamWithNoTypeMappingShouldHaveMarshalTypeEqualToPublic()
    {
        var marshallable = new CppParameter("param")
        {
            TypeName = "int"
        };

        TypeRegistry.BindType("int", TypeRegistry.Int32);

        var csMarshallable = marshalledElementFactory.Create(marshallable, marshallable.Name);

        Assert.Equal(csMarshallable.PublicType, csMarshallable.MarshalType);
    }
    public void DoublePointerParameterMappedAsOut()
    {
        var cppParameter = new CppParameter("param")
        {
            TypeName = "Interface",
            Pointer  = "**"
        };

        TypeRegistry.BindType("Interface", new CsInterface(null, "Interface"));

        var csParameter = marshalledElementFactory.Create(cppParameter, cppParameter.Name);

        Assert.Equal(CsParameterAttribute.Out, csParameter.Attribute);
    }
Esempio n. 11
0
        private ParameterSyntax BuildParameter(CppParameter cppParameter)
        {
            var name = Identifier(cppParameter.Name);

            if (TypeMap.TryResolveType(cppParameter.Type, out var typeInfo))
            {
                return(Parameter(name).WithType(typeInfo.TypeSyntax));
            }

            if (Debugger.IsAttached)
            {
                Debugger.Break();
            }
            throw new NotSupportedException();
        }
    public void CharPointerMappedToStringMarshalledWithIntPtr()
    {
        var cppMarshallable = new CppParameter("param")
        {
            TypeName = "char",
            Pointer  = "*"
        };

        var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

        Assert.Equal(TypeRegistry.String, csMarshallable.PublicType);
        Assert.Equal(TypeRegistry.IntPtr, csMarshallable.MarshalType);
        Assert.True(csMarshallable.HasPointer);
        Assert.False(csMarshallable.IsArray);
        Assert.Equal(0, csMarshallable.ArrayDimensionValue);
    }
    public void PointerToTypeWithMarshallingMarshalsAsUnderlyingType()
    {
        var cppMarshallable = new CppParameter("param")
        {
            TypeName = "bool",
            Pointer  = "*"
        };

        TypeRegistry.BindType("bool", TypeRegistry.Boolean, TypeRegistry.Int32);

        var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

        Assert.Equal(TypeRegistry.Int32, csMarshallable.MarshalType);
        Assert.Equal(0, csMarshallable.ArrayDimensionValue);
        Assert.False(csMarshallable.IsArray);
        Assert.True(csMarshallable.HasPointer);
    }
        public void ParamWithNoTypeMappingShouldHaveMarshalTypeEqualToPublic()
        {
            var marshallable = new CppParameter("param")
            {
                TypeName = "int"
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("int", TypeRegistry.Int32);

            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csMarshallable = marshalledElementFactory.Create(marshallable, marshallable.Name);

            Assert.Equal(csMarshallable.PublicType, csMarshallable.MarshalType);
        }
        public void ParameterWithStructSizeRelationLogsError()
        {
            var cppParameter = new CppParameter
            {
                TypeName = "int"
            };

            cppParameter.GetMappingRule().Relation = "struct_size()";

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("int", typeRegistry.ImportType(typeof(int)));
            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider("SharpGen.Runtime"), typeRegistry);
            var csParameter = marshalledElementFactory.Create(cppParameter);

            AssertLoggingCodeLogged(LoggingCodes.InvalidRelation);
        }
    public void CharArrayMappedToStringMarshalledWithByte()
    {
        var cppMarshallable = new CppParameter("param")
        {
            TypeName       = "char",
            ArrayDimension = "10"
        };

        var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

        Assert.Equal(TypeRegistry.String, csMarshallable.PublicType);
        Assert.Equal(TypeRegistry.UInt8, csMarshallable.MarshalType);
        Assert.Equal(10, csMarshallable.ArrayDimensionValue);
        Assert.False(csMarshallable.IsArray);
        Assert.True(csMarshallable.IsString);
        Assert.False(csMarshallable.IsWideChar);
    }
    public void DoublePointerNonInterfaceParameterMappedAsIntPtr()
    {
        var cppParameter = new CppParameter("param")
        {
            TypeName = "int",
            Pointer  = "**"
        };

        TypeRegistry.BindType("int", TypeRegistry.Int32);
        var csParameter = marshalledElementFactory.Create(cppParameter, cppParameter.Name);

        Assert.Equal(TypeRegistry.IntPtr, csParameter.PublicType);
        Assert.Equal(TypeRegistry.IntPtr, csParameter.MarshalType);
        Assert.True(csParameter.HasPointer);
        Assert.False(csParameter.IsArray);
        Assert.Equal(0, csParameter.ArrayDimensionValue);
    }
    public void PointerParameterMarkedAsHasPointer()
    {
        var cppParameter = new CppParameter("param")
        {
            TypeName = "int",
            Pointer  = "*"
        };

        TypeRegistry.BindType("int", TypeRegistry.Int32);

        var csParameter = marshalledElementFactory.Create(cppParameter, cppParameter.Name);

        Assert.False(csParameter.IsArray);
        Assert.False(csParameter.IsString);
        Assert.True(csParameter.HasPointer);
        Assert.Equal(0, csParameter.ArrayDimensionValue);
    }
        public void PointerParameterMarkedAsHasPointer()
        {
            var cppParameter = new CppParameter
            {
                TypeName = "int",
                Pointer  = "*"
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("int", typeRegistry.ImportType(typeof(int)));
            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csParameter = marshalledElementFactory.Create(cppParameter);

            Assert.True(csParameter.HasPointer);
        }
        public void DoublePointerNonInterfaceParameterMappedAsIntPtr()
        {
            var cppParameter = new CppParameter
            {
                TypeName = "int",
                Pointer  = "**"
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("int", typeRegistry.ImportType(typeof(int)));
            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);
            var csParameter = marshalledElementFactory.Create(cppParameter);

            Assert.Equal(typeRegistry.ImportType(typeof(IntPtr)), csParameter.PublicType);
            Assert.Equal(typeRegistry.ImportType(typeof(IntPtr)), csParameter.MarshalType);
            Assert.True(csParameter.HasPointer);
        }
    public void BoolToIntArray()
    {
        var cppParameter = new CppParameter("param")
        {
            TypeName  = "bool", Pointer = "*",
            Attribute = ParamAttribute.In | ParamAttribute.Buffer
        };

        TypeRegistry.BindType("bool", TypeRegistry.Boolean, TypeRegistry.UInt8);

        var csParameter = marshalledElementFactory.Create(cppParameter, cppParameter.Name);

        Assert.Equal(TypeRegistry.Boolean, csParameter.PublicType);
        Assert.Equal(TypeRegistry.UInt8, csParameter.MarshalType);
        Assert.True(csParameter.IsArray);
        Assert.True(csParameter.IsBoolToInt);
        Assert.Equal(0, csParameter.ArrayDimensionValue);
    }
        public void DoublePointerParameterMappedAsOut()
        {
            var cppParameter = new CppParameter("param")
            {
                TypeName = "Interface",
                Pointer  = "**"
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("Interface", new CsInterface(null, "Interface"));

            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csParameter = marshalledElementFactory.Create(cppParameter, cppParameter.Name);

            Assert.Equal(CsParameterAttribute.Out, csParameter.Attribute);
        }
    public void ZeroDimensionArrayMappedAsSingleElement()
    {
        var cppMarshallable = new CppParameter("param")
        {
            TypeName       = "int",
            ArrayDimension = "0"
        };

        TypeRegistry.BindType("int", TypeRegistry.Int32);

        var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

        Assert.Equal(TypeRegistry.Int32, csMarshallable.PublicType);
        Assert.Equal(TypeRegistry.Int32, csMarshallable.MarshalType);
        Assert.False(csMarshallable.IsArray);
        Assert.False(csMarshallable.HasPointer);
        Assert.Equal(0, csMarshallable.ArrayDimensionValue);
    }
    public void DoubleVoidPointerParameterPreserved()
    {
        var cppParameter = new CppParameter("param")
        {
            TypeName  = "void",
            Pointer   = "**",
            Attribute = ParamAttribute.Out
        };

        TypeRegistry.BindType("void", TypeRegistry.Void);
        var csParameter = marshalledElementFactory.Create(cppParameter, cppParameter.Name);

        Assert.Equal(TypeRegistry.IntPtr, csParameter.PublicType);
        Assert.Equal(TypeRegistry.IntPtr, csParameter.MarshalType);
        Assert.True(csParameter.HasPointer);
        Assert.False(csParameter.IsArray);
        Assert.True(csParameter.IsOut);
        Assert.Equal(0, csParameter.ArrayDimensionValue);
    }
        public void PointerToTypeWithMarshallingMarshalsAsUnderlyingType()
        {
            var cppMarshallable = new CppParameter("param")
            {
                TypeName = "bool",
                Pointer  = "*"
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("bool", TypeRegistry.Boolean, TypeRegistry.Int32);
            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

            Assert.Equal(TypeRegistry.Int32, csMarshallable.MarshalType);
            Assert.Equal(0, csMarshallable.ArrayDimensionValue);
            Assert.False(csMarshallable.IsArray);
            Assert.True(csMarshallable.HasPointer);
        }
        public void DoubleVoidPointerParameterPreserved()
        {
            var cppParameter = new CppParameter
            {
                TypeName  = "void",
                Pointer   = "**",
                Attribute = ParamAttribute.Out
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("void", typeRegistry.ImportType(typeof(void)));
            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);
            var csParameter = marshalledElementFactory.Create(cppParameter);

            Assert.Equal(typeRegistry.ImportType(typeof(IntPtr)), csParameter.PublicType);
            Assert.Equal(typeRegistry.ImportType(typeof(IntPtr)), csParameter.MarshalType);
            Assert.True(csParameter.HasPointer);
            Assert.True(csParameter.IsOut);
        }
        public void DoublePointerNonInterfaceParameterMappedAsIntPtr()
        {
            var cppParameter = new CppParameter("param")
            {
                TypeName = "int",
                Pointer  = "**"
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("int", TypeRegistry.Int32);
            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);
            var csParameter = marshalledElementFactory.Create(cppParameter, cppParameter.Name);

            Assert.Equal(TypeRegistry.IntPtr, csParameter.PublicType);
            Assert.Equal(TypeRegistry.IntPtr, csParameter.MarshalType);
            Assert.True(csParameter.HasPointer);
            Assert.False(csParameter.IsArray);
            Assert.Equal(0, csParameter.ArrayDimensionValue);
        }
        public void CharPointerMappedToStringMarshalledWithIntPtr()
        {
            var cppMarshallable = new CppParameter("param")
            {
                TypeName = "char",
                Pointer  = "*"
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csMarshallable = marshalledElementFactory.Create(cppMarshallable, cppMarshallable.Name);

            Assert.Equal(TypeRegistry.String, csMarshallable.PublicType);
            Assert.Equal(TypeRegistry.IntPtr, csMarshallable.MarshalType);
            Assert.True(csMarshallable.HasPointer);
            Assert.False(csMarshallable.IsArray);
            Assert.Equal(0, csMarshallable.ArrayDimensionValue);
        }
    public void ParameterWithStructSizeRelationLogsError()
    {
        var cppParameter = new CppParameter("param")
        {
            TypeName = "int",
            Rule     =
            {
                Relation = "struct-size()"
            }
        };

        TypeRegistry.BindType("int", TypeRegistry.Int32);

        using (LoggerMessageCountEnvironment(1, LogLevel.Error))
            using (LoggerMessageCountEnvironment(0, ~LogLevel.Error))
                using (LoggerCodeRequiredEnvironment(LoggingCodes.InvalidRelation))
                {
                    marshalledElementFactory.Create(cppParameter, cppParameter.Name);
                }
    }
        public void BoolToIntArray()
        {
            var cppParameter = new CppParameter("param")
            {
                TypeName  = "bool", Pointer = "*",
                Attribute = ParamAttribute.In | ParamAttribute.Buffer
            };

            var typeRegistry = new TypeRegistry(Logger, A.Fake <IDocumentationLinker>());

            typeRegistry.BindType("bool", TypeRegistry.Boolean, TypeRegistry.UInt8);
            var marshalledElementFactory = new MarshalledElementFactory(Logger, new GlobalNamespaceProvider(), typeRegistry);

            var csParameter = marshalledElementFactory.Create(cppParameter, cppParameter.Name);

            Assert.Equal(TypeRegistry.Boolean, csParameter.PublicType);
            Assert.Equal(TypeRegistry.UInt8, csParameter.MarshalType);
            Assert.True(csParameter.IsArray);
            Assert.True(csParameter.IsBoolToInt);
            Assert.Equal(0, csParameter.ArrayDimensionValue);
        }