diff --git a/Makefile b/Makefile index a01adbf..bce1933 100644 --- a/Makefile +++ b/Makefile @@ -1,7 +1,7 @@ .PHONY: all clean test LDFLAGS=-g -Wall -Werror -fPIC -CFLAGS=-g -Wall -Werror -fPIC -I. -Wno-unused-function -ansi -pedantic +CFLAGS=-g -Wall -Werror -fPIC -I. -Wno-unused-function all: capn.so capnpc-c test diff --git a/compiler/capnpc-c.c b/compiler/capnpc-c.c index e69d5ce..5c955df 100644 --- a/compiler/capnpc-c.c +++ b/compiler/capnpc-c.c @@ -4,22 +4,31 @@ #include #include +struct value { + struct Type t; + const char *tname; + struct str tname_buf; + struct Value v; + capn_ptr ptrval; + int64_t intval; +}; + +struct field { + struct Field f; + struct value v; + struct node *group; +}; + struct node { struct capn_tree hdr; struct Node n; struct node *next; - struct node *first_child, *next_child; + struct node *file_nodes, *next_file_node; struct str name; - struct StructNode s; - struct EnumNode e; - struct InterfaceNode i; - struct ConstNode c; - struct FileNode f; + struct field *fields; }; -static struct node *g_files; -static FILE *HDR; -static struct str SRC; +static struct str SRC = STR_INIT, HDR = STR_INIT; static struct capn g_valcapn; static struct capn_segment g_valseg; static int g_valc; @@ -49,184 +58,181 @@ static void insert_node(struct node *s) { g_node_tree = capn_tree_insert(g_node_tree, &s->hdr); } +/* resolve_names recursively follows the nestedNodes tree in order to + * set node->name. + * It also builds up the list of nodes within a file (file_nodes and + * next_file_node). */ static void resolve_names(struct str *b, struct node *n, capn_text name, struct node *file) { int i, sz = b->len; str_add(b, name.str, name.len); str_add(&n->name, b->str, b->len); str_add(b, "_", 1); - for (i = n->n.nestedNodes.p.len-1; i >= 0; i--) { + for (i = 0; i < n->n.nestedNodes.p.len; i++) { struct Node_NestedNode nest; get_Node_NestedNode(&nest, n->n.nestedNodes, i); resolve_names(b, find_node(nest.id), nest.name, file); } - n->next_child = file->first_child; - file->first_child = n; + if (n->n.which == Node_struct) { + for (i = 0; i < n->n._struct.fields.p.len; i++) { + if (n->fields[i].group) { + resolve_names(b, n->fields[i].group, n->fields[i].f.name, file); + } + } + } + + if (n->n.which != Node_struct || !n->n._struct.isGroup) { + n->next_file_node = file->file_nodes; + file->file_nodes = n; + } + str_setlen(b, sz); } static void define_enum(struct node *n) { int i; - fprintf(HDR, "\nenum %s {", n->name.str); - for (i = 0; i < n->e.enumerants.p.len; i++) { - struct EnumNode_Enumerant ee; - get_EnumNode_Enumerant(&ee, n->e.enumerants, i); + str_addf(&HDR, "\nenum %s {", n->name.str); + for (i = 0; i < n->n._enum.enumerants.p.len; i++) { + struct Enumerant e; + get_Enumerant(&e, n->n._enum.enumerants, i); if (i) { - fprintf(HDR, ","); + str_addf(&HDR, ","); } - fprintf(HDR, "\n\t%s_%s = %d", n->name.str, ee.name.str, i); + str_addf(&HDR, "\n\t%s_%s = %d", n->name.str, e.name.str, i); } - fprintf(HDR, "\n};\n"); + str_addf(&HDR, "\n};\n"); } -struct value { - struct Type t; - const char *tname; - struct str buf; - struct Value v; - capn_ptr ptr; - int64_t num; -}; - static void decode_value(struct value* v, Type_ptr type, Value_ptr value, const char *symbol) { - struct Type lt; + struct Type list_type; memset(v, 0, sizeof(*v)); read_Type(&v->t, type); read_Value(&v->v, value); - switch (v->t.body_tag) { - case Type_voidType: + switch (v->t.which) { + case Type_void: v->tname = "void"; break; - case Type_boolType: + case Type_bool: v->tname = "unsigned"; break; - case Type_int8Type: + case Type_int8: v->tname = "int8_t"; break; - case Type_int16Type: + case Type_int16: v->tname = "int16_t"; break; - case Type_int32Type: + case Type_int32: v->tname = "int32_t"; break; - case Type_int64Type: + case Type_int64: v->tname = "int64_t"; break; - case Type_uint8Type: + case Type_uint8: v->tname = "uint8_t"; break; - case Type_uint16Type: + case Type_uint16: v->tname = "uint16_t"; break; - case Type_uint32Type: + case Type_uint32: v->tname = "uint32_t"; break; - case Type_uint64Type: + case Type_uint64: v->tname = "uint64_t"; break; - case Type_float32Type: + case Type_float32: v->tname = "float"; break; - case Type_float64Type: + case Type_float64: v->tname = "double"; break; - case Type_textType: + case Type_text: v->tname = "capn_text"; break; - case Type_dataType: + case Type_data: v->tname = "capn_data"; break; - case Type_enumType: - v->tname = strf(&v->buf, "enum %s", find_node(v->t.body.enumType)->name.str); + case Type_enum: + v->tname = strf(&v->tname_buf, "enum %s", find_node(v->t._enum.typeId)->name.str); break; - case Type_structType: - case Type_interfaceType: - v->tname = strf(&v->buf, "%s_ptr", find_node(v->t.body.structType)->name.str); + case Type_struct: + case Type_interface: + v->tname = strf(&v->tname_buf, "%s_ptr", find_node(v->t._struct.typeId)->name.str); break; - case Type_objectType: + case Type_object: v->tname = "capn_ptr"; break; - case Type_listType: - read_Type(<, v->t.body.listType); + case Type__list: + read_Type(&list_type, v->t.list.elementType); - switch (lt.body_tag) { - case Type_voidType: + switch (list_type.which) { + case Type_void: v->tname = "capn_ptr"; break; - case Type_boolType: + case Type_bool: v->tname = "capn_list1"; break; - case Type_int8Type: - case Type_uint8Type: + case Type_int8: + case Type_uint8: v->tname = "capn_list8"; break; - case Type_int16Type: - case Type_uint16Type: - case Type_enumType: + case Type_int16: + case Type_uint16: + case Type_enum: v->tname = "capn_list16"; break; - case Type_int32Type: - case Type_uint32Type: - case Type_float32Type: + case Type_int32: + case Type_uint32: + case Type_float32: v->tname = "capn_list32"; break; - case Type_int64Type: - case Type_uint64Type: - case Type_float64Type: + case Type_int64: + case Type_uint64: + case Type_float64: v->tname = "capn_list64"; break; - case Type_textType: - case Type_dataType: - case Type_objectType: - case Type_listType: + case Type_text: + case Type_data: + case Type_object: + case Type__list: v->tname = "capn_ptr"; break; - case Type_structType: - case Type_interfaceType: - v->tname = strf(&v->buf, "%s_list", find_node(lt.body.structType)->name.str); + case Type_struct: + case Type_interface: + v->tname = strf(&v->tname_buf, "%s_list", find_node(list_type._struct.typeId)->name.str); break; } } - switch (v->v.body_tag) { - case Value_boolValue: - v->num = v->v.body.boolValue; + switch (v->v.which) { + case Value_bool: + v->intval = v->v._bool; break; - case Value_int8Value: - v->num = v->v.body.int8Value; + case Value_int8: + case Value_uint8: + v->intval = v->v.int8; break; - case Value_uint8Value: - v->num = v->v.body.uint8Value; + case Value_int16: + case Value_uint16: + case Value_enum: + v->intval = v->v.int16; break; - case Value_int16Value: - v->num = v->v.body.int16Value; + case Value_int32: + case Value_uint32: + case Value_float32: + v->intval = v->v.int32; break; - case Value_uint16Value: - case Value_enumValue: - v->num = v->v.body.uint16Value; + case Value_int64: + case Value_float64: + case Value_uint64: + v->intval = v->v.int64; break; - case Value_int32Value: - v->num = v->v.body.int32Value; - break; - case Value_uint32Value: - case Value_float32Value: - v->num = v->v.body.uint32Value; - break; - case Value_int64Value: - v->num = v->v.body.int64Value; - break; - case Value_float64Value: - case Value_uint64Value: - v->num = v->v.body.uint64Value; - break; - case Value_textValue: - if (v->v.body.textValue.len) { - const char *scope = ""; + case Value_text: + if (v->v.text.len) { capn_ptr p = capn_root(&g_valcapn); - if (capn_set_text(p, 0, v->v.body.textValue)) { + if (capn_set_text(p, 0, v->v.text)) { fprintf(stderr, "failed to copy text\n"); exit(2); } @@ -234,30 +240,29 @@ static void decode_value(struct value* v, Type_ptr type, Value_ptr value, const if (!p.type) break; - v->ptr = p; + v->ptrval = p; if (!symbol) { static struct str buf = STR_INIT; - v->num = ++g_valc; - symbol = strf(&buf, "capn_val%d", (int) v->num); - scope = "static "; - } else { - v->num = 1; + v->intval = ++g_valc; + symbol = strf(&buf, "capn_val%d", (int) v->intval); } str_addf(&SRC, "%scapn_text %s = {%d,(char*)&capn_buf[%d],(struct capn_segment*)&capn_seg};\n", - scope, symbol, p.len-1, (int) (p.data-p.seg->data-8)); + symbol ? "" : "static ", + symbol, + p.len-1, + (int) (p.data-p.seg->data-8)); } break; - case Value_dataValue: - case Value_structValue: - case Value_objectValue: - case Value_listValue: - if (v->v.body.objectValue.type) { - const char *scope = ""; + case Value_data: + case Value_struct: + case Value_object: + case Value__list: + if (v->v.object.type) { capn_ptr p = capn_root(&g_valcapn); - if (capn_setp(p, 0, v->v.body.objectValue)) { + if (capn_setp(p, 0, v->v.object)) { fprintf(stderr, "failed to copy object\n"); exit(2); } @@ -265,25 +270,25 @@ static void decode_value(struct value* v, Type_ptr type, Value_ptr value, const if (!p.type) break; - v->ptr = p; + v->ptrval = p; if (!symbol) { static struct str buf = STR_INIT; - v->num = ++g_valc; - symbol = strf(&buf, "capn_val%d", (int) v->num); - scope = "static "; - } else { - v->num = 1; + v->intval = ++g_valc; + symbol = strf(&buf, "capn_val%d", (int) v->intval); } - str_addf(&SRC, "%s%s %s = {", scope, v->tname, symbol); + str_addf(&SRC, "%s%s %s = {", symbol ? "" : "static ", v->tname, symbol); if (strcmp(v->tname, "capn_ptr")) str_addf(&SRC, "{"); str_addf(&SRC, "%d,%d,%d,%d,%d,(char*)&capn_buf[%d],(struct capn_segment*)&capn_seg", - p.type, p.has_ptr_tag, - p.datasz, p.ptrsz, - p.len, (int) (p.data-p.seg->data-8)); + p.type, + p.has_ptr_tag, + p.datasz, + p.ptrsz, + p.len, + (int) (p.data-p.seg->data-8)); if (strcmp(v->tname, "capn_ptr")) str_addf(&SRC, "}"); @@ -292,129 +297,120 @@ static void decode_value(struct value* v, Type_ptr type, Value_ptr value, const } break; - case Value_interfaceValue: - case Value_voidValue: + case Value_interface: + case Value_void: break; } } static void define_const(struct node *n) { struct value v; - decode_value(&v, n->c.type, n->c.value, n->name.str); + decode_value(&v, n->n._const.type, n->n._const.value, n->name.str); - switch (v.v.body_tag) { - case Value_boolValue: - case Value_int8Value: - case Value_int16Value: - case Value_int32Value: - fprintf(HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = %d;\n", v.tname, n->name.str, (int) v.num); + switch (v.v.which) { + case Value_bool: + case Value_int8: + case Value_int16: + case Value_int32: + str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "%s %s = %d;\n", v.tname, n->name.str, (int) v.intval); break; - case Value_uint8Value: - case Value_uint16Value: - case Value_uint32Value: - fprintf(HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = %uu;\n", v.tname, n->name.str, (uint32_t) v.num); + case Value_uint8: + case Value_uint16: + case Value_uint32: + str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "%s %s = %uu;\n", v.tname, n->name.str, (uint32_t) v.intval); break; - case Value_enumValue: - fprintf(HDR, "extern %s %s;\n", v.tname, n->name.str); - str_addf(&SRC, "%s %s = (%s) %uu;\n", v.tname, n->name.str, v.tname, (uint32_t) v.num); + case Value_enum: + str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); + str_addf(&SRC, "%s %s = (%s) %uu;\n", v.tname, n->name.str, v.tname, (uint32_t) v.intval); break; - case Value_int64Value: - case Value_uint64Value: - fprintf(HDR, "extern %s %s;\n", v.tname, n->name.str); + case Value_int64: + case Value_uint64: + str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); str_addf(&SRC, "%s %s = ((uint64_t) %#xu << 32) | %#xu;\n", v.tname, n->name.str, - (uint32_t) (v.num >> 32), (uint32_t) v.num); + (uint32_t) (v.intval >> 32), (uint32_t) v.intval); break; - case Value_float32Value: - fprintf(HDR, "extern union capn_conv_f32 %s;\n", n->name.str); - str_addf(&SRC, "union capn_conv_f32 %s = {%#xu};\n", n->name.str, (uint32_t) v.num); + case Value_float32: + str_addf(&HDR, "extern union capn_conv_f32 %s;\n", n->name.str); + str_addf(&SRC, "union capn_conv_f32 %s = {%#xu};\n", n->name.str, (uint32_t) v.intval); break; - case Value_float64Value: - fprintf(HDR, "extern union capn_conv_f64 %s;\n", n->name.str); + case Value_float64: + str_addf(&HDR, "extern union capn_conv_f64 %s;\n", n->name.str); str_addf(&SRC, "union capn_conv_f64 %s = {((uint64_t) %#xu << 32) | %#xu};\n", - n->name.str, (uint32_t) (v.num >> 32), (uint32_t) v.num); + n->name.str, (uint32_t) (v.intval >> 32), (uint32_t) v.intval); break; - case Value_textValue: - case Value_dataValue: - case Value_structValue: - case Value_objectValue: - case Value_listValue: - fprintf(HDR, "extern %s %s;\n", v.tname, n->name.str); - if (!v.num) { + case Value_text: + case Value_data: + case Value_struct: + case Value_object: + case Value__list: + str_addf(&HDR, "extern %s %s;\n", v.tname, n->name.str); + if (!v.ptrval.type) { str_addf(&SRC, "%s %s;\n", v.tname, n->name.str); } break; - case Value_interfaceValue: - case Value_voidValue: + case Value_interface: + case Value_void: break; } - str_release(&v.buf); + str_release(&v.tname_buf); } -struct member { - unsigned int is_valid : 1; - struct StructNode_Member m; - struct StructNode_Field f; - struct StructNode_Union u; - struct value v; - struct member *mbrs; - int idx; -}; +static void decode_field(struct field *fields, Field_list l, int i) { + struct field f; + memset(&f, 0, sizeof(f)); + get_Field(&f.f, l, i); -static struct member *decode_member(struct member *mbrs, StructNode_Member_list l, int i) { - struct member m; - memset(&m, 0, sizeof(m)); - m.is_valid = 1; - m.idx = i; - get_StructNode_Member(&m.m, l, i); - - if (m.m.codeOrder >= l.p.len) { - fprintf(stderr, "unexpectedly large code order %d >= %d\n", m.m.codeOrder, l.p.len); + if (f.f.codeOrder >= l.p.len) { + fprintf(stderr, "unexpectedly large code order %d >= %d\n", f.f.codeOrder, l.p.len); exit(3); } - if (m.m.body_tag == StructNode_Member_fieldMember) { - read_StructNode_Field(&m.f, m.m.body.fieldMember); - decode_value(&m.v, m.f.type, m.f.defaultValue, NULL); + switch (f.f.which) { + case Field_slot: + decode_value(&f.v, f.f.slot.type, f.f.slot.defaultValue, NULL); + break; + case Field_group: + f.group = find_node(f.f.group.typeId); + break; } - memcpy(&mbrs[m.m.codeOrder], &m, sizeof(m)); - return &mbrs[m.m.codeOrder]; + memcpy(&fields[f.f.codeOrder], &f, sizeof(f)); } -static const char *xor_member(struct member *m) { +static const char *xor_member(struct field *f) { static struct str buf = STR_INIT; - if (m->v.num) { - switch (m->v.v.body_tag) { - case Value_int8Value: - case Value_int16Value: - case Value_int32Value: - return strf(&buf, " ^ %d", (int32_t) m->v.num); + if (f->v.intval) { + switch (f->v.v.which) { + case Value_int8: + case Value_int16: + case Value_int32: + return strf(&buf, " ^ %d", (int32_t) f->v.intval); - case Value_uint8Value: - case Value_uint16Value: - case Value_uint32Value: - case Value_enumValue: - return strf(&buf, " ^ %uu", (uint32_t) m->v.num); + case Value_uint8: + case Value_uint16: + case Value_uint32: + case Value_enum: + return strf(&buf, " ^ %uu", (uint32_t) f->v.intval); - case Value_float32Value: - return strf(&buf, " ^ %#xu", (uint32_t) m->v.num); + case Value_float32: + return strf(&buf, " ^ %#xu", (uint32_t) f->v.intval); - case Value_int64Value: - case Value_uint64Value: - case Value_float64Value: + case Value_int64: + case Value_uint64: + case Value_float64: return strf(&buf, " ^ ((uint64_t) %#xu << 32) ^ %#xu", - (uint32_t) (m->v.num >> 32), (uint32_t) m->v.num); + (uint32_t) (f->v.intval >> 32), (uint32_t) f->v.intval); default: return ""; @@ -424,9 +420,9 @@ static const char *xor_member(struct member *m) { } } -static const char *ptr_member(struct member *m, const char *var) { +static const char *ptr_member(struct field *f, const char *var) { static struct str buf = STR_INIT; - if (!strcmp(m->v.tname, "capn_ptr")) { + if (!strcmp(f->v.tname, "capn_ptr")) { return var; } else if (var[0] == '*') { return strf(&buf, "%s->p", var+1); @@ -435,334 +431,408 @@ static const char *ptr_member(struct member *m, const char *var) { } } -static void set_member(struct member *m, const char *ptr, const char *tab, const char *var) { - const char *xor = xor_member(m); - const char *pvar = ptr_member(m, var); +static void set_member(struct str *func, struct field *f, const char *ptr, const char *tab, const char *var) { + const char *xor = xor_member(f); + const char *pvar = ptr_member(f, var); - if (m->v.t.body_tag == Type_voidType) + if (f->v.t.which == Type_void) return; - str_add(&SRC, tab, -1); + str_add(func, tab, -1); - switch (m->v.t.body_tag) { - case Type_voidType: + switch (f->v.t.which) { + case Type_bool: + str_addf(func, "capn_write1(%s, %d, %s != %d);\n", ptr, f->f.slot.offset, var, (int) f->v.intval); break; - case Type_boolType: - str_addf(&SRC, "err = err || capn_write1(%s, %d, %s != %d);\n", ptr, m->f.offset, var, (int) m->v.num); + case Type_int8: + str_addf(func, "capn_write8(%s, %d, (uint8_t) %s%s);\n", ptr, f->f.slot.offset, var, xor); break; - case Type_int8Type: - str_addf(&SRC, "err = err || capn_write8(%s, %d, (uint8_t) %s%s);\n", ptr, m->f.offset, var, xor); + case Type_int16: + case Type_enum: + str_addf(func, "capn_write16(%s, %d, (uint16_t) %s%s);\n", ptr, 2*f->f.slot.offset, var, xor); break; - case Type_int16Type: - case Type_enumType: - str_addf(&SRC, "err = err || capn_write16(%s, %d, (uint16_t) %s%s);\n", ptr, 2*m->f.offset, var, xor); + case Type_int32: + str_addf(func, "capn_write32(%s, %d, (uint32_t) %s%s);\n", ptr, 4*f->f.slot.offset, var, xor); break; - case Type_int32Type: - str_addf(&SRC, "err = err || capn_write32(%s, %d, (uint32_t) %s%s);\n", ptr, 4*m->f.offset, var, xor); + case Type_int64: + str_addf(func, "capn_write64(%s, %d, (uint64_t) %s%s);\n", ptr, 8*f->f.slot.offset, var, xor); break; - case Type_int64Type: - str_addf(&SRC, "err = err || capn_write64(%s, %d, (uint64_t) %s%s);\n", ptr, 8*m->f.offset, var, xor); + case Type_uint8: + str_addf(func, "capn_write8(%s, %d, %s%s);\n", ptr, f->f.slot.offset, var, xor); break; - case Type_uint8Type: - str_addf(&SRC, "err = err || capn_write8(%s, %d, %s%s);\n", ptr, m->f.offset, var, xor); + case Type_uint16: + str_addf(func, "capn_write16(%s, %d, %s%s);\n", ptr, 2*f->f.slot.offset, var, xor); break; - case Type_uint16Type: - str_addf(&SRC, "err = err || capn_write16(%s, %d, %s%s);\n", ptr, 2*m->f.offset, var, xor); + case Type_uint32: + str_addf(func, "capn_write32(%s, %d, %s%s);\n", ptr, 4*f->f.slot.offset, var, xor); break; - case Type_uint32Type: - str_addf(&SRC, "err = err || capn_write32(%s, %d, %s%s);\n", ptr, 4*m->f.offset, var, xor); + case Type_float32: + str_addf(func, "capn_write32(%s, %d, capn_from_f32(%s)%s);\n", ptr, 4*f->f.slot.offset, var, xor); break; - case Type_float32Type: - str_addf(&SRC, "err = err || capn_write32(%s, %d, capn_from_f32(%s)%s);\n", ptr, 4*m->f.offset, var, xor); + case Type_uint64: + str_addf(func, "capn_write64(%s, %d, %s%s);\n", ptr, 8*f->f.slot.offset, var, xor); break; - case Type_uint64Type: - str_addf(&SRC, "err = err || capn_write64(%s, %d, %s%s);\n", ptr, 8*m->f.offset, var, xor); + case Type_float64: + str_addf(func, "capn_write64(%s, %d, capn_from_f64(%s)%s);\n", ptr, 8*f->f.slot.offset, var, xor); break; - case Type_float64Type: - str_addf(&SRC, "err = err || capn_write64(%s, %d, capn_from_f64(%s)%s);\n", ptr, 8*m->f.offset, var, xor); - break; - case Type_textType: - if (m->v.num) { + case Type_text: + if (f->v.ptrval.type) { g_val0used = 1; - str_addf(&SRC, "err = err || capn_set_text(%s, %d, (%s.str != capn_val%d.str) ? %s : capn_val0);\n", - ptr, m->f.offset, var, (int)m->v.num, var); + str_addf(func, "capn_set_text(%s, %d, (%s.str != capn_val%d.str) ? %s : capn_val0);\n", + ptr, f->f.slot.offset, var, (int)f->v.intval, var); } else { - str_addf(&SRC, "err = err || capn_set_text(%s, %d, %s);\n", - ptr, m->f.offset, var); + str_addf(func, "capn_set_text(%s, %d, %s);\n", + ptr, f->f.slot.offset, var); } break; - case Type_dataType: - case Type_structType: - case Type_interfaceType: - case Type_listType: - case Type_objectType: - if (!m->v.num) { - str_addf(&SRC, "err = err || capn_setp(%s, %d, %s);\n", - ptr, m->f.offset, pvar); - } else if (!strcmp(m->v.tname, "capn_ptr")) { + case Type_data: + case Type_struct: + case Type_interface: + case Type__list: + case Type_object: + if (!f->v.intval) { + str_addf(func, "capn_setp(%s, %d, %s);\n", + ptr, f->f.slot.offset, pvar); + } else if (!strcmp(f->v.tname, "capn_ptr")) { g_nullused = 1; - str_addf(&SRC, "err = err || capn_setp(%s, %d, (%s.data != capn_val%d.data) ? %s : capn_null);\n", - ptr, m->f.offset, pvar, (int)m->v.num, pvar); + str_addf(func, "capn_setp(%s, %d, (%s.data != capn_val%d.data) ? %s : capn_null);\n", + ptr, f->f.slot.offset, pvar, (int)f->v.intval, pvar); } else { g_nullused = 1; - str_addf(&SRC, "err = err || capn_setp(%s, %d, (%s.data != capn_val%d.p.data) ? %s : capn_null);\n", - ptr, m->f.offset, pvar, (int)m->v.num, pvar); + str_addf(func, "capn_setp(%s, %d, (%s.data != capn_val%d.p.data) ? %s : capn_null);\n", + ptr, f->f.slot.offset, pvar, (int)f->v.intval, pvar); } break; + default: + break; } } -static void get_member(struct member *m, const char *ptr, const char *tab, const char *var) { - const char *xor = xor_member(m); - const char *pvar = ptr_member(m, var); +static void get_member(struct str *func, struct field *f, const char *ptr, const char *tab, const char *var) { + const char *xor = xor_member(f); + const char *pvar = ptr_member(f, var); - if (m->v.t.body_tag == Type_voidType) + if (f->v.t.which == Type_void) return; - str_add(&SRC, tab, -1); + str_add(func, tab, -1); - switch (m->v.t.body_tag) { - case Type_voidType: + switch (f->v.t.which) { + case Type_bool: + str_addf(func, "%s = (capn_read8(%s, %d) & %d) != %d;\n", + var, ptr, f->f.slot.offset/8, 1 << (f->f.slot.offset%8), (int)f->v.intval); return; - case Type_boolType: - str_addf(&SRC, "%s = (capn_read8(%s, %d) & %d) != %d;\n", - var, ptr, m->f.offset/8, 1 << (m->f.offset%8), (int)m->v.num); + case Type_int8: + str_addf(func, "%s = (int8_t) capn_read8(%s, %d)%s;\n", var, ptr, f->f.slot.offset, xor); return; - case Type_int8Type: - str_addf(&SRC, "%s = (int8_t) capn_read8(%s, %d)%s;\n", var, ptr, m->f.offset, xor); + case Type_int16: + str_addf(func, "%s = (int16_t) capn_read16(%s, %d)%s;\n", var, ptr, 2*f->f.slot.offset, xor); return; - case Type_int16Type: - str_addf(&SRC, "%s = (int16_t) capn_read16(%s, %d)%s;\n", var, ptr, 2*m->f.offset, xor); + case Type_int32: + str_addf(func, "%s = (int32_t) capn_read32(%s, %d)%s;\n", var, ptr, 4*f->f.slot.offset, xor); return; - case Type_int32Type: - str_addf(&SRC, "%s = (int32_t) capn_read32(%s, %d)%s;\n", var, ptr, 4*m->f.offset, xor); + case Type_int64: + str_addf(func, "%s = (int64_t) capn_read64(%s, %d)%s;\n", var, ptr, 8*f->f.slot.offset, xor); return; - case Type_int64Type: - str_addf(&SRC, "%s = (int64_t) capn_read64(%s, %d)%s;\n", var, ptr, 8*m->f.offset, xor); + case Type_uint8: + str_addf(func, "%s = capn_read8(%s, %d)%s;\n", var, ptr, f->f.slot.offset, xor); return; - case Type_uint8Type: - str_addf(&SRC, "%s = capn_read8(%s, %d)%s;\n", var, ptr, m->f.offset, xor); + case Type_uint16: + str_addf(func, "%s = capn_read16(%s, %d)%s;\n", var, ptr, 2*f->f.slot.offset, xor); return; - case Type_uint16Type: - str_addf(&SRC, "%s = capn_read16(%s, %d)%s;\n", var, ptr, 2*m->f.offset, xor); + case Type_uint32: + str_addf(func, "%s = capn_read32(%s, %d)%s;\n", var, ptr, 4*f->f.slot.offset, xor); return; - case Type_uint32Type: - str_addf(&SRC, "%s = capn_read32(%s, %d)%s;\n", var, ptr, 4*m->f.offset, xor); + case Type_uint64: + str_addf(func, "%s = capn_read64(%s, %d)%s;\n", var, ptr, 8*f->f.slot.offset, xor); return; - case Type_uint64Type: - str_addf(&SRC, "%s = capn_read64(%s, %d)%s;\n", var, ptr, 8*m->f.offset, xor); + case Type_float32: + str_addf(func, "%s = capn_to_f32(capn_read32(%s, %d)%s);\n", var, ptr, 4*f->f.slot.offset, xor); return; - case Type_float32Type: - str_addf(&SRC, "%s = capn_to_f32(capn_read32(%s, %d)%s);\n", var, ptr, 4*m->f.offset, xor); + case Type_float64: + str_addf(func, "%s = capn_to_f64(capn_read64(%s, %d)%s);\n", var, ptr, 8*f->f.slot.offset, xor); return; - case Type_float64Type: - str_addf(&SRC, "%s = capn_to_f64(capn_read64(%s, %d)%s);\n", var, ptr, 8*m->f.offset, xor); + case Type_enum: + str_addf(func, "%s = (%s) capn_read16(%s, %d)%s;\n", var, f->v.tname, ptr, 2*f->f.slot.offset, xor); return; - case Type_enumType: - str_addf(&SRC, "%s = (%s) capn_read16(%s, %d)%s;\n", var, m->v.tname, ptr, 2*m->f.offset, xor); - return; - case Type_textType: - if (!m->v.num) + case Type_text: + if (!f->v.intval) g_val0used = 1; - str_addf(&SRC, "%s = capn_get_text(%s, %d, capn_val%d);\n", var, ptr, m->f.offset, (int)m->v.num); + str_addf(func, "%s = capn_get_text(%s, %d, capn_val%d);\n", var, ptr, f->f.slot.offset, (int)f->v.intval); return; - case Type_dataType: - str_addf(&SRC, "%s = capn_get_data(%s, %d);\n", var, ptr, m->f.offset); + case Type_data: + str_addf(func, "%s = capn_get_data(%s, %d);\n", var, ptr, f->f.slot.offset); break; - case Type_structType: - case Type_interfaceType: - case Type_objectType: - case Type_listType: - str_addf(&SRC, "%s = capn_getp(%s, %d);\n", pvar, ptr, m->f.offset); + case Type_struct: + case Type_interface: + case Type_object: + case Type__list: + str_addf(func, "%s = capn_getp(%s, %d);\n", pvar, ptr, f->f.slot.offset); break; + default: + return; } - if (m->v.num) { - str_addf(&SRC, "%sif (!%s.type) {\n", tab, pvar); - str_addf(&SRC, "%s\t%s = capn_val%d;\n", tab, var, (int)m->v.num); - str_addf(&SRC, "%s}\n", tab); + if (f->v.intval) { + str_addf(func, "%sif (!%s.type) {\n", tab, pvar); + str_addf(func, "%s\t%s = capn_val%d;\n", tab, var, (int)f->v.intval); + str_addf(func, "%s}\n", tab); } } -static void union_block(struct member *m, struct member *u, int set) { +struct strings { + struct str ftab; + struct str dtab; + struct str get; + struct str set; + struct str enums; + struct str decl; + struct str var; +}; + +static void union_block(struct strings *s, struct field *f) { static struct str buf = STR_INIT; - if (set) { - set_member(u, "p.p", "\t\t", strf(&buf, "s->%s.%s", m->m.name.str, u->m.name.str)); - } else { - get_member(u, "p.p", "\t\t", strf(&buf, "s->%s.%s", m->m.name.str, u->m.name.str)); - } - - str_addf(&SRC, "\t\tbreak;\n"); + str_add(&s->ftab, "\t", -1); + set_member(&s->set, f, "p.p", s->ftab.str, strf(&buf, "%s%s", s->var.str, f->f.name.str)); + get_member(&s->get, f, "p.p", s->ftab.str, strf(&buf, "%s%s", s->var.str, f->f.name.str)); + str_addf(&s->set, "%sbreak;\n", s->ftab.str); + str_addf(&s->get, "%sbreak;\n", s->ftab.str); + str_setlen(&s->ftab, s->ftab.len-1); } -static void union_cases(struct node *n, struct member *m, int set, int mask) { - struct member *u = NULL; - int j; +static int in_union(struct field *f) { + return f->f.discriminantValue != 0xFFFF; +} - for (j = 0; j < m->u.members.p.len; j++) { - if (!m->mbrs[j].v.num && (mask & (1 << m->mbrs[j].v.t.body_tag))) { - u = &m->mbrs[j]; - str_addf(&SRC, "\tcase %s_%s:\n", n->name.str, u->m.name.str); - } +static void union_cases(struct strings *s, struct node *n, struct field *first_field, int mask) { + struct field *f, *u = NULL; + + for (f = first_field; f < n->fields + n->n._struct.fields.p.len && in_union(f); f++) { + + if (f->f.which != Field_slot) + continue; + if (f->v.ptrval.type || f->v.intval) + continue; + if ((mask & (1 << f->v.t.which)) == 0) + continue; + + u = f; + str_addf(&s->set, "%scase %s_%s:\n", s->ftab.str, n->name.str, f->f.name.str); + str_addf(&s->get, "%scase %s_%s:\n", s->ftab.str, n->name.str, f->f.name.str); } if (u) - union_block(m, u, set); + union_block(s, u); } -static void do_union(struct node *n, struct member *m, int set) { - int j; +static void declare_slot(struct strings *s, struct field *f) { + switch (f->v.t.which) { + case Type_void: + break; + case Type_bool: + str_addf(&s->decl, "%s%s %s : 1;\n", s->dtab.str, f->v.tname, f->f.name.str); + break; + default: + str_addf(&s->decl, "%s%s %s;\n", s->dtab.str, f->v.tname, f->f.name.str); + break; + } +} - if (set) { - str_addf(&SRC, "\terr = err || capn_write16(p.p, %d, s->%s_tag);\n", - 2*m->u.discriminantOffset, m->m.name.str); +static void define_group(struct strings *s, struct node *n, const char *group_name); + +static void do_union(struct strings *s, struct node *n, struct field *first_field, const char *union_name) { + int tagoff = 2 * n->n._struct.discriminantOffset; + struct field *f; + static struct str tag = STR_INIT; + struct str enums = STR_INIT; + + str_reset(&tag); + + if (union_name) { + str_addf(&tag, "%.*s_which", s->var.len - 1, s->var.str); + str_addf(&enums, "enum %s_which {", n->name.str); + str_addf(&s->decl, "%senum %s_which %s_which;\n", s->dtab.str, n->name.str, union_name); + str_addf(&s->get, "%s%s = (enum %s_which) capn_read16(p.p, %d);\n", + s->ftab.str, tag.str, n->name.str, tagoff); } else { - str_addf(&SRC, "\ts->%s_tag = (enum %s_%s) capn_read16(p.p, %d);\n", - m->m.name.str, n->name.str, m->m.name.str, 2*m->u.discriminantOffset); + str_addf(&tag, "%swhich", s->var.str); + str_addf(&enums, "enum %s_which {", n->name.str); + str_addf(&s->decl, "%senum %s_which which;\n", s->dtab.str, n->name.str); + str_addf(&s->get, "%s%s = (enum %s_which) capn_read16(p.p, %d);\n", + s->ftab.str, tag.str, n->name.str, tagoff); } - str_addf(&SRC, "\n\tswitch (s->%s_tag) {\n", m->m.name.str); + str_addf(&s->set, "%scapn_write16(p.p, %d, %s);\n", s->ftab.str, tagoff, tag.str); + str_addf(&s->set, "%sswitch (%s) {\n", s->ftab.str, tag.str); + str_addf(&s->get, "%sswitch (%s) {\n", s->ftab.str, tag.str); /* if we have a bunch of the same C type with zero defaults, we * only need to emit one switch block as the layout will line up * in the C union */ - union_cases(n, m, set, (1 << Type_voidType)); - union_cases(n, m, set, (1 << Type_boolType)); - union_cases(n, m, set, (1 << Type_int8Type) | (1 << Type_uint8Type)); - union_cases(n, m, set, (1 << Type_int16Type) | (1 << Type_uint16Type) | (1 << Type_enumType)); - union_cases(n, m, set, (1 << Type_int32Type) | (1 << Type_uint32Type) | (1 << Type_float32Type)); - union_cases(n, m, set, (1 << Type_int64Type) | (1 << Type_uint64Type) | (1 << Type_float64Type)); - union_cases(n, m, set, (1 << Type_textType)); - union_cases(n, m, set, (1 << Type_dataType)); - union_cases(n, m, set, (1 << Type_structType) | (1 << Type_interfaceType) | (1 << Type_objectType) | (1 << Type_listType)); + union_cases(s, n, first_field, (1 << Type_bool)); + union_cases(s, n, first_field, (1 << Type_int8) | (1 << Type_uint8)); + union_cases(s, n, first_field, (1 << Type_int16) | (1 << Type_uint16) | (1 << Type_enum)); + union_cases(s, n, first_field, (1 << Type_int32) | (1 << Type_uint32) | (1 << Type_float32)); + union_cases(s, n, first_field, (1 << Type_int64) | (1 << Type_uint64) | (1 << Type_float64)); + union_cases(s, n, first_field, (1 << Type_text)); + union_cases(s, n, first_field, (1 << Type_data)); + union_cases(s, n, first_field, (1 << Type_struct) | (1 << Type_interface) | (1 << Type_object) | (1 << Type__list)); - /* when we have defaults we have to emit each case seperately */ - for (j = 0; j < m->u.members.p.len; j++) { - struct member *u = &m->mbrs[j]; - if (u->v.num) { - str_addf(&SRC, "\tcase %s_%s:\n", n->name.str, u->m.name.str); - union_block(m, u, set); + str_addf(&s->decl, "%sunion {\n", s->dtab.str); + str_add(&s->dtab, "\t", -1); + + /* when we have defaults or groups we have to emit each case seperately */ + for (f = first_field; f < n->fields + n->n._struct.fields.p.len && in_union(f); f++) { + if (f > first_field) { + str_addf(&enums, ","); + } + + str_addf(&enums, "\n\t%s_%s = %d", n->name.str, f->f.name.str, f->f.discriminantValue); + + switch (f->f.which) { + case Field_group: + str_addf(&s->get, "%scase %s_%s:\n", s->ftab.str, n->name.str, f->f.name.str); + str_addf(&s->set, "%scase %s_%s:\n", s->ftab.str, n->name.str, f->f.name.str); + str_add(&s->ftab, "\t", -1); + define_group(s, f->group, f->f.name.str); + str_addf(&s->get, "%sbreak;\n", s->ftab.str); + str_addf(&s->set, "%sbreak;\n", s->ftab.str); + str_setlen(&s->ftab, s->ftab.len-1); + break; + + case Field_slot: + declare_slot(s, f); + if (f->v.ptrval.type || f->v.intval) { + str_addf(&s->get, "%scase %s_%s:\n", s->ftab.str, n->name.str, f->f.name.str); + str_addf(&s->set, "%scase %s_%s:\n", s->ftab.str, n->name.str, f->f.name.str); + union_block(s, f); + } + break; + + default: + break; } } - str_addf(&SRC, "\t}\n"); + str_setlen(&s->dtab, s->dtab.len - 1); + + if (union_name) { + str_addf(&s->decl, "%s} %s;\n", s->dtab.str, union_name); + } else { + str_addf(&s->decl, "%s};\n", s->dtab.str); + } + + str_addf(&s->get, "%sdefault:\n%s\tbreak;\n%s}\n", s->ftab.str, s->ftab.str, s->ftab.str); + str_addf(&s->set, "%sdefault:\n%s\tbreak;\n%s}\n", s->ftab.str, s->ftab.str, s->ftab.str); + + str_addf(&enums, "\n};\n"); + str_add(&s->enums, enums.str, enums.len); + str_release(&enums); } -static void print_member(struct member *m, const char *tab) { - switch (m->v.t.body_tag) { - case Type_voidType: - break; - case Type_boolType: - fprintf(HDR, "%s%s %s:1;\n", tab, m->v.tname, m->m.name.str); - break; - default: - fprintf(HDR, "%s%s %s;\n", tab, m->v.tname, m->m.name.str); - break; - } -} -static void define_struct(struct node *n) { +static void define_field(struct strings *s, struct field *f) { static struct str buf = STR_INIT; - int i, j, mlen = n->s.members.p.len; - struct member *mbrs = calloc(mlen, sizeof(*mbrs)); + switch (f->f.which) { + case Field_slot: + declare_slot(s, f); + set_member(&s->set, f, "p.p", s->ftab.str, strf(&buf, "%s%s", s->var.str, f->f.name.str)); + get_member(&s->get, f, "p.p", s->ftab.str, strf(&buf, "%s%s", s->var.str, f->f.name.str)); + break; - /* get list of members in code order and emit union enums */ - for (i = 0; i < mlen; i++) { - struct member *m = decode_member(mbrs, n->s.members, i); + case Field_group: + define_group(s, f->group, f->f.name.str); + break; + } +} - if (m->m.body_tag == StructNode_Member_unionMember) { - int first, ulen; +static void define_group(struct strings *s, struct node *n, const char *group_name) { + struct field *f; + int flen = n->n._struct.fields.p.len; + int ulen = n->n._struct.discriminantCount; + /* named union is where all group members are in the union */ + int named_union = (group_name && ulen == flen && ulen > 0); + int named_struct = (group_name && !named_union); - /* get union members in code order */ - read_StructNode_Union(&m->u, m->m.body.unionMember); - ulen = m->u.members.p.len; - m->mbrs = calloc(ulen, sizeof(*m->mbrs)); - for (j = 0; j < ulen; j++) { - decode_member(m->mbrs, m->u.members, j); - } + if (named_struct) { + str_addf(&s->decl, "%sstruct {\n", s->dtab.str); + str_add(&s->dtab, "\t", 1); + } - /* emit union enum definition */ - first = 1; - fprintf(HDR, "\nenum %s_%s {", n->name.str, m->m.name.str); - for (j = 0; j < ulen; j++) { - struct member *u = &m->mbrs[j]; - if (!u->is_valid) continue; - if (!first) fprintf(HDR, ","); - fprintf(HDR, "\n\t%s_%s = %d", n->name.str, u->m.name.str, u->idx); - first = 0; - } - fprintf(HDR, "\n};\n"); + if (group_name) { + str_addf(&s->var, "%s.", group_name); + } + + /* fields before the union members */ + for (f = n->fields; f < n->fields + flen && !in_union(f); f++) { + define_field(s, f); + } + + if (ulen > 0) { + do_union(s, n, f, named_union ? group_name : NULL); + + while (f < n->fields + flen && in_union(f)) + f++; + + /* fields after the unnamed union */ + for (;f < n->fields + flen; f++) { + define_field(s, f); } } - /* emit struct definition */ - fprintf(HDR, "\nstruct %s {\n", n->name.str); - for (i = 0; i < mlen; i++) { - struct member *m = &mbrs[i]; - if (!m->is_valid) - continue; - - switch (m->m.body_tag) { - case StructNode_Member_fieldMember: - print_member(m, "\t"); - break; - case StructNode_Member_unionMember: - fprintf(HDR, "\tenum %s_%s %s_tag;\n", n->name.str, m->m.name.str, m->m.name.str); - fprintf(HDR, "\tunion {\n"); - for (j = 0; j < m->u.members.p.len; j++) { - print_member(&m->mbrs[j], "\t\t"); - } - fprintf(HDR, "\t} %s;\n", m->m.name.str); - break; - } + if (named_struct) { + str_setlen(&s->dtab, s->dtab.len-1); + str_addf(&s->decl, "%s} %s;\n", s->dtab.str, group_name); } - fprintf(HDR, "};\n"); + + if (group_name) { + str_setlen(&s->var, s->var.len - strlen(group_name) - 1); + } +} + +static void define_struct(struct node *n) { + static struct strings s; + + str_reset(&s.dtab); + str_reset(&s.ftab); + str_reset(&s.get); + str_reset(&s.set); + str_reset(&s.enums); + str_reset(&s.decl); + str_reset(&s.var); + + str_add(&s.dtab, "\t", -1); + str_add(&s.ftab, "\t", -1); + str_add(&s.var, "s->", -1); + + define_group(&s, n, NULL); + + str_add(&HDR, s.enums.str, s.enums.len); + + str_addf(&HDR, "\nstruct %s {\n", n->name.str); + str_add(&HDR, s.decl.str, s.decl.len); + str_addf(&HDR, "};\n"); 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, "\tp.p = capn_new_struct(s, %d, %d);\n", 8*n->s.dataSectionWordSize, n->s.pointerSectionSize); + str_addf(&SRC, "\tp.p = capn_new_struct(s, %d, %d);\n", 8*n->n._struct.dataWordCount, n->n._struct.pointerCount); str_addf(&SRC, "\treturn p;\n"); str_addf(&SRC, "}\n"); str_addf(&SRC, "%s_list new_%s_list(struct capn_segment *s, int len) {\n", n->name.str, n->name.str); str_addf(&SRC, "\t%s_list p;\n", n->name.str); - str_addf(&SRC, "\tp.p = capn_new_list(s, len, %d, %d);\n", 8*n->s.dataSectionWordSize, n->s.pointerSectionSize); + str_addf(&SRC, "\tp.p = capn_new_list(s, len, %d, %d);\n", 8*n->n._struct.dataWordCount, n->n._struct.pointerCount); str_addf(&SRC, "\treturn p;\n"); str_addf(&SRC, "}\n"); str_addf(&SRC, "void read_%s(struct %s *s, %s_ptr p) {\n", n->name.str, n->name.str, n->name.str); - for (i = 0; i < mlen; i++) { - struct member *m = &mbrs[i]; - if (!m->is_valid) continue; - - switch (m->m.body_tag) { - case StructNode_Member_fieldMember: - get_member(m, "p.p", "\t", strf(&buf, "s->%s", m->m.name.str)); - break; - case StructNode_Member_unionMember: - do_union(n, m, 0); - break; - } - } + str_add(&SRC, s.get.str, s.get.len); str_addf(&SRC, "}\n"); - str_addf(&SRC, "int write_%s(const struct %s *s, %s_ptr p) {\n", n->name.str, n->name.str, n->name.str); - str_addf(&SRC, "\tint err = 0;\n"); - for (i = 0; i < mlen; i++) { - struct member *m = &mbrs[i]; - if (!m->is_valid) continue; - - switch (m->m.body_tag) { - case StructNode_Member_fieldMember: - set_member(m, "p.p", "\t", strf(&buf, "s->%s", m->m.name.str)); - break; - case StructNode_Member_unionMember: - do_union(n, m, 1); - break; - } - } - str_addf(&SRC, "\treturn err;\n}\n"); + str_addf(&SRC, "void write_%s(const struct %s *s, %s_ptr p) {\n", n->name.str, n->name.str, n->name.str); + str_add(&SRC, s.set.str, s.set.len); + str_addf(&SRC, "}\n"); str_addf(&SRC, "void get_%s(struct %s *s, %s_list l, int i) {\n", n->name.str, n->name.str, n->name.str); str_addf(&SRC, "\t%s_ptr p;\n", n->name.str); @@ -770,13 +840,15 @@ static void define_struct(struct node *n) { str_addf(&SRC, "\tread_%s(s, p);\n", n->name.str); str_addf(&SRC, "}\n"); - str_addf(&SRC, "int set_%s(const struct %s *s, %s_list l, int i) {\n", n->name.str, n->name.str, n->name.str); + str_addf(&SRC, "void set_%s(const struct %s *s, %s_list l, int i) {\n", n->name.str, n->name.str, n->name.str); str_addf(&SRC, "\t%s_ptr p;\n", n->name.str); str_addf(&SRC, "\tp.p = capn_getp(l.p, i);\n"); - str_addf(&SRC, "\treturn write_%s(s, p);\n", n->name.str); + str_addf(&SRC, "\twrite_%s(s, p);\n", n->name.str); str_addf(&SRC, "}\n"); } +#if 0 +Commenting out interfaces until the RPC protocol has been spec'd static int find_offset(struct str *v, int inc, uint64_t mask) { int i, j; union {uint64_t u; char c[8];} umask; @@ -822,36 +894,36 @@ static void define_method(struct node *iface, int ord) { m->m.ordinal = i; switch (m->v.t.body_tag) { - case Type_voidType: + case Type_void: break; - case Type_boolType: + case Type_bool: m->f.offset = find_offset(&buf, 1, 1); break; - case Type_int8Type: - case Type_uint8Type: + case Type_int8: + case Type_uint8: m->f.offset = find_offset(&buf, 8, 0xFF); break; - case Type_int16Type: - case Type_uint16Type: - case Type_enumType: + case Type_int16: + case Type_uint16: + case Type_enum: m->f.offset = find_offset(&buf, 16, 0xFFFF); break; - case Type_int32Type: - case Type_uint32Type: - case Type_float32Type: + case Type_int32: + case Type_uint32: + case Type_float32: m->f.offset = find_offset(&buf, 32, 0xFFFFFFFFu); break; - case Type_int64Type: - case Type_uint64Type: - case Type_float64Type: + case Type_int64: + case Type_uint64: + case Type_float64: m->f.offset = find_offset(&buf, 64, ~((uint64_t) 0)); break; - case Type_textType: - case Type_dataType: - case Type_listType: - case Type_structType: - case Type_interfaceType: - case Type_objectType: + case Type_text: + case Type_data: + case Type__list: + case Type_struct: + case Type_interface: + case Type_object: m->f.offset = ptrs++; break; } @@ -862,14 +934,14 @@ static void define_method(struct node *iface, int ord) { /* write function to initiate a call */ - fprintf(HDR, "\nint write_%s_%s(struct capn_msg*", iface->name.str, method.name.str); + str_addf(&HDR, "\nint write_%s_%s(struct capn_msg*", iface->name.str, method.name.str); str_addf(&SRC, "\nint write_%s_%s(struct capn_msg *m", iface->name.str, method.name.str); for (i = 0; i < method.params.p.len; i++) { struct member *m = &mbrs[i]; - fprintf(HDR, ", %s %s", m->v.tname, m->m.name.str); + str_addf(&HDR, ", %s %s", m->v.tname, m->m.name.str); str_addf(&SRC, ", %s a%d", m->v.tname, i); } - fprintf(HDR, ");\n"); + str_addf(&HDR, ");\n"); str_addf(&SRC, ") {\n"); str_addf(&SRC, "\tint err = 0;\n"); @@ -894,14 +966,14 @@ static void define_method(struct node *iface, int ord) { /* read function to handle a call */ if (datasz || ptrs) { - fprintf(HDR, "void read_%s_%s(struct capn_msg*", iface->name.str, method.name.str); + str_addf(&HDR, "void read_%s_%s(struct capn_msg*", iface->name.str, method.name.str); str_addf(&SRC, "void read_%s_%s(struct capn_msg *m", iface->name.str, method.name.str); for (i = 0; i < method.params.p.len; i++) { struct member *m = &mbrs[i]; - fprintf(HDR, ", %s *%s", m->v.tname, m->m.name.str); + str_addf(&HDR, ", %s *%s", m->v.tname, m->m.name.str); str_addf(&SRC, ", %s *a%d", m->v.tname, i); } - fprintf(HDR, ");\n"); + str_addf(&HDR, ");\n"); str_addf(&SRC, ") {\n"); for (i = 0; i < method.params.p.len; i++) { @@ -913,30 +985,34 @@ static void define_method(struct node *iface, int ord) { free(mbrs); } +#endif -static void declare(struct node *n, enum Node_body type, const char *format, int num) { - fprintf(HDR, "\n"); - for (n = n->first_child; n != NULL; n = n->next_child) { - if (n->n.body_tag == type) { +static void declare(struct node *file_node, const char *format, int num) { + struct node *n; + str_addf(&HDR, "\n"); + for (n = file_node->file_nodes; n != NULL; n = n->next_file_node) { + if (n->n.which == Node_struct && !n->n._struct.isGroup) { switch (num) { case 3: - fprintf(HDR, format, n->name.str, n->name.str, n->name.str); + str_addf(&HDR, format, n->name.str, n->name.str, n->name.str); break; case 2: - fprintf(HDR, format, n->name.str, n->name.str); + str_addf(&HDR, format, n->name.str, n->name.str); break; case 1: - fprintf(HDR, format, n->name.str); + str_addf(&HDR, format, n->name.str); break; } } } } + int main() { struct capn capn; CodeGeneratorRequest_ptr root; struct CodeGeneratorRequest req; - struct node *n; + struct node *file_node, *n; + struct node *all_files = NULL, *all_structs = NULL; int i, j; if (capn_init_fp(&capn, stdin, 0)) { @@ -955,30 +1031,30 @@ int main() { get_Node(&n->n, req.nodes, i); insert_node(n); - switch (n->n.body_tag) { - case Node_fileNode: - n->next = g_files; - g_files = n; - read_FileNode(&n->f, n->n.body.fileNode); + switch (n->n.which) { + case Node_file: + n->next = all_files; + all_files = n; break; - case Node_structNode: - read_StructNode(&n->s, n->n.body.structNode); - break; - case Node_enumNode: - read_EnumNode(&n->e, n->n.body.enumNode); - break; - case Node_interfaceNode: - read_InterfaceNode(&n->i, n->n.body.interfaceNode); - break; - case Node_constNode: - read_ConstNode(&n->c, n->n.body.constNode); + + case Node_struct: + n->next = all_structs; + all_structs = n; break; + default: break; } } - for (n = g_files; n != NULL; n = n->next) { + for (n = all_structs; n != NULL; n = n->next) { + n->fields = calloc(n->n._struct.fields.p.len, sizeof(n->fields[0])); + for (j = 0; j < n->n._struct.fields.p.len; j++) { + decode_field(n->fields, n->n._struct.fields, j); + } + } + + for (n = all_files; n != NULL; n = n->next) { struct str b = STR_INIT; for (i = n->n.nestedNodes.p.len-1; i >= 0; i--) { @@ -991,10 +1067,10 @@ int main() { } for (i = 0; i < req.requestedFiles.p.len; i++) { + struct CodeGeneratorRequest_RequestedFile file_req; static struct str b = STR_INIT; - struct node *s; char *p; - FILE *srcf; + FILE *srcf, *hdrf; g_valc = 0; g_valseg.len = 0; @@ -1003,71 +1079,75 @@ int main() { capn_init_malloc(&g_valcapn); capn_append_segment(&g_valcapn, &g_valseg); - n = find_node(capn_get64(req.requestedFiles, i)); - - HDR = fopen(strf(&b, "%s.h", n->n.displayName.str), "w"); - if (!HDR) { - fprintf(stderr, "failed to open %s: %s\n", b.str, strerror(errno)); - exit(2); - } + get_CodeGeneratorRequest_RequestedFile(&file_req, req.requestedFiles, i); + file_node = find_node(file_req.id); + str_reset(&HDR); str_reset(&SRC); - srcf = fopen(strf(&b, "%s.c", n->n.displayName.str), "w"); - if (!srcf) { - fprintf(stderr, "failed to open %s: %s\n", b.str, strerror(errno)); - exit(2); + + str_addf(&HDR, "#ifndef CAPN_%X%X\n", (uint32_t) (file_node->n.id >> 32), (uint32_t) file_node->n.id); + str_addf(&HDR, "#define CAPN_%X%X\n", (uint32_t) (file_node->n.id >> 32), (uint32_t) file_node->n.id); + str_addf(&HDR, "/* AUTO GENERATED - DO NOT EDIT */\n"); + str_addf(&HDR, "#include \n"); + + for (j = 0; j < file_req.imports.p.len; j++) { + struct CodeGeneratorRequest_RequestedFile_Import im; + get_CodeGeneratorRequest_RequestedFile_Import(&im, file_req.imports, j); + str_addf(&HDR, "#include \"%s.h\"\n", im.name.str); } - fprintf(HDR, "#ifndef CAPN_%X%X\n", (uint32_t) (n->n.id >> 32), (uint32_t) n->n.id); - fprintf(HDR, "#define CAPN_%X%X\n", (uint32_t) (n->n.id >> 32), (uint32_t) n->n.id); - fprintf(HDR, "/* AUTO GENERATED - DO NOT EDIT */\n"); - fprintf(HDR, "#include \n"); + str_addf(&HDR, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); - for (i = 0; i < n->f.imports.p.len; i++) { - struct FileNode_Import im; - get_FileNode_Import(&im, n->f.imports, i); - fprintf(HDR, "#include \"%s.h\"\n", im.name.str); - } + declare(file_node, "struct %s;\n", 1); + declare(file_node, "typedef struct {capn_ptr p;} %s_ptr;\n", 1); + declare(file_node, "typedef struct {capn_ptr p;} %s_list;\n", 1); - fprintf(HDR, "\n#ifdef __cplusplus\nextern \"C\" {\n#endif\n"); - - - declare(n, Node_structNode, "struct %s;\n", 1); - declare(n, Node_structNode, "typedef struct {capn_ptr p;} %s_ptr;\n", 1); - declare(n, Node_structNode, "typedef struct {capn_ptr p;} %s_list;\n", 1); - declare(n, Node_interfaceNode, "typedef struct {capn_ptr p;} %s_ptr;\n", 1); - declare(n, Node_interfaceNode, "typedef struct {capn_ptr p;} %s_list;\n", 1); - - for (s = n->first_child; s != NULL; s = s->next_child) { - switch (s->n.body_tag) { - case Node_structNode: - define_struct(s); - break; - case Node_enumNode: - define_enum(s); - break; - case Node_constNode: - define_const(s); - break; - case Node_interfaceNode: - for (j = 0; j < s->i.methods.p.len; j++) { - define_method(s, j); + for (n = file_node->file_nodes; n != NULL; n = n->next_file_node) { + switch (n->n.which) { + case Node_struct: + if (!n->n._struct.isGroup) { + define_struct(n); } break; + case Node_enum: + define_enum(n); + break; + case Node_const: + define_const(n); + break; default: break; } } - declare(n, Node_structNode, "%s_ptr new_%s(struct capn_segment*);\n", 2); - declare(n, Node_structNode, "%s_list new_%s_list(struct capn_segment*, int len);\n", 2); - declare(n, Node_structNode, "void read_%s(struct %s*, %s_ptr);\n", 3); - declare(n, Node_structNode, "int write_%s(const struct %s*, %s_ptr);\n", 3); - declare(n, Node_structNode, "void get_%s(struct %s*, %s_list, int i);\n", 3); - declare(n, Node_structNode, "int set_%s(const struct %s*, %s_list, int i);\n", 3); + declare(file_node, "%s_ptr new_%s(struct capn_segment*);\n", 2); + declare(file_node, "%s_list new_%s_list(struct capn_segment*, int len);\n", 2); + declare(file_node, "void read_%s(struct %s*, %s_ptr);\n", 3); + declare(file_node, "void write_%s(const struct %s*, %s_ptr);\n", 3); + declare(file_node, "void get_%s(struct %s*, %s_list, int i);\n", 3); + declare(file_node, "void set_%s(const struct %s*, %s_list, int i);\n", 3); - p = strrchr(n->n.displayName.str, '/'); - fprintf(srcf, "#include \"%s.h\"\n", p ? p+1 : n->n.displayName.str); + str_addf(&HDR, "\n#ifdef __cplusplus\n}\n#endif\n#endif\n"); + + /* write out the header */ + + hdrf = fopen(strf(&b, "%s.h", file_node->n.displayName.str), "w"); + if (!hdrf) { + fprintf(stderr, "failed to open %s: %s\n", b.str, strerror(errno)); + exit(2); + } + fwrite(HDR.str, 1, HDR.len, hdrf); + fclose(hdrf); + + /* write out the source */ + + srcf = fopen(strf(&b, "%s.c", file_node->n.displayName.str), "w"); + if (!srcf) { + fprintf(stderr, "failed to open %s: %s\n", b.str, strerror(errno)); + exit(2); + } + p = strrchr(file_node->n.displayName.str, '/'); + fprintf(srcf, "#include \"%s.h\"\n", p ? p+1 : file_node->n.displayName.str); fprintf(srcf, "/* AUTO GENERATED - DO NOT EDIT */\n"); if (g_val0used) @@ -1091,12 +1171,9 @@ int main() { } fwrite(SRC.str, 1, SRC.len, srcf); - - fprintf(HDR, "\n#ifdef __cplusplus\n}\n#endif\n#endif\n"); - fclose(HDR); fclose(srcf); + capn_free(&g_valcapn); - HDR = srcf = NULL; } return 0; diff --git a/compiler/schema.capnp b/compiler/schema.capnp index 2b1b5a6..950bda2 100644 --- a/compiler/schema.capnp +++ b/compiler/schema.capnp @@ -21,9 +21,10 @@ # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. -@0xb471df2f45ca32c7; +using Cxx = import "c++.capnp"; -# WARNING: This protocol is still subject to backwards-incompatible change. +@0xa93fc509624c72d9; +$Cxx.namespace("capnp::schema"); using Id = UInt64; # The globally-unique ID of a file, type, or annotation. @@ -37,13 +38,18 @@ struct Node { # # (On Zooko's triangle, this is the node's nickname.) - scopeId @2 :Id = 0; - # ID of the lexical parent node. Typically, the scope node will have a NestedNode pointing back - # at this node, but robust code should avoid relying on this. `scopeId` is zero if the node has - # no parent, which is normally only the case with files, but should be allowed for any kind of - # node (in order to make runtime type generation easier). + displayNamePrefixLength @2 :UInt32; + # If you want a shorter version of `displayName` (just naming this node, without its surrounding + # scope), chop off this many characters from the beginning of `displayName`. - nestedNodes @3 :List(NestedNode); + scopeId @3 :Id; + # ID of the lexical parent node. Typically, the scope node will have a NestedNode pointing back + # at this node, but robust code should avoid relying on this (and, in fact, group nodes are not + # listed in the outer struct's nestedNodes, since they are listed in the fields). `scopeId` is + # zero if the node has no parent, which is normally only the case with files, but should be + # allowed for any kind of node (in order to make runtime type generation easier). + + nestedNodes @4 :List(NestedNode); # List of nodes nested within this node, along with the names under which they were declared. struct NestedNode { @@ -57,80 +63,260 @@ struct Node { # robust code should avoid relying on this. } - annotations @4 :List(Annotation); + annotations @5 :List(Annotation); # Annotations applied to this node. - body @5 union { + union { # Info specific to each kind of node. - fileNode @6 :FileNode; - structNode @7 :StructNode; - enumNode @8 :EnumNode; - interfaceNode @9 :InterfaceNode; - constNode @10 :ConstNode; - annotationNode @11 :AnnotationNode; + file @6 :Void; + + struct :group { + dataWordCount @7 :UInt16; + # Size of the data section, in words. + + pointerCount @8 :UInt16; + # Size of the pointer section, in pointers (which are one word each). + + preferredListEncoding @9 :ElementSize; + # The preferred element size to use when encoding a list of this struct. If this is anything + # other than `inlineComposite` then the struct is one word or less in size and is a candidate + # for list packing optimization. + + isGroup @10 :Bool; + # If true, then this "struct" node is actually not an independent node, but merely represents + # some named union or group within a particular parent struct. This node's scopeId refers + # to the parent struct, which may itself be a union/group in yet another struct. + # + # All group nodes share the same dataWordCount and pointerCount as the top-level + # struct, and their fields live in the same ordinal and offset spaces as all other fields in + # the struct. + # + # Note that a named union is considered a special kind of group -- in fact, a named union + # is exactly equivalent to a group that contains nothing but an unnamed union. + + discriminantCount @11 :UInt16; + # Number of fields in this struct which are members of an anonymous union, and thus may + # overlap. If this is non-zero, then a 16-bit discriminant is present indicating which + # of the overlapping fields is active. This can never be 1 -- if it is non-zero, it must be + # two or more. + # + # Note that the fields of an unnamed union are considered fields of the scope containing the + # union -- an unnamed union is not its own group. So, a top-level struct may contain a + # non-zero discriminant count. Named unions, on the other hand, are equivalent to groups + # containing unnamed unions. So, a named union has its own independent schema node, with + # `isGroup` = true. + + discriminantOffset @12 :UInt32; + # If `discriminantCount` is non-zero, this is the offset of the union discriminant, in + # multiples of 16 bits. + + fields @13 :List(Field); + # Fields defined within this scope (either the struct's top-level fields, or the fields of + # a particular group; see `isGroup`). + # + # The fields are sorted by ordinal number, but note that because groups share the same + # ordinal space, the field's index in this list is not necessarily exactly its ordinal. + # On the other hand, the field's position in this list does remain the same even as the + # protocol evolves, since it is not possible to insert or remove an earlier ordinal. + # Therefore, for most use cases, if you want to identify a field by number, it may make the + # most sense to use the field's index in this list rather than its ordinal. + } + + enum :group { + enumerants@14 :List(Enumerant); + # Enumerants ordered by numeric value (ordinal). + } + + interface :group { + methods @15 :List(Method); + # Methods ordered by ordinal. + } + + const :group { + type @16 :Type; + value @17 :Value; + } + + annotation :group { + type @18 :Type; + + targetsFile @19 :Bool; + targetsConst @20 :Bool; + targetsEnum @21 :Bool; + targetsEnumerant @22 :Bool; + targetsStruct @23 :Bool; + targetsField @24 :Bool; + targetsUnion @25 :Bool; + targetsGroup @26 :Bool; + targetsInterface @27 :Bool; + targetsMethod @28 :Bool; + targetsParam @29 :Bool; + targetsAnnotation @30 :Bool; + } } } +struct Field { + # Schema for a field of a struct. + + name @0 :Text; + + codeOrder @1 :UInt16; + # Indicates where this member appeared in the code, relative to other members. + # Code ordering may have semantic relevance -- programmers tend to place related fields + # together. So, using code ordering makes sense in human-readable formats where ordering is + # otherwise irrelevant, like JSON. The values of codeOrder are tightly-packed, so the maximum + # value is count(members) - 1. Fields that are members of a union are only ordered relative to + # the other members of that union, so the maximum value there is count(union.members). + + annotations @2 :List(Annotation); + + discriminantValue @3 :UInt16 = 0xffff; + # If the field is in a union, this is the value which the union's discriminant should take when + # the field is active. If the field is not in a union, this is 0xffff (so hasDiscriminantValue() + # returns false). + + union { + slot :group { + # A regular, non-group, non-fixed-list field. + + offset @4 :UInt32; + # Offset, in units of the field's size, from the beginning of the section in which the field + # resides. E.g. for a UInt32 field, multiply this by 4 to get the byte offset from the + # beginning of the data section. + + type @5 :Type; + defaultValue @6 :Value; + } + + group :group { + # A group. + + typeId @7 :Id; + # The ID of the group's node. + } + } + + ordinal :union { + implicit @8 :Void; + explicit @9 :UInt16; + # The original ordinal number given to the field. You probably should NOT use this; if you need + # a numeric identifier for a field, use its position within the field array for its scope. + # The ordinal is given here mainly just so that the original schema text can be reproduced given + # the compiled version -- i.e. so that `capnp compile -ocapnp` can do its job. + } +} + +struct Enumerant { + # Schema for member of an enum. + + name @0 :Text; + + codeOrder @1 :UInt16; + # Specifies order in which the enumerants were declared in the code. + # Like Struct.Field.codeOrder. + + annotations @2 :List(Annotation); +} + +struct Method { + # Schema for method of an interface. + + name @0 :Text; + + codeOrder @1 :UInt16; + # Specifies order in which the methods were declared in the code. + # Like Struct.Field.codeOrder. + + params @2 :List(Param); + struct Param { + name @0 :Text; + type @1 :Type; + defaultValue @2 :Value; + annotations @3 :List(Annotation); + } + + requiredParamCount @3 :UInt16; + # One plus the index of the last parameter that has no default value. In languages where + # method calls look like function calls, this is the minimum number of parameters that must + # always be specified, while subsequent parameters are optional. + + returnType @4 :Type; + + annotations @5 :List(Annotation); +} + struct Type { # Represents a type expression. - body @0 union { - voidType @1 :Void; - boolType @2 :Void; - int8Type @3 :Void; - int16Type @4 :Void; - int32Type @5 :Void; - int64Type @6 :Void; - uint8Type @7 :Void; - uint16Type @8 :Void; - uint32Type @9 :Void; - uint64Type @10 :Void; - float32Type @11 :Void; - float64Type @12 :Void; - textType @13 :Void; - dataType @14 :Void; + union { + # The ordinals intentionally match those of Value. - listType @15 :Type; # Value = the element type. + void @0 :Void; + bool @1 :Void; + int8 @2 :Void; + int16 @3 :Void; + int32 @4 :Void; + int64 @5 :Void; + uint8 @6 :Void; + uint16 @7 :Void; + uint32 @8 :Void; + uint64 @9 :Void; + float32 @10 :Void; + float64 @11 :Void; + text @12 :Void; + data @13 :Void; - enumType @16 :Id; - structType @17 :Id; - interfaceType @18 :Id; + list :group { + elementType @14 :Type; + } - objectType @19 :Void; + enum :group { + typeId @15 :Id; + } + struct :group { + typeId @16 :Id; + } + interface :group { + typeId @17 :Id; + } + + object @18 :Void; } } struct Value { # Represents a value, e.g. a field default value, constant value, or annotation value. - body @0 union { - # Note ordinals 1 and 10 are intentionally swapped to improve union layout. - voidValue @10 :Void; - boolValue @2 :Bool; - int8Value @3 :Int8; - int16Value @4 :Int16; - int32Value @5 :Int32; - int64Value @6 :Int64; - uint8Value @7 :UInt8; - uint16Value @8 :UInt16; - uint32Value @9 :UInt32; - uint64Value @1 :UInt64; - float32Value @11 :Float32; - float64Value @12 :Float64; - textValue @13 :Text; - dataValue @14 :Data; + union { + # The ordinals intentionally match those of Type. - listValue @15 :Object; + void @0 :Void; + bool @1 :Bool; + int8 @2 :Int8; + int16 @3 :Int16; + int32 @4 :Int32; + int64 @5 :Int64; + uint8 @6 :UInt8; + uint16 @7 :UInt16; + uint32 @8 :UInt32; + uint64 @9 :UInt64; + float32 @10 :Float32; + float64 @11 :Float64; + text @12 :Text; + data @13 :Data; - enumValue @16 :UInt16; - structValue @17 :Object; + list @14 :Object; - interfaceValue @18 :Void; + enum @15 :UInt16; + struct @16 :Object; + + interface @17 :Void; # The only interface value that can be represented statically is "null", whose methods always # throw exceptions. - objectValue @19 :Object; + object @18 :Object; } } @@ -144,21 +330,6 @@ struct Annotation { value @1 :Value; } -struct FileNode { - imports @0 :List(Import); - struct Import { - id @0 :Id; - # ID of the imported file. - - name @1 :Text; - # Name which *this* file used to refer to the foreign file. This may be a relative name. - # This information is provided because it might be useful for code generation, e.g. to generate - # #include directives in C++. - # - # (On Zooko's triangle, this is the import's petname according to the importing file.) - } -} - enum ElementSize { # Possible element sizes for encoded lists. These correspond exactly to the possible values of # the 3-bit element size component of a list pointer. @@ -173,144 +344,36 @@ enum ElementSize { inlineComposite @7; } -struct StructNode { - dataSectionWordSize @0 :UInt16; - pointerSectionSize @1 :UInt16; - - preferredListEncoding @2 :ElementSize; - # The preferred element size to use when encoding a list of this struct. If this is anything - # other than `inlineComposite` then the struct is one word or less in size and is a candidate for - # list packing optimization. - - members @3 :List(Member); - # Top-level fields and unions of the struct, ordered by ordinal number, except that members of - # unions are not included in this list (because they are nested inside the union declaration). - # Note that this ordering is stable as the protocol evolves -- new members can only be added to - # the end. So, when encoding a struct as tag/value pairs with numeric tags, it actually may make - # sense to use the field's position in this list rather than the original ordinal number to - # identify fields. - - struct Member { - name @0 :Text; - - ordinal @1 :UInt16; - - codeOrder @2 :UInt16; - # Indicates where this member appeared in the code, relative to other members. - # Code ordering may have semantic relevance -- programmers tend to place related fields - # together. So, using code ordering makes sense in human-readable formats where ordering is - # otherwise irrelevant, like JSON. The values of codeOrder are tightly-packed, so for unions - # and non-union fields the maximum value of codeOrder is count(fields) + count(unions). - # Fields that are members of a union are only ordered relative to the other members of that - # union, so the maximum value there is count(union.fields). - - annotations @3 :List(Annotation); - - body @4 union { - # More member types could be added over time. Consumers should skip those that they - # don't understand. - - fieldMember @5 :Field; - unionMember @6 :Union; - } - } - - struct Field { - offset @0 :UInt32; - # Offset, in units of the field's size, from the beginning of the section in which the field - # resides. E.g. for a UInt32 field, multiply this by 4 to get the byte offset from the - # beginning of the data section. - - type @1 :Type; - defaultValue @2 :Value; - } - - struct Union { - discriminantOffset @0 :UInt32; - # Offset of the union's 16-bit discriminant within the struct's data section, in 16-bit units. - - members @1 :List(Member); - # Fields of this union, ordered by ordinal. Currently all members are fields, but - # consumers should skip member types that they don't understand. The first member in this list - # gets discriminant value zero, the next gets one, and so on. - # - # TODO(soon): Discriminant zero should be reserved to mean "unset", unless the first field in - # the union actually predates the union (it was retroactively unionized), in which case it - # gets discriminant zero. - } -} - -struct EnumNode { - enumerants @0 :List(Enumerant); - # Enumerants, in order by ordinal. - - struct Enumerant { - name @0 :Text; - - codeOrder @1 :UInt16; - # Specifies order in which the enumerants were declared in the code. - # Like Struct.Field.codeOrder. - - annotations @2 :List(Annotation); - } -} - -struct InterfaceNode { - methods @0 :List(Method); - # Methods, in order by ordinal. - - struct Method { - name @0 :Text; - - codeOrder @1 :UInt16; - # Specifies order in which the methods were declared in the code. - # Like Struct.Field.codeOrder. - - params @2 :List(Param); - struct Param { - name @0 :Text; - type @1 :Type; - defaultValue @2 :Value; - annotations @3 :List(Annotation); - } - - requiredParamCount @3 :UInt16; - # One plus the index of the last parameter that has no default value. In languages where - # method calls look like function calls, this is the minimum number of parameters that must - # always be specified, while subsequent parameters are optional. - - returnType @4 :Type; - - annotations @5 :List(Annotation); - } -} - -struct ConstNode { - type @0 :Type; - value @1 :Value; -} - -struct AnnotationNode { - type @0 :Type; - - targetsFile @1 :Bool; - targetsConst @2 :Bool; - targetsEnum @3 :Bool; - targetsEnumerant @4 :Bool; - targetsStruct @5 :Bool; - targetsField @6 :Bool; - targetsUnion @7 :Bool; - targetsInterface @8 :Bool; - targetsMethod @9 :Bool; - targetsParam @10 :Bool; - targetsAnnotation @11 :Bool; -} - struct CodeGeneratorRequest { nodes @0 :List(Node); # All nodes parsed by the compiler, including for the files on the command line and their # imports. - requestedFiles @1 :List(Id); - # IDs of files which were listed on the command line. + requestedFiles @1 :List(RequestedFile); + # Files which were listed on the command line. + + struct RequestedFile { + id @0 :Id; + # ID of the file. + + filename @1 :Text; + # Name of the file as it appeared on the command-line (minus the src-prefix). You may use + # this to decide where to write the output. + + imports @2 :List(Import); + # List of all imported paths seen in this file. + + struct Import { + id @0 :Id; + # ID of the imported file. + + name @1 :Text; + # Name which *this* file used to refer to the foreign file. This may be a relative name. + # This information is provided because it might be useful for code generation, e.g. to + # generate #include directives in C++. We don't put this in Node.file because this + # information is only meaningful at compile time anyway. + # + # (On Zooko's triangle, this is the import's petname according to the importing file.) + } + } } diff --git a/compiler/schema.capnp.c b/compiler/schema.capnp.c index c298ca4..3045d02 100644 --- a/compiler/schema.capnp.c +++ b/compiler/schema.capnp.c @@ -4,63 +4,119 @@ static const capn_text capn_val0 = {0,""}; Node_ptr new_Node(struct capn_segment *s) { Node_ptr p; - p.p = capn_new_struct(s, 24, 4); + p.p = capn_new_struct(s, 40, 5); return p; } Node_list new_Node_list(struct capn_segment *s, int len) { Node_list p; - p.p = capn_new_list(s, len, 24, 4); + p.p = capn_new_list(s, len, 40, 5); return p; } void read_Node(struct Node *s, Node_ptr p) { s->id = capn_read64(p.p, 0); s->displayName = capn_get_text(p.p, 0, capn_val0); - s->scopeId = capn_read64(p.p, 8); + s->displayNamePrefixLength = capn_read32(p.p, 8); + s->scopeId = capn_read64(p.p, 16); s->nestedNodes.p = capn_getp(p.p, 1); s->annotations.p = capn_getp(p.p, 2); - s->body_tag = (enum Node_body) capn_read16(p.p, 16); + s->which = (enum Node_which) capn_read16(p.p, 12); - switch (s->body_tag) { - case Node_fileNode: - case Node_structNode: - case Node_enumNode: - case Node_interfaceNode: - case Node_constNode: - case Node_annotationNode: - s->body.annotationNode.p = capn_getp(p.p, 3); + switch (s->which) { + case Node_struct: + s->_struct.dataWordCount = capn_read16(p.p, 14); + s->_struct.pointerCount = capn_read16(p.p, 24); + s->_struct.preferredListEncoding = (enum ElementSize) capn_read16(p.p, 26); + s->_struct.isGroup = (capn_read8(p.p, 28) & 1) != 0; + s->_struct.discriminantCount = capn_read16(p.p, 30); + s->_struct.discriminantOffset = capn_read16(p.p, 32); + s->_struct.fields.p = capn_getp(p.p, 3); + break; + case Node_enum: + s->_enum.enumerants.p = capn_getp(p.p, 3); + break; + case Node_interface: + s->_interface.methods.p = capn_getp(p.p, 3); + break; + case Node_const: + s->_const.type.p = capn_getp(p.p, 3); + s->_const.value.p = capn_getp(p.p, 3); + break; + case Node_annotation: + s->annotation.type.p = capn_getp(p.p, 3); + s->annotation.targetsFile = (capn_read8(p.p, 14) & 1) != 0; + s->annotation.targetsConst = (capn_read8(p.p, 14) & 2) != 0; + s->annotation.targetsEnum = (capn_read8(p.p, 14) & 4) != 0; + s->annotation.targetsEnumerant = (capn_read8(p.p, 14) & 8) != 0; + s->annotation.targetsStruct = (capn_read8(p.p, 14) & 16) != 0; + s->annotation.targetsField = (capn_read8(p.p, 14) & 32) != 0; + s->annotation.targetsUnion = (capn_read8(p.p, 14) & 64) != 0; + s->annotation.targetsGroup = (capn_read8(p.p, 14) & 128) != 0; + s->annotation.targetsInterface = (capn_read8(p.p, 15) & 1) != 0; + s->annotation.targetsMethod = (capn_read8(p.p, 15) & 2) != 0; + s->annotation.targetsParam = (capn_read8(p.p, 15) & 4) != 0; + s->annotation.targetsAnnotation = (capn_read8(p.p, 15) & 8) != 0; + break; + default: break; } } -int write_Node(const struct Node *s, Node_ptr p) { - int err = 0; - err = err || capn_write64(p.p, 0, s->id); - err = err || capn_set_text(p.p, 0, s->displayName); - err = err || capn_write64(p.p, 8, s->scopeId); - err = err || capn_setp(p.p, 1, s->nestedNodes.p); - err = err || capn_setp(p.p, 2, s->annotations.p); - err = err || capn_write16(p.p, 16, s->body_tag); +void write_Node(const struct Node *s, Node_ptr p) { + capn_write64(p.p, 0, s->id); + capn_set_text(p.p, 0, s->displayName); + capn_write16(p.p, 8, s->displayNamePrefixLength); + capn_write64(p.p, 16, s->scopeId); + capn_setp(p.p, 1, s->nestedNodes.p); + capn_setp(p.p, 2, s->annotations.p); + capn_write16(p.p, 12, s->which); - switch (s->body_tag) { - case Node_fileNode: - case Node_structNode: - case Node_enumNode: - case Node_interfaceNode: - case Node_constNode: - case Node_annotationNode: - err = err || capn_setp(p.p, 3, s->body.annotationNode.p); + switch (s->which) { + case Node_struct: + capn_write16(p.p, 14, s->_struct.dataWordCount); + capn_write16(p.p, 24, s->_struct.pointerCount ); + capn_write16(p.p, 26, s->_struct.preferredListEncoding); + capn_write1(p.p, 224, s->_struct.isGroup != 0); + capn_write16(p.p, 30, s->_struct.discriminantCount); + capn_write16(p.p, 32, s->_struct.discriminantOffset); + capn_setp(p.p, 3, s->_struct.fields.p ); + break; + case Node_enum: + capn_setp(p.p, 3, s->_enum.enumerants.p); + break; + case Node_interface: + capn_setp(p.p, 3, s->_interface.methods.p); + break; + case Node_const: + capn_setp(p.p, 3, s->_const.type.p); + capn_setp(p.p, 3, s->_const.value.p); + break; + case Node_annotation: + capn_setp(p.p, 3, s->annotation.type.p); + capn_write1(p.p, 112, s->annotation.targetsFile != 0); + capn_write1(p.p, 113, s->annotation.targetsConst != 0); + capn_write1(p.p, 114, s->annotation.targetsEnum != 0); + capn_write1(p.p, 115, s->annotation.targetsEnumerant != 0); + capn_write1(p.p, 116, s->annotation.targetsStruct != 0); + capn_write1(p.p, 117, s->annotation.targetsField != 0); + capn_write1(p.p, 118, s->annotation.targetsUnion != 0); + capn_write1(p.p, 118, s->annotation.targetsGroup != 0); + capn_write1(p.p, 120, s->annotation.targetsInterface != 0); + capn_write1(p.p, 121, s->annotation.targetsMethod != 0); + capn_write1(p.p, 122, s->annotation.targetsParam != 0); + capn_write1(p.p, 123, s->annotation.targetsAnnotation != 0); + break; + default: break; } - return err; } void get_Node(struct Node *s, Node_list l, int i) { Node_ptr p; p.p = capn_getp(l.p, i); read_Node(s, p); } -int set_Node(const struct Node *s, Node_list l, int i) { +void set_Node(const struct Node *s, Node_list l, int i) { Node_ptr p; p.p = capn_getp(l.p, i); - return write_Node(s, p); + write_Node(s, p); } Node_NestedNode_ptr new_Node_NestedNode(struct capn_segment *s) { @@ -77,21 +133,201 @@ void read_Node_NestedNode(struct Node_NestedNode *s, Node_NestedNode_ptr p) { s->name = capn_get_text(p.p, 0, capn_val0); s->id = capn_read64(p.p, 0); } -int write_Node_NestedNode(const struct Node_NestedNode *s, Node_NestedNode_ptr p) { - int err = 0; - err = err || capn_set_text(p.p, 0, s->name); - err = err || capn_write64(p.p, 0, s->id); - return err; +void write_Node_NestedNode(const struct Node_NestedNode *s, Node_NestedNode_ptr p) { + capn_set_text(p.p, 0, s->name); + capn_write64(p.p, 0, s->id); } void get_Node_NestedNode(struct Node_NestedNode *s, Node_NestedNode_list l, int i) { Node_NestedNode_ptr p; p.p = capn_getp(l.p, i); read_Node_NestedNode(s, p); } -int set_Node_NestedNode(const struct Node_NestedNode *s, Node_NestedNode_list l, int i) { +void set_Node_NestedNode(const struct Node_NestedNode *s, Node_NestedNode_list l, int i) { Node_NestedNode_ptr p; p.p = capn_getp(l.p, i); - return write_Node_NestedNode(s, p); + write_Node_NestedNode(s, p); +} + +Field_ptr new_Field(struct capn_segment *s) { + Field_ptr p; + p.p = capn_new_struct(s, 24, 4); + return p; +} +Field_list new_Field_list(struct capn_segment *s, int len) { + Field_list p; + p.p = capn_new_list(s, len, 24, 4); + return p; +} +void read_Field(struct Field *s, Field_ptr p) { + s->name = capn_get_text(p.p, 0, capn_val0); + s->codeOrder = capn_read16(p.p, 0); + s->annotations.p = capn_getp(p.p, 1); + s->discriminantValue = capn_read16(p.p, 2) ^ 65535; + s->which = (enum Field_which) capn_read16(p.p, 8); + + switch (s->which) { + case Field_slot: + s->slot.offset = capn_read32(p.p, 4); + s->slot.type.p = capn_getp(p.p, 2); + s->slot.defaultValue.p = capn_getp(p.p, 3); + break; + case Field_group: + s->group.typeId = capn_read64(p.p, 16); + break; + default: + break; + } + + s->ordinal_which = (enum Field_ordinal) capn_read16(p.p, 10); + + switch (s->ordinal_which) { + case Field_explicit: + s->ordinal._explicit = capn_read16(p.p, 12); + break; + default: + break; + } +} +void write_Field(const struct Field *s, Field_ptr p) { + capn_set_text(p.p, 0, s->name); + capn_write16(p.p, 0, s->codeOrder); + capn_setp(p.p, 1, s->annotations.p); + capn_write16(p.p, 2, s->discriminantValue ^ 65535); + capn_write16(p.p, 8, s->which); + + switch (s->which) { + case Field_slot: + capn_write32(p.p, 4, s->slot.offset); + capn_setp(p.p, 2, s->slot.type.p); + capn_setp(p.p, 3, s->slot.defaultValue.p); + break; + case Field_group: + capn_write64(p.p, 16, s->group.typeId); + break; + default: + break; + } + + capn_write16(p.p, 10, s->ordinal_which); + + switch (s->ordinal_which) { + case Field_explicit: + capn_write16(p.p, 12, s->ordinal._explicit); + break; + default: + break; + } +} +void get_Field(struct Field *s, Field_list l, int i) { + Field_ptr p; + p.p = capn_getp(l.p, i); + read_Field(s, p); +} +void set_Field(const struct Field *s, Field_list l, int i) { + Field_ptr p; + p.p = capn_getp(l.p, i); + write_Field(s, p); +} + +Enumerant_ptr new_Enumerant(struct capn_segment *s) { + Enumerant_ptr p; + p.p = capn_new_struct(s, 8, 2); + return p; +} +Enumerant_list new_Enumerant_list(struct capn_segment *s, int len) { + Enumerant_list p; + p.p = capn_new_list(s, len, 8, 2); + return p; +} +void read_Enumerant(struct Enumerant *s, Enumerant_ptr p) { + s->name = capn_get_text(p.p, 0, capn_val0); + s->codeOrder = capn_read16(p.p, 0); + s->annotations.p = capn_getp(p.p, 1); +} +void write_Enumerant(const struct Enumerant *s, Enumerant_ptr p) { + capn_set_text(p.p, 0, s->name); + capn_write16(p.p, 0, s->codeOrder); + capn_setp(p.p, 1, s->annotations.p); +} +void get_Enumerant(struct Enumerant *s, Enumerant_list l, int i) { + Enumerant_ptr p; + p.p = capn_getp(l.p, i); + read_Enumerant(s, p); +} +void set_Enumerant(const struct Enumerant *s, Enumerant_list l, int i) { + Enumerant_ptr p; + p.p = capn_getp(l.p, i); + write_Enumerant(s, p); +} + +Method_ptr new_Method(struct capn_segment *s) { + Method_ptr p; + p.p = capn_new_struct(s, 8, 4); + return p; +} +Method_list new_Method_list(struct capn_segment *s, int len) { + Method_list p; + p.p = capn_new_list(s, len, 8, 4); + return p; +} +void read_Method(struct Method *s, Method_ptr p) { + s->name = capn_get_text(p.p, 0, capn_val0); + s->codeOrder = capn_read16(p.p, 0); + s->params.p = capn_getp(p.p, 1); + s->requiredParamCount = capn_read16(p.p, 2); + s->returnType.p = capn_getp(p.p, 2); + s->annotations.p = capn_getp(p.p, 3); +} +void write_Method(const struct Method *s, Method_ptr p) { + capn_set_text(p.p, 0, s->name); + capn_write16(p.p, 0, s->codeOrder); + capn_setp(p.p, 1, s->params.p); + capn_write16(p.p, 2, s->requiredParamCount); + capn_setp(p.p, 2, s->returnType.p); + capn_setp(p.p, 3, s->annotations.p); +} +void get_Method(struct Method *s, Method_list l, int i) { + Method_ptr p; + p.p = capn_getp(l.p, i); + read_Method(s, p); +} +void set_Method(const struct Method *s, Method_list l, int i) { + Method_ptr p; + p.p = capn_getp(l.p, i); + write_Method(s, p); +} + +Method_Param_ptr new_Method_Param(struct capn_segment *s) { + Method_Param_ptr p; + p.p = capn_new_struct(s, 0, 4); + return p; +} +Method_Param_list new_Method_Param_list(struct capn_segment *s, int len) { + Method_Param_list p; + p.p = capn_new_list(s, len, 0, 4); + return p; +} +void read_Method_Param(struct Method_Param *s, Method_Param_ptr p) { + s->name = capn_get_text(p.p, 0, capn_val0); + s->type.p = capn_getp(p.p, 1); + s->defaultValue.p = capn_getp(p.p, 2); + s->annotations.p = capn_getp(p.p, 3); +} +void write_Method_Param(const struct Method_Param *s, Method_Param_ptr p) { + capn_set_text(p.p, 0, s->name); + capn_setp(p.p, 1, s->type.p); + capn_setp(p.p, 2, s->defaultValue.p); + capn_setp(p.p, 3, s->annotations.p); +} +void get_Method_Param(struct Method_Param *s, Method_Param_list l, int i) { + Method_Param_ptr p; + p.p = capn_getp(l.p, i); + read_Method_Param(s, p); +} +void set_Method_Param(const struct Method_Param *s, Method_Param_list l, int i) { + Method_Param_ptr p; + p.p = capn_getp(l.p, i); + write_Method_Param(s, p); } Type_ptr new_Type(struct capn_segment *s) { @@ -105,73 +341,51 @@ Type_list new_Type_list(struct capn_segment *s, int len) { return p; } void read_Type(struct Type *s, Type_ptr p) { - s->body_tag = (enum Type_body) capn_read16(p.p, 0); + s->which = (enum Type_which) capn_read16(p.p, 0); - switch (s->body_tag) { - case Type_voidType: - case Type_boolType: - case Type_int8Type: - case Type_int16Type: - case Type_int32Type: - case Type_int64Type: - case Type_uint8Type: - case Type_uint16Type: - case Type_uint32Type: - case Type_uint64Type: - case Type_float32Type: - case Type_float64Type: - case Type_textType: - case Type_dataType: - case Type_objectType: + switch (s->which) { + case Type__list: + s->list.elementType.p = capn_getp(p.p, 0); break; - case Type_enumType: - case Type_structType: - case Type_interfaceType: - s->body.interfaceType = capn_read64(p.p, 8); + case Type_enum: + s->_enum.typeId = capn_read64(p.p, 8); break; - case Type_listType: - s->body.listType.p = capn_getp(p.p, 0); + case Type_struct: + s->_struct.typeId = capn_read64(p.p, 8); + break; + case Type_interface: + s->_interface.typeId = capn_read64(p.p, 8); + break; + default: break; } } -int write_Type(const struct Type *s, Type_ptr p) { - int err = 0; - err = err || capn_write16(p.p, 0, s->body_tag); +void write_Type(const struct Type *s, Type_ptr p) { + capn_write16(p.p, 0, s->which); - switch (s->body_tag) { - case Type_voidType: - case Type_boolType: - case Type_int8Type: - case Type_int16Type: - case Type_int32Type: - case Type_int64Type: - case Type_uint8Type: - case Type_uint16Type: - case Type_uint32Type: - case Type_uint64Type: - case Type_float32Type: - case Type_float64Type: - case Type_textType: - case Type_dataType: - case Type_objectType: + switch (s->which) { + case Type__list: + capn_setp(p.p, 0, s->list.elementType.p); break; - case Type_enumType: - case Type_structType: - case Type_interfaceType: - err = err || capn_write64(p.p, 8, s->body.interfaceType); + case Type_enum: + capn_write64(p.p, 8, s->_enum.typeId); break; - case Type_listType: - err = err || capn_setp(p.p, 0, s->body.listType.p); + case Type_struct: + capn_write64(p.p, 8, s->_struct.typeId); + break; + case Type_interface: + capn_write64(p.p, 8, s->_interface.typeId); + break; + default: break; } - return err; } void get_Type(struct Type *s, Type_list l, int i) { Type_ptr p; p.p = capn_getp(l.p, i); read_Type(s, p); } -int set_Type(const struct Type *s, Type_list l, int i) { +void set_Type(const struct Type *s, Type_list l, int i) { Type_ptr p; p.p = capn_getp(l.p, i); return write_Type(s, p); @@ -188,97 +402,93 @@ Value_list new_Value_list(struct capn_segment *s, int len) { return p; } void read_Value(struct Value *s, Value_ptr p) { - s->body_tag = (enum Value_body) capn_read16(p.p, 0); + s->which = (enum Value_which) capn_read16(p.p, 0); - switch (s->body_tag) { - case Value_voidValue: - case Value_interfaceValue: + switch (s->which) { + case Value_bool: + s->_bool = (capn_read8(p.p, 8) & 1) != 0; break; - case Value_boolValue: - s->body.boolValue = (capn_read8(p.p, 8) & 1) != 0; + case Value_int8: + case Value_uint8: + s->uint8 = capn_read8(p.p, 2); break; - case Value_int8Value: - case Value_uint8Value: - s->body.uint8Value = capn_read8(p.p, 8); + case Value_int16: + case Value_uint16: + case Value_enum: + s->_enum = capn_read16(p.p, 2); break; - case Value_int16Value: - case Value_uint16Value: - case Value_enumValue: - s->body.enumValue = capn_read16(p.p, 8); + case Value_int32: + case Value_uint32: + case Value_float32: + s->float32 = capn_to_f32(capn_read32(p.p, 4)); break; - case Value_int32Value: - case Value_uint32Value: - case Value_float32Value: - s->body.float32Value = capn_to_f32(capn_read32(p.p, 8)); + case Value_int64: + case Value_uint64: + case Value_float64: + s->float64 = capn_to_f64(capn_read64(p.p, 8)); break; - case Value_int64Value: - case Value_uint64Value: - case Value_float64Value: - s->body.float64Value = capn_to_f64(capn_read64(p.p, 8)); + case Value_text: + s->text = capn_get_text(p.p, 0, capn_val0); break; - case Value_textValue: - s->body.textValue = capn_get_text(p.p, 0, capn_val0); + case Value_data: + s->data = capn_get_data(p.p, 0); break; - case Value_dataValue: - s->body.dataValue = capn_get_data(p.p, 0); + case Value__list: + case Value_struct: + case Value_object: + s->object = capn_getp(p.p, 0); break; - case Value_listValue: - case Value_structValue: - case Value_objectValue: - s->body.objectValue = capn_getp(p.p, 0); + default: break; } } -int write_Value(const struct Value *s, Value_ptr p) { - int err = 0; - err = err || capn_write16(p.p, 0, s->body_tag); +void write_Value(const struct Value *s, Value_ptr p) { + capn_write16(p.p, 0, s->which); - switch (s->body_tag) { - case Value_voidValue: - case Value_interfaceValue: + switch (s->which) { + case Value_bool: + capn_write1(p.p, 16, s->_bool != 0); break; - case Value_boolValue: - err = err || capn_write1(p.p, 64, s->body.boolValue != 0); + case Value_int8: + case Value_uint8: + capn_write8(p.p, 2, s->uint8); break; - case Value_int8Value: - case Value_uint8Value: - err = err || capn_write8(p.p, 8, s->body.uint8Value); + case Value_int16: + case Value_uint16: + case Value_enum: + capn_write16(p.p, 2, s->_enum); break; - case Value_int16Value: - case Value_uint16Value: - case Value_enumValue: - err = err || capn_write16(p.p, 8, s->body.enumValue); + case Value_int32: + case Value_uint32: + case Value_float32: + capn_write32(p.p, 4, capn_from_f32(s->float32)); break; - case Value_int32Value: - case Value_uint32Value: - case Value_float32Value: - err = err || capn_write32(p.p, 8, capn_from_f32(s->body.float32Value)); + case Value_int64: + case Value_uint64: + case Value_float64: + capn_write64(p.p, 8, capn_from_f64(s->float64)); break; - case Value_int64Value: - case Value_uint64Value: - case Value_float64Value: - err = err || capn_write64(p.p, 8, capn_from_f64(s->body.float64Value)); + case Value_text: + capn_set_text(p.p, 0, s->text); break; - case Value_textValue: - err = err || capn_set_text(p.p, 0, s->body.textValue); + case Value_data: + capn_setp(p.p, 0, s->data.p); break; - case Value_dataValue: - err = err || capn_setp(p.p, 0, s->body.dataValue.p); + case Value__list: + case Value_struct: + case Value_object: + capn_setp(p.p, 0, s->object); break; - case Value_listValue: - case Value_structValue: - case Value_objectValue: - err = err || capn_setp(p.p, 0, s->body.objectValue); + default: break; } - return err; } void get_Value(struct Value *s, Value_list l, int i) { Value_ptr p; p.p = capn_getp(l.p, i); read_Value(s, p); } -int set_Value(const struct Value *s, Value_list l, int i) { +void set_Value(const struct Value *s, Value_list l, int i) { Value_ptr p; p.p = capn_getp(l.p, i); return write_Value(s, p); @@ -298,478 +508,19 @@ void read_Annotation(struct Annotation *s, Annotation_ptr p) { s->id = capn_read64(p.p, 0); s->value.p = capn_getp(p.p, 0); } -int write_Annotation(const struct Annotation *s, Annotation_ptr p) { - int err = 0; - err = err || capn_write64(p.p, 0, s->id); - err = err || capn_setp(p.p, 0, s->value.p); - return err; +void write_Annotation(const struct Annotation *s, Annotation_ptr p) { + capn_write64(p.p, 0, s->id); + capn_setp(p.p, 0, s->value.p); } void get_Annotation(struct Annotation *s, Annotation_list l, int i) { Annotation_ptr p; p.p = capn_getp(l.p, i); read_Annotation(s, p); } -int set_Annotation(const struct Annotation *s, Annotation_list l, int i) { +void set_Annotation(const struct Annotation *s, Annotation_list l, int i) { Annotation_ptr p; p.p = capn_getp(l.p, i); - return write_Annotation(s, p); -} - -FileNode_ptr new_FileNode(struct capn_segment *s) { - FileNode_ptr p; - p.p = capn_new_struct(s, 0, 1); - return p; -} -FileNode_list new_FileNode_list(struct capn_segment *s, int len) { - FileNode_list p; - p.p = capn_new_list(s, len, 0, 1); - return p; -} -void read_FileNode(struct FileNode *s, FileNode_ptr p) { - s->imports.p = capn_getp(p.p, 0); -} -int write_FileNode(const struct FileNode *s, FileNode_ptr p) { - int err = 0; - err = err || capn_setp(p.p, 0, s->imports.p); - return err; -} -void get_FileNode(struct FileNode *s, FileNode_list l, int i) { - FileNode_ptr p; - p.p = capn_getp(l.p, i); - read_FileNode(s, p); -} -int set_FileNode(const struct FileNode *s, FileNode_list l, int i) { - FileNode_ptr p; - p.p = capn_getp(l.p, i); - return write_FileNode(s, p); -} - -FileNode_Import_ptr new_FileNode_Import(struct capn_segment *s) { - FileNode_Import_ptr p; - p.p = capn_new_struct(s, 8, 1); - return p; -} -FileNode_Import_list new_FileNode_Import_list(struct capn_segment *s, int len) { - FileNode_Import_list p; - p.p = capn_new_list(s, len, 8, 1); - return p; -} -void read_FileNode_Import(struct FileNode_Import *s, FileNode_Import_ptr p) { - s->id = capn_read64(p.p, 0); - s->name = capn_get_text(p.p, 0, capn_val0); -} -int write_FileNode_Import(const struct FileNode_Import *s, FileNode_Import_ptr p) { - int err = 0; - err = err || capn_write64(p.p, 0, s->id); - err = err || capn_set_text(p.p, 0, s->name); - return err; -} -void get_FileNode_Import(struct FileNode_Import *s, FileNode_Import_list l, int i) { - FileNode_Import_ptr p; - p.p = capn_getp(l.p, i); - read_FileNode_Import(s, p); -} -int set_FileNode_Import(const struct FileNode_Import *s, FileNode_Import_list l, int i) { - FileNode_Import_ptr p; - p.p = capn_getp(l.p, i); - return write_FileNode_Import(s, p); -} - -StructNode_ptr new_StructNode(struct capn_segment *s) { - StructNode_ptr p; - p.p = capn_new_struct(s, 8, 1); - return p; -} -StructNode_list new_StructNode_list(struct capn_segment *s, int len) { - StructNode_list p; - p.p = capn_new_list(s, len, 8, 1); - return p; -} -void read_StructNode(struct StructNode *s, StructNode_ptr p) { - s->dataSectionWordSize = capn_read16(p.p, 0); - s->pointerSectionSize = capn_read16(p.p, 2); - s->preferredListEncoding = (enum ElementSize) capn_read16(p.p, 4); - s->members.p = capn_getp(p.p, 0); -} -int write_StructNode(const struct StructNode *s, StructNode_ptr p) { - int err = 0; - err = err || capn_write16(p.p, 0, s->dataSectionWordSize); - err = err || capn_write16(p.p, 2, s->pointerSectionSize); - err = err || capn_write16(p.p, 4, (uint16_t) s->preferredListEncoding); - err = err || capn_setp(p.p, 0, s->members.p); - return err; -} -void get_StructNode(struct StructNode *s, StructNode_list l, int i) { - StructNode_ptr p; - p.p = capn_getp(l.p, i); - read_StructNode(s, p); -} -int set_StructNode(const struct StructNode *s, StructNode_list l, int i) { - StructNode_ptr p; - p.p = capn_getp(l.p, i); - return write_StructNode(s, p); -} - -StructNode_Member_ptr new_StructNode_Member(struct capn_segment *s) { - StructNode_Member_ptr p; - p.p = capn_new_struct(s, 8, 3); - return p; -} -StructNode_Member_list new_StructNode_Member_list(struct capn_segment *s, int len) { - StructNode_Member_list p; - p.p = capn_new_list(s, len, 8, 3); - return p; -} -void read_StructNode_Member(struct StructNode_Member *s, StructNode_Member_ptr p) { - s->name = capn_get_text(p.p, 0, capn_val0); - s->ordinal = capn_read16(p.p, 0); - s->codeOrder = capn_read16(p.p, 2); - s->annotations.p = capn_getp(p.p, 1); - s->body_tag = (enum StructNode_Member_body) capn_read16(p.p, 4); - - switch (s->body_tag) { - case StructNode_Member_fieldMember: - case StructNode_Member_unionMember: - s->body.unionMember.p = capn_getp(p.p, 2); - break; - } -} -int write_StructNode_Member(const struct StructNode_Member *s, StructNode_Member_ptr p) { - int err = 0; - err = err || capn_set_text(p.p, 0, s->name); - err = err || capn_write16(p.p, 0, s->ordinal); - err = err || capn_write16(p.p, 2, s->codeOrder); - err = err || capn_setp(p.p, 1, s->annotations.p); - err = err || capn_write16(p.p, 4, s->body_tag); - - switch (s->body_tag) { - case StructNode_Member_fieldMember: - case StructNode_Member_unionMember: - err = err || capn_setp(p.p, 2, s->body.unionMember.p); - break; - } - return err; -} -void get_StructNode_Member(struct StructNode_Member *s, StructNode_Member_list l, int i) { - StructNode_Member_ptr p; - p.p = capn_getp(l.p, i); - read_StructNode_Member(s, p); -} -int set_StructNode_Member(const struct StructNode_Member *s, StructNode_Member_list l, int i) { - StructNode_Member_ptr p; - p.p = capn_getp(l.p, i); - return write_StructNode_Member(s, p); -} - -StructNode_Field_ptr new_StructNode_Field(struct capn_segment *s) { - StructNode_Field_ptr p; - p.p = capn_new_struct(s, 8, 2); - return p; -} -StructNode_Field_list new_StructNode_Field_list(struct capn_segment *s, int len) { - StructNode_Field_list p; - p.p = capn_new_list(s, len, 8, 2); - return p; -} -void read_StructNode_Field(struct StructNode_Field *s, StructNode_Field_ptr p) { - s->offset = capn_read32(p.p, 0); - s->type.p = capn_getp(p.p, 0); - s->defaultValue.p = capn_getp(p.p, 1); -} -int write_StructNode_Field(const struct StructNode_Field *s, StructNode_Field_ptr p) { - int err = 0; - err = err || capn_write32(p.p, 0, s->offset); - err = err || capn_setp(p.p, 0, s->type.p); - err = err || capn_setp(p.p, 1, s->defaultValue.p); - return err; -} -void get_StructNode_Field(struct StructNode_Field *s, StructNode_Field_list l, int i) { - StructNode_Field_ptr p; - p.p = capn_getp(l.p, i); - read_StructNode_Field(s, p); -} -int set_StructNode_Field(const struct StructNode_Field *s, StructNode_Field_list l, int i) { - StructNode_Field_ptr p; - p.p = capn_getp(l.p, i); - return write_StructNode_Field(s, p); -} - -StructNode_Union_ptr new_StructNode_Union(struct capn_segment *s) { - StructNode_Union_ptr p; - p.p = capn_new_struct(s, 8, 1); - return p; -} -StructNode_Union_list new_StructNode_Union_list(struct capn_segment *s, int len) { - StructNode_Union_list p; - p.p = capn_new_list(s, len, 8, 1); - return p; -} -void read_StructNode_Union(struct StructNode_Union *s, StructNode_Union_ptr p) { - s->discriminantOffset = capn_read32(p.p, 0); - s->members.p = capn_getp(p.p, 0); -} -int write_StructNode_Union(const struct StructNode_Union *s, StructNode_Union_ptr p) { - int err = 0; - err = err || capn_write32(p.p, 0, s->discriminantOffset); - err = err || capn_setp(p.p, 0, s->members.p); - return err; -} -void get_StructNode_Union(struct StructNode_Union *s, StructNode_Union_list l, int i) { - StructNode_Union_ptr p; - p.p = capn_getp(l.p, i); - read_StructNode_Union(s, p); -} -int set_StructNode_Union(const struct StructNode_Union *s, StructNode_Union_list l, int i) { - StructNode_Union_ptr p; - p.p = capn_getp(l.p, i); - return write_StructNode_Union(s, p); -} - -EnumNode_ptr new_EnumNode(struct capn_segment *s) { - EnumNode_ptr p; - p.p = capn_new_struct(s, 0, 1); - return p; -} -EnumNode_list new_EnumNode_list(struct capn_segment *s, int len) { - EnumNode_list p; - p.p = capn_new_list(s, len, 0, 1); - return p; -} -void read_EnumNode(struct EnumNode *s, EnumNode_ptr p) { - s->enumerants.p = capn_getp(p.p, 0); -} -int write_EnumNode(const struct EnumNode *s, EnumNode_ptr p) { - int err = 0; - err = err || capn_setp(p.p, 0, s->enumerants.p); - return err; -} -void get_EnumNode(struct EnumNode *s, EnumNode_list l, int i) { - EnumNode_ptr p; - p.p = capn_getp(l.p, i); - read_EnumNode(s, p); -} -int set_EnumNode(const struct EnumNode *s, EnumNode_list l, int i) { - EnumNode_ptr p; - p.p = capn_getp(l.p, i); - return write_EnumNode(s, p); -} - -EnumNode_Enumerant_ptr new_EnumNode_Enumerant(struct capn_segment *s) { - EnumNode_Enumerant_ptr p; - p.p = capn_new_struct(s, 8, 2); - return p; -} -EnumNode_Enumerant_list new_EnumNode_Enumerant_list(struct capn_segment *s, int len) { - EnumNode_Enumerant_list p; - p.p = capn_new_list(s, len, 8, 2); - return p; -} -void read_EnumNode_Enumerant(struct EnumNode_Enumerant *s, EnumNode_Enumerant_ptr p) { - s->name = capn_get_text(p.p, 0, capn_val0); - s->codeOrder = capn_read16(p.p, 0); - s->annotations.p = capn_getp(p.p, 1); -} -int write_EnumNode_Enumerant(const struct EnumNode_Enumerant *s, EnumNode_Enumerant_ptr p) { - int err = 0; - err = err || capn_set_text(p.p, 0, s->name); - err = err || capn_write16(p.p, 0, s->codeOrder); - err = err || capn_setp(p.p, 1, s->annotations.p); - return err; -} -void get_EnumNode_Enumerant(struct EnumNode_Enumerant *s, EnumNode_Enumerant_list l, int i) { - EnumNode_Enumerant_ptr p; - p.p = capn_getp(l.p, i); - read_EnumNode_Enumerant(s, p); -} -int set_EnumNode_Enumerant(const struct EnumNode_Enumerant *s, EnumNode_Enumerant_list l, int i) { - EnumNode_Enumerant_ptr p; - p.p = capn_getp(l.p, i); - return write_EnumNode_Enumerant(s, p); -} - -InterfaceNode_ptr new_InterfaceNode(struct capn_segment *s) { - InterfaceNode_ptr p; - p.p = capn_new_struct(s, 0, 1); - return p; -} -InterfaceNode_list new_InterfaceNode_list(struct capn_segment *s, int len) { - InterfaceNode_list p; - p.p = capn_new_list(s, len, 0, 1); - return p; -} -void read_InterfaceNode(struct InterfaceNode *s, InterfaceNode_ptr p) { - s->methods.p = capn_getp(p.p, 0); -} -int write_InterfaceNode(const struct InterfaceNode *s, InterfaceNode_ptr p) { - int err = 0; - err = err || capn_setp(p.p, 0, s->methods.p); - return err; -} -void get_InterfaceNode(struct InterfaceNode *s, InterfaceNode_list l, int i) { - InterfaceNode_ptr p; - p.p = capn_getp(l.p, i); - read_InterfaceNode(s, p); -} -int set_InterfaceNode(const struct InterfaceNode *s, InterfaceNode_list l, int i) { - InterfaceNode_ptr p; - p.p = capn_getp(l.p, i); - return write_InterfaceNode(s, p); -} - -InterfaceNode_Method_ptr new_InterfaceNode_Method(struct capn_segment *s) { - InterfaceNode_Method_ptr p; - p.p = capn_new_struct(s, 8, 4); - return p; -} -InterfaceNode_Method_list new_InterfaceNode_Method_list(struct capn_segment *s, int len) { - InterfaceNode_Method_list p; - p.p = capn_new_list(s, len, 8, 4); - return p; -} -void read_InterfaceNode_Method(struct InterfaceNode_Method *s, InterfaceNode_Method_ptr p) { - s->name = capn_get_text(p.p, 0, capn_val0); - s->codeOrder = capn_read16(p.p, 0); - s->params.p = capn_getp(p.p, 1); - s->requiredParamCount = capn_read16(p.p, 2); - s->returnType.p = capn_getp(p.p, 2); - s->annotations.p = capn_getp(p.p, 3); -} -int write_InterfaceNode_Method(const struct InterfaceNode_Method *s, InterfaceNode_Method_ptr p) { - int err = 0; - err = err || capn_set_text(p.p, 0, s->name); - err = err || capn_write16(p.p, 0, s->codeOrder); - err = err || capn_setp(p.p, 1, s->params.p); - err = err || capn_write16(p.p, 2, s->requiredParamCount); - err = err || capn_setp(p.p, 2, s->returnType.p); - err = err || capn_setp(p.p, 3, s->annotations.p); - return err; -} -void get_InterfaceNode_Method(struct InterfaceNode_Method *s, InterfaceNode_Method_list l, int i) { - InterfaceNode_Method_ptr p; - p.p = capn_getp(l.p, i); - read_InterfaceNode_Method(s, p); -} -int set_InterfaceNode_Method(const struct InterfaceNode_Method *s, InterfaceNode_Method_list l, int i) { - InterfaceNode_Method_ptr p; - p.p = capn_getp(l.p, i); - return write_InterfaceNode_Method(s, p); -} - -InterfaceNode_Method_Param_ptr new_InterfaceNode_Method_Param(struct capn_segment *s) { - InterfaceNode_Method_Param_ptr p; - p.p = capn_new_struct(s, 0, 4); - return p; -} -InterfaceNode_Method_Param_list new_InterfaceNode_Method_Param_list(struct capn_segment *s, int len) { - InterfaceNode_Method_Param_list p; - p.p = capn_new_list(s, len, 0, 4); - return p; -} -void read_InterfaceNode_Method_Param(struct InterfaceNode_Method_Param *s, InterfaceNode_Method_Param_ptr p) { - s->name = capn_get_text(p.p, 0, capn_val0); - s->type.p = capn_getp(p.p, 1); - s->defaultValue.p = capn_getp(p.p, 2); - s->annotations.p = capn_getp(p.p, 3); -} -int write_InterfaceNode_Method_Param(const struct InterfaceNode_Method_Param *s, InterfaceNode_Method_Param_ptr p) { - int err = 0; - err = err || capn_set_text(p.p, 0, s->name); - err = err || capn_setp(p.p, 1, s->type.p); - err = err || capn_setp(p.p, 2, s->defaultValue.p); - err = err || capn_setp(p.p, 3, s->annotations.p); - return err; -} -void get_InterfaceNode_Method_Param(struct InterfaceNode_Method_Param *s, InterfaceNode_Method_Param_list l, int i) { - InterfaceNode_Method_Param_ptr p; - p.p = capn_getp(l.p, i); - read_InterfaceNode_Method_Param(s, p); -} -int set_InterfaceNode_Method_Param(const struct InterfaceNode_Method_Param *s, InterfaceNode_Method_Param_list l, int i) { - InterfaceNode_Method_Param_ptr p; - p.p = capn_getp(l.p, i); - return write_InterfaceNode_Method_Param(s, p); -} - -ConstNode_ptr new_ConstNode(struct capn_segment *s) { - ConstNode_ptr p; - p.p = capn_new_struct(s, 0, 2); - return p; -} -ConstNode_list new_ConstNode_list(struct capn_segment *s, int len) { - ConstNode_list p; - p.p = capn_new_list(s, len, 0, 2); - return p; -} -void read_ConstNode(struct ConstNode *s, ConstNode_ptr p) { - s->type.p = capn_getp(p.p, 0); - s->value.p = capn_getp(p.p, 1); -} -int write_ConstNode(const struct ConstNode *s, ConstNode_ptr p) { - int err = 0; - err = err || capn_setp(p.p, 0, s->type.p); - err = err || capn_setp(p.p, 1, s->value.p); - return err; -} -void get_ConstNode(struct ConstNode *s, ConstNode_list l, int i) { - ConstNode_ptr p; - p.p = capn_getp(l.p, i); - read_ConstNode(s, p); -} -int set_ConstNode(const struct ConstNode *s, ConstNode_list l, int i) { - ConstNode_ptr p; - p.p = capn_getp(l.p, i); - return write_ConstNode(s, p); -} - -AnnotationNode_ptr new_AnnotationNode(struct capn_segment *s) { - AnnotationNode_ptr p; - p.p = capn_new_struct(s, 8, 1); - return p; -} -AnnotationNode_list new_AnnotationNode_list(struct capn_segment *s, int len) { - AnnotationNode_list p; - p.p = capn_new_list(s, len, 8, 1); - return p; -} -void read_AnnotationNode(struct AnnotationNode *s, AnnotationNode_ptr p) { - s->type.p = capn_getp(p.p, 0); - s->targetsFile = (capn_read8(p.p, 0) & 1) != 0; - s->targetsConst = (capn_read8(p.p, 0) & 2) != 0; - s->targetsEnum = (capn_read8(p.p, 0) & 4) != 0; - s->targetsEnumerant = (capn_read8(p.p, 0) & 8) != 0; - s->targetsStruct = (capn_read8(p.p, 0) & 16) != 0; - s->targetsField = (capn_read8(p.p, 0) & 32) != 0; - s->targetsUnion = (capn_read8(p.p, 0) & 64) != 0; - s->targetsInterface = (capn_read8(p.p, 0) & 128) != 0; - s->targetsMethod = (capn_read8(p.p, 1) & 1) != 0; - s->targetsParam = (capn_read8(p.p, 1) & 2) != 0; - s->targetsAnnotation = (capn_read8(p.p, 1) & 4) != 0; -} -int write_AnnotationNode(const struct AnnotationNode *s, AnnotationNode_ptr p) { - int err = 0; - err = err || capn_setp(p.p, 0, s->type.p); - err = err || capn_write1(p.p, 0, s->targetsFile != 0); - err = err || capn_write1(p.p, 1, s->targetsConst != 0); - err = err || capn_write1(p.p, 2, s->targetsEnum != 0); - err = err || capn_write1(p.p, 3, s->targetsEnumerant != 0); - err = err || capn_write1(p.p, 4, s->targetsStruct != 0); - err = err || capn_write1(p.p, 5, s->targetsField != 0); - err = err || capn_write1(p.p, 6, s->targetsUnion != 0); - err = err || capn_write1(p.p, 7, s->targetsInterface != 0); - err = err || capn_write1(p.p, 8, s->targetsMethod != 0); - err = err || capn_write1(p.p, 9, s->targetsParam != 0); - err = err || capn_write1(p.p, 10, s->targetsAnnotation != 0); - return err; -} -void get_AnnotationNode(struct AnnotationNode *s, AnnotationNode_list l, int i) { - AnnotationNode_ptr p; - p.p = capn_getp(l.p, i); - read_AnnotationNode(s, p); -} -int set_AnnotationNode(const struct AnnotationNode *s, AnnotationNode_list l, int i) { - AnnotationNode_ptr p; - p.p = capn_getp(l.p, i); - return write_AnnotationNode(s, p); + write_Annotation(s, p); } CodeGeneratorRequest_ptr new_CodeGeneratorRequest(struct capn_segment *s) { @@ -786,19 +537,77 @@ void read_CodeGeneratorRequest(struct CodeGeneratorRequest *s, CodeGeneratorRequ s->nodes.p = capn_getp(p.p, 0); s->requestedFiles.p = capn_getp(p.p, 1); } -int write_CodeGeneratorRequest(const struct CodeGeneratorRequest *s, CodeGeneratorRequest_ptr p) { - int err = 0; - err = err || capn_setp(p.p, 0, s->nodes.p); - err = err || capn_setp(p.p, 1, s->requestedFiles.p); - return err; +void write_CodeGeneratorRequest(const struct CodeGeneratorRequest *s, CodeGeneratorRequest_ptr p) { + capn_setp(p.p, 0, s->nodes.p); + capn_setp(p.p, 1, s->requestedFiles.p); } void get_CodeGeneratorRequest(struct CodeGeneratorRequest *s, CodeGeneratorRequest_list l, int i) { CodeGeneratorRequest_ptr p; p.p = capn_getp(l.p, i); read_CodeGeneratorRequest(s, p); } -int set_CodeGeneratorRequest(const struct CodeGeneratorRequest *s, CodeGeneratorRequest_list l, int i) { +void set_CodeGeneratorRequest(const struct CodeGeneratorRequest *s, CodeGeneratorRequest_list l, int i) { CodeGeneratorRequest_ptr p; p.p = capn_getp(l.p, i); - return write_CodeGeneratorRequest(s, p); + write_CodeGeneratorRequest(s, p); +} + +CodeGeneratorRequest_RequestedFile_ptr new_CodeGeneratorRequest_RequestedFile(struct capn_segment *s) { + CodeGeneratorRequest_RequestedFile_ptr p; + p.p = capn_new_struct(s, 8, 2); + return p; +} +CodeGeneratorRequest_RequestedFile_list new_CodeGeneratorRequest_RequestedFile_list(struct capn_segment *s, int len) { + CodeGeneratorRequest_RequestedFile_list p; + p.p = capn_new_list(s, len, 8, 2); + return p; +} +void read_CodeGeneratorRequest_RequestedFile(struct CodeGeneratorRequest_RequestedFile *s, CodeGeneratorRequest_RequestedFile_ptr p) { + s->id = capn_read64(p.p, 0); + s->filename = capn_get_text(p.p, 0, capn_val0); + s->imports.p = capn_getp(p.p, 1); +} +void write_CodeGeneratorRequest_RequestedFile(const struct CodeGeneratorRequest_RequestedFile *s, CodeGeneratorRequest_RequestedFile_ptr p) { + capn_write64(p.p, 0, s->id); + capn_set_text(p.p, 0, s->filename); + capn_setp(p.p, 1, s->imports.p); +} +void get_CodeGeneratorRequest_RequestedFile(struct CodeGeneratorRequest_RequestedFile *s, CodeGeneratorRequest_RequestedFile_list l, int i) { + CodeGeneratorRequest_RequestedFile_ptr p; + p.p = capn_getp(l.p, i); + read_CodeGeneratorRequest_RequestedFile(s, p); +} +void set_CodeGeneratorRequest_RequestedFile(const struct CodeGeneratorRequest_RequestedFile *s, CodeGeneratorRequest_RequestedFile_list l, int i) { + CodeGeneratorRequest_RequestedFile_ptr p; + p.p = capn_getp(l.p, i); + write_CodeGeneratorRequest_RequestedFile(s, p); +} + +CodeGeneratorRequest_RequestedFile_Import_ptr new_CodeGeneratorRequest_RequestedFile_Import(struct capn_segment *s) { + CodeGeneratorRequest_RequestedFile_Import_ptr p; + p.p = capn_new_struct(s, 8, 1); + return p; +} +CodeGeneratorRequest_RequestedFile_Import_list new_CodeGeneratorRequest_RequestedFile_Import_list(struct capn_segment *s, int len) { + CodeGeneratorRequest_RequestedFile_Import_list p; + p.p = capn_new_list(s, len, 8, 1); + return p; +} +void read_CodeGeneratorRequest_RequestedFile_Import(struct CodeGeneratorRequest_RequestedFile_Import *s, CodeGeneratorRequest_RequestedFile_Import_ptr p) { + s->id = capn_read64(p.p, 0); + s->name = capn_get_text(p.p, 0, capn_val0); +} +void write_CodeGeneratorRequest_RequestedFile_Import(const struct CodeGeneratorRequest_RequestedFile_Import *s, CodeGeneratorRequest_RequestedFile_Import_ptr p) { + capn_write64(p.p, 0, s->id); + capn_set_text(p.p, 0, s->name); +} +void get_CodeGeneratorRequest_RequestedFile_Import(struct CodeGeneratorRequest_RequestedFile_Import *s, CodeGeneratorRequest_RequestedFile_Import_list l, int i) { + CodeGeneratorRequest_RequestedFile_Import_ptr p; + p.p = capn_getp(l.p, i); + read_CodeGeneratorRequest_RequestedFile_Import(s, p); +} +void set_CodeGeneratorRequest_RequestedFile_Import(const struct CodeGeneratorRequest_RequestedFile_Import *s, CodeGeneratorRequest_RequestedFile_Import_list l, int i) { + CodeGeneratorRequest_RequestedFile_Import_ptr p; + p.p = capn_getp(l.p, i); + write_CodeGeneratorRequest_RequestedFile_Import(s, p); } diff --git a/compiler/schema.capnp.h b/compiler/schema.capnp.h index 2bdb8e8..c280ccc 100644 --- a/compiler/schema.capnp.h +++ b/compiler/schema.capnp.h @@ -9,187 +9,42 @@ extern "C" { struct Node; struct Node_NestedNode; +struct Field; +struct Enumerant; +struct Method; +struct Method_Param; struct Type; struct Value; struct Annotation; -struct FileNode; -struct FileNode_Import; -struct StructNode; -struct StructNode_Member; -struct StructNode_Field; -struct StructNode_Union; -struct EnumNode; -struct EnumNode_Enumerant; -struct InterfaceNode; -struct InterfaceNode_Method; -struct InterfaceNode_Method_Param; -struct ConstNode; -struct AnnotationNode; struct CodeGeneratorRequest; +struct CodeGeneratorRequest_RequestedFile; +struct CodeGeneratorRequest_RequestedFile_Import; typedef struct {capn_ptr p;} Node_ptr; typedef struct {capn_ptr p;} Node_NestedNode_ptr; +typedef struct {capn_ptr p;} Field_ptr; +typedef struct {capn_ptr p;} Enumerant_ptr; +typedef struct {capn_ptr p;} Method_ptr; +typedef struct {capn_ptr p;} Method_Param_ptr; typedef struct {capn_ptr p;} Type_ptr; typedef struct {capn_ptr p;} Value_ptr; typedef struct {capn_ptr p;} Annotation_ptr; -typedef struct {capn_ptr p;} FileNode_ptr; -typedef struct {capn_ptr p;} FileNode_Import_ptr; -typedef struct {capn_ptr p;} StructNode_ptr; -typedef struct {capn_ptr p;} StructNode_Member_ptr; -typedef struct {capn_ptr p;} StructNode_Field_ptr; -typedef struct {capn_ptr p;} StructNode_Union_ptr; -typedef struct {capn_ptr p;} EnumNode_ptr; -typedef struct {capn_ptr p;} EnumNode_Enumerant_ptr; -typedef struct {capn_ptr p;} InterfaceNode_ptr; -typedef struct {capn_ptr p;} InterfaceNode_Method_ptr; -typedef struct {capn_ptr p;} InterfaceNode_Method_Param_ptr; -typedef struct {capn_ptr p;} ConstNode_ptr; -typedef struct {capn_ptr p;} AnnotationNode_ptr; typedef struct {capn_ptr p;} CodeGeneratorRequest_ptr; +typedef struct {capn_ptr p;} CodeGeneratorRequest_RequestedFile_ptr; +typedef struct {capn_ptr p;} CodeGeneratorRequest_RequestedFile_Import_ptr; typedef struct {capn_ptr p;} Node_list; typedef struct {capn_ptr p;} Node_NestedNode_list; +typedef struct {capn_ptr p;} Field_list; +typedef struct {capn_ptr p;} Enumerant_list; +typedef struct {capn_ptr p;} Method_list; +typedef struct {capn_ptr p;} Method_Param_list; typedef struct {capn_ptr p;} Type_list; typedef struct {capn_ptr p;} Value_list; typedef struct {capn_ptr p;} Annotation_list; -typedef struct {capn_ptr p;} FileNode_list; -typedef struct {capn_ptr p;} FileNode_Import_list; -typedef struct {capn_ptr p;} StructNode_list; -typedef struct {capn_ptr p;} StructNode_Member_list; -typedef struct {capn_ptr p;} StructNode_Field_list; -typedef struct {capn_ptr p;} StructNode_Union_list; -typedef struct {capn_ptr p;} EnumNode_list; -typedef struct {capn_ptr p;} EnumNode_Enumerant_list; -typedef struct {capn_ptr p;} InterfaceNode_list; -typedef struct {capn_ptr p;} InterfaceNode_Method_list; -typedef struct {capn_ptr p;} InterfaceNode_Method_Param_list; -typedef struct {capn_ptr p;} ConstNode_list; -typedef struct {capn_ptr p;} AnnotationNode_list; typedef struct {capn_ptr p;} CodeGeneratorRequest_list; - - - -enum Node_body { - Node_fileNode = 0, - Node_structNode = 1, - Node_enumNode = 2, - Node_interfaceNode = 3, - Node_constNode = 4, - Node_annotationNode = 5 -}; - -struct Node { - uint64_t id; - capn_text displayName; - uint64_t scopeId; - Node_NestedNode_list nestedNodes; - Annotation_list annotations; - enum Node_body body_tag; - union { - FileNode_ptr fileNode; - StructNode_ptr structNode; - EnumNode_ptr enumNode; - InterfaceNode_ptr interfaceNode; - ConstNode_ptr constNode; - AnnotationNode_ptr annotationNode; - } body; -}; - -struct Node_NestedNode { - capn_text name; - uint64_t id; -}; - -enum Type_body { - Type_voidType = 0, - Type_boolType = 1, - Type_int8Type = 2, - Type_int16Type = 3, - Type_int32Type = 4, - Type_int64Type = 5, - Type_uint8Type = 6, - Type_uint16Type = 7, - Type_uint32Type = 8, - Type_uint64Type = 9, - Type_float32Type = 10, - Type_float64Type = 11, - Type_textType = 12, - Type_dataType = 13, - Type_listType = 14, - Type_enumType = 15, - Type_structType = 16, - Type_interfaceType = 17, - Type_objectType = 18 -}; - -struct Type { - enum Type_body body_tag; - union { - Type_ptr listType; - uint64_t enumType; - uint64_t structType; - uint64_t interfaceType; - } body; -}; - -enum Value_body { - Value_voidValue = 9, - Value_boolValue = 1, - Value_int8Value = 2, - Value_int16Value = 3, - Value_int32Value = 4, - Value_int64Value = 5, - Value_uint8Value = 6, - Value_uint16Value = 7, - Value_uint32Value = 8, - Value_uint64Value = 0, - Value_float32Value = 10, - Value_float64Value = 11, - Value_textValue = 12, - Value_dataValue = 13, - Value_listValue = 14, - Value_enumValue = 15, - Value_structValue = 16, - Value_interfaceValue = 17, - Value_objectValue = 18 -}; - -struct Value { - enum Value_body body_tag; - union { - unsigned boolValue:1; - int8_t int8Value; - int16_t int16Value; - int32_t int32Value; - int64_t int64Value; - uint8_t uint8Value; - uint16_t uint16Value; - uint32_t uint32Value; - uint64_t uint64Value; - float float32Value; - double float64Value; - capn_text textValue; - capn_data dataValue; - capn_ptr listValue; - uint16_t enumValue; - capn_ptr structValue; - capn_ptr objectValue; - } body; -}; - -struct Annotation { - uint64_t id; - Value_ptr value; -}; - -struct FileNode { - FileNode_Import_list imports; -}; - -struct FileNode_Import { - uint64_t id; - capn_text name; -}; +typedef struct {capn_ptr p;} CodeGeneratorRequest_RequestedFile_list; +typedef struct {capn_ptr p;} CodeGeneratorRequest_RequestedFile_Import_list; enum ElementSize { ElementSize_empty = 0, @@ -202,215 +57,313 @@ enum ElementSize { ElementSize_inlineComposite = 7 }; -struct StructNode { - uint16_t dataSectionWordSize; - uint16_t pointerSectionSize; - enum ElementSize preferredListEncoding; - StructNode_Member_list members; + +enum Node_which { + Node_file = 0, + Node_struct = 1, + Node_enum = 2, + Node_interface = 3, + Node_const = 4, + Node_annotation = 5 }; -enum StructNode_Member_body { - StructNode_Member_fieldMember = 0, - StructNode_Member_unionMember = 1 -}; - -struct StructNode_Member { - capn_text name; - uint16_t ordinal; - uint16_t codeOrder; +struct Node { + uint64_t id; + capn_text displayName; + uint32_t displayNamePrefixLength; + uint64_t scopeId; + Node_NestedNode_list nestedNodes; Annotation_list annotations; - enum StructNode_Member_body body_tag; + enum Node_which which; union { - StructNode_Field_ptr fieldMember; - StructNode_Union_ptr unionMember; - } body; + struct { + uint16_t dataWordCount; + uint16_t pointerCount; + enum ElementSize preferredListEncoding; + unsigned isGroup : 1; + uint16_t discriminantCount; + uint16_t discriminantOffset; + Field_list fields; + } _struct; + + struct { + Enumerant_list enumerants; + } _enum; + + struct { + Method_list methods; + } _interface; + + struct { + Type_ptr type; + Value_ptr value; + } _const; + + struct { + Type_ptr type; + unsigned targetsFile : 1; + unsigned targetsConst : 1; + unsigned targetsEnum : 1; + unsigned targetsEnumerant : 1; + unsigned targetsStruct : 1; + unsigned targetsField : 1; + unsigned targetsUnion : 1; + unsigned targetsGroup : 1; + unsigned targetsInterface : 1; + unsigned targetsMethod : 1; + unsigned targetsParam : 1; + unsigned targetsAnnotation : 1; + } annotation; + }; }; -struct StructNode_Field { - uint32_t offset; - Type_ptr type; - Value_ptr defaultValue; +struct Node_NestedNode { + capn_text name; + uint64_t id; }; -struct StructNode_Union { - uint32_t discriminantOffset; - StructNode_Member_list members; +enum Field_which { + Field_slot = 0, + Field_group = 1, }; -struct EnumNode { - EnumNode_Enumerant_list enumerants; +enum Field_ordinal { + Field_implicit = 8, + Field_explicit = 9, }; -struct EnumNode_Enumerant { +struct Field { + capn_text name; + uint16_t codeOrder; + Annotation_list annotations; + uint16_t discriminantValue; + enum Field_which which; + union { + struct { + uint32_t offset; + Type_ptr type; + Value_ptr defaultValue; + } slot; + + struct { + uint64_t typeId; + } group; + }; + + enum Field_ordinal ordinal_which; + union { + uint16_t _explicit; + } ordinal; +}; + +struct Enumerant { capn_text name; uint16_t codeOrder; Annotation_list annotations; }; -struct InterfaceNode { - InterfaceNode_Method_list methods; -}; - -struct InterfaceNode_Method { +struct Method { capn_text name; uint16_t codeOrder; - InterfaceNode_Method_Param_list params; + Method_Param_list params; uint16_t requiredParamCount; Type_ptr returnType; Annotation_list annotations; }; -struct InterfaceNode_Method_Param { +struct Method_Param { capn_text name; Type_ptr type; Value_ptr defaultValue; Annotation_list annotations; }; -struct ConstNode { - Type_ptr type; - Value_ptr value; +enum Type_which { + Type_void = 0, + Type_bool = 1, + Type_int8 = 2, + Type_int16 = 3, + Type_int32 = 4, + Type_int64 = 5, + Type_uint8 = 6, + Type_uint16 = 7, + Type_uint32 = 8, + Type_uint64 = 9, + Type_float32 = 10, + Type_float64 = 11, + Type_text = 12, + Type_data = 13, + Type__list = 14, + Type_enum = 15, + Type_struct = 16, + Type_interface = 17, + Type_object = 18 }; -struct AnnotationNode { - Type_ptr type; - unsigned targetsFile:1; - unsigned targetsConst:1; - unsigned targetsEnum:1; - unsigned targetsEnumerant:1; - unsigned targetsStruct:1; - unsigned targetsField:1; - unsigned targetsUnion:1; - unsigned targetsInterface:1; - unsigned targetsMethod:1; - unsigned targetsParam:1; - unsigned targetsAnnotation:1; +struct Type { + enum Type_which which; + union { + struct { + Type_ptr elementType; + } list; + + struct { + uint64_t typeId; + } _enum; + + struct { + uint64_t typeId; + } _struct; + + struct { + uint64_t typeId; + } _interface; + }; +}; + +enum Value_which { + Value_void = 0, + Value_bool = 1, + Value_int8 = 2, + Value_int16 = 3, + Value_int32 = 4, + Value_int64 = 5, + Value_uint8 = 6, + Value_uint16 = 7, + Value_uint32 = 8, + Value_uint64 = 9, + Value_float32 = 10, + Value_float64 = 11, + Value_text = 12, + Value_data = 13, + Value__list = 14, + Value_enum = 15, + Value_struct = 16, + Value_interface = 17, + Value_object = 18 +}; + +struct Value { + enum Value_which which; + union { + unsigned _bool : 1; + int8_t int8; + int16_t int16; + int32_t int32; + int64_t int64; + uint8_t uint8; + uint16_t uint16; + uint32_t uint32; + uint64_t uint64; + float float32; + double float64; + capn_text text; + capn_data data; + capn_ptr list; + uint16_t _enum; + capn_ptr _struct; + capn_ptr object; + }; +}; + +struct Annotation { + uint64_t id; + Value_ptr value; }; struct CodeGeneratorRequest { Node_list nodes; - capn_list64 requestedFiles; + CodeGeneratorRequest_RequestedFile_list requestedFiles; +}; + +struct CodeGeneratorRequest_RequestedFile { + uint64_t id; + capn_text filename; + CodeGeneratorRequest_RequestedFile_Import_list imports; +}; + +struct CodeGeneratorRequest_RequestedFile_Import { + uint64_t id; + capn_text name; }; Node_ptr new_Node(struct capn_segment*); Node_NestedNode_ptr new_Node_NestedNode(struct capn_segment*); +Field_ptr new_Field(struct capn_segment*); +Enumerant_ptr new_Enumerant(struct capn_segment*); +Method_ptr new_Method(struct capn_segment*); +Method_Param_ptr new_Method_Param(struct capn_segment*); Type_ptr new_Type(struct capn_segment*); Value_ptr new_Value(struct capn_segment*); Annotation_ptr new_Annotation(struct capn_segment*); -FileNode_ptr new_FileNode(struct capn_segment*); -FileNode_Import_ptr new_FileNode_Import(struct capn_segment*); -StructNode_ptr new_StructNode(struct capn_segment*); -StructNode_Member_ptr new_StructNode_Member(struct capn_segment*); -StructNode_Field_ptr new_StructNode_Field(struct capn_segment*); -StructNode_Union_ptr new_StructNode_Union(struct capn_segment*); -EnumNode_ptr new_EnumNode(struct capn_segment*); -EnumNode_Enumerant_ptr new_EnumNode_Enumerant(struct capn_segment*); -InterfaceNode_ptr new_InterfaceNode(struct capn_segment*); -InterfaceNode_Method_ptr new_InterfaceNode_Method(struct capn_segment*); -InterfaceNode_Method_Param_ptr new_InterfaceNode_Method_Param(struct capn_segment*); -ConstNode_ptr new_ConstNode(struct capn_segment*); -AnnotationNode_ptr new_AnnotationNode(struct capn_segment*); CodeGeneratorRequest_ptr new_CodeGeneratorRequest(struct capn_segment*); +CodeGeneratorRequest_RequestedFile_ptr new_CodeGeneratorRequest_RequestedFile(struct capn_segment*); +CodeGeneratorRequest_RequestedFile_Import_ptr new_CodeGeneratorRequest_RequestedFile_Import(struct capn_segment*); Node_list new_Node_list(struct capn_segment*, int len); Node_NestedNode_list new_Node_NestedNode_list(struct capn_segment*, int len); +Field_list new_Field_list(struct capn_segment*, int len); +Enumerant_list new_Enumerant_list(struct capn_segment*, int len); +Method_list new_Method_list(struct capn_segment*, int len); +Method_Param_list new_Method_Param_list(struct capn_segment*, int len); Type_list new_Type_list(struct capn_segment*, int len); Value_list new_Value_list(struct capn_segment*, int len); Annotation_list new_Annotation_list(struct capn_segment*, int len); -FileNode_list new_FileNode_list(struct capn_segment*, int len); -FileNode_Import_list new_FileNode_Import_list(struct capn_segment*, int len); -StructNode_list new_StructNode_list(struct capn_segment*, int len); -StructNode_Member_list new_StructNode_Member_list(struct capn_segment*, int len); -StructNode_Field_list new_StructNode_Field_list(struct capn_segment*, int len); -StructNode_Union_list new_StructNode_Union_list(struct capn_segment*, int len); -EnumNode_list new_EnumNode_list(struct capn_segment*, int len); -EnumNode_Enumerant_list new_EnumNode_Enumerant_list(struct capn_segment*, int len); -InterfaceNode_list new_InterfaceNode_list(struct capn_segment*, int len); -InterfaceNode_Method_list new_InterfaceNode_Method_list(struct capn_segment*, int len); -InterfaceNode_Method_Param_list new_InterfaceNode_Method_Param_list(struct capn_segment*, int len); -ConstNode_list new_ConstNode_list(struct capn_segment*, int len); -AnnotationNode_list new_AnnotationNode_list(struct capn_segment*, int len); CodeGeneratorRequest_list new_CodeGeneratorRequest_list(struct capn_segment*, int len); +CodeGeneratorRequest_RequestedFile_list new_CodeGeneratorRequest_RequestedFile_list(struct capn_segment*, int len); +CodeGeneratorRequest_RequestedFile_Import_list new_CodeGeneratorRequest_RequestedFile_Import_list(struct capn_segment*, int len); void read_Node(struct Node*, Node_ptr); void read_Node_NestedNode(struct Node_NestedNode*, Node_NestedNode_ptr); +void read_Field(struct Field*, Field_ptr); +void read_Enumerant(struct Enumerant*, Enumerant_ptr); +void read_Method(struct Method*, Method_ptr); +void read_Method_Param(struct Method_Param*, Method_Param_ptr); void read_Type(struct Type*, Type_ptr); void read_Value(struct Value*, Value_ptr); void read_Annotation(struct Annotation*, Annotation_ptr); -void read_FileNode(struct FileNode*, FileNode_ptr); -void read_FileNode_Import(struct FileNode_Import*, FileNode_Import_ptr); -void read_StructNode(struct StructNode*, StructNode_ptr); -void read_StructNode_Member(struct StructNode_Member*, StructNode_Member_ptr); -void read_StructNode_Field(struct StructNode_Field*, StructNode_Field_ptr); -void read_StructNode_Union(struct StructNode_Union*, StructNode_Union_ptr); -void read_EnumNode(struct EnumNode*, EnumNode_ptr); -void read_EnumNode_Enumerant(struct EnumNode_Enumerant*, EnumNode_Enumerant_ptr); -void read_InterfaceNode(struct InterfaceNode*, InterfaceNode_ptr); -void read_InterfaceNode_Method(struct InterfaceNode_Method*, InterfaceNode_Method_ptr); -void read_InterfaceNode_Method_Param(struct InterfaceNode_Method_Param*, InterfaceNode_Method_Param_ptr); -void read_ConstNode(struct ConstNode*, ConstNode_ptr); -void read_AnnotationNode(struct AnnotationNode*, AnnotationNode_ptr); void read_CodeGeneratorRequest(struct CodeGeneratorRequest*, CodeGeneratorRequest_ptr); +void read_CodeGeneratorRequest_RequestedFile(struct CodeGeneratorRequest_RequestedFile*, CodeGeneratorRequest_RequestedFile_ptr); +void read_CodeGeneratorRequest_RequestedFile_Import(struct CodeGeneratorRequest_RequestedFile_Import*, CodeGeneratorRequest_RequestedFile_Import_ptr); -int write_Node(const struct Node*, Node_ptr); -int write_Node_NestedNode(const struct Node_NestedNode*, Node_NestedNode_ptr); -int write_Type(const struct Type*, Type_ptr); -int write_Value(const struct Value*, Value_ptr); -int write_Annotation(const struct Annotation*, Annotation_ptr); -int write_FileNode(const struct FileNode*, FileNode_ptr); -int write_FileNode_Import(const struct FileNode_Import*, FileNode_Import_ptr); -int write_StructNode(const struct StructNode*, StructNode_ptr); -int write_StructNode_Member(const struct StructNode_Member*, StructNode_Member_ptr); -int write_StructNode_Field(const struct StructNode_Field*, StructNode_Field_ptr); -int write_StructNode_Union(const struct StructNode_Union*, StructNode_Union_ptr); -int write_EnumNode(const struct EnumNode*, EnumNode_ptr); -int write_EnumNode_Enumerant(const struct EnumNode_Enumerant*, EnumNode_Enumerant_ptr); -int write_InterfaceNode(const struct InterfaceNode*, InterfaceNode_ptr); -int write_InterfaceNode_Method(const struct InterfaceNode_Method*, InterfaceNode_Method_ptr); -int write_InterfaceNode_Method_Param(const struct InterfaceNode_Method_Param*, InterfaceNode_Method_Param_ptr); -int write_ConstNode(const struct ConstNode*, ConstNode_ptr); -int write_AnnotationNode(const struct AnnotationNode*, AnnotationNode_ptr); -int write_CodeGeneratorRequest(const struct CodeGeneratorRequest*, CodeGeneratorRequest_ptr); +void write_Node(const struct Node*, Node_ptr); +void write_Node_NestedNode(const struct Node_NestedNode*, Node_NestedNode_ptr); +void write_Field(const struct Field*, Field_ptr); +void write_Enumerant(const struct Enumerant*, Enumerant_ptr); +void write_Method(const struct Method*, Method_ptr); +void write_Method_Param(const struct Method_Param*, Method_Param_ptr); +void write_Type(const struct Type*, Type_ptr); +void write_Value(const struct Value*, Value_ptr); +void write_Annotation(const struct Annotation*, Annotation_ptr); +void write_CodeGeneratorRequest(const struct CodeGeneratorRequest*, CodeGeneratorRequest_ptr); +void write_CodeGeneratorRequest_RequestedFile(const struct CodeGeneratorRequest_RequestedFile*, CodeGeneratorRequest_RequestedFile_ptr); +void write_CodeGeneratorRequest_RequestedFile_Import(const struct CodeGeneratorRequest_RequestedFile_Import*, CodeGeneratorRequest_RequestedFile_Import_ptr); void get_Node(struct Node*, Node_list, int i); void get_Node_NestedNode(struct Node_NestedNode*, Node_NestedNode_list, int i); +void get_Field(struct Field*, Field_list, int i); +void get_Enumerant(struct Enumerant*, Enumerant_list, int i); +void get_Method(struct Method*, Method_list, int i); +void get_Method_Param(struct Method_Param*, Method_Param_list, int i); void get_Type(struct Type*, Type_list, int i); void get_Value(struct Value*, Value_list, int i); void get_Annotation(struct Annotation*, Annotation_list, int i); -void get_FileNode(struct FileNode*, FileNode_list, int i); -void get_FileNode_Import(struct FileNode_Import*, FileNode_Import_list, int i); -void get_StructNode(struct StructNode*, StructNode_list, int i); -void get_StructNode_Member(struct StructNode_Member*, StructNode_Member_list, int i); -void get_StructNode_Field(struct StructNode_Field*, StructNode_Field_list, int i); -void get_StructNode_Union(struct StructNode_Union*, StructNode_Union_list, int i); -void get_EnumNode(struct EnumNode*, EnumNode_list, int i); -void get_EnumNode_Enumerant(struct EnumNode_Enumerant*, EnumNode_Enumerant_list, int i); -void get_InterfaceNode(struct InterfaceNode*, InterfaceNode_list, int i); -void get_InterfaceNode_Method(struct InterfaceNode_Method*, InterfaceNode_Method_list, int i); -void get_InterfaceNode_Method_Param(struct InterfaceNode_Method_Param*, InterfaceNode_Method_Param_list, int i); -void get_ConstNode(struct ConstNode*, ConstNode_list, int i); -void get_AnnotationNode(struct AnnotationNode*, AnnotationNode_list, int i); void get_CodeGeneratorRequest(struct CodeGeneratorRequest*, CodeGeneratorRequest_list, int i); +void get_CodeGeneratorRequest_RequestedFile(struct CodeGeneratorRequest_RequestedFile*, CodeGeneratorRequest_RequestedFile_list, int i); +void get_CodeGeneratorRequest_RequestedFile_Import(struct CodeGeneratorRequest_RequestedFile_Import*, CodeGeneratorRequest_RequestedFile_Import_list, int i); -int set_Node(const struct Node*, Node_list, int i); -int set_Node_NestedNode(const struct Node_NestedNode*, Node_NestedNode_list, int i); -int set_Type(const struct Type*, Type_list, int i); -int set_Value(const struct Value*, Value_list, int i); -int set_Annotation(const struct Annotation*, Annotation_list, int i); -int set_FileNode(const struct FileNode*, FileNode_list, int i); -int set_FileNode_Import(const struct FileNode_Import*, FileNode_Import_list, int i); -int set_StructNode(const struct StructNode*, StructNode_list, int i); -int set_StructNode_Member(const struct StructNode_Member*, StructNode_Member_list, int i); -int set_StructNode_Field(const struct StructNode_Field*, StructNode_Field_list, int i); -int set_StructNode_Union(const struct StructNode_Union*, StructNode_Union_list, int i); -int set_EnumNode(const struct EnumNode*, EnumNode_list, int i); -int set_EnumNode_Enumerant(const struct EnumNode_Enumerant*, EnumNode_Enumerant_list, int i); -int set_InterfaceNode(const struct InterfaceNode*, InterfaceNode_list, int i); -int set_InterfaceNode_Method(const struct InterfaceNode_Method*, InterfaceNode_Method_list, int i); -int set_InterfaceNode_Method_Param(const struct InterfaceNode_Method_Param*, InterfaceNode_Method_Param_list, int i); -int set_ConstNode(const struct ConstNode*, ConstNode_list, int i); -int set_AnnotationNode(const struct AnnotationNode*, AnnotationNode_list, int i); -int set_CodeGeneratorRequest(const struct CodeGeneratorRequest*, CodeGeneratorRequest_list, int i); +void set_Node(const struct Node*, Node_list, int i); +void set_Node_NestedNode(const struct Node_NestedNode*, Node_NestedNode_list, int i); +void set_Field(const struct Field*, Field_list, int i); +void set_Enumerant(const struct Enumerant*, Enumerant_list, int i); +void set_Method(const struct Method*, Method_list, int i); +void set_Method_Param(const struct Method_Param*, Method_Param_list, int i); +void set_Type(const struct Type*, Type_list, int i); +void set_Value(const struct Value*, Value_list, int i); +void set_Annotation(const struct Annotation*, Annotation_list, int i); +void set_CodeGeneratorRequest(const struct CodeGeneratorRequest*, CodeGeneratorRequest_list, int i); +void set_CodeGeneratorRequest_RequestedFile(const struct CodeGeneratorRequest_RequestedFile*, CodeGeneratorRequest_RequestedFile_list, int i); +void set_CodeGeneratorRequest_RequestedFile_Import(const struct CodeGeneratorRequest_RequestedFile_Import*, CodeGeneratorRequest_RequestedFile_Import_list, int i); #ifdef __cplusplus } diff --git a/compiler/str.h b/compiler/str.h index 5021269..8502775 100644 --- a/compiler/str.h +++ b/compiler/str.h @@ -12,6 +12,13 @@ extern char str_static[]; void str_reserve(struct str *v, int sz); +static void str_init(struct str *v, int sz) { + v->str = str_static; + v->len = v->cap = 0; + if (sz) + str_reserve(v, sz); +} + static void str_release(struct str *v) { if (v->cap) { free(v->str);