[vala/0.54] codegen: Use CCodeConstant for member access of constant symbol



commit a68d35d32004818447e3dc70f8fee77f67cf9f75
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sat Nov 6 18:55:38 2021 +0100

    codegen: Use CCodeConstant for member access of constant symbol
    
    This broke assumptions in CCodeBaseModule.is_constant_ccode_expression()

 codegen/valaccodememberaccessmodule.vala    |  2 +-
 tests/Makefile.am                           |  1 +
 tests/control-flow/switch-string.c-expected | 15 +++++++----
 tests/semantic/field-constant.c-expected    | 41 +++++++++++++++++++++++++++++
 tests/semantic/field-constant.vala          |  5 ++++
 5 files changed, 58 insertions(+), 6 deletions(-)
---
diff --git a/codegen/valaccodememberaccessmodule.vala b/codegen/valaccodememberaccessmodule.vala
index 4e411c73e..87ed58390 100644
--- a/codegen/valaccodememberaccessmodule.vala
+++ b/codegen/valaccodememberaccessmodule.vala
@@ -168,7 +168,7 @@ public abstract class Vala.CCodeMemberAccessModule : CCodeControlFlowModule {
                                }
                                set_cvalue (expr, new CCodeConstant ("\"%s\"".printf (s)));
                        } else {
-                               set_cvalue (expr, new CCodeIdentifier (get_ccode_name (c)));
+                               set_cvalue (expr, new CCodeConstant (get_ccode_name (c)));
                        }
 
                        if (array_type != null) {
diff --git a/tests/Makefile.am b/tests/Makefile.am
index a453523fd..f6a6bc3fa 100644
--- a/tests/Makefile.am
+++ b/tests/Makefile.am
@@ -1028,6 +1028,7 @@ TESTS = \
        semantic/errordomain-empty.test \
        semantic/field-accessibility.test \
        semantic/field-compact-static.test \
+       semantic/field-constant.vala \
        semantic/field-external.test \
        semantic/field-incompatible.test \
        semantic/field-interface.test \
diff --git a/tests/control-flow/switch-string.c-expected b/tests/control-flow/switch-string.c-expected
index 0711523ed..6a1a1b015 100644
--- a/tests/control-flow/switch-string.c-expected
+++ b/tests/control-flow/switch-string.c-expected
@@ -37,26 +37,29 @@ foo (void)
        const gchar* _tmp1_;
        GQuark _tmp3_ = 0U;
        static GQuark _tmp2_label0 = 0;
+       static GQuark _tmp2_label1 = 0;
+       static GQuark _tmp2_label2 = 0;
+       static GQuark _tmp2_label3 = 0;
        gchar* result = NULL;
        _tmp0_ = g_strdup ("foo");
        foo = _tmp0_;
        _tmp1_ = foo;
        _tmp3_ = (NULL == _tmp1_) ? 0 : g_quark_from_string (_tmp1_);
-       if (_tmp3_ == g_quark_from_string (FOO)) {
+       if (_tmp3_ == ((0 != _tmp2_label0) ? _tmp2_label0 : (_tmp2_label0 = g_quark_from_static_string 
(FOO)))) {
                switch (0) {
                        default:
                        {
                                break;
                        }
                }
-       } else if ((_tmp3_ == g_quark_from_string (BAR)) || (_tmp3_ == g_quark_from_string (MANAM))) {
+       } else if ((_tmp3_ == ((0 != _tmp2_label1) ? _tmp2_label1 : (_tmp2_label1 = 
g_quark_from_static_string (BAR)))) || (_tmp3_ == ((0 != _tmp2_label2) ? _tmp2_label2 : (_tmp2_label2 = 
g_quark_from_static_string (MANAM))))) {
                switch (0) {
                        default:
                        {
                                g_assert_not_reached ();
                        }
                }
-       } else if (_tmp3_ == ((0 != _tmp2_label0) ? _tmp2_label0 : (_tmp2_label0 = g_quark_from_static_string 
("minim")))) {
+       } else if (_tmp3_ == ((0 != _tmp2_label3) ? _tmp2_label3 : (_tmp2_label3 = g_quark_from_static_string 
("minim")))) {
                switch (0) {
                        default:
                        {
@@ -85,20 +88,22 @@ bar (void)
        gchar* _tmp1_;
        GQuark _tmp3_ = 0U;
        static GQuark _tmp2_label0 = 0;
+       static GQuark _tmp2_label1 = 0;
+       static GQuark _tmp2_label2 = 0;
        gchar* _tmp4_;
        gchar* result = NULL;
        _tmp0_ = get_bar ();
        _tmp1_ = _tmp0_;
        _tmp3_ = (NULL == _tmp1_) ? 0 : g_quark_from_string (_tmp1_);
        g_free (_tmp1_);
-       if (_tmp3_ == g_quark_from_string (BAR)) {
+       if (_tmp3_ == ((0 != _tmp2_label0) ? _tmp2_label0 : (_tmp2_label0 = g_quark_from_static_string 
(BAR)))) {
                switch (0) {
                        default:
                        {
                                break;
                        }
                }
-       } else if ((_tmp3_ == g_quark_from_string (FOO)) || (_tmp3_ == ((0 != _tmp2_label0) ? _tmp2_label0 : 
(_tmp2_label0 = g_quark_from_static_string ("minim"))))) {
+       } else if ((_tmp3_ == ((0 != _tmp2_label1) ? _tmp2_label1 : (_tmp2_label1 = 
g_quark_from_static_string (FOO)))) || (_tmp3_ == ((0 != _tmp2_label2) ? _tmp2_label2 : (_tmp2_label2 = 
g_quark_from_static_string ("minim"))))) {
                switch (0) {
                        default:
                        {
diff --git a/tests/semantic/field-constant.c-expected b/tests/semantic/field-constant.c-expected
new file mode 100644
index 000000000..84d863ea1
--- /dev/null
+++ b/tests/semantic/field-constant.c-expected
@@ -0,0 +1,41 @@
+/* semantic_field_constant.c generated by valac, the Vala compiler
+ * generated from semantic_field_constant.vala, do not modify */
+
+#include <glib.h>
+#include <float.h>
+#include <math.h>
+
+#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
+
+#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 gdouble foo;
+gdouble foo = G_PI;
+
+static void _vala_main (void);
+
+static void
+_vala_main (void)
+{
+       _vala_assert (foo == G_PI, "foo == GLib.Math.PI");
+}
+
+int
+main (int argc,
+      char ** argv)
+{
+       _vala_main ();
+       return 0;
+}
+
diff --git a/tests/semantic/field-constant.vala b/tests/semantic/field-constant.vala
new file mode 100644
index 000000000..43162dc38
--- /dev/null
+++ b/tests/semantic/field-constant.vala
@@ -0,0 +1,5 @@
+double foo = GLib.Math.PI;
+
+void main () {
+       assert (foo == GLib.Math.PI);
+}


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