コード例 #1
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var pointee = pointer.Pointee.Desugar();

            if (CSharpTypePrinter.IsConstCharString(pointer))
            {
                Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName,
                                                            pointer.Pointee.Desugar() as BuiltinType));
                return(true);
            }

            PrimitiveType primitive;

            if (pointee.IsPrimitiveType(out primitive) || pointee.IsEnumType())
            {
                var param = Context.Parameter;
                if (param != null && (param.IsOut || param.IsInOut))
                {
                    Context.Return.Write("_{0}", param.Name);
                    return(true);
                }

                Context.Return.Write(Context.ReturnVarName);
                return(true);
            }

            return(pointer.Pointee.Visit(this, quals));
        }
コード例 #2
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var pointee = pointer.Pointee;

            if (CSharpTypePrinter.IsConstCharString(pointer))
            {
                Context.Return.Write("Marshal.PtrToStringAnsi({0})",
                                     Context.ReturnVarName);
                return(true);
            }

            PrimitiveType primitive;

            if (pointee.Desugar().IsPrimitiveType(out primitive))
            {
                Context.Return.Write(Context.ReturnVarName);
                return(true);
            }

            if (!pointee.Visit(this, quals))
            {
                return(false);
            }

            return(true);
        }
コード例 #3
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var param      = Context.Parameter;
            var isRefParam = param != null && (param.IsInOut || param.IsOut);

            var  pointee = pointer.Pointee.Desugar();
            bool marshalPointeeAsString = CSharpTypePrinter.IsConstCharString(pointee) && isRefParam;

            if ((CSharpTypePrinter.IsConstCharString(pointer) && !MarshalsParameter) ||
                marshalPointeeAsString)
            {
                Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName,
                                                            pointer.GetFinalPointee().Desugar() as BuiltinType));
                return(true);
            }

            var           finalPointee = pointer.GetFinalPointee();
            PrimitiveType primitive;

            if (finalPointee.IsPrimitiveType(out primitive) || finalPointee.IsEnumType())
            {
                if (isRefParam)
                {
                    Context.Return.Write("_{0}", param.Name);
                    return(true);
                }

                if (Context.Context.Options.MarshalCharAsManagedChar &&
                    primitive == PrimitiveType.Char)
                {
                    Context.Return.Write($"({pointer}) ");
                }

                var type = Context.ReturnType.Type.Desugar(
                    resolveTemplateSubstitution: false);
                if (Context.Function != null &&
                    Context.Function.OperatorKind == CXXOperatorKind.Subscript &&
                    type.IsPrimitiveType(primitive))
                {
                    var substitute = type as TemplateParameterSubstitutionType;
                    if (substitute != null)
                    {
                        Context.Return.Write($@"({
                            substitute.ReplacedParameter.Parameter.Name}) (object) ");
                    }
                    Context.Return.Write("*");
                }

                Context.Return.Write(Context.ReturnVarName);
                return(true);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
