1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
4 // Use of this source code is governed by a BSD-style
5 // license that can be found in the LICENSE file or at
6 // https://developers.google.com/open-source/licenses/bsd
8 #ifndef UPB_GENERATOR_FILE_LAYOUT_H
9 #define UPB_GENERATOR_FILE_LAYOUT_H
14 #include "absl/container/flat_hash_map.h"
15 #include "upb/base/status.hpp"
16 #include "upb/mini_descriptor/decode.h"
17 #include "upb/reflection/def.h"
18 #include "upb/reflection/def.hpp"
19 #include "upb/reflection/descriptor_bootstrap.h"
22 #include "upb/port/def.inc"
32 std::vector
<upb::EnumDefPtr
> SortedEnums(upb::FileDefPtr file
,
35 // Ordering must match upb/def.c!
37 // The ordering is significant because each upb_MessageDef* will point at the
38 // corresponding upb_MiniTable and we just iterate through the list without
39 // any search or lookup.
40 std::vector
<upb::MessageDefPtr
> SortedMessages(upb::FileDefPtr file
);
42 // Ordering must match upb/def.c!
44 // The ordering is significant because each upb_FieldDef* will point at the
45 // corresponding upb_MiniTableExtension and we just iterate through the list
46 // without any search or lookup.
47 std::vector
<upb::FieldDefPtr
> SortedExtensions(upb::FileDefPtr file
);
49 std::vector
<upb::FieldDefPtr
> FieldNumberOrder(upb::MessageDefPtr message
);
51 // DefPoolPair is a pair of DefPools: one for 32-bit and one for 64-bit.
55 pool32_
._SetPlatform(kUpb_MiniTablePlatform_32Bit
);
56 pool64_
._SetPlatform(kUpb_MiniTablePlatform_64Bit
);
59 upb::FileDefPtr
AddFile(const UPB_DESC(FileDescriptorProto
) * file_proto
,
60 upb::Status
* status
) {
61 upb::FileDefPtr file32
= pool32_
.AddFile(file_proto
, status
);
62 upb::FileDefPtr file64
= pool64_
.AddFile(file_proto
, status
);
63 if (!file32
) return file32
;
67 const upb_MiniTable
* GetMiniTable32(upb::MessageDefPtr m
) const {
68 return pool32_
.FindMessageByName(m
.full_name()).mini_table();
71 const upb_MiniTable
* GetMiniTable64(upb::MessageDefPtr m
) const {
72 return pool64_
.FindMessageByName(m
.full_name()).mini_table();
75 const upb_MiniTableField
* GetField32(upb::FieldDefPtr f
) const {
76 return GetFieldFromPool(&pool32_
, f
);
79 const upb_MiniTableField
* GetField64(upb::FieldDefPtr f
) const {
80 return GetFieldFromPool(&pool64_
, f
);
84 static const upb_MiniTableField
* GetFieldFromPool(const upb::DefPool
* pool
,
86 if (f
.is_extension()) {
87 return pool
->FindExtensionByName(f
.full_name()).mini_table();
89 return pool
->FindMessageByName(f
.containing_type().full_name())
90 .FindFieldByNumber(f
.number())
99 } // namespace generator
102 #include "upb/port/undef.inc"
104 #endif // UPB_GENERATOR_FILE_LAYOUT_H