[vala/staging: 1/2] ccode: Use __attribute__ replacements of G_GNUC_* for posix profile




commit 52b14a943cde150ff429162d663e1bcafbbf31e9
Author: Rico Tzschichholz <ricotz ubuntu com>
Date:   Sun Nov 15 16:58:15 2020 +0100

    ccode: Use __attribute__ replacements of G_GNUC_* for posix profile

 ccode/Makefile.am                      |  1 +
 ccode/valaccode.vala                   | 59 ++++++++++++++++++++++++++++++++++
 ccode/valaccodedeclaration.vala        |  4 +--
 ccode/valaccodeenum.vala               |  2 +-
 ccode/valaccodeenumvalue.vala          |  2 +-
 ccode/valaccodefunction.vala           | 16 ++++-----
 ccode/valaccodefunctiondeclarator.vala |  8 ++---
 ccode/valaccodestruct.vala             |  2 +-
 ccode/valaccodetypedefinition.vala     |  2 +-
 codegen/valaccodebasemodule.vala       |  2 ++
 10 files changed, 80 insertions(+), 18 deletions(-)
---
diff --git a/ccode/Makefile.am b/ccode/Makefile.am
index a5defb0bb..9ba660760 100644
--- a/ccode/Makefile.am
+++ b/ccode/Makefile.am
@@ -17,6 +17,7 @@ noinst_LTLIBRARIES = \
        $(NULL)
 
 libvalaccode_la_VALASOURCES = \
+       valaccode.vala \
        valaccodeassignment.vala \
        valaccodebinaryexpression.vala \
        valaccodeblock.vala \
