xref: /aosp_15_r20/external/stg/fidelity.h (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2023 Google LLC
5 //
6 // Licensed under the Apache License v2.0 with LLVM Exceptions (the
7 // "License"); you may not use this file except in compliance with the
8 // License.  You may obtain a copy of the License at
9 //
10 //     https://llvm.org/LICENSE.txt
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 //
18 // Author: Siddharth Nayyar
19 
20 #ifndef STG_FIDELITY_H_
21 #define STG_FIDELITY_H_
22 
23 #include <cstddef>
24 #include <functional>
25 #include <ostream>
26 #include <string>
27 #include <unordered_map>
28 #include <utility>
29 #include <vector>
30 
31 #include "graph.h"
32 
33 namespace stg {
34 
35 enum class SymbolFidelity {
36   ABSENT = 0,
37   UNTYPED = 1,
38   TYPED = 2,
39 };
40 
41 enum class TypeFidelity {
42   ABSENT = 0,
43   DECLARATION_ONLY = 1,
44   FULLY_DEFINED = 2,
45 };
46 
47 using SymbolFidelityTransition = std::pair<SymbolFidelity, SymbolFidelity>;
48 using TypeFidelityTransition = std::pair<TypeFidelity, TypeFidelity>;
49 
50 std::ostream& operator<<(std::ostream& os, SymbolFidelity x);
51 std::ostream& operator<<(std::ostream& os, TypeFidelity x);
52 std::ostream& operator<<(std::ostream& os, SymbolFidelityTransition x);
53 std::ostream& operator<<(std::ostream& os, TypeFidelityTransition x);
54 
55 }  // namespace stg
56 
57 namespace std {
58 
59 template <>
60 struct hash<stg::SymbolFidelityTransition> {
61   size_t operator()(const stg::SymbolFidelityTransition& x) const {
62     return static_cast<size_t>(x.first) << 2 | static_cast<size_t>(x.second);
63   }
64 };
65 
66 template <>
67 struct hash<stg::TypeFidelityTransition> {
68   size_t operator()(const stg::TypeFidelityTransition& x) const {
69     return static_cast<size_t>(x.first) << 2 | static_cast<size_t>(x.second);
70   }
71 };
72 
73 }  // namespace std
74 
75 namespace stg {
76 
77 struct FidelityDiff {
78   std::unordered_map<SymbolFidelityTransition, std::vector<std::string>>
79       symbol_transitions;
80   std::unordered_map<TypeFidelityTransition, std::vector<std::string>>
81       type_transitions;
82 };
83 
84 FidelityDiff GetFidelityTransitions(const Graph& graph, Id root1, Id root2);
85 
86 }  // namespace stg
87 
88 #endif  // STG_FIDELITY_H_
89