private void AddValueStruct(GLibType type) { // System.Console.WriteLine($"Adding value struct {type.FullName}"); // _sb.AppendFormat("\tpublic struct {0}\n", type.Name); // _sb.Append("\t{\n"); // _sb.Append("\t}\n"); }
internal TypeName AddType(string name, GLibType type) { if (type == null) { throw new ArgumentNullException(nameof(type)); } if (type.Namespace != null) { throw new ArgumentException($"Type '{name}' is already added to namespace '{type.Namespace.Name}'"); } TypeName?id = null; if (name != null) { id = FindTypeName(name); } if (!id.HasValue) { id = CreateNewTypeName(name); } TypeName typeName = id.Value; type.TypeName = typeName; if (typeName.Type != null) { throw new InvalidOperationException($"Type '{name}' already defined in '{Name}'."); } if (_types[typeName.Index] == null) { _types[typeName.Index] = type; } return(typeName); }
private void AddEnum(GLibType type, List <Member> members, bool flags) { if (flags) { _sb.Append("\t[Flags]\n"); } _sb.AppendFormat("\tpublic enum {0}\n", type.Name); _sb.Append("\t{\n"); foreach (var member in members) { string value = member.Value; if (!int.TryParse(value, out int res)) { if (uint.TryParse(value, out uint uintvalue)) { value = $"unchecked((int){value}U)"; } else { // skip continue; } } // TODO: prettify name _sb.AppendFormat("\t\t{0} = {1},\n", EscapeIdentifier(member.Name), value); } _sb.Append("\t}\n"); }
private TypeAnalysis GetTypeInfo(GLibType type) { TypeAnalysis info; if (!_typeAnalyses.TryGetValue(type, out info)) { info = new TypeAnalysis(type); _typeAnalyses.Add(type, info); } return(info); }
internal static (string cType, GLibType type) ResolveType(string cType, GLibType type) { cType = cType.Replace("gpointer", "void*"); cType = cType.Replace("gconstpointer", "void*"); if (type is RecordType rec && rec.Disguised) { cType += "*"; } if (type is AliasType a) { cType = cType.Replace(a.CIdentifier, a.TargetCType); return(ResolveType(cType, a.AliasedType)); } else { return(cType, type); } }
private static List <Function> GetFunctions(GLibType type) { switch (type) { case BitfieldType b: return(b.Functions); case ClassType c: return(c.Functions); case InterfaceType i: return(i.Functions); case EnumerationType e: return(e.Functions); case RecordType r: return(r.Functions); case UnionType u: return(u.Functions); default: return(null); } }
private bool InheritsGObject(GLibType type) { if (type == null) { return(false); } if (type.FullName == "GObject.Object") { return(true); } else if (type is ClassType cls) { return(InheritsGObject(cls.Parent)); } else { return(false); } }
private (string modifier, string typeName) MarshallAsType(Parameter p, Function function) { GLibType type = p.Type; string cType = p.CType; (cType, type) = ResolveType(cType, type); TypeAnalysis info; if (_typeAnalyses.TryGetValue(type, out info)) { return(info.DetermineInteropType(p)); } else if (cType.EndsWith("*")) { return(string.Empty, "System.IntPtr"); } else { return(null, null); } }
private void AddRefStruct(GLibType type) { string getGLibType = null; switch (type) { case ClassType cls: getGLibType = cls.GLibGetType; break; case RecordType rec: getGLibType = rec.GLibGetType; break; case InterfaceType interf: getGLibType = interf.GLibGetType; break; } _sb.AppendFormat("\tpublic ref struct {0}\n", type.Name); _sb.Append("\t{\n"); _sb.Append("\t\tprivate IntPtr _pointer;\n"); _sb.AppendFormat("\t\tpublic {0}(IntPtr pointer, bool checkType = false)\n", type.Name); _sb.Append("\t\t{\n"); if (getGLibType != null && InheritsGObject(type)) { // g_type_check_instance_is_a _sb.Append("\t\t\tif (checkType)\n"); _sb.Append("\t\t\t{\n"); _sb.Append("\t\t\t\tGObject.ObjectType.CheckInstanceIsA(pointer, TypeOf());\n"); _sb.Append("\t\t\t}\n"); } _sb.Append("\t\t\t_pointer = pointer;\n"); _sb.Append("\t\t}\n"); _sb.AppendFormat("\t\tpublic static explicit operator {0}(IntPtr pointer) => new {0}(pointer, checkType: true);\n", type.Name); _sb.AppendFormat("\t\tpublic static explicit operator IntPtr({0} value) => value._pointer;\n", type.Name); if (type is ClassType clsType) { ClassType parent = clsType.Parent as ClassType; while (parent != null) { _sb.AppendFormat("\t\tpublic static implicit operator {0}({1} value) => new {0}((IntPtr)value, checkType: false);\n", parent.FullName, type.Name); _sb.AppendFormat("\t\tpublic static explicit operator {0}({1} value) => new {0}((IntPtr)value, checkType: true);\n", type.Name, parent.FullName); parent = parent.Parent as ClassType; } } GLibType functionType = type; HashSet <string> functionNames = new HashSet <string>(); do { var functions = GetFunctions(functionType); // TODO: skip deprecated if (functions != null) { foreach (Function function in functions) { (bool unknownType, ParameterInfo returnParameter, ParameterInfo[] argumentParameters) = InspectParameters(function); if (unknownType) { continue; } if (functionNames.Contains(function.Name)) { continue; } functionNames.Add(function.Name); switch (function.Kind) { case FunctionKind.Constructor: case FunctionKind.Function: if (functionType == type) { _sb.AppendFormat("\t\tpublic static {0} {1}", returnParameter.Type, EscapeIdentifier(function.Name)); // TODO: prettify AddArgumentList(param => $"{param.Modifier}{param.Type} {param.Name}", argumentParameters); _sb.AppendFormat(" => {0}Interop.{1}", type.Namespace.Name, function.CIdentifier); AddArgumentList(param => $"{param.Modifier}{param.Name}", argumentParameters); _sb.Append(";\n"); } break; case FunctionKind.Method: _sb.AppendFormat("\t\tpublic {0} {1}", returnParameter.Type, EscapeIdentifier(function.Name)); // TODO: prettify AddArgumentList(param => $"{param.Modifier}{param.Type} {param.Name}", argumentParameters.Skip(1).ToList()); _sb.AppendFormat(" => {0}.{0}Interop.{1}", functionType.Namespace.Name, function.CIdentifier); argumentParameters[0].Name = "this"; AddArgumentList(param => $"{param.Modifier}{param.Name}", argumentParameters); _sb.Append(";\n"); break; } } } functionType = (functionType as ClassType)?.Parent; } while (functionType != null); if (getGLibType != null) { string libraryName = type.Namespace.SharedLibraries[0]; _sb.AppendFormat("\t\t[DllImport(\"{0}\", EntryPoint = \"{1}\")]\n", libraryName, getGLibType); _sb.AppendFormat("\t\tpublic static extern GLib.GType TypeOf();\n"); } _sb.Append("\t}\n"); }