From 664b5a6d4e0e7668b72438421fa9c462ecd7bb41 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sun, 22 Jan 2017 13:06:18 +0100 Subject: [PATCH] Add cpp_variable --- include/cppast/cpp_entity_kind.hpp | 2 ++ include/cppast/cpp_variable.hpp | 57 ++++++++++++++++++++++++++++++ src/cpp_entity_kind.cpp | 4 +++ src/cpp_variable.cpp | 25 +++++++++++++ src/visitor.cpp | 1 + 5 files changed, 89 insertions(+) create mode 100644 include/cppast/cpp_variable.hpp create mode 100644 src/cpp_variable.cpp diff --git a/include/cppast/cpp_entity_kind.hpp b/include/cppast/cpp_entity_kind.hpp index 7ac2974..6eb5055 100644 --- a/include/cppast/cpp_entity_kind.hpp +++ b/include/cppast/cpp_entity_kind.hpp @@ -24,6 +24,8 @@ namespace cppast enum_t, enum_value_t, + variable_t, + count, }; diff --git a/include/cppast/cpp_variable.hpp b/include/cppast/cpp_variable.hpp new file mode 100644 index 0000000..2e75bc5 --- /dev/null +++ b/include/cppast/cpp_variable.hpp @@ -0,0 +1,57 @@ +// 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_VARIABLE_HPP_INCLUDED +#define CPPAST_CPP_VARIABLE_HPP_INCLUDED + +#include + +namespace cppast +{ + /// Storage class and other specifiers for a [cppast::cpp_variable](). + enum cpp_variable_specifiers + { + cpp_var_none = 0, + + cpp_var_static = 1, + cpp_var_extern = 2, + + cpp_var_thread_local = 4, + + cpp_var_constexpr = 8, + }; + + /// A [cppast::cpp_entity]() modelling a C++ variable. + /// \notes This is not a member variable, + /// use [cppast::cpp_member_variable]() for that. + class cpp_variable final : public cpp_variable_base + { + public: + /// \returns A newly created and registered variable. + /// \notes The default value may be `nullptr` indicating no default value. + static std::unique_ptr build(const cpp_entity_index& idx, cpp_entity_id id, + std::string name, std::unique_ptr type, + std::unique_ptr def, + cpp_variable_specifiers spec); + + /// \returns The [cppast::cpp_variable_specifiers]() on that variable. + cpp_variable_specifiers specifiers() const noexcept + { + return specifiers_; + } + + private: + cpp_variable(std::string name, std::unique_ptr type, + std::unique_ptr def, cpp_variable_specifiers spec) + : cpp_variable_base(std::move(name), std::move(type), std::move(def)), specifiers_(spec) + { + } + + cpp_entity_kind do_get_entity_kind() const noexcept override; + + cpp_variable_specifiers specifiers_; + }; +} // namespace cppast + +#endif // CPPAST_CPP_VARIABLE_HPP_INCLUDED diff --git a/src/cpp_entity_kind.cpp b/src/cpp_entity_kind.cpp index b580b76..c7d88c6 100644 --- a/src/cpp_entity_kind.cpp +++ b/src/cpp_entity_kind.cpp @@ -30,6 +30,9 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept case cpp_entity_kind::enum_value_t: return "enum value"; + case cpp_entity_kind::variable_t: + return "variable"; + case cpp_entity_kind::count: break; } @@ -51,6 +54,7 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept case cpp_entity_kind::using_directive_t: case cpp_entity_kind::using_declaration_t: case cpp_entity_kind::enum_value_t: + case cpp_entity_kind::variable_t: case cpp_entity_kind::count: break; } diff --git a/src/cpp_variable.cpp b/src/cpp_variable.cpp new file mode 100644 index 0000000..bdec5fb --- /dev/null +++ b/src/cpp_variable.cpp @@ -0,0 +1,25 @@ +// 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; + +std::unique_ptr cpp_variable::build(const cpp_entity_index& idx, cpp_entity_id id, + std::string name, std::unique_ptr type, + std::unique_ptr def, + cpp_variable_specifiers spec) +{ + auto result = std::unique_ptr( + new cpp_variable(std::move(name), std::move(type), std::move(def), spec)); + idx.register_entity(std::move(id), type_safe::cref(*result)); + return result; +} + +cpp_entity_kind cpp_variable::do_get_entity_kind() const noexcept +{ + return cpp_entity_kind::variable_t; +} diff --git a/src/visitor.cpp b/src/visitor.cpp index e42e11c..4c7cca2 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -47,6 +47,7 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun case cpp_entity_kind::using_declaration_t: case cpp_entity_kind::type_alias_t: case cpp_entity_kind::enum_value_t: + case cpp_entity_kind::variable_t: return cb(functor, e, visitor_info::leaf_entity); case cpp_entity_kind::count: