Add support for [CCode (cname = "foo")] to property accessors, based on
authorJürg Billeter <j@bitron.ch>
Sat, 5 Jul 2008 14:21:12 +0000 (14:21 +0000)
committerJürg Billeter <juergbi@src.gnome.org>
Sat, 5 Jul 2008 14:21:12 +0000 (14:21 +0000)
2008-07-05  Jürg Billeter  <j@bitron.ch>

* vala/valaattributeprocessor.vala:
* vala/valaparser.vala:
* vala/valapropertyaccessor.vala:
* gobject/valaccodegenerator.vala:
* gobject/valaccodememberaccessbinding.vala:

Add support for [CCode (cname = "foo")] to property accessors,
based on patch by Jared Moore

svn path=/trunk/; revision=1677

ChangeLog
gobject/valaccodegenerator.vala
gobject/valaccodememberaccessbinding.vala
vala/valaattributeprocessor.vala
vala/valaparser.vala
vala/valapropertyaccessor.vala

index 34e0270..87472f9 100644 (file)
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2008-07-05  Jürg Billeter  <j@bitron.ch>
+
+       * vala/valaattributeprocessor.vala:
+       * vala/valaparser.vala:
+       * vala/valapropertyaccessor.vala:
+       * gobject/valaccodegenerator.vala:
+       * gobject/valaccodememberaccessbinding.vala:
+
+       Add support for [CCode (cname = "foo")] to property accessors,
+       based on patch by Jared Moore
+
 2008-07-05  Jared Moore  <jaredm@svn.gnome.org>
 
        * vala/valasemanticanalyzer.vala:
