1 /*
2 **      cdecl -- C gibberish translator
3 **      src/types.h
4 **
5 **      Copyright (C) 2017-2021  Paul J. Lucas
6 **
7 **      This program is free software: you can redistribute it and/or modify
8 **      it under the terms of the GNU General Public License as published by
9 **      the Free Software Foundation, either version 3 of the License, or
10 **      (at your option) any later version.
11 **
12 **      This program is distributed in the hope that it will be useful,
13 **      but WITHOUT ANY WARRANTY; without even the implied warranty of
14 **      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 **      GNU General Public License for more details.
16 **
17 **      You should have received a copy of the GNU General Public License
18 **      along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 */
20 
21 #ifndef cdecl_types_H
22 #define cdecl_types_H
23 
24 /**
25  * @file
26  * Declares some types and many `typedef` definitions in one file.
27  *
28  * Some headers are bidirectionally dependent, so `typedef`s were used
29  * originally rather than `include`.  However, some old C compilers don't like
30  * multiple `typedef` definitions even if the types match.  Hence, just put all
31  * `typedef` definitions in one file.
32  */
33 
34 // standard
35 #include <stdint.h>                     /* for uint*_t */
36 
37 /**
38  * Decimal print conversion specifier for \ref c_bit_width_t.
39  */
40 #define PRId_C_BIT_WIDTH_T        "%u"
41 
42 /**
43  * Decimal print conversion specifier for \ref c_array_size_t.
44  */
45 #define PRId_C_ARRAY_SIZE_T       "%d"
46 
47 /**
48  * Decimal print conversion specifier for \ref c_ast_depth_t.
49  */
50 #define PRId_C_AST_DEPTH_T        "%u"
51 
52 /**
53  * Decimal print conversion specifier for \ref c_ast_id_t.
54  *
55  * @sa #PRId_C_AST_SID_T
56  */
57 #define PRId_C_AST_ID_T           "%u"
58 
59 /**
60  * Decimal print conversion specifier for \ref c_ast_sid_t.
61  *
62  * @sa #PRId_C_AST_ID_T
63  */
64 #define PRId_C_AST_SID_T          "%d"
65 
66 // We have to define these typedefs here rather than just use the struct tags
67 // in the typedefs below otherwise Doxygen won't know about them.
68 typedef struct slist slist_t;
69 typedef struct slist_node slist_node_t;
70 
71 /**
72  * @defgroup type-declarations-group Type Declarations
73  * Declares many types and `typedef` declarations in one file.
74  * @{
75  */
76 
77 ///////////////////////////////////////////////////////////////////////////////
78 
79 typedef struct c_alignas          c_alignas_t;
80 typedef enum   c_alignas_arg      c_alignas_arg_t;
81 typedef struct c_apple_block_ast  c_apple_block_ast_t;
82 typedef struct c_array_ast        c_array_ast_t;
83 
84 /**
85  * One of:
86  *
87  *  + The actual size of a C array, but only when &ge; 0.
88  *  + #C_ARRAY_SIZE_NONE
89  *  + #C_ARRAY_SIZE_VARIABLE
90  */
91 typedef int                       c_array_size_t;
92 
93 typedef struct c_ast              c_ast_t;
94 typedef unsigned                  c_ast_depth_t;  ///< How many `()` deep.
95 typedef unsigned                  c_ast_id_t;     ///< Unique AST node id.
96 typedef enum   c_ast_kind         c_ast_kind_t;
97 typedef slist_t                   c_ast_list_t;   ///< AST list.
98 typedef struct c_ast_pair         c_ast_pair_t;
99 typedef slist_node_t              c_ast_param_t;  ///< Function-like parameter.
100 typedef int                       c_ast_sid_t;    ///< Signed \ref c_ast_id_t.
101 typedef unsigned                  c_bit_width_t;  ///< Bit-field width.
102 typedef struct c_builtin_ast      c_builtin_ast_t;
103 typedef enum   c_cast_kind        c_cast_kind_t;
104 typedef struct c_constructor_ast  c_constructor_ast_t;
105 typedef struct c_ecsu_ast         c_ecsu_ast_t;
106 typedef struct c_function_ast     c_function_ast_t;
107 typedef unsigned                  c_gib_flags_t;  ///< Gibberish printing flags.
108 typedef enum   c_graph            c_graph_t;
109 typedef struct c_keyword          c_keyword_t;
110 typedef enum   c_keyword_ctx      c_keyword_ctx_t;
111 typedef struct c_lang             c_lang_t;
112 typedef uint32_t                  c_lang_id_t;    ///< Languages bitmask.
113 typedef struct c_lang_lit         c_lang_lit_t;
114 typedef struct c_loc              c_loc_t;
115 typedef struct c_operator_ast     c_operator_ast_t;
116 typedef enum   c_oper_id          c_oper_id_t;
117 typedef struct c_operator         c_operator_t;
118 typedef struct c_parent_ast       c_parent_ast_t;
119 typedef struct c_ptr_mbr_ast      c_ptr_mbr_ast_t;
120 typedef struct c_ptr_ref_ast      c_ptr_ref_ast_t;
121 typedef slist_node_t              c_scope_t;      ///< Scope in \ref c_sname_t.
122 typedef struct c_scope_data       c_scope_data_t;
123 typedef struct c_sglob            c_sglob_t;
124 typedef slist_t                   c_sname_t;      ///< C++ scoped name.
125 typedef uint64_t                  c_tid_t;        ///< Type ID(s) bits.
126 typedef enum   c_tpid             c_tpid_t;
127 typedef struct c_typedef          c_typedef_t;
128 typedef enum   c_typedef_add_rv   c_typedef_add_rv_t;
129 typedef struct c_typedef_ast      c_typedef_ast_t;
130 typedef struct c_type             c_type_t;
131 typedef struct c_udef_conv_ast    c_udef_conv_ast_t;
132 typedef struct c_udef_lit_ast     c_udef_lit_ast_t;
133 typedef enum   c_visit_dir        c_visit_dir_t;
134 typedef struct cdecl_command      cdecl_command_t;
135 typedef enum   cdecl_command_kind cdecl_command_kind_t;
136 typedef enum   cdecl_help         cdecl_help_t;
137 typedef struct cdecl_keyword      cdecl_keyword_t;
138 typedef enum   cdecl_mode         cdecl_mode_t;
139 typedef enum   yytokentype        yytokentype;
140 
141 typedef c_loc_t YYLTYPE;                ///< Source location type for Bison.
142 /// @cond DOXYGEN_IGNORE
143 #define YYLTYPE_IS_DECLARED       1
144 #define YYLTYPE_IS_TRIVIAL        1
145 /// @endcond
146 
147 /**
148  * A pair of AST pointers used as one of the synthesized attribute types in the
149  * parser.
150  */
151 struct c_ast_pair {
152   /**
153    * A pointer to the AST being built.
154    */
155   c_ast_t *ast;
156 
157   /**
158    * Array and function-like declarations need a separate AST pointer that
159    * points to their `of_ast` or `ret_ast` (respectively) to be the "target" of
160    * subsequent additions to the AST.
161    */
162   c_ast_t *target_ast;
163 };
164 
165 /**
166  * C/C++ cast kinds.
167  */
168 enum c_cast_kind {
169   C_CAST_NONE,                          ///< Not a cast.
170   C_CAST_C,                             ///< C-style cast.
171   C_CAST_CONST,                         ///< C++ `const_cast`.
172   C_CAST_DYNAMIC,                       ///< C++ `dynamic_cast`.
173   C_CAST_REINTERPRET,                   ///< C++ `reinterpret_cast`.
174   C_CAST_STATIC                         ///< C++ `static_cast`.
175 };
176 
177 /**
178  * Di/Trigraph mode.
179  */
180 enum c_graph {
181   C_GRAPH_NONE,                         ///< Ordinary characters.
182   C_GRAPH_DI,                           ///< Digraphs.
183   C_GRAPH_TRI                           ///< Trigraphs.
184 };
185 
186 /**
187  * The source location used by Bison.
188  */
189 struct c_loc {
190   //
191   // These should be either unsigned or size_t, but Bison generates code that
192   // tests these for >= 0 which is always true for unsigned types so it
193   // generates warnings; hence these are kept as int to eliminate the warnings.
194   //
195   int first_line;                       ///< First line of location range.
196   int first_column;                     ///< First column of location range.
197   //
198   // Cdecl doesn't use either of these.
199   //
200   int last_line;                        ///< Last line of location range.
201   int last_column;                      ///< Last column of location range.
202 };
203 
204 /**
205  * The kind of cdecl command in least-to-most restrictive order.
206  */
207 enum cdecl_command_kind {
208   CDECL_COMMAND_ANYWHERE,               ///< Command is OK anywhere.
209   CDECL_COMMAND_FIRST_ARG,              ///< `$ cdecl` _command_ _args_
210   CDECL_COMMAND_PROG_NAME,              ///< `$` _command_ _args_
211   CDECL_COMMAND_LANG_ONLY               ///< `cdecl>` _command_ _args_
212 };
213 
214 /**
215  * A cdecl command.
216  */
217 struct cdecl_command {
218   char const           *literal;        ///< The command literal.
219   cdecl_command_kind_t  kind;           ///< The kind of command.
220   c_lang_id_t           lang_ids;       ///< Language(s) command is in.
221 };
222 
223 /**
224  * Types of help.
225  */
226 enum cdecl_help {
227   CDECL_HELP_COMMANDS,                  ///< Help for cdecl commands.
228   CDECL_HELP_ENGLISH,                   ///< Help for cdecl pseudo-English.
229   CDECL_HELP_OPTIONS                    ///< Help for cdecl options.
230 };
231 
232 /**
233  * Mode of operation.
234  */
235 enum cdecl_mode {
236   CDECL_ENGLISH_TO_GIBBERISH,           ///< Convert English into gibberish.
237   CDECL_GIBBERISH_TO_ENGLISH            ///< Decipher gibberish into English.
238 };
239 
240 ///////////////////////////////////////////////////////////////////////////////
241 
242 /** @} */
243 
244 #endif /* cdecl_types_H */
245 /* vim:set et sw=2 ts=2: */
246