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);
72 foreach(var c in element.get_structs()) {
73 this.add_struct(g, c);
76 element.accept_children(this); // catch sub namespaces..
82 public void add_enum(GirObject parent, Vala.Enum cls)
85 var c = new GirObject("Enum", cls.name);
86 parent.consts.set(cls.name, c);
91 foreach(var e in cls.get_values()) {
92 var em = new GirObject("EnumMember",e.name);
97 em.type = e.type_reference == null || e.type_reference.type_symbol == null ? "" : e.type_reference.type_symbol.get_full_name();
99 em.type = e.type_reference == null || e.type_reference.data_type == null ? "" : e.type_reference.data_type.get_full_name();
103 // unlikely to get value..
104 //c.value = element->get_prop("value");
105 c.consts.set(e.name,em);
111 public void add_interface(GirObject parent, Vala.Interface cls)
114 var c = new GirObject("Interface", parent.name + "." + cls.name);
115 parent.classes.set(cls.name, c);
117 //c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
120 foreach(var p in cls.get_properties()) {
121 this.add_property(c, p);
124 foreach(var p in cls.get_signals()) {
125 this.add_signal(c, p);
128 foreach(var p in cls.get_methods()) {
129 // skip static methods..
130 if (p.binding != Vala.MemberBinding.INSTANCE &&
131 !(p is Vala.CreationMethod)
136 this.add_method(c, p);
139 //if (cls.base_class != null) {
140 // c.inherits.add(cls.base_class.get_full_name());
142 //foreach(var p in cls.get_base_types()) {
143 // if (p.data_type != null) {
144 // c.implements.add(p.data_type.get_full_name());
152 //https://learnxinyminutes.com/docs/vala/ -- see for ctor on structs.
154 public void add_struct(GirObject parent, Vala.Struct cls)
157 var c = new GirObject("Struct", parent.name + "." + cls.name);
158 parent.classes.set(cls.name, c);
160 foreach(var p in cls.get_fields()) {
161 this.add_field(c, p);
167 if (cls.version.deprecated) {
168 GLib.debug("class %s is deprecated", c.name);
169 c.is_deprecated = true;
176 public void add_class(GirObject parent, Vala.Class cls)
179 var c = new GirObject("Class", parent.name + "." + cls.name);
180 parent.classes.set(cls.name, c);
182 c.parent = cls.base_class == null ? "" : cls.base_class.get_full_name() ; // extends...
184 c.is_abstract = cls.is_abstract;
185 foreach(var p in cls.get_properties()) {
186 this.add_property(c, p);
189 foreach(var p in cls.get_signals()) {
190 this.add_signal(c, p);
193 foreach(var p in cls.get_methods()) {
194 // skip static methods..
195 if (p.binding != Vala.MemberBinding.INSTANCE &&
196 !(p is Vala.CreationMethod)
201 this.add_method(c, p);
204 if (cls.base_class != null) {
205 c.inherits.add(cls.base_class.get_full_name());
207 foreach(var p in cls.get_base_types()) {
209 if (p.type_symbol != null) {
210 c.implements.add(p.type_symbol.get_full_name());
213 if (p.data_type != null) {
214 c.implements.add(p.data_type.get_full_name());
221 if (cls.version.deprecated) {
222 GLib.debug("class %s is deprecated", c.name);
223 c.is_deprecated = true;
228 public GirObject? fqn_to_cls(string fqn)
230 var ar = fqn.split(".");
231 var pkg = this.project.gir_cache.get(ar[0]);
232 var cls = pkg != null ? pkg.classes.get(ar[1]) : null;
236 public void augment_inherits_for(GirObject cls, Gee.ArrayList<string> to_check, bool is_top)
238 foreach (var chk_cls in to_check) {
239 if (!cls.inherits.contains(chk_cls)) {
240 cls.inherits.add(chk_cls);
249 var subcls = this.fqn_to_cls(chk_cls);
250 if (subcls == null) {
253 this.augment_inherits_for(cls, subcls.inherits, false);
254 this.augment_implements_for(cls, subcls.implements);
258 public void augment_implements_for(GirObject cls, Gee.ArrayList<string> to_check)
260 foreach (var chk_cls in to_check) {
261 if (cls.implements.contains(chk_cls)) {
264 cls.implements.add(chk_cls);
266 var subcls = this.fqn_to_cls(chk_cls);
267 if (subcls == null) {
270 this.augment_implements_for(cls, subcls.implements);
276 // this might miss out interfaces of child classes?
277 public void augment_all_inheritence()
279 // this works out all the children...
280 foreach(var pkgname in this.project.gir_cache.keys) {
282 var pkg = this.project.gir_cache.get(pkgname);
283 foreach (var clsname in pkg.classes.keys) {
284 var cls = pkg.classes.get(clsname);
285 this.augment_inherits_for(cls, cls.inherits, true);
286 this.augment_implements_for(cls, cls.implements);
289 // now do the implementations
290 foreach(var pkgname in this.project.gir_cache.keys) {
292 var pkg = this.project.gir_cache.get(pkgname);
293 foreach (var clsname in pkg.classes.keys) {
294 var cls = pkg.classes.get(clsname);
295 foreach(var parentname in cls.inherits) {
296 var parent = this.fqn_to_cls(parentname);
297 if (parent == null) {
300 if (parent.implementations.contains(cls.fqn())) {
303 parent.implementations.add(cls.fqn());
306 foreach(var parentname in cls.implements) {
307 var parent = this.fqn_to_cls(parentname);
308 if (parent == null) {
311 if (parent.implementations.contains(cls.fqn())) {
314 parent.implementations.add(cls.fqn());
325 public void add_property(GirObject parent, Vala.Property prop)
327 var c = new GirObject("Prop",prop.name);
330 c.propertyof = parent.name;
332 c.type = prop.property_type.type_symbol == null ? "" : prop.property_type.type_symbol.get_full_name();
334 c.type = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
336 c.is_readable = prop.get_accessor != null ? prop.get_accessor.readable : false;
337 c.is_writable = prop.get_accessor != null ? prop.get_accessor.writable : false;
338 if (prop.version.deprecated) {
339 GLib.debug("class %s is deprecated", c.name);
340 c.is_deprecated = true;
342 parent.props.set(prop.name,c);
348 public void add_field(GirObject parent, Vala.Field prop)
350 var c = new GirObject("Field",prop.name);
353 c.propertyof = parent.name;
355 c.type = prop.variable_type.type_symbol == null ? "" : prop.variable_type.type_symbol.get_full_name();
357 c.type = prop.variable_type.data_type == null ? "" : prop.variable_type.data_type.get_full_name();
360 if (prop.version.deprecated) {
361 GLib.debug("class %s is deprecated", c.name);
362 c.is_deprecated = true;
364 parent.props.set(prop.name,c);
369 public void add_signal(GirObject parent, Vala.Signal sig)
371 var c = new GirObject("Signal",sig.name);
376 var dt = sig.return_type.type_symbol ;
378 var dt = sig.return_type.data_type;
385 //print("creating return type on signal %s\n", sig.name);
386 var cc = new GirObject("Return", "return-value");
389 cc.type = dt.get_full_name();
392 retval = "\treturn " + cc.type +";";
394 parent.signals.set(sig.name,c);
396 var params = sig.get_parameters() ;
397 if (params.size < 1) {
399 c.sig = "( ) => {\n\n"+ retval + "\n}\n";
403 var cc = new GirObject("Paramset",sig.name); // what's the name on this?
409 foreach(var p in params) {
410 this.add_param(cc, p);
411 args += args.length > 0 ? ", " : "";
414 // add c.sig -> this is the empty
415 c.sig = "(" + args + ") => {\n\n"+ retval + "\n}\n";
421 public void add_method(GirObject parent, Vala.Method met)
423 var n = met.name == null ? "" : met.name;
425 if (met is Vala.CreationMethod) {
427 if(n == "" || n == ".new") {
432 //print("add_method : %s\n", n);
434 var c = new GirObject(ty,n);
438 if (met.return_type.type_symbol != null) {
440 if (met.return_type.data_type != null) {
444 //print("creating return type on method %s\n", met.name);
445 var cc = new GirObject("Return", "return-value");
450 cc.type = met.return_type.type_symbol.get_full_name();
452 cc.type = met.return_type.data_type.get_full_name();
459 if (met is Vala.CreationMethod) {
460 parent.ctors.set(c.name,c);
462 parent.methods.set(met.name,c);
465 var params = met.get_parameters() ;
466 if (params.size < 1) {
469 var cc = new GirObject("Paramset",met.name); // what's the name on this?
475 foreach(var p in params) {
476 if (p.name == null && !p.ellipsis) {
479 var pp = this.add_param(cc, p);
480 c.sig += (c.sig == "(" ? "" : ",");
481 c.sig += " " + (pp.direction == "in" ? "" : pp.direction) + " " + pp.type + " " + pp.name;
483 c.sig += (c.sig == "(" ? ")" : " )");
487 public GirObject add_param(GirObject parent, Vala.Parameter pam)
494 var c = new GirObject("Param",n);
498 switch (pam.direction) {
499 case Vala.ParameterDirection.IN:
502 case Vala.ParameterDirection.OUT:
505 case Vala.ParameterDirection.REF:
510 parent.params.add(c);
514 c.type = pam.variable_type.type_symbol == null ? "" : pam.variable_type.type_symbol.get_full_name();
516 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
519 Gir.checkParamOverride(c);
524 public void create_valac_tree( )
527 context = new Vala.CodeContext ();
528 Vala.CodeContext.push (context);
530 context.experimental = false;
531 context.experimental_non_null = false;
538 for (int i = 2; i <= ver; i += 2) {
539 context.add_define ("VALA_0_%d".printf (i));
543 //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
544 // what's the current version of vala???
547 //vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
549 var vapidirs = context.vapi_directories;
551 vapidirs += (BuilderApplication.configDirectory() + "/resources/vapi");
552 vapidirs += "/usr/share/vala-0.%d/vapi".printf(ver);
553 vapidirs += "/usr/share/vala/vapi";
554 context.vapi_directories = vapidirs;
556 // or context.get_vapi_path("glib-2.0"); // should return path..
557 //context.vapi_directories = vapidirs;
558 context.report.enable_warnings = true;
559 context.metadata_directories = { };
560 context.gir_directories = {};
561 //context.thread = true;
564 //this.report = new ValaSourceReport(this.file);
565 //context.report = this.report;
568 context.basedir = "/tmp"; //Posix.realpath (".");
570 context.directory = context.basedir;
573 // add default packages:
574 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
576 context.set_target_profile (Vala.Profile.GOBJECT);
578 context.profile = Vala.Profile.GOBJECT;
581 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
582 context.root.add_using_directive (ns_ref);
585 context.add_external_package ("glib-2.0");
586 context.add_external_package ("gobject-2.0");
587 // user defined ones..
589 var dcg = this.project.compilegroups.get("_default_");
590 for (var i = 0; i < dcg.packages.size; i++) {
592 var pkg = dcg.packages.get(i);
593 // do not add libvala versions except the one that matches the one we are compiled against..
594 if (Regex.match_simple("^libvala", pkg) && pkg != ("libvala-0." + ver.to_string())) {
597 //valac += " --pkg " + dcg.packages.get(i);
598 if (!this.has_vapi(context.vapi_directories, dcg.packages.get(i))) {
602 GLib.debug("ADD vapi '%s'", dcg.packages.get(i));
603 context.add_external_package (dcg.packages.get(i));
609 // core packages we are interested in for the builder..
610 // some of these may fail... - we probalby need a better way to handle this..
612 context.add_external_package ("gtk+-3.0");
613 context.add_external_package ("libsoup-2.4");
614 if (!context.add_external_package ("webkit2gtk-4.0")) {
615 context.add_external_package ("webkit2gtk-3.0");
617 // these are supposed to be in the 'deps' file, but it's not getting read..
618 context.add_external_package ("cogl-1.0");
619 context.add_external_package ("json-glib-1.0");
620 context.add_external_package ("clutter-gtk-1.0");
624 context.add_external_package ("gdl-3.0");
625 context.add_external_package ("gtksourceview-3.0");
626 context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
628 //add_documented_files (context, settings.source_files);
630 Vala.Parser parser = new Vala.Parser ();
631 parser.parse (context);
632 //gir_parser.parse (context);
633 if (context.report.get_errors () > 0) {
635 //throw new VapiParserError.PARSE_FAILED("failed parse VAPIS, so we can not write file correctly");
637 print("parse got errors");
640 Vala.CodeContext.pop ();
648 if (context.report.get_errors () > 0) {
649 GLib.error("failed check VAPIS, so we can not write file correctly");
650 // throw new VapiParserError.PARSE_FAILED("failed check VAPIS, so we can not write file correctly");
651 Vala.CodeContext.pop ();
659 context.accept(this);
662 // dump the tree for Gtk?
664 Vala.CodeContext.pop ();
667 this.augment_all_inheritence();
677 public bool has_vapi(string[] dirs, string vapi)
679 for(var i =0 ; i < dirs.length; i++) {
680 GLib.debug("check VAPI - %s", dirs[i] + "/" + vapi + ".vapi");
681 if (!FileUtils.test( dirs[i] + "/" + vapi + ".vapi", FileTest.EXISTS)) {
692 int main (string[] args) {
694 var g = Palete.Gir.factoryFqn("Gtk.SourceView");
695 print("%s\n", g.asJSONString());