Ejemplo n.º 1
0
        public StructField Generate(GenerationInfo gen_info, string indent,
                                    string parent_name, StructABIField prev_field,
                                    StructABIField next, string struct_parent_name,
                                    TextWriter tw)
        {
            StreamWriter sw    = gen_info.Writer;
            var          name  = Elem.GetAttribute("name");
            var          cname = Elem.GetAttribute("cname");

            if (!unique_field)
            {
                parent_name = parent_name + '.' + name;
            }

            StructABIField next_field = null;

            sw.WriteLine(indent + "// union struct " + parent_name);
            for (int i = 0; i < fields.Count; i++)
            {
                var field = fields[i];
                next_field = fields.Count > i + 1 ? fields[i + 1] : null;

                field.parent_structure_name = parent_name;

                field.Generate(gen_info, indent, prev_field, next_field, struct_parent_name,
                               tw);

                prev_field = field;
            }

            sw.WriteLine(indent + "// End " + parent_name);
            sw.WriteLine();

            return(prev_field);
        }
Ejemplo n.º 2
0
        public void EnsureParentStructName(string parent_name)
        {
            var name = Elem.GetAttribute("name");

            if (!unique_field)
            {
                parent_name = parent_name + '.' + name;
            }

            StructABIField next_field = null;

            foreach (var field in fields)
            {
                field.parent_structure_name = parent_name;
            }
        }
Ejemplo n.º 3
0
        public override StructABIField Generate(GenerationInfo gen_info, string indent,
                                                StructABIField prev_field, StructABIField next_field, string parent_name,
                                                TextWriter tw)
        {
            StreamWriter sw    = gen_info.Writer;
            var          name  = Elem.GetAttribute("name");
            var          cname = Elem.GetAttribute("cname");

            foreach (UnionSubstruct _struct in substructs)
            {
                _struct.EnsureParentStructName(cname);
            }

            foreach (UnionSubstruct _struct in substructs)
            {
                _struct.Generate(gen_info, indent + "\t", cname, prev_field,
                                 next_field, parent_name, tw);
            }

            base.Generate(gen_info, indent, prev_field, next_field, parent_name, null);


            return(this);
        }
Ejemplo n.º 4
0
        protected ClassBase(XmlElement ns, XmlElement elem) : base(ns, elem)
        {
            deprecated       = elem.GetAttributeAsBoolean("deprecated");
            isabstract       = elem.GetAttributeAsBoolean("abstract");
            abi_fields_valid = true;
            string parent_type = Elem.GetAttribute("parent");

            int num_abi_fields = 0;

            foreach (XmlNode node in elem.ChildNodes)
            {
                if (!(node is XmlElement))
                {
                    continue;
                }
                XmlElement     member    = (XmlElement)node;
                StructABIField abi_field = null;

                // Make sure ABI fields are taken into account, even when hidden.
                if (node.Name == "field")
                {
                    num_abi_fields += 1;
                    // Skip instance parent struct if present, taking into account
                    // bindinator broken behaviour concerning parent field (ie.
                    // marking it as pointer, somehow risky but good enough for now.)
                    if (num_abi_fields != 1 ||
                        parent_type == "" ||
                        (member.GetAttribute("type").Replace("*", "") != parent_type
                        ))
                    {
                        abi_field = new StructABIField(member, this, "abi_info");
                        abi_fields.Add(abi_field);
                    }
                }
                else if (node.Name == "union")
                {
                    abi_field = new UnionABIField(member, this, "abi_info");
                    abi_fields.Add(abi_field);
                }

                if (member.GetAttributeAsBoolean("hidden"))
                {
                    continue;
                }

                string name;
                switch (node.Name)
                {
                case "method":
                    name = member.GetAttribute("name");
                    while (methods.ContainsKey(name))
                    {
                        name += "mangled";
                    }
                    methods.Add(name, new Method(member, this));
                    break;

                case "property":
                    name = member.GetAttribute("name");
                    while (props.ContainsKey(name))
                    {
                        name += "mangled";
                    }
                    props.Add(name, new Property(member, this));
                    break;

                case "field":
                    // FIXME Generate callbacks.
                    if (member.GetAttributeAsBoolean("is_callback"))
                    {
                        continue;
                    }

                    name = member.GetAttribute("name");
                    while (fields.ContainsKey(name))
                    {
                        name += "mangled";
                    }

                    var field = new ObjectField(member, this);
                    field.abi_field = abi_field;
                    fields.Add(name, field);
                    break;

                case "implements":
                    ParseImplements(member);
                    break;

                case "constructor":
                    ctors.Add(new Ctor(member, this));
                    break;

                case "constant":
                    name = member.GetAttribute("name");
                    constants.Add(name, new Constant(member));
                    break;

                default:
                    break;
                }
            }
        }
