-
Notifications
You must be signed in to change notification settings - Fork 27
/
ctypeswriter.vala
690 lines (634 loc) · 18.2 KB
/
ctypeswriter.vala
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
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/* Copyright GPLv3 - 2009-2019 - pancake */
using Vala;
private class CtypeCompiler {
public GLib.SList<weak CtypeClass> classes;
public CtypeClass cur;
private bool sorted = false;
public string to_string () {
var str = "";
if (!this.sorted)
this.sort ();
foreach (var c in this.classes)
str += c.to_string ();
return str;
}
public CtypeCompiler() {
this.cur = null;
}
public bool contains (string c) {
foreach (var a in this.classes)
if (a.name == c)
return true;
return false;
}
public void sort () {
uint count, count2 = 0;
var n = new GLib.SList<weak CtypeClass>();
do {
count = classes.length ();
foreach (var c in classes) {
if (c.is_satisfied (this)) {
n.append (c);
classes.remove (c);
} else {
warning ("UNSATISFIED "+c.name);
}
}
count2 = classes.length ();
if (count2 > 0 && count == count2) { // detect infinite loop
error ("Cannot compile, infinite loop dependency detected\n");
}
} while (count2 > 0);
/* refill */
foreach (var c in classes) {
if (c.is_satisfied (this)) {
n.prepend (c);
classes.remove (c);
}
}
this.classes = n.copy();
this.sorted = true;
}
public void add_class (string name, string text) {
var cc = new CtypeClass (name);
this.classes.append (cc);
this.cur = cc;
this.cur.append (text);
this.sorted = false;
}
}
private class CtypeClass {
public string name;
public string text;
public GLib.SList<string> deps;
public bool is_satisfied (CtypeCompiler c) {
foreach (var d in deps) {
if (d == this.name) {
stderr.printf ("==> "+d+"\n");
// break;
}
stderr.printf ("--> "+d+"\n");
if (!c.contains (d)) {
return false;
}
}
stderr.printf (" *** Yep. "+name+" is satisfied\n");
return true;
}
public CtypeClass(string? name) {
this.deps = new SList<string>();
this.name = name;
this.text = "";
}
public bool contains(SList<string> a, string b) {
if (b == this.name) {
error ("Classes with self dependencies are not supported");
// return true;
}
foreach (var i in a) {
if (i == b)
return true;
}
return false;
}
public void add_dependency (string d) {
if (!this.contains (deps, d))
deps.append (d);
}
/*
public bool depends_on(string d) {
foreach (var s in deps)
if (s == d)
return true;
return false;
}
*/
public string to_string() {
return this.text;
}
public void append(string s) {
this.text += s;
}
}
public class CtypesWriter : ValabindWriter {
public GLib.List<string> includefiles = new GLib.List<string> ();
CtypeCompiler ctc = new CtypeCompiler ();
string ?ns_pfx;
string delegatestr = "";
public CtypesWriter () {
}
public override string get_filename (string base_name) {
return base_name + ".py";
}
// FIXME duplicate from NodeFFIWriter
void add_includes (Symbol s) {
foreach (string i in Vala.get_ccode_header_filenames (s).split (",")) {
bool include = true;
foreach (string j in includefiles) {
if (i == j) {
include = false;
break;
}
}
if (include) {
includefiles.prepend (i);
}
}
}
// FIXME duplicate from NodeFFIWriter
string sep (string str, string separator) {
if (str.length != 0) {
char last = str[str.length-1];
if (last != '(' && last != '[' && last != '{') {
return str + separator;
}
}
return str;
}
string get_alias (string oname) {
string name = oname;
switch (oname) {
case "break":
case "import":
case "from":
case "del":
name = "_"+oname;
break;
case "continue":
name = "cont";
break;
}
if (name != oname) {
warning ("Method %s renamed to %s".printf (oname, name));
}
return name;
}
string type_name (DataType type, bool retType=false, bool ignoreRef=false) {
if (type == null) {
warning ("Cannot resolve type");
return "__UNRESOLVED_TYPE_OH_PLEASE_KILL_ME__";
}
// HACK is this required?
if (type is EnumValueType)
return "c_long";
// HACK needs proper generic support
if (type is GenericType)
return "c_void_p";
//return type.to_qualified_string ();
if (type is PointerType) {
var tn = type_name ((type as PointerType).base_type, retType, true);
if (tn == "void")
return "c_void_p";
return "POINTER("+tn+")";
}
if (type is ArrayType) {
ArrayType array = type as ArrayType;
string element = type_name (array.element_type, retType);
int len = array_length(array);
if (len < 0) {
return element; //+"*"; // FIXME should this be element+"[]"?
}
return "'"+element+"', * %d".printf (len); // FIXME will this work?
}
if (!ignoreRef && (type is ReferenceType)) {
string unref_type = type_name (type, retType, true);
// HACK just check for the string class instead (how?)
if (unref_type == "char*" || unref_type == "const char*") {
return unref_type;
}
// FIXME should it be & under C++?
return unref_type; //+"_p"; //+"*";
}
string generic = "";
foreach (DataType t in type.get_type_arguments ())
generic = sep (generic, ", ") + type_name (t);
string _type = type.to_string ();
// HACK find a better way to remove generic type args
_type = _type.split ("<", 2)[0];
_type = _type.replace (ns_pfx, "").replace (".", "");
_type = _type.replace ("?","");
_type = _type.replace ("unsigned ", "u");
switch (_type) {
case "bool":
case "gboolean":
return "c_bool";
case "gconstpointer":
case "gpointer":
return "c_void_p";
case "char":
case "gchar":
return "c_char";
case "gint":
return "c_int";
case "uint":
case "guint":
return "c_uint";
case "glong":
return "c_long";
case "ut8":
case "uint8":
case "guint8":
return "c_ubyte";
case "guint16":
case "uint16":
return "c_ushort";
case "gint16":
case "int16":
case "short":
return "c_short";
case "int":
case "st32":
case "int32":
case "gint32":
return "c_int";
case "ut32":
case "uint32":
case "guint32":
return "c_uint";
case "st64":
case "int64":
case "gint64":
return "c_longlong";
case "ut64":
case "uint64":
case "guint64":
// HACK uint64_t doesn't work here because radare2 doesn't use the right type
return "c_ulonglong";
case "gdouble":
return "c_double";
case "gfloat":
return "c_float";
case "string":
return "c_char_p";
//case "const gchar*":
// return "const char*";
// HACK needs proper generic support
case "RList":
return "RList";
case "SdbList":
return "SdbList";
}
return _type;
}
public override void visit_constant (Constant c) {
warning ("Constants not yet supported on ctypes ("+c.name+")");
//var cname = Vala.get_ccode_name (c);
//classes += c.name+" = "+cname+";\n";
}
public override void visit_enum (Vala.Enum e) {
add_includes (e);
// TODO: copy from node
}
public override void visit_struct (Struct s) {
string name = s.get_full_name ().replace (ns_pfx, "").replace (".", "");
visit_struct_or_class (s, name, s.get_fields (), s.get_methods (), null);
}
public override void visit_class (Class c) {
string name = c.get_full_name ().replace (ns_pfx, "").replace (".", "");
visit_struct_or_class (c, name, c.get_fields (), c.get_methods (), c.get_delegates ());
/* walk nested structs and classes */
foreach (Struct s in c.get_structs ()) {
s.accept (this);
}
foreach (Class k in c.get_classes ()) {
k.accept (this);
}
/* TODO: add support for freefun in destructor
string? freefun = null;
if (Vala.is_reference_counting (c))
freefun = Vala.get_ccode_unref_function (c);
else
freefun = Vala.get_ccode_free_function (c);
if (freefun == "")
freefun = null;
var methods = c.get_methods ();
if (freefun != null || methods.size > 0) { ... */
}
private string get_constructor_args(Vala.List<Method> methods) {
foreach (Method m in methods) {
if (m is CreationMethod) {
var str = "";
add_includes (m);
foreach (var foo in m.get_parameters ()) {
str += ", "+get_alias (foo.name);
}
return str;
}
}
return "";
}
string wrappers;
int n = 0;
private void visit_struct_or_class (Symbol s, string name,
Vala.List<Field> fields, Vala.List<Method> methods,
Vala.List<Delegate>? delegates) {
//string cname = Vala.get_ccode_name (s);
add_includes (s);
ctc.add_class (name, "class "+name+"(Structure): #"+n.to_string()+"\n");
stderr.printf ("--> "+name+" ("+n.to_string()+")\n");
n++;
if (fields.size > 0) {
ctc.cur.append ("\t_fields_ = [\n");
foreach (Field f in fields)
f.accept (this);
ctc.cur.append ("\t]\n");
} else {
ctc.cur.append ("\t_fields_ = [\n\t]\n");
}
if (methods.size > 0) {
string call_args = get_constructor_args (methods);
ctc.cur.append ("\tdef __init__(self"+call_args+"):\n");
ctc.cur.append ("\t\tStructure.__init__(self)\n");
// TODO: control how many methods and constructors there are
// TODO: add support for more than one constructor
/* constructors */
wrappers = "";
foreach (Method m in methods)
if (m is CreationMethod)
visit_method (m);
/* helper function */
if (name == "RList") {
ctc.cur.append (
"\tdef to_list(self,type):\n"+
"\t\treturn rlist2array(self,type)\n");
}
if (name == "SdbList") {
ctc.cur.append (
"\tdef to_list(self,type):\n"+
"\t\treturn sdblist2array(self,type)\n");
}
/* delegates */
if (delegates != null)
foreach (Delegate d in delegates)
visit_delegate (d);
ctc.cur.append ("\n\t_o = AddressHolder()\n\n");
/* methods */
foreach (Method m in methods)
if (!(m is CreationMethod))
visit_method (m);
ctc.cur.append (wrappers);
ctc.cur.append ("\n");
}
}
public override void visit_field (Field f) {
if (f.is_private_symbol ())
return;
// HACK don't output fields with C++ keywords as names.
if (f.name == "class")
return;
string field = "";
DataType type = f.variable_type;
var stype = type.to_string ();
if (type is ArrayType) {
ArrayType array = type as ArrayType;
string element = type_name (array.element_type);
warning ("Arrays not yet supported in ctypes bindings");
int len = array_length(array);
if (len < 0) {
field = element + "* " + f.name; // FIXME should this be element+"[]"?
}
field = "'%s', %s * %d".printf (f.name, element, len);
// warning ("Field is array " + element);
// ctc.cur.add_dependency ("RRegItem");
// if dependency type is not a c one
if (element[0] != 'c' && element[1] != '_') {
ctc.cur.add_dependency (element);
} else {
warning ("NODEP " + element);
}
} else {
/* HACK to support generics. this is r2 specific */
if (stype.index_of ("RListIter") != -1) {
stype = "c_void_p"; // XXX RListIter*";
} else if (stype.index_of ("RList") != -1) {
stype = "c_void_p"; // XXX "RList*";
} else if (stype.index_of ("SdbListIter") != -1) {
stype = "c_void_p"; // XXX SdbListIter*";
} else if (stype.index_of ("SdbList") != -1) {
stype = "c_void_p"; // XXX "SdbList*";
} else stype = type_name (type);
field = "\"%s\", %s".printf (f.name, stype);
}
ctc.cur.append ("\t\t(" + field + "),\n");
if (stype[0].isupper () &&
!stype.has_prefix ("POINTER") &&
stype.index_of (".") == -1) {
//stderr.printf ("ADD DEP : "+field+" "+stype+"\n");
ctc.cur.add_dependency (stype);
}
}
public override void visit_delegate (Delegate d) {
string cname = Vala.get_ccode_name (d);
string args = "";
foreach (var p in d.get_parameters ()) {
DataType? bar = p.variable_type;
string? arg_type = type_name (bar);
args += ", "+arg_type;
}
var cn = d.parent_symbol.get_full_name ().replace (ns_pfx, "").replace (".", "");
this.delegatestr += cname + " = CFUNCTYPE ("+cn+args+")\n";
}
public override void visit_method (Method m) {
if (m.is_private_symbol ())
return;
add_includes (m);
string cname = Vala.get_ccode_name (m);
//string alias = get_alias (m.name);
bool is_static = (m.binding & MemberBinding.STATIC) != 0;
bool is_constructor = (m is CreationMethod);
//var parent = m.parent_symbol;
//bool parent_is_class = parent is Class || parent is Struct;
// TODO: Implement contractual support
// m.get_preconditions ();
// m.get_postconditions ();
string ret = type_name (m.return_type, true);
string pyc_args = "";
string def_args = "";
string call_args = "";
string clears = "";
foreach (var foo in m.get_parameters ()) {
//DataType? bar = foo.parameter_type;
DataType? bar = foo.variable_type;
if (bar == null) {
warning ("Unknown datatype "+foo.name+"\n");
continue;
}
string arg_name = get_alias (foo.name);
string? arg_type = type_name (bar);
/* TODO: move to type_name */
if (foo.direction != ParameterDirection.IN) {
var var_name = "";
if (foo.direction == ParameterDirection.OUT)
var_name = "OUTPUT";
else if (foo.direction == ParameterDirection.REF)
var_name = "INOUT";
if (arg_type.index_of ("*") == -1)
arg_type = "POINTER("+arg_type+")";
//applys += "\t%%apply %s %s { %s %s };\n".printf (arg_type, var_name, arg_type, arg_name);
clears += "\t%%clear %s %s;\n".printf (arg_type, arg_name);
}
call_args = sep (call_args, ", ") + arg_name;
def_args = sep (def_args, ", ") + arg_type + " " + arg_name;
pyc_args = sep (pyc_args, ", ") + arg_type;
}
if (is_constructor) {
var text = "\t\t%s = lib.%s\n".printf (cname, cname);
text += "\t\t%s.restype = c_void_p\n".printf (cname);
text += "\t\tself._o = %s (%s)\n".printf (cname, call_args); // TODO: support constructor arguments
#if 0
var classname = parent.get_full_name ().replace (ns_pfx, "").replace (".", "");
text += "\t%s (%s) {\n".printf (classname, def_args);
if (context.is_defined ("GOBJECT"))
text += "\t\tg_type_init ();\n";
text += "\t\treturn %s (%s);\n\t}\n".printf (cname, call_args);
#endif
text += clears;
ctc.cur.append (text);
} else {
if (!is_static) {
if (pyc_args == "")
pyc_args = "c_void_p";
else pyc_args = "c_void_p, " + pyc_args;
}
if (ret == "RList") {
wrappers +="\tdef %s(self%s):\n".printf (m.name, call_args);
m.name = "_"+m.name;
string gen = "";
var type = m.return_type;
foreach (DataType t in type.get_type_arguments ())
gen = sep (gen, ", ") + type_name (t);
ret = "RList<"+gen+">";
wrappers +="\t\treturn rlist2array(self.%s(%s),%s)\n".printf (
m.name, call_args, gen);
}
if (ret == "SdbList") {
wrappers +="\tdef %s(self%s):\n".printf (m.name, call_args);
m.name = "_"+m.name;
string gen = "";
var type = m.return_type;
foreach (DataType t in type.get_type_arguments ())
gen = sep (gen, ", ") + type_name (t);
ret = "SdbList<"+gen+">";
wrappers +="\t\treturn sdblist2array(self.%s(%s),%s)\n".printf (
m.name, call_args, gen);
}
ret = (ret=="void")? "None": "'"+ret+"'";
ctc.cur.append (
"\t%s, %s = register('%s','%s',%s)\n".printf (
get_alias(m.name), cname, cname, pyc_args, ret));
}
}
public override void visit_namespace (Namespace ns) {
string name = ns.get_full_name ();
bool use = use_namespace (ns);
if (use)
ns_pfx = name+ ".";
if (ns_pfx != null)
add_includes (ns);
foreach (var n in ns.get_namespaces ())
n.accept (this);
if (ns_pfx != null) {
foreach (Constant c in ns.get_constants ())
c.accept (this);
foreach (Vala.Enum e in ns.get_enums ())
e.accept (this);
foreach (Struct s in ns.get_structs ())
s.accept (this);
foreach (Class c in ns.get_classes ())
c.accept (this);
}
if (use)
ns_pfx = null;
}
public override void write (string file) {
var stream = FileStream.open (file, "w");
if (stream == null)
error ("Cannot open %s for writing".printf (file));
stream.printf (
"# this file has been automatically generated by valabind\n"+
"import sys\n"+
"from ctypes import *\n"+
"from ctypes.util import find_library\n"+
"if sys.platform.startswith('win'):\n"+
" lib = WinDLL (find_library ('%s'))\n"+
"else:\n"+
" lib = CDLL (find_library ('%s'))\n", library, library);
stream.puts (
"def rlist2array(x,y):\n"+
" it = x.iterator ()\n"+
" ret = []\n"+
" while True:\n"+
" data = it.get_data ()\n"+
" ds = cast (data, POINTER(y)).contents\n"+
" ret.append (ds)\n"+
" if it.n == None:\n"+
" break\n"+
" it = it.get_next ()\n"+
" return ret\n"+
"\n"+
"class AddressHolder(object):\n"+
" def __get__(self, obj, type_):\n"+
" if getattr(obj, '_address', None) is None:\n"+
" obj._address = addressof(obj)\n"+
" return obj._address\n"+
"\n"+
" def __set__(self, obj, value):\n"+
" obj._address = value\n"+
"\n"+
"class WrappedRMethod(object):\n"+
" def __init__(self, cname, args, ret):\n"+
" self.cname = cname\n"+
" self.args = args\n"+
" self.ret = ret\n"+
" self.args_set = False\n"+
" self.method = getattr(lib, cname)\n"+
"\n"+
" def __call__(self, *a):\n"+
" if not self.args_set:\n"+
" if self.args:\n"+
" self.method.argtypes = [eval(x.strip()) for x in self.args.split(',')]\n"+
" self.method.restype = eval(self.ret) if self.ret else None\n"+
" self.args_set = True\n"+
" return self.method(*a)\n"+
"\n"+
"class WrappedApiMethod(object):\n"+
" def __init__(self, method, ret2, last):\n"+
" self.method = method\n"+
" self._o = None\n"+
" self.ret2 = ret2\n"+
" self.last = last\n"+
"\n"+
" def __call__(self, *a):\n"+
" result = self.method(self._o, *a)\n"+
" if self.ret2:\n"+
" result = eval(self.ret2)(result)\n"+
" if self.last:\n"+
" return getattr(result, self.last)\n"+
" return result\n"+
"\n"+
" def __get__(self, obj, type_):\n"+
" self._o = obj._o\n"+
" return self\n"+
"\n"+
"def register(cname, args, ret):\n"+
" ret2 = last = None\n"+
" if ret:\n"+
" if ret[0]>='A' and ret[0]<='Z':\n"+
" x = ret.find('<')\n"+
" if x != -1:\n"+
" ret = ret[0:x]\n"+
" last = 'contents'\n"+
" ret = 'POINTER('+ret+')'\n"+
" else:\n"+
" last = 'value'\n"+
" ret2 = ret\n"+
" \n"+
" method = WrappedRMethod(cname, args, ret)\n"+
" wrapped_method = WrappedApiMethod(method, ret2, last)\n"+
" return wrapped_method, method\n\n");
context.root.accept (this);
stream.printf (ctc.to_string ());
stream.puts (delegatestr);
}
}