diff --git a/ccode/valaccode.vala b/ccode/valaccode.vala
new file mode 100644
index 000000000..07f4bed2a
--- /dev/null
+++ b/ccode/valaccode.vala
@@ -0,0 +1,59 @@
+/* valaccode.vala
+ *
+ * Copyright (C) 2020  Rico Tzschichholz
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
+ *
+ * Author:
+ *     Rico Tzschichholz <ricotz ubuntu com>
+ */
+
+namespace Vala {
+       public unowned string GNUC_CONST;
+       public unowned string GNUC_DEPRECATED;
+       public unowned string GNUC_FORMAT;
+       public unowned string GNUC_INTERNAL;
+       public unowned string GNUC_NO_INLINE;
+       public unowned string GNUC_PRINTF;
+       public unowned string GNUC_SCANF;
+       public unowned string GNUC_UNUSED;
+
+       public static void ccode_init (Vala.Profile profile) {
+               switch (profile) {
+               case Vala.Profile.GOBJECT:
+                       GNUC_CONST = " G_GNUC_CONST ";
+                       GNUC_DEPRECATED = " G_GNUC_DEPRECATED ";
+                       GNUC_FORMAT = " G_GNUC_FORMAT(%d) ";
+                       GNUC_INTERNAL = " G_GNUC_INTERNAL ";
+                       GNUC_NO_INLINE = " G_GNUC_NO_INLINE ";
+                       GNUC_PRINTF = "  G_GNUC_PRINTF(%d,%d) ";
+                       GNUC_SCANF = " G_GNUC_SCANF(%d,%d) ";
+                       GNUC_UNUSED = " G_GNUC_UNUSED ";
+                       break;
+               case Vala.Profile.POSIX:
+                       GNUC_CONST = " __attribute__((__const__)) ";
+                       GNUC_DEPRECATED = " __attribute__((__deprecated__)) ";
+                       GNUC_FORMAT = " __attribute__((__format_arg__ (arg_idx))) ";
+                       GNUC_INTERNAL = " __attribute__((visibility(\"hidden\"))) ";
+                       GNUC_NO_INLINE = " __attribute__((noinline)) ";
+                       GNUC_PRINTF = " __attribute__((__format__ (__printf__, %d, %d))) ";
+                       GNUC_SCANF = " __attribute__((__format__ (__scanf__, %d, %d))) ";
+                       GNUC_UNUSED = " __attribute__((__unused__)) ";
+                       break;
+               default:
+                       assert_not_reached ();
+               }
+       }
+}
diff --git a/ccode/valaccodedeclaration.vala b/ccode/valaccodedeclaration.vala
index eb69c6a61..6295477b3 100644
--- a/ccode/valaccodedeclaration.vala
+++ b/ccode/valaccodedeclaration.vala
@@ -69,7 +69,7 @@ public class Vala.CCodeDeclaration : CCodeStatement {
                        // combined declaration and initialization for static and extern variables
                        writer.write_indent (line);
                        if ((modifiers & CCodeModifiers.INTERNAL) != 0) {
-                               writer.write_string ("G_GNUC_INTERNAL ");
+                               writer.write_string (GNUC_INTERNAL);
                        }
                        if ((modifiers & CCodeModifiers.STATIC) != 0) {
                                writer.write_string ("static ");
@@ -122,7 +122,7 @@ public class Vala.CCodeDeclaration : CCodeStatement {
                }
 
                if (CCodeModifiers.DEPRECATED in modifiers) {
-                       writer.write_string (" G_GNUC_DEPRECATED");
+                       writer.write_string (GNUC_DEPRECATED);
                }
 
                writer.write_string (";");
diff --git a/ccode/valaccodeenum.vala b/ccode/valaccodeenum.vala
index c94ad02ae..d59db0e5a 100644
--- a/ccode/valaccodeenum.vala
+++ b/ccode/valaccodeenum.vala
@@ -71,7 +71,7 @@ public class Vala.CCodeEnum : CCodeNode {
                        writer.write_string (name);
                }
                if (CCodeModifiers.DEPRECATED in modifiers) {
-                       writer.write_string (" G_GNUC_DEPRECATED");
+                       writer.write_string (GNUC_DEPRECATED);
                }
                writer.write_string (";");
                writer.write_newline ();
diff --git a/ccode/valaccodeenumvalue.vala b/ccode/valaccodeenumvalue.vala
index f11326f56..23d37c492 100644
--- a/ccode/valaccodeenumvalue.vala
+++ b/ccode/valaccodeenumvalue.vala
@@ -46,7 +46,7 @@ public class Vala.CCodeEnumValue : CCodeNode {
                if (CCodeModifiers.DEPRECATED in modifiers) {
                        // FIXME Requires GCC 6.0 to work at this place
                        // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=47043
-                       //writer.write_string (" G_GNUC_DEPRECATED");
+                       //writer.write_string (GNUC_DEPRECATED);
                }
                if (value != null) {
                        writer.write_string (" = ");
diff --git a/ccode/valaccodefunction.vala b/ccode/valaccodefunction.vala
index 7d29de9fd..b1a529711 100644
--- a/ccode/valaccodefunction.vala
+++ b/ccode/valaccodefunction.vala
@@ -109,10 +109,10 @@ public class Vala.CCodeFunction : CCodeNode {
        public override void write (CCodeWriter writer) {
                writer.write_indent (line);
                if (CCodeModifiers.INTERNAL in modifiers) {
-                       writer.write_string ("G_GNUC_INTERNAL ");
+                       writer.write_string (GNUC_INTERNAL);
                }
                if (!is_declaration && CCodeModifiers.NO_INLINE in modifiers) {
-                       writer.write_string ("G_GNUC_NO_INLINE ");
+                       writer.write_string (GNUC_NO_INLINE);
                }
                if (CCodeModifiers.STATIC in modifiers) {
                        writer.write_string ("static ");
@@ -159,24 +159,24 @@ public class Vala.CCodeFunction : CCodeNode {
 
                if (is_declaration) {
                        if (CCodeModifiers.DEPRECATED in modifiers) {
-                               writer.write_string (" G_GNUC_DEPRECATED");
+                               writer.write_string (GNUC_DEPRECATED);
                        }
 
                        if (CCodeModifiers.PRINTF in modifiers) {
                                format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : 
args_index);
-                               writer.write_string (" G_GNUC_PRINTF(%d,%d)".printf (format_arg_index, 
args_index + 1));
+                               writer.write_string (GNUC_PRINTF.printf (format_arg_index, args_index + 1));
                        } else if (CCodeModifiers.SCANF in modifiers) {
                                format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : 
args_index);
-                               writer.write_string (" G_GNUC_SCANF(%d,%d)".printf (format_arg_index, 
args_index + 1));
+                               writer.write_string (GNUC_SCANF.printf (format_arg_index, args_index + 1));
                        } else if (format_arg_index >= 0) {
-                               writer.write_string (" G_GNUC_FORMAT(%d)".printf (format_arg_index + 1));
+                               writer.write_string (GNUC_FORMAT.printf (format_arg_index + 1));
                        }
 
                        if (CCodeModifiers.CONST in modifiers) {
-                               writer.write_string (" G_GNUC_CONST");
+                               writer.write_string (GNUC_CONST);
                        }
                        if (CCodeModifiers.UNUSED in modifiers) {
-                               writer.write_string (" G_GNUC_UNUSED");
+                               writer.write_string (GNUC_UNUSED);
                        }
 
                        if (CCodeModifiers.CONSTRUCTOR in modifiers) {
diff --git a/ccode/valaccodefunctiondeclarator.vala b/ccode/valaccodefunctiondeclarator.vala
index c67a087a1..e9d634411 100644
--- a/ccode/valaccodefunctiondeclarator.vala
+++ b/ccode/valaccodefunctiondeclarator.vala
@@ -81,17 +81,17 @@ public class Vala.CCodeFunctionDeclarator : CCodeDeclarator {
                writer.write_string (")");
 
                if (CCodeModifiers.DEPRECATED in modifiers) {
-                       writer.write_string (" G_GNUC_DEPRECATED");
+                       writer.write_string (GNUC_DEPRECATED);
                }
 
                if (CCodeModifiers.PRINTF in modifiers) {
                        format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : args_index);
-                       writer.write_string (" G_GNUC_PRINTF(%d,%d)".printf (format_arg_index, args_index + 
1));
+                       writer.write_string (GNUC_PRINTF.printf (format_arg_index, args_index + 1));
                } else if (CCodeModifiers.SCANF in modifiers) {
                        format_arg_index = (format_arg_index >= 0 ? format_arg_index + 1 : args_index);
-                       writer.write_string (" G_GNUC_SCANF(%d,%d)".printf (format_arg_index, args_index + 
1));
+                       writer.write_string (GNUC_SCANF.printf (format_arg_index, args_index + 1));
                } else if (format_arg_index >= 0) {
-                       writer.write_string (" G_GNUC_FORMAT(%d)".printf (format_arg_index + 1));
+                       writer.write_string (GNUC_FORMAT.printf (format_arg_index + 1));
                }
        }
 }
diff --git a/ccode/valaccodestruct.vala b/ccode/valaccodestruct.vala
index 50af6a2bf..9630a37c4 100644
--- a/ccode/valaccodestruct.vala
+++ b/ccode/valaccodestruct.vala
@@ -71,7 +71,7 @@ public class Vala.CCodeStruct : CCodeNode {
 
                writer.write_end_block ();
                if (CCodeModifiers.DEPRECATED in modifiers) {
-                       writer.write_string (" G_GNUC_DEPRECATED");
+                       writer.write_string (GNUC_DEPRECATED);
                }
                writer.write_string (";");
                writer.write_newline ();
diff --git a/ccode/valaccodetypedefinition.vala b/ccode/valaccodetypedefinition.vala
index f476ad12b..39f014a9c 100644
--- a/ccode/valaccodetypedefinition.vala
+++ b/ccode/valaccodetypedefinition.vala
@@ -55,7 +55,7 @@ public class Vala.CCodeTypeDefinition : CCodeNode {
                declarator.write_declaration (writer);
 
                if (CCodeModifiers.DEPRECATED in modifiers) {
-                       writer.write_string (" G_GNUC_DEPRECATED");
+                       writer.write_string (GNUC_DEPRECATED);
                }
 
                writer.write_string (";");
diff --git a/codegen/valaccodebasemodule.vala b/codegen/valaccodebasemodule.vala
index 9a801e369..469282a0a 100644
--- a/codegen/valaccodebasemodule.vala
+++ b/codegen/valaccodebasemodule.vala
@@ -463,6 +463,8 @@ public abstract class Vala.CCodeBaseModule : CodeGenerator {
        public override void emit (CodeContext context) {
                this.context = context;
 
+               ccode_init (context.profile);
+
                root_symbol = context.root;
 
                bool_type = new BooleanType ((Struct) root_symbol.scope.lookup ("bool"));


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