3 # Copyright © 2021-2022, 2024 Nick Bowler
5 # Generate a mapping from a (random) integer to a list of type specifiers
6 # represented by struct cdecl_declspec. Used internally by declgen to
7 # produce random valid declarations.
9 # License WTFPL2: Do What The Fuck You Want To Public License, version 2.
10 # This is free software: you are free to do what the fuck you want to.
11 # There is NO WARRANTY, to the extent permitted by law.
16 print " * Automatically generated by gen-typegen.awk from " FILENAME
18 print " * Automatically generated by gen-typegen.awk"
20 print " * Do not edit."
28 $0 ~ /^[abcdefghijklmnopqrstuvwxyz_]/ {
29 for (i = 1; i <= NF; i++) {
31 $i = "CDECL_TYPE_" toupper($i)
37 print "static inline struct cdecl_declspec *";
38 print "gen_raw_typespec_(unsigned type, struct cdecl_declspec *next)\n{";
39 print "\tstruct cdecl_declspec *s = malloc_nofail(sizeof *s);";
40 print "\ts->type = type;";
41 print "\ts->next = next;";
42 print "\ts->ident = 0;";
43 print "\treturn s;\n}\n"
45 print "static inline struct cdecl_declspec *"
46 print "gen_raw_typespecs(unsigned rngval)\n{"
47 print "\tswitch (rngval) {"
49 for (i = 0; i < count; i++) {
50 print "\tcase " i ":";
52 n = split(specs[i], parts);
53 for (j = 1; j <= n; j++) {
54 prefix = j == 1 ? "return" : "";
56 printf "\t\t%6s gen_raw_typespec_(%s,\n", prefix, parts[j];
58 printf "\t\t%25sNULL%s;\n", "", substr("))))))))", 1, n);
60 print "\tdefault:\n\t\tassert(0);\n\t}"
62 print "enum { GEN_TOTAL_TYPES = " count " };"