File: StructABIField.cs

package info (click to toggle)
gnome-subtitles 1.8-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 103,144 kB
  • sloc: xml: 406,395; cs: 364,495; ansic: 3,104; perl: 1,477; sh: 769; python: 545; javascript: 500; makefile: 49
file content (144 lines) | stat: -rw-r--r-- 4,089 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
namespace GtkSharp.Generation {

	using System;
	using System.Collections;
	using System.IO;
	using System.Xml;

	public class StructABIField : StructField {
		protected new ClassBase container_type;
		public string parent_structure_name;
		public string abi_info_name;

		public StructABIField (XmlElement elem, ClassBase container_type,
				string info_name) : base (elem, container_type) {
			this.container_type = container_type;
			this.getOffsetName = null;
			this.abi_info_name = info_name;
		}

		public override string CName {

			get {
				if (parent_structure_name != null)
					return parent_structure_name + '.' + elem.GetAttribute ("cname");
				return elem.GetAttribute ("cname");
			}
		}

		public override void Generate (GenerationInfo gen_info, string indent) {
			base.Generate(gen_info, indent);
		}

		// All field are visible and private
		// as the goal is to respect the ABI
		protected override string Access {
			get {
				return "private";
			}
		}

		public override bool Hidden {
			get {
				return false;
			}
		}

		public override bool Validate (LogWriter log)
		{
			string cstype = SymbolTable.Table.GetCSType(CType, true);

			if (elem.GetAttributeAsBoolean("is_callback"))
				return true;

			if (cstype == null || cstype == "") {
				log.Warn (" field \"" + CName + "\" has no cstype, can't generate ABI field.");
				return false;
			}

			if (!base.Validate (log))
				return false;

			return true;
		}

		public void SetGetOffseName() {
			this.getOffsetName = "Get" + CName + "Offset";
		}

		public override string GenerateGetSizeOf(string indent) {
			return base.GenerateGetSizeOf(indent) + " // " + CName;
		}

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