Merge pull request #145 from haberman/arrayapi

Generated code API changes for array and hazzers.
pull/13171/head
Joshua Haberman 6 years ago committed by GitHub
commit 2b62fbce67
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 170
      BUILD
  2. 3
      CMakeLists.txt
  3. 37
      build_defs.bzl
  4. 126
      google/protobuf/descriptor.upb.c
  5. 1349
      google/protobuf/descriptor.upb.h
  6. 18
      tests/conformance_upb.c
  7. 2
      tests/json/test.upbdefs.c
  8. 4
      tools/amalgamate.py
  9. 1
      tools/dump_cinit.lua
  10. 8
      tools/make_cmakelists.py
  11. 2
      upb/bindings/lua/upb/pb.c
  12. 1
      upb/bindings/lua/upb/table.c
  13. 14
      upb/decode.c
  14. 2
      upb/decode.h
  15. 5
      upb/def.c
  16. 153
      upb/def.h
  17. 2
      upb/descriptor/descriptor.upbdefs.c
  18. 31
      upb/descriptor/reader.c
  19. 6
      upb/encode.c
  20. 102
      upb/generated_util.h
  21. 2
      upb/handlers-inl.h
  22. 1
      upb/handlers.c
  23. 2
      upb/msg.c
  24. 20
      upb/msg.h
  25. 2
      upb/msgfactory.c
  26. 6
      upb/msgfactory.h
  27. 15
      upb/pb/decoder.int.h
  28. 196
      upb/structdefs.int.h
  29. 3
      upb/structs.int.h
  30. 209
      upbc/generator.cc
  31. 16
      upbc/message_layout.cc
  32. 5
      upbc/message_layout.h

170
BUILD

@ -1,15 +1,32 @@
load( load(
":build_defs.bzl", ":build_defs.bzl",
"licenses", # copybara:strip_for_google3
"lua_cclibrary", "lua_cclibrary",
"lua_library", "lua_library",
"lua_binary", "lua_binary",
"lua_test", "lua_test",
"generated_file_staleness_test", "generated_file_staleness_test",
"make_shell_script", "make_shell_script",
"map_dep",
"upb_amalgamation", "upb_amalgamation",
"upb_proto_library", "upb_proto_library",
) )
licenses(["notice"]) # BSD (Google-authored w/ possible external contributions)
exports_files([
"LICENSE",
"build_defs",
])
COPTS = [
# copybara:strip_for_google3_begin
"-std=c89",
"-pedantic",
"-Wno-long-long",
# copybara:strip_end
]
# C/C++ rules ################################################################## # C/C++ rules ##################################################################
cc_library( cc_library(
@ -27,7 +44,6 @@ cc_library(
"upb/port_undef.inc", "upb/port_undef.inc",
"upb/refcounted.c", "upb/refcounted.c",
"upb/sink.c", "upb/sink.c",
"upb/structdefs.int.h",
"upb/structs.int.h", "upb/structs.int.h",
"upb/table.c", "upb/table.c",
"upb/table.int.h", "upb/table.int.h",
@ -38,6 +54,7 @@ cc_library(
"upb/decode.h", "upb/decode.h",
"upb/def.h", "upb/def.h",
"upb/encode.h", "upb/encode.h",
"upb/generated_util.h",
"upb/handlers.h", "upb/handlers.h",
"upb/msg.h", "upb/msg.h",
"upb/msgfactory.h", "upb/msgfactory.h",
@ -45,11 +62,7 @@ cc_library(
"upb/sink.h", "upb/sink.h",
"upb/upb.h", "upb/upb.h",
], ],
copts = [ copts = COPTS,
"-std=c89",
"-pedantic",
"-Wno-long-long",
],
visibility = ["//visibility:public"], visibility = ["//visibility:public"],
) )
@ -63,11 +76,7 @@ cc_library(
"upb/descriptor/descriptor.upbdefs.h", "upb/descriptor/descriptor.upbdefs.h",
"upb/descriptor/reader.h", "upb/descriptor/reader.h",
], ],
copts = [ copts = COPTS,
"-std=c89",
"-pedantic",
"-Wno-long-long",
],
deps = [":upb"], deps = [":upb"],
) )
@ -82,6 +91,7 @@ cc_library(
"upb/pb/textprinter.c", "upb/pb/textprinter.c",
"upb/pb/varint.c", "upb/pb/varint.c",
"upb/pb/varint.int.h", "upb/pb/varint.int.h",
"upb/table.int.h",
], ],
hdrs = [ hdrs = [
"upb/pb/decoder.h", "upb/pb/decoder.h",
@ -89,11 +99,7 @@ cc_library(
"upb/pb/glue.h", "upb/pb/glue.h",
"upb/pb/textprinter.h", "upb/pb/textprinter.h",
], ],
copts = [ copts = COPTS,
"-std=c89",
"-pedantic",
"-Wno-long-long",
],
deps = [ deps = [
":upb", ":upb",
":upb_descriptor", ":upb_descriptor",
@ -110,11 +116,7 @@ cc_library(
"upb/json/parser.h", "upb/json/parser.h",
"upb/json/printer.h", "upb/json/printer.h",
], ],
copts = [ copts = COPTS,
"-std=c89",
"-pedantic",
"-Wno-long-long",
],
deps = [ deps = [
":upb", ":upb",
":upb_pb", ":upb_pb",
@ -129,32 +131,44 @@ cc_library(
deps = [":upb"], deps = [":upb"],
) )
# Amalgamation ################################################################# # upb compiler #################################################################
py_binary( cc_library(
name = "amalgamate", name = "upbc_generator",
srcs = ["tools/amalgamate.py"], srcs = [
"upbc/generator.cc",
"upbc/message_layout.cc",
"upbc/message_layout.h",
],
hdrs = ["upbc/generator.h"],
deps = [
map_dep("@absl//absl/base:core_headers"),
map_dep("@absl//absl/strings"),
map_dep("@com_google_protobuf//:protobuf"),
map_dep("@com_google_protobuf//:protoc_lib"),
],
) )
upb_amalgamation( cc_binary(
name = "gen_amalgamation", name = "protoc-gen-upb",
outs = [ srcs = ["upbc/main.cc"],
"upb.c", deps = [
"upb.h", ":upbc_generator",
], map_dep("@com_google_protobuf//:protoc_lib"),
amalgamator = ":amalgamate",
libs = [
":upb",
":upb_descriptor",
":upb_pb",
":upb_json",
], ],
) )
cc_library( # We strip the tests and remaining rules from google3 until the upb_proto_library()
name = "amalgamation", # and upb_proto_reflection_library() rules are fixed.
srcs = ["upb.c"],
hdrs = ["upb.h"], # copybara:strip_for_google3_begin
lua_binary(
name = "lua_upbc",
luadeps = [
"lua/upbc_lib",
],
luamain = "tools/upbc.lua",
) )
# C/C++ tests ################################################################## # C/C++ tests ##################################################################
@ -265,11 +279,11 @@ cc_binary(
srcs = [ srcs = [
"tests/conformance_upb.c", "tests/conformance_upb.c",
], ],
copts = ["-Ibazel-out/k8-fastbuild/bin"],
deps = [ deps = [
":conformance_proto_upb", ":conformance_proto_upb",
":upb", ":upb",
], ],
copts = ["-Ibazel-out/k8-fastbuild/bin"],
) )
make_shell_script( make_shell_script(
@ -289,6 +303,35 @@ sh_test(
], ],
) )
# Amalgamation #################################################################
py_binary(
name = "amalgamate",
srcs = ["tools/amalgamate.py"],
)
upb_amalgamation(
name = "gen_amalgamation",
outs = [
"upb.c",
"upb.h",
],
amalgamator = ":amalgamate",
libs = [
":upb",
":upb_descriptor",
":upb_pb",
":upb_json",
],
)
cc_library(
name = "amalgamation",
srcs = ["upb.c"],
hdrs = ["upb.h"],
copts = COPTS,
)
# Lua libraries. ############################################################### # Lua libraries. ###############################################################
lua_cclibrary( lua_cclibrary(
@ -374,42 +417,12 @@ lua_test(
luamain = "tests/bindings/lua/test_upb.pb.lua", luamain = "tests/bindings/lua/test_upb.pb.lua",
) )
# upb compiler #################################################################
lua_binary(
name = "lua_upbc",
luadeps = [
"lua/upbc_lib",
],
luamain = "tools/upbc.lua",
)
cc_library(
name = "upbc_generator",
hdrs = ["upbc/generator.h"],
srcs = ["upbc/generator.cc", "upbc/message_layout.h", "upbc/message_layout.cc"],
deps = [
"@com_google_protobuf//:protobuf",
"@com_google_protobuf//:protoc_lib",
"@absl//absl/strings",
],
)
cc_binary(
name = "protoc-gen-upb",
srcs = ["upbc/main.cc"],
deps = [
":upbc_generator",
"@com_google_protobuf//:protoc_lib",
],
)
# Test the CMake build ######################################################### # Test the CMake build #########################################################
make_shell_script( make_shell_script(
name = "gen_run_cmake_build", name = "gen_run_cmake_build",
out = "run_cmake_build.sh", out = "run_cmake_build.sh",
contents = "mkdir build && cd build && cmake .. && make -j8 && make test" contents = "mkdir build && cd build && cmake .. && make -j8 && make test",
) )
sh_test( sh_test(
@ -463,10 +476,13 @@ py_binary(
genrule( genrule(
name = "gen_cmakelists", name = "gen_cmakelists",
srcs = [
"BUILD",
"WORKSPACE",
],
outs = ["generated/CMakeLists.txt"], outs = ["generated/CMakeLists.txt"],
srcs = ["BUILD", "WORKSPACE"], cmd = "$(location :make_cmakelists) $@",
tools = [":make_cmakelists"], tools = [":make_cmakelists"],
cmd = "$(location :make_cmakelists) $@"
) )
genrule( genrule(
@ -510,8 +526,8 @@ genrule(
], ],
cmd = "$(location @com_google_protobuf//:protoc) $< --upb_out=$(GENDIR)/generated --plugin=protoc-gen-upb=$(location :protoc-gen-upb)", cmd = "$(location @com_google_protobuf//:protoc) $< --upb_out=$(GENDIR)/generated --plugin=protoc-gen-upb=$(location :protoc-gen-upb)",
tools = [ tools = [
":protoc-gen-upb",
"@com_google_protobuf//:protoc", "@com_google_protobuf//:protoc",
":protoc-gen-upb"
], ],
) )
@ -563,3 +579,5 @@ generated_file_staleness_test(
], ],
generated_pattern = "generated/%s", generated_pattern = "generated/%s",
) )
# copybara:strip_end

@ -72,7 +72,6 @@ add_library(upb
upb/port_undef.inc upb/port_undef.inc
upb/refcounted.c upb/refcounted.c
upb/sink.c upb/sink.c
upb/structdefs.int.h
upb/structs.int.h upb/structs.int.h
upb/table.c upb/table.c
upb/table.int.h upb/table.int.h
@ -81,6 +80,7 @@ add_library(upb
upb/decode.h upb/decode.h
upb/def.h upb/def.h
upb/encode.h upb/encode.h
upb/generated_util.h
upb/handlers.h upb/handlers.h
upb/msg.h upb/msg.h
upb/msgfactory.h upb/msgfactory.h
@ -103,6 +103,7 @@ add_library(upb_pb
upb/pb/textprinter.c upb/pb/textprinter.c
upb/pb/varint.c upb/pb/varint.c
upb/pb/varint.int.h upb/pb/varint.int.h
upb/table.int.h
upb/pb/decoder.h upb/pb/decoder.h
upb/pb/encoder.h upb/pb/encoder.h
upb/pb/glue.h upb/pb/glue.h

@ -1,4 +1,3 @@
_shell_find_runfiles = """ _shell_find_runfiles = """
# --- begin runfiles.bash initialization --- # --- begin runfiles.bash initialization ---
# Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash). # Copy-pasted from Bazel's Bash runfiles library (tools/bash/runfiles/runfiles.bash).
@ -100,7 +99,7 @@ BASE=$(dirname $(rlocation upb/upb_c.so))
export LUA_CPATH="$BASE/?.so" export LUA_CPATH="$BASE/?.so"
export LUA_PATH="$BASE/?.lua" export LUA_PATH="$BASE/?.lua"
$(rlocation lua/lua) $(rlocation upb/tools/upbc.lua) "$@" $(rlocation lua/lua) $(rlocation upb/tools/upbc.lua) "$@"
""" """,
) )
rule( rule(
@ -109,10 +108,10 @@ $(rlocation lua/lua) $(rlocation upb/tools/upbc.lua) "$@"
data = ["@lua//:lua", "@bazel_tools//tools/bash/runfiles", luamain] + luadeps, data = ["@lua//:lua", "@bazel_tools//tools/bash/runfiles", luamain] + luadeps,
) )
def lua_binary(name, luamain, luadeps=[]): def lua_binary(name, luamain, luadeps = []):
_lua_binary_or_test(name, luamain, luadeps, native.sh_binary) _lua_binary_or_test(name, luamain, luadeps, native.sh_binary)
def lua_test(name, luamain, luadeps=[]): def lua_test(name, luamain, luadeps = []):
_lua_binary_or_test(name, luamain, luadeps, native.sh_test) _lua_binary_or_test(name, luamain, luadeps, native.sh_test)
def generated_file_staleness_test(name, outs, generated_pattern): def generated_file_staleness_test(name, outs, generated_pattern):
@ -163,9 +162,9 @@ def generated_file_staleness_test(name, outs, generated_pattern):
SrcList = provider( SrcList = provider(
fields = { fields = {
'srcs' : 'list of srcs', "srcs": "list of srcs",
'hdrs' : 'list of hdrs', "hdrs": "list of hdrs",
} },
) )
def _file_list_aspect_impl(target, ctx): def _file_list_aspect_impl(target, ctx):
@ -205,9 +204,25 @@ upb_amalgamation = rule(
), ),
"libs": attr.label_list(aspects = [_file_list_aspect]), "libs": attr.label_list(aspects = [_file_list_aspect]),
"outs": attr.output_list(), "outs": attr.output_list(),
} },
) )
is_bazel = not hasattr(native, "genmpm")
google3_dep_map = {
"@absl//absl/base:core_headers": "//third_party/absl/base:core_headers",
"@absl//absl/strings": "//third_party/absl/strings",
"@com_google_protobuf//:protoc": "//third_party/protobuf:protoc",
"@com_google_protobuf//:protobuf": "//third_party/protobuf:protobuf",
"@com_google_protobuf//:protoc_lib": "//third_party/protobuf:libprotoc",
}
def map_dep(dep):
if is_bazel:
return dep
else:
return google3_dep_map[dep]
# upb_proto_library() rule # upb_proto_library() rule
def _remove_up(string): def _remove_up(string):
@ -223,7 +238,7 @@ def _upb_proto_library_srcs_impl(ctx):
outs = [] outs = []
include_dirs = {} include_dirs = {}
for dep in ctx.attr.deps: for dep in ctx.attr.deps:
if hasattr(dep, 'proto'): if hasattr(dep, "proto"):
for src in dep.proto.transitive_sources: for src in dep.proto.transitive_sources:
sources.append(src) sources.append(src)
include_dirs[_remove_suffix(src.path, _remove_up(src.short_path) + "." + src.extension)] = True include_dirs[_remove_suffix(src.path, _remove_up(src.short_path) + "." + src.extension)] = True
@ -273,3 +288,7 @@ def upb_proto_library(name, deps, upbc):
deps = [":upb"], deps = [":upb"],
copts = ["-Ibazel-out/k8-fastbuild/bin"], copts = ["-Ibazel-out/k8-fastbuild/bin"],
) )
def licenses(*args):
# No-op (for Google-internal usage).
pass

