Пример #1
0
        public override TypePrinterResult VisitPointerType(PointerType pointer,
                                                           TypeQualifiers quals)
        {
            var pointee = pointer.Pointee.Desugar();

            if (pointee is FunctionType)
            {
                var function = pointee as FunctionType;
                return(string.Format("{0}^", function.Visit(this, quals)));
            }

            if (pointer.IsConstCharString())
            {
                return("System::String^");
            }

            // From http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx
            // Any of the following types may be a pointer type:
            // * sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double, decimal, or bool.
            // * Any enum type.
            // * Any pointer type.
            // * Any user-defined struct type that contains fields of unmanaged types only.
            var finalPointee = pointer.GetFinalPointee();

            if (finalPointee.IsPrimitiveType())
            {
                // Skip one indirection if passed by reference
                bool isRefParam = Parameter != null && (Parameter.IsOut || Parameter.IsInOut);
                if (isRefParam)
                {
                    return(pointer.QualifiedPointee.Visit(this));
                }

                if (pointee.IsPrimitiveType(PrimitiveType.Void))
                {
                    return("::System::IntPtr");
                }

                var result = pointer.QualifiedPointee.Visit(this).ToString();
                return(!isRefParam && result == "::System::IntPtr" ? "void**" : result + "*");
            }

            Enumeration @enum;

            if (pointee.TryGetEnum(out @enum))
            {
                var typeName = @enum.Visit(this);

                // Skip one indirection if passed by reference
                if (Parameter != null && (Parameter.IsOut || Parameter.IsInOut) &&
                    pointee == finalPointee)
                {
                    return(string.Format("{0}", typeName));
                }

                return(string.Format("{0}*", typeName));
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
Пример #2
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            if (pointer.IsConstCharString())
            {
                return(VisitPrimitiveType(PrimitiveType.String));
            }

            var pointee = pointer.Pointee.Desugar();

            if (pointee is FunctionType)
            {
                typePrinter.PushContext(TypePrinterContextKind.Managed);
                var cppTypeName = pointer.Visit(typePrinter, quals);
                typePrinter.PopContext();

                return(VisitDelegateType(cppTypeName));
            }

            Enumeration @enum;

            if (pointee.TryGetEnum(out @enum))
            {
                var isRef = Context.Parameter.Usage == ParameterUsage.Out ||
                            Context.Parameter.Usage == ParameterUsage.InOut;

                Context.ArgumentPrefix.Write("&");
                Context.Return.Write($"(::{@enum.QualifiedOriginalName}){0}{Context.Parameter.Name}",
                                     isRef ? string.Empty : "*");
                return(true);
            }

            Class @class;

            if (pointee.TryGetClass(out @class) && @class.IsValueType)
            {
                if (Context.Function == null)
                {
                    Context.Return.Write("&");
                }
                return(pointer.QualifiedPointee.Visit(this));
            }

            var finalPointee = pointer.GetFinalPointee();

            if (finalPointee.IsPrimitiveType())
            {
                var cppTypeName = pointer.Visit(typePrinter, quals);

                Context.Return.Write($"({cppTypeName})");
                Context.Return.Write(Context.Parameter.Name);
                return(true);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (pointer.IsConstCharString())
            {
                return(VisitPrimitiveType(PrimitiveType.String, quals));
            }

            return(base.VisitPointerType(pointer, quals));
        }
Пример #4
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            if (pointer.IsConstCharString())
            {
                return(VisitPrimitiveType(PrimitiveType.String));
            }

            var pointee = pointer.Pointee.Desugar();

            return(pointer.QualifiedPointee.Visit(this));
        }
Пример #5
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (pointer.IsConstCharString())
            {
                return(true);
            }

            var pointee = pointer.GetFinalPointee().Desugar();

            if (!pointee.TryGetClass(out _))
            {
                Diagnostics.Warning($"Unsupported decl: {decl.QualifiedName}");
                return(false);
            }

            return(true);
        }
