[vala/staging] vala: Allow delegate without target to be used as constant type



commit 9669d762af8753cfee7c82b079f3a09a3af990c1
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Tue Feb 22 14:20:46 2022 +0100

    vala: Allow delegate without target to be used as constant type

 tests/Makefile.am                        |  1 +
 tests/constants/delegate-type.c-expected | 64 ++++++++++++++++++++++++++++++++
 tests/constants/delegate-type.vala       | 14 +++++++
 vala/valaconstant.vala                   |  3 ++
 4 files changed, 82 insertions(+)
---
diff --git a/tests/Makefile.am b/tests/Makefile.am
index 06043b4ed..be5b9f4e7 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -103,6 +103,7 @@ TESTS = \
        basic-types/bug787152.vala \
        basic-types/bug788775.vala \
        constants/array-type-invalid.test \
+       constants/delegate-type.vala \
        constants/glog.vala \
        constants/member-access.vala \
        constants/member-access-enum.vala \
diff --git a/tests/constants/delegate-type.c-expected b/tests/constants/delegate-type.c-expected
new file mode 100644
index 000000000..126728767
--- /dev/null
+++ b/tests/constants/delegate-type.c-expected
@@ -0,0 +1,64 @@
+/* constants_delegate_type.c generated by valac, the Vala compiler
+ * generated from constants_delegate_type.vala, do not modify */
+
+#include <stdlib.h>
+#include <string.h>
+#include <glib.h>
+
+#define FOO _func_foo_func
+#if !defined(VALA_EXTERN)
+#if defined(_MSC_VER)
+#define VALA_EXTERN __declspec(dllexport) extern
+#elif __GNUC__ >= 4
+#define VALA_EXTERN __attribute__((visibility("default"))) extern
+#else
+#define VALA_EXTERN extern
+#endif
+#endif
+
+typedef const gchar* (*FooFunc) (void);
+#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, 
__LINE__, G_STRFUNC, msg);
+#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, 
G_STRFUNC, msg); return; }
+#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning 
(G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
+#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, 
__LINE__, G_STRFUNC, msg);
+
+VALA_EXTERN const gchar* func (void);
+static const gchar* _func_foo_func (void);
+static void _vala_main (void);
+
+static const gchar*
+_func_foo_func (void)
+{
+       const gchar* result;
+       result = func ();
+       return result;
+}
+
+const gchar*
+func (void)
+{
+       const gchar* result;
+       result = "foo";
+       return result;
+}
+
+static void
+_vala_main (void)
+{
+       static const FooFunc foo = _func_foo_func;
+       const gchar* _tmp0_;
+       const gchar* _tmp1_;
+       _tmp0_ = foo ();
+       _vala_assert (g_strcmp0 (_tmp0_, "foo") == 0, "foo () == \"foo\"");
+       _tmp1_ = FOO ();
+       _vala_assert (g_strcmp0 (_tmp1_, "foo") == 0, "FOO () == \"foo\"");
+}
+
+int
+main (int argc,
+      char ** argv)
+{
+       _vala_main ();
+       return 0;
+}
+
diff --git a/tests/constants/delegate-type.vala b/tests/constants/delegate-type.vala
new file mode 100644
index 000000000..22c9f69b5
--- /dev/null
+++ b/tests/constants/delegate-type.vala
@@ -0,0 +1,14 @@
+[CCode (has_target = false)]
+delegate unowned string FooFunc ();
+
+const FooFunc FOO = func;
+
+unowned string func () {
+       return "foo";
+}
+
+void main () {
+       const FooFunc foo = func;
+       assert (foo () == "foo");
+       assert (FOO () == "foo");
+}
diff --git a/vala/valaconstant.vala b/vala/valaconstant.vala
index aa97e6ea2..9ada91cb3 100644
--- a/vala/valaconstant.vala
+++ b/vala/valaconstant.vala
@@ -202,6 +202,9 @@ public class Vala.Constant : Symbol {
                } else if (type is ArrayType) {
                        unowned ArrayType array_type = (ArrayType) type;
                        return check_const_type (array_type.element_type, context);
+               } else if (type is DelegateType) {
+                       unowned DelegateType delegate_type = (DelegateType) type;
+                       return !delegate_type.type_symbol.get_attribute_bool ("CCode", "has_target", true);
                } else if (type.type_symbol != null) {
                        return type.type_symbol.is_subtype_of (context.analyzer.string_type.type_symbol);
                } else {


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