index 839450b..814074b 100644 (file)
@@ -810,9 +810,9 @@ public class Vala.CCodeGenerator : CodeGenerator {
 
                if (prop.is_abstract || prop.is_virtual) {
                        if (acc.readable) {
-                               function = new CCodeFunction ("%s_get_%s".printf (t.get_lower_case_cname (null), prop.name), prop.property_type.get_cname ());
+                               function = new CCodeFunction (acc.get_cname (), prop.property_type.get_cname ());
                        } else {
-                               function = new CCodeFunction ("%s_set_%s".printf (t.get_lower_case_cname (null), prop.name), "void");
+                               function = new CCodeFunction (acc.get_cname (), "void");
                        }
                        function.add_parameter (cselfparam);
                        if (acc.writable || acc.construction) {
@@ -885,20 +885,28 @@ public class Vala.CCodeGenerator : CodeGenerator {
                if (!prop.is_abstract) {
                        bool is_virtual = prop.base_property != null || prop.base_interface_property != null;
 
-                       string prefix = t.get_lower_case_cname (null);
+                       string cname;
                        if (is_virtual) {
-                               prefix += "_real";
+                               if (acc.readable) {
+                                       cname = "%s_real_get_%s".printf (t.get_lower_case_cname (null), prop.name);
+                               } else {
+                                       cname = "%s_real_set_%s".printf (t.get_lower_case_cname (null), prop.name);
+                               }
+                       } else {
+                               cname = acc.get_cname ();
                        }
+
                        if (acc.readable) {
                                if (returns_real_struct) {
                                        // return non simple structs as out parameter
-                                       function = new CCodeFunction ("%s_get_%s".printf (prefix, prop.name), "void");
+                                       function = new CCodeFunction (cname, "void");
                                } else {
-                                       function = new CCodeFunction ("%s_get_%s".printf (prefix, prop.name), prop.property_type.get_cname ());
+                                       function = new CCodeFunction (cname, prop.property_type.get_cname ());
                                }
                        } else {
-                               function = new CCodeFunction ("%s_set_%s".printf (prefix, prop.name), "void");
+                               function = new CCodeFunction (cname, "void");
                        }
+
                        if (is_virtual) {
                                function.modifiers |= CCodeModifiers.STATIC;
                        }
index b772386..1931db2 100644 (file)
@@ -135,8 +135,11 @@ public class Vala.CCodeMemberAccessBinding : CCodeExpressionBinding {
                } else if (expr.symbol_reference is Property) {
                        var prop = (Property) expr.symbol_reference;
 
-                       if (prop.get_accessor != null &&
-                           prop.get_accessor.automatic_body &&
+                       if (prop.get_accessor == null) {
+                               return;
+                       }
+
+                       if (prop.get_accessor.automatic_body &&
                            codegen.current_type_symbol == prop.parent_symbol &&
                            prop.base_property == null &&
                            prop.base_interface_property == null) {
@@ -150,12 +153,11 @@ public class Vala.CCodeMemberAccessBinding : CCodeExpressionBinding {
                                } else if (prop.base_interface_property != null) {
                                        base_property = prop.base_interface_property;
                                }
-                               var base_property_type = (TypeSymbol) base_property.parent_symbol;
                                string getter_cname;
                                if (prop is DynamicProperty) {
                                        getter_cname = codegen.dynamic_property_binding ((DynamicProperty) prop).get_getter_cname ();
                                } else {
-                                       getter_cname = "%s_get_%s".printf (base_property_type.get_lower_case_cname (null), base_property.name);
+                                       getter_cname = base_property.get_accessor.get_cname ();
                                }
                                var ccall = new CCodeFunctionCall (new CCodeIdentifier (getter_cname));
 
index 9daaf88..3464934 100644 (file)
@@ -98,6 +98,12 @@ public class Vala.AttributeProcessor : CodeVisitor {
 
        public override void visit_property (Property prop) {
                prop.process_attributes ();
+
+               prop.accept_children (this);
+       }
+
+       public override void visit_property_accessor (PropertyAccessor p) {
+               p.process_attributes ();
        }
 
        public override void visit_delegate (Delegate d) {
index 86066ff..c38933a 100644 (file)
@@ -2181,7 +2181,7 @@ public class Vala.Parser : CodeVisitor {
                                expect (TokenType.SEMICOLON);
                        } else {
                                var accessor_begin = get_location ();
-                               parse_attributes ();
+                               var attrs = parse_attributes ();
                                var accessor_access = parse_access_modifier (SymbolAccessibility.PUBLIC);
                                if (accept (TokenType.GET)) {
                                        if (prop.get_accessor != null) {
@@ -2192,6 +2192,7 @@ public class Vala.Parser : CodeVisitor {
                                                block = parse_block ();
                                        }
                                        prop.get_accessor = new PropertyAccessor (true, false, false, block, get_src (accessor_begin));
+                                       set_attributes (prop.get_accessor, attrs);
                                        prop.get_accessor.access = accessor_access;
                                } else {
                                        bool writable, _construct;
@@ -2212,6 +2213,7 @@ public class Vala.Parser : CodeVisitor {
                                                block = parse_block ();
                                        }
                                        prop.set_accessor = new PropertyAccessor (false, writable, _construct, block, get_src (accessor_begin));
+                                       set_attributes (prop.set_accessor, attrs);
                                        prop.set_accessor.access = accessor_access;
                                }
                        }
index 664c718..f437f48 100644 (file)
@@ -70,6 +70,26 @@ public class Vala.PropertyAccessor : CodeNode {
         * Represents the generated value parameter in a set accessor.
         */
        public FormalParameter value_parameter { get; set; }
+
+       /**
+        * The publicly accessible name of the function that performs the
+        * access in C code.
+        */
+       public string get_cname () {
+               if (_cname != null) {
+                       return _cname;
+               }
+
+               var t = (TypeSymbol) prop.parent_symbol;
+
+               if (readable) {
+                       return "%s_get_%s".printf (t.get_lower_case_cname (null), prop.name);
+               } else {
+                       return "%s_set_%s".printf (t.get_lower_case_cname (null), prop.name);
+               }
+       }
+
+       private string? _cname;
        
        /**
         * Creates a new property accessor.
@@ -98,4 +118,17 @@ public class Vala.PropertyAccessor : CodeNode {
                        body.accept (visitor);
                }
        }
+
+       /**
+        * Process all associated attributes.
+        */
+       public void process_attributes () {
+               foreach (Attribute a in attributes) {
+                       if (a.name == "CCode") {
+                               if (a.has_argument ("cname")) {
+                                       _cname = a.get_string ("cname");
+                               }
+                       }
+               }
+       }
 }