1 /* 2 ** cdecl -- C gibberish translator 3 ** src/gibberish.h 4 ** 5 ** Copyright (C) 2019-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_gibberish_H 22 #define cdecl_gibberish_H 23 24 /** 25 * @file 26 * Declares functions for printing in gibberish, aka, a C/C++ declaration. 27 */ 28 29 // local 30 #include "pjl_config.h" /* must go first */ 31 #include "types.h" 32 33 /// @cond DOXYGEN_IGNORE 34 35 // standard 36 #include <stdio.h> /* for FILE */ 37 38 /// @endcond 39 40 /** 41 * @defgroup printing-gibberish-group Printing Gibberish 42 * Declares functions for printing in gibberish, aka, a C/C++ declaration. 43 * @{ 44 */ 45 46 // Flags for c_ast_gibberish() and c_typedef_gibberish(). 47 48 /** 49 * Unset value for \ref c_gib_flags_t. Can also be used to mean _not_ to print 50 * in gibberish (hence, print in English). 51 */ 52 #define C_GIB_NONE 0u 53 54 /** 55 * Flag for c_ast_gibberish() to print as a cast. 56 * 57 * @sa #C_GIB_DECL 58 */ 59 #define C_GIB_CAST (1u << 0) 60 61 /** 62 * Flag for c_ast_gibberish() to print as a declaration. 63 * 64 * @sa #C_GIB_CAST 65 * @sa #C_GIB_MULTI_DECL 66 * @sa #C_GIB_OMIT_TYPE 67 */ 68 #define C_GIB_DECL (1u << 1) 69 70 /** 71 * Flag for c_ast_gibberish() to indicate that the declaration is for multiple 72 * objects. 73 * 74 * @note Unlike #C_GIB_OMIT_TYPE, must be used for the entire declaration. 75 * @note May only be used in combination with #C_GIB_DECL. 76 * 77 * @sa #C_GIB_DECL 78 * @sa #C_GIB_OMIT_TYPE 79 */ 80 #define C_GIB_MULTI_DECL (1u << 2) 81 82 /** 83 * Flag for c_ast_gibberish() to omit the type name when printing gibberish for 84 * the _second_ and subsequent objects when printing multiple objects in the 85 * same declaration. For example, when printing: 86 * 87 * int *p, *q; 88 * 89 * the gibberish for `q` must _not_ print the `int` again. 90 * 91 * @note May only be used in combination with #C_GIB_DECL. 92 * 93 * @sa #C_GIB_DECL 94 * @sa #C_GIB_MULTI_DECL 95 */ 96 #define C_GIB_OMIT_TYPE (1u << 3) 97 98 /** 99 * Flag for c_typedef_gibberish() to print as a `typedef` declaration. 100 * 101 * @sa #C_GIB_USING 102 */ 103 #define C_GIB_TYPEDEF (1u << 4) 104 105 /** 106 * Flag for c_typedef_gibberish() to print as a `using` declaration. 107 * 108 * @sa #C_GIB_TYPEDEF 109 */ 110 #define C_GIB_USING (1u << 5) 111 112 ////////// extern functions /////////////////////////////////////////////////// 113 114 /** 115 * Prints \a ast as a C/C++ declaration or cast. 116 * 117 * @param ast The AST to print. 118 * @param flags The gibberish flags to use; must include either #C_GIB_CAST or 119 * #C_GIB_DECL. 120 * @param gout The `FILE` to print to. 121 * 122 * @sa c_typedef_gibberish() 123 */ 124 void c_ast_gibberish( c_ast_t const *ast, c_gib_flags_t flags, FILE *gout ); 125 126 /** 127 * Given \a kind, gets the associated C++ literal. 128 * 129 * @param kind The cast kind to get the literal for. 130 * @return Returns said literal. 131 */ 132 PJL_WARN_UNUSED_RESULT 133 char const* c_cast_gibberish( c_cast_kind_t kind ); 134 135 /** 136 * Prints \a tdef as a C/C++ type declaration. 137 * 138 * @param tdef The type to print. 139 * @param flags The gibberish flags to use; must include either #C_GIB_TYPEDEF 140 * or #C_GIB_USING. 141 * @param gout The `FILE` to print to. 142 * 143 * @sa c_ast_gibberish() 144 */ 145 void c_typedef_gibberish( c_typedef_t const *tdef, c_gib_flags_t flags, 146 FILE *gout ); 147 148 /** 149 * Gets the digraph or trigraph (collectively, "graph") equivalent of \a token. 150 * 151 * @param token The token to get the graph token for. 152 * @return If we're emitting graphs and \a token contains one or more 153 * characters that have a graph equivalent, returns \a token with said 154 * characters replaced by their graphs; otherwise returns \a token as-is. 155 */ 156 PJL_WARN_UNUSED_RESULT 157 char const* graph_token_c( char const *token ); 158 159 /////////////////////////////////////////////////////////////////////////////// 160 161 /** @} */ 162 163 #endif /* cdecl_gibberish_H */ 164 /* vim:set et sw=2 ts=2: */ 165