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...
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());
197 public void add_property(GirObject parent, Vala.Property prop)
199 var c = new GirObject("Prop",prop.name);
202 c.propertyof = parent.name;
204 c.type = prop.property_type.type_symbol == null ? "" : prop.property_type.type_symbol.get_full_name();
206 c.type = prop.property_type.data_type == null ? "" : prop.property_type.data_type.get_full_name();
208 parent.props.set(prop.name,c);
212 public void add_signal(GirObject parent, Vala.Signal sig)
214 var c = new GirObject("Signal",sig.name);
219 var dt = sig.return_type.type_symbol ;
221 var dt = sig.return_type.data_type;
226 //print("creating return type on signal %s\n", sig.name);
227 var cc = new GirObject("Return", "return-value");
230 cc.type = dt.get_full_name();
233 parent.signals.set(sig.name,c);
235 var params = sig.get_parameters() ;
236 if (params.size < 1) {
239 var cc = new GirObject("Paramset",sig.name); // what's the name on this?
245 foreach(var p in params) {
246 this.add_param(cc, p);
251 public void add_method(GirObject parent, Vala.Method met)
253 var n = met.name == null ? "" : met.name;
255 if (met is Vala.CreationMethod) {
257 if(n == "" || n == ".new") {
262 //print("add_method : %s\n", n);
264 var c = new GirObject(ty,n);
268 if (met.return_type.type_symbol != null) {
270 if (met.return_type.data_type != null) {
274 //print("creating return type on method %s\n", met.name);
275 var cc = new GirObject("Return", "return-value");
280 cc.type = met.return_type.type_symbol.get_full_name();
282 cc.type = met.return_type.data_type.get_full_name();
289 if (met is Vala.CreationMethod) {
290 parent.ctors.set(c.name,c);
292 parent.methods.set(met.name,c);
295 var params = met.get_parameters() ;
296 if (params.size < 1) {
299 var cc = new GirObject("Paramset",met.name); // what's the name on this?
305 foreach(var p in params) {
306 if (p.name == null && !p.ellipsis) {
309 var pp = this.add_param(cc, p);
310 c.sig += (c.sig == "(" ? "" : ",");
311 c.sig += " " + (pp.direction == "in" ? "" : pp.direction) + " " + pp.type + " " + pp.name;
313 c.sig += (c.sig == "(" ? ")" : " )");
317 public GirObject add_param(GirObject parent, Vala.Parameter pam)
324 var c = new GirObject("Param",n);
328 switch (pam.direction) {
329 case Vala.ParameterDirection.IN:
332 case Vala.ParameterDirection.OUT:
335 case Vala.ParameterDirection.REF:
340 parent.params.add(c);
344 c.type = pam.variable_type.type_symbol == null ? "" : pam.variable_type.type_symbol.get_full_name();
346 c.type = pam.variable_type.data_type == null ? "" : pam.variable_type.data_type.get_full_name();
349 Gir.checkParamOverride(c);
354 public void create_valac_tree( )
357 context = new Vala.CodeContext ();
358 Vala.CodeContext.push (context);
360 context.experimental = false;
361 context.experimental_non_null = false;
368 for (int i = 2; i <= ver; i += 2) {
369 context.add_define ("VALA_0_%d".printf (i));
373 //var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
374 // what's the current version of vala???
377 //vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
379 var vapidirs = context.vapi_directories;
381 vapidirs += (BuilderApplication.configDirectory() + "/resources/vapi");
382 vapidirs += "/usr/share/vala-0.%d/vapi".printf(ver);
383 context.vapi_directories = vapidirs;
385 // or context.get_vapi_path("glib-2.0"); // should return path..
386 //context.vapi_directories = vapidirs;
387 context.report.enable_warnings = true;
388 context.metadata_directories = { };
389 context.gir_directories = {};
390 //context.thread = true;
393 //this.report = new ValaSourceReport(this.file);
394 //context.report = this.report;
397 context.basedir = "/tmp"; //Posix.realpath (".");
399 context.directory = context.basedir;
402 // add default packages:
403 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
405 context.set_target_profile (Vala.Profile.GOBJECT);
407 context.profile = Vala.Profile.GOBJECT;
410 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
411 context.root.add_using_directive (ns_ref);
414 context.add_external_package ("glib-2.0");
415 context.add_external_package ("gobject-2.0");
416 // user defined ones..
418 var dcg = this.project.compilegroups.get("_default_");
419 for (var i = 0; i < dcg.packages.size; i++) {
421 var pkg = dcg.packages.get(i);
422 // do not add libvala versions except the one that matches the one we are compiled against..
423 if (Regex.match_simple("^libvala", pkg) && pkg != ("libvala-0." + ver.to_string())) {
426 //valac += " --pkg " + dcg.packages.get(i);
427 if (!this.has_vapi(context.vapi_directories, dcg.packages.get(i))) {
431 GLib.debug("ADD vapi '%s'", dcg.packages.get(i));
432 context.add_external_package (dcg.packages.get(i));
438 // core packages we are interested in for the builder..
439 // some of these may fail... - we probalby need a better way to handle this..
441 context.add_external_package ("gtk+-3.0");
442 context.add_external_package ("libsoup-2.4");
443 if (!context.add_external_package ("webkit2gtk-4.0")) {
444 context.add_external_package ("webkit2gtk-3.0");
446 // these are supposed to be in the 'deps' file, but it's not getting read..
447 context.add_external_package ("cogl-1.0");
448 context.add_external_package ("json-glib-1.0");
449 context.add_external_package ("clutter-gtk-1.0");
453 context.add_external_package ("gdl-3.0");
454 context.add_external_package ("gtksourceview-3.0");
455 context.add_external_package ("vte-2.90"); //??? -- hopefullly that works..
457 //add_documented_files (context, settings.source_files);
459 Vala.Parser parser = new Vala.Parser ();
460 parser.parse (context);
461 //gir_parser.parse (context);
462 if (context.report.get_errors () > 0) {
464 //throw new VapiParserError.PARSE_FAILED("failed parse VAPIS, so we can not write file correctly");
466 print("parse got errors");
469 Vala.CodeContext.pop ();
477 if (context.report.get_errors () > 0) {
478 GLib.error("failed check VAPIS, so we can not write file correctly");
479 // throw new VapiParserError.PARSE_FAILED("failed check VAPIS, so we can not write file correctly");
480 Vala.CodeContext.pop ();
488 context.accept(this);
491 // dump the tree for Gtk?
493 Vala.CodeContext.pop ();
501 public bool has_vapi(string[] dirs, string vapi)
503 for(var i =0 ; i < dirs.length; i++) {
504 GLib.debug("check VAPI - %s", dirs[i] + "/" + vapi + ".vapi");
505 if (!FileUtils.test( dirs[i] + "/" + vapi + ".vapi", FileTest.EXISTS)) {
516 int main (string[] args) {
518 var g = Palete.Gir.factoryFqn("Gtk.SourceView");
519 print("%s\n", g.asJSONString());