Parse cpp_member_variable and cpp_bitfield
This commit is contained in:
parent
51bdbca81d
commit
c87b296d4e
8 changed files with 238 additions and 18 deletions
|
|
@ -8,12 +8,51 @@
|
|||
|
||||
using namespace cppast;
|
||||
|
||||
cpp_entity_kind cpp_member_variable::do_get_entity_kind() const noexcept
|
||||
cpp_entity_kind cpp_member_variable::kind() noexcept
|
||||
{
|
||||
return cpp_entity_kind::member_variable_t;
|
||||
}
|
||||
|
||||
cpp_entity_kind cpp_bitfield::do_get_entity_kind() const noexcept
|
||||
std::unique_ptr<cpp_member_variable> cpp_member_variable::build(const cpp_entity_index& idx,
|
||||
cpp_entity_id id, std::string name,
|
||||
std::unique_ptr<cpp_type> type,
|
||||
std::unique_ptr<cpp_expression> def,
|
||||
bool is_mutable)
|
||||
{
|
||||
auto result = std::unique_ptr<cpp_member_variable>(
|
||||
new cpp_member_variable(std::move(name), std::move(type), std::move(def), is_mutable));
|
||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
||||
return result;
|
||||
}
|
||||
|
||||
cpp_entity_kind cpp_member_variable::do_get_entity_kind() const noexcept
|
||||
{
|
||||
return kind();
|
||||
}
|
||||
|
||||
cpp_entity_kind cpp_bitfield::kind() noexcept
|
||||
{
|
||||
return cpp_entity_kind::bitfield_t;
|
||||
}
|
||||
|
||||
std::unique_ptr<cpp_bitfield> cpp_bitfield::build(const cpp_entity_index& idx, cpp_entity_id id,
|
||||
std::string name, std::unique_ptr<cpp_type> type,
|
||||
unsigned no_bits, bool is_mutable)
|
||||
{
|
||||
auto result = std::unique_ptr<cpp_bitfield>(
|
||||
new cpp_bitfield(std::move(name), std::move(type), no_bits, is_mutable));
|
||||
idx.register_entity(std::move(id), type_safe::cref(*result));
|
||||
return result;
|
||||
}
|
||||
|
||||
std::unique_ptr<cpp_bitfield> cpp_bitfield::build(std::unique_ptr<cpp_type> type, unsigned no_bits,
|
||||
bool is_mutable)
|
||||
{
|
||||
return std::unique_ptr<cpp_bitfield>(
|
||||
new cpp_bitfield("", std::move(type), no_bits, is_mutable));
|
||||
}
|
||||
|
||||
cpp_entity_kind cpp_bitfield::do_get_entity_kind() const noexcept
|
||||
{
|
||||
return kind();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -8,6 +8,18 @@
|
|||
|
||||
using namespace cppast;
|
||||
|
||||
namespace
|
||||
{
|
||||
std::string get_expression_str(detail::token_stream& stream)
|
||||
{
|
||||
// just concat everything
|
||||
std::string expr;
|
||||
while (!stream.done())
|
||||
expr += stream.get().c_str();
|
||||
return expr;
|
||||
}
|
||||
}
|
||||
|
||||
std::unique_ptr<cpp_expression> detail::parse_expression(const detail::parse_context& context,
|
||||
const CXCursor& cur)
|
||||
{
|
||||
|
|
@ -17,12 +29,8 @@ std::unique_ptr<cpp_expression> detail::parse_expression(const detail::parse_con
|
|||
detail::tokenizer tokenizer(context.tu, context.file, cur);
|
||||
detail::token_stream stream(tokenizer, cur);
|
||||
|
||||
// just concat everything
|
||||
std::string expr;
|
||||
while (!stream.done())
|
||||
expr += stream.get().c_str();
|
||||
|
||||
auto type = parse_type(context, clang_getCursorType(cur));
|
||||
auto expr = get_expression_str(stream);
|
||||
|
||||
if (kind == CXCursor_CharacterLiteral || kind == CXCursor_CompoundLiteralExpr
|
||||
|| kind == CXCursor_FloatingLiteral || kind == CXCursor_ImaginaryLiteral
|
||||
|
|
@ -32,3 +40,13 @@ std::unique_ptr<cpp_expression> detail::parse_expression(const detail::parse_con
|
|||
else
|
||||
return cpp_unexposed_expression::build(std::move(type), std::move(expr));
|
||||
}
|
||||
|
||||
std::unique_ptr<cpp_expression> detail::parse_raw_expression(const parse_context& context,
|
||||
token_stream& stream,
|
||||
const CXType& type)
|
||||
{
|
||||
if (stream.done())
|
||||
return nullptr;
|
||||
auto expr = get_expression_str(stream);
|
||||
return cpp_unexposed_expression::build(parse_type(context, type), std::move(expr));
|
||||
}
|
||||
|
|
|
|||
|
|
@ -51,6 +51,8 @@ std::unique_ptr<cpp_entity> detail::parse_entity(const detail::parse_context& co
|
|||
|
||||
case CXCursor_VarDecl:
|
||||
return parse_cpp_variable(context, cur);
|
||||
case CXCursor_FieldDecl:
|
||||
return parse_cpp_member_variable(context, cur);
|
||||
|
||||
default:
|
||||
break;
|
||||
|
|
|
|||
|
|
@ -38,6 +38,13 @@ namespace cppast
|
|||
|
||||
std::unique_ptr<cpp_expression> parse_expression(const parse_context& context,
|
||||
const CXCursor& cur);
|
||||
// parse the expression starting at the current token in the stream
|
||||
// and ends at the end of the stream
|
||||
// this is required for situations where there is no expression cursor exposed,
|
||||
// like member initializers
|
||||
std::unique_ptr<cpp_expression> parse_raw_expression(const parse_context& context,
|
||||
token_stream& stream,
|
||||
const CXType& type);
|
||||
|
||||
// parse_entity() dispatches on the cursor type
|
||||
// it calls one of the other parse functions defined elsewhere
|
||||
|
|
@ -65,6 +72,9 @@ namespace cppast
|
|||
|
||||
std::unique_ptr<cpp_entity> parse_cpp_variable(const parse_context& context,
|
||||
const CXCursor& cur);
|
||||
// also parses bitfields
|
||||
std::unique_ptr<cpp_entity> parse_cpp_member_variable(const parse_context& context,
|
||||
const CXCursor& cur);
|
||||
|
||||
std::unique_ptr<cpp_entity> parse_entity(const parse_context& context, const CXCursor& cur);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -143,7 +143,8 @@ namespace
|
|||
}
|
||||
}
|
||||
else if (clang_isExpression(clang_getCursorKind(cur))
|
||||
|| clang_getCursorKind(cur) == CXCursor_CXXBaseSpecifier)
|
||||
|| clang_getCursorKind(cur) == CXCursor_CXXBaseSpecifier
|
||||
|| clang_getCursorKind(cur) == CXCursor_FieldDecl)
|
||||
// need to shrink range by one
|
||||
end = get_next_location(tu, file, end, -1);
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue