private static bool CheckForEnumValue(Parameter parameter, Type desugared) { var enumItem = parameter.DefaultArgument.Declaration as Enumeration.Item; if (enumItem != null) { parameter.DefaultArgument.String = string.Format("{0}{1}{2}.{3}", desugared.IsPrimitiveType() ? "(int) " : string.Empty, string.IsNullOrEmpty(enumItem.Namespace.Namespace.Name) ? string.Empty : enumItem.Namespace.Namespace.Name + ".", enumItem.Namespace.Name, enumItem.Name); return true; } var call = parameter.DefaultArgument.Declaration as Function; if (call != null || parameter.DefaultArgument.Class == StatementClass.BinaryOperator) { string @params = regexFunctionParams.Match(parameter.DefaultArgument.String).Groups[1].Value; if (@params.Contains("::")) parameter.DefaultArgument.String = regexDoubleColon.Replace(@params, desugared + "."); else parameter.DefaultArgument.String = regexName.Replace(@params, desugared + ".$1"); return true; } return false; }
private static bool CheckForDefaultPointer(Type desugared, Parameter parameter) { if (desugared.IsPointer()) { parameter.DefaultArgument.String = "null"; return true; } return false; }
private static bool CheckForDefaultPointer(Type desugared, Parameter parameter) { if (desugared.IsPointer()) { // IntPtr.Zero is not a constant parameter.DefaultArgument.String = desugared.IsPointerToPrimitiveType(PrimitiveType.Void) ? "new global::System.IntPtr()" : "null"; return true; } return false; }
private static Type GetEnumType(Type mappedType) { var type = mappedType.Desugar(); ClassTemplateSpecialization classTemplateSpecialization; var templateSpecializationType = type as TemplateSpecializationType; if (templateSpecializationType != null) classTemplateSpecialization = templateSpecializationType.GetClassTemplateSpecialization(); else classTemplateSpecialization = (ClassTemplateSpecialization) ((TagType) type).Declaration; return classTemplateSpecialization.Arguments[0].Type.Type; }
public override TypePrinterResult VisitParameter(Parameter arg, bool hasName = true) { string type = arg.Type.Visit(this, arg.QualifiedType.Qualifiers); string name = arg.Name; bool printName = hasName && !string.IsNullOrEmpty(name); if (PrintFlavorKind == CppTypePrintFlavorKind.ObjC) { return(printName ? string.Format(":({0}){1}", type, name) : string.Format(":({0})", type)); } CppSharp.AST.Type desugared = arg.Type.Desugar(); desugared = (desugared.GetFinalPointee() ?? desugared).Desugar(); return(printName ? ((!(arg.Type is TypedefType) || ResolveTypedefs) && desugared is FunctionType ? type.Replace("(*)", $"(*{name})") : $"{type} {name}") : type); }
public static bool TryGetHandleDecl(this CppSharp.AST.Type astType, out TypedefNameDecl decl) { switch (astType) { case TypedefType tdt when(tdt.Declaration.IsHandleTypeDef( )): decl = tdt.Declaration; return(true); case PointerType pt when(pt.Pointee is TypedefType tdt && tdt.Declaration.IsHandleTypeDef( )): decl = tdt.Declaration; return(true); default: decl = null; return(false); } }
public override bool VisitClassDecl(Class @class) { var originalClass = @class.OriginalClass ?? @class; Type returnType = Context.ReturnType.Type.Desugar(); // if the class is an abstract impl, use the original for the object map var qualifiedClass = originalClass.Visit(typePrinter); if (returnType.IsAddress()) { Context.Return.Write(HandleReturnedPointer(@class, qualifiedClass.Type)); } else { Context.Return.Write("{0}.{1}({2})", qualifiedClass, Helpers.CreateInstanceIdentifier, Context.ReturnVarName); } return(true); }
public override Type CSharpSignatureType(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(new CustomType(string.Format($@"{ specialization.Visit(typePrinter)}{ (Type.IsAddress() ? "*" : string.Empty)}", specialization.Visit(typePrinter), Type.IsAddress() ? "*" : string.Empty))); } return(new CustomType( $@"System.Collections.Generic.{ (ctx.MarshalKind == MarshalKind.DefaultExpression ? "List" : "IList")}<{ ctx.GetTemplateParameterList()}>")); }
public bool FindTypeMap(CppSharp.AST.Type type, out TypePrinterResult result) { result = null; if (!ResolveTypeMaps) { return(false); } TypeMap typeMap; if (!TypeMapDatabase.FindTypeMap(type, out typeMap) || typeMap.IsIgnored) { return(false); } var typePrinterContext = new TypePrinterContext { Type = type, Kind = Kind, MarshalKind = MarshalKind }; var typePrinter = new CppTypePrinter(Context) { PrintFlavorKind = PrintFlavorKind, ScopeKind = ScopeKind, PrintTypeQualifiers = PrintTypeQualifiers, PrintTypeModifiers = PrintTypeModifiers, ResolveTypeMaps = false }; typePrinter.PushContext(ContextKind); var typeName = typeMap.CppSignatureType(typePrinterContext).Visit(typePrinter); result = new TypePrinterResult(typeName) { TypeMap = typeMap }; return(true); }
public override bool VisitTypedefType(TypedefType typedef, TypeQualifiers quals) { if (!(typedef.Declaration.Type.Desugar(false) is TemplateParameterSubstitutionType) && !VisitType(typedef, quals)) { return(false); } var decl = typedef.Declaration; Type type = decl.Type.Desugar(); var functionType = type as FunctionType; if (functionType == null && !type.IsPointerTo(out functionType)) { return(decl.Type.Visit(this)); } var ptrName = $@"{Generator.GeneratedIdentifier("ptr")}{ Context.ParameterIndex}"; Context.Before.WriteLine($"var {ptrName} = {Context.ReturnVarName};"); var substitution = decl.Type.Desugar(false) as TemplateParameterSubstitutionType; if (substitution != null) { Context.Return.Write($@"({ substitution.ReplacedParameter.Parameter.Name}) (object) ("); } Context.Return.Write($@"{ptrName} == IntPtr.Zero? null : { (substitution == null ? $"({Context.ReturnType}) " : string.Empty)}Marshal.GetDelegateForFunctionPointer({ ptrName}, typeof({typedef}))"); if (substitution != null) { Context.Return.Write(")"); } return(true); }
public bool FindTypeMap(Type type, out TypeMap typeMap) { var typePrinter = new CppTypePrinter { PrintTypeQualifiers = false, PrintTypeModifiers = false, PrintLogicalNames = true }; var template = type as TemplateSpecializationType; if (template != null) { var specialization = template.GetClassTemplateSpecialization(); if (specialization != null && FindTypeMap(specialization, type, out typeMap)) { return(true); } if (template.Template.TemplatedDecl != null) { return(FindTypeMap(template.Template.TemplatedDecl, type, out typeMap)); } } typePrinter.PrintScopeKind = TypePrintScopeKind.Local; if (FindTypeMap(type.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return(true); } typePrinter.PrintScopeKind = TypePrintScopeKind.Qualified; if (FindTypeMap(type.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return(true); } var typedef = type as TypedefType; return(typedef != null && FindTypeMap(typedef.Declaration, type, out typeMap)); }
private static Type GetEnumType(Type mappedType) { var type = mappedType.Desugar(); ClassTemplateSpecialization classTemplateSpecialization; var templateSpecializationType = type as TemplateSpecializationType; if (templateSpecializationType != null) { return(templateSpecializationType.Arguments[0].Type.Type); } var declaration = ((TagType)type).Declaration; if (declaration.IsDependent) { return(new TagType(((Class)declaration).TemplateParameters[0])); } classTemplateSpecialization = (ClassTemplateSpecialization)declaration; return(classTemplateSpecialization.Arguments[0].Type.Type); }
public bool FindTypeMap(Declaration decl, Type type, out TypeMap typeMap) { // We try to find type maps from the most qualified to less qualified // types. Example: '::std::vector', 'std::vector' and 'vector' var typePrinter = new CppTypePrinter { PrintLogicalNames = true }; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return(true); } typePrinter.PrintScopeKind = CppTypePrintScopeKind.Qualified; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return(true); } typePrinter.PrintScopeKind = CppTypePrintScopeKind.Local; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return(true); } var specialization = decl as ClassTemplateSpecialization; if (specialization != null && FindTypeMap(specialization.TemplatedDecl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return(true); } var typedef = decl as TypedefDecl; return(typedef != null && FindTypeMap(typedef.Type, out typeMap)); }
public override bool VisitFunctionDecl(Function function) { bool result = base.VisitFunctionDecl(function); var overloadIndices = new List <int>(function.Parameters.Count); foreach (var parameter in function.Parameters.Where(p => p.DefaultArgument != null)) { Type desugared = parameter.Type.Desugar(); if (CheckForDefaultPointer(desugared, parameter)) { continue; } CheckFloatSyntax(desugared, parameter); bool?defaultConstruct = CheckForDefaultConstruct(desugared, parameter.DefaultArgument); if (defaultConstruct == null || (!Driver.Options.MarshalCharAsManagedChar && parameter.Type.Desugar().IsPrimitiveType(PrimitiveType.UChar))) { overloadIndices.Add(function.Parameters.IndexOf(parameter)); continue; } if (defaultConstruct == true) { continue; } if (CheckForEnumValue(parameter.DefaultArgument, desugared)) { continue; } CheckForDefaultEmptyChar(parameter, desugared); } GenerateOverloads(function, overloadIndices); return(result); }
private static void GetProperty(List <Property> properties, Method method, string name, QualifiedType type, bool isSetter = false) { Type underlyingType = GetUnderlyingType(type); Class @class = (Class)method.Namespace; Property property = properties.Find( p => p.Field == null && ((!isSetter && p.SetMethod?.IsStatic == method.IsStatic) || (isSetter && p.GetMethod?.IsStatic == method.IsStatic)) && ((p.HasGetter && GetUnderlyingType( p.GetMethod.OriginalReturnType).Equals(underlyingType)) || (p.HasSetter && GetUnderlyingType( p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType))) && Match(p, name)); if (property == null) { properties.Add(property = new Property { Name = name, QualifiedType = type }); } if (isSetter) { property.SetMethod = method; } else { property.GetMethod = method; property.QualifiedType = method.OriginalReturnType; } property.Access = (AccessSpecifier)Math.Max( (int)(property.GetMethod ?? property.SetMethod).Access, (int)method.Access); if (method.ExplicitInterfaceImpl != null) { property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl; } }
internal TypeDefinition GetTypeDefinition(Type type, string name = null) { var typedefType = type as TypedefType; if (typedefType != null) { type = typedefType.Declaration.Type; } var arrayType = type as ArrayType; if (arrayType?.SizeType == ArrayType.ArraySize.Constant) { return(GetFieldTypeForFixedArray(arrayType)); } var tagType = type as TagType; if (tagType != null) { return(GetFieldTypeForNestedDeclaration(tagType.Declaration, name)); } var pointerType = type as PointerType; if (pointerType != null) { return(GetTypeDefinition(pointerType, name)); } var declaration = type as TypedefType; if (declaration != null) { return(GetTypeDefinition(declaration.Declaration.Type, name)); } return(new TypeDefinition { Name = TypeHelper.GetTypeName(type) }); }
public override bool VisitClassDecl(Class @class) { var instance = Context.ReturnVarName; @class = @class.OriginalClass ?? @class; Type returnType = Context.ReturnType.Type.Desugar(); var type = QualifiedIdentifier(@class) + (Context.Driver.Options.GenerateAbstractImpls && @class.IsAbstract ? "Internal" : ""); if (returnType.IsAddress()) { var ret = Generator.GeneratedIdentifier("result") + Context.ParameterIndex; Context.SupportBefore.WriteLine("{0} {1};", type, ret); Context.SupportBefore.WriteLine("if ({0} == IntPtr.Zero) {1} = {2};", instance, ret, @class.IsRefType ? "null" : string.Format("new {0}()", type)); var dtor = @class.Destructors.FirstOrDefault(); var map = @class.IsRefType && dtor != null && dtor.IsVirtual; if (map) { Context.SupportBefore.WriteLine( "else if (CppSharp.Runtime.Helpers.NativeToManagedMap.ContainsKey({0}))", instance); Context.SupportBefore.WriteLineIndent("{0} = ({1}) CppSharp.Runtime.Helpers.NativeToManagedMap[{2}];", ret, type, instance); Context.SupportBefore.WriteLine("else CppSharp.Runtime.Helpers.NativeToManagedMap[{3}] = {0} = {1}.{2}({3});", ret, type, Helpers.CreateInstanceIdentifier, instance); } else { Context.SupportBefore.WriteLine("else {0} = {1}.{2}({3});", ret, type, Helpers.CreateInstanceIdentifier, instance); } Context.Return.Write(ret); } else { Context.Return.Write("{0}.{1}({2})", type, Helpers.CreateInstanceIdentifier, instance); } return(true); }
public static string GetTypeName(Type type) { switch (type) { case PointerType x: return(GetTypeName(x.QualifiedPointee.Type) + "*"); case BuiltinType x: return(GetTypeName(x.Type)); case TypedefType x: return(GetTypeName(x)); case TagType x: return(x.Declaration.Name); case ArrayType x: return(GetTypeName(x.Type) + "[]"); case AttributedType x: return(GetTypeName(PrimitiveType.Void)); default: throw new NotSupportedException(); } }
public static string GetTypeName(Type type) { switch (type) { case PointerType x: return(GetTypeName(x)); case BuiltinType x: return(GetTypeName(x)); case TypedefType x: return(GetTypeName(x)); case TagType x: return(GetTypeName(x)); case ArrayType x: return(GetTypeName(x)); case AttributedType x: return(GetTypeName(x)); default: throw new NotSupportedException(); } }
public override TypePrinterResult VisitPointerType(PointerType pointer, TypeQualifiers quals) { Type pointee = pointer.GetFinalPointee(); if (pointee != null) { if ((pointee is BuiltinType) && pointer.IsPointer()) { if ((pointee as BuiltinType).Type == PrimitiveType.Void) { return(NodeV8IsTypedBuffer); } } TypePrinterResult pointeeType = pointee.Visit(this); return(pointeeType); } throw new NotSupportedException(); }
internal Definitions.TypeDefinition GetTypeDefinition(Type type, string name, bool inStruct) { if (name != null && context.UnitsMap.TryGetValue(name, out var def) && def is Definitions.TypeDefinition t) { return(t); } var visitType = context.TypeVisitor.Visit(type); if (visitType != null) { return(visitType); } switch (type) { case TypedefType declaration when declaration.Declaration.Name == "size_t": return(new Definitions.TypeDefinition { Name = "size_t" }); case TypedefType declaration: return(GetTypeDefinition(declaration.Declaration.Type, declaration.Declaration.Name, inStruct)); case ArrayType arrayType when arrayType.SizeType == ArrayType.ArraySize.Constant: return(GetFieldTypeForFixedArray(arrayType)); case TagType tagType: return(GetFieldTypeForNestedDeclaration(tagType.Declaration, name)); case PointerType pointerType: return(GetTypeDefinitionForPointer(pointerType, name, inStruct)); case FunctionType functionType: return(context.FunctionProcessor.GetDelegateType(functionType, name, inStruct)); default: return(new Definitions.TypeDefinition { Name = TypeHelper.GetTypeName(type) }); } }
public CSharpTypePrinterResult VisitArrayType(ArrayType array, TypeQualifiers quals) { if (ContextKind == CSharpTypePrinterContextKind.Native && array.SizeType == ArrayType.ArraySize.Constant) { Type arrayType = array.Type.Desugar(); PrimitiveType primitiveType; if (arrayType.IsPointerToPrimitiveType(out primitiveType)) { if (primitiveType == PrimitiveType.Void) { return("void**"); } return(string.Format("{0}*", array.Type.Visit(this, quals))); } Class @class; if (arrayType.TryGetClass(out @class)) { return(new CSharpTypePrinterResult() { Type = "fixed byte", NameSuffix = string.Format("[{0}]", array.Size * @class.Layout.Size) }); } // Do not write the fixed keyword multiple times for nested array types var fixedKeyword = array.Type is ArrayType ? string.Empty : "fixed "; return(new CSharpTypePrinterResult() { Type = string.Format("{0}{1}", fixedKeyword, array.Type.Visit(this, quals)), NameSuffix = string.Format("[{0}]", array.Size) }); } return(string.Format("{0}[]", array.Type.Visit(this))); // C# only supports fixed arrays in unsafe sections // and they are constrained to a set of built-in types. }
private void MarshalRefClass(Class @class) { var method = Context.Function as Method; if (method != null && method.Conversion == MethodConversionKind.FunctionToInstanceMethod && Context.ParameterIndex == 0) { Context.Return.Write("{0}", Helpers.InstanceIdentifier); return; } string param = Context.Parameter.Name; Type type = Context.Parameter.Type.Desugar(); if (type.IsAddress()) { Class decl; if (type.TryGetClass(out decl) && decl.IsValueType) { Context.Return.Write("{0}.{1}", param, Helpers.InstanceIdentifier); } else { Context.Return.Write("{0}{1}.{2}", method != null && method.OperatorKind == CXXOperatorKind.EqualEqual ? string.Empty : string.Format("ReferenceEquals({0}, null) ? global::System.IntPtr.Zero : ", param), param, Helpers.InstanceIdentifier, type); } return; } var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier( @class.OriginalClass ?? @class); Context.Return.Write( "ReferenceEquals({0}, null) ? new {1}.Internal() : *({1}.Internal*) ({0}.{2})", param, qualifiedIdentifier, Helpers.InstanceIdentifier); }
private static void ModifyPrimitiveTypeDef(Type type, Func <TypedefType, bool> matchFunc, PrimitiveType primitiveType) { var typedefType = type as TypedefType; if (typedefType != null) { if (matchFunc(typedefType)) { if ((typedefType.Declaration.QualifiedType.Type is BuiltinType) && ((BuiltinType)typedefType.Declaration.QualifiedType.Type).Type == primitiveType) { return; } typedefType.Declaration.QualifiedType = new QualifiedType(new BuiltinType(primitiveType), typedefType.Declaration.QualifiedType.Qualifiers); } } }
private bool WalkBuiltinType(string typeName, out CppSharp.AST.Type builtinType) { string typeNameLower = typeName.ToLower(); switch (typeNameLower) { case "void": builtinType = new BuiltinType(PrimitiveType.Void); return(true); case "bool": builtinType = new BuiltinType(PrimitiveType.Bool); return(true); case "char": builtinType = new BuiltinType(PrimitiveType.Char); return(true); case "signed char": builtinType = new BuiltinType(PrimitiveType.SChar); return(true); case "unsigned char": builtinType = new BuiltinType(PrimitiveType.UChar); return(true); case "short int": builtinType = new BuiltinType(PrimitiveType.Short); return(true); case "short unsigned int": builtinType = new BuiltinType(PrimitiveType.UShort); return(true); case "int": builtinType = new BuiltinType(PrimitiveType.Int); return(true); case "unsigned int": builtinType = new BuiltinType(PrimitiveType.UInt); return(true); case "long int": builtinType = new BuiltinType(PrimitiveType.Long); return(true); case "long unsigned int": builtinType = new BuiltinType(PrimitiveType.ULong); return(true); case "long long int": builtinType = new BuiltinType(PrimitiveType.Int128); return(true); case "long long unsigned int": builtinType = new BuiltinType(PrimitiveType.UInt128); return(true); case "float": builtinType = new BuiltinType(PrimitiveType.Float); return(true); case "double": builtinType = new BuiltinType(PrimitiveType.Double); return(true); } builtinType = null; return(false); }
private bool WalkArrayType(CppType cppType, out CppSharp.AST.Type arrayType) { if (!cppType.IsArray) { arrayType = null; return(false); } // Create new array type arrayType = new ArrayType(); // Set content (arrayType as ArrayType).SizeType = ArraySize.Constant; (arrayType as ArrayType).Size = int.Parse(cppType.ArrayDimension); (arrayType as ArrayType).QualifiedType = new QualifiedType() { Qualifiers = new TypeQualifiers(), Type = WalkType(cppType.TypeName) }; return(true); }
internal TypeDefinition GetTypeDefinition(Type type, string name = null) { switch (type) { case TypedefType declaration: return(GetTypeDefinition(declaration.Declaration.Type, name)); case ArrayType arrayType when arrayType.SizeType == ArrayType.ArraySize.Constant: return(GetFieldTypeForFixedArray(arrayType)); case TagType tagType: return(GetFieldTypeForNestedDeclaration(tagType.Declaration, name)); case PointerType pointerType: return(GetTypeDefinition(pointerType, name)); default: return(new TypeDefinition { Name = TypeHelper.GetTypeName(type) }); } }
private static Property GetProperty(Method method, string name, QualifiedType type, bool isSetter = false) { Type underlyingType = GetUnderlyingType(type); Class @class = (Class)method.Namespace; Property property = @class.Properties.Find( p => p.Field == null && ((!isSetter && p.HasSetter && p.Name == name) || (isSetter && p.HasGetter && GetReadWritePropertyName(p.GetMethod, name) == name)) && ((p.HasGetter && GetUnderlyingType(p.GetMethod.OriginalReturnType).Equals(underlyingType)) || (p.HasSetter && GetUnderlyingType(p.SetMethod.Parameters[0].QualifiedType).Equals(underlyingType)))) ?? new Property { Name = name, QualifiedType = type }; if (property.Namespace == null) { property.Namespace = method.Namespace; property.Access = method.Access; @class.Properties.Add(property); } else { property.Access = (AccessSpecifier)Math.Max( (int)(property.GetMethod ?? property.SetMethod).Access, (int)method.Access); } property.Name = property.OriginalName = name; method.GenerationKind = GenerationKind.Internal; if (method.ExplicitInterfaceImpl != null) { property.ExplicitInterfaceImpl = method.ExplicitInterfaceImpl; } return(property); }
private static bool CheckForEnumValue(Expression arg, Type desugared) { var enumItem = arg.Declaration as Enumeration.Item; if (enumItem != null) { arg.String = string.Format("{0}{1}{2}.{3}", desugared.IsPrimitiveType() ? "(int) " : string.Empty, string.IsNullOrEmpty(enumItem.Namespace.Namespace.Name) ? string.Empty : enumItem.Namespace.Namespace.Name + ".", enumItem.Namespace.Name, enumItem.Name); return true; } var call = arg.Declaration as Function; if ((call != null || arg.Class == StatementClass.BinaryOperator) && arg.String != "0") { string @params = regexFunctionParams.Match(arg.String).Groups[1].Value; TranslateEnumExpression(arg, desugared, @params); return true; } return false; }
public override TypeDefinition Visit(CppSharp.AST.Type type) { if (type is TypedefType typedef) { if (typedef.Declaration.Name.StartsWith("LLVM") && typedef.Declaration.Name.EndsWith("Ref") && typedef.Declaration.Type is PointerType pointerType && pointerType.Pointee is TagType tagType && tagType.Declaration is Class) { if (!Processor.UnitsMap.ContainsKey(typedef.Declaration.Name)) { Processor.AddUnit(new StructDefinition { Name = typedef.Declaration.Name, Fields = new[] { new StructField { Name = "Pointer", FieldType = new TypeDefinition { Name = "void", }.AddModify(TypeDefinition.ModifyType.Pointer), } }, IsComplete = true, }); } return(new TypeDefinition { Name = typedef.Declaration.Name }); } if (typedef.Declaration.Name == "LLVMBool") { return(new TypeDefinition { Name = "bool" }); } } return(null); }
private TypeDefinition GetParameterType(Type type, string name) { if (type is PointerType pointerType && pointerType.QualifiedPointee.Qualifiers.IsConst && pointerType.Pointee is BuiltinType builtinType) { switch (builtinType.Type) { case PrimitiveType.Char: return(new TypeDefinition { Name = "string", Attributes = new[] { "[MarshalAs(UnmanagedType.LPStr)]" } }); case PrimitiveType.Void: return(new TypeDefinition { Name = "void*" }); default: return(new TypeDefinition { Name = TypeHelper.GetTypeName(type) }); } } // edge case when type is array of pointers to none builtin type (type[]* -> type**) if (type is ArrayType arrayType && arrayType.SizeType == ArrayType.ArraySize.Incomplete && arrayType.Type is PointerType arrayPointerType && !(arrayPointerType.Pointee is BuiltinType || arrayPointerType.Pointee is TypedefType typedefType && typedefType.Declaration.Type is BuiltinType)) { return(new TypeDefinition { Name = TypeHelper.GetTypeName(arrayPointerType) + "*" }); } return(_context.StructureProcessor.GetTypeDefinition(type, name)); }
private void MarshalRefClass(Class @class) { var method = Context.Function as Method; if (method != null && method.Conversion == MethodConversionKind.FunctionToInstanceMethod && Context.ParameterIndex == 0) { Context.Return.Write("{0}", Helpers.InstanceIdentifier); return; } string param = Helpers.SafeIdentifier(Context.Parameter.Name); Type type = Context.Parameter.Type.Desugar(); if (type.IsAddress()) { Class decl; if (type.IsTagDecl(out decl) && decl.IsValueType) { Context.Return.Write("{0}.{1}", param, Helpers.InstanceIdentifier); } else { Context.Return.Write("{0} == ({2}) null ? global::System.IntPtr.Zero : {0}.{1}", param, Helpers.InstanceIdentifier, type); } return; } var qualifiedIdentifier = CSharpMarshalNativeToManagedPrinter.QualifiedIdentifier( @class.OriginalClass ?? @class); Context.Return.Write( "{1} == ({0}) null ? new {0}.Internal() : *({0}.Internal*) ({1}.{2})", qualifiedIdentifier, param, Helpers.InstanceIdentifier); }
public override bool VisitClassDecl(Class @class) { var instance = Context.ReturnVarName; @class = @class.OriginalClass ?? @class; Type returnType = Context.ReturnType.Type.Desugar(); var type = QualifiedIdentifier(@class) + (Context.Driver.Options.GenerateAbstractImpls && @class.IsAbstract ? "Internal" : ""); if (returnType.IsAddress()) { Context.Return.Write("({0} == IntPtr.Zero) ? {1} : {2}.{3}({0})", instance, @class.IsRefType ? "null" : string.Format("new {0}()", type), type, Helpers.CreateInstanceIdentifier); } else { Context.Return.Write("{0}.{1}({2})", type, Helpers.CreateInstanceIdentifier, instance); } return(true); }
public static string GetTypeName(Type type) { if (type is PointerType) { return(GetTypeName((PointerType)type)); } if (type is BuiltinType) { return(GetTypeName((BuiltinType)type)); } if (type is TypedefType) { return(GetTypeName((TypedefType)type)); } if (type is TagType) { return(GetTypeName((TagType)type)); } if (type is ArrayType) { return(GetTypeName((ArrayType)type)); } throw new NotSupportedException(); }
private static bool CheckForEnumValue(Expression arg, Type desugared) { var enumItem = arg.Declaration as Enumeration.Item; if (enumItem != null) { arg.String = string.Format("{0}{1}{2}.{3}", desugared.IsPrimitiveType() ? "(int) " : string.Empty, string.IsNullOrEmpty(enumItem.Namespace.Namespace.Name) ? string.Empty : enumItem.Namespace.Namespace.Name + ".", enumItem.Namespace.Name, enumItem.Name); return(true); } var call = arg.Declaration as Function; if ((call != null || arg.Class == StatementClass.BinaryOperator) && arg.String != "0") { string @params = regexFunctionParams.Match(arg.String).Groups[1].Value; TranslateEnumExpression(arg, desugared, @params); return(true); } return(false); }
private static void CheckForULongValue(Parameter parameter, Type desugared) { ulong value; string @default = parameter.DefaultArgument.String; // HACK: .NET's Parse/TryParse have a bug preventing them from parsing UL-suffixed ulongs if (desugared.IsPrimitiveType() && @default.EndsWith("UL")) @default = @default.Substring(0, @default.Length - 2); if (ulong.TryParse(@default, out value)) parameter.DefaultArgument.String = value.ToString(CultureInfo.InvariantCulture); }
private static void TranslateEnumExpression(Expression arg, Type desugared, string @params) { if (@params.Contains("::")) arg.String = regexDoubleColon.Replace(@params, desugared + "."); else arg.String = regexName.Replace(@params, desugared + ".$1"); }
private void CheckFloatSyntax(Type desugared, Parameter parameter) { var builtin = desugared as BuiltinType; if (builtin != null && builtin.Type == AST.PrimitiveType.Float && parameter.DefaultArgument.String.EndsWith(".F")) { parameter.DefaultArgument.String = parameter.DefaultArgument.String.Replace(".F", ".0F"); } }
private bool? CheckForDefaultConstruct(Type desugared, Expression arg) { // Unwrapping the underlying type behind a possible pointer/reference Type type; desugared.IsPointerTo(out type); type = type ?? desugared; Class decl; if (!type.TryGetClass(out decl)) return false; var ctor = arg.Declaration as Method; TypeMap typeMap; if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap)) { Type typeInSignature; string mappedTo; if (Driver.Options.IsCSharpGenerator) { var typePrinterContext = new CSharpTypePrinterContext { CSharpKind = CSharpTypePrinterContextKind.Managed, Type = type }; typeInSignature = typeMap.CSharpSignatureType(typePrinterContext).SkipPointerRefs(); mappedTo = typeMap.CSharpSignature(typePrinterContext); } else { var typePrinterContext = new CLITypePrinterContext { Type = type }; typeInSignature = typeMap.CLISignatureType(typePrinterContext).SkipPointerRefs(); mappedTo = typeMap.CLISignature(typePrinterContext); } Enumeration @enum; if (typeInSignature.TryGetEnum(out @enum)) { return false; } if (ctor == null || !ctor.IsConstructor) return false; if (mappedTo == "string" && ctor.Parameters.Count == 0) { arg.String = "\"\""; return true; } } if (regexCtor.IsMatch(arg.String)) { arg.String = string.Format("new {0}", arg.String); if (ctor != null && ctor.Parameters.Count > 0 && ctor.Parameters[0].Type.IsAddress()) { arg.String = arg.String.Replace("(0)", "()"); return decl.IsValueType ? true : (bool?) null; } } else { if (ctor != null && ctor.Parameters.Count > 0) { var finalPointee = ctor.Parameters[0].Type.SkipPointerRefs().Desugar(); Enumeration @enum; if (finalPointee.TryGetEnum(out @enum)) TranslateEnumExpression(arg, finalPointee, arg.String); } } return decl.IsValueType ? true : (bool?) null; }
private void CheckForDefaultEmptyChar(Parameter parameter, Type desugared) { if (parameter.DefaultArgument.String == "0" && Driver.Options.MarshalCharAsManagedChar && desugared.IsPrimitiveType(PrimitiveType.Char)) { parameter.DefaultArgument.String = "'\\0'"; } }
public override bool VisitType(Type type, TypeQualifiers quals) { TypeMap typeMap; if (Context.Driver.TypeDatabase.FindTypeMap(type, out typeMap) && typeMap.DoesMarshalling) { typeMap.Type = type; typeMap.CSharpMarshalToManaged(Context); return false; } return true; }
private bool? CheckForDefaultConstruct(Type desugared, Parameter parameter) { Method ctor = parameter.DefaultArgument.Declaration as Method; if (ctor == null || !ctor.IsConstructor) return false; Type type; desugared.IsPointerTo(out type); type = type ?? desugared; Class decl; if (!type.TryGetClass(out decl)) return false; TypeMap typeMap; if (Driver.TypeDatabase.FindTypeMap(decl, type, out typeMap)) { string mappedTo; if (Driver.Options.IsCSharpGenerator) { var typePrinterContext = new CSharpTypePrinterContext { CSharpKind = CSharpTypePrinterContextKind.Managed, Type = type }; mappedTo = typeMap.CSharpSignature(typePrinterContext); } else { var typePrinterContext = new CLITypePrinterContext { Type = type }; mappedTo = typeMap.CLISignature(typePrinterContext); } if (mappedTo == "string" && ctor.Parameters.Count == 0) { parameter.DefaultArgument.String = "\"\""; return true; } } parameter.DefaultArgument.String = string.Format("new {0}", parameter.DefaultArgument.String); if (ctor.Parameters.Count > 0 && ctor.Parameters[0].OriginalName == "_0") parameter.DefaultArgument.String = parameter.DefaultArgument.String.Replace("(0)", "()"); return decl.IsValueType ? true : (bool?) null; }
public bool FindTypeMapRecursive(Type type, out TypeMap typeMap) { while (true) { if (FindTypeMap(type, out typeMap)) return true; var desugaredType = type.Desugar(); if (desugaredType == type) return false; type = desugaredType; } }
public bool FindTypeMap(Type type, out TypeMap typeMap) { var typePrinter = new CppTypePrinter(this); var template = type as TemplateSpecializationType; if (template != null) return FindTypeMap(template.Template.TemplatedDecl, type, out typeMap); if (FindTypeMap(type.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } typePrinter.PrintKind = CppTypePrintKind.Qualified; if (FindTypeMap(type.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } return false; }
public bool FindTypeMap(Declaration decl, Type type, out TypeMap typeMap) { // We try to find type maps from the most qualified to less qualified // types. Example: '::std::vector', 'std::vector' and 'vector' var typePrinter = new CppTypePrinter(this) { PrintKind = CppTypePrintKind.GlobalQualified }; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } typePrinter.PrintKind = CppTypePrintKind.Qualified; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } typePrinter.PrintKind = CppTypePrintKind.Local; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } return false; }
public bool FindTypeMap(Declaration decl, Type type, out TypeMap typeMap) { // We try to find type maps from the most qualified to less qualified // types. Example: '::std::vector', 'std::vector' and 'vector' var typePrinter = new CppTypePrinter { PrintLogicalNames = true }; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } typePrinter.PrintScopeKind = CppTypePrintScopeKind.Qualified; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } typePrinter.PrintScopeKind = CppTypePrintScopeKind.Local; if (FindTypeMap(decl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } var specialization = decl as ClassTemplateSpecialization; if (specialization != null && FindTypeMap(specialization.TemplatedDecl.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } var typedef = decl as TypedefDecl; return typedef != null && FindTypeMap(typedef.Type, out typeMap); }
public bool FindTypeMap(Type type, out TypeMap typeMap) { var typePrinter = new CppTypePrinter { PrintTypeQualifiers = false, PrintTypeModifiers = false, PrintLogicalNames = true }; var template = type as TemplateSpecializationType; if (template != null) { var specialization = template.GetClassTemplateSpecialization(); if (specialization != null && FindTypeMap(specialization, type, out typeMap)) return true; if (template.Template.TemplatedDecl != null) return FindTypeMap(template.Template.TemplatedDecl, type, out typeMap); } if (FindTypeMap(type.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } if (FindTypeMap(type.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } typePrinter.PrintScopeKind = CppTypePrintScopeKind.Qualified; if (FindTypeMap(type.Visit(typePrinter), out typeMap)) { typeMap.Type = type; return true; } var typedef = type as TypedefType; return typedef != null && FindTypeMap(typedef.Declaration, type, out typeMap); }