support default values for properties, fixes bug 437434
authorJuerg Billeter <j@bitron.ch>
Tue, 5 Feb 2008 21:24:48 +0000 (21:24 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Tue, 5 Feb 2008 21:24:48 +0000 (21:24 +0000)
2008-02-05  Juerg Billeter  <j@bitron.ch>

* vala/parser.y, vala/valaclass.vala, vala/valaproperty.vala,
  gobject/valaccodegeneratorinterface.vala: support default values
  for properties, fixes bug 437434

svn path=/trunk/; revision=980

ChangeLog
gobject/valaccodegeneratorinterface.vala
vala/parser.y
vala/valaclass.vala
vala/valaproperty.vala

index c83cb1c..ba6a70c 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
 2008-02-05  Jürg Billeter  <j@bitron.ch>
 
+       * vala/parser.y, vala/valaclass.vala, vala/valaproperty.vala,
+         gobject/valaccodegeneratorinterface.vala: support default values
+         for properties, fixes bug 437434
+
+2008-02-05  Jürg Billeter  <j@bitron.ch>
+
        * vala/valastruct.vala, gobject/valaccodegeneratorsignal.vala:
          support non-simple struct as signal parameter type
 
index 87193f2..c8d2103 100644 (file)
@@ -109,35 +109,63 @@ public class Vala.CCodeGenerator {
                        cspec.call = new CCodeIdentifier ("g_param_spec_int");
                        cspec.add_argument (new CCodeConstant ("G_MININT"));
                        cspec.add_argument (new CCodeConstant ("G_MAXINT"));
-                       cspec.add_argument (new CCodeConstant ("0"));
+                       if (prop.default_expression != null) {
+                               cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
+                       } else {
+                               cspec.add_argument (new CCodeConstant ("0"));
+                       }
                } else if (prop.type_reference.data_type == uint_type.data_type) {
                        cspec.call = new CCodeIdentifier ("g_param_spec_uint");
                        cspec.add_argument (new CCodeConstant ("0"));
                        cspec.add_argument (new CCodeConstant ("G_MAXUINT"));
-                       cspec.add_argument (new CCodeConstant ("0U"));
+                       if (prop.default_expression != null) {
+                               cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
+                       } else {
+                               cspec.add_argument (new CCodeConstant ("0U"));
+                       }
                } else if (prop.type_reference.data_type == long_type.data_type) {
                        cspec.call = new CCodeIdentifier ("g_param_spec_long");
                        cspec.add_argument (new CCodeConstant ("G_MINLONG"));
                        cspec.add_argument (new CCodeConstant ("G_MAXLONG"));
-                       cspec.add_argument (new CCodeConstant ("0L"));
+                       if (prop.default_expression != null) {
+                               cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
+                       } else {
+                               cspec.add_argument (new CCodeConstant ("0L"));
+                       }
                } else if (prop.type_reference.data_type == ulong_type.data_type) {
                        cspec.call = new CCodeIdentifier ("g_param_spec_ulong");
                        cspec.add_argument (new CCodeConstant ("0"));
                        cspec.add_argument (new CCodeConstant ("G_MAXULONG"));
-                       cspec.add_argument (new CCodeConstant ("0UL"));
+                       if (prop.default_expression != null) {
+                               cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
+                       } else {
+                               cspec.add_argument (new CCodeConstant ("0UL"));
+                       }
                } else if (prop.type_reference.data_type == bool_type.data_type) {
                        cspec.call = new CCodeIdentifier ("g_param_spec_boolean");
-                       cspec.add_argument (new CCodeConstant ("FALSE"));
+                       if (prop.default_expression != null) {
+                               cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
+                       } else {
+                               cspec.add_argument (new CCodeConstant ("FALSE"));
+                       }
                } else if (prop.type_reference.data_type == float_type.data_type) {
                        cspec.call = new CCodeIdentifier ("g_param_spec_float");
                        cspec.add_argument (new CCodeConstant ("-G_MAXFLOAT"));
                        cspec.add_argument (new CCodeConstant ("G_MAXFLOAT"));
-                       cspec.add_argument (new CCodeConstant ("0.0F"));
+                       if (prop.default_expression != null) {
+                               cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
+                       } else {
+                               cspec.add_argument (new CCodeConstant ("0.0F"));
+                       }
                } else if (prop.type_reference.data_type == double_type.data_type) {
                        cspec.call = new CCodeIdentifier ("g_param_spec_double");
                        cspec.add_argument (new CCodeConstant ("-G_MAXDOUBLE"));
                        cspec.add_argument (new CCodeConstant ("G_MAXDOUBLE"));
-                       cspec.add_argument (new CCodeConstant ("0.0"));
+                       if (prop.default_expression != null) {
+                               cspec.add_argument ((CCodeExpression) prop.default_expression.ccodenode);
+                       } else {
+                               cspec.add_argument (new CCodeConstant ("0.0"));
+                       }
                } else {
                        cspec.call = new CCodeIdentifier ("g_param_spec_pointer");
                }
index 0b67676..952bae3 100644 (file)
@@ -342,6 +342,8 @@ static gboolean check_is_struct (ValaSymbol *symbol, ValaSourceReference *src);
 %type <property_accessor> get_accessor_declaration
 %type <property_accessor> opt_set_accessor_declaration
 %type <property_accessor> set_accessor_declaration
+%type <expression> opt_default_value
+%type <expression> default_value
 %type <constant> constant_declaration
 %type <field> field_declaration
 %type <list> variable_declarators
@@ -3331,7 +3333,7 @@ ensures_declaration
        ;
 
 property_declaration
-       : comment opt_attributes opt_access_modifier opt_modifiers type identifier OPEN_BRACE get_accessor_declaration opt_set_accessor_declaration CLOSE_BRACE
+       : comment opt_attributes opt_access_modifier opt_modifiers type identifier OPEN_BRACE get_accessor_declaration opt_set_accessor_declaration opt_default_value CLOSE_BRACE
          {
                ValaSourceReference *src;
 
@@ -3356,6 +3358,11 @@ property_declaration
                        g_object_unref ($9);
                }
 
+               if ($10 != NULL) {
+                       vala_property_set_default_expression ($$, $10);
+                       g_object_unref ($10);
+               }
+
                if (($4 & VALA_MODIFIER_ABSTRACT) == VALA_MODIFIER_ABSTRACT) {
                        vala_property_set_is_abstract ($$, TRUE);
                }
@@ -3369,7 +3376,7 @@ property_declaration
                        vala_property_set_instance ($$, FALSE);
                }
          }
