summaryrefslogtreecommitdiffstats
path: root/qtsharp/src/generator/Printer.cs
blob: c71bed7cc373a500d6eea946d26e1005781dfa91 (plain)
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
// A Qt to C# binding generator.
//
// Copyright (C) 2002  Adam Treat ([email protected])
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.

using System;
using System.IO;
using System.Text;
using System.Collections;

namespace QtCSharp {

	public class Printer {

		TQType qtype;
		string tab, directory;

		public Printer (TQType qtype, string directory)
		{
			this.qtype = qtype;
			this.directory = directory;
			tab = "\t";
			if (qtype.IsInterface)
				PrintInterface ();
			else
				Print ();
		}

		public void Print ()
		{
			StreamWriter writer = new StreamWriter(directory + Path.DirectorySeparatorChar +
				qtype.Name + ".cs", false, new ASCIIEncoding());
			writer.Write(Header);
			writer.Write(StartClass(qtype.Name, qtype.Access, new ArrayList(qtype.TQAncestors)));

			foreach (TQEnum qenum in qtype.TQEnums) {
				writer.Write(StartEnum(qenum.Name, qenum.Access, qenum.Type));
				writer.Write(Items(qenum.TQItems));
				writer.Write(EndMember);
			}

			foreach (TQCtor qctor in qtype.TQCtors) {
				if (qctor.Throttle) {
				} else if (qctor.Inherited) {
					writer.Write (WriteInherited (qctor.Name, qctor.Access));
				} else if (qctor.Dummy) {
					writer.Write (WriteDummy (qctor.Name, qctor.Access));
				} else if (qctor.Boxer) {
					writer.Write (WriteBoxer (qctor.Name, qctor.Access));
				} else if (!qctor.Overload) {
					writer.Write (ImportAttrib);
					writer.Write (ImportCtorCall (qctor.Name, qctor.Id, qctor.PinvokeParams));
					writer.Write (StartCtor (qctor.Name, qctor.Access, qctor.CSharpParams));
					writer.Write (RawObject (qctor.Name, qctor.Id, qctor.PinvokeCallParams));
					writer.Write (CheckParent (qctor.Parent));
					writer.Write (Register);
					writer.Write (DefaultConnections ());
					writer.Write (EndMember);
				} else {
					writer.Write (OverLoadCtor(qctor.Name, qctor.Access, qctor.CSharpParams, qctor.OverloadParams));
				}
			}

			writer.Write (StartDCtor (qtype.Name));
			writer.Write (DCtorCall);
			writer.Write (EndMember);

			//writer.Write (DisposePublic ());

			foreach (TQDCtor qdctor in qtype.TQDCtors) {
				writer.Write (ImportAttrib);
				writer.Write (ImportDCtorCall (qdctor.Name));
			}

			//writer.Write (DisposeProtected ());
			writer.Write (Delete ());

			foreach (TQMethod qmethod in qtype.TQMethods) {
				if (qmethod.Throttle) {
				} else if (!qmethod.Overload) {
					writer.Write(ImportAttrib);
					if (qmethod.Access == "public" || qmethod.Access == "protected" || qmethod.Access == "internal") {
						writer.Write(ImportMethodCall(qtype.Name, qmethod.Name, qmethod.Id, qmethod.PinvokeReturn, qmethod.PinvokeParams));
						writer.Write(StartMethod(qmethod.PascalName, qmethod.Access, qmethod.Return, qmethod.CSharpParams));
						writer.Write(MethodCall(qmethod.TQStringReturn, qmethod.Boxer, qtype.Name, qmethod.Name, "rawObject", qmethod.Id, qmethod.Return, qmethod.PinvokeCallParams));
					} else {
						writer.Write(StaticImportMethodCall(qtype.Name, qmethod.Name, qmethod.Id, qmethod.PinvokeReturn, qmethod.PinvokeParams));
						writer.Write(StartMethod(qmethod.PascalName, qmethod.Access, qmethod.Return, qmethod.CSharpParams));
						writer.Write(StaticMethodCall(qmethod.TQStringReturn, qmethod.Boxer, qtype.Name, qmethod.Name, qmethod.Id, qmethod.Return, qmethod.PinvokeCallParams));
					}
					writer.Write(EndMember);
				} else {
					writer.Write(OverLoadMethod(qmethod.PascalName, qmethod.Access, qmethod.Return, qmethod.CSharpParams, qmethod.OverloadParams));
				}
			}

			foreach (TQAncestor qancestor in qtype.TQAncestors) {
				writer.Write("\n\n"+tab+tab+"// Begin interface methods.\n");
				if (qancestor.IsInterface) {
					Printer printer = new Printer (qancestor, directory);
					foreach (TQMethod qmethod in qancestor.TQMethods) {
						if (qmethod.Throttle) {
						} else if (!qmethod.Overload) {
							writer.Write(ImportAttrib);
							if (qmethod.Access == "public" || qmethod.Access == "protected") {
								writer.Write(ImportMethodCall(qancestor.Name, qmethod.Name, qmethod.Id, qmethod.PinvokeReturn, qmethod.PinvokeParams));
								writer.Write(StartMethod(qmethod.PascalName, qmethod.Access, qmethod.Return, qmethod.CSharpParams));
								writer.Write(MethodCall(qmethod.TQStringReturn, qmethod.Boxer, qancestor.Name, qmethod.Name, qancestor.Name+" ()", qmethod.Id, qmethod.Return, qmethod.PinvokeCallParams));
							} else {
								writer.Write(StaticImportMethodCall(qancestor.Name, qmethod.Name, qmethod.Id, qmethod.PinvokeReturn, qmethod.PinvokeParams));
								writer.Write(StartMethod(qmethod.PascalName, qmethod.Access, qmethod.Return, qmethod.CSharpParams));
								writer.Write(StaticMethodCall(qmethod.TQStringReturn, qmethod.Boxer, qancestor.Name, qmethod.Name, qmethod.Id, qmethod.Return, qmethod.PinvokeCallParams));
							}
							writer.Write(EndMember);
						} else {
							writer.Write(OverLoadMethod(qmethod.PascalName, qmethod.Access, qmethod.Return, qmethod.CSharpParams, qmethod.OverloadParams));
						}
					}
				}
			}

			writer.Write(EndClass);
			writer.Write(Footer);
			writer.Close();
		}

