xref: /aosp_15_r20/development/vndk/tools/header-checker/src/repr/protobuf/converter.h (revision 90c8c64db3049935a07c6143d7fd006e26f8ecca)
1 // Copyright (C) 2019 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //      http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEADER_CHECKER_PROTOBUF_CONVERTER_H_
16 #define HEADER_CHECKER_PROTOBUF_CONVERTER_H_
17 
18 #include "repr/ir_diff_representation.h"
19 #include "repr/ir_representation.h"
20 #include "repr/protobuf/abi_diff.h"
21 #include "repr/protobuf/abi_dump.h"
22 
23 
24 namespace header_checker {
25 namespace repr {
26 
27 
CompatibilityStatusIRToProtobuf(CompatibilityStatusIR status)28 inline abi_diff::CompatibilityStatus CompatibilityStatusIRToProtobuf(
29     CompatibilityStatusIR status) {
30   if (status & (CompatibilityStatusIR::Incompatible |
31                 CompatibilityStatusIR::ElfIncompatible |
32                 CompatibilityStatusIR::UnreferencedChanges)) {
33     return abi_diff::CompatibilityStatus::INCOMPATIBLE;
34   }
35   if (status & (CompatibilityStatusIR::Extension |
36                 CompatibilityStatusIR::ElfExtension)) {
37     return abi_diff::CompatibilityStatus::EXTENSION;
38   }
39   return abi_diff::CompatibilityStatus::COMPATIBLE;
40 }
41 
ElfSymbolBindingIRToProtobuf(ElfSymbolIR::ElfSymbolBinding binding)42 inline abi_dump::ElfSymbolBinding ElfSymbolBindingIRToProtobuf(
43     ElfSymbolIR::ElfSymbolBinding binding) {
44   switch (binding) {
45     case ElfSymbolIR::ElfSymbolBinding::Global:
46       return abi_dump::ElfSymbolBinding::Global;
47     case ElfSymbolIR::ElfSymbolBinding::Weak:
48       return abi_dump::ElfSymbolBinding::Weak;
49   }
50   // We skip symbols of all other Bindings
51   // TODO: Add all bindings, don't leave out info
52   assert(0);
53 }
54 
ElfSymbolBindingProtobufToIR(abi_dump::ElfSymbolBinding binding)55 inline ElfSymbolIR::ElfSymbolBinding ElfSymbolBindingProtobufToIR(
56     abi_dump::ElfSymbolBinding binding) {
57   switch (binding) {
58     case abi_dump::ElfSymbolBinding::Global:
59       return ElfSymbolIR::ElfSymbolBinding::Global;
60     case abi_dump::ElfSymbolBinding::Weak:
61       return ElfSymbolIR::ElfSymbolBinding::Weak;
62   }
63   // We skip symbols of all other Bindings
64   assert(0);
65 }
66 
AccessIRToProtobuf(AccessSpecifierIR access)67 inline abi_dump::AccessSpecifier AccessIRToProtobuf(AccessSpecifierIR access) {
68   switch (access) {
69     case AccessSpecifierIR::ProtectedAccess:
70       return abi_dump::AccessSpecifier::protected_access;
71     case AccessSpecifierIR::PrivateAccess:
72       return abi_dump::AccessSpecifier::private_access;
73     default:
74       return abi_dump::AccessSpecifier::public_access;
75   }
76   return abi_dump::AccessSpecifier::public_access;
77 }
78 
AccessProtobufToIR(abi_dump::AccessSpecifier access)79 inline AccessSpecifierIR AccessProtobufToIR(
80     abi_dump::AccessSpecifier access) {
81   switch (access) {
82     case abi_dump::AccessSpecifier::protected_access:
83       return AccessSpecifierIR::ProtectedAccess;
84     case abi_dump::AccessSpecifier::private_access:
85       return AccessSpecifierIR::PrivateAccess;
86     default:
87       return AccessSpecifierIR::PublicAccess;
88   }
89   return AccessSpecifierIR::PublicAccess;
90 }
91 
RecordKindIRToProtobuf(RecordTypeIR::RecordKind kind)92 inline abi_dump::RecordKind RecordKindIRToProtobuf(
93     RecordTypeIR::RecordKind kind) {
94   switch (kind) {
95     case RecordTypeIR::RecordKind::struct_kind:
96       return abi_dump::RecordKind::struct_kind;
97 
98     case RecordTypeIR::RecordKind::class_kind:
99       return abi_dump::RecordKind::class_kind;
100 
101     case RecordTypeIR::RecordKind::union_kind:
102       return abi_dump::RecordKind::union_kind;
103 
104     default:
105       return abi_dump::RecordKind::struct_kind;
106   }
107   // Should not be reached
108   assert(false);
109 }
110 
RecordKindProtobufToIR(abi_dump::RecordKind kind)111 inline RecordTypeIR::RecordKind RecordKindProtobufToIR(
112     abi_dump::RecordKind kind) {
113   switch (kind) {
114     case abi_dump::RecordKind::struct_kind:
115       return RecordTypeIR::struct_kind;
116 
117     case abi_dump::RecordKind::class_kind:
118       return RecordTypeIR::class_kind;
119 
120     case abi_dump::RecordKind::union_kind:
121       return RecordTypeIR::union_kind;
122 
123     default:
124       return RecordTypeIR::struct_kind;
125   }
126   // Should not be reached
127   assert(false);
128 }
129 
VTableComponentKindIRToProtobuf(VTableComponentIR::Kind kind)130 inline abi_dump::VTableComponent::Kind VTableComponentKindIRToProtobuf(
131     VTableComponentIR::Kind kind) {
132   switch (kind) {
133     case VTableComponentIR::Kind::VCallOffset:
134       return abi_dump::VTableComponent_Kind_VCallOffset;
135 
136     case VTableComponentIR::Kind::VBaseOffset:
137       return abi_dump::VTableComponent_Kind_VBaseOffset;
138 
139     case VTableComponentIR::Kind::OffsetToTop:
140       return abi_dump::VTableComponent_Kind_OffsetToTop;
141 
142     case VTableComponentIR::Kind::RTTI:
143       return abi_dump::VTableComponent_Kind_RTTI;
144 
145     case VTableComponentIR::Kind::FunctionPointer:
146       return abi_dump::VTableComponent_Kind_FunctionPointer;
147 
148     case VTableComponentIR::Kind::CompleteDtorPointer:
149       return abi_dump::VTableComponent_Kind_CompleteDtorPointer;
150 
151     case VTableComponentIR::Kind::DeletingDtorPointer:
152       return abi_dump::VTableComponent_Kind_DeletingDtorPointer;
153 
154     default:
155       return abi_dump::VTableComponent_Kind_UnusedFunctionPointer;
156   }
157   // Should not be reached
158   assert(false);
159 }
160 
VTableComponentKindProtobufToIR(abi_dump::VTableComponent_Kind kind)161 inline VTableComponentIR::Kind VTableComponentKindProtobufToIR(
162     abi_dump::VTableComponent_Kind kind) {
163   switch (kind) {
164     case abi_dump::VTableComponent_Kind_VCallOffset:
165       return VTableComponentIR::Kind::VCallOffset;
166 
167     case abi_dump::VTableComponent_Kind_VBaseOffset:
168       return VTableComponentIR::Kind::VBaseOffset;
169 
170     case abi_dump::VTableComponent_Kind_OffsetToTop:
171       return VTableComponentIR::Kind::OffsetToTop;
172 
173     case abi_dump::VTableComponent_Kind_RTTI:
174       return VTableComponentIR::Kind::RTTI;
175 
176     case abi_dump::VTableComponent_Kind_FunctionPointer:
177       return VTableComponentIR::Kind::FunctionPointer;
178 
179     case abi_dump::VTableComponent_Kind_CompleteDtorPointer:
180       return VTableComponentIR::Kind::CompleteDtorPointer;
181 
182     case abi_dump::VTableComponent_Kind_DeletingDtorPointer:
183       return VTableComponentIR::Kind::DeletingDtorPointer;
184 
185     default:
186       return VTableComponentIR::Kind::UnusedFunctionPointer;
187   }
188   // Should not be reached
189   assert(false);
190 }
191 
192 // ProtobufIRDiffDumper calls the following functions.
193 bool ConvertVTableLayoutIR(abi_dump::VTableLayout *vtable_layout_protobuf,
194                            const VTableLayoutIR &vtable_layout_ir);
195 
196 bool ConvertCXXBaseSpecifierIR(
197     abi_dump::CXXBaseSpecifier *base_specifier_protobuf,
198     const CXXBaseSpecifierIR &base_specifier_ir);
199 
200 bool ConvertRecordFieldIR(abi_dump::RecordFieldDecl *record_field_protobuf,
201                           const RecordFieldIR *record_field_ir);
202 
203 bool ConvertEnumFieldIR(abi_dump::EnumFieldDecl *enum_field_protobuf,
204                         const EnumFieldIR *enum_field_ir);
205 
206 abi_dump::FunctionDecl ConvertFunctionIR(const FunctionIR *functionp);
207 
208 abi_dump::GlobalVarDecl ConvertGlobalVarIR(const GlobalVarIR *global_varp);
209 
210 abi_dump::ElfFunction ConvertElfFunctionIR(
211     const ElfFunctionIR *elf_function_ir);
212 
213 abi_dump::ElfObject ConvertElfObjectIR(const ElfObjectIR *elf_object_ir);
214 
215 abi_dump::RecordType ConvertRecordTypeIR(const RecordTypeIR *recordp);
216 
217 abi_dump::EnumType ConvertEnumTypeIR(const EnumTypeIR *enump);
218 
219 
220 }  // namespace repr
221 }  // namespace header_checker
222 
223 
224 #endif  // HEADER_CHECKER_PROTOBUF_CONVERTER_H_
225