]> git.ipfire.org Git - thirdparty/vala.git/commitdiff
Support return statements in constructors and destructors
authorJürg Billeter <j@bitron.ch>
Thu, 21 Oct 2010 08:08:04 +0000 (10:08 +0200)
committerJürg Billeter <j@bitron.ch>
Thu, 21 Oct 2010 08:08:04 +0000 (10:08 +0200)
Fixes bug 573763.

codegen/valaccodebasemodule.vala
codegen/valagtypemodule.vala
vala/valasemanticanalyzer.vala

index a1bc6998c77dee5555c312f66b7c9aeb05772a08..561f535dc72174d91095432e92c81b356bf33fc2 100644 (file)
@@ -121,10 +121,36 @@ public class Vala.CCodeBaseModule : CodeGenerator {
                                }
                        }
 
+                       if (is_in_constructor () || is_in_destructor ()) {
+                               return void_type;
+                       }
+
                        return null;
                }
        }
 
+       bool is_in_constructor () {
+               var sym = current_symbol;
+               while (sym != null) {
+                       if (sym is Constructor) {
+                               return true;
+                       }
+                       sym = sym.parent_symbol;
+               }
+               return false;
+       }
+
+       bool is_in_destructor () {
+               var sym = current_symbol;
+               while (sym != null) {
+                       if (sym is Destructor) {
+                               return true;
+                       }
+                       sym = sym.parent_symbol;
+               }
+               return false;
+       }
+
        public Block? current_closure_block {
                get {
                        return next_closure_block (current_symbol);
@@ -3326,7 +3352,13 @@ public class Vala.CCodeBaseModule : CodeGenerator {
                        }
                }
 
-               if (current_method is CreationMethod) {
+               if (is_in_constructor ()) {
+                       ccode.add_return (new CCodeIdentifier ("obj"));
+               } else if (is_in_destructor ()) {
+                       // do not call return as member cleanup and chain up to base finalizer
+                       // stil need to be executed
+                       ccode.add_goto ("_return");
+               } else if (current_method is CreationMethod) {
                        ccode.add_return (new CCodeIdentifier ("self"));
                } else if (current_method != null && current_method.coroutine) {
                } else if (current_return_type is VoidType || current_return_type.is_real_non_null_struct_type ()) {
index e5d0bc4230ab0604f52fe48d0cc3603000f6297a..61c61f11750c18dd7c47ab93e1782413db1eb207 100644 (file)
@@ -1653,6 +1653,9 @@ public class Vala.GTypeModule : GErrorModule {
                        if (current_method_inner_error) {
                                ccode.add_declaration ("GError *", new CCodeVariableDeclarator.zero ("_inner_error_", new CCodeConstant ("NULL")));
                        }
+
+                       // support return statements in destructors
+                       ccode.add_label ("_return");
                }
 
                pop_context ();
index c6b718b90c2f09eda2094c5a7a80c9cc4fd54269..22d394f4851c13010c4a741624f97c4ecd605fef 100644 (file)
@@ -123,6 +123,10 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                                }
                        }
 
+                       if (is_in_constructor () || is_in_destructor ()) {
+                               return void_type;
+                       }
+
                        return null;
                }
        }
@@ -885,4 +889,15 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
                }
                return false;
        }
+
+       public bool is_in_destructor () {
+               var sym = current_symbol;
+               while (sym != null) {
+                       if (sym is Destructor) {
+                               return true;
+                       }
+                       sym = sym.parent_symbol;
+               }
+               return false;
+       }
 }