Add and parse cpp_auto_type

This commit is contained in:
Jonathan Müller 2017-03-27 12:10:40 +02:00
commit acb2f06bfd
5 changed files with 46 additions and 3 deletions

View file

@ -18,6 +18,8 @@ namespace cppast
builtin, builtin,
user_defined, user_defined,
auto_,
cv_qualified, cv_qualified,
pointer, pointer,
reference, reference,
@ -176,6 +178,25 @@ namespace cppast
cpp_type_ref entity_; cpp_type_ref entity_;
}; };
/// A [cppast::cpp_type]() that isn't given but deduced by `auto`.
class cpp_auto_type final : public cpp_type
{
public:
/// \returns A newly created `auto` type.
static std::unique_ptr<cpp_auto_type> build()
{
return std::unique_ptr<cpp_auto_type>(new cpp_auto_type);
}
private:
cpp_auto_type() = default;
cpp_type_kind do_get_kind() const noexcept override
{
return cpp_type_kind::auto_;
}
};
class cpp_template_parameter_type; class cpp_template_parameter_type;
class cpp_template_instantiation_type; class cpp_template_instantiation_type;

View file

@ -122,6 +122,7 @@ bool cppast::is_valid(const cpp_type& type) noexcept
case cpp_type_kind::builtin: case cpp_type_kind::builtin:
case cpp_type_kind::user_defined: case cpp_type_kind::user_defined:
case cpp_type_kind::auto_:
case cpp_type_kind::template_parameter: case cpp_type_kind::template_parameter:
case cpp_type_kind::template_instantiation: case cpp_type_kind::template_instantiation:
case cpp_type_kind::unexposed: case cpp_type_kind::unexposed:

View file

@ -544,9 +544,8 @@ namespace
case CXType_MemberPointer: case CXType_MemberPointer:
return cpp_pointer_type::build(parse_member_pointee_type(context, cur, type)); return cpp_pointer_type::build(parse_member_pointee_type(context, cur, type));
// TODO: auto/decltype
case CXType_Auto: case CXType_Auto:
break; return make_leave_type(type, [](std::string&&) { return cpp_auto_type::build(); });
} }
DEBUG_UNREACHABLE(detail::assert_handler{}); DEBUG_UNREACHABLE(detail::assert_handler{});

View file

@ -31,6 +31,9 @@ bool equal_types(const cpp_entity_index& idx, const cpp_type& parsed, const cpp_
return equal_ref(idx, user_parsed.entity(), user_synthesized.entity()); return equal_ref(idx, user_parsed.entity(), user_synthesized.entity());
} }
case cpp_type_kind::auto_:
return true;
case cpp_type_kind::cv_qualified: case cpp_type_kind::cv_qualified:
{ {
auto& cv_a = static_cast<const cpp_cv_qualified_type&>(parsed); auto& cv_a = static_cast<const cpp_cv_qualified_type&>(parsed);

View file

@ -30,6 +30,10 @@ struct foo {} i;
const struct bar {} j = bar(); const struct bar {} j = bar();
struct baz {} k{}; struct baz {} k{};
static struct {} l; static struct {} l;
// auto
auto m = 128;
const auto& n = m;
)"; )";
cpp_entity_index idx; cpp_entity_index idx;
@ -122,10 +126,25 @@ static struct {} l;
else if (var.name() == "l") else if (var.name() == "l")
check_variable(var, *cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "")), check_variable(var, *cpp_user_defined_type::build(cpp_type_ref(cpp_entity_id(""), "")),
nullptr, cpp_storage_class_static, false, false); nullptr, cpp_storage_class_static, false, false);
else if (var.name() == "m")
check_variable(var, *cpp_auto_type::build(),
type_safe::ref(
*cpp_literal_expression::build(cpp_builtin_type::build("int"),
"128")),
cpp_storage_class_none, false, false);
else if (var.name() == "n")
check_variable(var, *cpp_reference_type::
build(cpp_cv_qualified_type::build(cpp_auto_type::build(),
cpp_cv_const),
cpp_ref_lvalue),
type_safe::ref(
*cpp_unexposed_expression::build(cpp_builtin_type::build("int"),
"m")),
cpp_storage_class_none, false, false);
else else
REQUIRE(false); REQUIRE(false);
}); });
REQUIRE(count == 12u); REQUIRE(count == 14u);
} }
TEST_CASE("static cpp_variable") TEST_CASE("static cpp_variable")