1 // Copyright (c) 2009-2021, Google LLC
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //     * Redistributions of source code must retain the above copyright
7 //       notice, this list of conditions and the following disclaimer.
8 //     * Redistributions in binary form must reproduce the above copyright
9 //       notice, this list of conditions and the following disclaimer in the
10 //       documentation and/or other materials provided with the distribution.
11 //     * Neither the name of Google LLC nor the
12 //       names of its contributors may be used to endorse or promote products
13 //       derived from this software without specific prior written permission.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18 // ARE DISCLAIMED. IN NO EVENT SHALL Google LLC BE LIABLE FOR ANY DIRECT,
19 // INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
21 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
23 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24 
25 #ifndef UPBC_FILE_LAYOUT_H
26 #define UPBC_FILE_LAYOUT_H
27 
28 #include <string>
29 
30 // begin:google_only
31 // #ifndef UPB_BOOTSTRAP_STAGE0
32 // #include "net/proto2/proto/descriptor.upb.h"
33 // #else
34 // #include "google/protobuf/descriptor.upb.h"
35 // #endif
36 // end:google_only
37 
38 // begin:github_only
39 #include "google/protobuf/descriptor.upb.h"
40 // end:github_only
41 
42 #include "absl/container/flat_hash_map.h"
43 #include "upb/mini_table/decode.h"
44 #include "upb/reflection/def.h"
45 #include "upb/reflection/def.hpp"
46 #include "upb/upb.hpp"
47 
48 // Must be last
49 #include "upb/port/def.inc"
50 
51 namespace upbc {
52 
53 std::vector<upb::EnumDefPtr> SortedEnums(upb::FileDefPtr file);
54 
55 // Ordering must match upb/def.c!
56 //
57 // The ordering is significant because each upb_MessageDef* will point at the
58 // corresponding upb_MiniTable and we just iterate through the list without
59 // any search or lookup.
60 std::vector<upb::MessageDefPtr> SortedMessages(upb::FileDefPtr file);
61 
62 // Ordering must match upb/def.c!
63 //
64 // The ordering is significant because each upb_FieldDef* will point at the
65 // corresponding upb_MiniTableExtension and we just iterate through the list
66 // without any search or lookup.
67 std::vector<upb::FieldDefPtr> SortedExtensions(upb::FileDefPtr file);
68 
69 std::vector<upb::FieldDefPtr> FieldNumberOrder(upb::MessageDefPtr message);
70 
71 // DefPoolPair is a pair of DefPools: one for 32-bit and one for 64-bit.
72 class DefPoolPair {
73  public:
DefPoolPair()74   DefPoolPair() {
75     pool32_._SetPlatform(kUpb_MiniTablePlatform_32Bit);
76     pool64_._SetPlatform(kUpb_MiniTablePlatform_64Bit);
77   }
78 
AddFile(const UPB_DESC (FileDescriptorProto)* file_proto,upb::Status * status)79   upb::FileDefPtr AddFile(const UPB_DESC(FileDescriptorProto) * file_proto,
80                           upb::Status* status) {
81     upb::FileDefPtr file32 = pool32_.AddFile(file_proto, status);
82     upb::FileDefPtr file64 = pool64_.AddFile(file_proto, status);
83     if (!file32) return file32;
84     return file64;
85   }
86 
GetMiniTable32(upb::MessageDefPtr m)87   const upb_MiniTable* GetMiniTable32(upb::MessageDefPtr m) const {
88     return pool32_.FindMessageByName(m.full_name()).mini_table();
89   }
90 
GetMiniTable64(upb::MessageDefPtr m)91   const upb_MiniTable* GetMiniTable64(upb::MessageDefPtr m) const {
92     return pool64_.FindMessageByName(m.full_name()).mini_table();
93   }
94 
GetField32(upb::FieldDefPtr f)95   const upb_MiniTableField* GetField32(upb::FieldDefPtr f) const {
96     return GetFieldFromPool(&pool32_, f);
97   }
98 
GetField64(upb::FieldDefPtr f)99   const upb_MiniTableField* GetField64(upb::FieldDefPtr f) const {
100     return GetFieldFromPool(&pool64_, f);
101   }
102 
103  private:
GetFieldFromPool(const upb::DefPool * pool,upb::FieldDefPtr f)104   static const upb_MiniTableField* GetFieldFromPool(const upb::DefPool* pool,
105                                                     upb::FieldDefPtr f) {
106     if (f.is_extension()) {
107       return pool->FindExtensionByName(f.full_name()).mini_table();
108     } else {
109       return pool->FindMessageByName(f.containing_type().full_name())
110           .FindFieldByNumber(f.number())
111           .mini_table();
112     }
113   }
114 
115   upb::DefPool pool32_;
116   upb::DefPool pool64_;
117 };
118 
119 }  // namespace upbc
120 
121 #include "upb/port/undef.inc"
122 
123 #endif  // UPBC_FILE_LAYOUT_H
124