public override bool VisitMethodDecl(Method method) { if (!VisitDeclaration(method)) { return(false); } if (method.IsImplicit) { return(false); } PushBlock(BlockKind.Method, method); TypePrinter.PushContext(TypePrinterContextKind.Native); var returnTypeName = method.ReturnType.Visit(TypePrinter); Write($"public {returnTypeName} {GetCMethodIdentifier(method)}("); Write(TypePrinter.VisitParameters(method.Parameters, hasNames: true).ToString()); Write(");"); TypePrinter.PopContext(); PopBlock(NewLineKind.Never); return(true); }
public override bool VisitClassDecl(Class @class) { var @object = IsByRefParameter ? $"{ArgName}.get()" : ArgName; var objectRef = @class.IsInterface ? "__getObject()" : "__object"; if (IsByRefParameter) { CheckRefOutParameter(Parameter.IsInOut); TypePrinter.PushContext(TypePrinterContextKind.Native); var typeName = Parameter.Visit(TypePrinter); TypePrinter.PopContext(); var varName = JavaGenerator.GeneratedIdentifier(ArgName); var marshal = Parameter.IsInOut ? $"new {typeName}({@object}.{objectRef})" : $"new {typeName}()"; Before.WriteLine($"{typeName} {varName} = {marshal};"); var marshaler = new JavaMarshalNativeToManaged(Context) { Parameter = Parameter, ReturnVarName = $"{varName}.getValue()" }; @class.Visit(marshaler); After.WriteLine($"{ArgName}.set({marshaler.Return});"); Return.Write(varName); return(true); } Return.Write($"{ArgName} == null ? null : {ArgName}.{objectRef}"); return(true); }
private string GenerateDelegateSignature(IEnumerable <Parameter> @params, QualifiedType returnType) { TypePrinter.PushContext(CSharpTypePrinterContextKind.Native); CSharpTypePrinter.AppendGlobal = false; var typesBuilder = new StringBuilder(); if (!returnType.Type.IsPrimitiveType(PrimitiveType.Void)) { typesBuilder.Insert(0, returnType.Type.CSharpType(TypePrinter)); typesBuilder.Append('_'); } foreach (var parameter in @params) { typesBuilder.Append(parameter.CSharpType(TypePrinter)); typesBuilder.Append('_'); } if (typesBuilder.Length > 0) { typesBuilder.Remove(typesBuilder.Length - 1, 1); } var delegateName = Helpers.FormatTypesStringForIdentifier(typesBuilder); if (returnType.Type.IsPrimitiveType(PrimitiveType.Void)) { delegateName = "Action_" + delegateName; } else { delegateName = "Func_" + delegateName; } TypePrinter.PopContext(); CSharpTypePrinter.AppendGlobal = true; return(delegateName); }
public void HandleRefOutPrimitiveType(PrimitiveType type, Enumeration @enum = null) { TypePrinter.PushContext(TypePrinterContextKind.Native); var typeName = Context.Parameter.Visit(TypePrinter); TypePrinter.PopContext(); // Perform null checking for all primitive value types. string extraCheck = (type != PrimitiveType.String && Context.Parameter.IsInOut) ? $" || {Context.ArgName}.get() == null" : string.Empty; Context.SupportBefore.WriteLine($"if ({Context.ArgName} == null{extraCheck})"); Context.SupportBefore.WriteLineIndent($"throw new NullRefParameterException(\"{Context.Parameter.Name}\");"); string marshal = $"{Context.ArgName}.get()"; var isEnum = @enum != null; if (isEnum) { marshal = $"{marshal}.getValue()"; } if (type == PrimitiveType.Bool) { marshal = $"((byte) ({marshal} ? 1 : 0))"; } var varName = JavaGenerator.GeneratedIdentifier(Context.ArgName); Context.SupportBefore.Write($"{typeName} {varName} = "); if (isEnum || type == PrimitiveType.Bool) { Context.SupportBefore.WriteLine($"new {typeName}({marshal});"); } else { Context.SupportBefore.WriteLine($"({marshal}) != null ? new {typeName}({marshal}) : new {typeName}();"); } Context.Return.Write(varName); var value = $"{varName}.getValue()"; marshal = value; if (isEnum) { marshal = $"{@enum.Name}.fromOrdinal({value})"; } if (type == PrimitiveType.Bool) { marshal = $"{value} != 0"; } Context.SupportAfter.WriteLine($"{Context.ArgName}.set({marshal});"); }
public void HandleRefOutPrimitiveType(PrimitiveType type, Enumeration @enum = null) { TypePrinter.PushContext(TypePrinterContextKind.Native); var typeName = Context.Parameter.Visit(TypePrinter); TypePrinter.PopContext(); CheckRefOutParameter(type != PrimitiveType.String && Context.Parameter.IsInOut); string marshal = $"{Context.ArgName}.get()"; var isEnum = @enum != null; if (isEnum) { marshal = $"{marshal}.getValue()"; } if (type == PrimitiveType.Bool) { marshal = $"((byte) ({marshal} ? 1 : 0))"; } if (IsReferenceIntegerType(type)) { var integerTypeName = TypePrinter.VisitPrimitiveType(GetSignedIntegerType(type)); marshal = $"({integerTypeName}){marshal}.intValue()"; } var varName = JavaGenerator.GeneratedIdentifier(Context.ArgName); Context.SupportBefore.Write($"{typeName} {varName} = "); if (isEnum || type == PrimitiveType.Bool || IsReferenceIntegerType(type)) { Context.SupportBefore.WriteLine($"new {typeName}({marshal});"); } else { Context.SupportBefore.WriteLine($"({marshal}) != null ? new {typeName}({marshal}) : new {typeName}();"); } Context.Return.Write(varName); var value = $"{varName}.getValue()"; marshal = value; if (isEnum) { marshal = $"{@enum.Visit(TypePrinter)}.fromOrdinal({value})"; } if (type == PrimitiveType.Bool) { marshal = $"{value} != 0"; } if (IsReferenceIntegerType(type)) { var integerTypeName = Context.Parameter.Type.Visit(TypePrinter); marshal = $"new {integerTypeName}({value})"; } Context.SupportAfter.WriteLine($"{Context.ArgName}.set({marshal});"); }