Esempio n. 1
0
        public override void CSharpMarshalToManaged(CSharpMarshalContext ctx)
        {
            var type = Type.Desugar(resolveTemplateSubstitution: false);
            ClassTemplateSpecialization basicString = GetBasicString(type);
            var    data                 = basicString.Methods.First(m => m.OriginalName == "data");
            var    typePrinter          = new CSharpTypePrinter(ctx.Context);
            string qualifiedBasicString = GetQualifiedBasicString(basicString);
            string varBasicString       = $"__basicStringRet{ctx.ParameterIndex}";
            bool   usePointer           = type.IsAddress() || ctx.MarshalKind == MarshalKind.NativeField ||
                                          ctx.MarshalKind == MarshalKind.ReturnVariableArray;

            ctx.Before.WriteLine($@"var {varBasicString} = {
                basicString.Visit(typePrinter)}.{Helpers.CreateInstanceIdentifier}({
                (usePointer ? string.Empty : $"new {typePrinter.IntPtrType}(&")}{
Esempio n. 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(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);

            if (type.IsPointer() || (type.IsReference() && ctx.Declaration is Field))
            {
                ctx.Return.Write($@"new {basicString.Visit(typePrinter)}({
                    ctx.Parameter.Name}, new {allocatorChar.Visit(typePrinter)}()).{
                    Helpers.InstanceIdentifier}");
            }
            else
            {
                var varAllocator   = $"__allocator{ctx.ParameterIndex}";
                var varBasicString = $"__basicString{ctx.ParameterIndex}";
                ctx.SupportBefore.WriteLine($@"var {varAllocator} = new {
                    allocatorChar.Visit(typePrinter)}();");
                ctx.SupportBefore.WriteLine($@"var {varBasicString} = new {
                    basicString.Visit(typePrinter)}({ctx.Parameter.Name}, {varAllocator});");
                ctx.Return.Write($"{varBasicString}.{Helpers.InstanceIdentifier}");
                ctx.Cleanup.WriteLine($@"{varBasicString}.Dispose({
                    (type.IsPointer() ? "true" : "false")});");
                ctx.Cleanup.WriteLine($"{varAllocator}.Dispose();");
            }
        }
Esempio 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(TypePrinterContextKind.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);
            }
        }
Esempio n. 4
0
        public override Type CSharpSignatureType(TypePrinterContext ctx)
        {
            if (ctx.Kind == TypePrinterContextKind.Managed)
            {
                return(new CILType(typeof(string)));
            }
            var typePrinter = new CSharpTypePrinter(null);

            typePrinter.PushContext(TypePrinterContextKind.Native);
            if (ctx.Type.Desugar().IsAddress())
            {
                return(new CustomType(typePrinter.IntPtrType));
            }
            ClassTemplateSpecialization basicString = GetBasicString(ctx.Type);

            return(new CustomType(basicString.Visit(typePrinter).Type));
        }
 public override bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecialization specialization)
 {
     if (!specialization.IsDependent &&
         (!specialization.TranslationUnit.IsSystemHeader ||
         specialization.IsSupportedStdSpecialization()))
     {
         var cppTypePrinter = new CppTypePrinter
         {
             PrintScopeKind = CppTypePrintScopeKind.Qualified,
             PrintLogicalNames = true
         };
         var cppCode = specialization.Visit(cppTypePrinter);
         var module = specialization.TranslationUnit.Module;
         if (templateInstantiations.ContainsKey(module))
             templateInstantiations[module].Add(cppCode);
         else
             templateInstantiations.Add(module, new HashSet<string> { cppCode });
     }
     return true;
 }
Esempio n. 6
0
        public override void CSharpMarshalToNative(CSharpMarshalContext ctx)
        {
            Type type = ctx.Parameter.Type.Desugar();
            ClassTemplateSpecialization basicString = GetBasicString(type);
            var typePrinter = new CSharpTypePrinter(ctx.Context);

            if (!ctx.Parameter.Type.Desugar().IsAddress() &&
                ctx.MarshalKind != MarshalKind.NativeField)
            {
                ctx.Return.Write($"*({typePrinter.PrintNative(basicString)}*) ");
            }
            string qualifiedBasicString = GetQualifiedBasicString(basicString);
            var    assign = basicString.Methods.First(m => m.OriginalName == "assign");

            if (ctx.MarshalKind == MarshalKind.NativeField)
            {
                ctx.Return.Write($@"{qualifiedBasicString}Extensions.{
                    Helpers.InternalStruct}.{assign.Name}(new {
                    typePrinter.IntPtrType}(&{
                    ctx.ReturnVarName}), {ctx.Parameter.Name})");
                ctx.ReturnVarName = string.Empty;
            }
            else
            {
                var varBasicString = $"__basicString{ctx.ParameterIndex}";
                ctx.Before.WriteLine($@"var {varBasicString} = new {
                    basicString.Visit(typePrinter)}();");
                ctx.Before.WriteLine($@"{qualifiedBasicString}Extensions.{
                    assign.Name}({varBasicString}, {ctx.Parameter.Name});");
                ctx.Return.Write($"{varBasicString}.{Helpers.InstanceIdentifier}");
                if (!type.IsPointer() || ctx.Parameter.IsIndirect)
                {
                    ctx.Cleanup.WriteLine($@"{varBasicString}.Dispose({
                        (ctx.MarshalKind == MarshalKind.NativeField ? "false" : string.Empty)});");
                }
            }
        }
Esempio n. 7
0
 public override bool VisitClassTemplateSpecializationDecl(ClassTemplateSpecialization specialization)
 {
     WriteLine($"template class {GetExporting()}{specialization.Visit(cppTypePrinter)};");
     return(true);
 }