From 46b759a5f2d3c5096e778bd250e3233e22c29af6 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=BCrg=20Billeter?= Date: Wed, 25 Oct 2006 20:12:20 +0000 Subject: [PATCH] start to use const specifier for strings to reduce number of warnings MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit 2006-10-25 Jürg Billeter * 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 --- vala/ChangeLog | 9 +++++++++ vala/vala/valaarray.vala | 2 +- vala/vala/valacallback.vala | 2 +- vala/vala/valaclass.vala | 2 +- vala/vala/valacodegenerator.vala | 13 +++++++------ vala/vala/valadatatype.vala | 2 +- vala/vala/valaenum.vala | 2 +- vala/vala/valaflags.vala | 2 +- vala/vala/valainterface.vala | 2 +- vala/vala/valastruct.vala | 19 ++++++++++++++++++- vala/vala/valatypereference.vala | 4 ++-- vala/vapi/glib-2.0.vala | 2 +- 12 files changed, 44 insertions(+), 17 deletions(-) diff --git a/vala/ChangeLog b/vala/ChangeLog index 5378dbd..befded3 100644 --- a/vala/ChangeLog +++ b/vala/ChangeLog @@ -1,5 +1,14 @@ 2006-10-25 Jürg Billeter + * 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 + * vala/parser.y, vala/valacodevisitor.vala, vala/valasemanticanalyzer.vala, vala/valacodegenerator.vala, vala/valabaseaccess.vala: support base access diff --git a/vala/vala/valaarray.vala b/vala/vala/valaarray.vala index 63c1d39..7becec2 100644 --- a/vala/vala/valaarray.vala +++ b/vala/vala/valaarray.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 ()); diff --git a/vala/vala/valacallback.vala b/vala/vala/valacallback.vala index 0e8de0b..f73579d 100644 --- a/vala/vala/valacallback.vala +++ b/vala/vala/valacallback.vala @@ -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); } diff --git a/vala/vala/valaclass.vala b/vala/vala/valaclass.vala index 6d7d26a..25724c0 100644 --- a/vala/vala/valaclass.vala +++ b/vala/vala/valaclass.vala @@ -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); } diff --git a/vala/vala/valacodegenerator.vala b/vala/vala/valacodegenerator.vala index c856b11..e34865f 100644 --- a/vala/vala/valacodegenerator.vala +++ b/vala/vala/valacodegenerator.vala @@ -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 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; } diff --git a/vala/vala/valadatatype.vala b/vala/vala/valadatatype.vala index 8bc155f..56575c9 100644 --- a/vala/vala/valadatatype.vala +++ b/vala/vala/valadatatype.vala @@ -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. diff --git a/vala/vala/valaenum.vala b/vala/vala/valaenum.vala index b0be4b4..36b093b 100644 --- a/vala/vala/valaenum.vala +++ b/vala/vala/valaenum.vala @@ -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); } diff --git a/vala/vala/valaflags.vala b/vala/vala/valaflags.vala index 2d607be..940e345 100644 --- a/vala/vala/valaflags.vala +++ b/vala/vala/valaflags.vala @@ -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); } diff --git a/vala/vala/valainterface.vala b/vala/vala/valainterface.vala index 35234c3..661bfb7 100644 --- a/vala/vala/valainterface.vala +++ b/vala/vala/valainterface.vala @@ -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); } diff --git a/vala/vala/valastruct.vala b/vala/vala/valastruct.vala index 370980b..f0126d9 100644 --- a/vala/vala/valastruct.vala +++ b/vala/vala/valastruct.vala @@ -34,6 +34,7 @@ public class Vala.Struct : DataType { private List 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) { diff --git a/vala/vala/valatypereference.vala b/vala/vala/valatypereference.vala index b49de90..f2dca2f 100644 --- a/vala/vala/valatypereference.vala +++ b/vala/vala/valatypereference.vala @@ -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 { diff --git a/vala/vapi/glib-2.0.vala b/vala/vapi/glib-2.0.vala index a3b358b..632114b 100644 --- a/vala/vapi/glib-2.0.vala +++ b/vala/vapi/glib-2.0.vala @@ -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); -- 2.7.4