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_REPR_IR_REPRESENTATION_INTERNAL_H_
16 #define HEADER_CHECKER_REPR_IR_REPRESENTATION_INTERNAL_H_
17 
18 #include "repr/ir_representation.h"
19 
20 #include <string>
21 
22 
23 namespace header_checker {
24 namespace repr {
25 
26 
27 template <typename T>
GetReferencedTypeMapKey(T & element)28 inline std::string GetReferencedTypeMapKey(T &element) {
29   return element.GetReferencedType();
30 }
31 
32 template <>
33 inline std::string GetReferencedTypeMapKey<ArrayTypeIR>(ArrayTypeIR &element) {
34   return element.GetReferencedType() + ":" + std::to_string(element.GetSize());
35 }
36 
37 template <>
38 inline std::string GetReferencedTypeMapKey<BuiltinTypeIR>(
39     BuiltinTypeIR &element) {
40   return element.GetLinkerSetKey();
41 }
42 
BoolToString(bool val)43 inline static std::string BoolToString(bool val) {
44   return val ? "true" : "false";
45 }
46 
47 template <>
48 inline std::string GetReferencedTypeMapKey<QualifiedTypeIR>(
49     QualifiedTypeIR &element) {
50   return element.GetReferencedType() + BoolToString(element.IsRestricted()) +
51       BoolToString(element.IsVolatile()) + BoolToString(element.IsConst());
52 }
53 
GetODRListMapKey(const RecordTypeIR * record_type_ir)54 inline std::string GetODRListMapKey(const RecordTypeIR *record_type_ir) {
55   if (record_type_ir->IsAnonymous()) {
56     return record_type_ir->GetLinkerSetKey();
57   }
58   return record_type_ir->GetLinkerSetKey() + record_type_ir->GetSourceFile();
59 }
60 
GetODRListMapKey(const EnumTypeIR * enum_type_ir)61 inline std::string GetODRListMapKey(const EnumTypeIR *enum_type_ir) {
62   return enum_type_ir->GetLinkerSetKey() + enum_type_ir->GetSourceFile();
63 }
64 
GetODRListMapKey(const FunctionTypeIR * function_type_ir)65 inline std::string GetODRListMapKey(const FunctionTypeIR *function_type_ir) {
66   return function_type_ir->GetLinkerSetKey();
67 }
68 
69 // The map that is being updated maps special_key -> Type / Function/ GlobVar
70 // This special key is needed to distinguish what is being referenced.
71 template <typename T>
AddToMapAndTypeGraph(T && element,AbiElementMap<T> * map_to_update,AbiElementMap<const TypeIR * > * type_graph)72 typename AbiElementMap<T>::iterator AddToMapAndTypeGraph(
73     T &&element, AbiElementMap<T> *map_to_update,
74     AbiElementMap<const TypeIR *> *type_graph) {
75   auto it = map_to_update->emplace(GetReferencedTypeMapKey(element),
76                                    std::move(element));
77   type_graph->emplace(it.first->second.GetSelfType(), &(it.first->second));
78   return it.first;
79 }
80 
81 
82 }  // namespace repr
83 }  // namespace header_checker
84 
85 
86 #endif  // HEADER_CHECKER_REPR_IR_REPRESENTATION_INTERNAL_H_
87