Commit 6c9d3f30 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

Don't report value ownership errors for non-disposable types

2008-05-18  Juerg Billeter  <j@bitron.ch>

	* vala/valadatatype.vala:
	* vala/valapointertype.vala:
	* vala/valasemanticanalyzer.vala:
	* gobject/valaccodegenerator.vala:

	Don't report value ownership errors for non-disposable types

svn path=/trunk/; revision=1399
parent 414ff723
2008-05-18 Jürg Billeter <j@bitron.ch>
* vala/valadatatype.vala:
* vala/valapointertype.vala:
* vala/valasemanticanalyzer.vala:
* gobject/valaccodegenerator.vala:
Don't report value ownership errors for non-disposable types
2008-05-18 Jürg Billeter <j@bitron.ch>
* vala/Makefile.am:
......
......@@ -2811,41 +2811,31 @@ public class Vala.CCodeGenerator : CodeGenerator {
}
public bool requires_copy (DataType type) {
if (!type.value_owned) {
if (!type.is_disposable ()) {
return false;
}
if (type.is_reference_type_or_type_parameter ()) {
if (type.type_parameter != null) {
if (!(current_type_symbol is Class) || !current_class.is_subtype_of (gobject_type)) {
return false;
}
if (type.type_parameter != null) {
if (!(current_type_symbol is Class) || !current_class.is_subtype_of (gobject_type)) {
return false;
}
return true;
} else if (type is ValueType) {
// nullable structs are heap allocated
return type.nullable;
}
return false;
return true;
}
public bool requires_destroy (DataType type) {
if (!type.value_owned) {
if (!type.is_disposable ()) {
return false;
}
if (type.is_reference_type_or_type_parameter ()) {
if (type.type_parameter != null) {
if (!(current_type_symbol is Class) || !current_class.is_subtype_of (gobject_type)) {
return false;
}
if (type.type_parameter != null) {
if (!(current_type_symbol is Class) || !current_class.is_subtype_of (gobject_type)) {
return false;
}
return true;
} else if (type is ValueType) {
// nullable structs are heap allocated
return type.nullable;
}
return false;
return true;
}
private CCodeExpression? get_ref_expression (Expression expr) {
......
......@@ -414,4 +414,23 @@ public abstract class Vala.DataType : CodeNode {
return null;
}
}
/**
* Returns whether the value needs to be disposed, i.e. whether
* allocated memory or other resources need to be released when
* the value is no longer needed.
*/
public virtual bool is_disposable () {
if (!value_owned) {
return false;
}
if (is_reference_type_or_type_parameter ()) {
return true;
} else if (this is ValueType) {
// nullable structs are heap allocated
return nullable;
}
return false;
}
}
......@@ -107,4 +107,8 @@ public class Vala.PointerType : DataType {
base_type = new_type;
}
}
public override bool is_disposable () {
return false;
}
}
......@@ -851,7 +851,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return;
}
if (local.initializer.value_type.value_owned) {
if (local.initializer.value_type.is_disposable ()) {
/* rhs transfers ownership of the expression */
if (!(local.variable_type is PointerType) && !local.variable_type.value_owned) {
/* lhs doesn't own the value */
......@@ -1154,7 +1154,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
}
if (stmt.return_expression != null &&
stmt.return_expression.value_type.value_owned &&
stmt.return_expression.value_type.is_disposable () &&
!current_return_type.value_owned) {
stmt.error = true;
Report.error (stmt.source_reference, "Return value transfers ownership but method return type hasn't been declared to transfer ownership");
......@@ -1163,7 +1163,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
if (stmt.return_expression != null &&
stmt.return_expression.symbol_reference is LocalVariable &&
stmt.return_expression.value_type.value_owned &&
stmt.return_expression.value_type.is_disposable () &&
!current_return_type.value_owned) {
Report.warning (stmt.source_reference, "Local variable with strong reference used as return value and method return type hasn't been declared to transfer ownership");
}
......@@ -2688,7 +2688,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return;
}
if (!expr.inner.value_type.value_owned
if (!expr.inner.value_type.is_disposable ()
&& !(expr.inner.value_type is PointerType)) {
expr.error = true;
Report.error (expr.source_reference, "No reference to be transferred");
......@@ -3170,7 +3170,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return;
}
if (a.right.value_type.value_owned) {
if (a.right.value_type.is_disposable ()) {
/* rhs transfers ownership of the expression */
if (!(a.left.value_type is PointerType) && !a.left.value_type.value_owned) {
/* lhs doesn't own the value */
......@@ -3193,7 +3193,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return;
}
if (a.right.value_type.value_owned) {
if (a.right.value_type.is_disposable ()) {
/* rhs transfers ownership of the expression */
DataType element_type;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment