Beispiel #1
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;
     }
 }
Beispiel #2
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);
     }
 }
Beispiel #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);
     }
 }
Beispiel #4
0
 public override string CSharpSignature(CSharpTypePrinterContext ctx)
 {
     if (ctx.CSharpKind == CSharpTypePrinterContextKind.Managed)
         return "string";
     ClassTemplateSpecialization basicString = GetBasicString(ctx.Type);
     var typePrinter = new CSharpTypePrinter(null);
     typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
     return basicString.Visit(typePrinter).Type;
 }
Beispiel #5
0
 public override string CSharpSignature(CSharpTypePrinterContext ctx)
 {
     if (ctx.CSharpKind == CSharpTypePrinterContextKind.Managed)
         return "string";
     ClassTemplateSpecialization basicString = GetBasicString(ctx.Type);
     var typePrinter = new CSharpTypePrinter(null);
     typePrinter.PushContext(CSharpTypePrinterContextKind.Native);
     return basicString.Visit(typePrinter).Type;
 }
Beispiel #6
0
        public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
        {
            // pointless, put just so that the generated code compiles
            var type           = (TemplateSpecializationType)ctx.Parameter.Type.Desugar();
            var specialization = type.GetClassTemplateSpecialization();
            var typePrinter    = new CSharpTypePrinter(null);

            typePrinter.PushContext(TypePrinterContextKind.Native);
            ctx.Return.Write("new {0}()", specialization.Visit(typePrinter));
        }
Beispiel #7
0
        public override Type CSharpSignatureType(TypePrinterContext ctx)
        {
            if (ctx.Kind == TypePrinterContextKind.Managed)
            {
                return(new CILType(typeof(string)));
            }
            ClassTemplateSpecialization basicString = GetBasicString(ctx.Type);
            var typePrinter = new CSharpTypePrinter(null);

            typePrinter.PushContext(TypePrinterContextKind.Native);
            return(new CustomType(basicString.Visit(typePrinter).Type));
        }
Beispiel #8
0
        public override string CSharpSignature(TypePrinterContext ctx)
        {
            if (ctx.Kind == TypePrinterContextKind.Native)
            {
                var type           = (TemplateSpecializationType)ctx.Type.Desugar();
                var specialization = type.GetClassTemplateSpecialization();
                var typePrinter    = new CSharpTypePrinter(null);
                typePrinter.PushContext(TypePrinterContextKind.Native);
                return(string.Format($"{specialization.Visit(typePrinter)}{(Type.IsAddress() ? "*" : string.Empty)}", specialization.Visit(typePrinter),
                                     Type.IsAddress() ? "*" : string.Empty));
            }

            return(string.Format("System.Collections.Generic.{0}<{1}>",
                                 ctx.MarshalKind == MarshalKind.DefaultExpression ? "List" : "IList",
                                 ctx.GetTemplateParameterList()));
        }
Beispiel #9
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();");
            }
        }
Beispiel #10
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);
        }
