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