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