Ejemplo n.º 5
0
        protected void GenerateStructureABI(GenerationInfo gen_info, List <StructABIField> _fields,
                                            string info_name, string structname)
        {
            string cs_parent_struct = null;

            if (_fields == null)
            {
                _fields = abi_fields;
            }

            LogWriter log = new LogWriter(QualifiedName);

            if (!CheckABIStructParent(log, out cs_parent_struct))
            {
                return;
            }

            StreamWriter sw = gen_info.Writer;

            var _new = "";

            if (cs_parent_struct != "")
            {
                _new = "new ";
            }

            sw.WriteLine();
            sw.WriteLine("\t\t// Internal representation of the wrapped structure ABI.");
            sw.WriteLine("\t\tstatic GLib.AbiStruct _" + info_name + " = null;");
            sw.WriteLine("\t\tstatic public " + _new + "GLib.AbiStruct " + info_name + " {");
            sw.WriteLine("\t\t\tget {");
            sw.WriteLine("\t\t\t\tif (_" + info_name + " == null)");

            // Generate Tests
            var using_parent_fields = false;

            if (_fields.Count > 0)
            {
                sw.WriteLine("\t\t\t\t\t_" + info_name + " = new GLib.AbiStruct (new List<GLib.AbiField>{ ");

                if (gen_info.CAbiWriter != null)
                {
                    gen_info.CAbiWriter.WriteLine("\tg_print(\"\\\"sizeof({0})\\\": \\\"%\" G_GUINT64_FORMAT \"\\\"\\n\", (guint64) sizeof({0}));", structname);
                    gen_info.AbiWriter.WriteLine("\t\t\tConsole.WriteLine(\"\\\"sizeof({0})\\\": \\\"\" + {1}.{2}." + info_name + ".Size + \"\\\"\");", structname, NS, Name);
                }
            }
            else
            {
                if (cs_parent_struct != "")
                {
                    sw.WriteLine("\t\t\t\t\t_" + info_name + " = new GLib.AbiStruct ({0}.{1}.Fields);", cs_parent_struct, info_name);
                    using_parent_fields = true;
                }
                else
                {
                    sw.WriteLine("\t\t\t\t\t_" + info_name + " = new GLib.AbiStruct (new List<GLib.AbiField>{ ");
                    using_parent_fields = false;
                }
            }

            StructABIField prev = null;
            StructABIField next = null;

            StringWriter field_alignment_structures_writer = new StringWriter();

            for (int i = 0; i < _fields.Count; i++)
            {
                var field = _fields[i];
                next = _fields.Count > i + 1 ? _fields[i + 1] : null;


                prev = field.Generate(gen_info, "\t\t\t\t\t", prev, next, cs_parent_struct,
                                      field_alignment_structures_writer);
                if (field.IsPadding)
                {
                    continue;
                }
                var union = field as UnionABIField;
                if (union == null && gen_info.CAbiWriter != null && !field.IsBitfield)
                {
                    gen_info.AbiWriter.WriteLine("\t\t\tConsole.WriteLine(\"\\\"{0}.{3}\\\": \\\"\" + {1}.{2}." + info_name + ".GetFieldOffset(\"{3}\") + \"\\\"\");", structname, NS, Name, field.CName);
                    gen_info.CAbiWriter.WriteLine("\tg_print(\"\\\"{0}.{1}\\\": \\\"%\" G_GUINT64_FORMAT \"\\\"\\n\", (guint64) G_STRUCT_OFFSET({0}, {1}));", structname, field.CName);
                }
            }

            if (_fields.Count > 0 && gen_info.CAbiWriter != null)
            {
                gen_info.AbiWriter.Flush();
                gen_info.CAbiWriter.Flush();
            }

            if (!using_parent_fields)
            {
                sw.WriteLine("\t\t\t\t\t});");
            }
            sw.WriteLine();
            sw.WriteLine("\t\t\t\treturn _" + info_name + ";");
            sw.WriteLine("\t\t\t}");
            sw.WriteLine("\t\t}");
            sw.WriteLine();

            sw.WriteLine(field_alignment_structures_writer.ToString());
            sw.WriteLine("\t\t// End of the ABI representation.");
            sw.WriteLine();
        }
