public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) { if (!VisitType(pointer, quals)) { return(false); } var pointee = pointer.Pointee; if (CSharpTypePrinter.IsConstCharString(pointer)) { Context.Return.Write("Marshal.PtrToStringAnsi({0})", Context.ReturnVarName); return(true); } PrimitiveType primitive; if (pointee.Desugar().IsPrimitiveType(out primitive)) { Context.Return.Write(Context.ReturnVarName); return(true); } if (!pointee.Visit(this, quals)) { return(false); } return(true); }
public override bool VisitPointerType(PointerType pointer, TypeQualifiers quals) { if (!VisitType(pointer, quals)) { return(false); } var pointee = pointer.Pointee.Desugar(); if (CSharpTypePrinter.IsConstCharString(pointer)) { Context.Return.Write(MarshalStringToManaged(Context.ReturnVarName, pointer.Pointee.Desugar() as BuiltinType)); return(true); } PrimitiveType primitive; if (pointee.IsPrimitiveType(out primitive) || pointee.IsEnumType()) { var param = Context.Parameter; if (param != null && (param.IsOut || param.IsInOut)) { Context.Return.Write("_{0}", param.Name); return(true); } Context.Return.Write(Context.ReturnVarName); return(true); } return(pointer.Pointee.Visit(this, quals)); }
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 = CSharpTypePrinter.IsConstCharString(pointee) && isRefParam; if ((CSharpTypePrinter.IsConstCharString(pointer) && !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 && type.IsPrimitiveType(primitive)) { var substitute = type as TemplateParameterSubstitutionType; if (substitute != null) { Context.Return.Write($@"({ substitute.ReplacedParameter.Parameter.Name}) (object) "); } Context.Return.Write("*"); } Context.Return.Write(Context.ReturnVarName); return(true); } return(pointer.QualifiedPointee.Visit(this)); }
public static CSharpTypePrinterResult CSharpType(this Declaration decl, CSharpTypePrinter printer) { if (decl is ITypedDecl) { var type = (decl as ITypedDecl).QualifiedType; printer.TypePrinterContext.FullType = type; } return(decl.Visit(printer)); }
private static void ThrowException(CSharpSources gen, Class @class) { var typePrinter = new CSharpTypePrinter(gen.Context); var supportedTypes = string.Join(", ", @class.Specializations.Where(s => !s.Ignore).Select(s => $@"<{string.Join(", ", s.Arguments.Select(a => typePrinter.VisitTemplateArgument(a)))}>")); var typeArguments = string.Join(", ", @class.TemplateParameters.Select(p => p.Name)); var managedTypes = string.Join(", ", @class.TemplateParameters.Select(p => $"typeof({p.Name}).FullName")); gen.WriteLine($"throw new ArgumentOutOfRangeException(\"{typeArguments}\", " + $@"string.Join("", "", new[] {{ {managedTypes} }}), " + $"\"{@class.Visit(typePrinter)} maps a C++ template class and therefore it only supports a limited set of types and their subclasses: {supportedTypes}.\");"); }
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 = CSharpTypePrinter.IsConstCharString(pointee) && isRefParam; if ((CSharpTypePrinter.IsConstCharString(pointer) && !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(string.Format("({0}) ", pointer)); } Context.Return.Write(Context.ReturnVarName); return(true); } return(pointer.QualifiedPointee.Visit(this)); }
public static CSharpTypePrinterResult CSharpType(this Type type, CSharpTypePrinter printer) { return(CSharpType(new QualifiedType(type), printer)); }
public static CSharpTypePrinterResult CSharpType(this QualifiedType type, CSharpTypePrinter printer) { printer.TypePrinterContext.FullType = type; return(type.Visit(printer)); }
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 = CSharpTypePrinter.IsConstCharString(pointee) && isRefParam; if (CSharpTypePrinter.IsConstCharString(pointer) || marshalPointeeAsString) { if (param.IsOut) { Context.Return.Write("IntPtr.Zero"); CSharpContext.ArgumentPrefix.Write("&"); } else if (param.IsInOut) { Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name)); CSharpContext.ArgumentPrefix.Write("&"); } else { Context.Return.Write(MarshalStringToUnmanaged(Context.Parameter.Name)); CSharpContext.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) { Context.SupportBefore.WriteLine("var {0} = new {1}.Internal();", Generator.GeneratedIdentifier(Context.ArgName), @class.Name); } 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 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 (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 { Context.Return.Write(Context.Parameter.Name); } return(true); } return(pointer.Pointee.Visit(this, quals)); }
public CSharpGenerator(BindingContext context) : base(context) { typePrinter = new CSharpTypePrinter(context); }
public CSharpGenerator(Driver driver) : base(driver) { typePrinter = new CSharpTypePrinter(driver); expressionPrinter = new CSharpExpressionPrinter(typePrinter); }
public CSharpGenerator(Driver driver) : base(driver) { typePrinter = new CSharpTypePrinter(driver.TypeDatabase, driver.Library); CppSharp.AST.Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type; }
public CSharpExpressionPrinter(CSharpTypePrinter typePrinter) { this.typePrinter = typePrinter; }
public CSharpMarshalManagedToNativePrinter(CSharpMarshalContext context) : base(context) { typePrinter = new CSharpTypePrinter(context.Driver); }
public CSharpGenerator(BindingContext context) : base(context) { typePrinter = new CSharpTypePrinter(context); expressionPrinter = new CSharpExpressionPrinter(typePrinter); }
public CSharpMarshalNativeToManagedPrinter(CSharpMarshalContext context) : base(context) { typePrinter = new CSharpTypePrinter(context.Context); }
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 CSharpGenerator(Driver driver) : base(driver) { typePrinter = new CSharpTypePrinter(driver); expressionPrinter = new CSharpExpressionPrinter(); CppSharp.AST.Type.TypePrinterDelegate += type => type.Visit(typePrinter).Type; }