aboutsummaryrefslogtreecommitdiff
path: root/src/build/cmeta.h
diff options
context:
space:
mode:
authorGravatar Michael Smith <mikesmiffy128@gmail.com> 2025-03-10 02:37:19 +0000
committerGravatar Michael Smith <mikesmiffy128@gmail.com> 2025-04-06 16:41:13 +0100
commit244fea664121acf12871ab5858a5fe95a2606b52 (patch)
treee42b1990ef97adc0f0ab48b9be7e11de7fee0558 /src/build/cmeta.h
parentd86b7b41453c69b3854baa7cdc05a79a3cdfe092 (diff)
downloadsst-244fea664121acf12871ab5858a5fe95a2606b52.tar.gz
sst-244fea664121acf12871ab5858a5fe95a2606b52.zip
Rewrite and redesign codegen and feature system
Also switch to somewhat proper C23 flags while we're at it. This is a huge change. It took me forever, in between being really busy. Sorry about that. But the good news is I'm now free to start integrating the various patches that have accumulated since last release. Well, at least in between still being really busy. Gotta manage expectations. The main benefit of introducing GAMESPECIFIC() is that features that don't apply to a particular game no longer show up *at all*, and less time is wasted on init. It also enables a cool optimisation wherein unnecessary REQUIRE_GAMEDATA() checks can elided at compile time whenever the gamedata is known up-front to always exist in supported games. The DEF_FEAT_CVAR macro family meanwhile makes it easier to manage the lifecycle of cvars/ccmds, with less manual registering, unhiding and such. Originally I was going to try and just hack these features into the existing codegen abomination, but it just got too terrible. This rewrite should make it easier to continue tweaking codegen behaviour in future. It also has slightly better error messages.
Diffstat (limited to 'src/build/cmeta.h')
-rw-r--r--src/build/cmeta.h115
1 files changed, 58 insertions, 57 deletions
diff --git a/src/build/cmeta.h b/src/build/cmeta.h
index 86ae0ec..2a3f741 100644
--- a/src/build/cmeta.h
+++ b/src/build/cmeta.h
@@ -1,5 +1,5 @@
/*
- * Copyright © 2024 Michael Smith <mikesmiffy128@gmail.com>
+ * Copyright © 2025 Michael Smith <mikesmiffy128@gmail.com>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
@@ -17,71 +17,72 @@
#ifndef INC_CMETA_H
#define INC_CMETA_H
+#include "../intdefs.h"
#include "../os.h"
-struct cmeta;
+// XXX: leaking chibicc internals. won't matter after we do away with that
+typedef struct Token Token;
-const struct cmeta *cmeta_loadfile(const os_char *path);
+enum cmeta_item {
+ CMETA_ITEM_DEF_CVAR, // includes all min/max/unreg variants
+ CMETA_ITEM_DEF_CCMD, // includes plusminus/unreg variants
+ CMETA_ITEM_DEF_EVENT, // includes predicates
+ CMETA_ITEM_HANDLE_EVENT,
+ CMETA_ITEM_FEATURE,
+ CMETA_ITEM_REQUIRE, // includes all REQUIRE_*/REQUEST variants
+ CMETA_ITEM_GAMESPECIFIC,
+ CMETA_ITEM_PREINIT,
+ CMETA_ITEM_INIT,
+ CMETA_ITEM_END
+};
-/*
- * Iterates through all the #include directives in a file, passing each one in
- * turn to the callback cb.
- */
-void cmeta_includes(const struct cmeta *cm,
- void (*cb)(const char *f, bool issys, void *ctxt), void *ctxt);
+struct cmeta {
+ char *sbase;
+ u32 nitems; // number of interesting macros
+ //u32 *itemoffs; // file offsets of interesting macros (ONE DAY!)
+ Token **itemtoks; // crappy linked token structures, for the time being
+ u8 *itemtypes; // CMETA_ITEM_* enum values
+};
-/*
- * Iterates through all commands and variables declared using the macros in
- * con_.h, passing each one in turn to the callback cb.
- */
-void cmeta_conmacros(const struct cmeta *cm,
- void (*cb)(const char *name, bool isvar, bool unreg));
+enum cmeta_flag_cvar {
+ CMETA_CVAR_UNREG = 1,
+ CMETA_CVAR_FEAT = 2,
+};
+enum cmeta_flag_ccmd {
+ CMETA_CCMD_UNREG = 1,
+ CMETA_CCMD_FEAT = 2,
+ CMETA_CCMD_PLUSMINUS = 4
+};
+enum cmeta_flag_event {
+ CMETA_EVENT_ISPREDICATE = 1
+};
+enum cmeta_flag_require {
+ CMETA_REQUIRE_OPTIONAL = 1, // i.e. REQUEST() macro, could be extended
+ CMETA_REQUIRE_GAMEDATA = 2,
+ CMETA_REQUIRE_GLOBAL = 4
+};
-/*
- * Looks for a feature description macro in file, returning the description
- * string if it exists, an empty string if the feature is defined without a
- * user-facing description, and null if source file does not define a feature.
- */
-const char *cmeta_findfeatmacro(const struct cmeta *cm);
+struct cmeta_slice { const char *s; int len; };
-/*
- * the various kinds of feature specficiation macros, besides the feature
- * declaration macro itself
- */
-enum cmeta_featmacro {
- CMETA_FEAT_REQUIRE,
- CMETA_FEAT_REQUIREGD,
- CMETA_FEAT_REQUIREGLOBAL,
- CMETA_FEAT_REQUEST,
- CMETA_FEAT_PREINIT,
- CMETA_FEAT_INIT,
- CMETA_FEAT_END
-};
+struct cmeta cmeta_loadfile(const os_char *path);
+int cmeta_flags_cvar(const struct cmeta *cm, u32 i);
+int cmeta_flags_ccmd(const struct cmeta *cm, u32 i);
+int cmeta_flags_event(const struct cmeta *cm, u32 i);
+int cmeta_flags_require(const struct cmeta *cm, u32 i);
-/*
- * Iterates through all feature dependency macros and init/end/preinit
- * indicators, passing each bit of information to the callback cb.
- *
- * PREINT, INIT and END macros don't pass anything to param.
- *
- * This one takes a context pointer, while the others don't, because this is all
- * cobbled together without much consistent abstraction.
- */
-void cmeta_featinfomacros(const struct cmeta *cm, void (*cb)(
- enum cmeta_featmacro type, const char *param, void *ctxt), void *ctxt);
+int cmeta_nparams(const struct cmeta *cm, u32 i);
+struct cmeta_param_iter { Token *cur; };
+struct cmeta_param_iter cmeta_param_iter_init(const struct cmeta *cm, u32 i);
+struct cmeta_slice cmeta_param_iter(struct cmeta_param_iter *it);
+
+#define cmeta_param_foreach(varname, cm, u32) \
+ switch (0) for (struct cmeta_slice varname; 0;) default: \
+ for (struct cmeta_param_iter _it = cmeta_param_iter_init(cm, i); \
+ varname = cmeta_param_iter(&_it), varname.s;) \
+ /* {...} */
+
+u32 cmeta_line(const struct cmeta *cm, u32 i);
-/*
- * Iterates through all event-related macros and takes note of which events are
- * defined, giving a callback for each.
- */
-void cmeta_evdefmacros(const struct cmeta *cm, void (*cb)(const char *name,
- const char *const *params, int nparams, bool predicate));
-/*
- * Iterates through all event-related macros and gives a callback for each event
- * that is handled by the given module.
- */
-void cmeta_evhandlermacros(const struct cmeta *cm, const char *modname,
- void (*cb)(const char *evname, const char *modname));
#endif
// vi: sw=4 ts=4 noet tw=80 cc=80