xref: /aosp_15_r20/external/stg/graph.cc (revision 9e3b08ae94a55201065475453d799e8b1378bea6)
1 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2 // -*- mode: C++ -*-
3 //
4 // Copyright 2020-2022 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: Maria Teguiani
19 // Author: Giuliano Procida
20 // Author: Ignes Simeonova
21 
22 #include "graph.h"
23 
24 #include <limits>
25 #include <ostream>
26 #include <string>
27 #include <string_view>
28 
29 #include "hex.h"
30 
31 namespace stg {
32 
33 const Id Id::kInvalid(std::numeric_limits<decltype(Id::ix_)>::max());
34 
operator <<(std::ostream & os,Id id)35 std::ostream& operator<<(std::ostream& os, Id id) {
36   return os << '<' << id.ix_ << '>';
37 }
38 
operator <<(std::ostream & os,BaseClass::Inheritance inheritance)39 std::ostream& operator<<(std::ostream& os, BaseClass::Inheritance inheritance) {
40   switch (inheritance) {
41     case BaseClass::Inheritance::NON_VIRTUAL:
42       return os << "non-virtual";
43     case BaseClass::Inheritance::VIRTUAL:
44       return os << "virtual";
45   }
46 }
47 
48 namespace {
49 
ToString(StructUnion::Kind kind)50 std::string_view ToString(StructUnion::Kind kind) {
51   switch (kind) {
52     case StructUnion::Kind::STRUCT:
53       return "struct";
54     case StructUnion::Kind::UNION:
55       return "union";
56   }
57 }
58 
59 }  // namespace
60 
operator <<(std::ostream & os,StructUnion::Kind kind)61 std::ostream& operator<<(std::ostream& os, StructUnion::Kind kind) {
62   return os << ToString(kind);
63 }
64 
operator +=(std::string & os,StructUnion::Kind kind)65 std::string& operator+=(std::string& os, StructUnion::Kind kind) {
66   return os += ToString(kind);
67 }
68 
operator <<(std::ostream & os,Qualifier qualifier)69 std::ostream& operator<<(std::ostream& os, Qualifier qualifier) {
70   switch (qualifier) {
71     case Qualifier::CONST:
72       return os << "const";
73     case Qualifier::VOLATILE:
74       return os << "volatile";
75     case Qualifier::RESTRICT:
76       return os << "restrict";
77     case Qualifier::ATOMIC:
78       return os << "atomic";
79   }
80 }
81 
operator <<(std::ostream & os,ElfSymbol::SymbolType type)82 std::ostream& operator<<(std::ostream& os, ElfSymbol::SymbolType type) {
83   switch (type) {
84     case ElfSymbol::SymbolType::NOTYPE:
85       return os << "no-type";
86     case ElfSymbol::SymbolType::OBJECT:
87       return os << "variable";
88     case ElfSymbol::SymbolType::FUNCTION:
89       return os << "function";
90     case ElfSymbol::SymbolType::COMMON:
91       return os << "common";
92     case ElfSymbol::SymbolType::TLS:
93       return os << "TLS";
94     case ElfSymbol::SymbolType::GNU_IFUNC:
95       return os << "indirect (ifunc) function";
96   }
97 }
98 
operator <<(std::ostream & os,ElfSymbol::Binding binding)99 std::ostream& operator<<(std::ostream& os, ElfSymbol::Binding binding) {
100   switch (binding) {
101     case ElfSymbol::Binding::GLOBAL:
102       return os << "global";
103     case ElfSymbol::Binding::LOCAL:
104       return os << "local";
105     case ElfSymbol::Binding::WEAK:
106       return os << "weak";
107     case ElfSymbol::Binding::GNU_UNIQUE:
108       return os << "GNU unique";
109   }
110 }
111 
operator <<(std::ostream & os,ElfSymbol::Visibility visibility)112 std::ostream& operator<<(std::ostream& os, ElfSymbol::Visibility visibility) {
113   switch (visibility) {
114     case ElfSymbol::Visibility::DEFAULT:
115       return os << "default";
116     case ElfSymbol::Visibility::PROTECTED:
117       return os << "protected";
118     case ElfSymbol::Visibility::HIDDEN:
119       return os << "hidden";
120     case ElfSymbol::Visibility::INTERNAL:
121       return os << "internal";
122   }
123 }
124 
VersionInfoToString(const ElfSymbol::VersionInfo & version_info)125 std::string VersionInfoToString(const ElfSymbol::VersionInfo& version_info) {
126   return '@' + std::string(version_info.is_default ? "@" : "") +
127          version_info.name;
128 }
129 
VersionedSymbolName(const ElfSymbol & symbol)130 std::string VersionedSymbolName(const ElfSymbol& symbol) {
131   return symbol.symbol_name + (symbol.version_info
132                                    ? VersionInfoToString(*symbol.version_info)
133                                    : std::string());
134 }
135 
operator <<(std::ostream & os,ElfSymbol::CRC crc)136 std::ostream& operator<<(std::ostream& os, ElfSymbol::CRC crc) {
137   return os << Hex(crc.number);
138 }
139 
operator <<(std::ostream & os,Primitive::Encoding encoding)140 std::ostream& operator<<(std::ostream& os, Primitive::Encoding encoding) {
141   switch (encoding) {
142     case Primitive::Encoding::BOOLEAN:
143       return os << "boolean";
144     case Primitive::Encoding::SIGNED_INTEGER:
145       return os << "signed integer";
146     case Primitive::Encoding::UNSIGNED_INTEGER:
147       return os << "unsigned integer";
148     case Primitive::Encoding::SIGNED_CHARACTER:
149       return os << "signed character";
150     case Primitive::Encoding::UNSIGNED_CHARACTER:
151       return os << "unsigned character";
152     case Primitive::Encoding::REAL_NUMBER:
153       return os << "real number";
154     case Primitive::Encoding::COMPLEX_NUMBER:
155       return os << "complex number";
156     case Primitive::Encoding::UTF:
157       return os << "UTF";
158   }
159 }
160 
161 }  // namespace stg
162