Пример #6
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var qualifiedPointer = new QualifiedType(pointer, quals);

            var         templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType;
            PointerType realPointer          = null;

            if (templateSubstitution != null)
            {
                realPointer = templateSubstitution.Replacement.Type.Desugar() as PointerType;
            }
            realPointer = realPointer ?? pointer;
            var pointee = pointer.Pointee.Desugar();

            if (Context.Function != null &&
                (realPointer.IsPrimitiveTypeConvertibleToRef() ||
                 (templateSubstitution != null && realPointer.Pointee.IsEnumType())) &&
                Context.MarshalKind != MarshalKind.VTableReturnValue)
            {
                var refParamPtr = $"__refParamPtr{Context.ParameterIndex}";
                if (templateSubstitution != null)
                {
                    var castParam = $"__{Context.Parameter.Name}{Context.ParameterIndex}";
                    Context.Before.Write($"var {castParam} = ({templateSubstitution}) ");
                    if (realPointer != pointer)
                    {
                        Context.Before.Write($"({CSharpTypePrinter.IntPtrType}) ");
                    }
                    Context.Before.WriteLine($"(object) {Context.Parameter.Name};");
                    Context.Before.Write($"var {refParamPtr} = ");
                    if (realPointer == pointer)
                    {
                        Context.Before.Write("&");
                    }
                    Context.Before.WriteLine($"{castParam};");
                    Context.Return.Write(refParamPtr);
                    return(true);
                }
                if (Context.Function.OperatorKind != CXXOperatorKind.Subscript)
                {
                    if (Context.Parameter.Kind == ParameterKind.PropertyValue ||
                        qualifiedPointer.IsConstRefToPrimitive())
                    {
                        Context.Return.Write($"&{Context.Parameter.Name}");
                    }
                    else
                    {
                        Context.Before.WriteLine(
                            $"fixed ({realPointer} {refParamPtr} = &{Context.Parameter.Name})");
                        Context.HasCodeBlock = true;
                        Context.Before.WriteOpenBraceAndIndent();
                        Context.Return.Write(refParamPtr);
                    }
                    return(true);
                }
            }

            var param      = Context.Parameter;
            var isRefParam = param != null && (param.IsInOut || param.IsOut);

            if (pointee.IsConstCharString() && isRefParam)
            {
                if (param.IsOut)
                {
                    Context.Return.Write("IntPtr.Zero");
                    Context.ArgumentPrefix.Write("&");
                }
                else if (param.IsInOut)
                {
                    Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
                    Context.ArgumentPrefix.Write("&");
                }
                else
                {
                    Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
                    Context.Cleanup.WriteLine("Marshal.FreeHGlobal({0});", Context.ArgName);
                }
                return(true);
            }

            if (pointee is FunctionType)
            {
                return(VisitDelegateType());
            }

            Class @class;

            if (pointee.TryGetClass(out @class) && @class.IsValueType)
            {
                if (Context.Parameter.Usage == ParameterUsage.Out)
                {
                    var qualifiedIdentifier = (@class.OriginalClass ?? @class).Visit(typePrinter);
                    Context.Before.WriteLine("var {0} = new {1}.{2}();",
                                             Generator.GeneratedIdentifier(Context.ArgName), qualifiedIdentifier,
                                             Helpers.InternalStruct);
                }
                else
                {
                    Context.Before.WriteLine("var {0} = {1}.{2};",
                                             Generator.GeneratedIdentifier(Context.ArgName),
                                             Context.Parameter.Name,
                                             Helpers.InstanceIdentifier);
                }

                Context.Return.Write("new global::System.IntPtr(&{0})",
                                     Generator.GeneratedIdentifier(Context.ArgName));
                return(true);
            }

            var           marshalAsString = pointer.IsConstCharString();
            var           finalPointee    = pointer.GetFinalPointee();
            PrimitiveType primitive;

            if (finalPointee.IsPrimitiveType(out primitive) || finalPointee.IsEnumType() ||
                marshalAsString)
            {
                // From MSDN: "note that a ref or out parameter is classified as a moveable
                // variable". This means we must create a local variable to hold the result
                // and then assign this value to the parameter.

                if (isRefParam)
                {
                    var typeName = Type.TypePrinterDelegate(finalPointee);
                    if (Context.Function.OperatorKind == CXXOperatorKind.Subscript)
                    {
                        Context.Return.Write(param.Name);
                    }
                    else
                    {
                        if (param.IsInOut)
                        {
                            Context.Before.WriteLine($"{typeName} _{param.Name} = {param.Name};");
                        }
                        else
                        {
                            Context.Before.WriteLine($"{typeName} _{param.Name};");
                        }

                        Context.Return.Write($"&_{param.Name}");
                    }
                }
                else
                {
                    if (!marshalAsString &&
                        Context.Context.Options.MarshalCharAsManagedChar &&
                        primitive == PrimitiveType.Char)
                    {
                        Context.Return.Write($"({typePrinter.PrintNative(pointer)}) ");
                    }

                    if (marshalAsString && (Context.MarshalKind == MarshalKind.NativeField ||
                                            Context.MarshalKind == MarshalKind.VTableReturnValue ||
                                            Context.MarshalKind == MarshalKind.Variable))
                    {
                        Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name));
                    }
                    else
                    {
                        if (qualifiedPointer.IsConstRefToPrimitive())
                        {
                            Context.Return.Write("&");
                        }
                        Context.Return.Write(Context.Parameter.Name);
                    }
                }

                return(true);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
