protected virtual bool CanGenerate (GenerationInfo gen_info, ObjectBase implementor) { if (implementor != null || this.CName.Length == 0 || CodeType == VMCodeType.None || (CodeType == VMCodeType.Glue && !gen_info.GlueEnabled)) return false; else return true; }
protected override void GenerateUnmanagedInvocation (GenerationInfo gen_info, ObjectBase implementor) { if (!base.CanGenerate (gen_info, implementor)) GenerateChainVirtualMethod (gen_info.Writer, implementor); else base.GenerateUnmanagedInvocation (gen_info, implementor); }
public override void Generate(GenerationInfo gen_info) { gen_info.CurrentType = Name; StreamWriter sw = gen_info.Writer = gen_info.OpenStream(Name); sw.WriteLine ("namespace " + NS + " {"); sw.WriteLine (); sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); sw.WriteLine ("#region Autogenerated code"); if (IsDeprecated) sw.WriteLine ("\t[Obsolete]"); sw.Write ("\t{0} class " + Name, IsInternal ? "internal" : "public"); sw.WriteLine (" {"); sw.WriteLine (); GenProperties (gen_info, null); GenMethods (gen_info, null, null); sw.WriteLine ("#endregion"); AppendCustom(sw, gen_info.CustomDir); sw.WriteLine ("\t}"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = null; }
public void Generate (GenerationInfo gen_info, ObjectBase implementor) { if (!CanGenerate (gen_info, implementor)) throw new NotSupportedException (String.Format ("Cannot generate virtual method {0}.{1}. Make sure a writable glue path was provided to the generator.", container_type.Name, this.CallString)); GenerateOverride (gen_info, implementor); GenerateCallback (gen_info.Writer, implementor); if (!IsStatic) GenerateUnmanagedInvocation (gen_info, implementor); }
protected override void GenerateOverride (GenerationInfo gen_info, ObjectBase implementor) { StreamWriter sw = gen_info.Writer; if (!base.CanGenerate (gen_info, implementor)) { GenerateOverrideBody (sw); sw.WriteLine ("\t\t\tOverrideVirtualMethod (gtype, \"{0}\", callback);", signal_name); sw.WriteLine ("\t\t}"); } else base.GenerateOverride (gen_info, implementor); }
public virtual void Generate (GenerationInfo gen_info, string indent) { StreamWriter sw = gen_info.Writer; sw.WriteLine ("{0}public const {1} {2} = {3}{4}{5};", indent, ConstType, Name, IsString ? "@\"": String.Empty, value, IsString ? "\"": String.Empty); }
public void Initialize (GenerationInfo gen_info, bool is_get, bool is_set, string indent) { if (parameters.Count == 0) return; StreamWriter sw = gen_info.Writer; for (int i = 0; i < parameters.Count; i++) { Parameter p = parameters [i]; IGeneratable gen = p.Generatable; string name = p.Name; if (is_set) name = "value"; p.CallName = name; foreach (string prep in p.Prepare) sw.WriteLine (indent + "\t\t\t" + prep); if (gen is CallbackGen) { CallbackGen cbgen = gen as CallbackGen; string wrapper = cbgen.GenWrapper(gen_info); switch (p.Scope) { case "notified": sw.WriteLine (indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name); sw.WriteLine (indent + "\t\t\tIntPtr {0};", parameters [i + 1].Name); sw.WriteLine (indent + "\t\t\t{0} {1};", parameters [i + 2].CSType, parameters [i + 2].Name); sw.WriteLine (indent + "\t\t\tif ({0} == null) {{", name); sw.WriteLine (indent + "\t\t\t\t{0} = IntPtr.Zero;", parameters [i + 1].Name); sw.WriteLine (indent + "\t\t\t\t{0} = null;", parameters [i + 2].Name); sw.WriteLine (indent + "\t\t\t} else {"); sw.WriteLine (indent + "\t\t\t\t{0} = (IntPtr) GCHandle.Alloc ({1}_wrapper);", parameters [i + 1].Name, name); sw.WriteLine (indent + "\t\t\t\t{0} = GLib.DestroyHelper.NotifyHandler;", parameters [i + 2].Name, parameters [i + 2].CSType); sw.WriteLine (indent + "\t\t\t}"); break; case "async": sw.WriteLine (indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name); sw.WriteLine (indent + "\t\t\t{0}_wrapper.PersistUntilCalled ();", name); break; case "call": default: if (p.Scope == String.Empty) Console.WriteLine ("Defaulting " + gen.Name + " param to 'call' scope in method " + gen_info.CurrentMember); sw.WriteLine (indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name); break; } } } if (ThrowsException) sw.WriteLine (indent + "\t\t\tIntPtr error = IntPtr.Zero;"); }
public override void Generate(GenerationInfo gen_info) { Method copy = methods["Copy"] as Method; Method free = methods["Free"] as Method; methods.Remove ("Copy"); methods.Remove ("Free"); gen_info.CurrentType = QualifiedName; StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name); base.Generate (gen_info); sw.WriteLine ("\t\tpublic static explicit operator GLib.Value (" + QualifiedName + " boxed)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tGLib.Value val = GLib.Value.Empty;"); sw.WriteLine ("\t\t\tval.Init (" + QualifiedName + ".GType);"); sw.WriteLine ("\t\t\tval.Val = boxed;"); sw.WriteLine ("\t\t\treturn val;"); sw.WriteLine ("\t\t}"); sw.WriteLine (); sw.WriteLine ("\t\tpublic static explicit operator " + QualifiedName + " (GLib.Value val)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\treturn (" + QualifiedName + ") val.Val;"); sw.WriteLine ("\t\t}"); if (copy != null && copy.IsDeprecated) { sw.WriteLine (); sw.WriteLine ("\t\t[Obsolete(\"This is a no-op\")]"); sw.WriteLine ("\t\tpublic " + QualifiedName + " Copy() {"); sw.WriteLine ("\t\t\treturn this;"); sw.WriteLine ("\t\t}"); } if (free != null && free.IsDeprecated) { sw.WriteLine (); sw.WriteLine ("\t\t[Obsolete(\"This is a no-op\")]"); sw.WriteLine ("\t\tpublic " + QualifiedName + " Free () {"); sw.WriteLine ("\t\t\treturn this;"); sw.WriteLine ("\t\t}"); } sw.WriteLine ("#endregion"); AppendCustom(sw, gen_info.CustomDir); sw.WriteLine ("\t}"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = null; Statistics.BoxedCount++; }
public override void Generate(GenerationInfo gen_info) { Method copy = methods["Copy"] as Method; methods.Remove ("Copy"); methods.Remove ("Free"); gen_info.CurrentType = Name; StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name); base.Generate (gen_info); sw.WriteLine ("\t\t[DllImport(\"glibsharpglue-2\")]"); sw.WriteLine ("\t\tstatic extern IntPtr glibsharp_value_get_boxed (ref GLib.Value val);"); sw.WriteLine (); sw.WriteLine ("\t\t[DllImport(\"glibsharpglue-2\")]"); sw.WriteLine ("\t\tstatic extern void glibsharp_value_set_boxed (ref GLib.Value val, ref " + QualifiedName + " boxed);"); sw.WriteLine (); sw.WriteLine ("\t\tpublic static explicit operator GLib.Value (" + QualifiedName + " boxed)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tGLib.Value val = GLib.Value.Empty;"); sw.WriteLine ("\t\t\tval.Init (" + QualifiedName + ".GType);"); sw.WriteLine ("\t\t\tglibsharp_value_set_boxed (ref val, ref boxed);"); sw.WriteLine ("\t\t\treturn val;"); sw.WriteLine ("\t\t}"); sw.WriteLine (); sw.WriteLine ("\t\tpublic static explicit operator " + QualifiedName + " (GLib.Value val)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tIntPtr boxed_ptr = glibsharp_value_get_boxed (ref val);"); sw.WriteLine ("\t\t\treturn New (boxed_ptr);"); sw.WriteLine ("\t\t}"); if (copy != null && copy.IsDeprecated) { sw.WriteLine (); sw.WriteLine ("\t\t[Obsolete(\"This is a no-op\")]"); sw.WriteLine ("\t\tpublic " + QualifiedName + " Copy() {"); sw.WriteLine ("\t\t\treturn this;"); sw.WriteLine ("\t\t}"); } sw.WriteLine ("#endregion"); AppendCustom(sw, gen_info.CustomDir); sw.WriteLine ("\t}"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = null; Statistics.BoxedCount++; }
public override void Generate (GenerationInfo gen_info) { StreamWriter sw = gen_info.OpenStream (Name); sw.WriteLine ("namespace " + NS + " {"); sw.WriteLine (); sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); sw.WriteLine ("#region Autogenerated code"); if (Elem.GetAttribute("type") == "flags") sw.WriteLine ("\t[Flags]"); if (Elem.HasAttribute("gtype")) sw.WriteLine ("\t[Gst.GLib.GType (typeof (" + NS + "." + Name + "GType))]"); string access = IsInternal ? "internal" : "public"; sw.WriteLine ("\t" + access + " enum " + Name + enum_type + " {"); sw.WriteLine (); foreach (string member in members) sw.WriteLine (member); sw.WriteLine ("\t}"); if (Elem.HasAttribute ("gtype")) { sw.WriteLine (); sw.WriteLine ("\tinternal class " + Name + "GType {"); sw.WriteLine ("\t\t[DllImport (\"" + LibraryName + "\", CallingConvention = CallingConvention.Cdecl)]"); sw.WriteLine ("\t\tstatic extern IntPtr " + Elem.GetAttribute ("gtype") + " ();"); sw.WriteLine (); sw.WriteLine ("\t\tpublic static Gst.GLib.GType GType {"); sw.WriteLine ("\t\t\tget {"); sw.WriteLine ("\t\t\t\treturn new Gst.GLib.GType (" + Elem.GetAttribute ("gtype") + " ());"); sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t}"); sw.WriteLine ("\t}"); } sw.WriteLine ("#endregion"); sw.WriteLine ("}"); sw.Close (); Statistics.EnumCount++; }
void GenerateStatic (GenerationInfo gen_info) { StreamWriter sw = gen_info.Writer; sw.WriteLine("\t\t" + Protection + " static " + Safety + Modifiers + name + " " + StaticName + "(" + Signature + ")"); sw.WriteLine("\t\t{"); Body.Initialize(gen_info, false, false, ""); sw.Write("\t\t\t" + name + " result = "); if (container_type is StructBase) sw.Write ("{0}.New (", name); else sw.Write ("new {0} (", name); sw.WriteLine (CName + "(" + Body.GetCallString (false) + "));"); Body.Finish (sw, ""); Body.HandleException (sw, ""); sw.WriteLine ("\t\t\treturn result;"); }
public override void Generate(GenerationInfo gen_info) { gen_info.CurrentType = QualifiedName; StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name); base.Generate (gen_info); if (GetMethod ("GetType") == null && GetMethod ("GetGType") == null) { sw.WriteLine ("\t\tprivate static GLib.GType GType {"); sw.WriteLine ("\t\t\tget { return GLib.GType.Pointer; }"); sw.WriteLine ("\t\t}"); } sw.WriteLine ("#endregion"); sw.WriteLine ("\t}"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = null; Statistics.StructCount++; }
public override void Generate (GenerationInfo gen_info) { bool need_close = false; if (gen_info.Writer == null) { gen_info.Writer = gen_info.OpenStream (Name, NS); need_close = true; } StreamWriter sw = gen_info.Writer; sw.WriteLine ("namespace " + NS + " {"); sw.WriteLine (); sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System.Collections;"); sw.WriteLine ("\tusing System.Collections.Generic;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); sw.WriteLine ("#region Autogenerated code"); if (IsDeprecated) sw.WriteLine ("\t[Obsolete]"); string access = IsInternal ? "internal" : "public"; sw.WriteLine ("\t" + access + " partial class {0} : {1} IEquatable<{0}> {{", Name, Parent == null ? "GLib.IWrapper," : (Parent.QualifiedName + ",")); sw.WriteLine (); GenNativeStruct (gen_info); GenNativeAccessor (gen_info); GenFields (gen_info); sw.WriteLine (); GenCtors (gen_info); GenMethods (gen_info, null, this); GenEqualsAndHash (sw); if (!need_close) return; sw.WriteLine ("#endregion"); sw.WriteLine ("\t}"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = null; }
public override void Generate(GenerationInfo gen_info) { GenerateAdapter (gen_info); StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name); sw.WriteLine ("namespace " + NS + " {"); sw.WriteLine (); sw.WriteLine ("\tusing System;"); sw.WriteLine (); sw.WriteLine ("#region Autogenerated code"); string access = IsInternal ? "internal" : "public"; sw.WriteLine ("\t" + access + " interface " + Name + " : GLib.IWrapper {"); sw.WriteLine (); foreach (Signal sig in sigs.Values) { sig.GenerateDecl (sw); sig.GenEventHandler (gen_info); } foreach (Method method in methods.Values) { if (IgnoreMethod (method, this)) continue; method.GenerateDecl (sw); } foreach (Property prop in props.Values) prop.GenerateDecl (sw, "\t\t"); AppendCustom (sw, gen_info.CustomDir); sw.WriteLine ("\t}"); GenerateImplementorIface (sw); sw.WriteLine ("#endregion"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = null; Statistics.IFaceCount++; }
public override void Generate (GenerationInfo gen_info, string indent) { if (Hidden) return; visible = Access != "private"; StreamWriter sw = gen_info.Writer; SymbolTable table = SymbolTable.Table; string wrapped = table.GetCSType (CType); string wrapped_name = SymbolTable.Table.MangleName (CName); IGeneratable gen = table [CType]; if (IsArray && !IsNullTermArray) { sw.WriteLine (indent + "[MarshalAs (UnmanagedType.ByValArray, SizeConst=" + ArrayLength + ")]"); sw.WriteLine (indent + "{0} {1} {2};", Access, CSType, StudlyName); } else if (IsArray && IsNullTermArray) { sw.WriteLine (indent + "private {0} {1};", "IntPtr", StudlyName+ "Ptr"); if ((Readable || Writable) && Access == "public") { sw.WriteLine (indent + "public {0} {1} {{", CSType, StudlyName); if (Readable) sw.WriteLine (indent + "\tget {{ return GLib.Marshaller.StructArrayFromNullTerminatedIntPtr<{0}> ({1}); }}", base.CSType, StudlyName + "Ptr"); if (Writable) sw.WriteLine (indent + "\tset {{ {0} = GLib.Marshaller.StructArrayToNullTerminatedStructArrayIntPtr<{1}> (value); }}", StudlyName + "Ptr", base.CSType); sw.WriteLine (indent + "}"); } } else if (IsBitfield) { base.Generate (gen_info, indent); } else if (gen is IAccessor) { sw.WriteLine (indent + "private {0} {1};", gen.MarshalType, Name); if (Access != "private") { IAccessor acc = table [CType] as IAccessor; sw.WriteLine (indent + Access + " " + wrapped + " " + StudlyName + " {"); acc.WriteAccessors (sw, indent + "\t", Name); sw.WriteLine (indent + "}"); } } else if (IsPointer && (gen is StructGen || gen is BoxedGen || gen is UnionGen)) { sw.WriteLine (indent + "private {0} {1};", CSType, Name); sw.WriteLine (); if (Access != "private") { sw.WriteLine (indent + Access + " " + wrapped + " " + wrapped_name + " {"); sw.WriteLine (indent + "\tget { return " + table.FromNative (CType, Name) + "; }"); sw.WriteLine (indent + "}"); } } else if (IsPointer && CSType != "string") { // FIXME: probably some fields here which should be visible. visible = false; sw.WriteLine (indent + "private {0} {1};", CSType, Name); } else { sw.WriteLine (indent + "{0} {1} {2};", Access, CSType, Access == "public" ? StudlyName : Name); } }
public void Generate(GenerationInfo gen_info, ObjectBase implementor) { StreamWriter sw = gen_info.Writer; if (implementor == null) GenEventHandler (gen_info); GenEvent (sw, implementor, "this"); Statistics.SignalCount++; }
public void GenVirtualMethods(GenerationInfo gen_info, ObjectBase implementor) { foreach (GObjectVM vm in virtual_methods) vm.Generate (gen_info, implementor); }
public virtual void Generate(GenerationInfo gen_info, string indent) { if (Ignored || Hidden) { return; } CheckGlue(); if ((getterName != null || setterName != null || getOffsetName != null) && gen_info.GlueWriter == null) { Console.WriteLine("No glue-filename specified, can't create glue for {0}.{1}", container_type.Name, Name); return; } GenerateImports(gen_info, indent); SymbolTable table = SymbolTable.Table; StreamWriter sw = gen_info.Writer; string modifiers = elem.HasAttribute("new_flag") ? "new " : ""; bool is_struct = table.IsStruct(CType) || table.IsBoxed(CType); sw.WriteLine(indent + "public " + modifiers + CSType + " " + Name + " {"); if (Getter != null) { sw.Write(indent + "\tget "); Getter.GenerateBody(gen_info, container_type, "\t"); sw.WriteLine(""); } else if (getterName != null) { sw.WriteLine(indent + "\tget {"); container_type.Prepare(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\t" + CSType + " result = " + table.FromNativeReturn(ctype, getterName + " (" + container_type.CallByName() + ")") + ";"); container_type.Finish(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\treturn result;"); sw.WriteLine(indent + "\t}"); } else if (Readable && offsetName != null) { sw.WriteLine(indent + "\tget {"); sw.WriteLine(indent + "\t\tunsafe {"); if (is_struct) { sw.WriteLine(indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\treturn *raw_ptr;"); } else { sw.WriteLine(indent + "\t\t\t" + table.GetMarshalReturnType(CType) + "* raw_ptr = (" + table.GetMarshalReturnType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\treturn " + table.FromNativeReturn(ctype, "(*raw_ptr)") + ";"); } sw.WriteLine(indent + "\t\t}"); sw.WriteLine(indent + "\t}"); } if (Setter != null) { sw.Write(indent + "\tset "); Setter.GenerateBody(gen_info, container_type, "\t"); sw.WriteLine(""); } else if (setterName != null) { sw.WriteLine(indent + "\tset {"); container_type.Prepare(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\t" + setterName + " (" + container_type.CallByName() + ", " + table.CallByName(ctype, "value") + ");"); container_type.Finish(sw, indent + "\t\t"); sw.WriteLine(indent + "\t}"); } else if (Writable && offsetName != null) { sw.WriteLine(indent + "\tset {"); sw.WriteLine(indent + "\t\tunsafe {"); if (is_struct) { sw.WriteLine(indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\t*raw_ptr = value;"); } else { sw.WriteLine(indent + "\t\t\t" + table.GetMarshalReturnType(CType) + "* raw_ptr = (" + table.GetMarshalReturnType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\t*raw_ptr = " + table.ToNativeReturn(ctype, "value") + ";"); } sw.WriteLine(indent + "\t\t}"); sw.WriteLine(indent + "\t}"); } sw.WriteLine(indent + "}"); sw.WriteLine(""); if (getterName != null || setterName != null || getOffsetName != null) { GenerateGlue(gen_info); } }
void GenerateImplementorIface(GenerationInfo gen_info) { StreamWriter sw = gen_info.Writer; if (IsConsumeOnly) { return; } sw.WriteLine(); sw.WriteLine("\t[GLib.GInterface (typeof (" + Name + "Adapter))]"); string access = IsInternal ? "internal" : "public"; sw.WriteLine("\t" + access + " partial interface " + Name + "Implementor : GLib.IWrapper {"); sw.WriteLine(); Hashtable vm_table = new Hashtable(); foreach (InterfaceVM vm in interface_vms) { vm_table [vm.Name] = vm; } foreach (InterfaceVM vm in interface_vms) { if (vm_table [vm.Name] == null) { continue; } else if (!vm.IsValid) { vm_table.Remove(vm.Name); continue; } else if (vm.IsGetter || vm.IsSetter) { string cmp_name = (vm.IsGetter ? "Set" : "Get") + vm.Name.Substring(3); InterfaceVM cmp = vm_table [cmp_name] as InterfaceVM; if (cmp != null && (cmp.IsGetter || cmp.IsSetter)) { if (vm.IsSetter) { cmp.GenerateDeclaration(sw, vm); } else { vm.GenerateDeclaration(sw, cmp); } vm_table.Remove(cmp.Name); } else { vm.GenerateDeclaration(sw, null); } vm_table.Remove(vm.Name); } else { vm.GenerateDeclaration(sw, null); vm_table.Remove(vm.Name); } } AppendCustom(sw, gen_info.CustomDir, Name + "Implementor"); sw.WriteLine("\t}"); }
void GenerateUnmanagedInvocation_managed(GenerationInfo gen_info) { StreamWriter sw = gen_info.Writer; string native_call = "this.Handle"; if (parms.Count > 0) native_call += ", " + Body.GetCallString (false); this.GenerateMethodBody (sw, null); // Find the first unmanaged ancestor sw.WriteLine ("\t\t\t{0}NativeDelegate unmanaged = GetClassStruct (this.LookupGType ().GetThresholdType (), true).{0};", this.Name); sw.Write ("\t\t\tif (unmanaged == null) "); if (parms.HasOutParam) sw.WriteLine ("throw new InvalidOperationException (\"No base method to invoke\");"); else if (retval.IsVoid) sw.WriteLine ("return;"); else sw.WriteLine ("return {0};", retval.DefaultValue); sw.WriteLine (); Body.Initialize (gen_info); sw.Write ("\t\t\t"); if (!retval.IsVoid) sw.Write ("{0} __result = ", retval.MarshalType); sw.WriteLine ("unmanaged ({0});", native_call); Body.Finish (gen_info.Writer, ""); if(!retval.IsVoid) sw.WriteLine ("\t\t\treturn {0};", retval.FromNative ("__result")); sw.WriteLine ("\t\t}"); sw.WriteLine (); }
public override void Generate(GenerationInfo gen_info) { gen_info.CurrentType = Name; StreamWriter sw = gen_info.Writer = gen_info.OpenStream(Name); sw.WriteLine("namespace " + NS + " {"); sw.WriteLine(); sw.WriteLine("\tusing System;"); sw.WriteLine("\tusing System.Collections;"); sw.WriteLine("\tusing System.Runtime.InteropServices;"); sw.WriteLine(); sw.WriteLine("#region Autogenerated code"); SymbolTable table = SymbolTable.Table; Method ref_, unref, dispose; GetSpecialMethods(out ref_, out unref, out dispose); if (IsDeprecated) { sw.WriteLine("\t[Obsolete]"); } sw.Write("\t{0} class " + Name, IsInternal ? "internal" : "public"); string cs_parent = table.GetCSType(Elem.GetAttribute("parent")); if (cs_parent != "") { sw.Write(" : " + cs_parent); } else { sw.Write(" : GLib.Opaque"); } sw.WriteLine(" {"); sw.WriteLine(); GenFields(gen_info); GenMethods(gen_info, null, null); GenCtors(gen_info); if (ref_ != null) { ref_.GenerateImport(sw); sw.WriteLine("\t\tprotected override void Ref (IntPtr raw)"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tif (!Owned) {"); sw.WriteLine("\t\t\t\t" + ref_.CName + " (raw);"); sw.WriteLine("\t\t\t\tOwned = true;"); sw.WriteLine("\t\t\t}"); sw.WriteLine("\t\t}"); sw.WriteLine(); if (ref_.IsDeprecated) { sw.WriteLine("\t\t[Obsolete(\"" + QualifiedName + " is now refcounted automatically\")]"); if (ref_.ReturnType == "void") { sw.WriteLine("\t\tpublic void Ref () {}"); } else { sw.WriteLine("\t\tpublic " + Name + " Ref () { return this; }"); } sw.WriteLine(); } } bool finalizer_needed = false; if (unref != null) { unref.GenerateImport(sw); sw.WriteLine("\t\tprotected override void Unref (IntPtr raw)"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tif (Owned) {"); sw.WriteLine("\t\t\t\t" + unref.CName + " (raw);"); sw.WriteLine("\t\t\t\tOwned = false;"); sw.WriteLine("\t\t\t}"); sw.WriteLine("\t\t}"); sw.WriteLine(); if (unref.IsDeprecated) { sw.WriteLine("\t\t[Obsolete(\"" + QualifiedName + " is now refcounted automatically\")]"); sw.WriteLine("\t\tpublic void Unref () {}"); sw.WriteLine(); } finalizer_needed = true; } if (dispose != null) { dispose.GenerateImport(sw); sw.WriteLine("\t\tprotected override void Free (IntPtr raw)"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\t" + dispose.CName + " (raw);"); sw.WriteLine("\t\t}"); sw.WriteLine(); if (dispose.IsDeprecated) { sw.WriteLine("\t\t[Obsolete(\"" + QualifiedName + " is now freed automatically\")]"); sw.WriteLine("\t\tpublic void " + dispose.Name + " () {}"); sw.WriteLine(); } finalizer_needed = true; } if (finalizer_needed) { sw.WriteLine("\t\tclass FinalizerInfo {"); sw.WriteLine("\t\t\tIntPtr handle;"); sw.WriteLine(); sw.WriteLine("\t\t\tpublic FinalizerInfo (IntPtr handle)"); sw.WriteLine("\t\t\t{"); sw.WriteLine("\t\t\t\tthis.handle = handle;"); sw.WriteLine("\t\t\t}"); sw.WriteLine(); sw.WriteLine("\t\t\tpublic bool Handler ()"); sw.WriteLine("\t\t\t{"); if (dispose != null) { sw.WriteLine("\t\t\t\t{0} (handle);", dispose.CName); } else if (unref != null) { sw.WriteLine("\t\t\t\t{0} (handle);", unref.CName); } sw.WriteLine("\t\t\t\treturn false;"); sw.WriteLine("\t\t\t}"); sw.WriteLine("\t\t}"); sw.WriteLine(); sw.WriteLine("\t\t~{0} ()", Name); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tif (!Owned)"); sw.WriteLine("\t\t\t\treturn;"); sw.WriteLine("\t\t\tFinalizerInfo info = new FinalizerInfo (Handle);"); sw.WriteLine("\t\t\tGLib.Timeout.Add (50, new GLib.TimeoutHandler (info.Handler));"); sw.WriteLine("\t\t}"); sw.WriteLine(); } #if false Method copy = Methods ["Copy"] as Method; if (copy != null && copy.Parameters.Count == 0) { sw.WriteLine("\t\tprotected override GLib.Opaque Copy (IntPtr raw)"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tGLib.Opaque result = new " + QualifiedName + " (" + copy.CName + " (raw));"); sw.WriteLine("\t\t\tresult.Owned = true;"); sw.WriteLine("\t\t\treturn result;"); sw.WriteLine("\t\t}"); sw.WriteLine(); } #endif sw.WriteLine("#endregion"); AppendCustom(sw, gen_info.CustomDir); sw.WriteLine("\t}"); sw.WriteLine("}"); sw.Close(); gen_info.Writer = null; Statistics.OpaqueCount++; }
public virtual void Generate(GenerationInfo gen_info, string indent) { if (Ignored || Hidden) { return; } CheckGlue(gen_info); GenerateImports(gen_info, indent); if (Getter == null && getterName == null && offsetName == null && Setter == null && setterName == null) { return; } SymbolTable table = SymbolTable.Table; IGeneratable gen = table [CType]; StreamWriter sw = gen_info.Writer; string modifiers = elem.GetAttributeAsBoolean("new_flag") ? "new " : ""; sw.WriteLine(indent + "public " + modifiers + CSType + " " + Name + " {"); if (Getter != null) { sw.Write(indent + "\tget "); Getter.GenerateBody(gen_info, container_type, "\t"); sw.WriteLine(""); } else if (getterName != null) { sw.WriteLine(indent + "\tget {"); container_type.Prepare(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\t" + CSType + " result = " + table.FromNative(ctype, getterName + " (" + container_type.CallByName() + ")") + ";"); container_type.Finish(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\treturn result;"); sw.WriteLine(indent + "\t}"); } else if (Readable && offsetName != null) { sw.WriteLine(indent + "\tget {"); sw.WriteLine(indent + "\t\tunsafe {"); if (gen is CallbackGen) { sw.WriteLine(indent + "\t\t\tIntPtr* raw_ptr = (IntPtr*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\t {0} del = ({0})Marshal.GetDelegateForFunctionPointer(*raw_ptr, typeof({0}));", table.GetMarshalType(CType)); sw.WriteLine(indent + "\t\t\treturn " + table.FromNative(ctype, "(del)") + ";"); } else { sw.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" + table.GetMarshalType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\treturn " + table.FromNative(ctype, "(*raw_ptr)") + ";"); } sw.WriteLine(indent + "\t\t}"); sw.WriteLine(indent + "\t}"); } string to_native = (gen is IManualMarshaler) ? (gen as IManualMarshaler).AllocNative("value") : gen.CallByName("value"); if (Setter != null) { sw.Write(indent + "\tset "); Setter.GenerateBody(gen_info, container_type, "\t"); sw.WriteLine(""); } else if (setterName != null) { sw.WriteLine(indent + "\tset {"); container_type.Prepare(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\t" + setterName + " (" + container_type.CallByName() + ", " + to_native + ");"); container_type.Finish(sw, indent + "\t\t"); sw.WriteLine(indent + "\t}"); } else if (Writable && offsetName != null) { sw.WriteLine(indent + "\tset {"); sw.WriteLine(indent + "\t\tunsafe {"); if (gen is CallbackGen) { sw.WriteLine(indent + "\t\t\t{0} wrapper = new {0} (value);", ((CallbackGen)gen).WrapperName); sw.WriteLine(indent + "\t\t\tIntPtr* raw_ptr = (IntPtr*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\t*raw_ptr = Marshal.GetFunctionPointerForDelegate (wrapper.NativeDelegate);"); } else { sw.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" + table.GetMarshalType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\t*raw_ptr = " + to_native + ";"); } sw.WriteLine(indent + "\t\t}"); sw.WriteLine(indent + "\t}"); } sw.WriteLine(indent + "}"); sw.WriteLine(""); if ((getterName != null || setterName != null || getOffsetName != null) && gen_info.GlueWriter != null) { GenerateGlue(gen_info); } }
public override void Generate(GenerationInfo gen_info) { gen_info.CurrentType = Name; string asm_name = gen_info.AssemblyName.Length == 0 ? NS.ToLower() + "-sharp" : gen_info.AssemblyName; DirectoryInfo di = GetDirectoryInfo(gen_info.Dir, asm_name); StreamWriter sw = gen_info.Writer = gen_info.OpenStream(Name); sw.WriteLine("namespace " + NS + " {"); sw.WriteLine(); sw.WriteLine("\tusing System;"); sw.WriteLine("\tusing System.Collections;"); sw.WriteLine("\tusing System.Runtime.InteropServices;"); sw.WriteLine(); SymbolTable table = SymbolTable.Table; sw.WriteLine("#region Autogenerated code"); if (IsDeprecated) { sw.WriteLine("\t[Obsolete]"); } foreach (string attr in custom_attrs) { sw.WriteLine("\t" + attr); } GenerateAttribute(sw); sw.Write("\t{0} {1}class " + Name, IsInternal ? "internal" : "public", IsAbstract ? "abstract " : ""); string cs_parent = table.GetCSType(Elem.GetAttribute("parent")); if (cs_parent != "") { di.objects.Add(CName, QualifiedName); sw.Write(" : " + cs_parent); } foreach (string iface in interfaces) { if (Parent != null && Parent.Implements(iface)) { continue; } sw.Write(", " + table.GetCSType(iface)); } foreach (string iface in managed_interfaces) { if (Parent != null && Parent.Implements(iface)) { continue; } sw.Write(", " + iface); } sw.WriteLine(" {"); sw.WriteLine(); GenCtors(gen_info); GenProperties(gen_info, null); GenFields(gen_info); GenChildProperties(gen_info); bool has_sigs = (sigs != null && sigs.Count > 0); if (!has_sigs) { foreach (string iface in interfaces) { ClassBase igen = table.GetClassGen(iface); if (igen != null && igen.Signals != null) { has_sigs = true; break; } } } if (has_sigs && Elem.HasAttribute("parent")) { GenSignals(gen_info, null); } if (vm_nodes.Count > 0) { if (gen_info.GlueEnabled) { GenVirtualMethods(gen_info); } else { Statistics.VMIgnored = true; Statistics.ThrottledCount += vm_nodes.Count; } } GenMethods(gen_info, null, null); if (interfaces.Count != 0) { Hashtable all_methods = new Hashtable(); foreach (Method m in Methods.Values) { all_methods[m.Name] = m; } Hashtable collisions = new Hashtable(); foreach (string iface in interfaces) { ClassBase igen = table.GetClassGen(iface); foreach (Method m in igen.Methods.Values) { Method collision = all_methods[m.Name] as Method; if (collision != null && collision.Signature.Types == m.Signature.Types) { collisions[m.Name] = true; } else { all_methods[m.Name] = m; } } } foreach (string iface in interfaces) { if (Parent != null && Parent.Implements(iface)) { continue; } ClassBase igen = table.GetClassGen(iface); igen.GenMethods(gen_info, collisions, this); igen.GenProperties(gen_info, this); igen.GenSignals(gen_info, this); } } foreach (XmlElement str in strings) { sw.Write("\t\tpublic static string " + str.GetAttribute("name")); sw.WriteLine(" {\n\t\t\t get { return \"" + str.GetAttribute("value") + "\"; }\n\t\t}"); } if (cs_parent != String.Empty && GetExpected(CName) != QualifiedName) { sw.WriteLine(); sw.WriteLine("\t\tstatic " + Name + " ()"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tGtkSharp." + Studlify(asm_name) + ".ObjectManager.Initialize ();"); sw.WriteLine("\t\t}"); } sw.WriteLine("#endregion"); AppendCustom(sw, gen_info.CustomDir); sw.WriteLine("\t}"); var parentGType = GetParentWithGType(this); if (parentGType == this) { var method = parentGType.GetMethod("GetType") ?? parentGType.GetMethod("GetGType"); AttributeHelper.Gen(sw, Name, LibraryName, method.CName); } sw.WriteLine("}"); sw.Close(); gen_info.Writer = null; Statistics.ObjectCount++; }
void GenerateAdapter(GenerationInfo gen_info) { var sw = gen_info.Writer = gen_info.OpenStream(Name + "Adapter"); sw.WriteLine("namespace " + NS + " {"); sw.WriteLine(); sw.WriteLine("\tusing System;"); sw.WriteLine("\tusing System.Runtime.InteropServices;"); sw.WriteLine(); sw.WriteLine("#region Autogenerated code"); sw.WriteLine("\tpublic class " + Name + "Adapter : GLib.GInterfaceAdapter, " + QualifiedName + " {"); sw.WriteLine(); if (!IsConsumeOnly) { GenerateIfaceStruct(sw); GenerateStaticCtor(sw); GenerateCallbacks(sw); GenerateInitialize(sw); } GenerateCtors(sw); GenerateGType(sw); GenerateHandleProp(sw); GenerateGetObject(sw); if (!IsConsumeOnly) { GenerateImplementorProp(sw); } GenProperties(gen_info, null); foreach (Signal sig in sigs.Values) { sig.GenEvent(sw, null, "GLib.Object.GetObject (Handle)"); } var temp = methods ["GetType"] as Method; if (temp != null) { methods.Remove("GetType"); } GenMethods(gen_info, new Hashtable(), this); if (temp != null) { methods ["GetType"] = temp; } sw.WriteLine("#endregion"); string custom = Path.Combine(gen_info.CustomDir, Name + "Adapter.custom"); if (File.Exists(custom)) { sw.WriteLine("#region Customized extensions"); sw.WriteLine("#line 1 \"" + Name + "Adapter.custom\""); using (var sr = new StreamReader(new FileStream(custom, FileMode.Open, FileAccess.Read))) sw.WriteLine(sr.ReadToEnd()); sw.WriteLine("#endregion"); } sw.WriteLine("\t}"); sw.WriteLine("}"); sw.Close(); gen_info.Writer = null; }
public virtual void Generate(GenerationInfo gen_info, string indent) { if (Ignored || Hidden) { return; } CheckGlue(); if ((getterName != null || setterName != null || getOffsetName != null) && gen_info.GlueWriter == null) { LogWriter log = new LogWriter(container_type.QualifiedName); log.Member = Name; log.Warn("needs glue for field access. Specify --glue-filename"); return; } GenerateImports(gen_info, indent); SymbolTable table = SymbolTable.Table; StreamWriter sw = gen_info.Writer; string modifiers = elem.HasAttribute("new_flag") ? "new " : ""; bool is_struct = table.IsStruct(CType) || table.IsBoxed(CType); string access = elem.HasAttribute("access") ? elem.GetAttribute("access") : "public"; sw.WriteLine(indent + access + " " + modifiers + CSType + " " + Name + " {"); if (Getter != null) { sw.Write(indent + "\tget "); Getter.GenerateBody(gen_info, container_type, "\t"); sw.WriteLine(""); } else if (getterName != null) { sw.WriteLine(indent + "\tget {"); container_type.Prepare(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\t" + CSType + " result = " + table.FromNative(ctype, getterName + " (" + container_type.CallByName() + ")") + ";"); container_type.Finish(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\treturn result;"); sw.WriteLine(indent + "\t}"); } else if (Readable && offsetName != null) { sw.WriteLine(indent + "\tget {"); sw.WriteLine(indent + "\t\tunsafe {"); if (is_struct) { sw.WriteLine(indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\treturn *raw_ptr;"); } else { sw.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" + table.GetMarshalType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\treturn " + table.FromNative(ctype, "(*raw_ptr)") + ";"); } sw.WriteLine(indent + "\t\t}"); sw.WriteLine(indent + "\t}"); } IGeneratable gen = table [CType]; string to_native = (gen is IManualMarshaler) ? (gen as IManualMarshaler).AllocNative("value") : gen.CallByName("value"); if (Setter != null) { sw.Write(indent + "\tset "); Setter.GenerateBody(gen_info, container_type, "\t"); sw.WriteLine(""); } else if (setterName != null) { sw.WriteLine(indent + "\tset {"); container_type.Prepare(sw, indent + "\t\t"); sw.WriteLine(indent + "\t\t" + setterName + " (" + container_type.CallByName() + ", " + to_native + ");"); container_type.Finish(sw, indent + "\t\t"); sw.WriteLine(indent + "\t}"); } else if (Writable && offsetName != null) { sw.WriteLine(indent + "\tset {"); sw.WriteLine(indent + "\t\tunsafe {"); if (is_struct) { sw.WriteLine(indent + "\t\t\t" + CSType + "* raw_ptr = (" + CSType + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\t*raw_ptr = value;"); } else { sw.WriteLine(indent + "\t\t\t" + table.GetMarshalType(CType) + "* raw_ptr = (" + table.GetMarshalType(CType) + "*)(((byte*)" + container_type.CallByName() + ") + " + offsetName + ");"); sw.WriteLine(indent + "\t\t\t*raw_ptr = " + to_native + ";"); } sw.WriteLine(indent + "\t\t}"); sw.WriteLine(indent + "\t}"); } sw.WriteLine(indent + "}"); sw.WriteLine(""); if (getterName != null || setterName != null || getOffsetName != null) { GenerateGlue(gen_info); } }
public void GenerateBody(GenerationInfo gen_info, ClassBase implementor, string indent) { StreamWriter sw = gen_info.Writer; sw.WriteLine(" {"); if (!IsStatic && implementor != null) { implementor.Prepare(sw, indent + "\t\t\t"); } if (IsAccessor) { Body.InitAccessor(sw, Signature, indent); } Body.Initialize(gen_info, is_get, is_set, indent); if (HasWin32Utf8Variant) { if (!retval.IsVoid) { sw.WriteLine(indent + "\t\t\t" + retval.MarshalType + " raw_ret;"); } sw.WriteLine(indent + "\t\t\t" + "if (Environment.OSVersion.Platform == PlatformID.Win32NT ||"); sw.WriteLine(indent + "\t\t\t" + " Environment.OSVersion.Platform == PlatformID.Win32S ||"); sw.WriteLine(indent + "\t\t\t" + " Environment.OSVersion.Platform == PlatformID.Win32Windows ||"); sw.WriteLine(indent + "\t\t\t" + " Environment.OSVersion.Platform == PlatformID.WinCE)"); if (retval.IsVoid) { sw.WriteLine(indent + "\t\t\t\t" + CName + "_utf8" + call + ";"); sw.WriteLine(indent + "\t\t\t" + "else"); sw.WriteLine(indent + "\t\t\t\t" + CName + call + ";"); } else { sw.WriteLine(indent + "\t\t\t\traw_ret = " + CName + "_utf8" + call + ";"); sw.WriteLine(indent + "\t\t\t" + "else"); sw.WriteLine(indent + "\t\t\t\traw_ret = " + CName + call + ";"); sw.WriteLine(indent + "\t\t\t" + retval.CSType + " ret = " + retval.FromNative("raw_ret") + ";"); } } else { sw.Write(indent + "\t\t\t"); if (retval.IsVoid) { sw.WriteLine(CName + call + ";"); } else { sw.WriteLine(retval.MarshalType + " raw_ret = " + CName + call + ";"); sw.WriteLine(indent + "\t\t\t" + retval.CSType + " ret = " + retval.FromNative("raw_ret") + ";"); } } if (!IsStatic && implementor != null) { implementor.Finish(sw, indent + "\t\t\t"); } Body.Finish(sw, indent); Body.HandleException(sw, indent); if (is_get && Parameters.Count > 0) { sw.WriteLine(indent + "\t\t\treturn " + Parameters.AccessorName + ";"); } else if (!retval.IsVoid) { sw.WriteLine(indent + "\t\t\treturn ret;"); } else if (IsAccessor) { Body.FinishAccessor(sw, Signature, indent); } sw.Write(indent + "\t\t}"); }
public void Generate(GenerationInfo gen_info, ClassBase implementor) { Method comp = null; gen_info.CurrentMember = Name; /* we are generated by the get Method, if there is one */ if (is_set || is_get) { if (Modifiers != "new " && container_type.GetPropertyRecursively(Name.Substring(3)) != null) { return; } comp = GetComplement(); if (comp != null && is_set) { if (Parameters.AccessorReturnType == comp.ReturnType) { return; } else { is_set = false; call = "(" + (IsStatic ? "" : container_type.CallByName() + (parms.Count > 0 ? ", " : "")) + Body.GetCallString(false) + ")"; comp = null; } } /* some setters take more than one arg */ if (comp != null && !comp.is_set) { comp = null; } } GenerateImport(gen_info.Writer); if (comp != null && retval.CSType == comp.Parameters.AccessorReturnType) { comp.GenerateImport(gen_info.Writer); } if (IsDeprecated) { gen_info.Writer.WriteLine("\t\t[Obsolete]"); } gen_info.Writer.Write("\t\t"); if (Protection != "") { gen_info.Writer.Write("{0} ", Protection); } GenerateDeclCommon(gen_info.Writer, implementor); if (is_get || is_set) { gen_info.Writer.Write("\t\t\t"); gen_info.Writer.Write((is_get) ? "get" : "set"); GenerateBody(gen_info, implementor, "\t"); } else { GenerateBody(gen_info, implementor, ""); } if (is_get || is_set) { if (comp != null && retval.CSType == comp.Parameters.AccessorReturnType) { gen_info.Writer.WriteLine(); gen_info.Writer.Write("\t\t\tset"); comp.GenerateBody(gen_info, implementor, "\t"); } gen_info.Writer.WriteLine(); gen_info.Writer.WriteLine("\t\t}"); } else { gen_info.Writer.WriteLine(); } if (Parameters.HasOptional && !(is_get || is_set)) { GenerateOverloads(gen_info.Writer); } gen_info.Writer.WriteLine(); Statistics.MethodCount++; }
public void Generate(GenerationInfo gen_info) { if (!Validate ()) return; StreamWriter sw = gen_info.Writer; gen_info.CurrentMember = CName; GenerateImport (sw); if (IsStatic) GenerateStatic (gen_info); else { sw.WriteLine("\t\t{0} {1}{2} ({3}) {4}", Protection, Safety, name, Signature.ToString(), needs_chaining ? ": base (IntPtr.Zero)" : ""); sw.WriteLine("\t\t{"); if (needs_chaining) { sw.WriteLine ("\t\t\tif (GetType () != typeof (" + name + ")) {"); if (Parameters.Count == 0) { sw.WriteLine ("\t\t\t\tCreateNativeObject (new string [0], new GLib.Value[0]);"); sw.WriteLine ("\t\t\t\treturn;"); } else { ArrayList names = new ArrayList (); ArrayList values = new ArrayList (); for (int i = 0; i < Parameters.Count; i++) { Parameter p = Parameters[i]; if (container_type.GetPropertyRecursively (p.StudlyName) != null) { names.Add (p.Name); values.Add (p.Name); } else if (p.PropertyName != String.Empty) { names.Add (p.PropertyName); values.Add (p.Name); } } if (names.Count == Parameters.Count) { sw.WriteLine ("\t\t\t\tArrayList vals = new ArrayList();"); sw.WriteLine ("\t\t\t\tArrayList names = new ArrayList();"); for (int i = 0; i < names.Count; i++) { Parameter p = Parameters [i]; string indent = "\t\t\t\t"; if (p.Generatable is ClassBase && !(p.Generatable is StructBase)) { sw.WriteLine (indent + "if (" + p.Name + " != null) {"); indent += "\t"; } sw.WriteLine (indent + "names.Add (\"" + names [i] + "\");"); sw.WriteLine (indent + "vals.Add (new GLib.Value (" + values[i] + "));"); if (p.Generatable is ClassBase && !(p.Generatable is StructBase)) sw.WriteLine ("\t\t\t\t}"); } sw.WriteLine ("\t\t\t\tCreateNativeObject ((string[])names.ToArray (typeof (string)), (GLib.Value[])vals.ToArray (typeof (GLib.Value)));"); sw.WriteLine ("\t\t\t\treturn;"); } else sw.WriteLine ("\t\t\t\tthrow new InvalidOperationException (\"Can't override this constructor.\");"); } sw.WriteLine ("\t\t\t}"); } Body.Initialize(gen_info, false, false, ""); sw.WriteLine("\t\t\t{0} = {1}({2});", container_type.AssignToName, CName, Body.GetCallString (false)); Body.Finish (sw, ""); Body.HandleException (sw, ""); } sw.WriteLine("\t\t}"); sw.WriteLine(); Statistics.CtorCount++; }
public override void Generate(GenerationInfo gen_info) { bool need_close = false; if (gen_info.Writer == null) { gen_info.Writer = gen_info.OpenStream(Name, NS); need_close = true; } StreamWriter sw = gen_info.Writer; sw.WriteLine("namespace " + NS + " {"); sw.WriteLine(); sw.WriteLine("\tusing System;"); sw.WriteLine("\tusing System.Collections;"); sw.WriteLine("\tusing System.Collections.Generic;"); sw.WriteLine("\tusing System.Runtime.InteropServices;"); sw.WriteLine(); sw.WriteLine("#region Autogenerated code"); if (IsDeprecated) { sw.WriteLine("\t[Obsolete]"); } if (Union) { sw.WriteLine("\t[StructLayout(LayoutKind.Explicit)]"); } else { sw.WriteLine("\t[StructLayout(LayoutKind.Sequential)]"); } string access = IsInternal ? "internal" : "public"; sw.WriteLine("\t" + access + " partial struct {0} : IEquatable<{0}> {{", Name); sw.WriteLine(); need_read_native = false; GenFields(gen_info); sw.WriteLine(); GenCtors(gen_info); GenMethods(gen_info, null, this); if (need_read_native) { GenReadNative(sw); } GenEqualsAndHash(sw); if (!need_close) { return; } sw.WriteLine("#endregion"); sw.WriteLine("\t}"); sw.WriteLine("}"); sw.Close(); gen_info.Writer = null; }
public void Generate(GenerationInfo gen_info, string indent, ClassBase implementor) { SymbolTable table = SymbolTable.Table; StreamWriter sw = gen_info.Writer; if (Hidden || (!Readable && !Writable)) { return; } string modifiers = ""; if (IsNew || (container_type.Parent != null && container_type.Parent.GetPropertyRecursively(Name) != null)) { modifiers = "new "; } else if (implementor != null && implementor.Parent != null && implementor.Parent.GetPropertyRecursively(Name) != null) { modifiers = "new "; } string name = Name; if (name == container_type.Name) { name += "Prop"; } string qpname = "\"" + CName + "\""; string v_type = ""; if (table.IsInterface(CType)) { v_type = "(GLib.Object)"; } else if (table.IsOpaque(CType)) { v_type = "(GLib.Opaque)"; } else if (table.IsEnum(CType)) { v_type = "(Enum)"; } GenerateImports(gen_info, indent); if (IsDeprecated || (Getter != null && Getter.IsDeprecated) || (Setter != null && Setter.IsDeprecated)) { sw.WriteLine(indent + "[Obsolete]"); } sw.WriteLine(indent + PropertyAttribute(qpname)); sw.WriteLine(indent + "public " + modifiers + CSType + " " + name + " {"); indent += "\t"; if (Getter != null) { sw.Write(indent + "get "); Getter.GenerateBody(gen_info, implementor, "\t"); sw.WriteLine(); } else if (Readable) { sw.WriteLine(indent + "get {"); sw.WriteLine(indent + "\tGLib.Value val = " + RawGetter(qpname) + ";"); if (table.IsOpaque(CType) || table.IsBoxed(CType)) { sw.WriteLine(indent + "\t" + CSType + " ret = (" + CSType + ") val;"); } else if (table.IsInterface(CType)) { // Do we have to dispose the GLib.Object from the GLib.Value? sw.WriteLine(indent + "\t{0} ret = {0}Adapter.GetObject ((GLib.Object) val);", CSType); } else { sw.Write(indent + "\t" + CSType + " ret = "); sw.Write("(" + CSType + ") "); if (v_type != "") { sw.Write(v_type + " "); } sw.WriteLine("val;"); } sw.WriteLine(indent + "\tval.Dispose ();"); sw.WriteLine(indent + "\treturn ret;"); sw.WriteLine(indent + "}"); } if (Setter != null) { sw.Write(indent + "set "); Setter.GenerateBody(gen_info, implementor, "\t"); sw.WriteLine(); } else if (Writable) { sw.WriteLine(indent + "set {"); sw.Write(indent + "\tGLib.Value val = "); if (table.IsBoxed(CType)) { sw.WriteLine("(GLib.Value) value;"); } else if (table.IsOpaque(CType)) { sw.WriteLine("new GLib.Value(value, \"{0}\");", CType); } else { sw.Write("new GLib.Value("); if (v_type != "" && !(table.IsObject(CType) || table.IsInterface(CType) || table.IsOpaque(CType))) { sw.Write(v_type + " "); } sw.WriteLine("value);"); } sw.WriteLine(indent + "\t" + RawSetter(qpname) + ";"); sw.WriteLine(indent + "\tval.Dispose ();"); sw.WriteLine(indent + "}"); } sw.WriteLine(indent.Substring(1) + "}"); sw.WriteLine(); Statistics.PropCount++; }
void GenClassMembers(GenerationInfo gen_info, string cs_parent) { GenVirtualMethods(gen_info, null); GenerateStructureABI(gen_info, abi_class_members, "class_abi", ClassStructName); }
public string GenWrapper(GenerationInfo gen_info) { string wrapper = Name + "Native"; string qualname = MarshalType; if (!Validate()) { return(String.Empty); } body = new MethodBody(parms); StreamWriter save_sw = gen_info.Writer; StreamWriter sw = gen_info.Writer = gen_info.OpenStream(qualname); sw.WriteLine("namespace " + NS + "Sharp {"); sw.WriteLine(); sw.WriteLine("\tusing System;"); sw.WriteLine("\tusing System.Runtime.InteropServices;"); sw.WriteLine(); sw.WriteLine("#region Autogenerated code"); sw.WriteLine("\t[UnmanagedFunctionPointer (CallingConvention.Cdecl)]"); sw.WriteLine("\tinternal delegate " + retval.MarshalType + " " + wrapper + "(" + parms.ImportSignature + ");"); sw.WriteLine(); GenInvoker(gen_info, sw); sw.WriteLine("\tinternal class " + Name + "Wrapper {"); sw.WriteLine(); ManagedCallString call = new ManagedCallString(parms); sw.WriteLine("\t\tpublic " + retval.MarshalType + " NativeCallback (" + parms.ImportSignature + ")"); sw.WriteLine("\t\t{"); string unconditional = call.Unconditional("\t\t\t"); if (unconditional.Length > 0) { sw.WriteLine(unconditional); } sw.WriteLine("\t\t\ttry {"); string call_setup = call.Setup("\t\t\t\t"); if (call_setup.Length > 0) { sw.WriteLine(call_setup); } if (retval.CSType == "void") { sw.WriteLine("\t\t\t\tmanaged ({0});", call); } else { sw.WriteLine("\t\t\t\t{0} __ret = managed ({1});", retval.CSType, call); } string finish = call.Finish("\t\t\t\t"); if (finish.Length > 0) { sw.WriteLine(finish); } sw.WriteLine("\t\t\t\tif (release_on_call)\n\t\t\t\t\tgch.Free ();"); if (retval.CSType != "void") { sw.WriteLine("\t\t\t\treturn {0};", retval.ToNative("__ret")); } /* If the function expects one or more "out" parameters(error parameters are excluded) or has a return value different from void and bool, exceptions * thrown in the managed function have to be considered fatal meaning that an exception is to be thrown and the function call cannot not return */ bool fatal = (retval.MarshalType != "void" && retval.MarshalType != "bool") || call.HasOutParam; sw.WriteLine("\t\t\t} catch (Exception e) {"); sw.WriteLine("\t\t\t\tGLib.ExceptionManager.RaiseUnhandledException (e, " + (fatal ? "true" : "false") + ");"); if (fatal) { sw.WriteLine("\t\t\t\t// NOTREACHED: Above call does not return."); sw.WriteLine("\t\t\t\tthrow e;"); } else if (retval.MarshalType == "bool") { sw.WriteLine("\t\t\t\treturn false;"); } sw.WriteLine("\t\t\t}"); sw.WriteLine("\t\t}"); sw.WriteLine(); sw.WriteLine("\t\tbool release_on_call = false;"); sw.WriteLine("\t\tGCHandle gch;"); sw.WriteLine(); sw.WriteLine("\t\tpublic void PersistUntilCalled ()"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\trelease_on_call = true;"); sw.WriteLine("\t\t\tgch = GCHandle.Alloc (this);"); sw.WriteLine("\t\t}"); sw.WriteLine(); sw.WriteLine("\t\tinternal " + wrapper + " NativeDelegate;"); sw.WriteLine("\t\t" + NS + "." + Name + " managed;"); sw.WriteLine(); sw.WriteLine("\t\tpublic " + Name + "Wrapper (" + NS + "." + Name + " managed)"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tthis.managed = managed;"); sw.WriteLine("\t\t\tif (managed != null)"); sw.WriteLine("\t\t\t\tNativeDelegate = new " + wrapper + " (NativeCallback);"); sw.WriteLine("\t\t}"); sw.WriteLine(); sw.WriteLine("\t\tpublic static " + NS + "." + Name + " GetManagedDelegate (" + wrapper + " native)"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tif (native == null)"); sw.WriteLine("\t\t\t\treturn null;"); sw.WriteLine("\t\t\t" + Name + "Wrapper wrapper = (" + Name + "Wrapper) native.Target;"); sw.WriteLine("\t\t\tif (wrapper == null)"); sw.WriteLine("\t\t\t\treturn null;"); sw.WriteLine("\t\t\treturn wrapper.managed;"); sw.WriteLine("\t\t}"); sw.WriteLine("\t}"); sw.WriteLine("#endregion"); sw.WriteLine("}"); sw.Close(); gen_info.Writer = save_sw; return(NS + "Sharp." + Name + "Wrapper"); }
protected override void GenCtors(GenerationInfo gen_info) { if (!DisableRawCtor) { gen_info.Writer.WriteLine("\t\tpublic " + Name + "(IntPtr raw) : base(raw) {}"); gen_info.Writer.WriteLine(); } base.GenCtors (gen_info); }
void GenerateUnmanagedInvocation_glue(GenerationInfo gen_info) { StreamWriter glue = gen_info.GlueWriter; string glue_name = String.Format ("{0}sharp_{1}_invoke_{2}", container_type.NS.ToLower ().Replace (".", "_"), container_type.Name.ToLower (), CName); glue.WriteLine ("{0} {1} ({2});\n", retval.CType.Replace ("const-", "const "), glue_name, GlueSignature); glue.WriteLine ("{0}\n{1} ({2})", retval.CType.Replace ("const-", "const "), glue_name, GlueSignature); glue.WriteLine ("{"); glue.Write ("\tif (class_struct->{0})\n\t\t", CName); if (!retval.IsVoid) glue.Write ("return "); string[] call_args = new string [IsStatic ? parms.Count : parms.Count + 1]; if (!IsStatic) call_args [0] = "inst"; for (int i = 0; i < parms.Count; i++) call_args [IsStatic ? i : i + 1] = parms[i].Name; glue.WriteLine ("(* class_struct->{0}) ({1});", CName, String.Join (", ", call_args)); if (!retval.IsVoid) glue.WriteLine ("\treturn " + DefaultGlueValue + ";"); glue.WriteLine ("}"); glue.WriteLine (); StreamWriter sw = gen_info.Writer; sw.WriteLine ("\t\t[DllImport (\"{0}\")]", gen_info.GluelibName); sw.Write ("\t\tstatic extern {0} {1} (IntPtr class_struct", retval.MarshalType, glue_name); if (!IsStatic) sw.Write (", IntPtr inst"); if (parms.Count > 0) sw.Write (", {0}", parms.ImportSignature); sw.WriteLine (");"); sw.WriteLine (); GenerateMethodBody (sw, null); Body.Initialize (gen_info, false, false, String.Empty); string glue_call_string = "this.LookupGType ().GetThresholdType ().GetClassPtr ()"; if (!IsStatic) glue_call_string += ", Handle"; if (parms.Count > 0) glue_call_string += ", " + Body.GetCallString (false); sw.Write ("\t\t\t"); if (!retval.IsVoid) sw.Write ("{0} __result = ", retval.MarshalType); sw.WriteLine ("{0} ({1});", glue_name, glue_call_string); Body.Finish (gen_info.Writer, ""); if(!retval.IsVoid) sw.WriteLine ("\t\t\treturn {0};", retval.FromNative ("__result")); sw.WriteLine ("\t\t}"); sw.WriteLine (); }
public static int Main(string[] args) { if (args.Length < 2) { Console.WriteLine("Usage: codegen --generate <filename1...>"); return(0); } bool generate = false; string dir = ""; string assembly_name = ""; string glue_filename = ""; string glue_includes = ""; string gluelib_name = ""; SymbolTable table = SymbolTable.Table; ArrayList gens = new ArrayList(); foreach (string arg in args) { if (arg.StartsWith("--customdir=")) { Console.WriteLine("Using .custom files is not supported anymore, use partial classes instead."); return(0); } string filename = arg; if (arg == "--generate") { generate = true; continue; } else if (arg == "--include") { generate = false; continue; } else if (arg.StartsWith("-I:")) { generate = false; filename = filename.Substring(3); } else if (arg.StartsWith("--outdir=")) { generate = false; dir = arg.Substring(9); continue; } else if (arg.StartsWith("--assembly-name=")) { generate = false; assembly_name = arg.Substring(16); continue; } else if (arg.StartsWith("--glue-filename=")) { generate = false; glue_filename = arg.Substring(16); continue; } else if (arg.StartsWith("--glue-includes=")) { generate = false; glue_includes = arg.Substring(16); continue; } else if (arg.StartsWith("--gluelib-name=")) { generate = false; gluelib_name = arg.Substring(15); continue; } Parser p = new Parser(); IGeneratable[] curr_gens = p.Parse(filename); table.AddTypes(curr_gens); if (generate) { gens.AddRange(curr_gens); } } // Now that everything is loaded, validate all the to-be- // generated generatables and then remove the invalid ones. ArrayList invalids = new ArrayList(); foreach (IGeneratable gen in gens) { if (!gen.Validate()) { invalids.Add(gen); } } foreach (IGeneratable gen in invalids) { gens.Remove(gen); } GenerationInfo gen_info = null; if (dir != "" || assembly_name != "" || glue_filename != "" || glue_includes != "" || gluelib_name != "") { gen_info = new GenerationInfo(dir, assembly_name, glue_filename, glue_includes, gluelib_name); } foreach (IGeneratable gen in gens) { if (gen_info == null) { gen.Generate(); } else { gen.Generate(gen_info); } } ObjectGen.GenerateMappers(); if (gen_info != null) { gen_info.CloseGlueWriter(); } Statistics.Report(); return(0); }
public void GenSignals(GenerationInfo gen_info, ObjectBase implementor) { foreach (Signal sig in sigs.Values) sig.Generate (gen_info, implementor); }
public override void Generate(GenerationInfo gen_info) { gen_info.CurrentType = QualifiedName; string asm_name = gen_info.AssemblyName.Length == 0 ? NS.ToLower() + "-sharp" : gen_info.AssemblyName; DirectoryInfo di = GetDirectoryInfo(gen_info.Dir, asm_name); StreamWriter sw = gen_info.Writer = gen_info.OpenStream(Name, NS); sw.WriteLine("namespace " + NS + " {"); sw.WriteLine(); sw.WriteLine("\tusing System;"); sw.WriteLine("\tusing System.Collections;"); sw.WriteLine("\tusing System.Collections.Generic;"); sw.WriteLine("\tusing System.Runtime.InteropServices;"); sw.WriteLine("\tusing static GLib.AbiStructExtension;"); sw.WriteLine(); SymbolTable table = SymbolTable.Table; sw.WriteLine("#region Autogenerated code"); if (IsDeprecated) { sw.WriteLine("\t[Obsolete]"); } foreach (string attr in custom_attrs) { sw.WriteLine("\t" + attr); } sw.Write("\t{0} {1}partial class " + Name, IsInternal ? "internal" : "public", IsAbstract ? "abstract " : ""); string cs_parent = table.GetCSType(Elem.GetAttribute("parent")); if (cs_parent != "") { di.objects.Add(CName, QualifiedName); sw.Write(" : " + cs_parent); } foreach (string iface in interfaces) { if (Parent != null && Parent.Implements(iface)) { continue; } sw.Write(", " + table.GetCSType(iface)); } foreach (string iface in managed_interfaces) { if (Parent != null && Parent.Implements(iface)) { continue; } sw.Write(", " + iface); } sw.WriteLine(" {"); sw.WriteLine(); GenCtors(gen_info); GenProperties(gen_info, null); GenFields(gen_info); GenChildProperties(gen_info); bool has_sigs = (sigs != null && sigs.Count > 0); if (!has_sigs) { foreach (string iface in interfaces) { InterfaceGen igen = table.GetClassGen(iface) as InterfaceGen; if (igen != null && igen.Signals != null) { has_sigs = true; break; } } } if (has_sigs && Elem.HasAttribute("parent")) { GenSignals(gen_info, null); } GenConstants(gen_info); GenClassMembers(gen_info, cs_parent); GenMethods(gen_info, null, null); if (interfaces.Count != 0) { var all_methods = new Dictionary <string, Method> (); foreach (Method m in Methods.Values) { all_methods[m.Name] = m; } var collisions = new Dictionary <string, bool> (); foreach (string iface in interfaces) { ClassBase igen = table.GetClassGen(iface); foreach (Method m in igen.Methods.Values) { if (m.Name.StartsWith("Get") || m.Name.StartsWith("Set")) { if (GetProperty(m.Name.Substring(3)) != null) { collisions[m.Name] = true; continue; } } Method collision = null; all_methods.TryGetValue(m.Name, out collision); if (collision != null && collision.Signature.Types == m.Signature.Types) { collisions[m.Name] = true; } else { all_methods[m.Name] = m; } } } foreach (string iface in interfaces) { if (Parent != null && Parent.Implements(iface)) { continue; } InterfaceGen igen = table.GetClassGen(iface) as InterfaceGen; igen.GenMethods(gen_info, collisions, this); igen.GenProperties(gen_info, this); igen.GenSignals(gen_info, this); igen.GenVirtualMethods(gen_info, this); } } foreach (XmlElement str in strings) { sw.Write("\t\tpublic static string " + str.GetAttribute("name")); sw.WriteLine(" {\n\t\t\t get { return \"" + str.GetAttribute("value") + "\"; }\n\t\t}"); } if (cs_parent != String.Empty && GetExpected(CName) != QualifiedName) { sw.WriteLine(); sw.WriteLine("\t\tstatic " + Name + " ()"); sw.WriteLine("\t\t{"); sw.WriteLine("\t\t\tGtkSharp." + Studlify(asm_name) + ".ObjectManager.Initialize ();"); sw.WriteLine("\t\t}"); } GenerateStructureABI(gen_info); sw.WriteLine("#endregion"); sw.WriteLine("\t}"); sw.WriteLine("}"); sw.Close(); gen_info.Writer = null; Statistics.ObjectCount++; }
protected void GenerateClassStruct(GenerationInfo gen_info) { if (class_struct_name == null || !CanGenerateClassStruct) return; StreamWriter sw = gen_info.Writer; sw.WriteLine ("\t\t[StructLayout (LayoutKind.Sequential)]"); sw.WriteLine ("\t\tstruct " + class_struct_name + " {"); foreach (object member in class_members) { if (member is VirtualMethod) { VirtualMethod vm = member as VirtualMethod; if (hidden_vms.Contains (vm) || (is_interface && vm is DefaultSignalHandler)) sw.WriteLine ("\t\t\tIntPtr {0};", vm.Name); else sw.WriteLine ("\t\t\tpublic {0}NativeDelegate {0};", vm.Name); } else if (member is ClassField) { ClassField field = member as ClassField; field.Generate (gen_info, "\t\t\t"); } } sw.WriteLine ("\t\t}"); sw.WriteLine (); }
public override void Generate (GenerationInfo gen_info) { bool need_close = false; if (gen_info.Writer == null) { gen_info.Writer = gen_info.OpenStream (Name, NS); need_close = true; } StreamWriter sw = gen_info.Writer; sw.WriteLine ("namespace " + NS + " {"); sw.WriteLine (); sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System.Collections;"); sw.WriteLine ("\tusing System.Collections.Generic;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); sw.WriteLine ("#region Autogenerated code"); if (IsDeprecated) sw.WriteLine ("\t[Obsolete]"); if (Union) sw.WriteLine ("\t[StructLayout(LayoutKind.Explicit)]"); else sw.WriteLine ("\t[StructLayout(LayoutKind.Sequential)]"); string access = IsInternal ? "internal" : "public"; sw.WriteLine ("\t" + access + " partial struct {0} : IEquatable<{0}> {{", Name); sw.WriteLine (); need_read_native = false; GenFields (gen_info); sw.WriteLine (); GenCtors (gen_info); GenMethods (gen_info, null, this); if (need_read_native) GenReadNative (sw); GenEqualsAndHash (sw); if (!need_close) return; sw.WriteLine ("#endregion"); sw.WriteLine ("\t}"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = null; }
public static int Main(string[] args) { bool show_help = false; string dir = ""; string assembly_name = ""; string glue_filename = ""; string glue_includes = ""; string gluelib_name = ""; string schema_name = ""; SymbolTable table = SymbolTable.Table; var gens = new List<IGeneratable> (); var filenames = new List<string> (); var includes = new List<string> (); var options = new OptionSet () { { "generate=", "Generate the C# code for this GAPI XML file.", (string v) => { filenames.Add (v); } }, { "I|include=", "GAPI XML file that contain symbols used in the main GAPI XML file.", (string v) => { includes.Add (v); } }, { "outdir=", "Directory where the C# files will be generated.", (string v) => { dir = v; } }, { "assembly-name=", "Name of the assembly for which the code is generated.", (string v) => { assembly_name = v; } }, { "glue-filename=", "Filename for the generated C glue code.", (string v) => { glue_filename = v; } }, { "glue-includes=", "Content of #include directive to add in the generated C glue code.", (string v) => { glue_includes = v; } }, { "gluelib-name=", "Name of the C library into which the C glue code will be compiled. " + "Used to generated correct DllImport attributes.", (string v) => { gluelib_name = v; } }, { "schema=", "Validate all GAPI XML files against this XSD schema.", (string v) => { schema_name = v; } }, { "h|help", "Show this message and exit", v => show_help = v != null }, }; List<string> extra; try { extra = options.Parse (args); } catch (OptionException e) { Console.Write ("gapi-codegen: "); Console.WriteLine (e.Message); Console.WriteLine ("Try `gapi-codegen --help' for more information."); return 0; } if (show_help) { ShowHelp (options); return 1; } if (filenames.Count == 0) { Console.WriteLine ("You need to specify a file to process using the --generate option."); Console.WriteLine ("Try `gapi-codegen --help' for more information."); return 0; } if (extra.Exists (v => { return v.StartsWith ("--customdir"); })) { Console.WriteLine ("Using .custom files is not supported anymore, use partial classes instead."); return 0; } if (!String.IsNullOrEmpty (schema_name) && !File.Exists (schema_name)) { Console.WriteLine ("WARNING: Could not find schema file at '{0}', no validation will be done.", schema_name); schema_name = null; } Parser p = new Parser (); foreach (string include in includes) { IGeneratable[] curr_gens = p.Parse (include, schema_name); table.AddTypes (curr_gens); } foreach (string filename in filenames) { IGeneratable[] curr_gens = p.Parse (filename, schema_name); table.AddTypes (curr_gens); gens.AddRange (curr_gens); } // Now that everything is loaded, validate all the to-be- // generated generatables and then remove the invalid ones. var invalids = new List<IGeneratable> (); foreach (IGeneratable gen in gens) { if (!gen.Validate ()) invalids.Add (gen); } foreach (IGeneratable gen in invalids) gens.Remove (gen); GenerationInfo gen_info = null; if (dir != "" || assembly_name != "" || glue_filename != "" || glue_includes != "" || gluelib_name != "") gen_info = new GenerationInfo (dir, assembly_name, glue_filename, glue_includes, gluelib_name); foreach (IGeneratable gen in gens) { if (gen_info == null) gen.Generate (); else gen.Generate (gen_info); } ObjectGen.GenerateMappers (); if (gen_info != null) gen_info.CloseGlueWriter (); Statistics.Report(); return 0; }
public void Initialize(GenerationInfo gen_info, bool is_get, bool is_set, string indent) { if (parameters.Count == 0) { return; } var sw = gen_info.Writer; for (int i = 0; i < parameters.Count; i++) { var p = parameters [i]; var gen = p.Generatable; string name = p.Name; if (is_set) { name = "value"; } p.CallName = name; foreach (string prep in p.Prepare) { sw.WriteLine(indent + "\t\t\t" + prep); } if (gen is CallbackGen) { var cbgen = gen as CallbackGen; string wrapper = cbgen.GenWrapper(gen_info); switch (p.Scope) { case "notified": sw.WriteLine(indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name); sw.WriteLine(indent + "\t\t\tIntPtr {0};", parameters [i + 1].Name); sw.WriteLine(indent + "\t\t\t{0} {1};", parameters [i + 2].CSType, parameters [i + 2].Name); sw.WriteLine(indent + "\t\t\tif ({0} == null) {{", name); sw.WriteLine(indent + "\t\t\t\t{0} = IntPtr.Zero;", parameters [i + 1].Name); sw.WriteLine(indent + "\t\t\t\t{0} = null;", parameters [i + 2].Name); sw.WriteLine(indent + "\t\t\t} else {"); sw.WriteLine(indent + "\t\t\t\t{0} = (IntPtr) GCHandle.Alloc ({1}_wrapper);", parameters [i + 1].Name, name); sw.WriteLine(indent + "\t\t\t\t{0} = GLib.DestroyHelper.NotifyHandler;", parameters [i + 2].Name, parameters [i + 2].CSType); sw.WriteLine(indent + "\t\t\t}"); break; case "async": sw.WriteLine(indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name); sw.WriteLine(indent + "\t\t\t{0}_wrapper.PersistUntilCalled ();", name); break; case "call": default: if (p.Scope == string.Empty) { Console.WriteLine("Defaulting " + gen.Name + " param to 'call' scope in method " + gen_info.CurrentMember); } sw.WriteLine(indent + "\t\t\t{0} {1}_wrapper = new {0} ({1});", wrapper, name); break; } } } if (ThrowsException) { sw.WriteLine(indent + "\t\t\tIntPtr error = IntPtr.Zero;"); } }
public static int Main(string[] args) { if (args.Length < 2) { Console.WriteLine ("Usage: codegen --generate <filename1...>"); return 0; } bool generate = false; string dir = ""; string assembly_name = ""; string glue_filename = ""; string glue_includes = ""; string gluelib_name = ""; SymbolTable table = SymbolTable.Table; ArrayList gens = new ArrayList (); foreach (string arg in args) { if (arg.StartsWith ("--customdir=")) { Console.WriteLine ("Using .custom files is not supported anymore, use partial classes instead."); return 0; } string filename = arg; if (arg == "--generate") { generate = true; continue; } else if (arg == "--include") { generate = false; continue; } else if (arg.StartsWith ("-I:")) { generate = false; filename = filename.Substring (3); } else if (arg.StartsWith ("--outdir=")) { generate = false; dir = arg.Substring (9); continue; } else if (arg.StartsWith ("--assembly-name=")) { generate = false; assembly_name = arg.Substring (16); continue; } else if (arg.StartsWith ("--glue-filename=")) { generate = false; glue_filename = arg.Substring (16); continue; } else if (arg.StartsWith ("--glue-includes=")) { generate = false; glue_includes = arg.Substring (16); continue; } else if (arg.StartsWith ("--gluelib-name=")) { generate = false; gluelib_name = arg.Substring (15); continue; } Parser p = new Parser (); IGeneratable[] curr_gens = p.Parse (filename); table.AddTypes (curr_gens); if (generate) gens.AddRange (curr_gens); } // Now that everything is loaded, validate all the to-be- // generated generatables and then remove the invalid ones. ArrayList invalids = new ArrayList (); foreach (IGeneratable gen in gens) { if (!gen.Validate ()) invalids.Add (gen); } foreach (IGeneratable gen in invalids) gens.Remove (gen); GenerationInfo gen_info = null; if (dir != "" || assembly_name != "" || glue_filename != "" || glue_includes != "" || gluelib_name != "") gen_info = new GenerationInfo (dir, assembly_name, glue_filename, glue_includes, gluelib_name); foreach (IGeneratable gen in gens) { if (gen_info == null) gen.Generate (); else gen.Generate (gen_info); } ObjectGen.GenerateMappers (); if (gen_info != null) gen_info.CloseGlueWriter (); Statistics.Report(); return 0; }
public abstract void Generate(GenerationInfo geninfo);
public void GenEventHandler(GenerationInfo gen_info) { if (IsEventHandler) return; string ns = container_type.NS; StreamWriter sw = gen_info.OpenStream (EventHandlerName); sw.WriteLine ("namespace " + ns + " {"); sw.WriteLine (); sw.WriteLine ("\tusing System;"); sw.WriteLine (); sw.WriteLine ("\tpublic delegate void " + EventHandlerName + "(object o, " + EventArgsName + " args);"); sw.WriteLine (); sw.WriteLine ("\tpublic class " + EventArgsName + " : GLib.SignalArgs {"); for (int i = 0; i < parms.Count; i++) { sw.WriteLine ("\t\tpublic " + parms[i].CSType + " " + parms[i].StudlyName + "{"); if (parms[i].PassAs != "out") { sw.WriteLine ("\t\t\tget {"); if (SymbolTable.Table.IsInterface (parms [i].CType)) sw.WriteLine ("\t\t\t\treturn {0}Adapter.GetObject (Args [{1}] as GLib.Object);", parms [i].CSType, i); else sw.WriteLine ("\t\t\t\treturn ({0}) Args [{1}];", parms [i].CSType, i); sw.WriteLine ("\t\t\t}"); } if (parms[i].PassAs != "") { sw.WriteLine ("\t\t\tset {"); if (SymbolTable.Table.IsInterface (parms [i].CType)) sw.WriteLine ("\t\t\t\tArgs [{0}] = value is {1}Adapter ? (value as {1}Adapter).Implementor : value;", i, parms [i].CSType); else sw.WriteLine ("\t\t\t\tArgs[" + i + "] = (" + parms[i].CSType + ")value;"); sw.WriteLine ("\t\t\t}"); } sw.WriteLine ("\t\t}"); sw.WriteLine (); } sw.WriteLine ("\t}"); sw.WriteLine ("}"); sw.Close (); }
public void Generate() { GenerationInfo geninfo = new GenerationInfo(ns); Generate(geninfo); }
protected new void GenFields (GenerationInfo gen_info) { int bitfields = 0; bool need_field = true; StreamWriter sw = gen_info.Writer; foreach (StructField field in fields) { if (Union) sw.WriteLine ("\t\t[FieldOffset(0)]"); if (field.IsBitfield) { if (need_field) { sw.WriteLine ("\t\tprivate uint _bitfield{0};\n", bitfields++); need_field = false; } } else need_field = true; field.Generate (gen_info, "\t\t"); } }
public void Generate(GenerationInfo gen_info) { if (!Validate()) { return; } StreamWriter sw = gen_info.Writer; gen_info.CurrentMember = CName; GenerateImport(sw); if (IsStatic) { GenerateStatic(gen_info); } else { sw.WriteLine("\t\t{0} {1}{2} ({3}) {4}", Protection, Safety, name, Signature.ToString(), needs_chaining ? ": base (IntPtr.Zero)" : ""); sw.WriteLine("\t\t{"); if (needs_chaining) { sw.WriteLine("\t\t\tif (GetType () != typeof (" + name + ")) {"); if (Parameters.Count == 0) { sw.WriteLine("\t\t\t\tCreateNativeObject (new string [0], new GLib.Value[0]);"); sw.WriteLine("\t\t\t\treturn;"); } else { ArrayList names = new ArrayList(); ArrayList values = new ArrayList(); for (int i = 0; i < Parameters.Count; i++) { Parameter p = Parameters[i]; if (container_type.GetPropertyRecursively(p.StudlyName) != null) { names.Add(p.Name); values.Add(p.Name); } else if (p.PropertyName != String.Empty) { names.Add(p.PropertyName); values.Add(p.Name); } } if (names.Count == Parameters.Count) { sw.WriteLine("\t\t\t\tArrayList vals = new ArrayList();"); sw.WriteLine("\t\t\t\tArrayList names = new ArrayList();"); for (int i = 0; i < names.Count; i++) { Parameter p = Parameters [i]; string indent = "\t\t\t\t"; if (p.Generatable is ClassBase && !(p.Generatable is StructBase)) { sw.WriteLine(indent + "if (" + p.Name + " != null) {"); indent += "\t"; } sw.WriteLine(indent + "names.Add (\"" + names [i] + "\");"); sw.WriteLine(indent + "vals.Add (new GLib.Value (" + values[i] + "));"); if (p.Generatable is ClassBase && !(p.Generatable is StructBase)) { sw.WriteLine("\t\t\t\t}"); } } sw.WriteLine("\t\t\t\tCreateNativeObject ((string[])names.ToArray (typeof (string)), (GLib.Value[])vals.ToArray (typeof (GLib.Value)));"); sw.WriteLine("\t\t\t\treturn;"); } else { sw.WriteLine("\t\t\t\tthrow new InvalidOperationException (\"Can't override this constructor.\");"); } } sw.WriteLine("\t\t\t}"); } Body.Initialize(gen_info, false, false, ""); sw.WriteLine("\t\t\t{0} = {1}({2});", container_type.AssignToName, CName, Body.GetCallString(false)); Body.Finish(sw, ""); Body.HandleException(sw, ""); } sw.WriteLine("\t\t}"); sw.WriteLine(); Statistics.CtorCount++; }
protected override void GenCtors (GenerationInfo gen_info) { StreamWriter sw = gen_info.Writer; sw.WriteLine ("\t\tpublic static {0} Zero = new {0} ();", QualifiedName); sw.WriteLine(); if (!DisableNew) { sw.WriteLine ("\t\tpublic static " + QualifiedName + " New(IntPtr raw) {"); sw.WriteLine ("\t\t\tif (raw == IntPtr.Zero)"); sw.WriteLine ("\t\t\t\treturn {0}.Zero;", QualifiedName); sw.WriteLine ("\t\t\treturn ({0}) Marshal.PtrToStructure (raw, typeof ({0}));", QualifiedName); sw.WriteLine ("\t\t}"); sw.WriteLine (); } foreach (Ctor ctor in Ctors) ctor.IsStatic = true; base.GenCtors (gen_info); }
public void Initialize(GenerationInfo gen_info) { Initialize(gen_info, false, false, string.Empty); }
public override void Generate(GenerationInfo gen_info) { gen_info.CurrentType = QualifiedName; StreamWriter sw = gen_info.Writer = gen_info.OpenStream (Name); sw.WriteLine ("namespace " + NS + " {"); sw.WriteLine (); sw.WriteLine ("\tusing System;"); sw.WriteLine ("\tusing System.Collections;"); sw.WriteLine ("\tusing System.Runtime.InteropServices;"); sw.WriteLine (); sw.WriteLine ("#region Autogenerated code"); SymbolTable table = SymbolTable.Table; Method ref_, unref, dispose; GetSpecialMethods (out ref_, out unref, out dispose); if (IsDeprecated) sw.WriteLine ("\t[Obsolete]"); sw.Write ("\t{0} partial {1}class " + Name, IsInternal ? "internal" : "public", IsAbstract ? "abstract " : String.Empty); string cs_parent = table.GetCSType(Elem.GetAttribute("parent")); if (cs_parent != "") sw.Write (" : " + cs_parent); else sw.Write (" : GLib.Opaque"); foreach (string iface in managed_interfaces) { if (Parent != null && Parent.Implements (iface)) continue; sw.Write (", " + iface); } sw.WriteLine (" {"); sw.WriteLine (); GenFields (gen_info); GenMethods (gen_info, null, null); GenCtors (gen_info); if (ref_ != null) { ref_.GenerateImport (sw); sw.WriteLine ("\t\tprotected override void Ref (IntPtr raw)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tif (!Owned) {"); sw.WriteLine ("\t\t\t\t" + ref_.CName + " (raw);"); sw.WriteLine ("\t\t\t\tOwned = true;"); sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t}"); sw.WriteLine (); if (ref_.IsDeprecated) { sw.WriteLine ("\t\t[Obsolete(\"" + QualifiedName + " is now refcounted automatically\")]"); if (ref_.ReturnType == "void") sw.WriteLine ("\t\tpublic void Ref () {}"); else sw.WriteLine ("\t\tpublic " + Name + " Ref () { return this; }"); sw.WriteLine (); } } bool finalizer_needed = false; if (unref != null) { unref.GenerateImport (sw); sw.WriteLine ("\t\tprotected override void Unref (IntPtr raw)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tif (Owned) {"); sw.WriteLine ("\t\t\t\t" + unref.CName + " (raw);"); sw.WriteLine ("\t\t\t\tOwned = false;"); sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t}"); sw.WriteLine (); if (unref.IsDeprecated) { sw.WriteLine ("\t\t[Obsolete(\"" + QualifiedName + " is now refcounted automatically\")]"); sw.WriteLine ("\t\tpublic void Unref () {}"); sw.WriteLine (); } finalizer_needed = true; } if (dispose != null) { dispose.GenerateImport (sw); sw.WriteLine ("\t\tprotected override void Free (IntPtr raw)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\t" + dispose.CName + " (raw);"); sw.WriteLine ("\t\t}"); sw.WriteLine (); if (dispose.IsDeprecated) { sw.WriteLine ("\t\t[Obsolete(\"" + QualifiedName + " is now freed automatically\")]"); sw.WriteLine ("\t\tpublic void " + dispose.Name + " () {}"); sw.WriteLine (); } finalizer_needed = true; } if (finalizer_needed) { sw.WriteLine ("\t\tclass FinalizerInfo {"); sw.WriteLine ("\t\t\tIntPtr handle;"); sw.WriteLine (); sw.WriteLine ("\t\t\tpublic FinalizerInfo (IntPtr handle)"); sw.WriteLine ("\t\t\t{"); sw.WriteLine ("\t\t\t\tthis.handle = handle;"); sw.WriteLine ("\t\t\t}"); sw.WriteLine (); sw.WriteLine ("\t\t\tpublic bool Handler ()"); sw.WriteLine ("\t\t\t{"); if (dispose != null) sw.WriteLine ("\t\t\t\t{0} (handle);", dispose.CName); else if (unref != null) sw.WriteLine ("\t\t\t\t{0} (handle);", unref.CName); sw.WriteLine ("\t\t\t\treturn false;"); sw.WriteLine ("\t\t\t}"); sw.WriteLine ("\t\t}"); sw.WriteLine (); sw.WriteLine ("\t\t~{0} ()", Name); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tif (!Owned)"); sw.WriteLine ("\t\t\t\treturn;"); sw.WriteLine ("\t\t\tFinalizerInfo info = new FinalizerInfo (Handle);"); sw.WriteLine ("\t\t\tGLib.Timeout.Add (50, new GLib.TimeoutHandler (info.Handler));"); sw.WriteLine ("\t\t}"); sw.WriteLine (); } #if false Method copy = Methods ["Copy"] as Method; if (copy != null && copy.Parameters.Count == 0) { sw.WriteLine ("\t\tprotected override GLib.Opaque Copy (IntPtr raw)"); sw.WriteLine ("\t\t{"); sw.WriteLine ("\t\t\tGLib.Opaque result = new " + QualifiedName + " (" + copy.CName + " (raw));"); sw.WriteLine ("\t\t\tresult.Owned = true;"); sw.WriteLine ("\t\t\treturn result;"); sw.WriteLine ("\t\t}"); sw.WriteLine (); } #endif sw.WriteLine ("#endregion"); sw.WriteLine ("\t}"); sw.WriteLine ("}"); sw.Close (); gen_info.Writer = null; Statistics.OpaqueCount++; }
public void Generate(GenerationInfo gen_info) { }