From 83363b2b4661a88a2cd703003341438dfceba869 Mon Sep 17 00:00:00 2001 From: Juerg Billeter Date: Fri, 13 Jul 2007 09:27:38 +0000 Subject: [PATCH] move iteration of throw statements, try statements, and catch clauses from 2007-07-13 Juerg Billeter * vala/valacatchclause.vala, vala/valacodevisitor.vala, vala/valamemorymanager.vala, vala/valasemanticanalyzer.vala, vala/valasymbolbuilder.vala, vala/valasymbolresolver.vala, vala/valathrowstatement.vala, vala/valatrystatement.vala, gobject/valacodegenerator.vala: move iteration of throw statements, try statements, and catch clauses from accept to accept_children method, fixes nested try statements * gobject/valacodegenerator.vala: clear unhandled error instead of returning from the current function * vala/valasemanticanalyzer.vala: generic pointer is compatible with generic pointer * vapi/glib-2.0.vala: add some more default values svn path=/trunk/; revision=354 --- ChangeLog | 15 +++++++++++++ gobject/valacodegenerator.vala | 50 +++++++++++++++++++++++++++--------------- vala/valacatchclause.vala | 6 ++--- vala/valacodevisitor.vala | 36 +++++------------------------- vala/valamemorymanager.vala | 12 ++++++++++ vala/valasemanticanalyzer.vala | 15 +++++++++++-- vala/valasymbolbuilder.vala | 8 +++++++ vala/valasymbolresolver.vala | 12 ++++++++++ vala/valathrowstatement.vala | 8 +++---- vala/valatrystatement.vala | 6 ++--- vapi/glib-2.0.vala | 8 +++---- 11 files changed, 112 insertions(+), 64 deletions(-) diff --git a/ChangeLog b/ChangeLog index 9b8c175..47b26cd 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,20 @@ 2007-07-13 Jürg Billeter + * vala/valacatchclause.vala, vala/valacodevisitor.vala, + vala/valamemorymanager.vala, vala/valasemanticanalyzer.vala, + vala/valasymbolbuilder.vala, vala/valasymbolresolver.vala, + vala/valathrowstatement.vala, vala/valatrystatement.vala, + gobject/valacodegenerator.vala: move iteration of throw statements, + try statements, and catch clauses from accept to accept_children + method, fixes nested try statements + * gobject/valacodegenerator.vala: clear unhandled error instead of + returning from the current function + * vala/valasemanticanalyzer.vala: generic pointer is compatible with + generic pointer + * vapi/glib-2.0.vala: add some more default values + +2007-07-13 Jürg Billeter + * vala/parser.y, gobject/valacodegenerator.vala: support multi- dimensional arrays diff --git a/gobject/valacodegenerator.vala b/gobject/valacodegenerator.vala index e8c9d6b..6e91f14 100644 --- a/gobject/valacodegenerator.vala +++ b/gobject/valacodegenerator.vala @@ -76,6 +76,7 @@ public class Vala.CodeGenerator : CodeVisitor { HashTable c_keywords; private int next_temp_var_id = 0; + private int current_try_id = 0; private int next_try_id = 0; private bool in_creation_method = false; private bool current_method_inner_error = false; @@ -1040,11 +1041,9 @@ public class Vala.CodeGenerator : CodeVisitor { ccritical.add_argument (new CCodeConstant ("__LINE__")); ccritical.add_argument (new CCodeMemberAccess.pointer (new CCodeIdentifier ("inner_error"), "message")); cprint_frag.append (new CCodeExpressionStatement (ccritical)); - if (current_return_type != null && current_return_type.data_type != null) { - cprint_frag.append (new CCodeReturnStatement (default_value_for_type (current_return_type.data_type))); - } else { - cprint_frag.append (new CCodeReturnStatement ()); - } + var cclear = new CCodeFunctionCall (new CCodeIdentifier ("g_clear_error")); + cclear.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, new CCodeIdentifier ("inner_error"))); + cprint_frag.append (new CCodeExpressionStatement (cclear)); if (current_try != null) { // surrounding try found @@ -1056,11 +1055,11 @@ public class Vala.CodeGenerator : CodeVisitor { var ccond = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, new CCodeMemberAccess.pointer (new CCodeIdentifier ("inner_error"), "domain"), new CCodeIdentifier (clause.type_reference.data_type.get_upper_case_cname ())); var cgoto_block = new CCodeBlock (); - cgoto_block.add_statement (new CCodeGotoStatement ("__catch%d_%s".printf (next_try_id, clause.type_reference.data_type.get_lower_case_cname ()))); + cgoto_block.add_statement (new CCodeGotoStatement ("__catch%d_%s".printf (current_try_id, clause.type_reference.data_type.get_lower_case_cname ()))); cerror_block.add_statement (new CCodeIfStatement (ccond, cgoto_block)); } - // print critical message and return if no catch clause matches + // print critical message if no catch clause matches cerror_block.add_statement (cprint_frag); // check error domain if expression failed @@ -1088,8 +1087,17 @@ public class Vala.CodeGenerator : CodeVisitor { cfrag.append (new CCodeIfStatement (ccond, cerror_block)); } else { + // TODO improve check and move to semantic analyzer Report.warning (node.source_reference, "unhandled error"); - cfrag.append (cprint_frag); + + var cerror_block = new CCodeBlock (); + // print critical message + cerror_block.add_statement (cprint_frag); + + // check error domain if expression failed + var ccond = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier ("inner_error"), new CCodeConstant ("NULL")); + + cfrag.append (new CCodeIfStatement (ccond, cerror_block)); } } @@ -1559,7 +1567,9 @@ public class Vala.CodeGenerator : CodeVisitor { } } - public override void visit_end_throw_statement (ThrowStatement! stmt) { + public override void visit_throw_statement (ThrowStatement! stmt) { + stmt.accept_children (this); + var cfrag = new CCodeFragment (); cfrag.append (new CCodeExpressionStatement ((CCodeExpression) stmt.error_expression.ccodenode)); @@ -1573,23 +1583,24 @@ public class Vala.CodeGenerator : CodeVisitor { stmt.ccodenode = cfrag; } - public override void visit_begin_try_statement (TryStatement! stmt) { + public override void visit_try_statement (TryStatement! stmt) { + var old_try = current_try; + var old_try_id = current_try_id; current_try = stmt; - } + current_try_id = next_try_id++; - public override void visit_end_try_statement (TryStatement! stmt) { - current_try = null; + stmt.accept_children (this); var cfrag = new CCodeFragment (); cfrag.append (stmt.body.ccodenode); foreach (CatchClause clause in stmt.get_catch_clauses ()) { - cfrag.append (new CCodeGotoStatement ("__finally%d".printf (next_try_id))); + cfrag.append (new CCodeGotoStatement ("__finally%d".printf (current_try_id))); cfrag.append (clause.ccodenode); } - cfrag.append (new CCodeLabel ("__finally%d".printf (next_try_id))); + cfrag.append (new CCodeLabel ("__finally%d".printf (current_try_id))); if (stmt.finally_body != null) { cfrag.append (stmt.finally_body.ccodenode); } else { @@ -1599,12 +1610,15 @@ public class Vala.CodeGenerator : CodeVisitor { stmt.ccodenode = cfrag; - next_try_id++; + current_try = old_try; + current_try_id = old_try_id; } - public override void visit_end_catch_clause (CatchClause! clause) { + public override void visit_catch_clause (CatchClause! clause) { + clause.accept_children (this); + var cfrag = new CCodeFragment (); - cfrag.append (new CCodeLabel ("__catch%d_%s".printf (next_try_id, clause.type_reference.data_type.get_lower_case_cname ()))); + cfrag.append (new CCodeLabel ("__catch%d_%s".printf (current_try_id, clause.type_reference.data_type.get_lower_case_cname ()))); var cblock = new CCodeBlock (); diff --git a/vala/valacatchclause.vala b/vala/valacatchclause.vala index 3c8af19..389e09f 100644 --- a/vala/valacatchclause.vala +++ b/vala/valacatchclause.vala @@ -59,11 +59,11 @@ public class Vala.CatchClause : CodeNode { } public override void accept (CodeVisitor! visitor) { - visitor.visit_begin_catch_clause (this); + visitor.visit_catch_clause (this); + } + public override void accept_children (CodeVisitor! visitor) { type_reference.accept (visitor); body.accept (visitor); - - visitor.visit_end_catch_clause (this); } } diff --git a/vala/valacodevisitor.vala b/vala/valacodevisitor.vala index f7589e7..b4b702e 100644 --- a/vala/valacodevisitor.vala +++ b/vala/valacodevisitor.vala @@ -404,54 +404,30 @@ public abstract class Vala.CodeVisitor { } /** - * Visit operation called at beginning of throw statements. + * Visit operation called for throw statements. * * @param stmt a throw statement */ - public virtual void visit_begin_throw_statement (ThrowStatement! stmt) { + public virtual void visit_throw_statement (ThrowStatement! stmt) { } /** - * Visit operation called at end of throw statements. - * - * @param stmt a throw statement - */ - public virtual void visit_end_throw_statement (ThrowStatement! stmt) { - } - - /** - * Visit operation called at beginning of try statements. + * Visit operation called for try statements. * * @param stmt a try statement */ - public virtual void visit_begin_try_statement (TryStatement! stmt) { + public virtual void visit_try_statement (TryStatement! stmt) { } /** - * Visit operation called at end of try statements. - * - * @param stmt a try statement - */ - public virtual void visit_end_try_statement (TryStatement! stmt) { - } - - /** - * Visit operation called at beginning of catch clauses. + * Visit operation called for catch clauses. * * @param clause a catch cluase */ - public virtual void visit_begin_catch_clause (CatchClause! clause) { + public virtual void visit_catch_clause (CatchClause! clause) { } /** - * Visit operation called at end of catch clauses. - * - * @param clause a catch clause - */ - public virtual void visit_end_catch_clause (CatchClause! clause) { - } - - /** * Visit operation called for lock statements before the body has been visited. * * @param stmt a lock statement diff --git a/vala/valamemorymanager.vala b/vala/valamemorymanager.vala index 1a2411a..909f6a6 100644 --- a/vala/valamemorymanager.vala +++ b/vala/valamemorymanager.vala @@ -154,6 +154,18 @@ public class Vala.MemoryManager : CodeVisitor { } } + public override void visit_throw_statement (ThrowStatement! stmt) { + stmt.accept_children (this); + } + + public override void visit_try_statement (TryStatement! stmt) { + stmt.accept_children (this); + } + + public override void visit_catch_clause (CatchClause! clause) { + clause.accept_children (this); + } + public override void visit_member_access (MemberAccess! expr) { if (expr.inner != null) { visit_possibly_leaked_expression (expr.inner); diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala index aea30a2..c62c613 100644 --- a/vala/valasemanticanalyzer.vala +++ b/vala/valasemanticanalyzer.vala @@ -890,7 +890,15 @@ public class Vala.SemanticAnalyzer : CodeVisitor { } } - public override void visit_begin_catch_clause (CatchClause! clause) { + public override void visit_throw_statement (ThrowStatement! stmt) { + stmt.accept_children (this); + } + + public override void visit_try_statement (TryStatement! stmt) { + stmt.accept_children (this); + } + + public override void visit_catch_clause (CatchClause! clause) { if (clause.type_reference.data_type != null) { current_source_file.add_symbol_dependency (clause.type_reference.data_type.symbol, SourceFileDependencyType.SOURCE); } @@ -901,6 +909,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor { clause.variable_declarator.symbol = new Symbol (clause.variable_declarator); clause.body.symbol.add (clause.variable_name, clause.variable_declarator.symbol); + + clause.accept_children (this); } /** @@ -1202,7 +1212,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor { expression_type.data_type.is_reference_type () || expression_type.data_type is Pointer || expression_type.data_type is Array || - expression_type.data_type is Callback) { + expression_type.data_type is Callback || + expression_type.data_type == pointer_type) { return true; } diff --git a/vala/valasymbolbuilder.vala b/vala/valasymbolbuilder.vala index 98c1a26..3389324 100644 --- a/vala/valasymbolbuilder.vala +++ b/vala/valasymbolbuilder.vala @@ -406,6 +406,14 @@ public class Vala.SymbolBuilder : CodeVisitor { current_symbol = current_symbol.parent_symbol; } + public override void visit_try_statement (TryStatement! stmt) { + stmt.accept_children (this); + } + + public override void visit_catch_clause (CatchClause! clause) { + clause.accept_children (this); + } + public override void visit_begin_block (Block! b) { b.symbol = new Symbol (b); b.symbol.parent_symbol = current_symbol; diff --git a/vala/valasymbolresolver.vala b/vala/valasymbolresolver.vala index e2c9f01..1111857 100644 --- a/vala/valasymbolresolver.vala +++ b/vala/valasymbolresolver.vala @@ -282,4 +282,16 @@ public class Vala.SymbolResolver : CodeVisitor { type.transfers_ownership = false; } } + + public override void visit_throw_statement (ThrowStatement! stmt) { + stmt.accept_children (this); + } + + public override void visit_try_statement (TryStatement! stmt) { + stmt.accept_children (this); + } + + public override void visit_catch_clause (CatchClause! clause) { + clause.accept_children (this); + } } diff --git a/vala/valathrowstatement.vala b/vala/valathrowstatement.vala index 3a69ae5..f89854e 100644 --- a/vala/valathrowstatement.vala +++ b/vala/valathrowstatement.vala @@ -52,17 +52,17 @@ public class Vala.ThrowStatement : Statement { */ public ThrowStatement (construct Expression! error_expression, construct SourceReference source_reference = null) { } - + public override void accept (CodeVisitor! visitor) { - visitor.visit_begin_throw_statement (this); + visitor.visit_throw_statement (this); + } + public override void accept_children (CodeVisitor! visitor) { if (error_expression != null) { error_expression.accept (visitor); visitor.visit_end_full_expression (error_expression); } - - visitor.visit_end_throw_statement (this); } public override void replace (CodeNode! old_node, CodeNode! new_node) { diff --git a/vala/valatrystatement.vala b/vala/valatrystatement.vala index 45b3165..3e25a49 100644 --- a/vala/valatrystatement.vala +++ b/vala/valatrystatement.vala @@ -68,14 +68,14 @@ public class Vala.TryStatement : Statement { } public override void accept (CodeVisitor! visitor) { - visitor.visit_begin_try_statement (this); + visitor.visit_try_statement (this); + } + public override void accept_children (CodeVisitor! visitor) { body.accept (visitor); foreach (CatchClause clause in catch_clauses) { clause.accept (visitor); } - - visitor.visit_end_try_statement (this); } } diff --git a/vapi/glib-2.0.vala b/vapi/glib-2.0.vala index d13f3ef..e6534aa 100644 --- a/vapi/glib-2.0.vala +++ b/vapi/glib-2.0.vala @@ -1033,7 +1033,7 @@ namespace GLib { /* Character Set Conversions */ - public static string convert (string! str, long len, string! to_codeset, string! from_codeset, out int bytes_read, out int bytes_written) throws ConvertError; + public static string convert (string! str, long len, string! to_codeset, string! from_codeset, out int bytes_read = null, out int bytes_written = null) throws ConvertError; public struct IConv { [CCode (cname = "g_iconv_open")] @@ -1373,8 +1373,8 @@ namespace GLib { [CCode (cprefix = "g_file_", cheader_filename = "glib/gstdio.h")] public struct FileUtils { - public static bool get_contents (string! filename, out string contents, out long length) throws FileError; - public static bool set_contents (string! filename, string contents, long length) throws FileError; + public static bool get_contents (string! filename, out string contents, out long length = null) throws FileError; + public static bool set_contents (string! filename, string contents, long length = -1) throws FileError; public static bool test (string filename, FileTest test); public static int open_tmp (string tmpl, out string name_used) throws FileError; public static string read_link (string filename) throws FileError; @@ -1390,7 +1390,7 @@ namespace GLib { [ReferenceType (free_function = "g_dir_close")] public struct Dir { - public static Dir open (string filename, uint _flags) throws FileError; + public static Dir open (string filename, uint _flags = 0) throws FileError; public weak string read_name (); [CCode (cname = "g_mkdir")] -- 2.7.4