summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJoshua Haberman <joshua@reverberate.org>2009-07-22 19:49:53 -0700
committerJoshua Haberman <joshua@reverberate.org>2009-07-22 19:49:53 -0700
commitee1ed1ccb87effa403dc91603d452d4c98ed716f (patch)
treee1ee8c1b38d9351d4d3494c5e45b91b288f3a081
parente4e89247e5f7548b5ebdb9a667657cdcb3feb17f (diff)
Compiler finally works (except string arrays). Untested. Holy crap that was a lot of work.
-rw-r--r--src/upb.h23
-rw-r--r--src/upb_array.h6
-rw-r--r--src/upb_msg.c4
-rw-r--r--src/upb_msg.h7
-rw-r--r--src/upb_parse.c37
-rw-r--r--src/upb_string.c1
-rw-r--r--src/upb_string.h10
-rw-r--r--src/upb_text.c8
-rw-r--r--src/upb_text.h4
-rw-r--r--tools/upbc.c183
10 files changed, 187 insertions, 96 deletions
diff --git a/src/upb.h b/src/upb.h
index 113e6e8..f8cbba3 100644
--- a/src/upb.h
+++ b/src/upb.h
@@ -12,6 +12,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h> /* for size_t. */
+#include "upb_string.h"
#ifdef __cplusplus
extern "C" {
@@ -35,7 +36,6 @@ extern "C" {
#define UPB_INDEX(base, i, m) (void*)((char*)(base) + ((i)*(m)))
INLINE uint32_t max(uint32_t a, uint32_t b) { return a > b ? a : b; }
-INLINE uint32_t min(uint32_t a, uint32_t b) { return a < b ? a : b; }
/* Value type as defined in a .proto file. The values of this are defined by
* google_protobuf_FieldDescriptorProto_Type (from descriptor.proto).
@@ -43,20 +43,21 @@ INLINE uint32_t min(uint32_t a, uint32_t b) { return a < b ? a : b; }
* represent exceptional circumstances. */
typedef uint8_t upb_field_type_t;
-/* Label (optional, repeated, required) as defined in a .proto file. The values
- * of this are defined by google.protobuf.FieldDescriptorProto.Label (from
- * descriptor.proto). */
-typedef uint8_t upb_label_t;
-
struct upb_type_info {
uint8_t align;
uint8_t size;
uint8_t expected_wire_type;
+ struct upb_string ctype;
};
/* Contains information for all .proto types. Indexed by upb_field_type_t. */
extern struct upb_type_info upb_type_info[];
+/* Label (optional, repeated, required) as defined in a .proto file. The values
+ * of this are defined by google.protobuf.FieldDescriptorProto.Label (from
+ * descriptor.proto). */
+typedef uint8_t upb_label_t;
+
/* A pointer to a .proto value. The owner must have an out-of-band way of
* knowing the type, so it knows which union member to use. */
union upb_value {
@@ -67,8 +68,8 @@ union upb_value {
uint32_t uint32;
uint64_t uint64;
bool _bool;
- struct upb_string **string;
- struct upb_array **array;
+ struct upb_string *str;
+ struct upb_array *arr;
void *msg;
};
@@ -86,6 +87,12 @@ union upb_value_ptr {
void *_void;
};
+INLINE union upb_value upb_deref(union upb_value_ptr ptr, upb_field_type_t t) {
+ union upb_value val;
+ memcpy(&val, ptr._void, upb_type_info[t].size);
+ return val;
+}
+
union upb_symbol_ref {
struct upb_msg *msg;
struct upb_enum *_enum;
diff --git a/src/upb_array.h b/src/upb_array.h
index 4282a7f..0e5178f 100644
--- a/src/upb_array.h
+++ b/src/upb_array.h
@@ -34,6 +34,12 @@ INLINE union upb_value_ptr upb_array_getelementptr(
return ptr;
}
+INLINE union upb_value upb_array_getelement(
+ struct upb_array *arr, uint32_t n, upb_field_type_t type)
+{
+ return upb_deref(upb_array_getelementptr(arr, n, type), type);
+}
+
/* These are all overlays on upb_array, pointers between them can be cast. */
#define UPB_DEFINE_ARRAY_TYPE(name, type) \
struct name ## _array { \
diff --git a/src/upb_msg.c b/src/upb_msg.c
index d44649e..19edba8 100644
--- a/src/upb_msg.c
+++ b/src/upb_msg.c
@@ -265,7 +265,7 @@ static union upb_value_ptr get_value_ptr(void *data, struct upb_msg_field *f)
{
union upb_value_ptr p = upb_msg_getptr(data, f);
if(upb_isarray(f)) {
- size_t len = upb_msg_is_set(data, f) ? (*p.arr)->len : 0;
+ size_t len = upb_msg_isset(data, f) ? (*p.arr)->len : 0;
upb_msg_reuse_array(p.arr, len+1, f->type);
(*p.arr)->len = len + 1;
assert(p._void);
@@ -448,7 +448,7 @@ bool upb_msg_eql(void *data1, void *data2, struct upb_msg *m, bool recursive)
for(uint32_t i = 0; i < m->num_fields; i++) {
struct upb_msg_field *f = &m->fields[i];
- if(!upb_msg_is_set(data1, f)) continue;
+ if(!upb_msg_isset(data1, f)) continue;
union upb_value_ptr p1 = upb_msg_getptr(data1, f);
union upb_value_ptr p2 = upb_msg_getptr(data2, f);
if(f->label == GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_LABEL_REPEATED) {
diff --git a/src/upb_msg.h b/src/upb_msg.h
index 6be9405..233e604 100644
--- a/src/upb_msg.h
+++ b/src/upb_msg.h
@@ -231,7 +231,7 @@ INLINE void upb_msg_unset(void *s, struct upb_msg_field *f)
((char*)s)[upb_isset_offset(f->field_index)] &= ~upb_isset_mask(f->field_index);
}
-INLINE bool upb_msg_is_set(void *s, struct upb_msg_field *f)
+INLINE bool upb_msg_isset(void *s, struct upb_msg_field *f)
{
return ((char*)s)[upb_isset_offset(f->field_index)] & upb_isset_mask(f->field_index);
}
@@ -262,6 +262,11 @@ INLINE union upb_value_ptr upb_msg_getptr(void *data, struct upb_msg_field *f) {
return p;
}
+/* Returns a a specific field in a message. */
+INLINE union upb_value upb_msg_get(void *data, struct upb_msg_field *f) {
+ return upb_deref(upb_msg_getptr(data, f), f->type);
+}
+
/* Memory management *********************************************************/
/* One important note about these memory management routines: they must be used
diff --git a/src/upb_parse.c b/src/upb_parse.c
index 60533b4..96b7647 100644
--- a/src/upb_parse.c
+++ b/src/upb_parse.c
@@ -11,25 +11,26 @@
/* May want to move this to upb.c if enough other things warrant it. */
#define alignof(t) offsetof(struct { char c; t x; }, x)
+#define TYPE_INFO(proto_type, wire_type, ctype) [proto_type] = {alignof(ctype), sizeof(ctype), wire_type, UPB_STRLIT(#ctype)},
struct upb_type_info upb_type_info[] = {
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_DOUBLE] = {alignof(double), sizeof(double), UPB_WIRE_TYPE_64BIT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FLOAT] = {alignof(float), sizeof(float), UPB_WIRE_TYPE_32BIT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT64] = {alignof(int64_t), sizeof(int64_t), UPB_WIRE_TYPE_VARINT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT64] = {alignof(uint64_t), sizeof(uint64_t), UPB_WIRE_TYPE_VARINT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32] = {alignof(int32_t), sizeof(int32_t), UPB_WIRE_TYPE_VARINT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED64] = {alignof(uint64_t), sizeof(uint64_t), UPB_WIRE_TYPE_64BIT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED32] = {alignof(uint32_t), sizeof(uint32_t), UPB_WIRE_TYPE_32BIT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BOOL] = {alignof(bool), sizeof(bool), UPB_WIRE_TYPE_VARINT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE] = {alignof(void*), sizeof(void*), UPB_WIRE_TYPE_DELIMITED},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP] = {alignof(void*), sizeof(void*), UPB_WIRE_TYPE_START_GROUP},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32] = {alignof(uint32_t), sizeof(uint32_t), UPB_WIRE_TYPE_VARINT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ENUM] = {alignof(uint32_t), sizeof(uint32_t), UPB_WIRE_TYPE_VARINT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED32]= {alignof(int32_t), sizeof(int32_t), UPB_WIRE_TYPE_32BIT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED64]= {alignof(int64_t), sizeof(int64_t), UPB_WIRE_TYPE_64BIT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT32] = {alignof(int32_t), sizeof(int32_t), UPB_WIRE_TYPE_VARINT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64] = {alignof(int64_t), sizeof(int64_t), UPB_WIRE_TYPE_VARINT},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING] = {alignof(struct upb_string*), sizeof(struct upb_string*), UPB_WIRE_TYPE_DELIMITED},
- [GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES] = {alignof(struct upb_string*), sizeof(struct upb_string*), UPB_WIRE_TYPE_DELIMITED},
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_DOUBLE, UPB_WIRE_TYPE_64BIT, double)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FLOAT, UPB_WIRE_TYPE_32BIT, float)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT64, UPB_WIRE_TYPE_VARINT, int64_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT64, UPB_WIRE_TYPE_VARINT, uint64_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32, UPB_WIRE_TYPE_VARINT, int32_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED64, UPB_WIRE_TYPE_64BIT, uint64_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_FIXED32, UPB_WIRE_TYPE_32BIT, uint32_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BOOL, UPB_WIRE_TYPE_VARINT, bool)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE, UPB_WIRE_TYPE_DELIMITED, void*)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_GROUP, UPB_WIRE_TYPE_START_GROUP, void*)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_UINT32, UPB_WIRE_TYPE_VARINT, uint32_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_ENUM, UPB_WIRE_TYPE_VARINT, uint32_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED32, UPB_WIRE_TYPE_32BIT, int32_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SFIXED64, UPB_WIRE_TYPE_64BIT, int64_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT32, UPB_WIRE_TYPE_VARINT, int32_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_SINT64, UPB_WIRE_TYPE_VARINT, int64_t)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING, UPB_WIRE_TYPE_DELIMITED, struct upb_string*)
+ TYPE_INFO(GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES, UPB_WIRE_TYPE_DELIMITED, struct upb_string*)
};
/* Lowest-level functions -- these read integers from the input buffer. */
diff --git a/src/upb_string.c b/src/upb_string.c
index 733bafe..bb40a62 100644
--- a/src/upb_string.c
+++ b/src/upb_string.c
@@ -4,6 +4,7 @@
* Copyright (c) 2009 Joshua Haberman. See LICENSE for details.
*/
+#include <stdio.h>
#include "upb_string.h"
bool upb_strreadfile(const char *filename, struct upb_string *data) {
diff --git a/src/upb_string.h b/src/upb_string.h
index de2384d..8e56daa 100644
--- a/src/upb_string.h
+++ b/src/upb_string.h
@@ -27,9 +27,15 @@
extern "C" {
#endif
+#include <stdbool.h>
+#include <stdint.h>
#include <stdlib.h>
#include <string.h>
-#include "upb.h"
+
+/* inline if possible, emit standalone code if required. */
+#ifndef INLINE
+#define INLINE static inline
+#endif
struct upb_string {
/* We expect the data to be 8-bit clean (uint8_t), but char* is such an
@@ -38,6 +44,8 @@ struct upb_string {
uint32_t byte_len;
};
+INLINE uint32_t min(uint32_t a, uint32_t b) { return a < b ? a : b; }
+
INLINE bool upb_streql(struct upb_string *s1, struct upb_string *s2) {
return s1->byte_len == s2->byte_len &&
memcmp(s1->ptr, s2->ptr, s1->byte_len) == 0;
diff --git a/src/upb_text.c b/src/upb_text.c
index d51db06..7ca17cc 100644
--- a/src/upb_text.c
+++ b/src/upb_text.c
@@ -8,9 +8,9 @@
#include "upb_text.h"
#include "descriptor.h"
-void upb_text_printval(upb_field_type_t type, union upb_value_ptr p, FILE *file)
+void upb_text_printval(upb_field_type_t type, union upb_value val, FILE *file)
{
-#define CASE(fmtstr, member) fprintf(file, fmtstr, *p.member); break;
+#define CASE(fmtstr, member) fprintf(file, fmtstr, val.member); break;
switch(type) {
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_DOUBLE:
CASE("%0.f", _double);
@@ -36,7 +36,7 @@ void upb_text_printval(upb_field_type_t type, union upb_value_ptr p, FILE *file)
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_STRING:
case GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_BYTES:
/* TODO: escaping. */
- fprintf(file, "\"" UPB_STRFMT "\"", UPB_STRARG(**p.str)); break;
+ fprintf(file, "\"" UPB_STRFMT "\"", UPB_STRARG(*val.str)); break;
}
}
@@ -49,7 +49,7 @@ static void print_indent(struct upb_text_printer *p, FILE *stream)
void upb_text_printfield(struct upb_text_printer *p,
struct upb_string name,
- upb_field_type_t valtype, union upb_value_ptr val,
+ upb_field_type_t valtype, union upb_value val,
FILE *stream)
{
print_indent(p, stream);
diff --git a/src/upb_text.h b/src/upb_text.h
index b1dbf59..f35f8d8 100644
--- a/src/upb_text.h
+++ b/src/upb_text.h
@@ -22,9 +22,9 @@ INLINE void upb_text_printer_init(struct upb_text_printer *p, bool single_line)
p->indent_depth = 0;
p->single_line = single_line;
}
-void upb_text_printval(upb_field_type_t type, union upb_value_ptr p, FILE *file);
+void upb_text_printval(upb_field_type_t type, union upb_value p, FILE *file);
void upb_text_printfield(struct upb_text_printer *p, struct upb_string name,
- upb_field_type_t valtype, union upb_value_ptr val,
+ upb_field_type_t valtype, union upb_value val,
FILE *stream);
void upb_text_push(struct upb_text_printer *p, struct upb_string submsg_type,
FILE *stream);
diff --git a/tools/upbc.c b/tools/upbc.c
index 3a4de70..b0f6d6c 100644
--- a/tools/upbc.c
+++ b/tools/upbc.c
@@ -1,7 +1,11 @@
/*
* upb - a minimalist implementation of protocol buffers.
*
- * upbc is the upb compiler.
+ * upbc is the upb compiler. This is some deep code that I wish could be
+ * easier to understand, but by its nature it is doing some very "meta"
+ * kinds of things.
+ *
+ * TODO: compiler currently has memory leaks (trivial to fix with valgrind).
*
* Copyright (c) 2009 Joshua Haberman. See LICENSE for details.
*/
@@ -12,6 +16,7 @@
#include "upb_context.h"
#include "upb_enum.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). */
@@ -215,13 +220,15 @@ struct strtable_entry {
struct typetable_entry {
struct upb_strtable_entry e;
- struct upb_msg *msg;
- void **msgs; /* A list of all msgs of this type, in an established order. */
- int msgs_size, msgs_len;
+ struct upb_msg_field *field;
+ struct upb_string c_ident; /* 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;
- void *elem0;
+ struct upb_array *ptr; /* So we can find it later. */
} *arrays;
int arrays_size, arrays_len;
};
@@ -264,7 +271,7 @@ static void add_strings_from_msg(void *data, struct upb_msg *m,
{
for(uint32_t i = 0; i < m->num_fields; i++) {
struct upb_msg_field *f = &m->fields[i];
- if(!upb_msg_is_set(data, f)) continue;
+ if(!upb_msg_isset(data, f)) continue;
union upb_value_ptr p = upb_msg_getptr(data, f);
if(upb_isarray(f)) {
struct upb_array *arr = *p.arr;
@@ -283,59 +290,62 @@ static void add_strings_from_msg(void *data, struct upb_msg *m,
struct typetable_entry *get_or_insert_typeentry(struct upb_strtable *t,
- struct upb_msg *m)
+ struct upb_msg_field *f)
{
- struct typetable_entry *type_e = upb_strtable_lookup(t, &m->fqname);
+ struct upb_string type_name = upb_issubmsg(f) ? f->ref.msg->fqname :
+ 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 = {.e = {.key = m->fqname},
- .msg = m,
- .msgs = NULL,
- .msgs_size = 0, .msgs_len = 0,
- .arrays = NULL,
- .arrays_size = 0, .arrays_len = 0};
+ struct typetable_entry new_type_e = {
+ .e = {.key = type_name}, .field = f, .c_ident = 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);
upb_strtable_insert(t, &new_type_e.e);
- type_e = upb_strtable_lookup(t, &m->fqname);
+ type_e = upb_strtable_lookup(t, &type_name);
assert(type_e);
}
return type_e;
}
-static void add_msg(void *data, struct upb_msg *m, struct upb_strtable *t)
+static void add_value(union upb_value value, struct upb_msg_field *f,
+ struct upb_strtable *t)
{
- struct typetable_entry *type_e = get_or_insert_typeentry(t, m);
- if(type_e->msgs_size == type_e->msgs_len) {
- type_e->msgs_size = max(type_e->msgs_size * 2, 4);
- type_e->msgs = realloc(type_e->msgs, sizeof(*type_e->msgs) * type_e->msgs_size);
+ struct typetable_entry *type_e = get_or_insert_typeentry(t, f);
+ if(type_e->values_len == type_e->values_size) {
+ type_e->values_size = max(type_e->values_size * 2, 4);
+ type_e->values = realloc(type_e->values, sizeof(*type_e->values) * type_e->values_size);
}
- type_e->msgs[type_e->msgs_len++] = data;
+ type_e->values[type_e->values_len++] = value;
}
static void add_submsgs(void *data, struct upb_msg *m, struct upb_strtable *t)
{
for(uint32_t i = 0; i < m->num_fields; i++) {
struct upb_msg_field *f = &m->fields[i];
- if(!upb_issubmsg(f) || !upb_msg_is_set(data, f)) continue;
+ if(!upb_msg_isset(data, f)) continue;
union upb_value_ptr p = upb_msg_getptr(data, f);
if(upb_isarray(f)) {
+ if(upb_isstring(f)) continue; /* Handled by a different code-path. */
struct upb_array *arr = *p.arr;
/* Add to our list of arrays for this type. */
struct typetable_entry *arr_type_e =
- get_or_insert_typeentry(t, f->ref.msg);
+ get_or_insert_typeentry(t, f);
if(arr_type_e->arrays_len == arr_type_e->arrays_size) {
arr_type_e->arrays_size = max(arr_type_e->arrays_size * 2, 4);
arr_type_e->arrays = realloc(arr_type_e->arrays,
sizeof(*arr_type_e->arrays)*arr_type_e->arrays_size);
}
- arr_type_e->arrays[arr_type_e->arrays_len].offset = arr_type_e->msgs_len;
+ 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].elem0 =
- *upb_array_getelementptr(arr, 0, f->type).msg;
+ arr_type_e->arrays[arr_type_e->arrays_len].ptr = *p.arr;
arr_type_e->arrays_len++;
- /* Add the individual messages in the array. */
+ /* Add the individual values in the array. */
for(uint32_t j = 0; j < arr->len; j++)
- add_msg(*upb_array_getelementptr(arr, j, f->type).msg, f->ref.msg, t);
+ add_value(upb_array_getelement(arr, j, f->type), 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
@@ -343,7 +353,8 @@ static void add_submsgs(void *data, struct upb_msg *m, struct upb_strtable *t)
for(uint32_t j = 0; j < arr->len; j++)
add_submsgs(*upb_array_getelementptr(arr, j, f->type).msg, f->ref.msg, t);
} else {
- add_msg(*p.msg, f->ref.msg, t);
+ if(!upb_issubmsg(f)) continue;
+ add_value(upb_deref(p, f->type), f, t);
add_submsgs(*p.msg, f->ref.msg, t);
}
}
@@ -399,65 +410,117 @@ static void write_messages_c(void *data, struct upb_msg *m,
* a unique number within its type. */
struct upb_strtable types;
upb_strtable_init(&types, 16, sizeof(struct typetable_entry));
- add_msg(data, m, &types);
+ union upb_value val = {.msg = data};
+ /* A fake field to get the recursion going. */
+ struct upb_msg_field fake_field = {
+ .type = GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_MESSAGE,
+ .ref = {.msg = m}
+ };
+ add_value(val, &fake_field, &types);
add_submsgs(data, m, &types);
/* Emit foward declarations for all msgs of all types, and define arrays. */
fprintf(stream, "/* Forward declarations of messages, and array decls. */\n");
struct typetable_entry *e = upb_strtable_begin(&types);
for(; e; e = upb_strtable_next(&types, &e->e)) {
- struct upb_string s = upb_strdup(e->e.key);
- to_cident(s);
- fprintf(stream, "static " UPB_STRFMT " " UPB_STRFMT "_msgs[%d];\n\n",
- UPB_STRARG(s), UPB_STRARG(s), e->msgs_len);
+ fprintf(stream, "static " UPB_STRFMT " " UPB_STRFMT "_values[%d];\n\n",
+ UPB_STRARG(e->c_ident), UPB_STRARG(e->c_ident), e->values_len);
if(e->arrays_len > 0) {
fprintf(stream, "static " UPB_STRFMT " *" UPB_STRFMT "_array_elems[] = {\n",
- UPB_STRARG(s), UPB_STRARG(s));
+ UPB_STRARG(e->c_ident), UPB_STRARG(e->c_ident));
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 "_msgs[%d],\n", UPB_STRARG(s), arr->offset + j);
+ fprintf(stream, " &" UPB_STRFMT "_values[%d],\n", UPB_STRARG(e->c_ident), 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(s), UPB_STRARG(s), e->arrays_len);
+ UPB_STRARG(e->c_ident), UPB_STRARG(e->c_ident), 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(s), cum_offset, arr->len);
+ UPB_STRARG(e->c_ident), cum_offset, arr->len);
cum_offset += arr->len;
}
fprintf(stream, "};\n");
}
- upb_strfree(s);
}
/* Emit definitions. */
for(e = upb_strtable_begin(&types); e; e = upb_strtable_next(&types, &e->e)) {
- struct upb_string s = upb_strdup(e->e.key);
- to_cident(s);
- fprintf(stream, "static " UPB_STRFMT " " UPB_STRFMT "_msgs[%d] = {\n\n",
- UPB_STRARG(s), UPB_STRARG(s), e->msgs_len);
- for(int i = 0; i < e->msgs_len; i++) {
- void *msgdata = e->msgs[i];
- /* Print set flags. */
- fprintf(stream, " {.set_flags = {.bytes = {");
- for(unsigned int j = 0; j < e->msg->set_flags_bytes; j++) {
- fprintf(stream, "0x%02hhx", *(uint8_t*)(msgdata + j));
- if(j < e->msg->set_flags_bytes - 1) fprintf(stream, ", ");
- }
- fprintf(stream, "}},\n");
- /* Print msg data. */
- for(unsigned int j = 0; j < e->msg->num_fields; j++) {
- struct upb_msg_field *f = &e->msg->fields[j];
- struct google_protobuf_FieldDescriptorProto *fd =
- e->msg->field_descriptors[j];
- fprintf(stream, " ." UPB_STRFMT " = ", UPB_STRARG(*fd->name));
- fprintf(stream, "\n");
+ fprintf(stream, "static " UPB_STRFMT " " UPB_STRFMT "_values[%d] = {\n\n",
+ UPB_STRARG(e->c_ident), UPB_STRARG(e->c_ident), 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 = {");
+ 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, ", ");
+ }
+ fprintf(stream, "}},\n");
+ /* Print msg data. */
+ for(unsigned int j = 0; j < m->num_fields; j++) {
+ struct upb_msg_field *f = &m->fields[j];
+ google_protobuf_FieldDescriptorProto *fd = m->field_descriptors[j];
+ 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. */");
+ } else if(upb_isstring(f)) {
+ if(upb_isarray(f)) {
+ fprintf(stderr, "Ack, string arrays are not supported yet!\n");
+ exit(1);
+ } else {
+ struct strtable_entry *str_e = upb_strtable_lookup(&strings, val.str);
+ assert(str_e);
+ fprintf(stream, "&strings[%d], /* \"" UPB_STRFMT "\" */",
+ str_e->num, UPB_STRARG(*val.str));
+ }
+ } else if(upb_isarray(f)) {
+ /* Find this submessage in the list of msgs for that type. */
+ struct typetable_entry *type_e = get_or_insert_typeentry(&types, f);
+ assert(type_e);
+ int arr_num = -1;
+ for(int k = 0; k < type_e->arrays_len; k++) {
+ if(type_e->arrays[k].ptr == val.arr) {
+ arr_num = k;
+ break;
+ }
+ }
+ assert(arr_num != -1);
+ fprintf(stream, "&" UPB_STRFMT "_arrays[%d],", UPB_STRARG(type_e->c_ident), 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);
+ assert(type_e);
+ int msg_num = -1;
+ for(int k = 0; k < type_e->values_len; k++) {
+ if(type_e->values[k].msg == val.msg) {
+ msg_num = k;
+ break;
+ }
+ }
+ assert(msg_num != -1);
+ fprintf(stream, "&" UPB_STRFMT "_values[%d],", UPB_STRARG(type_e->c_ident), msg_num);
+ } else {
+ upb_text_printval(f->type, val, stream);
+ fprintf(stream, ",");
+ }
+ fprintf(stream, "\n");
+ }
+ fprintf(stream, " },\n");
+ } else if(upb_isstring(e->field)) {
+
+ } else {
+ /* Non string, non-message data. */
+ upb_text_printval(e->field->type, val, stream);
}
- fprintf(stream, " },\n");
}
fprintf(stream, "};\n");
}
generated by cgit on debian on lair
contact matthew@masot.net with questions or feedback