[vala/staging] vala: Check accessibility of default arguments



commit 78588e6cf96a6f239840f728aedc2ecf5aabf7de
Author: Simon Werbeck <simon werbeck gmail com>
Date:   Sun Oct 2 00:20:59 2016 +0200

    vala: Check accessibility of default arguments
    
    https://bugzilla.gnome.org/show_bug.cgi?id=648030

 tests/Makefile.am                         |    1 +
 tests/methods/bug648030.test              |   10 ++++++++++
 vala/valaaddressofexpression.vala         |    4 ++++
 vala/valaarraycreationexpression.vala     |   14 ++++++++++++++
 vala/valaassignment.vala                  |    4 ++++
 vala/valabinaryexpression.vala            |    4 ++++
 vala/valacastexpression.vala              |    4 ++++
 vala/valaconditionalexpression.vala       |    4 ++++
 vala/valaelementaccess.vala               |   10 ++++++++++
 vala/valaexpression.vala                  |    8 ++++++++
 vala/valainitializerlist.vala             |   10 ++++++++++
 vala/valamemberaccess.vala                |    4 ++++
 vala/valamethodcall.vala                  |   10 ++++++++++
 vala/valaobjectcreationexpression.vala    |   20 ++++++++++++++++++++
 vala/valaparameter.vala                   |    2 ++
 vala/valapointerindirection.vala          |    4 ++++
 vala/valapostfixexpression.vala           |    4 ++++
 vala/valareferencetransferexpression.vala |    4 ++++
 vala/valasliceexpression.vala             |    4 ++++
 vala/valaunaryexpression.vala             |    4 ++++
 20 files changed, 129 insertions(+), 0 deletions(-)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 8fcf079..e10be4d 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -61,6 +61,7 @@ TESTS = \
        methods/bug642885.vala \
        methods/bug642899.vala \
        methods/bug646345.vala \
+       methods/bug648030.test \
        methods/bug648320.vala \
        methods/bug649562.vala \
        methods/bug652098.vala \
