|
|
|
@ -20,6 +20,8 @@ |
|
|
|
|
|
|
|
|
|
/* 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) |
|
|
|
|
{ |
|
|
|
|
for(uint32_t i = 0; i < str.byte_len; i++) |
|
|
|
@ -27,6 +29,7 @@ static void to_cident(struct upb_string str) |
|
|
|
|
str.ptr[i] = '_'; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Convert to C proprocessor identifier: foo.bar.Baz -> FOO_BAR_BAZ. */ |
|
|
|
|
static void to_preproc(struct upb_string str) |
|
|
|
|
{ |
|
|
|
|
to_cident(str); |
|
|
|
@ -58,7 +61,7 @@ void *strtable_to_array(struct upb_strtable *t, int *size) |
|
|
|
|
* |
|
|
|
|
* Assumes that d has been validated. */ |
|
|
|
|
static void write_h(struct upb_symtab_entry *entries[], int num_entries, |
|
|
|
|
char *outfile_name, FILE *stream) |
|
|
|
|
char *outfile_name, char *descriptor_cident, FILE *stream) |
|
|
|
|
{ |
|
|
|
|
/* Header file prologue. */ |
|
|
|
|
struct upb_string include_guard_name = upb_strdupc(outfile_name); |
|
|
|
@ -73,6 +76,12 @@ static void write_h(struct upb_symtab_entry *entries[], int num_entries, |
|
|
|
|
fputs("extern \"C\" {\n", stream); |
|
|
|
|
fputs("#endif\n\n", stream); |
|
|
|
|
|
|
|
|
|
if(descriptor_cident) { |
|
|
|
|
fputs("struct google_protobuf_FileDescriptorSet;\n", stream); |
|
|
|
|
fprintf(stream, "extern struct google_protobuf_FileDescriptorSet *%s;\n\n", |
|
|
|
|
descriptor_cident); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/* Enums. */ |
|
|
|
|
fprintf(stream, "/* Enums. */\n\n"); |
|
|
|
|
for(int i = 0; i < num_entries; i++) { /* Foreach enum */ |
|
|
|
@ -221,7 +230,7 @@ struct strtable_entry { |
|
|
|
|
struct typetable_entry { |
|
|
|
|
struct upb_strtable_entry e; |
|
|
|
|
struct upb_msg_field *field; |
|
|
|
|
struct upb_string c_ident; /* Type name converted with to_cident(). */ |
|
|
|
|
struct 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; |
|
|
|
@ -297,11 +306,11 @@ struct typetable_entry *get_or_insert_typeentry(struct upb_strtable *t, |
|
|
|
|
struct typetable_entry *type_e = upb_strtable_lookup(t, &type_name); |
|
|
|
|
if(type_e == NULL) { |
|
|
|
|
struct typetable_entry new_type_e = { |
|
|
|
|
.e = {.key = type_name}, .field = f, .c_ident = upb_strdup(type_name), |
|
|
|
|
.e = {.key = type_name}, .field = f, .cident = upb_strdup(type_name), |
|
|
|
|
.values = NULL, .values_size = 0, .values_len = 0, |
|
|
|
|
.arrays = NULL, .arrays_size = 0, .arrays_len = 0 |
|
|
|
|
}; |
|
|
|
|
to_cident(new_type_e.c_ident); |
|
|
|
|
to_cident(new_type_e.cident); |
|
|
|
|
upb_strtable_insert(t, &new_type_e.e); |
|
|
|
|
type_e = upb_strtable_lookup(t, &type_name); |
|
|
|
|
assert(type_e); |
|
|
|
@ -362,8 +371,8 @@ static void add_submsgs(void *data, struct upb_msg *m, struct upb_strtable *t) |
|
|
|
|
|
|
|
|
|
/* write_messages_c emits a .c file that contains the data of a protobuf,
|
|
|
|
|
* serialized as C structures. */ |
|
|
|
|
static void write_messages_c(void *data, struct upb_msg *m, |
|
|
|
|
char *hfile_name, FILE *stream) |
|
|
|
|
static void write_message_c(void *data, struct upb_msg *m, |
|
|
|
|
char *cident, char *hfile_name, FILE *stream) |
|
|
|
|
{ |
|
|
|
|
fputs("/* This file was generated by upbc (the upb compiler). " |
|
|
|
|
"Do not edit. */\n\n", stream), |
|
|
|
@ -424,24 +433,24 @@ static void write_messages_c(void *data, struct upb_msg *m, |
|
|
|
|
struct typetable_entry *e = upb_strtable_begin(&types); |
|
|
|
|
for(; e; e = upb_strtable_next(&types, &e->e)) { |
|
|
|
|
fprintf(stream, "static " UPB_STRFMT " " UPB_STRFMT "_values[%d];\n\n", |
|
|
|
|
UPB_STRARG(e->c_ident), UPB_STRARG(e->c_ident), e->values_len); |
|
|
|
|
UPB_STRARG(e->cident), UPB_STRARG(e->cident), e->values_len); |
|
|
|
|
if(e->arrays_len > 0) { |
|
|
|
|
fprintf(stream, "static " UPB_STRFMT " *" UPB_STRFMT "_array_elems[] = {\n", |
|
|
|
|
UPB_STRARG(e->c_ident), UPB_STRARG(e->c_ident)); |
|
|
|
|
UPB_STRARG(e->cident), UPB_STRARG(e->cident)); |
|
|
|
|
for(int i = 0; i < e->arrays_len; i++) { |
|
|
|
|
struct array *arr = &e->arrays[i]; |
|
|
|
|
for(int j = 0; j < arr->len; j++) |
|
|
|
|
fprintf(stream, " &" UPB_STRFMT "_values[%d],\n", UPB_STRARG(e->c_ident), arr->offset + j); |
|
|
|
|
fprintf(stream, " &" UPB_STRFMT "_values[%d],\n", UPB_STRARG(e->cident), arr->offset + j); |
|
|
|
|
} |
|
|
|
|
fprintf(stream, "};\n"); |
|
|
|
|
|
|
|
|
|
int cum_offset = 0; |
|
|
|
|
fprintf(stream, "static UPB_MSG_ARRAY(" UPB_STRFMT ") " UPB_STRFMT "_arrays[%d] = {\n", |
|
|
|
|
UPB_STRARG(e->c_ident), UPB_STRARG(e->c_ident), e->arrays_len); |
|
|
|
|
UPB_STRARG(e->cident), UPB_STRARG(e->cident), e->arrays_len); |
|
|
|
|
for(int i = 0; i < e->arrays_len; i++) { |
|
|
|
|
struct array *arr = &e->arrays[i]; |
|
|
|
|
fprintf(stream, " {.elements = &" UPB_STRFMT "_array_elems[%d], .len=%d},\n", |
|
|
|
|
UPB_STRARG(e->c_ident), cum_offset, arr->len); |
|
|
|
|
UPB_STRARG(e->cident), cum_offset, arr->len); |
|
|
|
|
cum_offset += arr->len; |
|
|
|
|
} |
|
|
|
|
fprintf(stream, "};\n"); |
|
|
|
@ -451,19 +460,19 @@ static void write_messages_c(void *data, struct upb_msg *m, |
|
|
|
|
/* Emit definitions. */ |
|
|
|
|
for(e = upb_strtable_begin(&types); e; e = upb_strtable_next(&types, &e->e)) { |
|
|
|
|
fprintf(stream, "static " UPB_STRFMT " " UPB_STRFMT "_values[%d] = {\n\n", |
|
|
|
|
UPB_STRARG(e->c_ident), UPB_STRARG(e->c_ident), e->values_len); |
|
|
|
|
UPB_STRARG(e->cident), UPB_STRARG(e->cident), e->values_len); |
|
|
|
|
for(int i = 0; i < e->values_len; i++) { |
|
|
|
|
union upb_value val = e->values[i]; |
|
|
|
|
if(upb_issubmsg(e->field)) { |
|
|
|
|
struct upb_msg *m = e->field->ref.msg; |
|
|
|
|
void *msgdata = val.msg; |
|
|
|
|
/* Print set flags. */ |
|
|
|
|
fprintf(stream, " {.set_flags = {.bytes = {"); |
|
|
|
|
fputs(" {.set_flags = {.bytes = {", stream); |
|
|
|
|
for(unsigned int j = 0; j < m->set_flags_bytes; j++) { |
|
|
|
|
fprintf(stream, "0x%02hhx", *(uint8_t*)(val.msg + j)); |
|
|
|
|
if(j < m->set_flags_bytes - 1) fprintf(stream, ", "); |
|
|
|
|
if(j < m->set_flags_bytes - 1) fputs(", ", stream); |
|
|
|
|
} |
|
|
|
|
fprintf(stream, "}},\n"); |
|
|
|
|
fputs("}},\n", stream); |
|
|
|
|
/* Print msg data. */ |
|
|
|
|
for(unsigned int j = 0; j < m->num_fields; j++) { |
|
|
|
|
struct upb_msg_field *f = &m->fields[j]; |
|
|
|
@ -471,10 +480,10 @@ static void write_messages_c(void *data, struct upb_msg *m, |
|
|
|
|
union upb_value val = upb_msg_get(msgdata, f); |
|
|
|
|
fprintf(stream, " ." UPB_STRFMT " = ", UPB_STRARG(*fd->name)); |
|
|
|
|
if(!upb_msg_isset(msgdata, f)) { |
|
|
|
|
fprintf(stream, "0, /* Not set. */"); |
|
|
|
|
fputs("0, /* Not set. */", stream); |
|
|
|
|
} else if(upb_isstring(f)) { |
|
|
|
|
if(upb_isarray(f)) { |
|
|
|
|
fprintf(stderr, "Ack, string arrays are not supported yet!\n"); |
|
|
|
|
fputs("Ack, string arrays are not supported yet!\n", stderr); |
|
|
|
|
exit(1); |
|
|
|
|
} else { |
|
|
|
|
struct strtable_entry *str_e = upb_strtable_lookup(&strings, val.str); |
|
|
|
@ -494,7 +503,7 @@ static void write_messages_c(void *data, struct upb_msg *m, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
assert(arr_num != -1); |
|
|
|
|
fprintf(stream, "&" UPB_STRFMT "_arrays[%d],", UPB_STRARG(type_e->c_ident), arr_num); |
|
|
|
|
fprintf(stream, "&" UPB_STRFMT "_arrays[%d],", UPB_STRARG(type_e->cident), arr_num); |
|
|
|
|
} else if(upb_issubmsg(f)) { |
|
|
|
|
/* Find this submessage in the list of msgs for that type. */ |
|
|
|
|
struct typetable_entry *type_e = get_or_insert_typeentry(&types, f); |
|
|
|
@ -507,14 +516,14 @@ static void write_messages_c(void *data, struct upb_msg *m, |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
assert(msg_num != -1); |
|
|
|
|
fprintf(stream, "&" UPB_STRFMT "_values[%d],", UPB_STRARG(type_e->c_ident), msg_num); |
|
|
|
|
fprintf(stream, "&" UPB_STRFMT "_values[%d],", UPB_STRARG(type_e->cident), msg_num); |
|
|
|
|
} else { |
|
|
|
|
upb_text_printval(f->type, val, stream); |
|
|
|
|
fprintf(stream, ","); |
|
|
|
|
fputs(",", stream); |
|
|
|
|
} |
|
|
|
|
fprintf(stream, "\n"); |
|
|
|
|
fputs("\n", stream); |
|
|
|
|
} |
|
|
|
|
fprintf(stream, " },\n"); |
|
|
|
|
fputs(" },\n", stream); |
|
|
|
|
} else if(upb_isstring(e->field)) { |
|
|
|
|
|
|
|
|
|
} else { |
|
|
|
@ -522,15 +531,28 @@ static void write_messages_c(void *data, struct upb_msg *m, |
|
|
|
|
upb_text_printval(e->field->type, val, stream); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
fprintf(stream, "};\n"); |
|
|
|
|
fputs("};\n", stream); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
struct typetable_entry *toplevel_type = |
|
|
|
|
get_or_insert_typeentry(&types, &fake_field); |
|
|
|
|
assert(toplevel_type); |
|
|
|
|
fputs("/* The externally-visible definition. */\n", stream); |
|
|
|
|
/* It is always at offset zero, because we add it first. */ |
|
|
|
|
fprintf(stream, UPB_STRFMT " *%s = &" UPB_STRFMT "_values[0];\n", |
|
|
|
|
UPB_STRARG(toplevel_type->cident), cident, |
|
|
|
|
UPB_STRARG(toplevel_type->cident)); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
const char usage[] = |
|
|
|
|
"upbc -- upb compiler.\n" |
|
|
|
|
"upb v0.1 http://blog.reverberate.org/upb/\n" |
|
|
|
|
"\n" |
|
|
|
|
"Usage: upbc [options] input-file\n" |
|
|
|
|
"Usage: upbc [options] descriptor-file\n" |
|
|
|
|
"\n" |
|
|
|
|
" -i C-IDENFITER Output the descriptor as a C data structure with the\n" |
|
|
|
|
" given identifier (otherwise only a header will be\n" |
|
|
|
|
" generated\n" |
|
|
|
|
"\n" |
|
|
|
|
" -o OUTFILE-BASE Write to OUTFILE-BASE.h and OUTFILE-BASE.c instead\n" |
|
|
|
|
" of using the input file as a basename.\n" |
|
|
|
@ -552,7 +574,7 @@ void error(char *err) |
|
|
|
|
int main(int argc, char *argv[]) |
|
|
|
|
{ |
|
|
|
|
/* Parse arguments. */ |
|
|
|
|
char *outfile_base = NULL, *input_file = NULL; |
|
|
|
|
char *outfile_base = NULL, *input_file = NULL, *cident = NULL; |
|
|
|
|
for(int i = 1; i < argc; i++) { |
|
|
|
|
if(strcmp(argv[i], "-o") == 0) { |
|
|
|
|
if(++i == argc) |
|
|
|
@ -560,6 +582,12 @@ int main(int argc, char *argv[]) |
|
|
|
|
else if(outfile_base) |
|
|
|
|
usage_err("-o was specified multiple times."); |
|
|
|
|
outfile_base = argv[i]; |
|
|
|
|
} else if(strcmp(argv[i], "-i") == 0) { |
|
|
|
|
if(++i == argc) |
|
|
|
|
usage_err("-i must be followed by a C-IDENTIFIER."); |
|
|
|
|
else if(cident) |
|
|
|
|
usage_err("-i was specified multiple times."); |
|
|
|
|
cident = argv[i]; |
|
|
|
|
} else { |
|
|
|
|
if(input_file) |
|
|
|
|
usage_err("You can only specify one input file."); |
|
|
|
@ -591,18 +619,21 @@ int main(int argc, char *argv[]) |
|
|
|
|
snprintf(c_filename, maxsize, "%s.c", outfile_base) >= maxsize) |
|
|
|
|
error("File base too long.\n"); |
|
|
|
|
|
|
|
|
|
FILE *h_file = fopen(h_filename, "w"), *c_file = fopen(c_filename, "w"); |
|
|
|
|
if(!h_file || !c_file) |
|
|
|
|
error("Failed to open output file(s)"); |
|
|
|
|
FILE *h_file = fopen(h_filename, "w"); |
|
|
|
|
if(!h_file) error("Failed to open .h output file"); |
|
|
|
|
|
|
|
|
|
int symcount; |
|
|
|
|
struct upb_symtab_entry **entries = strtable_to_array(&c.symtab, &symcount); |
|
|
|
|
write_h(entries, symcount, h_filename, h_file); |
|
|
|
|
write_messages_c(fds, c.fds_msg, h_filename, c_file); |
|
|
|
|
write_h(entries, symcount, h_filename, cident, h_file); |
|
|
|
|
if(cident) { |
|
|
|
|
FILE *c_file = fopen(c_filename, "w"); |
|
|
|
|
if(!c_file) error("Failed to open .h output file"); |
|
|
|
|
write_message_c(fds, c.fds_msg, cident, h_filename, c_file); |
|
|
|
|
fclose(c_file); |
|
|
|
|
} |
|
|
|
|
upb_context_free(&c); |
|
|
|
|
upb_strfree(descriptor); |
|
|
|
|
fclose(h_file); |
|
|
|
|
fclose(c_file); |
|
|
|
|
|
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|