修改pods

This commit is contained in:
2025-09-20 17:13:38 +08:00
parent 7787b3ee30
commit 28ff2b0264
5251 changed files with 345029 additions and 285168 deletions

View File

@@ -64,7 +64,34 @@ typedef enum {
extern "C" {
#endif
UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType type) {
// Convert from upb_FieldType to upb_CType
UPB_INLINE upb_CType upb_FieldType_CType(upb_FieldType field_type) {
static const upb_CType c_type[] = {
kUpb_CType_Double, // kUpb_FieldType_Double
kUpb_CType_Float, // kUpb_FieldType_Float
kUpb_CType_Int64, // kUpb_FieldType_Int64
kUpb_CType_UInt64, // kUpb_FieldType_UInt64
kUpb_CType_Int32, // kUpb_FieldType_Int32
kUpb_CType_UInt64, // kUpb_FieldType_Fixed64
kUpb_CType_UInt32, // kUpb_FieldType_Fixed32
kUpb_CType_Bool, // kUpb_FieldType_Bool
kUpb_CType_String, // kUpb_FieldType_String
kUpb_CType_Message, // kUpb_FieldType_Group
kUpb_CType_Message, // kUpb_FieldType_Message
kUpb_CType_Bytes, // kUpb_FieldType_Bytes
kUpb_CType_UInt32, // kUpb_FieldType_UInt32
kUpb_CType_Enum, // kUpb_FieldType_Enum
kUpb_CType_Int32, // kUpb_FieldType_SFixed32
kUpb_CType_Int64, // kUpb_FieldType_SFixed64
kUpb_CType_Int32, // kUpb_FieldType_SInt32
kUpb_CType_Int64, // kUpb_FieldType_SInt64
};
// -1 here because the enum is one-based but the table is zero-based.
return c_type[field_type - 1];
}
UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType field_type) {
// clang-format off
const unsigned kUnpackableTypes =
(1 << kUpb_FieldType_String) |
@@ -72,7 +99,7 @@ UPB_INLINE bool upb_FieldType_IsPackable(upb_FieldType type) {
(1 << kUpb_FieldType_Message) |
(1 << kUpb_FieldType_Group);
// clang-format on
return (1 << type) & ~kUnpackableTypes;
return (1 << field_type) & ~kUnpackableTypes;
}
#ifdef __cplusplus

View File

@@ -5,8 +5,8 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_WIRE_INTERNAL_SWAP_H_
#define UPB_WIRE_INTERNAL_SWAP_H_
#ifndef UPB_BASE_INTERNAL_ENDIAN_H_
#define UPB_BASE_INTERNAL_ENDIAN_H_
#include <stdint.h>
@@ -17,23 +17,24 @@
extern "C" {
#endif
UPB_INLINE bool _upb_IsLittleEndian(void) {
int x = 1;
UPB_INLINE bool upb_IsLittleEndian(void) {
const int x = 1;
return *(char*)&x == 1;
}
UPB_INLINE uint32_t _upb_BigEndian_Swap32(uint32_t val) {
if (_upb_IsLittleEndian()) return val;
UPB_INLINE uint32_t upb_BigEndian32(uint32_t val) {
if (upb_IsLittleEndian()) return val;
return ((val & 0xff) << 24) | ((val & 0xff00) << 8) |
((val & 0xff0000) >> 8) | ((val & 0xff000000) >> 24);
}
UPB_INLINE uint64_t _upb_BigEndian_Swap64(uint64_t val) {
if (_upb_IsLittleEndian()) return val;
UPB_INLINE uint64_t upb_BigEndian64(uint64_t val) {
if (upb_IsLittleEndian()) return val;
return ((uint64_t)_upb_BigEndian_Swap32((uint32_t)val) << 32) |
_upb_BigEndian_Swap32((uint32_t)(val >> 32));
const uint64_t hi = ((uint64_t)upb_BigEndian32((uint32_t)val)) << 32;
const uint64_t lo = upb_BigEndian32((uint32_t)(val >> 32));
return hi | lo;
}
#ifdef __cplusplus
@@ -42,4 +43,4 @@ UPB_INLINE uint64_t _upb_BigEndian_Swap64(uint64_t val) {
#include "upb/port/undef.inc"
#endif /* UPB_WIRE_INTERNAL_SWAP_H_ */
#endif /* UPB_BASE_INTERNAL_ENDIAN_H_ */

View File

@@ -13,7 +13,7 @@
// Must be last.
#include "upb/port/def.inc"
#define _kUpb_Status_MaxMessage 127
#define _kUpb_Status_MaxMessage 511
typedef struct {
bool ok;

View File

@@ -8,11 +8,13 @@
#ifndef UPB_BASE_STATUS_HPP_
#define UPB_BASE_STATUS_HPP_
#ifdef __cplusplus
#include "upb/base/status.h"
namespace upb {
class Status {
class Status final {
public:
Status() { upb_Status_Clear(&status_); }
@@ -47,4 +49,6 @@ class Status {
} // namespace upb
#endif // __cplusplus
#endif // UPB_BASE_STATUS_HPP_

View File

@@ -24,10 +24,6 @@ typedef struct {
const char* data;
size_t size;
} upb_StringView;
// LINT.ThenChange(
// GoogleInternalName0,
// //depot/google3/third_party/upb/bits/golang/accessor.go:map_go_string
// )
#ifdef __cplusplus
extern "C" {
@@ -46,9 +42,15 @@ UPB_INLINE upb_StringView upb_StringView_FromString(const char* data) {
}
UPB_INLINE bool upb_StringView_IsEqual(upb_StringView a, upb_StringView b) {
return a.size == b.size && memcmp(a.data, b.data, a.size) == 0;
return (a.size == b.size) && (!a.size || !memcmp(a.data, b.data, a.size));
}
// LINT.ThenChange(
// GoogleInternalName1,
// //depot/google3/third_party/upb/bits/golang/accessor.go:map_go_string,
// //depot/google3/third_party/upb/bits/typescript/string_view.ts
// )
#ifdef __cplusplus
} /* extern "C" */
#endif

29
Pods/gRPC-C++/third_party/upb/upb/base/upcast.h generated vendored Normal file
View File

@@ -0,0 +1,29 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_BASE_UPCAST_H_
#define UPB_BASE_UPCAST_H_
// Must be last.
#include "upb/port/def.inc"
// This macro provides a way to upcast message pointers in a way that is
// somewhat more bulletproof than blindly casting a pointer. Example:
//
// typedef struct {
// upb_Message UPB_PRIVATE(base);
// } pkg_FooMessage;
//
// void f(pkg_FooMessage* msg) {
// upb_Decode(UPB_UPCAST(msg), ...);
// }
#define UPB_UPCAST(x) (&(x)->base##_dont_copy_me__upb_internal_use_only)
#include "upb/port/undef.inc"
#endif /* UPB_BASE_UPCAST_H_ */

View File

@@ -9,6 +9,7 @@
#define UPB_GENERATED_CODE_SUPPORT_H_
// IWYU pragma: begin_exports
#include "upb/base/upcast.h"
#include "upb/message/accessors.h"
#include "upb/message/array.h"
#include "upb/message/internal/accessors.h"
@@ -26,8 +27,8 @@
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
#include "upb/wire/decode.h"
#include "upb/wire/decode_fast.h"
#include "upb/wire/encode.h"
#include "upb/wire/internal/decode_fast.h"
// IWYU pragma: end_exports
#endif // UPB_GENERATED_CODE_SUPPORT_H_

View File

@@ -1,36 +1,18 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_JSON_DECODE_H_
#define UPB_JSON_DECODE_H_
#include <stddef.h>
#include "upb/base/status.h"
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/reflection/def.h"
// Must be last.
@@ -42,9 +24,27 @@ extern "C" {
enum { upb_JsonDecode_IgnoreUnknown = 1 };
UPB_API bool upb_JsonDecode(const char* buf, size_t size, upb_Message* msg,
const upb_MessageDef* m, const upb_DefPool* symtab,
int options, upb_Arena* arena, upb_Status* status);
enum {
kUpb_JsonDecodeResult_Ok = 0,
kUpb_JsonDecodeResult_OkWithEmptyStringNumerics = 1,
kUpb_JsonDecodeResult_Error = 2,
};
UPB_API int upb_JsonDecodeDetectingNonconformance(const char* buf, size_t size,
upb_Message* msg,
const upb_MessageDef* m,
const upb_DefPool* symtab,
int options, upb_Arena* arena,
upb_Status* status);
UPB_API_INLINE bool upb_JsonDecode(const char* buf, size_t size,
upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* symtab, int options,
upb_Arena* arena, upb_Status* status) {
return upb_JsonDecodeDetectingNonconformance(buf, size, msg, m, symtab,
options, arena, status) ==
kUpb_JsonDecodeResult_Ok;
}
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_JSON_ENCODE_H_
#define UPB_JSON_ENCODE_H_

View File

@@ -22,19 +22,15 @@
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/mem/alloc.h"
#include "upb/mem/internal/arena.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_Arena upb_Arena;
typedef struct {
char *ptr, *end;
} _upb_ArenaHead;
#ifdef __cplusplus
extern "C" {
#endif
@@ -47,81 +43,44 @@ UPB_API upb_Arena* upb_Arena_Init(void* mem, size_t n, upb_alloc* alloc);
UPB_API void upb_Arena_Free(upb_Arena* a);
UPB_API bool upb_Arena_Fuse(upb_Arena* a, upb_Arena* b);
void upb_Arena_IncRefFor(upb_Arena* arena, const void* owner);
void upb_Arena_DecRefFor(upb_Arena* arena, const void* owner);
bool upb_Arena_IncRefFor(upb_Arena* a, const void* owner);
void upb_Arena_DecRefFor(upb_Arena* a, const void* owner);
void* _upb_Arena_SlowMalloc(upb_Arena* a, size_t size);
size_t upb_Arena_SpaceAllocated(upb_Arena* arena);
uint32_t upb_Arena_DebugRefCount(upb_Arena* arena);
size_t upb_Arena_SpaceAllocated(upb_Arena* a, size_t* fused_count);
uint32_t upb_Arena_DebugRefCount(upb_Arena* a);
UPB_INLINE size_t _upb_ArenaHas(upb_Arena* a) {
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
return (size_t)(h->end - h->ptr);
UPB_API_INLINE upb_Arena* upb_Arena_New(void) {
return upb_Arena_Init(NULL, 0, &upb_alloc_global);
}
UPB_API_INLINE void* upb_Arena_Malloc(upb_Arena* a, size_t size) {
size = UPB_ALIGN_MALLOC(size);
size_t span = size + UPB_ASAN_GUARD_SIZE;
if (UPB_UNLIKELY(_upb_ArenaHas(a) < span)) {
return _upb_Arena_SlowMalloc(a, size);
}
UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size);
// We have enough space to do a fast malloc.
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
void* ret = h->ptr;
UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret);
UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
UPB_UNPOISON_MEMORY_REGION(ret, size);
UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
size_t size);
h->ptr += span;
return ret;
}
// Sets the maximum block size for all arenas. This is a global configuration
// setting that will affect all existing and future arenas. If
// upb_Arena_Malloc() is called with a size larger than this, we will exceed
// this size and allocate a larger block.
//
// This API is meant for experimentation only. It will likely be removed in
// the future.
void upb_Arena_SetMaxBlockSize(size_t max);
// Shrinks the last alloc from arena.
// REQUIRES: (ptr, oldsize) was the last malloc/realloc from this arena.
// We could also add a upb_Arena_TryShrinkLast() which is simply a no-op if
// this was not the last alloc.
UPB_API_INLINE void upb_Arena_ShrinkLast(upb_Arena* a, void* ptr,
size_t oldsize, size_t size) {
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
// Must be the last alloc.
UPB_ASSERT((char*)ptr + oldsize == h->ptr - UPB_ASAN_GUARD_SIZE);
UPB_ASSERT(size <= oldsize);
h->ptr = (char*)ptr + size;
}
size_t oldsize, size_t size);
UPB_API_INLINE void* upb_Arena_Realloc(upb_Arena* a, void* ptr, size_t oldsize,
size_t size) {
_upb_ArenaHead* h = (_upb_ArenaHead*)a;
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
bool is_most_recent_alloc = (uintptr_t)ptr + oldsize == (uintptr_t)h->ptr;
if (is_most_recent_alloc) {
ptrdiff_t diff = size - oldsize;
if ((ptrdiff_t)_upb_ArenaHas(a) >= diff) {
h->ptr += diff;
return ptr;
}
} else if (size <= oldsize) {
return ptr;
}
void* ret = upb_Arena_Malloc(a, size);
if (ret && oldsize > 0) {
memcpy(ret, ptr, UPB_MIN(oldsize, size));
}
return ret;
}
UPB_API_INLINE upb_Arena* upb_Arena_New(void) {
return upb_Arena_Init(NULL, 0, &upb_alloc_global);
}
#ifdef UPB_TRACING_ENABLED
void upb_Arena_SetTraceHandler(void (*initArenaTraceHandler)(const upb_Arena*,
size_t size),
void (*fuseArenaTraceHandler)(const upb_Arena*,
const upb_Arena*),
void (*freeArenaTraceHandler)(const upb_Arena*));
#endif
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -8,6 +8,8 @@
#ifndef UPB_MEM_ARENA_HPP_
#define UPB_MEM_ARENA_HPP_
#ifdef __cplusplus
#include <memory>
#include "upb/mem/arena.h"
@@ -24,14 +26,18 @@ class Arena {
upb_Arena* ptr() const { return ptr_.get(); }
void Fuse(Arena& other) { upb_Arena_Fuse(ptr(), other.ptr()); }
// Fuses the arenas together.
// This operation can only be performed on arenas with no initial blocks. Will
// return false if the fuse failed due to either arena having an initial
// block.
bool Fuse(Arena& other) { return upb_Arena_Fuse(ptr(), other.ptr()); }
protected:
std::unique_ptr<upb_Arena, decltype(&upb_Arena_Free)> ptr_;
};
// InlinedArena seeds the arenas with a predefined amount of memory. No
// heap memory will be allocated until the initial block is exceeded.
// InlinedArena seeds the arenas with a predefined amount of memory. No heap
// memory will be allocated until the initial block is exceeded.
template <int N>
class InlinedArena : public Arena {
public:
@@ -43,12 +49,14 @@ class InlinedArena : public Arena {
}
private:
InlinedArena(const InlinedArena*) = delete;
InlinedArena& operator=(const InlinedArena*) = delete;
InlinedArena(const InlinedArena&) = delete;
InlinedArena& operator=(const InlinedArena&) = delete;
char initial_block_[N];
};
} // namespace upb
#endif // __cplusplus
#endif // UPB_MEM_ARENA_HPP_

View File

@@ -8,86 +8,107 @@
#ifndef UPB_MEM_INTERNAL_ARENA_H_
#define UPB_MEM_INTERNAL_ARENA_H_
#include "upb/mem/arena.h"
#include <stddef.h>
#include <stdint.h>
#include <string.h>
// Must be last.
#include "upb/port/def.inc"
typedef struct _upb_MemBlock _upb_MemBlock;
// This is QUITE an ugly hack, which specifies the number of pointers needed
// to equal (or exceed) the storage required for one upb_Arena.
//
// We need this because the decoder inlines a upb_Arena for performance but
// the full struct is not visible outside of arena.c. Yes, I know, it's awful.
#define UPB_ARENA_SIZE_HACK 7
// LINT.IfChange(upb_Arena)
struct upb_Arena {
_upb_ArenaHead head;
// upb_alloc* together with a low bit which signals if there is an initial
// block.
uintptr_t block_alloc;
// When multiple arenas are fused together, each arena points to a parent
// arena (root points to itself). The root tracks how many live arenas
// reference it.
// The low bit is tagged:
// 0: pointer to parent
// 1: count, left shifted by one
UPB_ATOMIC(uintptr_t) parent_or_count;
// All nodes that are fused together are in a singly-linked list.
UPB_ATOMIC(upb_Arena*) next; // NULL at end of list.
// The last element of the linked list. This is present only as an
// optimization, so that we do not have to iterate over all members for every
// fuse. Only significant for an arena root. In other cases it is ignored.
UPB_ATOMIC(upb_Arena*) tail; // == self when no other list members.
// Linked list of blocks to free/cleanup. Atomic only for the benefit of
// upb_Arena_SpaceAllocated().
UPB_ATOMIC(_upb_MemBlock*) blocks;
char* UPB_ONLYBITS(ptr);
char* UPB_ONLYBITS(end);
};
UPB_INLINE bool _upb_Arena_IsTaggedRefcount(uintptr_t parent_or_count) {
return (parent_or_count & 1) == 1;
// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/arena.ts:upb_Arena)
#ifdef __cplusplus
extern "C" {
#endif
void UPB_PRIVATE(_upb_Arena_SwapIn)(struct upb_Arena* des,
const struct upb_Arena* src);
void UPB_PRIVATE(_upb_Arena_SwapOut)(struct upb_Arena* des,
const struct upb_Arena* src);
// Returns whether |ptr| was allocated directly by |a| (so care must be used
// with fused arenas).
UPB_API bool UPB_ONLYBITS(_upb_Arena_Contains)(const struct upb_Arena* a,
void* ptr);
UPB_INLINE size_t UPB_PRIVATE(_upb_ArenaHas)(const struct upb_Arena* a) {
return (size_t)(a->UPB_ONLYBITS(end) - a->UPB_ONLYBITS(ptr));
}
UPB_INLINE bool _upb_Arena_IsTaggedPointer(uintptr_t parent_or_count) {
return (parent_or_count & 1) == 0;
UPB_API_INLINE void* upb_Arena_Malloc(struct upb_Arena* a, size_t size) {
void* UPB_PRIVATE(_upb_Arena_SlowMalloc)(struct upb_Arena * a, size_t size);
size = UPB_ALIGN_MALLOC(size);
const size_t span = size + UPB_ASAN_GUARD_SIZE;
if (UPB_UNLIKELY(UPB_PRIVATE(_upb_ArenaHas)(a) < span)) {
return UPB_PRIVATE(_upb_Arena_SlowMalloc)(a, span);
}
// We have enough space to do a fast malloc.
void* ret = a->UPB_ONLYBITS(ptr);
UPB_ASSERT(UPB_ALIGN_MALLOC((uintptr_t)ret) == (uintptr_t)ret);
UPB_ASSERT(UPB_ALIGN_MALLOC(size) == size);
UPB_UNPOISON_MEMORY_REGION(ret, size);
a->UPB_ONLYBITS(ptr) += span;
return ret;
}
UPB_INLINE uintptr_t _upb_Arena_RefCountFromTagged(uintptr_t parent_or_count) {
UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
return parent_or_count >> 1;
UPB_API_INLINE void* upb_Arena_Realloc(struct upb_Arena* a, void* ptr,
size_t oldsize, size_t size) {
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
bool is_most_recent_alloc =
(uintptr_t)ptr + oldsize == (uintptr_t)a->UPB_ONLYBITS(ptr);
if (is_most_recent_alloc) {
ptrdiff_t diff = size - oldsize;
if ((ptrdiff_t)UPB_PRIVATE(_upb_ArenaHas)(a) >= diff) {
a->UPB_ONLYBITS(ptr) += diff;
return ptr;
}
} else if (size <= oldsize) {
return ptr;
}
void* ret = upb_Arena_Malloc(a, size);
if (ret && oldsize > 0) {
memcpy(ret, ptr, UPB_MIN(oldsize, size));
}
return ret;
}
UPB_INLINE uintptr_t _upb_Arena_TaggedFromRefcount(uintptr_t refcount) {
uintptr_t parent_or_count = (refcount << 1) | 1;
UPB_ASSERT(_upb_Arena_IsTaggedRefcount(parent_or_count));
return parent_or_count;
UPB_API_INLINE void upb_Arena_ShrinkLast(struct upb_Arena* a, void* ptr,
size_t oldsize, size_t size) {
oldsize = UPB_ALIGN_MALLOC(oldsize);
size = UPB_ALIGN_MALLOC(size);
// Must be the last alloc.
UPB_ASSERT((char*)ptr + oldsize ==
a->UPB_ONLYBITS(ptr) - UPB_ASAN_GUARD_SIZE);
UPB_ASSERT(size <= oldsize);
a->UPB_ONLYBITS(ptr) = (char*)ptr + size;
}
UPB_INLINE upb_Arena* _upb_Arena_PointerFromTagged(uintptr_t parent_or_count) {
UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
return (upb_Arena*)parent_or_count;
}
UPB_INLINE uintptr_t _upb_Arena_TaggedFromPointer(upb_Arena* a) {
uintptr_t parent_or_count = (uintptr_t)a;
UPB_ASSERT(_upb_Arena_IsTaggedPointer(parent_or_count));
return parent_or_count;
}
UPB_INLINE upb_alloc* upb_Arena_BlockAlloc(upb_Arena* arena) {
return (upb_alloc*)(arena->block_alloc & ~0x1);
}
UPB_INLINE uintptr_t upb_Arena_MakeBlockAlloc(upb_alloc* alloc,
bool has_initial) {
uintptr_t alloc_uint = (uintptr_t)alloc;
UPB_ASSERT((alloc_uint & 1) == 0);
return alloc_uint | (has_initial ? 1 : 0);
}
UPB_INLINE bool upb_Arena_HasInitialBlock(upb_Arena* arena) {
return arena->block_alloc & 0x1;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"

View File

@@ -8,15 +8,19 @@
#ifndef UPB_MESSAGE_ACCESSORS_H_
#define UPB_MESSAGE_ACCESSORS_H_
#include "upb/base/descriptor_constants.h"
#include <stdint.h>
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/array.h"
#include "upb/message/internal/accessors.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.h"
#include "upb/message/map.h"
#include "upb/mini_table/enum.h"
#include "upb/message/message.h"
#include "upb/message/tagged_ptr.h"
#include "upb/message/value.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -25,351 +29,242 @@
extern "C" {
#endif
UPB_API_INLINE void upb_Message_ClearField(upb_Message* msg,
const upb_MiniTableField* field) {
if (upb_MiniTableField_IsExtension(field)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
_upb_Message_ClearExtensionField(msg, ext);
} else {
_upb_Message_ClearNonExtensionField(msg, field);
}
}
// Functions ending in BaseField() take a (upb_MiniTableField*) argument
// and work only on non-extension fields.
//
// Functions ending in Extension() take a (upb_MiniTableExtension*) argument
// and work only on extensions.
UPB_API_INLINE void upb_Message_Clear(upb_Message* msg,
const upb_MiniTable* l) {
// Note: Can't use UPB_PTR_AT() here because we are doing pointer subtraction.
char* mem = (char*)msg - sizeof(upb_Message_Internal);
memset(mem, 0, upb_msg_sizeof(l));
}
UPB_API_INLINE void upb_Message_Clear(upb_Message* msg, const upb_MiniTable* m);
UPB_API_INLINE bool upb_Message_HasField(const upb_Message* msg,
const upb_MiniTableField* field) {
if (upb_MiniTableField_IsExtension(field)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
return _upb_Message_HasExtensionField(msg, ext);
} else {
return _upb_Message_HasNonExtensionField(msg, field);
}
}
UPB_API_INLINE void upb_Message_ClearBaseField(upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const upb_Message* message, const upb_MiniTableField* oneof_field) {
UPB_ASSUME(_upb_MiniTableField_InOneOf(oneof_field));
return _upb_getoneofcase_field(message, oneof_field);
}
UPB_API_INLINE void upb_Message_ClearExtension(upb_Message* msg,
const upb_MiniTableExtension* e);
UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg,
const upb_MiniTableField* field,
bool default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Bool);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
bool ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE void upb_Message_ClearOneof(upb_Message* msg,
const upb_MiniTable* m,
const upb_MiniTableField* f);
UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg,
const upb_MiniTableField* field,
bool value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Bool);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE bool upb_Message_HasBaseField(const upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE int32_t upb_Message_GetInt32(const upb_Message* msg,
const upb_MiniTableField* field,
int32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(field) == kUpb_CType_Enum);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
int32_t ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_HasExtension(const upb_Message* msg,
const upb_MiniTableExtension* e);
UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg,
const upb_MiniTableField* field,
int32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(field) == kUpb_CType_Enum);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg,
const upb_MiniTableField* field,
uint32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt32);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
uint32_t ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg,
const upb_MiniTableField* field,
uint32_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt32);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE void upb_Message_SetClosedEnum(
upb_Message* msg, const upb_MiniTable* msg_mini_table,
const upb_MiniTableField* field, int32_t value) {
UPB_ASSERT(upb_MiniTableField_IsClosedEnum(field));
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSERT(upb_MiniTableEnum_CheckValue(
upb_MiniTable_GetSubEnumTable(msg_mini_table, field), value));
_upb_Message_SetNonExtensionField(msg, field, &value);
}
UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg,
const upb_MiniTableField* field,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int64);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
int64_t ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg,
const upb_MiniTableField* field,
int64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Int64);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE uint64_t upb_Message_GetUInt64(const upb_Message* msg,
const upb_MiniTableField* field,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt64);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
uint64_t ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg,
const upb_MiniTableField* field,
uint64_t value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_UInt64);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg,
const upb_MiniTableField* field,
float default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Float);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
float ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetFloat(upb_Message* msg,
const upb_MiniTableField* field,
float value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Float);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_4Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg,
const upb_MiniTableField* field,
double default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Double);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
double ret;
_upb_Message_GetField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg,
const upb_MiniTableField* field,
double value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Double);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_8Byte);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE upb_StringView
upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field,
upb_StringView def_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_String ||
upb_MiniTableField_CType(field) == kUpb_CType_Bytes);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
upb_StringView ret;
_upb_Message_GetField(msg, field, &def_val, &ret);
return ret;
}
UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg,
const upb_MiniTableField* field,
upb_StringView value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_String ||
upb_MiniTableField_CType(field) == kUpb_CType_Bytes);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_StringView);
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
return _upb_Message_SetField(msg, field, &value, a);
}
UPB_API_INLINE upb_MessageValue
upb_Message_GetField(const upb_Message* msg, const upb_MiniTableField* f,
upb_MessageValue default_val);
UPB_API_INLINE upb_TaggedMessagePtr upb_Message_GetTaggedMessagePtr(
const upb_Message* msg, const upb_MiniTableField* field,
upb_Message* default_val) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
upb_TaggedMessagePtr tagged;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged);
return tagged;
}
UPB_API_INLINE const upb_Message* upb_Message_GetMessage(
const upb_Message* msg, const upb_MiniTableField* field,
upb_Message* default_val) {
upb_TaggedMessagePtr tagged =
upb_Message_GetTaggedMessagePtr(msg, field, default_val);
return upb_TaggedMessagePtr_GetNonEmptyMessage(tagged);
}
// For internal use only; users cannot set tagged messages because only the
// parser and the message copier are allowed to directly create an empty
// message.
UPB_API_INLINE void _upb_Message_SetTaggedMessagePtr(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* field, upb_TaggedMessagePtr sub_message) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
UPB_ASSUME(_upb_MiniTableField_GetRep(field) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(!upb_IsRepeatedOrMap(field));
UPB_ASSERT(mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg);
_upb_Message_SetNonExtensionField(msg, field, &sub_message);
}
UPB_API_INLINE void upb_Message_SetMessage(upb_Message* msg,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* sub_message) {
_upb_Message_SetTaggedMessagePtr(
msg, mini_table, field, _upb_TaggedMessagePtr_Pack(sub_message, false));
}
UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
upb_Message* sub_message = *UPB_PTR_AT(msg, field->offset, upb_Message*);
if (!sub_message) {
const upb_MiniTable* sub_mini_table =
mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg;
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, field->offset, upb_Message*) = sub_message;
_upb_Message_SetPresence(msg, field);
}
return sub_message;
}
upb_Message* default_val);
UPB_API_INLINE const upb_Array* upb_Message_GetArray(
const upb_Message* msg, const upb_MiniTableField* field) {
_upb_MiniTableField_CheckIsArray(field);
upb_Array* ret;
const upb_Array* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
return ret;
}
const upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE bool upb_Message_GetBool(const upb_Message* msg,
const upb_MiniTableField* f,
bool default_val);
UPB_API_INLINE double upb_Message_GetDouble(const upb_Message* msg,
const upb_MiniTableField* field,
double default_val);
UPB_API_INLINE float upb_Message_GetFloat(const upb_Message* msg,
const upb_MiniTableField* f,
float default_val);
UPB_API_INLINE int32_t upb_Message_GetInt32(const upb_Message* msg,
const upb_MiniTableField* f,
int32_t default_val);
UPB_API_INLINE int64_t upb_Message_GetInt64(const upb_Message* msg,
const upb_MiniTableField* f,
int64_t default_val);
UPB_API_INLINE const upb_Map* upb_Message_GetMap(const upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE const upb_Message* upb_Message_GetMessage(
const upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
upb_Message* msg, const upb_MiniTableField* field) {
_upb_MiniTableField_CheckIsArray(field);
return (upb_Array*)upb_Message_GetArray(msg, field);
}
upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE upb_Map* upb_Message_GetMutableMap(upb_Message* msg,
const upb_MiniTableField* f);
UPB_API_INLINE upb_Message* upb_Message_GetMutableMessage(
upb_Message* msg, const upb_MiniTableField* f);
UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
upb_Message* msg, const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSERT(arena);
_upb_MiniTableField_CheckIsArray(field);
upb_Array* array = upb_Message_GetMutableArray(msg, field);
if (!array) {
array = _upb_Array_New(arena, 4, _upb_MiniTable_ElementSizeLg2(field));
// Check again due to: https://godbolt.org/z/7WfaoKG1r
_upb_MiniTableField_CheckIsArray(field);
_upb_Message_SetField(msg, field, &array, arena);
}
return array;
}
UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
upb_Message* msg, const upb_MiniTableField* field, size_t size,
upb_Arena* arena) {
_upb_MiniTableField_CheckIsArray(field);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, field, arena);
if (!arr || !_upb_Array_ResizeUninitialized(arr, size, arena)) return NULL;
return _upb_array_ptr(arr);
}
UPB_API_INLINE const upb_Map* upb_Message_GetMap(
const upb_Message* msg, const upb_MiniTableField* field) {
_upb_MiniTableField_CheckIsMap(field);
_upb_Message_AssertMapIsUntagged(msg, field);
upb_Map* ret;
const upb_Map* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
return ret;
}
UPB_API_INLINE upb_Map* upb_Message_GetMutableMap(
upb_Message* msg, const upb_MiniTableField* field) {
return (upb_Map*)upb_Message_GetMap(msg, field);
}
upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena);
UPB_API_INLINE upb_Map* upb_Message_GetOrCreateMutableMap(
upb_Message* msg, const upb_MiniTable* map_entry_mini_table,
const upb_MiniTableField* field, upb_Arena* arena) {
UPB_ASSUME(upb_MiniTableField_CType(field) == kUpb_CType_Message);
const upb_MiniTableField* map_entry_key_field =
&map_entry_mini_table->fields[0];
const upb_MiniTableField* map_entry_value_field =
&map_entry_mini_table->fields[1];
return _upb_Message_GetOrCreateMutableMap(
msg, field,
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_key_field)),
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_value_field)),
arena);
}
const upb_MiniTableField* f, upb_Arena* arena);
UPB_API_INLINE upb_Message* upb_Message_GetOrCreateMutableMessage(
upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* f, upb_Arena* arena);
UPB_API_INLINE upb_StringView
upb_Message_GetString(const upb_Message* msg, const upb_MiniTableField* field,
upb_StringView default_val);
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const upb_Message* msg,
const upb_MiniTableField* f,
uint32_t default_val);
UPB_API_INLINE uint64_t upb_Message_GetUInt64(const upb_Message* msg,
const upb_MiniTableField* f,
uint64_t default_val);
UPB_API_INLINE void upb_Message_SetClosedEnum(
upb_Message* msg, const upb_MiniTable* msg_mini_table,
const upb_MiniTableField* f, int32_t value);
// BaseField Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE void upb_Message_SetBaseField(upb_Message* msg,
const upb_MiniTableField* f,
const void* val);
UPB_API_INLINE void upb_Message_SetBaseFieldBool(struct upb_Message* msg,
const upb_MiniTableField* f,
bool value);
UPB_API_INLINE void upb_Message_SetBaseFieldDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value);
UPB_API_INLINE void upb_Message_SetBaseFieldFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value);
UPB_API_INLINE void upb_Message_SetBaseFieldInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value);
UPB_API_INLINE void upb_Message_SetBaseFieldInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value);
UPB_API_INLINE void upb_Message_SetBaseFieldMessage(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_Message* value);
UPB_API_INLINE void upb_Message_SetBaseFieldString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value);
UPB_API_INLINE void upb_Message_SetBaseFieldUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value);
UPB_API_INLINE void upb_Message_SetBaseFieldUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value);
// Extension Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetExtension(upb_Message* msg,
const upb_MiniTableExtension* e,
const void* value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionBool(
struct upb_Message* msg, const upb_MiniTableExtension* e, bool value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionDouble(
struct upb_Message* msg, const upb_MiniTableExtension* e, double value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionFloat(
struct upb_Message* msg, const upb_MiniTableExtension* e, float value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionString(
struct upb_Message* msg, const upb_MiniTableExtension* e,
upb_StringView value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionUInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetExtensionUInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value,
upb_Arena* a);
// Universal Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetBool(upb_Message* msg,
const upb_MiniTableField* f, bool value,
upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetDouble(upb_Message* msg,
const upb_MiniTableField* f,
double value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetFloat(upb_Message* msg,
const upb_MiniTableField* f,
float value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetInt32(upb_Message* msg,
const upb_MiniTableField* f,
int32_t value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetInt64(upb_Message* msg,
const upb_MiniTableField* f,
int64_t value, upb_Arena* a);
// Unlike the other similarly-named setters, this function can only be
// called on base fields. Prefer upb_Message_SetBaseFieldMessage().
UPB_API_INLINE void upb_Message_SetMessage(upb_Message* msg,
const upb_MiniTableField* f,
upb_Message* value);
UPB_API_INLINE bool upb_Message_SetString(upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetUInt32(upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value, upb_Arena* a);
UPB_API_INLINE bool upb_Message_SetUInt64(upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value, upb_Arena* a);
////////////////////////////////////////////////////////////////////////////////
UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
upb_Message* msg, const upb_MiniTableField* f, size_t size,
upb_Arena* arena);
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const upb_Message* message, const upb_MiniTableField* oneof_field);
// For a field `f` which is in a oneof, return the field of that
// oneof that is actually set (or NULL if none).
UPB_API_INLINE const upb_MiniTableField* upb_Message_WhichOneof(
const upb_Message* msg, const upb_MiniTable* m,
const upb_MiniTableField* f);
// Updates a map entry given an entry message.
upb_MapInsertStatus upb_Message_InsertMapEntry(upb_Map* map,
const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* map_entry_message,
upb_Arena* arena);
// Compares two messages by serializing them and calling memcmp().
bool upb_Message_IsExactlyEqual(const upb_Message* m1, const upb_Message* m2,
const upb_MiniTable* layout);
bool upb_Message_SetMapEntry(upb_Map* map, const upb_MiniTable* mini_table,
const upb_MiniTableField* field,
upb_Message* map_entry_message, upb_Arena* arena);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_ARRAY_H_
#define UPB_MESSAGE_ARRAY_H_
@@ -35,7 +12,10 @@
#include "upb/base/descriptor_constants.h"
#include "upb/mem/arena.h"
#include "upb/message/value.h" // IWYU pragma: export
#include "upb/message/internal/array.h"
#include "upb/message/value.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -50,11 +30,15 @@ extern "C" {
UPB_API upb_Array* upb_Array_New(upb_Arena* a, upb_CType type);
// Returns the number of elements in the array.
UPB_API size_t upb_Array_Size(const upb_Array* arr);
UPB_API_INLINE size_t upb_Array_Size(const upb_Array* arr);
// Returns the given element, which must be within the array's current size.
UPB_API upb_MessageValue upb_Array_Get(const upb_Array* arr, size_t i);
// Returns a mutating pointer to the given element, which must be within the
// array's current size.
UPB_API upb_MutableMessageValue upb_Array_GetMutable(upb_Array* arr, size_t i);
// Sets the given element, which must be within the array's current size.
UPB_API void upb_Array_Set(upb_Array* arr, size_t i, upb_MessageValue val);
@@ -79,15 +63,27 @@ UPB_API bool upb_Array_Insert(upb_Array* array, size_t i, size_t count,
// REQUIRES: `i + count <= upb_Array_Size(arr)`
UPB_API void upb_Array_Delete(upb_Array* array, size_t i, size_t count);
// Reserves |size| elements of storage for the array.
UPB_API_INLINE bool upb_Array_Reserve(struct upb_Array* array, size_t size,
upb_Arena* arena);
// Changes the size of a vector. New elements are initialized to NULL/0.
// Returns false on allocation failure.
UPB_API bool upb_Array_Resize(upb_Array* array, size_t size, upb_Arena* arena);
// Returns pointer to array data.
UPB_API const void* upb_Array_DataPtr(const upb_Array* arr);
UPB_API_INLINE const void* upb_Array_DataPtr(const upb_Array* arr);
// Returns mutable pointer to array data.
UPB_API void* upb_Array_MutableDataPtr(upb_Array* arr);
UPB_API_INLINE void* upb_Array_MutableDataPtr(upb_Array* arr);
// Mark an array and all of its descendents as frozen/immutable.
// If the array elements are messages then |m| must point to the minitable for
// those messages. Otherwise |m| must be NULL.
UPB_API void upb_Array_Freeze(upb_Array* arr, const upb_MiniTable* m);
// Returns whether an array has been frozen.
UPB_API_INLINE bool upb_Array_IsFrozen(const upb_Array* arr);
#ifdef __cplusplus
} /* extern "C" */

41
Pods/gRPC-C++/third_party/upb/upb/message/compat.h generated vendored Normal file
View File

@@ -0,0 +1,41 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_COMPAT_H_
#define UPB_MESSAGE_COMPAT_H_
#include <stdint.h>
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
// Must be last.
#include "upb/port/def.inc"
// upb does not support mixing minitables from different sources but these
// functions are still used by some existing users so for now we make them
// available here. This may or may not change in the future so do not add
// them to new code.
#ifdef __cplusplus
extern "C" {
#endif
const upb_MiniTableExtension* upb_Message_ExtensionByIndex(
const upb_Message* msg, size_t index);
// Returns the minitable with the given field number, or NULL on failure.
const upb_MiniTableExtension* upb_Message_FindExtensionByNumber(
const upb_Message* msg, uint32_t field_number);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_COMPAT_H_ */

56
Pods/gRPC-C++/third_party/upb/upb/message/copy.h generated vendored Normal file
View File

@@ -0,0 +1,56 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_COPY_H_
#define UPB_MESSAGE_COPY_H_
#include "upb/mem/arena.h"
#include "upb/message/array.h"
#include "upb/message/map.h"
#include "upb/message/message.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Deep clones a message using the provided target arena.
upb_Message* upb_Message_DeepClone(const upb_Message* msg,
const upb_MiniTable* m, upb_Arena* arena);
// Shallow clones a message using the provided target arena.
upb_Message* upb_Message_ShallowClone(const upb_Message* msg,
const upb_MiniTable* m, upb_Arena* arena);
// Deep clones array contents.
upb_Array* upb_Array_DeepClone(const upb_Array* array, upb_CType value_type,
const upb_MiniTable* sub, upb_Arena* arena);
// Deep clones map contents.
upb_Map* upb_Map_DeepClone(const upb_Map* map, upb_CType key_type,
upb_CType value_type,
const upb_MiniTable* map_entry_table,
upb_Arena* arena);
// Deep copies the message from src to dst.
bool upb_Message_DeepCopy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* m, upb_Arena* arena);
// Shallow copies the message from src to dst.
void upb_Message_ShallowCopy(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* m);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif // UPB_MESSAGE_COPY_H_

View File

@@ -8,10 +8,26 @@
#ifndef UPB_MESSAGE_INTERNAL_ACCESSORS_H_
#define UPB_MESSAGE_INTERNAL_ACCESSORS_H_
#include <stddef.h>
#include <stdint.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/internal/endian.h"
#include "upb/base/string_view.h"
#include "upb/mem/arena.h"
#include "upb/message/internal/array.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/message.h"
#include "upb/message/internal/tagged_ptr.h"
#include "upb/message/internal/types.h"
#include "upb/message/value.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -42,101 +58,114 @@ extern "C" {
// Hasbit access ///////////////////////////////////////////////////////////////
UPB_INLINE size_t _upb_hasbit_ofs(size_t idx) { return idx / 8; }
UPB_INLINE bool UPB_PRIVATE(_upb_Message_GetHasbit)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
UPB_INLINE char _upb_hasbit_mask(size_t idx) { return 1 << (idx % 8); }
UPB_INLINE bool _upb_hasbit(const upb_Message* msg, size_t idx) {
return (*UPB_PTR_AT(msg, _upb_hasbit_ofs(idx), const char) &
_upb_hasbit_mask(idx)) != 0;
return (*UPB_PTR_AT(msg, offset, const char) & mask) != 0;
}
UPB_INLINE void _upb_sethas(const upb_Message* msg, size_t idx) {
(*UPB_PTR_AT(msg, _upb_hasbit_ofs(idx), char)) |= _upb_hasbit_mask(idx);
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetHasbit)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
(*UPB_PTR_AT(msg, offset, char)) |= mask;
}
UPB_INLINE void _upb_clearhas(const upb_Message* msg, size_t idx) {
(*UPB_PTR_AT(msg, _upb_hasbit_ofs(idx), char)) &= ~_upb_hasbit_mask(idx);
}
UPB_INLINE void UPB_PRIVATE(_upb_Message_ClearHasbit)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
const size_t offset = UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(f);
const char mask = UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(f);
UPB_INLINE size_t _upb_Message_Hasidx(const upb_MiniTableField* f) {
UPB_ASSERT(f->presence > 0);
return f->presence;
}
UPB_INLINE bool _upb_hasbit_field(const upb_Message* msg,
const upb_MiniTableField* f) {
return _upb_hasbit(msg, _upb_Message_Hasidx(f));
}
UPB_INLINE void _upb_sethas_field(const upb_Message* msg,
const upb_MiniTableField* f) {
_upb_sethas(msg, _upb_Message_Hasidx(f));
(*UPB_PTR_AT(msg, offset, char)) &= ~mask;
}
// Oneof case access ///////////////////////////////////////////////////////////
UPB_INLINE size_t _upb_oneofcase_ofs(const upb_MiniTableField* f) {
UPB_ASSERT(f->presence < 0);
return ~(ptrdiff_t)f->presence;
UPB_INLINE uint32_t* UPB_PRIVATE(_upb_Message_OneofCasePtr)(
struct upb_Message* msg, const upb_MiniTableField* f) {
return UPB_PTR_AT(msg, UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(f),
uint32_t);
}
UPB_INLINE uint32_t* _upb_oneofcase_field(upb_Message* msg,
const upb_MiniTableField* f) {
return UPB_PTR_AT(msg, _upb_oneofcase_ofs(f), uint32_t);
UPB_INLINE uint32_t UPB_PRIVATE(_upb_Message_GetOneofCase)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
const uint32_t* ptr =
UPB_PRIVATE(_upb_Message_OneofCasePtr)((struct upb_Message*)msg, f);
return *ptr;
}
UPB_INLINE uint32_t _upb_getoneofcase_field(const upb_Message* msg,
const upb_MiniTableField* f) {
return *_upb_oneofcase_field((upb_Message*)msg, f);
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetOneofCase)(
struct upb_Message* msg, const upb_MiniTableField* f) {
uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
*ptr = upb_MiniTableField_Number(f);
}
// Returns true if the given field is the current oneof case.
// Does nothing if it is not the current oneof case.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_ClearOneofCase)(
struct upb_Message* msg, const upb_MiniTableField* f) {
uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
if (*ptr != upb_MiniTableField_Number(f)) return false;
*ptr = 0;
return true;
}
UPB_API_INLINE uint32_t upb_Message_WhichOneofFieldNumber(
const struct upb_Message* message, const upb_MiniTableField* oneof_field) {
UPB_ASSUME(upb_MiniTableField_IsInOneof(oneof_field));
return UPB_PRIVATE(_upb_Message_GetOneofCase)(message, oneof_field);
}
UPB_API_INLINE const upb_MiniTableField* upb_Message_WhichOneof(
const struct upb_Message* msg, const upb_MiniTable* m,
const upb_MiniTableField* f) {
uint32_t field_number = upb_Message_WhichOneofFieldNumber(msg, f);
if (field_number == 0) {
// No field in the oneof is set.
return NULL;
}
return upb_MiniTable_FindFieldByNumber(m, field_number);
}
// LINT.ThenChange(GoogleInternalName2)
UPB_INLINE bool _upb_MiniTableField_InOneOf(const upb_MiniTableField* field) {
return field->presence < 0;
// Returns false if the message is missing any of its required fields.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_IsInitializedShallow)(
const struct upb_Message* msg, const upb_MiniTable* m) {
uint64_t bits;
memcpy(&bits, msg + 1, sizeof(bits));
bits = upb_BigEndian64(bits);
return (UPB_PRIVATE(_upb_MiniTable_RequiredMask)(m) & ~bits) == 0;
}
UPB_INLINE void* _upb_MiniTableField_GetPtr(upb_Message* msg,
const upb_MiniTableField* field) {
return (char*)msg + field->offset;
UPB_INLINE void* UPB_PRIVATE(_upb_Message_MutableDataPtr)(
struct upb_Message* msg, const upb_MiniTableField* f) {
return (char*)msg + f->UPB_ONLYBITS(offset);
}
UPB_INLINE const void* _upb_MiniTableField_GetConstPtr(
const upb_Message* msg, const upb_MiniTableField* field) {
return (char*)msg + field->offset;
UPB_INLINE const void* UPB_PRIVATE(_upb_Message_DataPtr)(
const struct upb_Message* msg, const upb_MiniTableField* f) {
return (const char*)msg + f->UPB_ONLYBITS(offset);
}
UPB_INLINE void _upb_Message_SetPresence(upb_Message* msg,
const upb_MiniTableField* field) {
if (field->presence > 0) {
_upb_sethas_field(msg, field);
} else if (_upb_MiniTableField_InOneOf(field)) {
*_upb_oneofcase_field(msg, field) = field->number;
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetPresence)(
struct upb_Message* msg, const upb_MiniTableField* f) {
if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
UPB_PRIVATE(_upb_Message_SetHasbit)(msg, f);
} else if (upb_MiniTableField_IsInOneof(f)) {
UPB_PRIVATE(_upb_Message_SetOneofCase)(msg, f);
}
}
UPB_INLINE bool _upb_MiniTable_ValueIsNonZero(const void* default_val,
const upb_MiniTableField* field) {
char zero[16] = {0};
switch (_upb_MiniTableField_GetRep(field)) {
case kUpb_FieldRep_1Byte:
return memcmp(&zero, default_val, 1) != 0;
case kUpb_FieldRep_4Byte:
return memcmp(&zero, default_val, 4) != 0;
case kUpb_FieldRep_8Byte:
return memcmp(&zero, default_val, 8) != 0;
case kUpb_FieldRep_StringView: {
const upb_StringView* sv = (const upb_StringView*)default_val;
return sv->size != 0;
}
}
UPB_UNREACHABLE();
}
UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from,
const upb_MiniTableField* field) {
switch (_upb_MiniTableField_GetRep(field)) {
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataCopy)(
const upb_MiniTableField* f, void* to, const void* from) {
switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
case kUpb_FieldRep_1Byte:
memcpy(to, from, 1);
return;
@@ -154,30 +183,34 @@ UPB_INLINE void _upb_MiniTable_CopyFieldData(void* to, const void* from,
UPB_UNREACHABLE();
}
UPB_INLINE size_t
_upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
const unsigned char table[] = {
0,
3, // kUpb_FieldType_Double = 1,
2, // kUpb_FieldType_Float = 2,
3, // kUpb_FieldType_Int64 = 3,
3, // kUpb_FieldType_UInt64 = 4,
2, // kUpb_FieldType_Int32 = 5,
3, // kUpb_FieldType_Fixed64 = 6,
2, // kUpb_FieldType_Fixed32 = 7,
0, // kUpb_FieldType_Bool = 8,
UPB_SIZE(3, 4), // kUpb_FieldType_String = 9,
UPB_SIZE(2, 3), // kUpb_FieldType_Group = 10,
UPB_SIZE(2, 3), // kUpb_FieldType_Message = 11,
UPB_SIZE(3, 4), // kUpb_FieldType_Bytes = 12,
2, // kUpb_FieldType_UInt32 = 13,
2, // kUpb_FieldType_Enum = 14,
2, // kUpb_FieldType_SFixed32 = 15,
3, // kUpb_FieldType_SFixed64 = 16,
2, // kUpb_FieldType_SInt32 = 17,
3, // kUpb_FieldType_SInt64 = 18,
};
return table[field->UPB_PRIVATE(descriptortype)];
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataEquals)(
const upb_MiniTableField* f, const void* a, const void* b) {
switch (UPB_PRIVATE(_upb_MiniTableField_GetRep)(f)) {
case kUpb_FieldRep_1Byte:
return memcmp(a, b, 1) == 0;
case kUpb_FieldRep_4Byte:
return memcmp(a, b, 4) == 0;
case kUpb_FieldRep_8Byte:
return memcmp(a, b, 8) == 0;
case kUpb_FieldRep_StringView: {
const upb_StringView sa = *(const upb_StringView*)a;
const upb_StringView sb = *(const upb_StringView*)b;
return upb_StringView_IsEqual(sa, sb);
}
}
UPB_UNREACHABLE();
}
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_DataClear)(
const upb_MiniTableField* f, void* val) {
const char zero[16] = {0};
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, zero);
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(
const upb_MiniTableField* f, const void* val) {
const char zero[16] = {0};
return UPB_PRIVATE(_upb_MiniTableField_DataEquals)(f, val, zero);
}
// Here we define universal getter/setter functions for message fields.
@@ -190,7 +223,7 @@ _upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
// bool FooMessage_set_bool_field(const upb_Message* msg, bool val) {
// const upb_MiniTableField field = {1, 0, 0, /* etc... */};
// // All value in "field" are compile-time known.
// _upb_Message_SetNonExtensionField(msg, &field, &value);
// upb_Message_SetBaseField(msg, &field, &value);
// }
//
// // Via UPB_ASSUME().
@@ -198,9 +231,9 @@ _upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
// const upb_MiniTableField* field,
// bool value, upb_Arena* a) {
// UPB_ASSUME(field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bool);
// UPB_ASSUME(!upb_IsRepeatedOrMap(field));
// UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_1Byte);
// _upb_Message_SetField(msg, field, &value, a);
// UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(field) ==
// kUpb_FieldRep_1Byte);
// upb_Message_SetField(msg, field, &value, a);
// }
//
// As a result, we can use these universal getters/setters for *all* message
@@ -212,148 +245,677 @@ _upb_MiniTable_ElementSizeLg2(const upb_MiniTableField* field) {
// of a setter is known to be a non-extension, the arena may be NULL and the
// returned bool value may be ignored since it will always succeed.
UPB_INLINE bool _upb_Message_HasExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* ext) {
UPB_ASSERT(upb_MiniTableField_HasPresence(&ext->field));
return _upb_Message_Getext(msg, ext) != NULL;
}
UPB_INLINE bool _upb_Message_HasNonExtensionField(
const upb_Message* msg, const upb_MiniTableField* field) {
UPB_API_INLINE bool upb_Message_HasBaseField(const struct upb_Message* msg,
const upb_MiniTableField* field) {
UPB_ASSERT(upb_MiniTableField_HasPresence(field));
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
if (_upb_MiniTableField_InOneOf(field)) {
return _upb_getoneofcase_field(msg, field) == field->number;
if (upb_MiniTableField_IsInOneof(field)) {
return UPB_PRIVATE(_upb_Message_GetOneofCase)(msg, field) ==
upb_MiniTableField_Number(field);
} else {
return _upb_hasbit_field(msg, field);
return UPB_PRIVATE(_upb_Message_GetHasbit)(msg, field);
}
}
static UPB_FORCEINLINE void _upb_Message_GetNonExtensionField(
const upb_Message* msg, const upb_MiniTableField* field,
UPB_API_INLINE bool upb_Message_HasExtension(const struct upb_Message* msg,
const upb_MiniTableExtension* e) {
UPB_ASSERT(upb_MiniTableField_HasPresence(&e->UPB_PRIVATE(field)));
return UPB_PRIVATE(_upb_Message_Getext)(msg, e) != NULL;
}
UPB_FORCEINLINE void _upb_Message_GetNonExtensionField(
const struct upb_Message* msg, const upb_MiniTableField* field,
const void* default_val, void* val) {
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
if ((_upb_MiniTableField_InOneOf(field) ||
_upb_MiniTable_ValueIsNonZero(default_val, field)) &&
!_upb_Message_HasNonExtensionField(msg, field)) {
_upb_MiniTable_CopyFieldData(val, default_val, field);
if ((upb_MiniTableField_IsInOneof(field) ||
!UPB_PRIVATE(_upb_MiniTableField_DataIsZero)(field, default_val)) &&
!upb_Message_HasBaseField(msg, field)) {
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(field, val, default_val);
return;
}
_upb_MiniTable_CopyFieldData(val, _upb_MiniTableField_GetConstPtr(msg, field),
field);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(field, val, UPB_PRIVATE(_upb_Message_DataPtr)(msg, field));
}
UPB_INLINE void _upb_Message_GetExtensionField(
const upb_Message* msg, const upb_MiniTableExtension* mt_ext,
const struct upb_Message* msg, const upb_MiniTableExtension* mt_ext,
const void* default_val, void* val) {
UPB_ASSUME(upb_MiniTableField_IsExtension(&mt_ext->field));
const upb_Message_Extension* ext = _upb_Message_Getext(msg, mt_ext);
const upb_Extension* ext = UPB_PRIVATE(_upb_Message_Getext)(msg, mt_ext);
const upb_MiniTableField* f = &mt_ext->UPB_PRIVATE(field);
UPB_ASSUME(upb_MiniTableField_IsExtension(f));
if (ext) {
_upb_MiniTable_CopyFieldData(val, &ext->data, &mt_ext->field);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, &ext->data);
} else {
_upb_MiniTable_CopyFieldData(val, default_val, &mt_ext->field);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)(f, val, default_val);
}
}
UPB_INLINE void _upb_Message_GetField(const upb_Message* msg,
const upb_MiniTableField* field,
const void* default_val, void* val) {
// NOTE: The default_val is only used for fields that support presence.
// For repeated/map fields, the resulting upb_Array*/upb_Map* can be NULL if a
// upb_Array/upb_Map has not been allocated yet. Array/map fields do not have
// presence, so this is semantically identical to a pointer to an empty
// array/map, and must be treated the same for all semantic purposes.
UPB_API_INLINE upb_MessageValue upb_Message_GetField(
const struct upb_Message* msg, const upb_MiniTableField* field,
upb_MessageValue default_val) {
upb_MessageValue ret;
if (upb_MiniTableField_IsExtension(field)) {
_upb_Message_GetExtensionField(msg, (upb_MiniTableExtension*)field,
default_val, val);
&default_val, &ret);
} else {
_upb_Message_GetNonExtensionField(msg, field, default_val, val);
_upb_Message_GetNonExtensionField(msg, field, &default_val, &ret);
}
return ret;
}
UPB_INLINE void _upb_Message_SetNonExtensionField(
upb_Message* msg, const upb_MiniTableField* field, const void* val) {
UPB_ASSUME(!upb_MiniTableField_IsExtension(field));
_upb_Message_SetPresence(msg, field);
_upb_MiniTable_CopyFieldData(_upb_MiniTableField_GetPtr(msg, field), val,
field);
UPB_API_INLINE void upb_Message_SetBaseField(struct upb_Message* msg,
const upb_MiniTableField* f,
const void* val) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), val);
}
UPB_INLINE bool _upb_Message_SetExtensionField(
upb_Message* msg, const upb_MiniTableExtension* mt_ext, const void* val,
upb_Arena* a) {
UPB_API_INLINE bool upb_Message_SetExtension(struct upb_Message* msg,
const upb_MiniTableExtension* e,
const void* val, upb_Arena* a) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
UPB_ASSERT(a);
upb_Message_Extension* ext =
_upb_Message_GetOrCreateExtension(msg, mt_ext, a);
upb_Extension* ext =
UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(msg, e, a);
if (!ext) return false;
_upb_MiniTable_CopyFieldData(&ext->data, val, &mt_ext->field);
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(&e->UPB_PRIVATE(field), &ext->data, val);
return true;
}
UPB_INLINE bool _upb_Message_SetField(upb_Message* msg,
const upb_MiniTableField* field,
const void* val, upb_Arena* a) {
if (upb_MiniTableField_IsExtension(field)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)field;
return _upb_Message_SetExtensionField(msg, ext, val, a);
// Sets the value of the given field in the given msg. The return value is true
// if the operation completed successfully, or false if memory allocation
// failed.
UPB_INLINE bool UPB_PRIVATE(_upb_Message_SetField)(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_MessageValue val,
upb_Arena* a) {
if (upb_MiniTableField_IsExtension(f)) {
const upb_MiniTableExtension* ext = (const upb_MiniTableExtension*)f;
return upb_Message_SetExtension(msg, ext, &val, a);
} else {
_upb_Message_SetNonExtensionField(msg, field, val);
upb_Message_SetBaseField(msg, f, &val);
return true;
}
}
UPB_INLINE void _upb_Message_ClearExtensionField(
upb_Message* msg, const upb_MiniTableExtension* ext_l) {
upb_Message_Internal* in = upb_Message_Getinternal(msg);
if (!in->internal) return;
const upb_Message_Extension* base =
UPB_PTR_AT(in->internal, in->internal->ext_begin, upb_Message_Extension);
upb_Message_Extension* ext =
(upb_Message_Extension*)_upb_Message_Getext(msg, ext_l);
if (ext) {
*ext = *base;
in->internal->ext_begin += sizeof(upb_Message_Extension);
}
UPB_API_INLINE const upb_Array* upb_Message_GetArray(
const struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* ret;
const upb_Array* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, f, &default_val, &ret);
return ret;
}
UPB_INLINE void _upb_Message_ClearNonExtensionField(
upb_Message* msg, const upb_MiniTableField* field) {
if (field->presence > 0) {
_upb_clearhas(msg, _upb_Message_Hasidx(field));
} else if (_upb_MiniTableField_InOneOf(field)) {
uint32_t* oneof_case = _upb_oneofcase_field(msg, field);
if (*oneof_case != field->number) return;
*oneof_case = 0;
}
const char zeros[16] = {0};
_upb_MiniTable_CopyFieldData(_upb_MiniTableField_GetPtr(msg, field), zeros,
field);
UPB_API_INLINE bool upb_Message_GetBool(const struct upb_Message* msg,
const upb_MiniTableField* f,
bool default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
upb_MessageValue def;
def.bool_val = default_val;
return upb_Message_GetField(msg, f, def).bool_val;
}
UPB_INLINE void _upb_Message_AssertMapIsUntagged(
const upb_Message* msg, const upb_MiniTableField* field) {
UPB_API_INLINE double upb_Message_GetDouble(const struct upb_Message* msg,
const upb_MiniTableField* f,
double default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.double_val = default_val;
return upb_Message_GetField(msg, f, def).double_val;
}
UPB_API_INLINE float upb_Message_GetFloat(const struct upb_Message* msg,
const upb_MiniTableField* f,
float default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.float_val = default_val;
return upb_Message_GetField(msg, f, def).float_val;
}
UPB_API_INLINE int32_t upb_Message_GetInt32(const struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.int32_val = default_val;
return upb_Message_GetField(msg, f, def).int32_val;
}
UPB_API_INLINE int64_t upb_Message_GetInt64(const struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.int64_val = default_val;
return upb_Message_GetField(msg, f, def).int64_val;
}
UPB_INLINE void UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(
const struct upb_Message* msg, const upb_MiniTableField* field) {
UPB_UNUSED(msg);
_upb_MiniTableField_CheckIsMap(field);
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
#ifndef NDEBUG
upb_TaggedMessagePtr default_val = 0;
upb_TaggedMessagePtr tagged;
uintptr_t default_val = 0;
uintptr_t tagged;
_upb_Message_GetNonExtensionField(msg, field, &default_val, &tagged);
UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(tagged));
#endif
}
UPB_INLINE upb_Map* _upb_Message_GetOrCreateMutableMap(
upb_Message* msg, const upb_MiniTableField* field, size_t key_size,
UPB_API_INLINE const struct upb_Map* upb_Message_GetMap(
const struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(f);
UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(msg, f);
struct upb_Map* ret;
const struct upb_Map* default_val = NULL;
_upb_Message_GetNonExtensionField(msg, f, &default_val, &ret);
return ret;
}
UPB_API_INLINE uintptr_t upb_Message_GetTaggedMessagePtr(
const struct upb_Message* msg, const upb_MiniTableField* f,
struct upb_Message* default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
uintptr_t tagged;
_upb_Message_GetNonExtensionField(msg, f, &default_val, &tagged);
return tagged;
}
// For internal use only; users cannot set tagged messages because only the
// parser and the message copier are allowed to directly create an empty
// message.
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)(
struct upb_Message* msg, const upb_MiniTableField* f,
uintptr_t sub_message) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
UPB_SIZE(kUpb_FieldRep_4Byte, kUpb_FieldRep_8Byte));
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
upb_Message_SetBaseField(msg, f, &sub_message);
}
UPB_API_INLINE const struct upb_Message* upb_Message_GetMessage(
const struct upb_Message* msg, const upb_MiniTableField* f) {
uintptr_t tagged = upb_Message_GetTaggedMessagePtr(msg, f, NULL);
return upb_TaggedMessagePtr_GetNonEmptyMessage(tagged);
}
UPB_API_INLINE upb_Array* upb_Message_GetMutableArray(
struct upb_Message* msg, const upb_MiniTableField* f) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
return (upb_Array*)upb_Message_GetArray(msg, f);
}
UPB_API_INLINE struct upb_Map* upb_Message_GetMutableMap(
struct upb_Message* msg, const upb_MiniTableField* f) {
return (struct upb_Map*)upb_Message_GetMap(msg, f);
}
UPB_API_INLINE struct upb_Message* upb_Message_GetMutableMessage(
struct upb_Message* msg, const upb_MiniTableField* f) {
return (struct upb_Message*)upb_Message_GetMessage(msg, f);
}
UPB_API_INLINE upb_Array* upb_Message_GetOrCreateMutableArray(
struct upb_Message* msg, const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* array = upb_Message_GetMutableArray(msg, f);
if (!array) {
array = UPB_PRIVATE(_upb_Array_New)(
arena, 4, UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(f));
// Check again due to: https://godbolt.org/z/7WfaoKG1r
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_MessageValue val;
val.array_val = array;
UPB_PRIVATE(_upb_Message_SetField)(msg, f, val, arena);
}
return array;
}
UPB_INLINE struct upb_Map* _upb_Message_GetOrCreateMutableMap(
struct upb_Message* msg, const upb_MiniTableField* field, size_t key_size,
size_t val_size, upb_Arena* arena) {
_upb_MiniTableField_CheckIsMap(field);
_upb_Message_AssertMapIsUntagged(msg, field);
upb_Map* map = NULL;
upb_Map* default_map_value = NULL;
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
UPB_PRIVATE(_upb_Message_AssertMapIsUntagged)(msg, field);
struct upb_Map* map = NULL;
struct upb_Map* default_map_value = NULL;
_upb_Message_GetNonExtensionField(msg, field, &default_map_value, &map);
if (!map) {
map = _upb_Map_New(arena, key_size, val_size);
// Check again due to: https://godbolt.org/z/7WfaoKG1r
_upb_MiniTableField_CheckIsMap(field);
_upb_Message_SetNonExtensionField(msg, field, &map);
UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(field);
upb_Message_SetBaseField(msg, field, &map);
}
return map;
}
UPB_API_INLINE struct upb_Map* upb_Message_GetOrCreateMutableMap(
struct upb_Message* msg, const upb_MiniTable* map_entry_mini_table,
const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
const upb_MiniTableField* map_entry_key_field =
&map_entry_mini_table->UPB_ONLYBITS(fields)[0];
const upb_MiniTableField* map_entry_value_field =
&map_entry_mini_table->UPB_ONLYBITS(fields)[1];
return _upb_Message_GetOrCreateMutableMap(
msg, f, _upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_key_field)),
_upb_Map_CTypeSize(upb_MiniTableField_CType(map_entry_value_field)),
arena);
}
UPB_API_INLINE struct upb_Message* upb_Message_GetOrCreateMutableMessage(
struct upb_Message* msg, const upb_MiniTable* mini_table,
const upb_MiniTableField* f, upb_Arena* arena) {
UPB_ASSERT(arena);
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
UPB_ASSUME(!upb_MiniTableField_IsExtension(f));
struct upb_Message* sub_message =
*UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*);
if (!sub_message) {
const upb_MiniTable* sub_mini_table =
upb_MiniTable_SubMessage(mini_table, f);
UPB_ASSERT(sub_mini_table);
sub_message = _upb_Message_New(sub_mini_table, arena);
*UPB_PTR_AT(msg, f->UPB_ONLYBITS(offset), struct upb_Message*) =
sub_message;
UPB_PRIVATE(_upb_Message_SetPresence)(msg, f);
}
return sub_message;
}
UPB_API_INLINE upb_StringView
upb_Message_GetString(const struct upb_Message* msg,
const upb_MiniTableField* f, upb_StringView default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
upb_MessageValue def;
def.str_val = default_val;
return upb_Message_GetField(msg, f, def).str_val;
}
UPB_API_INLINE uint32_t upb_Message_GetUInt32(const struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_MessageValue def;
def.uint32_val = default_val;
return upb_Message_GetField(msg, f, def).uint32_val;
}
UPB_API_INLINE uint64_t upb_Message_GetUInt64(const struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t default_val) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_MessageValue def;
def.uint64_val = default_val;
return upb_Message_GetField(msg, f, def).uint64_val;
}
// BaseField Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE void upb_Message_SetBaseFieldBool(struct upb_Message* msg,
const upb_MiniTableField* f,
bool value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Bool);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_1Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Double);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Float);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int32 ||
upb_MiniTableField_CType(f) == kUpb_CType_Enum);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Int64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldMessage(struct upb_Message* msg,
const upb_MiniTableField* f,
struct upb_Message* value) {
UPB_PRIVATE(_upb_Message_SetTaggedMessagePtr)
(msg, f, UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(value, false));
}
UPB_API_INLINE void upb_Message_SetBaseFieldString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_String ||
upb_MiniTableField_CType(f) == kUpb_CType_Bytes);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_StringView);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt32);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetBaseFieldUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_UInt64);
UPB_ASSUME(upb_MiniTableField_IsScalar(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_8Byte);
upb_Message_SetBaseField(msg, f, &value);
}
UPB_API_INLINE void upb_Message_SetClosedEnum(struct upb_Message* msg,
const upb_MiniTable* m,
const upb_MiniTableField* f,
int32_t value) {
UPB_ASSERT(upb_MiniTableField_IsClosedEnum(f));
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) == kUpb_FieldRep_4Byte);
UPB_ASSERT(
upb_MiniTableEnum_CheckValue(upb_MiniTable_GetSubEnumTable(m, f), value));
upb_Message_SetBaseField(msg, f, &value);
}
// Extension Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetExtensionBool(
struct upb_Message* msg, const upb_MiniTableExtension* e, bool value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Bool);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_1Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionDouble(
struct upb_Message* msg, const upb_MiniTableExtension* e, double value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Double);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionFloat(
struct upb_Message* msg, const upb_MiniTableExtension* e, float value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Float);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, int32_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int32 ||
upb_MiniTableExtension_CType(e) == kUpb_CType_Enum);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, int64_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_Int64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionString(
struct upb_Message* msg, const upb_MiniTableExtension* e,
upb_StringView value, upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_String ||
upb_MiniTableExtension_CType(e) == kUpb_CType_Bytes);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_StringView);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionUInt32(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint32_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt32);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_4Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
UPB_API_INLINE bool upb_Message_SetExtensionUInt64(
struct upb_Message* msg, const upb_MiniTableExtension* e, uint64_t value,
upb_Arena* a) {
UPB_ASSUME(upb_MiniTableExtension_CType(e) == kUpb_CType_UInt64);
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(e) ==
kUpb_FieldRep_8Byte);
return upb_Message_SetExtension(msg, e, &value, a);
}
// Universal Setters ///////////////////////////////////////////////////////////
UPB_API_INLINE bool upb_Message_SetBool(struct upb_Message* msg,
const upb_MiniTableField* f, bool value,
upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionBool(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldBool(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetDouble(struct upb_Message* msg,
const upb_MiniTableField* f,
double value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionDouble(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldDouble(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetFloat(struct upb_Message* msg,
const upb_MiniTableField* f,
float value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionFloat(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldFloat(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
int32_t value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionInt32(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldInt32(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
int64_t value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionInt64(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldInt64(msg, f, value), true);
}
// Sets the value of a message-typed field. The mini_tables of `msg` and
// `value` must have been linked for this to work correctly.
UPB_API_INLINE void upb_Message_SetMessage(struct upb_Message* msg,
const upb_MiniTableField* f,
struct upb_Message* value) {
UPB_ASSERT(!upb_MiniTableField_IsExtension(f));
upb_Message_SetBaseFieldMessage(msg, f, value);
}
// Sets the value of a `string` or `bytes` field. The bytes of the value are not
// copied, so it is the caller's responsibility to ensure that they remain valid
// for the lifetime of `msg`. That might be done by copying them into the given
// arena, or by fusing that arena with the arena the bytes live in, for example.
UPB_API_INLINE bool upb_Message_SetString(struct upb_Message* msg,
const upb_MiniTableField* f,
upb_StringView value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionString(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldString(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetUInt32(struct upb_Message* msg,
const upb_MiniTableField* f,
uint32_t value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionUInt32(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldUInt32(msg, f, value), true);
}
UPB_API_INLINE bool upb_Message_SetUInt64(struct upb_Message* msg,
const upb_MiniTableField* f,
uint64_t value, upb_Arena* a) {
return upb_MiniTableField_IsExtension(f)
? upb_Message_SetExtensionUInt64(
msg, (const upb_MiniTableExtension*)f, value, a)
: (upb_Message_SetBaseFieldUInt64(msg, f, value), true);
}
UPB_API_INLINE void upb_Message_Clear(struct upb_Message* msg,
const upb_MiniTable* m) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
memset(msg, 0, m->UPB_PRIVATE(size));
if (in) {
// Reset the internal buffer to empty.
in->unknown_end = sizeof(upb_Message_Internal);
in->ext_begin = in->size;
UPB_PRIVATE(_upb_Message_SetInternal)(msg, in);
}
}
UPB_API_INLINE void upb_Message_ClearBaseField(struct upb_Message* msg,
const upb_MiniTableField* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
if (UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f)) {
UPB_PRIVATE(_upb_Message_ClearHasbit)(msg, f);
} else if (upb_MiniTableField_IsInOneof(f)) {
uint32_t* ptr = UPB_PRIVATE(_upb_Message_OneofCasePtr)(msg, f);
if (*ptr != upb_MiniTableField_Number(f)) return;
*ptr = 0;
}
const char zeros[16] = {0};
UPB_PRIVATE(_upb_MiniTableField_DataCopy)
(f, UPB_PRIVATE(_upb_Message_MutableDataPtr)(msg, f), zeros);
}
UPB_API_INLINE void upb_Message_ClearExtension(
struct upb_Message* msg, const upb_MiniTableExtension* e) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
upb_Message_Internal* in = UPB_PRIVATE(_upb_Message_GetInternal)(msg);
if (!in) return;
const upb_Extension* base = UPB_PTR_AT(in, in->ext_begin, upb_Extension);
upb_Extension* ext = (upb_Extension*)UPB_PRIVATE(_upb_Message_Getext)(msg, e);
if (ext) {
*ext = *base;
in->ext_begin += sizeof(upb_Extension);
}
}
UPB_API_INLINE void upb_Message_ClearOneof(struct upb_Message* msg,
const upb_MiniTable* m,
const upb_MiniTableField* f) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
uint32_t field_number = upb_Message_WhichOneofFieldNumber(msg, f);
if (field_number == 0) {
// No field in the oneof is set.
return;
}
const upb_MiniTableField* field =
upb_MiniTable_FindFieldByNumber(m, field_number);
upb_Message_ClearBaseField(msg, field);
}
UPB_API_INLINE void* upb_Message_ResizeArrayUninitialized(
struct upb_Message* msg, const upb_MiniTableField* f, size_t size,
upb_Arena* arena) {
UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(f);
upb_Array* arr = upb_Message_GetOrCreateMutableArray(msg, f, arena);
if (!arr || !UPB_PRIVATE(_upb_Array_ResizeUninitialized)(arr, size, arena)) {
return NULL;
}
return upb_Array_MutableDataPtr(arr);
}
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -1,138 +1,145 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_INTERNAL_ARRAY_H_
#define UPB_MESSAGE_INTERNAL_ARRAY_H_
#include <stdint.h>
#include <string.h>
#include "upb/message/array.h"
#include "upb/mem/arena.h"
// Must be last.
#include "upb/port/def.inc"
#define _UPB_ARRAY_MASK_IMM 0x4 // Frozen/immutable bit.
#define _UPB_ARRAY_MASK_LG2 0x3 // Encoded elem size.
#define _UPB_ARRAY_MASK_ALL (_UPB_ARRAY_MASK_IMM | _UPB_ARRAY_MASK_LG2)
#ifdef __cplusplus
extern "C" {
#endif
// LINT.IfChange(struct_definition)
// LINT.IfChange(upb_Array)
// Our internal representation for repeated fields.
struct upb_Array {
uintptr_t data; /* Tagged ptr: low 3 bits of ptr are lg2(elem size). */
size_t size; /* The number of elements in the array. */
size_t capacity; /* Allocated storage. Measured in elements. */
// This is a tagged pointer. Bits #0 and #1 encode the elem size as follows:
// 0 maps to elem size 1
// 1 maps to elem size 4
// 2 maps to elem size 8
// 3 maps to elem size 16
//
// Bit #2 contains the frozen/immutable flag.
uintptr_t UPB_ONLYBITS(data);
size_t UPB_ONLYBITS(size); // The number of elements in the array.
size_t UPB_PRIVATE(capacity); // Allocated storage. Measured in elements.
};
// LINT.ThenChange(GoogleInternalName1)
UPB_INLINE size_t _upb_Array_ElementSizeLg2(const upb_Array* arr) {
size_t ret = arr->data & 7;
UPB_ASSERT(ret <= 4);
return ret;
UPB_INLINE void UPB_PRIVATE(_upb_Array_ShallowFreeze)(struct upb_Array* arr) {
arr->UPB_ONLYBITS(data) |= _UPB_ARRAY_MASK_IMM;
}
UPB_INLINE const void* _upb_array_constptr(const upb_Array* arr) {
_upb_Array_ElementSizeLg2(arr); // Check assertion.
return (void*)(arr->data & ~(uintptr_t)7);
UPB_API_INLINE bool upb_Array_IsFrozen(const struct upb_Array* arr) {
return (arr->UPB_ONLYBITS(data) & _UPB_ARRAY_MASK_IMM) != 0;
}
UPB_INLINE uintptr_t _upb_array_tagptr(void* ptr, int elem_size_lg2) {
UPB_INLINE void UPB_PRIVATE(_upb_Array_SetTaggedPtr)(struct upb_Array* array,
void* data, size_t lg2) {
UPB_ASSERT(lg2 != 1);
UPB_ASSERT(lg2 <= 4);
const size_t bits = lg2 - (lg2 != 0);
array->UPB_ONLYBITS(data) = (uintptr_t)data | bits;
}
UPB_INLINE size_t
UPB_PRIVATE(_upb_Array_ElemSizeLg2)(const struct upb_Array* array) {
const size_t bits = array->UPB_ONLYBITS(data) & _UPB_ARRAY_MASK_LG2;
const size_t lg2 = bits + (bits != 0);
return lg2;
}
UPB_API_INLINE const void* upb_Array_DataPtr(const struct upb_Array* array) {
UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array); // Check assertions.
return (void*)(array->UPB_ONLYBITS(data) & ~(uintptr_t)_UPB_ARRAY_MASK_ALL);
}
UPB_API_INLINE void* upb_Array_MutableDataPtr(struct upb_Array* array) {
return (void*)upb_Array_DataPtr(array);
}
UPB_INLINE struct upb_Array* UPB_PRIVATE(_upb_Array_New)(upb_Arena* arena,
size_t init_capacity,
int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 != 1);
UPB_ASSERT(elem_size_lg2 <= 4);
return (uintptr_t)ptr | elem_size_lg2;
}
UPB_INLINE void* _upb_array_ptr(upb_Array* arr) {
return (void*)_upb_array_constptr(arr);
}
UPB_INLINE uintptr_t _upb_tag_arrptr(void* ptr, int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 <= 4);
UPB_ASSERT(((uintptr_t)ptr & 7) == 0);
return (uintptr_t)ptr | (unsigned)elem_size_lg2;
}
extern const char _upb_Array_CTypeSizeLg2Table[];
UPB_INLINE size_t _upb_Array_CTypeSizeLg2(upb_CType ctype) {
return _upb_Array_CTypeSizeLg2Table[ctype];
}
UPB_INLINE upb_Array* _upb_Array_New(upb_Arena* a, size_t init_capacity,
int elem_size_lg2) {
UPB_ASSERT(elem_size_lg2 <= 4);
const size_t arr_size = UPB_ALIGN_UP(sizeof(upb_Array), UPB_MALLOC_ALIGN);
const size_t bytes = arr_size + (init_capacity << elem_size_lg2);
upb_Array* arr = (upb_Array*)upb_Arena_Malloc(a, bytes);
if (!arr) return NULL;
arr->data = _upb_tag_arrptr(UPB_PTR_AT(arr, arr_size, void), elem_size_lg2);
arr->size = 0;
arr->capacity = init_capacity;
return arr;
const size_t array_size =
UPB_ALIGN_UP(sizeof(struct upb_Array), UPB_MALLOC_ALIGN);
const size_t bytes = array_size + (init_capacity << elem_size_lg2);
struct upb_Array* array = (struct upb_Array*)upb_Arena_Malloc(arena, bytes);
if (!array) return NULL;
UPB_PRIVATE(_upb_Array_SetTaggedPtr)
(array, UPB_PTR_AT(array, array_size, void), elem_size_lg2);
array->UPB_ONLYBITS(size) = 0;
array->UPB_PRIVATE(capacity) = init_capacity;
return array;
}
// Resizes the capacity of the array to be at least min_size.
bool _upb_array_realloc(upb_Array* arr, size_t min_size, upb_Arena* arena);
bool UPB_PRIVATE(_upb_Array_Realloc)(struct upb_Array* array, size_t min_size,
upb_Arena* arena);
UPB_INLINE bool _upb_array_reserve(upb_Array* arr, size_t size,
upb_Arena* arena) {
if (arr->capacity < size) return _upb_array_realloc(arr, size, arena);
UPB_API_INLINE bool upb_Array_Reserve(struct upb_Array* array, size_t size,
upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(array));
if (array->UPB_PRIVATE(capacity) < size)
return UPB_PRIVATE(_upb_Array_Realloc)(array, size, arena);
return true;
}
// Resize without initializing new elements.
UPB_INLINE bool _upb_Array_ResizeUninitialized(upb_Array* arr, size_t size,
upb_Arena* arena) {
UPB_ASSERT(size <= arr->size || arena); // Allow NULL arena when shrinking.
if (!_upb_array_reserve(arr, size, arena)) return false;
arr->size = size;
UPB_INLINE bool UPB_PRIVATE(_upb_Array_ResizeUninitialized)(
struct upb_Array* array, size_t size, upb_Arena* arena) {
UPB_ASSERT(!upb_Array_IsFrozen(array));
UPB_ASSERT(size <= array->UPB_ONLYBITS(size) ||
arena); // Allow NULL arena when shrinking.
if (!upb_Array_Reserve(array, size, arena)) return false;
array->UPB_ONLYBITS(size) = size;
return true;
}
// This function is intended for situations where elem_size is compile-time
// constant or a known expression of the form (1 << lg2), so that the expression
// i*elem_size does not result in an actual multiplication.
UPB_INLINE void _upb_Array_Set(upb_Array* arr, size_t i, const void* data,
size_t elem_size) {
UPB_ASSERT(i < arr->size);
UPB_ASSERT(elem_size == 1U << _upb_Array_ElementSizeLg2(arr));
char* arr_data = (char*)_upb_array_ptr(arr);
UPB_INLINE void UPB_PRIVATE(_upb_Array_Set)(struct upb_Array* array, size_t i,
const void* data,
size_t elem_size) {
UPB_ASSERT(!upb_Array_IsFrozen(array));
UPB_ASSERT(i < array->UPB_ONLYBITS(size));
UPB_ASSERT(elem_size == 1U << UPB_PRIVATE(_upb_Array_ElemSizeLg2)(array));
char* arr_data = (char*)upb_Array_MutableDataPtr(array);
memcpy(arr_data + (i * elem_size), data, elem_size);
}
UPB_INLINE void _upb_array_detach(const void* msg, size_t ofs) {
*UPB_PTR_AT(msg, ofs, upb_Array*) = NULL;
UPB_API_INLINE size_t upb_Array_Size(const struct upb_Array* arr) {
return arr->UPB_ONLYBITS(size);
}
// LINT.ThenChange(GoogleInternalName0)
#ifdef __cplusplus
} /* extern "C" */
#endif
#undef _UPB_ARRAY_MASK_IMM
#undef _UPB_ARRAY_MASK_LG2
#undef _UPB_ARRAY_MASK_ALL
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_INTERNAL_ARRAY_H_ */

View File

@@ -0,0 +1,49 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_
#define UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_
#include <stddef.h>
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Returns true if unknown fields from the two messages are equal when sorted
// and varints are made canonical.
//
// This function is discouraged, as the comparison is inherently lossy without
// schema data:
//
// 1. We don't know whether delimited fields are sub-messages. Unknown
// sub-messages will therefore not have their fields sorted and varints
// canonicalized.
// 2. We don't know about oneof/non-repeated fields, which should semantically
// discard every value except the last.
typedef enum {
kUpb_UnknownCompareResult_Equal = 0,
kUpb_UnknownCompareResult_NotEqual = 1,
kUpb_UnknownCompareResult_OutOfMemory = 2,
kUpb_UnknownCompareResult_MaxDepthExceeded = 3,
} upb_UnknownCompareResult;
upb_UnknownCompareResult UPB_PRIVATE(_upb_Message_UnknownFieldsAreEqual)(
const char* buf1, size_t size1, const char* buf2, size_t size2,
int max_depth);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_INTERNAL_COMPARE_UNKNOWN_H_ */

View File

@@ -8,10 +8,10 @@
#ifndef UPB_MESSAGE_INTERNAL_EXTENSION_H_
#define UPB_MESSAGE_INTERNAL_EXTENSION_H_
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include <stddef.h>
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/message/value.h"
#include "upb/mini_table/extension.h"
// Must be last.
@@ -27,12 +27,8 @@
// the most common extension type.
typedef struct {
const upb_MiniTableExtension* ext;
union {
upb_StringView str;
void* ptr;
char scalar_data[8];
} data;
} upb_Message_Extension;
upb_MessageValue data;
} upb_Extension;
#ifdef __cplusplus
extern "C" {
@@ -41,18 +37,19 @@ extern "C" {
// Adds the given extension data to the given message.
// |ext| is copied into the message instance.
// This logically replaces any previously-added extension with this number.
upb_Message_Extension* _upb_Message_GetOrCreateExtension(
upb_Message* msg, const upb_MiniTableExtension* ext, upb_Arena* arena);
upb_Extension* UPB_PRIVATE(_upb_Message_GetOrCreateExtension)(
struct upb_Message* msg, const upb_MiniTableExtension* ext,
upb_Arena* arena);
// Returns an array of extensions for this message.
// Note: the array is ordered in reverse relative to the order of creation.
const upb_Message_Extension* _upb_Message_Getexts(const upb_Message* msg,
size_t* count);
const upb_Extension* UPB_PRIVATE(_upb_Message_Getexts)(
const struct upb_Message* msg, size_t* count);
// Returns an extension for the given field number, or NULL if no extension
// exists for this field number.
const upb_Message_Extension* _upb_Message_Getext(
const upb_Message* msg, const upb_MiniTableExtension* ext);
// Returns an extension for a message with a given mini table,
// or NULL if no extension exists with this mini table.
const upb_Extension* UPB_PRIVATE(_upb_Message_Getext)(
const struct upb_Message* msg, const upb_MiniTableExtension* ext);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -1,51 +1,38 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
#ifndef UPB_MESSAGE_INTERNAL_MAP_H_
#define UPB_MESSAGE_INTERNAL_MAP_H_
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_H_
#include <stddef.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/hash/str_table.h"
#include "upb/mem/arena.h"
#include "upb/message/map.h"
// Must be last.
#include "upb/port/def.inc"
typedef enum {
kUpb_MapInsertStatus_Inserted = 0,
kUpb_MapInsertStatus_Replaced = 1,
kUpb_MapInsertStatus_OutOfMemory = 2,
} upb_MapInsertStatus;
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
struct upb_Map {
// Size of key and val, based on the map type.
// Strings are represented as '0' because they must be handled specially.
char key_size;
char val_size;
bool UPB_PRIVATE(is_frozen);
upb_strtable table;
};
@@ -54,6 +41,14 @@ struct upb_Map {
extern "C" {
#endif
UPB_INLINE void UPB_PRIVATE(_upb_Map_ShallowFreeze)(struct upb_Map* map) {
map->UPB_PRIVATE(is_frozen) = true;
}
UPB_API_INLINE bool upb_Map_IsFrozen(const struct upb_Map* map) {
return map->UPB_PRIVATE(is_frozen);
}
// Converting between internal table representation and user values.
//
// _upb_map_tokey() and _upb_map_fromkey() are inverses.
@@ -100,7 +95,7 @@ UPB_INLINE void _upb_map_fromvalue(upb_value val, void* out, size_t size) {
}
}
UPB_INLINE void* _upb_map_next(const upb_Map* map, size_t* iter) {
UPB_INLINE void* _upb_map_next(const struct upb_Map* map, size_t* iter) {
upb_strtable_iter it;
it.t = &map->table;
it.index = *iter;
@@ -110,17 +105,21 @@ UPB_INLINE void* _upb_map_next(const upb_Map* map, size_t* iter) {
return (void*)str_tabent(&it);
}
UPB_INLINE void _upb_Map_Clear(upb_Map* map) {
UPB_INLINE void _upb_Map_Clear(struct upb_Map* map) {
UPB_ASSERT(!upb_Map_IsFrozen(map));
upb_strtable_clear(&map->table);
}
UPB_INLINE bool _upb_Map_Delete(upb_Map* map, const void* key, size_t key_size,
upb_value* val) {
UPB_INLINE bool _upb_Map_Delete(struct upb_Map* map, const void* key,
size_t key_size, upb_value* val) {
UPB_ASSERT(!upb_Map_IsFrozen(map));
upb_StringView k = _upb_map_tokey(key, key_size);
return upb_strtable_remove2(&map->table, k.data, k.size, val);
}
UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key,
UPB_INLINE bool _upb_Map_Get(const struct upb_Map* map, const void* key,
size_t key_size, void* val, size_t val_size) {
upb_value tabval;
upb_StringView k = _upb_map_tokey(key, key_size);
@@ -131,9 +130,12 @@ UPB_INLINE bool _upb_Map_Get(const upb_Map* map, const void* key,
return ret;
}
UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(upb_Map* map, const void* key,
size_t key_size, void* val,
size_t val_size, upb_Arena* a) {
UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(struct upb_Map* map,
const void* key, size_t key_size,
void* val, size_t val_size,
upb_Arena* a) {
UPB_ASSERT(!upb_Map_IsFrozen(map));
upb_StringView strkey = _upb_map_tokey(key, key_size);
upb_value tabval = {0};
if (!_upb_map_tovalue(val, val_size, &tabval, a)) {
@@ -150,7 +152,7 @@ UPB_INLINE upb_MapInsertStatus _upb_Map_Insert(upb_Map* map, const void* key,
: kUpb_MapInsertStatus_Inserted;
}
UPB_INLINE size_t _upb_Map_Size(const upb_Map* map) {
UPB_INLINE size_t _upb_Map_Size(const struct upb_Map* map) {
return map->table.t.count;
}
@@ -162,7 +164,7 @@ UPB_INLINE size_t _upb_Map_CTypeSize(upb_CType ctype) {
}
// Creates a new map on the given arena with this key/value type.
upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
struct upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
#ifdef __cplusplus
} /* extern "C" */
@@ -170,4 +172,4 @@ upb_Map* _upb_Map_New(upb_Arena* a, size_t key_size, size_t value_size);
#include "upb/port/undef.inc"
#endif /* UPB_COLLECTIONS_INTERNAL_MAP_H_ */
#endif /* UPB_MESSAGE_INTERNAL_MAP_H_ */

View File

@@ -1,66 +1,41 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#ifndef UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_
#define UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_
#include <stdint.h>
#include "upb/base/string_view.h"
#include "upb/hash/common.h"
#include "upb/message/internal/types.h"
// Map entries aren't actually stored for map fields, they are only used during
// parsing. For parsing, it helps a lot if all map entry messages have the same
// layout. The layout code in mini_table/decode.c will ensure that all map
// entries have this layout.
// parsing. (It helps a lot if all map entry messages have the same layout.)
// The mini_table layout code will ensure that all map entries have this layout.
//
// Note that users can and do create map entries directly, which will also use
// this layout.
//
// NOTE: sync with wire/decode.c.
typedef struct {
struct upb_Message message;
// We only need 2 hasbits max, but due to alignment we'll use 8 bytes here,
// and the uint64_t helps make this clear.
uint64_t hasbits;
union {
upb_StringView str; // For str/bytes.
upb_value val; // For all other types.
double d[2]; // Padding for 32-bit builds.
} k;
union {
upb_StringView str; // For str/bytes.
upb_value val; // For all other types.
double d[2]; // Padding for 32-bit builds.
} v;
} upb_MapEntryData;
typedef struct {
upb_Message_Internal internal;
upb_MapEntryData data;
} upb_MapEntry;
#endif // UPB_COLLECTIONS_INTERNAL_MAP_ENTRY_H_
#endif // UPB_MESSAGE_INTERNAL_MAP_ENTRY_H_

View File

@@ -1,40 +1,20 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// EVERYTHING BELOW THIS LINE IS INTERNAL - DO NOT USE /////////////////////////
#ifndef UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_
#define UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_
#ifndef UPB_MESSAGE_INTERNAL_MAP_SORTER_H_
#define UPB_MESSAGE_INTERNAL_MAP_SORTER_H_
#include <stdlib.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/mem/alloc.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/map.h"
#include "upb/message/internal/map_entry.h"
@@ -69,25 +49,26 @@ UPB_INLINE void _upb_mapsorter_init(_upb_mapsorter* s) {
}
UPB_INLINE void _upb_mapsorter_destroy(_upb_mapsorter* s) {
if (s->entries) free(s->entries);
if (s->entries) upb_gfree(s->entries);
}
UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s, const upb_Map* map,
UPB_INLINE bool _upb_sortedmap_next(_upb_mapsorter* s,
const struct upb_Map* map,
_upb_sortedmap* sorted, upb_MapEntry* ent) {
if (sorted->pos == sorted->end) return false;
const upb_tabent* tabent = (const upb_tabent*)s->entries[sorted->pos++];
upb_StringView key = upb_tabstrview(tabent->key);
_upb_map_fromkey(key, &ent->data.k, map->key_size);
_upb_map_fromkey(key, &ent->k, map->key_size);
upb_value val = {tabent->val.val};
_upb_map_fromvalue(val, &ent->data.v, map->val_size);
_upb_map_fromvalue(val, &ent->v, map->val_size);
return true;
}
UPB_INLINE bool _upb_sortedmap_nextext(_upb_mapsorter* s,
_upb_sortedmap* sorted,
const upb_Message_Extension** ext) {
const upb_Extension** ext) {
if (sorted->pos == sorted->end) return false;
*ext = (const upb_Message_Extension*)s->entries[sorted->pos++];
*ext = (const upb_Extension*)s->entries[sorted->pos++];
return true;
}
@@ -97,11 +78,10 @@ UPB_INLINE void _upb_mapsorter_popmap(_upb_mapsorter* s,
}
bool _upb_mapsorter_pushmap(_upb_mapsorter* s, upb_FieldType key_type,
const upb_Map* map, _upb_sortedmap* sorted);
const struct upb_Map* map, _upb_sortedmap* sorted);
bool _upb_mapsorter_pushexts(_upb_mapsorter* s,
const upb_Message_Extension* exts, size_t count,
_upb_sortedmap* sorted);
bool _upb_mapsorter_pushexts(_upb_mapsorter* s, const upb_Extension* exts,
size_t count, _upb_sortedmap* sorted);
#ifdef __cplusplus
} /* extern "C" */
@@ -109,4 +89,4 @@ bool _upb_mapsorter_pushexts(_upb_mapsorter* s,
#include "upb/port/undef.inc"
#endif /* UPB_COLLECTIONS_INTERNAL_MAP_SORTER_H_ */
#endif /* UPB_MESSAGE_INTERNAL_MAP_SORTER_H_ */

View File

@@ -12,17 +12,14 @@
** The definitions in this file are internal to upb.
**/
#ifndef UPB_MESSAGE_INTERNAL_H_
#define UPB_MESSAGE_INTERNAL_H_
#ifndef UPB_MESSAGE_INTERNAL_MESSAGE_H_
#define UPB_MESSAGE_INTERNAL_MESSAGE_H_
#include <stdlib.h>
#include <string.h>
#include "upb/mem/arena.h"
#include "upb/message/internal/extension.h"
#include "upb/message/internal/types.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/extension_registry.h"
#include "upb/mini_table/message.h"
// Must be last.
@@ -36,14 +33,12 @@ extern const float kUpb_FltInfinity;
extern const double kUpb_Infinity;
extern const double kUpb_NaN;
/* Internal members of a upb_Message that track unknown fields and/or
* extensions. We can change this without breaking binary compatibility. We put
* these before the user's data. The user's upb_Message* points after the
* upb_Message_Internal. */
// Internal members of a upb_Message that track unknown fields and/or
// extensions. We can change this without breaking binary compatibility.
struct upb_Message_InternalData {
/* Total size of this structure, including the data that follows.
* Must be aligned to 8, which is alignof(upb_Message_Extension) */
typedef struct upb_Message_Internal {
// Total size of this structure, including the data that follows.
// Must be aligned to 8, which is alignof(upb_Extension)
uint32_t size;
/* Offsets relative to the beginning of this structure.
@@ -53,49 +48,49 @@ struct upb_Message_InternalData {
* When the two meet, we're out of data and have to realloc.
*
* If we imagine that the final member of this struct is:
* char data[size - overhead]; // overhead =
* sizeof(upb_Message_InternalData)
* char data[size - overhead]; // overhead = sizeof(upb_Message_Internal)
*
* Then we have:
* unknown data: data[0 .. (unknown_end - overhead)]
* extensions data: data[(ext_begin - overhead) .. (size - overhead)] */
uint32_t unknown_end;
uint32_t ext_begin;
/* Data follows, as if there were an array:
* char data[size - sizeof(upb_Message_InternalData)]; */
};
// Data follows, as if there were an array:
// char data[size - sizeof(upb_Message_Internal)];
} upb_Message_Internal;
/* Maps upb_CType -> memory size. */
extern char _upb_CTypeo_size[12];
UPB_INLINE size_t upb_msg_sizeof(const upb_MiniTable* t) {
return t->size + sizeof(upb_Message_Internal);
}
#ifdef UPB_TRACING_ENABLED
UPB_API void upb_Message_LogNewMessage(const upb_MiniTable* m,
const upb_Arena* arena);
UPB_API void upb_Message_SetNewMessageTraceHandler(
void (*handler)(const upb_MiniTable*, const upb_Arena*));
#endif // UPB_TRACING_ENABLED
// Inline version upb_Message_New(), for internal use.
UPB_INLINE upb_Message* _upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena) {
size_t size = upb_msg_sizeof(mini_table);
void* mem = upb_Arena_Malloc(arena, size + sizeof(upb_Message_Internal));
if (UPB_UNLIKELY(!mem)) return NULL;
upb_Message* msg = UPB_PTR_AT(mem, sizeof(upb_Message_Internal), upb_Message);
memset(mem, 0, size);
UPB_INLINE struct upb_Message* _upb_Message_New(const upb_MiniTable* m,
upb_Arena* a) {
#ifdef UPB_TRACING_ENABLED
upb_Message_LogNewMessage(m, a);
#endif // UPB_TRACING_ENABLED
const int size = m->UPB_PRIVATE(size);
struct upb_Message* msg = (struct upb_Message*)upb_Arena_Malloc(a, size);
if (UPB_UNLIKELY(!msg)) return NULL;
memset(msg, 0, size);
return msg;
}
UPB_INLINE upb_Message_Internal* upb_Message_Getinternal(
const upb_Message* msg) {
ptrdiff_t size = sizeof(upb_Message_Internal);
return (upb_Message_Internal*)((char*)msg - size);
}
// Discards the unknown fields for this message only.
void _upb_Message_DiscardUnknown_shallow(upb_Message* msg);
void _upb_Message_DiscardUnknown_shallow(struct upb_Message* msg);
// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance.
bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena);
bool UPB_PRIVATE(_upb_Message_AddUnknown)(struct upb_Message* msg,
const char* data, size_t len,
upb_Arena* arena);
bool UPB_PRIVATE(_upb_Message_Realloc)(struct upb_Message* msg, size_t need,
upb_Arena* arena);
#ifdef __cplusplus
} /* extern "C" */
@@ -103,4 +98,4 @@ bool _upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_INTERNAL_H_ */
#endif /* UPB_MESSAGE_INTERNAL_MESSAGE_H_ */

View File

@@ -0,0 +1,56 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_INTERNAL_TAGGED_PTR_H_
#define UPB_MINI_TABLE_INTERNAL_TAGGED_PTR_H_
#include <stdint.h>
#include "upb/message/internal/message.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Internal-only because empty messages cannot be created by the user.
UPB_INLINE uintptr_t
UPB_PRIVATE(_upb_TaggedMessagePtr_Pack)(struct upb_Message* ptr, bool empty) {
UPB_ASSERT(((uintptr_t)ptr & 1) == 0);
return (uintptr_t)ptr | (empty ? 1 : 0);
}
UPB_API_INLINE bool upb_TaggedMessagePtr_IsEmpty(uintptr_t ptr) {
return ptr & 1;
}
UPB_INLINE struct upb_Message* UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(
uintptr_t ptr) {
return (struct upb_Message*)(ptr & ~(uintptr_t)1);
}
UPB_API_INLINE struct upb_Message* upb_TaggedMessagePtr_GetNonEmptyMessage(
uintptr_t ptr) {
UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(ptr));
return UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(ptr);
}
UPB_INLINE struct upb_Message* UPB_PRIVATE(
_upb_TaggedMessagePtr_GetEmptyMessage)(uintptr_t ptr) {
UPB_ASSERT(upb_TaggedMessagePtr_IsEmpty(ptr));
return UPB_PRIVATE(_upb_TaggedMessagePtr_GetMessage)(ptr);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_TAGGED_PTR_H_ */

View File

@@ -5,19 +5,54 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_INTERNAL_TYPES_H_
#define UPB_MINI_TABLE_INTERNAL_TYPES_H_
#ifndef UPB_MESSAGE_INTERNAL_TYPES_H_
#define UPB_MESSAGE_INTERNAL_TYPES_H_
typedef struct upb_Message_InternalData upb_Message_InternalData;
#include <stdint.h>
typedef struct {
// Must be last.
#include "upb/port/def.inc"
#define UPB_OPAQUE(x) x##_opaque
struct upb_Message {
union {
upb_Message_InternalData* internal;
// Force 8-byte alignment, since the data members may contain members that
// require 8-byte alignment.
double d;
uintptr_t UPB_OPAQUE(internal); // tagged pointer, low bit == frozen
double d; // Forces same size for 32-bit/64-bit builds
};
} upb_Message_Internal;
};
#endif // UPB_MINI_TABLE_INTERNAL_TYPES_H_
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE void UPB_PRIVATE(_upb_Message_ShallowFreeze)(
struct upb_Message* msg) {
msg->UPB_OPAQUE(internal) |= 1ULL;
}
UPB_API_INLINE bool upb_Message_IsFrozen(const struct upb_Message* msg) {
return (msg->UPB_OPAQUE(internal) & 1ULL) != 0;
}
UPB_INLINE struct upb_Message_Internal* UPB_PRIVATE(_upb_Message_GetInternal)(
const struct upb_Message* msg) {
const uintptr_t tmp = msg->UPB_OPAQUE(internal) & ~1ULL;
return (struct upb_Message_Internal*)tmp;
}
UPB_INLINE void UPB_PRIVATE(_upb_Message_SetInternal)(
struct upb_Message* msg, struct upb_Message_Internal* internal) {
UPB_ASSERT(!upb_Message_IsFrozen(msg));
msg->UPB_OPAQUE(internal) = (uintptr_t)internal;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#undef UPB_OPAQUE
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_INTERNAL_TYPES_H_ */

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_MAP_H_
#define UPB_MESSAGE_MAP_H_
@@ -35,7 +12,10 @@
#include "upb/base/descriptor_constants.h"
#include "upb/mem/arena.h"
#include "upb/message/value.h" // IWYU pragma: export
#include "upb/message/internal/map.h"
#include "upb/message/value.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
@@ -62,12 +42,6 @@ UPB_API bool upb_Map_Get(const upb_Map* map, upb_MessageValue key,
// Removes all entries in the map.
UPB_API void upb_Map_Clear(upb_Map* map);
typedef enum {
kUpb_MapInsertStatus_Inserted = 0,
kUpb_MapInsertStatus_Replaced = 1,
kUpb_MapInsertStatus_OutOfMemory = 2,
} upb_MapInsertStatus;
// Sets the given key to the given value, returning whether the key was inserted
// or replaced. If the key was inserted, then any existing iterators will be
// invalidated.
@@ -89,12 +63,6 @@ UPB_API_INLINE bool upb_Map_Set(upb_Map* map, upb_MessageValue key,
UPB_API bool upb_Map_Delete(upb_Map* map, upb_MessageValue key,
upb_MessageValue* val);
// (DEPRECATED and going away soon. Do not use.)
UPB_INLINE bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key,
upb_MessageValue* val) {
return upb_Map_Delete(map, key, val);
}
// Map iteration:
//
// size_t iter = kUpb_Map_Begin;
@@ -103,7 +71,7 @@ UPB_INLINE bool upb_Map_Delete2(upb_Map* map, upb_MessageValue key,
// ...
// }
#define kUpb_Map_Begin ((size_t)-1)
#define kUpb_Map_Begin ((size_t) - 1)
// Advances to the next entry. Returns false if no more entries are present.
// Otherwise returns true and populates both *key and *value.
@@ -138,6 +106,14 @@ UPB_API bool upb_MapIterator_Done(const upb_Map* map, size_t iter);
UPB_API upb_MessageValue upb_MapIterator_Key(const upb_Map* map, size_t iter);
UPB_API upb_MessageValue upb_MapIterator_Value(const upb_Map* map, size_t iter);
// Mark a map and all of its descendents as frozen/immutable.
// If the map values are messages then |m| must point to the minitable for
// those messages. Otherwise |m| must be NULL.
UPB_API void upb_Map_Freeze(upb_Map* map, const upb_MiniTable* m);
// Returns whether a map has been frozen.
UPB_API_INLINE bool upb_Map_IsFrozen(const upb_Map* map);
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -1,32 +1,9 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are
// met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above
// copyright notice, this list of conditions and the following disclaimer
// in the documentation and/or other materials provided with the
// distribution.
// * Neither the name of Google LLC nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
// These functions are only used by generated code.

26
Pods/gRPC-C++/third_party/upb/upb/message/merge.h generated vendored Normal file
View File

@@ -0,0 +1,26 @@
#ifndef THIRD_PARTY_UPB_UPB_MESSAGE_MERGE_H_
#define THIRD_PARTY_UPB_UPB_MESSAGE_MERGE_H_
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/mini_table/extension_registry.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
UPB_API bool upb_Message_MergeFrom(upb_Message* dst, const upb_Message* src,
const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg,
upb_Arena* arena);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif // THIRD_PARTY_UPB_UPB_MESSAGE_MERGE_H_

View File

@@ -15,24 +15,21 @@
#include <stddef.h>
#include "upb/mem/arena.h"
#include "upb/message/types.h" // IWYU pragma: export
#include "upb/message/internal/message.h"
#include "upb/message/internal/types.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_Message upb_Message;
#ifdef __cplusplus
extern "C" {
#endif
// Creates a new message with the given mini_table on the given arena.
UPB_API upb_Message* upb_Message_New(const upb_MiniTable* mini_table,
upb_Arena* arena);
// Adds unknown data (serialized protobuf data) to the given message.
// The data is copied into the message instance.
void upb_Message_AddUnknown(upb_Message* msg, const char* data, size_t len,
upb_Arena* arena);
UPB_API upb_Message* upb_Message_New(const upb_MiniTable* m, upb_Arena* arena);
// Returns a reference to the message's unknown data.
const char* upb_Message_GetUnknown(const upb_Message* msg, size_t* len);
@@ -43,6 +40,20 @@ void upb_Message_DeleteUnknown(upb_Message* msg, const char* data, size_t len);
// Returns the number of extensions present in this message.
size_t upb_Message_ExtensionCount(const upb_Message* msg);
// Mark a message and all of its descendents as frozen/immutable.
UPB_API void upb_Message_Freeze(upb_Message* msg, const upb_MiniTable* m);
// Returns whether a message has been frozen.
UPB_API_INLINE bool upb_Message_IsFrozen(const upb_Message* msg);
#ifdef UPB_TRACING_ENABLED
UPB_API void upb_Message_LogNewMessage(const upb_MiniTable* m,
const upb_Arena* arena);
UPB_API void upb_Message_SetNewMessageTraceHandler(
void (*handler)(const upb_MiniTable* m, const upb_Arena* arena));
#endif // UPB_TRACING_ENABLED
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -5,60 +5,39 @@
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_TYPES_H_
#define UPB_MINI_TABLE_TYPES_H_
#ifndef UPB_MINI_TABLE_TAGGED_PTR_H_
#define UPB_MINI_TABLE_TAGGED_PTR_H_
#include <stdint.h>
#include "upb/message/types.h" // IWYU pragma: export
#include "upb/message/internal/tagged_ptr.h"
#include "upb/message/message.h"
// Must be last.
#include "upb/port/def.inc"
// When a upb_Message* is stored in a message, array, or map, it is stored in a
// tagged form. If the tag bit is set, the referenced upb_Message is of type
// _kUpb_MiniTable_Empty (a sentinel message type with no fields) instead of
// that field's true message type. This forms the basis of what we call
// "dynamic tree shaking."
//
// See the documentation for kUpb_DecodeOption_ExperimentalAllowUnlinked for
// more information.
typedef uintptr_t upb_TaggedMessagePtr;
#ifdef __cplusplus
extern "C" {
#endif
// When a upb_Message* is stored in a message, array, or map, it is stored in a
// tagged form. If the tag bit is set, the referenced upb_Message is of type
// _kUpb_MiniTable_Empty (a sentinel message type with no fields) instead of
// that field's true message type. This forms the basis of what we call
// "dynamic tree shaking."
//
// See the documentation for kUpb_DecodeOption_ExperimentalAllowUnlinked for
// more information.
typedef uintptr_t upb_TaggedMessagePtr;
// Internal-only because empty messages cannot be created by the user.
UPB_INLINE upb_TaggedMessagePtr _upb_TaggedMessagePtr_Pack(upb_Message* ptr,
bool empty) {
UPB_ASSERT(((uintptr_t)ptr & 1) == 0);
return (uintptr_t)ptr | (empty ? 1 : 0);
}
// Users who enable unlinked sub-messages must use this to test whether a
// message is empty before accessing it. If a message is empty, it must be
// message is empty before accessing it. If a message is empty, it must be
// first promoted using the interfaces in message/promote.h.
UPB_INLINE bool upb_TaggedMessagePtr_IsEmpty(upb_TaggedMessagePtr ptr) {
return ptr & 1;
}
UPB_API_INLINE bool upb_TaggedMessagePtr_IsEmpty(upb_TaggedMessagePtr ptr);
UPB_INLINE upb_Message* _upb_TaggedMessagePtr_GetMessage(
upb_TaggedMessagePtr ptr) {
return (upb_Message*)(ptr & ~(uintptr_t)1);
}
UPB_INLINE upb_Message* upb_TaggedMessagePtr_GetNonEmptyMessage(
upb_TaggedMessagePtr ptr) {
UPB_ASSERT(!upb_TaggedMessagePtr_IsEmpty(ptr));
return _upb_TaggedMessagePtr_GetMessage(ptr);
}
UPB_INLINE upb_Message* _upb_TaggedMessagePtr_GetEmptyMessage(
upb_TaggedMessagePtr ptr) {
UPB_ASSERT(upb_TaggedMessagePtr_IsEmpty(ptr));
return _upb_TaggedMessagePtr_GetMessage(ptr);
}
UPB_API_INLINE upb_Message* upb_TaggedMessagePtr_GetNonEmptyMessage(
upb_TaggedMessagePtr ptr);
#ifdef __cplusplus
} /* extern "C" */
@@ -66,4 +45,4 @@ UPB_INLINE upb_Message* _upb_TaggedMessagePtr_GetEmptyMessage(
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_TYPES_H_ */
#endif /* UPB_MINI_TABLE_TAGGED_PTR_H_ */

View File

@@ -1,15 +0,0 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MESSAGE_TYPES_H_
#define UPB_MESSAGE_TYPES_H_
// This typedef is in a leaf header to resolve a circular dependency between
// messages and mini tables.
typedef void upb_Message;
#endif /* UPB_MESSAGE_TYPES_H_ */

View File

@@ -12,10 +12,16 @@
#define UPB_MESSAGE_VALUE_H_
#include <stdint.h>
#include <string.h>
#include "upb/base/string_view.h"
#include "upb/message/tagged_ptr.h"
#include "upb/message/types.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
typedef union {
bool bool_val;
@@ -27,20 +33,38 @@ typedef union {
uint64_t uint64_val;
const struct upb_Array* array_val;
const struct upb_Map* map_val;
const upb_Message* msg_val;
const struct upb_Message* msg_val;
upb_StringView str_val;
// EXPERIMENTAL: A tagged upb_Message*. Users must use this instead of
// msg_val if unlinked sub-messages may possibly be in use. See the
// documentation in kUpb_DecodeOption_ExperimentalAllowUnlinked for more
// information.
upb_TaggedMessagePtr tagged_msg_val;
uintptr_t tagged_msg_val; // upb_TaggedMessagePtr
} upb_MessageValue;
UPB_API_INLINE upb_MessageValue upb_MessageValue_Zero(void) {
upb_MessageValue zero;
memset(&zero, 0, sizeof(zero));
return zero;
}
typedef union {
struct upb_Array* array;
struct upb_Map* map;
upb_Message* msg;
struct upb_Message* msg;
} upb_MutableMessageValue;
UPB_API_INLINE upb_MutableMessageValue upb_MutableMessageValue_Zero(void) {
upb_MutableMessageValue zero;
memset(&zero, 0, sizeof(zero));
return zero;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MESSAGE_VALUE_H_ */

View File

@@ -1,86 +0,0 @@
load(
"//bazel:build_defs.bzl",
"UPB_DEFAULT_COPTS",
"UPB_DEFAULT_CPPOPTS",
)
cc_library(
name = "mini_descriptor",
srcs = [
"build_enum.c",
"decode.c",
"link.c",
],
hdrs = [
"build_enum.h",
"decode.h",
"link.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
":internal",
"//upb:base",
"//upb:mem",
"//upb:mini_table",
"//upb:mini_table_internal",
"//upb:port",
],
)
cc_library(
name = "internal",
srcs = [
"internal/base92.c",
"internal/encode.c",
],
hdrs = [
"internal/base92.h",
"internal/decoder.h",
"internal/encode.h",
"internal/encode.hpp",
"internal/modifiers.h",
"internal/wire_constants.h",
],
copts = UPB_DEFAULT_COPTS,
visibility = ["//visibility:public"],
deps = [
"//upb:base",
"//upb:base_internal",
"//upb:port",
],
)
cc_test(
name = "encode_test",
srcs = ["internal/encode_test.cc"],
copts = UPB_DEFAULT_CPPOPTS,
deps = [
":internal",
":mini_descriptor",
"//:protobuf",
"@com_google_googletest//:gtest_main",
"//upb:base",
"//upb:mem",
"//upb:message_accessors_internal",
"//upb:mini_table",
"//upb:wire",
"@com_google_absl//absl/container:flat_hash_set",
],
)
# begin:github_only
filegroup(
name = "source_files",
srcs = glob(
[
"**/*.c",
"**/*.h",
],
),
visibility = [
"//upb/cmake:__pkg__",
"//python/dist:__pkg__",
]
)
# end:github_only

View File

@@ -4,6 +4,7 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_DESCRIPTOR_BUILD_ENUM_H_
#define UPB_MINI_DESCRIPTOR_BUILD_ENUM_H_
@@ -18,20 +19,11 @@
extern "C" {
#endif
// Builds a upb_MiniTableEnum from an enum MiniDescriptor. The MiniDescriptor
// must be for an enum, not a message.
UPB_API upb_MiniTableEnum* upb_MiniDescriptor_BuildEnum(const char* data,
size_t len,
upb_Arena* arena,
upb_Status* status);
// TODO: Deprecated name; update callers.
UPB_API_INLINE upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data,
size_t len,
upb_Arena* arena,
upb_Status* status) {
return upb_MiniDescriptor_BuildEnum(data, len, arena, status);
}
// Builds a upb_MiniTableEnum from an enum mini descriptor.
// The mini descriptor must be for an enum, not a message.
UPB_API upb_MiniTableEnum* upb_MiniTableEnum_Build(const char* data, size_t len,
upb_Arena* arena,
upb_Status* status);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -11,8 +11,6 @@
#include "upb/base/status.h"
#include "upb/mem/arena.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"
// Export the newer headers, for legacy users. New users should include the
@@ -76,8 +74,7 @@ UPB_API upb_MiniTableExtension* _upb_MiniTableExtension_Build(
UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_Build(
const char* data, size_t len, const upb_MiniTable* extendee,
upb_Arena* arena, upb_Status* status) {
upb_MiniTableSub sub;
sub.submsg = NULL;
upb_MiniTableSub sub = upb_MiniTableSub_FromMessage(NULL);
return _upb_MiniTableExtension_Build(
data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
}
@@ -85,8 +82,7 @@ UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_Build(
UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildMessage(
const char* data, size_t len, const upb_MiniTable* extendee,
upb_MiniTable* submsg, upb_Arena* arena, upb_Status* status) {
upb_MiniTableSub sub;
sub.submsg = submsg;
upb_MiniTableSub sub = upb_MiniTableSub_FromMessage(submsg);
return _upb_MiniTableExtension_Build(
data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
}
@@ -94,8 +90,7 @@ UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildMessage(
UPB_API_INLINE upb_MiniTableExtension* upb_MiniTableExtension_BuildEnum(
const char* data, size_t len, const upb_MiniTable* extendee,
upb_MiniTableEnum* subenum, upb_Arena* arena, upb_Status* status) {
upb_MiniTableSub sub;
sub.subenum = subenum;
upb_MiniTableSub sub = upb_MiniTableSub_FromEnum(subenum);
return _upb_MiniTableExtension_Build(
data, len, extendee, sub, kUpb_MiniTablePlatform_Native, arena, status);
}

View File

@@ -18,7 +18,7 @@
#include "upb/base/status.h"
#include "upb/mem/arena.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
#include "upb/mini_table/sub.h"

View File

@@ -8,6 +8,8 @@
#ifndef UPB_MINI_TABLE_ENUM_H_
#define UPB_MINI_TABLE_ENUM_H_
#include <stdint.h>
#include "upb/mini_table/internal/enum.h"
// Must be last
@@ -20,14 +22,8 @@ extern "C" {
#endif
// Validates enum value against range defined by enum mini table.
UPB_INLINE bool upb_MiniTableEnum_CheckValue(const struct upb_MiniTableEnum* e,
uint32_t val) {
_kUpb_FastEnumCheck_Status status = _upb_MiniTable_CheckEnumValueFast(e, val);
if (UPB_UNLIKELY(status == _kUpb_FastEnumCheck_CannotCheckFast)) {
return _upb_MiniTable_CheckEnumValueSlow(e, val);
}
return status == _kUpb_FastEnumCheck_ValueIsInEnum ? true : false;
}
UPB_API_INLINE bool upb_MiniTableEnum_CheckValue(const upb_MiniTableEnum* e,
uint32_t val);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -8,8 +8,37 @@
#ifndef UPB_MINI_TABLE_EXTENSION_H_
#define UPB_MINI_TABLE_EXTENSION_H_
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/extension.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_MiniTableExtension upb_MiniTableExtension;
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE upb_CType
upb_MiniTableExtension_CType(const upb_MiniTableExtension* e);
UPB_API_INLINE uint32_t
upb_MiniTableExtension_Number(const upb_MiniTableExtension* e);
UPB_API_INLINE const upb_MiniTable* upb_MiniTableExtension_GetSubMessage(
const upb_MiniTableExtension* e);
UPB_API_INLINE void upb_MiniTableExtension_SetSubMessage(
upb_MiniTableExtension* e, const upb_MiniTable* m);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_EXTENSION_H_ */

View File

@@ -71,6 +71,23 @@ bool upb_ExtensionRegistry_AddArray(upb_ExtensionRegistry* r,
const upb_MiniTableExtension** e,
size_t count);
#ifdef UPB_LINKARR_DECLARE
// Adds all extensions linked into the binary into the registry. The set of
// linked extensions is assembled by the linker using linker arrays. This
// will likely not work properly if the extensions are split across multiple
// shared libraries.
//
// Returns true if all extensions were added successfully, false on out of
// memory or if any extensions were already present.
//
// This API is currently not available on MSVC (though it *is* available on
// Windows using clang-cl).
UPB_API bool upb_ExtensionRegistry_AddAllLinkedExtensions(
upb_ExtensionRegistry* r);
#endif // UPB_LINKARR_DECLARE
// Looks up the extension (if any) defined for message type |t| and field
// number |num|. Returns the extension if found, otherwise NULL.
UPB_API const upb_MiniTableExtension* upb_ExtensionRegistry_Lookup(

View File

@@ -8,86 +8,46 @@
#ifndef UPB_MINI_TABLE_FIELD_H_
#define UPB_MINI_TABLE_FIELD_H_
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/message.h"
#include "upb/mini_table/internal/sub.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_MiniTableField upb_MiniTableField;
#ifdef __cplusplus
extern "C" {
#endif
typedef struct upb_MiniTableField upb_MiniTableField;
UPB_API_INLINE upb_CType upb_MiniTableField_CType(const upb_MiniTableField* f);
UPB_API_INLINE upb_FieldType
upb_MiniTableField_Type(const upb_MiniTableField* field) {
if (field->mode & kUpb_LabelFlags_IsAlternate) {
if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Int32) {
return kUpb_FieldType_Enum;
} else if (field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Bytes) {
return kUpb_FieldType_String;
} else {
UPB_ASSERT(false);
}
}
return (upb_FieldType)field->UPB_PRIVATE(descriptortype);
}
UPB_API_INLINE bool upb_MiniTableField_HasPresence(const upb_MiniTableField* f);
UPB_API_INLINE upb_CType upb_MiniTableField_CType(const upb_MiniTableField* f) {
switch (upb_MiniTableField_Type(f)) {
case kUpb_FieldType_Double:
return kUpb_CType_Double;
case kUpb_FieldType_Float:
return kUpb_CType_Float;
case kUpb_FieldType_Int64:
case kUpb_FieldType_SInt64:
case kUpb_FieldType_SFixed64:
return kUpb_CType_Int64;
case kUpb_FieldType_Int32:
case kUpb_FieldType_SFixed32:
case kUpb_FieldType_SInt32:
return kUpb_CType_Int32;
case kUpb_FieldType_UInt64:
case kUpb_FieldType_Fixed64:
return kUpb_CType_UInt64;
case kUpb_FieldType_UInt32:
case kUpb_FieldType_Fixed32:
return kUpb_CType_UInt32;
case kUpb_FieldType_Enum:
return kUpb_CType_Enum;
case kUpb_FieldType_Bool:
return kUpb_CType_Bool;
case kUpb_FieldType_String:
return kUpb_CType_String;
case kUpb_FieldType_Bytes:
return kUpb_CType_Bytes;
case kUpb_FieldType_Group:
case kUpb_FieldType_Message:
return kUpb_CType_Message;
}
UPB_UNREACHABLE();
}
UPB_API_INLINE bool upb_MiniTableField_IsExtension(
const upb_MiniTableField* field) {
return field->mode & kUpb_LabelFlags_IsExtension;
}
UPB_API_INLINE bool upb_MiniTableField_IsArray(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum(
const upb_MiniTableField* field) {
return field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum;
}
const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_HasPresence(
const upb_MiniTableField* field) {
if (upb_MiniTableField_IsExtension(field)) {
return !upb_IsRepeatedOrMap(field);
} else {
return field->presence != 0;
}
}
UPB_API_INLINE bool upb_MiniTableField_IsExtension(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsInOneof(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsMap(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsPacked(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsScalar(const upb_MiniTableField* f);
UPB_API_INLINE bool upb_MiniTableField_IsSubMessage(
const upb_MiniTableField* f);
UPB_API_INLINE uint32_t upb_MiniTableField_Number(const upb_MiniTableField* f);
UPB_API_INLINE upb_FieldType
upb_MiniTableField_Type(const upb_MiniTableField* f);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -8,8 +8,39 @@
#ifndef UPB_MINI_TABLE_FILE_H_
#define UPB_MINI_TABLE_FILE_H_
#include "upb/mini_table/enum.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/internal/file.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_MiniTableFile upb_MiniTableFile;
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTableFile_Enum(
const upb_MiniTableFile* f, int i);
UPB_API_INLINE int upb_MiniTableFile_EnumCount(const upb_MiniTableFile* f);
UPB_API_INLINE const upb_MiniTableExtension* upb_MiniTableFile_Extension(
const upb_MiniTableFile* f, int i);
UPB_API_INLINE int upb_MiniTableFile_ExtensionCount(const upb_MiniTableFile* f);
UPB_API_INLINE const upb_MiniTable* upb_MiniTableFile_Message(
const upb_MiniTableFile* f, int i);
UPB_API_INLINE int upb_MiniTableFile_MessageCount(const upb_MiniTableFile* f);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_FILE_H_ */

View File

@@ -14,35 +14,34 @@
#include "upb/port/def.inc"
struct upb_MiniTableEnum {
uint32_t mask_limit; // Limit enum value that can be tested with mask.
uint32_t value_count; // Number of values after the bitfield.
uint32_t data[]; // Bitmask + enumerated values follow.
uint32_t UPB_PRIVATE(mask_limit); // Highest that can be tested with mask.
uint32_t UPB_PRIVATE(value_count); // Number of values after the bitfield.
uint32_t UPB_PRIVATE(data)[]; // Bitmask + enumerated values follow.
};
typedef enum {
_kUpb_FastEnumCheck_ValueIsInEnum = 0,
_kUpb_FastEnumCheck_ValueIsNotInEnum = 1,
_kUpb_FastEnumCheck_CannotCheckFast = 2,
} _kUpb_FastEnumCheck_Status;
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE _kUpb_FastEnumCheck_Status _upb_MiniTable_CheckEnumValueFast(
UPB_API_INLINE bool upb_MiniTableEnum_CheckValue(
const struct upb_MiniTableEnum* e, uint32_t val) {
if (UPB_UNLIKELY(val >= 64)) return _kUpb_FastEnumCheck_CannotCheckFast;
uint64_t mask = e->data[0] | ((uint64_t)e->data[1] << 32);
return (mask & (1ULL << val)) ? _kUpb_FastEnumCheck_ValueIsInEnum
: _kUpb_FastEnumCheck_ValueIsNotInEnum;
}
if (UPB_LIKELY(val < 64)) {
const uint64_t mask =
e->UPB_PRIVATE(data)[0] | ((uint64_t)e->UPB_PRIVATE(data)[1] << 32);
const uint64_t bit = 1ULL << val;
return (mask & bit) != 0;
}
if (UPB_LIKELY(val < e->UPB_PRIVATE(mask_limit))) {
const uint32_t mask = e->UPB_PRIVATE(data)[val / 32];
const uint32_t bit = 1ULL << (val % 32);
return (mask & bit) != 0;
}
UPB_INLINE bool _upb_MiniTable_CheckEnumValueSlow(
const struct upb_MiniTableEnum* e, uint32_t val) {
if (val < e->mask_limit) return e->data[val / 32] & (1ULL << (val % 32));
// OPT: binary search long lists?
const uint32_t* start = &e->data[e->mask_limit / 32];
const uint32_t* limit = &e->data[(e->mask_limit / 32) + e->value_count];
const uint32_t* start =
&e->UPB_PRIVATE(data)[e->UPB_PRIVATE(mask_limit) / 32];
const uint32_t* limit = &e->UPB_PRIVATE(
data)[e->UPB_PRIVATE(mask_limit) / 32 + e->UPB_PRIVATE(value_count)];
for (const uint32_t* p = start; p < limit; p++) {
if (*p == val) return true;
}

View File

@@ -8,6 +8,10 @@
#ifndef UPB_MINI_TABLE_INTERNAL_EXTENSION_H_
#define UPB_MINI_TABLE_INTERNAL_EXTENSION_H_
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/sub.h"
@@ -16,12 +20,48 @@
struct upb_MiniTableExtension {
// Do not move this field. We need to be able to alias pointers.
struct upb_MiniTableField field;
struct upb_MiniTableField UPB_PRIVATE(field);
const struct upb_MiniTable* extendee;
union upb_MiniTableSub sub; // NULL unless submessage or proto2 enum
const struct upb_MiniTable* UPB_PRIVATE(extendee);
union upb_MiniTableSub UPB_PRIVATE(sub); // NULL unless submsg or proto2 enum
};
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE upb_CType
upb_MiniTableExtension_CType(const struct upb_MiniTableExtension* e) {
return upb_MiniTableField_CType(&e->UPB_PRIVATE(field));
}
UPB_API_INLINE uint32_t
upb_MiniTableExtension_Number(const struct upb_MiniTableExtension* e) {
return e->UPB_PRIVATE(field).UPB_ONLYBITS(number);
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableExtension_GetSubMessage(
const struct upb_MiniTableExtension* e) {
if (upb_MiniTableExtension_CType(e) != kUpb_CType_Message) {
return NULL;
}
return upb_MiniTableSub_Message(e->UPB_PRIVATE(sub));
}
UPB_API_INLINE void upb_MiniTableExtension_SetSubMessage(
struct upb_MiniTableExtension* e, const struct upb_MiniTable* m) {
e->UPB_PRIVATE(sub).UPB_PRIVATE(submsg) = m;
}
UPB_INLINE upb_FieldRep UPB_PRIVATE(_upb_MiniTableExtension_GetRep)(
const struct upb_MiniTableExtension* e) {
return UPB_PRIVATE(_upb_MiniTableField_GetRep)(&e->UPB_PRIVATE(field));
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_EXTENSION_H_ */

View File

@@ -8,17 +8,20 @@
#ifndef UPB_MINI_TABLE_INTERNAL_FIELD_H_
#define UPB_MINI_TABLE_INTERNAL_FIELD_H_
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/size_log2.h"
// Must be last.
#include "upb/port/def.inc"
// LINT.IfChange(struct_definition)
struct upb_MiniTableField {
uint32_t number;
uint16_t offset;
int16_t presence; // If >0, hasbit_index. If <0, ~oneof_index
uint32_t UPB_ONLYBITS(number);
uint16_t UPB_ONLYBITS(offset);
int16_t presence; // If >0, hasbit_index. If <0, ~oneof_index
// Indexes into `upb_MiniTable.subs`
// Will be set to `kUpb_NoSub` if `descriptortype` != MESSAGE/GROUP/ENUM
@@ -27,10 +30,10 @@ struct upb_MiniTableField {
uint8_t UPB_PRIVATE(descriptortype);
// upb_FieldMode | upb_LabelFlags | (upb_FieldRep << kUpb_FieldRep_Shift)
uint8_t mode;
uint8_t UPB_ONLYBITS(mode);
};
#define kUpb_NoSub ((uint16_t)-1)
#define kUpb_NoSub ((uint16_t) - 1)
typedef enum {
kUpb_FieldMode_Map = 0,
@@ -67,44 +70,150 @@ typedef enum {
#define kUpb_FieldRep_Shift 6
UPB_INLINE upb_FieldRep
_upb_MiniTableField_GetRep(const struct upb_MiniTableField* field) {
return (upb_FieldRep)(field->mode >> kUpb_FieldRep_Shift);
}
#ifdef __cplusplus
extern "C" {
#endif
UPB_INLINE upb_FieldMode
upb_FieldMode_Get(const struct upb_MiniTableField* field) {
return (upb_FieldMode)(field->mode & 3);
UPB_PRIVATE(_upb_MiniTableField_Mode)(const struct upb_MiniTableField* f) {
return (upb_FieldMode)(f->UPB_ONLYBITS(mode) & kUpb_FieldMode_Mask);
}
UPB_INLINE void _upb_MiniTableField_CheckIsArray(
const struct upb_MiniTableField* field) {
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_NativePointer);
UPB_ASSUME(upb_FieldMode_Get(field) == kUpb_FieldMode_Array);
UPB_ASSUME(field->presence == 0);
UPB_INLINE upb_FieldRep
UPB_PRIVATE(_upb_MiniTableField_GetRep)(const struct upb_MiniTableField* f) {
return (upb_FieldRep)(f->UPB_ONLYBITS(mode) >> kUpb_FieldRep_Shift);
}
UPB_INLINE void _upb_MiniTableField_CheckIsMap(
const struct upb_MiniTableField* field) {
UPB_ASSUME(_upb_MiniTableField_GetRep(field) == kUpb_FieldRep_NativePointer);
UPB_ASSUME(upb_FieldMode_Get(field) == kUpb_FieldMode_Map);
UPB_ASSUME(field->presence == 0);
UPB_API_INLINE bool upb_MiniTableField_IsArray(
const struct upb_MiniTableField* f) {
return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Array;
}
UPB_INLINE bool upb_IsRepeatedOrMap(const struct upb_MiniTableField* field) {
// This works because upb_FieldMode has no value 3.
return !(field->mode & kUpb_FieldMode_Scalar);
UPB_API_INLINE bool upb_MiniTableField_IsMap(
const struct upb_MiniTableField* f) {
return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Map;
}
UPB_INLINE bool upb_IsSubMessage(const struct upb_MiniTableField* field) {
return field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message ||
field->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group;
UPB_API_INLINE bool upb_MiniTableField_IsScalar(
const struct upb_MiniTableField* f) {
return UPB_PRIVATE(_upb_MiniTableField_Mode)(f) == kUpb_FieldMode_Scalar;
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_IsAlternate)(
const struct upb_MiniTableField* f) {
return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsAlternate) != 0;
}
UPB_API_INLINE bool upb_MiniTableField_IsExtension(
const struct upb_MiniTableField* f) {
return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsExtension) != 0;
}
UPB_API_INLINE bool upb_MiniTableField_IsPacked(
const struct upb_MiniTableField* f) {
return (f->UPB_ONLYBITS(mode) & kUpb_LabelFlags_IsPacked) != 0;
}
UPB_API_INLINE upb_FieldType
upb_MiniTableField_Type(const struct upb_MiniTableField* f) {
const upb_FieldType type = (upb_FieldType)f->UPB_PRIVATE(descriptortype);
if (UPB_PRIVATE(_upb_MiniTableField_IsAlternate)(f)) {
if (type == kUpb_FieldType_Int32) return kUpb_FieldType_Enum;
if (type == kUpb_FieldType_Bytes) return kUpb_FieldType_String;
UPB_ASSERT(false);
}
return type;
}
UPB_API_INLINE
upb_CType upb_MiniTableField_CType(const struct upb_MiniTableField* f) {
return upb_FieldType_CType(upb_MiniTableField_Type(f));
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(
const struct upb_MiniTableField* f) {
return f->presence > 0;
}
UPB_INLINE char UPB_PRIVATE(_upb_MiniTableField_HasbitMask)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f));
const size_t index = f->presence;
return 1 << (index % 8);
}
UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_HasbitOffset)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(UPB_PRIVATE(_upb_MiniTableField_HasHasbit)(f));
const size_t index = f->presence;
return index / 8;
}
UPB_API_INLINE bool upb_MiniTableField_IsClosedEnum(
const struct upb_MiniTableField* f) {
return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Enum;
}
UPB_API_INLINE bool upb_MiniTableField_IsInOneof(
const struct upb_MiniTableField* f) {
return f->presence < 0;
}
UPB_API_INLINE bool upb_MiniTableField_IsSubMessage(
const struct upb_MiniTableField* f) {
return f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Message ||
f->UPB_PRIVATE(descriptortype) == kUpb_FieldType_Group;
}
UPB_API_INLINE bool upb_MiniTableField_HasPresence(
const struct upb_MiniTableField* f) {
if (upb_MiniTableField_IsExtension(f)) {
return upb_MiniTableField_IsScalar(f);
} else {
return f->presence != 0;
}
}
UPB_API_INLINE uint32_t
upb_MiniTableField_Number(const struct upb_MiniTableField* f) {
return f->UPB_ONLYBITS(number);
}
UPB_INLINE uint16_t
UPB_PRIVATE(_upb_MiniTableField_Offset)(const struct upb_MiniTableField* f) {
return f->UPB_ONLYBITS(offset);
}
UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_OneofOffset)(
const struct upb_MiniTableField* f) {
UPB_ASSERT(upb_MiniTableField_IsInOneof(f));
return ~(ptrdiff_t)f->presence;
}
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsArray)(
const struct upb_MiniTableField* f) {
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_NativePointer);
UPB_ASSUME(upb_MiniTableField_IsArray(f));
UPB_ASSUME(f->presence == 0);
}
UPB_INLINE void UPB_PRIVATE(_upb_MiniTableField_CheckIsMap)(
const struct upb_MiniTableField* f) {
UPB_ASSUME(UPB_PRIVATE(_upb_MiniTableField_GetRep)(f) ==
kUpb_FieldRep_NativePointer);
UPB_ASSUME(upb_MiniTableField_IsMap(f));
UPB_ASSUME(f->presence == 0);
}
UPB_INLINE size_t UPB_PRIVATE(_upb_MiniTableField_ElemSizeLg2)(
const struct upb_MiniTableField* f) {
const upb_FieldType field_type = upb_MiniTableField_Type(f);
return UPB_PRIVATE(_upb_FieldType_SizeLg2)(field_type);
}
// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/mini_table_field.ts)
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -8,22 +8,59 @@
#ifndef UPB_MINI_TABLE_INTERNAL_FILE_H_
#define UPB_MINI_TABLE_INTERNAL_FILE_H_
#include "upb/mini_table/internal/enum.h"
#include "upb/mini_table/internal/extension.h"
#include "upb/mini_table/internal/message.h"
// Must be last.
#include "upb/port/def.inc"
struct upb_MiniTableFile {
const struct upb_MiniTable** msgs;
const struct upb_MiniTableEnum** enums;
const struct upb_MiniTableExtension** exts;
int msg_count;
int enum_count;
int ext_count;
const struct upb_MiniTable** UPB_PRIVATE(msgs);
const struct upb_MiniTableEnum** UPB_PRIVATE(enums);
const struct upb_MiniTableExtension** UPB_PRIVATE(exts);
int UPB_PRIVATE(msg_count);
int UPB_PRIVATE(enum_count);
int UPB_PRIVATE(ext_count);
};
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE int upb_MiniTableFile_EnumCount(
const struct upb_MiniTableFile* f) {
return f->UPB_PRIVATE(enum_count);
}
UPB_API_INLINE int upb_MiniTableFile_ExtensionCount(
const struct upb_MiniTableFile* f) {
return f->UPB_PRIVATE(ext_count);
}
UPB_API_INLINE int upb_MiniTableFile_MessageCount(
const struct upb_MiniTableFile* f) {
return f->UPB_PRIVATE(msg_count);
}
UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTableFile_Enum(
const struct upb_MiniTableFile* f, int i) {
UPB_ASSERT(i < f->UPB_PRIVATE(enum_count));
return f->UPB_PRIVATE(enums)[i];
}
UPB_API_INLINE const struct upb_MiniTableExtension* upb_MiniTableFile_Extension(
const struct upb_MiniTableFile* f, int i) {
UPB_ASSERT(i < f->UPB_PRIVATE(ext_count));
return f->UPB_PRIVATE(exts)[i];
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableFile_Message(
const struct upb_MiniTableFile* f, int i) {
UPB_ASSERT(i < f->UPB_PRIVATE(msg_count));
return f->UPB_PRIVATE(msgs)[i];
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_FILE_H_ */

View File

@@ -8,15 +8,20 @@
#ifndef UPB_MINI_TABLE_INTERNAL_MESSAGE_H_
#define UPB_MINI_TABLE_INTERNAL_MESSAGE_H_
#include "upb/message/types.h"
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/mini_table/internal/field.h"
#include "upb/mini_table/internal/sub.h"
// Must be last.
#include "upb/port/def.inc"
struct upb_Decoder;
struct upb_Message;
typedef const char* _upb_FieldParser(struct upb_Decoder* d, const char* ptr,
upb_Message* msg, intptr_t table,
struct upb_Message* msg, intptr_t table,
uint64_t hasbits, uint64_t data);
typedef struct {
uint64_t field_data;
@@ -35,50 +40,160 @@ typedef enum {
kUpb_ExtMode_IsMapEntry = 4,
} upb_ExtMode;
union upb_MiniTableSub;
// upb_MiniTable represents the memory layout of a given upb_MessageDef.
// The members are public so generated code can initialize them,
// but users MUST NOT directly read or write any of its members.
// LINT.IfChange(minitable_struct_definition)
struct upb_MiniTable {
const union upb_MiniTableSub* subs;
const struct upb_MiniTableField* fields;
const upb_MiniTableSubInternal* UPB_PRIVATE(subs);
const struct upb_MiniTableField* UPB_ONLYBITS(fields);
// Must be aligned to sizeof(void*). Doesn't include internal members like
// unknown fields, extension dict, pointer to msglayout, etc.
uint16_t size;
uint16_t UPB_PRIVATE(size);
uint16_t field_count;
uint8_t ext; // upb_ExtMode, declared as uint8_t so sizeof(ext) == 1
uint8_t dense_below;
uint8_t table_mask;
uint8_t required_count; // Required fields have the lowest hasbits.
uint16_t UPB_ONLYBITS(field_count);
uint8_t UPB_PRIVATE(ext); // upb_ExtMode, uint8_t here so sizeof(ext) == 1
uint8_t UPB_PRIVATE(dense_below);
uint8_t UPB_PRIVATE(table_mask);
uint8_t UPB_PRIVATE(required_count); // Required fields have the low hasbits.
#ifdef UPB_TRACING_ENABLED
const char* UPB_PRIVATE(full_name);
#endif
#ifdef UPB_FASTTABLE_ENABLED
// To statically initialize the tables of variable length, we need a flexible
// array member, and we need to compile in gnu99 mode (constant initialization
// of flexible array members is a GNU extension, not in C99 unfortunately.
_upb_FastTable_Entry fasttable[];
_upb_FastTable_Entry UPB_PRIVATE(fasttable)[];
#endif
};
// LINT.ThenChange(//depot/google3/third_party/upb/bits/typescript/mini_table.ts)
#ifdef __cplusplus
extern "C" {
#endif
// A MiniTable for an empty message, used for unlinked sub-messages.
extern const struct upb_MiniTable _kUpb_MiniTable_Empty;
UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(
_upb_MiniTable_StrongReference)(const struct upb_MiniTable* mt) {
#if defined(__GNUC__)
__asm__("" : : "r"(mt));
#else
const struct upb_MiniTable* volatile unused = mt;
(void)&unused; // Use address to avoid an extra load of "unused".
#endif
return mt;
}
// Computes a bitmask in which the |l->required_count| lowest bits are set,
// except that we skip the lowest bit (because upb never uses hasbit 0).
UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(_upb_MiniTable_Empty)(void) {
extern const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_Empty);
return &UPB_PRIVATE(_kUpb_MiniTable_Empty);
}
UPB_API_INLINE int upb_MiniTable_FieldCount(const struct upb_MiniTable* m) {
return m->UPB_ONLYBITS(field_count);
}
UPB_INLINE bool UPB_PRIVATE(_upb_MiniTable_IsEmpty)(
const struct upb_MiniTable* m) {
extern const struct upb_MiniTable UPB_PRIVATE(_kUpb_MiniTable_Empty);
return m == &UPB_PRIVATE(_kUpb_MiniTable_Empty);
}
UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_GetFieldByIndex(
const struct upb_MiniTable* m, uint32_t i) {
return &m->UPB_ONLYBITS(fields)[i];
}
UPB_INLINE const struct upb_MiniTable* UPB_PRIVATE(
_upb_MiniTable_GetSubTableByIndex)(const struct upb_MiniTable* m,
uint32_t i) {
return *m->UPB_PRIVATE(subs)[i].UPB_PRIVATE(submsg);
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_SubMessage(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
if (upb_MiniTableField_CType(f) != kUpb_CType_Message) {
return NULL;
}
return UPB_PRIVATE(_upb_MiniTable_GetSubTableByIndex)(
m, f->UPB_PRIVATE(submsg_index));
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_GetSubMessageTable(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
UPB_ASSUME(upb_MiniTableField_CType(f) == kUpb_CType_Message);
const struct upb_MiniTable* ret = upb_MiniTable_SubMessage(m, f);
UPB_ASSUME(ret);
return UPB_PRIVATE(_upb_MiniTable_IsEmpty)(ret) ? NULL : ret;
}
UPB_API_INLINE bool upb_MiniTable_FieldIsLinked(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
return upb_MiniTable_GetSubMessageTable(m, f) != NULL;
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTable_MapEntrySubMessage(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
UPB_ASSERT(upb_MiniTable_FieldIsLinked(m, f)); // Map entries must be linked.
UPB_ASSERT(upb_MiniTableField_IsMap(f)); // Function precondition.
return upb_MiniTable_SubMessage(m, f);
}
UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTable_GetSubEnumTable(
const struct upb_MiniTable* m, const struct upb_MiniTableField* f) {
UPB_ASSERT(upb_MiniTableField_CType(f) == kUpb_CType_Enum);
return m->UPB_PRIVATE(subs)[f->UPB_PRIVATE(submsg_index)].UPB_PRIVATE(
subenum);
}
UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_MapKey(
const struct upb_MiniTable* m) {
UPB_ASSERT(upb_MiniTable_FieldCount(m) == 2);
const struct upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, 0);
UPB_ASSERT(upb_MiniTableField_Number(f) == 1);
return f;
}
UPB_API_INLINE const struct upb_MiniTableField* upb_MiniTable_MapValue(
const struct upb_MiniTable* m) {
UPB_ASSERT(upb_MiniTable_FieldCount(m) == 2);
const struct upb_MiniTableField* f = upb_MiniTable_GetFieldByIndex(m, 1);
UPB_ASSERT(upb_MiniTableField_Number(f) == 2);
return f;
}
// Computes a bitmask in which the |m->required_count| lowest bits are set.
//
// Sample output:
// requiredmask(1) => 0b10 (0x2)
// requiredmask(5) => 0b111110 (0x3e)
UPB_INLINE uint64_t upb_MiniTable_requiredmask(const struct upb_MiniTable* l) {
int n = l->required_count;
assert(0 < n && n <= 63);
return ((1ULL << n) - 1) << 1;
// RequiredMask(1) => 0b1 (0x1)
// RequiredMask(5) => 0b11111 (0x1f)
UPB_INLINE uint64_t
UPB_PRIVATE(_upb_MiniTable_RequiredMask)(const struct upb_MiniTable* m) {
int n = m->UPB_PRIVATE(required_count);
UPB_ASSERT(0 < n && n <= 64);
return (1ULL << n) - 1;
}
#ifdef UPB_TRACING_ENABLED
UPB_INLINE const char* upb_MiniTable_FullName(
const struct upb_MiniTable* mini_table) {
return mini_table->UPB_PRIVATE(full_name);
}
// Initializes tracing proto name from language runtimes that construct
// mini tables dynamically at runtime. The runtime is responsible for passing
// controlling lifetime of name such as storing in same arena as mini_table.
UPB_INLINE void upb_MiniTable_SetFullName(struct upb_MiniTable* mini_table,
const char* full_name) {
mini_table->UPB_PRIVATE(full_name) = full_name;
}
#endif
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -0,0 +1,77 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
// https://developers.google.com/protocol-buffers/
///
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_
#define UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_
#include <stddef.h>
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// Return the log2 of the storage size in bytes for a upb_CType
UPB_INLINE int UPB_PRIVATE(_upb_CType_SizeLg2)(upb_CType c_type) {
static const int8_t size[] = {
0, // kUpb_CType_Bool
2, // kUpb_CType_Float
2, // kUpb_CType_Int32
2, // kUpb_CType_UInt32
2, // kUpb_CType_Enum
UPB_SIZE(2, 3), // kUpb_CType_Message
3, // kUpb_CType_Double
3, // kUpb_CType_Int64
3, // kUpb_CType_UInt64
UPB_SIZE(3, 4), // kUpb_CType_String
UPB_SIZE(3, 4), // kUpb_CType_Bytes
};
// -1 here because the enum is one-based but the table is zero-based.
return size[c_type - 1];
}
// Return the log2 of the storage size in bytes for a upb_FieldType
UPB_INLINE int UPB_PRIVATE(_upb_FieldType_SizeLg2)(upb_FieldType field_type) {
static const int8_t size[] = {
3, // kUpb_FieldType_Double
2, // kUpb_FieldType_Float
3, // kUpb_FieldType_Int64
3, // kUpb_FieldType_UInt64
2, // kUpb_FieldType_Int32
3, // kUpb_FieldType_Fixed64
2, // kUpb_FieldType_Fixed32
0, // kUpb_FieldType_Bool
UPB_SIZE(3, 4), // kUpb_FieldType_String
UPB_SIZE(2, 3), // kUpb_FieldType_Group
UPB_SIZE(2, 3), // kUpb_FieldType_Message
UPB_SIZE(3, 4), // kUpb_FieldType_Bytes
2, // kUpb_FieldType_UInt32
2, // kUpb_FieldType_Enum
2, // kUpb_FieldType_SFixed32
3, // kUpb_FieldType_SFixed64
2, // kUpb_FieldType_SInt32
3, // kUpb_FieldType_SInt64
};
// -1 here because the enum is one-based but the table is zero-based.
return size[field_type - 1];
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_SIZE_LOG2_H_ */

View File

@@ -4,15 +4,55 @@
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_MINI_TABLE_INTERNAL_SUB_H_
#define UPB_MINI_TABLE_INTERNAL_SUB_H_
#include "upb/mini_table/internal/enum.h"
#include "upb/mini_table/internal/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef union {
const struct upb_MiniTable* const* UPB_PRIVATE(submsg);
const struct upb_MiniTableEnum* UPB_PRIVATE(subenum);
} upb_MiniTableSubInternal;
union upb_MiniTableSub {
const struct upb_MiniTable* submsg;
const struct upb_MiniTableEnum* subenum;
const struct upb_MiniTable* UPB_PRIVATE(submsg);
const struct upb_MiniTableEnum* UPB_PRIVATE(subenum);
};
#ifdef __cplusplus
extern "C" {
#endif
UPB_API_INLINE union upb_MiniTableSub upb_MiniTableSub_FromEnum(
const struct upb_MiniTableEnum* subenum) {
union upb_MiniTableSub out;
out.UPB_PRIVATE(subenum) = subenum;
return out;
}
UPB_API_INLINE union upb_MiniTableSub upb_MiniTableSub_FromMessage(
const struct upb_MiniTable* submsg) {
union upb_MiniTableSub out;
out.UPB_PRIVATE(submsg) = submsg;
return out;
}
UPB_API_INLINE const struct upb_MiniTableEnum* upb_MiniTableSub_Enum(
const union upb_MiniTableSub sub) {
return sub.UPB_PRIVATE(subenum);
}
UPB_API_INLINE const struct upb_MiniTable* upb_MiniTableSub_Message(
const union upb_MiniTableSub sub) {
return sub.UPB_PRIVATE(submsg);
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_INTERNAL_SUB_H_ */

View File

@@ -15,45 +15,55 @@
// Must be last.
#include "upb/port/def.inc"
typedef struct upb_MiniTable upb_MiniTable;
#ifdef __cplusplus
extern "C" {
#endif
typedef struct upb_MiniTable upb_MiniTable;
UPB_API const upb_MiniTableField* upb_MiniTable_FindFieldByNumber(
const upb_MiniTable* table, uint32_t number);
const upb_MiniTable* m, uint32_t number);
UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_GetFieldByIndex(
const upb_MiniTable* t, uint32_t index) {
return &t->fields[index];
}
const upb_MiniTable* m, uint32_t index);
// Returns the MiniTable for this message field. If the field is unlinked,
// returns NULL.
UPB_API_INLINE int upb_MiniTable_FieldCount(const upb_MiniTable* m);
// DEPRECATED: use upb_MiniTable_SubMessage() instead
// Returns the MiniTable for a message field, NULL if the field is unlinked.
UPB_API_INLINE const upb_MiniTable* upb_MiniTable_GetSubMessageTable(
const upb_MiniTable* mini_table, const upb_MiniTableField* field) {
UPB_ASSERT(upb_MiniTableField_CType(field) == kUpb_CType_Message);
const upb_MiniTable* ret =
mini_table->subs[field->UPB_PRIVATE(submsg_index)].submsg;
UPB_ASSUME(ret);
return ret == &_kUpb_MiniTable_Empty ? NULL : ret;
}
const upb_MiniTable* m, const upb_MiniTableField* f);
// Returns the MiniTableEnum for this enum field. If the field is unlinked,
// Returns the MiniTable for a message field if it is a submessage, otherwise
// returns NULL.
//
// WARNING: if dynamic tree shaking is in use, the return value may be the
// "empty", zero-field placeholder message instead of the real message type.
// If the message is later linked, this function will begin returning the real
// message type.
UPB_API_INLINE const upb_MiniTable* upb_MiniTable_SubMessage(
const upb_MiniTable* m, const upb_MiniTableField* f);
// Returns the MiniTable for a map field. The given field must refer to a map.
UPB_API_INLINE const upb_MiniTable* upb_MiniTable_MapEntrySubMessage(
const upb_MiniTable* m, const upb_MiniTableField* f);
// Returns the MiniTableEnum for a message field, NULL if the field is unlinked.
UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTable_GetSubEnumTable(
const upb_MiniTable* mini_table, const upb_MiniTableField* field) {
UPB_ASSERT(upb_MiniTableField_CType(field) == kUpb_CType_Enum);
return mini_table->subs[field->UPB_PRIVATE(submsg_index)].subenum;
}
const upb_MiniTable* m, const upb_MiniTableField* f);
// Returns the MiniTableField for the key of a map.
UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_MapKey(
const upb_MiniTable* m);
// Returns the MiniTableField for the value of a map.
UPB_API_INLINE const upb_MiniTableField* upb_MiniTable_MapValue(
const upb_MiniTable* m);
// Returns true if this MiniTable field is linked to a MiniTable for the
// sub-message.
UPB_API_INLINE bool upb_MiniTable_MessageFieldIsLinked(
const upb_MiniTable* mini_table, const upb_MiniTableField* field) {
return upb_MiniTable_GetSubMessageTable(mini_table, field) != NULL;
}
UPB_API_INLINE bool upb_MiniTable_FieldIsLinked(const upb_MiniTable* m,
const upb_MiniTableField* f);
// If this field is in a oneof, returns the first field in the oneof.
//

View File

@@ -8,8 +8,39 @@
#ifndef UPB_MINI_TABLE_SUB_H_
#define UPB_MINI_TABLE_SUB_H_
#include "upb/mini_table/enum.h"
#include "upb/mini_table/internal/sub.h"
#include "upb/mini_table/message.h"
// Must be last.
#include "upb/port/def.inc"
typedef union upb_MiniTableSub upb_MiniTableSub;
#endif /* UPB_MINI_TABLE_INTERNAL_SUB_H_ */
#ifdef __cplusplus
extern "C" {
#endif
// Constructors
UPB_API_INLINE upb_MiniTableSub
upb_MiniTableSub_FromEnum(const upb_MiniTableEnum* subenum);
UPB_API_INLINE upb_MiniTableSub
upb_MiniTableSub_FromMessage(const upb_MiniTable* submsg);
// Getters
UPB_API_INLINE const upb_MiniTableEnum* upb_MiniTableSub_Enum(
upb_MiniTableSub sub);
UPB_API_INLINE const upb_MiniTable* upb_MiniTableSub_Message(
upb_MiniTableSub sub);
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif /* UPB_MINI_TABLE_SUB_H_ */

View File

@@ -47,6 +47,7 @@
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#ifndef UINTPTR_MAX
Error, UINTPTR_MAX is undefined
@@ -89,6 +90,12 @@ Error, UINTPTR_MAX is undefined
#define UPB_API_INLINE UPB_INLINE
#endif
#ifdef EXPORT_UPBC
#define UPBC_API UPB_EXPORT
#else
#define UPBC_API
#endif
#define UPB_MALLOC_ALIGN 8
#define UPB_ALIGN_UP(size, align) (((size) + (align) - 1) / (align) * (align))
#define UPB_ALIGN_DOWN(size, align) ((size) / (align) * (align))
@@ -99,6 +106,13 @@ Error, UINTPTR_MAX is undefined
#define UPB_ALIGN_OF(type) offsetof (struct { char c; type member; }, member)
#endif
#ifdef _MSC_VER
// Some versions of our Windows compiler don't support the C11 syntax.
#define UPB_ALIGN_AS(x) __declspec(align(x))
#else
#define UPB_ALIGN_AS(x) _Alignas(x)
#endif
// Hints to the compiler about likely/unlikely branches.
#if defined (__GNUC__) || defined(__clang__)
#define UPB_LIKELY(x) __builtin_expect((bool)(x), 1)
@@ -110,17 +124,17 @@ Error, UINTPTR_MAX is undefined
// Macros for function attributes on compilers that support them.
#ifdef __GNUC__
#define UPB_FORCEINLINE __inline__ __attribute__((always_inline))
#define UPB_FORCEINLINE __inline__ __attribute__((always_inline)) static
#define UPB_NOINLINE __attribute__((noinline))
#define UPB_NORETURN __attribute__((__noreturn__))
#define UPB_PRINTF(str, first_vararg) __attribute__((format (printf, str, first_vararg)))
#elif defined(_MSC_VER)
#define UPB_NOINLINE
#define UPB_FORCEINLINE
#define UPB_FORCEINLINE static
#define UPB_NORETURN __declspec(noreturn)
#define UPB_PRINTF(str, first_vararg)
#else /* !defined(__GNUC__) */
#define UPB_FORCEINLINE
#define UPB_FORCEINLINE static
#define UPB_NOINLINE
#define UPB_NORETURN
#define UPB_PRINTF(str, first_vararg)
@@ -168,6 +182,9 @@ Error, UINTPTR_MAX is undefined
#ifdef __APPLE__
#define UPB_SETJMP(buf) _setjmp(buf)
#define UPB_LONGJMP(buf, val) _longjmp(buf, val)
#elif defined(WASM_WAMR)
#define UPB_SETJMP(buf) 0
#define UPB_LONGJMP(buf, val) abort()
#else
#define UPB_SETJMP(buf) setjmp(buf)
#define UPB_LONGJMP(buf, val) longjmp(buf, val)
@@ -185,6 +202,12 @@ Error, UINTPTR_MAX is undefined
#define UPB_PRIVATE(x) x##_dont_copy_me__upb_internal_use_only
#ifdef UPB_ALLOW_PRIVATE_ACCESS__FOR_BITS_ONLY
#define UPB_ONLYBITS(x) x
#else
#define UPB_ONLYBITS(x) UPB_PRIVATE(x)
#endif
/* Configure whether fasttable is switched on or not. *************************/
#ifdef __has_attribute
@@ -296,10 +319,10 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
/* Disable proto2 arena behavior (TEMPORARY) **********************************/
#ifdef UPB_DISABLE_PROTO2_ENUM_CHECKING
#define UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 1
#ifdef UPB_DISABLE_CLOSED_ENUM_CHECKING
#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 1
#else
#define UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3 0
#define UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN 0
#endif
#if defined(__cplusplus)
@@ -317,12 +340,103 @@ void __asan_unpoison_memory_region(void const volatile *addr, size_t size);
#define UPB_DEPRECATED
#endif
// begin:google_only
// #define UPB_IS_GOOGLE3
// end:google_only
#if defined(UPB_IS_GOOGLE3) && !defined(UPB_BOOTSTRAP_STAGE0)
#if defined(UPB_IS_GOOGLE3) && \
(!defined(UPB_BOOTSTRAP_STAGE) || UPB_BOOTSTRAP_STAGE != 0)
#define UPB_DESC(sym) proto2_##sym
#define UPB_DESC_MINITABLE(sym) &proto2__##sym##_msg_init
#elif defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0
#define UPB_DESC(sym) google_protobuf_##sym
#define UPB_DESC_MINITABLE(sym) google__protobuf__##sym##_msg_init()
#else
#define UPB_DESC(sym) google_protobuf_##sym
#define UPB_DESC_MINITABLE(sym) &google__protobuf__##sym##_msg_init
#endif
#undef UPB_IS_GOOGLE3
// Linker arrays combine elements from multiple translation units into a single
// array that can be iterated over at runtime.
//
// It is an alternative to pre-main "registration" functions.
//
// Usage:
//
// // In N translation units.
// UPB_LINKARR_APPEND(foo_array) static int elems[3] = {1, 2, 3};
//
// // At runtime:
// UPB_LINKARR_DECLARE(foo_array, int);
//
// void f() {
// const int* start = UPB_LINKARR_START(foo_array);
// const int* stop = UPB_LINKARR_STOP(foo_array);
// for (const int* p = start; p < stop; p++) {
// // Windows can introduce zero padding, so we have to skip zeroes.
// if (*p != 0) {
// vec.push_back(*p);
// }
// }
// }
#if defined(__ELF__) || defined(__wasm__)
#define UPB_LINKARR_APPEND(name) \
__attribute__((retain, used, section("linkarr_" #name)))
#define UPB_LINKARR_DECLARE(name, type) \
extern type const __start_linkarr_##name; \
extern type const __stop_linkarr_##name; \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1]
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#elif defined(__MACH__)
/* As described in: https://stackoverflow.com/a/22366882 */
#define UPB_LINKARR_APPEND(name) \
__attribute__((retain, used, section("__DATA,__la_" #name)))
#define UPB_LINKARR_DECLARE(name, type) \
extern type const __start_linkarr_##name __asm( \
"section$start$__DATA$__la_" #name); \
extern type const __stop_linkarr_##name __asm( \
"section$end$__DATA$" \
"__la_" #name); \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1]
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#elif defined(_MSC_VER) && defined(__clang__)
/* See:
* https://devblogs.microsoft.com/oldnewthing/20181107-00/?p=100155
* https://devblogs.microsoft.com/oldnewthing/20181108-00/?p=100165
* https://devblogs.microsoft.com/oldnewthing/20181109-00/?p=100175 */
// Usage of __attribute__ here probably means this is Clang-specific, and would
// not work on MSVC.
#define UPB_LINKARR_APPEND(name) \
__declspec(allocate("la_" #name "$j")) __attribute__((retain, used))
#define UPB_LINKARR_DECLARE(name, type) \
__declspec(allocate("la_" #name "$a")) type __start_linkarr_##name; \
__declspec(allocate("la_" #name "$z")) type __stop_linkarr_##name; \
UPB_LINKARR_APPEND(name) type UPB_linkarr_internal_empty_##name[1] = {0}
#define UPB_LINKARR_START(name) (&__start_linkarr_##name)
#define UPB_LINKARR_STOP(name) (&__stop_linkarr_##name)
#else
// Linker arrays are not supported on this platform. Make appends a no-op but
// don't define the other macros.
#define UPB_LINKARR_APPEND(name)
#endif
// Future versions of upb will include breaking changes to some APIs.
// This macro can be set to enable these API changes ahead of time, so that
// user code can be updated before upgrading versions of protobuf.
#ifdef UPB_FUTURE_BREAKING_CHANGES
// Properly enforce closed enums in python.
// Owner: mkruskal@
#define UPB_FUTURE_PYTHON_CLOSED_ENUM_ENFORCEMENT 1
#endif

View File

@@ -13,11 +13,13 @@
#undef UPB_EXPORT
#undef UPB_INLINE
#undef UPB_API
#undef UPBC_API
#undef UPB_API_INLINE
#undef UPB_ALIGN_UP
#undef UPB_ALIGN_DOWN
#undef UPB_ALIGN_MALLOC
#undef UPB_ALIGN_OF
#undef UPB_ALIGN_AS
#undef UPB_MALLOC_ALIGN
#undef UPB_LIKELY
#undef UPB_UNLIKELY
@@ -44,12 +46,20 @@
#undef UPB_ASAN
#undef UPB_ASAN_GUARD_SIZE
#undef UPB_CLANG_ASAN
#undef UPB_TREAT_PROTO2_ENUMS_LIKE_PROTO3
#undef UPB_TREAT_CLOSED_ENUMS_LIKE_OPEN
#undef UPB_DEPRECATED
#undef UPB_GNUC_MIN
#undef UPB_DESCRIPTOR_UPB_H_FILENAME
#undef UPB_DESC
#undef UPB_DESC_MINITABLE
#undef UPB_IS_GOOGLE3
#undef UPB_ATOMIC
#undef UPB_USE_C11_ATOMICS
#undef UPB_PRIVATE
#undef UPB_ONLYBITS
#undef UPB_LINKARR_DECLARE
#undef UPB_LINKARR_APPEND
#undef UPB_LINKARR_START
#undef UPB_LINKARR_STOP
#undef UPB_FUTURE_BREAKING_CHANGES
#undef UPB_FUTURE_PYTHON_CLOSED_ENUM_ENFORCEMENT

View File

@@ -12,17 +12,7 @@
#ifndef UPB_REFLECTION_COMMON_H_
#define UPB_REFLECTION_COMMON_H_
// begin:google_only
// #ifndef UPB_BOOTSTRAP_STAGE0
// #include "net/proto2/proto/descriptor.upb.h"
// #else
// #include "google/protobuf/descriptor.upb.h"
// #endif
// end:google_only
// begin:github_only
#include "google/protobuf/descriptor.upb.h"
// end:github_only
#include "upb/reflection/descriptor_bootstrap.h" // IWYU pragma: export
typedef enum {
kUpb_Syntax_Proto2 = 2,

View File

@@ -8,13 +8,21 @@
#ifndef UPB_REFLECTION_DEF_HPP_
#define UPB_REFLECTION_DEF_HPP_
#include <stdint.h>
#include <cstring>
#include <memory>
#include <string>
#include <vector>
#include "upb/base/descriptor_constants.h"
#include "upb/base/status.hpp"
#include "upb/base/string_view.h"
#include "upb/mem/arena.hpp"
#include "upb/message/value.h"
#include "upb/mini_descriptor/decode.h"
#include "upb/mini_table/enum.h"
#include "upb/mini_table/field.h"
#include "upb/mini_table/message.h"
#include "upb/reflection/def.h"
#include "upb/reflection/internal/def_pool.h"
#include "upb/reflection/internal/enum_def.h"
@@ -53,6 +61,13 @@ class FieldDefPtr {
return upb_FieldDef_MiniTable(ptr_);
}
std::string MiniDescriptorEncode() const {
upb::Arena arena;
upb_StringView md;
upb_FieldDef_MiniDescriptorEncode(ptr_, arena.ptr(), &md);
return std::string(md.data, md.size);
}
const UPB_DESC(FieldOptions) * options() const {
return upb_FieldDef_Options(ptr_);
}
@@ -80,6 +95,9 @@ class FieldDefPtr {
// been finalized.
uint32_t index() const { return upb_FieldDef_Index(ptr_); }
// Index into msgdef->layout->fields or file->exts
uint32_t layout_index() const { return upb_FieldDef_LayoutIndex(ptr_); }
// The MessageDef to which this field belongs (for extensions, the extended
// message).
MessageDefPtr containing_type() const;
@@ -94,6 +112,7 @@ class FieldDefPtr {
OneofDefPtr real_containing_oneof() const;
// Convenient field type tests.
bool IsEnum() const { return upb_FieldDef_IsEnum(ptr_); }
bool IsSubMessage() const { return upb_FieldDef_IsSubMessage(ptr_); }
bool IsString() const { return upb_FieldDef_IsString(ptr_); }
bool IsSequence() const { return upb_FieldDef_IsRepeated(ptr_); }
@@ -194,6 +213,9 @@ class MessageDefPtr {
const char* full_name() const { return upb_MessageDef_FullName(ptr_); }
const char* name() const { return upb_MessageDef_Name(ptr_); }
// Returns the MessageDef that contains this MessageDef (or null).
MessageDefPtr containing_type() const;
const upb_MiniTable* mini_table() const {
return upb_MessageDef_MiniTable(ptr_);
}
@@ -405,10 +427,14 @@ class EnumDefPtr {
const upb_EnumDef* ptr() const { return ptr_; }
explicit operator bool() const { return ptr_ != nullptr; }
FileDefPtr file() const;
const char* full_name() const { return upb_EnumDef_FullName(ptr_); }
const char* name() const { return upb_EnumDef_Name(ptr_); }
bool is_closed() const { return upb_EnumDef_IsClosed(ptr_); }
// Returns the MessageDef that contains this EnumDef (or null).
MessageDefPtr containing_type() const;
// The value that is used as the default when no field default is specified.
// If not set explicitly, the first value that was added will be used.
// The default value must be a member of the enum.
@@ -497,6 +523,10 @@ class FileDefPtr {
return FieldDefPtr(upb_FileDef_TopLevelExtension(ptr_, index));
}
bool resolves(const char* path) const {
return upb_FileDef_Resolves(ptr_, path);
}
explicit operator bool() const { return ptr_ != nullptr; }
friend bool operator==(FileDefPtr lhs, FileDefPtr rhs) {
@@ -555,8 +585,9 @@ class DefPool {
std::unique_ptr<upb_DefPool, decltype(&upb_DefPool_Free)> ptr_;
};
// TODO: This typedef is deprecated. Delete it.
using SymbolTable = DefPool;
inline FileDefPtr EnumDefPtr::file() const {
return FileDefPtr(upb_EnumDef_File(ptr_));
}
inline FileDefPtr FieldDefPtr::file() const {
return FileDefPtr(upb_FieldDef_File(ptr_));
@@ -566,6 +597,14 @@ inline FileDefPtr MessageDefPtr::file() const {
return FileDefPtr(upb_MessageDef_File(ptr_));
}
inline MessageDefPtr MessageDefPtr::containing_type() const {
return MessageDefPtr(upb_MessageDef_ContainingType(ptr_));
}
inline MessageDefPtr EnumDefPtr::containing_type() const {
return MessageDefPtr(upb_EnumDef_ContainingType(ptr_));
}
inline EnumDefPtr MessageDefPtr::enum_type(int i) const {
return EnumDefPtr(upb_MessageDef_NestedEnum(ptr_, i));
}

View File

@@ -26,6 +26,14 @@ UPB_API void upb_DefPool_Free(upb_DefPool* s);
UPB_API upb_DefPool* upb_DefPool_New(void);
UPB_API const UPB_DESC(FeatureSetDefaults) *
upb_DefPool_FeatureSetDefaults(const upb_DefPool* s);
UPB_API bool upb_DefPool_SetFeatureSetDefaults(upb_DefPool* s,
const char* serialized_defaults,
size_t serialized_len,
upb_Status* status);
UPB_API const upb_MessageDef* upb_DefPool_FindMessageByName(
const upb_DefPool* s, const char* sym);
@@ -58,8 +66,8 @@ const upb_FieldDef* upb_DefPool_FindExtensionByNumber(const upb_DefPool* s,
const upb_MessageDef* m,
int32_t fieldnum);
const upb_ServiceDef* upb_DefPool_FindServiceByName(const upb_DefPool* s,
const char* name);
UPB_API const upb_ServiceDef* upb_DefPool_FindServiceByName(
const upb_DefPool* s, const char* name);
const upb_ServiceDef* upb_DefPool_FindServiceByNameWithSize(
const upb_DefPool* s, const char* name, size_t size);

View File

@@ -0,0 +1,19 @@
#ifndef THIRD_PARTY_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H_
#define THIRD_PARTY_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H_
// IWYU pragma: begin_exports
#if defined(UPB_BOOTSTRAP_STAGE) && UPB_BOOTSTRAP_STAGE == 0
// This header is checked in.
#include "upb/reflection/stage0/google/protobuf/descriptor.upb.h"
#elif UPB_BOOTSTRAP_STAGE == 1
// This header is generated at build time by the bootstrapping process.
#include "upb/reflection/stage1/google/protobuf/descriptor.upb.h"
#else
// This is the normal header, generated by upb_c_proto_library().
#include "google/protobuf/descriptor.upb.h"
#endif
// IWYU pragma: end_exports
#endif // THIRD_PARTY_UPB_UPB_REFLECTION_DESCRIPTOR_BOOTSTRAP_H_

View File

@@ -33,6 +33,7 @@ UPB_API const upb_EnumValueDef* upb_EnumDef_FindValueByNumber(
UPB_API const char* upb_EnumDef_FullName(const upb_EnumDef* e);
bool upb_EnumDef_HasOptions(const upb_EnumDef* e);
bool upb_EnumDef_IsClosed(const upb_EnumDef* e);
bool upb_EnumDef_IsSpecifiedAsClosed(const upb_EnumDef* e);
// Creates a mini descriptor string for an enum, returns true on success.
bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
@@ -40,6 +41,7 @@ bool upb_EnumDef_MiniDescriptorEncode(const upb_EnumDef* e, upb_Arena* a,
const char* upb_EnumDef_Name(const upb_EnumDef* e);
const UPB_DESC(EnumOptions) * upb_EnumDef_Options(const upb_EnumDef* e);
const UPB_DESC(FeatureSet) * upb_EnumDef_ResolvedFeatures(const upb_EnumDef* e);
upb_StringView upb_EnumDef_ReservedName(const upb_EnumDef* e, int i);
int upb_EnumDef_ReservedNameCount(const upb_EnumDef* e);

View File

@@ -27,6 +27,8 @@ UPB_API const char* upb_EnumValueDef_Name(const upb_EnumValueDef* v);
UPB_API int32_t upb_EnumValueDef_Number(const upb_EnumValueDef* v);
const UPB_DESC(EnumValueOptions) *
upb_EnumValueDef_Options(const upb_EnumValueDef* v);
const UPB_DESC(FeatureSet) *
upb_EnumValueDef_ResolvedFeatures(const upb_EnumValueDef* e);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -25,6 +25,8 @@ int32_t upb_ExtensionRange_End(const upb_ExtensionRange* r);
bool upb_ExtensionRange_HasOptions(const upb_ExtensionRange* r);
const UPB_DESC(ExtensionRangeOptions) *
upb_ExtensionRange_Options(const upb_ExtensionRange* r);
const UPB_DESC(FeatureSet) *
upb_ExtensionRange_ResolvedFeatures(const upb_ExtensionRange* e);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -10,7 +10,13 @@
#ifndef UPB_REFLECTION_FIELD_DEF_H_
#define UPB_REFLECTION_FIELD_DEF_H_
#include <stdint.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/message/value.h"
#include "upb/mini_table/extension.h"
#include "upb/mini_table/field.h"
#include "upb/reflection/common.h"
// Must be last.
@@ -39,10 +45,11 @@ bool upb_FieldDef_HasOptions(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_HasPresence(const upb_FieldDef* f);
bool upb_FieldDef_HasSubDef(const upb_FieldDef* f);
uint32_t upb_FieldDef_Index(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsEnum(const upb_FieldDef* f);
bool upb_FieldDef_IsExtension(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsMap(const upb_FieldDef* f);
bool upb_FieldDef_IsOptional(const upb_FieldDef* f);
bool upb_FieldDef_IsPacked(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsPacked(const upb_FieldDef* f);
bool upb_FieldDef_IsPrimitive(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsRepeated(const upb_FieldDef* f);
bool upb_FieldDef_IsRequired(const upb_FieldDef* f);
@@ -50,17 +57,23 @@ bool upb_FieldDef_IsString(const upb_FieldDef* f);
UPB_API bool upb_FieldDef_IsSubMessage(const upb_FieldDef* f);
UPB_API const char* upb_FieldDef_JsonName(const upb_FieldDef* f);
UPB_API upb_Label upb_FieldDef_Label(const upb_FieldDef* f);
uint32_t upb_FieldDef_LayoutIndex(const upb_FieldDef* f);
UPB_API const upb_MessageDef* upb_FieldDef_MessageSubDef(const upb_FieldDef* f);
bool _upb_FieldDef_ValidateUtf8(const upb_FieldDef* f);
bool _upb_FieldDef_IsGroupLike(const upb_FieldDef* f);
// Creates a mini descriptor string for a field, returns true on success.
bool upb_FieldDef_MiniDescriptorEncode(const upb_FieldDef* f, upb_Arena* a,
upb_StringView* out);
const upb_MiniTableField* upb_FieldDef_MiniTable(const upb_FieldDef* f);
const upb_MiniTableExtension* upb_FieldDef_MiniTableExtension(
const upb_FieldDef* f);
UPB_API const char* upb_FieldDef_Name(const upb_FieldDef* f);
UPB_API uint32_t upb_FieldDef_Number(const upb_FieldDef* f);
const UPB_DESC(FieldOptions) * upb_FieldDef_Options(const upb_FieldDef* f);
const UPB_DESC(FeatureSet) *
upb_FieldDef_ResolvedFeatures(const upb_FieldDef* f);
UPB_API const upb_OneofDef* upb_FieldDef_RealContainingOneof(
const upb_FieldDef* f);
UPB_API upb_FieldType upb_FieldDef_Type(const upb_FieldDef* f);

View File

@@ -19,11 +19,14 @@
extern "C" {
#endif
UPB_API const char* upb_FileDef_EditionName(int edition);
const upb_FileDef* upb_FileDef_Dependency(const upb_FileDef* f, int i);
int upb_FileDef_DependencyCount(const upb_FileDef* f);
bool upb_FileDef_HasOptions(const upb_FileDef* f);
UPB_API const char* upb_FileDef_Name(const upb_FileDef* f);
const UPB_DESC(FileOptions) * upb_FileDef_Options(const upb_FileDef* f);
const UPB_DESC(FeatureSet) * upb_FileDef_ResolvedFeatures(const upb_FileDef* f);
const char* upb_FileDef_Package(const upb_FileDef* f);
UPB_DESC(Edition) upb_FileDef_Edition(const upb_FileDef* f);
UPB_API const upb_DefPool* upb_FileDef_Pool(const upb_FileDef* f);
@@ -48,6 +51,9 @@ int upb_FileDef_TopLevelMessageCount(const upb_FileDef* f);
const upb_FileDef* upb_FileDef_WeakDependency(const upb_FileDef* f, int i);
int upb_FileDef_WeakDependencyCount(const upb_FileDef* f);
// Returns whether |symbol| is transitively included by |f|
bool upb_FileDef_Resolves(const upb_FileDef* f, const char* symbol);
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -36,6 +36,10 @@ extern "C" {
struct upb_DefBuilder {
upb_DefPool* symtab;
upb_strtable feature_cache; // Caches features by identity.
UPB_DESC(FeatureSet*) legacy_features; // For computing legacy features.
char* tmp_buf; // Temporary buffer in tmp_arena.
size_t tmp_buf_size; // Size of temporary buffer.
upb_FileDef* file; // File we are building.
upb_Arena* arena; // Allocate defs here.
upb_Arena* tmp_arena; // For temporary allocations.
@@ -128,6 +132,25 @@ UPB_INLINE void _upb_DefBuilder_CheckIdentFull(upb_DefBuilder* ctx,
if (!good) _upb_DefBuilder_CheckIdentSlow(ctx, name, true);
}
// Returns true if the returned feature set is new and must be populated.
bool _upb_DefBuilder_GetOrCreateFeatureSet(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
upb_StringView key,
UPB_DESC(FeatureSet**) set);
const UPB_DESC(FeatureSet*)
_upb_DefBuilder_DoResolveFeatures(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
const UPB_DESC(FeatureSet*) child,
bool is_implicit);
UPB_INLINE const UPB_DESC(FeatureSet*)
_upb_DefBuilder_ResolveFeatures(upb_DefBuilder* ctx,
const UPB_DESC(FeatureSet*) parent,
const UPB_DESC(FeatureSet*) child) {
return _upb_DefBuilder_DoResolveFeatures(ctx, parent, child, false);
}
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -22,10 +22,11 @@ bool _upb_EnumDef_Insert(upb_EnumDef* e, upb_EnumValueDef* v, upb_Arena* a);
const upb_MiniTableEnum* _upb_EnumDef_MiniTable(const upb_EnumDef* e);
// Allocate and initialize an array of |n| enum defs.
upb_EnumDef* _upb_EnumDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto) * const* protos,
const upb_MessageDef* containing_type);
upb_EnumDef* _upb_EnumDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const upb_MessageDef* containing_type);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -23,7 +23,7 @@ upb_EnumReservedRange* _upb_EnumReservedRange_At(const upb_EnumReservedRange* r,
// Allocate and initialize an array of |n| reserved ranges owned by |e|.
upb_EnumReservedRange* _upb_EnumReservedRanges_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(EnumDescriptorProto_EnumReservedRange) * const* protos,
const UPB_DESC(EnumDescriptorProto_EnumReservedRange*) const* protos,
const upb_EnumDef* e);
#ifdef __cplusplus

View File

@@ -22,7 +22,8 @@ upb_EnumValueDef* _upb_EnumValueDef_At(const upb_EnumValueDef* v, int i);
// Allocate and initialize an array of |n| enum value defs owned by |e|.
upb_EnumValueDef* _upb_EnumValueDefs_New(
upb_DefBuilder* ctx, const char* prefix, int n,
const UPB_DESC(EnumValueDescriptorProto) * const* protos, upb_EnumDef* e,
const UPB_DESC(EnumValueDescriptorProto*) const* protos,
const UPB_DESC(FeatureSet*) parent_features, upb_EnumDef* e,
bool* is_sorted);
const upb_EnumValueDef** _upb_EnumValueDefs_Sorted(const upb_EnumValueDef* v,

View File

@@ -22,8 +22,8 @@ upb_ExtensionRange* _upb_ExtensionRange_At(const upb_ExtensionRange* r, int i);
// Allocate and initialize an array of |n| extension ranges owned by |m|.
upb_ExtensionRange* _upb_ExtensionRanges_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto_ExtensionRange) * const* protos,
const upb_MessageDef* m);
const UPB_DESC(DescriptorProto_ExtensionRange*) const* protos,
const UPB_DESC(FeatureSet*) parent_features, const upb_MessageDef* m);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -19,8 +19,6 @@ extern "C" {
upb_FieldDef* _upb_FieldDef_At(const upb_FieldDef* f, int i);
const upb_MiniTableExtension* _upb_FieldDef_ExtensionMiniTable(
const upb_FieldDef* f);
bool _upb_FieldDef_IsClosedEnum(const upb_FieldDef* f);
bool _upb_FieldDef_IsProto3Optional(const upb_FieldDef* f);
int _upb_FieldDef_LayoutIndex(const upb_FieldDef* f);
@@ -31,16 +29,19 @@ void _upb_FieldDef_BuildMiniTableExtension(upb_DefBuilder* ctx,
const upb_FieldDef* f);
// Allocate and initialize an array of |n| extensions (field defs).
upb_FieldDef* _upb_Extensions_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
upb_MessageDef* m);
upb_FieldDef* _upb_Extensions_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const char* prefix, upb_MessageDef* m);
// Allocate and initialize an array of |n| field defs.
upb_FieldDef* _upb_FieldDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto) * const* protos, const char* prefix,
upb_MessageDef* m, bool* is_sorted);
upb_FieldDef* _upb_FieldDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(FieldDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
const char* prefix, upb_MessageDef* m,
bool* is_sorted);
// Allocate and return a list of pointers to the |n| field defs in |ff|,
// sorted by field number.

View File

@@ -30,9 +30,12 @@ void _upb_MessageDef_LinkMiniTable(upb_DefBuilder* ctx,
void _upb_MessageDef_Resolve(upb_DefBuilder* ctx, upb_MessageDef* m);
// Allocate and initialize an array of |n| message defs.
upb_MessageDef* _upb_MessageDefs_New(
upb_DefBuilder* ctx, int n, const UPB_DESC(DescriptorProto) * const* protos,
const upb_MessageDef* containing_type);
upb_MessageDef* _upb_MessageDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(DescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*)
parent_features,
const upb_MessageDef* containing_type);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -20,9 +20,11 @@ extern "C" {
upb_MethodDef* _upb_MethodDef_At(const upb_MethodDef* m, int i);
// Allocate and initialize an array of |n| method defs owned by |s|.
upb_MethodDef* _upb_MethodDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(MethodDescriptorProto) * const* protos, upb_ServiceDef* s);
upb_MethodDef* _upb_MethodDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(MethodDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
upb_ServiceDef* s);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -22,9 +22,11 @@ void _upb_OneofDef_Insert(upb_DefBuilder* ctx, upb_OneofDef* o,
const upb_FieldDef* f, const char* name, size_t size);
// Allocate and initialize an array of |n| oneof defs owned by |m|.
upb_OneofDef* _upb_OneofDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(OneofDescriptorProto) * const* protos, upb_MessageDef* m);
upb_OneofDef* _upb_OneofDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(OneofDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*) parent_features,
upb_MessageDef* m);
size_t _upb_OneofDefs_Finalize(upb_DefBuilder* ctx, upb_MessageDef* m);

View File

@@ -20,9 +20,11 @@ extern "C" {
upb_ServiceDef* _upb_ServiceDef_At(const upb_ServiceDef* s, int i);
// Allocate and initialize an array of |n| service defs.
upb_ServiceDef* _upb_ServiceDefs_New(
upb_DefBuilder* ctx, int n,
const UPB_DESC(ServiceDescriptorProto) * const* protos);
upb_ServiceDef* _upb_ServiceDefs_New(upb_DefBuilder* ctx, int n,
const UPB_DESC(ServiceDescriptorProto*)
const* protos,
const UPB_DESC(FeatureSet*)
parent_features);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -0,0 +1,20 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_
#define UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_
// This file contains the serialized FeatureSetDefaults object for
// language-independent features and (possibly at some point) for upb-specific
// features. This is used for feature resolution under Editions.
// NOLINTBEGIN
// clang-format off
#define UPB_INTERNAL_UPB_EDITION_DEFAULTS "\n\023\030\204\007\"\000*\014\010\001\020\002\030\002 \003(\0010\002\n\023\030\347\007\"\000*\014\010\002\020\001\030\001 \002(\0010\001\n\023\030\350\007\"\014\010\001\020\001\030\001 \002(\0010\001*\000 \346\007(\350\007"
// clang-format on
// NOLINTEND
#endif // UPB_REFLECTION_UPB_EDITION_DEFAULTS_H_

View File

@@ -12,8 +12,7 @@
#include "upb/mem/arena.h"
#include "upb/message/map.h"
#include "upb/message/types.h"
#include "upb/message/value.h" // IWYU pragma: export
#include "upb/message/message.h"
#include "upb/reflection/common.h"
// Must be last.
@@ -31,8 +30,8 @@ UPB_API upb_MutableMessageValue upb_Message_Mutable(upb_Message* msg,
upb_Arena* a);
// Returns the field that is set in the oneof, or NULL if none are set.
UPB_API const upb_FieldDef* upb_Message_WhichOneof(const upb_Message* msg,
const upb_OneofDef* o);
UPB_API const upb_FieldDef* upb_Message_WhichOneofByDef(const upb_Message* msg,
const upb_OneofDef* o);
// Clear all data and unknown fields.
void upb_Message_ClearByDef(upb_Message* msg, const upb_MessageDef* m);
@@ -72,9 +71,10 @@ UPB_API bool upb_Message_SetFieldByDef(upb_Message* msg, const upb_FieldDef* f,
#define kUpb_Message_Begin -1
bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool, const upb_FieldDef** f,
upb_MessageValue* val, size_t* iter);
UPB_API bool upb_Message_Next(const upb_Message* msg, const upb_MessageDef* m,
const upb_DefPool* ext_pool,
const upb_FieldDef** f, upb_MessageValue* val,
size_t* iter);
// Clears all unknown field data from this message and all submessages.
UPB_API bool upb_Message_DiscardUnknown(upb_Message* msg,

View File

@@ -136,6 +136,8 @@ int upb_MessageDef_RealOneofCount(const upb_MessageDef* m);
const UPB_DESC(MessageOptions) *
upb_MessageDef_Options(const upb_MessageDef* m);
const UPB_DESC(FeatureSet) *
upb_MessageDef_ResolvedFeatures(const upb_MessageDef* m);
upb_StringView upb_MessageDef_ReservedName(const upb_MessageDef* m, int i);
int upb_MessageDef_ReservedNameCount(const upb_MessageDef* m);

View File

@@ -19,16 +19,19 @@
extern "C" {
#endif
bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m);
UPB_API bool upb_MethodDef_ClientStreaming(const upb_MethodDef* m);
const char* upb_MethodDef_FullName(const upb_MethodDef* m);
bool upb_MethodDef_HasOptions(const upb_MethodDef* m);
int upb_MethodDef_Index(const upb_MethodDef* m);
const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m);
const char* upb_MethodDef_Name(const upb_MethodDef* m);
const UPB_DESC(MethodOptions) * upb_MethodDef_Options(const upb_MethodDef* m);
const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m);
bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m);
const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m);
UPB_API const upb_MessageDef* upb_MethodDef_InputType(const upb_MethodDef* m);
UPB_API const char* upb_MethodDef_Name(const upb_MethodDef* m);
UPB_API const UPB_DESC(MethodOptions) *
upb_MethodDef_Options(const upb_MethodDef* m);
const UPB_DESC(FeatureSet) *
upb_MethodDef_ResolvedFeatures(const upb_MethodDef* m);
UPB_API const upb_MessageDef* upb_MethodDef_OutputType(const upb_MethodDef* m);
UPB_API bool upb_MethodDef_ServerStreaming(const upb_MethodDef* m);
UPB_API const upb_ServiceDef* upb_MethodDef_Service(const upb_MethodDef* m);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -36,7 +36,9 @@ const upb_FieldDef* upb_OneofDef_LookupNumber(const upb_OneofDef* o,
uint32_t num);
UPB_API const char* upb_OneofDef_Name(const upb_OneofDef* o);
int upb_OneofDef_numfields(const upb_OneofDef* o);
const UPB_DESC(OneofOptions) * upb_OneofDef_Options(const upb_OneofDef* o);
const UPB_DESC(OneofOptions*) upb_OneofDef_Options(const upb_OneofDef* o);
const UPB_DESC(FeatureSet*)
upb_OneofDef_ResolvedFeatures(const upb_OneofDef* o);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -19,17 +19,20 @@
extern "C" {
#endif
const upb_FileDef* upb_ServiceDef_File(const upb_ServiceDef* s);
UPB_API const upb_FileDef* upb_ServiceDef_File(const upb_ServiceDef* s);
const upb_MethodDef* upb_ServiceDef_FindMethodByName(const upb_ServiceDef* s,
const char* name);
const char* upb_ServiceDef_FullName(const upb_ServiceDef* s);
UPB_API const char* upb_ServiceDef_FullName(const upb_ServiceDef* s);
bool upb_ServiceDef_HasOptions(const upb_ServiceDef* s);
int upb_ServiceDef_Index(const upb_ServiceDef* s);
const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s, int i);
int upb_ServiceDef_MethodCount(const upb_ServiceDef* s);
UPB_API const upb_MethodDef* upb_ServiceDef_Method(const upb_ServiceDef* s,
int i);
UPB_API int upb_ServiceDef_MethodCount(const upb_ServiceDef* s);
const char* upb_ServiceDef_Name(const upb_ServiceDef* s);
const UPB_DESC(ServiceOptions) *
UPB_API const UPB_DESC(ServiceOptions) *
upb_ServiceDef_Options(const upb_ServiceDef* s);
const UPB_DESC(FeatureSet) *
upb_ServiceDef_ResolvedFeatures(const upb_ServiceDef* s);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -9,6 +9,7 @@
#define UPB_TEXT_ENCODE_H_
#include "upb/reflection/def.h"
#include "upb/text/options.h" // IWYU pragma: export
// Must be last.
#include "upb/port/def.inc"
@@ -17,17 +18,6 @@
extern "C" {
#endif
enum {
// When set, prints everything on a single line.
UPB_TXTENC_SINGLELINE = 1,
// When set, unknown fields are not printed.
UPB_TXTENC_SKIPUNKNOWN = 2,
// When set, maps are *not* sorted (this avoids allocating tmp mem).
UPB_TXTENC_NOSORT = 4
};
/* Encodes the given |msg| to text format. The message's reflection is given in
* |m|. The symtab in |symtab| is used to find extensions (if NULL, extensions
* will not be printed).

View File

@@ -0,0 +1,240 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_TEXT_ENCODE_INTERNAL_H_
#define UPB_TEXT_ENCODE_INTERNAL_H_
#include <stdarg.h>
#include <string.h>
#include "upb/base/descriptor_constants.h"
#include "upb/base/string_view.h"
#include "upb/message/array.h"
#include "upb/message/internal/map_sorter.h"
#include "upb/port/vsnprintf_compat.h"
#include "upb/text/options.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "utf8_range.h"
// Must be last.
#include "upb/port/def.inc"
typedef struct {
char *buf, *ptr, *end;
size_t overflow;
int indent_depth;
int options;
const struct upb_DefPool* ext_pool;
_upb_mapsorter sorter;
} txtenc;
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_PutBytes)(txtenc* e,
const void* data,
size_t len) {
size_t have = e->end - e->ptr;
if (UPB_LIKELY(have >= len)) {
memcpy(e->ptr, data, len);
e->ptr += len;
} else {
if (have) {
memcpy(e->ptr, data, have);
e->ptr += have;
}
e->overflow += (len - have);
}
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_PutStr)(txtenc* e,
const char* str) {
UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, str, strlen(str));
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Printf)(txtenc* e, const char* fmt,
...) {
size_t n;
size_t have = e->end - e->ptr;
va_list args;
va_start(args, fmt);
n = _upb_vsnprintf(e->ptr, have, fmt, args);
va_end(args);
if (UPB_LIKELY(have > n)) {
e->ptr += n;
} else {
e->ptr = UPB_PTRADD(e->ptr, have);
e->overflow += (n - have);
}
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Indent)(txtenc* e) {
if ((e->options & UPB_TXTENC_SINGLELINE) == 0) {
int i = e->indent_depth;
while (i-- > 0) {
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, " ");
}
}
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_EndField)(txtenc* e) {
if (e->options & UPB_TXTENC_SINGLELINE) {
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, " ");
} else {
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\n");
}
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Escaped)(txtenc* e,
unsigned char ch) {
switch (ch) {
case '\n':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\n");
break;
case '\r':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\r");
break;
case '\t':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\t");
break;
case '\"':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\\"");
break;
case '\'':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\'");
break;
case '\\':
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\\\\");
break;
default:
UPB_PRIVATE(_upb_TextEncode_Printf)(e, "\\%03o", ch);
break;
}
}
// Returns true if `ch` needs to be escaped in TextFormat, independent of any
// UTF-8 validity issues.
UPB_INLINE bool UPB_PRIVATE(_upb_DefinitelyNeedsEscape)(unsigned char ch) {
if (ch < 32) return true;
switch (ch) {
case '\"':
case '\'':
case '\\':
case 127:
return true;
}
return false;
}
UPB_INLINE bool UPB_PRIVATE(_upb_AsciiIsPrint)(unsigned char ch) {
return ch >= 32 && ch < 127;
}
// Returns true if this is a high byte that requires UTF-8 validation. If the
// UTF-8 validation fails, we must escape the byte.
UPB_INLINE bool UPB_PRIVATE(_upb_NeedsUtf8Validation)(unsigned char ch) {
return ch > 127;
}
// Returns the number of bytes in the prefix of `val` that do not need escaping.
// This is like utf8_range::SpanStructurallyValid(), except that it also
// terminates at any ASCII char that needs to be escaped in TextFormat (any char
// that has `DefinitelyNeedsEscape(ch) == true`).
//
// If we could get a variant of utf8_range::SpanStructurallyValid() that could
// terminate on any of these chars, that might be more efficient, but it would
// be much more complicated to modify that heavily SIMD code.
UPB_INLINE size_t UPB_PRIVATE(_SkipPassthroughBytes)(const char* ptr,
size_t size) {
for (size_t i = 0; i < size; i++) {
unsigned char uc = ptr[i];
if (UPB_PRIVATE(_upb_DefinitelyNeedsEscape)(uc)) return i;
if (UPB_PRIVATE(_upb_NeedsUtf8Validation)(uc)) {
// Find the end of this region of consecutive high bytes, so that we only
// give high bytes to the UTF-8 checker. This avoids needing to perform
// a second scan of the ASCII characters looking for characters that
// need escaping.
//
// We assume that high bytes are less frequent than plain, printable ASCII
// bytes, so we accept the double-scan of high bytes.
size_t end = i + 1;
for (; end < size; end++) {
if (!UPB_PRIVATE(_upb_NeedsUtf8Validation)(ptr[end])) break;
}
size_t n = end - i;
size_t ok = utf8_range_ValidPrefix(ptr + i, n);
if (ok != n) return i + ok;
i += ok - 1;
}
}
return size;
}
UPB_INLINE void UPB_PRIVATE(_upb_HardenedPrintString)(txtenc* e,
const char* ptr,
size_t len) {
// Print as UTF-8, while guarding against any invalid UTF-8 in the string
// field.
//
// If in the future we have a guaranteed invariant that invalid UTF-8 will
// never be present, we could avoid the UTF-8 check here.
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\"");
const char* end = ptr + len;
while (ptr < end) {
size_t n = UPB_PRIVATE(_SkipPassthroughBytes)(ptr, end - ptr);
if (n != 0) {
UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, ptr, n);
ptr += n;
if (ptr == end) break;
}
// If repeated calls to CEscape() and PrintString() are expensive, we could
// consider batching them, at the cost of some complexity.
UPB_PRIVATE(_upb_TextEncode_Escaped)(e, *ptr);
ptr++;
}
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\"");
}
UPB_INLINE void UPB_PRIVATE(_upb_TextEncode_Bytes)(txtenc* e,
upb_StringView data) {
const char* ptr = data.data;
const char* end = ptr + data.size;
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\"");
for (; ptr < end; ptr++) {
unsigned char uc = *ptr;
if (UPB_PRIVATE(_upb_AsciiIsPrint)(uc)) {
UPB_PRIVATE(_upb_TextEncode_PutBytes)(e, ptr, 1);
} else {
UPB_PRIVATE(_upb_TextEncode_Escaped)(e, uc);
}
}
UPB_PRIVATE(_upb_TextEncode_PutStr)(e, "\"");
}
UPB_INLINE size_t UPB_PRIVATE(_upb_TextEncode_Nullz)(txtenc* e, size_t size) {
size_t ret = e->ptr - e->buf + e->overflow;
if (size > 0) {
if (e->ptr == e->end) e->ptr--;
*e->ptr = '\0';
}
return ret;
}
const char* UPB_PRIVATE(_upb_TextEncode_Unknown)(txtenc* e, const char* ptr,
upb_EpsCopyInputStream* stream,
int groupnum);
// Must not be called for ctype = kUpb_CType_Enum, as they require different
// handling depending on whether or not we're doing reflection-based encoding.
void UPB_PRIVATE(_upb_TextEncode_Scalar)(txtenc* e, upb_MessageValue val,
upb_CType ctype);
#include "upb/port/undef.inc"
#endif // UPB_TEXT_ENCODE_INTERNAL_H_

22
Pods/gRPC-C++/third_party/upb/upb/text/options.h generated vendored Normal file
View File

@@ -0,0 +1,22 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2024 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_TEXT_OPTIONS_H_
#define UPB_TEXT_OPTIONS_H_
enum {
// When set, prints everything on a single line.
UPB_TXTENC_SINGLELINE = 1,
// When set, unknown fields are not printed.
UPB_TXTENC_SKIPUNKNOWN = 2,
// When set, maps are *not* sorted (this avoids allocating tmp mem).
UPB_TXTENC_NOSORT = 4
};
#endif // UPB_TEXT_OPTIONS_H_

View File

@@ -45,7 +45,7 @@ enum {
* already has some sub-message fields present. If the sub-message does
* not occur in the binary payload, we will never visit it and discover the
* incomplete sub-message. For this reason, this check is only useful for
* implemting ParseFromString() semantics. For MergeFromString(), a
* implementing ParseFromString() semantics. For MergeFromString(), a
* post-parse validation step will always be necessary. */
kUpb_DecodeOption_CheckRequired = 2,
@@ -83,6 +83,16 @@ enum {
* be created by the parser or the message-copying logic in message/copy.h.
*/
kUpb_DecodeOption_ExperimentalAllowUnlinked = 4,
/* EXPERIMENTAL:
*
* If set, decoding will enforce UTF-8 validation for string fields, even for
* proto2 or fields with `features.utf8_validation = NONE`. Normally, only
* proto3 string fields will be validated for UTF-8. Decoding will return
* kUpb_DecodeStatus_BadUtf8 for non-UTF-8 strings, which is the same behavior
* as non-UTF-8 proto3 string fields.
*/
kUpb_DecodeOption_AlwaysValidateUtf8 = 8,
};
UPB_INLINE uint32_t upb_DecodeOptions_MaxDepth(uint16_t depth) {
@@ -100,6 +110,7 @@ UPB_INLINE int upb_Decode_LimitDepth(uint32_t decode_options, uint32_t limit) {
return upb_DecodeOptions_MaxDepth(max_depth) | (decode_options & 0xffff);
}
// LINT.IfChange
typedef enum {
kUpb_DecodeStatus_Ok = 0,
kUpb_DecodeStatus_Malformed = 1, // Wire format was corrupt
@@ -117,12 +128,24 @@ typedef enum {
// of options.
kUpb_DecodeStatus_UnlinkedSubMessage = 6,
} upb_DecodeStatus;
// LINT.ThenChange(//depot/google3/third_party/protobuf/rust/upb.rs:decode_status)
UPB_API upb_DecodeStatus upb_Decode(const char* buf, size_t size,
upb_Message* msg, const upb_MiniTable* l,
upb_Message* msg, const upb_MiniTable* mt,
const upb_ExtensionRegistry* extreg,
int options, upb_Arena* arena);
// Same as upb_Decode but with a varint-encoded length prepended.
// On success 'num_bytes_read' will be set to the how many bytes were read,
// on failure the contents of num_bytes_read is undefined.
UPB_API upb_DecodeStatus upb_DecodeLengthPrefixed(
const char* buf, size_t size, upb_Message* msg, size_t* num_bytes_read,
const upb_MiniTable* mt, const upb_ExtensionRegistry* extreg, int options,
upb_Arena* arena);
// Utility function for wrapper languages to get an error string from a
// upb_DecodeStatus.
UPB_API const char* upb_DecodeStatus_String(upb_DecodeStatus status);
#ifdef __cplusplus
} /* extern "C" */
#endif

View File

@@ -14,6 +14,7 @@
#include <stdint.h>
#include "upb/mem/arena.h"
#include "upb/message/message.h"
#include "upb/mini_table/message.h"
// Must be last.
@@ -32,13 +33,14 @@ enum {
* memory during encode. */
kUpb_EncodeOption_Deterministic = 1,
// When set, unknown fields are not printed.
// When set, unknown fields are not encoded.
kUpb_EncodeOption_SkipUnknown = 2,
// When set, the encode will fail if any required fields are missing.
kUpb_EncodeOption_CheckRequired = 4,
};
// LINT.IfChange
typedef enum {
kUpb_EncodeStatus_Ok = 0,
kUpb_EncodeStatus_OutOfMemory = 1, // Arena alloc failed
@@ -47,6 +49,7 @@ typedef enum {
// kUpb_EncodeOption_CheckRequired failed but the parse otherwise succeeded.
kUpb_EncodeStatus_MissingRequired = 3,
} upb_EncodeStatus;
// LINT.ThenChange(//depot/google3/third_party/protobuf/rust/upb.rs:encode_status)
UPB_INLINE uint32_t upb_EncodeOptions_MaxDepth(uint16_t depth) {
return (uint32_t)depth << 16;
@@ -63,9 +66,18 @@ UPB_INLINE int upb_Encode_LimitDepth(uint32_t encode_options, uint32_t limit) {
return upb_EncodeOptions_MaxDepth(max_depth) | (encode_options & 0xffff);
}
UPB_API upb_EncodeStatus upb_Encode(const void* msg, const upb_MiniTable* l,
int options, upb_Arena* arena, char** buf,
size_t* size);
UPB_API upb_EncodeStatus upb_Encode(const upb_Message* msg,
const upb_MiniTable* l, int options,
upb_Arena* arena, char** buf, size_t* size);
// Encodes the message prepended by a varint of the serialized length.
UPB_API upb_EncodeStatus upb_EncodeLengthPrefixed(const upb_Message* msg,
const upb_MiniTable* l,
int options, upb_Arena* arena,
char** buf, size_t* size);
// Utility function for wrapper languages to get an error string from a
// upb_EncodeStatus.
UPB_API const char* upb_EncodeStatus_String(upb_EncodeStatus status);
#ifdef __cplusplus
} /* extern "C" */

View File

@@ -37,8 +37,8 @@ typedef struct {
const char* end; // Can read up to SlopBytes bytes beyond this.
const char* limit_ptr; // For bounds checks, = end + UPB_MIN(limit, 0)
uintptr_t aliasing;
int limit; // Submessage limit relative to end
bool error; // To distinguish between EOF and error.
int limit; // Submessage limit relative to end
bool error; // To distinguish between EOF and error.
char patch[kUpb_EpsCopyInputStream_SlopBytes * 2];
} upb_EpsCopyInputStream;
@@ -375,7 +375,7 @@ typedef const char* upb_EpsCopyInputStream_ParseDelimitedFunc(
// fits within this buffer, calls `func` with `ctx` as a parameter, where the
// pushing and popping of limits is handled automatically and with lower cost
// than the normal PushLimit()/PopLimit() sequence.
static UPB_FORCEINLINE bool upb_EpsCopyInputStream_TryParseDelimitedFast(
UPB_FORCEINLINE bool upb_EpsCopyInputStream_TryParseDelimitedFast(
upb_EpsCopyInputStream* e, const char** ptr, int len,
upb_EpsCopyInputStream_ParseDelimitedFunc* func, void* ctx) {
if (!upb_EpsCopyInputStream_CheckSubMessageSizeAvailable(e, *ptr, len)) {

View File

@@ -39,8 +39,8 @@
// - '1' for one-byte tags (field numbers 1-15)
// - '2' for two-byte tags (field numbers 16-2048)
#ifndef UPB_WIRE_DECODE_FAST_H_
#define UPB_WIRE_DECODE_FAST_H_
#ifndef UPB_WIRE_INTERNAL_DECODE_FAST_H_
#define UPB_WIRE_INTERNAL_DECODE_FAST_H_
#include "upb/message/message.h"
@@ -110,6 +110,7 @@ TAGBYTES(o)
TAGBYTES(r)
#undef F
#undef UTF8
#undef TAGBYTES
/* sub-message fields *********************************************************/
@@ -132,9 +133,9 @@ TAGBYTES(s)
TAGBYTES(o)
TAGBYTES(r)
#undef TAGBYTES
#undef SIZES
#undef F
#undef SIZES
#undef TAGBYTES
#undef UPB_PARSE_PARAMS
@@ -144,4 +145,4 @@ TAGBYTES(r)
#include "upb/port/undef.inc"
#endif /* UPB_WIRE_DECODE_FAST_H_ */
#endif /* UPB_WIRE_INTERNAL_DECODE_FAST_H_ */

View File

@@ -10,8 +10,8 @@
* decode.c and decode_fast.c.
*/
#ifndef UPB_WIRE_INTERNAL_DECODE_H_
#define UPB_WIRE_INTERNAL_DECODE_H_
#ifndef UPB_WIRE_INTERNAL_DECODER_H_
#define UPB_WIRE_INTERNAL_DECODER_H_
#include "upb/mem/internal/arena.h"
#include "upb/message/internal/message.h"
@@ -33,7 +33,10 @@ typedef struct upb_Decoder {
uint32_t end_group; // field number of END_GROUP tag, else DECODE_NOGROUP.
uint16_t options;
bool missing_required;
upb_Arena arena;
union {
upb_Arena arena;
void* foo[UPB_ARENA_SIZE_HACK];
};
upb_DecodeStatus status;
jmp_buf err;
@@ -56,36 +59,17 @@ extern const uint8_t upb_utf8_offsets[];
UPB_INLINE
bool _upb_Decoder_VerifyUtf8Inline(const char* ptr, int len) {
const char* end = ptr + len;
// Check 8 bytes at a time for any non-ASCII char.
while (end - ptr >= 8) {
uint64_t data;
memcpy(&data, ptr, 8);
if (data & 0x8080808080808080) goto non_ascii;
ptr += 8;
}
// Check one byte at a time for non-ASCII.
while (ptr < end) {
if (*ptr & 0x80) goto non_ascii;
ptr++;
}
return true;
non_ascii:
return utf8_range2((const unsigned char*)ptr, end - ptr) == 0;
return utf8_range_IsValid(ptr, len);
}
const char* _upb_Decoder_CheckRequired(upb_Decoder* d, const char* ptr,
const upb_Message* msg,
const upb_MiniTable* l);
const upb_MiniTable* m);
/* x86-64 pointers always have the high 16 bits matching. So we can shift
* left 8 and right 8 without loss of information. */
UPB_INLINE intptr_t decode_totable(const upb_MiniTable* tablep) {
return ((intptr_t)tablep << 8) | tablep->table_mask;
return ((intptr_t)tablep << 8) | tablep->UPB_PRIVATE(table_mask);
}
UPB_INLINE const upb_MiniTable* decode_totablep(intptr_t table) {
@@ -106,8 +90,8 @@ UPB_INLINE const char* _upb_Decoder_BufferFlipCallback(
if (!old_end) _upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_Malformed);
if (d->unknown) {
if (!_upb_Message_AddUnknown(d->unknown_msg, d->unknown,
old_end - d->unknown, &d->arena)) {
if (!UPB_PRIVATE(_upb_Message_AddUnknown)(
d->unknown_msg, d->unknown, old_end - d->unknown, &d->arena)) {
_upb_FastDecoder_ErrorJmp(d, kUpb_DecodeStatus_OutOfMemory);
}
d->unknown = new_start;
@@ -126,9 +110,9 @@ const char* _upb_FastDecoder_TagDispatch(upb_Decoder* d, const char* ptr,
size_t idx = tag & mask;
UPB_ASSUME((idx & 7) == 0);
idx >>= 3;
data = table_p->fasttable[idx].field_data ^ tag;
UPB_MUSTTAIL return table_p->fasttable[idx].field_parser(d, ptr, msg, table,
hasbits, data);
data = table_p->UPB_PRIVATE(fasttable)[idx].field_data ^ tag;
UPB_MUSTTAIL return table_p->UPB_PRIVATE(fasttable)[idx].field_parser(
d, ptr, msg, table, hasbits, data);
}
#endif
@@ -140,4 +124,4 @@ UPB_INLINE uint32_t _upb_FastDecoder_LoadTag(const char* ptr) {
#include "upb/port/undef.inc"
#endif /* UPB_WIRE_INTERNAL_DECODE_H_ */
#endif /* UPB_WIRE_INTERNAL_DECODER_H_ */

View File

@@ -0,0 +1,61 @@
// Protocol Buffers - Google's data interchange format
// Copyright 2023 Google LLC. All rights reserved.
//
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file or at
// https://developers.google.com/open-source/licenses/bsd
#ifndef UPB_WIRE_INTERNAL_READER_H_
#define UPB_WIRE_INTERNAL_READER_H_
// Must be last.
#include "upb/port/def.inc"
#define kUpb_WireReader_WireTypeBits 3
#define kUpb_WireReader_WireTypeMask 7
typedef struct {
const char* ptr;
uint64_t val;
} UPB_PRIVATE(_upb_WireReader_LongVarint);
#ifdef __cplusplus
extern "C" {
#endif
UPB_PRIVATE(_upb_WireReader_LongVarint)
UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(const char* ptr, uint64_t val);
UPB_FORCEINLINE const char* UPB_PRIVATE(_upb_WireReader_ReadVarint)(
const char* ptr, uint64_t* val, int maxlen, uint64_t maxval) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = (uint32_t)byte;
return ptr + 1;
}
const char* start = ptr;
UPB_PRIVATE(_upb_WireReader_LongVarint)
res = UPB_PRIVATE(_upb_WireReader_ReadLongVarint)(ptr, byte);
if (!res.ptr || (maxlen < 10 && res.ptr - start > maxlen) ||
res.val > maxval) {
return NULL; // Malformed.
}
*val = res.val;
return res.ptr;
}
UPB_API_INLINE uint32_t upb_WireReader_GetFieldNumber(uint32_t tag) {
return tag >> kUpb_WireReader_WireTypeBits;
}
UPB_API_INLINE uint8_t upb_WireReader_GetWireType(uint32_t tag) {
return tag & kUpb_WireReader_WireTypeMask;
}
#ifdef __cplusplus
} /* extern "C" */
#endif
#include "upb/port/undef.inc"
#endif // UPB_WIRE_INTERNAL_READER_H_

View File

@@ -8,53 +8,23 @@
#ifndef UPB_WIRE_READER_H_
#define UPB_WIRE_READER_H_
#include "upb/base/internal/endian.h"
#include "upb/wire/eps_copy_input_stream.h"
#include "upb/wire/internal/swap.h"
#include "upb/wire/internal/reader.h"
#include "upb/wire/types.h" // IWYU pragma: export
// Must be last.
#include "upb/port/def.inc"
#ifdef __cplusplus
extern "C" {
#endif
// The upb_WireReader interface is suitable for general-purpose parsing of
// protobuf binary wire format. It is designed to be used along with
// protobuf binary wire format. It is designed to be used along with
// upb_EpsCopyInputStream for buffering, and all parsing routines in this file
// assume that at least kUpb_EpsCopyInputStream_SlopBytes worth of data is
// available to read without any bounds checks.
#define kUpb_WireReader_WireTypeMask 7
#define kUpb_WireReader_WireTypeBits 3
typedef struct {
const char* ptr;
uint64_t val;
} _upb_WireReader_ReadLongVarintRet;
_upb_WireReader_ReadLongVarintRet _upb_WireReader_ReadLongVarint(
const char* ptr, uint64_t val);
static UPB_FORCEINLINE const char* _upb_WireReader_ReadVarint(const char* ptr,
uint64_t* val,
int maxlen,
uint64_t maxval) {
uint64_t byte = (uint8_t)*ptr;
if (UPB_LIKELY((byte & 0x80) == 0)) {
*val = (uint32_t)byte;
return ptr + 1;
}
const char* start = ptr;
_upb_WireReader_ReadLongVarintRet res =
_upb_WireReader_ReadLongVarint(ptr, byte);
if (!res.ptr || (maxlen < 10 && res.ptr - start > maxlen) ||
res.val > maxval) {
return NULL; // Malformed.
}
*val = res.val;
return res.ptr;
}
#ifdef __cplusplus
extern "C" {
#endif
// Parses a tag into `tag`, and returns a pointer past the end of the tag, or
// NULL if there was an error in the tag data.
@@ -62,28 +32,24 @@ static UPB_FORCEINLINE const char* _upb_WireReader_ReadVarint(const char* ptr,
// REQUIRES: there must be at least 10 bytes of data available at `ptr`.
// Bounds checks must be performed before calling this function, preferably
// by calling upb_EpsCopyInputStream_IsDone().
static UPB_FORCEINLINE const char* upb_WireReader_ReadTag(const char* ptr,
uint32_t* tag) {
UPB_FORCEINLINE const char* upb_WireReader_ReadTag(const char* ptr,
uint32_t* tag) {
uint64_t val;
ptr = _upb_WireReader_ReadVarint(ptr, &val, 5, UINT32_MAX);
ptr = UPB_PRIVATE(_upb_WireReader_ReadVarint)(ptr, &val, 5, UINT32_MAX);
if (!ptr) return NULL;
*tag = val;
return ptr;
}
// Given a tag, returns the field number.
UPB_INLINE uint32_t upb_WireReader_GetFieldNumber(uint32_t tag) {
return tag >> kUpb_WireReader_WireTypeBits;
}
UPB_API_INLINE uint32_t upb_WireReader_GetFieldNumber(uint32_t tag);
// Given a tag, returns the wire type.
UPB_INLINE uint8_t upb_WireReader_GetWireType(uint32_t tag) {
return tag & kUpb_WireReader_WireTypeMask;
}
UPB_API_INLINE uint8_t upb_WireReader_GetWireType(uint32_t tag);
UPB_INLINE const char* upb_WireReader_ReadVarint(const char* ptr,
uint64_t* val) {
return _upb_WireReader_ReadVarint(ptr, val, 10, UINT64_MAX);
return UPB_PRIVATE(_upb_WireReader_ReadVarint)(ptr, val, 10, UINT64_MAX);
}
// Skips data for a varint, returning a pointer past the end of the varint, or
@@ -119,7 +85,7 @@ UPB_INLINE const char* upb_WireReader_ReadSize(const char* ptr, int* size) {
UPB_INLINE const char* upb_WireReader_ReadFixed32(const char* ptr, void* val) {
uint32_t uval;
memcpy(&uval, ptr, 4);
uval = _upb_BigEndian_Swap32(uval);
uval = upb_BigEndian32(uval);
memcpy(val, &uval, 4);
return ptr + 4;
}
@@ -132,14 +98,14 @@ UPB_INLINE const char* upb_WireReader_ReadFixed32(const char* ptr, void* val) {
UPB_INLINE const char* upb_WireReader_ReadFixed64(const char* ptr, void* val) {
uint64_t uval;
memcpy(&uval, ptr, 8);
uval = _upb_BigEndian_Swap64(uval);
uval = upb_BigEndian64(uval);
memcpy(val, &uval, 8);
return ptr + 8;
}
const char* _upb_WireReader_SkipGroup(const char* ptr, uint32_t tag,
int depth_limit,
upb_EpsCopyInputStream* stream);
const char* UPB_PRIVATE(_upb_WireReader_SkipGroup)(
const char* ptr, uint32_t tag, int depth_limit,
upb_EpsCopyInputStream* stream);
// Skips data for a group, returning a pointer past the end of the group, or
// NULL if there was an error parsing the group. The `tag` argument should be
@@ -152,7 +118,7 @@ const char* _upb_WireReader_SkipGroup(const char* ptr, uint32_t tag,
// control over this?
UPB_INLINE const char* upb_WireReader_SkipGroup(
const char* ptr, uint32_t tag, upb_EpsCopyInputStream* stream) {
return _upb_WireReader_SkipGroup(ptr, tag, 100, stream);
return UPB_PRIVATE(_upb_WireReader_SkipGroup)(ptr, tag, 100, stream);
}
UPB_INLINE const char* _upb_WireReader_SkipValue(
@@ -173,7 +139,8 @@ UPB_INLINE const char* _upb_WireReader_SkipValue(
return ptr;
}
case kUpb_WireType_StartGroup:
return _upb_WireReader_SkipGroup(ptr, tag, depth_limit, stream);
return UPB_PRIVATE(_upb_WireReader_SkipGroup)(ptr, tag, depth_limit,
stream);
case kUpb_WireType_EndGroup:
return NULL; // Should be handled before now.
default: