Skip to content

Commit c44e0b5

Browse files
committed
Use enum for builtin types
1 parent a3143de commit c44e0b5

13 files changed

+278
-121
lines changed

include/cppast/cpp_type.hpp

Lines changed: 44 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -101,26 +101,63 @@ namespace cppast
101101
std::string name_;
102102
};
103103

104+
/// The C++ builtin types.
105+
enum cpp_builtin_type_kind
106+
{
107+
cpp_void, //< `void`
108+
109+
cpp_bool, //< `bool`
110+
111+
cpp_uchar, //< `unsigned char`
112+
cpp_ushort, //< `unsigned short`
113+
cpp_uint, //< `unsigned int`
114+
cpp_ulong, //< `unsigned long`
115+
cpp_ulonglong, //< `unsigned long long`
116+
cpp_uint128, //< `unsigned __int128`
117+
118+
cpp_schar, //< `signed char`
119+
cpp_short, //< `short`
120+
cpp_int, //< `int`
121+
cpp_long, //< `long`
122+
cpp_longlong, //< `long long`
123+
cpp_int128, //< `__int128`
124+
125+
cpp_float, //< `float`
126+
cpp_double, //< `double`
127+
cpp_longdouble, //< `long double`
128+
cpp_float128, //< `__float128`
129+
130+
cpp_char, //< `char`
131+
cpp_wchar, //< `wchar_t`
132+
cpp_char16, //< `char16_t`
133+
cpp_char32, //< `char32_t`
134+
135+
cpp_nullptr, //< `decltype(nullptr)` aka `std::nullptr_t`
136+
};
137+
138+
/// \returns The string representing the spelling of that type in the source code.
139+
const char* to_string(cpp_builtin_type_kind kind) noexcept;
140+
104141
/// A builtin [cppast::cpp_type]().
105142
///
106143
/// This is one where there is no associated [cppast::cpp_entity]().
107144
class cpp_builtin_type final : public cpp_type
108145
{
109146
public:
110147
/// \returns A newly created builtin type.
111-
static std::unique_ptr<cpp_builtin_type> build(std::string name)
148+
static std::unique_ptr<cpp_builtin_type> build(cpp_builtin_type_kind kind)
112149
{
113-
return std::unique_ptr<cpp_builtin_type>(new cpp_builtin_type(std::move(name)));
150+
return std::unique_ptr<cpp_builtin_type>(new cpp_builtin_type(kind));
114151
}
115152

116-
/// \returns The name of the type.
117-
const std::string& name() const noexcept
153+
/// \returns Which builtin type it is.
154+
cpp_builtin_type_kind builtin_type_kind() const noexcept
118155
{
119-
return name_;
156+
return kind_;
120157
}
121158

122159
private:
123-
cpp_builtin_type(std::string name) : name_(std::move(name))
160+
cpp_builtin_type(cpp_builtin_type_kind kind) : kind_(kind)
124161
{
125162
}
126163

@@ -129,7 +166,7 @@ namespace cppast
129166
return cpp_type_kind::builtin;
130167
}
131168

132-
std::string name_;
169+
cpp_builtin_type_kind kind_;
133170
};
134171

135172
/// \exclude

src/cpp_type.cpp

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,67 @@
1212

1313
using namespace cppast;
1414

