From d2b5a79ba27789afcd4cdedb8cec369975416935 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Tue, 24 Jan 2017 20:47:50 +0100 Subject: [PATCH] Add cpp_member_variable and cpp_bitfield --- include/cppast/cpp_entity_kind.hpp | 2 + include/cppast/cpp_member_variable.hpp | 79 ++++++++++++++++++++++++++ src/cpp_entity_kind.cpp | 6 ++ src/cpp_member_variable.cpp | 19 +++++++ src/visitor.cpp | 2 + 5 files changed, 108 insertions(+) create mode 100644 include/cppast/cpp_member_variable.hpp create mode 100644 src/cpp_member_variable.cpp diff --git a/include/cppast/cpp_entity_kind.hpp b/include/cppast/cpp_entity_kind.hpp index 9e35f1a..0d4e87e 100644 --- a/include/cppast/cpp_entity_kind.hpp +++ b/include/cppast/cpp_entity_kind.hpp @@ -31,6 +31,8 @@ namespace cppast base_class_t, variable_t, + member_variable_t, + bitfield_t, function_parameter_t, function_t, diff --git a/include/cppast/cpp_member_variable.hpp b/include/cppast/cpp_member_variable.hpp new file mode 100644 index 0000000..438cc73 --- /dev/null +++ b/include/cppast/cpp_member_variable.hpp @@ -0,0 +1,79 @@ +// Copyright (C) 2017 Jonathan Müller +// This file is subject to the license terms in the LICENSE file +// found in the top-level directory of this distribution. + +#ifndef CPPAST_CPP_MEMBER_VARIABLE_HPP_INCLUDED +#define CPPAST_CPP_MEMBER_VARIABLE_HPP_INCLUDED + +#include + +namespace cppast +{ + /// Base class for all kinds of member variables. + class cpp_member_variable_base : public cpp_variable_base + { + public: + /// \returns Whether or not the member variable is declared `mutable`. + bool is_mutable() const noexcept + { + return mutable_; + } + + protected: + cpp_member_variable_base(std::string name, std::unique_ptr type, + std::unique_ptr def, bool is_mutable) + : cpp_variable_base(std::move(name), std::move(type), std::move(def)), mutable_(is_mutable) + { + } + + private: + bool mutable_; + }; + + /// A [cppast::cpp_entity]() modelling a C++ member variable. + class cpp_member_variable final : public cpp_member_variable_base + { + public: + /// \returns A newly created and registered member variable. + /// \notes `def` may be `nullptr` in which case there is no member initializer provided. + static std::unique_ptr build(std::string name, + std::unique_ptr type, + std::unique_ptr def, + bool is_mutable) + { + return std::unique_ptr( + new cpp_member_variable(std::move(name), std::move(type), std::move(def), + is_mutable)); + } + + private: + using cpp_member_variable_base::cpp_member_variable_base; + + cpp_entity_kind do_get_entity_kind() const noexcept override; + }; + + /// A [cppast::cpp_entity]() modelling a C++ bitfield. + class cpp_bitfield final : public cpp_member_variable_base + { + public: + /// \returns The number of bits of the bitfield. + unsigned no_bits() const noexcept + { + return bits_; + } + + private: + cpp_bitfield(std::string name, std::unique_ptr type, + std::unique_ptr def, unsigned no_bits, bool is_mutable) + : cpp_member_variable_base(std::move(name), std::move(type), std::move(def), is_mutable), + bits_(no_bits) + { + } + + cpp_entity_kind do_get_entity_kind() const noexcept override; + + unsigned bits_; + }; +} // namespace cppast + +#endif // CPPAST_CPP_MEMBER_VARIABLE_HPP_INCLUDED diff --git a/src/cpp_entity_kind.cpp b/src/cpp_entity_kind.cpp index 27aea47..b4f820f 100644 --- a/src/cpp_entity_kind.cpp +++ b/src/cpp_entity_kind.cpp @@ -42,6 +42,10 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept case cpp_entity_kind::variable_t: return "variable"; + case cpp_entity_kind::member_variable_t: + return "member variable"; + case cpp_entity_kind::bitfield_t: + return "bit field"; case cpp_entity_kind::function_parameter_t: return "function parameter"; @@ -74,6 +78,8 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept case cpp_entity_kind::access_specifier_t: case cpp_entity_kind::base_class_t: case cpp_entity_kind::variable_t: + case cpp_entity_kind::member_variable_t: + case cpp_entity_kind::bitfield_t: case cpp_entity_kind::function_parameter_t: case cpp_entity_kind::function_t: case cpp_entity_kind::count: diff --git a/src/cpp_member_variable.cpp b/src/cpp_member_variable.cpp new file mode 100644 index 0000000..b417773 --- /dev/null +++ b/src/cpp_member_variable.cpp @@ -0,0 +1,19 @@ +// Copyright (C) 2017 Jonathan Müller +// This file is subject to the license terms in the LICENSE file +// found in the top-level directory of this distribution. + +#include + +#include + +using namespace cppast; + +cpp_entity_kind cpp_member_variable::do_get_entity_kind() const noexcept +{ + return cpp_entity_kind::member_variable_t; +} + +cpp_entity_kind cpp_bitfield::do_get_entity_kind() const noexcept +{ + return cpp_entity_kind::bitfield_t; +} diff --git a/src/visitor.cpp b/src/visitor.cpp index 8bfa2f3..fb0b0f5 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -59,6 +59,8 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun case cpp_entity_kind::access_specifier_t: case cpp_entity_kind::base_class_t: case cpp_entity_kind::variable_t: + case cpp_entity_kind::member_variable_t: + case cpp_entity_kind::bitfield_t: case cpp_entity_kind::function_parameter_t: return cb(functor, e, visitor_info::leaf_entity);