diff options
author | Joshua Haberman <joshua@reverberate.org> | 2009-12-28 16:41:33 -0800 |
---|---|---|
committer | Joshua Haberman <joshua@reverberate.org> | 2009-12-28 16:41:33 -0800 |
commit | e5debfa1c99757ad08bccd834b9596a4f5e97adb (patch) | |
tree | 9e47c92b10875f83571475e88efdf9ae4af25e34 /tools/upbc.c | |
parent | 602e093504dc07fec421df636d307654685892cb (diff) |
More incremental work; ported some of upbc.
Diffstat (limited to 'tools/upbc.c')
-rw-r--r-- | tools/upbc.c | 134 |
1 files changed, 71 insertions, 63 deletions
diff --git a/tools/upbc.c b/tools/upbc.c index a6ab289..8e89435 100644 --- a/tools/upbc.c +++ b/tools/upbc.c @@ -11,33 +11,36 @@ #include <ctype.h> #include <inttypes.h> #include <stdarg.h> +#include <stdlib.h> #include "descriptor.h" -#include "upb_array.h" +#include "upb_data.h" #include "upb_def.h" -#include "upb_mm.h" -#include "upb_msg.h" #include "upb_text.h" /* These are in-place string transformations that do not change the length of * the string (and thus never need to re-allocate). */ -/* Convert to C identifier: foo.bar.Baz -> foo_bar_Baz. */ -static void to_cident(struct upb_string *str) +// Convert to C identifier: foo.bar.Baz -> foo_bar_Baz. +static void to_cident(upb_string *str) { - for(uint32_t i = 0; i < str->byte_len; i++) - if(str->ptr[i] == '.' || str->ptr[i] == '/') - str->ptr[i] = '_'; + upb_strlen_t len = upb_strlen(str); + char *buf = upb_string_getrwbuf(str, len); + for(uint32_t i = 0; i < len; i++) + if(buf[i] == '.' || buf[i] == '/') + buf[i] = '_'; } -/* Convert to C proprocessor identifier: foo.bar.Baz -> FOO_BAR_BAZ. */ -static void to_preproc(struct upb_string *str) +// Convert to C proprocessor identifier: foo.bar.Baz -> FOO_BAR_BAZ. +static void to_preproc(upb_string *str) { to_cident(str); - for(uint32_t i = 0; i < str->byte_len; i++) - str->ptr[i] = toupper(str->ptr[i]); + upb_strlen_t len = upb_strlen(str); + char *buf = upb_string_getrwbuf(str, len); + for(uint32_t i = 0; i < len; i++) + buf[i] = toupper(buf[i]); } -static int my_memrchr(char *data, char c, size_t len) +static int my_memrchr(const char *data, char c, size_t len) { int off = len-1; while(off > 0 && data[off] != c) --off; @@ -62,10 +65,13 @@ static void write_const_h(struct upb_def *defs[], int num_entries, char *outfile_name, FILE *stream) { /* Header file prologue. */ - struct upb_string *include_guard_name = upb_strdupc(outfile_name); + upb_string *include_guard_name = upb_strdupc(outfile_name); to_preproc(include_guard_name); /* A bit cheesy, but will do the job. */ - include_guard_name->ptr[include_guard_name->byte_len-1] = 'C'; + upb_strlen_t len = upb_strlen(include_guard_name); + char *buf = upb_string_getrwbuf(include_guard_name, len); + buf[len-1] = 'C'; + fputs("/* This file was generated by upbc (the upb compiler). " "Do not edit. */\n\n", stream), fprintf(stream, "#ifndef " UPB_STRFMT "\n", UPB_STRARG(include_guard_name)); @@ -79,14 +85,14 @@ static void write_const_h(struct upb_def *defs[], int num_entries, for(int i = 0; i < num_entries; i++) { /* Foreach enum */ if(defs[i]->type != UPB_DEF_ENUM) continue; struct upb_enumdef *enumdef = upb_downcast_enumdef(defs[i]); - struct upb_string *enum_name = upb_strdup(UPB_UPCAST(enumdef)->fqname); - struct upb_string *enum_val_prefix = upb_strdup(enum_name); + upb_string *enum_name = upb_strdup(UPB_UPCAST(enumdef)->fqname); + upb_string *enum_val_prefix = upb_strdup(enum_name); to_cident(enum_name); - enum_val_prefix->byte_len = my_memrchr(enum_val_prefix->ptr, - UPB_SYMBOL_SEPARATOR, - enum_val_prefix->byte_len); - enum_val_prefix->byte_len++; + const char *data = upb_string_getrobuf(enum_val_prefix); + upb_strlen_t len = upb_strlen(enum_val_prefix); + upb_strlen_t lastsep = my_memrchr(data, UPB_SYMBOL_SEPARATOR, len); + upb_string_resize(enum_val_prefix, lastsep + 1); to_preproc(enum_val_prefix); fprintf(stream, "typedef enum " UPB_STRFMT " {\n", UPB_STRARG(enum_name)); @@ -94,7 +100,7 @@ static void write_const_h(struct upb_def *defs[], int num_entries, bool first = true; /* Foreach enum value. */ for(upb_enum_begin(&iter, enumdef); !upb_enum_done(&iter); upb_enum_next(&iter)) { - struct upb_string *value_name = upb_strdup(iter.name); + upb_string *value_name = upb_strdup(iter.name); to_preproc(value_name); /* " GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13," */ if (!first) fputs(",\n", stream); @@ -124,7 +130,7 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, char *descriptor_cident, FILE *stream) { /* Header file prologue. */ - struct upb_string *include_guard_name = upb_strdupc(outfile_name); + upb_string *include_guard_name = upb_strdupc(outfile_name); to_preproc(include_guard_name); fputs("/* This file was generated by upbc (the upb compiler). " "Do not edit. */\n\n", stream), @@ -149,7 +155,7 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, for(int i = 0; i < num_defs; i++) { /* Foreach message */ struct upb_msgdef *m = upb_dyncast_msgdef(defs[i]); if(!m) continue; - struct upb_string *msg_name = upb_strdup(UPB_UPCAST(m)->fqname); + upb_string *msg_name = upb_strdup(UPB_UPCAST(m)->fqname); to_cident(msg_name); fprintf(stream, "struct " UPB_STRFMT ";\n", UPB_STRARG(msg_name)); fprintf(stream, "typedef struct " UPB_STRFMT "\n " UPB_STRFMT ";\n\n", @@ -162,7 +168,7 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, for(int i = 0; i < num_defs; i++) { /* Foreach message */ struct upb_msgdef *m = upb_dyncast_msgdef(defs[i]); if(!m) continue; - struct upb_string *msg_name = upb_strdup(UPB_UPCAST(m)->fqname); + upb_string *msg_name = upb_strdup(UPB_UPCAST(m)->fqname); to_cident(msg_name); fprintf(stream, "struct " UPB_STRFMT " {\n", UPB_STRARG(msg_name)); fputs(" struct upb_mmhead mmhead;\n", stream); @@ -181,7 +187,7 @@ static void write_h(struct upb_def *defs[], int num_defs, char *outfile_name, for(upb_field_count_t j = 0; j < m->num_fields; j++) { struct upb_fielddef *f = &m->fields[j]; if(upb_issubmsg(f)) { - struct upb_string *type_name = upb_strdup(f->def->fqname); + upb_string *type_name = upb_strdup(f->def->fqname); to_cident(type_name); if(f->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED) { fprintf(stream, " UPB_MSG_ARRAY(" UPB_STRFMT ")* " UPB_STRFMT ";\n", @@ -241,14 +247,14 @@ struct strtable_entry { struct typetable_entry { struct upb_strtable_entry e; struct upb_fielddef *field; - struct upb_string *cident; /* Type name converted with to_cident(). */ + upb_string *cident; /* Type name converted with to_cident(). */ /* A list of all values of this type, in an established order. */ union upb_value *values; int values_size, values_len; struct array { int offset; int len; - struct upb_array *ptr; /* So we can find it later. */ + upb_array *ptr; /* So we can find it later. */ } *arrays; int arrays_size, arrays_len; }; @@ -270,32 +276,33 @@ int compare_entries(const void *_e1, const void *_e2) * * TODO: make these use a generic msg visitor. */ -static void add_strings_from_msg(struct upb_msg *msg, struct upb_strtable *t); +static void add_strings_from_msg(upb_msg *msg, struct upb_msgdef *md, + struct upb_strtable *t); -static void add_strings_from_value(union upb_value_ptr p, +static void add_strings_from_value(union upb_value p, struct upb_fielddef *f, struct upb_strtable *t) { if(upb_isstringtype(f->type)) { - struct strtable_entry e = {.e = {.key = *p.str}}; + struct strtable_entry e = {.e = {.key = p.str}}; if(upb_strtable_lookup(t, e.e.key) == NULL) upb_strtable_insert(t, &e.e); } else if(upb_issubmsg(f)) { - add_strings_from_msg(*p.msg, t); + add_strings_from_msg(p.msg, upb_downcast_msgdef(f->def), t); } } -static void add_strings_from_msg(struct upb_msg *msg, struct upb_strtable *t) +static void add_strings_from_msg(upb_msg *msg, struct upb_msgdef *md, + struct upb_strtable *t) { - struct upb_msgdef *m = msg->def; - for(upb_field_count_t i = 0; i < m->num_fields; i++) { - struct upb_fielddef *f = &m->fields[i]; - if(!upb_msg_isset(msg, f)) continue; - union upb_value_ptr p = upb_msg_getptr(msg, f); + for(upb_field_count_t i = 0; i < md->num_fields; i++) { + struct upb_fielddef *f = &md->fields[i]; + if(!upb_msg_has(msg, f)) continue; + union upb_value p = upb_msg_get(msg, f); if(upb_isarray(f)) { - struct upb_array *arr = *p.arr; - for(uint32_t j = 0; j < arr->len; j++) - add_strings_from_value(upb_array_getelementptr(arr, j), f, t); + upb_array *arr = p.arr; + for(uint32_t j = 0; j < upb_array_len(arr); j++) + add_strings_from_value(upb_array_get(arr, f, j), f, t); } else { add_strings_from_value(p, f, t); } @@ -310,8 +317,8 @@ static void add_strings_from_msg(struct upb_msg *msg, struct upb_strtable *t) struct typetable_entry *get_or_insert_typeentry(struct upb_strtable *t, struct upb_fielddef *f) { - struct upb_string *type_name = upb_issubmsg(f) ? upb_strdup(f->def->fqname) : - upb_strdupc(upb_type_info[f->type].ctype); + upb_string *type_name = upb_issubmsg(f) ? upb_strdup(f->def->fqname) : + upb_strdupc(upb_type_info[f->type].ctype); struct typetable_entry *type_e = upb_strtable_lookup(t, type_name); if(type_e == NULL) { struct typetable_entry new_type_e = { @@ -331,7 +338,7 @@ struct typetable_entry *get_or_insert_typeentry(struct upb_strtable *t, return type_e; } -static void add_value(union upb_value_ptr p, struct upb_fielddef *f, +static void add_value(union upb_value v, struct upb_fielddef *f, struct upb_strtable *t) { struct typetable_entry *type_e = get_or_insert_typeentry(t, f); @@ -339,19 +346,19 @@ static void add_value(union upb_value_ptr p, struct upb_fielddef *f, type_e->values_size = UPB_MAX(type_e->values_size * 2, 4); type_e->values = realloc(type_e->values, sizeof(*type_e->values) * type_e->values_size); } - type_e->values[type_e->values_len++] = upb_value_read(p, f->type); + type_e->values[type_e->values_len++] = v; } -static void add_submsgs(struct upb_msg *msg, struct upb_strtable *t) +static void add_submsgs(upb_msg *msg, struct upb_msgdef *md, + struct upb_strtable *t) { - struct upb_msgdef *m = msg->def; - for(upb_field_count_t i = 0; i < m->num_fields; i++) { - struct upb_fielddef *f = &m->fields[i]; - if(!upb_msg_isset(msg, f)) continue; - union upb_value_ptr p = upb_msg_getptr(msg, f); + for(upb_field_count_t i = 0; i < md->num_fields; i++) { + struct upb_fielddef *f = &md->fields[i]; + if(!upb_msg_has(msg, f)) continue; + union upb_value v = upb_msg_get(msg, f); if(upb_isarray(f)) { if(upb_isstring(f)) continue; /* Handled by a different code-path. */ - struct upb_array *arr = *p.arr; + upb_array *arr = v.arr; /* Add to our list of arrays for this type. */ struct typetable_entry *arr_type_e = @@ -362,30 +369,31 @@ static void add_submsgs(struct upb_msg *msg, struct upb_strtable *t) sizeof(*arr_type_e->arrays)*arr_type_e->arrays_size); } arr_type_e->arrays[arr_type_e->arrays_len].offset = arr_type_e->values_len; - arr_type_e->arrays[arr_type_e->arrays_len].len = arr->len; - arr_type_e->arrays[arr_type_e->arrays_len].ptr = *p.arr; + arr_type_e->arrays[arr_type_e->arrays_len].len = upb_array_len(arr); + arr_type_e->arrays[arr_type_e->arrays_len].ptr = v.arr; arr_type_e->arrays_len++; /* Add the individual values in the array. */ - for(uint32_t j = 0; j < arr->len; j++) - add_value(upb_array_getelementptr(arr, j), f, t); + for(uint32_t j = 0; j < upb_array_len(arr); j++) + add_value(upb_array_get(arr, f, j), f, t); /* Add submsgs. We must do this separately so that the msgs in this * array are contiguous (and don't have submsgs of the same type * interleaved). */ - for(uint32_t j = 0; j < arr->len; j++) - add_submsgs(*upb_array_getelementptr(arr, j).msg, t); + for(uint32_t j = 0; j < upb_array_len(arr); j++) + add_submsgs(upb_array_get(arr, f, j).msg, upb_downcast_msgdef(f->def), t); } else { if(!upb_issubmsg(f)) continue; - add_value(p, f, t); - add_submsgs(*p.msg, t); + add_value(v, f, t); + add_submsgs(v.msg, upb_downcast_msgdef(f->def), t); } } } /* write_messages_c emits a .c file that contains the data of a protobuf, * serialized as C structures. */ -static void write_message_c(struct upb_msg *msg, char *cident, char *hfile_name, +static void write_message_c(upb_msg *msg, struct upb_msgdef *md, + char *cident, char *hfile_name, int argc, char *argv[], char *infile_name, FILE *stream) { @@ -415,7 +423,7 @@ static void write_message_c(struct upb_msg *msg, char *cident, char *hfile_name, * same string more than once. */ struct upb_strtable strings; upb_strtable_init(&strings, 16, sizeof(struct strtable_entry)); - add_strings_from_msg(msg, &strings); + add_strings_from_msg(msg, md, &strings); int size; struct strtable_entry **str_entries = strtable_to_array(&strings, &size); @@ -427,7 +435,7 @@ static void write_message_c(struct upb_msg *msg, char *cident, char *hfile_name, int col = 2; int offset = 0; for(int i = 0; i < size; i++) { - struct upb_string *s = str_entries[i]->e.key; + upb_string *s = str_entries[i]->e.key; str_entries[i]->offset = offset; str_entries[i]->num = i; for(uint32_t j = 0; j < s->byte_len; j++) { |