1+ // Copyright (c) 2025 Roberto Raggi <[email protected] >2+ //
3+ // Permission is hereby granted, free of charge, to any person obtaining a copy
4+ // of this software and associated documentation files (the "Software"), to deal
5+ // in the Software without restriction, including without limitation the rights
6+ // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+ // copies of the Software, and to permit persons to whom the Software is
8+ // furnished to do so, subject to the following conditions:
9+ //
10+ // The above copyright notice and this permission notice shall be included in
11+ // all copies or substantial portions of the Software.
12+ //
13+ // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+ // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+ // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+ // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+ // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+ // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+ // SOFTWARE.
20+
21+ #include < cxx/external_name_encoder.h>
22+ #include < cxx/names.h>
23+ #include < cxx/symbols.h>
24+ #include < cxx/types.h>
25+
26+ #include < format>
27+
28+ namespace cxx {
29+
30+ struct ExternalNameEncoder ::NameVisitor {
31+ ExternalNameEncoder& encoder;
32+
33+ void operator ()(const Identifier* name) {}
34+
35+ void operator ()(const OperatorId* name) {}
36+
37+ void operator ()(const DestructorId* name) {}
38+
39+ void operator ()(const LiteralOperatorId* name) {}
40+
41+ void operator ()(const ConversionFunctionId* name) {}
42+
43+ void operator ()(const TemplateId* name) {}
44+ };
45+
46+ struct ExternalNameEncoder ::TypeVisitor {
47+ ExternalNameEncoder& encoder;
48+
49+ void operator ()(const VoidType* type) { encoder.out (" v" ); }
50+
51+ void operator ()(const NullptrType* type) { encoder.out (" Dn" ); }
52+
53+ void operator ()(const DecltypeAutoType* type) { encoder.out (" Dc" ); }
54+
55+ void operator ()(const AutoType* type) { encoder.out (" Da" ); }
56+
57+ void operator ()(const BoolType* type) { encoder.out (" b" ); }
58+
59+ void operator ()(const SignedCharType* type) { encoder.out (" a" ); }
60+
61+ void operator ()(const ShortIntType* type) { encoder.out (" s" ); }
62+
63+ void operator ()(const IntType* type) { encoder.out (" i" ); }
64+
65+ void operator ()(const LongIntType* type) { encoder.out (" l" ); }
66+
67+ void operator ()(const LongLongIntType* type) { encoder.out (" x" ); }
68+
69+ void operator ()(const UnsignedCharType* type) { encoder.out (" h" ); }
70+
71+ void operator ()(const UnsignedShortIntType* type) { encoder.out (" t" ); }
72+
73+ void operator ()(const UnsignedIntType* type) { encoder.out (" j" ); }
74+
75+ void operator ()(const UnsignedLongIntType* type) { encoder.out (" m" ); }
76+
77+ void operator ()(const UnsignedLongLongIntType* type) { encoder.out (" y" ); }
78+
79+ void operator ()(const CharType* type) { encoder.out (" c" ); }
80+
81+ void operator ()(const Char8Type* type) { encoder.out (" Du" ); }
82+
83+ void operator ()(const Char16Type* type) { encoder.out (" Ds" ); }
84+
85+ void operator ()(const Char32Type* type) { encoder.out (" Di" ); }
86+
87+ void operator ()(const WideCharType* type) { encoder.out (" w" ); }
88+
89+ void operator ()(const FloatType* type) { encoder.out (" f" ); }
90+
91+ void operator ()(const DoubleType* type) { encoder.out (" d" ); }
92+
93+ void operator ()(const LongDoubleType* type) { encoder.out (" e" ); }
94+
95+ void operator ()(const QualType* type) {}
96+
97+ void operator ()(const BoundedArrayType* type) {}
98+
99+ void operator ()(const UnboundedArrayType* type) {}
100+
101+ void operator ()(const PointerType* type) {}
102+
103+ void operator ()(const LvalueReferenceType* type) {}
104+
105+ void operator ()(const RvalueReferenceType* type) {}
106+
107+ void operator ()(const FunctionType* type) {}
108+
109+ void operator ()(const ClassType* type) {}
110+
111+ void operator ()(const EnumType* type) {}
112+
113+ void operator ()(const ScopedEnumType* type) {}
114+
115+ void operator ()(const MemberObjectPointerType* type) {}
116+
117+ void operator ()(const MemberFunctionPointerType* type) {}
118+
119+ void operator ()(const NamespaceType* type) {}
120+
121+ void operator ()(const TypeParameterType* type) {}
122+
123+ void operator ()(const TemplateTypeParameterType* type) {}
124+
125+ void operator ()(const UnresolvedNameType* type) {}
126+
127+ void operator ()(const UnresolvedBoundedArrayType* type) {}
128+
129+ void operator ()(const UnresolvedUnderlyingType* type) {}
130+
131+ void operator ()(const OverloadSetType* type) {}
132+
133+ void operator ()(const BuiltinVaListType* type) {}
134+ };
135+
136+ struct ExternalNameEncoder ::SymbolVisitor {
137+ ExternalNameEncoder& encoder;
138+
139+ void operator ()(NamespaceSymbol* symbol) {}
140+
141+ void operator ()(ConceptSymbol* symbol) {}
142+
143+ void operator ()(ClassSymbol* symbol) {}
144+
145+ void operator ()(EnumSymbol* symbol) {}
146+
147+ void operator ()(ScopedEnumSymbol* symbol) {}
148+
149+ void operator ()(FunctionSymbol* symbol) {}
150+
151+ void operator ()(TypeAliasSymbol* symbol) {}
152+
153+ void operator ()(VariableSymbol* symbol) {}
154+
155+ void operator ()(FieldSymbol* symbol) {}
156+
157+ void operator ()(ParameterSymbol* symbol) {}
158+
159+ void operator ()(EnumeratorSymbol* symbol) {}
160+
161+ void operator ()(FunctionParametersSymbol* symbol) {}
162+
163+ void operator ()(TemplateParametersSymbol* symbol) {}
164+
165+ void operator ()(BlockSymbol* symbol) {}
166+
167+ void operator ()(LambdaSymbol* symbol) {}
168+
169+ void operator ()(TypeParameterSymbol* symbol) {}
170+
171+ void operator ()(NonTypeParameterSymbol* symbol) {}
172+
173+ void operator ()(TemplateTypeParameterSymbol* symbol) {}
174+
175+ void operator ()(ConstraintTypeParameterSymbol* symbol) {}
176+
177+ void operator ()(OverloadSetSymbol* symbol) {}
178+
179+ void operator ()(BaseClassSymbol* symbol) {}
180+ };
181+
182+ ExternalNameEncoder::ExternalNameEncoder () {}
183+
184+ void ExternalNameEncoder::out (std::string_view s) { externalName_.append (s); }
185+
186+ auto ExternalNameEncoder::encode (Symbol* symbol) -> std::string {
187+ std::string externalName;
188+ if (symbol) {
189+ std::swap (externalName, externalName_);
190+ visit (SymbolVisitor{*this }, symbol);
191+ std::swap (externalName, externalName_);
192+ }
193+ return externalName;
194+ }
195+
196+ auto ExternalNameEncoder::encode (const Type* type) -> std::string {
197+ std::string externalName;
198+ if (type) {
199+ std::swap (externalName, externalName_);
200+ visit (TypeVisitor{*this }, type);
201+ std::swap (externalName, externalName_);
202+ }
203+ return externalName;
204+ }
205+
206+ } // namespace cxx
0 commit comments