Parse cpp_member_variable and cpp_bitfield

This commit is contained in:
Jonathan Müller 2017-02-25 11:17:18 +01:00
commit c87b296d4e
8 changed files with 238 additions and 18 deletions

View file

@ -4,6 +4,7 @@
#include "parse_functions.hpp"
#include <cppast/cpp_member_variable.hpp>
#include <cppast/cpp_variable.hpp>
#include <clang-c/Index.h>
@ -80,3 +81,38 @@ std::unique_ptr<cpp_entity> detail::parse_cpp_variable(const detail::parse_conte
return cpp_variable::build(*context.idx, get_entity_id(cur), name.c_str(), std::move(type),
std::move(default_value), storage_class, is_constexpr);
}
std::unique_ptr<cpp_entity> detail::parse_cpp_member_variable(const detail::parse_context& context,
const CXCursor& cur)
{
DEBUG_ASSERT(cur.kind == CXCursor_FieldDecl, detail::assert_handler{});
auto name = get_cursor_name(cur);
auto type = parse_type(context, clang_getCursorType(cur));
auto is_mutable = clang_CXXField_isMutable(cur) != 0u;
if (clang_Cursor_isBitField(cur))
{
auto no_bits = clang_getFieldDeclBitWidth(cur);
DEBUG_ASSERT(no_bits >= 0, detail::parse_error_handler{}, cur, "invalid number of bits");
if (name.empty())
return cpp_bitfield::build(std::move(type), unsigned(no_bits), is_mutable);
else
return cpp_bitfield::build(*context.idx, get_entity_id(cur), name.c_str(),
std::move(type), unsigned(no_bits), is_mutable);
}
else
{
// parse_default_value() doesn't work here
tokenizer tokenizer(context.tu, context.file, cur);
token_stream stream(tokenizer, cur);
// look for the equal sign, default value starts there
while (!stream.done() && !skip_if(stream, "="))
stream.bump();
auto default_value = parse_raw_expression(context, stream, clang_getCursorType(cur));
return cpp_member_variable::build(*context.idx, get_entity_id(cur), name.c_str(),
std::move(type), std::move(default_value), is_mutable);
}
}