3 // valac -g --pkg libvala-0.26 --pkg gee-1.0 --pkg json-glib-1.0 --pkg gtk+-3.0 VapiParser.vala Gir.vala GirObject.vala -o /tmp/vdoc
7 public errordomain VapiParserError {
12 public class VapiParser : Vala.CodeVisitor {
14 Vala.CodeContext context;
18 public VapiParser(Project.Gtk project) {
20 this.project = project;
21 // should not really happen..
22 if (project.gir_cache == null) {
23 project.gir_cache = new Gee.HashMap<string,Gir>();
28 public override void visit_namespace (Vala.Namespace element)
30 if (element == null) {
36 //print("parsing namespace %s\n", element.name);
37 if (element.name == null) {
38 element.accept_children(this); // catch sub namespaces..
41 this.add_namespace(null, element);
43 public void add_namespace(GirObject? parent, Vala.Namespace element)
47 var g = new GirObject("Package",element.name) ;
49 this.project.gir_cache.set(element.name, (Gir)g);
51 // we add it as a class of the package.. even though its a namespace..
52 parent.classes.set(element.name, g);
56 foreach(var c in element.get_classes()) {
59 foreach(var c in element.get_enums()) {
62 foreach(var c in element.get_interfaces()) {
63 this.add_interface(g, c);
65 foreach(var c in element.get_namespaces()) {
66 this.add_namespace(g, c);
68 foreach(var c in element.get_methods()) {
69 this.add_method(g, c);
71 element.accept_children(this); // catch sub namespaces..
77 public void add_enum(GirObject parent, Vala.Enum cls)
80 var c = new GirObject("Enum", cls.name);
81 parent.consts.set(cls.name, c);
86 foreach(var e in cls.get_values()) {
87 var em = new GirObject("EnumMember",e.name);
92 em.type = e.type_reference == null || e.type_reference.type_symbol == null ? "" : e.type_reference.type_symbol.get_full_name();
94 em.type = e.type_reference == null || e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
98 // unlikely to get value..
99 //c.value = element->get_prop("value");
100 c.consts.set(e.name,em);
106 public void add_interface(GirObject parent, Vala.Interface cls)
109 var c = new GirObject("Interface", parent.name + "." + cls.name);
110 parent.classes.set(cls.name, c);
112 //c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
115 foreach(var p in cls.get_properties()) {
116 this.add_property(c, p);
119 foreach(var p in cls.get_signals()) {
120 this.add_signal(c, p);
123 foreach(var p in cls.get_methods()) {
124 // skip static methods..
125 if (p.binding != Vala.MemberBinding.INSTANCE &&
126 !(p is Vala.CreationMethod)
131 this.add_method(c, p);
134 //if (cls.base_class != null) {
135 // c.inherits.add(cls.base_class.get_full_name());
137 //foreach(var p in cls.get_base_types()) {
138 // if (p.data_type != null) {
139 // c.implements.add(p.data_type.get_full_name());
148 public void add_class(GirObject parent, Vala.Class cls)
151 var c = new GirObject("Class", parent.name + "." + cls.name);
152 parent.classes.set(cls.name, c);
154 c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
156 c.is_abstract = cls.is_abstract;
157 foreach(var p in cls.get_properties()) {
158 this.add_property(c, p);
161 foreach(var p in cls.get_signals()) {
162 this.add_signal(c, p);
165 foreach(var p in cls.get_methods()) {
166 // skip static methods..
167 if (p.binding != Vala.MemberBinding.INSTANCE &&
168 !(p is Vala.CreationMethod)
173 this.add_method(c, p);
176 if (cls.base_class != null) {
177 c.inherits.add(cls.base_class.get_full_name());
179 foreach(var p in cls.get_base_types()) {
181 if (p.type_symbol != null) {
182 c.implements.add(p.type_symbol.get_full_name());
185 if (p.data_type != null) {
186 c.implements.add(p.data_type.get_full_name());
193 if (cls.version.deprecated) {
194 GLib.debug("class %s is deprecated", c.name);
195 c.is_deprecated = true;
200 public GirObject? fqn_to_cls(string fqn)
202 var ar = fqn.split(".");
203 var pkg = this.project.gir_cache.get(ar[0]);
204 var cls = pkg != null ? pkg.classes.get(ar[1]) : null;
208 public void augment_inherits_for(GirObject cls, Gee.ArrayList<string> to_check, bool is_top)
210 foreach (var chk_cls in to_check) {
211 if (!cls.inherits.contains(chk_cls)) {
212 cls.inherits.add(chk_cls);
221 var subcls = this.fqn_to_cls(chk_cls);
222 if (subcls == null) {
225 this.augment_inherits_for(cls, subcls.inherits, false);
226 this.augment_implements_for(cls, subcls.implements);
230 public void augment_implements_for(GirObject cls, Gee.ArrayList<string> to_check)
232 foreach (var chk_cls in to_check) {
233 if (cls.implements.contains(chk_cls)) {
236 cls.implements.add(chk_cls);
238 var subcls = this.fqn_to_cls(chk_cls);
239 if (subcls == null) {
242 this.augment_implements_for(cls, subcls.implements);
248 // this might miss out interfaces of child classes?
249 public void augment_all_inheritence()
251 // this works out all the children...
252 foreach(var pkgname in this.project.gir_cache.keys) {
254 var pkg = this.project.gir_cache.get(pkgname);
255 foreach (var clsname in pkg.classes.keys) {
256 var cls = pkg.classes.get(clsname);
257 this.augment_inherits_for(cls, cls.inherits, true);
258 this.augment_implements_for(cls, cls.implements);
261 // now do the implementations
262 foreach(var pkgname in this.project.gir_cache.keys) {
264 var pkg = this.project.gir_cache.get(pkgname);
265 foreach (var clsname in pkg.classes.keys) {
266 var cls = pkg.classes.get(clsname);
267 foreach(var parentname in cls.inherits) {
268 var parent = this.fqn_to_cls(parentname);
269 if (parent == null) {
272 if (parent.implementations.contains(cls.fqn())) {
275 parent.implementations.add(cls.fqn());
278 foreach(var parentname in cls.implements) {
279 var parent = this.fqn_to_cls(parentname);
280 if (parent == null) {
283 if (parent.implementations.contains(cls.fqn())) {
286 parent.implementations.add(cls.fqn());
297 public void add_property(GirObject parent, Vala.Property prop)
299 var c = new GirObject("Prop",prop.name);
302 c.propertyof = parent.name;
304 c.type = prop.property_type.type_symbol == null ? "" : prop.property_type.type_symbol.get_full_name();
306 c.type = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
308 c.is_readable = prop.get_accessor != null ? prop.get_accessor.readable : false;
309 c.is_writable = prop.get_accessor != null ? prop.get_accessor.writable : false;
310 if (prop.version.deprecated) {
311 GLib.debug("class %s is deprecated", c.name);
312 c.is_deprecated = true;
314 parent.props.set(prop.name,c);
318 public void add_signal(GirObject parent, Vala.Signal sig)
320 var c = new GirObject("Signal",sig.name);
325 var dt = sig.return_type.type_symbol ;
327 var dt = sig.return_type.data_type;
334 //print("creating return type on signal %s\n", sig.name);
335 var cc = new GirObject("Return", "return-value");
338 cc.type = dt.get_full_name();
341 retval = "\treturn " + cc.type +";";
343 parent.signals.set(sig.name,c);
345 var params = sig.get_parameters() ;
346 if (params.size < 1) {
348 c.sig = "( ) => {\n\n"+ retval + "\n}\n";
352 var cc = new GirObject("Paramset",sig.name); // what's the name on this?
358 foreach(var p in params) {
359 this.add_param(cc, p);
360 args += args.length > 0 ? ", " : "";
363 // add c.sig -> this is the empty
364 c.sig = "(" + args + ") => {\n\n"+ retval + "\n}\n";
370 public void add_method(GirObject parent, Vala.Method met)
372 var n = met.name == null ? "" : met.name;
374 if (met is Vala.CreationMethod) {
376 if(n == "" || n == ".new") {
381 //print("add_method : %s\n", n);
383 var c = new GirObject(ty,n);
387 if (met.return_type.type_symbol != null) {
389 if (met.return_type.data_type != null) {
393 //print("creating return type on method %s\n", met.name);
394 var cc = new GirObject("Return", "return-value");
399 cc.type = met.return_type.type_symbol.get_full_name();
401 cc.type = met.return_type.data_type.get_full_name();
408 if (met is Vala.CreationMethod) {
409 parent.ctors.set(c.name,c);
411 parent.methods.set(met.name,c);
414 var params = met.get_parameters() ;
415 if (params.size < 1) {
418 var cc = new GirObject("Paramset",met.name); // what's the name on this?
424 foreach(var p in params) {
425 if (p.name == null && !p.ellipsis) {
428 var pp = this.add_param(cc, p);
429 c.sig += (c.sig == "(" ? "" : ",");
430 c.sig += " " + (pp.direction == "in" ? "" : pp.direction) + " " + pp.type + " " + pp.name;
432 c.sig += (c.sig == "(" ? ")" : " )");
436 public GirObject add_param(GirObject parent, Vala.Parameter pam)
443 var c = new GirObject("Param",n);
447 switch (pam.direction) {
448 case Vala.ParameterDirection.IN:
451 case Vala.ParameterDirection.OUT:
454 case Vala.ParameterDirection.REF:
459 parent.params.add(c);
463 c.type = pam.variable_type.type_symbol == null ? "" : pam.variable_type.type_symbol.get_full_name();
465 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
468 Gir.checkParamOverride(c);
473 public void create_valac_tree( )
476 context = new Vala.CodeContext ();
477 Vala.CodeContext.push (context);
479 context.experimental = false;
480 context.experimental_non_null = false;
487 for (int i = 2; i <= ver; i += 2) {
488 context.add_define ("VALA_0_%d".printf (i));
492 //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
493 // what's the current version of vala???
496 //vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
498 var vapidirs = context.vapi_directories;
500 vapidirs += (BuilderApplication.configDirectory() + "/resources/vapi");
501 vapidirs += "/usr/share/vala-0.%d/vapi".printf(ver);
502 vapidirs += "/usr/share/vala/vapi";
503 context.vapi_directories = vapidirs;
505 // or context.get_vapi_path("glib-2.0"); // should return path..
506 //context.vapi_directories = vapidirs;
507 context.report.enable_warnings = true;
508 context.metadata_directories = { };
509 context.gir_directories = {};
510 //context.thread = true;
513 //this.report = new ValaSourceReport(this.file);
514 //context.report = this.report;
517 context.basedir = "/tmp"; //Posix.realpath (".");
519 context.directory = context.basedir;
522 // add default packages:
523 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
525 context.set_target_profile (Vala.Profile.GOBJECT);
527 context.profile = Vala.Profile.GOBJECT;
530 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
531 context.root.add_using_directive (ns_ref);
534 context.add_external_package ("glib-2.0");
535 context.add_external_package ("gobject-2.0");
536 // user defined ones..
538 var dcg = this.project.compilegroups.get("_default_");
539 for (var i = 0; i < dcg.packages.size; i++) {
541 var pkg = dcg.packages.get(i);
542 // do not add libvala versions except the one that matches the one we are compiled against..
543 if (Regex.match_simple("^libvala", pkg) && pkg != ("libvala-0." + ver.to_string())) {
546 //valac += " --pkg " + dcg.packages.get(i);
547 if (!this.has_vapi(context.vapi_directories, dcg.packages.get(i))) {
551 GLib.debug("ADD vapi '%s'", dcg.packages.get(i));
552 context.add_external_package (dcg.packages.get(i));
558 // core packages we are interested in for the builder..
559 // some of these may fail... - we probalby need a better way to handle this..
561 context.add_external_package ("gtk+-3.0");
562 context.add_external_package ("libsoup-2.4");
563 if (!context.add_external_package ("webkit2gtk-4.0")) {
564 context.add_external_package ("webkit2gtk-3.0");
566 // these are supposed to be in the 'deps' file, but it's not getting read..
567 context.add_external_package ("cogl-1.0");
568 context.add_external_package ("json-glib-1.0");
569 context.add_external_package ("clutter-gtk-1.0");
573 context.add_external_package ("gdl-3.0");
574 context.add_external_package ("gtksourceview-3.0");
575 context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
577 //add_documented_files (context, settings.source_files);
579 Vala.Parser parser = new Vala.Parser ();
580 parser.parse (context);
581 //gir_parser.parse (context);
582 if (context.report.get_errors () > 0) {
584 //throw new VapiParserError.PARSE_FAILED("failed parse VAPIS, so we can not write file correctly");
586 print("parse got errors");
589 Vala.CodeContext.pop ();
597 if (context.report.get_errors () > 0) {
598 GLib.error("failed check VAPIS, so we can not write file correctly");
599 // throw new VapiParserError.PARSE_FAILED("failed check VAPIS, so we can not write file correctly");
600 Vala.CodeContext.pop ();
608 context.accept(this);
611 // dump the tree for Gtk?
613 Vala.CodeContext.pop ();
616 this.augment_all_inheritence();
626 public bool has_vapi(string[] dirs, string vapi)
628 for(var i =0 ; i < dirs.length; i++) {
629 GLib.debug("check VAPI - %s", dirs[i] + "/" + vapi + ".vapi");
630 if (!FileUtils.test( dirs[i] + "/" + vapi + ".vapi", FileTest.EXISTS)) {
641 int main (string[] args) {
643 var g = Palete.Gir.factoryFqn("Gtk.SourceView");
644 print("%s\n", g.asJSONString());