Commit 1d3e2293 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

always default type arguments to strong avoid unnecessary ref/unref pair

2006-09-02  Jürg Billeter  <j@bitron.ch>

	* vala/parser.y, vala/valacodecontext.vala,
	  vala/valasemanticanalyzer.vala, vala/valamemorymanager.vala,
	  vala/valacodegenerator.vala, vala/valaarraycreationexpression.vala,
	  vala/valablock.vala, vala/valacallback.vala, vala/valaclass.vala,
	  vala/valaelementaccess.vala, vala/valafield.vala,
	  vala/valaformalparameter.vala, vala/valaforstatement.vala,
	  vala/valainitializerlist.vala, vala/valainterface.vala,
	  vala/valainvocationexpression.vala, vala/valainvokable.vala,
	  vala/valalambdaexpression.vala,
	  vala/valalocalvariabledeclaration.vala, vala/valamemberaccess.vala,
	  vala/valamethod.vala, vala/valanamespace.vala,
	  vala/valaobjectcreationexpression.vala, vala/valasignal.vala,
	  vala/valasourcefile.vala, vala/valastruct.vala,
	  vala/valaswitchsection.vala, vala/valaswitchstatement.vala,
	  vala/valatypereference.vala, vala/valavariabledeclarator.vala,
	  ccode/valaccodefragment.vala, vapi/glib-2.0.vala: always default type
	  arguments to strong
	* vala/valacodevisitor.vala, vala/valacodegenerator.vala,
	  vala/valaexpression.vala, vala/valareturnstatement.vala: avoid
	  unnecessary ref/unref pair in return expression
	* vala/valatypereference.vala: copy type arguments in copy method, too
	* vala/valasemanticanalyzer.vala: set type arguments in type_reference
	  of ObjectCreationExpression
	* vala/valadatatype.vala: fix warning

