Exemplo n.º 1
0
		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);
			}
		}
Exemplo n.º 2
0
        public override void Generate(GenerationInfo gen_info, string indent)
        {
            if (Hidden)
            {
                return;
            }

            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)
            {
                sw.WriteLine(indent + "[MarshalAs (UnmanagedType.ByValArray, SizeConst=" + ArrayLength + ")]");
                sw.WriteLine(indent + "{0} {1} {2};", Access, CSType, StudlyName);
            }
            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))
            {
                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.FromNativeReturn(CType, Name) + "; }");
                    sw.WriteLine(indent + "}");
                }
            }
            else if (IsPointer && CSType != "string")
            {
                // FIXME: probably some fields here which should be visible.
                sw.WriteLine(indent + "private {0} {1};", CSType, Name);
            }
            else
            {
                sw.WriteLine(indent + "{0} {1} {2};", Access, CSType, Access == "public" ? StudlyName : Name);
            }
        }
Exemplo n.º 3
0
        public void Generate(StreamWriter sw)
        {
            string lname, call;

            if (parms != null)
            {
                parms.Validate();
                call  = "(" + parms.CallString + ")";
                lname = "(" + parms.NameString + ")";
            }
            else
            {
                call  = "(BUG);";
                lname = "THIS_IS_A_BUG";
            }

            string rettype = elem.GetAttribute("type");
            string name    = elem.GetAttribute("name");
            string s_ret   = SymbolTable.GetCSType(rettype);

            if (parms.UnknownTypes || s_ret == "_ERROR_")
            {
                Console.WriteLine("skipped: " + name);
                Statistics.IgnoreCount++;
                return;
            }

            if (s_ret == "void[]")
            {
                s_ret = "IntPtr";
            }

            sw.WriteLine("\t\t[DllImport(\"" + SymbolTable.GetDllName(ns) +
                         "\", CallingConvention=CallingConvention.Cdecl)]");
            sw.WriteLine("\t\tstatic extern " + s_ret + " " + name + " " + call + ";");

            sw.Write("\t\tpublic static " + s_ret + " ");
            sw.Write(name.TrimStart(ns.ToCharArray()) + " ");
            sw.WriteLine(call);
            sw.WriteLine("\t\t{");

            if (s_ret == "void")
            {
                sw.WriteLine("\t\t\t" + name + lname + ";");
            }
            else
            {
                sw.WriteLine("\t\t\treturn " + name + lname + ";");
            }

            sw.WriteLine("\t\t}");
            sw.WriteLine("");

            Statistics.FuncsCount++;
        }
Exemplo n.º 4
0
        public override void Generate(GenerationInfo gen_info)
        {
            gen_info.CurrentType = QualifiedName;

            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();

            sw.WriteLine("#region Autogenerated code");

            SymbolTable table = SymbolTable.Table;

            Method ref_, unref, dispose, set_gvalue;

            GetSpecialMethods(out ref_, out unref, out dispose, out set_gvalue);

            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();

            GenConstants(gen_info);
            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
            if (set_gvalue != null)
            {
                sw.WriteLine("\t\tdelegate IntPtr d_{0}(ref GLib.Value val, IntPtr obj);", set_gvalue.CName);
                sw.WriteLine("\t\tstatic d_{0} {0} = FuncLoader.LoadFunction<d_{0}>(FuncLoader.GetProcAddress(GLibrary.Load(\"{1}\"), \"{0}\"));", set_gvalue.CName, LibraryName);
                sw.WriteLine();
                sw.WriteLine("\t\tpublic void SetGValue (ref GLib.Value val)");
                sw.WriteLine("\t\t{");
                sw.WriteLine("\t\t\t{0} (ref val, Handle);", set_gvalue.CName);
                sw.WriteLine("\t\t}");
                sw.WriteLine();
            }

            GenerateStructureABI(gen_info);
            sw.WriteLine("#endregion");

            sw.WriteLine("\t}");
            sw.WriteLine("}");

            sw.Close();
            gen_info.Writer = null;
            Statistics.OpaqueCount++;
        }
