Add an annotation to typedef structs and enums.

A new annotation 'typedefto' allows you to make a typedef in the generated
code for structs and enums (but not union 'which' enums).
This commit is contained in:
Jason Heeris 2021-01-01 21:26:02 +08:00
parent fe3a57de13
commit 9153fc39c4
2 changed files with 40 additions and 0 deletions

View file

@ -43,3 +43,6 @@ annotation fieldgetset @0xf72bc690355d66de (file): Void;
annotation donotinclude @0x8c99797357b357e9 (file): UInt64; annotation donotinclude @0x8c99797357b357e9 (file): UInt64;
# do not generate an include directive for an import statement for the file with # do not generate an include directive for an import statement for the file with
# the given ID # the given ID
annotation typedefto @0xcefaf27713042144 (struct, enum): Text;
# generate a typedef for the annotated struct or enum declaration

View file

@ -191,6 +191,24 @@ static void define_enum(struct node *n) {
str_addf(&HDR, "\n\t%s_%s = %d", n->name.str, e.name.str, i); str_addf(&HDR, "\n\t%s_%s = %d", n->name.str, e.name.str, i);
} }
str_addf(&HDR, "\n};\n"); str_addf(&HDR, "\n};\n");
for (i = capn_len(n->n.annotations)-1; i >= 0; i--) {
struct Annotation a;
struct Value v;
get_Annotation(&a, n->n.annotations, i);
read_Value(&v, a.value);
switch (a.id) {
case 0xcefaf27713042144UL:
if (v.which != Value_text) {
fprintf(stderr, "schema breakage on $C::typedefto annotation\n");
exit(2);
}
str_addf(&HDR, "\ntypedef enum %s %s;\n", n->name.str, v.text.str);
break;
}
}
} }
static void decode_value(struct value* v, Type_ptr type, Value_ptr value, const char *symbol) { static void decode_value(struct value* v, Type_ptr type, Value_ptr value, const char *symbol) {
@ -1007,6 +1025,7 @@ static void define_group(struct strings *s, struct node *n, const char *group_na
static void define_struct(struct node *n) { static void define_struct(struct node *n) {
static struct strings s; static struct strings s;
int i;
str_reset(&s.dtab); str_reset(&s.dtab);
str_reset(&s.ftab); str_reset(&s.ftab);
@ -1034,6 +1053,24 @@ static void define_struct(struct node *n) {
str_add(&HDR, s.decl.str, s.decl.len); str_add(&HDR, s.decl.str, s.decl.len);
str_addf(&HDR, "};\n"); str_addf(&HDR, "};\n");
for (i = capn_len(n->n.annotations)-1; i >= 0; i--) {
struct Annotation a;
struct Value v;
get_Annotation(&a, n->n.annotations, i);
read_Value(&v, a.value);
switch (a.id) {
case 0xcefaf27713042144UL:
if (v.which != Value_text) {
fprintf(stderr, "schema breakage on $C::typedefto annotation\n");
exit(2);
}
str_addf(&HDR, "\ntypedef struct %s %s;\n", n->name.str, v.text.str);
break;
}
}
str_addf(&SRC, "\n%s_ptr new_%s(struct capn_segment *s) {\n", n->name.str, n->name.str); str_addf(&SRC, "\n%s_ptr new_%s(struct capn_segment *s) {\n", n->name.str, n->name.str);
str_addf(&SRC, "\t%s_ptr p;\n", n->name.str); str_addf(&SRC, "\t%s_ptr p;\n", n->name.str);
str_addf(&SRC, "\tp.p = capn_new_struct(s, %d, %d);\n", 8*n->n._struct.dataWordCount, n->n._struct.pointerCount); str_addf(&SRC, "\tp.p = capn_new_struct(s, %d, %d);\n", 8*n->n._struct.dataWordCount, n->n._struct.pointerCount);