Commit 150681f4 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

Add CCodeInvocationExpressionBinding and CCodeMemberAccessBinding classes

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

	* vala/valacodegenerator.vala, vala/valainvocationexpression.vala,
	  vala/valamemberaccess.vala, gobject/Makefile.am,
	  gobject/valaccodegenerator.vala,
	  gobject/valaccodeinvocationexpressionbinding.vala,
	  gobject/valaccodememberaccessbinding.vala:

	  Add CCodeInvocationExpressionBinding and CCodeMemberAccessBinding
	  classes

svn path=/trunk/; revision=1262
parent 61461e69
2008-04-19 Jürg Billeter <j@bitron.ch>
* vala/valacodegenerator.vala, vala/valainvocationexpression.vala,
vala/valamemberaccess.vala, gobject/Makefile.am,
gobject/valaccodegenerator.vala,
gobject/valaccodeinvocationexpressionbinding.vala,
gobject/valaccodememberaccessbinding.vala:
Add CCodeInvocationExpressionBinding and CCodeMemberAccessBinding
classes
2008-04-19 Jürg Billeter <j@bitron.ch>
* gobject/Makefile.am, gobject/valaccodecreationmethodbinding.vala,
......
......@@ -21,12 +21,12 @@ libvala_la_VALASOURCES = \
valaccodeelementaccessbinding.vala \
valaccodeexpressionbinding.vala \
valaccodegenerator.vala \
valaccodegeneratorinvocationexpression.vala \
valaccodegeneratormemberaccess.vala \
valaccodegeneratorsignal.vala \
valaccodegeneratorsourcefile.vala \
valaccodegeneratorstruct.vala \
valaccodeinterfacebinding.vala \
valaccodeinvocationexpressionbinding.vala \
valaccodememberaccessbinding.vala \
valaccodemethodbinding.vala \
valaccodetypesymbolbinding.vala \
valaclassregisterfunction.vala \
......
......@@ -115,7 +115,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
public Typesymbol map_type;
public Typesymbol connection_type;
Method substring_method;
public Method substring_method;
public bool in_plugin = false;
public string module_init_param_name;
......@@ -123,7 +123,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
public bool string_h_needed;
private bool requires_free_checked;
private bool requires_array_free;
private bool requires_array_move;
public bool requires_array_move;
private bool requires_strcmp0;
private Set<string> wrappers;
......@@ -1084,7 +1084,7 @@ public class Vala.CCodeGenerator : CodeGenerator {
temp_vars.clear ();
}
private string get_variable_cname (string name) {
public string get_variable_cname (string name) {
if (c_keywords.contains (name)) {
return name + "_";
} else {
......@@ -2387,7 +2387,15 @@ public class Vala.CCodeGenerator : CodeGenerator {
visit_expression (expr);
}
public override void visit_member_access (MemberAccess expr) {
code_binding (expr).emit ();
}
public override void visit_invocation_expression (InvocationExpression expr) {
code_binding (expr).emit ();
}
public string get_array_length_cname (string array_cname, int dim) {
return "%s_length%d".printf (array_cname, dim);
}
......@@ -3646,6 +3654,22 @@ public class Vala.CCodeGenerator : CodeGenerator {
return new CCodeExpressionStatement (ccheck);
}
public int get_param_pos (double param_pos, bool ellipsis = false) {
if (!ellipsis) {
if (param_pos >= 0) {
return (int) (param_pos * 1000);
} else {
return (int) ((100 + param_pos) * 1000);
}
} else {
if (param_pos >= 0) {
return (int) ((100 + param_pos) * 1000);
} else {
return (int) ((200 + param_pos) * 1000);
}
}
}
public override CodeBinding? create_namespace_binding (Namespace node) {
return null;
}
......@@ -3847,15 +3871,11 @@ public class Vala.CCodeGenerator : CodeGenerator {
}
public override CodeBinding? create_member_access_binding (MemberAccess node) {
return null;
}
public override CodeBinding? create_member_access_simple_binding (MemberAccess node) {
return null;
return new CCodeMemberAccessBinding (this, node);
}
public override CodeBinding? create_invocation_expression_binding (InvocationExpression node) {
return null;
return new CCodeInvocationExpressionBinding (this, node);
}
public override CodeBinding? create_element_access_binding (ElementAccess node) {
......@@ -3918,10 +3938,6 @@ public class Vala.CCodeGenerator : CodeGenerator {
return null;
}
public override CodeBinding? create_lambda_expression_with_statement_body_binding (LambdaExpression node) {
return null;
}
public override CodeBinding? create_assignment_binding (Assignment node) {
return new CCodeAssignmentBinding (this, node);
}
......
/* valaccodegeneratormemberaccess.vala
/* valaccodememberaccessbinding.vala
*
* Copyright (C) 2006-2008 Jürg Billeter, Raffaele Sandrini
*
......@@ -23,7 +23,14 @@
using GLib;
public class Vala.CCodeGenerator {
public class Vala.CCodeMemberAccessBinding : CCodeExpressionBinding {
public MemberAccess member_access { get; set; }
public CCodeMemberAccessBinding (CCodeGenerator codegen, MemberAccess member_access) {
this.member_access = member_access;
this.codegen = codegen;
}
private void process_cmember (MemberAccess expr, CCodeExpression? pub_inst, DataType? base_type) {
if (expr.symbol_reference is Method) {
var m = (Method) expr.symbol_reference;
......@@ -32,13 +39,13 @@ public class Vala.CCodeGenerator {
if (m.base_method != null) {
var base_class = (Class) m.base_method.parent_symbol;
var vcast = new CCodeFunctionCall (new CCodeIdentifier ("%s_CLASS".printf (base_class.get_upper_case_cname (null))));
vcast.add_argument (new CCodeIdentifier ("%s_parent_class".printf (current_class.get_lower_case_cname (null))));
vcast.add_argument (new CCodeIdentifier ("%s_parent_class".printf (codegen.current_class.get_lower_case_cname (null))));
expr.ccodenode = new CCodeMemberAccess.pointer (vcast, m.name);
return;
} else if (m.base_interface_method != null) {
var base_iface = (Interface) m.base_interface_method.parent_symbol;
string parent_iface_var = "%s_%s_parent_iface".printf (current_class.get_lower_case_cname (null), base_iface.get_lower_case_cname (null));
string parent_iface_var = "%s_%s_parent_iface".printf (codegen.current_class.get_lower_case_cname (null), base_iface.get_lower_case_cname (null));
expr.ccodenode = new CCodeMemberAccess.pointer (new CCodeIdentifier (parent_iface_var), m.name);
return;
......@@ -52,8 +59,8 @@ public class Vala.CCodeGenerator {
if (expr.inner != null && !expr.inner.is_pure ()) {
// instance expression has side-effects
// store in temp. variable
var temp_var = get_temp_variable (expr.inner.static_type);
temp_vars.insert (0, temp_var);
var temp_var = codegen.get_temp_variable (expr.inner.static_type);
codegen.temp_vars.insert (0, temp_var);
var ctemp = new CCodeIdentifier (temp_var.name);
inst = new CCodeAssignment (ctemp, pub_inst);
expr.inner.ccodenode = ctemp;
......@@ -71,15 +78,15 @@ public class Vala.CCodeGenerator {
expr.ccodenode = new CCodeIdentifier (m.get_cname ());
}
} else if (expr.symbol_reference is ArrayLengthField) {
expr.ccodenode = get_array_length_cexpression (expr.inner, 1);
expr.ccodenode = codegen.get_array_length_cexpression (expr.inner, 1);
} else if (expr.symbol_reference is Field) {
var f = (Field) expr.symbol_reference;
if (f.instance) {
var instance_expression_type = base_type;
var instance_target_type = get_data_type_for_symbol ((Typesymbol) f.parent_symbol);
CCodeExpression typed_inst = get_implicit_cast_expression (pub_inst, instance_expression_type, instance_target_type);
var instance_target_type = codegen.get_data_type_for_symbol ((Typesymbol) f.parent_symbol);
CCodeExpression typed_inst = codegen.get_implicit_cast_expression (pub_inst, instance_expression_type, instance_target_type);
bool is_gtypeinstance = (instance_target_type.data_type.is_subtype_of (gtypeinstance_type));
bool is_gtypeinstance = (instance_target_type.data_type.is_subtype_of (codegen.gtypeinstance_type));
CCodeExpression inst;
if (is_gtypeinstance && f.access == SymbolAccessibility.PRIVATE) {
......@@ -97,7 +104,7 @@ public class Vala.CCodeGenerator {
}
if (f.type_reference.type_parameter != null && expr.static_type.type_parameter == null) {
expr.ccodenode = convert_from_generic_pointer ((CCodeExpression) expr.ccodenode, expr.static_type);
expr.ccodenode = codegen.convert_from_generic_pointer ((CCodeExpression) expr.ccodenode, expr.static_type);
}
} else if (expr.symbol_reference is Constant) {
var c = (Constant) expr.symbol_reference;
......@@ -107,7 +114,7 @@ public class Vala.CCodeGenerator {
if (prop.get_accessor != null &&
prop.get_accessor.automatic_body &&
current_type_symbol == prop.parent_symbol) {
codegen.current_type_symbol == prop.parent_symbol) {
CCodeExpression inst;
inst = new CCodeMemberAccess.pointer (pub_inst, "priv");
expr.ccodenode = new CCodeMemberAccess.pointer (inst, prop.field.get_cname());
......@@ -122,8 +129,8 @@ public class Vala.CCodeGenerator {
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_get_%s".printf (base_property_type.get_lower_case_cname (null), base_property.name)));
var instance_expression_type = base_type;
var instance_target_type = get_data_type_for_symbol (base_property_type);
CCodeExpression typed_pub_inst = get_implicit_cast_expression (pub_inst, instance_expression_type, instance_target_type);
var instance_target_type = codegen.get_data_type_for_symbol (base_property_type);
CCodeExpression typed_pub_inst = codegen.get_implicit_cast_expression (pub_inst, instance_expression_type, instance_target_type);
ccall.add_argument (typed_pub_inst);
......@@ -131,9 +138,9 @@ public class Vala.CCodeGenerator {
// They are returned as out parameter.
if (base_property.type_reference.is_real_struct_type ()) {
var ccomma = new CCodeCommaExpression ();
var temp_var = get_temp_variable (base_property.type_reference);
var temp_var = codegen.get_temp_variable (base_property.type_reference);
var ctemp = new CCodeIdentifier (temp_var.name);
temp_vars.add (temp_var);
codegen.temp_vars.add (temp_var);
ccall.add_argument (new CCodeUnaryExpression(CCodeUnaryOperator.ADDRESS_OF, ctemp));
ccomma.append_expression (ccall);
ccomma.append_expression (ctemp);
......@@ -153,8 +160,8 @@ public class Vala.CCodeGenerator {
// we need a temporary variable to save the property value
var temp_var = get_temp_variable (expr.static_type);
temp_vars.insert (0, temp_var);
var temp_var = codegen.get_temp_variable (expr.static_type);
codegen.temp_vars.insert (0, temp_var);
var ctemp = new CCodeIdentifier (temp_var.name);
ccall.add_argument (new CCodeUnaryExpression (CCodeUnaryOperator.ADDRESS_OF, ctemp));
......@@ -172,7 +179,7 @@ public class Vala.CCodeGenerator {
expr.ccodenode = new CCodeConstant (ev.get_cname ());
} else if (expr.symbol_reference is LocalVariable) {
var local = (LocalVariable) expr.symbol_reference;
expr.ccodenode = new CCodeIdentifier (get_variable_cname (local.name));
expr.ccodenode = new CCodeIdentifier (codegen.get_variable_cname (local.name));
} else if (expr.symbol_reference is FormalParameter) {
var p = (FormalParameter) expr.symbol_reference;
if (p.name == "this") {
......@@ -186,10 +193,10 @@ public class Vala.CCodeGenerator {
// Property setters of non simple structs shall replace all occurences
// of the "value" formal parameter with a dereferencing version of that
// parameter.
if (current_property_accessor != null &&
current_property_accessor.writable &&
current_property_accessor.value_parameter == p &&
current_property_accessor.prop.type_reference.is_real_struct_type ()) {
if (codegen.current_property_accessor != null &&
codegen.current_property_accessor.writable &&
codegen.current_property_accessor.value_parameter == p &&
codegen.current_property_accessor.prop.type_reference.is_real_struct_type ()) {
expr.ccodenode = new CCodeIdentifier ("(*value)");
} else {
expr.ccodenode = new CCodeIdentifier (p.name);
......@@ -203,8 +210,8 @@ public class Vala.CCodeGenerator {
if (sig.has_emitter) {
var ccall = new CCodeFunctionCall (new CCodeIdentifier ("%s_%s".printf (cl.get_lower_case_cname (null), sig.name)));
var instance_expression_type = base_type;
var instance_target_type = get_data_type_for_symbol (cl);
CCodeExpression typed_pub_inst = get_implicit_cast_expression (pub_inst, instance_expression_type, instance_target_type);
var instance_target_type = codegen.get_data_type_for_symbol (cl);
CCodeExpression typed_pub_inst = codegen.get_implicit_cast_expression (pub_inst, instance_expression_type, instance_target_type);
ccall.add_argument (typed_pub_inst);
expr.ccodenode = ccall;
......@@ -223,21 +230,23 @@ public class Vala.CCodeGenerator {
}
}
public override void visit_member_access (MemberAccess expr) {
public override void emit () {
var expr = member_access;
CCodeExpression pub_inst = null;
DataType base_type = null;
if (expr.inner == null) {
pub_inst = new CCodeIdentifier ("self");
if (current_type_symbol != null) {
if (codegen.current_type_symbol != null) {
/* base type is available if this is a type method */
if (current_type_symbol is Class) {
base_type = new ClassType ((Class) current_type_symbol);
} else if (current_type_symbol is Interface) {
base_type = new InterfaceType ((Interface) current_type_symbol);
if (codegen.current_type_symbol is Class) {
base_type = new ClassType ((Class) codegen.current_type_symbol);
} else if (codegen.current_type_symbol is Interface) {
base_type = new InterfaceType ((Interface) codegen.current_type_symbol);
} else {
base_type = new ValueType (current_type_symbol);
base_type = new ValueType (codegen.current_type_symbol);
pub_inst = new CCodeIdentifier ("(*self)");
}
}
......@@ -251,7 +260,7 @@ public class Vala.CCodeGenerator {
process_cmember (expr, pub_inst, base_type);
visit_expression (expr);
codegen.visit_expression (expr);
}
}
......@@ -238,10 +238,6 @@ public class Vala.CodeGenerator : CodeVisitor {
return null;
}
public virtual CodeBinding? create_member_access_simple_binding (MemberAccess node) {
return null;
}
public virtual CodeBinding? create_invocation_expression_binding (InvocationExpression node) {
return null;
}
......@@ -306,10 +302,6 @@ public class Vala.CodeGenerator : CodeVisitor {
return null;
}
public virtual CodeBinding? create_lambda_expression_with_statement_body_binding (LambdaExpression node) {
return null;
}
public virtual CodeBinding? create_assignment_binding (Assignment node) {
return null;
}
......
......@@ -118,4 +118,8 @@ public class Vala.InvocationExpression : Expression {
public override bool is_pure () {
return false;
}
public override CodeBinding? create_code_binding (CodeGenerator codegen) {
return codegen.create_invocation_expression_binding (this);
}
}
/* valamemberaccess.vala
*
* Copyright (C) 2006-2007 Jürg Billeter
* Copyright (C) 2006-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
......@@ -150,4 +150,8 @@ public class Vala.MemberAccess : Expression {
}
}
}
public override CodeBinding? create_code_binding (CodeGenerator codegen) {
return codegen.create_member_access_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