]> git.draconx.ca Git - cdecl99.git/blob - src/cdecl-internal.h
Release 1.3.
[cdecl99.git] / src / cdecl-internal.h
1 /*
2  * Internal declarations for libcdecl.
3  * Copyright © 2021, 2023-2024 Nick Bowler
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see <https://www.gnu.org/licenses/>.
17  */
18 #ifndef CDECL_INTERNAL_H_
19 #define CDECL_INTERNAL_H_
20
21 #include <stddef.h>
22 #include <gettext.h>
23 #include "cdecl.h"
24
25 #define MIN(a, b) ((a) < (b) ? (a) : (b))
26
27 #define _(s) dgettext(PACKAGE, s)
28 #define N_(s) s
29
30 /* Pack the 4 "kind" bits of a valid cdecl specifier value into two bits. */
31 #define PACK_KIND(x) ((( ((x>>8) - ((x)>>11)) ) >> 1) & 3)
32
33 /* Expand a packed "kind" to its original value. */
34 #define UNPACK_KIND(x) (0x100 << (x))
35
36 /*
37  * Pack a valid cdecl specifier value (CDECL_TYPE_xxx, CDECL_STOR_xxx, etc.)
38  * into 8 bits.  We do this by encoding the specifier kind in the upper two
39  * bits, and the enumerated sequence in the lower 6 bits. */
40 #define PACK_SPEC(x) (((x) & 0x3f) | (PACK_KIND(x) << 6))
41
42 /*
43  * Expand a packed specifier to its original value.
44  */
45 #define UNPACK_SPEC(x) (UNPACK_KIND((x) >> 6) | ((x) & 0x3f))
46
47 /*
48  * Pack a parser token into 7 bits.
49  *
50  * Bison normally numbers user-defined tokens sequentially starting from 258.
51  * If api.token,raw is used, then the numbering starts from 3.  As we have
52  * about 50 tokens, the latter case will fit in 7 bits easily; in the former
53  * case the upper bits are constant so we don't need to store them.
54  */
55 #define PACK_TOKEN(x) ((x) & 0x7f)
56
57 /*
58  * Expand a packed token to its original value.
59  */
60 #define UNPACK_TOKEN(x) ((x) | ((T_IDENT > 256) << 8))
61
62 struct cdecl_error;
63 struct cdecl_declspec;
64
65 #if ENABLE_NLS
66 void cdecl__init_i18n(void);
67 #else
68 static inline void cdecl__init_i18n(void)
69 {
70 }
71 #endif
72
73 void cdecl__err(const char *fmt, const char *arg);
74 void cdecl__errmsg(unsigned msg);
75
76 struct cdecl_declspec *cdecl__normalize_specs(struct cdecl_declspec *specs);
77
78 struct output_state {
79         char *dst;
80         size_t dstlen;
81         size_t accum;
82 };
83
84 size_t cdecl__advance(struct output_state *dst, size_t amount);
85 size_t cdecl__emit(struct output_state *dst, const char *src);
86 size_t cdecl__emit_uint(struct output_state *dst, cdecl_uintmax val);
87 size_t cdecl__strlcpy(char *dst, const char *src, size_t len);
88
89 const char *cdecl__emit_specs(struct output_state *dst,
90                               struct cdecl_declspec *s,
91                               unsigned mask);
92
93 /*
94  * If s is a (len bytes long) string corresponding to a declaration
95  * specifier, then:
96  *
97  *   - bits 7:0 (LSB) of the return value is the packed specifier type, and
98  *   - bits 15:8 of the return value is the packed parser token.
99  *
100  * Otherwise, if english_mode is nonzero and s is an "english keyword",
101  * returns the appropriate packed parser token in bits 15:8.
102  *
103  * Otherwise, PACK_TOKEN(T_IDENT) is returned in bits 15:8.
104  */
105 unsigned cdecl__to_keyword(const char *s, int len, int english_mode);
106
107 /* Container for an allocated parser token's value */
108 struct parse_item {
109         union {
110                 struct cdecl_declarator declarator;
111                 struct cdecl_declspec   declspec;
112                 struct cdecl            decl;
113         } u;
114
115         char s[FLEXIBLE_ARRAY_MEMBER];
116 };
117
118 struct parse_item *cdecl__alloc_item(size_t s_sz);
119
120 /*
121  * GCC enables -finline-small-functions in most optimization modes; this works
122  * on a highlevel estimate of how big a function actually is and occasionally
123  * this produces really bad results.
124  *
125  * While old versions of GCC do not support this attribute the result seems
126  * to be merely a warning so hopefully we can get away with this check and
127  * save a configure test.
128  */
129 #ifndef CDECL__NOINLINE
130 #  if __GNUC__
131 #    define CDECL__NOINLINE __attribute__((__noinline__))
132 #  else
133 #    define CDECL__NOINLINE /**/
134 #  endif
135 #endif
136
137 /*
138  * Build-time hook for white-box testing of memory allocation behaviour.
139  */
140 #if TEST_MALLOC_HOOK
141 void *test_realloc_hook(void *, size_t);
142
143 static inline void *test_wrap_malloc(size_t n)
144 {
145         return test_realloc_hook(0, n);
146 }
147
148 static inline void test_wrap_free(void *p)
149 {
150         test_realloc_hook(p, 0);
151 }
152
153 #undef  realloc
154 #define realloc test_realloc_hook
155 #undef  malloc
156 #define malloc test_wrap_malloc
157 #undef  free
158 #define free test_wrap_free
159 #endif
160
161 #endif