コード例 #4
0
ファイル: CSharpMarshal.cs プロジェクト: wuscier/CppSharp
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var param      = Context.Parameter;
            var isRefParam = param != null && (param.IsInOut || param.IsOut);

            var  pointee = pointer.Pointee.Desugar();
            bool marshalPointeeAsString = CSharpTypePrinter.IsConstCharString(pointee) && isRefParam;

            if ((CSharpTypePrinter.IsConstCharString(pointer) && !MarshalsParameter) ||
                marshalPointeeAsString)
            {
                Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName,
                                                            pointer.GetFinalPointee().Desugar() as BuiltinType));
                return(true);
            }

            var           finalPointee = pointer.GetFinalPointee();
            PrimitiveType primitive;

            if (finalPointee.IsPrimitiveType(out primitive) || finalPointee.IsEnumType())
            {
                if (isRefParam)
                {
                    Context.Return.Write("_{0}", param.Name);
                    return(true);
                }

                if (Context.Context.Options.MarshalCharAsManagedChar && primitive == PrimitiveType.Char)
                {
                    Context.Return.Write(string.Format("({0}) ", pointer));
                }
                Context.Return.Write(Context.ReturnVarName);
                return(true);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
コード例 #5
0
ファイル: CSharpMarshal.cs プロジェクト: wuscier/CppSharp
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var pointee = pointer.Pointee.Desugar();

            if (Context.Function != null && pointer.IsPrimitiveTypeConvertibleToRef() &&
                Context.Kind != MarshalKind.VTableReturnValue)
            {
                var refParamPtr          = string.Format("__refParamPtr{0}", Context.ParameterIndex);
                var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType;
                if (templateSubstitution != null)
                {
                    var castParam = $"__{Context.Parameter.Name}{Context.ParameterIndex}";
                    Context.SupportBefore.WriteLine(
                        $"var {castParam} = ({templateSubstitution}) (object) {Context.Parameter.Name};");
                    Context.SupportBefore.WriteLine($"{pointer} {refParamPtr} = &{castParam};");
                    Context.Return.Write(refParamPtr);
                }
                else
                {
                    Context.SupportBefore.WriteLine(
                        $"fixed ({pointer} {refParamPtr} = &{Context.Parameter.Name})");
                    Context.HasCodeBlock = true;
                    Context.SupportBefore.WriteStartBraceIndent();
                    Context.Return.Write(refParamPtr);
                }
                return(true);
            }

            var param      = Context.Parameter;
            var isRefParam = param != null && (param.IsInOut || param.IsOut);

            if (CSharpTypePrinter.IsConstCharString(pointee) && isRefParam)
            {
                if (param.IsOut)
                {
                    Context.Return.Write("IntPtr.Zero");
                    Context.ArgumentPrefix.Write("&");
                }
                else if (param.IsInOut)
                {
                    Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
                    Context.ArgumentPrefix.Write("&");
                }
                else
                {
                    Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
                    Context.Cleanup.WriteLine("Marshal.FreeHGlobal({0});", Context.ArgName);
                }
                return(true);
            }

            if (pointee is FunctionType)
            {
                var function = pointee as FunctionType;
                return(VisitDelegateType(function, function.ToString()));
            }

            Class @class;

            if (pointee.TryGetClass(out @class) && @class.IsValueType)
            {
                if (Context.Parameter.Usage == ParameterUsage.Out)
                {
                    var qualifiedIdentifier = (@class.OriginalClass ?? @class).Visit(typePrinter);
                    Context.SupportBefore.WriteLine("var {0} = new {1}.{2}();",
                                                    Generator.GeneratedIdentifier(Context.ArgName), qualifiedIdentifier,
                                                    Helpers.InternalStruct);
                }
                else
                {
                    Context.SupportBefore.WriteLine("var {0} = {1}.{2};",
                                                    Generator.GeneratedIdentifier(Context.ArgName),
                                                    Context.Parameter.Name,
                                                    Helpers.InstanceIdentifier);
                }

                Context.Return.Write("new global::System.IntPtr(&{0})",
                                     Generator.GeneratedIdentifier(Context.ArgName));
                return(true);
            }

            var           marshalAsString = CSharpTypePrinter.IsConstCharString(pointer);
            var           finalPointee    = pointer.GetFinalPointee();
            PrimitiveType primitive;

            if (finalPointee.IsPrimitiveType(out primitive) || finalPointee.IsEnumType() ||
                marshalAsString)
            {
                // From MSDN: "note that a ref or out parameter is classified as a moveable
                // variable". This means we must create a local variable to hold the result
                // and then assign this value to the parameter.

                if (isRefParam)
                {
                    var typeName = Type.TypePrinterDelegate(finalPointee);

                    if (param.IsInOut)
                    {
                        Context.SupportBefore.WriteLine("{0} _{1} = {1};", typeName, param.Name);
                    }
                    else
                    {
                        Context.SupportBefore.WriteLine("{0} _{1};", typeName, param.Name);
                    }

                    Context.Return.Write("&_{0}", param.Name);
                }
                else
                {
                    if (!marshalAsString &&
                        Context.Context.Options.MarshalCharAsManagedChar &&
                        primitive == PrimitiveType.Char)
                    {
                        typePrinter.PushContext(TypePrinterContextKind.Native);
                        Context.Return.Write(string.Format("({0}) ", pointer.Visit(typePrinter)));
                        typePrinter.PopContext();
                    }
                    if (marshalAsString && (Context.Kind == MarshalKind.NativeField ||
                                            Context.Kind == MarshalKind.VTableReturnValue ||
                                            Context.Kind == MarshalKind.Variable))
                    {
                        Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
                    }
                    else
                    {
                        Context.Return.Write(Context.Parameter.Name);
                    }
                }

                return(true);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
コード例 #6
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var param      = Context.Parameter;
            var isRefParam = param != null && (param.IsInOut || param.IsOut);

            var  pointee = pointer.Pointee.Desugar();
            bool marshalPointeeAsString = CSharpTypePrinter.IsConstCharString(pointee) && isRefParam;

            if (CSharpTypePrinter.IsConstCharString(pointer) || marshalPointeeAsString)
            {
                if (param.IsOut)
                {
                    Context.Return.Write("IntPtr.Zero");
                    CSharpContext.ArgumentPrefix.Write("&");
                }
                else if (param.IsInOut)
                {
                    Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
                    CSharpContext.ArgumentPrefix.Write("&");
                }
                else
                {
                    Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
                    CSharpContext.Cleanup.WriteLine("Marshal.FreeHGlobal({0});", Context.ArgName);
                }
                return(true);
            }

            if (pointee is FunctionType)
            {
                var function = pointee as FunctionType;
                return(VisitDelegateType(function, function.ToString()));
            }

            Class @class;

            if (pointee.TryGetClass(out @class) && @class.IsValueType)
            {
                if (Context.Parameter.Usage == ParameterUsage.Out)
                {
                    Context.SupportBefore.WriteLine("var {0} = new {1}.Internal();",
                                                    Generator.GeneratedIdentifier(Context.ArgName), @class.Name);
                }
                else
                {
                    Context.SupportBefore.WriteLine("var {0} = {1}.{2};",
                                                    Generator.GeneratedIdentifier(Context.ArgName),
                                                    Context.Parameter.Name,
                                                    Helpers.InstanceIdentifier);
                }

                Context.Return.Write("new global::System.IntPtr(&{0})",
                                     Generator.GeneratedIdentifier(Context.ArgName));
                return(true);
            }

            var           finalPointee = pointer.GetFinalPointee();
            PrimitiveType primitive;

            if (finalPointee.IsPrimitiveType(out primitive) || finalPointee.IsEnumType())
            {
                // From MSDN: "note that a ref or out parameter is classified as a moveable
                // variable". This means we must create a local variable to hold the result
                // and then assign this value to the parameter.

                if (isRefParam)
                {
                    var typeName = Type.TypePrinterDelegate(finalPointee);

                    if (param.IsInOut)
                    {
                        Context.SupportBefore.WriteLine("{0} _{1} = {1};", typeName, param.Name);
                    }
                    else
                    {
                        Context.SupportBefore.WriteLine("{0} _{1};", typeName, param.Name);
                    }

                    Context.Return.Write("&_{0}", param.Name);
                }
                else
                {
                    Context.Return.Write(Context.Parameter.Name);
                }

                return(true);
            }

            return(pointer.Pointee.Visit(this, quals));
        }