[vala/wip/issue/658: 19/34] Fixed vapi generation for anonymous delegates




commit f945f87ced367d0e2115da049c03b03c0972e45d
Author: Nick Schrader <nick schrader mailbox org>
Date:   Tue Oct 20 16:28:34 2020 -0300

    Fixed vapi generation for anonymous delegates

 vala/valacodewriter.vala | 55 +++++++++++++++++++++++++++++++++++++++++++++++-
 vala/valaparser.vala     | 24 +++++++--------------
 2 files changed, 62 insertions(+), 17 deletions(-)
---
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index f555b7a02..585cc1d42 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -683,7 +683,11 @@ public class Vala.CodeWriter : CodeVisitor {
                                }
                        }
 
-                       write_type (param.variable_type);
+                       if (param.variable_type is DelegateType && param.variable_type.type_symbol.anonymous) 
{
+                               write_anonymous_delegate (param.variable_type as DelegateType);
+                       } else {
+                               write_type (param.variable_type);
+                       }
 
                        write_string (" ");
                        write_identifier (param.name);
@@ -709,6 +713,11 @@ public class Vala.CodeWriter : CodeVisitor {
                        return;
                }
 
+               if (cb.anonymous) {
+                       return;
+               }
+                       
+
                if (context.vapi_comments && cb.comment != null) {
                        write_comment (cb.comment);
                }
@@ -1591,6 +1600,50 @@ public class Vala.CodeWriter : CodeVisitor {
                stream.putc ('}');
        }
 
+       private void write_anonymous_params (List<Parameter> params) {
+               write_string ("(");
+
+               int i = 1;
+               foreach (Parameter param in params) {
+                       if (i > 1) {
+                               write_string (", ");
+                       }
+
+                       // TODO: Parameter attributes?
+                       write_attributes (param);
+
+                       if (param.params_array) {
+                               write_string ("params ");
+                       }
+
+                       if (param.direction == ParameterDirection.REF) {
+                               write_string ("ref ");
+                       } else if (param.direction == ParameterDirection.OUT) {
+                               write_string ("out ");
+                       }
+
+                       // TODO: Array type?
+                       write_type (param.variable_type);
+                       write_type_suffix (param.variable_type);
+
+                       i++;
+               }
+
+               write_string (")");
+
+       }
+
+       private void write_anonymous_delegate (DelegateType cb) {
+               // TODO: What about attributes?
+               //write_attributes (cb);
+
+               write_string ("delegate(");
+               write_anonymous_params (cb.get_parameters ());
+
+               write_string (" => ");
+               write_return_type (cb.get_return_type ());
+       }
+
        private bool check_accessibility (Symbol sym) {
                switch (type) {
                case CodeWriterType.EXTERNAL:
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 551c6251a..6288fa92c 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -474,17 +474,13 @@ public class Vala.Parser : CodeVisitor {
                return result;
        }
 
-       DataType parse_type_with_parent (bool owned_by_default, bool can_weak_ref, Symbol? parent) throws 
ParseError {
-               return parse_type(owned_by_default, can_weak_ref, false, parent);
-       }
-
-       DataType parse_type (bool owned_by_default, bool can_weak_ref, bool require_unowned = false, Symbol? 
parent=null) throws ParseError {
+       DataType parse_type (bool owned_by_default, bool can_weak_ref, bool require_unowned = false, Symbol? 
parent=null, Method? method=null) throws ParseError {
                var begin = get_location ();
 
                // TODO: Can anonymous delegates be unowned or weak?
                if (accept (TokenType.DELEGATE)) {
                        rollback (begin);
-                       return parse_anonymous_delegate (parent);
+                       return parse_anonymous_delegate (parent, method);
                } else {
                        rollback (begin);
                }
@@ -3006,7 +3002,7 @@ public class Vala.Parser : CodeVisitor {
                expect (TokenType.OPEN_PARENS);
                if (current () != TokenType.CLOSE_PARENS) {
                        do {
-                               var param = parse_parameter (parent);
+                               var param = parse_parameter (parent, method);
                                method.add_parameter (param);
                        } while (accept (TokenType.COMMA));
                }
@@ -3536,7 +3532,7 @@ public class Vala.Parser : CodeVisitor {
                }
        }
 
-       Parameter parse_parameter (Symbol? parent=null) throws ParseError {
+       Parameter parse_parameter (Symbol? parent=null, Method? method=null) throws ParseError {
                var attrs = parse_attributes ();
                var begin = get_location ();
                if (accept (TokenType.ELLIPSIS)) {
@@ -3554,13 +3550,13 @@ public class Vala.Parser : CodeVisitor {
                DataType type;
                if (direction == ParameterDirection.IN) {
                        // in parameters are unowned by default
-                       type = parse_type_with_parent (false, false, parent);
+                       type = parse_type (false, false, false, parent, method);
                } else if (direction == ParameterDirection.REF) {
                        // ref parameters own the value by default
-                       type = parse_type_with_parent (true, true, parent);
+                       type = parse_type (true, true, false, parent, method);
                } else {
                        // out parameters own the value by default
-                       type = parse_type_with_parent (true, false, parent);
+                       type = parse_type (true, false, false, parent, method);
                }
                string id = parse_identifier ();
 
@@ -3737,11 +3733,7 @@ public class Vala.Parser : CodeVisitor {
                return param;
        }
 
-       DelegateType parse_anonymous_delegate (Symbol? parent) throws ParseError { //(Symbol parent, 
List<Attribute>? attrs) throws ParseError {
-               if (parent == null) {
-                       throw new ParseError.SYNTAX ("anonymous delegate: parent==null");
-               }
-
+       DelegateType parse_anonymous_delegate (Symbol parent, Method method) throws ParseError {
                var begin = get_location ();
                expect (TokenType.DELEGATE);
 


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