Commit 61461e69 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

Add CCodeCreationMethodBinding and CCodeMethodBinding classes

2008-04-19  Juerg Billeter  <j@bitron.ch>

	* gobject/Makefile.am, gobject/valaccodecreationmethodbinding.vala,
	  gobject/valaccodegenerator.vala, gobject/valaccodemethodbinding.vala,
	  vala/valacreationmethod.vala:

	  Add CCodeCreationMethodBinding and CCodeMethodBinding classes

svn path=/trunk/; revision=1261
parent 63d726f4
2008-04-19 Jürg Billeter <j@bitron.ch>
* gobject/Makefile.am, gobject/valaccodecreationmethodbinding.vala,
gobject/valaccodegenerator.vala, gobject/valaccodemethodbinding.vala,
vala/valacreationmethod.vala:
Add CCodeCreationMethodBinding and CCodeMethodBinding classes
2008-04-19 Jürg Billeter <j@bitron.ch>
* vala/valaclass.vala, vala/valainterface.vala, gobject/Makefile.am,
......
......@@ -17,12 +17,12 @@ libvala_la_VALASOURCES = \
valaccodebinding.vala \
valaccodeclassbinding.vala \
valaccodecompiler.vala \
valaccodecreationmethodbinding.vala \
valaccodeelementaccessbinding.vala \
valaccodeexpressionbinding.vala \
valaccodegenerator.vala \
valaccodegeneratorinvocationexpression.vala \
valaccodegeneratormemberaccess.vala \
valaccodegeneratormethod.vala \
valaccodegeneratorsignal.vala \
valaccodegeneratorsourcefile.vala \
valaccodegeneratorstruct.vala \
......
/* valaccodecreationmethodbinding.vala
*
* Copyright (C) 2007-2008 Jürg Billeter
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
* Author:
* Jürg Billeter <j@bitron.ch>
*/
using GLib;
using Gee;
/**
* The link between a creation method and generated code.
*/
public class Vala.CCodeCreationMethodBinding : CCodeMethodBinding {
public CreationMethod creation_method { get; set; }
public CCodeCreationMethodBinding (CCodeGenerator codegen, CreationMethod creation_method) {
this.creation_method = creation_method;
this.method = creation_method;
this.codegen = codegen;
}
public override void emit () {
var m = creation_method;
if (m.body != null && codegen.current_type_symbol is Class && codegen.current_class.is_subtype_of (codegen.gobject_type)) {
int n_params = 0;
foreach (Statement stmt in m.body.get_statements ()) {
if (!(stmt is ExpressionStatement) || ((ExpressionStatement) stmt).assigned_property () == null) {
m.error = true;
Report.error (stmt.source_reference, "class creation methods only allow property assignment statements");
return;
}
if (((ExpressionStatement) stmt).assigned_property ().set_accessor.construction) {
n_params++;
}
}
m.n_construction_params = n_params;
}
base.emit ();
}
}
......@@ -28,7 +28,7 @@ using Gee;
* Code visitor generating C Code.
*/
public class Vala.CCodeGenerator : CodeGenerator {
private CodeContext context;
public CodeContext context;
public Symbol root_symbol;
public Symbol current_symbol;
......@@ -77,12 +77,12 @@ public class Vala.CCodeGenerator : CodeGenerator {
/* (constant) hash table with all C keywords */
public Gee.Set<string> c_keywords;
private int next_temp_var_id = 0;
public int next_temp_var_id = 0;
private int current_try_id = 0;
private int next_try_id = 0;
public bool in_creation_method = false;
private bool in_constructor = false;
private bool current_method_inner_error = false;
public bool current_method_inner_error = false;
public DataType bool_type;
public DataType char_type;
......@@ -120,7 +120,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
public bool in_plugin = false;
public string module_init_param_name;
private bool string_h_needed;
public bool string_h_needed;
private bool requires_free_checked;
private bool requires_array_free;
private bool requires_array_move;
......@@ -693,6 +693,14 @@ public class Vala.CCodeGenerator : CodeGenerator {
return (null != cparenthesized && is_pure_ccode_expression (cparenthesized.inner));
}
public override void visit_method (Method m) {
code_binding (m).emit ();
}
public override void visit_creation_method (CreationMethod m) {
code_binding (m).emit ();
}
public override void visit_formal_parameter (FormalParameter p) {
p.accept_children (this);
......@@ -2380,7 +2388,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
visit_expression (expr);
}
private string get_array_length_cname (string array_cname, int dim) {
public string get_array_length_cname (string array_cname, int dim) {
return "%s_length%d".printf (array_cname, dim);
}
......@@ -2518,7 +2526,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
}
}
private string get_delegate_target_cname (string delegate_cname) {
public string get_delegate_target_cname (string delegate_cname) {
return "%s_target".printf (delegate_cname);
}
......@@ -3573,6 +3581,71 @@ public class Vala.CCodeGenerator : CodeGenerator {
return type;
}
public CCodeExpression? default_value_for_type (DataType type, bool initializer_expression) {
if ((type.data_type != null && type.data_type.is_reference_type ()) || type is PointerType || type is ArrayType) {
return new CCodeConstant ("NULL");
} else if (type.data_type != null && type.data_type.get_default_value () != null) {
return new CCodeConstant (type.data_type.get_default_value ());
} else if (type.data_type is Struct && initializer_expression) {
// 0-initialize struct with struct initializer { 0 }
// only allowed as initializer expression in C
var clist = new CCodeInitializerList ();
clist.append (new CCodeConstant ("0"));
return clist;
} else if (type.type_parameter != null) {
return new CCodeConstant ("NULL");
} else if (type is ErrorType) {
return new CCodeConstant ("NULL");
}
return null;
}
private CCodeStatement create_property_type_check_statement (Property prop, bool check_return_type, Typesymbol t, bool non_null, string var_name) {
if (check_return_type) {
return create_type_check_statement (prop, prop.type_reference, t, non_null, var_name);
} else {
return create_type_check_statement (prop, new VoidType (), t, non_null, var_name);
}
}
public CCodeStatement? create_type_check_statement (CodeNode method_node, DataType ret_type, Typesymbol t, bool non_null, string var_name) {
var ccheck = new CCodeFunctionCall ();
if ((t is Class && ((Class) t).is_subtype_of (gobject_type)) || (t is Interface && !((Interface) t).declaration_only)) {
var ctype_check = new CCodeFunctionCall (new CCodeIdentifier (t.get_upper_case_cname ("IS_")));
ctype_check.add_argument (new CCodeIdentifier (var_name));
CCodeExpression cexpr = ctype_check;
if (!non_null) {
var cnull = new CCodeBinaryExpression (CCodeBinaryOperator.EQUALITY, new CCodeIdentifier (var_name), new CCodeConstant ("NULL"));
cexpr = new CCodeBinaryExpression (CCodeBinaryOperator.OR, cnull, ctype_check);
}
ccheck.add_argument (cexpr);
} else if (!non_null) {
return null;
} else {
var cnonnull = new CCodeBinaryExpression (CCodeBinaryOperator.INEQUALITY, new CCodeIdentifier (var_name), new CCodeConstant ("NULL"));
ccheck.add_argument (cnonnull);
}
if (ret_type is VoidType) {
/* void function */
ccheck.call = new CCodeIdentifier ("g_return_if_fail");
} else {
ccheck.call = new CCodeIdentifier ("g_return_val_if_fail");
var cdefault = default_value_for_type (ret_type, false);
if (cdefault != null) {
ccheck.add_argument (cdefault);
} else {
return new CCodeExpressionStatement (new CCodeConstant ("0"));
}
}
return new CCodeExpressionStatement (ccheck);
}
public override CodeBinding? create_namespace_binding (Namespace node) {
return null;
}
......@@ -3622,7 +3695,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
}
public override CodeBinding? create_creation_method_binding (CreationMethod node) {
return null;
return new CCodeCreationMethodBinding (this, node);
}
public override CodeBinding? create_formal_parameter_binding (FormalParameter node) {
......
This diff is collapsed.
This diff is collapsed.
......@@ -83,4 +83,8 @@ public class Vala.CreationMethod : Method {
return "%s%s_%s".printf (parent.get_lower_case_cprefix (), infix, name.offset (".new.".len ()));
}
}
public override CodeBinding? create_code_binding (CodeGenerator codegen) {
return codegen.create_creation_method_binding (this);
}
}
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