Ejemplo n.º 6
0
        public virtual StructABIField Generate(GenerationInfo gen_info, string indent,
                                               StructABIField prev_field, StructABIField next_field, string parent_name,
                                               TextWriter structw)
        {
            StreamWriter sw  = gen_info.Writer;
            IGeneratable gen = SymbolTable.Table[CType];

            sw.WriteLine("{0}\tnew GLib.AbiField(\"{1}\"", indent, CName);

            indent = indent + "\t\t";
            if (prev_field != null)
            {
                sw.WriteLine(indent + ", -1");
            }
            else
            {
                if (parent_name != "")
                {
                    sw.WriteLine(indent + ", " + parent_name + "." + abi_info_name + ".Fields");
                }
                else
                {
                    sw.WriteLine(indent + ", 0");
                }
            }

            sw.WriteLine(indent + ", " + GenerateGetSizeOf(""));

            var prev_field_name = prev_field != null ? "\"" + prev_field.CName + "\"" : "null";

            sw.WriteLine(indent + ", " + prev_field_name);

            var container_name   = container_type.CName.Replace(".", "_");
            var sanitized_name   = CName.Replace(".", "_");
            var alig_struct_name = container_name + "_" + sanitized_name + "Align";
            var next_field_name  = next_field != null ? "\"" + next_field.CName + "\"" : "null";

            sw.WriteLine(indent + ", " + next_field_name);

            if (structw != null)
            {
                string min_align = gen != null?gen.GenerateAlign() : null;

                // Do not generate structs if the type is a simple pointer.
                if (IsCPointer())
                {
                    min_align = "(uint) Marshal.SizeOf(typeof(IntPtr))";
                }

                if (IsBitfield)
                {
                    min_align = "1";
                }

                if (min_align == null)
                {
                    var tmpindent = "\t\t";
                    structw.WriteLine(tmpindent + "[StructLayout(LayoutKind.Sequential)]");
                    structw.WriteLine(tmpindent + "public struct " + alig_struct_name);
                    structw.WriteLine(tmpindent + "{");
                    structw.WriteLine(tmpindent + "\tsbyte f1;");
                    base.Generate(gen_info, tmpindent + "\t", true, structw);
                    structw.WriteLine(tmpindent + "}");
                    structw.WriteLine();

                    var fieldname = SymbolTable.Table.MangleName(CName).Replace(".", "_");
                    if (IsArray && IsNullTermArray)
                    {
                        fieldname += "Ptr";
                    }
                    sw.WriteLine(indent + ", (long) Marshal.OffsetOf(typeof(" + alig_struct_name + "), \"" + fieldname + "\")");
                }
                else
                {
                    sw.WriteLine(indent + ", " + min_align);
                }
            }

            gen_info.Writer = sw;

            uint bits    = 0;
            var  bitsstr = elem.GetAttribute("bits");

            if (bitsstr != null && bitsstr != "")
            {
                bits = (uint)Int32.Parse(bitsstr);
            }

            sw.WriteLine(indent + ", " + bits);
            sw.WriteLine(indent + "),");

            return(this);
        }