From 611afe9c6928ea814abd37c4b3cc2869a6ed5efd Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sat, 16 Jan 2010 09:00:59 -0800 Subject: Removed union tag from types. --- src/upb.c | 4 ++-- src/upb.h | 23 +++++++++++------------ src/upb_data.c | 28 ++++++++++++++-------------- src/upb_data.h | 34 ++++++++++++++++------------------ src/upb_decoder.c | 18 +++++++----------- src/upb_def.h | 2 +- src/upb_encoder.c | 11 +++++------ src/upb_sink.h | 2 +- src/upb_text.c | 14 ++++++-------- src/upb_text.h | 5 ++--- 10 files changed, 65 insertions(+), 76 deletions(-) (limited to 'src') diff --git a/src/upb.c b/src/upb.c index 4cd4bba..146a9a5 100644 --- a/src/upb.c +++ b/src/upb.c @@ -32,8 +32,8 @@ upb_type_info upb_types[] = { TYPE_INFO(SFIXED64, UPB_WIRE_TYPE_64BIT, int64_t) TYPE_INFO(SINT32, UPB_WIRE_TYPE_VARINT, int32_t) TYPE_INFO(SINT64, UPB_WIRE_TYPE_VARINT, int64_t) - TYPE_INFO(STRING, UPB_WIRE_TYPE_DELIMITED, union upb_string*) - TYPE_INFO(BYTES, UPB_WIRE_TYPE_DELIMITED, union upb_string*) + TYPE_INFO(STRING, UPB_WIRE_TYPE_DELIMITED, upb_strptr) + TYPE_INFO(BYTES, UPB_WIRE_TYPE_DELIMITED, upb_strptr) }; void upb_seterr(upb_status *status, enum upb_status_code code, diff --git a/src/upb.h b/src/upb.h index d330ad4..b680d7b 100644 --- a/src/upb.h +++ b/src/upb.h @@ -110,11 +110,11 @@ typedef int32_t upb_field_number_t; typedef uint8_t upb_label_t; // A scalar (non-string) wire value. Used only for parsing unknown fields. -union upb_wire_value { +typedef union { uint64_t varint; uint64_t _64bit; uint32_t _32bit; -}; +} upb_wire_value; // A tag occurs before each value on-the-wire. typedef struct { @@ -155,7 +155,7 @@ typedef union { // A single .proto value. The owner must have an out-of-band way of knowing // the type, so that it knows which union member to use. -union upb_value { +typedef union { double _double; float _float; int32_t int32; @@ -167,11 +167,11 @@ union upb_value { upb_arrayptr arr; upb_msg *msg; upb_data *data; -}; +} upb_value; // 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_ptr { +typedef union { double *_double; float *_float; int32_t *int32; @@ -185,10 +185,10 @@ union upb_value_ptr { upb_msg **msg; upb_data **data; void *_void; -}; +} upb_valueptr; -INLINE union upb_value_ptr upb_value_addrof(union upb_value *val) { - union upb_value_ptr ptr = {&val->_double}; +INLINE upb_valueptr upb_value_addrof(upb_value *val) { + upb_valueptr ptr = {&val->_double}; return ptr; } @@ -197,9 +197,8 @@ INLINE union upb_value_ptr upb_value_addrof(union upb_value *val) { * know the field type to perform this operation, because we need to know how * much memory to copy. */ -INLINE union upb_value upb_value_read(union upb_value_ptr ptr, - upb_field_type_t ft) { - union upb_value val; +INLINE upb_value upb_value_read(upb_valueptr ptr, upb_field_type_t ft) { + upb_value val; #define CASE(t, member_name) \ case UPB_TYPE(t): val.member_name = *ptr.member_name; break; @@ -235,7 +234,7 @@ INLINE union upb_value upb_value_read(union upb_value_ptr ptr, * type to perform this operation, because we need to know how much memory to * copy. */ -INLINE void upb_value_write(union upb_value_ptr ptr, union upb_value val, +INLINE void upb_value_write(upb_valueptr ptr, upb_value val, upb_field_type_t ft) { #define CASE(t, member_name) \ case UPB_TYPE(t): *ptr.member_name = val.member_name; break; diff --git a/src/upb_data.c b/src/upb_data.c index 4008df7..8ed1646 100644 --- a/src/upb_data.c +++ b/src/upb_data.c @@ -24,7 +24,7 @@ static uint32_t round_up_to_pow2(uint32_t v) /* upb_data *******************************************************************/ -static void data_elem_unref(union upb_value_ptr p, upb_fielddef *f) { +static void data_elem_unref(upb_valueptr p, upb_fielddef *f) { if(upb_issubmsg(f)) { upb_msg_unref(*p.msg, upb_downcast_msgdef(f->def)); } else if(upb_isstring(f)) { @@ -34,7 +34,7 @@ static void data_elem_unref(union upb_value_ptr p, upb_fielddef *f) { } } -static void data_unref(union upb_value_ptr p, upb_fielddef *f) { +static void data_unref(upb_valueptr p, upb_fielddef *f) { if(upb_isarray(f)) { upb_array_unref(*p.arr, f); } else { @@ -212,7 +212,7 @@ void _upb_array_free(upb_arrayptr a, upb_fielddef *f) { if(upb_elem_ismm(f)) { for(upb_arraylen_t i = 0; i < a.refcounted->size; i++) { - union upb_value_ptr p = _upb_array_getptr(a, f, i); + upb_valueptr p = _upb_array_getptr(a, f, i); if(!*p.data) continue; data_elem_unref(p, f); } @@ -274,7 +274,7 @@ void _upb_msg_free(upb_msg *msg, upb_msgdef *md) { for(int i = 0; i < md->num_fields; i++) { upb_fielddef *f = &md->fields[i]; - union upb_value_ptr p = _upb_msg_getptr(msg, f); + upb_valueptr p = _upb_msg_getptr(msg, f); if(!upb_field_ismm(f) || !*p.data) continue; data_unref(p, f); } @@ -300,8 +300,8 @@ void upb_msg_decodestr(upb_msg *msg, upb_msgdef *md, upb_strptr str, /* upb_msgsrc ****************************************************************/ -static void _upb_msgsrc_produceval(union upb_value v, upb_fielddef *f, - upb_sink *sink, bool reverse) +static void _upb_msgsrc_produceval(upb_value v, upb_fielddef *f, upb_sink *sink, + bool reverse) { if(upb_issubmsg(f)) { upb_sink_onstart(sink, f); @@ -320,12 +320,12 @@ void upb_msgsrc_produce(upb_msg *msg, upb_msgdef *md, upb_sink *sink, for(int i = 0; i < md->num_fields; i++) { upb_fielddef *f = &md->fields[reverse ? md->num_fields - i - 1 : i]; if(!upb_msg_has(msg, f)) continue; - union upb_value v = upb_msg_get(msg, f); + upb_value v = upb_msg_get(msg, f); if(upb_isarray(f)) { upb_arrayptr arr = v.arr; upb_arraylen_t len = upb_array_len(arr); for(upb_arraylen_t j = 0; j < upb_array_len(arr); j++) { - union upb_value elem = upb_array_get(arr, f, reverse ? len - j - 1 : j); + upb_value elem = upb_array_get(arr, f, reverse ? len - j - 1 : j); _upb_msgsrc_produceval(elem, f, sink, reverse); } } else { @@ -351,9 +351,9 @@ struct upb_msgsink { /* Helper function that returns a pointer to where the next value for field "f" * should be stored, taking into account whether f is an array that may need to * be allocated or resized. */ -static union upb_value_ptr get_value_ptr(upb_msg *msg, upb_fielddef *f) +static upb_valueptr get_valueptr(upb_msg *msg, upb_fielddef *f) { - union upb_value_ptr p = _upb_msg_getptr(msg, f); + upb_valueptr p = _upb_msg_getptr(msg, f); if(upb_isarray(f)) { if(!upb_msg_has(msg, f)) { if(upb_array_isnull(*p.arr) || !upb_data_only(*p.data)) { @@ -377,11 +377,11 @@ static union upb_value_ptr get_value_ptr(upb_msg *msg, upb_fielddef *f) // TODO: implement these in terms of public interfaces. static upb_sink_status _upb_msgsink_valuecb(upb_sink *s, upb_fielddef *f, - union upb_value val) + upb_value val) { upb_msgsink *ms = (upb_msgsink*)s; upb_msg *msg = ms->top->msg; - union upb_value_ptr p = get_value_ptr(msg, f); + upb_valueptr p = get_valueptr(msg, f); upb_msg_sethas(msg, f); upb_value_write(p, val, f->type); return UPB_SINK_CONTINUE; @@ -393,7 +393,7 @@ static upb_sink_status _upb_msgsink_strcb(upb_sink *s, upb_fielddef *f, { upb_msgsink *ms = (upb_msgsink*)s; upb_msg *msg = ms->top->msg; - union upb_value_ptr p = get_value_ptr(msg, f); + upb_valueptr p = get_valueptr(msg, f); upb_msg_sethas(msg, f); if(end > upb_strlen(str)) abort(); /* TODO: support streaming. */ if(upb_string_isnull(*p.str) || !upb_data_only(*p.data)) { @@ -409,7 +409,7 @@ static upb_sink_status _upb_msgsink_startcb(upb_sink *s, upb_fielddef *f) { upb_msgsink *ms = (upb_msgsink*)s; upb_msg *oldmsg = ms->top->msg; - union upb_value_ptr p = get_value_ptr(oldmsg, f); + upb_valueptr p = get_valueptr(oldmsg, f); if(upb_isarray(f) || !upb_msg_has(oldmsg, f)) { upb_msgdef *md = upb_downcast_msgdef(f->def); diff --git a/src/upb_data.h b/src/upb_data.h index 8c050a2..2d2f402 100644 --- a/src/upb_data.h +++ b/src/upb_data.h @@ -350,13 +350,13 @@ typedef struct upb_norefcount_string upb_static_string; struct upb_norefcount_array { upb_data base; // We co-opt the refcount for the size. upb_arraylen_t len; - union upb_value_ptr elements; + upb_valueptr elements; }; struct upb_refcounted_array { upb_data base; upb_arraylen_t len; - union upb_value_ptr elements; + upb_valueptr elements; upb_arraylen_t size; }; @@ -399,10 +399,9 @@ void _upb_array_free(upb_arrayptr a, upb_fielddef *f); // INTERNAL-ONLY: // Returns a pointer to the given elem. -INLINE union upb_value_ptr _upb_array_getptr_raw(upb_arrayptr a, - upb_arraylen_t elem, - size_t type_size) { - union upb_value_ptr p; +INLINE upb_valueptr _upb_array_getptr_raw(upb_arrayptr a, upb_arraylen_t elem, + size_t type_size) { + upb_valueptr p; if(upb_data_hasflag(a.base, UPB_DATA_REFCOUNTED)) p._void = &a.refcounted->elements.uint8[elem * type_size]; else @@ -410,14 +409,13 @@ INLINE union upb_value_ptr _upb_array_getptr_raw(upb_arrayptr a, return p; } -INLINE union upb_value_ptr _upb_array_getptr(upb_arrayptr a, - upb_fielddef *f, - upb_arraylen_t elem) { +INLINE upb_valueptr _upb_array_getptr(upb_arrayptr a, upb_fielddef *f, + upb_arraylen_t elem) { return _upb_array_getptr_raw(a, elem, upb_types[f->type].size); } -INLINE union upb_value upb_array_get(upb_arrayptr a, upb_fielddef *f, - upb_arraylen_t elem) { +INLINE upb_value upb_array_get(upb_arrayptr a, upb_fielddef *f, + upb_arraylen_t elem) { assert(elem < upb_array_len(a)); return upb_value_read(_upb_array_getptr(a, f, elem), f->type); } @@ -438,15 +436,15 @@ INLINE upb_arrayptr upb_array_getref(upb_arrayptr src, int ref_flags); // must be greater than elem. If the field type is dynamic, the array will // take a ref on val and release a ref on what was previously in the array. INLINE void upb_array_set(upb_arrayptr a, upb_fielddef *f, int elem, - union upb_value val); + upb_value val); // Note that array_append will attempt to take a reference on the given value, // so to avoid a copy use append_default and get. INLINE void upb_array_append(upb_arrayptr a, upb_fielddef *f, - union upb_value val); + upb_value val); INLINE void upb_array_append_default(upb_arrayptr a, upb_fielddef *f, - union upb_value val); + upb_value val); #endif INLINE void upb_array_truncate(upb_arrayptr a) { @@ -476,8 +474,8 @@ void _upb_msg_free(upb_msg *msg, upb_msgdef *md); // INTERNAL-ONLY: // Returns a pointer to the given field. -INLINE union upb_value_ptr _upb_msg_getptr(upb_msg *msg, upb_fielddef *f) { - union upb_value_ptr p; +INLINE upb_valueptr _upb_msg_getptr(upb_msg *msg, upb_fielddef *f) { + upb_valueptr p; p._void = &msg->data[f->byte_offset]; return p; } @@ -495,7 +493,7 @@ INLINE bool upb_msg_has(upb_msg *msg, upb_fielddef *f) { // Returns the current value if set, or the default value if not set, of the // specified field. The caller does *not* own a ref. -INLINE union upb_value upb_msg_get(upb_msg *msg, upb_fielddef *f) { +INLINE upb_value upb_msg_get(upb_msg *msg, upb_fielddef *f) { if(upb_msg_has(msg, f)) { return upb_value_read(_upb_msg_getptr(msg, f), f->type); } else { @@ -505,7 +503,7 @@ INLINE union upb_value upb_msg_get(upb_msg *msg, upb_fielddef *f) { // Sets the given field to the given value. The msg will take a ref on val, // and will drop a ref on whatever was there before. -void upb_msg_set(upb_msg *msg, upb_fielddef *f, union upb_value val); +void upb_msg_set(upb_msg *msg, upb_fielddef *f, upb_value val); INLINE void upb_msg_clear(upb_msg *msg, upb_msgdef *md) { memset(msg->data, 0, md->set_flags_bytes); diff --git a/src/upb_decoder.c b/src/upb_decoder.c index 464e2b9..f07d901 100644 --- a/src/upb_decoder.c +++ b/src/upb_decoder.c @@ -181,12 +181,12 @@ T(SFIXED64, f, uint64_t, int64_t, int64) { return (int64_t)s; } T(BOOL, v, uint32_t, bool, _bool) { return (bool)s; } T(ENUM, v, uint32_t, int32_t, int32) { return (int32_t)s; } T(DOUBLE, f, uint64_t, double, _double) { - union upb_value v; + upb_value v; v.uint64 = s; return v._double; } T(FLOAT, f, uint32_t, float, _float) { - union upb_value v; + upb_value v; v.uint32 = s; return v._float; } @@ -236,8 +236,7 @@ const uint8_t *upb_get_v_uint64_t_full(const uint8_t *buf, const uint8_t *end, } const uint8_t *upb_decode_wire_value(uint8_t *buf, uint8_t *end, - upb_wire_type_t wt, - union upb_wire_value *wv, + upb_wire_type_t wt, upb_wire_value *wv, upb_status *status) { switch(wt) { @@ -253,10 +252,8 @@ const uint8_t *upb_decode_wire_value(uint8_t *buf, uint8_t *end, } } -/** - * Advances buf past the current wire value (of type wt), saving the result in - * outbuf. - */ +// Advances buf past the current wire value (of type wt), saving the result in +// outbuf. static const uint8_t *skip_wire_value(const uint8_t *buf, const uint8_t *end, upb_wire_type_t wt, upb_status *status) { @@ -278,8 +275,7 @@ static const uint8_t *skip_wire_value(const uint8_t *buf, const uint8_t *end, } static const uint8_t *upb_decode_value(const uint8_t *buf, const uint8_t *end, - upb_field_type_t ft, - union upb_value_ptr v, + upb_field_type_t ft, upb_valueptr v, upb_status *status) { #define CASE(t, member_name) \ @@ -474,7 +470,7 @@ size_t upb_decoder_decode(upb_decoder *d, upb_strptr str, upb_status *status) submsg_end = push(d, start, 0, f, status); msgdef = d->top->msgdef; } else { - union upb_value val; + upb_value val; buf = upb_decode_value(buf, end, f->type, upb_value_addrof(&val), status); CHECK_STATUS(); // Checking upb_decode_value(). diff --git a/src/upb_def.h b/src/upb_def.h index ac17a6f..25c7ff6 100644 --- a/src/upb_def.h +++ b/src/upb_def.h @@ -91,7 +91,7 @@ typedef struct _upb_fielddef { upb_label_t label; upb_field_number_t number; upb_strptr name; - union upb_value default_value; + upb_value default_value; // These are set only when this fielddef is part of a msgdef. uint32_t byte_offset; // Where in a upb_msg to find the data. diff --git a/src/upb_encoder.c b/src/upb_encoder.c index 3e60a02..9b8b213 100644 --- a/src/upb_encoder.c +++ b/src/upb_encoder.c @@ -154,12 +154,12 @@ T(SFIXED64, f, uint64_t, int64_t, int64) { return (uint64_t)s; } T(BOOL, v, uint32_t, bool, _bool) { return (uint32_t)s; } T(ENUM, v, uint32_t, int32_t, int32) { return (uint32_t)s; } T(DOUBLE, f, uint64_t, double, _double) { - union upb_value v; + upb_value v; v._double = s; return v.uint64; } T(FLOAT, f, uint32_t, float, _float) { - union upb_value v; + upb_value v; v._float = s; return v.uint32; } @@ -167,8 +167,7 @@ T(FLOAT, f, uint32_t, float, _float) { #undef PUT #undef T -uint8_t *upb_encode_value(uint8_t *buf, upb_field_type_t ft, - union upb_value v) +uint8_t *upb_encode_value(uint8_t *buf, upb_field_type_t ft, upb_value v) { #define CASE(t, member_name) \ case UPB_TYPE(t): return upb_put_ ## t(buf, v.member_name); @@ -228,7 +227,7 @@ static upb_sink_status _upb_encoder_push_buf(upb_encoder *s, const uint8_t *buf, } static upb_sink_status _upb_encoder_valuecb(upb_sink *sink, upb_fielddef *f, - union upb_value val) + upb_value val) { upb_encoder *s = (upb_encoder*)sink; uint8_t buf[UPB_ENCODER_BUFSIZE], *ptr = buf; @@ -287,7 +286,7 @@ upb_sink_callbacks _upb_encoder_sink_vtbl = { /* Public Interface ***********************************************************/ -size_t upb_get_encoded_size(union upb_value v, upb_fielddef *f) +size_t upb_get_encoded_size(upb_value v, upb_fielddef *f) { #define CASE(t, member_name) \ case UPB_TYPE(t): return upb_get_ ## t ## _size(v.member_name); diff --git a/src/upb_sink.h b/src/upb_sink.h index 5d34000..f117107 100644 --- a/src/upb_sink.h +++ b/src/upb_sink.h @@ -66,7 +66,7 @@ typedef struct { // The value callback is called for a regular value (ie. not a string or // submessage). typedef upb_sink_status (*upb_value_cb)(upb_sink *s, upb_fielddef *f, - union upb_value val); + upb_value val); // The string callback is called for string data. "str" is the string in which // the data lives, but it may contain more data than the effective string. diff --git a/src/upb_text.c b/src/upb_text.c index bef60a2..8662269 100644 --- a/src/upb_text.c +++ b/src/upb_text.c @@ -9,7 +9,7 @@ #include "upb_text.h" #include "upb_data.h" -void upb_text_printval(upb_field_type_t type, union upb_value val, FILE *file) +void upb_text_printval(upb_field_type_t type, upb_value val, FILE *file) { #define CASE(fmtstr, member) fprintf(file, fmtstr, val.member); break; switch(type) { @@ -49,7 +49,7 @@ static void print_indent(upb_text_printer *p, FILE *stream) } void upb_text_printfield(upb_text_printer *p, upb_strptr name, - upb_field_type_t valtype, union upb_value val, + upb_field_type_t valtype, upb_value val, FILE *stream) { print_indent(p, stream); @@ -76,8 +76,7 @@ void upb_text_pop(upb_text_printer *p, FILE *stream) fprintf(stream, "}\n"); } -static void printval(upb_text_printer *printer, union upb_value v, - upb_fielddef *f, +static void printval(upb_text_printer *printer, upb_value v, upb_fielddef *f, FILE *stream); static void printmsg(upb_text_printer *printer, upb_msg *msg, upb_msgdef *md, @@ -86,11 +85,11 @@ static void printmsg(upb_text_printer *printer, upb_msg *msg, upb_msgdef *md, for(upb_field_count_t i = 0; i < md->num_fields; i++) { upb_fielddef *f = &md->fields[i]; if(!upb_msg_has(msg, f)) continue; - union upb_value v = upb_msg_get(msg, f); + upb_value v = upb_msg_get(msg, f); if(upb_isarray(f)) { upb_arrayptr arr = v.arr; for(uint32_t j = 0; j < upb_array_len(arr); j++) { - union upb_value elem = upb_array_get(arr, f, j); + upb_value elem = upb_array_get(arr, f, j); printval(printer, elem, f, stream); } } else { @@ -99,8 +98,7 @@ static void printmsg(upb_text_printer *printer, upb_msg *msg, upb_msgdef *md, } } -static void printval(upb_text_printer *printer, union upb_value v, - upb_fielddef *f, +static void printval(upb_text_printer *printer, upb_value v, upb_fielddef *f, FILE *stream) { if(upb_issubmsg(f)) { diff --git a/src/upb_text.h b/src/upb_text.h index 86d69a2..d89c9d6 100644 --- a/src/upb_text.h +++ b/src/upb_text.h @@ -22,10 +22,9 @@ INLINE void upb_text_printer_init(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 p, FILE *file); +void upb_text_printval(upb_field_type_t type, upb_value p, FILE *file); void upb_text_printfield(upb_text_printer *p, upb_strptr name, - upb_field_type_t valtype, union upb_value val, - FILE *stream); + upb_field_type_t valtype, upb_value val, FILE *stream); void upb_text_push(upb_text_printer *p, upb_strptr submsg_type, FILE *stream); void upb_text_pop(upb_text_printer *p, FILE *stream); -- cgit v1.2.3