Add and parse cpp_decltype_type

This commit is contained in:
Jonathan Müller 2017-03-27 21:36:55 +02:00
commit 6f050bac66
9 changed files with 174 additions and 16 deletions

View file

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

View file

@ -15,17 +15,23 @@ namespace
std::unique_ptr<cpp_function_parameter> parse_parameter(const detail::parse_context& context,
const CXCursor& cur)
{
auto name = detail::get_cursor_name(cur);
auto type = detail::parse_type(context, cur, clang_getCursorType(cur));
auto name = detail::get_cursor_name(cur);
auto type = detail::parse_type(context, cur, clang_getCursorType(cur));
auto is_decltype = type->kind() == cpp_type_kind::decltype_;
std::unique_ptr<cpp_expression> default_value;
detail::visit_children(cur, [&](const CXCursor& child) {
if (!clang_isExpression(clang_getCursorKind(child)))
return;
DEBUG_ASSERT(!default_value, detail::parse_error_handler{}, child,
"unexpected child cursor of function parameter");
default_value = detail::parse_expression(context, child);
else if (is_decltype)
// skip first expression then
is_decltype = false;
else
{
DEBUG_ASSERT(!default_value, detail::parse_error_handler{}, child,
"unexpected child cursor of function parameter");
default_value = detail::parse_expression(context, child);
}
});
if (name.empty())

View file

@ -5,6 +5,7 @@
#include "parse_functions.hpp"
#include <cppast/cpp_array_type.hpp>
#include <cppast/cpp_decltype_type.hpp>
#include <cppast/cpp_expression.hpp>
#include <cppast/cpp_function_type.hpp>
#include <cppast/cpp_template.hpp>
@ -434,6 +435,29 @@ namespace
});
}
std::unique_ptr<cpp_type> try_parse_decltype_type(const detail::parse_context& context,
const CXCursor& cur, const CXType& type)
{
if (clang_isExpression(clang_getCursorKind(cur)))
return nullptr; // don't use decltype here
return make_leave_type(type, [&](std::string&& spelling) -> std::unique_ptr<cpp_type> {
if (!remove_prefix(spelling, "decltype("))
return nullptr;
std::unique_ptr<cpp_expression> expr;
detail::visit_children(cur, [&](const CXCursor& child) {
if (!expr && clang_isExpression(clang_getCursorKind(child)))
// first expression child belongs to the decltype
expr = detail::parse_expression(context, child);
});
DEBUG_ASSERT(expr != nullptr, detail::parse_error_handler{}, cur,
"missing child of cursor");
return cpp_decltype_type::build(std::move(expr));
});
}
std::unique_ptr<cpp_type> parse_type_impl(const detail::parse_context& context,
const CXCursor& cur, const CXType& type)
{
@ -467,6 +491,9 @@ namespace
else if (auto atype = try_parse_array_type(context, cur, type))
// same deal here
return atype;
else if (auto dtype = try_parse_decltype_type(context, cur, type))
// decltype unexposed
return dtype;
else if (auto itype = try_parse_instantiation_type(context, cur, type))
// instantiation unexposed
return itype;
@ -545,7 +572,7 @@ namespace
return cpp_pointer_type::build(parse_member_pointee_type(context, cur, type));
case CXType_Auto:
return make_leave_type(type, [](std::string&&) { return cpp_auto_type::build(); });
return make_leave_type(type, [&](std::string&&) { return cpp_auto_type::build(); });
}
DEBUG_UNREACHABLE(detail::assert_handler{});

View file

@ -15,16 +15,23 @@ using namespace cppast;
namespace
{
std::unique_ptr<cpp_expression> parse_default_value(const detail::parse_context& context,
const CXCursor& cur)
const CXCursor& cur, bool uses_decltype)
{
std::unique_ptr<cpp_expression> expression;
detail::visit_children(cur, [&](const CXCursor& child) {
if (clang_isDeclaration(clang_getCursorKind(child)))
return;
DEBUG_ASSERT(clang_isExpression(child.kind) && !expression,
detail::parse_error_handler{}, cur, "unexpected child cursor of variable");
else if (uses_decltype)
// skip first expression, belongs to the decltype
uses_decltype = false;
else
{
DEBUG_ASSERT(!expression && clang_isExpression(child.kind),
detail::parse_error_handler{}, cur,
"unexpected child cursor of variable");
expression = detail::parse_expression(context, child);
expression = detail::parse_expression(context, child);
}
});
return expression;
}
@ -39,6 +46,7 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_variable(const detail::parse_conte
auto type = parse_type(context, cur, clang_getCursorType(cur));
auto storage_class = get_storage_class(cur);
auto is_constexpr = false;
auto uses_decltype = false;
// just look for thread local or constexpr
// can't appear anywhere else, so good enough
@ -49,11 +57,13 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_variable(const detail::parse_conte
cpp_storage_class_specifiers(storage_class | cpp_storage_class_thread_local);
else if (token.value() == "constexpr")
is_constexpr = true;
else if (token.value() == "decltype")
uses_decltype = true;
std::unique_ptr<cpp_variable> result;
if (clang_isCursorDefinition(cur))
{
auto default_value = parse_default_value(context, cur);
auto default_value = parse_default_value(context, cur, uses_decltype);
result =
cpp_variable::build(*context.idx, get_entity_id(cur), name.c_str(), std::move(type),
std::move(default_value), storage_class, is_constexpr);