Exemplo n.º 1
0
 public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     var type = ctx.Parameter.Type.Desugar();
     ClassTemplateSpecialization basicString = GetBasicString(type);
     var typePrinter = new CSharpTypePrinter(ctx.Context);
     typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
     if (!ctx.Parameter.Type.Desugar().IsAddress())
         ctx.Return.Write("*({0}*) ", basicString.Visit(typePrinter));
     typePrinter.PopContext();
     var allocator = ctx.Context.ASTContext.FindClass("allocator", false, true).First(
         a => a.IsDependent);
     if (type.IsPointer() || (type.IsReference() && ctx.Declaration is Field))
     {
         ctx.Return.Write("new {0}({1}, new {2}()).{3}",
             basicString.Visit(typePrinter), ctx.Parameter.Name,
             allocator.Visit(typePrinter), Helpers.InstanceIdentifier);
     }
     else
     {
         string varAllocator = "__allocator" + ctx.ParameterIndex;
         string varBasicString = "__basicString" + ctx.ParameterIndex;
         ctx.SupportBefore.WriteLine("var {0} = new {1}();",
             varAllocator, allocator.Visit(typePrinter));
         ctx.SupportBefore.WriteLine("var {0} = new {1}({2}, {3});",
             varBasicString, basicString.Visit(typePrinter),
             ctx.Parameter.Name, varAllocator);
         ctx.Return.Write("{0}.{1}", varBasicString, Helpers.InstanceIdentifier);
         ctx.Cleanup.WriteLine("{0}.Dispose({1});", varBasicString,
             type.IsPointer() ? "true" : "false");
         ctx.Cleanup.WriteLine("{0}.Dispose();", varAllocator);
     }
 }
Exemplo n.º 2
0
 public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     var type = ctx.Parameter.Type.Desugar();
     ClassTemplateSpecialization basicString = GetBasicString(type);
     var typePrinter = new CSharpTypePrinter(ctx.Driver);
     typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
     if (!ctx.Parameter.Type.Desugar().IsAddress())
         ctx.Return.Write("*({0}*) ", basicString.Visit(typePrinter));
     typePrinter.PopContext();
     var allocator = ctx.Driver.ASTContext.FindClass("allocator", false, true).First(
         a => a.IsSupportedStdType());
     if (type.IsPointer() || (type.IsReference() && ctx.Declaration is Field))
     {
         ctx.Return.Write("new {0}({1}, new {2}()).{3}",
             basicString.Visit(typePrinter), ctx.Parameter.Name,
             allocator.Visit(typePrinter), Helpers.InstanceIdentifier);
     }
     else
     {
         string varBasicString = "__basicString" + ctx.ParameterIndex;
         ctx.SupportBefore.WriteLine("using (var {0} = new {1}({2}, new {3}()))",
             varBasicString, basicString.Visit(typePrinter),
             ctx.Parameter.Name, allocator.Visit(typePrinter));
         ctx.SupportBefore.WriteStartBraceIndent();
         ctx.Return.Write("{0}.{1}", varBasicString, Helpers.InstanceIdentifier);
         ctx.HasCodeBlock = true;
     }
 }
Exemplo n.º 3
0
 public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
 {
     var type = ctx.Parameter.Type.Desugar();
     ClassTemplateSpecialization basicString = GetBasicString(type);
     var typePrinter = new CSharpTypePrinter(ctx.Context);
     typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
     if (!ctx.Parameter.Type.Desugar().IsAddress())
         ctx.Return.Write("*({0}*) ", basicString.Visit(typePrinter));
     typePrinter.PopContext();
     var allocator = ctx.Context.ASTContext.FindClass("allocator", false, true).First(
         a => a.IsDependent && a.TranslationUnit.IsSystemHeader);
     if (type.IsPointer() || (type.IsReference() && ctx.Declaration is Field))
     {
         ctx.Return.Write("new {0}({1}, new {2}()).{3}",
             basicString.Visit(typePrinter), ctx.Parameter.Name,
             allocator.Visit(typePrinter), Helpers.InstanceIdentifier);
     }
     else
     {
         string varAllocator = "__allocator" + ctx.ParameterIndex;
         string varBasicString = "__basicString" + ctx.ParameterIndex;
         ctx.SupportBefore.WriteLine("var {0} = new {1}();",
             varAllocator, allocator.Visit(typePrinter));
         ctx.SupportBefore.WriteLine("var {0} = new {1}({2}, {3});",
             varBasicString, basicString.Visit(typePrinter),
             ctx.Parameter.Name, varAllocator);
         ctx.Return.Write("{0}.{1}", varBasicString, Helpers.InstanceIdentifier);
         ctx.Cleanup.WriteLine("{0}.Dispose({1});", varBasicString,
             type.IsPointer() ? "true" : "false");
         ctx.Cleanup.WriteLine("{0}.Dispose();", varAllocator);
     }
 }
