Skip to content

Commit cdfad0c

Browse files
committed
chore: Set up the external name encoder
Signed-off-by: Roberto Raggi <[email protected]>
1 parent 5128bf2 commit cdfad0c

File tree

5 files changed

+355
-0
lines changed

5 files changed

+355
-0
lines changed
Lines changed: 206 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,206 @@
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
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
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+
#pragma once
22+
23+
#include <cxx/names_fwd.h>
24+
#include <cxx/symbols_fwd.h>
25+
#include <cxx/types_fwd.h>
26+
27+
#include <string_view>
28+
29+
namespace cxx {
30+
31+
class ExternalNameEncoder {
32+
public:
33+
ExternalNameEncoder();
34+
35+
[[nodiscard]] auto encode(Symbol* symbol) -> std::string;
36+
[[nodiscard]] auto encode(const Type* type) -> std::string;
37+
38+
private:
39+
void out(std::string_view s);
40+
41+
struct NameVisitor;
42+
struct TypeVisitor;
43+
struct SymbolVisitor;
44+
45+
private:
46+
std::string externalName_;
47+
};
48+
49+
} // namespace cxx

tests/api_tests/CMakeLists.txt

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
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.
120

221
if (CMAKE_SYSTEM_NAME STREQUAL "WASI")
322
return()

tests/api_tests/test_control.cc

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,22 @@
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.
120

221
#include <cxx/control.h>
322
#include <cxx/literals.h>
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
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/control.h>
22+
#include <cxx/external_name_encoder.h>
23+
#include <cxx/literals.h>
24+
#include <cxx/names.h>
25+
#include <cxx/symbols.h>
26+
#include <cxx/types.h>
27+
#include <gtest/gtest.h>
28+
29+
using namespace cxx;
30+
31+
TEST(ExternalNames, BuiltinTypes) {
32+
Control control;
33+
ExternalNameEncoder encoder;
34+
35+
ASSERT_EQ("v", encoder.encode(control.getVoidType()));
36+
ASSERT_EQ("w", encoder.encode(control.getWideCharType()));
37+
ASSERT_EQ("b", encoder.encode(control.getBoolType()));
38+
ASSERT_EQ("c", encoder.encode(control.getCharType()));
39+
ASSERT_EQ("a", encoder.encode(control.getSignedCharType()));
40+
ASSERT_EQ("h", encoder.encode(control.getUnsignedCharType()));
41+
ASSERT_EQ("s", encoder.encode(control.getShortIntType()));
42+
ASSERT_EQ("t", encoder.encode(control.getUnsignedShortIntType()));
43+
ASSERT_EQ("i", encoder.encode(control.getIntType()));
44+
ASSERT_EQ("j", encoder.encode(control.getUnsignedIntType()));
45+
ASSERT_EQ("l", encoder.encode(control.getLongIntType()));
46+
ASSERT_EQ("m", encoder.encode(control.getUnsignedLongIntType()));
47+
ASSERT_EQ("x", encoder.encode(control.getLongLongIntType()));
48+
ASSERT_EQ("y", encoder.encode(control.getUnsignedLongLongIntType()));
49+
// ASSERT_EQ("n", encoder.encode(control.getInt128Type()));
50+
// ASSERT_EQ("o", encoder.encode(control.getUnsignedInt128Type()));
51+
ASSERT_EQ("f", encoder.encode(control.getFloatType()));
52+
ASSERT_EQ("d", encoder.encode(control.getDoubleType()));
53+
ASSERT_EQ("e", encoder.encode(control.getLongDoubleType()));
54+
// ASSERT_EQ("g", encoder.encode(control.getFloat128Type()));
55+
// ASSERT_EQ("z", encoder.encode(control.getEllipsisType()));
56+
ASSERT_EQ("Di", encoder.encode(control.getChar32Type()));
57+
ASSERT_EQ("Ds", encoder.encode(control.getChar16Type()));
58+
ASSERT_EQ("Du", encoder.encode(control.getChar8Type()));
59+
ASSERT_EQ("Da", encoder.encode(control.getAutoType()));
60+
ASSERT_EQ("Dc", encoder.encode(control.getDecltypeAutoType()));
61+
ASSERT_EQ("Dn", encoder.encode(control.getNullptrType()));
62+
}

0 commit comments

Comments
 (0)