svn:eol-style native, svn:keywords Author Date Id Revision
[fw/sdcc] / support / cpp2 / opt-functions.awk
index f392060c1f25e7f4b218b7c4ef7834a71e2755d3..3e4c80596060f9d4ff8c5e57fb6190d81492aa84 100644 (file)
-#  Copyright (C) 2003,2004 Free Software Foundation, Inc.\r
-#  Contributed by Kelley Cook, June 2004.\r
-#  Original code from Neil Booth, May 2003.\r
-#\r
-# This program is free software; you can redistribute it and/or modify it\r
-# under the terms of the GNU General Public License as published by the\r
-# Free Software Foundation; either version 2, or (at your option) any\r
-# later version.\r
-# \r
-# This program is distributed in the hope that it will be useful,\r
-# but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-# GNU General Public License for more details.\r
-# \r
-# You should have received a copy of the GNU General Public License\r
-# along with this program; if not, write to the Free Software\r
-# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.\r
-\r
-# Some common subroutines for use by opt[ch]-gen.awk.\r
-\r
-# Return nonzero if FLAGS contains a flag matching REGEX.\r
-function flag_set_p(regex, flags)\r
-{\r
-       return (" " flags " ") ~ (" " regex " ")\r
-}\r
-\r
-# Return STRING if FLAGS contains a flag matching regexp REGEX,\r
-# otherwise return the empty string.\r
-function test_flag(regex, flags, string)\r
-{\r
-       if (flag_set_p(regex, flags))\r
-               return string\r
-       return ""\r
-}\r
-\r
-# If FLAGS contains a "NAME(...argument...)" flag, return the value\r
-# of the argument.  Return the empty string otherwise.\r
-function opt_args(name, flags)\r
-{\r
-       flags = " " flags\r
-       if (flags !~ " " name "\\(")\r
-               return ""\r
-       sub(".* " name "\\(", "", flags)\r
-       sub("\\).*", "", flags)\r
-\r
-       return flags\r
-}\r
-\r
-# Return the Nth comma-separated element of S.  Return the empty string\r
-# if S does not contain N elements.\r
-function nth_arg(n, s)\r
-{\r
-       while (n-- > 0) {\r
-               if (s !~ ",")\r
-                       return ""\r
-               sub("[^,]*, *", "", s)\r
-       }\r
-       sub(",.*", "", s)\r
-       return s\r
-}\r
-\r
-# Return a bitmask of CL_* values for option flags FLAGS.\r
-function switch_flags (flags)\r
-{\r
-       result = "0"\r
-       for (j = 0; j < n_langs; j++) {\r
-               regex = langs[j]\r
-               gsub ( "\\+", "\\+", regex )\r
-               result = result test_flag(regex, flags, " | " macros[j])\r
-       }\r
-       result = result \\r
-         test_flag("Common", flags, " | CL_COMMON") \\r
-         test_flag("Target", flags, " | CL_TARGET") \\r
-         test_flag("Joined", flags, " | CL_JOINED") \\r
-         test_flag("JoinedOrMissing", flags, " | CL_JOINED | CL_MISSING_OK") \\r
-         test_flag("Separate", flags, " | CL_SEPARATE") \\r
-         test_flag("RejectNegative", flags, " | CL_REJECT_NEGATIVE") \\r
-         test_flag("UInteger", flags, " | CL_UINTEGER") \\r
-         test_flag("Undocumented", flags,  " | CL_UNDOCUMENTED") \\r
-         test_flag("Report", flags, " | CL_REPORT")\r
-       sub( "^0 \\| ", "", result )\r
-       return result\r
-}\r
-\r
-# If FLAGS includes a Var flag, return the name of the variable it specifies.\r
-# Return the empty string otherwise.\r
-function var_name(flags)\r
-{\r
-       return nth_arg(0, opt_args("Var", flags))\r
-}\r
-\r
-# Return true if the option described by FLAGS has a globally-visible state.\r
-function global_state_p(flags)\r
-{\r
-       return (var_name(flags) != "" \\r
-               || opt_args("Mask", flags) != "" \\r
-               || opt_args("InverseMask", flags) != "")\r
-}\r
-\r
-# Return true if the option described by FLAGS must have some state\r
-# associated with it.\r
-function needs_state_p(flags)\r
-{\r
-       return flag_set_p("Target", flags)\r
-}\r
-\r
-# If FLAGS describes an option that needs a static state variable,\r
-# return the name of that variable, otherwise return "".  NAME is\r
-# the name of the option.\r
-function static_var(name, flags)\r
-{\r
-       if (global_state_p(flags) || !needs_state_p(flags))\r
-               return ""\r
-       gsub ("[^A-Za-z0-9]", "_", name)\r
-       return "VAR_" name\r
-}\r
-\r
-# Return the type of variable that should be associated with the given flags.\r
-function var_type(flags)\r
-{\r
-       if (!flag_set_p("Joined.*", flags))\r
-               return "int "\r
-       else if (flag_set_p("UInteger", flags))\r
-               return "int "\r
-       else\r
-               return "const char *"\r
-}\r
-\r
-# Given that an option has flags FLAGS, return an initializer for the\r
-# "var_cond" and "var_value" fields of its cl_options[] entry.\r
-function var_set(flags)\r
-{\r
-       s = nth_arg(1, opt_args("Var", flags))\r
-       if (s != "")\r
-               return "CLVC_EQUAL, " s\r
-       s = opt_args("Mask", flags);\r
-       if (s != "") {\r
-               vn = var_name(flags);\r
-               if (vn)\r
-                       return "CLVC_BIT_SET, OPTION_MASK_" s\r
-               else\r
-                       return "CLVC_BIT_SET, MASK_" s\r
-       }\r
-       s = nth_arg(0, opt_args("InverseMask", flags));\r
-       if (s != "") {\r
-               vn = var_name(flags);\r
-               if (vn)\r
-                       return "CLVC_BIT_CLEAR, OPTION_MASK_" s\r
-               else\r
-                       return "CLVC_BIT_CLEAR, MASK_" s\r
-       }\r
-       if (var_type(flags) == "const char *")\r
-               return "CLVC_STRING, 0"\r
-       return "CLVC_BOOLEAN, 0"\r
-}\r
-\r
-# Given that an option called NAME has flags FLAGS, return an initializer\r
-# for the "flag_var" field of its cl_options[] entry.\r
-function var_ref(name, flags)\r
-{\r
-       name = var_name(flags) static_var(name, flags)\r
-       if (name != "")\r
-               return "&" name\r
-       if (opt_args("Mask", flags) != "")\r
-               return "&target_flags"\r
-       if (opt_args("InverseMask", flags) != "")\r
-               return "&target_flags"\r
-       return "0"\r
-}\r
+#  Copyright (C) 2003,2004 Free Software Foundation, Inc.
+#  Contributed by Kelley Cook, June 2004.
+#  Original code from Neil Booth, May 2003.
+#
+# This program is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by the
+# Free Software Foundation; either version 2, or (at your option) any
+# later version.
+# 
+# This program 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 General Public License for more details.
+# 
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+
+# Some common subroutines for use by opt[ch]-gen.awk.
+
+# Return nonzero if FLAGS contains a flag matching REGEX.
+function flag_set_p(regex, flags)
+{
+       return (" " flags " ") ~ (" " regex " ")
+}
+
+# Return STRING if FLAGS contains a flag matching regexp REGEX,
+# otherwise return the empty string.
+function test_flag(regex, flags, string)
+{
+       if (flag_set_p(regex, flags))
+               return string
+       return ""
+}
+
+# If FLAGS contains a "NAME(...argument...)" flag, return the value
+# of the argument.  Return the empty string otherwise.
+function opt_args(name, flags)
+{
+       flags = " " flags
+       if (flags !~ " " name "\\(")
+               return ""
+       sub(".* " name "\\(", "", flags)
+       sub("\\).*", "", flags)
+
+       return flags
+}
+
+# Return the Nth comma-separated element of S.  Return the empty string
+# if S does not contain N elements.
+function nth_arg(n, s)
+{
+       while (n-- > 0) {
+               if (s !~ ",")
+                       return ""
+               sub("[^,]*, *", "", s)
+       }
+       sub(",.*", "", s)
+       return s
+}
+
+# Return a bitmask of CL_* values for option flags FLAGS.
+function switch_flags (flags)
+{
+       result = "0"
+       for (j = 0; j < n_langs; j++) {
+               regex = langs[j]
+               gsub ( "\\+", "\\+", regex )
+               result = result test_flag(regex, flags, " | " macros[j])
+       }
+       result = result \
+         test_flag("Common", flags, " | CL_COMMON") \
+         test_flag("Target", flags, " | CL_TARGET") \
+         test_flag("Joined", flags, " | CL_JOINED") \
+         test_flag("JoinedOrMissing", flags, " | CL_JOINED | CL_MISSING_OK") \
+         test_flag("Separate", flags, " | CL_SEPARATE") \
+         test_flag("RejectNegative", flags, " | CL_REJECT_NEGATIVE") \
+         test_flag("UInteger", flags, " | CL_UINTEGER") \
+         test_flag("Undocumented", flags,  " | CL_UNDOCUMENTED") \
+         test_flag("Report", flags, " | CL_REPORT")
+       sub( "^0 \\| ", "", result )
+       return result
+}
+
+# If FLAGS includes a Var flag, return the name of the variable it specifies.
+# Return the empty string otherwise.
+function var_name(flags)
+{
+       return nth_arg(0, opt_args("Var", flags))
+}
+
+# Return true if the option described by FLAGS has a globally-visible state.
+function global_state_p(flags)
+{
+       return (var_name(flags) != "" \
+               || opt_args("Mask", flags) != "" \
+               || opt_args("InverseMask", flags) != "")
+}
+
+# Return true if the option described by FLAGS must have some state
+# associated with it.
+function needs_state_p(flags)
+{
+       return flag_set_p("Target", flags)
+}
+
+# If FLAGS describes an option that needs a static state variable,
+# return the name of that variable, otherwise return "".  NAME is
+# the name of the option.
+function static_var(name, flags)
+{
+       if (global_state_p(flags) || !needs_state_p(flags))
+               return ""
+       gsub ("[^A-Za-z0-9]", "_", name)
+       return "VAR_" name
+}
+
+# Return the type of variable that should be associated with the given flags.
+function var_type(flags)
+{
+       if (!flag_set_p("Joined.*", flags))
+               return "int "
+       else if (flag_set_p("UInteger", flags))
+               return "int "
+       else
+               return "const char *"
+}
+
+# Given that an option has flags FLAGS, return an initializer for the
+# "var_cond" and "var_value" fields of its cl_options[] entry.
+function var_set(flags)
+{
+       s = nth_arg(1, opt_args("Var", flags))
+       if (s != "")
+               return "CLVC_EQUAL, " s
+       s = opt_args("Mask", flags);
+       if (s != "") {
+               vn = var_name(flags);
+               if (vn)
+                       return "CLVC_BIT_SET, OPTION_MASK_" s
+               else
+                       return "CLVC_BIT_SET, MASK_" s
+       }
+       s = nth_arg(0, opt_args("InverseMask", flags));
+       if (s != "") {
+               vn = var_name(flags);
+               if (vn)
+                       return "CLVC_BIT_CLEAR, OPTION_MASK_" s
+               else
+                       return "CLVC_BIT_CLEAR, MASK_" s
+       }
+       if (var_type(flags) == "const char *")
+               return "CLVC_STRING, 0"
+       return "CLVC_BOOLEAN, 0"
+}
+
+# Given that an option called NAME has flags FLAGS, return an initializer
+# for the "flag_var" field of its cl_options[] entry.
+function var_ref(name, flags)
+{
+       name = var_name(flags) static_var(name, flags)
+       if (name != "")
+               return "&" name
+       if (opt_args("Mask", flags) != "")
+               return "&target_flags"
+       if (opt_args("InverseMask", flags) != "")
+               return "&target_flags"
+       return "0"
+}