From 919fea438a5ac5366684cfa26d2bb3d17519cb60 Mon Sep 17 00:00:00 2001 From: Josh Haberman Date: Mon, 18 May 2015 10:55:20 -0700 Subject: Ported upb to C89, for greater portability. A large part of this change contains surface-level porting, like moving variable declarations to the top of the block. However there are a few more substantial things too: - moved internal-only struct definitions to a separate file (structdefs.int.h), for greater encapsulation and ABI compatibility. - removed the UPB_UPCAST macro, since it requires access to the internal-only struct definitions. Replaced uses with calls to inline, type-safe casting functions. - removed the UPB_DEFINE_CLASS/UPB_DEFINE_STRUCT macros. Class and struct definitions are now more explicit -- you get to see the actual class/struct keywords in the source. The casting convenience functions have been moved into UPB_DECLARE_DERIVED_TYPE() and UPB_DECLARE_DERIVED_TYPE2(). - the new way that we duplicate base methods in derived types is also more convenient and requires less duplication. It is also less greppable, but hopefully that is not too big a problem. Compiler flags (-std=c89 -pedantic) should help to rigorously enforce that the code is free of C99-isms. A few functions are not available in C89 (strtoll). There are temporary, hacky solutions in place. --- tools/dump_cinit.lua | 78 +++++++++++++++++++++++++++------------------------- 1 file changed, 41 insertions(+), 37 deletions(-) (limited to 'tools/dump_cinit.lua') diff --git a/tools/dump_cinit.lua b/tools/dump_cinit.lua index 3742abd..048cb3a 100644 --- a/tools/dump_cinit.lua +++ b/tools/dump_cinit.lua @@ -205,25 +205,25 @@ function Dumper:new(linktab) return obj end --- Dumps a _upb_value, eg: --- UPB_VALUE_INIT_INT32(5) +-- Dumps a upb_tabval, eg: +-- UPB_TABVALUE_INIT(5) function Dumper:_value(val, upbtype) if type(val) == "nil" then - return "UPB__VALUE_INIT_NONE" + return "UPB_TABVALUE_EMPTY_INIT" elseif type(val) == "number" then -- Use upbtype to disambiguate what kind of number it is. if upbtype == upbtable.CTYPE_INT32 then - return string.format("UPB_VALUE_INIT_INT32(%d)", val) + return string.format("UPB_TABVALUE_INT_INIT(%d)", val) else -- TODO(haberman): add support for these so we can properly support -- default values. error("Unsupported number type " .. upbtype) end elseif type(val) == "string" then - return string.format('UPB_VALUE_INIT_CONSTPTR("%s")', val) + return string.format('UPB_TABVALUE_PTR_INIT("%s")', val) else -- We take this as an object reference that has an entry in the link table. - return string.format("UPB_VALUE_INIT_CONSTPTR(%s)", self.linktab:addr(val)) + return string.format("UPB_TABVALUE_PTR_INIT(%s)", self.linktab:addr(val)) end end @@ -258,7 +258,7 @@ function Dumper:arrayval(val) if val.val then return string.format(" %s,\n", self:_value(val.val, val.valtype)) else - return " UPB_ARRAY_EMPTYENT,\n" + return " UPB_TABVALUE_EMPTY_INIT,\n" end end @@ -296,9 +296,9 @@ local function gettables(def) end local function emit_file_warning(append) - append('// This file was generated by upbc (the upb compiler).\n') - append('// Do not edit -- your changes will be discarded when the file is\n') - append('// regenerated.\n\n') + append('/* This file was generated by upbc (the upb compiler).\n') + append(' * Do not edit -- your changes will be discarded when the file is\n') + append(' * regenerated. */\n\n') end local function join(...) @@ -356,7 +356,7 @@ end local function end_namespace(package, append) local package_components = split(package) for i=#package_components,1,-1 do - append("} // namespace %s\n", package_components[i]) + append("} /* namespace %s */\n", package_components[i]) end end @@ -426,6 +426,7 @@ local function dump_defs_c(symtab, basename, namespace, append) -- Emit forward declarations. emit_file_warning(append) append('#include "upb/def.h"\n') + append('#include "upb/structdefs.int.h"\n') append('#include "upb/symtab.h"\n\n') append("static const upb_msgdef %s;\n", linktab:cdecl(upb.DEF_MSG)) append("static const upb_fielddef %s;\n", linktab:cdecl(upb.DEF_FIELD)) @@ -436,7 +437,7 @@ local function dump_defs_c(symtab, basename, namespace, append) if not linktab:empty("intentries") then append("static const upb_tabent %s;\n", linktab:cdecl("intentries")) end - append("static const _upb_value %s;\n", linktab:cdecl("arrays")) + append("static const upb_tabval %s;\n", linktab:cdecl("arrays")) append("\n") append("#ifdef UPB_DEBUG_REFS\n") append("static upb_inttable reftables[%d];\n", reftable_count) @@ -456,8 +457,8 @@ local function dump_defs_c(symtab, basename, namespace, append) append(' UPB_MSGDEF_INIT("%s", %d, %d, %s, %s,' .. '&reftables[%d], &reftables[%d]),\n', m:full_name(), - m:_selector_count(), - m:_submsg_field_count(), + upbtable.msgdef_selector_count(m), + upbtable.msgdef_submsg_field_count(m), dumper:inttable(tables.int), dumper:strtable(tables.str), reftable, reftable + 1) @@ -469,7 +470,7 @@ local function dump_defs_c(symtab, basename, namespace, append) for f in linktab:objs(upb.DEF_FIELD) do local subdef = "NULL" if f:has_subdef() then - subdef = string.format("UPB_UPCAST(%s)", linktab:addr(f:subdef())) + subdef = string.format("(const upb_def*)(%s)", linktab:addr(f:subdef())) end local intfmt if f:type() == upb.TYPE_UINT32 or @@ -490,7 +491,7 @@ local function dump_defs_c(symtab, basename, namespace, append) boolstr(f:istagdelim()), boolstr(f:is_extension()), boolstr(f:lazy()), boolstr(f:packed()), f:name(), f:number(), linktab:addr(f:containing_type()), subdef, - f:_selector_base(), f:index(), + upbtable.fielddef_selector_base(f), f:index(), reftable, reftable + 1 ) reftable = reftable + 2 @@ -529,7 +530,7 @@ local function dump_defs_c(symtab, basename, namespace, append) append("};\n\n"); end - append("static const _upb_value %s = {\n", linktab:cdecl("arrays")) + append("static const upb_tabval %s = {\n", linktab:cdecl("arrays")) for ent in linktab:objs("arrays") do append(dumper:arrayval(ent)) end @@ -602,9 +603,14 @@ local function dump_enum_vals(enumdef, append) -- -- (notice the duplicated "TYPE"). local cident = to_cident(getpackage(enumdef:full_name())) - for _, pair in ipairs(enum_vals) do + for i, pair in ipairs(enum_vals) do k, v = pair[1], pair[2] - append(' %s = %d,\n', to_preproc(cident, k), v) + append(' %s = %d', to_preproc(cident, k), v) + if i == #enum_vals then + append('\n') + else + append(',\n') + end end end @@ -644,7 +650,7 @@ local function dump_selectors(msgdef, append, base) -- -- // So instead we make the latter the very beautiful: -- SEL_GOOGLE_PROTOBUF_FIELDDESCRIPTORPROTO_TYPE_INT32 - append("// %s\n", msgdef:full_name()) + append("/* %s */\n", msgdef:full_name()) local cident = to_cident(msgdef:full_name()) for _, pair in ipairs(selectors) do k, v = pair[1], pair[2] @@ -655,19 +661,19 @@ end local function dump_defs_h(symtab, basename, namespace, append, linktab) local basename_preproc = to_preproc(basename) - append("// This file contains accessors for a set of compiled-in defs.\n") - append("// Note that unlike Google's protobuf, it does *not* define\n") - append("// generated classes or any other kind of data structure for\n") - append("// actually storing protobufs. It only contains *defs* which\n") - append("// let you reflect over a protobuf *schema*.\n") - append("//\n") + append("/* This file contains accessors for a set of compiled-in defs.\n") + append(" * Note that unlike Google's protobuf, it does *not* define\n") + append(" * generated classes or any other kind of data structure for\n") + append(" * actually storing protobufs. It only contains *defs* which\n") + append(" * let you reflect over a protobuf *schema*.\n") + append(" */\n") emit_file_warning(append) append('#ifndef %s_UPB_H_\n', basename_preproc) append('#define %s_UPB_H_\n\n', basename_preproc) append('#include "upb/def.h"\n') append('#include "upb/symtab.h"\n\n') append('#ifdef __cplusplus\n') - append('extern "C" {\n') + append('UPB_BEGIN_EXTERN_C\n') append('#endif\n\n') local packages = {} @@ -681,7 +687,7 @@ local function dump_defs_h(symtab, basename, namespace, append, linktab) -- Dump C enums for proto enums. - append("// Enums\n\n") + append("/* Enums */\n\n") for _, def in ipairs(sorted_defs(symtab:defs(upb.DEF_ENUM))) do local cident = to_cident(def:full_name()) append('typedef enum {\n') @@ -690,7 +696,7 @@ local function dump_defs_h(symtab, basename, namespace, append, linktab) end -- selectors - append("// Selectors\n\n") + append("/* Selectors */\n\n") for _, def in ipairs(sorted_defs(symtab:defs(upb.DEF_MSG))) do dump_selectors(def, append, upb) end @@ -698,7 +704,7 @@ local function dump_defs_h(symtab, basename, namespace, append, linktab) append("const upb_symtab *%s_%s(const void *owner);" .. "\n\n", namespace, to_cident(basename)) - append("// MessageDefs\n") + append("/* MessageDefs */\n") dump_defs_for_type( "UPB_INLINE const upb_msgdef *%s_%s(const upb_symtab *s) {\n" .. " const upb_msgdef *m = upb_symtab_lookupmsg(s, \"%s\");\n" .. @@ -710,7 +716,7 @@ local function dump_defs_h(symtab, basename, namespace, append, linktab) append("\n") - append("// EnumDefs\n") + append("/* EnumDefs */\n") dump_defs_for_type( "UPB_INLINE const upb_enumdef *%s_%s(const upb_symtab *s) {\n" .. " const upb_enumdef *e = upb_symtab_lookupenum(s, \"%s\");\n" .. @@ -739,9 +745,7 @@ local function dump_defs_h(symtab, basename, namespace, append, linktab) end append("\n") - append('#ifdef __cplusplus\n') - append('}; // extern "C"\n') - append('#endif\n\n') + append('UPB_END_EXTERN_C\n\n') append("#ifdef __cplusplus\n\n") append("namespace %s {\n", namespace) @@ -794,12 +798,12 @@ local function dump_defs_h(symtab, basename, namespace, append, linktab) append("\n") end - append("} // namespace %s\n\n\n", namespace) + append("} /* namespace %s */\n\n\n", namespace) append("#undef RETURN_REFFED\n") - append("#endif // __cplusplus\n\n") + append("#endif /* __cplusplus */\n\n") - append('#endif // %s_UPB_H_\n', basename_preproc) + append('#endif /* %s_UPB_H_ */\n', basename_preproc) end function export.dump_defs(symtab, basename, append_h, append_c) -- cgit v1.2.3