More compiler work.

pull/13171/head
Joshua Haberman 16 years ago
parent 06efc6b12c
commit 907e3d7cd2
  1. 13
      upb_context.c
  2. 21
      upbc.c

@ -188,7 +188,6 @@ static bool insert_message(struct upb_strtable *t,
free(fqname.ptr);
return false;
}
printf("Inserting: " UPB_STRFMT ", len=%d\n", UPB_STRARG(e.e.key), e.e.key.byte_len);
upb_strtable_insert(t, &e.e);
/* Add nested messages and enums. */
@ -227,15 +226,9 @@ bool addfd(struct upb_strtable *addto, struct upb_strtable *existingdefs,
/* Attempt to resolve all references. */
struct upb_symtab_entry *e;
printf("Table dump:\n");
for(e = upb_strtable_begin(addto); e; e = upb_strtable_next(addto, &e->e)) {
printf(" key: " UPB_STRFMT "\n", UPB_STRARG(e->e.key));
}
for(e = upb_strtable_begin(addto); e; e = upb_strtable_next(addto, &e->e)) {
if(upb_strtable_lookup(existingdefs, &e->e.key)) {
printf("Redef!\n");
if(upb_strtable_lookup(existingdefs, &e->e.key))
return false; /* Redefinition prohibited. */
}
if(e->type == UPB_SYM_MESSAGE) {
struct upb_msg *m = e->ref.msg;
for(unsigned int i = 0; i < m->num_fields; i++) {
@ -250,9 +243,7 @@ bool addfd(struct upb_strtable *addto, struct upb_strtable *existingdefs,
UPB_SYM_ENUM);
else
continue; /* No resolving necessary. */
printf("Resolving '" UPB_STRFMT "'...", UPB_STRARG(*fd->type_name));
if(!ref.msg) { printf("undefined: " UPB_STRFMT ", len=%d\n", UPB_STRARG(*fd->type_name), fd->type_name->byte_len);return false;} /* Ref. to undefined symbol. */
printf("OK!\n");
if(!ref.msg) return false; /* Ref. to undefined symbol. */
upb_msg_ref(m, f, ref);
}
}

@ -28,6 +28,13 @@ static void to_preproc(struct upb_string str)
str.ptr[i] = toupper(str.ptr[i]);
}
static int memrchr(char *data, char c, size_t len)
{
int off = len-1;
while(off > 0 && data[off] != c) --off;
return off;
}
/* The .h file defines structs for the types defined in the .proto file. It
* also defines constants for the enum values.
*
@ -55,6 +62,14 @@ static void write_header(struct upb_symtab_entry entries[], int num_entries,
/* We use entry->e.key (the fully qualified name) instead of ed->name. */
struct upb_string enum_name = upb_strdup(entry->e.key);
to_cident(enum_name);
struct upb_string enum_val_prefix = upb_strdup(entry->e.key);
enum_val_prefix.byte_len = memrchr(enum_val_prefix.ptr,
UPB_CONTEXT_SEPARATOR,
enum_val_prefix.byte_len);
enum_val_prefix.byte_len++;
to_preproc(enum_val_prefix);
fprintf(stream, "typedef enum " UPB_STRFMT " {\n", UPB_STRARG(enum_name));
if(ed->set_flags.has.value) {
for(uint32_t j = 0; j < ed->value->len; j++) { /* Foreach enum value. */
@ -62,8 +77,8 @@ static void write_header(struct upb_symtab_entry entries[], int num_entries,
struct upb_string value_name = upb_strdup(*v->name);
to_preproc(value_name);
/* " GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32 = 13," */
fprintf(stream, " " UPB_STRFMT " = %" PRIu32,
UPB_STRARG(value_name), v->number);
fprintf(stream, " " UPB_STRFMT UPB_STRFMT " = %" PRIu32,
UPB_STRARG(enum_val_prefix), UPB_STRARG(value_name), v->number);
if(j != ed->value->len-1) fputc(',', stream);
fputc('\n', stream);
upb_strfree(value_name);
@ -71,6 +86,7 @@ static void write_header(struct upb_symtab_entry entries[], int num_entries,
}
fprintf(stream, "} " UPB_STRFMT ";\n\n", UPB_STRARG(enum_name));
upb_strfree(enum_name);
upb_strfree(enum_val_prefix);
}
/* Epilogue. */
@ -98,5 +114,6 @@ int main()
assert(e == NULL && i == symcount);
struct upb_string name = UPB_STRLIT("descriptor.proto");
write_header(entries, symcount, name, stdout);
upb_context_free(&c);
}

Loading…
Cancel
Save