[vala] Support postconditions in normal methods
- From: Jürg Billeter <juergbi src gnome org>
- To: svn-commits-list gnome org
- Subject: [vala] Support postconditions in normal methods
- Date: Thu, 4 Jun 2009 08:16:57 -0400 (EDT)
commit 9a017f8975900a736b231a3b2dee8e0aca8c383f
Author: Jürg Billeter <j bitron ch>
Date: Thu Jun 4 14:14:09 2009 +0200
Support postconditions in normal methods
Fixes bug 529680.
---
codegen/valaccodebasemodule.vala | 33 +++++++++++++++++++++++++++--
codegen/valaccodememberaccessmodule.vala | 7 +++++-
codegen/valaccodemethodmodule.vala | 8 -------
vala/valaclass.vala | 1 +
vala/valainterface.vala | 2 +-
vala/valalocalvariable.vala | 2 +
vala/valastruct.vala | 2 +-
7 files changed, 41 insertions(+), 14 deletions(-)
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 9ce9d08..fa55b8d 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -2343,9 +2343,21 @@ internal class Vala.CCodeBaseModule : CCodeModule {
stmt.accept_children (codegen);
if (stmt.return_expression == null) {
- stmt.ccodenode = new CCodeReturnStatement ();
-
- create_local_free (stmt);
+ var cfrag = new CCodeFragment ();
+
+ // free local variables
+ append_local_free (current_symbol, cfrag);
+
+ if (current_method != null) {
+ // check postconditions
+ foreach (Expression postcondition in current_method.get_postconditions ()) {
+ cfrag.append (create_postcondition_statement (postcondition));
+ }
+ }
+
+ cfrag.append (new CCodeReturnStatement ());
+
+ stmt.ccodenode = cfrag;
} else {
Symbol return_expression_symbol = null;
@@ -2390,6 +2402,13 @@ internal class Vala.CCodeBaseModule : CCodeModule {
// free local variables
append_local_free (current_symbol, cfrag);
+ if (current_method != null) {
+ // check postconditions
+ foreach (Expression postcondition in current_method.get_postconditions ()) {
+ cfrag.append (create_postcondition_statement (postcondition));
+ }
+ }
+
// Property getters of non simple structs shall return the struct value as out parameter,
// therefore replace any return statement with an assignment statement to the out formal
// paramenter and insert an empty return statement afterwards.
@@ -3912,6 +3931,14 @@ internal class Vala.CCodeBaseModule : CCodeModule {
public override void visit_class (Class cl) {
}
+
+ public CCodeStatement create_postcondition_statement (Expression postcondition) {
+ var cassert = new CCodeFunctionCall (new CCodeIdentifier ("g_warn_if_fail"));
+
+ cassert.add_argument ((CCodeExpression) postcondition.ccodenode);
+
+ return new CCodeExpressionStatement (cassert);
+ }
}
// vim:sw=8 noet
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index e257ed1..cdeec13 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -298,7 +298,12 @@ internal class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
expr.ccodenode = new CCodeConstant (ev.get_cname ());
} else if (expr.symbol_reference is LocalVariable) {
var local = (LocalVariable) expr.symbol_reference;
- expr.ccodenode = get_variable_cexpression (local.name);
+ if (local.is_result) {
+ // used in postconditions
+ expr.ccodenode = new CCodeIdentifier ("result");
+ } else {
+ expr.ccodenode = get_variable_cexpression (local.name);
+ }
} else if (expr.symbol_reference is FormalParameter) {
var p = (FormalParameter) expr.symbol_reference;
if (p.name == "this") {
diff --git a/codegen/valaccodemethodmodule.vala b/codegen/valaccodemethodmodule.vala
index ecf1519..7c1d8c7 100644
--- a/codegen/valaccodemethodmodule.vala
+++ b/codegen/valaccodemethodmodule.vala
@@ -918,14 +918,6 @@ internal class Vala.CCodeMethodModule : CCodeStructModule {
return new CCodeExpressionStatement (ccheck);
}
- private CCodeStatement create_postcondition_statement (Expression postcondition) {
- var cassert = new CCodeFunctionCall (new CCodeIdentifier ("g_assert"));
-
- cassert.add_argument ((CCodeExpression) postcondition.ccodenode);
-
- return new CCodeExpressionStatement (cassert);
- }
-
private TypeSymbol? find_parent_type (Symbol sym) {
while (sym != null) {
if (sym is TypeSymbol) {
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index 2511d1a..7d9a8cd 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -321,6 +321,7 @@ public class Vala.Class : ObjectTypeSymbol {
m.scope.remove (m.result_var.name);
}
m.result_var = new LocalVariable (m.return_type.copy (), "result");
+ m.result_var.is_result = true;
}
if (m is CreationMethod) {
if (m.name == null) {
diff --git a/vala/valainterface.vala b/vala/valainterface.vala
index be55fcf..9966477 100644
--- a/vala/valainterface.vala
+++ b/vala/valainterface.vala
@@ -140,7 +140,7 @@ public class Vala.Interface : ObjectTypeSymbol {
}
if (!(m.return_type is VoidType) && m.get_postconditions ().size > 0) {
m.result_var = new LocalVariable (m.return_type.copy (), "result");
- m.scope.add (m.result_var.name, m.result_var);
+ m.result_var.is_result = true;
}
methods.add (m);
diff --git a/vala/valalocalvariable.vala b/vala/valalocalvariable.vala
index 7502551..c404b2d 100644
--- a/vala/valalocalvariable.vala
+++ b/vala/valalocalvariable.vala
@@ -54,6 +54,8 @@ public class Vala.LocalVariable : Symbol {
}
}
+ public bool is_result { get; set; }
+
/**
* Floating variables may only be accessed exactly once.
*/
diff --git a/vala/valastruct.vala b/vala/valastruct.vala
index f715b3b..8f5fbca 100644
--- a/vala/valastruct.vala
+++ b/vala/valastruct.vala
@@ -177,7 +177,7 @@ public class Vala.Struct : TypeSymbol {
}
if (!(m.return_type is VoidType) && m.get_postconditions ().size > 0) {
m.result_var = new LocalVariable (m.return_type.copy (), "result");
- m.scope.add (m.result_var.name, m.result_var);
+ m.result_var.is_result = true;
}
if (m is CreationMethod) {
if (m.name == null) {
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]