• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
7 
8 #ifndef UPB_GENERATOR_FILE_LAYOUT_H
9 #define UPB_GENERATOR_FILE_LAYOUT_H
10 
11 #include <string>
12 #include <vector>
13 
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"
20 
21 // Must be last
22 #include "upb/port/def.inc"
23 
24 namespace upb {
25 namespace generator {
26 
27 enum WhichEnums {
28   kAllEnums = 0,
29   kClosedEnums = 1,
30 };
31 
32 std::vector<upb::EnumDefPtr> SortedEnums(upb::FileDefPtr file,
33                                          WhichEnums which);
34 
35 // Ordering must match upb/def.c!
36 //
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);
41 
42 // Ordering must match upb/def.c!
43 //
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);
48 
49 std::vector<upb::FieldDefPtr> FieldNumberOrder(upb::MessageDefPtr message);
50 
51 // DefPoolPair is a pair of DefPools: one for 32-bit and one for 64-bit.
52 class DefPoolPair {
53  public:
DefPoolPair()54   DefPoolPair() {
55     pool32_._SetPlatform(kUpb_MiniTablePlatform_32Bit);
56     pool64_._SetPlatform(kUpb_MiniTablePlatform_64Bit);
57   }
58 
AddFile(const UPB_DESC (FileDescriptorProto)* file_proto,upb::Status * status)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;
65   }
66 
GetMiniTable32(upb::MessageDefPtr m)67   const upb_MiniTable* GetMiniTable32(upb::MessageDefPtr m) const {
68     return pool32_.FindMessageByName(m.full_name()).mini_table();
69   }
70 
GetMiniTable64(upb::MessageDefPtr m)71   const upb_MiniTable* GetMiniTable64(upb::MessageDefPtr m) const {
72     return pool64_.FindMessageByName(m.full_name()).mini_table();
73   }
74 
GetField32(upb::FieldDefPtr f)75   const upb_MiniTableField* GetField32(upb::FieldDefPtr f) const {
76     return GetFieldFromPool(&pool32_, f);
77   }
78 
GetField64(upb::FieldDefPtr f)79   const upb_MiniTableField* GetField64(upb::FieldDefPtr f) const {
80     return GetFieldFromPool(&pool64_, f);
81   }
82 
83  private:
GetFieldFromPool(const upb::DefPool * pool,upb::FieldDefPtr f)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();
92     }
93   }
94 
95   upb::DefPool pool32_;
96   upb::DefPool pool64_;
97 };
98 
99 }  // namespace generator
100 }  // namespace upb
101 
102 #include "upb/port/undef.inc"
103 
104 #endif  // UPB_GENERATOR_FILE_LAYOUT_H
105