svn path=/trunk/; revision=122
parent 23106714
2006-09-02 Jürg Billeter <j@bitron.ch>
* vala/parser.y, vala/valacodecontext.vala,
vala/valasemanticanalyzer.vala, vala/valamemorymanager.vala,
vala/valacodegenerator.vala, vala/valaarraycreationexpression.vala,
vala/valablock.vala, vala/valacallback.vala, vala/valaclass.vala,
vala/valaelementaccess.vala, vala/valafield.vala,
vala/valaformalparameter.vala, vala/valaforstatement.vala,
vala/valainitializerlist.vala, vala/valainterface.vala,
vala/valainvocationexpression.vala, vala/valainvokable.vala,
vala/valalambdaexpression.vala,
vala/valalocalvariabledeclaration.vala, vala/valamemberaccess.vala,
vala/valamethod.vala, vala/valanamespace.vala,
vala/valaobjectcreationexpression.vala, vala/valasignal.vala,
vala/valasourcefile.vala, vala/valastruct.vala,
vala/valaswitchsection.vala, vala/valaswitchstatement.vala,
vala/valatypereference.vala, vala/valavariabledeclarator.vala,
ccode/valaccodefragment.vala, vapi/glib-2.0.vala: always default type
arguments to strong
* vala/valacodevisitor.vala, vala/valacodegenerator.vala,
vala/valaexpression.vala, vala/valareturnstatement.vala: avoid
unnecessary ref/unref pair in return expression
* vala/valatypereference.vala: copy type arguments in copy method, too
* vala/valasemanticanalyzer.vala: set type arguments in type_reference
of ObjectCreationExpression
* vala/valadatatype.vala: fix warning
2006-09-02 Jürg Billeter <j@bitron.ch>
* vala/valacodegenerator.vala: special case construction of GLib.List
......
......@@ -42,7 +42,7 @@ public class Vala.CCodeFragment : CCodeNode {
*
* @return children list
*/
public ref List<CCodeNode> get_children () {
public ref List<weak CCodeNode> get_children () {
return children.copy ();
}
......
......@@ -2126,14 +2126,6 @@ field_declaration
vala_type_reference_set_takes_ownership ($5, TRUE);
}
vala_type_reference_set_is_ref ($5, FALSE);
GList *l;
for (l = vala_type_reference_get_type_arguments ($5); l != NULL; l = l->next) {
ValaTypeReference *type_arg = VALA_TYPE_REFERENCE (l->data);
if (!vala_type_reference_get_is_weak (type_arg)) {
vala_type_reference_set_takes_ownership (type_arg, TRUE);
}
}
ValaSourceReference *src = src_com(@5, $1);
$$ = vala_field_new (vala_variable_declarator_get_name ($6), $5, vala_variable_declarator_get_initializer ($6), src);
......@@ -2387,14 +2379,6 @@ property_declaration
if (!vala_type_reference_get_is_weak ($5)) {
vala_type_reference_set_takes_ownership ($5, TRUE);
}
GList *l;
for (l = vala_type_reference_get_type_arguments ($5); l != NULL; l = l->next) {
ValaTypeReference *type_arg = VALA_TYPE_REFERENCE (l->data);
if (!vala_type_reference_get_is_weak (type_arg)) {
vala_type_reference_set_takes_ownership (type_arg, TRUE);
}
}
ValaSourceReference *src = src_com(@5, $1);
$$ = vala_property_new ($6, $5, $8, $9, src);
......@@ -3017,6 +3001,12 @@ type_arguments
type_argument
: type
{
$$ = $1;
if (!vala_type_reference_get_is_weak ($$)) {
vala_type_reference_set_takes_ownership ($$, TRUE);
}
}
;
open_parens
......
......@@ -56,7 +56,7 @@ public class Vala.ArrayCreationExpression : Expression {
/**
* Get the sizes for all dimensions ascending from left to right.
*/
public ref List<Expression> get_sizes () {
public ref List<weak Expression> get_sizes () {
return sizes.copy ();
}
......
......@@ -58,7 +58,7 @@ public class Vala.Block : Statement {
*
* @return statement list
*/
public ref List<Statement> get_statements () {
public ref List<weak Statement> get_statements () {
return statement_list.copy ();
}
......@@ -76,7 +76,7 @@ public class Vala.Block : Statement {
*
* @return variable declarator list
*/
public ref List<VariableDeclarator> get_local_variables () {
public ref List<weak VariableDeclarator> get_local_variables () {
return local_variables.copy ();
}
......
......@@ -81,7 +81,7 @@ public class Vala.Callback : DataType {
*
* @return parameter list
*/
public ref List<FormalParameter> get_parameters () {
public ref List<weak FormalParameter> get_parameters () {
return parameters.copy ();
}
......
......@@ -104,7 +104,7 @@ public class Vala.Class : DataType {
*
* @return list of base types
*/
public ref List<TypeReference> get_base_types () {
public ref List<weak TypeReference> get_base_types () {
return base_types.copy ();
}
......@@ -144,7 +144,7 @@ public class Vala.Class : DataType {
*
* @return list of fields
*/
public ref List<Field> get_fields () {
public ref List<weak Field> get_fields () {
return fields.copy ();
}
......@@ -162,7 +162,7 @@ public class Vala.Class : DataType {
*
* @return list of methods
*/
public ref List<Method> get_methods () {
public ref List<weak Method> get_methods () {
return methods.copy ();
}
......@@ -188,7 +188,7 @@ public class Vala.Class : DataType {
*
* @return list of properties
*/
public ref List<Property> get_properties () {
public ref List<weak Property> get_properties () {
return properties.copy ();
}
......@@ -206,7 +206,7 @@ public class Vala.Class : DataType {
*
* @return list of signals
*/
public ref List<Signal> get_signals () {
public ref List<weak Signal> get_signals () {
return signals.copy ();
}
......
......@@ -53,7 +53,7 @@ public class Vala.CodeContext {
*
* @return list of source files
*/
public ref List<SourceFile> get_source_files () {
public ref List<weak SourceFile> get_source_files () {
return source_files.copy ();
}
......@@ -149,7 +149,7 @@ public class Vala.CodeContext {
/* no deep copy available yet
* var l = chain.copy ();
*/
ref List<ref SourceFile> l = null;
ref List<weak SourceFile> l = null;
foreach (SourceFile chain_file in chain) {
l.append (chain_file);
}
......@@ -194,7 +194,7 @@ public class Vala.CodeContext {
/* file is in an other cycle, merge the two cycles */
/* broken memory management cycles.remove (cycle_file.cycle); */
ref List<ref SourceFileCycle> newlist = null;
ref List<weak SourceFileCycle> newlist = null;
foreach (SourceFileCycle oldcycle in cycles) {
if (oldcycle != cycle_file.cycle) {
newlist.append (oldcycle);
......
......@@ -147,7 +147,7 @@ public class Vala.CodeGenerator : CodeVisitor {
header_begin.append (new CCodeIncludeDirective ("glib-object.h"));
source_include_directives.append (new CCodeIncludeDirective (source_file.get_cheader_filename (), true));
ref List<string> used_includes = null;
ref List<weak string> used_includes = null;
used_includes.append ("glib.h");
used_includes.append ("glib-object.h");
used_includes.append (source_file.get_cheader_filename ());
......@@ -1602,7 +1602,7 @@ public class Vala.CodeGenerator : CodeVisitor {
create_temp_decl (stmt, stmt.expression.temp_vars);
List<Statement> default_statements = null;
List<weak Statement> default_statements = null;
// generate nested if statements
ref CCodeStatement ctopstmt = null;
......@@ -1808,17 +1808,58 @@ public class Vala.CodeGenerator : CodeVisitor {
expr.temp_vars.append (return_expr_decl);
}
public override void visit_return_statement (ReturnStatement! stmt) {
public override void visit_begin_return_statement (ReturnStatement! stmt) {
if (stmt.return_expression != null) {
// avoid unnecessary ref/unref pair
if (stmt.return_expression.ref_missing &&
stmt.return_expression.symbol_reference != null &&
stmt.return_expression.symbol_reference.node is VariableDeclarator) {
var decl = (VariableDeclarator) stmt.return_expression.symbol_reference.node;
if (decl.type_reference.takes_ownership) {
/* return expression is local variable taking ownership and
* current method is transferring ownership */
stmt.return_expression.ref_sink = true;
// don't ref expression
stmt.return_expression.ref_missing = false;
}
}
}
}
public override void visit_end_return_statement (ReturnStatement! stmt) {
if (stmt.return_expression == null) {
stmt.ccodenode = new CCodeReturnStatement ();
create_local_free (stmt);
} else {
Symbol return_expression_symbol = null;
// avoid unnecessary ref/unref pair
if (stmt.return_expression.ref_sink &&
stmt.return_expression.symbol_reference != null &&
stmt.return_expression.symbol_reference.node is VariableDeclarator) {
var decl = (VariableDeclarator) stmt.return_expression.symbol_reference.node;
if (decl.type_reference.takes_ownership) {
/* return expression is local variable taking ownership and
* current method is transferring ownership */
// don't unref expression
return_expression_symbol = decl.symbol;
return_expression_symbol.active = false;
}
}
create_local_free_expr (stmt.return_expression);
stmt.ccodenode = new CCodeReturnStatement ((CCodeExpression) stmt.return_expression.ccodenode);
create_temp_decl (stmt, stmt.return_expression.temp_vars);
if (return_expression_symbol != null) {
return_expression_symbol.active = true;
}
}
}
......@@ -2097,7 +2138,7 @@ public class Vala.CodeGenerator : CodeVisitor {
var ccall = new CCodeFunctionCall ((CCodeExpression) expr.call.ccodenode);
Method m = null;
List<FormalParameter> params;
List<weak FormalParameter> params;
if (!(expr.call is MemberAccess)) {
expr.error = true;
......@@ -2239,7 +2280,7 @@ public class Vala.CodeGenerator : CodeVisitor {
public override void visit_element_access (ElementAccess! expr)
{
List<Expression> indices = expr.get_indices ();
List<weak Expression> indices = expr.get_indices ();
int rank = indices.length ();
if (rank == 1) {
......
......@@ -484,11 +484,19 @@ public abstract class Vala.CodeVisitor {
}
/**
* Visit operation called for return statements.
* Visit operation called at beginning of return statements.
*
* @param stmt a return statement
*/
public virtual void visit_return_statement (ReturnStatement! stmt) {
public virtual void visit_begin_return_statement (ReturnStatement! stmt) {
}
/**
* Visit operation called at end of return statements.
*
* @param stmt a return statement
*/
public virtual void visit_end_return_statement (ReturnStatement! stmt) {
}
/**
......
......@@ -212,19 +212,21 @@ public abstract class Vala.DataType : CodeNode {
* @return array type for this data type
*/
public Array! get_array (int rank) {
Array array_type = (Array)array_types.lookup (rank.to_string ());
Array array_type = (Array) array_types.lookup (rank.to_string ());
if (array_type == null) {
array_type = new Array (this, rank);
var new_array_type = new Array (this, rank);
/* create a new Symbol */
array_type.symbol = new Symbol (array_type);
this.symbol.parent_symbol.add (array_type.name, array_type.symbol);
new_array_type.symbol = new Symbol (new_array_type);
this.symbol.parent_symbol.add (new_array_type.name, new_array_type.symbol);
/* link the array type to the same source as the container type */
array_type.source_reference = this.source_reference;
new_array_type.source_reference = this.source_reference;
/* link the namespace */
array_type.@namespace = this.@namespace;
new_array_type.@namespace = this.@namespace;
array_types.insert (rank.to_string (), array_type);
array_types.insert (rank.to_string (), new_array_type);
array_type = new_array_type;
}
return array_type;
......
......@@ -40,7 +40,7 @@ public class Vala.ElementAccess : Expression {
indices.append (index);
}
public ref List<Expression> get_indices () {
public ref List<weak Expression> get_indices () {
return indices.copy ();
}
......
......@@ -61,6 +61,11 @@ public abstract class Vala.Expression : CodeNode {
*/
public bool ref_missing { get; set; }
/**
* Specifies that this expression successfully transfers ownership.
*/
public bool ref_sink { get; set; }
/**
* Contains all temporary variables this expression requires for
* execution.
......
......@@ -137,7 +137,7 @@ public class Vala.Field : CodeNode, Invokable {
}
}
public override ref List<FormalParameter> get_parameters () {
public override ref List<weak FormalParameter> get_parameters () {
if (!is_invokable ()) {
return null;
}
......
......@@ -89,7 +89,7 @@ public class Vala.FormalParameter : CodeNode, Invokable {
visitor.visit_formal_parameter (this);
}
public override ref List<FormalParameter> get_parameters () {
public override ref List<weak FormalParameter> get_parameters () {
if (!is_invokable ()) {
return null;
}
......
......@@ -77,7 +77,7 @@ public class Vala.ForStatement : Statement {
*
* @return initializer list
*/
public ref List<Expression> get_initializer () {
public ref List<weak Expression> get_initializer () {
return initializer.copy ();
}
......@@ -95,7 +95,7 @@ public class Vala.ForStatement : Statement {
*
* @return iterator
*/
public ref List<Expression> get_iterator () {
public ref List<weak Expression> get_iterator () {
return iterator.copy ();
}
......
......@@ -42,7 +42,7 @@ public class Vala.InitializerList : Expression {
*
* @return expression list
*/
public ref List<Expression> get_initializers () {
public ref List<weak Expression> get_initializers () {
return initializers.copy ();
}
......
......@@ -75,7 +75,7 @@ public class Vala.Interface : DataType {
*
* @return list of base types
*/
public ref List<TypeReference> get_base_types () {
public ref List<weak TypeReference> get_base_types () {
return base_types.copy ();
}
......@@ -93,7 +93,7 @@ public class Vala.Interface : DataType {
*
* @return list of methods
*/
public ref List<Method> get_methods () {
public ref List<weak Method> get_methods () {
return methods.copy ();
}
......@@ -111,7 +111,7 @@ public class Vala.Interface : DataType {
*
* @return list of properties
*/
public ref List<Property> get_properties () {
public ref List<weak Property> get_properties () {
return properties.copy ();
}
......@@ -129,7 +129,7 @@ public class Vala.Interface : DataType {
*
* @return list of signals
*/
public ref List<Signal> get_signals () {
public ref List<weak Signal> get_signals () {
return signals.copy ();
}
......
......@@ -70,7 +70,7 @@ public class Vala.InvocationExpression : Expression {
*
* @return argument list
*/
public ref List<Expression> get_argument_list () {
public ref List<weak Expression> get_argument_list () {
return argument_list.copy ();
}
......
......@@ -45,5 +45,5 @@ public interface Vala.Invokable {
*
* @return parameter list
*/
public abstract ref List<FormalParameter> get_parameters ();
public abstract ref List<weak FormalParameter> get_parameters ();
}
......@@ -84,7 +84,7 @@ public class Vala.LambdaExpression : Expression {
*
* @return parameter list
*/
public ref List<string> get_parameters () {
public ref List<weak string> get_parameters () {
return parameters.copy ();
}
......
......@@ -72,7 +72,7 @@ public class Vala.LocalVariableDeclaration : CodeNode {
*
* @return variable declarator list
*/
public ref List<VariableDeclarator> get_variable_declarators () {
public ref List<weak VariableDeclarator> get_variable_declarators () {
return variable_declarators.copy ();
}
......
......@@ -82,7 +82,7 @@ public class Vala.MemberAccess : Expression {
*
* @return type argument list
*/
public ref List<TypeReference> get_type_arguments () {
public ref List<weak TypeReference> get_type_arguments () {
return type_argument_list.copy ();
}
......
......@@ -96,7 +96,7 @@ public class Vala.MemoryManager : CodeVisitor {
visit_possibly_leaked_expression (stmt.expression);
}
public override void visit_return_statement (ReturnStatement! stmt) {
public override void visit_end_return_statement (ReturnStatement! stmt) {
if (stmt.return_expression != null) {
if (current_symbol.node is Method) {
var m = (Method) current_symbol.node;
......@@ -120,7 +120,7 @@ public class Vala.MemoryManager : CodeVisitor {
}
public override void visit_end_invocation_expression (InvocationExpression! expr) {
List<FormalParameter> params;
List<weak FormalParameter> params;
var msym = expr.call.symbol_reference;
if (msym.node is VariableDeclarator) {
......
......@@ -161,7 +161,7 @@ public class Vala.Method : CodeNode, Invokable {
parameters.append (param);
}
public override ref List<FormalParameter> get_parameters () {
public override ref List<weak FormalParameter> get_parameters () {
return parameters.copy ();
}
......
......@@ -123,7 +123,7 @@ public class Vala.Namespace : CodeNode {
*
* @return struct list
*/
public ref List<Struct> get_structs () {
public ref List<weak Struct> get_structs () {
return structs.copy ();
}
......@@ -132,7 +132,7 @@ public class Vala.Namespace : CodeNode {
*
* @return class list
*/
public ref List<Class> get_classes () {
public ref List<weak Class> get_classes () {
return classes.copy ();
}
......@@ -294,7 +294,7 @@ public class Vala.Namespace : CodeNode {
*
* @return list of C header filenames for this namespace
*/
public ref List<string> get_cheader_filenames () {
public ref List<weak string> get_cheader_filenames () {
if (cheader_filenames == null) {
cheader_filenames.append (source_reference.file.get_cinclude_filename ());
}
......
......@@ -72,7 +72,7 @@ public class Vala.ObjectCreationExpression : Expression {
*
* @return argument list
*/
public ref List<Expression> get_argument_list () {
public ref List<weak Expression> get_argument_list () {
return argument_list.copy ();
}
......
......@@ -56,13 +56,15 @@ public class Vala.ReturnStatement : Statement {
}
public override void accept (CodeVisitor! visitor) {
visitor.visit_begin_return_statement (this);
if (return_expression != null) {
return_expression.accept (visitor);
visitor.visit_end_full_expression (return_expression);
}
visitor.visit_return_statement (this);
visitor.visit_end_return_statement (this);
}
public override void replace (CodeNode! old_node, CodeNode! new_node) {
......
......@@ -507,7 +507,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
current_symbol.add (stmt.variable_name, stmt.variable_declarator.symbol);
}
public override void visit_return_statement (ReturnStatement! stmt) {
public override void visit_end_return_statement (ReturnStatement! stmt) {
if (current_return_type == null) {
Report.error (stmt.source_reference, "Return not allowed in this context");
return;
......@@ -562,7 +562,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
*/
public override void visit_end_array_creation_expression (ArrayCreationExpression! expr) {
int i;
List<Expression> size = expr.get_sizes ();
List<weak Expression> size = expr.get_sizes ();
/* check for errors in the size list */
if (size != null) {
......@@ -842,7 +842,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
return;
}
List<FormalParameter> params;
List<weak FormalParameter> params;
if (msym.node is Invokable) {
var m = (Invokable) msym.node;
......@@ -932,7 +932,7 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
var msym = expr.call.symbol_reference;
TypeReference ret_type;
List<FormalParameter> params;
List<weak FormalParameter> params;
if (msym.node is Invokable) {
var m = (Invokable) msym.node;
......@@ -998,6 +998,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
var constructor_node = expr.member_name.symbol_reference.node;
var type_node = expr.member_name.symbol_reference.node;
var type_args = expr.member_name.get_type_arguments ();
if (constructor_node is Method) {
type_node = constructor_node.symbol.parent_symbol.node;
......@@ -1009,6 +1011,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
}
expr.symbol_reference = constructor.symbol;
type_args = ((MemberAccess) expr.member_name.inner).get_type_arguments ();
}
if (type_node is Class || type_node is Struct) {
......@@ -1018,6 +1022,12 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
Report.error (expr.source_reference, "`%s' is not a class or struct".printf (type.symbol.get_full_name ()));
return;
}
expr.type_reference = new TypeReference ();
expr.type_reference.data_type = type;
foreach (TypeReference type_arg in type_args) {
expr.type_reference.add_type_argument (type_arg);
}
}
if (!type.is_reference_type ()) {
......@@ -1028,9 +1038,6 @@ public class Vala.SemanticAnalyzer : CodeVisitor {
current_source_file.add_symbol_dependency (type.symbol, SourceFileDependencyType.SOURCE);
expr.type_reference = new TypeReference ();
expr.type_reference.data_type = type;
expr.static_type = expr.type_reference.copy ();
expr.static_type.transfers_ownership = true;
......
......@@ -75,7 +75,7 @@ public class Vala.Signal : CodeNode, Invokable {
parameters.append (param);
}
public override ref List<FormalParameter> get_parameters () {
public override ref List<weak FormalParameter> get_parameters () {
return parameters.copy ();
}
......
......@@ -116,7 +116,7 @@ public class Vala.SourceFile {
*
* @return using directive list
*/
public ref List<NamespaceReference> get_using_directives () {
public ref List<weak NamespaceReference> get_using_directives () {
return using_directives.copy ();
}
......@@ -143,7 +143,7 @@ public class Vala.SourceFile {
*
* @return namespace list
*/
public ref List<Namespace> get_namespaces () {
public ref List<weak Namespace> get_namespaces () {
return namespaces.copy ();
}
......
......@@ -95,7 +95,7 @@ public class Vala.Struct : DataType {
*
* @return list of fields
*/
public ref List<Field> get_fields () {