diff --git a/tests/methods/bug648030.test b/tests/methods/bug648030.test
new file mode 100644
index 0000000..e86e737
--- /dev/null
+++ b/tests/methods/bug648030.test
@@ -0,0 +1,10 @@
+Invalid Code
+
+const int FOO = 1;
+
+public void foo (int bar = FOO) {
+}
+
+void main () {
+       foo ();
+}
diff --git a/vala/valaaddressofexpression.vala b/vala/valaaddressofexpression.vala
index 81123ee..b537174 100644
--- a/vala/valaaddressofexpression.vala
+++ b/vala/valaaddressofexpression.vala
@@ -72,6 +72,10 @@ public class Vala.AddressofExpression : Expression {
                return inner.is_pure ();
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return inner.is_accessible (sym);
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
diff --git a/vala/valaarraycreationexpression.vala b/vala/valaarraycreationexpression.vala
index 5894784..a433157 100644
--- a/vala/valaarraycreationexpression.vala
+++ b/vala/valaarraycreationexpression.vala
@@ -113,6 +113,20 @@ public class Vala.ArrayCreationExpression : Expression {
                return false;
        }
 
+       public override bool is_accessible (Symbol sym) {
+               foreach (Expression e in sizes) {
+                       if (!e.is_accessible (sym)) {
+                               return false;
+                       }
+               }
+
+               if (initializer_list != null) {
+                       return initializer_list.is_accessible (sym);
+               }
+
+               return true;
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                for (int i = 0; i < sizes.size; i++) {
                        if (sizes[i] == old_node) {
diff --git a/vala/valaassignment.vala b/vala/valaassignment.vala
index fc27acf..88e45ff 100644
--- a/vala/valaassignment.vala
+++ b/vala/valaassignment.vala
@@ -97,6 +97,10 @@ public class Vala.Assignment : Expression {
                return false;
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return left.is_accessible (sym) && right.is_accessible (sym);
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
diff --git a/vala/valabinaryexpression.vala b/vala/valabinaryexpression.vala
index e57ebb4..b081090 100644
--- a/vala/valabinaryexpression.vala
+++ b/vala/valabinaryexpression.vala
@@ -141,6 +141,10 @@ public class Vala.BinaryExpression : Expression {
                return left.is_non_null () && right.is_non_null ();
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return left.is_accessible (sym) && right.is_accessible (sym);
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
diff --git a/vala/valacastexpression.vala b/vala/valacastexpression.vala
index 36edb73..f9e3d30 100644
--- a/vala/valacastexpression.vala
+++ b/vala/valacastexpression.vala
@@ -103,6 +103,10 @@ public class Vala.CastExpression : Expression {
                return inner.is_pure ();
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return inner.is_accessible (sym);
+       }
+
        public override void replace_type (DataType old_type, DataType new_type) {
                if (type_reference == old_type) {
                        type_reference = new_type;
diff --git a/vala/valaconditionalexpression.vala b/vala/valaconditionalexpression.vala
index 3ae003c..74cba96 100644
--- a/vala/valaconditionalexpression.vala
+++ b/vala/valaconditionalexpression.vala
@@ -100,6 +100,10 @@ public class Vala.ConditionalExpression : Expression {
                return condition.is_pure () && true_expression.is_pure () && false_expression.is_pure ();
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return condition.is_accessible (sym) && true_expression.is_accessible (sym) && 
false_expression.is_accessible (sym);
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
diff --git a/vala/valaelementaccess.vala b/vala/valaelementaccess.vala
index b879ffc..4fb44b8 100644
--- a/vala/valaelementaccess.vala
+++ b/vala/valaelementaccess.vala
@@ -96,6 +96,16 @@ public class Vala.ElementAccess : Expression {
                return container.is_pure ();
        }
 
+       public override bool is_accessible (Symbol sym) {
+               foreach (Expression index in indices) {
+                       if (!index.is_accessible (sym)) {
+                               return false;
+                       }
+               }
+
+               return container.is_accessible (sym);
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
diff --git a/vala/valaexpression.vala b/vala/valaexpression.vala
index 620086e..5f7a023 100644
--- a/vala/valaexpression.vala
+++ b/vala/valaexpression.vala
@@ -78,6 +78,14 @@ public abstract class Vala.Expression : CodeNode {
                return false;
        }
 
+       /**
+        * Check whether symbol_references in this expression are at least
+        * as accessible as the specified symbol.
+        */
+       public virtual bool is_accessible (Symbol sym) {
+               return true;
+       }
+
        public Statement? parent_statement {
                get {
                        var expr = parent_node as Expression;
diff --git a/vala/valainitializerlist.vala b/vala/valainitializerlist.vala
index 6955aad..c1f97ca 100644
--- a/vala/valainitializerlist.vala
+++ b/vala/valainitializerlist.vala
@@ -96,6 +96,16 @@ public class Vala.InitializerList : Expression {
                return true;
        }
 
+       public override bool is_accessible (Symbol sym) {
+               foreach (Expression initializer in initializers) {
+                       if (!initializer.is_accessible (sym)) {
+                               return false;
+                       }
+               }
+
+               return true;
+       }
+
        public override void replace_expression (Expression old_node, Expression new_node) {
                for (int i = 0; i < initializers.size; i++) {
                        if (initializers[i] == old_node) {
diff --git a/vala/valamemberaccess.vala b/vala/valamemberaccess.vala
index 2bab0b0..6911bef 100644
--- a/vala/valamemberaccess.vala
+++ b/vala/valamemberaccess.vala
@@ -156,6 +156,10 @@ public class Vala.MemberAccess : Expression {
                return (inner == null || inner.is_pure ()) && !(symbol_reference is Property);
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return (inner == null || inner.is_accessible (sym)) && symbol_reference.is_accessible (sym);
+       }
+
        public override void replace_type (DataType old_type, DataType new_type) {
                for (int i = 0; i < type_argument_list.size; i++) {
                        if (type_argument_list[i] == old_type) {
diff --git a/vala/valamethodcall.vala b/vala/valamethodcall.vala
index 23712a2..3d5835c 100644
--- a/vala/valamethodcall.vala
+++ b/vala/valamethodcall.vala
@@ -130,6 +130,16 @@ public class Vala.MethodCall : Expression {
                return false;
        }
 
+       public override bool is_accessible (Symbol sym) {
+               foreach (var arg in argument_list) {
+                       if (!arg.is_accessible (sym)) {
+                               return false;
+                       }
+               }
+
+               return call.is_accessible (sym);
+       }
+
        bool is_chainup () {
                if (!(call.symbol_reference is CreationMethod)) {
                        return false;
diff --git a/vala/valaobjectcreationexpression.vala b/vala/valaobjectcreationexpression.vala
index 954f77c..8481f44 100644
--- a/vala/valaobjectcreationexpression.vala
+++ b/vala/valaobjectcreationexpression.vala
@@ -139,6 +139,26 @@ public class Vala.ObjectCreationExpression : Expression {
                return false;
        }
 
+       public override bool is_accessible (Symbol sym) {
+               if (member_name != null && !member_name.is_accessible (sym)) {
+                       return false;
+               }
+
+               foreach (var arg in argument_list) {
+                       if (!arg.is_accessible (sym)) {
+                               return false;
+                       }
+               }
+
+               foreach (var init in object_initializer) {
+                       if (!init.initializer.is_accessible (sym)) {
+                               return false;
+                       }
+               }
+
+               return true;
+       }
+
        public override void replace_type (DataType old_type, DataType new_type) {
                if (type_reference == old_type) {
                        type_reference = new_type;
diff --git a/vala/valaparameter.vala b/vala/valaparameter.vala
index b55374c..67d172e 100644
--- a/vala/valaparameter.vala
+++ b/vala/valaparameter.vala
@@ -174,6 +174,8 @@ public class Vala.Parameter : Variable {
                                Report.error (initializer.source_reference, "Cannot convert from `%s' to 
`%s'".printf (initializer.value_type.to_string (), variable_type.to_string ()));
                        } else if (direction == ParameterDirection.REF) {
                                Report.error (source_reference, "default value not allowed for ref 
parameter");
+                       } else if (!initializer.is_accessible (this)) {
+                               Report.error (initializer.source_reference, "default value is less accessible 
than method `%s'".printf (parent_symbol.get_full_name ()));
                        }
                }
 
diff --git a/vala/valapointerindirection.vala b/vala/valapointerindirection.vala
index fffbf93..ec8b6b4 100644
--- a/vala/valapointerindirection.vala
+++ b/vala/valapointerindirection.vala
@@ -69,6 +69,10 @@ public class Vala.PointerIndirection : Expression {
                return inner.is_pure ();
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return inner.is_accessible (sym);
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
diff --git a/vala/valapostfixexpression.vala b/vala/valapostfixexpression.vala
index 2c829b4..05274c1 100644
--- a/vala/valapostfixexpression.vala
+++ b/vala/valapostfixexpression.vala
@@ -72,6 +72,10 @@ public class Vala.PostfixExpression : Expression {
                return false;
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return inner.is_accessible (sym);
+       }
+
        public override void get_defined_variables (Collection<Variable> collection) {
                inner.get_defined_variables (collection);
                var local = inner.symbol_reference as LocalVariable;
diff --git a/vala/valareferencetransferexpression.vala b/vala/valareferencetransferexpression.vala
index 7ceba6d..ceb51a2 100644
--- a/vala/valareferencetransferexpression.vala
+++ b/vala/valareferencetransferexpression.vala
@@ -71,6 +71,10 @@ public class Vala.ReferenceTransferExpression : Expression {
                return false;
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return inner.is_accessible (sym);
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
diff --git a/vala/valasliceexpression.vala b/vala/valasliceexpression.vala
index 5b1675b..6621b41 100644
--- a/vala/valasliceexpression.vala
+++ b/vala/valasliceexpression.vala
@@ -98,6 +98,10 @@ public class Vala.SliceExpression : Expression {
                return false;
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return container.is_accessible (sym) && start.is_accessible (sym) && stop.is_accessible (sym);
+       }
+
        public override bool check (CodeContext context) {
                if (checked) {
                        return !error;
diff --git a/vala/valaunaryexpression.vala b/vala/valaunaryexpression.vala
index ed30841..dfc795c 100644
--- a/vala/valaunaryexpression.vala
+++ b/vala/valaunaryexpression.vala
@@ -120,6 +120,10 @@ public class Vala.UnaryExpression : Expression {
                return inner.is_pure ();
        }
 
+       public override bool is_accessible (Symbol sym) {
+               return inner.is_accessible (sym);
+       }
+
        bool is_numeric_type (DataType type) {
                if (!(type.data_type is Struct)) {
                        return false;


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]