Don't add LayoutItems for non-oneof fields. This simplifies the code, and since the...
[google-protobuf.git] / upb_generator / file_layout.h
blob4bf832e12467de75e9fb6730aeaa5e2ca839a87a
1 // Protocol Buffers - Google's data interchange format
2 // Copyright 2023 Google LLC. All rights reserved.
3 //
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
11 #include <string>
12 #include <vector>
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"
21 // Must be last
22 #include "upb/port/def.inc"
24 namespace upb {
25 namespace generator {
27 enum WhichEnums {
28 kAllEnums = 0,
29 kClosedEnums = 1,
32 std::vector<upb::EnumDefPtr> SortedEnums(upb::FileDefPtr file,
33 WhichEnums which);
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.
52 class DefPoolPair {
53 public:
54 DefPoolPair() {
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;
64 return file64;
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);
83 private:
84 static const upb_MiniTableField* GetFieldFromPool(const upb::DefPool* pool,
85 upb::FieldDefPtr f) {
86 if (f.is_extension()) {
87 return pool->FindExtensionByName(f.full_name()).mini_table();
88 } else {
89 return pool->FindMessageByName(f.containing_type().full_name())
90 .FindFieldByNumber(f.number())
91 .mini_table();
95 upb::DefPool pool32_;
96 upb::DefPool pool64_;
99 } // namespace generator
100 } // namespace upb
102 #include "upb/port/undef.inc"
104 #endif // UPB_GENERATOR_FILE_LAYOUT_H