diff --git a/include/cppast/cpp_type.hpp b/include/cppast/cpp_type.hpp index 74a880f..6a0936f 100644 --- a/include/cppast/cpp_type.hpp +++ b/include/cppast/cpp_type.hpp @@ -101,6 +101,43 @@ namespace cppast std::string name_; }; + /// The C++ builtin types. + enum cpp_builtin_type_kind + { + cpp_void, //< `void` + + cpp_bool, //< `bool` + + cpp_uchar, //< `unsigned char` + cpp_ushort, //< `unsigned short` + cpp_uint, //< `unsigned int` + cpp_ulong, //< `unsigned long` + cpp_ulonglong, //< `unsigned long long` + cpp_uint128, //< `unsigned __int128` + + cpp_schar, //< `signed char` + cpp_short, //< `short` + cpp_int, //< `int` + cpp_long, //< `long` + cpp_longlong, //< `long long` + cpp_int128, //< `__int128` + + cpp_float, //< `float` + cpp_double, //< `double` + cpp_longdouble, //< `long double` + cpp_float128, //< `__float128` + + cpp_char, //< `char` + cpp_wchar, //< `wchar_t` + cpp_char16, //< `char16_t` + cpp_char32, //< `char32_t` + + cpp_nullptr, //< `decltype(nullptr)` aka `std::nullptr_t` + }; + + /// \returns The string representing the spelling of that type in the source code. + const char* to_string(cpp_builtin_type_kind kind) noexcept; + /// A builtin [cppast::cpp_type](). /// /// This is one where there is no associated [cppast::cpp_entity](). @@ -108,19 +145,19 @@ namespace cppast { public: /// \returns A newly created builtin type. - static std::unique_ptr build(std::string name) + static std::unique_ptr build(cpp_builtin_type_kind kind) { - return std::unique_ptr(new cpp_builtin_type(std::move(name))); + return std::unique_ptr(new cpp_builtin_type(kind)); } - /// \returns The name of the type. - const std::string& name() const noexcept + /// \returns Which builtin type it is. + cpp_builtin_type_kind builtin_type_kind() const noexcept { - return name_; + return kind_; } private: - cpp_builtin_type(std::string name) : name_(std::move(name)) + cpp_builtin_type(cpp_builtin_type_kind kind) : kind_(kind) { } @@ -129,7 +166,7 @@ namespace cppast return cpp_type_kind::builtin; } - std::string name_; + cpp_builtin_type_kind kind_; }; /// \exclude diff --git a/src/cpp_type.cpp b/src/cpp_type.cpp index 9810256..19d955e 100644 --- a/src/cpp_type.cpp +++ b/src/cpp_type.cpp @@ -12,6 +12,67 @@ using namespace cppast; +const char* cppast::to_string(cpp_builtin_type_kind kind) noexcept +{ + switch (kind) + { + case cpp_void: + return "void"; + + case cpp_bool: + return "bool"; + + case cpp_uchar: + return "unsigned char"; + case cpp_ushort: + return "unsigned short"; + case cpp_uint: + return "unsigned int"; + case cpp_ulong: + return "unsigned long"; + case cpp_ulonglong: + return "unsigned long long"; + case cpp_uint128: + return "unsigned __int128"; + + case cpp_schar: + return "signed char"; + case cpp_short: + return "short"; + case cpp_int: + return "int"; + case cpp_long: + return "long"; + case cpp_longlong: + return "long long"; + case cpp_int128: + return "__int128"; + + case cpp_float: + return "float"; + case cpp_double: + return "double"; + case cpp_longdouble: + return "long double"; + case cpp_float128: + return "__float128"; + + case cpp_char: + return "char"; + case cpp_wchar: + return "wchar_t"; + case cpp_char16: + return "char16_t"; + case cpp_char32: + return "char32_t"; + + case cpp_nullptr: + return "std::nullptr_t"; + } + DEBUG_UNREACHABLE(detail::assert_handler{}); + return "__ups"; +} + bool detail::cpp_type_ref_predicate::operator()(const cpp_entity& e) { return is_type(e.kind()); diff --git a/src/libclang/function_parser.cpp b/src/libclang/function_parser.cpp index d5f9f90..4588252 100644 --- a/src/libclang/function_parser.cpp +++ b/src/libclang/function_parser.cpp @@ -208,7 +208,7 @@ namespace if (!detail::skip_if(stream, "noexcept")) return nullptr; - auto type = cpp_builtin_type::build("bool"); + auto type = cpp_builtin_type::build(cpp_bool); if (stream.peek().value() != "(") return cpp_literal_expression::build(std::move(type), "true"); diff --git a/src/libclang/type_parser.cpp b/src/libclang/type_parser.cpp index 8d1a838..79286f6 100644 --- a/src/libclang/type_parser.cpp +++ b/src/libclang/type_parser.cpp @@ -196,7 +196,7 @@ namespace { auto size = clang_getArraySize(type); if (size != -1) - return cpp_literal_expression::build(cpp_builtin_type::build("unsigned long long"), + return cpp_literal_expression::build(cpp_builtin_type::build(cpp_ulonglong), std::to_string(size)); auto spelling = get_type_spelling(type); @@ -218,7 +218,7 @@ namespace return size_expr.empty() ? nullptr : - cpp_unexposed_expression::build(cpp_builtin_type::build("unsigned long long"), + cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_ulonglong), std::string(size_expr.rbegin(), size_expr.rend())); } @@ -503,31 +503,91 @@ namespace return cpp_unexposed_type::build(get_type_spelling(type).c_str()); case CXType_Void: + return make_leave_type(type, + [](std::string&&) { return cpp_builtin_type::build(cpp_void); }); case CXType_Bool: - case CXType_Char_U: + return make_leave_type(type, + [](std::string&&) { return cpp_builtin_type::build(cpp_bool); }); case CXType_UChar: - case CXType_Char16: - case CXType_Char32: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_uchar); + }); case CXType_UShort: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_ushort); + }); case CXType_UInt: + return make_leave_type(type, + [](std::string&&) { return cpp_builtin_type::build(cpp_uint); }); case CXType_ULong: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_ulong); + }); case CXType_ULongLong: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_ulonglong); + }); case CXType_UInt128: - case CXType_Char_S: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_uint128); + }); case CXType_SChar: - case CXType_WChar: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_schar); + }); case CXType_Short: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_short); + }); case CXType_Int: + return make_leave_type(type, + [](std::string&&) { return cpp_builtin_type::build(cpp_int); }); case CXType_Long: + return make_leave_type(type, + [](std::string&&) { return cpp_builtin_type::build(cpp_long); }); case CXType_LongLong: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_longlong); + }); case CXType_Int128: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_int128); + }); case CXType_Float: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_float); + }); case CXType_Double: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_double); + }); case CXType_LongDouble: - case CXType_NullPtr: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_longdouble); + }); case CXType_Float128: - return make_leave_type(type, [](std::string&& spelling) { - return cpp_builtin_type::build(std::move(spelling)); + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_float128); + }); + case CXType_Char_U: + case CXType_Char_S: + return make_leave_type(type, + [](std::string&&) { return cpp_builtin_type::build(cpp_char); }); + case CXType_Char16: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_char16); + }); + case CXType_Char32: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_char32); + }); + case CXType_WChar: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_wchar); + }); + case CXType_NullPtr: + return make_leave_type(type, [](std::string&&) { + return cpp_builtin_type::build(cpp_nullptr); }); case CXType_Record: diff --git a/test/cpp_alias_template.cpp b/test/cpp_alias_template.cpp index d02f954..e88cb6f 100644 --- a/test/cpp_alias_template.cpp +++ b/test/cpp_alias_template.cpp @@ -46,7 +46,7 @@ using h = g; { check_template_parameters(alias, {{cpp_entity_kind::template_type_parameter_t, "T"}}); REQUIRE(equal_types(idx, alias.type_alias().underlying_type(), - *cpp_builtin_type::build("int"))); + *cpp_builtin_type::build(cpp_int))); } else if (alias.name() == "b") { diff --git a/test/cpp_enum.cpp b/test/cpp_enum.cpp index f219ff0..b59d751 100644 --- a/test/cpp_enum.cpp +++ b/test/cpp_enum.cpp @@ -57,8 +57,7 @@ enum c : int; REQUIRE(expr.kind() == cpp_expression_kind::unexposed); REQUIRE(static_cast(expr).expression() == "42"); - REQUIRE( - equal_types(idx, expr.type(), *cpp_builtin_type::build("unsigned int"))); + REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build(cpp_uint))); } else if (val.name() == "a_d") { @@ -69,8 +68,7 @@ enum c : int; REQUIRE(expr.kind() == cpp_expression_kind::unexposed); REQUIRE(static_cast(expr).expression() == "a_a+2"); - REQUIRE( - equal_types(idx, expr.type(), *cpp_builtin_type::build("unsigned int"))); + REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build(cpp_uint))); } else REQUIRE(false); @@ -84,8 +82,8 @@ enum c : int; if (e.is_definition()) { REQUIRE(e.underlying_type()); - REQUIRE( - equal_types(idx, e.underlying_type().value(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, e.underlying_type().value(), + *cpp_builtin_type::build(cpp_int))); auto no_vals = 0u; for (auto& val : e) @@ -103,7 +101,7 @@ enum c : int; auto& expr = val.value().value(); REQUIRE(expr.kind() == cpp_expression_kind::literal); REQUIRE(static_cast(expr).value() == "42"); - REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, expr.type(), *cpp_builtin_type::build(cpp_int))); } else REQUIRE(false); @@ -128,7 +126,8 @@ enum c : int; REQUIRE(e.is_declaration()); REQUIRE(!e.is_definition()); REQUIRE(!e.is_scoped()); - REQUIRE(equal_types(idx, e.underlying_type().value(), *cpp_builtin_type::build("int"))); + REQUIRE( + equal_types(idx, e.underlying_type().value(), *cpp_builtin_type::build(cpp_int))); REQUIRE(count_children(e) == 0u); auto definition = get_definition(idx, e); diff --git a/test/cpp_function.cpp b/test/cpp_function.cpp index 34b3ef6..0fccf13 100644 --- a/test/cpp_function.cpp +++ b/test/cpp_function.cpp @@ -65,32 +65,32 @@ void ns::l() if (func.name() == "a") { - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("void"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_void))); REQUIRE(count_children(func) == 0u); REQUIRE(!func.is_variadic()); } else if (func.name() == "b") { - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_int))); auto count = 0u; for (auto& param : func) { if (param.name() == "a") { - REQUIRE(equal_types(idx, param.type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, param.type(), *cpp_builtin_type::build(cpp_int))); REQUIRE(!param.default_value()); } else if (param.name() == "b") { - REQUIRE( - equal_types(idx, param.type(), *cpp_pointer_type::build( - cpp_builtin_type::build("float")))); + REQUIRE(equal_types(idx, param.type(), + *cpp_pointer_type::build( + cpp_builtin_type::build(cpp_float)))); REQUIRE(param.default_value()); REQUIRE(equal_expressions(param.default_value().value(), *cpp_unexposed_expression:: build(cpp_pointer_type::build( - cpp_builtin_type::build("float")), + cpp_builtin_type::build(cpp_float)), "nullptr"))); } else @@ -105,10 +105,10 @@ void ns::l() REQUIRE( equal_types(idx, func.return_type(), *cpp_reference_type:: - build(cpp_array_type::build(cpp_builtin_type::build("int"), + build(cpp_array_type::build(cpp_builtin_type::build(cpp_int), cpp_literal_expression:: build(cpp_builtin_type::build( - "unsigned long long"), + cpp_ulonglong), "10")), cpp_ref_lvalue))); @@ -121,7 +121,7 @@ void ns::l() equal_types(idx, param.type(), *cpp_decltype_type::build( cpp_literal_expression::build(cpp_builtin_type::build( - "int"), + cpp_int), "42")))); REQUIRE(!param.default_value()); } @@ -135,7 +135,7 @@ void ns::l() } else if (func.name() == "d" || func.name() == "e" || func.name() == "f") { - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("void"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_void))); REQUIRE(count_children(func) == 0u); REQUIRE(!func.is_variadic()); REQUIRE(func.storage_class() == cpp_storage_class_none); @@ -143,7 +143,7 @@ void ns::l() REQUIRE(func.noexcept_condition()); check_body(func, cpp_function_declaration); - auto bool_t = cpp_builtin_type::build("bool"); + auto bool_t = cpp_builtin_type::build(cpp_bool); if (func.name() == "d") REQUIRE( equal_expressions(func.noexcept_condition().value(), @@ -160,7 +160,7 @@ void ns::l() else if (func.name() == "g" || func.name() == "h" || func.name() == "i" || func.name() == "j") { - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("void"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_void))); REQUIRE(count_children(func) == 0u); REQUIRE(!func.is_variadic()); REQUIRE(!func.noexcept_condition()); @@ -189,7 +189,7 @@ void ns::l() } else if (func.name() == "k" || func.name() == "l" || func.name() == "ns::l") { - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("void"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_void))); REQUIRE(count_children(func) == 0u); REQUIRE(!func.is_variadic()); REQUIRE(!func.noexcept_condition()); @@ -234,7 +234,7 @@ void foo::a() {} if (func.name() == "a" || func.name() == "foo::a") { - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("void"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_void))); REQUIRE(!func.noexcept_condition()); REQUIRE(!func.is_constexpr()); @@ -245,18 +245,18 @@ void foo::a() {} } else if (func.name() == "b") { - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_int))); REQUIRE(func.noexcept_condition()); REQUIRE( equal_expressions(func.noexcept_condition().value(), - *cpp_literal_expression::build(cpp_builtin_type::build("bool"), + *cpp_literal_expression::build(cpp_builtin_type::build(cpp_bool), "true"))); REQUIRE(!func.is_constexpr()); REQUIRE(func.body_kind() == cpp_function_definition); } else if (func.name() == "c") { - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("char"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_char))); REQUIRE(!func.noexcept_condition()); REQUIRE(func.is_constexpr()); REQUIRE(func.body_kind() == cpp_function_deleted); diff --git a/test/cpp_function_template.cpp b/test/cpp_function_template.cpp index d4407b1..f6f9647 100644 --- a/test/cpp_function_template.cpp +++ b/test/cpp_function_template.cpp @@ -178,7 +178,7 @@ d::d(const int&); REQUIRE(tfunc.function().kind() == cpp_entity_kind::function_t); auto& func = static_cast(tfunc.function()); - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_int))); auto count = 0u; for (auto& param : func) @@ -188,7 +188,7 @@ d::d(const int&); equal_types(idx, param.type(), *cpp_reference_type:: build(cpp_cv_qualified_type::build(cpp_builtin_type::build( - "int"), + cpp_int), cpp_cv_const), cpp_ref_lvalue))); } @@ -210,7 +210,7 @@ d::d(const int&); for (auto& param : func) { ++count; - REQUIRE(equal_types(idx, param.type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, param.type(), *cpp_builtin_type::build(cpp_int))); } REQUIRE(count == 1u); } @@ -222,7 +222,7 @@ d::d(const int&); auto& func = static_cast(tfunc.function()); REQUIRE(func.cv_qualifier() == cpp_cv_none); - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_int))); } else if (tfunc.name() == "d::operator int") { @@ -232,7 +232,7 @@ d::d(const int&); auto& func = static_cast(tfunc.function()); REQUIRE(func.cv_qualifier() == cpp_cv_const); - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_int))); } else if (tfunc.name() == "d::d") { @@ -249,7 +249,7 @@ d::d(const int&); equal_types(idx, param.type(), *cpp_reference_type:: build(cpp_cv_qualified_type::build(cpp_builtin_type::build( - "int"), + cpp_int), cpp_cv_const), cpp_ref_lvalue))); } diff --git a/test/cpp_member_function.cpp b/test/cpp_member_function.cpp index ed83d13..8efc67b 100644 --- a/test/cpp_member_function.cpp +++ b/test/cpp_member_function.cpp @@ -42,7 +42,7 @@ struct bar : foo REQUIRE(count_children(func) == 0u); REQUIRE(!func.is_variadic()); REQUIRE(!func.is_constexpr()); - REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build("void"))); + REQUIRE(equal_types(idx, func.return_type(), *cpp_builtin_type::build(cpp_void))); if (func.name() != "b") REQUIRE(!func.noexcept_condition()); if (func.name() != "g" && func.name() != "h") @@ -60,7 +60,7 @@ struct bar : foo REQUIRE(func.noexcept_condition()); REQUIRE( equal_expressions(func.noexcept_condition().value(), - *cpp_literal_expression::build(cpp_builtin_type::build("bool"), + *cpp_literal_expression::build(cpp_builtin_type::build(cpp_bool), "true"))); REQUIRE(func.cv_qualifier() == cpp_cv_none); REQUIRE(func.ref_qualifier() == cpp_ref_none); @@ -162,14 +162,14 @@ struct foo { REQUIRE(op.name() == "operator int&"); REQUIRE(equal_types(idx, op.return_type(), - *cpp_reference_type::build(cpp_builtin_type::build("int"), + *cpp_reference_type::build(cpp_builtin_type::build(cpp_int), cpp_ref_lvalue))); REQUIRE(op.cv_qualifier() == cpp_cv_none); } else if (op.is_explicit() && !op.is_constexpr()) { REQUIRE(op.name() == "operator bool"); - REQUIRE(equal_types(idx, op.return_type(), *cpp_builtin_type::build("bool"))); + REQUIRE(equal_types(idx, op.return_type(), *cpp_builtin_type::build(cpp_bool))); REQUIRE(op.cv_qualifier() == cpp_cv_const); } else if (!op.is_explicit() && op.is_constexpr()) @@ -209,7 +209,7 @@ struct foo REQUIRE(cont.noexcept_condition()); REQUIRE( equal_expressions(cont.noexcept_condition().value(), - *cpp_literal_expression::build(cpp_builtin_type::build("bool"), + *cpp_literal_expression::build(cpp_builtin_type::build(cpp_bool), "true"))); REQUIRE(!cont.is_explicit()); REQUIRE(!cont.is_constexpr()); @@ -275,10 +275,10 @@ struct d : c REQUIRE(!dtor.is_virtual()); REQUIRE(dtor.body_kind() == cpp_function_definition); REQUIRE(dtor.noexcept_condition()); - REQUIRE( - equal_expressions(dtor.noexcept_condition().value(), - *cpp_unexposed_expression::build(cpp_builtin_type::build("bool"), - "false"))); + REQUIRE(equal_expressions(dtor.noexcept_condition().value(), + *cpp_unexposed_expression::build(cpp_builtin_type::build( + cpp_bool), + "false"))); } else if (dtor.name() == "~c") { diff --git a/test/cpp_member_variable.cpp b/test/cpp_member_variable.cpp index e6d9520..820bb93 100644 --- a/test/cpp_member_variable.cpp +++ b/test/cpp_member_variable.cpp @@ -24,18 +24,18 @@ struct foo auto count = test_visit(*file, [&](const cpp_member_variable& var) { if (var.name() == "a") { - auto type = cpp_builtin_type::build("int"); + auto type = cpp_builtin_type::build(cpp_int); REQUIRE(equal_types(idx, var.type(), *type)); REQUIRE(!var.default_value()); REQUIRE(!var.is_mutable()); } else if (var.name() == "b") { - auto type = cpp_builtin_type::build("float"); + auto type = cpp_builtin_type::build(cpp_float); REQUIRE(equal_types(idx, var.type(), *type)); // all initializers are unexposed - auto def = cpp_unexposed_expression::build(cpp_builtin_type::build("float"), "3.14f"); + auto def = cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_float), "3.14f"); REQUIRE(var.default_value()); REQUIRE(equal_expressions(var.default_value().value(), *def)); @@ -43,7 +43,7 @@ struct foo } else if (var.name() == "c") { - auto type = cpp_builtin_type::build("char"); + auto type = cpp_builtin_type::build(cpp_char); REQUIRE(equal_types(idx, var.type(), *type)); REQUIRE(!var.default_value()); REQUIRE(var.is_mutable()); @@ -70,7 +70,7 @@ struct foo auto file = parse({}, "cpp_bitfield.cpp", code); auto count = test_visit(*file, [&](const cpp_bitfield& bf) { REQUIRE(!bf.default_value()); - REQUIRE(equal_types({}, bf.type(), *cpp_builtin_type::build("char"))); + REQUIRE(equal_types({}, bf.type(), *cpp_builtin_type::build(cpp_char))); if (bf.name() == "a") { diff --git a/test/cpp_template_parameter.cpp b/test/cpp_template_parameter.cpp index 9541737..cf4c3de 100644 --- a/test/cpp_template_parameter.cpp +++ b/test/cpp_template_parameter.cpp @@ -37,7 +37,7 @@ using e = void; auto file = parse(idx, "cpp_template_type_parameter.cpp", code); auto count = test_visit(*file, [&](const cpp_alias_template& alias) { REQUIRE(equal_types(idx, alias.type_alias().underlying_type(), - *cpp_builtin_type::build("void"))); + *cpp_builtin_type::build(cpp_void))); for (auto& p : alias.parameters()) { @@ -112,7 +112,7 @@ using d = void; auto file = parse(idx, "cpp_non_type_template_parameter.cpp", code); auto count = test_visit(*file, [&](const cpp_alias_template& alias) { REQUIRE(equal_types(idx, alias.type_alias().underlying_type(), - *cpp_builtin_type::build("void"))); + *cpp_builtin_type::build(cpp_void))); for (auto& p : alias.parameters()) { @@ -122,7 +122,7 @@ using d = void; if (param.name() == "A") { REQUIRE(alias.name() == "a"); - REQUIRE(equal_types(idx, param.type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, param.type(), *cpp_builtin_type::build(cpp_int))); REQUIRE(!param.is_variadic()); REQUIRE(!param.default_value()); } @@ -130,18 +130,18 @@ using d = void; { REQUIRE(alias.name() == "b"); REQUIRE(equal_types(idx, param.type(), - *cpp_pointer_type::build(cpp_builtin_type::build("char")))); + *cpp_pointer_type::build(cpp_builtin_type::build(cpp_char)))); REQUIRE(!param.is_variadic()); REQUIRE(param.default_value()); REQUIRE(equal_expressions(param.default_value().value(), *cpp_literal_expression::build(cpp_builtin_type::build( - "nullptr_t"), + cpp_nullptr), "nullptr"))); } else if (param.name() == "C") { REQUIRE(alias.name() == "c"); - REQUIRE(equal_types(idx, param.type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types(idx, param.type(), *cpp_builtin_type::build(cpp_int))); REQUIRE(param.is_variadic()); REQUIRE(!param.default_value()); } @@ -149,7 +149,7 @@ using d = void; { REQUIRE(alias.name() == "d"); - cpp_function_type::builder builder(cpp_builtin_type::build("void")); + cpp_function_type::builder builder(cpp_builtin_type::build(cpp_void)); builder.is_variadic(); REQUIRE(equal_types(idx, param.type(), *cpp_pointer_type::build(builder.finish()))); @@ -190,7 +190,7 @@ using d = void; auto file = parse(idx, "cpp_template_template_parameter.cpp", code); auto count = test_visit(*file, [&](const cpp_alias_template& alias) { REQUIRE(equal_types(idx, alias.type_alias().underlying_type(), - *cpp_builtin_type::build("void"))); + *cpp_builtin_type::build(cpp_void))); if (alias.name() == "def") return; diff --git a/test/cpp_type_alias.cpp b/test/cpp_type_alias.cpp index 9ef694c..893ead4 100644 --- a/test/cpp_type_alias.cpp +++ b/test/cpp_type_alias.cpp @@ -22,8 +22,8 @@ bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_ switch (parsed.kind()) { case cpp_type_kind::builtin: - return static_cast(parsed).name() - == static_cast(synthesized).name(); + return static_cast(parsed).builtin_type_kind() + == static_cast(synthesized).builtin_type_kind(); case cpp_type_kind::user_defined: { @@ -299,7 +299,7 @@ typedef decltype(0) w; }; auto make_size = [](std::string size, bool literal) -> std::unique_ptr { - auto type = cpp_builtin_type::build("unsigned long long"); + auto type = cpp_builtin_type::build(cpp_ulonglong); if (literal) return cpp_literal_expression::build(std::move(type), std::move(size)); else @@ -311,41 +311,41 @@ typedef decltype(0) w; auto count = test_visit(*file, [&](const cpp_type_alias& alias) { if (alias.name() == "a") { - auto type = cpp_builtin_type::build("int"); + auto type = cpp_builtin_type::build(cpp_int); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "b") { - auto type = add_cv(cpp_builtin_type::build("long double"), cpp_cv_const_volatile); + auto type = add_cv(cpp_builtin_type::build(cpp_longdouble), cpp_cv_const_volatile); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "c") { - auto type = cpp_pointer_type::build(cpp_builtin_type::build("int")); + auto type = cpp_pointer_type::build(cpp_builtin_type::build(cpp_int)); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "d") { - auto type = cpp_pointer_type::build( - add_cv(cpp_builtin_type::build("unsigned int"), cpp_cv_const)); + auto type = + cpp_pointer_type::build(add_cv(cpp_builtin_type::build(cpp_uint), cpp_cv_const)); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "e") { auto type = add_cv(cpp_pointer_type::build( - add_cv(cpp_builtin_type::build("unsigned int"), cpp_cv_const)), + add_cv(cpp_builtin_type::build(cpp_uint), cpp_cv_const)), cpp_cv_volatile); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "f") { - auto type = cpp_reference_type::build(cpp_builtin_type::build("int"), cpp_ref_lvalue); + auto type = cpp_reference_type::build(cpp_builtin_type::build(cpp_int), cpp_ref_lvalue); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "g") { auto type = - cpp_reference_type::build(add_cv(cpp_builtin_type::build("int"), cpp_cv_const), + cpp_reference_type::build(add_cv(cpp_builtin_type::build(cpp_int), cpp_cv_const), cpp_ref_rvalue); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } @@ -369,26 +369,26 @@ typedef decltype(0) w; else if (alias.name() == "k") { auto type = - cpp_array_type::build(cpp_builtin_type::build("int"), make_size("42", true)); + cpp_array_type::build(cpp_builtin_type::build(cpp_int), make_size("42", true)); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "l") { auto type = - cpp_array_type::build(cpp_pointer_type::build(cpp_builtin_type::build("float")), + cpp_array_type::build(cpp_pointer_type::build(cpp_builtin_type::build(cpp_float)), nullptr); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "m") { auto type = - cpp_array_type::build(cpp_builtin_type::build("char"), make_size("42", true)); + cpp_array_type::build(cpp_builtin_type::build(cpp_char), make_size("42", true)); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else if (alias.name() == "n") { - cpp_function_type::builder builder(cpp_builtin_type::build("void")); - builder.add_parameter(cpp_builtin_type::build("int")); + cpp_function_type::builder builder(cpp_builtin_type::build(cpp_void)); + builder.add_parameter(cpp_builtin_type::build(cpp_int)); auto type = cpp_pointer_type::build(builder.finish()); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); @@ -396,9 +396,9 @@ typedef decltype(0) w; else if (alias.name() == "o") { cpp_function_type::builder builder( - cpp_pointer_type::build(cpp_builtin_type::build("char"))); + cpp_pointer_type::build(cpp_builtin_type::build(cpp_char))); builder.add_parameter( - cpp_reference_type::build(add_cv(cpp_builtin_type::build("int"), cpp_cv_const), + cpp_reference_type::build(add_cv(cpp_builtin_type::build(cpp_int), cpp_cv_const), cpp_ref_lvalue)); builder.is_variadic(); auto type = cpp_reference_type::build(builder.finish(), cpp_ref_lvalue); @@ -409,7 +409,7 @@ typedef decltype(0) w; { cpp_function_type::builder builder( cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "n"))); - builder.add_parameter(cpp_builtin_type::build("int")); + builder.add_parameter(cpp_builtin_type::build(cpp_int)); builder.add_parameter( cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "o"))); auto type = cpp_pointer_type::build(builder.finish()); @@ -420,8 +420,8 @@ typedef decltype(0) w; { cpp_member_function_type::builder builder(cpp_user_defined_type::build( cpp_type_ref(cpp_entity_id(""), "foo")), - cpp_builtin_type::build("void")); - builder.add_parameter(cpp_builtin_type::build("int")); + cpp_builtin_type::build(cpp_void)); + builder.add_parameter(cpp_builtin_type::build(cpp_int)); auto type = cpp_pointer_type::build(builder.finish()); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); @@ -434,8 +434,8 @@ typedef decltype(0) w; cpp_ref_lvalue); cpp_member_function_type::builder builder(std::move(obj), - cpp_builtin_type::build("void")); - builder.add_parameter(cpp_builtin_type::build("int")); + cpp_builtin_type::build(cpp_void)); + builder.add_parameter(cpp_builtin_type::build(cpp_int)); builder.is_variadic(); auto type = cpp_pointer_type::build(builder.finish()); @@ -471,7 +471,7 @@ typedef decltype(0) w; else if (alias.name() == "w") { auto type = cpp_decltype_type::build( - cpp_literal_expression::build(cpp_builtin_type::build("int"), "0")); + cpp_literal_expression::build(cpp_builtin_type::build(cpp_int), "0")); REQUIRE(equal_types(idx, alias.underlying_type(), *type)); } else diff --git a/test/cpp_variable.cpp b/test/cpp_variable.cpp index 20af125..9734f69 100644 --- a/test/cpp_variable.cpp +++ b/test/cpp_variable.cpp @@ -72,21 +72,21 @@ const decltype(o)& p = o; auto file = parse(idx, "cpp_variable.cpp", code); - auto int_type = cpp_builtin_type::build("int"); + auto int_type = cpp_builtin_type::build(cpp_int); auto count = test_visit(*file, [&](const cpp_variable& var) { if (var.name() == "a") check_variable(var, *int_type, nullptr, cpp_storage_class_none, false, false); else if (var.name() == "b") - check_variable(var, *cpp_builtin_type::build("unsigned long long"), + check_variable(var, *cpp_builtin_type::build(cpp_ulonglong), // unexposed due to implicit cast, I think type_safe::ref( - *cpp_unexposed_expression::build(cpp_builtin_type::build("int"), + *cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int), "42")), cpp_storage_class_none, false, false); else if (var.name() == "c") - check_variable(var, *cpp_builtin_type::build("float"), + check_variable(var, *cpp_builtin_type::build(cpp_float), type_safe::ref( - *cpp_unexposed_expression::build(cpp_builtin_type::build("float"), + *cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_float), "3.f+0.14f")), cpp_storage_class_none, false, false); else if (var.name() == "d") @@ -99,10 +99,10 @@ const decltype(o)& p = o; check_variable(var, *int_type, nullptr, cpp_storage_class_static | cpp_storage_class_thread_local, false, false); else if (var.name() == "h") - check_variable(var, *cpp_cv_qualified_type::build(cpp_builtin_type::build("int"), + check_variable(var, *cpp_cv_qualified_type::build(cpp_builtin_type::build(cpp_int), cpp_cv_const), type_safe::ref( - *cpp_literal_expression::build(cpp_builtin_type::build("int"), + *cpp_literal_expression::build(cpp_builtin_type::build(cpp_int), "12")), cpp_storage_class_none, true, false); else if (var.name() == "i") @@ -135,7 +135,7 @@ const decltype(o)& p = o; else if (var.name() == "m") check_variable(var, *cpp_auto_type::build(), type_safe::ref( - *cpp_literal_expression::build(cpp_builtin_type::build("int"), + *cpp_literal_expression::build(cpp_builtin_type::build(cpp_int), "128")), cpp_storage_class_none, false, false); else if (var.name() == "n") @@ -144,25 +144,25 @@ const decltype(o)& p = o; cpp_cv_const), cpp_ref_lvalue), type_safe::ref( - *cpp_unexposed_expression::build(cpp_builtin_type::build("int"), + *cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int), "m")), cpp_storage_class_none, false, false); else if (var.name() == "o") - check_variable(var, - *cpp_decltype_type::build( - cpp_literal_expression::build(cpp_builtin_type::build("int"), "0")), + check_variable(var, *cpp_decltype_type::build( + cpp_literal_expression::build(cpp_builtin_type::build(cpp_int), + "0")), nullptr, cpp_storage_class_none, false, false); else if (var.name() == "p") - check_variable(var, - *cpp_reference_type:: - build(cpp_cv_qualified_type:: - build(cpp_decltype_type::build( - cpp_unexposed_expression:: - build(cpp_builtin_type::build("int"), "o")), - cpp_cv_const), - cpp_ref_lvalue), + check_variable(var, *cpp_reference_type:: + build(cpp_cv_qualified_type:: + build(cpp_decltype_type::build( + cpp_unexposed_expression:: + build(cpp_builtin_type::build(cpp_int), + "o")), + cpp_cv_const), + cpp_ref_lvalue), type_safe::ref( - *cpp_unexposed_expression::build(cpp_builtin_type::build("int"), + *cpp_unexposed_expression::build(cpp_builtin_type::build(cpp_int), "o")), cpp_storage_class_none, false, false); else @@ -191,14 +191,14 @@ struct test REQUIRE(is_static(var.storage_class())); if (var.name() == "a") - REQUIRE(equal_types({}, var.type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types({}, var.type(), *cpp_builtin_type::build(cpp_int))); else if (var.name() == "b") REQUIRE(equal_types({}, var.type(), - *cpp_cv_qualified_type::build(cpp_builtin_type::build("int"), + *cpp_cv_qualified_type::build(cpp_builtin_type::build(cpp_int), cpp_cv_const))); else if (var.name() == "c") { - REQUIRE(equal_types({}, var.type(), *cpp_builtin_type::build("int"))); + REQUIRE(equal_types({}, var.type(), *cpp_builtin_type::build(cpp_int))); REQUIRE(is_thread_local(var.storage_class())); } else