15+
const char* cppast::to_string(cpp_builtin_type_kind kind) noexcept
16+
{
17+
switch (kind)
18+
{
19+
case cpp_void:
20+
return "void";
21+
22+
case cpp_bool:
23+
return "bool";
24+
25+
case cpp_uchar:
26+
return "unsigned char";
27+
case cpp_ushort:
28+
return "unsigned short";
29+
case cpp_uint:
30+
return "unsigned int";
31+
case cpp_ulong:
32+
return "unsigned long";
33+
case cpp_ulonglong:
34+
return "unsigned long long";
35+
case cpp_uint128:
36+
return "unsigned __int128";
37+
38+
case cpp_schar:
39+
return "signed char";
40+
case cpp_short:
41+
return "short";
42+
case cpp_int:
43+
return "int";
44+
case cpp_long:
45+
return "long";
46+
case cpp_longlong:
47+
return "long long";
48+
case cpp_int128:
49+
return "__int128";
50+
51+
case cpp_float:
52+
return "float";
53+
case cpp_double:
54+
return "double";
55+
case cpp_longdouble:
56+
return "long double";
57+
case cpp_float128:
58+
return "__float128";
59+
60+
case cpp_char:
61+
return "char";
62+
case cpp_wchar:
63+
return "wchar_t";
64+
case cpp_char16:
65+
return "char16_t";
66+
case cpp_char32:
67+
return "char32_t";
68+
69+
case cpp_nullptr:
70+
return "std::nullptr_t";
71+
}
72+
DEBUG_UNREACHABLE(detail::assert_handler{});
73+
return "__ups";
74+
}
75+
1576
bool detail::cpp_type_ref_predicate::operator()(const cpp_entity& e)
1677
{
1778
return is_type(e.kind());

src/libclang/function_parser.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ namespace
208208
if (!detail::skip_if(stream, "noexcept"))
209209
return nullptr;
210210

211-
auto type = cpp_builtin_type::build("bool");
211+
auto type = cpp_builtin_type::build(cpp_bool);
212212
if (stream.peek().value() != "(")
213213
return cpp_literal_expression::build(std::move(type), "true");
214214

src/libclang/type_parser.cpp

Lines changed: 70 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,7 @@ namespace
196196
{
197197
auto size = clang_getArraySize(type);
198198
if (size != -1)
199-
return cpp_literal_expression::build(cpp_builtin_type::build("unsigned long long"),
199+
return cpp_literal_expression::build(cpp_builtin_type::build(cpp_ulonglong),
200200
std::to_string(size));
201201

202202
auto spelling = get_type_spelling(type);
@@ -218,7 +218,7 @@ namespace
218218

219219
return size_expr.empty() ?
220220
nullptr :
221-
cpp_unexposed_expression::build(cpp_builtin_type::build("unsigned long long"),
221+
cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_ulonglong),
222222
std::string(size_expr.rbegin(),
223223
size_expr.rend()));
224224
}
@@ -503,31 +503,91 @@ namespace
503503
return cpp_unexposed_type::build(get_type_spelling(type).c_str());
504504

505505
case CXType_Void:
506+
return make_leave_type(type,
507+
[](std::string&&) { return cpp_builtin_type::build(cpp_void); });
506508
case CXType_Bool:
507-
case CXType_Char_U:
509+
return make_leave_type(type,
510+
[](std::string&&) { return cpp_builtin_type::build(cpp_bool); });
508511
case CXType_UChar:
509-
case CXType_Char16:
510-
case CXType_Char32:
512+
return make_leave_type(type, [](std::string&&) {
513+
return cpp_builtin_type::build(cpp_uchar);
514+
});
511515
case CXType_UShort:
516+
return make_leave_type(type, [](std::string&&) {
517+
return cpp_builtin_type::build(cpp_ushort);
518+
});
512519
case CXType_UInt:
520+
return make_leave_type(type,
521+
[](std::string&&) { return cpp_builtin_type::build(cpp_uint); });
513522
case CXType_ULong:
523+
return make_leave_type(type, [](std::string&&) {
524+
return cpp_builtin_type::build(cpp_ulong);
525+
});
514526
case CXType_ULongLong:
527+
return make_leave_type(type, [](std::string&&) {
528+
return cpp_builtin_type::build(cpp_ulonglong);
529+
});
515530
case CXType_UInt128:
516-
case CXType_Char_S:
531+
return make_leave_type(type, [](std::string&&) {
532+
return cpp_builtin_type::build(cpp_uint128);
533+
});
517534
case CXType_SChar:
518-
case CXType_WChar:
535+
return make_leave_type(type, [](std::string&&) {
536+
return cpp_builtin_type::build(cpp_schar);
537+
});
519538
case CXType_Short:
539+
return make_leave_type(type, [](std::string&&) {
540+
return cpp_builtin_type::build(cpp_short);
541+
});
520542
case CXType_Int:
543+
return make_leave_type(type,
544+
[](std::string&&) { return cpp_builtin_type::build(cpp_int); });
521545
case CXType_Long:
546+
return make_leave_type(type,
547+
[](std::string&&) { return cpp_builtin_type::build(cpp_long); });
522548
case CXType_LongLong:
549+
return make_leave_type(type, [](std::string&&) {
550+
return cpp_builtin_type::build(cpp_longlong);
551+
});
523552
case CXType_Int128:
553+
return make_leave_type(type, [](std::string&&) {
554+
return cpp_builtin_type::build(cpp_int128);
555+
});
524556
case CXType_Float:
557+
return make_leave_type(type, [](std::string&&) {
558+
return cpp_builtin_type::build(cpp_float);
559+
});
525560
case CXType_Double:
561+
return make_leave_type(type, [](std::string&&) {
562+
return cpp_builtin_type::build(cpp_double);
563+
});
526564
case CXType_LongDouble:
527-
case CXType_NullPtr:
565+
return make_leave_type(type, [](std::string&&) {
566+
return cpp_builtin_type::build(cpp_longdouble);
567+
});
528568
case CXType_Float128:
529-
return make_leave_type(type, [](std::string&& spelling) {
530-
return cpp_builtin_type::build(std::move(spelling));
569+
return make_leave_type(type, [](std::string&&) {
570+
return cpp_builtin_type::build(cpp_float128);
571+
});
572+
case CXType_Char_U:
573+
case CXType_Char_S:
574+
return make_leave_type(type,
575+
[](std::string&&) { return cpp_builtin_type::build(cpp_char); });
576+
case CXType_Char16:
577+
return make_leave_type(type, [](std::string&&) {
578+
return cpp_builtin_type::build(cpp_char16);
579+
});
580+
case CXType_Char32:
581+
return make_leave_type(type, [](std::string&&) {
582+
return cpp_builtin_type::build(cpp_char32);
583+
});
584+
case CXType_WChar:
585+
return make_leave_type(type, [](std::string&&) {
586+
return cpp_builtin_type::build(cpp_wchar);
587+
});
588+
case CXType_NullPtr:
589+
return make_leave_type(type, [](std::string&&) {
590+
return cpp_builtin_type::build(cpp_nullptr);
531591
});
532592

533593
case CXType_Record:

test/cpp_alias_template.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ using h = g<T, a>;
4646
{
4747
check_template_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}});
4848
REQUIRE(equal_types(idx, alias.type_alias().underlying_type(),
49-
*cpp_builtin_type::build("int")));
49+
*cpp_builtin_type::build(cpp_int)));
5050
}
5151
else if (alias.name() == "b")
5252
{

test/cpp_enum.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,7 @@ enum c : int;
5757
REQUIRE(expr.kind() == cpp_expression_kind::unexposed);
5858
REQUIRE(static_cast<const cpp_unexposed_expression&>(expr).expression()
5959
== "42");
60-
REQUIRE(
61-
equal_types(idx, expr.type(), *cpp_builtin_type::build("unsigned int")));
60+
REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build(cpp_uint)));
6261
}
6362
else if (val.name() == "a_d")
6463
{
@@ -69,8 +68,7 @@ enum c : int;
6968
REQUIRE(expr.kind() == cpp_expression_kind::unexposed);
7069
REQUIRE(static_cast<const cpp_unexposed_expression&>(expr).expression()
7170
== "a_a+2");
72-
REQUIRE(
73-
equal_types(idx, expr.type(), *cpp_builtin_type::build("unsigned int")));
71+
REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build(cpp_uint)));
7472
}
7573
else
7674
REQUIRE(false);
@@ -84,8 +82,8 @@ enum c : int;
8482
if (e.is_definition())
8583
{
8684
REQUIRE(e.underlying_type());
87-
REQUIRE(
88-
equal_types(idx, e.underlying_type().value(), *cpp_builtin_type::build("int")));
85+
REQUIRE(equal_types(idx, e.underlying_type().value(),
86+
*cpp_builtin_type::build(cpp_int)));
8987

9088
auto no_vals = 0u;
9189
for (auto& val : e)
@@ -103,7 +101,7 @@ enum c : int;
103101
auto& expr = val.value().value();
104102
REQUIRE(expr.kind() == cpp_expression_kind::literal);
105103
REQUIRE(static_cast<const cpp_literal_expression&>(expr).value() == "42");
106-
REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build("int")));
104+
REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build(cpp_int)));
107105
}
108106
else
109107
REQUIRE(false);
@@ -128,7 +126,8 @@ enum c : int;
128126
REQUIRE(e.is_declaration());
129127
REQUIRE(!e.is_definition());
130128
REQUIRE(!e.is_scoped());
131-
REQUIRE(equal_types(idx, e.underlying_type().value(), *cpp_builtin_type::build("int")));
129+
REQUIRE(
130+
equal_types(idx, e.underlying_type().value(), *cpp_builtin_type::build(cpp_int)));
132131
REQUIRE(count_children(e) == 0u);
133132

134133
auto definition = get_definition(idx, e);

0 commit comments

Comments
 (0)