From 160fa1fe64b47c6f96df175941377ff761f4c1eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jonathan=20M=C3=BCller?= Date: Sun, 22 Jan 2017 13:17:00 +0100 Subject: [PATCH] Add cpp_language_linkage --- include/cppast/cpp_entity_kind.hpp | 2 + include/cppast/cpp_language_linkage.hpp | 54 +++++++++++++++++++++++++ src/cpp_entity_kind.cpp | 4 ++ src/cpp_language_linkage.cpp | 20 +++++++++ src/visitor.cpp | 3 ++ 5 files changed, 83 insertions(+) create mode 100644 include/cppast/cpp_language_linkage.hpp create mode 100644 src/cpp_language_linkage.cpp diff --git a/include/cppast/cpp_entity_kind.hpp b/include/cppast/cpp_entity_kind.hpp index 6eb5055..e0e88a6 100644 --- a/include/cppast/cpp_entity_kind.hpp +++ b/include/cppast/cpp_entity_kind.hpp @@ -14,6 +14,8 @@ namespace cppast { file_t, + language_linkage_t, + namespace_t, namespace_alias_t, using_directive_t, diff --git a/include/cppast/cpp_language_linkage.hpp b/include/cppast/cpp_language_linkage.hpp new file mode 100644 index 0000000..6f5227b --- /dev/null +++ b/include/cppast/cpp_language_linkage.hpp @@ -0,0 +1,54 @@ +// 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_LANGUAGE_LINKAGE_HPP_INCLUDED +#define CPPAST_CPP_LANGUAGE_LINKAGE_HPP_INCLUDED + +#include +#include + +namespace cppast +{ + /// A [cppast::cpp_entity]() modelling a language linkage. + class cpp_language_linkage final : public cpp_entity, + public cpp_entity_container + { + public: + /// Builds a [cppast::cpp_language_linkage](). + class builder + { + public: + /// \effects Sets the name, that is the kind of language linkage. + explicit builder(std::string name) : linkage_(new cpp_language_linkage(std::move(name))) + { + } + + /// \effects Adds an entity to the language linkage. + void add_child(std::unique_ptr child) + { + linkage_->add_child(std::move(child)); + } + + /// \returns The finalized language linkage. + /// \notes It is not registered on purpose as nothing can refer to it. + std::unique_ptr finish() + { + return std::move(linkage_); + } + + private: + std::unique_ptr linkage_; + }; + + /// \returns `true` if the linkage is a block, `false` otherwise. + bool is_block() const noexcept; + + private: + using cpp_entity::cpp_entity; + + cpp_entity_kind do_get_entity_kind() const noexcept override; + }; +} // namespace cppast + +#endif // CPPAST_CPP_LANGUAGE_LINKAGE_HPP_INCLUDED diff --git a/src/cpp_entity_kind.cpp b/src/cpp_entity_kind.cpp index c7d88c6..75bedb7 100644 --- a/src/cpp_entity_kind.cpp +++ b/src/cpp_entity_kind.cpp @@ -13,6 +13,9 @@ const char* cppast::to_string(cpp_entity_kind kind) noexcept case cpp_entity_kind::file_t: return "file"; + case cpp_entity_kind::language_linkage_t: + return "language linkage"; + case cpp_entity_kind::namespace_t: return "namespace"; case cpp_entity_kind::namespace_alias_t: @@ -49,6 +52,7 @@ bool cppast::is_type(cpp_entity_kind kind) noexcept return true; case cpp_entity_kind::file_t: + case cpp_entity_kind::language_linkage_t: case cpp_entity_kind::namespace_t: case cpp_entity_kind::namespace_alias_t: case cpp_entity_kind::using_directive_t: diff --git a/src/cpp_language_linkage.cpp b/src/cpp_language_linkage.cpp new file mode 100644 index 0000000..6cf92ca --- /dev/null +++ b/src/cpp_language_linkage.cpp @@ -0,0 +1,20 @@ +// 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; + +bool cpp_language_linkage::is_block() const noexcept +{ + DEBUG_ASSERT(begin() != end(), detail::assert_handler{}, "empty container"); + return std::next(begin()) != end(); // more than one entity, so block +} + +cpp_entity_kind cpp_language_linkage::do_get_entity_kind() const noexcept +{ + return cpp_entity_kind::language_linkage_t; +} diff --git a/src/visitor.cpp b/src/visitor.cpp index 4c7cca2..9f65191 100644 --- a/src/visitor.cpp +++ b/src/visitor.cpp @@ -8,6 +8,7 @@ #include #include #include +#include #include using namespace cppast; @@ -37,6 +38,8 @@ bool detail::visit(const cpp_entity& e, detail::visitor_callback_t cb, void* fun { case cpp_entity_kind::file_t: return handle_container(e, cb, functor); + case cpp_entity_kind::language_linkage_t: + return handle_container(e, cb, functor); case cpp_entity_kind::namespace_t: return handle_container(e, cb, functor); case cpp_entity_kind::enum_t: