Exemplo n.º 1
0
        public override void CLIMarshalToManaged(MarshalContext ctx)
        {
            var type = Type as TemplateSpecializationType;

            Class @class;
            if (!type.Arguments[0].Type.Type.IsTagDecl(out @class))
                return;

            var instance = string.Format("{0}.get()", ctx.ReturnVarName);
            ctx.MarshalToManaged.WriteClassInstance(@class, instance);
        }
Exemplo n.º 2
0
        private void GeneratePropertyGetter <T>(T decl, Class @class, string name, Type type)
            where T : Declaration, ITypedDecl
        {
            if (decl == null)
            {
                return;
            }

            var method    = decl as Method;
            var isIndexer = method != null &&
                            method.OperatorKind == CXXOperatorKind.Subscript;

            var args = new List <string>();

            if (isIndexer)
            {
                var indexParameter = method.Parameters[0];
                args.Add(string.Format("{0} {1}", indexParameter.Type, indexParameter.Name));
            }

            WriteLine("{0} {1}::{2}::get({3})", type, QualifiedIdentifier(@class),
                      name, string.Join(", ", args));

            WriteStartBraceIndent();

            if (decl is Function)
            {
                var func = decl as Function;
                if (isIndexer && func.Type.IsAddress())
                {
                    GenerateFunctionCall(func, @class, type);
                }
                else
                {
                    GenerateFunctionCall(func, @class);
                }
            }
            else
            {
                if (@class.IsValueType && decl is Field)
                {
                    WriteLine("return {0};", decl.Name);
                    WriteCloseBraceIndent();
                    NewLine();
                    return;
                }
                string variable;
                if (decl is Variable)
                {
                    variable = string.Format("::{0}::{1}",
                                             @class.QualifiedOriginalName, decl.OriginalName);
                }
                else
                {
                    variable = string.Format("((::{0}*)NativePtr)->{1}",
                                             @class.QualifiedOriginalName, decl.OriginalName);
                }

                var ctx = new MarshalContext(Driver)
                {
                    Declaration   = decl,
                    ArgName       = decl.Name,
                    ReturnVarName = variable,
                    ReturnType    = decl.QualifiedType
                };

                var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
                decl.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                {
                    Write(marshal.Context.SupportBefore);
                }

                WriteLine("return {0};", marshal.Context.Return);
            }


            WriteCloseBraceIndent();
            NewLine();
        }
Exemplo n.º 3
0
        private ParamMarshal GenerateFunctionParamMarshal(Parameter param, int paramIndex,
                                                          Function function = null)
        {
            var paramMarshal = new ParamMarshal {
                Name = param.Name, Param = param
            };

            if (param.Type is BuiltinType)
            {
                return(paramMarshal);
            }

            var argName = "arg" + paramIndex.ToString(CultureInfo.InvariantCulture);

            var isRef = param.IsOut || param.IsInOut;
            // Since both pointers and references to types are wrapped as CLI
            // tracking references when using in/out, we normalize them here to be able
            // to use the same code for marshaling.
            var paramType = param.Type;

            if (paramType is PointerType && isRef)
            {
                if (!paramType.IsReference())
                {
                    paramMarshal.Prefix = "&";
                }
                paramType = (paramType as PointerType).Pointee;
            }

            var effectiveParam = new Parameter(param)
            {
                QualifiedType = new QualifiedType(paramType)
            };

            var ctx = new MarshalContext(Driver)
            {
                Parameter      = effectiveParam,
                ParameterIndex = paramIndex,
                ArgName        = argName,
                Function       = function
            };

            var marshal = new CLIMarshalManagedToNativePrinter(ctx);

            effectiveParam.Visit(marshal);

            if (string.IsNullOrEmpty(marshal.Context.Return))
            {
                throw new Exception(string.Format("Cannot marshal argument of function '{0}'",
                                                  function.QualifiedOriginalName));
            }

            if (isRef)
            {
                var typePrinter = new CppTypePrinter();
                var type        = paramType.Visit(typePrinter);

                if (param.IsInOut)
                {
                    if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                    {
                        Write(marshal.Context.SupportBefore);
                    }

                    WriteLine("{0} {1} = {2};", type, argName, marshal.Context.Return);
                }
                else
                {
                    WriteLine("{0} {1};", type, argName);
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                {
                    Write(marshal.Context.SupportBefore);
                }

                WriteLine("auto {0}{1} = {2};", marshal.VarPrefix, argName,
                          marshal.Context.Return);
                paramMarshal.Prefix = marshal.ArgumentPrefix;
            }

            paramMarshal.Name = argName;
            return(paramMarshal);
        }
Exemplo n.º 4
0
 public CLIMarshalNativeToManagedPrinter(MarshalContext marshalContext)
     : base(marshalContext)
 {
     Context.MarshalToManaged = this;
 }
Exemplo n.º 5
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write(ctx.Parameter.Name);
 }
Exemplo n.º 6
0
        private ParamMarshal GenerateFunctionParamMarshal(Parameter param, int paramIndex,
                                                          Function function = null)
        {
            var paramMarshal = new ParamMarshal {
                Name = param.Name, Param = param
            };

            if (param.Type is BuiltinType)
            {
                return(paramMarshal);
            }

            var argName = Generator.GeneratedIdentifier("arg") + paramIndex.ToString(CultureInfo.InvariantCulture);

            Parameter effectiveParam = param;
            var       isRef          = param.IsOut || param.IsInOut;
            var       paramType      = param.Type;

            var ctx = new MarshalContext(Context, CurrentIndentation)
            {
                Parameter      = effectiveParam,
                ParameterIndex = paramIndex,
                ArgName        = argName,
                Function       = function
            };

            var marshal = new CppMarshalManagedToNativePrinter(ctx);

            effectiveParam.Visit(marshal);

            if (string.IsNullOrEmpty(marshal.Context.Return))
            {
                throw new Exception($"Cannot marshal argument of function '{function.QualifiedOriginalName}'");
            }

            if (isRef)
            {
                var type = paramType.Visit(CTypePrinter);

                if (param.IsInOut)
                {
                    if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                    {
                        Write(marshal.Context.Before);
                    }

                    WriteLine($"{type} {argName} = {marshal.Context.Return};");
                }
                else
                {
                    WriteLine($"{type} {argName};");
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                {
                    Write(marshal.Context.Before);
                }

                WriteLine($"auto {marshal.VarPrefix}{argName} = {marshal.Context.Return};");
                paramMarshal.Prefix = marshal.ArgumentPrefix;
            }

            paramMarshal.Name = argName;
            return(paramMarshal);
        }
Exemplo n.º 7
0
        public override void CSharpMarshalToNative(MarshalContext ctx)
        {
            var type = Type as TemplateSpecializationType;

            ctx.Return.Write("{0}._Instance", ctx.Parameter.Name);
        }
Exemplo n.º 8
0
 public override void CLIMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("(HandleId){0}.Id", ctx.Parameter.Name);
 }
Exemplo n.º 9
0
 public virtual void CSharpMarshalCopyCtorToManaged(MarshalContext ctx)
 {
 }
Exemplo n.º 10
0
 public override MarshalPrinter <MarshalContext> GetMarshalNativeToManagedPrinter(MarshalContext ctx)
 {
     return(new QuickJSMarshalNativeToManagedPrinter(ctx));
 }
Exemplo n.º 11
0
 public override MarshalPrinter <MarshalContext, CppTypePrinter> GetMarshalManagedToNativePrinter(MarshalContext ctx)
 {
     return(new QuickJSMarshalManagedToNativePrinter(ctx));
 }
Exemplo n.º 12
0
        private void GenerateEventInvoke(Event @event)
        {
            var invokeId = $"event_invoke_{@event.OriginalName}";

            PushBlock();
            {
                CTypePrinter.PushContext(TypePrinterContextKind.Native);
                var functionType = @event.Type as FunctionType;
                var retType      = functionType.ReturnType.Visit(CTypePrinter);
                CTypePrinter.PopContext();

                Write($"{retType} {invokeId}(");

                var @params = new List <string>();
                foreach (var param in @event.Parameters)
                {
                    CTypePrinter.PushContext(TypePrinterContextKind.Native);
                    var paramType = param.Type.Visit(CTypePrinter);
                    CTypePrinter.PopContext();

                    @params.Add($"{paramType} {param.Name}");
                }

                Write(string.Join(", ", @params));
                WriteLine(")");
                WriteOpenBraceAndIndent();

                WriteLine($"JSValue event = JS_Interop_FindEvent(&events, {@event.GlobalId});");
                WriteLine($"if (JS_IsUndefined(event))");

                var isVoidReturn = functionType.ReturnType.Type.IsPrimitiveType(PrimitiveType.Void);
                if (isVoidReturn)
                {
                    WriteLineIndent($"return;");
                }
                else
                {
                    var defaultValuePrinter = new CppDefaultValuePrinter(Context);
                    var defaultValue        = functionType.ReturnType.Visit(defaultValuePrinter);
                    WriteLineIndent($"return {defaultValue};");
                }
                NewLine();

                // Marshal the arguments.
                var marshalers = new List <MarshalPrinter <MarshalContext, CppTypePrinter> >();
                foreach (var param in @event.Parameters)
                {
                    var ctx = new MarshalContext(Context, CurrentIndentation)
                    {
                        ArgName       = param.Name,
                        ReturnVarName = param.Name,
                        ReturnType    = param.QualifiedType,
                        Parameter     = param
                    };

                    var marshal = GetMarshalNativeToManagedPrinter(ctx);
                    marshalers.Add(marshal);

                    param.Visit(marshal);

                    if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                    {
                        Write(marshal.Context.Before);
                    }
                }

                var args = marshalers.Select(m => m.Context.Return.ToString());
                WriteLine($"JSValueConst argv[] = {{ { string.Join(", ", args)} }};");
                WriteLine($"auto data = (JS_SignalContext*) JS_GetOpaque(event, 0);");
                WriteLine($"JSValue ret = JS_Call(ctx, data->function, JS_UNDEFINED, {@event.Parameters.Count}, argv);");
                WriteLine($"JS_FreeValue(ctx, ret);");

                //WriteLine($"{@class.QualifiedOriginalName}* instance = data->instance;");

                /*
                 *
                 *              if (!isVoidReturn)
                 *              {
                 *                  CTypePrinter.PushContext(TypePrinterContextKind.Native);
                 *                  var returnType = function.ReturnType.Visit(CTypePrinter);
                 *                  CTypePrinter.PopContext();
                 *
                 *                  Write($"{returnType} {Helpers.ReturnIdentifier} = ");
                 *              }
                 *
                 *              var @class = function.Namespace as Class;
                 */

                UnindentAndWriteCloseBrace();
            }
            PopBlock(NewLineKind.BeforeNextBlock);
        }
Exemplo n.º 13
0
 public override void CLIMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write("{0}({1}.id)", CLISignature(null), ctx.ReturnVarName);
 }
Exemplo n.º 14
0
 public override void CLIMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("(HandleId){0}.Id", ctx.Parameter.Name);
 }
Exemplo n.º 15
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("IntPtr.Zero");
 }
Exemplo n.º 16
0
 public CLIMarshalNativeToManagedPrinter(MarshalContext marshalContext)
     : base(marshalContext)
 {
     Context.MarshalToManaged = this;
 }
Exemplo n.º 17
0
        private ParamMarshal GenerateFunctionParamMarshal(Parameter param, int paramIndex,
                                                          Function function = null)
        {
            var paramMarshal = new ParamMarshal {
                Name = param.Name, Param = param
            };

            if (param.Type is BuiltinType)
            {
                return(paramMarshal);
            }

            var argName = Generator.GeneratedIdentifier("arg") + paramIndex.ToString(CultureInfo.InvariantCulture);

            var isRef = param.IsOut || param.IsInOut;

            var paramType = param.Type;

            // Get actual type if the param type is a typedef but not a function type because function types have to be typedef.
            // We need to get the actual type this early before we visit any marshalling code to ensure we hit the marshalling
            // logic for the actual type and not the typedef.
            // This fixes issues where typedefs to primitive pointers are involved.
            FunctionType functionType;
            var          paramTypeAsTypedef = paramType as TypedefType;

            if (paramTypeAsTypedef != null && !paramTypeAsTypedef.Declaration.Type.IsPointerTo(out functionType))
            {
                paramType = param.Type.Desugar();
            }

            // Since both pointers and references to types are wrapped as CLI
            // tracking references when using in/out, we normalize them here to be able
            // to use the same code for marshaling.
            if (paramType is PointerType && isRef)
            {
                if (!paramType.IsReference())
                {
                    paramMarshal.Prefix = "&";
                }
                paramType = (paramType as PointerType).Pointee;
            }

            var effectiveParam = new Parameter(param)
            {
                QualifiedType = new QualifiedType(paramType)
            };

            var ctx = new MarshalContext(Context, CurrentIndentation)
            {
                Parameter      = effectiveParam,
                ParameterIndex = paramIndex,
                ArgName        = argName,
                Function       = function
            };

            var marshal = new CLIMarshalManagedToNativePrinter(ctx);

            effectiveParam.Visit(marshal);

            if (string.IsNullOrEmpty(marshal.Context.Return))
            {
                throw new Exception($"Cannot marshal argument of function '{function.QualifiedOriginalName}'");
            }

            if (isRef)
            {
                var typePrinter = new CppTypePrinter(Context)
                {
                    ResolveTypeMaps = false
                };
                var type = paramType.Visit(typePrinter);

                if (param.IsInOut)
                {
                    if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                    {
                        Write(marshal.Context.Before);
                    }

                    WriteLine("{0} {1} = {2};", type, argName, marshal.Context.Return);
                }
                else
                {
                    WriteLine("{0} {1};", type, argName);
                }
            }
            else
            {
                if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                {
                    Write(marshal.Context.Before);
                }

                WriteLine("auto {0}{1} = {2};", marshal.VarPrefix, argName,
                          marshal.Context.Return);
                paramMarshal.Prefix = marshal.ArgumentPrefix;
            }

            paramMarshal.Name = argName;
            return(paramMarshal);
        }
Exemplo n.º 18
0
 public virtual void CLIMarshalToNative(MarshalContext ctx)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 19
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("new Std.WString()");
 }
Exemplo n.º 20
0
        public void GenerateFunctionCall(Function function)
        {
            var @params = GenerateFunctionParamsMarshal(function.Parameters, function);

            var needsReturn = !function.ReturnType.Type.IsPrimitiveType(PrimitiveType.Void);

            if (needsReturn)
            {
                CTypePrinter.PushContext(TypePrinterContextKind.Native);
                var returnType = function.ReturnType.Visit(CTypePrinter);
                CTypePrinter.PopContext();

                Write($"{returnType} {Helpers.ReturnIdentifier} = ");
            }

            var method = function as Method;
            var @class = function.Namespace as Class;

            var property = method?.AssociatedDeclaration as Property;
            var field    = property?.Field;

            if (field != null)
            {
                Write($"((::{@class.QualifiedOriginalName}*){Helpers.InstanceIdentifier})->");
                Write($"{field.OriginalName}");

                var isGetter = property.GetMethod == method;
                if (isGetter)
                {
                    WriteLine(";");
                }
                else
                {
                    WriteLine($" = {@params[0].Name};");
                }
            }
            else
            {
                if (IsNativeFunctionOrStaticMethod(function))
                {
                    Write($"::{function.QualifiedOriginalName}(");
                }
                else
                {
                    if (IsNativeMethod(function))
                    {
                        Write($"((::{@class.QualifiedOriginalName}*){Helpers.InstanceIdentifier})->");
                    }

                    Write($"{base.GetMethodIdentifier(function, TypePrinterContextKind.Native)}(");
                }

                GenerateFunctionParams(@params);
                WriteLine(");");
            }

            foreach (var paramInfo in @params)
            {
                var param = paramInfo.Param;
                if (param.Usage != ParameterUsage.Out && param.Usage != ParameterUsage.InOut)
                {
                    continue;
                }

                if (param.Type.IsPointer() && !param.Type.GetFinalPointee().IsPrimitiveType())
                {
                    param.QualifiedType = new QualifiedType(param.Type.GetFinalPointee());
                }

                var nativeVarName = paramInfo.Name;

                var ctx = new MarshalContext(Context, CurrentIndentation)
                {
                    ArgName       = nativeVarName,
                    ReturnVarName = nativeVarName,
                    ReturnType    = param.QualifiedType
                };

                var marshal = new CppMarshalNativeToManagedPrinter(ctx);
                param.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                {
                    Write(marshal.Context.Before);
                }

                WriteLine($"{param.Name} = {marshal.Context.Return};");
            }

            if (needsReturn)
            {
                var ctx = new MarshalContext(Context, CurrentIndentation)
                {
                    ArgName       = Helpers.ReturnIdentifier,
                    ReturnVarName = Helpers.ReturnIdentifier,
                    ReturnType    = function.ReturnType
                };

                var marshal = new CppMarshalNativeToManagedPrinter(ctx);
                function.ReturnType.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                {
                    Write(marshal.Context.Before);
                }

                WriteLine($"return {marshal.Context.Return};");
            }
        }
Exemplo n.º 21
0
        public override void CLIMarshalToNative(MarshalContext ctx)
        {
            var templateType         = Type as TemplateSpecializationType;
            var type                 = templateType.Arguments[0].Type;
            var isPointerToPrimitive = type.Type.IsPointerToPrimitiveType();
            var managedType          = isPointerToPrimitive
                ? new CILType(typeof(System.IntPtr))
                : type.Type;

            var entryString = (ctx.Parameter != null) ? ctx.Parameter.Name
                : ctx.ArgName;

            var tmpVarName = "_tmp" + entryString;

            var cppTypePrinter = new CppTypePrinter();
            var nativeType     = type.Type.Visit(cppTypePrinter);

            ctx.Before.WriteLine("auto {0} = std::vector<{1}>();",
                                 tmpVarName, nativeType);
            ctx.Before.WriteLine("for each({0} _element in {1})",
                                 managedType, entryString);
            ctx.Before.WriteOpenBraceAndIndent();
            {
                var param = new Parameter
                {
                    Name          = "_element",
                    QualifiedType = type
                };

                var elementCtx = new MarshalContext(ctx.Context, ctx.Indentation)
                {
                    Parameter = param,
                    ArgName   = param.Name,
                };

                var marshal = new CLIMarshalManagedToNativePrinter(elementCtx);
                type.Type.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.Before))
                {
                    ctx.Before.Write(marshal.Context.Before);
                }

                if (isPointerToPrimitive)
                {
                    ctx.Before.WriteLine("auto _marshalElement = {0}.ToPointer();",
                                         marshal.Context.Return);
                }
                else
                {
                    ctx.Before.WriteLine("auto _marshalElement = {0};",
                                         marshal.Context.Return);
                }

                ctx.Before.WriteLine("{0}.push_back(_marshalElement);",
                                     tmpVarName);
            }

            ctx.Before.UnindentAndWriteCloseBrace();

            ctx.Return.Write(tmpVarName);
        }
Exemplo n.º 22
0
 public override void CSharpMarshalToNative(MarshalContext ctx)
 {
     // pointless, put just so that the generated code compiles
     ctx.Return.Write("new QList.Internal()");
 }
Exemplo n.º 23
0
 public override void CLIMarshalToManaged(MarshalContext ctx)
 {
     throw new System.NotImplementedException();
 }
Exemplo n.º 24
0
 public override void CSharpMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write(ctx.ReturnVarName);
 }
Exemplo n.º 25
0
 public override void CLIMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("::TypeMappedIndex()");
 }
Exemplo n.º 26
0
        public void GenerateFunctionCall(Function function, Class @class = null, Type publicRetType = null)
        {
            CheckArgumentRange(function);

            if (function.OperatorKind == CXXOperatorKind.EqualEqual ||
                function.OperatorKind == CXXOperatorKind.ExclaimEqual)
            {
                WriteLine("bool {0}Null = ReferenceEquals({0}, nullptr);",
                          function.Parameters[0].Name);
                WriteLine("bool {0}Null = ReferenceEquals({0}, nullptr);",
                          function.Parameters[1].Name);
                WriteLine("if ({0}Null || {1}Null)",
                          function.Parameters[0].Name, function.Parameters[1].Name);
                WriteLineIndent("return {0}{1}Null && {2}Null{3};",
                                function.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : "!(",
                                function.Parameters[0].Name, function.Parameters[1].Name,
                                function.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : ")");
            }

            var retType = function.ReturnType;

            if (publicRetType == null)
            {
                publicRetType = retType.Type;
            }
            var needsReturn = !retType.Type.IsPrimitiveType(PrimitiveType.Void);

            const string valueMarshalName = "_this0";
            var          isValueType      = @class != null && @class.IsValueType;

            if (isValueType && !IsNativeFunctionOrStaticMethod(function))
            {
                WriteLine("auto {0} = ::{1}();", valueMarshalName, @class.QualifiedOriginalName);

                var param = new Parameter {
                    Name = "(*this)"
                };
                var ctx = new MarshalContext(Driver)
                {
                    MarshalVarPrefix = valueMarshalName,
                    Parameter        = param
                };

                var marshal = new CLIMarshalManagedToNativePrinter(ctx);
                marshal.MarshalValueClassProperties(@class, valueMarshalName);

                if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                {
                    Write(marshal.Context.SupportBefore);
                }
            }

            var @params = GenerateFunctionParamsMarshal(function.Parameters, function);

            var returnIdentifier = Helpers.ReturnIdentifier;

            if (needsReturn)
            {
                if (retType.Type.IsReference())
                {
                    Write("auto &{0} = ", returnIdentifier);
                }
                else
                {
                    Write("auto {0} = ", returnIdentifier);
                }
            }

            if (function.OperatorKind == CXXOperatorKind.Conversion ||
                function.OperatorKind == CXXOperatorKind.ExplicitConversion)
            {
                var method      = function as Method;
                var typePrinter = new CppTypePrinter();
                var typeName    = method.ConversionType.Visit(typePrinter);
                WriteLine("({0}) {1};", typeName, @params[0].Name);
            }
            else if (function.IsOperator &&
                     function.OperatorKind != CXXOperatorKind.Subscript)
            {
                var opName = function.Name.Replace("operator", "").Trim();

                switch (Operators.ClassifyOperator(function))
                {
                case CXXOperatorArity.Unary:
                    WriteLine("{0} {1};", opName, @params[0].Name);
                    break;

                case CXXOperatorArity.Binary:
                    WriteLine("{0} {1} {2};", @params[0].Name, opName, @params[1].Name);
                    break;
                }
            }
            else
            {
                if (IsNativeFunctionOrStaticMethod(function))
                {
                    Write("::{0}(", function.QualifiedOriginalName);
                }
                else
                {
                    if (isValueType)
                    {
                        Write("{0}.", valueMarshalName);
                    }
                    else if (IsNativeMethod(function))
                    {
                        Write("((::{0}*)NativePtr)->", @class.QualifiedOriginalName);
                    }
                    Write("{0}(", function.OriginalName);
                }

                GenerateFunctionParams(@params);
                WriteLine(");");
            }

            foreach (var paramInfo in @params)
            {
                var param = paramInfo.Param;
                if (param.Usage != ParameterUsage.Out && param.Usage != ParameterUsage.InOut)
                {
                    continue;
                }

                if (param.Type.IsPointer() && !param.Type.GetFinalPointee().IsPrimitiveType())
                {
                    param.QualifiedType = new QualifiedType(param.Type.GetFinalPointee());
                }

                var nativeVarName = paramInfo.Name;

                var ctx = new MarshalContext(Driver)
                {
                    ArgName       = nativeVarName,
                    ReturnVarName = nativeVarName,
                    ReturnType    = param.QualifiedType
                };

                var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
                param.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                {
                    Write(marshal.Context.SupportBefore);
                }

                WriteLine("{0} = {1};", param.Name, marshal.Context.Return);
            }

            if (isValueType && !IsNativeFunctionOrStaticMethod(function))
            {
                GenerateStructMarshaling(@class, valueMarshalName + ".");
            }

            if (needsReturn)
            {
                var retTypeName = retType.Visit(TypePrinter);
                var isIntPtr    = retTypeName.Contains("IntPtr");

                if (retType.Type.IsPointer() && (isIntPtr || retTypeName.EndsWith("^")))
                {
                    WriteLine("if ({0} == nullptr) return {1};",
                              returnIdentifier,
                              isIntPtr ? "System::IntPtr()" : "nullptr");
                }

                var ctx = new MarshalContext(Driver)
                {
                    ArgName       = returnIdentifier,
                    ReturnVarName = returnIdentifier,
                    ReturnType    = retType
                };

                var marshal = new CLIMarshalNativeToManagedPrinter(ctx);
                retType.Visit(marshal);

                if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                {
                    Write(marshal.Context.SupportBefore);
                }

                // Special case for indexer - needs to dereference if the internal
                // function is a pointer type and the property is not.
                if (retType.Type.IsPointer() &&
                    retType.Type.GetPointee().Equals(publicRetType) &&
                    publicRetType.IsPrimitiveType())
                {
                    WriteLine("return *({0});", marshal.Context.Return);
                }
                else if (retType.Type.IsReference() && publicRetType.IsReference())
                {
                    WriteLine("return ({0})({1});", publicRetType, marshal.Context.Return);
                }
                else
                {
                    WriteLine("return {0};", marshal.Context.Return);
                }
            }
        }
Exemplo n.º 27
0
 public virtual void CppMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write(ctx.Parameter.Name);
 }
Exemplo n.º 28
0
        private void GeneratePropertySetter <T>(T decl, Class @class, string name, Type type, Parameter indexParameter = null)
            where T : Declaration, ITypedDecl
        {
            if (decl == null)
            {
                return;
            }

            var args      = new List <string>();
            var isIndexer = indexParameter != null;

            if (isIndexer)
            {
                args.Add(string.Format("{0} {1}", indexParameter.Type, indexParameter.Name));
            }

            var function = decl as Function;
            var argName  = function != null && !isIndexer ? function.Parameters[0].Name : "value";

            args.Add(string.Format("{0} {1}", type, argName));

            WriteLine("void {0}::{1}::set({2})", QualifiedIdentifier(@class),
                      name, string.Join(", ", args));

            WriteStartBraceIndent();

            if (decl is Function && !isIndexer)
            {
                var func = decl as Function;
                GenerateFunctionCall(func, @class);
            }
            else
            {
                if (@class.IsValueType && decl is Field)
                {
                    WriteLine("{0} = value;", decl.Name);
                    WriteCloseBraceIndent();
                    NewLine();
                    return;
                }
                var param = new Parameter
                {
                    Name          = "value",
                    QualifiedType = new QualifiedType(type)
                };

                string variable;
                if (decl is Variable)
                {
                    variable = string.Format("::{0}::{1}",
                                             @class.QualifiedOriginalName, decl.OriginalName);
                }
                else
                {
                    variable = string.Format("((::{0}*)NativePtr)->{1}",
                                             @class.QualifiedOriginalName, decl.OriginalName);
                }

                var ctx = new MarshalContext(Driver)
                {
                    Parameter     = param,
                    ArgName       = param.Name,
                    ReturnVarName = variable
                };

                var marshal = new CLIMarshalManagedToNativePrinter(ctx);
                param.Visit(marshal);

                if (isIndexer)
                {
                    var ctx2 = new MarshalContext(Driver)
                    {
                        Parameter = indexParameter,
                        ArgName   = indexParameter.Name
                    };

                    var marshal2 = new CLIMarshalManagedToNativePrinter(ctx2);
                    indexParameter.Visit(marshal2);

                    variable += string.Format("({0})", marshal2.Context.Return);
                }

                if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                {
                    Write(marshal.Context.SupportBefore);
                }

                if (marshal.Context.Return.StringBuilder.Length > 0)
                {
                    if (isIndexer && decl.Type.IsPointer())
                    {
                        WriteLine("*({0}) = {1};", variable, marshal.Context.Return);
                    }
                    else
                    {
                        WriteLine("{0} = {1};", variable, marshal.Context.Return);
                    }
                }
            }

            WriteCloseBraceIndent();
            NewLine();
        }
Exemplo n.º 29
0
 public virtual void CppMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write(ctx.ReturnVarName);
 }
Exemplo n.º 30
0
 public override void CLIMarshalToNative(MarshalContext ctx)
 {
     ctx.Return.Write("clix::marshalString<clix::E_UTF16>({0})",
                      ctx.Parameter.Name);
 }
Exemplo n.º 31
0
 public override void CLIMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write($"gcnew CLI::Employee({ctx.ReturnVarName}.m_employee)");
 }
Exemplo n.º 32
0
        private void MarshalValueClassProperty(Property property, string marshalVar)
        {
            var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name,
                                         property.Name);

            var marshalCtx = new MarshalContext(Context.Driver)
                                 {
                                     ArgName = fieldRef,
                                     ParameterIndex = Context.ParameterIndex++,
                                     MarshalVarPrefix = Context.MarshalVarPrefix
                                 };

            var marshal = new CLIMarshalManagedToNativePrinter(marshalCtx);
            property.Visit(marshal);

            Context.ParameterIndex = marshalCtx.ParameterIndex;

            if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                Context.SupportBefore.Write(marshal.Context.SupportBefore);

            Type type;
            Class @class;
            var isRef = property.Type.IsPointerTo(out type) &&
                !(type.TryGetClass(out @class) && @class.IsValueType) &&
                !type.IsPrimitiveType();

            if (isRef)
            {
                Context.SupportBefore.WriteLine("if ({0} != nullptr)", fieldRef);
                Context.SupportBefore.PushIndent();
            }

            Context.SupportBefore.WriteLine("{0}.{1} = {2};", marshalVar,
                property.Field.OriginalName, marshal.Context.Return);

            if (isRef)
                Context.SupportBefore.PopIndent();
        }
Exemplo n.º 33
0
 public override void CLIMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write("clix::marshalString<clix::E_UTF8>({0}.m_str)", ctx.ReturnVarName);
 }
Exemplo n.º 34
0
 public override void CLIMarshalToManaged(MarshalContext ctx)
 {
     ctx.Return.Write("{0}({1}.id)", CLISignature(), ctx.ReturnVarName);
 }
Exemplo n.º 35
0
 public virtual void CSharpMarshalToManaged(MarshalContext ctx)
 {
     throw new NotImplementedException();
 }
Exemplo n.º 36
0
        public override void CLIMarshalToNative(MarshalContext ctx)
        {
            var type = Type as TemplateSpecializationType;

            Class @class;
            if (!type.Arguments[0].Type.Type.IsTagDecl(out @class))
                return;

            ctx.Return.Write("(::{0}*){1}->NativePtr",
                @class.QualifiedOriginalName, ctx.Parameter.Name);
        }
Exemplo n.º 37
0
        private void MarshalValueClassField(Field field, string marshalVar)
        {
            var fieldRef = string.Format("{0}.{1}", Context.Parameter.Name,
                                         field.Name);

            var marshalCtx = new MarshalContext(Context.Driver)
                                 {
                                     ArgName = fieldRef,
                                     ParameterIndex = Context.ParameterIndex++,
                                     MarshalVarPrefix = Context.MarshalVarPrefix
                                 };

            var marshal = new CLIMarshalManagedToNativePrinter(marshalCtx);
            field.Visit(marshal);

            Context.ParameterIndex = marshalCtx.ParameterIndex;

            if (!string.IsNullOrWhiteSpace(marshal.Context.SupportBefore))
                Context.SupportBefore.Write(marshal.Context.SupportBefore);

            if(field.Type.IsPointer())
            {
                Context.SupportBefore.WriteLine("if ({0} != nullptr)", fieldRef);
                Context.SupportBefore.PushIndent();
            }

            Context.SupportBefore.WriteLine("{0}.{1} = {2};", marshalVar, field.OriginalName,
                                    marshal.Context.Return);

            if(field.Type.IsPointer())
                Context.SupportBefore.PopIndent();
        }
Exemplo n.º 38
-1
        public CLIMarshalManagedToNativePrinter(MarshalContext ctx)
            : base(ctx)
        {
            VarPrefix = new TextGenerator();
            ArgumentPrefix = new TextGenerator();

            Context.MarshalToNative = this;
        }