1 /* Muscle table manager for Bison,
2 
3    Copyright (C) 2001-2003, 2006-2015, 2018-2021 Free Software
4    Foundation, Inc.
5 
6    This file is part of Bison, the GNU Compiler Compiler.
7 
8    This program is free software: you can redistribute it and/or modify
9    it under the terms of the GNU General Public License as published by
10    the Free Software Foundation, either version 3 of the License, or
11    (at your option) any later version.
12 
13    This program is distributed in the hope that it will be useful,
14    but WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16    GNU General Public License for more details.
17 
18    You should have received a copy of the GNU General Public License
19    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
20 
21 #ifndef MUSCLE_TAB_H_
22 # define MUSCLE_TAB_H_
23 
24 # include <quotearg.h>
25 
26 # include "location.h"
27 
28 /* The kind of value associated to this muscle, depending on the
29    syntax of the value: keyword (no delimiter, e.g., true), string
30    (double quotes, e.g., "foo.h"), or code (braces, e.g., {int}).  */
31 typedef enum
32 {
33   muscle_code,
34   muscle_keyword,
35   muscle_string
36 } muscle_kind;
37 
38 /* Conversion from string.  */
39 muscle_kind muscle_kind_new (char const *k);
40 
41 /* Conversion to string.  */
42 char const *muscle_kind_string (muscle_kind k);
43 
44 
45 /* Create the MUSCLE_TABLE, and initialize it with default values.
46    Also set up the MUSCLE_OBSTACK.  */
47 void muscle_init (void);
48 
49 /* Insert (KEY, VALUE).  If KEY already existed, overwrite the
50    previous value.  Otherwise create as a muscle_string type.  */
51 void muscle_insert (char const *key, char const *value);
52 
53 /* Find the value of muscle KEY.  Unlike MUSCLE_FIND, this is always
54    reliable to determine whether KEY has a value.  */
55 char const *muscle_find_const (char const *key);
56 
57 /* Find the value of muscle KEY.  Abort if muscle_insert was invoked
58    more recently than muscle_grow for KEY since muscle_find can't
59    return a char const *.  */
60 char *muscle_find (char const *key);
61 
62 /* Free all the memory consumed by the muscle machinery only.  */
63 void muscle_free (void);
64 
65 
66 /* An obstack dedicated to receive muscle keys and values.  */
67 extern struct obstack muscle_obstack;
68 
69 # define MUSCLE_INSERT_BOOL(Key, Value)         \
70   do {                                          \
71     int v__ = Value;                            \
72     MUSCLE_INSERT_INT (Key, v__);               \
73   } while (0)
74 
75 # define MUSCLE_INSERTF(Key, Format, Value)                     \
76   do {                                                          \
77     obstack_printf (&muscle_obstack, Format, Value);            \
78     muscle_insert (Key, obstack_finish0 (&muscle_obstack));     \
79   } while (0)
80 
81 # define MUSCLE_INSERT_INT(Key, Value)          \
82   MUSCLE_INSERTF (Key, "%d", Value)
83 
84 # define MUSCLE_INSERT_LONG_INT(Key, Value)     \
85   MUSCLE_INSERTF (Key, "%ld", Value)
86 
87 /* Key -> Value, but don't apply escaping to Value. */
88 # define MUSCLE_INSERT_STRING_RAW(Key, Value)   \
89   MUSCLE_INSERTF (Key, "%s", Value)
90 
91 /* Key -> Value, applying M4 escaping to Value. */
92 # define MUSCLE_INSERT_STRING(Key, Value)                       \
93   do {                                                          \
94     obstack_escape (&muscle_obstack, Value);                    \
95     muscle_insert (Key, obstack_finish0 (&muscle_obstack));     \
96   } while (0)
97 
98 /* Key -> Value, applying C escaping to Value (and then M4). */
99 # define MUSCLE_INSERT_C_STRING(Key, Value)                     \
100   MUSCLE_INSERT_STRING (Key, quotearg_style (c_quoting_style, Value))
101 
102 
103 /* Append VALUE to the current value of KEY, using muscle_grow.  But
104    in addition, issue a synchronization line for the location LOC.
105    Be sure to append on a new line.  */
106 void muscle_code_grow (const char *key, const char *value, location loc);
107 
108 
109 /* MUSCLE is an M4 list of pairs.  Create or extend it with the pair
110    (A1, A2) after escaping both values with digraphs.  Note that because the
111    muscle values are output *double* quoted, one needs to strip the first level
112    of quotes to reach the list itself.  */
113 void muscle_pair_list_grow (const char *muscle,
114                             const char *a1, const char *a2);
115 
116 /* Grow KEY for the occurrence of the name USER_NAME at LOC appropriately for
117    use with b4_check_user_names in ../data/bison.m4.  USER_NAME is not escaped
118    with digraphs, so it must not contain '[' or ']'.  */
119 void muscle_user_name_list_grow (char const *key, char const *user_name,
120                                  location loc);
121 
122 /* In the format '[[file_name:line.column]], [[file_name:line.column]]',
123    append LOC to MUSCLE.  Use digraphs for special characters in each
124    file name.  */
125 void muscle_location_grow (char const *key, location loc);
126 
127 /* Indicates whether a variable's value was specified with -D/--define, with
128    -F/--force-define, or in the grammar file.  */
129 typedef enum {
130   MUSCLE_PERCENT_DEFINE_D = 0,
131   MUSCLE_PERCENT_DEFINE_F,
132   MUSCLE_PERCENT_DEFINE_GRAMMAR_FILE
133 } muscle_percent_define_how;
134 
135 /* Define the muscles for %define variable VARIABLE with VALUE specified
136    at VARIABLE_LOC in the manner HOW unless it was specified in the
137    grammar file while the previous definition for VARIABLE was specified
138    with -F/--force-define.  Complain if a previous definition is being
139    overridden and the new definition is specified in the grammar file.
140    (These rules support the documented behavior as long as command-line
141    definitions are processed before grammar file definitions.)  Record
142    this as a user occurrence of VARIABLE by invoking
143    muscle_user_name_list_grow.  */
144 void muscle_percent_define_insert (char const *variable, location variable_loc,
145                                    muscle_kind kind,
146                                    char const *value,
147                                    muscle_percent_define_how how);
148 
149 /* Make sure that VARIABLE is set to the boolean VALUE.  Warn on mismatches
150    only, but accept repeated declaration.  Used for backward compatibility
151    between old directives such as %pure-parser, and the recommended use of
152    variables (%define api.pure).   */
153 void muscle_percent_define_ensure (char const *variable, location variable_loc,
154                                    bool value);
155 
156 /* Mimic b4_percent_define_get in ../data/bison.m4 exactly.  That is, if the
157    %define variable VARIABLE is defined, return its value.  Otherwise, return
158    the empty string.  Also, record Bison's usage of VARIABLE by defining
159    b4_percent_define_bison_variables(VARIABLE).  The caller is responsible for
160    freeing the memory of the returned string.  */
161 char *muscle_percent_define_get (char const *variable);
162 
163 /* Mimic muscle_percent_define_get_loc in ../data/bison.m4 exactly.  That is,
164    if the %define variable VARIABLE is undefined, complain fatally since that's
165    a Bison error.  Otherwise, return its definition location in a form
166    appropriate for the first argument of warn_at, complain_at, or fatal_at.
167    Don't record this as a Bison usage of VARIABLE as there's no reason to
168    suspect that the user-supplied value has yet influenced the output.  */
169 location muscle_percent_define_get_loc (char const *variable);
170 
171 /* Mimic muscle_percent_define_get_syncline in ../data/bison.m4 exactly.  That
172    is, if the %define variable VARIABLE is undefined, complain fatally since
173    that's a Bison error.  Otherwise, return its definition location as a
174    b4_syncline invocation.  Don't record this as a Bison usage of VARIABLE as
175    there's no reason to suspect that the user-supplied value has yet influenced
176    the output.  */
177 char const *muscle_percent_define_get_syncline (char const *variable);
178 
179 /* Mimic b4_percent_define_ifdef in ../data/bison.m4 exactly.  That is, if the
180    %define variable VARIABLE is defined, return true.  Otherwise, return false.
181    Also, record Bison's usage of VARIABLE by defining
182    b4_percent_define_bison_variables(VARIABLE).  */
183 bool muscle_percent_define_ifdef (char const *variable);
184 
185 /* Mimic b4_percent_define_flag_if in ../data/bison.m4 exactly.  That is, if
186    the %define variable VARIABLE is defined to "" or "true", return true.  If
187    it is defined to "false", return false.  Complain if it is undefined (a
188    Bison error since the default value should have been set already) or defined
189    to any other value (possibly a user error).  Also, record Bison's usage of
190    VARIABLE by defining b4_percent_define_bison_variables(VARIABLE).  */
191 bool muscle_percent_define_flag_if (char const *variable);
192 
193 /* Mimic b4_percent_define_default in ../data/bison.m4 exactly.  That is, if
194    the %define variable VARIABLE is undefined, set its value to VALUE.
195    Don't record this as a Bison usage of VARIABLE as there's no reason to
196    suspect that the value has yet influenced the output.  */
197 void muscle_percent_define_default (char const *variable, char const *value);
198 
199 /* Mimic b4_percent_define_check_values in ../data/bison.m4 exactly except that
200    the VALUES structure is more appropriate for C.  That is, VALUES points to a
201    list of strings that is partitioned into sublists by NULL's, one terminating
202    each sublist.  The last sublist is followed by a second NULL.  For each
203    sublist, the first string is the name of a %define variable, and all
204    remaining strings in that sublist are the valid values for that variable.
205    Complain if such a variable is undefined (a Bison error since the default
206    value should have been set already) or defined to any other value (possibly
207    a user error).  Don't record this as a Bison usage of the variable as
208    there's no reason to suspect that the value has yet influenced the
209    output.  */
210 void muscle_percent_define_check_values (char const * const *values);
211 
212 /* Grow the muscle for the %code qualifier QUALIFIER appearing at
213    QUALIFIER_LOC with code CODE appearing at CODE_LOC.  Record this as a
214    user occurrence of QUALIFIER by invoking
215    muscle_user_name_list_grow.  */
216 void muscle_percent_code_grow (char const *qualifier, location qualifier_loc,
217                                char const *code, location code_loc);
218 
219 /* Output the definition of all the current muscles into a list of
220    m4_defines.  */
221 void muscles_m4_output (FILE *out);
222 
223 #endif /* not MUSCLE_TAB_H_ */
224