Пример #7
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var param      = Context.Parameter;
            var isRefParam = param != null && (param.IsInOut || param.IsOut);

            var  pointee = pointer.Pointee.Desugar();
            bool marshalPointeeAsString = pointee.IsConstCharString() && isRefParam;

            if ((pointer.IsConstCharString() && !MarshalsParameter) ||
                marshalPointeeAsString)
            {
                Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName,
                                                            pointer.GetFinalPointee().Desugar() as BuiltinType));
                return(true);
            }

            var           finalPointee = pointer.GetFinalPointee();
            PrimitiveType primitive;

            if (finalPointee.IsPrimitiveType(out primitive) || finalPointee.IsEnumType())
            {
                if (isRefParam)
                {
                    Context.Return.Write("_{0}", param.Name);
                    return(true);
                }

                if (Context.Context.Options.MarshalCharAsManagedChar &&
                    primitive == PrimitiveType.Char)
                {
                    Context.Return.Write($"({pointer}) ");
                }

                var type = Context.ReturnType.Type.Desugar(
                    resolveTemplateSubstitution: false);
                if (Context.Function != null &&
                    Context.Function.OperatorKind == CXXOperatorKind.Subscript)
                {
                    if (type.IsPrimitiveType(primitive))
                    {
                        Context.Return.Write("*");
                    }
                    else
                    {
                        var templateParameter = type as TemplateParameterType;
                        if (templateParameter != null)
                        {
                            Context.Return.Write($"({templateParameter.Parameter.Name}) (object) *");
                        }
                    }
                }

                if (new QualifiedType(pointer, quals).IsConstRefToPrimitive())
                {
                    Context.Return.Write("*");
                }

                Context.Return.Write(Context.ReturnVarName);
                return(true);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
Пример #8
0
        public override TypePrinterResult VisitPointerType(PointerType pointer,
                                                           TypeQualifiers quals)
        {
            if (MarshalKind == MarshalKind.NativeField && !pointer.Pointee.IsEnumType())
            {
                return(IntPtrType);
            }

            if (pointer.Pointee is FunctionType)
            {
                return(pointer.Pointee.Visit(this, quals));
            }

            var isManagedContext = ContextKind == TypePrinterContextKind.Managed;

            if (allowStrings && pointer.IsConstCharString())
            {
                TypeMap typeMap;
                TypeMapDatabase.FindTypeMap(pointer, out typeMap);
                var typePrinterContext = new TypePrinterContext()
                {
                    Kind        = Kind,
                    MarshalKind = MarshalKind,
                    Type        = pointer.Pointee,
                    Parameter   = Parameter
                };
                return(typeMap.CSharpSignatureType(typePrinterContext).Visit(this));
            }

            var pointee = pointer.Pointee.Desugar();

            if (isManagedContext &&
                new QualifiedType(pointer, quals).IsConstRefToPrimitive())
            {
                return(pointee.Visit(this));
            }

            // From http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx
            // Any of the following types may be a pointer type:
            // * sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double,
            //   decimal, or bool.
            // * Any enum type.
            // * Any pointer type.
            // * Any user-defined struct type that contains fields of unmanaged types only.
            var finalPointee = (pointee.GetFinalPointee() ?? pointee).Desugar();

            if (finalPointee.IsPrimitiveType())
            {
                // Skip one indirection if passed by reference
                bool isRefParam = Parameter != null && (Parameter.IsOut || Parameter.IsInOut);
                if (isManagedContext && isRefParam)
                {
                    return(pointer.QualifiedPointee.Visit(this));
                }

                if (pointee.IsPrimitiveType(PrimitiveType.Void))
                {
                    return(IntPtrType);
                }

                if (pointee.IsConstCharString() && isRefParam)
                {
                    return(IntPtrType + "*");
                }

                // Do not allow strings inside primitive arrays case, else we'll get invalid types
                // like string* for const char **.
                allowStrings = isRefParam;
                var result = pointer.QualifiedPointee.Visit(this);
                allowStrings = true;

                return(!isRefParam && result.Type == IntPtrType ? "void**" : result + "*");
            }

            Enumeration @enum;

            if (pointee.TryGetEnum(out @enum))
            {
                // Skip one indirection if passed by reference
                if (isManagedContext && Parameter != null && (Parameter.IsOut || Parameter.IsInOut) &&
                    pointee == finalPointee)
                {
                    return(pointer.QualifiedPointee.Visit(this));
                }

                return(pointer.QualifiedPointee.Visit(this) + "*");
            }

            Class @class;

            if ((pointee.IsDependent || pointee.TryGetClass(out @class)) &&
                ContextKind == TypePrinterContextKind.Native)
            {
                return(IntPtrType);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
Пример #9
0
        public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals)
        {
            if (!VisitType(pointer, quals))
            {
                return(false);
            }

            var pointee = pointer.Pointee.Desugar();

            if (pointee.IsPrimitiveType(PrimitiveType.Void))
            {
                Context.Return.Write("::System::IntPtr({0})", Context.ReturnVarName);
                return(true);
            }

            if (pointer.IsConstCharString())
            {
                Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName,
                                                            pointer.Pointee.Desugar() as BuiltinType));
                return(true);
            }

            PrimitiveType primitive;
            var           param = Context.Parameter;

            if (param != null && (param.IsOut || param.IsInOut) &&
                pointee.IsPrimitiveType(out primitive))
            {
                Context.Return.Write(Context.ReturnVarName);
                return(true);
            }

            if (pointee.IsPrimitiveType(out primitive))
            {
                var returnVarName = Context.ReturnVarName;
                if (pointer.GetFinalQualifiedPointee().Qualifiers.IsConst !=
                    Context.ReturnType.Qualifiers.IsConst)
                {
                    var nativeTypePrinter = new CppTypePrinter {
                        PrintTypeQualifiers = false
                    };
                    var returnType       = Context.ReturnType.Type.Desugar();
                    var constlessPointer = new PointerType()
                    {
                        IsDependent      = pointer.IsDependent,
                        Modifier         = pointer.Modifier,
                        QualifiedPointee = new QualifiedType(returnType.GetPointee())
                    };
                    var nativeConstlessTypeName = constlessPointer.Visit(nativeTypePrinter, new TypeQualifiers());
                    returnVarName = string.Format("const_cast<{0}>({1})",
                                                  nativeConstlessTypeName, Context.ReturnVarName);
                }
                if (pointer.Pointee is TypedefType)
                {
                    var desugaredPointer = new PointerType()
                    {
                        IsDependent      = pointer.IsDependent,
                        Modifier         = pointer.Modifier,
                        QualifiedPointee = new QualifiedType(pointee)
                    };
                    var nativeTypePrinter = new CppTypePrinter();
                    var nativeTypeName    = desugaredPointer.Visit(nativeTypePrinter, quals);
                    Context.Return.Write("reinterpret_cast<{0}>({1})", nativeTypeName,
                                         returnVarName);
                }
                else
                {
                    Context.Return.Write(returnVarName);
                }
                return(true);
            }

            TypeMap typeMap = null;

            Context.Context.TypeMaps.FindTypeMap(pointee, out typeMap);

            Class @class;

            if (pointee.TryGetClass(out @class) && typeMap == null)
            {
                var instance = (pointer.IsReference) ? "&" + Context.ReturnVarName
                    : Context.ReturnVarName;
                WriteClassInstance(@class, instance);
                return(true);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }
Пример #10
0
        public override TypePrinterResult VisitPointerType(PointerType pointer,
                                                           TypeQualifiers quals)
        {
            if (MarshalKind == MarshalKind.NativeField && !pointer.Pointee.IsEnumType())
            {
                return(IntPtrType);
            }

            if (pointer.Pointee is FunctionType)
            {
                return(pointer.Pointee.Visit(this, quals));
            }

            var isManagedContext = ContextKind == TypePrinterContextKind.Managed;

            if (allowStrings && pointer.IsConstCharString())
            {
                if (isManagedContext)
                {
                    return("string");
                }
                if (Parameter == null || Parameter.Name == Helpers.ReturnIdentifier)
                {
                    return(IntPtrType);
                }
                if (Options.Encoding == Encoding.ASCII)
                {
                    return(string.Format("[MarshalAs(UnmanagedType.LPStr)] string"));
                }
                if (Options.Encoding == Encoding.Unicode ||
                    Options.Encoding == Encoding.BigEndianUnicode)
                {
                    return(string.Format("[MarshalAs(UnmanagedType.LPWStr)] string"));
                }
                throw new NotSupportedException($"{Options.Encoding.EncodingName} is not supported yet.");
            }

            var pointee = pointer.Pointee.Desugar();

            if (isManagedContext &&
                new QualifiedType(pointer, quals).IsConstRefToPrimitive())
            {
                return(pointee.Visit(this));
            }

            // From http://msdn.microsoft.com/en-us/library/y31yhkeb.aspx
            // Any of the following types may be a pointer type:
            // * sbyte, byte, short, ushort, int, uint, long, ulong, char, float, double,
            //   decimal, or bool.
            // * Any enum type.
            // * Any pointer type.
            // * Any user-defined struct type that contains fields of unmanaged types only.
            var finalPointee = pointer.GetFinalPointee();

            if (finalPointee.IsPrimitiveType())
            {
                // Skip one indirection if passed by reference
                bool isRefParam = Parameter != null && (Parameter.IsOut || Parameter.IsInOut);
                if (isManagedContext && isRefParam)
                {
                    return(pointer.QualifiedPointee.Visit(this));
                }

                if (pointee.IsPrimitiveType(PrimitiveType.Void))
                {
                    return(IntPtrType);
                }

                if (pointee.IsConstCharString() && isRefParam)
                {
                    return(IntPtrType + "*");
                }

                // Do not allow strings inside primitive arrays case, else we'll get invalid types
                // like string* for const char **.
                allowStrings = isRefParam;
                var result = pointer.QualifiedPointee.Visit(this);
                allowStrings = true;

                return(!isRefParam && result.Type == IntPtrType ? "void**" : result + "*");
            }

            Enumeration @enum;

            if (pointee.TryGetEnum(out @enum))
            {
                // Skip one indirection if passed by reference
                if (isManagedContext && Parameter != null && (Parameter.IsOut || Parameter.IsInOut) &&
                    pointee == finalPointee)
                {
                    return(pointer.QualifiedPointee.Visit(this));
                }

                return(pointer.QualifiedPointee.Visit(this) + "*");
            }

            Class @class;

            if ((pointee.IsDependent || pointee.TryGetClass(out @class)) &&
                ContextKind == TypePrinterContextKind.Native)
            {
                return(IntPtrType);
            }

            return(pointer.QualifiedPointee.Visit(this));
        }