2 // valac TreeBuilder.vala --pkg libvala-0.24 --pkg posix -o /tmp/treebuilder
9 public class TestCompiler : Vala.CodeVisitor {
11 Vala.CodeContext context;
12 public TestCompiler(string file) {
18 public void dumpCode(string str) {
19 var ls = str.split("\n");
20 for (var i=0;i < ls.length; i++) {
21 print("%d : %s\n", i+1, ls[i]);
25 public Gee.HashMap<int,string> checkFile()
27 return this.checkString(JsRender.NodeToVala.mungeFile(this.file));
30 public async Gee.HashMap<int,string> checkFileWithNodePropChange(
36 Gee.HashMap<int,string> ret = new Gee.HashMap<int,string> ();
37 var hash = ptype == "listener" ? node.listeners : node.props;
39 // untill we get a smarter renderer..
40 // we have some scenarios where changing the value does not work
41 if (prop == "* xns" || prop == "xtype") {
46 var old = hash.get(prop);
47 var newval = "/*--VALACHECK-START--*/ " + val ;
49 hash.set(prop, newval);
50 var tmpstring = JsRender.NodeToVala.mungeFile(this.file);
52 //print("%s\n", tmpstring);
53 var bits = tmpstring.split("/*--VALACHECK-START--*/");
55 if (bits.length > 0) {
56 offset = bits[0].split("\n").length +1;
58 //this.dumpCode(tmpstring);
59 //print("offset %d\n", offset);
60 yield this.checkStringThread(tmpstring);
64 var iter = this.report.line_errors.map_iterator();
66 // print("%d : %s\n",iter.get_key() - offset, iter.get_value());
67 // we have to prefix the error with the fake line number
68 // so that it's a unique mark..
69 ret.set(iter.get_key() - offset,
70 "%d : %s".printf(iter.get_key() - offset,iter.get_value()));
76 public async Gee.HashMap<int,string> checkStringThread(string contents)
78 SourceFunc callback = checkStringThread.callback;
79 var ret = new Gee.HashMap<int,string>();
80 ThreadFunc<void*> run = () => {
82 // Pass back result and schedule callback
83 ret = this.checkString(contents);
84 Idle.add((owned) callback);
87 Thread.create<void*>(run, false);
89 // Wait for background thread to schedule our callback
96 public Gee.HashMap<int,string> checkString(string contents)
99 var valac = "valac " ;
101 context = new Vala.CodeContext ();
102 Vala.CodeContext.push (context);
104 context.experimental = false;
105 context.experimental_non_null = false;
117 for (int i = 2; i <= ver; i += 2) {
118 context.add_define ("VALA_0_%d".printf (i));
127 var vapidirs = ((Project.Gtk)this.file.project).vapidirs();
128 // what's the current version of vala???
131 vapidirs += Path.get_dirname (context.get_vapi_path("glib-2.0")) ;
133 for(var i =0 ; i < vapidirs.length; i++) {
134 valac += " --vapidir=" + vapidirs[i];
138 // or context.get_vapi_path("glib-2.0"); // should return path..
139 context.vapi_directories = vapidirs;
140 context.report.enable_warnings = true;
141 context.metadata_directories = { };
142 context.gir_directories = {};
143 context.thread = true;
146 this.report = new ValaSourceReport(this.file);
147 context.report = this.report;
150 context.basedir = "/tmp"; //Posix.realpath (".");
152 context.directory = context.basedir;
155 // add default packages:
156 //if (settings.profile == "gobject-2.0" || settings.profile == "gobject" || settings.profile == null) {
157 context.profile = Vala.Profile.GOBJECT;
159 var ns_ref = new Vala.UsingDirective (new Vala.UnresolvedSymbol (null, "GLib", null));
160 context.root.add_using_directive (ns_ref);
162 var source_file = new Vala.SourceFile (
164 Vala.SourceFileType.SOURCE,
165 "~~~~~testfile.vala",
168 source_file.add_using_directive (ns_ref);
169 context.add_source_file (source_file);
171 // add all the files (except the current one) - this.file.path
172 var pr = ((Project.Gtk)this.file.project);
173 if (this.file.build_module.length > 0) {
174 var cg = pr.compilegroups.get(this.file.build_module);
175 for (var i = 0; i < cg.sources.size; i++) {
176 var path = pr.resolve_path(
177 pr.resolve_path_combine_path(pr.firstPath(),cg.sources.get(i)));
179 if (!FileUtils.test(path, FileTest.EXISTS)) {
183 if (path == this.file.path.replace(".bjs", ".vala")) {
187 if (FileUtils.test(path, FileTest.IS_DIR)) {
190 //print("Add source file %s\n", path);
194 if (Regex.match_simple("\\.c$", path)) {
195 context.add_c_source_file(path);
200 var xsf = new Vala.SourceFile (
202 Vala.SourceFileType.SOURCE,
205 xsf.add_using_directive (ns_ref);
206 context.add_source_file(xsf);
210 // default.. packages..
211 context.add_external_package ("glib-2.0");
212 context.add_external_package ("gobject-2.0");
213 // user defined ones..
215 var dcg = pr.compilegroups.get("_default_");
216 for (var i = 0; i < dcg.packages.size; i++) {
217 valac += " --pkg " + dcg.packages.get(i);
218 context.add_external_package (dcg.packages.get(i));
221 //Vala.Config.PACKAGE_SUFFIX.substring (1)
223 // add the modules...
227 //context.add_external_package ("libvala-0.24");
229 this.report.compile_notice("START", "", 0, "");
232 //add_documented_files (context, settings.source_files);
234 Vala.Parser parser = new Vala.Parser ();
235 parser.parse (context);
236 //gir_parser.parse (context);
237 if (context.report.get_errors () > 0) {
238 print("parse got errors");
239 ((ValaSourceReport)context.report).dump();
241 Vala.CodeContext.pop ();
242 this.report.compile_notice("END", "", 0, "");
243 return this.report.line_errors;
250 if (context.report.get_errors () > 0) {
251 print("check got errors");
252 ((ValaSourceReport)context.report).dump();
253 Vala.CodeContext.pop ();
254 this.report.compile_notice("END", "", 0, "");
255 return this.report.line_errors;
259 //context.codegen = new Vala.GDBusServerModule ();
262 context.output = "/tmp/testbuild";
263 valac += " -o " +context.output;
264 //context.codegen.emit (context);
266 var ccompiler = new Vala.CCodeCompiler ();
267 var cc_command = Environment.get_variable ("CC");
268 var pkg_config_command = Environment.get_variable ("PKG_CONFIG");
270 ccompiler.compile (context, cc_command, new string[] { }, pkg_config_command);
272 ccompiler.compile (context, cc_command, new string[] { });
276 Vala.CodeContext.pop ();
277 //(new Vala.CodeNode()).get_error_types().clear();
278 //(new Vala.NullType()).get_type_arguments().clear();
279 (new Vala.NullType(null)).get_type_arguments().clear();
281 this.report.compile_notice("END", "", 0, "");
282 print("%s\n", valac);
284 return this.report.line_errors;
293 int main (string[] args) {
295 var a = new ValaSource(file);
296 a.create_valac_tree();