/* * Common declarations for the transpiled C code that the bootstrap * compiler outputs. * * Copyright © 2025 Samuel Lidén Borell * * SPDX-License-Identifier: EUPL-1.2+ OR LGPL-2.1-or-later */ #ifndef SLUL_RTL_H #define SLUL_RTL_H #if !defined(__STDC_VERSION__) || __STDC_VERSION__ < 199901L #undef bool #undef true #undef false #define bool int #define true 1 #define false 0 #elif __STDC_VERSION__ < 202311L #include #endif #include #include #include #define SLUL_INT_MAX 0xFFFFFFFF #if UINT_MAX >= SLUL_INT_MAX typedef unsigned SlulInt; #define SLUL_INT_FMT "u" #else typedef unsigned long SlulInt; #define SLUL_INT_FMT "ul" #endif /* Define unreachable() */ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L /* C23 already has unreachable in stddef.h */ #elif (defined(__GNUC__) && __GNUC__ > 4) || defined(__clang__) #define SLUL_unreachable() __builtin_unreachable() #else #define SLUL_unreachable() ((void)0) #endif /* Define NORETURN */ #if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 202311L #define SLUL_NORETURN [[noreturn]] #elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L #define SLUL_NORETURN _Noreturn #elif (defined(__GNUC__) && __GNUC__ > 2) || defined(__clang__) #define SLUL_NORETURN __attribute__((__noreturn__)) #else #define SLUL_NORETURN #endif /* ======================================================================= ||||||||||||||||||||||||||| String Class |||||||||||||||||||||||||||| ======================================================================= */ enum SlulStrLen { SLUL_SL_UPTO_17F = 0x80, SLUL_SL_UPTO_1017F, SLUL_SL_UPTO_10001017F }; struct String; /* TODO string operations */ /* ======================================================================= ||||||||||||||||||||||||||| Reader Class |||||||||||||||||||||||||||| ======================================================================= */ struct Reader; void Reader__giveme__inputparam( struct Reader **field, const struct String *param, const struct String *helptext); void Reader__giveme__inputparam_default_stdin( struct Reader **field, const struct String *param, const struct String *helptext); /* TODO reader operations */ /* ======================================================================= ||||||||||||||||||||||||||| Writer Class |||||||||||||||||||||||||||| ======================================================================= */ struct Writer; void Writer__giveme__outputparam( struct Writer **field, const struct String *option, const struct String *helptext); void Writer__giveme__outputparam_default_stdout( struct Writer **field, const struct String *option, const struct String *helptext); void Writer_write_str( struct Writer *wr, const struct String *text); /* ======================================================================= |||||||||||||||||||| Assertion/Failure Handling ||||||||||||||||||||| ======================================================================= */ #define SLUL_ASSERT(c_expr, orig_expr, file, line) do { \ if (!(c_expr)) { \ SLUL_assert_fail((orig_expr), (file), (line)); \ } \ } while (0) SLUL_NORETURN void SLUL_assert_fail(const char *expr, const char *file, int line); #define SLUL_OOM_CHECK(p, file, line) do { \ if (!(p)) SLUL_oom(file, line); \ } while (0) SLUL_NORETURN void SLUL_oom(const char *file, int line); /* ======================================================================= ||||||||||||||||||||||||| Message Reporter |||||||||||||||||||||||||| ======================================================================= */ struct MessageReporter; void MessageReporter__giveme(struct MessageReporter **mr); void MessageReporter_begin_message( struct MessageReporter *mr, const struct String *format); void MessageReporter_add_int( struct MessageReporter *mr, SlulInt number); void MessageReporter_report(struct MessageReporter *mr); /* ======================================================================= |||||||||||||||||||| Command-Line Option Parsing |||||||||||||||||||| ======================================================================= */ void bool__giveme__param( bool *field, const struct String *param, const struct String *helptext); void bool__giveme__param_with_default( bool *field, const struct String *param, bool defval, const struct String *helptext); void uint__giveme__param( SlulInt *field, const struct String *option, const struct String *helptext); void uint__giveme__param_with_default( SlulInt *field, const struct String *option, SlulInt defval, const struct String *helptext); void SLUL__parse_cli_args(int argc, char **argv); #endif