From: Juerg Billeter Date: Tue, 5 Feb 2008 20:05:00 +0000 (+0000) Subject: add support for private property accessors X-Git-Tag: VALA_0_1_7~114 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=607dea4000a8f533f8c82e745ade0f2e772e5499;p=platform%2Fupstream%2Fvala.git add support for private property accessors 2008-02-05 Juerg Billeter * vala/parser.y, vala/valaexpression.vala, vala/valapropertyaccessor.vala, vala/valasemanticanalyzer.vala, gobject/valaccodegenerator.vala: add support for private property accessors svn path=/trunk/; revision=977 --- diff --git a/ChangeLog b/ChangeLog index a8bbe66..a724a0c 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,12 @@ 2008-02-05 Jürg Billeter + * vala/parser.y, vala/valaexpression.vala, + vala/valapropertyaccessor.vala, vala/valasemanticanalyzer.vala, + gobject/valaccodegenerator.vala: add support for private property + accessors + +2008-02-05 Jürg Billeter + * vala/valacfgbuilder.vala, vala/valapropertyaccessor.vala: build control flow graph for property accessors diff --git a/gobject/valaccodegenerator.vala b/gobject/valaccodegenerator.vala index d4ae23b..98793ca 100644 --- a/gobject/valaccodegenerator.vala +++ b/gobject/valaccodegenerator.vala @@ -601,7 +601,7 @@ public class Vala.CCodeGenerator : CodeGenerator { function.add_parameter (cvalueparam); } - if (!prop.is_internal_symbol () && (acc.readable || acc.writable)) { + if (!prop.is_internal_symbol () && (acc.readable || acc.writable) && acc.access != SymbolAccessibility.PRIVATE) { // accessor function should be public if the property is a public symbol and it's not a construct-only setter header_type_member_declaration.append (function.copy ()); } else { @@ -697,7 +697,7 @@ public class Vala.CCodeGenerator : CodeGenerator { } if (!is_virtual) { - if (!prop.is_internal_symbol () && (acc.readable || acc.writable)) { + if (!prop.is_internal_symbol () && (acc.readable || acc.writable) && acc.access != SymbolAccessibility.PRIVATE) { // accessor function should be public if the property is a public symbol and it's not a construct-only setter header_type_member_declaration.append (function.copy ()); } else { diff --git a/vala/parser.y b/vala/parser.y index 469419e..0b67676 100644 --- a/vala/parser.y +++ b/vala/parser.y @@ -2648,7 +2648,7 @@ class_declaration } VALA_CODE_NODE (current_symbol)->attributes = $2; - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL (current_symbol), $3); } if (($4 & VALA_MODIFIER_ABSTRACT) == VALA_MODIFIER_ABSTRACT) { @@ -2706,7 +2706,7 @@ class_declaration opt_access_modifier : /* empty */ { - $$ = 0; + $$ = -1; } | access_modifier ; @@ -2883,7 +2883,7 @@ constant_declaration g_object_unref (src); g_object_unref ($5); g_object_unref ($6); - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL ($$), $3); } VALA_CODE_NODE($$)->attributes = $2; @@ -2908,7 +2908,7 @@ field_declaration $$ = vala_code_context_create_field (context, vala_symbol_get_name (VALA_SYMBOL ($6)), $5, vala_variable_declarator_get_initializer ($6), src); g_object_unref (src); - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL ($$), $3); } if (($4 & VALA_MODIFIER_STATIC) == VALA_MODIFIER_STATIC) { @@ -3060,7 +3060,7 @@ method_header $$ = vala_code_context_create_method (context, $6, $5, src); g_object_unref (src); - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL ($$), $3); } if (($4 & VALA_MODIFIER_STATIC) == VALA_MODIFIER_STATIC) { @@ -3133,7 +3133,7 @@ method_header g_free ($6); g_object_unref (src); vala_method_set_instance ($$, FALSE); - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL ($$), $3); } VALA_CODE_NODE($$)->attributes = $2; @@ -3345,7 +3345,9 @@ property_declaration VALA_CODE_NODE($$)->attributes = $2; - vala_symbol_set_access (VALA_SYMBOL ($$), $3); + if ($3 != -1) { + vala_symbol_set_access (VALA_SYMBOL ($$), $3); + } g_object_unref ($5); g_free ($6); @@ -3381,7 +3383,9 @@ property_declaration VALA_CODE_NODE($$)->attributes = $2; - vala_symbol_set_access (VALA_SYMBOL ($$), $3); + if ($3 != -1) { + vala_symbol_set_access (VALA_SYMBOL ($$), $3); + } g_object_unref ($5); g_free ($6); @@ -3414,14 +3418,19 @@ opt_get_accessor_declaration ; get_accessor_declaration - : opt_attributes GET method_body + : opt_attributes opt_access_modifier GET method_body { - ValaSourceReference *src = src(@2); - $$ = vala_code_context_create_property_accessor (context, TRUE, FALSE, FALSE, $3, src); + ValaSourceReference *src = src(@3); + $$ = vala_code_context_create_property_accessor (context, TRUE, FALSE, FALSE, $4, src); g_object_unref (src); + if ($4 != NULL) { + g_object_unref ($4); + } - if ($3 != NULL) { - g_object_unref ($3); + if ($2 != -1) { + vala_property_accessor_set_access ($$, $2); + } else { + vala_property_accessor_set_access ($$, VALA_SYMBOL_ACCESSIBILITY_PUBLIC); } } ; @@ -3435,41 +3444,65 @@ opt_set_accessor_declaration ; set_accessor_declaration - : opt_attributes SET method_body + : opt_attributes opt_access_modifier SET method_body { - ValaSourceReference *src = src(@2); - $$ = vala_code_context_create_property_accessor (context, FALSE, TRUE, FALSE, $3, src); - g_object_unref (src); - if ($3 != NULL) { - g_object_unref ($3); - } - } - | opt_attributes SET CONSTRUCT method_body - { - ValaSourceReference *src = src(@2); - $$ = vala_code_context_create_property_accessor (context, FALSE, TRUE, TRUE, $4, src); + ValaSourceReference *src = src(@3); + $$ = vala_code_context_create_property_accessor (context, FALSE, TRUE, FALSE, $4, src); g_object_unref (src); if ($4 != NULL) { g_object_unref ($4); } + + if ($2 != -1) { + vala_property_accessor_set_access ($$, $2); + } else { + vala_property_accessor_set_access ($$, VALA_SYMBOL_ACCESSIBILITY_PUBLIC); + } } - | opt_attributes CONSTRUCT method_body + | opt_attributes opt_access_modifier SET CONSTRUCT method_body { - ValaSourceReference *src = src(@2); - $$ = vala_code_context_create_property_accessor (context, FALSE, FALSE, TRUE, $3, src); + ValaSourceReference *src = src(@3); + $$ = vala_code_context_create_property_accessor (context, FALSE, TRUE, TRUE, $5, src); g_object_unref (src); - if ($3 != NULL) { - g_object_unref ($3); + if ($5 != NULL) { + g_object_unref ($5); + } + + if ($2 != -1) { + vala_property_accessor_set_access ($$, $2); + } else { + vala_property_accessor_set_access ($$, VALA_SYMBOL_ACCESSIBILITY_PUBLIC); } } - | opt_attributes CONSTRUCT SET method_body + | opt_attributes opt_access_modifier CONSTRUCT method_body { - ValaSourceReference *src = src(@2); - $$ = vala_code_context_create_property_accessor (context, FALSE, TRUE, TRUE, $4, src); + ValaSourceReference *src = src(@3); + $$ = vala_code_context_create_property_accessor (context, FALSE, FALSE, TRUE, $4, src); g_object_unref (src); if ($4 != NULL) { g_object_unref ($4); } + + if ($2 != -1) { + vala_property_accessor_set_access ($$, $2); + } else { + vala_property_accessor_set_access ($$, VALA_SYMBOL_ACCESSIBILITY_PUBLIC); + } + } + | opt_attributes opt_access_modifier CONSTRUCT SET method_body + { + ValaSourceReference *src = src(@3); + $$ = vala_code_context_create_property_accessor (context, FALSE, TRUE, TRUE, $5, src); + g_object_unref (src); + if ($5 != NULL) { + g_object_unref ($5); + } + + if ($2 != -1) { + vala_property_accessor_set_access ($$, $2); + } else { + vala_property_accessor_set_access ($$, VALA_SYMBOL_ACCESSIBILITY_PUBLIC); + } } ; @@ -3481,7 +3514,7 @@ signal_declaration ValaSourceReference *src = src_com(@6, $1); $$ = vala_code_context_create_signal (context, $6, $5, src); g_object_unref (src); - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL ($$), $3); } VALA_CODE_NODE($$)->attributes = $2; @@ -3580,7 +3613,7 @@ struct_declaration vala_struct_add_type_parameter (VALA_STRUCT (current_symbol), l->data); } VALA_CODE_NODE(current_symbol)->attributes = $2; - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL (current_symbol), $3); } if ($8 != NULL) { @@ -3690,7 +3723,7 @@ interface_declaration g_object_unref (parent_symbol); VALA_CODE_NODE (iface)->attributes = $2; - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL (iface), $3); } if (($4 & VALA_MODIFIER_STATIC) == VALA_MODIFIER_STATIC) { @@ -3818,7 +3851,7 @@ enum_declaration VALA_CODE_NODE (en)->attributes = $2; - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL (en), $3); } @@ -3925,7 +3958,7 @@ delegate_declaration vala_source_file_add_node (current_source_file, VALA_CODE_NODE (cb)); g_object_unref (parent_symbol); - if ($3 != 0) { + if ($3 != -1) { vala_symbol_set_access (VALA_SYMBOL (cb), $3); } VALA_CODE_NODE (cb)->attributes = $2; diff --git a/vala/valaexpression.vala b/vala/valaexpression.vala index 01e7dfa..a82a211 100644 --- a/vala/valaexpression.vala +++ b/vala/valaexpression.vala @@ -1,6 +1,6 @@ /* valaexpression.vala * - * Copyright (C) 2006-2007 Jürg Billeter + * Copyright (C) 2006-2008 Jürg Billeter * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public @@ -73,6 +73,12 @@ public abstract class Vala.Expression : CodeNode { public bool can_fail { get; set; } /** + * Specifies that this expression is used as lvalue, i.e. the + * left hand side of an assignment. + */ + public bool lvalue { get; set; } + + /** * Contains all temporary variables this expression requires for * execution. * diff --git a/vala/valapropertyaccessor.vala b/vala/valapropertyaccessor.vala index 0c78df3..cd0857d 100644 --- a/vala/valapropertyaccessor.vala +++ b/vala/valapropertyaccessor.vala @@ -46,7 +46,12 @@ public class Vala.PropertyAccessor : CodeNode { * property. */ public bool construction { get; set; } - + + /** + * Specifies the accessibility of this property accessor. + */ + public SymbolAccessibility access { get; set; } + /** * The accessor body. */ diff --git a/vala/valasemanticanalyzer.vala b/vala/valasemanticanalyzer.vala index 878d2b2..fc98116 100644 --- a/vala/valasemanticanalyzer.vala +++ b/vala/valasemanticanalyzer.vala @@ -1485,7 +1485,36 @@ public class Vala.SemanticAnalyzer : CodeVisitor { var m = (Method) member; access = m.access; instance = m.instance; - } else if (member is Property || member is Signal) { + } else if (member is Property) { + var prop = (Property) member; + access = prop.access; + if (expr.lvalue) { + if (prop.set_accessor == null) { + expr.error = true; + Report.error (expr.source_reference, "Property `%s' is read-only".printf (prop.get_full_name ())); + return; + } + if (prop.access == SymbolAccessibility.PUBLIC) { + access = prop.set_accessor.access; + } else if (prop.access == SymbolAccessibility.PROTECTED + && prop.set_accessor.access != SymbolAccessibility.PUBLIC) { + access = prop.set_accessor.access; + } + } else { + if (prop.get_accessor == null) { + expr.error = true; + Report.error (expr.source_reference, "Property `%s' is write-only".printf (prop.get_full_name ())); + return; + } + if (prop.access == SymbolAccessibility.PUBLIC) { + access = prop.get_accessor.access; + } else if (prop.access == SymbolAccessibility.PROTECTED + && prop.get_accessor.access != SymbolAccessibility.PUBLIC) { + access = prop.get_accessor.access; + } + } + instance = prop.instance; + } else if (member is Signal) { instance = true; } @@ -2632,6 +2661,8 @@ public class Vala.SemanticAnalyzer : CodeVisitor { } public override void visit_assignment (Assignment! a) { + a.left.lvalue = true; + a.left.accept (this); if (a.left.error) {