Exemplo n.º 5
0
        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);
            }
            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}");
            sw.WriteLine("}");

            sw.Close();
            gen_info.Writer = null;
            Statistics.ObjectCount++;
        }
        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]");
            }
            GenerateAttribute(sw);
            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();
                }
            }

            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();
                }
            }

            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();
                }
            }

#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}");
            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.OpaqueCount++;
        }
Exemplo n.º 7
0
        public bool Validate()
        {
            call_string = "";
            name_string = "";
            string mang_name;
            bool   need_sep = false;

            foreach (XmlNode parm in elem.ChildNodes)
            {
                //if (parm.Name != "parameter") {
                //	continue;
                //}

                XmlElement p_elem  = (XmlElement)parm;
                string     type    = p_elem.GetAttribute("type");
                string     cs_type = SymbolTable.GetCSType(type);

                if (cs_type == "")
                {
                    return(false);
                }

                if (cs_type == "_ERROR_")
                {
                    unknown_types = true;
                }

                if (p_elem.GetAttribute("array") == "ss")
                {
                    cs_type = "IntPtr";
                }

                if (p_elem.GetAttribute("array") == "t")
                {
                    cs_type += "[]";

                    if (cs_type == "void[]")
                    {
                        cs_type = "IntPtr";
                    }
                }

                if (need_sep)
                {
                    call_string += ", ";
                    name_string += ", ";
                }
                else
                {
                    need_sep = true;
                }

                mang_name = MangleName(p_elem.GetAttribute("name"));

                call_string += (cs_type);
                call_string += (" " + mang_name);
                name_string += (mang_name);
            }

            if (call_string == "void ")
            {
                call_string = "";
                name_string = "";
            }

            return(true);
        }
Exemplo n.º 8
0
        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();

            SymbolTable table = SymbolTable.Table;

            sw.WriteLine("#region Autogenerated code");
            if (IsDeprecated)
            {
                sw.WriteLine("\t[Obsolete]");
            }
            foreach (string attr in customAttrs)
            {
                sw.WriteLine("\t" + attr);
            }
            sw.Write("\t{0} {1}partial class " + Name, IsInternal ? "internal" : "public", IsAbstract ? "abstract " : "");
            string csParent = table.GetCSType(Elem.GetAttribute("parent"));

            if (!string.IsNullOrEmpty(csParent))
            {
                di.objects.Add(CName, QualifiedName);
                sw.Write(" : " + csParent);
            }
            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)
                {
                    if (table.GetClassGen(iface) is InterfaceGen iGen && iGen.Signals != null)
                    {
                        has_sigs = true;
                        break;
                    }
                }
            }

            if (has_sigs && Elem.HasAttribute("parent"))
            {
                GenSignals(gen_info, null);
            }

            GenConstants(gen_info);
            GenClassMembers(gen_info, csParent);
            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;
                            }
                        }

                        all_methods.TryGetValue(m.Name, out Method 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 (!string.IsNullOrEmpty(csParent) && 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++;
        }
Exemplo n.º 9
0
        public override void Generate(GenerationInfo gen_info)
        {
            gen_info.CurrentType = QualifiedName;

            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();

            sw.WriteLine("#region Autogenerated code");

            SymbolTable table = SymbolTable.Table;

            Method ref_, unref, dispose, set_gvalue;

            GetSpecialMethods(out ref_, out unref, out dispose, out set_gvalue);

            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();

            GenConstants(gen_info);
            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();
                }
            }

            // GtkWidgetPath has both gtk_widget_path_unref and gtk_widget_path_free
            // If this happens add only one and prefer unref over free
            bool disposeUnmanagedFuncAdded = 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();
                sw.WriteLine("\t\tprotected override Action<IntPtr> DisposeUnmanagedFunc {");
                sw.WriteLine("\t\t\tget {");
                sw.WriteLine("\t\t\t\treturn " + unref.CName + ";");
                sw.WriteLine("\t\t\t}");
                sw.WriteLine("\t\t}");
                sw.WriteLine();
                disposeUnmanagedFuncAdded = true;
                if (unref.IsDeprecated)
                {
                    sw.WriteLine("\t\t[Obsolete(\"" + QualifiedName + " is now refcounted automatically\")]");
                    sw.WriteLine("\t\tpublic void Unref () {}");
                    sw.WriteLine();
                }
            }

            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 (!disposeUnmanagedFuncAdded)
                {
                    sw.WriteLine("\t\tprotected override Action<IntPtr> DisposeUnmanagedFunc {");
                    sw.WriteLine("\t\t\tget {");
                    sw.WriteLine("\t\t\t\treturn " + dispose.CName + ";");
                    sw.WriteLine("\t\t\t}");
                    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();
                }
            }


#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
            if (set_gvalue != null)
            {
                sw.WriteLine("\t\t[DllImport(\"{0}\", CallingConvention = CallingConvention.Cdecl)]", LibraryName);
                sw.WriteLine("\t\tstatic extern void {0}(ref GLib.Value val, IntPtr obj);", set_gvalue.CName);
                sw.WriteLine();
                sw.WriteLine("\t\tpublic void SetGValue (ref GLib.Value val)");
                sw.WriteLine("\t\t{");
                sw.WriteLine("\t\t\t{0} (ref val, Handle);", set_gvalue.CName);
                sw.WriteLine("\t\t}");
                sw.WriteLine();
            }

            GenerateStructureABI(gen_info);
            sw.WriteLine("#endregion");

            sw.WriteLine("\t}");
            sw.WriteLine("}");

            sw.Close();
            gen_info.Writer = null;
            Statistics.OpaqueCount++;
        }
Exemplo n.º 10
0
        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);
            }
            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 managed_interfaces)
            {
                if (Parent != null && Parent.Implements(iface))
                {
                    continue;
                }
                sw.Write(", " + iface);
            }
            sw.WriteLine(" {");
            sw.WriteLine();

            GenCtors(gen_info);
            GenFields(gen_info);

            GenClassMembers(gen_info, cs_parent);
            GenMethods(gen_info, null, null);

            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}");
            sw.WriteLine("}");

            sw.Close();
            gen_info.Writer = null;
            Statistics.ObjectCount++;
        }
Exemplo n.º 11
0
        public void Generate(GenerationInfo gen_info, string indent, bool use_cnames,
                             TextWriter sw)
        {
            if (Hidden && !use_cnames)
            {
                return;
            }

            visible = Access != "private";

            SymbolTable table = SymbolTable.Table;

            string wrapped = table.GetCSType(CType);

            string wrapped_name = SymbolTable.Table.MangleName(CName);
            string name         = Name;
            string studly_name  = StudlyName;
            string cstype       = CSType;

            IGeneratable gen = table [CType];

            if (use_cnames)
            {
                name = studly_name = wrapped_name = SymbolTable.Table.MangleName(CName).Replace(".", "_");

                var mangen = gen as ManualGen;
                if (mangen != null)
                {
                    if (mangen.AbiType != null)
                    {
                        cstype = mangen.AbiType;
                    }
                }

                if (IsCPointer())
                {
                    cstype = "IntPtr";
                }
            }

            if (IsArray && !IsNullTermArray)
            {
                sw.WriteLine(indent + "[MarshalAs (UnmanagedType.ByValArray, SizeConst=" + ArrayLength + ")]");
                sw.WriteLine(indent + "{0} {1} {2};", Access, cstype, studly_name);
            }
            else if (IsArray && IsNullTermArray)
            {
                sw.WriteLine(indent + "private {0} {1};", "IntPtr", studly_name + "Ptr");
                if ((Readable || Writable) && Access == "public")
                {
                    sw.WriteLine(indent + "public {0} {1} {{", cstype, studly_name);
                    if (Readable)
                    {
                        sw.WriteLine(indent + "\tget {{ return GLib.Marshaller.StructArrayFromNullTerminatedIntPtr<{0}> ({1}); }}",
                                     base.CSType, studly_name + "Ptr");
                    }
                    if (Writable)
                    {
                        sw.WriteLine(indent + "\tset {{ {0} = GLib.Marshaller.StructArrayToNullTerminatedStructArrayIntPtr<{1}> (value); }}",
                                     studly_name + "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 + " " + studly_name + " {");
                    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" ? studly_name : name);
            }
        }
Exemplo n.º 12
0
        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++;
        }