@ -36,24 +36,24 @@ static const upb_msglayout *const google_protobuf_FileDescriptorProto_submsgs[6]
}; };
static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = { static const upb_msglayout_field google_protobuf_FileDescriptorProto__fields[12] = {
{1, UPB_SIZE(8, 16), 1, 0, 9, 1}, {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
{2, UPB_SIZE(16, 32), 2, 0, 9, 1}, {2, UPB_SIZE(12, 24), 2, 0, 9, 1},
{3, UPB_SIZE(40, 80), 0, 0, 9, 3}, {3, UPB_SIZE(36, 72), 0, 0, 9, 3},
{4, UPB_SIZE(44, 88), 0, 0, 11, 3}, {4, UPB_SIZE(40, 80), 0, 0, 11, 3},
{5, UPB_SIZE(48, 96), 0, 1, 11, 3}, {5, UPB_SIZE(44, 88), 0, 1, 11, 3},
{6, UPB_SIZE(52, 104), 0, 4, 11, 3}, {6, UPB_SIZE(48, 96), 0, 4, 11, 3},
{7, UPB_SIZE(56, 112), 0, 2, 11, 3}, {7, UPB_SIZE(52, 104), 0, 2, 11, 3},
{8, UPB_SIZE(32, 64), 4, 3, 11, 1}, {8, UPB_SIZE(28, 56), 4, 3, 11, 1},
{9, UPB_SIZE(36, 72), 5, 5, 11, 1}, {9, UPB_SIZE(32, 64), 5, 5, 11, 1},
{10, UPB_SIZE(60, 120), 0, 0, 5, 3}, {10, UPB_SIZE(56, 112), 0, 0, 5, 3},
{11, UPB_SIZE(64, 128), 0, 0, 5, 3}, {11, UPB_SIZE(60, 120), 0, 0, 5, 3},
{12, UPB_SIZE(24, 48), 3, 0, 9, 1}, {12, UPB_SIZE(20, 40), 3, 0, 9, 1},
}; };
const upb_msglayout google_protobuf_FileDescriptorProto_msginit = { const upb_msglayout google_protobuf_FileDescriptorProto_msginit = {
&google_protobuf_FileDescriptorProto_submsgs[0], &google_protobuf_FileDescriptorProto_submsgs[0],
&google_protobuf_FileDescriptorProto__fields[0], &google_protobuf_FileDescriptorProto__fields[0],
UPB_SIZE(72, 144), 12, false, UPB_SIZE(64, 128), 12, false,
}; };
static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = { static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = {
@ -67,22 +67,22 @@ static const upb_msglayout *const google_protobuf_DescriptorProto_submsgs[8] = {
}; };
static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = { static const upb_msglayout_field google_protobuf_DescriptorProto__fields[10] = {
{1, UPB_SIZE(8, 16), 1, 0, 9, 1}, {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
{2, UPB_SIZE(20, 40), 0, 4, 11, 3}, {2, UPB_SIZE(16, 32), 0, 4, 11, 3},
{3, UPB_SIZE(24, 48), 0, 0, 11, 3}, {3, UPB_SIZE(20, 40), 0, 0, 11, 3},
{4, UPB_SIZE(28, 56), 0, 3, 11, 3}, {4, UPB_SIZE(24, 48), 0, 3, 11, 3},
{5, UPB_SIZE(32, 64), 0, 1, 11, 3}, {5, UPB_SIZE(28, 56), 0, 1, 11, 3},
{6, UPB_SIZE(36, 72), 0, 4, 11, 3}, {6, UPB_SIZE(32, 64), 0, 4, 11, 3},
{7, UPB_SIZE(16, 32), 2, 5, 11, 1}, {7, UPB_SIZE(12, 24), 2, 5, 11, 1},
{8, UPB_SIZE(40, 80), 0, 6, 11, 3}, {8, UPB_SIZE(36, 72), 0, 6, 11, 3},
{9, UPB_SIZE(44, 88), 0, 2, 11, 3}, {9, UPB_SIZE(40, 80), 0, 2, 11, 3},
{10, UPB_SIZE(48, 96), 0, 0, 9, 3}, {10, UPB_SIZE(44, 88), 0, 0, 9, 3},
}; };
const upb_msglayout google_protobuf_DescriptorProto_msginit = { const upb_msglayout google_protobuf_DescriptorProto_msginit = {
&google_protobuf_DescriptorProto_submsgs[0], &google_protobuf_DescriptorProto_submsgs[0],
&google_protobuf_DescriptorProto__fields[0], &google_protobuf_DescriptorProto__fields[0],
UPB_SIZE(56, 112), 10, false, UPB_SIZE(48, 96), 10, false,
}; };
static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = { static const upb_msglayout *const google_protobuf_DescriptorProto_ExtensionRange_submsgs[1] = {
@ -154,14 +154,14 @@ static const upb_msglayout *const google_protobuf_OneofDescriptorProto_submsgs[1
}; };
static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = { static const upb_msglayout_field google_protobuf_OneofDescriptorProto__fields[2] = {
{1, UPB_SIZE(8, 16), 1, 0, 9, 1}, {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
{2, UPB_SIZE(16, 32), 2, 0, 11, 1}, {2, UPB_SIZE(12, 24), 2, 0, 11, 1},
}; };
const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = { const upb_msglayout google_protobuf_OneofDescriptorProto_msginit = {
&google_protobuf_OneofDescriptorProto_submsgs[0], &google_protobuf_OneofDescriptorProto_submsgs[0],
&google_protobuf_OneofDescriptorProto__fields[0], &google_protobuf_OneofDescriptorProto__fields[0],
UPB_SIZE(24, 48), 2, false, UPB_SIZE(16, 32), 2, false,
}; };
static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = { static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3] = {
@ -171,11 +171,11 @@ static const upb_msglayout *const google_protobuf_EnumDescriptorProto_submsgs[3]
}; };
static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = { static const upb_msglayout_field google_protobuf_EnumDescriptorProto__fields[5] = {
{1, UPB_SIZE(8, 16), 1, 0, 9, 1}, {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
{2, UPB_SIZE(20, 40), 0, 2, 11, 3}, {2, UPB_SIZE(16, 32), 0, 2, 11, 3},
{3, UPB_SIZE(16, 32), 2, 1, 11, 1}, {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
{4, UPB_SIZE(24, 48), 0, 0, 11, 3}, {4, UPB_SIZE(20, 40), 0, 0, 11, 3},
{5, UPB_SIZE(28, 56), 0, 0, 9, 3}, {5, UPB_SIZE(24, 48), 0, 0, 9, 3},
}; };
const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = { const upb_msglayout google_protobuf_EnumDescriptorProto_msginit = {
@ -200,15 +200,15 @@ static const upb_msglayout *const google_protobuf_EnumValueDescriptorProto_subms
}; };
static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = { static const upb_msglayout_field google_protobuf_EnumValueDescriptorProto__fields[3] = {
{1, UPB_SIZE(8, 16), 2, 0, 9, 1}, {1, UPB_SIZE(8, 8), 2, 0, 9, 1},
{2, UPB_SIZE(4, 4), 1, 0, 5, 1}, {2, UPB_SIZE(4, 4), 1, 0, 5, 1},
{3, UPB_SIZE(16, 32), 3, 0, 11, 1}, {3, UPB_SIZE(16, 24), 3, 0, 11, 1},
}; };
const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = { const upb_msglayout google_protobuf_EnumValueDescriptorProto_msginit = {
&google_protobuf_EnumValueDescriptorProto_submsgs[0], &google_protobuf_EnumValueDescriptorProto_submsgs[0],
&google_protobuf_EnumValueDescriptorProto__fields[0], &google_protobuf_EnumValueDescriptorProto__fields[0],
UPB_SIZE(24, 48), 3, false, UPB_SIZE(24, 32), 3, false,
}; };
static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = { static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs[2] = {
@ -217,9 +217,9 @@ static const upb_msglayout *const google_protobuf_ServiceDescriptorProto_submsgs
}; };
static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = { static const upb_msglayout_field google_protobuf_ServiceDescriptorProto__fields[3] = {
{1, UPB_SIZE(8, 16), 1, 0, 9, 1}, {1, UPB_SIZE(4, 8), 1, 0, 9, 1},
{2, UPB_SIZE(20, 40), 0, 0, 11, 3}, {2, UPB_SIZE(16, 32), 0, 0, 11, 3},
{3, UPB_SIZE(16, 32), 2, 1, 11, 1}, {3, UPB_SIZE(12, 24), 2, 1, 11, 1},
}; };
const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = { const upb_msglayout google_protobuf_ServiceDescriptorProto_msginit = {
@ -233,10 +233,10 @@ static const upb_msglayout *const google_protobuf_MethodDescriptorProto_submsgs[
}; };
static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = { static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6] = {
{1, UPB_SIZE(8, 16), 3, 0, 9, 1}, {1, UPB_SIZE(4, 8), 3, 0, 9, 1},
{2, UPB_SIZE(16, 32), 4, 0, 9, 1}, {2, UPB_SIZE(12, 24), 4, 0, 9, 1},
{3, UPB_SIZE(24, 48), 5, 0, 9, 1}, {3, UPB_SIZE(20, 40), 5, 0, 9, 1},
{4, UPB_SIZE(32, 64), 6, 0, 11, 1}, {4, UPB_SIZE(28, 56), 6, 0, 11, 1},
{5, UPB_SIZE(1, 1), 1, 0, 8, 1}, {5, UPB_SIZE(1, 1), 1, 0, 8, 1},
{6, UPB_SIZE(2, 2), 2, 0, 8, 1}, {6, UPB_SIZE(2, 2), 2, 0, 8, 1},
}; };
@ -244,7 +244,7 @@ static const upb_msglayout_field google_protobuf_MethodDescriptorProto__fields[6
const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = { const upb_msglayout google_protobuf_MethodDescriptorProto_msginit = {
&google_protobuf_MethodDescriptorProto_submsgs[0], &google_protobuf_MethodDescriptorProto_submsgs[0],
&google_protobuf_MethodDescriptorProto__fields[0], &google_protobuf_MethodDescriptorProto__fields[0],
UPB_SIZE(40, 80), 6, false, UPB_SIZE(32, 64), 6, false,
}; };
static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = { static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
@ -252,11 +252,11 @@ static const upb_msglayout *const google_protobuf_FileOptions_submsgs[1] = {
}; };
static const upb_msglayout_field google_protobuf_FileOptions__fields[19] = { static const upb_msglayout_field google_protobuf_FileOptions__fields[19] = {
{1, UPB_SIZE(32, 32), 11, 0, 9, 1}, {1, UPB_SIZE(28, 32), 11, 0, 9, 1},
{8, UPB_SIZE(40, 48), 12, 0, 9, 1}, {8, UPB_SIZE(36, 48), 12, 0, 9, 1},
{9, UPB_SIZE(8, 8), 1, 0, 14, 1}, {9, UPB_SIZE(8, 8), 1, 0, 14, 1},
{10, UPB_SIZE(16, 16), 2, 0, 8, 1}, {10, UPB_SIZE(16, 16), 2, 0, 8, 1},
{11, UPB_SIZE(48, 64), 13, 0, 9, 1}, {11, UPB_SIZE(44, 64), 13, 0, 9, 1},
{16, UPB_SIZE(17, 17), 3, 0, 8, 1}, {16, UPB_SIZE(17, 17), 3, 0, 8, 1},
{17, UPB_SIZE(18, 18), 4, 0, 8, 1}, {17, UPB_SIZE(18, 18), 4, 0, 8, 1},
{18, UPB_SIZE(19, 19), 5, 0, 8, 1}, {18, UPB_SIZE(19, 19), 5, 0, 8, 1},
@ -264,19 +264,19 @@ static const upb_msglayout_field google_protobuf_FileOptions__fields[19] = {
{23, UPB_SIZE(21, 21), 7, 0, 8, 1}, {23, UPB_SIZE(21, 21), 7, 0, 8, 1},
{27, UPB_SIZE(22, 22), 8, 0, 8, 1}, {27, UPB_SIZE(22, 22), 8, 0, 8, 1},
{31, UPB_SIZE(23, 23), 9, 0, 8, 1}, {31, UPB_SIZE(23, 23), 9, 0, 8, 1},
{36, UPB_SIZE(56, 80), 14, 0, 9, 1}, {36, UPB_SIZE(52, 80), 14, 0, 9, 1},
{37, UPB_SIZE(64, 96), 15, 0, 9, 1}, {37, UPB_SIZE(60, 96), 15, 0, 9, 1},
{39, UPB_SIZE(72, 112), 16, 0, 9, 1}, {39, UPB_SIZE(68, 112), 16, 0, 9, 1},
{40, UPB_SIZE(80, 128), 17, 0, 9, 1}, {40, UPB_SIZE(76, 128), 17, 0, 9, 1},
{41, UPB_SIZE(88, 144), 18, 0, 9, 1}, {41, UPB_SIZE(84, 144), 18, 0, 9, 1},
{42, UPB_SIZE(24, 24), 10, 0, 8, 1}, {42, UPB_SIZE(24, 24), 10, 0, 8, 1},
{999, UPB_SIZE(96, 160), 0, 0, 11, 3}, {999, UPB_SIZE(92, 160), 0, 0, 11, 3},
}; };
const upb_msglayout google_protobuf_FileOptions_msginit = { const upb_msglayout google_protobuf_FileOptions_msginit = {
&google_protobuf_FileOptions_submsgs[0], &google_protobuf_FileOptions_submsgs[0],
&google_protobuf_FileOptions__fields[0], &google_protobuf_FileOptions__fields[0],
UPB_SIZE(104, 176), 19, false, UPB_SIZE(96, 176), 19, false,
}; };
static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = { static const upb_msglayout *const google_protobuf_MessageOptions_submsgs[1] = {
@ -414,7 +414,7 @@ const upb_msglayout google_protobuf_UninterpretedOption_msginit = {
}; };
static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = { static const upb_msglayout_field google_protobuf_UninterpretedOption_NamePart__fields[2] = {
{1, UPB_SIZE(8, 16), 2, 0, 9, 2}, {1, UPB_SIZE(4, 8), 2, 0, 9, 2},
{2, UPB_SIZE(1, 1), 1, 0, 8, 2}, {2, UPB_SIZE(1, 1), 1, 0, 8, 2},
}; };
@ -439,17 +439,17 @@ const upb_msglayout google_protobuf_SourceCodeInfo_msginit = {
}; };
static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = { static const upb_msglayout_field google_protobuf_SourceCodeInfo_Location__fields[5] = {
{1, UPB_SIZE(24, 48), 0, 0, 5, 3}, {1, UPB_SIZE(20, 40), 0, 0, 5, 3},
{2, UPB_SIZE(28, 56), 0, 0, 5, 3}, {2, UPB_SIZE(24, 48), 0, 0, 5, 3},
{3, UPB_SIZE(8, 16), 1, 0, 9, 1}, {3, UPB_SIZE(4, 8), 1, 0, 9, 1},
{4, UPB_SIZE(16, 32), 2, 0, 9, 1}, {4, UPB_SIZE(12, 24), 2, 0, 9, 1},
{6, UPB_SIZE(32, 64), 0, 0, 9, 3}, {6, UPB_SIZE(28, 56), 0, 0, 9, 3},
}; };
const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = { const upb_msglayout google_protobuf_SourceCodeInfo_Location_msginit = {
NULL, NULL,
&google_protobuf_SourceCodeInfo_Location__fields[0], &google_protobuf_SourceCodeInfo_Location__fields[0],
UPB_SIZE(40, 80), 5, false, UPB_SIZE(32, 64), 5, false,
}; };
static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = { static const upb_msglayout *const google_protobuf_GeneratedCodeInfo_submsgs[1] = {
@ -467,8 +467,8 @@ const upb_msglayout google_protobuf_GeneratedCodeInfo_msginit = {
}; };
static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = { static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__fields[4] = {
{1, UPB_SIZE(24, 32), 0, 0, 5, 3}, {1, UPB_SIZE(20, 32), 0, 0, 5, 3},
{2, UPB_SIZE(16, 16), 3, 0, 9, 1}, {2, UPB_SIZE(12, 16), 3, 0, 9, 1},
{3, UPB_SIZE(4, 4), 1, 0, 5, 1}, {3, UPB_SIZE(4, 4), 1, 0, 5, 1},
{4, UPB_SIZE(8, 8), 2, 0, 5, 1}, {4, UPB_SIZE(8, 8), 2, 0, 5, 1},
}; };
@ -476,7 +476,7 @@ static const upb_msglayout_field google_protobuf_GeneratedCodeInfo_Annotation__f
const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = { const upb_msglayout google_protobuf_GeneratedCodeInfo_Annotation_msginit = {
NULL, NULL,
&google_protobuf_GeneratedCodeInfo_Annotation__fields[0], &google_protobuf_GeneratedCodeInfo_Annotation__fields[0],
UPB_SIZE(32, 48), 4, false, UPB_SIZE(24, 48), 4, false,
}; };
#include "upb/port_undef.inc" #include "upb/port_undef.inc"

File diff suppressed because it is too large Load Diff

@ -39,7 +39,7 @@ void CheckedWrite(int fd, const void *buf, size_t len) {
} }
} }
bool stringview_eql(upb_stringview view, const char *str) { bool stringview_eql(upb_strview view, const char *str) {
return view.size == strlen(str) && memcmp(view.data, str, view.size) == 0; return view.size == strlen(str) && memcmp(view.data, str, view.size) == 0;
} }
@ -54,7 +54,7 @@ void DoTest(
proto3_msg)) { proto3_msg)) {
static const char msg[] = "Only proto3 for now."; static const char msg[] = "Only proto3 for now.";
conformance_ConformanceResponse_set_skipped( conformance_ConformanceResponse_set_skipped(
response, upb_stringview_make(msg, sizeof(msg))); response, upb_strview_make(msg, sizeof(msg)));
return; return;
} }
@ -62,14 +62,14 @@ void DoTest(
switch (conformance_ConformanceRequest_payload_case(request)) { switch (conformance_ConformanceRequest_payload_case(request)) {
case conformance_ConformanceRequest_payload_protobuf_payload: { case conformance_ConformanceRequest_payload_protobuf_payload: {
upb_stringview payload = conformance_ConformanceRequest_protobuf_payload(request); upb_strview payload = conformance_ConformanceRequest_protobuf_payload(request);
test_message = protobuf_test_messages_proto3_TestAllTypesProto3_parsenew( test_message = protobuf_test_messages_proto3_TestAllTypesProto3_parsenew(
payload, arena); payload, arena);
if (!test_message) { if (!test_message) {
static const char msg[] = "Parse error"; static const char msg[] = "Parse error";
conformance_ConformanceResponse_set_parse_error( conformance_ConformanceResponse_set_parse_error(
response, upb_stringview_make(msg, sizeof(msg))); response, upb_strview_make(msg, sizeof(msg)));
return; return;
} }
break; break;
@ -78,7 +78,7 @@ void DoTest(
case conformance_ConformanceRequest_payload_json_payload: { case conformance_ConformanceRequest_payload_json_payload: {
static const char msg[] = "JSON support not yet implemented."; static const char msg[] = "JSON support not yet implemented.";
conformance_ConformanceResponse_set_skipped( conformance_ConformanceResponse_set_skipped(
response, upb_stringview_make(msg, sizeof(msg))); response, upb_strview_make(msg, sizeof(msg)));
return; return;
} }
@ -105,18 +105,18 @@ void DoTest(
if (!serialized) { if (!serialized) {
static const char msg[] = "Error serializing."; static const char msg[] = "Error serializing.";
conformance_ConformanceResponse_set_serialize_error( conformance_ConformanceResponse_set_serialize_error(
response, upb_stringview_make(msg, sizeof(msg))); response, upb_strview_make(msg, sizeof(msg)));
return; return;
} }
conformance_ConformanceResponse_set_protobuf_payload( conformance_ConformanceResponse_set_protobuf_payload(
response, upb_stringview_make(serialized, serialized_len)); response, upb_strview_make(serialized, serialized_len));
break; break;
} }
case conformance_JSON: { case conformance_JSON: {
static const char msg[] = "JSON support not yet implemented."; static const char msg[] = "JSON support not yet implemented.";
conformance_ConformanceResponse_set_skipped( conformance_ConformanceResponse_set_skipped(
response, upb_stringview_make(msg, sizeof(msg))); response, upb_strview_make(msg, sizeof(msg)));
break; break;
} }
@ -155,7 +155,7 @@ bool DoTestIo() {
} }
request = conformance_ConformanceRequest_parsenew( request = conformance_ConformanceRequest_parsenew(
upb_stringview_make(serialized_input, input_size), &arena); upb_strview_make(serialized_input, input_size), &arena);
response = conformance_ConformanceResponse_new(&arena); response = conformance_ConformanceResponse_new(&arena);
if (request) { if (request) {

@ -7,8 +7,6 @@
* regenerated. */ * regenerated. */
#include "upb/def.h" #include "upb/def.h"
#include "upb/structdefs.int.h"
static const upb_msgdef msgs[8]; static const upb_msgdef msgs[8];
static const upb_fielddef fields[37]; static const upb_fielddef fields[37];
static const upb_enumdef enums[1]; static const upb_enumdef enums[1];

@ -16,11 +16,11 @@ class Amalgamator:
self.output_h = open(output_path + "upb.h", "w") self.output_h = open(output_path + "upb.h", "w")
self.output_c = open(output_path + "upb.c", "w") self.output_c = open(output_path + "upb.c", "w")
self.output_c.write("// Amalgamated source file\n") self.output_c.write("/* Amalgamated source file */\n")
self.output_c.write('#include "upb.h"\n') self.output_c.write('#include "upb.h"\n')
self.output_c.write(open("upb/port_def.inc").read()) self.output_c.write(open("upb/port_def.inc").read())
self.output_h.write("// Amalgamated source file\n") self.output_h.write("/* Amalgamated source file */\n")
self.output_h.write(open("upb/port_def.inc").read()) self.output_h.write(open("upb/port_def.inc").read())
def finish(self): def finish(self):

@ -433,7 +433,6 @@ local function dump_defs_c(filedef, append)
-- Emit forward declarations. -- Emit forward declarations.
emit_file_warning(filedef, append) emit_file_warning(filedef, append)
append('#include "upb/def.h"\n') append('#include "upb/def.h"\n')
append('#include "upb/structdefs.int.h"\n\n')
append("static const upb_msgdef %s;\n", linktab:cdecl(upb.DEF_MSG)) append("static const upb_msgdef %s;\n", linktab:cdecl(upb.DEF_MSG))
append("static const upb_fielddef %s;\n", linktab:cdecl(upb.DEF_FIELD)) append("static const upb_fielddef %s;\n", linktab:cdecl(upb.DEF_FIELD))
if not linktab:empty(upb.DEF_ENUM) then if not linktab:empty(upb.DEF_ENUM) then

@ -123,9 +123,15 @@ class BuildFileFunctions(object):
def genrule(self, **kwargs): def genrule(self, **kwargs):
pass pass
def glob(*args): def glob(self, *args):
return [] return []
def map_dep(self, dep):
return dep
def licenses(self, *args):
pass
class WorkspaceFileFunctions(object): class WorkspaceFileFunctions(object):
def __init__(self, converter): def __init__(self, converter):

@ -16,7 +16,7 @@ static int lupb_pb_decode(lua_State *L) {
const upb_msglayout *layout; const upb_msglayout *layout;
upb_msg *msg = lupb_msg_checkmsg2(L, 1, &layout); upb_msg *msg = lupb_msg_checkmsg2(L, 1, &layout);
const char *pb = lua_tolstring(L, 2, &len); const char *pb = lua_tolstring(L, 2, &len);
upb_stringview buf = upb_stringview_make(pb, len); upb_strview buf = upb_strview_make(pb, len);
upb_decode(buf, msg, layout); upb_decode(buf, msg, layout);
/* TODO(haberman): check for error. */ /* TODO(haberman): check for error. */

@ -21,7 +21,6 @@
#include "lauxlib.h" #include "lauxlib.h"
#include "upb/bindings/lua/upb.h" #include "upb/bindings/lua/upb.h"
#include "upb/def.h" #include "upb/def.h"
#include "upb/structdefs.int.h"
#include "upb/table.int.h" #include "upb/table.int.h"
static void lupbtable_setnum(lua_State *L, int tab, const char *key, static void lupbtable_setnum(lua_State *L, int tab, const char *key,

@ -112,14 +112,14 @@ static int64_t upb_zzdecode_64(uint64_t n) {
} }
static bool upb_decode_string(const char **ptr, const char *limit, static bool upb_decode_string(const char **ptr, const char *limit,
upb_stringview *val) { upb_strview *val) {
uint32_t len; uint32_t len;
CHK(upb_decode_varint32(ptr, limit, &len) && CHK(upb_decode_varint32(ptr, limit, &len) &&
len < INT32_MAX && len < INT32_MAX &&
limit - *ptr >= (int32_t)len); limit - *ptr >= (int32_t)len);
*val = upb_stringview_make(*ptr, len); *val = upb_strview_make(*ptr, len);
*ptr += len; *ptr += len;
return true; return true;
} }
@ -150,7 +150,7 @@ static bool upb_skip_unknownfielddata(upb_decstate *d, upb_decframe *frame,
return upb_decode_64bit(&d->ptr, frame->limit, &val); return upb_decode_64bit(&d->ptr, frame->limit, &val);
} }
case UPB_WIRE_TYPE_DELIMITED: { case UPB_WIRE_TYPE_DELIMITED: {
upb_stringview val; upb_strview val;
return upb_decode_string(&d->ptr, frame->limit, &val); return upb_decode_string(&d->ptr, frame->limit, &val);
} }
case UPB_WIRE_TYPE_START_GROUP: case UPB_WIRE_TYPE_START_GROUP:
@ -374,7 +374,7 @@ static bool upb_decode_32bitfield(upb_decstate *d, upb_decframe *frame,
return true; return true;
} }
static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data, static bool upb_decode_fixedpacked(upb_array *arr, upb_strview data,
int elem_size) { int elem_size) {
int elements = data.size / elem_size; int elements = data.size / elem_size;
void *field_mem; void *field_mem;
@ -389,7 +389,7 @@ static bool upb_decode_fixedpacked(upb_array *arr, upb_stringview data,
static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame, static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame,
const char *field_start, const char *field_start,
const upb_msglayout_field *field, const upb_msglayout_field *field,
upb_stringview val) { upb_strview val) {
upb_array *arr = upb_getorcreatearr(frame, field); upb_array *arr = upb_getorcreatearr(frame, field);
#define VARINT_CASE(ctype, decode) { \ #define VARINT_CASE(ctype, decode) { \
@ -470,7 +470,7 @@ static bool upb_decode_toarray(upb_decstate *d, upb_decframe *frame,
static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame, static bool upb_decode_delimitedfield(upb_decstate *d, upb_decframe *frame,
const char *field_start, const char *field_start,
const upb_msglayout_field *field) { const upb_msglayout_field *field) {
upb_stringview val; upb_strview val;
CHK(upb_decode_string(&d->ptr, frame->limit, &val)); CHK(upb_decode_string(&d->ptr, frame->limit, &val));
@ -584,7 +584,7 @@ static bool upb_decode_message(upb_decstate *d, const char *limit,
return true; return true;
} }
bool upb_decode(upb_stringview buf, void *msg, const upb_msglayout *l) { bool upb_decode(upb_strview buf, void *msg, const upb_msglayout *l) {
upb_decstate state; upb_decstate state;
state.ptr = buf.data; state.ptr = buf.data;

@ -9,7 +9,7 @@
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
bool upb_decode(upb_stringview buf, upb_msg *msg, const upb_msglayout *l); bool upb_decode(upb_strview buf, upb_msg *msg, const upb_msglayout *l);
UPB_END_EXTERN_C UPB_END_EXTERN_C

@ -4,7 +4,6 @@
#include <ctype.h> #include <ctype.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include "upb/structdefs.int.h"
#include "upb/handlers.h" #include "upb/handlers.h"
typedef struct { typedef struct {
@ -665,6 +664,8 @@ static void freefield(upb_refcounted *r) {
upb_fielddef_uninit_default(f); upb_fielddef_uninit_default(f);
if (f->subdef_is_symbolic) if (f->subdef_is_symbolic)
upb_gfree(f->sub.name); upb_gfree(f->sub.name);
if (f->msg_is_symbolic)
upb_gfree(f->msg.name);
upb_def_uninit(upb_fielddef_upcast_mutable(f)); upb_def_uninit(upb_fielddef_upcast_mutable(f));
upb_gfree(f); upb_gfree(f);
} }
@ -1022,7 +1023,7 @@ bool upb_fielddef_setnumber(upb_fielddef *f, uint32_t number, upb_status *s) {
s, "cannot change field number after adding to a message"); s, "cannot change field number after adding to a message");
return false; return false;
} }
if (number == 0 || number > UPB_MAX_FIELDNUMBER) { if (number == 0) {
upb_status_seterrf(s, "invalid field number (%u)", number); upb_status_seterrf(s, "invalid field number (%u)", number);
return false; return false;
} }

@ -118,9 +118,24 @@ class upb::Def {
private: private:
UPB_DISALLOW_POD_OPS(Def, upb::Def) UPB_DISALLOW_POD_OPS(Def, upb::Def)
#else
struct upb_def {
upb_refcounted base;
const char *fullname;
const upb_filedef* file;
char type; /* A upb_deftype_t (char to save space) */
/* Used as a flag during the def's mutable stage. Must be false unless
* it is currently being used by a function on the stack. This allows
* us to easily determine which defs were passed into the function's
* current invocation. */
bool came_from_user;
#endif
}; };
#endif /* __cplusplus */ #define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \
{ UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false }
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
@ -583,12 +598,57 @@ class upb::FieldDef {
private: private:
UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef) UPB_DISALLOW_POD_OPS(FieldDef, upb::FieldDef)
}; #else
struct upb_fielddef {
upb_def base;
union {
int64_t sint;
uint64_t uint;
double dbl;
float flt;
void *bytes;
} defaultval;
union {
const upb_msgdef *def; /* If !msg_is_symbolic. */
char *name; /* If msg_is_symbolic. */
} msg;
union {
const upb_def *def; /* If !subdef_is_symbolic. */
char *name; /* If subdef_is_symbolic. */
} sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */
bool subdef_is_symbolic;
bool msg_is_symbolic;
const upb_oneofdef *oneof;
bool default_is_string;
bool type_is_set_; /* False until type is explicitly set. */
bool is_extension_;
bool lazy_;
bool packed_;
upb_intfmt_t intfmt;
bool tagdelim;
upb_fieldtype_t type_;
upb_label_t label_;
uint32_t number_;
uint32_t selector_base; /* Used to index into a upb::Handlers table. */
uint32_t index_;
# endif /* defined(__cplusplus) */ # endif /* defined(__cplusplus) */
};
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
extern const struct upb_refcounted_vtbl upb_fielddef_vtbl;
#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \
packed, name, num, msgdef, subdef, selector_base, \
index, defaultval, refs, ref2s) \
{ \
UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s), \
defaultval, {msgdef}, {subdef}, NULL, false, false, \
type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
}
/* Native C API. */ /* Native C API. */
upb_fielddef *upb_fielddef_new(const void *owner); upb_fielddef *upb_fielddef_new(const void *owner);
@ -938,12 +998,45 @@ class upb::MessageDef {
private: private:
UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef) UPB_DISALLOW_POD_OPS(MessageDef, upb::MessageDef)
}; #else
struct upb_msgdef {
upb_def base;
size_t selector_count;
uint32_t submsg_field_count;
/* Tables for looking up fields by number and name. */
upb_inttable itof; /* int to field */
upb_strtable ntof; /* name to field/oneof */
/* Is this a map-entry message? */
bool map_entry;
/* Whether this message has proto2 or proto3 semantics. */
upb_syntax_t syntax;
/* Type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
* non-well-known message. */
upb_wellknowntype_t well_known_type;
/* TODO(haberman): proper extension ranges (there can be multiple). */
#endif /* __cplusplus */ #endif /* __cplusplus */
};
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
extern const struct upb_refcounted_vtbl upb_msgdef_vtbl;
/* TODO: also support static initialization of the oneofs table. This will be
* needed if we compile in descriptors that contain oneofs. */
#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
map_entry, syntax, well_known_type, refs, ref2s) \
{ \
UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s), \
selector_count, submsg_field_count, itof, ntof, map_entry, syntax, \
well_known_type \
}
/* Returns NULL if memory allocation failed. */ /* Returns NULL if memory allocation failed. */
upb_msgdef *upb_msgdef_new(const void *owner); upb_msgdef *upb_msgdef_new(const void *owner);
@ -1122,12 +1215,24 @@ class upb::EnumDef {
private: private:
UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef) UPB_DISALLOW_POD_OPS(EnumDef, upb::EnumDef)
}; #else
struct upb_enumdef {
upb_def base;
upb_strtable ntoi;
upb_inttable iton;
int32_t defaultval;
#endif /* __cplusplus */ #endif /* __cplusplus */
};
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
extern const struct upb_refcounted_vtbl upb_enumdef_vtbl;
#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \
{ UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi, \
iton, defaultval }
/* Native C API. */ /* Native C API. */
upb_enumdef *upb_enumdef_new(const void *owner); upb_enumdef *upb_enumdef_new(const void *owner);
@ -1276,12 +1381,25 @@ class upb::OneofDef {
private: private:
UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef) UPB_DISALLOW_POD_OPS(OneofDef, upb::OneofDef)
}; #else
struct upb_oneofdef {
upb_refcounted base;
uint32_t index; /* Index within oneofs. */
const char *name;
upb_strtable ntof;
upb_inttable itof;
const upb_msgdef *parent;
#endif /* __cplusplus */ #endif /* __cplusplus */
};
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl;
#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \
{ UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof }
/* Native C API. */ /* Native C API. */
upb_oneofdef *upb_oneofdef_new(const void *owner); upb_oneofdef *upb_oneofdef_new(const void *owner);
@ -1400,12 +1518,25 @@ class upb::FileDef {
private: private:
UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef) UPB_DISALLOW_POD_OPS(FileDef, upb::FileDef)
}; #else
struct upb_filedef {
upb_refcounted base;
const char *name;
const char *package;
const char *phpprefix;
const char *phpnamespace;
upb_syntax_t syntax;
upb_inttable defs;
upb_inttable deps;
#endif #endif
};
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
extern const struct upb_refcounted_vtbl upb_filedef_vtbl;
upb_filedef *upb_filedef_new(const void *owner); upb_filedef *upb_filedef_new(const void *owner);
/* Include upb_refcounted methods like upb_msgdef_ref(). */ /* Include upb_refcounted methods like upb_msgdef_ref(). */
@ -1535,9 +1666,13 @@ class upb::SymbolTable {
private: private:
UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable) UPB_DISALLOW_POD_OPS(SymbolTable, upb::SymbolTable)
}; #else
struct upb_symtab {
upb_refcounted base;
upb_strtable symtab;
#endif /* __cplusplus */ #endif /* __cplusplus */
};
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C

@ -7,8 +7,6 @@
* regenerated. */ * regenerated. */
#include "upb/def.h" #include "upb/def.h"
#include "upb/structdefs.int.h"
static const upb_msgdef msgs[22]; static const upb_msgdef msgs[22];
static const upb_fielddef fields[107]; static const upb_fielddef fields[107];
static const upb_enumdef enums[5]; static const upb_enumdef enums[5];

@ -321,14 +321,21 @@ static void *file_startenum(void *closure, const void *hd) {
static void *file_startext(void *closure, const void *hd) { static void *file_startext(void *closure, const void *hd) {
upb_descreader *r = closure; upb_descreader *r = closure;
bool ok;
r->f = upb_fielddef_new(r); r->f = upb_fielddef_new(r);
ok = upb_filedef_addext(r->file, r->f, r, NULL);
UPB_UNUSED(hd); UPB_UNUSED(hd);
UPB_ASSERT(ok);
return r; return r;
} }
static bool file_endext(void *closure, const void *hd) {
/* The current symtab code can't handle extensions, so we just discard
* them for now. */
upb_descreader *r = closure;
upb_fielddef_unref(r->f, r);
UPB_UNUSED(hd);
r->f = NULL;
return true;
}
static size_t file_ondep(void *closure, const void *hd, const char *buf, static size_t file_ondep(void *closure, const void *hd, const char *buf,
size_t n, const upb_bufhandle *handle) { size_t n, const upb_bufhandle *handle) {
upb_descreader *r = closure; upb_descreader *r = closure;
@ -702,13 +709,21 @@ static void *msg_startmsg(void *closure, const void *hd) {
static void *msg_startext(void *closure, const void *hd) { static void *msg_startext(void *closure, const void *hd) {
upb_descreader *r = closure; upb_descreader *r = closure;
upb_fielddef *f = upb_fielddef_new(&f); r->f = upb_fielddef_new(r);
bool ok = upb_filedef_addext(r->file, f, &f, NULL);
UPB_UNUSED(hd); UPB_UNUSED(hd);
UPB_ASSERT(ok);
return r; return r;
} }
static bool msg_endext(void *closure, const void *hd) {
/* The current symtab code can't handle extensions, so we just discard
* them for now. */
upb_descreader *r = closure;
upb_fielddef_unref(r->f, r);
UPB_UNUSED(hd);
r->f = NULL;
return true;
}
static void *msg_startfield(void *closure, const void *hd) { static void *msg_startfield(void *closure, const void *hd) {
upb_descreader *r = closure; upb_descreader *r = closure;
r->f = upb_fielddef_new(&r->f); r->f = upb_fielddef_new(&r->f);
@ -763,6 +778,8 @@ static void reghandlers(const void *closure, upb_handlers *h) {
upb_handlers_setstring(h, F(DescriptorProto, name), &msg_name, NULL); upb_handlers_setstring(h, F(DescriptorProto, name), &msg_name, NULL);
upb_handlers_setstartsubmsg(h, F(DescriptorProto, extension), &msg_startext, upb_handlers_setstartsubmsg(h, F(DescriptorProto, extension), &msg_startext,
NULL); NULL);
upb_handlers_setendsubmsg(h, F(DescriptorProto, extension), &msg_endext,
NULL);
upb_handlers_setstartsubmsg(h, F(DescriptorProto, nested_type), upb_handlers_setstartsubmsg(h, F(DescriptorProto, nested_type),
&msg_startmsg, NULL); &msg_startmsg, NULL);
upb_handlers_setstartsubmsg(h, F(DescriptorProto, field), upb_handlers_setstartsubmsg(h, F(DescriptorProto, field),
@ -786,6 +803,8 @@ static void reghandlers(const void *closure, upb_handlers *h) {
&file_startenum, NULL); &file_startenum, NULL);
upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, extension), upb_handlers_setstartsubmsg(h, F(FileDescriptorProto, extension),
&file_startext, NULL); &file_startext, NULL);
upb_handlers_setendsubmsg(h, F(FileDescriptorProto, extension),
&file_endext, NULL);
upb_handlers_setstring(h, F(FileDescriptorProto, dependency), upb_handlers_setstring(h, F(FileDescriptorProto, dependency),
&file_ondep, NULL); &file_ondep, NULL);
} else if (upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)) { } else if (upbdefs_google_protobuf_EnumValueDescriptorProto_is(m)) {

@ -207,8 +207,8 @@ do { ; } while(0)
VARINT_CASE(int64_t, upb_zzencode_64(*ptr)); VARINT_CASE(int64_t, upb_zzencode_64(*ptr));
case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_STRING:
case UPB_DESCRIPTOR_TYPE_BYTES: { case UPB_DESCRIPTOR_TYPE_BYTES: {
upb_stringview *start = arr->data; upb_strview *start = arr->data;
upb_stringview *ptr = start + arr->len; upb_strview *ptr = start + arr->len;
do { do {
ptr--; ptr--;
CHK(upb_put_bytes(e, ptr->data, ptr->size) && CHK(upb_put_bytes(e, ptr->data, ptr->size) &&
@ -292,7 +292,7 @@ static bool upb_encode_scalarfield(upb_encstate *e, const char *field_mem,
CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val)); CASE(int64_t, varint, UPB_WIRE_TYPE_VARINT, upb_zzencode_64(val));
case UPB_DESCRIPTOR_TYPE_STRING: case UPB_DESCRIPTOR_TYPE_STRING:
case UPB_DESCRIPTOR_TYPE_BYTES: { case UPB_DESCRIPTOR_TYPE_BYTES: {
upb_stringview view = *(upb_stringview*)field_mem; upb_strview view = *(upb_strview*)field_mem;
if (skip_zero_value && view.size == 0) { if (skip_zero_value && view.size == 0) {
return true; return true;
} }

@ -0,0 +1,102 @@
/*
** Functions for use by generated code. These are not public and users must
** not call them directly.
*/
#ifndef UPB_GENERATED_UTIL_H_
#define UPB_GENERATED_UTIL_H_
#include <stdint.h>
#include "upb/structs.int.h"
#define PTR_AT(msg, ofs, type) (type*)((const char*)msg + ofs)
UPB_INLINE const void *_upb_array_accessor(const void *msg, size_t ofs,
size_t *size) {
const upb_array *arr = *PTR_AT(msg, ofs, const upb_array*);
if (arr) {
if (size) *size = arr->size;
return arr->data;
} else {
if (size) *size = 0;
return NULL;
}
}
UPB_INLINE void *_upb_array_mutable_accessor(void *msg, size_t ofs,
size_t *size) {
upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
if (arr) {
if (size) *size = arr->size;
return arr->data;
} else {
if (size) *size = 0;
return NULL;
}
}
/* TODO(haberman): this is a mess. It will improve when upb_array no longer
* carries reflective state (type, elem_size). */
UPB_INLINE void *_upb_array_resize_accessor(void *msg, size_t ofs, size_t size,
size_t elem_size,
upb_fieldtype_t type,
upb_arena *arena) {
upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
if (!arr) {
arr = upb_array_new(type, arena);
if (!arr) return NULL;
*PTR_AT(msg, ofs, upb_array*) = arr;
}
if (size > arr->size) {
size_t new_size = UPB_MAX(arr->size, 4);
size_t old_bytes = arr->size * elem_size;
size_t new_bytes;
upb_alloc *alloc = upb_arena_alloc(arr->arena);
while (new_size < size) new_size *= 2;
new_bytes = new_size * elem_size;
arr->data = upb_realloc(alloc, arr->data, old_bytes, new_bytes);
if (!arr->data) {
return NULL;
}
arr->size = new_size;
}
arr->len = size;
return arr->data;
}
UPB_INLINE bool _upb_array_append_accessor(void *msg, size_t ofs,
size_t elem_size,
upb_fieldtype_t type,
const void *value,
upb_arena *arena) {
upb_array *arr = *PTR_AT(msg, ofs, upb_array*);
size_t i = arr ? arr->len : 0;
void *data =
_upb_array_resize_accessor(msg, ofs, i + 1, elem_size, type, arena);
if (!data) return false;
memcpy(PTR_AT(data, i * elem_size, char), value, elem_size);
return true;
}
UPB_INLINE bool _upb_has_field(const void *msg, size_t idx) {
return (*PTR_AT(msg, idx / 8, const char) & (idx % 8)) != 0;
}
UPB_INLINE bool _upb_sethas(const void *msg, size_t idx) {
return (*PTR_AT(msg, idx / 8, char)) |= (1 << (idx % 8));
}
UPB_INLINE bool _upb_clearhas(const void *msg, size_t idx) {
return (*PTR_AT(msg, idx / 8, char)) &= ~(1 << (idx % 8));
}
UPB_INLINE bool _upb_has_oneof_field(const void *msg, size_t case_ofs, int32_t num) {
return *PTR_AT(msg, case_ofs, int32_t) == num;
}
#undef PTR_AT
#endif /* UPB_GENERATED_UTIL_H_ */

@ -7,6 +7,8 @@
#define UPB_HANDLERS_INL_H_ #define UPB_HANDLERS_INL_H_
#include <limits.h> #include <limits.h>
#include "upb/upb.h"
#include "upb/handlers.h"
/* C inline methods. */ /* C inline methods. */

@ -4,7 +4,6 @@
*/ */
#include "upb/handlers.h" #include "upb/handlers.h"
#include "upb/structdefs.int.h"
#include <string.h> #include <string.h>

@ -51,7 +51,7 @@ static size_t upb_msgval_sizeof(upb_fieldtype_t type) {
return sizeof(void*); return sizeof(void*);
case UPB_TYPE_BYTES: case UPB_TYPE_BYTES:
case UPB_TYPE_STRING: case UPB_TYPE_STRING:
return sizeof(upb_stringview); return sizeof(upb_strview);
} }
UPB_UNREACHABLE(); UPB_UNREACHABLE();
} }

@ -36,10 +36,12 @@ class MessageLayout;
#endif #endif
UPB_DECLARE_TYPE(upb::Array, upb_array)
UPB_DECLARE_TYPE(upb::Map, upb_map) UPB_DECLARE_TYPE(upb::Map, upb_map)
UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter) UPB_DECLARE_TYPE(upb::MapIterator, upb_mapiter)
struct upb_array;
typedef struct upb_array upb_array;
/* TODO(haberman): C++ accessors */ /* TODO(haberman): C++ accessors */
UPB_BEGIN_EXTERN_C UPB_BEGIN_EXTERN_C
@ -73,21 +75,21 @@ typedef struct upb_msglayout {
} upb_msglayout; } upb_msglayout;
/** upb_stringview ************************************************************/ /** upb_strview ************************************************************/
typedef struct { typedef struct {
const char *data; const char *data;
size_t size; size_t size;
} upb_stringview; } upb_strview;
UPB_INLINE upb_stringview upb_stringview_make(const char *data, size_t size) { UPB_INLINE upb_strview upb_strview_make(const char *data, size_t size) {
upb_stringview ret; upb_strview ret;
ret.data = data; ret.data = data;
ret.size = size; ret.size = size;
return ret; return ret;
} }
#define UPB_STRINGVIEW_INIT(ptr, len) {ptr, len} #define UPB_STRVIEW_INIT(ptr, len) {ptr, len}
/** upb_msgval ****************************************************************/ /** upb_msgval ****************************************************************/
@ -107,7 +109,7 @@ typedef union {
const upb_msg* msg; const upb_msg* msg;
const upb_array* arr; const upb_array* arr;
const void* ptr; const void* ptr;
upb_stringview str; upb_strview str;
} upb_msgval; } upb_msgval;
#define ACCESSORS(name, membername, ctype) \ #define ACCESSORS(name, membername, ctype) \
@ -134,12 +136,12 @@ ACCESSORS(map, map, const upb_map*)
ACCESSORS(msg, msg, const upb_msg*) ACCESSORS(msg, msg, const upb_msg*)
ACCESSORS(ptr, ptr, const void*) ACCESSORS(ptr, ptr, const void*)
ACCESSORS(arr, arr, const upb_array*) ACCESSORS(arr, arr, const upb_array*)
ACCESSORS(str, str, upb_stringview) ACCESSORS(str, str, upb_strview)
#undef ACCESSORS #undef ACCESSORS
UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) { UPB_INLINE upb_msgval upb_msgval_makestr(const char *data, size_t size) {
return upb_msgval_str(upb_stringview_make(data, size)); return upb_msgval_str(upb_strview_make(data, size));
} }

@ -32,7 +32,7 @@ static size_t upb_msgval_sizeof2(upb_fieldtype_t type) {
return sizeof(void*); return sizeof(void*);
case UPB_TYPE_BYTES: case UPB_TYPE_BYTES:
case UPB_TYPE_STRING: case UPB_TYPE_STRING:
return sizeof(upb_stringview); return sizeof(upb_strview);
} }
UPB_UNREACHABLE(); UPB_UNREACHABLE();
} }

@ -5,6 +5,12 @@
#ifndef UPB_MSGFACTORY_H_ #ifndef UPB_MSGFACTORY_H_
#define UPB_MSGFACTORY_H_ #define UPB_MSGFACTORY_H_
#ifdef __cplusplus
namespace upb {
class MessageFactory;
}
#endif
UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory) UPB_DECLARE_TYPE(upb::MessageFactory, upb_msgfactory)
/** upb_msgfactory ************************************************************/ /** upb_msgfactory ************************************************************/

@ -9,17 +9,10 @@
#include "upb/handlers.h" #include "upb/handlers.h"
#include "upb/pb/decoder.h" #include "upb/pb/decoder.h"
#include "upb/sink.h" #include "upb/sink.h"
#include "upb/structdefs.int.h"
#include "upb/table.int.h" #include "upb/table.int.h"
/* C++ names are not actually used since this type isn't exposed to users. */ #ifndef __cplusplus
#ifdef __cplusplus
namespace upb {
namespace pb {
class MessageGroup;
} /* namespace pb */
} /* namespace upb */
#endif
UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted, UPB_DECLARE_DERIVED_TYPE(upb::pb::MessageGroup, upb::RefCounted,
mgroup, upb_refcounted) mgroup, upb_refcounted)
@ -82,7 +75,7 @@ typedef enum {
#define OP_MAX OP_HALT #define OP_MAX OP_HALT
UPB_INLINE opcode getop(uint32_t instr) { return instr & 0xff; } UPB_INLINE opcode getop(uint32_t instr) { return (opcode)(instr & 0xff); }
/* Method group; represents a set of decoder methods that had their code /* Method group; represents a set of decoder methods that had their code
* emitted together, and must therefore be freed together. Immutable once * emitted together, and must therefore be freed together. Immutable once
@ -329,4 +322,6 @@ UPB_INLINE void upb_pbdecoder_unpackdispatch(uint64_t dispatch, uint64_t *ofs,
#define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; } #define CHECK_RETURN(x) { int32_t ret = x; if (ret >= 0) return ret; }
#endif /* __cplusplus */
#endif /* UPB_DECODER_INT_H_ */ #endif /* UPB_DECODER_INT_H_ */

@ -1,196 +0,0 @@
/*
** This file contains definitions of structs that should be considered private
** and NOT stable across versions of upb.
**
** The only reason they are declared here and not in .c files is to allow upb
** and the application (if desired) to embed statically-initialized instances
** of structures like defs.
**
** If you include this file, all guarantees of ABI compatibility go out the
** window! Any code that includes this file needs to recompile against the
** exact same version of upb that they are linking against.
**
** You also need to recompile if you change the value of the UPB_DEBUG_REFS
** flag.
*/
#include "upb/def.h"
#ifndef UPB_STATICINIT_H_
#define UPB_STATICINIT_H_
#ifdef __cplusplus
/* Because of how we do our typedefs, this header can't be included from C++. */
#error This file cannot be included from C++
#endif
/* upb_refcounted *************************************************************/
/* upb_def ********************************************************************/
struct upb_def {
upb_refcounted base;
const char *fullname;
const upb_filedef* file;
char type; /* A upb_deftype_t (char to save space) */
/* Used as a flag during the def's mutable stage. Must be false unless
* it is currently being used by a function on the stack. This allows
* us to easily determine which defs were passed into the function's
* current invocation. */
bool came_from_user;
};
#define UPB_DEF_INIT(name, type, vtbl, refs, ref2s) \
{ UPB_REFCOUNT_INIT(vtbl, refs, ref2s), name, NULL, type, false }
/* upb_fielddef ***************************************************************/
struct upb_fielddef {
upb_def base;
union {
int64_t sint;
uint64_t uint;
double dbl;
float flt;
void *bytes;
} defaultval;
union {
const upb_msgdef *def; /* If !msg_is_symbolic. */
char *name; /* If msg_is_symbolic. */
} msg;
union {
const upb_def *def; /* If !subdef_is_symbolic. */
char *name; /* If subdef_is_symbolic. */
} sub; /* The msgdef or enumdef for this field, if upb_hassubdef(f). */
bool subdef_is_symbolic;
bool msg_is_symbolic;
const upb_oneofdef *oneof;
bool default_is_string;
bool type_is_set_; /* False until type is explicitly set. */
bool is_extension_;
bool lazy_;
bool packed_;
upb_intfmt_t intfmt;
bool tagdelim;
upb_fieldtype_t type_;
upb_label_t label_;
uint32_t number_;
uint32_t selector_base; /* Used to index into a upb::Handlers table. */
uint32_t index_;
};
extern const struct upb_refcounted_vtbl upb_fielddef_vtbl;
#define UPB_FIELDDEF_INIT(label, type, intfmt, tagdelim, is_extension, lazy, \
packed, name, num, msgdef, subdef, selector_base, \
index, defaultval, refs, ref2s) \
{ \
UPB_DEF_INIT(name, UPB_DEF_FIELD, &upb_fielddef_vtbl, refs, ref2s), \
defaultval, {msgdef}, {subdef}, NULL, false, false, \
type == UPB_TYPE_STRING || type == UPB_TYPE_BYTES, true, is_extension, \
lazy, packed, intfmt, tagdelim, type, label, num, selector_base, index \
}
/* upb_msgdef *****************************************************************/
struct upb_msgdef {
upb_def base;
size_t selector_count;
uint32_t submsg_field_count;
/* Tables for looking up fields by number and name. */
upb_inttable itof; /* int to field */
upb_strtable ntof; /* name to field/oneof */
/* Is this a map-entry message? */
bool map_entry;
/* Whether this message has proto2 or proto3 semantics. */
upb_syntax_t syntax;
/* Type of well known type message. UPB_WELLKNOWN_UNSPECIFIED for
* non-well-known message. */
upb_wellknowntype_t well_known_type;
/* TODO(haberman): proper extension ranges (there can be multiple). */
};
extern const struct upb_refcounted_vtbl upb_msgdef_vtbl;
/* TODO: also support static initialization of the oneofs table. This will be
* needed if we compile in descriptors that contain oneofs. */
#define UPB_MSGDEF_INIT(name, selector_count, submsg_field_count, itof, ntof, \
map_entry, syntax, well_known_type, refs, ref2s) \
{ \
UPB_DEF_INIT(name, UPB_DEF_MSG, &upb_fielddef_vtbl, refs, ref2s), \
selector_count, submsg_field_count, itof, ntof, map_entry, syntax, \
well_known_type \
}
/* upb_enumdef ****************************************************************/
struct upb_enumdef {
upb_def base;
upb_strtable ntoi;
upb_inttable iton;
int32_t defaultval;
};
extern const struct upb_refcounted_vtbl upb_enumdef_vtbl;
#define UPB_ENUMDEF_INIT(name, ntoi, iton, defaultval, refs, ref2s) \
{ UPB_DEF_INIT(name, UPB_DEF_ENUM, &upb_enumdef_vtbl, refs, ref2s), ntoi, \
iton, defaultval }
/* upb_oneofdef ***************************************************************/
struct upb_oneofdef {
upb_refcounted base;
uint32_t index; /* Index within oneofs. */
const char *name;
upb_strtable ntof;
upb_inttable itof;
const upb_msgdef *parent;
};
extern const struct upb_refcounted_vtbl upb_oneofdef_vtbl;
#define UPB_ONEOFDEF_INIT(name, ntof, itof, refs, ref2s) \
{ UPB_REFCOUNT_INIT(&upb_oneofdef_vtbl, refs, ref2s), 0, name, ntof, itof }
/* upb_symtab *****************************************************************/
struct upb_symtab {
upb_refcounted base;
upb_strtable symtab;
};
struct upb_filedef {
upb_refcounted base;
const char *name;
const char *package;
const char *phpprefix;
const char *phpnamespace;
upb_syntax_t syntax;
upb_inttable defs;
upb_inttable deps;
};
extern const struct upb_refcounted_vtbl upb_filedef_vtbl;
#endif /* UPB_STATICINIT_H_ */

@ -5,6 +5,9 @@
#ifndef UPB_STRUCTS_H_ #ifndef UPB_STRUCTS_H_
#define UPB_STRUCTS_H_ #define UPB_STRUCTS_H_
#include "upb/def.h"
#include "upb/msg.h"
struct upb_array { struct upb_array {
upb_fieldtype_t type; upb_fieldtype_t type;
uint8_t element_size; uint8_t element_size;

@ -177,19 +177,23 @@ std::string GetSizeInit(const MessageLayout::Size& size) {
return absl::Substitute("UPB_SIZE($0, $1)", size.size32, size.size64); return absl::Substitute("UPB_SIZE($0, $1)", size.size32, size.size64);
} }
std::string MessageName(const protobuf::Descriptor* descriptor) {
return ToCIdent(descriptor->full_name());
}
std::string MessageInit(const protobuf::Descriptor* descriptor) {
return MessageName(descriptor) + "_msginit";
}
std::string CTypeInternal(const protobuf::FieldDescriptor* field, std::string CTypeInternal(const protobuf::FieldDescriptor* field,
bool is_const) { bool is_const) {
std::string maybe_const = is_const ? "const " : ""; std::string maybe_const = is_const ? "const " : "";
if (field->label() == protobuf::FieldDescriptor::LABEL_REPEATED) {
return maybe_const + "upb_array*";
}
switch (field->cpp_type()) { switch (field->cpp_type()) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: { case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: {
std::string maybe_struct = std::string maybe_struct =
field->file() != field->message_type()->file() ? "struct " : ""; field->file() != field->message_type()->file() ? "struct " : "";
return maybe_const + maybe_struct + return maybe_const + maybe_struct + MessageName(field->message_type()) +
ToCIdent(field->message_type()->full_name()) + "*"; "*";
} }
case protobuf::FieldDescriptor::CPPTYPE_ENUM: case protobuf::FieldDescriptor::CPPTYPE_ENUM:
return ToCIdent(field->enum_type()->full_name()); return ToCIdent(field->enum_type()->full_name());
@ -208,7 +212,35 @@ std::string CTypeInternal(const protobuf::FieldDescriptor* field,
case protobuf::FieldDescriptor::CPPTYPE_UINT64: case protobuf::FieldDescriptor::CPPTYPE_UINT64:
return "uint64_t"; return "uint64_t";
case protobuf::FieldDescriptor::CPPTYPE_STRING: case protobuf::FieldDescriptor::CPPTYPE_STRING:
return "upb_stringview"; return "upb_strview";
default:
fprintf(stderr, "Unexpected type");
abort();
}
}
std::string UpbType(const protobuf::FieldDescriptor* field) {
switch (field->cpp_type()) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
return "UPB_TYPE_MESSAGE";
case protobuf::FieldDescriptor::CPPTYPE_ENUM:
return "UPB_TYPE_ENUM";
case protobuf::FieldDescriptor::CPPTYPE_BOOL:
return "UPB_TYPE_BOOL";
case protobuf::FieldDescriptor::CPPTYPE_FLOAT:
return "UPB_TYPE_FLOAT";
case protobuf::FieldDescriptor::CPPTYPE_INT32:
return "UPB_TYPE_INT32";
case protobuf::FieldDescriptor::CPPTYPE_UINT32:
return "UPB_TYPE_UINT32";
case protobuf::FieldDescriptor::CPPTYPE_DOUBLE:
return "UPB_TYPE_DOUBLE";
case protobuf::FieldDescriptor::CPPTYPE_INT64:
return "UPB_TYPE_INT64";
case protobuf::FieldDescriptor::CPPTYPE_UINT64:
return "UPB_TYPE_UINT64";
case protobuf::FieldDescriptor::CPPTYPE_STRING:
return "UPB_TYPE_STRING";
default: default:
fprintf(stderr, "Unexpected type"); fprintf(stderr, "Unexpected type");
abort(); abort();
@ -220,7 +252,7 @@ std::string FieldDefault(const protobuf::FieldDescriptor* field) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE: case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
return "NULL"; return "NULL";
case protobuf::FieldDescriptor::CPPTYPE_STRING: case protobuf::FieldDescriptor::CPPTYPE_STRING:
return absl::Substitute("upb_stringview_make(\"$0\", strlen(\"$0\"))", return absl::Substitute("upb_strview_make(\"$0\", strlen(\"$0\"))",
absl::CEscape(field->default_value_string())); absl::CEscape(field->default_value_string()));
case protobuf::FieldDescriptor::CPPTYPE_INT32: case protobuf::FieldDescriptor::CPPTYPE_INT32:
return absl::StrCat(field->default_value_int32()); return absl::StrCat(field->default_value_int32());
@ -290,20 +322,19 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output
output("/* $0 */\n\n", message->full_name()); output("/* $0 */\n\n", message->full_name());
std::string msgname = ToCIdent(message->full_name()); std::string msgname = ToCIdent(message->full_name());
output( output(
"extern const upb_msglayout $0_msginit;\n"
"UPB_INLINE $0 *$0_new(upb_arena *arena) {\n" "UPB_INLINE $0 *$0_new(upb_arena *arena) {\n"
" return ($0 *)upb_msg_new(&$0_msginit, arena);\n" " return ($0 *)upb_msg_new(&$1, arena);\n"
"}\n" "}\n"
"UPB_INLINE $0 *$0_parsenew(upb_stringview buf, upb_arena *arena) {\n" "UPB_INLINE $0 *$0_parsenew(upb_strview buf, upb_arena *arena) {\n"
" $0 *ret = $0_new(arena);\n" " $0 *ret = $0_new(arena);\n"
" return (ret && upb_decode(buf, ret, &$0_msginit)) ? ret : NULL;\n" " return (ret && upb_decode(buf, ret, &$1)) ? ret : NULL;\n"
"}\n" "}\n"
"UPB_INLINE char *$0_serialize(const $0 *msg, upb_arena *arena, size_t " "UPB_INLINE char *$0_serialize(const $0 *msg, upb_arena *arena, size_t "
"*len) {\n" "*len) {\n"
" return upb_encode(msg, &$0_msginit, arena, len);\n" " return upb_encode(msg, &$1, arena, len);\n"
"}\n" "}\n"
"\n", "\n",
msgname); MessageName(message), MessageInit(message));
for (int i = 0; i < message->oneof_decl_count(); i++) { for (int i = 0; i < message->oneof_decl_count(); i++) {
const protobuf::OneofDescriptor* oneof = message->oneof_decl(i); const protobuf::OneofDescriptor* oneof = message->oneof_decl(i);
@ -326,32 +357,122 @@ void GenerateMessageInHeader(const protobuf::Descriptor* message, Output& output
} }
for (auto field : FieldNumberOrder(message)) { for (auto field : FieldNumberOrder(message)) {
output("UPB_INLINE $0 $1_$2(const $1 *msg) {", CTypeConst(field), msgname,
field->name()); if (layout.HasHasbit(field)) {
if (field->containing_oneof()) { output(
output(" return UPB_READ_ONEOF(msg, $0, $1, $2, $3, $4); }\n", "UPB_INLINE bool $0_has_$1(const $0 *msg) { "
CTypeConst(field), GetSizeInit(layout.GetFieldOffset(field)), "return _upb_has_field(msg, $2); }\n",
GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())), msgname, field->name(), layout.GetHasbitIndex(field));
field->number(), FieldDefault(field)); } else if (field->containing_oneof()) {
output(
"UPB_INLINE bool $0_has_$1(const $0 *msg) { "
"return _upb_has_oneof_field(msg, $2, $3); }\n",
msgname, field->name(),
GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())),
field->number());
}
if (field->is_repeated()) {
output(
"UPB_INLINE $0 const* $1_$2(const $1 *msg, size_t *len) { "
"return ($0 const*)_upb_array_accessor(msg, $3, len); }\n",
CTypeConst(field), msgname, field->name(),
GetSizeInit(layout.GetFieldOffset(field)));
} else if (field->containing_oneof()) {
output(
"UPB_INLINE $0 $1_$2(const $1 *msg) { "
"return UPB_READ_ONEOF(msg, $0, $3, $4, $5, $6); }\n",
CTypeConst(field), msgname, field->name(),
GetSizeInit(layout.GetFieldOffset(field)),
GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())),
field->number(), FieldDefault(field));
} else { } else {
output(" return UPB_FIELD_AT(msg, $0, $1); }\n", CTypeConst(field), output(
GetSizeInit(layout.GetFieldOffset(field))); "UPB_INLINE $0 $1_$2(const $1 *msg) { "
"return UPB_FIELD_AT(msg, $0, $3); }\n",
CTypeConst(field), msgname, field->name(),
GetSizeInit(layout.GetFieldOffset(field)));
} }
} }
output("\n"); output("\n");
for (auto field : FieldNumberOrder(message)) { for (auto field : FieldNumberOrder(message)) {
output("UPB_INLINE void $0_set_$1($0 *msg, $2 value) { ", msgname, if (field->is_repeated()) {
field->name(), CType(field)); output(
if (field->containing_oneof()) { "UPB_INLINE $0* $1_mutable_$2($1 *msg, size_t *len) {\n"
output("UPB_WRITE_ONEOF(msg, $0, $1, value, $2, $3); }\n", CType(field), " return ($0*)_upb_array_mutable_accessor(msg, $3, len);\n"
GetSizeInit(layout.GetFieldOffset(field)), "}\n",
GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())), CType(field), msgname, field->name(),
field->number()); GetSizeInit(layout.GetFieldOffset(field)));
output(
"UPB_INLINE $0* $1_resize_$2($1 *msg, size_t len, "
"upb_arena *arena) {\n"
" return ($0*)_upb_array_resize_accessor(msg, $3, len, $4, $5, "
"arena);\n"
"}\n",
CType(field), msgname, field->name(),
GetSizeInit(layout.GetFieldOffset(field)),
GetSizeInit(MessageLayout::SizeOfUnwrapped(field).size),
UpbType(field));
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
output(
"UPB_INLINE struct $0* $1_add_$2($1 *msg, upb_arena *arena) {\n"
" struct $0* sub = (struct $0*)upb_msg_new(&$3, arena);\n"
" bool ok = _upb_array_append_accessor(\n"
" msg, $4, $5, $6, &sub, arena);\n"
" if (!ok) return NULL;\n"
" return sub;\n"
"}\n",
MessageName(field->message_type()), msgname, field->name(),
MessageInit(field->message_type()),
GetSizeInit(layout.GetFieldOffset(field)),
GetSizeInit(MessageLayout::SizeOfUnwrapped(field).size),
UpbType(field));
} else {
output(
"UPB_INLINE bool $1_add_$2($1 *msg, $0 val, upb_arena *arena) {\n"
" return _upb_array_append_accessor(\n"
" msg, $3, $4, $5, &val, arena);\n"
"}\n",
CType(field), msgname, field->name(),
GetSizeInit(layout.GetFieldOffset(field)),
GetSizeInit(MessageLayout::SizeOfUnwrapped(field).size),
UpbType(field));
}
} else { } else {
output("UPB_FIELD_AT(msg, $0, $1) = value; }\n", CType(field), output("UPB_INLINE void $0_set_$1($0 *msg, $2 value) {\n", msgname,
GetSizeInit(layout.GetFieldOffset(field))); field->name(), CType(field));
if (field->containing_oneof()) {
output(
" UPB_WRITE_ONEOF(msg, $0, $1, value, $2, $3);\n"
"}\n",
CType(field), GetSizeInit(layout.GetFieldOffset(field)),
GetSizeInit(layout.GetOneofCaseOffset(field->containing_oneof())),
field->number());
} else {
if (MessageLayout::HasHasbit(field)) {
output(" _upb_sethas(msg, $0);\n", layout.GetHasbitIndex(field));
}
output(
" UPB_FIELD_AT(msg, $0, $1) = value;\n"
"}\n",
CType(field), GetSizeInit(layout.GetFieldOffset(field)));
}
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) {
output(
"UPB_INLINE struct $0* $1_mutable_$2($1 *msg, upb_arena *arena) {\n"
" struct $0* sub = (struct $0*)$1_$2(msg);\n"
" if (sub == NULL) {\n"
" sub = (struct $0*)upb_msg_new(&$3, arena);\n"
" if (!sub) return NULL;\n"
" $1_set_$2(msg, sub);\n"
" }\n"
" return sub;\n"
"}\n",
MessageName(field->message_type()), msgname, field->name(),
MessageInit(field->message_type()));
}
} }
} }
@ -363,6 +484,7 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) {
output( output(
"#ifndef $0_UPB_H_\n" "#ifndef $0_UPB_H_\n"
"#define $0_UPB_H_\n\n" "#define $0_UPB_H_\n\n"
"#include \"upb/generated_util.h\"\n\n"
"#include \"upb/msg.h\"\n\n" "#include \"upb/msg.h\"\n\n"
"#include \"upb/decode.h\"\n" "#include \"upb/decode.h\"\n"
"#include \"upb/encode.h\"\n" "#include \"upb/encode.h\"\n"
@ -376,21 +498,27 @@ void WriteHeader(const protobuf::FileDescriptor* file, Output& output) {
} }
for (auto message : SortedMessages(file)) { for (auto message : SortedMessages(file)) {
output("typedef struct $0 $0;\n", ToCIdent(message->full_name())); output("typedef struct $0 $0;\n", ToCIdent(message->full_name()));
}; }
for (auto message : SortedMessages(file)) {
output("extern const upb_msglayout $0;\n", MessageInit(message));
}
// Forward-declare types not in this file, but used as submessages. // Forward-declare types not in this file, but used as submessages.
std::set<std::string> forward_names; std::set<const protobuf::Descriptor*> forward_messages;
for (auto message : SortedMessages(file)) { for (auto message : SortedMessages(file)) {
for (int i = 0; i < message->field_count(); i++) { for (int i = 0; i < message->field_count(); i++) {
const protobuf::FieldDescriptor* field = message->field(i); const protobuf::FieldDescriptor* field = message->field(i);
if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE && if (field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE &&
field->file() != message->file()) { field->file() != field->message_type()->file()) {
forward_names.insert(ToCIdent(field->message_type()->full_name())); forward_messages.insert(field->message_type());
} }
} }
} }
for (const auto& name : forward_names) { for (const auto& descriptor : forward_messages) {
output("struct $0;\n", name); output("struct $0;\n", MessageName(descriptor));
}
for (const auto& descriptor : forward_messages) {
output("extern const upb_msglayout $0;\n", MessageInit(descriptor));
} }
output( output(
@ -458,8 +586,7 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
submsg_indexes.end()) { submsg_indexes.end()) {
continue; continue;
} }
output(" &$0_msginit,\n", output(" &$0,\n", MessageInit(submsg->message_type()));
ToCIdent(submsg->message_type()->full_name()));
submsg_indexes[submsg->message_type()] = i++; submsg_indexes[submsg->message_type()] = i++;
} }
@ -504,7 +631,7 @@ void WriteSource(const protobuf::FileDescriptor* file, Output& output) {
output("};\n\n"); output("};\n\n");
} }
output("const upb_msglayout $0_msginit = {\n", msgname); output("const upb_msglayout $0 = {\n", MessageInit(message));
output(" $0,\n", submsgs_array_ref); output(" $0,\n", submsgs_array_ref);
output(" $0,\n", fields_array_ref); output(" $0,\n", fields_array_ref);
output(" $0, $1, $2,\n", GetSizeInit(layout.message_size()), output(" $0, $1, $2,\n", GetSizeInit(layout.message_size()),

@ -30,16 +30,20 @@ bool MessageLayout::HasHasbit(const protobuf::FieldDescriptor* field) {
MessageLayout::SizeAndAlign MessageLayout::SizeOf( MessageLayout::SizeAndAlign MessageLayout::SizeOf(
const protobuf::FieldDescriptor* field) { const protobuf::FieldDescriptor* field) {
if (field->label() == protobuf::FieldDescriptor::LABEL_REPEATED || if (field->is_repeated()) {
field->cpp_type() == protobuf::FieldDescriptor::CPPTYPE_MESSAGE) { return {{4, 8}, {4, 8}}; // Pointer to array object.
return {{4, 8}, {4, 8}}; } else {
return SizeOfUnwrapped(field);
} }
}
MessageLayout::SizeAndAlign MessageLayout::SizeOfUnwrapped(
const protobuf::FieldDescriptor* field) {
switch (field->cpp_type()) { switch (field->cpp_type()) {
case protobuf::FieldDescriptor::CPPTYPE_MESSAGE:
return {{4, 8}, {4, 8}}; // Pointer to message.
case protobuf::FieldDescriptor::CPPTYPE_STRING: case protobuf::FieldDescriptor::CPPTYPE_STRING:
// upb_stringview return {{8, 16}, {4, 8}}; // upb_stringview
// return {{8, 16}, {4, 8}};
return {{8, 16}, {8, 16}};
case protobuf::FieldDescriptor::CPPTYPE_BOOL: case protobuf::FieldDescriptor::CPPTYPE_BOOL:
return {{1, 1}, {1, 1}}; return {{1, 1}, {1, 1}};
case protobuf::FieldDescriptor::CPPTYPE_FLOAT: case protobuf::FieldDescriptor::CPPTYPE_FLOAT:

@ -60,6 +60,8 @@ class MessageLayout {
Size message_size() const { return size_; } Size message_size() const { return size_; }
static bool HasHasbit(const google::protobuf::FieldDescriptor* field); static bool HasHasbit(const google::protobuf::FieldDescriptor* field);
static SizeAndAlign SizeOfUnwrapped(
const google::protobuf::FieldDescriptor* field);
private: private:
void ComputeLayout(const google::protobuf::Descriptor* descriptor); void ComputeLayout(const google::protobuf::Descriptor* descriptor);
@ -87,7 +89,8 @@ class MessageLayout {
} }
static SizeAndAlign SizeOf(const google::protobuf::FieldDescriptor* field); static SizeAndAlign SizeOf(const google::protobuf::FieldDescriptor* field);
static int64_t FieldLayoutRank(const google::protobuf::FieldDescriptor* field); static int64_t FieldLayoutRank(
const google::protobuf::FieldDescriptor* field);
std::unordered_map<const google::protobuf::FieldDescriptor*, Size> std::unordered_map<const google::protobuf::FieldDescriptor*, Size>
field_offsets_; field_offsets_;

Loading…
Cancel
Save