From cea737028f7d34e6415c0de5d34587245624db2b Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Wed, 7 Mar 2018 16:30:21 -0800 Subject: Added google/protobuf/descriptor.upb.* to core. This is in preparation for making upb_def capable of parsing binary descriptors directly. We leave upb/descriptor/descriptor.upbdefs.* in place for now, because upb/descriptor/reader.* still depends on it. Also removed a bit of cruft from the codegen. --- tools/amalgamate.py | 3 ++- tools/dump_cinit.lua | 53 ---------------------------------------------------- tools/make_c_api.lua | 37 ++++++++++++++++++------------------ tools/upbc.lua | 40 +++++++++++++++++++-------------------- 4 files changed, 41 insertions(+), 92 deletions(-) (limited to 'tools') diff --git a/tools/amalgamate.py b/tools/amalgamate.py index 6fd8033..b496f24 100755 --- a/tools/amalgamate.py +++ b/tools/amalgamate.py @@ -24,7 +24,8 @@ class Amalgamator: def _process_file(self, infile_name, outfile): for line in open(infile_name): include = parse_include(line) - if include is not None and include.startswith("upb"): + if include is not None and (include.startswith("upb") or + include.startswith("google")): if include not in self.included: self.included.add(include) self._add_header(self.include_path + include) diff --git a/tools/dump_cinit.lua b/tools/dump_cinit.lua index 6135b03..56c47ea 100644 --- a/tools/dump_cinit.lua +++ b/tools/dump_cinit.lua @@ -583,51 +583,6 @@ local function make_children_map(file) return map end -local function dump_enum_vals(enumdef, append) - local enum_vals = {} - - for k, v in enumdef:values() do - enum_vals[#enum_vals + 1] = {k, v} - end - - table.sort(enum_vals, function(a, b) return a[2] < b[2] end) - - -- protobuf convention is that enum values are scoped at the level of the - -- enum itself, to follow C++. Ie, if you have the enum: - -- message Foo { - -- enum E { - -- VAL1 = 1; - -- VAL2 = 2; - -- } - -- } - -- - -- The name of VAL1 is Foo.VAL1, not Foo.E.VAL1. - -- - -- This seems a bit sketchy, but people often name their enum values - -- accordingly, ie: - -- - -- enum Foo { - -- FOO_VAL1 = 1; - -- FOO_VAL2 = 2; - -- } - -- - -- So if we don't respect this also, we end up with constants that look like: - -- - -- GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_TYPE_DOUBLE = 1 - -- - -- (notice the duplicated "TYPE"). - local cident = to_cident(remove_name(enumdef:full_name())) - for i, pair in ipairs(enum_vals) do - k, v = pair[1], pair[2] - append(' %s = %d', cident .. "_" .. k, v) - if i == #enum_vals then - append('\n') - else - append(',\n') - end - end -end - local print_classes local function print_message(def, map, indent, append) @@ -699,14 +654,6 @@ local function dump_defs_h(file, append, linktab) -- Dump C enums for proto enums. - append("/* Enums */\n\n") - for _, def in ipairs(sorted_defs(file:defs(upb.DEF_ENUM))) do - local cident = to_cident(def:full_name()) - append('typedef enum {\n') - dump_enum_vals(def, append) - append('} %s;\n\n', cident) - end - append("/* MessageDefs: call these functions to get a ref to a msgdef. */\n") dump_defs_for_type( "const upb_msgdef *upbdefs_%s_get(const void *owner);\n", diff --git a/tools/make_c_api.lua b/tools/make_c_api.lua index d81ca6b..1770a88 100644 --- a/tools/make_c_api.lua +++ b/tools/make_c_api.lua @@ -117,14 +117,20 @@ local function field_default(field) end end -local function ctype(field) +local function ctype(field, const) + if const then + const = "const " + else + const = "" + end + if field:label() == upb.LABEL_REPEATED then - return "upb_array*" + return const .. "upb_array*" elseif field:type() == upb.TYPE_MESSAGE then if field:containing_type():file() == field:subdef():file() then - return to_cident(field:subdef():full_name()) .. "*" + return const .. to_cident(field:subdef():full_name()) .. "*" else - return "struct " .. to_cident(field:subdef():full_name()) .. "*" + return const .. "struct " .. to_cident(field:subdef():full_name()) .. "*" end elseif field:type() == upb.TYPE_ENUM then return to_cident(field:subdef():full_name()) @@ -216,7 +222,7 @@ local function write_h_file(filedef, append) for msg in filedef:defs(upb.DEF_MSG) do local msgname = to_cident(msg:full_name()) - append('/* %s message definition. */\n', msgname) + append('/* %s */\n', msgname) append('extern const upb_msglayout_msginit_v1 %s_msginit;\n', msgname) append('%s *%s_new(upb_env *env);\n', msgname, msgname) append('%s *%s_parsenew(upb_stringview buf, upb_env *env);\n', @@ -226,7 +232,7 @@ local function write_h_file(filedef, append) append('void %s_free(%s *msg, upb_env *env);\n', msgname, msgname) append('\n') - append('/* %s getters. */\n', msgname) + append('/* getters. */\n') local setters, get_setters = dump_cinit.str_appender() for field in msg:fields() do local fieldname = to_cident(field:name()) @@ -235,14 +241,10 @@ local function write_h_file(filedef, append) -- Forward declaration for message type declared in another file. append('struct %s;\n', to_cident(field:subdef():full_name())) end - if field:label() == upb.LABEL_REPEATED then - else - local typename = ctype(field) - append('%s %s_%s(const %s *msg);\n', - typename, msgname, fieldname, msgname) - setters('void %s_set_%s(%s *msg, %s value);\n', - msgname, fieldname, msgname, typename) - end + append('%s %s_%s(const %s *msg);\n', + ctype(field, true), msgname, fieldname, msgname) + setters('void %s_set_%s(%s *msg, %s value);\n', + msgname, fieldname, msgname, ctype(field)) end for oneof in msg:oneofs() do @@ -257,7 +259,7 @@ local function write_h_file(filedef, append) end append('\n') - append('/* %s setters. */\n', msgname) + append('/* setters. */\n') append(get_setters()) append('\n') @@ -473,9 +475,8 @@ local function write_c_file(filedef, hfilename, append) append('}\n') for field in msg:fields() do - local typename = ctype(field) append('%s %s_%s(const %s *msg) {\n', - typename, msgname, field:name(), msgname); + ctype(field, true), msgname, field:name(), msgname); if field:containing_oneof() then local oneof = field:containing_oneof() append(' return msg->%s_case == %s ? msg->%s.%s : %s;\n', @@ -486,7 +487,7 @@ local function write_c_file(filedef, hfilename, append) end append('}\n') append('void %s_set_%s(%s *msg, %s value) {\n', - msgname, field:name(), msgname, typename); + msgname, field:name(), msgname, ctype(field)); if field:containing_oneof() then local oneof = field:containing_oneof() append(' msg->%s.%s = value;\n', oneof:name(), field:name()) diff --git a/tools/upbc.lua b/tools/upbc.lua index a538b1c..8ac4f73 100644 --- a/tools/upbc.lua +++ b/tools/upbc.lua @@ -77,28 +77,28 @@ for _, file in ipairs(files) do hfile:close() cfile:close() - end - - -- Write C API. - hfilename = outbase .. ".upb.h" - cfilename = outbase .. ".upb.c" - - if os.getenv("UPBC_VERBOSE") then - print("upbc:") - print(string.format(" source file=%s", src)) - print(string.format(" output file base=%s", outbase)) - print(string.format(" hfilename=%s", hfilename)) - print(string.format(" cfilename=%s", cfilename)) - end + else + -- Write C API. + hfilename = outbase .. ".upb.h" + cfilename = outbase .. ".upb.c" + + if os.getenv("UPBC_VERBOSE") then + print("upbc:") + print(string.format(" source file=%s", src)) + print(string.format(" output file base=%s", outbase)) + print(string.format(" hfilename=%s", hfilename)) + print(string.format(" cfilename=%s", cfilename)) + end - local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename) - local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename) + local hfile = assert(io.open(hfilename, "w"), "couldn't open " .. hfilename) + local cfile = assert(io.open(cfilename, "w"), "couldn't open " .. cfilename) - local happend = dump_cinit.file_appender(hfile) - local cappend = dump_cinit.file_appender(cfile) + local happend = dump_cinit.file_appender(hfile) + local cappend = dump_cinit.file_appender(cfile) - make_c_api.write_gencode(file, hfilename, happend, cappend) + make_c_api.write_gencode(file, hfilename, happend, cappend) - hfile:close() - cfile:close() + hfile:close() + cfile:close() + end end -- cgit v1.2.3