public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) { if (!VisitType(pointer, quals)) { return(false); } var pointee = pointer.Pointee.Desugar(); if (Context.Function != null && pointer.IsPrimitiveTypeConvertibleToRef() && Context.Kind != MarshalKind.VTableReturnValue) { var refParamPtr = string.Format("__refParamPtr{0}", Context.ParameterIndex); var templateSubstitution = pointer.Pointee as TemplateParameterSubstitutionType; if (templateSubstitution != null) { var castParam = $"__{Context.Parameter.Name}{Context.ParameterIndex}"; Context.SupportBefore.WriteLine( $"var {castParam} = ({templateSubstitution}) (object) {Context.Parameter.Name};"); Context.SupportBefore.WriteLine($"{pointer} {refParamPtr} = &{castParam};"); Context.Return.Write(refParamPtr); } else { Context.SupportBefore.WriteLine( $"fixed ({pointer} {refParamPtr} = &{Context.Parameter.Name})"); Context.HasCodeBlock = true; Context.SupportBefore.WriteStartBraceIndent(); Context.Return.Write(refParamPtr); } return(true); } var param = Context.Parameter; var isRefParam = param != null && (param.IsInOut || param.IsOut); if (CSharpTypePrinter.IsConstCharString(pointee) && 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) { var function = pointee as FunctionType; return(VisitDelegateType(function, function.ToString())); } Class @class; if (pointee.TryGetClass(out @class) && @class.IsValueType) { if (Context.Parameter.Usage == ParameterUsage.Out) { var qualifiedIdentifier = (@class.OriginalClass ?? @class).Visit(typePrinter); Context.SupportBefore.WriteLine("var {0} = new {1}.{2}();", Generator.GeneratedIdentifier(Context.ArgName), qualifiedIdentifier, Helpers.InternalStruct); } else { Context.SupportBefore.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 = CSharpTypePrinter.IsConstCharString(pointer); 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 (param.IsInOut) { Context.SupportBefore.WriteLine("{0} _{1} = {1};", typeName, param.Name); } else { Context.SupportBefore.WriteLine("{0} _{1};", typeName, param.Name); } Context.Return.Write("&_{0}", param.Name); } else { if (!marshalAsString && Context.Context.Options.MarshalCharAsManagedChar && primitive == PrimitiveType.Char) { typePrinter.PushContext(TypePrinterContextKind.Native); Context.Return.Write(string.Format("({0}) ", pointer.Visit(typePrinter))); typePrinter.PopContext(); } if (marshalAsString && (Context.Kind == MarshalKind.NativeField || Context.Kind == MarshalKind.VTableReturnValue || Context.Kind == MarshalKind.Variable)) { Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name)); } else { Context.Return.Write(Context.Parameter.Name); } } return(true); } return(pointer.QualifiedPointee.Visit(this)); }
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; 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); var pointee = pointer.Pointee.Desugar(); if (pointee.IsConstCharString() && isRefParam) { if (param.IsOut) { Context.Return.Write("IntPtr.Zero"); Context.ArgumentPrefix.Write("&"); return(true); } pointer.QualifiedPointee.Visit(this); if (param.IsInOut) { Context.ArgumentPrefix.Write("&"); } else { 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 finalPointee = pointer.GetFinalPointee(); PrimitiveType primitive; if (finalPointee.IsPrimitiveType(out primitive) || finalPointee.IsEnumType()) { // 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 (Context.Context.Options.MarshalCharAsManagedChar && primitive == PrimitiveType.Char) { Context.Return.Write($"({typePrinter.PrintNative(pointer)}) "); } if (qualifiedPointer.IsConstRefToPrimitive()) { Context.Return.Write("&"); } Context.Return.Write(Context.Parameter.Name); } return(true); } return(pointer.QualifiedPointee.Visit(this)); }