[vala/staging] Add support for sealed classes in bindings



commit 65ab8bd074b21902184cb93dc957d0a6c42d30cd
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Jun 16 19:28:24 2019 +0200

    Add support for sealed classes in bindings
    
    The "sealed" keyword was available and parsed for a long time. So simply
    pick it up information and expose it in the AST.
    
    Issue a warning when it is used in vala source.
    
    See https://gitlab.gnome.org/GNOME/vala/issues/278

 tests/Makefile.am                        |  4 ++++
 tests/objects/sealed-abstract-class.test |  7 +++++++
 tests/objects/sealed-class.test          | 10 ++++++++++
 tests/objects/sealed-compact-class.test  |  8 ++++++++
 tests/parser/sealed-class.vala           |  8 ++++++++
 vala/valaclass.vala                      | 28 ++++++++++++++++++++++++++++
 vala/valacodewriter.vala                 |  3 +++
 vala/valagirparser.vala                  |  2 ++
 vala/valaparser.vala                     |  3 +++
 9 files changed, 73 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index e73bc0ac3..c75dc67dd 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -385,6 +385,9 @@ TESTS = \
        objects/property-simple-type-struct-nullable.vala \
        objects/property-static.vala \
        objects/regex.vala \
+       objects/sealed-abstract-class.test \
+       objects/sealed-class.test \
+       objects/sealed-compact-class.test \
        objects/signals.vala \
        objects/signals-enum-marshal.vala \
        objects/signals-delegate.vala \
@@ -675,6 +678,7 @@ TESTS = \
        parser/property-set-redefined.test \
        parser/qualified-member-access.vala \
        parser/qualified-symbol-access.vala \
+       parser/sealed-class.vala \
        parser/signal-no-class.test \
        parser/signal-no-static.test \
        parser/statement-outside-root.test \
diff --git a/tests/objects/sealed-abstract-class.test b/tests/objects/sealed-abstract-class.test
new file mode 100644
index 000000000..337820a04
--- /dev/null
+++ b/tests/objects/sealed-abstract-class.test
@@ -0,0 +1,7 @@
+Invalid Code
+
+sealed abstract class Foo {
+}
+
+void main () {
+}
diff --git a/tests/objects/sealed-class.test b/tests/objects/sealed-class.test
new file mode 100644
index 000000000..b5ec1f052
--- /dev/null
+++ b/tests/objects/sealed-class.test
@@ -0,0 +1,10 @@
+Invalid Code
+
+sealed class Foo {
+}
+
+class Bar : Foo {
+}
+
+void main () {
+}
diff --git a/tests/objects/sealed-compact-class.test b/tests/objects/sealed-compact-class.test
new file mode 100644
index 000000000..006c7fc02
--- /dev/null
+++ b/tests/objects/sealed-compact-class.test
@@ -0,0 +1,8 @@
+Invalid Code
+
+[Compact]
+sealed class Foo {
+}
+
+void main () {
+}
diff --git a/tests/parser/sealed-class.vala b/tests/parser/sealed-class.vala
new file mode 100644
index 000000000..b6f420703
--- /dev/null
+++ b/tests/parser/sealed-class.vala
@@ -0,0 +1,8 @@
+sealed class Foo {
+}
+
+sealed class Bar : Object {
+}
+
+void main () {
+}
diff --git a/vala/valaclass.vala b/vala/valaclass.vala
index e9312c415..ace8ea694 100644
--- a/vala/valaclass.vala
+++ b/vala/valaclass.vala
@@ -37,6 +37,12 @@ public class Vala.Class : ObjectTypeSymbol {
         */
        public bool is_abstract { get; set; }
 
+       /**
+        * Specifies whether this class is sealed. Sealed classes may not be
+        * sub-classed.
+        */
+       public bool is_sealed { get; set; }
+
        /**
         * Instances of compact classes are fast to create and have a
         * compact memory layout. Compact classes don't support runtime
@@ -582,6 +588,28 @@ public class Vala.Class : ObjectTypeSymbol {
                        add_constructor (c);
                }
 
+               if (base_class != null && base_class.is_sealed) {
+                       error = true;
+                       Report.error (source_reference, "`%s' cannot inherit from sealed class `%s'".printf 
(get_full_name (), base_class.get_full_name ()));
+               }
+
+               if (is_sealed) {
+                       if (is_compact) {
+                               error = true;
+                               Report.error (source_reference, "Sealed class `%s' cannot be compact".printf 
(get_full_name ()));
+                               return false;
+                       }
+                       if (is_abstract) {
+                               error = true;
+                               Report.error (source_reference, "Sealed class `%s' cannot be abstract".printf 
(get_full_name ()));
+                               return false;
+                       }
+                       if (!external_package) {
+                               is_sealed = false;
+                               Report.warning (source_reference, "Sealed classes are not fully supported 
yet");
+                       }
+               }
+
                /* process enums first to avoid order problems in C code */
                foreach (Enum en in get_enums ()) {
                        en.check (context);
diff --git a/vala/valacodewriter.vala b/vala/valacodewriter.vala
index 2f735e87f..198ee51c2 100644
--- a/vala/valacodewriter.vala
+++ b/vala/valacodewriter.vala
@@ -246,6 +246,9 @@ public class Vala.CodeWriter : CodeVisitor {
                if (cl.is_abstract) {
                        write_string ("abstract ");
                }
+               if (cl.is_sealed) {
+                       write_string ("sealed ");
+               }
                write_string ("class ");
                write_identifier (cl.name);
 
diff --git a/vala/valagirparser.vala b/vala/valagirparser.vala
index 30468323a..6d6f767d8 100644
--- a/vala/valagirparser.vala
+++ b/vala/valagirparser.vala
@@ -62,6 +62,7 @@ public class Vala.GirParser : CodeVisitor {
                VFUNC_NAME,
                VIRTUAL,
                ABSTRACT,
+               SEALED,
                SCOPE,
                STRUCT,
                THROWS,
@@ -2884,6 +2885,7 @@ public class Vala.GirParser : CodeVisitor {
                if (current.new_symbol) {
                        cl = new Class (current.name, current.source_reference);
                        cl.is_abstract = metadata.get_bool (ArgumentType.ABSTRACT, reader.get_attribute 
("abstract") == "1");
+                       cl.is_sealed = metadata.get_bool (ArgumentType.SEALED, false);
 
                        if (parent != null) {
                                cl.add_base_type (parse_type_from_gir_name (parent));
diff --git a/vala/valaparser.vala b/vala/valaparser.vala
index 2b480ce59..62ad72898 100644
--- a/vala/valaparser.vala
+++ b/vala/valaparser.vala
@@ -2636,6 +2636,9 @@ public class Vala.Parser : CodeVisitor {
                if (ModifierFlags.ABSTRACT in flags) {
                        cl.is_abstract = true;
                }
+               if (ModifierFlags.SEALED in flags) {
+                       cl.is_sealed = true;
+               }
                if (ModifierFlags.EXTERN in flags) {
                        cl.is_extern = true;
                }


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