		public void PrintInterface ()
		{
			StreamWriter writer = new StreamWriter(directory + Path.DirectorySeparatorChar +
				qtype.IName + ".cs", false, new ASCIIEncoding());
			writer.Write(Header);
			writer.Write(StartClass(qtype.IName, "public", qtype.TQAncestors));

			foreach (TQMethod qmethod in qtype.TQMethods) {
				if (qmethod.Throttle || qmethod.Access == "public static" || qmethod.Access == "protected") {
				} else {
					writer.Write(IMethod(qmethod.PascalName, qmethod.Return, qmethod.CSharpParams));
				}
			}
			writer.Write("\n"+tab+tab+" IntPtr "+qtype.Name+" ();");
			writer.Write(EndClass);
			writer.Write(Footer);
			writer.Close();
		}

		public string StartClass (string name, string access, ArrayList qancestors)
		{
			StringBuilder sb = new StringBuilder ();

			if (qtype.IsInterface) {
				sb.Append(tab+access+" interface "+name+" {");
				return sb.ToString ();
			}

			sb.Append(tab+access+" class "+name+" : ");
			if (qancestors.Count == 0) sb.Append ("QtSupport");
			foreach (TQAncestor qancestor in qancestors) {
				if (!qancestor.IsInterface) {
					if (qancestor.Name == "Qt") {
						sb.Append ("QtSupport");
						break;
					} else {
						sb.Append (qancestor.Name);
						qancestors.Remove (qancestor);
						break;
					}
				}
			}

			foreach (TQAncestor qancestor in qancestors) {
				if (qancestor.IsInterface) {
						sb.Append (", "+qancestor.IName);
				}
			}

			sb.Append (", IDisposable {");
			return sb.ToString ();
		}

