Example #1
0
        public override bool VisitParameterDecl(Parameter parameter)
        {
            if (parameter.Usage == ParameterUsage.Unknown || parameter.IsIn)
            {
                return(base.VisitParameterDecl(parameter));
            }

            var ctx = new CSharpMarshalContext(Context.Context)
            {
                ReturnType    = Context.ReturnType,
                ReturnVarName = Context.ReturnVarName
            };

            var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);

            parameter.Type.Visit(marshal);

            if (!string.IsNullOrWhiteSpace(ctx.SupportBefore))
            {
                Context.SupportBefore.WriteLine(ctx.SupportBefore);
            }

            if (!string.IsNullOrWhiteSpace(ctx.Return) &&
                !parameter.Type.IsPrimitiveTypeConvertibleToRef())
            {
                Context.SupportBefore.WriteLine("var _{0} = {1};", parameter.Name,
                                                ctx.Return);
            }

            Context.Return.Write("{0}{1}",
                                 parameter.Type.IsPrimitiveTypeConvertibleToRef() ? "ref *" : "_", parameter.Name);
            return(true);
        }
Example #2
0
        private void MarshalString(Type pointee)
        {
            var marshal = new CSharpMarshalNativeToManagedPrinter(Context);

            Context.ReturnVarName = Context.ArgName;
            Context.ReturnType    = Context.Parameter.QualifiedType;
            pointee.Visit(marshal);
            Context.Cleanup.WriteLine($@"{Context.Parameter.Name} = {
                marshal.Context.Return};");
            Context.Return.StringBuilder.Clear();
        }
Example #3
0
        private void MarshalRefClass(Class @class)
        {
            var method = Context.Function as Method;

            if (method != null &&
                method.Conversion == MethodConversionKind.FunctionToInstanceMethod &&
                Context.ParameterIndex == 0)
            {
                Context.Return.Write("{0}", Helpers.InstanceIdentifier);
                return;
            }

            string param = Context.Parameter.Name;
            Type   type  = Context.Parameter.Type.Desugar();

            if (type.IsAddress())
            {
                Class decl;
                if (type.TryGetClass(out decl) && decl.IsValueType)
                {
                    Context.Return.Write("{0}.{1}", param, Helpers.InstanceIdentifier);
                }
                else
                {
                    Context.Return.Write("{0}{1}.{2}",
                                         method != null && method.OperatorKind == CXXOperatorKind.EqualEqual
                            ? string.Empty
                            : string.Format("ReferenceEquals({0}, null) ? global::System.IntPtr.Zero : ", param),
                                         param,
                                         Helpers.InstanceIdentifier, type);
                }
                return;
            }

            var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier(
                @class.OriginalClass ?? @class);

            Context.Return.Write(
                "ReferenceEquals({0}, null) ? new {1}.Internal() : *({1}.Internal*) ({0}.{2})", param,
                qualifiedIdentifier, Helpers.InstanceIdentifier);
        }
Example #4
0
        private void MarshalRefClass(Class @class)
        {
            var method = Context.Function as Method;

            if (method != null &&
                method.Conversion == MethodConversionKind.FunctionToInstanceMethod &&
                Context.ParameterIndex == 0)
            {
                Context.Return.Write("{0}", Helpers.InstanceIdentifier);
                return;
            }

            if (Context.Parameter.Type.IsPointer() || Context.Parameter.Type.IsReference())
            {
                Context.Return.Write("{0}.{1}", Helpers.SafeIdentifier(Context.Parameter.Name),
                                     Helpers.InstanceIdentifier);
                return;
            }

            Context.Return.Write("*({0}.Internal*){1}.{2}", CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier(@class),
                                 Helpers.SafeIdentifier(Context.Parameter.Name), Helpers.InstanceIdentifier);
        }
Example #5
0
        private void MarshalRefClass(Class @class)
        {
            var method = Context.Function as Method;

            if (method != null &&
                method.Conversion == MethodConversionKind.FunctionToInstanceMethod &&
                Context.ParameterIndex == 0)
            {
                Context.Return.Write("{0}", Helpers.InstanceIdentifier);
                return;
            }

            string param = Helpers.SafeIdentifier(Context.Parameter.Name);
            Type   type  = Context.Parameter.Type.Desugar();

            if (type.IsAddress())
            {
                Class decl;
                if (type.IsTagDecl(out decl) && decl.IsValueType)
                {
                    Context.Return.Write("{0}.{1}", param, Helpers.InstanceIdentifier);
                }
                else
                {
                    Context.Return.Write("{0} == ({2}) null ? global::System.IntPtr.Zero : {0}.{1}", param,
                                         Helpers.InstanceIdentifier, type);
                }
                return;
            }

            var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier(
                @class.OriginalClass ?? @class);

            Context.Return.Write(
                "{1} == ({0}) null ? new {0}.Internal() : *({0}.Internal*) ({1}.{2})", qualifiedIdentifier,
                param, Helpers.InstanceIdentifier);
        }
Example #6
0
 public static void CSharpMarshalToManaged(this ITypedDecl decl,
                                           CSharpMarshalNativeToManagedPrinter printer)
 {
     CSharpMarshalToManaged(decl.QualifiedType, printer);
 }
Example #7
0
 public static void CSharpMarshalToManaged(this Type type,
                                           CSharpMarshalNativeToManagedPrinter printer)
 {
     CSharpMarshalToManaged(new QualifiedType(type), printer);
 }
Example #8
0
 public static void CSharpMarshalToManaged(this QualifiedType type,
                                           CSharpMarshalNativeToManagedPrinter printer)
 {
     printer.Context.FullType = type;
     type.Visit(printer);
 }
Example #9
0
 public static void CSharpMarshalToManaged(this ITypedDecl decl,
     CSharpMarshalNativeToManagedPrinter printer)
 {
     CSharpMarshalToManaged(decl.QualifiedType, printer);
 }
Example #10
0
 public static void CSharpMarshalToManaged(this Type type,
     CSharpMarshalNativeToManagedPrinter printer)
 {
     CSharpMarshalToManaged(new QualifiedType(type), printer);
 }
Example #11
0
 public static void CSharpMarshalToManaged(this QualifiedType type,
     CSharpMarshalNativeToManagedPrinter printer)
 {
     (printer.Context as CSharpMarshalContext).FullType = type;
     type.Visit(printer);
 }
Example #12
0
        public override bool VisitParameterDecl(Parameter parameter)
        {
            if (parameter.Usage == ParameterUsage.Unknown || parameter.IsIn)
                return base.VisitParameterDecl(parameter);

            var ctx = new CSharpMarshalContext(Context.Driver)
            {
                ReturnType = Context.ReturnType,
                ReturnVarName = Context.ReturnVarName
            };

            var marshal = new CSharpMarshalNativeToManagedPrinter(ctx);
            parameter.Type.Visit(marshal);

            if (!string.IsNullOrWhiteSpace(ctx.SupportBefore))
                Context.SupportBefore.WriteLine(ctx.SupportBefore);

            if (!string.IsNullOrWhiteSpace(ctx.Return) &&
                !parameter.Type.IsPrimitiveTypeConvertibleToRef())
            {
                Context.SupportBefore.WriteLine("var _{0} = {1};", parameter.Name,
                    ctx.Return);
            }

            Context.Return.Write("{0}{1}",
                parameter.Type.IsPrimitiveTypeConvertibleToRef() ? "ref *" : "_", parameter.Name);
            return true;
        }