-       | comment opt_attributes opt_access_modifier opt_modifiers type identifier OPEN_BRACE set_accessor_declaration opt_get_accessor_declaration CLOSE_BRACE
+       | comment opt_attributes opt_access_modifier opt_modifiers type identifier OPEN_BRACE set_accessor_declaration opt_get_accessor_declaration opt_default_value CLOSE_BRACE
          {
                ValaSourceReference *src;
 
@@ -3394,6 +3401,11 @@ property_declaration
                        g_object_unref ($9);
                }
 
+               if ($10 != NULL) {
+                       vala_property_set_default_expression ($$, $10);
+                       g_object_unref ($10);
+               }
+
                if (($4 & VALA_MODIFIER_ABSTRACT) == VALA_MODIFIER_ABSTRACT) {
                        vala_property_set_is_abstract ($$, TRUE);
                }
@@ -3506,6 +3518,21 @@ set_accessor_declaration
          }
        ;
 
+opt_default_value
+       : /* empty */
+         {
+               $$ = NULL;
+         }
+       | default_value
+       ;
+
+default_value
+       : DEFAULT OPEN_PARENS expression CLOSE_PARENS SEMICOLON
+         {
+               $$ = $3;
+         }
+       ;
+
 signal_declaration
        : comment opt_attributes opt_access_modifier SIGNAL type identifier OPEN_PARENS opt_formal_parameter_list CLOSE_PARENS SEMICOLON
          {
index f42ff40..8a186ac 100644 (file)
@@ -254,7 +254,7 @@ public class Vala.Class : Typesymbol {
                        if (empty_get && empty_set) {
                                /* automatic property accessor body generation */
                                var field_type = prop.type_reference.copy ();
-                               var f = new Field ("_%s".printf (prop.name), field_type, null, prop.source_reference);
+                               var f = new Field ("_%s".printf (prop.name), field_type, prop.default_expression, prop.source_reference);
                                f.access = SymbolAccessibility.PRIVATE;
                                add_field (f);
                        }
index 9020a85..dbf0079 100644 (file)
@@ -111,6 +111,11 @@ public class Vala.Property : Member, Lockable {
        public Property base_interface_property { get; set; }
 
        /**
+        * Specifies the default value of this property.
+        */
+       public Expression default_expression { get; set; }
+
+       /**
         * Nickname of this property.
         */
        public string nick {
@@ -177,6 +182,10 @@ public class Vala.Property : Member, Lockable {
                if (set_accessor != null) {
                        set_accessor.accept (visitor);
                }
+
+               if (default_expression != null) {
+                       default_expression.accept (visitor);
+               }
        }
 
        /**