		public string EndClass
		{
			get {return "\n"+tab+"}";}
		}

		public string StartEnum (string name, string access, string type)
		{
			if (type != null)
				return 	"\n\n"+tab+tab+access+" enum "+name+" : "+type+" {";
			else
				return 	"\n\n"+tab+tab+access+" enum "+name+" {";
		}

		public string WriteInherited (string name, string access)
		{
			return 	"\n\n"+tab+tab+access+" "+name+" () : this (TQNull.Instance) {}";
		}

		public string WriteDummy (string name, string access)
		{
			return 	"\n\n"+tab+tab+access+" "+name+" (TQNull dummy) : base (TQNull.Instance) {}";
		}

		public string WriteBoxer (string name, string access)
		{
			return 	"\n\n" +
			        tab+tab+access+" "+name+" (IntPtr ptr) : this (TQNull.Instance)\n"+tab+tab+"{\n" +
			        tab+tab+tab+"rawObject = ptr;\n" +
					tab+tab+tab+"RegisterObject(this);\n" +
					tab+tab+"}";
		}

		public string CheckParent (bool check)
		{
			if (check)
				return "\n\n\t\t\tif ((qparent = parent) != null)\n" +
					   "\t\t\t\parent.AddChild (this);\n";
			else
				return "";
		}

		public string StartCtor (string name, string access, ArrayList csharpparams)
		{
			return 	"\n"+tab+tab+access+" "+name+" ("+Params(csharpparams)+") : this (TQNull.Instance)\n"+tab+tab+"{\n";
		}

		public string StartDCtor (string name)
		{
			return 	"\n\n"+tab+tab+"~"+name+" ()\n"+tab+tab+"{\n";
		}

		public string StartMethod (string name, string access, string returntype, ArrayList csharpparams)
		{
			string str = "\n"+tab+tab+access+" "+returntype+" "+name+" ("+Params(csharpparams)+")\n"+tab+tab+"{\n";

			// TQEvents should never be disposed inside C#
			if (access.IndexOf ("static") <= 0)
				str += "\t\t\tif (disposed)\n\t\t\t\tthrow new ObjectDisposedException (this+\": Attempted use of disposed object\");\n\n";

			return str;
		}

		public string IMethod (string name, string returntype, ArrayList csharpparams)
		{
			return 	"\n"+tab+tab+" "+returntype+" "+name+" ("+Params(csharpparams)+");";
		}

		public string EndMember
		{
			get {return "\n"+tab+tab+"}";}
		}

		public string OverLoadCtor (string name, string access, ArrayList csharpparams, ArrayList overloadparams)
		{
			return 	"\n\n"+tab+tab+access+" "+name+" ("+Params(csharpparams)+") : this ("+Params(overloadparams)+") {}";
		}

		public string OverLoadMethod (string name, string access, string returntype, ArrayList csharpparams, ArrayList overloadparams)
		{
			string str;

			if (returntype == "void")
				str = name + "(" + Params(overloadparams) + ");\n" + tab + tab;
			else
				str = "return "+name+"("+Params(overloadparams)+");\n" + tab+tab;

			return "\n\n"+tab+tab+access+" "+returntype+" "+name +
				" ("+Params(csharpparams)+")\n" + tab+tab+"{\n" + tab+tab+tab+ str + "}";
		}

		public string Items (ArrayList qitems)
		{
			int count = 0;
			StringBuilder sb = new StringBuilder();
			foreach (TQItem qitem in qitems) {
				count++;
				sb.Append("\n"+tab+tab+tab+qitem.Name+" = "+qitem.Value);
				if (count != qitems.Count)
					sb.Append(",");
			}
			return sb.ToString();
		}

