]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Add --dump-tree command-line option to write code tree to file
authorJürg Billeter <j@bitron.ch>
Sun, 30 Nov 2008 13:45:09 +0000 (13:45 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Sun, 30 Nov 2008 13:45:09 +0000 (13:45 +0000)
2008-11-30  Jürg Billeter  <j@bitron.ch>

* vala/Makefile.am:
* vala/valacodewriter.vala:
* compiler/valacompiler.vala:
* vapigen/valavapigen.vala:

Add --dump-tree command-line option to write code tree to file

svn path=/trunk/; revision=2099

ChangeLog
compiler/valacompiler.vala
vala/Makefile.am
vala/valacodewriter.vala [moved from vala/valainterfacewriter.vala with 69% similarity]
vapigen/valavapigen.vala

index 74e90ba93fedef2b7b30cd82b81e3dde3915a080..b53bce32a6cab0cf00216c16b94aecd9fae8bbf2 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,12 @@
+2008-11-30  Jürg Billeter  <j@bitron.ch>
+
+       * vala/Makefile.am:
+       * vala/valacodewriter.vala:
+       * compiler/valacompiler.vala:
+       * vapigen/valavapigen.vala:
+
+       Add --dump-tree command-line option to write code tree to file
+
 2008-11-30  Jürg Billeter  <j@bitron.ch>
 
        * vala/valanullchecker.vala:
index 4983deddbe7a5d03c3e382a2d6326eeaa6280477..620da4c8201c9de2e4e9dbe830bfdb27866a0652 100644 (file)
@@ -49,6 +49,7 @@ class Vala.Compiler {
        static string cc_command;
        [NoArrayLength]
        static string[] cc_options;
+       static string dump_tree;
        static bool save_temps;
        static bool quiet_mode;
 
@@ -73,6 +74,7 @@ class Vala.Compiler {
                { "disable-dbus-transformation", 0, 0, OptionArg.NONE, ref disable_dbus_transformation, "Disable transformation of D-Bus member names", null },
                { "cc", 0, 0, OptionArg.STRING, ref cc_command, "Use COMMAND as C compiler command", "COMMAND" },
                { "Xcc", 'X', 0, OptionArg.STRING_ARRAY, ref cc_options, "Pass OPTION to the C compiler", "OPTION..." },
+               { "dump-tree", 0, 0, OptionArg.FILENAME, ref dump_tree, "Write code tree to FILE", "FILE" },
                { "save-temps", 0, 0, OptionArg.NONE, ref save_temps, "Keep temporary files", null },
                { "quiet", 'q', 0, OptionArg.NONE, ref quiet_mode, "Do not print messages to the console", null },
                { "target-glib", 0, 0, OptionArg.STRING, ref target_glib, "Target version of glib for code generation", "MAJOR.MINOR" },
@@ -251,7 +253,12 @@ class Vala.Compiler {
 
                var analyzer = new SemanticAnalyzer ();
                analyzer.analyze (context);
-               
+
+               if (dump_tree != null) {
+                       var code_writer = new CodeWriter (true);
+                       code_writer.write_file (context, dump_tree);
+               }
+
                if (Report.get_errors () > 0) {
                        return quit ();
                }
@@ -279,7 +286,7 @@ class Vala.Compiler {
                }
                
                if (library != null) {
-                       var interface_writer = new InterfaceWriter ();
+                       var interface_writer = new CodeWriter ();
                        string vapi_filename = "%s.vapi".printf (library);
 
                        // put .vapi file in current directory unless -d has been explicitly specified
index 029b7d831c62c51eae5558c6eefc0f83c8284b40..736379650fb8cf114474b27b2421492ab582ec0c 100644 (file)
@@ -37,6 +37,7 @@ libvalacore_la_VALASOURCES = \
        valacodegenerator.vala \
        valacodenode.vala \
        valacodevisitor.vala \
+       valacodewriter.vala \
        valaconditionalexpression.vala \
        valaconstant.vala \
        valaconstructor.vala \
@@ -78,7 +79,6 @@ libvalacore_la_VALASOURCES = \
        valaintegertype.vala \
        valainterface.vala \
        valainterfacetype.vala \
-       valainterfacewriter.vala \
        valainvalidtype.vala \
        valalambdaexpression.vala \
        valaliteral.vala \
similarity index 69%
rename from vala/valainterfacewriter.vala
rename to vala/valacodewriter.vala
index 6d38c1b20e0f67973cf2bf2c45b173bb621271fc..ee6bd839201540e342190d6fe3225da93c8b8ab5 100644 (file)
@@ -1,4 +1,4 @@
-/* valainterfacewriter.vala
+/* valacodewriter.vala
  *
  * Copyright (C) 2006-2008  Jürg Billeter, Raffaele Sandrini
  *
  *     Raffaele Sandrini <raffaele@sandrini.ch>
  */
 
-using GLib;
 using Gee;
 
 /**
  * Code visitor generating Vala API file for the public interface.
  */
-public class Vala.InterfaceWriter : CodeVisitor {
+public class Vala.CodeWriter : CodeVisitor {
        private CodeContext context;
        
        FileStream stream;
@@ -38,6 +37,12 @@ public class Vala.InterfaceWriter : CodeVisitor {
 
        Scope current_scope;
 
+       bool dump_tree;
+
+       public CodeWriter (bool dump_tree = false) {
+               this.dump_tree = dump_tree;
+       }
+
        /**
         * Writes the public interface of the specified code context into the
         * specified file.
@@ -808,7 +813,9 @@ public class Vala.InterfaceWriter : CodeVisitor {
 
                // don't write interface implementation unless it's an abstract or virtual method
                if (!check_accessibility (m) || m.overrides || (m.base_interface_method != null && !m.is_abstract && !m.is_virtual)) {
-                       return;
+                       if (!dump_tree) {
+                               return;
+                       }
                }
 
                if (m.get_attribute ("NoWrapper") != null) {
@@ -912,11 +919,11 @@ public class Vala.InterfaceWriter : CodeVisitor {
                write_params (m.get_parameters ());
                write_error_domains (m.get_error_types ());
 
-               write_string (";");
+               write_code_block (m.body);
 
                write_newline ();
        }
-       
+
        public override void visit_creation_method (CreationMethod m) {
                visit_method (m);
        }
@@ -952,7 +959,8 @@ public class Vala.InterfaceWriter : CodeVisitor {
                write_identifier (prop.name);
                write_string (" {");
                if (prop.get_accessor != null) {
-                       write_string (" get;");
+                       write_string (" get");
+                       write_code_block (prop.get_accessor.body);
                }
                if (prop.set_accessor != null) {
                        if (prop.set_accessor.writable) {
@@ -961,7 +969,7 @@ public class Vala.InterfaceWriter : CodeVisitor {
                        if (prop.set_accessor.construction) {
                                write_string (" construct");
                        }
-                       write_string (";");
+                       write_code_block (prop.set_accessor.body);
                }
                write_string (" }");
                write_newline ();
@@ -1000,6 +1008,482 @@ public class Vala.InterfaceWriter : CodeVisitor {
                write_newline ();
        }
 
+       public override void visit_block (Block b) {
+               write_begin_block ();
+
+               foreach (Statement stmt in b.get_statements ()) {
+                       stmt.accept (this);
+               }
+
+               write_end_block ();
+       }
+
+       public override void visit_empty_statement (EmptyStatement stmt) {
+       }
+
+       public override void visit_declaration_statement (DeclarationStatement stmt) {
+               write_indent ();
+               stmt.declaration.accept (this);
+               write_string (";");
+               write_newline ();
+       }
+
+       public override void visit_local_variable (LocalVariable local) {
+               write_type (local.variable_type);
+               write_string (" ");
+               write_identifier (local.name);
+               if (local.initializer != null) {
+                       write_string (" = ");
+                       local.initializer.accept (this);
+               }
+       }
+
+       public override void visit_initializer_list (InitializerList list) {
+       }
+
+       public override void visit_expression_statement (ExpressionStatement stmt) {
+               write_indent ();
+               stmt.expression.accept (this);
+               write_string (";");
+               write_newline ();
+       }
+
+       public override void visit_if_statement (IfStatement stmt) {
+               write_indent ();
+               write_string ("if (");
+               stmt.condition.accept (this);
+               write_string (")");
+               stmt.true_statement.accept (this);
+               if (stmt.false_statement != null) {
+                       write_string (" else");
+                       stmt.false_statement.accept (this);
+               }
+               write_newline ();
+       }
+
+       public override void visit_switch_statement (SwitchStatement stmt) {
+               write_indent ();
+               write_string ("switch (");
+               stmt.expression.accept (this);
+               write_string (") {");
+               write_newline ();
+
+               foreach (SwitchSection section in stmt.get_sections ()) {
+                       section.accept (this);
+               }
+
+               write_indent ();
+               write_string ("}");
+               write_newline ();
+       }
+
+       public override void visit_switch_section (SwitchSection section) {
+               foreach (SwitchLabel label in section.get_labels ()) {
+                       label.accept (this);
+               }
+
+               visit_block (section);
+       }
+
+       public override void visit_switch_label (SwitchLabel label) {
+               if (label.expression != null) {
+                       write_indent ();
+                       write_string ("case ");
+                       label.expression.accept (this);
+                       write_string (":");
+                       write_newline ();
+               } else {
+                       write_indent ();
+                       write_string ("default:");
+                       write_newline ();
+               }
+       }
+
+       public override void visit_while_statement (WhileStatement stmt) {
+               write_indent ();
+               write_string ("while (");
+               stmt.condition.accept (this);
+               write_string (")");
+               stmt.body.accept (this);
+               write_newline ();
+       }
+
+       public override void visit_do_statement (DoStatement stmt) {
+               write_indent ();
+               write_string ("do");
+               stmt.body.accept (this);
+               write_string ("while (");
+               stmt.condition.accept (this);
+               write_string (");");
+               write_newline ();
+       }
+
+       public override void visit_for_statement (ForStatement stmt) {
+               write_indent ();
+               write_string ("for (");
+
+               bool first = true;
+               foreach (Expression initializer in stmt.get_initializer ()) {
+                       if (!first) {
+                               write_string (", ");
+                       }
+                       first = false;
+                       initializer.accept (this);
+               }
+               write_string ("; ");
+
+               stmt.condition.accept (this);
+               write_string ("; ");
+
+               first = true;
+               foreach (Expression iterator in stmt.get_iterator ()) {
+                       if (!first) {
+                               write_string (", ");
+                       }
+                       first = false;
+                       iterator.accept (this);
+               }
+
+               write_string (")");
+               stmt.body.accept (this);
+               write_newline ();
+       }
+
+       public override void visit_foreach_statement (ForeachStatement stmt) {
+       }
+
+       public override void visit_break_statement (BreakStatement stmt) {
+               write_indent ();
+               write_string ("break;");
+               write_newline ();
+       }
+
+       public override void visit_continue_statement (ContinueStatement stmt) {
+               write_indent ();
+               write_string ("continue;");
+               write_newline ();
+       }
+
+       public override void visit_return_statement (ReturnStatement stmt) {
+               write_indent ();
+               write_string ("return");
+               if (stmt.return_expression != null) {
+                       write_string (" ");
+                       stmt.return_expression.accept (this);
+               }
+               write_string (";");
+               write_newline ();
+       }
+
+       public override void visit_yield_statement (YieldStatement y) {
+               write_indent ();
+               write_string ("yield");
+               if (y.yield_expression != null) {
+                       write_string (" ");
+                       y.yield_expression.accept (this);
+               }
+               write_string (";");
+               write_newline ();
+       }
+
+       public override void visit_throw_statement (ThrowStatement stmt) {
+               write_indent ();
+               write_string ("throw");
+               if (stmt.error_expression != null) {
+                       write_string (" ");
+                       stmt.error_expression.accept (this);
+               }
+               write_string (";");
+               write_newline ();
+       }
+
+       public override void visit_try_statement (TryStatement stmt) {
+               write_indent ();
+               write_string ("try");
+               stmt.body.accept (this);
+               write_newline ();
+       }
+
+       public override void visit_catch_clause (CatchClause clause) {
+       }
+
+       public override void visit_lock_statement (LockStatement stmt) {
+       }
+
+       public override void visit_delete_statement (DeleteStatement stmt) {
+       }
+
+       public override void visit_array_creation_expression (ArrayCreationExpression expr) {
+       }
+
+       public override void visit_boolean_literal (BooleanLiteral lit) {
+               write_string (lit.value.to_string ());
+       }
+
+       public override void visit_character_literal (CharacterLiteral lit) {
+               write_string (lit.value);
+       }
+
+       public override void visit_integer_literal (IntegerLiteral lit) {
+               write_string (lit.value);
+       }
+
+       public override void visit_real_literal (RealLiteral lit) {
+               write_string (lit.value);
+       }
+
+       public override void visit_string_literal (StringLiteral lit) {
+               write_string (lit.value);
+       }
+
+       public override void visit_null_literal (NullLiteral lit) {
+               write_string ("null");
+       }
+
+       public override void visit_parenthesized_expression (ParenthesizedExpression expr) {
+               write_string ("(");
+               expr.inner.accept (this);
+               write_string (")");
+       }
+
+       public override void visit_member_access (MemberAccess expr) {
+               if (expr.inner != null) {
+                       expr.inner.accept (this);
+                       write_string (".");
+               }
+               write_identifier (expr.member_name);
+       }
+
+       public override void visit_method_call (MethodCall expr) {
+               expr.call.accept (this);
+               write_string (" (");
+
+               bool first = true;
+               foreach (Expression arg in expr.get_argument_list ()) {
+                       if (!first) {
+                               write_string (", ");
+                       }
+                       first = false;
+
+                       arg.accept (this);
+               }
+
+               write_string (")");
+       }
+       
+       public override void visit_element_access (ElementAccess expr) {
+               expr.container.accept (this);
+               write_string ("[");
+
+               bool first = true;
+               foreach (Expression index in expr.get_indices ()) {
+                       if (!first) {
+                               write_string (", ");
+                       }
+                       first = false;
+
+                       index.accept (this);
+               }
+
+               write_string ("]");
+       }
+
+       public override void visit_base_access (BaseAccess expr) {
+               write_string ("base");
+       }
+
+       public override void visit_postfix_expression (PostfixExpression expr) {
+               expr.inner.accept (this);
+               if (expr.increment) {
+                       write_string ("++");
+               } else {
+                       write_string ("--");
+               }
+       }
+
+       public override void visit_object_creation_expression (ObjectCreationExpression expr) {
+               write_string ("new ");
+               write_type (expr.type_reference);
+               write_string (" (");
+
+               bool first = true;
+               foreach (Expression arg in expr.get_argument_list ()) {
+                       if (!first) {
+                               write_string (", ");
+                       }
+                       first = false;
+
+                       arg.accept (this);
+               }
+
+               write_string (")");
+       }
+
+       public override void visit_sizeof_expression (SizeofExpression expr) {
+               write_string ("sizeof (");
+               write_type (expr.type_reference);
+               write_string (")");
+       }
+
+       public override void visit_typeof_expression (TypeofExpression expr) {
+               write_string ("typeof (");
+               write_type (expr.type_reference);
+               write_string (")");
+       }
+
+       public override void visit_unary_expression (UnaryExpression expr) {
+               switch (expr.operator) {
+               case UnaryOperator.PLUS:
+                       write_string ("+");
+                       break;
+               case UnaryOperator.MINUS:
+                       write_string ("-");
+                       break;
+               case UnaryOperator.LOGICAL_NEGATION:
+                       write_string ("!");
+                       break;
+               case UnaryOperator.BITWISE_COMPLEMENT:
+                       write_string ("~");
+                       break;
+               case UnaryOperator.INCREMENT:
+                       write_string ("++");
+                       break;
+               case UnaryOperator.DECREMENT:
+                       write_string ("--");
+                       break;
+               case UnaryOperator.REF:
+                       write_string ("ref ");
+                       break;
+               case UnaryOperator.OUT:
+                       write_string ("out ");
+                       break;
+               default:
+                       assert_not_reached ();
+               }
+               expr.inner.accept (this);
+       }
+
+       public override void visit_cast_expression (CastExpression expr) {
+               if (!expr.is_silent_cast) {
+                       write_string ("(");
+                       write_type (expr.type_reference);
+                       write_string (") ");
+               }
+
+               expr.inner.accept (this);
+
+               if (expr.is_silent_cast) {
+                       write_string (" as ");
+                       write_type (expr.type_reference);
+               }
+       }
+
+       public override void visit_pointer_indirection (PointerIndirection expr) {
+               write_string ("*");
+               expr.inner.accept (this);
+       }
+
+       public override void visit_addressof_expression (AddressofExpression expr) {
+               write_string ("&");
+               expr.inner.accept (this);
+       }
+
+       public override void visit_reference_transfer_expression (ReferenceTransferExpression expr) {
+               write_string ("#");
+               expr.inner.accept (this);
+       }
+
+       public override void visit_binary_expression (BinaryExpression expr) {
+               expr.left.accept (this);
+
+               switch (expr.operator) {
+               case BinaryOperator.PLUS:
+                       write_string (" + ");
+                       break;
+               case BinaryOperator.MINUS:
+                       write_string (" - ");
+                       break;
+               case BinaryOperator.MUL:
+                       write_string (" * ");
+                       break;
+               case BinaryOperator.DIV:
+                       write_string (" / ");
+                       break;
+               case BinaryOperator.MOD:
+                       write_string (" % ");
+                       break;
+               case BinaryOperator.SHIFT_LEFT:
+                       write_string (" << ");
+                       break;
+               case BinaryOperator.SHIFT_RIGHT:
+                       write_string (" >> ");
+                       break;
+               case BinaryOperator.LESS_THAN:
+                       write_string (" < ");
+                       break;
+               case BinaryOperator.GREATER_THAN:
+                       write_string (" > ");
+                       break;
+               case BinaryOperator.LESS_THAN_OR_EQUAL:
+                       write_string (" <= ");
+                       break;
+               case BinaryOperator.GREATER_THAN_OR_EQUAL:
+                       write_string (" >= ");
+                       break;
+               case BinaryOperator.EQUALITY:
+                       write_string (" == ");
+                       break;
+               case BinaryOperator.INEQUALITY:
+                       write_string (" != ");
+                       break;
+               case BinaryOperator.BITWISE_AND:
+                       write_string (" & ");
+                       break;
+               case BinaryOperator.BITWISE_OR:
+                       write_string (" | ");
+                       break;
+               case BinaryOperator.BITWISE_XOR:
+                       write_string (" ^ ");
+                       break;
+               case BinaryOperator.AND:
+                       write_string (" && ");
+                       break;
+               case BinaryOperator.OR:
+                       write_string (" || ");
+                       break;
+               case BinaryOperator.IN:
+                       write_string (" in ");
+                       break;
+               default:
+                       assert_not_reached ();
+               }
+
+               expr.right.accept (this);
+       }
+
+       public override void visit_type_check (TypeCheck expr) {
+               expr.expression.accept (this);
+               write_string (" is ");
+               write_type (expr.type_reference);
+       }
+
+       public override void visit_conditional_expression (ConditionalExpression expr) {
+               expr.condition.accept (this);
+               write_string ("?");
+               expr.true_expression.accept (this);
+               write_string (":");
+               expr.false_expression.accept (this);
+       }
+
+       public override void visit_lambda_expression (LambdaExpression expr) {
+       }
+
+       public override void visit_assignment (Assignment a) {
+               a.left.accept (this);
+               write_string (" = ");
+               a.right.accept (this);
+       }
+
        private void write_indent () {
                int i;
                
@@ -1066,6 +1550,15 @@ public class Vala.InterfaceWriter : CodeVisitor {
                bol = true;
        }
        
+       void write_code_block (Block? block) {
+               if (block == null || !dump_tree) {
+                       write_string (";");
+                       return;
+               }
+
+               block.accept (this);
+       }
+
        private void write_begin_block () {
                if (!bol) {
                        stream.putc (' ');
@@ -1084,7 +1577,8 @@ public class Vala.InterfaceWriter : CodeVisitor {
        }
 
        private bool check_accessibility (Symbol sym) {
-               if (sym.access == SymbolAccessibility.PUBLIC ||
+               if (dump_tree ||
+                   sym.access == SymbolAccessibility.PUBLIC ||
                    sym.access == SymbolAccessibility.PROTECTED) {
                        return true;
                }
@@ -1097,8 +1591,10 @@ public class Vala.InterfaceWriter : CodeVisitor {
                        write_string ("public ");
                } else if (sym.access == SymbolAccessibility.PROTECTED) {
                        write_string ("protected ");
-               } else {
-                       assert_not_reached ();
+               } else if (sym.access == SymbolAccessibility.INTERNAL) {
+                       write_string ("internal ");
+               } else if (sym.access == SymbolAccessibility.PRIVATE) {
+                       write_string ("private ");
                }
        }
 }
index 46bf6fdbd2c8b579012c464d4d92f8412ac45ac2..289aee1ab26c4500fb2e5ca9073dcf425eb1b67e 100644 (file)
@@ -194,7 +194,7 @@ class Vala.VAPIGen : Object {
                                }
                        }
 
-                       var interface_writer = new InterfaceWriter ();
+                       var interface_writer = new CodeWriter ();
                        interface_writer.write_file (context, "%s.vapi".printf (library));
                        
                        library = null;