start to use const specifier for strings to reduce number of warnings
authorJürg Billeter <j@bitron.ch>
Wed, 25 Oct 2006 20:12:20 +0000 (20:12 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Wed, 25 Oct 2006 20:12:20 +0000 (20:12 +0000)
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

12 files changed:
vala/ChangeLog
vala/vala/valaarray.vala
vala/vala/valacallback.vala
vala/vala/valaclass.vala
vala/vala/valacodegenerator.vala
vala/vala/valadatatype.vala
vala/vala/valaenum.vala
vala/vala/valaflags.vala
vala/vala/valainterface.vala
vala/vala/valastruct.vala
vala/vala/valatypereference.vala
vala/vapi/glib-2.0.vala

index 5378dbd..befded3 100644 (file)
@@ -1,5 +1,14 @@
 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,
          vala/valasemanticanalyzer.vala, vala/valacodegenerator.vala,
          vala/valabaseaccess.vala: support base access
index 63c1d39..7becec2 100644 (file)
@@ -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 ());
index 0e8de0b..f73579d 100644 (file)
@@ -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);
                }
index 6d7d26a..25724c0 100644 (file)
@@ -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);
                }
index c856b11..e34865f 100644 (file)
@@ -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;
                }
index 8bc155f..56575c9 100644 (file)
@@ -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.
index b0be4b4..36b093b 100644 (file)
@@ -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);
                }
index 2d607be..940e345 100644 (file)
@@ -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);
                }
index 35234c3..661bfb7 100644 (file)
@@ -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);
                }
index 370980b..f0126d9 100644 (file)
@@ -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) {
index b49de90..f2dca2f 100644 (file)
@@ -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 {
index a3b358b..632114b 100644 (file)
@@ -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);