		public string Params (ArrayList qparams)
		{
			int count = 0;
			StringBuilder sb = new StringBuilder();
			foreach (TQParam qparam in qparams) {
				count++;
				if (qparam.Type != "")
					sb.Append(qparam.Type+" "+qparam.Name);
				else
					sb.Append(qparam.Name);

				if (count != qparams.Count)
					sb.Append(", ");
			}
			return sb.ToString();
		}

		public string ImportCtorCall (string name, string id, ArrayList qparams)
		{
			if (id != "0")
				return "\n"+tab+tab+"private static extern IntPtr qt_new_"+name+id+" ("+Params(qparams)+");";
			else
				return "\n"+tab+tab+"private static extern IntPtr qt_new_"+name+" ("+Params(qparams)+");";
		}

		public string ImportDCtorCall (string name)
		{
			return "\n"+tab+tab+"private static extern void qt_del_"+name+" (IntPtr raw);";
		}

		public string ImportMethodCall (string type, string name, string id, string returntype, ArrayList qparams)
		{
			if (name.StartsWith ("Q_"))
				name = name.Replace ("Q_", "");
			string comma = "";
			if (qparams.Count != 0) comma = ", ";
			if (id != "0")
				return "\n"+tab+tab+"private static extern "+returntype+" qt_"+type+"_"+name+id+" (IntPtr raw"+comma+Params(qparams)+");";
			else
				return "\n"+tab+tab+"private static extern "+returntype+" qt_"+type+"_"+name+" (IntPtr raw"+comma+Params(qparams)+");";
		}

		public string StaticImportMethodCall (string type, string name, string id, string returntype, ArrayList qparams)
		{
			if (name.StartsWith ("Q_"))
				name = name.Replace ("Q_", "");
			if (id != "0")
				return "\n"+tab+tab+"private static extern "+returntype+" qt_"+type+"_"+name+id+" ("+Params(qparams)+");";
			else
				return "\n"+tab+tab+"private static extern "+returntype+" qt_"+type+"_"+name+" ("+Params(qparams)+");";
		}

		public string RawObject (string name, string id, ArrayList qparams)
		{
			if (qtype.IsTQObject) {
				ArrayList newparams = new ArrayList ();

				foreach (TQParam parm in qparams) {
					//Console.WriteLine ("--> {0}", parm.Name);
					if (parm.Name == "parent.RawObject") {
						TQParam newparm = parm.Clone() as TQParam;
						newparm.Name = "parent != null ? parent.RawObject : IntPtr.Zero";
						newparams.Add (newparm);
					}
					else
						newparams.Add (parm);
				}

				if (id != "0")
					return tab+tab+tab+"rawObject = qt_new_"+name+id+" ("+Params(newparams)+");";
				else
					return tab+tab+tab+"rawObject = qt_new_"+name+" ("+Params(newparams)+");";
			}
			else {
				if (id != "0")
					return tab+tab+tab+"rawObject = qt_new_"+name+id+" ("+Params(qparams)+");";
				else
					return tab+tab+tab+"rawObject = qt_new_"+name+" ("+Params(qparams)+");";
			}
		}

		public string DCtorCall {
			get {
				return tab+tab+tab+"Dispose (false);";
			}
		}

		public string MethodCall (bool qstring, bool boxer, string type, string name, string instPtr, string id, string ReturnType, ArrayList qparams)
		{
			if (name.StartsWith ("Q_"))
				name = name.Replace ("Q_", "");

			string ret = "";
			string comma = qparams.Count == 0 ? "" : ", ";
			string newid = id == "0" ? "" : id;

			if (boxer && qstring) {
				ret = "\t\t\treturn new TTQString (qt_"+type+"_"+name+newid+" ("+instPtr+comma+Params (qparams)+"));\n";
			}
			else if (boxer) {
				ret = "\t\t\treturn LookupObject (qt_"+type+"_"+name+newid+" ("+instPtr+comma+Params (qparams)+"), typeof ("+ReturnType+")) as "+ReturnType+";";
			}
			else {
				ret = "qt_"+type+"_"+name+newid+" ("+instPtr+comma+Params (qparams)+");";

				if (ReturnType != "void")
					ret = "return " + ret;

				ret = "\t\t\t" + ret;
			}

			return ret;
		}

		public string StaticMethodCall (bool qstring, bool boxer, string type, string name, string id, string ReturnType, ArrayList qparams)
		{
			if (name.StartsWith ("Q_"))
				name = name.Replace ("Q_", "");

			string ret;
			string newid = id == "0" ? "" : id;

			if (boxer && qstring) {
				ret = "\t\t\treturn new TTQString (qt_"+type+"_"+name+newid+" ("+Params (qparams)+"));\n";
			}
			else if (boxer) {
				ret = "\t\t\treturn LookupObject (qt_"+type+"_"+name+newid+" ("+Params (qparams)+"), typeof ("+ReturnType+")) as "+ReturnType+";\n";
			}
			else {
				ret = "qt_"+type+"_"+name+newid+" ("+Params (qparams)+");";

				if (ReturnType != "void")
					ret = "return " + ret;

				ret = "\t\t\t" + ret;
			}

			return ret;
		}

		public string ImportAttrib
		{
			get {return "\n\n"+tab+tab+"[DllImport(\"libqtc\", CharSet=CharSet.Ansi)]";}
		}

		public string Import () {
			if (!qtype.IsInterface) {
				return tab + "using System.Runtime.InteropServices;\n\n";
			} else {
				return "\n";
			}
		}

		public string Header
		{
			get {
			return	"// "+qtype.Name+".cs - A Qt to C# binding.\n" +
					"//\n" +
					"// Copyright (C) 2002  Adam Treat ([email protected])\n" +
					"//\n" +
					"// This program is free software; you can redistribute it and/or\n" +
					"// modify it under the terms of the GNU General Public License\n" +
					"// as published by the Free Software Foundation; either version 2\n" +
					"// of the License, or (at your option) any later version.\n" +
					"//\n" +
					"// This program is distributed in the hope that it will be useful,\n" +
					"// but WITHOUT ANY WARRANTY; without even the implied warranty of\n" +
					"// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\n" +
					"// GNU General Public License for more details.\n" +
					"//\n" +
					"// You should have received a copy of the GNU General Public License\n" +
					"// along with this program; if not, write to the Free Software\n" +
					"// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.\n" +
					"//\n" +
					"// Generated File.  Do Not Modify.\n" +
					"\n" +
					"namespace Qt {\n" +
					"\n" +
					tab + "using Qt;\n" +
    				tab + "using System;\n" + Import ();
			}
		}

		public string Footer
		{
			get {return "\n}";}
		}

		public string Register
		{
			get
			{
				return "\n"+tab+tab+tab+"RegisterObject (this);";
			}
		}

		public string Delete ()
		{

			string ret = "\n" +
						 "\t\tinternal override void Delete ()\n" +
						 "\t\t{\n";

			// Let Qt manage TQEvents
			if (! (qtype.Name.StartsWith ("Q") && qtype.Name.EndsWith ("Event")))
			{
				ret +=  "\t\t\tif (deleted) return;\n\n";

				if (qtype.TQDCtors.Count > 0)
					ret += "\t\t\tqt_del_"+qtype.Name+" (rawObject);\n";
				else
					ret = "\n" + ret + "\t\t\t// libqtc lacks a qt_del_"+qtype.Name+" function\n";

				ret += "\t\t\tdeleted = true;\n";
			}

			ret += "\t\t}";

			return ret;

		}

		public string DefaultConnections ()
		{
			if (qtype.IsTQObject)
				return "\n\t\t\tConnect (this, TQT_SIGNAL (\"destroyed ()\"), TQT_SLOT (\"NativeDestroyed ()\"));";
			else
				return "";
		}
	}
}