Commit 46b759a5 authored by Jürg Billeter's avatar Jürg Billeter Committed by Jürg Billeter

start to use const specifier for strings to reduce number of warnings

2006-10-25  Jürg Billeter  <j@bitron.ch>

	* vala/valacodegenerator.vala, vala/valaarray.vala,
	  vala/valacallback.vala, vala/valaclass.vala, vala/valadatatype.vala,
	  vala/valaenum.vala, vala/valaflags.vala, vala/valainterface.vala,
	  vala/valastruct.vala, vala/valatypereference.vala, vapi/glib-2.0.vala:
	  start to use const specifier for strings to reduce number of warnings
	  during C compilation

svn path=/trunk/; revision=151
parent 1a6a0b20
2006-10-25 Jürg Billeter <j@bitron.ch>
* vala/valacodegenerator.vala, vala/valaarray.vala,
vala/valacallback.vala, vala/valaclass.vala, vala/valadatatype.vala,
vala/valaenum.vala, vala/valaflags.vala, vala/valainterface.vala,
vala/valastruct.vala, vala/valatypereference.vala, vapi/glib-2.0.vala:
start to use const specifier for strings to reduce number of warnings
during C compilation
2006-10-25 Jürg Billeter <j@bitron.ch>
* vala/parser.y, vala/valacodevisitor.vala,
......
......@@ -74,7 +74,7 @@ public class Vala.Array : DataType {
*
* @return the name to be used in C code
*/
public override string get_cname () {
public override string get_cname (bool const_type = false) {
if (cname == null) {
if (element_type.is_reference_type ()) {
cname = "%s*".printf (element_type.get_cname ());
......
......@@ -137,7 +137,7 @@ public class Vala.Callback : DataType {
visitor.visit_end_callback (this);
}
public override string get_cname () {
public override string get_cname (bool const_type = false) {
if (cname == null) {
cname = "%s%s".printf (@namespace.get_cprefix (), name);
}
......
......@@ -252,7 +252,7 @@ public class Vala.Class : DataType {
visitor.visit_end_class (this);
}
public override string get_cname () {
public override string get_cname (bool const_type = false) {
if (cname == null) {
cname = "%s%s".printf (@namespace.get_cprefix (), name);
}
......
......@@ -1317,7 +1317,7 @@ public class Vala.CodeGenerator : CodeVisitor {
var cparam = new CCodeFormalParameter ("self", this_type.get_cname ());
function.add_parameter (cparam);
if (acc.writable || acc.construction) {
function.add_parameter (new CCodeFormalParameter ("value", prop.type_reference.get_cname ()));
function.add_parameter (new CCodeFormalParameter ("value", prop.type_reference.get_cname (false, true)));
}
header_type_member_declaration.append (function.copy ());
......@@ -1757,11 +1757,12 @@ public class Vala.CodeGenerator : CodeVisitor {
}
}
private ref VariableDeclarator get_temp_variable_declarator (TypeReference! type) {
private ref VariableDeclarator get_temp_variable_declarator (TypeReference! type, bool takes_ownership = true) {
var decl = new VariableDeclarator ("__temp%d".printf (next_temp_var_id));
decl.type_reference = type.copy ();
decl.type_reference.reference_to_value_type = false;
decl.type_reference.is_out = false;
decl.type_reference.takes_ownership = takes_ownership;
next_temp_var_id++;
......@@ -1877,7 +1878,7 @@ public class Vala.CodeGenerator : CodeVisitor {
private void append_temp_decl (CCodeFragment! cfrag, List<VariableDeclarator> temp_vars) {
foreach (VariableDeclarator decl in temp_vars) {
var cdecl = new CCodeDeclaration (decl.type_reference.get_cname (true));
var cdecl = new CCodeDeclaration (decl.type_reference.get_cname (true, !decl.type_reference.takes_ownership));
var vardecl = new CCodeVariableDeclarator (decl.name);
cdecl.add_declarator (vardecl);
......@@ -2827,7 +2828,7 @@ public class Vala.CodeGenerator : CodeVisitor {
}
private ref CCodeExpression get_ref_expression (Expression! expr) {
/* (temp = expr, temp == NULL ? temp : ref (temp))
/* (temp = expr, temp == NULL ? NULL : ref (temp))
*
* can be simplified to
* ref (expr)
......@@ -2855,7 +2856,7 @@ public class Vala.CodeGenerator : CodeVisitor {
return ccall;
} else {
var decl = get_temp_variable_declarator (expr.static_type);
var decl = get_temp_variable_declarator (expr.static_type, false);
temp_vars.prepend (decl);
var ctemp = new CCodeIdentifier (decl.name);
......@@ -2866,7 +2867,7 @@ public class Vala.CodeGenerator : CodeVisitor {
var ccomma = new CCodeCommaExpression ();
ccomma.append_expression (new CCodeAssignment (ctemp, (CCodeExpression) expr.ccodenode));
ccomma.append_expression (new CCodeConditionalExpression (cisnull, ctemp, ccall));
ccomma.append_expression (new CCodeConditionalExpression (cisnull, new CCodeConstant ("NULL"), ccall));
return ccomma;
}
......
......@@ -56,7 +56,7 @@ public abstract class Vala.DataType : CodeNode {
*
* @return the name to be used in C code
*/
public abstract string get_cname ();
public abstract string get_cname (bool const_type = false);
/**
* Checks whether this data type has value or reference type semantics.
......
......@@ -61,7 +61,7 @@ public class Vala.Enum : DataType {
visitor.visit_end_enum (this);
}
public override string get_cname () {
public override string get_cname (bool const_type = false) {
if (cname == null) {
cname = "%s%s".printf (@namespace.get_cprefix (), name);
}
......
......@@ -60,7 +60,7 @@ public class Vala.Flags : DataType {
visitor.visit_end_flags (this);
}
public override string get_cname () {
public override string get_cname (bool const_type = false) {
if (cname == null) {
cname = "%s%s".printf (@namespace.get_cprefix (), name);
}
......
......@@ -133,7 +133,7 @@ public class Vala.Interface : DataType {
return signals.copy ();
}
public override string get_cname () {
public override string get_cname (bool const_type = false) {
if (cname == null) {
cname = "%s%s".printf (@namespace.get_cprefix (), name);
}
......
......@@ -34,6 +34,7 @@ public class Vala.Struct : DataType {
private List<TypeReference> base_types;
private string cname;
private string const_cname;
private string dup_function;
private string free_function;
private string type_id;
......@@ -143,7 +144,11 @@ public class Vala.Struct : DataType {
visitor.visit_end_struct (this);
}
public override string get_cname () {
public override string get_cname (bool const_type = false) {
if (const_type && const_cname != null) {
return const_cname;
}
if (cname == null) {
cname = "%s%s".printf (@namespace.get_cprefix (), name);
}
......@@ -154,6 +159,10 @@ public class Vala.Struct : DataType {
this.cname = cname;
}
private void set_const_cname (string! cname) {
this.const_cname = cname;
}
public override ref string get_lower_case_cprefix () {
if (lower_case_cprefix == null) {
lower_case_cprefix = "%s_".printf (get_lower_case_cname (null));
......@@ -233,6 +242,14 @@ public class Vala.Struct : DataType {
set_cname (((StringLiteral) lit).eval ());
}
}
} else if (arg.name == "const_cname") {
/* this will already be checked during semantic analysis */
if (arg.argument is LiteralExpression) {
var lit = ((LiteralExpression) arg.argument).literal;
if (lit is StringLiteral) {
set_const_cname (((StringLiteral) lit).eval ());
}
}
} else if (arg.name == "cprefix") {
/* this will already be checked during semantic analysis */
if (arg.argument is LiteralExpression) {
......
......@@ -188,7 +188,7 @@ public class Vala.TypeReference : CodeNode {
*
* @return the type string to be used in C code
*/
public ref string get_cname (bool var_type = false) {
public ref string get_cname (bool var_type = false, bool const_type = false) {
if (data_type == null && type_parameter == null) {
if (var_type) {
return "gpointer";
......@@ -207,7 +207,7 @@ public class Vala.TypeReference : CodeNode {
ptr = "**";
}
if (data_type != null) {
return data_type.get_cname ().concat (ptr, arr, null);
return data_type.get_cname (const_type).concat (ptr, arr, null);
} else if (type_parameter != null) {
return "gpointer".concat (ptr, arr, null);
} else {
......
......@@ -164,7 +164,7 @@ public struct unichar {
}
[ReferenceType (dup_function = "g_strdup", free_function = "g_free", type_id = "G_TYPE_STRING")]
[CCode (cname = "char", cheader_filename = "stdlib.h,string.h,glib.h", type_id = "G_TYPE_STRING", marshaller_type_name = "STRING", get_value_function = "g_value_get_string", set_value_function = "g_value_set_string")]
[CCode (cname = "char", const_cname = "const char", cheader_filename = "stdlib.h,string.h,glib.h", type_id = "G_TYPE_STRING", marshaller_type_name = "STRING", get_value_function = "g_value_get_string", set_value_function = "g_value_set_string")]
public struct string {
[CCode (cname = "g_strstr")]
public string str (string! needle);
......
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