X-Git-Url: https://git.draconx.ca/gitweb/cdecl99.git/blobdiff_plain/655528745c72b4a7d50aee1688c359dc069ee546..HEAD:/src/cdecl-internal.h diff --git a/src/cdecl-internal.h b/src/cdecl-internal.h index 74c2654..335094c 100644 --- a/src/cdecl-internal.h +++ b/src/cdecl-internal.h @@ -1,6 +1,6 @@ /* * Internal declarations for libcdecl. - * Copyright © 2021, 2023 Nick Bowler + * Copyright © 2021, 2023-2024 Nick Bowler * * 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 @@ -20,10 +20,45 @@ #include #include +#include "cdecl.h" + +#define MIN(a, b) ((a) < (b) ? (a) : (b)) #define _(s) dgettext(PACKAGE, s) #define N_(s) s +/* Pack the 4 "kind" bits of a valid cdecl specifier value into two bits. */ +#define PACK_KIND(x) ((( ((x>>8) - ((x)>>11)) ) >> 1) & 3) + +/* Expand a packed "kind" to its original value. */ +#define UNPACK_KIND(x) (0x100 << (x)) + +/* + * Pack a valid cdecl specifier value (CDECL_TYPE_xxx, CDECL_STOR_xxx, etc.) + * into 8 bits. We do this by encoding the specifier kind in the upper two + * bits, and the enumerated sequence in the lower 6 bits. */ +#define PACK_SPEC(x) (((x) & 0x3f) | (PACK_KIND(x) << 6)) + +/* + * Expand a packed specifier to its original value. + */ +#define UNPACK_SPEC(x) (UNPACK_KIND((x) >> 6) | ((x) & 0x3f)) + +/* + * Pack a parser token into 7 bits. + * + * Bison normally numbers user-defined tokens sequentially starting from 258. + * If api.token,raw is used, then the numbering starts from 3. As we have + * about 50 tokens, the latter case will fit in 7 bits easily; in the former + * case the upper bits are constant so we don't need to store them. + */ +#define PACK_TOKEN(x) ((x) & 0x7f) + +/* + * Expand a packed token to its original value. + */ +#define UNPACK_TOKEN(x) ((x) | ((T_IDENT > 256) << 8)) + struct cdecl_error; struct cdecl_declspec; @@ -35,7 +70,7 @@ static inline void cdecl__init_i18n(void) } #endif -void cdecl__err(unsigned code, const char *fmt, const char *arg); +void cdecl__err(const char *fmt, const char *arg); void cdecl__errmsg(unsigned msg); struct cdecl_declspec *cdecl__normalize_specs(struct cdecl_declspec *specs); @@ -48,9 +83,79 @@ struct output_state { size_t cdecl__advance(struct output_state *dst, size_t amount); size_t cdecl__emit(struct output_state *dst, const char *src); +size_t cdecl__emit_uint(struct output_state *dst, cdecl_uintmax val); +size_t cdecl__strlcpy(char *dst, const char *src, size_t len); const char *cdecl__emit_specs(struct output_state *dst, struct cdecl_declspec *s, unsigned mask); +/* + * If s is a (len bytes long) string corresponding to a declaration + * specifier, then: + * + * - bits 7:0 (LSB) of the return value is the packed specifier type, and + * - bits 15:8 of the return value is the packed parser token. + * + * Otherwise, if english_mode is nonzero and s is an "english keyword", + * returns the appropriate packed parser token in bits 15:8. + * + * Otherwise, PACK_TOKEN(T_IDENT) is returned in bits 15:8. + */ +unsigned cdecl__to_keyword(const char *s, int len, int english_mode); + +/* Container for an allocated parser token's value */ +struct parse_item { + union { + struct cdecl_declarator declarator; + struct cdecl_declspec declspec; + struct cdecl decl; + } u; + + char s[FLEXIBLE_ARRAY_MEMBER]; +}; + +struct parse_item *cdecl__alloc_item(size_t s_sz); + +/* + * GCC enables -finline-small-functions in most optimization modes; this works + * on a highlevel estimate of how big a function actually is and occasionally + * this produces really bad results. + * + * While old versions of GCC do not support this attribute the result seems + * to be merely a warning so hopefully we can get away with this check and + * save a configure test. + */ +#ifndef CDECL__NOINLINE +# if __GNUC__ +# define CDECL__NOINLINE __attribute__((__noinline__)) +# else +# define CDECL__NOINLINE /**/ +# endif +#endif + +/* + * Build-time hook for white-box testing of memory allocation behaviour. + */ +#if TEST_MALLOC_HOOK +void *test_realloc_hook(void *, size_t); + +static inline void *test_wrap_malloc(size_t n) +{ + return test_realloc_hook(0, n); +} + +static inline void test_wrap_free(void *p) +{ + test_realloc_hook(p, 0); +} + +#undef realloc +#define realloc test_realloc_hook +#undef malloc +#define malloc test_wrap_malloc +#undef free +#define free test_wrap_free +#endif + #endif