Exemplo n.º 4
0
        public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
        {
            var type = ctx.Parameter.Type.Desugar();
            ClassTemplateSpecialization basicString = GetBasicString(type);
            var typePrinter = new CSharpTypePrinter(ctx.Context);

            typePrinter.PushContext(TypePrinterContextKind.Native);
            if (!ctx.Parameter.Type.Desugar().IsAddress())
            {
                ctx.Return.Write($"*({basicString.Visit(typePrinter)}*) ");
            }
            typePrinter.PopContext();
            var allocator = ctx.Context.ASTContext.FindClass("allocator", false, true).First(
                a => a.IsDependent && a.TranslationUnit.IsSystemHeader);
            var    allocatorChar        = allocator.Specializations.First(s => !s.Ignore);
            string qualifiedBasicString = GetQualifiedBasicString(basicString);

            if (type.IsPointer() || (type.IsReference() && ctx.Declaration is Field))
            {
                ctx.Return.Write($@"{qualifiedBasicString}Extensions.{basicString.Name}({
                    ctx.Parameter.Name}, new {allocatorChar.Visit(typePrinter)}()).{
                    Helpers.InstanceIdentifier}");
            }
            else
            {
                var varAllocator   = $"__allocator{ctx.ParameterIndex}";
                var varBasicString = $"__basicString{ctx.ParameterIndex}";
                ctx.Before.WriteLine($@"var {varAllocator} = new {
                    allocatorChar.Visit(typePrinter)}();");
                ctx.Before.WriteLine($@"var {varBasicString} = {
                    qualifiedBasicString}Extensions.{basicString.Name}({ctx.Parameter.Name}, {
                    varAllocator});");
                ctx.Return.Write($"{varBasicString}.{Helpers.InstanceIdentifier}");
                ctx.Cleanup.WriteLine($@"{varBasicString}.Dispose({
                    (type.IsPointer() ? "true" : "false")});");
                ctx.Cleanup.WriteLine($"{varAllocator}.Dispose();");
            }
        }
Exemplo n.º 5
0
        private string GenerateDelegateSignature(IEnumerable <Parameter> @params, QualifiedType returnType)
        {
            var typePrinter = new CSharpTypePrinter(Driver);

            typePrinter.PushContext(CSharpTypePrinterContextKind.Native);

            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 = typesBuilder.Replace("global::System.", string.Empty).Replace(
                "*", "Ptr").Replace('.', '_').ToString();

            if (returnType.Type.IsPrimitiveType(PrimitiveType.Void))
            {
                delegateName = "Action_" + delegateName;
            }
            else
            {
                delegateName = "Func_" + delegateName;
            }

            typePrinter.PopContext();
            return(delegateName);
        }
Exemplo n.º 6
0
        private string GenerateDelegateSignature(IEnumerable<Parameter> @params, QualifiedType returnType)
        {
            var typePrinter = new CSharpTypePrinter(Driver);
            typePrinter.PushContext(CSharpTypePrinterContextKind.Native);

            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 = typesBuilder.Replace("global::System.", string.Empty).Replace(
                "*", "Ptr").Replace('.', '_').ToString();
            if (returnType.Type.IsPrimitiveType(PrimitiveType.Void))
                delegateName = "Action_" + delegateName;
            else
                delegateName = "Func_" + delegateName;

            typePrinter.PopContext();
            return delegateName;
        }