Beispiel #11
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;
        }
        private bool? CheckForDefaultConstruct(Type desugared, Statement statement,
            ref string result)
        {
            var type = desugared.GetFinalPointee() ?? desugared;

            Class decl;
            if (!type.TryGetClass(out decl))
                return false;

            var ctor = statement as CXXConstructExpr;

            TypeMap typeMap;

            var typePrinter = new CSharpTypePrinter(Driver);
            typePrinter.PushContext(CSharpTypePrinterContextKind.DefaultExpression);
            var typePrinterResult = type.Visit(typePrinter).Type;
            if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
            {
                var typeInSignature = typeMap.CSharpSignatureType(
                    typePrinter.Context).SkipPointerRefs().Desugar();
                Enumeration @enum;
                if (typeInSignature.TryGetEnum(out @enum))
                {
                    if (ctor != null &&
                        (ctor.Arguments.Count == 0 ||
                         HasSingleZeroArgExpression((Function) ctor.Declaration)))
                    {
                        result = "0";
                        return true;
                    }
                    return false;
                }

                if (ctor != null && typePrinterResult == "string" && ctor.Arguments.Count == 0)
                {
                    result = "\"\"";
                    return true;
                }
            }

            if (ctor == null)
                return decl.IsValueType ? (bool?) false : null;

            var method = (Method) statement.Declaration;
            var expressionSupported = decl.IsValueType && method.Parameters.Count == 0;

            if (statement.String.Contains('('))
            {
                var argsBuilder = new StringBuilder("new ");
                argsBuilder.Append(typePrinterResult);
                argsBuilder.Append('(');
                for (var i = 0; i < ctor.Arguments.Count; i++)
                {
                    var argument = ctor.Arguments[i];
                    var argResult = argument.String;
                    expressionSupported &= PrintExpression(method.Parameters[i].Type.Desugar(),
                        argument, ref argResult) ?? false;
                    argsBuilder.Append(argResult);
                    if (i < ctor.Arguments.Count - 1)
                        argsBuilder.Append(", ");
                }
                argsBuilder.Append(')');
                result = argsBuilder.ToString();
            }
            else
            {
                if (method.Parameters.Count > 0)
                {
                    var paramType = method.Parameters[0].Type.SkipPointerRefs().Desugar();
                    Enumeration @enum;
                    if (paramType.TryGetEnum(out @enum))
                        result = TranslateEnumExpression(method, paramType, statement.String);
                }
            }
            return expressionSupported ? true : (bool?) null;
        }
        private bool?CheckForDefaultConstruct(Type desugared, Statement statement,
                                              ref string result)
        {
            var type = desugared.GetFinalPointee() ?? desugared;

            Class decl;

            if (!type.TryGetClass(out decl))
            {
                return(false);
            }

            var ctor = statement as CXXConstructExpr;

            TypeMap typeMap;

            var typePrinter = new CSharpTypePrinter(Driver);

            typePrinter.PushContext(CSharpTypePrinterContextKind.DefaultExpression);
            var typePrinterResult = type.Visit(typePrinter).Type;

            if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap))
            {
                var typeInSignature = typeMap.CSharpSignatureType(
                    typePrinter.Context).SkipPointerRefs().Desugar();
                Enumeration @enum;
                if (typeInSignature.TryGetEnum(out @enum))
                {
                    if (ctor != null &&
                        (ctor.Arguments.Count == 0 ||
                         HasSingleZeroArgExpression((Function)ctor.Declaration)))
                    {
                        result = "0";
                        return(true);
                    }
                    return(false);
                }

                if (ctor != null && typePrinterResult == "string" && ctor.Arguments.Count == 0)
                {
                    result = "\"\"";
                    return(true);
                }
            }

            if (ctor == null)
            {
                return(decl.IsValueType ? (bool?)false : null);
            }

            var method = (Method)statement.Declaration;
            var expressionSupported = decl.IsValueType && method.Parameters.Count == 0;

            if (statement.String.Contains('('))
            {
                var argsBuilder = new StringBuilder("new ");
                argsBuilder.Append(typePrinterResult);
                argsBuilder.Append('(');
                for (var i = 0; i < ctor.Arguments.Count; i++)
                {
                    var argument  = ctor.Arguments[i];
                    var argResult = argument.String;
                    expressionSupported &= PrintExpression(method.Parameters[i].Type.Desugar(),
                                                           argument, ref argResult) ?? false;
                    argsBuilder.Append(argResult);
                    if (i < ctor.Arguments.Count - 1)
                    {
                        argsBuilder.Append(", ");
                    }
                }
                argsBuilder.Append(')');
                result = argsBuilder.ToString();
            }
            else
            {
                if (method.Parameters.Count > 0)
                {
                    var         paramType = method.Parameters[0].Type.SkipPointerRefs().Desugar();
                    Enumeration @enum;
                    if (paramType.TryGetEnum(out @enum))
                    {
                        result = TranslateEnumExpression(method, paramType, statement.String);
                    }
                }
            }
            return(expressionSupported ? true : (bool?)null);
        }